re PR tree-optimization/63747 (icf mis-compares switch gimple)
[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 a call to built-in function FNDECL with 0 arguments.
9728 IGNORE is true if the result of the function call is ignored. This
9729 function returns NULL_TREE if no simplification was possible. */
9730
9731 static tree
9732 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9733 {
9734 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9735 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9736 switch (fcode)
9737 {
9738 CASE_FLT_FN (BUILT_IN_INF):
9739 case BUILT_IN_INFD32:
9740 case BUILT_IN_INFD64:
9741 case BUILT_IN_INFD128:
9742 return fold_builtin_inf (loc, type, true);
9743
9744 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9745 return fold_builtin_inf (loc, type, false);
9746
9747 case BUILT_IN_CLASSIFY_TYPE:
9748 return fold_builtin_classify_type (NULL_TREE);
9749
9750 case BUILT_IN_UNREACHABLE:
9751 if (flag_sanitize & SANITIZE_UNREACHABLE
9752 && (current_function_decl == NULL
9753 || !lookup_attribute ("no_sanitize_undefined",
9754 DECL_ATTRIBUTES (current_function_decl))))
9755 return ubsan_instrument_unreachable (loc);
9756 break;
9757
9758 default:
9759 break;
9760 }
9761 return NULL_TREE;
9762 }
9763
9764 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9765 IGNORE is true if the result of the function call is ignored. This
9766 function returns NULL_TREE if no simplification was possible. */
9767
9768 static tree
9769 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9770 {
9771 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9772 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9773 switch (fcode)
9774 {
9775 case BUILT_IN_CONSTANT_P:
9776 {
9777 tree val = fold_builtin_constant_p (arg0);
9778
9779 /* Gimplification will pull the CALL_EXPR for the builtin out of
9780 an if condition. When not optimizing, we'll not CSE it back.
9781 To avoid link error types of regressions, return false now. */
9782 if (!val && !optimize)
9783 val = integer_zero_node;
9784
9785 return val;
9786 }
9787
9788 case BUILT_IN_CLASSIFY_TYPE:
9789 return fold_builtin_classify_type (arg0);
9790
9791 case BUILT_IN_STRLEN:
9792 return fold_builtin_strlen (loc, type, arg0);
9793
9794 CASE_FLT_FN (BUILT_IN_FABS):
9795 case BUILT_IN_FABSD32:
9796 case BUILT_IN_FABSD64:
9797 case BUILT_IN_FABSD128:
9798 return fold_builtin_fabs (loc, arg0, type);
9799
9800 case BUILT_IN_ABS:
9801 case BUILT_IN_LABS:
9802 case BUILT_IN_LLABS:
9803 case BUILT_IN_IMAXABS:
9804 return fold_builtin_abs (loc, arg0, type);
9805
9806 CASE_FLT_FN (BUILT_IN_CONJ):
9807 if (validate_arg (arg0, COMPLEX_TYPE)
9808 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9809 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9810 break;
9811
9812 CASE_FLT_FN (BUILT_IN_CREAL):
9813 if (validate_arg (arg0, COMPLEX_TYPE)
9814 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9815 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9816 break;
9817
9818 CASE_FLT_FN (BUILT_IN_CIMAG):
9819 if (validate_arg (arg0, COMPLEX_TYPE)
9820 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9821 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9822 break;
9823
9824 CASE_FLT_FN (BUILT_IN_CCOS):
9825 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9826
9827 CASE_FLT_FN (BUILT_IN_CCOSH):
9828 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9829
9830 CASE_FLT_FN (BUILT_IN_CPROJ):
9831 return fold_builtin_cproj (loc, arg0, type);
9832
9833 CASE_FLT_FN (BUILT_IN_CSIN):
9834 if (validate_arg (arg0, COMPLEX_TYPE)
9835 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9836 return do_mpc_arg1 (arg0, type, mpc_sin);
9837 break;
9838
9839 CASE_FLT_FN (BUILT_IN_CSINH):
9840 if (validate_arg (arg0, COMPLEX_TYPE)
9841 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9842 return do_mpc_arg1 (arg0, type, mpc_sinh);
9843 break;
9844
9845 CASE_FLT_FN (BUILT_IN_CTAN):
9846 if (validate_arg (arg0, COMPLEX_TYPE)
9847 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9848 return do_mpc_arg1 (arg0, type, mpc_tan);
9849 break;
9850
9851 CASE_FLT_FN (BUILT_IN_CTANH):
9852 if (validate_arg (arg0, COMPLEX_TYPE)
9853 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9854 return do_mpc_arg1 (arg0, type, mpc_tanh);
9855 break;
9856
9857 CASE_FLT_FN (BUILT_IN_CLOG):
9858 if (validate_arg (arg0, COMPLEX_TYPE)
9859 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9860 return do_mpc_arg1 (arg0, type, mpc_log);
9861 break;
9862
9863 CASE_FLT_FN (BUILT_IN_CSQRT):
9864 if (validate_arg (arg0, COMPLEX_TYPE)
9865 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9866 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9867 break;
9868
9869 CASE_FLT_FN (BUILT_IN_CASIN):
9870 if (validate_arg (arg0, COMPLEX_TYPE)
9871 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9872 return do_mpc_arg1 (arg0, type, mpc_asin);
9873 break;
9874
9875 CASE_FLT_FN (BUILT_IN_CACOS):
9876 if (validate_arg (arg0, COMPLEX_TYPE)
9877 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9878 return do_mpc_arg1 (arg0, type, mpc_acos);
9879 break;
9880
9881 CASE_FLT_FN (BUILT_IN_CATAN):
9882 if (validate_arg (arg0, COMPLEX_TYPE)
9883 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9884 return do_mpc_arg1 (arg0, type, mpc_atan);
9885 break;
9886
9887 CASE_FLT_FN (BUILT_IN_CASINH):
9888 if (validate_arg (arg0, COMPLEX_TYPE)
9889 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9890 return do_mpc_arg1 (arg0, type, mpc_asinh);
9891 break;
9892
9893 CASE_FLT_FN (BUILT_IN_CACOSH):
9894 if (validate_arg (arg0, COMPLEX_TYPE)
9895 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9896 return do_mpc_arg1 (arg0, type, mpc_acosh);
9897 break;
9898
9899 CASE_FLT_FN (BUILT_IN_CATANH):
9900 if (validate_arg (arg0, COMPLEX_TYPE)
9901 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9902 return do_mpc_arg1 (arg0, type, mpc_atanh);
9903 break;
9904
9905 CASE_FLT_FN (BUILT_IN_CABS):
9906 return fold_builtin_cabs (loc, arg0, type, fndecl);
9907
9908 CASE_FLT_FN (BUILT_IN_CARG):
9909 return fold_builtin_carg (loc, arg0, type);
9910
9911 CASE_FLT_FN (BUILT_IN_SQRT):
9912 return fold_builtin_sqrt (loc, arg0, type);
9913
9914 CASE_FLT_FN (BUILT_IN_CBRT):
9915 return fold_builtin_cbrt (loc, arg0, type);
9916
9917 CASE_FLT_FN (BUILT_IN_ASIN):
9918 if (validate_arg (arg0, REAL_TYPE))
9919 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9920 &dconstm1, &dconst1, true);
9921 break;
9922
9923 CASE_FLT_FN (BUILT_IN_ACOS):
9924 if (validate_arg (arg0, REAL_TYPE))
9925 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9926 &dconstm1, &dconst1, true);
9927 break;
9928
9929 CASE_FLT_FN (BUILT_IN_ATAN):
9930 if (validate_arg (arg0, REAL_TYPE))
9931 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9932 break;
9933
9934 CASE_FLT_FN (BUILT_IN_ASINH):
9935 if (validate_arg (arg0, REAL_TYPE))
9936 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9937 break;
9938
9939 CASE_FLT_FN (BUILT_IN_ACOSH):
9940 if (validate_arg (arg0, REAL_TYPE))
9941 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9942 &dconst1, NULL, true);
9943 break;
9944
9945 CASE_FLT_FN (BUILT_IN_ATANH):
9946 if (validate_arg (arg0, REAL_TYPE))
9947 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9948 &dconstm1, &dconst1, false);
9949 break;
9950
9951 CASE_FLT_FN (BUILT_IN_SIN):
9952 if (validate_arg (arg0, REAL_TYPE))
9953 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9954 break;
9955
9956 CASE_FLT_FN (BUILT_IN_COS):
9957 return fold_builtin_cos (loc, arg0, type, fndecl);
9958
9959 CASE_FLT_FN (BUILT_IN_TAN):
9960 return fold_builtin_tan (arg0, type);
9961
9962 CASE_FLT_FN (BUILT_IN_CEXP):
9963 return fold_builtin_cexp (loc, arg0, type);
9964
9965 CASE_FLT_FN (BUILT_IN_CEXPI):
9966 if (validate_arg (arg0, REAL_TYPE))
9967 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9968 break;
9969
9970 CASE_FLT_FN (BUILT_IN_SINH):
9971 if (validate_arg (arg0, REAL_TYPE))
9972 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9973 break;
9974
9975 CASE_FLT_FN (BUILT_IN_COSH):
9976 return fold_builtin_cosh (loc, arg0, type, fndecl);
9977
9978 CASE_FLT_FN (BUILT_IN_TANH):
9979 if (validate_arg (arg0, REAL_TYPE))
9980 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9981 break;
9982
9983 CASE_FLT_FN (BUILT_IN_ERF):
9984 if (validate_arg (arg0, REAL_TYPE))
9985 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9986 break;
9987
9988 CASE_FLT_FN (BUILT_IN_ERFC):
9989 if (validate_arg (arg0, REAL_TYPE))
9990 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9991 break;
9992
9993 CASE_FLT_FN (BUILT_IN_TGAMMA):
9994 if (validate_arg (arg0, REAL_TYPE))
9995 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9996 break;
9997
9998 CASE_FLT_FN (BUILT_IN_EXP):
9999 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10000
10001 CASE_FLT_FN (BUILT_IN_EXP2):
10002 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10003
10004 CASE_FLT_FN (BUILT_IN_EXP10):
10005 CASE_FLT_FN (BUILT_IN_POW10):
10006 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10007
10008 CASE_FLT_FN (BUILT_IN_EXPM1):
10009 if (validate_arg (arg0, REAL_TYPE))
10010 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10011 break;
10012
10013 CASE_FLT_FN (BUILT_IN_LOG):
10014 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10015
10016 CASE_FLT_FN (BUILT_IN_LOG2):
10017 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10018
10019 CASE_FLT_FN (BUILT_IN_LOG10):
10020 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10021
10022 CASE_FLT_FN (BUILT_IN_LOG1P):
10023 if (validate_arg (arg0, REAL_TYPE))
10024 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10025 &dconstm1, NULL, false);
10026 break;
10027
10028 CASE_FLT_FN (BUILT_IN_J0):
10029 if (validate_arg (arg0, REAL_TYPE))
10030 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10031 NULL, NULL, 0);
10032 break;
10033
10034 CASE_FLT_FN (BUILT_IN_J1):
10035 if (validate_arg (arg0, REAL_TYPE))
10036 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10037 NULL, NULL, 0);
10038 break;
10039
10040 CASE_FLT_FN (BUILT_IN_Y0):
10041 if (validate_arg (arg0, REAL_TYPE))
10042 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10043 &dconst0, NULL, false);
10044 break;
10045
10046 CASE_FLT_FN (BUILT_IN_Y1):
10047 if (validate_arg (arg0, REAL_TYPE))
10048 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10049 &dconst0, NULL, false);
10050 break;
10051
10052 CASE_FLT_FN (BUILT_IN_NAN):
10053 case BUILT_IN_NAND32:
10054 case BUILT_IN_NAND64:
10055 case BUILT_IN_NAND128:
10056 return fold_builtin_nan (arg0, type, true);
10057
10058 CASE_FLT_FN (BUILT_IN_NANS):
10059 return fold_builtin_nan (arg0, type, false);
10060
10061 CASE_FLT_FN (BUILT_IN_FLOOR):
10062 return fold_builtin_floor (loc, fndecl, arg0);
10063
10064 CASE_FLT_FN (BUILT_IN_CEIL):
10065 return fold_builtin_ceil (loc, fndecl, arg0);
10066
10067 CASE_FLT_FN (BUILT_IN_TRUNC):
10068 return fold_builtin_trunc (loc, fndecl, arg0);
10069
10070 CASE_FLT_FN (BUILT_IN_ROUND):
10071 return fold_builtin_round (loc, fndecl, arg0);
10072
10073 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10074 CASE_FLT_FN (BUILT_IN_RINT):
10075 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10076
10077 CASE_FLT_FN (BUILT_IN_ICEIL):
10078 CASE_FLT_FN (BUILT_IN_LCEIL):
10079 CASE_FLT_FN (BUILT_IN_LLCEIL):
10080 CASE_FLT_FN (BUILT_IN_LFLOOR):
10081 CASE_FLT_FN (BUILT_IN_IFLOOR):
10082 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10083 CASE_FLT_FN (BUILT_IN_IROUND):
10084 CASE_FLT_FN (BUILT_IN_LROUND):
10085 CASE_FLT_FN (BUILT_IN_LLROUND):
10086 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10087
10088 CASE_FLT_FN (BUILT_IN_IRINT):
10089 CASE_FLT_FN (BUILT_IN_LRINT):
10090 CASE_FLT_FN (BUILT_IN_LLRINT):
10091 return fold_fixed_mathfn (loc, fndecl, arg0);
10092
10093 case BUILT_IN_BSWAP16:
10094 case BUILT_IN_BSWAP32:
10095 case BUILT_IN_BSWAP64:
10096 return fold_builtin_bswap (fndecl, arg0);
10097
10098 CASE_INT_FN (BUILT_IN_FFS):
10099 CASE_INT_FN (BUILT_IN_CLZ):
10100 CASE_INT_FN (BUILT_IN_CTZ):
10101 CASE_INT_FN (BUILT_IN_CLRSB):
10102 CASE_INT_FN (BUILT_IN_POPCOUNT):
10103 CASE_INT_FN (BUILT_IN_PARITY):
10104 return fold_builtin_bitop (fndecl, arg0);
10105
10106 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10107 return fold_builtin_signbit (loc, arg0, type);
10108
10109 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10110 return fold_builtin_significand (loc, arg0, type);
10111
10112 CASE_FLT_FN (BUILT_IN_ILOGB):
10113 CASE_FLT_FN (BUILT_IN_LOGB):
10114 return fold_builtin_logb (loc, arg0, type);
10115
10116 case BUILT_IN_ISASCII:
10117 return fold_builtin_isascii (loc, arg0);
10118
10119 case BUILT_IN_TOASCII:
10120 return fold_builtin_toascii (loc, arg0);
10121
10122 case BUILT_IN_ISDIGIT:
10123 return fold_builtin_isdigit (loc, arg0);
10124
10125 CASE_FLT_FN (BUILT_IN_FINITE):
10126 case BUILT_IN_FINITED32:
10127 case BUILT_IN_FINITED64:
10128 case BUILT_IN_FINITED128:
10129 case BUILT_IN_ISFINITE:
10130 {
10131 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10132 if (ret)
10133 return ret;
10134 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10135 }
10136
10137 CASE_FLT_FN (BUILT_IN_ISINF):
10138 case BUILT_IN_ISINFD32:
10139 case BUILT_IN_ISINFD64:
10140 case BUILT_IN_ISINFD128:
10141 {
10142 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10143 if (ret)
10144 return ret;
10145 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10146 }
10147
10148 case BUILT_IN_ISNORMAL:
10149 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10150
10151 case BUILT_IN_ISINF_SIGN:
10152 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10153
10154 CASE_FLT_FN (BUILT_IN_ISNAN):
10155 case BUILT_IN_ISNAND32:
10156 case BUILT_IN_ISNAND64:
10157 case BUILT_IN_ISNAND128:
10158 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10159
10160 case BUILT_IN_PRINTF:
10161 case BUILT_IN_PRINTF_UNLOCKED:
10162 case BUILT_IN_VPRINTF:
10163 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10164
10165 case BUILT_IN_FREE:
10166 if (integer_zerop (arg0))
10167 return build_empty_stmt (loc);
10168 break;
10169
10170 default:
10171 break;
10172 }
10173
10174 return NULL_TREE;
10175
10176 }
10177
10178 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10179 IGNORE is true if the result of the function call is ignored. This
10180 function returns NULL_TREE if no simplification was possible. */
10181
10182 static tree
10183 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10184 {
10185 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10186 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10187
10188 switch (fcode)
10189 {
10190 CASE_FLT_FN (BUILT_IN_JN):
10191 if (validate_arg (arg0, INTEGER_TYPE)
10192 && validate_arg (arg1, REAL_TYPE))
10193 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10194 break;
10195
10196 CASE_FLT_FN (BUILT_IN_YN):
10197 if (validate_arg (arg0, INTEGER_TYPE)
10198 && validate_arg (arg1, REAL_TYPE))
10199 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10200 &dconst0, false);
10201 break;
10202
10203 CASE_FLT_FN (BUILT_IN_DREM):
10204 CASE_FLT_FN (BUILT_IN_REMAINDER):
10205 if (validate_arg (arg0, REAL_TYPE)
10206 && validate_arg (arg1, REAL_TYPE))
10207 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10208 break;
10209
10210 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10211 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10212 if (validate_arg (arg0, REAL_TYPE)
10213 && validate_arg (arg1, POINTER_TYPE))
10214 return do_mpfr_lgamma_r (arg0, arg1, type);
10215 break;
10216
10217 CASE_FLT_FN (BUILT_IN_ATAN2):
10218 if (validate_arg (arg0, REAL_TYPE)
10219 && validate_arg (arg1, REAL_TYPE))
10220 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10221 break;
10222
10223 CASE_FLT_FN (BUILT_IN_FDIM):
10224 if (validate_arg (arg0, REAL_TYPE)
10225 && validate_arg (arg1, REAL_TYPE))
10226 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10227 break;
10228
10229 CASE_FLT_FN (BUILT_IN_HYPOT):
10230 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10231
10232 CASE_FLT_FN (BUILT_IN_CPOW):
10233 if (validate_arg (arg0, COMPLEX_TYPE)
10234 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10235 && validate_arg (arg1, COMPLEX_TYPE)
10236 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10237 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10238 break;
10239
10240 CASE_FLT_FN (BUILT_IN_LDEXP):
10241 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10242 CASE_FLT_FN (BUILT_IN_SCALBN):
10243 CASE_FLT_FN (BUILT_IN_SCALBLN):
10244 return fold_builtin_load_exponent (loc, arg0, arg1,
10245 type, /*ldexp=*/false);
10246
10247 CASE_FLT_FN (BUILT_IN_FREXP):
10248 return fold_builtin_frexp (loc, arg0, arg1, type);
10249
10250 CASE_FLT_FN (BUILT_IN_MODF):
10251 return fold_builtin_modf (loc, arg0, arg1, type);
10252
10253 case BUILT_IN_STRSTR:
10254 return fold_builtin_strstr (loc, arg0, arg1, type);
10255
10256 case BUILT_IN_STRSPN:
10257 return fold_builtin_strspn (loc, arg0, arg1);
10258
10259 case BUILT_IN_STRCSPN:
10260 return fold_builtin_strcspn (loc, arg0, arg1);
10261
10262 case BUILT_IN_STRCHR:
10263 case BUILT_IN_INDEX:
10264 return fold_builtin_strchr (loc, arg0, arg1, type);
10265
10266 case BUILT_IN_STRRCHR:
10267 case BUILT_IN_RINDEX:
10268 return fold_builtin_strrchr (loc, arg0, arg1, type);
10269
10270 case BUILT_IN_STPCPY:
10271 if (ignore)
10272 {
10273 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10274 if (!fn)
10275 break;
10276
10277 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10278 }
10279 else
10280 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10281 break;
10282
10283 case BUILT_IN_STRCMP:
10284 return fold_builtin_strcmp (loc, arg0, arg1);
10285
10286 case BUILT_IN_STRPBRK:
10287 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10288
10289 case BUILT_IN_EXPECT:
10290 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10291
10292 CASE_FLT_FN (BUILT_IN_POW):
10293 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10294
10295 CASE_FLT_FN (BUILT_IN_POWI):
10296 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10297
10298 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10299 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10300
10301 CASE_FLT_FN (BUILT_IN_FMIN):
10302 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10303
10304 CASE_FLT_FN (BUILT_IN_FMAX):
10305 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10306
10307 case BUILT_IN_ISGREATER:
10308 return fold_builtin_unordered_cmp (loc, fndecl,
10309 arg0, arg1, UNLE_EXPR, LE_EXPR);
10310 case BUILT_IN_ISGREATEREQUAL:
10311 return fold_builtin_unordered_cmp (loc, fndecl,
10312 arg0, arg1, UNLT_EXPR, LT_EXPR);
10313 case BUILT_IN_ISLESS:
10314 return fold_builtin_unordered_cmp (loc, fndecl,
10315 arg0, arg1, UNGE_EXPR, GE_EXPR);
10316 case BUILT_IN_ISLESSEQUAL:
10317 return fold_builtin_unordered_cmp (loc, fndecl,
10318 arg0, arg1, UNGT_EXPR, GT_EXPR);
10319 case BUILT_IN_ISLESSGREATER:
10320 return fold_builtin_unordered_cmp (loc, fndecl,
10321 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10322 case BUILT_IN_ISUNORDERED:
10323 return fold_builtin_unordered_cmp (loc, fndecl,
10324 arg0, arg1, UNORDERED_EXPR,
10325 NOP_EXPR);
10326
10327 /* We do the folding for va_start in the expander. */
10328 case BUILT_IN_VA_START:
10329 break;
10330
10331 case BUILT_IN_OBJECT_SIZE:
10332 return fold_builtin_object_size (arg0, arg1);
10333
10334 case BUILT_IN_PRINTF:
10335 case BUILT_IN_PRINTF_UNLOCKED:
10336 case BUILT_IN_VPRINTF:
10337 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10338
10339 case BUILT_IN_PRINTF_CHK:
10340 case BUILT_IN_VPRINTF_CHK:
10341 if (!validate_arg (arg0, INTEGER_TYPE)
10342 || TREE_SIDE_EFFECTS (arg0))
10343 return NULL_TREE;
10344 else
10345 return fold_builtin_printf (loc, fndecl,
10346 arg1, NULL_TREE, ignore, fcode);
10347 break;
10348
10349 case BUILT_IN_FPRINTF:
10350 case BUILT_IN_FPRINTF_UNLOCKED:
10351 case BUILT_IN_VFPRINTF:
10352 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10353 ignore, fcode);
10354
10355 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10356 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10357
10358 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10359 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10360
10361 default:
10362 break;
10363 }
10364 return NULL_TREE;
10365 }
10366
10367 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10368 and ARG2. IGNORE is true if the result of the function call is ignored.
10369 This function returns NULL_TREE if no simplification was possible. */
10370
10371 static tree
10372 fold_builtin_3 (location_t loc, tree fndecl,
10373 tree arg0, tree arg1, tree arg2, bool ignore)
10374 {
10375 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10376 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10377 switch (fcode)
10378 {
10379
10380 CASE_FLT_FN (BUILT_IN_SINCOS):
10381 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10382
10383 CASE_FLT_FN (BUILT_IN_FMA):
10384 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10385 break;
10386
10387 CASE_FLT_FN (BUILT_IN_REMQUO):
10388 if (validate_arg (arg0, REAL_TYPE)
10389 && validate_arg (arg1, REAL_TYPE)
10390 && validate_arg (arg2, POINTER_TYPE))
10391 return do_mpfr_remquo (arg0, arg1, arg2);
10392 break;
10393
10394 case BUILT_IN_STRNCAT:
10395 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10396
10397 case BUILT_IN_STRNCMP:
10398 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10399
10400 case BUILT_IN_MEMCHR:
10401 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10402
10403 case BUILT_IN_BCMP:
10404 case BUILT_IN_MEMCMP:
10405 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10406
10407 case BUILT_IN_PRINTF_CHK:
10408 case BUILT_IN_VPRINTF_CHK:
10409 if (!validate_arg (arg0, INTEGER_TYPE)
10410 || TREE_SIDE_EFFECTS (arg0))
10411 return NULL_TREE;
10412 else
10413 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10414 break;
10415
10416 case BUILT_IN_FPRINTF:
10417 case BUILT_IN_FPRINTF_UNLOCKED:
10418 case BUILT_IN_VFPRINTF:
10419 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10420 ignore, fcode);
10421
10422 case BUILT_IN_FPRINTF_CHK:
10423 case BUILT_IN_VFPRINTF_CHK:
10424 if (!validate_arg (arg1, INTEGER_TYPE)
10425 || TREE_SIDE_EFFECTS (arg1))
10426 return NULL_TREE;
10427 else
10428 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10429 ignore, fcode);
10430
10431 case BUILT_IN_EXPECT:
10432 return fold_builtin_expect (loc, arg0, arg1, arg2);
10433
10434 default:
10435 break;
10436 }
10437 return NULL_TREE;
10438 }
10439
10440 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10441 ARG2, and ARG3. IGNORE is true if the result of the function call is
10442 ignored. This function returns NULL_TREE if no simplification was
10443 possible. */
10444
10445 static tree
10446 fold_builtin_4 (location_t loc, tree fndecl,
10447 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10448 {
10449 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10450
10451 switch (fcode)
10452 {
10453 case BUILT_IN_FPRINTF_CHK:
10454 case BUILT_IN_VFPRINTF_CHK:
10455 if (!validate_arg (arg1, INTEGER_TYPE)
10456 || TREE_SIDE_EFFECTS (arg1))
10457 return NULL_TREE;
10458 else
10459 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10460 ignore, fcode);
10461 break;
10462
10463 default:
10464 break;
10465 }
10466 return NULL_TREE;
10467 }
10468
10469 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10470 arguments, where NARGS <= 4. IGNORE is true if the result of the
10471 function call is ignored. This function returns NULL_TREE if no
10472 simplification was possible. Note that this only folds builtins with
10473 fixed argument patterns. Foldings that do varargs-to-varargs
10474 transformations, or that match calls with more than 4 arguments,
10475 need to be handled with fold_builtin_varargs instead. */
10476
10477 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10478
10479 tree
10480 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10481 {
10482 tree ret = NULL_TREE;
10483
10484 switch (nargs)
10485 {
10486 case 0:
10487 ret = fold_builtin_0 (loc, fndecl, ignore);
10488 break;
10489 case 1:
10490 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10491 break;
10492 case 2:
10493 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10494 break;
10495 case 3:
10496 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10497 break;
10498 case 4:
10499 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10500 ignore);
10501 break;
10502 default:
10503 break;
10504 }
10505 if (ret)
10506 {
10507 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10508 SET_EXPR_LOCATION (ret, loc);
10509 TREE_NO_WARNING (ret) = 1;
10510 return ret;
10511 }
10512 return NULL_TREE;
10513 }
10514
10515 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10516 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10517 of arguments in ARGS to be omitted. OLDNARGS is the number of
10518 elements in ARGS. */
10519
10520 static tree
10521 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10522 int skip, tree fndecl, int n, va_list newargs)
10523 {
10524 int nargs = oldnargs - skip + n;
10525 tree *buffer;
10526
10527 if (n > 0)
10528 {
10529 int i, j;
10530
10531 buffer = XALLOCAVEC (tree, nargs);
10532 for (i = 0; i < n; i++)
10533 buffer[i] = va_arg (newargs, tree);
10534 for (j = skip; j < oldnargs; j++, i++)
10535 buffer[i] = args[j];
10536 }
10537 else
10538 buffer = args + skip;
10539
10540 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10541 }
10542
10543 /* Return true if FNDECL shouldn't be folded right now.
10544 If a built-in function has an inline attribute always_inline
10545 wrapper, defer folding it after always_inline functions have
10546 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10547 might not be performed. */
10548
10549 bool
10550 avoid_folding_inline_builtin (tree fndecl)
10551 {
10552 return (DECL_DECLARED_INLINE_P (fndecl)
10553 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10554 && cfun
10555 && !cfun->always_inline_functions_inlined
10556 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10557 }
10558
10559 /* A wrapper function for builtin folding that prevents warnings for
10560 "statement without effect" and the like, caused by removing the
10561 call node earlier than the warning is generated. */
10562
10563 tree
10564 fold_call_expr (location_t loc, tree exp, bool ignore)
10565 {
10566 tree ret = NULL_TREE;
10567 tree fndecl = get_callee_fndecl (exp);
10568 if (fndecl
10569 && TREE_CODE (fndecl) == FUNCTION_DECL
10570 && DECL_BUILT_IN (fndecl)
10571 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10572 yet. Defer folding until we see all the arguments
10573 (after inlining). */
10574 && !CALL_EXPR_VA_ARG_PACK (exp))
10575 {
10576 int nargs = call_expr_nargs (exp);
10577
10578 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10579 instead last argument is __builtin_va_arg_pack (). Defer folding
10580 even in that case, until arguments are finalized. */
10581 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10582 {
10583 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10584 if (fndecl2
10585 && TREE_CODE (fndecl2) == FUNCTION_DECL
10586 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10587 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10588 return NULL_TREE;
10589 }
10590
10591 if (avoid_folding_inline_builtin (fndecl))
10592 return NULL_TREE;
10593
10594 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10595 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10596 CALL_EXPR_ARGP (exp), ignore);
10597 else
10598 {
10599 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10600 {
10601 tree *args = CALL_EXPR_ARGP (exp);
10602 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10603 }
10604 if (!ret)
10605 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10606 if (ret)
10607 return ret;
10608 }
10609 }
10610 return NULL_TREE;
10611 }
10612
10613 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10614 N arguments are passed in the array ARGARRAY. */
10615
10616 tree
10617 fold_builtin_call_array (location_t loc, tree type,
10618 tree fn,
10619 int n,
10620 tree *argarray)
10621 {
10622 tree ret = NULL_TREE;
10623 tree exp;
10624
10625 if (TREE_CODE (fn) == ADDR_EXPR)
10626 {
10627 tree fndecl = TREE_OPERAND (fn, 0);
10628 if (TREE_CODE (fndecl) == FUNCTION_DECL
10629 && DECL_BUILT_IN (fndecl))
10630 {
10631 /* If last argument is __builtin_va_arg_pack (), arguments to this
10632 function are not finalized yet. Defer folding until they are. */
10633 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10634 {
10635 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10636 if (fndecl2
10637 && TREE_CODE (fndecl2) == FUNCTION_DECL
10638 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10639 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10640 return build_call_array_loc (loc, type, fn, n, argarray);
10641 }
10642 if (avoid_folding_inline_builtin (fndecl))
10643 return build_call_array_loc (loc, type, fn, n, argarray);
10644 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10645 {
10646 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10647 if (ret)
10648 return ret;
10649
10650 return build_call_array_loc (loc, type, fn, n, argarray);
10651 }
10652 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10653 {
10654 /* First try the transformations that don't require consing up
10655 an exp. */
10656 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10657 if (ret)
10658 return ret;
10659 }
10660
10661 /* If we got this far, we need to build an exp. */
10662 exp = build_call_array_loc (loc, type, fn, n, argarray);
10663 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10664 return ret ? ret : exp;
10665 }
10666 }
10667
10668 return build_call_array_loc (loc, type, fn, n, argarray);
10669 }
10670
10671 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10672 along with N new arguments specified as the "..." parameters. SKIP
10673 is the number of arguments in EXP to be omitted. This function is used
10674 to do varargs-to-varargs transformations. */
10675
10676 static tree
10677 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10678 {
10679 va_list ap;
10680 tree t;
10681
10682 va_start (ap, n);
10683 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10684 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10685 va_end (ap);
10686
10687 return t;
10688 }
10689
10690 /* Validate a single argument ARG against a tree code CODE representing
10691 a type. */
10692
10693 static bool
10694 validate_arg (const_tree arg, enum tree_code code)
10695 {
10696 if (!arg)
10697 return false;
10698 else if (code == POINTER_TYPE)
10699 return POINTER_TYPE_P (TREE_TYPE (arg));
10700 else if (code == INTEGER_TYPE)
10701 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10702 return code == TREE_CODE (TREE_TYPE (arg));
10703 }
10704
10705 /* This function validates the types of a function call argument list
10706 against a specified list of tree_codes. If the last specifier is a 0,
10707 that represents an ellipses, otherwise the last specifier must be a
10708 VOID_TYPE.
10709
10710 This is the GIMPLE version of validate_arglist. Eventually we want to
10711 completely convert builtins.c to work from GIMPLEs and the tree based
10712 validate_arglist will then be removed. */
10713
10714 bool
10715 validate_gimple_arglist (const_gimple call, ...)
10716 {
10717 enum tree_code code;
10718 bool res = 0;
10719 va_list ap;
10720 const_tree arg;
10721 size_t i;
10722
10723 va_start (ap, call);
10724 i = 0;
10725
10726 do
10727 {
10728 code = (enum tree_code) va_arg (ap, int);
10729 switch (code)
10730 {
10731 case 0:
10732 /* This signifies an ellipses, any further arguments are all ok. */
10733 res = true;
10734 goto end;
10735 case VOID_TYPE:
10736 /* This signifies an endlink, if no arguments remain, return
10737 true, otherwise return false. */
10738 res = (i == gimple_call_num_args (call));
10739 goto end;
10740 default:
10741 /* If no parameters remain or the parameter's code does not
10742 match the specified code, return false. Otherwise continue
10743 checking any remaining arguments. */
10744 arg = gimple_call_arg (call, i++);
10745 if (!validate_arg (arg, code))
10746 goto end;
10747 break;
10748 }
10749 }
10750 while (1);
10751
10752 /* We need gotos here since we can only have one VA_CLOSE in a
10753 function. */
10754 end: ;
10755 va_end (ap);
10756
10757 return res;
10758 }
10759
10760 /* Default target-specific builtin expander that does nothing. */
10761
10762 rtx
10763 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10764 rtx target ATTRIBUTE_UNUSED,
10765 rtx subtarget ATTRIBUTE_UNUSED,
10766 machine_mode mode ATTRIBUTE_UNUSED,
10767 int ignore ATTRIBUTE_UNUSED)
10768 {
10769 return NULL_RTX;
10770 }
10771
10772 /* Returns true is EXP represents data that would potentially reside
10773 in a readonly section. */
10774
10775 bool
10776 readonly_data_expr (tree exp)
10777 {
10778 STRIP_NOPS (exp);
10779
10780 if (TREE_CODE (exp) != ADDR_EXPR)
10781 return false;
10782
10783 exp = get_base_address (TREE_OPERAND (exp, 0));
10784 if (!exp)
10785 return false;
10786
10787 /* Make sure we call decl_readonly_section only for trees it
10788 can handle (since it returns true for everything it doesn't
10789 understand). */
10790 if (TREE_CODE (exp) == STRING_CST
10791 || TREE_CODE (exp) == CONSTRUCTOR
10792 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10793 return decl_readonly_section (exp, 0);
10794 else
10795 return false;
10796 }
10797
10798 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10799 to the call, and TYPE is its return type.
10800
10801 Return NULL_TREE if no simplification was possible, otherwise return the
10802 simplified form of the call as a tree.
10803
10804 The simplified form may be a constant or other expression which
10805 computes the same value, but in a more efficient manner (including
10806 calls to other builtin functions).
10807
10808 The call may contain arguments which need to be evaluated, but
10809 which are not useful to determine the result of the call. In
10810 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10811 COMPOUND_EXPR will be an argument which must be evaluated.
10812 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10813 COMPOUND_EXPR in the chain will contain the tree for the simplified
10814 form of the builtin function call. */
10815
10816 static tree
10817 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10818 {
10819 if (!validate_arg (s1, POINTER_TYPE)
10820 || !validate_arg (s2, POINTER_TYPE))
10821 return NULL_TREE;
10822 else
10823 {
10824 tree fn;
10825 const char *p1, *p2;
10826
10827 p2 = c_getstr (s2);
10828 if (p2 == NULL)
10829 return NULL_TREE;
10830
10831 p1 = c_getstr (s1);
10832 if (p1 != NULL)
10833 {
10834 const char *r = strstr (p1, p2);
10835 tree tem;
10836
10837 if (r == NULL)
10838 return build_int_cst (TREE_TYPE (s1), 0);
10839
10840 /* Return an offset into the constant string argument. */
10841 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10842 return fold_convert_loc (loc, type, tem);
10843 }
10844
10845 /* The argument is const char *, and the result is char *, so we need
10846 a type conversion here to avoid a warning. */
10847 if (p2[0] == '\0')
10848 return fold_convert_loc (loc, type, s1);
10849
10850 if (p2[1] != '\0')
10851 return NULL_TREE;
10852
10853 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10854 if (!fn)
10855 return NULL_TREE;
10856
10857 /* New argument list transforming strstr(s1, s2) to
10858 strchr(s1, s2[0]). */
10859 return build_call_expr_loc (loc, fn, 2, s1,
10860 build_int_cst (integer_type_node, p2[0]));
10861 }
10862 }
10863
10864 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10865 the call, and TYPE is its return type.
10866
10867 Return NULL_TREE if no simplification was possible, otherwise return the
10868 simplified form of the call as a tree.
10869
10870 The simplified form may be a constant or other expression which
10871 computes the same value, but in a more efficient manner (including
10872 calls to other builtin functions).
10873
10874 The call may contain arguments which need to be evaluated, but
10875 which are not useful to determine the result of the call. In
10876 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10877 COMPOUND_EXPR will be an argument which must be evaluated.
10878 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10879 COMPOUND_EXPR in the chain will contain the tree for the simplified
10880 form of the builtin function call. */
10881
10882 static tree
10883 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10884 {
10885 if (!validate_arg (s1, POINTER_TYPE)
10886 || !validate_arg (s2, INTEGER_TYPE))
10887 return NULL_TREE;
10888 else
10889 {
10890 const char *p1;
10891
10892 if (TREE_CODE (s2) != INTEGER_CST)
10893 return NULL_TREE;
10894
10895 p1 = c_getstr (s1);
10896 if (p1 != NULL)
10897 {
10898 char c;
10899 const char *r;
10900 tree tem;
10901
10902 if (target_char_cast (s2, &c))
10903 return NULL_TREE;
10904
10905 r = strchr (p1, c);
10906
10907 if (r == NULL)
10908 return build_int_cst (TREE_TYPE (s1), 0);
10909
10910 /* Return an offset into the constant string argument. */
10911 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10912 return fold_convert_loc (loc, type, tem);
10913 }
10914 return NULL_TREE;
10915 }
10916 }
10917
10918 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10919 the call, and TYPE is its return type.
10920
10921 Return NULL_TREE if no simplification was possible, otherwise return the
10922 simplified form of the call as a tree.
10923
10924 The simplified form may be a constant or other expression which
10925 computes the same value, but in a more efficient manner (including
10926 calls to other builtin functions).
10927
10928 The call may contain arguments which need to be evaluated, but
10929 which are not useful to determine the result of the call. In
10930 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10931 COMPOUND_EXPR will be an argument which must be evaluated.
10932 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10933 COMPOUND_EXPR in the chain will contain the tree for the simplified
10934 form of the builtin function call. */
10935
10936 static tree
10937 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10938 {
10939 if (!validate_arg (s1, POINTER_TYPE)
10940 || !validate_arg (s2, INTEGER_TYPE))
10941 return NULL_TREE;
10942 else
10943 {
10944 tree fn;
10945 const char *p1;
10946
10947 if (TREE_CODE (s2) != INTEGER_CST)
10948 return NULL_TREE;
10949
10950 p1 = c_getstr (s1);
10951 if (p1 != NULL)
10952 {
10953 char c;
10954 const char *r;
10955 tree tem;
10956
10957 if (target_char_cast (s2, &c))
10958 return NULL_TREE;
10959
10960 r = strrchr (p1, c);
10961
10962 if (r == NULL)
10963 return build_int_cst (TREE_TYPE (s1), 0);
10964
10965 /* Return an offset into the constant string argument. */
10966 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10967 return fold_convert_loc (loc, type, tem);
10968 }
10969
10970 if (! integer_zerop (s2))
10971 return NULL_TREE;
10972
10973 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10974 if (!fn)
10975 return NULL_TREE;
10976
10977 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10978 return build_call_expr_loc (loc, fn, 2, s1, s2);
10979 }
10980 }
10981
10982 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10983 to the call, and TYPE is its return type.
10984
10985 Return NULL_TREE if no simplification was possible, otherwise return the
10986 simplified form of the call as a tree.
10987
10988 The simplified form may be a constant or other expression which
10989 computes the same value, but in a more efficient manner (including
10990 calls to other builtin functions).
10991
10992 The call may contain arguments which need to be evaluated, but
10993 which are not useful to determine the result of the call. In
10994 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10995 COMPOUND_EXPR will be an argument which must be evaluated.
10996 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10997 COMPOUND_EXPR in the chain will contain the tree for the simplified
10998 form of the builtin function call. */
10999
11000 static tree
11001 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11002 {
11003 if (!validate_arg (s1, POINTER_TYPE)
11004 || !validate_arg (s2, POINTER_TYPE))
11005 return NULL_TREE;
11006 else
11007 {
11008 tree fn;
11009 const char *p1, *p2;
11010
11011 p2 = c_getstr (s2);
11012 if (p2 == NULL)
11013 return NULL_TREE;
11014
11015 p1 = c_getstr (s1);
11016 if (p1 != NULL)
11017 {
11018 const char *r = strpbrk (p1, p2);
11019 tree tem;
11020
11021 if (r == NULL)
11022 return build_int_cst (TREE_TYPE (s1), 0);
11023
11024 /* Return an offset into the constant string argument. */
11025 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11026 return fold_convert_loc (loc, type, tem);
11027 }
11028
11029 if (p2[0] == '\0')
11030 /* strpbrk(x, "") == NULL.
11031 Evaluate and ignore s1 in case it had side-effects. */
11032 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11033
11034 if (p2[1] != '\0')
11035 return NULL_TREE; /* Really call strpbrk. */
11036
11037 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11038 if (!fn)
11039 return NULL_TREE;
11040
11041 /* New argument list transforming strpbrk(s1, s2) to
11042 strchr(s1, s2[0]). */
11043 return build_call_expr_loc (loc, fn, 2, s1,
11044 build_int_cst (integer_type_node, p2[0]));
11045 }
11046 }
11047
11048 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11049 arguments to the call.
11050
11051 Return NULL_TREE if no simplification was possible, otherwise return the
11052 simplified form of the call as a tree.
11053
11054 The simplified form may be a constant or other expression which
11055 computes the same value, but in a more efficient manner (including
11056 calls to other builtin functions).
11057
11058 The call may contain arguments which need to be evaluated, but
11059 which are not useful to determine the result of the call. In
11060 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11061 COMPOUND_EXPR will be an argument which must be evaluated.
11062 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11063 COMPOUND_EXPR in the chain will contain the tree for the simplified
11064 form of the builtin function call. */
11065
11066 static tree
11067 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11068 {
11069 if (!validate_arg (dst, POINTER_TYPE)
11070 || !validate_arg (src, POINTER_TYPE)
11071 || !validate_arg (len, INTEGER_TYPE))
11072 return NULL_TREE;
11073 else
11074 {
11075 const char *p = c_getstr (src);
11076
11077 /* If the requested length is zero, or the src parameter string
11078 length is zero, return the dst parameter. */
11079 if (integer_zerop (len) || (p && *p == '\0'))
11080 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11081
11082 /* If the requested len is greater than or equal to the string
11083 length, call strcat. */
11084 if (TREE_CODE (len) == INTEGER_CST && p
11085 && compare_tree_int (len, strlen (p)) >= 0)
11086 {
11087 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11088
11089 /* If the replacement _DECL isn't initialized, don't do the
11090 transformation. */
11091 if (!fn)
11092 return NULL_TREE;
11093
11094 return build_call_expr_loc (loc, fn, 2, dst, src);
11095 }
11096 return NULL_TREE;
11097 }
11098 }
11099
11100 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11101 to the call.
11102
11103 Return NULL_TREE if no simplification was possible, otherwise return the
11104 simplified form of the call as a tree.
11105
11106 The simplified form may be a constant or other expression which
11107 computes the same value, but in a more efficient manner (including
11108 calls to other builtin functions).
11109
11110 The call may contain arguments which need to be evaluated, but
11111 which are not useful to determine the result of the call. In
11112 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11113 COMPOUND_EXPR will be an argument which must be evaluated.
11114 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11115 COMPOUND_EXPR in the chain will contain the tree for the simplified
11116 form of the builtin function call. */
11117
11118 static tree
11119 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11120 {
11121 if (!validate_arg (s1, POINTER_TYPE)
11122 || !validate_arg (s2, POINTER_TYPE))
11123 return NULL_TREE;
11124 else
11125 {
11126 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11127
11128 /* If both arguments are constants, evaluate at compile-time. */
11129 if (p1 && p2)
11130 {
11131 const size_t r = strspn (p1, p2);
11132 return build_int_cst (size_type_node, r);
11133 }
11134
11135 /* If either argument is "", return NULL_TREE. */
11136 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11137 /* Evaluate and ignore both arguments in case either one has
11138 side-effects. */
11139 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11140 s1, s2);
11141 return NULL_TREE;
11142 }
11143 }
11144
11145 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11146 to the call.
11147
11148 Return NULL_TREE if no simplification was possible, otherwise return the
11149 simplified form of the call as a tree.
11150
11151 The simplified form may be a constant or other expression which
11152 computes the same value, but in a more efficient manner (including
11153 calls to other builtin functions).
11154
11155 The call may contain arguments which need to be evaluated, but
11156 which are not useful to determine the result of the call. In
11157 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11158 COMPOUND_EXPR will be an argument which must be evaluated.
11159 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11160 COMPOUND_EXPR in the chain will contain the tree for the simplified
11161 form of the builtin function call. */
11162
11163 static tree
11164 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11165 {
11166 if (!validate_arg (s1, POINTER_TYPE)
11167 || !validate_arg (s2, POINTER_TYPE))
11168 return NULL_TREE;
11169 else
11170 {
11171 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11172
11173 /* If both arguments are constants, evaluate at compile-time. */
11174 if (p1 && p2)
11175 {
11176 const size_t r = strcspn (p1, p2);
11177 return build_int_cst (size_type_node, r);
11178 }
11179
11180 /* If the first argument is "", return NULL_TREE. */
11181 if (p1 && *p1 == '\0')
11182 {
11183 /* Evaluate and ignore argument s2 in case it has
11184 side-effects. */
11185 return omit_one_operand_loc (loc, size_type_node,
11186 size_zero_node, s2);
11187 }
11188
11189 /* If the second argument is "", return __builtin_strlen(s1). */
11190 if (p2 && *p2 == '\0')
11191 {
11192 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11193
11194 /* If the replacement _DECL isn't initialized, don't do the
11195 transformation. */
11196 if (!fn)
11197 return NULL_TREE;
11198
11199 return build_call_expr_loc (loc, fn, 1, s1);
11200 }
11201 return NULL_TREE;
11202 }
11203 }
11204
11205 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11206 produced. False otherwise. This is done so that we don't output the error
11207 or warning twice or three times. */
11208
11209 bool
11210 fold_builtin_next_arg (tree exp, bool va_start_p)
11211 {
11212 tree fntype = TREE_TYPE (current_function_decl);
11213 int nargs = call_expr_nargs (exp);
11214 tree arg;
11215 /* There is good chance the current input_location points inside the
11216 definition of the va_start macro (perhaps on the token for
11217 builtin) in a system header, so warnings will not be emitted.
11218 Use the location in real source code. */
11219 source_location current_location =
11220 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11221 NULL);
11222
11223 if (!stdarg_p (fntype))
11224 {
11225 error ("%<va_start%> used in function with fixed args");
11226 return true;
11227 }
11228
11229 if (va_start_p)
11230 {
11231 if (va_start_p && (nargs != 2))
11232 {
11233 error ("wrong number of arguments to function %<va_start%>");
11234 return true;
11235 }
11236 arg = CALL_EXPR_ARG (exp, 1);
11237 }
11238 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11239 when we checked the arguments and if needed issued a warning. */
11240 else
11241 {
11242 if (nargs == 0)
11243 {
11244 /* Evidently an out of date version of <stdarg.h>; can't validate
11245 va_start's second argument, but can still work as intended. */
11246 warning_at (current_location,
11247 OPT_Wvarargs,
11248 "%<__builtin_next_arg%> called without an argument");
11249 return true;
11250 }
11251 else if (nargs > 1)
11252 {
11253 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11254 return true;
11255 }
11256 arg = CALL_EXPR_ARG (exp, 0);
11257 }
11258
11259 if (TREE_CODE (arg) == SSA_NAME)
11260 arg = SSA_NAME_VAR (arg);
11261
11262 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11263 or __builtin_next_arg (0) the first time we see it, after checking
11264 the arguments and if needed issuing a warning. */
11265 if (!integer_zerop (arg))
11266 {
11267 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11268
11269 /* Strip off all nops for the sake of the comparison. This
11270 is not quite the same as STRIP_NOPS. It does more.
11271 We must also strip off INDIRECT_EXPR for C++ reference
11272 parameters. */
11273 while (CONVERT_EXPR_P (arg)
11274 || TREE_CODE (arg) == INDIRECT_REF)
11275 arg = TREE_OPERAND (arg, 0);
11276 if (arg != last_parm)
11277 {
11278 /* FIXME: Sometimes with the tree optimizers we can get the
11279 not the last argument even though the user used the last
11280 argument. We just warn and set the arg to be the last
11281 argument so that we will get wrong-code because of
11282 it. */
11283 warning_at (current_location,
11284 OPT_Wvarargs,
11285 "second parameter of %<va_start%> not last named argument");
11286 }
11287
11288 /* Undefined by C99 7.15.1.4p4 (va_start):
11289 "If the parameter parmN is declared with the register storage
11290 class, with a function or array type, or with a type that is
11291 not compatible with the type that results after application of
11292 the default argument promotions, the behavior is undefined."
11293 */
11294 else if (DECL_REGISTER (arg))
11295 {
11296 warning_at (current_location,
11297 OPT_Wvarargs,
11298 "undefined behaviour when second parameter of "
11299 "%<va_start%> is declared with %<register%> storage");
11300 }
11301
11302 /* We want to verify the second parameter just once before the tree
11303 optimizers are run and then avoid keeping it in the tree,
11304 as otherwise we could warn even for correct code like:
11305 void foo (int i, ...)
11306 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11307 if (va_start_p)
11308 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11309 else
11310 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11311 }
11312 return false;
11313 }
11314
11315
11316 /* Expand a call EXP to __builtin_object_size. */
11317
11318 static rtx
11319 expand_builtin_object_size (tree exp)
11320 {
11321 tree ost;
11322 int object_size_type;
11323 tree fndecl = get_callee_fndecl (exp);
11324
11325 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11326 {
11327 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11328 exp, fndecl);
11329 expand_builtin_trap ();
11330 return const0_rtx;
11331 }
11332
11333 ost = CALL_EXPR_ARG (exp, 1);
11334 STRIP_NOPS (ost);
11335
11336 if (TREE_CODE (ost) != INTEGER_CST
11337 || tree_int_cst_sgn (ost) < 0
11338 || compare_tree_int (ost, 3) > 0)
11339 {
11340 error ("%Klast argument of %D is not integer constant between 0 and 3",
11341 exp, fndecl);
11342 expand_builtin_trap ();
11343 return const0_rtx;
11344 }
11345
11346 object_size_type = tree_to_shwi (ost);
11347
11348 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11349 }
11350
11351 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11352 FCODE is the BUILT_IN_* to use.
11353 Return NULL_RTX if we failed; the caller should emit a normal call,
11354 otherwise try to get the result in TARGET, if convenient (and in
11355 mode MODE if that's convenient). */
11356
11357 static rtx
11358 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11359 enum built_in_function fcode)
11360 {
11361 tree dest, src, len, size;
11362
11363 if (!validate_arglist (exp,
11364 POINTER_TYPE,
11365 fcode == BUILT_IN_MEMSET_CHK
11366 ? INTEGER_TYPE : POINTER_TYPE,
11367 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11368 return NULL_RTX;
11369
11370 dest = CALL_EXPR_ARG (exp, 0);
11371 src = CALL_EXPR_ARG (exp, 1);
11372 len = CALL_EXPR_ARG (exp, 2);
11373 size = CALL_EXPR_ARG (exp, 3);
11374
11375 if (! tree_fits_uhwi_p (size))
11376 return NULL_RTX;
11377
11378 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11379 {
11380 tree fn;
11381
11382 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11383 {
11384 warning_at (tree_nonartificial_location (exp),
11385 0, "%Kcall to %D will always overflow destination buffer",
11386 exp, get_callee_fndecl (exp));
11387 return NULL_RTX;
11388 }
11389
11390 fn = NULL_TREE;
11391 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11392 mem{cpy,pcpy,move,set} is available. */
11393 switch (fcode)
11394 {
11395 case BUILT_IN_MEMCPY_CHK:
11396 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11397 break;
11398 case BUILT_IN_MEMPCPY_CHK:
11399 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11400 break;
11401 case BUILT_IN_MEMMOVE_CHK:
11402 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11403 break;
11404 case BUILT_IN_MEMSET_CHK:
11405 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11406 break;
11407 default:
11408 break;
11409 }
11410
11411 if (! fn)
11412 return NULL_RTX;
11413
11414 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11415 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11416 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11417 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11418 }
11419 else if (fcode == BUILT_IN_MEMSET_CHK)
11420 return NULL_RTX;
11421 else
11422 {
11423 unsigned int dest_align = get_pointer_alignment (dest);
11424
11425 /* If DEST is not a pointer type, call the normal function. */
11426 if (dest_align == 0)
11427 return NULL_RTX;
11428
11429 /* If SRC and DEST are the same (and not volatile), do nothing. */
11430 if (operand_equal_p (src, dest, 0))
11431 {
11432 tree expr;
11433
11434 if (fcode != BUILT_IN_MEMPCPY_CHK)
11435 {
11436 /* Evaluate and ignore LEN in case it has side-effects. */
11437 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11438 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11439 }
11440
11441 expr = fold_build_pointer_plus (dest, len);
11442 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11443 }
11444
11445 /* __memmove_chk special case. */
11446 if (fcode == BUILT_IN_MEMMOVE_CHK)
11447 {
11448 unsigned int src_align = get_pointer_alignment (src);
11449
11450 if (src_align == 0)
11451 return NULL_RTX;
11452
11453 /* If src is categorized for a readonly section we can use
11454 normal __memcpy_chk. */
11455 if (readonly_data_expr (src))
11456 {
11457 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11458 if (!fn)
11459 return NULL_RTX;
11460 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11461 dest, src, len, size);
11462 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11463 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11464 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11465 }
11466 }
11467 return NULL_RTX;
11468 }
11469 }
11470
11471 /* Emit warning if a buffer overflow is detected at compile time. */
11472
11473 static void
11474 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11475 {
11476 int is_strlen = 0;
11477 tree len, size;
11478 location_t loc = tree_nonartificial_location (exp);
11479
11480 switch (fcode)
11481 {
11482 case BUILT_IN_STRCPY_CHK:
11483 case BUILT_IN_STPCPY_CHK:
11484 /* For __strcat_chk the warning will be emitted only if overflowing
11485 by at least strlen (dest) + 1 bytes. */
11486 case BUILT_IN_STRCAT_CHK:
11487 len = CALL_EXPR_ARG (exp, 1);
11488 size = CALL_EXPR_ARG (exp, 2);
11489 is_strlen = 1;
11490 break;
11491 case BUILT_IN_STRNCAT_CHK:
11492 case BUILT_IN_STRNCPY_CHK:
11493 case BUILT_IN_STPNCPY_CHK:
11494 len = CALL_EXPR_ARG (exp, 2);
11495 size = CALL_EXPR_ARG (exp, 3);
11496 break;
11497 case BUILT_IN_SNPRINTF_CHK:
11498 case BUILT_IN_VSNPRINTF_CHK:
11499 len = CALL_EXPR_ARG (exp, 1);
11500 size = CALL_EXPR_ARG (exp, 3);
11501 break;
11502 default:
11503 gcc_unreachable ();
11504 }
11505
11506 if (!len || !size)
11507 return;
11508
11509 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11510 return;
11511
11512 if (is_strlen)
11513 {
11514 len = c_strlen (len, 1);
11515 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11516 return;
11517 }
11518 else if (fcode == BUILT_IN_STRNCAT_CHK)
11519 {
11520 tree src = CALL_EXPR_ARG (exp, 1);
11521 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11522 return;
11523 src = c_strlen (src, 1);
11524 if (! src || ! tree_fits_uhwi_p (src))
11525 {
11526 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11527 exp, get_callee_fndecl (exp));
11528 return;
11529 }
11530 else if (tree_int_cst_lt (src, size))
11531 return;
11532 }
11533 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11534 return;
11535
11536 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11537 exp, get_callee_fndecl (exp));
11538 }
11539
11540 /* Emit warning if a buffer overflow is detected at compile time
11541 in __sprintf_chk/__vsprintf_chk calls. */
11542
11543 static void
11544 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11545 {
11546 tree size, len, fmt;
11547 const char *fmt_str;
11548 int nargs = call_expr_nargs (exp);
11549
11550 /* Verify the required arguments in the original call. */
11551
11552 if (nargs < 4)
11553 return;
11554 size = CALL_EXPR_ARG (exp, 2);
11555 fmt = CALL_EXPR_ARG (exp, 3);
11556
11557 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11558 return;
11559
11560 /* Check whether the format is a literal string constant. */
11561 fmt_str = c_getstr (fmt);
11562 if (fmt_str == NULL)
11563 return;
11564
11565 if (!init_target_chars ())
11566 return;
11567
11568 /* If the format doesn't contain % args or %%, we know its size. */
11569 if (strchr (fmt_str, target_percent) == 0)
11570 len = build_int_cstu (size_type_node, strlen (fmt_str));
11571 /* If the format is "%s" and first ... argument is a string literal,
11572 we know it too. */
11573 else if (fcode == BUILT_IN_SPRINTF_CHK
11574 && strcmp (fmt_str, target_percent_s) == 0)
11575 {
11576 tree arg;
11577
11578 if (nargs < 5)
11579 return;
11580 arg = CALL_EXPR_ARG (exp, 4);
11581 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11582 return;
11583
11584 len = c_strlen (arg, 1);
11585 if (!len || ! tree_fits_uhwi_p (len))
11586 return;
11587 }
11588 else
11589 return;
11590
11591 if (! tree_int_cst_lt (len, size))
11592 warning_at (tree_nonartificial_location (exp),
11593 0, "%Kcall to %D will always overflow destination buffer",
11594 exp, get_callee_fndecl (exp));
11595 }
11596
11597 /* Emit warning if a free is called with address of a variable. */
11598
11599 static void
11600 maybe_emit_free_warning (tree exp)
11601 {
11602 tree arg = CALL_EXPR_ARG (exp, 0);
11603
11604 STRIP_NOPS (arg);
11605 if (TREE_CODE (arg) != ADDR_EXPR)
11606 return;
11607
11608 arg = get_base_address (TREE_OPERAND (arg, 0));
11609 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11610 return;
11611
11612 if (SSA_VAR_P (arg))
11613 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11614 "%Kattempt to free a non-heap object %qD", exp, arg);
11615 else
11616 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11617 "%Kattempt to free a non-heap object", exp);
11618 }
11619
11620 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11621 if possible. */
11622
11623 static tree
11624 fold_builtin_object_size (tree ptr, tree ost)
11625 {
11626 unsigned HOST_WIDE_INT bytes;
11627 int object_size_type;
11628
11629 if (!validate_arg (ptr, POINTER_TYPE)
11630 || !validate_arg (ost, INTEGER_TYPE))
11631 return NULL_TREE;
11632
11633 STRIP_NOPS (ost);
11634
11635 if (TREE_CODE (ost) != INTEGER_CST
11636 || tree_int_cst_sgn (ost) < 0
11637 || compare_tree_int (ost, 3) > 0)
11638 return NULL_TREE;
11639
11640 object_size_type = tree_to_shwi (ost);
11641
11642 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11643 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11644 and (size_t) 0 for types 2 and 3. */
11645 if (TREE_SIDE_EFFECTS (ptr))
11646 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11647
11648 if (TREE_CODE (ptr) == ADDR_EXPR)
11649 {
11650 bytes = compute_builtin_object_size (ptr, object_size_type);
11651 if (wi::fits_to_tree_p (bytes, size_type_node))
11652 return build_int_cstu (size_type_node, bytes);
11653 }
11654 else if (TREE_CODE (ptr) == SSA_NAME)
11655 {
11656 /* If object size is not known yet, delay folding until
11657 later. Maybe subsequent passes will help determining
11658 it. */
11659 bytes = compute_builtin_object_size (ptr, object_size_type);
11660 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11661 && wi::fits_to_tree_p (bytes, size_type_node))
11662 return build_int_cstu (size_type_node, bytes);
11663 }
11664
11665 return NULL_TREE;
11666 }
11667
11668 /* Builtins with folding operations that operate on "..." arguments
11669 need special handling; we need to store the arguments in a convenient
11670 data structure before attempting any folding. Fortunately there are
11671 only a few builtins that fall into this category. FNDECL is the
11672 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11673 result of the function call is ignored. */
11674
11675 static tree
11676 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11677 bool ignore ATTRIBUTE_UNUSED)
11678 {
11679 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11680 tree ret = NULL_TREE;
11681
11682 switch (fcode)
11683 {
11684 case BUILT_IN_FPCLASSIFY:
11685 ret = fold_builtin_fpclassify (loc, exp);
11686 break;
11687
11688 default:
11689 break;
11690 }
11691 if (ret)
11692 {
11693 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11694 SET_EXPR_LOCATION (ret, loc);
11695 TREE_NO_WARNING (ret) = 1;
11696 return ret;
11697 }
11698 return NULL_TREE;
11699 }
11700
11701 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11702 FMT and ARG are the arguments to the call; we don't fold cases with
11703 more than 2 arguments, and ARG may be null if this is a 1-argument case.
11704
11705 Return NULL_TREE if no simplification was possible, otherwise return the
11706 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11707 code of the function to be simplified. */
11708
11709 static tree
11710 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
11711 tree arg, bool ignore,
11712 enum built_in_function fcode)
11713 {
11714 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
11715 const char *fmt_str = NULL;
11716
11717 /* If the return value is used, don't do the transformation. */
11718 if (! ignore)
11719 return NULL_TREE;
11720
11721 /* Verify the required arguments in the original call. */
11722 if (!validate_arg (fmt, POINTER_TYPE))
11723 return NULL_TREE;
11724
11725 /* Check whether the format is a literal string constant. */
11726 fmt_str = c_getstr (fmt);
11727 if (fmt_str == NULL)
11728 return NULL_TREE;
11729
11730 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11731 {
11732 /* If we're using an unlocked function, assume the other
11733 unlocked functions exist explicitly. */
11734 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
11735 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
11736 }
11737 else
11738 {
11739 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
11740 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
11741 }
11742
11743 if (!init_target_chars ())
11744 return NULL_TREE;
11745
11746 if (strcmp (fmt_str, target_percent_s) == 0
11747 || strchr (fmt_str, target_percent) == NULL)
11748 {
11749 const char *str;
11750
11751 if (strcmp (fmt_str, target_percent_s) == 0)
11752 {
11753 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11754 return NULL_TREE;
11755
11756 if (!arg || !validate_arg (arg, POINTER_TYPE))
11757 return NULL_TREE;
11758
11759 str = c_getstr (arg);
11760 if (str == NULL)
11761 return NULL_TREE;
11762 }
11763 else
11764 {
11765 /* The format specifier doesn't contain any '%' characters. */
11766 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11767 && arg)
11768 return NULL_TREE;
11769 str = fmt_str;
11770 }
11771
11772 /* If the string was "", printf does nothing. */
11773 if (str[0] == '\0')
11774 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11775
11776 /* If the string has length of 1, call putchar. */
11777 if (str[1] == '\0')
11778 {
11779 /* Given printf("c"), (where c is any one character,)
11780 convert "c"[0] to an int and pass that to the replacement
11781 function. */
11782 newarg = build_int_cst (integer_type_node, str[0]);
11783 if (fn_putchar)
11784 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
11785 }
11786 else
11787 {
11788 /* If the string was "string\n", call puts("string"). */
11789 size_t len = strlen (str);
11790 if ((unsigned char)str[len - 1] == target_newline
11791 && (size_t) (int) len == len
11792 && (int) len > 0)
11793 {
11794 char *newstr;
11795 tree offset_node, string_cst;
11796
11797 /* Create a NUL-terminated string that's one char shorter
11798 than the original, stripping off the trailing '\n'. */
11799 newarg = build_string_literal (len, str);
11800 string_cst = string_constant (newarg, &offset_node);
11801 gcc_checking_assert (string_cst
11802 && (TREE_STRING_LENGTH (string_cst)
11803 == (int) len)
11804 && integer_zerop (offset_node)
11805 && (unsigned char)
11806 TREE_STRING_POINTER (string_cst)[len - 1]
11807 == target_newline);
11808 /* build_string_literal creates a new STRING_CST,
11809 modify it in place to avoid double copying. */
11810 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
11811 newstr[len - 1] = '\0';
11812 if (fn_puts)
11813 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
11814 }
11815 else
11816 /* We'd like to arrange to call fputs(string,stdout) here,
11817 but we need stdout and don't have a way to get it yet. */
11818 return NULL_TREE;
11819 }
11820 }
11821
11822 /* The other optimizations can be done only on the non-va_list variants. */
11823 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11824 return NULL_TREE;
11825
11826 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11827 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11828 {
11829 if (!arg || !validate_arg (arg, POINTER_TYPE))
11830 return NULL_TREE;
11831 if (fn_puts)
11832 call = build_call_expr_loc (loc, fn_puts, 1, arg);
11833 }
11834
11835 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11836 else if (strcmp (fmt_str, target_percent_c) == 0)
11837 {
11838 if (!arg || !validate_arg (arg, INTEGER_TYPE))
11839 return NULL_TREE;
11840 if (fn_putchar)
11841 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
11842 }
11843
11844 if (!call)
11845 return NULL_TREE;
11846
11847 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
11848 }
11849
11850 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11851 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
11852 more than 3 arguments, and ARG may be null in the 2-argument case.
11853
11854 Return NULL_TREE if no simplification was possible, otherwise return the
11855 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11856 code of the function to be simplified. */
11857
11858 static tree
11859 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
11860 tree fmt, tree arg, bool ignore,
11861 enum built_in_function fcode)
11862 {
11863 tree fn_fputc, fn_fputs, call = NULL_TREE;
11864 const char *fmt_str = NULL;
11865
11866 /* If the return value is used, don't do the transformation. */
11867 if (! ignore)
11868 return NULL_TREE;
11869
11870 /* Verify the required arguments in the original call. */
11871 if (!validate_arg (fp, POINTER_TYPE))
11872 return NULL_TREE;
11873 if (!validate_arg (fmt, POINTER_TYPE))
11874 return NULL_TREE;
11875
11876 /* Check whether the format is a literal string constant. */
11877 fmt_str = c_getstr (fmt);
11878 if (fmt_str == NULL)
11879 return NULL_TREE;
11880
11881 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11882 {
11883 /* If we're using an unlocked function, assume the other
11884 unlocked functions exist explicitly. */
11885 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
11886 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
11887 }
11888 else
11889 {
11890 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
11891 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
11892 }
11893
11894 if (!init_target_chars ())
11895 return NULL_TREE;
11896
11897 /* If the format doesn't contain % args or %%, use strcpy. */
11898 if (strchr (fmt_str, target_percent) == NULL)
11899 {
11900 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11901 && arg)
11902 return NULL_TREE;
11903
11904 /* If the format specifier was "", fprintf does nothing. */
11905 if (fmt_str[0] == '\0')
11906 {
11907 /* If FP has side-effects, just wait until gimplification is
11908 done. */
11909 if (TREE_SIDE_EFFECTS (fp))
11910 return NULL_TREE;
11911
11912 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11913 }
11914
11915 /* When "string" doesn't contain %, replace all cases of
11916 fprintf (fp, string) with fputs (string, fp). The fputs
11917 builtin will take care of special cases like length == 1. */
11918 if (fn_fputs)
11919 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
11920 }
11921
11922 /* The other optimizations can be done only on the non-va_list variants. */
11923 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11924 return NULL_TREE;
11925
11926 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11927 else if (strcmp (fmt_str, target_percent_s) == 0)
11928 {
11929 if (!arg || !validate_arg (arg, POINTER_TYPE))
11930 return NULL_TREE;
11931 if (fn_fputs)
11932 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
11933 }
11934
11935 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11936 else if (strcmp (fmt_str, target_percent_c) == 0)
11937 {
11938 if (!arg || !validate_arg (arg, INTEGER_TYPE))
11939 return NULL_TREE;
11940 if (fn_fputc)
11941 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
11942 }
11943
11944 if (!call)
11945 return NULL_TREE;
11946 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
11947 }
11948
11949 /* Initialize format string characters in the target charset. */
11950
11951 bool
11952 init_target_chars (void)
11953 {
11954 static bool init;
11955 if (!init)
11956 {
11957 target_newline = lang_hooks.to_target_charset ('\n');
11958 target_percent = lang_hooks.to_target_charset ('%');
11959 target_c = lang_hooks.to_target_charset ('c');
11960 target_s = lang_hooks.to_target_charset ('s');
11961 if (target_newline == 0 || target_percent == 0 || target_c == 0
11962 || target_s == 0)
11963 return false;
11964
11965 target_percent_c[0] = target_percent;
11966 target_percent_c[1] = target_c;
11967 target_percent_c[2] = '\0';
11968
11969 target_percent_s[0] = target_percent;
11970 target_percent_s[1] = target_s;
11971 target_percent_s[2] = '\0';
11972
11973 target_percent_s_newline[0] = target_percent;
11974 target_percent_s_newline[1] = target_s;
11975 target_percent_s_newline[2] = target_newline;
11976 target_percent_s_newline[3] = '\0';
11977
11978 init = true;
11979 }
11980 return true;
11981 }
11982
11983 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11984 and no overflow/underflow occurred. INEXACT is true if M was not
11985 exactly calculated. TYPE is the tree type for the result. This
11986 function assumes that you cleared the MPFR flags and then
11987 calculated M to see if anything subsequently set a flag prior to
11988 entering this function. Return NULL_TREE if any checks fail. */
11989
11990 static tree
11991 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11992 {
11993 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11994 overflow/underflow occurred. If -frounding-math, proceed iff the
11995 result of calling FUNC was exact. */
11996 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11997 && (!flag_rounding_math || !inexact))
11998 {
11999 REAL_VALUE_TYPE rr;
12000
12001 real_from_mpfr (&rr, m, type, GMP_RNDN);
12002 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12003 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12004 but the mpft_t is not, then we underflowed in the
12005 conversion. */
12006 if (real_isfinite (&rr)
12007 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12008 {
12009 REAL_VALUE_TYPE rmode;
12010
12011 real_convert (&rmode, TYPE_MODE (type), &rr);
12012 /* Proceed iff the specified mode can hold the value. */
12013 if (real_identical (&rmode, &rr))
12014 return build_real (type, rmode);
12015 }
12016 }
12017 return NULL_TREE;
12018 }
12019
12020 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12021 number and no overflow/underflow occurred. INEXACT is true if M
12022 was not exactly calculated. TYPE is the tree type for the result.
12023 This function assumes that you cleared the MPFR flags and then
12024 calculated M to see if anything subsequently set a flag prior to
12025 entering this function. Return NULL_TREE if any checks fail, if
12026 FORCE_CONVERT is true, then bypass the checks. */
12027
12028 static tree
12029 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
12030 {
12031 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12032 overflow/underflow occurred. If -frounding-math, proceed iff the
12033 result of calling FUNC was exact. */
12034 if (force_convert
12035 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
12036 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12037 && (!flag_rounding_math || !inexact)))
12038 {
12039 REAL_VALUE_TYPE re, im;
12040
12041 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
12042 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
12043 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12044 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12045 but the mpft_t is not, then we underflowed in the
12046 conversion. */
12047 if (force_convert
12048 || (real_isfinite (&re) && real_isfinite (&im)
12049 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
12050 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
12051 {
12052 REAL_VALUE_TYPE re_mode, im_mode;
12053
12054 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
12055 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
12056 /* Proceed iff the specified mode can hold the value. */
12057 if (force_convert
12058 || (real_identical (&re_mode, &re)
12059 && real_identical (&im_mode, &im)))
12060 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
12061 build_real (TREE_TYPE (type), im_mode));
12062 }
12063 }
12064 return NULL_TREE;
12065 }
12066
12067 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12068 FUNC on it and return the resulting value as a tree with type TYPE.
12069 If MIN and/or MAX are not NULL, then the supplied ARG must be
12070 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12071 acceptable values, otherwise they are not. The mpfr precision is
12072 set to the precision of TYPE. We assume that function FUNC returns
12073 zero if the result could be calculated exactly within the requested
12074 precision. */
12075
12076 static tree
12077 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
12078 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
12079 bool inclusive)
12080 {
12081 tree result = NULL_TREE;
12082
12083 STRIP_NOPS (arg);
12084
12085 /* To proceed, MPFR must exactly represent the target floating point
12086 format, which only happens when the target base equals two. */
12087 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12088 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12089 {
12090 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12091
12092 if (real_isfinite (ra)
12093 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12094 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12095 {
12096 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12097 const int prec = fmt->p;
12098 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12099 int inexact;
12100 mpfr_t m;
12101
12102 mpfr_init2 (m, prec);
12103 mpfr_from_real (m, ra, GMP_RNDN);
12104 mpfr_clear_flags ();
12105 inexact = func (m, m, rnd);
12106 result = do_mpfr_ckconv (m, type, inexact);
12107 mpfr_clear (m);
12108 }
12109 }
12110
12111 return result;
12112 }
12113
12114 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12115 FUNC on it and return the resulting value as a tree with type TYPE.
12116 The mpfr precision is set to the precision of TYPE. We assume that
12117 function FUNC returns zero if the result could be calculated
12118 exactly within the requested precision. */
12119
12120 static tree
12121 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12122 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12123 {
12124 tree result = NULL_TREE;
12125
12126 STRIP_NOPS (arg1);
12127 STRIP_NOPS (arg2);
12128
12129 /* To proceed, MPFR must exactly represent the target floating point
12130 format, which only happens when the target base equals two. */
12131 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12132 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12133 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12134 {
12135 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12136 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12137
12138 if (real_isfinite (ra1) && real_isfinite (ra2))
12139 {
12140 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12141 const int prec = fmt->p;
12142 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12143 int inexact;
12144 mpfr_t m1, m2;
12145
12146 mpfr_inits2 (prec, m1, m2, NULL);
12147 mpfr_from_real (m1, ra1, GMP_RNDN);
12148 mpfr_from_real (m2, ra2, GMP_RNDN);
12149 mpfr_clear_flags ();
12150 inexact = func (m1, m1, m2, rnd);
12151 result = do_mpfr_ckconv (m1, type, inexact);
12152 mpfr_clears (m1, m2, NULL);
12153 }
12154 }
12155
12156 return result;
12157 }
12158
12159 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12160 FUNC on it and return the resulting value as a tree with type TYPE.
12161 The mpfr precision is set to the precision of TYPE. We assume that
12162 function FUNC returns zero if the result could be calculated
12163 exactly within the requested precision. */
12164
12165 static tree
12166 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12167 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12168 {
12169 tree result = NULL_TREE;
12170
12171 STRIP_NOPS (arg1);
12172 STRIP_NOPS (arg2);
12173 STRIP_NOPS (arg3);
12174
12175 /* To proceed, MPFR must exactly represent the target floating point
12176 format, which only happens when the target base equals two. */
12177 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12178 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12179 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12180 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12181 {
12182 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12183 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12184 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12185
12186 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12187 {
12188 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12189 const int prec = fmt->p;
12190 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12191 int inexact;
12192 mpfr_t m1, m2, m3;
12193
12194 mpfr_inits2 (prec, m1, m2, m3, NULL);
12195 mpfr_from_real (m1, ra1, GMP_RNDN);
12196 mpfr_from_real (m2, ra2, GMP_RNDN);
12197 mpfr_from_real (m3, ra3, GMP_RNDN);
12198 mpfr_clear_flags ();
12199 inexact = func (m1, m1, m2, m3, rnd);
12200 result = do_mpfr_ckconv (m1, type, inexact);
12201 mpfr_clears (m1, m2, m3, NULL);
12202 }
12203 }
12204
12205 return result;
12206 }
12207
12208 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12209 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12210 If ARG_SINP and ARG_COSP are NULL then the result is returned
12211 as a complex value.
12212 The type is taken from the type of ARG and is used for setting the
12213 precision of the calculation and results. */
12214
12215 static tree
12216 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
12217 {
12218 tree const type = TREE_TYPE (arg);
12219 tree result = NULL_TREE;
12220
12221 STRIP_NOPS (arg);
12222
12223 /* To proceed, MPFR must exactly represent the target floating point
12224 format, which only happens when the target base equals two. */
12225 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12226 && TREE_CODE (arg) == REAL_CST
12227 && !TREE_OVERFLOW (arg))
12228 {
12229 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12230
12231 if (real_isfinite (ra))
12232 {
12233 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12234 const int prec = fmt->p;
12235 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12236 tree result_s, result_c;
12237 int inexact;
12238 mpfr_t m, ms, mc;
12239
12240 mpfr_inits2 (prec, m, ms, mc, NULL);
12241 mpfr_from_real (m, ra, GMP_RNDN);
12242 mpfr_clear_flags ();
12243 inexact = mpfr_sin_cos (ms, mc, m, rnd);
12244 result_s = do_mpfr_ckconv (ms, type, inexact);
12245 result_c = do_mpfr_ckconv (mc, type, inexact);
12246 mpfr_clears (m, ms, mc, NULL);
12247 if (result_s && result_c)
12248 {
12249 /* If we are to return in a complex value do so. */
12250 if (!arg_sinp && !arg_cosp)
12251 return build_complex (build_complex_type (type),
12252 result_c, result_s);
12253
12254 /* Dereference the sin/cos pointer arguments. */
12255 arg_sinp = build_fold_indirect_ref (arg_sinp);
12256 arg_cosp = build_fold_indirect_ref (arg_cosp);
12257 /* Proceed if valid pointer type were passed in. */
12258 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
12259 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
12260 {
12261 /* Set the values. */
12262 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12263 result_s);
12264 TREE_SIDE_EFFECTS (result_s) = 1;
12265 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12266 result_c);
12267 TREE_SIDE_EFFECTS (result_c) = 1;
12268 /* Combine the assignments into a compound expr. */
12269 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12270 result_s, result_c));
12271 }
12272 }
12273 }
12274 }
12275 return result;
12276 }
12277
12278 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12279 two-argument mpfr order N Bessel function FUNC on them and return
12280 the resulting value as a tree with type TYPE. The mpfr precision
12281 is set to the precision of TYPE. We assume that function FUNC
12282 returns zero if the result could be calculated exactly within the
12283 requested precision. */
12284 static tree
12285 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12286 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12287 const REAL_VALUE_TYPE *min, bool inclusive)
12288 {
12289 tree result = NULL_TREE;
12290
12291 STRIP_NOPS (arg1);
12292 STRIP_NOPS (arg2);
12293
12294 /* To proceed, MPFR must exactly represent the target floating point
12295 format, which only happens when the target base equals two. */
12296 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12297 && tree_fits_shwi_p (arg1)
12298 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12299 {
12300 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12301 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12302
12303 if (n == (long)n
12304 && real_isfinite (ra)
12305 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12306 {
12307 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12308 const int prec = fmt->p;
12309 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12310 int inexact;
12311 mpfr_t m;
12312
12313 mpfr_init2 (m, prec);
12314 mpfr_from_real (m, ra, GMP_RNDN);
12315 mpfr_clear_flags ();
12316 inexact = func (m, n, m, rnd);
12317 result = do_mpfr_ckconv (m, type, inexact);
12318 mpfr_clear (m);
12319 }
12320 }
12321
12322 return result;
12323 }
12324
12325 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12326 the pointer *(ARG_QUO) and return the result. The type is taken
12327 from the type of ARG0 and is used for setting the precision of the
12328 calculation and results. */
12329
12330 static tree
12331 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12332 {
12333 tree const type = TREE_TYPE (arg0);
12334 tree result = NULL_TREE;
12335
12336 STRIP_NOPS (arg0);
12337 STRIP_NOPS (arg1);
12338
12339 /* To proceed, MPFR must exactly represent the target floating point
12340 format, which only happens when the target base equals two. */
12341 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12342 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12343 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12344 {
12345 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12346 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12347
12348 if (real_isfinite (ra0) && real_isfinite (ra1))
12349 {
12350 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12351 const int prec = fmt->p;
12352 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12353 tree result_rem;
12354 long integer_quo;
12355 mpfr_t m0, m1;
12356
12357 mpfr_inits2 (prec, m0, m1, NULL);
12358 mpfr_from_real (m0, ra0, GMP_RNDN);
12359 mpfr_from_real (m1, ra1, GMP_RNDN);
12360 mpfr_clear_flags ();
12361 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12362 /* Remquo is independent of the rounding mode, so pass
12363 inexact=0 to do_mpfr_ckconv(). */
12364 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12365 mpfr_clears (m0, m1, NULL);
12366 if (result_rem)
12367 {
12368 /* MPFR calculates quo in the host's long so it may
12369 return more bits in quo than the target int can hold
12370 if sizeof(host long) > sizeof(target int). This can
12371 happen even for native compilers in LP64 mode. In
12372 these cases, modulo the quo value with the largest
12373 number that the target int can hold while leaving one
12374 bit for the sign. */
12375 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12376 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12377
12378 /* Dereference the quo pointer argument. */
12379 arg_quo = build_fold_indirect_ref (arg_quo);
12380 /* Proceed iff a valid pointer type was passed in. */
12381 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12382 {
12383 /* Set the value. */
12384 tree result_quo
12385 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12386 build_int_cst (TREE_TYPE (arg_quo),
12387 integer_quo));
12388 TREE_SIDE_EFFECTS (result_quo) = 1;
12389 /* Combine the quo assignment with the rem. */
12390 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12391 result_quo, result_rem));
12392 }
12393 }
12394 }
12395 }
12396 return result;
12397 }
12398
12399 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12400 resulting value as a tree with type TYPE. The mpfr precision is
12401 set to the precision of TYPE. We assume that this mpfr function
12402 returns zero if the result could be calculated exactly within the
12403 requested precision. In addition, the integer pointer represented
12404 by ARG_SG will be dereferenced and set to the appropriate signgam
12405 (-1,1) value. */
12406
12407 static tree
12408 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12409 {
12410 tree result = NULL_TREE;
12411
12412 STRIP_NOPS (arg);
12413
12414 /* To proceed, MPFR must exactly represent the target floating point
12415 format, which only happens when the target base equals two. Also
12416 verify ARG is a constant and that ARG_SG is an int pointer. */
12417 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12418 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12419 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12420 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12421 {
12422 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12423
12424 /* In addition to NaN and Inf, the argument cannot be zero or a
12425 negative integer. */
12426 if (real_isfinite (ra)
12427 && ra->cl != rvc_zero
12428 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12429 {
12430 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12431 const int prec = fmt->p;
12432 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12433 int inexact, sg;
12434 mpfr_t m;
12435 tree result_lg;
12436
12437 mpfr_init2 (m, prec);
12438 mpfr_from_real (m, ra, GMP_RNDN);
12439 mpfr_clear_flags ();
12440 inexact = mpfr_lgamma (m, &sg, m, rnd);
12441 result_lg = do_mpfr_ckconv (m, type, inexact);
12442 mpfr_clear (m);
12443 if (result_lg)
12444 {
12445 tree result_sg;
12446
12447 /* Dereference the arg_sg pointer argument. */
12448 arg_sg = build_fold_indirect_ref (arg_sg);
12449 /* Assign the signgam value into *arg_sg. */
12450 result_sg = fold_build2 (MODIFY_EXPR,
12451 TREE_TYPE (arg_sg), arg_sg,
12452 build_int_cst (TREE_TYPE (arg_sg), sg));
12453 TREE_SIDE_EFFECTS (result_sg) = 1;
12454 /* Combine the signgam assignment with the lgamma result. */
12455 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12456 result_sg, result_lg));
12457 }
12458 }
12459 }
12460
12461 return result;
12462 }
12463
12464 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12465 function FUNC on it and return the resulting value as a tree with
12466 type TYPE. The mpfr precision is set to the precision of TYPE. We
12467 assume that function FUNC returns zero if the result could be
12468 calculated exactly within the requested precision. */
12469
12470 static tree
12471 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12472 {
12473 tree result = NULL_TREE;
12474
12475 STRIP_NOPS (arg);
12476
12477 /* To proceed, MPFR must exactly represent the target floating point
12478 format, which only happens when the target base equals two. */
12479 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12480 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12481 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12482 {
12483 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12484 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12485
12486 if (real_isfinite (re) && real_isfinite (im))
12487 {
12488 const struct real_format *const fmt =
12489 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12490 const int prec = fmt->p;
12491 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12492 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12493 int inexact;
12494 mpc_t m;
12495
12496 mpc_init2 (m, prec);
12497 mpfr_from_real (mpc_realref (m), re, rnd);
12498 mpfr_from_real (mpc_imagref (m), im, rnd);
12499 mpfr_clear_flags ();
12500 inexact = func (m, m, crnd);
12501 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12502 mpc_clear (m);
12503 }
12504 }
12505
12506 return result;
12507 }
12508
12509 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12510 mpc function FUNC on it and return the resulting value as a tree
12511 with type TYPE. The mpfr precision is set to the precision of
12512 TYPE. We assume that function FUNC returns zero if the result
12513 could be calculated exactly within the requested precision. If
12514 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12515 in the arguments and/or results. */
12516
12517 tree
12518 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12519 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12520 {
12521 tree result = NULL_TREE;
12522
12523 STRIP_NOPS (arg0);
12524 STRIP_NOPS (arg1);
12525
12526 /* To proceed, MPFR must exactly represent the target floating point
12527 format, which only happens when the target base equals two. */
12528 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12529 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12530 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12531 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12532 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12533 {
12534 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12535 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12536 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12537 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12538
12539 if (do_nonfinite
12540 || (real_isfinite (re0) && real_isfinite (im0)
12541 && real_isfinite (re1) && real_isfinite (im1)))
12542 {
12543 const struct real_format *const fmt =
12544 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12545 const int prec = fmt->p;
12546 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12547 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12548 int inexact;
12549 mpc_t m0, m1;
12550
12551 mpc_init2 (m0, prec);
12552 mpc_init2 (m1, prec);
12553 mpfr_from_real (mpc_realref (m0), re0, rnd);
12554 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12555 mpfr_from_real (mpc_realref (m1), re1, rnd);
12556 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12557 mpfr_clear_flags ();
12558 inexact = func (m0, m0, m1, crnd);
12559 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12560 mpc_clear (m0);
12561 mpc_clear (m1);
12562 }
12563 }
12564
12565 return result;
12566 }
12567
12568 /* A wrapper function for builtin folding that prevents warnings for
12569 "statement without effect" and the like, caused by removing the
12570 call node earlier than the warning is generated. */
12571
12572 tree
12573 fold_call_stmt (gimple stmt, bool ignore)
12574 {
12575 tree ret = NULL_TREE;
12576 tree fndecl = gimple_call_fndecl (stmt);
12577 location_t loc = gimple_location (stmt);
12578 if (fndecl
12579 && TREE_CODE (fndecl) == FUNCTION_DECL
12580 && DECL_BUILT_IN (fndecl)
12581 && !gimple_call_va_arg_pack_p (stmt))
12582 {
12583 int nargs = gimple_call_num_args (stmt);
12584 tree *args = (nargs > 0
12585 ? gimple_call_arg_ptr (stmt, 0)
12586 : &error_mark_node);
12587
12588 if (avoid_folding_inline_builtin (fndecl))
12589 return NULL_TREE;
12590 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12591 {
12592 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12593 }
12594 else
12595 {
12596 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
12597 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12598 if (ret)
12599 {
12600 /* Propagate location information from original call to
12601 expansion of builtin. Otherwise things like
12602 maybe_emit_chk_warning, that operate on the expansion
12603 of a builtin, will use the wrong location information. */
12604 if (gimple_has_location (stmt))
12605 {
12606 tree realret = ret;
12607 if (TREE_CODE (ret) == NOP_EXPR)
12608 realret = TREE_OPERAND (ret, 0);
12609 if (CAN_HAVE_LOCATION_P (realret)
12610 && !EXPR_HAS_LOCATION (realret))
12611 SET_EXPR_LOCATION (realret, loc);
12612 return realret;
12613 }
12614 return ret;
12615 }
12616 }
12617 }
12618 return NULL_TREE;
12619 }
12620
12621 /* Look up the function in builtin_decl that corresponds to DECL
12622 and set ASMSPEC as its user assembler name. DECL must be a
12623 function decl that declares a builtin. */
12624
12625 void
12626 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12627 {
12628 tree builtin;
12629 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12630 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12631 && asmspec != 0);
12632
12633 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12634 set_user_assembler_name (builtin, asmspec);
12635 switch (DECL_FUNCTION_CODE (decl))
12636 {
12637 case BUILT_IN_MEMCPY:
12638 init_block_move_fn (asmspec);
12639 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12640 break;
12641 case BUILT_IN_MEMSET:
12642 init_block_clear_fn (asmspec);
12643 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12644 break;
12645 case BUILT_IN_MEMMOVE:
12646 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12647 break;
12648 case BUILT_IN_MEMCMP:
12649 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12650 break;
12651 case BUILT_IN_ABORT:
12652 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12653 break;
12654 case BUILT_IN_FFS:
12655 if (INT_TYPE_SIZE < BITS_PER_WORD)
12656 {
12657 set_user_assembler_libfunc ("ffs", asmspec);
12658 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12659 MODE_INT, 0), "ffs");
12660 }
12661 break;
12662 default:
12663 break;
12664 }
12665 }
12666
12667 /* Return true if DECL is a builtin that expands to a constant or similarly
12668 simple code. */
12669 bool
12670 is_simple_builtin (tree decl)
12671 {
12672 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12673 switch (DECL_FUNCTION_CODE (decl))
12674 {
12675 /* Builtins that expand to constants. */
12676 case BUILT_IN_CONSTANT_P:
12677 case BUILT_IN_EXPECT:
12678 case BUILT_IN_OBJECT_SIZE:
12679 case BUILT_IN_UNREACHABLE:
12680 /* Simple register moves or loads from stack. */
12681 case BUILT_IN_ASSUME_ALIGNED:
12682 case BUILT_IN_RETURN_ADDRESS:
12683 case BUILT_IN_EXTRACT_RETURN_ADDR:
12684 case BUILT_IN_FROB_RETURN_ADDR:
12685 case BUILT_IN_RETURN:
12686 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12687 case BUILT_IN_FRAME_ADDRESS:
12688 case BUILT_IN_VA_END:
12689 case BUILT_IN_STACK_SAVE:
12690 case BUILT_IN_STACK_RESTORE:
12691 /* Exception state returns or moves registers around. */
12692 case BUILT_IN_EH_FILTER:
12693 case BUILT_IN_EH_POINTER:
12694 case BUILT_IN_EH_COPY_VALUES:
12695 return true;
12696
12697 default:
12698 return false;
12699 }
12700
12701 return false;
12702 }
12703
12704 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12705 most probably expanded inline into reasonably simple code. This is a
12706 superset of is_simple_builtin. */
12707 bool
12708 is_inexpensive_builtin (tree decl)
12709 {
12710 if (!decl)
12711 return false;
12712 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12713 return true;
12714 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12715 switch (DECL_FUNCTION_CODE (decl))
12716 {
12717 case BUILT_IN_ABS:
12718 case BUILT_IN_ALLOCA:
12719 case BUILT_IN_ALLOCA_WITH_ALIGN:
12720 case BUILT_IN_BSWAP16:
12721 case BUILT_IN_BSWAP32:
12722 case BUILT_IN_BSWAP64:
12723 case BUILT_IN_CLZ:
12724 case BUILT_IN_CLZIMAX:
12725 case BUILT_IN_CLZL:
12726 case BUILT_IN_CLZLL:
12727 case BUILT_IN_CTZ:
12728 case BUILT_IN_CTZIMAX:
12729 case BUILT_IN_CTZL:
12730 case BUILT_IN_CTZLL:
12731 case BUILT_IN_FFS:
12732 case BUILT_IN_FFSIMAX:
12733 case BUILT_IN_FFSL:
12734 case BUILT_IN_FFSLL:
12735 case BUILT_IN_IMAXABS:
12736 case BUILT_IN_FINITE:
12737 case BUILT_IN_FINITEF:
12738 case BUILT_IN_FINITEL:
12739 case BUILT_IN_FINITED32:
12740 case BUILT_IN_FINITED64:
12741 case BUILT_IN_FINITED128:
12742 case BUILT_IN_FPCLASSIFY:
12743 case BUILT_IN_ISFINITE:
12744 case BUILT_IN_ISINF_SIGN:
12745 case BUILT_IN_ISINF:
12746 case BUILT_IN_ISINFF:
12747 case BUILT_IN_ISINFL:
12748 case BUILT_IN_ISINFD32:
12749 case BUILT_IN_ISINFD64:
12750 case BUILT_IN_ISINFD128:
12751 case BUILT_IN_ISNAN:
12752 case BUILT_IN_ISNANF:
12753 case BUILT_IN_ISNANL:
12754 case BUILT_IN_ISNAND32:
12755 case BUILT_IN_ISNAND64:
12756 case BUILT_IN_ISNAND128:
12757 case BUILT_IN_ISNORMAL:
12758 case BUILT_IN_ISGREATER:
12759 case BUILT_IN_ISGREATEREQUAL:
12760 case BUILT_IN_ISLESS:
12761 case BUILT_IN_ISLESSEQUAL:
12762 case BUILT_IN_ISLESSGREATER:
12763 case BUILT_IN_ISUNORDERED:
12764 case BUILT_IN_VA_ARG_PACK:
12765 case BUILT_IN_VA_ARG_PACK_LEN:
12766 case BUILT_IN_VA_COPY:
12767 case BUILT_IN_TRAP:
12768 case BUILT_IN_SAVEREGS:
12769 case BUILT_IN_POPCOUNTL:
12770 case BUILT_IN_POPCOUNTLL:
12771 case BUILT_IN_POPCOUNTIMAX:
12772 case BUILT_IN_POPCOUNT:
12773 case BUILT_IN_PARITYL:
12774 case BUILT_IN_PARITYLL:
12775 case BUILT_IN_PARITYIMAX:
12776 case BUILT_IN_PARITY:
12777 case BUILT_IN_LABS:
12778 case BUILT_IN_LLABS:
12779 case BUILT_IN_PREFETCH:
12780 return true;
12781
12782 default:
12783 return is_simple_builtin (decl);
12784 }
12785
12786 return false;
12787 }