re PR c/59708 (clang-compatible checked arithmetic builtins)
[gcc.git] / gcc / builtins.c
1 /* Expand builtin functions.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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 "stringpool.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "varasm.h"
31 #include "tree-object-size.h"
32 #include "realmpfr.h"
33 #include "predict.h"
34 #include "vec.h"
35 #include "hashtab.h"
36 #include "hash-set.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "cfgrtl.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "flags.h"
48 #include "regs.h"
49 #include "except.h"
50 #include "insn-config.h"
51 #include "expr.h"
52 #include "insn-codes.h"
53 #include "optabs.h"
54 #include "libfuncs.h"
55 #include "recog.h"
56 #include "output.h"
57 #include "typeclass.h"
58 #include "tm_p.h"
59 #include "target.h"
60 #include "langhooks.h"
61 #include "tree-ssanames.h"
62 #include "tree-dfa.h"
63 #include "value-prof.h"
64 #include "diagnostic-core.h"
65 #include "builtins.h"
66 #include "asan.h"
67 #include "ubsan.h"
68 #include "cilk.h"
69 #include "ipa-ref.h"
70 #include "lto-streamer.h"
71 #include "cgraph.h"
72 #include "tree-chkp.h"
73 #include "rtl-chkp.h"
74
75
76 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
77
78 struct target_builtins default_target_builtins;
79 #if SWITCHABLE_TARGET
80 struct target_builtins *this_target_builtins = &default_target_builtins;
81 #endif
82
83 /* Define the names of the builtin function types and codes. */
84 const char *const built_in_class_names[BUILT_IN_LAST]
85 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
86
87 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
88 const char * built_in_names[(int) END_BUILTINS] =
89 {
90 #include "builtins.def"
91 };
92 #undef DEF_BUILTIN
93
94 /* Setup an array of _DECL trees, make sure each element is
95 initialized to NULL_TREE. */
96 builtin_info_type builtin_info;
97
98 /* Non-zero if __builtin_constant_p should be folded right away. */
99 bool force_folding_builtin_constant_p;
100
101 static rtx c_readstr (const char *, machine_mode);
102 static int target_char_cast (tree, char *);
103 static rtx get_memory_rtx (tree, tree);
104 static int apply_args_size (void);
105 static int apply_result_size (void);
106 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
107 static rtx result_vector (int, rtx);
108 #endif
109 static void expand_builtin_update_setjmp_buf (rtx);
110 static void expand_builtin_prefetch (tree);
111 static rtx expand_builtin_apply_args (void);
112 static rtx expand_builtin_apply_args_1 (void);
113 static rtx expand_builtin_apply (rtx, rtx, rtx);
114 static void expand_builtin_return (rtx);
115 static enum type_class type_to_class (tree);
116 static rtx expand_builtin_classify_type (tree);
117 static void expand_errno_check (tree, rtx);
118 static rtx expand_builtin_mathfn (tree, rtx, rtx);
119 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
120 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
121 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
122 static rtx expand_builtin_interclass_mathfn (tree, rtx);
123 static rtx expand_builtin_sincos (tree);
124 static rtx expand_builtin_cexpi (tree, rtx);
125 static rtx expand_builtin_int_roundingfn (tree, rtx);
126 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
127 static rtx expand_builtin_next_arg (void);
128 static rtx expand_builtin_va_start (tree);
129 static rtx expand_builtin_va_end (tree);
130 static rtx expand_builtin_va_copy (tree);
131 static rtx expand_builtin_memcmp (tree, rtx, machine_mode);
132 static rtx expand_builtin_strcmp (tree, rtx);
133 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
134 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
135 static rtx expand_builtin_memcpy (tree, rtx);
136 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
137 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
138 machine_mode, int);
139 static rtx expand_builtin_strcpy (tree, rtx);
140 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
141 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
142 static rtx expand_builtin_strncpy (tree, rtx);
143 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
144 static rtx expand_builtin_memset (tree, rtx, machine_mode);
145 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
146 static rtx expand_builtin_bzero (tree);
147 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
148 static rtx expand_builtin_alloca (tree, bool);
149 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
150 static rtx expand_builtin_frame_address (tree, tree);
151 static tree stabilize_va_list_loc (location_t, tree, int);
152 static rtx expand_builtin_expect (tree, rtx);
153 static tree fold_builtin_constant_p (tree);
154 static tree fold_builtin_classify_type (tree);
155 static tree fold_builtin_strlen (location_t, tree, tree);
156 static tree fold_builtin_inf (location_t, tree, int);
157 static tree fold_builtin_nan (tree, tree, int);
158 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
159 static bool validate_arg (const_tree, enum tree_code code);
160 static bool integer_valued_real_p (tree);
161 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
162 static rtx expand_builtin_fabs (tree, rtx, rtx);
163 static rtx expand_builtin_signbit (tree, rtx);
164 static tree fold_builtin_sqrt (location_t, tree, tree);
165 static tree fold_builtin_cbrt (location_t, tree, tree);
166 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
168 static tree fold_builtin_cos (location_t, tree, tree, tree);
169 static tree fold_builtin_cosh (location_t, tree, tree, tree);
170 static tree fold_builtin_tan (tree, tree);
171 static tree fold_builtin_trunc (location_t, tree, tree);
172 static tree fold_builtin_floor (location_t, tree, tree);
173 static tree fold_builtin_ceil (location_t, tree, tree);
174 static tree fold_builtin_round (location_t, tree, tree);
175 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
176 static tree fold_builtin_bitop (tree, tree);
177 static tree fold_builtin_strchr (location_t, tree, tree, tree);
178 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
179 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
180 static tree fold_builtin_strcmp (location_t, tree, tree);
181 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
182 static tree fold_builtin_signbit (location_t, tree, tree);
183 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
184 static tree fold_builtin_isascii (location_t, tree);
185 static tree fold_builtin_toascii (location_t, tree);
186 static tree fold_builtin_isdigit (location_t, tree);
187 static tree fold_builtin_fabs (location_t, tree, tree);
188 static tree fold_builtin_abs (location_t, tree, tree);
189 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
190 enum tree_code);
191 static tree fold_builtin_0 (location_t, tree, bool);
192 static tree fold_builtin_1 (location_t, tree, tree, bool);
193 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
194 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
195 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
196 static tree fold_builtin_varargs (location_t, tree, tree, bool);
197
198 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
199 static tree fold_builtin_strstr (location_t, tree, tree, tree);
200 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
201 static tree fold_builtin_strncat (location_t, tree, tree, tree);
202 static tree fold_builtin_strspn (location_t, tree, tree);
203 static tree fold_builtin_strcspn (location_t, tree, tree);
204
205 static rtx expand_builtin_object_size (tree);
206 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
207 enum built_in_function);
208 static void maybe_emit_chk_warning (tree, enum built_in_function);
209 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
210 static void maybe_emit_free_warning (tree);
211 static tree fold_builtin_object_size (tree, tree);
212 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
213 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
214 enum built_in_function);
215
216 static unsigned HOST_WIDE_INT target_newline;
217 unsigned HOST_WIDE_INT target_percent;
218 static unsigned HOST_WIDE_INT target_c;
219 static unsigned HOST_WIDE_INT target_s;
220 static char target_percent_c[3];
221 char target_percent_s[3];
222 static char target_percent_s_newline[4];
223 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
224 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
225 static tree do_mpfr_arg2 (tree, tree, tree,
226 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
227 static tree do_mpfr_arg3 (tree, tree, tree, tree,
228 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
229 static tree do_mpfr_sincos (tree, tree, tree);
230 static tree do_mpfr_bessel_n (tree, tree, tree,
231 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
232 const REAL_VALUE_TYPE *, bool);
233 static tree do_mpfr_remquo (tree, tree, tree);
234 static tree do_mpfr_lgamma_r (tree, tree, tree);
235 static void expand_builtin_sync_synchronize (void);
236
237 /* Return true if NAME starts with __builtin_ or __sync_. */
238
239 static bool
240 is_builtin_name (const char *name)
241 {
242 if (strncmp (name, "__builtin_", 10) == 0)
243 return true;
244 if (strncmp (name, "__sync_", 7) == 0)
245 return true;
246 if (strncmp (name, "__atomic_", 9) == 0)
247 return true;
248 if (flag_cilkplus
249 && (!strcmp (name, "__cilkrts_detach")
250 || !strcmp (name, "__cilkrts_pop_frame")))
251 return true;
252 return false;
253 }
254
255
256 /* Return true if DECL is a function symbol representing a built-in. */
257
258 bool
259 is_builtin_fn (tree decl)
260 {
261 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
262 }
263
264 /* Return true if NODE should be considered for inline expansion regardless
265 of the optimization level. This means whenever a function is invoked with
266 its "internal" name, which normally contains the prefix "__builtin". */
267
268 static bool
269 called_as_built_in (tree node)
270 {
271 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
272 we want the name used to call the function, not the name it
273 will have. */
274 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
275 return is_builtin_name (name);
276 }
277
278 /* Compute values M and N such that M divides (address of EXP - N) and such
279 that N < M. If these numbers can be determined, store M in alignp and N in
280 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
281 *alignp and any bit-offset to *bitposp.
282
283 Note that the address (and thus the alignment) computed here is based
284 on the address to which a symbol resolves, whereas DECL_ALIGN is based
285 on the address at which an object is actually located. These two
286 addresses are not always the same. For example, on ARM targets,
287 the address &foo of a Thumb function foo() has the lowest bit set,
288 whereas foo() itself starts on an even address.
289
290 If ADDR_P is true we are taking the address of the memory reference EXP
291 and thus cannot rely on the access taking place. */
292
293 static bool
294 get_object_alignment_2 (tree exp, unsigned int *alignp,
295 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
296 {
297 HOST_WIDE_INT bitsize, bitpos;
298 tree offset;
299 machine_mode mode;
300 int unsignedp, volatilep;
301 unsigned int align = BITS_PER_UNIT;
302 bool known_alignment = false;
303
304 /* Get the innermost object and the constant (bitpos) and possibly
305 variable (offset) offset of the access. */
306 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
307 &mode, &unsignedp, &volatilep, true);
308
309 /* Extract alignment information from the innermost object and
310 possibly adjust bitpos and offset. */
311 if (TREE_CODE (exp) == FUNCTION_DECL)
312 {
313 /* Function addresses can encode extra information besides their
314 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
315 allows the low bit to be used as a virtual bit, we know
316 that the address itself must be at least 2-byte aligned. */
317 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
318 align = 2 * BITS_PER_UNIT;
319 }
320 else if (TREE_CODE (exp) == LABEL_DECL)
321 ;
322 else if (TREE_CODE (exp) == CONST_DECL)
323 {
324 /* The alignment of a CONST_DECL is determined by its initializer. */
325 exp = DECL_INITIAL (exp);
326 align = TYPE_ALIGN (TREE_TYPE (exp));
327 #ifdef CONSTANT_ALIGNMENT
328 if (CONSTANT_CLASS_P (exp))
329 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
330 #endif
331 known_alignment = true;
332 }
333 else if (DECL_P (exp))
334 {
335 align = DECL_ALIGN (exp);
336 known_alignment = true;
337 }
338 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
339 {
340 align = TYPE_ALIGN (TREE_TYPE (exp));
341 }
342 else if (TREE_CODE (exp) == INDIRECT_REF
343 || TREE_CODE (exp) == MEM_REF
344 || TREE_CODE (exp) == TARGET_MEM_REF)
345 {
346 tree addr = TREE_OPERAND (exp, 0);
347 unsigned ptr_align;
348 unsigned HOST_WIDE_INT ptr_bitpos;
349
350 if (TREE_CODE (addr) == BIT_AND_EXPR
351 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
352 {
353 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
354 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
355 align *= BITS_PER_UNIT;
356 addr = TREE_OPERAND (addr, 0);
357 }
358
359 known_alignment
360 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
361 align = MAX (ptr_align, align);
362
363 /* The alignment of the pointer operand in a TARGET_MEM_REF
364 has to take the variable offset parts into account. */
365 if (TREE_CODE (exp) == TARGET_MEM_REF)
366 {
367 if (TMR_INDEX (exp))
368 {
369 unsigned HOST_WIDE_INT step = 1;
370 if (TMR_STEP (exp))
371 step = TREE_INT_CST_LOW (TMR_STEP (exp));
372 align = MIN (align, (step & -step) * BITS_PER_UNIT);
373 }
374 if (TMR_INDEX2 (exp))
375 align = BITS_PER_UNIT;
376 known_alignment = false;
377 }
378
379 /* When EXP is an actual memory reference then we can use
380 TYPE_ALIGN of a pointer indirection to derive alignment.
381 Do so only if get_pointer_alignment_1 did not reveal absolute
382 alignment knowledge and if using that alignment would
383 improve the situation. */
384 if (!addr_p && !known_alignment
385 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
386 align = TYPE_ALIGN (TREE_TYPE (exp));
387 else
388 {
389 /* Else adjust bitpos accordingly. */
390 bitpos += ptr_bitpos;
391 if (TREE_CODE (exp) == MEM_REF
392 || TREE_CODE (exp) == TARGET_MEM_REF)
393 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
394 }
395 }
396 else if (TREE_CODE (exp) == STRING_CST)
397 {
398 /* STRING_CST are the only constant objects we allow to be not
399 wrapped inside a CONST_DECL. */
400 align = TYPE_ALIGN (TREE_TYPE (exp));
401 #ifdef CONSTANT_ALIGNMENT
402 if (CONSTANT_CLASS_P (exp))
403 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
404 #endif
405 known_alignment = true;
406 }
407
408 /* If there is a non-constant offset part extract the maximum
409 alignment that can prevail. */
410 if (offset)
411 {
412 unsigned int trailing_zeros = tree_ctz (offset);
413 if (trailing_zeros < HOST_BITS_PER_INT)
414 {
415 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
416 if (inner)
417 align = MIN (align, inner);
418 }
419 }
420
421 *alignp = align;
422 *bitposp = bitpos & (*alignp - 1);
423 return known_alignment;
424 }
425
426 /* For a memory reference expression EXP compute values M and N such that M
427 divides (&EXP - N) and such that N < M. If these numbers can be determined,
428 store M in alignp and N in *BITPOSP and return true. Otherwise return false
429 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
430
431 bool
432 get_object_alignment_1 (tree exp, unsigned int *alignp,
433 unsigned HOST_WIDE_INT *bitposp)
434 {
435 return get_object_alignment_2 (exp, alignp, bitposp, false);
436 }
437
438 /* Return the alignment in bits of EXP, an object. */
439
440 unsigned int
441 get_object_alignment (tree exp)
442 {
443 unsigned HOST_WIDE_INT bitpos = 0;
444 unsigned int align;
445
446 get_object_alignment_1 (exp, &align, &bitpos);
447
448 /* align and bitpos now specify known low bits of the pointer.
449 ptr & (align - 1) == bitpos. */
450
451 if (bitpos != 0)
452 align = (bitpos & -bitpos);
453 return align;
454 }
455
456 /* For a pointer valued expression EXP compute values M and N such that M
457 divides (EXP - N) and such that N < M. If these numbers can be determined,
458 store M in alignp and N in *BITPOSP and return true. Return false if
459 the results are just a conservative approximation.
460
461 If EXP is not a pointer, false is returned too. */
462
463 bool
464 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
465 unsigned HOST_WIDE_INT *bitposp)
466 {
467 STRIP_NOPS (exp);
468
469 if (TREE_CODE (exp) == ADDR_EXPR)
470 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
471 alignp, bitposp, true);
472 else if (TREE_CODE (exp) == SSA_NAME
473 && POINTER_TYPE_P (TREE_TYPE (exp)))
474 {
475 unsigned int ptr_align, ptr_misalign;
476 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
477
478 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
479 {
480 *bitposp = ptr_misalign * BITS_PER_UNIT;
481 *alignp = ptr_align * BITS_PER_UNIT;
482 /* We cannot really tell whether this result is an approximation. */
483 return true;
484 }
485 else
486 {
487 *bitposp = 0;
488 *alignp = BITS_PER_UNIT;
489 return false;
490 }
491 }
492 else if (TREE_CODE (exp) == INTEGER_CST)
493 {
494 *alignp = BIGGEST_ALIGNMENT;
495 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
496 & (BIGGEST_ALIGNMENT - 1));
497 return true;
498 }
499
500 *bitposp = 0;
501 *alignp = BITS_PER_UNIT;
502 return false;
503 }
504
505 /* Return the alignment in bits of EXP, a pointer valued expression.
506 The alignment returned is, by default, the alignment of the thing that
507 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
508
509 Otherwise, look at the expression to see if we can do better, i.e., if the
510 expression is actually pointing at an object whose alignment is tighter. */
511
512 unsigned int
513 get_pointer_alignment (tree exp)
514 {
515 unsigned HOST_WIDE_INT bitpos = 0;
516 unsigned int align;
517
518 get_pointer_alignment_1 (exp, &align, &bitpos);
519
520 /* align and bitpos now specify known low bits of the pointer.
521 ptr & (align - 1) == bitpos. */
522
523 if (bitpos != 0)
524 align = (bitpos & -bitpos);
525
526 return align;
527 }
528
529 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
530 way, because it could contain a zero byte in the middle.
531 TREE_STRING_LENGTH is the size of the character array, not the string.
532
533 ONLY_VALUE should be nonzero if the result is not going to be emitted
534 into the instruction stream and zero if it is going to be expanded.
535 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
536 is returned, otherwise NULL, since
537 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
538 evaluate the side-effects.
539
540 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
541 accesses. Note that this implies the result is not going to be emitted
542 into the instruction stream.
543
544 The value returned is of type `ssizetype'.
545
546 Unfortunately, string_constant can't access the values of const char
547 arrays with initializers, so neither can we do so here. */
548
549 tree
550 c_strlen (tree src, int only_value)
551 {
552 tree offset_node;
553 HOST_WIDE_INT offset;
554 int max;
555 const char *ptr;
556 location_t loc;
557
558 STRIP_NOPS (src);
559 if (TREE_CODE (src) == COND_EXPR
560 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
561 {
562 tree len1, len2;
563
564 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
565 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
566 if (tree_int_cst_equal (len1, len2))
567 return len1;
568 }
569
570 if (TREE_CODE (src) == COMPOUND_EXPR
571 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
572 return c_strlen (TREE_OPERAND (src, 1), only_value);
573
574 loc = EXPR_LOC_OR_LOC (src, input_location);
575
576 src = string_constant (src, &offset_node);
577 if (src == 0)
578 return NULL_TREE;
579
580 max = TREE_STRING_LENGTH (src) - 1;
581 ptr = TREE_STRING_POINTER (src);
582
583 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
584 {
585 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
586 compute the offset to the following null if we don't know where to
587 start searching for it. */
588 int i;
589
590 for (i = 0; i < max; i++)
591 if (ptr[i] == 0)
592 return NULL_TREE;
593
594 /* We don't know the starting offset, but we do know that the string
595 has no internal zero bytes. We can assume that the offset falls
596 within the bounds of the string; otherwise, the programmer deserves
597 what he gets. Subtract the offset from the length of the string,
598 and return that. This would perhaps not be valid if we were dealing
599 with named arrays in addition to literal string constants. */
600
601 return size_diffop_loc (loc, size_int (max), offset_node);
602 }
603
604 /* We have a known offset into the string. Start searching there for
605 a null character if we can represent it as a single HOST_WIDE_INT. */
606 if (offset_node == 0)
607 offset = 0;
608 else if (! tree_fits_shwi_p (offset_node))
609 offset = -1;
610 else
611 offset = tree_to_shwi (offset_node);
612
613 /* If the offset is known to be out of bounds, warn, and call strlen at
614 runtime. */
615 if (offset < 0 || offset > max)
616 {
617 /* Suppress multiple warnings for propagated constant strings. */
618 if (only_value != 2
619 && !TREE_NO_WARNING (src))
620 {
621 warning_at (loc, 0, "offset outside bounds of constant string");
622 TREE_NO_WARNING (src) = 1;
623 }
624 return NULL_TREE;
625 }
626
627 /* Use strlen to search for the first zero byte. Since any strings
628 constructed with build_string will have nulls appended, we win even
629 if we get handed something like (char[4])"abcd".
630
631 Since OFFSET is our starting index into the string, no further
632 calculation is needed. */
633 return ssize_int (strlen (ptr + offset));
634 }
635
636 /* Return a char pointer for a C string if it is a string constant
637 or sum of string constant and integer constant. */
638
639 const char *
640 c_getstr (tree src)
641 {
642 tree offset_node;
643
644 src = string_constant (src, &offset_node);
645 if (src == 0)
646 return 0;
647
648 if (offset_node == 0)
649 return TREE_STRING_POINTER (src);
650 else if (!tree_fits_uhwi_p (offset_node)
651 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
652 return 0;
653
654 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
655 }
656
657 /* Return a constant integer corresponding to target reading
658 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
659
660 static rtx
661 c_readstr (const char *str, machine_mode mode)
662 {
663 HOST_WIDE_INT ch;
664 unsigned int i, j;
665 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
666
667 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
668 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
669 / HOST_BITS_PER_WIDE_INT;
670
671 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
672 for (i = 0; i < len; i++)
673 tmp[i] = 0;
674
675 ch = 1;
676 for (i = 0; i < GET_MODE_SIZE (mode); i++)
677 {
678 j = i;
679 if (WORDS_BIG_ENDIAN)
680 j = GET_MODE_SIZE (mode) - i - 1;
681 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
682 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
683 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
684 j *= BITS_PER_UNIT;
685
686 if (ch)
687 ch = (unsigned char) str[i];
688 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
689 }
690
691 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
692 return immed_wide_int_const (c, mode);
693 }
694
695 /* Cast a target constant CST to target CHAR and if that value fits into
696 host char type, return zero and put that value into variable pointed to by
697 P. */
698
699 static int
700 target_char_cast (tree cst, char *p)
701 {
702 unsigned HOST_WIDE_INT val, hostval;
703
704 if (TREE_CODE (cst) != INTEGER_CST
705 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
706 return 1;
707
708 /* Do not care if it fits or not right here. */
709 val = TREE_INT_CST_LOW (cst);
710
711 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
712 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
713
714 hostval = val;
715 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
716 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
717
718 if (val != hostval)
719 return 1;
720
721 *p = hostval;
722 return 0;
723 }
724
725 /* Similar to save_expr, but assumes that arbitrary code is not executed
726 in between the multiple evaluations. In particular, we assume that a
727 non-addressable local variable will not be modified. */
728
729 static tree
730 builtin_save_expr (tree exp)
731 {
732 if (TREE_CODE (exp) == SSA_NAME
733 || (TREE_ADDRESSABLE (exp) == 0
734 && (TREE_CODE (exp) == PARM_DECL
735 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
736 return exp;
737
738 return save_expr (exp);
739 }
740
741 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
742 times to get the address of either a higher stack frame, or a return
743 address located within it (depending on FNDECL_CODE). */
744
745 static rtx
746 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
747 {
748 int i;
749
750 #ifdef INITIAL_FRAME_ADDRESS_RTX
751 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
752 #else
753 rtx tem;
754
755 /* For a zero count with __builtin_return_address, we don't care what
756 frame address we return, because target-specific definitions will
757 override us. Therefore frame pointer elimination is OK, and using
758 the soft frame pointer is OK.
759
760 For a nonzero count, or a zero count with __builtin_frame_address,
761 we require a stable offset from the current frame pointer to the
762 previous one, so we must use the hard frame pointer, and
763 we must disable frame pointer elimination. */
764 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
765 tem = frame_pointer_rtx;
766 else
767 {
768 tem = hard_frame_pointer_rtx;
769
770 /* Tell reload not to eliminate the frame pointer. */
771 crtl->accesses_prior_frames = 1;
772 }
773 #endif
774
775 /* Some machines need special handling before we can access
776 arbitrary frames. For example, on the SPARC, we must first flush
777 all register windows to the stack. */
778 #ifdef SETUP_FRAME_ADDRESSES
779 if (count > 0)
780 SETUP_FRAME_ADDRESSES ();
781 #endif
782
783 /* On the SPARC, the return address is not in the frame, it is in a
784 register. There is no way to access it off of the current frame
785 pointer, but it can be accessed off the previous frame pointer by
786 reading the value from the register window save area. */
787 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
788 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
789 count--;
790 #endif
791
792 /* Scan back COUNT frames to the specified frame. */
793 for (i = 0; i < count; i++)
794 {
795 /* Assume the dynamic chain pointer is in the word that the
796 frame address points to, unless otherwise specified. */
797 #ifdef DYNAMIC_CHAIN_ADDRESS
798 tem = DYNAMIC_CHAIN_ADDRESS (tem);
799 #endif
800 tem = memory_address (Pmode, tem);
801 tem = gen_frame_mem (Pmode, tem);
802 tem = copy_to_reg (tem);
803 }
804
805 /* For __builtin_frame_address, return what we've got. But, on
806 the SPARC for example, we may have to add a bias. */
807 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
808 #ifdef FRAME_ADDR_RTX
809 return FRAME_ADDR_RTX (tem);
810 #else
811 return tem;
812 #endif
813
814 /* For __builtin_return_address, get the return address from that frame. */
815 #ifdef RETURN_ADDR_RTX
816 tem = RETURN_ADDR_RTX (count, tem);
817 #else
818 tem = memory_address (Pmode,
819 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
820 tem = gen_frame_mem (Pmode, tem);
821 #endif
822 return tem;
823 }
824
825 /* Alias set used for setjmp buffer. */
826 static alias_set_type setjmp_alias_set = -1;
827
828 /* Construct the leading half of a __builtin_setjmp call. Control will
829 return to RECEIVER_LABEL. This is also called directly by the SJLJ
830 exception handling code. */
831
832 void
833 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
834 {
835 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
836 rtx stack_save;
837 rtx mem;
838
839 if (setjmp_alias_set == -1)
840 setjmp_alias_set = new_alias_set ();
841
842 buf_addr = convert_memory_address (Pmode, buf_addr);
843
844 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
845
846 /* We store the frame pointer and the address of receiver_label in
847 the buffer and use the rest of it for the stack save area, which
848 is machine-dependent. */
849
850 mem = gen_rtx_MEM (Pmode, buf_addr);
851 set_mem_alias_set (mem, setjmp_alias_set);
852 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
853
854 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
855 GET_MODE_SIZE (Pmode))),
856 set_mem_alias_set (mem, setjmp_alias_set);
857
858 emit_move_insn (validize_mem (mem),
859 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
860
861 stack_save = gen_rtx_MEM (sa_mode,
862 plus_constant (Pmode, buf_addr,
863 2 * GET_MODE_SIZE (Pmode)));
864 set_mem_alias_set (stack_save, setjmp_alias_set);
865 emit_stack_save (SAVE_NONLOCAL, &stack_save);
866
867 /* If there is further processing to do, do it. */
868 #ifdef HAVE_builtin_setjmp_setup
869 if (HAVE_builtin_setjmp_setup)
870 emit_insn (gen_builtin_setjmp_setup (buf_addr));
871 #endif
872
873 /* We have a nonlocal label. */
874 cfun->has_nonlocal_label = 1;
875 }
876
877 /* Construct the trailing part of a __builtin_setjmp call. This is
878 also called directly by the SJLJ exception handling code.
879 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
880
881 void
882 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
883 {
884 rtx chain;
885
886 /* Mark the FP as used when we get here, so we have to make sure it's
887 marked as used by this function. */
888 emit_use (hard_frame_pointer_rtx);
889
890 /* Mark the static chain as clobbered here so life information
891 doesn't get messed up for it. */
892 chain = targetm.calls.static_chain (current_function_decl, true);
893 if (chain && REG_P (chain))
894 emit_clobber (chain);
895
896 /* Now put in the code to restore the frame pointer, and argument
897 pointer, if needed. */
898 #ifdef HAVE_nonlocal_goto
899 if (! HAVE_nonlocal_goto)
900 #endif
901 {
902 /* First adjust our frame pointer to its actual value. It was
903 previously set to the start of the virtual area corresponding to
904 the stacked variables when we branched here and now needs to be
905 adjusted to the actual hardware fp value.
906
907 Assignments to virtual registers are converted by
908 instantiate_virtual_regs into the corresponding assignment
909 to the underlying register (fp in this case) that makes
910 the original assignment true.
911 So the following insn will actually be decrementing fp by
912 STARTING_FRAME_OFFSET. */
913 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
914
915 /* Restoring the frame pointer also modifies the hard frame pointer.
916 Mark it used (so that the previous assignment remains live once
917 the frame pointer is eliminated) and clobbered (to represent the
918 implicit update from the assignment). */
919 emit_use (hard_frame_pointer_rtx);
920 emit_clobber (hard_frame_pointer_rtx);
921 }
922
923 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
924 if (fixed_regs[ARG_POINTER_REGNUM])
925 {
926 #ifdef ELIMINABLE_REGS
927 /* If the argument pointer can be eliminated in favor of the
928 frame pointer, we don't need to restore it. We assume here
929 that if such an elimination is present, it can always be used.
930 This is the case on all known machines; if we don't make this
931 assumption, we do unnecessary saving on many machines. */
932 size_t i;
933 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
934
935 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
936 if (elim_regs[i].from == ARG_POINTER_REGNUM
937 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
938 break;
939
940 if (i == ARRAY_SIZE (elim_regs))
941 #endif
942 {
943 /* Now restore our arg pointer from the address at which it
944 was saved in our stack frame. */
945 emit_move_insn (crtl->args.internal_arg_pointer,
946 copy_to_reg (get_arg_pointer_save_area ()));
947 }
948 }
949 #endif
950
951 #ifdef HAVE_builtin_setjmp_receiver
952 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
953 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
954 else
955 #endif
956 #ifdef HAVE_nonlocal_goto_receiver
957 if (HAVE_nonlocal_goto_receiver)
958 emit_insn (gen_nonlocal_goto_receiver ());
959 else
960 #endif
961 { /* Nothing */ }
962
963 /* We must not allow the code we just generated to be reordered by
964 scheduling. Specifically, the update of the frame pointer must
965 happen immediately, not later. */
966 emit_insn (gen_blockage ());
967 }
968
969 /* __builtin_longjmp is passed a pointer to an array of five words (not
970 all will be used on all machines). It operates similarly to the C
971 library function of the same name, but is more efficient. Much of
972 the code below is copied from the handling of non-local gotos. */
973
974 static void
975 expand_builtin_longjmp (rtx buf_addr, rtx value)
976 {
977 rtx fp, lab, stack;
978 rtx_insn *insn, *last;
979 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
980
981 /* DRAP is needed for stack realign if longjmp is expanded to current
982 function */
983 if (SUPPORTS_STACK_ALIGNMENT)
984 crtl->need_drap = true;
985
986 if (setjmp_alias_set == -1)
987 setjmp_alias_set = new_alias_set ();
988
989 buf_addr = convert_memory_address (Pmode, buf_addr);
990
991 buf_addr = force_reg (Pmode, buf_addr);
992
993 /* We require that the user must pass a second argument of 1, because
994 that is what builtin_setjmp will return. */
995 gcc_assert (value == const1_rtx);
996
997 last = get_last_insn ();
998 #ifdef HAVE_builtin_longjmp
999 if (HAVE_builtin_longjmp)
1000 emit_insn (gen_builtin_longjmp (buf_addr));
1001 else
1002 #endif
1003 {
1004 fp = gen_rtx_MEM (Pmode, buf_addr);
1005 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1006 GET_MODE_SIZE (Pmode)));
1007
1008 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1009 2 * GET_MODE_SIZE (Pmode)));
1010 set_mem_alias_set (fp, setjmp_alias_set);
1011 set_mem_alias_set (lab, setjmp_alias_set);
1012 set_mem_alias_set (stack, setjmp_alias_set);
1013
1014 /* Pick up FP, label, and SP from the block and jump. This code is
1015 from expand_goto in stmt.c; see there for detailed comments. */
1016 #ifdef HAVE_nonlocal_goto
1017 if (HAVE_nonlocal_goto)
1018 /* We have to pass a value to the nonlocal_goto pattern that will
1019 get copied into the static_chain pointer, but it does not matter
1020 what that value is, because builtin_setjmp does not use it. */
1021 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1022 else
1023 #endif
1024 {
1025 lab = copy_to_reg (lab);
1026
1027 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1028 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1029
1030 emit_move_insn (hard_frame_pointer_rtx, fp);
1031 emit_stack_restore (SAVE_NONLOCAL, stack);
1032
1033 emit_use (hard_frame_pointer_rtx);
1034 emit_use (stack_pointer_rtx);
1035 emit_indirect_jump (lab);
1036 }
1037 }
1038
1039 /* Search backwards and mark the jump insn as a non-local goto.
1040 Note that this precludes the use of __builtin_longjmp to a
1041 __builtin_setjmp target in the same function. However, we've
1042 already cautioned the user that these functions are for
1043 internal exception handling use only. */
1044 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1045 {
1046 gcc_assert (insn != last);
1047
1048 if (JUMP_P (insn))
1049 {
1050 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1051 break;
1052 }
1053 else if (CALL_P (insn))
1054 break;
1055 }
1056 }
1057
1058 static inline bool
1059 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1060 {
1061 return (iter->i < iter->n);
1062 }
1063
1064 /* This function validates the types of a function call argument list
1065 against a specified list of tree_codes. If the last specifier is a 0,
1066 that represents an ellipses, otherwise the last specifier must be a
1067 VOID_TYPE. */
1068
1069 static bool
1070 validate_arglist (const_tree callexpr, ...)
1071 {
1072 enum tree_code code;
1073 bool res = 0;
1074 va_list ap;
1075 const_call_expr_arg_iterator iter;
1076 const_tree arg;
1077
1078 va_start (ap, callexpr);
1079 init_const_call_expr_arg_iterator (callexpr, &iter);
1080
1081 do
1082 {
1083 code = (enum tree_code) va_arg (ap, int);
1084 switch (code)
1085 {
1086 case 0:
1087 /* This signifies an ellipses, any further arguments are all ok. */
1088 res = true;
1089 goto end;
1090 case VOID_TYPE:
1091 /* This signifies an endlink, if no arguments remain, return
1092 true, otherwise return false. */
1093 res = !more_const_call_expr_args_p (&iter);
1094 goto end;
1095 default:
1096 /* If no parameters remain or the parameter's code does not
1097 match the specified code, return false. Otherwise continue
1098 checking any remaining arguments. */
1099 arg = next_const_call_expr_arg (&iter);
1100 if (!validate_arg (arg, code))
1101 goto end;
1102 break;
1103 }
1104 }
1105 while (1);
1106
1107 /* We need gotos here since we can only have one VA_CLOSE in a
1108 function. */
1109 end: ;
1110 va_end (ap);
1111
1112 return res;
1113 }
1114
1115 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1116 and the address of the save area. */
1117
1118 static rtx
1119 expand_builtin_nonlocal_goto (tree exp)
1120 {
1121 tree t_label, t_save_area;
1122 rtx r_label, r_save_area, r_fp, r_sp;
1123 rtx_insn *insn;
1124
1125 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1126 return NULL_RTX;
1127
1128 t_label = CALL_EXPR_ARG (exp, 0);
1129 t_save_area = CALL_EXPR_ARG (exp, 1);
1130
1131 r_label = expand_normal (t_label);
1132 r_label = convert_memory_address (Pmode, r_label);
1133 r_save_area = expand_normal (t_save_area);
1134 r_save_area = convert_memory_address (Pmode, r_save_area);
1135 /* Copy the address of the save location to a register just in case it was
1136 based on the frame pointer. */
1137 r_save_area = copy_to_reg (r_save_area);
1138 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1139 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1140 plus_constant (Pmode, r_save_area,
1141 GET_MODE_SIZE (Pmode)));
1142
1143 crtl->has_nonlocal_goto = 1;
1144
1145 #ifdef HAVE_nonlocal_goto
1146 /* ??? We no longer need to pass the static chain value, afaik. */
1147 if (HAVE_nonlocal_goto)
1148 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1149 else
1150 #endif
1151 {
1152 r_label = copy_to_reg (r_label);
1153
1154 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1155 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1156
1157 /* Restore frame pointer for containing function. */
1158 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1159 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1160
1161 /* USE of hard_frame_pointer_rtx added for consistency;
1162 not clear if really needed. */
1163 emit_use (hard_frame_pointer_rtx);
1164 emit_use (stack_pointer_rtx);
1165
1166 /* If the architecture is using a GP register, we must
1167 conservatively assume that the target function makes use of it.
1168 The prologue of functions with nonlocal gotos must therefore
1169 initialize the GP register to the appropriate value, and we
1170 must then make sure that this value is live at the point
1171 of the jump. (Note that this doesn't necessarily apply
1172 to targets with a nonlocal_goto pattern; they are free
1173 to implement it in their own way. Note also that this is
1174 a no-op if the GP register is a global invariant.) */
1175 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1176 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1177 emit_use (pic_offset_table_rtx);
1178
1179 emit_indirect_jump (r_label);
1180 }
1181
1182 /* Search backwards to the jump insn and mark it as a
1183 non-local goto. */
1184 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1185 {
1186 if (JUMP_P (insn))
1187 {
1188 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1189 break;
1190 }
1191 else if (CALL_P (insn))
1192 break;
1193 }
1194
1195 return const0_rtx;
1196 }
1197
1198 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1199 (not all will be used on all machines) that was passed to __builtin_setjmp.
1200 It updates the stack pointer in that block to correspond to the current
1201 stack pointer. */
1202
1203 static void
1204 expand_builtin_update_setjmp_buf (rtx buf_addr)
1205 {
1206 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1207 rtx stack_save
1208 = gen_rtx_MEM (sa_mode,
1209 memory_address
1210 (sa_mode,
1211 plus_constant (Pmode, buf_addr,
1212 2 * GET_MODE_SIZE (Pmode))));
1213
1214 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1215 }
1216
1217 /* Expand a call to __builtin_prefetch. For a target that does not support
1218 data prefetch, evaluate the memory address argument in case it has side
1219 effects. */
1220
1221 static void
1222 expand_builtin_prefetch (tree exp)
1223 {
1224 tree arg0, arg1, arg2;
1225 int nargs;
1226 rtx op0, op1, op2;
1227
1228 if (!validate_arglist (exp, POINTER_TYPE, 0))
1229 return;
1230
1231 arg0 = CALL_EXPR_ARG (exp, 0);
1232
1233 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1234 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1235 locality). */
1236 nargs = call_expr_nargs (exp);
1237 if (nargs > 1)
1238 arg1 = CALL_EXPR_ARG (exp, 1);
1239 else
1240 arg1 = integer_zero_node;
1241 if (nargs > 2)
1242 arg2 = CALL_EXPR_ARG (exp, 2);
1243 else
1244 arg2 = integer_three_node;
1245
1246 /* Argument 0 is an address. */
1247 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1248
1249 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1250 if (TREE_CODE (arg1) != INTEGER_CST)
1251 {
1252 error ("second argument to %<__builtin_prefetch%> must be a constant");
1253 arg1 = integer_zero_node;
1254 }
1255 op1 = expand_normal (arg1);
1256 /* Argument 1 must be either zero or one. */
1257 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1258 {
1259 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1260 " using zero");
1261 op1 = const0_rtx;
1262 }
1263
1264 /* Argument 2 (locality) must be a compile-time constant int. */
1265 if (TREE_CODE (arg2) != INTEGER_CST)
1266 {
1267 error ("third argument to %<__builtin_prefetch%> must be a constant");
1268 arg2 = integer_zero_node;
1269 }
1270 op2 = expand_normal (arg2);
1271 /* Argument 2 must be 0, 1, 2, or 3. */
1272 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1273 {
1274 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1275 op2 = const0_rtx;
1276 }
1277
1278 #ifdef HAVE_prefetch
1279 if (HAVE_prefetch)
1280 {
1281 struct expand_operand ops[3];
1282
1283 create_address_operand (&ops[0], op0);
1284 create_integer_operand (&ops[1], INTVAL (op1));
1285 create_integer_operand (&ops[2], INTVAL (op2));
1286 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1287 return;
1288 }
1289 #endif
1290
1291 /* Don't do anything with direct references to volatile memory, but
1292 generate code to handle other side effects. */
1293 if (!MEM_P (op0) && side_effects_p (op0))
1294 emit_insn (op0);
1295 }
1296
1297 /* Get a MEM rtx for expression EXP which is the address of an operand
1298 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1299 the maximum length of the block of memory that might be accessed or
1300 NULL if unknown. */
1301
1302 static rtx
1303 get_memory_rtx (tree exp, tree len)
1304 {
1305 tree orig_exp = exp;
1306 rtx addr, mem;
1307
1308 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1309 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1310 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1311 exp = TREE_OPERAND (exp, 0);
1312
1313 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1314 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1315
1316 /* Get an expression we can use to find the attributes to assign to MEM.
1317 First remove any nops. */
1318 while (CONVERT_EXPR_P (exp)
1319 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1320 exp = TREE_OPERAND (exp, 0);
1321
1322 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1323 (as builtin stringops may alias with anything). */
1324 exp = fold_build2 (MEM_REF,
1325 build_array_type (char_type_node,
1326 build_range_type (sizetype,
1327 size_one_node, len)),
1328 exp, build_int_cst (ptr_type_node, 0));
1329
1330 /* If the MEM_REF has no acceptable address, try to get the base object
1331 from the original address we got, and build an all-aliasing
1332 unknown-sized access to that one. */
1333 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1334 set_mem_attributes (mem, exp, 0);
1335 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1336 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1337 0))))
1338 {
1339 exp = build_fold_addr_expr (exp);
1340 exp = fold_build2 (MEM_REF,
1341 build_array_type (char_type_node,
1342 build_range_type (sizetype,
1343 size_zero_node,
1344 NULL)),
1345 exp, build_int_cst (ptr_type_node, 0));
1346 set_mem_attributes (mem, exp, 0);
1347 }
1348 set_mem_alias_set (mem, 0);
1349 return mem;
1350 }
1351 \f
1352 /* Built-in functions to perform an untyped call and return. */
1353
1354 #define apply_args_mode \
1355 (this_target_builtins->x_apply_args_mode)
1356 #define apply_result_mode \
1357 (this_target_builtins->x_apply_result_mode)
1358
1359 /* Return the size required for the block returned by __builtin_apply_args,
1360 and initialize apply_args_mode. */
1361
1362 static int
1363 apply_args_size (void)
1364 {
1365 static int size = -1;
1366 int align;
1367 unsigned int regno;
1368 machine_mode mode;
1369
1370 /* The values computed by this function never change. */
1371 if (size < 0)
1372 {
1373 /* The first value is the incoming arg-pointer. */
1374 size = GET_MODE_SIZE (Pmode);
1375
1376 /* The second value is the structure value address unless this is
1377 passed as an "invisible" first argument. */
1378 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1379 size += GET_MODE_SIZE (Pmode);
1380
1381 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1382 if (FUNCTION_ARG_REGNO_P (regno))
1383 {
1384 mode = targetm.calls.get_raw_arg_mode (regno);
1385
1386 gcc_assert (mode != VOIDmode);
1387
1388 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1389 if (size % align != 0)
1390 size = CEIL (size, align) * align;
1391 size += GET_MODE_SIZE (mode);
1392 apply_args_mode[regno] = mode;
1393 }
1394 else
1395 {
1396 apply_args_mode[regno] = VOIDmode;
1397 }
1398 }
1399 return size;
1400 }
1401
1402 /* Return the size required for the block returned by __builtin_apply,
1403 and initialize apply_result_mode. */
1404
1405 static int
1406 apply_result_size (void)
1407 {
1408 static int size = -1;
1409 int align, regno;
1410 machine_mode mode;
1411
1412 /* The values computed by this function never change. */
1413 if (size < 0)
1414 {
1415 size = 0;
1416
1417 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1418 if (targetm.calls.function_value_regno_p (regno))
1419 {
1420 mode = targetm.calls.get_raw_result_mode (regno);
1421
1422 gcc_assert (mode != VOIDmode);
1423
1424 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1425 if (size % align != 0)
1426 size = CEIL (size, align) * align;
1427 size += GET_MODE_SIZE (mode);
1428 apply_result_mode[regno] = mode;
1429 }
1430 else
1431 apply_result_mode[regno] = VOIDmode;
1432
1433 /* Allow targets that use untyped_call and untyped_return to override
1434 the size so that machine-specific information can be stored here. */
1435 #ifdef APPLY_RESULT_SIZE
1436 size = APPLY_RESULT_SIZE;
1437 #endif
1438 }
1439 return size;
1440 }
1441
1442 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1443 /* Create a vector describing the result block RESULT. If SAVEP is true,
1444 the result block is used to save the values; otherwise it is used to
1445 restore the values. */
1446
1447 static rtx
1448 result_vector (int savep, rtx result)
1449 {
1450 int regno, size, align, nelts;
1451 machine_mode mode;
1452 rtx reg, mem;
1453 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1454
1455 size = nelts = 0;
1456 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1457 if ((mode = apply_result_mode[regno]) != VOIDmode)
1458 {
1459 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1460 if (size % align != 0)
1461 size = CEIL (size, align) * align;
1462 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1463 mem = adjust_address (result, mode, size);
1464 savevec[nelts++] = (savep
1465 ? gen_rtx_SET (VOIDmode, mem, reg)
1466 : gen_rtx_SET (VOIDmode, reg, mem));
1467 size += GET_MODE_SIZE (mode);
1468 }
1469 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1470 }
1471 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1472
1473 /* Save the state required to perform an untyped call with the same
1474 arguments as were passed to the current function. */
1475
1476 static rtx
1477 expand_builtin_apply_args_1 (void)
1478 {
1479 rtx registers, tem;
1480 int size, align, regno;
1481 machine_mode mode;
1482 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1483
1484 /* Create a block where the arg-pointer, structure value address,
1485 and argument registers can be saved. */
1486 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1487
1488 /* Walk past the arg-pointer and structure value address. */
1489 size = GET_MODE_SIZE (Pmode);
1490 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1491 size += GET_MODE_SIZE (Pmode);
1492
1493 /* Save each register used in calling a function to the block. */
1494 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1495 if ((mode = apply_args_mode[regno]) != VOIDmode)
1496 {
1497 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1498 if (size % align != 0)
1499 size = CEIL (size, align) * align;
1500
1501 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1502
1503 emit_move_insn (adjust_address (registers, mode, size), tem);
1504 size += GET_MODE_SIZE (mode);
1505 }
1506
1507 /* Save the arg pointer to the block. */
1508 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1509 #ifdef STACK_GROWS_DOWNWARD
1510 /* We need the pointer as the caller actually passed them to us, not
1511 as we might have pretended they were passed. Make sure it's a valid
1512 operand, as emit_move_insn isn't expected to handle a PLUS. */
1513 tem
1514 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1515 NULL_RTX);
1516 #endif
1517 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1518
1519 size = GET_MODE_SIZE (Pmode);
1520
1521 /* Save the structure value address unless this is passed as an
1522 "invisible" first argument. */
1523 if (struct_incoming_value)
1524 {
1525 emit_move_insn (adjust_address (registers, Pmode, size),
1526 copy_to_reg (struct_incoming_value));
1527 size += GET_MODE_SIZE (Pmode);
1528 }
1529
1530 /* Return the address of the block. */
1531 return copy_addr_to_reg (XEXP (registers, 0));
1532 }
1533
1534 /* __builtin_apply_args returns block of memory allocated on
1535 the stack into which is stored the arg pointer, structure
1536 value address, static chain, and all the registers that might
1537 possibly be used in performing a function call. The code is
1538 moved to the start of the function so the incoming values are
1539 saved. */
1540
1541 static rtx
1542 expand_builtin_apply_args (void)
1543 {
1544 /* Don't do __builtin_apply_args more than once in a function.
1545 Save the result of the first call and reuse it. */
1546 if (apply_args_value != 0)
1547 return apply_args_value;
1548 {
1549 /* When this function is called, it means that registers must be
1550 saved on entry to this function. So we migrate the
1551 call to the first insn of this function. */
1552 rtx temp;
1553 rtx seq;
1554
1555 start_sequence ();
1556 temp = expand_builtin_apply_args_1 ();
1557 seq = get_insns ();
1558 end_sequence ();
1559
1560 apply_args_value = temp;
1561
1562 /* Put the insns after the NOTE that starts the function.
1563 If this is inside a start_sequence, make the outer-level insn
1564 chain current, so the code is placed at the start of the
1565 function. If internal_arg_pointer is a non-virtual pseudo,
1566 it needs to be placed after the function that initializes
1567 that pseudo. */
1568 push_topmost_sequence ();
1569 if (REG_P (crtl->args.internal_arg_pointer)
1570 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1571 emit_insn_before (seq, parm_birth_insn);
1572 else
1573 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1574 pop_topmost_sequence ();
1575 return temp;
1576 }
1577 }
1578
1579 /* Perform an untyped call and save the state required to perform an
1580 untyped return of whatever value was returned by the given function. */
1581
1582 static rtx
1583 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1584 {
1585 int size, align, regno;
1586 machine_mode mode;
1587 rtx incoming_args, result, reg, dest, src;
1588 rtx_call_insn *call_insn;
1589 rtx old_stack_level = 0;
1590 rtx call_fusage = 0;
1591 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1592
1593 arguments = convert_memory_address (Pmode, arguments);
1594
1595 /* Create a block where the return registers can be saved. */
1596 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1597
1598 /* Fetch the arg pointer from the ARGUMENTS block. */
1599 incoming_args = gen_reg_rtx (Pmode);
1600 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1601 #ifndef STACK_GROWS_DOWNWARD
1602 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1603 incoming_args, 0, OPTAB_LIB_WIDEN);
1604 #endif
1605
1606 /* Push a new argument block and copy the arguments. Do not allow
1607 the (potential) memcpy call below to interfere with our stack
1608 manipulations. */
1609 do_pending_stack_adjust ();
1610 NO_DEFER_POP;
1611
1612 /* Save the stack with nonlocal if available. */
1613 #ifdef HAVE_save_stack_nonlocal
1614 if (HAVE_save_stack_nonlocal)
1615 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1616 else
1617 #endif
1618 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1619
1620 /* Allocate a block of memory onto the stack and copy the memory
1621 arguments to the outgoing arguments address. We can pass TRUE
1622 as the 4th argument because we just saved the stack pointer
1623 and will restore it right after the call. */
1624 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1625
1626 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1627 may have already set current_function_calls_alloca to true.
1628 current_function_calls_alloca won't be set if argsize is zero,
1629 so we have to guarantee need_drap is true here. */
1630 if (SUPPORTS_STACK_ALIGNMENT)
1631 crtl->need_drap = true;
1632
1633 dest = virtual_outgoing_args_rtx;
1634 #ifndef STACK_GROWS_DOWNWARD
1635 if (CONST_INT_P (argsize))
1636 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1637 else
1638 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1639 #endif
1640 dest = gen_rtx_MEM (BLKmode, dest);
1641 set_mem_align (dest, PARM_BOUNDARY);
1642 src = gen_rtx_MEM (BLKmode, incoming_args);
1643 set_mem_align (src, PARM_BOUNDARY);
1644 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1645
1646 /* Refer to the argument block. */
1647 apply_args_size ();
1648 arguments = gen_rtx_MEM (BLKmode, arguments);
1649 set_mem_align (arguments, PARM_BOUNDARY);
1650
1651 /* Walk past the arg-pointer and structure value address. */
1652 size = GET_MODE_SIZE (Pmode);
1653 if (struct_value)
1654 size += GET_MODE_SIZE (Pmode);
1655
1656 /* Restore each of the registers previously saved. Make USE insns
1657 for each of these registers for use in making the call. */
1658 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1659 if ((mode = apply_args_mode[regno]) != VOIDmode)
1660 {
1661 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1662 if (size % align != 0)
1663 size = CEIL (size, align) * align;
1664 reg = gen_rtx_REG (mode, regno);
1665 emit_move_insn (reg, adjust_address (arguments, mode, size));
1666 use_reg (&call_fusage, reg);
1667 size += GET_MODE_SIZE (mode);
1668 }
1669
1670 /* Restore the structure value address unless this is passed as an
1671 "invisible" first argument. */
1672 size = GET_MODE_SIZE (Pmode);
1673 if (struct_value)
1674 {
1675 rtx value = gen_reg_rtx (Pmode);
1676 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1677 emit_move_insn (struct_value, value);
1678 if (REG_P (struct_value))
1679 use_reg (&call_fusage, struct_value);
1680 size += GET_MODE_SIZE (Pmode);
1681 }
1682
1683 /* All arguments and registers used for the call are set up by now! */
1684 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1685
1686 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1687 and we don't want to load it into a register as an optimization,
1688 because prepare_call_address already did it if it should be done. */
1689 if (GET_CODE (function) != SYMBOL_REF)
1690 function = memory_address (FUNCTION_MODE, function);
1691
1692 /* Generate the actual call instruction and save the return value. */
1693 #ifdef HAVE_untyped_call
1694 if (HAVE_untyped_call)
1695 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1696 result, result_vector (1, result)));
1697 else
1698 #endif
1699 #ifdef HAVE_call_value
1700 if (HAVE_call_value)
1701 {
1702 rtx valreg = 0;
1703
1704 /* Locate the unique return register. It is not possible to
1705 express a call that sets more than one return register using
1706 call_value; use untyped_call for that. In fact, untyped_call
1707 only needs to save the return registers in the given block. */
1708 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1709 if ((mode = apply_result_mode[regno]) != VOIDmode)
1710 {
1711 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1712
1713 valreg = gen_rtx_REG (mode, regno);
1714 }
1715
1716 emit_call_insn (GEN_CALL_VALUE (valreg,
1717 gen_rtx_MEM (FUNCTION_MODE, function),
1718 const0_rtx, NULL_RTX, const0_rtx));
1719
1720 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1721 }
1722 else
1723 #endif
1724 gcc_unreachable ();
1725
1726 /* Find the CALL insn we just emitted, and attach the register usage
1727 information. */
1728 call_insn = last_call_insn ();
1729 add_function_usage_to (call_insn, call_fusage);
1730
1731 /* Restore the stack. */
1732 #ifdef HAVE_save_stack_nonlocal
1733 if (HAVE_save_stack_nonlocal)
1734 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1735 else
1736 #endif
1737 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1738 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1739
1740 OK_DEFER_POP;
1741
1742 /* Return the address of the result block. */
1743 result = copy_addr_to_reg (XEXP (result, 0));
1744 return convert_memory_address (ptr_mode, result);
1745 }
1746
1747 /* Perform an untyped return. */
1748
1749 static void
1750 expand_builtin_return (rtx result)
1751 {
1752 int size, align, regno;
1753 machine_mode mode;
1754 rtx reg;
1755 rtx_insn *call_fusage = 0;
1756
1757 result = convert_memory_address (Pmode, result);
1758
1759 apply_result_size ();
1760 result = gen_rtx_MEM (BLKmode, result);
1761
1762 #ifdef HAVE_untyped_return
1763 if (HAVE_untyped_return)
1764 {
1765 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1766 emit_barrier ();
1767 return;
1768 }
1769 #endif
1770
1771 /* Restore the return value and note that each value is used. */
1772 size = 0;
1773 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1774 if ((mode = apply_result_mode[regno]) != VOIDmode)
1775 {
1776 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1777 if (size % align != 0)
1778 size = CEIL (size, align) * align;
1779 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1780 emit_move_insn (reg, adjust_address (result, mode, size));
1781
1782 push_to_sequence (call_fusage);
1783 emit_use (reg);
1784 call_fusage = get_insns ();
1785 end_sequence ();
1786 size += GET_MODE_SIZE (mode);
1787 }
1788
1789 /* Put the USE insns before the return. */
1790 emit_insn (call_fusage);
1791
1792 /* Return whatever values was restored by jumping directly to the end
1793 of the function. */
1794 expand_naked_return ();
1795 }
1796
1797 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1798
1799 static enum type_class
1800 type_to_class (tree type)
1801 {
1802 switch (TREE_CODE (type))
1803 {
1804 case VOID_TYPE: return void_type_class;
1805 case INTEGER_TYPE: return integer_type_class;
1806 case ENUMERAL_TYPE: return enumeral_type_class;
1807 case BOOLEAN_TYPE: return boolean_type_class;
1808 case POINTER_TYPE: return pointer_type_class;
1809 case REFERENCE_TYPE: return reference_type_class;
1810 case OFFSET_TYPE: return offset_type_class;
1811 case REAL_TYPE: return real_type_class;
1812 case COMPLEX_TYPE: return complex_type_class;
1813 case FUNCTION_TYPE: return function_type_class;
1814 case METHOD_TYPE: return method_type_class;
1815 case RECORD_TYPE: return record_type_class;
1816 case UNION_TYPE:
1817 case QUAL_UNION_TYPE: return union_type_class;
1818 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1819 ? string_type_class : array_type_class);
1820 case LANG_TYPE: return lang_type_class;
1821 default: return no_type_class;
1822 }
1823 }
1824
1825 /* Expand a call EXP to __builtin_classify_type. */
1826
1827 static rtx
1828 expand_builtin_classify_type (tree exp)
1829 {
1830 if (call_expr_nargs (exp))
1831 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1832 return GEN_INT (no_type_class);
1833 }
1834
1835 /* This helper macro, meant to be used in mathfn_built_in below,
1836 determines which among a set of three builtin math functions is
1837 appropriate for a given type mode. The `F' and `L' cases are
1838 automatically generated from the `double' case. */
1839 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1840 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1841 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1842 fcodel = BUILT_IN_MATHFN##L ; break;
1843 /* Similar to above, but appends _R after any F/L suffix. */
1844 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1845 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1846 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1847 fcodel = BUILT_IN_MATHFN##L_R ; break;
1848
1849 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1850 if available. If IMPLICIT is true use the implicit builtin declaration,
1851 otherwise use the explicit declaration. If we can't do the conversion,
1852 return zero. */
1853
1854 static tree
1855 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1856 {
1857 enum built_in_function fcode, fcodef, fcodel, fcode2;
1858
1859 switch (fn)
1860 {
1861 CASE_MATHFN (BUILT_IN_ACOS)
1862 CASE_MATHFN (BUILT_IN_ACOSH)
1863 CASE_MATHFN (BUILT_IN_ASIN)
1864 CASE_MATHFN (BUILT_IN_ASINH)
1865 CASE_MATHFN (BUILT_IN_ATAN)
1866 CASE_MATHFN (BUILT_IN_ATAN2)
1867 CASE_MATHFN (BUILT_IN_ATANH)
1868 CASE_MATHFN (BUILT_IN_CBRT)
1869 CASE_MATHFN (BUILT_IN_CEIL)
1870 CASE_MATHFN (BUILT_IN_CEXPI)
1871 CASE_MATHFN (BUILT_IN_COPYSIGN)
1872 CASE_MATHFN (BUILT_IN_COS)
1873 CASE_MATHFN (BUILT_IN_COSH)
1874 CASE_MATHFN (BUILT_IN_DREM)
1875 CASE_MATHFN (BUILT_IN_ERF)
1876 CASE_MATHFN (BUILT_IN_ERFC)
1877 CASE_MATHFN (BUILT_IN_EXP)
1878 CASE_MATHFN (BUILT_IN_EXP10)
1879 CASE_MATHFN (BUILT_IN_EXP2)
1880 CASE_MATHFN (BUILT_IN_EXPM1)
1881 CASE_MATHFN (BUILT_IN_FABS)
1882 CASE_MATHFN (BUILT_IN_FDIM)
1883 CASE_MATHFN (BUILT_IN_FLOOR)
1884 CASE_MATHFN (BUILT_IN_FMA)
1885 CASE_MATHFN (BUILT_IN_FMAX)
1886 CASE_MATHFN (BUILT_IN_FMIN)
1887 CASE_MATHFN (BUILT_IN_FMOD)
1888 CASE_MATHFN (BUILT_IN_FREXP)
1889 CASE_MATHFN (BUILT_IN_GAMMA)
1890 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1891 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1892 CASE_MATHFN (BUILT_IN_HYPOT)
1893 CASE_MATHFN (BUILT_IN_ILOGB)
1894 CASE_MATHFN (BUILT_IN_ICEIL)
1895 CASE_MATHFN (BUILT_IN_IFLOOR)
1896 CASE_MATHFN (BUILT_IN_INF)
1897 CASE_MATHFN (BUILT_IN_IRINT)
1898 CASE_MATHFN (BUILT_IN_IROUND)
1899 CASE_MATHFN (BUILT_IN_ISINF)
1900 CASE_MATHFN (BUILT_IN_J0)
1901 CASE_MATHFN (BUILT_IN_J1)
1902 CASE_MATHFN (BUILT_IN_JN)
1903 CASE_MATHFN (BUILT_IN_LCEIL)
1904 CASE_MATHFN (BUILT_IN_LDEXP)
1905 CASE_MATHFN (BUILT_IN_LFLOOR)
1906 CASE_MATHFN (BUILT_IN_LGAMMA)
1907 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1908 CASE_MATHFN (BUILT_IN_LLCEIL)
1909 CASE_MATHFN (BUILT_IN_LLFLOOR)
1910 CASE_MATHFN (BUILT_IN_LLRINT)
1911 CASE_MATHFN (BUILT_IN_LLROUND)
1912 CASE_MATHFN (BUILT_IN_LOG)
1913 CASE_MATHFN (BUILT_IN_LOG10)
1914 CASE_MATHFN (BUILT_IN_LOG1P)
1915 CASE_MATHFN (BUILT_IN_LOG2)
1916 CASE_MATHFN (BUILT_IN_LOGB)
1917 CASE_MATHFN (BUILT_IN_LRINT)
1918 CASE_MATHFN (BUILT_IN_LROUND)
1919 CASE_MATHFN (BUILT_IN_MODF)
1920 CASE_MATHFN (BUILT_IN_NAN)
1921 CASE_MATHFN (BUILT_IN_NANS)
1922 CASE_MATHFN (BUILT_IN_NEARBYINT)
1923 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1924 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1925 CASE_MATHFN (BUILT_IN_POW)
1926 CASE_MATHFN (BUILT_IN_POWI)
1927 CASE_MATHFN (BUILT_IN_POW10)
1928 CASE_MATHFN (BUILT_IN_REMAINDER)
1929 CASE_MATHFN (BUILT_IN_REMQUO)
1930 CASE_MATHFN (BUILT_IN_RINT)
1931 CASE_MATHFN (BUILT_IN_ROUND)
1932 CASE_MATHFN (BUILT_IN_SCALB)
1933 CASE_MATHFN (BUILT_IN_SCALBLN)
1934 CASE_MATHFN (BUILT_IN_SCALBN)
1935 CASE_MATHFN (BUILT_IN_SIGNBIT)
1936 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1937 CASE_MATHFN (BUILT_IN_SIN)
1938 CASE_MATHFN (BUILT_IN_SINCOS)
1939 CASE_MATHFN (BUILT_IN_SINH)
1940 CASE_MATHFN (BUILT_IN_SQRT)
1941 CASE_MATHFN (BUILT_IN_TAN)
1942 CASE_MATHFN (BUILT_IN_TANH)
1943 CASE_MATHFN (BUILT_IN_TGAMMA)
1944 CASE_MATHFN (BUILT_IN_TRUNC)
1945 CASE_MATHFN (BUILT_IN_Y0)
1946 CASE_MATHFN (BUILT_IN_Y1)
1947 CASE_MATHFN (BUILT_IN_YN)
1948
1949 default:
1950 return NULL_TREE;
1951 }
1952
1953 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1954 fcode2 = fcode;
1955 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1956 fcode2 = fcodef;
1957 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1958 fcode2 = fcodel;
1959 else
1960 return NULL_TREE;
1961
1962 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1963 return NULL_TREE;
1964
1965 return builtin_decl_explicit (fcode2);
1966 }
1967
1968 /* Like mathfn_built_in_1(), but always use the implicit array. */
1969
1970 tree
1971 mathfn_built_in (tree type, enum built_in_function fn)
1972 {
1973 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1974 }
1975
1976 /* If errno must be maintained, expand the RTL to check if the result,
1977 TARGET, of a built-in function call, EXP, is NaN, and if so set
1978 errno to EDOM. */
1979
1980 static void
1981 expand_errno_check (tree exp, rtx target)
1982 {
1983 rtx_code_label *lab = gen_label_rtx ();
1984
1985 /* Test the result; if it is NaN, set errno=EDOM because
1986 the argument was not in the domain. */
1987 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1988 NULL_RTX, NULL_RTX, lab,
1989 /* The jump is very likely. */
1990 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1991
1992 #ifdef TARGET_EDOM
1993 /* If this built-in doesn't throw an exception, set errno directly. */
1994 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1995 {
1996 #ifdef GEN_ERRNO_RTX
1997 rtx errno_rtx = GEN_ERRNO_RTX;
1998 #else
1999 rtx errno_rtx
2000 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2001 #endif
2002 emit_move_insn (errno_rtx,
2003 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2004 emit_label (lab);
2005 return;
2006 }
2007 #endif
2008
2009 /* Make sure the library call isn't expanded as a tail call. */
2010 CALL_EXPR_TAILCALL (exp) = 0;
2011
2012 /* We can't set errno=EDOM directly; let the library call do it.
2013 Pop the arguments right away in case the call gets deleted. */
2014 NO_DEFER_POP;
2015 expand_call (exp, target, 0);
2016 OK_DEFER_POP;
2017 emit_label (lab);
2018 }
2019
2020 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2021 Return NULL_RTX if a normal call should be emitted rather than expanding
2022 the function in-line. EXP is the expression that is a call to the builtin
2023 function; if convenient, the result should be placed in TARGET.
2024 SUBTARGET may be used as the target for computing one of EXP's operands. */
2025
2026 static rtx
2027 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2028 {
2029 optab builtin_optab;
2030 rtx op0;
2031 rtx_insn *insns;
2032 tree fndecl = get_callee_fndecl (exp);
2033 machine_mode mode;
2034 bool errno_set = false;
2035 bool try_widening = false;
2036 tree arg;
2037
2038 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2039 return NULL_RTX;
2040
2041 arg = CALL_EXPR_ARG (exp, 0);
2042
2043 switch (DECL_FUNCTION_CODE (fndecl))
2044 {
2045 CASE_FLT_FN (BUILT_IN_SQRT):
2046 errno_set = ! tree_expr_nonnegative_p (arg);
2047 try_widening = true;
2048 builtin_optab = sqrt_optab;
2049 break;
2050 CASE_FLT_FN (BUILT_IN_EXP):
2051 errno_set = true; builtin_optab = exp_optab; break;
2052 CASE_FLT_FN (BUILT_IN_EXP10):
2053 CASE_FLT_FN (BUILT_IN_POW10):
2054 errno_set = true; builtin_optab = exp10_optab; break;
2055 CASE_FLT_FN (BUILT_IN_EXP2):
2056 errno_set = true; builtin_optab = exp2_optab; break;
2057 CASE_FLT_FN (BUILT_IN_EXPM1):
2058 errno_set = true; builtin_optab = expm1_optab; break;
2059 CASE_FLT_FN (BUILT_IN_LOGB):
2060 errno_set = true; builtin_optab = logb_optab; break;
2061 CASE_FLT_FN (BUILT_IN_LOG):
2062 errno_set = true; builtin_optab = log_optab; break;
2063 CASE_FLT_FN (BUILT_IN_LOG10):
2064 errno_set = true; builtin_optab = log10_optab; break;
2065 CASE_FLT_FN (BUILT_IN_LOG2):
2066 errno_set = true; builtin_optab = log2_optab; break;
2067 CASE_FLT_FN (BUILT_IN_LOG1P):
2068 errno_set = true; builtin_optab = log1p_optab; break;
2069 CASE_FLT_FN (BUILT_IN_ASIN):
2070 builtin_optab = asin_optab; break;
2071 CASE_FLT_FN (BUILT_IN_ACOS):
2072 builtin_optab = acos_optab; break;
2073 CASE_FLT_FN (BUILT_IN_TAN):
2074 builtin_optab = tan_optab; break;
2075 CASE_FLT_FN (BUILT_IN_ATAN):
2076 builtin_optab = atan_optab; break;
2077 CASE_FLT_FN (BUILT_IN_FLOOR):
2078 builtin_optab = floor_optab; break;
2079 CASE_FLT_FN (BUILT_IN_CEIL):
2080 builtin_optab = ceil_optab; break;
2081 CASE_FLT_FN (BUILT_IN_TRUNC):
2082 builtin_optab = btrunc_optab; break;
2083 CASE_FLT_FN (BUILT_IN_ROUND):
2084 builtin_optab = round_optab; break;
2085 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2086 builtin_optab = nearbyint_optab;
2087 if (flag_trapping_math)
2088 break;
2089 /* Else fallthrough and expand as rint. */
2090 CASE_FLT_FN (BUILT_IN_RINT):
2091 builtin_optab = rint_optab; break;
2092 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2093 builtin_optab = significand_optab; break;
2094 default:
2095 gcc_unreachable ();
2096 }
2097
2098 /* Make a suitable register to place result in. */
2099 mode = TYPE_MODE (TREE_TYPE (exp));
2100
2101 if (! flag_errno_math || ! HONOR_NANS (mode))
2102 errno_set = false;
2103
2104 /* Before working hard, check whether the instruction is available, but try
2105 to widen the mode for specific operations. */
2106 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2107 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2108 && (!errno_set || !optimize_insn_for_size_p ()))
2109 {
2110 rtx result = gen_reg_rtx (mode);
2111
2112 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2113 need to expand the argument again. This way, we will not perform
2114 side-effects more the once. */
2115 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2116
2117 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2118
2119 start_sequence ();
2120
2121 /* Compute into RESULT.
2122 Set RESULT to wherever the result comes back. */
2123 result = expand_unop (mode, builtin_optab, op0, result, 0);
2124
2125 if (result != 0)
2126 {
2127 if (errno_set)
2128 expand_errno_check (exp, result);
2129
2130 /* Output the entire sequence. */
2131 insns = get_insns ();
2132 end_sequence ();
2133 emit_insn (insns);
2134 return result;
2135 }
2136
2137 /* If we were unable to expand via the builtin, stop the sequence
2138 (without outputting the insns) and call to the library function
2139 with the stabilized argument list. */
2140 end_sequence ();
2141 }
2142
2143 return expand_call (exp, target, target == const0_rtx);
2144 }
2145
2146 /* Expand a call to the builtin binary math functions (pow and atan2).
2147 Return NULL_RTX if a normal call should be emitted rather than expanding the
2148 function in-line. EXP is the expression that is a call to the builtin
2149 function; if convenient, the result should be placed in TARGET.
2150 SUBTARGET may be used as the target for computing one of EXP's
2151 operands. */
2152
2153 static rtx
2154 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2155 {
2156 optab builtin_optab;
2157 rtx op0, op1, result;
2158 rtx_insn *insns;
2159 int op1_type = REAL_TYPE;
2160 tree fndecl = get_callee_fndecl (exp);
2161 tree arg0, arg1;
2162 machine_mode mode;
2163 bool errno_set = true;
2164
2165 switch (DECL_FUNCTION_CODE (fndecl))
2166 {
2167 CASE_FLT_FN (BUILT_IN_SCALBN):
2168 CASE_FLT_FN (BUILT_IN_SCALBLN):
2169 CASE_FLT_FN (BUILT_IN_LDEXP):
2170 op1_type = INTEGER_TYPE;
2171 default:
2172 break;
2173 }
2174
2175 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2176 return NULL_RTX;
2177
2178 arg0 = CALL_EXPR_ARG (exp, 0);
2179 arg1 = CALL_EXPR_ARG (exp, 1);
2180
2181 switch (DECL_FUNCTION_CODE (fndecl))
2182 {
2183 CASE_FLT_FN (BUILT_IN_POW):
2184 builtin_optab = pow_optab; break;
2185 CASE_FLT_FN (BUILT_IN_ATAN2):
2186 builtin_optab = atan2_optab; break;
2187 CASE_FLT_FN (BUILT_IN_SCALB):
2188 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2189 return 0;
2190 builtin_optab = scalb_optab; break;
2191 CASE_FLT_FN (BUILT_IN_SCALBN):
2192 CASE_FLT_FN (BUILT_IN_SCALBLN):
2193 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2194 return 0;
2195 /* Fall through... */
2196 CASE_FLT_FN (BUILT_IN_LDEXP):
2197 builtin_optab = ldexp_optab; break;
2198 CASE_FLT_FN (BUILT_IN_FMOD):
2199 builtin_optab = fmod_optab; break;
2200 CASE_FLT_FN (BUILT_IN_REMAINDER):
2201 CASE_FLT_FN (BUILT_IN_DREM):
2202 builtin_optab = remainder_optab; break;
2203 default:
2204 gcc_unreachable ();
2205 }
2206
2207 /* Make a suitable register to place result in. */
2208 mode = TYPE_MODE (TREE_TYPE (exp));
2209
2210 /* Before working hard, check whether the instruction is available. */
2211 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2212 return NULL_RTX;
2213
2214 result = gen_reg_rtx (mode);
2215
2216 if (! flag_errno_math || ! HONOR_NANS (mode))
2217 errno_set = false;
2218
2219 if (errno_set && optimize_insn_for_size_p ())
2220 return 0;
2221
2222 /* Always stabilize the argument list. */
2223 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2224 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2225
2226 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2227 op1 = expand_normal (arg1);
2228
2229 start_sequence ();
2230
2231 /* Compute into RESULT.
2232 Set RESULT to wherever the result comes back. */
2233 result = expand_binop (mode, builtin_optab, op0, op1,
2234 result, 0, OPTAB_DIRECT);
2235
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)
2240 {
2241 end_sequence ();
2242 return expand_call (exp, target, target == const0_rtx);
2243 }
2244
2245 if (errno_set)
2246 expand_errno_check (exp, result);
2247
2248 /* Output the entire sequence. */
2249 insns = get_insns ();
2250 end_sequence ();
2251 emit_insn (insns);
2252
2253 return result;
2254 }
2255
2256 /* Expand a call to the builtin trinary math functions (fma).
2257 Return NULL_RTX if a normal call should be emitted rather than expanding the
2258 function in-line. EXP is the expression that is a call to the builtin
2259 function; if convenient, the result should be placed in TARGET.
2260 SUBTARGET may be used as the target for computing one of EXP's
2261 operands. */
2262
2263 static rtx
2264 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2265 {
2266 optab builtin_optab;
2267 rtx op0, op1, op2, result;
2268 rtx_insn *insns;
2269 tree fndecl = get_callee_fndecl (exp);
2270 tree arg0, arg1, arg2;
2271 machine_mode mode;
2272
2273 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2274 return NULL_RTX;
2275
2276 arg0 = CALL_EXPR_ARG (exp, 0);
2277 arg1 = CALL_EXPR_ARG (exp, 1);
2278 arg2 = CALL_EXPR_ARG (exp, 2);
2279
2280 switch (DECL_FUNCTION_CODE (fndecl))
2281 {
2282 CASE_FLT_FN (BUILT_IN_FMA):
2283 builtin_optab = fma_optab; break;
2284 default:
2285 gcc_unreachable ();
2286 }
2287
2288 /* Make a suitable register to place result in. */
2289 mode = TYPE_MODE (TREE_TYPE (exp));
2290
2291 /* Before working hard, check whether the instruction is available. */
2292 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2293 return NULL_RTX;
2294
2295 result = gen_reg_rtx (mode);
2296
2297 /* Always stabilize the argument list. */
2298 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2299 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2300 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2301
2302 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2303 op1 = expand_normal (arg1);
2304 op2 = expand_normal (arg2);
2305
2306 start_sequence ();
2307
2308 /* Compute into RESULT.
2309 Set RESULT to wherever the result comes back. */
2310 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2311 result, 0);
2312
2313 /* If we were unable to expand via the builtin, stop the sequence
2314 (without outputting the insns) and call to the library function
2315 with the stabilized argument list. */
2316 if (result == 0)
2317 {
2318 end_sequence ();
2319 return expand_call (exp, target, target == const0_rtx);
2320 }
2321
2322 /* Output the entire sequence. */
2323 insns = get_insns ();
2324 end_sequence ();
2325 emit_insn (insns);
2326
2327 return result;
2328 }
2329
2330 /* Expand a call to the builtin sin and cos math functions.
2331 Return NULL_RTX if a normal call should be emitted rather than expanding the
2332 function in-line. EXP is the expression that is a call to the builtin
2333 function; if convenient, the result should be placed in TARGET.
2334 SUBTARGET may be used as the target for computing one of EXP's
2335 operands. */
2336
2337 static rtx
2338 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2339 {
2340 optab builtin_optab;
2341 rtx op0;
2342 rtx_insn *insns;
2343 tree fndecl = get_callee_fndecl (exp);
2344 machine_mode mode;
2345 tree arg;
2346
2347 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2348 return NULL_RTX;
2349
2350 arg = CALL_EXPR_ARG (exp, 0);
2351
2352 switch (DECL_FUNCTION_CODE (fndecl))
2353 {
2354 CASE_FLT_FN (BUILT_IN_SIN):
2355 CASE_FLT_FN (BUILT_IN_COS):
2356 builtin_optab = sincos_optab; break;
2357 default:
2358 gcc_unreachable ();
2359 }
2360
2361 /* Make a suitable register to place result in. */
2362 mode = TYPE_MODE (TREE_TYPE (exp));
2363
2364 /* Check if sincos insn is available, otherwise fallback
2365 to sin or cos insn. */
2366 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2367 switch (DECL_FUNCTION_CODE (fndecl))
2368 {
2369 CASE_FLT_FN (BUILT_IN_SIN):
2370 builtin_optab = sin_optab; break;
2371 CASE_FLT_FN (BUILT_IN_COS):
2372 builtin_optab = cos_optab; break;
2373 default:
2374 gcc_unreachable ();
2375 }
2376
2377 /* Before working hard, check whether the instruction is available. */
2378 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2379 {
2380 rtx result = gen_reg_rtx (mode);
2381
2382 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2383 need to expand the argument again. This way, we will not perform
2384 side-effects more the once. */
2385 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2386
2387 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2388
2389 start_sequence ();
2390
2391 /* Compute into RESULT.
2392 Set RESULT to wherever the result comes back. */
2393 if (builtin_optab == sincos_optab)
2394 {
2395 int ok;
2396
2397 switch (DECL_FUNCTION_CODE (fndecl))
2398 {
2399 CASE_FLT_FN (BUILT_IN_SIN):
2400 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2401 break;
2402 CASE_FLT_FN (BUILT_IN_COS):
2403 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2404 break;
2405 default:
2406 gcc_unreachable ();
2407 }
2408 gcc_assert (ok);
2409 }
2410 else
2411 result = expand_unop (mode, builtin_optab, op0, result, 0);
2412
2413 if (result != 0)
2414 {
2415 /* Output the entire sequence. */
2416 insns = get_insns ();
2417 end_sequence ();
2418 emit_insn (insns);
2419 return result;
2420 }
2421
2422 /* If we were unable to expand via the builtin, stop the sequence
2423 (without outputting the insns) and call to the library function
2424 with the stabilized argument list. */
2425 end_sequence ();
2426 }
2427
2428 return expand_call (exp, target, target == const0_rtx);
2429 }
2430
2431 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2432 return an RTL instruction code that implements the functionality.
2433 If that isn't possible or available return CODE_FOR_nothing. */
2434
2435 static enum insn_code
2436 interclass_mathfn_icode (tree arg, tree fndecl)
2437 {
2438 bool errno_set = false;
2439 optab builtin_optab = unknown_optab;
2440 machine_mode mode;
2441
2442 switch (DECL_FUNCTION_CODE (fndecl))
2443 {
2444 CASE_FLT_FN (BUILT_IN_ILOGB):
2445 errno_set = true; builtin_optab = ilogb_optab; break;
2446 CASE_FLT_FN (BUILT_IN_ISINF):
2447 builtin_optab = isinf_optab; break;
2448 case BUILT_IN_ISNORMAL:
2449 case BUILT_IN_ISFINITE:
2450 CASE_FLT_FN (BUILT_IN_FINITE):
2451 case BUILT_IN_FINITED32:
2452 case BUILT_IN_FINITED64:
2453 case BUILT_IN_FINITED128:
2454 case BUILT_IN_ISINFD32:
2455 case BUILT_IN_ISINFD64:
2456 case BUILT_IN_ISINFD128:
2457 /* These builtins have no optabs (yet). */
2458 break;
2459 default:
2460 gcc_unreachable ();
2461 }
2462
2463 /* There's no easy way to detect the case we need to set EDOM. */
2464 if (flag_errno_math && errno_set)
2465 return CODE_FOR_nothing;
2466
2467 /* Optab mode depends on the mode of the input argument. */
2468 mode = TYPE_MODE (TREE_TYPE (arg));
2469
2470 if (builtin_optab)
2471 return optab_handler (builtin_optab, mode);
2472 return CODE_FOR_nothing;
2473 }
2474
2475 /* Expand a call to one of the builtin math functions that operate on
2476 floating point argument and output an integer result (ilogb, isinf,
2477 isnan, etc).
2478 Return 0 if a normal call should be emitted rather than expanding the
2479 function in-line. EXP is the expression that is a call to the builtin
2480 function; if convenient, the result should be placed in TARGET. */
2481
2482 static rtx
2483 expand_builtin_interclass_mathfn (tree exp, rtx target)
2484 {
2485 enum insn_code icode = CODE_FOR_nothing;
2486 rtx op0;
2487 tree fndecl = get_callee_fndecl (exp);
2488 machine_mode mode;
2489 tree arg;
2490
2491 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2492 return NULL_RTX;
2493
2494 arg = CALL_EXPR_ARG (exp, 0);
2495 icode = interclass_mathfn_icode (arg, fndecl);
2496 mode = TYPE_MODE (TREE_TYPE (arg));
2497
2498 if (icode != CODE_FOR_nothing)
2499 {
2500 struct expand_operand ops[1];
2501 rtx_insn *last = get_last_insn ();
2502 tree orig_arg = arg;
2503
2504 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2505 need to expand the argument again. This way, we will not perform
2506 side-effects more the once. */
2507 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2508
2509 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2510
2511 if (mode != GET_MODE (op0))
2512 op0 = convert_to_mode (mode, op0, 0);
2513
2514 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2515 if (maybe_legitimize_operands (icode, 0, 1, ops)
2516 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2517 return ops[0].value;
2518
2519 delete_insns_since (last);
2520 CALL_EXPR_ARG (exp, 0) = orig_arg;
2521 }
2522
2523 return NULL_RTX;
2524 }
2525
2526 /* Expand a call to the builtin sincos math function.
2527 Return NULL_RTX if a normal call should be emitted rather than expanding the
2528 function in-line. EXP is the expression that is a call to the builtin
2529 function. */
2530
2531 static rtx
2532 expand_builtin_sincos (tree exp)
2533 {
2534 rtx op0, op1, op2, target1, target2;
2535 machine_mode mode;
2536 tree arg, sinp, cosp;
2537 int result;
2538 location_t loc = EXPR_LOCATION (exp);
2539 tree alias_type, alias_off;
2540
2541 if (!validate_arglist (exp, REAL_TYPE,
2542 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2543 return NULL_RTX;
2544
2545 arg = CALL_EXPR_ARG (exp, 0);
2546 sinp = CALL_EXPR_ARG (exp, 1);
2547 cosp = CALL_EXPR_ARG (exp, 2);
2548
2549 /* Make a suitable register to place result in. */
2550 mode = TYPE_MODE (TREE_TYPE (arg));
2551
2552 /* Check if sincos insn is available, otherwise emit the call. */
2553 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2554 return NULL_RTX;
2555
2556 target1 = gen_reg_rtx (mode);
2557 target2 = gen_reg_rtx (mode);
2558
2559 op0 = expand_normal (arg);
2560 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2561 alias_off = build_int_cst (alias_type, 0);
2562 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2563 sinp, alias_off));
2564 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2565 cosp, alias_off));
2566
2567 /* Compute into target1 and target2.
2568 Set TARGET to wherever the result comes back. */
2569 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2570 gcc_assert (result);
2571
2572 /* Move target1 and target2 to the memory locations indicated
2573 by op1 and op2. */
2574 emit_move_insn (op1, target1);
2575 emit_move_insn (op2, target2);
2576
2577 return const0_rtx;
2578 }
2579
2580 /* Expand a call to the internal cexpi builtin to the sincos math function.
2581 EXP is the expression that is a call to the builtin function; if convenient,
2582 the result should be placed in TARGET. */
2583
2584 static rtx
2585 expand_builtin_cexpi (tree exp, rtx target)
2586 {
2587 tree fndecl = get_callee_fndecl (exp);
2588 tree arg, type;
2589 machine_mode mode;
2590 rtx op0, op1, op2;
2591 location_t loc = EXPR_LOCATION (exp);
2592
2593 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2594 return NULL_RTX;
2595
2596 arg = CALL_EXPR_ARG (exp, 0);
2597 type = TREE_TYPE (arg);
2598 mode = TYPE_MODE (TREE_TYPE (arg));
2599
2600 /* Try expanding via a sincos optab, fall back to emitting a libcall
2601 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2602 is only generated from sincos, cexp or if we have either of them. */
2603 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2604 {
2605 op1 = gen_reg_rtx (mode);
2606 op2 = gen_reg_rtx (mode);
2607
2608 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2609
2610 /* Compute into op1 and op2. */
2611 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2612 }
2613 else if (targetm.libc_has_function (function_sincos))
2614 {
2615 tree call, fn = NULL_TREE;
2616 tree top1, top2;
2617 rtx op1a, op2a;
2618
2619 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2620 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2621 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2622 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2623 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2624 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2625 else
2626 gcc_unreachable ();
2627
2628 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2629 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2630 op1a = copy_addr_to_reg (XEXP (op1, 0));
2631 op2a = copy_addr_to_reg (XEXP (op2, 0));
2632 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2633 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2634
2635 /* Make sure not to fold the sincos call again. */
2636 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2637 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2638 call, 3, arg, top1, top2));
2639 }
2640 else
2641 {
2642 tree call, fn = NULL_TREE, narg;
2643 tree ctype = build_complex_type (type);
2644
2645 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2646 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2647 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2648 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2649 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2650 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2651 else
2652 gcc_unreachable ();
2653
2654 /* If we don't have a decl for cexp create one. This is the
2655 friendliest fallback if the user calls __builtin_cexpi
2656 without full target C99 function support. */
2657 if (fn == NULL_TREE)
2658 {
2659 tree fntype;
2660 const char *name = NULL;
2661
2662 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2663 name = "cexpf";
2664 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2665 name = "cexp";
2666 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2667 name = "cexpl";
2668
2669 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2670 fn = build_fn_decl (name, fntype);
2671 }
2672
2673 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2674 build_real (type, dconst0), arg);
2675
2676 /* Make sure not to fold the cexp call again. */
2677 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2678 return expand_expr (build_call_nary (ctype, call, 1, narg),
2679 target, VOIDmode, EXPAND_NORMAL);
2680 }
2681
2682 /* Now build the proper return type. */
2683 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2684 make_tree (TREE_TYPE (arg), op2),
2685 make_tree (TREE_TYPE (arg), op1)),
2686 target, VOIDmode, EXPAND_NORMAL);
2687 }
2688
2689 /* Conveniently construct a function call expression. FNDECL names the
2690 function to be called, N is the number of arguments, and the "..."
2691 parameters are the argument expressions. Unlike build_call_exr
2692 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2693
2694 static tree
2695 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2696 {
2697 va_list ap;
2698 tree fntype = TREE_TYPE (fndecl);
2699 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2700
2701 va_start (ap, n);
2702 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2703 va_end (ap);
2704 SET_EXPR_LOCATION (fn, loc);
2705 return fn;
2706 }
2707
2708 /* Expand a call to one of the builtin rounding functions gcc defines
2709 as an extension (lfloor and lceil). As these are gcc extensions we
2710 do not need to worry about setting errno to EDOM.
2711 If expanding via optab fails, lower expression to (int)(floor(x)).
2712 EXP is the expression that is a call to the builtin function;
2713 if convenient, the result should be placed in TARGET. */
2714
2715 static rtx
2716 expand_builtin_int_roundingfn (tree exp, rtx target)
2717 {
2718 convert_optab builtin_optab;
2719 rtx op0, tmp;
2720 rtx_insn *insns;
2721 tree fndecl = get_callee_fndecl (exp);
2722 enum built_in_function fallback_fn;
2723 tree fallback_fndecl;
2724 machine_mode mode;
2725 tree arg;
2726
2727 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2728 gcc_unreachable ();
2729
2730 arg = CALL_EXPR_ARG (exp, 0);
2731
2732 switch (DECL_FUNCTION_CODE (fndecl))
2733 {
2734 CASE_FLT_FN (BUILT_IN_ICEIL):
2735 CASE_FLT_FN (BUILT_IN_LCEIL):
2736 CASE_FLT_FN (BUILT_IN_LLCEIL):
2737 builtin_optab = lceil_optab;
2738 fallback_fn = BUILT_IN_CEIL;
2739 break;
2740
2741 CASE_FLT_FN (BUILT_IN_IFLOOR):
2742 CASE_FLT_FN (BUILT_IN_LFLOOR):
2743 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2744 builtin_optab = lfloor_optab;
2745 fallback_fn = BUILT_IN_FLOOR;
2746 break;
2747
2748 default:
2749 gcc_unreachable ();
2750 }
2751
2752 /* Make a suitable register to place result in. */
2753 mode = TYPE_MODE (TREE_TYPE (exp));
2754
2755 target = gen_reg_rtx (mode);
2756
2757 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2758 need to expand the argument again. This way, we will not perform
2759 side-effects more the once. */
2760 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2761
2762 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2763
2764 start_sequence ();
2765
2766 /* Compute into TARGET. */
2767 if (expand_sfix_optab (target, op0, builtin_optab))
2768 {
2769 /* Output the entire sequence. */
2770 insns = get_insns ();
2771 end_sequence ();
2772 emit_insn (insns);
2773 return target;
2774 }
2775
2776 /* If we were unable to expand via the builtin, stop the sequence
2777 (without outputting the insns). */
2778 end_sequence ();
2779
2780 /* Fall back to floating point rounding optab. */
2781 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2782
2783 /* For non-C99 targets we may end up without a fallback fndecl here
2784 if the user called __builtin_lfloor directly. In this case emit
2785 a call to the floor/ceil variants nevertheless. This should result
2786 in the best user experience for not full C99 targets. */
2787 if (fallback_fndecl == NULL_TREE)
2788 {
2789 tree fntype;
2790 const char *name = NULL;
2791
2792 switch (DECL_FUNCTION_CODE (fndecl))
2793 {
2794 case BUILT_IN_ICEIL:
2795 case BUILT_IN_LCEIL:
2796 case BUILT_IN_LLCEIL:
2797 name = "ceil";
2798 break;
2799 case BUILT_IN_ICEILF:
2800 case BUILT_IN_LCEILF:
2801 case BUILT_IN_LLCEILF:
2802 name = "ceilf";
2803 break;
2804 case BUILT_IN_ICEILL:
2805 case BUILT_IN_LCEILL:
2806 case BUILT_IN_LLCEILL:
2807 name = "ceill";
2808 break;
2809 case BUILT_IN_IFLOOR:
2810 case BUILT_IN_LFLOOR:
2811 case BUILT_IN_LLFLOOR:
2812 name = "floor";
2813 break;
2814 case BUILT_IN_IFLOORF:
2815 case BUILT_IN_LFLOORF:
2816 case BUILT_IN_LLFLOORF:
2817 name = "floorf";
2818 break;
2819 case BUILT_IN_IFLOORL:
2820 case BUILT_IN_LFLOORL:
2821 case BUILT_IN_LLFLOORL:
2822 name = "floorl";
2823 break;
2824 default:
2825 gcc_unreachable ();
2826 }
2827
2828 fntype = build_function_type_list (TREE_TYPE (arg),
2829 TREE_TYPE (arg), NULL_TREE);
2830 fallback_fndecl = build_fn_decl (name, fntype);
2831 }
2832
2833 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2834
2835 tmp = expand_normal (exp);
2836 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2837
2838 /* Truncate the result of floating point optab to integer
2839 via expand_fix (). */
2840 target = gen_reg_rtx (mode);
2841 expand_fix (target, tmp, 0);
2842
2843 return target;
2844 }
2845
2846 /* Expand a call to one of the builtin math functions doing integer
2847 conversion (lrint).
2848 Return 0 if a normal call should be emitted rather than expanding the
2849 function in-line. EXP is the expression that is a call to the builtin
2850 function; if convenient, the result should be placed in TARGET. */
2851
2852 static rtx
2853 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2854 {
2855 convert_optab builtin_optab;
2856 rtx op0;
2857 rtx_insn *insns;
2858 tree fndecl = get_callee_fndecl (exp);
2859 tree arg;
2860 machine_mode mode;
2861 enum built_in_function fallback_fn = BUILT_IN_NONE;
2862
2863 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2864 gcc_unreachable ();
2865
2866 arg = CALL_EXPR_ARG (exp, 0);
2867
2868 switch (DECL_FUNCTION_CODE (fndecl))
2869 {
2870 CASE_FLT_FN (BUILT_IN_IRINT):
2871 fallback_fn = BUILT_IN_LRINT;
2872 /* FALLTHRU */
2873 CASE_FLT_FN (BUILT_IN_LRINT):
2874 CASE_FLT_FN (BUILT_IN_LLRINT):
2875 builtin_optab = lrint_optab;
2876 break;
2877
2878 CASE_FLT_FN (BUILT_IN_IROUND):
2879 fallback_fn = BUILT_IN_LROUND;
2880 /* FALLTHRU */
2881 CASE_FLT_FN (BUILT_IN_LROUND):
2882 CASE_FLT_FN (BUILT_IN_LLROUND):
2883 builtin_optab = lround_optab;
2884 break;
2885
2886 default:
2887 gcc_unreachable ();
2888 }
2889
2890 /* There's no easy way to detect the case we need to set EDOM. */
2891 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2892 return NULL_RTX;
2893
2894 /* Make a suitable register to place result in. */
2895 mode = TYPE_MODE (TREE_TYPE (exp));
2896
2897 /* There's no easy way to detect the case we need to set EDOM. */
2898 if (!flag_errno_math)
2899 {
2900 rtx result = gen_reg_rtx (mode);
2901
2902 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2903 need to expand the argument again. This way, we will not perform
2904 side-effects more the once. */
2905 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2906
2907 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2908
2909 start_sequence ();
2910
2911 if (expand_sfix_optab (result, op0, builtin_optab))
2912 {
2913 /* Output the entire sequence. */
2914 insns = get_insns ();
2915 end_sequence ();
2916 emit_insn (insns);
2917 return result;
2918 }
2919
2920 /* If we were unable to expand via the builtin, stop the sequence
2921 (without outputting the insns) and call to the library function
2922 with the stabilized argument list. */
2923 end_sequence ();
2924 }
2925
2926 if (fallback_fn != BUILT_IN_NONE)
2927 {
2928 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2929 targets, (int) round (x) should never be transformed into
2930 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2931 a call to lround in the hope that the target provides at least some
2932 C99 functions. This should result in the best user experience for
2933 not full C99 targets. */
2934 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2935 fallback_fn, 0);
2936
2937 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2938 fallback_fndecl, 1, arg);
2939
2940 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2941 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2942 return convert_to_mode (mode, target, 0);
2943 }
2944
2945 return expand_call (exp, target, target == const0_rtx);
2946 }
2947
2948 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2949 a normal call should be emitted rather than expanding the function
2950 in-line. EXP is the expression that is a call to the builtin
2951 function; if convenient, the result should be placed in TARGET. */
2952
2953 static rtx
2954 expand_builtin_powi (tree exp, rtx target)
2955 {
2956 tree arg0, arg1;
2957 rtx op0, op1;
2958 machine_mode mode;
2959 machine_mode mode2;
2960
2961 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2962 return NULL_RTX;
2963
2964 arg0 = CALL_EXPR_ARG (exp, 0);
2965 arg1 = CALL_EXPR_ARG (exp, 1);
2966 mode = TYPE_MODE (TREE_TYPE (exp));
2967
2968 /* Emit a libcall to libgcc. */
2969
2970 /* Mode of the 2nd argument must match that of an int. */
2971 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2972
2973 if (target == NULL_RTX)
2974 target = gen_reg_rtx (mode);
2975
2976 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2977 if (GET_MODE (op0) != mode)
2978 op0 = convert_to_mode (mode, op0, 0);
2979 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2980 if (GET_MODE (op1) != mode2)
2981 op1 = convert_to_mode (mode2, op1, 0);
2982
2983 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2984 target, LCT_CONST, mode, 2,
2985 op0, mode, op1, mode2);
2986
2987 return target;
2988 }
2989
2990 /* Expand expression EXP which is a call to the strlen builtin. Return
2991 NULL_RTX if we failed the caller should emit a normal call, otherwise
2992 try to get the result in TARGET, if convenient. */
2993
2994 static rtx
2995 expand_builtin_strlen (tree exp, rtx target,
2996 machine_mode target_mode)
2997 {
2998 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2999 return NULL_RTX;
3000 else
3001 {
3002 struct expand_operand ops[4];
3003 rtx pat;
3004 tree len;
3005 tree src = CALL_EXPR_ARG (exp, 0);
3006 rtx src_reg;
3007 rtx_insn *before_strlen;
3008 machine_mode insn_mode = target_mode;
3009 enum insn_code icode = CODE_FOR_nothing;
3010 unsigned int align;
3011
3012 /* If the length can be computed at compile-time, return it. */
3013 len = c_strlen (src, 0);
3014 if (len)
3015 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3016
3017 /* If the length can be computed at compile-time and is constant
3018 integer, but there are side-effects in src, evaluate
3019 src for side-effects, then return len.
3020 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3021 can be optimized into: i++; x = 3; */
3022 len = c_strlen (src, 1);
3023 if (len && TREE_CODE (len) == INTEGER_CST)
3024 {
3025 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3026 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3027 }
3028
3029 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3030
3031 /* If SRC is not a pointer type, don't do this operation inline. */
3032 if (align == 0)
3033 return NULL_RTX;
3034
3035 /* Bail out if we can't compute strlen in the right mode. */
3036 while (insn_mode != VOIDmode)
3037 {
3038 icode = optab_handler (strlen_optab, insn_mode);
3039 if (icode != CODE_FOR_nothing)
3040 break;
3041
3042 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3043 }
3044 if (insn_mode == VOIDmode)
3045 return NULL_RTX;
3046
3047 /* Make a place to hold the source address. We will not expand
3048 the actual source until we are sure that the expansion will
3049 not fail -- there are trees that cannot be expanded twice. */
3050 src_reg = gen_reg_rtx (Pmode);
3051
3052 /* Mark the beginning of the strlen sequence so we can emit the
3053 source operand later. */
3054 before_strlen = get_last_insn ();
3055
3056 create_output_operand (&ops[0], target, insn_mode);
3057 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3058 create_integer_operand (&ops[2], 0);
3059 create_integer_operand (&ops[3], align);
3060 if (!maybe_expand_insn (icode, 4, ops))
3061 return NULL_RTX;
3062
3063 /* Now that we are assured of success, expand the source. */
3064 start_sequence ();
3065 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3066 if (pat != src_reg)
3067 {
3068 #ifdef POINTERS_EXTEND_UNSIGNED
3069 if (GET_MODE (pat) != Pmode)
3070 pat = convert_to_mode (Pmode, pat,
3071 POINTERS_EXTEND_UNSIGNED);
3072 #endif
3073 emit_move_insn (src_reg, pat);
3074 }
3075 pat = get_insns ();
3076 end_sequence ();
3077
3078 if (before_strlen)
3079 emit_insn_after (pat, before_strlen);
3080 else
3081 emit_insn_before (pat, get_insns ());
3082
3083 /* Return the value in the proper mode for this function. */
3084 if (GET_MODE (ops[0].value) == target_mode)
3085 target = ops[0].value;
3086 else if (target != 0)
3087 convert_move (target, ops[0].value, 0);
3088 else
3089 target = convert_to_mode (target_mode, ops[0].value, 0);
3090
3091 return target;
3092 }
3093 }
3094
3095 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3096 bytes from constant string DATA + OFFSET and return it as target
3097 constant. */
3098
3099 static rtx
3100 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3101 machine_mode mode)
3102 {
3103 const char *str = (const char *) data;
3104
3105 gcc_assert (offset >= 0
3106 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3107 <= strlen (str) + 1));
3108
3109 return c_readstr (str + offset, mode);
3110 }
3111
3112 /* LEN specify length of the block of memcpy/memset operation.
3113 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3114 In some cases we can make very likely guess on max size, then we
3115 set it into PROBABLE_MAX_SIZE. */
3116
3117 static void
3118 determine_block_size (tree len, rtx len_rtx,
3119 unsigned HOST_WIDE_INT *min_size,
3120 unsigned HOST_WIDE_INT *max_size,
3121 unsigned HOST_WIDE_INT *probable_max_size)
3122 {
3123 if (CONST_INT_P (len_rtx))
3124 {
3125 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3126 return;
3127 }
3128 else
3129 {
3130 wide_int min, max;
3131 enum value_range_type range_type = VR_UNDEFINED;
3132
3133 /* Determine bounds from the type. */
3134 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3135 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3136 else
3137 *min_size = 0;
3138 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3139 *probable_max_size = *max_size
3140 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3141 else
3142 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3143
3144 if (TREE_CODE (len) == SSA_NAME)
3145 range_type = get_range_info (len, &min, &max);
3146 if (range_type == VR_RANGE)
3147 {
3148 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3149 *min_size = min.to_uhwi ();
3150 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3151 *probable_max_size = *max_size = max.to_uhwi ();
3152 }
3153 else if (range_type == VR_ANTI_RANGE)
3154 {
3155 /* Anti range 0...N lets us to determine minimal size to N+1. */
3156 if (min == 0)
3157 {
3158 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3159 *min_size = max.to_uhwi () + 1;
3160 }
3161 /* Code like
3162
3163 int n;
3164 if (n < 100)
3165 memcpy (a, b, n)
3166
3167 Produce anti range allowing negative values of N. We still
3168 can use the information and make a guess that N is not negative.
3169 */
3170 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3171 *probable_max_size = min.to_uhwi () - 1;
3172 }
3173 }
3174 gcc_checking_assert (*max_size <=
3175 (unsigned HOST_WIDE_INT)
3176 GET_MODE_MASK (GET_MODE (len_rtx)));
3177 }
3178
3179 /* Expand a call EXP to the memcpy builtin.
3180 Return NULL_RTX if we failed, the caller should emit a normal call,
3181 otherwise try to get the result in TARGET, if convenient (and in
3182 mode MODE if that's convenient). */
3183
3184 static rtx
3185 expand_builtin_memcpy (tree exp, rtx target)
3186 {
3187 if (!validate_arglist (exp,
3188 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3189 return NULL_RTX;
3190 else
3191 {
3192 tree dest = CALL_EXPR_ARG (exp, 0);
3193 tree src = CALL_EXPR_ARG (exp, 1);
3194 tree len = CALL_EXPR_ARG (exp, 2);
3195 const char *src_str;
3196 unsigned int src_align = get_pointer_alignment (src);
3197 unsigned int dest_align = get_pointer_alignment (dest);
3198 rtx dest_mem, src_mem, dest_addr, len_rtx;
3199 HOST_WIDE_INT expected_size = -1;
3200 unsigned int expected_align = 0;
3201 unsigned HOST_WIDE_INT min_size;
3202 unsigned HOST_WIDE_INT max_size;
3203 unsigned HOST_WIDE_INT probable_max_size;
3204
3205 /* If DEST is not a pointer type, call the normal function. */
3206 if (dest_align == 0)
3207 return NULL_RTX;
3208
3209 /* If either SRC is not a pointer type, don't do this
3210 operation in-line. */
3211 if (src_align == 0)
3212 return NULL_RTX;
3213
3214 if (currently_expanding_gimple_stmt)
3215 stringop_block_profile (currently_expanding_gimple_stmt,
3216 &expected_align, &expected_size);
3217
3218 if (expected_align < dest_align)
3219 expected_align = dest_align;
3220 dest_mem = get_memory_rtx (dest, len);
3221 set_mem_align (dest_mem, dest_align);
3222 len_rtx = expand_normal (len);
3223 determine_block_size (len, len_rtx, &min_size, &max_size,
3224 &probable_max_size);
3225 src_str = c_getstr (src);
3226
3227 /* If SRC is a string constant and block move would be done
3228 by pieces, we can avoid loading the string from memory
3229 and only stored the computed constants. */
3230 if (src_str
3231 && CONST_INT_P (len_rtx)
3232 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3233 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3234 CONST_CAST (char *, src_str),
3235 dest_align, false))
3236 {
3237 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3238 builtin_memcpy_read_str,
3239 CONST_CAST (char *, src_str),
3240 dest_align, false, 0);
3241 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3242 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3243 return dest_mem;
3244 }
3245
3246 src_mem = get_memory_rtx (src, len);
3247 set_mem_align (src_mem, src_align);
3248
3249 /* Copy word part most expediently. */
3250 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3251 CALL_EXPR_TAILCALL (exp)
3252 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3253 expected_align, expected_size,
3254 min_size, max_size, probable_max_size);
3255
3256 if (dest_addr == 0)
3257 {
3258 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3259 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3260 }
3261 return dest_addr;
3262 }
3263 }
3264
3265 /* Expand a call EXP to the mempcpy builtin.
3266 Return NULL_RTX if we failed; the caller should emit a normal call,
3267 otherwise try to get the result in TARGET, if convenient (and in
3268 mode MODE if that's convenient). If ENDP is 0 return the
3269 destination pointer, if ENDP is 1 return the end pointer ala
3270 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3271 stpcpy. */
3272
3273 static rtx
3274 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3275 {
3276 if (!validate_arglist (exp,
3277 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3278 return NULL_RTX;
3279 else
3280 {
3281 tree dest = CALL_EXPR_ARG (exp, 0);
3282 tree src = CALL_EXPR_ARG (exp, 1);
3283 tree len = CALL_EXPR_ARG (exp, 2);
3284 return expand_builtin_mempcpy_args (dest, src, len,
3285 target, mode, /*endp=*/ 1);
3286 }
3287 }
3288
3289 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3290 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3291 so that this can also be called without constructing an actual CALL_EXPR.
3292 The other arguments and return value are the same as for
3293 expand_builtin_mempcpy. */
3294
3295 static rtx
3296 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3297 rtx target, machine_mode mode, int endp)
3298 {
3299 /* If return value is ignored, transform mempcpy into memcpy. */
3300 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3301 {
3302 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3303 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3304 dest, src, len);
3305 return expand_expr (result, target, mode, EXPAND_NORMAL);
3306 }
3307 else
3308 {
3309 const char *src_str;
3310 unsigned int src_align = get_pointer_alignment (src);
3311 unsigned int dest_align = get_pointer_alignment (dest);
3312 rtx dest_mem, src_mem, len_rtx;
3313
3314 /* If either SRC or DEST is not a pointer type, don't do this
3315 operation in-line. */
3316 if (dest_align == 0 || src_align == 0)
3317 return NULL_RTX;
3318
3319 /* If LEN is not constant, call the normal function. */
3320 if (! tree_fits_uhwi_p (len))
3321 return NULL_RTX;
3322
3323 len_rtx = expand_normal (len);
3324 src_str = c_getstr (src);
3325
3326 /* If SRC is a string constant and block move would be done
3327 by pieces, we can avoid loading the string from memory
3328 and only stored the computed constants. */
3329 if (src_str
3330 && CONST_INT_P (len_rtx)
3331 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3332 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3333 CONST_CAST (char *, src_str),
3334 dest_align, false))
3335 {
3336 dest_mem = get_memory_rtx (dest, len);
3337 set_mem_align (dest_mem, dest_align);
3338 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3339 builtin_memcpy_read_str,
3340 CONST_CAST (char *, src_str),
3341 dest_align, false, endp);
3342 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3343 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3344 return dest_mem;
3345 }
3346
3347 if (CONST_INT_P (len_rtx)
3348 && can_move_by_pieces (INTVAL (len_rtx),
3349 MIN (dest_align, src_align)))
3350 {
3351 dest_mem = get_memory_rtx (dest, len);
3352 set_mem_align (dest_mem, dest_align);
3353 src_mem = get_memory_rtx (src, len);
3354 set_mem_align (src_mem, src_align);
3355 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3356 MIN (dest_align, src_align), endp);
3357 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3358 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3359 return dest_mem;
3360 }
3361
3362 return NULL_RTX;
3363 }
3364 }
3365
3366 #ifndef HAVE_movstr
3367 # define HAVE_movstr 0
3368 # define CODE_FOR_movstr CODE_FOR_nothing
3369 #endif
3370
3371 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3372 we failed, the caller should emit a normal call, otherwise try to
3373 get the result in TARGET, if convenient. If ENDP is 0 return the
3374 destination pointer, if ENDP is 1 return the end pointer ala
3375 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3376 stpcpy. */
3377
3378 static rtx
3379 expand_movstr (tree dest, tree src, rtx target, int endp)
3380 {
3381 struct expand_operand ops[3];
3382 rtx dest_mem;
3383 rtx src_mem;
3384
3385 if (!HAVE_movstr)
3386 return NULL_RTX;
3387
3388 dest_mem = get_memory_rtx (dest, NULL);
3389 src_mem = get_memory_rtx (src, NULL);
3390 if (!endp)
3391 {
3392 target = force_reg (Pmode, XEXP (dest_mem, 0));
3393 dest_mem = replace_equiv_address (dest_mem, target);
3394 }
3395
3396 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3397 create_fixed_operand (&ops[1], dest_mem);
3398 create_fixed_operand (&ops[2], src_mem);
3399 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3400 return NULL_RTX;
3401
3402 if (endp && target != const0_rtx)
3403 {
3404 target = ops[0].value;
3405 /* movstr is supposed to set end to the address of the NUL
3406 terminator. If the caller requested a mempcpy-like return value,
3407 adjust it. */
3408 if (endp == 1)
3409 {
3410 rtx tem = plus_constant (GET_MODE (target),
3411 gen_lowpart (GET_MODE (target), target), 1);
3412 emit_move_insn (target, force_operand (tem, NULL_RTX));
3413 }
3414 }
3415 return target;
3416 }
3417
3418 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3419 NULL_RTX if we failed the caller should emit a normal call, otherwise
3420 try to get the result in TARGET, if convenient (and in mode MODE if that's
3421 convenient). */
3422
3423 static rtx
3424 expand_builtin_strcpy (tree exp, rtx target)
3425 {
3426 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3427 {
3428 tree dest = CALL_EXPR_ARG (exp, 0);
3429 tree src = CALL_EXPR_ARG (exp, 1);
3430 return expand_builtin_strcpy_args (dest, src, target);
3431 }
3432 return NULL_RTX;
3433 }
3434
3435 /* Helper function to do the actual work for expand_builtin_strcpy. The
3436 arguments to the builtin_strcpy call DEST and SRC are broken out
3437 so that this can also be called without constructing an actual CALL_EXPR.
3438 The other arguments and return value are the same as for
3439 expand_builtin_strcpy. */
3440
3441 static rtx
3442 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3443 {
3444 return expand_movstr (dest, src, target, /*endp=*/0);
3445 }
3446
3447 /* Expand a call EXP to the stpcpy builtin.
3448 Return NULL_RTX if we failed the caller should emit a normal call,
3449 otherwise try to get the result in TARGET, if convenient (and in
3450 mode MODE if that's convenient). */
3451
3452 static rtx
3453 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3454 {
3455 tree dst, src;
3456 location_t loc = EXPR_LOCATION (exp);
3457
3458 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3459 return NULL_RTX;
3460
3461 dst = CALL_EXPR_ARG (exp, 0);
3462 src = CALL_EXPR_ARG (exp, 1);
3463
3464 /* If return value is ignored, transform stpcpy into strcpy. */
3465 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3466 {
3467 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3468 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3469 return expand_expr (result, target, mode, EXPAND_NORMAL);
3470 }
3471 else
3472 {
3473 tree len, lenp1;
3474 rtx ret;
3475
3476 /* Ensure we get an actual string whose length can be evaluated at
3477 compile-time, not an expression containing a string. This is
3478 because the latter will potentially produce pessimized code
3479 when used to produce the return value. */
3480 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3481 return expand_movstr (dst, src, target, /*endp=*/2);
3482
3483 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3484 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3485 target, mode, /*endp=*/2);
3486
3487 if (ret)
3488 return ret;
3489
3490 if (TREE_CODE (len) == INTEGER_CST)
3491 {
3492 rtx len_rtx = expand_normal (len);
3493
3494 if (CONST_INT_P (len_rtx))
3495 {
3496 ret = expand_builtin_strcpy_args (dst, src, target);
3497
3498 if (ret)
3499 {
3500 if (! target)
3501 {
3502 if (mode != VOIDmode)
3503 target = gen_reg_rtx (mode);
3504 else
3505 target = gen_reg_rtx (GET_MODE (ret));
3506 }
3507 if (GET_MODE (target) != GET_MODE (ret))
3508 ret = gen_lowpart (GET_MODE (target), ret);
3509
3510 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3511 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3512 gcc_assert (ret);
3513
3514 return target;
3515 }
3516 }
3517 }
3518
3519 return expand_movstr (dst, src, target, /*endp=*/2);
3520 }
3521 }
3522
3523 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3524 bytes from constant string DATA + OFFSET and return it as target
3525 constant. */
3526
3527 rtx
3528 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3529 machine_mode mode)
3530 {
3531 const char *str = (const char *) data;
3532
3533 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3534 return const0_rtx;
3535
3536 return c_readstr (str + offset, mode);
3537 }
3538
3539 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3540 NULL_RTX if we failed the caller should emit a normal call. */
3541
3542 static rtx
3543 expand_builtin_strncpy (tree exp, rtx target)
3544 {
3545 location_t loc = EXPR_LOCATION (exp);
3546
3547 if (validate_arglist (exp,
3548 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3549 {
3550 tree dest = CALL_EXPR_ARG (exp, 0);
3551 tree src = CALL_EXPR_ARG (exp, 1);
3552 tree len = CALL_EXPR_ARG (exp, 2);
3553 tree slen = c_strlen (src, 1);
3554
3555 /* We must be passed a constant len and src parameter. */
3556 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3557 return NULL_RTX;
3558
3559 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3560
3561 /* We're required to pad with trailing zeros if the requested
3562 len is greater than strlen(s2)+1. In that case try to
3563 use store_by_pieces, if it fails, punt. */
3564 if (tree_int_cst_lt (slen, len))
3565 {
3566 unsigned int dest_align = get_pointer_alignment (dest);
3567 const char *p = c_getstr (src);
3568 rtx dest_mem;
3569
3570 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3571 || !can_store_by_pieces (tree_to_uhwi (len),
3572 builtin_strncpy_read_str,
3573 CONST_CAST (char *, p),
3574 dest_align, false))
3575 return NULL_RTX;
3576
3577 dest_mem = get_memory_rtx (dest, len);
3578 store_by_pieces (dest_mem, tree_to_uhwi (len),
3579 builtin_strncpy_read_str,
3580 CONST_CAST (char *, p), dest_align, false, 0);
3581 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3582 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3583 return dest_mem;
3584 }
3585 }
3586 return NULL_RTX;
3587 }
3588
3589 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3590 bytes from constant string DATA + OFFSET and return it as target
3591 constant. */
3592
3593 rtx
3594 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3595 machine_mode mode)
3596 {
3597 const char *c = (const char *) data;
3598 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3599
3600 memset (p, *c, GET_MODE_SIZE (mode));
3601
3602 return c_readstr (p, mode);
3603 }
3604
3605 /* Callback routine for store_by_pieces. Return the RTL of a register
3606 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3607 char value given in the RTL register data. For example, if mode is
3608 4 bytes wide, return the RTL for 0x01010101*data. */
3609
3610 static rtx
3611 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3612 machine_mode mode)
3613 {
3614 rtx target, coeff;
3615 size_t size;
3616 char *p;
3617
3618 size = GET_MODE_SIZE (mode);
3619 if (size == 1)
3620 return (rtx) data;
3621
3622 p = XALLOCAVEC (char, size);
3623 memset (p, 1, size);
3624 coeff = c_readstr (p, mode);
3625
3626 target = convert_to_mode (mode, (rtx) data, 1);
3627 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3628 return force_reg (mode, target);
3629 }
3630
3631 /* Expand expression EXP, which is a call to the memset builtin. Return
3632 NULL_RTX if we failed the caller should emit a normal call, otherwise
3633 try to get the result in TARGET, if convenient (and in mode MODE if that's
3634 convenient). */
3635
3636 static rtx
3637 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3638 {
3639 if (!validate_arglist (exp,
3640 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3641 return NULL_RTX;
3642 else
3643 {
3644 tree dest = CALL_EXPR_ARG (exp, 0);
3645 tree val = CALL_EXPR_ARG (exp, 1);
3646 tree len = CALL_EXPR_ARG (exp, 2);
3647 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3648 }
3649 }
3650
3651 /* Helper function to do the actual work for expand_builtin_memset. The
3652 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3653 so that this can also be called without constructing an actual CALL_EXPR.
3654 The other arguments and return value are the same as for
3655 expand_builtin_memset. */
3656
3657 static rtx
3658 expand_builtin_memset_args (tree dest, tree val, tree len,
3659 rtx target, machine_mode mode, tree orig_exp)
3660 {
3661 tree fndecl, fn;
3662 enum built_in_function fcode;
3663 machine_mode val_mode;
3664 char c;
3665 unsigned int dest_align;
3666 rtx dest_mem, dest_addr, len_rtx;
3667 HOST_WIDE_INT expected_size = -1;
3668 unsigned int expected_align = 0;
3669 unsigned HOST_WIDE_INT min_size;
3670 unsigned HOST_WIDE_INT max_size;
3671 unsigned HOST_WIDE_INT probable_max_size;
3672
3673 dest_align = get_pointer_alignment (dest);
3674
3675 /* If DEST is not a pointer type, don't do this operation in-line. */
3676 if (dest_align == 0)
3677 return NULL_RTX;
3678
3679 if (currently_expanding_gimple_stmt)
3680 stringop_block_profile (currently_expanding_gimple_stmt,
3681 &expected_align, &expected_size);
3682
3683 if (expected_align < dest_align)
3684 expected_align = dest_align;
3685
3686 /* If the LEN parameter is zero, return DEST. */
3687 if (integer_zerop (len))
3688 {
3689 /* Evaluate and ignore VAL in case it has side-effects. */
3690 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3691 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3692 }
3693
3694 /* Stabilize the arguments in case we fail. */
3695 dest = builtin_save_expr (dest);
3696 val = builtin_save_expr (val);
3697 len = builtin_save_expr (len);
3698
3699 len_rtx = expand_normal (len);
3700 determine_block_size (len, len_rtx, &min_size, &max_size,
3701 &probable_max_size);
3702 dest_mem = get_memory_rtx (dest, len);
3703 val_mode = TYPE_MODE (unsigned_char_type_node);
3704
3705 if (TREE_CODE (val) != INTEGER_CST)
3706 {
3707 rtx val_rtx;
3708
3709 val_rtx = expand_normal (val);
3710 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3711
3712 /* Assume that we can memset by pieces if we can store
3713 * the coefficients by pieces (in the required modes).
3714 * We can't pass builtin_memset_gen_str as that emits RTL. */
3715 c = 1;
3716 if (tree_fits_uhwi_p (len)
3717 && can_store_by_pieces (tree_to_uhwi (len),
3718 builtin_memset_read_str, &c, dest_align,
3719 true))
3720 {
3721 val_rtx = force_reg (val_mode, val_rtx);
3722 store_by_pieces (dest_mem, tree_to_uhwi (len),
3723 builtin_memset_gen_str, val_rtx, dest_align,
3724 true, 0);
3725 }
3726 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3727 dest_align, expected_align,
3728 expected_size, min_size, max_size,
3729 probable_max_size))
3730 goto do_libcall;
3731
3732 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3733 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3734 return dest_mem;
3735 }
3736
3737 if (target_char_cast (val, &c))
3738 goto do_libcall;
3739
3740 if (c)
3741 {
3742 if (tree_fits_uhwi_p (len)
3743 && can_store_by_pieces (tree_to_uhwi (len),
3744 builtin_memset_read_str, &c, dest_align,
3745 true))
3746 store_by_pieces (dest_mem, tree_to_uhwi (len),
3747 builtin_memset_read_str, &c, dest_align, true, 0);
3748 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3749 gen_int_mode (c, val_mode),
3750 dest_align, expected_align,
3751 expected_size, min_size, max_size,
3752 probable_max_size))
3753 goto do_libcall;
3754
3755 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3756 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3757 return dest_mem;
3758 }
3759
3760 set_mem_align (dest_mem, dest_align);
3761 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3762 CALL_EXPR_TAILCALL (orig_exp)
3763 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3764 expected_align, expected_size,
3765 min_size, max_size,
3766 probable_max_size);
3767
3768 if (dest_addr == 0)
3769 {
3770 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3771 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3772 }
3773
3774 return dest_addr;
3775
3776 do_libcall:
3777 fndecl = get_callee_fndecl (orig_exp);
3778 fcode = DECL_FUNCTION_CODE (fndecl);
3779 if (fcode == BUILT_IN_MEMSET)
3780 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3781 dest, val, len);
3782 else if (fcode == BUILT_IN_BZERO)
3783 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3784 dest, len);
3785 else
3786 gcc_unreachable ();
3787 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3788 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3789 return expand_call (fn, target, target == const0_rtx);
3790 }
3791
3792 /* Expand expression EXP, which is a call to the bzero builtin. Return
3793 NULL_RTX if we failed the caller should emit a normal call. */
3794
3795 static rtx
3796 expand_builtin_bzero (tree exp)
3797 {
3798 tree dest, size;
3799 location_t loc = EXPR_LOCATION (exp);
3800
3801 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3802 return NULL_RTX;
3803
3804 dest = CALL_EXPR_ARG (exp, 0);
3805 size = CALL_EXPR_ARG (exp, 1);
3806
3807 /* New argument list transforming bzero(ptr x, int y) to
3808 memset(ptr x, int 0, size_t y). This is done this way
3809 so that if it isn't expanded inline, we fallback to
3810 calling bzero instead of memset. */
3811
3812 return expand_builtin_memset_args (dest, integer_zero_node,
3813 fold_convert_loc (loc,
3814 size_type_node, size),
3815 const0_rtx, VOIDmode, exp);
3816 }
3817
3818 /* Expand expression EXP, which is a call to the memcmp built-in function.
3819 Return NULL_RTX if we failed and the caller should emit a normal call,
3820 otherwise try to get the result in TARGET, if convenient (and in mode
3821 MODE, if that's convenient). */
3822
3823 static rtx
3824 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3825 ATTRIBUTE_UNUSED machine_mode mode)
3826 {
3827 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3828
3829 if (!validate_arglist (exp,
3830 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3831 return NULL_RTX;
3832
3833 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3834 implementing memcmp because it will stop if it encounters two
3835 zero bytes. */
3836 #if defined HAVE_cmpmemsi
3837 {
3838 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3839 rtx result;
3840 rtx insn;
3841 tree arg1 = CALL_EXPR_ARG (exp, 0);
3842 tree arg2 = CALL_EXPR_ARG (exp, 1);
3843 tree len = CALL_EXPR_ARG (exp, 2);
3844
3845 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3846 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3847 machine_mode insn_mode;
3848
3849 if (HAVE_cmpmemsi)
3850 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3851 else
3852 return NULL_RTX;
3853
3854 /* If we don't have POINTER_TYPE, call the function. */
3855 if (arg1_align == 0 || arg2_align == 0)
3856 return NULL_RTX;
3857
3858 /* Make a place to write the result of the instruction. */
3859 result = target;
3860 if (! (result != 0
3861 && REG_P (result) && GET_MODE (result) == insn_mode
3862 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3863 result = gen_reg_rtx (insn_mode);
3864
3865 arg1_rtx = get_memory_rtx (arg1, len);
3866 arg2_rtx = get_memory_rtx (arg2, len);
3867 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3868
3869 /* Set MEM_SIZE as appropriate. */
3870 if (CONST_INT_P (arg3_rtx))
3871 {
3872 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3873 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3874 }
3875
3876 if (HAVE_cmpmemsi)
3877 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3878 GEN_INT (MIN (arg1_align, arg2_align)));
3879 else
3880 gcc_unreachable ();
3881
3882 if (insn)
3883 emit_insn (insn);
3884 else
3885 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3886 TYPE_MODE (integer_type_node), 3,
3887 XEXP (arg1_rtx, 0), Pmode,
3888 XEXP (arg2_rtx, 0), Pmode,
3889 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3890 TYPE_UNSIGNED (sizetype)),
3891 TYPE_MODE (sizetype));
3892
3893 /* Return the value in the proper mode for this function. */
3894 mode = TYPE_MODE (TREE_TYPE (exp));
3895 if (GET_MODE (result) == mode)
3896 return result;
3897 else if (target != 0)
3898 {
3899 convert_move (target, result, 0);
3900 return target;
3901 }
3902 else
3903 return convert_to_mode (mode, result, 0);
3904 }
3905 #endif /* HAVE_cmpmemsi. */
3906
3907 return NULL_RTX;
3908 }
3909
3910 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3911 if we failed the caller should emit a normal call, otherwise try to get
3912 the result in TARGET, if convenient. */
3913
3914 static rtx
3915 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3916 {
3917 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3918 return NULL_RTX;
3919
3920 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3921 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3922 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3923 {
3924 rtx arg1_rtx, arg2_rtx;
3925 rtx result, insn = NULL_RTX;
3926 tree fndecl, fn;
3927 tree arg1 = CALL_EXPR_ARG (exp, 0);
3928 tree arg2 = CALL_EXPR_ARG (exp, 1);
3929
3930 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3931 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3932
3933 /* If we don't have POINTER_TYPE, call the function. */
3934 if (arg1_align == 0 || arg2_align == 0)
3935 return NULL_RTX;
3936
3937 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3938 arg1 = builtin_save_expr (arg1);
3939 arg2 = builtin_save_expr (arg2);
3940
3941 arg1_rtx = get_memory_rtx (arg1, NULL);
3942 arg2_rtx = get_memory_rtx (arg2, NULL);
3943
3944 #ifdef HAVE_cmpstrsi
3945 /* Try to call cmpstrsi. */
3946 if (HAVE_cmpstrsi)
3947 {
3948 machine_mode insn_mode
3949 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3950
3951 /* Make a place to write the result of the instruction. */
3952 result = target;
3953 if (! (result != 0
3954 && REG_P (result) && GET_MODE (result) == insn_mode
3955 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3956 result = gen_reg_rtx (insn_mode);
3957
3958 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3959 GEN_INT (MIN (arg1_align, arg2_align)));
3960 }
3961 #endif
3962 #ifdef HAVE_cmpstrnsi
3963 /* Try to determine at least one length and call cmpstrnsi. */
3964 if (!insn && HAVE_cmpstrnsi)
3965 {
3966 tree len;
3967 rtx arg3_rtx;
3968
3969 machine_mode insn_mode
3970 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3971 tree len1 = c_strlen (arg1, 1);
3972 tree len2 = c_strlen (arg2, 1);
3973
3974 if (len1)
3975 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3976 if (len2)
3977 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3978
3979 /* If we don't have a constant length for the first, use the length
3980 of the second, if we know it. We don't require a constant for
3981 this case; some cost analysis could be done if both are available
3982 but neither is constant. For now, assume they're equally cheap,
3983 unless one has side effects. If both strings have constant lengths,
3984 use the smaller. */
3985
3986 if (!len1)
3987 len = len2;
3988 else if (!len2)
3989 len = len1;
3990 else if (TREE_SIDE_EFFECTS (len1))
3991 len = len2;
3992 else if (TREE_SIDE_EFFECTS (len2))
3993 len = len1;
3994 else if (TREE_CODE (len1) != INTEGER_CST)
3995 len = len2;
3996 else if (TREE_CODE (len2) != INTEGER_CST)
3997 len = len1;
3998 else if (tree_int_cst_lt (len1, len2))
3999 len = len1;
4000 else
4001 len = len2;
4002
4003 /* If both arguments have side effects, we cannot optimize. */
4004 if (!len || TREE_SIDE_EFFECTS (len))
4005 goto do_libcall;
4006
4007 arg3_rtx = expand_normal (len);
4008
4009 /* Make a place to write the result of the instruction. */
4010 result = target;
4011 if (! (result != 0
4012 && REG_P (result) && GET_MODE (result) == insn_mode
4013 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4014 result = gen_reg_rtx (insn_mode);
4015
4016 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4017 GEN_INT (MIN (arg1_align, arg2_align)));
4018 }
4019 #endif
4020
4021 if (insn)
4022 {
4023 machine_mode mode;
4024 emit_insn (insn);
4025
4026 /* Return the value in the proper mode for this function. */
4027 mode = TYPE_MODE (TREE_TYPE (exp));
4028 if (GET_MODE (result) == mode)
4029 return result;
4030 if (target == 0)
4031 return convert_to_mode (mode, result, 0);
4032 convert_move (target, result, 0);
4033 return target;
4034 }
4035
4036 /* Expand the library call ourselves using a stabilized argument
4037 list to avoid re-evaluating the function's arguments twice. */
4038 #ifdef HAVE_cmpstrnsi
4039 do_libcall:
4040 #endif
4041 fndecl = get_callee_fndecl (exp);
4042 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4043 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4044 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4045 return expand_call (fn, target, target == const0_rtx);
4046 }
4047 #endif
4048 return NULL_RTX;
4049 }
4050
4051 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4052 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4053 the result in TARGET, if convenient. */
4054
4055 static rtx
4056 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4057 ATTRIBUTE_UNUSED machine_mode mode)
4058 {
4059 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4060
4061 if (!validate_arglist (exp,
4062 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4063 return NULL_RTX;
4064
4065 /* If c_strlen can determine an expression for one of the string
4066 lengths, and it doesn't have side effects, then emit cmpstrnsi
4067 using length MIN(strlen(string)+1, arg3). */
4068 #ifdef HAVE_cmpstrnsi
4069 if (HAVE_cmpstrnsi)
4070 {
4071 tree len, len1, len2;
4072 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4073 rtx result, insn;
4074 tree fndecl, fn;
4075 tree arg1 = CALL_EXPR_ARG (exp, 0);
4076 tree arg2 = CALL_EXPR_ARG (exp, 1);
4077 tree arg3 = CALL_EXPR_ARG (exp, 2);
4078
4079 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4080 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4081 machine_mode insn_mode
4082 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4083
4084 len1 = c_strlen (arg1, 1);
4085 len2 = c_strlen (arg2, 1);
4086
4087 if (len1)
4088 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4089 if (len2)
4090 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4091
4092 /* If we don't have a constant length for the first, use the length
4093 of the second, if we know it. We don't require a constant for
4094 this case; some cost analysis could be done if both are available
4095 but neither is constant. For now, assume they're equally cheap,
4096 unless one has side effects. If both strings have constant lengths,
4097 use the smaller. */
4098
4099 if (!len1)
4100 len = len2;
4101 else if (!len2)
4102 len = len1;
4103 else if (TREE_SIDE_EFFECTS (len1))
4104 len = len2;
4105 else if (TREE_SIDE_EFFECTS (len2))
4106 len = len1;
4107 else if (TREE_CODE (len1) != INTEGER_CST)
4108 len = len2;
4109 else if (TREE_CODE (len2) != INTEGER_CST)
4110 len = len1;
4111 else if (tree_int_cst_lt (len1, len2))
4112 len = len1;
4113 else
4114 len = len2;
4115
4116 /* If both arguments have side effects, we cannot optimize. */
4117 if (!len || TREE_SIDE_EFFECTS (len))
4118 return NULL_RTX;
4119
4120 /* The actual new length parameter is MIN(len,arg3). */
4121 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4122 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4123
4124 /* If we don't have POINTER_TYPE, call the function. */
4125 if (arg1_align == 0 || arg2_align == 0)
4126 return NULL_RTX;
4127
4128 /* Make a place to write the result of the instruction. */
4129 result = target;
4130 if (! (result != 0
4131 && REG_P (result) && GET_MODE (result) == insn_mode
4132 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4133 result = gen_reg_rtx (insn_mode);
4134
4135 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4136 arg1 = builtin_save_expr (arg1);
4137 arg2 = builtin_save_expr (arg2);
4138 len = builtin_save_expr (len);
4139
4140 arg1_rtx = get_memory_rtx (arg1, len);
4141 arg2_rtx = get_memory_rtx (arg2, len);
4142 arg3_rtx = expand_normal (len);
4143 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4144 GEN_INT (MIN (arg1_align, arg2_align)));
4145 if (insn)
4146 {
4147 emit_insn (insn);
4148
4149 /* Return the value in the proper mode for this function. */
4150 mode = TYPE_MODE (TREE_TYPE (exp));
4151 if (GET_MODE (result) == mode)
4152 return result;
4153 if (target == 0)
4154 return convert_to_mode (mode, result, 0);
4155 convert_move (target, result, 0);
4156 return target;
4157 }
4158
4159 /* Expand the library call ourselves using a stabilized argument
4160 list to avoid re-evaluating the function's arguments twice. */
4161 fndecl = get_callee_fndecl (exp);
4162 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4163 arg1, arg2, len);
4164 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4165 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4166 return expand_call (fn, target, target == const0_rtx);
4167 }
4168 #endif
4169 return NULL_RTX;
4170 }
4171
4172 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4173 if that's convenient. */
4174
4175 rtx
4176 expand_builtin_saveregs (void)
4177 {
4178 rtx val;
4179 rtx_insn *seq;
4180
4181 /* Don't do __builtin_saveregs more than once in a function.
4182 Save the result of the first call and reuse it. */
4183 if (saveregs_value != 0)
4184 return saveregs_value;
4185
4186 /* When this function is called, it means that registers must be
4187 saved on entry to this function. So we migrate the call to the
4188 first insn of this function. */
4189
4190 start_sequence ();
4191
4192 /* Do whatever the machine needs done in this case. */
4193 val = targetm.calls.expand_builtin_saveregs ();
4194
4195 seq = get_insns ();
4196 end_sequence ();
4197
4198 saveregs_value = val;
4199
4200 /* Put the insns after the NOTE that starts the function. If this
4201 is inside a start_sequence, make the outer-level insn chain current, so
4202 the code is placed at the start of the function. */
4203 push_topmost_sequence ();
4204 emit_insn_after (seq, entry_of_function ());
4205 pop_topmost_sequence ();
4206
4207 return val;
4208 }
4209
4210 /* Expand a call to __builtin_next_arg. */
4211
4212 static rtx
4213 expand_builtin_next_arg (void)
4214 {
4215 /* Checking arguments is already done in fold_builtin_next_arg
4216 that must be called before this function. */
4217 return expand_binop (ptr_mode, add_optab,
4218 crtl->args.internal_arg_pointer,
4219 crtl->args.arg_offset_rtx,
4220 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4221 }
4222
4223 /* Make it easier for the backends by protecting the valist argument
4224 from multiple evaluations. */
4225
4226 static tree
4227 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4228 {
4229 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4230
4231 /* The current way of determining the type of valist is completely
4232 bogus. We should have the information on the va builtin instead. */
4233 if (!vatype)
4234 vatype = targetm.fn_abi_va_list (cfun->decl);
4235
4236 if (TREE_CODE (vatype) == ARRAY_TYPE)
4237 {
4238 if (TREE_SIDE_EFFECTS (valist))
4239 valist = save_expr (valist);
4240
4241 /* For this case, the backends will be expecting a pointer to
4242 vatype, but it's possible we've actually been given an array
4243 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4244 So fix it. */
4245 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4246 {
4247 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4248 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4249 }
4250 }
4251 else
4252 {
4253 tree pt = build_pointer_type (vatype);
4254
4255 if (! needs_lvalue)
4256 {
4257 if (! TREE_SIDE_EFFECTS (valist))
4258 return valist;
4259
4260 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4261 TREE_SIDE_EFFECTS (valist) = 1;
4262 }
4263
4264 if (TREE_SIDE_EFFECTS (valist))
4265 valist = save_expr (valist);
4266 valist = fold_build2_loc (loc, MEM_REF,
4267 vatype, valist, build_int_cst (pt, 0));
4268 }
4269
4270 return valist;
4271 }
4272
4273 /* The "standard" definition of va_list is void*. */
4274
4275 tree
4276 std_build_builtin_va_list (void)
4277 {
4278 return ptr_type_node;
4279 }
4280
4281 /* The "standard" abi va_list is va_list_type_node. */
4282
4283 tree
4284 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4285 {
4286 return va_list_type_node;
4287 }
4288
4289 /* The "standard" type of va_list is va_list_type_node. */
4290
4291 tree
4292 std_canonical_va_list_type (tree type)
4293 {
4294 tree wtype, htype;
4295
4296 if (INDIRECT_REF_P (type))
4297 type = TREE_TYPE (type);
4298 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4299 type = TREE_TYPE (type);
4300 wtype = va_list_type_node;
4301 htype = type;
4302 /* Treat structure va_list types. */
4303 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4304 htype = TREE_TYPE (htype);
4305 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4306 {
4307 /* If va_list is an array type, the argument may have decayed
4308 to a pointer type, e.g. by being passed to another function.
4309 In that case, unwrap both types so that we can compare the
4310 underlying records. */
4311 if (TREE_CODE (htype) == ARRAY_TYPE
4312 || POINTER_TYPE_P (htype))
4313 {
4314 wtype = TREE_TYPE (wtype);
4315 htype = TREE_TYPE (htype);
4316 }
4317 }
4318 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4319 return va_list_type_node;
4320
4321 return NULL_TREE;
4322 }
4323
4324 /* The "standard" implementation of va_start: just assign `nextarg' to
4325 the variable. */
4326
4327 void
4328 std_expand_builtin_va_start (tree valist, rtx nextarg)
4329 {
4330 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4331 convert_move (va_r, nextarg, 0);
4332
4333 /* We do not have any valid bounds for the pointer, so
4334 just store zero bounds for it. */
4335 if (chkp_function_instrumented_p (current_function_decl))
4336 chkp_expand_bounds_reset_for_mem (valist,
4337 make_tree (TREE_TYPE (valist),
4338 nextarg));
4339 }
4340
4341 /* Expand EXP, a call to __builtin_va_start. */
4342
4343 static rtx
4344 expand_builtin_va_start (tree exp)
4345 {
4346 rtx nextarg;
4347 tree valist;
4348 location_t loc = EXPR_LOCATION (exp);
4349
4350 if (call_expr_nargs (exp) < 2)
4351 {
4352 error_at (loc, "too few arguments to function %<va_start%>");
4353 return const0_rtx;
4354 }
4355
4356 if (fold_builtin_next_arg (exp, true))
4357 return const0_rtx;
4358
4359 nextarg = expand_builtin_next_arg ();
4360 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4361
4362 if (targetm.expand_builtin_va_start)
4363 targetm.expand_builtin_va_start (valist, nextarg);
4364 else
4365 std_expand_builtin_va_start (valist, nextarg);
4366
4367 return const0_rtx;
4368 }
4369
4370 /* Expand EXP, a call to __builtin_va_end. */
4371
4372 static rtx
4373 expand_builtin_va_end (tree exp)
4374 {
4375 tree valist = CALL_EXPR_ARG (exp, 0);
4376
4377 /* Evaluate for side effects, if needed. I hate macros that don't
4378 do that. */
4379 if (TREE_SIDE_EFFECTS (valist))
4380 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4381
4382 return const0_rtx;
4383 }
4384
4385 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4386 builtin rather than just as an assignment in stdarg.h because of the
4387 nastiness of array-type va_list types. */
4388
4389 static rtx
4390 expand_builtin_va_copy (tree exp)
4391 {
4392 tree dst, src, t;
4393 location_t loc = EXPR_LOCATION (exp);
4394
4395 dst = CALL_EXPR_ARG (exp, 0);
4396 src = CALL_EXPR_ARG (exp, 1);
4397
4398 dst = stabilize_va_list_loc (loc, dst, 1);
4399 src = stabilize_va_list_loc (loc, src, 0);
4400
4401 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4402
4403 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4404 {
4405 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4406 TREE_SIDE_EFFECTS (t) = 1;
4407 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4408 }
4409 else
4410 {
4411 rtx dstb, srcb, size;
4412
4413 /* Evaluate to pointers. */
4414 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4415 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4416 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4417 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4418
4419 dstb = convert_memory_address (Pmode, dstb);
4420 srcb = convert_memory_address (Pmode, srcb);
4421
4422 /* "Dereference" to BLKmode memories. */
4423 dstb = gen_rtx_MEM (BLKmode, dstb);
4424 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4425 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4426 srcb = gen_rtx_MEM (BLKmode, srcb);
4427 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4428 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4429
4430 /* Copy. */
4431 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4432 }
4433
4434 return const0_rtx;
4435 }
4436
4437 /* Expand a call to one of the builtin functions __builtin_frame_address or
4438 __builtin_return_address. */
4439
4440 static rtx
4441 expand_builtin_frame_address (tree fndecl, tree exp)
4442 {
4443 /* The argument must be a nonnegative integer constant.
4444 It counts the number of frames to scan up the stack.
4445 The value is the return address saved in that frame. */
4446 if (call_expr_nargs (exp) == 0)
4447 /* Warning about missing arg was already issued. */
4448 return const0_rtx;
4449 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4450 {
4451 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4452 error ("invalid argument to %<__builtin_frame_address%>");
4453 else
4454 error ("invalid argument to %<__builtin_return_address%>");
4455 return const0_rtx;
4456 }
4457 else
4458 {
4459 rtx tem
4460 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4461 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4462
4463 /* Some ports cannot access arbitrary stack frames. */
4464 if (tem == NULL)
4465 {
4466 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4467 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4468 else
4469 warning (0, "unsupported argument to %<__builtin_return_address%>");
4470 return const0_rtx;
4471 }
4472
4473 /* For __builtin_frame_address, return what we've got. */
4474 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4475 return tem;
4476
4477 if (!REG_P (tem)
4478 && ! CONSTANT_P (tem))
4479 tem = copy_addr_to_reg (tem);
4480 return tem;
4481 }
4482 }
4483
4484 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4485 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4486 is the same as for allocate_dynamic_stack_space. */
4487
4488 static rtx
4489 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4490 {
4491 rtx op0;
4492 rtx result;
4493 bool valid_arglist;
4494 unsigned int align;
4495 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4496 == BUILT_IN_ALLOCA_WITH_ALIGN);
4497
4498 valid_arglist
4499 = (alloca_with_align
4500 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4501 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4502
4503 if (!valid_arglist)
4504 return NULL_RTX;
4505
4506 /* Compute the argument. */
4507 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4508
4509 /* Compute the alignment. */
4510 align = (alloca_with_align
4511 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4512 : BIGGEST_ALIGNMENT);
4513
4514 /* Allocate the desired space. */
4515 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4516 result = convert_memory_address (ptr_mode, result);
4517
4518 return result;
4519 }
4520
4521 /* Expand a call to bswap builtin in EXP.
4522 Return NULL_RTX if a normal call should be emitted rather than expanding the
4523 function in-line. If convenient, the result should be placed in TARGET.
4524 SUBTARGET may be used as the target for computing one of EXP's operands. */
4525
4526 static rtx
4527 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4528 rtx subtarget)
4529 {
4530 tree arg;
4531 rtx op0;
4532
4533 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4534 return NULL_RTX;
4535
4536 arg = CALL_EXPR_ARG (exp, 0);
4537 op0 = expand_expr (arg,
4538 subtarget && GET_MODE (subtarget) == target_mode
4539 ? subtarget : NULL_RTX,
4540 target_mode, EXPAND_NORMAL);
4541 if (GET_MODE (op0) != target_mode)
4542 op0 = convert_to_mode (target_mode, op0, 1);
4543
4544 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4545
4546 gcc_assert (target);
4547
4548 return convert_to_mode (target_mode, target, 1);
4549 }
4550
4551 /* Expand a call to a unary builtin in EXP.
4552 Return NULL_RTX if a normal call should be emitted rather than expanding the
4553 function in-line. If convenient, the result should be placed in TARGET.
4554 SUBTARGET may be used as the target for computing one of EXP's operands. */
4555
4556 static rtx
4557 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4558 rtx subtarget, optab op_optab)
4559 {
4560 rtx op0;
4561
4562 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4563 return NULL_RTX;
4564
4565 /* Compute the argument. */
4566 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4567 (subtarget
4568 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4569 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4570 VOIDmode, EXPAND_NORMAL);
4571 /* Compute op, into TARGET if possible.
4572 Set TARGET to wherever the result comes back. */
4573 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4574 op_optab, op0, target, op_optab != clrsb_optab);
4575 gcc_assert (target);
4576
4577 return convert_to_mode (target_mode, target, 0);
4578 }
4579
4580 /* Expand a call to __builtin_expect. We just return our argument
4581 as the builtin_expect semantic should've been already executed by
4582 tree branch prediction pass. */
4583
4584 static rtx
4585 expand_builtin_expect (tree exp, rtx target)
4586 {
4587 tree arg;
4588
4589 if (call_expr_nargs (exp) < 2)
4590 return const0_rtx;
4591 arg = CALL_EXPR_ARG (exp, 0);
4592
4593 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4594 /* When guessing was done, the hints should be already stripped away. */
4595 gcc_assert (!flag_guess_branch_prob
4596 || optimize == 0 || seen_error ());
4597 return target;
4598 }
4599
4600 /* Expand a call to __builtin_assume_aligned. We just return our first
4601 argument as the builtin_assume_aligned semantic should've been already
4602 executed by CCP. */
4603
4604 static rtx
4605 expand_builtin_assume_aligned (tree exp, rtx target)
4606 {
4607 if (call_expr_nargs (exp) < 2)
4608 return const0_rtx;
4609 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4610 EXPAND_NORMAL);
4611 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4612 && (call_expr_nargs (exp) < 3
4613 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4614 return target;
4615 }
4616
4617 void
4618 expand_builtin_trap (void)
4619 {
4620 #ifdef HAVE_trap
4621 if (HAVE_trap)
4622 {
4623 rtx insn = emit_insn (gen_trap ());
4624 /* For trap insns when not accumulating outgoing args force
4625 REG_ARGS_SIZE note to prevent crossjumping of calls with
4626 different args sizes. */
4627 if (!ACCUMULATE_OUTGOING_ARGS)
4628 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4629 }
4630 else
4631 #endif
4632 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4633 emit_barrier ();
4634 }
4635
4636 /* Expand a call to __builtin_unreachable. We do nothing except emit
4637 a barrier saying that control flow will not pass here.
4638
4639 It is the responsibility of the program being compiled to ensure
4640 that control flow does never reach __builtin_unreachable. */
4641 static void
4642 expand_builtin_unreachable (void)
4643 {
4644 emit_barrier ();
4645 }
4646
4647 /* Expand EXP, a call to fabs, fabsf or fabsl.
4648 Return NULL_RTX if a normal call should be emitted rather than expanding
4649 the function inline. If convenient, the result should be placed
4650 in TARGET. SUBTARGET may be used as the target for computing
4651 the operand. */
4652
4653 static rtx
4654 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4655 {
4656 machine_mode mode;
4657 tree arg;
4658 rtx op0;
4659
4660 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4661 return NULL_RTX;
4662
4663 arg = CALL_EXPR_ARG (exp, 0);
4664 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4665 mode = TYPE_MODE (TREE_TYPE (arg));
4666 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4667 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4668 }
4669
4670 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4671 Return NULL is a normal call should be emitted rather than expanding the
4672 function inline. If convenient, the result should be placed in TARGET.
4673 SUBTARGET may be used as the target for computing the operand. */
4674
4675 static rtx
4676 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4677 {
4678 rtx op0, op1;
4679 tree arg;
4680
4681 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4682 return NULL_RTX;
4683
4684 arg = CALL_EXPR_ARG (exp, 0);
4685 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4686
4687 arg = CALL_EXPR_ARG (exp, 1);
4688 op1 = expand_normal (arg);
4689
4690 return expand_copysign (op0, op1, target);
4691 }
4692
4693 /* Expand a call to __builtin___clear_cache. */
4694
4695 static rtx
4696 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4697 {
4698 #ifndef HAVE_clear_cache
4699 #ifdef CLEAR_INSN_CACHE
4700 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4701 does something. Just do the default expansion to a call to
4702 __clear_cache(). */
4703 return NULL_RTX;
4704 #else
4705 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4706 does nothing. There is no need to call it. Do nothing. */
4707 return const0_rtx;
4708 #endif /* CLEAR_INSN_CACHE */
4709 #else
4710 /* We have a "clear_cache" insn, and it will handle everything. */
4711 tree begin, end;
4712 rtx begin_rtx, end_rtx;
4713
4714 /* We must not expand to a library call. If we did, any
4715 fallback library function in libgcc that might contain a call to
4716 __builtin___clear_cache() would recurse infinitely. */
4717 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4718 {
4719 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4720 return const0_rtx;
4721 }
4722
4723 if (HAVE_clear_cache)
4724 {
4725 struct expand_operand ops[2];
4726
4727 begin = CALL_EXPR_ARG (exp, 0);
4728 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4729
4730 end = CALL_EXPR_ARG (exp, 1);
4731 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4732
4733 create_address_operand (&ops[0], begin_rtx);
4734 create_address_operand (&ops[1], end_rtx);
4735 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4736 return const0_rtx;
4737 }
4738 return const0_rtx;
4739 #endif /* HAVE_clear_cache */
4740 }
4741
4742 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4743
4744 static rtx
4745 round_trampoline_addr (rtx tramp)
4746 {
4747 rtx temp, addend, mask;
4748
4749 /* If we don't need too much alignment, we'll have been guaranteed
4750 proper alignment by get_trampoline_type. */
4751 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4752 return tramp;
4753
4754 /* Round address up to desired boundary. */
4755 temp = gen_reg_rtx (Pmode);
4756 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4757 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4758
4759 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4760 temp, 0, OPTAB_LIB_WIDEN);
4761 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4762 temp, 0, OPTAB_LIB_WIDEN);
4763
4764 return tramp;
4765 }
4766
4767 static rtx
4768 expand_builtin_init_trampoline (tree exp, bool onstack)
4769 {
4770 tree t_tramp, t_func, t_chain;
4771 rtx m_tramp, r_tramp, r_chain, tmp;
4772
4773 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4774 POINTER_TYPE, VOID_TYPE))
4775 return NULL_RTX;
4776
4777 t_tramp = CALL_EXPR_ARG (exp, 0);
4778 t_func = CALL_EXPR_ARG (exp, 1);
4779 t_chain = CALL_EXPR_ARG (exp, 2);
4780
4781 r_tramp = expand_normal (t_tramp);
4782 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4783 MEM_NOTRAP_P (m_tramp) = 1;
4784
4785 /* If ONSTACK, the TRAMP argument should be the address of a field
4786 within the local function's FRAME decl. Either way, let's see if
4787 we can fill in the MEM_ATTRs for this memory. */
4788 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4789 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4790
4791 /* Creator of a heap trampoline is responsible for making sure the
4792 address is aligned to at least STACK_BOUNDARY. Normally malloc
4793 will ensure this anyhow. */
4794 tmp = round_trampoline_addr (r_tramp);
4795 if (tmp != r_tramp)
4796 {
4797 m_tramp = change_address (m_tramp, BLKmode, tmp);
4798 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4799 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4800 }
4801
4802 /* The FUNC argument should be the address of the nested function.
4803 Extract the actual function decl to pass to the hook. */
4804 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4805 t_func = TREE_OPERAND (t_func, 0);
4806 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4807
4808 r_chain = expand_normal (t_chain);
4809
4810 /* Generate insns to initialize the trampoline. */
4811 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4812
4813 if (onstack)
4814 {
4815 trampolines_created = 1;
4816
4817 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4818 "trampoline generated for nested function %qD", t_func);
4819 }
4820
4821 return const0_rtx;
4822 }
4823
4824 static rtx
4825 expand_builtin_adjust_trampoline (tree exp)
4826 {
4827 rtx tramp;
4828
4829 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4830 return NULL_RTX;
4831
4832 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4833 tramp = round_trampoline_addr (tramp);
4834 if (targetm.calls.trampoline_adjust_address)
4835 tramp = targetm.calls.trampoline_adjust_address (tramp);
4836
4837 return tramp;
4838 }
4839
4840 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4841 function. The function first checks whether the back end provides
4842 an insn to implement signbit for the respective mode. If not, it
4843 checks whether the floating point format of the value is such that
4844 the sign bit can be extracted. If that is not the case, the
4845 function returns NULL_RTX to indicate that a normal call should be
4846 emitted rather than expanding the function in-line. EXP is the
4847 expression that is a call to the builtin function; if convenient,
4848 the result should be placed in TARGET. */
4849 static rtx
4850 expand_builtin_signbit (tree exp, rtx target)
4851 {
4852 const struct real_format *fmt;
4853 machine_mode fmode, imode, rmode;
4854 tree arg;
4855 int word, bitpos;
4856 enum insn_code icode;
4857 rtx temp;
4858 location_t loc = EXPR_LOCATION (exp);
4859
4860 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4861 return NULL_RTX;
4862
4863 arg = CALL_EXPR_ARG (exp, 0);
4864 fmode = TYPE_MODE (TREE_TYPE (arg));
4865 rmode = TYPE_MODE (TREE_TYPE (exp));
4866 fmt = REAL_MODE_FORMAT (fmode);
4867
4868 arg = builtin_save_expr (arg);
4869
4870 /* Expand the argument yielding a RTX expression. */
4871 temp = expand_normal (arg);
4872
4873 /* Check if the back end provides an insn that handles signbit for the
4874 argument's mode. */
4875 icode = optab_handler (signbit_optab, fmode);
4876 if (icode != CODE_FOR_nothing)
4877 {
4878 rtx_insn *last = get_last_insn ();
4879 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4880 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4881 return target;
4882 delete_insns_since (last);
4883 }
4884
4885 /* For floating point formats without a sign bit, implement signbit
4886 as "ARG < 0.0". */
4887 bitpos = fmt->signbit_ro;
4888 if (bitpos < 0)
4889 {
4890 /* But we can't do this if the format supports signed zero. */
4891 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4892 return NULL_RTX;
4893
4894 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4895 build_real (TREE_TYPE (arg), dconst0));
4896 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4897 }
4898
4899 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4900 {
4901 imode = int_mode_for_mode (fmode);
4902 if (imode == BLKmode)
4903 return NULL_RTX;
4904 temp = gen_lowpart (imode, temp);
4905 }
4906 else
4907 {
4908 imode = word_mode;
4909 /* Handle targets with different FP word orders. */
4910 if (FLOAT_WORDS_BIG_ENDIAN)
4911 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4912 else
4913 word = bitpos / BITS_PER_WORD;
4914 temp = operand_subword_force (temp, word, fmode);
4915 bitpos = bitpos % BITS_PER_WORD;
4916 }
4917
4918 /* Force the intermediate word_mode (or narrower) result into a
4919 register. This avoids attempting to create paradoxical SUBREGs
4920 of floating point modes below. */
4921 temp = force_reg (imode, temp);
4922
4923 /* If the bitpos is within the "result mode" lowpart, the operation
4924 can be implement with a single bitwise AND. Otherwise, we need
4925 a right shift and an AND. */
4926
4927 if (bitpos < GET_MODE_BITSIZE (rmode))
4928 {
4929 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4930
4931 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4932 temp = gen_lowpart (rmode, temp);
4933 temp = expand_binop (rmode, and_optab, temp,
4934 immed_wide_int_const (mask, rmode),
4935 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4936 }
4937 else
4938 {
4939 /* Perform a logical right shift to place the signbit in the least
4940 significant bit, then truncate the result to the desired mode
4941 and mask just this bit. */
4942 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4943 temp = gen_lowpart (rmode, temp);
4944 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4945 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4946 }
4947
4948 return temp;
4949 }
4950
4951 /* Expand fork or exec calls. TARGET is the desired target of the
4952 call. EXP is the call. FN is the
4953 identificator of the actual function. IGNORE is nonzero if the
4954 value is to be ignored. */
4955
4956 static rtx
4957 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4958 {
4959 tree id, decl;
4960 tree call;
4961
4962 /* If we are not profiling, just call the function. */
4963 if (!profile_arc_flag)
4964 return NULL_RTX;
4965
4966 /* Otherwise call the wrapper. This should be equivalent for the rest of
4967 compiler, so the code does not diverge, and the wrapper may run the
4968 code necessary for keeping the profiling sane. */
4969
4970 switch (DECL_FUNCTION_CODE (fn))
4971 {
4972 case BUILT_IN_FORK:
4973 id = get_identifier ("__gcov_fork");
4974 break;
4975
4976 case BUILT_IN_EXECL:
4977 id = get_identifier ("__gcov_execl");
4978 break;
4979
4980 case BUILT_IN_EXECV:
4981 id = get_identifier ("__gcov_execv");
4982 break;
4983
4984 case BUILT_IN_EXECLP:
4985 id = get_identifier ("__gcov_execlp");
4986 break;
4987
4988 case BUILT_IN_EXECLE:
4989 id = get_identifier ("__gcov_execle");
4990 break;
4991
4992 case BUILT_IN_EXECVP:
4993 id = get_identifier ("__gcov_execvp");
4994 break;
4995
4996 case BUILT_IN_EXECVE:
4997 id = get_identifier ("__gcov_execve");
4998 break;
4999
5000 default:
5001 gcc_unreachable ();
5002 }
5003
5004 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5005 FUNCTION_DECL, id, TREE_TYPE (fn));
5006 DECL_EXTERNAL (decl) = 1;
5007 TREE_PUBLIC (decl) = 1;
5008 DECL_ARTIFICIAL (decl) = 1;
5009 TREE_NOTHROW (decl) = 1;
5010 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5011 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5012 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5013 return expand_call (call, target, ignore);
5014 }
5015
5016
5017 \f
5018 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5019 the pointer in these functions is void*, the tree optimizers may remove
5020 casts. The mode computed in expand_builtin isn't reliable either, due
5021 to __sync_bool_compare_and_swap.
5022
5023 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5024 group of builtins. This gives us log2 of the mode size. */
5025
5026 static inline machine_mode
5027 get_builtin_sync_mode (int fcode_diff)
5028 {
5029 /* The size is not negotiable, so ask not to get BLKmode in return
5030 if the target indicates that a smaller size would be better. */
5031 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5032 }
5033
5034 /* Expand the memory expression LOC and return the appropriate memory operand
5035 for the builtin_sync operations. */
5036
5037 static rtx
5038 get_builtin_sync_mem (tree loc, machine_mode mode)
5039 {
5040 rtx addr, mem;
5041
5042 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5043 addr = convert_memory_address (Pmode, addr);
5044
5045 /* Note that we explicitly do not want any alias information for this
5046 memory, so that we kill all other live memories. Otherwise we don't
5047 satisfy the full barrier semantics of the intrinsic. */
5048 mem = validize_mem (gen_rtx_MEM (mode, addr));
5049
5050 /* The alignment needs to be at least according to that of the mode. */
5051 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5052 get_pointer_alignment (loc)));
5053 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5054 MEM_VOLATILE_P (mem) = 1;
5055
5056 return mem;
5057 }
5058
5059 /* Make sure an argument is in the right mode.
5060 EXP is the tree argument.
5061 MODE is the mode it should be in. */
5062
5063 static rtx
5064 expand_expr_force_mode (tree exp, machine_mode mode)
5065 {
5066 rtx val;
5067 machine_mode old_mode;
5068
5069 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5070 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5071 of CONST_INTs, where we know the old_mode only from the call argument. */
5072
5073 old_mode = GET_MODE (val);
5074 if (old_mode == VOIDmode)
5075 old_mode = TYPE_MODE (TREE_TYPE (exp));
5076 val = convert_modes (mode, old_mode, val, 1);
5077 return val;
5078 }
5079
5080
5081 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5082 EXP is the CALL_EXPR. CODE is the rtx code
5083 that corresponds to the arithmetic or logical operation from the name;
5084 an exception here is that NOT actually means NAND. TARGET is an optional
5085 place for us to store the results; AFTER is true if this is the
5086 fetch_and_xxx form. */
5087
5088 static rtx
5089 expand_builtin_sync_operation (machine_mode mode, tree exp,
5090 enum rtx_code code, bool after,
5091 rtx target)
5092 {
5093 rtx val, mem;
5094 location_t loc = EXPR_LOCATION (exp);
5095
5096 if (code == NOT && warn_sync_nand)
5097 {
5098 tree fndecl = get_callee_fndecl (exp);
5099 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5100
5101 static bool warned_f_a_n, warned_n_a_f;
5102
5103 switch (fcode)
5104 {
5105 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5106 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5107 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5108 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5109 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5110 if (warned_f_a_n)
5111 break;
5112
5113 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5114 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5115 warned_f_a_n = true;
5116 break;
5117
5118 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5119 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5120 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5121 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5122 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5123 if (warned_n_a_f)
5124 break;
5125
5126 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5127 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5128 warned_n_a_f = true;
5129 break;
5130
5131 default:
5132 gcc_unreachable ();
5133 }
5134 }
5135
5136 /* Expand the operands. */
5137 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5138 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5139
5140 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5141 after);
5142 }
5143
5144 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5145 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5146 true if this is the boolean form. TARGET is a place for us to store the
5147 results; this is NOT optional if IS_BOOL is true. */
5148
5149 static rtx
5150 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5151 bool is_bool, rtx target)
5152 {
5153 rtx old_val, new_val, mem;
5154 rtx *pbool, *poval;
5155
5156 /* Expand the operands. */
5157 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5158 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5159 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5160
5161 pbool = poval = NULL;
5162 if (target != const0_rtx)
5163 {
5164 if (is_bool)
5165 pbool = &target;
5166 else
5167 poval = &target;
5168 }
5169 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5170 false, MEMMODEL_SEQ_CST,
5171 MEMMODEL_SEQ_CST))
5172 return NULL_RTX;
5173
5174 return target;
5175 }
5176
5177 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5178 general form is actually an atomic exchange, and some targets only
5179 support a reduced form with the second argument being a constant 1.
5180 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5181 the results. */
5182
5183 static rtx
5184 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5185 rtx target)
5186 {
5187 rtx val, mem;
5188
5189 /* Expand the operands. */
5190 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5191 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5192
5193 return expand_sync_lock_test_and_set (target, mem, val);
5194 }
5195
5196 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5197
5198 static void
5199 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5200 {
5201 rtx mem;
5202
5203 /* Expand the operands. */
5204 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5205
5206 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5207 }
5208
5209 /* Given an integer representing an ``enum memmodel'', verify its
5210 correctness and return the memory model enum. */
5211
5212 static enum memmodel
5213 get_memmodel (tree exp)
5214 {
5215 rtx op;
5216 unsigned HOST_WIDE_INT val;
5217
5218 /* If the parameter is not a constant, it's a run time value so we'll just
5219 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5220 if (TREE_CODE (exp) != INTEGER_CST)
5221 return MEMMODEL_SEQ_CST;
5222
5223 op = expand_normal (exp);
5224
5225 val = INTVAL (op);
5226 if (targetm.memmodel_check)
5227 val = targetm.memmodel_check (val);
5228 else if (val & ~MEMMODEL_MASK)
5229 {
5230 warning (OPT_Winvalid_memory_model,
5231 "Unknown architecture specifier in memory model to builtin.");
5232 return MEMMODEL_SEQ_CST;
5233 }
5234
5235 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5236 {
5237 warning (OPT_Winvalid_memory_model,
5238 "invalid memory model argument to builtin");
5239 return MEMMODEL_SEQ_CST;
5240 }
5241
5242 return (enum memmodel) val;
5243 }
5244
5245 /* Expand the __atomic_exchange intrinsic:
5246 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5247 EXP is the CALL_EXPR.
5248 TARGET is an optional place for us to store the results. */
5249
5250 static rtx
5251 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5252 {
5253 rtx val, mem;
5254 enum memmodel model;
5255
5256 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5257 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5258 {
5259 error ("invalid memory model for %<__atomic_exchange%>");
5260 return NULL_RTX;
5261 }
5262
5263 if (!flag_inline_atomics)
5264 return NULL_RTX;
5265
5266 /* Expand the operands. */
5267 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5268 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5269
5270 return expand_atomic_exchange (target, mem, val, model);
5271 }
5272
5273 /* Expand the __atomic_compare_exchange intrinsic:
5274 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5275 TYPE desired, BOOL weak,
5276 enum memmodel success,
5277 enum memmodel failure)
5278 EXP is the CALL_EXPR.
5279 TARGET is an optional place for us to store the results. */
5280
5281 static rtx
5282 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5283 rtx target)
5284 {
5285 rtx expect, desired, mem, oldval;
5286 rtx_code_label *label;
5287 enum memmodel success, failure;
5288 tree weak;
5289 bool is_weak;
5290
5291 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5292 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5293
5294 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5295 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5296 {
5297 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5298 return NULL_RTX;
5299 }
5300
5301 if (failure > success)
5302 {
5303 error ("failure memory model cannot be stronger than success "
5304 "memory model for %<__atomic_compare_exchange%>");
5305 return NULL_RTX;
5306 }
5307
5308 if (!flag_inline_atomics)
5309 return NULL_RTX;
5310
5311 /* Expand the operands. */
5312 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5313
5314 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5315 expect = convert_memory_address (Pmode, expect);
5316 expect = gen_rtx_MEM (mode, expect);
5317 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5318
5319 weak = CALL_EXPR_ARG (exp, 3);
5320 is_weak = false;
5321 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5322 is_weak = true;
5323
5324 if (target == const0_rtx)
5325 target = NULL;
5326
5327 /* Lest the rtl backend create a race condition with an imporoper store
5328 to memory, always create a new pseudo for OLDVAL. */
5329 oldval = NULL;
5330
5331 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5332 is_weak, success, failure))
5333 return NULL_RTX;
5334
5335 /* Conditionally store back to EXPECT, lest we create a race condition
5336 with an improper store to memory. */
5337 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5338 the normal case where EXPECT is totally private, i.e. a register. At
5339 which point the store can be unconditional. */
5340 label = gen_label_rtx ();
5341 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5342 emit_move_insn (expect, oldval);
5343 emit_label (label);
5344
5345 return target;
5346 }
5347
5348 /* Expand the __atomic_load intrinsic:
5349 TYPE __atomic_load (TYPE *object, enum memmodel)
5350 EXP is the CALL_EXPR.
5351 TARGET is an optional place for us to store the results. */
5352
5353 static rtx
5354 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5355 {
5356 rtx mem;
5357 enum memmodel model;
5358
5359 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5360 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5361 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5362 {
5363 error ("invalid memory model for %<__atomic_load%>");
5364 return NULL_RTX;
5365 }
5366
5367 if (!flag_inline_atomics)
5368 return NULL_RTX;
5369
5370 /* Expand the operand. */
5371 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5372
5373 return expand_atomic_load (target, mem, model);
5374 }
5375
5376
5377 /* Expand the __atomic_store intrinsic:
5378 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5379 EXP is the CALL_EXPR.
5380 TARGET is an optional place for us to store the results. */
5381
5382 static rtx
5383 expand_builtin_atomic_store (machine_mode mode, tree exp)
5384 {
5385 rtx mem, val;
5386 enum memmodel model;
5387
5388 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5389 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5390 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5391 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5392 {
5393 error ("invalid memory model for %<__atomic_store%>");
5394 return NULL_RTX;
5395 }
5396
5397 if (!flag_inline_atomics)
5398 return NULL_RTX;
5399
5400 /* Expand the operands. */
5401 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5402 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5403
5404 return expand_atomic_store (mem, val, model, false);
5405 }
5406
5407 /* Expand the __atomic_fetch_XXX intrinsic:
5408 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5409 EXP is the CALL_EXPR.
5410 TARGET is an optional place for us to store the results.
5411 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5412 FETCH_AFTER is true if returning the result of the operation.
5413 FETCH_AFTER is false if returning the value before the operation.
5414 IGNORE is true if the result is not used.
5415 EXT_CALL is the correct builtin for an external call if this cannot be
5416 resolved to an instruction sequence. */
5417
5418 static rtx
5419 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5420 enum rtx_code code, bool fetch_after,
5421 bool ignore, enum built_in_function ext_call)
5422 {
5423 rtx val, mem, ret;
5424 enum memmodel model;
5425 tree fndecl;
5426 tree addr;
5427
5428 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5429
5430 /* Expand the operands. */
5431 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5432 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5433
5434 /* Only try generating instructions if inlining is turned on. */
5435 if (flag_inline_atomics)
5436 {
5437 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5438 if (ret)
5439 return ret;
5440 }
5441
5442 /* Return if a different routine isn't needed for the library call. */
5443 if (ext_call == BUILT_IN_NONE)
5444 return NULL_RTX;
5445
5446 /* Change the call to the specified function. */
5447 fndecl = get_callee_fndecl (exp);
5448 addr = CALL_EXPR_FN (exp);
5449 STRIP_NOPS (addr);
5450
5451 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5452 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5453
5454 /* Expand the call here so we can emit trailing code. */
5455 ret = expand_call (exp, target, ignore);
5456
5457 /* Replace the original function just in case it matters. */
5458 TREE_OPERAND (addr, 0) = fndecl;
5459
5460 /* Then issue the arithmetic correction to return the right result. */
5461 if (!ignore)
5462 {
5463 if (code == NOT)
5464 {
5465 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5466 OPTAB_LIB_WIDEN);
5467 ret = expand_simple_unop (mode, NOT, ret, target, true);
5468 }
5469 else
5470 ret = expand_simple_binop (mode, code, ret, val, target, true,
5471 OPTAB_LIB_WIDEN);
5472 }
5473 return ret;
5474 }
5475
5476
5477 #ifndef HAVE_atomic_clear
5478 # define HAVE_atomic_clear 0
5479 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5480 #endif
5481
5482 /* Expand an atomic clear operation.
5483 void _atomic_clear (BOOL *obj, enum memmodel)
5484 EXP is the call expression. */
5485
5486 static rtx
5487 expand_builtin_atomic_clear (tree exp)
5488 {
5489 machine_mode mode;
5490 rtx mem, ret;
5491 enum memmodel model;
5492
5493 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5494 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5495 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5496
5497 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5498 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5499 {
5500 error ("invalid memory model for %<__atomic_store%>");
5501 return const0_rtx;
5502 }
5503
5504 if (HAVE_atomic_clear)
5505 {
5506 emit_insn (gen_atomic_clear (mem, model));
5507 return const0_rtx;
5508 }
5509
5510 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5511 Failing that, a store is issued by __atomic_store. The only way this can
5512 fail is if the bool type is larger than a word size. Unlikely, but
5513 handle it anyway for completeness. Assume a single threaded model since
5514 there is no atomic support in this case, and no barriers are required. */
5515 ret = expand_atomic_store (mem, const0_rtx, model, true);
5516 if (!ret)
5517 emit_move_insn (mem, const0_rtx);
5518 return const0_rtx;
5519 }
5520
5521 /* Expand an atomic test_and_set operation.
5522 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5523 EXP is the call expression. */
5524
5525 static rtx
5526 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5527 {
5528 rtx mem;
5529 enum memmodel model;
5530 machine_mode mode;
5531
5532 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5533 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5534 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5535
5536 return expand_atomic_test_and_set (target, mem, model);
5537 }
5538
5539
5540 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5541 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5542
5543 static tree
5544 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5545 {
5546 int size;
5547 machine_mode mode;
5548 unsigned int mode_align, type_align;
5549
5550 if (TREE_CODE (arg0) != INTEGER_CST)
5551 return NULL_TREE;
5552
5553 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5554 mode = mode_for_size (size, MODE_INT, 0);
5555 mode_align = GET_MODE_ALIGNMENT (mode);
5556
5557 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5558 type_align = mode_align;
5559 else
5560 {
5561 tree ttype = TREE_TYPE (arg1);
5562
5563 /* This function is usually invoked and folded immediately by the front
5564 end before anything else has a chance to look at it. The pointer
5565 parameter at this point is usually cast to a void *, so check for that
5566 and look past the cast. */
5567 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5568 && VOID_TYPE_P (TREE_TYPE (ttype)))
5569 arg1 = TREE_OPERAND (arg1, 0);
5570
5571 ttype = TREE_TYPE (arg1);
5572 gcc_assert (POINTER_TYPE_P (ttype));
5573
5574 /* Get the underlying type of the object. */
5575 ttype = TREE_TYPE (ttype);
5576 type_align = TYPE_ALIGN (ttype);
5577 }
5578
5579 /* If the object has smaller alignment, the the lock free routines cannot
5580 be used. */
5581 if (type_align < mode_align)
5582 return boolean_false_node;
5583
5584 /* Check if a compare_and_swap pattern exists for the mode which represents
5585 the required size. The pattern is not allowed to fail, so the existence
5586 of the pattern indicates support is present. */
5587 if (can_compare_and_swap_p (mode, true))
5588 return boolean_true_node;
5589 else
5590 return boolean_false_node;
5591 }
5592
5593 /* Return true if the parameters to call EXP represent an object which will
5594 always generate lock free instructions. The first argument represents the
5595 size of the object, and the second parameter is a pointer to the object
5596 itself. If NULL is passed for the object, then the result is based on
5597 typical alignment for an object of the specified size. Otherwise return
5598 false. */
5599
5600 static rtx
5601 expand_builtin_atomic_always_lock_free (tree exp)
5602 {
5603 tree size;
5604 tree arg0 = CALL_EXPR_ARG (exp, 0);
5605 tree arg1 = CALL_EXPR_ARG (exp, 1);
5606
5607 if (TREE_CODE (arg0) != INTEGER_CST)
5608 {
5609 error ("non-constant argument 1 to __atomic_always_lock_free");
5610 return const0_rtx;
5611 }
5612
5613 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5614 if (size == boolean_true_node)
5615 return const1_rtx;
5616 return const0_rtx;
5617 }
5618
5619 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5620 is lock free on this architecture. */
5621
5622 static tree
5623 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5624 {
5625 if (!flag_inline_atomics)
5626 return NULL_TREE;
5627
5628 /* If it isn't always lock free, don't generate a result. */
5629 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5630 return boolean_true_node;
5631
5632 return NULL_TREE;
5633 }
5634
5635 /* Return true if the parameters to call EXP represent an object which will
5636 always generate lock free instructions. The first argument represents the
5637 size of the object, and the second parameter is a pointer to the object
5638 itself. If NULL is passed for the object, then the result is based on
5639 typical alignment for an object of the specified size. Otherwise return
5640 NULL*/
5641
5642 static rtx
5643 expand_builtin_atomic_is_lock_free (tree exp)
5644 {
5645 tree size;
5646 tree arg0 = CALL_EXPR_ARG (exp, 0);
5647 tree arg1 = CALL_EXPR_ARG (exp, 1);
5648
5649 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5650 {
5651 error ("non-integer argument 1 to __atomic_is_lock_free");
5652 return NULL_RTX;
5653 }
5654
5655 if (!flag_inline_atomics)
5656 return NULL_RTX;
5657
5658 /* If the value is known at compile time, return the RTX for it. */
5659 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5660 if (size == boolean_true_node)
5661 return const1_rtx;
5662
5663 return NULL_RTX;
5664 }
5665
5666 /* Expand the __atomic_thread_fence intrinsic:
5667 void __atomic_thread_fence (enum memmodel)
5668 EXP is the CALL_EXPR. */
5669
5670 static void
5671 expand_builtin_atomic_thread_fence (tree exp)
5672 {
5673 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5674 expand_mem_thread_fence (model);
5675 }
5676
5677 /* Expand the __atomic_signal_fence intrinsic:
5678 void __atomic_signal_fence (enum memmodel)
5679 EXP is the CALL_EXPR. */
5680
5681 static void
5682 expand_builtin_atomic_signal_fence (tree exp)
5683 {
5684 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5685 expand_mem_signal_fence (model);
5686 }
5687
5688 /* Expand the __sync_synchronize intrinsic. */
5689
5690 static void
5691 expand_builtin_sync_synchronize (void)
5692 {
5693 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5694 }
5695
5696 static rtx
5697 expand_builtin_thread_pointer (tree exp, rtx target)
5698 {
5699 enum insn_code icode;
5700 if (!validate_arglist (exp, VOID_TYPE))
5701 return const0_rtx;
5702 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5703 if (icode != CODE_FOR_nothing)
5704 {
5705 struct expand_operand op;
5706 /* If the target is not sutitable then create a new target. */
5707 if (target == NULL_RTX
5708 || !REG_P (target)
5709 || GET_MODE (target) != Pmode)
5710 target = gen_reg_rtx (Pmode);
5711 create_output_operand (&op, target, Pmode);
5712 expand_insn (icode, 1, &op);
5713 return target;
5714 }
5715 error ("__builtin_thread_pointer is not supported on this target");
5716 return const0_rtx;
5717 }
5718
5719 static void
5720 expand_builtin_set_thread_pointer (tree exp)
5721 {
5722 enum insn_code icode;
5723 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5724 return;
5725 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5726 if (icode != CODE_FOR_nothing)
5727 {
5728 struct expand_operand op;
5729 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5730 Pmode, EXPAND_NORMAL);
5731 create_input_operand (&op, val, Pmode);
5732 expand_insn (icode, 1, &op);
5733 return;
5734 }
5735 error ("__builtin_set_thread_pointer is not supported on this target");
5736 }
5737
5738 \f
5739 /* Emit code to restore the current value of stack. */
5740
5741 static void
5742 expand_stack_restore (tree var)
5743 {
5744 rtx_insn *prev;
5745 rtx sa = expand_normal (var);
5746
5747 sa = convert_memory_address (Pmode, sa);
5748
5749 prev = get_last_insn ();
5750 emit_stack_restore (SAVE_BLOCK, sa);
5751 fixup_args_size_notes (prev, get_last_insn (), 0);
5752 }
5753
5754
5755 /* Emit code to save the current value of stack. */
5756
5757 static rtx
5758 expand_stack_save (void)
5759 {
5760 rtx ret = NULL_RTX;
5761
5762 do_pending_stack_adjust ();
5763 emit_stack_save (SAVE_BLOCK, &ret);
5764 return ret;
5765 }
5766
5767 /* Expand an expression EXP that calls a built-in function,
5768 with result going to TARGET if that's convenient
5769 (and in mode MODE if that's convenient).
5770 SUBTARGET may be used as the target for computing one of EXP's operands.
5771 IGNORE is nonzero if the value is to be ignored. */
5772
5773 rtx
5774 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5775 int ignore)
5776 {
5777 tree fndecl = get_callee_fndecl (exp);
5778 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5779 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5780 int flags;
5781
5782 /* When ASan is enabled, we don't want to expand some memory/string
5783 builtins and rely on libsanitizer's hooks. This allows us to avoid
5784 redundant checks and be sure, that possible overflow will be detected
5785 by ASan. */
5786
5787 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5788 return expand_call (exp, target, ignore);
5789
5790 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5791 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5792
5793 /* When not optimizing, generate calls to library functions for a certain
5794 set of builtins. */
5795 if (!optimize
5796 && !called_as_built_in (fndecl)
5797 && fcode != BUILT_IN_FORK
5798 && fcode != BUILT_IN_EXECL
5799 && fcode != BUILT_IN_EXECV
5800 && fcode != BUILT_IN_EXECLP
5801 && fcode != BUILT_IN_EXECLE
5802 && fcode != BUILT_IN_EXECVP
5803 && fcode != BUILT_IN_EXECVE
5804 && fcode != BUILT_IN_ALLOCA
5805 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5806 && fcode != BUILT_IN_FREE
5807 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5808 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5809 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5810 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5811 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5812 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5813 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5814 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5815 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5816 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5817 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
5818 && fcode != BUILT_IN_CHKP_BNDRET)
5819 return expand_call (exp, target, ignore);
5820
5821 /* The built-in function expanders test for target == const0_rtx
5822 to determine whether the function's result will be ignored. */
5823 if (ignore)
5824 target = const0_rtx;
5825
5826 /* If the result of a pure or const built-in function is ignored, and
5827 none of its arguments are volatile, we can avoid expanding the
5828 built-in call and just evaluate the arguments for side-effects. */
5829 if (target == const0_rtx
5830 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5831 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5832 {
5833 bool volatilep = false;
5834 tree arg;
5835 call_expr_arg_iterator iter;
5836
5837 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5838 if (TREE_THIS_VOLATILE (arg))
5839 {
5840 volatilep = true;
5841 break;
5842 }
5843
5844 if (! volatilep)
5845 {
5846 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5847 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5848 return const0_rtx;
5849 }
5850 }
5851
5852 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
5853
5854 switch (fcode)
5855 {
5856 CASE_FLT_FN (BUILT_IN_FABS):
5857 case BUILT_IN_FABSD32:
5858 case BUILT_IN_FABSD64:
5859 case BUILT_IN_FABSD128:
5860 target = expand_builtin_fabs (exp, target, subtarget);
5861 if (target)
5862 return target;
5863 break;
5864
5865 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5866 target = expand_builtin_copysign (exp, target, subtarget);
5867 if (target)
5868 return target;
5869 break;
5870
5871 /* Just do a normal library call if we were unable to fold
5872 the values. */
5873 CASE_FLT_FN (BUILT_IN_CABS):
5874 break;
5875
5876 CASE_FLT_FN (BUILT_IN_EXP):
5877 CASE_FLT_FN (BUILT_IN_EXP10):
5878 CASE_FLT_FN (BUILT_IN_POW10):
5879 CASE_FLT_FN (BUILT_IN_EXP2):
5880 CASE_FLT_FN (BUILT_IN_EXPM1):
5881 CASE_FLT_FN (BUILT_IN_LOGB):
5882 CASE_FLT_FN (BUILT_IN_LOG):
5883 CASE_FLT_FN (BUILT_IN_LOG10):
5884 CASE_FLT_FN (BUILT_IN_LOG2):
5885 CASE_FLT_FN (BUILT_IN_LOG1P):
5886 CASE_FLT_FN (BUILT_IN_TAN):
5887 CASE_FLT_FN (BUILT_IN_ASIN):
5888 CASE_FLT_FN (BUILT_IN_ACOS):
5889 CASE_FLT_FN (BUILT_IN_ATAN):
5890 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5891 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5892 because of possible accuracy problems. */
5893 if (! flag_unsafe_math_optimizations)
5894 break;
5895 CASE_FLT_FN (BUILT_IN_SQRT):
5896 CASE_FLT_FN (BUILT_IN_FLOOR):
5897 CASE_FLT_FN (BUILT_IN_CEIL):
5898 CASE_FLT_FN (BUILT_IN_TRUNC):
5899 CASE_FLT_FN (BUILT_IN_ROUND):
5900 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5901 CASE_FLT_FN (BUILT_IN_RINT):
5902 target = expand_builtin_mathfn (exp, target, subtarget);
5903 if (target)
5904 return target;
5905 break;
5906
5907 CASE_FLT_FN (BUILT_IN_FMA):
5908 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5909 if (target)
5910 return target;
5911 break;
5912
5913 CASE_FLT_FN (BUILT_IN_ILOGB):
5914 if (! flag_unsafe_math_optimizations)
5915 break;
5916 CASE_FLT_FN (BUILT_IN_ISINF):
5917 CASE_FLT_FN (BUILT_IN_FINITE):
5918 case BUILT_IN_ISFINITE:
5919 case BUILT_IN_ISNORMAL:
5920 target = expand_builtin_interclass_mathfn (exp, target);
5921 if (target)
5922 return target;
5923 break;
5924
5925 CASE_FLT_FN (BUILT_IN_ICEIL):
5926 CASE_FLT_FN (BUILT_IN_LCEIL):
5927 CASE_FLT_FN (BUILT_IN_LLCEIL):
5928 CASE_FLT_FN (BUILT_IN_LFLOOR):
5929 CASE_FLT_FN (BUILT_IN_IFLOOR):
5930 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5931 target = expand_builtin_int_roundingfn (exp, target);
5932 if (target)
5933 return target;
5934 break;
5935
5936 CASE_FLT_FN (BUILT_IN_IRINT):
5937 CASE_FLT_FN (BUILT_IN_LRINT):
5938 CASE_FLT_FN (BUILT_IN_LLRINT):
5939 CASE_FLT_FN (BUILT_IN_IROUND):
5940 CASE_FLT_FN (BUILT_IN_LROUND):
5941 CASE_FLT_FN (BUILT_IN_LLROUND):
5942 target = expand_builtin_int_roundingfn_2 (exp, target);
5943 if (target)
5944 return target;
5945 break;
5946
5947 CASE_FLT_FN (BUILT_IN_POWI):
5948 target = expand_builtin_powi (exp, target);
5949 if (target)
5950 return target;
5951 break;
5952
5953 CASE_FLT_FN (BUILT_IN_ATAN2):
5954 CASE_FLT_FN (BUILT_IN_LDEXP):
5955 CASE_FLT_FN (BUILT_IN_SCALB):
5956 CASE_FLT_FN (BUILT_IN_SCALBN):
5957 CASE_FLT_FN (BUILT_IN_SCALBLN):
5958 if (! flag_unsafe_math_optimizations)
5959 break;
5960
5961 CASE_FLT_FN (BUILT_IN_FMOD):
5962 CASE_FLT_FN (BUILT_IN_REMAINDER):
5963 CASE_FLT_FN (BUILT_IN_DREM):
5964 CASE_FLT_FN (BUILT_IN_POW):
5965 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5966 if (target)
5967 return target;
5968 break;
5969
5970 CASE_FLT_FN (BUILT_IN_CEXPI):
5971 target = expand_builtin_cexpi (exp, target);
5972 gcc_assert (target);
5973 return target;
5974
5975 CASE_FLT_FN (BUILT_IN_SIN):
5976 CASE_FLT_FN (BUILT_IN_COS):
5977 if (! flag_unsafe_math_optimizations)
5978 break;
5979 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5980 if (target)
5981 return target;
5982 break;
5983
5984 CASE_FLT_FN (BUILT_IN_SINCOS):
5985 if (! flag_unsafe_math_optimizations)
5986 break;
5987 target = expand_builtin_sincos (exp);
5988 if (target)
5989 return target;
5990 break;
5991
5992 case BUILT_IN_APPLY_ARGS:
5993 return expand_builtin_apply_args ();
5994
5995 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5996 FUNCTION with a copy of the parameters described by
5997 ARGUMENTS, and ARGSIZE. It returns a block of memory
5998 allocated on the stack into which is stored all the registers
5999 that might possibly be used for returning the result of a
6000 function. ARGUMENTS is the value returned by
6001 __builtin_apply_args. ARGSIZE is the number of bytes of
6002 arguments that must be copied. ??? How should this value be
6003 computed? We'll also need a safe worst case value for varargs
6004 functions. */
6005 case BUILT_IN_APPLY:
6006 if (!validate_arglist (exp, POINTER_TYPE,
6007 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6008 && !validate_arglist (exp, REFERENCE_TYPE,
6009 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6010 return const0_rtx;
6011 else
6012 {
6013 rtx ops[3];
6014
6015 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6016 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6017 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6018
6019 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6020 }
6021
6022 /* __builtin_return (RESULT) causes the function to return the
6023 value described by RESULT. RESULT is address of the block of
6024 memory returned by __builtin_apply. */
6025 case BUILT_IN_RETURN:
6026 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6027 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6028 return const0_rtx;
6029
6030 case BUILT_IN_SAVEREGS:
6031 return expand_builtin_saveregs ();
6032
6033 case BUILT_IN_VA_ARG_PACK:
6034 /* All valid uses of __builtin_va_arg_pack () are removed during
6035 inlining. */
6036 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6037 return const0_rtx;
6038
6039 case BUILT_IN_VA_ARG_PACK_LEN:
6040 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6041 inlining. */
6042 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6043 return const0_rtx;
6044
6045 /* Return the address of the first anonymous stack arg. */
6046 case BUILT_IN_NEXT_ARG:
6047 if (fold_builtin_next_arg (exp, false))
6048 return const0_rtx;
6049 return expand_builtin_next_arg ();
6050
6051 case BUILT_IN_CLEAR_CACHE:
6052 target = expand_builtin___clear_cache (exp);
6053 if (target)
6054 return target;
6055 break;
6056
6057 case BUILT_IN_CLASSIFY_TYPE:
6058 return expand_builtin_classify_type (exp);
6059
6060 case BUILT_IN_CONSTANT_P:
6061 return const0_rtx;
6062
6063 case BUILT_IN_FRAME_ADDRESS:
6064 case BUILT_IN_RETURN_ADDRESS:
6065 return expand_builtin_frame_address (fndecl, exp);
6066
6067 /* Returns the address of the area where the structure is returned.
6068 0 otherwise. */
6069 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6070 if (call_expr_nargs (exp) != 0
6071 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6072 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6073 return const0_rtx;
6074 else
6075 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6076
6077 case BUILT_IN_ALLOCA:
6078 case BUILT_IN_ALLOCA_WITH_ALIGN:
6079 /* If the allocation stems from the declaration of a variable-sized
6080 object, it cannot accumulate. */
6081 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6082 if (target)
6083 return target;
6084 break;
6085
6086 case BUILT_IN_STACK_SAVE:
6087 return expand_stack_save ();
6088
6089 case BUILT_IN_STACK_RESTORE:
6090 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6091 return const0_rtx;
6092
6093 case BUILT_IN_BSWAP16:
6094 case BUILT_IN_BSWAP32:
6095 case BUILT_IN_BSWAP64:
6096 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6097 if (target)
6098 return target;
6099 break;
6100
6101 CASE_INT_FN (BUILT_IN_FFS):
6102 target = expand_builtin_unop (target_mode, exp, target,
6103 subtarget, ffs_optab);
6104 if (target)
6105 return target;
6106 break;
6107
6108 CASE_INT_FN (BUILT_IN_CLZ):
6109 target = expand_builtin_unop (target_mode, exp, target,
6110 subtarget, clz_optab);
6111 if (target)
6112 return target;
6113 break;
6114
6115 CASE_INT_FN (BUILT_IN_CTZ):
6116 target = expand_builtin_unop (target_mode, exp, target,
6117 subtarget, ctz_optab);
6118 if (target)
6119 return target;
6120 break;
6121
6122 CASE_INT_FN (BUILT_IN_CLRSB):
6123 target = expand_builtin_unop (target_mode, exp, target,
6124 subtarget, clrsb_optab);
6125 if (target)
6126 return target;
6127 break;
6128
6129 CASE_INT_FN (BUILT_IN_POPCOUNT):
6130 target = expand_builtin_unop (target_mode, exp, target,
6131 subtarget, popcount_optab);
6132 if (target)
6133 return target;
6134 break;
6135
6136 CASE_INT_FN (BUILT_IN_PARITY):
6137 target = expand_builtin_unop (target_mode, exp, target,
6138 subtarget, parity_optab);
6139 if (target)
6140 return target;
6141 break;
6142
6143 case BUILT_IN_STRLEN:
6144 target = expand_builtin_strlen (exp, target, target_mode);
6145 if (target)
6146 return target;
6147 break;
6148
6149 case BUILT_IN_STRCPY:
6150 target = expand_builtin_strcpy (exp, target);
6151 if (target)
6152 return target;
6153 break;
6154
6155 case BUILT_IN_STRNCPY:
6156 target = expand_builtin_strncpy (exp, target);
6157 if (target)
6158 return target;
6159 break;
6160
6161 case BUILT_IN_STPCPY:
6162 target = expand_builtin_stpcpy (exp, target, mode);
6163 if (target)
6164 return target;
6165 break;
6166
6167 case BUILT_IN_MEMCPY:
6168 target = expand_builtin_memcpy (exp, target);
6169 if (target)
6170 return target;
6171 break;
6172
6173 case BUILT_IN_MEMPCPY:
6174 target = expand_builtin_mempcpy (exp, target, mode);
6175 if (target)
6176 return target;
6177 break;
6178
6179 case BUILT_IN_MEMSET:
6180 target = expand_builtin_memset (exp, target, mode);
6181 if (target)
6182 return target;
6183 break;
6184
6185 case BUILT_IN_BZERO:
6186 target = expand_builtin_bzero (exp);
6187 if (target)
6188 return target;
6189 break;
6190
6191 case BUILT_IN_STRCMP:
6192 target = expand_builtin_strcmp (exp, target);
6193 if (target)
6194 return target;
6195 break;
6196
6197 case BUILT_IN_STRNCMP:
6198 target = expand_builtin_strncmp (exp, target, mode);
6199 if (target)
6200 return target;
6201 break;
6202
6203 case BUILT_IN_BCMP:
6204 case BUILT_IN_MEMCMP:
6205 target = expand_builtin_memcmp (exp, target, mode);
6206 if (target)
6207 return target;
6208 break;
6209
6210 case BUILT_IN_SETJMP:
6211 /* This should have been lowered to the builtins below. */
6212 gcc_unreachable ();
6213
6214 case BUILT_IN_SETJMP_SETUP:
6215 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6216 and the receiver label. */
6217 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6218 {
6219 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6220 VOIDmode, EXPAND_NORMAL);
6221 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6222 rtx label_r = label_rtx (label);
6223
6224 /* This is copied from the handling of non-local gotos. */
6225 expand_builtin_setjmp_setup (buf_addr, label_r);
6226 nonlocal_goto_handler_labels
6227 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6228 nonlocal_goto_handler_labels);
6229 /* ??? Do not let expand_label treat us as such since we would
6230 not want to be both on the list of non-local labels and on
6231 the list of forced labels. */
6232 FORCED_LABEL (label) = 0;
6233 return const0_rtx;
6234 }
6235 break;
6236
6237 case BUILT_IN_SETJMP_RECEIVER:
6238 /* __builtin_setjmp_receiver is passed the receiver label. */
6239 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6240 {
6241 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6242 rtx label_r = label_rtx (label);
6243
6244 expand_builtin_setjmp_receiver (label_r);
6245 return const0_rtx;
6246 }
6247 break;
6248
6249 /* __builtin_longjmp is passed a pointer to an array of five words.
6250 It's similar to the C library longjmp function but works with
6251 __builtin_setjmp above. */
6252 case BUILT_IN_LONGJMP:
6253 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6254 {
6255 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6256 VOIDmode, EXPAND_NORMAL);
6257 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6258
6259 if (value != const1_rtx)
6260 {
6261 error ("%<__builtin_longjmp%> second argument must be 1");
6262 return const0_rtx;
6263 }
6264
6265 expand_builtin_longjmp (buf_addr, value);
6266 return const0_rtx;
6267 }
6268 break;
6269
6270 case BUILT_IN_NONLOCAL_GOTO:
6271 target = expand_builtin_nonlocal_goto (exp);
6272 if (target)
6273 return target;
6274 break;
6275
6276 /* This updates the setjmp buffer that is its argument with the value
6277 of the current stack pointer. */
6278 case BUILT_IN_UPDATE_SETJMP_BUF:
6279 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6280 {
6281 rtx buf_addr
6282 = expand_normal (CALL_EXPR_ARG (exp, 0));
6283
6284 expand_builtin_update_setjmp_buf (buf_addr);
6285 return const0_rtx;
6286 }
6287 break;
6288
6289 case BUILT_IN_TRAP:
6290 expand_builtin_trap ();
6291 return const0_rtx;
6292
6293 case BUILT_IN_UNREACHABLE:
6294 expand_builtin_unreachable ();
6295 return const0_rtx;
6296
6297 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6298 case BUILT_IN_SIGNBITD32:
6299 case BUILT_IN_SIGNBITD64:
6300 case BUILT_IN_SIGNBITD128:
6301 target = expand_builtin_signbit (exp, target);
6302 if (target)
6303 return target;
6304 break;
6305
6306 /* Various hooks for the DWARF 2 __throw routine. */
6307 case BUILT_IN_UNWIND_INIT:
6308 expand_builtin_unwind_init ();
6309 return const0_rtx;
6310 case BUILT_IN_DWARF_CFA:
6311 return virtual_cfa_rtx;
6312 #ifdef DWARF2_UNWIND_INFO
6313 case BUILT_IN_DWARF_SP_COLUMN:
6314 return expand_builtin_dwarf_sp_column ();
6315 case BUILT_IN_INIT_DWARF_REG_SIZES:
6316 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6317 return const0_rtx;
6318 #endif
6319 case BUILT_IN_FROB_RETURN_ADDR:
6320 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6321 case BUILT_IN_EXTRACT_RETURN_ADDR:
6322 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6323 case BUILT_IN_EH_RETURN:
6324 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6325 CALL_EXPR_ARG (exp, 1));
6326 return const0_rtx;
6327 #ifdef EH_RETURN_DATA_REGNO
6328 case BUILT_IN_EH_RETURN_DATA_REGNO:
6329 return expand_builtin_eh_return_data_regno (exp);
6330 #endif
6331 case BUILT_IN_EXTEND_POINTER:
6332 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6333 case BUILT_IN_EH_POINTER:
6334 return expand_builtin_eh_pointer (exp);
6335 case BUILT_IN_EH_FILTER:
6336 return expand_builtin_eh_filter (exp);
6337 case BUILT_IN_EH_COPY_VALUES:
6338 return expand_builtin_eh_copy_values (exp);
6339
6340 case BUILT_IN_VA_START:
6341 return expand_builtin_va_start (exp);
6342 case BUILT_IN_VA_END:
6343 return expand_builtin_va_end (exp);
6344 case BUILT_IN_VA_COPY:
6345 return expand_builtin_va_copy (exp);
6346 case BUILT_IN_EXPECT:
6347 return expand_builtin_expect (exp, target);
6348 case BUILT_IN_ASSUME_ALIGNED:
6349 return expand_builtin_assume_aligned (exp, target);
6350 case BUILT_IN_PREFETCH:
6351 expand_builtin_prefetch (exp);
6352 return const0_rtx;
6353
6354 case BUILT_IN_INIT_TRAMPOLINE:
6355 return expand_builtin_init_trampoline (exp, true);
6356 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6357 return expand_builtin_init_trampoline (exp, false);
6358 case BUILT_IN_ADJUST_TRAMPOLINE:
6359 return expand_builtin_adjust_trampoline (exp);
6360
6361 case BUILT_IN_FORK:
6362 case BUILT_IN_EXECL:
6363 case BUILT_IN_EXECV:
6364 case BUILT_IN_EXECLP:
6365 case BUILT_IN_EXECLE:
6366 case BUILT_IN_EXECVP:
6367 case BUILT_IN_EXECVE:
6368 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6369 if (target)
6370 return target;
6371 break;
6372
6373 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6374 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6375 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6376 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6377 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6378 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6379 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6380 if (target)
6381 return target;
6382 break;
6383
6384 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6385 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6386 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6387 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6388 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6389 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6390 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6391 if (target)
6392 return target;
6393 break;
6394
6395 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6396 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6397 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6398 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6399 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6400 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6401 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6402 if (target)
6403 return target;
6404 break;
6405
6406 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6407 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6408 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6409 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6410 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6411 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6412 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6413 if (target)
6414 return target;
6415 break;
6416
6417 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6418 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6419 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6420 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6421 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6422 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6423 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6424 if (target)
6425 return target;
6426 break;
6427
6428 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6429 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6430 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6431 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6432 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6433 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6434 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6435 if (target)
6436 return target;
6437 break;
6438
6439 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6440 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6441 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6442 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6443 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6444 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6445 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6446 if (target)
6447 return target;
6448 break;
6449
6450 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6451 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6452 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6453 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6454 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6455 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6456 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6457 if (target)
6458 return target;
6459 break;
6460
6461 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6462 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6463 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6464 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6465 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6466 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6467 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6468 if (target)
6469 return target;
6470 break;
6471
6472 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6473 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6474 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6475 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6476 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6477 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6478 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6479 if (target)
6480 return target;
6481 break;
6482
6483 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6484 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6485 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6486 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6487 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6488 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6489 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6490 if (target)
6491 return target;
6492 break;
6493
6494 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6495 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6496 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6497 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6498 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6499 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6500 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6501 if (target)
6502 return target;
6503 break;
6504
6505 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6506 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6507 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6508 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6509 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6510 if (mode == VOIDmode)
6511 mode = TYPE_MODE (boolean_type_node);
6512 if (!target || !register_operand (target, mode))
6513 target = gen_reg_rtx (mode);
6514
6515 mode = get_builtin_sync_mode
6516 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6517 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6518 if (target)
6519 return target;
6520 break;
6521
6522 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6523 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6524 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6525 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6526 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6527 mode = get_builtin_sync_mode
6528 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6529 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6530 if (target)
6531 return target;
6532 break;
6533
6534 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6535 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6536 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6537 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6538 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6539 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6540 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6541 if (target)
6542 return target;
6543 break;
6544
6545 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6546 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6547 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6548 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6549 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6550 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6551 expand_builtin_sync_lock_release (mode, exp);
6552 return const0_rtx;
6553
6554 case BUILT_IN_SYNC_SYNCHRONIZE:
6555 expand_builtin_sync_synchronize ();
6556 return const0_rtx;
6557
6558 case BUILT_IN_ATOMIC_EXCHANGE_1:
6559 case BUILT_IN_ATOMIC_EXCHANGE_2:
6560 case BUILT_IN_ATOMIC_EXCHANGE_4:
6561 case BUILT_IN_ATOMIC_EXCHANGE_8:
6562 case BUILT_IN_ATOMIC_EXCHANGE_16:
6563 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6564 target = expand_builtin_atomic_exchange (mode, exp, target);
6565 if (target)
6566 return target;
6567 break;
6568
6569 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6570 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6571 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6572 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6573 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6574 {
6575 unsigned int nargs, z;
6576 vec<tree, va_gc> *vec;
6577
6578 mode =
6579 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6580 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6581 if (target)
6582 return target;
6583
6584 /* If this is turned into an external library call, the weak parameter
6585 must be dropped to match the expected parameter list. */
6586 nargs = call_expr_nargs (exp);
6587 vec_alloc (vec, nargs - 1);
6588 for (z = 0; z < 3; z++)
6589 vec->quick_push (CALL_EXPR_ARG (exp, z));
6590 /* Skip the boolean weak parameter. */
6591 for (z = 4; z < 6; z++)
6592 vec->quick_push (CALL_EXPR_ARG (exp, z));
6593 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6594 break;
6595 }
6596
6597 case BUILT_IN_ATOMIC_LOAD_1:
6598 case BUILT_IN_ATOMIC_LOAD_2:
6599 case BUILT_IN_ATOMIC_LOAD_4:
6600 case BUILT_IN_ATOMIC_LOAD_8:
6601 case BUILT_IN_ATOMIC_LOAD_16:
6602 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6603 target = expand_builtin_atomic_load (mode, exp, target);
6604 if (target)
6605 return target;
6606 break;
6607
6608 case BUILT_IN_ATOMIC_STORE_1:
6609 case BUILT_IN_ATOMIC_STORE_2:
6610 case BUILT_IN_ATOMIC_STORE_4:
6611 case BUILT_IN_ATOMIC_STORE_8:
6612 case BUILT_IN_ATOMIC_STORE_16:
6613 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6614 target = expand_builtin_atomic_store (mode, exp);
6615 if (target)
6616 return const0_rtx;
6617 break;
6618
6619 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6620 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6621 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6622 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6623 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6624 {
6625 enum built_in_function lib;
6626 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6627 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6628 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6629 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6630 ignore, lib);
6631 if (target)
6632 return target;
6633 break;
6634 }
6635 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6636 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6637 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6638 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6639 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6640 {
6641 enum built_in_function lib;
6642 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6643 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6644 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6645 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6646 ignore, lib);
6647 if (target)
6648 return target;
6649 break;
6650 }
6651 case BUILT_IN_ATOMIC_AND_FETCH_1:
6652 case BUILT_IN_ATOMIC_AND_FETCH_2:
6653 case BUILT_IN_ATOMIC_AND_FETCH_4:
6654 case BUILT_IN_ATOMIC_AND_FETCH_8:
6655 case BUILT_IN_ATOMIC_AND_FETCH_16:
6656 {
6657 enum built_in_function lib;
6658 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6659 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6660 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6661 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6662 ignore, lib);
6663 if (target)
6664 return target;
6665 break;
6666 }
6667 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6668 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6669 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6670 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6671 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6672 {
6673 enum built_in_function lib;
6674 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6675 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6676 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6677 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6678 ignore, lib);
6679 if (target)
6680 return target;
6681 break;
6682 }
6683 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6684 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6685 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6686 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6687 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6688 {
6689 enum built_in_function lib;
6690 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6691 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6692 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6693 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6694 ignore, lib);
6695 if (target)
6696 return target;
6697 break;
6698 }
6699 case BUILT_IN_ATOMIC_OR_FETCH_1:
6700 case BUILT_IN_ATOMIC_OR_FETCH_2:
6701 case BUILT_IN_ATOMIC_OR_FETCH_4:
6702 case BUILT_IN_ATOMIC_OR_FETCH_8:
6703 case BUILT_IN_ATOMIC_OR_FETCH_16:
6704 {
6705 enum built_in_function lib;
6706 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6707 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6708 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6709 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6710 ignore, lib);
6711 if (target)
6712 return target;
6713 break;
6714 }
6715 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6716 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6717 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6718 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6719 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6720 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6721 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6722 ignore, BUILT_IN_NONE);
6723 if (target)
6724 return target;
6725 break;
6726
6727 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6728 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6729 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6730 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6731 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6732 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6733 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6734 ignore, BUILT_IN_NONE);
6735 if (target)
6736 return target;
6737 break;
6738
6739 case BUILT_IN_ATOMIC_FETCH_AND_1:
6740 case BUILT_IN_ATOMIC_FETCH_AND_2:
6741 case BUILT_IN_ATOMIC_FETCH_AND_4:
6742 case BUILT_IN_ATOMIC_FETCH_AND_8:
6743 case BUILT_IN_ATOMIC_FETCH_AND_16:
6744 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6745 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6746 ignore, BUILT_IN_NONE);
6747 if (target)
6748 return target;
6749 break;
6750
6751 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6752 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6753 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6754 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6755 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6756 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6757 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6758 ignore, BUILT_IN_NONE);
6759 if (target)
6760 return target;
6761 break;
6762
6763 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6764 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6765 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6766 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6767 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6768 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6769 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6770 ignore, BUILT_IN_NONE);
6771 if (target)
6772 return target;
6773 break;
6774
6775 case BUILT_IN_ATOMIC_FETCH_OR_1:
6776 case BUILT_IN_ATOMIC_FETCH_OR_2:
6777 case BUILT_IN_ATOMIC_FETCH_OR_4:
6778 case BUILT_IN_ATOMIC_FETCH_OR_8:
6779 case BUILT_IN_ATOMIC_FETCH_OR_16:
6780 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6781 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6782 ignore, BUILT_IN_NONE);
6783 if (target)
6784 return target;
6785 break;
6786
6787 case BUILT_IN_ATOMIC_TEST_AND_SET:
6788 return expand_builtin_atomic_test_and_set (exp, target);
6789
6790 case BUILT_IN_ATOMIC_CLEAR:
6791 return expand_builtin_atomic_clear (exp);
6792
6793 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6794 return expand_builtin_atomic_always_lock_free (exp);
6795
6796 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6797 target = expand_builtin_atomic_is_lock_free (exp);
6798 if (target)
6799 return target;
6800 break;
6801
6802 case BUILT_IN_ATOMIC_THREAD_FENCE:
6803 expand_builtin_atomic_thread_fence (exp);
6804 return const0_rtx;
6805
6806 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6807 expand_builtin_atomic_signal_fence (exp);
6808 return const0_rtx;
6809
6810 case BUILT_IN_OBJECT_SIZE:
6811 return expand_builtin_object_size (exp);
6812
6813 case BUILT_IN_MEMCPY_CHK:
6814 case BUILT_IN_MEMPCPY_CHK:
6815 case BUILT_IN_MEMMOVE_CHK:
6816 case BUILT_IN_MEMSET_CHK:
6817 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6818 if (target)
6819 return target;
6820 break;
6821
6822 case BUILT_IN_STRCPY_CHK:
6823 case BUILT_IN_STPCPY_CHK:
6824 case BUILT_IN_STRNCPY_CHK:
6825 case BUILT_IN_STPNCPY_CHK:
6826 case BUILT_IN_STRCAT_CHK:
6827 case BUILT_IN_STRNCAT_CHK:
6828 case BUILT_IN_SNPRINTF_CHK:
6829 case BUILT_IN_VSNPRINTF_CHK:
6830 maybe_emit_chk_warning (exp, fcode);
6831 break;
6832
6833 case BUILT_IN_SPRINTF_CHK:
6834 case BUILT_IN_VSPRINTF_CHK:
6835 maybe_emit_sprintf_chk_warning (exp, fcode);
6836 break;
6837
6838 case BUILT_IN_FREE:
6839 if (warn_free_nonheap_object)
6840 maybe_emit_free_warning (exp);
6841 break;
6842
6843 case BUILT_IN_THREAD_POINTER:
6844 return expand_builtin_thread_pointer (exp, target);
6845
6846 case BUILT_IN_SET_THREAD_POINTER:
6847 expand_builtin_set_thread_pointer (exp);
6848 return const0_rtx;
6849
6850 case BUILT_IN_CILK_DETACH:
6851 expand_builtin_cilk_detach (exp);
6852 return const0_rtx;
6853
6854 case BUILT_IN_CILK_POP_FRAME:
6855 expand_builtin_cilk_pop_frame (exp);
6856 return const0_rtx;
6857
6858 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
6859 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
6860 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
6861 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
6862 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
6863 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
6864 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
6865 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
6866 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
6867 case BUILT_IN_CHKP_GET_PTR_LBOUND:
6868 case BUILT_IN_CHKP_GET_PTR_UBOUND:
6869 /* We allow user CHKP builtins if Pointer Bounds
6870 Checker is off. */
6871 if (!chkp_function_instrumented_p (current_function_decl))
6872 {
6873 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
6874 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6875 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
6876 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
6877 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
6878 return expand_normal (CALL_EXPR_ARG (exp, 0));
6879 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
6880 return expand_normal (size_zero_node);
6881 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
6882 return expand_normal (size_int (-1));
6883 else
6884 return const0_rtx;
6885 }
6886 /* FALLTHROUGH */
6887
6888 case BUILT_IN_CHKP_BNDMK:
6889 case BUILT_IN_CHKP_BNDSTX:
6890 case BUILT_IN_CHKP_BNDCL:
6891 case BUILT_IN_CHKP_BNDCU:
6892 case BUILT_IN_CHKP_BNDLDX:
6893 case BUILT_IN_CHKP_BNDRET:
6894 case BUILT_IN_CHKP_INTERSECT:
6895 case BUILT_IN_CHKP_NARROW:
6896 case BUILT_IN_CHKP_EXTRACT_LOWER:
6897 case BUILT_IN_CHKP_EXTRACT_UPPER:
6898 /* Software implementation of Pointer Bounds Checker is NYI.
6899 Target support is required. */
6900 error ("Your target platform does not support -fcheck-pointer-bounds");
6901 break;
6902
6903 default: /* just do library call, if unknown builtin */
6904 break;
6905 }
6906
6907 /* The switch statement above can drop through to cause the function
6908 to be called normally. */
6909 return expand_call (exp, target, ignore);
6910 }
6911
6912 /* Determine whether a tree node represents a call to a built-in
6913 function. If the tree T is a call to a built-in function with
6914 the right number of arguments of the appropriate types, return
6915 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6916 Otherwise the return value is END_BUILTINS. */
6917
6918 enum built_in_function
6919 builtin_mathfn_code (const_tree t)
6920 {
6921 const_tree fndecl, arg, parmlist;
6922 const_tree argtype, parmtype;
6923 const_call_expr_arg_iterator iter;
6924
6925 if (TREE_CODE (t) != CALL_EXPR
6926 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6927 return END_BUILTINS;
6928
6929 fndecl = get_callee_fndecl (t);
6930 if (fndecl == NULL_TREE
6931 || TREE_CODE (fndecl) != FUNCTION_DECL
6932 || ! DECL_BUILT_IN (fndecl)
6933 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6934 return END_BUILTINS;
6935
6936 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6937 init_const_call_expr_arg_iterator (t, &iter);
6938 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6939 {
6940 /* If a function doesn't take a variable number of arguments,
6941 the last element in the list will have type `void'. */
6942 parmtype = TREE_VALUE (parmlist);
6943 if (VOID_TYPE_P (parmtype))
6944 {
6945 if (more_const_call_expr_args_p (&iter))
6946 return END_BUILTINS;
6947 return DECL_FUNCTION_CODE (fndecl);
6948 }
6949
6950 if (! more_const_call_expr_args_p (&iter))
6951 return END_BUILTINS;
6952
6953 arg = next_const_call_expr_arg (&iter);
6954 argtype = TREE_TYPE (arg);
6955
6956 if (SCALAR_FLOAT_TYPE_P (parmtype))
6957 {
6958 if (! SCALAR_FLOAT_TYPE_P (argtype))
6959 return END_BUILTINS;
6960 }
6961 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6962 {
6963 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6964 return END_BUILTINS;
6965 }
6966 else if (POINTER_TYPE_P (parmtype))
6967 {
6968 if (! POINTER_TYPE_P (argtype))
6969 return END_BUILTINS;
6970 }
6971 else if (INTEGRAL_TYPE_P (parmtype))
6972 {
6973 if (! INTEGRAL_TYPE_P (argtype))
6974 return END_BUILTINS;
6975 }
6976 else
6977 return END_BUILTINS;
6978 }
6979
6980 /* Variable-length argument list. */
6981 return DECL_FUNCTION_CODE (fndecl);
6982 }
6983
6984 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6985 evaluate to a constant. */
6986
6987 static tree
6988 fold_builtin_constant_p (tree arg)
6989 {
6990 /* We return 1 for a numeric type that's known to be a constant
6991 value at compile-time or for an aggregate type that's a
6992 literal constant. */
6993 STRIP_NOPS (arg);
6994
6995 /* If we know this is a constant, emit the constant of one. */
6996 if (CONSTANT_CLASS_P (arg)
6997 || (TREE_CODE (arg) == CONSTRUCTOR
6998 && TREE_CONSTANT (arg)))
6999 return integer_one_node;
7000 if (TREE_CODE (arg) == ADDR_EXPR)
7001 {
7002 tree op = TREE_OPERAND (arg, 0);
7003 if (TREE_CODE (op) == STRING_CST
7004 || (TREE_CODE (op) == ARRAY_REF
7005 && integer_zerop (TREE_OPERAND (op, 1))
7006 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7007 return integer_one_node;
7008 }
7009
7010 /* If this expression has side effects, show we don't know it to be a
7011 constant. Likewise if it's a pointer or aggregate type since in
7012 those case we only want literals, since those are only optimized
7013 when generating RTL, not later.
7014 And finally, if we are compiling an initializer, not code, we
7015 need to return a definite result now; there's not going to be any
7016 more optimization done. */
7017 if (TREE_SIDE_EFFECTS (arg)
7018 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7019 || POINTER_TYPE_P (TREE_TYPE (arg))
7020 || cfun == 0
7021 || folding_initializer
7022 || force_folding_builtin_constant_p)
7023 return integer_zero_node;
7024
7025 return NULL_TREE;
7026 }
7027
7028 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7029 return it as a truthvalue. */
7030
7031 static tree
7032 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7033 tree predictor)
7034 {
7035 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7036
7037 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7038 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7039 ret_type = TREE_TYPE (TREE_TYPE (fn));
7040 pred_type = TREE_VALUE (arg_types);
7041 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7042
7043 pred = fold_convert_loc (loc, pred_type, pred);
7044 expected = fold_convert_loc (loc, expected_type, expected);
7045 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7046 predictor);
7047
7048 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7049 build_int_cst (ret_type, 0));
7050 }
7051
7052 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7053 NULL_TREE if no simplification is possible. */
7054
7055 tree
7056 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7057 {
7058 tree inner, fndecl, inner_arg0;
7059 enum tree_code code;
7060
7061 /* Distribute the expected value over short-circuiting operators.
7062 See through the cast from truthvalue_type_node to long. */
7063 inner_arg0 = arg0;
7064 while (CONVERT_EXPR_P (inner_arg0)
7065 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7066 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7067 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7068
7069 /* If this is a builtin_expect within a builtin_expect keep the
7070 inner one. See through a comparison against a constant. It
7071 might have been added to create a thruthvalue. */
7072 inner = inner_arg0;
7073
7074 if (COMPARISON_CLASS_P (inner)
7075 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7076 inner = TREE_OPERAND (inner, 0);
7077
7078 if (TREE_CODE (inner) == CALL_EXPR
7079 && (fndecl = get_callee_fndecl (inner))
7080 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7081 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7082 return arg0;
7083
7084 inner = inner_arg0;
7085 code = TREE_CODE (inner);
7086 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7087 {
7088 tree op0 = TREE_OPERAND (inner, 0);
7089 tree op1 = TREE_OPERAND (inner, 1);
7090
7091 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7092 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7093 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7094
7095 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7096 }
7097
7098 /* If the argument isn't invariant then there's nothing else we can do. */
7099 if (!TREE_CONSTANT (inner_arg0))
7100 return NULL_TREE;
7101
7102 /* If we expect that a comparison against the argument will fold to
7103 a constant return the constant. In practice, this means a true
7104 constant or the address of a non-weak symbol. */
7105 inner = inner_arg0;
7106 STRIP_NOPS (inner);
7107 if (TREE_CODE (inner) == ADDR_EXPR)
7108 {
7109 do
7110 {
7111 inner = TREE_OPERAND (inner, 0);
7112 }
7113 while (TREE_CODE (inner) == COMPONENT_REF
7114 || TREE_CODE (inner) == ARRAY_REF);
7115 if ((TREE_CODE (inner) == VAR_DECL
7116 || TREE_CODE (inner) == FUNCTION_DECL)
7117 && DECL_WEAK (inner))
7118 return NULL_TREE;
7119 }
7120
7121 /* Otherwise, ARG0 already has the proper type for the return value. */
7122 return arg0;
7123 }
7124
7125 /* Fold a call to __builtin_classify_type with argument ARG. */
7126
7127 static tree
7128 fold_builtin_classify_type (tree arg)
7129 {
7130 if (arg == 0)
7131 return build_int_cst (integer_type_node, no_type_class);
7132
7133 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7134 }
7135
7136 /* Fold a call to __builtin_strlen with argument ARG. */
7137
7138 static tree
7139 fold_builtin_strlen (location_t loc, tree type, tree arg)
7140 {
7141 if (!validate_arg (arg, POINTER_TYPE))
7142 return NULL_TREE;
7143 else
7144 {
7145 tree len = c_strlen (arg, 0);
7146
7147 if (len)
7148 return fold_convert_loc (loc, type, len);
7149
7150 return NULL_TREE;
7151 }
7152 }
7153
7154 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7155
7156 static tree
7157 fold_builtin_inf (location_t loc, tree type, int warn)
7158 {
7159 REAL_VALUE_TYPE real;
7160
7161 /* __builtin_inff is intended to be usable to define INFINITY on all
7162 targets. If an infinity is not available, INFINITY expands "to a
7163 positive constant of type float that overflows at translation
7164 time", footnote "In this case, using INFINITY will violate the
7165 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7166 Thus we pedwarn to ensure this constraint violation is
7167 diagnosed. */
7168 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7169 pedwarn (loc, 0, "target format does not support infinity");
7170
7171 real_inf (&real);
7172 return build_real (type, real);
7173 }
7174
7175 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7176
7177 static tree
7178 fold_builtin_nan (tree arg, tree type, int quiet)
7179 {
7180 REAL_VALUE_TYPE real;
7181 const char *str;
7182
7183 if (!validate_arg (arg, POINTER_TYPE))
7184 return NULL_TREE;
7185 str = c_getstr (arg);
7186 if (!str)
7187 return NULL_TREE;
7188
7189 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7190 return NULL_TREE;
7191
7192 return build_real (type, real);
7193 }
7194
7195 /* Return true if the floating point expression T has an integer value.
7196 We also allow +Inf, -Inf and NaN to be considered integer values. */
7197
7198 static bool
7199 integer_valued_real_p (tree t)
7200 {
7201 switch (TREE_CODE (t))
7202 {
7203 case FLOAT_EXPR:
7204 return true;
7205
7206 case ABS_EXPR:
7207 case SAVE_EXPR:
7208 return integer_valued_real_p (TREE_OPERAND (t, 0));
7209
7210 case COMPOUND_EXPR:
7211 case MODIFY_EXPR:
7212 case BIND_EXPR:
7213 return integer_valued_real_p (TREE_OPERAND (t, 1));
7214
7215 case PLUS_EXPR:
7216 case MINUS_EXPR:
7217 case MULT_EXPR:
7218 case MIN_EXPR:
7219 case MAX_EXPR:
7220 return integer_valued_real_p (TREE_OPERAND (t, 0))
7221 && integer_valued_real_p (TREE_OPERAND (t, 1));
7222
7223 case COND_EXPR:
7224 return integer_valued_real_p (TREE_OPERAND (t, 1))
7225 && integer_valued_real_p (TREE_OPERAND (t, 2));
7226
7227 case REAL_CST:
7228 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7229
7230 CASE_CONVERT:
7231 {
7232 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7233 if (TREE_CODE (type) == INTEGER_TYPE)
7234 return true;
7235 if (TREE_CODE (type) == REAL_TYPE)
7236 return integer_valued_real_p (TREE_OPERAND (t, 0));
7237 break;
7238 }
7239
7240 case CALL_EXPR:
7241 switch (builtin_mathfn_code (t))
7242 {
7243 CASE_FLT_FN (BUILT_IN_CEIL):
7244 CASE_FLT_FN (BUILT_IN_FLOOR):
7245 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7246 CASE_FLT_FN (BUILT_IN_RINT):
7247 CASE_FLT_FN (BUILT_IN_ROUND):
7248 CASE_FLT_FN (BUILT_IN_TRUNC):
7249 return true;
7250
7251 CASE_FLT_FN (BUILT_IN_FMIN):
7252 CASE_FLT_FN (BUILT_IN_FMAX):
7253 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7254 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7255
7256 default:
7257 break;
7258 }
7259 break;
7260
7261 default:
7262 break;
7263 }
7264 return false;
7265 }
7266
7267 /* FNDECL is assumed to be a builtin where truncation can be propagated
7268 across (for instance floor((double)f) == (double)floorf (f).
7269 Do the transformation for a call with argument ARG. */
7270
7271 static tree
7272 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7273 {
7274 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7275
7276 if (!validate_arg (arg, REAL_TYPE))
7277 return NULL_TREE;
7278
7279 /* Integer rounding functions are idempotent. */
7280 if (fcode == builtin_mathfn_code (arg))
7281 return arg;
7282
7283 /* If argument is already integer valued, and we don't need to worry
7284 about setting errno, there's no need to perform rounding. */
7285 if (! flag_errno_math && integer_valued_real_p (arg))
7286 return arg;
7287
7288 if (optimize)
7289 {
7290 tree arg0 = strip_float_extensions (arg);
7291 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7292 tree newtype = TREE_TYPE (arg0);
7293 tree decl;
7294
7295 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7296 && (decl = mathfn_built_in (newtype, fcode)))
7297 return fold_convert_loc (loc, ftype,
7298 build_call_expr_loc (loc, decl, 1,
7299 fold_convert_loc (loc,
7300 newtype,
7301 arg0)));
7302 }
7303 return NULL_TREE;
7304 }
7305
7306 /* FNDECL is assumed to be builtin which can narrow the FP type of
7307 the argument, for instance lround((double)f) -> lroundf (f).
7308 Do the transformation for a call with argument ARG. */
7309
7310 static tree
7311 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7312 {
7313 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7314
7315 if (!validate_arg (arg, REAL_TYPE))
7316 return NULL_TREE;
7317
7318 /* If argument is already integer valued, and we don't need to worry
7319 about setting errno, there's no need to perform rounding. */
7320 if (! flag_errno_math && integer_valued_real_p (arg))
7321 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7322 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7323
7324 if (optimize)
7325 {
7326 tree ftype = TREE_TYPE (arg);
7327 tree arg0 = strip_float_extensions (arg);
7328 tree newtype = TREE_TYPE (arg0);
7329 tree decl;
7330
7331 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7332 && (decl = mathfn_built_in (newtype, fcode)))
7333 return build_call_expr_loc (loc, decl, 1,
7334 fold_convert_loc (loc, newtype, arg0));
7335 }
7336
7337 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7338 sizeof (int) == sizeof (long). */
7339 if (TYPE_PRECISION (integer_type_node)
7340 == TYPE_PRECISION (long_integer_type_node))
7341 {
7342 tree newfn = NULL_TREE;
7343 switch (fcode)
7344 {
7345 CASE_FLT_FN (BUILT_IN_ICEIL):
7346 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7347 break;
7348
7349 CASE_FLT_FN (BUILT_IN_IFLOOR):
7350 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7351 break;
7352
7353 CASE_FLT_FN (BUILT_IN_IROUND):
7354 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7355 break;
7356
7357 CASE_FLT_FN (BUILT_IN_IRINT):
7358 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7359 break;
7360
7361 default:
7362 break;
7363 }
7364
7365 if (newfn)
7366 {
7367 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7368 return fold_convert_loc (loc,
7369 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7370 }
7371 }
7372
7373 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7374 sizeof (long long) == sizeof (long). */
7375 if (TYPE_PRECISION (long_long_integer_type_node)
7376 == TYPE_PRECISION (long_integer_type_node))
7377 {
7378 tree newfn = NULL_TREE;
7379 switch (fcode)
7380 {
7381 CASE_FLT_FN (BUILT_IN_LLCEIL):
7382 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7383 break;
7384
7385 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7386 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7387 break;
7388
7389 CASE_FLT_FN (BUILT_IN_LLROUND):
7390 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7391 break;
7392
7393 CASE_FLT_FN (BUILT_IN_LLRINT):
7394 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7395 break;
7396
7397 default:
7398 break;
7399 }
7400
7401 if (newfn)
7402 {
7403 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7404 return fold_convert_loc (loc,
7405 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7406 }
7407 }
7408
7409 return NULL_TREE;
7410 }
7411
7412 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7413 return type. Return NULL_TREE if no simplification can be made. */
7414
7415 static tree
7416 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7417 {
7418 tree res;
7419
7420 if (!validate_arg (arg, COMPLEX_TYPE)
7421 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7422 return NULL_TREE;
7423
7424 /* Calculate the result when the argument is a constant. */
7425 if (TREE_CODE (arg) == COMPLEX_CST
7426 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7427 type, mpfr_hypot)))
7428 return res;
7429
7430 if (TREE_CODE (arg) == COMPLEX_EXPR)
7431 {
7432 tree real = TREE_OPERAND (arg, 0);
7433 tree imag = TREE_OPERAND (arg, 1);
7434
7435 /* If either part is zero, cabs is fabs of the other. */
7436 if (real_zerop (real))
7437 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7438 if (real_zerop (imag))
7439 return fold_build1_loc (loc, ABS_EXPR, type, real);
7440
7441 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7442 if (flag_unsafe_math_optimizations
7443 && operand_equal_p (real, imag, OEP_PURE_SAME))
7444 {
7445 const REAL_VALUE_TYPE sqrt2_trunc
7446 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7447 STRIP_NOPS (real);
7448 return fold_build2_loc (loc, MULT_EXPR, type,
7449 fold_build1_loc (loc, ABS_EXPR, type, real),
7450 build_real (type, sqrt2_trunc));
7451 }
7452 }
7453
7454 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7455 if (TREE_CODE (arg) == NEGATE_EXPR
7456 || TREE_CODE (arg) == CONJ_EXPR)
7457 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7458
7459 /* Don't do this when optimizing for size. */
7460 if (flag_unsafe_math_optimizations
7461 && optimize && optimize_function_for_speed_p (cfun))
7462 {
7463 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7464
7465 if (sqrtfn != NULL_TREE)
7466 {
7467 tree rpart, ipart, result;
7468
7469 arg = builtin_save_expr (arg);
7470
7471 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7472 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7473
7474 rpart = builtin_save_expr (rpart);
7475 ipart = builtin_save_expr (ipart);
7476
7477 result = fold_build2_loc (loc, PLUS_EXPR, type,
7478 fold_build2_loc (loc, MULT_EXPR, type,
7479 rpart, rpart),
7480 fold_build2_loc (loc, MULT_EXPR, type,
7481 ipart, ipart));
7482
7483 return build_call_expr_loc (loc, sqrtfn, 1, result);
7484 }
7485 }
7486
7487 return NULL_TREE;
7488 }
7489
7490 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7491 complex tree type of the result. If NEG is true, the imaginary
7492 zero is negative. */
7493
7494 static tree
7495 build_complex_cproj (tree type, bool neg)
7496 {
7497 REAL_VALUE_TYPE rinf, rzero = dconst0;
7498
7499 real_inf (&rinf);
7500 rzero.sign = neg;
7501 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7502 build_real (TREE_TYPE (type), rzero));
7503 }
7504
7505 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7506 return type. Return NULL_TREE if no simplification can be made. */
7507
7508 static tree
7509 fold_builtin_cproj (location_t loc, tree arg, tree type)
7510 {
7511 if (!validate_arg (arg, COMPLEX_TYPE)
7512 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7513 return NULL_TREE;
7514
7515 /* If there are no infinities, return arg. */
7516 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7517 return non_lvalue_loc (loc, arg);
7518
7519 /* Calculate the result when the argument is a constant. */
7520 if (TREE_CODE (arg) == COMPLEX_CST)
7521 {
7522 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7523 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7524
7525 if (real_isinf (real) || real_isinf (imag))
7526 return build_complex_cproj (type, imag->sign);
7527 else
7528 return arg;
7529 }
7530 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7531 {
7532 tree real = TREE_OPERAND (arg, 0);
7533 tree imag = TREE_OPERAND (arg, 1);
7534
7535 STRIP_NOPS (real);
7536 STRIP_NOPS (imag);
7537
7538 /* If the real part is inf and the imag part is known to be
7539 nonnegative, return (inf + 0i). Remember side-effects are
7540 possible in the imag part. */
7541 if (TREE_CODE (real) == REAL_CST
7542 && real_isinf (TREE_REAL_CST_PTR (real))
7543 && tree_expr_nonnegative_p (imag))
7544 return omit_one_operand_loc (loc, type,
7545 build_complex_cproj (type, false),
7546 arg);
7547
7548 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7549 Remember side-effects are possible in the real part. */
7550 if (TREE_CODE (imag) == REAL_CST
7551 && real_isinf (TREE_REAL_CST_PTR (imag)))
7552 return
7553 omit_one_operand_loc (loc, type,
7554 build_complex_cproj (type, TREE_REAL_CST_PTR
7555 (imag)->sign), arg);
7556 }
7557
7558 return NULL_TREE;
7559 }
7560
7561 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7562 Return NULL_TREE if no simplification can be made. */
7563
7564 static tree
7565 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7566 {
7567
7568 enum built_in_function fcode;
7569 tree res;
7570
7571 if (!validate_arg (arg, REAL_TYPE))
7572 return NULL_TREE;
7573
7574 /* Calculate the result when the argument is a constant. */
7575 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7576 return res;
7577
7578 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7579 fcode = builtin_mathfn_code (arg);
7580 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7581 {
7582 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7583 arg = fold_build2_loc (loc, MULT_EXPR, type,
7584 CALL_EXPR_ARG (arg, 0),
7585 build_real (type, dconsthalf));
7586 return build_call_expr_loc (loc, expfn, 1, arg);
7587 }
7588
7589 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7590 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7591 {
7592 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7593
7594 if (powfn)
7595 {
7596 tree arg0 = CALL_EXPR_ARG (arg, 0);
7597 tree tree_root;
7598 /* The inner root was either sqrt or cbrt. */
7599 /* This was a conditional expression but it triggered a bug
7600 in Sun C 5.5. */
7601 REAL_VALUE_TYPE dconstroot;
7602 if (BUILTIN_SQRT_P (fcode))
7603 dconstroot = dconsthalf;
7604 else
7605 dconstroot = dconst_third ();
7606
7607 /* Adjust for the outer root. */
7608 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7609 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7610 tree_root = build_real (type, dconstroot);
7611 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7612 }
7613 }
7614
7615 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7616 if (flag_unsafe_math_optimizations
7617 && (fcode == BUILT_IN_POW
7618 || fcode == BUILT_IN_POWF
7619 || fcode == BUILT_IN_POWL))
7620 {
7621 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7622 tree arg0 = CALL_EXPR_ARG (arg, 0);
7623 tree arg1 = CALL_EXPR_ARG (arg, 1);
7624 tree narg1;
7625 if (!tree_expr_nonnegative_p (arg0))
7626 arg0 = build1 (ABS_EXPR, type, arg0);
7627 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7628 build_real (type, dconsthalf));
7629 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7630 }
7631
7632 return NULL_TREE;
7633 }
7634
7635 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7636 Return NULL_TREE if no simplification can be made. */
7637
7638 static tree
7639 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7640 {
7641 const enum built_in_function fcode = builtin_mathfn_code (arg);
7642 tree res;
7643
7644 if (!validate_arg (arg, REAL_TYPE))
7645 return NULL_TREE;
7646
7647 /* Calculate the result when the argument is a constant. */
7648 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7649 return res;
7650
7651 if (flag_unsafe_math_optimizations)
7652 {
7653 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7654 if (BUILTIN_EXPONENT_P (fcode))
7655 {
7656 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7657 const REAL_VALUE_TYPE third_trunc =
7658 real_value_truncate (TYPE_MODE (type), dconst_third ());
7659 arg = fold_build2_loc (loc, MULT_EXPR, type,
7660 CALL_EXPR_ARG (arg, 0),
7661 build_real (type, third_trunc));
7662 return build_call_expr_loc (loc, expfn, 1, arg);
7663 }
7664
7665 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7666 if (BUILTIN_SQRT_P (fcode))
7667 {
7668 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7669
7670 if (powfn)
7671 {
7672 tree arg0 = CALL_EXPR_ARG (arg, 0);
7673 tree tree_root;
7674 REAL_VALUE_TYPE dconstroot = dconst_third ();
7675
7676 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7677 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7678 tree_root = build_real (type, dconstroot);
7679 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7680 }
7681 }
7682
7683 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7684 if (BUILTIN_CBRT_P (fcode))
7685 {
7686 tree arg0 = CALL_EXPR_ARG (arg, 0);
7687 if (tree_expr_nonnegative_p (arg0))
7688 {
7689 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7690
7691 if (powfn)
7692 {
7693 tree tree_root;
7694 REAL_VALUE_TYPE dconstroot;
7695
7696 real_arithmetic (&dconstroot, MULT_EXPR,
7697 dconst_third_ptr (), dconst_third_ptr ());
7698 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7699 tree_root = build_real (type, dconstroot);
7700 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7701 }
7702 }
7703 }
7704
7705 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7706 if (fcode == BUILT_IN_POW
7707 || fcode == BUILT_IN_POWF
7708 || fcode == BUILT_IN_POWL)
7709 {
7710 tree arg00 = CALL_EXPR_ARG (arg, 0);
7711 tree arg01 = CALL_EXPR_ARG (arg, 1);
7712 if (tree_expr_nonnegative_p (arg00))
7713 {
7714 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7715 const REAL_VALUE_TYPE dconstroot
7716 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7717 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7718 build_real (type, dconstroot));
7719 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7720 }
7721 }
7722 }
7723 return NULL_TREE;
7724 }
7725
7726 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7727 TYPE is the type of the return value. Return NULL_TREE if no
7728 simplification can be made. */
7729
7730 static tree
7731 fold_builtin_cos (location_t loc,
7732 tree arg, tree type, tree fndecl)
7733 {
7734 tree res, narg;
7735
7736 if (!validate_arg (arg, REAL_TYPE))
7737 return NULL_TREE;
7738
7739 /* Calculate the result when the argument is a constant. */
7740 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7741 return res;
7742
7743 /* Optimize cos(-x) into cos (x). */
7744 if ((narg = fold_strip_sign_ops (arg)))
7745 return build_call_expr_loc (loc, fndecl, 1, narg);
7746
7747 return NULL_TREE;
7748 }
7749
7750 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7751 Return NULL_TREE if no simplification can be made. */
7752
7753 static tree
7754 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7755 {
7756 if (validate_arg (arg, REAL_TYPE))
7757 {
7758 tree res, narg;
7759
7760 /* Calculate the result when the argument is a constant. */
7761 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7762 return res;
7763
7764 /* Optimize cosh(-x) into cosh (x). */
7765 if ((narg = fold_strip_sign_ops (arg)))
7766 return build_call_expr_loc (loc, fndecl, 1, narg);
7767 }
7768
7769 return NULL_TREE;
7770 }
7771
7772 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7773 argument ARG. TYPE is the type of the return value. Return
7774 NULL_TREE if no simplification can be made. */
7775
7776 static tree
7777 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7778 bool hyper)
7779 {
7780 if (validate_arg (arg, COMPLEX_TYPE)
7781 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7782 {
7783 tree tmp;
7784
7785 /* Calculate the result when the argument is a constant. */
7786 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7787 return tmp;
7788
7789 /* Optimize fn(-x) into fn(x). */
7790 if ((tmp = fold_strip_sign_ops (arg)))
7791 return build_call_expr_loc (loc, fndecl, 1, tmp);
7792 }
7793
7794 return NULL_TREE;
7795 }
7796
7797 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7798 Return NULL_TREE if no simplification can be made. */
7799
7800 static tree
7801 fold_builtin_tan (tree arg, tree type)
7802 {
7803 enum built_in_function fcode;
7804 tree res;
7805
7806 if (!validate_arg (arg, REAL_TYPE))
7807 return NULL_TREE;
7808
7809 /* Calculate the result when the argument is a constant. */
7810 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7811 return res;
7812
7813 /* Optimize tan(atan(x)) = x. */
7814 fcode = builtin_mathfn_code (arg);
7815 if (flag_unsafe_math_optimizations
7816 && (fcode == BUILT_IN_ATAN
7817 || fcode == BUILT_IN_ATANF
7818 || fcode == BUILT_IN_ATANL))
7819 return CALL_EXPR_ARG (arg, 0);
7820
7821 return NULL_TREE;
7822 }
7823
7824 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7825 NULL_TREE if no simplification can be made. */
7826
7827 static tree
7828 fold_builtin_sincos (location_t loc,
7829 tree arg0, tree arg1, tree arg2)
7830 {
7831 tree type;
7832 tree res, fn, call;
7833
7834 if (!validate_arg (arg0, REAL_TYPE)
7835 || !validate_arg (arg1, POINTER_TYPE)
7836 || !validate_arg (arg2, POINTER_TYPE))
7837 return NULL_TREE;
7838
7839 type = TREE_TYPE (arg0);
7840
7841 /* Calculate the result when the argument is a constant. */
7842 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7843 return res;
7844
7845 /* Canonicalize sincos to cexpi. */
7846 if (!targetm.libc_has_function (function_c99_math_complex))
7847 return NULL_TREE;
7848 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7849 if (!fn)
7850 return NULL_TREE;
7851
7852 call = build_call_expr_loc (loc, fn, 1, arg0);
7853 call = builtin_save_expr (call);
7854
7855 return build2 (COMPOUND_EXPR, void_type_node,
7856 build2 (MODIFY_EXPR, void_type_node,
7857 build_fold_indirect_ref_loc (loc, arg1),
7858 build1 (IMAGPART_EXPR, type, call)),
7859 build2 (MODIFY_EXPR, void_type_node,
7860 build_fold_indirect_ref_loc (loc, arg2),
7861 build1 (REALPART_EXPR, type, call)));
7862 }
7863
7864 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7865 NULL_TREE if no simplification can be made. */
7866
7867 static tree
7868 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7869 {
7870 tree rtype;
7871 tree realp, imagp, ifn;
7872 tree res;
7873
7874 if (!validate_arg (arg0, COMPLEX_TYPE)
7875 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7876 return NULL_TREE;
7877
7878 /* Calculate the result when the argument is a constant. */
7879 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7880 return res;
7881
7882 rtype = TREE_TYPE (TREE_TYPE (arg0));
7883
7884 /* In case we can figure out the real part of arg0 and it is constant zero
7885 fold to cexpi. */
7886 if (!targetm.libc_has_function (function_c99_math_complex))
7887 return NULL_TREE;
7888 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7889 if (!ifn)
7890 return NULL_TREE;
7891
7892 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7893 && real_zerop (realp))
7894 {
7895 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7896 return build_call_expr_loc (loc, ifn, 1, narg);
7897 }
7898
7899 /* In case we can easily decompose real and imaginary parts split cexp
7900 to exp (r) * cexpi (i). */
7901 if (flag_unsafe_math_optimizations
7902 && realp)
7903 {
7904 tree rfn, rcall, icall;
7905
7906 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7907 if (!rfn)
7908 return NULL_TREE;
7909
7910 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7911 if (!imagp)
7912 return NULL_TREE;
7913
7914 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7915 icall = builtin_save_expr (icall);
7916 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7917 rcall = builtin_save_expr (rcall);
7918 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7919 fold_build2_loc (loc, MULT_EXPR, rtype,
7920 rcall,
7921 fold_build1_loc (loc, REALPART_EXPR,
7922 rtype, icall)),
7923 fold_build2_loc (loc, MULT_EXPR, rtype,
7924 rcall,
7925 fold_build1_loc (loc, IMAGPART_EXPR,
7926 rtype, icall)));
7927 }
7928
7929 return NULL_TREE;
7930 }
7931
7932 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7933 Return NULL_TREE if no simplification can be made. */
7934
7935 static tree
7936 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7937 {
7938 if (!validate_arg (arg, REAL_TYPE))
7939 return NULL_TREE;
7940
7941 /* Optimize trunc of constant value. */
7942 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7943 {
7944 REAL_VALUE_TYPE r, x;
7945 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7946
7947 x = TREE_REAL_CST (arg);
7948 real_trunc (&r, TYPE_MODE (type), &x);
7949 return build_real (type, r);
7950 }
7951
7952 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7953 }
7954
7955 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7956 Return NULL_TREE if no simplification can be made. */
7957
7958 static tree
7959 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7960 {
7961 if (!validate_arg (arg, REAL_TYPE))
7962 return NULL_TREE;
7963
7964 /* Optimize floor of constant value. */
7965 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7966 {
7967 REAL_VALUE_TYPE x;
7968
7969 x = TREE_REAL_CST (arg);
7970 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7971 {
7972 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7973 REAL_VALUE_TYPE r;
7974
7975 real_floor (&r, TYPE_MODE (type), &x);
7976 return build_real (type, r);
7977 }
7978 }
7979
7980 /* Fold floor (x) where x is nonnegative to trunc (x). */
7981 if (tree_expr_nonnegative_p (arg))
7982 {
7983 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7984 if (truncfn)
7985 return build_call_expr_loc (loc, truncfn, 1, arg);
7986 }
7987
7988 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7989 }
7990
7991 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7992 Return NULL_TREE if no simplification can be made. */
7993
7994 static tree
7995 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7996 {
7997 if (!validate_arg (arg, REAL_TYPE))
7998 return NULL_TREE;
7999
8000 /* Optimize ceil of constant value. */
8001 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8002 {
8003 REAL_VALUE_TYPE x;
8004
8005 x = TREE_REAL_CST (arg);
8006 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8007 {
8008 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8009 REAL_VALUE_TYPE r;
8010
8011 real_ceil (&r, TYPE_MODE (type), &x);
8012 return build_real (type, r);
8013 }
8014 }
8015
8016 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8017 }
8018
8019 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8020 Return NULL_TREE if no simplification can be made. */
8021
8022 static tree
8023 fold_builtin_round (location_t loc, tree fndecl, tree arg)
8024 {
8025 if (!validate_arg (arg, REAL_TYPE))
8026 return NULL_TREE;
8027
8028 /* Optimize round of constant value. */
8029 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8030 {
8031 REAL_VALUE_TYPE x;
8032
8033 x = TREE_REAL_CST (arg);
8034 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8035 {
8036 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8037 REAL_VALUE_TYPE r;
8038
8039 real_round (&r, TYPE_MODE (type), &x);
8040 return build_real (type, r);
8041 }
8042 }
8043
8044 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8045 }
8046
8047 /* Fold function call to builtin lround, lroundf or lroundl (or the
8048 corresponding long long versions) and other rounding functions. ARG
8049 is the argument to the call. Return NULL_TREE if no simplification
8050 can be made. */
8051
8052 static tree
8053 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8054 {
8055 if (!validate_arg (arg, REAL_TYPE))
8056 return NULL_TREE;
8057
8058 /* Optimize lround of constant value. */
8059 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8060 {
8061 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8062
8063 if (real_isfinite (&x))
8064 {
8065 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8066 tree ftype = TREE_TYPE (arg);
8067 REAL_VALUE_TYPE r;
8068 bool fail = false;
8069
8070 switch (DECL_FUNCTION_CODE (fndecl))
8071 {
8072 CASE_FLT_FN (BUILT_IN_IFLOOR):
8073 CASE_FLT_FN (BUILT_IN_LFLOOR):
8074 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8075 real_floor (&r, TYPE_MODE (ftype), &x);
8076 break;
8077
8078 CASE_FLT_FN (BUILT_IN_ICEIL):
8079 CASE_FLT_FN (BUILT_IN_LCEIL):
8080 CASE_FLT_FN (BUILT_IN_LLCEIL):
8081 real_ceil (&r, TYPE_MODE (ftype), &x);
8082 break;
8083
8084 CASE_FLT_FN (BUILT_IN_IROUND):
8085 CASE_FLT_FN (BUILT_IN_LROUND):
8086 CASE_FLT_FN (BUILT_IN_LLROUND):
8087 real_round (&r, TYPE_MODE (ftype), &x);
8088 break;
8089
8090 default:
8091 gcc_unreachable ();
8092 }
8093
8094 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8095 if (!fail)
8096 return wide_int_to_tree (itype, val);
8097 }
8098 }
8099
8100 switch (DECL_FUNCTION_CODE (fndecl))
8101 {
8102 CASE_FLT_FN (BUILT_IN_LFLOOR):
8103 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8104 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8105 if (tree_expr_nonnegative_p (arg))
8106 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8107 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8108 break;
8109 default:;
8110 }
8111
8112 return fold_fixed_mathfn (loc, fndecl, arg);
8113 }
8114
8115 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8116 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8117 the argument to the call. Return NULL_TREE if no simplification can
8118 be made. */
8119
8120 static tree
8121 fold_builtin_bitop (tree fndecl, tree arg)
8122 {
8123 if (!validate_arg (arg, INTEGER_TYPE))
8124 return NULL_TREE;
8125
8126 /* Optimize for constant argument. */
8127 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8128 {
8129 tree type = TREE_TYPE (arg);
8130 int result;
8131
8132 switch (DECL_FUNCTION_CODE (fndecl))
8133 {
8134 CASE_INT_FN (BUILT_IN_FFS):
8135 result = wi::ffs (arg);
8136 break;
8137
8138 CASE_INT_FN (BUILT_IN_CLZ):
8139 if (wi::ne_p (arg, 0))
8140 result = wi::clz (arg);
8141 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8142 result = TYPE_PRECISION (type);
8143 break;
8144
8145 CASE_INT_FN (BUILT_IN_CTZ):
8146 if (wi::ne_p (arg, 0))
8147 result = wi::ctz (arg);
8148 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8149 result = TYPE_PRECISION (type);
8150 break;
8151
8152 CASE_INT_FN (BUILT_IN_CLRSB):
8153 result = wi::clrsb (arg);
8154 break;
8155
8156 CASE_INT_FN (BUILT_IN_POPCOUNT):
8157 result = wi::popcount (arg);
8158 break;
8159
8160 CASE_INT_FN (BUILT_IN_PARITY):
8161 result = wi::parity (arg);
8162 break;
8163
8164 default:
8165 gcc_unreachable ();
8166 }
8167
8168 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8169 }
8170
8171 return NULL_TREE;
8172 }
8173
8174 /* Fold function call to builtin_bswap and the short, long and long long
8175 variants. Return NULL_TREE if no simplification can be made. */
8176 static tree
8177 fold_builtin_bswap (tree fndecl, tree arg)
8178 {
8179 if (! validate_arg (arg, INTEGER_TYPE))
8180 return NULL_TREE;
8181
8182 /* Optimize constant value. */
8183 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8184 {
8185 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8186
8187 switch (DECL_FUNCTION_CODE (fndecl))
8188 {
8189 case BUILT_IN_BSWAP16:
8190 case BUILT_IN_BSWAP32:
8191 case BUILT_IN_BSWAP64:
8192 {
8193 signop sgn = TYPE_SIGN (type);
8194 tree result =
8195 wide_int_to_tree (type,
8196 wide_int::from (arg, TYPE_PRECISION (type),
8197 sgn).bswap ());
8198 return result;
8199 }
8200 default:
8201 gcc_unreachable ();
8202 }
8203 }
8204
8205 return NULL_TREE;
8206 }
8207
8208 /* A subroutine of fold_builtin to fold the various logarithmic
8209 functions. Return NULL_TREE if no simplification can me made.
8210 FUNC is the corresponding MPFR logarithm function. */
8211
8212 static tree
8213 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8214 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8215 {
8216 if (validate_arg (arg, REAL_TYPE))
8217 {
8218 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8219 tree res;
8220 const enum built_in_function fcode = builtin_mathfn_code (arg);
8221
8222 /* Calculate the result when the argument is a constant. */
8223 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8224 return res;
8225
8226 /* Special case, optimize logN(expN(x)) = x. */
8227 if (flag_unsafe_math_optimizations
8228 && ((func == mpfr_log
8229 && (fcode == BUILT_IN_EXP
8230 || fcode == BUILT_IN_EXPF
8231 || fcode == BUILT_IN_EXPL))
8232 || (func == mpfr_log2
8233 && (fcode == BUILT_IN_EXP2
8234 || fcode == BUILT_IN_EXP2F
8235 || fcode == BUILT_IN_EXP2L))
8236 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8237 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8238
8239 /* Optimize logN(func()) for various exponential functions. We
8240 want to determine the value "x" and the power "exponent" in
8241 order to transform logN(x**exponent) into exponent*logN(x). */
8242 if (flag_unsafe_math_optimizations)
8243 {
8244 tree exponent = 0, x = 0;
8245
8246 switch (fcode)
8247 {
8248 CASE_FLT_FN (BUILT_IN_EXP):
8249 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8250 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8251 dconst_e ()));
8252 exponent = CALL_EXPR_ARG (arg, 0);
8253 break;
8254 CASE_FLT_FN (BUILT_IN_EXP2):
8255 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8256 x = build_real (type, dconst2);
8257 exponent = CALL_EXPR_ARG (arg, 0);
8258 break;
8259 CASE_FLT_FN (BUILT_IN_EXP10):
8260 CASE_FLT_FN (BUILT_IN_POW10):
8261 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8262 {
8263 REAL_VALUE_TYPE dconst10;
8264 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8265 x = build_real (type, dconst10);
8266 }
8267 exponent = CALL_EXPR_ARG (arg, 0);
8268 break;
8269 CASE_FLT_FN (BUILT_IN_SQRT):
8270 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8271 x = CALL_EXPR_ARG (arg, 0);
8272 exponent = build_real (type, dconsthalf);
8273 break;
8274 CASE_FLT_FN (BUILT_IN_CBRT):
8275 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8276 x = CALL_EXPR_ARG (arg, 0);
8277 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8278 dconst_third ()));
8279 break;
8280 CASE_FLT_FN (BUILT_IN_POW):
8281 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8282 x = CALL_EXPR_ARG (arg, 0);
8283 exponent = CALL_EXPR_ARG (arg, 1);
8284 break;
8285 default:
8286 break;
8287 }
8288
8289 /* Now perform the optimization. */
8290 if (x && exponent)
8291 {
8292 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8293 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8294 }
8295 }
8296 }
8297
8298 return NULL_TREE;
8299 }
8300
8301 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8302 NULL_TREE if no simplification can be made. */
8303
8304 static tree
8305 fold_builtin_hypot (location_t loc, tree fndecl,
8306 tree arg0, tree arg1, tree type)
8307 {
8308 tree res, narg0, narg1;
8309
8310 if (!validate_arg (arg0, REAL_TYPE)
8311 || !validate_arg (arg1, REAL_TYPE))
8312 return NULL_TREE;
8313
8314 /* Calculate the result when the argument is a constant. */
8315 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8316 return res;
8317
8318 /* If either argument to hypot has a negate or abs, strip that off.
8319 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8320 narg0 = fold_strip_sign_ops (arg0);
8321 narg1 = fold_strip_sign_ops (arg1);
8322 if (narg0 || narg1)
8323 {
8324 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8325 narg1 ? narg1 : arg1);
8326 }
8327
8328 /* If either argument is zero, hypot is fabs of the other. */
8329 if (real_zerop (arg0))
8330 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8331 else if (real_zerop (arg1))
8332 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8333
8334 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8335 if (flag_unsafe_math_optimizations
8336 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8337 {
8338 const REAL_VALUE_TYPE sqrt2_trunc
8339 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8340 return fold_build2_loc (loc, MULT_EXPR, type,
8341 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8342 build_real (type, sqrt2_trunc));
8343 }
8344
8345 return NULL_TREE;
8346 }
8347
8348
8349 /* Fold a builtin function call to pow, powf, or powl. Return
8350 NULL_TREE if no simplification can be made. */
8351 static tree
8352 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8353 {
8354 tree res;
8355
8356 if (!validate_arg (arg0, REAL_TYPE)
8357 || !validate_arg (arg1, REAL_TYPE))
8358 return NULL_TREE;
8359
8360 /* Calculate the result when the argument is a constant. */
8361 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8362 return res;
8363
8364 /* Optimize pow(1.0,y) = 1.0. */
8365 if (real_onep (arg0))
8366 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8367
8368 if (TREE_CODE (arg1) == REAL_CST
8369 && !TREE_OVERFLOW (arg1))
8370 {
8371 REAL_VALUE_TYPE cint;
8372 REAL_VALUE_TYPE c;
8373 HOST_WIDE_INT n;
8374
8375 c = TREE_REAL_CST (arg1);
8376
8377 /* Optimize pow(x,0.0) = 1.0. */
8378 if (REAL_VALUES_EQUAL (c, dconst0))
8379 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8380 arg0);
8381
8382 /* Optimize pow(x,1.0) = x. */
8383 if (REAL_VALUES_EQUAL (c, dconst1))
8384 return arg0;
8385
8386 /* Optimize pow(x,-1.0) = 1.0/x. */
8387 if (REAL_VALUES_EQUAL (c, dconstm1))
8388 return fold_build2_loc (loc, RDIV_EXPR, type,
8389 build_real (type, dconst1), arg0);
8390
8391 /* Optimize pow(x,0.5) = sqrt(x). */
8392 if (flag_unsafe_math_optimizations
8393 && REAL_VALUES_EQUAL (c, dconsthalf))
8394 {
8395 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8396
8397 if (sqrtfn != NULL_TREE)
8398 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8399 }
8400
8401 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8402 if (flag_unsafe_math_optimizations)
8403 {
8404 const REAL_VALUE_TYPE dconstroot
8405 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8406
8407 if (REAL_VALUES_EQUAL (c, dconstroot))
8408 {
8409 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8410 if (cbrtfn != NULL_TREE)
8411 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8412 }
8413 }
8414
8415 /* Check for an integer exponent. */
8416 n = real_to_integer (&c);
8417 real_from_integer (&cint, VOIDmode, n, SIGNED);
8418 if (real_identical (&c, &cint))
8419 {
8420 /* Attempt to evaluate pow at compile-time, unless this should
8421 raise an exception. */
8422 if (TREE_CODE (arg0) == REAL_CST
8423 && !TREE_OVERFLOW (arg0)
8424 && (n > 0
8425 || (!flag_trapping_math && !flag_errno_math)
8426 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8427 {
8428 REAL_VALUE_TYPE x;
8429 bool inexact;
8430
8431 x = TREE_REAL_CST (arg0);
8432 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8433 if (flag_unsafe_math_optimizations || !inexact)
8434 return build_real (type, x);
8435 }
8436
8437 /* Strip sign ops from even integer powers. */
8438 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8439 {
8440 tree narg0 = fold_strip_sign_ops (arg0);
8441 if (narg0)
8442 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8443 }
8444 }
8445 }
8446
8447 if (flag_unsafe_math_optimizations)
8448 {
8449 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8450
8451 /* Optimize pow(expN(x),y) = expN(x*y). */
8452 if (BUILTIN_EXPONENT_P (fcode))
8453 {
8454 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8455 tree arg = CALL_EXPR_ARG (arg0, 0);
8456 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8457 return build_call_expr_loc (loc, expfn, 1, arg);
8458 }
8459
8460 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8461 if (BUILTIN_SQRT_P (fcode))
8462 {
8463 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8464 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8465 build_real (type, dconsthalf));
8466 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8467 }
8468
8469 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8470 if (BUILTIN_CBRT_P (fcode))
8471 {
8472 tree arg = CALL_EXPR_ARG (arg0, 0);
8473 if (tree_expr_nonnegative_p (arg))
8474 {
8475 const REAL_VALUE_TYPE dconstroot
8476 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8477 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8478 build_real (type, dconstroot));
8479 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8480 }
8481 }
8482
8483 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8484 if (fcode == BUILT_IN_POW
8485 || fcode == BUILT_IN_POWF
8486 || fcode == BUILT_IN_POWL)
8487 {
8488 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8489 if (tree_expr_nonnegative_p (arg00))
8490 {
8491 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8492 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8493 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8494 }
8495 }
8496 }
8497
8498 return NULL_TREE;
8499 }
8500
8501 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8502 Return NULL_TREE if no simplification can be made. */
8503 static tree
8504 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8505 tree arg0, tree arg1, tree type)
8506 {
8507 if (!validate_arg (arg0, REAL_TYPE)
8508 || !validate_arg (arg1, INTEGER_TYPE))
8509 return NULL_TREE;
8510
8511 /* Optimize pow(1.0,y) = 1.0. */
8512 if (real_onep (arg0))
8513 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8514
8515 if (tree_fits_shwi_p (arg1))
8516 {
8517 HOST_WIDE_INT c = tree_to_shwi (arg1);
8518
8519 /* Evaluate powi at compile-time. */
8520 if (TREE_CODE (arg0) == REAL_CST
8521 && !TREE_OVERFLOW (arg0))
8522 {
8523 REAL_VALUE_TYPE x;
8524 x = TREE_REAL_CST (arg0);
8525 real_powi (&x, TYPE_MODE (type), &x, c);
8526 return build_real (type, x);
8527 }
8528
8529 /* Optimize pow(x,0) = 1.0. */
8530 if (c == 0)
8531 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8532 arg0);
8533
8534 /* Optimize pow(x,1) = x. */
8535 if (c == 1)
8536 return arg0;
8537
8538 /* Optimize pow(x,-1) = 1.0/x. */
8539 if (c == -1)
8540 return fold_build2_loc (loc, RDIV_EXPR, type,
8541 build_real (type, dconst1), arg0);
8542 }
8543
8544 return NULL_TREE;
8545 }
8546
8547 /* A subroutine of fold_builtin to fold the various exponent
8548 functions. Return NULL_TREE if no simplification can be made.
8549 FUNC is the corresponding MPFR exponent function. */
8550
8551 static tree
8552 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8553 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8554 {
8555 if (validate_arg (arg, REAL_TYPE))
8556 {
8557 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8558 tree res;
8559
8560 /* Calculate the result when the argument is a constant. */
8561 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8562 return res;
8563
8564 /* Optimize expN(logN(x)) = x. */
8565 if (flag_unsafe_math_optimizations)
8566 {
8567 const enum built_in_function fcode = builtin_mathfn_code (arg);
8568
8569 if ((func == mpfr_exp
8570 && (fcode == BUILT_IN_LOG
8571 || fcode == BUILT_IN_LOGF
8572 || fcode == BUILT_IN_LOGL))
8573 || (func == mpfr_exp2
8574 && (fcode == BUILT_IN_LOG2
8575 || fcode == BUILT_IN_LOG2F
8576 || fcode == BUILT_IN_LOG2L))
8577 || (func == mpfr_exp10
8578 && (fcode == BUILT_IN_LOG10
8579 || fcode == BUILT_IN_LOG10F
8580 || fcode == BUILT_IN_LOG10L)))
8581 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8582 }
8583 }
8584
8585 return NULL_TREE;
8586 }
8587
8588 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8589 Return NULL_TREE if no simplification can be made. */
8590
8591 static tree
8592 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8593 {
8594 tree fn, len, lenp1, call, type;
8595
8596 if (!validate_arg (dest, POINTER_TYPE)
8597 || !validate_arg (src, POINTER_TYPE))
8598 return NULL_TREE;
8599
8600 len = c_strlen (src, 1);
8601 if (!len
8602 || TREE_CODE (len) != INTEGER_CST)
8603 return NULL_TREE;
8604
8605 if (optimize_function_for_size_p (cfun)
8606 /* If length is zero it's small enough. */
8607 && !integer_zerop (len))
8608 return NULL_TREE;
8609
8610 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8611 if (!fn)
8612 return NULL_TREE;
8613
8614 lenp1 = size_binop_loc (loc, PLUS_EXPR,
8615 fold_convert_loc (loc, size_type_node, len),
8616 build_int_cst (size_type_node, 1));
8617 /* We use dest twice in building our expression. Save it from
8618 multiple expansions. */
8619 dest = builtin_save_expr (dest);
8620 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8621
8622 type = TREE_TYPE (TREE_TYPE (fndecl));
8623 dest = fold_build_pointer_plus_loc (loc, dest, len);
8624 dest = fold_convert_loc (loc, type, dest);
8625 dest = omit_one_operand_loc (loc, type, dest, call);
8626 return dest;
8627 }
8628
8629 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8630 arguments to the call, and TYPE is its return type.
8631 Return NULL_TREE if no simplification can be made. */
8632
8633 static tree
8634 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8635 {
8636 if (!validate_arg (arg1, POINTER_TYPE)
8637 || !validate_arg (arg2, INTEGER_TYPE)
8638 || !validate_arg (len, INTEGER_TYPE))
8639 return NULL_TREE;
8640 else
8641 {
8642 const char *p1;
8643
8644 if (TREE_CODE (arg2) != INTEGER_CST
8645 || !tree_fits_uhwi_p (len))
8646 return NULL_TREE;
8647
8648 p1 = c_getstr (arg1);
8649 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8650 {
8651 char c;
8652 const char *r;
8653 tree tem;
8654
8655 if (target_char_cast (arg2, &c))
8656 return NULL_TREE;
8657
8658 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8659
8660 if (r == NULL)
8661 return build_int_cst (TREE_TYPE (arg1), 0);
8662
8663 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8664 return fold_convert_loc (loc, type, tem);
8665 }
8666 return NULL_TREE;
8667 }
8668 }
8669
8670 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8671 Return NULL_TREE if no simplification can be made. */
8672
8673 static tree
8674 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8675 {
8676 const char *p1, *p2;
8677
8678 if (!validate_arg (arg1, POINTER_TYPE)
8679 || !validate_arg (arg2, POINTER_TYPE)
8680 || !validate_arg (len, INTEGER_TYPE))
8681 return NULL_TREE;
8682
8683 /* If the LEN parameter is zero, return zero. */
8684 if (integer_zerop (len))
8685 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8686 arg1, arg2);
8687
8688 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8689 if (operand_equal_p (arg1, arg2, 0))
8690 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8691
8692 p1 = c_getstr (arg1);
8693 p2 = c_getstr (arg2);
8694
8695 /* If all arguments are constant, and the value of len is not greater
8696 than the lengths of arg1 and arg2, evaluate at compile-time. */
8697 if (tree_fits_uhwi_p (len) && p1 && p2
8698 && compare_tree_int (len, strlen (p1) + 1) <= 0
8699 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8700 {
8701 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8702
8703 if (r > 0)
8704 return integer_one_node;
8705 else if (r < 0)
8706 return integer_minus_one_node;
8707 else
8708 return integer_zero_node;
8709 }
8710
8711 /* If len parameter is one, return an expression corresponding to
8712 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8713 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8714 {
8715 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8716 tree cst_uchar_ptr_node
8717 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8718
8719 tree ind1
8720 = fold_convert_loc (loc, integer_type_node,
8721 build1 (INDIRECT_REF, cst_uchar_node,
8722 fold_convert_loc (loc,
8723 cst_uchar_ptr_node,
8724 arg1)));
8725 tree ind2
8726 = fold_convert_loc (loc, integer_type_node,
8727 build1 (INDIRECT_REF, cst_uchar_node,
8728 fold_convert_loc (loc,
8729 cst_uchar_ptr_node,
8730 arg2)));
8731 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8732 }
8733
8734 return NULL_TREE;
8735 }
8736
8737 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8738 Return NULL_TREE if no simplification can be made. */
8739
8740 static tree
8741 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8742 {
8743 const char *p1, *p2;
8744
8745 if (!validate_arg (arg1, POINTER_TYPE)
8746 || !validate_arg (arg2, POINTER_TYPE))
8747 return NULL_TREE;
8748
8749 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8750 if (operand_equal_p (arg1, arg2, 0))
8751 return integer_zero_node;
8752
8753 p1 = c_getstr (arg1);
8754 p2 = c_getstr (arg2);
8755
8756 if (p1 && p2)
8757 {
8758 const int i = strcmp (p1, p2);
8759 if (i < 0)
8760 return integer_minus_one_node;
8761 else if (i > 0)
8762 return integer_one_node;
8763 else
8764 return integer_zero_node;
8765 }
8766
8767 /* If the second arg is "", return *(const unsigned char*)arg1. */
8768 if (p2 && *p2 == '\0')
8769 {
8770 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8771 tree cst_uchar_ptr_node
8772 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8773
8774 return fold_convert_loc (loc, integer_type_node,
8775 build1 (INDIRECT_REF, cst_uchar_node,
8776 fold_convert_loc (loc,
8777 cst_uchar_ptr_node,
8778 arg1)));
8779 }
8780
8781 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8782 if (p1 && *p1 == '\0')
8783 {
8784 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8785 tree cst_uchar_ptr_node
8786 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8787
8788 tree temp
8789 = fold_convert_loc (loc, integer_type_node,
8790 build1 (INDIRECT_REF, cst_uchar_node,
8791 fold_convert_loc (loc,
8792 cst_uchar_ptr_node,
8793 arg2)));
8794 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8795 }
8796
8797 return NULL_TREE;
8798 }
8799
8800 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8801 Return NULL_TREE if no simplification can be made. */
8802
8803 static tree
8804 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8805 {
8806 const char *p1, *p2;
8807
8808 if (!validate_arg (arg1, POINTER_TYPE)
8809 || !validate_arg (arg2, POINTER_TYPE)
8810 || !validate_arg (len, INTEGER_TYPE))
8811 return NULL_TREE;
8812
8813 /* If the LEN parameter is zero, return zero. */
8814 if (integer_zerop (len))
8815 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8816 arg1, arg2);
8817
8818 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8819 if (operand_equal_p (arg1, arg2, 0))
8820 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8821
8822 p1 = c_getstr (arg1);
8823 p2 = c_getstr (arg2);
8824
8825 if (tree_fits_uhwi_p (len) && p1 && p2)
8826 {
8827 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8828 if (i > 0)
8829 return integer_one_node;
8830 else if (i < 0)
8831 return integer_minus_one_node;
8832 else
8833 return integer_zero_node;
8834 }
8835
8836 /* If the second arg is "", and the length is greater than zero,
8837 return *(const unsigned char*)arg1. */
8838 if (p2 && *p2 == '\0'
8839 && TREE_CODE (len) == INTEGER_CST
8840 && tree_int_cst_sgn (len) == 1)
8841 {
8842 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8843 tree cst_uchar_ptr_node
8844 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8845
8846 return fold_convert_loc (loc, integer_type_node,
8847 build1 (INDIRECT_REF, cst_uchar_node,
8848 fold_convert_loc (loc,
8849 cst_uchar_ptr_node,
8850 arg1)));
8851 }
8852
8853 /* If the first arg is "", and the length is greater than zero,
8854 return -*(const unsigned char*)arg2. */
8855 if (p1 && *p1 == '\0'
8856 && TREE_CODE (len) == INTEGER_CST
8857 && tree_int_cst_sgn (len) == 1)
8858 {
8859 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8860 tree cst_uchar_ptr_node
8861 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8862
8863 tree temp = fold_convert_loc (loc, integer_type_node,
8864 build1 (INDIRECT_REF, cst_uchar_node,
8865 fold_convert_loc (loc,
8866 cst_uchar_ptr_node,
8867 arg2)));
8868 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8869 }
8870
8871 /* If len parameter is one, return an expression corresponding to
8872 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8873 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8874 {
8875 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8876 tree cst_uchar_ptr_node
8877 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8878
8879 tree ind1 = fold_convert_loc (loc, integer_type_node,
8880 build1 (INDIRECT_REF, cst_uchar_node,
8881 fold_convert_loc (loc,
8882 cst_uchar_ptr_node,
8883 arg1)));
8884 tree ind2 = fold_convert_loc (loc, integer_type_node,
8885 build1 (INDIRECT_REF, cst_uchar_node,
8886 fold_convert_loc (loc,
8887 cst_uchar_ptr_node,
8888 arg2)));
8889 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8890 }
8891
8892 return NULL_TREE;
8893 }
8894
8895 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8896 ARG. Return NULL_TREE if no simplification can be made. */
8897
8898 static tree
8899 fold_builtin_signbit (location_t loc, tree arg, tree type)
8900 {
8901 if (!validate_arg (arg, REAL_TYPE))
8902 return NULL_TREE;
8903
8904 /* If ARG is a compile-time constant, determine the result. */
8905 if (TREE_CODE (arg) == REAL_CST
8906 && !TREE_OVERFLOW (arg))
8907 {
8908 REAL_VALUE_TYPE c;
8909
8910 c = TREE_REAL_CST (arg);
8911 return (REAL_VALUE_NEGATIVE (c)
8912 ? build_one_cst (type)
8913 : build_zero_cst (type));
8914 }
8915
8916 /* If ARG is non-negative, the result is always zero. */
8917 if (tree_expr_nonnegative_p (arg))
8918 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8919
8920 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8921 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8922 return fold_convert (type,
8923 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
8924 build_real (TREE_TYPE (arg), dconst0)));
8925
8926 return NULL_TREE;
8927 }
8928
8929 /* Fold function call to builtin copysign, copysignf or copysignl with
8930 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8931 be made. */
8932
8933 static tree
8934 fold_builtin_copysign (location_t loc, tree fndecl,
8935 tree arg1, tree arg2, tree type)
8936 {
8937 tree tem;
8938
8939 if (!validate_arg (arg1, REAL_TYPE)
8940 || !validate_arg (arg2, REAL_TYPE))
8941 return NULL_TREE;
8942
8943 /* copysign(X,X) is X. */
8944 if (operand_equal_p (arg1, arg2, 0))
8945 return fold_convert_loc (loc, type, arg1);
8946
8947 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8948 if (TREE_CODE (arg1) == REAL_CST
8949 && TREE_CODE (arg2) == REAL_CST
8950 && !TREE_OVERFLOW (arg1)
8951 && !TREE_OVERFLOW (arg2))
8952 {
8953 REAL_VALUE_TYPE c1, c2;
8954
8955 c1 = TREE_REAL_CST (arg1);
8956 c2 = TREE_REAL_CST (arg2);
8957 /* c1.sign := c2.sign. */
8958 real_copysign (&c1, &c2);
8959 return build_real (type, c1);
8960 }
8961
8962 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8963 Remember to evaluate Y for side-effects. */
8964 if (tree_expr_nonnegative_p (arg2))
8965 return omit_one_operand_loc (loc, type,
8966 fold_build1_loc (loc, ABS_EXPR, type, arg1),
8967 arg2);
8968
8969 /* Strip sign changing operations for the first argument. */
8970 tem = fold_strip_sign_ops (arg1);
8971 if (tem)
8972 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
8973
8974 return NULL_TREE;
8975 }
8976
8977 /* Fold a call to builtin isascii with argument ARG. */
8978
8979 static tree
8980 fold_builtin_isascii (location_t loc, tree arg)
8981 {
8982 if (!validate_arg (arg, INTEGER_TYPE))
8983 return NULL_TREE;
8984 else
8985 {
8986 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8987 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8988 build_int_cst (integer_type_node,
8989 ~ (unsigned HOST_WIDE_INT) 0x7f));
8990 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8991 arg, integer_zero_node);
8992 }
8993 }
8994
8995 /* Fold a call to builtin toascii with argument ARG. */
8996
8997 static tree
8998 fold_builtin_toascii (location_t loc, tree arg)
8999 {
9000 if (!validate_arg (arg, INTEGER_TYPE))
9001 return NULL_TREE;
9002
9003 /* Transform toascii(c) -> (c & 0x7f). */
9004 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9005 build_int_cst (integer_type_node, 0x7f));
9006 }
9007
9008 /* Fold a call to builtin isdigit with argument ARG. */
9009
9010 static tree
9011 fold_builtin_isdigit (location_t loc, tree arg)
9012 {
9013 if (!validate_arg (arg, INTEGER_TYPE))
9014 return NULL_TREE;
9015 else
9016 {
9017 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9018 /* According to the C standard, isdigit is unaffected by locale.
9019 However, it definitely is affected by the target character set. */
9020 unsigned HOST_WIDE_INT target_digit0
9021 = lang_hooks.to_target_charset ('0');
9022
9023 if (target_digit0 == 0)
9024 return NULL_TREE;
9025
9026 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9027 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9028 build_int_cst (unsigned_type_node, target_digit0));
9029 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9030 build_int_cst (unsigned_type_node, 9));
9031 }
9032 }
9033
9034 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9035
9036 static tree
9037 fold_builtin_fabs (location_t loc, tree arg, tree type)
9038 {
9039 if (!validate_arg (arg, REAL_TYPE))
9040 return NULL_TREE;
9041
9042 arg = fold_convert_loc (loc, type, arg);
9043 if (TREE_CODE (arg) == REAL_CST)
9044 return fold_abs_const (arg, type);
9045 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9046 }
9047
9048 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9049
9050 static tree
9051 fold_builtin_abs (location_t loc, tree arg, tree type)
9052 {
9053 if (!validate_arg (arg, INTEGER_TYPE))
9054 return NULL_TREE;
9055
9056 arg = fold_convert_loc (loc, type, arg);
9057 if (TREE_CODE (arg) == INTEGER_CST)
9058 return fold_abs_const (arg, type);
9059 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9060 }
9061
9062 /* Fold a fma operation with arguments ARG[012]. */
9063
9064 tree
9065 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9066 tree type, tree arg0, tree arg1, tree arg2)
9067 {
9068 if (TREE_CODE (arg0) == REAL_CST
9069 && TREE_CODE (arg1) == REAL_CST
9070 && TREE_CODE (arg2) == REAL_CST)
9071 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9072
9073 return NULL_TREE;
9074 }
9075
9076 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9077
9078 static tree
9079 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9080 {
9081 if (validate_arg (arg0, REAL_TYPE)
9082 && validate_arg (arg1, REAL_TYPE)
9083 && validate_arg (arg2, REAL_TYPE))
9084 {
9085 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9086 if (tem)
9087 return tem;
9088
9089 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9090 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9091 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9092 }
9093 return NULL_TREE;
9094 }
9095
9096 /* Fold a call to builtin fmin or fmax. */
9097
9098 static tree
9099 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9100 tree type, bool max)
9101 {
9102 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9103 {
9104 /* Calculate the result when the argument is a constant. */
9105 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9106
9107 if (res)
9108 return res;
9109
9110 /* If either argument is NaN, return the other one. Avoid the
9111 transformation if we get (and honor) a signalling NaN. Using
9112 omit_one_operand() ensures we create a non-lvalue. */
9113 if (TREE_CODE (arg0) == REAL_CST
9114 && real_isnan (&TREE_REAL_CST (arg0))
9115 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9116 || ! TREE_REAL_CST (arg0).signalling))
9117 return omit_one_operand_loc (loc, type, arg1, arg0);
9118 if (TREE_CODE (arg1) == REAL_CST
9119 && real_isnan (&TREE_REAL_CST (arg1))
9120 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9121 || ! TREE_REAL_CST (arg1).signalling))
9122 return omit_one_operand_loc (loc, type, arg0, arg1);
9123
9124 /* Transform fmin/fmax(x,x) -> x. */
9125 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9126 return omit_one_operand_loc (loc, type, arg0, arg1);
9127
9128 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9129 functions to return the numeric arg if the other one is NaN.
9130 These tree codes don't honor that, so only transform if
9131 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9132 handled, so we don't have to worry about it either. */
9133 if (flag_finite_math_only)
9134 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9135 fold_convert_loc (loc, type, arg0),
9136 fold_convert_loc (loc, type, arg1));
9137 }
9138 return NULL_TREE;
9139 }
9140
9141 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9142
9143 static tree
9144 fold_builtin_carg (location_t loc, tree arg, tree type)
9145 {
9146 if (validate_arg (arg, COMPLEX_TYPE)
9147 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9148 {
9149 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9150
9151 if (atan2_fn)
9152 {
9153 tree new_arg = builtin_save_expr (arg);
9154 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9155 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9156 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9157 }
9158 }
9159
9160 return NULL_TREE;
9161 }
9162
9163 /* Fold a call to builtin logb/ilogb. */
9164
9165 static tree
9166 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9167 {
9168 if (! validate_arg (arg, REAL_TYPE))
9169 return NULL_TREE;
9170
9171 STRIP_NOPS (arg);
9172
9173 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9174 {
9175 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9176
9177 switch (value->cl)
9178 {
9179 case rvc_nan:
9180 case rvc_inf:
9181 /* If arg is Inf or NaN and we're logb, return it. */
9182 if (TREE_CODE (rettype) == REAL_TYPE)
9183 {
9184 /* For logb(-Inf) we have to return +Inf. */
9185 if (real_isinf (value) && real_isneg (value))
9186 {
9187 REAL_VALUE_TYPE tem;
9188 real_inf (&tem);
9189 return build_real (rettype, tem);
9190 }
9191 return fold_convert_loc (loc, rettype, arg);
9192 }
9193 /* Fall through... */
9194 case rvc_zero:
9195 /* Zero may set errno and/or raise an exception for logb, also
9196 for ilogb we don't know FP_ILOGB0. */
9197 return NULL_TREE;
9198 case rvc_normal:
9199 /* For normal numbers, proceed iff radix == 2. In GCC,
9200 normalized significands are in the range [0.5, 1.0). We
9201 want the exponent as if they were [1.0, 2.0) so get the
9202 exponent and subtract 1. */
9203 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9204 return fold_convert_loc (loc, rettype,
9205 build_int_cst (integer_type_node,
9206 REAL_EXP (value)-1));
9207 break;
9208 }
9209 }
9210
9211 return NULL_TREE;
9212 }
9213
9214 /* Fold a call to builtin significand, if radix == 2. */
9215
9216 static tree
9217 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9218 {
9219 if (! validate_arg (arg, REAL_TYPE))
9220 return NULL_TREE;
9221
9222 STRIP_NOPS (arg);
9223
9224 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9225 {
9226 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9227
9228 switch (value->cl)
9229 {
9230 case rvc_zero:
9231 case rvc_nan:
9232 case rvc_inf:
9233 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9234 return fold_convert_loc (loc, rettype, arg);
9235 case rvc_normal:
9236 /* For normal numbers, proceed iff radix == 2. */
9237 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9238 {
9239 REAL_VALUE_TYPE result = *value;
9240 /* In GCC, normalized significands are in the range [0.5,
9241 1.0). We want them to be [1.0, 2.0) so set the
9242 exponent to 1. */
9243 SET_REAL_EXP (&result, 1);
9244 return build_real (rettype, result);
9245 }
9246 break;
9247 }
9248 }
9249
9250 return NULL_TREE;
9251 }
9252
9253 /* Fold a call to builtin frexp, we can assume the base is 2. */
9254
9255 static tree
9256 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9257 {
9258 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9259 return NULL_TREE;
9260
9261 STRIP_NOPS (arg0);
9262
9263 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9264 return NULL_TREE;
9265
9266 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9267
9268 /* Proceed if a valid pointer type was passed in. */
9269 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9270 {
9271 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9272 tree frac, exp;
9273
9274 switch (value->cl)
9275 {
9276 case rvc_zero:
9277 /* For +-0, return (*exp = 0, +-0). */
9278 exp = integer_zero_node;
9279 frac = arg0;
9280 break;
9281 case rvc_nan:
9282 case rvc_inf:
9283 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9284 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9285 case rvc_normal:
9286 {
9287 /* Since the frexp function always expects base 2, and in
9288 GCC normalized significands are already in the range
9289 [0.5, 1.0), we have exactly what frexp wants. */
9290 REAL_VALUE_TYPE frac_rvt = *value;
9291 SET_REAL_EXP (&frac_rvt, 0);
9292 frac = build_real (rettype, frac_rvt);
9293 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9294 }
9295 break;
9296 default:
9297 gcc_unreachable ();
9298 }
9299
9300 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9301 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9302 TREE_SIDE_EFFECTS (arg1) = 1;
9303 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9304 }
9305
9306 return NULL_TREE;
9307 }
9308
9309 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9310 then we can assume the base is two. If it's false, then we have to
9311 check the mode of the TYPE parameter in certain cases. */
9312
9313 static tree
9314 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9315 tree type, bool ldexp)
9316 {
9317 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9318 {
9319 STRIP_NOPS (arg0);
9320 STRIP_NOPS (arg1);
9321
9322 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9323 if (real_zerop (arg0) || integer_zerop (arg1)
9324 || (TREE_CODE (arg0) == REAL_CST
9325 && !real_isfinite (&TREE_REAL_CST (arg0))))
9326 return omit_one_operand_loc (loc, type, arg0, arg1);
9327
9328 /* If both arguments are constant, then try to evaluate it. */
9329 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9330 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9331 && tree_fits_shwi_p (arg1))
9332 {
9333 /* Bound the maximum adjustment to twice the range of the
9334 mode's valid exponents. Use abs to ensure the range is
9335 positive as a sanity check. */
9336 const long max_exp_adj = 2 *
9337 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9338 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9339
9340 /* Get the user-requested adjustment. */
9341 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9342
9343 /* The requested adjustment must be inside this range. This
9344 is a preliminary cap to avoid things like overflow, we
9345 may still fail to compute the result for other reasons. */
9346 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9347 {
9348 REAL_VALUE_TYPE initial_result;
9349
9350 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9351
9352 /* Ensure we didn't overflow. */
9353 if (! real_isinf (&initial_result))
9354 {
9355 const REAL_VALUE_TYPE trunc_result
9356 = real_value_truncate (TYPE_MODE (type), initial_result);
9357
9358 /* Only proceed if the target mode can hold the
9359 resulting value. */
9360 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9361 return build_real (type, trunc_result);
9362 }
9363 }
9364 }
9365 }
9366
9367 return NULL_TREE;
9368 }
9369
9370 /* Fold a call to builtin modf. */
9371
9372 static tree
9373 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9374 {
9375 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9376 return NULL_TREE;
9377
9378 STRIP_NOPS (arg0);
9379
9380 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9381 return NULL_TREE;
9382
9383 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9384
9385 /* Proceed if a valid pointer type was passed in. */
9386 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9387 {
9388 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9389 REAL_VALUE_TYPE trunc, frac;
9390
9391 switch (value->cl)
9392 {
9393 case rvc_nan:
9394 case rvc_zero:
9395 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9396 trunc = frac = *value;
9397 break;
9398 case rvc_inf:
9399 /* For +-Inf, return (*arg1 = arg0, +-0). */
9400 frac = dconst0;
9401 frac.sign = value->sign;
9402 trunc = *value;
9403 break;
9404 case rvc_normal:
9405 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9406 real_trunc (&trunc, VOIDmode, value);
9407 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9408 /* If the original number was negative and already
9409 integral, then the fractional part is -0.0. */
9410 if (value->sign && frac.cl == rvc_zero)
9411 frac.sign = value->sign;
9412 break;
9413 }
9414
9415 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9416 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9417 build_real (rettype, trunc));
9418 TREE_SIDE_EFFECTS (arg1) = 1;
9419 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9420 build_real (rettype, frac));
9421 }
9422
9423 return NULL_TREE;
9424 }
9425
9426 /* Given a location LOC, an interclass builtin function decl FNDECL
9427 and its single argument ARG, return an folded expression computing
9428 the same, or NULL_TREE if we either couldn't or didn't want to fold
9429 (the latter happen if there's an RTL instruction available). */
9430
9431 static tree
9432 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9433 {
9434 machine_mode mode;
9435
9436 if (!validate_arg (arg, REAL_TYPE))
9437 return NULL_TREE;
9438
9439 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9440 return NULL_TREE;
9441
9442 mode = TYPE_MODE (TREE_TYPE (arg));
9443
9444 /* If there is no optab, try generic code. */
9445 switch (DECL_FUNCTION_CODE (fndecl))
9446 {
9447 tree result;
9448
9449 CASE_FLT_FN (BUILT_IN_ISINF):
9450 {
9451 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9452 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9453 tree const type = TREE_TYPE (arg);
9454 REAL_VALUE_TYPE r;
9455 char buf[128];
9456
9457 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9458 real_from_string (&r, buf);
9459 result = build_call_expr (isgr_fn, 2,
9460 fold_build1_loc (loc, ABS_EXPR, type, arg),
9461 build_real (type, r));
9462 return result;
9463 }
9464 CASE_FLT_FN (BUILT_IN_FINITE):
9465 case BUILT_IN_ISFINITE:
9466 {
9467 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9468 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9469 tree const type = TREE_TYPE (arg);
9470 REAL_VALUE_TYPE r;
9471 char buf[128];
9472
9473 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9474 real_from_string (&r, buf);
9475 result = build_call_expr (isle_fn, 2,
9476 fold_build1_loc (loc, ABS_EXPR, type, arg),
9477 build_real (type, r));
9478 /*result = fold_build2_loc (loc, UNGT_EXPR,
9479 TREE_TYPE (TREE_TYPE (fndecl)),
9480 fold_build1_loc (loc, ABS_EXPR, type, arg),
9481 build_real (type, r));
9482 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9483 TREE_TYPE (TREE_TYPE (fndecl)),
9484 result);*/
9485 return result;
9486 }
9487 case BUILT_IN_ISNORMAL:
9488 {
9489 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9490 islessequal(fabs(x),DBL_MAX). */
9491 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9492 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9493 tree const type = TREE_TYPE (arg);
9494 REAL_VALUE_TYPE rmax, rmin;
9495 char buf[128];
9496
9497 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9498 real_from_string (&rmax, buf);
9499 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9500 real_from_string (&rmin, buf);
9501 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9502 result = build_call_expr (isle_fn, 2, arg,
9503 build_real (type, rmax));
9504 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9505 build_call_expr (isge_fn, 2, arg,
9506 build_real (type, rmin)));
9507 return result;
9508 }
9509 default:
9510 break;
9511 }
9512
9513 return NULL_TREE;
9514 }
9515
9516 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9517 ARG is the argument for the call. */
9518
9519 static tree
9520 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9521 {
9522 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9523 REAL_VALUE_TYPE r;
9524
9525 if (!validate_arg (arg, REAL_TYPE))
9526 return NULL_TREE;
9527
9528 switch (builtin_index)
9529 {
9530 case BUILT_IN_ISINF:
9531 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9532 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9533
9534 if (TREE_CODE (arg) == REAL_CST)
9535 {
9536 r = TREE_REAL_CST (arg);
9537 if (real_isinf (&r))
9538 return real_compare (GT_EXPR, &r, &dconst0)
9539 ? integer_one_node : integer_minus_one_node;
9540 else
9541 return integer_zero_node;
9542 }
9543
9544 return NULL_TREE;
9545
9546 case BUILT_IN_ISINF_SIGN:
9547 {
9548 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9549 /* In a boolean context, GCC will fold the inner COND_EXPR to
9550 1. So e.g. "if (isinf_sign(x))" would be folded to just
9551 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9552 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9553 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9554 tree tmp = NULL_TREE;
9555
9556 arg = builtin_save_expr (arg);
9557
9558 if (signbit_fn && isinf_fn)
9559 {
9560 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9561 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9562
9563 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9564 signbit_call, integer_zero_node);
9565 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9566 isinf_call, integer_zero_node);
9567
9568 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9569 integer_minus_one_node, integer_one_node);
9570 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9571 isinf_call, tmp,
9572 integer_zero_node);
9573 }
9574
9575 return tmp;
9576 }
9577
9578 case BUILT_IN_ISFINITE:
9579 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9580 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9581 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9582
9583 if (TREE_CODE (arg) == REAL_CST)
9584 {
9585 r = TREE_REAL_CST (arg);
9586 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9587 }
9588
9589 return NULL_TREE;
9590
9591 case BUILT_IN_ISNAN:
9592 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9593 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9594
9595 if (TREE_CODE (arg) == REAL_CST)
9596 {
9597 r = TREE_REAL_CST (arg);
9598 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9599 }
9600
9601 arg = builtin_save_expr (arg);
9602 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9603
9604 default:
9605 gcc_unreachable ();
9606 }
9607 }
9608
9609 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9610 This builtin will generate code to return the appropriate floating
9611 point classification depending on the value of the floating point
9612 number passed in. The possible return values must be supplied as
9613 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9614 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9615 one floating point argument which is "type generic". */
9616
9617 static tree
9618 fold_builtin_fpclassify (location_t loc, tree exp)
9619 {
9620 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9621 arg, type, res, tmp;
9622 machine_mode mode;
9623 REAL_VALUE_TYPE r;
9624 char buf[128];
9625
9626 /* Verify the required arguments in the original call. */
9627 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9628 INTEGER_TYPE, INTEGER_TYPE,
9629 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9630 return NULL_TREE;
9631
9632 fp_nan = CALL_EXPR_ARG (exp, 0);
9633 fp_infinite = CALL_EXPR_ARG (exp, 1);
9634 fp_normal = CALL_EXPR_ARG (exp, 2);
9635 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9636 fp_zero = CALL_EXPR_ARG (exp, 4);
9637 arg = CALL_EXPR_ARG (exp, 5);
9638 type = TREE_TYPE (arg);
9639 mode = TYPE_MODE (type);
9640 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9641
9642 /* fpclassify(x) ->
9643 isnan(x) ? FP_NAN :
9644 (fabs(x) == Inf ? FP_INFINITE :
9645 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9646 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9647
9648 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9649 build_real (type, dconst0));
9650 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9651 tmp, fp_zero, fp_subnormal);
9652
9653 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9654 real_from_string (&r, buf);
9655 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9656 arg, build_real (type, r));
9657 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9658
9659 if (HONOR_INFINITIES (mode))
9660 {
9661 real_inf (&r);
9662 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9663 build_real (type, r));
9664 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9665 fp_infinite, res);
9666 }
9667
9668 if (HONOR_NANS (mode))
9669 {
9670 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9671 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9672 }
9673
9674 return res;
9675 }
9676
9677 /* Fold a call to an unordered comparison function such as
9678 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9679 being called and ARG0 and ARG1 are the arguments for the call.
9680 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9681 the opposite of the desired result. UNORDERED_CODE is used
9682 for modes that can hold NaNs and ORDERED_CODE is used for
9683 the rest. */
9684
9685 static tree
9686 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9687 enum tree_code unordered_code,
9688 enum tree_code ordered_code)
9689 {
9690 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9691 enum tree_code code;
9692 tree type0, type1;
9693 enum tree_code code0, code1;
9694 tree cmp_type = NULL_TREE;
9695
9696 type0 = TREE_TYPE (arg0);
9697 type1 = TREE_TYPE (arg1);
9698
9699 code0 = TREE_CODE (type0);
9700 code1 = TREE_CODE (type1);
9701
9702 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9703 /* Choose the wider of two real types. */
9704 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9705 ? type0 : type1;
9706 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9707 cmp_type = type0;
9708 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9709 cmp_type = type1;
9710
9711 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9712 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9713
9714 if (unordered_code == UNORDERED_EXPR)
9715 {
9716 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9717 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9718 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9719 }
9720
9721 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9722 : ordered_code;
9723 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9724 fold_build2_loc (loc, code, type, arg0, arg1));
9725 }
9726
9727 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9728 arithmetics if it can never overflow, or into internal functions that
9729 return both result of arithmetics and overflowed boolean flag in
9730 a complex integer result, or some other check for overflow. */
9731
9732 static tree
9733 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9734 tree arg0, tree arg1, tree arg2)
9735 {
9736 enum internal_fn ifn = IFN_LAST;
9737 tree type = TREE_TYPE (TREE_TYPE (arg2));
9738 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9739 switch (fcode)
9740 {
9741 case BUILT_IN_ADD_OVERFLOW:
9742 case BUILT_IN_SADD_OVERFLOW:
9743 case BUILT_IN_SADDL_OVERFLOW:
9744 case BUILT_IN_SADDLL_OVERFLOW:
9745 case BUILT_IN_UADD_OVERFLOW:
9746 case BUILT_IN_UADDL_OVERFLOW:
9747 case BUILT_IN_UADDLL_OVERFLOW:
9748 ifn = IFN_ADD_OVERFLOW;
9749 break;
9750 case BUILT_IN_SUB_OVERFLOW:
9751 case BUILT_IN_SSUB_OVERFLOW:
9752 case BUILT_IN_SSUBL_OVERFLOW:
9753 case BUILT_IN_SSUBLL_OVERFLOW:
9754 case BUILT_IN_USUB_OVERFLOW:
9755 case BUILT_IN_USUBL_OVERFLOW:
9756 case BUILT_IN_USUBLL_OVERFLOW:
9757 ifn = IFN_SUB_OVERFLOW;
9758 break;
9759 case BUILT_IN_MUL_OVERFLOW:
9760 case BUILT_IN_SMUL_OVERFLOW:
9761 case BUILT_IN_SMULL_OVERFLOW:
9762 case BUILT_IN_SMULLL_OVERFLOW:
9763 case BUILT_IN_UMUL_OVERFLOW:
9764 case BUILT_IN_UMULL_OVERFLOW:
9765 case BUILT_IN_UMULLL_OVERFLOW:
9766 ifn = IFN_MUL_OVERFLOW;
9767 break;
9768 default:
9769 gcc_unreachable ();
9770 }
9771 tree ctype = build_complex_type (type);
9772 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
9773 2, arg0, arg1);
9774 tree tgt = save_expr (call);
9775 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9776 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9777 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9778 tree store
9779 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9780 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9781 }
9782
9783 /* Fold a call to built-in function FNDECL with 0 arguments.
9784 IGNORE is true if the result of the function call is ignored. This
9785 function returns NULL_TREE if no simplification was possible. */
9786
9787 static tree
9788 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9789 {
9790 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9791 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9792 switch (fcode)
9793 {
9794 CASE_FLT_FN (BUILT_IN_INF):
9795 case BUILT_IN_INFD32:
9796 case BUILT_IN_INFD64:
9797 case BUILT_IN_INFD128:
9798 return fold_builtin_inf (loc, type, true);
9799
9800 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9801 return fold_builtin_inf (loc, type, false);
9802
9803 case BUILT_IN_CLASSIFY_TYPE:
9804 return fold_builtin_classify_type (NULL_TREE);
9805
9806 case BUILT_IN_UNREACHABLE:
9807 if (flag_sanitize & SANITIZE_UNREACHABLE
9808 && (current_function_decl == NULL
9809 || !lookup_attribute ("no_sanitize_undefined",
9810 DECL_ATTRIBUTES (current_function_decl))))
9811 return ubsan_instrument_unreachable (loc);
9812 break;
9813
9814 default:
9815 break;
9816 }
9817 return NULL_TREE;
9818 }
9819
9820 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9821 IGNORE is true if the result of the function call is ignored. This
9822 function returns NULL_TREE if no simplification was possible. */
9823
9824 static tree
9825 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9826 {
9827 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9828 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9829 switch (fcode)
9830 {
9831 case BUILT_IN_CONSTANT_P:
9832 {
9833 tree val = fold_builtin_constant_p (arg0);
9834
9835 /* Gimplification will pull the CALL_EXPR for the builtin out of
9836 an if condition. When not optimizing, we'll not CSE it back.
9837 To avoid link error types of regressions, return false now. */
9838 if (!val && !optimize)
9839 val = integer_zero_node;
9840
9841 return val;
9842 }
9843
9844 case BUILT_IN_CLASSIFY_TYPE:
9845 return fold_builtin_classify_type (arg0);
9846
9847 case BUILT_IN_STRLEN:
9848 return fold_builtin_strlen (loc, type, arg0);
9849
9850 CASE_FLT_FN (BUILT_IN_FABS):
9851 case BUILT_IN_FABSD32:
9852 case BUILT_IN_FABSD64:
9853 case BUILT_IN_FABSD128:
9854 return fold_builtin_fabs (loc, arg0, type);
9855
9856 case BUILT_IN_ABS:
9857 case BUILT_IN_LABS:
9858 case BUILT_IN_LLABS:
9859 case BUILT_IN_IMAXABS:
9860 return fold_builtin_abs (loc, arg0, type);
9861
9862 CASE_FLT_FN (BUILT_IN_CONJ):
9863 if (validate_arg (arg0, COMPLEX_TYPE)
9864 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9865 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9866 break;
9867
9868 CASE_FLT_FN (BUILT_IN_CREAL):
9869 if (validate_arg (arg0, COMPLEX_TYPE)
9870 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9871 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9872 break;
9873
9874 CASE_FLT_FN (BUILT_IN_CIMAG):
9875 if (validate_arg (arg0, COMPLEX_TYPE)
9876 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9877 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9878 break;
9879
9880 CASE_FLT_FN (BUILT_IN_CCOS):
9881 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9882
9883 CASE_FLT_FN (BUILT_IN_CCOSH):
9884 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9885
9886 CASE_FLT_FN (BUILT_IN_CPROJ):
9887 return fold_builtin_cproj (loc, arg0, type);
9888
9889 CASE_FLT_FN (BUILT_IN_CSIN):
9890 if (validate_arg (arg0, COMPLEX_TYPE)
9891 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9892 return do_mpc_arg1 (arg0, type, mpc_sin);
9893 break;
9894
9895 CASE_FLT_FN (BUILT_IN_CSINH):
9896 if (validate_arg (arg0, COMPLEX_TYPE)
9897 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9898 return do_mpc_arg1 (arg0, type, mpc_sinh);
9899 break;
9900
9901 CASE_FLT_FN (BUILT_IN_CTAN):
9902 if (validate_arg (arg0, COMPLEX_TYPE)
9903 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9904 return do_mpc_arg1 (arg0, type, mpc_tan);
9905 break;
9906
9907 CASE_FLT_FN (BUILT_IN_CTANH):
9908 if (validate_arg (arg0, COMPLEX_TYPE)
9909 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9910 return do_mpc_arg1 (arg0, type, mpc_tanh);
9911 break;
9912
9913 CASE_FLT_FN (BUILT_IN_CLOG):
9914 if (validate_arg (arg0, COMPLEX_TYPE)
9915 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9916 return do_mpc_arg1 (arg0, type, mpc_log);
9917 break;
9918
9919 CASE_FLT_FN (BUILT_IN_CSQRT):
9920 if (validate_arg (arg0, COMPLEX_TYPE)
9921 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9922 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9923 break;
9924
9925 CASE_FLT_FN (BUILT_IN_CASIN):
9926 if (validate_arg (arg0, COMPLEX_TYPE)
9927 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9928 return do_mpc_arg1 (arg0, type, mpc_asin);
9929 break;
9930
9931 CASE_FLT_FN (BUILT_IN_CACOS):
9932 if (validate_arg (arg0, COMPLEX_TYPE)
9933 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9934 return do_mpc_arg1 (arg0, type, mpc_acos);
9935 break;
9936
9937 CASE_FLT_FN (BUILT_IN_CATAN):
9938 if (validate_arg (arg0, COMPLEX_TYPE)
9939 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9940 return do_mpc_arg1 (arg0, type, mpc_atan);
9941 break;
9942
9943 CASE_FLT_FN (BUILT_IN_CASINH):
9944 if (validate_arg (arg0, COMPLEX_TYPE)
9945 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9946 return do_mpc_arg1 (arg0, type, mpc_asinh);
9947 break;
9948
9949 CASE_FLT_FN (BUILT_IN_CACOSH):
9950 if (validate_arg (arg0, COMPLEX_TYPE)
9951 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9952 return do_mpc_arg1 (arg0, type, mpc_acosh);
9953 break;
9954
9955 CASE_FLT_FN (BUILT_IN_CATANH):
9956 if (validate_arg (arg0, COMPLEX_TYPE)
9957 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9958 return do_mpc_arg1 (arg0, type, mpc_atanh);
9959 break;
9960
9961 CASE_FLT_FN (BUILT_IN_CABS):
9962 return fold_builtin_cabs (loc, arg0, type, fndecl);
9963
9964 CASE_FLT_FN (BUILT_IN_CARG):
9965 return fold_builtin_carg (loc, arg0, type);
9966
9967 CASE_FLT_FN (BUILT_IN_SQRT):
9968 return fold_builtin_sqrt (loc, arg0, type);
9969
9970 CASE_FLT_FN (BUILT_IN_CBRT):
9971 return fold_builtin_cbrt (loc, arg0, type);
9972
9973 CASE_FLT_FN (BUILT_IN_ASIN):
9974 if (validate_arg (arg0, REAL_TYPE))
9975 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9976 &dconstm1, &dconst1, true);
9977 break;
9978
9979 CASE_FLT_FN (BUILT_IN_ACOS):
9980 if (validate_arg (arg0, REAL_TYPE))
9981 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9982 &dconstm1, &dconst1, true);
9983 break;
9984
9985 CASE_FLT_FN (BUILT_IN_ATAN):
9986 if (validate_arg (arg0, REAL_TYPE))
9987 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9988 break;
9989
9990 CASE_FLT_FN (BUILT_IN_ASINH):
9991 if (validate_arg (arg0, REAL_TYPE))
9992 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9993 break;
9994
9995 CASE_FLT_FN (BUILT_IN_ACOSH):
9996 if (validate_arg (arg0, REAL_TYPE))
9997 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9998 &dconst1, NULL, true);
9999 break;
10000
10001 CASE_FLT_FN (BUILT_IN_ATANH):
10002 if (validate_arg (arg0, REAL_TYPE))
10003 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10004 &dconstm1, &dconst1, false);
10005 break;
10006
10007 CASE_FLT_FN (BUILT_IN_SIN):
10008 if (validate_arg (arg0, REAL_TYPE))
10009 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10010 break;
10011
10012 CASE_FLT_FN (BUILT_IN_COS):
10013 return fold_builtin_cos (loc, arg0, type, fndecl);
10014
10015 CASE_FLT_FN (BUILT_IN_TAN):
10016 return fold_builtin_tan (arg0, type);
10017
10018 CASE_FLT_FN (BUILT_IN_CEXP):
10019 return fold_builtin_cexp (loc, arg0, type);
10020
10021 CASE_FLT_FN (BUILT_IN_CEXPI):
10022 if (validate_arg (arg0, REAL_TYPE))
10023 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10024 break;
10025
10026 CASE_FLT_FN (BUILT_IN_SINH):
10027 if (validate_arg (arg0, REAL_TYPE))
10028 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10029 break;
10030
10031 CASE_FLT_FN (BUILT_IN_COSH):
10032 return fold_builtin_cosh (loc, arg0, type, fndecl);
10033
10034 CASE_FLT_FN (BUILT_IN_TANH):
10035 if (validate_arg (arg0, REAL_TYPE))
10036 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10037 break;
10038
10039 CASE_FLT_FN (BUILT_IN_ERF):
10040 if (validate_arg (arg0, REAL_TYPE))
10041 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10042 break;
10043
10044 CASE_FLT_FN (BUILT_IN_ERFC):
10045 if (validate_arg (arg0, REAL_TYPE))
10046 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10047 break;
10048
10049 CASE_FLT_FN (BUILT_IN_TGAMMA):
10050 if (validate_arg (arg0, REAL_TYPE))
10051 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10052 break;
10053
10054 CASE_FLT_FN (BUILT_IN_EXP):
10055 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10056
10057 CASE_FLT_FN (BUILT_IN_EXP2):
10058 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10059
10060 CASE_FLT_FN (BUILT_IN_EXP10):
10061 CASE_FLT_FN (BUILT_IN_POW10):
10062 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10063
10064 CASE_FLT_FN (BUILT_IN_EXPM1):
10065 if (validate_arg (arg0, REAL_TYPE))
10066 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10067 break;
10068
10069 CASE_FLT_FN (BUILT_IN_LOG):
10070 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10071
10072 CASE_FLT_FN (BUILT_IN_LOG2):
10073 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10074
10075 CASE_FLT_FN (BUILT_IN_LOG10):
10076 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10077
10078 CASE_FLT_FN (BUILT_IN_LOG1P):
10079 if (validate_arg (arg0, REAL_TYPE))
10080 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10081 &dconstm1, NULL, false);
10082 break;
10083
10084 CASE_FLT_FN (BUILT_IN_J0):
10085 if (validate_arg (arg0, REAL_TYPE))
10086 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10087 NULL, NULL, 0);
10088 break;
10089
10090 CASE_FLT_FN (BUILT_IN_J1):
10091 if (validate_arg (arg0, REAL_TYPE))
10092 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10093 NULL, NULL, 0);
10094 break;
10095
10096 CASE_FLT_FN (BUILT_IN_Y0):
10097 if (validate_arg (arg0, REAL_TYPE))
10098 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10099 &dconst0, NULL, false);
10100 break;
10101
10102 CASE_FLT_FN (BUILT_IN_Y1):
10103 if (validate_arg (arg0, REAL_TYPE))
10104 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10105 &dconst0, NULL, false);
10106 break;
10107
10108 CASE_FLT_FN (BUILT_IN_NAN):
10109 case BUILT_IN_NAND32:
10110 case BUILT_IN_NAND64:
10111 case BUILT_IN_NAND128:
10112 return fold_builtin_nan (arg0, type, true);
10113
10114 CASE_FLT_FN (BUILT_IN_NANS):
10115 return fold_builtin_nan (arg0, type, false);
10116
10117 CASE_FLT_FN (BUILT_IN_FLOOR):
10118 return fold_builtin_floor (loc, fndecl, arg0);
10119
10120 CASE_FLT_FN (BUILT_IN_CEIL):
10121 return fold_builtin_ceil (loc, fndecl, arg0);
10122
10123 CASE_FLT_FN (BUILT_IN_TRUNC):
10124 return fold_builtin_trunc (loc, fndecl, arg0);
10125
10126 CASE_FLT_FN (BUILT_IN_ROUND):
10127 return fold_builtin_round (loc, fndecl, arg0);
10128
10129 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10130 CASE_FLT_FN (BUILT_IN_RINT):
10131 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10132
10133 CASE_FLT_FN (BUILT_IN_ICEIL):
10134 CASE_FLT_FN (BUILT_IN_LCEIL):
10135 CASE_FLT_FN (BUILT_IN_LLCEIL):
10136 CASE_FLT_FN (BUILT_IN_LFLOOR):
10137 CASE_FLT_FN (BUILT_IN_IFLOOR):
10138 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10139 CASE_FLT_FN (BUILT_IN_IROUND):
10140 CASE_FLT_FN (BUILT_IN_LROUND):
10141 CASE_FLT_FN (BUILT_IN_LLROUND):
10142 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10143
10144 CASE_FLT_FN (BUILT_IN_IRINT):
10145 CASE_FLT_FN (BUILT_IN_LRINT):
10146 CASE_FLT_FN (BUILT_IN_LLRINT):
10147 return fold_fixed_mathfn (loc, fndecl, arg0);
10148
10149 case BUILT_IN_BSWAP16:
10150 case BUILT_IN_BSWAP32:
10151 case BUILT_IN_BSWAP64:
10152 return fold_builtin_bswap (fndecl, arg0);
10153
10154 CASE_INT_FN (BUILT_IN_FFS):
10155 CASE_INT_FN (BUILT_IN_CLZ):
10156 CASE_INT_FN (BUILT_IN_CTZ):
10157 CASE_INT_FN (BUILT_IN_CLRSB):
10158 CASE_INT_FN (BUILT_IN_POPCOUNT):
10159 CASE_INT_FN (BUILT_IN_PARITY):
10160 return fold_builtin_bitop (fndecl, arg0);
10161
10162 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10163 return fold_builtin_signbit (loc, arg0, type);
10164
10165 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10166 return fold_builtin_significand (loc, arg0, type);
10167
10168 CASE_FLT_FN (BUILT_IN_ILOGB):
10169 CASE_FLT_FN (BUILT_IN_LOGB):
10170 return fold_builtin_logb (loc, arg0, type);
10171
10172 case BUILT_IN_ISASCII:
10173 return fold_builtin_isascii (loc, arg0);
10174
10175 case BUILT_IN_TOASCII:
10176 return fold_builtin_toascii (loc, arg0);
10177
10178 case BUILT_IN_ISDIGIT:
10179 return fold_builtin_isdigit (loc, arg0);
10180
10181 CASE_FLT_FN (BUILT_IN_FINITE):
10182 case BUILT_IN_FINITED32:
10183 case BUILT_IN_FINITED64:
10184 case BUILT_IN_FINITED128:
10185 case BUILT_IN_ISFINITE:
10186 {
10187 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10188 if (ret)
10189 return ret;
10190 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10191 }
10192
10193 CASE_FLT_FN (BUILT_IN_ISINF):
10194 case BUILT_IN_ISINFD32:
10195 case BUILT_IN_ISINFD64:
10196 case BUILT_IN_ISINFD128:
10197 {
10198 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10199 if (ret)
10200 return ret;
10201 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10202 }
10203
10204 case BUILT_IN_ISNORMAL:
10205 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10206
10207 case BUILT_IN_ISINF_SIGN:
10208 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10209
10210 CASE_FLT_FN (BUILT_IN_ISNAN):
10211 case BUILT_IN_ISNAND32:
10212 case BUILT_IN_ISNAND64:
10213 case BUILT_IN_ISNAND128:
10214 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10215
10216 case BUILT_IN_PRINTF:
10217 case BUILT_IN_PRINTF_UNLOCKED:
10218 case BUILT_IN_VPRINTF:
10219 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10220
10221 case BUILT_IN_FREE:
10222 if (integer_zerop (arg0))
10223 return build_empty_stmt (loc);
10224 break;
10225
10226 default:
10227 break;
10228 }
10229
10230 return NULL_TREE;
10231
10232 }
10233
10234 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10235 IGNORE is true if the result of the function call is ignored. This
10236 function returns NULL_TREE if no simplification was possible. */
10237
10238 static tree
10239 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10240 {
10241 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10242 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10243
10244 switch (fcode)
10245 {
10246 CASE_FLT_FN (BUILT_IN_JN):
10247 if (validate_arg (arg0, INTEGER_TYPE)
10248 && validate_arg (arg1, REAL_TYPE))
10249 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10250 break;
10251
10252 CASE_FLT_FN (BUILT_IN_YN):
10253 if (validate_arg (arg0, INTEGER_TYPE)
10254 && validate_arg (arg1, REAL_TYPE))
10255 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10256 &dconst0, false);
10257 break;
10258
10259 CASE_FLT_FN (BUILT_IN_DREM):
10260 CASE_FLT_FN (BUILT_IN_REMAINDER):
10261 if (validate_arg (arg0, REAL_TYPE)
10262 && validate_arg (arg1, REAL_TYPE))
10263 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10264 break;
10265
10266 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10267 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10268 if (validate_arg (arg0, REAL_TYPE)
10269 && validate_arg (arg1, POINTER_TYPE))
10270 return do_mpfr_lgamma_r (arg0, arg1, type);
10271 break;
10272
10273 CASE_FLT_FN (BUILT_IN_ATAN2):
10274 if (validate_arg (arg0, REAL_TYPE)
10275 && validate_arg (arg1, REAL_TYPE))
10276 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10277 break;
10278
10279 CASE_FLT_FN (BUILT_IN_FDIM):
10280 if (validate_arg (arg0, REAL_TYPE)
10281 && validate_arg (arg1, REAL_TYPE))
10282 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10283 break;
10284
10285 CASE_FLT_FN (BUILT_IN_HYPOT):
10286 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10287
10288 CASE_FLT_FN (BUILT_IN_CPOW):
10289 if (validate_arg (arg0, COMPLEX_TYPE)
10290 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10291 && validate_arg (arg1, COMPLEX_TYPE)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10293 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10294 break;
10295
10296 CASE_FLT_FN (BUILT_IN_LDEXP):
10297 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10298 CASE_FLT_FN (BUILT_IN_SCALBN):
10299 CASE_FLT_FN (BUILT_IN_SCALBLN):
10300 return fold_builtin_load_exponent (loc, arg0, arg1,
10301 type, /*ldexp=*/false);
10302
10303 CASE_FLT_FN (BUILT_IN_FREXP):
10304 return fold_builtin_frexp (loc, arg0, arg1, type);
10305
10306 CASE_FLT_FN (BUILT_IN_MODF):
10307 return fold_builtin_modf (loc, arg0, arg1, type);
10308
10309 case BUILT_IN_STRSTR:
10310 return fold_builtin_strstr (loc, arg0, arg1, type);
10311
10312 case BUILT_IN_STRSPN:
10313 return fold_builtin_strspn (loc, arg0, arg1);
10314
10315 case BUILT_IN_STRCSPN:
10316 return fold_builtin_strcspn (loc, arg0, arg1);
10317
10318 case BUILT_IN_STRCHR:
10319 case BUILT_IN_INDEX:
10320 return fold_builtin_strchr (loc, arg0, arg1, type);
10321
10322 case BUILT_IN_STRRCHR:
10323 case BUILT_IN_RINDEX:
10324 return fold_builtin_strrchr (loc, arg0, arg1, type);
10325
10326 case BUILT_IN_STPCPY:
10327 if (ignore)
10328 {
10329 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10330 if (!fn)
10331 break;
10332
10333 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10334 }
10335 else
10336 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10337 break;
10338
10339 case BUILT_IN_STRCMP:
10340 return fold_builtin_strcmp (loc, arg0, arg1);
10341
10342 case BUILT_IN_STRPBRK:
10343 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10344
10345 case BUILT_IN_EXPECT:
10346 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10347
10348 CASE_FLT_FN (BUILT_IN_POW):
10349 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10350
10351 CASE_FLT_FN (BUILT_IN_POWI):
10352 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10353
10354 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10355 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10356
10357 CASE_FLT_FN (BUILT_IN_FMIN):
10358 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10359
10360 CASE_FLT_FN (BUILT_IN_FMAX):
10361 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10362
10363 case BUILT_IN_ISGREATER:
10364 return fold_builtin_unordered_cmp (loc, fndecl,
10365 arg0, arg1, UNLE_EXPR, LE_EXPR);
10366 case BUILT_IN_ISGREATEREQUAL:
10367 return fold_builtin_unordered_cmp (loc, fndecl,
10368 arg0, arg1, UNLT_EXPR, LT_EXPR);
10369 case BUILT_IN_ISLESS:
10370 return fold_builtin_unordered_cmp (loc, fndecl,
10371 arg0, arg1, UNGE_EXPR, GE_EXPR);
10372 case BUILT_IN_ISLESSEQUAL:
10373 return fold_builtin_unordered_cmp (loc, fndecl,
10374 arg0, arg1, UNGT_EXPR, GT_EXPR);
10375 case BUILT_IN_ISLESSGREATER:
10376 return fold_builtin_unordered_cmp (loc, fndecl,
10377 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10378 case BUILT_IN_ISUNORDERED:
10379 return fold_builtin_unordered_cmp (loc, fndecl,
10380 arg0, arg1, UNORDERED_EXPR,
10381 NOP_EXPR);
10382
10383 /* We do the folding for va_start in the expander. */
10384 case BUILT_IN_VA_START:
10385 break;
10386
10387 case BUILT_IN_OBJECT_SIZE:
10388 return fold_builtin_object_size (arg0, arg1);
10389
10390 case BUILT_IN_PRINTF:
10391 case BUILT_IN_PRINTF_UNLOCKED:
10392 case BUILT_IN_VPRINTF:
10393 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10394
10395 case BUILT_IN_PRINTF_CHK:
10396 case BUILT_IN_VPRINTF_CHK:
10397 if (!validate_arg (arg0, INTEGER_TYPE)
10398 || TREE_SIDE_EFFECTS (arg0))
10399 return NULL_TREE;
10400 else
10401 return fold_builtin_printf (loc, fndecl,
10402 arg1, NULL_TREE, ignore, fcode);
10403 break;
10404
10405 case BUILT_IN_FPRINTF:
10406 case BUILT_IN_FPRINTF_UNLOCKED:
10407 case BUILT_IN_VFPRINTF:
10408 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10409 ignore, fcode);
10410
10411 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10412 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10413
10414 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10415 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10416
10417 default:
10418 break;
10419 }
10420 return NULL_TREE;
10421 }
10422
10423 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10424 and ARG2. IGNORE is true if the result of the function call is ignored.
10425 This function returns NULL_TREE if no simplification was possible. */
10426
10427 static tree
10428 fold_builtin_3 (location_t loc, tree fndecl,
10429 tree arg0, tree arg1, tree arg2, bool ignore)
10430 {
10431 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10432 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10433 switch (fcode)
10434 {
10435
10436 CASE_FLT_FN (BUILT_IN_SINCOS):
10437 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10438
10439 CASE_FLT_FN (BUILT_IN_FMA):
10440 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10441 break;
10442
10443 CASE_FLT_FN (BUILT_IN_REMQUO):
10444 if (validate_arg (arg0, REAL_TYPE)
10445 && validate_arg (arg1, REAL_TYPE)
10446 && validate_arg (arg2, POINTER_TYPE))
10447 return do_mpfr_remquo (arg0, arg1, arg2);
10448 break;
10449
10450 case BUILT_IN_STRNCAT:
10451 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10452
10453 case BUILT_IN_STRNCMP:
10454 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10455
10456 case BUILT_IN_MEMCHR:
10457 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10458
10459 case BUILT_IN_BCMP:
10460 case BUILT_IN_MEMCMP:
10461 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10462
10463 case BUILT_IN_PRINTF_CHK:
10464 case BUILT_IN_VPRINTF_CHK:
10465 if (!validate_arg (arg0, INTEGER_TYPE)
10466 || TREE_SIDE_EFFECTS (arg0))
10467 return NULL_TREE;
10468 else
10469 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10470 break;
10471
10472 case BUILT_IN_FPRINTF:
10473 case BUILT_IN_FPRINTF_UNLOCKED:
10474 case BUILT_IN_VFPRINTF:
10475 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10476 ignore, fcode);
10477
10478 case BUILT_IN_FPRINTF_CHK:
10479 case BUILT_IN_VFPRINTF_CHK:
10480 if (!validate_arg (arg1, INTEGER_TYPE)
10481 || TREE_SIDE_EFFECTS (arg1))
10482 return NULL_TREE;
10483 else
10484 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10485 ignore, fcode);
10486
10487 case BUILT_IN_EXPECT:
10488 return fold_builtin_expect (loc, arg0, arg1, arg2);
10489
10490 case BUILT_IN_ADD_OVERFLOW:
10491 case BUILT_IN_SUB_OVERFLOW:
10492 case BUILT_IN_MUL_OVERFLOW:
10493 case BUILT_IN_SADD_OVERFLOW:
10494 case BUILT_IN_SADDL_OVERFLOW:
10495 case BUILT_IN_SADDLL_OVERFLOW:
10496 case BUILT_IN_SSUB_OVERFLOW:
10497 case BUILT_IN_SSUBL_OVERFLOW:
10498 case BUILT_IN_SSUBLL_OVERFLOW:
10499 case BUILT_IN_SMUL_OVERFLOW:
10500 case BUILT_IN_SMULL_OVERFLOW:
10501 case BUILT_IN_SMULLL_OVERFLOW:
10502 case BUILT_IN_UADD_OVERFLOW:
10503 case BUILT_IN_UADDL_OVERFLOW:
10504 case BUILT_IN_UADDLL_OVERFLOW:
10505 case BUILT_IN_USUB_OVERFLOW:
10506 case BUILT_IN_USUBL_OVERFLOW:
10507 case BUILT_IN_USUBLL_OVERFLOW:
10508 case BUILT_IN_UMUL_OVERFLOW:
10509 case BUILT_IN_UMULL_OVERFLOW:
10510 case BUILT_IN_UMULLL_OVERFLOW:
10511 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10512
10513 default:
10514 break;
10515 }
10516 return NULL_TREE;
10517 }
10518
10519 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10520 ARG2, and ARG3. IGNORE is true if the result of the function call is
10521 ignored. This function returns NULL_TREE if no simplification was
10522 possible. */
10523
10524 static tree
10525 fold_builtin_4 (location_t loc, tree fndecl,
10526 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10527 {
10528 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10529
10530 switch (fcode)
10531 {
10532 case BUILT_IN_FPRINTF_CHK:
10533 case BUILT_IN_VFPRINTF_CHK:
10534 if (!validate_arg (arg1, INTEGER_TYPE)
10535 || TREE_SIDE_EFFECTS (arg1))
10536 return NULL_TREE;
10537 else
10538 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10539 ignore, fcode);
10540 break;
10541
10542 default:
10543 break;
10544 }
10545 return NULL_TREE;
10546 }
10547
10548 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10549 arguments, where NARGS <= 4. IGNORE is true if the result of the
10550 function call is ignored. This function returns NULL_TREE if no
10551 simplification was possible. Note that this only folds builtins with
10552 fixed argument patterns. Foldings that do varargs-to-varargs
10553 transformations, or that match calls with more than 4 arguments,
10554 need to be handled with fold_builtin_varargs instead. */
10555
10556 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10557
10558 tree
10559 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10560 {
10561 tree ret = NULL_TREE;
10562
10563 switch (nargs)
10564 {
10565 case 0:
10566 ret = fold_builtin_0 (loc, fndecl, ignore);
10567 break;
10568 case 1:
10569 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10570 break;
10571 case 2:
10572 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10573 break;
10574 case 3:
10575 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10576 break;
10577 case 4:
10578 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10579 ignore);
10580 break;
10581 default:
10582 break;
10583 }
10584 if (ret)
10585 {
10586 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10587 SET_EXPR_LOCATION (ret, loc);
10588 TREE_NO_WARNING (ret) = 1;
10589 return ret;
10590 }
10591 return NULL_TREE;
10592 }
10593
10594 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10595 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10596 of arguments in ARGS to be omitted. OLDNARGS is the number of
10597 elements in ARGS. */
10598
10599 static tree
10600 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10601 int skip, tree fndecl, int n, va_list newargs)
10602 {
10603 int nargs = oldnargs - skip + n;
10604 tree *buffer;
10605
10606 if (n > 0)
10607 {
10608 int i, j;
10609
10610 buffer = XALLOCAVEC (tree, nargs);
10611 for (i = 0; i < n; i++)
10612 buffer[i] = va_arg (newargs, tree);
10613 for (j = skip; j < oldnargs; j++, i++)
10614 buffer[i] = args[j];
10615 }
10616 else
10617 buffer = args + skip;
10618
10619 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10620 }
10621
10622 /* Return true if FNDECL shouldn't be folded right now.
10623 If a built-in function has an inline attribute always_inline
10624 wrapper, defer folding it after always_inline functions have
10625 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10626 might not be performed. */
10627
10628 bool
10629 avoid_folding_inline_builtin (tree fndecl)
10630 {
10631 return (DECL_DECLARED_INLINE_P (fndecl)
10632 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10633 && cfun
10634 && !cfun->always_inline_functions_inlined
10635 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10636 }
10637
10638 /* A wrapper function for builtin folding that prevents warnings for
10639 "statement without effect" and the like, caused by removing the
10640 call node earlier than the warning is generated. */
10641
10642 tree
10643 fold_call_expr (location_t loc, tree exp, bool ignore)
10644 {
10645 tree ret = NULL_TREE;
10646 tree fndecl = get_callee_fndecl (exp);
10647 if (fndecl
10648 && TREE_CODE (fndecl) == FUNCTION_DECL
10649 && DECL_BUILT_IN (fndecl)
10650 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10651 yet. Defer folding until we see all the arguments
10652 (after inlining). */
10653 && !CALL_EXPR_VA_ARG_PACK (exp))
10654 {
10655 int nargs = call_expr_nargs (exp);
10656
10657 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10658 instead last argument is __builtin_va_arg_pack (). Defer folding
10659 even in that case, until arguments are finalized. */
10660 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10661 {
10662 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10663 if (fndecl2
10664 && TREE_CODE (fndecl2) == FUNCTION_DECL
10665 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10666 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10667 return NULL_TREE;
10668 }
10669
10670 if (avoid_folding_inline_builtin (fndecl))
10671 return NULL_TREE;
10672
10673 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10674 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10675 CALL_EXPR_ARGP (exp), ignore);
10676 else
10677 {
10678 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10679 {
10680 tree *args = CALL_EXPR_ARGP (exp);
10681 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10682 }
10683 if (!ret)
10684 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10685 if (ret)
10686 return ret;
10687 }
10688 }
10689 return NULL_TREE;
10690 }
10691
10692 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10693 N arguments are passed in the array ARGARRAY. */
10694
10695 tree
10696 fold_builtin_call_array (location_t loc, tree type,
10697 tree fn,
10698 int n,
10699 tree *argarray)
10700 {
10701 tree ret = NULL_TREE;
10702 tree exp;
10703
10704 if (TREE_CODE (fn) == ADDR_EXPR)
10705 {
10706 tree fndecl = TREE_OPERAND (fn, 0);
10707 if (TREE_CODE (fndecl) == FUNCTION_DECL
10708 && DECL_BUILT_IN (fndecl))
10709 {
10710 /* If last argument is __builtin_va_arg_pack (), arguments to this
10711 function are not finalized yet. Defer folding until they are. */
10712 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10713 {
10714 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10715 if (fndecl2
10716 && TREE_CODE (fndecl2) == FUNCTION_DECL
10717 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10718 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10719 return build_call_array_loc (loc, type, fn, n, argarray);
10720 }
10721 if (avoid_folding_inline_builtin (fndecl))
10722 return build_call_array_loc (loc, type, fn, n, argarray);
10723 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10724 {
10725 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10726 if (ret)
10727 return ret;
10728
10729 return build_call_array_loc (loc, type, fn, n, argarray);
10730 }
10731 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10732 {
10733 /* First try the transformations that don't require consing up
10734 an exp. */
10735 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10736 if (ret)
10737 return ret;
10738 }
10739
10740 /* If we got this far, we need to build an exp. */
10741 exp = build_call_array_loc (loc, type, fn, n, argarray);
10742 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10743 return ret ? ret : exp;
10744 }
10745 }
10746
10747 return build_call_array_loc (loc, type, fn, n, argarray);
10748 }
10749
10750 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10751 along with N new arguments specified as the "..." parameters. SKIP
10752 is the number of arguments in EXP to be omitted. This function is used
10753 to do varargs-to-varargs transformations. */
10754
10755 static tree
10756 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10757 {
10758 va_list ap;
10759 tree t;
10760
10761 va_start (ap, n);
10762 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10763 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10764 va_end (ap);
10765
10766 return t;
10767 }
10768
10769 /* Validate a single argument ARG against a tree code CODE representing
10770 a type. */
10771
10772 static bool
10773 validate_arg (const_tree arg, enum tree_code code)
10774 {
10775 if (!arg)
10776 return false;
10777 else if (code == POINTER_TYPE)
10778 return POINTER_TYPE_P (TREE_TYPE (arg));
10779 else if (code == INTEGER_TYPE)
10780 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10781 return code == TREE_CODE (TREE_TYPE (arg));
10782 }
10783
10784 /* This function validates the types of a function call argument list
10785 against a specified list of tree_codes. If the last specifier is a 0,
10786 that represents an ellipses, otherwise the last specifier must be a
10787 VOID_TYPE.
10788
10789 This is the GIMPLE version of validate_arglist. Eventually we want to
10790 completely convert builtins.c to work from GIMPLEs and the tree based
10791 validate_arglist will then be removed. */
10792
10793 bool
10794 validate_gimple_arglist (const_gimple call, ...)
10795 {
10796 enum tree_code code;
10797 bool res = 0;
10798 va_list ap;
10799 const_tree arg;
10800 size_t i;
10801
10802 va_start (ap, call);
10803 i = 0;
10804
10805 do
10806 {
10807 code = (enum tree_code) va_arg (ap, int);
10808 switch (code)
10809 {
10810 case 0:
10811 /* This signifies an ellipses, any further arguments are all ok. */
10812 res = true;
10813 goto end;
10814 case VOID_TYPE:
10815 /* This signifies an endlink, if no arguments remain, return
10816 true, otherwise return false. */
10817 res = (i == gimple_call_num_args (call));
10818 goto end;
10819 default:
10820 /* If no parameters remain or the parameter's code does not
10821 match the specified code, return false. Otherwise continue
10822 checking any remaining arguments. */
10823 arg = gimple_call_arg (call, i++);
10824 if (!validate_arg (arg, code))
10825 goto end;
10826 break;
10827 }
10828 }
10829 while (1);
10830
10831 /* We need gotos here since we can only have one VA_CLOSE in a
10832 function. */
10833 end: ;
10834 va_end (ap);
10835
10836 return res;
10837 }
10838
10839 /* Default target-specific builtin expander that does nothing. */
10840
10841 rtx
10842 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10843 rtx target ATTRIBUTE_UNUSED,
10844 rtx subtarget ATTRIBUTE_UNUSED,
10845 machine_mode mode ATTRIBUTE_UNUSED,
10846 int ignore ATTRIBUTE_UNUSED)
10847 {
10848 return NULL_RTX;
10849 }
10850
10851 /* Returns true is EXP represents data that would potentially reside
10852 in a readonly section. */
10853
10854 bool
10855 readonly_data_expr (tree exp)
10856 {
10857 STRIP_NOPS (exp);
10858
10859 if (TREE_CODE (exp) != ADDR_EXPR)
10860 return false;
10861
10862 exp = get_base_address (TREE_OPERAND (exp, 0));
10863 if (!exp)
10864 return false;
10865
10866 /* Make sure we call decl_readonly_section only for trees it
10867 can handle (since it returns true for everything it doesn't
10868 understand). */
10869 if (TREE_CODE (exp) == STRING_CST
10870 || TREE_CODE (exp) == CONSTRUCTOR
10871 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10872 return decl_readonly_section (exp, 0);
10873 else
10874 return false;
10875 }
10876
10877 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10878 to the call, and TYPE is its return type.
10879
10880 Return NULL_TREE if no simplification was possible, otherwise return the
10881 simplified form of the call as a tree.
10882
10883 The simplified form may be a constant or other expression which
10884 computes the same value, but in a more efficient manner (including
10885 calls to other builtin functions).
10886
10887 The call may contain arguments which need to be evaluated, but
10888 which are not useful to determine the result of the call. In
10889 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10890 COMPOUND_EXPR will be an argument which must be evaluated.
10891 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10892 COMPOUND_EXPR in the chain will contain the tree for the simplified
10893 form of the builtin function call. */
10894
10895 static tree
10896 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10897 {
10898 if (!validate_arg (s1, POINTER_TYPE)
10899 || !validate_arg (s2, POINTER_TYPE))
10900 return NULL_TREE;
10901 else
10902 {
10903 tree fn;
10904 const char *p1, *p2;
10905
10906 p2 = c_getstr (s2);
10907 if (p2 == NULL)
10908 return NULL_TREE;
10909
10910 p1 = c_getstr (s1);
10911 if (p1 != NULL)
10912 {
10913 const char *r = strstr (p1, p2);
10914 tree tem;
10915
10916 if (r == NULL)
10917 return build_int_cst (TREE_TYPE (s1), 0);
10918
10919 /* Return an offset into the constant string argument. */
10920 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10921 return fold_convert_loc (loc, type, tem);
10922 }
10923
10924 /* The argument is const char *, and the result is char *, so we need
10925 a type conversion here to avoid a warning. */
10926 if (p2[0] == '\0')
10927 return fold_convert_loc (loc, type, s1);
10928
10929 if (p2[1] != '\0')
10930 return NULL_TREE;
10931
10932 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10933 if (!fn)
10934 return NULL_TREE;
10935
10936 /* New argument list transforming strstr(s1, s2) to
10937 strchr(s1, s2[0]). */
10938 return build_call_expr_loc (loc, fn, 2, s1,
10939 build_int_cst (integer_type_node, p2[0]));
10940 }
10941 }
10942
10943 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10944 the call, and TYPE is its return type.
10945
10946 Return NULL_TREE if no simplification was possible, otherwise return the
10947 simplified form of the call as a tree.
10948
10949 The simplified form may be a constant or other expression which
10950 computes the same value, but in a more efficient manner (including
10951 calls to other builtin functions).
10952
10953 The call may contain arguments which need to be evaluated, but
10954 which are not useful to determine the result of the call. In
10955 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10956 COMPOUND_EXPR will be an argument which must be evaluated.
10957 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10958 COMPOUND_EXPR in the chain will contain the tree for the simplified
10959 form of the builtin function call. */
10960
10961 static tree
10962 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10963 {
10964 if (!validate_arg (s1, POINTER_TYPE)
10965 || !validate_arg (s2, INTEGER_TYPE))
10966 return NULL_TREE;
10967 else
10968 {
10969 const char *p1;
10970
10971 if (TREE_CODE (s2) != INTEGER_CST)
10972 return NULL_TREE;
10973
10974 p1 = c_getstr (s1);
10975 if (p1 != NULL)
10976 {
10977 char c;
10978 const char *r;
10979 tree tem;
10980
10981 if (target_char_cast (s2, &c))
10982 return NULL_TREE;
10983
10984 r = strchr (p1, c);
10985
10986 if (r == NULL)
10987 return build_int_cst (TREE_TYPE (s1), 0);
10988
10989 /* Return an offset into the constant string argument. */
10990 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10991 return fold_convert_loc (loc, type, tem);
10992 }
10993 return NULL_TREE;
10994 }
10995 }
10996
10997 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10998 the call, and TYPE is its return type.
10999
11000 Return NULL_TREE if no simplification was possible, otherwise return the
11001 simplified form of the call as a tree.
11002
11003 The simplified form may be a constant or other expression which
11004 computes the same value, but in a more efficient manner (including
11005 calls to other builtin functions).
11006
11007 The call may contain arguments which need to be evaluated, but
11008 which are not useful to determine the result of the call. In
11009 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11010 COMPOUND_EXPR will be an argument which must be evaluated.
11011 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11012 COMPOUND_EXPR in the chain will contain the tree for the simplified
11013 form of the builtin function call. */
11014
11015 static tree
11016 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11017 {
11018 if (!validate_arg (s1, POINTER_TYPE)
11019 || !validate_arg (s2, INTEGER_TYPE))
11020 return NULL_TREE;
11021 else
11022 {
11023 tree fn;
11024 const char *p1;
11025
11026 if (TREE_CODE (s2) != INTEGER_CST)
11027 return NULL_TREE;
11028
11029 p1 = c_getstr (s1);
11030 if (p1 != NULL)
11031 {
11032 char c;
11033 const char *r;
11034 tree tem;
11035
11036 if (target_char_cast (s2, &c))
11037 return NULL_TREE;
11038
11039 r = strrchr (p1, c);
11040
11041 if (r == NULL)
11042 return build_int_cst (TREE_TYPE (s1), 0);
11043
11044 /* Return an offset into the constant string argument. */
11045 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11046 return fold_convert_loc (loc, type, tem);
11047 }
11048
11049 if (! integer_zerop (s2))
11050 return NULL_TREE;
11051
11052 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11053 if (!fn)
11054 return NULL_TREE;
11055
11056 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11057 return build_call_expr_loc (loc, fn, 2, s1, s2);
11058 }
11059 }
11060
11061 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11062 to the call, and TYPE is its return type.
11063
11064 Return NULL_TREE if no simplification was possible, otherwise return the
11065 simplified form of the call as a tree.
11066
11067 The simplified form may be a constant or other expression which
11068 computes the same value, but in a more efficient manner (including
11069 calls to other builtin functions).
11070
11071 The call may contain arguments which need to be evaluated, but
11072 which are not useful to determine the result of the call. In
11073 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11074 COMPOUND_EXPR will be an argument which must be evaluated.
11075 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11076 COMPOUND_EXPR in the chain will contain the tree for the simplified
11077 form of the builtin function call. */
11078
11079 static tree
11080 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11081 {
11082 if (!validate_arg (s1, POINTER_TYPE)
11083 || !validate_arg (s2, POINTER_TYPE))
11084 return NULL_TREE;
11085 else
11086 {
11087 tree fn;
11088 const char *p1, *p2;
11089
11090 p2 = c_getstr (s2);
11091 if (p2 == NULL)
11092 return NULL_TREE;
11093
11094 p1 = c_getstr (s1);
11095 if (p1 != NULL)
11096 {
11097 const char *r = strpbrk (p1, p2);
11098 tree tem;
11099
11100 if (r == NULL)
11101 return build_int_cst (TREE_TYPE (s1), 0);
11102
11103 /* Return an offset into the constant string argument. */
11104 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11105 return fold_convert_loc (loc, type, tem);
11106 }
11107
11108 if (p2[0] == '\0')
11109 /* strpbrk(x, "") == NULL.
11110 Evaluate and ignore s1 in case it had side-effects. */
11111 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11112
11113 if (p2[1] != '\0')
11114 return NULL_TREE; /* Really call strpbrk. */
11115
11116 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11117 if (!fn)
11118 return NULL_TREE;
11119
11120 /* New argument list transforming strpbrk(s1, s2) to
11121 strchr(s1, s2[0]). */
11122 return build_call_expr_loc (loc, fn, 2, s1,
11123 build_int_cst (integer_type_node, p2[0]));
11124 }
11125 }
11126
11127 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11128 arguments to the call.
11129
11130 Return NULL_TREE if no simplification was possible, otherwise return the
11131 simplified form of the call as a tree.
11132
11133 The simplified form may be a constant or other expression which
11134 computes the same value, but in a more efficient manner (including
11135 calls to other builtin functions).
11136
11137 The call may contain arguments which need to be evaluated, but
11138 which are not useful to determine the result of the call. In
11139 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11140 COMPOUND_EXPR will be an argument which must be evaluated.
11141 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11142 COMPOUND_EXPR in the chain will contain the tree for the simplified
11143 form of the builtin function call. */
11144
11145 static tree
11146 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11147 {
11148 if (!validate_arg (dst, POINTER_TYPE)
11149 || !validate_arg (src, POINTER_TYPE)
11150 || !validate_arg (len, INTEGER_TYPE))
11151 return NULL_TREE;
11152 else
11153 {
11154 const char *p = c_getstr (src);
11155
11156 /* If the requested length is zero, or the src parameter string
11157 length is zero, return the dst parameter. */
11158 if (integer_zerop (len) || (p && *p == '\0'))
11159 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11160
11161 /* If the requested len is greater than or equal to the string
11162 length, call strcat. */
11163 if (TREE_CODE (len) == INTEGER_CST && p
11164 && compare_tree_int (len, strlen (p)) >= 0)
11165 {
11166 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11167
11168 /* If the replacement _DECL isn't initialized, don't do the
11169 transformation. */
11170 if (!fn)
11171 return NULL_TREE;
11172
11173 return build_call_expr_loc (loc, fn, 2, dst, src);
11174 }
11175 return NULL_TREE;
11176 }
11177 }
11178
11179 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11180 to the call.
11181
11182 Return NULL_TREE if no simplification was possible, otherwise return the
11183 simplified form of the call as a tree.
11184
11185 The simplified form may be a constant or other expression which
11186 computes the same value, but in a more efficient manner (including
11187 calls to other builtin functions).
11188
11189 The call may contain arguments which need to be evaluated, but
11190 which are not useful to determine the result of the call. In
11191 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11192 COMPOUND_EXPR will be an argument which must be evaluated.
11193 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11194 COMPOUND_EXPR in the chain will contain the tree for the simplified
11195 form of the builtin function call. */
11196
11197 static tree
11198 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11199 {
11200 if (!validate_arg (s1, POINTER_TYPE)
11201 || !validate_arg (s2, POINTER_TYPE))
11202 return NULL_TREE;
11203 else
11204 {
11205 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11206
11207 /* If both arguments are constants, evaluate at compile-time. */
11208 if (p1 && p2)
11209 {
11210 const size_t r = strspn (p1, p2);
11211 return build_int_cst (size_type_node, r);
11212 }
11213
11214 /* If either argument is "", return NULL_TREE. */
11215 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11216 /* Evaluate and ignore both arguments in case either one has
11217 side-effects. */
11218 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11219 s1, s2);
11220 return NULL_TREE;
11221 }
11222 }
11223
11224 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11225 to the call.
11226
11227 Return NULL_TREE if no simplification was possible, otherwise return the
11228 simplified form of the call as a tree.
11229
11230 The simplified form may be a constant or other expression which
11231 computes the same value, but in a more efficient manner (including
11232 calls to other builtin functions).
11233
11234 The call may contain arguments which need to be evaluated, but
11235 which are not useful to determine the result of the call. In
11236 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11237 COMPOUND_EXPR will be an argument which must be evaluated.
11238 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11239 COMPOUND_EXPR in the chain will contain the tree for the simplified
11240 form of the builtin function call. */
11241
11242 static tree
11243 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11244 {
11245 if (!validate_arg (s1, POINTER_TYPE)
11246 || !validate_arg (s2, POINTER_TYPE))
11247 return NULL_TREE;
11248 else
11249 {
11250 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11251
11252 /* If both arguments are constants, evaluate at compile-time. */
11253 if (p1 && p2)
11254 {
11255 const size_t r = strcspn (p1, p2);
11256 return build_int_cst (size_type_node, r);
11257 }
11258
11259 /* If the first argument is "", return NULL_TREE. */
11260 if (p1 && *p1 == '\0')
11261 {
11262 /* Evaluate and ignore argument s2 in case it has
11263 side-effects. */
11264 return omit_one_operand_loc (loc, size_type_node,
11265 size_zero_node, s2);
11266 }
11267
11268 /* If the second argument is "", return __builtin_strlen(s1). */
11269 if (p2 && *p2 == '\0')
11270 {
11271 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11272
11273 /* If the replacement _DECL isn't initialized, don't do the
11274 transformation. */
11275 if (!fn)
11276 return NULL_TREE;
11277
11278 return build_call_expr_loc (loc, fn, 1, s1);
11279 }
11280 return NULL_TREE;
11281 }
11282 }
11283
11284 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11285 produced. False otherwise. This is done so that we don't output the error
11286 or warning twice or three times. */
11287
11288 bool
11289 fold_builtin_next_arg (tree exp, bool va_start_p)
11290 {
11291 tree fntype = TREE_TYPE (current_function_decl);
11292 int nargs = call_expr_nargs (exp);
11293 tree arg;
11294 /* There is good chance the current input_location points inside the
11295 definition of the va_start macro (perhaps on the token for
11296 builtin) in a system header, so warnings will not be emitted.
11297 Use the location in real source code. */
11298 source_location current_location =
11299 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11300 NULL);
11301
11302 if (!stdarg_p (fntype))
11303 {
11304 error ("%<va_start%> used in function with fixed args");
11305 return true;
11306 }
11307
11308 if (va_start_p)
11309 {
11310 if (va_start_p && (nargs != 2))
11311 {
11312 error ("wrong number of arguments to function %<va_start%>");
11313 return true;
11314 }
11315 arg = CALL_EXPR_ARG (exp, 1);
11316 }
11317 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11318 when we checked the arguments and if needed issued a warning. */
11319 else
11320 {
11321 if (nargs == 0)
11322 {
11323 /* Evidently an out of date version of <stdarg.h>; can't validate
11324 va_start's second argument, but can still work as intended. */
11325 warning_at (current_location,
11326 OPT_Wvarargs,
11327 "%<__builtin_next_arg%> called without an argument");
11328 return true;
11329 }
11330 else if (nargs > 1)
11331 {
11332 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11333 return true;
11334 }
11335 arg = CALL_EXPR_ARG (exp, 0);
11336 }
11337
11338 if (TREE_CODE (arg) == SSA_NAME)
11339 arg = SSA_NAME_VAR (arg);
11340
11341 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11342 or __builtin_next_arg (0) the first time we see it, after checking
11343 the arguments and if needed issuing a warning. */
11344 if (!integer_zerop (arg))
11345 {
11346 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11347
11348 /* Strip off all nops for the sake of the comparison. This
11349 is not quite the same as STRIP_NOPS. It does more.
11350 We must also strip off INDIRECT_EXPR for C++ reference
11351 parameters. */
11352 while (CONVERT_EXPR_P (arg)
11353 || TREE_CODE (arg) == INDIRECT_REF)
11354 arg = TREE_OPERAND (arg, 0);
11355 if (arg != last_parm)
11356 {
11357 /* FIXME: Sometimes with the tree optimizers we can get the
11358 not the last argument even though the user used the last
11359 argument. We just warn and set the arg to be the last
11360 argument so that we will get wrong-code because of
11361 it. */
11362 warning_at (current_location,
11363 OPT_Wvarargs,
11364 "second parameter of %<va_start%> not last named argument");
11365 }
11366
11367 /* Undefined by C99 7.15.1.4p4 (va_start):
11368 "If the parameter parmN is declared with the register storage
11369 class, with a function or array type, or with a type that is
11370 not compatible with the type that results after application of
11371 the default argument promotions, the behavior is undefined."
11372 */
11373 else if (DECL_REGISTER (arg))
11374 {
11375 warning_at (current_location,
11376 OPT_Wvarargs,
11377 "undefined behaviour when second parameter of "
11378 "%<va_start%> is declared with %<register%> storage");
11379 }
11380
11381 /* We want to verify the second parameter just once before the tree
11382 optimizers are run and then avoid keeping it in the tree,
11383 as otherwise we could warn even for correct code like:
11384 void foo (int i, ...)
11385 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11386 if (va_start_p)
11387 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11388 else
11389 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11390 }
11391 return false;
11392 }
11393
11394
11395 /* Expand a call EXP to __builtin_object_size. */
11396
11397 static rtx
11398 expand_builtin_object_size (tree exp)
11399 {
11400 tree ost;
11401 int object_size_type;
11402 tree fndecl = get_callee_fndecl (exp);
11403
11404 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11405 {
11406 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11407 exp, fndecl);
11408 expand_builtin_trap ();
11409 return const0_rtx;
11410 }
11411
11412 ost = CALL_EXPR_ARG (exp, 1);
11413 STRIP_NOPS (ost);
11414
11415 if (TREE_CODE (ost) != INTEGER_CST
11416 || tree_int_cst_sgn (ost) < 0
11417 || compare_tree_int (ost, 3) > 0)
11418 {
11419 error ("%Klast argument of %D is not integer constant between 0 and 3",
11420 exp, fndecl);
11421 expand_builtin_trap ();
11422 return const0_rtx;
11423 }
11424
11425 object_size_type = tree_to_shwi (ost);
11426
11427 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11428 }
11429
11430 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11431 FCODE is the BUILT_IN_* to use.
11432 Return NULL_RTX if we failed; the caller should emit a normal call,
11433 otherwise try to get the result in TARGET, if convenient (and in
11434 mode MODE if that's convenient). */
11435
11436 static rtx
11437 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11438 enum built_in_function fcode)
11439 {
11440 tree dest, src, len, size;
11441
11442 if (!validate_arglist (exp,
11443 POINTER_TYPE,
11444 fcode == BUILT_IN_MEMSET_CHK
11445 ? INTEGER_TYPE : POINTER_TYPE,
11446 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11447 return NULL_RTX;
11448
11449 dest = CALL_EXPR_ARG (exp, 0);
11450 src = CALL_EXPR_ARG (exp, 1);
11451 len = CALL_EXPR_ARG (exp, 2);
11452 size = CALL_EXPR_ARG (exp, 3);
11453
11454 if (! tree_fits_uhwi_p (size))
11455 return NULL_RTX;
11456
11457 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11458 {
11459 tree fn;
11460
11461 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11462 {
11463 warning_at (tree_nonartificial_location (exp),
11464 0, "%Kcall to %D will always overflow destination buffer",
11465 exp, get_callee_fndecl (exp));
11466 return NULL_RTX;
11467 }
11468
11469 fn = NULL_TREE;
11470 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11471 mem{cpy,pcpy,move,set} is available. */
11472 switch (fcode)
11473 {
11474 case BUILT_IN_MEMCPY_CHK:
11475 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11476 break;
11477 case BUILT_IN_MEMPCPY_CHK:
11478 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11479 break;
11480 case BUILT_IN_MEMMOVE_CHK:
11481 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11482 break;
11483 case BUILT_IN_MEMSET_CHK:
11484 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11485 break;
11486 default:
11487 break;
11488 }
11489
11490 if (! fn)
11491 return NULL_RTX;
11492
11493 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11494 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11495 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11496 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11497 }
11498 else if (fcode == BUILT_IN_MEMSET_CHK)
11499 return NULL_RTX;
11500 else
11501 {
11502 unsigned int dest_align = get_pointer_alignment (dest);
11503
11504 /* If DEST is not a pointer type, call the normal function. */
11505 if (dest_align == 0)
11506 return NULL_RTX;
11507
11508 /* If SRC and DEST are the same (and not volatile), do nothing. */
11509 if (operand_equal_p (src, dest, 0))
11510 {
11511 tree expr;
11512
11513 if (fcode != BUILT_IN_MEMPCPY_CHK)
11514 {
11515 /* Evaluate and ignore LEN in case it has side-effects. */
11516 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11517 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11518 }
11519
11520 expr = fold_build_pointer_plus (dest, len);
11521 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11522 }
11523
11524 /* __memmove_chk special case. */
11525 if (fcode == BUILT_IN_MEMMOVE_CHK)
11526 {
11527 unsigned int src_align = get_pointer_alignment (src);
11528
11529 if (src_align == 0)
11530 return NULL_RTX;
11531
11532 /* If src is categorized for a readonly section we can use
11533 normal __memcpy_chk. */
11534 if (readonly_data_expr (src))
11535 {
11536 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11537 if (!fn)
11538 return NULL_RTX;
11539 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11540 dest, src, len, size);
11541 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11542 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11543 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11544 }
11545 }
11546 return NULL_RTX;
11547 }
11548 }
11549
11550 /* Emit warning if a buffer overflow is detected at compile time. */
11551
11552 static void
11553 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11554 {
11555 int is_strlen = 0;
11556 tree len, size;
11557 location_t loc = tree_nonartificial_location (exp);
11558
11559 switch (fcode)
11560 {
11561 case BUILT_IN_STRCPY_CHK:
11562 case BUILT_IN_STPCPY_CHK:
11563 /* For __strcat_chk the warning will be emitted only if overflowing
11564 by at least strlen (dest) + 1 bytes. */
11565 case BUILT_IN_STRCAT_CHK:
11566 len = CALL_EXPR_ARG (exp, 1);
11567 size = CALL_EXPR_ARG (exp, 2);
11568 is_strlen = 1;
11569 break;
11570 case BUILT_IN_STRNCAT_CHK:
11571 case BUILT_IN_STRNCPY_CHK:
11572 case BUILT_IN_STPNCPY_CHK:
11573 len = CALL_EXPR_ARG (exp, 2);
11574 size = CALL_EXPR_ARG (exp, 3);
11575 break;
11576 case BUILT_IN_SNPRINTF_CHK:
11577 case BUILT_IN_VSNPRINTF_CHK:
11578 len = CALL_EXPR_ARG (exp, 1);
11579 size = CALL_EXPR_ARG (exp, 3);
11580 break;
11581 default:
11582 gcc_unreachable ();
11583 }
11584
11585 if (!len || !size)
11586 return;
11587
11588 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11589 return;
11590
11591 if (is_strlen)
11592 {
11593 len = c_strlen (len, 1);
11594 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11595 return;
11596 }
11597 else if (fcode == BUILT_IN_STRNCAT_CHK)
11598 {
11599 tree src = CALL_EXPR_ARG (exp, 1);
11600 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11601 return;
11602 src = c_strlen (src, 1);
11603 if (! src || ! tree_fits_uhwi_p (src))
11604 {
11605 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11606 exp, get_callee_fndecl (exp));
11607 return;
11608 }
11609 else if (tree_int_cst_lt (src, size))
11610 return;
11611 }
11612 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11613 return;
11614
11615 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11616 exp, get_callee_fndecl (exp));
11617 }
11618
11619 /* Emit warning if a buffer overflow is detected at compile time
11620 in __sprintf_chk/__vsprintf_chk calls. */
11621
11622 static void
11623 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11624 {
11625 tree size, len, fmt;
11626 const char *fmt_str;
11627 int nargs = call_expr_nargs (exp);
11628
11629 /* Verify the required arguments in the original call. */
11630
11631 if (nargs < 4)
11632 return;
11633 size = CALL_EXPR_ARG (exp, 2);
11634 fmt = CALL_EXPR_ARG (exp, 3);
11635
11636 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11637 return;
11638
11639 /* Check whether the format is a literal string constant. */
11640 fmt_str = c_getstr (fmt);
11641 if (fmt_str == NULL)
11642 return;
11643
11644 if (!init_target_chars ())
11645 return;
11646
11647 /* If the format doesn't contain % args or %%, we know its size. */
11648 if (strchr (fmt_str, target_percent) == 0)
11649 len = build_int_cstu (size_type_node, strlen (fmt_str));
11650 /* If the format is "%s" and first ... argument is a string literal,
11651 we know it too. */
11652 else if (fcode == BUILT_IN_SPRINTF_CHK
11653 && strcmp (fmt_str, target_percent_s) == 0)
11654 {
11655 tree arg;
11656
11657 if (nargs < 5)
11658 return;
11659 arg = CALL_EXPR_ARG (exp, 4);
11660 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11661 return;
11662
11663 len = c_strlen (arg, 1);
11664 if (!len || ! tree_fits_uhwi_p (len))
11665 return;
11666 }
11667 else
11668 return;
11669
11670 if (! tree_int_cst_lt (len, size))
11671 warning_at (tree_nonartificial_location (exp),
11672 0, "%Kcall to %D will always overflow destination buffer",
11673 exp, get_callee_fndecl (exp));
11674 }
11675
11676 /* Emit warning if a free is called with address of a variable. */
11677
11678 static void
11679 maybe_emit_free_warning (tree exp)
11680 {
11681 tree arg = CALL_EXPR_ARG (exp, 0);
11682
11683 STRIP_NOPS (arg);
11684 if (TREE_CODE (arg) != ADDR_EXPR)
11685 return;
11686
11687 arg = get_base_address (TREE_OPERAND (arg, 0));
11688 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11689 return;
11690
11691 if (SSA_VAR_P (arg))
11692 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11693 "%Kattempt to free a non-heap object %qD", exp, arg);
11694 else
11695 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11696 "%Kattempt to free a non-heap object", exp);
11697 }
11698
11699 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11700 if possible. */
11701
11702 static tree
11703 fold_builtin_object_size (tree ptr, tree ost)
11704 {
11705 unsigned HOST_WIDE_INT bytes;
11706 int object_size_type;
11707
11708 if (!validate_arg (ptr, POINTER_TYPE)
11709 || !validate_arg (ost, INTEGER_TYPE))
11710 return NULL_TREE;
11711
11712 STRIP_NOPS (ost);
11713
11714 if (TREE_CODE (ost) != INTEGER_CST
11715 || tree_int_cst_sgn (ost) < 0
11716 || compare_tree_int (ost, 3) > 0)
11717 return NULL_TREE;
11718
11719 object_size_type = tree_to_shwi (ost);
11720
11721 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11722 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11723 and (size_t) 0 for types 2 and 3. */
11724 if (TREE_SIDE_EFFECTS (ptr))
11725 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11726
11727 if (TREE_CODE (ptr) == ADDR_EXPR)
11728 {
11729 bytes = compute_builtin_object_size (ptr, object_size_type);
11730 if (wi::fits_to_tree_p (bytes, size_type_node))
11731 return build_int_cstu (size_type_node, bytes);
11732 }
11733 else if (TREE_CODE (ptr) == SSA_NAME)
11734 {
11735 /* If object size is not known yet, delay folding until
11736 later. Maybe subsequent passes will help determining
11737 it. */
11738 bytes = compute_builtin_object_size (ptr, object_size_type);
11739 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11740 && wi::fits_to_tree_p (bytes, size_type_node))
11741 return build_int_cstu (size_type_node, bytes);
11742 }
11743
11744 return NULL_TREE;
11745 }
11746
11747 /* Builtins with folding operations that operate on "..." arguments
11748 need special handling; we need to store the arguments in a convenient
11749 data structure before attempting any folding. Fortunately there are
11750 only a few builtins that fall into this category. FNDECL is the
11751 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11752 result of the function call is ignored. */
11753
11754 static tree
11755 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11756 bool ignore ATTRIBUTE_UNUSED)
11757 {
11758 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11759 tree ret = NULL_TREE;
11760
11761 switch (fcode)
11762 {
11763 case BUILT_IN_FPCLASSIFY:
11764 ret = fold_builtin_fpclassify (loc, exp);
11765 break;
11766
11767 default:
11768 break;
11769 }
11770 if (ret)
11771 {
11772 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11773 SET_EXPR_LOCATION (ret, loc);
11774 TREE_NO_WARNING (ret) = 1;
11775 return ret;
11776 }
11777 return NULL_TREE;
11778 }
11779
11780 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11781 FMT and ARG are the arguments to the call; we don't fold cases with
11782 more than 2 arguments, and ARG may be null if this is a 1-argument case.
11783
11784 Return NULL_TREE if no simplification was possible, otherwise return the
11785 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11786 code of the function to be simplified. */
11787
11788 static tree
11789 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
11790 tree arg, bool ignore,
11791 enum built_in_function fcode)
11792 {
11793 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
11794 const char *fmt_str = NULL;
11795
11796 /* If the return value is used, don't do the transformation. */
11797 if (! ignore)
11798 return NULL_TREE;
11799
11800 /* Verify the required arguments in the original call. */
11801 if (!validate_arg (fmt, POINTER_TYPE))
11802 return NULL_TREE;
11803
11804 /* Check whether the format is a literal string constant. */
11805 fmt_str = c_getstr (fmt);
11806 if (fmt_str == NULL)
11807 return NULL_TREE;
11808
11809 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11810 {
11811 /* If we're using an unlocked function, assume the other
11812 unlocked functions exist explicitly. */
11813 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
11814 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
11815 }
11816 else
11817 {
11818 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
11819 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
11820 }
11821
11822 if (!init_target_chars ())
11823 return NULL_TREE;
11824
11825 if (strcmp (fmt_str, target_percent_s) == 0
11826 || strchr (fmt_str, target_percent) == NULL)
11827 {
11828 const char *str;
11829
11830 if (strcmp (fmt_str, target_percent_s) == 0)
11831 {
11832 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11833 return NULL_TREE;
11834
11835 if (!arg || !validate_arg (arg, POINTER_TYPE))
11836 return NULL_TREE;
11837
11838 str = c_getstr (arg);
11839 if (str == NULL)
11840 return NULL_TREE;
11841 }
11842 else
11843 {
11844 /* The format specifier doesn't contain any '%' characters. */
11845 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11846 && arg)
11847 return NULL_TREE;
11848 str = fmt_str;
11849 }
11850
11851 /* If the string was "", printf does nothing. */
11852 if (str[0] == '\0')
11853 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11854
11855 /* If the string has length of 1, call putchar. */
11856 if (str[1] == '\0')
11857 {
11858 /* Given printf("c"), (where c is any one character,)
11859 convert "c"[0] to an int and pass that to the replacement
11860 function. */
11861 newarg = build_int_cst (integer_type_node, str[0]);
11862 if (fn_putchar)
11863 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
11864 }
11865 else
11866 {
11867 /* If the string was "string\n", call puts("string"). */
11868 size_t len = strlen (str);
11869 if ((unsigned char)str[len - 1] == target_newline
11870 && (size_t) (int) len == len
11871 && (int) len > 0)
11872 {
11873 char *newstr;
11874 tree offset_node, string_cst;
11875
11876 /* Create a NUL-terminated string that's one char shorter
11877 than the original, stripping off the trailing '\n'. */
11878 newarg = build_string_literal (len, str);
11879 string_cst = string_constant (newarg, &offset_node);
11880 gcc_checking_assert (string_cst
11881 && (TREE_STRING_LENGTH (string_cst)
11882 == (int) len)
11883 && integer_zerop (offset_node)
11884 && (unsigned char)
11885 TREE_STRING_POINTER (string_cst)[len - 1]
11886 == target_newline);
11887 /* build_string_literal creates a new STRING_CST,
11888 modify it in place to avoid double copying. */
11889 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
11890 newstr[len - 1] = '\0';
11891 if (fn_puts)
11892 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
11893 }
11894 else
11895 /* We'd like to arrange to call fputs(string,stdout) here,
11896 but we need stdout and don't have a way to get it yet. */
11897 return NULL_TREE;
11898 }
11899 }
11900
11901 /* The other optimizations can be done only on the non-va_list variants. */
11902 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11903 return NULL_TREE;
11904
11905 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11906 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11907 {
11908 if (!arg || !validate_arg (arg, POINTER_TYPE))
11909 return NULL_TREE;
11910 if (fn_puts)
11911 call = build_call_expr_loc (loc, fn_puts, 1, arg);
11912 }
11913
11914 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11915 else if (strcmp (fmt_str, target_percent_c) == 0)
11916 {
11917 if (!arg || !validate_arg (arg, INTEGER_TYPE))
11918 return NULL_TREE;
11919 if (fn_putchar)
11920 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
11921 }
11922
11923 if (!call)
11924 return NULL_TREE;
11925
11926 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
11927 }
11928
11929 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11930 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
11931 more than 3 arguments, and ARG may be null in the 2-argument case.
11932
11933 Return NULL_TREE if no simplification was possible, otherwise return the
11934 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11935 code of the function to be simplified. */
11936
11937 static tree
11938 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
11939 tree fmt, tree arg, bool ignore,
11940 enum built_in_function fcode)
11941 {
11942 tree fn_fputc, fn_fputs, call = NULL_TREE;
11943 const char *fmt_str = NULL;
11944
11945 /* If the return value is used, don't do the transformation. */
11946 if (! ignore)
11947 return NULL_TREE;
11948
11949 /* Verify the required arguments in the original call. */
11950 if (!validate_arg (fp, POINTER_TYPE))
11951 return NULL_TREE;
11952 if (!validate_arg (fmt, POINTER_TYPE))
11953 return NULL_TREE;
11954
11955 /* Check whether the format is a literal string constant. */
11956 fmt_str = c_getstr (fmt);
11957 if (fmt_str == NULL)
11958 return NULL_TREE;
11959
11960 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11961 {
11962 /* If we're using an unlocked function, assume the other
11963 unlocked functions exist explicitly. */
11964 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
11965 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
11966 }
11967 else
11968 {
11969 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
11970 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
11971 }
11972
11973 if (!init_target_chars ())
11974 return NULL_TREE;
11975
11976 /* If the format doesn't contain % args or %%, use strcpy. */
11977 if (strchr (fmt_str, target_percent) == NULL)
11978 {
11979 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11980 && arg)
11981 return NULL_TREE;
11982
11983 /* If the format specifier was "", fprintf does nothing. */
11984 if (fmt_str[0] == '\0')
11985 {
11986 /* If FP has side-effects, just wait until gimplification is
11987 done. */
11988 if (TREE_SIDE_EFFECTS (fp))
11989 return NULL_TREE;
11990
11991 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11992 }
11993
11994 /* When "string" doesn't contain %, replace all cases of
11995 fprintf (fp, string) with fputs (string, fp). The fputs
11996 builtin will take care of special cases like length == 1. */
11997 if (fn_fputs)
11998 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
11999 }
12000
12001 /* The other optimizations can be done only on the non-va_list variants. */
12002 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12003 return NULL_TREE;
12004
12005 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12006 else if (strcmp (fmt_str, target_percent_s) == 0)
12007 {
12008 if (!arg || !validate_arg (arg, POINTER_TYPE))
12009 return NULL_TREE;
12010 if (fn_fputs)
12011 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
12012 }
12013
12014 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12015 else if (strcmp (fmt_str, target_percent_c) == 0)
12016 {
12017 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12018 return NULL_TREE;
12019 if (fn_fputc)
12020 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
12021 }
12022
12023 if (!call)
12024 return NULL_TREE;
12025 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12026 }
12027
12028 /* Initialize format string characters in the target charset. */
12029
12030 bool
12031 init_target_chars (void)
12032 {
12033 static bool init;
12034 if (!init)
12035 {
12036 target_newline = lang_hooks.to_target_charset ('\n');
12037 target_percent = lang_hooks.to_target_charset ('%');
12038 target_c = lang_hooks.to_target_charset ('c');
12039 target_s = lang_hooks.to_target_charset ('s');
12040 if (target_newline == 0 || target_percent == 0 || target_c == 0
12041 || target_s == 0)
12042 return false;
12043
12044 target_percent_c[0] = target_percent;
12045 target_percent_c[1] = target_c;
12046 target_percent_c[2] = '\0';
12047
12048 target_percent_s[0] = target_percent;
12049 target_percent_s[1] = target_s;
12050 target_percent_s[2] = '\0';
12051
12052 target_percent_s_newline[0] = target_percent;
12053 target_percent_s_newline[1] = target_s;
12054 target_percent_s_newline[2] = target_newline;
12055 target_percent_s_newline[3] = '\0';
12056
12057 init = true;
12058 }
12059 return true;
12060 }
12061
12062 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12063 and no overflow/underflow occurred. INEXACT is true if M was not
12064 exactly calculated. TYPE is the tree type for the result. This
12065 function assumes that you cleared the MPFR flags and then
12066 calculated M to see if anything subsequently set a flag prior to
12067 entering this function. Return NULL_TREE if any checks fail. */
12068
12069 static tree
12070 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12071 {
12072 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12073 overflow/underflow occurred. If -frounding-math, proceed iff the
12074 result of calling FUNC was exact. */
12075 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12076 && (!flag_rounding_math || !inexact))
12077 {
12078 REAL_VALUE_TYPE rr;
12079
12080 real_from_mpfr (&rr, m, type, GMP_RNDN);
12081 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12082 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12083 but the mpft_t is not, then we underflowed in the
12084 conversion. */
12085 if (real_isfinite (&rr)
12086 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12087 {
12088 REAL_VALUE_TYPE rmode;
12089
12090 real_convert (&rmode, TYPE_MODE (type), &rr);
12091 /* Proceed iff the specified mode can hold the value. */
12092 if (real_identical (&rmode, &rr))
12093 return build_real (type, rmode);
12094 }
12095 }
12096 return NULL_TREE;
12097 }
12098
12099 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12100 number and no overflow/underflow occurred. INEXACT is true if M
12101 was not exactly calculated. TYPE is the tree type for the result.
12102 This function assumes that you cleared the MPFR flags and then
12103 calculated M to see if anything subsequently set a flag prior to
12104 entering this function. Return NULL_TREE if any checks fail, if
12105 FORCE_CONVERT is true, then bypass the checks. */
12106
12107 static tree
12108 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
12109 {
12110 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12111 overflow/underflow occurred. If -frounding-math, proceed iff the
12112 result of calling FUNC was exact. */
12113 if (force_convert
12114 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
12115 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12116 && (!flag_rounding_math || !inexact)))
12117 {
12118 REAL_VALUE_TYPE re, im;
12119
12120 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
12121 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
12122 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12123 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12124 but the mpft_t is not, then we underflowed in the
12125 conversion. */
12126 if (force_convert
12127 || (real_isfinite (&re) && real_isfinite (&im)
12128 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
12129 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
12130 {
12131 REAL_VALUE_TYPE re_mode, im_mode;
12132
12133 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
12134 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
12135 /* Proceed iff the specified mode can hold the value. */
12136 if (force_convert
12137 || (real_identical (&re_mode, &re)
12138 && real_identical (&im_mode, &im)))
12139 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
12140 build_real (TREE_TYPE (type), im_mode));
12141 }
12142 }
12143 return NULL_TREE;
12144 }
12145
12146 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12147 FUNC on it and return the resulting value as a tree with type TYPE.
12148 If MIN and/or MAX are not NULL, then the supplied ARG must be
12149 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12150 acceptable values, otherwise they are not. The mpfr precision is
12151 set to the precision of TYPE. We assume that function FUNC returns
12152 zero if the result could be calculated exactly within the requested
12153 precision. */
12154
12155 static tree
12156 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
12157 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
12158 bool inclusive)
12159 {
12160 tree result = NULL_TREE;
12161
12162 STRIP_NOPS (arg);
12163
12164 /* To proceed, MPFR must exactly represent the target floating point
12165 format, which only happens when the target base equals two. */
12166 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12167 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12168 {
12169 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12170
12171 if (real_isfinite (ra)
12172 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12173 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12174 {
12175 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12176 const int prec = fmt->p;
12177 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12178 int inexact;
12179 mpfr_t m;
12180
12181 mpfr_init2 (m, prec);
12182 mpfr_from_real (m, ra, GMP_RNDN);
12183 mpfr_clear_flags ();
12184 inexact = func (m, m, rnd);
12185 result = do_mpfr_ckconv (m, type, inexact);
12186 mpfr_clear (m);
12187 }
12188 }
12189
12190 return result;
12191 }
12192
12193 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12194 FUNC on it and return the resulting value as a tree with type TYPE.
12195 The mpfr precision is set to the precision of TYPE. We assume that
12196 function FUNC returns zero if the result could be calculated
12197 exactly within the requested precision. */
12198
12199 static tree
12200 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12201 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12202 {
12203 tree result = NULL_TREE;
12204
12205 STRIP_NOPS (arg1);
12206 STRIP_NOPS (arg2);
12207
12208 /* To proceed, MPFR must exactly represent the target floating point
12209 format, which only happens when the target base equals two. */
12210 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12211 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12212 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12213 {
12214 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12215 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12216
12217 if (real_isfinite (ra1) && real_isfinite (ra2))
12218 {
12219 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12220 const int prec = fmt->p;
12221 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12222 int inexact;
12223 mpfr_t m1, m2;
12224
12225 mpfr_inits2 (prec, m1, m2, NULL);
12226 mpfr_from_real (m1, ra1, GMP_RNDN);
12227 mpfr_from_real (m2, ra2, GMP_RNDN);
12228 mpfr_clear_flags ();
12229 inexact = func (m1, m1, m2, rnd);
12230 result = do_mpfr_ckconv (m1, type, inexact);
12231 mpfr_clears (m1, m2, NULL);
12232 }
12233 }
12234
12235 return result;
12236 }
12237
12238 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12239 FUNC on it and return the resulting value as a tree with type TYPE.
12240 The mpfr precision is set to the precision of TYPE. We assume that
12241 function FUNC returns zero if the result could be calculated
12242 exactly within the requested precision. */
12243
12244 static tree
12245 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12246 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12247 {
12248 tree result = NULL_TREE;
12249
12250 STRIP_NOPS (arg1);
12251 STRIP_NOPS (arg2);
12252 STRIP_NOPS (arg3);
12253
12254 /* To proceed, MPFR must exactly represent the target floating point
12255 format, which only happens when the target base equals two. */
12256 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12257 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12258 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12259 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12260 {
12261 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12262 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12263 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12264
12265 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12266 {
12267 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12268 const int prec = fmt->p;
12269 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12270 int inexact;
12271 mpfr_t m1, m2, m3;
12272
12273 mpfr_inits2 (prec, m1, m2, m3, NULL);
12274 mpfr_from_real (m1, ra1, GMP_RNDN);
12275 mpfr_from_real (m2, ra2, GMP_RNDN);
12276 mpfr_from_real (m3, ra3, GMP_RNDN);
12277 mpfr_clear_flags ();
12278 inexact = func (m1, m1, m2, m3, rnd);
12279 result = do_mpfr_ckconv (m1, type, inexact);
12280 mpfr_clears (m1, m2, m3, NULL);
12281 }
12282 }
12283
12284 return result;
12285 }
12286
12287 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12288 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12289 If ARG_SINP and ARG_COSP are NULL then the result is returned
12290 as a complex value.
12291 The type is taken from the type of ARG and is used for setting the
12292 precision of the calculation and results. */
12293
12294 static tree
12295 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
12296 {
12297 tree const type = TREE_TYPE (arg);
12298 tree result = NULL_TREE;
12299
12300 STRIP_NOPS (arg);
12301
12302 /* To proceed, MPFR must exactly represent the target floating point
12303 format, which only happens when the target base equals two. */
12304 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12305 && TREE_CODE (arg) == REAL_CST
12306 && !TREE_OVERFLOW (arg))
12307 {
12308 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12309
12310 if (real_isfinite (ra))
12311 {
12312 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12313 const int prec = fmt->p;
12314 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12315 tree result_s, result_c;
12316 int inexact;
12317 mpfr_t m, ms, mc;
12318
12319 mpfr_inits2 (prec, m, ms, mc, NULL);
12320 mpfr_from_real (m, ra, GMP_RNDN);
12321 mpfr_clear_flags ();
12322 inexact = mpfr_sin_cos (ms, mc, m, rnd);
12323 result_s = do_mpfr_ckconv (ms, type, inexact);
12324 result_c = do_mpfr_ckconv (mc, type, inexact);
12325 mpfr_clears (m, ms, mc, NULL);
12326 if (result_s && result_c)
12327 {
12328 /* If we are to return in a complex value do so. */
12329 if (!arg_sinp && !arg_cosp)
12330 return build_complex (build_complex_type (type),
12331 result_c, result_s);
12332
12333 /* Dereference the sin/cos pointer arguments. */
12334 arg_sinp = build_fold_indirect_ref (arg_sinp);
12335 arg_cosp = build_fold_indirect_ref (arg_cosp);
12336 /* Proceed if valid pointer type were passed in. */
12337 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
12338 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
12339 {
12340 /* Set the values. */
12341 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12342 result_s);
12343 TREE_SIDE_EFFECTS (result_s) = 1;
12344 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12345 result_c);
12346 TREE_SIDE_EFFECTS (result_c) = 1;
12347 /* Combine the assignments into a compound expr. */
12348 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12349 result_s, result_c));
12350 }
12351 }
12352 }
12353 }
12354 return result;
12355 }
12356
12357 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12358 two-argument mpfr order N Bessel function FUNC on them and return
12359 the resulting value as a tree with type TYPE. The mpfr precision
12360 is set to the precision of TYPE. We assume that function FUNC
12361 returns zero if the result could be calculated exactly within the
12362 requested precision. */
12363 static tree
12364 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12365 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12366 const REAL_VALUE_TYPE *min, bool inclusive)
12367 {
12368 tree result = NULL_TREE;
12369
12370 STRIP_NOPS (arg1);
12371 STRIP_NOPS (arg2);
12372
12373 /* To proceed, MPFR must exactly represent the target floating point
12374 format, which only happens when the target base equals two. */
12375 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12376 && tree_fits_shwi_p (arg1)
12377 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12378 {
12379 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12380 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12381
12382 if (n == (long)n
12383 && real_isfinite (ra)
12384 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12385 {
12386 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12387 const int prec = fmt->p;
12388 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12389 int inexact;
12390 mpfr_t m;
12391
12392 mpfr_init2 (m, prec);
12393 mpfr_from_real (m, ra, GMP_RNDN);
12394 mpfr_clear_flags ();
12395 inexact = func (m, n, m, rnd);
12396 result = do_mpfr_ckconv (m, type, inexact);
12397 mpfr_clear (m);
12398 }
12399 }
12400
12401 return result;
12402 }
12403
12404 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12405 the pointer *(ARG_QUO) and return the result. The type is taken
12406 from the type of ARG0 and is used for setting the precision of the
12407 calculation and results. */
12408
12409 static tree
12410 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12411 {
12412 tree const type = TREE_TYPE (arg0);
12413 tree result = NULL_TREE;
12414
12415 STRIP_NOPS (arg0);
12416 STRIP_NOPS (arg1);
12417
12418 /* To proceed, MPFR must exactly represent the target floating point
12419 format, which only happens when the target base equals two. */
12420 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12421 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12422 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12423 {
12424 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12425 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12426
12427 if (real_isfinite (ra0) && real_isfinite (ra1))
12428 {
12429 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12430 const int prec = fmt->p;
12431 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12432 tree result_rem;
12433 long integer_quo;
12434 mpfr_t m0, m1;
12435
12436 mpfr_inits2 (prec, m0, m1, NULL);
12437 mpfr_from_real (m0, ra0, GMP_RNDN);
12438 mpfr_from_real (m1, ra1, GMP_RNDN);
12439 mpfr_clear_flags ();
12440 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12441 /* Remquo is independent of the rounding mode, so pass
12442 inexact=0 to do_mpfr_ckconv(). */
12443 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12444 mpfr_clears (m0, m1, NULL);
12445 if (result_rem)
12446 {
12447 /* MPFR calculates quo in the host's long so it may
12448 return more bits in quo than the target int can hold
12449 if sizeof(host long) > sizeof(target int). This can
12450 happen even for native compilers in LP64 mode. In
12451 these cases, modulo the quo value with the largest
12452 number that the target int can hold while leaving one
12453 bit for the sign. */
12454 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12455 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12456
12457 /* Dereference the quo pointer argument. */
12458 arg_quo = build_fold_indirect_ref (arg_quo);
12459 /* Proceed iff a valid pointer type was passed in. */
12460 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12461 {
12462 /* Set the value. */
12463 tree result_quo
12464 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12465 build_int_cst (TREE_TYPE (arg_quo),
12466 integer_quo));
12467 TREE_SIDE_EFFECTS (result_quo) = 1;
12468 /* Combine the quo assignment with the rem. */
12469 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12470 result_quo, result_rem));
12471 }
12472 }
12473 }
12474 }
12475 return result;
12476 }
12477
12478 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12479 resulting value as a tree with type TYPE. The mpfr precision is
12480 set to the precision of TYPE. We assume that this mpfr function
12481 returns zero if the result could be calculated exactly within the
12482 requested precision. In addition, the integer pointer represented
12483 by ARG_SG will be dereferenced and set to the appropriate signgam
12484 (-1,1) value. */
12485
12486 static tree
12487 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12488 {
12489 tree result = NULL_TREE;
12490
12491 STRIP_NOPS (arg);
12492
12493 /* To proceed, MPFR must exactly represent the target floating point
12494 format, which only happens when the target base equals two. Also
12495 verify ARG is a constant and that ARG_SG is an int pointer. */
12496 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12497 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12498 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12499 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12500 {
12501 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12502
12503 /* In addition to NaN and Inf, the argument cannot be zero or a
12504 negative integer. */
12505 if (real_isfinite (ra)
12506 && ra->cl != rvc_zero
12507 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12508 {
12509 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12510 const int prec = fmt->p;
12511 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12512 int inexact, sg;
12513 mpfr_t m;
12514 tree result_lg;
12515
12516 mpfr_init2 (m, prec);
12517 mpfr_from_real (m, ra, GMP_RNDN);
12518 mpfr_clear_flags ();
12519 inexact = mpfr_lgamma (m, &sg, m, rnd);
12520 result_lg = do_mpfr_ckconv (m, type, inexact);
12521 mpfr_clear (m);
12522 if (result_lg)
12523 {
12524 tree result_sg;
12525
12526 /* Dereference the arg_sg pointer argument. */
12527 arg_sg = build_fold_indirect_ref (arg_sg);
12528 /* Assign the signgam value into *arg_sg. */
12529 result_sg = fold_build2 (MODIFY_EXPR,
12530 TREE_TYPE (arg_sg), arg_sg,
12531 build_int_cst (TREE_TYPE (arg_sg), sg));
12532 TREE_SIDE_EFFECTS (result_sg) = 1;
12533 /* Combine the signgam assignment with the lgamma result. */
12534 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12535 result_sg, result_lg));
12536 }
12537 }
12538 }
12539
12540 return result;
12541 }
12542
12543 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12544 function FUNC on it and return the resulting value as a tree with
12545 type TYPE. The mpfr precision is set to the precision of TYPE. We
12546 assume that function FUNC returns zero if the result could be
12547 calculated exactly within the requested precision. */
12548
12549 static tree
12550 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12551 {
12552 tree result = NULL_TREE;
12553
12554 STRIP_NOPS (arg);
12555
12556 /* To proceed, MPFR must exactly represent the target floating point
12557 format, which only happens when the target base equals two. */
12558 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12559 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12560 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12561 {
12562 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12563 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12564
12565 if (real_isfinite (re) && real_isfinite (im))
12566 {
12567 const struct real_format *const fmt =
12568 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12569 const int prec = fmt->p;
12570 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12571 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12572 int inexact;
12573 mpc_t m;
12574
12575 mpc_init2 (m, prec);
12576 mpfr_from_real (mpc_realref (m), re, rnd);
12577 mpfr_from_real (mpc_imagref (m), im, rnd);
12578 mpfr_clear_flags ();
12579 inexact = func (m, m, crnd);
12580 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12581 mpc_clear (m);
12582 }
12583 }
12584
12585 return result;
12586 }
12587
12588 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12589 mpc function FUNC on it and return the resulting value as a tree
12590 with type TYPE. The mpfr precision is set to the precision of
12591 TYPE. We assume that function FUNC returns zero if the result
12592 could be calculated exactly within the requested precision. If
12593 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12594 in the arguments and/or results. */
12595
12596 tree
12597 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12598 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12599 {
12600 tree result = NULL_TREE;
12601
12602 STRIP_NOPS (arg0);
12603 STRIP_NOPS (arg1);
12604
12605 /* To proceed, MPFR must exactly represent the target floating point
12606 format, which only happens when the target base equals two. */
12607 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12608 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12609 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12610 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12611 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12612 {
12613 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12614 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12615 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12616 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12617
12618 if (do_nonfinite
12619 || (real_isfinite (re0) && real_isfinite (im0)
12620 && real_isfinite (re1) && real_isfinite (im1)))
12621 {
12622 const struct real_format *const fmt =
12623 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12624 const int prec = fmt->p;
12625 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12626 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12627 int inexact;
12628 mpc_t m0, m1;
12629
12630 mpc_init2 (m0, prec);
12631 mpc_init2 (m1, prec);
12632 mpfr_from_real (mpc_realref (m0), re0, rnd);
12633 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12634 mpfr_from_real (mpc_realref (m1), re1, rnd);
12635 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12636 mpfr_clear_flags ();
12637 inexact = func (m0, m0, m1, crnd);
12638 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12639 mpc_clear (m0);
12640 mpc_clear (m1);
12641 }
12642 }
12643
12644 return result;
12645 }
12646
12647 /* A wrapper function for builtin folding that prevents warnings for
12648 "statement without effect" and the like, caused by removing the
12649 call node earlier than the warning is generated. */
12650
12651 tree
12652 fold_call_stmt (gimple stmt, bool ignore)
12653 {
12654 tree ret = NULL_TREE;
12655 tree fndecl = gimple_call_fndecl (stmt);
12656 location_t loc = gimple_location (stmt);
12657 if (fndecl
12658 && TREE_CODE (fndecl) == FUNCTION_DECL
12659 && DECL_BUILT_IN (fndecl)
12660 && !gimple_call_va_arg_pack_p (stmt))
12661 {
12662 int nargs = gimple_call_num_args (stmt);
12663 tree *args = (nargs > 0
12664 ? gimple_call_arg_ptr (stmt, 0)
12665 : &error_mark_node);
12666
12667 if (avoid_folding_inline_builtin (fndecl))
12668 return NULL_TREE;
12669 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12670 {
12671 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12672 }
12673 else
12674 {
12675 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
12676 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12677 if (ret)
12678 {
12679 /* Propagate location information from original call to
12680 expansion of builtin. Otherwise things like
12681 maybe_emit_chk_warning, that operate on the expansion
12682 of a builtin, will use the wrong location information. */
12683 if (gimple_has_location (stmt))
12684 {
12685 tree realret = ret;
12686 if (TREE_CODE (ret) == NOP_EXPR)
12687 realret = TREE_OPERAND (ret, 0);
12688 if (CAN_HAVE_LOCATION_P (realret)
12689 && !EXPR_HAS_LOCATION (realret))
12690 SET_EXPR_LOCATION (realret, loc);
12691 return realret;
12692 }
12693 return ret;
12694 }
12695 }
12696 }
12697 return NULL_TREE;
12698 }
12699
12700 /* Look up the function in builtin_decl that corresponds to DECL
12701 and set ASMSPEC as its user assembler name. DECL must be a
12702 function decl that declares a builtin. */
12703
12704 void
12705 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12706 {
12707 tree builtin;
12708 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12709 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12710 && asmspec != 0);
12711
12712 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12713 set_user_assembler_name (builtin, asmspec);
12714 switch (DECL_FUNCTION_CODE (decl))
12715 {
12716 case BUILT_IN_MEMCPY:
12717 init_block_move_fn (asmspec);
12718 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12719 break;
12720 case BUILT_IN_MEMSET:
12721 init_block_clear_fn (asmspec);
12722 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12723 break;
12724 case BUILT_IN_MEMMOVE:
12725 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12726 break;
12727 case BUILT_IN_MEMCMP:
12728 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12729 break;
12730 case BUILT_IN_ABORT:
12731 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12732 break;
12733 case BUILT_IN_FFS:
12734 if (INT_TYPE_SIZE < BITS_PER_WORD)
12735 {
12736 set_user_assembler_libfunc ("ffs", asmspec);
12737 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12738 MODE_INT, 0), "ffs");
12739 }
12740 break;
12741 default:
12742 break;
12743 }
12744 }
12745
12746 /* Return true if DECL is a builtin that expands to a constant or similarly
12747 simple code. */
12748 bool
12749 is_simple_builtin (tree decl)
12750 {
12751 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12752 switch (DECL_FUNCTION_CODE (decl))
12753 {
12754 /* Builtins that expand to constants. */
12755 case BUILT_IN_CONSTANT_P:
12756 case BUILT_IN_EXPECT:
12757 case BUILT_IN_OBJECT_SIZE:
12758 case BUILT_IN_UNREACHABLE:
12759 /* Simple register moves or loads from stack. */
12760 case BUILT_IN_ASSUME_ALIGNED:
12761 case BUILT_IN_RETURN_ADDRESS:
12762 case BUILT_IN_EXTRACT_RETURN_ADDR:
12763 case BUILT_IN_FROB_RETURN_ADDR:
12764 case BUILT_IN_RETURN:
12765 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12766 case BUILT_IN_FRAME_ADDRESS:
12767 case BUILT_IN_VA_END:
12768 case BUILT_IN_STACK_SAVE:
12769 case BUILT_IN_STACK_RESTORE:
12770 /* Exception state returns or moves registers around. */
12771 case BUILT_IN_EH_FILTER:
12772 case BUILT_IN_EH_POINTER:
12773 case BUILT_IN_EH_COPY_VALUES:
12774 return true;
12775
12776 default:
12777 return false;
12778 }
12779
12780 return false;
12781 }
12782
12783 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12784 most probably expanded inline into reasonably simple code. This is a
12785 superset of is_simple_builtin. */
12786 bool
12787 is_inexpensive_builtin (tree decl)
12788 {
12789 if (!decl)
12790 return false;
12791 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12792 return true;
12793 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12794 switch (DECL_FUNCTION_CODE (decl))
12795 {
12796 case BUILT_IN_ABS:
12797 case BUILT_IN_ALLOCA:
12798 case BUILT_IN_ALLOCA_WITH_ALIGN:
12799 case BUILT_IN_BSWAP16:
12800 case BUILT_IN_BSWAP32:
12801 case BUILT_IN_BSWAP64:
12802 case BUILT_IN_CLZ:
12803 case BUILT_IN_CLZIMAX:
12804 case BUILT_IN_CLZL:
12805 case BUILT_IN_CLZLL:
12806 case BUILT_IN_CTZ:
12807 case BUILT_IN_CTZIMAX:
12808 case BUILT_IN_CTZL:
12809 case BUILT_IN_CTZLL:
12810 case BUILT_IN_FFS:
12811 case BUILT_IN_FFSIMAX:
12812 case BUILT_IN_FFSL:
12813 case BUILT_IN_FFSLL:
12814 case BUILT_IN_IMAXABS:
12815 case BUILT_IN_FINITE:
12816 case BUILT_IN_FINITEF:
12817 case BUILT_IN_FINITEL:
12818 case BUILT_IN_FINITED32:
12819 case BUILT_IN_FINITED64:
12820 case BUILT_IN_FINITED128:
12821 case BUILT_IN_FPCLASSIFY:
12822 case BUILT_IN_ISFINITE:
12823 case BUILT_IN_ISINF_SIGN:
12824 case BUILT_IN_ISINF:
12825 case BUILT_IN_ISINFF:
12826 case BUILT_IN_ISINFL:
12827 case BUILT_IN_ISINFD32:
12828 case BUILT_IN_ISINFD64:
12829 case BUILT_IN_ISINFD128:
12830 case BUILT_IN_ISNAN:
12831 case BUILT_IN_ISNANF:
12832 case BUILT_IN_ISNANL:
12833 case BUILT_IN_ISNAND32:
12834 case BUILT_IN_ISNAND64:
12835 case BUILT_IN_ISNAND128:
12836 case BUILT_IN_ISNORMAL:
12837 case BUILT_IN_ISGREATER:
12838 case BUILT_IN_ISGREATEREQUAL:
12839 case BUILT_IN_ISLESS:
12840 case BUILT_IN_ISLESSEQUAL:
12841 case BUILT_IN_ISLESSGREATER:
12842 case BUILT_IN_ISUNORDERED:
12843 case BUILT_IN_VA_ARG_PACK:
12844 case BUILT_IN_VA_ARG_PACK_LEN:
12845 case BUILT_IN_VA_COPY:
12846 case BUILT_IN_TRAP:
12847 case BUILT_IN_SAVEREGS:
12848 case BUILT_IN_POPCOUNTL:
12849 case BUILT_IN_POPCOUNTLL:
12850 case BUILT_IN_POPCOUNTIMAX:
12851 case BUILT_IN_POPCOUNT:
12852 case BUILT_IN_PARITYL:
12853 case BUILT_IN_PARITYLL:
12854 case BUILT_IN_PARITYIMAX:
12855 case BUILT_IN_PARITY:
12856 case BUILT_IN_LABS:
12857 case BUILT_IN_LLABS:
12858 case BUILT_IN_PREFETCH:
12859 return true;
12860
12861 default:
12862 return is_simple_builtin (decl);
12863 }
12864
12865 return false;
12866 }