gcc/
[official-gcc.git] / gcc / builtins.c
blob67c96df2689b654cb32e50e3399ad72c3979796e
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 rtx result = 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 RESULT.
2046 Set RESULT to wherever the result comes back. */
2047 result = expand_unop (mode, builtin_optab, op0, result, 0);
2049 if (result != 0)
2051 if (errno_set)
2052 expand_errno_check (exp, result);
2054 /* Output the entire sequence. */
2055 insns = get_insns ();
2056 end_sequence ();
2057 emit_insn (insns);
2058 return result;
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, result;
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 result = 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 RESULT.
2155 Set RESULT to wherever the result comes back. */
2156 result = expand_binop (mode, builtin_optab, op0, op1,
2157 result, 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 (result == 0)
2164 end_sequence ();
2165 return expand_call (exp, target, target == const0_rtx);
2168 if (errno_set)
2169 expand_errno_check (exp, result);
2171 /* Output the entire sequence. */
2172 insns = get_insns ();
2173 end_sequence ();
2174 emit_insn (insns);
2176 return result;
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, result;
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 result = 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 RESULT.
2231 Set RESULT to wherever the result comes back. */
2232 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2233 result, 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 (result == 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 result;
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 rtx result = 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 RESULT.
2313 Set RESULT to wherever the result comes back. */
2314 if (builtin_optab == sincos_optab)
2316 int ok;
2318 switch (DECL_FUNCTION_CODE (fndecl))
2320 CASE_FLT_FN (BUILT_IN_SIN):
2321 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2322 break;
2323 CASE_FLT_FN (BUILT_IN_COS):
2324 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2325 break;
2326 default:
2327 gcc_unreachable ();
2329 gcc_assert (ok);
2331 else
2332 result = expand_unop (mode, builtin_optab, op0, result, 0);
2334 if (result != 0)
2336 /* Output the entire sequence. */
2337 insns = get_insns ();
2338 end_sequence ();
2339 emit_insn (insns);
2340 return result;
2343 /* If we were unable to expand via the builtin, stop the sequence
2344 (without outputting the insns) and call to the library function
2345 with the stabilized argument list. */
2346 end_sequence ();
2349 return expand_call (exp, target, target == const0_rtx);
2352 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2353 return an RTL instruction code that implements the functionality.
2354 If that isn't possible or available return CODE_FOR_nothing. */
2356 static enum insn_code
2357 interclass_mathfn_icode (tree arg, tree fndecl)
2359 bool errno_set = false;
2360 optab builtin_optab = unknown_optab;
2361 enum machine_mode mode;
2363 switch (DECL_FUNCTION_CODE (fndecl))
2365 CASE_FLT_FN (BUILT_IN_ILOGB):
2366 errno_set = true; builtin_optab = ilogb_optab; break;
2367 CASE_FLT_FN (BUILT_IN_ISINF):
2368 builtin_optab = isinf_optab; break;
2369 case BUILT_IN_ISNORMAL:
2370 case BUILT_IN_ISFINITE:
2371 CASE_FLT_FN (BUILT_IN_FINITE):
2372 case BUILT_IN_FINITED32:
2373 case BUILT_IN_FINITED64:
2374 case BUILT_IN_FINITED128:
2375 case BUILT_IN_ISINFD32:
2376 case BUILT_IN_ISINFD64:
2377 case BUILT_IN_ISINFD128:
2378 /* These builtins have no optabs (yet). */
2379 break;
2380 default:
2381 gcc_unreachable ();
2384 /* There's no easy way to detect the case we need to set EDOM. */
2385 if (flag_errno_math && errno_set)
2386 return CODE_FOR_nothing;
2388 /* Optab mode depends on the mode of the input argument. */
2389 mode = TYPE_MODE (TREE_TYPE (arg));
2391 if (builtin_optab)
2392 return optab_handler (builtin_optab, mode);
2393 return CODE_FOR_nothing;
2396 /* Expand a call to one of the builtin math functions that operate on
2397 floating point argument and output an integer result (ilogb, isinf,
2398 isnan, etc).
2399 Return 0 if a normal call should be emitted rather than expanding the
2400 function in-line. EXP is the expression that is a call to the builtin
2401 function; if convenient, the result should be placed in TARGET. */
2403 static rtx
2404 expand_builtin_interclass_mathfn (tree exp, rtx target)
2406 enum insn_code icode = CODE_FOR_nothing;
2407 rtx op0;
2408 tree fndecl = get_callee_fndecl (exp);
2409 enum machine_mode mode;
2410 tree arg;
2412 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2413 return NULL_RTX;
2415 arg = CALL_EXPR_ARG (exp, 0);
2416 icode = interclass_mathfn_icode (arg, fndecl);
2417 mode = TYPE_MODE (TREE_TYPE (arg));
2419 if (icode != CODE_FOR_nothing)
2421 struct expand_operand ops[1];
2422 rtx last = get_last_insn ();
2423 tree orig_arg = arg;
2425 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2426 need to expand the argument again. This way, we will not perform
2427 side-effects more the once. */
2428 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2430 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2432 if (mode != GET_MODE (op0))
2433 op0 = convert_to_mode (mode, op0, 0);
2435 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2436 if (maybe_legitimize_operands (icode, 0, 1, ops)
2437 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2438 return ops[0].value;
2440 delete_insns_since (last);
2441 CALL_EXPR_ARG (exp, 0) = orig_arg;
2444 return NULL_RTX;
2447 /* Expand a call to the builtin sincos math function.
2448 Return NULL_RTX if a normal call should be emitted rather than expanding the
2449 function in-line. EXP is the expression that is a call to the builtin
2450 function. */
2452 static rtx
2453 expand_builtin_sincos (tree exp)
2455 rtx op0, op1, op2, target1, target2;
2456 enum machine_mode mode;
2457 tree arg, sinp, cosp;
2458 int result;
2459 location_t loc = EXPR_LOCATION (exp);
2460 tree alias_type, alias_off;
2462 if (!validate_arglist (exp, REAL_TYPE,
2463 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2464 return NULL_RTX;
2466 arg = CALL_EXPR_ARG (exp, 0);
2467 sinp = CALL_EXPR_ARG (exp, 1);
2468 cosp = CALL_EXPR_ARG (exp, 2);
2470 /* Make a suitable register to place result in. */
2471 mode = TYPE_MODE (TREE_TYPE (arg));
2473 /* Check if sincos insn is available, otherwise emit the call. */
2474 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2475 return NULL_RTX;
2477 target1 = gen_reg_rtx (mode);
2478 target2 = gen_reg_rtx (mode);
2480 op0 = expand_normal (arg);
2481 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2482 alias_off = build_int_cst (alias_type, 0);
2483 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2484 sinp, alias_off));
2485 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2486 cosp, alias_off));
2488 /* Compute into target1 and target2.
2489 Set TARGET to wherever the result comes back. */
2490 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2491 gcc_assert (result);
2493 /* Move target1 and target2 to the memory locations indicated
2494 by op1 and op2. */
2495 emit_move_insn (op1, target1);
2496 emit_move_insn (op2, target2);
2498 return const0_rtx;
2501 /* Expand a call to the internal cexpi builtin to the sincos math function.
2502 EXP is the expression that is a call to the builtin function; if convenient,
2503 the result should be placed in TARGET. */
2505 static rtx
2506 expand_builtin_cexpi (tree exp, rtx target)
2508 tree fndecl = get_callee_fndecl (exp);
2509 tree arg, type;
2510 enum machine_mode mode;
2511 rtx op0, op1, op2;
2512 location_t loc = EXPR_LOCATION (exp);
2514 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2515 return NULL_RTX;
2517 arg = CALL_EXPR_ARG (exp, 0);
2518 type = TREE_TYPE (arg);
2519 mode = TYPE_MODE (TREE_TYPE (arg));
2521 /* Try expanding via a sincos optab, fall back to emitting a libcall
2522 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2523 is only generated from sincos, cexp or if we have either of them. */
2524 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2526 op1 = gen_reg_rtx (mode);
2527 op2 = gen_reg_rtx (mode);
2529 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2531 /* Compute into op1 and op2. */
2532 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2534 else if (TARGET_HAS_SINCOS)
2536 tree call, fn = NULL_TREE;
2537 tree top1, top2;
2538 rtx op1a, op2a;
2540 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2541 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2542 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2543 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2544 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2545 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2546 else
2547 gcc_unreachable ();
2549 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2550 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2551 op1a = copy_addr_to_reg (XEXP (op1, 0));
2552 op2a = copy_addr_to_reg (XEXP (op2, 0));
2553 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2554 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2556 /* Make sure not to fold the sincos call again. */
2557 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2558 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2559 call, 3, arg, top1, top2));
2561 else
2563 tree call, fn = NULL_TREE, narg;
2564 tree ctype = build_complex_type (type);
2566 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2567 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2568 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2569 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2570 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2571 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2572 else
2573 gcc_unreachable ();
2575 /* If we don't have a decl for cexp create one. This is the
2576 friendliest fallback if the user calls __builtin_cexpi
2577 without full target C99 function support. */
2578 if (fn == NULL_TREE)
2580 tree fntype;
2581 const char *name = NULL;
2583 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2584 name = "cexpf";
2585 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2586 name = "cexp";
2587 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2588 name = "cexpl";
2590 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2591 fn = build_fn_decl (name, fntype);
2594 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2595 build_real (type, dconst0), arg);
2597 /* Make sure not to fold the cexp call again. */
2598 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2599 return expand_expr (build_call_nary (ctype, call, 1, narg),
2600 target, VOIDmode, EXPAND_NORMAL);
2603 /* Now build the proper return type. */
2604 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2605 make_tree (TREE_TYPE (arg), op2),
2606 make_tree (TREE_TYPE (arg), op1)),
2607 target, VOIDmode, EXPAND_NORMAL);
2610 /* Conveniently construct a function call expression. FNDECL names the
2611 function to be called, N is the number of arguments, and the "..."
2612 parameters are the argument expressions. Unlike build_call_exr
2613 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2615 static tree
2616 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2618 va_list ap;
2619 tree fntype = TREE_TYPE (fndecl);
2620 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2622 va_start (ap, n);
2623 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2624 va_end (ap);
2625 SET_EXPR_LOCATION (fn, loc);
2626 return fn;
2629 /* Expand a call to one of the builtin rounding functions gcc defines
2630 as an extension (lfloor and lceil). As these are gcc extensions we
2631 do not need to worry about setting errno to EDOM.
2632 If expanding via optab fails, lower expression to (int)(floor(x)).
2633 EXP is the expression that is a call to the builtin function;
2634 if convenient, the result should be placed in TARGET. */
2636 static rtx
2637 expand_builtin_int_roundingfn (tree exp, rtx target)
2639 convert_optab builtin_optab;
2640 rtx op0, insns, tmp;
2641 tree fndecl = get_callee_fndecl (exp);
2642 enum built_in_function fallback_fn;
2643 tree fallback_fndecl;
2644 enum machine_mode mode;
2645 tree arg;
2647 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2648 gcc_unreachable ();
2650 arg = CALL_EXPR_ARG (exp, 0);
2652 switch (DECL_FUNCTION_CODE (fndecl))
2654 CASE_FLT_FN (BUILT_IN_ICEIL):
2655 CASE_FLT_FN (BUILT_IN_LCEIL):
2656 CASE_FLT_FN (BUILT_IN_LLCEIL):
2657 builtin_optab = lceil_optab;
2658 fallback_fn = BUILT_IN_CEIL;
2659 break;
2661 CASE_FLT_FN (BUILT_IN_IFLOOR):
2662 CASE_FLT_FN (BUILT_IN_LFLOOR):
2663 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2664 builtin_optab = lfloor_optab;
2665 fallback_fn = BUILT_IN_FLOOR;
2666 break;
2668 default:
2669 gcc_unreachable ();
2672 /* Make a suitable register to place result in. */
2673 mode = TYPE_MODE (TREE_TYPE (exp));
2675 target = gen_reg_rtx (mode);
2677 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2678 need to expand the argument again. This way, we will not perform
2679 side-effects more the once. */
2680 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2682 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2684 start_sequence ();
2686 /* Compute into TARGET. */
2687 if (expand_sfix_optab (target, op0, builtin_optab))
2689 /* Output the entire sequence. */
2690 insns = get_insns ();
2691 end_sequence ();
2692 emit_insn (insns);
2693 return target;
2696 /* If we were unable to expand via the builtin, stop the sequence
2697 (without outputting the insns). */
2698 end_sequence ();
2700 /* Fall back to floating point rounding optab. */
2701 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2703 /* For non-C99 targets we may end up without a fallback fndecl here
2704 if the user called __builtin_lfloor directly. In this case emit
2705 a call to the floor/ceil variants nevertheless. This should result
2706 in the best user experience for not full C99 targets. */
2707 if (fallback_fndecl == NULL_TREE)
2709 tree fntype;
2710 const char *name = NULL;
2712 switch (DECL_FUNCTION_CODE (fndecl))
2714 case BUILT_IN_ICEIL:
2715 case BUILT_IN_LCEIL:
2716 case BUILT_IN_LLCEIL:
2717 name = "ceil";
2718 break;
2719 case BUILT_IN_ICEILF:
2720 case BUILT_IN_LCEILF:
2721 case BUILT_IN_LLCEILF:
2722 name = "ceilf";
2723 break;
2724 case BUILT_IN_ICEILL:
2725 case BUILT_IN_LCEILL:
2726 case BUILT_IN_LLCEILL:
2727 name = "ceill";
2728 break;
2729 case BUILT_IN_IFLOOR:
2730 case BUILT_IN_LFLOOR:
2731 case BUILT_IN_LLFLOOR:
2732 name = "floor";
2733 break;
2734 case BUILT_IN_IFLOORF:
2735 case BUILT_IN_LFLOORF:
2736 case BUILT_IN_LLFLOORF:
2737 name = "floorf";
2738 break;
2739 case BUILT_IN_IFLOORL:
2740 case BUILT_IN_LFLOORL:
2741 case BUILT_IN_LLFLOORL:
2742 name = "floorl";
2743 break;
2744 default:
2745 gcc_unreachable ();
2748 fntype = build_function_type_list (TREE_TYPE (arg),
2749 TREE_TYPE (arg), NULL_TREE);
2750 fallback_fndecl = build_fn_decl (name, fntype);
2753 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2755 tmp = expand_normal (exp);
2757 /* Truncate the result of floating point optab to integer
2758 via expand_fix (). */
2759 target = gen_reg_rtx (mode);
2760 expand_fix (target, tmp, 0);
2762 return target;
2765 /* Expand a call to one of the builtin math functions doing integer
2766 conversion (lrint).
2767 Return 0 if a normal call should be emitted rather than expanding the
2768 function in-line. EXP is the expression that is a call to the builtin
2769 function; if convenient, the result should be placed in TARGET. */
2771 static rtx
2772 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2774 convert_optab builtin_optab;
2775 rtx op0, insns;
2776 tree fndecl = get_callee_fndecl (exp);
2777 tree arg;
2778 enum machine_mode mode;
2779 enum built_in_function fallback_fn = BUILT_IN_NONE;
2781 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2782 gcc_unreachable ();
2784 arg = CALL_EXPR_ARG (exp, 0);
2786 switch (DECL_FUNCTION_CODE (fndecl))
2788 CASE_FLT_FN (BUILT_IN_IRINT):
2789 fallback_fn = BUILT_IN_LRINT;
2790 /* FALLTHRU */
2791 CASE_FLT_FN (BUILT_IN_LRINT):
2792 CASE_FLT_FN (BUILT_IN_LLRINT):
2793 builtin_optab = lrint_optab;
2794 break;
2796 CASE_FLT_FN (BUILT_IN_IROUND):
2797 fallback_fn = BUILT_IN_LROUND;
2798 /* FALLTHRU */
2799 CASE_FLT_FN (BUILT_IN_LROUND):
2800 CASE_FLT_FN (BUILT_IN_LLROUND):
2801 builtin_optab = lround_optab;
2802 break;
2804 default:
2805 gcc_unreachable ();
2808 /* There's no easy way to detect the case we need to set EDOM. */
2809 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2810 return NULL_RTX;
2812 /* Make a suitable register to place result in. */
2813 mode = TYPE_MODE (TREE_TYPE (exp));
2815 /* There's no easy way to detect the case we need to set EDOM. */
2816 if (!flag_errno_math)
2818 rtx result = gen_reg_rtx (mode);
2820 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2821 need to expand the argument again. This way, we will not perform
2822 side-effects more the once. */
2823 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2825 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2827 start_sequence ();
2829 if (expand_sfix_optab (result, op0, builtin_optab))
2831 /* Output the entire sequence. */
2832 insns = get_insns ();
2833 end_sequence ();
2834 emit_insn (insns);
2835 return result;
2838 /* If we were unable to expand via the builtin, stop the sequence
2839 (without outputting the insns) and call to the library function
2840 with the stabilized argument list. */
2841 end_sequence ();
2844 if (fallback_fn != BUILT_IN_NONE)
2846 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2847 targets, (int) round (x) should never be transformed into
2848 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2849 a call to lround in the hope that the target provides at least some
2850 C99 functions. This should result in the best user experience for
2851 not full C99 targets. */
2852 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2853 fallback_fn, 0);
2855 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2856 fallback_fndecl, 1, arg);
2858 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2859 return convert_to_mode (mode, target, 0);
2862 return expand_call (exp, target, target == const0_rtx);
2865 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2866 a normal call should be emitted rather than expanding the function
2867 in-line. EXP is the expression that is a call to the builtin
2868 function; if convenient, the result should be placed in TARGET. */
2870 static rtx
2871 expand_builtin_powi (tree exp, rtx target)
2873 tree arg0, arg1;
2874 rtx op0, op1;
2875 enum machine_mode mode;
2876 enum machine_mode mode2;
2878 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2879 return NULL_RTX;
2881 arg0 = CALL_EXPR_ARG (exp, 0);
2882 arg1 = CALL_EXPR_ARG (exp, 1);
2883 mode = TYPE_MODE (TREE_TYPE (exp));
2885 /* Emit a libcall to libgcc. */
2887 /* Mode of the 2nd argument must match that of an int. */
2888 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2890 if (target == NULL_RTX)
2891 target = gen_reg_rtx (mode);
2893 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2894 if (GET_MODE (op0) != mode)
2895 op0 = convert_to_mode (mode, op0, 0);
2896 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2897 if (GET_MODE (op1) != mode2)
2898 op1 = convert_to_mode (mode2, op1, 0);
2900 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2901 target, LCT_CONST, mode, 2,
2902 op0, mode, op1, mode2);
2904 return target;
2907 /* Expand expression EXP which is a call to the strlen builtin. Return
2908 NULL_RTX if we failed the caller should emit a normal call, otherwise
2909 try to get the result in TARGET, if convenient. */
2911 static rtx
2912 expand_builtin_strlen (tree exp, rtx target,
2913 enum machine_mode target_mode)
2915 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2916 return NULL_RTX;
2917 else
2919 struct expand_operand ops[4];
2920 rtx pat;
2921 tree len;
2922 tree src = CALL_EXPR_ARG (exp, 0);
2923 rtx src_reg, before_strlen;
2924 enum machine_mode insn_mode = target_mode;
2925 enum insn_code icode = CODE_FOR_nothing;
2926 unsigned int align;
2928 /* If the length can be computed at compile-time, return it. */
2929 len = c_strlen (src, 0);
2930 if (len)
2931 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2933 /* If the length can be computed at compile-time and is constant
2934 integer, but there are side-effects in src, evaluate
2935 src for side-effects, then return len.
2936 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2937 can be optimized into: i++; x = 3; */
2938 len = c_strlen (src, 1);
2939 if (len && TREE_CODE (len) == INTEGER_CST)
2941 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2942 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2945 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2947 /* If SRC is not a pointer type, don't do this operation inline. */
2948 if (align == 0)
2949 return NULL_RTX;
2951 /* Bail out if we can't compute strlen in the right mode. */
2952 while (insn_mode != VOIDmode)
2954 icode = optab_handler (strlen_optab, insn_mode);
2955 if (icode != CODE_FOR_nothing)
2956 break;
2958 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2960 if (insn_mode == VOIDmode)
2961 return NULL_RTX;
2963 /* Make a place to hold the source address. We will not expand
2964 the actual source until we are sure that the expansion will
2965 not fail -- there are trees that cannot be expanded twice. */
2966 src_reg = gen_reg_rtx (Pmode);
2968 /* Mark the beginning of the strlen sequence so we can emit the
2969 source operand later. */
2970 before_strlen = get_last_insn ();
2972 create_output_operand (&ops[0], target, insn_mode);
2973 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2974 create_integer_operand (&ops[2], 0);
2975 create_integer_operand (&ops[3], align);
2976 if (!maybe_expand_insn (icode, 4, ops))
2977 return NULL_RTX;
2979 /* Now that we are assured of success, expand the source. */
2980 start_sequence ();
2981 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2982 if (pat != src_reg)
2984 #ifdef POINTERS_EXTEND_UNSIGNED
2985 if (GET_MODE (pat) != Pmode)
2986 pat = convert_to_mode (Pmode, pat,
2987 POINTERS_EXTEND_UNSIGNED);
2988 #endif
2989 emit_move_insn (src_reg, pat);
2991 pat = get_insns ();
2992 end_sequence ();
2994 if (before_strlen)
2995 emit_insn_after (pat, before_strlen);
2996 else
2997 emit_insn_before (pat, get_insns ());
2999 /* Return the value in the proper mode for this function. */
3000 if (GET_MODE (ops[0].value) == target_mode)
3001 target = ops[0].value;
3002 else if (target != 0)
3003 convert_move (target, ops[0].value, 0);
3004 else
3005 target = convert_to_mode (target_mode, ops[0].value, 0);
3007 return target;
3011 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3012 bytes from constant string DATA + OFFSET and return it as target
3013 constant. */
3015 static rtx
3016 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3017 enum machine_mode mode)
3019 const char *str = (const char *) data;
3021 gcc_assert (offset >= 0
3022 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3023 <= strlen (str) + 1));
3025 return c_readstr (str + offset, mode);
3028 /* Expand a call EXP to the memcpy builtin.
3029 Return NULL_RTX if we failed, the caller should emit a normal call,
3030 otherwise try to get the result in TARGET, if convenient (and in
3031 mode MODE if that's convenient). */
3033 static rtx
3034 expand_builtin_memcpy (tree exp, rtx target)
3036 if (!validate_arglist (exp,
3037 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3038 return NULL_RTX;
3039 else
3041 tree dest = CALL_EXPR_ARG (exp, 0);
3042 tree src = CALL_EXPR_ARG (exp, 1);
3043 tree len = CALL_EXPR_ARG (exp, 2);
3044 const char *src_str;
3045 unsigned int src_align = get_pointer_alignment (src);
3046 unsigned int dest_align = get_pointer_alignment (dest);
3047 rtx dest_mem, src_mem, dest_addr, len_rtx;
3048 HOST_WIDE_INT expected_size = -1;
3049 unsigned int expected_align = 0;
3051 /* If DEST is not a pointer type, call the normal function. */
3052 if (dest_align == 0)
3053 return NULL_RTX;
3055 /* If either SRC is not a pointer type, don't do this
3056 operation in-line. */
3057 if (src_align == 0)
3058 return NULL_RTX;
3060 if (currently_expanding_gimple_stmt)
3061 stringop_block_profile (currently_expanding_gimple_stmt,
3062 &expected_align, &expected_size);
3064 if (expected_align < dest_align)
3065 expected_align = dest_align;
3066 dest_mem = get_memory_rtx (dest, len);
3067 set_mem_align (dest_mem, dest_align);
3068 len_rtx = expand_normal (len);
3069 src_str = c_getstr (src);
3071 /* If SRC is a string constant and block move would be done
3072 by pieces, we can avoid loading the string from memory
3073 and only stored the computed constants. */
3074 if (src_str
3075 && CONST_INT_P (len_rtx)
3076 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3077 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3078 CONST_CAST (char *, src_str),
3079 dest_align, false))
3081 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3082 builtin_memcpy_read_str,
3083 CONST_CAST (char *, src_str),
3084 dest_align, false, 0);
3085 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3086 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3087 return dest_mem;
3090 src_mem = get_memory_rtx (src, len);
3091 set_mem_align (src_mem, src_align);
3093 /* Copy word part most expediently. */
3094 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3095 CALL_EXPR_TAILCALL (exp)
3096 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3097 expected_align, expected_size);
3099 if (dest_addr == 0)
3101 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3102 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3104 return dest_addr;
3108 /* Expand a call EXP to the mempcpy builtin.
3109 Return NULL_RTX if we failed; the caller should emit a normal call,
3110 otherwise try to get the result in TARGET, if convenient (and in
3111 mode MODE if that's convenient). If ENDP is 0 return the
3112 destination pointer, if ENDP is 1 return the end pointer ala
3113 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3114 stpcpy. */
3116 static rtx
3117 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3119 if (!validate_arglist (exp,
3120 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3121 return NULL_RTX;
3122 else
3124 tree dest = CALL_EXPR_ARG (exp, 0);
3125 tree src = CALL_EXPR_ARG (exp, 1);
3126 tree len = CALL_EXPR_ARG (exp, 2);
3127 return expand_builtin_mempcpy_args (dest, src, len,
3128 target, mode, /*endp=*/ 1);
3132 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3133 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3134 so that this can also be called without constructing an actual CALL_EXPR.
3135 The other arguments and return value are the same as for
3136 expand_builtin_mempcpy. */
3138 static rtx
3139 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3140 rtx target, enum machine_mode mode, int endp)
3142 /* If return value is ignored, transform mempcpy into memcpy. */
3143 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3145 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3146 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3147 dest, src, len);
3148 return expand_expr (result, target, mode, EXPAND_NORMAL);
3150 else
3152 const char *src_str;
3153 unsigned int src_align = get_pointer_alignment (src);
3154 unsigned int dest_align = get_pointer_alignment (dest);
3155 rtx dest_mem, src_mem, len_rtx;
3157 /* If either SRC or DEST is not a pointer type, don't do this
3158 operation in-line. */
3159 if (dest_align == 0 || src_align == 0)
3160 return NULL_RTX;
3162 /* If LEN is not constant, call the normal function. */
3163 if (! host_integerp (len, 1))
3164 return NULL_RTX;
3166 len_rtx = expand_normal (len);
3167 src_str = c_getstr (src);
3169 /* If SRC is a string constant and block move would be done
3170 by pieces, we can avoid loading the string from memory
3171 and only stored the computed constants. */
3172 if (src_str
3173 && CONST_INT_P (len_rtx)
3174 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3175 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3176 CONST_CAST (char *, src_str),
3177 dest_align, false))
3179 dest_mem = get_memory_rtx (dest, len);
3180 set_mem_align (dest_mem, dest_align);
3181 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3182 builtin_memcpy_read_str,
3183 CONST_CAST (char *, src_str),
3184 dest_align, false, endp);
3185 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3186 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3187 return dest_mem;
3190 if (CONST_INT_P (len_rtx)
3191 && can_move_by_pieces (INTVAL (len_rtx),
3192 MIN (dest_align, src_align)))
3194 dest_mem = get_memory_rtx (dest, len);
3195 set_mem_align (dest_mem, dest_align);
3196 src_mem = get_memory_rtx (src, len);
3197 set_mem_align (src_mem, src_align);
3198 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3199 MIN (dest_align, src_align), endp);
3200 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3201 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3202 return dest_mem;
3205 return NULL_RTX;
3209 #ifndef HAVE_movstr
3210 # define HAVE_movstr 0
3211 # define CODE_FOR_movstr CODE_FOR_nothing
3212 #endif
3214 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3215 we failed, the caller should emit a normal call, otherwise try to
3216 get the result in TARGET, if convenient. If ENDP is 0 return the
3217 destination pointer, if ENDP is 1 return the end pointer ala
3218 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3219 stpcpy. */
3221 static rtx
3222 expand_movstr (tree dest, tree src, rtx target, int endp)
3224 struct expand_operand ops[3];
3225 rtx dest_mem;
3226 rtx src_mem;
3228 if (!HAVE_movstr)
3229 return NULL_RTX;
3231 dest_mem = get_memory_rtx (dest, NULL);
3232 src_mem = get_memory_rtx (src, NULL);
3233 if (!endp)
3235 target = force_reg (Pmode, XEXP (dest_mem, 0));
3236 dest_mem = replace_equiv_address (dest_mem, target);
3239 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3240 create_fixed_operand (&ops[1], dest_mem);
3241 create_fixed_operand (&ops[2], src_mem);
3242 expand_insn (CODE_FOR_movstr, 3, ops);
3244 if (endp && target != const0_rtx)
3246 target = ops[0].value;
3247 /* movstr is supposed to set end to the address of the NUL
3248 terminator. If the caller requested a mempcpy-like return value,
3249 adjust it. */
3250 if (endp == 1)
3252 rtx tem = plus_constant (GET_MODE (target),
3253 gen_lowpart (GET_MODE (target), target), 1);
3254 emit_move_insn (target, force_operand (tem, NULL_RTX));
3257 return target;
3260 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3261 NULL_RTX if we failed the caller should emit a normal call, otherwise
3262 try to get the result in TARGET, if convenient (and in mode MODE if that's
3263 convenient). */
3265 static rtx
3266 expand_builtin_strcpy (tree exp, rtx target)
3268 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3270 tree dest = CALL_EXPR_ARG (exp, 0);
3271 tree src = CALL_EXPR_ARG (exp, 1);
3272 return expand_builtin_strcpy_args (dest, src, target);
3274 return NULL_RTX;
3277 /* Helper function to do the actual work for expand_builtin_strcpy. The
3278 arguments to the builtin_strcpy call DEST and SRC are broken out
3279 so that this can also be called without constructing an actual CALL_EXPR.
3280 The other arguments and return value are the same as for
3281 expand_builtin_strcpy. */
3283 static rtx
3284 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3286 return expand_movstr (dest, src, target, /*endp=*/0);
3289 /* Expand a call EXP to the stpcpy builtin.
3290 Return NULL_RTX if we failed the caller should emit a normal call,
3291 otherwise try to get the result in TARGET, if convenient (and in
3292 mode MODE if that's convenient). */
3294 static rtx
3295 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3297 tree dst, src;
3298 location_t loc = EXPR_LOCATION (exp);
3300 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3301 return NULL_RTX;
3303 dst = CALL_EXPR_ARG (exp, 0);
3304 src = CALL_EXPR_ARG (exp, 1);
3306 /* If return value is ignored, transform stpcpy into strcpy. */
3307 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3309 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3310 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3311 return expand_expr (result, target, mode, EXPAND_NORMAL);
3313 else
3315 tree len, lenp1;
3316 rtx ret;
3318 /* Ensure we get an actual string whose length can be evaluated at
3319 compile-time, not an expression containing a string. This is
3320 because the latter will potentially produce pessimized code
3321 when used to produce the return value. */
3322 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3323 return expand_movstr (dst, src, target, /*endp=*/2);
3325 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3326 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3327 target, mode, /*endp=*/2);
3329 if (ret)
3330 return ret;
3332 if (TREE_CODE (len) == INTEGER_CST)
3334 rtx len_rtx = expand_normal (len);
3336 if (CONST_INT_P (len_rtx))
3338 ret = expand_builtin_strcpy_args (dst, src, target);
3340 if (ret)
3342 if (! target)
3344 if (mode != VOIDmode)
3345 target = gen_reg_rtx (mode);
3346 else
3347 target = gen_reg_rtx (GET_MODE (ret));
3349 if (GET_MODE (target) != GET_MODE (ret))
3350 ret = gen_lowpart (GET_MODE (target), ret);
3352 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3353 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3354 gcc_assert (ret);
3356 return target;
3361 return expand_movstr (dst, src, target, /*endp=*/2);
3365 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3366 bytes from constant string DATA + OFFSET and return it as target
3367 constant. */
3370 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3371 enum machine_mode mode)
3373 const char *str = (const char *) data;
3375 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3376 return const0_rtx;
3378 return c_readstr (str + offset, mode);
3381 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3382 NULL_RTX if we failed the caller should emit a normal call. */
3384 static rtx
3385 expand_builtin_strncpy (tree exp, rtx target)
3387 location_t loc = EXPR_LOCATION (exp);
3389 if (validate_arglist (exp,
3390 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3392 tree dest = CALL_EXPR_ARG (exp, 0);
3393 tree src = CALL_EXPR_ARG (exp, 1);
3394 tree len = CALL_EXPR_ARG (exp, 2);
3395 tree slen = c_strlen (src, 1);
3397 /* We must be passed a constant len and src parameter. */
3398 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3399 return NULL_RTX;
3401 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3403 /* We're required to pad with trailing zeros if the requested
3404 len is greater than strlen(s2)+1. In that case try to
3405 use store_by_pieces, if it fails, punt. */
3406 if (tree_int_cst_lt (slen, len))
3408 unsigned int dest_align = get_pointer_alignment (dest);
3409 const char *p = c_getstr (src);
3410 rtx dest_mem;
3412 if (!p || dest_align == 0 || !host_integerp (len, 1)
3413 || !can_store_by_pieces (tree_low_cst (len, 1),
3414 builtin_strncpy_read_str,
3415 CONST_CAST (char *, p),
3416 dest_align, false))
3417 return NULL_RTX;
3419 dest_mem = get_memory_rtx (dest, len);
3420 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3421 builtin_strncpy_read_str,
3422 CONST_CAST (char *, p), dest_align, false, 0);
3423 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3424 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3425 return dest_mem;
3428 return NULL_RTX;
3431 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3432 bytes from constant string DATA + OFFSET and return it as target
3433 constant. */
3436 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3437 enum machine_mode mode)
3439 const char *c = (const char *) data;
3440 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3442 memset (p, *c, GET_MODE_SIZE (mode));
3444 return c_readstr (p, mode);
3447 /* Callback routine for store_by_pieces. Return the RTL of a register
3448 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3449 char value given in the RTL register data. For example, if mode is
3450 4 bytes wide, return the RTL for 0x01010101*data. */
3452 static rtx
3453 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3454 enum machine_mode mode)
3456 rtx target, coeff;
3457 size_t size;
3458 char *p;
3460 size = GET_MODE_SIZE (mode);
3461 if (size == 1)
3462 return (rtx) data;
3464 p = XALLOCAVEC (char, size);
3465 memset (p, 1, size);
3466 coeff = c_readstr (p, mode);
3468 target = convert_to_mode (mode, (rtx) data, 1);
3469 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3470 return force_reg (mode, target);
3473 /* Expand expression EXP, which is a call to the memset builtin. Return
3474 NULL_RTX if we failed the caller should emit a normal call, otherwise
3475 try to get the result in TARGET, if convenient (and in mode MODE if that's
3476 convenient). */
3478 static rtx
3479 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3481 if (!validate_arglist (exp,
3482 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3483 return NULL_RTX;
3484 else
3486 tree dest = CALL_EXPR_ARG (exp, 0);
3487 tree val = CALL_EXPR_ARG (exp, 1);
3488 tree len = CALL_EXPR_ARG (exp, 2);
3489 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3493 /* Helper function to do the actual work for expand_builtin_memset. The
3494 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3495 so that this can also be called without constructing an actual CALL_EXPR.
3496 The other arguments and return value are the same as for
3497 expand_builtin_memset. */
3499 static rtx
3500 expand_builtin_memset_args (tree dest, tree val, tree len,
3501 rtx target, enum machine_mode mode, tree orig_exp)
3503 tree fndecl, fn;
3504 enum built_in_function fcode;
3505 enum machine_mode val_mode;
3506 char c;
3507 unsigned int dest_align;
3508 rtx dest_mem, dest_addr, len_rtx;
3509 HOST_WIDE_INT expected_size = -1;
3510 unsigned int expected_align = 0;
3512 dest_align = get_pointer_alignment (dest);
3514 /* If DEST is not a pointer type, don't do this operation in-line. */
3515 if (dest_align == 0)
3516 return NULL_RTX;
3518 if (currently_expanding_gimple_stmt)
3519 stringop_block_profile (currently_expanding_gimple_stmt,
3520 &expected_align, &expected_size);
3522 if (expected_align < dest_align)
3523 expected_align = dest_align;
3525 /* If the LEN parameter is zero, return DEST. */
3526 if (integer_zerop (len))
3528 /* Evaluate and ignore VAL in case it has side-effects. */
3529 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3530 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3533 /* Stabilize the arguments in case we fail. */
3534 dest = builtin_save_expr (dest);
3535 val = builtin_save_expr (val);
3536 len = builtin_save_expr (len);
3538 len_rtx = expand_normal (len);
3539 dest_mem = get_memory_rtx (dest, len);
3540 val_mode = TYPE_MODE (unsigned_char_type_node);
3542 if (TREE_CODE (val) != INTEGER_CST)
3544 rtx val_rtx;
3546 val_rtx = expand_normal (val);
3547 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3549 /* Assume that we can memset by pieces if we can store
3550 * the coefficients by pieces (in the required modes).
3551 * We can't pass builtin_memset_gen_str as that emits RTL. */
3552 c = 1;
3553 if (host_integerp (len, 1)
3554 && can_store_by_pieces (tree_low_cst (len, 1),
3555 builtin_memset_read_str, &c, dest_align,
3556 true))
3558 val_rtx = force_reg (val_mode, val_rtx);
3559 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3560 builtin_memset_gen_str, val_rtx, dest_align,
3561 true, 0);
3563 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3564 dest_align, expected_align,
3565 expected_size))
3566 goto do_libcall;
3568 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3569 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3570 return dest_mem;
3573 if (target_char_cast (val, &c))
3574 goto do_libcall;
3576 if (c)
3578 if (host_integerp (len, 1)
3579 && can_store_by_pieces (tree_low_cst (len, 1),
3580 builtin_memset_read_str, &c, dest_align,
3581 true))
3582 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3583 builtin_memset_read_str, &c, dest_align, true, 0);
3584 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3585 gen_int_mode (c, val_mode),
3586 dest_align, expected_align,
3587 expected_size))
3588 goto do_libcall;
3590 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3591 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3592 return dest_mem;
3595 set_mem_align (dest_mem, dest_align);
3596 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3597 CALL_EXPR_TAILCALL (orig_exp)
3598 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3599 expected_align, expected_size);
3601 if (dest_addr == 0)
3603 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3604 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3607 return dest_addr;
3609 do_libcall:
3610 fndecl = get_callee_fndecl (orig_exp);
3611 fcode = DECL_FUNCTION_CODE (fndecl);
3612 if (fcode == BUILT_IN_MEMSET)
3613 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3614 dest, val, len);
3615 else if (fcode == BUILT_IN_BZERO)
3616 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3617 dest, len);
3618 else
3619 gcc_unreachable ();
3620 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3621 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3622 return expand_call (fn, target, target == const0_rtx);
3625 /* Expand expression EXP, which is a call to the bzero builtin. Return
3626 NULL_RTX if we failed the caller should emit a normal call. */
3628 static rtx
3629 expand_builtin_bzero (tree exp)
3631 tree dest, size;
3632 location_t loc = EXPR_LOCATION (exp);
3634 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3635 return NULL_RTX;
3637 dest = CALL_EXPR_ARG (exp, 0);
3638 size = CALL_EXPR_ARG (exp, 1);
3640 /* New argument list transforming bzero(ptr x, int y) to
3641 memset(ptr x, int 0, size_t y). This is done this way
3642 so that if it isn't expanded inline, we fallback to
3643 calling bzero instead of memset. */
3645 return expand_builtin_memset_args (dest, integer_zero_node,
3646 fold_convert_loc (loc,
3647 size_type_node, size),
3648 const0_rtx, VOIDmode, exp);
3651 /* Expand expression EXP, which is a call to the memcmp built-in function.
3652 Return NULL_RTX if we failed and the caller should emit a normal call,
3653 otherwise try to get the result in TARGET, if convenient (and in mode
3654 MODE, if that's convenient). */
3656 static rtx
3657 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3658 ATTRIBUTE_UNUSED enum machine_mode mode)
3660 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3662 if (!validate_arglist (exp,
3663 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3664 return NULL_RTX;
3666 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3667 implementing memcmp because it will stop if it encounters two
3668 zero bytes. */
3669 #if defined HAVE_cmpmemsi
3671 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3672 rtx result;
3673 rtx insn;
3674 tree arg1 = CALL_EXPR_ARG (exp, 0);
3675 tree arg2 = CALL_EXPR_ARG (exp, 1);
3676 tree len = CALL_EXPR_ARG (exp, 2);
3678 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3679 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3680 enum machine_mode insn_mode;
3682 if (HAVE_cmpmemsi)
3683 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3684 else
3685 return NULL_RTX;
3687 /* If we don't have POINTER_TYPE, call the function. */
3688 if (arg1_align == 0 || arg2_align == 0)
3689 return NULL_RTX;
3691 /* Make a place to write the result of the instruction. */
3692 result = target;
3693 if (! (result != 0
3694 && REG_P (result) && GET_MODE (result) == insn_mode
3695 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3696 result = gen_reg_rtx (insn_mode);
3698 arg1_rtx = get_memory_rtx (arg1, len);
3699 arg2_rtx = get_memory_rtx (arg2, len);
3700 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3702 /* Set MEM_SIZE as appropriate. */
3703 if (CONST_INT_P (arg3_rtx))
3705 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3706 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3709 if (HAVE_cmpmemsi)
3710 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3711 GEN_INT (MIN (arg1_align, arg2_align)));
3712 else
3713 gcc_unreachable ();
3715 if (insn)
3716 emit_insn (insn);
3717 else
3718 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3719 TYPE_MODE (integer_type_node), 3,
3720 XEXP (arg1_rtx, 0), Pmode,
3721 XEXP (arg2_rtx, 0), Pmode,
3722 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3723 TYPE_UNSIGNED (sizetype)),
3724 TYPE_MODE (sizetype));
3726 /* Return the value in the proper mode for this function. */
3727 mode = TYPE_MODE (TREE_TYPE (exp));
3728 if (GET_MODE (result) == mode)
3729 return result;
3730 else if (target != 0)
3732 convert_move (target, result, 0);
3733 return target;
3735 else
3736 return convert_to_mode (mode, result, 0);
3738 #endif /* HAVE_cmpmemsi. */
3740 return NULL_RTX;
3743 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3744 if we failed the caller should emit a normal call, otherwise try to get
3745 the result in TARGET, if convenient. */
3747 static rtx
3748 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3750 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3751 return NULL_RTX;
3753 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3754 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3755 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3757 rtx arg1_rtx, arg2_rtx;
3758 rtx result, insn = NULL_RTX;
3759 tree fndecl, fn;
3760 tree arg1 = CALL_EXPR_ARG (exp, 0);
3761 tree arg2 = CALL_EXPR_ARG (exp, 1);
3763 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3764 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3766 /* If we don't have POINTER_TYPE, call the function. */
3767 if (arg1_align == 0 || arg2_align == 0)
3768 return NULL_RTX;
3770 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3771 arg1 = builtin_save_expr (arg1);
3772 arg2 = builtin_save_expr (arg2);
3774 arg1_rtx = get_memory_rtx (arg1, NULL);
3775 arg2_rtx = get_memory_rtx (arg2, NULL);
3777 #ifdef HAVE_cmpstrsi
3778 /* Try to call cmpstrsi. */
3779 if (HAVE_cmpstrsi)
3781 enum machine_mode insn_mode
3782 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3784 /* Make a place to write the result of the instruction. */
3785 result = target;
3786 if (! (result != 0
3787 && REG_P (result) && GET_MODE (result) == insn_mode
3788 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3789 result = gen_reg_rtx (insn_mode);
3791 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3792 GEN_INT (MIN (arg1_align, arg2_align)));
3794 #endif
3795 #ifdef HAVE_cmpstrnsi
3796 /* Try to determine at least one length and call cmpstrnsi. */
3797 if (!insn && HAVE_cmpstrnsi)
3799 tree len;
3800 rtx arg3_rtx;
3802 enum machine_mode insn_mode
3803 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3804 tree len1 = c_strlen (arg1, 1);
3805 tree len2 = c_strlen (arg2, 1);
3807 if (len1)
3808 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3809 if (len2)
3810 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3812 /* If we don't have a constant length for the first, use the length
3813 of the second, if we know it. We don't require a constant for
3814 this case; some cost analysis could be done if both are available
3815 but neither is constant. For now, assume they're equally cheap,
3816 unless one has side effects. If both strings have constant lengths,
3817 use the smaller. */
3819 if (!len1)
3820 len = len2;
3821 else if (!len2)
3822 len = len1;
3823 else if (TREE_SIDE_EFFECTS (len1))
3824 len = len2;
3825 else if (TREE_SIDE_EFFECTS (len2))
3826 len = len1;
3827 else if (TREE_CODE (len1) != INTEGER_CST)
3828 len = len2;
3829 else if (TREE_CODE (len2) != INTEGER_CST)
3830 len = len1;
3831 else if (tree_int_cst_lt (len1, len2))
3832 len = len1;
3833 else
3834 len = len2;
3836 /* If both arguments have side effects, we cannot optimize. */
3837 if (!len || TREE_SIDE_EFFECTS (len))
3838 goto do_libcall;
3840 arg3_rtx = expand_normal (len);
3842 /* Make a place to write the result of the instruction. */
3843 result = target;
3844 if (! (result != 0
3845 && REG_P (result) && GET_MODE (result) == insn_mode
3846 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3847 result = gen_reg_rtx (insn_mode);
3849 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3850 GEN_INT (MIN (arg1_align, arg2_align)));
3852 #endif
3854 if (insn)
3856 enum machine_mode mode;
3857 emit_insn (insn);
3859 /* Return the value in the proper mode for this function. */
3860 mode = TYPE_MODE (TREE_TYPE (exp));
3861 if (GET_MODE (result) == mode)
3862 return result;
3863 if (target == 0)
3864 return convert_to_mode (mode, result, 0);
3865 convert_move (target, result, 0);
3866 return target;
3869 /* Expand the library call ourselves using a stabilized argument
3870 list to avoid re-evaluating the function's arguments twice. */
3871 #ifdef HAVE_cmpstrnsi
3872 do_libcall:
3873 #endif
3874 fndecl = get_callee_fndecl (exp);
3875 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3876 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3877 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3878 return expand_call (fn, target, target == const0_rtx);
3880 #endif
3881 return NULL_RTX;
3884 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3885 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3886 the result in TARGET, if convenient. */
3888 static rtx
3889 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3890 ATTRIBUTE_UNUSED enum machine_mode mode)
3892 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3894 if (!validate_arglist (exp,
3895 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3896 return NULL_RTX;
3898 /* If c_strlen can determine an expression for one of the string
3899 lengths, and it doesn't have side effects, then emit cmpstrnsi
3900 using length MIN(strlen(string)+1, arg3). */
3901 #ifdef HAVE_cmpstrnsi
3902 if (HAVE_cmpstrnsi)
3904 tree len, len1, len2;
3905 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3906 rtx result, insn;
3907 tree fndecl, fn;
3908 tree arg1 = CALL_EXPR_ARG (exp, 0);
3909 tree arg2 = CALL_EXPR_ARG (exp, 1);
3910 tree arg3 = CALL_EXPR_ARG (exp, 2);
3912 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3913 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3914 enum machine_mode insn_mode
3915 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3917 len1 = c_strlen (arg1, 1);
3918 len2 = c_strlen (arg2, 1);
3920 if (len1)
3921 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3922 if (len2)
3923 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3925 /* If we don't have a constant length for the first, use the length
3926 of the second, if we know it. We don't require a constant for
3927 this case; some cost analysis could be done if both are available
3928 but neither is constant. For now, assume they're equally cheap,
3929 unless one has side effects. If both strings have constant lengths,
3930 use the smaller. */
3932 if (!len1)
3933 len = len2;
3934 else if (!len2)
3935 len = len1;
3936 else if (TREE_SIDE_EFFECTS (len1))
3937 len = len2;
3938 else if (TREE_SIDE_EFFECTS (len2))
3939 len = len1;
3940 else if (TREE_CODE (len1) != INTEGER_CST)
3941 len = len2;
3942 else if (TREE_CODE (len2) != INTEGER_CST)
3943 len = len1;
3944 else if (tree_int_cst_lt (len1, len2))
3945 len = len1;
3946 else
3947 len = len2;
3949 /* If both arguments have side effects, we cannot optimize. */
3950 if (!len || TREE_SIDE_EFFECTS (len))
3951 return NULL_RTX;
3953 /* The actual new length parameter is MIN(len,arg3). */
3954 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3955 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3957 /* If we don't have POINTER_TYPE, call the function. */
3958 if (arg1_align == 0 || arg2_align == 0)
3959 return NULL_RTX;
3961 /* Make a place to write the result of the instruction. */
3962 result = target;
3963 if (! (result != 0
3964 && REG_P (result) && GET_MODE (result) == insn_mode
3965 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3966 result = gen_reg_rtx (insn_mode);
3968 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3969 arg1 = builtin_save_expr (arg1);
3970 arg2 = builtin_save_expr (arg2);
3971 len = builtin_save_expr (len);
3973 arg1_rtx = get_memory_rtx (arg1, len);
3974 arg2_rtx = get_memory_rtx (arg2, len);
3975 arg3_rtx = expand_normal (len);
3976 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3977 GEN_INT (MIN (arg1_align, arg2_align)));
3978 if (insn)
3980 emit_insn (insn);
3982 /* Return the value in the proper mode for this function. */
3983 mode = TYPE_MODE (TREE_TYPE (exp));
3984 if (GET_MODE (result) == mode)
3985 return result;
3986 if (target == 0)
3987 return convert_to_mode (mode, result, 0);
3988 convert_move (target, result, 0);
3989 return target;
3992 /* Expand the library call ourselves using a stabilized argument
3993 list to avoid re-evaluating the function's arguments twice. */
3994 fndecl = get_callee_fndecl (exp);
3995 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
3996 arg1, arg2, len);
3997 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3998 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3999 return expand_call (fn, target, target == const0_rtx);
4001 #endif
4002 return NULL_RTX;
4005 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4006 if that's convenient. */
4009 expand_builtin_saveregs (void)
4011 rtx val, seq;
4013 /* Don't do __builtin_saveregs more than once in a function.
4014 Save the result of the first call and reuse it. */
4015 if (saveregs_value != 0)
4016 return saveregs_value;
4018 /* When this function is called, it means that registers must be
4019 saved on entry to this function. So we migrate the call to the
4020 first insn of this function. */
4022 start_sequence ();
4024 /* Do whatever the machine needs done in this case. */
4025 val = targetm.calls.expand_builtin_saveregs ();
4027 seq = get_insns ();
4028 end_sequence ();
4030 saveregs_value = val;
4032 /* Put the insns after the NOTE that starts the function. If this
4033 is inside a start_sequence, make the outer-level insn chain current, so
4034 the code is placed at the start of the function. */
4035 push_topmost_sequence ();
4036 emit_insn_after (seq, entry_of_function ());
4037 pop_topmost_sequence ();
4039 return val;
4042 /* Expand a call to __builtin_next_arg. */
4044 static rtx
4045 expand_builtin_next_arg (void)
4047 /* Checking arguments is already done in fold_builtin_next_arg
4048 that must be called before this function. */
4049 return expand_binop (ptr_mode, add_optab,
4050 crtl->args.internal_arg_pointer,
4051 crtl->args.arg_offset_rtx,
4052 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4055 /* Make it easier for the backends by protecting the valist argument
4056 from multiple evaluations. */
4058 static tree
4059 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4061 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4063 /* The current way of determining the type of valist is completely
4064 bogus. We should have the information on the va builtin instead. */
4065 if (!vatype)
4066 vatype = targetm.fn_abi_va_list (cfun->decl);
4068 if (TREE_CODE (vatype) == ARRAY_TYPE)
4070 if (TREE_SIDE_EFFECTS (valist))
4071 valist = save_expr (valist);
4073 /* For this case, the backends will be expecting a pointer to
4074 vatype, but it's possible we've actually been given an array
4075 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4076 So fix it. */
4077 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4079 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4080 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4083 else
4085 tree pt = build_pointer_type (vatype);
4087 if (! needs_lvalue)
4089 if (! TREE_SIDE_EFFECTS (valist))
4090 return valist;
4092 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4093 TREE_SIDE_EFFECTS (valist) = 1;
4096 if (TREE_SIDE_EFFECTS (valist))
4097 valist = save_expr (valist);
4098 valist = fold_build2_loc (loc, MEM_REF,
4099 vatype, valist, build_int_cst (pt, 0));
4102 return valist;
4105 /* The "standard" definition of va_list is void*. */
4107 tree
4108 std_build_builtin_va_list (void)
4110 return ptr_type_node;
4113 /* The "standard" abi va_list is va_list_type_node. */
4115 tree
4116 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4118 return va_list_type_node;
4121 /* The "standard" type of va_list is va_list_type_node. */
4123 tree
4124 std_canonical_va_list_type (tree type)
4126 tree wtype, htype;
4128 if (INDIRECT_REF_P (type))
4129 type = TREE_TYPE (type);
4130 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4131 type = TREE_TYPE (type);
4132 wtype = va_list_type_node;
4133 htype = type;
4134 /* Treat structure va_list types. */
4135 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4136 htype = TREE_TYPE (htype);
4137 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4139 /* If va_list is an array type, the argument may have decayed
4140 to a pointer type, e.g. by being passed to another function.
4141 In that case, unwrap both types so that we can compare the
4142 underlying records. */
4143 if (TREE_CODE (htype) == ARRAY_TYPE
4144 || POINTER_TYPE_P (htype))
4146 wtype = TREE_TYPE (wtype);
4147 htype = TREE_TYPE (htype);
4150 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4151 return va_list_type_node;
4153 return NULL_TREE;
4156 /* The "standard" implementation of va_start: just assign `nextarg' to
4157 the variable. */
4159 void
4160 std_expand_builtin_va_start (tree valist, rtx nextarg)
4162 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4163 convert_move (va_r, nextarg, 0);
4166 /* Expand EXP, a call to __builtin_va_start. */
4168 static rtx
4169 expand_builtin_va_start (tree exp)
4171 rtx nextarg;
4172 tree valist;
4173 location_t loc = EXPR_LOCATION (exp);
4175 if (call_expr_nargs (exp) < 2)
4177 error_at (loc, "too few arguments to function %<va_start%>");
4178 return const0_rtx;
4181 if (fold_builtin_next_arg (exp, true))
4182 return const0_rtx;
4184 nextarg = expand_builtin_next_arg ();
4185 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4187 if (targetm.expand_builtin_va_start)
4188 targetm.expand_builtin_va_start (valist, nextarg);
4189 else
4190 std_expand_builtin_va_start (valist, nextarg);
4192 return const0_rtx;
4195 /* The "standard" implementation of va_arg: read the value from the
4196 current (padded) address and increment by the (padded) size. */
4198 tree
4199 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4200 gimple_seq *post_p)
4202 tree addr, t, type_size, rounded_size, valist_tmp;
4203 unsigned HOST_WIDE_INT align, boundary;
4204 bool indirect;
4206 #ifdef ARGS_GROW_DOWNWARD
4207 /* All of the alignment and movement below is for args-grow-up machines.
4208 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4209 implement their own specialized gimplify_va_arg_expr routines. */
4210 gcc_unreachable ();
4211 #endif
4213 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4214 if (indirect)
4215 type = build_pointer_type (type);
4217 align = PARM_BOUNDARY / BITS_PER_UNIT;
4218 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4220 /* When we align parameter on stack for caller, if the parameter
4221 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4222 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4223 here with caller. */
4224 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4225 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4227 boundary /= BITS_PER_UNIT;
4229 /* Hoist the valist value into a temporary for the moment. */
4230 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4232 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4233 requires greater alignment, we must perform dynamic alignment. */
4234 if (boundary > align
4235 && !integer_zerop (TYPE_SIZE (type)))
4237 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4238 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4239 gimplify_and_add (t, pre_p);
4241 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4242 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4243 valist_tmp,
4244 build_int_cst (TREE_TYPE (valist), -boundary)));
4245 gimplify_and_add (t, pre_p);
4247 else
4248 boundary = align;
4250 /* If the actual alignment is less than the alignment of the type,
4251 adjust the type accordingly so that we don't assume strict alignment
4252 when dereferencing the pointer. */
4253 boundary *= BITS_PER_UNIT;
4254 if (boundary < TYPE_ALIGN (type))
4256 type = build_variant_type_copy (type);
4257 TYPE_ALIGN (type) = boundary;
4260 /* Compute the rounded size of the type. */
4261 type_size = size_in_bytes (type);
4262 rounded_size = round_up (type_size, align);
4264 /* Reduce rounded_size so it's sharable with the postqueue. */
4265 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4267 /* Get AP. */
4268 addr = valist_tmp;
4269 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4271 /* Small args are padded downward. */
4272 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4273 rounded_size, size_int (align));
4274 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4275 size_binop (MINUS_EXPR, rounded_size, type_size));
4276 addr = fold_build_pointer_plus (addr, t);
4279 /* Compute new value for AP. */
4280 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4281 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4282 gimplify_and_add (t, pre_p);
4284 addr = fold_convert (build_pointer_type (type), addr);
4286 if (indirect)
4287 addr = build_va_arg_indirect_ref (addr);
4289 return build_va_arg_indirect_ref (addr);
4292 /* Build an indirect-ref expression over the given TREE, which represents a
4293 piece of a va_arg() expansion. */
4294 tree
4295 build_va_arg_indirect_ref (tree addr)
4297 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4299 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4300 mf_mark (addr);
4302 return addr;
4305 /* Return a dummy expression of type TYPE in order to keep going after an
4306 error. */
4308 static tree
4309 dummy_object (tree type)
4311 tree t = build_int_cst (build_pointer_type (type), 0);
4312 return build2 (MEM_REF, type, t, t);
4315 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4316 builtin function, but a very special sort of operator. */
4318 enum gimplify_status
4319 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4321 tree promoted_type, have_va_type;
4322 tree valist = TREE_OPERAND (*expr_p, 0);
4323 tree type = TREE_TYPE (*expr_p);
4324 tree t;
4325 location_t loc = EXPR_LOCATION (*expr_p);
4327 /* Verify that valist is of the proper type. */
4328 have_va_type = TREE_TYPE (valist);
4329 if (have_va_type == error_mark_node)
4330 return GS_ERROR;
4331 have_va_type = targetm.canonical_va_list_type (have_va_type);
4333 if (have_va_type == NULL_TREE)
4335 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4336 return GS_ERROR;
4339 /* Generate a diagnostic for requesting data of a type that cannot
4340 be passed through `...' due to type promotion at the call site. */
4341 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4342 != type)
4344 static bool gave_help;
4345 bool warned;
4347 /* Unfortunately, this is merely undefined, rather than a constraint
4348 violation, so we cannot make this an error. If this call is never
4349 executed, the program is still strictly conforming. */
4350 warned = warning_at (loc, 0,
4351 "%qT is promoted to %qT when passed through %<...%>",
4352 type, promoted_type);
4353 if (!gave_help && warned)
4355 gave_help = true;
4356 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4357 promoted_type, type);
4360 /* We can, however, treat "undefined" any way we please.
4361 Call abort to encourage the user to fix the program. */
4362 if (warned)
4363 inform (loc, "if this code is reached, the program will abort");
4364 /* Before the abort, allow the evaluation of the va_list
4365 expression to exit or longjmp. */
4366 gimplify_and_add (valist, pre_p);
4367 t = build_call_expr_loc (loc,
4368 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4369 gimplify_and_add (t, pre_p);
4371 /* This is dead code, but go ahead and finish so that the
4372 mode of the result comes out right. */
4373 *expr_p = dummy_object (type);
4374 return GS_ALL_DONE;
4376 else
4378 /* Make it easier for the backends by protecting the valist argument
4379 from multiple evaluations. */
4380 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4382 /* For this case, the backends will be expecting a pointer to
4383 TREE_TYPE (abi), but it's possible we've
4384 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4385 So fix it. */
4386 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4388 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4389 valist = fold_convert_loc (loc, p1,
4390 build_fold_addr_expr_loc (loc, valist));
4393 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4395 else
4396 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4398 if (!targetm.gimplify_va_arg_expr)
4399 /* FIXME: Once most targets are converted we should merely
4400 assert this is non-null. */
4401 return GS_ALL_DONE;
4403 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4404 return GS_OK;
4408 /* Expand EXP, a call to __builtin_va_end. */
4410 static rtx
4411 expand_builtin_va_end (tree exp)
4413 tree valist = CALL_EXPR_ARG (exp, 0);
4415 /* Evaluate for side effects, if needed. I hate macros that don't
4416 do that. */
4417 if (TREE_SIDE_EFFECTS (valist))
4418 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4420 return const0_rtx;
4423 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4424 builtin rather than just as an assignment in stdarg.h because of the
4425 nastiness of array-type va_list types. */
4427 static rtx
4428 expand_builtin_va_copy (tree exp)
4430 tree dst, src, t;
4431 location_t loc = EXPR_LOCATION (exp);
4433 dst = CALL_EXPR_ARG (exp, 0);
4434 src = CALL_EXPR_ARG (exp, 1);
4436 dst = stabilize_va_list_loc (loc, dst, 1);
4437 src = stabilize_va_list_loc (loc, src, 0);
4439 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4441 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4443 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4444 TREE_SIDE_EFFECTS (t) = 1;
4445 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4447 else
4449 rtx dstb, srcb, size;
4451 /* Evaluate to pointers. */
4452 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4453 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4454 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4455 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4457 dstb = convert_memory_address (Pmode, dstb);
4458 srcb = convert_memory_address (Pmode, srcb);
4460 /* "Dereference" to BLKmode memories. */
4461 dstb = gen_rtx_MEM (BLKmode, dstb);
4462 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4463 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4464 srcb = gen_rtx_MEM (BLKmode, srcb);
4465 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4466 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4468 /* Copy. */
4469 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4472 return const0_rtx;
4475 /* Expand a call to one of the builtin functions __builtin_frame_address or
4476 __builtin_return_address. */
4478 static rtx
4479 expand_builtin_frame_address (tree fndecl, tree exp)
4481 /* The argument must be a nonnegative integer constant.
4482 It counts the number of frames to scan up the stack.
4483 The value is the return address saved in that frame. */
4484 if (call_expr_nargs (exp) == 0)
4485 /* Warning about missing arg was already issued. */
4486 return const0_rtx;
4487 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4489 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4490 error ("invalid argument to %<__builtin_frame_address%>");
4491 else
4492 error ("invalid argument to %<__builtin_return_address%>");
4493 return const0_rtx;
4495 else
4497 rtx tem
4498 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4499 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4501 /* Some ports cannot access arbitrary stack frames. */
4502 if (tem == NULL)
4504 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4505 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4506 else
4507 warning (0, "unsupported argument to %<__builtin_return_address%>");
4508 return const0_rtx;
4511 /* For __builtin_frame_address, return what we've got. */
4512 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4513 return tem;
4515 if (!REG_P (tem)
4516 && ! CONSTANT_P (tem))
4517 tem = copy_addr_to_reg (tem);
4518 return tem;
4522 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4523 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4524 is the same as for allocate_dynamic_stack_space. */
4526 static rtx
4527 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4529 rtx op0;
4530 rtx result;
4531 bool valid_arglist;
4532 unsigned int align;
4533 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4534 == BUILT_IN_ALLOCA_WITH_ALIGN);
4536 /* Emit normal call if we use mudflap. */
4537 if (flag_mudflap)
4538 return NULL_RTX;
4540 valid_arglist
4541 = (alloca_with_align
4542 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4543 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4545 if (!valid_arglist)
4546 return NULL_RTX;
4548 /* Compute the argument. */
4549 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4551 /* Compute the alignment. */
4552 align = (alloca_with_align
4553 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4554 : BIGGEST_ALIGNMENT);
4556 /* Allocate the desired space. */
4557 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4558 result = convert_memory_address (ptr_mode, result);
4560 return result;
4563 /* Expand a call to bswap builtin in EXP.
4564 Return NULL_RTX if a normal call should be emitted rather than expanding the
4565 function in-line. If convenient, the result should be placed in TARGET.
4566 SUBTARGET may be used as the target for computing one of EXP's operands. */
4568 static rtx
4569 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4570 rtx subtarget)
4572 tree arg;
4573 rtx op0;
4575 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4576 return NULL_RTX;
4578 arg = CALL_EXPR_ARG (exp, 0);
4579 op0 = expand_expr (arg,
4580 subtarget && GET_MODE (subtarget) == target_mode
4581 ? subtarget : NULL_RTX,
4582 target_mode, EXPAND_NORMAL);
4583 if (GET_MODE (op0) != target_mode)
4584 op0 = convert_to_mode (target_mode, op0, 1);
4586 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4588 gcc_assert (target);
4590 return convert_to_mode (target_mode, target, 1);
4593 /* Expand a call to a unary builtin in EXP.
4594 Return NULL_RTX if a normal call should be emitted rather than expanding the
4595 function in-line. If convenient, the result should be placed in TARGET.
4596 SUBTARGET may be used as the target for computing one of EXP's operands. */
4598 static rtx
4599 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4600 rtx subtarget, optab op_optab)
4602 rtx op0;
4604 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4605 return NULL_RTX;
4607 /* Compute the argument. */
4608 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4609 (subtarget
4610 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4611 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4612 VOIDmode, EXPAND_NORMAL);
4613 /* Compute op, into TARGET if possible.
4614 Set TARGET to wherever the result comes back. */
4615 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4616 op_optab, op0, target, op_optab != clrsb_optab);
4617 gcc_assert (target);
4619 return convert_to_mode (target_mode, target, 0);
4622 /* Expand a call to __builtin_expect. We just return our argument
4623 as the builtin_expect semantic should've been already executed by
4624 tree branch prediction pass. */
4626 static rtx
4627 expand_builtin_expect (tree exp, rtx target)
4629 tree arg;
4631 if (call_expr_nargs (exp) < 2)
4632 return const0_rtx;
4633 arg = CALL_EXPR_ARG (exp, 0);
4635 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4636 /* When guessing was done, the hints should be already stripped away. */
4637 gcc_assert (!flag_guess_branch_prob
4638 || optimize == 0 || seen_error ());
4639 return target;
4642 /* Expand a call to __builtin_assume_aligned. We just return our first
4643 argument as the builtin_assume_aligned semantic should've been already
4644 executed by CCP. */
4646 static rtx
4647 expand_builtin_assume_aligned (tree exp, rtx target)
4649 if (call_expr_nargs (exp) < 2)
4650 return const0_rtx;
4651 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4652 EXPAND_NORMAL);
4653 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4654 && (call_expr_nargs (exp) < 3
4655 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4656 return target;
4659 void
4660 expand_builtin_trap (void)
4662 #ifdef HAVE_trap
4663 if (HAVE_trap)
4665 rtx insn = emit_insn (gen_trap ());
4666 /* For trap insns when not accumulating outgoing args force
4667 REG_ARGS_SIZE note to prevent crossjumping of calls with
4668 different args sizes. */
4669 if (!ACCUMULATE_OUTGOING_ARGS)
4670 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4672 else
4673 #endif
4674 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4675 emit_barrier ();
4678 /* Expand a call to __builtin_unreachable. We do nothing except emit
4679 a barrier saying that control flow will not pass here.
4681 It is the responsibility of the program being compiled to ensure
4682 that control flow does never reach __builtin_unreachable. */
4683 static void
4684 expand_builtin_unreachable (void)
4686 emit_barrier ();
4689 /* Expand EXP, a call to fabs, fabsf or fabsl.
4690 Return NULL_RTX if a normal call should be emitted rather than expanding
4691 the function inline. If convenient, the result should be placed
4692 in TARGET. SUBTARGET may be used as the target for computing
4693 the operand. */
4695 static rtx
4696 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4698 enum machine_mode mode;
4699 tree arg;
4700 rtx op0;
4702 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4703 return NULL_RTX;
4705 arg = CALL_EXPR_ARG (exp, 0);
4706 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4707 mode = TYPE_MODE (TREE_TYPE (arg));
4708 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4709 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4712 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4713 Return NULL is a normal call should be emitted rather than expanding the
4714 function inline. If convenient, the result should be placed in TARGET.
4715 SUBTARGET may be used as the target for computing the operand. */
4717 static rtx
4718 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4720 rtx op0, op1;
4721 tree arg;
4723 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4724 return NULL_RTX;
4726 arg = CALL_EXPR_ARG (exp, 0);
4727 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4729 arg = CALL_EXPR_ARG (exp, 1);
4730 op1 = expand_normal (arg);
4732 return expand_copysign (op0, op1, target);
4735 /* Create a new constant string literal and return a char* pointer to it.
4736 The STRING_CST value is the LEN characters at STR. */
4737 tree
4738 build_string_literal (int len, const char *str)
4740 tree t, elem, index, type;
4742 t = build_string (len, str);
4743 elem = build_type_variant (char_type_node, 1, 0);
4744 index = build_index_type (size_int (len - 1));
4745 type = build_array_type (elem, index);
4746 TREE_TYPE (t) = type;
4747 TREE_CONSTANT (t) = 1;
4748 TREE_READONLY (t) = 1;
4749 TREE_STATIC (t) = 1;
4751 type = build_pointer_type (elem);
4752 t = build1 (ADDR_EXPR, type,
4753 build4 (ARRAY_REF, elem,
4754 t, integer_zero_node, NULL_TREE, NULL_TREE));
4755 return t;
4758 /* Expand a call to __builtin___clear_cache. */
4760 static rtx
4761 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4763 #ifndef HAVE_clear_cache
4764 #ifdef CLEAR_INSN_CACHE
4765 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4766 does something. Just do the default expansion to a call to
4767 __clear_cache(). */
4768 return NULL_RTX;
4769 #else
4770 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4771 does nothing. There is no need to call it. Do nothing. */
4772 return const0_rtx;
4773 #endif /* CLEAR_INSN_CACHE */
4774 #else
4775 /* We have a "clear_cache" insn, and it will handle everything. */
4776 tree begin, end;
4777 rtx begin_rtx, end_rtx;
4779 /* We must not expand to a library call. If we did, any
4780 fallback library function in libgcc that might contain a call to
4781 __builtin___clear_cache() would recurse infinitely. */
4782 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4784 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4785 return const0_rtx;
4788 if (HAVE_clear_cache)
4790 struct expand_operand ops[2];
4792 begin = CALL_EXPR_ARG (exp, 0);
4793 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4795 end = CALL_EXPR_ARG (exp, 1);
4796 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4798 create_address_operand (&ops[0], begin_rtx);
4799 create_address_operand (&ops[1], end_rtx);
4800 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4801 return const0_rtx;
4803 return const0_rtx;
4804 #endif /* HAVE_clear_cache */
4807 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4809 static rtx
4810 round_trampoline_addr (rtx tramp)
4812 rtx temp, addend, mask;
4814 /* If we don't need too much alignment, we'll have been guaranteed
4815 proper alignment by get_trampoline_type. */
4816 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4817 return tramp;
4819 /* Round address up to desired boundary. */
4820 temp = gen_reg_rtx (Pmode);
4821 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4822 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4824 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4825 temp, 0, OPTAB_LIB_WIDEN);
4826 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4827 temp, 0, OPTAB_LIB_WIDEN);
4829 return tramp;
4832 static rtx
4833 expand_builtin_init_trampoline (tree exp, bool onstack)
4835 tree t_tramp, t_func, t_chain;
4836 rtx m_tramp, r_tramp, r_chain, tmp;
4838 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4839 POINTER_TYPE, VOID_TYPE))
4840 return NULL_RTX;
4842 t_tramp = CALL_EXPR_ARG (exp, 0);
4843 t_func = CALL_EXPR_ARG (exp, 1);
4844 t_chain = CALL_EXPR_ARG (exp, 2);
4846 r_tramp = expand_normal (t_tramp);
4847 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4848 MEM_NOTRAP_P (m_tramp) = 1;
4850 /* If ONSTACK, the TRAMP argument should be the address of a field
4851 within the local function's FRAME decl. Either way, let's see if
4852 we can fill in the MEM_ATTRs for this memory. */
4853 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4854 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4855 true, 0);
4857 /* Creator of a heap trampoline is responsible for making sure the
4858 address is aligned to at least STACK_BOUNDARY. Normally malloc
4859 will ensure this anyhow. */
4860 tmp = round_trampoline_addr (r_tramp);
4861 if (tmp != r_tramp)
4863 m_tramp = change_address (m_tramp, BLKmode, tmp);
4864 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4865 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4868 /* The FUNC argument should be the address of the nested function.
4869 Extract the actual function decl to pass to the hook. */
4870 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4871 t_func = TREE_OPERAND (t_func, 0);
4872 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4874 r_chain = expand_normal (t_chain);
4876 /* Generate insns to initialize the trampoline. */
4877 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4879 if (onstack)
4881 trampolines_created = 1;
4883 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4884 "trampoline generated for nested function %qD", t_func);
4887 return const0_rtx;
4890 static rtx
4891 expand_builtin_adjust_trampoline (tree exp)
4893 rtx tramp;
4895 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4896 return NULL_RTX;
4898 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4899 tramp = round_trampoline_addr (tramp);
4900 if (targetm.calls.trampoline_adjust_address)
4901 tramp = targetm.calls.trampoline_adjust_address (tramp);
4903 return tramp;
4906 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4907 function. The function first checks whether the back end provides
4908 an insn to implement signbit for the respective mode. If not, it
4909 checks whether the floating point format of the value is such that
4910 the sign bit can be extracted. If that is not the case, the
4911 function returns NULL_RTX to indicate that a normal call should be
4912 emitted rather than expanding the function in-line. EXP is the
4913 expression that is a call to the builtin function; if convenient,
4914 the result should be placed in TARGET. */
4915 static rtx
4916 expand_builtin_signbit (tree exp, rtx target)
4918 const struct real_format *fmt;
4919 enum machine_mode fmode, imode, rmode;
4920 tree arg;
4921 int word, bitpos;
4922 enum insn_code icode;
4923 rtx temp;
4924 location_t loc = EXPR_LOCATION (exp);
4926 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4927 return NULL_RTX;
4929 arg = CALL_EXPR_ARG (exp, 0);
4930 fmode = TYPE_MODE (TREE_TYPE (arg));
4931 rmode = TYPE_MODE (TREE_TYPE (exp));
4932 fmt = REAL_MODE_FORMAT (fmode);
4934 arg = builtin_save_expr (arg);
4936 /* Expand the argument yielding a RTX expression. */
4937 temp = expand_normal (arg);
4939 /* Check if the back end provides an insn that handles signbit for the
4940 argument's mode. */
4941 icode = optab_handler (signbit_optab, fmode);
4942 if (icode != CODE_FOR_nothing)
4944 rtx last = get_last_insn ();
4945 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4946 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4947 return target;
4948 delete_insns_since (last);
4951 /* For floating point formats without a sign bit, implement signbit
4952 as "ARG < 0.0". */
4953 bitpos = fmt->signbit_ro;
4954 if (bitpos < 0)
4956 /* But we can't do this if the format supports signed zero. */
4957 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4958 return NULL_RTX;
4960 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4961 build_real (TREE_TYPE (arg), dconst0));
4962 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4965 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4967 imode = int_mode_for_mode (fmode);
4968 if (imode == BLKmode)
4969 return NULL_RTX;
4970 temp = gen_lowpart (imode, temp);
4972 else
4974 imode = word_mode;
4975 /* Handle targets with different FP word orders. */
4976 if (FLOAT_WORDS_BIG_ENDIAN)
4977 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4978 else
4979 word = bitpos / BITS_PER_WORD;
4980 temp = operand_subword_force (temp, word, fmode);
4981 bitpos = bitpos % BITS_PER_WORD;
4984 /* Force the intermediate word_mode (or narrower) result into a
4985 register. This avoids attempting to create paradoxical SUBREGs
4986 of floating point modes below. */
4987 temp = force_reg (imode, temp);
4989 /* If the bitpos is within the "result mode" lowpart, the operation
4990 can be implement with a single bitwise AND. Otherwise, we need
4991 a right shift and an AND. */
4993 if (bitpos < GET_MODE_BITSIZE (rmode))
4995 double_int mask = double_int_zero.set_bit (bitpos);
4997 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4998 temp = gen_lowpart (rmode, temp);
4999 temp = expand_binop (rmode, and_optab, temp,
5000 immed_double_int_const (mask, rmode),
5001 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5003 else
5005 /* Perform a logical right shift to place the signbit in the least
5006 significant bit, then truncate the result to the desired mode
5007 and mask just this bit. */
5008 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5009 temp = gen_lowpart (rmode, temp);
5010 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5011 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5014 return temp;
5017 /* Expand fork or exec calls. TARGET is the desired target of the
5018 call. EXP is the call. FN is the
5019 identificator of the actual function. IGNORE is nonzero if the
5020 value is to be ignored. */
5022 static rtx
5023 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5025 tree id, decl;
5026 tree call;
5028 /* If we are not profiling, just call the function. */
5029 if (!profile_arc_flag)
5030 return NULL_RTX;
5032 /* Otherwise call the wrapper. This should be equivalent for the rest of
5033 compiler, so the code does not diverge, and the wrapper may run the
5034 code necessary for keeping the profiling sane. */
5036 switch (DECL_FUNCTION_CODE (fn))
5038 case BUILT_IN_FORK:
5039 id = get_identifier ("__gcov_fork");
5040 break;
5042 case BUILT_IN_EXECL:
5043 id = get_identifier ("__gcov_execl");
5044 break;
5046 case BUILT_IN_EXECV:
5047 id = get_identifier ("__gcov_execv");
5048 break;
5050 case BUILT_IN_EXECLP:
5051 id = get_identifier ("__gcov_execlp");
5052 break;
5054 case BUILT_IN_EXECLE:
5055 id = get_identifier ("__gcov_execle");
5056 break;
5058 case BUILT_IN_EXECVP:
5059 id = get_identifier ("__gcov_execvp");
5060 break;
5062 case BUILT_IN_EXECVE:
5063 id = get_identifier ("__gcov_execve");
5064 break;
5066 default:
5067 gcc_unreachable ();
5070 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5071 FUNCTION_DECL, id, TREE_TYPE (fn));
5072 DECL_EXTERNAL (decl) = 1;
5073 TREE_PUBLIC (decl) = 1;
5074 DECL_ARTIFICIAL (decl) = 1;
5075 TREE_NOTHROW (decl) = 1;
5076 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5077 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5078 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5079 return expand_call (call, target, ignore);
5084 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5085 the pointer in these functions is void*, the tree optimizers may remove
5086 casts. The mode computed in expand_builtin isn't reliable either, due
5087 to __sync_bool_compare_and_swap.
5089 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5090 group of builtins. This gives us log2 of the mode size. */
5092 static inline enum machine_mode
5093 get_builtin_sync_mode (int fcode_diff)
5095 /* The size is not negotiable, so ask not to get BLKmode in return
5096 if the target indicates that a smaller size would be better. */
5097 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5100 /* Expand the memory expression LOC and return the appropriate memory operand
5101 for the builtin_sync operations. */
5103 static rtx
5104 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5106 rtx addr, mem;
5108 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5109 addr = convert_memory_address (Pmode, addr);
5111 /* Note that we explicitly do not want any alias information for this
5112 memory, so that we kill all other live memories. Otherwise we don't
5113 satisfy the full barrier semantics of the intrinsic. */
5114 mem = validize_mem (gen_rtx_MEM (mode, addr));
5116 /* The alignment needs to be at least according to that of the mode. */
5117 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5118 get_pointer_alignment (loc)));
5119 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5120 MEM_VOLATILE_P (mem) = 1;
5122 return mem;
5125 /* Make sure an argument is in the right mode.
5126 EXP is the tree argument.
5127 MODE is the mode it should be in. */
5129 static rtx
5130 expand_expr_force_mode (tree exp, enum machine_mode mode)
5132 rtx val;
5133 enum machine_mode old_mode;
5135 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5136 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5137 of CONST_INTs, where we know the old_mode only from the call argument. */
5139 old_mode = GET_MODE (val);
5140 if (old_mode == VOIDmode)
5141 old_mode = TYPE_MODE (TREE_TYPE (exp));
5142 val = convert_modes (mode, old_mode, val, 1);
5143 return val;
5147 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5148 EXP is the CALL_EXPR. CODE is the rtx code
5149 that corresponds to the arithmetic or logical operation from the name;
5150 an exception here is that NOT actually means NAND. TARGET is an optional
5151 place for us to store the results; AFTER is true if this is the
5152 fetch_and_xxx form. */
5154 static rtx
5155 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5156 enum rtx_code code, bool after,
5157 rtx target)
5159 rtx val, mem;
5160 location_t loc = EXPR_LOCATION (exp);
5162 if (code == NOT && warn_sync_nand)
5164 tree fndecl = get_callee_fndecl (exp);
5165 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5167 static bool warned_f_a_n, warned_n_a_f;
5169 switch (fcode)
5171 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5172 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5173 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5174 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5175 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5176 if (warned_f_a_n)
5177 break;
5179 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5180 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5181 warned_f_a_n = true;
5182 break;
5184 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5185 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5186 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5187 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5188 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5189 if (warned_n_a_f)
5190 break;
5192 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5193 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5194 warned_n_a_f = true;
5195 break;
5197 default:
5198 gcc_unreachable ();
5202 /* Expand the operands. */
5203 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5204 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5206 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5207 after);
5210 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5211 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5212 true if this is the boolean form. TARGET is a place for us to store the
5213 results; this is NOT optional if IS_BOOL is true. */
5215 static rtx
5216 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5217 bool is_bool, rtx target)
5219 rtx old_val, new_val, mem;
5220 rtx *pbool, *poval;
5222 /* Expand the operands. */
5223 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5224 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5225 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5227 pbool = poval = NULL;
5228 if (target != const0_rtx)
5230 if (is_bool)
5231 pbool = &target;
5232 else
5233 poval = &target;
5235 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5236 false, MEMMODEL_SEQ_CST,
5237 MEMMODEL_SEQ_CST))
5238 return NULL_RTX;
5240 return target;
5243 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5244 general form is actually an atomic exchange, and some targets only
5245 support a reduced form with the second argument being a constant 1.
5246 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5247 the results. */
5249 static rtx
5250 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5251 rtx target)
5253 rtx val, mem;
5255 /* Expand the operands. */
5256 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5257 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5259 return expand_sync_lock_test_and_set (target, mem, val);
5262 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5264 static void
5265 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5267 rtx mem;
5269 /* Expand the operands. */
5270 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5272 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5275 /* Given an integer representing an ``enum memmodel'', verify its
5276 correctness and return the memory model enum. */
5278 static enum memmodel
5279 get_memmodel (tree exp)
5281 rtx op;
5282 unsigned HOST_WIDE_INT val;
5284 /* If the parameter is not a constant, it's a run time value so we'll just
5285 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5286 if (TREE_CODE (exp) != INTEGER_CST)
5287 return MEMMODEL_SEQ_CST;
5289 op = expand_normal (exp);
5291 val = INTVAL (op);
5292 if (targetm.memmodel_check)
5293 val = targetm.memmodel_check (val);
5294 else if (val & ~MEMMODEL_MASK)
5296 warning (OPT_Winvalid_memory_model,
5297 "Unknown architecture specifier in memory model to builtin.");
5298 return MEMMODEL_SEQ_CST;
5301 if ((INTVAL(op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5303 warning (OPT_Winvalid_memory_model,
5304 "invalid memory model argument to builtin");
5305 return MEMMODEL_SEQ_CST;
5308 return (enum memmodel) val;
5311 /* Expand the __atomic_exchange intrinsic:
5312 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5313 EXP is the CALL_EXPR.
5314 TARGET is an optional place for us to store the results. */
5316 static rtx
5317 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5319 rtx val, mem;
5320 enum memmodel model;
5322 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5323 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5325 error ("invalid memory model for %<__atomic_exchange%>");
5326 return NULL_RTX;
5329 if (!flag_inline_atomics)
5330 return NULL_RTX;
5332 /* Expand the operands. */
5333 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5334 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5336 return expand_atomic_exchange (target, mem, val, model);
5339 /* Expand the __atomic_compare_exchange intrinsic:
5340 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5341 TYPE desired, BOOL weak,
5342 enum memmodel success,
5343 enum memmodel failure)
5344 EXP is the CALL_EXPR.
5345 TARGET is an optional place for us to store the results. */
5347 static rtx
5348 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5349 rtx target)
5351 rtx expect, desired, mem, oldval;
5352 enum memmodel success, failure;
5353 tree weak;
5354 bool is_weak;
5356 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5357 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5359 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5360 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5362 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5363 return NULL_RTX;
5366 if (failure > success)
5368 error ("failure memory model cannot be stronger than success "
5369 "memory model for %<__atomic_compare_exchange%>");
5370 return NULL_RTX;
5373 if (!flag_inline_atomics)
5374 return NULL_RTX;
5376 /* Expand the operands. */
5377 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5379 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5380 expect = convert_memory_address (Pmode, expect);
5381 expect = gen_rtx_MEM (mode, expect);
5382 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5384 weak = CALL_EXPR_ARG (exp, 3);
5385 is_weak = false;
5386 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5387 is_weak = true;
5389 oldval = expect;
5390 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5391 &oldval, mem, oldval, desired,
5392 is_weak, success, failure))
5393 return NULL_RTX;
5395 if (oldval != expect)
5396 emit_move_insn (expect, oldval);
5398 return target;
5401 /* Expand the __atomic_load intrinsic:
5402 TYPE __atomic_load (TYPE *object, enum memmodel)
5403 EXP is the CALL_EXPR.
5404 TARGET is an optional place for us to store the results. */
5406 static rtx
5407 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5409 rtx mem;
5410 enum memmodel model;
5412 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5413 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5414 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5416 error ("invalid memory model for %<__atomic_load%>");
5417 return NULL_RTX;
5420 if (!flag_inline_atomics)
5421 return NULL_RTX;
5423 /* Expand the operand. */
5424 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5426 return expand_atomic_load (target, mem, model);
5430 /* Expand the __atomic_store intrinsic:
5431 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5432 EXP is the CALL_EXPR.
5433 TARGET is an optional place for us to store the results. */
5435 static rtx
5436 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5438 rtx mem, val;
5439 enum memmodel model;
5441 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5442 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5443 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5444 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5446 error ("invalid memory model for %<__atomic_store%>");
5447 return NULL_RTX;
5450 if (!flag_inline_atomics)
5451 return NULL_RTX;
5453 /* Expand the operands. */
5454 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5455 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5457 return expand_atomic_store (mem, val, model, false);
5460 /* Expand the __atomic_fetch_XXX intrinsic:
5461 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5462 EXP is the CALL_EXPR.
5463 TARGET is an optional place for us to store the results.
5464 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5465 FETCH_AFTER is true if returning the result of the operation.
5466 FETCH_AFTER is false if returning the value before the operation.
5467 IGNORE is true if the result is not used.
5468 EXT_CALL is the correct builtin for an external call if this cannot be
5469 resolved to an instruction sequence. */
5471 static rtx
5472 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5473 enum rtx_code code, bool fetch_after,
5474 bool ignore, enum built_in_function ext_call)
5476 rtx val, mem, ret;
5477 enum memmodel model;
5478 tree fndecl;
5479 tree addr;
5481 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5483 /* Expand the operands. */
5484 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5485 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5487 /* Only try generating instructions if inlining is turned on. */
5488 if (flag_inline_atomics)
5490 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5491 if (ret)
5492 return ret;
5495 /* Return if a different routine isn't needed for the library call. */
5496 if (ext_call == BUILT_IN_NONE)
5497 return NULL_RTX;
5499 /* Change the call to the specified function. */
5500 fndecl = get_callee_fndecl (exp);
5501 addr = CALL_EXPR_FN (exp);
5502 STRIP_NOPS (addr);
5504 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5505 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5507 /* Expand the call here so we can emit trailing code. */
5508 ret = expand_call (exp, target, ignore);
5510 /* Replace the original function just in case it matters. */
5511 TREE_OPERAND (addr, 0) = fndecl;
5513 /* Then issue the arithmetic correction to return the right result. */
5514 if (!ignore)
5516 if (code == NOT)
5518 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5519 OPTAB_LIB_WIDEN);
5520 ret = expand_simple_unop (mode, NOT, ret, target, true);
5522 else
5523 ret = expand_simple_binop (mode, code, ret, val, target, true,
5524 OPTAB_LIB_WIDEN);
5526 return ret;
5530 #ifndef HAVE_atomic_clear
5531 # define HAVE_atomic_clear 0
5532 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5533 #endif
5535 /* Expand an atomic clear operation.
5536 void _atomic_clear (BOOL *obj, enum memmodel)
5537 EXP is the call expression. */
5539 static rtx
5540 expand_builtin_atomic_clear (tree exp)
5542 enum machine_mode mode;
5543 rtx mem, ret;
5544 enum memmodel model;
5546 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5547 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5548 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5550 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5551 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5553 error ("invalid memory model for %<__atomic_store%>");
5554 return const0_rtx;
5557 if (HAVE_atomic_clear)
5559 emit_insn (gen_atomic_clear (mem, model));
5560 return const0_rtx;
5563 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5564 Failing that, a store is issued by __atomic_store. The only way this can
5565 fail is if the bool type is larger than a word size. Unlikely, but
5566 handle it anyway for completeness. Assume a single threaded model since
5567 there is no atomic support in this case, and no barriers are required. */
5568 ret = expand_atomic_store (mem, const0_rtx, model, true);
5569 if (!ret)
5570 emit_move_insn (mem, const0_rtx);
5571 return const0_rtx;
5574 /* Expand an atomic test_and_set operation.
5575 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5576 EXP is the call expression. */
5578 static rtx
5579 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5581 rtx mem;
5582 enum memmodel model;
5583 enum machine_mode mode;
5585 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5586 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5587 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5589 return expand_atomic_test_and_set (target, mem, model);
5593 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5594 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5596 static tree
5597 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5599 int size;
5600 enum machine_mode mode;
5601 unsigned int mode_align, type_align;
5603 if (TREE_CODE (arg0) != INTEGER_CST)
5604 return NULL_TREE;
5606 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5607 mode = mode_for_size (size, MODE_INT, 0);
5608 mode_align = GET_MODE_ALIGNMENT (mode);
5610 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5611 type_align = mode_align;
5612 else
5614 tree ttype = TREE_TYPE (arg1);
5616 /* This function is usually invoked and folded immediately by the front
5617 end before anything else has a chance to look at it. The pointer
5618 parameter at this point is usually cast to a void *, so check for that
5619 and look past the cast. */
5620 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5621 && VOID_TYPE_P (TREE_TYPE (ttype)))
5622 arg1 = TREE_OPERAND (arg1, 0);
5624 ttype = TREE_TYPE (arg1);
5625 gcc_assert (POINTER_TYPE_P (ttype));
5627 /* Get the underlying type of the object. */
5628 ttype = TREE_TYPE (ttype);
5629 type_align = TYPE_ALIGN (ttype);
5632 /* If the object has smaller alignment, the the lock free routines cannot
5633 be used. */
5634 if (type_align < mode_align)
5635 return boolean_false_node;
5637 /* Check if a compare_and_swap pattern exists for the mode which represents
5638 the required size. The pattern is not allowed to fail, so the existence
5639 of the pattern indicates support is present. */
5640 if (can_compare_and_swap_p (mode, true))
5641 return boolean_true_node;
5642 else
5643 return boolean_false_node;
5646 /* Return true if the parameters to call EXP represent an object which will
5647 always generate lock free instructions. The first argument represents the
5648 size of the object, and the second parameter is a pointer to the object
5649 itself. If NULL is passed for the object, then the result is based on
5650 typical alignment for an object of the specified size. Otherwise return
5651 false. */
5653 static rtx
5654 expand_builtin_atomic_always_lock_free (tree exp)
5656 tree size;
5657 tree arg0 = CALL_EXPR_ARG (exp, 0);
5658 tree arg1 = CALL_EXPR_ARG (exp, 1);
5660 if (TREE_CODE (arg0) != INTEGER_CST)
5662 error ("non-constant argument 1 to __atomic_always_lock_free");
5663 return const0_rtx;
5666 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5667 if (size == boolean_true_node)
5668 return const1_rtx;
5669 return const0_rtx;
5672 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5673 is lock free on this architecture. */
5675 static tree
5676 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5678 if (!flag_inline_atomics)
5679 return NULL_TREE;
5681 /* If it isn't always lock free, don't generate a result. */
5682 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5683 return boolean_true_node;
5685 return NULL_TREE;
5688 /* Return true if the parameters to call EXP represent an object which will
5689 always generate lock free instructions. The first argument represents the
5690 size of the object, and the second parameter is a pointer to the object
5691 itself. If NULL is passed for the object, then the result is based on
5692 typical alignment for an object of the specified size. Otherwise return
5693 NULL*/
5695 static rtx
5696 expand_builtin_atomic_is_lock_free (tree exp)
5698 tree size;
5699 tree arg0 = CALL_EXPR_ARG (exp, 0);
5700 tree arg1 = CALL_EXPR_ARG (exp, 1);
5702 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5704 error ("non-integer argument 1 to __atomic_is_lock_free");
5705 return NULL_RTX;
5708 if (!flag_inline_atomics)
5709 return NULL_RTX;
5711 /* If the value is known at compile time, return the RTX for it. */
5712 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5713 if (size == boolean_true_node)
5714 return const1_rtx;
5716 return NULL_RTX;
5719 /* Expand the __atomic_thread_fence intrinsic:
5720 void __atomic_thread_fence (enum memmodel)
5721 EXP is the CALL_EXPR. */
5723 static void
5724 expand_builtin_atomic_thread_fence (tree exp)
5726 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5727 expand_mem_thread_fence (model);
5730 /* Expand the __atomic_signal_fence intrinsic:
5731 void __atomic_signal_fence (enum memmodel)
5732 EXP is the CALL_EXPR. */
5734 static void
5735 expand_builtin_atomic_signal_fence (tree exp)
5737 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5738 expand_mem_signal_fence (model);
5741 /* Expand the __sync_synchronize intrinsic. */
5743 static void
5744 expand_builtin_sync_synchronize (void)
5746 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5749 static rtx
5750 expand_builtin_thread_pointer (tree exp, rtx target)
5752 enum insn_code icode;
5753 if (!validate_arglist (exp, VOID_TYPE))
5754 return const0_rtx;
5755 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5756 if (icode != CODE_FOR_nothing)
5758 struct expand_operand op;
5759 if (!REG_P (target) || GET_MODE (target) != Pmode)
5760 target = gen_reg_rtx (Pmode);
5761 create_output_operand (&op, target, Pmode);
5762 expand_insn (icode, 1, &op);
5763 return target;
5765 error ("__builtin_thread_pointer is not supported on this target");
5766 return const0_rtx;
5769 static void
5770 expand_builtin_set_thread_pointer (tree exp)
5772 enum insn_code icode;
5773 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5774 return;
5775 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5776 if (icode != CODE_FOR_nothing)
5778 struct expand_operand op;
5779 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5780 Pmode, EXPAND_NORMAL);
5781 create_input_operand (&op, val, Pmode);
5782 expand_insn (icode, 1, &op);
5783 return;
5785 error ("__builtin_set_thread_pointer is not supported on this target");
5789 /* Expand an expression EXP that calls a built-in function,
5790 with result going to TARGET if that's convenient
5791 (and in mode MODE if that's convenient).
5792 SUBTARGET may be used as the target for computing one of EXP's operands.
5793 IGNORE is nonzero if the value is to be ignored. */
5796 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5797 int ignore)
5799 tree fndecl = get_callee_fndecl (exp);
5800 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5801 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5802 int flags;
5804 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5805 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5807 /* When not optimizing, generate calls to library functions for a certain
5808 set of builtins. */
5809 if (!optimize
5810 && !called_as_built_in (fndecl)
5811 && fcode != BUILT_IN_ALLOCA
5812 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5813 && fcode != BUILT_IN_FREE)
5814 return expand_call (exp, target, ignore);
5816 /* The built-in function expanders test for target == const0_rtx
5817 to determine whether the function's result will be ignored. */
5818 if (ignore)
5819 target = const0_rtx;
5821 /* If the result of a pure or const built-in function is ignored, and
5822 none of its arguments are volatile, we can avoid expanding the
5823 built-in call and just evaluate the arguments for side-effects. */
5824 if (target == const0_rtx
5825 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5826 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5828 bool volatilep = false;
5829 tree arg;
5830 call_expr_arg_iterator iter;
5832 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5833 if (TREE_THIS_VOLATILE (arg))
5835 volatilep = true;
5836 break;
5839 if (! volatilep)
5841 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5842 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5843 return const0_rtx;
5847 switch (fcode)
5849 CASE_FLT_FN (BUILT_IN_FABS):
5850 target = expand_builtin_fabs (exp, target, subtarget);
5851 if (target)
5852 return target;
5853 break;
5855 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5856 target = expand_builtin_copysign (exp, target, subtarget);
5857 if (target)
5858 return target;
5859 break;
5861 /* Just do a normal library call if we were unable to fold
5862 the values. */
5863 CASE_FLT_FN (BUILT_IN_CABS):
5864 break;
5866 CASE_FLT_FN (BUILT_IN_EXP):
5867 CASE_FLT_FN (BUILT_IN_EXP10):
5868 CASE_FLT_FN (BUILT_IN_POW10):
5869 CASE_FLT_FN (BUILT_IN_EXP2):
5870 CASE_FLT_FN (BUILT_IN_EXPM1):
5871 CASE_FLT_FN (BUILT_IN_LOGB):
5872 CASE_FLT_FN (BUILT_IN_LOG):
5873 CASE_FLT_FN (BUILT_IN_LOG10):
5874 CASE_FLT_FN (BUILT_IN_LOG2):
5875 CASE_FLT_FN (BUILT_IN_LOG1P):
5876 CASE_FLT_FN (BUILT_IN_TAN):
5877 CASE_FLT_FN (BUILT_IN_ASIN):
5878 CASE_FLT_FN (BUILT_IN_ACOS):
5879 CASE_FLT_FN (BUILT_IN_ATAN):
5880 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5881 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5882 because of possible accuracy problems. */
5883 if (! flag_unsafe_math_optimizations)
5884 break;
5885 CASE_FLT_FN (BUILT_IN_SQRT):
5886 CASE_FLT_FN (BUILT_IN_FLOOR):
5887 CASE_FLT_FN (BUILT_IN_CEIL):
5888 CASE_FLT_FN (BUILT_IN_TRUNC):
5889 CASE_FLT_FN (BUILT_IN_ROUND):
5890 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5891 CASE_FLT_FN (BUILT_IN_RINT):
5892 target = expand_builtin_mathfn (exp, target, subtarget);
5893 if (target)
5894 return target;
5895 break;
5897 CASE_FLT_FN (BUILT_IN_FMA):
5898 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5899 if (target)
5900 return target;
5901 break;
5903 CASE_FLT_FN (BUILT_IN_ILOGB):
5904 if (! flag_unsafe_math_optimizations)
5905 break;
5906 CASE_FLT_FN (BUILT_IN_ISINF):
5907 CASE_FLT_FN (BUILT_IN_FINITE):
5908 case BUILT_IN_ISFINITE:
5909 case BUILT_IN_ISNORMAL:
5910 target = expand_builtin_interclass_mathfn (exp, target);
5911 if (target)
5912 return target;
5913 break;
5915 CASE_FLT_FN (BUILT_IN_ICEIL):
5916 CASE_FLT_FN (BUILT_IN_LCEIL):
5917 CASE_FLT_FN (BUILT_IN_LLCEIL):
5918 CASE_FLT_FN (BUILT_IN_LFLOOR):
5919 CASE_FLT_FN (BUILT_IN_IFLOOR):
5920 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5921 target = expand_builtin_int_roundingfn (exp, target);
5922 if (target)
5923 return target;
5924 break;
5926 CASE_FLT_FN (BUILT_IN_IRINT):
5927 CASE_FLT_FN (BUILT_IN_LRINT):
5928 CASE_FLT_FN (BUILT_IN_LLRINT):
5929 CASE_FLT_FN (BUILT_IN_IROUND):
5930 CASE_FLT_FN (BUILT_IN_LROUND):
5931 CASE_FLT_FN (BUILT_IN_LLROUND):
5932 target = expand_builtin_int_roundingfn_2 (exp, target);
5933 if (target)
5934 return target;
5935 break;
5937 CASE_FLT_FN (BUILT_IN_POWI):
5938 target = expand_builtin_powi (exp, target);
5939 if (target)
5940 return target;
5941 break;
5943 CASE_FLT_FN (BUILT_IN_ATAN2):
5944 CASE_FLT_FN (BUILT_IN_LDEXP):
5945 CASE_FLT_FN (BUILT_IN_SCALB):
5946 CASE_FLT_FN (BUILT_IN_SCALBN):
5947 CASE_FLT_FN (BUILT_IN_SCALBLN):
5948 if (! flag_unsafe_math_optimizations)
5949 break;
5951 CASE_FLT_FN (BUILT_IN_FMOD):
5952 CASE_FLT_FN (BUILT_IN_REMAINDER):
5953 CASE_FLT_FN (BUILT_IN_DREM):
5954 CASE_FLT_FN (BUILT_IN_POW):
5955 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5956 if (target)
5957 return target;
5958 break;
5960 CASE_FLT_FN (BUILT_IN_CEXPI):
5961 target = expand_builtin_cexpi (exp, target);
5962 gcc_assert (target);
5963 return target;
5965 CASE_FLT_FN (BUILT_IN_SIN):
5966 CASE_FLT_FN (BUILT_IN_COS):
5967 if (! flag_unsafe_math_optimizations)
5968 break;
5969 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5970 if (target)
5971 return target;
5972 break;
5974 CASE_FLT_FN (BUILT_IN_SINCOS):
5975 if (! flag_unsafe_math_optimizations)
5976 break;
5977 target = expand_builtin_sincos (exp);
5978 if (target)
5979 return target;
5980 break;
5982 case BUILT_IN_APPLY_ARGS:
5983 return expand_builtin_apply_args ();
5985 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5986 FUNCTION with a copy of the parameters described by
5987 ARGUMENTS, and ARGSIZE. It returns a block of memory
5988 allocated on the stack into which is stored all the registers
5989 that might possibly be used for returning the result of a
5990 function. ARGUMENTS is the value returned by
5991 __builtin_apply_args. ARGSIZE is the number of bytes of
5992 arguments that must be copied. ??? How should this value be
5993 computed? We'll also need a safe worst case value for varargs
5994 functions. */
5995 case BUILT_IN_APPLY:
5996 if (!validate_arglist (exp, POINTER_TYPE,
5997 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5998 && !validate_arglist (exp, REFERENCE_TYPE,
5999 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6000 return const0_rtx;
6001 else
6003 rtx ops[3];
6005 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6006 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6007 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6009 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6012 /* __builtin_return (RESULT) causes the function to return the
6013 value described by RESULT. RESULT is address of the block of
6014 memory returned by __builtin_apply. */
6015 case BUILT_IN_RETURN:
6016 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6017 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6018 return const0_rtx;
6020 case BUILT_IN_SAVEREGS:
6021 return expand_builtin_saveregs ();
6023 case BUILT_IN_VA_ARG_PACK:
6024 /* All valid uses of __builtin_va_arg_pack () are removed during
6025 inlining. */
6026 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6027 return const0_rtx;
6029 case BUILT_IN_VA_ARG_PACK_LEN:
6030 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6031 inlining. */
6032 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6033 return const0_rtx;
6035 /* Return the address of the first anonymous stack arg. */
6036 case BUILT_IN_NEXT_ARG:
6037 if (fold_builtin_next_arg (exp, false))
6038 return const0_rtx;
6039 return expand_builtin_next_arg ();
6041 case BUILT_IN_CLEAR_CACHE:
6042 target = expand_builtin___clear_cache (exp);
6043 if (target)
6044 return target;
6045 break;
6047 case BUILT_IN_CLASSIFY_TYPE:
6048 return expand_builtin_classify_type (exp);
6050 case BUILT_IN_CONSTANT_P:
6051 return const0_rtx;
6053 case BUILT_IN_FRAME_ADDRESS:
6054 case BUILT_IN_RETURN_ADDRESS:
6055 return expand_builtin_frame_address (fndecl, exp);
6057 /* Returns the address of the area where the structure is returned.
6058 0 otherwise. */
6059 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6060 if (call_expr_nargs (exp) != 0
6061 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6062 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6063 return const0_rtx;
6064 else
6065 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6067 case BUILT_IN_ALLOCA:
6068 case BUILT_IN_ALLOCA_WITH_ALIGN:
6069 /* If the allocation stems from the declaration of a variable-sized
6070 object, it cannot accumulate. */
6071 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6072 if (target)
6073 return target;
6074 break;
6076 case BUILT_IN_STACK_SAVE:
6077 return expand_stack_save ();
6079 case BUILT_IN_STACK_RESTORE:
6080 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6081 return const0_rtx;
6083 case BUILT_IN_BSWAP16:
6084 case BUILT_IN_BSWAP32:
6085 case BUILT_IN_BSWAP64:
6086 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6087 if (target)
6088 return target;
6089 break;
6091 CASE_INT_FN (BUILT_IN_FFS):
6092 case BUILT_IN_FFSIMAX:
6093 target = expand_builtin_unop (target_mode, exp, target,
6094 subtarget, ffs_optab);
6095 if (target)
6096 return target;
6097 break;
6099 CASE_INT_FN (BUILT_IN_CLZ):
6100 case BUILT_IN_CLZIMAX:
6101 target = expand_builtin_unop (target_mode, exp, target,
6102 subtarget, clz_optab);
6103 if (target)
6104 return target;
6105 break;
6107 CASE_INT_FN (BUILT_IN_CTZ):
6108 case BUILT_IN_CTZIMAX:
6109 target = expand_builtin_unop (target_mode, exp, target,
6110 subtarget, ctz_optab);
6111 if (target)
6112 return target;
6113 break;
6115 CASE_INT_FN (BUILT_IN_CLRSB):
6116 case BUILT_IN_CLRSBIMAX:
6117 target = expand_builtin_unop (target_mode, exp, target,
6118 subtarget, clrsb_optab);
6119 if (target)
6120 return target;
6121 break;
6123 CASE_INT_FN (BUILT_IN_POPCOUNT):
6124 case BUILT_IN_POPCOUNTIMAX:
6125 target = expand_builtin_unop (target_mode, exp, target,
6126 subtarget, popcount_optab);
6127 if (target)
6128 return target;
6129 break;
6131 CASE_INT_FN (BUILT_IN_PARITY):
6132 case BUILT_IN_PARITYIMAX:
6133 target = expand_builtin_unop (target_mode, exp, target,
6134 subtarget, parity_optab);
6135 if (target)
6136 return target;
6137 break;
6139 case BUILT_IN_STRLEN:
6140 target = expand_builtin_strlen (exp, target, target_mode);
6141 if (target)
6142 return target;
6143 break;
6145 case BUILT_IN_STRCPY:
6146 target = expand_builtin_strcpy (exp, target);
6147 if (target)
6148 return target;
6149 break;
6151 case BUILT_IN_STRNCPY:
6152 target = expand_builtin_strncpy (exp, target);
6153 if (target)
6154 return target;
6155 break;
6157 case BUILT_IN_STPCPY:
6158 target = expand_builtin_stpcpy (exp, target, mode);
6159 if (target)
6160 return target;
6161 break;
6163 case BUILT_IN_MEMCPY:
6164 target = expand_builtin_memcpy (exp, target);
6165 if (target)
6166 return target;
6167 break;
6169 case BUILT_IN_MEMPCPY:
6170 target = expand_builtin_mempcpy (exp, target, mode);
6171 if (target)
6172 return target;
6173 break;
6175 case BUILT_IN_MEMSET:
6176 target = expand_builtin_memset (exp, target, mode);
6177 if (target)
6178 return target;
6179 break;
6181 case BUILT_IN_BZERO:
6182 target = expand_builtin_bzero (exp);
6183 if (target)
6184 return target;
6185 break;
6187 case BUILT_IN_STRCMP:
6188 target = expand_builtin_strcmp (exp, target);
6189 if (target)
6190 return target;
6191 break;
6193 case BUILT_IN_STRNCMP:
6194 target = expand_builtin_strncmp (exp, target, mode);
6195 if (target)
6196 return target;
6197 break;
6199 case BUILT_IN_BCMP:
6200 case BUILT_IN_MEMCMP:
6201 target = expand_builtin_memcmp (exp, target, mode);
6202 if (target)
6203 return target;
6204 break;
6206 case BUILT_IN_SETJMP:
6207 /* This should have been lowered to the builtins below. */
6208 gcc_unreachable ();
6210 case BUILT_IN_SETJMP_SETUP:
6211 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6212 and the receiver label. */
6213 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6215 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6216 VOIDmode, EXPAND_NORMAL);
6217 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6218 rtx label_r = label_rtx (label);
6220 /* This is copied from the handling of non-local gotos. */
6221 expand_builtin_setjmp_setup (buf_addr, label_r);
6222 nonlocal_goto_handler_labels
6223 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6224 nonlocal_goto_handler_labels);
6225 /* ??? Do not let expand_label treat us as such since we would
6226 not want to be both on the list of non-local labels and on
6227 the list of forced labels. */
6228 FORCED_LABEL (label) = 0;
6229 return const0_rtx;
6231 break;
6233 case BUILT_IN_SETJMP_DISPATCHER:
6234 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6235 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6237 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6238 rtx label_r = label_rtx (label);
6240 /* Remove the dispatcher label from the list of non-local labels
6241 since the receiver labels have been added to it above. */
6242 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6243 return const0_rtx;
6245 break;
6247 case BUILT_IN_SETJMP_RECEIVER:
6248 /* __builtin_setjmp_receiver is passed the receiver label. */
6249 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6251 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6252 rtx label_r = label_rtx (label);
6254 expand_builtin_setjmp_receiver (label_r);
6255 return const0_rtx;
6257 break;
6259 /* __builtin_longjmp is passed a pointer to an array of five words.
6260 It's similar to the C library longjmp function but works with
6261 __builtin_setjmp above. */
6262 case BUILT_IN_LONGJMP:
6263 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6265 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6266 VOIDmode, EXPAND_NORMAL);
6267 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6269 if (value != const1_rtx)
6271 error ("%<__builtin_longjmp%> second argument must be 1");
6272 return const0_rtx;
6275 expand_builtin_longjmp (buf_addr, value);
6276 return const0_rtx;
6278 break;
6280 case BUILT_IN_NONLOCAL_GOTO:
6281 target = expand_builtin_nonlocal_goto (exp);
6282 if (target)
6283 return target;
6284 break;
6286 /* This updates the setjmp buffer that is its argument with the value
6287 of the current stack pointer. */
6288 case BUILT_IN_UPDATE_SETJMP_BUF:
6289 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6291 rtx buf_addr
6292 = expand_normal (CALL_EXPR_ARG (exp, 0));
6294 expand_builtin_update_setjmp_buf (buf_addr);
6295 return const0_rtx;
6297 break;
6299 case BUILT_IN_TRAP:
6300 expand_builtin_trap ();
6301 return const0_rtx;
6303 case BUILT_IN_UNREACHABLE:
6304 expand_builtin_unreachable ();
6305 return const0_rtx;
6307 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6308 case BUILT_IN_SIGNBITD32:
6309 case BUILT_IN_SIGNBITD64:
6310 case BUILT_IN_SIGNBITD128:
6311 target = expand_builtin_signbit (exp, target);
6312 if (target)
6313 return target;
6314 break;
6316 /* Various hooks for the DWARF 2 __throw routine. */
6317 case BUILT_IN_UNWIND_INIT:
6318 expand_builtin_unwind_init ();
6319 return const0_rtx;
6320 case BUILT_IN_DWARF_CFA:
6321 return virtual_cfa_rtx;
6322 #ifdef DWARF2_UNWIND_INFO
6323 case BUILT_IN_DWARF_SP_COLUMN:
6324 return expand_builtin_dwarf_sp_column ();
6325 case BUILT_IN_INIT_DWARF_REG_SIZES:
6326 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6327 return const0_rtx;
6328 #endif
6329 case BUILT_IN_FROB_RETURN_ADDR:
6330 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6331 case BUILT_IN_EXTRACT_RETURN_ADDR:
6332 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6333 case BUILT_IN_EH_RETURN:
6334 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6335 CALL_EXPR_ARG (exp, 1));
6336 return const0_rtx;
6337 #ifdef EH_RETURN_DATA_REGNO
6338 case BUILT_IN_EH_RETURN_DATA_REGNO:
6339 return expand_builtin_eh_return_data_regno (exp);
6340 #endif
6341 case BUILT_IN_EXTEND_POINTER:
6342 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6343 case BUILT_IN_EH_POINTER:
6344 return expand_builtin_eh_pointer (exp);
6345 case BUILT_IN_EH_FILTER:
6346 return expand_builtin_eh_filter (exp);
6347 case BUILT_IN_EH_COPY_VALUES:
6348 return expand_builtin_eh_copy_values (exp);
6350 case BUILT_IN_VA_START:
6351 return expand_builtin_va_start (exp);
6352 case BUILT_IN_VA_END:
6353 return expand_builtin_va_end (exp);
6354 case BUILT_IN_VA_COPY:
6355 return expand_builtin_va_copy (exp);
6356 case BUILT_IN_EXPECT:
6357 return expand_builtin_expect (exp, target);
6358 case BUILT_IN_ASSUME_ALIGNED:
6359 return expand_builtin_assume_aligned (exp, target);
6360 case BUILT_IN_PREFETCH:
6361 expand_builtin_prefetch (exp);
6362 return const0_rtx;
6364 case BUILT_IN_INIT_TRAMPOLINE:
6365 return expand_builtin_init_trampoline (exp, true);
6366 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6367 return expand_builtin_init_trampoline (exp, false);
6368 case BUILT_IN_ADJUST_TRAMPOLINE:
6369 return expand_builtin_adjust_trampoline (exp);
6371 case BUILT_IN_FORK:
6372 case BUILT_IN_EXECL:
6373 case BUILT_IN_EXECV:
6374 case BUILT_IN_EXECLP:
6375 case BUILT_IN_EXECLE:
6376 case BUILT_IN_EXECVP:
6377 case BUILT_IN_EXECVE:
6378 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6379 if (target)
6380 return target;
6381 break;
6383 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6384 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6385 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6386 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6387 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6388 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6389 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6390 if (target)
6391 return target;
6392 break;
6394 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6395 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6396 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6397 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6398 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6399 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6400 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6401 if (target)
6402 return target;
6403 break;
6405 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6406 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6407 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6408 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6409 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6410 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6411 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6412 if (target)
6413 return target;
6414 break;
6416 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6417 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6418 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6419 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6420 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6421 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6422 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6423 if (target)
6424 return target;
6425 break;
6427 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6428 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6429 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6430 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6431 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6432 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6433 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6434 if (target)
6435 return target;
6436 break;
6438 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6439 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6440 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6441 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6442 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6443 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6444 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6445 if (target)
6446 return target;
6447 break;
6449 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6450 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6451 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6452 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6453 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6454 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6455 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6456 if (target)
6457 return target;
6458 break;
6460 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6461 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6462 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6463 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6464 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6465 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6466 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6467 if (target)
6468 return target;
6469 break;
6471 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6472 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6473 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6474 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6475 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6476 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6477 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6478 if (target)
6479 return target;
6480 break;
6482 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6483 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6484 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6485 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6486 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6487 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6488 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6489 if (target)
6490 return target;
6491 break;
6493 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6494 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6495 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6496 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6497 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6498 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6499 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6500 if (target)
6501 return target;
6502 break;
6504 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6505 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6506 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6507 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6508 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6509 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6510 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6511 if (target)
6512 return target;
6513 break;
6515 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6516 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6517 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6518 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6519 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6520 if (mode == VOIDmode)
6521 mode = TYPE_MODE (boolean_type_node);
6522 if (!target || !register_operand (target, mode))
6523 target = gen_reg_rtx (mode);
6525 mode = get_builtin_sync_mode
6526 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6527 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6528 if (target)
6529 return target;
6530 break;
6532 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6533 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6534 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6535 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6536 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6537 mode = get_builtin_sync_mode
6538 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6539 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6540 if (target)
6541 return target;
6542 break;
6544 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6545 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6546 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6547 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6548 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6549 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6550 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6551 if (target)
6552 return target;
6553 break;
6555 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6556 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6557 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6558 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6559 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6560 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6561 expand_builtin_sync_lock_release (mode, exp);
6562 return const0_rtx;
6564 case BUILT_IN_SYNC_SYNCHRONIZE:
6565 expand_builtin_sync_synchronize ();
6566 return const0_rtx;
6568 case BUILT_IN_ATOMIC_EXCHANGE_1:
6569 case BUILT_IN_ATOMIC_EXCHANGE_2:
6570 case BUILT_IN_ATOMIC_EXCHANGE_4:
6571 case BUILT_IN_ATOMIC_EXCHANGE_8:
6572 case BUILT_IN_ATOMIC_EXCHANGE_16:
6573 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6574 target = expand_builtin_atomic_exchange (mode, exp, target);
6575 if (target)
6576 return target;
6577 break;
6579 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6580 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6581 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6582 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6583 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6585 unsigned int nargs, z;
6586 vec<tree, va_gc> *vec;
6588 mode =
6589 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6590 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6591 if (target)
6592 return target;
6594 /* If this is turned into an external library call, the weak parameter
6595 must be dropped to match the expected parameter list. */
6596 nargs = call_expr_nargs (exp);
6597 vec_alloc (vec, nargs - 1);
6598 for (z = 0; z < 3; z++)
6599 vec->quick_push (CALL_EXPR_ARG (exp, z));
6600 /* Skip the boolean weak parameter. */
6601 for (z = 4; z < 6; z++)
6602 vec->quick_push (CALL_EXPR_ARG (exp, z));
6603 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6604 break;
6607 case BUILT_IN_ATOMIC_LOAD_1:
6608 case BUILT_IN_ATOMIC_LOAD_2:
6609 case BUILT_IN_ATOMIC_LOAD_4:
6610 case BUILT_IN_ATOMIC_LOAD_8:
6611 case BUILT_IN_ATOMIC_LOAD_16:
6612 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6613 target = expand_builtin_atomic_load (mode, exp, target);
6614 if (target)
6615 return target;
6616 break;
6618 case BUILT_IN_ATOMIC_STORE_1:
6619 case BUILT_IN_ATOMIC_STORE_2:
6620 case BUILT_IN_ATOMIC_STORE_4:
6621 case BUILT_IN_ATOMIC_STORE_8:
6622 case BUILT_IN_ATOMIC_STORE_16:
6623 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6624 target = expand_builtin_atomic_store (mode, exp);
6625 if (target)
6626 return const0_rtx;
6627 break;
6629 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6630 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6631 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6632 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6633 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6635 enum built_in_function lib;
6636 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6637 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6638 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6639 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6640 ignore, lib);
6641 if (target)
6642 return target;
6643 break;
6645 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6646 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6647 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6648 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6649 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6651 enum built_in_function lib;
6652 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6653 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6654 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6655 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6656 ignore, lib);
6657 if (target)
6658 return target;
6659 break;
6661 case BUILT_IN_ATOMIC_AND_FETCH_1:
6662 case BUILT_IN_ATOMIC_AND_FETCH_2:
6663 case BUILT_IN_ATOMIC_AND_FETCH_4:
6664 case BUILT_IN_ATOMIC_AND_FETCH_8:
6665 case BUILT_IN_ATOMIC_AND_FETCH_16:
6667 enum built_in_function lib;
6668 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6669 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6670 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6671 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6672 ignore, lib);
6673 if (target)
6674 return target;
6675 break;
6677 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6678 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6679 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6680 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6681 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6683 enum built_in_function lib;
6684 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6685 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6686 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6687 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6688 ignore, lib);
6689 if (target)
6690 return target;
6691 break;
6693 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6694 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6695 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6696 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6697 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6699 enum built_in_function lib;
6700 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6701 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6702 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6703 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6704 ignore, lib);
6705 if (target)
6706 return target;
6707 break;
6709 case BUILT_IN_ATOMIC_OR_FETCH_1:
6710 case BUILT_IN_ATOMIC_OR_FETCH_2:
6711 case BUILT_IN_ATOMIC_OR_FETCH_4:
6712 case BUILT_IN_ATOMIC_OR_FETCH_8:
6713 case BUILT_IN_ATOMIC_OR_FETCH_16:
6715 enum built_in_function lib;
6716 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6717 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6718 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6719 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6720 ignore, lib);
6721 if (target)
6722 return target;
6723 break;
6725 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6726 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6727 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6728 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6729 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6730 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6731 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6732 ignore, BUILT_IN_NONE);
6733 if (target)
6734 return target;
6735 break;
6737 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6738 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6739 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6740 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6741 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6742 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6743 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6744 ignore, BUILT_IN_NONE);
6745 if (target)
6746 return target;
6747 break;
6749 case BUILT_IN_ATOMIC_FETCH_AND_1:
6750 case BUILT_IN_ATOMIC_FETCH_AND_2:
6751 case BUILT_IN_ATOMIC_FETCH_AND_4:
6752 case BUILT_IN_ATOMIC_FETCH_AND_8:
6753 case BUILT_IN_ATOMIC_FETCH_AND_16:
6754 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6755 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6756 ignore, BUILT_IN_NONE);
6757 if (target)
6758 return target;
6759 break;
6761 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6762 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6763 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6764 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6765 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6766 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6767 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6768 ignore, BUILT_IN_NONE);
6769 if (target)
6770 return target;
6771 break;
6773 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6774 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6775 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6776 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6777 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6778 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6779 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6780 ignore, BUILT_IN_NONE);
6781 if (target)
6782 return target;
6783 break;
6785 case BUILT_IN_ATOMIC_FETCH_OR_1:
6786 case BUILT_IN_ATOMIC_FETCH_OR_2:
6787 case BUILT_IN_ATOMIC_FETCH_OR_4:
6788 case BUILT_IN_ATOMIC_FETCH_OR_8:
6789 case BUILT_IN_ATOMIC_FETCH_OR_16:
6790 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6791 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6792 ignore, BUILT_IN_NONE);
6793 if (target)
6794 return target;
6795 break;
6797 case BUILT_IN_ATOMIC_TEST_AND_SET:
6798 return expand_builtin_atomic_test_and_set (exp, target);
6800 case BUILT_IN_ATOMIC_CLEAR:
6801 return expand_builtin_atomic_clear (exp);
6803 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6804 return expand_builtin_atomic_always_lock_free (exp);
6806 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6807 target = expand_builtin_atomic_is_lock_free (exp);
6808 if (target)
6809 return target;
6810 break;
6812 case BUILT_IN_ATOMIC_THREAD_FENCE:
6813 expand_builtin_atomic_thread_fence (exp);
6814 return const0_rtx;
6816 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6817 expand_builtin_atomic_signal_fence (exp);
6818 return const0_rtx;
6820 case BUILT_IN_OBJECT_SIZE:
6821 return expand_builtin_object_size (exp);
6823 case BUILT_IN_MEMCPY_CHK:
6824 case BUILT_IN_MEMPCPY_CHK:
6825 case BUILT_IN_MEMMOVE_CHK:
6826 case BUILT_IN_MEMSET_CHK:
6827 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6828 if (target)
6829 return target;
6830 break;
6832 case BUILT_IN_STRCPY_CHK:
6833 case BUILT_IN_STPCPY_CHK:
6834 case BUILT_IN_STRNCPY_CHK:
6835 case BUILT_IN_STPNCPY_CHK:
6836 case BUILT_IN_STRCAT_CHK:
6837 case BUILT_IN_STRNCAT_CHK:
6838 case BUILT_IN_SNPRINTF_CHK:
6839 case BUILT_IN_VSNPRINTF_CHK:
6840 maybe_emit_chk_warning (exp, fcode);
6841 break;
6843 case BUILT_IN_SPRINTF_CHK:
6844 case BUILT_IN_VSPRINTF_CHK:
6845 maybe_emit_sprintf_chk_warning (exp, fcode);
6846 break;
6848 case BUILT_IN_FREE:
6849 if (warn_free_nonheap_object)
6850 maybe_emit_free_warning (exp);
6851 break;
6853 case BUILT_IN_THREAD_POINTER:
6854 return expand_builtin_thread_pointer (exp, target);
6856 case BUILT_IN_SET_THREAD_POINTER:
6857 expand_builtin_set_thread_pointer (exp);
6858 return const0_rtx;
6860 default: /* just do library call, if unknown builtin */
6861 break;
6864 /* The switch statement above can drop through to cause the function
6865 to be called normally. */
6866 return expand_call (exp, target, ignore);
6869 /* Determine whether a tree node represents a call to a built-in
6870 function. If the tree T is a call to a built-in function with
6871 the right number of arguments of the appropriate types, return
6872 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6873 Otherwise the return value is END_BUILTINS. */
6875 enum built_in_function
6876 builtin_mathfn_code (const_tree t)
6878 const_tree fndecl, arg, parmlist;
6879 const_tree argtype, parmtype;
6880 const_call_expr_arg_iterator iter;
6882 if (TREE_CODE (t) != CALL_EXPR
6883 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6884 return END_BUILTINS;
6886 fndecl = get_callee_fndecl (t);
6887 if (fndecl == NULL_TREE
6888 || TREE_CODE (fndecl) != FUNCTION_DECL
6889 || ! DECL_BUILT_IN (fndecl)
6890 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6891 return END_BUILTINS;
6893 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6894 init_const_call_expr_arg_iterator (t, &iter);
6895 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6897 /* If a function doesn't take a variable number of arguments,
6898 the last element in the list will have type `void'. */
6899 parmtype = TREE_VALUE (parmlist);
6900 if (VOID_TYPE_P (parmtype))
6902 if (more_const_call_expr_args_p (&iter))
6903 return END_BUILTINS;
6904 return DECL_FUNCTION_CODE (fndecl);
6907 if (! more_const_call_expr_args_p (&iter))
6908 return END_BUILTINS;
6910 arg = next_const_call_expr_arg (&iter);
6911 argtype = TREE_TYPE (arg);
6913 if (SCALAR_FLOAT_TYPE_P (parmtype))
6915 if (! SCALAR_FLOAT_TYPE_P (argtype))
6916 return END_BUILTINS;
6918 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6920 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6921 return END_BUILTINS;
6923 else if (POINTER_TYPE_P (parmtype))
6925 if (! POINTER_TYPE_P (argtype))
6926 return END_BUILTINS;
6928 else if (INTEGRAL_TYPE_P (parmtype))
6930 if (! INTEGRAL_TYPE_P (argtype))
6931 return END_BUILTINS;
6933 else
6934 return END_BUILTINS;
6937 /* Variable-length argument list. */
6938 return DECL_FUNCTION_CODE (fndecl);
6941 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6942 evaluate to a constant. */
6944 static tree
6945 fold_builtin_constant_p (tree arg)
6947 /* We return 1 for a numeric type that's known to be a constant
6948 value at compile-time or for an aggregate type that's a
6949 literal constant. */
6950 STRIP_NOPS (arg);
6952 /* If we know this is a constant, emit the constant of one. */
6953 if (CONSTANT_CLASS_P (arg)
6954 || (TREE_CODE (arg) == CONSTRUCTOR
6955 && TREE_CONSTANT (arg)))
6956 return integer_one_node;
6957 if (TREE_CODE (arg) == ADDR_EXPR)
6959 tree op = TREE_OPERAND (arg, 0);
6960 if (TREE_CODE (op) == STRING_CST
6961 || (TREE_CODE (op) == ARRAY_REF
6962 && integer_zerop (TREE_OPERAND (op, 1))
6963 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6964 return integer_one_node;
6967 /* If this expression has side effects, show we don't know it to be a
6968 constant. Likewise if it's a pointer or aggregate type since in
6969 those case we only want literals, since those are only optimized
6970 when generating RTL, not later.
6971 And finally, if we are compiling an initializer, not code, we
6972 need to return a definite result now; there's not going to be any
6973 more optimization done. */
6974 if (TREE_SIDE_EFFECTS (arg)
6975 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6976 || POINTER_TYPE_P (TREE_TYPE (arg))
6977 || cfun == 0
6978 || folding_initializer)
6979 return integer_zero_node;
6981 return NULL_TREE;
6984 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6985 return it as a truthvalue. */
6987 static tree
6988 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6990 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6992 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6993 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6994 ret_type = TREE_TYPE (TREE_TYPE (fn));
6995 pred_type = TREE_VALUE (arg_types);
6996 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6998 pred = fold_convert_loc (loc, pred_type, pred);
6999 expected = fold_convert_loc (loc, expected_type, expected);
7000 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
7002 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7003 build_int_cst (ret_type, 0));
7006 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7007 NULL_TREE if no simplification is possible. */
7009 static tree
7010 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7012 tree inner, fndecl, inner_arg0;
7013 enum tree_code code;
7015 /* Distribute the expected value over short-circuiting operators.
7016 See through the cast from truthvalue_type_node to long. */
7017 inner_arg0 = arg0;
7018 while (TREE_CODE (inner_arg0) == NOP_EXPR
7019 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7020 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7021 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7023 /* If this is a builtin_expect within a builtin_expect keep the
7024 inner one. See through a comparison against a constant. It
7025 might have been added to create a thruthvalue. */
7026 inner = inner_arg0;
7028 if (COMPARISON_CLASS_P (inner)
7029 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7030 inner = TREE_OPERAND (inner, 0);
7032 if (TREE_CODE (inner) == CALL_EXPR
7033 && (fndecl = get_callee_fndecl (inner))
7034 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7035 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7036 return arg0;
7038 inner = inner_arg0;
7039 code = TREE_CODE (inner);
7040 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7042 tree op0 = TREE_OPERAND (inner, 0);
7043 tree op1 = TREE_OPERAND (inner, 1);
7045 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7046 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7047 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7049 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7052 /* If the argument isn't invariant then there's nothing else we can do. */
7053 if (!TREE_CONSTANT (inner_arg0))
7054 return NULL_TREE;
7056 /* If we expect that a comparison against the argument will fold to
7057 a constant return the constant. In practice, this means a true
7058 constant or the address of a non-weak symbol. */
7059 inner = inner_arg0;
7060 STRIP_NOPS (inner);
7061 if (TREE_CODE (inner) == ADDR_EXPR)
7065 inner = TREE_OPERAND (inner, 0);
7067 while (TREE_CODE (inner) == COMPONENT_REF
7068 || TREE_CODE (inner) == ARRAY_REF);
7069 if ((TREE_CODE (inner) == VAR_DECL
7070 || TREE_CODE (inner) == FUNCTION_DECL)
7071 && DECL_WEAK (inner))
7072 return NULL_TREE;
7075 /* Otherwise, ARG0 already has the proper type for the return value. */
7076 return arg0;
7079 /* Fold a call to __builtin_classify_type with argument ARG. */
7081 static tree
7082 fold_builtin_classify_type (tree arg)
7084 if (arg == 0)
7085 return build_int_cst (integer_type_node, no_type_class);
7087 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7090 /* Fold a call to __builtin_strlen with argument ARG. */
7092 static tree
7093 fold_builtin_strlen (location_t loc, tree type, tree arg)
7095 if (!validate_arg (arg, POINTER_TYPE))
7096 return NULL_TREE;
7097 else
7099 tree len = c_strlen (arg, 0);
7101 if (len)
7102 return fold_convert_loc (loc, type, len);
7104 return NULL_TREE;
7108 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7110 static tree
7111 fold_builtin_inf (location_t loc, tree type, int warn)
7113 REAL_VALUE_TYPE real;
7115 /* __builtin_inff is intended to be usable to define INFINITY on all
7116 targets. If an infinity is not available, INFINITY expands "to a
7117 positive constant of type float that overflows at translation
7118 time", footnote "In this case, using INFINITY will violate the
7119 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7120 Thus we pedwarn to ensure this constraint violation is
7121 diagnosed. */
7122 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7123 pedwarn (loc, 0, "target format does not support infinity");
7125 real_inf (&real);
7126 return build_real (type, real);
7129 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7131 static tree
7132 fold_builtin_nan (tree arg, tree type, int quiet)
7134 REAL_VALUE_TYPE real;
7135 const char *str;
7137 if (!validate_arg (arg, POINTER_TYPE))
7138 return NULL_TREE;
7139 str = c_getstr (arg);
7140 if (!str)
7141 return NULL_TREE;
7143 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7144 return NULL_TREE;
7146 return build_real (type, real);
7149 /* Return true if the floating point expression T has an integer value.
7150 We also allow +Inf, -Inf and NaN to be considered integer values. */
7152 static bool
7153 integer_valued_real_p (tree t)
7155 switch (TREE_CODE (t))
7157 case FLOAT_EXPR:
7158 return true;
7160 case ABS_EXPR:
7161 case SAVE_EXPR:
7162 return integer_valued_real_p (TREE_OPERAND (t, 0));
7164 case COMPOUND_EXPR:
7165 case MODIFY_EXPR:
7166 case BIND_EXPR:
7167 return integer_valued_real_p (TREE_OPERAND (t, 1));
7169 case PLUS_EXPR:
7170 case MINUS_EXPR:
7171 case MULT_EXPR:
7172 case MIN_EXPR:
7173 case MAX_EXPR:
7174 return integer_valued_real_p (TREE_OPERAND (t, 0))
7175 && integer_valued_real_p (TREE_OPERAND (t, 1));
7177 case COND_EXPR:
7178 return integer_valued_real_p (TREE_OPERAND (t, 1))
7179 && integer_valued_real_p (TREE_OPERAND (t, 2));
7181 case REAL_CST:
7182 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7184 case NOP_EXPR:
7186 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7187 if (TREE_CODE (type) == INTEGER_TYPE)
7188 return true;
7189 if (TREE_CODE (type) == REAL_TYPE)
7190 return integer_valued_real_p (TREE_OPERAND (t, 0));
7191 break;
7194 case CALL_EXPR:
7195 switch (builtin_mathfn_code (t))
7197 CASE_FLT_FN (BUILT_IN_CEIL):
7198 CASE_FLT_FN (BUILT_IN_FLOOR):
7199 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7200 CASE_FLT_FN (BUILT_IN_RINT):
7201 CASE_FLT_FN (BUILT_IN_ROUND):
7202 CASE_FLT_FN (BUILT_IN_TRUNC):
7203 return true;
7205 CASE_FLT_FN (BUILT_IN_FMIN):
7206 CASE_FLT_FN (BUILT_IN_FMAX):
7207 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7208 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7210 default:
7211 break;
7213 break;
7215 default:
7216 break;
7218 return false;
7221 /* FNDECL is assumed to be a builtin where truncation can be propagated
7222 across (for instance floor((double)f) == (double)floorf (f).
7223 Do the transformation for a call with argument ARG. */
7225 static tree
7226 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7228 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7230 if (!validate_arg (arg, REAL_TYPE))
7231 return NULL_TREE;
7233 /* Integer rounding functions are idempotent. */
7234 if (fcode == builtin_mathfn_code (arg))
7235 return arg;
7237 /* If argument is already integer valued, and we don't need to worry
7238 about setting errno, there's no need to perform rounding. */
7239 if (! flag_errno_math && integer_valued_real_p (arg))
7240 return arg;
7242 if (optimize)
7244 tree arg0 = strip_float_extensions (arg);
7245 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7246 tree newtype = TREE_TYPE (arg0);
7247 tree decl;
7249 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7250 && (decl = mathfn_built_in (newtype, fcode)))
7251 return fold_convert_loc (loc, ftype,
7252 build_call_expr_loc (loc, decl, 1,
7253 fold_convert_loc (loc,
7254 newtype,
7255 arg0)));
7257 return NULL_TREE;
7260 /* FNDECL is assumed to be builtin which can narrow the FP type of
7261 the argument, for instance lround((double)f) -> lroundf (f).
7262 Do the transformation for a call with argument ARG. */
7264 static tree
7265 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7267 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7269 if (!validate_arg (arg, REAL_TYPE))
7270 return NULL_TREE;
7272 /* If argument is already integer valued, and we don't need to worry
7273 about setting errno, there's no need to perform rounding. */
7274 if (! flag_errno_math && integer_valued_real_p (arg))
7275 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7276 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7278 if (optimize)
7280 tree ftype = TREE_TYPE (arg);
7281 tree arg0 = strip_float_extensions (arg);
7282 tree newtype = TREE_TYPE (arg0);
7283 tree decl;
7285 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7286 && (decl = mathfn_built_in (newtype, fcode)))
7287 return build_call_expr_loc (loc, decl, 1,
7288 fold_convert_loc (loc, newtype, arg0));
7291 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7292 sizeof (int) == sizeof (long). */
7293 if (TYPE_PRECISION (integer_type_node)
7294 == TYPE_PRECISION (long_integer_type_node))
7296 tree newfn = NULL_TREE;
7297 switch (fcode)
7299 CASE_FLT_FN (BUILT_IN_ICEIL):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7301 break;
7303 CASE_FLT_FN (BUILT_IN_IFLOOR):
7304 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7305 break;
7307 CASE_FLT_FN (BUILT_IN_IROUND):
7308 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7309 break;
7311 CASE_FLT_FN (BUILT_IN_IRINT):
7312 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7313 break;
7315 default:
7316 break;
7319 if (newfn)
7321 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7322 return fold_convert_loc (loc,
7323 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7327 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7328 sizeof (long long) == sizeof (long). */
7329 if (TYPE_PRECISION (long_long_integer_type_node)
7330 == TYPE_PRECISION (long_integer_type_node))
7332 tree newfn = NULL_TREE;
7333 switch (fcode)
7335 CASE_FLT_FN (BUILT_IN_LLCEIL):
7336 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7337 break;
7339 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7340 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7341 break;
7343 CASE_FLT_FN (BUILT_IN_LLROUND):
7344 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7345 break;
7347 CASE_FLT_FN (BUILT_IN_LLRINT):
7348 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7349 break;
7351 default:
7352 break;
7355 if (newfn)
7357 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7358 return fold_convert_loc (loc,
7359 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7363 return NULL_TREE;
7366 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7367 return type. Return NULL_TREE if no simplification can be made. */
7369 static tree
7370 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7372 tree res;
7374 if (!validate_arg (arg, COMPLEX_TYPE)
7375 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7376 return NULL_TREE;
7378 /* Calculate the result when the argument is a constant. */
7379 if (TREE_CODE (arg) == COMPLEX_CST
7380 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7381 type, mpfr_hypot)))
7382 return res;
7384 if (TREE_CODE (arg) == COMPLEX_EXPR)
7386 tree real = TREE_OPERAND (arg, 0);
7387 tree imag = TREE_OPERAND (arg, 1);
7389 /* If either part is zero, cabs is fabs of the other. */
7390 if (real_zerop (real))
7391 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7392 if (real_zerop (imag))
7393 return fold_build1_loc (loc, ABS_EXPR, type, real);
7395 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7396 if (flag_unsafe_math_optimizations
7397 && operand_equal_p (real, imag, OEP_PURE_SAME))
7399 const REAL_VALUE_TYPE sqrt2_trunc
7400 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7401 STRIP_NOPS (real);
7402 return fold_build2_loc (loc, MULT_EXPR, type,
7403 fold_build1_loc (loc, ABS_EXPR, type, real),
7404 build_real (type, sqrt2_trunc));
7408 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7409 if (TREE_CODE (arg) == NEGATE_EXPR
7410 || TREE_CODE (arg) == CONJ_EXPR)
7411 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7413 /* Don't do this when optimizing for size. */
7414 if (flag_unsafe_math_optimizations
7415 && optimize && optimize_function_for_speed_p (cfun))
7417 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7419 if (sqrtfn != NULL_TREE)
7421 tree rpart, ipart, result;
7423 arg = builtin_save_expr (arg);
7425 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7426 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7428 rpart = builtin_save_expr (rpart);
7429 ipart = builtin_save_expr (ipart);
7431 result = fold_build2_loc (loc, PLUS_EXPR, type,
7432 fold_build2_loc (loc, MULT_EXPR, type,
7433 rpart, rpart),
7434 fold_build2_loc (loc, MULT_EXPR, type,
7435 ipart, ipart));
7437 return build_call_expr_loc (loc, sqrtfn, 1, result);
7441 return NULL_TREE;
7444 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7445 complex tree type of the result. If NEG is true, the imaginary
7446 zero is negative. */
7448 static tree
7449 build_complex_cproj (tree type, bool neg)
7451 REAL_VALUE_TYPE rinf, rzero = dconst0;
7453 real_inf (&rinf);
7454 rzero.sign = neg;
7455 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7456 build_real (TREE_TYPE (type), rzero));
7459 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7460 return type. Return NULL_TREE if no simplification can be made. */
7462 static tree
7463 fold_builtin_cproj (location_t loc, tree arg, tree type)
7465 if (!validate_arg (arg, COMPLEX_TYPE)
7466 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7467 return NULL_TREE;
7469 /* If there are no infinities, return arg. */
7470 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7471 return non_lvalue_loc (loc, arg);
7473 /* Calculate the result when the argument is a constant. */
7474 if (TREE_CODE (arg) == COMPLEX_CST)
7476 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7477 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7479 if (real_isinf (real) || real_isinf (imag))
7480 return build_complex_cproj (type, imag->sign);
7481 else
7482 return arg;
7484 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7486 tree real = TREE_OPERAND (arg, 0);
7487 tree imag = TREE_OPERAND (arg, 1);
7489 STRIP_NOPS (real);
7490 STRIP_NOPS (imag);
7492 /* If the real part is inf and the imag part is known to be
7493 nonnegative, return (inf + 0i). Remember side-effects are
7494 possible in the imag part. */
7495 if (TREE_CODE (real) == REAL_CST
7496 && real_isinf (TREE_REAL_CST_PTR (real))
7497 && tree_expr_nonnegative_p (imag))
7498 return omit_one_operand_loc (loc, type,
7499 build_complex_cproj (type, false),
7500 arg);
7502 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7503 Remember side-effects are possible in the real part. */
7504 if (TREE_CODE (imag) == REAL_CST
7505 && real_isinf (TREE_REAL_CST_PTR (imag)))
7506 return
7507 omit_one_operand_loc (loc, type,
7508 build_complex_cproj (type, TREE_REAL_CST_PTR
7509 (imag)->sign), arg);
7512 return NULL_TREE;
7515 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7516 Return NULL_TREE if no simplification can be made. */
7518 static tree
7519 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7522 enum built_in_function fcode;
7523 tree res;
7525 if (!validate_arg (arg, REAL_TYPE))
7526 return NULL_TREE;
7528 /* Calculate the result when the argument is a constant. */
7529 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7530 return res;
7532 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7533 fcode = builtin_mathfn_code (arg);
7534 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7536 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7537 arg = fold_build2_loc (loc, MULT_EXPR, type,
7538 CALL_EXPR_ARG (arg, 0),
7539 build_real (type, dconsthalf));
7540 return build_call_expr_loc (loc, expfn, 1, arg);
7543 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7544 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7546 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7548 if (powfn)
7550 tree arg0 = CALL_EXPR_ARG (arg, 0);
7551 tree tree_root;
7552 /* The inner root was either sqrt or cbrt. */
7553 /* This was a conditional expression but it triggered a bug
7554 in Sun C 5.5. */
7555 REAL_VALUE_TYPE dconstroot;
7556 if (BUILTIN_SQRT_P (fcode))
7557 dconstroot = dconsthalf;
7558 else
7559 dconstroot = dconst_third ();
7561 /* Adjust for the outer root. */
7562 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7563 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7564 tree_root = build_real (type, dconstroot);
7565 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7569 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7570 if (flag_unsafe_math_optimizations
7571 && (fcode == BUILT_IN_POW
7572 || fcode == BUILT_IN_POWF
7573 || fcode == BUILT_IN_POWL))
7575 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7576 tree arg0 = CALL_EXPR_ARG (arg, 0);
7577 tree arg1 = CALL_EXPR_ARG (arg, 1);
7578 tree narg1;
7579 if (!tree_expr_nonnegative_p (arg0))
7580 arg0 = build1 (ABS_EXPR, type, arg0);
7581 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7582 build_real (type, dconsthalf));
7583 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7586 return NULL_TREE;
7589 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7590 Return NULL_TREE if no simplification can be made. */
7592 static tree
7593 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7595 const enum built_in_function fcode = builtin_mathfn_code (arg);
7596 tree res;
7598 if (!validate_arg (arg, REAL_TYPE))
7599 return NULL_TREE;
7601 /* Calculate the result when the argument is a constant. */
7602 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7603 return res;
7605 if (flag_unsafe_math_optimizations)
7607 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7608 if (BUILTIN_EXPONENT_P (fcode))
7610 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7611 const REAL_VALUE_TYPE third_trunc =
7612 real_value_truncate (TYPE_MODE (type), dconst_third ());
7613 arg = fold_build2_loc (loc, MULT_EXPR, type,
7614 CALL_EXPR_ARG (arg, 0),
7615 build_real (type, third_trunc));
7616 return build_call_expr_loc (loc, expfn, 1, arg);
7619 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7620 if (BUILTIN_SQRT_P (fcode))
7622 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7624 if (powfn)
7626 tree arg0 = CALL_EXPR_ARG (arg, 0);
7627 tree tree_root;
7628 REAL_VALUE_TYPE dconstroot = dconst_third ();
7630 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7631 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7632 tree_root = build_real (type, dconstroot);
7633 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7637 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7638 if (BUILTIN_CBRT_P (fcode))
7640 tree arg0 = CALL_EXPR_ARG (arg, 0);
7641 if (tree_expr_nonnegative_p (arg0))
7643 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7645 if (powfn)
7647 tree tree_root;
7648 REAL_VALUE_TYPE dconstroot;
7650 real_arithmetic (&dconstroot, MULT_EXPR,
7651 dconst_third_ptr (), dconst_third_ptr ());
7652 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7653 tree_root = build_real (type, dconstroot);
7654 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7659 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7660 if (fcode == BUILT_IN_POW
7661 || fcode == BUILT_IN_POWF
7662 || fcode == BUILT_IN_POWL)
7664 tree arg00 = CALL_EXPR_ARG (arg, 0);
7665 tree arg01 = CALL_EXPR_ARG (arg, 1);
7666 if (tree_expr_nonnegative_p (arg00))
7668 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7669 const REAL_VALUE_TYPE dconstroot
7670 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7671 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7672 build_real (type, dconstroot));
7673 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7677 return NULL_TREE;
7680 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7681 TYPE is the type of the return value. Return NULL_TREE if no
7682 simplification can be made. */
7684 static tree
7685 fold_builtin_cos (location_t loc,
7686 tree arg, tree type, tree fndecl)
7688 tree res, narg;
7690 if (!validate_arg (arg, REAL_TYPE))
7691 return NULL_TREE;
7693 /* Calculate the result when the argument is a constant. */
7694 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7695 return res;
7697 /* Optimize cos(-x) into cos (x). */
7698 if ((narg = fold_strip_sign_ops (arg)))
7699 return build_call_expr_loc (loc, fndecl, 1, narg);
7701 return NULL_TREE;
7704 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7705 Return NULL_TREE if no simplification can be made. */
7707 static tree
7708 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7710 if (validate_arg (arg, REAL_TYPE))
7712 tree res, narg;
7714 /* Calculate the result when the argument is a constant. */
7715 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7716 return res;
7718 /* Optimize cosh(-x) into cosh (x). */
7719 if ((narg = fold_strip_sign_ops (arg)))
7720 return build_call_expr_loc (loc, fndecl, 1, narg);
7723 return NULL_TREE;
7726 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7727 argument ARG. TYPE is the type of the return value. Return
7728 NULL_TREE if no simplification can be made. */
7730 static tree
7731 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7732 bool hyper)
7734 if (validate_arg (arg, COMPLEX_TYPE)
7735 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7737 tree tmp;
7739 /* Calculate the result when the argument is a constant. */
7740 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7741 return tmp;
7743 /* Optimize fn(-x) into fn(x). */
7744 if ((tmp = fold_strip_sign_ops (arg)))
7745 return build_call_expr_loc (loc, fndecl, 1, tmp);
7748 return NULL_TREE;
7751 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7752 Return NULL_TREE if no simplification can be made. */
7754 static tree
7755 fold_builtin_tan (tree arg, tree type)
7757 enum built_in_function fcode;
7758 tree res;
7760 if (!validate_arg (arg, REAL_TYPE))
7761 return NULL_TREE;
7763 /* Calculate the result when the argument is a constant. */
7764 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7765 return res;
7767 /* Optimize tan(atan(x)) = x. */
7768 fcode = builtin_mathfn_code (arg);
7769 if (flag_unsafe_math_optimizations
7770 && (fcode == BUILT_IN_ATAN
7771 || fcode == BUILT_IN_ATANF
7772 || fcode == BUILT_IN_ATANL))
7773 return CALL_EXPR_ARG (arg, 0);
7775 return NULL_TREE;
7778 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7779 NULL_TREE if no simplification can be made. */
7781 static tree
7782 fold_builtin_sincos (location_t loc,
7783 tree arg0, tree arg1, tree arg2)
7785 tree type;
7786 tree res, fn, call;
7788 if (!validate_arg (arg0, REAL_TYPE)
7789 || !validate_arg (arg1, POINTER_TYPE)
7790 || !validate_arg (arg2, POINTER_TYPE))
7791 return NULL_TREE;
7793 type = TREE_TYPE (arg0);
7795 /* Calculate the result when the argument is a constant. */
7796 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7797 return res;
7799 /* Canonicalize sincos to cexpi. */
7800 if (!TARGET_C99_FUNCTIONS)
7801 return NULL_TREE;
7802 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7803 if (!fn)
7804 return NULL_TREE;
7806 call = build_call_expr_loc (loc, fn, 1, arg0);
7807 call = builtin_save_expr (call);
7809 return build2 (COMPOUND_EXPR, void_type_node,
7810 build2 (MODIFY_EXPR, void_type_node,
7811 build_fold_indirect_ref_loc (loc, arg1),
7812 build1 (IMAGPART_EXPR, type, call)),
7813 build2 (MODIFY_EXPR, void_type_node,
7814 build_fold_indirect_ref_loc (loc, arg2),
7815 build1 (REALPART_EXPR, type, call)));
7818 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7819 NULL_TREE if no simplification can be made. */
7821 static tree
7822 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7824 tree rtype;
7825 tree realp, imagp, ifn;
7826 tree res;
7828 if (!validate_arg (arg0, COMPLEX_TYPE)
7829 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7830 return NULL_TREE;
7832 /* Calculate the result when the argument is a constant. */
7833 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7834 return res;
7836 rtype = TREE_TYPE (TREE_TYPE (arg0));
7838 /* In case we can figure out the real part of arg0 and it is constant zero
7839 fold to cexpi. */
7840 if (!TARGET_C99_FUNCTIONS)
7841 return NULL_TREE;
7842 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7843 if (!ifn)
7844 return NULL_TREE;
7846 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7847 && real_zerop (realp))
7849 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7850 return build_call_expr_loc (loc, ifn, 1, narg);
7853 /* In case we can easily decompose real and imaginary parts split cexp
7854 to exp (r) * cexpi (i). */
7855 if (flag_unsafe_math_optimizations
7856 && realp)
7858 tree rfn, rcall, icall;
7860 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7861 if (!rfn)
7862 return NULL_TREE;
7864 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7865 if (!imagp)
7866 return NULL_TREE;
7868 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7869 icall = builtin_save_expr (icall);
7870 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7871 rcall = builtin_save_expr (rcall);
7872 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7873 fold_build2_loc (loc, MULT_EXPR, rtype,
7874 rcall,
7875 fold_build1_loc (loc, REALPART_EXPR,
7876 rtype, icall)),
7877 fold_build2_loc (loc, MULT_EXPR, rtype,
7878 rcall,
7879 fold_build1_loc (loc, IMAGPART_EXPR,
7880 rtype, icall)));
7883 return NULL_TREE;
7886 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7887 Return NULL_TREE if no simplification can be made. */
7889 static tree
7890 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7892 if (!validate_arg (arg, REAL_TYPE))
7893 return NULL_TREE;
7895 /* Optimize trunc of constant value. */
7896 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7898 REAL_VALUE_TYPE r, x;
7899 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7901 x = TREE_REAL_CST (arg);
7902 real_trunc (&r, TYPE_MODE (type), &x);
7903 return build_real (type, r);
7906 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7909 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7910 Return NULL_TREE if no simplification can be made. */
7912 static tree
7913 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7915 if (!validate_arg (arg, REAL_TYPE))
7916 return NULL_TREE;
7918 /* Optimize floor of constant value. */
7919 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7921 REAL_VALUE_TYPE x;
7923 x = TREE_REAL_CST (arg);
7924 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7926 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7927 REAL_VALUE_TYPE r;
7929 real_floor (&r, TYPE_MODE (type), &x);
7930 return build_real (type, r);
7934 /* Fold floor (x) where x is nonnegative to trunc (x). */
7935 if (tree_expr_nonnegative_p (arg))
7937 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7938 if (truncfn)
7939 return build_call_expr_loc (loc, truncfn, 1, arg);
7942 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7945 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7946 Return NULL_TREE if no simplification can be made. */
7948 static tree
7949 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7951 if (!validate_arg (arg, REAL_TYPE))
7952 return NULL_TREE;
7954 /* Optimize ceil of constant value. */
7955 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7957 REAL_VALUE_TYPE x;
7959 x = TREE_REAL_CST (arg);
7960 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7962 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7963 REAL_VALUE_TYPE r;
7965 real_ceil (&r, TYPE_MODE (type), &x);
7966 return build_real (type, r);
7970 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7973 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7974 Return NULL_TREE if no simplification can be made. */
7976 static tree
7977 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7979 if (!validate_arg (arg, REAL_TYPE))
7980 return NULL_TREE;
7982 /* Optimize round of constant value. */
7983 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7985 REAL_VALUE_TYPE x;
7987 x = TREE_REAL_CST (arg);
7988 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7990 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7991 REAL_VALUE_TYPE r;
7993 real_round (&r, TYPE_MODE (type), &x);
7994 return build_real (type, r);
7998 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8001 /* Fold function call to builtin lround, lroundf or lroundl (or the
8002 corresponding long long versions) and other rounding functions. ARG
8003 is the argument to the call. Return NULL_TREE if no simplification
8004 can be made. */
8006 static tree
8007 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8009 if (!validate_arg (arg, REAL_TYPE))
8010 return NULL_TREE;
8012 /* Optimize lround of constant value. */
8013 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8015 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8017 if (real_isfinite (&x))
8019 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8020 tree ftype = TREE_TYPE (arg);
8021 double_int val;
8022 REAL_VALUE_TYPE r;
8024 switch (DECL_FUNCTION_CODE (fndecl))
8026 CASE_FLT_FN (BUILT_IN_IFLOOR):
8027 CASE_FLT_FN (BUILT_IN_LFLOOR):
8028 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8029 real_floor (&r, TYPE_MODE (ftype), &x);
8030 break;
8032 CASE_FLT_FN (BUILT_IN_ICEIL):
8033 CASE_FLT_FN (BUILT_IN_LCEIL):
8034 CASE_FLT_FN (BUILT_IN_LLCEIL):
8035 real_ceil (&r, TYPE_MODE (ftype), &x);
8036 break;
8038 CASE_FLT_FN (BUILT_IN_IROUND):
8039 CASE_FLT_FN (BUILT_IN_LROUND):
8040 CASE_FLT_FN (BUILT_IN_LLROUND):
8041 real_round (&r, TYPE_MODE (ftype), &x);
8042 break;
8044 default:
8045 gcc_unreachable ();
8048 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8049 if (double_int_fits_to_tree_p (itype, val))
8050 return double_int_to_tree (itype, val);
8054 switch (DECL_FUNCTION_CODE (fndecl))
8056 CASE_FLT_FN (BUILT_IN_LFLOOR):
8057 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8058 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8059 if (tree_expr_nonnegative_p (arg))
8060 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8061 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8062 break;
8063 default:;
8066 return fold_fixed_mathfn (loc, fndecl, arg);
8069 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8070 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8071 the argument to the call. Return NULL_TREE if no simplification can
8072 be made. */
8074 static tree
8075 fold_builtin_bitop (tree fndecl, tree arg)
8077 if (!validate_arg (arg, INTEGER_TYPE))
8078 return NULL_TREE;
8080 /* Optimize for constant argument. */
8081 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8083 HOST_WIDE_INT hi, width, result;
8084 unsigned HOST_WIDE_INT lo;
8085 tree type;
8087 type = TREE_TYPE (arg);
8088 width = TYPE_PRECISION (type);
8089 lo = TREE_INT_CST_LOW (arg);
8091 /* Clear all the bits that are beyond the type's precision. */
8092 if (width > HOST_BITS_PER_WIDE_INT)
8094 hi = TREE_INT_CST_HIGH (arg);
8095 if (width < HOST_BITS_PER_DOUBLE_INT)
8096 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8097 << (width - HOST_BITS_PER_WIDE_INT));
8099 else
8101 hi = 0;
8102 if (width < HOST_BITS_PER_WIDE_INT)
8103 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8106 switch (DECL_FUNCTION_CODE (fndecl))
8108 CASE_INT_FN (BUILT_IN_FFS):
8109 if (lo != 0)
8110 result = ffs_hwi (lo);
8111 else if (hi != 0)
8112 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8113 else
8114 result = 0;
8115 break;
8117 CASE_INT_FN (BUILT_IN_CLZ):
8118 if (hi != 0)
8119 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8120 else if (lo != 0)
8121 result = width - floor_log2 (lo) - 1;
8122 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8123 result = width;
8124 break;
8126 CASE_INT_FN (BUILT_IN_CTZ):
8127 if (lo != 0)
8128 result = ctz_hwi (lo);
8129 else if (hi != 0)
8130 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8131 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8132 result = width;
8133 break;
8135 CASE_INT_FN (BUILT_IN_CLRSB):
8136 if (width > HOST_BITS_PER_WIDE_INT
8137 && (hi & ((unsigned HOST_WIDE_INT) 1
8138 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8140 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8141 << (width - HOST_BITS_PER_WIDE_INT - 1));
8142 lo = ~lo;
8144 else if (width <= HOST_BITS_PER_WIDE_INT
8145 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8146 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8147 if (hi != 0)
8148 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8149 else if (lo != 0)
8150 result = width - floor_log2 (lo) - 2;
8151 else
8152 result = width - 1;
8153 break;
8155 CASE_INT_FN (BUILT_IN_POPCOUNT):
8156 result = 0;
8157 while (lo)
8158 result++, lo &= lo - 1;
8159 while (hi)
8160 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8161 break;
8163 CASE_INT_FN (BUILT_IN_PARITY):
8164 result = 0;
8165 while (lo)
8166 result++, lo &= lo - 1;
8167 while (hi)
8168 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8169 result &= 1;
8170 break;
8172 default:
8173 gcc_unreachable ();
8176 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8179 return NULL_TREE;
8182 /* Fold function call to builtin_bswap and the short, long and long long
8183 variants. Return NULL_TREE if no simplification can be made. */
8184 static tree
8185 fold_builtin_bswap (tree fndecl, tree arg)
8187 if (! validate_arg (arg, INTEGER_TYPE))
8188 return NULL_TREE;
8190 /* Optimize constant value. */
8191 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8193 HOST_WIDE_INT hi, width, r_hi = 0;
8194 unsigned HOST_WIDE_INT lo, r_lo = 0;
8195 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8197 width = TYPE_PRECISION (type);
8198 lo = TREE_INT_CST_LOW (arg);
8199 hi = TREE_INT_CST_HIGH (arg);
8201 switch (DECL_FUNCTION_CODE (fndecl))
8203 case BUILT_IN_BSWAP16:
8204 case BUILT_IN_BSWAP32:
8205 case BUILT_IN_BSWAP64:
8207 int s;
8209 for (s = 0; s < width; s += 8)
8211 int d = width - s - 8;
8212 unsigned HOST_WIDE_INT byte;
8214 if (s < HOST_BITS_PER_WIDE_INT)
8215 byte = (lo >> s) & 0xff;
8216 else
8217 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8219 if (d < HOST_BITS_PER_WIDE_INT)
8220 r_lo |= byte << d;
8221 else
8222 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8226 break;
8228 default:
8229 gcc_unreachable ();
8232 if (width < HOST_BITS_PER_WIDE_INT)
8233 return build_int_cst (type, r_lo);
8234 else
8235 return build_int_cst_wide (type, r_lo, r_hi);
8238 return NULL_TREE;
8241 /* A subroutine of fold_builtin to fold the various logarithmic
8242 functions. Return NULL_TREE if no simplification can me made.
8243 FUNC is the corresponding MPFR logarithm function. */
8245 static tree
8246 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8247 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8249 if (validate_arg (arg, REAL_TYPE))
8251 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8252 tree res;
8253 const enum built_in_function fcode = builtin_mathfn_code (arg);
8255 /* Calculate the result when the argument is a constant. */
8256 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8257 return res;
8259 /* Special case, optimize logN(expN(x)) = x. */
8260 if (flag_unsafe_math_optimizations
8261 && ((func == mpfr_log
8262 && (fcode == BUILT_IN_EXP
8263 || fcode == BUILT_IN_EXPF
8264 || fcode == BUILT_IN_EXPL))
8265 || (func == mpfr_log2
8266 && (fcode == BUILT_IN_EXP2
8267 || fcode == BUILT_IN_EXP2F
8268 || fcode == BUILT_IN_EXP2L))
8269 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8270 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8272 /* Optimize logN(func()) for various exponential functions. We
8273 want to determine the value "x" and the power "exponent" in
8274 order to transform logN(x**exponent) into exponent*logN(x). */
8275 if (flag_unsafe_math_optimizations)
8277 tree exponent = 0, x = 0;
8279 switch (fcode)
8281 CASE_FLT_FN (BUILT_IN_EXP):
8282 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8283 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8284 dconst_e ()));
8285 exponent = CALL_EXPR_ARG (arg, 0);
8286 break;
8287 CASE_FLT_FN (BUILT_IN_EXP2):
8288 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8289 x = build_real (type, dconst2);
8290 exponent = CALL_EXPR_ARG (arg, 0);
8291 break;
8292 CASE_FLT_FN (BUILT_IN_EXP10):
8293 CASE_FLT_FN (BUILT_IN_POW10):
8294 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8296 REAL_VALUE_TYPE dconst10;
8297 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8298 x = build_real (type, dconst10);
8300 exponent = CALL_EXPR_ARG (arg, 0);
8301 break;
8302 CASE_FLT_FN (BUILT_IN_SQRT):
8303 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8304 x = CALL_EXPR_ARG (arg, 0);
8305 exponent = build_real (type, dconsthalf);
8306 break;
8307 CASE_FLT_FN (BUILT_IN_CBRT):
8308 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8309 x = CALL_EXPR_ARG (arg, 0);
8310 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8311 dconst_third ()));
8312 break;
8313 CASE_FLT_FN (BUILT_IN_POW):
8314 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8315 x = CALL_EXPR_ARG (arg, 0);
8316 exponent = CALL_EXPR_ARG (arg, 1);
8317 break;
8318 default:
8319 break;
8322 /* Now perform the optimization. */
8323 if (x && exponent)
8325 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8326 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8331 return NULL_TREE;
8334 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8335 NULL_TREE if no simplification can be made. */
8337 static tree
8338 fold_builtin_hypot (location_t loc, tree fndecl,
8339 tree arg0, tree arg1, tree type)
8341 tree res, narg0, narg1;
8343 if (!validate_arg (arg0, REAL_TYPE)
8344 || !validate_arg (arg1, REAL_TYPE))
8345 return NULL_TREE;
8347 /* Calculate the result when the argument is a constant. */
8348 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8349 return res;
8351 /* If either argument to hypot has a negate or abs, strip that off.
8352 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8353 narg0 = fold_strip_sign_ops (arg0);
8354 narg1 = fold_strip_sign_ops (arg1);
8355 if (narg0 || narg1)
8357 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8358 narg1 ? narg1 : arg1);
8361 /* If either argument is zero, hypot is fabs of the other. */
8362 if (real_zerop (arg0))
8363 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8364 else if (real_zerop (arg1))
8365 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8367 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8368 if (flag_unsafe_math_optimizations
8369 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8371 const REAL_VALUE_TYPE sqrt2_trunc
8372 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8373 return fold_build2_loc (loc, MULT_EXPR, type,
8374 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8375 build_real (type, sqrt2_trunc));
8378 return NULL_TREE;
8382 /* Fold a builtin function call to pow, powf, or powl. Return
8383 NULL_TREE if no simplification can be made. */
8384 static tree
8385 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8387 tree res;
8389 if (!validate_arg (arg0, REAL_TYPE)
8390 || !validate_arg (arg1, REAL_TYPE))
8391 return NULL_TREE;
8393 /* Calculate the result when the argument is a constant. */
8394 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8395 return res;
8397 /* Optimize pow(1.0,y) = 1.0. */
8398 if (real_onep (arg0))
8399 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8401 if (TREE_CODE (arg1) == REAL_CST
8402 && !TREE_OVERFLOW (arg1))
8404 REAL_VALUE_TYPE cint;
8405 REAL_VALUE_TYPE c;
8406 HOST_WIDE_INT n;
8408 c = TREE_REAL_CST (arg1);
8410 /* Optimize pow(x,0.0) = 1.0. */
8411 if (REAL_VALUES_EQUAL (c, dconst0))
8412 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8413 arg0);
8415 /* Optimize pow(x,1.0) = x. */
8416 if (REAL_VALUES_EQUAL (c, dconst1))
8417 return arg0;
8419 /* Optimize pow(x,-1.0) = 1.0/x. */
8420 if (REAL_VALUES_EQUAL (c, dconstm1))
8421 return fold_build2_loc (loc, RDIV_EXPR, type,
8422 build_real (type, dconst1), arg0);
8424 /* Optimize pow(x,0.5) = sqrt(x). */
8425 if (flag_unsafe_math_optimizations
8426 && REAL_VALUES_EQUAL (c, dconsthalf))
8428 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8430 if (sqrtfn != NULL_TREE)
8431 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8434 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8435 if (flag_unsafe_math_optimizations)
8437 const REAL_VALUE_TYPE dconstroot
8438 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8440 if (REAL_VALUES_EQUAL (c, dconstroot))
8442 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8443 if (cbrtfn != NULL_TREE)
8444 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8448 /* Check for an integer exponent. */
8449 n = real_to_integer (&c);
8450 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8451 if (real_identical (&c, &cint))
8453 /* Attempt to evaluate pow at compile-time, unless this should
8454 raise an exception. */
8455 if (TREE_CODE (arg0) == REAL_CST
8456 && !TREE_OVERFLOW (arg0)
8457 && (n > 0
8458 || (!flag_trapping_math && !flag_errno_math)
8459 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8461 REAL_VALUE_TYPE x;
8462 bool inexact;
8464 x = TREE_REAL_CST (arg0);
8465 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8466 if (flag_unsafe_math_optimizations || !inexact)
8467 return build_real (type, x);
8470 /* Strip sign ops from even integer powers. */
8471 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8473 tree narg0 = fold_strip_sign_ops (arg0);
8474 if (narg0)
8475 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8480 if (flag_unsafe_math_optimizations)
8482 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8484 /* Optimize pow(expN(x),y) = expN(x*y). */
8485 if (BUILTIN_EXPONENT_P (fcode))
8487 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8488 tree arg = CALL_EXPR_ARG (arg0, 0);
8489 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8490 return build_call_expr_loc (loc, expfn, 1, arg);
8493 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8494 if (BUILTIN_SQRT_P (fcode))
8496 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8497 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8498 build_real (type, dconsthalf));
8499 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8502 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8503 if (BUILTIN_CBRT_P (fcode))
8505 tree arg = CALL_EXPR_ARG (arg0, 0);
8506 if (tree_expr_nonnegative_p (arg))
8508 const REAL_VALUE_TYPE dconstroot
8509 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8510 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8511 build_real (type, dconstroot));
8512 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8516 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8517 if (fcode == BUILT_IN_POW
8518 || fcode == BUILT_IN_POWF
8519 || fcode == BUILT_IN_POWL)
8521 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8522 if (tree_expr_nonnegative_p (arg00))
8524 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8525 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8526 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8531 return NULL_TREE;
8534 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8535 Return NULL_TREE if no simplification can be made. */
8536 static tree
8537 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8538 tree arg0, tree arg1, tree type)
8540 if (!validate_arg (arg0, REAL_TYPE)
8541 || !validate_arg (arg1, INTEGER_TYPE))
8542 return NULL_TREE;
8544 /* Optimize pow(1.0,y) = 1.0. */
8545 if (real_onep (arg0))
8546 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8548 if (host_integerp (arg1, 0))
8550 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8552 /* Evaluate powi at compile-time. */
8553 if (TREE_CODE (arg0) == REAL_CST
8554 && !TREE_OVERFLOW (arg0))
8556 REAL_VALUE_TYPE x;
8557 x = TREE_REAL_CST (arg0);
8558 real_powi (&x, TYPE_MODE (type), &x, c);
8559 return build_real (type, x);
8562 /* Optimize pow(x,0) = 1.0. */
8563 if (c == 0)
8564 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8565 arg0);
8567 /* Optimize pow(x,1) = x. */
8568 if (c == 1)
8569 return arg0;
8571 /* Optimize pow(x,-1) = 1.0/x. */
8572 if (c == -1)
8573 return fold_build2_loc (loc, RDIV_EXPR, type,
8574 build_real (type, dconst1), arg0);
8577 return NULL_TREE;
8580 /* A subroutine of fold_builtin to fold the various exponent
8581 functions. Return NULL_TREE if no simplification can be made.
8582 FUNC is the corresponding MPFR exponent function. */
8584 static tree
8585 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8586 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8588 if (validate_arg (arg, REAL_TYPE))
8590 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8591 tree res;
8593 /* Calculate the result when the argument is a constant. */
8594 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8595 return res;
8597 /* Optimize expN(logN(x)) = x. */
8598 if (flag_unsafe_math_optimizations)
8600 const enum built_in_function fcode = builtin_mathfn_code (arg);
8602 if ((func == mpfr_exp
8603 && (fcode == BUILT_IN_LOG
8604 || fcode == BUILT_IN_LOGF
8605 || fcode == BUILT_IN_LOGL))
8606 || (func == mpfr_exp2
8607 && (fcode == BUILT_IN_LOG2
8608 || fcode == BUILT_IN_LOG2F
8609 || fcode == BUILT_IN_LOG2L))
8610 || (func == mpfr_exp10
8611 && (fcode == BUILT_IN_LOG10
8612 || fcode == BUILT_IN_LOG10F
8613 || fcode == BUILT_IN_LOG10L)))
8614 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8618 return NULL_TREE;
8621 /* Return true if VAR is a VAR_DECL or a component thereof. */
8623 static bool
8624 var_decl_component_p (tree var)
8626 tree inner = var;
8627 while (handled_component_p (inner))
8628 inner = TREE_OPERAND (inner, 0);
8629 return SSA_VAR_P (inner);
8632 /* Fold function call to builtin memset. Return
8633 NULL_TREE if no simplification can be made. */
8635 static tree
8636 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8637 tree type, bool ignore)
8639 tree var, ret, etype;
8640 unsigned HOST_WIDE_INT length, cval;
8642 if (! validate_arg (dest, POINTER_TYPE)
8643 || ! validate_arg (c, INTEGER_TYPE)
8644 || ! validate_arg (len, INTEGER_TYPE))
8645 return NULL_TREE;
8647 if (! host_integerp (len, 1))
8648 return NULL_TREE;
8650 /* If the LEN parameter is zero, return DEST. */
8651 if (integer_zerop (len))
8652 return omit_one_operand_loc (loc, type, dest, c);
8654 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8655 return NULL_TREE;
8657 var = dest;
8658 STRIP_NOPS (var);
8659 if (TREE_CODE (var) != ADDR_EXPR)
8660 return NULL_TREE;
8662 var = TREE_OPERAND (var, 0);
8663 if (TREE_THIS_VOLATILE (var))
8664 return NULL_TREE;
8666 etype = TREE_TYPE (var);
8667 if (TREE_CODE (etype) == ARRAY_TYPE)
8668 etype = TREE_TYPE (etype);
8670 if (!INTEGRAL_TYPE_P (etype)
8671 && !POINTER_TYPE_P (etype))
8672 return NULL_TREE;
8674 if (! var_decl_component_p (var))
8675 return NULL_TREE;
8677 length = tree_low_cst (len, 1);
8678 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8679 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8680 return NULL_TREE;
8682 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8683 return NULL_TREE;
8685 if (integer_zerop (c))
8686 cval = 0;
8687 else
8689 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8690 return NULL_TREE;
8692 cval = TREE_INT_CST_LOW (c);
8693 cval &= 0xff;
8694 cval |= cval << 8;
8695 cval |= cval << 16;
8696 cval |= (cval << 31) << 1;
8699 ret = build_int_cst_type (etype, cval);
8700 var = build_fold_indirect_ref_loc (loc,
8701 fold_convert_loc (loc,
8702 build_pointer_type (etype),
8703 dest));
8704 ret = build2 (MODIFY_EXPR, etype, var, ret);
8705 if (ignore)
8706 return ret;
8708 return omit_one_operand_loc (loc, type, dest, ret);
8711 /* Fold function call to builtin memset. Return
8712 NULL_TREE if no simplification can be made. */
8714 static tree
8715 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8717 if (! validate_arg (dest, POINTER_TYPE)
8718 || ! validate_arg (size, INTEGER_TYPE))
8719 return NULL_TREE;
8721 if (!ignore)
8722 return NULL_TREE;
8724 /* New argument list transforming bzero(ptr x, int y) to
8725 memset(ptr x, int 0, size_t y). This is done this way
8726 so that if it isn't expanded inline, we fallback to
8727 calling bzero instead of memset. */
8729 return fold_builtin_memset (loc, dest, integer_zero_node,
8730 fold_convert_loc (loc, size_type_node, size),
8731 void_type_node, ignore);
8734 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8735 NULL_TREE if no simplification can be made.
8736 If ENDP is 0, return DEST (like memcpy).
8737 If ENDP is 1, return DEST+LEN (like mempcpy).
8738 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8739 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8740 (memmove). */
8742 static tree
8743 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8744 tree len, tree type, bool ignore, int endp)
8746 tree destvar, srcvar, expr;
8748 if (! validate_arg (dest, POINTER_TYPE)
8749 || ! validate_arg (src, POINTER_TYPE)
8750 || ! validate_arg (len, INTEGER_TYPE))
8751 return NULL_TREE;
8753 /* If the LEN parameter is zero, return DEST. */
8754 if (integer_zerop (len))
8755 return omit_one_operand_loc (loc, type, dest, src);
8757 /* If SRC and DEST are the same (and not volatile), return
8758 DEST{,+LEN,+LEN-1}. */
8759 if (operand_equal_p (src, dest, 0))
8760 expr = len;
8761 else
8763 tree srctype, desttype;
8764 unsigned int src_align, dest_align;
8765 tree off0;
8767 if (endp == 3)
8769 src_align = get_pointer_alignment (src);
8770 dest_align = get_pointer_alignment (dest);
8772 /* Both DEST and SRC must be pointer types.
8773 ??? This is what old code did. Is the testing for pointer types
8774 really mandatory?
8776 If either SRC is readonly or length is 1, we can use memcpy. */
8777 if (!dest_align || !src_align)
8778 return NULL_TREE;
8779 if (readonly_data_expr (src)
8780 || (host_integerp (len, 1)
8781 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8782 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8784 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8785 if (!fn)
8786 return NULL_TREE;
8787 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8790 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8791 if (TREE_CODE (src) == ADDR_EXPR
8792 && TREE_CODE (dest) == ADDR_EXPR)
8794 tree src_base, dest_base, fn;
8795 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8796 HOST_WIDE_INT size = -1;
8797 HOST_WIDE_INT maxsize = -1;
8799 srcvar = TREE_OPERAND (src, 0);
8800 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8801 &size, &maxsize);
8802 destvar = TREE_OPERAND (dest, 0);
8803 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8804 &size, &maxsize);
8805 if (host_integerp (len, 1))
8806 maxsize = tree_low_cst (len, 1);
8807 else
8808 maxsize = -1;
8809 src_offset /= BITS_PER_UNIT;
8810 dest_offset /= BITS_PER_UNIT;
8811 if (SSA_VAR_P (src_base)
8812 && SSA_VAR_P (dest_base))
8814 if (operand_equal_p (src_base, dest_base, 0)
8815 && ranges_overlap_p (src_offset, maxsize,
8816 dest_offset, maxsize))
8817 return NULL_TREE;
8819 else if (TREE_CODE (src_base) == MEM_REF
8820 && TREE_CODE (dest_base) == MEM_REF)
8822 double_int off;
8823 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8824 TREE_OPERAND (dest_base, 0), 0))
8825 return NULL_TREE;
8826 off = mem_ref_offset (src_base) +
8827 double_int::from_shwi (src_offset);
8828 if (!off.fits_shwi ())
8829 return NULL_TREE;
8830 src_offset = off.low;
8831 off = mem_ref_offset (dest_base) +
8832 double_int::from_shwi (dest_offset);
8833 if (!off.fits_shwi ())
8834 return NULL_TREE;
8835 dest_offset = off.low;
8836 if (ranges_overlap_p (src_offset, maxsize,
8837 dest_offset, maxsize))
8838 return NULL_TREE;
8840 else
8841 return NULL_TREE;
8843 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8844 if (!fn)
8845 return NULL_TREE;
8846 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8849 /* If the destination and source do not alias optimize into
8850 memcpy as well. */
8851 if ((is_gimple_min_invariant (dest)
8852 || TREE_CODE (dest) == SSA_NAME)
8853 && (is_gimple_min_invariant (src)
8854 || TREE_CODE (src) == SSA_NAME))
8856 ao_ref destr, srcr;
8857 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8858 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8859 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8861 tree fn;
8862 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8863 if (!fn)
8864 return NULL_TREE;
8865 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8869 return NULL_TREE;
8872 if (!host_integerp (len, 0))
8873 return NULL_TREE;
8874 /* FIXME:
8875 This logic lose for arguments like (type *)malloc (sizeof (type)),
8876 since we strip the casts of up to VOID return value from malloc.
8877 Perhaps we ought to inherit type from non-VOID argument here? */
8878 STRIP_NOPS (src);
8879 STRIP_NOPS (dest);
8880 if (!POINTER_TYPE_P (TREE_TYPE (src))
8881 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8882 return NULL_TREE;
8883 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8884 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8886 tree tem = TREE_OPERAND (src, 0);
8887 STRIP_NOPS (tem);
8888 if (tem != TREE_OPERAND (src, 0))
8889 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8891 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8893 tree tem = TREE_OPERAND (dest, 0);
8894 STRIP_NOPS (tem);
8895 if (tem != TREE_OPERAND (dest, 0))
8896 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8898 srctype = TREE_TYPE (TREE_TYPE (src));
8899 if (TREE_CODE (srctype) == ARRAY_TYPE
8900 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8902 srctype = TREE_TYPE (srctype);
8903 STRIP_NOPS (src);
8904 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8906 desttype = TREE_TYPE (TREE_TYPE (dest));
8907 if (TREE_CODE (desttype) == ARRAY_TYPE
8908 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8910 desttype = TREE_TYPE (desttype);
8911 STRIP_NOPS (dest);
8912 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8914 if (TREE_ADDRESSABLE (srctype)
8915 || TREE_ADDRESSABLE (desttype))
8916 return NULL_TREE;
8918 src_align = get_pointer_alignment (src);
8919 dest_align = get_pointer_alignment (dest);
8920 if (dest_align < TYPE_ALIGN (desttype)
8921 || src_align < TYPE_ALIGN (srctype))
8922 return NULL_TREE;
8924 if (!ignore)
8925 dest = builtin_save_expr (dest);
8927 /* Build accesses at offset zero with a ref-all character type. */
8928 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8929 ptr_mode, true), 0);
8931 destvar = dest;
8932 STRIP_NOPS (destvar);
8933 if (TREE_CODE (destvar) == ADDR_EXPR
8934 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8935 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8936 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8937 else
8938 destvar = NULL_TREE;
8940 srcvar = src;
8941 STRIP_NOPS (srcvar);
8942 if (TREE_CODE (srcvar) == ADDR_EXPR
8943 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8944 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8946 if (!destvar
8947 || src_align >= TYPE_ALIGN (desttype))
8948 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8949 srcvar, off0);
8950 else if (!STRICT_ALIGNMENT)
8952 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8953 src_align);
8954 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8956 else
8957 srcvar = NULL_TREE;
8959 else
8960 srcvar = NULL_TREE;
8962 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8963 return NULL_TREE;
8965 if (srcvar == NULL_TREE)
8967 STRIP_NOPS (src);
8968 if (src_align >= TYPE_ALIGN (desttype))
8969 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8970 else
8972 if (STRICT_ALIGNMENT)
8973 return NULL_TREE;
8974 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8975 src_align);
8976 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8979 else if (destvar == NULL_TREE)
8981 STRIP_NOPS (dest);
8982 if (dest_align >= TYPE_ALIGN (srctype))
8983 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8984 else
8986 if (STRICT_ALIGNMENT)
8987 return NULL_TREE;
8988 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8989 dest_align);
8990 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8994 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8997 if (ignore)
8998 return expr;
9000 if (endp == 0 || endp == 3)
9001 return omit_one_operand_loc (loc, type, dest, expr);
9003 if (expr == len)
9004 expr = NULL_TREE;
9006 if (endp == 2)
9007 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9008 ssize_int (1));
9010 dest = fold_build_pointer_plus_loc (loc, dest, len);
9011 dest = fold_convert_loc (loc, type, dest);
9012 if (expr)
9013 dest = omit_one_operand_loc (loc, type, dest, expr);
9014 return dest;
9017 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9018 If LEN is not NULL, it represents the length of the string to be
9019 copied. Return NULL_TREE if no simplification can be made. */
9021 tree
9022 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9024 tree fn;
9026 if (!validate_arg (dest, POINTER_TYPE)
9027 || !validate_arg (src, POINTER_TYPE))
9028 return NULL_TREE;
9030 /* If SRC and DEST are the same (and not volatile), return DEST. */
9031 if (operand_equal_p (src, dest, 0))
9032 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9034 if (optimize_function_for_size_p (cfun))
9035 return NULL_TREE;
9037 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9038 if (!fn)
9039 return NULL_TREE;
9041 if (!len)
9043 len = c_strlen (src, 1);
9044 if (! len || TREE_SIDE_EFFECTS (len))
9045 return NULL_TREE;
9048 len = fold_convert_loc (loc, size_type_node, len);
9049 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9050 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9051 build_call_expr_loc (loc, fn, 3, dest, src, len));
9054 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9055 Return NULL_TREE if no simplification can be made. */
9057 static tree
9058 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9060 tree fn, len, lenp1, call, type;
9062 if (!validate_arg (dest, POINTER_TYPE)
9063 || !validate_arg (src, POINTER_TYPE))
9064 return NULL_TREE;
9066 len = c_strlen (src, 1);
9067 if (!len
9068 || TREE_CODE (len) != INTEGER_CST)
9069 return NULL_TREE;
9071 if (optimize_function_for_size_p (cfun)
9072 /* If length is zero it's small enough. */
9073 && !integer_zerop (len))
9074 return NULL_TREE;
9076 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9077 if (!fn)
9078 return NULL_TREE;
9080 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9081 fold_convert_loc (loc, size_type_node, len),
9082 build_int_cst (size_type_node, 1));
9083 /* We use dest twice in building our expression. Save it from
9084 multiple expansions. */
9085 dest = builtin_save_expr (dest);
9086 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9088 type = TREE_TYPE (TREE_TYPE (fndecl));
9089 dest = fold_build_pointer_plus_loc (loc, dest, len);
9090 dest = fold_convert_loc (loc, type, dest);
9091 dest = omit_one_operand_loc (loc, type, dest, call);
9092 return dest;
9095 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9096 If SLEN is not NULL, it represents the length of the source string.
9097 Return NULL_TREE if no simplification can be made. */
9099 tree
9100 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9101 tree src, tree len, tree slen)
9103 tree fn;
9105 if (!validate_arg (dest, POINTER_TYPE)
9106 || !validate_arg (src, POINTER_TYPE)
9107 || !validate_arg (len, INTEGER_TYPE))
9108 return NULL_TREE;
9110 /* If the LEN parameter is zero, return DEST. */
9111 if (integer_zerop (len))
9112 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9114 /* We can't compare slen with len as constants below if len is not a
9115 constant. */
9116 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9117 return NULL_TREE;
9119 if (!slen)
9120 slen = c_strlen (src, 1);
9122 /* Now, we must be passed a constant src ptr parameter. */
9123 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9124 return NULL_TREE;
9126 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9128 /* We do not support simplification of this case, though we do
9129 support it when expanding trees into RTL. */
9130 /* FIXME: generate a call to __builtin_memset. */
9131 if (tree_int_cst_lt (slen, len))
9132 return NULL_TREE;
9134 /* OK transform into builtin memcpy. */
9135 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9136 if (!fn)
9137 return NULL_TREE;
9139 len = fold_convert_loc (loc, size_type_node, len);
9140 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9141 build_call_expr_loc (loc, fn, 3, dest, src, len));
9144 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9145 arguments to the call, and TYPE is its return type.
9146 Return NULL_TREE if no simplification can be made. */
9148 static tree
9149 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9151 if (!validate_arg (arg1, POINTER_TYPE)
9152 || !validate_arg (arg2, INTEGER_TYPE)
9153 || !validate_arg (len, INTEGER_TYPE))
9154 return NULL_TREE;
9155 else
9157 const char *p1;
9159 if (TREE_CODE (arg2) != INTEGER_CST
9160 || !host_integerp (len, 1))
9161 return NULL_TREE;
9163 p1 = c_getstr (arg1);
9164 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9166 char c;
9167 const char *r;
9168 tree tem;
9170 if (target_char_cast (arg2, &c))
9171 return NULL_TREE;
9173 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9175 if (r == NULL)
9176 return build_int_cst (TREE_TYPE (arg1), 0);
9178 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9179 return fold_convert_loc (loc, type, tem);
9181 return NULL_TREE;
9185 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9186 Return NULL_TREE if no simplification can be made. */
9188 static tree
9189 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9191 const char *p1, *p2;
9193 if (!validate_arg (arg1, POINTER_TYPE)
9194 || !validate_arg (arg2, POINTER_TYPE)
9195 || !validate_arg (len, INTEGER_TYPE))
9196 return NULL_TREE;
9198 /* If the LEN parameter is zero, return zero. */
9199 if (integer_zerop (len))
9200 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9201 arg1, arg2);
9203 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9204 if (operand_equal_p (arg1, arg2, 0))
9205 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9207 p1 = c_getstr (arg1);
9208 p2 = c_getstr (arg2);
9210 /* If all arguments are constant, and the value of len is not greater
9211 than the lengths of arg1 and arg2, evaluate at compile-time. */
9212 if (host_integerp (len, 1) && p1 && p2
9213 && compare_tree_int (len, strlen (p1) + 1) <= 0
9214 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9216 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9218 if (r > 0)
9219 return integer_one_node;
9220 else if (r < 0)
9221 return integer_minus_one_node;
9222 else
9223 return integer_zero_node;
9226 /* If len parameter is one, return an expression corresponding to
9227 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9228 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9230 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9231 tree cst_uchar_ptr_node
9232 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9234 tree ind1
9235 = fold_convert_loc (loc, integer_type_node,
9236 build1 (INDIRECT_REF, cst_uchar_node,
9237 fold_convert_loc (loc,
9238 cst_uchar_ptr_node,
9239 arg1)));
9240 tree ind2
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 arg2)));
9246 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9249 return NULL_TREE;
9252 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9253 Return NULL_TREE if no simplification can be made. */
9255 static tree
9256 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9258 const char *p1, *p2;
9260 if (!validate_arg (arg1, POINTER_TYPE)
9261 || !validate_arg (arg2, POINTER_TYPE))
9262 return NULL_TREE;
9264 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9265 if (operand_equal_p (arg1, arg2, 0))
9266 return integer_zero_node;
9268 p1 = c_getstr (arg1);
9269 p2 = c_getstr (arg2);
9271 if (p1 && p2)
9273 const int i = strcmp (p1, p2);
9274 if (i < 0)
9275 return integer_minus_one_node;
9276 else if (i > 0)
9277 return integer_one_node;
9278 else
9279 return integer_zero_node;
9282 /* If the second arg is "", return *(const unsigned char*)arg1. */
9283 if (p2 && *p2 == '\0')
9285 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9286 tree cst_uchar_ptr_node
9287 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9289 return fold_convert_loc (loc, integer_type_node,
9290 build1 (INDIRECT_REF, cst_uchar_node,
9291 fold_convert_loc (loc,
9292 cst_uchar_ptr_node,
9293 arg1)));
9296 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9297 if (p1 && *p1 == '\0')
9299 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9300 tree cst_uchar_ptr_node
9301 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9303 tree temp
9304 = fold_convert_loc (loc, integer_type_node,
9305 build1 (INDIRECT_REF, cst_uchar_node,
9306 fold_convert_loc (loc,
9307 cst_uchar_ptr_node,
9308 arg2)));
9309 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9312 return NULL_TREE;
9315 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9316 Return NULL_TREE if no simplification can be made. */
9318 static tree
9319 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9321 const char *p1, *p2;
9323 if (!validate_arg (arg1, POINTER_TYPE)
9324 || !validate_arg (arg2, POINTER_TYPE)
9325 || !validate_arg (len, INTEGER_TYPE))
9326 return NULL_TREE;
9328 /* If the LEN parameter is zero, return zero. */
9329 if (integer_zerop (len))
9330 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9331 arg1, arg2);
9333 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9334 if (operand_equal_p (arg1, arg2, 0))
9335 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9337 p1 = c_getstr (arg1);
9338 p2 = c_getstr (arg2);
9340 if (host_integerp (len, 1) && p1 && p2)
9342 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9343 if (i > 0)
9344 return integer_one_node;
9345 else if (i < 0)
9346 return integer_minus_one_node;
9347 else
9348 return integer_zero_node;
9351 /* If the second arg is "", and the length is greater than zero,
9352 return *(const unsigned char*)arg1. */
9353 if (p2 && *p2 == '\0'
9354 && TREE_CODE (len) == INTEGER_CST
9355 && tree_int_cst_sgn (len) == 1)
9357 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9358 tree cst_uchar_ptr_node
9359 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9361 return fold_convert_loc (loc, integer_type_node,
9362 build1 (INDIRECT_REF, cst_uchar_node,
9363 fold_convert_loc (loc,
9364 cst_uchar_ptr_node,
9365 arg1)));
9368 /* If the first arg is "", and the length is greater than zero,
9369 return -*(const unsigned char*)arg2. */
9370 if (p1 && *p1 == '\0'
9371 && TREE_CODE (len) == INTEGER_CST
9372 && tree_int_cst_sgn (len) == 1)
9374 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9375 tree cst_uchar_ptr_node
9376 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9378 tree temp = fold_convert_loc (loc, integer_type_node,
9379 build1 (INDIRECT_REF, cst_uchar_node,
9380 fold_convert_loc (loc,
9381 cst_uchar_ptr_node,
9382 arg2)));
9383 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9386 /* If len parameter is one, return an expression corresponding to
9387 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9388 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9390 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9391 tree cst_uchar_ptr_node
9392 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9394 tree ind1 = fold_convert_loc (loc, integer_type_node,
9395 build1 (INDIRECT_REF, cst_uchar_node,
9396 fold_convert_loc (loc,
9397 cst_uchar_ptr_node,
9398 arg1)));
9399 tree ind2 = fold_convert_loc (loc, integer_type_node,
9400 build1 (INDIRECT_REF, cst_uchar_node,
9401 fold_convert_loc (loc,
9402 cst_uchar_ptr_node,
9403 arg2)));
9404 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9407 return NULL_TREE;
9410 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9411 ARG. Return NULL_TREE if no simplification can be made. */
9413 static tree
9414 fold_builtin_signbit (location_t loc, tree arg, tree type)
9416 if (!validate_arg (arg, REAL_TYPE))
9417 return NULL_TREE;
9419 /* If ARG is a compile-time constant, determine the result. */
9420 if (TREE_CODE (arg) == REAL_CST
9421 && !TREE_OVERFLOW (arg))
9423 REAL_VALUE_TYPE c;
9425 c = TREE_REAL_CST (arg);
9426 return (REAL_VALUE_NEGATIVE (c)
9427 ? build_one_cst (type)
9428 : build_zero_cst (type));
9431 /* If ARG is non-negative, the result is always zero. */
9432 if (tree_expr_nonnegative_p (arg))
9433 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9435 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9436 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9437 return fold_convert (type,
9438 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9439 build_real (TREE_TYPE (arg), dconst0)));
9441 return NULL_TREE;
9444 /* Fold function call to builtin copysign, copysignf or copysignl with
9445 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9446 be made. */
9448 static tree
9449 fold_builtin_copysign (location_t loc, tree fndecl,
9450 tree arg1, tree arg2, tree type)
9452 tree tem;
9454 if (!validate_arg (arg1, REAL_TYPE)
9455 || !validate_arg (arg2, REAL_TYPE))
9456 return NULL_TREE;
9458 /* copysign(X,X) is X. */
9459 if (operand_equal_p (arg1, arg2, 0))
9460 return fold_convert_loc (loc, type, arg1);
9462 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9463 if (TREE_CODE (arg1) == REAL_CST
9464 && TREE_CODE (arg2) == REAL_CST
9465 && !TREE_OVERFLOW (arg1)
9466 && !TREE_OVERFLOW (arg2))
9468 REAL_VALUE_TYPE c1, c2;
9470 c1 = TREE_REAL_CST (arg1);
9471 c2 = TREE_REAL_CST (arg2);
9472 /* c1.sign := c2.sign. */
9473 real_copysign (&c1, &c2);
9474 return build_real (type, c1);
9477 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9478 Remember to evaluate Y for side-effects. */
9479 if (tree_expr_nonnegative_p (arg2))
9480 return omit_one_operand_loc (loc, type,
9481 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9482 arg2);
9484 /* Strip sign changing operations for the first argument. */
9485 tem = fold_strip_sign_ops (arg1);
9486 if (tem)
9487 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9489 return NULL_TREE;
9492 /* Fold a call to builtin isascii with argument ARG. */
9494 static tree
9495 fold_builtin_isascii (location_t loc, tree arg)
9497 if (!validate_arg (arg, INTEGER_TYPE))
9498 return NULL_TREE;
9499 else
9501 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9502 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9503 build_int_cst (integer_type_node,
9504 ~ (unsigned HOST_WIDE_INT) 0x7f));
9505 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9506 arg, integer_zero_node);
9510 /* Fold a call to builtin toascii with argument ARG. */
9512 static tree
9513 fold_builtin_toascii (location_t loc, tree arg)
9515 if (!validate_arg (arg, INTEGER_TYPE))
9516 return NULL_TREE;
9518 /* Transform toascii(c) -> (c & 0x7f). */
9519 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9520 build_int_cst (integer_type_node, 0x7f));
9523 /* Fold a call to builtin isdigit with argument ARG. */
9525 static tree
9526 fold_builtin_isdigit (location_t loc, tree arg)
9528 if (!validate_arg (arg, INTEGER_TYPE))
9529 return NULL_TREE;
9530 else
9532 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9533 /* According to the C standard, isdigit is unaffected by locale.
9534 However, it definitely is affected by the target character set. */
9535 unsigned HOST_WIDE_INT target_digit0
9536 = lang_hooks.to_target_charset ('0');
9538 if (target_digit0 == 0)
9539 return NULL_TREE;
9541 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9542 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9543 build_int_cst (unsigned_type_node, target_digit0));
9544 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9545 build_int_cst (unsigned_type_node, 9));
9549 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9551 static tree
9552 fold_builtin_fabs (location_t loc, tree arg, tree type)
9554 if (!validate_arg (arg, REAL_TYPE))
9555 return NULL_TREE;
9557 arg = fold_convert_loc (loc, type, arg);
9558 if (TREE_CODE (arg) == REAL_CST)
9559 return fold_abs_const (arg, type);
9560 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9563 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9565 static tree
9566 fold_builtin_abs (location_t loc, tree arg, tree type)
9568 if (!validate_arg (arg, INTEGER_TYPE))
9569 return NULL_TREE;
9571 arg = fold_convert_loc (loc, type, arg);
9572 if (TREE_CODE (arg) == INTEGER_CST)
9573 return fold_abs_const (arg, type);
9574 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9577 /* Fold a fma operation with arguments ARG[012]. */
9579 tree
9580 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9581 tree type, tree arg0, tree arg1, tree arg2)
9583 if (TREE_CODE (arg0) == REAL_CST
9584 && TREE_CODE (arg1) == REAL_CST
9585 && TREE_CODE (arg2) == REAL_CST)
9586 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9588 return NULL_TREE;
9591 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9593 static tree
9594 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9596 if (validate_arg (arg0, REAL_TYPE)
9597 && validate_arg(arg1, REAL_TYPE)
9598 && validate_arg(arg2, REAL_TYPE))
9600 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9601 if (tem)
9602 return tem;
9604 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9605 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9606 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9608 return NULL_TREE;
9611 /* Fold a call to builtin fmin or fmax. */
9613 static tree
9614 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9615 tree type, bool max)
9617 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9619 /* Calculate the result when the argument is a constant. */
9620 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9622 if (res)
9623 return res;
9625 /* If either argument is NaN, return the other one. Avoid the
9626 transformation if we get (and honor) a signalling NaN. Using
9627 omit_one_operand() ensures we create a non-lvalue. */
9628 if (TREE_CODE (arg0) == REAL_CST
9629 && real_isnan (&TREE_REAL_CST (arg0))
9630 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9631 || ! TREE_REAL_CST (arg0).signalling))
9632 return omit_one_operand_loc (loc, type, arg1, arg0);
9633 if (TREE_CODE (arg1) == REAL_CST
9634 && real_isnan (&TREE_REAL_CST (arg1))
9635 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9636 || ! TREE_REAL_CST (arg1).signalling))
9637 return omit_one_operand_loc (loc, type, arg0, arg1);
9639 /* Transform fmin/fmax(x,x) -> x. */
9640 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9641 return omit_one_operand_loc (loc, type, arg0, arg1);
9643 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9644 functions to return the numeric arg if the other one is NaN.
9645 These tree codes don't honor that, so only transform if
9646 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9647 handled, so we don't have to worry about it either. */
9648 if (flag_finite_math_only)
9649 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9650 fold_convert_loc (loc, type, arg0),
9651 fold_convert_loc (loc, type, arg1));
9653 return NULL_TREE;
9656 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9658 static tree
9659 fold_builtin_carg (location_t loc, tree arg, tree type)
9661 if (validate_arg (arg, COMPLEX_TYPE)
9662 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9664 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9666 if (atan2_fn)
9668 tree new_arg = builtin_save_expr (arg);
9669 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9670 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9671 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9675 return NULL_TREE;
9678 /* Fold a call to builtin logb/ilogb. */
9680 static tree
9681 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9683 if (! validate_arg (arg, REAL_TYPE))
9684 return NULL_TREE;
9686 STRIP_NOPS (arg);
9688 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9690 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9692 switch (value->cl)
9694 case rvc_nan:
9695 case rvc_inf:
9696 /* If arg is Inf or NaN and we're logb, return it. */
9697 if (TREE_CODE (rettype) == REAL_TYPE)
9698 return fold_convert_loc (loc, rettype, arg);
9699 /* Fall through... */
9700 case rvc_zero:
9701 /* Zero may set errno and/or raise an exception for logb, also
9702 for ilogb we don't know FP_ILOGB0. */
9703 return NULL_TREE;
9704 case rvc_normal:
9705 /* For normal numbers, proceed iff radix == 2. In GCC,
9706 normalized significands are in the range [0.5, 1.0). We
9707 want the exponent as if they were [1.0, 2.0) so get the
9708 exponent and subtract 1. */
9709 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9710 return fold_convert_loc (loc, rettype,
9711 build_int_cst (integer_type_node,
9712 REAL_EXP (value)-1));
9713 break;
9717 return NULL_TREE;
9720 /* Fold a call to builtin significand, if radix == 2. */
9722 static tree
9723 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9725 if (! validate_arg (arg, REAL_TYPE))
9726 return NULL_TREE;
9728 STRIP_NOPS (arg);
9730 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9732 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9734 switch (value->cl)
9736 case rvc_zero:
9737 case rvc_nan:
9738 case rvc_inf:
9739 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9740 return fold_convert_loc (loc, rettype, arg);
9741 case rvc_normal:
9742 /* For normal numbers, proceed iff radix == 2. */
9743 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9745 REAL_VALUE_TYPE result = *value;
9746 /* In GCC, normalized significands are in the range [0.5,
9747 1.0). We want them to be [1.0, 2.0) so set the
9748 exponent to 1. */
9749 SET_REAL_EXP (&result, 1);
9750 return build_real (rettype, result);
9752 break;
9756 return NULL_TREE;
9759 /* Fold a call to builtin frexp, we can assume the base is 2. */
9761 static tree
9762 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9764 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9765 return NULL_TREE;
9767 STRIP_NOPS (arg0);
9769 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9770 return NULL_TREE;
9772 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9774 /* Proceed if a valid pointer type was passed in. */
9775 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9777 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9778 tree frac, exp;
9780 switch (value->cl)
9782 case rvc_zero:
9783 /* For +-0, return (*exp = 0, +-0). */
9784 exp = integer_zero_node;
9785 frac = arg0;
9786 break;
9787 case rvc_nan:
9788 case rvc_inf:
9789 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9790 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9791 case rvc_normal:
9793 /* Since the frexp function always expects base 2, and in
9794 GCC normalized significands are already in the range
9795 [0.5, 1.0), we have exactly what frexp wants. */
9796 REAL_VALUE_TYPE frac_rvt = *value;
9797 SET_REAL_EXP (&frac_rvt, 0);
9798 frac = build_real (rettype, frac_rvt);
9799 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9801 break;
9802 default:
9803 gcc_unreachable ();
9806 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9807 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9808 TREE_SIDE_EFFECTS (arg1) = 1;
9809 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9812 return NULL_TREE;
9815 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9816 then we can assume the base is two. If it's false, then we have to
9817 check the mode of the TYPE parameter in certain cases. */
9819 static tree
9820 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9821 tree type, bool ldexp)
9823 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9825 STRIP_NOPS (arg0);
9826 STRIP_NOPS (arg1);
9828 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9829 if (real_zerop (arg0) || integer_zerop (arg1)
9830 || (TREE_CODE (arg0) == REAL_CST
9831 && !real_isfinite (&TREE_REAL_CST (arg0))))
9832 return omit_one_operand_loc (loc, type, arg0, arg1);
9834 /* If both arguments are constant, then try to evaluate it. */
9835 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9836 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9837 && host_integerp (arg1, 0))
9839 /* Bound the maximum adjustment to twice the range of the
9840 mode's valid exponents. Use abs to ensure the range is
9841 positive as a sanity check. */
9842 const long max_exp_adj = 2 *
9843 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9844 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9846 /* Get the user-requested adjustment. */
9847 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9849 /* The requested adjustment must be inside this range. This
9850 is a preliminary cap to avoid things like overflow, we
9851 may still fail to compute the result for other reasons. */
9852 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9854 REAL_VALUE_TYPE initial_result;
9856 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9858 /* Ensure we didn't overflow. */
9859 if (! real_isinf (&initial_result))
9861 const REAL_VALUE_TYPE trunc_result
9862 = real_value_truncate (TYPE_MODE (type), initial_result);
9864 /* Only proceed if the target mode can hold the
9865 resulting value. */
9866 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9867 return build_real (type, trunc_result);
9873 return NULL_TREE;
9876 /* Fold a call to builtin modf. */
9878 static tree
9879 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9881 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9882 return NULL_TREE;
9884 STRIP_NOPS (arg0);
9886 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9887 return NULL_TREE;
9889 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9891 /* Proceed if a valid pointer type was passed in. */
9892 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9894 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9895 REAL_VALUE_TYPE trunc, frac;
9897 switch (value->cl)
9899 case rvc_nan:
9900 case rvc_zero:
9901 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9902 trunc = frac = *value;
9903 break;
9904 case rvc_inf:
9905 /* For +-Inf, return (*arg1 = arg0, +-0). */
9906 frac = dconst0;
9907 frac.sign = value->sign;
9908 trunc = *value;
9909 break;
9910 case rvc_normal:
9911 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9912 real_trunc (&trunc, VOIDmode, value);
9913 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9914 /* If the original number was negative and already
9915 integral, then the fractional part is -0.0. */
9916 if (value->sign && frac.cl == rvc_zero)
9917 frac.sign = value->sign;
9918 break;
9921 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9922 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9923 build_real (rettype, trunc));
9924 TREE_SIDE_EFFECTS (arg1) = 1;
9925 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9926 build_real (rettype, frac));
9929 return NULL_TREE;
9932 /* Given a location LOC, an interclass builtin function decl FNDECL
9933 and its single argument ARG, return an folded expression computing
9934 the same, or NULL_TREE if we either couldn't or didn't want to fold
9935 (the latter happen if there's an RTL instruction available). */
9937 static tree
9938 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9940 enum machine_mode mode;
9942 if (!validate_arg (arg, REAL_TYPE))
9943 return NULL_TREE;
9945 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9946 return NULL_TREE;
9948 mode = TYPE_MODE (TREE_TYPE (arg));
9950 /* If there is no optab, try generic code. */
9951 switch (DECL_FUNCTION_CODE (fndecl))
9953 tree result;
9955 CASE_FLT_FN (BUILT_IN_ISINF):
9957 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9958 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9959 tree const type = TREE_TYPE (arg);
9960 REAL_VALUE_TYPE r;
9961 char buf[128];
9963 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9964 real_from_string (&r, buf);
9965 result = build_call_expr (isgr_fn, 2,
9966 fold_build1_loc (loc, ABS_EXPR, type, arg),
9967 build_real (type, r));
9968 return result;
9970 CASE_FLT_FN (BUILT_IN_FINITE):
9971 case BUILT_IN_ISFINITE:
9973 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9974 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9975 tree const type = TREE_TYPE (arg);
9976 REAL_VALUE_TYPE r;
9977 char buf[128];
9979 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9980 real_from_string (&r, buf);
9981 result = build_call_expr (isle_fn, 2,
9982 fold_build1_loc (loc, ABS_EXPR, type, arg),
9983 build_real (type, r));
9984 /*result = fold_build2_loc (loc, UNGT_EXPR,
9985 TREE_TYPE (TREE_TYPE (fndecl)),
9986 fold_build1_loc (loc, ABS_EXPR, type, arg),
9987 build_real (type, r));
9988 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9989 TREE_TYPE (TREE_TYPE (fndecl)),
9990 result);*/
9991 return result;
9993 case BUILT_IN_ISNORMAL:
9995 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9996 islessequal(fabs(x),DBL_MAX). */
9997 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9998 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9999 tree const type = TREE_TYPE (arg);
10000 REAL_VALUE_TYPE rmax, rmin;
10001 char buf[128];
10003 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10004 real_from_string (&rmax, buf);
10005 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10006 real_from_string (&rmin, buf);
10007 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10008 result = build_call_expr (isle_fn, 2, arg,
10009 build_real (type, rmax));
10010 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10011 build_call_expr (isge_fn, 2, arg,
10012 build_real (type, rmin)));
10013 return result;
10015 default:
10016 break;
10019 return NULL_TREE;
10022 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10023 ARG is the argument for the call. */
10025 static tree
10026 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10028 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10029 REAL_VALUE_TYPE r;
10031 if (!validate_arg (arg, REAL_TYPE))
10032 return NULL_TREE;
10034 switch (builtin_index)
10036 case BUILT_IN_ISINF:
10037 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10038 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10040 if (TREE_CODE (arg) == REAL_CST)
10042 r = TREE_REAL_CST (arg);
10043 if (real_isinf (&r))
10044 return real_compare (GT_EXPR, &r, &dconst0)
10045 ? integer_one_node : integer_minus_one_node;
10046 else
10047 return integer_zero_node;
10050 return NULL_TREE;
10052 case BUILT_IN_ISINF_SIGN:
10054 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10055 /* In a boolean context, GCC will fold the inner COND_EXPR to
10056 1. So e.g. "if (isinf_sign(x))" would be folded to just
10057 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10058 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10059 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10060 tree tmp = NULL_TREE;
10062 arg = builtin_save_expr (arg);
10064 if (signbit_fn && isinf_fn)
10066 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10067 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10069 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10070 signbit_call, integer_zero_node);
10071 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10072 isinf_call, integer_zero_node);
10074 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10075 integer_minus_one_node, integer_one_node);
10076 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10077 isinf_call, tmp,
10078 integer_zero_node);
10081 return tmp;
10084 case BUILT_IN_ISFINITE:
10085 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10086 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10087 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10089 if (TREE_CODE (arg) == REAL_CST)
10091 r = TREE_REAL_CST (arg);
10092 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10095 return NULL_TREE;
10097 case BUILT_IN_ISNAN:
10098 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10099 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10101 if (TREE_CODE (arg) == REAL_CST)
10103 r = TREE_REAL_CST (arg);
10104 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10107 arg = builtin_save_expr (arg);
10108 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10110 default:
10111 gcc_unreachable ();
10115 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10116 This builtin will generate code to return the appropriate floating
10117 point classification depending on the value of the floating point
10118 number passed in. The possible return values must be supplied as
10119 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10120 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10121 one floating point argument which is "type generic". */
10123 static tree
10124 fold_builtin_fpclassify (location_t loc, tree exp)
10126 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10127 arg, type, res, tmp;
10128 enum machine_mode mode;
10129 REAL_VALUE_TYPE r;
10130 char buf[128];
10132 /* Verify the required arguments in the original call. */
10133 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10134 INTEGER_TYPE, INTEGER_TYPE,
10135 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10136 return NULL_TREE;
10138 fp_nan = CALL_EXPR_ARG (exp, 0);
10139 fp_infinite = CALL_EXPR_ARG (exp, 1);
10140 fp_normal = CALL_EXPR_ARG (exp, 2);
10141 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10142 fp_zero = CALL_EXPR_ARG (exp, 4);
10143 arg = CALL_EXPR_ARG (exp, 5);
10144 type = TREE_TYPE (arg);
10145 mode = TYPE_MODE (type);
10146 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10148 /* fpclassify(x) ->
10149 isnan(x) ? FP_NAN :
10150 (fabs(x) == Inf ? FP_INFINITE :
10151 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10152 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10154 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10155 build_real (type, dconst0));
10156 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10157 tmp, fp_zero, fp_subnormal);
10159 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10160 real_from_string (&r, buf);
10161 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10162 arg, build_real (type, r));
10163 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10165 if (HONOR_INFINITIES (mode))
10167 real_inf (&r);
10168 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10169 build_real (type, r));
10170 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10171 fp_infinite, res);
10174 if (HONOR_NANS (mode))
10176 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10177 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10180 return res;
10183 /* Fold a call to an unordered comparison function such as
10184 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10185 being called and ARG0 and ARG1 are the arguments for the call.
10186 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10187 the opposite of the desired result. UNORDERED_CODE is used
10188 for modes that can hold NaNs and ORDERED_CODE is used for
10189 the rest. */
10191 static tree
10192 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10193 enum tree_code unordered_code,
10194 enum tree_code ordered_code)
10196 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10197 enum tree_code code;
10198 tree type0, type1;
10199 enum tree_code code0, code1;
10200 tree cmp_type = NULL_TREE;
10202 type0 = TREE_TYPE (arg0);
10203 type1 = TREE_TYPE (arg1);
10205 code0 = TREE_CODE (type0);
10206 code1 = TREE_CODE (type1);
10208 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10209 /* Choose the wider of two real types. */
10210 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10211 ? type0 : type1;
10212 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10213 cmp_type = type0;
10214 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10215 cmp_type = type1;
10217 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10218 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10220 if (unordered_code == UNORDERED_EXPR)
10222 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10223 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10224 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10227 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10228 : ordered_code;
10229 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10230 fold_build2_loc (loc, code, type, arg0, arg1));
10233 /* Fold a call to built-in function FNDECL with 0 arguments.
10234 IGNORE is true if the result of the function call is ignored. This
10235 function returns NULL_TREE if no simplification was possible. */
10237 static tree
10238 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10240 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10241 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10242 switch (fcode)
10244 CASE_FLT_FN (BUILT_IN_INF):
10245 case BUILT_IN_INFD32:
10246 case BUILT_IN_INFD64:
10247 case BUILT_IN_INFD128:
10248 return fold_builtin_inf (loc, type, true);
10250 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10251 return fold_builtin_inf (loc, type, false);
10253 case BUILT_IN_CLASSIFY_TYPE:
10254 return fold_builtin_classify_type (NULL_TREE);
10256 default:
10257 break;
10259 return NULL_TREE;
10262 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10263 IGNORE is true if the result of the function call is ignored. This
10264 function returns NULL_TREE if no simplification was possible. */
10266 static tree
10267 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10269 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10270 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10271 switch (fcode)
10273 case BUILT_IN_CONSTANT_P:
10275 tree val = fold_builtin_constant_p (arg0);
10277 /* Gimplification will pull the CALL_EXPR for the builtin out of
10278 an if condition. When not optimizing, we'll not CSE it back.
10279 To avoid link error types of regressions, return false now. */
10280 if (!val && !optimize)
10281 val = integer_zero_node;
10283 return val;
10286 case BUILT_IN_CLASSIFY_TYPE:
10287 return fold_builtin_classify_type (arg0);
10289 case BUILT_IN_STRLEN:
10290 return fold_builtin_strlen (loc, type, arg0);
10292 CASE_FLT_FN (BUILT_IN_FABS):
10293 return fold_builtin_fabs (loc, arg0, type);
10295 case BUILT_IN_ABS:
10296 case BUILT_IN_LABS:
10297 case BUILT_IN_LLABS:
10298 case BUILT_IN_IMAXABS:
10299 return fold_builtin_abs (loc, arg0, type);
10301 CASE_FLT_FN (BUILT_IN_CONJ):
10302 if (validate_arg (arg0, COMPLEX_TYPE)
10303 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10304 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10305 break;
10307 CASE_FLT_FN (BUILT_IN_CREAL):
10308 if (validate_arg (arg0, COMPLEX_TYPE)
10309 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10310 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10311 break;
10313 CASE_FLT_FN (BUILT_IN_CIMAG):
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, IMAGPART_EXPR, type, arg0));
10317 break;
10319 CASE_FLT_FN (BUILT_IN_CCOS):
10320 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10322 CASE_FLT_FN (BUILT_IN_CCOSH):
10323 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10325 CASE_FLT_FN (BUILT_IN_CPROJ):
10326 return fold_builtin_cproj(loc, arg0, type);
10328 CASE_FLT_FN (BUILT_IN_CSIN):
10329 if (validate_arg (arg0, COMPLEX_TYPE)
10330 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10331 return do_mpc_arg1 (arg0, type, mpc_sin);
10332 break;
10334 CASE_FLT_FN (BUILT_IN_CSINH):
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_sinh);
10338 break;
10340 CASE_FLT_FN (BUILT_IN_CTAN):
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_tan);
10344 break;
10346 CASE_FLT_FN (BUILT_IN_CTANH):
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_tanh);
10350 break;
10352 CASE_FLT_FN (BUILT_IN_CLOG):
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_log);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_CSQRT):
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_sqrt);
10362 break;
10364 CASE_FLT_FN (BUILT_IN_CASIN):
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_asin);
10368 break;
10370 CASE_FLT_FN (BUILT_IN_CACOS):
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_acos);
10374 break;
10376 CASE_FLT_FN (BUILT_IN_CATAN):
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_atan);
10380 break;
10382 CASE_FLT_FN (BUILT_IN_CASINH):
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_asinh);
10386 break;
10388 CASE_FLT_FN (BUILT_IN_CACOSH):
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_acosh);
10392 break;
10394 CASE_FLT_FN (BUILT_IN_CATANH):
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_atanh);
10398 break;
10400 CASE_FLT_FN (BUILT_IN_CABS):
10401 return fold_builtin_cabs (loc, arg0, type, fndecl);
10403 CASE_FLT_FN (BUILT_IN_CARG):
10404 return fold_builtin_carg (loc, arg0, type);
10406 CASE_FLT_FN (BUILT_IN_SQRT):
10407 return fold_builtin_sqrt (loc, arg0, type);
10409 CASE_FLT_FN (BUILT_IN_CBRT):
10410 return fold_builtin_cbrt (loc, arg0, type);
10412 CASE_FLT_FN (BUILT_IN_ASIN):
10413 if (validate_arg (arg0, REAL_TYPE))
10414 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10415 &dconstm1, &dconst1, true);
10416 break;
10418 CASE_FLT_FN (BUILT_IN_ACOS):
10419 if (validate_arg (arg0, REAL_TYPE))
10420 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10421 &dconstm1, &dconst1, true);
10422 break;
10424 CASE_FLT_FN (BUILT_IN_ATAN):
10425 if (validate_arg (arg0, REAL_TYPE))
10426 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10427 break;
10429 CASE_FLT_FN (BUILT_IN_ASINH):
10430 if (validate_arg (arg0, REAL_TYPE))
10431 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10432 break;
10434 CASE_FLT_FN (BUILT_IN_ACOSH):
10435 if (validate_arg (arg0, REAL_TYPE))
10436 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10437 &dconst1, NULL, true);
10438 break;
10440 CASE_FLT_FN (BUILT_IN_ATANH):
10441 if (validate_arg (arg0, REAL_TYPE))
10442 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10443 &dconstm1, &dconst1, false);
10444 break;
10446 CASE_FLT_FN (BUILT_IN_SIN):
10447 if (validate_arg (arg0, REAL_TYPE))
10448 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10449 break;
10451 CASE_FLT_FN (BUILT_IN_COS):
10452 return fold_builtin_cos (loc, arg0, type, fndecl);
10454 CASE_FLT_FN (BUILT_IN_TAN):
10455 return fold_builtin_tan (arg0, type);
10457 CASE_FLT_FN (BUILT_IN_CEXP):
10458 return fold_builtin_cexp (loc, arg0, type);
10460 CASE_FLT_FN (BUILT_IN_CEXPI):
10461 if (validate_arg (arg0, REAL_TYPE))
10462 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10463 break;
10465 CASE_FLT_FN (BUILT_IN_SINH):
10466 if (validate_arg (arg0, REAL_TYPE))
10467 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10468 break;
10470 CASE_FLT_FN (BUILT_IN_COSH):
10471 return fold_builtin_cosh (loc, arg0, type, fndecl);
10473 CASE_FLT_FN (BUILT_IN_TANH):
10474 if (validate_arg (arg0, REAL_TYPE))
10475 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10476 break;
10478 CASE_FLT_FN (BUILT_IN_ERF):
10479 if (validate_arg (arg0, REAL_TYPE))
10480 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10481 break;
10483 CASE_FLT_FN (BUILT_IN_ERFC):
10484 if (validate_arg (arg0, REAL_TYPE))
10485 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10486 break;
10488 CASE_FLT_FN (BUILT_IN_TGAMMA):
10489 if (validate_arg (arg0, REAL_TYPE))
10490 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10491 break;
10493 CASE_FLT_FN (BUILT_IN_EXP):
10494 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10496 CASE_FLT_FN (BUILT_IN_EXP2):
10497 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10499 CASE_FLT_FN (BUILT_IN_EXP10):
10500 CASE_FLT_FN (BUILT_IN_POW10):
10501 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10503 CASE_FLT_FN (BUILT_IN_EXPM1):
10504 if (validate_arg (arg0, REAL_TYPE))
10505 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10506 break;
10508 CASE_FLT_FN (BUILT_IN_LOG):
10509 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10511 CASE_FLT_FN (BUILT_IN_LOG2):
10512 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10514 CASE_FLT_FN (BUILT_IN_LOG10):
10515 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10517 CASE_FLT_FN (BUILT_IN_LOG1P):
10518 if (validate_arg (arg0, REAL_TYPE))
10519 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10520 &dconstm1, NULL, false);
10521 break;
10523 CASE_FLT_FN (BUILT_IN_J0):
10524 if (validate_arg (arg0, REAL_TYPE))
10525 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10526 NULL, NULL, 0);
10527 break;
10529 CASE_FLT_FN (BUILT_IN_J1):
10530 if (validate_arg (arg0, REAL_TYPE))
10531 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10532 NULL, NULL, 0);
10533 break;
10535 CASE_FLT_FN (BUILT_IN_Y0):
10536 if (validate_arg (arg0, REAL_TYPE))
10537 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10538 &dconst0, NULL, false);
10539 break;
10541 CASE_FLT_FN (BUILT_IN_Y1):
10542 if (validate_arg (arg0, REAL_TYPE))
10543 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10544 &dconst0, NULL, false);
10545 break;
10547 CASE_FLT_FN (BUILT_IN_NAN):
10548 case BUILT_IN_NAND32:
10549 case BUILT_IN_NAND64:
10550 case BUILT_IN_NAND128:
10551 return fold_builtin_nan (arg0, type, true);
10553 CASE_FLT_FN (BUILT_IN_NANS):
10554 return fold_builtin_nan (arg0, type, false);
10556 CASE_FLT_FN (BUILT_IN_FLOOR):
10557 return fold_builtin_floor (loc, fndecl, arg0);
10559 CASE_FLT_FN (BUILT_IN_CEIL):
10560 return fold_builtin_ceil (loc, fndecl, arg0);
10562 CASE_FLT_FN (BUILT_IN_TRUNC):
10563 return fold_builtin_trunc (loc, fndecl, arg0);
10565 CASE_FLT_FN (BUILT_IN_ROUND):
10566 return fold_builtin_round (loc, fndecl, arg0);
10568 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10569 CASE_FLT_FN (BUILT_IN_RINT):
10570 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10572 CASE_FLT_FN (BUILT_IN_ICEIL):
10573 CASE_FLT_FN (BUILT_IN_LCEIL):
10574 CASE_FLT_FN (BUILT_IN_LLCEIL):
10575 CASE_FLT_FN (BUILT_IN_LFLOOR):
10576 CASE_FLT_FN (BUILT_IN_IFLOOR):
10577 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10578 CASE_FLT_FN (BUILT_IN_IROUND):
10579 CASE_FLT_FN (BUILT_IN_LROUND):
10580 CASE_FLT_FN (BUILT_IN_LLROUND):
10581 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10583 CASE_FLT_FN (BUILT_IN_IRINT):
10584 CASE_FLT_FN (BUILT_IN_LRINT):
10585 CASE_FLT_FN (BUILT_IN_LLRINT):
10586 return fold_fixed_mathfn (loc, fndecl, arg0);
10588 case BUILT_IN_BSWAP16:
10589 case BUILT_IN_BSWAP32:
10590 case BUILT_IN_BSWAP64:
10591 return fold_builtin_bswap (fndecl, arg0);
10593 CASE_INT_FN (BUILT_IN_FFS):
10594 CASE_INT_FN (BUILT_IN_CLZ):
10595 CASE_INT_FN (BUILT_IN_CTZ):
10596 CASE_INT_FN (BUILT_IN_CLRSB):
10597 CASE_INT_FN (BUILT_IN_POPCOUNT):
10598 CASE_INT_FN (BUILT_IN_PARITY):
10599 return fold_builtin_bitop (fndecl, arg0);
10601 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10602 return fold_builtin_signbit (loc, arg0, type);
10604 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10605 return fold_builtin_significand (loc, arg0, type);
10607 CASE_FLT_FN (BUILT_IN_ILOGB):
10608 CASE_FLT_FN (BUILT_IN_LOGB):
10609 return fold_builtin_logb (loc, arg0, type);
10611 case BUILT_IN_ISASCII:
10612 return fold_builtin_isascii (loc, arg0);
10614 case BUILT_IN_TOASCII:
10615 return fold_builtin_toascii (loc, arg0);
10617 case BUILT_IN_ISDIGIT:
10618 return fold_builtin_isdigit (loc, arg0);
10620 CASE_FLT_FN (BUILT_IN_FINITE):
10621 case BUILT_IN_FINITED32:
10622 case BUILT_IN_FINITED64:
10623 case BUILT_IN_FINITED128:
10624 case BUILT_IN_ISFINITE:
10626 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10627 if (ret)
10628 return ret;
10629 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10632 CASE_FLT_FN (BUILT_IN_ISINF):
10633 case BUILT_IN_ISINFD32:
10634 case BUILT_IN_ISINFD64:
10635 case BUILT_IN_ISINFD128:
10637 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10638 if (ret)
10639 return ret;
10640 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10643 case BUILT_IN_ISNORMAL:
10644 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10646 case BUILT_IN_ISINF_SIGN:
10647 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10649 CASE_FLT_FN (BUILT_IN_ISNAN):
10650 case BUILT_IN_ISNAND32:
10651 case BUILT_IN_ISNAND64:
10652 case BUILT_IN_ISNAND128:
10653 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10655 case BUILT_IN_PRINTF:
10656 case BUILT_IN_PRINTF_UNLOCKED:
10657 case BUILT_IN_VPRINTF:
10658 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10660 case BUILT_IN_FREE:
10661 if (integer_zerop (arg0))
10662 return build_empty_stmt (loc);
10663 break;
10665 default:
10666 break;
10669 return NULL_TREE;
10673 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10674 IGNORE is true if the result of the function call is ignored. This
10675 function returns NULL_TREE if no simplification was possible. */
10677 static tree
10678 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10680 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10681 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10683 switch (fcode)
10685 CASE_FLT_FN (BUILT_IN_JN):
10686 if (validate_arg (arg0, INTEGER_TYPE)
10687 && validate_arg (arg1, REAL_TYPE))
10688 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10689 break;
10691 CASE_FLT_FN (BUILT_IN_YN):
10692 if (validate_arg (arg0, INTEGER_TYPE)
10693 && validate_arg (arg1, REAL_TYPE))
10694 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10695 &dconst0, false);
10696 break;
10698 CASE_FLT_FN (BUILT_IN_DREM):
10699 CASE_FLT_FN (BUILT_IN_REMAINDER):
10700 if (validate_arg (arg0, REAL_TYPE)
10701 && validate_arg(arg1, REAL_TYPE))
10702 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10703 break;
10705 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10706 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10707 if (validate_arg (arg0, REAL_TYPE)
10708 && validate_arg(arg1, POINTER_TYPE))
10709 return do_mpfr_lgamma_r (arg0, arg1, type);
10710 break;
10712 CASE_FLT_FN (BUILT_IN_ATAN2):
10713 if (validate_arg (arg0, REAL_TYPE)
10714 && validate_arg(arg1, REAL_TYPE))
10715 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10716 break;
10718 CASE_FLT_FN (BUILT_IN_FDIM):
10719 if (validate_arg (arg0, REAL_TYPE)
10720 && validate_arg(arg1, REAL_TYPE))
10721 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10722 break;
10724 CASE_FLT_FN (BUILT_IN_HYPOT):
10725 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10727 CASE_FLT_FN (BUILT_IN_CPOW):
10728 if (validate_arg (arg0, COMPLEX_TYPE)
10729 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10730 && validate_arg (arg1, COMPLEX_TYPE)
10731 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10732 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10733 break;
10735 CASE_FLT_FN (BUILT_IN_LDEXP):
10736 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10737 CASE_FLT_FN (BUILT_IN_SCALBN):
10738 CASE_FLT_FN (BUILT_IN_SCALBLN):
10739 return fold_builtin_load_exponent (loc, arg0, arg1,
10740 type, /*ldexp=*/false);
10742 CASE_FLT_FN (BUILT_IN_FREXP):
10743 return fold_builtin_frexp (loc, arg0, arg1, type);
10745 CASE_FLT_FN (BUILT_IN_MODF):
10746 return fold_builtin_modf (loc, arg0, arg1, type);
10748 case BUILT_IN_BZERO:
10749 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10751 case BUILT_IN_FPUTS:
10752 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10754 case BUILT_IN_FPUTS_UNLOCKED:
10755 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10757 case BUILT_IN_STRSTR:
10758 return fold_builtin_strstr (loc, arg0, arg1, type);
10760 case BUILT_IN_STRCAT:
10761 return fold_builtin_strcat (loc, arg0, arg1);
10763 case BUILT_IN_STRSPN:
10764 return fold_builtin_strspn (loc, arg0, arg1);
10766 case BUILT_IN_STRCSPN:
10767 return fold_builtin_strcspn (loc, arg0, arg1);
10769 case BUILT_IN_STRCHR:
10770 case BUILT_IN_INDEX:
10771 return fold_builtin_strchr (loc, arg0, arg1, type);
10773 case BUILT_IN_STRRCHR:
10774 case BUILT_IN_RINDEX:
10775 return fold_builtin_strrchr (loc, arg0, arg1, type);
10777 case BUILT_IN_STRCPY:
10778 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10780 case BUILT_IN_STPCPY:
10781 if (ignore)
10783 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10784 if (!fn)
10785 break;
10787 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10789 else
10790 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10791 break;
10793 case BUILT_IN_STRCMP:
10794 return fold_builtin_strcmp (loc, arg0, arg1);
10796 case BUILT_IN_STRPBRK:
10797 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10799 case BUILT_IN_EXPECT:
10800 return fold_builtin_expect (loc, arg0, arg1);
10802 CASE_FLT_FN (BUILT_IN_POW):
10803 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10805 CASE_FLT_FN (BUILT_IN_POWI):
10806 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10808 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10809 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10811 CASE_FLT_FN (BUILT_IN_FMIN):
10812 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10814 CASE_FLT_FN (BUILT_IN_FMAX):
10815 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10817 case BUILT_IN_ISGREATER:
10818 return fold_builtin_unordered_cmp (loc, fndecl,
10819 arg0, arg1, UNLE_EXPR, LE_EXPR);
10820 case BUILT_IN_ISGREATEREQUAL:
10821 return fold_builtin_unordered_cmp (loc, fndecl,
10822 arg0, arg1, UNLT_EXPR, LT_EXPR);
10823 case BUILT_IN_ISLESS:
10824 return fold_builtin_unordered_cmp (loc, fndecl,
10825 arg0, arg1, UNGE_EXPR, GE_EXPR);
10826 case BUILT_IN_ISLESSEQUAL:
10827 return fold_builtin_unordered_cmp (loc, fndecl,
10828 arg0, arg1, UNGT_EXPR, GT_EXPR);
10829 case BUILT_IN_ISLESSGREATER:
10830 return fold_builtin_unordered_cmp (loc, fndecl,
10831 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10832 case BUILT_IN_ISUNORDERED:
10833 return fold_builtin_unordered_cmp (loc, fndecl,
10834 arg0, arg1, UNORDERED_EXPR,
10835 NOP_EXPR);
10837 /* We do the folding for va_start in the expander. */
10838 case BUILT_IN_VA_START:
10839 break;
10841 case BUILT_IN_SPRINTF:
10842 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10844 case BUILT_IN_OBJECT_SIZE:
10845 return fold_builtin_object_size (arg0, arg1);
10847 case BUILT_IN_PRINTF:
10848 case BUILT_IN_PRINTF_UNLOCKED:
10849 case BUILT_IN_VPRINTF:
10850 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10852 case BUILT_IN_PRINTF_CHK:
10853 case BUILT_IN_VPRINTF_CHK:
10854 if (!validate_arg (arg0, INTEGER_TYPE)
10855 || TREE_SIDE_EFFECTS (arg0))
10856 return NULL_TREE;
10857 else
10858 return fold_builtin_printf (loc, fndecl,
10859 arg1, NULL_TREE, ignore, fcode);
10860 break;
10862 case BUILT_IN_FPRINTF:
10863 case BUILT_IN_FPRINTF_UNLOCKED:
10864 case BUILT_IN_VFPRINTF:
10865 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10866 ignore, fcode);
10868 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10869 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10871 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10872 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10874 default:
10875 break;
10877 return NULL_TREE;
10880 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10881 and ARG2. IGNORE is true if the result of the function call is ignored.
10882 This function returns NULL_TREE if no simplification was possible. */
10884 static tree
10885 fold_builtin_3 (location_t loc, tree fndecl,
10886 tree arg0, tree arg1, tree arg2, bool ignore)
10888 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10889 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10890 switch (fcode)
10893 CASE_FLT_FN (BUILT_IN_SINCOS):
10894 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10896 CASE_FLT_FN (BUILT_IN_FMA):
10897 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10898 break;
10900 CASE_FLT_FN (BUILT_IN_REMQUO):
10901 if (validate_arg (arg0, REAL_TYPE)
10902 && validate_arg(arg1, REAL_TYPE)
10903 && validate_arg(arg2, POINTER_TYPE))
10904 return do_mpfr_remquo (arg0, arg1, arg2);
10905 break;
10907 case BUILT_IN_MEMSET:
10908 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10910 case BUILT_IN_BCOPY:
10911 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10912 void_type_node, true, /*endp=*/3);
10914 case BUILT_IN_MEMCPY:
10915 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10916 type, ignore, /*endp=*/0);
10918 case BUILT_IN_MEMPCPY:
10919 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10920 type, ignore, /*endp=*/1);
10922 case BUILT_IN_MEMMOVE:
10923 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10924 type, ignore, /*endp=*/3);
10926 case BUILT_IN_STRNCAT:
10927 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10929 case BUILT_IN_STRNCPY:
10930 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10932 case BUILT_IN_STRNCMP:
10933 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10935 case BUILT_IN_MEMCHR:
10936 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10938 case BUILT_IN_BCMP:
10939 case BUILT_IN_MEMCMP:
10940 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10942 case BUILT_IN_SPRINTF:
10943 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10945 case BUILT_IN_SNPRINTF:
10946 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10948 case BUILT_IN_STRCPY_CHK:
10949 case BUILT_IN_STPCPY_CHK:
10950 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10951 ignore, fcode);
10953 case BUILT_IN_STRCAT_CHK:
10954 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10956 case BUILT_IN_PRINTF_CHK:
10957 case BUILT_IN_VPRINTF_CHK:
10958 if (!validate_arg (arg0, INTEGER_TYPE)
10959 || TREE_SIDE_EFFECTS (arg0))
10960 return NULL_TREE;
10961 else
10962 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10963 break;
10965 case BUILT_IN_FPRINTF:
10966 case BUILT_IN_FPRINTF_UNLOCKED:
10967 case BUILT_IN_VFPRINTF:
10968 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10969 ignore, fcode);
10971 case BUILT_IN_FPRINTF_CHK:
10972 case BUILT_IN_VFPRINTF_CHK:
10973 if (!validate_arg (arg1, INTEGER_TYPE)
10974 || TREE_SIDE_EFFECTS (arg1))
10975 return NULL_TREE;
10976 else
10977 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10978 ignore, fcode);
10980 default:
10981 break;
10983 return NULL_TREE;
10986 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10987 ARG2, and ARG3. IGNORE is true if the result of the function call is
10988 ignored. This function returns NULL_TREE if no simplification was
10989 possible. */
10991 static tree
10992 fold_builtin_4 (location_t loc, tree fndecl,
10993 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10995 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10997 switch (fcode)
10999 case BUILT_IN_MEMCPY_CHK:
11000 case BUILT_IN_MEMPCPY_CHK:
11001 case BUILT_IN_MEMMOVE_CHK:
11002 case BUILT_IN_MEMSET_CHK:
11003 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11004 NULL_TREE, ignore,
11005 DECL_FUNCTION_CODE (fndecl));
11007 case BUILT_IN_STRNCPY_CHK:
11008 case BUILT_IN_STPNCPY_CHK:
11009 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11010 ignore, fcode);
11012 case BUILT_IN_STRNCAT_CHK:
11013 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11015 case BUILT_IN_SNPRINTF:
11016 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11018 case BUILT_IN_FPRINTF_CHK:
11019 case BUILT_IN_VFPRINTF_CHK:
11020 if (!validate_arg (arg1, INTEGER_TYPE)
11021 || TREE_SIDE_EFFECTS (arg1))
11022 return NULL_TREE;
11023 else
11024 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11025 ignore, fcode);
11026 break;
11028 default:
11029 break;
11031 return NULL_TREE;
11034 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11035 arguments, where NARGS <= 4. IGNORE is true if the result of the
11036 function call is ignored. This function returns NULL_TREE if no
11037 simplification was possible. Note that this only folds builtins with
11038 fixed argument patterns. Foldings that do varargs-to-varargs
11039 transformations, or that match calls with more than 4 arguments,
11040 need to be handled with fold_builtin_varargs instead. */
11042 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11044 static tree
11045 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11047 tree ret = NULL_TREE;
11049 switch (nargs)
11051 case 0:
11052 ret = fold_builtin_0 (loc, fndecl, ignore);
11053 break;
11054 case 1:
11055 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11056 break;
11057 case 2:
11058 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11059 break;
11060 case 3:
11061 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11062 break;
11063 case 4:
11064 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11065 ignore);
11066 break;
11067 default:
11068 break;
11070 if (ret)
11072 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11073 SET_EXPR_LOCATION (ret, loc);
11074 TREE_NO_WARNING (ret) = 1;
11075 return ret;
11077 return NULL_TREE;
11080 /* Builtins with folding operations that operate on "..." arguments
11081 need special handling; we need to store the arguments in a convenient
11082 data structure before attempting any folding. Fortunately there are
11083 only a few builtins that fall into this category. FNDECL is the
11084 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11085 result of the function call is ignored. */
11087 static tree
11088 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11089 bool ignore ATTRIBUTE_UNUSED)
11091 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11092 tree ret = NULL_TREE;
11094 switch (fcode)
11096 case BUILT_IN_SPRINTF_CHK:
11097 case BUILT_IN_VSPRINTF_CHK:
11098 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11099 break;
11101 case BUILT_IN_SNPRINTF_CHK:
11102 case BUILT_IN_VSNPRINTF_CHK:
11103 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11104 break;
11106 case BUILT_IN_FPCLASSIFY:
11107 ret = fold_builtin_fpclassify (loc, exp);
11108 break;
11110 default:
11111 break;
11113 if (ret)
11115 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11116 SET_EXPR_LOCATION (ret, loc);
11117 TREE_NO_WARNING (ret) = 1;
11118 return ret;
11120 return NULL_TREE;
11123 /* Return true if FNDECL shouldn't be folded right now.
11124 If a built-in function has an inline attribute always_inline
11125 wrapper, defer folding it after always_inline functions have
11126 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11127 might not be performed. */
11129 bool
11130 avoid_folding_inline_builtin (tree fndecl)
11132 return (DECL_DECLARED_INLINE_P (fndecl)
11133 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11134 && cfun
11135 && !cfun->always_inline_functions_inlined
11136 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11139 /* A wrapper function for builtin folding that prevents warnings for
11140 "statement without effect" and the like, caused by removing the
11141 call node earlier than the warning is generated. */
11143 tree
11144 fold_call_expr (location_t loc, tree exp, bool ignore)
11146 tree ret = NULL_TREE;
11147 tree fndecl = get_callee_fndecl (exp);
11148 if (fndecl
11149 && TREE_CODE (fndecl) == FUNCTION_DECL
11150 && DECL_BUILT_IN (fndecl)
11151 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11152 yet. Defer folding until we see all the arguments
11153 (after inlining). */
11154 && !CALL_EXPR_VA_ARG_PACK (exp))
11156 int nargs = call_expr_nargs (exp);
11158 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11159 instead last argument is __builtin_va_arg_pack (). Defer folding
11160 even in that case, until arguments are finalized. */
11161 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11163 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11164 if (fndecl2
11165 && TREE_CODE (fndecl2) == FUNCTION_DECL
11166 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11167 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11168 return NULL_TREE;
11171 if (avoid_folding_inline_builtin (fndecl))
11172 return NULL_TREE;
11174 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11175 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11176 CALL_EXPR_ARGP (exp), ignore);
11177 else
11179 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11181 tree *args = CALL_EXPR_ARGP (exp);
11182 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11184 if (!ret)
11185 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11186 if (ret)
11187 return ret;
11190 return NULL_TREE;
11193 /* Conveniently construct a function call expression. FNDECL names the
11194 function to be called and N arguments are passed in the array
11195 ARGARRAY. */
11197 tree
11198 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11200 tree fntype = TREE_TYPE (fndecl);
11201 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11203 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11206 /* Conveniently construct a function call expression. FNDECL names the
11207 function to be called and the arguments are passed in the vector
11208 VEC. */
11210 tree
11211 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11213 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11214 vec_safe_address (vec));
11218 /* Conveniently construct a function call expression. FNDECL names the
11219 function to be called, N is the number of arguments, and the "..."
11220 parameters are the argument expressions. */
11222 tree
11223 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11225 va_list ap;
11226 tree *argarray = XALLOCAVEC (tree, n);
11227 int i;
11229 va_start (ap, n);
11230 for (i = 0; i < n; i++)
11231 argarray[i] = va_arg (ap, tree);
11232 va_end (ap);
11233 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11236 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11237 varargs macros aren't supported by all bootstrap compilers. */
11239 tree
11240 build_call_expr (tree fndecl, int n, ...)
11242 va_list ap;
11243 tree *argarray = XALLOCAVEC (tree, n);
11244 int i;
11246 va_start (ap, n);
11247 for (i = 0; i < n; i++)
11248 argarray[i] = va_arg (ap, tree);
11249 va_end (ap);
11250 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11253 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11254 N arguments are passed in the array ARGARRAY. */
11256 tree
11257 fold_builtin_call_array (location_t loc, tree type,
11258 tree fn,
11259 int n,
11260 tree *argarray)
11262 tree ret = NULL_TREE;
11263 tree exp;
11265 if (TREE_CODE (fn) == ADDR_EXPR)
11267 tree fndecl = TREE_OPERAND (fn, 0);
11268 if (TREE_CODE (fndecl) == FUNCTION_DECL
11269 && DECL_BUILT_IN (fndecl))
11271 /* If last argument is __builtin_va_arg_pack (), arguments to this
11272 function are not finalized yet. Defer folding until they are. */
11273 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11275 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11276 if (fndecl2
11277 && TREE_CODE (fndecl2) == FUNCTION_DECL
11278 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11279 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11280 return build_call_array_loc (loc, type, fn, n, argarray);
11282 if (avoid_folding_inline_builtin (fndecl))
11283 return build_call_array_loc (loc, type, fn, n, argarray);
11284 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11286 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11287 if (ret)
11288 return ret;
11290 return build_call_array_loc (loc, type, fn, n, argarray);
11292 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11294 /* First try the transformations that don't require consing up
11295 an exp. */
11296 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11297 if (ret)
11298 return ret;
11301 /* If we got this far, we need to build an exp. */
11302 exp = build_call_array_loc (loc, type, fn, n, argarray);
11303 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11304 return ret ? ret : exp;
11308 return build_call_array_loc (loc, type, fn, n, argarray);
11311 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11312 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11313 of arguments in ARGS to be omitted. OLDNARGS is the number of
11314 elements in ARGS. */
11316 static tree
11317 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11318 int skip, tree fndecl, int n, va_list newargs)
11320 int nargs = oldnargs - skip + n;
11321 tree *buffer;
11323 if (n > 0)
11325 int i, j;
11327 buffer = XALLOCAVEC (tree, nargs);
11328 for (i = 0; i < n; i++)
11329 buffer[i] = va_arg (newargs, tree);
11330 for (j = skip; j < oldnargs; j++, i++)
11331 buffer[i] = args[j];
11333 else
11334 buffer = args + skip;
11336 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11339 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11340 list ARGS along with N new arguments specified as the "..."
11341 parameters. SKIP is the number of arguments in ARGS to be omitted.
11342 OLDNARGS is the number of elements in ARGS. */
11344 static tree
11345 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11346 int skip, tree fndecl, int n, ...)
11348 va_list ap;
11349 tree t;
11351 va_start (ap, n);
11352 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11353 va_end (ap);
11355 return t;
11358 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11359 along with N new arguments specified as the "..." parameters. SKIP
11360 is the number of arguments in EXP to be omitted. This function is used
11361 to do varargs-to-varargs transformations. */
11363 static tree
11364 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11366 va_list ap;
11367 tree t;
11369 va_start (ap, n);
11370 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11371 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11372 va_end (ap);
11374 return t;
11377 /* Validate a single argument ARG against a tree code CODE representing
11378 a type. */
11380 static bool
11381 validate_arg (const_tree arg, enum tree_code code)
11383 if (!arg)
11384 return false;
11385 else if (code == POINTER_TYPE)
11386 return POINTER_TYPE_P (TREE_TYPE (arg));
11387 else if (code == INTEGER_TYPE)
11388 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11389 return code == TREE_CODE (TREE_TYPE (arg));
11392 /* This function validates the types of a function call argument list
11393 against a specified list of tree_codes. If the last specifier is a 0,
11394 that represents an ellipses, otherwise the last specifier must be a
11395 VOID_TYPE.
11397 This is the GIMPLE version of validate_arglist. Eventually we want to
11398 completely convert builtins.c to work from GIMPLEs and the tree based
11399 validate_arglist will then be removed. */
11401 bool
11402 validate_gimple_arglist (const_gimple call, ...)
11404 enum tree_code code;
11405 bool res = 0;
11406 va_list ap;
11407 const_tree arg;
11408 size_t i;
11410 va_start (ap, call);
11411 i = 0;
11415 code = (enum tree_code) va_arg (ap, int);
11416 switch (code)
11418 case 0:
11419 /* This signifies an ellipses, any further arguments are all ok. */
11420 res = true;
11421 goto end;
11422 case VOID_TYPE:
11423 /* This signifies an endlink, if no arguments remain, return
11424 true, otherwise return false. */
11425 res = (i == gimple_call_num_args (call));
11426 goto end;
11427 default:
11428 /* If no parameters remain or the parameter's code does not
11429 match the specified code, return false. Otherwise continue
11430 checking any remaining arguments. */
11431 arg = gimple_call_arg (call, i++);
11432 if (!validate_arg (arg, code))
11433 goto end;
11434 break;
11437 while (1);
11439 /* We need gotos here since we can only have one VA_CLOSE in a
11440 function. */
11441 end: ;
11442 va_end (ap);
11444 return res;
11447 /* This function validates the types of a function call argument list
11448 against a specified list of tree_codes. If the last specifier is a 0,
11449 that represents an ellipses, otherwise the last specifier must be a
11450 VOID_TYPE. */
11452 bool
11453 validate_arglist (const_tree callexpr, ...)
11455 enum tree_code code;
11456 bool res = 0;
11457 va_list ap;
11458 const_call_expr_arg_iterator iter;
11459 const_tree arg;
11461 va_start (ap, callexpr);
11462 init_const_call_expr_arg_iterator (callexpr, &iter);
11466 code = (enum tree_code) va_arg (ap, int);
11467 switch (code)
11469 case 0:
11470 /* This signifies an ellipses, any further arguments are all ok. */
11471 res = true;
11472 goto end;
11473 case VOID_TYPE:
11474 /* This signifies an endlink, if no arguments remain, return
11475 true, otherwise return false. */
11476 res = !more_const_call_expr_args_p (&iter);
11477 goto end;
11478 default:
11479 /* If no parameters remain or the parameter's code does not
11480 match the specified code, return false. Otherwise continue
11481 checking any remaining arguments. */
11482 arg = next_const_call_expr_arg (&iter);
11483 if (!validate_arg (arg, code))
11484 goto end;
11485 break;
11488 while (1);
11490 /* We need gotos here since we can only have one VA_CLOSE in a
11491 function. */
11492 end: ;
11493 va_end (ap);
11495 return res;
11498 /* Default target-specific builtin expander that does nothing. */
11501 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11502 rtx target ATTRIBUTE_UNUSED,
11503 rtx subtarget ATTRIBUTE_UNUSED,
11504 enum machine_mode mode ATTRIBUTE_UNUSED,
11505 int ignore ATTRIBUTE_UNUSED)
11507 return NULL_RTX;
11510 /* Returns true is EXP represents data that would potentially reside
11511 in a readonly section. */
11513 static bool
11514 readonly_data_expr (tree exp)
11516 STRIP_NOPS (exp);
11518 if (TREE_CODE (exp) != ADDR_EXPR)
11519 return false;
11521 exp = get_base_address (TREE_OPERAND (exp, 0));
11522 if (!exp)
11523 return false;
11525 /* Make sure we call decl_readonly_section only for trees it
11526 can handle (since it returns true for everything it doesn't
11527 understand). */
11528 if (TREE_CODE (exp) == STRING_CST
11529 || TREE_CODE (exp) == CONSTRUCTOR
11530 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11531 return decl_readonly_section (exp, 0);
11532 else
11533 return false;
11536 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11537 to the call, and TYPE is its return type.
11539 Return NULL_TREE if no simplification was possible, otherwise return the
11540 simplified form of the call as a tree.
11542 The simplified form may be a constant or other expression which
11543 computes the same value, but in a more efficient manner (including
11544 calls to other builtin functions).
11546 The call may contain arguments which need to be evaluated, but
11547 which are not useful to determine the result of the call. In
11548 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11549 COMPOUND_EXPR will be an argument which must be evaluated.
11550 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11551 COMPOUND_EXPR in the chain will contain the tree for the simplified
11552 form of the builtin function call. */
11554 static tree
11555 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11557 if (!validate_arg (s1, POINTER_TYPE)
11558 || !validate_arg (s2, POINTER_TYPE))
11559 return NULL_TREE;
11560 else
11562 tree fn;
11563 const char *p1, *p2;
11565 p2 = c_getstr (s2);
11566 if (p2 == NULL)
11567 return NULL_TREE;
11569 p1 = c_getstr (s1);
11570 if (p1 != NULL)
11572 const char *r = strstr (p1, p2);
11573 tree tem;
11575 if (r == NULL)
11576 return build_int_cst (TREE_TYPE (s1), 0);
11578 /* Return an offset into the constant string argument. */
11579 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11580 return fold_convert_loc (loc, type, tem);
11583 /* The argument is const char *, and the result is char *, so we need
11584 a type conversion here to avoid a warning. */
11585 if (p2[0] == '\0')
11586 return fold_convert_loc (loc, type, s1);
11588 if (p2[1] != '\0')
11589 return NULL_TREE;
11591 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11592 if (!fn)
11593 return NULL_TREE;
11595 /* New argument list transforming strstr(s1, s2) to
11596 strchr(s1, s2[0]). */
11597 return build_call_expr_loc (loc, fn, 2, s1,
11598 build_int_cst (integer_type_node, p2[0]));
11602 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11603 the call, and TYPE is its return type.
11605 Return NULL_TREE if no simplification was possible, otherwise return the
11606 simplified form of the call as a tree.
11608 The simplified form may be a constant or other expression which
11609 computes the same value, but in a more efficient manner (including
11610 calls to other builtin functions).
11612 The call may contain arguments which need to be evaluated, but
11613 which are not useful to determine the result of the call. In
11614 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11615 COMPOUND_EXPR will be an argument which must be evaluated.
11616 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11617 COMPOUND_EXPR in the chain will contain the tree for the simplified
11618 form of the builtin function call. */
11620 static tree
11621 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11623 if (!validate_arg (s1, POINTER_TYPE)
11624 || !validate_arg (s2, INTEGER_TYPE))
11625 return NULL_TREE;
11626 else
11628 const char *p1;
11630 if (TREE_CODE (s2) != INTEGER_CST)
11631 return NULL_TREE;
11633 p1 = c_getstr (s1);
11634 if (p1 != NULL)
11636 char c;
11637 const char *r;
11638 tree tem;
11640 if (target_char_cast (s2, &c))
11641 return NULL_TREE;
11643 r = strchr (p1, c);
11645 if (r == NULL)
11646 return build_int_cst (TREE_TYPE (s1), 0);
11648 /* Return an offset into the constant string argument. */
11649 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11650 return fold_convert_loc (loc, type, tem);
11652 return NULL_TREE;
11656 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11657 the call, and TYPE is its return type.
11659 Return NULL_TREE if no simplification was possible, otherwise return the
11660 simplified form of the call as a tree.
11662 The simplified form may be a constant or other expression which
11663 computes the same value, but in a more efficient manner (including
11664 calls to other builtin functions).
11666 The call may contain arguments which need to be evaluated, but
11667 which are not useful to determine the result of the call. In
11668 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11669 COMPOUND_EXPR will be an argument which must be evaluated.
11670 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11671 COMPOUND_EXPR in the chain will contain the tree for the simplified
11672 form of the builtin function call. */
11674 static tree
11675 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11677 if (!validate_arg (s1, POINTER_TYPE)
11678 || !validate_arg (s2, INTEGER_TYPE))
11679 return NULL_TREE;
11680 else
11682 tree fn;
11683 const char *p1;
11685 if (TREE_CODE (s2) != INTEGER_CST)
11686 return NULL_TREE;
11688 p1 = c_getstr (s1);
11689 if (p1 != NULL)
11691 char c;
11692 const char *r;
11693 tree tem;
11695 if (target_char_cast (s2, &c))
11696 return NULL_TREE;
11698 r = strrchr (p1, c);
11700 if (r == NULL)
11701 return build_int_cst (TREE_TYPE (s1), 0);
11703 /* Return an offset into the constant string argument. */
11704 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11705 return fold_convert_loc (loc, type, tem);
11708 if (! integer_zerop (s2))
11709 return NULL_TREE;
11711 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11712 if (!fn)
11713 return NULL_TREE;
11715 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11716 return build_call_expr_loc (loc, fn, 2, s1, s2);
11720 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11721 to the call, and TYPE is its return type.
11723 Return NULL_TREE if no simplification was possible, otherwise return the
11724 simplified form of the call as a tree.
11726 The simplified form may be a constant or other expression which
11727 computes the same value, but in a more efficient manner (including
11728 calls to other builtin functions).
11730 The call may contain arguments which need to be evaluated, but
11731 which are not useful to determine the result of the call. In
11732 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11733 COMPOUND_EXPR will be an argument which must be evaluated.
11734 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11735 COMPOUND_EXPR in the chain will contain the tree for the simplified
11736 form of the builtin function call. */
11738 static tree
11739 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11741 if (!validate_arg (s1, POINTER_TYPE)
11742 || !validate_arg (s2, POINTER_TYPE))
11743 return NULL_TREE;
11744 else
11746 tree fn;
11747 const char *p1, *p2;
11749 p2 = c_getstr (s2);
11750 if (p2 == NULL)
11751 return NULL_TREE;
11753 p1 = c_getstr (s1);
11754 if (p1 != NULL)
11756 const char *r = strpbrk (p1, p2);
11757 tree tem;
11759 if (r == NULL)
11760 return build_int_cst (TREE_TYPE (s1), 0);
11762 /* Return an offset into the constant string argument. */
11763 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11764 return fold_convert_loc (loc, type, tem);
11767 if (p2[0] == '\0')
11768 /* strpbrk(x, "") == NULL.
11769 Evaluate and ignore s1 in case it had side-effects. */
11770 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11772 if (p2[1] != '\0')
11773 return NULL_TREE; /* Really call strpbrk. */
11775 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11776 if (!fn)
11777 return NULL_TREE;
11779 /* New argument list transforming strpbrk(s1, s2) to
11780 strchr(s1, s2[0]). */
11781 return build_call_expr_loc (loc, fn, 2, s1,
11782 build_int_cst (integer_type_node, p2[0]));
11786 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11787 to the call.
11789 Return NULL_TREE if no simplification was possible, otherwise return the
11790 simplified form of the call as a tree.
11792 The simplified form may be a constant or other expression which
11793 computes the same value, but in a more efficient manner (including
11794 calls to other builtin functions).
11796 The call may contain arguments which need to be evaluated, but
11797 which are not useful to determine the result of the call. In
11798 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11799 COMPOUND_EXPR will be an argument which must be evaluated.
11800 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11801 COMPOUND_EXPR in the chain will contain the tree for the simplified
11802 form of the builtin function call. */
11804 static tree
11805 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11807 if (!validate_arg (dst, POINTER_TYPE)
11808 || !validate_arg (src, POINTER_TYPE))
11809 return NULL_TREE;
11810 else
11812 const char *p = c_getstr (src);
11814 /* If the string length is zero, return the dst parameter. */
11815 if (p && *p == '\0')
11816 return dst;
11818 if (optimize_insn_for_speed_p ())
11820 /* See if we can store by pieces into (dst + strlen(dst)). */
11821 tree newdst, call;
11822 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11823 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11825 if (!strlen_fn || !strcpy_fn)
11826 return NULL_TREE;
11828 /* If we don't have a movstr we don't want to emit an strcpy
11829 call. We have to do that if the length of the source string
11830 isn't computable (in that case we can use memcpy probably
11831 later expanding to a sequence of mov instructions). If we
11832 have movstr instructions we can emit strcpy calls. */
11833 if (!HAVE_movstr)
11835 tree len = c_strlen (src, 1);
11836 if (! len || TREE_SIDE_EFFECTS (len))
11837 return NULL_TREE;
11840 /* Stabilize the argument list. */
11841 dst = builtin_save_expr (dst);
11843 /* Create strlen (dst). */
11844 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11845 /* Create (dst p+ strlen (dst)). */
11847 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11848 newdst = builtin_save_expr (newdst);
11850 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11851 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11853 return NULL_TREE;
11857 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11858 arguments to the call.
11860 Return NULL_TREE if no simplification was possible, otherwise return the
11861 simplified form of the call as a tree.
11863 The simplified form may be a constant or other expression which
11864 computes the same value, but in a more efficient manner (including
11865 calls to other builtin functions).
11867 The call may contain arguments which need to be evaluated, but
11868 which are not useful to determine the result of the call. In
11869 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11870 COMPOUND_EXPR will be an argument which must be evaluated.
11871 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11872 COMPOUND_EXPR in the chain will contain the tree for the simplified
11873 form of the builtin function call. */
11875 static tree
11876 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11878 if (!validate_arg (dst, POINTER_TYPE)
11879 || !validate_arg (src, POINTER_TYPE)
11880 || !validate_arg (len, INTEGER_TYPE))
11881 return NULL_TREE;
11882 else
11884 const char *p = c_getstr (src);
11886 /* If the requested length is zero, or the src parameter string
11887 length is zero, return the dst parameter. */
11888 if (integer_zerop (len) || (p && *p == '\0'))
11889 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11891 /* If the requested len is greater than or equal to the string
11892 length, call strcat. */
11893 if (TREE_CODE (len) == INTEGER_CST && p
11894 && compare_tree_int (len, strlen (p)) >= 0)
11896 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11898 /* If the replacement _DECL isn't initialized, don't do the
11899 transformation. */
11900 if (!fn)
11901 return NULL_TREE;
11903 return build_call_expr_loc (loc, fn, 2, dst, src);
11905 return NULL_TREE;
11909 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11910 to the call.
11912 Return NULL_TREE if no simplification was possible, otherwise return the
11913 simplified form of the call as a tree.
11915 The simplified form may be a constant or other expression which
11916 computes the same value, but in a more efficient manner (including
11917 calls to other builtin functions).
11919 The call may contain arguments which need to be evaluated, but
11920 which are not useful to determine the result of the call. In
11921 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11922 COMPOUND_EXPR will be an argument which must be evaluated.
11923 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11924 COMPOUND_EXPR in the chain will contain the tree for the simplified
11925 form of the builtin function call. */
11927 static tree
11928 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11930 if (!validate_arg (s1, POINTER_TYPE)
11931 || !validate_arg (s2, POINTER_TYPE))
11932 return NULL_TREE;
11933 else
11935 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11937 /* If both arguments are constants, evaluate at compile-time. */
11938 if (p1 && p2)
11940 const size_t r = strspn (p1, p2);
11941 return build_int_cst (size_type_node, r);
11944 /* If either argument is "", return NULL_TREE. */
11945 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11946 /* Evaluate and ignore both arguments in case either one has
11947 side-effects. */
11948 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11949 s1, s2);
11950 return NULL_TREE;
11954 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11955 to the call.
11957 Return NULL_TREE if no simplification was possible, otherwise return the
11958 simplified form of the call as a tree.
11960 The simplified form may be a constant or other expression which
11961 computes the same value, but in a more efficient manner (including
11962 calls to other builtin functions).
11964 The call may contain arguments which need to be evaluated, but
11965 which are not useful to determine the result of the call. In
11966 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11967 COMPOUND_EXPR will be an argument which must be evaluated.
11968 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11969 COMPOUND_EXPR in the chain will contain the tree for the simplified
11970 form of the builtin function call. */
11972 static tree
11973 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11975 if (!validate_arg (s1, POINTER_TYPE)
11976 || !validate_arg (s2, POINTER_TYPE))
11977 return NULL_TREE;
11978 else
11980 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11982 /* If both arguments are constants, evaluate at compile-time. */
11983 if (p1 && p2)
11985 const size_t r = strcspn (p1, p2);
11986 return build_int_cst (size_type_node, r);
11989 /* If the first argument is "", return NULL_TREE. */
11990 if (p1 && *p1 == '\0')
11992 /* Evaluate and ignore argument s2 in case it has
11993 side-effects. */
11994 return omit_one_operand_loc (loc, size_type_node,
11995 size_zero_node, s2);
11998 /* If the second argument is "", return __builtin_strlen(s1). */
11999 if (p2 && *p2 == '\0')
12001 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
12003 /* If the replacement _DECL isn't initialized, don't do the
12004 transformation. */
12005 if (!fn)
12006 return NULL_TREE;
12008 return build_call_expr_loc (loc, fn, 1, s1);
12010 return NULL_TREE;
12014 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
12015 to the call. IGNORE is true if the value returned
12016 by the builtin will be ignored. UNLOCKED is true is true if this
12017 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
12018 the known length of the string. Return NULL_TREE if no simplification
12019 was possible. */
12021 tree
12022 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12023 bool ignore, bool unlocked, tree len)
12025 /* If we're using an unlocked function, assume the other unlocked
12026 functions exist explicitly. */
12027 tree const fn_fputc = (unlocked
12028 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12029 : builtin_decl_implicit (BUILT_IN_FPUTC));
12030 tree const fn_fwrite = (unlocked
12031 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12032 : builtin_decl_implicit (BUILT_IN_FWRITE));
12034 /* If the return value is used, don't do the transformation. */
12035 if (!ignore)
12036 return NULL_TREE;
12038 /* Verify the arguments in the original call. */
12039 if (!validate_arg (arg0, POINTER_TYPE)
12040 || !validate_arg (arg1, POINTER_TYPE))
12041 return NULL_TREE;
12043 if (! len)
12044 len = c_strlen (arg0, 0);
12046 /* Get the length of the string passed to fputs. If the length
12047 can't be determined, punt. */
12048 if (!len
12049 || TREE_CODE (len) != INTEGER_CST)
12050 return NULL_TREE;
12052 switch (compare_tree_int (len, 1))
12054 case -1: /* length is 0, delete the call entirely . */
12055 return omit_one_operand_loc (loc, integer_type_node,
12056 integer_zero_node, arg1);;
12058 case 0: /* length is 1, call fputc. */
12060 const char *p = c_getstr (arg0);
12062 if (p != NULL)
12064 if (fn_fputc)
12065 return build_call_expr_loc (loc, fn_fputc, 2,
12066 build_int_cst
12067 (integer_type_node, p[0]), arg1);
12068 else
12069 return NULL_TREE;
12072 /* FALLTHROUGH */
12073 case 1: /* length is greater than 1, call fwrite. */
12075 /* If optimizing for size keep fputs. */
12076 if (optimize_function_for_size_p (cfun))
12077 return NULL_TREE;
12078 /* New argument list transforming fputs(string, stream) to
12079 fwrite(string, 1, len, stream). */
12080 if (fn_fwrite)
12081 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12082 size_one_node, len, arg1);
12083 else
12084 return NULL_TREE;
12086 default:
12087 gcc_unreachable ();
12089 return NULL_TREE;
12092 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12093 produced. False otherwise. This is done so that we don't output the error
12094 or warning twice or three times. */
12096 bool
12097 fold_builtin_next_arg (tree exp, bool va_start_p)
12099 tree fntype = TREE_TYPE (current_function_decl);
12100 int nargs = call_expr_nargs (exp);
12101 tree arg;
12102 /* There is good chance the current input_location points inside the
12103 definition of the va_start macro (perhaps on the token for
12104 builtin) in a system header, so warnings will not be emitted.
12105 Use the location in real source code. */
12106 source_location current_location =
12107 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12108 NULL);
12110 if (!stdarg_p (fntype))
12112 error ("%<va_start%> used in function with fixed args");
12113 return true;
12116 if (va_start_p)
12118 if (va_start_p && (nargs != 2))
12120 error ("wrong number of arguments to function %<va_start%>");
12121 return true;
12123 arg = CALL_EXPR_ARG (exp, 1);
12125 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12126 when we checked the arguments and if needed issued a warning. */
12127 else
12129 if (nargs == 0)
12131 /* Evidently an out of date version of <stdarg.h>; can't validate
12132 va_start's second argument, but can still work as intended. */
12133 warning_at (current_location,
12134 OPT_Wvarargs,
12135 "%<__builtin_next_arg%> called without an argument");
12136 return true;
12138 else if (nargs > 1)
12140 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12141 return true;
12143 arg = CALL_EXPR_ARG (exp, 0);
12146 if (TREE_CODE (arg) == SSA_NAME)
12147 arg = SSA_NAME_VAR (arg);
12149 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12150 or __builtin_next_arg (0) the first time we see it, after checking
12151 the arguments and if needed issuing a warning. */
12152 if (!integer_zerop (arg))
12154 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12156 /* Strip off all nops for the sake of the comparison. This
12157 is not quite the same as STRIP_NOPS. It does more.
12158 We must also strip off INDIRECT_EXPR for C++ reference
12159 parameters. */
12160 while (CONVERT_EXPR_P (arg)
12161 || TREE_CODE (arg) == INDIRECT_REF)
12162 arg = TREE_OPERAND (arg, 0);
12163 if (arg != last_parm)
12165 /* FIXME: Sometimes with the tree optimizers we can get the
12166 not the last argument even though the user used the last
12167 argument. We just warn and set the arg to be the last
12168 argument so that we will get wrong-code because of
12169 it. */
12170 warning_at (current_location,
12171 OPT_Wvarargs,
12172 "second parameter of %<va_start%> not last named argument");
12175 /* Undefined by C99 7.15.1.4p4 (va_start):
12176 "If the parameter parmN is declared with the register storage
12177 class, with a function or array type, or with a type that is
12178 not compatible with the type that results after application of
12179 the default argument promotions, the behavior is undefined."
12181 else if (DECL_REGISTER (arg))
12183 warning_at (current_location,
12184 OPT_Wvarargs,
12185 "undefined behaviour when second parameter of "
12186 "%<va_start%> is declared with %<register%> storage");
12189 /* We want to verify the second parameter just once before the tree
12190 optimizers are run and then avoid keeping it in the tree,
12191 as otherwise we could warn even for correct code like:
12192 void foo (int i, ...)
12193 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12194 if (va_start_p)
12195 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12196 else
12197 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12199 return false;
12203 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12204 ORIG may be null if this is a 2-argument call. We don't attempt to
12205 simplify calls with more than 3 arguments.
12207 Return NULL_TREE if no simplification was possible, otherwise return the
12208 simplified form of the call as a tree. If IGNORED is true, it means that
12209 the caller does not use the returned value of the function. */
12211 static tree
12212 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12213 tree orig, int ignored)
12215 tree call, retval;
12216 const char *fmt_str = NULL;
12218 /* Verify the required arguments in the original call. We deal with two
12219 types of sprintf() calls: 'sprintf (str, fmt)' and
12220 'sprintf (dest, "%s", orig)'. */
12221 if (!validate_arg (dest, POINTER_TYPE)
12222 || !validate_arg (fmt, POINTER_TYPE))
12223 return NULL_TREE;
12224 if (orig && !validate_arg (orig, POINTER_TYPE))
12225 return NULL_TREE;
12227 /* Check whether the format is a literal string constant. */
12228 fmt_str = c_getstr (fmt);
12229 if (fmt_str == NULL)
12230 return NULL_TREE;
12232 call = NULL_TREE;
12233 retval = NULL_TREE;
12235 if (!init_target_chars ())
12236 return NULL_TREE;
12238 /* If the format doesn't contain % args or %%, use strcpy. */
12239 if (strchr (fmt_str, target_percent) == NULL)
12241 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12243 if (!fn)
12244 return NULL_TREE;
12246 /* Don't optimize sprintf (buf, "abc", ptr++). */
12247 if (orig)
12248 return NULL_TREE;
12250 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12251 'format' is known to contain no % formats. */
12252 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12253 if (!ignored)
12254 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12257 /* If the format is "%s", use strcpy if the result isn't used. */
12258 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12260 tree fn;
12261 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12263 if (!fn)
12264 return NULL_TREE;
12266 /* Don't crash on sprintf (str1, "%s"). */
12267 if (!orig)
12268 return NULL_TREE;
12270 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12271 if (!ignored)
12273 retval = c_strlen (orig, 1);
12274 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12275 return NULL_TREE;
12277 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12280 if (call && retval)
12282 retval = fold_convert_loc
12283 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12284 retval);
12285 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12287 else
12288 return call;
12291 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12292 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12293 attempt to simplify calls with more than 4 arguments.
12295 Return NULL_TREE if no simplification was possible, otherwise return the
12296 simplified form of the call as a tree. If IGNORED is true, it means that
12297 the caller does not use the returned value of the function. */
12299 static tree
12300 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12301 tree orig, int ignored)
12303 tree call, retval;
12304 const char *fmt_str = NULL;
12305 unsigned HOST_WIDE_INT destlen;
12307 /* Verify the required arguments in the original call. We deal with two
12308 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12309 'snprintf (dest, cst, "%s", orig)'. */
12310 if (!validate_arg (dest, POINTER_TYPE)
12311 || !validate_arg (destsize, INTEGER_TYPE)
12312 || !validate_arg (fmt, POINTER_TYPE))
12313 return NULL_TREE;
12314 if (orig && !validate_arg (orig, POINTER_TYPE))
12315 return NULL_TREE;
12317 if (!host_integerp (destsize, 1))
12318 return NULL_TREE;
12320 /* Check whether the format is a literal string constant. */
12321 fmt_str = c_getstr (fmt);
12322 if (fmt_str == NULL)
12323 return NULL_TREE;
12325 call = NULL_TREE;
12326 retval = NULL_TREE;
12328 if (!init_target_chars ())
12329 return NULL_TREE;
12331 destlen = tree_low_cst (destsize, 1);
12333 /* If the format doesn't contain % args or %%, use strcpy. */
12334 if (strchr (fmt_str, target_percent) == NULL)
12336 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12337 size_t len = strlen (fmt_str);
12339 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12340 if (orig)
12341 return NULL_TREE;
12343 /* We could expand this as
12344 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12345 or to
12346 memcpy (str, fmt_with_nul_at_cstm1, cst);
12347 but in the former case that might increase code size
12348 and in the latter case grow .rodata section too much.
12349 So punt for now. */
12350 if (len >= destlen)
12351 return NULL_TREE;
12353 if (!fn)
12354 return NULL_TREE;
12356 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12357 'format' is known to contain no % formats and
12358 strlen (fmt) < cst. */
12359 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12361 if (!ignored)
12362 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12365 /* If the format is "%s", use strcpy if the result isn't used. */
12366 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12368 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12369 unsigned HOST_WIDE_INT origlen;
12371 /* Don't crash on snprintf (str1, cst, "%s"). */
12372 if (!orig)
12373 return NULL_TREE;
12375 retval = c_strlen (orig, 1);
12376 if (!retval || !host_integerp (retval, 1))
12377 return NULL_TREE;
12379 origlen = tree_low_cst (retval, 1);
12380 /* We could expand this as
12381 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12382 or to
12383 memcpy (str1, str2_with_nul_at_cstm1, cst);
12384 but in the former case that might increase code size
12385 and in the latter case grow .rodata section too much.
12386 So punt for now. */
12387 if (origlen >= destlen)
12388 return NULL_TREE;
12390 /* Convert snprintf (str1, cst, "%s", str2) into
12391 strcpy (str1, str2) if strlen (str2) < cst. */
12392 if (!fn)
12393 return NULL_TREE;
12395 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12397 if (ignored)
12398 retval = NULL_TREE;
12401 if (call && retval)
12403 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12404 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12405 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12407 else
12408 return call;
12411 /* Expand a call EXP to __builtin_object_size. */
12414 expand_builtin_object_size (tree exp)
12416 tree ost;
12417 int object_size_type;
12418 tree fndecl = get_callee_fndecl (exp);
12420 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12422 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12423 exp, fndecl);
12424 expand_builtin_trap ();
12425 return const0_rtx;
12428 ost = CALL_EXPR_ARG (exp, 1);
12429 STRIP_NOPS (ost);
12431 if (TREE_CODE (ost) != INTEGER_CST
12432 || tree_int_cst_sgn (ost) < 0
12433 || compare_tree_int (ost, 3) > 0)
12435 error ("%Klast argument of %D is not integer constant between 0 and 3",
12436 exp, fndecl);
12437 expand_builtin_trap ();
12438 return const0_rtx;
12441 object_size_type = tree_low_cst (ost, 0);
12443 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12446 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12447 FCODE is the BUILT_IN_* to use.
12448 Return NULL_RTX if we failed; the caller should emit a normal call,
12449 otherwise try to get the result in TARGET, if convenient (and in
12450 mode MODE if that's convenient). */
12452 static rtx
12453 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12454 enum built_in_function fcode)
12456 tree dest, src, len, size;
12458 if (!validate_arglist (exp,
12459 POINTER_TYPE,
12460 fcode == BUILT_IN_MEMSET_CHK
12461 ? INTEGER_TYPE : POINTER_TYPE,
12462 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12463 return NULL_RTX;
12465 dest = CALL_EXPR_ARG (exp, 0);
12466 src = CALL_EXPR_ARG (exp, 1);
12467 len = CALL_EXPR_ARG (exp, 2);
12468 size = CALL_EXPR_ARG (exp, 3);
12470 if (! host_integerp (size, 1))
12471 return NULL_RTX;
12473 if (host_integerp (len, 1) || integer_all_onesp (size))
12475 tree fn;
12477 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12479 warning_at (tree_nonartificial_location (exp),
12480 0, "%Kcall to %D will always overflow destination buffer",
12481 exp, get_callee_fndecl (exp));
12482 return NULL_RTX;
12485 fn = NULL_TREE;
12486 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12487 mem{cpy,pcpy,move,set} is available. */
12488 switch (fcode)
12490 case BUILT_IN_MEMCPY_CHK:
12491 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12492 break;
12493 case BUILT_IN_MEMPCPY_CHK:
12494 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12495 break;
12496 case BUILT_IN_MEMMOVE_CHK:
12497 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12498 break;
12499 case BUILT_IN_MEMSET_CHK:
12500 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12501 break;
12502 default:
12503 break;
12506 if (! fn)
12507 return NULL_RTX;
12509 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12510 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12511 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12512 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12514 else if (fcode == BUILT_IN_MEMSET_CHK)
12515 return NULL_RTX;
12516 else
12518 unsigned int dest_align = get_pointer_alignment (dest);
12520 /* If DEST is not a pointer type, call the normal function. */
12521 if (dest_align == 0)
12522 return NULL_RTX;
12524 /* If SRC and DEST are the same (and not volatile), do nothing. */
12525 if (operand_equal_p (src, dest, 0))
12527 tree expr;
12529 if (fcode != BUILT_IN_MEMPCPY_CHK)
12531 /* Evaluate and ignore LEN in case it has side-effects. */
12532 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12533 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12536 expr = fold_build_pointer_plus (dest, len);
12537 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12540 /* __memmove_chk special case. */
12541 if (fcode == BUILT_IN_MEMMOVE_CHK)
12543 unsigned int src_align = get_pointer_alignment (src);
12545 if (src_align == 0)
12546 return NULL_RTX;
12548 /* If src is categorized for a readonly section we can use
12549 normal __memcpy_chk. */
12550 if (readonly_data_expr (src))
12552 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12553 if (!fn)
12554 return NULL_RTX;
12555 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12556 dest, src, len, size);
12557 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12558 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12559 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12562 return NULL_RTX;
12566 /* Emit warning if a buffer overflow is detected at compile time. */
12568 static void
12569 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12571 int is_strlen = 0;
12572 tree len, size;
12573 location_t loc = tree_nonartificial_location (exp);
12575 switch (fcode)
12577 case BUILT_IN_STRCPY_CHK:
12578 case BUILT_IN_STPCPY_CHK:
12579 /* For __strcat_chk the warning will be emitted only if overflowing
12580 by at least strlen (dest) + 1 bytes. */
12581 case BUILT_IN_STRCAT_CHK:
12582 len = CALL_EXPR_ARG (exp, 1);
12583 size = CALL_EXPR_ARG (exp, 2);
12584 is_strlen = 1;
12585 break;
12586 case BUILT_IN_STRNCAT_CHK:
12587 case BUILT_IN_STRNCPY_CHK:
12588 case BUILT_IN_STPNCPY_CHK:
12589 len = CALL_EXPR_ARG (exp, 2);
12590 size = CALL_EXPR_ARG (exp, 3);
12591 break;
12592 case BUILT_IN_SNPRINTF_CHK:
12593 case BUILT_IN_VSNPRINTF_CHK:
12594 len = CALL_EXPR_ARG (exp, 1);
12595 size = CALL_EXPR_ARG (exp, 3);
12596 break;
12597 default:
12598 gcc_unreachable ();
12601 if (!len || !size)
12602 return;
12604 if (! host_integerp (size, 1) || integer_all_onesp (size))
12605 return;
12607 if (is_strlen)
12609 len = c_strlen (len, 1);
12610 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12611 return;
12613 else if (fcode == BUILT_IN_STRNCAT_CHK)
12615 tree src = CALL_EXPR_ARG (exp, 1);
12616 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12617 return;
12618 src = c_strlen (src, 1);
12619 if (! src || ! host_integerp (src, 1))
12621 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12622 exp, get_callee_fndecl (exp));
12623 return;
12625 else if (tree_int_cst_lt (src, size))
12626 return;
12628 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12629 return;
12631 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12632 exp, get_callee_fndecl (exp));
12635 /* Emit warning if a buffer overflow is detected at compile time
12636 in __sprintf_chk/__vsprintf_chk calls. */
12638 static void
12639 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12641 tree size, len, fmt;
12642 const char *fmt_str;
12643 int nargs = call_expr_nargs (exp);
12645 /* Verify the required arguments in the original call. */
12647 if (nargs < 4)
12648 return;
12649 size = CALL_EXPR_ARG (exp, 2);
12650 fmt = CALL_EXPR_ARG (exp, 3);
12652 if (! host_integerp (size, 1) || integer_all_onesp (size))
12653 return;
12655 /* Check whether the format is a literal string constant. */
12656 fmt_str = c_getstr (fmt);
12657 if (fmt_str == NULL)
12658 return;
12660 if (!init_target_chars ())
12661 return;
12663 /* If the format doesn't contain % args or %%, we know its size. */
12664 if (strchr (fmt_str, target_percent) == 0)
12665 len = build_int_cstu (size_type_node, strlen (fmt_str));
12666 /* If the format is "%s" and first ... argument is a string literal,
12667 we know it too. */
12668 else if (fcode == BUILT_IN_SPRINTF_CHK
12669 && strcmp (fmt_str, target_percent_s) == 0)
12671 tree arg;
12673 if (nargs < 5)
12674 return;
12675 arg = CALL_EXPR_ARG (exp, 4);
12676 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12677 return;
12679 len = c_strlen (arg, 1);
12680 if (!len || ! host_integerp (len, 1))
12681 return;
12683 else
12684 return;
12686 if (! tree_int_cst_lt (len, size))
12687 warning_at (tree_nonartificial_location (exp),
12688 0, "%Kcall to %D will always overflow destination buffer",
12689 exp, get_callee_fndecl (exp));
12692 /* Emit warning if a free is called with address of a variable. */
12694 static void
12695 maybe_emit_free_warning (tree exp)
12697 tree arg = CALL_EXPR_ARG (exp, 0);
12699 STRIP_NOPS (arg);
12700 if (TREE_CODE (arg) != ADDR_EXPR)
12701 return;
12703 arg = get_base_address (TREE_OPERAND (arg, 0));
12704 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12705 return;
12707 if (SSA_VAR_P (arg))
12708 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12709 "%Kattempt to free a non-heap object %qD", exp, arg);
12710 else
12711 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12712 "%Kattempt to free a non-heap object", exp);
12715 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12716 if possible. */
12718 tree
12719 fold_builtin_object_size (tree ptr, tree ost)
12721 unsigned HOST_WIDE_INT bytes;
12722 int object_size_type;
12724 if (!validate_arg (ptr, POINTER_TYPE)
12725 || !validate_arg (ost, INTEGER_TYPE))
12726 return NULL_TREE;
12728 STRIP_NOPS (ost);
12730 if (TREE_CODE (ost) != INTEGER_CST
12731 || tree_int_cst_sgn (ost) < 0
12732 || compare_tree_int (ost, 3) > 0)
12733 return NULL_TREE;
12735 object_size_type = tree_low_cst (ost, 0);
12737 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12738 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12739 and (size_t) 0 for types 2 and 3. */
12740 if (TREE_SIDE_EFFECTS (ptr))
12741 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12743 if (TREE_CODE (ptr) == ADDR_EXPR)
12745 bytes = compute_builtin_object_size (ptr, object_size_type);
12746 if (double_int_fits_to_tree_p (size_type_node,
12747 double_int::from_uhwi (bytes)))
12748 return build_int_cstu (size_type_node, bytes);
12750 else if (TREE_CODE (ptr) == SSA_NAME)
12752 /* If object size is not known yet, delay folding until
12753 later. Maybe subsequent passes will help determining
12754 it. */
12755 bytes = compute_builtin_object_size (ptr, object_size_type);
12756 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12757 && double_int_fits_to_tree_p (size_type_node,
12758 double_int::from_uhwi (bytes)))
12759 return build_int_cstu (size_type_node, bytes);
12762 return NULL_TREE;
12765 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12766 DEST, SRC, LEN, and SIZE are the arguments to the call.
12767 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12768 code of the builtin. If MAXLEN is not NULL, it is maximum length
12769 passed as third argument. */
12771 tree
12772 fold_builtin_memory_chk (location_t loc, tree fndecl,
12773 tree dest, tree src, tree len, tree size,
12774 tree maxlen, bool ignore,
12775 enum built_in_function fcode)
12777 tree fn;
12779 if (!validate_arg (dest, POINTER_TYPE)
12780 || !validate_arg (src,
12781 (fcode == BUILT_IN_MEMSET_CHK
12782 ? INTEGER_TYPE : POINTER_TYPE))
12783 || !validate_arg (len, INTEGER_TYPE)
12784 || !validate_arg (size, INTEGER_TYPE))
12785 return NULL_TREE;
12787 /* If SRC and DEST are the same (and not volatile), return DEST
12788 (resp. DEST+LEN for __mempcpy_chk). */
12789 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12791 if (fcode != BUILT_IN_MEMPCPY_CHK)
12792 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12793 dest, len);
12794 else
12796 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12797 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12801 if (! host_integerp (size, 1))
12802 return NULL_TREE;
12804 if (! integer_all_onesp (size))
12806 if (! host_integerp (len, 1))
12808 /* If LEN is not constant, try MAXLEN too.
12809 For MAXLEN only allow optimizing into non-_ocs function
12810 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12811 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12813 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12815 /* (void) __mempcpy_chk () can be optimized into
12816 (void) __memcpy_chk (). */
12817 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12818 if (!fn)
12819 return NULL_TREE;
12821 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12823 return NULL_TREE;
12826 else
12827 maxlen = len;
12829 if (tree_int_cst_lt (size, maxlen))
12830 return NULL_TREE;
12833 fn = NULL_TREE;
12834 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12835 mem{cpy,pcpy,move,set} is available. */
12836 switch (fcode)
12838 case BUILT_IN_MEMCPY_CHK:
12839 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12840 break;
12841 case BUILT_IN_MEMPCPY_CHK:
12842 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12843 break;
12844 case BUILT_IN_MEMMOVE_CHK:
12845 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12846 break;
12847 case BUILT_IN_MEMSET_CHK:
12848 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12849 break;
12850 default:
12851 break;
12854 if (!fn)
12855 return NULL_TREE;
12857 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12860 /* Fold a call to the __st[rp]cpy_chk builtin.
12861 DEST, SRC, and SIZE are the arguments to the call.
12862 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12863 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12864 strings passed as second argument. */
12866 tree
12867 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12868 tree src, tree size,
12869 tree maxlen, bool ignore,
12870 enum built_in_function fcode)
12872 tree len, fn;
12874 if (!validate_arg (dest, POINTER_TYPE)
12875 || !validate_arg (src, POINTER_TYPE)
12876 || !validate_arg (size, INTEGER_TYPE))
12877 return NULL_TREE;
12879 /* If SRC and DEST are the same (and not volatile), return DEST. */
12880 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12881 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12883 if (! host_integerp (size, 1))
12884 return NULL_TREE;
12886 if (! integer_all_onesp (size))
12888 len = c_strlen (src, 1);
12889 if (! len || ! host_integerp (len, 1))
12891 /* If LEN is not constant, try MAXLEN too.
12892 For MAXLEN only allow optimizing into non-_ocs function
12893 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12894 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12896 if (fcode == BUILT_IN_STPCPY_CHK)
12898 if (! ignore)
12899 return NULL_TREE;
12901 /* If return value of __stpcpy_chk is ignored,
12902 optimize into __strcpy_chk. */
12903 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12904 if (!fn)
12905 return NULL_TREE;
12907 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12910 if (! len || TREE_SIDE_EFFECTS (len))
12911 return NULL_TREE;
12913 /* If c_strlen returned something, but not a constant,
12914 transform __strcpy_chk into __memcpy_chk. */
12915 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12916 if (!fn)
12917 return NULL_TREE;
12919 len = fold_convert_loc (loc, size_type_node, len);
12920 len = size_binop_loc (loc, PLUS_EXPR, len,
12921 build_int_cst (size_type_node, 1));
12922 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12923 build_call_expr_loc (loc, fn, 4,
12924 dest, src, len, size));
12927 else
12928 maxlen = len;
12930 if (! tree_int_cst_lt (maxlen, size))
12931 return NULL_TREE;
12934 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12935 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12936 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12937 if (!fn)
12938 return NULL_TREE;
12940 return build_call_expr_loc (loc, fn, 2, dest, src);
12943 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12944 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12945 length passed as third argument. IGNORE is true if return value can be
12946 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12948 tree
12949 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12950 tree len, tree size, tree maxlen, bool ignore,
12951 enum built_in_function fcode)
12953 tree fn;
12955 if (!validate_arg (dest, POINTER_TYPE)
12956 || !validate_arg (src, POINTER_TYPE)
12957 || !validate_arg (len, INTEGER_TYPE)
12958 || !validate_arg (size, INTEGER_TYPE))
12959 return NULL_TREE;
12961 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12963 /* If return value of __stpncpy_chk is ignored,
12964 optimize into __strncpy_chk. */
12965 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12966 if (fn)
12967 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12970 if (! host_integerp (size, 1))
12971 return NULL_TREE;
12973 if (! integer_all_onesp (size))
12975 if (! host_integerp (len, 1))
12977 /* If LEN is not constant, try MAXLEN too.
12978 For MAXLEN only allow optimizing into non-_ocs function
12979 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12980 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12981 return NULL_TREE;
12983 else
12984 maxlen = len;
12986 if (tree_int_cst_lt (size, maxlen))
12987 return NULL_TREE;
12990 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12991 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12992 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12993 if (!fn)
12994 return NULL_TREE;
12996 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12999 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
13000 are the arguments to the call. */
13002 static tree
13003 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
13004 tree src, tree size)
13006 tree fn;
13007 const char *p;
13009 if (!validate_arg (dest, POINTER_TYPE)
13010 || !validate_arg (src, POINTER_TYPE)
13011 || !validate_arg (size, INTEGER_TYPE))
13012 return NULL_TREE;
13014 p = c_getstr (src);
13015 /* If the SRC parameter is "", return DEST. */
13016 if (p && *p == '\0')
13017 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13019 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
13020 return NULL_TREE;
13022 /* If __builtin_strcat_chk is used, assume strcat is available. */
13023 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
13024 if (!fn)
13025 return NULL_TREE;
13027 return build_call_expr_loc (loc, fn, 2, dest, src);
13030 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13031 LEN, and SIZE. */
13033 static tree
13034 fold_builtin_strncat_chk (location_t loc, tree fndecl,
13035 tree dest, tree src, tree len, tree size)
13037 tree fn;
13038 const char *p;
13040 if (!validate_arg (dest, POINTER_TYPE)
13041 || !validate_arg (src, POINTER_TYPE)
13042 || !validate_arg (size, INTEGER_TYPE)
13043 || !validate_arg (size, INTEGER_TYPE))
13044 return NULL_TREE;
13046 p = c_getstr (src);
13047 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13048 if (p && *p == '\0')
13049 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13050 else if (integer_zerop (len))
13051 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13053 if (! host_integerp (size, 1))
13054 return NULL_TREE;
13056 if (! integer_all_onesp (size))
13058 tree src_len = c_strlen (src, 1);
13059 if (src_len
13060 && host_integerp (src_len, 1)
13061 && host_integerp (len, 1)
13062 && ! tree_int_cst_lt (len, src_len))
13064 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13065 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13066 if (!fn)
13067 return NULL_TREE;
13069 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13071 return NULL_TREE;
13074 /* If __builtin_strncat_chk is used, assume strncat is available. */
13075 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13076 if (!fn)
13077 return NULL_TREE;
13079 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13082 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13083 Return NULL_TREE if a normal call should be emitted rather than
13084 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13085 or BUILT_IN_VSPRINTF_CHK. */
13087 static tree
13088 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13089 enum built_in_function fcode)
13091 tree dest, size, len, fn, fmt, flag;
13092 const char *fmt_str;
13094 /* Verify the required arguments in the original call. */
13095 if (nargs < 4)
13096 return NULL_TREE;
13097 dest = args[0];
13098 if (!validate_arg (dest, POINTER_TYPE))
13099 return NULL_TREE;
13100 flag = args[1];
13101 if (!validate_arg (flag, INTEGER_TYPE))
13102 return NULL_TREE;
13103 size = args[2];
13104 if (!validate_arg (size, INTEGER_TYPE))
13105 return NULL_TREE;
13106 fmt = args[3];
13107 if (!validate_arg (fmt, POINTER_TYPE))
13108 return NULL_TREE;
13110 if (! host_integerp (size, 1))
13111 return NULL_TREE;
13113 len = NULL_TREE;
13115 if (!init_target_chars ())
13116 return NULL_TREE;
13118 /* Check whether the format is a literal string constant. */
13119 fmt_str = c_getstr (fmt);
13120 if (fmt_str != NULL)
13122 /* If the format doesn't contain % args or %%, we know the size. */
13123 if (strchr (fmt_str, target_percent) == 0)
13125 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13126 len = build_int_cstu (size_type_node, strlen (fmt_str));
13128 /* If the format is "%s" and first ... argument is a string literal,
13129 we know the size too. */
13130 else if (fcode == BUILT_IN_SPRINTF_CHK
13131 && strcmp (fmt_str, target_percent_s) == 0)
13133 tree arg;
13135 if (nargs == 5)
13137 arg = args[4];
13138 if (validate_arg (arg, POINTER_TYPE))
13140 len = c_strlen (arg, 1);
13141 if (! len || ! host_integerp (len, 1))
13142 len = NULL_TREE;
13148 if (! integer_all_onesp (size))
13150 if (! len || ! tree_int_cst_lt (len, size))
13151 return NULL_TREE;
13154 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13155 or if format doesn't contain % chars or is "%s". */
13156 if (! integer_zerop (flag))
13158 if (fmt_str == NULL)
13159 return NULL_TREE;
13160 if (strchr (fmt_str, target_percent) != NULL
13161 && strcmp (fmt_str, target_percent_s))
13162 return NULL_TREE;
13165 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13166 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13167 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13168 if (!fn)
13169 return NULL_TREE;
13171 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13174 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13175 a normal call should be emitted rather than expanding the function
13176 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13178 static tree
13179 fold_builtin_sprintf_chk (location_t loc, tree exp,
13180 enum built_in_function fcode)
13182 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13183 CALL_EXPR_ARGP (exp), fcode);
13186 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13187 NULL_TREE if a normal call should be emitted rather than expanding
13188 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13189 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13190 passed as second argument. */
13192 static tree
13193 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13194 tree maxlen, enum built_in_function fcode)
13196 tree dest, size, len, fn, fmt, flag;
13197 const char *fmt_str;
13199 /* Verify the required arguments in the original call. */
13200 if (nargs < 5)
13201 return NULL_TREE;
13202 dest = args[0];
13203 if (!validate_arg (dest, POINTER_TYPE))
13204 return NULL_TREE;
13205 len = args[1];
13206 if (!validate_arg (len, INTEGER_TYPE))
13207 return NULL_TREE;
13208 flag = args[2];
13209 if (!validate_arg (flag, INTEGER_TYPE))
13210 return NULL_TREE;
13211 size = args[3];
13212 if (!validate_arg (size, INTEGER_TYPE))
13213 return NULL_TREE;
13214 fmt = args[4];
13215 if (!validate_arg (fmt, POINTER_TYPE))
13216 return NULL_TREE;
13218 if (! host_integerp (size, 1))
13219 return NULL_TREE;
13221 if (! integer_all_onesp (size))
13223 if (! host_integerp (len, 1))
13225 /* If LEN is not constant, try MAXLEN too.
13226 For MAXLEN only allow optimizing into non-_ocs function
13227 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13228 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13229 return NULL_TREE;
13231 else
13232 maxlen = len;
13234 if (tree_int_cst_lt (size, maxlen))
13235 return NULL_TREE;
13238 if (!init_target_chars ())
13239 return NULL_TREE;
13241 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13242 or if format doesn't contain % chars or is "%s". */
13243 if (! integer_zerop (flag))
13245 fmt_str = c_getstr (fmt);
13246 if (fmt_str == NULL)
13247 return NULL_TREE;
13248 if (strchr (fmt_str, target_percent) != NULL
13249 && strcmp (fmt_str, target_percent_s))
13250 return NULL_TREE;
13253 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13254 available. */
13255 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13256 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13257 if (!fn)
13258 return NULL_TREE;
13260 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13263 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13264 a normal call should be emitted rather than expanding the function
13265 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13266 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13267 passed as second argument. */
13269 tree
13270 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13271 enum built_in_function fcode)
13273 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13274 CALL_EXPR_ARGP (exp), maxlen, fcode);
13277 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13278 FMT and ARG are the arguments to the call; we don't fold cases with
13279 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13281 Return NULL_TREE if no simplification was possible, otherwise return the
13282 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13283 code of the function to be simplified. */
13285 static tree
13286 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13287 tree arg, bool ignore,
13288 enum built_in_function fcode)
13290 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13291 const char *fmt_str = NULL;
13293 /* If the return value is used, don't do the transformation. */
13294 if (! ignore)
13295 return NULL_TREE;
13297 /* Verify the required arguments in the original call. */
13298 if (!validate_arg (fmt, POINTER_TYPE))
13299 return NULL_TREE;
13301 /* Check whether the format is a literal string constant. */
13302 fmt_str = c_getstr (fmt);
13303 if (fmt_str == NULL)
13304 return NULL_TREE;
13306 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13308 /* If we're using an unlocked function, assume the other
13309 unlocked functions exist explicitly. */
13310 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13311 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13313 else
13315 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13316 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13319 if (!init_target_chars ())
13320 return NULL_TREE;
13322 if (strcmp (fmt_str, target_percent_s) == 0
13323 || strchr (fmt_str, target_percent) == NULL)
13325 const char *str;
13327 if (strcmp (fmt_str, target_percent_s) == 0)
13329 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13330 return NULL_TREE;
13332 if (!arg || !validate_arg (arg, POINTER_TYPE))
13333 return NULL_TREE;
13335 str = c_getstr (arg);
13336 if (str == NULL)
13337 return NULL_TREE;
13339 else
13341 /* The format specifier doesn't contain any '%' characters. */
13342 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13343 && arg)
13344 return NULL_TREE;
13345 str = fmt_str;
13348 /* If the string was "", printf does nothing. */
13349 if (str[0] == '\0')
13350 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13352 /* If the string has length of 1, call putchar. */
13353 if (str[1] == '\0')
13355 /* Given printf("c"), (where c is any one character,)
13356 convert "c"[0] to an int and pass that to the replacement
13357 function. */
13358 newarg = build_int_cst (integer_type_node, str[0]);
13359 if (fn_putchar)
13360 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13362 else
13364 /* If the string was "string\n", call puts("string"). */
13365 size_t len = strlen (str);
13366 if ((unsigned char)str[len - 1] == target_newline
13367 && (size_t) (int) len == len
13368 && (int) len > 0)
13370 char *newstr;
13371 tree offset_node, string_cst;
13373 /* Create a NUL-terminated string that's one char shorter
13374 than the original, stripping off the trailing '\n'. */
13375 newarg = build_string_literal (len, str);
13376 string_cst = string_constant (newarg, &offset_node);
13377 gcc_checking_assert (string_cst
13378 && (TREE_STRING_LENGTH (string_cst)
13379 == (int) len)
13380 && integer_zerop (offset_node)
13381 && (unsigned char)
13382 TREE_STRING_POINTER (string_cst)[len - 1]
13383 == target_newline);
13384 /* build_string_literal creates a new STRING_CST,
13385 modify it in place to avoid double copying. */
13386 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13387 newstr[len - 1] = '\0';
13388 if (fn_puts)
13389 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13391 else
13392 /* We'd like to arrange to call fputs(string,stdout) here,
13393 but we need stdout and don't have a way to get it yet. */
13394 return NULL_TREE;
13398 /* The other optimizations can be done only on the non-va_list variants. */
13399 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13400 return NULL_TREE;
13402 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13403 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13405 if (!arg || !validate_arg (arg, POINTER_TYPE))
13406 return NULL_TREE;
13407 if (fn_puts)
13408 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13411 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13412 else if (strcmp (fmt_str, target_percent_c) == 0)
13414 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13415 return NULL_TREE;
13416 if (fn_putchar)
13417 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13420 if (!call)
13421 return NULL_TREE;
13423 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13426 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13427 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13428 more than 3 arguments, and ARG may be null in the 2-argument case.
13430 Return NULL_TREE if no simplification was possible, otherwise return the
13431 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13432 code of the function to be simplified. */
13434 static tree
13435 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13436 tree fmt, tree arg, bool ignore,
13437 enum built_in_function fcode)
13439 tree fn_fputc, fn_fputs, call = NULL_TREE;
13440 const char *fmt_str = NULL;
13442 /* If the return value is used, don't do the transformation. */
13443 if (! ignore)
13444 return NULL_TREE;
13446 /* Verify the required arguments in the original call. */
13447 if (!validate_arg (fp, POINTER_TYPE))
13448 return NULL_TREE;
13449 if (!validate_arg (fmt, POINTER_TYPE))
13450 return NULL_TREE;
13452 /* Check whether the format is a literal string constant. */
13453 fmt_str = c_getstr (fmt);
13454 if (fmt_str == NULL)
13455 return NULL_TREE;
13457 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13459 /* If we're using an unlocked function, assume the other
13460 unlocked functions exist explicitly. */
13461 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13462 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13464 else
13466 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13467 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13470 if (!init_target_chars ())
13471 return NULL_TREE;
13473 /* If the format doesn't contain % args or %%, use strcpy. */
13474 if (strchr (fmt_str, target_percent) == NULL)
13476 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13477 && arg)
13478 return NULL_TREE;
13480 /* If the format specifier was "", fprintf does nothing. */
13481 if (fmt_str[0] == '\0')
13483 /* If FP has side-effects, just wait until gimplification is
13484 done. */
13485 if (TREE_SIDE_EFFECTS (fp))
13486 return NULL_TREE;
13488 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13491 /* When "string" doesn't contain %, replace all cases of
13492 fprintf (fp, string) with fputs (string, fp). The fputs
13493 builtin will take care of special cases like length == 1. */
13494 if (fn_fputs)
13495 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13498 /* The other optimizations can be done only on the non-va_list variants. */
13499 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13500 return NULL_TREE;
13502 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13503 else if (strcmp (fmt_str, target_percent_s) == 0)
13505 if (!arg || !validate_arg (arg, POINTER_TYPE))
13506 return NULL_TREE;
13507 if (fn_fputs)
13508 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13511 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13512 else if (strcmp (fmt_str, target_percent_c) == 0)
13514 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13515 return NULL_TREE;
13516 if (fn_fputc)
13517 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13520 if (!call)
13521 return NULL_TREE;
13522 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13525 /* Initialize format string characters in the target charset. */
13527 static bool
13528 init_target_chars (void)
13530 static bool init;
13531 if (!init)
13533 target_newline = lang_hooks.to_target_charset ('\n');
13534 target_percent = lang_hooks.to_target_charset ('%');
13535 target_c = lang_hooks.to_target_charset ('c');
13536 target_s = lang_hooks.to_target_charset ('s');
13537 if (target_newline == 0 || target_percent == 0 || target_c == 0
13538 || target_s == 0)
13539 return false;
13541 target_percent_c[0] = target_percent;
13542 target_percent_c[1] = target_c;
13543 target_percent_c[2] = '\0';
13545 target_percent_s[0] = target_percent;
13546 target_percent_s[1] = target_s;
13547 target_percent_s[2] = '\0';
13549 target_percent_s_newline[0] = target_percent;
13550 target_percent_s_newline[1] = target_s;
13551 target_percent_s_newline[2] = target_newline;
13552 target_percent_s_newline[3] = '\0';
13554 init = true;
13556 return true;
13559 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13560 and no overflow/underflow occurred. INEXACT is true if M was not
13561 exactly calculated. TYPE is the tree type for the result. This
13562 function assumes that you cleared the MPFR flags and then
13563 calculated M to see if anything subsequently set a flag prior to
13564 entering this function. Return NULL_TREE if any checks fail. */
13566 static tree
13567 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13569 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13570 overflow/underflow occurred. If -frounding-math, proceed iff the
13571 result of calling FUNC was exact. */
13572 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13573 && (!flag_rounding_math || !inexact))
13575 REAL_VALUE_TYPE rr;
13577 real_from_mpfr (&rr, m, type, GMP_RNDN);
13578 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13579 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13580 but the mpft_t is not, then we underflowed in the
13581 conversion. */
13582 if (real_isfinite (&rr)
13583 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13585 REAL_VALUE_TYPE rmode;
13587 real_convert (&rmode, TYPE_MODE (type), &rr);
13588 /* Proceed iff the specified mode can hold the value. */
13589 if (real_identical (&rmode, &rr))
13590 return build_real (type, rmode);
13593 return NULL_TREE;
13596 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13597 number and no overflow/underflow occurred. INEXACT is true if M
13598 was not exactly calculated. TYPE is the tree type for the result.
13599 This function assumes that you cleared the MPFR flags and then
13600 calculated M to see if anything subsequently set a flag prior to
13601 entering this function. Return NULL_TREE if any checks fail, if
13602 FORCE_CONVERT is true, then bypass the checks. */
13604 static tree
13605 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13607 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13608 overflow/underflow occurred. If -frounding-math, proceed iff the
13609 result of calling FUNC was exact. */
13610 if (force_convert
13611 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13612 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13613 && (!flag_rounding_math || !inexact)))
13615 REAL_VALUE_TYPE re, im;
13617 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13618 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13619 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13620 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13621 but the mpft_t is not, then we underflowed in the
13622 conversion. */
13623 if (force_convert
13624 || (real_isfinite (&re) && real_isfinite (&im)
13625 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13626 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13628 REAL_VALUE_TYPE re_mode, im_mode;
13630 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13631 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13632 /* Proceed iff the specified mode can hold the value. */
13633 if (force_convert
13634 || (real_identical (&re_mode, &re)
13635 && real_identical (&im_mode, &im)))
13636 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13637 build_real (TREE_TYPE (type), im_mode));
13640 return NULL_TREE;
13643 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13644 FUNC on it and return the resulting value as a tree with type TYPE.
13645 If MIN and/or MAX are not NULL, then the supplied ARG must be
13646 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13647 acceptable values, otherwise they are not. The mpfr precision is
13648 set to the precision of TYPE. We assume that function FUNC returns
13649 zero if the result could be calculated exactly within the requested
13650 precision. */
13652 static tree
13653 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13654 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13655 bool inclusive)
13657 tree result = NULL_TREE;
13659 STRIP_NOPS (arg);
13661 /* To proceed, MPFR must exactly represent the target floating point
13662 format, which only happens when the target base equals two. */
13663 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13664 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13666 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13668 if (real_isfinite (ra)
13669 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13670 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13672 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13673 const int prec = fmt->p;
13674 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13675 int inexact;
13676 mpfr_t m;
13678 mpfr_init2 (m, prec);
13679 mpfr_from_real (m, ra, GMP_RNDN);
13680 mpfr_clear_flags ();
13681 inexact = func (m, m, rnd);
13682 result = do_mpfr_ckconv (m, type, inexact);
13683 mpfr_clear (m);
13687 return result;
13690 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13691 FUNC on it and return the resulting value as a tree with type TYPE.
13692 The mpfr precision is set to the precision of TYPE. We assume that
13693 function FUNC returns zero if the result could be calculated
13694 exactly within the requested precision. */
13696 static tree
13697 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13698 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13700 tree result = NULL_TREE;
13702 STRIP_NOPS (arg1);
13703 STRIP_NOPS (arg2);
13705 /* To proceed, MPFR must exactly represent the target floating point
13706 format, which only happens when the target base equals two. */
13707 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13708 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13709 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13711 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13712 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13714 if (real_isfinite (ra1) && real_isfinite (ra2))
13716 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13717 const int prec = fmt->p;
13718 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13719 int inexact;
13720 mpfr_t m1, m2;
13722 mpfr_inits2 (prec, m1, m2, NULL);
13723 mpfr_from_real (m1, ra1, GMP_RNDN);
13724 mpfr_from_real (m2, ra2, GMP_RNDN);
13725 mpfr_clear_flags ();
13726 inexact = func (m1, m1, m2, rnd);
13727 result = do_mpfr_ckconv (m1, type, inexact);
13728 mpfr_clears (m1, m2, NULL);
13732 return result;
13735 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13736 FUNC on it and return the resulting value as a tree with type TYPE.
13737 The mpfr precision is set to the precision of TYPE. We assume that
13738 function FUNC returns zero if the result could be calculated
13739 exactly within the requested precision. */
13741 static tree
13742 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13743 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13745 tree result = NULL_TREE;
13747 STRIP_NOPS (arg1);
13748 STRIP_NOPS (arg2);
13749 STRIP_NOPS (arg3);
13751 /* To proceed, MPFR must exactly represent the target floating point
13752 format, which only happens when the target base equals two. */
13753 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13754 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13755 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13756 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13758 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13759 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13760 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13762 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13764 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13765 const int prec = fmt->p;
13766 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13767 int inexact;
13768 mpfr_t m1, m2, m3;
13770 mpfr_inits2 (prec, m1, m2, m3, NULL);
13771 mpfr_from_real (m1, ra1, GMP_RNDN);
13772 mpfr_from_real (m2, ra2, GMP_RNDN);
13773 mpfr_from_real (m3, ra3, GMP_RNDN);
13774 mpfr_clear_flags ();
13775 inexact = func (m1, m1, m2, m3, rnd);
13776 result = do_mpfr_ckconv (m1, type, inexact);
13777 mpfr_clears (m1, m2, m3, NULL);
13781 return result;
13784 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13785 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13786 If ARG_SINP and ARG_COSP are NULL then the result is returned
13787 as a complex value.
13788 The type is taken from the type of ARG and is used for setting the
13789 precision of the calculation and results. */
13791 static tree
13792 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13794 tree const type = TREE_TYPE (arg);
13795 tree result = NULL_TREE;
13797 STRIP_NOPS (arg);
13799 /* To proceed, MPFR must exactly represent the target floating point
13800 format, which only happens when the target base equals two. */
13801 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13802 && TREE_CODE (arg) == REAL_CST
13803 && !TREE_OVERFLOW (arg))
13805 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13807 if (real_isfinite (ra))
13809 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13810 const int prec = fmt->p;
13811 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13812 tree result_s, result_c;
13813 int inexact;
13814 mpfr_t m, ms, mc;
13816 mpfr_inits2 (prec, m, ms, mc, NULL);
13817 mpfr_from_real (m, ra, GMP_RNDN);
13818 mpfr_clear_flags ();
13819 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13820 result_s = do_mpfr_ckconv (ms, type, inexact);
13821 result_c = do_mpfr_ckconv (mc, type, inexact);
13822 mpfr_clears (m, ms, mc, NULL);
13823 if (result_s && result_c)
13825 /* If we are to return in a complex value do so. */
13826 if (!arg_sinp && !arg_cosp)
13827 return build_complex (build_complex_type (type),
13828 result_c, result_s);
13830 /* Dereference the sin/cos pointer arguments. */
13831 arg_sinp = build_fold_indirect_ref (arg_sinp);
13832 arg_cosp = build_fold_indirect_ref (arg_cosp);
13833 /* Proceed if valid pointer type were passed in. */
13834 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13835 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13837 /* Set the values. */
13838 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13839 result_s);
13840 TREE_SIDE_EFFECTS (result_s) = 1;
13841 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13842 result_c);
13843 TREE_SIDE_EFFECTS (result_c) = 1;
13844 /* Combine the assignments into a compound expr. */
13845 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13846 result_s, result_c));
13851 return result;
13854 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13855 two-argument mpfr order N Bessel function FUNC on them and return
13856 the resulting value as a tree with type TYPE. The mpfr precision
13857 is set to the precision of TYPE. We assume that function FUNC
13858 returns zero if the result could be calculated exactly within the
13859 requested precision. */
13860 static tree
13861 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13862 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13863 const REAL_VALUE_TYPE *min, bool inclusive)
13865 tree result = NULL_TREE;
13867 STRIP_NOPS (arg1);
13868 STRIP_NOPS (arg2);
13870 /* To proceed, MPFR must exactly represent the target floating point
13871 format, which only happens when the target base equals two. */
13872 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13873 && host_integerp (arg1, 0)
13874 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13876 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13877 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13879 if (n == (long)n
13880 && real_isfinite (ra)
13881 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13883 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13884 const int prec = fmt->p;
13885 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13886 int inexact;
13887 mpfr_t m;
13889 mpfr_init2 (m, prec);
13890 mpfr_from_real (m, ra, GMP_RNDN);
13891 mpfr_clear_flags ();
13892 inexact = func (m, n, m, rnd);
13893 result = do_mpfr_ckconv (m, type, inexact);
13894 mpfr_clear (m);
13898 return result;
13901 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13902 the pointer *(ARG_QUO) and return the result. The type is taken
13903 from the type of ARG0 and is used for setting the precision of the
13904 calculation and results. */
13906 static tree
13907 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13909 tree const type = TREE_TYPE (arg0);
13910 tree result = NULL_TREE;
13912 STRIP_NOPS (arg0);
13913 STRIP_NOPS (arg1);
13915 /* To proceed, MPFR must exactly represent the target floating point
13916 format, which only happens when the target base equals two. */
13917 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13918 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13919 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13921 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13922 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13924 if (real_isfinite (ra0) && real_isfinite (ra1))
13926 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13927 const int prec = fmt->p;
13928 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13929 tree result_rem;
13930 long integer_quo;
13931 mpfr_t m0, m1;
13933 mpfr_inits2 (prec, m0, m1, NULL);
13934 mpfr_from_real (m0, ra0, GMP_RNDN);
13935 mpfr_from_real (m1, ra1, GMP_RNDN);
13936 mpfr_clear_flags ();
13937 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13938 /* Remquo is independent of the rounding mode, so pass
13939 inexact=0 to do_mpfr_ckconv(). */
13940 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13941 mpfr_clears (m0, m1, NULL);
13942 if (result_rem)
13944 /* MPFR calculates quo in the host's long so it may
13945 return more bits in quo than the target int can hold
13946 if sizeof(host long) > sizeof(target int). This can
13947 happen even for native compilers in LP64 mode. In
13948 these cases, modulo the quo value with the largest
13949 number that the target int can hold while leaving one
13950 bit for the sign. */
13951 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13952 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13954 /* Dereference the quo pointer argument. */
13955 arg_quo = build_fold_indirect_ref (arg_quo);
13956 /* Proceed iff a valid pointer type was passed in. */
13957 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13959 /* Set the value. */
13960 tree result_quo
13961 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13962 build_int_cst (TREE_TYPE (arg_quo),
13963 integer_quo));
13964 TREE_SIDE_EFFECTS (result_quo) = 1;
13965 /* Combine the quo assignment with the rem. */
13966 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13967 result_quo, result_rem));
13972 return result;
13975 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13976 resulting value as a tree with type TYPE. The mpfr precision is
13977 set to the precision of TYPE. We assume that this mpfr function
13978 returns zero if the result could be calculated exactly within the
13979 requested precision. In addition, the integer pointer represented
13980 by ARG_SG will be dereferenced and set to the appropriate signgam
13981 (-1,1) value. */
13983 static tree
13984 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13986 tree result = NULL_TREE;
13988 STRIP_NOPS (arg);
13990 /* To proceed, MPFR must exactly represent the target floating point
13991 format, which only happens when the target base equals two. Also
13992 verify ARG is a constant and that ARG_SG is an int pointer. */
13993 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13994 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13995 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13996 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13998 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
14000 /* In addition to NaN and Inf, the argument cannot be zero or a
14001 negative integer. */
14002 if (real_isfinite (ra)
14003 && ra->cl != rvc_zero
14004 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
14006 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14007 const int prec = fmt->p;
14008 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14009 int inexact, sg;
14010 mpfr_t m;
14011 tree result_lg;
14013 mpfr_init2 (m, prec);
14014 mpfr_from_real (m, ra, GMP_RNDN);
14015 mpfr_clear_flags ();
14016 inexact = mpfr_lgamma (m, &sg, m, rnd);
14017 result_lg = do_mpfr_ckconv (m, type, inexact);
14018 mpfr_clear (m);
14019 if (result_lg)
14021 tree result_sg;
14023 /* Dereference the arg_sg pointer argument. */
14024 arg_sg = build_fold_indirect_ref (arg_sg);
14025 /* Assign the signgam value into *arg_sg. */
14026 result_sg = fold_build2 (MODIFY_EXPR,
14027 TREE_TYPE (arg_sg), arg_sg,
14028 build_int_cst (TREE_TYPE (arg_sg), sg));
14029 TREE_SIDE_EFFECTS (result_sg) = 1;
14030 /* Combine the signgam assignment with the lgamma result. */
14031 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14032 result_sg, result_lg));
14037 return result;
14040 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14041 function FUNC on it and return the resulting value as a tree with
14042 type TYPE. The mpfr precision is set to the precision of TYPE. We
14043 assume that function FUNC returns zero if the result could be
14044 calculated exactly within the requested precision. */
14046 static tree
14047 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14049 tree result = NULL_TREE;
14051 STRIP_NOPS (arg);
14053 /* To proceed, MPFR must exactly represent the target floating point
14054 format, which only happens when the target base equals two. */
14055 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14056 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14057 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14059 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14060 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14062 if (real_isfinite (re) && real_isfinite (im))
14064 const struct real_format *const fmt =
14065 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14066 const int prec = fmt->p;
14067 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14068 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14069 int inexact;
14070 mpc_t m;
14072 mpc_init2 (m, prec);
14073 mpfr_from_real (mpc_realref(m), re, rnd);
14074 mpfr_from_real (mpc_imagref(m), im, rnd);
14075 mpfr_clear_flags ();
14076 inexact = func (m, m, crnd);
14077 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14078 mpc_clear (m);
14082 return result;
14085 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14086 mpc function FUNC on it and return the resulting value as a tree
14087 with type TYPE. The mpfr precision is set to the precision of
14088 TYPE. We assume that function FUNC returns zero if the result
14089 could be calculated exactly within the requested precision. If
14090 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14091 in the arguments and/or results. */
14093 tree
14094 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14095 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14097 tree result = NULL_TREE;
14099 STRIP_NOPS (arg0);
14100 STRIP_NOPS (arg1);
14102 /* To proceed, MPFR must exactly represent the target floating point
14103 format, which only happens when the target base equals two. */
14104 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14105 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14106 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14107 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14108 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14110 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14111 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14112 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14113 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14115 if (do_nonfinite
14116 || (real_isfinite (re0) && real_isfinite (im0)
14117 && real_isfinite (re1) && real_isfinite (im1)))
14119 const struct real_format *const fmt =
14120 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14121 const int prec = fmt->p;
14122 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14123 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14124 int inexact;
14125 mpc_t m0, m1;
14127 mpc_init2 (m0, prec);
14128 mpc_init2 (m1, prec);
14129 mpfr_from_real (mpc_realref(m0), re0, rnd);
14130 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14131 mpfr_from_real (mpc_realref(m1), re1, rnd);
14132 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14133 mpfr_clear_flags ();
14134 inexact = func (m0, m0, m1, crnd);
14135 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14136 mpc_clear (m0);
14137 mpc_clear (m1);
14141 return result;
14144 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14145 a normal call should be emitted rather than expanding the function
14146 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14148 static tree
14149 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14151 int nargs = gimple_call_num_args (stmt);
14153 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14154 (nargs > 0
14155 ? gimple_call_arg_ptr (stmt, 0)
14156 : &error_mark_node), fcode);
14159 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14160 a normal call should be emitted rather than expanding the function
14161 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14162 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14163 passed as second argument. */
14165 tree
14166 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14167 enum built_in_function fcode)
14169 int nargs = gimple_call_num_args (stmt);
14171 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14172 (nargs > 0
14173 ? gimple_call_arg_ptr (stmt, 0)
14174 : &error_mark_node), maxlen, fcode);
14177 /* Builtins with folding operations that operate on "..." arguments
14178 need special handling; we need to store the arguments in a convenient
14179 data structure before attempting any folding. Fortunately there are
14180 only a few builtins that fall into this category. FNDECL is the
14181 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14182 result of the function call is ignored. */
14184 static tree
14185 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14186 bool ignore ATTRIBUTE_UNUSED)
14188 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14189 tree ret = NULL_TREE;
14191 switch (fcode)
14193 case BUILT_IN_SPRINTF_CHK:
14194 case BUILT_IN_VSPRINTF_CHK:
14195 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14196 break;
14198 case BUILT_IN_SNPRINTF_CHK:
14199 case BUILT_IN_VSNPRINTF_CHK:
14200 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14202 default:
14203 break;
14205 if (ret)
14207 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14208 TREE_NO_WARNING (ret) = 1;
14209 return ret;
14211 return NULL_TREE;
14214 /* A wrapper function for builtin folding that prevents warnings for
14215 "statement without effect" and the like, caused by removing the
14216 call node earlier than the warning is generated. */
14218 tree
14219 fold_call_stmt (gimple stmt, bool ignore)
14221 tree ret = NULL_TREE;
14222 tree fndecl = gimple_call_fndecl (stmt);
14223 location_t loc = gimple_location (stmt);
14224 if (fndecl
14225 && TREE_CODE (fndecl) == FUNCTION_DECL
14226 && DECL_BUILT_IN (fndecl)
14227 && !gimple_call_va_arg_pack_p (stmt))
14229 int nargs = gimple_call_num_args (stmt);
14230 tree *args = (nargs > 0
14231 ? gimple_call_arg_ptr (stmt, 0)
14232 : &error_mark_node);
14234 if (avoid_folding_inline_builtin (fndecl))
14235 return NULL_TREE;
14236 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14238 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14240 else
14242 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14243 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14244 if (!ret)
14245 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14246 if (ret)
14248 /* Propagate location information from original call to
14249 expansion of builtin. Otherwise things like
14250 maybe_emit_chk_warning, that operate on the expansion
14251 of a builtin, will use the wrong location information. */
14252 if (gimple_has_location (stmt))
14254 tree realret = ret;
14255 if (TREE_CODE (ret) == NOP_EXPR)
14256 realret = TREE_OPERAND (ret, 0);
14257 if (CAN_HAVE_LOCATION_P (realret)
14258 && !EXPR_HAS_LOCATION (realret))
14259 SET_EXPR_LOCATION (realret, loc);
14260 return realret;
14262 return ret;
14266 return NULL_TREE;
14269 /* Look up the function in builtin_decl that corresponds to DECL
14270 and set ASMSPEC as its user assembler name. DECL must be a
14271 function decl that declares a builtin. */
14273 void
14274 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14276 tree builtin;
14277 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14278 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14279 && asmspec != 0);
14281 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14282 set_user_assembler_name (builtin, asmspec);
14283 switch (DECL_FUNCTION_CODE (decl))
14285 case BUILT_IN_MEMCPY:
14286 init_block_move_fn (asmspec);
14287 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14288 break;
14289 case BUILT_IN_MEMSET:
14290 init_block_clear_fn (asmspec);
14291 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14292 break;
14293 case BUILT_IN_MEMMOVE:
14294 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14295 break;
14296 case BUILT_IN_MEMCMP:
14297 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14298 break;
14299 case BUILT_IN_ABORT:
14300 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14301 break;
14302 case BUILT_IN_FFS:
14303 if (INT_TYPE_SIZE < BITS_PER_WORD)
14305 set_user_assembler_libfunc ("ffs", asmspec);
14306 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14307 MODE_INT, 0), "ffs");
14309 break;
14310 default:
14311 break;
14315 /* Return true if DECL is a builtin that expands to a constant or similarly
14316 simple code. */
14317 bool
14318 is_simple_builtin (tree decl)
14320 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14321 switch (DECL_FUNCTION_CODE (decl))
14323 /* Builtins that expand to constants. */
14324 case BUILT_IN_CONSTANT_P:
14325 case BUILT_IN_EXPECT:
14326 case BUILT_IN_OBJECT_SIZE:
14327 case BUILT_IN_UNREACHABLE:
14328 /* Simple register moves or loads from stack. */
14329 case BUILT_IN_ASSUME_ALIGNED:
14330 case BUILT_IN_RETURN_ADDRESS:
14331 case BUILT_IN_EXTRACT_RETURN_ADDR:
14332 case BUILT_IN_FROB_RETURN_ADDR:
14333 case BUILT_IN_RETURN:
14334 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14335 case BUILT_IN_FRAME_ADDRESS:
14336 case BUILT_IN_VA_END:
14337 case BUILT_IN_STACK_SAVE:
14338 case BUILT_IN_STACK_RESTORE:
14339 /* Exception state returns or moves registers around. */
14340 case BUILT_IN_EH_FILTER:
14341 case BUILT_IN_EH_POINTER:
14342 case BUILT_IN_EH_COPY_VALUES:
14343 return true;
14345 default:
14346 return false;
14349 return false;
14352 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14353 most probably expanded inline into reasonably simple code. This is a
14354 superset of is_simple_builtin. */
14355 bool
14356 is_inexpensive_builtin (tree decl)
14358 if (!decl)
14359 return false;
14360 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14361 return true;
14362 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14363 switch (DECL_FUNCTION_CODE (decl))
14365 case BUILT_IN_ABS:
14366 case BUILT_IN_ALLOCA:
14367 case BUILT_IN_ALLOCA_WITH_ALIGN:
14368 case BUILT_IN_BSWAP16:
14369 case BUILT_IN_BSWAP32:
14370 case BUILT_IN_BSWAP64:
14371 case BUILT_IN_CLZ:
14372 case BUILT_IN_CLZIMAX:
14373 case BUILT_IN_CLZL:
14374 case BUILT_IN_CLZLL:
14375 case BUILT_IN_CTZ:
14376 case BUILT_IN_CTZIMAX:
14377 case BUILT_IN_CTZL:
14378 case BUILT_IN_CTZLL:
14379 case BUILT_IN_FFS:
14380 case BUILT_IN_FFSIMAX:
14381 case BUILT_IN_FFSL:
14382 case BUILT_IN_FFSLL:
14383 case BUILT_IN_IMAXABS:
14384 case BUILT_IN_FINITE:
14385 case BUILT_IN_FINITEF:
14386 case BUILT_IN_FINITEL:
14387 case BUILT_IN_FINITED32:
14388 case BUILT_IN_FINITED64:
14389 case BUILT_IN_FINITED128:
14390 case BUILT_IN_FPCLASSIFY:
14391 case BUILT_IN_ISFINITE:
14392 case BUILT_IN_ISINF_SIGN:
14393 case BUILT_IN_ISINF:
14394 case BUILT_IN_ISINFF:
14395 case BUILT_IN_ISINFL:
14396 case BUILT_IN_ISINFD32:
14397 case BUILT_IN_ISINFD64:
14398 case BUILT_IN_ISINFD128:
14399 case BUILT_IN_ISNAN:
14400 case BUILT_IN_ISNANF:
14401 case BUILT_IN_ISNANL:
14402 case BUILT_IN_ISNAND32:
14403 case BUILT_IN_ISNAND64:
14404 case BUILT_IN_ISNAND128:
14405 case BUILT_IN_ISNORMAL:
14406 case BUILT_IN_ISGREATER:
14407 case BUILT_IN_ISGREATEREQUAL:
14408 case BUILT_IN_ISLESS:
14409 case BUILT_IN_ISLESSEQUAL:
14410 case BUILT_IN_ISLESSGREATER:
14411 case BUILT_IN_ISUNORDERED:
14412 case BUILT_IN_VA_ARG_PACK:
14413 case BUILT_IN_VA_ARG_PACK_LEN:
14414 case BUILT_IN_VA_COPY:
14415 case BUILT_IN_TRAP:
14416 case BUILT_IN_SAVEREGS:
14417 case BUILT_IN_POPCOUNTL:
14418 case BUILT_IN_POPCOUNTLL:
14419 case BUILT_IN_POPCOUNTIMAX:
14420 case BUILT_IN_POPCOUNT:
14421 case BUILT_IN_PARITYL:
14422 case BUILT_IN_PARITYLL:
14423 case BUILT_IN_PARITYIMAX:
14424 case BUILT_IN_PARITY:
14425 case BUILT_IN_LABS:
14426 case BUILT_IN_LLABS:
14427 case BUILT_IN_PREFETCH:
14428 return true;
14430 default:
14431 return is_simple_builtin (decl);
14434 return false;