Daily bump.
[official-gcc.git] / gcc / builtins.c
blob1fbd2f32eddf80ccf4fe8cefa1dcbbac88aa235a
1 /* Expand builtin functions.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "realmpfr.h"
28 #include "gimple.h"
29 #include "flags.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "except.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "libfuncs.h"
38 #include "recog.h"
39 #include "output.h"
40 #include "typeclass.h"
41 #include "predict.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "langhooks.h"
45 #include "basic-block.h"
46 #include "tree-mudflap.h"
47 #include "tree-flow.h"
48 #include "value-prof.h"
49 #include "diagnostic-core.h"
50 #include "builtins.h"
53 #ifndef PAD_VARARGS_DOWN
54 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 #endif
56 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
58 struct target_builtins default_target_builtins;
59 #if SWITCHABLE_TARGET
60 struct target_builtins *this_target_builtins = &default_target_builtins;
61 #endif
63 /* Define the names of the builtin function types and codes. */
64 const char *const built_in_class_names[4]
65 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
67 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
68 const char * built_in_names[(int) END_BUILTINS] =
70 #include "builtins.def"
72 #undef DEF_BUILTIN
74 /* Setup an array of _DECL trees, make sure each element is
75 initialized to NULL_TREE. */
76 builtin_info_type builtin_info;
78 /* Non-zero if __builtin_constant_p should be folded right away. */
79 bool force_folding_builtin_constant_p;
81 static const char *c_getstr (tree);
82 static rtx c_readstr (const char *, enum machine_mode);
83 static int target_char_cast (tree, char *);
84 static rtx get_memory_rtx (tree, tree);
85 static int apply_args_size (void);
86 static int apply_result_size (void);
87 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
88 static rtx result_vector (int, rtx);
89 #endif
90 static void expand_builtin_update_setjmp_buf (rtx);
91 static void expand_builtin_prefetch (tree);
92 static rtx expand_builtin_apply_args (void);
93 static rtx expand_builtin_apply_args_1 (void);
94 static rtx expand_builtin_apply (rtx, rtx, rtx);
95 static void expand_builtin_return (rtx);
96 static enum type_class type_to_class (tree);
97 static rtx expand_builtin_classify_type (tree);
98 static void expand_errno_check (tree, rtx);
99 static rtx expand_builtin_mathfn (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
102 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
103 static rtx expand_builtin_interclass_mathfn (tree, rtx);
104 static rtx expand_builtin_sincos (tree);
105 static rtx expand_builtin_cexpi (tree, rtx);
106 static rtx expand_builtin_int_roundingfn (tree, rtx);
107 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
108 static rtx expand_builtin_next_arg (void);
109 static rtx expand_builtin_va_start (tree);
110 static rtx expand_builtin_va_end (tree);
111 static rtx expand_builtin_va_copy (tree);
112 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcmp (tree, rtx);
114 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
115 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
116 static rtx expand_builtin_memcpy (tree, rtx);
117 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
119 enum machine_mode, int);
120 static rtx expand_builtin_strcpy (tree, rtx);
121 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
122 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
123 static rtx expand_builtin_strncpy (tree, rtx);
124 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
125 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
127 static rtx expand_builtin_bzero (tree);
128 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, bool);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static tree stabilize_va_list_loc (location_t, tree, int);
133 static rtx expand_builtin_expect (tree, rtx);
134 static tree fold_builtin_constant_p (tree);
135 static tree fold_builtin_expect (location_t, tree, tree);
136 static tree fold_builtin_classify_type (tree);
137 static tree fold_builtin_strlen (location_t, tree, tree);
138 static tree fold_builtin_inf (location_t, tree, int);
139 static tree fold_builtin_nan (tree, tree, int);
140 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
141 static bool validate_arg (const_tree, enum tree_code code);
142 static bool integer_valued_real_p (tree);
143 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
144 static bool readonly_data_expr (tree);
145 static rtx expand_builtin_fabs (tree, rtx, rtx);
146 static rtx expand_builtin_signbit (tree, rtx);
147 static tree fold_builtin_sqrt (location_t, tree, tree);
148 static tree fold_builtin_cbrt (location_t, tree, tree);
149 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
151 static tree fold_builtin_cos (location_t, tree, tree, tree);
152 static tree fold_builtin_cosh (location_t, tree, tree, tree);
153 static tree fold_builtin_tan (tree, tree);
154 static tree fold_builtin_trunc (location_t, tree, tree);
155 static tree fold_builtin_floor (location_t, tree, tree);
156 static tree fold_builtin_ceil (location_t, tree, tree);
157 static tree fold_builtin_round (location_t, tree, tree);
158 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
159 static tree fold_builtin_bitop (tree, tree);
160 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
161 static tree fold_builtin_strchr (location_t, tree, tree, tree);
162 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
163 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
164 static tree fold_builtin_strcmp (location_t, tree, tree);
165 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
166 static tree fold_builtin_signbit (location_t, tree, tree);
167 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
168 static tree fold_builtin_isascii (location_t, tree);
169 static tree fold_builtin_toascii (location_t, tree);
170 static tree fold_builtin_isdigit (location_t, tree);
171 static tree fold_builtin_fabs (location_t, tree, tree);
172 static tree fold_builtin_abs (location_t, tree, tree);
173 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
174 enum tree_code);
175 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
176 static tree fold_builtin_0 (location_t, tree, bool);
177 static tree fold_builtin_1 (location_t, tree, tree, bool);
178 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
179 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
180 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
181 static tree fold_builtin_varargs (location_t, tree, tree, bool);
183 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
184 static tree fold_builtin_strstr (location_t, tree, tree, tree);
185 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
186 static tree fold_builtin_strcat (location_t, tree, tree);
187 static tree fold_builtin_strncat (location_t, tree, tree, tree);
188 static tree fold_builtin_strspn (location_t, tree, tree);
189 static tree fold_builtin_strcspn (location_t, tree, tree);
190 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
191 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
193 static rtx expand_builtin_object_size (tree);
194 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
195 enum built_in_function);
196 static void maybe_emit_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
198 static void maybe_emit_free_warning (tree);
199 static tree fold_builtin_object_size (tree, tree);
200 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
201 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
202 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
203 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
204 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
205 enum built_in_function);
206 static bool init_target_chars (void);
208 static unsigned HOST_WIDE_INT target_newline;
209 static unsigned HOST_WIDE_INT target_percent;
210 static unsigned HOST_WIDE_INT target_c;
211 static unsigned HOST_WIDE_INT target_s;
212 static char target_percent_c[3];
213 static char target_percent_s[3];
214 static char target_percent_s_newline[4];
215 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
216 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
217 static tree do_mpfr_arg2 (tree, tree, tree,
218 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
219 static tree do_mpfr_arg3 (tree, tree, tree, tree,
220 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
221 static tree do_mpfr_sincos (tree, tree, tree);
222 static tree do_mpfr_bessel_n (tree, tree, tree,
223 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
224 const REAL_VALUE_TYPE *, bool);
225 static tree do_mpfr_remquo (tree, tree, tree);
226 static tree do_mpfr_lgamma_r (tree, tree, tree);
227 static void expand_builtin_sync_synchronize (void);
229 /* Return true if NAME starts with __builtin_ or __sync_. */
231 static bool
232 is_builtin_name (const char *name)
234 if (strncmp (name, "__builtin_", 10) == 0)
235 return true;
236 if (strncmp (name, "__sync_", 7) == 0)
237 return true;
238 if (strncmp (name, "__atomic_", 9) == 0)
239 return true;
240 return false;
244 /* Return true if DECL is a function symbol representing a built-in. */
246 bool
247 is_builtin_fn (tree decl)
249 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
253 /* Return true if NODE should be considered for inline expansion regardless
254 of the optimization level. This means whenever a function is invoked with
255 its "internal" name, which normally contains the prefix "__builtin". */
257 static bool
258 called_as_built_in (tree node)
260 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
261 we want the name used to call the function, not the name it
262 will have. */
263 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
264 return is_builtin_name (name);
267 /* Compute values M and N such that M divides (address of EXP - N) and such
268 that N < M. If these numbers can be determined, store M in alignp and N in
269 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
270 *alignp and any bit-offset to *bitposp.
272 Note that the address (and thus the alignment) computed here is based
273 on the address to which a symbol resolves, whereas DECL_ALIGN is based
274 on the address at which an object is actually located. These two
275 addresses are not always the same. For example, on ARM targets,
276 the address &foo of a Thumb function foo() has the lowest bit set,
277 whereas foo() itself starts on an even address.
279 If ADDR_P is true we are taking the address of the memory reference EXP
280 and thus cannot rely on the access taking place. */
282 static bool
283 get_object_alignment_2 (tree exp, unsigned int *alignp,
284 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
286 HOST_WIDE_INT bitsize, bitpos;
287 tree offset;
288 enum machine_mode mode;
289 int unsignedp, volatilep;
290 unsigned int inner, align = BITS_PER_UNIT;
291 bool known_alignment = false;
293 /* Get the innermost object and the constant (bitpos) and possibly
294 variable (offset) offset of the access. */
295 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
296 &mode, &unsignedp, &volatilep, true);
298 /* Extract alignment information from the innermost object and
299 possibly adjust bitpos and offset. */
300 if (TREE_CODE (exp) == FUNCTION_DECL)
302 /* Function addresses can encode extra information besides their
303 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
304 allows the low bit to be used as a virtual bit, we know
305 that the address itself must be at least 2-byte aligned. */
306 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
307 align = 2 * BITS_PER_UNIT;
309 else if (TREE_CODE (exp) == LABEL_DECL)
311 else if (TREE_CODE (exp) == CONST_DECL)
313 /* The alignment of a CONST_DECL is determined by its initializer. */
314 exp = DECL_INITIAL (exp);
315 align = TYPE_ALIGN (TREE_TYPE (exp));
316 #ifdef CONSTANT_ALIGNMENT
317 if (CONSTANT_CLASS_P (exp))
318 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
319 #endif
320 known_alignment = true;
322 else if (DECL_P (exp))
324 align = DECL_ALIGN (exp);
325 known_alignment = true;
327 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
329 align = TYPE_ALIGN (TREE_TYPE (exp));
331 else if (TREE_CODE (exp) == INDIRECT_REF
332 || TREE_CODE (exp) == MEM_REF
333 || TREE_CODE (exp) == TARGET_MEM_REF)
335 tree addr = TREE_OPERAND (exp, 0);
336 unsigned ptr_align;
337 unsigned HOST_WIDE_INT ptr_bitpos;
339 if (TREE_CODE (addr) == BIT_AND_EXPR
340 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
342 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
343 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
344 align *= BITS_PER_UNIT;
345 addr = TREE_OPERAND (addr, 0);
348 known_alignment
349 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
350 align = MAX (ptr_align, align);
352 /* The alignment of the pointer operand in a TARGET_MEM_REF
353 has to take the variable offset parts into account. */
354 if (TREE_CODE (exp) == TARGET_MEM_REF)
356 if (TMR_INDEX (exp))
358 unsigned HOST_WIDE_INT step = 1;
359 if (TMR_STEP (exp))
360 step = TREE_INT_CST_LOW (TMR_STEP (exp));
361 align = MIN (align, (step & -step) * BITS_PER_UNIT);
363 if (TMR_INDEX2 (exp))
364 align = BITS_PER_UNIT;
365 known_alignment = false;
368 /* When EXP is an actual memory reference then we can use
369 TYPE_ALIGN of a pointer indirection to derive alignment.
370 Do so only if get_pointer_alignment_1 did not reveal absolute
371 alignment knowledge and if using that alignment would
372 improve the situation. */
373 if (!addr_p && !known_alignment
374 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
375 align = TYPE_ALIGN (TREE_TYPE (exp));
376 else
378 /* Else adjust bitpos accordingly. */
379 bitpos += ptr_bitpos;
380 if (TREE_CODE (exp) == MEM_REF
381 || TREE_CODE (exp) == TARGET_MEM_REF)
382 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
385 else if (TREE_CODE (exp) == STRING_CST)
387 /* STRING_CST are the only constant objects we allow to be not
388 wrapped inside a CONST_DECL. */
389 align = TYPE_ALIGN (TREE_TYPE (exp));
390 #ifdef CONSTANT_ALIGNMENT
391 if (CONSTANT_CLASS_P (exp))
392 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
393 #endif
394 known_alignment = true;
397 /* If there is a non-constant offset part extract the maximum
398 alignment that can prevail. */
399 inner = ~0U;
400 while (offset)
402 tree next_offset;
404 if (TREE_CODE (offset) == PLUS_EXPR)
406 next_offset = TREE_OPERAND (offset, 0);
407 offset = TREE_OPERAND (offset, 1);
409 else
410 next_offset = NULL;
411 if (host_integerp (offset, 1))
413 /* Any overflow in calculating offset_bits won't change
414 the alignment. */
415 unsigned offset_bits
416 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
418 if (offset_bits)
419 inner = MIN (inner, (offset_bits & -offset_bits));
421 else if (TREE_CODE (offset) == MULT_EXPR
422 && host_integerp (TREE_OPERAND (offset, 1), 1))
424 /* Any overflow in calculating offset_factor won't change
425 the alignment. */
426 unsigned offset_factor
427 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
428 * BITS_PER_UNIT);
430 if (offset_factor)
431 inner = MIN (inner, (offset_factor & -offset_factor));
433 else
435 inner = MIN (inner, BITS_PER_UNIT);
436 break;
438 offset = next_offset;
440 /* Alignment is innermost object alignment adjusted by the constant
441 and non-constant offset parts. */
442 align = MIN (align, inner);
444 *alignp = align;
445 *bitposp = bitpos & (*alignp - 1);
446 return known_alignment;
449 /* For a memory reference expression EXP compute values M and N such that M
450 divides (&EXP - N) and such that N < M. If these numbers can be determined,
451 store M in alignp and N in *BITPOSP and return true. Otherwise return false
452 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
454 bool
455 get_object_alignment_1 (tree exp, unsigned int *alignp,
456 unsigned HOST_WIDE_INT *bitposp)
458 return get_object_alignment_2 (exp, alignp, bitposp, false);
461 /* Return the alignment in bits of EXP, an object. */
463 unsigned int
464 get_object_alignment (tree exp)
466 unsigned HOST_WIDE_INT bitpos = 0;
467 unsigned int align;
469 get_object_alignment_1 (exp, &align, &bitpos);
471 /* align and bitpos now specify known low bits of the pointer.
472 ptr & (align - 1) == bitpos. */
474 if (bitpos != 0)
475 align = (bitpos & -bitpos);
476 return align;
479 /* For a pointer valued expression EXP compute values M and N such that M
480 divides (EXP - N) and such that N < M. If these numbers can be determined,
481 store M in alignp and N in *BITPOSP and return true. Return false if
482 the results are just a conservative approximation.
484 If EXP is not a pointer, false is returned too. */
486 bool
487 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
488 unsigned HOST_WIDE_INT *bitposp)
490 STRIP_NOPS (exp);
492 if (TREE_CODE (exp) == ADDR_EXPR)
493 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
494 alignp, bitposp, true);
495 else if (TREE_CODE (exp) == SSA_NAME
496 && POINTER_TYPE_P (TREE_TYPE (exp)))
498 unsigned int ptr_align, ptr_misalign;
499 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
501 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
503 *bitposp = ptr_misalign * BITS_PER_UNIT;
504 *alignp = ptr_align * BITS_PER_UNIT;
505 /* We cannot really tell whether this result is an approximation. */
506 return true;
508 else
510 *bitposp = 0;
511 *alignp = BITS_PER_UNIT;
512 return false;
515 else if (TREE_CODE (exp) == INTEGER_CST)
517 *alignp = BIGGEST_ALIGNMENT;
518 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
519 & (BIGGEST_ALIGNMENT - 1));
520 return true;
523 *bitposp = 0;
524 *alignp = BITS_PER_UNIT;
525 return false;
528 /* Return the alignment in bits of EXP, a pointer valued expression.
529 The alignment returned is, by default, the alignment of the thing that
530 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
532 Otherwise, look at the expression to see if we can do better, i.e., if the
533 expression is actually pointing at an object whose alignment is tighter. */
535 unsigned int
536 get_pointer_alignment (tree exp)
538 unsigned HOST_WIDE_INT bitpos = 0;
539 unsigned int align;
541 get_pointer_alignment_1 (exp, &align, &bitpos);
543 /* align and bitpos now specify known low bits of the pointer.
544 ptr & (align - 1) == bitpos. */
546 if (bitpos != 0)
547 align = (bitpos & -bitpos);
549 return align;
552 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
553 way, because it could contain a zero byte in the middle.
554 TREE_STRING_LENGTH is the size of the character array, not the string.
556 ONLY_VALUE should be nonzero if the result is not going to be emitted
557 into the instruction stream and zero if it is going to be expanded.
558 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
559 is returned, otherwise NULL, since
560 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
561 evaluate the side-effects.
563 The value returned is of type `ssizetype'.
565 Unfortunately, string_constant can't access the values of const char
566 arrays with initializers, so neither can we do so here. */
568 tree
569 c_strlen (tree src, int only_value)
571 tree offset_node;
572 HOST_WIDE_INT offset;
573 int max;
574 const char *ptr;
575 location_t loc;
577 STRIP_NOPS (src);
578 if (TREE_CODE (src) == COND_EXPR
579 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
581 tree len1, len2;
583 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
584 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
585 if (tree_int_cst_equal (len1, len2))
586 return len1;
589 if (TREE_CODE (src) == COMPOUND_EXPR
590 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
591 return c_strlen (TREE_OPERAND (src, 1), only_value);
593 loc = EXPR_LOC_OR_HERE (src);
595 src = string_constant (src, &offset_node);
596 if (src == 0)
597 return NULL_TREE;
599 max = TREE_STRING_LENGTH (src) - 1;
600 ptr = TREE_STRING_POINTER (src);
602 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
604 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
605 compute the offset to the following null if we don't know where to
606 start searching for it. */
607 int i;
609 for (i = 0; i < max; i++)
610 if (ptr[i] == 0)
611 return NULL_TREE;
613 /* We don't know the starting offset, but we do know that the string
614 has no internal zero bytes. We can assume that the offset falls
615 within the bounds of the string; otherwise, the programmer deserves
616 what he gets. Subtract the offset from the length of the string,
617 and return that. This would perhaps not be valid if we were dealing
618 with named arrays in addition to literal string constants. */
620 return size_diffop_loc (loc, size_int (max), offset_node);
623 /* We have a known offset into the string. Start searching there for
624 a null character if we can represent it as a single HOST_WIDE_INT. */
625 if (offset_node == 0)
626 offset = 0;
627 else if (! host_integerp (offset_node, 0))
628 offset = -1;
629 else
630 offset = tree_low_cst (offset_node, 0);
632 /* If the offset is known to be out of bounds, warn, and call strlen at
633 runtime. */
634 if (offset < 0 || offset > max)
636 /* Suppress multiple warnings for propagated constant strings. */
637 if (! TREE_NO_WARNING (src))
639 warning_at (loc, 0, "offset outside bounds of constant string");
640 TREE_NO_WARNING (src) = 1;
642 return NULL_TREE;
645 /* Use strlen to search for the first zero byte. Since any strings
646 constructed with build_string will have nulls appended, we win even
647 if we get handed something like (char[4])"abcd".
649 Since OFFSET is our starting index into the string, no further
650 calculation is needed. */
651 return ssize_int (strlen (ptr + offset));
654 /* Return a char pointer for a C string if it is a string constant
655 or sum of string constant and integer constant. */
657 static const char *
658 c_getstr (tree src)
660 tree offset_node;
662 src = string_constant (src, &offset_node);
663 if (src == 0)
664 return 0;
666 if (offset_node == 0)
667 return TREE_STRING_POINTER (src);
668 else if (!host_integerp (offset_node, 1)
669 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
670 return 0;
672 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
675 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
676 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
678 static rtx
679 c_readstr (const char *str, enum machine_mode mode)
681 HOST_WIDE_INT c[2];
682 HOST_WIDE_INT ch;
683 unsigned int i, j;
685 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
687 c[0] = 0;
688 c[1] = 0;
689 ch = 1;
690 for (i = 0; i < GET_MODE_SIZE (mode); i++)
692 j = i;
693 if (WORDS_BIG_ENDIAN)
694 j = GET_MODE_SIZE (mode) - i - 1;
695 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
696 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
697 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
698 j *= BITS_PER_UNIT;
699 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
701 if (ch)
702 ch = (unsigned char) str[i];
703 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
705 return immed_double_const (c[0], c[1], mode);
708 /* Cast a target constant CST to target CHAR and if that value fits into
709 host char type, return zero and put that value into variable pointed to by
710 P. */
712 static int
713 target_char_cast (tree cst, char *p)
715 unsigned HOST_WIDE_INT val, hostval;
717 if (TREE_CODE (cst) != INTEGER_CST
718 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
719 return 1;
721 val = TREE_INT_CST_LOW (cst);
722 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
723 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
725 hostval = val;
726 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
727 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
729 if (val != hostval)
730 return 1;
732 *p = hostval;
733 return 0;
736 /* Similar to save_expr, but assumes that arbitrary code is not executed
737 in between the multiple evaluations. In particular, we assume that a
738 non-addressable local variable will not be modified. */
740 static tree
741 builtin_save_expr (tree exp)
743 if (TREE_CODE (exp) == SSA_NAME
744 || (TREE_ADDRESSABLE (exp) == 0
745 && (TREE_CODE (exp) == PARM_DECL
746 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
747 return exp;
749 return save_expr (exp);
752 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
753 times to get the address of either a higher stack frame, or a return
754 address located within it (depending on FNDECL_CODE). */
756 static rtx
757 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
759 int i;
761 #ifdef INITIAL_FRAME_ADDRESS_RTX
762 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
763 #else
764 rtx tem;
766 /* For a zero count with __builtin_return_address, we don't care what
767 frame address we return, because target-specific definitions will
768 override us. Therefore frame pointer elimination is OK, and using
769 the soft frame pointer is OK.
771 For a nonzero count, or a zero count with __builtin_frame_address,
772 we require a stable offset from the current frame pointer to the
773 previous one, so we must use the hard frame pointer, and
774 we must disable frame pointer elimination. */
775 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
776 tem = frame_pointer_rtx;
777 else
779 tem = hard_frame_pointer_rtx;
781 /* Tell reload not to eliminate the frame pointer. */
782 crtl->accesses_prior_frames = 1;
784 #endif
786 /* Some machines need special handling before we can access
787 arbitrary frames. For example, on the SPARC, we must first flush
788 all register windows to the stack. */
789 #ifdef SETUP_FRAME_ADDRESSES
790 if (count > 0)
791 SETUP_FRAME_ADDRESSES ();
792 #endif
794 /* On the SPARC, the return address is not in the frame, it is in a
795 register. There is no way to access it off of the current frame
796 pointer, but it can be accessed off the previous frame pointer by
797 reading the value from the register window save area. */
798 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
799 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
800 count--;
801 #endif
803 /* Scan back COUNT frames to the specified frame. */
804 for (i = 0; i < count; i++)
806 /* Assume the dynamic chain pointer is in the word that the
807 frame address points to, unless otherwise specified. */
808 #ifdef DYNAMIC_CHAIN_ADDRESS
809 tem = DYNAMIC_CHAIN_ADDRESS (tem);
810 #endif
811 tem = memory_address (Pmode, tem);
812 tem = gen_frame_mem (Pmode, tem);
813 tem = copy_to_reg (tem);
816 /* For __builtin_frame_address, return what we've got. But, on
817 the SPARC for example, we may have to add a bias. */
818 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
819 #ifdef FRAME_ADDR_RTX
820 return FRAME_ADDR_RTX (tem);
821 #else
822 return tem;
823 #endif
825 /* For __builtin_return_address, get the return address from that frame. */
826 #ifdef RETURN_ADDR_RTX
827 tem = RETURN_ADDR_RTX (count, tem);
828 #else
829 tem = memory_address (Pmode,
830 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
831 tem = gen_frame_mem (Pmode, tem);
832 #endif
833 return tem;
836 /* Alias set used for setjmp buffer. */
837 static alias_set_type setjmp_alias_set = -1;
839 /* Construct the leading half of a __builtin_setjmp call. Control will
840 return to RECEIVER_LABEL. This is also called directly by the SJLJ
841 exception handling code. */
843 void
844 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
846 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
847 rtx stack_save;
848 rtx mem;
850 if (setjmp_alias_set == -1)
851 setjmp_alias_set = new_alias_set ();
853 buf_addr = convert_memory_address (Pmode, buf_addr);
855 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
857 /* We store the frame pointer and the address of receiver_label in
858 the buffer and use the rest of it for the stack save area, which
859 is machine-dependent. */
861 mem = gen_rtx_MEM (Pmode, buf_addr);
862 set_mem_alias_set (mem, setjmp_alias_set);
863 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
865 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
866 GET_MODE_SIZE (Pmode))),
867 set_mem_alias_set (mem, setjmp_alias_set);
869 emit_move_insn (validize_mem (mem),
870 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
872 stack_save = gen_rtx_MEM (sa_mode,
873 plus_constant (Pmode, buf_addr,
874 2 * GET_MODE_SIZE (Pmode)));
875 set_mem_alias_set (stack_save, setjmp_alias_set);
876 emit_stack_save (SAVE_NONLOCAL, &stack_save);
878 /* If there is further processing to do, do it. */
879 #ifdef HAVE_builtin_setjmp_setup
880 if (HAVE_builtin_setjmp_setup)
881 emit_insn (gen_builtin_setjmp_setup (buf_addr));
882 #endif
884 /* We have a nonlocal label. */
885 cfun->has_nonlocal_label = 1;
888 /* Construct the trailing part of a __builtin_setjmp call. This is
889 also called directly by the SJLJ exception handling code. */
891 void
892 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
894 rtx chain;
896 /* Clobber the FP when we get here, so we have to make sure it's
897 marked as used by this function. */
898 emit_use (hard_frame_pointer_rtx);
900 /* Mark the static chain as clobbered here so life information
901 doesn't get messed up for it. */
902 chain = targetm.calls.static_chain (current_function_decl, true);
903 if (chain && REG_P (chain))
904 emit_clobber (chain);
906 /* Now put in the code to restore the frame pointer, and argument
907 pointer, if needed. */
908 #ifdef HAVE_nonlocal_goto
909 if (! HAVE_nonlocal_goto)
910 #endif
912 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
913 /* This might change the hard frame pointer in ways that aren't
914 apparent to early optimization passes, so force a clobber. */
915 emit_clobber (hard_frame_pointer_rtx);
918 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
919 if (fixed_regs[ARG_POINTER_REGNUM])
921 #ifdef ELIMINABLE_REGS
922 size_t i;
923 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
925 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
926 if (elim_regs[i].from == ARG_POINTER_REGNUM
927 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
928 break;
930 if (i == ARRAY_SIZE (elim_regs))
931 #endif
933 /* Now restore our arg pointer from the address at which it
934 was saved in our stack frame. */
935 emit_move_insn (crtl->args.internal_arg_pointer,
936 copy_to_reg (get_arg_pointer_save_area ()));
939 #endif
941 #ifdef HAVE_builtin_setjmp_receiver
942 if (HAVE_builtin_setjmp_receiver)
943 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
944 else
945 #endif
946 #ifdef HAVE_nonlocal_goto_receiver
947 if (HAVE_nonlocal_goto_receiver)
948 emit_insn (gen_nonlocal_goto_receiver ());
949 else
950 #endif
951 { /* Nothing */ }
953 /* We must not allow the code we just generated to be reordered by
954 scheduling. Specifically, the update of the frame pointer must
955 happen immediately, not later. Similarly, we must block
956 (frame-related) register values to be used across this code. */
957 emit_insn (gen_blockage ());
960 /* __builtin_longjmp is passed a pointer to an array of five words (not
961 all will be used on all machines). It operates similarly to the C
962 library function of the same name, but is more efficient. Much of
963 the code below is copied from the handling of non-local gotos. */
965 static void
966 expand_builtin_longjmp (rtx buf_addr, rtx value)
968 rtx fp, lab, stack, insn, last;
969 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
971 /* DRAP is needed for stack realign if longjmp is expanded to current
972 function */
973 if (SUPPORTS_STACK_ALIGNMENT)
974 crtl->need_drap = true;
976 if (setjmp_alias_set == -1)
977 setjmp_alias_set = new_alias_set ();
979 buf_addr = convert_memory_address (Pmode, buf_addr);
981 buf_addr = force_reg (Pmode, buf_addr);
983 /* We require that the user must pass a second argument of 1, because
984 that is what builtin_setjmp will return. */
985 gcc_assert (value == const1_rtx);
987 last = get_last_insn ();
988 #ifdef HAVE_builtin_longjmp
989 if (HAVE_builtin_longjmp)
990 emit_insn (gen_builtin_longjmp (buf_addr));
991 else
992 #endif
994 fp = gen_rtx_MEM (Pmode, buf_addr);
995 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
996 GET_MODE_SIZE (Pmode)));
998 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
999 2 * GET_MODE_SIZE (Pmode)));
1000 set_mem_alias_set (fp, setjmp_alias_set);
1001 set_mem_alias_set (lab, setjmp_alias_set);
1002 set_mem_alias_set (stack, setjmp_alias_set);
1004 /* Pick up FP, label, and SP from the block and jump. This code is
1005 from expand_goto in stmt.c; see there for detailed comments. */
1006 #ifdef HAVE_nonlocal_goto
1007 if (HAVE_nonlocal_goto)
1008 /* We have to pass a value to the nonlocal_goto pattern that will
1009 get copied into the static_chain pointer, but it does not matter
1010 what that value is, because builtin_setjmp does not use it. */
1011 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1012 else
1013 #endif
1015 lab = copy_to_reg (lab);
1017 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1018 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1020 emit_move_insn (hard_frame_pointer_rtx, fp);
1021 emit_stack_restore (SAVE_NONLOCAL, stack);
1023 emit_use (hard_frame_pointer_rtx);
1024 emit_use (stack_pointer_rtx);
1025 emit_indirect_jump (lab);
1029 /* Search backwards and mark the jump insn as a non-local goto.
1030 Note that this precludes the use of __builtin_longjmp to a
1031 __builtin_setjmp target in the same function. However, we've
1032 already cautioned the user that these functions are for
1033 internal exception handling use only. */
1034 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1036 gcc_assert (insn != last);
1038 if (JUMP_P (insn))
1040 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1041 break;
1043 else if (CALL_P (insn))
1044 break;
1048 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1049 and the address of the save area. */
1051 static rtx
1052 expand_builtin_nonlocal_goto (tree exp)
1054 tree t_label, t_save_area;
1055 rtx r_label, r_save_area, r_fp, r_sp, insn;
1057 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1058 return NULL_RTX;
1060 t_label = CALL_EXPR_ARG (exp, 0);
1061 t_save_area = CALL_EXPR_ARG (exp, 1);
1063 r_label = expand_normal (t_label);
1064 r_label = convert_memory_address (Pmode, r_label);
1065 r_save_area = expand_normal (t_save_area);
1066 r_save_area = convert_memory_address (Pmode, r_save_area);
1067 /* Copy the address of the save location to a register just in case it was
1068 based on the frame pointer. */
1069 r_save_area = copy_to_reg (r_save_area);
1070 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1071 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1072 plus_constant (Pmode, r_save_area,
1073 GET_MODE_SIZE (Pmode)));
1075 crtl->has_nonlocal_goto = 1;
1077 #ifdef HAVE_nonlocal_goto
1078 /* ??? We no longer need to pass the static chain value, afaik. */
1079 if (HAVE_nonlocal_goto)
1080 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1081 else
1082 #endif
1084 r_label = copy_to_reg (r_label);
1086 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1087 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1089 /* Restore frame pointer for containing function. */
1090 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1091 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1093 /* USE of hard_frame_pointer_rtx added for consistency;
1094 not clear if really needed. */
1095 emit_use (hard_frame_pointer_rtx);
1096 emit_use (stack_pointer_rtx);
1098 /* If the architecture is using a GP register, we must
1099 conservatively assume that the target function makes use of it.
1100 The prologue of functions with nonlocal gotos must therefore
1101 initialize the GP register to the appropriate value, and we
1102 must then make sure that this value is live at the point
1103 of the jump. (Note that this doesn't necessarily apply
1104 to targets with a nonlocal_goto pattern; they are free
1105 to implement it in their own way. Note also that this is
1106 a no-op if the GP register is a global invariant.) */
1107 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1108 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1109 emit_use (pic_offset_table_rtx);
1111 emit_indirect_jump (r_label);
1114 /* Search backwards to the jump insn and mark it as a
1115 non-local goto. */
1116 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1118 if (JUMP_P (insn))
1120 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1121 break;
1123 else if (CALL_P (insn))
1124 break;
1127 return const0_rtx;
1130 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1131 (not all will be used on all machines) that was passed to __builtin_setjmp.
1132 It updates the stack pointer in that block to correspond to the current
1133 stack pointer. */
1135 static void
1136 expand_builtin_update_setjmp_buf (rtx buf_addr)
1138 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1139 rtx stack_save
1140 = gen_rtx_MEM (sa_mode,
1141 memory_address
1142 (sa_mode,
1143 plus_constant (Pmode, buf_addr,
1144 2 * GET_MODE_SIZE (Pmode))));
1146 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1149 /* Expand a call to __builtin_prefetch. For a target that does not support
1150 data prefetch, evaluate the memory address argument in case it has side
1151 effects. */
1153 static void
1154 expand_builtin_prefetch (tree exp)
1156 tree arg0, arg1, arg2;
1157 int nargs;
1158 rtx op0, op1, op2;
1160 if (!validate_arglist (exp, POINTER_TYPE, 0))
1161 return;
1163 arg0 = CALL_EXPR_ARG (exp, 0);
1165 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1166 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1167 locality). */
1168 nargs = call_expr_nargs (exp);
1169 if (nargs > 1)
1170 arg1 = CALL_EXPR_ARG (exp, 1);
1171 else
1172 arg1 = integer_zero_node;
1173 if (nargs > 2)
1174 arg2 = CALL_EXPR_ARG (exp, 2);
1175 else
1176 arg2 = integer_three_node;
1178 /* Argument 0 is an address. */
1179 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1181 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1182 if (TREE_CODE (arg1) != INTEGER_CST)
1184 error ("second argument to %<__builtin_prefetch%> must be a constant");
1185 arg1 = integer_zero_node;
1187 op1 = expand_normal (arg1);
1188 /* Argument 1 must be either zero or one. */
1189 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1191 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1192 " using zero");
1193 op1 = const0_rtx;
1196 /* Argument 2 (locality) must be a compile-time constant int. */
1197 if (TREE_CODE (arg2) != INTEGER_CST)
1199 error ("third argument to %<__builtin_prefetch%> must be a constant");
1200 arg2 = integer_zero_node;
1202 op2 = expand_normal (arg2);
1203 /* Argument 2 must be 0, 1, 2, or 3. */
1204 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1206 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1207 op2 = const0_rtx;
1210 #ifdef HAVE_prefetch
1211 if (HAVE_prefetch)
1213 struct expand_operand ops[3];
1215 create_address_operand (&ops[0], op0);
1216 create_integer_operand (&ops[1], INTVAL (op1));
1217 create_integer_operand (&ops[2], INTVAL (op2));
1218 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1219 return;
1221 #endif
1223 /* Don't do anything with direct references to volatile memory, but
1224 generate code to handle other side effects. */
1225 if (!MEM_P (op0) && side_effects_p (op0))
1226 emit_insn (op0);
1229 /* Get a MEM rtx for expression EXP which is the address of an operand
1230 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1231 the maximum length of the block of memory that might be accessed or
1232 NULL if unknown. */
1234 static rtx
1235 get_memory_rtx (tree exp, tree len)
1237 tree orig_exp = exp;
1238 rtx addr, mem;
1240 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1241 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1242 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1243 exp = TREE_OPERAND (exp, 0);
1245 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1246 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1248 /* Get an expression we can use to find the attributes to assign to MEM.
1249 First remove any nops. */
1250 while (CONVERT_EXPR_P (exp)
1251 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1252 exp = TREE_OPERAND (exp, 0);
1254 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1255 (as builtin stringops may alias with anything). */
1256 exp = fold_build2 (MEM_REF,
1257 build_array_type (char_type_node,
1258 build_range_type (sizetype,
1259 size_one_node, len)),
1260 exp, build_int_cst (ptr_type_node, 0));
1262 /* If the MEM_REF has no acceptable address, try to get the base object
1263 from the original address we got, and build an all-aliasing
1264 unknown-sized access to that one. */
1265 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1266 set_mem_attributes (mem, exp, 0);
1267 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1268 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1269 0))))
1271 exp = build_fold_addr_expr (exp);
1272 exp = fold_build2 (MEM_REF,
1273 build_array_type (char_type_node,
1274 build_range_type (sizetype,
1275 size_zero_node,
1276 NULL)),
1277 exp, build_int_cst (ptr_type_node, 0));
1278 set_mem_attributes (mem, exp, 0);
1280 set_mem_alias_set (mem, 0);
1281 return mem;
1284 /* Built-in functions to perform an untyped call and return. */
1286 #define apply_args_mode \
1287 (this_target_builtins->x_apply_args_mode)
1288 #define apply_result_mode \
1289 (this_target_builtins->x_apply_result_mode)
1291 /* Return the size required for the block returned by __builtin_apply_args,
1292 and initialize apply_args_mode. */
1294 static int
1295 apply_args_size (void)
1297 static int size = -1;
1298 int align;
1299 unsigned int regno;
1300 enum machine_mode mode;
1302 /* The values computed by this function never change. */
1303 if (size < 0)
1305 /* The first value is the incoming arg-pointer. */
1306 size = GET_MODE_SIZE (Pmode);
1308 /* The second value is the structure value address unless this is
1309 passed as an "invisible" first argument. */
1310 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1311 size += GET_MODE_SIZE (Pmode);
1313 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1314 if (FUNCTION_ARG_REGNO_P (regno))
1316 mode = targetm.calls.get_raw_arg_mode (regno);
1318 gcc_assert (mode != VOIDmode);
1320 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1321 if (size % align != 0)
1322 size = CEIL (size, align) * align;
1323 size += GET_MODE_SIZE (mode);
1324 apply_args_mode[regno] = mode;
1326 else
1328 apply_args_mode[regno] = VOIDmode;
1331 return size;
1334 /* Return the size required for the block returned by __builtin_apply,
1335 and initialize apply_result_mode. */
1337 static int
1338 apply_result_size (void)
1340 static int size = -1;
1341 int align, regno;
1342 enum machine_mode mode;
1344 /* The values computed by this function never change. */
1345 if (size < 0)
1347 size = 0;
1349 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1350 if (targetm.calls.function_value_regno_p (regno))
1352 mode = targetm.calls.get_raw_result_mode (regno);
1354 gcc_assert (mode != VOIDmode);
1356 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1357 if (size % align != 0)
1358 size = CEIL (size, align) * align;
1359 size += GET_MODE_SIZE (mode);
1360 apply_result_mode[regno] = mode;
1362 else
1363 apply_result_mode[regno] = VOIDmode;
1365 /* Allow targets that use untyped_call and untyped_return to override
1366 the size so that machine-specific information can be stored here. */
1367 #ifdef APPLY_RESULT_SIZE
1368 size = APPLY_RESULT_SIZE;
1369 #endif
1371 return size;
1374 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1375 /* Create a vector describing the result block RESULT. If SAVEP is true,
1376 the result block is used to save the values; otherwise it is used to
1377 restore the values. */
1379 static rtx
1380 result_vector (int savep, rtx result)
1382 int regno, size, align, nelts;
1383 enum machine_mode mode;
1384 rtx reg, mem;
1385 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1387 size = nelts = 0;
1388 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1389 if ((mode = apply_result_mode[regno]) != VOIDmode)
1391 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1392 if (size % align != 0)
1393 size = CEIL (size, align) * align;
1394 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1395 mem = adjust_address (result, mode, size);
1396 savevec[nelts++] = (savep
1397 ? gen_rtx_SET (VOIDmode, mem, reg)
1398 : gen_rtx_SET (VOIDmode, reg, mem));
1399 size += GET_MODE_SIZE (mode);
1401 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1403 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1405 /* Save the state required to perform an untyped call with the same
1406 arguments as were passed to the current function. */
1408 static rtx
1409 expand_builtin_apply_args_1 (void)
1411 rtx registers, tem;
1412 int size, align, regno;
1413 enum machine_mode mode;
1414 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1416 /* Create a block where the arg-pointer, structure value address,
1417 and argument registers can be saved. */
1418 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1420 /* Walk past the arg-pointer and structure value address. */
1421 size = GET_MODE_SIZE (Pmode);
1422 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1423 size += GET_MODE_SIZE (Pmode);
1425 /* Save each register used in calling a function to the block. */
1426 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1427 if ((mode = apply_args_mode[regno]) != VOIDmode)
1429 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1430 if (size % align != 0)
1431 size = CEIL (size, align) * align;
1433 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1435 emit_move_insn (adjust_address (registers, mode, size), tem);
1436 size += GET_MODE_SIZE (mode);
1439 /* Save the arg pointer to the block. */
1440 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1441 #ifdef STACK_GROWS_DOWNWARD
1442 /* We need the pointer as the caller actually passed them to us, not
1443 as we might have pretended they were passed. Make sure it's a valid
1444 operand, as emit_move_insn isn't expected to handle a PLUS. */
1446 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1447 NULL_RTX);
1448 #endif
1449 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1451 size = GET_MODE_SIZE (Pmode);
1453 /* Save the structure value address unless this is passed as an
1454 "invisible" first argument. */
1455 if (struct_incoming_value)
1457 emit_move_insn (adjust_address (registers, Pmode, size),
1458 copy_to_reg (struct_incoming_value));
1459 size += GET_MODE_SIZE (Pmode);
1462 /* Return the address of the block. */
1463 return copy_addr_to_reg (XEXP (registers, 0));
1466 /* __builtin_apply_args returns block of memory allocated on
1467 the stack into which is stored the arg pointer, structure
1468 value address, static chain, and all the registers that might
1469 possibly be used in performing a function call. The code is
1470 moved to the start of the function so the incoming values are
1471 saved. */
1473 static rtx
1474 expand_builtin_apply_args (void)
1476 /* Don't do __builtin_apply_args more than once in a function.
1477 Save the result of the first call and reuse it. */
1478 if (apply_args_value != 0)
1479 return apply_args_value;
1481 /* When this function is called, it means that registers must be
1482 saved on entry to this function. So we migrate the
1483 call to the first insn of this function. */
1484 rtx temp;
1485 rtx seq;
1487 start_sequence ();
1488 temp = expand_builtin_apply_args_1 ();
1489 seq = get_insns ();
1490 end_sequence ();
1492 apply_args_value = temp;
1494 /* Put the insns after the NOTE that starts the function.
1495 If this is inside a start_sequence, make the outer-level insn
1496 chain current, so the code is placed at the start of the
1497 function. If internal_arg_pointer is a non-virtual pseudo,
1498 it needs to be placed after the function that initializes
1499 that pseudo. */
1500 push_topmost_sequence ();
1501 if (REG_P (crtl->args.internal_arg_pointer)
1502 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1503 emit_insn_before (seq, parm_birth_insn);
1504 else
1505 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1506 pop_topmost_sequence ();
1507 return temp;
1511 /* Perform an untyped call and save the state required to perform an
1512 untyped return of whatever value was returned by the given function. */
1514 static rtx
1515 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1517 int size, align, regno;
1518 enum machine_mode mode;
1519 rtx incoming_args, result, reg, dest, src, call_insn;
1520 rtx old_stack_level = 0;
1521 rtx call_fusage = 0;
1522 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1524 arguments = convert_memory_address (Pmode, arguments);
1526 /* Create a block where the return registers can be saved. */
1527 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1529 /* Fetch the arg pointer from the ARGUMENTS block. */
1530 incoming_args = gen_reg_rtx (Pmode);
1531 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1532 #ifndef STACK_GROWS_DOWNWARD
1533 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1534 incoming_args, 0, OPTAB_LIB_WIDEN);
1535 #endif
1537 /* Push a new argument block and copy the arguments. Do not allow
1538 the (potential) memcpy call below to interfere with our stack
1539 manipulations. */
1540 do_pending_stack_adjust ();
1541 NO_DEFER_POP;
1543 /* Save the stack with nonlocal if available. */
1544 #ifdef HAVE_save_stack_nonlocal
1545 if (HAVE_save_stack_nonlocal)
1546 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1547 else
1548 #endif
1549 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1551 /* Allocate a block of memory onto the stack and copy the memory
1552 arguments to the outgoing arguments address. We can pass TRUE
1553 as the 4th argument because we just saved the stack pointer
1554 and will restore it right after the call. */
1555 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1557 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1558 may have already set current_function_calls_alloca to true.
1559 current_function_calls_alloca won't be set if argsize is zero,
1560 so we have to guarantee need_drap is true here. */
1561 if (SUPPORTS_STACK_ALIGNMENT)
1562 crtl->need_drap = true;
1564 dest = virtual_outgoing_args_rtx;
1565 #ifndef STACK_GROWS_DOWNWARD
1566 if (CONST_INT_P (argsize))
1567 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1568 else
1569 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1570 #endif
1571 dest = gen_rtx_MEM (BLKmode, dest);
1572 set_mem_align (dest, PARM_BOUNDARY);
1573 src = gen_rtx_MEM (BLKmode, incoming_args);
1574 set_mem_align (src, PARM_BOUNDARY);
1575 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1577 /* Refer to the argument block. */
1578 apply_args_size ();
1579 arguments = gen_rtx_MEM (BLKmode, arguments);
1580 set_mem_align (arguments, PARM_BOUNDARY);
1582 /* Walk past the arg-pointer and structure value address. */
1583 size = GET_MODE_SIZE (Pmode);
1584 if (struct_value)
1585 size += GET_MODE_SIZE (Pmode);
1587 /* Restore each of the registers previously saved. Make USE insns
1588 for each of these registers for use in making the call. */
1589 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1590 if ((mode = apply_args_mode[regno]) != VOIDmode)
1592 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1593 if (size % align != 0)
1594 size = CEIL (size, align) * align;
1595 reg = gen_rtx_REG (mode, regno);
1596 emit_move_insn (reg, adjust_address (arguments, mode, size));
1597 use_reg (&call_fusage, reg);
1598 size += GET_MODE_SIZE (mode);
1601 /* Restore the structure value address unless this is passed as an
1602 "invisible" first argument. */
1603 size = GET_MODE_SIZE (Pmode);
1604 if (struct_value)
1606 rtx value = gen_reg_rtx (Pmode);
1607 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1608 emit_move_insn (struct_value, value);
1609 if (REG_P (struct_value))
1610 use_reg (&call_fusage, struct_value);
1611 size += GET_MODE_SIZE (Pmode);
1614 /* All arguments and registers used for the call are set up by now! */
1615 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1617 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1618 and we don't want to load it into a register as an optimization,
1619 because prepare_call_address already did it if it should be done. */
1620 if (GET_CODE (function) != SYMBOL_REF)
1621 function = memory_address (FUNCTION_MODE, function);
1623 /* Generate the actual call instruction and save the return value. */
1624 #ifdef HAVE_untyped_call
1625 if (HAVE_untyped_call)
1626 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1627 result, result_vector (1, result)));
1628 else
1629 #endif
1630 #ifdef HAVE_call_value
1631 if (HAVE_call_value)
1633 rtx valreg = 0;
1635 /* Locate the unique return register. It is not possible to
1636 express a call that sets more than one return register using
1637 call_value; use untyped_call for that. In fact, untyped_call
1638 only needs to save the return registers in the given block. */
1639 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1640 if ((mode = apply_result_mode[regno]) != VOIDmode)
1642 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1644 valreg = gen_rtx_REG (mode, regno);
1647 emit_call_insn (GEN_CALL_VALUE (valreg,
1648 gen_rtx_MEM (FUNCTION_MODE, function),
1649 const0_rtx, NULL_RTX, const0_rtx));
1651 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1653 else
1654 #endif
1655 gcc_unreachable ();
1657 /* Find the CALL insn we just emitted, and attach the register usage
1658 information. */
1659 call_insn = last_call_insn ();
1660 add_function_usage_to (call_insn, call_fusage);
1662 /* Restore the stack. */
1663 #ifdef HAVE_save_stack_nonlocal
1664 if (HAVE_save_stack_nonlocal)
1665 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1666 else
1667 #endif
1668 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1669 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1671 OK_DEFER_POP;
1673 /* Return the address of the result block. */
1674 result = copy_addr_to_reg (XEXP (result, 0));
1675 return convert_memory_address (ptr_mode, result);
1678 /* Perform an untyped return. */
1680 static void
1681 expand_builtin_return (rtx result)
1683 int size, align, regno;
1684 enum machine_mode mode;
1685 rtx reg;
1686 rtx call_fusage = 0;
1688 result = convert_memory_address (Pmode, result);
1690 apply_result_size ();
1691 result = gen_rtx_MEM (BLKmode, result);
1693 #ifdef HAVE_untyped_return
1694 if (HAVE_untyped_return)
1696 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1697 emit_barrier ();
1698 return;
1700 #endif
1702 /* Restore the return value and note that each value is used. */
1703 size = 0;
1704 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1705 if ((mode = apply_result_mode[regno]) != VOIDmode)
1707 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1708 if (size % align != 0)
1709 size = CEIL (size, align) * align;
1710 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1711 emit_move_insn (reg, adjust_address (result, mode, size));
1713 push_to_sequence (call_fusage);
1714 emit_use (reg);
1715 call_fusage = get_insns ();
1716 end_sequence ();
1717 size += GET_MODE_SIZE (mode);
1720 /* Put the USE insns before the return. */
1721 emit_insn (call_fusage);
1723 /* Return whatever values was restored by jumping directly to the end
1724 of the function. */
1725 expand_naked_return ();
1728 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1730 static enum type_class
1731 type_to_class (tree type)
1733 switch (TREE_CODE (type))
1735 case VOID_TYPE: return void_type_class;
1736 case INTEGER_TYPE: return integer_type_class;
1737 case ENUMERAL_TYPE: return enumeral_type_class;
1738 case BOOLEAN_TYPE: return boolean_type_class;
1739 case POINTER_TYPE: return pointer_type_class;
1740 case REFERENCE_TYPE: return reference_type_class;
1741 case OFFSET_TYPE: return offset_type_class;
1742 case REAL_TYPE: return real_type_class;
1743 case COMPLEX_TYPE: return complex_type_class;
1744 case FUNCTION_TYPE: return function_type_class;
1745 case METHOD_TYPE: return method_type_class;
1746 case RECORD_TYPE: return record_type_class;
1747 case UNION_TYPE:
1748 case QUAL_UNION_TYPE: return union_type_class;
1749 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1750 ? string_type_class : array_type_class);
1751 case LANG_TYPE: return lang_type_class;
1752 default: return no_type_class;
1756 /* Expand a call EXP to __builtin_classify_type. */
1758 static rtx
1759 expand_builtin_classify_type (tree exp)
1761 if (call_expr_nargs (exp))
1762 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1763 return GEN_INT (no_type_class);
1766 /* This helper macro, meant to be used in mathfn_built_in below,
1767 determines which among a set of three builtin math functions is
1768 appropriate for a given type mode. The `F' and `L' cases are
1769 automatically generated from the `double' case. */
1770 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1771 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1772 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1773 fcodel = BUILT_IN_MATHFN##L ; break;
1774 /* Similar to above, but appends _R after any F/L suffix. */
1775 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1776 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1777 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1778 fcodel = BUILT_IN_MATHFN##L_R ; break;
1780 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1781 if available. If IMPLICIT is true use the implicit builtin declaration,
1782 otherwise use the explicit declaration. If we can't do the conversion,
1783 return zero. */
1785 static tree
1786 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1788 enum built_in_function fcode, fcodef, fcodel, fcode2;
1790 switch (fn)
1792 CASE_MATHFN (BUILT_IN_ACOS)
1793 CASE_MATHFN (BUILT_IN_ACOSH)
1794 CASE_MATHFN (BUILT_IN_ASIN)
1795 CASE_MATHFN (BUILT_IN_ASINH)
1796 CASE_MATHFN (BUILT_IN_ATAN)
1797 CASE_MATHFN (BUILT_IN_ATAN2)
1798 CASE_MATHFN (BUILT_IN_ATANH)
1799 CASE_MATHFN (BUILT_IN_CBRT)
1800 CASE_MATHFN (BUILT_IN_CEIL)
1801 CASE_MATHFN (BUILT_IN_CEXPI)
1802 CASE_MATHFN (BUILT_IN_COPYSIGN)
1803 CASE_MATHFN (BUILT_IN_COS)
1804 CASE_MATHFN (BUILT_IN_COSH)
1805 CASE_MATHFN (BUILT_IN_DREM)
1806 CASE_MATHFN (BUILT_IN_ERF)
1807 CASE_MATHFN (BUILT_IN_ERFC)
1808 CASE_MATHFN (BUILT_IN_EXP)
1809 CASE_MATHFN (BUILT_IN_EXP10)
1810 CASE_MATHFN (BUILT_IN_EXP2)
1811 CASE_MATHFN (BUILT_IN_EXPM1)
1812 CASE_MATHFN (BUILT_IN_FABS)
1813 CASE_MATHFN (BUILT_IN_FDIM)
1814 CASE_MATHFN (BUILT_IN_FLOOR)
1815 CASE_MATHFN (BUILT_IN_FMA)
1816 CASE_MATHFN (BUILT_IN_FMAX)
1817 CASE_MATHFN (BUILT_IN_FMIN)
1818 CASE_MATHFN (BUILT_IN_FMOD)
1819 CASE_MATHFN (BUILT_IN_FREXP)
1820 CASE_MATHFN (BUILT_IN_GAMMA)
1821 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1822 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1823 CASE_MATHFN (BUILT_IN_HYPOT)
1824 CASE_MATHFN (BUILT_IN_ILOGB)
1825 CASE_MATHFN (BUILT_IN_ICEIL)
1826 CASE_MATHFN (BUILT_IN_IFLOOR)
1827 CASE_MATHFN (BUILT_IN_INF)
1828 CASE_MATHFN (BUILT_IN_IRINT)
1829 CASE_MATHFN (BUILT_IN_IROUND)
1830 CASE_MATHFN (BUILT_IN_ISINF)
1831 CASE_MATHFN (BUILT_IN_J0)
1832 CASE_MATHFN (BUILT_IN_J1)
1833 CASE_MATHFN (BUILT_IN_JN)
1834 CASE_MATHFN (BUILT_IN_LCEIL)
1835 CASE_MATHFN (BUILT_IN_LDEXP)
1836 CASE_MATHFN (BUILT_IN_LFLOOR)
1837 CASE_MATHFN (BUILT_IN_LGAMMA)
1838 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1839 CASE_MATHFN (BUILT_IN_LLCEIL)
1840 CASE_MATHFN (BUILT_IN_LLFLOOR)
1841 CASE_MATHFN (BUILT_IN_LLRINT)
1842 CASE_MATHFN (BUILT_IN_LLROUND)
1843 CASE_MATHFN (BUILT_IN_LOG)
1844 CASE_MATHFN (BUILT_IN_LOG10)
1845 CASE_MATHFN (BUILT_IN_LOG1P)
1846 CASE_MATHFN (BUILT_IN_LOG2)
1847 CASE_MATHFN (BUILT_IN_LOGB)
1848 CASE_MATHFN (BUILT_IN_LRINT)
1849 CASE_MATHFN (BUILT_IN_LROUND)
1850 CASE_MATHFN (BUILT_IN_MODF)
1851 CASE_MATHFN (BUILT_IN_NAN)
1852 CASE_MATHFN (BUILT_IN_NANS)
1853 CASE_MATHFN (BUILT_IN_NEARBYINT)
1854 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1855 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1856 CASE_MATHFN (BUILT_IN_POW)
1857 CASE_MATHFN (BUILT_IN_POWI)
1858 CASE_MATHFN (BUILT_IN_POW10)
1859 CASE_MATHFN (BUILT_IN_REMAINDER)
1860 CASE_MATHFN (BUILT_IN_REMQUO)
1861 CASE_MATHFN (BUILT_IN_RINT)
1862 CASE_MATHFN (BUILT_IN_ROUND)
1863 CASE_MATHFN (BUILT_IN_SCALB)
1864 CASE_MATHFN (BUILT_IN_SCALBLN)
1865 CASE_MATHFN (BUILT_IN_SCALBN)
1866 CASE_MATHFN (BUILT_IN_SIGNBIT)
1867 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1868 CASE_MATHFN (BUILT_IN_SIN)
1869 CASE_MATHFN (BUILT_IN_SINCOS)
1870 CASE_MATHFN (BUILT_IN_SINH)
1871 CASE_MATHFN (BUILT_IN_SQRT)
1872 CASE_MATHFN (BUILT_IN_TAN)
1873 CASE_MATHFN (BUILT_IN_TANH)
1874 CASE_MATHFN (BUILT_IN_TGAMMA)
1875 CASE_MATHFN (BUILT_IN_TRUNC)
1876 CASE_MATHFN (BUILT_IN_Y0)
1877 CASE_MATHFN (BUILT_IN_Y1)
1878 CASE_MATHFN (BUILT_IN_YN)
1880 default:
1881 return NULL_TREE;
1884 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1885 fcode2 = fcode;
1886 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1887 fcode2 = fcodef;
1888 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1889 fcode2 = fcodel;
1890 else
1891 return NULL_TREE;
1893 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1894 return NULL_TREE;
1896 return builtin_decl_explicit (fcode2);
1899 /* Like mathfn_built_in_1(), but always use the implicit array. */
1901 tree
1902 mathfn_built_in (tree type, enum built_in_function fn)
1904 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1907 /* If errno must be maintained, expand the RTL to check if the result,
1908 TARGET, of a built-in function call, EXP, is NaN, and if so set
1909 errno to EDOM. */
1911 static void
1912 expand_errno_check (tree exp, rtx target)
1914 rtx lab = gen_label_rtx ();
1916 /* Test the result; if it is NaN, set errno=EDOM because
1917 the argument was not in the domain. */
1918 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1919 NULL_RTX, NULL_RTX, lab,
1920 /* The jump is very likely. */
1921 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1923 #ifdef TARGET_EDOM
1924 /* If this built-in doesn't throw an exception, set errno directly. */
1925 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1927 #ifdef GEN_ERRNO_RTX
1928 rtx errno_rtx = GEN_ERRNO_RTX;
1929 #else
1930 rtx errno_rtx
1931 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1932 #endif
1933 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1934 emit_label (lab);
1935 return;
1937 #endif
1939 /* Make sure the library call isn't expanded as a tail call. */
1940 CALL_EXPR_TAILCALL (exp) = 0;
1942 /* We can't set errno=EDOM directly; let the library call do it.
1943 Pop the arguments right away in case the call gets deleted. */
1944 NO_DEFER_POP;
1945 expand_call (exp, target, 0);
1946 OK_DEFER_POP;
1947 emit_label (lab);
1950 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1951 Return NULL_RTX if a normal call should be emitted rather than expanding
1952 the function in-line. EXP is the expression that is a call to the builtin
1953 function; if convenient, the result should be placed in TARGET.
1954 SUBTARGET may be used as the target for computing one of EXP's operands. */
1956 static rtx
1957 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1959 optab builtin_optab;
1960 rtx op0, insns;
1961 tree fndecl = get_callee_fndecl (exp);
1962 enum machine_mode mode;
1963 bool errno_set = false;
1964 tree arg;
1966 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1967 return NULL_RTX;
1969 arg = CALL_EXPR_ARG (exp, 0);
1971 switch (DECL_FUNCTION_CODE (fndecl))
1973 CASE_FLT_FN (BUILT_IN_SQRT):
1974 errno_set = ! tree_expr_nonnegative_p (arg);
1975 builtin_optab = sqrt_optab;
1976 break;
1977 CASE_FLT_FN (BUILT_IN_EXP):
1978 errno_set = true; builtin_optab = exp_optab; break;
1979 CASE_FLT_FN (BUILT_IN_EXP10):
1980 CASE_FLT_FN (BUILT_IN_POW10):
1981 errno_set = true; builtin_optab = exp10_optab; break;
1982 CASE_FLT_FN (BUILT_IN_EXP2):
1983 errno_set = true; builtin_optab = exp2_optab; break;
1984 CASE_FLT_FN (BUILT_IN_EXPM1):
1985 errno_set = true; builtin_optab = expm1_optab; break;
1986 CASE_FLT_FN (BUILT_IN_LOGB):
1987 errno_set = true; builtin_optab = logb_optab; break;
1988 CASE_FLT_FN (BUILT_IN_LOG):
1989 errno_set = true; builtin_optab = log_optab; break;
1990 CASE_FLT_FN (BUILT_IN_LOG10):
1991 errno_set = true; builtin_optab = log10_optab; break;
1992 CASE_FLT_FN (BUILT_IN_LOG2):
1993 errno_set = true; builtin_optab = log2_optab; break;
1994 CASE_FLT_FN (BUILT_IN_LOG1P):
1995 errno_set = true; builtin_optab = log1p_optab; break;
1996 CASE_FLT_FN (BUILT_IN_ASIN):
1997 builtin_optab = asin_optab; break;
1998 CASE_FLT_FN (BUILT_IN_ACOS):
1999 builtin_optab = acos_optab; break;
2000 CASE_FLT_FN (BUILT_IN_TAN):
2001 builtin_optab = tan_optab; break;
2002 CASE_FLT_FN (BUILT_IN_ATAN):
2003 builtin_optab = atan_optab; break;
2004 CASE_FLT_FN (BUILT_IN_FLOOR):
2005 builtin_optab = floor_optab; break;
2006 CASE_FLT_FN (BUILT_IN_CEIL):
2007 builtin_optab = ceil_optab; break;
2008 CASE_FLT_FN (BUILT_IN_TRUNC):
2009 builtin_optab = btrunc_optab; break;
2010 CASE_FLT_FN (BUILT_IN_ROUND):
2011 builtin_optab = round_optab; break;
2012 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2013 builtin_optab = nearbyint_optab;
2014 if (flag_trapping_math)
2015 break;
2016 /* Else fallthrough and expand as rint. */
2017 CASE_FLT_FN (BUILT_IN_RINT):
2018 builtin_optab = rint_optab; break;
2019 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2020 builtin_optab = significand_optab; break;
2021 default:
2022 gcc_unreachable ();
2025 /* Make a suitable register to place result in. */
2026 mode = TYPE_MODE (TREE_TYPE (exp));
2028 if (! flag_errno_math || ! HONOR_NANS (mode))
2029 errno_set = false;
2031 /* Before working hard, check whether the instruction is available. */
2032 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2033 && (!errno_set || !optimize_insn_for_size_p ()))
2035 rtx result = gen_reg_rtx (mode);
2037 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2038 need to expand the argument again. This way, we will not perform
2039 side-effects more the once. */
2040 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2042 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2044 start_sequence ();
2046 /* Compute into RESULT.
2047 Set RESULT to wherever the result comes back. */
2048 result = expand_unop (mode, builtin_optab, op0, result, 0);
2050 if (result != 0)
2052 if (errno_set)
2053 expand_errno_check (exp, result);
2055 /* Output the entire sequence. */
2056 insns = get_insns ();
2057 end_sequence ();
2058 emit_insn (insns);
2059 return result;
2062 /* If we were unable to expand via the builtin, stop the sequence
2063 (without outputting the insns) and call to the library function
2064 with the stabilized argument list. */
2065 end_sequence ();
2068 return expand_call (exp, target, target == const0_rtx);
2071 /* Expand a call to the builtin binary math functions (pow and atan2).
2072 Return NULL_RTX if a normal call should be emitted rather than expanding the
2073 function in-line. EXP is the expression that is a call to the builtin
2074 function; if convenient, the result should be placed in TARGET.
2075 SUBTARGET may be used as the target for computing one of EXP's
2076 operands. */
2078 static rtx
2079 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2081 optab builtin_optab;
2082 rtx op0, op1, insns, result;
2083 int op1_type = REAL_TYPE;
2084 tree fndecl = get_callee_fndecl (exp);
2085 tree arg0, arg1;
2086 enum machine_mode mode;
2087 bool errno_set = true;
2089 switch (DECL_FUNCTION_CODE (fndecl))
2091 CASE_FLT_FN (BUILT_IN_SCALBN):
2092 CASE_FLT_FN (BUILT_IN_SCALBLN):
2093 CASE_FLT_FN (BUILT_IN_LDEXP):
2094 op1_type = INTEGER_TYPE;
2095 default:
2096 break;
2099 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2100 return NULL_RTX;
2102 arg0 = CALL_EXPR_ARG (exp, 0);
2103 arg1 = CALL_EXPR_ARG (exp, 1);
2105 switch (DECL_FUNCTION_CODE (fndecl))
2107 CASE_FLT_FN (BUILT_IN_POW):
2108 builtin_optab = pow_optab; break;
2109 CASE_FLT_FN (BUILT_IN_ATAN2):
2110 builtin_optab = atan2_optab; break;
2111 CASE_FLT_FN (BUILT_IN_SCALB):
2112 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2113 return 0;
2114 builtin_optab = scalb_optab; break;
2115 CASE_FLT_FN (BUILT_IN_SCALBN):
2116 CASE_FLT_FN (BUILT_IN_SCALBLN):
2117 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2118 return 0;
2119 /* Fall through... */
2120 CASE_FLT_FN (BUILT_IN_LDEXP):
2121 builtin_optab = ldexp_optab; break;
2122 CASE_FLT_FN (BUILT_IN_FMOD):
2123 builtin_optab = fmod_optab; break;
2124 CASE_FLT_FN (BUILT_IN_REMAINDER):
2125 CASE_FLT_FN (BUILT_IN_DREM):
2126 builtin_optab = remainder_optab; break;
2127 default:
2128 gcc_unreachable ();
2131 /* Make a suitable register to place result in. */
2132 mode = TYPE_MODE (TREE_TYPE (exp));
2134 /* Before working hard, check whether the instruction is available. */
2135 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2136 return NULL_RTX;
2138 result = gen_reg_rtx (mode);
2140 if (! flag_errno_math || ! HONOR_NANS (mode))
2141 errno_set = false;
2143 if (errno_set && optimize_insn_for_size_p ())
2144 return 0;
2146 /* Always stabilize the argument list. */
2147 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2148 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2150 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2151 op1 = expand_normal (arg1);
2153 start_sequence ();
2155 /* Compute into RESULT.
2156 Set RESULT to wherever the result comes back. */
2157 result = expand_binop (mode, builtin_optab, op0, op1,
2158 result, 0, OPTAB_DIRECT);
2160 /* If we were unable to expand via the builtin, stop the sequence
2161 (without outputting the insns) and call to the library function
2162 with the stabilized argument list. */
2163 if (result == 0)
2165 end_sequence ();
2166 return expand_call (exp, target, target == const0_rtx);
2169 if (errno_set)
2170 expand_errno_check (exp, result);
2172 /* Output the entire sequence. */
2173 insns = get_insns ();
2174 end_sequence ();
2175 emit_insn (insns);
2177 return result;
2180 /* Expand a call to the builtin trinary math functions (fma).
2181 Return NULL_RTX if a normal call should be emitted rather than expanding the
2182 function in-line. EXP is the expression that is a call to the builtin
2183 function; if convenient, the result should be placed in TARGET.
2184 SUBTARGET may be used as the target for computing one of EXP's
2185 operands. */
2187 static rtx
2188 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2190 optab builtin_optab;
2191 rtx op0, op1, op2, insns, result;
2192 tree fndecl = get_callee_fndecl (exp);
2193 tree arg0, arg1, arg2;
2194 enum machine_mode mode;
2196 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2197 return NULL_RTX;
2199 arg0 = CALL_EXPR_ARG (exp, 0);
2200 arg1 = CALL_EXPR_ARG (exp, 1);
2201 arg2 = CALL_EXPR_ARG (exp, 2);
2203 switch (DECL_FUNCTION_CODE (fndecl))
2205 CASE_FLT_FN (BUILT_IN_FMA):
2206 builtin_optab = fma_optab; break;
2207 default:
2208 gcc_unreachable ();
2211 /* Make a suitable register to place result in. */
2212 mode = TYPE_MODE (TREE_TYPE (exp));
2214 /* Before working hard, check whether the instruction is available. */
2215 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2216 return NULL_RTX;
2218 result = gen_reg_rtx (mode);
2220 /* Always stabilize the argument list. */
2221 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2222 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2223 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2225 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2226 op1 = expand_normal (arg1);
2227 op2 = expand_normal (arg2);
2229 start_sequence ();
2231 /* Compute into RESULT.
2232 Set RESULT to wherever the result comes back. */
2233 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2234 result, 0);
2236 /* If we were unable to expand via the builtin, stop the sequence
2237 (without outputting the insns) and call to the library function
2238 with the stabilized argument list. */
2239 if (result == 0)
2241 end_sequence ();
2242 return expand_call (exp, target, target == const0_rtx);
2245 /* Output the entire sequence. */
2246 insns = get_insns ();
2247 end_sequence ();
2248 emit_insn (insns);
2250 return result;
2253 /* Expand a call to the builtin sin and cos math functions.
2254 Return NULL_RTX if a normal call should be emitted rather than expanding the
2255 function in-line. EXP is the expression that is a call to the builtin
2256 function; if convenient, the result should be placed in TARGET.
2257 SUBTARGET may be used as the target for computing one of EXP's
2258 operands. */
2260 static rtx
2261 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2263 optab builtin_optab;
2264 rtx op0, insns;
2265 tree fndecl = get_callee_fndecl (exp);
2266 enum machine_mode mode;
2267 tree arg;
2269 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2270 return NULL_RTX;
2272 arg = CALL_EXPR_ARG (exp, 0);
2274 switch (DECL_FUNCTION_CODE (fndecl))
2276 CASE_FLT_FN (BUILT_IN_SIN):
2277 CASE_FLT_FN (BUILT_IN_COS):
2278 builtin_optab = sincos_optab; break;
2279 default:
2280 gcc_unreachable ();
2283 /* Make a suitable register to place result in. */
2284 mode = TYPE_MODE (TREE_TYPE (exp));
2286 /* Check if sincos insn is available, otherwise fallback
2287 to sin or cos insn. */
2288 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2289 switch (DECL_FUNCTION_CODE (fndecl))
2291 CASE_FLT_FN (BUILT_IN_SIN):
2292 builtin_optab = sin_optab; break;
2293 CASE_FLT_FN (BUILT_IN_COS):
2294 builtin_optab = cos_optab; break;
2295 default:
2296 gcc_unreachable ();
2299 /* Before working hard, check whether the instruction is available. */
2300 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2302 rtx result = gen_reg_rtx (mode);
2304 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2305 need to expand the argument again. This way, we will not perform
2306 side-effects more the once. */
2307 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2309 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2311 start_sequence ();
2313 /* Compute into RESULT.
2314 Set RESULT to wherever the result comes back. */
2315 if (builtin_optab == sincos_optab)
2317 int ok;
2319 switch (DECL_FUNCTION_CODE (fndecl))
2321 CASE_FLT_FN (BUILT_IN_SIN):
2322 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2323 break;
2324 CASE_FLT_FN (BUILT_IN_COS):
2325 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2326 break;
2327 default:
2328 gcc_unreachable ();
2330 gcc_assert (ok);
2332 else
2333 result = expand_unop (mode, builtin_optab, op0, result, 0);
2335 if (result != 0)
2337 /* Output the entire sequence. */
2338 insns = get_insns ();
2339 end_sequence ();
2340 emit_insn (insns);
2341 return result;
2344 /* If we were unable to expand via the builtin, stop the sequence
2345 (without outputting the insns) and call to the library function
2346 with the stabilized argument list. */
2347 end_sequence ();
2350 return expand_call (exp, target, target == const0_rtx);
2353 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2354 return an RTL instruction code that implements the functionality.
2355 If that isn't possible or available return CODE_FOR_nothing. */
2357 static enum insn_code
2358 interclass_mathfn_icode (tree arg, tree fndecl)
2360 bool errno_set = false;
2361 optab builtin_optab = unknown_optab;
2362 enum machine_mode mode;
2364 switch (DECL_FUNCTION_CODE (fndecl))
2366 CASE_FLT_FN (BUILT_IN_ILOGB):
2367 errno_set = true; builtin_optab = ilogb_optab; break;
2368 CASE_FLT_FN (BUILT_IN_ISINF):
2369 builtin_optab = isinf_optab; break;
2370 case BUILT_IN_ISNORMAL:
2371 case BUILT_IN_ISFINITE:
2372 CASE_FLT_FN (BUILT_IN_FINITE):
2373 case BUILT_IN_FINITED32:
2374 case BUILT_IN_FINITED64:
2375 case BUILT_IN_FINITED128:
2376 case BUILT_IN_ISINFD32:
2377 case BUILT_IN_ISINFD64:
2378 case BUILT_IN_ISINFD128:
2379 /* These builtins have no optabs (yet). */
2380 break;
2381 default:
2382 gcc_unreachable ();
2385 /* There's no easy way to detect the case we need to set EDOM. */
2386 if (flag_errno_math && errno_set)
2387 return CODE_FOR_nothing;
2389 /* Optab mode depends on the mode of the input argument. */
2390 mode = TYPE_MODE (TREE_TYPE (arg));
2392 if (builtin_optab)
2393 return optab_handler (builtin_optab, mode);
2394 return CODE_FOR_nothing;
2397 /* Expand a call to one of the builtin math functions that operate on
2398 floating point argument and output an integer result (ilogb, isinf,
2399 isnan, etc).
2400 Return 0 if a normal call should be emitted rather than expanding the
2401 function in-line. EXP is the expression that is a call to the builtin
2402 function; if convenient, the result should be placed in TARGET. */
2404 static rtx
2405 expand_builtin_interclass_mathfn (tree exp, rtx target)
2407 enum insn_code icode = CODE_FOR_nothing;
2408 rtx op0;
2409 tree fndecl = get_callee_fndecl (exp);
2410 enum machine_mode mode;
2411 tree arg;
2413 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2414 return NULL_RTX;
2416 arg = CALL_EXPR_ARG (exp, 0);
2417 icode = interclass_mathfn_icode (arg, fndecl);
2418 mode = TYPE_MODE (TREE_TYPE (arg));
2420 if (icode != CODE_FOR_nothing)
2422 struct expand_operand ops[1];
2423 rtx last = get_last_insn ();
2424 tree orig_arg = arg;
2426 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2427 need to expand the argument again. This way, we will not perform
2428 side-effects more the once. */
2429 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2431 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2433 if (mode != GET_MODE (op0))
2434 op0 = convert_to_mode (mode, op0, 0);
2436 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2437 if (maybe_legitimize_operands (icode, 0, 1, ops)
2438 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2439 return ops[0].value;
2441 delete_insns_since (last);
2442 CALL_EXPR_ARG (exp, 0) = orig_arg;
2445 return NULL_RTX;
2448 /* Expand a call to the builtin sincos math function.
2449 Return NULL_RTX if a normal call should be emitted rather than expanding the
2450 function in-line. EXP is the expression that is a call to the builtin
2451 function. */
2453 static rtx
2454 expand_builtin_sincos (tree exp)
2456 rtx op0, op1, op2, target1, target2;
2457 enum machine_mode mode;
2458 tree arg, sinp, cosp;
2459 int result;
2460 location_t loc = EXPR_LOCATION (exp);
2461 tree alias_type, alias_off;
2463 if (!validate_arglist (exp, REAL_TYPE,
2464 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2465 return NULL_RTX;
2467 arg = CALL_EXPR_ARG (exp, 0);
2468 sinp = CALL_EXPR_ARG (exp, 1);
2469 cosp = CALL_EXPR_ARG (exp, 2);
2471 /* Make a suitable register to place result in. */
2472 mode = TYPE_MODE (TREE_TYPE (arg));
2474 /* Check if sincos insn is available, otherwise emit the call. */
2475 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2476 return NULL_RTX;
2478 target1 = gen_reg_rtx (mode);
2479 target2 = gen_reg_rtx (mode);
2481 op0 = expand_normal (arg);
2482 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2483 alias_off = build_int_cst (alias_type, 0);
2484 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2485 sinp, alias_off));
2486 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2487 cosp, alias_off));
2489 /* Compute into target1 and target2.
2490 Set TARGET to wherever the result comes back. */
2491 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2492 gcc_assert (result);
2494 /* Move target1 and target2 to the memory locations indicated
2495 by op1 and op2. */
2496 emit_move_insn (op1, target1);
2497 emit_move_insn (op2, target2);
2499 return const0_rtx;
2502 /* Expand a call to the internal cexpi builtin to the sincos math function.
2503 EXP is the expression that is a call to the builtin function; if convenient,
2504 the result should be placed in TARGET. */
2506 static rtx
2507 expand_builtin_cexpi (tree exp, rtx target)
2509 tree fndecl = get_callee_fndecl (exp);
2510 tree arg, type;
2511 enum machine_mode mode;
2512 rtx op0, op1, op2;
2513 location_t loc = EXPR_LOCATION (exp);
2515 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2516 return NULL_RTX;
2518 arg = CALL_EXPR_ARG (exp, 0);
2519 type = TREE_TYPE (arg);
2520 mode = TYPE_MODE (TREE_TYPE (arg));
2522 /* Try expanding via a sincos optab, fall back to emitting a libcall
2523 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2524 is only generated from sincos, cexp or if we have either of them. */
2525 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2527 op1 = gen_reg_rtx (mode);
2528 op2 = gen_reg_rtx (mode);
2530 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2532 /* Compute into op1 and op2. */
2533 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2535 else if (TARGET_HAS_SINCOS)
2537 tree call, fn = NULL_TREE;
2538 tree top1, top2;
2539 rtx op1a, op2a;
2541 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2542 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2543 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2544 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2545 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2546 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2547 else
2548 gcc_unreachable ();
2550 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2551 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2552 op1a = copy_addr_to_reg (XEXP (op1, 0));
2553 op2a = copy_addr_to_reg (XEXP (op2, 0));
2554 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2555 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2557 /* Make sure not to fold the sincos call again. */
2558 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2559 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2560 call, 3, arg, top1, top2));
2562 else
2564 tree call, fn = NULL_TREE, narg;
2565 tree ctype = build_complex_type (type);
2567 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2568 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2569 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2570 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2571 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2572 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2573 else
2574 gcc_unreachable ();
2576 /* If we don't have a decl for cexp create one. This is the
2577 friendliest fallback if the user calls __builtin_cexpi
2578 without full target C99 function support. */
2579 if (fn == NULL_TREE)
2581 tree fntype;
2582 const char *name = NULL;
2584 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2585 name = "cexpf";
2586 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2587 name = "cexp";
2588 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2589 name = "cexpl";
2591 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2592 fn = build_fn_decl (name, fntype);
2595 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2596 build_real (type, dconst0), arg);
2598 /* Make sure not to fold the cexp call again. */
2599 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2600 return expand_expr (build_call_nary (ctype, call, 1, narg),
2601 target, VOIDmode, EXPAND_NORMAL);
2604 /* Now build the proper return type. */
2605 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2606 make_tree (TREE_TYPE (arg), op2),
2607 make_tree (TREE_TYPE (arg), op1)),
2608 target, VOIDmode, EXPAND_NORMAL);
2611 /* Conveniently construct a function call expression. FNDECL names the
2612 function to be called, N is the number of arguments, and the "..."
2613 parameters are the argument expressions. Unlike build_call_exr
2614 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2616 static tree
2617 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2619 va_list ap;
2620 tree fntype = TREE_TYPE (fndecl);
2621 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2623 va_start (ap, n);
2624 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2625 va_end (ap);
2626 SET_EXPR_LOCATION (fn, loc);
2627 return fn;
2630 /* Expand a call to one of the builtin rounding functions gcc defines
2631 as an extension (lfloor and lceil). As these are gcc extensions we
2632 do not need to worry about setting errno to EDOM.
2633 If expanding via optab fails, lower expression to (int)(floor(x)).
2634 EXP is the expression that is a call to the builtin function;
2635 if convenient, the result should be placed in TARGET. */
2637 static rtx
2638 expand_builtin_int_roundingfn (tree exp, rtx target)
2640 convert_optab builtin_optab;
2641 rtx op0, insns, tmp;
2642 tree fndecl = get_callee_fndecl (exp);
2643 enum built_in_function fallback_fn;
2644 tree fallback_fndecl;
2645 enum machine_mode mode;
2646 tree arg;
2648 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2649 gcc_unreachable ();
2651 arg = CALL_EXPR_ARG (exp, 0);
2653 switch (DECL_FUNCTION_CODE (fndecl))
2655 CASE_FLT_FN (BUILT_IN_ICEIL):
2656 CASE_FLT_FN (BUILT_IN_LCEIL):
2657 CASE_FLT_FN (BUILT_IN_LLCEIL):
2658 builtin_optab = lceil_optab;
2659 fallback_fn = BUILT_IN_CEIL;
2660 break;
2662 CASE_FLT_FN (BUILT_IN_IFLOOR):
2663 CASE_FLT_FN (BUILT_IN_LFLOOR):
2664 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2665 builtin_optab = lfloor_optab;
2666 fallback_fn = BUILT_IN_FLOOR;
2667 break;
2669 default:
2670 gcc_unreachable ();
2673 /* Make a suitable register to place result in. */
2674 mode = TYPE_MODE (TREE_TYPE (exp));
2676 target = gen_reg_rtx (mode);
2678 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2679 need to expand the argument again. This way, we will not perform
2680 side-effects more the once. */
2681 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2683 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2685 start_sequence ();
2687 /* Compute into TARGET. */
2688 if (expand_sfix_optab (target, op0, builtin_optab))
2690 /* Output the entire sequence. */
2691 insns = get_insns ();
2692 end_sequence ();
2693 emit_insn (insns);
2694 return target;
2697 /* If we were unable to expand via the builtin, stop the sequence
2698 (without outputting the insns). */
2699 end_sequence ();
2701 /* Fall back to floating point rounding optab. */
2702 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2704 /* For non-C99 targets we may end up without a fallback fndecl here
2705 if the user called __builtin_lfloor directly. In this case emit
2706 a call to the floor/ceil variants nevertheless. This should result
2707 in the best user experience for not full C99 targets. */
2708 if (fallback_fndecl == NULL_TREE)
2710 tree fntype;
2711 const char *name = NULL;
2713 switch (DECL_FUNCTION_CODE (fndecl))
2715 case BUILT_IN_ICEIL:
2716 case BUILT_IN_LCEIL:
2717 case BUILT_IN_LLCEIL:
2718 name = "ceil";
2719 break;
2720 case BUILT_IN_ICEILF:
2721 case BUILT_IN_LCEILF:
2722 case BUILT_IN_LLCEILF:
2723 name = "ceilf";
2724 break;
2725 case BUILT_IN_ICEILL:
2726 case BUILT_IN_LCEILL:
2727 case BUILT_IN_LLCEILL:
2728 name = "ceill";
2729 break;
2730 case BUILT_IN_IFLOOR:
2731 case BUILT_IN_LFLOOR:
2732 case BUILT_IN_LLFLOOR:
2733 name = "floor";
2734 break;
2735 case BUILT_IN_IFLOORF:
2736 case BUILT_IN_LFLOORF:
2737 case BUILT_IN_LLFLOORF:
2738 name = "floorf";
2739 break;
2740 case BUILT_IN_IFLOORL:
2741 case BUILT_IN_LFLOORL:
2742 case BUILT_IN_LLFLOORL:
2743 name = "floorl";
2744 break;
2745 default:
2746 gcc_unreachable ();
2749 fntype = build_function_type_list (TREE_TYPE (arg),
2750 TREE_TYPE (arg), NULL_TREE);
2751 fallback_fndecl = build_fn_decl (name, fntype);
2754 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2756 tmp = expand_normal (exp);
2757 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2759 /* Truncate the result of floating point optab to integer
2760 via expand_fix (). */
2761 target = gen_reg_rtx (mode);
2762 expand_fix (target, tmp, 0);
2764 return target;
2767 /* Expand a call to one of the builtin math functions doing integer
2768 conversion (lrint).
2769 Return 0 if a normal call should be emitted rather than expanding the
2770 function in-line. EXP is the expression that is a call to the builtin
2771 function; if convenient, the result should be placed in TARGET. */
2773 static rtx
2774 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2776 convert_optab builtin_optab;
2777 rtx op0, insns;
2778 tree fndecl = get_callee_fndecl (exp);
2779 tree arg;
2780 enum machine_mode mode;
2781 enum built_in_function fallback_fn = BUILT_IN_NONE;
2783 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2784 gcc_unreachable ();
2786 arg = CALL_EXPR_ARG (exp, 0);
2788 switch (DECL_FUNCTION_CODE (fndecl))
2790 CASE_FLT_FN (BUILT_IN_IRINT):
2791 fallback_fn = BUILT_IN_LRINT;
2792 /* FALLTHRU */
2793 CASE_FLT_FN (BUILT_IN_LRINT):
2794 CASE_FLT_FN (BUILT_IN_LLRINT):
2795 builtin_optab = lrint_optab;
2796 break;
2798 CASE_FLT_FN (BUILT_IN_IROUND):
2799 fallback_fn = BUILT_IN_LROUND;
2800 /* FALLTHRU */
2801 CASE_FLT_FN (BUILT_IN_LROUND):
2802 CASE_FLT_FN (BUILT_IN_LLROUND):
2803 builtin_optab = lround_optab;
2804 break;
2806 default:
2807 gcc_unreachable ();
2810 /* There's no easy way to detect the case we need to set EDOM. */
2811 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2812 return NULL_RTX;
2814 /* Make a suitable register to place result in. */
2815 mode = TYPE_MODE (TREE_TYPE (exp));
2817 /* There's no easy way to detect the case we need to set EDOM. */
2818 if (!flag_errno_math)
2820 rtx result = gen_reg_rtx (mode);
2822 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2823 need to expand the argument again. This way, we will not perform
2824 side-effects more the once. */
2825 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2827 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2829 start_sequence ();
2831 if (expand_sfix_optab (result, op0, builtin_optab))
2833 /* Output the entire sequence. */
2834 insns = get_insns ();
2835 end_sequence ();
2836 emit_insn (insns);
2837 return result;
2840 /* If we were unable to expand via the builtin, stop the sequence
2841 (without outputting the insns) and call to the library function
2842 with the stabilized argument list. */
2843 end_sequence ();
2846 if (fallback_fn != BUILT_IN_NONE)
2848 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2849 targets, (int) round (x) should never be transformed into
2850 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2851 a call to lround in the hope that the target provides at least some
2852 C99 functions. This should result in the best user experience for
2853 not full C99 targets. */
2854 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2855 fallback_fn, 0);
2857 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2858 fallback_fndecl, 1, arg);
2860 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2861 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2862 return convert_to_mode (mode, target, 0);
2865 return expand_call (exp, target, target == const0_rtx);
2868 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2869 a normal call should be emitted rather than expanding the function
2870 in-line. EXP is the expression that is a call to the builtin
2871 function; if convenient, the result should be placed in TARGET. */
2873 static rtx
2874 expand_builtin_powi (tree exp, rtx target)
2876 tree arg0, arg1;
2877 rtx op0, op1;
2878 enum machine_mode mode;
2879 enum machine_mode mode2;
2881 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2882 return NULL_RTX;
2884 arg0 = CALL_EXPR_ARG (exp, 0);
2885 arg1 = CALL_EXPR_ARG (exp, 1);
2886 mode = TYPE_MODE (TREE_TYPE (exp));
2888 /* Emit a libcall to libgcc. */
2890 /* Mode of the 2nd argument must match that of an int. */
2891 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2893 if (target == NULL_RTX)
2894 target = gen_reg_rtx (mode);
2896 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2897 if (GET_MODE (op0) != mode)
2898 op0 = convert_to_mode (mode, op0, 0);
2899 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2900 if (GET_MODE (op1) != mode2)
2901 op1 = convert_to_mode (mode2, op1, 0);
2903 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2904 target, LCT_CONST, mode, 2,
2905 op0, mode, op1, mode2);
2907 return target;
2910 /* Expand expression EXP which is a call to the strlen builtin. Return
2911 NULL_RTX if we failed the caller should emit a normal call, otherwise
2912 try to get the result in TARGET, if convenient. */
2914 static rtx
2915 expand_builtin_strlen (tree exp, rtx target,
2916 enum machine_mode target_mode)
2918 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2919 return NULL_RTX;
2920 else
2922 struct expand_operand ops[4];
2923 rtx pat;
2924 tree len;
2925 tree src = CALL_EXPR_ARG (exp, 0);
2926 rtx src_reg, before_strlen;
2927 enum machine_mode insn_mode = target_mode;
2928 enum insn_code icode = CODE_FOR_nothing;
2929 unsigned int align;
2931 /* If the length can be computed at compile-time, return it. */
2932 len = c_strlen (src, 0);
2933 if (len)
2934 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2936 /* If the length can be computed at compile-time and is constant
2937 integer, but there are side-effects in src, evaluate
2938 src for side-effects, then return len.
2939 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2940 can be optimized into: i++; x = 3; */
2941 len = c_strlen (src, 1);
2942 if (len && TREE_CODE (len) == INTEGER_CST)
2944 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2945 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2948 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2950 /* If SRC is not a pointer type, don't do this operation inline. */
2951 if (align == 0)
2952 return NULL_RTX;
2954 /* Bail out if we can't compute strlen in the right mode. */
2955 while (insn_mode != VOIDmode)
2957 icode = optab_handler (strlen_optab, insn_mode);
2958 if (icode != CODE_FOR_nothing)
2959 break;
2961 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2963 if (insn_mode == VOIDmode)
2964 return NULL_RTX;
2966 /* Make a place to hold the source address. We will not expand
2967 the actual source until we are sure that the expansion will
2968 not fail -- there are trees that cannot be expanded twice. */
2969 src_reg = gen_reg_rtx (Pmode);
2971 /* Mark the beginning of the strlen sequence so we can emit the
2972 source operand later. */
2973 before_strlen = get_last_insn ();
2975 create_output_operand (&ops[0], target, insn_mode);
2976 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2977 create_integer_operand (&ops[2], 0);
2978 create_integer_operand (&ops[3], align);
2979 if (!maybe_expand_insn (icode, 4, ops))
2980 return NULL_RTX;
2982 /* Now that we are assured of success, expand the source. */
2983 start_sequence ();
2984 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2985 if (pat != src_reg)
2987 #ifdef POINTERS_EXTEND_UNSIGNED
2988 if (GET_MODE (pat) != Pmode)
2989 pat = convert_to_mode (Pmode, pat,
2990 POINTERS_EXTEND_UNSIGNED);
2991 #endif
2992 emit_move_insn (src_reg, pat);
2994 pat = get_insns ();
2995 end_sequence ();
2997 if (before_strlen)
2998 emit_insn_after (pat, before_strlen);
2999 else
3000 emit_insn_before (pat, get_insns ());
3002 /* Return the value in the proper mode for this function. */
3003 if (GET_MODE (ops[0].value) == target_mode)
3004 target = ops[0].value;
3005 else if (target != 0)
3006 convert_move (target, ops[0].value, 0);
3007 else
3008 target = convert_to_mode (target_mode, ops[0].value, 0);
3010 return target;
3014 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3015 bytes from constant string DATA + OFFSET and return it as target
3016 constant. */
3018 static rtx
3019 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3020 enum machine_mode mode)
3022 const char *str = (const char *) data;
3024 gcc_assert (offset >= 0
3025 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3026 <= strlen (str) + 1));
3028 return c_readstr (str + offset, mode);
3031 /* Expand a call EXP to the memcpy builtin.
3032 Return NULL_RTX if we failed, the caller should emit a normal call,
3033 otherwise try to get the result in TARGET, if convenient (and in
3034 mode MODE if that's convenient). */
3036 static rtx
3037 expand_builtin_memcpy (tree exp, rtx target)
3039 if (!validate_arglist (exp,
3040 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3041 return NULL_RTX;
3042 else
3044 tree dest = CALL_EXPR_ARG (exp, 0);
3045 tree src = CALL_EXPR_ARG (exp, 1);
3046 tree len = CALL_EXPR_ARG (exp, 2);
3047 const char *src_str;
3048 unsigned int src_align = get_pointer_alignment (src);
3049 unsigned int dest_align = get_pointer_alignment (dest);
3050 rtx dest_mem, src_mem, dest_addr, len_rtx;
3051 HOST_WIDE_INT expected_size = -1;
3052 unsigned int expected_align = 0;
3054 /* If DEST is not a pointer type, call the normal function. */
3055 if (dest_align == 0)
3056 return NULL_RTX;
3058 /* If either SRC is not a pointer type, don't do this
3059 operation in-line. */
3060 if (src_align == 0)
3061 return NULL_RTX;
3063 if (currently_expanding_gimple_stmt)
3064 stringop_block_profile (currently_expanding_gimple_stmt,
3065 &expected_align, &expected_size);
3067 if (expected_align < dest_align)
3068 expected_align = dest_align;
3069 dest_mem = get_memory_rtx (dest, len);
3070 set_mem_align (dest_mem, dest_align);
3071 len_rtx = expand_normal (len);
3072 src_str = c_getstr (src);
3074 /* If SRC is a string constant and block move would be done
3075 by pieces, we can avoid loading the string from memory
3076 and only stored the computed constants. */
3077 if (src_str
3078 && CONST_INT_P (len_rtx)
3079 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3080 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3081 CONST_CAST (char *, src_str),
3082 dest_align, false))
3084 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3085 builtin_memcpy_read_str,
3086 CONST_CAST (char *, src_str),
3087 dest_align, false, 0);
3088 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3089 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3090 return dest_mem;
3093 src_mem = get_memory_rtx (src, len);
3094 set_mem_align (src_mem, src_align);
3096 /* Copy word part most expediently. */
3097 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3098 CALL_EXPR_TAILCALL (exp)
3099 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3100 expected_align, expected_size);
3102 if (dest_addr == 0)
3104 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3105 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3107 return dest_addr;
3111 /* Expand a call EXP to the mempcpy builtin.
3112 Return NULL_RTX if we failed; the caller should emit a normal call,
3113 otherwise try to get the result in TARGET, if convenient (and in
3114 mode MODE if that's convenient). If ENDP is 0 return the
3115 destination pointer, if ENDP is 1 return the end pointer ala
3116 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3117 stpcpy. */
3119 static rtx
3120 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3122 if (!validate_arglist (exp,
3123 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3124 return NULL_RTX;
3125 else
3127 tree dest = CALL_EXPR_ARG (exp, 0);
3128 tree src = CALL_EXPR_ARG (exp, 1);
3129 tree len = CALL_EXPR_ARG (exp, 2);
3130 return expand_builtin_mempcpy_args (dest, src, len,
3131 target, mode, /*endp=*/ 1);
3135 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3136 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3137 so that this can also be called without constructing an actual CALL_EXPR.
3138 The other arguments and return value are the same as for
3139 expand_builtin_mempcpy. */
3141 static rtx
3142 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3143 rtx target, enum machine_mode mode, int endp)
3145 /* If return value is ignored, transform mempcpy into memcpy. */
3146 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3148 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3149 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3150 dest, src, len);
3151 return expand_expr (result, target, mode, EXPAND_NORMAL);
3153 else
3155 const char *src_str;
3156 unsigned int src_align = get_pointer_alignment (src);
3157 unsigned int dest_align = get_pointer_alignment (dest);
3158 rtx dest_mem, src_mem, len_rtx;
3160 /* If either SRC or DEST is not a pointer type, don't do this
3161 operation in-line. */
3162 if (dest_align == 0 || src_align == 0)
3163 return NULL_RTX;
3165 /* If LEN is not constant, call the normal function. */
3166 if (! host_integerp (len, 1))
3167 return NULL_RTX;
3169 len_rtx = expand_normal (len);
3170 src_str = c_getstr (src);
3172 /* If SRC is a string constant and block move would be done
3173 by pieces, we can avoid loading the string from memory
3174 and only stored the computed constants. */
3175 if (src_str
3176 && CONST_INT_P (len_rtx)
3177 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3178 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3179 CONST_CAST (char *, src_str),
3180 dest_align, false))
3182 dest_mem = get_memory_rtx (dest, len);
3183 set_mem_align (dest_mem, dest_align);
3184 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3185 builtin_memcpy_read_str,
3186 CONST_CAST (char *, src_str),
3187 dest_align, false, endp);
3188 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3189 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3190 return dest_mem;
3193 if (CONST_INT_P (len_rtx)
3194 && can_move_by_pieces (INTVAL (len_rtx),
3195 MIN (dest_align, src_align)))
3197 dest_mem = get_memory_rtx (dest, len);
3198 set_mem_align (dest_mem, dest_align);
3199 src_mem = get_memory_rtx (src, len);
3200 set_mem_align (src_mem, src_align);
3201 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3202 MIN (dest_align, src_align), endp);
3203 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3204 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3205 return dest_mem;
3208 return NULL_RTX;
3212 #ifndef HAVE_movstr
3213 # define HAVE_movstr 0
3214 # define CODE_FOR_movstr CODE_FOR_nothing
3215 #endif
3217 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3218 we failed, the caller should emit a normal call, otherwise try to
3219 get the result in TARGET, if convenient. If ENDP is 0 return the
3220 destination pointer, if ENDP is 1 return the end pointer ala
3221 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3222 stpcpy. */
3224 static rtx
3225 expand_movstr (tree dest, tree src, rtx target, int endp)
3227 struct expand_operand ops[3];
3228 rtx dest_mem;
3229 rtx src_mem;
3231 if (!HAVE_movstr)
3232 return NULL_RTX;
3234 dest_mem = get_memory_rtx (dest, NULL);
3235 src_mem = get_memory_rtx (src, NULL);
3236 if (!endp)
3238 target = force_reg (Pmode, XEXP (dest_mem, 0));
3239 dest_mem = replace_equiv_address (dest_mem, target);
3242 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3243 create_fixed_operand (&ops[1], dest_mem);
3244 create_fixed_operand (&ops[2], src_mem);
3245 expand_insn (CODE_FOR_movstr, 3, ops);
3247 if (endp && target != const0_rtx)
3249 target = ops[0].value;
3250 /* movstr is supposed to set end to the address of the NUL
3251 terminator. If the caller requested a mempcpy-like return value,
3252 adjust it. */
3253 if (endp == 1)
3255 rtx tem = plus_constant (GET_MODE (target),
3256 gen_lowpart (GET_MODE (target), target), 1);
3257 emit_move_insn (target, force_operand (tem, NULL_RTX));
3260 return target;
3263 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3264 NULL_RTX if we failed the caller should emit a normal call, otherwise
3265 try to get the result in TARGET, if convenient (and in mode MODE if that's
3266 convenient). */
3268 static rtx
3269 expand_builtin_strcpy (tree exp, rtx target)
3271 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3273 tree dest = CALL_EXPR_ARG (exp, 0);
3274 tree src = CALL_EXPR_ARG (exp, 1);
3275 return expand_builtin_strcpy_args (dest, src, target);
3277 return NULL_RTX;
3280 /* Helper function to do the actual work for expand_builtin_strcpy. The
3281 arguments to the builtin_strcpy call DEST and SRC are broken out
3282 so that this can also be called without constructing an actual CALL_EXPR.
3283 The other arguments and return value are the same as for
3284 expand_builtin_strcpy. */
3286 static rtx
3287 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3289 return expand_movstr (dest, src, target, /*endp=*/0);
3292 /* Expand a call EXP to the stpcpy builtin.
3293 Return NULL_RTX if we failed the caller should emit a normal call,
3294 otherwise try to get the result in TARGET, if convenient (and in
3295 mode MODE if that's convenient). */
3297 static rtx
3298 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3300 tree dst, src;
3301 location_t loc = EXPR_LOCATION (exp);
3303 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3304 return NULL_RTX;
3306 dst = CALL_EXPR_ARG (exp, 0);
3307 src = CALL_EXPR_ARG (exp, 1);
3309 /* If return value is ignored, transform stpcpy into strcpy. */
3310 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3312 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3313 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3314 return expand_expr (result, target, mode, EXPAND_NORMAL);
3316 else
3318 tree len, lenp1;
3319 rtx ret;
3321 /* Ensure we get an actual string whose length can be evaluated at
3322 compile-time, not an expression containing a string. This is
3323 because the latter will potentially produce pessimized code
3324 when used to produce the return value. */
3325 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3326 return expand_movstr (dst, src, target, /*endp=*/2);
3328 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3329 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3330 target, mode, /*endp=*/2);
3332 if (ret)
3333 return ret;
3335 if (TREE_CODE (len) == INTEGER_CST)
3337 rtx len_rtx = expand_normal (len);
3339 if (CONST_INT_P (len_rtx))
3341 ret = expand_builtin_strcpy_args (dst, src, target);
3343 if (ret)
3345 if (! target)
3347 if (mode != VOIDmode)
3348 target = gen_reg_rtx (mode);
3349 else
3350 target = gen_reg_rtx (GET_MODE (ret));
3352 if (GET_MODE (target) != GET_MODE (ret))
3353 ret = gen_lowpart (GET_MODE (target), ret);
3355 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3356 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3357 gcc_assert (ret);
3359 return target;
3364 return expand_movstr (dst, src, target, /*endp=*/2);
3368 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3369 bytes from constant string DATA + OFFSET and return it as target
3370 constant. */
3373 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3374 enum machine_mode mode)
3376 const char *str = (const char *) data;
3378 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3379 return const0_rtx;
3381 return c_readstr (str + offset, mode);
3384 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3385 NULL_RTX if we failed the caller should emit a normal call. */
3387 static rtx
3388 expand_builtin_strncpy (tree exp, rtx target)
3390 location_t loc = EXPR_LOCATION (exp);
3392 if (validate_arglist (exp,
3393 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3395 tree dest = CALL_EXPR_ARG (exp, 0);
3396 tree src = CALL_EXPR_ARG (exp, 1);
3397 tree len = CALL_EXPR_ARG (exp, 2);
3398 tree slen = c_strlen (src, 1);
3400 /* We must be passed a constant len and src parameter. */
3401 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3402 return NULL_RTX;
3404 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3406 /* We're required to pad with trailing zeros if the requested
3407 len is greater than strlen(s2)+1. In that case try to
3408 use store_by_pieces, if it fails, punt. */
3409 if (tree_int_cst_lt (slen, len))
3411 unsigned int dest_align = get_pointer_alignment (dest);
3412 const char *p = c_getstr (src);
3413 rtx dest_mem;
3415 if (!p || dest_align == 0 || !host_integerp (len, 1)
3416 || !can_store_by_pieces (tree_low_cst (len, 1),
3417 builtin_strncpy_read_str,
3418 CONST_CAST (char *, p),
3419 dest_align, false))
3420 return NULL_RTX;
3422 dest_mem = get_memory_rtx (dest, len);
3423 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3424 builtin_strncpy_read_str,
3425 CONST_CAST (char *, p), dest_align, false, 0);
3426 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3427 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3428 return dest_mem;
3431 return NULL_RTX;
3434 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3435 bytes from constant string DATA + OFFSET and return it as target
3436 constant. */
3439 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3440 enum machine_mode mode)
3442 const char *c = (const char *) data;
3443 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3445 memset (p, *c, GET_MODE_SIZE (mode));
3447 return c_readstr (p, mode);
3450 /* Callback routine for store_by_pieces. Return the RTL of a register
3451 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3452 char value given in the RTL register data. For example, if mode is
3453 4 bytes wide, return the RTL for 0x01010101*data. */
3455 static rtx
3456 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3457 enum machine_mode mode)
3459 rtx target, coeff;
3460 size_t size;
3461 char *p;
3463 size = GET_MODE_SIZE (mode);
3464 if (size == 1)
3465 return (rtx) data;
3467 p = XALLOCAVEC (char, size);
3468 memset (p, 1, size);
3469 coeff = c_readstr (p, mode);
3471 target = convert_to_mode (mode, (rtx) data, 1);
3472 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3473 return force_reg (mode, target);
3476 /* Expand expression EXP, which is a call to the memset builtin. Return
3477 NULL_RTX if we failed the caller should emit a normal call, otherwise
3478 try to get the result in TARGET, if convenient (and in mode MODE if that's
3479 convenient). */
3481 static rtx
3482 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3484 if (!validate_arglist (exp,
3485 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3486 return NULL_RTX;
3487 else
3489 tree dest = CALL_EXPR_ARG (exp, 0);
3490 tree val = CALL_EXPR_ARG (exp, 1);
3491 tree len = CALL_EXPR_ARG (exp, 2);
3492 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3496 /* Helper function to do the actual work for expand_builtin_memset. The
3497 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3498 so that this can also be called without constructing an actual CALL_EXPR.
3499 The other arguments and return value are the same as for
3500 expand_builtin_memset. */
3502 static rtx
3503 expand_builtin_memset_args (tree dest, tree val, tree len,
3504 rtx target, enum machine_mode mode, tree orig_exp)
3506 tree fndecl, fn;
3507 enum built_in_function fcode;
3508 enum machine_mode val_mode;
3509 char c;
3510 unsigned int dest_align;
3511 rtx dest_mem, dest_addr, len_rtx;
3512 HOST_WIDE_INT expected_size = -1;
3513 unsigned int expected_align = 0;
3515 dest_align = get_pointer_alignment (dest);
3517 /* If DEST is not a pointer type, don't do this operation in-line. */
3518 if (dest_align == 0)
3519 return NULL_RTX;
3521 if (currently_expanding_gimple_stmt)
3522 stringop_block_profile (currently_expanding_gimple_stmt,
3523 &expected_align, &expected_size);
3525 if (expected_align < dest_align)
3526 expected_align = dest_align;
3528 /* If the LEN parameter is zero, return DEST. */
3529 if (integer_zerop (len))
3531 /* Evaluate and ignore VAL in case it has side-effects. */
3532 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3533 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3536 /* Stabilize the arguments in case we fail. */
3537 dest = builtin_save_expr (dest);
3538 val = builtin_save_expr (val);
3539 len = builtin_save_expr (len);
3541 len_rtx = expand_normal (len);
3542 dest_mem = get_memory_rtx (dest, len);
3543 val_mode = TYPE_MODE (unsigned_char_type_node);
3545 if (TREE_CODE (val) != INTEGER_CST)
3547 rtx val_rtx;
3549 val_rtx = expand_normal (val);
3550 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3552 /* Assume that we can memset by pieces if we can store
3553 * the coefficients by pieces (in the required modes).
3554 * We can't pass builtin_memset_gen_str as that emits RTL. */
3555 c = 1;
3556 if (host_integerp (len, 1)
3557 && can_store_by_pieces (tree_low_cst (len, 1),
3558 builtin_memset_read_str, &c, dest_align,
3559 true))
3561 val_rtx = force_reg (val_mode, val_rtx);
3562 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3563 builtin_memset_gen_str, val_rtx, dest_align,
3564 true, 0);
3566 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3567 dest_align, expected_align,
3568 expected_size))
3569 goto do_libcall;
3571 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3572 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3573 return dest_mem;
3576 if (target_char_cast (val, &c))
3577 goto do_libcall;
3579 if (c)
3581 if (host_integerp (len, 1)
3582 && can_store_by_pieces (tree_low_cst (len, 1),
3583 builtin_memset_read_str, &c, dest_align,
3584 true))
3585 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3586 builtin_memset_read_str, &c, dest_align, true, 0);
3587 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3588 gen_int_mode (c, val_mode),
3589 dest_align, expected_align,
3590 expected_size))
3591 goto do_libcall;
3593 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3594 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3595 return dest_mem;
3598 set_mem_align (dest_mem, dest_align);
3599 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3600 CALL_EXPR_TAILCALL (orig_exp)
3601 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3602 expected_align, expected_size);
3604 if (dest_addr == 0)
3606 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3607 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3610 return dest_addr;
3612 do_libcall:
3613 fndecl = get_callee_fndecl (orig_exp);
3614 fcode = DECL_FUNCTION_CODE (fndecl);
3615 if (fcode == BUILT_IN_MEMSET)
3616 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3617 dest, val, len);
3618 else if (fcode == BUILT_IN_BZERO)
3619 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3620 dest, len);
3621 else
3622 gcc_unreachable ();
3623 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3624 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3625 return expand_call (fn, target, target == const0_rtx);
3628 /* Expand expression EXP, which is a call to the bzero builtin. Return
3629 NULL_RTX if we failed the caller should emit a normal call. */
3631 static rtx
3632 expand_builtin_bzero (tree exp)
3634 tree dest, size;
3635 location_t loc = EXPR_LOCATION (exp);
3637 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3638 return NULL_RTX;
3640 dest = CALL_EXPR_ARG (exp, 0);
3641 size = CALL_EXPR_ARG (exp, 1);
3643 /* New argument list transforming bzero(ptr x, int y) to
3644 memset(ptr x, int 0, size_t y). This is done this way
3645 so that if it isn't expanded inline, we fallback to
3646 calling bzero instead of memset. */
3648 return expand_builtin_memset_args (dest, integer_zero_node,
3649 fold_convert_loc (loc,
3650 size_type_node, size),
3651 const0_rtx, VOIDmode, exp);
3654 /* Expand expression EXP, which is a call to the memcmp built-in function.
3655 Return NULL_RTX if we failed and the caller should emit a normal call,
3656 otherwise try to get the result in TARGET, if convenient (and in mode
3657 MODE, if that's convenient). */
3659 static rtx
3660 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3661 ATTRIBUTE_UNUSED enum machine_mode mode)
3663 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3665 if (!validate_arglist (exp,
3666 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3667 return NULL_RTX;
3669 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3670 implementing memcmp because it will stop if it encounters two
3671 zero bytes. */
3672 #if defined HAVE_cmpmemsi
3674 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3675 rtx result;
3676 rtx insn;
3677 tree arg1 = CALL_EXPR_ARG (exp, 0);
3678 tree arg2 = CALL_EXPR_ARG (exp, 1);
3679 tree len = CALL_EXPR_ARG (exp, 2);
3681 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3682 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3683 enum machine_mode insn_mode;
3685 if (HAVE_cmpmemsi)
3686 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3687 else
3688 return NULL_RTX;
3690 /* If we don't have POINTER_TYPE, call the function. */
3691 if (arg1_align == 0 || arg2_align == 0)
3692 return NULL_RTX;
3694 /* Make a place to write the result of the instruction. */
3695 result = target;
3696 if (! (result != 0
3697 && REG_P (result) && GET_MODE (result) == insn_mode
3698 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3699 result = gen_reg_rtx (insn_mode);
3701 arg1_rtx = get_memory_rtx (arg1, len);
3702 arg2_rtx = get_memory_rtx (arg2, len);
3703 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3705 /* Set MEM_SIZE as appropriate. */
3706 if (CONST_INT_P (arg3_rtx))
3708 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3709 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3712 if (HAVE_cmpmemsi)
3713 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3714 GEN_INT (MIN (arg1_align, arg2_align)));
3715 else
3716 gcc_unreachable ();
3718 if (insn)
3719 emit_insn (insn);
3720 else
3721 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3722 TYPE_MODE (integer_type_node), 3,
3723 XEXP (arg1_rtx, 0), Pmode,
3724 XEXP (arg2_rtx, 0), Pmode,
3725 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3726 TYPE_UNSIGNED (sizetype)),
3727 TYPE_MODE (sizetype));
3729 /* Return the value in the proper mode for this function. */
3730 mode = TYPE_MODE (TREE_TYPE (exp));
3731 if (GET_MODE (result) == mode)
3732 return result;
3733 else if (target != 0)
3735 convert_move (target, result, 0);
3736 return target;
3738 else
3739 return convert_to_mode (mode, result, 0);
3741 #endif /* HAVE_cmpmemsi. */
3743 return NULL_RTX;
3746 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3747 if we failed the caller should emit a normal call, otherwise try to get
3748 the result in TARGET, if convenient. */
3750 static rtx
3751 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3753 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3754 return NULL_RTX;
3756 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3757 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3758 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3760 rtx arg1_rtx, arg2_rtx;
3761 rtx result, insn = NULL_RTX;
3762 tree fndecl, fn;
3763 tree arg1 = CALL_EXPR_ARG (exp, 0);
3764 tree arg2 = CALL_EXPR_ARG (exp, 1);
3766 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3767 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3769 /* If we don't have POINTER_TYPE, call the function. */
3770 if (arg1_align == 0 || arg2_align == 0)
3771 return NULL_RTX;
3773 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3774 arg1 = builtin_save_expr (arg1);
3775 arg2 = builtin_save_expr (arg2);
3777 arg1_rtx = get_memory_rtx (arg1, NULL);
3778 arg2_rtx = get_memory_rtx (arg2, NULL);
3780 #ifdef HAVE_cmpstrsi
3781 /* Try to call cmpstrsi. */
3782 if (HAVE_cmpstrsi)
3784 enum machine_mode insn_mode
3785 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3787 /* Make a place to write the result of the instruction. */
3788 result = target;
3789 if (! (result != 0
3790 && REG_P (result) && GET_MODE (result) == insn_mode
3791 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3792 result = gen_reg_rtx (insn_mode);
3794 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3795 GEN_INT (MIN (arg1_align, arg2_align)));
3797 #endif
3798 #ifdef HAVE_cmpstrnsi
3799 /* Try to determine at least one length and call cmpstrnsi. */
3800 if (!insn && HAVE_cmpstrnsi)
3802 tree len;
3803 rtx arg3_rtx;
3805 enum machine_mode insn_mode
3806 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3807 tree len1 = c_strlen (arg1, 1);
3808 tree len2 = c_strlen (arg2, 1);
3810 if (len1)
3811 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3812 if (len2)
3813 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3815 /* If we don't have a constant length for the first, use the length
3816 of the second, if we know it. We don't require a constant for
3817 this case; some cost analysis could be done if both are available
3818 but neither is constant. For now, assume they're equally cheap,
3819 unless one has side effects. If both strings have constant lengths,
3820 use the smaller. */
3822 if (!len1)
3823 len = len2;
3824 else if (!len2)
3825 len = len1;
3826 else if (TREE_SIDE_EFFECTS (len1))
3827 len = len2;
3828 else if (TREE_SIDE_EFFECTS (len2))
3829 len = len1;
3830 else if (TREE_CODE (len1) != INTEGER_CST)
3831 len = len2;
3832 else if (TREE_CODE (len2) != INTEGER_CST)
3833 len = len1;
3834 else if (tree_int_cst_lt (len1, len2))
3835 len = len1;
3836 else
3837 len = len2;
3839 /* If both arguments have side effects, we cannot optimize. */
3840 if (!len || TREE_SIDE_EFFECTS (len))
3841 goto do_libcall;
3843 arg3_rtx = expand_normal (len);
3845 /* Make a place to write the result of the instruction. */
3846 result = target;
3847 if (! (result != 0
3848 && REG_P (result) && GET_MODE (result) == insn_mode
3849 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3850 result = gen_reg_rtx (insn_mode);
3852 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3853 GEN_INT (MIN (arg1_align, arg2_align)));
3855 #endif
3857 if (insn)
3859 enum machine_mode mode;
3860 emit_insn (insn);
3862 /* Return the value in the proper mode for this function. */
3863 mode = TYPE_MODE (TREE_TYPE (exp));
3864 if (GET_MODE (result) == mode)
3865 return result;
3866 if (target == 0)
3867 return convert_to_mode (mode, result, 0);
3868 convert_move (target, result, 0);
3869 return target;
3872 /* Expand the library call ourselves using a stabilized argument
3873 list to avoid re-evaluating the function's arguments twice. */
3874 #ifdef HAVE_cmpstrnsi
3875 do_libcall:
3876 #endif
3877 fndecl = get_callee_fndecl (exp);
3878 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3879 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3880 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3881 return expand_call (fn, target, target == const0_rtx);
3883 #endif
3884 return NULL_RTX;
3887 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3888 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3889 the result in TARGET, if convenient. */
3891 static rtx
3892 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3893 ATTRIBUTE_UNUSED enum machine_mode mode)
3895 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3897 if (!validate_arglist (exp,
3898 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3899 return NULL_RTX;
3901 /* If c_strlen can determine an expression for one of the string
3902 lengths, and it doesn't have side effects, then emit cmpstrnsi
3903 using length MIN(strlen(string)+1, arg3). */
3904 #ifdef HAVE_cmpstrnsi
3905 if (HAVE_cmpstrnsi)
3907 tree len, len1, len2;
3908 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3909 rtx result, insn;
3910 tree fndecl, fn;
3911 tree arg1 = CALL_EXPR_ARG (exp, 0);
3912 tree arg2 = CALL_EXPR_ARG (exp, 1);
3913 tree arg3 = CALL_EXPR_ARG (exp, 2);
3915 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3916 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3917 enum machine_mode insn_mode
3918 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3920 len1 = c_strlen (arg1, 1);
3921 len2 = c_strlen (arg2, 1);
3923 if (len1)
3924 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3925 if (len2)
3926 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3928 /* If we don't have a constant length for the first, use the length
3929 of the second, if we know it. We don't require a constant for
3930 this case; some cost analysis could be done if both are available
3931 but neither is constant. For now, assume they're equally cheap,
3932 unless one has side effects. If both strings have constant lengths,
3933 use the smaller. */
3935 if (!len1)
3936 len = len2;
3937 else if (!len2)
3938 len = len1;
3939 else if (TREE_SIDE_EFFECTS (len1))
3940 len = len2;
3941 else if (TREE_SIDE_EFFECTS (len2))
3942 len = len1;
3943 else if (TREE_CODE (len1) != INTEGER_CST)
3944 len = len2;
3945 else if (TREE_CODE (len2) != INTEGER_CST)
3946 len = len1;
3947 else if (tree_int_cst_lt (len1, len2))
3948 len = len1;
3949 else
3950 len = len2;
3952 /* If both arguments have side effects, we cannot optimize. */
3953 if (!len || TREE_SIDE_EFFECTS (len))
3954 return NULL_RTX;
3956 /* The actual new length parameter is MIN(len,arg3). */
3957 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3958 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3960 /* If we don't have POINTER_TYPE, call the function. */
3961 if (arg1_align == 0 || arg2_align == 0)
3962 return NULL_RTX;
3964 /* Make a place to write the result of the instruction. */
3965 result = target;
3966 if (! (result != 0
3967 && REG_P (result) && GET_MODE (result) == insn_mode
3968 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3969 result = gen_reg_rtx (insn_mode);
3971 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3972 arg1 = builtin_save_expr (arg1);
3973 arg2 = builtin_save_expr (arg2);
3974 len = builtin_save_expr (len);
3976 arg1_rtx = get_memory_rtx (arg1, len);
3977 arg2_rtx = get_memory_rtx (arg2, len);
3978 arg3_rtx = expand_normal (len);
3979 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3980 GEN_INT (MIN (arg1_align, arg2_align)));
3981 if (insn)
3983 emit_insn (insn);
3985 /* Return the value in the proper mode for this function. */
3986 mode = TYPE_MODE (TREE_TYPE (exp));
3987 if (GET_MODE (result) == mode)
3988 return result;
3989 if (target == 0)
3990 return convert_to_mode (mode, result, 0);
3991 convert_move (target, result, 0);
3992 return target;
3995 /* Expand the library call ourselves using a stabilized argument
3996 list to avoid re-evaluating the function's arguments twice. */
3997 fndecl = get_callee_fndecl (exp);
3998 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
3999 arg1, arg2, len);
4000 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4001 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4002 return expand_call (fn, target, target == const0_rtx);
4004 #endif
4005 return NULL_RTX;
4008 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4009 if that's convenient. */
4012 expand_builtin_saveregs (void)
4014 rtx val, seq;
4016 /* Don't do __builtin_saveregs more than once in a function.
4017 Save the result of the first call and reuse it. */
4018 if (saveregs_value != 0)
4019 return saveregs_value;
4021 /* When this function is called, it means that registers must be
4022 saved on entry to this function. So we migrate the call to the
4023 first insn of this function. */
4025 start_sequence ();
4027 /* Do whatever the machine needs done in this case. */
4028 val = targetm.calls.expand_builtin_saveregs ();
4030 seq = get_insns ();
4031 end_sequence ();
4033 saveregs_value = val;
4035 /* Put the insns after the NOTE that starts the function. If this
4036 is inside a start_sequence, make the outer-level insn chain current, so
4037 the code is placed at the start of the function. */
4038 push_topmost_sequence ();
4039 emit_insn_after (seq, entry_of_function ());
4040 pop_topmost_sequence ();
4042 return val;
4045 /* Expand a call to __builtin_next_arg. */
4047 static rtx
4048 expand_builtin_next_arg (void)
4050 /* Checking arguments is already done in fold_builtin_next_arg
4051 that must be called before this function. */
4052 return expand_binop (ptr_mode, add_optab,
4053 crtl->args.internal_arg_pointer,
4054 crtl->args.arg_offset_rtx,
4055 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4058 /* Make it easier for the backends by protecting the valist argument
4059 from multiple evaluations. */
4061 static tree
4062 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4064 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4066 /* The current way of determining the type of valist is completely
4067 bogus. We should have the information on the va builtin instead. */
4068 if (!vatype)
4069 vatype = targetm.fn_abi_va_list (cfun->decl);
4071 if (TREE_CODE (vatype) == ARRAY_TYPE)
4073 if (TREE_SIDE_EFFECTS (valist))
4074 valist = save_expr (valist);
4076 /* For this case, the backends will be expecting a pointer to
4077 vatype, but it's possible we've actually been given an array
4078 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4079 So fix it. */
4080 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4082 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4083 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4086 else
4088 tree pt = build_pointer_type (vatype);
4090 if (! needs_lvalue)
4092 if (! TREE_SIDE_EFFECTS (valist))
4093 return valist;
4095 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4096 TREE_SIDE_EFFECTS (valist) = 1;
4099 if (TREE_SIDE_EFFECTS (valist))
4100 valist = save_expr (valist);
4101 valist = fold_build2_loc (loc, MEM_REF,
4102 vatype, valist, build_int_cst (pt, 0));
4105 return valist;
4108 /* The "standard" definition of va_list is void*. */
4110 tree
4111 std_build_builtin_va_list (void)
4113 return ptr_type_node;
4116 /* The "standard" abi va_list is va_list_type_node. */
4118 tree
4119 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4121 return va_list_type_node;
4124 /* The "standard" type of va_list is va_list_type_node. */
4126 tree
4127 std_canonical_va_list_type (tree type)
4129 tree wtype, htype;
4131 if (INDIRECT_REF_P (type))
4132 type = TREE_TYPE (type);
4133 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4134 type = TREE_TYPE (type);
4135 wtype = va_list_type_node;
4136 htype = type;
4137 /* Treat structure va_list types. */
4138 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4139 htype = TREE_TYPE (htype);
4140 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4142 /* If va_list is an array type, the argument may have decayed
4143 to a pointer type, e.g. by being passed to another function.
4144 In that case, unwrap both types so that we can compare the
4145 underlying records. */
4146 if (TREE_CODE (htype) == ARRAY_TYPE
4147 || POINTER_TYPE_P (htype))
4149 wtype = TREE_TYPE (wtype);
4150 htype = TREE_TYPE (htype);
4153 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4154 return va_list_type_node;
4156 return NULL_TREE;
4159 /* The "standard" implementation of va_start: just assign `nextarg' to
4160 the variable. */
4162 void
4163 std_expand_builtin_va_start (tree valist, rtx nextarg)
4165 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4166 convert_move (va_r, nextarg, 0);
4169 /* Expand EXP, a call to __builtin_va_start. */
4171 static rtx
4172 expand_builtin_va_start (tree exp)
4174 rtx nextarg;
4175 tree valist;
4176 location_t loc = EXPR_LOCATION (exp);
4178 if (call_expr_nargs (exp) < 2)
4180 error_at (loc, "too few arguments to function %<va_start%>");
4181 return const0_rtx;
4184 if (fold_builtin_next_arg (exp, true))
4185 return const0_rtx;
4187 nextarg = expand_builtin_next_arg ();
4188 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4190 if (targetm.expand_builtin_va_start)
4191 targetm.expand_builtin_va_start (valist, nextarg);
4192 else
4193 std_expand_builtin_va_start (valist, nextarg);
4195 return const0_rtx;
4198 /* The "standard" implementation of va_arg: read the value from the
4199 current (padded) address and increment by the (padded) size. */
4201 tree
4202 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4203 gimple_seq *post_p)
4205 tree addr, t, type_size, rounded_size, valist_tmp;
4206 unsigned HOST_WIDE_INT align, boundary;
4207 bool indirect;
4209 #ifdef ARGS_GROW_DOWNWARD
4210 /* All of the alignment and movement below is for args-grow-up machines.
4211 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4212 implement their own specialized gimplify_va_arg_expr routines. */
4213 gcc_unreachable ();
4214 #endif
4216 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4217 if (indirect)
4218 type = build_pointer_type (type);
4220 align = PARM_BOUNDARY / BITS_PER_UNIT;
4221 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4223 /* When we align parameter on stack for caller, if the parameter
4224 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4225 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4226 here with caller. */
4227 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4228 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4230 boundary /= BITS_PER_UNIT;
4232 /* Hoist the valist value into a temporary for the moment. */
4233 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4235 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4236 requires greater alignment, we must perform dynamic alignment. */
4237 if (boundary > align
4238 && !integer_zerop (TYPE_SIZE (type)))
4240 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4241 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4242 gimplify_and_add (t, pre_p);
4244 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4245 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4246 valist_tmp,
4247 build_int_cst (TREE_TYPE (valist), -boundary)));
4248 gimplify_and_add (t, pre_p);
4250 else
4251 boundary = align;
4253 /* If the actual alignment is less than the alignment of the type,
4254 adjust the type accordingly so that we don't assume strict alignment
4255 when dereferencing the pointer. */
4256 boundary *= BITS_PER_UNIT;
4257 if (boundary < TYPE_ALIGN (type))
4259 type = build_variant_type_copy (type);
4260 TYPE_ALIGN (type) = boundary;
4263 /* Compute the rounded size of the type. */
4264 type_size = size_in_bytes (type);
4265 rounded_size = round_up (type_size, align);
4267 /* Reduce rounded_size so it's sharable with the postqueue. */
4268 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4270 /* Get AP. */
4271 addr = valist_tmp;
4272 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4274 /* Small args are padded downward. */
4275 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4276 rounded_size, size_int (align));
4277 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4278 size_binop (MINUS_EXPR, rounded_size, type_size));
4279 addr = fold_build_pointer_plus (addr, t);
4282 /* Compute new value for AP. */
4283 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4284 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4285 gimplify_and_add (t, pre_p);
4287 addr = fold_convert (build_pointer_type (type), addr);
4289 if (indirect)
4290 addr = build_va_arg_indirect_ref (addr);
4292 return build_va_arg_indirect_ref (addr);
4295 /* Build an indirect-ref expression over the given TREE, which represents a
4296 piece of a va_arg() expansion. */
4297 tree
4298 build_va_arg_indirect_ref (tree addr)
4300 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4302 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4303 mf_mark (addr);
4305 return addr;
4308 /* Return a dummy expression of type TYPE in order to keep going after an
4309 error. */
4311 static tree
4312 dummy_object (tree type)
4314 tree t = build_int_cst (build_pointer_type (type), 0);
4315 return build2 (MEM_REF, type, t, t);
4318 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4319 builtin function, but a very special sort of operator. */
4321 enum gimplify_status
4322 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4324 tree promoted_type, have_va_type;
4325 tree valist = TREE_OPERAND (*expr_p, 0);
4326 tree type = TREE_TYPE (*expr_p);
4327 tree t;
4328 location_t loc = EXPR_LOCATION (*expr_p);
4330 /* Verify that valist is of the proper type. */
4331 have_va_type = TREE_TYPE (valist);
4332 if (have_va_type == error_mark_node)
4333 return GS_ERROR;
4334 have_va_type = targetm.canonical_va_list_type (have_va_type);
4336 if (have_va_type == NULL_TREE)
4338 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4339 return GS_ERROR;
4342 /* Generate a diagnostic for requesting data of a type that cannot
4343 be passed through `...' due to type promotion at the call site. */
4344 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4345 != type)
4347 static bool gave_help;
4348 bool warned;
4350 /* Unfortunately, this is merely undefined, rather than a constraint
4351 violation, so we cannot make this an error. If this call is never
4352 executed, the program is still strictly conforming. */
4353 warned = warning_at (loc, 0,
4354 "%qT is promoted to %qT when passed through %<...%>",
4355 type, promoted_type);
4356 if (!gave_help && warned)
4358 gave_help = true;
4359 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4360 promoted_type, type);
4363 /* We can, however, treat "undefined" any way we please.
4364 Call abort to encourage the user to fix the program. */
4365 if (warned)
4366 inform (loc, "if this code is reached, the program will abort");
4367 /* Before the abort, allow the evaluation of the va_list
4368 expression to exit or longjmp. */
4369 gimplify_and_add (valist, pre_p);
4370 t = build_call_expr_loc (loc,
4371 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4372 gimplify_and_add (t, pre_p);
4374 /* This is dead code, but go ahead and finish so that the
4375 mode of the result comes out right. */
4376 *expr_p = dummy_object (type);
4377 return GS_ALL_DONE;
4379 else
4381 /* Make it easier for the backends by protecting the valist argument
4382 from multiple evaluations. */
4383 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4385 /* For this case, the backends will be expecting a pointer to
4386 TREE_TYPE (abi), but it's possible we've
4387 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4388 So fix it. */
4389 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4391 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4392 valist = fold_convert_loc (loc, p1,
4393 build_fold_addr_expr_loc (loc, valist));
4396 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4398 else
4399 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4401 if (!targetm.gimplify_va_arg_expr)
4402 /* FIXME: Once most targets are converted we should merely
4403 assert this is non-null. */
4404 return GS_ALL_DONE;
4406 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4407 return GS_OK;
4411 /* Expand EXP, a call to __builtin_va_end. */
4413 static rtx
4414 expand_builtin_va_end (tree exp)
4416 tree valist = CALL_EXPR_ARG (exp, 0);
4418 /* Evaluate for side effects, if needed. I hate macros that don't
4419 do that. */
4420 if (TREE_SIDE_EFFECTS (valist))
4421 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4423 return const0_rtx;
4426 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4427 builtin rather than just as an assignment in stdarg.h because of the
4428 nastiness of array-type va_list types. */
4430 static rtx
4431 expand_builtin_va_copy (tree exp)
4433 tree dst, src, t;
4434 location_t loc = EXPR_LOCATION (exp);
4436 dst = CALL_EXPR_ARG (exp, 0);
4437 src = CALL_EXPR_ARG (exp, 1);
4439 dst = stabilize_va_list_loc (loc, dst, 1);
4440 src = stabilize_va_list_loc (loc, src, 0);
4442 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4444 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4446 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4447 TREE_SIDE_EFFECTS (t) = 1;
4448 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4450 else
4452 rtx dstb, srcb, size;
4454 /* Evaluate to pointers. */
4455 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4456 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4457 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4458 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4460 dstb = convert_memory_address (Pmode, dstb);
4461 srcb = convert_memory_address (Pmode, srcb);
4463 /* "Dereference" to BLKmode memories. */
4464 dstb = gen_rtx_MEM (BLKmode, dstb);
4465 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4466 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4467 srcb = gen_rtx_MEM (BLKmode, srcb);
4468 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4469 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4471 /* Copy. */
4472 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4475 return const0_rtx;
4478 /* Expand a call to one of the builtin functions __builtin_frame_address or
4479 __builtin_return_address. */
4481 static rtx
4482 expand_builtin_frame_address (tree fndecl, tree exp)
4484 /* The argument must be a nonnegative integer constant.
4485 It counts the number of frames to scan up the stack.
4486 The value is the return address saved in that frame. */
4487 if (call_expr_nargs (exp) == 0)
4488 /* Warning about missing arg was already issued. */
4489 return const0_rtx;
4490 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4492 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4493 error ("invalid argument to %<__builtin_frame_address%>");
4494 else
4495 error ("invalid argument to %<__builtin_return_address%>");
4496 return const0_rtx;
4498 else
4500 rtx tem
4501 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4502 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4504 /* Some ports cannot access arbitrary stack frames. */
4505 if (tem == NULL)
4507 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4508 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4509 else
4510 warning (0, "unsupported argument to %<__builtin_return_address%>");
4511 return const0_rtx;
4514 /* For __builtin_frame_address, return what we've got. */
4515 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4516 return tem;
4518 if (!REG_P (tem)
4519 && ! CONSTANT_P (tem))
4520 tem = copy_addr_to_reg (tem);
4521 return tem;
4525 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4526 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4527 is the same as for allocate_dynamic_stack_space. */
4529 static rtx
4530 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4532 rtx op0;
4533 rtx result;
4534 bool valid_arglist;
4535 unsigned int align;
4536 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4537 == BUILT_IN_ALLOCA_WITH_ALIGN);
4539 /* Emit normal call if we use mudflap. */
4540 if (flag_mudflap)
4541 return NULL_RTX;
4543 valid_arglist
4544 = (alloca_with_align
4545 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4546 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4548 if (!valid_arglist)
4549 return NULL_RTX;
4551 /* Compute the argument. */
4552 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4554 /* Compute the alignment. */
4555 align = (alloca_with_align
4556 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4557 : BIGGEST_ALIGNMENT);
4559 /* Allocate the desired space. */
4560 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4561 result = convert_memory_address (ptr_mode, result);
4563 return result;
4566 /* Expand a call to bswap builtin in EXP.
4567 Return NULL_RTX if a normal call should be emitted rather than expanding the
4568 function in-line. If convenient, the result should be placed in TARGET.
4569 SUBTARGET may be used as the target for computing one of EXP's operands. */
4571 static rtx
4572 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4573 rtx subtarget)
4575 tree arg;
4576 rtx op0;
4578 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4579 return NULL_RTX;
4581 arg = CALL_EXPR_ARG (exp, 0);
4582 op0 = expand_expr (arg,
4583 subtarget && GET_MODE (subtarget) == target_mode
4584 ? subtarget : NULL_RTX,
4585 target_mode, EXPAND_NORMAL);
4586 if (GET_MODE (op0) != target_mode)
4587 op0 = convert_to_mode (target_mode, op0, 1);
4589 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4591 gcc_assert (target);
4593 return convert_to_mode (target_mode, target, 1);
4596 /* Expand a call to a unary builtin in EXP.
4597 Return NULL_RTX if a normal call should be emitted rather than expanding the
4598 function in-line. If convenient, the result should be placed in TARGET.
4599 SUBTARGET may be used as the target for computing one of EXP's operands. */
4601 static rtx
4602 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4603 rtx subtarget, optab op_optab)
4605 rtx op0;
4607 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4608 return NULL_RTX;
4610 /* Compute the argument. */
4611 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4612 (subtarget
4613 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4614 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4615 VOIDmode, EXPAND_NORMAL);
4616 /* Compute op, into TARGET if possible.
4617 Set TARGET to wherever the result comes back. */
4618 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4619 op_optab, op0, target, op_optab != clrsb_optab);
4620 gcc_assert (target);
4622 return convert_to_mode (target_mode, target, 0);
4625 /* Expand a call to __builtin_expect. We just return our argument
4626 as the builtin_expect semantic should've been already executed by
4627 tree branch prediction pass. */
4629 static rtx
4630 expand_builtin_expect (tree exp, rtx target)
4632 tree arg;
4634 if (call_expr_nargs (exp) < 2)
4635 return const0_rtx;
4636 arg = CALL_EXPR_ARG (exp, 0);
4638 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4639 /* When guessing was done, the hints should be already stripped away. */
4640 gcc_assert (!flag_guess_branch_prob
4641 || optimize == 0 || seen_error ());
4642 return target;
4645 /* Expand a call to __builtin_assume_aligned. We just return our first
4646 argument as the builtin_assume_aligned semantic should've been already
4647 executed by CCP. */
4649 static rtx
4650 expand_builtin_assume_aligned (tree exp, rtx target)
4652 if (call_expr_nargs (exp) < 2)
4653 return const0_rtx;
4654 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4655 EXPAND_NORMAL);
4656 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4657 && (call_expr_nargs (exp) < 3
4658 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4659 return target;
4662 void
4663 expand_builtin_trap (void)
4665 #ifdef HAVE_trap
4666 if (HAVE_trap)
4668 rtx insn = emit_insn (gen_trap ());
4669 /* For trap insns when not accumulating outgoing args force
4670 REG_ARGS_SIZE note to prevent crossjumping of calls with
4671 different args sizes. */
4672 if (!ACCUMULATE_OUTGOING_ARGS)
4673 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4675 else
4676 #endif
4677 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4678 emit_barrier ();
4681 /* Expand a call to __builtin_unreachable. We do nothing except emit
4682 a barrier saying that control flow will not pass here.
4684 It is the responsibility of the program being compiled to ensure
4685 that control flow does never reach __builtin_unreachable. */
4686 static void
4687 expand_builtin_unreachable (void)
4689 emit_barrier ();
4692 /* Expand EXP, a call to fabs, fabsf or fabsl.
4693 Return NULL_RTX if a normal call should be emitted rather than expanding
4694 the function inline. If convenient, the result should be placed
4695 in TARGET. SUBTARGET may be used as the target for computing
4696 the operand. */
4698 static rtx
4699 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4701 enum machine_mode mode;
4702 tree arg;
4703 rtx op0;
4705 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4706 return NULL_RTX;
4708 arg = CALL_EXPR_ARG (exp, 0);
4709 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4710 mode = TYPE_MODE (TREE_TYPE (arg));
4711 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4712 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4715 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4716 Return NULL is a normal call should be emitted rather than expanding the
4717 function inline. If convenient, the result should be placed in TARGET.
4718 SUBTARGET may be used as the target for computing the operand. */
4720 static rtx
4721 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4723 rtx op0, op1;
4724 tree arg;
4726 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4727 return NULL_RTX;
4729 arg = CALL_EXPR_ARG (exp, 0);
4730 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4732 arg = CALL_EXPR_ARG (exp, 1);
4733 op1 = expand_normal (arg);
4735 return expand_copysign (op0, op1, target);
4738 /* Create a new constant string literal and return a char* pointer to it.
4739 The STRING_CST value is the LEN characters at STR. */
4740 tree
4741 build_string_literal (int len, const char *str)
4743 tree t, elem, index, type;
4745 t = build_string (len, str);
4746 elem = build_type_variant (char_type_node, 1, 0);
4747 index = build_index_type (size_int (len - 1));
4748 type = build_array_type (elem, index);
4749 TREE_TYPE (t) = type;
4750 TREE_CONSTANT (t) = 1;
4751 TREE_READONLY (t) = 1;
4752 TREE_STATIC (t) = 1;
4754 type = build_pointer_type (elem);
4755 t = build1 (ADDR_EXPR, type,
4756 build4 (ARRAY_REF, elem,
4757 t, integer_zero_node, NULL_TREE, NULL_TREE));
4758 return t;
4761 /* Expand a call to __builtin___clear_cache. */
4763 static rtx
4764 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4766 #ifndef HAVE_clear_cache
4767 #ifdef CLEAR_INSN_CACHE
4768 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4769 does something. Just do the default expansion to a call to
4770 __clear_cache(). */
4771 return NULL_RTX;
4772 #else
4773 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4774 does nothing. There is no need to call it. Do nothing. */
4775 return const0_rtx;
4776 #endif /* CLEAR_INSN_CACHE */
4777 #else
4778 /* We have a "clear_cache" insn, and it will handle everything. */
4779 tree begin, end;
4780 rtx begin_rtx, end_rtx;
4782 /* We must not expand to a library call. If we did, any
4783 fallback library function in libgcc that might contain a call to
4784 __builtin___clear_cache() would recurse infinitely. */
4785 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4787 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4788 return const0_rtx;
4791 if (HAVE_clear_cache)
4793 struct expand_operand ops[2];
4795 begin = CALL_EXPR_ARG (exp, 0);
4796 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4798 end = CALL_EXPR_ARG (exp, 1);
4799 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4801 create_address_operand (&ops[0], begin_rtx);
4802 create_address_operand (&ops[1], end_rtx);
4803 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4804 return const0_rtx;
4806 return const0_rtx;
4807 #endif /* HAVE_clear_cache */
4810 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4812 static rtx
4813 round_trampoline_addr (rtx tramp)
4815 rtx temp, addend, mask;
4817 /* If we don't need too much alignment, we'll have been guaranteed
4818 proper alignment by get_trampoline_type. */
4819 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4820 return tramp;
4822 /* Round address up to desired boundary. */
4823 temp = gen_reg_rtx (Pmode);
4824 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4825 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4827 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4828 temp, 0, OPTAB_LIB_WIDEN);
4829 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4830 temp, 0, OPTAB_LIB_WIDEN);
4832 return tramp;
4835 static rtx
4836 expand_builtin_init_trampoline (tree exp, bool onstack)
4838 tree t_tramp, t_func, t_chain;
4839 rtx m_tramp, r_tramp, r_chain, tmp;
4841 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4842 POINTER_TYPE, VOID_TYPE))
4843 return NULL_RTX;
4845 t_tramp = CALL_EXPR_ARG (exp, 0);
4846 t_func = CALL_EXPR_ARG (exp, 1);
4847 t_chain = CALL_EXPR_ARG (exp, 2);
4849 r_tramp = expand_normal (t_tramp);
4850 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4851 MEM_NOTRAP_P (m_tramp) = 1;
4853 /* If ONSTACK, the TRAMP argument should be the address of a field
4854 within the local function's FRAME decl. Either way, let's see if
4855 we can fill in the MEM_ATTRs for this memory. */
4856 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4857 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4859 /* Creator of a heap trampoline is responsible for making sure the
4860 address is aligned to at least STACK_BOUNDARY. Normally malloc
4861 will ensure this anyhow. */
4862 tmp = round_trampoline_addr (r_tramp);
4863 if (tmp != r_tramp)
4865 m_tramp = change_address (m_tramp, BLKmode, tmp);
4866 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4867 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4870 /* The FUNC argument should be the address of the nested function.
4871 Extract the actual function decl to pass to the hook. */
4872 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4873 t_func = TREE_OPERAND (t_func, 0);
4874 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4876 r_chain = expand_normal (t_chain);
4878 /* Generate insns to initialize the trampoline. */
4879 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4881 if (onstack)
4883 trampolines_created = 1;
4885 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4886 "trampoline generated for nested function %qD", t_func);
4889 return const0_rtx;
4892 static rtx
4893 expand_builtin_adjust_trampoline (tree exp)
4895 rtx tramp;
4897 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4898 return NULL_RTX;
4900 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4901 tramp = round_trampoline_addr (tramp);
4902 if (targetm.calls.trampoline_adjust_address)
4903 tramp = targetm.calls.trampoline_adjust_address (tramp);
4905 return tramp;
4908 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4909 function. The function first checks whether the back end provides
4910 an insn to implement signbit for the respective mode. If not, it
4911 checks whether the floating point format of the value is such that
4912 the sign bit can be extracted. If that is not the case, the
4913 function returns NULL_RTX to indicate that a normal call should be
4914 emitted rather than expanding the function in-line. EXP is the
4915 expression that is a call to the builtin function; if convenient,
4916 the result should be placed in TARGET. */
4917 static rtx
4918 expand_builtin_signbit (tree exp, rtx target)
4920 const struct real_format *fmt;
4921 enum machine_mode fmode, imode, rmode;
4922 tree arg;
4923 int word, bitpos;
4924 enum insn_code icode;
4925 rtx temp;
4926 location_t loc = EXPR_LOCATION (exp);
4928 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4929 return NULL_RTX;
4931 arg = CALL_EXPR_ARG (exp, 0);
4932 fmode = TYPE_MODE (TREE_TYPE (arg));
4933 rmode = TYPE_MODE (TREE_TYPE (exp));
4934 fmt = REAL_MODE_FORMAT (fmode);
4936 arg = builtin_save_expr (arg);
4938 /* Expand the argument yielding a RTX expression. */
4939 temp = expand_normal (arg);
4941 /* Check if the back end provides an insn that handles signbit for the
4942 argument's mode. */
4943 icode = optab_handler (signbit_optab, fmode);
4944 if (icode != CODE_FOR_nothing)
4946 rtx last = get_last_insn ();
4947 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4948 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4949 return target;
4950 delete_insns_since (last);
4953 /* For floating point formats without a sign bit, implement signbit
4954 as "ARG < 0.0". */
4955 bitpos = fmt->signbit_ro;
4956 if (bitpos < 0)
4958 /* But we can't do this if the format supports signed zero. */
4959 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4960 return NULL_RTX;
4962 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4963 build_real (TREE_TYPE (arg), dconst0));
4964 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4967 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4969 imode = int_mode_for_mode (fmode);
4970 if (imode == BLKmode)
4971 return NULL_RTX;
4972 temp = gen_lowpart (imode, temp);
4974 else
4976 imode = word_mode;
4977 /* Handle targets with different FP word orders. */
4978 if (FLOAT_WORDS_BIG_ENDIAN)
4979 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4980 else
4981 word = bitpos / BITS_PER_WORD;
4982 temp = operand_subword_force (temp, word, fmode);
4983 bitpos = bitpos % BITS_PER_WORD;
4986 /* Force the intermediate word_mode (or narrower) result into a
4987 register. This avoids attempting to create paradoxical SUBREGs
4988 of floating point modes below. */
4989 temp = force_reg (imode, temp);
4991 /* If the bitpos is within the "result mode" lowpart, the operation
4992 can be implement with a single bitwise AND. Otherwise, we need
4993 a right shift and an AND. */
4995 if (bitpos < GET_MODE_BITSIZE (rmode))
4997 double_int mask = double_int_zero.set_bit (bitpos);
4999 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5000 temp = gen_lowpart (rmode, temp);
5001 temp = expand_binop (rmode, and_optab, temp,
5002 immed_double_int_const (mask, rmode),
5003 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5005 else
5007 /* Perform a logical right shift to place the signbit in the least
5008 significant bit, then truncate the result to the desired mode
5009 and mask just this bit. */
5010 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5011 temp = gen_lowpart (rmode, temp);
5012 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5013 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5016 return temp;
5019 /* Expand fork or exec calls. TARGET is the desired target of the
5020 call. EXP is the call. FN is the
5021 identificator of the actual function. IGNORE is nonzero if the
5022 value is to be ignored. */
5024 static rtx
5025 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5027 tree id, decl;
5028 tree call;
5030 /* If we are not profiling, just call the function. */
5031 if (!profile_arc_flag)
5032 return NULL_RTX;
5034 /* Otherwise call the wrapper. This should be equivalent for the rest of
5035 compiler, so the code does not diverge, and the wrapper may run the
5036 code necessary for keeping the profiling sane. */
5038 switch (DECL_FUNCTION_CODE (fn))
5040 case BUILT_IN_FORK:
5041 id = get_identifier ("__gcov_fork");
5042 break;
5044 case BUILT_IN_EXECL:
5045 id = get_identifier ("__gcov_execl");
5046 break;
5048 case BUILT_IN_EXECV:
5049 id = get_identifier ("__gcov_execv");
5050 break;
5052 case BUILT_IN_EXECLP:
5053 id = get_identifier ("__gcov_execlp");
5054 break;
5056 case BUILT_IN_EXECLE:
5057 id = get_identifier ("__gcov_execle");
5058 break;
5060 case BUILT_IN_EXECVP:
5061 id = get_identifier ("__gcov_execvp");
5062 break;
5064 case BUILT_IN_EXECVE:
5065 id = get_identifier ("__gcov_execve");
5066 break;
5068 default:
5069 gcc_unreachable ();
5072 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5073 FUNCTION_DECL, id, TREE_TYPE (fn));
5074 DECL_EXTERNAL (decl) = 1;
5075 TREE_PUBLIC (decl) = 1;
5076 DECL_ARTIFICIAL (decl) = 1;
5077 TREE_NOTHROW (decl) = 1;
5078 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5079 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5080 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5081 return expand_call (call, target, ignore);
5086 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5087 the pointer in these functions is void*, the tree optimizers may remove
5088 casts. The mode computed in expand_builtin isn't reliable either, due
5089 to __sync_bool_compare_and_swap.
5091 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5092 group of builtins. This gives us log2 of the mode size. */
5094 static inline enum machine_mode
5095 get_builtin_sync_mode (int fcode_diff)
5097 /* The size is not negotiable, so ask not to get BLKmode in return
5098 if the target indicates that a smaller size would be better. */
5099 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5102 /* Expand the memory expression LOC and return the appropriate memory operand
5103 for the builtin_sync operations. */
5105 static rtx
5106 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5108 rtx addr, mem;
5110 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5111 addr = convert_memory_address (Pmode, addr);
5113 /* Note that we explicitly do not want any alias information for this
5114 memory, so that we kill all other live memories. Otherwise we don't
5115 satisfy the full barrier semantics of the intrinsic. */
5116 mem = validize_mem (gen_rtx_MEM (mode, addr));
5118 /* The alignment needs to be at least according to that of the mode. */
5119 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5120 get_pointer_alignment (loc)));
5121 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5122 MEM_VOLATILE_P (mem) = 1;
5124 return mem;
5127 /* Make sure an argument is in the right mode.
5128 EXP is the tree argument.
5129 MODE is the mode it should be in. */
5131 static rtx
5132 expand_expr_force_mode (tree exp, enum machine_mode mode)
5134 rtx val;
5135 enum machine_mode old_mode;
5137 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5138 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5139 of CONST_INTs, where we know the old_mode only from the call argument. */
5141 old_mode = GET_MODE (val);
5142 if (old_mode == VOIDmode)
5143 old_mode = TYPE_MODE (TREE_TYPE (exp));
5144 val = convert_modes (mode, old_mode, val, 1);
5145 return val;
5149 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5150 EXP is the CALL_EXPR. CODE is the rtx code
5151 that corresponds to the arithmetic or logical operation from the name;
5152 an exception here is that NOT actually means NAND. TARGET is an optional
5153 place for us to store the results; AFTER is true if this is the
5154 fetch_and_xxx form. */
5156 static rtx
5157 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5158 enum rtx_code code, bool after,
5159 rtx target)
5161 rtx val, mem;
5162 location_t loc = EXPR_LOCATION (exp);
5164 if (code == NOT && warn_sync_nand)
5166 tree fndecl = get_callee_fndecl (exp);
5167 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5169 static bool warned_f_a_n, warned_n_a_f;
5171 switch (fcode)
5173 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5174 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5175 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5176 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5177 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5178 if (warned_f_a_n)
5179 break;
5181 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5182 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5183 warned_f_a_n = true;
5184 break;
5186 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5187 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5188 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5189 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5190 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5191 if (warned_n_a_f)
5192 break;
5194 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5195 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5196 warned_n_a_f = true;
5197 break;
5199 default:
5200 gcc_unreachable ();
5204 /* Expand the operands. */
5205 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5206 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5208 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5209 after);
5212 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5213 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5214 true if this is the boolean form. TARGET is a place for us to store the
5215 results; this is NOT optional if IS_BOOL is true. */
5217 static rtx
5218 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5219 bool is_bool, rtx target)
5221 rtx old_val, new_val, mem;
5222 rtx *pbool, *poval;
5224 /* Expand the operands. */
5225 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5226 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5227 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5229 pbool = poval = NULL;
5230 if (target != const0_rtx)
5232 if (is_bool)
5233 pbool = &target;
5234 else
5235 poval = &target;
5237 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5238 false, MEMMODEL_SEQ_CST,
5239 MEMMODEL_SEQ_CST))
5240 return NULL_RTX;
5242 return target;
5245 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5246 general form is actually an atomic exchange, and some targets only
5247 support a reduced form with the second argument being a constant 1.
5248 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5249 the results. */
5251 static rtx
5252 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5253 rtx target)
5255 rtx val, mem;
5257 /* Expand the operands. */
5258 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5259 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5261 return expand_sync_lock_test_and_set (target, mem, val);
5264 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5266 static void
5267 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5269 rtx mem;
5271 /* Expand the operands. */
5272 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5274 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5277 /* Given an integer representing an ``enum memmodel'', verify its
5278 correctness and return the memory model enum. */
5280 static enum memmodel
5281 get_memmodel (tree exp)
5283 rtx op;
5284 unsigned HOST_WIDE_INT val;
5286 /* If the parameter is not a constant, it's a run time value so we'll just
5287 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5288 if (TREE_CODE (exp) != INTEGER_CST)
5289 return MEMMODEL_SEQ_CST;
5291 op = expand_normal (exp);
5293 val = INTVAL (op);
5294 if (targetm.memmodel_check)
5295 val = targetm.memmodel_check (val);
5296 else if (val & ~MEMMODEL_MASK)
5298 warning (OPT_Winvalid_memory_model,
5299 "Unknown architecture specifier in memory model to builtin.");
5300 return MEMMODEL_SEQ_CST;
5303 if ((INTVAL(op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5305 warning (OPT_Winvalid_memory_model,
5306 "invalid memory model argument to builtin");
5307 return MEMMODEL_SEQ_CST;
5310 return (enum memmodel) val;
5313 /* Expand the __atomic_exchange intrinsic:
5314 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5315 EXP is the CALL_EXPR.
5316 TARGET is an optional place for us to store the results. */
5318 static rtx
5319 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5321 rtx val, mem;
5322 enum memmodel model;
5324 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5325 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5327 error ("invalid memory model for %<__atomic_exchange%>");
5328 return NULL_RTX;
5331 if (!flag_inline_atomics)
5332 return NULL_RTX;
5334 /* Expand the operands. */
5335 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5336 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5338 return expand_atomic_exchange (target, mem, val, model);
5341 /* Expand the __atomic_compare_exchange intrinsic:
5342 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5343 TYPE desired, BOOL weak,
5344 enum memmodel success,
5345 enum memmodel failure)
5346 EXP is the CALL_EXPR.
5347 TARGET is an optional place for us to store the results. */
5349 static rtx
5350 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5351 rtx target)
5353 rtx expect, desired, mem, oldval;
5354 enum memmodel success, failure;
5355 tree weak;
5356 bool is_weak;
5358 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5359 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5361 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5362 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5364 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5365 return NULL_RTX;
5368 if (failure > success)
5370 error ("failure memory model cannot be stronger than success "
5371 "memory model for %<__atomic_compare_exchange%>");
5372 return NULL_RTX;
5375 if (!flag_inline_atomics)
5376 return NULL_RTX;
5378 /* Expand the operands. */
5379 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5381 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5382 expect = convert_memory_address (Pmode, expect);
5383 expect = gen_rtx_MEM (mode, expect);
5384 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5386 weak = CALL_EXPR_ARG (exp, 3);
5387 is_weak = false;
5388 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5389 is_weak = true;
5391 oldval = expect;
5392 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5393 &oldval, mem, oldval, desired,
5394 is_weak, success, failure))
5395 return NULL_RTX;
5397 if (oldval != expect)
5398 emit_move_insn (expect, oldval);
5400 return target;
5403 /* Expand the __atomic_load intrinsic:
5404 TYPE __atomic_load (TYPE *object, enum memmodel)
5405 EXP is the CALL_EXPR.
5406 TARGET is an optional place for us to store the results. */
5408 static rtx
5409 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5411 rtx mem;
5412 enum memmodel model;
5414 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5415 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5416 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5418 error ("invalid memory model for %<__atomic_load%>");
5419 return NULL_RTX;
5422 if (!flag_inline_atomics)
5423 return NULL_RTX;
5425 /* Expand the operand. */
5426 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5428 return expand_atomic_load (target, mem, model);
5432 /* Expand the __atomic_store intrinsic:
5433 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5434 EXP is the CALL_EXPR.
5435 TARGET is an optional place for us to store the results. */
5437 static rtx
5438 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5440 rtx mem, val;
5441 enum memmodel model;
5443 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5444 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5445 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5446 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5448 error ("invalid memory model for %<__atomic_store%>");
5449 return NULL_RTX;
5452 if (!flag_inline_atomics)
5453 return NULL_RTX;
5455 /* Expand the operands. */
5456 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5457 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5459 return expand_atomic_store (mem, val, model, false);
5462 /* Expand the __atomic_fetch_XXX intrinsic:
5463 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5464 EXP is the CALL_EXPR.
5465 TARGET is an optional place for us to store the results.
5466 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5467 FETCH_AFTER is true if returning the result of the operation.
5468 FETCH_AFTER is false if returning the value before the operation.
5469 IGNORE is true if the result is not used.
5470 EXT_CALL is the correct builtin for an external call if this cannot be
5471 resolved to an instruction sequence. */
5473 static rtx
5474 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5475 enum rtx_code code, bool fetch_after,
5476 bool ignore, enum built_in_function ext_call)
5478 rtx val, mem, ret;
5479 enum memmodel model;
5480 tree fndecl;
5481 tree addr;
5483 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5485 /* Expand the operands. */
5486 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5487 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5489 /* Only try generating instructions if inlining is turned on. */
5490 if (flag_inline_atomics)
5492 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5493 if (ret)
5494 return ret;
5497 /* Return if a different routine isn't needed for the library call. */
5498 if (ext_call == BUILT_IN_NONE)
5499 return NULL_RTX;
5501 /* Change the call to the specified function. */
5502 fndecl = get_callee_fndecl (exp);
5503 addr = CALL_EXPR_FN (exp);
5504 STRIP_NOPS (addr);
5506 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5507 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5509 /* Expand the call here so we can emit trailing code. */
5510 ret = expand_call (exp, target, ignore);
5512 /* Replace the original function just in case it matters. */
5513 TREE_OPERAND (addr, 0) = fndecl;
5515 /* Then issue the arithmetic correction to return the right result. */
5516 if (!ignore)
5518 if (code == NOT)
5520 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5521 OPTAB_LIB_WIDEN);
5522 ret = expand_simple_unop (mode, NOT, ret, target, true);
5524 else
5525 ret = expand_simple_binop (mode, code, ret, val, target, true,
5526 OPTAB_LIB_WIDEN);
5528 return ret;
5532 #ifndef HAVE_atomic_clear
5533 # define HAVE_atomic_clear 0
5534 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5535 #endif
5537 /* Expand an atomic clear operation.
5538 void _atomic_clear (BOOL *obj, enum memmodel)
5539 EXP is the call expression. */
5541 static rtx
5542 expand_builtin_atomic_clear (tree exp)
5544 enum machine_mode mode;
5545 rtx mem, ret;
5546 enum memmodel model;
5548 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5549 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5550 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5552 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5553 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5555 error ("invalid memory model for %<__atomic_store%>");
5556 return const0_rtx;
5559 if (HAVE_atomic_clear)
5561 emit_insn (gen_atomic_clear (mem, model));
5562 return const0_rtx;
5565 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5566 Failing that, a store is issued by __atomic_store. The only way this can
5567 fail is if the bool type is larger than a word size. Unlikely, but
5568 handle it anyway for completeness. Assume a single threaded model since
5569 there is no atomic support in this case, and no barriers are required. */
5570 ret = expand_atomic_store (mem, const0_rtx, model, true);
5571 if (!ret)
5572 emit_move_insn (mem, const0_rtx);
5573 return const0_rtx;
5576 /* Expand an atomic test_and_set operation.
5577 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5578 EXP is the call expression. */
5580 static rtx
5581 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5583 rtx mem;
5584 enum memmodel model;
5585 enum machine_mode mode;
5587 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5588 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5589 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5591 return expand_atomic_test_and_set (target, mem, model);
5595 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5596 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5598 static tree
5599 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5601 int size;
5602 enum machine_mode mode;
5603 unsigned int mode_align, type_align;
5605 if (TREE_CODE (arg0) != INTEGER_CST)
5606 return NULL_TREE;
5608 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5609 mode = mode_for_size (size, MODE_INT, 0);
5610 mode_align = GET_MODE_ALIGNMENT (mode);
5612 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5613 type_align = mode_align;
5614 else
5616 tree ttype = TREE_TYPE (arg1);
5618 /* This function is usually invoked and folded immediately by the front
5619 end before anything else has a chance to look at it. The pointer
5620 parameter at this point is usually cast to a void *, so check for that
5621 and look past the cast. */
5622 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5623 && VOID_TYPE_P (TREE_TYPE (ttype)))
5624 arg1 = TREE_OPERAND (arg1, 0);
5626 ttype = TREE_TYPE (arg1);
5627 gcc_assert (POINTER_TYPE_P (ttype));
5629 /* Get the underlying type of the object. */
5630 ttype = TREE_TYPE (ttype);
5631 type_align = TYPE_ALIGN (ttype);
5634 /* If the object has smaller alignment, the the lock free routines cannot
5635 be used. */
5636 if (type_align < mode_align)
5637 return boolean_false_node;
5639 /* Check if a compare_and_swap pattern exists for the mode which represents
5640 the required size. The pattern is not allowed to fail, so the existence
5641 of the pattern indicates support is present. */
5642 if (can_compare_and_swap_p (mode, true))
5643 return boolean_true_node;
5644 else
5645 return boolean_false_node;
5648 /* Return true if the parameters to call EXP represent an object which will
5649 always generate lock free instructions. The first argument represents the
5650 size of the object, and the second parameter is a pointer to the object
5651 itself. If NULL is passed for the object, then the result is based on
5652 typical alignment for an object of the specified size. Otherwise return
5653 false. */
5655 static rtx
5656 expand_builtin_atomic_always_lock_free (tree exp)
5658 tree size;
5659 tree arg0 = CALL_EXPR_ARG (exp, 0);
5660 tree arg1 = CALL_EXPR_ARG (exp, 1);
5662 if (TREE_CODE (arg0) != INTEGER_CST)
5664 error ("non-constant argument 1 to __atomic_always_lock_free");
5665 return const0_rtx;
5668 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5669 if (size == boolean_true_node)
5670 return const1_rtx;
5671 return const0_rtx;
5674 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5675 is lock free on this architecture. */
5677 static tree
5678 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5680 if (!flag_inline_atomics)
5681 return NULL_TREE;
5683 /* If it isn't always lock free, don't generate a result. */
5684 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5685 return boolean_true_node;
5687 return NULL_TREE;
5690 /* Return true if the parameters to call EXP represent an object which will
5691 always generate lock free instructions. The first argument represents the
5692 size of the object, and the second parameter is a pointer to the object
5693 itself. If NULL is passed for the object, then the result is based on
5694 typical alignment for an object of the specified size. Otherwise return
5695 NULL*/
5697 static rtx
5698 expand_builtin_atomic_is_lock_free (tree exp)
5700 tree size;
5701 tree arg0 = CALL_EXPR_ARG (exp, 0);
5702 tree arg1 = CALL_EXPR_ARG (exp, 1);
5704 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5706 error ("non-integer argument 1 to __atomic_is_lock_free");
5707 return NULL_RTX;
5710 if (!flag_inline_atomics)
5711 return NULL_RTX;
5713 /* If the value is known at compile time, return the RTX for it. */
5714 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5715 if (size == boolean_true_node)
5716 return const1_rtx;
5718 return NULL_RTX;
5721 /* Expand the __atomic_thread_fence intrinsic:
5722 void __atomic_thread_fence (enum memmodel)
5723 EXP is the CALL_EXPR. */
5725 static void
5726 expand_builtin_atomic_thread_fence (tree exp)
5728 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5729 expand_mem_thread_fence (model);
5732 /* Expand the __atomic_signal_fence intrinsic:
5733 void __atomic_signal_fence (enum memmodel)
5734 EXP is the CALL_EXPR. */
5736 static void
5737 expand_builtin_atomic_signal_fence (tree exp)
5739 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5740 expand_mem_signal_fence (model);
5743 /* Expand the __sync_synchronize intrinsic. */
5745 static void
5746 expand_builtin_sync_synchronize (void)
5748 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5751 static rtx
5752 expand_builtin_thread_pointer (tree exp, rtx target)
5754 enum insn_code icode;
5755 if (!validate_arglist (exp, VOID_TYPE))
5756 return const0_rtx;
5757 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5758 if (icode != CODE_FOR_nothing)
5760 struct expand_operand op;
5761 if (!REG_P (target) || GET_MODE (target) != Pmode)
5762 target = gen_reg_rtx (Pmode);
5763 create_output_operand (&op, target, Pmode);
5764 expand_insn (icode, 1, &op);
5765 return target;
5767 error ("__builtin_thread_pointer is not supported on this target");
5768 return const0_rtx;
5771 static void
5772 expand_builtin_set_thread_pointer (tree exp)
5774 enum insn_code icode;
5775 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5776 return;
5777 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5778 if (icode != CODE_FOR_nothing)
5780 struct expand_operand op;
5781 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5782 Pmode, EXPAND_NORMAL);
5783 create_input_operand (&op, val, Pmode);
5784 expand_insn (icode, 1, &op);
5785 return;
5787 error ("__builtin_set_thread_pointer is not supported on this target");
5791 /* Expand an expression EXP that calls a built-in function,
5792 with result going to TARGET if that's convenient
5793 (and in mode MODE if that's convenient).
5794 SUBTARGET may be used as the target for computing one of EXP's operands.
5795 IGNORE is nonzero if the value is to be ignored. */
5798 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5799 int ignore)
5801 tree fndecl = get_callee_fndecl (exp);
5802 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5803 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5804 int flags;
5806 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5807 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5809 /* When not optimizing, generate calls to library functions for a certain
5810 set of builtins. */
5811 if (!optimize
5812 && !called_as_built_in (fndecl)
5813 && fcode != BUILT_IN_ALLOCA
5814 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5815 && fcode != BUILT_IN_FREE)
5816 return expand_call (exp, target, ignore);
5818 /* The built-in function expanders test for target == const0_rtx
5819 to determine whether the function's result will be ignored. */
5820 if (ignore)
5821 target = const0_rtx;
5823 /* If the result of a pure or const built-in function is ignored, and
5824 none of its arguments are volatile, we can avoid expanding the
5825 built-in call and just evaluate the arguments for side-effects. */
5826 if (target == const0_rtx
5827 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5828 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5830 bool volatilep = false;
5831 tree arg;
5832 call_expr_arg_iterator iter;
5834 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5835 if (TREE_THIS_VOLATILE (arg))
5837 volatilep = true;
5838 break;
5841 if (! volatilep)
5843 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5844 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5845 return const0_rtx;
5849 switch (fcode)
5851 CASE_FLT_FN (BUILT_IN_FABS):
5852 target = expand_builtin_fabs (exp, target, subtarget);
5853 if (target)
5854 return target;
5855 break;
5857 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5858 target = expand_builtin_copysign (exp, target, subtarget);
5859 if (target)
5860 return target;
5861 break;
5863 /* Just do a normal library call if we were unable to fold
5864 the values. */
5865 CASE_FLT_FN (BUILT_IN_CABS):
5866 break;
5868 CASE_FLT_FN (BUILT_IN_EXP):
5869 CASE_FLT_FN (BUILT_IN_EXP10):
5870 CASE_FLT_FN (BUILT_IN_POW10):
5871 CASE_FLT_FN (BUILT_IN_EXP2):
5872 CASE_FLT_FN (BUILT_IN_EXPM1):
5873 CASE_FLT_FN (BUILT_IN_LOGB):
5874 CASE_FLT_FN (BUILT_IN_LOG):
5875 CASE_FLT_FN (BUILT_IN_LOG10):
5876 CASE_FLT_FN (BUILT_IN_LOG2):
5877 CASE_FLT_FN (BUILT_IN_LOG1P):
5878 CASE_FLT_FN (BUILT_IN_TAN):
5879 CASE_FLT_FN (BUILT_IN_ASIN):
5880 CASE_FLT_FN (BUILT_IN_ACOS):
5881 CASE_FLT_FN (BUILT_IN_ATAN):
5882 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5883 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5884 because of possible accuracy problems. */
5885 if (! flag_unsafe_math_optimizations)
5886 break;
5887 CASE_FLT_FN (BUILT_IN_SQRT):
5888 CASE_FLT_FN (BUILT_IN_FLOOR):
5889 CASE_FLT_FN (BUILT_IN_CEIL):
5890 CASE_FLT_FN (BUILT_IN_TRUNC):
5891 CASE_FLT_FN (BUILT_IN_ROUND):
5892 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5893 CASE_FLT_FN (BUILT_IN_RINT):
5894 target = expand_builtin_mathfn (exp, target, subtarget);
5895 if (target)
5896 return target;
5897 break;
5899 CASE_FLT_FN (BUILT_IN_FMA):
5900 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5901 if (target)
5902 return target;
5903 break;
5905 CASE_FLT_FN (BUILT_IN_ILOGB):
5906 if (! flag_unsafe_math_optimizations)
5907 break;
5908 CASE_FLT_FN (BUILT_IN_ISINF):
5909 CASE_FLT_FN (BUILT_IN_FINITE):
5910 case BUILT_IN_ISFINITE:
5911 case BUILT_IN_ISNORMAL:
5912 target = expand_builtin_interclass_mathfn (exp, target);
5913 if (target)
5914 return target;
5915 break;
5917 CASE_FLT_FN (BUILT_IN_ICEIL):
5918 CASE_FLT_FN (BUILT_IN_LCEIL):
5919 CASE_FLT_FN (BUILT_IN_LLCEIL):
5920 CASE_FLT_FN (BUILT_IN_LFLOOR):
5921 CASE_FLT_FN (BUILT_IN_IFLOOR):
5922 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5923 target = expand_builtin_int_roundingfn (exp, target);
5924 if (target)
5925 return target;
5926 break;
5928 CASE_FLT_FN (BUILT_IN_IRINT):
5929 CASE_FLT_FN (BUILT_IN_LRINT):
5930 CASE_FLT_FN (BUILT_IN_LLRINT):
5931 CASE_FLT_FN (BUILT_IN_IROUND):
5932 CASE_FLT_FN (BUILT_IN_LROUND):
5933 CASE_FLT_FN (BUILT_IN_LLROUND):
5934 target = expand_builtin_int_roundingfn_2 (exp, target);
5935 if (target)
5936 return target;
5937 break;
5939 CASE_FLT_FN (BUILT_IN_POWI):
5940 target = expand_builtin_powi (exp, target);
5941 if (target)
5942 return target;
5943 break;
5945 CASE_FLT_FN (BUILT_IN_ATAN2):
5946 CASE_FLT_FN (BUILT_IN_LDEXP):
5947 CASE_FLT_FN (BUILT_IN_SCALB):
5948 CASE_FLT_FN (BUILT_IN_SCALBN):
5949 CASE_FLT_FN (BUILT_IN_SCALBLN):
5950 if (! flag_unsafe_math_optimizations)
5951 break;
5953 CASE_FLT_FN (BUILT_IN_FMOD):
5954 CASE_FLT_FN (BUILT_IN_REMAINDER):
5955 CASE_FLT_FN (BUILT_IN_DREM):
5956 CASE_FLT_FN (BUILT_IN_POW):
5957 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5958 if (target)
5959 return target;
5960 break;
5962 CASE_FLT_FN (BUILT_IN_CEXPI):
5963 target = expand_builtin_cexpi (exp, target);
5964 gcc_assert (target);
5965 return target;
5967 CASE_FLT_FN (BUILT_IN_SIN):
5968 CASE_FLT_FN (BUILT_IN_COS):
5969 if (! flag_unsafe_math_optimizations)
5970 break;
5971 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5972 if (target)
5973 return target;
5974 break;
5976 CASE_FLT_FN (BUILT_IN_SINCOS):
5977 if (! flag_unsafe_math_optimizations)
5978 break;
5979 target = expand_builtin_sincos (exp);
5980 if (target)
5981 return target;
5982 break;
5984 case BUILT_IN_APPLY_ARGS:
5985 return expand_builtin_apply_args ();
5987 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5988 FUNCTION with a copy of the parameters described by
5989 ARGUMENTS, and ARGSIZE. It returns a block of memory
5990 allocated on the stack into which is stored all the registers
5991 that might possibly be used for returning the result of a
5992 function. ARGUMENTS is the value returned by
5993 __builtin_apply_args. ARGSIZE is the number of bytes of
5994 arguments that must be copied. ??? How should this value be
5995 computed? We'll also need a safe worst case value for varargs
5996 functions. */
5997 case BUILT_IN_APPLY:
5998 if (!validate_arglist (exp, POINTER_TYPE,
5999 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6000 && !validate_arglist (exp, REFERENCE_TYPE,
6001 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6002 return const0_rtx;
6003 else
6005 rtx ops[3];
6007 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6008 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6009 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6011 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6014 /* __builtin_return (RESULT) causes the function to return the
6015 value described by RESULT. RESULT is address of the block of
6016 memory returned by __builtin_apply. */
6017 case BUILT_IN_RETURN:
6018 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6019 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6020 return const0_rtx;
6022 case BUILT_IN_SAVEREGS:
6023 return expand_builtin_saveregs ();
6025 case BUILT_IN_VA_ARG_PACK:
6026 /* All valid uses of __builtin_va_arg_pack () are removed during
6027 inlining. */
6028 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6029 return const0_rtx;
6031 case BUILT_IN_VA_ARG_PACK_LEN:
6032 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6033 inlining. */
6034 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6035 return const0_rtx;
6037 /* Return the address of the first anonymous stack arg. */
6038 case BUILT_IN_NEXT_ARG:
6039 if (fold_builtin_next_arg (exp, false))
6040 return const0_rtx;
6041 return expand_builtin_next_arg ();
6043 case BUILT_IN_CLEAR_CACHE:
6044 target = expand_builtin___clear_cache (exp);
6045 if (target)
6046 return target;
6047 break;
6049 case BUILT_IN_CLASSIFY_TYPE:
6050 return expand_builtin_classify_type (exp);
6052 case BUILT_IN_CONSTANT_P:
6053 return const0_rtx;
6055 case BUILT_IN_FRAME_ADDRESS:
6056 case BUILT_IN_RETURN_ADDRESS:
6057 return expand_builtin_frame_address (fndecl, exp);
6059 /* Returns the address of the area where the structure is returned.
6060 0 otherwise. */
6061 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6062 if (call_expr_nargs (exp) != 0
6063 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6064 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6065 return const0_rtx;
6066 else
6067 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6069 case BUILT_IN_ALLOCA:
6070 case BUILT_IN_ALLOCA_WITH_ALIGN:
6071 /* If the allocation stems from the declaration of a variable-sized
6072 object, it cannot accumulate. */
6073 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6074 if (target)
6075 return target;
6076 break;
6078 case BUILT_IN_STACK_SAVE:
6079 return expand_stack_save ();
6081 case BUILT_IN_STACK_RESTORE:
6082 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6083 return const0_rtx;
6085 case BUILT_IN_BSWAP16:
6086 case BUILT_IN_BSWAP32:
6087 case BUILT_IN_BSWAP64:
6088 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6089 if (target)
6090 return target;
6091 break;
6093 CASE_INT_FN (BUILT_IN_FFS):
6094 case BUILT_IN_FFSIMAX:
6095 target = expand_builtin_unop (target_mode, exp, target,
6096 subtarget, ffs_optab);
6097 if (target)
6098 return target;
6099 break;
6101 CASE_INT_FN (BUILT_IN_CLZ):
6102 case BUILT_IN_CLZIMAX:
6103 target = expand_builtin_unop (target_mode, exp, target,
6104 subtarget, clz_optab);
6105 if (target)
6106 return target;
6107 break;
6109 CASE_INT_FN (BUILT_IN_CTZ):
6110 case BUILT_IN_CTZIMAX:
6111 target = expand_builtin_unop (target_mode, exp, target,
6112 subtarget, ctz_optab);
6113 if (target)
6114 return target;
6115 break;
6117 CASE_INT_FN (BUILT_IN_CLRSB):
6118 case BUILT_IN_CLRSBIMAX:
6119 target = expand_builtin_unop (target_mode, exp, target,
6120 subtarget, clrsb_optab);
6121 if (target)
6122 return target;
6123 break;
6125 CASE_INT_FN (BUILT_IN_POPCOUNT):
6126 case BUILT_IN_POPCOUNTIMAX:
6127 target = expand_builtin_unop (target_mode, exp, target,
6128 subtarget, popcount_optab);
6129 if (target)
6130 return target;
6131 break;
6133 CASE_INT_FN (BUILT_IN_PARITY):
6134 case BUILT_IN_PARITYIMAX:
6135 target = expand_builtin_unop (target_mode, exp, target,
6136 subtarget, parity_optab);
6137 if (target)
6138 return target;
6139 break;
6141 case BUILT_IN_STRLEN:
6142 target = expand_builtin_strlen (exp, target, target_mode);
6143 if (target)
6144 return target;
6145 break;
6147 case BUILT_IN_STRCPY:
6148 target = expand_builtin_strcpy (exp, target);
6149 if (target)
6150 return target;
6151 break;
6153 case BUILT_IN_STRNCPY:
6154 target = expand_builtin_strncpy (exp, target);
6155 if (target)
6156 return target;
6157 break;
6159 case BUILT_IN_STPCPY:
6160 target = expand_builtin_stpcpy (exp, target, mode);
6161 if (target)
6162 return target;
6163 break;
6165 case BUILT_IN_MEMCPY:
6166 target = expand_builtin_memcpy (exp, target);
6167 if (target)
6168 return target;
6169 break;
6171 case BUILT_IN_MEMPCPY:
6172 target = expand_builtin_mempcpy (exp, target, mode);
6173 if (target)
6174 return target;
6175 break;
6177 case BUILT_IN_MEMSET:
6178 target = expand_builtin_memset (exp, target, mode);
6179 if (target)
6180 return target;
6181 break;
6183 case BUILT_IN_BZERO:
6184 target = expand_builtin_bzero (exp);
6185 if (target)
6186 return target;
6187 break;
6189 case BUILT_IN_STRCMP:
6190 target = expand_builtin_strcmp (exp, target);
6191 if (target)
6192 return target;
6193 break;
6195 case BUILT_IN_STRNCMP:
6196 target = expand_builtin_strncmp (exp, target, mode);
6197 if (target)
6198 return target;
6199 break;
6201 case BUILT_IN_BCMP:
6202 case BUILT_IN_MEMCMP:
6203 target = expand_builtin_memcmp (exp, target, mode);
6204 if (target)
6205 return target;
6206 break;
6208 case BUILT_IN_SETJMP:
6209 /* This should have been lowered to the builtins below. */
6210 gcc_unreachable ();
6212 case BUILT_IN_SETJMP_SETUP:
6213 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6214 and the receiver label. */
6215 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6217 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6218 VOIDmode, EXPAND_NORMAL);
6219 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6220 rtx label_r = label_rtx (label);
6222 /* This is copied from the handling of non-local gotos. */
6223 expand_builtin_setjmp_setup (buf_addr, label_r);
6224 nonlocal_goto_handler_labels
6225 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6226 nonlocal_goto_handler_labels);
6227 /* ??? Do not let expand_label treat us as such since we would
6228 not want to be both on the list of non-local labels and on
6229 the list of forced labels. */
6230 FORCED_LABEL (label) = 0;
6231 return const0_rtx;
6233 break;
6235 case BUILT_IN_SETJMP_DISPATCHER:
6236 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6237 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6239 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6240 rtx label_r = label_rtx (label);
6242 /* Remove the dispatcher label from the list of non-local labels
6243 since the receiver labels have been added to it above. */
6244 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6245 return const0_rtx;
6247 break;
6249 case BUILT_IN_SETJMP_RECEIVER:
6250 /* __builtin_setjmp_receiver is passed the receiver label. */
6251 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6253 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6254 rtx label_r = label_rtx (label);
6256 expand_builtin_setjmp_receiver (label_r);
6257 return const0_rtx;
6259 break;
6261 /* __builtin_longjmp is passed a pointer to an array of five words.
6262 It's similar to the C library longjmp function but works with
6263 __builtin_setjmp above. */
6264 case BUILT_IN_LONGJMP:
6265 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6267 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6268 VOIDmode, EXPAND_NORMAL);
6269 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6271 if (value != const1_rtx)
6273 error ("%<__builtin_longjmp%> second argument must be 1");
6274 return const0_rtx;
6277 expand_builtin_longjmp (buf_addr, value);
6278 return const0_rtx;
6280 break;
6282 case BUILT_IN_NONLOCAL_GOTO:
6283 target = expand_builtin_nonlocal_goto (exp);
6284 if (target)
6285 return target;
6286 break;
6288 /* This updates the setjmp buffer that is its argument with the value
6289 of the current stack pointer. */
6290 case BUILT_IN_UPDATE_SETJMP_BUF:
6291 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6293 rtx buf_addr
6294 = expand_normal (CALL_EXPR_ARG (exp, 0));
6296 expand_builtin_update_setjmp_buf (buf_addr);
6297 return const0_rtx;
6299 break;
6301 case BUILT_IN_TRAP:
6302 expand_builtin_trap ();
6303 return const0_rtx;
6305 case BUILT_IN_UNREACHABLE:
6306 expand_builtin_unreachable ();
6307 return const0_rtx;
6309 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6310 case BUILT_IN_SIGNBITD32:
6311 case BUILT_IN_SIGNBITD64:
6312 case BUILT_IN_SIGNBITD128:
6313 target = expand_builtin_signbit (exp, target);
6314 if (target)
6315 return target;
6316 break;
6318 /* Various hooks for the DWARF 2 __throw routine. */
6319 case BUILT_IN_UNWIND_INIT:
6320 expand_builtin_unwind_init ();
6321 return const0_rtx;
6322 case BUILT_IN_DWARF_CFA:
6323 return virtual_cfa_rtx;
6324 #ifdef DWARF2_UNWIND_INFO
6325 case BUILT_IN_DWARF_SP_COLUMN:
6326 return expand_builtin_dwarf_sp_column ();
6327 case BUILT_IN_INIT_DWARF_REG_SIZES:
6328 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6329 return const0_rtx;
6330 #endif
6331 case BUILT_IN_FROB_RETURN_ADDR:
6332 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6333 case BUILT_IN_EXTRACT_RETURN_ADDR:
6334 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6335 case BUILT_IN_EH_RETURN:
6336 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6337 CALL_EXPR_ARG (exp, 1));
6338 return const0_rtx;
6339 #ifdef EH_RETURN_DATA_REGNO
6340 case BUILT_IN_EH_RETURN_DATA_REGNO:
6341 return expand_builtin_eh_return_data_regno (exp);
6342 #endif
6343 case BUILT_IN_EXTEND_POINTER:
6344 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6345 case BUILT_IN_EH_POINTER:
6346 return expand_builtin_eh_pointer (exp);
6347 case BUILT_IN_EH_FILTER:
6348 return expand_builtin_eh_filter (exp);
6349 case BUILT_IN_EH_COPY_VALUES:
6350 return expand_builtin_eh_copy_values (exp);
6352 case BUILT_IN_VA_START:
6353 return expand_builtin_va_start (exp);
6354 case BUILT_IN_VA_END:
6355 return expand_builtin_va_end (exp);
6356 case BUILT_IN_VA_COPY:
6357 return expand_builtin_va_copy (exp);
6358 case BUILT_IN_EXPECT:
6359 return expand_builtin_expect (exp, target);
6360 case BUILT_IN_ASSUME_ALIGNED:
6361 return expand_builtin_assume_aligned (exp, target);
6362 case BUILT_IN_PREFETCH:
6363 expand_builtin_prefetch (exp);
6364 return const0_rtx;
6366 case BUILT_IN_INIT_TRAMPOLINE:
6367 return expand_builtin_init_trampoline (exp, true);
6368 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6369 return expand_builtin_init_trampoline (exp, false);
6370 case BUILT_IN_ADJUST_TRAMPOLINE:
6371 return expand_builtin_adjust_trampoline (exp);
6373 case BUILT_IN_FORK:
6374 case BUILT_IN_EXECL:
6375 case BUILT_IN_EXECV:
6376 case BUILT_IN_EXECLP:
6377 case BUILT_IN_EXECLE:
6378 case BUILT_IN_EXECVP:
6379 case BUILT_IN_EXECVE:
6380 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6381 if (target)
6382 return target;
6383 break;
6385 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6386 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6387 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6388 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6389 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6390 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6391 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6392 if (target)
6393 return target;
6394 break;
6396 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6397 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6398 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6399 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6400 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6401 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6402 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6403 if (target)
6404 return target;
6405 break;
6407 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6408 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6409 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6410 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6411 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6412 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6413 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6414 if (target)
6415 return target;
6416 break;
6418 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6419 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6420 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6421 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6422 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6423 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6424 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6425 if (target)
6426 return target;
6427 break;
6429 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6430 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6431 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6432 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6433 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6434 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6435 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6436 if (target)
6437 return target;
6438 break;
6440 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6441 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6442 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6443 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6444 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6445 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6446 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6447 if (target)
6448 return target;
6449 break;
6451 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6452 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6453 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6454 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6455 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6456 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6457 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6458 if (target)
6459 return target;
6460 break;
6462 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6463 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6464 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6465 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6466 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6467 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6468 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6469 if (target)
6470 return target;
6471 break;
6473 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6474 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6475 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6476 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6477 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6478 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6479 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6480 if (target)
6481 return target;
6482 break;
6484 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6485 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6486 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6487 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6488 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6489 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6490 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6491 if (target)
6492 return target;
6493 break;
6495 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6496 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6497 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6498 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6499 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6500 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6501 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6502 if (target)
6503 return target;
6504 break;
6506 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6507 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6508 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6509 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6510 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6511 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6512 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6513 if (target)
6514 return target;
6515 break;
6517 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6518 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6519 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6520 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6521 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6522 if (mode == VOIDmode)
6523 mode = TYPE_MODE (boolean_type_node);
6524 if (!target || !register_operand (target, mode))
6525 target = gen_reg_rtx (mode);
6527 mode = get_builtin_sync_mode
6528 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6529 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6530 if (target)
6531 return target;
6532 break;
6534 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6535 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6536 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6537 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6538 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6539 mode = get_builtin_sync_mode
6540 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6541 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6542 if (target)
6543 return target;
6544 break;
6546 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6547 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6548 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6549 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6550 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6551 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6552 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6553 if (target)
6554 return target;
6555 break;
6557 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6558 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6559 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6560 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6561 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6562 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6563 expand_builtin_sync_lock_release (mode, exp);
6564 return const0_rtx;
6566 case BUILT_IN_SYNC_SYNCHRONIZE:
6567 expand_builtin_sync_synchronize ();
6568 return const0_rtx;
6570 case BUILT_IN_ATOMIC_EXCHANGE_1:
6571 case BUILT_IN_ATOMIC_EXCHANGE_2:
6572 case BUILT_IN_ATOMIC_EXCHANGE_4:
6573 case BUILT_IN_ATOMIC_EXCHANGE_8:
6574 case BUILT_IN_ATOMIC_EXCHANGE_16:
6575 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6576 target = expand_builtin_atomic_exchange (mode, exp, target);
6577 if (target)
6578 return target;
6579 break;
6581 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6582 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6583 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6584 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6585 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6587 unsigned int nargs, z;
6588 vec<tree, va_gc> *vec;
6590 mode =
6591 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6592 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6593 if (target)
6594 return target;
6596 /* If this is turned into an external library call, the weak parameter
6597 must be dropped to match the expected parameter list. */
6598 nargs = call_expr_nargs (exp);
6599 vec_alloc (vec, nargs - 1);
6600 for (z = 0; z < 3; z++)
6601 vec->quick_push (CALL_EXPR_ARG (exp, z));
6602 /* Skip the boolean weak parameter. */
6603 for (z = 4; z < 6; z++)
6604 vec->quick_push (CALL_EXPR_ARG (exp, z));
6605 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6606 break;
6609 case BUILT_IN_ATOMIC_LOAD_1:
6610 case BUILT_IN_ATOMIC_LOAD_2:
6611 case BUILT_IN_ATOMIC_LOAD_4:
6612 case BUILT_IN_ATOMIC_LOAD_8:
6613 case BUILT_IN_ATOMIC_LOAD_16:
6614 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6615 target = expand_builtin_atomic_load (mode, exp, target);
6616 if (target)
6617 return target;
6618 break;
6620 case BUILT_IN_ATOMIC_STORE_1:
6621 case BUILT_IN_ATOMIC_STORE_2:
6622 case BUILT_IN_ATOMIC_STORE_4:
6623 case BUILT_IN_ATOMIC_STORE_8:
6624 case BUILT_IN_ATOMIC_STORE_16:
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6626 target = expand_builtin_atomic_store (mode, exp);
6627 if (target)
6628 return const0_rtx;
6629 break;
6631 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6632 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6633 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6634 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6635 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6637 enum built_in_function lib;
6638 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6639 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6640 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6641 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6642 ignore, lib);
6643 if (target)
6644 return target;
6645 break;
6647 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6648 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6649 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6650 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6651 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6653 enum built_in_function lib;
6654 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6655 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6656 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6657 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6658 ignore, lib);
6659 if (target)
6660 return target;
6661 break;
6663 case BUILT_IN_ATOMIC_AND_FETCH_1:
6664 case BUILT_IN_ATOMIC_AND_FETCH_2:
6665 case BUILT_IN_ATOMIC_AND_FETCH_4:
6666 case BUILT_IN_ATOMIC_AND_FETCH_8:
6667 case BUILT_IN_ATOMIC_AND_FETCH_16:
6669 enum built_in_function lib;
6670 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6671 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6672 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6673 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6674 ignore, lib);
6675 if (target)
6676 return target;
6677 break;
6679 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6680 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6681 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6682 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6683 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6685 enum built_in_function lib;
6686 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6687 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6688 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6689 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6690 ignore, lib);
6691 if (target)
6692 return target;
6693 break;
6695 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6696 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6697 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6698 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6699 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6701 enum built_in_function lib;
6702 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6703 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6704 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6705 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6706 ignore, lib);
6707 if (target)
6708 return target;
6709 break;
6711 case BUILT_IN_ATOMIC_OR_FETCH_1:
6712 case BUILT_IN_ATOMIC_OR_FETCH_2:
6713 case BUILT_IN_ATOMIC_OR_FETCH_4:
6714 case BUILT_IN_ATOMIC_OR_FETCH_8:
6715 case BUILT_IN_ATOMIC_OR_FETCH_16:
6717 enum built_in_function lib;
6718 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6719 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6720 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6721 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6722 ignore, lib);
6723 if (target)
6724 return target;
6725 break;
6727 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6728 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6729 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6730 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6731 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6732 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6733 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6734 ignore, BUILT_IN_NONE);
6735 if (target)
6736 return target;
6737 break;
6739 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6740 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6741 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6742 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6743 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6744 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6745 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6746 ignore, BUILT_IN_NONE);
6747 if (target)
6748 return target;
6749 break;
6751 case BUILT_IN_ATOMIC_FETCH_AND_1:
6752 case BUILT_IN_ATOMIC_FETCH_AND_2:
6753 case BUILT_IN_ATOMIC_FETCH_AND_4:
6754 case BUILT_IN_ATOMIC_FETCH_AND_8:
6755 case BUILT_IN_ATOMIC_FETCH_AND_16:
6756 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6757 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6758 ignore, BUILT_IN_NONE);
6759 if (target)
6760 return target;
6761 break;
6763 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6764 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6765 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6766 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6767 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6768 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6769 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6770 ignore, BUILT_IN_NONE);
6771 if (target)
6772 return target;
6773 break;
6775 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6776 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6777 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6778 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6779 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6780 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6781 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6782 ignore, BUILT_IN_NONE);
6783 if (target)
6784 return target;
6785 break;
6787 case BUILT_IN_ATOMIC_FETCH_OR_1:
6788 case BUILT_IN_ATOMIC_FETCH_OR_2:
6789 case BUILT_IN_ATOMIC_FETCH_OR_4:
6790 case BUILT_IN_ATOMIC_FETCH_OR_8:
6791 case BUILT_IN_ATOMIC_FETCH_OR_16:
6792 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6793 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6794 ignore, BUILT_IN_NONE);
6795 if (target)
6796 return target;
6797 break;
6799 case BUILT_IN_ATOMIC_TEST_AND_SET:
6800 return expand_builtin_atomic_test_and_set (exp, target);
6802 case BUILT_IN_ATOMIC_CLEAR:
6803 return expand_builtin_atomic_clear (exp);
6805 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6806 return expand_builtin_atomic_always_lock_free (exp);
6808 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6809 target = expand_builtin_atomic_is_lock_free (exp);
6810 if (target)
6811 return target;
6812 break;
6814 case BUILT_IN_ATOMIC_THREAD_FENCE:
6815 expand_builtin_atomic_thread_fence (exp);
6816 return const0_rtx;
6818 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6819 expand_builtin_atomic_signal_fence (exp);
6820 return const0_rtx;
6822 case BUILT_IN_OBJECT_SIZE:
6823 return expand_builtin_object_size (exp);
6825 case BUILT_IN_MEMCPY_CHK:
6826 case BUILT_IN_MEMPCPY_CHK:
6827 case BUILT_IN_MEMMOVE_CHK:
6828 case BUILT_IN_MEMSET_CHK:
6829 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6830 if (target)
6831 return target;
6832 break;
6834 case BUILT_IN_STRCPY_CHK:
6835 case BUILT_IN_STPCPY_CHK:
6836 case BUILT_IN_STRNCPY_CHK:
6837 case BUILT_IN_STPNCPY_CHK:
6838 case BUILT_IN_STRCAT_CHK:
6839 case BUILT_IN_STRNCAT_CHK:
6840 case BUILT_IN_SNPRINTF_CHK:
6841 case BUILT_IN_VSNPRINTF_CHK:
6842 maybe_emit_chk_warning (exp, fcode);
6843 break;
6845 case BUILT_IN_SPRINTF_CHK:
6846 case BUILT_IN_VSPRINTF_CHK:
6847 maybe_emit_sprintf_chk_warning (exp, fcode);
6848 break;
6850 case BUILT_IN_FREE:
6851 if (warn_free_nonheap_object)
6852 maybe_emit_free_warning (exp);
6853 break;
6855 case BUILT_IN_THREAD_POINTER:
6856 return expand_builtin_thread_pointer (exp, target);
6858 case BUILT_IN_SET_THREAD_POINTER:
6859 expand_builtin_set_thread_pointer (exp);
6860 return const0_rtx;
6862 default: /* just do library call, if unknown builtin */
6863 break;
6866 /* The switch statement above can drop through to cause the function
6867 to be called normally. */
6868 return expand_call (exp, target, ignore);
6871 /* Determine whether a tree node represents a call to a built-in
6872 function. If the tree T is a call to a built-in function with
6873 the right number of arguments of the appropriate types, return
6874 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6875 Otherwise the return value is END_BUILTINS. */
6877 enum built_in_function
6878 builtin_mathfn_code (const_tree t)
6880 const_tree fndecl, arg, parmlist;
6881 const_tree argtype, parmtype;
6882 const_call_expr_arg_iterator iter;
6884 if (TREE_CODE (t) != CALL_EXPR
6885 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6886 return END_BUILTINS;
6888 fndecl = get_callee_fndecl (t);
6889 if (fndecl == NULL_TREE
6890 || TREE_CODE (fndecl) != FUNCTION_DECL
6891 || ! DECL_BUILT_IN (fndecl)
6892 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6893 return END_BUILTINS;
6895 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6896 init_const_call_expr_arg_iterator (t, &iter);
6897 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6899 /* If a function doesn't take a variable number of arguments,
6900 the last element in the list will have type `void'. */
6901 parmtype = TREE_VALUE (parmlist);
6902 if (VOID_TYPE_P (parmtype))
6904 if (more_const_call_expr_args_p (&iter))
6905 return END_BUILTINS;
6906 return DECL_FUNCTION_CODE (fndecl);
6909 if (! more_const_call_expr_args_p (&iter))
6910 return END_BUILTINS;
6912 arg = next_const_call_expr_arg (&iter);
6913 argtype = TREE_TYPE (arg);
6915 if (SCALAR_FLOAT_TYPE_P (parmtype))
6917 if (! SCALAR_FLOAT_TYPE_P (argtype))
6918 return END_BUILTINS;
6920 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6922 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6923 return END_BUILTINS;
6925 else if (POINTER_TYPE_P (parmtype))
6927 if (! POINTER_TYPE_P (argtype))
6928 return END_BUILTINS;
6930 else if (INTEGRAL_TYPE_P (parmtype))
6932 if (! INTEGRAL_TYPE_P (argtype))
6933 return END_BUILTINS;
6935 else
6936 return END_BUILTINS;
6939 /* Variable-length argument list. */
6940 return DECL_FUNCTION_CODE (fndecl);
6943 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6944 evaluate to a constant. */
6946 static tree
6947 fold_builtin_constant_p (tree arg)
6949 /* We return 1 for a numeric type that's known to be a constant
6950 value at compile-time or for an aggregate type that's a
6951 literal constant. */
6952 STRIP_NOPS (arg);
6954 /* If we know this is a constant, emit the constant of one. */
6955 if (CONSTANT_CLASS_P (arg)
6956 || (TREE_CODE (arg) == CONSTRUCTOR
6957 && TREE_CONSTANT (arg)))
6958 return integer_one_node;
6959 if (TREE_CODE (arg) == ADDR_EXPR)
6961 tree op = TREE_OPERAND (arg, 0);
6962 if (TREE_CODE (op) == STRING_CST
6963 || (TREE_CODE (op) == ARRAY_REF
6964 && integer_zerop (TREE_OPERAND (op, 1))
6965 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6966 return integer_one_node;
6969 /* If this expression has side effects, show we don't know it to be a
6970 constant. Likewise if it's a pointer or aggregate type since in
6971 those case we only want literals, since those are only optimized
6972 when generating RTL, not later.
6973 And finally, if we are compiling an initializer, not code, we
6974 need to return a definite result now; there's not going to be any
6975 more optimization done. */
6976 if (TREE_SIDE_EFFECTS (arg)
6977 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6978 || POINTER_TYPE_P (TREE_TYPE (arg))
6979 || cfun == 0
6980 || folding_initializer
6981 || force_folding_builtin_constant_p)
6982 return integer_zero_node;
6984 return NULL_TREE;
6987 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6988 return it as a truthvalue. */
6990 static tree
6991 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6993 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6995 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6996 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6997 ret_type = TREE_TYPE (TREE_TYPE (fn));
6998 pred_type = TREE_VALUE (arg_types);
6999 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7001 pred = fold_convert_loc (loc, pred_type, pred);
7002 expected = fold_convert_loc (loc, expected_type, expected);
7003 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
7005 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7006 build_int_cst (ret_type, 0));
7009 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7010 NULL_TREE if no simplification is possible. */
7012 static tree
7013 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7015 tree inner, fndecl, inner_arg0;
7016 enum tree_code code;
7018 /* Distribute the expected value over short-circuiting operators.
7019 See through the cast from truthvalue_type_node to long. */
7020 inner_arg0 = arg0;
7021 while (TREE_CODE (inner_arg0) == NOP_EXPR
7022 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7023 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7024 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7026 /* If this is a builtin_expect within a builtin_expect keep the
7027 inner one. See through a comparison against a constant. It
7028 might have been added to create a thruthvalue. */
7029 inner = inner_arg0;
7031 if (COMPARISON_CLASS_P (inner)
7032 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7033 inner = TREE_OPERAND (inner, 0);
7035 if (TREE_CODE (inner) == CALL_EXPR
7036 && (fndecl = get_callee_fndecl (inner))
7037 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7038 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7039 return arg0;
7041 inner = inner_arg0;
7042 code = TREE_CODE (inner);
7043 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7045 tree op0 = TREE_OPERAND (inner, 0);
7046 tree op1 = TREE_OPERAND (inner, 1);
7048 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7049 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7050 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7052 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7055 /* If the argument isn't invariant then there's nothing else we can do. */
7056 if (!TREE_CONSTANT (inner_arg0))
7057 return NULL_TREE;
7059 /* If we expect that a comparison against the argument will fold to
7060 a constant return the constant. In practice, this means a true
7061 constant or the address of a non-weak symbol. */
7062 inner = inner_arg0;
7063 STRIP_NOPS (inner);
7064 if (TREE_CODE (inner) == ADDR_EXPR)
7068 inner = TREE_OPERAND (inner, 0);
7070 while (TREE_CODE (inner) == COMPONENT_REF
7071 || TREE_CODE (inner) == ARRAY_REF);
7072 if ((TREE_CODE (inner) == VAR_DECL
7073 || TREE_CODE (inner) == FUNCTION_DECL)
7074 && DECL_WEAK (inner))
7075 return NULL_TREE;
7078 /* Otherwise, ARG0 already has the proper type for the return value. */
7079 return arg0;
7082 /* Fold a call to __builtin_classify_type with argument ARG. */
7084 static tree
7085 fold_builtin_classify_type (tree arg)
7087 if (arg == 0)
7088 return build_int_cst (integer_type_node, no_type_class);
7090 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7093 /* Fold a call to __builtin_strlen with argument ARG. */
7095 static tree
7096 fold_builtin_strlen (location_t loc, tree type, tree arg)
7098 if (!validate_arg (arg, POINTER_TYPE))
7099 return NULL_TREE;
7100 else
7102 tree len = c_strlen (arg, 0);
7104 if (len)
7105 return fold_convert_loc (loc, type, len);
7107 return NULL_TREE;
7111 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7113 static tree
7114 fold_builtin_inf (location_t loc, tree type, int warn)
7116 REAL_VALUE_TYPE real;
7118 /* __builtin_inff is intended to be usable to define INFINITY on all
7119 targets. If an infinity is not available, INFINITY expands "to a
7120 positive constant of type float that overflows at translation
7121 time", footnote "In this case, using INFINITY will violate the
7122 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7123 Thus we pedwarn to ensure this constraint violation is
7124 diagnosed. */
7125 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7126 pedwarn (loc, 0, "target format does not support infinity");
7128 real_inf (&real);
7129 return build_real (type, real);
7132 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7134 static tree
7135 fold_builtin_nan (tree arg, tree type, int quiet)
7137 REAL_VALUE_TYPE real;
7138 const char *str;
7140 if (!validate_arg (arg, POINTER_TYPE))
7141 return NULL_TREE;
7142 str = c_getstr (arg);
7143 if (!str)
7144 return NULL_TREE;
7146 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7147 return NULL_TREE;
7149 return build_real (type, real);
7152 /* Return true if the floating point expression T has an integer value.
7153 We also allow +Inf, -Inf and NaN to be considered integer values. */
7155 static bool
7156 integer_valued_real_p (tree t)
7158 switch (TREE_CODE (t))
7160 case FLOAT_EXPR:
7161 return true;
7163 case ABS_EXPR:
7164 case SAVE_EXPR:
7165 return integer_valued_real_p (TREE_OPERAND (t, 0));
7167 case COMPOUND_EXPR:
7168 case MODIFY_EXPR:
7169 case BIND_EXPR:
7170 return integer_valued_real_p (TREE_OPERAND (t, 1));
7172 case PLUS_EXPR:
7173 case MINUS_EXPR:
7174 case MULT_EXPR:
7175 case MIN_EXPR:
7176 case MAX_EXPR:
7177 return integer_valued_real_p (TREE_OPERAND (t, 0))
7178 && integer_valued_real_p (TREE_OPERAND (t, 1));
7180 case COND_EXPR:
7181 return integer_valued_real_p (TREE_OPERAND (t, 1))
7182 && integer_valued_real_p (TREE_OPERAND (t, 2));
7184 case REAL_CST:
7185 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7187 case NOP_EXPR:
7189 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7190 if (TREE_CODE (type) == INTEGER_TYPE)
7191 return true;
7192 if (TREE_CODE (type) == REAL_TYPE)
7193 return integer_valued_real_p (TREE_OPERAND (t, 0));
7194 break;
7197 case CALL_EXPR:
7198 switch (builtin_mathfn_code (t))
7200 CASE_FLT_FN (BUILT_IN_CEIL):
7201 CASE_FLT_FN (BUILT_IN_FLOOR):
7202 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7203 CASE_FLT_FN (BUILT_IN_RINT):
7204 CASE_FLT_FN (BUILT_IN_ROUND):
7205 CASE_FLT_FN (BUILT_IN_TRUNC):
7206 return true;
7208 CASE_FLT_FN (BUILT_IN_FMIN):
7209 CASE_FLT_FN (BUILT_IN_FMAX):
7210 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7211 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7213 default:
7214 break;
7216 break;
7218 default:
7219 break;
7221 return false;
7224 /* FNDECL is assumed to be a builtin where truncation can be propagated
7225 across (for instance floor((double)f) == (double)floorf (f).
7226 Do the transformation for a call with argument ARG. */
7228 static tree
7229 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7231 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7233 if (!validate_arg (arg, REAL_TYPE))
7234 return NULL_TREE;
7236 /* Integer rounding functions are idempotent. */
7237 if (fcode == builtin_mathfn_code (arg))
7238 return arg;
7240 /* If argument is already integer valued, and we don't need to worry
7241 about setting errno, there's no need to perform rounding. */
7242 if (! flag_errno_math && integer_valued_real_p (arg))
7243 return arg;
7245 if (optimize)
7247 tree arg0 = strip_float_extensions (arg);
7248 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7249 tree newtype = TREE_TYPE (arg0);
7250 tree decl;
7252 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7253 && (decl = mathfn_built_in (newtype, fcode)))
7254 return fold_convert_loc (loc, ftype,
7255 build_call_expr_loc (loc, decl, 1,
7256 fold_convert_loc (loc,
7257 newtype,
7258 arg0)));
7260 return NULL_TREE;
7263 /* FNDECL is assumed to be builtin which can narrow the FP type of
7264 the argument, for instance lround((double)f) -> lroundf (f).
7265 Do the transformation for a call with argument ARG. */
7267 static tree
7268 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7270 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7272 if (!validate_arg (arg, REAL_TYPE))
7273 return NULL_TREE;
7275 /* If argument is already integer valued, and we don't need to worry
7276 about setting errno, there's no need to perform rounding. */
7277 if (! flag_errno_math && integer_valued_real_p (arg))
7278 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7279 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7281 if (optimize)
7283 tree ftype = TREE_TYPE (arg);
7284 tree arg0 = strip_float_extensions (arg);
7285 tree newtype = TREE_TYPE (arg0);
7286 tree decl;
7288 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7289 && (decl = mathfn_built_in (newtype, fcode)))
7290 return build_call_expr_loc (loc, decl, 1,
7291 fold_convert_loc (loc, newtype, arg0));
7294 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7295 sizeof (int) == sizeof (long). */
7296 if (TYPE_PRECISION (integer_type_node)
7297 == TYPE_PRECISION (long_integer_type_node))
7299 tree newfn = NULL_TREE;
7300 switch (fcode)
7302 CASE_FLT_FN (BUILT_IN_ICEIL):
7303 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7304 break;
7306 CASE_FLT_FN (BUILT_IN_IFLOOR):
7307 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7308 break;
7310 CASE_FLT_FN (BUILT_IN_IROUND):
7311 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7312 break;
7314 CASE_FLT_FN (BUILT_IN_IRINT):
7315 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7316 break;
7318 default:
7319 break;
7322 if (newfn)
7324 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7325 return fold_convert_loc (loc,
7326 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7330 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7331 sizeof (long long) == sizeof (long). */
7332 if (TYPE_PRECISION (long_long_integer_type_node)
7333 == TYPE_PRECISION (long_integer_type_node))
7335 tree newfn = NULL_TREE;
7336 switch (fcode)
7338 CASE_FLT_FN (BUILT_IN_LLCEIL):
7339 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7340 break;
7342 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7343 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7344 break;
7346 CASE_FLT_FN (BUILT_IN_LLROUND):
7347 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7348 break;
7350 CASE_FLT_FN (BUILT_IN_LLRINT):
7351 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7352 break;
7354 default:
7355 break;
7358 if (newfn)
7360 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7361 return fold_convert_loc (loc,
7362 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7366 return NULL_TREE;
7369 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7370 return type. Return NULL_TREE if no simplification can be made. */
7372 static tree
7373 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7375 tree res;
7377 if (!validate_arg (arg, COMPLEX_TYPE)
7378 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7379 return NULL_TREE;
7381 /* Calculate the result when the argument is a constant. */
7382 if (TREE_CODE (arg) == COMPLEX_CST
7383 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7384 type, mpfr_hypot)))
7385 return res;
7387 if (TREE_CODE (arg) == COMPLEX_EXPR)
7389 tree real = TREE_OPERAND (arg, 0);
7390 tree imag = TREE_OPERAND (arg, 1);
7392 /* If either part is zero, cabs is fabs of the other. */
7393 if (real_zerop (real))
7394 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7395 if (real_zerop (imag))
7396 return fold_build1_loc (loc, ABS_EXPR, type, real);
7398 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7399 if (flag_unsafe_math_optimizations
7400 && operand_equal_p (real, imag, OEP_PURE_SAME))
7402 const REAL_VALUE_TYPE sqrt2_trunc
7403 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7404 STRIP_NOPS (real);
7405 return fold_build2_loc (loc, MULT_EXPR, type,
7406 fold_build1_loc (loc, ABS_EXPR, type, real),
7407 build_real (type, sqrt2_trunc));
7411 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7412 if (TREE_CODE (arg) == NEGATE_EXPR
7413 || TREE_CODE (arg) == CONJ_EXPR)
7414 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7416 /* Don't do this when optimizing for size. */
7417 if (flag_unsafe_math_optimizations
7418 && optimize && optimize_function_for_speed_p (cfun))
7420 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7422 if (sqrtfn != NULL_TREE)
7424 tree rpart, ipart, result;
7426 arg = builtin_save_expr (arg);
7428 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7429 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7431 rpart = builtin_save_expr (rpart);
7432 ipart = builtin_save_expr (ipart);
7434 result = fold_build2_loc (loc, PLUS_EXPR, type,
7435 fold_build2_loc (loc, MULT_EXPR, type,
7436 rpart, rpart),
7437 fold_build2_loc (loc, MULT_EXPR, type,
7438 ipart, ipart));
7440 return build_call_expr_loc (loc, sqrtfn, 1, result);
7444 return NULL_TREE;
7447 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7448 complex tree type of the result. If NEG is true, the imaginary
7449 zero is negative. */
7451 static tree
7452 build_complex_cproj (tree type, bool neg)
7454 REAL_VALUE_TYPE rinf, rzero = dconst0;
7456 real_inf (&rinf);
7457 rzero.sign = neg;
7458 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7459 build_real (TREE_TYPE (type), rzero));
7462 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7463 return type. Return NULL_TREE if no simplification can be made. */
7465 static tree
7466 fold_builtin_cproj (location_t loc, tree arg, tree type)
7468 if (!validate_arg (arg, COMPLEX_TYPE)
7469 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7470 return NULL_TREE;
7472 /* If there are no infinities, return arg. */
7473 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7474 return non_lvalue_loc (loc, arg);
7476 /* Calculate the result when the argument is a constant. */
7477 if (TREE_CODE (arg) == COMPLEX_CST)
7479 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7480 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7482 if (real_isinf (real) || real_isinf (imag))
7483 return build_complex_cproj (type, imag->sign);
7484 else
7485 return arg;
7487 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7489 tree real = TREE_OPERAND (arg, 0);
7490 tree imag = TREE_OPERAND (arg, 1);
7492 STRIP_NOPS (real);
7493 STRIP_NOPS (imag);
7495 /* If the real part is inf and the imag part is known to be
7496 nonnegative, return (inf + 0i). Remember side-effects are
7497 possible in the imag part. */
7498 if (TREE_CODE (real) == REAL_CST
7499 && real_isinf (TREE_REAL_CST_PTR (real))
7500 && tree_expr_nonnegative_p (imag))
7501 return omit_one_operand_loc (loc, type,
7502 build_complex_cproj (type, false),
7503 arg);
7505 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7506 Remember side-effects are possible in the real part. */
7507 if (TREE_CODE (imag) == REAL_CST
7508 && real_isinf (TREE_REAL_CST_PTR (imag)))
7509 return
7510 omit_one_operand_loc (loc, type,
7511 build_complex_cproj (type, TREE_REAL_CST_PTR
7512 (imag)->sign), arg);
7515 return NULL_TREE;
7518 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7519 Return NULL_TREE if no simplification can be made. */
7521 static tree
7522 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7525 enum built_in_function fcode;
7526 tree res;
7528 if (!validate_arg (arg, REAL_TYPE))
7529 return NULL_TREE;
7531 /* Calculate the result when the argument is a constant. */
7532 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7533 return res;
7535 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7536 fcode = builtin_mathfn_code (arg);
7537 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7539 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7540 arg = fold_build2_loc (loc, MULT_EXPR, type,
7541 CALL_EXPR_ARG (arg, 0),
7542 build_real (type, dconsthalf));
7543 return build_call_expr_loc (loc, expfn, 1, arg);
7546 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7547 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7549 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7551 if (powfn)
7553 tree arg0 = CALL_EXPR_ARG (arg, 0);
7554 tree tree_root;
7555 /* The inner root was either sqrt or cbrt. */
7556 /* This was a conditional expression but it triggered a bug
7557 in Sun C 5.5. */
7558 REAL_VALUE_TYPE dconstroot;
7559 if (BUILTIN_SQRT_P (fcode))
7560 dconstroot = dconsthalf;
7561 else
7562 dconstroot = dconst_third ();
7564 /* Adjust for the outer root. */
7565 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7566 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7567 tree_root = build_real (type, dconstroot);
7568 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7572 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7573 if (flag_unsafe_math_optimizations
7574 && (fcode == BUILT_IN_POW
7575 || fcode == BUILT_IN_POWF
7576 || fcode == BUILT_IN_POWL))
7578 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7579 tree arg0 = CALL_EXPR_ARG (arg, 0);
7580 tree arg1 = CALL_EXPR_ARG (arg, 1);
7581 tree narg1;
7582 if (!tree_expr_nonnegative_p (arg0))
7583 arg0 = build1 (ABS_EXPR, type, arg0);
7584 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7585 build_real (type, dconsthalf));
7586 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7589 return NULL_TREE;
7592 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7593 Return NULL_TREE if no simplification can be made. */
7595 static tree
7596 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7598 const enum built_in_function fcode = builtin_mathfn_code (arg);
7599 tree res;
7601 if (!validate_arg (arg, REAL_TYPE))
7602 return NULL_TREE;
7604 /* Calculate the result when the argument is a constant. */
7605 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7606 return res;
7608 if (flag_unsafe_math_optimizations)
7610 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7611 if (BUILTIN_EXPONENT_P (fcode))
7613 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7614 const REAL_VALUE_TYPE third_trunc =
7615 real_value_truncate (TYPE_MODE (type), dconst_third ());
7616 arg = fold_build2_loc (loc, MULT_EXPR, type,
7617 CALL_EXPR_ARG (arg, 0),
7618 build_real (type, third_trunc));
7619 return build_call_expr_loc (loc, expfn, 1, arg);
7622 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7623 if (BUILTIN_SQRT_P (fcode))
7625 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7627 if (powfn)
7629 tree arg0 = CALL_EXPR_ARG (arg, 0);
7630 tree tree_root;
7631 REAL_VALUE_TYPE dconstroot = dconst_third ();
7633 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7634 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7635 tree_root = build_real (type, dconstroot);
7636 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7640 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7641 if (BUILTIN_CBRT_P (fcode))
7643 tree arg0 = CALL_EXPR_ARG (arg, 0);
7644 if (tree_expr_nonnegative_p (arg0))
7646 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7648 if (powfn)
7650 tree tree_root;
7651 REAL_VALUE_TYPE dconstroot;
7653 real_arithmetic (&dconstroot, MULT_EXPR,
7654 dconst_third_ptr (), dconst_third_ptr ());
7655 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7656 tree_root = build_real (type, dconstroot);
7657 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7662 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7663 if (fcode == BUILT_IN_POW
7664 || fcode == BUILT_IN_POWF
7665 || fcode == BUILT_IN_POWL)
7667 tree arg00 = CALL_EXPR_ARG (arg, 0);
7668 tree arg01 = CALL_EXPR_ARG (arg, 1);
7669 if (tree_expr_nonnegative_p (arg00))
7671 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7672 const REAL_VALUE_TYPE dconstroot
7673 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7674 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7675 build_real (type, dconstroot));
7676 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7680 return NULL_TREE;
7683 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7684 TYPE is the type of the return value. Return NULL_TREE if no
7685 simplification can be made. */
7687 static tree
7688 fold_builtin_cos (location_t loc,
7689 tree arg, tree type, tree fndecl)
7691 tree res, narg;
7693 if (!validate_arg (arg, REAL_TYPE))
7694 return NULL_TREE;
7696 /* Calculate the result when the argument is a constant. */
7697 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7698 return res;
7700 /* Optimize cos(-x) into cos (x). */
7701 if ((narg = fold_strip_sign_ops (arg)))
7702 return build_call_expr_loc (loc, fndecl, 1, narg);
7704 return NULL_TREE;
7707 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7708 Return NULL_TREE if no simplification can be made. */
7710 static tree
7711 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7713 if (validate_arg (arg, REAL_TYPE))
7715 tree res, narg;
7717 /* Calculate the result when the argument is a constant. */
7718 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7719 return res;
7721 /* Optimize cosh(-x) into cosh (x). */
7722 if ((narg = fold_strip_sign_ops (arg)))
7723 return build_call_expr_loc (loc, fndecl, 1, narg);
7726 return NULL_TREE;
7729 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7730 argument ARG. TYPE is the type of the return value. Return
7731 NULL_TREE if no simplification can be made. */
7733 static tree
7734 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7735 bool hyper)
7737 if (validate_arg (arg, COMPLEX_TYPE)
7738 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7740 tree tmp;
7742 /* Calculate the result when the argument is a constant. */
7743 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7744 return tmp;
7746 /* Optimize fn(-x) into fn(x). */
7747 if ((tmp = fold_strip_sign_ops (arg)))
7748 return build_call_expr_loc (loc, fndecl, 1, tmp);
7751 return NULL_TREE;
7754 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7755 Return NULL_TREE if no simplification can be made. */
7757 static tree
7758 fold_builtin_tan (tree arg, tree type)
7760 enum built_in_function fcode;
7761 tree res;
7763 if (!validate_arg (arg, REAL_TYPE))
7764 return NULL_TREE;
7766 /* Calculate the result when the argument is a constant. */
7767 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7768 return res;
7770 /* Optimize tan(atan(x)) = x. */
7771 fcode = builtin_mathfn_code (arg);
7772 if (flag_unsafe_math_optimizations
7773 && (fcode == BUILT_IN_ATAN
7774 || fcode == BUILT_IN_ATANF
7775 || fcode == BUILT_IN_ATANL))
7776 return CALL_EXPR_ARG (arg, 0);
7778 return NULL_TREE;
7781 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7782 NULL_TREE if no simplification can be made. */
7784 static tree
7785 fold_builtin_sincos (location_t loc,
7786 tree arg0, tree arg1, tree arg2)
7788 tree type;
7789 tree res, fn, call;
7791 if (!validate_arg (arg0, REAL_TYPE)
7792 || !validate_arg (arg1, POINTER_TYPE)
7793 || !validate_arg (arg2, POINTER_TYPE))
7794 return NULL_TREE;
7796 type = TREE_TYPE (arg0);
7798 /* Calculate the result when the argument is a constant. */
7799 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7800 return res;
7802 /* Canonicalize sincos to cexpi. */
7803 if (!TARGET_C99_FUNCTIONS)
7804 return NULL_TREE;
7805 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7806 if (!fn)
7807 return NULL_TREE;
7809 call = build_call_expr_loc (loc, fn, 1, arg0);
7810 call = builtin_save_expr (call);
7812 return build2 (COMPOUND_EXPR, void_type_node,
7813 build2 (MODIFY_EXPR, void_type_node,
7814 build_fold_indirect_ref_loc (loc, arg1),
7815 build1 (IMAGPART_EXPR, type, call)),
7816 build2 (MODIFY_EXPR, void_type_node,
7817 build_fold_indirect_ref_loc (loc, arg2),
7818 build1 (REALPART_EXPR, type, call)));
7821 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7822 NULL_TREE if no simplification can be made. */
7824 static tree
7825 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7827 tree rtype;
7828 tree realp, imagp, ifn;
7829 tree res;
7831 if (!validate_arg (arg0, COMPLEX_TYPE)
7832 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7833 return NULL_TREE;
7835 /* Calculate the result when the argument is a constant. */
7836 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7837 return res;
7839 rtype = TREE_TYPE (TREE_TYPE (arg0));
7841 /* In case we can figure out the real part of arg0 and it is constant zero
7842 fold to cexpi. */
7843 if (!TARGET_C99_FUNCTIONS)
7844 return NULL_TREE;
7845 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7846 if (!ifn)
7847 return NULL_TREE;
7849 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7850 && real_zerop (realp))
7852 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7853 return build_call_expr_loc (loc, ifn, 1, narg);
7856 /* In case we can easily decompose real and imaginary parts split cexp
7857 to exp (r) * cexpi (i). */
7858 if (flag_unsafe_math_optimizations
7859 && realp)
7861 tree rfn, rcall, icall;
7863 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7864 if (!rfn)
7865 return NULL_TREE;
7867 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7868 if (!imagp)
7869 return NULL_TREE;
7871 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7872 icall = builtin_save_expr (icall);
7873 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7874 rcall = builtin_save_expr (rcall);
7875 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7876 fold_build2_loc (loc, MULT_EXPR, rtype,
7877 rcall,
7878 fold_build1_loc (loc, REALPART_EXPR,
7879 rtype, icall)),
7880 fold_build2_loc (loc, MULT_EXPR, rtype,
7881 rcall,
7882 fold_build1_loc (loc, IMAGPART_EXPR,
7883 rtype, icall)));
7886 return NULL_TREE;
7889 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7890 Return NULL_TREE if no simplification can be made. */
7892 static tree
7893 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7895 if (!validate_arg (arg, REAL_TYPE))
7896 return NULL_TREE;
7898 /* Optimize trunc of constant value. */
7899 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7901 REAL_VALUE_TYPE r, x;
7902 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7904 x = TREE_REAL_CST (arg);
7905 real_trunc (&r, TYPE_MODE (type), &x);
7906 return build_real (type, r);
7909 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7912 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7913 Return NULL_TREE if no simplification can be made. */
7915 static tree
7916 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7918 if (!validate_arg (arg, REAL_TYPE))
7919 return NULL_TREE;
7921 /* Optimize floor of constant value. */
7922 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7924 REAL_VALUE_TYPE x;
7926 x = TREE_REAL_CST (arg);
7927 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7929 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7930 REAL_VALUE_TYPE r;
7932 real_floor (&r, TYPE_MODE (type), &x);
7933 return build_real (type, r);
7937 /* Fold floor (x) where x is nonnegative to trunc (x). */
7938 if (tree_expr_nonnegative_p (arg))
7940 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7941 if (truncfn)
7942 return build_call_expr_loc (loc, truncfn, 1, arg);
7945 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7948 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7949 Return NULL_TREE if no simplification can be made. */
7951 static tree
7952 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7954 if (!validate_arg (arg, REAL_TYPE))
7955 return NULL_TREE;
7957 /* Optimize ceil of constant value. */
7958 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7960 REAL_VALUE_TYPE x;
7962 x = TREE_REAL_CST (arg);
7963 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7965 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7966 REAL_VALUE_TYPE r;
7968 real_ceil (&r, TYPE_MODE (type), &x);
7969 return build_real (type, r);
7973 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7976 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7977 Return NULL_TREE if no simplification can be made. */
7979 static tree
7980 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7982 if (!validate_arg (arg, REAL_TYPE))
7983 return NULL_TREE;
7985 /* Optimize round of constant value. */
7986 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7988 REAL_VALUE_TYPE x;
7990 x = TREE_REAL_CST (arg);
7991 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7993 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7994 REAL_VALUE_TYPE r;
7996 real_round (&r, TYPE_MODE (type), &x);
7997 return build_real (type, r);
8001 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8004 /* Fold function call to builtin lround, lroundf or lroundl (or the
8005 corresponding long long versions) and other rounding functions. ARG
8006 is the argument to the call. Return NULL_TREE if no simplification
8007 can be made. */
8009 static tree
8010 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8012 if (!validate_arg (arg, REAL_TYPE))
8013 return NULL_TREE;
8015 /* Optimize lround of constant value. */
8016 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8018 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8020 if (real_isfinite (&x))
8022 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8023 tree ftype = TREE_TYPE (arg);
8024 double_int val;
8025 REAL_VALUE_TYPE r;
8027 switch (DECL_FUNCTION_CODE (fndecl))
8029 CASE_FLT_FN (BUILT_IN_IFLOOR):
8030 CASE_FLT_FN (BUILT_IN_LFLOOR):
8031 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8032 real_floor (&r, TYPE_MODE (ftype), &x);
8033 break;
8035 CASE_FLT_FN (BUILT_IN_ICEIL):
8036 CASE_FLT_FN (BUILT_IN_LCEIL):
8037 CASE_FLT_FN (BUILT_IN_LLCEIL):
8038 real_ceil (&r, TYPE_MODE (ftype), &x);
8039 break;
8041 CASE_FLT_FN (BUILT_IN_IROUND):
8042 CASE_FLT_FN (BUILT_IN_LROUND):
8043 CASE_FLT_FN (BUILT_IN_LLROUND):
8044 real_round (&r, TYPE_MODE (ftype), &x);
8045 break;
8047 default:
8048 gcc_unreachable ();
8051 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8052 if (double_int_fits_to_tree_p (itype, val))
8053 return double_int_to_tree (itype, val);
8057 switch (DECL_FUNCTION_CODE (fndecl))
8059 CASE_FLT_FN (BUILT_IN_LFLOOR):
8060 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8061 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8062 if (tree_expr_nonnegative_p (arg))
8063 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8064 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8065 break;
8066 default:;
8069 return fold_fixed_mathfn (loc, fndecl, arg);
8072 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8073 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8074 the argument to the call. Return NULL_TREE if no simplification can
8075 be made. */
8077 static tree
8078 fold_builtin_bitop (tree fndecl, tree arg)
8080 if (!validate_arg (arg, INTEGER_TYPE))
8081 return NULL_TREE;
8083 /* Optimize for constant argument. */
8084 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8086 HOST_WIDE_INT hi, width, result;
8087 unsigned HOST_WIDE_INT lo;
8088 tree type;
8090 type = TREE_TYPE (arg);
8091 width = TYPE_PRECISION (type);
8092 lo = TREE_INT_CST_LOW (arg);
8094 /* Clear all the bits that are beyond the type's precision. */
8095 if (width > HOST_BITS_PER_WIDE_INT)
8097 hi = TREE_INT_CST_HIGH (arg);
8098 if (width < HOST_BITS_PER_DOUBLE_INT)
8099 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8100 << (width - HOST_BITS_PER_WIDE_INT));
8102 else
8104 hi = 0;
8105 if (width < HOST_BITS_PER_WIDE_INT)
8106 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8109 switch (DECL_FUNCTION_CODE (fndecl))
8111 CASE_INT_FN (BUILT_IN_FFS):
8112 if (lo != 0)
8113 result = ffs_hwi (lo);
8114 else if (hi != 0)
8115 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8116 else
8117 result = 0;
8118 break;
8120 CASE_INT_FN (BUILT_IN_CLZ):
8121 if (hi != 0)
8122 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8123 else if (lo != 0)
8124 result = width - floor_log2 (lo) - 1;
8125 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8126 result = width;
8127 break;
8129 CASE_INT_FN (BUILT_IN_CTZ):
8130 if (lo != 0)
8131 result = ctz_hwi (lo);
8132 else if (hi != 0)
8133 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8134 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8135 result = width;
8136 break;
8138 CASE_INT_FN (BUILT_IN_CLRSB):
8139 if (width > HOST_BITS_PER_WIDE_INT
8140 && (hi & ((unsigned HOST_WIDE_INT) 1
8141 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8143 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8144 << (width - HOST_BITS_PER_WIDE_INT - 1));
8145 lo = ~lo;
8147 else if (width <= HOST_BITS_PER_WIDE_INT
8148 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8149 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8150 if (hi != 0)
8151 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8152 else if (lo != 0)
8153 result = width - floor_log2 (lo) - 2;
8154 else
8155 result = width - 1;
8156 break;
8158 CASE_INT_FN (BUILT_IN_POPCOUNT):
8159 result = 0;
8160 while (lo)
8161 result++, lo &= lo - 1;
8162 while (hi)
8163 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8164 break;
8166 CASE_INT_FN (BUILT_IN_PARITY):
8167 result = 0;
8168 while (lo)
8169 result++, lo &= lo - 1;
8170 while (hi)
8171 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8172 result &= 1;
8173 break;
8175 default:
8176 gcc_unreachable ();
8179 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8182 return NULL_TREE;
8185 /* Fold function call to builtin_bswap and the short, long and long long
8186 variants. Return NULL_TREE if no simplification can be made. */
8187 static tree
8188 fold_builtin_bswap (tree fndecl, tree arg)
8190 if (! validate_arg (arg, INTEGER_TYPE))
8191 return NULL_TREE;
8193 /* Optimize constant value. */
8194 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8196 HOST_WIDE_INT hi, width, r_hi = 0;
8197 unsigned HOST_WIDE_INT lo, r_lo = 0;
8198 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8200 width = TYPE_PRECISION (type);
8201 lo = TREE_INT_CST_LOW (arg);
8202 hi = TREE_INT_CST_HIGH (arg);
8204 switch (DECL_FUNCTION_CODE (fndecl))
8206 case BUILT_IN_BSWAP16:
8207 case BUILT_IN_BSWAP32:
8208 case BUILT_IN_BSWAP64:
8210 int s;
8212 for (s = 0; s < width; s += 8)
8214 int d = width - s - 8;
8215 unsigned HOST_WIDE_INT byte;
8217 if (s < HOST_BITS_PER_WIDE_INT)
8218 byte = (lo >> s) & 0xff;
8219 else
8220 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8222 if (d < HOST_BITS_PER_WIDE_INT)
8223 r_lo |= byte << d;
8224 else
8225 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8229 break;
8231 default:
8232 gcc_unreachable ();
8235 if (width < HOST_BITS_PER_WIDE_INT)
8236 return build_int_cst (type, r_lo);
8237 else
8238 return build_int_cst_wide (type, r_lo, r_hi);
8241 return NULL_TREE;
8244 /* A subroutine of fold_builtin to fold the various logarithmic
8245 functions. Return NULL_TREE if no simplification can me made.
8246 FUNC is the corresponding MPFR logarithm function. */
8248 static tree
8249 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8250 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8252 if (validate_arg (arg, REAL_TYPE))
8254 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8255 tree res;
8256 const enum built_in_function fcode = builtin_mathfn_code (arg);
8258 /* Calculate the result when the argument is a constant. */
8259 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8260 return res;
8262 /* Special case, optimize logN(expN(x)) = x. */
8263 if (flag_unsafe_math_optimizations
8264 && ((func == mpfr_log
8265 && (fcode == BUILT_IN_EXP
8266 || fcode == BUILT_IN_EXPF
8267 || fcode == BUILT_IN_EXPL))
8268 || (func == mpfr_log2
8269 && (fcode == BUILT_IN_EXP2
8270 || fcode == BUILT_IN_EXP2F
8271 || fcode == BUILT_IN_EXP2L))
8272 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8273 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8275 /* Optimize logN(func()) for various exponential functions. We
8276 want to determine the value "x" and the power "exponent" in
8277 order to transform logN(x**exponent) into exponent*logN(x). */
8278 if (flag_unsafe_math_optimizations)
8280 tree exponent = 0, x = 0;
8282 switch (fcode)
8284 CASE_FLT_FN (BUILT_IN_EXP):
8285 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8286 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8287 dconst_e ()));
8288 exponent = CALL_EXPR_ARG (arg, 0);
8289 break;
8290 CASE_FLT_FN (BUILT_IN_EXP2):
8291 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8292 x = build_real (type, dconst2);
8293 exponent = CALL_EXPR_ARG (arg, 0);
8294 break;
8295 CASE_FLT_FN (BUILT_IN_EXP10):
8296 CASE_FLT_FN (BUILT_IN_POW10):
8297 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8299 REAL_VALUE_TYPE dconst10;
8300 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8301 x = build_real (type, dconst10);
8303 exponent = CALL_EXPR_ARG (arg, 0);
8304 break;
8305 CASE_FLT_FN (BUILT_IN_SQRT):
8306 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8307 x = CALL_EXPR_ARG (arg, 0);
8308 exponent = build_real (type, dconsthalf);
8309 break;
8310 CASE_FLT_FN (BUILT_IN_CBRT):
8311 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8312 x = CALL_EXPR_ARG (arg, 0);
8313 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8314 dconst_third ()));
8315 break;
8316 CASE_FLT_FN (BUILT_IN_POW):
8317 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8318 x = CALL_EXPR_ARG (arg, 0);
8319 exponent = CALL_EXPR_ARG (arg, 1);
8320 break;
8321 default:
8322 break;
8325 /* Now perform the optimization. */
8326 if (x && exponent)
8328 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8329 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8334 return NULL_TREE;
8337 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8338 NULL_TREE if no simplification can be made. */
8340 static tree
8341 fold_builtin_hypot (location_t loc, tree fndecl,
8342 tree arg0, tree arg1, tree type)
8344 tree res, narg0, narg1;
8346 if (!validate_arg (arg0, REAL_TYPE)
8347 || !validate_arg (arg1, REAL_TYPE))
8348 return NULL_TREE;
8350 /* Calculate the result when the argument is a constant. */
8351 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8352 return res;
8354 /* If either argument to hypot has a negate or abs, strip that off.
8355 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8356 narg0 = fold_strip_sign_ops (arg0);
8357 narg1 = fold_strip_sign_ops (arg1);
8358 if (narg0 || narg1)
8360 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8361 narg1 ? narg1 : arg1);
8364 /* If either argument is zero, hypot is fabs of the other. */
8365 if (real_zerop (arg0))
8366 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8367 else if (real_zerop (arg1))
8368 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8370 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8371 if (flag_unsafe_math_optimizations
8372 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8374 const REAL_VALUE_TYPE sqrt2_trunc
8375 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8376 return fold_build2_loc (loc, MULT_EXPR, type,
8377 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8378 build_real (type, sqrt2_trunc));
8381 return NULL_TREE;
8385 /* Fold a builtin function call to pow, powf, or powl. Return
8386 NULL_TREE if no simplification can be made. */
8387 static tree
8388 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8390 tree res;
8392 if (!validate_arg (arg0, REAL_TYPE)
8393 || !validate_arg (arg1, REAL_TYPE))
8394 return NULL_TREE;
8396 /* Calculate the result when the argument is a constant. */
8397 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8398 return res;
8400 /* Optimize pow(1.0,y) = 1.0. */
8401 if (real_onep (arg0))
8402 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8404 if (TREE_CODE (arg1) == REAL_CST
8405 && !TREE_OVERFLOW (arg1))
8407 REAL_VALUE_TYPE cint;
8408 REAL_VALUE_TYPE c;
8409 HOST_WIDE_INT n;
8411 c = TREE_REAL_CST (arg1);
8413 /* Optimize pow(x,0.0) = 1.0. */
8414 if (REAL_VALUES_EQUAL (c, dconst0))
8415 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8416 arg0);
8418 /* Optimize pow(x,1.0) = x. */
8419 if (REAL_VALUES_EQUAL (c, dconst1))
8420 return arg0;
8422 /* Optimize pow(x,-1.0) = 1.0/x. */
8423 if (REAL_VALUES_EQUAL (c, dconstm1))
8424 return fold_build2_loc (loc, RDIV_EXPR, type,
8425 build_real (type, dconst1), arg0);
8427 /* Optimize pow(x,0.5) = sqrt(x). */
8428 if (flag_unsafe_math_optimizations
8429 && REAL_VALUES_EQUAL (c, dconsthalf))
8431 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8433 if (sqrtfn != NULL_TREE)
8434 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8437 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8438 if (flag_unsafe_math_optimizations)
8440 const REAL_VALUE_TYPE dconstroot
8441 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8443 if (REAL_VALUES_EQUAL (c, dconstroot))
8445 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8446 if (cbrtfn != NULL_TREE)
8447 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8451 /* Check for an integer exponent. */
8452 n = real_to_integer (&c);
8453 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8454 if (real_identical (&c, &cint))
8456 /* Attempt to evaluate pow at compile-time, unless this should
8457 raise an exception. */
8458 if (TREE_CODE (arg0) == REAL_CST
8459 && !TREE_OVERFLOW (arg0)
8460 && (n > 0
8461 || (!flag_trapping_math && !flag_errno_math)
8462 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8464 REAL_VALUE_TYPE x;
8465 bool inexact;
8467 x = TREE_REAL_CST (arg0);
8468 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8469 if (flag_unsafe_math_optimizations || !inexact)
8470 return build_real (type, x);
8473 /* Strip sign ops from even integer powers. */
8474 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8476 tree narg0 = fold_strip_sign_ops (arg0);
8477 if (narg0)
8478 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8483 if (flag_unsafe_math_optimizations)
8485 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8487 /* Optimize pow(expN(x),y) = expN(x*y). */
8488 if (BUILTIN_EXPONENT_P (fcode))
8490 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8491 tree arg = CALL_EXPR_ARG (arg0, 0);
8492 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8493 return build_call_expr_loc (loc, expfn, 1, arg);
8496 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8497 if (BUILTIN_SQRT_P (fcode))
8499 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8500 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8501 build_real (type, dconsthalf));
8502 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8505 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8506 if (BUILTIN_CBRT_P (fcode))
8508 tree arg = CALL_EXPR_ARG (arg0, 0);
8509 if (tree_expr_nonnegative_p (arg))
8511 const REAL_VALUE_TYPE dconstroot
8512 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8513 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8514 build_real (type, dconstroot));
8515 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8519 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8520 if (fcode == BUILT_IN_POW
8521 || fcode == BUILT_IN_POWF
8522 || fcode == BUILT_IN_POWL)
8524 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8525 if (tree_expr_nonnegative_p (arg00))
8527 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8528 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8529 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8534 return NULL_TREE;
8537 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8538 Return NULL_TREE if no simplification can be made. */
8539 static tree
8540 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8541 tree arg0, tree arg1, tree type)
8543 if (!validate_arg (arg0, REAL_TYPE)
8544 || !validate_arg (arg1, INTEGER_TYPE))
8545 return NULL_TREE;
8547 /* Optimize pow(1.0,y) = 1.0. */
8548 if (real_onep (arg0))
8549 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8551 if (host_integerp (arg1, 0))
8553 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8555 /* Evaluate powi at compile-time. */
8556 if (TREE_CODE (arg0) == REAL_CST
8557 && !TREE_OVERFLOW (arg0))
8559 REAL_VALUE_TYPE x;
8560 x = TREE_REAL_CST (arg0);
8561 real_powi (&x, TYPE_MODE (type), &x, c);
8562 return build_real (type, x);
8565 /* Optimize pow(x,0) = 1.0. */
8566 if (c == 0)
8567 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8568 arg0);
8570 /* Optimize pow(x,1) = x. */
8571 if (c == 1)
8572 return arg0;
8574 /* Optimize pow(x,-1) = 1.0/x. */
8575 if (c == -1)
8576 return fold_build2_loc (loc, RDIV_EXPR, type,
8577 build_real (type, dconst1), arg0);
8580 return NULL_TREE;
8583 /* A subroutine of fold_builtin to fold the various exponent
8584 functions. Return NULL_TREE if no simplification can be made.
8585 FUNC is the corresponding MPFR exponent function. */
8587 static tree
8588 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8589 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8591 if (validate_arg (arg, REAL_TYPE))
8593 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8594 tree res;
8596 /* Calculate the result when the argument is a constant. */
8597 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8598 return res;
8600 /* Optimize expN(logN(x)) = x. */
8601 if (flag_unsafe_math_optimizations)
8603 const enum built_in_function fcode = builtin_mathfn_code (arg);
8605 if ((func == mpfr_exp
8606 && (fcode == BUILT_IN_LOG
8607 || fcode == BUILT_IN_LOGF
8608 || fcode == BUILT_IN_LOGL))
8609 || (func == mpfr_exp2
8610 && (fcode == BUILT_IN_LOG2
8611 || fcode == BUILT_IN_LOG2F
8612 || fcode == BUILT_IN_LOG2L))
8613 || (func == mpfr_exp10
8614 && (fcode == BUILT_IN_LOG10
8615 || fcode == BUILT_IN_LOG10F
8616 || fcode == BUILT_IN_LOG10L)))
8617 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8621 return NULL_TREE;
8624 /* Return true if VAR is a VAR_DECL or a component thereof. */
8626 static bool
8627 var_decl_component_p (tree var)
8629 tree inner = var;
8630 while (handled_component_p (inner))
8631 inner = TREE_OPERAND (inner, 0);
8632 return SSA_VAR_P (inner);
8635 /* Fold function call to builtin memset. Return
8636 NULL_TREE if no simplification can be made. */
8638 static tree
8639 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8640 tree type, bool ignore)
8642 tree var, ret, etype;
8643 unsigned HOST_WIDE_INT length, cval;
8645 if (! validate_arg (dest, POINTER_TYPE)
8646 || ! validate_arg (c, INTEGER_TYPE)
8647 || ! validate_arg (len, INTEGER_TYPE))
8648 return NULL_TREE;
8650 if (! host_integerp (len, 1))
8651 return NULL_TREE;
8653 /* If the LEN parameter is zero, return DEST. */
8654 if (integer_zerop (len))
8655 return omit_one_operand_loc (loc, type, dest, c);
8657 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8658 return NULL_TREE;
8660 var = dest;
8661 STRIP_NOPS (var);
8662 if (TREE_CODE (var) != ADDR_EXPR)
8663 return NULL_TREE;
8665 var = TREE_OPERAND (var, 0);
8666 if (TREE_THIS_VOLATILE (var))
8667 return NULL_TREE;
8669 etype = TREE_TYPE (var);
8670 if (TREE_CODE (etype) == ARRAY_TYPE)
8671 etype = TREE_TYPE (etype);
8673 if (!INTEGRAL_TYPE_P (etype)
8674 && !POINTER_TYPE_P (etype))
8675 return NULL_TREE;
8677 if (! var_decl_component_p (var))
8678 return NULL_TREE;
8680 length = tree_low_cst (len, 1);
8681 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8682 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8683 return NULL_TREE;
8685 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8686 return NULL_TREE;
8688 if (integer_zerop (c))
8689 cval = 0;
8690 else
8692 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8693 return NULL_TREE;
8695 cval = TREE_INT_CST_LOW (c);
8696 cval &= 0xff;
8697 cval |= cval << 8;
8698 cval |= cval << 16;
8699 cval |= (cval << 31) << 1;
8702 ret = build_int_cst_type (etype, cval);
8703 var = build_fold_indirect_ref_loc (loc,
8704 fold_convert_loc (loc,
8705 build_pointer_type (etype),
8706 dest));
8707 ret = build2 (MODIFY_EXPR, etype, var, ret);
8708 if (ignore)
8709 return ret;
8711 return omit_one_operand_loc (loc, type, dest, ret);
8714 /* Fold function call to builtin memset. Return
8715 NULL_TREE if no simplification can be made. */
8717 static tree
8718 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8720 if (! validate_arg (dest, POINTER_TYPE)
8721 || ! validate_arg (size, INTEGER_TYPE))
8722 return NULL_TREE;
8724 if (!ignore)
8725 return NULL_TREE;
8727 /* New argument list transforming bzero(ptr x, int y) to
8728 memset(ptr x, int 0, size_t y). This is done this way
8729 so that if it isn't expanded inline, we fallback to
8730 calling bzero instead of memset. */
8732 return fold_builtin_memset (loc, dest, integer_zero_node,
8733 fold_convert_loc (loc, size_type_node, size),
8734 void_type_node, ignore);
8737 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8738 NULL_TREE if no simplification can be made.
8739 If ENDP is 0, return DEST (like memcpy).
8740 If ENDP is 1, return DEST+LEN (like mempcpy).
8741 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8742 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8743 (memmove). */
8745 static tree
8746 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8747 tree len, tree type, bool ignore, int endp)
8749 tree destvar, srcvar, expr;
8751 if (! validate_arg (dest, POINTER_TYPE)
8752 || ! validate_arg (src, POINTER_TYPE)
8753 || ! validate_arg (len, INTEGER_TYPE))
8754 return NULL_TREE;
8756 /* If the LEN parameter is zero, return DEST. */
8757 if (integer_zerop (len))
8758 return omit_one_operand_loc (loc, type, dest, src);
8760 /* If SRC and DEST are the same (and not volatile), return
8761 DEST{,+LEN,+LEN-1}. */
8762 if (operand_equal_p (src, dest, 0))
8763 expr = len;
8764 else
8766 tree srctype, desttype;
8767 unsigned int src_align, dest_align;
8768 tree off0;
8770 if (endp == 3)
8772 src_align = get_pointer_alignment (src);
8773 dest_align = get_pointer_alignment (dest);
8775 /* Both DEST and SRC must be pointer types.
8776 ??? This is what old code did. Is the testing for pointer types
8777 really mandatory?
8779 If either SRC is readonly or length is 1, we can use memcpy. */
8780 if (!dest_align || !src_align)
8781 return NULL_TREE;
8782 if (readonly_data_expr (src)
8783 || (host_integerp (len, 1)
8784 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8785 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8787 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8788 if (!fn)
8789 return NULL_TREE;
8790 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8793 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8794 if (TREE_CODE (src) == ADDR_EXPR
8795 && TREE_CODE (dest) == ADDR_EXPR)
8797 tree src_base, dest_base, fn;
8798 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8799 HOST_WIDE_INT size = -1;
8800 HOST_WIDE_INT maxsize = -1;
8802 srcvar = TREE_OPERAND (src, 0);
8803 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8804 &size, &maxsize);
8805 destvar = TREE_OPERAND (dest, 0);
8806 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8807 &size, &maxsize);
8808 if (host_integerp (len, 1))
8809 maxsize = tree_low_cst (len, 1);
8810 else
8811 maxsize = -1;
8812 src_offset /= BITS_PER_UNIT;
8813 dest_offset /= BITS_PER_UNIT;
8814 if (SSA_VAR_P (src_base)
8815 && SSA_VAR_P (dest_base))
8817 if (operand_equal_p (src_base, dest_base, 0)
8818 && ranges_overlap_p (src_offset, maxsize,
8819 dest_offset, maxsize))
8820 return NULL_TREE;
8822 else if (TREE_CODE (src_base) == MEM_REF
8823 && TREE_CODE (dest_base) == MEM_REF)
8825 double_int off;
8826 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8827 TREE_OPERAND (dest_base, 0), 0))
8828 return NULL_TREE;
8829 off = mem_ref_offset (src_base) +
8830 double_int::from_shwi (src_offset);
8831 if (!off.fits_shwi ())
8832 return NULL_TREE;
8833 src_offset = off.low;
8834 off = mem_ref_offset (dest_base) +
8835 double_int::from_shwi (dest_offset);
8836 if (!off.fits_shwi ())
8837 return NULL_TREE;
8838 dest_offset = off.low;
8839 if (ranges_overlap_p (src_offset, maxsize,
8840 dest_offset, maxsize))
8841 return NULL_TREE;
8843 else
8844 return NULL_TREE;
8846 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8847 if (!fn)
8848 return NULL_TREE;
8849 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8852 /* If the destination and source do not alias optimize into
8853 memcpy as well. */
8854 if ((is_gimple_min_invariant (dest)
8855 || TREE_CODE (dest) == SSA_NAME)
8856 && (is_gimple_min_invariant (src)
8857 || TREE_CODE (src) == SSA_NAME))
8859 ao_ref destr, srcr;
8860 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8861 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8862 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8864 tree fn;
8865 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8866 if (!fn)
8867 return NULL_TREE;
8868 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8872 return NULL_TREE;
8875 if (!host_integerp (len, 0))
8876 return NULL_TREE;
8877 /* FIXME:
8878 This logic lose for arguments like (type *)malloc (sizeof (type)),
8879 since we strip the casts of up to VOID return value from malloc.
8880 Perhaps we ought to inherit type from non-VOID argument here? */
8881 STRIP_NOPS (src);
8882 STRIP_NOPS (dest);
8883 if (!POINTER_TYPE_P (TREE_TYPE (src))
8884 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8885 return NULL_TREE;
8886 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8887 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8889 tree tem = TREE_OPERAND (src, 0);
8890 STRIP_NOPS (tem);
8891 if (tem != TREE_OPERAND (src, 0))
8892 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8894 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8896 tree tem = TREE_OPERAND (dest, 0);
8897 STRIP_NOPS (tem);
8898 if (tem != TREE_OPERAND (dest, 0))
8899 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8901 srctype = TREE_TYPE (TREE_TYPE (src));
8902 if (TREE_CODE (srctype) == ARRAY_TYPE
8903 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8905 srctype = TREE_TYPE (srctype);
8906 STRIP_NOPS (src);
8907 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8909 desttype = TREE_TYPE (TREE_TYPE (dest));
8910 if (TREE_CODE (desttype) == ARRAY_TYPE
8911 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8913 desttype = TREE_TYPE (desttype);
8914 STRIP_NOPS (dest);
8915 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8917 if (TREE_ADDRESSABLE (srctype)
8918 || TREE_ADDRESSABLE (desttype))
8919 return NULL_TREE;
8921 src_align = get_pointer_alignment (src);
8922 dest_align = get_pointer_alignment (dest);
8923 if (dest_align < TYPE_ALIGN (desttype)
8924 || src_align < TYPE_ALIGN (srctype))
8925 return NULL_TREE;
8927 if (!ignore)
8928 dest = builtin_save_expr (dest);
8930 /* Build accesses at offset zero with a ref-all character type. */
8931 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8932 ptr_mode, true), 0);
8934 destvar = dest;
8935 STRIP_NOPS (destvar);
8936 if (TREE_CODE (destvar) == ADDR_EXPR
8937 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8938 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8939 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8940 else
8941 destvar = NULL_TREE;
8943 srcvar = src;
8944 STRIP_NOPS (srcvar);
8945 if (TREE_CODE (srcvar) == ADDR_EXPR
8946 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8947 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8949 if (!destvar
8950 || src_align >= TYPE_ALIGN (desttype))
8951 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8952 srcvar, off0);
8953 else if (!STRICT_ALIGNMENT)
8955 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8956 src_align);
8957 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8959 else
8960 srcvar = NULL_TREE;
8962 else
8963 srcvar = NULL_TREE;
8965 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8966 return NULL_TREE;
8968 if (srcvar == NULL_TREE)
8970 STRIP_NOPS (src);
8971 if (src_align >= TYPE_ALIGN (desttype))
8972 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8973 else
8975 if (STRICT_ALIGNMENT)
8976 return NULL_TREE;
8977 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8978 src_align);
8979 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8982 else if (destvar == NULL_TREE)
8984 STRIP_NOPS (dest);
8985 if (dest_align >= TYPE_ALIGN (srctype))
8986 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8987 else
8989 if (STRICT_ALIGNMENT)
8990 return NULL_TREE;
8991 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8992 dest_align);
8993 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8997 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9000 if (ignore)
9001 return expr;
9003 if (endp == 0 || endp == 3)
9004 return omit_one_operand_loc (loc, type, dest, expr);
9006 if (expr == len)
9007 expr = NULL_TREE;
9009 if (endp == 2)
9010 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9011 ssize_int (1));
9013 dest = fold_build_pointer_plus_loc (loc, dest, len);
9014 dest = fold_convert_loc (loc, type, dest);
9015 if (expr)
9016 dest = omit_one_operand_loc (loc, type, dest, expr);
9017 return dest;
9020 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9021 If LEN is not NULL, it represents the length of the string to be
9022 copied. Return NULL_TREE if no simplification can be made. */
9024 tree
9025 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9027 tree fn;
9029 if (!validate_arg (dest, POINTER_TYPE)
9030 || !validate_arg (src, POINTER_TYPE))
9031 return NULL_TREE;
9033 /* If SRC and DEST are the same (and not volatile), return DEST. */
9034 if (operand_equal_p (src, dest, 0))
9035 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9037 if (optimize_function_for_size_p (cfun))
9038 return NULL_TREE;
9040 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9041 if (!fn)
9042 return NULL_TREE;
9044 if (!len)
9046 len = c_strlen (src, 1);
9047 if (! len || TREE_SIDE_EFFECTS (len))
9048 return NULL_TREE;
9051 len = fold_convert_loc (loc, size_type_node, len);
9052 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9053 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9054 build_call_expr_loc (loc, fn, 3, dest, src, len));
9057 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9058 Return NULL_TREE if no simplification can be made. */
9060 static tree
9061 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9063 tree fn, len, lenp1, call, type;
9065 if (!validate_arg (dest, POINTER_TYPE)
9066 || !validate_arg (src, POINTER_TYPE))
9067 return NULL_TREE;
9069 len = c_strlen (src, 1);
9070 if (!len
9071 || TREE_CODE (len) != INTEGER_CST)
9072 return NULL_TREE;
9074 if (optimize_function_for_size_p (cfun)
9075 /* If length is zero it's small enough. */
9076 && !integer_zerop (len))
9077 return NULL_TREE;
9079 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9080 if (!fn)
9081 return NULL_TREE;
9083 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9084 fold_convert_loc (loc, size_type_node, len),
9085 build_int_cst (size_type_node, 1));
9086 /* We use dest twice in building our expression. Save it from
9087 multiple expansions. */
9088 dest = builtin_save_expr (dest);
9089 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9091 type = TREE_TYPE (TREE_TYPE (fndecl));
9092 dest = fold_build_pointer_plus_loc (loc, dest, len);
9093 dest = fold_convert_loc (loc, type, dest);
9094 dest = omit_one_operand_loc (loc, type, dest, call);
9095 return dest;
9098 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9099 If SLEN is not NULL, it represents the length of the source string.
9100 Return NULL_TREE if no simplification can be made. */
9102 tree
9103 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9104 tree src, tree len, tree slen)
9106 tree fn;
9108 if (!validate_arg (dest, POINTER_TYPE)
9109 || !validate_arg (src, POINTER_TYPE)
9110 || !validate_arg (len, INTEGER_TYPE))
9111 return NULL_TREE;
9113 /* If the LEN parameter is zero, return DEST. */
9114 if (integer_zerop (len))
9115 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9117 /* We can't compare slen with len as constants below if len is not a
9118 constant. */
9119 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9120 return NULL_TREE;
9122 if (!slen)
9123 slen = c_strlen (src, 1);
9125 /* Now, we must be passed a constant src ptr parameter. */
9126 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9127 return NULL_TREE;
9129 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9131 /* We do not support simplification of this case, though we do
9132 support it when expanding trees into RTL. */
9133 /* FIXME: generate a call to __builtin_memset. */
9134 if (tree_int_cst_lt (slen, len))
9135 return NULL_TREE;
9137 /* OK transform into builtin memcpy. */
9138 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9139 if (!fn)
9140 return NULL_TREE;
9142 len = fold_convert_loc (loc, size_type_node, len);
9143 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9144 build_call_expr_loc (loc, fn, 3, dest, src, len));
9147 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9148 arguments to the call, and TYPE is its return type.
9149 Return NULL_TREE if no simplification can be made. */
9151 static tree
9152 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9154 if (!validate_arg (arg1, POINTER_TYPE)
9155 || !validate_arg (arg2, INTEGER_TYPE)
9156 || !validate_arg (len, INTEGER_TYPE))
9157 return NULL_TREE;
9158 else
9160 const char *p1;
9162 if (TREE_CODE (arg2) != INTEGER_CST
9163 || !host_integerp (len, 1))
9164 return NULL_TREE;
9166 p1 = c_getstr (arg1);
9167 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9169 char c;
9170 const char *r;
9171 tree tem;
9173 if (target_char_cast (arg2, &c))
9174 return NULL_TREE;
9176 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9178 if (r == NULL)
9179 return build_int_cst (TREE_TYPE (arg1), 0);
9181 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9182 return fold_convert_loc (loc, type, tem);
9184 return NULL_TREE;
9188 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9189 Return NULL_TREE if no simplification can be made. */
9191 static tree
9192 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9194 const char *p1, *p2;
9196 if (!validate_arg (arg1, POINTER_TYPE)
9197 || !validate_arg (arg2, POINTER_TYPE)
9198 || !validate_arg (len, INTEGER_TYPE))
9199 return NULL_TREE;
9201 /* If the LEN parameter is zero, return zero. */
9202 if (integer_zerop (len))
9203 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9204 arg1, arg2);
9206 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9207 if (operand_equal_p (arg1, arg2, 0))
9208 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9210 p1 = c_getstr (arg1);
9211 p2 = c_getstr (arg2);
9213 /* If all arguments are constant, and the value of len is not greater
9214 than the lengths of arg1 and arg2, evaluate at compile-time. */
9215 if (host_integerp (len, 1) && p1 && p2
9216 && compare_tree_int (len, strlen (p1) + 1) <= 0
9217 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9219 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9221 if (r > 0)
9222 return integer_one_node;
9223 else if (r < 0)
9224 return integer_minus_one_node;
9225 else
9226 return integer_zero_node;
9229 /* If len parameter is one, return an expression corresponding to
9230 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9231 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9233 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9234 tree cst_uchar_ptr_node
9235 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9237 tree ind1
9238 = fold_convert_loc (loc, integer_type_node,
9239 build1 (INDIRECT_REF, cst_uchar_node,
9240 fold_convert_loc (loc,
9241 cst_uchar_ptr_node,
9242 arg1)));
9243 tree ind2
9244 = fold_convert_loc (loc, integer_type_node,
9245 build1 (INDIRECT_REF, cst_uchar_node,
9246 fold_convert_loc (loc,
9247 cst_uchar_ptr_node,
9248 arg2)));
9249 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9252 return NULL_TREE;
9255 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9256 Return NULL_TREE if no simplification can be made. */
9258 static tree
9259 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9261 const char *p1, *p2;
9263 if (!validate_arg (arg1, POINTER_TYPE)
9264 || !validate_arg (arg2, POINTER_TYPE))
9265 return NULL_TREE;
9267 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9268 if (operand_equal_p (arg1, arg2, 0))
9269 return integer_zero_node;
9271 p1 = c_getstr (arg1);
9272 p2 = c_getstr (arg2);
9274 if (p1 && p2)
9276 const int i = strcmp (p1, p2);
9277 if (i < 0)
9278 return integer_minus_one_node;
9279 else if (i > 0)
9280 return integer_one_node;
9281 else
9282 return integer_zero_node;
9285 /* If the second arg is "", return *(const unsigned char*)arg1. */
9286 if (p2 && *p2 == '\0')
9288 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9289 tree cst_uchar_ptr_node
9290 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9292 return fold_convert_loc (loc, integer_type_node,
9293 build1 (INDIRECT_REF, cst_uchar_node,
9294 fold_convert_loc (loc,
9295 cst_uchar_ptr_node,
9296 arg1)));
9299 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9300 if (p1 && *p1 == '\0')
9302 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9303 tree cst_uchar_ptr_node
9304 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9306 tree temp
9307 = fold_convert_loc (loc, integer_type_node,
9308 build1 (INDIRECT_REF, cst_uchar_node,
9309 fold_convert_loc (loc,
9310 cst_uchar_ptr_node,
9311 arg2)));
9312 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9315 return NULL_TREE;
9318 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9319 Return NULL_TREE if no simplification can be made. */
9321 static tree
9322 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9324 const char *p1, *p2;
9326 if (!validate_arg (arg1, POINTER_TYPE)
9327 || !validate_arg (arg2, POINTER_TYPE)
9328 || !validate_arg (len, INTEGER_TYPE))
9329 return NULL_TREE;
9331 /* If the LEN parameter is zero, return zero. */
9332 if (integer_zerop (len))
9333 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9334 arg1, arg2);
9336 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9337 if (operand_equal_p (arg1, arg2, 0))
9338 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9340 p1 = c_getstr (arg1);
9341 p2 = c_getstr (arg2);
9343 if (host_integerp (len, 1) && p1 && p2)
9345 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9346 if (i > 0)
9347 return integer_one_node;
9348 else if (i < 0)
9349 return integer_minus_one_node;
9350 else
9351 return integer_zero_node;
9354 /* If the second arg is "", and the length is greater than zero,
9355 return *(const unsigned char*)arg1. */
9356 if (p2 && *p2 == '\0'
9357 && TREE_CODE (len) == INTEGER_CST
9358 && tree_int_cst_sgn (len) == 1)
9360 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9361 tree cst_uchar_ptr_node
9362 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9364 return fold_convert_loc (loc, integer_type_node,
9365 build1 (INDIRECT_REF, cst_uchar_node,
9366 fold_convert_loc (loc,
9367 cst_uchar_ptr_node,
9368 arg1)));
9371 /* If the first arg is "", and the length is greater than zero,
9372 return -*(const unsigned char*)arg2. */
9373 if (p1 && *p1 == '\0'
9374 && TREE_CODE (len) == INTEGER_CST
9375 && tree_int_cst_sgn (len) == 1)
9377 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9378 tree cst_uchar_ptr_node
9379 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9381 tree temp = fold_convert_loc (loc, integer_type_node,
9382 build1 (INDIRECT_REF, cst_uchar_node,
9383 fold_convert_loc (loc,
9384 cst_uchar_ptr_node,
9385 arg2)));
9386 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9389 /* If len parameter is one, return an expression corresponding to
9390 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9391 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9393 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9394 tree cst_uchar_ptr_node
9395 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9397 tree ind1 = fold_convert_loc (loc, integer_type_node,
9398 build1 (INDIRECT_REF, cst_uchar_node,
9399 fold_convert_loc (loc,
9400 cst_uchar_ptr_node,
9401 arg1)));
9402 tree ind2 = fold_convert_loc (loc, integer_type_node,
9403 build1 (INDIRECT_REF, cst_uchar_node,
9404 fold_convert_loc (loc,
9405 cst_uchar_ptr_node,
9406 arg2)));
9407 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9410 return NULL_TREE;
9413 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9414 ARG. Return NULL_TREE if no simplification can be made. */
9416 static tree
9417 fold_builtin_signbit (location_t loc, tree arg, tree type)
9419 if (!validate_arg (arg, REAL_TYPE))
9420 return NULL_TREE;
9422 /* If ARG is a compile-time constant, determine the result. */
9423 if (TREE_CODE (arg) == REAL_CST
9424 && !TREE_OVERFLOW (arg))
9426 REAL_VALUE_TYPE c;
9428 c = TREE_REAL_CST (arg);
9429 return (REAL_VALUE_NEGATIVE (c)
9430 ? build_one_cst (type)
9431 : build_zero_cst (type));
9434 /* If ARG is non-negative, the result is always zero. */
9435 if (tree_expr_nonnegative_p (arg))
9436 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9438 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9439 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9440 return fold_convert (type,
9441 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9442 build_real (TREE_TYPE (arg), dconst0)));
9444 return NULL_TREE;
9447 /* Fold function call to builtin copysign, copysignf or copysignl with
9448 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9449 be made. */
9451 static tree
9452 fold_builtin_copysign (location_t loc, tree fndecl,
9453 tree arg1, tree arg2, tree type)
9455 tree tem;
9457 if (!validate_arg (arg1, REAL_TYPE)
9458 || !validate_arg (arg2, REAL_TYPE))
9459 return NULL_TREE;
9461 /* copysign(X,X) is X. */
9462 if (operand_equal_p (arg1, arg2, 0))
9463 return fold_convert_loc (loc, type, arg1);
9465 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9466 if (TREE_CODE (arg1) == REAL_CST
9467 && TREE_CODE (arg2) == REAL_CST
9468 && !TREE_OVERFLOW (arg1)
9469 && !TREE_OVERFLOW (arg2))
9471 REAL_VALUE_TYPE c1, c2;
9473 c1 = TREE_REAL_CST (arg1);
9474 c2 = TREE_REAL_CST (arg2);
9475 /* c1.sign := c2.sign. */
9476 real_copysign (&c1, &c2);
9477 return build_real (type, c1);
9480 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9481 Remember to evaluate Y for side-effects. */
9482 if (tree_expr_nonnegative_p (arg2))
9483 return omit_one_operand_loc (loc, type,
9484 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9485 arg2);
9487 /* Strip sign changing operations for the first argument. */
9488 tem = fold_strip_sign_ops (arg1);
9489 if (tem)
9490 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9492 return NULL_TREE;
9495 /* Fold a call to builtin isascii with argument ARG. */
9497 static tree
9498 fold_builtin_isascii (location_t loc, tree arg)
9500 if (!validate_arg (arg, INTEGER_TYPE))
9501 return NULL_TREE;
9502 else
9504 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9505 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9506 build_int_cst (integer_type_node,
9507 ~ (unsigned HOST_WIDE_INT) 0x7f));
9508 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9509 arg, integer_zero_node);
9513 /* Fold a call to builtin toascii with argument ARG. */
9515 static tree
9516 fold_builtin_toascii (location_t loc, tree arg)
9518 if (!validate_arg (arg, INTEGER_TYPE))
9519 return NULL_TREE;
9521 /* Transform toascii(c) -> (c & 0x7f). */
9522 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9523 build_int_cst (integer_type_node, 0x7f));
9526 /* Fold a call to builtin isdigit with argument ARG. */
9528 static tree
9529 fold_builtin_isdigit (location_t loc, tree arg)
9531 if (!validate_arg (arg, INTEGER_TYPE))
9532 return NULL_TREE;
9533 else
9535 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9536 /* According to the C standard, isdigit is unaffected by locale.
9537 However, it definitely is affected by the target character set. */
9538 unsigned HOST_WIDE_INT target_digit0
9539 = lang_hooks.to_target_charset ('0');
9541 if (target_digit0 == 0)
9542 return NULL_TREE;
9544 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9545 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9546 build_int_cst (unsigned_type_node, target_digit0));
9547 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9548 build_int_cst (unsigned_type_node, 9));
9552 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9554 static tree
9555 fold_builtin_fabs (location_t loc, tree arg, tree type)
9557 if (!validate_arg (arg, REAL_TYPE))
9558 return NULL_TREE;
9560 arg = fold_convert_loc (loc, type, arg);
9561 if (TREE_CODE (arg) == REAL_CST)
9562 return fold_abs_const (arg, type);
9563 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9566 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9568 static tree
9569 fold_builtin_abs (location_t loc, tree arg, tree type)
9571 if (!validate_arg (arg, INTEGER_TYPE))
9572 return NULL_TREE;
9574 arg = fold_convert_loc (loc, type, arg);
9575 if (TREE_CODE (arg) == INTEGER_CST)
9576 return fold_abs_const (arg, type);
9577 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9580 /* Fold a fma operation with arguments ARG[012]. */
9582 tree
9583 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9584 tree type, tree arg0, tree arg1, tree arg2)
9586 if (TREE_CODE (arg0) == REAL_CST
9587 && TREE_CODE (arg1) == REAL_CST
9588 && TREE_CODE (arg2) == REAL_CST)
9589 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9591 return NULL_TREE;
9594 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9596 static tree
9597 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9599 if (validate_arg (arg0, REAL_TYPE)
9600 && validate_arg(arg1, REAL_TYPE)
9601 && validate_arg(arg2, REAL_TYPE))
9603 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9604 if (tem)
9605 return tem;
9607 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9608 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9609 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9611 return NULL_TREE;
9614 /* Fold a call to builtin fmin or fmax. */
9616 static tree
9617 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9618 tree type, bool max)
9620 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9622 /* Calculate the result when the argument is a constant. */
9623 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9625 if (res)
9626 return res;
9628 /* If either argument is NaN, return the other one. Avoid the
9629 transformation if we get (and honor) a signalling NaN. Using
9630 omit_one_operand() ensures we create a non-lvalue. */
9631 if (TREE_CODE (arg0) == REAL_CST
9632 && real_isnan (&TREE_REAL_CST (arg0))
9633 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9634 || ! TREE_REAL_CST (arg0).signalling))
9635 return omit_one_operand_loc (loc, type, arg1, arg0);
9636 if (TREE_CODE (arg1) == REAL_CST
9637 && real_isnan (&TREE_REAL_CST (arg1))
9638 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9639 || ! TREE_REAL_CST (arg1).signalling))
9640 return omit_one_operand_loc (loc, type, arg0, arg1);
9642 /* Transform fmin/fmax(x,x) -> x. */
9643 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9644 return omit_one_operand_loc (loc, type, arg0, arg1);
9646 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9647 functions to return the numeric arg if the other one is NaN.
9648 These tree codes don't honor that, so only transform if
9649 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9650 handled, so we don't have to worry about it either. */
9651 if (flag_finite_math_only)
9652 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9653 fold_convert_loc (loc, type, arg0),
9654 fold_convert_loc (loc, type, arg1));
9656 return NULL_TREE;
9659 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9661 static tree
9662 fold_builtin_carg (location_t loc, tree arg, tree type)
9664 if (validate_arg (arg, COMPLEX_TYPE)
9665 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9667 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9669 if (atan2_fn)
9671 tree new_arg = builtin_save_expr (arg);
9672 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9673 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9674 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9678 return NULL_TREE;
9681 /* Fold a call to builtin logb/ilogb. */
9683 static tree
9684 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9686 if (! validate_arg (arg, REAL_TYPE))
9687 return NULL_TREE;
9689 STRIP_NOPS (arg);
9691 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9693 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9695 switch (value->cl)
9697 case rvc_nan:
9698 case rvc_inf:
9699 /* If arg is Inf or NaN and we're logb, return it. */
9700 if (TREE_CODE (rettype) == REAL_TYPE)
9702 /* For logb(-Inf) we have to return +Inf. */
9703 if (real_isinf (value) && real_isneg (value))
9705 REAL_VALUE_TYPE tem;
9706 real_inf (&tem);
9707 return build_real (rettype, tem);
9709 return fold_convert_loc (loc, rettype, arg);
9711 /* Fall through... */
9712 case rvc_zero:
9713 /* Zero may set errno and/or raise an exception for logb, also
9714 for ilogb we don't know FP_ILOGB0. */
9715 return NULL_TREE;
9716 case rvc_normal:
9717 /* For normal numbers, proceed iff radix == 2. In GCC,
9718 normalized significands are in the range [0.5, 1.0). We
9719 want the exponent as if they were [1.0, 2.0) so get the
9720 exponent and subtract 1. */
9721 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9722 return fold_convert_loc (loc, rettype,
9723 build_int_cst (integer_type_node,
9724 REAL_EXP (value)-1));
9725 break;
9729 return NULL_TREE;
9732 /* Fold a call to builtin significand, if radix == 2. */
9734 static tree
9735 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9737 if (! validate_arg (arg, REAL_TYPE))
9738 return NULL_TREE;
9740 STRIP_NOPS (arg);
9742 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9744 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9746 switch (value->cl)
9748 case rvc_zero:
9749 case rvc_nan:
9750 case rvc_inf:
9751 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9752 return fold_convert_loc (loc, rettype, arg);
9753 case rvc_normal:
9754 /* For normal numbers, proceed iff radix == 2. */
9755 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9757 REAL_VALUE_TYPE result = *value;
9758 /* In GCC, normalized significands are in the range [0.5,
9759 1.0). We want them to be [1.0, 2.0) so set the
9760 exponent to 1. */
9761 SET_REAL_EXP (&result, 1);
9762 return build_real (rettype, result);
9764 break;
9768 return NULL_TREE;
9771 /* Fold a call to builtin frexp, we can assume the base is 2. */
9773 static tree
9774 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9776 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9777 return NULL_TREE;
9779 STRIP_NOPS (arg0);
9781 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9782 return NULL_TREE;
9784 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9786 /* Proceed if a valid pointer type was passed in. */
9787 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9789 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9790 tree frac, exp;
9792 switch (value->cl)
9794 case rvc_zero:
9795 /* For +-0, return (*exp = 0, +-0). */
9796 exp = integer_zero_node;
9797 frac = arg0;
9798 break;
9799 case rvc_nan:
9800 case rvc_inf:
9801 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9802 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9803 case rvc_normal:
9805 /* Since the frexp function always expects base 2, and in
9806 GCC normalized significands are already in the range
9807 [0.5, 1.0), we have exactly what frexp wants. */
9808 REAL_VALUE_TYPE frac_rvt = *value;
9809 SET_REAL_EXP (&frac_rvt, 0);
9810 frac = build_real (rettype, frac_rvt);
9811 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9813 break;
9814 default:
9815 gcc_unreachable ();
9818 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9819 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9820 TREE_SIDE_EFFECTS (arg1) = 1;
9821 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9824 return NULL_TREE;
9827 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9828 then we can assume the base is two. If it's false, then we have to
9829 check the mode of the TYPE parameter in certain cases. */
9831 static tree
9832 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9833 tree type, bool ldexp)
9835 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9837 STRIP_NOPS (arg0);
9838 STRIP_NOPS (arg1);
9840 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9841 if (real_zerop (arg0) || integer_zerop (arg1)
9842 || (TREE_CODE (arg0) == REAL_CST
9843 && !real_isfinite (&TREE_REAL_CST (arg0))))
9844 return omit_one_operand_loc (loc, type, arg0, arg1);
9846 /* If both arguments are constant, then try to evaluate it. */
9847 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9848 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9849 && host_integerp (arg1, 0))
9851 /* Bound the maximum adjustment to twice the range of the
9852 mode's valid exponents. Use abs to ensure the range is
9853 positive as a sanity check. */
9854 const long max_exp_adj = 2 *
9855 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9856 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9858 /* Get the user-requested adjustment. */
9859 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9861 /* The requested adjustment must be inside this range. This
9862 is a preliminary cap to avoid things like overflow, we
9863 may still fail to compute the result for other reasons. */
9864 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9866 REAL_VALUE_TYPE initial_result;
9868 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9870 /* Ensure we didn't overflow. */
9871 if (! real_isinf (&initial_result))
9873 const REAL_VALUE_TYPE trunc_result
9874 = real_value_truncate (TYPE_MODE (type), initial_result);
9876 /* Only proceed if the target mode can hold the
9877 resulting value. */
9878 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9879 return build_real (type, trunc_result);
9885 return NULL_TREE;
9888 /* Fold a call to builtin modf. */
9890 static tree
9891 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9893 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9894 return NULL_TREE;
9896 STRIP_NOPS (arg0);
9898 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9899 return NULL_TREE;
9901 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9903 /* Proceed if a valid pointer type was passed in. */
9904 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9906 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9907 REAL_VALUE_TYPE trunc, frac;
9909 switch (value->cl)
9911 case rvc_nan:
9912 case rvc_zero:
9913 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9914 trunc = frac = *value;
9915 break;
9916 case rvc_inf:
9917 /* For +-Inf, return (*arg1 = arg0, +-0). */
9918 frac = dconst0;
9919 frac.sign = value->sign;
9920 trunc = *value;
9921 break;
9922 case rvc_normal:
9923 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9924 real_trunc (&trunc, VOIDmode, value);
9925 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9926 /* If the original number was negative and already
9927 integral, then the fractional part is -0.0. */
9928 if (value->sign && frac.cl == rvc_zero)
9929 frac.sign = value->sign;
9930 break;
9933 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9934 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9935 build_real (rettype, trunc));
9936 TREE_SIDE_EFFECTS (arg1) = 1;
9937 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9938 build_real (rettype, frac));
9941 return NULL_TREE;
9944 /* Given a location LOC, an interclass builtin function decl FNDECL
9945 and its single argument ARG, return an folded expression computing
9946 the same, or NULL_TREE if we either couldn't or didn't want to fold
9947 (the latter happen if there's an RTL instruction available). */
9949 static tree
9950 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9952 enum machine_mode mode;
9954 if (!validate_arg (arg, REAL_TYPE))
9955 return NULL_TREE;
9957 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9958 return NULL_TREE;
9960 mode = TYPE_MODE (TREE_TYPE (arg));
9962 /* If there is no optab, try generic code. */
9963 switch (DECL_FUNCTION_CODE (fndecl))
9965 tree result;
9967 CASE_FLT_FN (BUILT_IN_ISINF):
9969 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9970 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9971 tree const type = TREE_TYPE (arg);
9972 REAL_VALUE_TYPE r;
9973 char buf[128];
9975 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9976 real_from_string (&r, buf);
9977 result = build_call_expr (isgr_fn, 2,
9978 fold_build1_loc (loc, ABS_EXPR, type, arg),
9979 build_real (type, r));
9980 return result;
9982 CASE_FLT_FN (BUILT_IN_FINITE):
9983 case BUILT_IN_ISFINITE:
9985 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9986 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9987 tree const type = TREE_TYPE (arg);
9988 REAL_VALUE_TYPE r;
9989 char buf[128];
9991 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9992 real_from_string (&r, buf);
9993 result = build_call_expr (isle_fn, 2,
9994 fold_build1_loc (loc, ABS_EXPR, type, arg),
9995 build_real (type, r));
9996 /*result = fold_build2_loc (loc, UNGT_EXPR,
9997 TREE_TYPE (TREE_TYPE (fndecl)),
9998 fold_build1_loc (loc, ABS_EXPR, type, arg),
9999 build_real (type, r));
10000 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10001 TREE_TYPE (TREE_TYPE (fndecl)),
10002 result);*/
10003 return result;
10005 case BUILT_IN_ISNORMAL:
10007 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10008 islessequal(fabs(x),DBL_MAX). */
10009 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10010 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10011 tree const type = TREE_TYPE (arg);
10012 REAL_VALUE_TYPE rmax, rmin;
10013 char buf[128];
10015 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10016 real_from_string (&rmax, buf);
10017 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10018 real_from_string (&rmin, buf);
10019 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10020 result = build_call_expr (isle_fn, 2, arg,
10021 build_real (type, rmax));
10022 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10023 build_call_expr (isge_fn, 2, arg,
10024 build_real (type, rmin)));
10025 return result;
10027 default:
10028 break;
10031 return NULL_TREE;
10034 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10035 ARG is the argument for the call. */
10037 static tree
10038 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10040 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10041 REAL_VALUE_TYPE r;
10043 if (!validate_arg (arg, REAL_TYPE))
10044 return NULL_TREE;
10046 switch (builtin_index)
10048 case BUILT_IN_ISINF:
10049 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10050 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10052 if (TREE_CODE (arg) == REAL_CST)
10054 r = TREE_REAL_CST (arg);
10055 if (real_isinf (&r))
10056 return real_compare (GT_EXPR, &r, &dconst0)
10057 ? integer_one_node : integer_minus_one_node;
10058 else
10059 return integer_zero_node;
10062 return NULL_TREE;
10064 case BUILT_IN_ISINF_SIGN:
10066 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10067 /* In a boolean context, GCC will fold the inner COND_EXPR to
10068 1. So e.g. "if (isinf_sign(x))" would be folded to just
10069 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10070 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10071 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10072 tree tmp = NULL_TREE;
10074 arg = builtin_save_expr (arg);
10076 if (signbit_fn && isinf_fn)
10078 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10079 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10081 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10082 signbit_call, integer_zero_node);
10083 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10084 isinf_call, integer_zero_node);
10086 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10087 integer_minus_one_node, integer_one_node);
10088 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10089 isinf_call, tmp,
10090 integer_zero_node);
10093 return tmp;
10096 case BUILT_IN_ISFINITE:
10097 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10098 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10099 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10101 if (TREE_CODE (arg) == REAL_CST)
10103 r = TREE_REAL_CST (arg);
10104 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10107 return NULL_TREE;
10109 case BUILT_IN_ISNAN:
10110 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10111 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10113 if (TREE_CODE (arg) == REAL_CST)
10115 r = TREE_REAL_CST (arg);
10116 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10119 arg = builtin_save_expr (arg);
10120 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10122 default:
10123 gcc_unreachable ();
10127 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10128 This builtin will generate code to return the appropriate floating
10129 point classification depending on the value of the floating point
10130 number passed in. The possible return values must be supplied as
10131 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10132 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10133 one floating point argument which is "type generic". */
10135 static tree
10136 fold_builtin_fpclassify (location_t loc, tree exp)
10138 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10139 arg, type, res, tmp;
10140 enum machine_mode mode;
10141 REAL_VALUE_TYPE r;
10142 char buf[128];
10144 /* Verify the required arguments in the original call. */
10145 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10146 INTEGER_TYPE, INTEGER_TYPE,
10147 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10148 return NULL_TREE;
10150 fp_nan = CALL_EXPR_ARG (exp, 0);
10151 fp_infinite = CALL_EXPR_ARG (exp, 1);
10152 fp_normal = CALL_EXPR_ARG (exp, 2);
10153 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10154 fp_zero = CALL_EXPR_ARG (exp, 4);
10155 arg = CALL_EXPR_ARG (exp, 5);
10156 type = TREE_TYPE (arg);
10157 mode = TYPE_MODE (type);
10158 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10160 /* fpclassify(x) ->
10161 isnan(x) ? FP_NAN :
10162 (fabs(x) == Inf ? FP_INFINITE :
10163 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10164 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10166 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10167 build_real (type, dconst0));
10168 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10169 tmp, fp_zero, fp_subnormal);
10171 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10172 real_from_string (&r, buf);
10173 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10174 arg, build_real (type, r));
10175 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10177 if (HONOR_INFINITIES (mode))
10179 real_inf (&r);
10180 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10181 build_real (type, r));
10182 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10183 fp_infinite, res);
10186 if (HONOR_NANS (mode))
10188 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10189 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10192 return res;
10195 /* Fold a call to an unordered comparison function such as
10196 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10197 being called and ARG0 and ARG1 are the arguments for the call.
10198 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10199 the opposite of the desired result. UNORDERED_CODE is used
10200 for modes that can hold NaNs and ORDERED_CODE is used for
10201 the rest. */
10203 static tree
10204 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10205 enum tree_code unordered_code,
10206 enum tree_code ordered_code)
10208 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10209 enum tree_code code;
10210 tree type0, type1;
10211 enum tree_code code0, code1;
10212 tree cmp_type = NULL_TREE;
10214 type0 = TREE_TYPE (arg0);
10215 type1 = TREE_TYPE (arg1);
10217 code0 = TREE_CODE (type0);
10218 code1 = TREE_CODE (type1);
10220 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10221 /* Choose the wider of two real types. */
10222 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10223 ? type0 : type1;
10224 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10225 cmp_type = type0;
10226 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10227 cmp_type = type1;
10229 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10230 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10232 if (unordered_code == UNORDERED_EXPR)
10234 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10235 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10236 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10239 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10240 : ordered_code;
10241 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10242 fold_build2_loc (loc, code, type, arg0, arg1));
10245 /* Fold a call to built-in function FNDECL with 0 arguments.
10246 IGNORE is true if the result of the function call is ignored. This
10247 function returns NULL_TREE if no simplification was possible. */
10249 static tree
10250 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10252 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10253 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10254 switch (fcode)
10256 CASE_FLT_FN (BUILT_IN_INF):
10257 case BUILT_IN_INFD32:
10258 case BUILT_IN_INFD64:
10259 case BUILT_IN_INFD128:
10260 return fold_builtin_inf (loc, type, true);
10262 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10263 return fold_builtin_inf (loc, type, false);
10265 case BUILT_IN_CLASSIFY_TYPE:
10266 return fold_builtin_classify_type (NULL_TREE);
10268 default:
10269 break;
10271 return NULL_TREE;
10274 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10275 IGNORE is true if the result of the function call is ignored. This
10276 function returns NULL_TREE if no simplification was possible. */
10278 static tree
10279 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10281 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10282 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10283 switch (fcode)
10285 case BUILT_IN_CONSTANT_P:
10287 tree val = fold_builtin_constant_p (arg0);
10289 /* Gimplification will pull the CALL_EXPR for the builtin out of
10290 an if condition. When not optimizing, we'll not CSE it back.
10291 To avoid link error types of regressions, return false now. */
10292 if (!val && !optimize)
10293 val = integer_zero_node;
10295 return val;
10298 case BUILT_IN_CLASSIFY_TYPE:
10299 return fold_builtin_classify_type (arg0);
10301 case BUILT_IN_STRLEN:
10302 return fold_builtin_strlen (loc, type, arg0);
10304 CASE_FLT_FN (BUILT_IN_FABS):
10305 return fold_builtin_fabs (loc, arg0, type);
10307 case BUILT_IN_ABS:
10308 case BUILT_IN_LABS:
10309 case BUILT_IN_LLABS:
10310 case BUILT_IN_IMAXABS:
10311 return fold_builtin_abs (loc, arg0, type);
10313 CASE_FLT_FN (BUILT_IN_CONJ):
10314 if (validate_arg (arg0, COMPLEX_TYPE)
10315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10316 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10317 break;
10319 CASE_FLT_FN (BUILT_IN_CREAL):
10320 if (validate_arg (arg0, COMPLEX_TYPE)
10321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10322 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10323 break;
10325 CASE_FLT_FN (BUILT_IN_CIMAG):
10326 if (validate_arg (arg0, COMPLEX_TYPE)
10327 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10328 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10329 break;
10331 CASE_FLT_FN (BUILT_IN_CCOS):
10332 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10334 CASE_FLT_FN (BUILT_IN_CCOSH):
10335 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10337 CASE_FLT_FN (BUILT_IN_CPROJ):
10338 return fold_builtin_cproj(loc, arg0, type);
10340 CASE_FLT_FN (BUILT_IN_CSIN):
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_sin);
10344 break;
10346 CASE_FLT_FN (BUILT_IN_CSINH):
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_sinh);
10350 break;
10352 CASE_FLT_FN (BUILT_IN_CTAN):
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_tan);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_CTANH):
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_tanh);
10362 break;
10364 CASE_FLT_FN (BUILT_IN_CLOG):
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_log);
10368 break;
10370 CASE_FLT_FN (BUILT_IN_CSQRT):
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_sqrt);
10374 break;
10376 CASE_FLT_FN (BUILT_IN_CASIN):
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_asin);
10380 break;
10382 CASE_FLT_FN (BUILT_IN_CACOS):
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_acos);
10386 break;
10388 CASE_FLT_FN (BUILT_IN_CATAN):
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_atan);
10392 break;
10394 CASE_FLT_FN (BUILT_IN_CASINH):
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_asinh);
10398 break;
10400 CASE_FLT_FN (BUILT_IN_CACOSH):
10401 if (validate_arg (arg0, COMPLEX_TYPE)
10402 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10403 return do_mpc_arg1 (arg0, type, mpc_acosh);
10404 break;
10406 CASE_FLT_FN (BUILT_IN_CATANH):
10407 if (validate_arg (arg0, COMPLEX_TYPE)
10408 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10409 return do_mpc_arg1 (arg0, type, mpc_atanh);
10410 break;
10412 CASE_FLT_FN (BUILT_IN_CABS):
10413 return fold_builtin_cabs (loc, arg0, type, fndecl);
10415 CASE_FLT_FN (BUILT_IN_CARG):
10416 return fold_builtin_carg (loc, arg0, type);
10418 CASE_FLT_FN (BUILT_IN_SQRT):
10419 return fold_builtin_sqrt (loc, arg0, type);
10421 CASE_FLT_FN (BUILT_IN_CBRT):
10422 return fold_builtin_cbrt (loc, arg0, type);
10424 CASE_FLT_FN (BUILT_IN_ASIN):
10425 if (validate_arg (arg0, REAL_TYPE))
10426 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10427 &dconstm1, &dconst1, true);
10428 break;
10430 CASE_FLT_FN (BUILT_IN_ACOS):
10431 if (validate_arg (arg0, REAL_TYPE))
10432 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10433 &dconstm1, &dconst1, true);
10434 break;
10436 CASE_FLT_FN (BUILT_IN_ATAN):
10437 if (validate_arg (arg0, REAL_TYPE))
10438 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10439 break;
10441 CASE_FLT_FN (BUILT_IN_ASINH):
10442 if (validate_arg (arg0, REAL_TYPE))
10443 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10444 break;
10446 CASE_FLT_FN (BUILT_IN_ACOSH):
10447 if (validate_arg (arg0, REAL_TYPE))
10448 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10449 &dconst1, NULL, true);
10450 break;
10452 CASE_FLT_FN (BUILT_IN_ATANH):
10453 if (validate_arg (arg0, REAL_TYPE))
10454 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10455 &dconstm1, &dconst1, false);
10456 break;
10458 CASE_FLT_FN (BUILT_IN_SIN):
10459 if (validate_arg (arg0, REAL_TYPE))
10460 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10461 break;
10463 CASE_FLT_FN (BUILT_IN_COS):
10464 return fold_builtin_cos (loc, arg0, type, fndecl);
10466 CASE_FLT_FN (BUILT_IN_TAN):
10467 return fold_builtin_tan (arg0, type);
10469 CASE_FLT_FN (BUILT_IN_CEXP):
10470 return fold_builtin_cexp (loc, arg0, type);
10472 CASE_FLT_FN (BUILT_IN_CEXPI):
10473 if (validate_arg (arg0, REAL_TYPE))
10474 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10475 break;
10477 CASE_FLT_FN (BUILT_IN_SINH):
10478 if (validate_arg (arg0, REAL_TYPE))
10479 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10480 break;
10482 CASE_FLT_FN (BUILT_IN_COSH):
10483 return fold_builtin_cosh (loc, arg0, type, fndecl);
10485 CASE_FLT_FN (BUILT_IN_TANH):
10486 if (validate_arg (arg0, REAL_TYPE))
10487 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10488 break;
10490 CASE_FLT_FN (BUILT_IN_ERF):
10491 if (validate_arg (arg0, REAL_TYPE))
10492 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10493 break;
10495 CASE_FLT_FN (BUILT_IN_ERFC):
10496 if (validate_arg (arg0, REAL_TYPE))
10497 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10498 break;
10500 CASE_FLT_FN (BUILT_IN_TGAMMA):
10501 if (validate_arg (arg0, REAL_TYPE))
10502 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10503 break;
10505 CASE_FLT_FN (BUILT_IN_EXP):
10506 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10508 CASE_FLT_FN (BUILT_IN_EXP2):
10509 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10511 CASE_FLT_FN (BUILT_IN_EXP10):
10512 CASE_FLT_FN (BUILT_IN_POW10):
10513 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10515 CASE_FLT_FN (BUILT_IN_EXPM1):
10516 if (validate_arg (arg0, REAL_TYPE))
10517 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10518 break;
10520 CASE_FLT_FN (BUILT_IN_LOG):
10521 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10523 CASE_FLT_FN (BUILT_IN_LOG2):
10524 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10526 CASE_FLT_FN (BUILT_IN_LOG10):
10527 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10529 CASE_FLT_FN (BUILT_IN_LOG1P):
10530 if (validate_arg (arg0, REAL_TYPE))
10531 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10532 &dconstm1, NULL, false);
10533 break;
10535 CASE_FLT_FN (BUILT_IN_J0):
10536 if (validate_arg (arg0, REAL_TYPE))
10537 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10538 NULL, NULL, 0);
10539 break;
10541 CASE_FLT_FN (BUILT_IN_J1):
10542 if (validate_arg (arg0, REAL_TYPE))
10543 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10544 NULL, NULL, 0);
10545 break;
10547 CASE_FLT_FN (BUILT_IN_Y0):
10548 if (validate_arg (arg0, REAL_TYPE))
10549 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10550 &dconst0, NULL, false);
10551 break;
10553 CASE_FLT_FN (BUILT_IN_Y1):
10554 if (validate_arg (arg0, REAL_TYPE))
10555 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10556 &dconst0, NULL, false);
10557 break;
10559 CASE_FLT_FN (BUILT_IN_NAN):
10560 case BUILT_IN_NAND32:
10561 case BUILT_IN_NAND64:
10562 case BUILT_IN_NAND128:
10563 return fold_builtin_nan (arg0, type, true);
10565 CASE_FLT_FN (BUILT_IN_NANS):
10566 return fold_builtin_nan (arg0, type, false);
10568 CASE_FLT_FN (BUILT_IN_FLOOR):
10569 return fold_builtin_floor (loc, fndecl, arg0);
10571 CASE_FLT_FN (BUILT_IN_CEIL):
10572 return fold_builtin_ceil (loc, fndecl, arg0);
10574 CASE_FLT_FN (BUILT_IN_TRUNC):
10575 return fold_builtin_trunc (loc, fndecl, arg0);
10577 CASE_FLT_FN (BUILT_IN_ROUND):
10578 return fold_builtin_round (loc, fndecl, arg0);
10580 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10581 CASE_FLT_FN (BUILT_IN_RINT):
10582 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10584 CASE_FLT_FN (BUILT_IN_ICEIL):
10585 CASE_FLT_FN (BUILT_IN_LCEIL):
10586 CASE_FLT_FN (BUILT_IN_LLCEIL):
10587 CASE_FLT_FN (BUILT_IN_LFLOOR):
10588 CASE_FLT_FN (BUILT_IN_IFLOOR):
10589 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10590 CASE_FLT_FN (BUILT_IN_IROUND):
10591 CASE_FLT_FN (BUILT_IN_LROUND):
10592 CASE_FLT_FN (BUILT_IN_LLROUND):
10593 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10595 CASE_FLT_FN (BUILT_IN_IRINT):
10596 CASE_FLT_FN (BUILT_IN_LRINT):
10597 CASE_FLT_FN (BUILT_IN_LLRINT):
10598 return fold_fixed_mathfn (loc, fndecl, arg0);
10600 case BUILT_IN_BSWAP16:
10601 case BUILT_IN_BSWAP32:
10602 case BUILT_IN_BSWAP64:
10603 return fold_builtin_bswap (fndecl, arg0);
10605 CASE_INT_FN (BUILT_IN_FFS):
10606 CASE_INT_FN (BUILT_IN_CLZ):
10607 CASE_INT_FN (BUILT_IN_CTZ):
10608 CASE_INT_FN (BUILT_IN_CLRSB):
10609 CASE_INT_FN (BUILT_IN_POPCOUNT):
10610 CASE_INT_FN (BUILT_IN_PARITY):
10611 return fold_builtin_bitop (fndecl, arg0);
10613 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10614 return fold_builtin_signbit (loc, arg0, type);
10616 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10617 return fold_builtin_significand (loc, arg0, type);
10619 CASE_FLT_FN (BUILT_IN_ILOGB):
10620 CASE_FLT_FN (BUILT_IN_LOGB):
10621 return fold_builtin_logb (loc, arg0, type);
10623 case BUILT_IN_ISASCII:
10624 return fold_builtin_isascii (loc, arg0);
10626 case BUILT_IN_TOASCII:
10627 return fold_builtin_toascii (loc, arg0);
10629 case BUILT_IN_ISDIGIT:
10630 return fold_builtin_isdigit (loc, arg0);
10632 CASE_FLT_FN (BUILT_IN_FINITE):
10633 case BUILT_IN_FINITED32:
10634 case BUILT_IN_FINITED64:
10635 case BUILT_IN_FINITED128:
10636 case BUILT_IN_ISFINITE:
10638 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10639 if (ret)
10640 return ret;
10641 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10644 CASE_FLT_FN (BUILT_IN_ISINF):
10645 case BUILT_IN_ISINFD32:
10646 case BUILT_IN_ISINFD64:
10647 case BUILT_IN_ISINFD128:
10649 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10650 if (ret)
10651 return ret;
10652 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10655 case BUILT_IN_ISNORMAL:
10656 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10658 case BUILT_IN_ISINF_SIGN:
10659 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10661 CASE_FLT_FN (BUILT_IN_ISNAN):
10662 case BUILT_IN_ISNAND32:
10663 case BUILT_IN_ISNAND64:
10664 case BUILT_IN_ISNAND128:
10665 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10667 case BUILT_IN_PRINTF:
10668 case BUILT_IN_PRINTF_UNLOCKED:
10669 case BUILT_IN_VPRINTF:
10670 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10672 case BUILT_IN_FREE:
10673 if (integer_zerop (arg0))
10674 return build_empty_stmt (loc);
10675 break;
10677 default:
10678 break;
10681 return NULL_TREE;
10685 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10686 IGNORE is true if the result of the function call is ignored. This
10687 function returns NULL_TREE if no simplification was possible. */
10689 static tree
10690 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10692 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10693 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10695 switch (fcode)
10697 CASE_FLT_FN (BUILT_IN_JN):
10698 if (validate_arg (arg0, INTEGER_TYPE)
10699 && validate_arg (arg1, REAL_TYPE))
10700 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10701 break;
10703 CASE_FLT_FN (BUILT_IN_YN):
10704 if (validate_arg (arg0, INTEGER_TYPE)
10705 && validate_arg (arg1, REAL_TYPE))
10706 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10707 &dconst0, false);
10708 break;
10710 CASE_FLT_FN (BUILT_IN_DREM):
10711 CASE_FLT_FN (BUILT_IN_REMAINDER):
10712 if (validate_arg (arg0, REAL_TYPE)
10713 && validate_arg(arg1, REAL_TYPE))
10714 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10715 break;
10717 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10718 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10719 if (validate_arg (arg0, REAL_TYPE)
10720 && validate_arg(arg1, POINTER_TYPE))
10721 return do_mpfr_lgamma_r (arg0, arg1, type);
10722 break;
10724 CASE_FLT_FN (BUILT_IN_ATAN2):
10725 if (validate_arg (arg0, REAL_TYPE)
10726 && validate_arg(arg1, REAL_TYPE))
10727 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10728 break;
10730 CASE_FLT_FN (BUILT_IN_FDIM):
10731 if (validate_arg (arg0, REAL_TYPE)
10732 && validate_arg(arg1, REAL_TYPE))
10733 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10734 break;
10736 CASE_FLT_FN (BUILT_IN_HYPOT):
10737 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10739 CASE_FLT_FN (BUILT_IN_CPOW):
10740 if (validate_arg (arg0, COMPLEX_TYPE)
10741 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10742 && validate_arg (arg1, COMPLEX_TYPE)
10743 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10744 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10745 break;
10747 CASE_FLT_FN (BUILT_IN_LDEXP):
10748 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10749 CASE_FLT_FN (BUILT_IN_SCALBN):
10750 CASE_FLT_FN (BUILT_IN_SCALBLN):
10751 return fold_builtin_load_exponent (loc, arg0, arg1,
10752 type, /*ldexp=*/false);
10754 CASE_FLT_FN (BUILT_IN_FREXP):
10755 return fold_builtin_frexp (loc, arg0, arg1, type);
10757 CASE_FLT_FN (BUILT_IN_MODF):
10758 return fold_builtin_modf (loc, arg0, arg1, type);
10760 case BUILT_IN_BZERO:
10761 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10763 case BUILT_IN_FPUTS:
10764 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10766 case BUILT_IN_FPUTS_UNLOCKED:
10767 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10769 case BUILT_IN_STRSTR:
10770 return fold_builtin_strstr (loc, arg0, arg1, type);
10772 case BUILT_IN_STRCAT:
10773 return fold_builtin_strcat (loc, arg0, arg1);
10775 case BUILT_IN_STRSPN:
10776 return fold_builtin_strspn (loc, arg0, arg1);
10778 case BUILT_IN_STRCSPN:
10779 return fold_builtin_strcspn (loc, arg0, arg1);
10781 case BUILT_IN_STRCHR:
10782 case BUILT_IN_INDEX:
10783 return fold_builtin_strchr (loc, arg0, arg1, type);
10785 case BUILT_IN_STRRCHR:
10786 case BUILT_IN_RINDEX:
10787 return fold_builtin_strrchr (loc, arg0, arg1, type);
10789 case BUILT_IN_STRCPY:
10790 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10792 case BUILT_IN_STPCPY:
10793 if (ignore)
10795 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10796 if (!fn)
10797 break;
10799 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10801 else
10802 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10803 break;
10805 case BUILT_IN_STRCMP:
10806 return fold_builtin_strcmp (loc, arg0, arg1);
10808 case BUILT_IN_STRPBRK:
10809 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10811 case BUILT_IN_EXPECT:
10812 return fold_builtin_expect (loc, arg0, arg1);
10814 CASE_FLT_FN (BUILT_IN_POW):
10815 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10817 CASE_FLT_FN (BUILT_IN_POWI):
10818 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10820 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10821 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10823 CASE_FLT_FN (BUILT_IN_FMIN):
10824 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10826 CASE_FLT_FN (BUILT_IN_FMAX):
10827 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10829 case BUILT_IN_ISGREATER:
10830 return fold_builtin_unordered_cmp (loc, fndecl,
10831 arg0, arg1, UNLE_EXPR, LE_EXPR);
10832 case BUILT_IN_ISGREATEREQUAL:
10833 return fold_builtin_unordered_cmp (loc, fndecl,
10834 arg0, arg1, UNLT_EXPR, LT_EXPR);
10835 case BUILT_IN_ISLESS:
10836 return fold_builtin_unordered_cmp (loc, fndecl,
10837 arg0, arg1, UNGE_EXPR, GE_EXPR);
10838 case BUILT_IN_ISLESSEQUAL:
10839 return fold_builtin_unordered_cmp (loc, fndecl,
10840 arg0, arg1, UNGT_EXPR, GT_EXPR);
10841 case BUILT_IN_ISLESSGREATER:
10842 return fold_builtin_unordered_cmp (loc, fndecl,
10843 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10844 case BUILT_IN_ISUNORDERED:
10845 return fold_builtin_unordered_cmp (loc, fndecl,
10846 arg0, arg1, UNORDERED_EXPR,
10847 NOP_EXPR);
10849 /* We do the folding for va_start in the expander. */
10850 case BUILT_IN_VA_START:
10851 break;
10853 case BUILT_IN_SPRINTF:
10854 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10856 case BUILT_IN_OBJECT_SIZE:
10857 return fold_builtin_object_size (arg0, arg1);
10859 case BUILT_IN_PRINTF:
10860 case BUILT_IN_PRINTF_UNLOCKED:
10861 case BUILT_IN_VPRINTF:
10862 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10864 case BUILT_IN_PRINTF_CHK:
10865 case BUILT_IN_VPRINTF_CHK:
10866 if (!validate_arg (arg0, INTEGER_TYPE)
10867 || TREE_SIDE_EFFECTS (arg0))
10868 return NULL_TREE;
10869 else
10870 return fold_builtin_printf (loc, fndecl,
10871 arg1, NULL_TREE, ignore, fcode);
10872 break;
10874 case BUILT_IN_FPRINTF:
10875 case BUILT_IN_FPRINTF_UNLOCKED:
10876 case BUILT_IN_VFPRINTF:
10877 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10878 ignore, fcode);
10880 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10881 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10883 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10884 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10886 default:
10887 break;
10889 return NULL_TREE;
10892 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10893 and ARG2. IGNORE is true if the result of the function call is ignored.
10894 This function returns NULL_TREE if no simplification was possible. */
10896 static tree
10897 fold_builtin_3 (location_t loc, tree fndecl,
10898 tree arg0, tree arg1, tree arg2, bool ignore)
10900 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10901 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10902 switch (fcode)
10905 CASE_FLT_FN (BUILT_IN_SINCOS):
10906 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10908 CASE_FLT_FN (BUILT_IN_FMA):
10909 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10910 break;
10912 CASE_FLT_FN (BUILT_IN_REMQUO):
10913 if (validate_arg (arg0, REAL_TYPE)
10914 && validate_arg(arg1, REAL_TYPE)
10915 && validate_arg(arg2, POINTER_TYPE))
10916 return do_mpfr_remquo (arg0, arg1, arg2);
10917 break;
10919 case BUILT_IN_MEMSET:
10920 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10922 case BUILT_IN_BCOPY:
10923 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10924 void_type_node, true, /*endp=*/3);
10926 case BUILT_IN_MEMCPY:
10927 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10928 type, ignore, /*endp=*/0);
10930 case BUILT_IN_MEMPCPY:
10931 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10932 type, ignore, /*endp=*/1);
10934 case BUILT_IN_MEMMOVE:
10935 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10936 type, ignore, /*endp=*/3);
10938 case BUILT_IN_STRNCAT:
10939 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10941 case BUILT_IN_STRNCPY:
10942 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10944 case BUILT_IN_STRNCMP:
10945 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10947 case BUILT_IN_MEMCHR:
10948 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10950 case BUILT_IN_BCMP:
10951 case BUILT_IN_MEMCMP:
10952 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10954 case BUILT_IN_SPRINTF:
10955 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10957 case BUILT_IN_SNPRINTF:
10958 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10960 case BUILT_IN_STRCPY_CHK:
10961 case BUILT_IN_STPCPY_CHK:
10962 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10963 ignore, fcode);
10965 case BUILT_IN_STRCAT_CHK:
10966 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10968 case BUILT_IN_PRINTF_CHK:
10969 case BUILT_IN_VPRINTF_CHK:
10970 if (!validate_arg (arg0, INTEGER_TYPE)
10971 || TREE_SIDE_EFFECTS (arg0))
10972 return NULL_TREE;
10973 else
10974 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10975 break;
10977 case BUILT_IN_FPRINTF:
10978 case BUILT_IN_FPRINTF_UNLOCKED:
10979 case BUILT_IN_VFPRINTF:
10980 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10981 ignore, fcode);
10983 case BUILT_IN_FPRINTF_CHK:
10984 case BUILT_IN_VFPRINTF_CHK:
10985 if (!validate_arg (arg1, INTEGER_TYPE)
10986 || TREE_SIDE_EFFECTS (arg1))
10987 return NULL_TREE;
10988 else
10989 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10990 ignore, fcode);
10992 default:
10993 break;
10995 return NULL_TREE;
10998 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10999 ARG2, and ARG3. IGNORE is true if the result of the function call is
11000 ignored. This function returns NULL_TREE if no simplification was
11001 possible. */
11003 static tree
11004 fold_builtin_4 (location_t loc, tree fndecl,
11005 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11007 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11009 switch (fcode)
11011 case BUILT_IN_MEMCPY_CHK:
11012 case BUILT_IN_MEMPCPY_CHK:
11013 case BUILT_IN_MEMMOVE_CHK:
11014 case BUILT_IN_MEMSET_CHK:
11015 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11016 NULL_TREE, ignore,
11017 DECL_FUNCTION_CODE (fndecl));
11019 case BUILT_IN_STRNCPY_CHK:
11020 case BUILT_IN_STPNCPY_CHK:
11021 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11022 ignore, fcode);
11024 case BUILT_IN_STRNCAT_CHK:
11025 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11027 case BUILT_IN_SNPRINTF:
11028 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11030 case BUILT_IN_FPRINTF_CHK:
11031 case BUILT_IN_VFPRINTF_CHK:
11032 if (!validate_arg (arg1, INTEGER_TYPE)
11033 || TREE_SIDE_EFFECTS (arg1))
11034 return NULL_TREE;
11035 else
11036 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11037 ignore, fcode);
11038 break;
11040 default:
11041 break;
11043 return NULL_TREE;
11046 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11047 arguments, where NARGS <= 4. IGNORE is true if the result of the
11048 function call is ignored. This function returns NULL_TREE if no
11049 simplification was possible. Note that this only folds builtins with
11050 fixed argument patterns. Foldings that do varargs-to-varargs
11051 transformations, or that match calls with more than 4 arguments,
11052 need to be handled with fold_builtin_varargs instead. */
11054 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11056 static tree
11057 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11059 tree ret = NULL_TREE;
11061 switch (nargs)
11063 case 0:
11064 ret = fold_builtin_0 (loc, fndecl, ignore);
11065 break;
11066 case 1:
11067 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11068 break;
11069 case 2:
11070 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11071 break;
11072 case 3:
11073 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11074 break;
11075 case 4:
11076 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11077 ignore);
11078 break;
11079 default:
11080 break;
11082 if (ret)
11084 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11085 SET_EXPR_LOCATION (ret, loc);
11086 TREE_NO_WARNING (ret) = 1;
11087 return ret;
11089 return NULL_TREE;
11092 /* Builtins with folding operations that operate on "..." arguments
11093 need special handling; we need to store the arguments in a convenient
11094 data structure before attempting any folding. Fortunately there are
11095 only a few builtins that fall into this category. FNDECL is the
11096 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11097 result of the function call is ignored. */
11099 static tree
11100 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11101 bool ignore ATTRIBUTE_UNUSED)
11103 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11104 tree ret = NULL_TREE;
11106 switch (fcode)
11108 case BUILT_IN_SPRINTF_CHK:
11109 case BUILT_IN_VSPRINTF_CHK:
11110 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11111 break;
11113 case BUILT_IN_SNPRINTF_CHK:
11114 case BUILT_IN_VSNPRINTF_CHK:
11115 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11116 break;
11118 case BUILT_IN_FPCLASSIFY:
11119 ret = fold_builtin_fpclassify (loc, exp);
11120 break;
11122 default:
11123 break;
11125 if (ret)
11127 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11128 SET_EXPR_LOCATION (ret, loc);
11129 TREE_NO_WARNING (ret) = 1;
11130 return ret;
11132 return NULL_TREE;
11135 /* Return true if FNDECL shouldn't be folded right now.
11136 If a built-in function has an inline attribute always_inline
11137 wrapper, defer folding it after always_inline functions have
11138 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11139 might not be performed. */
11141 bool
11142 avoid_folding_inline_builtin (tree fndecl)
11144 return (DECL_DECLARED_INLINE_P (fndecl)
11145 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11146 && cfun
11147 && !cfun->always_inline_functions_inlined
11148 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11151 /* A wrapper function for builtin folding that prevents warnings for
11152 "statement without effect" and the like, caused by removing the
11153 call node earlier than the warning is generated. */
11155 tree
11156 fold_call_expr (location_t loc, tree exp, bool ignore)
11158 tree ret = NULL_TREE;
11159 tree fndecl = get_callee_fndecl (exp);
11160 if (fndecl
11161 && TREE_CODE (fndecl) == FUNCTION_DECL
11162 && DECL_BUILT_IN (fndecl)
11163 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11164 yet. Defer folding until we see all the arguments
11165 (after inlining). */
11166 && !CALL_EXPR_VA_ARG_PACK (exp))
11168 int nargs = call_expr_nargs (exp);
11170 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11171 instead last argument is __builtin_va_arg_pack (). Defer folding
11172 even in that case, until arguments are finalized. */
11173 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11175 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11176 if (fndecl2
11177 && TREE_CODE (fndecl2) == FUNCTION_DECL
11178 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11179 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11180 return NULL_TREE;
11183 if (avoid_folding_inline_builtin (fndecl))
11184 return NULL_TREE;
11186 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11187 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11188 CALL_EXPR_ARGP (exp), ignore);
11189 else
11191 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11193 tree *args = CALL_EXPR_ARGP (exp);
11194 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11196 if (!ret)
11197 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11198 if (ret)
11199 return ret;
11202 return NULL_TREE;
11205 /* Conveniently construct a function call expression. FNDECL names the
11206 function to be called and N arguments are passed in the array
11207 ARGARRAY. */
11209 tree
11210 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11212 tree fntype = TREE_TYPE (fndecl);
11213 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11215 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11218 /* Conveniently construct a function call expression. FNDECL names the
11219 function to be called and the arguments are passed in the vector
11220 VEC. */
11222 tree
11223 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11225 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11226 vec_safe_address (vec));
11230 /* Conveniently construct a function call expression. FNDECL names the
11231 function to be called, N is the number of arguments, and the "..."
11232 parameters are the argument expressions. */
11234 tree
11235 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11237 va_list ap;
11238 tree *argarray = XALLOCAVEC (tree, n);
11239 int i;
11241 va_start (ap, n);
11242 for (i = 0; i < n; i++)
11243 argarray[i] = va_arg (ap, tree);
11244 va_end (ap);
11245 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11248 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11249 varargs macros aren't supported by all bootstrap compilers. */
11251 tree
11252 build_call_expr (tree fndecl, int n, ...)
11254 va_list ap;
11255 tree *argarray = XALLOCAVEC (tree, n);
11256 int i;
11258 va_start (ap, n);
11259 for (i = 0; i < n; i++)
11260 argarray[i] = va_arg (ap, tree);
11261 va_end (ap);
11262 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11265 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11266 N arguments are passed in the array ARGARRAY. */
11268 tree
11269 fold_builtin_call_array (location_t loc, tree type,
11270 tree fn,
11271 int n,
11272 tree *argarray)
11274 tree ret = NULL_TREE;
11275 tree exp;
11277 if (TREE_CODE (fn) == ADDR_EXPR)
11279 tree fndecl = TREE_OPERAND (fn, 0);
11280 if (TREE_CODE (fndecl) == FUNCTION_DECL
11281 && DECL_BUILT_IN (fndecl))
11283 /* If last argument is __builtin_va_arg_pack (), arguments to this
11284 function are not finalized yet. Defer folding until they are. */
11285 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11287 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11288 if (fndecl2
11289 && TREE_CODE (fndecl2) == FUNCTION_DECL
11290 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11291 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11292 return build_call_array_loc (loc, type, fn, n, argarray);
11294 if (avoid_folding_inline_builtin (fndecl))
11295 return build_call_array_loc (loc, type, fn, n, argarray);
11296 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11298 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11299 if (ret)
11300 return ret;
11302 return build_call_array_loc (loc, type, fn, n, argarray);
11304 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11306 /* First try the transformations that don't require consing up
11307 an exp. */
11308 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11309 if (ret)
11310 return ret;
11313 /* If we got this far, we need to build an exp. */
11314 exp = build_call_array_loc (loc, type, fn, n, argarray);
11315 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11316 return ret ? ret : exp;
11320 return build_call_array_loc (loc, type, fn, n, argarray);
11323 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11324 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11325 of arguments in ARGS to be omitted. OLDNARGS is the number of
11326 elements in ARGS. */
11328 static tree
11329 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11330 int skip, tree fndecl, int n, va_list newargs)
11332 int nargs = oldnargs - skip + n;
11333 tree *buffer;
11335 if (n > 0)
11337 int i, j;
11339 buffer = XALLOCAVEC (tree, nargs);
11340 for (i = 0; i < n; i++)
11341 buffer[i] = va_arg (newargs, tree);
11342 for (j = skip; j < oldnargs; j++, i++)
11343 buffer[i] = args[j];
11345 else
11346 buffer = args + skip;
11348 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11351 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11352 list ARGS along with N new arguments specified as the "..."
11353 parameters. SKIP is the number of arguments in ARGS to be omitted.
11354 OLDNARGS is the number of elements in ARGS. */
11356 static tree
11357 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11358 int skip, tree fndecl, int n, ...)
11360 va_list ap;
11361 tree t;
11363 va_start (ap, n);
11364 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11365 va_end (ap);
11367 return t;
11370 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11371 along with N new arguments specified as the "..." parameters. SKIP
11372 is the number of arguments in EXP to be omitted. This function is used
11373 to do varargs-to-varargs transformations. */
11375 static tree
11376 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11378 va_list ap;
11379 tree t;
11381 va_start (ap, n);
11382 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11383 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11384 va_end (ap);
11386 return t;
11389 /* Validate a single argument ARG against a tree code CODE representing
11390 a type. */
11392 static bool
11393 validate_arg (const_tree arg, enum tree_code code)
11395 if (!arg)
11396 return false;
11397 else if (code == POINTER_TYPE)
11398 return POINTER_TYPE_P (TREE_TYPE (arg));
11399 else if (code == INTEGER_TYPE)
11400 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11401 return code == TREE_CODE (TREE_TYPE (arg));
11404 /* This function validates the types of a function call argument list
11405 against a specified list of tree_codes. If the last specifier is a 0,
11406 that represents an ellipses, otherwise the last specifier must be a
11407 VOID_TYPE.
11409 This is the GIMPLE version of validate_arglist. Eventually we want to
11410 completely convert builtins.c to work from GIMPLEs and the tree based
11411 validate_arglist will then be removed. */
11413 bool
11414 validate_gimple_arglist (const_gimple call, ...)
11416 enum tree_code code;
11417 bool res = 0;
11418 va_list ap;
11419 const_tree arg;
11420 size_t i;
11422 va_start (ap, call);
11423 i = 0;
11427 code = (enum tree_code) va_arg (ap, int);
11428 switch (code)
11430 case 0:
11431 /* This signifies an ellipses, any further arguments are all ok. */
11432 res = true;
11433 goto end;
11434 case VOID_TYPE:
11435 /* This signifies an endlink, if no arguments remain, return
11436 true, otherwise return false. */
11437 res = (i == gimple_call_num_args (call));
11438 goto end;
11439 default:
11440 /* If no parameters remain or the parameter's code does not
11441 match the specified code, return false. Otherwise continue
11442 checking any remaining arguments. */
11443 arg = gimple_call_arg (call, i++);
11444 if (!validate_arg (arg, code))
11445 goto end;
11446 break;
11449 while (1);
11451 /* We need gotos here since we can only have one VA_CLOSE in a
11452 function. */
11453 end: ;
11454 va_end (ap);
11456 return res;
11459 /* This function validates the types of a function call argument list
11460 against a specified list of tree_codes. If the last specifier is a 0,
11461 that represents an ellipses, otherwise the last specifier must be a
11462 VOID_TYPE. */
11464 bool
11465 validate_arglist (const_tree callexpr, ...)
11467 enum tree_code code;
11468 bool res = 0;
11469 va_list ap;
11470 const_call_expr_arg_iterator iter;
11471 const_tree arg;
11473 va_start (ap, callexpr);
11474 init_const_call_expr_arg_iterator (callexpr, &iter);
11478 code = (enum tree_code) va_arg (ap, int);
11479 switch (code)
11481 case 0:
11482 /* This signifies an ellipses, any further arguments are all ok. */
11483 res = true;
11484 goto end;
11485 case VOID_TYPE:
11486 /* This signifies an endlink, if no arguments remain, return
11487 true, otherwise return false. */
11488 res = !more_const_call_expr_args_p (&iter);
11489 goto end;
11490 default:
11491 /* If no parameters remain or the parameter's code does not
11492 match the specified code, return false. Otherwise continue
11493 checking any remaining arguments. */
11494 arg = next_const_call_expr_arg (&iter);
11495 if (!validate_arg (arg, code))
11496 goto end;
11497 break;
11500 while (1);
11502 /* We need gotos here since we can only have one VA_CLOSE in a
11503 function. */
11504 end: ;
11505 va_end (ap);
11507 return res;
11510 /* Default target-specific builtin expander that does nothing. */
11513 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11514 rtx target ATTRIBUTE_UNUSED,
11515 rtx subtarget ATTRIBUTE_UNUSED,
11516 enum machine_mode mode ATTRIBUTE_UNUSED,
11517 int ignore ATTRIBUTE_UNUSED)
11519 return NULL_RTX;
11522 /* Returns true is EXP represents data that would potentially reside
11523 in a readonly section. */
11525 static bool
11526 readonly_data_expr (tree exp)
11528 STRIP_NOPS (exp);
11530 if (TREE_CODE (exp) != ADDR_EXPR)
11531 return false;
11533 exp = get_base_address (TREE_OPERAND (exp, 0));
11534 if (!exp)
11535 return false;
11537 /* Make sure we call decl_readonly_section only for trees it
11538 can handle (since it returns true for everything it doesn't
11539 understand). */
11540 if (TREE_CODE (exp) == STRING_CST
11541 || TREE_CODE (exp) == CONSTRUCTOR
11542 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11543 return decl_readonly_section (exp, 0);
11544 else
11545 return false;
11548 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11549 to the call, and TYPE is its return type.
11551 Return NULL_TREE if no simplification was possible, otherwise return the
11552 simplified form of the call as a tree.
11554 The simplified form may be a constant or other expression which
11555 computes the same value, but in a more efficient manner (including
11556 calls to other builtin functions).
11558 The call may contain arguments which need to be evaluated, but
11559 which are not useful to determine the result of the call. In
11560 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11561 COMPOUND_EXPR will be an argument which must be evaluated.
11562 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11563 COMPOUND_EXPR in the chain will contain the tree for the simplified
11564 form of the builtin function call. */
11566 static tree
11567 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11569 if (!validate_arg (s1, POINTER_TYPE)
11570 || !validate_arg (s2, POINTER_TYPE))
11571 return NULL_TREE;
11572 else
11574 tree fn;
11575 const char *p1, *p2;
11577 p2 = c_getstr (s2);
11578 if (p2 == NULL)
11579 return NULL_TREE;
11581 p1 = c_getstr (s1);
11582 if (p1 != NULL)
11584 const char *r = strstr (p1, p2);
11585 tree tem;
11587 if (r == NULL)
11588 return build_int_cst (TREE_TYPE (s1), 0);
11590 /* Return an offset into the constant string argument. */
11591 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11592 return fold_convert_loc (loc, type, tem);
11595 /* The argument is const char *, and the result is char *, so we need
11596 a type conversion here to avoid a warning. */
11597 if (p2[0] == '\0')
11598 return fold_convert_loc (loc, type, s1);
11600 if (p2[1] != '\0')
11601 return NULL_TREE;
11603 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11604 if (!fn)
11605 return NULL_TREE;
11607 /* New argument list transforming strstr(s1, s2) to
11608 strchr(s1, s2[0]). */
11609 return build_call_expr_loc (loc, fn, 2, s1,
11610 build_int_cst (integer_type_node, p2[0]));
11614 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11615 the call, and TYPE is its return type.
11617 Return NULL_TREE if no simplification was possible, otherwise return the
11618 simplified form of the call as a tree.
11620 The simplified form may be a constant or other expression which
11621 computes the same value, but in a more efficient manner (including
11622 calls to other builtin functions).
11624 The call may contain arguments which need to be evaluated, but
11625 which are not useful to determine the result of the call. In
11626 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11627 COMPOUND_EXPR will be an argument which must be evaluated.
11628 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11629 COMPOUND_EXPR in the chain will contain the tree for the simplified
11630 form of the builtin function call. */
11632 static tree
11633 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11635 if (!validate_arg (s1, POINTER_TYPE)
11636 || !validate_arg (s2, INTEGER_TYPE))
11637 return NULL_TREE;
11638 else
11640 const char *p1;
11642 if (TREE_CODE (s2) != INTEGER_CST)
11643 return NULL_TREE;
11645 p1 = c_getstr (s1);
11646 if (p1 != NULL)
11648 char c;
11649 const char *r;
11650 tree tem;
11652 if (target_char_cast (s2, &c))
11653 return NULL_TREE;
11655 r = strchr (p1, c);
11657 if (r == NULL)
11658 return build_int_cst (TREE_TYPE (s1), 0);
11660 /* Return an offset into the constant string argument. */
11661 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11662 return fold_convert_loc (loc, type, tem);
11664 return NULL_TREE;
11668 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11669 the call, and TYPE is its return type.
11671 Return NULL_TREE if no simplification was possible, otherwise return the
11672 simplified form of the call as a tree.
11674 The simplified form may be a constant or other expression which
11675 computes the same value, but in a more efficient manner (including
11676 calls to other builtin functions).
11678 The call may contain arguments which need to be evaluated, but
11679 which are not useful to determine the result of the call. In
11680 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11681 COMPOUND_EXPR will be an argument which must be evaluated.
11682 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11683 COMPOUND_EXPR in the chain will contain the tree for the simplified
11684 form of the builtin function call. */
11686 static tree
11687 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11689 if (!validate_arg (s1, POINTER_TYPE)
11690 || !validate_arg (s2, INTEGER_TYPE))
11691 return NULL_TREE;
11692 else
11694 tree fn;
11695 const char *p1;
11697 if (TREE_CODE (s2) != INTEGER_CST)
11698 return NULL_TREE;
11700 p1 = c_getstr (s1);
11701 if (p1 != NULL)
11703 char c;
11704 const char *r;
11705 tree tem;
11707 if (target_char_cast (s2, &c))
11708 return NULL_TREE;
11710 r = strrchr (p1, c);
11712 if (r == NULL)
11713 return build_int_cst (TREE_TYPE (s1), 0);
11715 /* Return an offset into the constant string argument. */
11716 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11717 return fold_convert_loc (loc, type, tem);
11720 if (! integer_zerop (s2))
11721 return NULL_TREE;
11723 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11724 if (!fn)
11725 return NULL_TREE;
11727 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11728 return build_call_expr_loc (loc, fn, 2, s1, s2);
11732 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11733 to the call, and TYPE is its return type.
11735 Return NULL_TREE if no simplification was possible, otherwise return the
11736 simplified form of the call as a tree.
11738 The simplified form may be a constant or other expression which
11739 computes the same value, but in a more efficient manner (including
11740 calls to other builtin functions).
11742 The call may contain arguments which need to be evaluated, but
11743 which are not useful to determine the result of the call. In
11744 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11745 COMPOUND_EXPR will be an argument which must be evaluated.
11746 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11747 COMPOUND_EXPR in the chain will contain the tree for the simplified
11748 form of the builtin function call. */
11750 static tree
11751 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11753 if (!validate_arg (s1, POINTER_TYPE)
11754 || !validate_arg (s2, POINTER_TYPE))
11755 return NULL_TREE;
11756 else
11758 tree fn;
11759 const char *p1, *p2;
11761 p2 = c_getstr (s2);
11762 if (p2 == NULL)
11763 return NULL_TREE;
11765 p1 = c_getstr (s1);
11766 if (p1 != NULL)
11768 const char *r = strpbrk (p1, p2);
11769 tree tem;
11771 if (r == NULL)
11772 return build_int_cst (TREE_TYPE (s1), 0);
11774 /* Return an offset into the constant string argument. */
11775 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11776 return fold_convert_loc (loc, type, tem);
11779 if (p2[0] == '\0')
11780 /* strpbrk(x, "") == NULL.
11781 Evaluate and ignore s1 in case it had side-effects. */
11782 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11784 if (p2[1] != '\0')
11785 return NULL_TREE; /* Really call strpbrk. */
11787 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11788 if (!fn)
11789 return NULL_TREE;
11791 /* New argument list transforming strpbrk(s1, s2) to
11792 strchr(s1, s2[0]). */
11793 return build_call_expr_loc (loc, fn, 2, s1,
11794 build_int_cst (integer_type_node, p2[0]));
11798 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11799 to the call.
11801 Return NULL_TREE if no simplification was possible, otherwise return the
11802 simplified form of the call as a tree.
11804 The simplified form may be a constant or other expression which
11805 computes the same value, but in a more efficient manner (including
11806 calls to other builtin functions).
11808 The call may contain arguments which need to be evaluated, but
11809 which are not useful to determine the result of the call. In
11810 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11811 COMPOUND_EXPR will be an argument which must be evaluated.
11812 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11813 COMPOUND_EXPR in the chain will contain the tree for the simplified
11814 form of the builtin function call. */
11816 static tree
11817 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11819 if (!validate_arg (dst, POINTER_TYPE)
11820 || !validate_arg (src, POINTER_TYPE))
11821 return NULL_TREE;
11822 else
11824 const char *p = c_getstr (src);
11826 /* If the string length is zero, return the dst parameter. */
11827 if (p && *p == '\0')
11828 return dst;
11830 if (optimize_insn_for_speed_p ())
11832 /* See if we can store by pieces into (dst + strlen(dst)). */
11833 tree newdst, call;
11834 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11835 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11837 if (!strlen_fn || !strcpy_fn)
11838 return NULL_TREE;
11840 /* If we don't have a movstr we don't want to emit an strcpy
11841 call. We have to do that if the length of the source string
11842 isn't computable (in that case we can use memcpy probably
11843 later expanding to a sequence of mov instructions). If we
11844 have movstr instructions we can emit strcpy calls. */
11845 if (!HAVE_movstr)
11847 tree len = c_strlen (src, 1);
11848 if (! len || TREE_SIDE_EFFECTS (len))
11849 return NULL_TREE;
11852 /* Stabilize the argument list. */
11853 dst = builtin_save_expr (dst);
11855 /* Create strlen (dst). */
11856 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11857 /* Create (dst p+ strlen (dst)). */
11859 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11860 newdst = builtin_save_expr (newdst);
11862 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11863 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11865 return NULL_TREE;
11869 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11870 arguments to the call.
11872 Return NULL_TREE if no simplification was possible, otherwise return the
11873 simplified form of the call as a tree.
11875 The simplified form may be a constant or other expression which
11876 computes the same value, but in a more efficient manner (including
11877 calls to other builtin functions).
11879 The call may contain arguments which need to be evaluated, but
11880 which are not useful to determine the result of the call. In
11881 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11882 COMPOUND_EXPR will be an argument which must be evaluated.
11883 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11884 COMPOUND_EXPR in the chain will contain the tree for the simplified
11885 form of the builtin function call. */
11887 static tree
11888 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11890 if (!validate_arg (dst, POINTER_TYPE)
11891 || !validate_arg (src, POINTER_TYPE)
11892 || !validate_arg (len, INTEGER_TYPE))
11893 return NULL_TREE;
11894 else
11896 const char *p = c_getstr (src);
11898 /* If the requested length is zero, or the src parameter string
11899 length is zero, return the dst parameter. */
11900 if (integer_zerop (len) || (p && *p == '\0'))
11901 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11903 /* If the requested len is greater than or equal to the string
11904 length, call strcat. */
11905 if (TREE_CODE (len) == INTEGER_CST && p
11906 && compare_tree_int (len, strlen (p)) >= 0)
11908 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11910 /* If the replacement _DECL isn't initialized, don't do the
11911 transformation. */
11912 if (!fn)
11913 return NULL_TREE;
11915 return build_call_expr_loc (loc, fn, 2, dst, src);
11917 return NULL_TREE;
11921 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11922 to the call.
11924 Return NULL_TREE if no simplification was possible, otherwise return the
11925 simplified form of the call as a tree.
11927 The simplified form may be a constant or other expression which
11928 computes the same value, but in a more efficient manner (including
11929 calls to other builtin functions).
11931 The call may contain arguments which need to be evaluated, but
11932 which are not useful to determine the result of the call. In
11933 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11934 COMPOUND_EXPR will be an argument which must be evaluated.
11935 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11936 COMPOUND_EXPR in the chain will contain the tree for the simplified
11937 form of the builtin function call. */
11939 static tree
11940 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11942 if (!validate_arg (s1, POINTER_TYPE)
11943 || !validate_arg (s2, POINTER_TYPE))
11944 return NULL_TREE;
11945 else
11947 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11949 /* If both arguments are constants, evaluate at compile-time. */
11950 if (p1 && p2)
11952 const size_t r = strspn (p1, p2);
11953 return build_int_cst (size_type_node, r);
11956 /* If either argument is "", return NULL_TREE. */
11957 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11958 /* Evaluate and ignore both arguments in case either one has
11959 side-effects. */
11960 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11961 s1, s2);
11962 return NULL_TREE;
11966 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11967 to the call.
11969 Return NULL_TREE if no simplification was possible, otherwise return the
11970 simplified form of the call as a tree.
11972 The simplified form may be a constant or other expression which
11973 computes the same value, but in a more efficient manner (including
11974 calls to other builtin functions).
11976 The call may contain arguments which need to be evaluated, but
11977 which are not useful to determine the result of the call. In
11978 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11979 COMPOUND_EXPR will be an argument which must be evaluated.
11980 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11981 COMPOUND_EXPR in the chain will contain the tree for the simplified
11982 form of the builtin function call. */
11984 static tree
11985 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11987 if (!validate_arg (s1, POINTER_TYPE)
11988 || !validate_arg (s2, POINTER_TYPE))
11989 return NULL_TREE;
11990 else
11992 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11994 /* If both arguments are constants, evaluate at compile-time. */
11995 if (p1 && p2)
11997 const size_t r = strcspn (p1, p2);
11998 return build_int_cst (size_type_node, r);
12001 /* If the first argument is "", return NULL_TREE. */
12002 if (p1 && *p1 == '\0')
12004 /* Evaluate and ignore argument s2 in case it has
12005 side-effects. */
12006 return omit_one_operand_loc (loc, size_type_node,
12007 size_zero_node, s2);
12010 /* If the second argument is "", return __builtin_strlen(s1). */
12011 if (p2 && *p2 == '\0')
12013 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
12015 /* If the replacement _DECL isn't initialized, don't do the
12016 transformation. */
12017 if (!fn)
12018 return NULL_TREE;
12020 return build_call_expr_loc (loc, fn, 1, s1);
12022 return NULL_TREE;
12026 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
12027 to the call. IGNORE is true if the value returned
12028 by the builtin will be ignored. UNLOCKED is true is true if this
12029 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
12030 the known length of the string. Return NULL_TREE if no simplification
12031 was possible. */
12033 tree
12034 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12035 bool ignore, bool unlocked, tree len)
12037 /* If we're using an unlocked function, assume the other unlocked
12038 functions exist explicitly. */
12039 tree const fn_fputc = (unlocked
12040 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12041 : builtin_decl_implicit (BUILT_IN_FPUTC));
12042 tree const fn_fwrite = (unlocked
12043 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12044 : builtin_decl_implicit (BUILT_IN_FWRITE));
12046 /* If the return value is used, don't do the transformation. */
12047 if (!ignore)
12048 return NULL_TREE;
12050 /* Verify the arguments in the original call. */
12051 if (!validate_arg (arg0, POINTER_TYPE)
12052 || !validate_arg (arg1, POINTER_TYPE))
12053 return NULL_TREE;
12055 if (! len)
12056 len = c_strlen (arg0, 0);
12058 /* Get the length of the string passed to fputs. If the length
12059 can't be determined, punt. */
12060 if (!len
12061 || TREE_CODE (len) != INTEGER_CST)
12062 return NULL_TREE;
12064 switch (compare_tree_int (len, 1))
12066 case -1: /* length is 0, delete the call entirely . */
12067 return omit_one_operand_loc (loc, integer_type_node,
12068 integer_zero_node, arg1);;
12070 case 0: /* length is 1, call fputc. */
12072 const char *p = c_getstr (arg0);
12074 if (p != NULL)
12076 if (fn_fputc)
12077 return build_call_expr_loc (loc, fn_fputc, 2,
12078 build_int_cst
12079 (integer_type_node, p[0]), arg1);
12080 else
12081 return NULL_TREE;
12084 /* FALLTHROUGH */
12085 case 1: /* length is greater than 1, call fwrite. */
12087 /* If optimizing for size keep fputs. */
12088 if (optimize_function_for_size_p (cfun))
12089 return NULL_TREE;
12090 /* New argument list transforming fputs(string, stream) to
12091 fwrite(string, 1, len, stream). */
12092 if (fn_fwrite)
12093 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12094 size_one_node, len, arg1);
12095 else
12096 return NULL_TREE;
12098 default:
12099 gcc_unreachable ();
12101 return NULL_TREE;
12104 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12105 produced. False otherwise. This is done so that we don't output the error
12106 or warning twice or three times. */
12108 bool
12109 fold_builtin_next_arg (tree exp, bool va_start_p)
12111 tree fntype = TREE_TYPE (current_function_decl);
12112 int nargs = call_expr_nargs (exp);
12113 tree arg;
12114 /* There is good chance the current input_location points inside the
12115 definition of the va_start macro (perhaps on the token for
12116 builtin) in a system header, so warnings will not be emitted.
12117 Use the location in real source code. */
12118 source_location current_location =
12119 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12120 NULL);
12122 if (!stdarg_p (fntype))
12124 error ("%<va_start%> used in function with fixed args");
12125 return true;
12128 if (va_start_p)
12130 if (va_start_p && (nargs != 2))
12132 error ("wrong number of arguments to function %<va_start%>");
12133 return true;
12135 arg = CALL_EXPR_ARG (exp, 1);
12137 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12138 when we checked the arguments and if needed issued a warning. */
12139 else
12141 if (nargs == 0)
12143 /* Evidently an out of date version of <stdarg.h>; can't validate
12144 va_start's second argument, but can still work as intended. */
12145 warning_at (current_location,
12146 OPT_Wvarargs,
12147 "%<__builtin_next_arg%> called without an argument");
12148 return true;
12150 else if (nargs > 1)
12152 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12153 return true;
12155 arg = CALL_EXPR_ARG (exp, 0);
12158 if (TREE_CODE (arg) == SSA_NAME)
12159 arg = SSA_NAME_VAR (arg);
12161 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12162 or __builtin_next_arg (0) the first time we see it, after checking
12163 the arguments and if needed issuing a warning. */
12164 if (!integer_zerop (arg))
12166 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12168 /* Strip off all nops for the sake of the comparison. This
12169 is not quite the same as STRIP_NOPS. It does more.
12170 We must also strip off INDIRECT_EXPR for C++ reference
12171 parameters. */
12172 while (CONVERT_EXPR_P (arg)
12173 || TREE_CODE (arg) == INDIRECT_REF)
12174 arg = TREE_OPERAND (arg, 0);
12175 if (arg != last_parm)
12177 /* FIXME: Sometimes with the tree optimizers we can get the
12178 not the last argument even though the user used the last
12179 argument. We just warn and set the arg to be the last
12180 argument so that we will get wrong-code because of
12181 it. */
12182 warning_at (current_location,
12183 OPT_Wvarargs,
12184 "second parameter of %<va_start%> not last named argument");
12187 /* Undefined by C99 7.15.1.4p4 (va_start):
12188 "If the parameter parmN is declared with the register storage
12189 class, with a function or array type, or with a type that is
12190 not compatible with the type that results after application of
12191 the default argument promotions, the behavior is undefined."
12193 else if (DECL_REGISTER (arg))
12195 warning_at (current_location,
12196 OPT_Wvarargs,
12197 "undefined behaviour when second parameter of "
12198 "%<va_start%> is declared with %<register%> storage");
12201 /* We want to verify the second parameter just once before the tree
12202 optimizers are run and then avoid keeping it in the tree,
12203 as otherwise we could warn even for correct code like:
12204 void foo (int i, ...)
12205 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12206 if (va_start_p)
12207 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12208 else
12209 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12211 return false;
12215 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12216 ORIG may be null if this is a 2-argument call. We don't attempt to
12217 simplify calls with more than 3 arguments.
12219 Return NULL_TREE if no simplification was possible, otherwise return the
12220 simplified form of the call as a tree. If IGNORED is true, it means that
12221 the caller does not use the returned value of the function. */
12223 static tree
12224 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12225 tree orig, int ignored)
12227 tree call, retval;
12228 const char *fmt_str = NULL;
12230 /* Verify the required arguments in the original call. We deal with two
12231 types of sprintf() calls: 'sprintf (str, fmt)' and
12232 'sprintf (dest, "%s", orig)'. */
12233 if (!validate_arg (dest, POINTER_TYPE)
12234 || !validate_arg (fmt, POINTER_TYPE))
12235 return NULL_TREE;
12236 if (orig && !validate_arg (orig, POINTER_TYPE))
12237 return NULL_TREE;
12239 /* Check whether the format is a literal string constant. */
12240 fmt_str = c_getstr (fmt);
12241 if (fmt_str == NULL)
12242 return NULL_TREE;
12244 call = NULL_TREE;
12245 retval = NULL_TREE;
12247 if (!init_target_chars ())
12248 return NULL_TREE;
12250 /* If the format doesn't contain % args or %%, use strcpy. */
12251 if (strchr (fmt_str, target_percent) == NULL)
12253 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12255 if (!fn)
12256 return NULL_TREE;
12258 /* Don't optimize sprintf (buf, "abc", ptr++). */
12259 if (orig)
12260 return NULL_TREE;
12262 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12263 'format' is known to contain no % formats. */
12264 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12265 if (!ignored)
12266 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12269 /* If the format is "%s", use strcpy if the result isn't used. */
12270 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12272 tree fn;
12273 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12275 if (!fn)
12276 return NULL_TREE;
12278 /* Don't crash on sprintf (str1, "%s"). */
12279 if (!orig)
12280 return NULL_TREE;
12282 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12283 if (!ignored)
12285 retval = c_strlen (orig, 1);
12286 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12287 return NULL_TREE;
12289 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12292 if (call && retval)
12294 retval = fold_convert_loc
12295 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12296 retval);
12297 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12299 else
12300 return call;
12303 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12304 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12305 attempt to simplify calls with more than 4 arguments.
12307 Return NULL_TREE if no simplification was possible, otherwise return the
12308 simplified form of the call as a tree. If IGNORED is true, it means that
12309 the caller does not use the returned value of the function. */
12311 static tree
12312 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12313 tree orig, int ignored)
12315 tree call, retval;
12316 const char *fmt_str = NULL;
12317 unsigned HOST_WIDE_INT destlen;
12319 /* Verify the required arguments in the original call. We deal with two
12320 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12321 'snprintf (dest, cst, "%s", orig)'. */
12322 if (!validate_arg (dest, POINTER_TYPE)
12323 || !validate_arg (destsize, INTEGER_TYPE)
12324 || !validate_arg (fmt, POINTER_TYPE))
12325 return NULL_TREE;
12326 if (orig && !validate_arg (orig, POINTER_TYPE))
12327 return NULL_TREE;
12329 if (!host_integerp (destsize, 1))
12330 return NULL_TREE;
12332 /* Check whether the format is a literal string constant. */
12333 fmt_str = c_getstr (fmt);
12334 if (fmt_str == NULL)
12335 return NULL_TREE;
12337 call = NULL_TREE;
12338 retval = NULL_TREE;
12340 if (!init_target_chars ())
12341 return NULL_TREE;
12343 destlen = tree_low_cst (destsize, 1);
12345 /* If the format doesn't contain % args or %%, use strcpy. */
12346 if (strchr (fmt_str, target_percent) == NULL)
12348 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12349 size_t len = strlen (fmt_str);
12351 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12352 if (orig)
12353 return NULL_TREE;
12355 /* We could expand this as
12356 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12357 or to
12358 memcpy (str, fmt_with_nul_at_cstm1, cst);
12359 but in the former case that might increase code size
12360 and in the latter case grow .rodata section too much.
12361 So punt for now. */
12362 if (len >= destlen)
12363 return NULL_TREE;
12365 if (!fn)
12366 return NULL_TREE;
12368 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12369 'format' is known to contain no % formats and
12370 strlen (fmt) < cst. */
12371 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12373 if (!ignored)
12374 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12377 /* If the format is "%s", use strcpy if the result isn't used. */
12378 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12380 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12381 unsigned HOST_WIDE_INT origlen;
12383 /* Don't crash on snprintf (str1, cst, "%s"). */
12384 if (!orig)
12385 return NULL_TREE;
12387 retval = c_strlen (orig, 1);
12388 if (!retval || !host_integerp (retval, 1))
12389 return NULL_TREE;
12391 origlen = tree_low_cst (retval, 1);
12392 /* We could expand this as
12393 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12394 or to
12395 memcpy (str1, str2_with_nul_at_cstm1, cst);
12396 but in the former case that might increase code size
12397 and in the latter case grow .rodata section too much.
12398 So punt for now. */
12399 if (origlen >= destlen)
12400 return NULL_TREE;
12402 /* Convert snprintf (str1, cst, "%s", str2) into
12403 strcpy (str1, str2) if strlen (str2) < cst. */
12404 if (!fn)
12405 return NULL_TREE;
12407 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12409 if (ignored)
12410 retval = NULL_TREE;
12413 if (call && retval)
12415 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12416 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12417 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12419 else
12420 return call;
12423 /* Expand a call EXP to __builtin_object_size. */
12426 expand_builtin_object_size (tree exp)
12428 tree ost;
12429 int object_size_type;
12430 tree fndecl = get_callee_fndecl (exp);
12432 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12434 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12435 exp, fndecl);
12436 expand_builtin_trap ();
12437 return const0_rtx;
12440 ost = CALL_EXPR_ARG (exp, 1);
12441 STRIP_NOPS (ost);
12443 if (TREE_CODE (ost) != INTEGER_CST
12444 || tree_int_cst_sgn (ost) < 0
12445 || compare_tree_int (ost, 3) > 0)
12447 error ("%Klast argument of %D is not integer constant between 0 and 3",
12448 exp, fndecl);
12449 expand_builtin_trap ();
12450 return const0_rtx;
12453 object_size_type = tree_low_cst (ost, 0);
12455 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12458 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12459 FCODE is the BUILT_IN_* to use.
12460 Return NULL_RTX if we failed; the caller should emit a normal call,
12461 otherwise try to get the result in TARGET, if convenient (and in
12462 mode MODE if that's convenient). */
12464 static rtx
12465 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12466 enum built_in_function fcode)
12468 tree dest, src, len, size;
12470 if (!validate_arglist (exp,
12471 POINTER_TYPE,
12472 fcode == BUILT_IN_MEMSET_CHK
12473 ? INTEGER_TYPE : POINTER_TYPE,
12474 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12475 return NULL_RTX;
12477 dest = CALL_EXPR_ARG (exp, 0);
12478 src = CALL_EXPR_ARG (exp, 1);
12479 len = CALL_EXPR_ARG (exp, 2);
12480 size = CALL_EXPR_ARG (exp, 3);
12482 if (! host_integerp (size, 1))
12483 return NULL_RTX;
12485 if (host_integerp (len, 1) || integer_all_onesp (size))
12487 tree fn;
12489 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12491 warning_at (tree_nonartificial_location (exp),
12492 0, "%Kcall to %D will always overflow destination buffer",
12493 exp, get_callee_fndecl (exp));
12494 return NULL_RTX;
12497 fn = NULL_TREE;
12498 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12499 mem{cpy,pcpy,move,set} is available. */
12500 switch (fcode)
12502 case BUILT_IN_MEMCPY_CHK:
12503 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12504 break;
12505 case BUILT_IN_MEMPCPY_CHK:
12506 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12507 break;
12508 case BUILT_IN_MEMMOVE_CHK:
12509 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12510 break;
12511 case BUILT_IN_MEMSET_CHK:
12512 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12513 break;
12514 default:
12515 break;
12518 if (! fn)
12519 return NULL_RTX;
12521 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12522 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12523 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12524 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12526 else if (fcode == BUILT_IN_MEMSET_CHK)
12527 return NULL_RTX;
12528 else
12530 unsigned int dest_align = get_pointer_alignment (dest);
12532 /* If DEST is not a pointer type, call the normal function. */
12533 if (dest_align == 0)
12534 return NULL_RTX;
12536 /* If SRC and DEST are the same (and not volatile), do nothing. */
12537 if (operand_equal_p (src, dest, 0))
12539 tree expr;
12541 if (fcode != BUILT_IN_MEMPCPY_CHK)
12543 /* Evaluate and ignore LEN in case it has side-effects. */
12544 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12545 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12548 expr = fold_build_pointer_plus (dest, len);
12549 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12552 /* __memmove_chk special case. */
12553 if (fcode == BUILT_IN_MEMMOVE_CHK)
12555 unsigned int src_align = get_pointer_alignment (src);
12557 if (src_align == 0)
12558 return NULL_RTX;
12560 /* If src is categorized for a readonly section we can use
12561 normal __memcpy_chk. */
12562 if (readonly_data_expr (src))
12564 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12565 if (!fn)
12566 return NULL_RTX;
12567 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12568 dest, src, len, size);
12569 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12570 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12571 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12574 return NULL_RTX;
12578 /* Emit warning if a buffer overflow is detected at compile time. */
12580 static void
12581 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12583 int is_strlen = 0;
12584 tree len, size;
12585 location_t loc = tree_nonartificial_location (exp);
12587 switch (fcode)
12589 case BUILT_IN_STRCPY_CHK:
12590 case BUILT_IN_STPCPY_CHK:
12591 /* For __strcat_chk the warning will be emitted only if overflowing
12592 by at least strlen (dest) + 1 bytes. */
12593 case BUILT_IN_STRCAT_CHK:
12594 len = CALL_EXPR_ARG (exp, 1);
12595 size = CALL_EXPR_ARG (exp, 2);
12596 is_strlen = 1;
12597 break;
12598 case BUILT_IN_STRNCAT_CHK:
12599 case BUILT_IN_STRNCPY_CHK:
12600 case BUILT_IN_STPNCPY_CHK:
12601 len = CALL_EXPR_ARG (exp, 2);
12602 size = CALL_EXPR_ARG (exp, 3);
12603 break;
12604 case BUILT_IN_SNPRINTF_CHK:
12605 case BUILT_IN_VSNPRINTF_CHK:
12606 len = CALL_EXPR_ARG (exp, 1);
12607 size = CALL_EXPR_ARG (exp, 3);
12608 break;
12609 default:
12610 gcc_unreachable ();
12613 if (!len || !size)
12614 return;
12616 if (! host_integerp (size, 1) || integer_all_onesp (size))
12617 return;
12619 if (is_strlen)
12621 len = c_strlen (len, 1);
12622 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12623 return;
12625 else if (fcode == BUILT_IN_STRNCAT_CHK)
12627 tree src = CALL_EXPR_ARG (exp, 1);
12628 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12629 return;
12630 src = c_strlen (src, 1);
12631 if (! src || ! host_integerp (src, 1))
12633 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12634 exp, get_callee_fndecl (exp));
12635 return;
12637 else if (tree_int_cst_lt (src, size))
12638 return;
12640 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12641 return;
12643 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12644 exp, get_callee_fndecl (exp));
12647 /* Emit warning if a buffer overflow is detected at compile time
12648 in __sprintf_chk/__vsprintf_chk calls. */
12650 static void
12651 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12653 tree size, len, fmt;
12654 const char *fmt_str;
12655 int nargs = call_expr_nargs (exp);
12657 /* Verify the required arguments in the original call. */
12659 if (nargs < 4)
12660 return;
12661 size = CALL_EXPR_ARG (exp, 2);
12662 fmt = CALL_EXPR_ARG (exp, 3);
12664 if (! host_integerp (size, 1) || integer_all_onesp (size))
12665 return;
12667 /* Check whether the format is a literal string constant. */
12668 fmt_str = c_getstr (fmt);
12669 if (fmt_str == NULL)
12670 return;
12672 if (!init_target_chars ())
12673 return;
12675 /* If the format doesn't contain % args or %%, we know its size. */
12676 if (strchr (fmt_str, target_percent) == 0)
12677 len = build_int_cstu (size_type_node, strlen (fmt_str));
12678 /* If the format is "%s" and first ... argument is a string literal,
12679 we know it too. */
12680 else if (fcode == BUILT_IN_SPRINTF_CHK
12681 && strcmp (fmt_str, target_percent_s) == 0)
12683 tree arg;
12685 if (nargs < 5)
12686 return;
12687 arg = CALL_EXPR_ARG (exp, 4);
12688 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12689 return;
12691 len = c_strlen (arg, 1);
12692 if (!len || ! host_integerp (len, 1))
12693 return;
12695 else
12696 return;
12698 if (! tree_int_cst_lt (len, size))
12699 warning_at (tree_nonartificial_location (exp),
12700 0, "%Kcall to %D will always overflow destination buffer",
12701 exp, get_callee_fndecl (exp));
12704 /* Emit warning if a free is called with address of a variable. */
12706 static void
12707 maybe_emit_free_warning (tree exp)
12709 tree arg = CALL_EXPR_ARG (exp, 0);
12711 STRIP_NOPS (arg);
12712 if (TREE_CODE (arg) != ADDR_EXPR)
12713 return;
12715 arg = get_base_address (TREE_OPERAND (arg, 0));
12716 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12717 return;
12719 if (SSA_VAR_P (arg))
12720 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12721 "%Kattempt to free a non-heap object %qD", exp, arg);
12722 else
12723 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12724 "%Kattempt to free a non-heap object", exp);
12727 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12728 if possible. */
12730 tree
12731 fold_builtin_object_size (tree ptr, tree ost)
12733 unsigned HOST_WIDE_INT bytes;
12734 int object_size_type;
12736 if (!validate_arg (ptr, POINTER_TYPE)
12737 || !validate_arg (ost, INTEGER_TYPE))
12738 return NULL_TREE;
12740 STRIP_NOPS (ost);
12742 if (TREE_CODE (ost) != INTEGER_CST
12743 || tree_int_cst_sgn (ost) < 0
12744 || compare_tree_int (ost, 3) > 0)
12745 return NULL_TREE;
12747 object_size_type = tree_low_cst (ost, 0);
12749 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12750 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12751 and (size_t) 0 for types 2 and 3. */
12752 if (TREE_SIDE_EFFECTS (ptr))
12753 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12755 if (TREE_CODE (ptr) == ADDR_EXPR)
12757 bytes = compute_builtin_object_size (ptr, object_size_type);
12758 if (double_int_fits_to_tree_p (size_type_node,
12759 double_int::from_uhwi (bytes)))
12760 return build_int_cstu (size_type_node, bytes);
12762 else if (TREE_CODE (ptr) == SSA_NAME)
12764 /* If object size is not known yet, delay folding until
12765 later. Maybe subsequent passes will help determining
12766 it. */
12767 bytes = compute_builtin_object_size (ptr, object_size_type);
12768 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12769 && double_int_fits_to_tree_p (size_type_node,
12770 double_int::from_uhwi (bytes)))
12771 return build_int_cstu (size_type_node, bytes);
12774 return NULL_TREE;
12777 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12778 DEST, SRC, LEN, and SIZE are the arguments to the call.
12779 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12780 code of the builtin. If MAXLEN is not NULL, it is maximum length
12781 passed as third argument. */
12783 tree
12784 fold_builtin_memory_chk (location_t loc, tree fndecl,
12785 tree dest, tree src, tree len, tree size,
12786 tree maxlen, bool ignore,
12787 enum built_in_function fcode)
12789 tree fn;
12791 if (!validate_arg (dest, POINTER_TYPE)
12792 || !validate_arg (src,
12793 (fcode == BUILT_IN_MEMSET_CHK
12794 ? INTEGER_TYPE : POINTER_TYPE))
12795 || !validate_arg (len, INTEGER_TYPE)
12796 || !validate_arg (size, INTEGER_TYPE))
12797 return NULL_TREE;
12799 /* If SRC and DEST are the same (and not volatile), return DEST
12800 (resp. DEST+LEN for __mempcpy_chk). */
12801 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12803 if (fcode != BUILT_IN_MEMPCPY_CHK)
12804 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12805 dest, len);
12806 else
12808 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12809 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12813 if (! host_integerp (size, 1))
12814 return NULL_TREE;
12816 if (! integer_all_onesp (size))
12818 if (! host_integerp (len, 1))
12820 /* If LEN is not constant, try MAXLEN too.
12821 For MAXLEN only allow optimizing into non-_ocs function
12822 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12823 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12825 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12827 /* (void) __mempcpy_chk () can be optimized into
12828 (void) __memcpy_chk (). */
12829 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12830 if (!fn)
12831 return NULL_TREE;
12833 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12835 return NULL_TREE;
12838 else
12839 maxlen = len;
12841 if (tree_int_cst_lt (size, maxlen))
12842 return NULL_TREE;
12845 fn = NULL_TREE;
12846 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12847 mem{cpy,pcpy,move,set} is available. */
12848 switch (fcode)
12850 case BUILT_IN_MEMCPY_CHK:
12851 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12852 break;
12853 case BUILT_IN_MEMPCPY_CHK:
12854 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12855 break;
12856 case BUILT_IN_MEMMOVE_CHK:
12857 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12858 break;
12859 case BUILT_IN_MEMSET_CHK:
12860 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12861 break;
12862 default:
12863 break;
12866 if (!fn)
12867 return NULL_TREE;
12869 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12872 /* Fold a call to the __st[rp]cpy_chk builtin.
12873 DEST, SRC, and SIZE are the arguments to the call.
12874 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12875 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12876 strings passed as second argument. */
12878 tree
12879 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12880 tree src, tree size,
12881 tree maxlen, bool ignore,
12882 enum built_in_function fcode)
12884 tree len, fn;
12886 if (!validate_arg (dest, POINTER_TYPE)
12887 || !validate_arg (src, POINTER_TYPE)
12888 || !validate_arg (size, INTEGER_TYPE))
12889 return NULL_TREE;
12891 /* If SRC and DEST are the same (and not volatile), return DEST. */
12892 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12893 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12895 if (! host_integerp (size, 1))
12896 return NULL_TREE;
12898 if (! integer_all_onesp (size))
12900 len = c_strlen (src, 1);
12901 if (! len || ! host_integerp (len, 1))
12903 /* If LEN is not constant, try MAXLEN too.
12904 For MAXLEN only allow optimizing into non-_ocs function
12905 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12906 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12908 if (fcode == BUILT_IN_STPCPY_CHK)
12910 if (! ignore)
12911 return NULL_TREE;
12913 /* If return value of __stpcpy_chk is ignored,
12914 optimize into __strcpy_chk. */
12915 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12916 if (!fn)
12917 return NULL_TREE;
12919 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12922 if (! len || TREE_SIDE_EFFECTS (len))
12923 return NULL_TREE;
12925 /* If c_strlen returned something, but not a constant,
12926 transform __strcpy_chk into __memcpy_chk. */
12927 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12928 if (!fn)
12929 return NULL_TREE;
12931 len = fold_convert_loc (loc, size_type_node, len);
12932 len = size_binop_loc (loc, PLUS_EXPR, len,
12933 build_int_cst (size_type_node, 1));
12934 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12935 build_call_expr_loc (loc, fn, 4,
12936 dest, src, len, size));
12939 else
12940 maxlen = len;
12942 if (! tree_int_cst_lt (maxlen, size))
12943 return NULL_TREE;
12946 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12947 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12948 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12949 if (!fn)
12950 return NULL_TREE;
12952 return build_call_expr_loc (loc, fn, 2, dest, src);
12955 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12956 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12957 length passed as third argument. IGNORE is true if return value can be
12958 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12960 tree
12961 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12962 tree len, tree size, tree maxlen, bool ignore,
12963 enum built_in_function fcode)
12965 tree fn;
12967 if (!validate_arg (dest, POINTER_TYPE)
12968 || !validate_arg (src, POINTER_TYPE)
12969 || !validate_arg (len, INTEGER_TYPE)
12970 || !validate_arg (size, INTEGER_TYPE))
12971 return NULL_TREE;
12973 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12975 /* If return value of __stpncpy_chk is ignored,
12976 optimize into __strncpy_chk. */
12977 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12978 if (fn)
12979 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12982 if (! host_integerp (size, 1))
12983 return NULL_TREE;
12985 if (! integer_all_onesp (size))
12987 if (! host_integerp (len, 1))
12989 /* If LEN is not constant, try MAXLEN too.
12990 For MAXLEN only allow optimizing into non-_ocs function
12991 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12992 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12993 return NULL_TREE;
12995 else
12996 maxlen = len;
12998 if (tree_int_cst_lt (size, maxlen))
12999 return NULL_TREE;
13002 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
13003 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
13004 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
13005 if (!fn)
13006 return NULL_TREE;
13008 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13011 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
13012 are the arguments to the call. */
13014 static tree
13015 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
13016 tree src, tree size)
13018 tree fn;
13019 const char *p;
13021 if (!validate_arg (dest, POINTER_TYPE)
13022 || !validate_arg (src, POINTER_TYPE)
13023 || !validate_arg (size, INTEGER_TYPE))
13024 return NULL_TREE;
13026 p = c_getstr (src);
13027 /* If the SRC parameter is "", return DEST. */
13028 if (p && *p == '\0')
13029 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13031 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
13032 return NULL_TREE;
13034 /* If __builtin_strcat_chk is used, assume strcat is available. */
13035 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
13036 if (!fn)
13037 return NULL_TREE;
13039 return build_call_expr_loc (loc, fn, 2, dest, src);
13042 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13043 LEN, and SIZE. */
13045 static tree
13046 fold_builtin_strncat_chk (location_t loc, tree fndecl,
13047 tree dest, tree src, tree len, tree size)
13049 tree fn;
13050 const char *p;
13052 if (!validate_arg (dest, POINTER_TYPE)
13053 || !validate_arg (src, POINTER_TYPE)
13054 || !validate_arg (size, INTEGER_TYPE)
13055 || !validate_arg (size, INTEGER_TYPE))
13056 return NULL_TREE;
13058 p = c_getstr (src);
13059 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13060 if (p && *p == '\0')
13061 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13062 else if (integer_zerop (len))
13063 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13065 if (! host_integerp (size, 1))
13066 return NULL_TREE;
13068 if (! integer_all_onesp (size))
13070 tree src_len = c_strlen (src, 1);
13071 if (src_len
13072 && host_integerp (src_len, 1)
13073 && host_integerp (len, 1)
13074 && ! tree_int_cst_lt (len, src_len))
13076 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13077 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13078 if (!fn)
13079 return NULL_TREE;
13081 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13083 return NULL_TREE;
13086 /* If __builtin_strncat_chk is used, assume strncat is available. */
13087 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13088 if (!fn)
13089 return NULL_TREE;
13091 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13094 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13095 Return NULL_TREE if a normal call should be emitted rather than
13096 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13097 or BUILT_IN_VSPRINTF_CHK. */
13099 static tree
13100 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13101 enum built_in_function fcode)
13103 tree dest, size, len, fn, fmt, flag;
13104 const char *fmt_str;
13106 /* Verify the required arguments in the original call. */
13107 if (nargs < 4)
13108 return NULL_TREE;
13109 dest = args[0];
13110 if (!validate_arg (dest, POINTER_TYPE))
13111 return NULL_TREE;
13112 flag = args[1];
13113 if (!validate_arg (flag, INTEGER_TYPE))
13114 return NULL_TREE;
13115 size = args[2];
13116 if (!validate_arg (size, INTEGER_TYPE))
13117 return NULL_TREE;
13118 fmt = args[3];
13119 if (!validate_arg (fmt, POINTER_TYPE))
13120 return NULL_TREE;
13122 if (! host_integerp (size, 1))
13123 return NULL_TREE;
13125 len = NULL_TREE;
13127 if (!init_target_chars ())
13128 return NULL_TREE;
13130 /* Check whether the format is a literal string constant. */
13131 fmt_str = c_getstr (fmt);
13132 if (fmt_str != NULL)
13134 /* If the format doesn't contain % args or %%, we know the size. */
13135 if (strchr (fmt_str, target_percent) == 0)
13137 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13138 len = build_int_cstu (size_type_node, strlen (fmt_str));
13140 /* If the format is "%s" and first ... argument is a string literal,
13141 we know the size too. */
13142 else if (fcode == BUILT_IN_SPRINTF_CHK
13143 && strcmp (fmt_str, target_percent_s) == 0)
13145 tree arg;
13147 if (nargs == 5)
13149 arg = args[4];
13150 if (validate_arg (arg, POINTER_TYPE))
13152 len = c_strlen (arg, 1);
13153 if (! len || ! host_integerp (len, 1))
13154 len = NULL_TREE;
13160 if (! integer_all_onesp (size))
13162 if (! len || ! tree_int_cst_lt (len, size))
13163 return NULL_TREE;
13166 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13167 or if format doesn't contain % chars or is "%s". */
13168 if (! integer_zerop (flag))
13170 if (fmt_str == NULL)
13171 return NULL_TREE;
13172 if (strchr (fmt_str, target_percent) != NULL
13173 && strcmp (fmt_str, target_percent_s))
13174 return NULL_TREE;
13177 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13178 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13179 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13180 if (!fn)
13181 return NULL_TREE;
13183 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13186 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13187 a normal call should be emitted rather than expanding the function
13188 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13190 static tree
13191 fold_builtin_sprintf_chk (location_t loc, tree exp,
13192 enum built_in_function fcode)
13194 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13195 CALL_EXPR_ARGP (exp), fcode);
13198 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13199 NULL_TREE if a normal call should be emitted rather than expanding
13200 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13201 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13202 passed as second argument. */
13204 static tree
13205 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13206 tree maxlen, enum built_in_function fcode)
13208 tree dest, size, len, fn, fmt, flag;
13209 const char *fmt_str;
13211 /* Verify the required arguments in the original call. */
13212 if (nargs < 5)
13213 return NULL_TREE;
13214 dest = args[0];
13215 if (!validate_arg (dest, POINTER_TYPE))
13216 return NULL_TREE;
13217 len = args[1];
13218 if (!validate_arg (len, INTEGER_TYPE))
13219 return NULL_TREE;
13220 flag = args[2];
13221 if (!validate_arg (flag, INTEGER_TYPE))
13222 return NULL_TREE;
13223 size = args[3];
13224 if (!validate_arg (size, INTEGER_TYPE))
13225 return NULL_TREE;
13226 fmt = args[4];
13227 if (!validate_arg (fmt, POINTER_TYPE))
13228 return NULL_TREE;
13230 if (! host_integerp (size, 1))
13231 return NULL_TREE;
13233 if (! integer_all_onesp (size))
13235 if (! host_integerp (len, 1))
13237 /* If LEN is not constant, try MAXLEN too.
13238 For MAXLEN only allow optimizing into non-_ocs function
13239 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13240 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13241 return NULL_TREE;
13243 else
13244 maxlen = len;
13246 if (tree_int_cst_lt (size, maxlen))
13247 return NULL_TREE;
13250 if (!init_target_chars ())
13251 return NULL_TREE;
13253 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13254 or if format doesn't contain % chars or is "%s". */
13255 if (! integer_zerop (flag))
13257 fmt_str = c_getstr (fmt);
13258 if (fmt_str == NULL)
13259 return NULL_TREE;
13260 if (strchr (fmt_str, target_percent) != NULL
13261 && strcmp (fmt_str, target_percent_s))
13262 return NULL_TREE;
13265 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13266 available. */
13267 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13268 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13269 if (!fn)
13270 return NULL_TREE;
13272 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13275 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13276 a normal call should be emitted rather than expanding the function
13277 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13278 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13279 passed as second argument. */
13281 tree
13282 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13283 enum built_in_function fcode)
13285 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13286 CALL_EXPR_ARGP (exp), maxlen, fcode);
13289 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13290 FMT and ARG are the arguments to the call; we don't fold cases with
13291 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13293 Return NULL_TREE if no simplification was possible, otherwise return the
13294 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13295 code of the function to be simplified. */
13297 static tree
13298 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13299 tree arg, bool ignore,
13300 enum built_in_function fcode)
13302 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13303 const char *fmt_str = NULL;
13305 /* If the return value is used, don't do the transformation. */
13306 if (! ignore)
13307 return NULL_TREE;
13309 /* Verify the required arguments in the original call. */
13310 if (!validate_arg (fmt, POINTER_TYPE))
13311 return NULL_TREE;
13313 /* Check whether the format is a literal string constant. */
13314 fmt_str = c_getstr (fmt);
13315 if (fmt_str == NULL)
13316 return NULL_TREE;
13318 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13320 /* If we're using an unlocked function, assume the other
13321 unlocked functions exist explicitly. */
13322 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13323 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13325 else
13327 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13328 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13331 if (!init_target_chars ())
13332 return NULL_TREE;
13334 if (strcmp (fmt_str, target_percent_s) == 0
13335 || strchr (fmt_str, target_percent) == NULL)
13337 const char *str;
13339 if (strcmp (fmt_str, target_percent_s) == 0)
13341 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13342 return NULL_TREE;
13344 if (!arg || !validate_arg (arg, POINTER_TYPE))
13345 return NULL_TREE;
13347 str = c_getstr (arg);
13348 if (str == NULL)
13349 return NULL_TREE;
13351 else
13353 /* The format specifier doesn't contain any '%' characters. */
13354 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13355 && arg)
13356 return NULL_TREE;
13357 str = fmt_str;
13360 /* If the string was "", printf does nothing. */
13361 if (str[0] == '\0')
13362 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13364 /* If the string has length of 1, call putchar. */
13365 if (str[1] == '\0')
13367 /* Given printf("c"), (where c is any one character,)
13368 convert "c"[0] to an int and pass that to the replacement
13369 function. */
13370 newarg = build_int_cst (integer_type_node, str[0]);
13371 if (fn_putchar)
13372 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13374 else
13376 /* If the string was "string\n", call puts("string"). */
13377 size_t len = strlen (str);
13378 if ((unsigned char)str[len - 1] == target_newline
13379 && (size_t) (int) len == len
13380 && (int) len > 0)
13382 char *newstr;
13383 tree offset_node, string_cst;
13385 /* Create a NUL-terminated string that's one char shorter
13386 than the original, stripping off the trailing '\n'. */
13387 newarg = build_string_literal (len, str);
13388 string_cst = string_constant (newarg, &offset_node);
13389 gcc_checking_assert (string_cst
13390 && (TREE_STRING_LENGTH (string_cst)
13391 == (int) len)
13392 && integer_zerop (offset_node)
13393 && (unsigned char)
13394 TREE_STRING_POINTER (string_cst)[len - 1]
13395 == target_newline);
13396 /* build_string_literal creates a new STRING_CST,
13397 modify it in place to avoid double copying. */
13398 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13399 newstr[len - 1] = '\0';
13400 if (fn_puts)
13401 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13403 else
13404 /* We'd like to arrange to call fputs(string,stdout) here,
13405 but we need stdout and don't have a way to get it yet. */
13406 return NULL_TREE;
13410 /* The other optimizations can be done only on the non-va_list variants. */
13411 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13412 return NULL_TREE;
13414 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13415 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13417 if (!arg || !validate_arg (arg, POINTER_TYPE))
13418 return NULL_TREE;
13419 if (fn_puts)
13420 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13423 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13424 else if (strcmp (fmt_str, target_percent_c) == 0)
13426 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13427 return NULL_TREE;
13428 if (fn_putchar)
13429 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13432 if (!call)
13433 return NULL_TREE;
13435 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13438 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13439 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13440 more than 3 arguments, and ARG may be null in the 2-argument case.
13442 Return NULL_TREE if no simplification was possible, otherwise return the
13443 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13444 code of the function to be simplified. */
13446 static tree
13447 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13448 tree fmt, tree arg, bool ignore,
13449 enum built_in_function fcode)
13451 tree fn_fputc, fn_fputs, call = NULL_TREE;
13452 const char *fmt_str = NULL;
13454 /* If the return value is used, don't do the transformation. */
13455 if (! ignore)
13456 return NULL_TREE;
13458 /* Verify the required arguments in the original call. */
13459 if (!validate_arg (fp, POINTER_TYPE))
13460 return NULL_TREE;
13461 if (!validate_arg (fmt, POINTER_TYPE))
13462 return NULL_TREE;
13464 /* Check whether the format is a literal string constant. */
13465 fmt_str = c_getstr (fmt);
13466 if (fmt_str == NULL)
13467 return NULL_TREE;
13469 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13471 /* If we're using an unlocked function, assume the other
13472 unlocked functions exist explicitly. */
13473 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13474 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13476 else
13478 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13479 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13482 if (!init_target_chars ())
13483 return NULL_TREE;
13485 /* If the format doesn't contain % args or %%, use strcpy. */
13486 if (strchr (fmt_str, target_percent) == NULL)
13488 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13489 && arg)
13490 return NULL_TREE;
13492 /* If the format specifier was "", fprintf does nothing. */
13493 if (fmt_str[0] == '\0')
13495 /* If FP has side-effects, just wait until gimplification is
13496 done. */
13497 if (TREE_SIDE_EFFECTS (fp))
13498 return NULL_TREE;
13500 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13503 /* When "string" doesn't contain %, replace all cases of
13504 fprintf (fp, string) with fputs (string, fp). The fputs
13505 builtin will take care of special cases like length == 1. */
13506 if (fn_fputs)
13507 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13510 /* The other optimizations can be done only on the non-va_list variants. */
13511 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13512 return NULL_TREE;
13514 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13515 else if (strcmp (fmt_str, target_percent_s) == 0)
13517 if (!arg || !validate_arg (arg, POINTER_TYPE))
13518 return NULL_TREE;
13519 if (fn_fputs)
13520 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13523 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13524 else if (strcmp (fmt_str, target_percent_c) == 0)
13526 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13527 return NULL_TREE;
13528 if (fn_fputc)
13529 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13532 if (!call)
13533 return NULL_TREE;
13534 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13537 /* Initialize format string characters in the target charset. */
13539 static bool
13540 init_target_chars (void)
13542 static bool init;
13543 if (!init)
13545 target_newline = lang_hooks.to_target_charset ('\n');
13546 target_percent = lang_hooks.to_target_charset ('%');
13547 target_c = lang_hooks.to_target_charset ('c');
13548 target_s = lang_hooks.to_target_charset ('s');
13549 if (target_newline == 0 || target_percent == 0 || target_c == 0
13550 || target_s == 0)
13551 return false;
13553 target_percent_c[0] = target_percent;
13554 target_percent_c[1] = target_c;
13555 target_percent_c[2] = '\0';
13557 target_percent_s[0] = target_percent;
13558 target_percent_s[1] = target_s;
13559 target_percent_s[2] = '\0';
13561 target_percent_s_newline[0] = target_percent;
13562 target_percent_s_newline[1] = target_s;
13563 target_percent_s_newline[2] = target_newline;
13564 target_percent_s_newline[3] = '\0';
13566 init = true;
13568 return true;
13571 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13572 and no overflow/underflow occurred. INEXACT is true if M was not
13573 exactly calculated. TYPE is the tree type for the result. This
13574 function assumes that you cleared the MPFR flags and then
13575 calculated M to see if anything subsequently set a flag prior to
13576 entering this function. Return NULL_TREE if any checks fail. */
13578 static tree
13579 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13581 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13582 overflow/underflow occurred. If -frounding-math, proceed iff the
13583 result of calling FUNC was exact. */
13584 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13585 && (!flag_rounding_math || !inexact))
13587 REAL_VALUE_TYPE rr;
13589 real_from_mpfr (&rr, m, type, GMP_RNDN);
13590 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13591 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13592 but the mpft_t is not, then we underflowed in the
13593 conversion. */
13594 if (real_isfinite (&rr)
13595 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13597 REAL_VALUE_TYPE rmode;
13599 real_convert (&rmode, TYPE_MODE (type), &rr);
13600 /* Proceed iff the specified mode can hold the value. */
13601 if (real_identical (&rmode, &rr))
13602 return build_real (type, rmode);
13605 return NULL_TREE;
13608 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13609 number and no overflow/underflow occurred. INEXACT is true if M
13610 was not exactly calculated. TYPE is the tree type for the result.
13611 This function assumes that you cleared the MPFR flags and then
13612 calculated M to see if anything subsequently set a flag prior to
13613 entering this function. Return NULL_TREE if any checks fail, if
13614 FORCE_CONVERT is true, then bypass the checks. */
13616 static tree
13617 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13619 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13620 overflow/underflow occurred. If -frounding-math, proceed iff the
13621 result of calling FUNC was exact. */
13622 if (force_convert
13623 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13624 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13625 && (!flag_rounding_math || !inexact)))
13627 REAL_VALUE_TYPE re, im;
13629 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13630 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13631 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13632 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13633 but the mpft_t is not, then we underflowed in the
13634 conversion. */
13635 if (force_convert
13636 || (real_isfinite (&re) && real_isfinite (&im)
13637 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13638 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13640 REAL_VALUE_TYPE re_mode, im_mode;
13642 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13643 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13644 /* Proceed iff the specified mode can hold the value. */
13645 if (force_convert
13646 || (real_identical (&re_mode, &re)
13647 && real_identical (&im_mode, &im)))
13648 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13649 build_real (TREE_TYPE (type), im_mode));
13652 return NULL_TREE;
13655 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13656 FUNC on it and return the resulting value as a tree with type TYPE.
13657 If MIN and/or MAX are not NULL, then the supplied ARG must be
13658 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13659 acceptable values, otherwise they are not. The mpfr precision is
13660 set to the precision of TYPE. We assume that function FUNC returns
13661 zero if the result could be calculated exactly within the requested
13662 precision. */
13664 static tree
13665 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13666 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13667 bool inclusive)
13669 tree result = NULL_TREE;
13671 STRIP_NOPS (arg);
13673 /* To proceed, MPFR must exactly represent the target floating point
13674 format, which only happens when the target base equals two. */
13675 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13676 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13678 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13680 if (real_isfinite (ra)
13681 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13682 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13684 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13685 const int prec = fmt->p;
13686 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13687 int inexact;
13688 mpfr_t m;
13690 mpfr_init2 (m, prec);
13691 mpfr_from_real (m, ra, GMP_RNDN);
13692 mpfr_clear_flags ();
13693 inexact = func (m, m, rnd);
13694 result = do_mpfr_ckconv (m, type, inexact);
13695 mpfr_clear (m);
13699 return result;
13702 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13703 FUNC on it and return the resulting value as a tree with type TYPE.
13704 The mpfr precision is set to the precision of TYPE. We assume that
13705 function FUNC returns zero if the result could be calculated
13706 exactly within the requested precision. */
13708 static tree
13709 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13710 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13712 tree result = NULL_TREE;
13714 STRIP_NOPS (arg1);
13715 STRIP_NOPS (arg2);
13717 /* To proceed, MPFR must exactly represent the target floating point
13718 format, which only happens when the target base equals two. */
13719 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13720 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13721 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13723 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13724 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13726 if (real_isfinite (ra1) && real_isfinite (ra2))
13728 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13729 const int prec = fmt->p;
13730 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13731 int inexact;
13732 mpfr_t m1, m2;
13734 mpfr_inits2 (prec, m1, m2, NULL);
13735 mpfr_from_real (m1, ra1, GMP_RNDN);
13736 mpfr_from_real (m2, ra2, GMP_RNDN);
13737 mpfr_clear_flags ();
13738 inexact = func (m1, m1, m2, rnd);
13739 result = do_mpfr_ckconv (m1, type, inexact);
13740 mpfr_clears (m1, m2, NULL);
13744 return result;
13747 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13748 FUNC on it and return the resulting value as a tree with type TYPE.
13749 The mpfr precision is set to the precision of TYPE. We assume that
13750 function FUNC returns zero if the result could be calculated
13751 exactly within the requested precision. */
13753 static tree
13754 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13755 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13757 tree result = NULL_TREE;
13759 STRIP_NOPS (arg1);
13760 STRIP_NOPS (arg2);
13761 STRIP_NOPS (arg3);
13763 /* To proceed, MPFR must exactly represent the target floating point
13764 format, which only happens when the target base equals two. */
13765 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13766 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13767 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13768 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13770 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13771 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13772 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13774 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13776 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13777 const int prec = fmt->p;
13778 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13779 int inexact;
13780 mpfr_t m1, m2, m3;
13782 mpfr_inits2 (prec, m1, m2, m3, NULL);
13783 mpfr_from_real (m1, ra1, GMP_RNDN);
13784 mpfr_from_real (m2, ra2, GMP_RNDN);
13785 mpfr_from_real (m3, ra3, GMP_RNDN);
13786 mpfr_clear_flags ();
13787 inexact = func (m1, m1, m2, m3, rnd);
13788 result = do_mpfr_ckconv (m1, type, inexact);
13789 mpfr_clears (m1, m2, m3, NULL);
13793 return result;
13796 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13797 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13798 If ARG_SINP and ARG_COSP are NULL then the result is returned
13799 as a complex value.
13800 The type is taken from the type of ARG and is used for setting the
13801 precision of the calculation and results. */
13803 static tree
13804 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13806 tree const type = TREE_TYPE (arg);
13807 tree result = NULL_TREE;
13809 STRIP_NOPS (arg);
13811 /* To proceed, MPFR must exactly represent the target floating point
13812 format, which only happens when the target base equals two. */
13813 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13814 && TREE_CODE (arg) == REAL_CST
13815 && !TREE_OVERFLOW (arg))
13817 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13819 if (real_isfinite (ra))
13821 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13822 const int prec = fmt->p;
13823 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13824 tree result_s, result_c;
13825 int inexact;
13826 mpfr_t m, ms, mc;
13828 mpfr_inits2 (prec, m, ms, mc, NULL);
13829 mpfr_from_real (m, ra, GMP_RNDN);
13830 mpfr_clear_flags ();
13831 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13832 result_s = do_mpfr_ckconv (ms, type, inexact);
13833 result_c = do_mpfr_ckconv (mc, type, inexact);
13834 mpfr_clears (m, ms, mc, NULL);
13835 if (result_s && result_c)
13837 /* If we are to return in a complex value do so. */
13838 if (!arg_sinp && !arg_cosp)
13839 return build_complex (build_complex_type (type),
13840 result_c, result_s);
13842 /* Dereference the sin/cos pointer arguments. */
13843 arg_sinp = build_fold_indirect_ref (arg_sinp);
13844 arg_cosp = build_fold_indirect_ref (arg_cosp);
13845 /* Proceed if valid pointer type were passed in. */
13846 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13847 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13849 /* Set the values. */
13850 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13851 result_s);
13852 TREE_SIDE_EFFECTS (result_s) = 1;
13853 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13854 result_c);
13855 TREE_SIDE_EFFECTS (result_c) = 1;
13856 /* Combine the assignments into a compound expr. */
13857 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13858 result_s, result_c));
13863 return result;
13866 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13867 two-argument mpfr order N Bessel function FUNC on them and return
13868 the resulting value as a tree with type TYPE. The mpfr precision
13869 is set to the precision of TYPE. We assume that function FUNC
13870 returns zero if the result could be calculated exactly within the
13871 requested precision. */
13872 static tree
13873 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13874 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13875 const REAL_VALUE_TYPE *min, bool inclusive)
13877 tree result = NULL_TREE;
13879 STRIP_NOPS (arg1);
13880 STRIP_NOPS (arg2);
13882 /* To proceed, MPFR must exactly represent the target floating point
13883 format, which only happens when the target base equals two. */
13884 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13885 && host_integerp (arg1, 0)
13886 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13888 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13889 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13891 if (n == (long)n
13892 && real_isfinite (ra)
13893 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13895 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13896 const int prec = fmt->p;
13897 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13898 int inexact;
13899 mpfr_t m;
13901 mpfr_init2 (m, prec);
13902 mpfr_from_real (m, ra, GMP_RNDN);
13903 mpfr_clear_flags ();
13904 inexact = func (m, n, m, rnd);
13905 result = do_mpfr_ckconv (m, type, inexact);
13906 mpfr_clear (m);
13910 return result;
13913 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13914 the pointer *(ARG_QUO) and return the result. The type is taken
13915 from the type of ARG0 and is used for setting the precision of the
13916 calculation and results. */
13918 static tree
13919 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13921 tree const type = TREE_TYPE (arg0);
13922 tree result = NULL_TREE;
13924 STRIP_NOPS (arg0);
13925 STRIP_NOPS (arg1);
13927 /* To proceed, MPFR must exactly represent the target floating point
13928 format, which only happens when the target base equals two. */
13929 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13930 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13931 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13933 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13934 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13936 if (real_isfinite (ra0) && real_isfinite (ra1))
13938 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13939 const int prec = fmt->p;
13940 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13941 tree result_rem;
13942 long integer_quo;
13943 mpfr_t m0, m1;
13945 mpfr_inits2 (prec, m0, m1, NULL);
13946 mpfr_from_real (m0, ra0, GMP_RNDN);
13947 mpfr_from_real (m1, ra1, GMP_RNDN);
13948 mpfr_clear_flags ();
13949 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13950 /* Remquo is independent of the rounding mode, so pass
13951 inexact=0 to do_mpfr_ckconv(). */
13952 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13953 mpfr_clears (m0, m1, NULL);
13954 if (result_rem)
13956 /* MPFR calculates quo in the host's long so it may
13957 return more bits in quo than the target int can hold
13958 if sizeof(host long) > sizeof(target int). This can
13959 happen even for native compilers in LP64 mode. In
13960 these cases, modulo the quo value with the largest
13961 number that the target int can hold while leaving one
13962 bit for the sign. */
13963 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13964 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13966 /* Dereference the quo pointer argument. */
13967 arg_quo = build_fold_indirect_ref (arg_quo);
13968 /* Proceed iff a valid pointer type was passed in. */
13969 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13971 /* Set the value. */
13972 tree result_quo
13973 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13974 build_int_cst (TREE_TYPE (arg_quo),
13975 integer_quo));
13976 TREE_SIDE_EFFECTS (result_quo) = 1;
13977 /* Combine the quo assignment with the rem. */
13978 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13979 result_quo, result_rem));
13984 return result;
13987 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13988 resulting value as a tree with type TYPE. The mpfr precision is
13989 set to the precision of TYPE. We assume that this mpfr function
13990 returns zero if the result could be calculated exactly within the
13991 requested precision. In addition, the integer pointer represented
13992 by ARG_SG will be dereferenced and set to the appropriate signgam
13993 (-1,1) value. */
13995 static tree
13996 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13998 tree result = NULL_TREE;
14000 STRIP_NOPS (arg);
14002 /* To proceed, MPFR must exactly represent the target floating point
14003 format, which only happens when the target base equals two. Also
14004 verify ARG is a constant and that ARG_SG is an int pointer. */
14005 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
14006 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
14007 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
14008 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
14010 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
14012 /* In addition to NaN and Inf, the argument cannot be zero or a
14013 negative integer. */
14014 if (real_isfinite (ra)
14015 && ra->cl != rvc_zero
14016 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
14018 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14019 const int prec = fmt->p;
14020 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14021 int inexact, sg;
14022 mpfr_t m;
14023 tree result_lg;
14025 mpfr_init2 (m, prec);
14026 mpfr_from_real (m, ra, GMP_RNDN);
14027 mpfr_clear_flags ();
14028 inexact = mpfr_lgamma (m, &sg, m, rnd);
14029 result_lg = do_mpfr_ckconv (m, type, inexact);
14030 mpfr_clear (m);
14031 if (result_lg)
14033 tree result_sg;
14035 /* Dereference the arg_sg pointer argument. */
14036 arg_sg = build_fold_indirect_ref (arg_sg);
14037 /* Assign the signgam value into *arg_sg. */
14038 result_sg = fold_build2 (MODIFY_EXPR,
14039 TREE_TYPE (arg_sg), arg_sg,
14040 build_int_cst (TREE_TYPE (arg_sg), sg));
14041 TREE_SIDE_EFFECTS (result_sg) = 1;
14042 /* Combine the signgam assignment with the lgamma result. */
14043 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14044 result_sg, result_lg));
14049 return result;
14052 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14053 function FUNC on it and return the resulting value as a tree with
14054 type TYPE. The mpfr precision is set to the precision of TYPE. We
14055 assume that function FUNC returns zero if the result could be
14056 calculated exactly within the requested precision. */
14058 static tree
14059 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14061 tree result = NULL_TREE;
14063 STRIP_NOPS (arg);
14065 /* To proceed, MPFR must exactly represent the target floating point
14066 format, which only happens when the target base equals two. */
14067 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14068 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14069 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14071 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14072 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14074 if (real_isfinite (re) && real_isfinite (im))
14076 const struct real_format *const fmt =
14077 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14078 const int prec = fmt->p;
14079 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14080 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14081 int inexact;
14082 mpc_t m;
14084 mpc_init2 (m, prec);
14085 mpfr_from_real (mpc_realref(m), re, rnd);
14086 mpfr_from_real (mpc_imagref(m), im, rnd);
14087 mpfr_clear_flags ();
14088 inexact = func (m, m, crnd);
14089 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14090 mpc_clear (m);
14094 return result;
14097 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14098 mpc function FUNC on it and return the resulting value as a tree
14099 with type TYPE. The mpfr precision is set to the precision of
14100 TYPE. We assume that function FUNC returns zero if the result
14101 could be calculated exactly within the requested precision. If
14102 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14103 in the arguments and/or results. */
14105 tree
14106 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14107 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14109 tree result = NULL_TREE;
14111 STRIP_NOPS (arg0);
14112 STRIP_NOPS (arg1);
14114 /* To proceed, MPFR must exactly represent the target floating point
14115 format, which only happens when the target base equals two. */
14116 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14117 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14118 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14119 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14120 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14122 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14123 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14124 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14125 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14127 if (do_nonfinite
14128 || (real_isfinite (re0) && real_isfinite (im0)
14129 && real_isfinite (re1) && real_isfinite (im1)))
14131 const struct real_format *const fmt =
14132 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14133 const int prec = fmt->p;
14134 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14135 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14136 int inexact;
14137 mpc_t m0, m1;
14139 mpc_init2 (m0, prec);
14140 mpc_init2 (m1, prec);
14141 mpfr_from_real (mpc_realref(m0), re0, rnd);
14142 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14143 mpfr_from_real (mpc_realref(m1), re1, rnd);
14144 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14145 mpfr_clear_flags ();
14146 inexact = func (m0, m0, m1, crnd);
14147 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14148 mpc_clear (m0);
14149 mpc_clear (m1);
14153 return result;
14156 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14157 a normal call should be emitted rather than expanding the function
14158 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14160 static tree
14161 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14163 int nargs = gimple_call_num_args (stmt);
14165 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14166 (nargs > 0
14167 ? gimple_call_arg_ptr (stmt, 0)
14168 : &error_mark_node), fcode);
14171 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14172 a normal call should be emitted rather than expanding the function
14173 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14174 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14175 passed as second argument. */
14177 tree
14178 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14179 enum built_in_function fcode)
14181 int nargs = gimple_call_num_args (stmt);
14183 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14184 (nargs > 0
14185 ? gimple_call_arg_ptr (stmt, 0)
14186 : &error_mark_node), maxlen, fcode);
14189 /* Builtins with folding operations that operate on "..." arguments
14190 need special handling; we need to store the arguments in a convenient
14191 data structure before attempting any folding. Fortunately there are
14192 only a few builtins that fall into this category. FNDECL is the
14193 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14194 result of the function call is ignored. */
14196 static tree
14197 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14198 bool ignore ATTRIBUTE_UNUSED)
14200 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14201 tree ret = NULL_TREE;
14203 switch (fcode)
14205 case BUILT_IN_SPRINTF_CHK:
14206 case BUILT_IN_VSPRINTF_CHK:
14207 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14208 break;
14210 case BUILT_IN_SNPRINTF_CHK:
14211 case BUILT_IN_VSNPRINTF_CHK:
14212 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14214 default:
14215 break;
14217 if (ret)
14219 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14220 TREE_NO_WARNING (ret) = 1;
14221 return ret;
14223 return NULL_TREE;
14226 /* A wrapper function for builtin folding that prevents warnings for
14227 "statement without effect" and the like, caused by removing the
14228 call node earlier than the warning is generated. */
14230 tree
14231 fold_call_stmt (gimple stmt, bool ignore)
14233 tree ret = NULL_TREE;
14234 tree fndecl = gimple_call_fndecl (stmt);
14235 location_t loc = gimple_location (stmt);
14236 if (fndecl
14237 && TREE_CODE (fndecl) == FUNCTION_DECL
14238 && DECL_BUILT_IN (fndecl)
14239 && !gimple_call_va_arg_pack_p (stmt))
14241 int nargs = gimple_call_num_args (stmt);
14242 tree *args = (nargs > 0
14243 ? gimple_call_arg_ptr (stmt, 0)
14244 : &error_mark_node);
14246 if (avoid_folding_inline_builtin (fndecl))
14247 return NULL_TREE;
14248 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14250 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14252 else
14254 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14255 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14256 if (!ret)
14257 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14258 if (ret)
14260 /* Propagate location information from original call to
14261 expansion of builtin. Otherwise things like
14262 maybe_emit_chk_warning, that operate on the expansion
14263 of a builtin, will use the wrong location information. */
14264 if (gimple_has_location (stmt))
14266 tree realret = ret;
14267 if (TREE_CODE (ret) == NOP_EXPR)
14268 realret = TREE_OPERAND (ret, 0);
14269 if (CAN_HAVE_LOCATION_P (realret)
14270 && !EXPR_HAS_LOCATION (realret))
14271 SET_EXPR_LOCATION (realret, loc);
14272 return realret;
14274 return ret;
14278 return NULL_TREE;
14281 /* Look up the function in builtin_decl that corresponds to DECL
14282 and set ASMSPEC as its user assembler name. DECL must be a
14283 function decl that declares a builtin. */
14285 void
14286 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14288 tree builtin;
14289 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14290 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14291 && asmspec != 0);
14293 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14294 set_user_assembler_name (builtin, asmspec);
14295 switch (DECL_FUNCTION_CODE (decl))
14297 case BUILT_IN_MEMCPY:
14298 init_block_move_fn (asmspec);
14299 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14300 break;
14301 case BUILT_IN_MEMSET:
14302 init_block_clear_fn (asmspec);
14303 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14304 break;
14305 case BUILT_IN_MEMMOVE:
14306 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14307 break;
14308 case BUILT_IN_MEMCMP:
14309 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14310 break;
14311 case BUILT_IN_ABORT:
14312 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14313 break;
14314 case BUILT_IN_FFS:
14315 if (INT_TYPE_SIZE < BITS_PER_WORD)
14317 set_user_assembler_libfunc ("ffs", asmspec);
14318 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14319 MODE_INT, 0), "ffs");
14321 break;
14322 default:
14323 break;
14327 /* Return true if DECL is a builtin that expands to a constant or similarly
14328 simple code. */
14329 bool
14330 is_simple_builtin (tree decl)
14332 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14333 switch (DECL_FUNCTION_CODE (decl))
14335 /* Builtins that expand to constants. */
14336 case BUILT_IN_CONSTANT_P:
14337 case BUILT_IN_EXPECT:
14338 case BUILT_IN_OBJECT_SIZE:
14339 case BUILT_IN_UNREACHABLE:
14340 /* Simple register moves or loads from stack. */
14341 case BUILT_IN_ASSUME_ALIGNED:
14342 case BUILT_IN_RETURN_ADDRESS:
14343 case BUILT_IN_EXTRACT_RETURN_ADDR:
14344 case BUILT_IN_FROB_RETURN_ADDR:
14345 case BUILT_IN_RETURN:
14346 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14347 case BUILT_IN_FRAME_ADDRESS:
14348 case BUILT_IN_VA_END:
14349 case BUILT_IN_STACK_SAVE:
14350 case BUILT_IN_STACK_RESTORE:
14351 /* Exception state returns or moves registers around. */
14352 case BUILT_IN_EH_FILTER:
14353 case BUILT_IN_EH_POINTER:
14354 case BUILT_IN_EH_COPY_VALUES:
14355 return true;
14357 default:
14358 return false;
14361 return false;
14364 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14365 most probably expanded inline into reasonably simple code. This is a
14366 superset of is_simple_builtin. */
14367 bool
14368 is_inexpensive_builtin (tree decl)
14370 if (!decl)
14371 return false;
14372 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14373 return true;
14374 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14375 switch (DECL_FUNCTION_CODE (decl))
14377 case BUILT_IN_ABS:
14378 case BUILT_IN_ALLOCA:
14379 case BUILT_IN_ALLOCA_WITH_ALIGN:
14380 case BUILT_IN_BSWAP16:
14381 case BUILT_IN_BSWAP32:
14382 case BUILT_IN_BSWAP64:
14383 case BUILT_IN_CLZ:
14384 case BUILT_IN_CLZIMAX:
14385 case BUILT_IN_CLZL:
14386 case BUILT_IN_CLZLL:
14387 case BUILT_IN_CTZ:
14388 case BUILT_IN_CTZIMAX:
14389 case BUILT_IN_CTZL:
14390 case BUILT_IN_CTZLL:
14391 case BUILT_IN_FFS:
14392 case BUILT_IN_FFSIMAX:
14393 case BUILT_IN_FFSL:
14394 case BUILT_IN_FFSLL:
14395 case BUILT_IN_IMAXABS:
14396 case BUILT_IN_FINITE:
14397 case BUILT_IN_FINITEF:
14398 case BUILT_IN_FINITEL:
14399 case BUILT_IN_FINITED32:
14400 case BUILT_IN_FINITED64:
14401 case BUILT_IN_FINITED128:
14402 case BUILT_IN_FPCLASSIFY:
14403 case BUILT_IN_ISFINITE:
14404 case BUILT_IN_ISINF_SIGN:
14405 case BUILT_IN_ISINF:
14406 case BUILT_IN_ISINFF:
14407 case BUILT_IN_ISINFL:
14408 case BUILT_IN_ISINFD32:
14409 case BUILT_IN_ISINFD64:
14410 case BUILT_IN_ISINFD128:
14411 case BUILT_IN_ISNAN:
14412 case BUILT_IN_ISNANF:
14413 case BUILT_IN_ISNANL:
14414 case BUILT_IN_ISNAND32:
14415 case BUILT_IN_ISNAND64:
14416 case BUILT_IN_ISNAND128:
14417 case BUILT_IN_ISNORMAL:
14418 case BUILT_IN_ISGREATER:
14419 case BUILT_IN_ISGREATEREQUAL:
14420 case BUILT_IN_ISLESS:
14421 case BUILT_IN_ISLESSEQUAL:
14422 case BUILT_IN_ISLESSGREATER:
14423 case BUILT_IN_ISUNORDERED:
14424 case BUILT_IN_VA_ARG_PACK:
14425 case BUILT_IN_VA_ARG_PACK_LEN:
14426 case BUILT_IN_VA_COPY:
14427 case BUILT_IN_TRAP:
14428 case BUILT_IN_SAVEREGS:
14429 case BUILT_IN_POPCOUNTL:
14430 case BUILT_IN_POPCOUNTLL:
14431 case BUILT_IN_POPCOUNTIMAX:
14432 case BUILT_IN_POPCOUNT:
14433 case BUILT_IN_PARITYL:
14434 case BUILT_IN_PARITYLL:
14435 case BUILT_IN_PARITYIMAX:
14436 case BUILT_IN_PARITY:
14437 case BUILT_IN_LABS:
14438 case BUILT_IN_LLABS:
14439 case BUILT_IN_PREFETCH:
14440 return true;
14442 default:
14443 return is_simple_builtin (decl);
14446 return false;