gcc/testsuite/
[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 "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "flags.h"
40 #include "regs.h"
41 #include "hard-reg-set.h"
42 #include "except.h"
43 #include "function.h"
44 #include "insn-config.h"
45 #include "expr.h"
46 #include "optabs.h"
47 #include "libfuncs.h"
48 #include "recog.h"
49 #include "output.h"
50 #include "typeclass.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "target.h"
54 #include "langhooks.h"
55 #include "tree-ssanames.h"
56 #include "tree-dfa.h"
57 #include "value-prof.h"
58 #include "diagnostic-core.h"
59 #include "builtins.h"
60 #include "ubsan.h"
61 #include "cilk.h"
62
63
64 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
65
66 struct target_builtins default_target_builtins;
67 #if SWITCHABLE_TARGET
68 struct target_builtins *this_target_builtins = &default_target_builtins;
69 #endif
70
71 /* Define the names of the builtin function types and codes. */
72 const char *const built_in_class_names[BUILT_IN_LAST]
73 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
74
75 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
76 const char * built_in_names[(int) END_BUILTINS] =
77 {
78 #include "builtins.def"
79 };
80 #undef DEF_BUILTIN
81
82 /* Setup an array of _DECL trees, make sure each element is
83 initialized to NULL_TREE. */
84 builtin_info_type builtin_info;
85
86 /* Non-zero if __builtin_constant_p should be folded right away. */
87 bool force_folding_builtin_constant_p;
88
89 static const char *c_getstr (tree);
90 static rtx c_readstr (const char *, enum machine_mode);
91 static int target_char_cast (tree, char *);
92 static rtx get_memory_rtx (tree, tree);
93 static int apply_args_size (void);
94 static int apply_result_size (void);
95 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
96 static rtx result_vector (int, rtx);
97 #endif
98 static void expand_builtin_update_setjmp_buf (rtx);
99 static void expand_builtin_prefetch (tree);
100 static rtx expand_builtin_apply_args (void);
101 static rtx expand_builtin_apply_args_1 (void);
102 static rtx expand_builtin_apply (rtx, rtx, rtx);
103 static void expand_builtin_return (rtx);
104 static enum type_class type_to_class (tree);
105 static rtx expand_builtin_classify_type (tree);
106 static void expand_errno_check (tree, rtx);
107 static rtx expand_builtin_mathfn (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
109 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
110 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
111 static rtx expand_builtin_interclass_mathfn (tree, rtx);
112 static rtx expand_builtin_sincos (tree);
113 static rtx expand_builtin_cexpi (tree, rtx);
114 static rtx expand_builtin_int_roundingfn (tree, rtx);
115 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
116 static rtx expand_builtin_next_arg (void);
117 static rtx expand_builtin_va_start (tree);
118 static rtx expand_builtin_va_end (tree);
119 static rtx expand_builtin_va_copy (tree);
120 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strcmp (tree, rtx);
122 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
123 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memcpy (tree, rtx);
125 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
127 enum machine_mode, int);
128 static rtx expand_builtin_strcpy (tree, rtx);
129 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
130 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_strncpy (tree, rtx);
132 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
133 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
135 static rtx expand_builtin_bzero (tree);
136 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_alloca (tree, bool);
138 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
139 static rtx expand_builtin_frame_address (tree, tree);
140 static tree stabilize_va_list_loc (location_t, tree, int);
141 static rtx expand_builtin_expect (tree, rtx);
142 static tree fold_builtin_constant_p (tree);
143 static tree fold_builtin_classify_type (tree);
144 static tree fold_builtin_strlen (location_t, tree, tree);
145 static tree fold_builtin_inf (location_t, tree, int);
146 static tree fold_builtin_nan (tree, tree, int);
147 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
148 static bool validate_arg (const_tree, enum tree_code code);
149 static bool integer_valued_real_p (tree);
150 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
151 static bool readonly_data_expr (tree);
152 static rtx expand_builtin_fabs (tree, rtx, rtx);
153 static rtx expand_builtin_signbit (tree, rtx);
154 static tree fold_builtin_sqrt (location_t, tree, tree);
155 static tree fold_builtin_cbrt (location_t, tree, tree);
156 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
158 static tree fold_builtin_cos (location_t, tree, tree, tree);
159 static tree fold_builtin_cosh (location_t, tree, tree, tree);
160 static tree fold_builtin_tan (tree, tree);
161 static tree fold_builtin_trunc (location_t, tree, tree);
162 static tree fold_builtin_floor (location_t, tree, tree);
163 static tree fold_builtin_ceil (location_t, tree, tree);
164 static tree fold_builtin_round (location_t, tree, tree);
165 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
166 static tree fold_builtin_bitop (tree, tree);
167 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
168 static tree fold_builtin_strchr (location_t, tree, tree, tree);
169 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
170 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
171 static tree fold_builtin_strcmp (location_t, tree, tree);
172 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
173 static tree fold_builtin_signbit (location_t, tree, tree);
174 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
175 static tree fold_builtin_isascii (location_t, tree);
176 static tree fold_builtin_toascii (location_t, tree);
177 static tree fold_builtin_isdigit (location_t, tree);
178 static tree fold_builtin_fabs (location_t, tree, tree);
179 static tree fold_builtin_abs (location_t, tree, tree);
180 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
181 enum tree_code);
182 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
183 static tree fold_builtin_0 (location_t, tree, bool);
184 static tree fold_builtin_1 (location_t, tree, tree, bool);
185 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
186 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
187 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
188 static tree fold_builtin_varargs (location_t, tree, tree, bool);
189
190 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
191 static tree fold_builtin_strstr (location_t, tree, tree, tree);
192 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
193 static tree fold_builtin_strncat (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
196 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
197 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
198
199 static rtx expand_builtin_object_size (tree);
200 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
201 enum built_in_function);
202 static void maybe_emit_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
204 static void maybe_emit_free_warning (tree);
205 static tree fold_builtin_object_size (tree, tree);
206 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
207 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
208 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
209 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
210 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
211 enum built_in_function);
212 static bool init_target_chars (void);
213
214 static unsigned HOST_WIDE_INT target_newline;
215 static unsigned HOST_WIDE_INT target_percent;
216 static unsigned HOST_WIDE_INT target_c;
217 static unsigned HOST_WIDE_INT target_s;
218 static char target_percent_c[3];
219 static char target_percent_s[3];
220 static char target_percent_s_newline[4];
221 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
222 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
223 static tree do_mpfr_arg2 (tree, tree, tree,
224 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
225 static tree do_mpfr_arg3 (tree, tree, tree, tree,
226 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
227 static tree do_mpfr_sincos (tree, tree, tree);
228 static tree do_mpfr_bessel_n (tree, tree, tree,
229 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
230 const REAL_VALUE_TYPE *, bool);
231 static tree do_mpfr_remquo (tree, tree, tree);
232 static tree do_mpfr_lgamma_r (tree, tree, tree);
233 static void expand_builtin_sync_synchronize (void);
234
235 /* Return true if NAME starts with __builtin_ or __sync_. */
236
237 static bool
238 is_builtin_name (const char *name)
239 {
240 if (strncmp (name, "__builtin_", 10) == 0)
241 return true;
242 if (strncmp (name, "__sync_", 7) == 0)
243 return true;
244 if (strncmp (name, "__atomic_", 9) == 0)
245 return true;
246 if (flag_cilkplus
247 && (!strcmp (name, "__cilkrts_detach")
248 || !strcmp (name, "__cilkrts_pop_frame")))
249 return true;
250 return false;
251 }
252
253
254 /* Return true if DECL is a function symbol representing a built-in. */
255
256 bool
257 is_builtin_fn (tree decl)
258 {
259 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
260 }
261
262 /* Return true if NODE should be considered for inline expansion regardless
263 of the optimization level. This means whenever a function is invoked with
264 its "internal" name, which normally contains the prefix "__builtin". */
265
266 static bool
267 called_as_built_in (tree node)
268 {
269 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
270 we want the name used to call the function, not the name it
271 will have. */
272 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
273 return is_builtin_name (name);
274 }
275
276 /* Compute values M and N such that M divides (address of EXP - N) and such
277 that N < M. If these numbers can be determined, store M in alignp and N in
278 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
279 *alignp and any bit-offset to *bitposp.
280
281 Note that the address (and thus the alignment) computed here is based
282 on the address to which a symbol resolves, whereas DECL_ALIGN is based
283 on the address at which an object is actually located. These two
284 addresses are not always the same. For example, on ARM targets,
285 the address &foo of a Thumb function foo() has the lowest bit set,
286 whereas foo() itself starts on an even address.
287
288 If ADDR_P is true we are taking the address of the memory reference EXP
289 and thus cannot rely on the access taking place. */
290
291 static bool
292 get_object_alignment_2 (tree exp, unsigned int *alignp,
293 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
294 {
295 HOST_WIDE_INT bitsize, bitpos;
296 tree offset;
297 enum machine_mode mode;
298 int unsignedp, volatilep;
299 unsigned int align = BITS_PER_UNIT;
300 bool known_alignment = false;
301
302 /* Get the innermost object and the constant (bitpos) and possibly
303 variable (offset) offset of the access. */
304 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
305 &mode, &unsignedp, &volatilep, true);
306
307 /* Extract alignment information from the innermost object and
308 possibly adjust bitpos and offset. */
309 if (TREE_CODE (exp) == FUNCTION_DECL)
310 {
311 /* Function addresses can encode extra information besides their
312 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
313 allows the low bit to be used as a virtual bit, we know
314 that the address itself must be at least 2-byte aligned. */
315 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
316 align = 2 * BITS_PER_UNIT;
317 }
318 else if (TREE_CODE (exp) == LABEL_DECL)
319 ;
320 else if (TREE_CODE (exp) == CONST_DECL)
321 {
322 /* The alignment of a CONST_DECL is determined by its initializer. */
323 exp = DECL_INITIAL (exp);
324 align = TYPE_ALIGN (TREE_TYPE (exp));
325 #ifdef CONSTANT_ALIGNMENT
326 if (CONSTANT_CLASS_P (exp))
327 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
328 #endif
329 known_alignment = true;
330 }
331 else if (DECL_P (exp))
332 {
333 align = DECL_ALIGN (exp);
334 known_alignment = true;
335 }
336 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
337 {
338 align = TYPE_ALIGN (TREE_TYPE (exp));
339 }
340 else if (TREE_CODE (exp) == INDIRECT_REF
341 || TREE_CODE (exp) == MEM_REF
342 || TREE_CODE (exp) == TARGET_MEM_REF)
343 {
344 tree addr = TREE_OPERAND (exp, 0);
345 unsigned ptr_align;
346 unsigned HOST_WIDE_INT ptr_bitpos;
347
348 if (TREE_CODE (addr) == BIT_AND_EXPR
349 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
350 {
351 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
352 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
353 align *= BITS_PER_UNIT;
354 addr = TREE_OPERAND (addr, 0);
355 }
356
357 known_alignment
358 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
359 align = MAX (ptr_align, align);
360
361 /* The alignment of the pointer operand in a TARGET_MEM_REF
362 has to take the variable offset parts into account. */
363 if (TREE_CODE (exp) == TARGET_MEM_REF)
364 {
365 if (TMR_INDEX (exp))
366 {
367 unsigned HOST_WIDE_INT step = 1;
368 if (TMR_STEP (exp))
369 step = TREE_INT_CST_LOW (TMR_STEP (exp));
370 align = MIN (align, (step & -step) * BITS_PER_UNIT);
371 }
372 if (TMR_INDEX2 (exp))
373 align = BITS_PER_UNIT;
374 known_alignment = false;
375 }
376
377 /* When EXP is an actual memory reference then we can use
378 TYPE_ALIGN of a pointer indirection to derive alignment.
379 Do so only if get_pointer_alignment_1 did not reveal absolute
380 alignment knowledge and if using that alignment would
381 improve the situation. */
382 if (!addr_p && !known_alignment
383 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
384 align = TYPE_ALIGN (TREE_TYPE (exp));
385 else
386 {
387 /* Else adjust bitpos accordingly. */
388 bitpos += ptr_bitpos;
389 if (TREE_CODE (exp) == MEM_REF
390 || TREE_CODE (exp) == TARGET_MEM_REF)
391 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
392 }
393 }
394 else if (TREE_CODE (exp) == STRING_CST)
395 {
396 /* STRING_CST are the only constant objects we allow to be not
397 wrapped inside a CONST_DECL. */
398 align = TYPE_ALIGN (TREE_TYPE (exp));
399 #ifdef CONSTANT_ALIGNMENT
400 if (CONSTANT_CLASS_P (exp))
401 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
402 #endif
403 known_alignment = true;
404 }
405
406 /* If there is a non-constant offset part extract the maximum
407 alignment that can prevail. */
408 if (offset)
409 {
410 unsigned int trailing_zeros = tree_ctz (offset);
411 if (trailing_zeros < HOST_BITS_PER_INT)
412 {
413 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
414 if (inner)
415 align = MIN (align, inner);
416 }
417 }
418
419 *alignp = align;
420 *bitposp = bitpos & (*alignp - 1);
421 return known_alignment;
422 }
423
424 /* For a memory reference expression EXP compute values M and N such that M
425 divides (&EXP - N) and such that N < M. If these numbers can be determined,
426 store M in alignp and N in *BITPOSP and return true. Otherwise return false
427 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
428
429 bool
430 get_object_alignment_1 (tree exp, unsigned int *alignp,
431 unsigned HOST_WIDE_INT *bitposp)
432 {
433 return get_object_alignment_2 (exp, alignp, bitposp, false);
434 }
435
436 /* Return the alignment in bits of EXP, an object. */
437
438 unsigned int
439 get_object_alignment (tree exp)
440 {
441 unsigned HOST_WIDE_INT bitpos = 0;
442 unsigned int align;
443
444 get_object_alignment_1 (exp, &align, &bitpos);
445
446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
448
449 if (bitpos != 0)
450 align = (bitpos & -bitpos);
451 return align;
452 }
453
454 /* For a pointer valued expression EXP compute values M and N such that M
455 divides (EXP - N) and such that N < M. If these numbers can be determined,
456 store M in alignp and N in *BITPOSP and return true. Return false if
457 the results are just a conservative approximation.
458
459 If EXP is not a pointer, false is returned too. */
460
461 bool
462 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
463 unsigned HOST_WIDE_INT *bitposp)
464 {
465 STRIP_NOPS (exp);
466
467 if (TREE_CODE (exp) == ADDR_EXPR)
468 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
469 alignp, bitposp, true);
470 else if (TREE_CODE (exp) == SSA_NAME
471 && POINTER_TYPE_P (TREE_TYPE (exp)))
472 {
473 unsigned int ptr_align, ptr_misalign;
474 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
475
476 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
477 {
478 *bitposp = ptr_misalign * BITS_PER_UNIT;
479 *alignp = ptr_align * BITS_PER_UNIT;
480 /* We cannot really tell whether this result is an approximation. */
481 return true;
482 }
483 else
484 {
485 *bitposp = 0;
486 *alignp = BITS_PER_UNIT;
487 return false;
488 }
489 }
490 else if (TREE_CODE (exp) == INTEGER_CST)
491 {
492 *alignp = BIGGEST_ALIGNMENT;
493 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
494 & (BIGGEST_ALIGNMENT - 1));
495 return true;
496 }
497
498 *bitposp = 0;
499 *alignp = BITS_PER_UNIT;
500 return false;
501 }
502
503 /* Return the alignment in bits of EXP, a pointer valued expression.
504 The alignment returned is, by default, the alignment of the thing that
505 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
506
507 Otherwise, look at the expression to see if we can do better, i.e., if the
508 expression is actually pointing at an object whose alignment is tighter. */
509
510 unsigned int
511 get_pointer_alignment (tree exp)
512 {
513 unsigned HOST_WIDE_INT bitpos = 0;
514 unsigned int align;
515
516 get_pointer_alignment_1 (exp, &align, &bitpos);
517
518 /* align and bitpos now specify known low bits of the pointer.
519 ptr & (align - 1) == bitpos. */
520
521 if (bitpos != 0)
522 align = (bitpos & -bitpos);
523
524 return align;
525 }
526
527 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
528 way, because it could contain a zero byte in the middle.
529 TREE_STRING_LENGTH is the size of the character array, not the string.
530
531 ONLY_VALUE should be nonzero if the result is not going to be emitted
532 into the instruction stream and zero if it is going to be expanded.
533 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
534 is returned, otherwise NULL, since
535 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
536 evaluate the side-effects.
537
538 The value returned is of type `ssizetype'.
539
540 Unfortunately, string_constant can't access the values of const char
541 arrays with initializers, so neither can we do so here. */
542
543 tree
544 c_strlen (tree src, int only_value)
545 {
546 tree offset_node;
547 HOST_WIDE_INT offset;
548 int max;
549 const char *ptr;
550 location_t loc;
551
552 STRIP_NOPS (src);
553 if (TREE_CODE (src) == COND_EXPR
554 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
555 {
556 tree len1, len2;
557
558 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
559 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
560 if (tree_int_cst_equal (len1, len2))
561 return len1;
562 }
563
564 if (TREE_CODE (src) == COMPOUND_EXPR
565 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
566 return c_strlen (TREE_OPERAND (src, 1), only_value);
567
568 loc = EXPR_LOC_OR_LOC (src, input_location);
569
570 src = string_constant (src, &offset_node);
571 if (src == 0)
572 return NULL_TREE;
573
574 max = TREE_STRING_LENGTH (src) - 1;
575 ptr = TREE_STRING_POINTER (src);
576
577 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
578 {
579 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
580 compute the offset to the following null if we don't know where to
581 start searching for it. */
582 int i;
583
584 for (i = 0; i < max; i++)
585 if (ptr[i] == 0)
586 return NULL_TREE;
587
588 /* We don't know the starting offset, but we do know that the string
589 has no internal zero bytes. We can assume that the offset falls
590 within the bounds of the string; otherwise, the programmer deserves
591 what he gets. Subtract the offset from the length of the string,
592 and return that. This would perhaps not be valid if we were dealing
593 with named arrays in addition to literal string constants. */
594
595 return size_diffop_loc (loc, size_int (max), offset_node);
596 }
597
598 /* We have a known offset into the string. Start searching there for
599 a null character if we can represent it as a single HOST_WIDE_INT. */
600 if (offset_node == 0)
601 offset = 0;
602 else if (! tree_fits_shwi_p (offset_node))
603 offset = -1;
604 else
605 offset = tree_to_shwi (offset_node);
606
607 /* If the offset is known to be out of bounds, warn, and call strlen at
608 runtime. */
609 if (offset < 0 || offset > max)
610 {
611 /* Suppress multiple warnings for propagated constant strings. */
612 if (! TREE_NO_WARNING (src))
613 {
614 warning_at (loc, 0, "offset outside bounds of constant string");
615 TREE_NO_WARNING (src) = 1;
616 }
617 return NULL_TREE;
618 }
619
620 /* Use strlen to search for the first zero byte. Since any strings
621 constructed with build_string will have nulls appended, we win even
622 if we get handed something like (char[4])"abcd".
623
624 Since OFFSET is our starting index into the string, no further
625 calculation is needed. */
626 return ssize_int (strlen (ptr + offset));
627 }
628
629 /* Return a char pointer for a C string if it is a string constant
630 or sum of string constant and integer constant. */
631
632 static const char *
633 c_getstr (tree src)
634 {
635 tree offset_node;
636
637 src = string_constant (src, &offset_node);
638 if (src == 0)
639 return 0;
640
641 if (offset_node == 0)
642 return TREE_STRING_POINTER (src);
643 else if (!tree_fits_uhwi_p (offset_node)
644 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
645 return 0;
646
647 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
648 }
649
650 /* Return a constant integer corresponding to target reading
651 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
652
653 static rtx
654 c_readstr (const char *str, enum machine_mode mode)
655 {
656 HOST_WIDE_INT ch;
657 unsigned int i, j;
658 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
659
660 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
661 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
662 / HOST_BITS_PER_WIDE_INT;
663
664 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
665 for (i = 0; i < len; i++)
666 tmp[i] = 0;
667
668 ch = 1;
669 for (i = 0; i < GET_MODE_SIZE (mode); i++)
670 {
671 j = i;
672 if (WORDS_BIG_ENDIAN)
673 j = GET_MODE_SIZE (mode) - i - 1;
674 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
675 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
676 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
677 j *= BITS_PER_UNIT;
678
679 if (ch)
680 ch = (unsigned char) str[i];
681 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
682 }
683
684 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
685 return immed_wide_int_const (c, mode);
686 }
687
688 /* Cast a target constant CST to target CHAR and if that value fits into
689 host char type, return zero and put that value into variable pointed to by
690 P. */
691
692 static int
693 target_char_cast (tree cst, char *p)
694 {
695 unsigned HOST_WIDE_INT val, hostval;
696
697 if (TREE_CODE (cst) != INTEGER_CST
698 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
699 return 1;
700
701 /* Do not care if it fits or not right here. */
702 val = TREE_INT_CST_LOW (cst);
703
704 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
705 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
706
707 hostval = val;
708 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
709 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
710
711 if (val != hostval)
712 return 1;
713
714 *p = hostval;
715 return 0;
716 }
717
718 /* Similar to save_expr, but assumes that arbitrary code is not executed
719 in between the multiple evaluations. In particular, we assume that a
720 non-addressable local variable will not be modified. */
721
722 static tree
723 builtin_save_expr (tree exp)
724 {
725 if (TREE_CODE (exp) == SSA_NAME
726 || (TREE_ADDRESSABLE (exp) == 0
727 && (TREE_CODE (exp) == PARM_DECL
728 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
729 return exp;
730
731 return save_expr (exp);
732 }
733
734 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
735 times to get the address of either a higher stack frame, or a return
736 address located within it (depending on FNDECL_CODE). */
737
738 static rtx
739 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
740 {
741 int i;
742
743 #ifdef INITIAL_FRAME_ADDRESS_RTX
744 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
745 #else
746 rtx tem;
747
748 /* For a zero count with __builtin_return_address, we don't care what
749 frame address we return, because target-specific definitions will
750 override us. Therefore frame pointer elimination is OK, and using
751 the soft frame pointer is OK.
752
753 For a nonzero count, or a zero count with __builtin_frame_address,
754 we require a stable offset from the current frame pointer to the
755 previous one, so we must use the hard frame pointer, and
756 we must disable frame pointer elimination. */
757 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
758 tem = frame_pointer_rtx;
759 else
760 {
761 tem = hard_frame_pointer_rtx;
762
763 /* Tell reload not to eliminate the frame pointer. */
764 crtl->accesses_prior_frames = 1;
765 }
766 #endif
767
768 /* Some machines need special handling before we can access
769 arbitrary frames. For example, on the SPARC, we must first flush
770 all register windows to the stack. */
771 #ifdef SETUP_FRAME_ADDRESSES
772 if (count > 0)
773 SETUP_FRAME_ADDRESSES ();
774 #endif
775
776 /* On the SPARC, the return address is not in the frame, it is in a
777 register. There is no way to access it off of the current frame
778 pointer, but it can be accessed off the previous frame pointer by
779 reading the value from the register window save area. */
780 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
781 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
782 count--;
783 #endif
784
785 /* Scan back COUNT frames to the specified frame. */
786 for (i = 0; i < count; i++)
787 {
788 /* Assume the dynamic chain pointer is in the word that the
789 frame address points to, unless otherwise specified. */
790 #ifdef DYNAMIC_CHAIN_ADDRESS
791 tem = DYNAMIC_CHAIN_ADDRESS (tem);
792 #endif
793 tem = memory_address (Pmode, tem);
794 tem = gen_frame_mem (Pmode, tem);
795 tem = copy_to_reg (tem);
796 }
797
798 /* For __builtin_frame_address, return what we've got. But, on
799 the SPARC for example, we may have to add a bias. */
800 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
801 #ifdef FRAME_ADDR_RTX
802 return FRAME_ADDR_RTX (tem);
803 #else
804 return tem;
805 #endif
806
807 /* For __builtin_return_address, get the return address from that frame. */
808 #ifdef RETURN_ADDR_RTX
809 tem = RETURN_ADDR_RTX (count, tem);
810 #else
811 tem = memory_address (Pmode,
812 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
813 tem = gen_frame_mem (Pmode, tem);
814 #endif
815 return tem;
816 }
817
818 /* Alias set used for setjmp buffer. */
819 static alias_set_type setjmp_alias_set = -1;
820
821 /* Construct the leading half of a __builtin_setjmp call. Control will
822 return to RECEIVER_LABEL. This is also called directly by the SJLJ
823 exception handling code. */
824
825 void
826 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
827 {
828 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
829 rtx stack_save;
830 rtx mem;
831
832 if (setjmp_alias_set == -1)
833 setjmp_alias_set = new_alias_set ();
834
835 buf_addr = convert_memory_address (Pmode, buf_addr);
836
837 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
838
839 /* We store the frame pointer and the address of receiver_label in
840 the buffer and use the rest of it for the stack save area, which
841 is machine-dependent. */
842
843 mem = gen_rtx_MEM (Pmode, buf_addr);
844 set_mem_alias_set (mem, setjmp_alias_set);
845 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
846
847 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
848 GET_MODE_SIZE (Pmode))),
849 set_mem_alias_set (mem, setjmp_alias_set);
850
851 emit_move_insn (validize_mem (mem),
852 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
853
854 stack_save = gen_rtx_MEM (sa_mode,
855 plus_constant (Pmode, buf_addr,
856 2 * GET_MODE_SIZE (Pmode)));
857 set_mem_alias_set (stack_save, setjmp_alias_set);
858 emit_stack_save (SAVE_NONLOCAL, &stack_save);
859
860 /* If there is further processing to do, do it. */
861 #ifdef HAVE_builtin_setjmp_setup
862 if (HAVE_builtin_setjmp_setup)
863 emit_insn (gen_builtin_setjmp_setup (buf_addr));
864 #endif
865
866 /* We have a nonlocal label. */
867 cfun->has_nonlocal_label = 1;
868 }
869
870 /* Construct the trailing part of a __builtin_setjmp call. This is
871 also called directly by the SJLJ exception handling code.
872 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
873
874 void
875 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
876 {
877 rtx chain;
878
879 /* Mark the FP as used when we get here, so we have to make sure it's
880 marked as used by this function. */
881 emit_use (hard_frame_pointer_rtx);
882
883 /* Mark the static chain as clobbered here so life information
884 doesn't get messed up for it. */
885 chain = targetm.calls.static_chain (current_function_decl, true);
886 if (chain && REG_P (chain))
887 emit_clobber (chain);
888
889 /* Now put in the code to restore the frame pointer, and argument
890 pointer, if needed. */
891 #ifdef HAVE_nonlocal_goto
892 if (! HAVE_nonlocal_goto)
893 #endif
894 {
895 /* First adjust our frame pointer to its actual value. It was
896 previously set to the start of the virtual area corresponding to
897 the stacked variables when we branched here and now needs to be
898 adjusted to the actual hardware fp value.
899
900 Assignments to virtual registers are converted by
901 instantiate_virtual_regs into the corresponding assignment
902 to the underlying register (fp in this case) that makes
903 the original assignment true.
904 So the following insn will actually be decrementing fp by
905 STARTING_FRAME_OFFSET. */
906 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
907
908 /* Restoring the frame pointer also modifies the hard frame pointer.
909 Mark it used (so that the previous assignment remains live once
910 the frame pointer is eliminated) and clobbered (to represent the
911 implicit update from the assignment). */
912 emit_use (hard_frame_pointer_rtx);
913 emit_clobber (hard_frame_pointer_rtx);
914 }
915
916 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
917 if (fixed_regs[ARG_POINTER_REGNUM])
918 {
919 #ifdef ELIMINABLE_REGS
920 /* If the argument pointer can be eliminated in favor of the
921 frame pointer, we don't need to restore it. We assume here
922 that if such an elimination is present, it can always be used.
923 This is the case on all known machines; if we don't make this
924 assumption, we do unnecessary saving on many machines. */
925 size_t i;
926 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
927
928 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
929 if (elim_regs[i].from == ARG_POINTER_REGNUM
930 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
931 break;
932
933 if (i == ARRAY_SIZE (elim_regs))
934 #endif
935 {
936 /* Now restore our arg pointer from the address at which it
937 was saved in our stack frame. */
938 emit_move_insn (crtl->args.internal_arg_pointer,
939 copy_to_reg (get_arg_pointer_save_area ()));
940 }
941 }
942 #endif
943
944 #ifdef HAVE_builtin_setjmp_receiver
945 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
946 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
947 else
948 #endif
949 #ifdef HAVE_nonlocal_goto_receiver
950 if (HAVE_nonlocal_goto_receiver)
951 emit_insn (gen_nonlocal_goto_receiver ());
952 else
953 #endif
954 { /* Nothing */ }
955
956 /* We must not allow the code we just generated to be reordered by
957 scheduling. Specifically, the update of the frame pointer must
958 happen immediately, not later. */
959 emit_insn (gen_blockage ());
960 }
961
962 /* __builtin_longjmp is passed a pointer to an array of five words (not
963 all will be used on all machines). It operates similarly to the C
964 library function of the same name, but is more efficient. Much of
965 the code below is copied from the handling of non-local gotos. */
966
967 static void
968 expand_builtin_longjmp (rtx buf_addr, rtx value)
969 {
970 rtx fp, lab, stack, insn, last;
971 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
972
973 /* DRAP is needed for stack realign if longjmp is expanded to current
974 function */
975 if (SUPPORTS_STACK_ALIGNMENT)
976 crtl->need_drap = true;
977
978 if (setjmp_alias_set == -1)
979 setjmp_alias_set = new_alias_set ();
980
981 buf_addr = convert_memory_address (Pmode, buf_addr);
982
983 buf_addr = force_reg (Pmode, buf_addr);
984
985 /* We require that the user must pass a second argument of 1, because
986 that is what builtin_setjmp will return. */
987 gcc_assert (value == const1_rtx);
988
989 last = get_last_insn ();
990 #ifdef HAVE_builtin_longjmp
991 if (HAVE_builtin_longjmp)
992 emit_insn (gen_builtin_longjmp (buf_addr));
993 else
994 #endif
995 {
996 fp = gen_rtx_MEM (Pmode, buf_addr);
997 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
998 GET_MODE_SIZE (Pmode)));
999
1000 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1001 2 * GET_MODE_SIZE (Pmode)));
1002 set_mem_alias_set (fp, setjmp_alias_set);
1003 set_mem_alias_set (lab, setjmp_alias_set);
1004 set_mem_alias_set (stack, setjmp_alias_set);
1005
1006 /* Pick up FP, label, and SP from the block and jump. This code is
1007 from expand_goto in stmt.c; see there for detailed comments. */
1008 #ifdef HAVE_nonlocal_goto
1009 if (HAVE_nonlocal_goto)
1010 /* We have to pass a value to the nonlocal_goto pattern that will
1011 get copied into the static_chain pointer, but it does not matter
1012 what that value is, because builtin_setjmp does not use it. */
1013 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1014 else
1015 #endif
1016 {
1017 lab = copy_to_reg (lab);
1018
1019 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1020 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1021
1022 emit_move_insn (hard_frame_pointer_rtx, fp);
1023 emit_stack_restore (SAVE_NONLOCAL, stack);
1024
1025 emit_use (hard_frame_pointer_rtx);
1026 emit_use (stack_pointer_rtx);
1027 emit_indirect_jump (lab);
1028 }
1029 }
1030
1031 /* Search backwards and mark the jump insn as a non-local goto.
1032 Note that this precludes the use of __builtin_longjmp to a
1033 __builtin_setjmp target in the same function. However, we've
1034 already cautioned the user that these functions are for
1035 internal exception handling use only. */
1036 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1037 {
1038 gcc_assert (insn != last);
1039
1040 if (JUMP_P (insn))
1041 {
1042 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1043 break;
1044 }
1045 else if (CALL_P (insn))
1046 break;
1047 }
1048 }
1049
1050 static inline bool
1051 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1052 {
1053 return (iter->i < iter->n);
1054 }
1055
1056 /* This function validates the types of a function call argument list
1057 against a specified list of tree_codes. If the last specifier is a 0,
1058 that represents an ellipses, otherwise the last specifier must be a
1059 VOID_TYPE. */
1060
1061 static bool
1062 validate_arglist (const_tree callexpr, ...)
1063 {
1064 enum tree_code code;
1065 bool res = 0;
1066 va_list ap;
1067 const_call_expr_arg_iterator iter;
1068 const_tree arg;
1069
1070 va_start (ap, callexpr);
1071 init_const_call_expr_arg_iterator (callexpr, &iter);
1072
1073 do
1074 {
1075 code = (enum tree_code) va_arg (ap, int);
1076 switch (code)
1077 {
1078 case 0:
1079 /* This signifies an ellipses, any further arguments are all ok. */
1080 res = true;
1081 goto end;
1082 case VOID_TYPE:
1083 /* This signifies an endlink, if no arguments remain, return
1084 true, otherwise return false. */
1085 res = !more_const_call_expr_args_p (&iter);
1086 goto end;
1087 default:
1088 /* If no parameters remain or the parameter's code does not
1089 match the specified code, return false. Otherwise continue
1090 checking any remaining arguments. */
1091 arg = next_const_call_expr_arg (&iter);
1092 if (!validate_arg (arg, code))
1093 goto end;
1094 break;
1095 }
1096 }
1097 while (1);
1098
1099 /* We need gotos here since we can only have one VA_CLOSE in a
1100 function. */
1101 end: ;
1102 va_end (ap);
1103
1104 return res;
1105 }
1106
1107 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1108 and the address of the save area. */
1109
1110 static rtx
1111 expand_builtin_nonlocal_goto (tree exp)
1112 {
1113 tree t_label, t_save_area;
1114 rtx r_label, r_save_area, r_fp, r_sp, insn;
1115
1116 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1117 return NULL_RTX;
1118
1119 t_label = CALL_EXPR_ARG (exp, 0);
1120 t_save_area = CALL_EXPR_ARG (exp, 1);
1121
1122 r_label = expand_normal (t_label);
1123 r_label = convert_memory_address (Pmode, r_label);
1124 r_save_area = expand_normal (t_save_area);
1125 r_save_area = convert_memory_address (Pmode, r_save_area);
1126 /* Copy the address of the save location to a register just in case it was
1127 based on the frame pointer. */
1128 r_save_area = copy_to_reg (r_save_area);
1129 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1130 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1131 plus_constant (Pmode, r_save_area,
1132 GET_MODE_SIZE (Pmode)));
1133
1134 crtl->has_nonlocal_goto = 1;
1135
1136 #ifdef HAVE_nonlocal_goto
1137 /* ??? We no longer need to pass the static chain value, afaik. */
1138 if (HAVE_nonlocal_goto)
1139 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1140 else
1141 #endif
1142 {
1143 r_label = copy_to_reg (r_label);
1144
1145 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1146 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1147
1148 /* Restore frame pointer for containing function. */
1149 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1150 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1151
1152 /* USE of hard_frame_pointer_rtx added for consistency;
1153 not clear if really needed. */
1154 emit_use (hard_frame_pointer_rtx);
1155 emit_use (stack_pointer_rtx);
1156
1157 /* If the architecture is using a GP register, we must
1158 conservatively assume that the target function makes use of it.
1159 The prologue of functions with nonlocal gotos must therefore
1160 initialize the GP register to the appropriate value, and we
1161 must then make sure that this value is live at the point
1162 of the jump. (Note that this doesn't necessarily apply
1163 to targets with a nonlocal_goto pattern; they are free
1164 to implement it in their own way. Note also that this is
1165 a no-op if the GP register is a global invariant.) */
1166 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1167 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1168 emit_use (pic_offset_table_rtx);
1169
1170 emit_indirect_jump (r_label);
1171 }
1172
1173 /* Search backwards to the jump insn and mark it as a
1174 non-local goto. */
1175 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1176 {
1177 if (JUMP_P (insn))
1178 {
1179 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1180 break;
1181 }
1182 else if (CALL_P (insn))
1183 break;
1184 }
1185
1186 return const0_rtx;
1187 }
1188
1189 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1190 (not all will be used on all machines) that was passed to __builtin_setjmp.
1191 It updates the stack pointer in that block to correspond to the current
1192 stack pointer. */
1193
1194 static void
1195 expand_builtin_update_setjmp_buf (rtx buf_addr)
1196 {
1197 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1198 rtx stack_save
1199 = gen_rtx_MEM (sa_mode,
1200 memory_address
1201 (sa_mode,
1202 plus_constant (Pmode, buf_addr,
1203 2 * GET_MODE_SIZE (Pmode))));
1204
1205 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1206 }
1207
1208 /* Expand a call to __builtin_prefetch. For a target that does not support
1209 data prefetch, evaluate the memory address argument in case it has side
1210 effects. */
1211
1212 static void
1213 expand_builtin_prefetch (tree exp)
1214 {
1215 tree arg0, arg1, arg2;
1216 int nargs;
1217 rtx op0, op1, op2;
1218
1219 if (!validate_arglist (exp, POINTER_TYPE, 0))
1220 return;
1221
1222 arg0 = CALL_EXPR_ARG (exp, 0);
1223
1224 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1225 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1226 locality). */
1227 nargs = call_expr_nargs (exp);
1228 if (nargs > 1)
1229 arg1 = CALL_EXPR_ARG (exp, 1);
1230 else
1231 arg1 = integer_zero_node;
1232 if (nargs > 2)
1233 arg2 = CALL_EXPR_ARG (exp, 2);
1234 else
1235 arg2 = integer_three_node;
1236
1237 /* Argument 0 is an address. */
1238 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1239
1240 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1241 if (TREE_CODE (arg1) != INTEGER_CST)
1242 {
1243 error ("second argument to %<__builtin_prefetch%> must be a constant");
1244 arg1 = integer_zero_node;
1245 }
1246 op1 = expand_normal (arg1);
1247 /* Argument 1 must be either zero or one. */
1248 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1249 {
1250 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1251 " using zero");
1252 op1 = const0_rtx;
1253 }
1254
1255 /* Argument 2 (locality) must be a compile-time constant int. */
1256 if (TREE_CODE (arg2) != INTEGER_CST)
1257 {
1258 error ("third argument to %<__builtin_prefetch%> must be a constant");
1259 arg2 = integer_zero_node;
1260 }
1261 op2 = expand_normal (arg2);
1262 /* Argument 2 must be 0, 1, 2, or 3. */
1263 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1264 {
1265 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1266 op2 = const0_rtx;
1267 }
1268
1269 #ifdef HAVE_prefetch
1270 if (HAVE_prefetch)
1271 {
1272 struct expand_operand ops[3];
1273
1274 create_address_operand (&ops[0], op0);
1275 create_integer_operand (&ops[1], INTVAL (op1));
1276 create_integer_operand (&ops[2], INTVAL (op2));
1277 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1278 return;
1279 }
1280 #endif
1281
1282 /* Don't do anything with direct references to volatile memory, but
1283 generate code to handle other side effects. */
1284 if (!MEM_P (op0) && side_effects_p (op0))
1285 emit_insn (op0);
1286 }
1287
1288 /* Get a MEM rtx for expression EXP which is the address of an operand
1289 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1290 the maximum length of the block of memory that might be accessed or
1291 NULL if unknown. */
1292
1293 static rtx
1294 get_memory_rtx (tree exp, tree len)
1295 {
1296 tree orig_exp = exp;
1297 rtx addr, mem;
1298
1299 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1300 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1301 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1302 exp = TREE_OPERAND (exp, 0);
1303
1304 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1305 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1306
1307 /* Get an expression we can use to find the attributes to assign to MEM.
1308 First remove any nops. */
1309 while (CONVERT_EXPR_P (exp)
1310 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1311 exp = TREE_OPERAND (exp, 0);
1312
1313 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1314 (as builtin stringops may alias with anything). */
1315 exp = fold_build2 (MEM_REF,
1316 build_array_type (char_type_node,
1317 build_range_type (sizetype,
1318 size_one_node, len)),
1319 exp, build_int_cst (ptr_type_node, 0));
1320
1321 /* If the MEM_REF has no acceptable address, try to get the base object
1322 from the original address we got, and build an all-aliasing
1323 unknown-sized access to that one. */
1324 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1325 set_mem_attributes (mem, exp, 0);
1326 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1327 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1328 0))))
1329 {
1330 exp = build_fold_addr_expr (exp);
1331 exp = fold_build2 (MEM_REF,
1332 build_array_type (char_type_node,
1333 build_range_type (sizetype,
1334 size_zero_node,
1335 NULL)),
1336 exp, build_int_cst (ptr_type_node, 0));
1337 set_mem_attributes (mem, exp, 0);
1338 }
1339 set_mem_alias_set (mem, 0);
1340 return mem;
1341 }
1342 \f
1343 /* Built-in functions to perform an untyped call and return. */
1344
1345 #define apply_args_mode \
1346 (this_target_builtins->x_apply_args_mode)
1347 #define apply_result_mode \
1348 (this_target_builtins->x_apply_result_mode)
1349
1350 /* Return the size required for the block returned by __builtin_apply_args,
1351 and initialize apply_args_mode. */
1352
1353 static int
1354 apply_args_size (void)
1355 {
1356 static int size = -1;
1357 int align;
1358 unsigned int regno;
1359 enum machine_mode mode;
1360
1361 /* The values computed by this function never change. */
1362 if (size < 0)
1363 {
1364 /* The first value is the incoming arg-pointer. */
1365 size = GET_MODE_SIZE (Pmode);
1366
1367 /* The second value is the structure value address unless this is
1368 passed as an "invisible" first argument. */
1369 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1370 size += GET_MODE_SIZE (Pmode);
1371
1372 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1373 if (FUNCTION_ARG_REGNO_P (regno))
1374 {
1375 mode = targetm.calls.get_raw_arg_mode (regno);
1376
1377 gcc_assert (mode != VOIDmode);
1378
1379 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1380 if (size % align != 0)
1381 size = CEIL (size, align) * align;
1382 size += GET_MODE_SIZE (mode);
1383 apply_args_mode[regno] = mode;
1384 }
1385 else
1386 {
1387 apply_args_mode[regno] = VOIDmode;
1388 }
1389 }
1390 return size;
1391 }
1392
1393 /* Return the size required for the block returned by __builtin_apply,
1394 and initialize apply_result_mode. */
1395
1396 static int
1397 apply_result_size (void)
1398 {
1399 static int size = -1;
1400 int align, regno;
1401 enum machine_mode mode;
1402
1403 /* The values computed by this function never change. */
1404 if (size < 0)
1405 {
1406 size = 0;
1407
1408 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1409 if (targetm.calls.function_value_regno_p (regno))
1410 {
1411 mode = targetm.calls.get_raw_result_mode (regno);
1412
1413 gcc_assert (mode != VOIDmode);
1414
1415 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1416 if (size % align != 0)
1417 size = CEIL (size, align) * align;
1418 size += GET_MODE_SIZE (mode);
1419 apply_result_mode[regno] = mode;
1420 }
1421 else
1422 apply_result_mode[regno] = VOIDmode;
1423
1424 /* Allow targets that use untyped_call and untyped_return to override
1425 the size so that machine-specific information can be stored here. */
1426 #ifdef APPLY_RESULT_SIZE
1427 size = APPLY_RESULT_SIZE;
1428 #endif
1429 }
1430 return size;
1431 }
1432
1433 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1434 /* Create a vector describing the result block RESULT. If SAVEP is true,
1435 the result block is used to save the values; otherwise it is used to
1436 restore the values. */
1437
1438 static rtx
1439 result_vector (int savep, rtx result)
1440 {
1441 int regno, size, align, nelts;
1442 enum machine_mode mode;
1443 rtx reg, mem;
1444 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1445
1446 size = nelts = 0;
1447 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1448 if ((mode = apply_result_mode[regno]) != VOIDmode)
1449 {
1450 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1451 if (size % align != 0)
1452 size = CEIL (size, align) * align;
1453 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1454 mem = adjust_address (result, mode, size);
1455 savevec[nelts++] = (savep
1456 ? gen_rtx_SET (VOIDmode, mem, reg)
1457 : gen_rtx_SET (VOIDmode, reg, mem));
1458 size += GET_MODE_SIZE (mode);
1459 }
1460 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1461 }
1462 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1463
1464 /* Save the state required to perform an untyped call with the same
1465 arguments as were passed to the current function. */
1466
1467 static rtx
1468 expand_builtin_apply_args_1 (void)
1469 {
1470 rtx registers, tem;
1471 int size, align, regno;
1472 enum machine_mode mode;
1473 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1474
1475 /* Create a block where the arg-pointer, structure value address,
1476 and argument registers can be saved. */
1477 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1478
1479 /* Walk past the arg-pointer and structure value address. */
1480 size = GET_MODE_SIZE (Pmode);
1481 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1482 size += GET_MODE_SIZE (Pmode);
1483
1484 /* Save each register used in calling a function to the block. */
1485 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1486 if ((mode = apply_args_mode[regno]) != VOIDmode)
1487 {
1488 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1489 if (size % align != 0)
1490 size = CEIL (size, align) * align;
1491
1492 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1493
1494 emit_move_insn (adjust_address (registers, mode, size), tem);
1495 size += GET_MODE_SIZE (mode);
1496 }
1497
1498 /* Save the arg pointer to the block. */
1499 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1500 #ifdef STACK_GROWS_DOWNWARD
1501 /* We need the pointer as the caller actually passed them to us, not
1502 as we might have pretended they were passed. Make sure it's a valid
1503 operand, as emit_move_insn isn't expected to handle a PLUS. */
1504 tem
1505 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1506 NULL_RTX);
1507 #endif
1508 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1509
1510 size = GET_MODE_SIZE (Pmode);
1511
1512 /* Save the structure value address unless this is passed as an
1513 "invisible" first argument. */
1514 if (struct_incoming_value)
1515 {
1516 emit_move_insn (adjust_address (registers, Pmode, size),
1517 copy_to_reg (struct_incoming_value));
1518 size += GET_MODE_SIZE (Pmode);
1519 }
1520
1521 /* Return the address of the block. */
1522 return copy_addr_to_reg (XEXP (registers, 0));
1523 }
1524
1525 /* __builtin_apply_args returns block of memory allocated on
1526 the stack into which is stored the arg pointer, structure
1527 value address, static chain, and all the registers that might
1528 possibly be used in performing a function call. The code is
1529 moved to the start of the function so the incoming values are
1530 saved. */
1531
1532 static rtx
1533 expand_builtin_apply_args (void)
1534 {
1535 /* Don't do __builtin_apply_args more than once in a function.
1536 Save the result of the first call and reuse it. */
1537 if (apply_args_value != 0)
1538 return apply_args_value;
1539 {
1540 /* When this function is called, it means that registers must be
1541 saved on entry to this function. So we migrate the
1542 call to the first insn of this function. */
1543 rtx temp;
1544 rtx seq;
1545
1546 start_sequence ();
1547 temp = expand_builtin_apply_args_1 ();
1548 seq = get_insns ();
1549 end_sequence ();
1550
1551 apply_args_value = temp;
1552
1553 /* Put the insns after the NOTE that starts the function.
1554 If this is inside a start_sequence, make the outer-level insn
1555 chain current, so the code is placed at the start of the
1556 function. If internal_arg_pointer is a non-virtual pseudo,
1557 it needs to be placed after the function that initializes
1558 that pseudo. */
1559 push_topmost_sequence ();
1560 if (REG_P (crtl->args.internal_arg_pointer)
1561 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1562 emit_insn_before (seq, parm_birth_insn);
1563 else
1564 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1565 pop_topmost_sequence ();
1566 return temp;
1567 }
1568 }
1569
1570 /* Perform an untyped call and save the state required to perform an
1571 untyped return of whatever value was returned by the given function. */
1572
1573 static rtx
1574 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1575 {
1576 int size, align, regno;
1577 enum machine_mode mode;
1578 rtx incoming_args, result, reg, dest, src, call_insn;
1579 rtx old_stack_level = 0;
1580 rtx call_fusage = 0;
1581 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1582
1583 arguments = convert_memory_address (Pmode, arguments);
1584
1585 /* Create a block where the return registers can be saved. */
1586 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1587
1588 /* Fetch the arg pointer from the ARGUMENTS block. */
1589 incoming_args = gen_reg_rtx (Pmode);
1590 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1591 #ifndef STACK_GROWS_DOWNWARD
1592 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1593 incoming_args, 0, OPTAB_LIB_WIDEN);
1594 #endif
1595
1596 /* Push a new argument block and copy the arguments. Do not allow
1597 the (potential) memcpy call below to interfere with our stack
1598 manipulations. */
1599 do_pending_stack_adjust ();
1600 NO_DEFER_POP;
1601
1602 /* Save the stack with nonlocal if available. */
1603 #ifdef HAVE_save_stack_nonlocal
1604 if (HAVE_save_stack_nonlocal)
1605 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1606 else
1607 #endif
1608 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1609
1610 /* Allocate a block of memory onto the stack and copy the memory
1611 arguments to the outgoing arguments address. We can pass TRUE
1612 as the 4th argument because we just saved the stack pointer
1613 and will restore it right after the call. */
1614 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1615
1616 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1617 may have already set current_function_calls_alloca to true.
1618 current_function_calls_alloca won't be set if argsize is zero,
1619 so we have to guarantee need_drap is true here. */
1620 if (SUPPORTS_STACK_ALIGNMENT)
1621 crtl->need_drap = true;
1622
1623 dest = virtual_outgoing_args_rtx;
1624 #ifndef STACK_GROWS_DOWNWARD
1625 if (CONST_INT_P (argsize))
1626 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1627 else
1628 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1629 #endif
1630 dest = gen_rtx_MEM (BLKmode, dest);
1631 set_mem_align (dest, PARM_BOUNDARY);
1632 src = gen_rtx_MEM (BLKmode, incoming_args);
1633 set_mem_align (src, PARM_BOUNDARY);
1634 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1635
1636 /* Refer to the argument block. */
1637 apply_args_size ();
1638 arguments = gen_rtx_MEM (BLKmode, arguments);
1639 set_mem_align (arguments, PARM_BOUNDARY);
1640
1641 /* Walk past the arg-pointer and structure value address. */
1642 size = GET_MODE_SIZE (Pmode);
1643 if (struct_value)
1644 size += GET_MODE_SIZE (Pmode);
1645
1646 /* Restore each of the registers previously saved. Make USE insns
1647 for each of these registers for use in making the call. */
1648 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1649 if ((mode = apply_args_mode[regno]) != VOIDmode)
1650 {
1651 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1652 if (size % align != 0)
1653 size = CEIL (size, align) * align;
1654 reg = gen_rtx_REG (mode, regno);
1655 emit_move_insn (reg, adjust_address (arguments, mode, size));
1656 use_reg (&call_fusage, reg);
1657 size += GET_MODE_SIZE (mode);
1658 }
1659
1660 /* Restore the structure value address unless this is passed as an
1661 "invisible" first argument. */
1662 size = GET_MODE_SIZE (Pmode);
1663 if (struct_value)
1664 {
1665 rtx value = gen_reg_rtx (Pmode);
1666 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1667 emit_move_insn (struct_value, value);
1668 if (REG_P (struct_value))
1669 use_reg (&call_fusage, struct_value);
1670 size += GET_MODE_SIZE (Pmode);
1671 }
1672
1673 /* All arguments and registers used for the call are set up by now! */
1674 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1675
1676 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1677 and we don't want to load it into a register as an optimization,
1678 because prepare_call_address already did it if it should be done. */
1679 if (GET_CODE (function) != SYMBOL_REF)
1680 function = memory_address (FUNCTION_MODE, function);
1681
1682 /* Generate the actual call instruction and save the return value. */
1683 #ifdef HAVE_untyped_call
1684 if (HAVE_untyped_call)
1685 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1686 result, result_vector (1, result)));
1687 else
1688 #endif
1689 #ifdef HAVE_call_value
1690 if (HAVE_call_value)
1691 {
1692 rtx valreg = 0;
1693
1694 /* Locate the unique return register. It is not possible to
1695 express a call that sets more than one return register using
1696 call_value; use untyped_call for that. In fact, untyped_call
1697 only needs to save the return registers in the given block. */
1698 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1699 if ((mode = apply_result_mode[regno]) != VOIDmode)
1700 {
1701 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1702
1703 valreg = gen_rtx_REG (mode, regno);
1704 }
1705
1706 emit_call_insn (GEN_CALL_VALUE (valreg,
1707 gen_rtx_MEM (FUNCTION_MODE, function),
1708 const0_rtx, NULL_RTX, const0_rtx));
1709
1710 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1711 }
1712 else
1713 #endif
1714 gcc_unreachable ();
1715
1716 /* Find the CALL insn we just emitted, and attach the register usage
1717 information. */
1718 call_insn = last_call_insn ();
1719 add_function_usage_to (call_insn, call_fusage);
1720
1721 /* Restore the stack. */
1722 #ifdef HAVE_save_stack_nonlocal
1723 if (HAVE_save_stack_nonlocal)
1724 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1725 else
1726 #endif
1727 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1728 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1729
1730 OK_DEFER_POP;
1731
1732 /* Return the address of the result block. */
1733 result = copy_addr_to_reg (XEXP (result, 0));
1734 return convert_memory_address (ptr_mode, result);
1735 }
1736
1737 /* Perform an untyped return. */
1738
1739 static void
1740 expand_builtin_return (rtx result)
1741 {
1742 int size, align, regno;
1743 enum machine_mode mode;
1744 rtx reg;
1745 rtx call_fusage = 0;
1746
1747 result = convert_memory_address (Pmode, result);
1748
1749 apply_result_size ();
1750 result = gen_rtx_MEM (BLKmode, result);
1751
1752 #ifdef HAVE_untyped_return
1753 if (HAVE_untyped_return)
1754 {
1755 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1756 emit_barrier ();
1757 return;
1758 }
1759 #endif
1760
1761 /* Restore the return value and note that each value is used. */
1762 size = 0;
1763 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1764 if ((mode = apply_result_mode[regno]) != VOIDmode)
1765 {
1766 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1767 if (size % align != 0)
1768 size = CEIL (size, align) * align;
1769 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1770 emit_move_insn (reg, adjust_address (result, mode, size));
1771
1772 push_to_sequence (call_fusage);
1773 emit_use (reg);
1774 call_fusage = get_insns ();
1775 end_sequence ();
1776 size += GET_MODE_SIZE (mode);
1777 }
1778
1779 /* Put the USE insns before the return. */
1780 emit_insn (call_fusage);
1781
1782 /* Return whatever values was restored by jumping directly to the end
1783 of the function. */
1784 expand_naked_return ();
1785 }
1786
1787 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1788
1789 static enum type_class
1790 type_to_class (tree type)
1791 {
1792 switch (TREE_CODE (type))
1793 {
1794 case VOID_TYPE: return void_type_class;
1795 case INTEGER_TYPE: return integer_type_class;
1796 case ENUMERAL_TYPE: return enumeral_type_class;
1797 case BOOLEAN_TYPE: return boolean_type_class;
1798 case POINTER_TYPE: return pointer_type_class;
1799 case REFERENCE_TYPE: return reference_type_class;
1800 case OFFSET_TYPE: return offset_type_class;
1801 case REAL_TYPE: return real_type_class;
1802 case COMPLEX_TYPE: return complex_type_class;
1803 case FUNCTION_TYPE: return function_type_class;
1804 case METHOD_TYPE: return method_type_class;
1805 case RECORD_TYPE: return record_type_class;
1806 case UNION_TYPE:
1807 case QUAL_UNION_TYPE: return union_type_class;
1808 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1809 ? string_type_class : array_type_class);
1810 case LANG_TYPE: return lang_type_class;
1811 default: return no_type_class;
1812 }
1813 }
1814
1815 /* Expand a call EXP to __builtin_classify_type. */
1816
1817 static rtx
1818 expand_builtin_classify_type (tree exp)
1819 {
1820 if (call_expr_nargs (exp))
1821 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1822 return GEN_INT (no_type_class);
1823 }
1824
1825 /* This helper macro, meant to be used in mathfn_built_in below,
1826 determines which among a set of three builtin math functions is
1827 appropriate for a given type mode. The `F' and `L' cases are
1828 automatically generated from the `double' case. */
1829 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1830 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1831 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1832 fcodel = BUILT_IN_MATHFN##L ; break;
1833 /* Similar to above, but appends _R after any F/L suffix. */
1834 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1835 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1836 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1837 fcodel = BUILT_IN_MATHFN##L_R ; break;
1838
1839 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1840 if available. If IMPLICIT is true use the implicit builtin declaration,
1841 otherwise use the explicit declaration. If we can't do the conversion,
1842 return zero. */
1843
1844 static tree
1845 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1846 {
1847 enum built_in_function fcode, fcodef, fcodel, fcode2;
1848
1849 switch (fn)
1850 {
1851 CASE_MATHFN (BUILT_IN_ACOS)
1852 CASE_MATHFN (BUILT_IN_ACOSH)
1853 CASE_MATHFN (BUILT_IN_ASIN)
1854 CASE_MATHFN (BUILT_IN_ASINH)
1855 CASE_MATHFN (BUILT_IN_ATAN)
1856 CASE_MATHFN (BUILT_IN_ATAN2)
1857 CASE_MATHFN (BUILT_IN_ATANH)
1858 CASE_MATHFN (BUILT_IN_CBRT)
1859 CASE_MATHFN (BUILT_IN_CEIL)
1860 CASE_MATHFN (BUILT_IN_CEXPI)
1861 CASE_MATHFN (BUILT_IN_COPYSIGN)
1862 CASE_MATHFN (BUILT_IN_COS)
1863 CASE_MATHFN (BUILT_IN_COSH)
1864 CASE_MATHFN (BUILT_IN_DREM)
1865 CASE_MATHFN (BUILT_IN_ERF)
1866 CASE_MATHFN (BUILT_IN_ERFC)
1867 CASE_MATHFN (BUILT_IN_EXP)
1868 CASE_MATHFN (BUILT_IN_EXP10)
1869 CASE_MATHFN (BUILT_IN_EXP2)
1870 CASE_MATHFN (BUILT_IN_EXPM1)
1871 CASE_MATHFN (BUILT_IN_FABS)
1872 CASE_MATHFN (BUILT_IN_FDIM)
1873 CASE_MATHFN (BUILT_IN_FLOOR)
1874 CASE_MATHFN (BUILT_IN_FMA)
1875 CASE_MATHFN (BUILT_IN_FMAX)
1876 CASE_MATHFN (BUILT_IN_FMIN)
1877 CASE_MATHFN (BUILT_IN_FMOD)
1878 CASE_MATHFN (BUILT_IN_FREXP)
1879 CASE_MATHFN (BUILT_IN_GAMMA)
1880 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1881 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1882 CASE_MATHFN (BUILT_IN_HYPOT)
1883 CASE_MATHFN (BUILT_IN_ILOGB)
1884 CASE_MATHFN (BUILT_IN_ICEIL)
1885 CASE_MATHFN (BUILT_IN_IFLOOR)
1886 CASE_MATHFN (BUILT_IN_INF)
1887 CASE_MATHFN (BUILT_IN_IRINT)
1888 CASE_MATHFN (BUILT_IN_IROUND)
1889 CASE_MATHFN (BUILT_IN_ISINF)
1890 CASE_MATHFN (BUILT_IN_J0)
1891 CASE_MATHFN (BUILT_IN_J1)
1892 CASE_MATHFN (BUILT_IN_JN)
1893 CASE_MATHFN (BUILT_IN_LCEIL)
1894 CASE_MATHFN (BUILT_IN_LDEXP)
1895 CASE_MATHFN (BUILT_IN_LFLOOR)
1896 CASE_MATHFN (BUILT_IN_LGAMMA)
1897 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1898 CASE_MATHFN (BUILT_IN_LLCEIL)
1899 CASE_MATHFN (BUILT_IN_LLFLOOR)
1900 CASE_MATHFN (BUILT_IN_LLRINT)
1901 CASE_MATHFN (BUILT_IN_LLROUND)
1902 CASE_MATHFN (BUILT_IN_LOG)
1903 CASE_MATHFN (BUILT_IN_LOG10)
1904 CASE_MATHFN (BUILT_IN_LOG1P)
1905 CASE_MATHFN (BUILT_IN_LOG2)
1906 CASE_MATHFN (BUILT_IN_LOGB)
1907 CASE_MATHFN (BUILT_IN_LRINT)
1908 CASE_MATHFN (BUILT_IN_LROUND)
1909 CASE_MATHFN (BUILT_IN_MODF)
1910 CASE_MATHFN (BUILT_IN_NAN)
1911 CASE_MATHFN (BUILT_IN_NANS)
1912 CASE_MATHFN (BUILT_IN_NEARBYINT)
1913 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1914 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1915 CASE_MATHFN (BUILT_IN_POW)
1916 CASE_MATHFN (BUILT_IN_POWI)
1917 CASE_MATHFN (BUILT_IN_POW10)
1918 CASE_MATHFN (BUILT_IN_REMAINDER)
1919 CASE_MATHFN (BUILT_IN_REMQUO)
1920 CASE_MATHFN (BUILT_IN_RINT)
1921 CASE_MATHFN (BUILT_IN_ROUND)
1922 CASE_MATHFN (BUILT_IN_SCALB)
1923 CASE_MATHFN (BUILT_IN_SCALBLN)
1924 CASE_MATHFN (BUILT_IN_SCALBN)
1925 CASE_MATHFN (BUILT_IN_SIGNBIT)
1926 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1927 CASE_MATHFN (BUILT_IN_SIN)
1928 CASE_MATHFN (BUILT_IN_SINCOS)
1929 CASE_MATHFN (BUILT_IN_SINH)
1930 CASE_MATHFN (BUILT_IN_SQRT)
1931 CASE_MATHFN (BUILT_IN_TAN)
1932 CASE_MATHFN (BUILT_IN_TANH)
1933 CASE_MATHFN (BUILT_IN_TGAMMA)
1934 CASE_MATHFN (BUILT_IN_TRUNC)
1935 CASE_MATHFN (BUILT_IN_Y0)
1936 CASE_MATHFN (BUILT_IN_Y1)
1937 CASE_MATHFN (BUILT_IN_YN)
1938
1939 default:
1940 return NULL_TREE;
1941 }
1942
1943 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1944 fcode2 = fcode;
1945 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1946 fcode2 = fcodef;
1947 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1948 fcode2 = fcodel;
1949 else
1950 return NULL_TREE;
1951
1952 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1953 return NULL_TREE;
1954
1955 return builtin_decl_explicit (fcode2);
1956 }
1957
1958 /* Like mathfn_built_in_1(), but always use the implicit array. */
1959
1960 tree
1961 mathfn_built_in (tree type, enum built_in_function fn)
1962 {
1963 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1964 }
1965
1966 /* If errno must be maintained, expand the RTL to check if the result,
1967 TARGET, of a built-in function call, EXP, is NaN, and if so set
1968 errno to EDOM. */
1969
1970 static void
1971 expand_errno_check (tree exp, rtx target)
1972 {
1973 rtx lab = gen_label_rtx ();
1974
1975 /* Test the result; if it is NaN, set errno=EDOM because
1976 the argument was not in the domain. */
1977 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1978 NULL_RTX, NULL_RTX, lab,
1979 /* The jump is very likely. */
1980 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1981
1982 #ifdef TARGET_EDOM
1983 /* If this built-in doesn't throw an exception, set errno directly. */
1984 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1985 {
1986 #ifdef GEN_ERRNO_RTX
1987 rtx errno_rtx = GEN_ERRNO_RTX;
1988 #else
1989 rtx errno_rtx
1990 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1991 #endif
1992 emit_move_insn (errno_rtx,
1993 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1994 emit_label (lab);
1995 return;
1996 }
1997 #endif
1998
1999 /* Make sure the library call isn't expanded as a tail call. */
2000 CALL_EXPR_TAILCALL (exp) = 0;
2001
2002 /* We can't set errno=EDOM directly; let the library call do it.
2003 Pop the arguments right away in case the call gets deleted. */
2004 NO_DEFER_POP;
2005 expand_call (exp, target, 0);
2006 OK_DEFER_POP;
2007 emit_label (lab);
2008 }
2009
2010 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2011 Return NULL_RTX if a normal call should be emitted rather than expanding
2012 the function in-line. EXP is the expression that is a call to the builtin
2013 function; if convenient, the result should be placed in TARGET.
2014 SUBTARGET may be used as the target for computing one of EXP's operands. */
2015
2016 static rtx
2017 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2018 {
2019 optab builtin_optab;
2020 rtx op0, insns;
2021 tree fndecl = get_callee_fndecl (exp);
2022 enum machine_mode mode;
2023 bool errno_set = false;
2024 bool try_widening = false;
2025 tree arg;
2026
2027 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2028 return NULL_RTX;
2029
2030 arg = CALL_EXPR_ARG (exp, 0);
2031
2032 switch (DECL_FUNCTION_CODE (fndecl))
2033 {
2034 CASE_FLT_FN (BUILT_IN_SQRT):
2035 errno_set = ! tree_expr_nonnegative_p (arg);
2036 try_widening = true;
2037 builtin_optab = sqrt_optab;
2038 break;
2039 CASE_FLT_FN (BUILT_IN_EXP):
2040 errno_set = true; builtin_optab = exp_optab; break;
2041 CASE_FLT_FN (BUILT_IN_EXP10):
2042 CASE_FLT_FN (BUILT_IN_POW10):
2043 errno_set = true; builtin_optab = exp10_optab; break;
2044 CASE_FLT_FN (BUILT_IN_EXP2):
2045 errno_set = true; builtin_optab = exp2_optab; break;
2046 CASE_FLT_FN (BUILT_IN_EXPM1):
2047 errno_set = true; builtin_optab = expm1_optab; break;
2048 CASE_FLT_FN (BUILT_IN_LOGB):
2049 errno_set = true; builtin_optab = logb_optab; break;
2050 CASE_FLT_FN (BUILT_IN_LOG):
2051 errno_set = true; builtin_optab = log_optab; break;
2052 CASE_FLT_FN (BUILT_IN_LOG10):
2053 errno_set = true; builtin_optab = log10_optab; break;
2054 CASE_FLT_FN (BUILT_IN_LOG2):
2055 errno_set = true; builtin_optab = log2_optab; break;
2056 CASE_FLT_FN (BUILT_IN_LOG1P):
2057 errno_set = true; builtin_optab = log1p_optab; break;
2058 CASE_FLT_FN (BUILT_IN_ASIN):
2059 builtin_optab = asin_optab; break;
2060 CASE_FLT_FN (BUILT_IN_ACOS):
2061 builtin_optab = acos_optab; break;
2062 CASE_FLT_FN (BUILT_IN_TAN):
2063 builtin_optab = tan_optab; break;
2064 CASE_FLT_FN (BUILT_IN_ATAN):
2065 builtin_optab = atan_optab; break;
2066 CASE_FLT_FN (BUILT_IN_FLOOR):
2067 builtin_optab = floor_optab; break;
2068 CASE_FLT_FN (BUILT_IN_CEIL):
2069 builtin_optab = ceil_optab; break;
2070 CASE_FLT_FN (BUILT_IN_TRUNC):
2071 builtin_optab = btrunc_optab; break;
2072 CASE_FLT_FN (BUILT_IN_ROUND):
2073 builtin_optab = round_optab; break;
2074 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2075 builtin_optab = nearbyint_optab;
2076 if (flag_trapping_math)
2077 break;
2078 /* Else fallthrough and expand as rint. */
2079 CASE_FLT_FN (BUILT_IN_RINT):
2080 builtin_optab = rint_optab; break;
2081 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2082 builtin_optab = significand_optab; break;
2083 default:
2084 gcc_unreachable ();
2085 }
2086
2087 /* Make a suitable register to place result in. */
2088 mode = TYPE_MODE (TREE_TYPE (exp));
2089
2090 if (! flag_errno_math || ! HONOR_NANS (mode))
2091 errno_set = false;
2092
2093 /* Before working hard, check whether the instruction is available, but try
2094 to widen the mode for specific operations. */
2095 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2096 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2097 && (!errno_set || !optimize_insn_for_size_p ()))
2098 {
2099 rtx result = gen_reg_rtx (mode);
2100
2101 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2102 need to expand the argument again. This way, we will not perform
2103 side-effects more the once. */
2104 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2105
2106 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2107
2108 start_sequence ();
2109
2110 /* Compute into RESULT.
2111 Set RESULT to wherever the result comes back. */
2112 result = expand_unop (mode, builtin_optab, op0, result, 0);
2113
2114 if (result != 0)
2115 {
2116 if (errno_set)
2117 expand_errno_check (exp, result);
2118
2119 /* Output the entire sequence. */
2120 insns = get_insns ();
2121 end_sequence ();
2122 emit_insn (insns);
2123 return result;
2124 }
2125
2126 /* If we were unable to expand via the builtin, stop the sequence
2127 (without outputting the insns) and call to the library function
2128 with the stabilized argument list. */
2129 end_sequence ();
2130 }
2131
2132 return expand_call (exp, target, target == const0_rtx);
2133 }
2134
2135 /* Expand a call to the builtin binary math functions (pow and atan2).
2136 Return NULL_RTX if a normal call should be emitted rather than expanding the
2137 function in-line. EXP is the expression that is a call to the builtin
2138 function; if convenient, the result should be placed in TARGET.
2139 SUBTARGET may be used as the target for computing one of EXP's
2140 operands. */
2141
2142 static rtx
2143 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2144 {
2145 optab builtin_optab;
2146 rtx op0, op1, insns, result;
2147 int op1_type = REAL_TYPE;
2148 tree fndecl = get_callee_fndecl (exp);
2149 tree arg0, arg1;
2150 enum machine_mode mode;
2151 bool errno_set = true;
2152
2153 switch (DECL_FUNCTION_CODE (fndecl))
2154 {
2155 CASE_FLT_FN (BUILT_IN_SCALBN):
2156 CASE_FLT_FN (BUILT_IN_SCALBLN):
2157 CASE_FLT_FN (BUILT_IN_LDEXP):
2158 op1_type = INTEGER_TYPE;
2159 default:
2160 break;
2161 }
2162
2163 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2164 return NULL_RTX;
2165
2166 arg0 = CALL_EXPR_ARG (exp, 0);
2167 arg1 = CALL_EXPR_ARG (exp, 1);
2168
2169 switch (DECL_FUNCTION_CODE (fndecl))
2170 {
2171 CASE_FLT_FN (BUILT_IN_POW):
2172 builtin_optab = pow_optab; break;
2173 CASE_FLT_FN (BUILT_IN_ATAN2):
2174 builtin_optab = atan2_optab; break;
2175 CASE_FLT_FN (BUILT_IN_SCALB):
2176 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2177 return 0;
2178 builtin_optab = scalb_optab; break;
2179 CASE_FLT_FN (BUILT_IN_SCALBN):
2180 CASE_FLT_FN (BUILT_IN_SCALBLN):
2181 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2182 return 0;
2183 /* Fall through... */
2184 CASE_FLT_FN (BUILT_IN_LDEXP):
2185 builtin_optab = ldexp_optab; break;
2186 CASE_FLT_FN (BUILT_IN_FMOD):
2187 builtin_optab = fmod_optab; break;
2188 CASE_FLT_FN (BUILT_IN_REMAINDER):
2189 CASE_FLT_FN (BUILT_IN_DREM):
2190 builtin_optab = remainder_optab; break;
2191 default:
2192 gcc_unreachable ();
2193 }
2194
2195 /* Make a suitable register to place result in. */
2196 mode = TYPE_MODE (TREE_TYPE (exp));
2197
2198 /* Before working hard, check whether the instruction is available. */
2199 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2200 return NULL_RTX;
2201
2202 result = gen_reg_rtx (mode);
2203
2204 if (! flag_errno_math || ! HONOR_NANS (mode))
2205 errno_set = false;
2206
2207 if (errno_set && optimize_insn_for_size_p ())
2208 return 0;
2209
2210 /* Always stabilize the argument list. */
2211 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2212 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2213
2214 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2215 op1 = expand_normal (arg1);
2216
2217 start_sequence ();
2218
2219 /* Compute into RESULT.
2220 Set RESULT to wherever the result comes back. */
2221 result = expand_binop (mode, builtin_optab, op0, op1,
2222 result, 0, OPTAB_DIRECT);
2223
2224 /* If we were unable to expand via the builtin, stop the sequence
2225 (without outputting the insns) and call to the library function
2226 with the stabilized argument list. */
2227 if (result == 0)
2228 {
2229 end_sequence ();
2230 return expand_call (exp, target, target == const0_rtx);
2231 }
2232
2233 if (errno_set)
2234 expand_errno_check (exp, result);
2235
2236 /* Output the entire sequence. */
2237 insns = get_insns ();
2238 end_sequence ();
2239 emit_insn (insns);
2240
2241 return result;
2242 }
2243
2244 /* Expand a call to the builtin trinary math functions (fma).
2245 Return NULL_RTX if a normal call should be emitted rather than expanding the
2246 function in-line. EXP is the expression that is a call to the builtin
2247 function; if convenient, the result should be placed in TARGET.
2248 SUBTARGET may be used as the target for computing one of EXP's
2249 operands. */
2250
2251 static rtx
2252 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2253 {
2254 optab builtin_optab;
2255 rtx op0, op1, op2, insns, result;
2256 tree fndecl = get_callee_fndecl (exp);
2257 tree arg0, arg1, arg2;
2258 enum machine_mode mode;
2259
2260 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2261 return NULL_RTX;
2262
2263 arg0 = CALL_EXPR_ARG (exp, 0);
2264 arg1 = CALL_EXPR_ARG (exp, 1);
2265 arg2 = CALL_EXPR_ARG (exp, 2);
2266
2267 switch (DECL_FUNCTION_CODE (fndecl))
2268 {
2269 CASE_FLT_FN (BUILT_IN_FMA):
2270 builtin_optab = fma_optab; break;
2271 default:
2272 gcc_unreachable ();
2273 }
2274
2275 /* Make a suitable register to place result in. */
2276 mode = TYPE_MODE (TREE_TYPE (exp));
2277
2278 /* Before working hard, check whether the instruction is available. */
2279 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2280 return NULL_RTX;
2281
2282 result = gen_reg_rtx (mode);
2283
2284 /* Always stabilize the argument list. */
2285 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2286 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2287 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2288
2289 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2290 op1 = expand_normal (arg1);
2291 op2 = expand_normal (arg2);
2292
2293 start_sequence ();
2294
2295 /* Compute into RESULT.
2296 Set RESULT to wherever the result comes back. */
2297 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2298 result, 0);
2299
2300 /* If we were unable to expand via the builtin, stop the sequence
2301 (without outputting the insns) and call to the library function
2302 with the stabilized argument list. */
2303 if (result == 0)
2304 {
2305 end_sequence ();
2306 return expand_call (exp, target, target == const0_rtx);
2307 }
2308
2309 /* Output the entire sequence. */
2310 insns = get_insns ();
2311 end_sequence ();
2312 emit_insn (insns);
2313
2314 return result;
2315 }
2316
2317 /* Expand a call to the builtin sin and cos math functions.
2318 Return NULL_RTX if a normal call should be emitted rather than expanding the
2319 function in-line. EXP is the expression that is a call to the builtin
2320 function; if convenient, the result should be placed in TARGET.
2321 SUBTARGET may be used as the target for computing one of EXP's
2322 operands. */
2323
2324 static rtx
2325 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2326 {
2327 optab builtin_optab;
2328 rtx op0, insns;
2329 tree fndecl = get_callee_fndecl (exp);
2330 enum machine_mode mode;
2331 tree arg;
2332
2333 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2334 return NULL_RTX;
2335
2336 arg = CALL_EXPR_ARG (exp, 0);
2337
2338 switch (DECL_FUNCTION_CODE (fndecl))
2339 {
2340 CASE_FLT_FN (BUILT_IN_SIN):
2341 CASE_FLT_FN (BUILT_IN_COS):
2342 builtin_optab = sincos_optab; break;
2343 default:
2344 gcc_unreachable ();
2345 }
2346
2347 /* Make a suitable register to place result in. */
2348 mode = TYPE_MODE (TREE_TYPE (exp));
2349
2350 /* Check if sincos insn is available, otherwise fallback
2351 to sin or cos insn. */
2352 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2353 switch (DECL_FUNCTION_CODE (fndecl))
2354 {
2355 CASE_FLT_FN (BUILT_IN_SIN):
2356 builtin_optab = sin_optab; break;
2357 CASE_FLT_FN (BUILT_IN_COS):
2358 builtin_optab = cos_optab; break;
2359 default:
2360 gcc_unreachable ();
2361 }
2362
2363 /* Before working hard, check whether the instruction is available. */
2364 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2365 {
2366 rtx result = gen_reg_rtx (mode);
2367
2368 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2369 need to expand the argument again. This way, we will not perform
2370 side-effects more the once. */
2371 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2372
2373 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2374
2375 start_sequence ();
2376
2377 /* Compute into RESULT.
2378 Set RESULT to wherever the result comes back. */
2379 if (builtin_optab == sincos_optab)
2380 {
2381 int ok;
2382
2383 switch (DECL_FUNCTION_CODE (fndecl))
2384 {
2385 CASE_FLT_FN (BUILT_IN_SIN):
2386 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2387 break;
2388 CASE_FLT_FN (BUILT_IN_COS):
2389 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2390 break;
2391 default:
2392 gcc_unreachable ();
2393 }
2394 gcc_assert (ok);
2395 }
2396 else
2397 result = expand_unop (mode, builtin_optab, op0, result, 0);
2398
2399 if (result != 0)
2400 {
2401 /* Output the entire sequence. */
2402 insns = get_insns ();
2403 end_sequence ();
2404 emit_insn (insns);
2405 return result;
2406 }
2407
2408 /* If we were unable to expand via the builtin, stop the sequence
2409 (without outputting the insns) and call to the library function
2410 with the stabilized argument list. */
2411 end_sequence ();
2412 }
2413
2414 return expand_call (exp, target, target == const0_rtx);
2415 }
2416
2417 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2418 return an RTL instruction code that implements the functionality.
2419 If that isn't possible or available return CODE_FOR_nothing. */
2420
2421 static enum insn_code
2422 interclass_mathfn_icode (tree arg, tree fndecl)
2423 {
2424 bool errno_set = false;
2425 optab builtin_optab = unknown_optab;
2426 enum machine_mode mode;
2427
2428 switch (DECL_FUNCTION_CODE (fndecl))
2429 {
2430 CASE_FLT_FN (BUILT_IN_ILOGB):
2431 errno_set = true; builtin_optab = ilogb_optab; break;
2432 CASE_FLT_FN (BUILT_IN_ISINF):
2433 builtin_optab = isinf_optab; break;
2434 case BUILT_IN_ISNORMAL:
2435 case BUILT_IN_ISFINITE:
2436 CASE_FLT_FN (BUILT_IN_FINITE):
2437 case BUILT_IN_FINITED32:
2438 case BUILT_IN_FINITED64:
2439 case BUILT_IN_FINITED128:
2440 case BUILT_IN_ISINFD32:
2441 case BUILT_IN_ISINFD64:
2442 case BUILT_IN_ISINFD128:
2443 /* These builtins have no optabs (yet). */
2444 break;
2445 default:
2446 gcc_unreachable ();
2447 }
2448
2449 /* There's no easy way to detect the case we need to set EDOM. */
2450 if (flag_errno_math && errno_set)
2451 return CODE_FOR_nothing;
2452
2453 /* Optab mode depends on the mode of the input argument. */
2454 mode = TYPE_MODE (TREE_TYPE (arg));
2455
2456 if (builtin_optab)
2457 return optab_handler (builtin_optab, mode);
2458 return CODE_FOR_nothing;
2459 }
2460
2461 /* Expand a call to one of the builtin math functions that operate on
2462 floating point argument and output an integer result (ilogb, isinf,
2463 isnan, etc).
2464 Return 0 if a normal call should be emitted rather than expanding the
2465 function in-line. EXP is the expression that is a call to the builtin
2466 function; if convenient, the result should be placed in TARGET. */
2467
2468 static rtx
2469 expand_builtin_interclass_mathfn (tree exp, rtx target)
2470 {
2471 enum insn_code icode = CODE_FOR_nothing;
2472 rtx op0;
2473 tree fndecl = get_callee_fndecl (exp);
2474 enum machine_mode mode;
2475 tree arg;
2476
2477 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2478 return NULL_RTX;
2479
2480 arg = CALL_EXPR_ARG (exp, 0);
2481 icode = interclass_mathfn_icode (arg, fndecl);
2482 mode = TYPE_MODE (TREE_TYPE (arg));
2483
2484 if (icode != CODE_FOR_nothing)
2485 {
2486 struct expand_operand ops[1];
2487 rtx last = get_last_insn ();
2488 tree orig_arg = arg;
2489
2490 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2491 need to expand the argument again. This way, we will not perform
2492 side-effects more the once. */
2493 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2494
2495 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2496
2497 if (mode != GET_MODE (op0))
2498 op0 = convert_to_mode (mode, op0, 0);
2499
2500 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2501 if (maybe_legitimize_operands (icode, 0, 1, ops)
2502 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2503 return ops[0].value;
2504
2505 delete_insns_since (last);
2506 CALL_EXPR_ARG (exp, 0) = orig_arg;
2507 }
2508
2509 return NULL_RTX;
2510 }
2511
2512 /* Expand a call to the builtin sincos math function.
2513 Return NULL_RTX if a normal call should be emitted rather than expanding the
2514 function in-line. EXP is the expression that is a call to the builtin
2515 function. */
2516
2517 static rtx
2518 expand_builtin_sincos (tree exp)
2519 {
2520 rtx op0, op1, op2, target1, target2;
2521 enum machine_mode mode;
2522 tree arg, sinp, cosp;
2523 int result;
2524 location_t loc = EXPR_LOCATION (exp);
2525 tree alias_type, alias_off;
2526
2527 if (!validate_arglist (exp, REAL_TYPE,
2528 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2529 return NULL_RTX;
2530
2531 arg = CALL_EXPR_ARG (exp, 0);
2532 sinp = CALL_EXPR_ARG (exp, 1);
2533 cosp = CALL_EXPR_ARG (exp, 2);
2534
2535 /* Make a suitable register to place result in. */
2536 mode = TYPE_MODE (TREE_TYPE (arg));
2537
2538 /* Check if sincos insn is available, otherwise emit the call. */
2539 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2540 return NULL_RTX;
2541
2542 target1 = gen_reg_rtx (mode);
2543 target2 = gen_reg_rtx (mode);
2544
2545 op0 = expand_normal (arg);
2546 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2547 alias_off = build_int_cst (alias_type, 0);
2548 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2549 sinp, alias_off));
2550 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2551 cosp, alias_off));
2552
2553 /* Compute into target1 and target2.
2554 Set TARGET to wherever the result comes back. */
2555 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2556 gcc_assert (result);
2557
2558 /* Move target1 and target2 to the memory locations indicated
2559 by op1 and op2. */
2560 emit_move_insn (op1, target1);
2561 emit_move_insn (op2, target2);
2562
2563 return const0_rtx;
2564 }
2565
2566 /* Expand a call to the internal cexpi builtin to the sincos math function.
2567 EXP is the expression that is a call to the builtin function; if convenient,
2568 the result should be placed in TARGET. */
2569
2570 static rtx
2571 expand_builtin_cexpi (tree exp, rtx target)
2572 {
2573 tree fndecl = get_callee_fndecl (exp);
2574 tree arg, type;
2575 enum machine_mode mode;
2576 rtx op0, op1, op2;
2577 location_t loc = EXPR_LOCATION (exp);
2578
2579 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2580 return NULL_RTX;
2581
2582 arg = CALL_EXPR_ARG (exp, 0);
2583 type = TREE_TYPE (arg);
2584 mode = TYPE_MODE (TREE_TYPE (arg));
2585
2586 /* Try expanding via a sincos optab, fall back to emitting a libcall
2587 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2588 is only generated from sincos, cexp or if we have either of them. */
2589 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2590 {
2591 op1 = gen_reg_rtx (mode);
2592 op2 = gen_reg_rtx (mode);
2593
2594 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2595
2596 /* Compute into op1 and op2. */
2597 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2598 }
2599 else if (targetm.libc_has_function (function_sincos))
2600 {
2601 tree call, fn = NULL_TREE;
2602 tree top1, top2;
2603 rtx op1a, op2a;
2604
2605 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2606 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2607 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2608 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2610 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2611 else
2612 gcc_unreachable ();
2613
2614 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2615 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2616 op1a = copy_addr_to_reg (XEXP (op1, 0));
2617 op2a = copy_addr_to_reg (XEXP (op2, 0));
2618 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2619 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2620
2621 /* Make sure not to fold the sincos call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2624 call, 3, arg, top1, top2));
2625 }
2626 else
2627 {
2628 tree call, fn = NULL_TREE, narg;
2629 tree ctype = build_complex_type (type);
2630
2631 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2632 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2633 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2634 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2635 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2636 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2637 else
2638 gcc_unreachable ();
2639
2640 /* If we don't have a decl for cexp create one. This is the
2641 friendliest fallback if the user calls __builtin_cexpi
2642 without full target C99 function support. */
2643 if (fn == NULL_TREE)
2644 {
2645 tree fntype;
2646 const char *name = NULL;
2647
2648 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2649 name = "cexpf";
2650 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2651 name = "cexp";
2652 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2653 name = "cexpl";
2654
2655 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2656 fn = build_fn_decl (name, fntype);
2657 }
2658
2659 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2660 build_real (type, dconst0), arg);
2661
2662 /* Make sure not to fold the cexp call again. */
2663 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2664 return expand_expr (build_call_nary (ctype, call, 1, narg),
2665 target, VOIDmode, EXPAND_NORMAL);
2666 }
2667
2668 /* Now build the proper return type. */
2669 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2670 make_tree (TREE_TYPE (arg), op2),
2671 make_tree (TREE_TYPE (arg), op1)),
2672 target, VOIDmode, EXPAND_NORMAL);
2673 }
2674
2675 /* Conveniently construct a function call expression. FNDECL names the
2676 function to be called, N is the number of arguments, and the "..."
2677 parameters are the argument expressions. Unlike build_call_exr
2678 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2679
2680 static tree
2681 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2682 {
2683 va_list ap;
2684 tree fntype = TREE_TYPE (fndecl);
2685 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2686
2687 va_start (ap, n);
2688 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2689 va_end (ap);
2690 SET_EXPR_LOCATION (fn, loc);
2691 return fn;
2692 }
2693
2694 /* Expand a call to one of the builtin rounding functions gcc defines
2695 as an extension (lfloor and lceil). As these are gcc extensions we
2696 do not need to worry about setting errno to EDOM.
2697 If expanding via optab fails, lower expression to (int)(floor(x)).
2698 EXP is the expression that is a call to the builtin function;
2699 if convenient, the result should be placed in TARGET. */
2700
2701 static rtx
2702 expand_builtin_int_roundingfn (tree exp, rtx target)
2703 {
2704 convert_optab builtin_optab;
2705 rtx op0, insns, tmp;
2706 tree fndecl = get_callee_fndecl (exp);
2707 enum built_in_function fallback_fn;
2708 tree fallback_fndecl;
2709 enum machine_mode mode;
2710 tree arg;
2711
2712 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2713 gcc_unreachable ();
2714
2715 arg = CALL_EXPR_ARG (exp, 0);
2716
2717 switch (DECL_FUNCTION_CODE (fndecl))
2718 {
2719 CASE_FLT_FN (BUILT_IN_ICEIL):
2720 CASE_FLT_FN (BUILT_IN_LCEIL):
2721 CASE_FLT_FN (BUILT_IN_LLCEIL):
2722 builtin_optab = lceil_optab;
2723 fallback_fn = BUILT_IN_CEIL;
2724 break;
2725
2726 CASE_FLT_FN (BUILT_IN_IFLOOR):
2727 CASE_FLT_FN (BUILT_IN_LFLOOR):
2728 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2729 builtin_optab = lfloor_optab;
2730 fallback_fn = BUILT_IN_FLOOR;
2731 break;
2732
2733 default:
2734 gcc_unreachable ();
2735 }
2736
2737 /* Make a suitable register to place result in. */
2738 mode = TYPE_MODE (TREE_TYPE (exp));
2739
2740 target = gen_reg_rtx (mode);
2741
2742 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2743 need to expand the argument again. This way, we will not perform
2744 side-effects more the once. */
2745 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2746
2747 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2748
2749 start_sequence ();
2750
2751 /* Compute into TARGET. */
2752 if (expand_sfix_optab (target, op0, builtin_optab))
2753 {
2754 /* Output the entire sequence. */
2755 insns = get_insns ();
2756 end_sequence ();
2757 emit_insn (insns);
2758 return target;
2759 }
2760
2761 /* If we were unable to expand via the builtin, stop the sequence
2762 (without outputting the insns). */
2763 end_sequence ();
2764
2765 /* Fall back to floating point rounding optab. */
2766 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2767
2768 /* For non-C99 targets we may end up without a fallback fndecl here
2769 if the user called __builtin_lfloor directly. In this case emit
2770 a call to the floor/ceil variants nevertheless. This should result
2771 in the best user experience for not full C99 targets. */
2772 if (fallback_fndecl == NULL_TREE)
2773 {
2774 tree fntype;
2775 const char *name = NULL;
2776
2777 switch (DECL_FUNCTION_CODE (fndecl))
2778 {
2779 case BUILT_IN_ICEIL:
2780 case BUILT_IN_LCEIL:
2781 case BUILT_IN_LLCEIL:
2782 name = "ceil";
2783 break;
2784 case BUILT_IN_ICEILF:
2785 case BUILT_IN_LCEILF:
2786 case BUILT_IN_LLCEILF:
2787 name = "ceilf";
2788 break;
2789 case BUILT_IN_ICEILL:
2790 case BUILT_IN_LCEILL:
2791 case BUILT_IN_LLCEILL:
2792 name = "ceill";
2793 break;
2794 case BUILT_IN_IFLOOR:
2795 case BUILT_IN_LFLOOR:
2796 case BUILT_IN_LLFLOOR:
2797 name = "floor";
2798 break;
2799 case BUILT_IN_IFLOORF:
2800 case BUILT_IN_LFLOORF:
2801 case BUILT_IN_LLFLOORF:
2802 name = "floorf";
2803 break;
2804 case BUILT_IN_IFLOORL:
2805 case BUILT_IN_LFLOORL:
2806 case BUILT_IN_LLFLOORL:
2807 name = "floorl";
2808 break;
2809 default:
2810 gcc_unreachable ();
2811 }
2812
2813 fntype = build_function_type_list (TREE_TYPE (arg),
2814 TREE_TYPE (arg), NULL_TREE);
2815 fallback_fndecl = build_fn_decl (name, fntype);
2816 }
2817
2818 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2819
2820 tmp = expand_normal (exp);
2821 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2822
2823 /* Truncate the result of floating point optab to integer
2824 via expand_fix (). */
2825 target = gen_reg_rtx (mode);
2826 expand_fix (target, tmp, 0);
2827
2828 return target;
2829 }
2830
2831 /* Expand a call to one of the builtin math functions doing integer
2832 conversion (lrint).
2833 Return 0 if a normal call should be emitted rather than expanding the
2834 function in-line. EXP is the expression that is a call to the builtin
2835 function; if convenient, the result should be placed in TARGET. */
2836
2837 static rtx
2838 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2839 {
2840 convert_optab builtin_optab;
2841 rtx op0, insns;
2842 tree fndecl = get_callee_fndecl (exp);
2843 tree arg;
2844 enum machine_mode mode;
2845 enum built_in_function fallback_fn = BUILT_IN_NONE;
2846
2847 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2848 gcc_unreachable ();
2849
2850 arg = CALL_EXPR_ARG (exp, 0);
2851
2852 switch (DECL_FUNCTION_CODE (fndecl))
2853 {
2854 CASE_FLT_FN (BUILT_IN_IRINT):
2855 fallback_fn = BUILT_IN_LRINT;
2856 /* FALLTHRU */
2857 CASE_FLT_FN (BUILT_IN_LRINT):
2858 CASE_FLT_FN (BUILT_IN_LLRINT):
2859 builtin_optab = lrint_optab;
2860 break;
2861
2862 CASE_FLT_FN (BUILT_IN_IROUND):
2863 fallback_fn = BUILT_IN_LROUND;
2864 /* FALLTHRU */
2865 CASE_FLT_FN (BUILT_IN_LROUND):
2866 CASE_FLT_FN (BUILT_IN_LLROUND):
2867 builtin_optab = lround_optab;
2868 break;
2869
2870 default:
2871 gcc_unreachable ();
2872 }
2873
2874 /* There's no easy way to detect the case we need to set EDOM. */
2875 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2876 return NULL_RTX;
2877
2878 /* Make a suitable register to place result in. */
2879 mode = TYPE_MODE (TREE_TYPE (exp));
2880
2881 /* There's no easy way to detect the case we need to set EDOM. */
2882 if (!flag_errno_math)
2883 {
2884 rtx result = gen_reg_rtx (mode);
2885
2886 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2887 need to expand the argument again. This way, we will not perform
2888 side-effects more the once. */
2889 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2890
2891 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2892
2893 start_sequence ();
2894
2895 if (expand_sfix_optab (result, op0, builtin_optab))
2896 {
2897 /* Output the entire sequence. */
2898 insns = get_insns ();
2899 end_sequence ();
2900 emit_insn (insns);
2901 return result;
2902 }
2903
2904 /* If we were unable to expand via the builtin, stop the sequence
2905 (without outputting the insns) and call to the library function
2906 with the stabilized argument list. */
2907 end_sequence ();
2908 }
2909
2910 if (fallback_fn != BUILT_IN_NONE)
2911 {
2912 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2913 targets, (int) round (x) should never be transformed into
2914 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2915 a call to lround in the hope that the target provides at least some
2916 C99 functions. This should result in the best user experience for
2917 not full C99 targets. */
2918 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2919 fallback_fn, 0);
2920
2921 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2922 fallback_fndecl, 1, arg);
2923
2924 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2925 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2926 return convert_to_mode (mode, target, 0);
2927 }
2928
2929 return expand_call (exp, target, target == const0_rtx);
2930 }
2931
2932 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2933 a normal call should be emitted rather than expanding the function
2934 in-line. EXP is the expression that is a call to the builtin
2935 function; if convenient, the result should be placed in TARGET. */
2936
2937 static rtx
2938 expand_builtin_powi (tree exp, rtx target)
2939 {
2940 tree arg0, arg1;
2941 rtx op0, op1;
2942 enum machine_mode mode;
2943 enum machine_mode mode2;
2944
2945 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2946 return NULL_RTX;
2947
2948 arg0 = CALL_EXPR_ARG (exp, 0);
2949 arg1 = CALL_EXPR_ARG (exp, 1);
2950 mode = TYPE_MODE (TREE_TYPE (exp));
2951
2952 /* Emit a libcall to libgcc. */
2953
2954 /* Mode of the 2nd argument must match that of an int. */
2955 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2956
2957 if (target == NULL_RTX)
2958 target = gen_reg_rtx (mode);
2959
2960 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2961 if (GET_MODE (op0) != mode)
2962 op0 = convert_to_mode (mode, op0, 0);
2963 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2964 if (GET_MODE (op1) != mode2)
2965 op1 = convert_to_mode (mode2, op1, 0);
2966
2967 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2968 target, LCT_CONST, mode, 2,
2969 op0, mode, op1, mode2);
2970
2971 return target;
2972 }
2973
2974 /* Expand expression EXP which is a call to the strlen builtin. Return
2975 NULL_RTX if we failed the caller should emit a normal call, otherwise
2976 try to get the result in TARGET, if convenient. */
2977
2978 static rtx
2979 expand_builtin_strlen (tree exp, rtx target,
2980 enum machine_mode target_mode)
2981 {
2982 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2983 return NULL_RTX;
2984 else
2985 {
2986 struct expand_operand ops[4];
2987 rtx pat;
2988 tree len;
2989 tree src = CALL_EXPR_ARG (exp, 0);
2990 rtx src_reg, before_strlen;
2991 enum machine_mode insn_mode = target_mode;
2992 enum insn_code icode = CODE_FOR_nothing;
2993 unsigned int align;
2994
2995 /* If the length can be computed at compile-time, return it. */
2996 len = c_strlen (src, 0);
2997 if (len)
2998 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2999
3000 /* If the length can be computed at compile-time and is constant
3001 integer, but there are side-effects in src, evaluate
3002 src for side-effects, then return len.
3003 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3004 can be optimized into: i++; x = 3; */
3005 len = c_strlen (src, 1);
3006 if (len && TREE_CODE (len) == INTEGER_CST)
3007 {
3008 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3009 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3010 }
3011
3012 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3013
3014 /* If SRC is not a pointer type, don't do this operation inline. */
3015 if (align == 0)
3016 return NULL_RTX;
3017
3018 /* Bail out if we can't compute strlen in the right mode. */
3019 while (insn_mode != VOIDmode)
3020 {
3021 icode = optab_handler (strlen_optab, insn_mode);
3022 if (icode != CODE_FOR_nothing)
3023 break;
3024
3025 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3026 }
3027 if (insn_mode == VOIDmode)
3028 return NULL_RTX;
3029
3030 /* Make a place to hold the source address. We will not expand
3031 the actual source until we are sure that the expansion will
3032 not fail -- there are trees that cannot be expanded twice. */
3033 src_reg = gen_reg_rtx (Pmode);
3034
3035 /* Mark the beginning of the strlen sequence so we can emit the
3036 source operand later. */
3037 before_strlen = get_last_insn ();
3038
3039 create_output_operand (&ops[0], target, insn_mode);
3040 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3041 create_integer_operand (&ops[2], 0);
3042 create_integer_operand (&ops[3], align);
3043 if (!maybe_expand_insn (icode, 4, ops))
3044 return NULL_RTX;
3045
3046 /* Now that we are assured of success, expand the source. */
3047 start_sequence ();
3048 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3049 if (pat != src_reg)
3050 {
3051 #ifdef POINTERS_EXTEND_UNSIGNED
3052 if (GET_MODE (pat) != Pmode)
3053 pat = convert_to_mode (Pmode, pat,
3054 POINTERS_EXTEND_UNSIGNED);
3055 #endif
3056 emit_move_insn (src_reg, pat);
3057 }
3058 pat = get_insns ();
3059 end_sequence ();
3060
3061 if (before_strlen)
3062 emit_insn_after (pat, before_strlen);
3063 else
3064 emit_insn_before (pat, get_insns ());
3065
3066 /* Return the value in the proper mode for this function. */
3067 if (GET_MODE (ops[0].value) == target_mode)
3068 target = ops[0].value;
3069 else if (target != 0)
3070 convert_move (target, ops[0].value, 0);
3071 else
3072 target = convert_to_mode (target_mode, ops[0].value, 0);
3073
3074 return target;
3075 }
3076 }
3077
3078 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3079 bytes from constant string DATA + OFFSET and return it as target
3080 constant. */
3081
3082 static rtx
3083 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3084 enum machine_mode mode)
3085 {
3086 const char *str = (const char *) data;
3087
3088 gcc_assert (offset >= 0
3089 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3090 <= strlen (str) + 1));
3091
3092 return c_readstr (str + offset, mode);
3093 }
3094
3095 /* LEN specify length of the block of memcpy/memset operation.
3096 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3097 In some cases we can make very likely guess on max size, then we
3098 set it into PROBABLE_MAX_SIZE. */
3099
3100 static void
3101 determine_block_size (tree len, rtx len_rtx,
3102 unsigned HOST_WIDE_INT *min_size,
3103 unsigned HOST_WIDE_INT *max_size,
3104 unsigned HOST_WIDE_INT *probable_max_size)
3105 {
3106 if (CONST_INT_P (len_rtx))
3107 {
3108 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3109 return;
3110 }
3111 else
3112 {
3113 wide_int min, max;
3114 enum value_range_type range_type = VR_UNDEFINED;
3115
3116 /* Determine bounds from the type. */
3117 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3118 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3119 else
3120 *min_size = 0;
3121 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3122 *probable_max_size = *max_size
3123 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3124 else
3125 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3126
3127 if (TREE_CODE (len) == SSA_NAME)
3128 range_type = get_range_info (len, &min, &max);
3129 if (range_type == VR_RANGE)
3130 {
3131 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3132 *min_size = min.to_uhwi ();
3133 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3134 *probable_max_size = *max_size = max.to_uhwi ();
3135 }
3136 else if (range_type == VR_ANTI_RANGE)
3137 {
3138 /* Anti range 0...N lets us to determine minimal size to N+1. */
3139 if (min == 0)
3140 {
3141 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3142 *min_size = max.to_uhwi () + 1;
3143 }
3144 /* Code like
3145
3146 int n;
3147 if (n < 100)
3148 memcpy (a, b, n)
3149
3150 Produce anti range allowing negative values of N. We still
3151 can use the information and make a guess that N is not negative.
3152 */
3153 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3154 *probable_max_size = min.to_uhwi () - 1;
3155 }
3156 }
3157 gcc_checking_assert (*max_size <=
3158 (unsigned HOST_WIDE_INT)
3159 GET_MODE_MASK (GET_MODE (len_rtx)));
3160 }
3161
3162 /* Expand a call EXP to the memcpy builtin.
3163 Return NULL_RTX if we failed, the caller should emit a normal call,
3164 otherwise try to get the result in TARGET, if convenient (and in
3165 mode MODE if that's convenient). */
3166
3167 static rtx
3168 expand_builtin_memcpy (tree exp, rtx target)
3169 {
3170 if (!validate_arglist (exp,
3171 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3172 return NULL_RTX;
3173 else
3174 {
3175 tree dest = CALL_EXPR_ARG (exp, 0);
3176 tree src = CALL_EXPR_ARG (exp, 1);
3177 tree len = CALL_EXPR_ARG (exp, 2);
3178 const char *src_str;
3179 unsigned int src_align = get_pointer_alignment (src);
3180 unsigned int dest_align = get_pointer_alignment (dest);
3181 rtx dest_mem, src_mem, dest_addr, len_rtx;
3182 HOST_WIDE_INT expected_size = -1;
3183 unsigned int expected_align = 0;
3184 unsigned HOST_WIDE_INT min_size;
3185 unsigned HOST_WIDE_INT max_size;
3186 unsigned HOST_WIDE_INT probable_max_size;
3187
3188 /* If DEST is not a pointer type, call the normal function. */
3189 if (dest_align == 0)
3190 return NULL_RTX;
3191
3192 /* If either SRC is not a pointer type, don't do this
3193 operation in-line. */
3194 if (src_align == 0)
3195 return NULL_RTX;
3196
3197 if (currently_expanding_gimple_stmt)
3198 stringop_block_profile (currently_expanding_gimple_stmt,
3199 &expected_align, &expected_size);
3200
3201 if (expected_align < dest_align)
3202 expected_align = dest_align;
3203 dest_mem = get_memory_rtx (dest, len);
3204 set_mem_align (dest_mem, dest_align);
3205 len_rtx = expand_normal (len);
3206 determine_block_size (len, len_rtx, &min_size, &max_size,
3207 &probable_max_size);
3208 src_str = c_getstr (src);
3209
3210 /* If SRC is a string constant and block move would be done
3211 by pieces, we can avoid loading the string from memory
3212 and only stored the computed constants. */
3213 if (src_str
3214 && CONST_INT_P (len_rtx)
3215 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3216 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3217 CONST_CAST (char *, src_str),
3218 dest_align, false))
3219 {
3220 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3221 builtin_memcpy_read_str,
3222 CONST_CAST (char *, src_str),
3223 dest_align, false, 0);
3224 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3225 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3226 return dest_mem;
3227 }
3228
3229 src_mem = get_memory_rtx (src, len);
3230 set_mem_align (src_mem, src_align);
3231
3232 /* Copy word part most expediently. */
3233 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3234 CALL_EXPR_TAILCALL (exp)
3235 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3236 expected_align, expected_size,
3237 min_size, max_size, probable_max_size);
3238
3239 if (dest_addr == 0)
3240 {
3241 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3242 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3243 }
3244 return dest_addr;
3245 }
3246 }
3247
3248 /* Expand a call EXP to the mempcpy builtin.
3249 Return NULL_RTX if we failed; the caller should emit a normal call,
3250 otherwise try to get the result in TARGET, if convenient (and in
3251 mode MODE if that's convenient). If ENDP is 0 return the
3252 destination pointer, if ENDP is 1 return the end pointer ala
3253 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3254 stpcpy. */
3255
3256 static rtx
3257 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3258 {
3259 if (!validate_arglist (exp,
3260 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3261 return NULL_RTX;
3262 else
3263 {
3264 tree dest = CALL_EXPR_ARG (exp, 0);
3265 tree src = CALL_EXPR_ARG (exp, 1);
3266 tree len = CALL_EXPR_ARG (exp, 2);
3267 return expand_builtin_mempcpy_args (dest, src, len,
3268 target, mode, /*endp=*/ 1);
3269 }
3270 }
3271
3272 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3273 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3274 so that this can also be called without constructing an actual CALL_EXPR.
3275 The other arguments and return value are the same as for
3276 expand_builtin_mempcpy. */
3277
3278 static rtx
3279 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3280 rtx target, enum machine_mode mode, int endp)
3281 {
3282 /* If return value is ignored, transform mempcpy into memcpy. */
3283 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3284 {
3285 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3286 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3287 dest, src, len);
3288 return expand_expr (result, target, mode, EXPAND_NORMAL);
3289 }
3290 else
3291 {
3292 const char *src_str;
3293 unsigned int src_align = get_pointer_alignment (src);
3294 unsigned int dest_align = get_pointer_alignment (dest);
3295 rtx dest_mem, src_mem, len_rtx;
3296
3297 /* If either SRC or DEST is not a pointer type, don't do this
3298 operation in-line. */
3299 if (dest_align == 0 || src_align == 0)
3300 return NULL_RTX;
3301
3302 /* If LEN is not constant, call the normal function. */
3303 if (! tree_fits_uhwi_p (len))
3304 return NULL_RTX;
3305
3306 len_rtx = expand_normal (len);
3307 src_str = c_getstr (src);
3308
3309 /* If SRC is a string constant and block move would be done
3310 by pieces, we can avoid loading the string from memory
3311 and only stored the computed constants. */
3312 if (src_str
3313 && CONST_INT_P (len_rtx)
3314 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3315 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3316 CONST_CAST (char *, src_str),
3317 dest_align, false))
3318 {
3319 dest_mem = get_memory_rtx (dest, len);
3320 set_mem_align (dest_mem, dest_align);
3321 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3322 builtin_memcpy_read_str,
3323 CONST_CAST (char *, src_str),
3324 dest_align, false, endp);
3325 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3326 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3327 return dest_mem;
3328 }
3329
3330 if (CONST_INT_P (len_rtx)
3331 && can_move_by_pieces (INTVAL (len_rtx),
3332 MIN (dest_align, src_align)))
3333 {
3334 dest_mem = get_memory_rtx (dest, len);
3335 set_mem_align (dest_mem, dest_align);
3336 src_mem = get_memory_rtx (src, len);
3337 set_mem_align (src_mem, src_align);
3338 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3339 MIN (dest_align, src_align), endp);
3340 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3341 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3342 return dest_mem;
3343 }
3344
3345 return NULL_RTX;
3346 }
3347 }
3348
3349 #ifndef HAVE_movstr
3350 # define HAVE_movstr 0
3351 # define CODE_FOR_movstr CODE_FOR_nothing
3352 #endif
3353
3354 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3355 we failed, the caller should emit a normal call, otherwise try to
3356 get the result in TARGET, if convenient. If ENDP is 0 return the
3357 destination pointer, if ENDP is 1 return the end pointer ala
3358 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3359 stpcpy. */
3360
3361 static rtx
3362 expand_movstr (tree dest, tree src, rtx target, int endp)
3363 {
3364 struct expand_operand ops[3];
3365 rtx dest_mem;
3366 rtx src_mem;
3367
3368 if (!HAVE_movstr)
3369 return NULL_RTX;
3370
3371 dest_mem = get_memory_rtx (dest, NULL);
3372 src_mem = get_memory_rtx (src, NULL);
3373 if (!endp)
3374 {
3375 target = force_reg (Pmode, XEXP (dest_mem, 0));
3376 dest_mem = replace_equiv_address (dest_mem, target);
3377 }
3378
3379 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3380 create_fixed_operand (&ops[1], dest_mem);
3381 create_fixed_operand (&ops[2], src_mem);
3382 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3383 return NULL_RTX;
3384
3385 if (endp && target != const0_rtx)
3386 {
3387 target = ops[0].value;
3388 /* movstr is supposed to set end to the address of the NUL
3389 terminator. If the caller requested a mempcpy-like return value,
3390 adjust it. */
3391 if (endp == 1)
3392 {
3393 rtx tem = plus_constant (GET_MODE (target),
3394 gen_lowpart (GET_MODE (target), target), 1);
3395 emit_move_insn (target, force_operand (tem, NULL_RTX));
3396 }
3397 }
3398 return target;
3399 }
3400
3401 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3402 NULL_RTX if we failed the caller should emit a normal call, otherwise
3403 try to get the result in TARGET, if convenient (and in mode MODE if that's
3404 convenient). */
3405
3406 static rtx
3407 expand_builtin_strcpy (tree exp, rtx target)
3408 {
3409 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3410 {
3411 tree dest = CALL_EXPR_ARG (exp, 0);
3412 tree src = CALL_EXPR_ARG (exp, 1);
3413 return expand_builtin_strcpy_args (dest, src, target);
3414 }
3415 return NULL_RTX;
3416 }
3417
3418 /* Helper function to do the actual work for expand_builtin_strcpy. The
3419 arguments to the builtin_strcpy call DEST and SRC are broken out
3420 so that this can also be called without constructing an actual CALL_EXPR.
3421 The other arguments and return value are the same as for
3422 expand_builtin_strcpy. */
3423
3424 static rtx
3425 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3426 {
3427 return expand_movstr (dest, src, target, /*endp=*/0);
3428 }
3429
3430 /* Expand a call EXP to the stpcpy builtin.
3431 Return NULL_RTX if we failed the caller should emit a normal call,
3432 otherwise try to get the result in TARGET, if convenient (and in
3433 mode MODE if that's convenient). */
3434
3435 static rtx
3436 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3437 {
3438 tree dst, src;
3439 location_t loc = EXPR_LOCATION (exp);
3440
3441 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3442 return NULL_RTX;
3443
3444 dst = CALL_EXPR_ARG (exp, 0);
3445 src = CALL_EXPR_ARG (exp, 1);
3446
3447 /* If return value is ignored, transform stpcpy into strcpy. */
3448 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3449 {
3450 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3451 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3452 return expand_expr (result, target, mode, EXPAND_NORMAL);
3453 }
3454 else
3455 {
3456 tree len, lenp1;
3457 rtx ret;
3458
3459 /* Ensure we get an actual string whose length can be evaluated at
3460 compile-time, not an expression containing a string. This is
3461 because the latter will potentially produce pessimized code
3462 when used to produce the return value. */
3463 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3464 return expand_movstr (dst, src, target, /*endp=*/2);
3465
3466 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3467 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3468 target, mode, /*endp=*/2);
3469
3470 if (ret)
3471 return ret;
3472
3473 if (TREE_CODE (len) == INTEGER_CST)
3474 {
3475 rtx len_rtx = expand_normal (len);
3476
3477 if (CONST_INT_P (len_rtx))
3478 {
3479 ret = expand_builtin_strcpy_args (dst, src, target);
3480
3481 if (ret)
3482 {
3483 if (! target)
3484 {
3485 if (mode != VOIDmode)
3486 target = gen_reg_rtx (mode);
3487 else
3488 target = gen_reg_rtx (GET_MODE (ret));
3489 }
3490 if (GET_MODE (target) != GET_MODE (ret))
3491 ret = gen_lowpart (GET_MODE (target), ret);
3492
3493 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3494 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3495 gcc_assert (ret);
3496
3497 return target;
3498 }
3499 }
3500 }
3501
3502 return expand_movstr (dst, src, target, /*endp=*/2);
3503 }
3504 }
3505
3506 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3507 bytes from constant string DATA + OFFSET and return it as target
3508 constant. */
3509
3510 rtx
3511 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3512 enum machine_mode mode)
3513 {
3514 const char *str = (const char *) data;
3515
3516 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3517 return const0_rtx;
3518
3519 return c_readstr (str + offset, mode);
3520 }
3521
3522 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3523 NULL_RTX if we failed the caller should emit a normal call. */
3524
3525 static rtx
3526 expand_builtin_strncpy (tree exp, rtx target)
3527 {
3528 location_t loc = EXPR_LOCATION (exp);
3529
3530 if (validate_arglist (exp,
3531 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3532 {
3533 tree dest = CALL_EXPR_ARG (exp, 0);
3534 tree src = CALL_EXPR_ARG (exp, 1);
3535 tree len = CALL_EXPR_ARG (exp, 2);
3536 tree slen = c_strlen (src, 1);
3537
3538 /* We must be passed a constant len and src parameter. */
3539 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3540 return NULL_RTX;
3541
3542 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3543
3544 /* We're required to pad with trailing zeros if the requested
3545 len is greater than strlen(s2)+1. In that case try to
3546 use store_by_pieces, if it fails, punt. */
3547 if (tree_int_cst_lt (slen, len))
3548 {
3549 unsigned int dest_align = get_pointer_alignment (dest);
3550 const char *p = c_getstr (src);
3551 rtx dest_mem;
3552
3553 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3554 || !can_store_by_pieces (tree_to_uhwi (len),
3555 builtin_strncpy_read_str,
3556 CONST_CAST (char *, p),
3557 dest_align, false))
3558 return NULL_RTX;
3559
3560 dest_mem = get_memory_rtx (dest, len);
3561 store_by_pieces (dest_mem, tree_to_uhwi (len),
3562 builtin_strncpy_read_str,
3563 CONST_CAST (char *, p), dest_align, false, 0);
3564 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3565 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3566 return dest_mem;
3567 }
3568 }
3569 return NULL_RTX;
3570 }
3571
3572 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3573 bytes from constant string DATA + OFFSET and return it as target
3574 constant. */
3575
3576 rtx
3577 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3578 enum machine_mode mode)
3579 {
3580 const char *c = (const char *) data;
3581 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3582
3583 memset (p, *c, GET_MODE_SIZE (mode));
3584
3585 return c_readstr (p, mode);
3586 }
3587
3588 /* Callback routine for store_by_pieces. Return the RTL of a register
3589 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3590 char value given in the RTL register data. For example, if mode is
3591 4 bytes wide, return the RTL for 0x01010101*data. */
3592
3593 static rtx
3594 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3595 enum machine_mode mode)
3596 {
3597 rtx target, coeff;
3598 size_t size;
3599 char *p;
3600
3601 size = GET_MODE_SIZE (mode);
3602 if (size == 1)
3603 return (rtx) data;
3604
3605 p = XALLOCAVEC (char, size);
3606 memset (p, 1, size);
3607 coeff = c_readstr (p, mode);
3608
3609 target = convert_to_mode (mode, (rtx) data, 1);
3610 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3611 return force_reg (mode, target);
3612 }
3613
3614 /* Expand expression EXP, which is a call to the memset builtin. Return
3615 NULL_RTX if we failed the caller should emit a normal call, otherwise
3616 try to get the result in TARGET, if convenient (and in mode MODE if that's
3617 convenient). */
3618
3619 static rtx
3620 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3621 {
3622 if (!validate_arglist (exp,
3623 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3624 return NULL_RTX;
3625 else
3626 {
3627 tree dest = CALL_EXPR_ARG (exp, 0);
3628 tree val = CALL_EXPR_ARG (exp, 1);
3629 tree len = CALL_EXPR_ARG (exp, 2);
3630 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3631 }
3632 }
3633
3634 /* Helper function to do the actual work for expand_builtin_memset. The
3635 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3636 so that this can also be called without constructing an actual CALL_EXPR.
3637 The other arguments and return value are the same as for
3638 expand_builtin_memset. */
3639
3640 static rtx
3641 expand_builtin_memset_args (tree dest, tree val, tree len,
3642 rtx target, enum machine_mode mode, tree orig_exp)
3643 {
3644 tree fndecl, fn;
3645 enum built_in_function fcode;
3646 enum machine_mode val_mode;
3647 char c;
3648 unsigned int dest_align;
3649 rtx dest_mem, dest_addr, len_rtx;
3650 HOST_WIDE_INT expected_size = -1;
3651 unsigned int expected_align = 0;
3652 unsigned HOST_WIDE_INT min_size;
3653 unsigned HOST_WIDE_INT max_size;
3654 unsigned HOST_WIDE_INT probable_max_size;
3655
3656 dest_align = get_pointer_alignment (dest);
3657
3658 /* If DEST is not a pointer type, don't do this operation in-line. */
3659 if (dest_align == 0)
3660 return NULL_RTX;
3661
3662 if (currently_expanding_gimple_stmt)
3663 stringop_block_profile (currently_expanding_gimple_stmt,
3664 &expected_align, &expected_size);
3665
3666 if (expected_align < dest_align)
3667 expected_align = dest_align;
3668
3669 /* If the LEN parameter is zero, return DEST. */
3670 if (integer_zerop (len))
3671 {
3672 /* Evaluate and ignore VAL in case it has side-effects. */
3673 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3674 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3675 }
3676
3677 /* Stabilize the arguments in case we fail. */
3678 dest = builtin_save_expr (dest);
3679 val = builtin_save_expr (val);
3680 len = builtin_save_expr (len);
3681
3682 len_rtx = expand_normal (len);
3683 determine_block_size (len, len_rtx, &min_size, &max_size,
3684 &probable_max_size);
3685 dest_mem = get_memory_rtx (dest, len);
3686 val_mode = TYPE_MODE (unsigned_char_type_node);
3687
3688 if (TREE_CODE (val) != INTEGER_CST)
3689 {
3690 rtx val_rtx;
3691
3692 val_rtx = expand_normal (val);
3693 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3694
3695 /* Assume that we can memset by pieces if we can store
3696 * the coefficients by pieces (in the required modes).
3697 * We can't pass builtin_memset_gen_str as that emits RTL. */
3698 c = 1;
3699 if (tree_fits_uhwi_p (len)
3700 && can_store_by_pieces (tree_to_uhwi (len),
3701 builtin_memset_read_str, &c, dest_align,
3702 true))
3703 {
3704 val_rtx = force_reg (val_mode, val_rtx);
3705 store_by_pieces (dest_mem, tree_to_uhwi (len),
3706 builtin_memset_gen_str, val_rtx, dest_align,
3707 true, 0);
3708 }
3709 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3710 dest_align, expected_align,
3711 expected_size, min_size, max_size,
3712 probable_max_size))
3713 goto do_libcall;
3714
3715 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3716 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3717 return dest_mem;
3718 }
3719
3720 if (target_char_cast (val, &c))
3721 goto do_libcall;
3722
3723 if (c)
3724 {
3725 if (tree_fits_uhwi_p (len)
3726 && can_store_by_pieces (tree_to_uhwi (len),
3727 builtin_memset_read_str, &c, dest_align,
3728 true))
3729 store_by_pieces (dest_mem, tree_to_uhwi (len),
3730 builtin_memset_read_str, &c, dest_align, true, 0);
3731 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3732 gen_int_mode (c, val_mode),
3733 dest_align, expected_align,
3734 expected_size, min_size, max_size,
3735 probable_max_size))
3736 goto do_libcall;
3737
3738 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3739 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3740 return dest_mem;
3741 }
3742
3743 set_mem_align (dest_mem, dest_align);
3744 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3745 CALL_EXPR_TAILCALL (orig_exp)
3746 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3747 expected_align, expected_size,
3748 min_size, max_size,
3749 probable_max_size);
3750
3751 if (dest_addr == 0)
3752 {
3753 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3754 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3755 }
3756
3757 return dest_addr;
3758
3759 do_libcall:
3760 fndecl = get_callee_fndecl (orig_exp);
3761 fcode = DECL_FUNCTION_CODE (fndecl);
3762 if (fcode == BUILT_IN_MEMSET)
3763 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3764 dest, val, len);
3765 else if (fcode == BUILT_IN_BZERO)
3766 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3767 dest, len);
3768 else
3769 gcc_unreachable ();
3770 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3771 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3772 return expand_call (fn, target, target == const0_rtx);
3773 }
3774
3775 /* Expand expression EXP, which is a call to the bzero builtin. Return
3776 NULL_RTX if we failed the caller should emit a normal call. */
3777
3778 static rtx
3779 expand_builtin_bzero (tree exp)
3780 {
3781 tree dest, size;
3782 location_t loc = EXPR_LOCATION (exp);
3783
3784 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3785 return NULL_RTX;
3786
3787 dest = CALL_EXPR_ARG (exp, 0);
3788 size = CALL_EXPR_ARG (exp, 1);
3789
3790 /* New argument list transforming bzero(ptr x, int y) to
3791 memset(ptr x, int 0, size_t y). This is done this way
3792 so that if it isn't expanded inline, we fallback to
3793 calling bzero instead of memset. */
3794
3795 return expand_builtin_memset_args (dest, integer_zero_node,
3796 fold_convert_loc (loc,
3797 size_type_node, size),
3798 const0_rtx, VOIDmode, exp);
3799 }
3800
3801 /* Expand expression EXP, which is a call to the memcmp built-in function.
3802 Return NULL_RTX if we failed and the caller should emit a normal call,
3803 otherwise try to get the result in TARGET, if convenient (and in mode
3804 MODE, if that's convenient). */
3805
3806 static rtx
3807 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3808 ATTRIBUTE_UNUSED enum machine_mode mode)
3809 {
3810 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3811
3812 if (!validate_arglist (exp,
3813 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3814 return NULL_RTX;
3815
3816 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3817 implementing memcmp because it will stop if it encounters two
3818 zero bytes. */
3819 #if defined HAVE_cmpmemsi
3820 {
3821 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3822 rtx result;
3823 rtx insn;
3824 tree arg1 = CALL_EXPR_ARG (exp, 0);
3825 tree arg2 = CALL_EXPR_ARG (exp, 1);
3826 tree len = CALL_EXPR_ARG (exp, 2);
3827
3828 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3829 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3830 enum machine_mode insn_mode;
3831
3832 if (HAVE_cmpmemsi)
3833 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3834 else
3835 return NULL_RTX;
3836
3837 /* If we don't have POINTER_TYPE, call the function. */
3838 if (arg1_align == 0 || arg2_align == 0)
3839 return NULL_RTX;
3840
3841 /* Make a place to write the result of the instruction. */
3842 result = target;
3843 if (! (result != 0
3844 && REG_P (result) && GET_MODE (result) == insn_mode
3845 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3846 result = gen_reg_rtx (insn_mode);
3847
3848 arg1_rtx = get_memory_rtx (arg1, len);
3849 arg2_rtx = get_memory_rtx (arg2, len);
3850 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3851
3852 /* Set MEM_SIZE as appropriate. */
3853 if (CONST_INT_P (arg3_rtx))
3854 {
3855 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3856 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3857 }
3858
3859 if (HAVE_cmpmemsi)
3860 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3861 GEN_INT (MIN (arg1_align, arg2_align)));
3862 else
3863 gcc_unreachable ();
3864
3865 if (insn)
3866 emit_insn (insn);
3867 else
3868 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3869 TYPE_MODE (integer_type_node), 3,
3870 XEXP (arg1_rtx, 0), Pmode,
3871 XEXP (arg2_rtx, 0), Pmode,
3872 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3873 TYPE_UNSIGNED (sizetype)),
3874 TYPE_MODE (sizetype));
3875
3876 /* Return the value in the proper mode for this function. */
3877 mode = TYPE_MODE (TREE_TYPE (exp));
3878 if (GET_MODE (result) == mode)
3879 return result;
3880 else if (target != 0)
3881 {
3882 convert_move (target, result, 0);
3883 return target;
3884 }
3885 else
3886 return convert_to_mode (mode, result, 0);
3887 }
3888 #endif /* HAVE_cmpmemsi. */
3889
3890 return NULL_RTX;
3891 }
3892
3893 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3894 if we failed the caller should emit a normal call, otherwise try to get
3895 the result in TARGET, if convenient. */
3896
3897 static rtx
3898 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3899 {
3900 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3901 return NULL_RTX;
3902
3903 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3904 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3905 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3906 {
3907 rtx arg1_rtx, arg2_rtx;
3908 rtx result, insn = NULL_RTX;
3909 tree fndecl, fn;
3910 tree arg1 = CALL_EXPR_ARG (exp, 0);
3911 tree arg2 = CALL_EXPR_ARG (exp, 1);
3912
3913 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3914 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3915
3916 /* If we don't have POINTER_TYPE, call the function. */
3917 if (arg1_align == 0 || arg2_align == 0)
3918 return NULL_RTX;
3919
3920 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3921 arg1 = builtin_save_expr (arg1);
3922 arg2 = builtin_save_expr (arg2);
3923
3924 arg1_rtx = get_memory_rtx (arg1, NULL);
3925 arg2_rtx = get_memory_rtx (arg2, NULL);
3926
3927 #ifdef HAVE_cmpstrsi
3928 /* Try to call cmpstrsi. */
3929 if (HAVE_cmpstrsi)
3930 {
3931 enum machine_mode insn_mode
3932 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3933
3934 /* Make a place to write the result of the instruction. */
3935 result = target;
3936 if (! (result != 0
3937 && REG_P (result) && GET_MODE (result) == insn_mode
3938 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3939 result = gen_reg_rtx (insn_mode);
3940
3941 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3942 GEN_INT (MIN (arg1_align, arg2_align)));
3943 }
3944 #endif
3945 #ifdef HAVE_cmpstrnsi
3946 /* Try to determine at least one length and call cmpstrnsi. */
3947 if (!insn && HAVE_cmpstrnsi)
3948 {
3949 tree len;
3950 rtx arg3_rtx;
3951
3952 enum machine_mode insn_mode
3953 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3954 tree len1 = c_strlen (arg1, 1);
3955 tree len2 = c_strlen (arg2, 1);
3956
3957 if (len1)
3958 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3959 if (len2)
3960 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3961
3962 /* If we don't have a constant length for the first, use the length
3963 of the second, if we know it. We don't require a constant for
3964 this case; some cost analysis could be done if both are available
3965 but neither is constant. For now, assume they're equally cheap,
3966 unless one has side effects. If both strings have constant lengths,
3967 use the smaller. */
3968
3969 if (!len1)
3970 len = len2;
3971 else if (!len2)
3972 len = len1;
3973 else if (TREE_SIDE_EFFECTS (len1))
3974 len = len2;
3975 else if (TREE_SIDE_EFFECTS (len2))
3976 len = len1;
3977 else if (TREE_CODE (len1) != INTEGER_CST)
3978 len = len2;
3979 else if (TREE_CODE (len2) != INTEGER_CST)
3980 len = len1;
3981 else if (tree_int_cst_lt (len1, len2))
3982 len = len1;
3983 else
3984 len = len2;
3985
3986 /* If both arguments have side effects, we cannot optimize. */
3987 if (!len || TREE_SIDE_EFFECTS (len))
3988 goto do_libcall;
3989
3990 arg3_rtx = expand_normal (len);
3991
3992 /* Make a place to write the result of the instruction. */
3993 result = target;
3994 if (! (result != 0
3995 && REG_P (result) && GET_MODE (result) == insn_mode
3996 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3997 result = gen_reg_rtx (insn_mode);
3998
3999 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4000 GEN_INT (MIN (arg1_align, arg2_align)));
4001 }
4002 #endif
4003
4004 if (insn)
4005 {
4006 enum machine_mode mode;
4007 emit_insn (insn);
4008
4009 /* Return the value in the proper mode for this function. */
4010 mode = TYPE_MODE (TREE_TYPE (exp));
4011 if (GET_MODE (result) == mode)
4012 return result;
4013 if (target == 0)
4014 return convert_to_mode (mode, result, 0);
4015 convert_move (target, result, 0);
4016 return target;
4017 }
4018
4019 /* Expand the library call ourselves using a stabilized argument
4020 list to avoid re-evaluating the function's arguments twice. */
4021 #ifdef HAVE_cmpstrnsi
4022 do_libcall:
4023 #endif
4024 fndecl = get_callee_fndecl (exp);
4025 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4026 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4027 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4028 return expand_call (fn, target, target == const0_rtx);
4029 }
4030 #endif
4031 return NULL_RTX;
4032 }
4033
4034 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4035 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4036 the result in TARGET, if convenient. */
4037
4038 static rtx
4039 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4040 ATTRIBUTE_UNUSED enum machine_mode mode)
4041 {
4042 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4043
4044 if (!validate_arglist (exp,
4045 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4046 return NULL_RTX;
4047
4048 /* If c_strlen can determine an expression for one of the string
4049 lengths, and it doesn't have side effects, then emit cmpstrnsi
4050 using length MIN(strlen(string)+1, arg3). */
4051 #ifdef HAVE_cmpstrnsi
4052 if (HAVE_cmpstrnsi)
4053 {
4054 tree len, len1, len2;
4055 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4056 rtx result, insn;
4057 tree fndecl, fn;
4058 tree arg1 = CALL_EXPR_ARG (exp, 0);
4059 tree arg2 = CALL_EXPR_ARG (exp, 1);
4060 tree arg3 = CALL_EXPR_ARG (exp, 2);
4061
4062 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4063 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4064 enum machine_mode insn_mode
4065 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4066
4067 len1 = c_strlen (arg1, 1);
4068 len2 = c_strlen (arg2, 1);
4069
4070 if (len1)
4071 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4072 if (len2)
4073 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4074
4075 /* If we don't have a constant length for the first, use the length
4076 of the second, if we know it. We don't require a constant for
4077 this case; some cost analysis could be done if both are available
4078 but neither is constant. For now, assume they're equally cheap,
4079 unless one has side effects. If both strings have constant lengths,
4080 use the smaller. */
4081
4082 if (!len1)
4083 len = len2;
4084 else if (!len2)
4085 len = len1;
4086 else if (TREE_SIDE_EFFECTS (len1))
4087 len = len2;
4088 else if (TREE_SIDE_EFFECTS (len2))
4089 len = len1;
4090 else if (TREE_CODE (len1) != INTEGER_CST)
4091 len = len2;
4092 else if (TREE_CODE (len2) != INTEGER_CST)
4093 len = len1;
4094 else if (tree_int_cst_lt (len1, len2))
4095 len = len1;
4096 else
4097 len = len2;
4098
4099 /* If both arguments have side effects, we cannot optimize. */
4100 if (!len || TREE_SIDE_EFFECTS (len))
4101 return NULL_RTX;
4102
4103 /* The actual new length parameter is MIN(len,arg3). */
4104 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4105 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4106
4107 /* If we don't have POINTER_TYPE, call the function. */
4108 if (arg1_align == 0 || arg2_align == 0)
4109 return NULL_RTX;
4110
4111 /* Make a place to write the result of the instruction. */
4112 result = target;
4113 if (! (result != 0
4114 && REG_P (result) && GET_MODE (result) == insn_mode
4115 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4116 result = gen_reg_rtx (insn_mode);
4117
4118 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4119 arg1 = builtin_save_expr (arg1);
4120 arg2 = builtin_save_expr (arg2);
4121 len = builtin_save_expr (len);
4122
4123 arg1_rtx = get_memory_rtx (arg1, len);
4124 arg2_rtx = get_memory_rtx (arg2, len);
4125 arg3_rtx = expand_normal (len);
4126 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4127 GEN_INT (MIN (arg1_align, arg2_align)));
4128 if (insn)
4129 {
4130 emit_insn (insn);
4131
4132 /* Return the value in the proper mode for this function. */
4133 mode = TYPE_MODE (TREE_TYPE (exp));
4134 if (GET_MODE (result) == mode)
4135 return result;
4136 if (target == 0)
4137 return convert_to_mode (mode, result, 0);
4138 convert_move (target, result, 0);
4139 return target;
4140 }
4141
4142 /* Expand the library call ourselves using a stabilized argument
4143 list to avoid re-evaluating the function's arguments twice. */
4144 fndecl = get_callee_fndecl (exp);
4145 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4146 arg1, arg2, len);
4147 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4148 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4149 return expand_call (fn, target, target == const0_rtx);
4150 }
4151 #endif
4152 return NULL_RTX;
4153 }
4154
4155 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4156 if that's convenient. */
4157
4158 rtx
4159 expand_builtin_saveregs (void)
4160 {
4161 rtx val, seq;
4162
4163 /* Don't do __builtin_saveregs more than once in a function.
4164 Save the result of the first call and reuse it. */
4165 if (saveregs_value != 0)
4166 return saveregs_value;
4167
4168 /* When this function is called, it means that registers must be
4169 saved on entry to this function. So we migrate the call to the
4170 first insn of this function. */
4171
4172 start_sequence ();
4173
4174 /* Do whatever the machine needs done in this case. */
4175 val = targetm.calls.expand_builtin_saveregs ();
4176
4177 seq = get_insns ();
4178 end_sequence ();
4179
4180 saveregs_value = val;
4181
4182 /* Put the insns after the NOTE that starts the function. If this
4183 is inside a start_sequence, make the outer-level insn chain current, so
4184 the code is placed at the start of the function. */
4185 push_topmost_sequence ();
4186 emit_insn_after (seq, entry_of_function ());
4187 pop_topmost_sequence ();
4188
4189 return val;
4190 }
4191
4192 /* Expand a call to __builtin_next_arg. */
4193
4194 static rtx
4195 expand_builtin_next_arg (void)
4196 {
4197 /* Checking arguments is already done in fold_builtin_next_arg
4198 that must be called before this function. */
4199 return expand_binop (ptr_mode, add_optab,
4200 crtl->args.internal_arg_pointer,
4201 crtl->args.arg_offset_rtx,
4202 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4203 }
4204
4205 /* Make it easier for the backends by protecting the valist argument
4206 from multiple evaluations. */
4207
4208 static tree
4209 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4210 {
4211 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4212
4213 /* The current way of determining the type of valist is completely
4214 bogus. We should have the information on the va builtin instead. */
4215 if (!vatype)
4216 vatype = targetm.fn_abi_va_list (cfun->decl);
4217
4218 if (TREE_CODE (vatype) == ARRAY_TYPE)
4219 {
4220 if (TREE_SIDE_EFFECTS (valist))
4221 valist = save_expr (valist);
4222
4223 /* For this case, the backends will be expecting a pointer to
4224 vatype, but it's possible we've actually been given an array
4225 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4226 So fix it. */
4227 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4228 {
4229 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4230 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4231 }
4232 }
4233 else
4234 {
4235 tree pt = build_pointer_type (vatype);
4236
4237 if (! needs_lvalue)
4238 {
4239 if (! TREE_SIDE_EFFECTS (valist))
4240 return valist;
4241
4242 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4243 TREE_SIDE_EFFECTS (valist) = 1;
4244 }
4245
4246 if (TREE_SIDE_EFFECTS (valist))
4247 valist = save_expr (valist);
4248 valist = fold_build2_loc (loc, MEM_REF,
4249 vatype, valist, build_int_cst (pt, 0));
4250 }
4251
4252 return valist;
4253 }
4254
4255 /* The "standard" definition of va_list is void*. */
4256
4257 tree
4258 std_build_builtin_va_list (void)
4259 {
4260 return ptr_type_node;
4261 }
4262
4263 /* The "standard" abi va_list is va_list_type_node. */
4264
4265 tree
4266 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4267 {
4268 return va_list_type_node;
4269 }
4270
4271 /* The "standard" type of va_list is va_list_type_node. */
4272
4273 tree
4274 std_canonical_va_list_type (tree type)
4275 {
4276 tree wtype, htype;
4277
4278 if (INDIRECT_REF_P (type))
4279 type = TREE_TYPE (type);
4280 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4281 type = TREE_TYPE (type);
4282 wtype = va_list_type_node;
4283 htype = type;
4284 /* Treat structure va_list types. */
4285 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4286 htype = TREE_TYPE (htype);
4287 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4288 {
4289 /* If va_list is an array type, the argument may have decayed
4290 to a pointer type, e.g. by being passed to another function.
4291 In that case, unwrap both types so that we can compare the
4292 underlying records. */
4293 if (TREE_CODE (htype) == ARRAY_TYPE
4294 || POINTER_TYPE_P (htype))
4295 {
4296 wtype = TREE_TYPE (wtype);
4297 htype = TREE_TYPE (htype);
4298 }
4299 }
4300 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4301 return va_list_type_node;
4302
4303 return NULL_TREE;
4304 }
4305
4306 /* The "standard" implementation of va_start: just assign `nextarg' to
4307 the variable. */
4308
4309 void
4310 std_expand_builtin_va_start (tree valist, rtx nextarg)
4311 {
4312 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4313 convert_move (va_r, nextarg, 0);
4314 }
4315
4316 /* Expand EXP, a call to __builtin_va_start. */
4317
4318 static rtx
4319 expand_builtin_va_start (tree exp)
4320 {
4321 rtx nextarg;
4322 tree valist;
4323 location_t loc = EXPR_LOCATION (exp);
4324
4325 if (call_expr_nargs (exp) < 2)
4326 {
4327 error_at (loc, "too few arguments to function %<va_start%>");
4328 return const0_rtx;
4329 }
4330
4331 if (fold_builtin_next_arg (exp, true))
4332 return const0_rtx;
4333
4334 nextarg = expand_builtin_next_arg ();
4335 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4336
4337 if (targetm.expand_builtin_va_start)
4338 targetm.expand_builtin_va_start (valist, nextarg);
4339 else
4340 std_expand_builtin_va_start (valist, nextarg);
4341
4342 return const0_rtx;
4343 }
4344
4345 /* Expand EXP, a call to __builtin_va_end. */
4346
4347 static rtx
4348 expand_builtin_va_end (tree exp)
4349 {
4350 tree valist = CALL_EXPR_ARG (exp, 0);
4351
4352 /* Evaluate for side effects, if needed. I hate macros that don't
4353 do that. */
4354 if (TREE_SIDE_EFFECTS (valist))
4355 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4356
4357 return const0_rtx;
4358 }
4359
4360 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4361 builtin rather than just as an assignment in stdarg.h because of the
4362 nastiness of array-type va_list types. */
4363
4364 static rtx
4365 expand_builtin_va_copy (tree exp)
4366 {
4367 tree dst, src, t;
4368 location_t loc = EXPR_LOCATION (exp);
4369
4370 dst = CALL_EXPR_ARG (exp, 0);
4371 src = CALL_EXPR_ARG (exp, 1);
4372
4373 dst = stabilize_va_list_loc (loc, dst, 1);
4374 src = stabilize_va_list_loc (loc, src, 0);
4375
4376 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4377
4378 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4379 {
4380 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4381 TREE_SIDE_EFFECTS (t) = 1;
4382 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4383 }
4384 else
4385 {
4386 rtx dstb, srcb, size;
4387
4388 /* Evaluate to pointers. */
4389 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4390 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4391 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4392 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4393
4394 dstb = convert_memory_address (Pmode, dstb);
4395 srcb = convert_memory_address (Pmode, srcb);
4396
4397 /* "Dereference" to BLKmode memories. */
4398 dstb = gen_rtx_MEM (BLKmode, dstb);
4399 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4400 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4401 srcb = gen_rtx_MEM (BLKmode, srcb);
4402 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4403 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4404
4405 /* Copy. */
4406 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4407 }
4408
4409 return const0_rtx;
4410 }
4411
4412 /* Expand a call to one of the builtin functions __builtin_frame_address or
4413 __builtin_return_address. */
4414
4415 static rtx
4416 expand_builtin_frame_address (tree fndecl, tree exp)
4417 {
4418 /* The argument must be a nonnegative integer constant.
4419 It counts the number of frames to scan up the stack.
4420 The value is the return address saved in that frame. */
4421 if (call_expr_nargs (exp) == 0)
4422 /* Warning about missing arg was already issued. */
4423 return const0_rtx;
4424 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4425 {
4426 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4427 error ("invalid argument to %<__builtin_frame_address%>");
4428 else
4429 error ("invalid argument to %<__builtin_return_address%>");
4430 return const0_rtx;
4431 }
4432 else
4433 {
4434 rtx tem
4435 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4436 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4437
4438 /* Some ports cannot access arbitrary stack frames. */
4439 if (tem == NULL)
4440 {
4441 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4442 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4443 else
4444 warning (0, "unsupported argument to %<__builtin_return_address%>");
4445 return const0_rtx;
4446 }
4447
4448 /* For __builtin_frame_address, return what we've got. */
4449 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4450 return tem;
4451
4452 if (!REG_P (tem)
4453 && ! CONSTANT_P (tem))
4454 tem = copy_addr_to_reg (tem);
4455 return tem;
4456 }
4457 }
4458
4459 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4460 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4461 is the same as for allocate_dynamic_stack_space. */
4462
4463 static rtx
4464 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4465 {
4466 rtx op0;
4467 rtx result;
4468 bool valid_arglist;
4469 unsigned int align;
4470 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4471 == BUILT_IN_ALLOCA_WITH_ALIGN);
4472
4473 valid_arglist
4474 = (alloca_with_align
4475 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4476 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4477
4478 if (!valid_arglist)
4479 return NULL_RTX;
4480
4481 /* Compute the argument. */
4482 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4483
4484 /* Compute the alignment. */
4485 align = (alloca_with_align
4486 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4487 : BIGGEST_ALIGNMENT);
4488
4489 /* Allocate the desired space. */
4490 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4491 result = convert_memory_address (ptr_mode, result);
4492
4493 return result;
4494 }
4495
4496 /* Expand a call to bswap builtin in EXP.
4497 Return NULL_RTX if a normal call should be emitted rather than expanding the
4498 function in-line. If convenient, the result should be placed in TARGET.
4499 SUBTARGET may be used as the target for computing one of EXP's operands. */
4500
4501 static rtx
4502 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4503 rtx subtarget)
4504 {
4505 tree arg;
4506 rtx op0;
4507
4508 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4509 return NULL_RTX;
4510
4511 arg = CALL_EXPR_ARG (exp, 0);
4512 op0 = expand_expr (arg,
4513 subtarget && GET_MODE (subtarget) == target_mode
4514 ? subtarget : NULL_RTX,
4515 target_mode, EXPAND_NORMAL);
4516 if (GET_MODE (op0) != target_mode)
4517 op0 = convert_to_mode (target_mode, op0, 1);
4518
4519 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4520
4521 gcc_assert (target);
4522
4523 return convert_to_mode (target_mode, target, 1);
4524 }
4525
4526 /* Expand a call to a unary builtin in EXP.
4527 Return NULL_RTX if a normal call should be emitted rather than expanding the
4528 function in-line. If convenient, the result should be placed in TARGET.
4529 SUBTARGET may be used as the target for computing one of EXP's operands. */
4530
4531 static rtx
4532 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4533 rtx subtarget, optab op_optab)
4534 {
4535 rtx op0;
4536
4537 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4538 return NULL_RTX;
4539
4540 /* Compute the argument. */
4541 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4542 (subtarget
4543 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4544 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4545 VOIDmode, EXPAND_NORMAL);
4546 /* Compute op, into TARGET if possible.
4547 Set TARGET to wherever the result comes back. */
4548 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4549 op_optab, op0, target, op_optab != clrsb_optab);
4550 gcc_assert (target);
4551
4552 return convert_to_mode (target_mode, target, 0);
4553 }
4554
4555 /* Expand a call to __builtin_expect. We just return our argument
4556 as the builtin_expect semantic should've been already executed by
4557 tree branch prediction pass. */
4558
4559 static rtx
4560 expand_builtin_expect (tree exp, rtx target)
4561 {
4562 tree arg;
4563
4564 if (call_expr_nargs (exp) < 2)
4565 return const0_rtx;
4566 arg = CALL_EXPR_ARG (exp, 0);
4567
4568 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4569 /* When guessing was done, the hints should be already stripped away. */
4570 gcc_assert (!flag_guess_branch_prob
4571 || optimize == 0 || seen_error ());
4572 return target;
4573 }
4574
4575 /* Expand a call to __builtin_assume_aligned. We just return our first
4576 argument as the builtin_assume_aligned semantic should've been already
4577 executed by CCP. */
4578
4579 static rtx
4580 expand_builtin_assume_aligned (tree exp, rtx target)
4581 {
4582 if (call_expr_nargs (exp) < 2)
4583 return const0_rtx;
4584 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4585 EXPAND_NORMAL);
4586 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4587 && (call_expr_nargs (exp) < 3
4588 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4589 return target;
4590 }
4591
4592 void
4593 expand_builtin_trap (void)
4594 {
4595 #ifdef HAVE_trap
4596 if (HAVE_trap)
4597 {
4598 rtx insn = emit_insn (gen_trap ());
4599 /* For trap insns when not accumulating outgoing args force
4600 REG_ARGS_SIZE note to prevent crossjumping of calls with
4601 different args sizes. */
4602 if (!ACCUMULATE_OUTGOING_ARGS)
4603 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4604 }
4605 else
4606 #endif
4607 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4608 emit_barrier ();
4609 }
4610
4611 /* Expand a call to __builtin_unreachable. We do nothing except emit
4612 a barrier saying that control flow will not pass here.
4613
4614 It is the responsibility of the program being compiled to ensure
4615 that control flow does never reach __builtin_unreachable. */
4616 static void
4617 expand_builtin_unreachable (void)
4618 {
4619 emit_barrier ();
4620 }
4621
4622 /* Expand EXP, a call to fabs, fabsf or fabsl.
4623 Return NULL_RTX if a normal call should be emitted rather than expanding
4624 the function inline. If convenient, the result should be placed
4625 in TARGET. SUBTARGET may be used as the target for computing
4626 the operand. */
4627
4628 static rtx
4629 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4630 {
4631 enum machine_mode mode;
4632 tree arg;
4633 rtx op0;
4634
4635 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4636 return NULL_RTX;
4637
4638 arg = CALL_EXPR_ARG (exp, 0);
4639 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4640 mode = TYPE_MODE (TREE_TYPE (arg));
4641 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4642 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4643 }
4644
4645 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4646 Return NULL is a normal call should be emitted rather than expanding the
4647 function inline. If convenient, the result should be placed in TARGET.
4648 SUBTARGET may be used as the target for computing the operand. */
4649
4650 static rtx
4651 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4652 {
4653 rtx op0, op1;
4654 tree arg;
4655
4656 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4657 return NULL_RTX;
4658
4659 arg = CALL_EXPR_ARG (exp, 0);
4660 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4661
4662 arg = CALL_EXPR_ARG (exp, 1);
4663 op1 = expand_normal (arg);
4664
4665 return expand_copysign (op0, op1, target);
4666 }
4667
4668 /* Expand a call to __builtin___clear_cache. */
4669
4670 static rtx
4671 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4672 {
4673 #ifndef HAVE_clear_cache
4674 #ifdef CLEAR_INSN_CACHE
4675 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4676 does something. Just do the default expansion to a call to
4677 __clear_cache(). */
4678 return NULL_RTX;
4679 #else
4680 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4681 does nothing. There is no need to call it. Do nothing. */
4682 return const0_rtx;
4683 #endif /* CLEAR_INSN_CACHE */
4684 #else
4685 /* We have a "clear_cache" insn, and it will handle everything. */
4686 tree begin, end;
4687 rtx begin_rtx, end_rtx;
4688
4689 /* We must not expand to a library call. If we did, any
4690 fallback library function in libgcc that might contain a call to
4691 __builtin___clear_cache() would recurse infinitely. */
4692 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4693 {
4694 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4695 return const0_rtx;
4696 }
4697
4698 if (HAVE_clear_cache)
4699 {
4700 struct expand_operand ops[2];
4701
4702 begin = CALL_EXPR_ARG (exp, 0);
4703 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4704
4705 end = CALL_EXPR_ARG (exp, 1);
4706 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4707
4708 create_address_operand (&ops[0], begin_rtx);
4709 create_address_operand (&ops[1], end_rtx);
4710 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4711 return const0_rtx;
4712 }
4713 return const0_rtx;
4714 #endif /* HAVE_clear_cache */
4715 }
4716
4717 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4718
4719 static rtx
4720 round_trampoline_addr (rtx tramp)
4721 {
4722 rtx temp, addend, mask;
4723
4724 /* If we don't need too much alignment, we'll have been guaranteed
4725 proper alignment by get_trampoline_type. */
4726 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4727 return tramp;
4728
4729 /* Round address up to desired boundary. */
4730 temp = gen_reg_rtx (Pmode);
4731 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4732 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4733
4734 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4735 temp, 0, OPTAB_LIB_WIDEN);
4736 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4737 temp, 0, OPTAB_LIB_WIDEN);
4738
4739 return tramp;
4740 }
4741
4742 static rtx
4743 expand_builtin_init_trampoline (tree exp, bool onstack)
4744 {
4745 tree t_tramp, t_func, t_chain;
4746 rtx m_tramp, r_tramp, r_chain, tmp;
4747
4748 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4749 POINTER_TYPE, VOID_TYPE))
4750 return NULL_RTX;
4751
4752 t_tramp = CALL_EXPR_ARG (exp, 0);
4753 t_func = CALL_EXPR_ARG (exp, 1);
4754 t_chain = CALL_EXPR_ARG (exp, 2);
4755
4756 r_tramp = expand_normal (t_tramp);
4757 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4758 MEM_NOTRAP_P (m_tramp) = 1;
4759
4760 /* If ONSTACK, the TRAMP argument should be the address of a field
4761 within the local function's FRAME decl. Either way, let's see if
4762 we can fill in the MEM_ATTRs for this memory. */
4763 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4764 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4765
4766 /* Creator of a heap trampoline is responsible for making sure the
4767 address is aligned to at least STACK_BOUNDARY. Normally malloc
4768 will ensure this anyhow. */
4769 tmp = round_trampoline_addr (r_tramp);
4770 if (tmp != r_tramp)
4771 {
4772 m_tramp = change_address (m_tramp, BLKmode, tmp);
4773 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4774 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4775 }
4776
4777 /* The FUNC argument should be the address of the nested function.
4778 Extract the actual function decl to pass to the hook. */
4779 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4780 t_func = TREE_OPERAND (t_func, 0);
4781 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4782
4783 r_chain = expand_normal (t_chain);
4784
4785 /* Generate insns to initialize the trampoline. */
4786 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4787
4788 if (onstack)
4789 {
4790 trampolines_created = 1;
4791
4792 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4793 "trampoline generated for nested function %qD", t_func);
4794 }
4795
4796 return const0_rtx;
4797 }
4798
4799 static rtx
4800 expand_builtin_adjust_trampoline (tree exp)
4801 {
4802 rtx tramp;
4803
4804 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4805 return NULL_RTX;
4806
4807 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4808 tramp = round_trampoline_addr (tramp);
4809 if (targetm.calls.trampoline_adjust_address)
4810 tramp = targetm.calls.trampoline_adjust_address (tramp);
4811
4812 return tramp;
4813 }
4814
4815 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4816 function. The function first checks whether the back end provides
4817 an insn to implement signbit for the respective mode. If not, it
4818 checks whether the floating point format of the value is such that
4819 the sign bit can be extracted. If that is not the case, the
4820 function returns NULL_RTX to indicate that a normal call should be
4821 emitted rather than expanding the function in-line. EXP is the
4822 expression that is a call to the builtin function; if convenient,
4823 the result should be placed in TARGET. */
4824 static rtx
4825 expand_builtin_signbit (tree exp, rtx target)
4826 {
4827 const struct real_format *fmt;
4828 enum machine_mode fmode, imode, rmode;
4829 tree arg;
4830 int word, bitpos;
4831 enum insn_code icode;
4832 rtx temp;
4833 location_t loc = EXPR_LOCATION (exp);
4834
4835 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4836 return NULL_RTX;
4837
4838 arg = CALL_EXPR_ARG (exp, 0);
4839 fmode = TYPE_MODE (TREE_TYPE (arg));
4840 rmode = TYPE_MODE (TREE_TYPE (exp));
4841 fmt = REAL_MODE_FORMAT (fmode);
4842
4843 arg = builtin_save_expr (arg);
4844
4845 /* Expand the argument yielding a RTX expression. */
4846 temp = expand_normal (arg);
4847
4848 /* Check if the back end provides an insn that handles signbit for the
4849 argument's mode. */
4850 icode = optab_handler (signbit_optab, fmode);
4851 if (icode != CODE_FOR_nothing)
4852 {
4853 rtx last = get_last_insn ();
4854 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4855 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4856 return target;
4857 delete_insns_since (last);
4858 }
4859
4860 /* For floating point formats without a sign bit, implement signbit
4861 as "ARG < 0.0". */
4862 bitpos = fmt->signbit_ro;
4863 if (bitpos < 0)
4864 {
4865 /* But we can't do this if the format supports signed zero. */
4866 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4867 return NULL_RTX;
4868
4869 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4870 build_real (TREE_TYPE (arg), dconst0));
4871 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4872 }
4873
4874 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4875 {
4876 imode = int_mode_for_mode (fmode);
4877 if (imode == BLKmode)
4878 return NULL_RTX;
4879 temp = gen_lowpart (imode, temp);
4880 }
4881 else
4882 {
4883 imode = word_mode;
4884 /* Handle targets with different FP word orders. */
4885 if (FLOAT_WORDS_BIG_ENDIAN)
4886 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4887 else
4888 word = bitpos / BITS_PER_WORD;
4889 temp = operand_subword_force (temp, word, fmode);
4890 bitpos = bitpos % BITS_PER_WORD;
4891 }
4892
4893 /* Force the intermediate word_mode (or narrower) result into a
4894 register. This avoids attempting to create paradoxical SUBREGs
4895 of floating point modes below. */
4896 temp = force_reg (imode, temp);
4897
4898 /* If the bitpos is within the "result mode" lowpart, the operation
4899 can be implement with a single bitwise AND. Otherwise, we need
4900 a right shift and an AND. */
4901
4902 if (bitpos < GET_MODE_BITSIZE (rmode))
4903 {
4904 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4905
4906 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4907 temp = gen_lowpart (rmode, temp);
4908 temp = expand_binop (rmode, and_optab, temp,
4909 immed_wide_int_const (mask, rmode),
4910 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4911 }
4912 else
4913 {
4914 /* Perform a logical right shift to place the signbit in the least
4915 significant bit, then truncate the result to the desired mode
4916 and mask just this bit. */
4917 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4918 temp = gen_lowpart (rmode, temp);
4919 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4920 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4921 }
4922
4923 return temp;
4924 }
4925
4926 /* Expand fork or exec calls. TARGET is the desired target of the
4927 call. EXP is the call. FN is the
4928 identificator of the actual function. IGNORE is nonzero if the
4929 value is to be ignored. */
4930
4931 static rtx
4932 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4933 {
4934 tree id, decl;
4935 tree call;
4936
4937 /* If we are not profiling, just call the function. */
4938 if (!profile_arc_flag)
4939 return NULL_RTX;
4940
4941 /* Otherwise call the wrapper. This should be equivalent for the rest of
4942 compiler, so the code does not diverge, and the wrapper may run the
4943 code necessary for keeping the profiling sane. */
4944
4945 switch (DECL_FUNCTION_CODE (fn))
4946 {
4947 case BUILT_IN_FORK:
4948 id = get_identifier ("__gcov_fork");
4949 break;
4950
4951 case BUILT_IN_EXECL:
4952 id = get_identifier ("__gcov_execl");
4953 break;
4954
4955 case BUILT_IN_EXECV:
4956 id = get_identifier ("__gcov_execv");
4957 break;
4958
4959 case BUILT_IN_EXECLP:
4960 id = get_identifier ("__gcov_execlp");
4961 break;
4962
4963 case BUILT_IN_EXECLE:
4964 id = get_identifier ("__gcov_execle");
4965 break;
4966
4967 case BUILT_IN_EXECVP:
4968 id = get_identifier ("__gcov_execvp");
4969 break;
4970
4971 case BUILT_IN_EXECVE:
4972 id = get_identifier ("__gcov_execve");
4973 break;
4974
4975 default:
4976 gcc_unreachable ();
4977 }
4978
4979 decl = build_decl (DECL_SOURCE_LOCATION (fn),
4980 FUNCTION_DECL, id, TREE_TYPE (fn));
4981 DECL_EXTERNAL (decl) = 1;
4982 TREE_PUBLIC (decl) = 1;
4983 DECL_ARTIFICIAL (decl) = 1;
4984 TREE_NOTHROW (decl) = 1;
4985 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4986 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4987 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
4988 return expand_call (call, target, ignore);
4989 }
4990
4991
4992 \f
4993 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
4994 the pointer in these functions is void*, the tree optimizers may remove
4995 casts. The mode computed in expand_builtin isn't reliable either, due
4996 to __sync_bool_compare_and_swap.
4997
4998 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
4999 group of builtins. This gives us log2 of the mode size. */
5000
5001 static inline enum machine_mode
5002 get_builtin_sync_mode (int fcode_diff)
5003 {
5004 /* The size is not negotiable, so ask not to get BLKmode in return
5005 if the target indicates that a smaller size would be better. */
5006 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5007 }
5008
5009 /* Expand the memory expression LOC and return the appropriate memory operand
5010 for the builtin_sync operations. */
5011
5012 static rtx
5013 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5014 {
5015 rtx addr, mem;
5016
5017 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5018 addr = convert_memory_address (Pmode, addr);
5019
5020 /* Note that we explicitly do not want any alias information for this
5021 memory, so that we kill all other live memories. Otherwise we don't
5022 satisfy the full barrier semantics of the intrinsic. */
5023 mem = validize_mem (gen_rtx_MEM (mode, addr));
5024
5025 /* The alignment needs to be at least according to that of the mode. */
5026 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5027 get_pointer_alignment (loc)));
5028 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5029 MEM_VOLATILE_P (mem) = 1;
5030
5031 return mem;
5032 }
5033
5034 /* Make sure an argument is in the right mode.
5035 EXP is the tree argument.
5036 MODE is the mode it should be in. */
5037
5038 static rtx
5039 expand_expr_force_mode (tree exp, enum machine_mode mode)
5040 {
5041 rtx val;
5042 enum machine_mode old_mode;
5043
5044 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5045 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5046 of CONST_INTs, where we know the old_mode only from the call argument. */
5047
5048 old_mode = GET_MODE (val);
5049 if (old_mode == VOIDmode)
5050 old_mode = TYPE_MODE (TREE_TYPE (exp));
5051 val = convert_modes (mode, old_mode, val, 1);
5052 return val;
5053 }
5054
5055
5056 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5057 EXP is the CALL_EXPR. CODE is the rtx code
5058 that corresponds to the arithmetic or logical operation from the name;
5059 an exception here is that NOT actually means NAND. TARGET is an optional
5060 place for us to store the results; AFTER is true if this is the
5061 fetch_and_xxx form. */
5062
5063 static rtx
5064 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5065 enum rtx_code code, bool after,
5066 rtx target)
5067 {
5068 rtx val, mem;
5069 location_t loc = EXPR_LOCATION (exp);
5070
5071 if (code == NOT && warn_sync_nand)
5072 {
5073 tree fndecl = get_callee_fndecl (exp);
5074 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5075
5076 static bool warned_f_a_n, warned_n_a_f;
5077
5078 switch (fcode)
5079 {
5080 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5081 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5082 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5083 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5084 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5085 if (warned_f_a_n)
5086 break;
5087
5088 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5089 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5090 warned_f_a_n = true;
5091 break;
5092
5093 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5094 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5095 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5096 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5097 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5098 if (warned_n_a_f)
5099 break;
5100
5101 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5102 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5103 warned_n_a_f = true;
5104 break;
5105
5106 default:
5107 gcc_unreachable ();
5108 }
5109 }
5110
5111 /* Expand the operands. */
5112 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5113 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5114
5115 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5116 after);
5117 }
5118
5119 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5120 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5121 true if this is the boolean form. TARGET is a place for us to store the
5122 results; this is NOT optional if IS_BOOL is true. */
5123
5124 static rtx
5125 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5126 bool is_bool, rtx target)
5127 {
5128 rtx old_val, new_val, mem;
5129 rtx *pbool, *poval;
5130
5131 /* Expand the operands. */
5132 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5133 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5134 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5135
5136 pbool = poval = NULL;
5137 if (target != const0_rtx)
5138 {
5139 if (is_bool)
5140 pbool = &target;
5141 else
5142 poval = &target;
5143 }
5144 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5145 false, MEMMODEL_SEQ_CST,
5146 MEMMODEL_SEQ_CST))
5147 return NULL_RTX;
5148
5149 return target;
5150 }
5151
5152 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5153 general form is actually an atomic exchange, and some targets only
5154 support a reduced form with the second argument being a constant 1.
5155 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5156 the results. */
5157
5158 static rtx
5159 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5160 rtx target)
5161 {
5162 rtx val, mem;
5163
5164 /* Expand the operands. */
5165 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5166 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5167
5168 return expand_sync_lock_test_and_set (target, mem, val);
5169 }
5170
5171 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5172
5173 static void
5174 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5175 {
5176 rtx mem;
5177
5178 /* Expand the operands. */
5179 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5180
5181 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5182 }
5183
5184 /* Given an integer representing an ``enum memmodel'', verify its
5185 correctness and return the memory model enum. */
5186
5187 static enum memmodel
5188 get_memmodel (tree exp)
5189 {
5190 rtx op;
5191 unsigned HOST_WIDE_INT val;
5192
5193 /* If the parameter is not a constant, it's a run time value so we'll just
5194 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5195 if (TREE_CODE (exp) != INTEGER_CST)
5196 return MEMMODEL_SEQ_CST;
5197
5198 op = expand_normal (exp);
5199
5200 val = INTVAL (op);
5201 if (targetm.memmodel_check)
5202 val = targetm.memmodel_check (val);
5203 else if (val & ~MEMMODEL_MASK)
5204 {
5205 warning (OPT_Winvalid_memory_model,
5206 "Unknown architecture specifier in memory model to builtin.");
5207 return MEMMODEL_SEQ_CST;
5208 }
5209
5210 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5211 {
5212 warning (OPT_Winvalid_memory_model,
5213 "invalid memory model argument to builtin");
5214 return MEMMODEL_SEQ_CST;
5215 }
5216
5217 return (enum memmodel) val;
5218 }
5219
5220 /* Expand the __atomic_exchange intrinsic:
5221 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5222 EXP is the CALL_EXPR.
5223 TARGET is an optional place for us to store the results. */
5224
5225 static rtx
5226 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5227 {
5228 rtx val, mem;
5229 enum memmodel model;
5230
5231 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5232 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5233 {
5234 error ("invalid memory model for %<__atomic_exchange%>");
5235 return NULL_RTX;
5236 }
5237
5238 if (!flag_inline_atomics)
5239 return NULL_RTX;
5240
5241 /* Expand the operands. */
5242 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5243 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5244
5245 return expand_atomic_exchange (target, mem, val, model);
5246 }
5247
5248 /* Expand the __atomic_compare_exchange intrinsic:
5249 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5250 TYPE desired, BOOL weak,
5251 enum memmodel success,
5252 enum memmodel failure)
5253 EXP is the CALL_EXPR.
5254 TARGET is an optional place for us to store the results. */
5255
5256 static rtx
5257 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5258 rtx target)
5259 {
5260 rtx expect, desired, mem, oldval, label;
5261 enum memmodel success, failure;
5262 tree weak;
5263 bool is_weak;
5264
5265 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5266 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5267
5268 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5269 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5270 {
5271 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5272 return NULL_RTX;
5273 }
5274
5275 if (failure > success)
5276 {
5277 error ("failure memory model cannot be stronger than success "
5278 "memory model for %<__atomic_compare_exchange%>");
5279 return NULL_RTX;
5280 }
5281
5282 if (!flag_inline_atomics)
5283 return NULL_RTX;
5284
5285 /* Expand the operands. */
5286 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5287
5288 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5289 expect = convert_memory_address (Pmode, expect);
5290 expect = gen_rtx_MEM (mode, expect);
5291 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5292
5293 weak = CALL_EXPR_ARG (exp, 3);
5294 is_weak = false;
5295 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5296 is_weak = true;
5297
5298 if (target == const0_rtx)
5299 target = NULL;
5300
5301 /* Lest the rtl backend create a race condition with an imporoper store
5302 to memory, always create a new pseudo for OLDVAL. */
5303 oldval = NULL;
5304
5305 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5306 is_weak, success, failure))
5307 return NULL_RTX;
5308
5309 /* Conditionally store back to EXPECT, lest we create a race condition
5310 with an improper store to memory. */
5311 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5312 the normal case where EXPECT is totally private, i.e. a register. At
5313 which point the store can be unconditional. */
5314 label = gen_label_rtx ();
5315 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5316 emit_move_insn (expect, oldval);
5317 emit_label (label);
5318
5319 return target;
5320 }
5321
5322 /* Expand the __atomic_load intrinsic:
5323 TYPE __atomic_load (TYPE *object, enum memmodel)
5324 EXP is the CALL_EXPR.
5325 TARGET is an optional place for us to store the results. */
5326
5327 static rtx
5328 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5329 {
5330 rtx mem;
5331 enum memmodel model;
5332
5333 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5334 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5335 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5336 {
5337 error ("invalid memory model for %<__atomic_load%>");
5338 return NULL_RTX;
5339 }
5340
5341 if (!flag_inline_atomics)
5342 return NULL_RTX;
5343
5344 /* Expand the operand. */
5345 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5346
5347 return expand_atomic_load (target, mem, model);
5348 }
5349
5350
5351 /* Expand the __atomic_store intrinsic:
5352 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5353 EXP is the CALL_EXPR.
5354 TARGET is an optional place for us to store the results. */
5355
5356 static rtx
5357 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5358 {
5359 rtx mem, val;
5360 enum memmodel model;
5361
5362 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5363 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5364 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5365 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5366 {
5367 error ("invalid memory model for %<__atomic_store%>");
5368 return NULL_RTX;
5369 }
5370
5371 if (!flag_inline_atomics)
5372 return NULL_RTX;
5373
5374 /* Expand the operands. */
5375 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5376 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5377
5378 return expand_atomic_store (mem, val, model, false);
5379 }
5380
5381 /* Expand the __atomic_fetch_XXX intrinsic:
5382 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5383 EXP is the CALL_EXPR.
5384 TARGET is an optional place for us to store the results.
5385 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5386 FETCH_AFTER is true if returning the result of the operation.
5387 FETCH_AFTER is false if returning the value before the operation.
5388 IGNORE is true if the result is not used.
5389 EXT_CALL is the correct builtin for an external call if this cannot be
5390 resolved to an instruction sequence. */
5391
5392 static rtx
5393 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5394 enum rtx_code code, bool fetch_after,
5395 bool ignore, enum built_in_function ext_call)
5396 {
5397 rtx val, mem, ret;
5398 enum memmodel model;
5399 tree fndecl;
5400 tree addr;
5401
5402 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5403
5404 /* Expand the operands. */
5405 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5406 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5407
5408 /* Only try generating instructions if inlining is turned on. */
5409 if (flag_inline_atomics)
5410 {
5411 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5412 if (ret)
5413 return ret;
5414 }
5415
5416 /* Return if a different routine isn't needed for the library call. */
5417 if (ext_call == BUILT_IN_NONE)
5418 return NULL_RTX;
5419
5420 /* Change the call to the specified function. */
5421 fndecl = get_callee_fndecl (exp);
5422 addr = CALL_EXPR_FN (exp);
5423 STRIP_NOPS (addr);
5424
5425 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5426 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5427
5428 /* Expand the call here so we can emit trailing code. */
5429 ret = expand_call (exp, target, ignore);
5430
5431 /* Replace the original function just in case it matters. */
5432 TREE_OPERAND (addr, 0) = fndecl;
5433
5434 /* Then issue the arithmetic correction to return the right result. */
5435 if (!ignore)
5436 {
5437 if (code == NOT)
5438 {
5439 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5440 OPTAB_LIB_WIDEN);
5441 ret = expand_simple_unop (mode, NOT, ret, target, true);
5442 }
5443 else
5444 ret = expand_simple_binop (mode, code, ret, val, target, true,
5445 OPTAB_LIB_WIDEN);
5446 }
5447 return ret;
5448 }
5449
5450
5451 #ifndef HAVE_atomic_clear
5452 # define HAVE_atomic_clear 0
5453 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5454 #endif
5455
5456 /* Expand an atomic clear operation.
5457 void _atomic_clear (BOOL *obj, enum memmodel)
5458 EXP is the call expression. */
5459
5460 static rtx
5461 expand_builtin_atomic_clear (tree exp)
5462 {
5463 enum machine_mode mode;
5464 rtx mem, ret;
5465 enum memmodel model;
5466
5467 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5468 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5469 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5470
5471 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5472 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5473 {
5474 error ("invalid memory model for %<__atomic_store%>");
5475 return const0_rtx;
5476 }
5477
5478 if (HAVE_atomic_clear)
5479 {
5480 emit_insn (gen_atomic_clear (mem, model));
5481 return const0_rtx;
5482 }
5483
5484 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5485 Failing that, a store is issued by __atomic_store. The only way this can
5486 fail is if the bool type is larger than a word size. Unlikely, but
5487 handle it anyway for completeness. Assume a single threaded model since
5488 there is no atomic support in this case, and no barriers are required. */
5489 ret = expand_atomic_store (mem, const0_rtx, model, true);
5490 if (!ret)
5491 emit_move_insn (mem, const0_rtx);
5492 return const0_rtx;
5493 }
5494
5495 /* Expand an atomic test_and_set operation.
5496 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5497 EXP is the call expression. */
5498
5499 static rtx
5500 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5501 {
5502 rtx mem;
5503 enum memmodel model;
5504 enum machine_mode mode;
5505
5506 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5507 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5508 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5509
5510 return expand_atomic_test_and_set (target, mem, model);
5511 }
5512
5513
5514 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5515 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5516
5517 static tree
5518 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5519 {
5520 int size;
5521 enum machine_mode mode;
5522 unsigned int mode_align, type_align;
5523
5524 if (TREE_CODE (arg0) != INTEGER_CST)
5525 return NULL_TREE;
5526
5527 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5528 mode = mode_for_size (size, MODE_INT, 0);
5529 mode_align = GET_MODE_ALIGNMENT (mode);
5530
5531 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5532 type_align = mode_align;
5533 else
5534 {
5535 tree ttype = TREE_TYPE (arg1);
5536
5537 /* This function is usually invoked and folded immediately by the front
5538 end before anything else has a chance to look at it. The pointer
5539 parameter at this point is usually cast to a void *, so check for that
5540 and look past the cast. */
5541 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5542 && VOID_TYPE_P (TREE_TYPE (ttype)))
5543 arg1 = TREE_OPERAND (arg1, 0);
5544
5545 ttype = TREE_TYPE (arg1);
5546 gcc_assert (POINTER_TYPE_P (ttype));
5547
5548 /* Get the underlying type of the object. */
5549 ttype = TREE_TYPE (ttype);
5550 type_align = TYPE_ALIGN (ttype);
5551 }
5552
5553 /* If the object has smaller alignment, the the lock free routines cannot
5554 be used. */
5555 if (type_align < mode_align)
5556 return boolean_false_node;
5557
5558 /* Check if a compare_and_swap pattern exists for the mode which represents
5559 the required size. The pattern is not allowed to fail, so the existence
5560 of the pattern indicates support is present. */
5561 if (can_compare_and_swap_p (mode, true))
5562 return boolean_true_node;
5563 else
5564 return boolean_false_node;
5565 }
5566
5567 /* Return true if the parameters to call EXP represent an object which will
5568 always generate lock free instructions. The first argument represents the
5569 size of the object, and the second parameter is a pointer to the object
5570 itself. If NULL is passed for the object, then the result is based on
5571 typical alignment for an object of the specified size. Otherwise return
5572 false. */
5573
5574 static rtx
5575 expand_builtin_atomic_always_lock_free (tree exp)
5576 {
5577 tree size;
5578 tree arg0 = CALL_EXPR_ARG (exp, 0);
5579 tree arg1 = CALL_EXPR_ARG (exp, 1);
5580
5581 if (TREE_CODE (arg0) != INTEGER_CST)
5582 {
5583 error ("non-constant argument 1 to __atomic_always_lock_free");
5584 return const0_rtx;
5585 }
5586
5587 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5588 if (size == boolean_true_node)
5589 return const1_rtx;
5590 return const0_rtx;
5591 }
5592
5593 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5594 is lock free on this architecture. */
5595
5596 static tree
5597 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5598 {
5599 if (!flag_inline_atomics)
5600 return NULL_TREE;
5601
5602 /* If it isn't always lock free, don't generate a result. */
5603 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5604 return boolean_true_node;
5605
5606 return NULL_TREE;
5607 }
5608
5609 /* Return true if the parameters to call EXP represent an object which will
5610 always generate lock free instructions. The first argument represents the
5611 size of the object, and the second parameter is a pointer to the object
5612 itself. If NULL is passed for the object, then the result is based on
5613 typical alignment for an object of the specified size. Otherwise return
5614 NULL*/
5615
5616 static rtx
5617 expand_builtin_atomic_is_lock_free (tree exp)
5618 {
5619 tree size;
5620 tree arg0 = CALL_EXPR_ARG (exp, 0);
5621 tree arg1 = CALL_EXPR_ARG (exp, 1);
5622
5623 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5624 {
5625 error ("non-integer argument 1 to __atomic_is_lock_free");
5626 return NULL_RTX;
5627 }
5628
5629 if (!flag_inline_atomics)
5630 return NULL_RTX;
5631
5632 /* If the value is known at compile time, return the RTX for it. */
5633 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5634 if (size == boolean_true_node)
5635 return const1_rtx;
5636
5637 return NULL_RTX;
5638 }
5639
5640 /* Expand the __atomic_thread_fence intrinsic:
5641 void __atomic_thread_fence (enum memmodel)
5642 EXP is the CALL_EXPR. */
5643
5644 static void
5645 expand_builtin_atomic_thread_fence (tree exp)
5646 {
5647 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5648 expand_mem_thread_fence (model);
5649 }
5650
5651 /* Expand the __atomic_signal_fence intrinsic:
5652 void __atomic_signal_fence (enum memmodel)
5653 EXP is the CALL_EXPR. */
5654
5655 static void
5656 expand_builtin_atomic_signal_fence (tree exp)
5657 {
5658 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5659 expand_mem_signal_fence (model);
5660 }
5661
5662 /* Expand the __sync_synchronize intrinsic. */
5663
5664 static void
5665 expand_builtin_sync_synchronize (void)
5666 {
5667 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5668 }
5669
5670 static rtx
5671 expand_builtin_thread_pointer (tree exp, rtx target)
5672 {
5673 enum insn_code icode;
5674 if (!validate_arglist (exp, VOID_TYPE))
5675 return const0_rtx;
5676 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5677 if (icode != CODE_FOR_nothing)
5678 {
5679 struct expand_operand op;
5680 /* If the target is not sutitable then create a new target. */
5681 if (target == NULL_RTX
5682 || !REG_P (target)
5683 || GET_MODE (target) != Pmode)
5684 target = gen_reg_rtx (Pmode);
5685 create_output_operand (&op, target, Pmode);
5686 expand_insn (icode, 1, &op);
5687 return target;
5688 }
5689 error ("__builtin_thread_pointer is not supported on this target");
5690 return const0_rtx;
5691 }
5692
5693 static void
5694 expand_builtin_set_thread_pointer (tree exp)
5695 {
5696 enum insn_code icode;
5697 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5698 return;
5699 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5700 if (icode != CODE_FOR_nothing)
5701 {
5702 struct expand_operand op;
5703 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5704 Pmode, EXPAND_NORMAL);
5705 create_input_operand (&op, val, Pmode);
5706 expand_insn (icode, 1, &op);
5707 return;
5708 }
5709 error ("__builtin_set_thread_pointer is not supported on this target");
5710 }
5711
5712 \f
5713 /* Emit code to restore the current value of stack. */
5714
5715 static void
5716 expand_stack_restore (tree var)
5717 {
5718 rtx prev, sa = expand_normal (var);
5719
5720 sa = convert_memory_address (Pmode, sa);
5721
5722 prev = get_last_insn ();
5723 emit_stack_restore (SAVE_BLOCK, sa);
5724 fixup_args_size_notes (prev, get_last_insn (), 0);
5725 }
5726
5727
5728 /* Emit code to save the current value of stack. */
5729
5730 static rtx
5731 expand_stack_save (void)
5732 {
5733 rtx ret = NULL_RTX;
5734
5735 do_pending_stack_adjust ();
5736 emit_stack_save (SAVE_BLOCK, &ret);
5737 return ret;
5738 }
5739
5740 /* Expand an expression EXP that calls a built-in function,
5741 with result going to TARGET if that's convenient
5742 (and in mode MODE if that's convenient).
5743 SUBTARGET may be used as the target for computing one of EXP's operands.
5744 IGNORE is nonzero if the value is to be ignored. */
5745
5746 rtx
5747 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5748 int ignore)
5749 {
5750 tree fndecl = get_callee_fndecl (exp);
5751 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5752 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5753 int flags;
5754
5755 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5756 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5757
5758 /* When not optimizing, generate calls to library functions for a certain
5759 set of builtins. */
5760 if (!optimize
5761 && !called_as_built_in (fndecl)
5762 && fcode != BUILT_IN_FORK
5763 && fcode != BUILT_IN_EXECL
5764 && fcode != BUILT_IN_EXECV
5765 && fcode != BUILT_IN_EXECLP
5766 && fcode != BUILT_IN_EXECLE
5767 && fcode != BUILT_IN_EXECVP
5768 && fcode != BUILT_IN_EXECVE
5769 && fcode != BUILT_IN_ALLOCA
5770 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5771 && fcode != BUILT_IN_FREE)
5772 return expand_call (exp, target, ignore);
5773
5774 /* The built-in function expanders test for target == const0_rtx
5775 to determine whether the function's result will be ignored. */
5776 if (ignore)
5777 target = const0_rtx;
5778
5779 /* If the result of a pure or const built-in function is ignored, and
5780 none of its arguments are volatile, we can avoid expanding the
5781 built-in call and just evaluate the arguments for side-effects. */
5782 if (target == const0_rtx
5783 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5784 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5785 {
5786 bool volatilep = false;
5787 tree arg;
5788 call_expr_arg_iterator iter;
5789
5790 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5791 if (TREE_THIS_VOLATILE (arg))
5792 {
5793 volatilep = true;
5794 break;
5795 }
5796
5797 if (! volatilep)
5798 {
5799 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5800 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5801 return const0_rtx;
5802 }
5803 }
5804
5805 switch (fcode)
5806 {
5807 CASE_FLT_FN (BUILT_IN_FABS):
5808 case BUILT_IN_FABSD32:
5809 case BUILT_IN_FABSD64:
5810 case BUILT_IN_FABSD128:
5811 target = expand_builtin_fabs (exp, target, subtarget);
5812 if (target)
5813 return target;
5814 break;
5815
5816 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5817 target = expand_builtin_copysign (exp, target, subtarget);
5818 if (target)
5819 return target;
5820 break;
5821
5822 /* Just do a normal library call if we were unable to fold
5823 the values. */
5824 CASE_FLT_FN (BUILT_IN_CABS):
5825 break;
5826
5827 CASE_FLT_FN (BUILT_IN_EXP):
5828 CASE_FLT_FN (BUILT_IN_EXP10):
5829 CASE_FLT_FN (BUILT_IN_POW10):
5830 CASE_FLT_FN (BUILT_IN_EXP2):
5831 CASE_FLT_FN (BUILT_IN_EXPM1):
5832 CASE_FLT_FN (BUILT_IN_LOGB):
5833 CASE_FLT_FN (BUILT_IN_LOG):
5834 CASE_FLT_FN (BUILT_IN_LOG10):
5835 CASE_FLT_FN (BUILT_IN_LOG2):
5836 CASE_FLT_FN (BUILT_IN_LOG1P):
5837 CASE_FLT_FN (BUILT_IN_TAN):
5838 CASE_FLT_FN (BUILT_IN_ASIN):
5839 CASE_FLT_FN (BUILT_IN_ACOS):
5840 CASE_FLT_FN (BUILT_IN_ATAN):
5841 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5842 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5843 because of possible accuracy problems. */
5844 if (! flag_unsafe_math_optimizations)
5845 break;
5846 CASE_FLT_FN (BUILT_IN_SQRT):
5847 CASE_FLT_FN (BUILT_IN_FLOOR):
5848 CASE_FLT_FN (BUILT_IN_CEIL):
5849 CASE_FLT_FN (BUILT_IN_TRUNC):
5850 CASE_FLT_FN (BUILT_IN_ROUND):
5851 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5852 CASE_FLT_FN (BUILT_IN_RINT):
5853 target = expand_builtin_mathfn (exp, target, subtarget);
5854 if (target)
5855 return target;
5856 break;
5857
5858 CASE_FLT_FN (BUILT_IN_FMA):
5859 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5860 if (target)
5861 return target;
5862 break;
5863
5864 CASE_FLT_FN (BUILT_IN_ILOGB):
5865 if (! flag_unsafe_math_optimizations)
5866 break;
5867 CASE_FLT_FN (BUILT_IN_ISINF):
5868 CASE_FLT_FN (BUILT_IN_FINITE):
5869 case BUILT_IN_ISFINITE:
5870 case BUILT_IN_ISNORMAL:
5871 target = expand_builtin_interclass_mathfn (exp, target);
5872 if (target)
5873 return target;
5874 break;
5875
5876 CASE_FLT_FN (BUILT_IN_ICEIL):
5877 CASE_FLT_FN (BUILT_IN_LCEIL):
5878 CASE_FLT_FN (BUILT_IN_LLCEIL):
5879 CASE_FLT_FN (BUILT_IN_LFLOOR):
5880 CASE_FLT_FN (BUILT_IN_IFLOOR):
5881 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5882 target = expand_builtin_int_roundingfn (exp, target);
5883 if (target)
5884 return target;
5885 break;
5886
5887 CASE_FLT_FN (BUILT_IN_IRINT):
5888 CASE_FLT_FN (BUILT_IN_LRINT):
5889 CASE_FLT_FN (BUILT_IN_LLRINT):
5890 CASE_FLT_FN (BUILT_IN_IROUND):
5891 CASE_FLT_FN (BUILT_IN_LROUND):
5892 CASE_FLT_FN (BUILT_IN_LLROUND):
5893 target = expand_builtin_int_roundingfn_2 (exp, target);
5894 if (target)
5895 return target;
5896 break;
5897
5898 CASE_FLT_FN (BUILT_IN_POWI):
5899 target = expand_builtin_powi (exp, target);
5900 if (target)
5901 return target;
5902 break;
5903
5904 CASE_FLT_FN (BUILT_IN_ATAN2):
5905 CASE_FLT_FN (BUILT_IN_LDEXP):
5906 CASE_FLT_FN (BUILT_IN_SCALB):
5907 CASE_FLT_FN (BUILT_IN_SCALBN):
5908 CASE_FLT_FN (BUILT_IN_SCALBLN):
5909 if (! flag_unsafe_math_optimizations)
5910 break;
5911
5912 CASE_FLT_FN (BUILT_IN_FMOD):
5913 CASE_FLT_FN (BUILT_IN_REMAINDER):
5914 CASE_FLT_FN (BUILT_IN_DREM):
5915 CASE_FLT_FN (BUILT_IN_POW):
5916 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5917 if (target)
5918 return target;
5919 break;
5920
5921 CASE_FLT_FN (BUILT_IN_CEXPI):
5922 target = expand_builtin_cexpi (exp, target);
5923 gcc_assert (target);
5924 return target;
5925
5926 CASE_FLT_FN (BUILT_IN_SIN):
5927 CASE_FLT_FN (BUILT_IN_COS):
5928 if (! flag_unsafe_math_optimizations)
5929 break;
5930 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5931 if (target)
5932 return target;
5933 break;
5934
5935 CASE_FLT_FN (BUILT_IN_SINCOS):
5936 if (! flag_unsafe_math_optimizations)
5937 break;
5938 target = expand_builtin_sincos (exp);
5939 if (target)
5940 return target;
5941 break;
5942
5943 case BUILT_IN_APPLY_ARGS:
5944 return expand_builtin_apply_args ();
5945
5946 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5947 FUNCTION with a copy of the parameters described by
5948 ARGUMENTS, and ARGSIZE. It returns a block of memory
5949 allocated on the stack into which is stored all the registers
5950 that might possibly be used for returning the result of a
5951 function. ARGUMENTS is the value returned by
5952 __builtin_apply_args. ARGSIZE is the number of bytes of
5953 arguments that must be copied. ??? How should this value be
5954 computed? We'll also need a safe worst case value for varargs
5955 functions. */
5956 case BUILT_IN_APPLY:
5957 if (!validate_arglist (exp, POINTER_TYPE,
5958 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5959 && !validate_arglist (exp, REFERENCE_TYPE,
5960 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5961 return const0_rtx;
5962 else
5963 {
5964 rtx ops[3];
5965
5966 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5967 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5968 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5969
5970 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5971 }
5972
5973 /* __builtin_return (RESULT) causes the function to return the
5974 value described by RESULT. RESULT is address of the block of
5975 memory returned by __builtin_apply. */
5976 case BUILT_IN_RETURN:
5977 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5978 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5979 return const0_rtx;
5980
5981 case BUILT_IN_SAVEREGS:
5982 return expand_builtin_saveregs ();
5983
5984 case BUILT_IN_VA_ARG_PACK:
5985 /* All valid uses of __builtin_va_arg_pack () are removed during
5986 inlining. */
5987 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5988 return const0_rtx;
5989
5990 case BUILT_IN_VA_ARG_PACK_LEN:
5991 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5992 inlining. */
5993 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5994 return const0_rtx;
5995
5996 /* Return the address of the first anonymous stack arg. */
5997 case BUILT_IN_NEXT_ARG:
5998 if (fold_builtin_next_arg (exp, false))
5999 return const0_rtx;
6000 return expand_builtin_next_arg ();
6001
6002 case BUILT_IN_CLEAR_CACHE:
6003 target = expand_builtin___clear_cache (exp);
6004 if (target)
6005 return target;
6006 break;
6007
6008 case BUILT_IN_CLASSIFY_TYPE:
6009 return expand_builtin_classify_type (exp);
6010
6011 case BUILT_IN_CONSTANT_P:
6012 return const0_rtx;
6013
6014 case BUILT_IN_FRAME_ADDRESS:
6015 case BUILT_IN_RETURN_ADDRESS:
6016 return expand_builtin_frame_address (fndecl, exp);
6017
6018 /* Returns the address of the area where the structure is returned.
6019 0 otherwise. */
6020 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6021 if (call_expr_nargs (exp) != 0
6022 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6023 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6024 return const0_rtx;
6025 else
6026 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6027
6028 case BUILT_IN_ALLOCA:
6029 case BUILT_IN_ALLOCA_WITH_ALIGN:
6030 /* If the allocation stems from the declaration of a variable-sized
6031 object, it cannot accumulate. */
6032 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6033 if (target)
6034 return target;
6035 break;
6036
6037 case BUILT_IN_STACK_SAVE:
6038 return expand_stack_save ();
6039
6040 case BUILT_IN_STACK_RESTORE:
6041 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6042 return const0_rtx;
6043
6044 case BUILT_IN_BSWAP16:
6045 case BUILT_IN_BSWAP32:
6046 case BUILT_IN_BSWAP64:
6047 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6048 if (target)
6049 return target;
6050 break;
6051
6052 CASE_INT_FN (BUILT_IN_FFS):
6053 target = expand_builtin_unop (target_mode, exp, target,
6054 subtarget, ffs_optab);
6055 if (target)
6056 return target;
6057 break;
6058
6059 CASE_INT_FN (BUILT_IN_CLZ):
6060 target = expand_builtin_unop (target_mode, exp, target,
6061 subtarget, clz_optab);
6062 if (target)
6063 return target;
6064 break;
6065
6066 CASE_INT_FN (BUILT_IN_CTZ):
6067 target = expand_builtin_unop (target_mode, exp, target,
6068 subtarget, ctz_optab);
6069 if (target)
6070 return target;
6071 break;
6072
6073 CASE_INT_FN (BUILT_IN_CLRSB):
6074 target = expand_builtin_unop (target_mode, exp, target,
6075 subtarget, clrsb_optab);
6076 if (target)
6077 return target;
6078 break;
6079
6080 CASE_INT_FN (BUILT_IN_POPCOUNT):
6081 target = expand_builtin_unop (target_mode, exp, target,
6082 subtarget, popcount_optab);
6083 if (target)
6084 return target;
6085 break;
6086
6087 CASE_INT_FN (BUILT_IN_PARITY):
6088 target = expand_builtin_unop (target_mode, exp, target,
6089 subtarget, parity_optab);
6090 if (target)
6091 return target;
6092 break;
6093
6094 case BUILT_IN_STRLEN:
6095 target = expand_builtin_strlen (exp, target, target_mode);
6096 if (target)
6097 return target;
6098 break;
6099
6100 case BUILT_IN_STRCPY:
6101 target = expand_builtin_strcpy (exp, target);
6102 if (target)
6103 return target;
6104 break;
6105
6106 case BUILT_IN_STRNCPY:
6107 target = expand_builtin_strncpy (exp, target);
6108 if (target)
6109 return target;
6110 break;
6111
6112 case BUILT_IN_STPCPY:
6113 target = expand_builtin_stpcpy (exp, target, mode);
6114 if (target)
6115 return target;
6116 break;
6117
6118 case BUILT_IN_MEMCPY:
6119 target = expand_builtin_memcpy (exp, target);
6120 if (target)
6121 return target;
6122 break;
6123
6124 case BUILT_IN_MEMPCPY:
6125 target = expand_builtin_mempcpy (exp, target, mode);
6126 if (target)
6127 return target;
6128 break;
6129
6130 case BUILT_IN_MEMSET:
6131 target = expand_builtin_memset (exp, target, mode);
6132 if (target)
6133 return target;
6134 break;
6135
6136 case BUILT_IN_BZERO:
6137 target = expand_builtin_bzero (exp);
6138 if (target)
6139 return target;
6140 break;
6141
6142 case BUILT_IN_STRCMP:
6143 target = expand_builtin_strcmp (exp, target);
6144 if (target)
6145 return target;
6146 break;
6147
6148 case BUILT_IN_STRNCMP:
6149 target = expand_builtin_strncmp (exp, target, mode);
6150 if (target)
6151 return target;
6152 break;
6153
6154 case BUILT_IN_BCMP:
6155 case BUILT_IN_MEMCMP:
6156 target = expand_builtin_memcmp (exp, target, mode);
6157 if (target)
6158 return target;
6159 break;
6160
6161 case BUILT_IN_SETJMP:
6162 /* This should have been lowered to the builtins below. */
6163 gcc_unreachable ();
6164
6165 case BUILT_IN_SETJMP_SETUP:
6166 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6167 and the receiver label. */
6168 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6169 {
6170 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6171 VOIDmode, EXPAND_NORMAL);
6172 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6173 rtx label_r = label_rtx (label);
6174
6175 /* This is copied from the handling of non-local gotos. */
6176 expand_builtin_setjmp_setup (buf_addr, label_r);
6177 nonlocal_goto_handler_labels
6178 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6179 nonlocal_goto_handler_labels);
6180 /* ??? Do not let expand_label treat us as such since we would
6181 not want to be both on the list of non-local labels and on
6182 the list of forced labels. */
6183 FORCED_LABEL (label) = 0;
6184 return const0_rtx;
6185 }
6186 break;
6187
6188 case BUILT_IN_SETJMP_RECEIVER:
6189 /* __builtin_setjmp_receiver is passed the receiver label. */
6190 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6191 {
6192 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6193 rtx label_r = label_rtx (label);
6194
6195 expand_builtin_setjmp_receiver (label_r);
6196 return const0_rtx;
6197 }
6198 break;
6199
6200 /* __builtin_longjmp is passed a pointer to an array of five words.
6201 It's similar to the C library longjmp function but works with
6202 __builtin_setjmp above. */
6203 case BUILT_IN_LONGJMP:
6204 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6205 {
6206 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6207 VOIDmode, EXPAND_NORMAL);
6208 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6209
6210 if (value != const1_rtx)
6211 {
6212 error ("%<__builtin_longjmp%> second argument must be 1");
6213 return const0_rtx;
6214 }
6215
6216 expand_builtin_longjmp (buf_addr, value);
6217 return const0_rtx;
6218 }
6219 break;
6220
6221 case BUILT_IN_NONLOCAL_GOTO:
6222 target = expand_builtin_nonlocal_goto (exp);
6223 if (target)
6224 return target;
6225 break;
6226
6227 /* This updates the setjmp buffer that is its argument with the value
6228 of the current stack pointer. */
6229 case BUILT_IN_UPDATE_SETJMP_BUF:
6230 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6231 {
6232 rtx buf_addr
6233 = expand_normal (CALL_EXPR_ARG (exp, 0));
6234
6235 expand_builtin_update_setjmp_buf (buf_addr);
6236 return const0_rtx;
6237 }
6238 break;
6239
6240 case BUILT_IN_TRAP:
6241 expand_builtin_trap ();
6242 return const0_rtx;
6243
6244 case BUILT_IN_UNREACHABLE:
6245 expand_builtin_unreachable ();
6246 return const0_rtx;
6247
6248 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6249 case BUILT_IN_SIGNBITD32:
6250 case BUILT_IN_SIGNBITD64:
6251 case BUILT_IN_SIGNBITD128:
6252 target = expand_builtin_signbit (exp, target);
6253 if (target)
6254 return target;
6255 break;
6256
6257 /* Various hooks for the DWARF 2 __throw routine. */
6258 case BUILT_IN_UNWIND_INIT:
6259 expand_builtin_unwind_init ();
6260 return const0_rtx;
6261 case BUILT_IN_DWARF_CFA:
6262 return virtual_cfa_rtx;
6263 #ifdef DWARF2_UNWIND_INFO
6264 case BUILT_IN_DWARF_SP_COLUMN:
6265 return expand_builtin_dwarf_sp_column ();
6266 case BUILT_IN_INIT_DWARF_REG_SIZES:
6267 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6268 return const0_rtx;
6269 #endif
6270 case BUILT_IN_FROB_RETURN_ADDR:
6271 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6272 case BUILT_IN_EXTRACT_RETURN_ADDR:
6273 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6274 case BUILT_IN_EH_RETURN:
6275 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6276 CALL_EXPR_ARG (exp, 1));
6277 return const0_rtx;
6278 #ifdef EH_RETURN_DATA_REGNO
6279 case BUILT_IN_EH_RETURN_DATA_REGNO:
6280 return expand_builtin_eh_return_data_regno (exp);
6281 #endif
6282 case BUILT_IN_EXTEND_POINTER:
6283 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6284 case BUILT_IN_EH_POINTER:
6285 return expand_builtin_eh_pointer (exp);
6286 case BUILT_IN_EH_FILTER:
6287 return expand_builtin_eh_filter (exp);
6288 case BUILT_IN_EH_COPY_VALUES:
6289 return expand_builtin_eh_copy_values (exp);
6290
6291 case BUILT_IN_VA_START:
6292 return expand_builtin_va_start (exp);
6293 case BUILT_IN_VA_END:
6294 return expand_builtin_va_end (exp);
6295 case BUILT_IN_VA_COPY:
6296 return expand_builtin_va_copy (exp);
6297 case BUILT_IN_EXPECT:
6298 return expand_builtin_expect (exp, target);
6299 case BUILT_IN_ASSUME_ALIGNED:
6300 return expand_builtin_assume_aligned (exp, target);
6301 case BUILT_IN_PREFETCH:
6302 expand_builtin_prefetch (exp);
6303 return const0_rtx;
6304
6305 case BUILT_IN_INIT_TRAMPOLINE:
6306 return expand_builtin_init_trampoline (exp, true);
6307 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6308 return expand_builtin_init_trampoline (exp, false);
6309 case BUILT_IN_ADJUST_TRAMPOLINE:
6310 return expand_builtin_adjust_trampoline (exp);
6311
6312 case BUILT_IN_FORK:
6313 case BUILT_IN_EXECL:
6314 case BUILT_IN_EXECV:
6315 case BUILT_IN_EXECLP:
6316 case BUILT_IN_EXECLE:
6317 case BUILT_IN_EXECVP:
6318 case BUILT_IN_EXECVE:
6319 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6320 if (target)
6321 return target;
6322 break;
6323
6324 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6325 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6326 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6327 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6328 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6329 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6330 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6331 if (target)
6332 return target;
6333 break;
6334
6335 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6336 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6337 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6338 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6339 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6340 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6341 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6342 if (target)
6343 return target;
6344 break;
6345
6346 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6347 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6348 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6349 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6350 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6351 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6352 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6353 if (target)
6354 return target;
6355 break;
6356
6357 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6358 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6359 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6360 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6361 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6362 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6363 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6364 if (target)
6365 return target;
6366 break;
6367
6368 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6369 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6370 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6371 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6372 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6373 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6374 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6375 if (target)
6376 return target;
6377 break;
6378
6379 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6380 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6381 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6382 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6383 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6384 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6385 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6386 if (target)
6387 return target;
6388 break;
6389
6390 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6391 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6392 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6393 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6394 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6395 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6396 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6397 if (target)
6398 return target;
6399 break;
6400
6401 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6402 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6403 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6404 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6405 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6406 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6407 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6408 if (target)
6409 return target;
6410 break;
6411
6412 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6413 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6414 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6415 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6416 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6417 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6418 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6419 if (target)
6420 return target;
6421 break;
6422
6423 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6424 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6425 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6426 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6427 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6430 if (target)
6431 return target;
6432 break;
6433
6434 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6435 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6436 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6437 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6438 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6439 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6440 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6441 if (target)
6442 return target;
6443 break;
6444
6445 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6446 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6447 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6448 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6449 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6450 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6451 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6452 if (target)
6453 return target;
6454 break;
6455
6456 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6458 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6459 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6460 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6461 if (mode == VOIDmode)
6462 mode = TYPE_MODE (boolean_type_node);
6463 if (!target || !register_operand (target, mode))
6464 target = gen_reg_rtx (mode);
6465
6466 mode = get_builtin_sync_mode
6467 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6468 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6469 if (target)
6470 return target;
6471 break;
6472
6473 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6474 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6475 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6476 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6477 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6478 mode = get_builtin_sync_mode
6479 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6480 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6481 if (target)
6482 return target;
6483 break;
6484
6485 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6486 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6487 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6488 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6489 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6490 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6491 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6492 if (target)
6493 return target;
6494 break;
6495
6496 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6497 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6498 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6499 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6500 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6501 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6502 expand_builtin_sync_lock_release (mode, exp);
6503 return const0_rtx;
6504
6505 case BUILT_IN_SYNC_SYNCHRONIZE:
6506 expand_builtin_sync_synchronize ();
6507 return const0_rtx;
6508
6509 case BUILT_IN_ATOMIC_EXCHANGE_1:
6510 case BUILT_IN_ATOMIC_EXCHANGE_2:
6511 case BUILT_IN_ATOMIC_EXCHANGE_4:
6512 case BUILT_IN_ATOMIC_EXCHANGE_8:
6513 case BUILT_IN_ATOMIC_EXCHANGE_16:
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6515 target = expand_builtin_atomic_exchange (mode, exp, target);
6516 if (target)
6517 return target;
6518 break;
6519
6520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6522 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6523 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6524 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6525 {
6526 unsigned int nargs, z;
6527 vec<tree, va_gc> *vec;
6528
6529 mode =
6530 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6531 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6532 if (target)
6533 return target;
6534
6535 /* If this is turned into an external library call, the weak parameter
6536 must be dropped to match the expected parameter list. */
6537 nargs = call_expr_nargs (exp);
6538 vec_alloc (vec, nargs - 1);
6539 for (z = 0; z < 3; z++)
6540 vec->quick_push (CALL_EXPR_ARG (exp, z));
6541 /* Skip the boolean weak parameter. */
6542 for (z = 4; z < 6; z++)
6543 vec->quick_push (CALL_EXPR_ARG (exp, z));
6544 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6545 break;
6546 }
6547
6548 case BUILT_IN_ATOMIC_LOAD_1:
6549 case BUILT_IN_ATOMIC_LOAD_2:
6550 case BUILT_IN_ATOMIC_LOAD_4:
6551 case BUILT_IN_ATOMIC_LOAD_8:
6552 case BUILT_IN_ATOMIC_LOAD_16:
6553 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6554 target = expand_builtin_atomic_load (mode, exp, target);
6555 if (target)
6556 return target;
6557 break;
6558
6559 case BUILT_IN_ATOMIC_STORE_1:
6560 case BUILT_IN_ATOMIC_STORE_2:
6561 case BUILT_IN_ATOMIC_STORE_4:
6562 case BUILT_IN_ATOMIC_STORE_8:
6563 case BUILT_IN_ATOMIC_STORE_16:
6564 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6565 target = expand_builtin_atomic_store (mode, exp);
6566 if (target)
6567 return const0_rtx;
6568 break;
6569
6570 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6571 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6572 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6573 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6574 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6575 {
6576 enum built_in_function lib;
6577 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6578 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6579 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6580 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6581 ignore, lib);
6582 if (target)
6583 return target;
6584 break;
6585 }
6586 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6587 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6588 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6589 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6590 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6591 {
6592 enum built_in_function lib;
6593 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6594 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6595 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6596 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6597 ignore, lib);
6598 if (target)
6599 return target;
6600 break;
6601 }
6602 case BUILT_IN_ATOMIC_AND_FETCH_1:
6603 case BUILT_IN_ATOMIC_AND_FETCH_2:
6604 case BUILT_IN_ATOMIC_AND_FETCH_4:
6605 case BUILT_IN_ATOMIC_AND_FETCH_8:
6606 case BUILT_IN_ATOMIC_AND_FETCH_16:
6607 {
6608 enum built_in_function lib;
6609 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6610 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6611 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6612 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6613 ignore, lib);
6614 if (target)
6615 return target;
6616 break;
6617 }
6618 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6619 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6620 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6621 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6622 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6623 {
6624 enum built_in_function lib;
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6626 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6627 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6628 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6629 ignore, lib);
6630 if (target)
6631 return target;
6632 break;
6633 }
6634 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6635 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6636 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6637 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6638 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6639 {
6640 enum built_in_function lib;
6641 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6642 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6643 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6644 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6645 ignore, lib);
6646 if (target)
6647 return target;
6648 break;
6649 }
6650 case BUILT_IN_ATOMIC_OR_FETCH_1:
6651 case BUILT_IN_ATOMIC_OR_FETCH_2:
6652 case BUILT_IN_ATOMIC_OR_FETCH_4:
6653 case BUILT_IN_ATOMIC_OR_FETCH_8:
6654 case BUILT_IN_ATOMIC_OR_FETCH_16:
6655 {
6656 enum built_in_function lib;
6657 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6658 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6659 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6660 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6661 ignore, lib);
6662 if (target)
6663 return target;
6664 break;
6665 }
6666 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6667 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6668 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6669 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6670 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6671 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6672 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6673 ignore, BUILT_IN_NONE);
6674 if (target)
6675 return target;
6676 break;
6677
6678 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6679 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6680 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6681 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6682 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6683 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6684 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6685 ignore, BUILT_IN_NONE);
6686 if (target)
6687 return target;
6688 break;
6689
6690 case BUILT_IN_ATOMIC_FETCH_AND_1:
6691 case BUILT_IN_ATOMIC_FETCH_AND_2:
6692 case BUILT_IN_ATOMIC_FETCH_AND_4:
6693 case BUILT_IN_ATOMIC_FETCH_AND_8:
6694 case BUILT_IN_ATOMIC_FETCH_AND_16:
6695 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6696 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6697 ignore, BUILT_IN_NONE);
6698 if (target)
6699 return target;
6700 break;
6701
6702 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6703 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6704 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6705 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6706 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6707 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6708 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6709 ignore, BUILT_IN_NONE);
6710 if (target)
6711 return target;
6712 break;
6713
6714 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6715 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6716 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6717 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6718 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6719 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6720 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6721 ignore, BUILT_IN_NONE);
6722 if (target)
6723 return target;
6724 break;
6725
6726 case BUILT_IN_ATOMIC_FETCH_OR_1:
6727 case BUILT_IN_ATOMIC_FETCH_OR_2:
6728 case BUILT_IN_ATOMIC_FETCH_OR_4:
6729 case BUILT_IN_ATOMIC_FETCH_OR_8:
6730 case BUILT_IN_ATOMIC_FETCH_OR_16:
6731 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6732 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6733 ignore, BUILT_IN_NONE);
6734 if (target)
6735 return target;
6736 break;
6737
6738 case BUILT_IN_ATOMIC_TEST_AND_SET:
6739 return expand_builtin_atomic_test_and_set (exp, target);
6740
6741 case BUILT_IN_ATOMIC_CLEAR:
6742 return expand_builtin_atomic_clear (exp);
6743
6744 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6745 return expand_builtin_atomic_always_lock_free (exp);
6746
6747 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6748 target = expand_builtin_atomic_is_lock_free (exp);
6749 if (target)
6750 return target;
6751 break;
6752
6753 case BUILT_IN_ATOMIC_THREAD_FENCE:
6754 expand_builtin_atomic_thread_fence (exp);
6755 return const0_rtx;
6756
6757 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6758 expand_builtin_atomic_signal_fence (exp);
6759 return const0_rtx;
6760
6761 case BUILT_IN_OBJECT_SIZE:
6762 return expand_builtin_object_size (exp);
6763
6764 case BUILT_IN_MEMCPY_CHK:
6765 case BUILT_IN_MEMPCPY_CHK:
6766 case BUILT_IN_MEMMOVE_CHK:
6767 case BUILT_IN_MEMSET_CHK:
6768 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6769 if (target)
6770 return target;
6771 break;
6772
6773 case BUILT_IN_STRCPY_CHK:
6774 case BUILT_IN_STPCPY_CHK:
6775 case BUILT_IN_STRNCPY_CHK:
6776 case BUILT_IN_STPNCPY_CHK:
6777 case BUILT_IN_STRCAT_CHK:
6778 case BUILT_IN_STRNCAT_CHK:
6779 case BUILT_IN_SNPRINTF_CHK:
6780 case BUILT_IN_VSNPRINTF_CHK:
6781 maybe_emit_chk_warning (exp, fcode);
6782 break;
6783
6784 case BUILT_IN_SPRINTF_CHK:
6785 case BUILT_IN_VSPRINTF_CHK:
6786 maybe_emit_sprintf_chk_warning (exp, fcode);
6787 break;
6788
6789 case BUILT_IN_FREE:
6790 if (warn_free_nonheap_object)
6791 maybe_emit_free_warning (exp);
6792 break;
6793
6794 case BUILT_IN_THREAD_POINTER:
6795 return expand_builtin_thread_pointer (exp, target);
6796
6797 case BUILT_IN_SET_THREAD_POINTER:
6798 expand_builtin_set_thread_pointer (exp);
6799 return const0_rtx;
6800
6801 case BUILT_IN_CILK_DETACH:
6802 expand_builtin_cilk_detach (exp);
6803 return const0_rtx;
6804
6805 case BUILT_IN_CILK_POP_FRAME:
6806 expand_builtin_cilk_pop_frame (exp);
6807 return const0_rtx;
6808
6809 default: /* just do library call, if unknown builtin */
6810 break;
6811 }
6812
6813 /* The switch statement above can drop through to cause the function
6814 to be called normally. */
6815 return expand_call (exp, target, ignore);
6816 }
6817
6818 /* Determine whether a tree node represents a call to a built-in
6819 function. If the tree T is a call to a built-in function with
6820 the right number of arguments of the appropriate types, return
6821 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6822 Otherwise the return value is END_BUILTINS. */
6823
6824 enum built_in_function
6825 builtin_mathfn_code (const_tree t)
6826 {
6827 const_tree fndecl, arg, parmlist;
6828 const_tree argtype, parmtype;
6829 const_call_expr_arg_iterator iter;
6830
6831 if (TREE_CODE (t) != CALL_EXPR
6832 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6833 return END_BUILTINS;
6834
6835 fndecl = get_callee_fndecl (t);
6836 if (fndecl == NULL_TREE
6837 || TREE_CODE (fndecl) != FUNCTION_DECL
6838 || ! DECL_BUILT_IN (fndecl)
6839 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6840 return END_BUILTINS;
6841
6842 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6843 init_const_call_expr_arg_iterator (t, &iter);
6844 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6845 {
6846 /* If a function doesn't take a variable number of arguments,
6847 the last element in the list will have type `void'. */
6848 parmtype = TREE_VALUE (parmlist);
6849 if (VOID_TYPE_P (parmtype))
6850 {
6851 if (more_const_call_expr_args_p (&iter))
6852 return END_BUILTINS;
6853 return DECL_FUNCTION_CODE (fndecl);
6854 }
6855
6856 if (! more_const_call_expr_args_p (&iter))
6857 return END_BUILTINS;
6858
6859 arg = next_const_call_expr_arg (&iter);
6860 argtype = TREE_TYPE (arg);
6861
6862 if (SCALAR_FLOAT_TYPE_P (parmtype))
6863 {
6864 if (! SCALAR_FLOAT_TYPE_P (argtype))
6865 return END_BUILTINS;
6866 }
6867 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6868 {
6869 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6870 return END_BUILTINS;
6871 }
6872 else if (POINTER_TYPE_P (parmtype))
6873 {
6874 if (! POINTER_TYPE_P (argtype))
6875 return END_BUILTINS;
6876 }
6877 else if (INTEGRAL_TYPE_P (parmtype))
6878 {
6879 if (! INTEGRAL_TYPE_P (argtype))
6880 return END_BUILTINS;
6881 }
6882 else
6883 return END_BUILTINS;
6884 }
6885
6886 /* Variable-length argument list. */
6887 return DECL_FUNCTION_CODE (fndecl);
6888 }
6889
6890 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6891 evaluate to a constant. */
6892
6893 static tree
6894 fold_builtin_constant_p (tree arg)
6895 {
6896 /* We return 1 for a numeric type that's known to be a constant
6897 value at compile-time or for an aggregate type that's a
6898 literal constant. */
6899 STRIP_NOPS (arg);
6900
6901 /* If we know this is a constant, emit the constant of one. */
6902 if (CONSTANT_CLASS_P (arg)
6903 || (TREE_CODE (arg) == CONSTRUCTOR
6904 && TREE_CONSTANT (arg)))
6905 return integer_one_node;
6906 if (TREE_CODE (arg) == ADDR_EXPR)
6907 {
6908 tree op = TREE_OPERAND (arg, 0);
6909 if (TREE_CODE (op) == STRING_CST
6910 || (TREE_CODE (op) == ARRAY_REF
6911 && integer_zerop (TREE_OPERAND (op, 1))
6912 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6913 return integer_one_node;
6914 }
6915
6916 /* If this expression has side effects, show we don't know it to be a
6917 constant. Likewise if it's a pointer or aggregate type since in
6918 those case we only want literals, since those are only optimized
6919 when generating RTL, not later.
6920 And finally, if we are compiling an initializer, not code, we
6921 need to return a definite result now; there's not going to be any
6922 more optimization done. */
6923 if (TREE_SIDE_EFFECTS (arg)
6924 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6925 || POINTER_TYPE_P (TREE_TYPE (arg))
6926 || cfun == 0
6927 || folding_initializer
6928 || force_folding_builtin_constant_p)
6929 return integer_zero_node;
6930
6931 return NULL_TREE;
6932 }
6933
6934 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6935 return it as a truthvalue. */
6936
6937 static tree
6938 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
6939 tree predictor)
6940 {
6941 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6942
6943 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6944 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6945 ret_type = TREE_TYPE (TREE_TYPE (fn));
6946 pred_type = TREE_VALUE (arg_types);
6947 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6948
6949 pred = fold_convert_loc (loc, pred_type, pred);
6950 expected = fold_convert_loc (loc, expected_type, expected);
6951 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
6952 predictor);
6953
6954 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6955 build_int_cst (ret_type, 0));
6956 }
6957
6958 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6959 NULL_TREE if no simplification is possible. */
6960
6961 tree
6962 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
6963 {
6964 tree inner, fndecl, inner_arg0;
6965 enum tree_code code;
6966
6967 /* Distribute the expected value over short-circuiting operators.
6968 See through the cast from truthvalue_type_node to long. */
6969 inner_arg0 = arg0;
6970 while (TREE_CODE (inner_arg0) == NOP_EXPR
6971 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6972 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6973 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6974
6975 /* If this is a builtin_expect within a builtin_expect keep the
6976 inner one. See through a comparison against a constant. It
6977 might have been added to create a thruthvalue. */
6978 inner = inner_arg0;
6979
6980 if (COMPARISON_CLASS_P (inner)
6981 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6982 inner = TREE_OPERAND (inner, 0);
6983
6984 if (TREE_CODE (inner) == CALL_EXPR
6985 && (fndecl = get_callee_fndecl (inner))
6986 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6987 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6988 return arg0;
6989
6990 inner = inner_arg0;
6991 code = TREE_CODE (inner);
6992 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6993 {
6994 tree op0 = TREE_OPERAND (inner, 0);
6995 tree op1 = TREE_OPERAND (inner, 1);
6996
6997 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
6998 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
6999 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7000
7001 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7002 }
7003
7004 /* If the argument isn't invariant then there's nothing else we can do. */
7005 if (!TREE_CONSTANT (inner_arg0))
7006 return NULL_TREE;
7007
7008 /* If we expect that a comparison against the argument will fold to
7009 a constant return the constant. In practice, this means a true
7010 constant or the address of a non-weak symbol. */
7011 inner = inner_arg0;
7012 STRIP_NOPS (inner);
7013 if (TREE_CODE (inner) == ADDR_EXPR)
7014 {
7015 do
7016 {
7017 inner = TREE_OPERAND (inner, 0);
7018 }
7019 while (TREE_CODE (inner) == COMPONENT_REF
7020 || TREE_CODE (inner) == ARRAY_REF);
7021 if ((TREE_CODE (inner) == VAR_DECL
7022 || TREE_CODE (inner) == FUNCTION_DECL)
7023 && DECL_WEAK (inner))
7024 return NULL_TREE;
7025 }
7026
7027 /* Otherwise, ARG0 already has the proper type for the return value. */
7028 return arg0;
7029 }
7030
7031 /* Fold a call to __builtin_classify_type with argument ARG. */
7032
7033 static tree
7034 fold_builtin_classify_type (tree arg)
7035 {
7036 if (arg == 0)
7037 return build_int_cst (integer_type_node, no_type_class);
7038
7039 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7040 }
7041
7042 /* Fold a call to __builtin_strlen with argument ARG. */
7043
7044 static tree
7045 fold_builtin_strlen (location_t loc, tree type, tree arg)
7046 {
7047 if (!validate_arg (arg, POINTER_TYPE))
7048 return NULL_TREE;
7049 else
7050 {
7051 tree len = c_strlen (arg, 0);
7052
7053 if (len)
7054 return fold_convert_loc (loc, type, len);
7055
7056 return NULL_TREE;
7057 }
7058 }
7059
7060 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7061
7062 static tree
7063 fold_builtin_inf (location_t loc, tree type, int warn)
7064 {
7065 REAL_VALUE_TYPE real;
7066
7067 /* __builtin_inff is intended to be usable to define INFINITY on all
7068 targets. If an infinity is not available, INFINITY expands "to a
7069 positive constant of type float that overflows at translation
7070 time", footnote "In this case, using INFINITY will violate the
7071 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7072 Thus we pedwarn to ensure this constraint violation is
7073 diagnosed. */
7074 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7075 pedwarn (loc, 0, "target format does not support infinity");
7076
7077 real_inf (&real);
7078 return build_real (type, real);
7079 }
7080
7081 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7082
7083 static tree
7084 fold_builtin_nan (tree arg, tree type, int quiet)
7085 {
7086 REAL_VALUE_TYPE real;
7087 const char *str;
7088
7089 if (!validate_arg (arg, POINTER_TYPE))
7090 return NULL_TREE;
7091 str = c_getstr (arg);
7092 if (!str)
7093 return NULL_TREE;
7094
7095 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7096 return NULL_TREE;
7097
7098 return build_real (type, real);
7099 }
7100
7101 /* Return true if the floating point expression T has an integer value.
7102 We also allow +Inf, -Inf and NaN to be considered integer values. */
7103
7104 static bool
7105 integer_valued_real_p (tree t)
7106 {
7107 switch (TREE_CODE (t))
7108 {
7109 case FLOAT_EXPR:
7110 return true;
7111
7112 case ABS_EXPR:
7113 case SAVE_EXPR:
7114 return integer_valued_real_p (TREE_OPERAND (t, 0));
7115
7116 case COMPOUND_EXPR:
7117 case MODIFY_EXPR:
7118 case BIND_EXPR:
7119 return integer_valued_real_p (TREE_OPERAND (t, 1));
7120
7121 case PLUS_EXPR:
7122 case MINUS_EXPR:
7123 case MULT_EXPR:
7124 case MIN_EXPR:
7125 case MAX_EXPR:
7126 return integer_valued_real_p (TREE_OPERAND (t, 0))
7127 && integer_valued_real_p (TREE_OPERAND (t, 1));
7128
7129 case COND_EXPR:
7130 return integer_valued_real_p (TREE_OPERAND (t, 1))
7131 && integer_valued_real_p (TREE_OPERAND (t, 2));
7132
7133 case REAL_CST:
7134 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7135
7136 case NOP_EXPR:
7137 {
7138 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7139 if (TREE_CODE (type) == INTEGER_TYPE)
7140 return true;
7141 if (TREE_CODE (type) == REAL_TYPE)
7142 return integer_valued_real_p (TREE_OPERAND (t, 0));
7143 break;
7144 }
7145
7146 case CALL_EXPR:
7147 switch (builtin_mathfn_code (t))
7148 {
7149 CASE_FLT_FN (BUILT_IN_CEIL):
7150 CASE_FLT_FN (BUILT_IN_FLOOR):
7151 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7152 CASE_FLT_FN (BUILT_IN_RINT):
7153 CASE_FLT_FN (BUILT_IN_ROUND):
7154 CASE_FLT_FN (BUILT_IN_TRUNC):
7155 return true;
7156
7157 CASE_FLT_FN (BUILT_IN_FMIN):
7158 CASE_FLT_FN (BUILT_IN_FMAX):
7159 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7160 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7161
7162 default:
7163 break;
7164 }
7165 break;
7166
7167 default:
7168 break;
7169 }
7170 return false;
7171 }
7172
7173 /* FNDECL is assumed to be a builtin where truncation can be propagated
7174 across (for instance floor((double)f) == (double)floorf (f).
7175 Do the transformation for a call with argument ARG. */
7176
7177 static tree
7178 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7179 {
7180 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7181
7182 if (!validate_arg (arg, REAL_TYPE))
7183 return NULL_TREE;
7184
7185 /* Integer rounding functions are idempotent. */
7186 if (fcode == builtin_mathfn_code (arg))
7187 return arg;
7188
7189 /* If argument is already integer valued, and we don't need to worry
7190 about setting errno, there's no need to perform rounding. */
7191 if (! flag_errno_math && integer_valued_real_p (arg))
7192 return arg;
7193
7194 if (optimize)
7195 {
7196 tree arg0 = strip_float_extensions (arg);
7197 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7198 tree newtype = TREE_TYPE (arg0);
7199 tree decl;
7200
7201 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7202 && (decl = mathfn_built_in (newtype, fcode)))
7203 return fold_convert_loc (loc, ftype,
7204 build_call_expr_loc (loc, decl, 1,
7205 fold_convert_loc (loc,
7206 newtype,
7207 arg0)));
7208 }
7209 return NULL_TREE;
7210 }
7211
7212 /* FNDECL is assumed to be builtin which can narrow the FP type of
7213 the argument, for instance lround((double)f) -> lroundf (f).
7214 Do the transformation for a call with argument ARG. */
7215
7216 static tree
7217 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7218 {
7219 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7220
7221 if (!validate_arg (arg, REAL_TYPE))
7222 return NULL_TREE;
7223
7224 /* If argument is already integer valued, and we don't need to worry
7225 about setting errno, there's no need to perform rounding. */
7226 if (! flag_errno_math && integer_valued_real_p (arg))
7227 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7228 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7229
7230 if (optimize)
7231 {
7232 tree ftype = TREE_TYPE (arg);
7233 tree arg0 = strip_float_extensions (arg);
7234 tree newtype = TREE_TYPE (arg0);
7235 tree decl;
7236
7237 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7238 && (decl = mathfn_built_in (newtype, fcode)))
7239 return build_call_expr_loc (loc, decl, 1,
7240 fold_convert_loc (loc, newtype, arg0));
7241 }
7242
7243 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7244 sizeof (int) == sizeof (long). */
7245 if (TYPE_PRECISION (integer_type_node)
7246 == TYPE_PRECISION (long_integer_type_node))
7247 {
7248 tree newfn = NULL_TREE;
7249 switch (fcode)
7250 {
7251 CASE_FLT_FN (BUILT_IN_ICEIL):
7252 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7253 break;
7254
7255 CASE_FLT_FN (BUILT_IN_IFLOOR):
7256 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7257 break;
7258
7259 CASE_FLT_FN (BUILT_IN_IROUND):
7260 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7261 break;
7262
7263 CASE_FLT_FN (BUILT_IN_IRINT):
7264 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7265 break;
7266
7267 default:
7268 break;
7269 }
7270
7271 if (newfn)
7272 {
7273 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7274 return fold_convert_loc (loc,
7275 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7276 }
7277 }
7278
7279 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7280 sizeof (long long) == sizeof (long). */
7281 if (TYPE_PRECISION (long_long_integer_type_node)
7282 == TYPE_PRECISION (long_integer_type_node))
7283 {
7284 tree newfn = NULL_TREE;
7285 switch (fcode)
7286 {
7287 CASE_FLT_FN (BUILT_IN_LLCEIL):
7288 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7289 break;
7290
7291 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7292 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7293 break;
7294
7295 CASE_FLT_FN (BUILT_IN_LLROUND):
7296 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7297 break;
7298
7299 CASE_FLT_FN (BUILT_IN_LLRINT):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7301 break;
7302
7303 default:
7304 break;
7305 }
7306
7307 if (newfn)
7308 {
7309 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7310 return fold_convert_loc (loc,
7311 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7312 }
7313 }
7314
7315 return NULL_TREE;
7316 }
7317
7318 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7319 return type. Return NULL_TREE if no simplification can be made. */
7320
7321 static tree
7322 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7323 {
7324 tree res;
7325
7326 if (!validate_arg (arg, COMPLEX_TYPE)
7327 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7328 return NULL_TREE;
7329
7330 /* Calculate the result when the argument is a constant. */
7331 if (TREE_CODE (arg) == COMPLEX_CST
7332 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7333 type, mpfr_hypot)))
7334 return res;
7335
7336 if (TREE_CODE (arg) == COMPLEX_EXPR)
7337 {
7338 tree real = TREE_OPERAND (arg, 0);
7339 tree imag = TREE_OPERAND (arg, 1);
7340
7341 /* If either part is zero, cabs is fabs of the other. */
7342 if (real_zerop (real))
7343 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7344 if (real_zerop (imag))
7345 return fold_build1_loc (loc, ABS_EXPR, type, real);
7346
7347 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7348 if (flag_unsafe_math_optimizations
7349 && operand_equal_p (real, imag, OEP_PURE_SAME))
7350 {
7351 const REAL_VALUE_TYPE sqrt2_trunc
7352 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7353 STRIP_NOPS (real);
7354 return fold_build2_loc (loc, MULT_EXPR, type,
7355 fold_build1_loc (loc, ABS_EXPR, type, real),
7356 build_real (type, sqrt2_trunc));
7357 }
7358 }
7359
7360 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7361 if (TREE_CODE (arg) == NEGATE_EXPR
7362 || TREE_CODE (arg) == CONJ_EXPR)
7363 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7364
7365 /* Don't do this when optimizing for size. */
7366 if (flag_unsafe_math_optimizations
7367 && optimize && optimize_function_for_speed_p (cfun))
7368 {
7369 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7370
7371 if (sqrtfn != NULL_TREE)
7372 {
7373 tree rpart, ipart, result;
7374
7375 arg = builtin_save_expr (arg);
7376
7377 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7378 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7379
7380 rpart = builtin_save_expr (rpart);
7381 ipart = builtin_save_expr (ipart);
7382
7383 result = fold_build2_loc (loc, PLUS_EXPR, type,
7384 fold_build2_loc (loc, MULT_EXPR, type,
7385 rpart, rpart),
7386 fold_build2_loc (loc, MULT_EXPR, type,
7387 ipart, ipart));
7388
7389 return build_call_expr_loc (loc, sqrtfn, 1, result);
7390 }
7391 }
7392
7393 return NULL_TREE;
7394 }
7395
7396 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7397 complex tree type of the result. If NEG is true, the imaginary
7398 zero is negative. */
7399
7400 static tree
7401 build_complex_cproj (tree type, bool neg)
7402 {
7403 REAL_VALUE_TYPE rinf, rzero = dconst0;
7404
7405 real_inf (&rinf);
7406 rzero.sign = neg;
7407 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7408 build_real (TREE_TYPE (type), rzero));
7409 }
7410
7411 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7412 return type. Return NULL_TREE if no simplification can be made. */
7413
7414 static tree
7415 fold_builtin_cproj (location_t loc, tree arg, tree type)
7416 {
7417 if (!validate_arg (arg, COMPLEX_TYPE)
7418 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7419 return NULL_TREE;
7420
7421 /* If there are no infinities, return arg. */
7422 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7423 return non_lvalue_loc (loc, arg);
7424
7425 /* Calculate the result when the argument is a constant. */
7426 if (TREE_CODE (arg) == COMPLEX_CST)
7427 {
7428 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7429 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7430
7431 if (real_isinf (real) || real_isinf (imag))
7432 return build_complex_cproj (type, imag->sign);
7433 else
7434 return arg;
7435 }
7436 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7437 {
7438 tree real = TREE_OPERAND (arg, 0);
7439 tree imag = TREE_OPERAND (arg, 1);
7440
7441 STRIP_NOPS (real);
7442 STRIP_NOPS (imag);
7443
7444 /* If the real part is inf and the imag part is known to be
7445 nonnegative, return (inf + 0i). Remember side-effects are
7446 possible in the imag part. */
7447 if (TREE_CODE (real) == REAL_CST
7448 && real_isinf (TREE_REAL_CST_PTR (real))
7449 && tree_expr_nonnegative_p (imag))
7450 return omit_one_operand_loc (loc, type,
7451 build_complex_cproj (type, false),
7452 arg);
7453
7454 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7455 Remember side-effects are possible in the real part. */
7456 if (TREE_CODE (imag) == REAL_CST
7457 && real_isinf (TREE_REAL_CST_PTR (imag)))
7458 return
7459 omit_one_operand_loc (loc, type,
7460 build_complex_cproj (type, TREE_REAL_CST_PTR
7461 (imag)->sign), arg);
7462 }
7463
7464 return NULL_TREE;
7465 }
7466
7467 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7468 Return NULL_TREE if no simplification can be made. */
7469
7470 static tree
7471 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7472 {
7473
7474 enum built_in_function fcode;
7475 tree res;
7476
7477 if (!validate_arg (arg, REAL_TYPE))
7478 return NULL_TREE;
7479
7480 /* Calculate the result when the argument is a constant. */
7481 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7482 return res;
7483
7484 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7485 fcode = builtin_mathfn_code (arg);
7486 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7487 {
7488 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7489 arg = fold_build2_loc (loc, MULT_EXPR, type,
7490 CALL_EXPR_ARG (arg, 0),
7491 build_real (type, dconsthalf));
7492 return build_call_expr_loc (loc, expfn, 1, arg);
7493 }
7494
7495 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7496 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7497 {
7498 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7499
7500 if (powfn)
7501 {
7502 tree arg0 = CALL_EXPR_ARG (arg, 0);
7503 tree tree_root;
7504 /* The inner root was either sqrt or cbrt. */
7505 /* This was a conditional expression but it triggered a bug
7506 in Sun C 5.5. */
7507 REAL_VALUE_TYPE dconstroot;
7508 if (BUILTIN_SQRT_P (fcode))
7509 dconstroot = dconsthalf;
7510 else
7511 dconstroot = dconst_third ();
7512
7513 /* Adjust for the outer root. */
7514 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7515 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7516 tree_root = build_real (type, dconstroot);
7517 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7518 }
7519 }
7520
7521 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7522 if (flag_unsafe_math_optimizations
7523 && (fcode == BUILT_IN_POW
7524 || fcode == BUILT_IN_POWF
7525 || fcode == BUILT_IN_POWL))
7526 {
7527 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7528 tree arg0 = CALL_EXPR_ARG (arg, 0);
7529 tree arg1 = CALL_EXPR_ARG (arg, 1);
7530 tree narg1;
7531 if (!tree_expr_nonnegative_p (arg0))
7532 arg0 = build1 (ABS_EXPR, type, arg0);
7533 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7534 build_real (type, dconsthalf));
7535 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7536 }
7537
7538 return NULL_TREE;
7539 }
7540
7541 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7542 Return NULL_TREE if no simplification can be made. */
7543
7544 static tree
7545 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7546 {
7547 const enum built_in_function fcode = builtin_mathfn_code (arg);
7548 tree res;
7549
7550 if (!validate_arg (arg, REAL_TYPE))
7551 return NULL_TREE;
7552
7553 /* Calculate the result when the argument is a constant. */
7554 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7555 return res;
7556
7557 if (flag_unsafe_math_optimizations)
7558 {
7559 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7560 if (BUILTIN_EXPONENT_P (fcode))
7561 {
7562 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7563 const REAL_VALUE_TYPE third_trunc =
7564 real_value_truncate (TYPE_MODE (type), dconst_third ());
7565 arg = fold_build2_loc (loc, MULT_EXPR, type,
7566 CALL_EXPR_ARG (arg, 0),
7567 build_real (type, third_trunc));
7568 return build_call_expr_loc (loc, expfn, 1, arg);
7569 }
7570
7571 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7572 if (BUILTIN_SQRT_P (fcode))
7573 {
7574 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7575
7576 if (powfn)
7577 {
7578 tree arg0 = CALL_EXPR_ARG (arg, 0);
7579 tree tree_root;
7580 REAL_VALUE_TYPE dconstroot = dconst_third ();
7581
7582 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7583 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7584 tree_root = build_real (type, dconstroot);
7585 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7586 }
7587 }
7588
7589 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7590 if (BUILTIN_CBRT_P (fcode))
7591 {
7592 tree arg0 = CALL_EXPR_ARG (arg, 0);
7593 if (tree_expr_nonnegative_p (arg0))
7594 {
7595 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7596
7597 if (powfn)
7598 {
7599 tree tree_root;
7600 REAL_VALUE_TYPE dconstroot;
7601
7602 real_arithmetic (&dconstroot, MULT_EXPR,
7603 dconst_third_ptr (), dconst_third_ptr ());
7604 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7605 tree_root = build_real (type, dconstroot);
7606 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7607 }
7608 }
7609 }
7610
7611 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7612 if (fcode == BUILT_IN_POW
7613 || fcode == BUILT_IN_POWF
7614 || fcode == BUILT_IN_POWL)
7615 {
7616 tree arg00 = CALL_EXPR_ARG (arg, 0);
7617 tree arg01 = CALL_EXPR_ARG (arg, 1);
7618 if (tree_expr_nonnegative_p (arg00))
7619 {
7620 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7621 const REAL_VALUE_TYPE dconstroot
7622 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7623 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7624 build_real (type, dconstroot));
7625 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7626 }
7627 }
7628 }
7629 return NULL_TREE;
7630 }
7631
7632 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7633 TYPE is the type of the return value. Return NULL_TREE if no
7634 simplification can be made. */
7635
7636 static tree
7637 fold_builtin_cos (location_t loc,
7638 tree arg, tree type, tree fndecl)
7639 {
7640 tree res, narg;
7641
7642 if (!validate_arg (arg, REAL_TYPE))
7643 return NULL_TREE;
7644
7645 /* Calculate the result when the argument is a constant. */
7646 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7647 return res;
7648
7649 /* Optimize cos(-x) into cos (x). */
7650 if ((narg = fold_strip_sign_ops (arg)))
7651 return build_call_expr_loc (loc, fndecl, 1, narg);
7652
7653 return NULL_TREE;
7654 }
7655
7656 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7657 Return NULL_TREE if no simplification can be made. */
7658
7659 static tree
7660 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7661 {
7662 if (validate_arg (arg, REAL_TYPE))
7663 {
7664 tree res, narg;
7665
7666 /* Calculate the result when the argument is a constant. */
7667 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7668 return res;
7669
7670 /* Optimize cosh(-x) into cosh (x). */
7671 if ((narg = fold_strip_sign_ops (arg)))
7672 return build_call_expr_loc (loc, fndecl, 1, narg);
7673 }
7674
7675 return NULL_TREE;
7676 }
7677
7678 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7679 argument ARG. TYPE is the type of the return value. Return
7680 NULL_TREE if no simplification can be made. */
7681
7682 static tree
7683 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7684 bool hyper)
7685 {
7686 if (validate_arg (arg, COMPLEX_TYPE)
7687 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7688 {
7689 tree tmp;
7690
7691 /* Calculate the result when the argument is a constant. */
7692 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7693 return tmp;
7694
7695 /* Optimize fn(-x) into fn(x). */
7696 if ((tmp = fold_strip_sign_ops (arg)))
7697 return build_call_expr_loc (loc, fndecl, 1, tmp);
7698 }
7699
7700 return NULL_TREE;
7701 }
7702
7703 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7704 Return NULL_TREE if no simplification can be made. */
7705
7706 static tree
7707 fold_builtin_tan (tree arg, tree type)
7708 {
7709 enum built_in_function fcode;
7710 tree res;
7711
7712 if (!validate_arg (arg, REAL_TYPE))
7713 return NULL_TREE;
7714
7715 /* Calculate the result when the argument is a constant. */
7716 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7717 return res;
7718
7719 /* Optimize tan(atan(x)) = x. */
7720 fcode = builtin_mathfn_code (arg);
7721 if (flag_unsafe_math_optimizations
7722 && (fcode == BUILT_IN_ATAN
7723 || fcode == BUILT_IN_ATANF
7724 || fcode == BUILT_IN_ATANL))
7725 return CALL_EXPR_ARG (arg, 0);
7726
7727 return NULL_TREE;
7728 }
7729
7730 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7731 NULL_TREE if no simplification can be made. */
7732
7733 static tree
7734 fold_builtin_sincos (location_t loc,
7735 tree arg0, tree arg1, tree arg2)
7736 {
7737 tree type;
7738 tree res, fn, call;
7739
7740 if (!validate_arg (arg0, REAL_TYPE)
7741 || !validate_arg (arg1, POINTER_TYPE)
7742 || !validate_arg (arg2, POINTER_TYPE))
7743 return NULL_TREE;
7744
7745 type = TREE_TYPE (arg0);
7746
7747 /* Calculate the result when the argument is a constant. */
7748 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7749 return res;
7750
7751 /* Canonicalize sincos to cexpi. */
7752 if (!targetm.libc_has_function (function_c99_math_complex))
7753 return NULL_TREE;
7754 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7755 if (!fn)
7756 return NULL_TREE;
7757
7758 call = build_call_expr_loc (loc, fn, 1, arg0);
7759 call = builtin_save_expr (call);
7760
7761 return build2 (COMPOUND_EXPR, void_type_node,
7762 build2 (MODIFY_EXPR, void_type_node,
7763 build_fold_indirect_ref_loc (loc, arg1),
7764 build1 (IMAGPART_EXPR, type, call)),
7765 build2 (MODIFY_EXPR, void_type_node,
7766 build_fold_indirect_ref_loc (loc, arg2),
7767 build1 (REALPART_EXPR, type, call)));
7768 }
7769
7770 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7771 NULL_TREE if no simplification can be made. */
7772
7773 static tree
7774 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7775 {
7776 tree rtype;
7777 tree realp, imagp, ifn;
7778 tree res;
7779
7780 if (!validate_arg (arg0, COMPLEX_TYPE)
7781 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7782 return NULL_TREE;
7783
7784 /* Calculate the result when the argument is a constant. */
7785 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7786 return res;
7787
7788 rtype = TREE_TYPE (TREE_TYPE (arg0));
7789
7790 /* In case we can figure out the real part of arg0 and it is constant zero
7791 fold to cexpi. */
7792 if (!targetm.libc_has_function (function_c99_math_complex))
7793 return NULL_TREE;
7794 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7795 if (!ifn)
7796 return NULL_TREE;
7797
7798 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7799 && real_zerop (realp))
7800 {
7801 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7802 return build_call_expr_loc (loc, ifn, 1, narg);
7803 }
7804
7805 /* In case we can easily decompose real and imaginary parts split cexp
7806 to exp (r) * cexpi (i). */
7807 if (flag_unsafe_math_optimizations
7808 && realp)
7809 {
7810 tree rfn, rcall, icall;
7811
7812 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7813 if (!rfn)
7814 return NULL_TREE;
7815
7816 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7817 if (!imagp)
7818 return NULL_TREE;
7819
7820 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7821 icall = builtin_save_expr (icall);
7822 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7823 rcall = builtin_save_expr (rcall);
7824 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7825 fold_build2_loc (loc, MULT_EXPR, rtype,
7826 rcall,
7827 fold_build1_loc (loc, REALPART_EXPR,
7828 rtype, icall)),
7829 fold_build2_loc (loc, MULT_EXPR, rtype,
7830 rcall,
7831 fold_build1_loc (loc, IMAGPART_EXPR,
7832 rtype, icall)));
7833 }
7834
7835 return NULL_TREE;
7836 }
7837
7838 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7839 Return NULL_TREE if no simplification can be made. */
7840
7841 static tree
7842 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7843 {
7844 if (!validate_arg (arg, REAL_TYPE))
7845 return NULL_TREE;
7846
7847 /* Optimize trunc of constant value. */
7848 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7849 {
7850 REAL_VALUE_TYPE r, x;
7851 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7852
7853 x = TREE_REAL_CST (arg);
7854 real_trunc (&r, TYPE_MODE (type), &x);
7855 return build_real (type, r);
7856 }
7857
7858 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7859 }
7860
7861 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7862 Return NULL_TREE if no simplification can be made. */
7863
7864 static tree
7865 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7866 {
7867 if (!validate_arg (arg, REAL_TYPE))
7868 return NULL_TREE;
7869
7870 /* Optimize floor of constant value. */
7871 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7872 {
7873 REAL_VALUE_TYPE x;
7874
7875 x = TREE_REAL_CST (arg);
7876 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7877 {
7878 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7879 REAL_VALUE_TYPE r;
7880
7881 real_floor (&r, TYPE_MODE (type), &x);
7882 return build_real (type, r);
7883 }
7884 }
7885
7886 /* Fold floor (x) where x is nonnegative to trunc (x). */
7887 if (tree_expr_nonnegative_p (arg))
7888 {
7889 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7890 if (truncfn)
7891 return build_call_expr_loc (loc, truncfn, 1, arg);
7892 }
7893
7894 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7895 }
7896
7897 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7898 Return NULL_TREE if no simplification can be made. */
7899
7900 static tree
7901 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7902 {
7903 if (!validate_arg (arg, REAL_TYPE))
7904 return NULL_TREE;
7905
7906 /* Optimize ceil of constant value. */
7907 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7908 {
7909 REAL_VALUE_TYPE x;
7910
7911 x = TREE_REAL_CST (arg);
7912 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7913 {
7914 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7915 REAL_VALUE_TYPE r;
7916
7917 real_ceil (&r, TYPE_MODE (type), &x);
7918 return build_real (type, r);
7919 }
7920 }
7921
7922 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7923 }
7924
7925 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7926 Return NULL_TREE if no simplification can be made. */
7927
7928 static tree
7929 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7930 {
7931 if (!validate_arg (arg, REAL_TYPE))
7932 return NULL_TREE;
7933
7934 /* Optimize round of constant value. */
7935 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7936 {
7937 REAL_VALUE_TYPE x;
7938
7939 x = TREE_REAL_CST (arg);
7940 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7941 {
7942 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7943 REAL_VALUE_TYPE r;
7944
7945 real_round (&r, TYPE_MODE (type), &x);
7946 return build_real (type, r);
7947 }
7948 }
7949
7950 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7951 }
7952
7953 /* Fold function call to builtin lround, lroundf or lroundl (or the
7954 corresponding long long versions) and other rounding functions. ARG
7955 is the argument to the call. Return NULL_TREE if no simplification
7956 can be made. */
7957
7958 static tree
7959 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7960 {
7961 if (!validate_arg (arg, REAL_TYPE))
7962 return NULL_TREE;
7963
7964 /* Optimize lround of constant value. */
7965 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7966 {
7967 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7968
7969 if (real_isfinite (&x))
7970 {
7971 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7972 tree ftype = TREE_TYPE (arg);
7973 REAL_VALUE_TYPE r;
7974 bool fail = false;
7975
7976 switch (DECL_FUNCTION_CODE (fndecl))
7977 {
7978 CASE_FLT_FN (BUILT_IN_IFLOOR):
7979 CASE_FLT_FN (BUILT_IN_LFLOOR):
7980 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7981 real_floor (&r, TYPE_MODE (ftype), &x);
7982 break;
7983
7984 CASE_FLT_FN (BUILT_IN_ICEIL):
7985 CASE_FLT_FN (BUILT_IN_LCEIL):
7986 CASE_FLT_FN (BUILT_IN_LLCEIL):
7987 real_ceil (&r, TYPE_MODE (ftype), &x);
7988 break;
7989
7990 CASE_FLT_FN (BUILT_IN_IROUND):
7991 CASE_FLT_FN (BUILT_IN_LROUND):
7992 CASE_FLT_FN (BUILT_IN_LLROUND):
7993 real_round (&r, TYPE_MODE (ftype), &x);
7994 break;
7995
7996 default:
7997 gcc_unreachable ();
7998 }
7999
8000 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8001 if (!fail)
8002 return wide_int_to_tree (itype, val);
8003 }
8004 }
8005
8006 switch (DECL_FUNCTION_CODE (fndecl))
8007 {
8008 CASE_FLT_FN (BUILT_IN_LFLOOR):
8009 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8010 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8011 if (tree_expr_nonnegative_p (arg))
8012 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8013 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8014 break;
8015 default:;
8016 }
8017
8018 return fold_fixed_mathfn (loc, fndecl, arg);
8019 }
8020
8021 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8022 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8023 the argument to the call. Return NULL_TREE if no simplification can
8024 be made. */
8025
8026 static tree
8027 fold_builtin_bitop (tree fndecl, tree arg)
8028 {
8029 if (!validate_arg (arg, INTEGER_TYPE))
8030 return NULL_TREE;
8031
8032 /* Optimize for constant argument. */
8033 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8034 {
8035 tree type = TREE_TYPE (arg);
8036 int result;
8037
8038 switch (DECL_FUNCTION_CODE (fndecl))
8039 {
8040 CASE_INT_FN (BUILT_IN_FFS):
8041 result = wi::ffs (arg);
8042 break;
8043
8044 CASE_INT_FN (BUILT_IN_CLZ):
8045 if (wi::ne_p (arg, 0))
8046 result = wi::clz (arg);
8047 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8048 result = TYPE_PRECISION (type);
8049 break;
8050
8051 CASE_INT_FN (BUILT_IN_CTZ):
8052 if (wi::ne_p (arg, 0))
8053 result = wi::ctz (arg);
8054 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8055 result = TYPE_PRECISION (type);
8056 break;
8057
8058 CASE_INT_FN (BUILT_IN_CLRSB):
8059 result = wi::clrsb (arg);
8060 break;
8061
8062 CASE_INT_FN (BUILT_IN_POPCOUNT):
8063 result = wi::popcount (arg);
8064 break;
8065
8066 CASE_INT_FN (BUILT_IN_PARITY):
8067 result = wi::parity (arg);
8068 break;
8069
8070 default:
8071 gcc_unreachable ();
8072 }
8073
8074 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8075 }
8076
8077 return NULL_TREE;
8078 }
8079
8080 /* Fold function call to builtin_bswap and the short, long and long long
8081 variants. Return NULL_TREE if no simplification can be made. */
8082 static tree
8083 fold_builtin_bswap (tree fndecl, tree arg)
8084 {
8085 if (! validate_arg (arg, INTEGER_TYPE))
8086 return NULL_TREE;
8087
8088 /* Optimize constant value. */
8089 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8090 {
8091 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8092
8093 switch (DECL_FUNCTION_CODE (fndecl))
8094 {
8095 case BUILT_IN_BSWAP16:
8096 case BUILT_IN_BSWAP32:
8097 case BUILT_IN_BSWAP64:
8098 {
8099 signop sgn = TYPE_SIGN (type);
8100 tree result =
8101 wide_int_to_tree (type,
8102 wide_int::from (arg, TYPE_PRECISION (type),
8103 sgn).bswap ());
8104 return result;
8105 }
8106 default:
8107 gcc_unreachable ();
8108 }
8109 }
8110
8111 return NULL_TREE;
8112 }
8113
8114 /* A subroutine of fold_builtin to fold the various logarithmic
8115 functions. Return NULL_TREE if no simplification can me made.
8116 FUNC is the corresponding MPFR logarithm function. */
8117
8118 static tree
8119 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8120 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8121 {
8122 if (validate_arg (arg, REAL_TYPE))
8123 {
8124 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8125 tree res;
8126 const enum built_in_function fcode = builtin_mathfn_code (arg);
8127
8128 /* Calculate the result when the argument is a constant. */
8129 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8130 return res;
8131
8132 /* Special case, optimize logN(expN(x)) = x. */
8133 if (flag_unsafe_math_optimizations
8134 && ((func == mpfr_log
8135 && (fcode == BUILT_IN_EXP
8136 || fcode == BUILT_IN_EXPF
8137 || fcode == BUILT_IN_EXPL))
8138 || (func == mpfr_log2
8139 && (fcode == BUILT_IN_EXP2
8140 || fcode == BUILT_IN_EXP2F
8141 || fcode == BUILT_IN_EXP2L))
8142 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8143 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8144
8145 /* Optimize logN(func()) for various exponential functions. We
8146 want to determine the value "x" and the power "exponent" in
8147 order to transform logN(x**exponent) into exponent*logN(x). */
8148 if (flag_unsafe_math_optimizations)
8149 {
8150 tree exponent = 0, x = 0;
8151
8152 switch (fcode)
8153 {
8154 CASE_FLT_FN (BUILT_IN_EXP):
8155 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8156 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8157 dconst_e ()));
8158 exponent = CALL_EXPR_ARG (arg, 0);
8159 break;
8160 CASE_FLT_FN (BUILT_IN_EXP2):
8161 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8162 x = build_real (type, dconst2);
8163 exponent = CALL_EXPR_ARG (arg, 0);
8164 break;
8165 CASE_FLT_FN (BUILT_IN_EXP10):
8166 CASE_FLT_FN (BUILT_IN_POW10):
8167 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8168 {
8169 REAL_VALUE_TYPE dconst10;
8170 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8171 x = build_real (type, dconst10);
8172 }
8173 exponent = CALL_EXPR_ARG (arg, 0);
8174 break;
8175 CASE_FLT_FN (BUILT_IN_SQRT):
8176 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8177 x = CALL_EXPR_ARG (arg, 0);
8178 exponent = build_real (type, dconsthalf);
8179 break;
8180 CASE_FLT_FN (BUILT_IN_CBRT):
8181 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8182 x = CALL_EXPR_ARG (arg, 0);
8183 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8184 dconst_third ()));
8185 break;
8186 CASE_FLT_FN (BUILT_IN_POW):
8187 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8188 x = CALL_EXPR_ARG (arg, 0);
8189 exponent = CALL_EXPR_ARG (arg, 1);
8190 break;
8191 default:
8192 break;
8193 }
8194
8195 /* Now perform the optimization. */
8196 if (x && exponent)
8197 {
8198 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8199 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8200 }
8201 }
8202 }
8203
8204 return NULL_TREE;
8205 }
8206
8207 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8208 NULL_TREE if no simplification can be made. */
8209
8210 static tree
8211 fold_builtin_hypot (location_t loc, tree fndecl,
8212 tree arg0, tree arg1, tree type)
8213 {
8214 tree res, narg0, narg1;
8215
8216 if (!validate_arg (arg0, REAL_TYPE)
8217 || !validate_arg (arg1, REAL_TYPE))
8218 return NULL_TREE;
8219
8220 /* Calculate the result when the argument is a constant. */
8221 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8222 return res;
8223
8224 /* If either argument to hypot has a negate or abs, strip that off.
8225 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8226 narg0 = fold_strip_sign_ops (arg0);
8227 narg1 = fold_strip_sign_ops (arg1);
8228 if (narg0 || narg1)
8229 {
8230 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8231 narg1 ? narg1 : arg1);
8232 }
8233
8234 /* If either argument is zero, hypot is fabs of the other. */
8235 if (real_zerop (arg0))
8236 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8237 else if (real_zerop (arg1))
8238 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8239
8240 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8241 if (flag_unsafe_math_optimizations
8242 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8243 {
8244 const REAL_VALUE_TYPE sqrt2_trunc
8245 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8246 return fold_build2_loc (loc, MULT_EXPR, type,
8247 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8248 build_real (type, sqrt2_trunc));
8249 }
8250
8251 return NULL_TREE;
8252 }
8253
8254
8255 /* Fold a builtin function call to pow, powf, or powl. Return
8256 NULL_TREE if no simplification can be made. */
8257 static tree
8258 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8259 {
8260 tree res;
8261
8262 if (!validate_arg (arg0, REAL_TYPE)
8263 || !validate_arg (arg1, REAL_TYPE))
8264 return NULL_TREE;
8265
8266 /* Calculate the result when the argument is a constant. */
8267 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8268 return res;
8269
8270 /* Optimize pow(1.0,y) = 1.0. */
8271 if (real_onep (arg0))
8272 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8273
8274 if (TREE_CODE (arg1) == REAL_CST
8275 && !TREE_OVERFLOW (arg1))
8276 {
8277 REAL_VALUE_TYPE cint;
8278 REAL_VALUE_TYPE c;
8279 HOST_WIDE_INT n;
8280
8281 c = TREE_REAL_CST (arg1);
8282
8283 /* Optimize pow(x,0.0) = 1.0. */
8284 if (REAL_VALUES_EQUAL (c, dconst0))
8285 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8286 arg0);
8287
8288 /* Optimize pow(x,1.0) = x. */
8289 if (REAL_VALUES_EQUAL (c, dconst1))
8290 return arg0;
8291
8292 /* Optimize pow(x,-1.0) = 1.0/x. */
8293 if (REAL_VALUES_EQUAL (c, dconstm1))
8294 return fold_build2_loc (loc, RDIV_EXPR, type,
8295 build_real (type, dconst1), arg0);
8296
8297 /* Optimize pow(x,0.5) = sqrt(x). */
8298 if (flag_unsafe_math_optimizations
8299 && REAL_VALUES_EQUAL (c, dconsthalf))
8300 {
8301 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8302
8303 if (sqrtfn != NULL_TREE)
8304 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8305 }
8306
8307 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8308 if (flag_unsafe_math_optimizations)
8309 {
8310 const REAL_VALUE_TYPE dconstroot
8311 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8312
8313 if (REAL_VALUES_EQUAL (c, dconstroot))
8314 {
8315 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8316 if (cbrtfn != NULL_TREE)
8317 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8318 }
8319 }
8320
8321 /* Check for an integer exponent. */
8322 n = real_to_integer (&c);
8323 real_from_integer (&cint, VOIDmode, n, SIGNED);
8324 if (real_identical (&c, &cint))
8325 {
8326 /* Attempt to evaluate pow at compile-time, unless this should
8327 raise an exception. */
8328 if (TREE_CODE (arg0) == REAL_CST
8329 && !TREE_OVERFLOW (arg0)
8330 && (n > 0
8331 || (!flag_trapping_math && !flag_errno_math)
8332 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8333 {
8334 REAL_VALUE_TYPE x;
8335 bool inexact;
8336
8337 x = TREE_REAL_CST (arg0);
8338 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8339 if (flag_unsafe_math_optimizations || !inexact)
8340 return build_real (type, x);
8341 }
8342
8343 /* Strip sign ops from even integer powers. */
8344 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8345 {
8346 tree narg0 = fold_strip_sign_ops (arg0);
8347 if (narg0)
8348 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8349 }
8350 }
8351 }
8352
8353 if (flag_unsafe_math_optimizations)
8354 {
8355 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8356
8357 /* Optimize pow(expN(x),y) = expN(x*y). */
8358 if (BUILTIN_EXPONENT_P (fcode))
8359 {
8360 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8361 tree arg = CALL_EXPR_ARG (arg0, 0);
8362 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8363 return build_call_expr_loc (loc, expfn, 1, arg);
8364 }
8365
8366 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8367 if (BUILTIN_SQRT_P (fcode))
8368 {
8369 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8370 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8371 build_real (type, dconsthalf));
8372 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8373 }
8374
8375 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8376 if (BUILTIN_CBRT_P (fcode))
8377 {
8378 tree arg = CALL_EXPR_ARG (arg0, 0);
8379 if (tree_expr_nonnegative_p (arg))
8380 {
8381 const REAL_VALUE_TYPE dconstroot
8382 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8383 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8384 build_real (type, dconstroot));
8385 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8386 }
8387 }
8388
8389 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8390 if (fcode == BUILT_IN_POW
8391 || fcode == BUILT_IN_POWF
8392 || fcode == BUILT_IN_POWL)
8393 {
8394 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8395 if (tree_expr_nonnegative_p (arg00))
8396 {
8397 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8398 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8399 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8400 }
8401 }
8402 }
8403
8404 return NULL_TREE;
8405 }
8406
8407 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8408 Return NULL_TREE if no simplification can be made. */
8409 static tree
8410 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8411 tree arg0, tree arg1, tree type)
8412 {
8413 if (!validate_arg (arg0, REAL_TYPE)
8414 || !validate_arg (arg1, INTEGER_TYPE))
8415 return NULL_TREE;
8416
8417 /* Optimize pow(1.0,y) = 1.0. */
8418 if (real_onep (arg0))
8419 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8420
8421 if (tree_fits_shwi_p (arg1))
8422 {
8423 HOST_WIDE_INT c = tree_to_shwi (arg1);
8424
8425 /* Evaluate powi at compile-time. */
8426 if (TREE_CODE (arg0) == REAL_CST
8427 && !TREE_OVERFLOW (arg0))
8428 {
8429 REAL_VALUE_TYPE x;
8430 x = TREE_REAL_CST (arg0);
8431 real_powi (&x, TYPE_MODE (type), &x, c);
8432 return build_real (type, x);
8433 }
8434
8435 /* Optimize pow(x,0) = 1.0. */
8436 if (c == 0)
8437 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8438 arg0);
8439
8440 /* Optimize pow(x,1) = x. */
8441 if (c == 1)
8442 return arg0;
8443
8444 /* Optimize pow(x,-1) = 1.0/x. */
8445 if (c == -1)
8446 return fold_build2_loc (loc, RDIV_EXPR, type,
8447 build_real (type, dconst1), arg0);
8448 }
8449
8450 return NULL_TREE;
8451 }
8452
8453 /* A subroutine of fold_builtin to fold the various exponent
8454 functions. Return NULL_TREE if no simplification can be made.
8455 FUNC is the corresponding MPFR exponent function. */
8456
8457 static tree
8458 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8459 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8460 {
8461 if (validate_arg (arg, REAL_TYPE))
8462 {
8463 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8464 tree res;
8465
8466 /* Calculate the result when the argument is a constant. */
8467 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8468 return res;
8469
8470 /* Optimize expN(logN(x)) = x. */
8471 if (flag_unsafe_math_optimizations)
8472 {
8473 const enum built_in_function fcode = builtin_mathfn_code (arg);
8474
8475 if ((func == mpfr_exp
8476 && (fcode == BUILT_IN_LOG
8477 || fcode == BUILT_IN_LOGF
8478 || fcode == BUILT_IN_LOGL))
8479 || (func == mpfr_exp2
8480 && (fcode == BUILT_IN_LOG2
8481 || fcode == BUILT_IN_LOG2F
8482 || fcode == BUILT_IN_LOG2L))
8483 || (func == mpfr_exp10
8484 && (fcode == BUILT_IN_LOG10
8485 || fcode == BUILT_IN_LOG10F
8486 || fcode == BUILT_IN_LOG10L)))
8487 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8488 }
8489 }
8490
8491 return NULL_TREE;
8492 }
8493
8494 /* Return true if VAR is a VAR_DECL or a component thereof. */
8495
8496 static bool
8497 var_decl_component_p (tree var)
8498 {
8499 tree inner = var;
8500 while (handled_component_p (inner))
8501 inner = TREE_OPERAND (inner, 0);
8502 return SSA_VAR_P (inner);
8503 }
8504
8505 /* Fold function call to builtin memset. Return
8506 NULL_TREE if no simplification can be made. */
8507
8508 static tree
8509 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8510 tree type, bool ignore)
8511 {
8512 tree var, ret, etype;
8513 unsigned HOST_WIDE_INT length, cval;
8514
8515 if (! validate_arg (dest, POINTER_TYPE)
8516 || ! validate_arg (c, INTEGER_TYPE)
8517 || ! validate_arg (len, INTEGER_TYPE))
8518 return NULL_TREE;
8519
8520 if (! tree_fits_uhwi_p (len))
8521 return NULL_TREE;
8522
8523 /* If the LEN parameter is zero, return DEST. */
8524 if (integer_zerop (len))
8525 return omit_one_operand_loc (loc, type, dest, c);
8526
8527 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8528 return NULL_TREE;
8529
8530 var = dest;
8531 STRIP_NOPS (var);
8532 if (TREE_CODE (var) != ADDR_EXPR)
8533 return NULL_TREE;
8534
8535 var = TREE_OPERAND (var, 0);
8536 if (TREE_THIS_VOLATILE (var))
8537 return NULL_TREE;
8538
8539 etype = TREE_TYPE (var);
8540 if (TREE_CODE (etype) == ARRAY_TYPE)
8541 etype = TREE_TYPE (etype);
8542
8543 if (!INTEGRAL_TYPE_P (etype)
8544 && !POINTER_TYPE_P (etype))
8545 return NULL_TREE;
8546
8547 if (! var_decl_component_p (var))
8548 return NULL_TREE;
8549
8550 length = tree_to_uhwi (len);
8551 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8552 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8553 return NULL_TREE;
8554
8555 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8556 return NULL_TREE;
8557
8558 if (integer_zerop (c))
8559 cval = 0;
8560 else
8561 {
8562 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8563 return NULL_TREE;
8564
8565 cval = TREE_INT_CST_LOW (c);
8566 cval &= 0xff;
8567 cval |= cval << 8;
8568 cval |= cval << 16;
8569 cval |= (cval << 31) << 1;
8570 }
8571
8572 ret = build_int_cst_type (etype, cval);
8573 var = build_fold_indirect_ref_loc (loc,
8574 fold_convert_loc (loc,
8575 build_pointer_type (etype),
8576 dest));
8577 ret = build2 (MODIFY_EXPR, etype, var, ret);
8578 if (ignore)
8579 return ret;
8580
8581 return omit_one_operand_loc (loc, type, dest, ret);
8582 }
8583
8584 /* Fold function call to builtin memset. Return
8585 NULL_TREE if no simplification can be made. */
8586
8587 static tree
8588 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8589 {
8590 if (! validate_arg (dest, POINTER_TYPE)
8591 || ! validate_arg (size, INTEGER_TYPE))
8592 return NULL_TREE;
8593
8594 if (!ignore)
8595 return NULL_TREE;
8596
8597 /* New argument list transforming bzero(ptr x, int y) to
8598 memset(ptr x, int 0, size_t y). This is done this way
8599 so that if it isn't expanded inline, we fallback to
8600 calling bzero instead of memset. */
8601
8602 return fold_builtin_memset (loc, dest, integer_zero_node,
8603 fold_convert_loc (loc, size_type_node, size),
8604 void_type_node, ignore);
8605 }
8606
8607 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8608 NULL_TREE if no simplification can be made.
8609 If ENDP is 0, return DEST (like memcpy).
8610 If ENDP is 1, return DEST+LEN (like mempcpy).
8611 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8612 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8613 (memmove). */
8614
8615 static tree
8616 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8617 tree len, tree type, bool ignore, int endp)
8618 {
8619 tree destvar, srcvar, expr;
8620
8621 if (! validate_arg (dest, POINTER_TYPE)
8622 || ! validate_arg (src, POINTER_TYPE)
8623 || ! validate_arg (len, INTEGER_TYPE))
8624 return NULL_TREE;
8625
8626 /* If the LEN parameter is zero, return DEST. */
8627 if (integer_zerop (len))
8628 return omit_one_operand_loc (loc, type, dest, src);
8629
8630 /* If SRC and DEST are the same (and not volatile), return
8631 DEST{,+LEN,+LEN-1}. */
8632 if (operand_equal_p (src, dest, 0))
8633 expr = len;
8634 else
8635 {
8636 tree srctype, desttype;
8637 unsigned int src_align, dest_align;
8638 tree off0;
8639
8640 if (endp == 3)
8641 {
8642 src_align = get_pointer_alignment (src);
8643 dest_align = get_pointer_alignment (dest);
8644
8645 /* Both DEST and SRC must be pointer types.
8646 ??? This is what old code did. Is the testing for pointer types
8647 really mandatory?
8648
8649 If either SRC is readonly or length is 1, we can use memcpy. */
8650 if (!dest_align || !src_align)
8651 return NULL_TREE;
8652 if (readonly_data_expr (src)
8653 || (tree_fits_uhwi_p (len)
8654 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8655 >= tree_to_uhwi (len))))
8656 {
8657 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8658 if (!fn)
8659 return NULL_TREE;
8660 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8661 }
8662
8663 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8664 if (TREE_CODE (src) == ADDR_EXPR
8665 && TREE_CODE (dest) == ADDR_EXPR)
8666 {
8667 tree src_base, dest_base, fn;
8668 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8669 HOST_WIDE_INT size = -1;
8670 HOST_WIDE_INT maxsize = -1;
8671
8672 srcvar = TREE_OPERAND (src, 0);
8673 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8674 &size, &maxsize);
8675 destvar = TREE_OPERAND (dest, 0);
8676 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8677 &size, &maxsize);
8678 if (tree_fits_uhwi_p (len))
8679 maxsize = tree_to_uhwi (len);
8680 else
8681 maxsize = -1;
8682 src_offset /= BITS_PER_UNIT;
8683 dest_offset /= BITS_PER_UNIT;
8684 if (SSA_VAR_P (src_base)
8685 && SSA_VAR_P (dest_base))
8686 {
8687 if (operand_equal_p (src_base, dest_base, 0)
8688 && ranges_overlap_p (src_offset, maxsize,
8689 dest_offset, maxsize))
8690 return NULL_TREE;
8691 }
8692 else if (TREE_CODE (src_base) == MEM_REF
8693 && TREE_CODE (dest_base) == MEM_REF)
8694 {
8695 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8696 TREE_OPERAND (dest_base, 0), 0))
8697 return NULL_TREE;
8698 offset_int off = mem_ref_offset (src_base) + src_offset;
8699 if (!wi::fits_shwi_p (off))
8700 return NULL_TREE;
8701 src_offset = off.to_shwi ();
8702
8703 off = mem_ref_offset (dest_base) + dest_offset;
8704 if (!wi::fits_shwi_p (off))
8705 return NULL_TREE;
8706 dest_offset = off.to_shwi ();
8707 if (ranges_overlap_p (src_offset, maxsize,
8708 dest_offset, maxsize))
8709 return NULL_TREE;
8710 }
8711 else
8712 return NULL_TREE;
8713
8714 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8715 if (!fn)
8716 return NULL_TREE;
8717 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8718 }
8719
8720 /* If the destination and source do not alias optimize into
8721 memcpy as well. */
8722 if ((is_gimple_min_invariant (dest)
8723 || TREE_CODE (dest) == SSA_NAME)
8724 && (is_gimple_min_invariant (src)
8725 || TREE_CODE (src) == SSA_NAME))
8726 {
8727 ao_ref destr, srcr;
8728 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8729 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8730 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8731 {
8732 tree fn;
8733 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8734 if (!fn)
8735 return NULL_TREE;
8736 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8737 }
8738 }
8739
8740 return NULL_TREE;
8741 }
8742
8743 if (!tree_fits_shwi_p (len))
8744 return NULL_TREE;
8745 /* FIXME:
8746 This logic lose for arguments like (type *)malloc (sizeof (type)),
8747 since we strip the casts of up to VOID return value from malloc.
8748 Perhaps we ought to inherit type from non-VOID argument here? */
8749 STRIP_NOPS (src);
8750 STRIP_NOPS (dest);
8751 if (!POINTER_TYPE_P (TREE_TYPE (src))
8752 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8753 return NULL_TREE;
8754 /* In the following try to find a type that is most natural to be
8755 used for the memcpy source and destination and that allows
8756 the most optimization when memcpy is turned into a plain assignment
8757 using that type. In theory we could always use a char[len] type
8758 but that only gains us that the destination and source possibly
8759 no longer will have their address taken. */
8760 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8761 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8762 {
8763 tree tem = TREE_OPERAND (src, 0);
8764 STRIP_NOPS (tem);
8765 if (tem != TREE_OPERAND (src, 0))
8766 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8767 }
8768 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8769 {
8770 tree tem = TREE_OPERAND (dest, 0);
8771 STRIP_NOPS (tem);
8772 if (tem != TREE_OPERAND (dest, 0))
8773 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8774 }
8775 srctype = TREE_TYPE (TREE_TYPE (src));
8776 if (TREE_CODE (srctype) == ARRAY_TYPE
8777 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8778 {
8779 srctype = TREE_TYPE (srctype);
8780 STRIP_NOPS (src);
8781 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8782 }
8783 desttype = TREE_TYPE (TREE_TYPE (dest));
8784 if (TREE_CODE (desttype) == ARRAY_TYPE
8785 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8786 {
8787 desttype = TREE_TYPE (desttype);
8788 STRIP_NOPS (dest);
8789 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8790 }
8791 if (TREE_ADDRESSABLE (srctype)
8792 || TREE_ADDRESSABLE (desttype))
8793 return NULL_TREE;
8794
8795 /* Make sure we are not copying using a floating-point mode or
8796 a type whose size possibly does not match its precision. */
8797 if (FLOAT_MODE_P (TYPE_MODE (desttype))
8798 || TREE_CODE (desttype) == BOOLEAN_TYPE
8799 || TREE_CODE (desttype) == ENUMERAL_TYPE)
8800 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
8801 if (FLOAT_MODE_P (TYPE_MODE (srctype))
8802 || TREE_CODE (srctype) == BOOLEAN_TYPE
8803 || TREE_CODE (srctype) == ENUMERAL_TYPE)
8804 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
8805 if (!srctype)
8806 srctype = desttype;
8807 if (!desttype)
8808 desttype = srctype;
8809 if (!srctype)
8810 return NULL_TREE;
8811
8812 src_align = get_pointer_alignment (src);
8813 dest_align = get_pointer_alignment (dest);
8814 if (dest_align < TYPE_ALIGN (desttype)
8815 || src_align < TYPE_ALIGN (srctype))
8816 return NULL_TREE;
8817
8818 if (!ignore)
8819 dest = builtin_save_expr (dest);
8820
8821 /* Build accesses at offset zero with a ref-all character type. */
8822 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8823 ptr_mode, true), 0);
8824
8825 destvar = dest;
8826 STRIP_NOPS (destvar);
8827 if (TREE_CODE (destvar) == ADDR_EXPR
8828 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8829 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8830 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8831 else
8832 destvar = NULL_TREE;
8833
8834 srcvar = src;
8835 STRIP_NOPS (srcvar);
8836 if (TREE_CODE (srcvar) == ADDR_EXPR
8837 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8838 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8839 {
8840 if (!destvar
8841 || src_align >= TYPE_ALIGN (desttype))
8842 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8843 srcvar, off0);
8844 else if (!STRICT_ALIGNMENT)
8845 {
8846 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8847 src_align);
8848 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8849 }
8850 else
8851 srcvar = NULL_TREE;
8852 }
8853 else
8854 srcvar = NULL_TREE;
8855
8856 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8857 return NULL_TREE;
8858
8859 if (srcvar == NULL_TREE)
8860 {
8861 STRIP_NOPS (src);
8862 if (src_align >= TYPE_ALIGN (desttype))
8863 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8864 else
8865 {
8866 if (STRICT_ALIGNMENT)
8867 return NULL_TREE;
8868 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8869 src_align);
8870 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8871 }
8872 }
8873 else if (destvar == NULL_TREE)
8874 {
8875 STRIP_NOPS (dest);
8876 if (dest_align >= TYPE_ALIGN (srctype))
8877 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8878 else
8879 {
8880 if (STRICT_ALIGNMENT)
8881 return NULL_TREE;
8882 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8883 dest_align);
8884 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8885 }
8886 }
8887
8888 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8889 }
8890
8891 if (ignore)
8892 return expr;
8893
8894 if (endp == 0 || endp == 3)
8895 return omit_one_operand_loc (loc, type, dest, expr);
8896
8897 if (expr == len)
8898 expr = NULL_TREE;
8899
8900 if (endp == 2)
8901 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8902 ssize_int (1));
8903
8904 dest = fold_build_pointer_plus_loc (loc, dest, len);
8905 dest = fold_convert_loc (loc, type, dest);
8906 if (expr)
8907 dest = omit_one_operand_loc (loc, type, dest, expr);
8908 return dest;
8909 }
8910
8911 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8912 If LEN is not NULL, it represents the length of the string to be
8913 copied. Return NULL_TREE if no simplification can be made. */
8914
8915 tree
8916 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8917 {
8918 tree fn;
8919
8920 if (!validate_arg (dest, POINTER_TYPE)
8921 || !validate_arg (src, POINTER_TYPE))
8922 return NULL_TREE;
8923
8924 /* If SRC and DEST are the same (and not volatile), return DEST. */
8925 if (operand_equal_p (src, dest, 0))
8926 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8927
8928 if (optimize_function_for_size_p (cfun))
8929 return NULL_TREE;
8930
8931 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8932 if (!fn)
8933 return NULL_TREE;
8934
8935 if (!len)
8936 {
8937 len = c_strlen (src, 1);
8938 if (! len || TREE_SIDE_EFFECTS (len))
8939 return NULL_TREE;
8940 }
8941
8942 len = fold_convert_loc (loc, size_type_node, len);
8943 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8944 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8945 build_call_expr_loc (loc, fn, 3, dest, src, len));
8946 }
8947
8948 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8949 Return NULL_TREE if no simplification can be made. */
8950
8951 static tree
8952 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8953 {
8954 tree fn, len, lenp1, call, type;
8955
8956 if (!validate_arg (dest, POINTER_TYPE)
8957 || !validate_arg (src, POINTER_TYPE))
8958 return NULL_TREE;
8959
8960 len = c_strlen (src, 1);
8961 if (!len
8962 || TREE_CODE (len) != INTEGER_CST)
8963 return NULL_TREE;
8964
8965 if (optimize_function_for_size_p (cfun)
8966 /* If length is zero it's small enough. */
8967 && !integer_zerop (len))
8968 return NULL_TREE;
8969
8970 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8971 if (!fn)
8972 return NULL_TREE;
8973
8974 lenp1 = size_binop_loc (loc, PLUS_EXPR,
8975 fold_convert_loc (loc, size_type_node, len),
8976 build_int_cst (size_type_node, 1));
8977 /* We use dest twice in building our expression. Save it from
8978 multiple expansions. */
8979 dest = builtin_save_expr (dest);
8980 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8981
8982 type = TREE_TYPE (TREE_TYPE (fndecl));
8983 dest = fold_build_pointer_plus_loc (loc, dest, len);
8984 dest = fold_convert_loc (loc, type, dest);
8985 dest = omit_one_operand_loc (loc, type, dest, call);
8986 return dest;
8987 }
8988
8989 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8990 If SLEN is not NULL, it represents the length of the source string.
8991 Return NULL_TREE if no simplification can be made. */
8992
8993 tree
8994 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8995 tree src, tree len, tree slen)
8996 {
8997 tree fn;
8998
8999 if (!validate_arg (dest, POINTER_TYPE)
9000 || !validate_arg (src, POINTER_TYPE)
9001 || !validate_arg (len, INTEGER_TYPE))
9002 return NULL_TREE;
9003
9004 /* If the LEN parameter is zero, return DEST. */
9005 if (integer_zerop (len))
9006 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9007
9008 /* We can't compare slen with len as constants below if len is not a
9009 constant. */
9010 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9011 return NULL_TREE;
9012
9013 if (!slen)
9014 slen = c_strlen (src, 1);
9015
9016 /* Now, we must be passed a constant src ptr parameter. */
9017 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9018 return NULL_TREE;
9019
9020 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9021
9022 /* We do not support simplification of this case, though we do
9023 support it when expanding trees into RTL. */
9024 /* FIXME: generate a call to __builtin_memset. */
9025 if (tree_int_cst_lt (slen, len))
9026 return NULL_TREE;
9027
9028 /* OK transform into builtin memcpy. */
9029 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9030 if (!fn)
9031 return NULL_TREE;
9032
9033 len = fold_convert_loc (loc, size_type_node, len);
9034 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9035 build_call_expr_loc (loc, fn, 3, dest, src, len));
9036 }
9037
9038 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9039 arguments to the call, and TYPE is its return type.
9040 Return NULL_TREE if no simplification can be made. */
9041
9042 static tree
9043 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9044 {
9045 if (!validate_arg (arg1, POINTER_TYPE)
9046 || !validate_arg (arg2, INTEGER_TYPE)
9047 || !validate_arg (len, INTEGER_TYPE))
9048 return NULL_TREE;
9049 else
9050 {
9051 const char *p1;
9052
9053 if (TREE_CODE (arg2) != INTEGER_CST
9054 || !tree_fits_uhwi_p (len))
9055 return NULL_TREE;
9056
9057 p1 = c_getstr (arg1);
9058 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9059 {
9060 char c;
9061 const char *r;
9062 tree tem;
9063
9064 if (target_char_cast (arg2, &c))
9065 return NULL_TREE;
9066
9067 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9068
9069 if (r == NULL)
9070 return build_int_cst (TREE_TYPE (arg1), 0);
9071
9072 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9073 return fold_convert_loc (loc, type, tem);
9074 }
9075 return NULL_TREE;
9076 }
9077 }
9078
9079 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9080 Return NULL_TREE if no simplification can be made. */
9081
9082 static tree
9083 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9084 {
9085 const char *p1, *p2;
9086
9087 if (!validate_arg (arg1, POINTER_TYPE)
9088 || !validate_arg (arg2, POINTER_TYPE)
9089 || !validate_arg (len, INTEGER_TYPE))
9090 return NULL_TREE;
9091
9092 /* If the LEN parameter is zero, return zero. */
9093 if (integer_zerop (len))
9094 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9095 arg1, arg2);
9096
9097 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9098 if (operand_equal_p (arg1, arg2, 0))
9099 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9100
9101 p1 = c_getstr (arg1);
9102 p2 = c_getstr (arg2);
9103
9104 /* If all arguments are constant, and the value of len is not greater
9105 than the lengths of arg1 and arg2, evaluate at compile-time. */
9106 if (tree_fits_uhwi_p (len) && p1 && p2
9107 && compare_tree_int (len, strlen (p1) + 1) <= 0
9108 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9109 {
9110 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9111
9112 if (r > 0)
9113 return integer_one_node;
9114 else if (r < 0)
9115 return integer_minus_one_node;
9116 else
9117 return integer_zero_node;
9118 }
9119
9120 /* If len parameter is one, return an expression corresponding to
9121 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9122 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9123 {
9124 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9125 tree cst_uchar_ptr_node
9126 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9127
9128 tree ind1
9129 = fold_convert_loc (loc, integer_type_node,
9130 build1 (INDIRECT_REF, cst_uchar_node,
9131 fold_convert_loc (loc,
9132 cst_uchar_ptr_node,
9133 arg1)));
9134 tree ind2
9135 = fold_convert_loc (loc, integer_type_node,
9136 build1 (INDIRECT_REF, cst_uchar_node,
9137 fold_convert_loc (loc,
9138 cst_uchar_ptr_node,
9139 arg2)));
9140 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9141 }
9142
9143 return NULL_TREE;
9144 }
9145
9146 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9147 Return NULL_TREE if no simplification can be made. */
9148
9149 static tree
9150 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9151 {
9152 const char *p1, *p2;
9153
9154 if (!validate_arg (arg1, POINTER_TYPE)
9155 || !validate_arg (arg2, POINTER_TYPE))
9156 return NULL_TREE;
9157
9158 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9159 if (operand_equal_p (arg1, arg2, 0))
9160 return integer_zero_node;
9161
9162 p1 = c_getstr (arg1);
9163 p2 = c_getstr (arg2);
9164
9165 if (p1 && p2)
9166 {
9167 const int i = strcmp (p1, p2);
9168 if (i < 0)
9169 return integer_minus_one_node;
9170 else if (i > 0)
9171 return integer_one_node;
9172 else
9173 return integer_zero_node;
9174 }
9175
9176 /* If the second arg is "", return *(const unsigned char*)arg1. */
9177 if (p2 && *p2 == '\0')
9178 {
9179 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9180 tree cst_uchar_ptr_node
9181 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9182
9183 return fold_convert_loc (loc, integer_type_node,
9184 build1 (INDIRECT_REF, cst_uchar_node,
9185 fold_convert_loc (loc,
9186 cst_uchar_ptr_node,
9187 arg1)));
9188 }
9189
9190 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9191 if (p1 && *p1 == '\0')
9192 {
9193 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9194 tree cst_uchar_ptr_node
9195 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9196
9197 tree temp
9198 = fold_convert_loc (loc, integer_type_node,
9199 build1 (INDIRECT_REF, cst_uchar_node,
9200 fold_convert_loc (loc,
9201 cst_uchar_ptr_node,
9202 arg2)));
9203 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9204 }
9205
9206 return NULL_TREE;
9207 }
9208
9209 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9210 Return NULL_TREE if no simplification can be made. */
9211
9212 static tree
9213 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9214 {
9215 const char *p1, *p2;
9216
9217 if (!validate_arg (arg1, POINTER_TYPE)
9218 || !validate_arg (arg2, POINTER_TYPE)
9219 || !validate_arg (len, INTEGER_TYPE))
9220 return NULL_TREE;
9221
9222 /* If the LEN parameter is zero, return zero. */
9223 if (integer_zerop (len))
9224 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9225 arg1, arg2);
9226
9227 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9228 if (operand_equal_p (arg1, arg2, 0))
9229 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9230
9231 p1 = c_getstr (arg1);
9232 p2 = c_getstr (arg2);
9233
9234 if (tree_fits_uhwi_p (len) && p1 && p2)
9235 {
9236 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9237 if (i > 0)
9238 return integer_one_node;
9239 else if (i < 0)
9240 return integer_minus_one_node;
9241 else
9242 return integer_zero_node;
9243 }
9244
9245 /* If the second arg is "", and the length is greater than zero,
9246 return *(const unsigned char*)arg1. */
9247 if (p2 && *p2 == '\0'
9248 && TREE_CODE (len) == INTEGER_CST
9249 && tree_int_cst_sgn (len) == 1)
9250 {
9251 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9252 tree cst_uchar_ptr_node
9253 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9254
9255 return fold_convert_loc (loc, integer_type_node,
9256 build1 (INDIRECT_REF, cst_uchar_node,
9257 fold_convert_loc (loc,
9258 cst_uchar_ptr_node,
9259 arg1)));
9260 }
9261
9262 /* If the first arg is "", and the length is greater than zero,
9263 return -*(const unsigned char*)arg2. */
9264 if (p1 && *p1 == '\0'
9265 && TREE_CODE (len) == INTEGER_CST
9266 && tree_int_cst_sgn (len) == 1)
9267 {
9268 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9269 tree cst_uchar_ptr_node
9270 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9271
9272 tree temp = fold_convert_loc (loc, integer_type_node,
9273 build1 (INDIRECT_REF, cst_uchar_node,
9274 fold_convert_loc (loc,
9275 cst_uchar_ptr_node,
9276 arg2)));
9277 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9278 }
9279
9280 /* If len parameter is one, return an expression corresponding to
9281 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9282 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9283 {
9284 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9285 tree cst_uchar_ptr_node
9286 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9287
9288 tree ind1 = fold_convert_loc (loc, integer_type_node,
9289 build1 (INDIRECT_REF, cst_uchar_node,
9290 fold_convert_loc (loc,
9291 cst_uchar_ptr_node,
9292 arg1)));
9293 tree ind2 = fold_convert_loc (loc, integer_type_node,
9294 build1 (INDIRECT_REF, cst_uchar_node,
9295 fold_convert_loc (loc,
9296 cst_uchar_ptr_node,
9297 arg2)));
9298 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9299 }
9300
9301 return NULL_TREE;
9302 }
9303
9304 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9305 ARG. Return NULL_TREE if no simplification can be made. */
9306
9307 static tree
9308 fold_builtin_signbit (location_t loc, tree arg, tree type)
9309 {
9310 if (!validate_arg (arg, REAL_TYPE))
9311 return NULL_TREE;
9312
9313 /* If ARG is a compile-time constant, determine the result. */
9314 if (TREE_CODE (arg) == REAL_CST
9315 && !TREE_OVERFLOW (arg))
9316 {
9317 REAL_VALUE_TYPE c;
9318
9319 c = TREE_REAL_CST (arg);
9320 return (REAL_VALUE_NEGATIVE (c)
9321 ? build_one_cst (type)
9322 : build_zero_cst (type));
9323 }
9324
9325 /* If ARG is non-negative, the result is always zero. */
9326 if (tree_expr_nonnegative_p (arg))
9327 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9328
9329 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9330 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9331 return fold_convert (type,
9332 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9333 build_real (TREE_TYPE (arg), dconst0)));
9334
9335 return NULL_TREE;
9336 }
9337
9338 /* Fold function call to builtin copysign, copysignf or copysignl with
9339 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9340 be made. */
9341
9342 static tree
9343 fold_builtin_copysign (location_t loc, tree fndecl,
9344 tree arg1, tree arg2, tree type)
9345 {
9346 tree tem;
9347
9348 if (!validate_arg (arg1, REAL_TYPE)
9349 || !validate_arg (arg2, REAL_TYPE))
9350 return NULL_TREE;
9351
9352 /* copysign(X,X) is X. */
9353 if (operand_equal_p (arg1, arg2, 0))
9354 return fold_convert_loc (loc, type, arg1);
9355
9356 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9357 if (TREE_CODE (arg1) == REAL_CST
9358 && TREE_CODE (arg2) == REAL_CST
9359 && !TREE_OVERFLOW (arg1)
9360 && !TREE_OVERFLOW (arg2))
9361 {
9362 REAL_VALUE_TYPE c1, c2;
9363
9364 c1 = TREE_REAL_CST (arg1);
9365 c2 = TREE_REAL_CST (arg2);
9366 /* c1.sign := c2.sign. */
9367 real_copysign (&c1, &c2);
9368 return build_real (type, c1);
9369 }
9370
9371 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9372 Remember to evaluate Y for side-effects. */
9373 if (tree_expr_nonnegative_p (arg2))
9374 return omit_one_operand_loc (loc, type,
9375 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9376 arg2);
9377
9378 /* Strip sign changing operations for the first argument. */
9379 tem = fold_strip_sign_ops (arg1);
9380 if (tem)
9381 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9382
9383 return NULL_TREE;
9384 }
9385
9386 /* Fold a call to builtin isascii with argument ARG. */
9387
9388 static tree
9389 fold_builtin_isascii (location_t loc, tree arg)
9390 {
9391 if (!validate_arg (arg, INTEGER_TYPE))
9392 return NULL_TREE;
9393 else
9394 {
9395 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9396 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9397 build_int_cst (integer_type_node,
9398 ~ (unsigned HOST_WIDE_INT) 0x7f));
9399 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9400 arg, integer_zero_node);
9401 }
9402 }
9403
9404 /* Fold a call to builtin toascii with argument ARG. */
9405
9406 static tree
9407 fold_builtin_toascii (location_t loc, tree arg)
9408 {
9409 if (!validate_arg (arg, INTEGER_TYPE))
9410 return NULL_TREE;
9411
9412 /* Transform toascii(c) -> (c & 0x7f). */
9413 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9414 build_int_cst (integer_type_node, 0x7f));
9415 }
9416
9417 /* Fold a call to builtin isdigit with argument ARG. */
9418
9419 static tree
9420 fold_builtin_isdigit (location_t loc, tree arg)
9421 {
9422 if (!validate_arg (arg, INTEGER_TYPE))
9423 return NULL_TREE;
9424 else
9425 {
9426 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9427 /* According to the C standard, isdigit is unaffected by locale.
9428 However, it definitely is affected by the target character set. */
9429 unsigned HOST_WIDE_INT target_digit0
9430 = lang_hooks.to_target_charset ('0');
9431
9432 if (target_digit0 == 0)
9433 return NULL_TREE;
9434
9435 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9436 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9437 build_int_cst (unsigned_type_node, target_digit0));
9438 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9439 build_int_cst (unsigned_type_node, 9));
9440 }
9441 }
9442
9443 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9444
9445 static tree
9446 fold_builtin_fabs (location_t loc, tree arg, tree type)
9447 {
9448 if (!validate_arg (arg, REAL_TYPE))
9449 return NULL_TREE;
9450
9451 arg = fold_convert_loc (loc, type, arg);
9452 if (TREE_CODE (arg) == REAL_CST)
9453 return fold_abs_const (arg, type);
9454 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9455 }
9456
9457 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9458
9459 static tree
9460 fold_builtin_abs (location_t loc, tree arg, tree type)
9461 {
9462 if (!validate_arg (arg, INTEGER_TYPE))
9463 return NULL_TREE;
9464
9465 arg = fold_convert_loc (loc, type, arg);
9466 if (TREE_CODE (arg) == INTEGER_CST)
9467 return fold_abs_const (arg, type);
9468 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9469 }
9470
9471 /* Fold a fma operation with arguments ARG[012]. */
9472
9473 tree
9474 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9475 tree type, tree arg0, tree arg1, tree arg2)
9476 {
9477 if (TREE_CODE (arg0) == REAL_CST
9478 && TREE_CODE (arg1) == REAL_CST
9479 && TREE_CODE (arg2) == REAL_CST)
9480 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9481
9482 return NULL_TREE;
9483 }
9484
9485 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9486
9487 static tree
9488 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9489 {
9490 if (validate_arg (arg0, REAL_TYPE)
9491 && validate_arg (arg1, REAL_TYPE)
9492 && validate_arg (arg2, REAL_TYPE))
9493 {
9494 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9495 if (tem)
9496 return tem;
9497
9498 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9499 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9500 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9501 }
9502 return NULL_TREE;
9503 }
9504
9505 /* Fold a call to builtin fmin or fmax. */
9506
9507 static tree
9508 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9509 tree type, bool max)
9510 {
9511 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9512 {
9513 /* Calculate the result when the argument is a constant. */
9514 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9515
9516 if (res)
9517 return res;
9518
9519 /* If either argument is NaN, return the other one. Avoid the
9520 transformation if we get (and honor) a signalling NaN. Using
9521 omit_one_operand() ensures we create a non-lvalue. */
9522 if (TREE_CODE (arg0) == REAL_CST
9523 && real_isnan (&TREE_REAL_CST (arg0))
9524 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9525 || ! TREE_REAL_CST (arg0).signalling))
9526 return omit_one_operand_loc (loc, type, arg1, arg0);
9527 if (TREE_CODE (arg1) == REAL_CST
9528 && real_isnan (&TREE_REAL_CST (arg1))
9529 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9530 || ! TREE_REAL_CST (arg1).signalling))
9531 return omit_one_operand_loc (loc, type, arg0, arg1);
9532
9533 /* Transform fmin/fmax(x,x) -> x. */
9534 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9535 return omit_one_operand_loc (loc, type, arg0, arg1);
9536
9537 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9538 functions to return the numeric arg if the other one is NaN.
9539 These tree codes don't honor that, so only transform if
9540 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9541 handled, so we don't have to worry about it either. */
9542 if (flag_finite_math_only)
9543 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9544 fold_convert_loc (loc, type, arg0),
9545 fold_convert_loc (loc, type, arg1));
9546 }
9547 return NULL_TREE;
9548 }
9549
9550 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9551
9552 static tree
9553 fold_builtin_carg (location_t loc, tree arg, tree type)
9554 {
9555 if (validate_arg (arg, COMPLEX_TYPE)
9556 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9557 {
9558 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9559
9560 if (atan2_fn)
9561 {
9562 tree new_arg = builtin_save_expr (arg);
9563 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9564 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9565 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9566 }
9567 }
9568
9569 return NULL_TREE;
9570 }
9571
9572 /* Fold a call to builtin logb/ilogb. */
9573
9574 static tree
9575 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9576 {
9577 if (! validate_arg (arg, REAL_TYPE))
9578 return NULL_TREE;
9579
9580 STRIP_NOPS (arg);
9581
9582 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9583 {
9584 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9585
9586 switch (value->cl)
9587 {
9588 case rvc_nan:
9589 case rvc_inf:
9590 /* If arg is Inf or NaN and we're logb, return it. */
9591 if (TREE_CODE (rettype) == REAL_TYPE)
9592 {
9593 /* For logb(-Inf) we have to return +Inf. */
9594 if (real_isinf (value) && real_isneg (value))
9595 {
9596 REAL_VALUE_TYPE tem;
9597 real_inf (&tem);
9598 return build_real (rettype, tem);
9599 }
9600 return fold_convert_loc (loc, rettype, arg);
9601 }
9602 /* Fall through... */
9603 case rvc_zero:
9604 /* Zero may set errno and/or raise an exception for logb, also
9605 for ilogb we don't know FP_ILOGB0. */
9606 return NULL_TREE;
9607 case rvc_normal:
9608 /* For normal numbers, proceed iff radix == 2. In GCC,
9609 normalized significands are in the range [0.5, 1.0). We
9610 want the exponent as if they were [1.0, 2.0) so get the
9611 exponent and subtract 1. */
9612 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9613 return fold_convert_loc (loc, rettype,
9614 build_int_cst (integer_type_node,
9615 REAL_EXP (value)-1));
9616 break;
9617 }
9618 }
9619
9620 return NULL_TREE;
9621 }
9622
9623 /* Fold a call to builtin significand, if radix == 2. */
9624
9625 static tree
9626 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9627 {
9628 if (! validate_arg (arg, REAL_TYPE))
9629 return NULL_TREE;
9630
9631 STRIP_NOPS (arg);
9632
9633 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9634 {
9635 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9636
9637 switch (value->cl)
9638 {
9639 case rvc_zero:
9640 case rvc_nan:
9641 case rvc_inf:
9642 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9643 return fold_convert_loc (loc, rettype, arg);
9644 case rvc_normal:
9645 /* For normal numbers, proceed iff radix == 2. */
9646 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9647 {
9648 REAL_VALUE_TYPE result = *value;
9649 /* In GCC, normalized significands are in the range [0.5,
9650 1.0). We want them to be [1.0, 2.0) so set the
9651 exponent to 1. */
9652 SET_REAL_EXP (&result, 1);
9653 return build_real (rettype, result);
9654 }
9655 break;
9656 }
9657 }
9658
9659 return NULL_TREE;
9660 }
9661
9662 /* Fold a call to builtin frexp, we can assume the base is 2. */
9663
9664 static tree
9665 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9666 {
9667 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9668 return NULL_TREE;
9669
9670 STRIP_NOPS (arg0);
9671
9672 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9673 return NULL_TREE;
9674
9675 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9676
9677 /* Proceed if a valid pointer type was passed in. */
9678 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9679 {
9680 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9681 tree frac, exp;
9682
9683 switch (value->cl)
9684 {
9685 case rvc_zero:
9686 /* For +-0, return (*exp = 0, +-0). */
9687 exp = integer_zero_node;
9688 frac = arg0;
9689 break;
9690 case rvc_nan:
9691 case rvc_inf:
9692 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9693 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9694 case rvc_normal:
9695 {
9696 /* Since the frexp function always expects base 2, and in
9697 GCC normalized significands are already in the range
9698 [0.5, 1.0), we have exactly what frexp wants. */
9699 REAL_VALUE_TYPE frac_rvt = *value;
9700 SET_REAL_EXP (&frac_rvt, 0);
9701 frac = build_real (rettype, frac_rvt);
9702 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9703 }
9704 break;
9705 default:
9706 gcc_unreachable ();
9707 }
9708
9709 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9710 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9711 TREE_SIDE_EFFECTS (arg1) = 1;
9712 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9713 }
9714
9715 return NULL_TREE;
9716 }
9717
9718 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9719 then we can assume the base is two. If it's false, then we have to
9720 check the mode of the TYPE parameter in certain cases. */
9721
9722 static tree
9723 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9724 tree type, bool ldexp)
9725 {
9726 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9727 {
9728 STRIP_NOPS (arg0);
9729 STRIP_NOPS (arg1);
9730
9731 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9732 if (real_zerop (arg0) || integer_zerop (arg1)
9733 || (TREE_CODE (arg0) == REAL_CST
9734 && !real_isfinite (&TREE_REAL_CST (arg0))))
9735 return omit_one_operand_loc (loc, type, arg0, arg1);
9736
9737 /* If both arguments are constant, then try to evaluate it. */
9738 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9739 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9740 && tree_fits_shwi_p (arg1))
9741 {
9742 /* Bound the maximum adjustment to twice the range of the
9743 mode's valid exponents. Use abs to ensure the range is
9744 positive as a sanity check. */
9745 const long max_exp_adj = 2 *
9746 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9747 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9748
9749 /* Get the user-requested adjustment. */
9750 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9751
9752 /* The requested adjustment must be inside this range. This
9753 is a preliminary cap to avoid things like overflow, we
9754 may still fail to compute the result for other reasons. */
9755 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9756 {
9757 REAL_VALUE_TYPE initial_result;
9758
9759 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9760
9761 /* Ensure we didn't overflow. */
9762 if (! real_isinf (&initial_result))
9763 {
9764 const REAL_VALUE_TYPE trunc_result
9765 = real_value_truncate (TYPE_MODE (type), initial_result);
9766
9767 /* Only proceed if the target mode can hold the
9768 resulting value. */
9769 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9770 return build_real (type, trunc_result);
9771 }
9772 }
9773 }
9774 }
9775
9776 return NULL_TREE;
9777 }
9778
9779 /* Fold a call to builtin modf. */
9780
9781 static tree
9782 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9783 {
9784 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9785 return NULL_TREE;
9786
9787 STRIP_NOPS (arg0);
9788
9789 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9790 return NULL_TREE;
9791
9792 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9793
9794 /* Proceed if a valid pointer type was passed in. */
9795 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9796 {
9797 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9798 REAL_VALUE_TYPE trunc, frac;
9799
9800 switch (value->cl)
9801 {
9802 case rvc_nan:
9803 case rvc_zero:
9804 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9805 trunc = frac = *value;
9806 break;
9807 case rvc_inf:
9808 /* For +-Inf, return (*arg1 = arg0, +-0). */
9809 frac = dconst0;
9810 frac.sign = value->sign;
9811 trunc = *value;
9812 break;
9813 case rvc_normal:
9814 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9815 real_trunc (&trunc, VOIDmode, value);
9816 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9817 /* If the original number was negative and already
9818 integral, then the fractional part is -0.0. */
9819 if (value->sign && frac.cl == rvc_zero)
9820 frac.sign = value->sign;
9821 break;
9822 }
9823
9824 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9825 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9826 build_real (rettype, trunc));
9827 TREE_SIDE_EFFECTS (arg1) = 1;
9828 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9829 build_real (rettype, frac));
9830 }
9831
9832 return NULL_TREE;
9833 }
9834
9835 /* Given a location LOC, an interclass builtin function decl FNDECL
9836 and its single argument ARG, return an folded expression computing
9837 the same, or NULL_TREE if we either couldn't or didn't want to fold
9838 (the latter happen if there's an RTL instruction available). */
9839
9840 static tree
9841 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9842 {
9843 enum machine_mode mode;
9844
9845 if (!validate_arg (arg, REAL_TYPE))
9846 return NULL_TREE;
9847
9848 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9849 return NULL_TREE;
9850
9851 mode = TYPE_MODE (TREE_TYPE (arg));
9852
9853 /* If there is no optab, try generic code. */
9854 switch (DECL_FUNCTION_CODE (fndecl))
9855 {
9856 tree result;
9857
9858 CASE_FLT_FN (BUILT_IN_ISINF):
9859 {
9860 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9861 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9862 tree const type = TREE_TYPE (arg);
9863 REAL_VALUE_TYPE r;
9864 char buf[128];
9865
9866 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9867 real_from_string (&r, buf);
9868 result = build_call_expr (isgr_fn, 2,
9869 fold_build1_loc (loc, ABS_EXPR, type, arg),
9870 build_real (type, r));
9871 return result;
9872 }
9873 CASE_FLT_FN (BUILT_IN_FINITE):
9874 case BUILT_IN_ISFINITE:
9875 {
9876 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9877 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9878 tree const type = TREE_TYPE (arg);
9879 REAL_VALUE_TYPE r;
9880 char buf[128];
9881
9882 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9883 real_from_string (&r, buf);
9884 result = build_call_expr (isle_fn, 2,
9885 fold_build1_loc (loc, ABS_EXPR, type, arg),
9886 build_real (type, r));
9887 /*result = fold_build2_loc (loc, UNGT_EXPR,
9888 TREE_TYPE (TREE_TYPE (fndecl)),
9889 fold_build1_loc (loc, ABS_EXPR, type, arg),
9890 build_real (type, r));
9891 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9892 TREE_TYPE (TREE_TYPE (fndecl)),
9893 result);*/
9894 return result;
9895 }
9896 case BUILT_IN_ISNORMAL:
9897 {
9898 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9899 islessequal(fabs(x),DBL_MAX). */
9900 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9901 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9902 tree const type = TREE_TYPE (arg);
9903 REAL_VALUE_TYPE rmax, rmin;
9904 char buf[128];
9905
9906 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9907 real_from_string (&rmax, buf);
9908 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9909 real_from_string (&rmin, buf);
9910 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9911 result = build_call_expr (isle_fn, 2, arg,
9912 build_real (type, rmax));
9913 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9914 build_call_expr (isge_fn, 2, arg,
9915 build_real (type, rmin)));
9916 return result;
9917 }
9918 default:
9919 break;
9920 }
9921
9922 return NULL_TREE;
9923 }
9924
9925 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9926 ARG is the argument for the call. */
9927
9928 static tree
9929 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9930 {
9931 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9932 REAL_VALUE_TYPE r;
9933
9934 if (!validate_arg (arg, REAL_TYPE))
9935 return NULL_TREE;
9936
9937 switch (builtin_index)
9938 {
9939 case BUILT_IN_ISINF:
9940 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9941 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9942
9943 if (TREE_CODE (arg) == REAL_CST)
9944 {
9945 r = TREE_REAL_CST (arg);
9946 if (real_isinf (&r))
9947 return real_compare (GT_EXPR, &r, &dconst0)
9948 ? integer_one_node : integer_minus_one_node;
9949 else
9950 return integer_zero_node;
9951 }
9952
9953 return NULL_TREE;
9954
9955 case BUILT_IN_ISINF_SIGN:
9956 {
9957 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9958 /* In a boolean context, GCC will fold the inner COND_EXPR to
9959 1. So e.g. "if (isinf_sign(x))" would be folded to just
9960 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9961 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9962 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9963 tree tmp = NULL_TREE;
9964
9965 arg = builtin_save_expr (arg);
9966
9967 if (signbit_fn && isinf_fn)
9968 {
9969 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9970 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9971
9972 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9973 signbit_call, integer_zero_node);
9974 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9975 isinf_call, integer_zero_node);
9976
9977 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9978 integer_minus_one_node, integer_one_node);
9979 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9980 isinf_call, tmp,
9981 integer_zero_node);
9982 }
9983
9984 return tmp;
9985 }
9986
9987 case BUILT_IN_ISFINITE:
9988 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9989 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9990 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9991
9992 if (TREE_CODE (arg) == REAL_CST)
9993 {
9994 r = TREE_REAL_CST (arg);
9995 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9996 }
9997
9998 return NULL_TREE;
9999
10000 case BUILT_IN_ISNAN:
10001 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10002 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10003
10004 if (TREE_CODE (arg) == REAL_CST)
10005 {
10006 r = TREE_REAL_CST (arg);
10007 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10008 }
10009
10010 arg = builtin_save_expr (arg);
10011 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10012
10013 default:
10014 gcc_unreachable ();
10015 }
10016 }
10017
10018 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10019 This builtin will generate code to return the appropriate floating
10020 point classification depending on the value of the floating point
10021 number passed in. The possible return values must be supplied as
10022 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10023 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10024 one floating point argument which is "type generic". */
10025
10026 static tree
10027 fold_builtin_fpclassify (location_t loc, tree exp)
10028 {
10029 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10030 arg, type, res, tmp;
10031 enum machine_mode mode;
10032 REAL_VALUE_TYPE r;
10033 char buf[128];
10034
10035 /* Verify the required arguments in the original call. */
10036 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10037 INTEGER_TYPE, INTEGER_TYPE,
10038 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10039 return NULL_TREE;
10040
10041 fp_nan = CALL_EXPR_ARG (exp, 0);
10042 fp_infinite = CALL_EXPR_ARG (exp, 1);
10043 fp_normal = CALL_EXPR_ARG (exp, 2);
10044 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10045 fp_zero = CALL_EXPR_ARG (exp, 4);
10046 arg = CALL_EXPR_ARG (exp, 5);
10047 type = TREE_TYPE (arg);
10048 mode = TYPE_MODE (type);
10049 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10050
10051 /* fpclassify(x) ->
10052 isnan(x) ? FP_NAN :
10053 (fabs(x) == Inf ? FP_INFINITE :
10054 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10055 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10056
10057 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10058 build_real (type, dconst0));
10059 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10060 tmp, fp_zero, fp_subnormal);
10061
10062 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10063 real_from_string (&r, buf);
10064 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10065 arg, build_real (type, r));
10066 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10067
10068 if (HONOR_INFINITIES (mode))
10069 {
10070 real_inf (&r);
10071 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10072 build_real (type, r));
10073 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10074 fp_infinite, res);
10075 }
10076
10077 if (HONOR_NANS (mode))
10078 {
10079 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10080 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10081 }
10082
10083 return res;
10084 }
10085
10086 /* Fold a call to an unordered comparison function such as
10087 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10088 being called and ARG0 and ARG1 are the arguments for the call.
10089 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10090 the opposite of the desired result. UNORDERED_CODE is used
10091 for modes that can hold NaNs and ORDERED_CODE is used for
10092 the rest. */
10093
10094 static tree
10095 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10096 enum tree_code unordered_code,
10097 enum tree_code ordered_code)
10098 {
10099 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10100 enum tree_code code;
10101 tree type0, type1;
10102 enum tree_code code0, code1;
10103 tree cmp_type = NULL_TREE;
10104
10105 type0 = TREE_TYPE (arg0);
10106 type1 = TREE_TYPE (arg1);
10107
10108 code0 = TREE_CODE (type0);
10109 code1 = TREE_CODE (type1);
10110
10111 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10112 /* Choose the wider of two real types. */
10113 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10114 ? type0 : type1;
10115 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10116 cmp_type = type0;
10117 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10118 cmp_type = type1;
10119
10120 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10121 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10122
10123 if (unordered_code == UNORDERED_EXPR)
10124 {
10125 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10126 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10127 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10128 }
10129
10130 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10131 : ordered_code;
10132 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10133 fold_build2_loc (loc, code, type, arg0, arg1));
10134 }
10135
10136 /* Fold a call to built-in function FNDECL with 0 arguments.
10137 IGNORE is true if the result of the function call is ignored. This
10138 function returns NULL_TREE if no simplification was possible. */
10139
10140 static tree
10141 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10142 {
10143 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10144 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10145 switch (fcode)
10146 {
10147 CASE_FLT_FN (BUILT_IN_INF):
10148 case BUILT_IN_INFD32:
10149 case BUILT_IN_INFD64:
10150 case BUILT_IN_INFD128:
10151 return fold_builtin_inf (loc, type, true);
10152
10153 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10154 return fold_builtin_inf (loc, type, false);
10155
10156 case BUILT_IN_CLASSIFY_TYPE:
10157 return fold_builtin_classify_type (NULL_TREE);
10158
10159 case BUILT_IN_UNREACHABLE:
10160 if (flag_sanitize & SANITIZE_UNREACHABLE
10161 && (current_function_decl == NULL
10162 || !lookup_attribute ("no_sanitize_undefined",
10163 DECL_ATTRIBUTES (current_function_decl))))
10164 return ubsan_instrument_unreachable (loc);
10165 break;
10166
10167 default:
10168 break;
10169 }
10170 return NULL_TREE;
10171 }
10172
10173 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10174 IGNORE is true if the result of the function call is ignored. This
10175 function returns NULL_TREE if no simplification was possible. */
10176
10177 static tree
10178 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10179 {
10180 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10181 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10182 switch (fcode)
10183 {
10184 case BUILT_IN_CONSTANT_P:
10185 {
10186 tree val = fold_builtin_constant_p (arg0);
10187
10188 /* Gimplification will pull the CALL_EXPR for the builtin out of
10189 an if condition. When not optimizing, we'll not CSE it back.
10190 To avoid link error types of regressions, return false now. */
10191 if (!val && !optimize)
10192 val = integer_zero_node;
10193
10194 return val;
10195 }
10196
10197 case BUILT_IN_CLASSIFY_TYPE:
10198 return fold_builtin_classify_type (arg0);
10199
10200 case BUILT_IN_STRLEN:
10201 return fold_builtin_strlen (loc, type, arg0);
10202
10203 CASE_FLT_FN (BUILT_IN_FABS):
10204 case BUILT_IN_FABSD32:
10205 case BUILT_IN_FABSD64:
10206 case BUILT_IN_FABSD128:
10207 return fold_builtin_fabs (loc, arg0, type);
10208
10209 case BUILT_IN_ABS:
10210 case BUILT_IN_LABS:
10211 case BUILT_IN_LLABS:
10212 case BUILT_IN_IMAXABS:
10213 return fold_builtin_abs (loc, arg0, type);
10214
10215 CASE_FLT_FN (BUILT_IN_CONJ):
10216 if (validate_arg (arg0, COMPLEX_TYPE)
10217 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10218 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10219 break;
10220
10221 CASE_FLT_FN (BUILT_IN_CREAL):
10222 if (validate_arg (arg0, COMPLEX_TYPE)
10223 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10224 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10225 break;
10226
10227 CASE_FLT_FN (BUILT_IN_CIMAG):
10228 if (validate_arg (arg0, COMPLEX_TYPE)
10229 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10230 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10231 break;
10232
10233 CASE_FLT_FN (BUILT_IN_CCOS):
10234 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10235
10236 CASE_FLT_FN (BUILT_IN_CCOSH):
10237 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10238
10239 CASE_FLT_FN (BUILT_IN_CPROJ):
10240 return fold_builtin_cproj (loc, arg0, type);
10241
10242 CASE_FLT_FN (BUILT_IN_CSIN):
10243 if (validate_arg (arg0, COMPLEX_TYPE)
10244 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10245 return do_mpc_arg1 (arg0, type, mpc_sin);
10246 break;
10247
10248 CASE_FLT_FN (BUILT_IN_CSINH):
10249 if (validate_arg (arg0, COMPLEX_TYPE)
10250 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10251 return do_mpc_arg1 (arg0, type, mpc_sinh);
10252 break;
10253
10254 CASE_FLT_FN (BUILT_IN_CTAN):
10255 if (validate_arg (arg0, COMPLEX_TYPE)
10256 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10257 return do_mpc_arg1 (arg0, type, mpc_tan);
10258 break;
10259
10260 CASE_FLT_FN (BUILT_IN_CTANH):
10261 if (validate_arg (arg0, COMPLEX_TYPE)
10262 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10263 return do_mpc_arg1 (arg0, type, mpc_tanh);
10264 break;
10265
10266 CASE_FLT_FN (BUILT_IN_CLOG):
10267 if (validate_arg (arg0, COMPLEX_TYPE)
10268 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10269 return do_mpc_arg1 (arg0, type, mpc_log);
10270 break;
10271
10272 CASE_FLT_FN (BUILT_IN_CSQRT):
10273 if (validate_arg (arg0, COMPLEX_TYPE)
10274 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10275 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10276 break;
10277
10278 CASE_FLT_FN (BUILT_IN_CASIN):
10279 if (validate_arg (arg0, COMPLEX_TYPE)
10280 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10281 return do_mpc_arg1 (arg0, type, mpc_asin);
10282 break;
10283
10284 CASE_FLT_FN (BUILT_IN_CACOS):
10285 if (validate_arg (arg0, COMPLEX_TYPE)
10286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10287 return do_mpc_arg1 (arg0, type, mpc_acos);
10288 break;
10289
10290 CASE_FLT_FN (BUILT_IN_CATAN):
10291 if (validate_arg (arg0, COMPLEX_TYPE)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10293 return do_mpc_arg1 (arg0, type, mpc_atan);
10294 break;
10295
10296 CASE_FLT_FN (BUILT_IN_CASINH):
10297 if (validate_arg (arg0, COMPLEX_TYPE)
10298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10299 return do_mpc_arg1 (arg0, type, mpc_asinh);
10300 break;
10301
10302 CASE_FLT_FN (BUILT_IN_CACOSH):
10303 if (validate_arg (arg0, COMPLEX_TYPE)
10304 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10305 return do_mpc_arg1 (arg0, type, mpc_acosh);
10306 break;
10307
10308 CASE_FLT_FN (BUILT_IN_CATANH):
10309 if (validate_arg (arg0, COMPLEX_TYPE)
10310 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10311 return do_mpc_arg1 (arg0, type, mpc_atanh);
10312 break;
10313
10314 CASE_FLT_FN (BUILT_IN_CABS):
10315 return fold_builtin_cabs (loc, arg0, type, fndecl);
10316
10317 CASE_FLT_FN (BUILT_IN_CARG):
10318 return fold_builtin_carg (loc, arg0, type);
10319
10320 CASE_FLT_FN (BUILT_IN_SQRT):
10321 return fold_builtin_sqrt (loc, arg0, type);
10322
10323 CASE_FLT_FN (BUILT_IN_CBRT):
10324 return fold_builtin_cbrt (loc, arg0, type);
10325
10326 CASE_FLT_FN (BUILT_IN_ASIN):
10327 if (validate_arg (arg0, REAL_TYPE))
10328 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10329 &dconstm1, &dconst1, true);
10330 break;
10331
10332 CASE_FLT_FN (BUILT_IN_ACOS):
10333 if (validate_arg (arg0, REAL_TYPE))
10334 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10335 &dconstm1, &dconst1, true);
10336 break;
10337
10338 CASE_FLT_FN (BUILT_IN_ATAN):
10339 if (validate_arg (arg0, REAL_TYPE))
10340 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10341 break;
10342
10343 CASE_FLT_FN (BUILT_IN_ASINH):
10344 if (validate_arg (arg0, REAL_TYPE))
10345 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10346 break;
10347
10348 CASE_FLT_FN (BUILT_IN_ACOSH):
10349 if (validate_arg (arg0, REAL_TYPE))
10350 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10351 &dconst1, NULL, true);
10352 break;
10353
10354 CASE_FLT_FN (BUILT_IN_ATANH):
10355 if (validate_arg (arg0, REAL_TYPE))
10356 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10357 &dconstm1, &dconst1, false);
10358 break;
10359
10360 CASE_FLT_FN (BUILT_IN_SIN):
10361 if (validate_arg (arg0, REAL_TYPE))
10362 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10363 break;
10364
10365 CASE_FLT_FN (BUILT_IN_COS):
10366 return fold_builtin_cos (loc, arg0, type, fndecl);
10367
10368 CASE_FLT_FN (BUILT_IN_TAN):
10369 return fold_builtin_tan (arg0, type);
10370
10371 CASE_FLT_FN (BUILT_IN_CEXP):
10372 return fold_builtin_cexp (loc, arg0, type);
10373
10374 CASE_FLT_FN (BUILT_IN_CEXPI):
10375 if (validate_arg (arg0, REAL_TYPE))
10376 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10377 break;
10378
10379 CASE_FLT_FN (BUILT_IN_SINH):
10380 if (validate_arg (arg0, REAL_TYPE))
10381 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10382 break;
10383
10384 CASE_FLT_FN (BUILT_IN_COSH):
10385 return fold_builtin_cosh (loc, arg0, type, fndecl);
10386
10387 CASE_FLT_FN (BUILT_IN_TANH):
10388 if (validate_arg (arg0, REAL_TYPE))
10389 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10390 break;
10391
10392 CASE_FLT_FN (BUILT_IN_ERF):
10393 if (validate_arg (arg0, REAL_TYPE))
10394 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10395 break;
10396
10397 CASE_FLT_FN (BUILT_IN_ERFC):
10398 if (validate_arg (arg0, REAL_TYPE))
10399 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10400 break;
10401
10402 CASE_FLT_FN (BUILT_IN_TGAMMA):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10405 break;
10406
10407 CASE_FLT_FN (BUILT_IN_EXP):
10408 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10409
10410 CASE_FLT_FN (BUILT_IN_EXP2):
10411 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10412
10413 CASE_FLT_FN (BUILT_IN_EXP10):
10414 CASE_FLT_FN (BUILT_IN_POW10):
10415 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10416
10417 CASE_FLT_FN (BUILT_IN_EXPM1):
10418 if (validate_arg (arg0, REAL_TYPE))
10419 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10420 break;
10421
10422 CASE_FLT_FN (BUILT_IN_LOG):
10423 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10424
10425 CASE_FLT_FN (BUILT_IN_LOG2):
10426 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10427
10428 CASE_FLT_FN (BUILT_IN_LOG10):
10429 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10430
10431 CASE_FLT_FN (BUILT_IN_LOG1P):
10432 if (validate_arg (arg0, REAL_TYPE))
10433 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10434 &dconstm1, NULL, false);
10435 break;
10436
10437 CASE_FLT_FN (BUILT_IN_J0):
10438 if (validate_arg (arg0, REAL_TYPE))
10439 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10440 NULL, NULL, 0);
10441 break;
10442
10443 CASE_FLT_FN (BUILT_IN_J1):
10444 if (validate_arg (arg0, REAL_TYPE))
10445 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10446 NULL, NULL, 0);
10447 break;
10448
10449 CASE_FLT_FN (BUILT_IN_Y0):
10450 if (validate_arg (arg0, REAL_TYPE))
10451 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10452 &dconst0, NULL, false);
10453 break;
10454
10455 CASE_FLT_FN (BUILT_IN_Y1):
10456 if (validate_arg (arg0, REAL_TYPE))
10457 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10458 &dconst0, NULL, false);
10459 break;
10460
10461 CASE_FLT_FN (BUILT_IN_NAN):
10462 case BUILT_IN_NAND32:
10463 case BUILT_IN_NAND64:
10464 case BUILT_IN_NAND128:
10465 return fold_builtin_nan (arg0, type, true);
10466
10467 CASE_FLT_FN (BUILT_IN_NANS):
10468 return fold_builtin_nan (arg0, type, false);
10469
10470 CASE_FLT_FN (BUILT_IN_FLOOR):
10471 return fold_builtin_floor (loc, fndecl, arg0);
10472
10473 CASE_FLT_FN (BUILT_IN_CEIL):
10474 return fold_builtin_ceil (loc, fndecl, arg0);
10475
10476 CASE_FLT_FN (BUILT_IN_TRUNC):
10477 return fold_builtin_trunc (loc, fndecl, arg0);
10478
10479 CASE_FLT_FN (BUILT_IN_ROUND):
10480 return fold_builtin_round (loc, fndecl, arg0);
10481
10482 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10483 CASE_FLT_FN (BUILT_IN_RINT):
10484 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10485
10486 CASE_FLT_FN (BUILT_IN_ICEIL):
10487 CASE_FLT_FN (BUILT_IN_LCEIL):
10488 CASE_FLT_FN (BUILT_IN_LLCEIL):
10489 CASE_FLT_FN (BUILT_IN_LFLOOR):
10490 CASE_FLT_FN (BUILT_IN_IFLOOR):
10491 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10492 CASE_FLT_FN (BUILT_IN_IROUND):
10493 CASE_FLT_FN (BUILT_IN_LROUND):
10494 CASE_FLT_FN (BUILT_IN_LLROUND):
10495 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10496
10497 CASE_FLT_FN (BUILT_IN_IRINT):
10498 CASE_FLT_FN (BUILT_IN_LRINT):
10499 CASE_FLT_FN (BUILT_IN_LLRINT):
10500 return fold_fixed_mathfn (loc, fndecl, arg0);
10501
10502 case BUILT_IN_BSWAP16:
10503 case BUILT_IN_BSWAP32:
10504 case BUILT_IN_BSWAP64:
10505 return fold_builtin_bswap (fndecl, arg0);
10506
10507 CASE_INT_FN (BUILT_IN_FFS):
10508 CASE_INT_FN (BUILT_IN_CLZ):
10509 CASE_INT_FN (BUILT_IN_CTZ):
10510 CASE_INT_FN (BUILT_IN_CLRSB):
10511 CASE_INT_FN (BUILT_IN_POPCOUNT):
10512 CASE_INT_FN (BUILT_IN_PARITY):
10513 return fold_builtin_bitop (fndecl, arg0);
10514
10515 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10516 return fold_builtin_signbit (loc, arg0, type);
10517
10518 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10519 return fold_builtin_significand (loc, arg0, type);
10520
10521 CASE_FLT_FN (BUILT_IN_ILOGB):
10522 CASE_FLT_FN (BUILT_IN_LOGB):
10523 return fold_builtin_logb (loc, arg0, type);
10524
10525 case BUILT_IN_ISASCII:
10526 return fold_builtin_isascii (loc, arg0);
10527
10528 case BUILT_IN_TOASCII:
10529 return fold_builtin_toascii (loc, arg0);
10530
10531 case BUILT_IN_ISDIGIT:
10532 return fold_builtin_isdigit (loc, arg0);
10533
10534 CASE_FLT_FN (BUILT_IN_FINITE):
10535 case BUILT_IN_FINITED32:
10536 case BUILT_IN_FINITED64:
10537 case BUILT_IN_FINITED128:
10538 case BUILT_IN_ISFINITE:
10539 {
10540 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10541 if (ret)
10542 return ret;
10543 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10544 }
10545
10546 CASE_FLT_FN (BUILT_IN_ISINF):
10547 case BUILT_IN_ISINFD32:
10548 case BUILT_IN_ISINFD64:
10549 case BUILT_IN_ISINFD128:
10550 {
10551 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10552 if (ret)
10553 return ret;
10554 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10555 }
10556
10557 case BUILT_IN_ISNORMAL:
10558 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10559
10560 case BUILT_IN_ISINF_SIGN:
10561 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10562
10563 CASE_FLT_FN (BUILT_IN_ISNAN):
10564 case BUILT_IN_ISNAND32:
10565 case BUILT_IN_ISNAND64:
10566 case BUILT_IN_ISNAND128:
10567 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10568
10569 case BUILT_IN_PRINTF:
10570 case BUILT_IN_PRINTF_UNLOCKED:
10571 case BUILT_IN_VPRINTF:
10572 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10573
10574 case BUILT_IN_FREE:
10575 if (integer_zerop (arg0))
10576 return build_empty_stmt (loc);
10577 break;
10578
10579 default:
10580 break;
10581 }
10582
10583 return NULL_TREE;
10584
10585 }
10586
10587 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10588 IGNORE is true if the result of the function call is ignored. This
10589 function returns NULL_TREE if no simplification was possible. */
10590
10591 static tree
10592 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10593 {
10594 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10595 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10596
10597 switch (fcode)
10598 {
10599 CASE_FLT_FN (BUILT_IN_JN):
10600 if (validate_arg (arg0, INTEGER_TYPE)
10601 && validate_arg (arg1, REAL_TYPE))
10602 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10603 break;
10604
10605 CASE_FLT_FN (BUILT_IN_YN):
10606 if (validate_arg (arg0, INTEGER_TYPE)
10607 && validate_arg (arg1, REAL_TYPE))
10608 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10609 &dconst0, false);
10610 break;
10611
10612 CASE_FLT_FN (BUILT_IN_DREM):
10613 CASE_FLT_FN (BUILT_IN_REMAINDER):
10614 if (validate_arg (arg0, REAL_TYPE)
10615 && validate_arg (arg1, REAL_TYPE))
10616 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10617 break;
10618
10619 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10620 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10621 if (validate_arg (arg0, REAL_TYPE)
10622 && validate_arg (arg1, POINTER_TYPE))
10623 return do_mpfr_lgamma_r (arg0, arg1, type);
10624 break;
10625
10626 CASE_FLT_FN (BUILT_IN_ATAN2):
10627 if (validate_arg (arg0, REAL_TYPE)
10628 && validate_arg (arg1, REAL_TYPE))
10629 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10630 break;
10631
10632 CASE_FLT_FN (BUILT_IN_FDIM):
10633 if (validate_arg (arg0, REAL_TYPE)
10634 && validate_arg (arg1, REAL_TYPE))
10635 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10636 break;
10637
10638 CASE_FLT_FN (BUILT_IN_HYPOT):
10639 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10640
10641 CASE_FLT_FN (BUILT_IN_CPOW):
10642 if (validate_arg (arg0, COMPLEX_TYPE)
10643 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10644 && validate_arg (arg1, COMPLEX_TYPE)
10645 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10646 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10647 break;
10648
10649 CASE_FLT_FN (BUILT_IN_LDEXP):
10650 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10651 CASE_FLT_FN (BUILT_IN_SCALBN):
10652 CASE_FLT_FN (BUILT_IN_SCALBLN):
10653 return fold_builtin_load_exponent (loc, arg0, arg1,
10654 type, /*ldexp=*/false);
10655
10656 CASE_FLT_FN (BUILT_IN_FREXP):
10657 return fold_builtin_frexp (loc, arg0, arg1, type);
10658
10659 CASE_FLT_FN (BUILT_IN_MODF):
10660 return fold_builtin_modf (loc, arg0, arg1, type);
10661
10662 case BUILT_IN_BZERO:
10663 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10664
10665 case BUILT_IN_FPUTS:
10666 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10667
10668 case BUILT_IN_FPUTS_UNLOCKED:
10669 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10670
10671 case BUILT_IN_STRSTR:
10672 return fold_builtin_strstr (loc, arg0, arg1, type);
10673
10674 case BUILT_IN_STRCAT:
10675 return fold_builtin_strcat (loc, arg0, arg1, NULL_TREE);
10676
10677 case BUILT_IN_STRSPN:
10678 return fold_builtin_strspn (loc, arg0, arg1);
10679
10680 case BUILT_IN_STRCSPN:
10681 return fold_builtin_strcspn (loc, arg0, arg1);
10682
10683 case BUILT_IN_STRCHR:
10684 case BUILT_IN_INDEX:
10685 return fold_builtin_strchr (loc, arg0, arg1, type);
10686
10687 case BUILT_IN_STRRCHR:
10688 case BUILT_IN_RINDEX:
10689 return fold_builtin_strrchr (loc, arg0, arg1, type);
10690
10691 case BUILT_IN_STRCPY:
10692 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10693
10694 case BUILT_IN_STPCPY:
10695 if (ignore)
10696 {
10697 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10698 if (!fn)
10699 break;
10700
10701 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10702 }
10703 else
10704 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10705 break;
10706
10707 case BUILT_IN_STRCMP:
10708 return fold_builtin_strcmp (loc, arg0, arg1);
10709
10710 case BUILT_IN_STRPBRK:
10711 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10712
10713 case BUILT_IN_EXPECT:
10714 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10715
10716 CASE_FLT_FN (BUILT_IN_POW):
10717 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10718
10719 CASE_FLT_FN (BUILT_IN_POWI):
10720 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10721
10722 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10723 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10724
10725 CASE_FLT_FN (BUILT_IN_FMIN):
10726 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10727
10728 CASE_FLT_FN (BUILT_IN_FMAX):
10729 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10730
10731 case BUILT_IN_ISGREATER:
10732 return fold_builtin_unordered_cmp (loc, fndecl,
10733 arg0, arg1, UNLE_EXPR, LE_EXPR);
10734 case BUILT_IN_ISGREATEREQUAL:
10735 return fold_builtin_unordered_cmp (loc, fndecl,
10736 arg0, arg1, UNLT_EXPR, LT_EXPR);
10737 case BUILT_IN_ISLESS:
10738 return fold_builtin_unordered_cmp (loc, fndecl,
10739 arg0, arg1, UNGE_EXPR, GE_EXPR);
10740 case BUILT_IN_ISLESSEQUAL:
10741 return fold_builtin_unordered_cmp (loc, fndecl,
10742 arg0, arg1, UNGT_EXPR, GT_EXPR);
10743 case BUILT_IN_ISLESSGREATER:
10744 return fold_builtin_unordered_cmp (loc, fndecl,
10745 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10746 case BUILT_IN_ISUNORDERED:
10747 return fold_builtin_unordered_cmp (loc, fndecl,
10748 arg0, arg1, UNORDERED_EXPR,
10749 NOP_EXPR);
10750
10751 /* We do the folding for va_start in the expander. */
10752 case BUILT_IN_VA_START:
10753 break;
10754
10755 case BUILT_IN_SPRINTF:
10756 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10757
10758 case BUILT_IN_OBJECT_SIZE:
10759 return fold_builtin_object_size (arg0, arg1);
10760
10761 case BUILT_IN_PRINTF:
10762 case BUILT_IN_PRINTF_UNLOCKED:
10763 case BUILT_IN_VPRINTF:
10764 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10765
10766 case BUILT_IN_PRINTF_CHK:
10767 case BUILT_IN_VPRINTF_CHK:
10768 if (!validate_arg (arg0, INTEGER_TYPE)
10769 || TREE_SIDE_EFFECTS (arg0))
10770 return NULL_TREE;
10771 else
10772 return fold_builtin_printf (loc, fndecl,
10773 arg1, NULL_TREE, ignore, fcode);
10774 break;
10775
10776 case BUILT_IN_FPRINTF:
10777 case BUILT_IN_FPRINTF_UNLOCKED:
10778 case BUILT_IN_VFPRINTF:
10779 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10780 ignore, fcode);
10781
10782 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10783 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10784
10785 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10786 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10787
10788 default:
10789 break;
10790 }
10791 return NULL_TREE;
10792 }
10793
10794 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10795 and ARG2. IGNORE is true if the result of the function call is ignored.
10796 This function returns NULL_TREE if no simplification was possible. */
10797
10798 static tree
10799 fold_builtin_3 (location_t loc, tree fndecl,
10800 tree arg0, tree arg1, tree arg2, bool ignore)
10801 {
10802 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10803 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10804 switch (fcode)
10805 {
10806
10807 CASE_FLT_FN (BUILT_IN_SINCOS):
10808 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10809
10810 CASE_FLT_FN (BUILT_IN_FMA):
10811 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10812 break;
10813
10814 CASE_FLT_FN (BUILT_IN_REMQUO):
10815 if (validate_arg (arg0, REAL_TYPE)
10816 && validate_arg (arg1, REAL_TYPE)
10817 && validate_arg (arg2, POINTER_TYPE))
10818 return do_mpfr_remquo (arg0, arg1, arg2);
10819 break;
10820
10821 case BUILT_IN_MEMSET:
10822 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10823
10824 case BUILT_IN_BCOPY:
10825 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10826 void_type_node, true, /*endp=*/3);
10827
10828 case BUILT_IN_MEMCPY:
10829 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10830 type, ignore, /*endp=*/0);
10831
10832 case BUILT_IN_MEMPCPY:
10833 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10834 type, ignore, /*endp=*/1);
10835
10836 case BUILT_IN_MEMMOVE:
10837 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10838 type, ignore, /*endp=*/3);
10839
10840 case BUILT_IN_STRNCAT:
10841 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10842
10843 case BUILT_IN_STRNCPY:
10844 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10845
10846 case BUILT_IN_STRNCMP:
10847 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10848
10849 case BUILT_IN_MEMCHR:
10850 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10851
10852 case BUILT_IN_BCMP:
10853 case BUILT_IN_MEMCMP:
10854 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10855
10856 case BUILT_IN_SPRINTF:
10857 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10858
10859 case BUILT_IN_SNPRINTF:
10860 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10861
10862 case BUILT_IN_STRCPY_CHK:
10863 case BUILT_IN_STPCPY_CHK:
10864 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10865 ignore, fcode);
10866
10867 case BUILT_IN_STRCAT_CHK:
10868 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10869
10870 case BUILT_IN_PRINTF_CHK:
10871 case BUILT_IN_VPRINTF_CHK:
10872 if (!validate_arg (arg0, INTEGER_TYPE)
10873 || TREE_SIDE_EFFECTS (arg0))
10874 return NULL_TREE;
10875 else
10876 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10877 break;
10878
10879 case BUILT_IN_FPRINTF:
10880 case BUILT_IN_FPRINTF_UNLOCKED:
10881 case BUILT_IN_VFPRINTF:
10882 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10883 ignore, fcode);
10884
10885 case BUILT_IN_FPRINTF_CHK:
10886 case BUILT_IN_VFPRINTF_CHK:
10887 if (!validate_arg (arg1, INTEGER_TYPE)
10888 || TREE_SIDE_EFFECTS (arg1))
10889 return NULL_TREE;
10890 else
10891 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10892 ignore, fcode);
10893
10894 case BUILT_IN_EXPECT:
10895 return fold_builtin_expect (loc, arg0, arg1, arg2);
10896
10897 default:
10898 break;
10899 }
10900 return NULL_TREE;
10901 }
10902
10903 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10904 ARG2, and ARG3. IGNORE is true if the result of the function call is
10905 ignored. This function returns NULL_TREE if no simplification was
10906 possible. */
10907
10908 static tree
10909 fold_builtin_4 (location_t loc, tree fndecl,
10910 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10911 {
10912 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10913
10914 switch (fcode)
10915 {
10916 case BUILT_IN_MEMCPY_CHK:
10917 case BUILT_IN_MEMPCPY_CHK:
10918 case BUILT_IN_MEMMOVE_CHK:
10919 case BUILT_IN_MEMSET_CHK:
10920 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10921 NULL_TREE, ignore,
10922 DECL_FUNCTION_CODE (fndecl));
10923
10924 case BUILT_IN_STRNCPY_CHK:
10925 case BUILT_IN_STPNCPY_CHK:
10926 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10927 ignore, fcode);
10928
10929 case BUILT_IN_STRNCAT_CHK:
10930 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10931
10932 case BUILT_IN_SNPRINTF:
10933 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10934
10935 case BUILT_IN_FPRINTF_CHK:
10936 case BUILT_IN_VFPRINTF_CHK:
10937 if (!validate_arg (arg1, INTEGER_TYPE)
10938 || TREE_SIDE_EFFECTS (arg1))
10939 return NULL_TREE;
10940 else
10941 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10942 ignore, fcode);
10943 break;
10944
10945 default:
10946 break;
10947 }
10948 return NULL_TREE;
10949 }
10950
10951 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10952 arguments, where NARGS <= 4. IGNORE is true if the result of the
10953 function call is ignored. This function returns NULL_TREE if no
10954 simplification was possible. Note that this only folds builtins with
10955 fixed argument patterns. Foldings that do varargs-to-varargs
10956 transformations, or that match calls with more than 4 arguments,
10957 need to be handled with fold_builtin_varargs instead. */
10958
10959 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10960
10961 static tree
10962 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10963 {
10964 tree ret = NULL_TREE;
10965
10966 switch (nargs)
10967 {
10968 case 0:
10969 ret = fold_builtin_0 (loc, fndecl, ignore);
10970 break;
10971 case 1:
10972 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10973 break;
10974 case 2:
10975 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10976 break;
10977 case 3:
10978 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10979 break;
10980 case 4:
10981 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10982 ignore);
10983 break;
10984 default:
10985 break;
10986 }
10987 if (ret)
10988 {
10989 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10990 SET_EXPR_LOCATION (ret, loc);
10991 TREE_NO_WARNING (ret) = 1;
10992 return ret;
10993 }
10994 return NULL_TREE;
10995 }
10996
10997 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10998 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10999 of arguments in ARGS to be omitted. OLDNARGS is the number of
11000 elements in ARGS. */
11001
11002 static tree
11003 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11004 int skip, tree fndecl, int n, va_list newargs)
11005 {
11006 int nargs = oldnargs - skip + n;
11007 tree *buffer;
11008
11009 if (n > 0)
11010 {
11011 int i, j;
11012
11013 buffer = XALLOCAVEC (tree, nargs);
11014 for (i = 0; i < n; i++)
11015 buffer[i] = va_arg (newargs, tree);
11016 for (j = skip; j < oldnargs; j++, i++)
11017 buffer[i] = args[j];
11018 }
11019 else
11020 buffer = args + skip;
11021
11022 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11023 }
11024
11025 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11026 list ARGS along with N new arguments specified as the "..."
11027 parameters. SKIP is the number of arguments in ARGS to be omitted.
11028 OLDNARGS is the number of elements in ARGS. */
11029
11030 static tree
11031 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11032 int skip, tree fndecl, int n, ...)
11033 {
11034 va_list ap;
11035 tree t;
11036
11037 va_start (ap, n);
11038 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11039 va_end (ap);
11040
11041 return t;
11042 }
11043
11044 /* Return true if FNDECL shouldn't be folded right now.
11045 If a built-in function has an inline attribute always_inline
11046 wrapper, defer folding it after always_inline functions have
11047 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11048 might not be performed. */
11049
11050 bool
11051 avoid_folding_inline_builtin (tree fndecl)
11052 {
11053 return (DECL_DECLARED_INLINE_P (fndecl)
11054 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11055 && cfun
11056 && !cfun->always_inline_functions_inlined
11057 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11058 }
11059
11060 /* A wrapper function for builtin folding that prevents warnings for
11061 "statement without effect" and the like, caused by removing the
11062 call node earlier than the warning is generated. */
11063
11064 tree
11065 fold_call_expr (location_t loc, tree exp, bool ignore)
11066 {
11067 tree ret = NULL_TREE;
11068 tree fndecl = get_callee_fndecl (exp);
11069 if (fndecl
11070 && TREE_CODE (fndecl) == FUNCTION_DECL
11071 && DECL_BUILT_IN (fndecl)
11072 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11073 yet. Defer folding until we see all the arguments
11074 (after inlining). */
11075 && !CALL_EXPR_VA_ARG_PACK (exp))
11076 {
11077 int nargs = call_expr_nargs (exp);
11078
11079 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11080 instead last argument is __builtin_va_arg_pack (). Defer folding
11081 even in that case, until arguments are finalized. */
11082 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11083 {
11084 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11085 if (fndecl2
11086 && TREE_CODE (fndecl2) == FUNCTION_DECL
11087 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11088 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11089 return NULL_TREE;
11090 }
11091
11092 if (avoid_folding_inline_builtin (fndecl))
11093 return NULL_TREE;
11094
11095 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11096 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11097 CALL_EXPR_ARGP (exp), ignore);
11098 else
11099 {
11100 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11101 {
11102 tree *args = CALL_EXPR_ARGP (exp);
11103 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11104 }
11105 if (!ret)
11106 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11107 if (ret)
11108 return ret;
11109 }
11110 }
11111 return NULL_TREE;
11112 }
11113
11114 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11115 N arguments are passed in the array ARGARRAY. */
11116
11117 tree
11118 fold_builtin_call_array (location_t loc, tree type,
11119 tree fn,
11120 int n,
11121 tree *argarray)
11122 {
11123 tree ret = NULL_TREE;
11124 tree exp;
11125
11126 if (TREE_CODE (fn) == ADDR_EXPR)
11127 {
11128 tree fndecl = TREE_OPERAND (fn, 0);
11129 if (TREE_CODE (fndecl) == FUNCTION_DECL
11130 && DECL_BUILT_IN (fndecl))
11131 {
11132 /* If last argument is __builtin_va_arg_pack (), arguments to this
11133 function are not finalized yet. Defer folding until they are. */
11134 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11135 {
11136 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11137 if (fndecl2
11138 && TREE_CODE (fndecl2) == FUNCTION_DECL
11139 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11140 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11141 return build_call_array_loc (loc, type, fn, n, argarray);
11142 }
11143 if (avoid_folding_inline_builtin (fndecl))
11144 return build_call_array_loc (loc, type, fn, n, argarray);
11145 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11146 {
11147 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11148 if (ret)
11149 return ret;
11150
11151 return build_call_array_loc (loc, type, fn, n, argarray);
11152 }
11153 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11154 {
11155 /* First try the transformations that don't require consing up
11156 an exp. */
11157 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11158 if (ret)
11159 return ret;
11160 }
11161
11162 /* If we got this far, we need to build an exp. */
11163 exp = build_call_array_loc (loc, type, fn, n, argarray);
11164 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11165 return ret ? ret : exp;
11166 }
11167 }
11168
11169 return build_call_array_loc (loc, type, fn, n, argarray);
11170 }
11171
11172 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11173 along with N new arguments specified as the "..." parameters. SKIP
11174 is the number of arguments in EXP to be omitted. This function is used
11175 to do varargs-to-varargs transformations. */
11176
11177 static tree
11178 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11179 {
11180 va_list ap;
11181 tree t;
11182
11183 va_start (ap, n);
11184 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11185 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11186 va_end (ap);
11187
11188 return t;
11189 }
11190
11191 /* Validate a single argument ARG against a tree code CODE representing
11192 a type. */
11193
11194 static bool
11195 validate_arg (const_tree arg, enum tree_code code)
11196 {
11197 if (!arg)
11198 return false;
11199 else if (code == POINTER_TYPE)
11200 return POINTER_TYPE_P (TREE_TYPE (arg));
11201 else if (code == INTEGER_TYPE)
11202 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11203 return code == TREE_CODE (TREE_TYPE (arg));
11204 }
11205
11206 /* This function validates the types of a function call argument list
11207 against a specified list of tree_codes. If the last specifier is a 0,
11208 that represents an ellipses, otherwise the last specifier must be a
11209 VOID_TYPE.
11210
11211 This is the GIMPLE version of validate_arglist. Eventually we want to
11212 completely convert builtins.c to work from GIMPLEs and the tree based
11213 validate_arglist will then be removed. */
11214
11215 bool
11216 validate_gimple_arglist (const_gimple call, ...)
11217 {
11218 enum tree_code code;
11219 bool res = 0;
11220 va_list ap;
11221 const_tree arg;
11222 size_t i;
11223
11224 va_start (ap, call);
11225 i = 0;
11226
11227 do
11228 {
11229 code = (enum tree_code) va_arg (ap, int);
11230 switch (code)
11231 {
11232 case 0:
11233 /* This signifies an ellipses, any further arguments are all ok. */
11234 res = true;
11235 goto end;
11236 case VOID_TYPE:
11237 /* This signifies an endlink, if no arguments remain, return
11238 true, otherwise return false. */
11239 res = (i == gimple_call_num_args (call));
11240 goto end;
11241 default:
11242 /* If no parameters remain or the parameter's code does not
11243 match the specified code, return false. Otherwise continue
11244 checking any remaining arguments. */
11245 arg = gimple_call_arg (call, i++);
11246 if (!validate_arg (arg, code))
11247 goto end;
11248 break;
11249 }
11250 }
11251 while (1);
11252
11253 /* We need gotos here since we can only have one VA_CLOSE in a
11254 function. */
11255 end: ;
11256 va_end (ap);
11257
11258 return res;
11259 }
11260
11261 /* Default target-specific builtin expander that does nothing. */
11262
11263 rtx
11264 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11265 rtx target ATTRIBUTE_UNUSED,
11266 rtx subtarget ATTRIBUTE_UNUSED,
11267 enum machine_mode mode ATTRIBUTE_UNUSED,
11268 int ignore ATTRIBUTE_UNUSED)
11269 {
11270 return NULL_RTX;
11271 }
11272
11273 /* Returns true is EXP represents data that would potentially reside
11274 in a readonly section. */
11275
11276 static bool
11277 readonly_data_expr (tree exp)
11278 {
11279 STRIP_NOPS (exp);
11280
11281 if (TREE_CODE (exp) != ADDR_EXPR)
11282 return false;
11283
11284 exp = get_base_address (TREE_OPERAND (exp, 0));
11285 if (!exp)
11286 return false;
11287
11288 /* Make sure we call decl_readonly_section only for trees it
11289 can handle (since it returns true for everything it doesn't
11290 understand). */
11291 if (TREE_CODE (exp) == STRING_CST
11292 || TREE_CODE (exp) == CONSTRUCTOR
11293 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11294 return decl_readonly_section (exp, 0);
11295 else
11296 return false;
11297 }
11298
11299 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11300 to the call, and TYPE is its return type.
11301
11302 Return NULL_TREE if no simplification was possible, otherwise return the
11303 simplified form of the call as a tree.
11304
11305 The simplified form may be a constant or other expression which
11306 computes the same value, but in a more efficient manner (including
11307 calls to other builtin functions).
11308
11309 The call may contain arguments which need to be evaluated, but
11310 which are not useful to determine the result of the call. In
11311 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11312 COMPOUND_EXPR will be an argument which must be evaluated.
11313 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11314 COMPOUND_EXPR in the chain will contain the tree for the simplified
11315 form of the builtin function call. */
11316
11317 static tree
11318 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11319 {
11320 if (!validate_arg (s1, POINTER_TYPE)
11321 || !validate_arg (s2, POINTER_TYPE))
11322 return NULL_TREE;
11323 else
11324 {
11325 tree fn;
11326 const char *p1, *p2;
11327
11328 p2 = c_getstr (s2);
11329 if (p2 == NULL)
11330 return NULL_TREE;
11331
11332 p1 = c_getstr (s1);
11333 if (p1 != NULL)
11334 {
11335 const char *r = strstr (p1, p2);
11336 tree tem;
11337
11338 if (r == NULL)
11339 return build_int_cst (TREE_TYPE (s1), 0);
11340
11341 /* Return an offset into the constant string argument. */
11342 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11343 return fold_convert_loc (loc, type, tem);
11344 }
11345
11346 /* The argument is const char *, and the result is char *, so we need
11347 a type conversion here to avoid a warning. */
11348 if (p2[0] == '\0')
11349 return fold_convert_loc (loc, type, s1);
11350
11351 if (p2[1] != '\0')
11352 return NULL_TREE;
11353
11354 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11355 if (!fn)
11356 return NULL_TREE;
11357
11358 /* New argument list transforming strstr(s1, s2) to
11359 strchr(s1, s2[0]). */
11360 return build_call_expr_loc (loc, fn, 2, s1,
11361 build_int_cst (integer_type_node, p2[0]));
11362 }
11363 }
11364
11365 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11366 the call, and TYPE is its return type.
11367
11368 Return NULL_TREE if no simplification was possible, otherwise return the
11369 simplified form of the call as a tree.
11370
11371 The simplified form may be a constant or other expression which
11372 computes the same value, but in a more efficient manner (including
11373 calls to other builtin functions).
11374
11375 The call may contain arguments which need to be evaluated, but
11376 which are not useful to determine the result of the call. In
11377 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11378 COMPOUND_EXPR will be an argument which must be evaluated.
11379 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11380 COMPOUND_EXPR in the chain will contain the tree for the simplified
11381 form of the builtin function call. */
11382
11383 static tree
11384 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11385 {
11386 if (!validate_arg (s1, POINTER_TYPE)
11387 || !validate_arg (s2, INTEGER_TYPE))
11388 return NULL_TREE;
11389 else
11390 {
11391 const char *p1;
11392
11393 if (TREE_CODE (s2) != INTEGER_CST)
11394 return NULL_TREE;
11395
11396 p1 = c_getstr (s1);
11397 if (p1 != NULL)
11398 {
11399 char c;
11400 const char *r;
11401 tree tem;
11402
11403 if (target_char_cast (s2, &c))
11404 return NULL_TREE;
11405
11406 r = strchr (p1, c);
11407
11408 if (r == NULL)
11409 return build_int_cst (TREE_TYPE (s1), 0);
11410
11411 /* Return an offset into the constant string argument. */
11412 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11413 return fold_convert_loc (loc, type, tem);
11414 }
11415 return NULL_TREE;
11416 }
11417 }
11418
11419 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11420 the call, and TYPE is its return type.
11421
11422 Return NULL_TREE if no simplification was possible, otherwise return the
11423 simplified form of the call as a tree.
11424
11425 The simplified form may be a constant or other expression which
11426 computes the same value, but in a more efficient manner (including
11427 calls to other builtin functions).
11428
11429 The call may contain arguments which need to be evaluated, but
11430 which are not useful to determine the result of the call. In
11431 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11432 COMPOUND_EXPR will be an argument which must be evaluated.
11433 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11434 COMPOUND_EXPR in the chain will contain the tree for the simplified
11435 form of the builtin function call. */
11436
11437 static tree
11438 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11439 {
11440 if (!validate_arg (s1, POINTER_TYPE)
11441 || !validate_arg (s2, INTEGER_TYPE))
11442 return NULL_TREE;
11443 else
11444 {
11445 tree fn;
11446 const char *p1;
11447
11448 if (TREE_CODE (s2) != INTEGER_CST)
11449 return NULL_TREE;
11450
11451 p1 = c_getstr (s1);
11452 if (p1 != NULL)
11453 {
11454 char c;
11455 const char *r;
11456 tree tem;
11457
11458 if (target_char_cast (s2, &c))
11459 return NULL_TREE;
11460
11461 r = strrchr (p1, c);
11462
11463 if (r == NULL)
11464 return build_int_cst (TREE_TYPE (s1), 0);
11465
11466 /* Return an offset into the constant string argument. */
11467 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11468 return fold_convert_loc (loc, type, tem);
11469 }
11470
11471 if (! integer_zerop (s2))
11472 return NULL_TREE;
11473
11474 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11475 if (!fn)
11476 return NULL_TREE;
11477
11478 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11479 return build_call_expr_loc (loc, fn, 2, s1, s2);
11480 }
11481 }
11482
11483 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11484 to the call, and TYPE is its return type.
11485
11486 Return NULL_TREE if no simplification was possible, otherwise return the
11487 simplified form of the call as a tree.
11488
11489 The simplified form may be a constant or other expression which
11490 computes the same value, but in a more efficient manner (including
11491 calls to other builtin functions).
11492
11493 The call may contain arguments which need to be evaluated, but
11494 which are not useful to determine the result of the call. In
11495 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11496 COMPOUND_EXPR will be an argument which must be evaluated.
11497 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11498 COMPOUND_EXPR in the chain will contain the tree for the simplified
11499 form of the builtin function call. */
11500
11501 static tree
11502 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11503 {
11504 if (!validate_arg (s1, POINTER_TYPE)
11505 || !validate_arg (s2, POINTER_TYPE))
11506 return NULL_TREE;
11507 else
11508 {
11509 tree fn;
11510 const char *p1, *p2;
11511
11512 p2 = c_getstr (s2);
11513 if (p2 == NULL)
11514 return NULL_TREE;
11515
11516 p1 = c_getstr (s1);
11517 if (p1 != NULL)
11518 {
11519 const char *r = strpbrk (p1, p2);
11520 tree tem;
11521
11522 if (r == NULL)
11523 return build_int_cst (TREE_TYPE (s1), 0);
11524
11525 /* Return an offset into the constant string argument. */
11526 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11527 return fold_convert_loc (loc, type, tem);
11528 }
11529
11530 if (p2[0] == '\0')
11531 /* strpbrk(x, "") == NULL.
11532 Evaluate and ignore s1 in case it had side-effects. */
11533 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11534
11535 if (p2[1] != '\0')
11536 return NULL_TREE; /* Really call strpbrk. */
11537
11538 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11539 if (!fn)
11540 return NULL_TREE;
11541
11542 /* New argument list transforming strpbrk(s1, s2) to
11543 strchr(s1, s2[0]). */
11544 return build_call_expr_loc (loc, fn, 2, s1,
11545 build_int_cst (integer_type_node, p2[0]));
11546 }
11547 }
11548
11549 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11550 to the call.
11551
11552 Return NULL_TREE if no simplification was possible, otherwise return the
11553 simplified form of the call as a tree.
11554
11555 The simplified form may be a constant or other expression which
11556 computes the same value, but in a more efficient manner (including
11557 calls to other builtin functions).
11558
11559 The call may contain arguments which need to be evaluated, but
11560 which are not useful to determine the result of the call. In
11561 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11562 COMPOUND_EXPR will be an argument which must be evaluated.
11563 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11564 COMPOUND_EXPR in the chain will contain the tree for the simplified
11565 form of the builtin function call. */
11566
11567 tree
11568 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src,
11569 tree len)
11570 {
11571 if (!validate_arg (dst, POINTER_TYPE)
11572 || !validate_arg (src, POINTER_TYPE))
11573 return NULL_TREE;
11574 else
11575 {
11576 const char *p = c_getstr (src);
11577
11578 /* If the string length is zero, return the dst parameter. */
11579 if (p && *p == '\0')
11580 return dst;
11581
11582 if (optimize_insn_for_speed_p ())
11583 {
11584 /* See if we can store by pieces into (dst + strlen(dst)). */
11585 tree newdst, call;
11586 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11587 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
11588
11589 if (!strlen_fn || !memcpy_fn)
11590 return NULL_TREE;
11591
11592 /* If the length of the source string isn't computable don't
11593 split strcat into strlen and memcpy. */
11594 if (! len)
11595 len = c_strlen (src, 1);
11596 if (! len || TREE_SIDE_EFFECTS (len))
11597 return NULL_TREE;
11598
11599 /* Stabilize the argument list. */
11600 dst = builtin_save_expr (dst);
11601
11602 /* Create strlen (dst). */
11603 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11604 /* Create (dst p+ strlen (dst)). */
11605
11606 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11607 newdst = builtin_save_expr (newdst);
11608
11609 len = fold_convert_loc (loc, size_type_node, len);
11610 len = size_binop_loc (loc, PLUS_EXPR, len,
11611 build_int_cst (size_type_node, 1));
11612
11613 call = build_call_expr_loc (loc, memcpy_fn, 3, newdst, src, len);
11614 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11615 }
11616 return NULL_TREE;
11617 }
11618 }
11619
11620 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11621 arguments to the call.
11622
11623 Return NULL_TREE if no simplification was possible, otherwise return the
11624 simplified form of the call as a tree.
11625
11626 The simplified form may be a constant or other expression which
11627 computes the same value, but in a more efficient manner (including
11628 calls to other builtin functions).
11629
11630 The call may contain arguments which need to be evaluated, but
11631 which are not useful to determine the result of the call. In
11632 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11633 COMPOUND_EXPR will be an argument which must be evaluated.
11634 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11635 COMPOUND_EXPR in the chain will contain the tree for the simplified
11636 form of the builtin function call. */
11637
11638 static tree
11639 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11640 {
11641 if (!validate_arg (dst, POINTER_TYPE)
11642 || !validate_arg (src, POINTER_TYPE)
11643 || !validate_arg (len, INTEGER_TYPE))
11644 return NULL_TREE;
11645 else
11646 {
11647 const char *p = c_getstr (src);
11648
11649 /* If the requested length is zero, or the src parameter string
11650 length is zero, return the dst parameter. */
11651 if (integer_zerop (len) || (p && *p == '\0'))
11652 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11653
11654 /* If the requested len is greater than or equal to the string
11655 length, call strcat. */
11656 if (TREE_CODE (len) == INTEGER_CST && p
11657 && compare_tree_int (len, strlen (p)) >= 0)
11658 {
11659 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11660
11661 /* If the replacement _DECL isn't initialized, don't do the
11662 transformation. */
11663 if (!fn)
11664 return NULL_TREE;
11665
11666 return build_call_expr_loc (loc, fn, 2, dst, src);
11667 }
11668 return NULL_TREE;
11669 }
11670 }
11671
11672 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11673 to the call.
11674
11675 Return NULL_TREE if no simplification was possible, otherwise return the
11676 simplified form of the call as a tree.
11677
11678 The simplified form may be a constant or other expression which
11679 computes the same value, but in a more efficient manner (including
11680 calls to other builtin functions).
11681
11682 The call may contain arguments which need to be evaluated, but
11683 which are not useful to determine the result of the call. In
11684 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11685 COMPOUND_EXPR will be an argument which must be evaluated.
11686 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11687 COMPOUND_EXPR in the chain will contain the tree for the simplified
11688 form of the builtin function call. */
11689
11690 static tree
11691 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11692 {
11693 if (!validate_arg (s1, POINTER_TYPE)
11694 || !validate_arg (s2, POINTER_TYPE))
11695 return NULL_TREE;
11696 else
11697 {
11698 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11699
11700 /* If both arguments are constants, evaluate at compile-time. */
11701 if (p1 && p2)
11702 {
11703 const size_t r = strspn (p1, p2);
11704 return build_int_cst (size_type_node, r);
11705 }
11706
11707 /* If either argument is "", return NULL_TREE. */
11708 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11709 /* Evaluate and ignore both arguments in case either one has
11710 side-effects. */
11711 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11712 s1, s2);
11713 return NULL_TREE;
11714 }
11715 }
11716
11717 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11718 to the call.
11719
11720 Return NULL_TREE if no simplification was possible, otherwise return the
11721 simplified form of the call as a tree.
11722
11723 The simplified form may be a constant or other expression which
11724 computes the same value, but in a more efficient manner (including
11725 calls to other builtin functions).
11726
11727 The call may contain arguments which need to be evaluated, but
11728 which are not useful to determine the result of the call. In
11729 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11730 COMPOUND_EXPR will be an argument which must be evaluated.
11731 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11732 COMPOUND_EXPR in the chain will contain the tree for the simplified
11733 form of the builtin function call. */
11734
11735 static tree
11736 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11737 {
11738 if (!validate_arg (s1, POINTER_TYPE)
11739 || !validate_arg (s2, POINTER_TYPE))
11740 return NULL_TREE;
11741 else
11742 {
11743 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11744
11745 /* If both arguments are constants, evaluate at compile-time. */
11746 if (p1 && p2)
11747 {
11748 const size_t r = strcspn (p1, p2);
11749 return build_int_cst (size_type_node, r);
11750 }
11751
11752 /* If the first argument is "", return NULL_TREE. */
11753 if (p1 && *p1 == '\0')
11754 {
11755 /* Evaluate and ignore argument s2 in case it has
11756 side-effects. */
11757 return omit_one_operand_loc (loc, size_type_node,
11758 size_zero_node, s2);
11759 }
11760
11761 /* If the second argument is "", return __builtin_strlen(s1). */
11762 if (p2 && *p2 == '\0')
11763 {
11764 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11765
11766 /* If the replacement _DECL isn't initialized, don't do the
11767 transformation. */
11768 if (!fn)
11769 return NULL_TREE;
11770
11771 return build_call_expr_loc (loc, fn, 1, s1);
11772 }
11773 return NULL_TREE;
11774 }
11775 }
11776
11777 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11778 to the call. IGNORE is true if the value returned
11779 by the builtin will be ignored. UNLOCKED is true is true if this
11780 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11781 the known length of the string. Return NULL_TREE if no simplification
11782 was possible. */
11783
11784 tree
11785 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11786 bool ignore, bool unlocked, tree len)
11787 {
11788 /* If we're using an unlocked function, assume the other unlocked
11789 functions exist explicitly. */
11790 tree const fn_fputc = (unlocked
11791 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11792 : builtin_decl_implicit (BUILT_IN_FPUTC));
11793 tree const fn_fwrite = (unlocked
11794 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11795 : builtin_decl_implicit (BUILT_IN_FWRITE));
11796
11797 /* If the return value is used, don't do the transformation. */
11798 if (!ignore)
11799 return NULL_TREE;
11800
11801 /* Verify the arguments in the original call. */
11802 if (!validate_arg (arg0, POINTER_TYPE)
11803 || !validate_arg (arg1, POINTER_TYPE))
11804 return NULL_TREE;
11805
11806 if (! len)
11807 len = c_strlen (arg0, 0);
11808
11809 /* Get the length of the string passed to fputs. If the length
11810 can't be determined, punt. */
11811 if (!len
11812 || TREE_CODE (len) != INTEGER_CST)
11813 return NULL_TREE;
11814
11815 switch (compare_tree_int (len, 1))
11816 {
11817 case -1: /* length is 0, delete the call entirely . */
11818 return omit_one_operand_loc (loc, integer_type_node,
11819 integer_zero_node, arg1);;
11820
11821 case 0: /* length is 1, call fputc. */
11822 {
11823 const char *p = c_getstr (arg0);
11824
11825 if (p != NULL)
11826 {
11827 if (fn_fputc)
11828 return build_call_expr_loc (loc, fn_fputc, 2,
11829 build_int_cst
11830 (integer_type_node, p[0]), arg1);
11831 else
11832 return NULL_TREE;
11833 }
11834 }
11835 /* FALLTHROUGH */
11836 case 1: /* length is greater than 1, call fwrite. */
11837 {
11838 /* If optimizing for size keep fputs. */
11839 if (optimize_function_for_size_p (cfun))
11840 return NULL_TREE;
11841 /* New argument list transforming fputs(string, stream) to
11842 fwrite(string, 1, len, stream). */
11843 if (fn_fwrite)
11844 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11845 size_one_node, len, arg1);
11846 else
11847 return NULL_TREE;
11848 }
11849 default:
11850 gcc_unreachable ();
11851 }
11852 return NULL_TREE;
11853 }
11854
11855 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11856 produced. False otherwise. This is done so that we don't output the error
11857 or warning twice or three times. */
11858
11859 bool
11860 fold_builtin_next_arg (tree exp, bool va_start_p)
11861 {
11862 tree fntype = TREE_TYPE (current_function_decl);
11863 int nargs = call_expr_nargs (exp);
11864 tree arg;
11865 /* There is good chance the current input_location points inside the
11866 definition of the va_start macro (perhaps on the token for
11867 builtin) in a system header, so warnings will not be emitted.
11868 Use the location in real source code. */
11869 source_location current_location =
11870 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11871 NULL);
11872
11873 if (!stdarg_p (fntype))
11874 {
11875 error ("%<va_start%> used in function with fixed args");
11876 return true;
11877 }
11878
11879 if (va_start_p)
11880 {
11881 if (va_start_p && (nargs != 2))
11882 {
11883 error ("wrong number of arguments to function %<va_start%>");
11884 return true;
11885 }
11886 arg = CALL_EXPR_ARG (exp, 1);
11887 }
11888 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11889 when we checked the arguments and if needed issued a warning. */
11890 else
11891 {
11892 if (nargs == 0)
11893 {
11894 /* Evidently an out of date version of <stdarg.h>; can't validate
11895 va_start's second argument, but can still work as intended. */
11896 warning_at (current_location,
11897 OPT_Wvarargs,
11898 "%<__builtin_next_arg%> called without an argument");
11899 return true;
11900 }
11901 else if (nargs > 1)
11902 {
11903 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11904 return true;
11905 }
11906 arg = CALL_EXPR_ARG (exp, 0);
11907 }
11908
11909 if (TREE_CODE (arg) == SSA_NAME)
11910 arg = SSA_NAME_VAR (arg);
11911
11912 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11913 or __builtin_next_arg (0) the first time we see it, after checking
11914 the arguments and if needed issuing a warning. */
11915 if (!integer_zerop (arg))
11916 {
11917 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11918
11919 /* Strip off all nops for the sake of the comparison. This
11920 is not quite the same as STRIP_NOPS. It does more.
11921 We must also strip off INDIRECT_EXPR for C++ reference
11922 parameters. */
11923 while (CONVERT_EXPR_P (arg)
11924 || TREE_CODE (arg) == INDIRECT_REF)
11925 arg = TREE_OPERAND (arg, 0);
11926 if (arg != last_parm)
11927 {
11928 /* FIXME: Sometimes with the tree optimizers we can get the
11929 not the last argument even though the user used the last
11930 argument. We just warn and set the arg to be the last
11931 argument so that we will get wrong-code because of
11932 it. */
11933 warning_at (current_location,
11934 OPT_Wvarargs,
11935 "second parameter of %<va_start%> not last named argument");
11936 }
11937
11938 /* Undefined by C99 7.15.1.4p4 (va_start):
11939 "If the parameter parmN is declared with the register storage
11940 class, with a function or array type, or with a type that is
11941 not compatible with the type that results after application of
11942 the default argument promotions, the behavior is undefined."
11943 */
11944 else if (DECL_REGISTER (arg))
11945 {
11946 warning_at (current_location,
11947 OPT_Wvarargs,
11948 "undefined behaviour when second parameter of "
11949 "%<va_start%> is declared with %<register%> storage");
11950 }
11951
11952 /* We want to verify the second parameter just once before the tree
11953 optimizers are run and then avoid keeping it in the tree,
11954 as otherwise we could warn even for correct code like:
11955 void foo (int i, ...)
11956 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11957 if (va_start_p)
11958 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11959 else
11960 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11961 }
11962 return false;
11963 }
11964
11965
11966 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11967 ORIG may be null if this is a 2-argument call. We don't attempt to
11968 simplify calls with more than 3 arguments.
11969
11970 Return NULL_TREE if no simplification was possible, otherwise return the
11971 simplified form of the call as a tree. If IGNORED is true, it means that
11972 the caller does not use the returned value of the function. */
11973
11974 static tree
11975 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11976 tree orig, int ignored)
11977 {
11978 tree call, retval;
11979 const char *fmt_str = NULL;
11980
11981 /* Verify the required arguments in the original call. We deal with two
11982 types of sprintf() calls: 'sprintf (str, fmt)' and
11983 'sprintf (dest, "%s", orig)'. */
11984 if (!validate_arg (dest, POINTER_TYPE)
11985 || !validate_arg (fmt, POINTER_TYPE))
11986 return NULL_TREE;
11987 if (orig && !validate_arg (orig, POINTER_TYPE))
11988 return NULL_TREE;
11989
11990 /* Check whether the format is a literal string constant. */
11991 fmt_str = c_getstr (fmt);
11992 if (fmt_str == NULL)
11993 return NULL_TREE;
11994
11995 call = NULL_TREE;
11996 retval = NULL_TREE;
11997
11998 if (!init_target_chars ())
11999 return NULL_TREE;
12000
12001 /* If the format doesn't contain % args or %%, use strcpy. */
12002 if (strchr (fmt_str, target_percent) == NULL)
12003 {
12004 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12005
12006 if (!fn)
12007 return NULL_TREE;
12008
12009 /* Don't optimize sprintf (buf, "abc", ptr++). */
12010 if (orig)
12011 return NULL_TREE;
12012
12013 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12014 'format' is known to contain no % formats. */
12015 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12016 if (!ignored)
12017 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12018 }
12019
12020 /* If the format is "%s", use strcpy if the result isn't used. */
12021 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12022 {
12023 tree fn;
12024 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12025
12026 if (!fn)
12027 return NULL_TREE;
12028
12029 /* Don't crash on sprintf (str1, "%s"). */
12030 if (!orig)
12031 return NULL_TREE;
12032
12033 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12034 if (!ignored)
12035 {
12036 retval = c_strlen (orig, 1);
12037 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12038 return NULL_TREE;
12039 }
12040 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12041 }
12042
12043 if (call && retval)
12044 {
12045 retval = fold_convert_loc
12046 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12047 retval);
12048 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12049 }
12050 else
12051 return call;
12052 }
12053
12054 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12055 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12056 attempt to simplify calls with more than 4 arguments.
12057
12058 Return NULL_TREE if no simplification was possible, otherwise return the
12059 simplified form of the call as a tree. If IGNORED is true, it means that
12060 the caller does not use the returned value of the function. */
12061
12062 static tree
12063 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12064 tree orig, int ignored)
12065 {
12066 tree call, retval;
12067 const char *fmt_str = NULL;
12068 unsigned HOST_WIDE_INT destlen;
12069
12070 /* Verify the required arguments in the original call. We deal with two
12071 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12072 'snprintf (dest, cst, "%s", orig)'. */
12073 if (!validate_arg (dest, POINTER_TYPE)
12074 || !validate_arg (destsize, INTEGER_TYPE)
12075 || !validate_arg (fmt, POINTER_TYPE))
12076 return NULL_TREE;
12077 if (orig && !validate_arg (orig, POINTER_TYPE))
12078 return NULL_TREE;
12079
12080 if (!tree_fits_uhwi_p (destsize))
12081 return NULL_TREE;
12082
12083 /* Check whether the format is a literal string constant. */
12084 fmt_str = c_getstr (fmt);
12085 if (fmt_str == NULL)
12086 return NULL_TREE;
12087
12088 call = NULL_TREE;
12089 retval = NULL_TREE;
12090
12091 if (!init_target_chars ())
12092 return NULL_TREE;
12093
12094 destlen = tree_to_uhwi (destsize);
12095
12096 /* If the format doesn't contain % args or %%, use strcpy. */
12097 if (strchr (fmt_str, target_percent) == NULL)
12098 {
12099 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12100 size_t len = strlen (fmt_str);
12101
12102 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12103 if (orig)
12104 return NULL_TREE;
12105
12106 /* We could expand this as
12107 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12108 or to
12109 memcpy (str, fmt_with_nul_at_cstm1, cst);
12110 but in the former case that might increase code size
12111 and in the latter case grow .rodata section too much.
12112 So punt for now. */
12113 if (len >= destlen)
12114 return NULL_TREE;
12115
12116 if (!fn)
12117 return NULL_TREE;
12118
12119 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12120 'format' is known to contain no % formats and
12121 strlen (fmt) < cst. */
12122 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12123
12124 if (!ignored)
12125 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12126 }
12127
12128 /* If the format is "%s", use strcpy if the result isn't used. */
12129 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12130 {
12131 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12132 unsigned HOST_WIDE_INT origlen;
12133
12134 /* Don't crash on snprintf (str1, cst, "%s"). */
12135 if (!orig)
12136 return NULL_TREE;
12137
12138 retval = c_strlen (orig, 1);
12139 if (!retval || !tree_fits_uhwi_p (retval))
12140 return NULL_TREE;
12141
12142 origlen = tree_to_uhwi (retval);
12143 /* We could expand this as
12144 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12145 or to
12146 memcpy (str1, str2_with_nul_at_cstm1, cst);
12147 but in the former case that might increase code size
12148 and in the latter case grow .rodata section too much.
12149 So punt for now. */
12150 if (origlen >= destlen)
12151 return NULL_TREE;
12152
12153 /* Convert snprintf (str1, cst, "%s", str2) into
12154 strcpy (str1, str2) if strlen (str2) < cst. */
12155 if (!fn)
12156 return NULL_TREE;
12157
12158 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12159
12160 if (ignored)
12161 retval = NULL_TREE;
12162 }
12163
12164 if (call && retval)
12165 {
12166 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12167 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12168 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12169 }
12170 else
12171 return call;
12172 }
12173
12174 /* Expand a call EXP to __builtin_object_size. */
12175
12176 static rtx
12177 expand_builtin_object_size (tree exp)
12178 {
12179 tree ost;
12180 int object_size_type;
12181 tree fndecl = get_callee_fndecl (exp);
12182
12183 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12184 {
12185 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12186 exp, fndecl);
12187 expand_builtin_trap ();
12188 return const0_rtx;
12189 }
12190
12191 ost = CALL_EXPR_ARG (exp, 1);
12192 STRIP_NOPS (ost);
12193
12194 if (TREE_CODE (ost) != INTEGER_CST
12195 || tree_int_cst_sgn (ost) < 0
12196 || compare_tree_int (ost, 3) > 0)
12197 {
12198 error ("%Klast argument of %D is not integer constant between 0 and 3",
12199 exp, fndecl);
12200 expand_builtin_trap ();
12201 return const0_rtx;
12202 }
12203
12204 object_size_type = tree_to_shwi (ost);
12205
12206 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12207 }
12208
12209 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12210 FCODE is the BUILT_IN_* to use.
12211 Return NULL_RTX if we failed; the caller should emit a normal call,
12212 otherwise try to get the result in TARGET, if convenient (and in
12213 mode MODE if that's convenient). */
12214
12215 static rtx
12216 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12217 enum built_in_function fcode)
12218 {
12219 tree dest, src, len, size;
12220
12221 if (!validate_arglist (exp,
12222 POINTER_TYPE,
12223 fcode == BUILT_IN_MEMSET_CHK
12224 ? INTEGER_TYPE : POINTER_TYPE,
12225 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12226 return NULL_RTX;
12227
12228 dest = CALL_EXPR_ARG (exp, 0);
12229 src = CALL_EXPR_ARG (exp, 1);
12230 len = CALL_EXPR_ARG (exp, 2);
12231 size = CALL_EXPR_ARG (exp, 3);
12232
12233 if (! tree_fits_uhwi_p (size))
12234 return NULL_RTX;
12235
12236 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12237 {
12238 tree fn;
12239
12240 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12241 {
12242 warning_at (tree_nonartificial_location (exp),
12243 0, "%Kcall to %D will always overflow destination buffer",
12244 exp, get_callee_fndecl (exp));
12245 return NULL_RTX;
12246 }
12247
12248 fn = NULL_TREE;
12249 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12250 mem{cpy,pcpy,move,set} is available. */
12251 switch (fcode)
12252 {
12253 case BUILT_IN_MEMCPY_CHK:
12254 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12255 break;
12256 case BUILT_IN_MEMPCPY_CHK:
12257 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12258 break;
12259 case BUILT_IN_MEMMOVE_CHK:
12260 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12261 break;
12262 case BUILT_IN_MEMSET_CHK:
12263 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12264 break;
12265 default:
12266 break;
12267 }
12268
12269 if (! fn)
12270 return NULL_RTX;
12271
12272 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12273 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12274 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12275 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12276 }
12277 else if (fcode == BUILT_IN_MEMSET_CHK)
12278 return NULL_RTX;
12279 else
12280 {
12281 unsigned int dest_align = get_pointer_alignment (dest);
12282
12283 /* If DEST is not a pointer type, call the normal function. */
12284 if (dest_align == 0)
12285 return NULL_RTX;
12286
12287 /* If SRC and DEST are the same (and not volatile), do nothing. */
12288 if (operand_equal_p (src, dest, 0))
12289 {
12290 tree expr;
12291
12292 if (fcode != BUILT_IN_MEMPCPY_CHK)
12293 {
12294 /* Evaluate and ignore LEN in case it has side-effects. */
12295 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12296 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12297 }
12298
12299 expr = fold_build_pointer_plus (dest, len);
12300 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12301 }
12302
12303 /* __memmove_chk special case. */
12304 if (fcode == BUILT_IN_MEMMOVE_CHK)
12305 {
12306 unsigned int src_align = get_pointer_alignment (src);
12307
12308 if (src_align == 0)
12309 return NULL_RTX;
12310
12311 /* If src is categorized for a readonly section we can use
12312 normal __memcpy_chk. */
12313 if (readonly_data_expr (src))
12314 {
12315 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12316 if (!fn)
12317 return NULL_RTX;
12318 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12319 dest, src, len, size);
12320 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12321 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12322 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12323 }
12324 }
12325 return NULL_RTX;
12326 }
12327 }
12328
12329 /* Emit warning if a buffer overflow is detected at compile time. */
12330
12331 static void
12332 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12333 {
12334 int is_strlen = 0;
12335 tree len, size;
12336 location_t loc = tree_nonartificial_location (exp);
12337
12338 switch (fcode)
12339 {
12340 case BUILT_IN_STRCPY_CHK:
12341 case BUILT_IN_STPCPY_CHK:
12342 /* For __strcat_chk the warning will be emitted only if overflowing
12343 by at least strlen (dest) + 1 bytes. */
12344 case BUILT_IN_STRCAT_CHK:
12345 len = CALL_EXPR_ARG (exp, 1);
12346 size = CALL_EXPR_ARG (exp, 2);
12347 is_strlen = 1;
12348 break;
12349 case BUILT_IN_STRNCAT_CHK:
12350 case BUILT_IN_STRNCPY_CHK:
12351 case BUILT_IN_STPNCPY_CHK:
12352 len = CALL_EXPR_ARG (exp, 2);
12353 size = CALL_EXPR_ARG (exp, 3);
12354 break;
12355 case BUILT_IN_SNPRINTF_CHK:
12356 case BUILT_IN_VSNPRINTF_CHK:
12357 len = CALL_EXPR_ARG (exp, 1);
12358 size = CALL_EXPR_ARG (exp, 3);
12359 break;
12360 default:
12361 gcc_unreachable ();
12362 }
12363
12364 if (!len || !size)
12365 return;
12366
12367 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12368 return;
12369
12370 if (is_strlen)
12371 {
12372 len = c_strlen (len, 1);
12373 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12374 return;
12375 }
12376 else if (fcode == BUILT_IN_STRNCAT_CHK)
12377 {
12378 tree src = CALL_EXPR_ARG (exp, 1);
12379 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12380 return;
12381 src = c_strlen (src, 1);
12382 if (! src || ! tree_fits_uhwi_p (src))
12383 {
12384 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12385 exp, get_callee_fndecl (exp));
12386 return;
12387 }
12388 else if (tree_int_cst_lt (src, size))
12389 return;
12390 }
12391 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12392 return;
12393
12394 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12395 exp, get_callee_fndecl (exp));
12396 }
12397
12398 /* Emit warning if a buffer overflow is detected at compile time
12399 in __sprintf_chk/__vsprintf_chk calls. */
12400
12401 static void
12402 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12403 {
12404 tree size, len, fmt;
12405 const char *fmt_str;
12406 int nargs = call_expr_nargs (exp);
12407
12408 /* Verify the required arguments in the original call. */
12409
12410 if (nargs < 4)
12411 return;
12412 size = CALL_EXPR_ARG (exp, 2);
12413 fmt = CALL_EXPR_ARG (exp, 3);
12414
12415 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12416 return;
12417
12418 /* Check whether the format is a literal string constant. */
12419 fmt_str = c_getstr (fmt);
12420 if (fmt_str == NULL)
12421 return;
12422
12423 if (!init_target_chars ())
12424 return;
12425
12426 /* If the format doesn't contain % args or %%, we know its size. */
12427 if (strchr (fmt_str, target_percent) == 0)
12428 len = build_int_cstu (size_type_node, strlen (fmt_str));
12429 /* If the format is "%s" and first ... argument is a string literal,
12430 we know it too. */
12431 else if (fcode == BUILT_IN_SPRINTF_CHK
12432 && strcmp (fmt_str, target_percent_s) == 0)
12433 {
12434 tree arg;
12435
12436 if (nargs < 5)
12437 return;
12438 arg = CALL_EXPR_ARG (exp, 4);
12439 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12440 return;
12441
12442 len = c_strlen (arg, 1);
12443 if (!len || ! tree_fits_uhwi_p (len))
12444 return;
12445 }
12446 else
12447 return;
12448
12449 if (! tree_int_cst_lt (len, size))
12450 warning_at (tree_nonartificial_location (exp),
12451 0, "%Kcall to %D will always overflow destination buffer",
12452 exp, get_callee_fndecl (exp));
12453 }
12454
12455 /* Emit warning if a free is called with address of a variable. */
12456
12457 static void
12458 maybe_emit_free_warning (tree exp)
12459 {
12460 tree arg = CALL_EXPR_ARG (exp, 0);
12461
12462 STRIP_NOPS (arg);
12463 if (TREE_CODE (arg) != ADDR_EXPR)
12464 return;
12465
12466 arg = get_base_address (TREE_OPERAND (arg, 0));
12467 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12468 return;
12469
12470 if (SSA_VAR_P (arg))
12471 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12472 "%Kattempt to free a non-heap object %qD", exp, arg);
12473 else
12474 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12475 "%Kattempt to free a non-heap object", exp);
12476 }
12477
12478 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12479 if possible. */
12480
12481 static tree
12482 fold_builtin_object_size (tree ptr, tree ost)
12483 {
12484 unsigned HOST_WIDE_INT bytes;
12485 int object_size_type;
12486
12487 if (!validate_arg (ptr, POINTER_TYPE)
12488 || !validate_arg (ost, INTEGER_TYPE))
12489 return NULL_TREE;
12490
12491 STRIP_NOPS (ost);
12492
12493 if (TREE_CODE (ost) != INTEGER_CST
12494 || tree_int_cst_sgn (ost) < 0
12495 || compare_tree_int (ost, 3) > 0)
12496 return NULL_TREE;
12497
12498 object_size_type = tree_to_shwi (ost);
12499
12500 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12501 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12502 and (size_t) 0 for types 2 and 3. */
12503 if (TREE_SIDE_EFFECTS (ptr))
12504 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12505
12506 if (TREE_CODE (ptr) == ADDR_EXPR)
12507 {
12508 bytes = compute_builtin_object_size (ptr, object_size_type);
12509 if (wi::fits_to_tree_p (bytes, size_type_node))
12510 return build_int_cstu (size_type_node, bytes);
12511 }
12512 else if (TREE_CODE (ptr) == SSA_NAME)
12513 {
12514 /* If object size is not known yet, delay folding until
12515 later. Maybe subsequent passes will help determining
12516 it. */
12517 bytes = compute_builtin_object_size (ptr, object_size_type);
12518 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12519 && wi::fits_to_tree_p (bytes, size_type_node))
12520 return build_int_cstu (size_type_node, bytes);
12521 }
12522
12523 return NULL_TREE;
12524 }
12525
12526 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12527 DEST, SRC, LEN, and SIZE are the arguments to the call.
12528 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12529 code of the builtin. If MAXLEN is not NULL, it is maximum length
12530 passed as third argument. */
12531
12532 tree
12533 fold_builtin_memory_chk (location_t loc, tree fndecl,
12534 tree dest, tree src, tree len, tree size,
12535 tree maxlen, bool ignore,
12536 enum built_in_function fcode)
12537 {
12538 tree fn;
12539
12540 if (!validate_arg (dest, POINTER_TYPE)
12541 || !validate_arg (src,
12542 (fcode == BUILT_IN_MEMSET_CHK
12543 ? INTEGER_TYPE : POINTER_TYPE))
12544 || !validate_arg (len, INTEGER_TYPE)
12545 || !validate_arg (size, INTEGER_TYPE))
12546 return NULL_TREE;
12547
12548 /* If SRC and DEST are the same (and not volatile), return DEST
12549 (resp. DEST+LEN for __mempcpy_chk). */
12550 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12551 {
12552 if (fcode != BUILT_IN_MEMPCPY_CHK)
12553 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12554 dest, len);
12555 else
12556 {
12557 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12558 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12559 }
12560 }
12561
12562 if (! tree_fits_uhwi_p (size))
12563 return NULL_TREE;
12564
12565 if (! integer_all_onesp (size))
12566 {
12567 if (! tree_fits_uhwi_p (len))
12568 {
12569 /* If LEN is not constant, try MAXLEN too.
12570 For MAXLEN only allow optimizing into non-_ocs function
12571 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12572 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12573 {
12574 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12575 {
12576 /* (void) __mempcpy_chk () can be optimized into
12577 (void) __memcpy_chk (). */
12578 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12579 if (!fn)
12580 return NULL_TREE;
12581
12582 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12583 }
12584 return NULL_TREE;
12585 }
12586 }
12587 else
12588 maxlen = len;
12589
12590 if (tree_int_cst_lt (size, maxlen))
12591 return NULL_TREE;
12592 }
12593
12594 fn = NULL_TREE;
12595 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12596 mem{cpy,pcpy,move,set} is available. */
12597 switch (fcode)
12598 {
12599 case BUILT_IN_MEMCPY_CHK:
12600 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12601 break;
12602 case BUILT_IN_MEMPCPY_CHK:
12603 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12604 break;
12605 case BUILT_IN_MEMMOVE_CHK:
12606 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12607 break;
12608 case BUILT_IN_MEMSET_CHK:
12609 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12610 break;
12611 default:
12612 break;
12613 }
12614
12615 if (!fn)
12616 return NULL_TREE;
12617
12618 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12619 }
12620
12621 /* Fold a call to the __st[rp]cpy_chk builtin.
12622 DEST, SRC, and SIZE are the arguments to the call.
12623 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12624 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12625 strings passed as second argument. */
12626
12627 tree
12628 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12629 tree src, tree size,
12630 tree maxlen, bool ignore,
12631 enum built_in_function fcode)
12632 {
12633 tree len, fn;
12634
12635 if (!validate_arg (dest, POINTER_TYPE)
12636 || !validate_arg (src, POINTER_TYPE)
12637 || !validate_arg (size, INTEGER_TYPE))
12638 return NULL_TREE;
12639
12640 /* If SRC and DEST are the same (and not volatile), return DEST. */
12641 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12642 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12643
12644 if (! tree_fits_uhwi_p (size))
12645 return NULL_TREE;
12646
12647 if (! integer_all_onesp (size))
12648 {
12649 len = c_strlen (src, 1);
12650 if (! len || ! tree_fits_uhwi_p (len))
12651 {
12652 /* If LEN is not constant, try MAXLEN too.
12653 For MAXLEN only allow optimizing into non-_ocs function
12654 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12655 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12656 {
12657 if (fcode == BUILT_IN_STPCPY_CHK)
12658 {
12659 if (! ignore)
12660 return NULL_TREE;
12661
12662 /* If return value of __stpcpy_chk is ignored,
12663 optimize into __strcpy_chk. */
12664 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12665 if (!fn)
12666 return NULL_TREE;
12667
12668 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12669 }
12670
12671 if (! len || TREE_SIDE_EFFECTS (len))
12672 return NULL_TREE;
12673
12674 /* If c_strlen returned something, but not a constant,
12675 transform __strcpy_chk into __memcpy_chk. */
12676 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12677 if (!fn)
12678 return NULL_TREE;
12679
12680 len = fold_convert_loc (loc, size_type_node, len);
12681 len = size_binop_loc (loc, PLUS_EXPR, len,
12682 build_int_cst (size_type_node, 1));
12683 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12684 build_call_expr_loc (loc, fn, 4,
12685 dest, src, len, size));
12686 }
12687 }
12688 else
12689 maxlen = len;
12690
12691 if (! tree_int_cst_lt (maxlen, size))
12692 return NULL_TREE;
12693 }
12694
12695 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12696 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12697 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12698 if (!fn)
12699 return NULL_TREE;
12700
12701 return build_call_expr_loc (loc, fn, 2, dest, src);
12702 }
12703
12704 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12705 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12706 length passed as third argument. IGNORE is true if return value can be
12707 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12708
12709 tree
12710 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12711 tree len, tree size, tree maxlen, bool ignore,
12712 enum built_in_function fcode)
12713 {
12714 tree fn;
12715
12716 if (!validate_arg (dest, POINTER_TYPE)
12717 || !validate_arg (src, POINTER_TYPE)
12718 || !validate_arg (len, INTEGER_TYPE)
12719 || !validate_arg (size, INTEGER_TYPE))
12720 return NULL_TREE;
12721
12722 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12723 {
12724 /* If return value of __stpncpy_chk is ignored,
12725 optimize into __strncpy_chk. */
12726 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12727 if (fn)
12728 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12729 }
12730
12731 if (! tree_fits_uhwi_p (size))
12732 return NULL_TREE;
12733
12734 if (! integer_all_onesp (size))
12735 {
12736 if (! tree_fits_uhwi_p (len))
12737 {
12738 /* If LEN is not constant, try MAXLEN too.
12739 For MAXLEN only allow optimizing into non-_ocs function
12740 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12741 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12742 return NULL_TREE;
12743 }
12744 else
12745 maxlen = len;
12746
12747 if (tree_int_cst_lt (size, maxlen))
12748 return NULL_TREE;
12749 }
12750
12751 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12752 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12753 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12754 if (!fn)
12755 return NULL_TREE;
12756
12757 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12758 }
12759
12760 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12761 are the arguments to the call. */
12762
12763 static tree
12764 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12765 tree src, tree size)
12766 {
12767 tree fn;
12768 const char *p;
12769
12770 if (!validate_arg (dest, POINTER_TYPE)
12771 || !validate_arg (src, POINTER_TYPE)
12772 || !validate_arg (size, INTEGER_TYPE))
12773 return NULL_TREE;
12774
12775 p = c_getstr (src);
12776 /* If the SRC parameter is "", return DEST. */
12777 if (p && *p == '\0')
12778 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12779
12780 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12781 return NULL_TREE;
12782
12783 /* If __builtin_strcat_chk is used, assume strcat is available. */
12784 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12785 if (!fn)
12786 return NULL_TREE;
12787
12788 return build_call_expr_loc (loc, fn, 2, dest, src);
12789 }
12790
12791 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12792 LEN, and SIZE. */
12793
12794 static tree
12795 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12796 tree dest, tree src, tree len, tree size)
12797 {
12798 tree fn;
12799 const char *p;
12800
12801 if (!validate_arg (dest, POINTER_TYPE)
12802 || !validate_arg (src, POINTER_TYPE)
12803 || !validate_arg (size, INTEGER_TYPE)
12804 || !validate_arg (size, INTEGER_TYPE))
12805 return NULL_TREE;
12806
12807 p = c_getstr (src);
12808 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12809 if (p && *p == '\0')
12810 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12811 else if (integer_zerop (len))
12812 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12813
12814 if (! tree_fits_uhwi_p (size))
12815 return NULL_TREE;
12816
12817 if (! integer_all_onesp (size))
12818 {
12819 tree src_len = c_strlen (src, 1);
12820 if (src_len
12821 && tree_fits_uhwi_p (src_len)
12822 && tree_fits_uhwi_p (len)
12823 && ! tree_int_cst_lt (len, src_len))
12824 {
12825 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12826 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12827 if (!fn)
12828 return NULL_TREE;
12829
12830 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12831 }
12832 return NULL_TREE;
12833 }
12834
12835 /* If __builtin_strncat_chk is used, assume strncat is available. */
12836 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
12837 if (!fn)
12838 return NULL_TREE;
12839
12840 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12841 }
12842
12843 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12844 Return NULL_TREE if a normal call should be emitted rather than
12845 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12846 or BUILT_IN_VSPRINTF_CHK. */
12847
12848 static tree
12849 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12850 enum built_in_function fcode)
12851 {
12852 tree dest, size, len, fn, fmt, flag;
12853 const char *fmt_str;
12854
12855 /* Verify the required arguments in the original call. */
12856 if (nargs < 4)
12857 return NULL_TREE;
12858 dest = args[0];
12859 if (!validate_arg (dest, POINTER_TYPE))
12860 return NULL_TREE;
12861 flag = args[1];
12862 if (!validate_arg (flag, INTEGER_TYPE))
12863 return NULL_TREE;
12864 size = args[2];
12865 if (!validate_arg (size, INTEGER_TYPE))
12866 return NULL_TREE;
12867 fmt = args[3];
12868 if (!validate_arg (fmt, POINTER_TYPE))
12869 return NULL_TREE;
12870
12871 if (! tree_fits_uhwi_p (size))
12872 return NULL_TREE;
12873
12874 len = NULL_TREE;
12875
12876 if (!init_target_chars ())
12877 return NULL_TREE;
12878
12879 /* Check whether the format is a literal string constant. */
12880 fmt_str = c_getstr (fmt);
12881 if (fmt_str != NULL)
12882 {
12883 /* If the format doesn't contain % args or %%, we know the size. */
12884 if (strchr (fmt_str, target_percent) == 0)
12885 {
12886 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12887 len = build_int_cstu (size_type_node, strlen (fmt_str));
12888 }
12889 /* If the format is "%s" and first ... argument is a string literal,
12890 we know the size too. */
12891 else if (fcode == BUILT_IN_SPRINTF_CHK
12892 && strcmp (fmt_str, target_percent_s) == 0)
12893 {
12894 tree arg;
12895
12896 if (nargs == 5)
12897 {
12898 arg = args[4];
12899 if (validate_arg (arg, POINTER_TYPE))
12900 {
12901 len = c_strlen (arg, 1);
12902 if (! len || ! tree_fits_uhwi_p (len))
12903 len = NULL_TREE;
12904 }
12905 }
12906 }
12907 }
12908
12909 if (! integer_all_onesp (size))
12910 {
12911 if (! len || ! tree_int_cst_lt (len, size))
12912 return NULL_TREE;
12913 }
12914
12915 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12916 or if format doesn't contain % chars or is "%s". */
12917 if (! integer_zerop (flag))
12918 {
12919 if (fmt_str == NULL)
12920 return NULL_TREE;
12921 if (strchr (fmt_str, target_percent) != NULL
12922 && strcmp (fmt_str, target_percent_s))
12923 return NULL_TREE;
12924 }
12925
12926 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12927 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
12928 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
12929 if (!fn)
12930 return NULL_TREE;
12931
12932 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12933 }
12934
12935 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12936 a normal call should be emitted rather than expanding the function
12937 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12938
12939 static tree
12940 fold_builtin_sprintf_chk (location_t loc, tree exp,
12941 enum built_in_function fcode)
12942 {
12943 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12944 CALL_EXPR_ARGP (exp), fcode);
12945 }
12946
12947 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12948 NULL_TREE if a normal call should be emitted rather than expanding
12949 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12950 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12951 passed as second argument. */
12952
12953 static tree
12954 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12955 tree maxlen, enum built_in_function fcode)
12956 {
12957 tree dest, size, len, fn, fmt, flag;
12958 const char *fmt_str;
12959
12960 /* Verify the required arguments in the original call. */
12961 if (nargs < 5)
12962 return NULL_TREE;
12963 dest = args[0];
12964 if (!validate_arg (dest, POINTER_TYPE))
12965 return NULL_TREE;
12966 len = args[1];
12967 if (!validate_arg (len, INTEGER_TYPE))
12968 return NULL_TREE;
12969 flag = args[2];
12970 if (!validate_arg (flag, INTEGER_TYPE))
12971 return NULL_TREE;
12972 size = args[3];
12973 if (!validate_arg (size, INTEGER_TYPE))
12974 return NULL_TREE;
12975 fmt = args[4];
12976 if (!validate_arg (fmt, POINTER_TYPE))
12977 return NULL_TREE;
12978
12979 if (! tree_fits_uhwi_p (size))
12980 return NULL_TREE;
12981
12982 if (! integer_all_onesp (size))
12983 {
12984 if (! tree_fits_uhwi_p (len))
12985 {
12986 /* If LEN is not constant, try MAXLEN too.
12987 For MAXLEN only allow optimizing into non-_ocs function
12988 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12989 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12990 return NULL_TREE;
12991 }
12992 else
12993 maxlen = len;
12994
12995 if (tree_int_cst_lt (size, maxlen))
12996 return NULL_TREE;
12997 }
12998
12999 if (!init_target_chars ())
13000 return NULL_TREE;
13001
13002 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13003 or if format doesn't contain % chars or is "%s". */
13004 if (! integer_zerop (flag))
13005 {
13006 fmt_str = c_getstr (fmt);
13007 if (fmt_str == NULL)
13008 return NULL_TREE;
13009 if (strchr (fmt_str, target_percent) != NULL
13010 && strcmp (fmt_str, target_percent_s))
13011 return NULL_TREE;
13012 }
13013
13014 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13015 available. */
13016 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13017 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13018 if (!fn)
13019 return NULL_TREE;
13020
13021 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13022 }
13023
13024 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13025 a normal call should be emitted rather than expanding the function
13026 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13027 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13028 passed as second argument. */
13029
13030 static tree
13031 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13032 enum built_in_function fcode)
13033 {
13034 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13035 CALL_EXPR_ARGP (exp), maxlen, fcode);
13036 }
13037
13038 /* Builtins with folding operations that operate on "..." arguments
13039 need special handling; we need to store the arguments in a convenient
13040 data structure before attempting any folding. Fortunately there are
13041 only a few builtins that fall into this category. FNDECL is the
13042 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13043 result of the function call is ignored. */
13044
13045 static tree
13046 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13047 bool ignore ATTRIBUTE_UNUSED)
13048 {
13049 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13050 tree ret = NULL_TREE;
13051
13052 switch (fcode)
13053 {
13054 case BUILT_IN_SPRINTF_CHK:
13055 case BUILT_IN_VSPRINTF_CHK:
13056 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13057 break;
13058
13059 case BUILT_IN_SNPRINTF_CHK:
13060 case BUILT_IN_VSNPRINTF_CHK:
13061 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13062 break;
13063
13064 case BUILT_IN_FPCLASSIFY:
13065 ret = fold_builtin_fpclassify (loc, exp);
13066 break;
13067
13068 default:
13069 break;
13070 }
13071 if (ret)
13072 {
13073 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13074 SET_EXPR_LOCATION (ret, loc);
13075 TREE_NO_WARNING (ret) = 1;
13076 return ret;
13077 }
13078 return NULL_TREE;
13079 }
13080
13081 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13082 FMT and ARG are the arguments to the call; we don't fold cases with
13083 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13084
13085 Return NULL_TREE if no simplification was possible, otherwise return the
13086 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13087 code of the function to be simplified. */
13088
13089 static tree
13090 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13091 tree arg, bool ignore,
13092 enum built_in_function fcode)
13093 {
13094 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13095 const char *fmt_str = NULL;
13096
13097 /* If the return value is used, don't do the transformation. */
13098 if (! ignore)
13099 return NULL_TREE;
13100
13101 /* Verify the required arguments in the original call. */
13102 if (!validate_arg (fmt, POINTER_TYPE))
13103 return NULL_TREE;
13104
13105 /* Check whether the format is a literal string constant. */
13106 fmt_str = c_getstr (fmt);
13107 if (fmt_str == NULL)
13108 return NULL_TREE;
13109
13110 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13111 {
13112 /* If we're using an unlocked function, assume the other
13113 unlocked functions exist explicitly. */
13114 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13115 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13116 }
13117 else
13118 {
13119 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13120 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13121 }
13122
13123 if (!init_target_chars ())
13124 return NULL_TREE;
13125
13126 if (strcmp (fmt_str, target_percent_s) == 0
13127 || strchr (fmt_str, target_percent) == NULL)
13128 {
13129 const char *str;
13130
13131 if (strcmp (fmt_str, target_percent_s) == 0)
13132 {
13133 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13134 return NULL_TREE;
13135
13136 if (!arg || !validate_arg (arg, POINTER_TYPE))
13137 return NULL_TREE;
13138
13139 str = c_getstr (arg);
13140 if (str == NULL)
13141 return NULL_TREE;
13142 }
13143 else
13144 {
13145 /* The format specifier doesn't contain any '%' characters. */
13146 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13147 && arg)
13148 return NULL_TREE;
13149 str = fmt_str;
13150 }
13151
13152 /* If the string was "", printf does nothing. */
13153 if (str[0] == '\0')
13154 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13155
13156 /* If the string has length of 1, call putchar. */
13157 if (str[1] == '\0')
13158 {
13159 /* Given printf("c"), (where c is any one character,)
13160 convert "c"[0] to an int and pass that to the replacement
13161 function. */
13162 newarg = build_int_cst (integer_type_node, str[0]);
13163 if (fn_putchar)
13164 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13165 }
13166 else
13167 {
13168 /* If the string was "string\n", call puts("string"). */
13169 size_t len = strlen (str);
13170 if ((unsigned char)str[len - 1] == target_newline
13171 && (size_t) (int) len == len
13172 && (int) len > 0)
13173 {
13174 char *newstr;
13175 tree offset_node, string_cst;
13176
13177 /* Create a NUL-terminated string that's one char shorter
13178 than the original, stripping off the trailing '\n'. */
13179 newarg = build_string_literal (len, str);
13180 string_cst = string_constant (newarg, &offset_node);
13181 gcc_checking_assert (string_cst
13182 && (TREE_STRING_LENGTH (string_cst)
13183 == (int) len)
13184 && integer_zerop (offset_node)
13185 && (unsigned char)
13186 TREE_STRING_POINTER (string_cst)[len - 1]
13187 == target_newline);
13188 /* build_string_literal creates a new STRING_CST,
13189 modify it in place to avoid double copying. */
13190 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13191 newstr[len - 1] = '\0';
13192 if (fn_puts)
13193 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13194 }
13195 else
13196 /* We'd like to arrange to call fputs(string,stdout) here,
13197 but we need stdout and don't have a way to get it yet. */
13198 return NULL_TREE;
13199 }
13200 }
13201
13202 /* The other optimizations can be done only on the non-va_list variants. */
13203 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13204 return NULL_TREE;
13205
13206 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13207 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13208 {
13209 if (!arg || !validate_arg (arg, POINTER_TYPE))
13210 return NULL_TREE;
13211 if (fn_puts)
13212 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13213 }
13214
13215 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13216 else if (strcmp (fmt_str, target_percent_c) == 0)
13217 {
13218 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13219 return NULL_TREE;
13220 if (fn_putchar)
13221 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13222 }
13223
13224 if (!call)
13225 return NULL_TREE;
13226
13227 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13228 }
13229
13230 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13231 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13232 more than 3 arguments, and ARG may be null in the 2-argument case.
13233
13234 Return NULL_TREE if no simplification was possible, otherwise return the
13235 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13236 code of the function to be simplified. */
13237
13238 static tree
13239 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13240 tree fmt, tree arg, bool ignore,
13241 enum built_in_function fcode)
13242 {
13243 tree fn_fputc, fn_fputs, call = NULL_TREE;
13244 const char *fmt_str = NULL;
13245
13246 /* If the return value is used, don't do the transformation. */
13247 if (! ignore)
13248 return NULL_TREE;
13249
13250 /* Verify the required arguments in the original call. */
13251 if (!validate_arg (fp, POINTER_TYPE))
13252 return NULL_TREE;
13253 if (!validate_arg (fmt, POINTER_TYPE))
13254 return NULL_TREE;
13255
13256 /* Check whether the format is a literal string constant. */
13257 fmt_str = c_getstr (fmt);
13258 if (fmt_str == NULL)
13259 return NULL_TREE;
13260
13261 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13262 {
13263 /* If we're using an unlocked function, assume the other
13264 unlocked functions exist explicitly. */
13265 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13266 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13267 }
13268 else
13269 {
13270 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13271 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13272 }
13273
13274 if (!init_target_chars ())
13275 return NULL_TREE;
13276
13277 /* If the format doesn't contain % args or %%, use strcpy. */
13278 if (strchr (fmt_str, target_percent) == NULL)
13279 {
13280 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13281 && arg)
13282 return NULL_TREE;
13283
13284 /* If the format specifier was "", fprintf does nothing. */
13285 if (fmt_str[0] == '\0')
13286 {
13287 /* If FP has side-effects, just wait until gimplification is
13288 done. */
13289 if (TREE_SIDE_EFFECTS (fp))
13290 return NULL_TREE;
13291
13292 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13293 }
13294
13295 /* When "string" doesn't contain %, replace all cases of
13296 fprintf (fp, string) with fputs (string, fp). The fputs
13297 builtin will take care of special cases like length == 1. */
13298 if (fn_fputs)
13299 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13300 }
13301
13302 /* The other optimizations can be done only on the non-va_list variants. */
13303 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13304 return NULL_TREE;
13305
13306 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13307 else if (strcmp (fmt_str, target_percent_s) == 0)
13308 {
13309 if (!arg || !validate_arg (arg, POINTER_TYPE))
13310 return NULL_TREE;
13311 if (fn_fputs)
13312 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13313 }
13314
13315 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13316 else if (strcmp (fmt_str, target_percent_c) == 0)
13317 {
13318 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13319 return NULL_TREE;
13320 if (fn_fputc)
13321 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13322 }
13323
13324 if (!call)
13325 return NULL_TREE;
13326 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13327 }
13328
13329 /* Initialize format string characters in the target charset. */
13330
13331 static bool
13332 init_target_chars (void)
13333 {
13334 static bool init;
13335 if (!init)
13336 {
13337 target_newline = lang_hooks.to_target_charset ('\n');
13338 target_percent = lang_hooks.to_target_charset ('%');
13339 target_c = lang_hooks.to_target_charset ('c');
13340 target_s = lang_hooks.to_target_charset ('s');
13341 if (target_newline == 0 || target_percent == 0 || target_c == 0
13342 || target_s == 0)
13343 return false;
13344
13345 target_percent_c[0] = target_percent;
13346 target_percent_c[1] = target_c;
13347 target_percent_c[2] = '\0';
13348
13349 target_percent_s[0] = target_percent;
13350 target_percent_s[1] = target_s;
13351 target_percent_s[2] = '\0';
13352
13353 target_percent_s_newline[0] = target_percent;
13354 target_percent_s_newline[1] = target_s;
13355 target_percent_s_newline[2] = target_newline;
13356 target_percent_s_newline[3] = '\0';
13357
13358 init = true;
13359 }
13360 return true;
13361 }
13362
13363 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13364 and no overflow/underflow occurred. INEXACT is true if M was not
13365 exactly calculated. TYPE is the tree type for the result. This
13366 function assumes that you cleared the MPFR flags and then
13367 calculated M to see if anything subsequently set a flag prior to
13368 entering this function. Return NULL_TREE if any checks fail. */
13369
13370 static tree
13371 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13372 {
13373 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13374 overflow/underflow occurred. If -frounding-math, proceed iff the
13375 result of calling FUNC was exact. */
13376 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13377 && (!flag_rounding_math || !inexact))
13378 {
13379 REAL_VALUE_TYPE rr;
13380
13381 real_from_mpfr (&rr, m, type, GMP_RNDN);
13382 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13383 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13384 but the mpft_t is not, then we underflowed in the
13385 conversion. */
13386 if (real_isfinite (&rr)
13387 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13388 {
13389 REAL_VALUE_TYPE rmode;
13390
13391 real_convert (&rmode, TYPE_MODE (type), &rr);
13392 /* Proceed iff the specified mode can hold the value. */
13393 if (real_identical (&rmode, &rr))
13394 return build_real (type, rmode);
13395 }
13396 }
13397 return NULL_TREE;
13398 }
13399
13400 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13401 number and no overflow/underflow occurred. INEXACT is true if M
13402 was not exactly calculated. TYPE is the tree type for the result.
13403 This function assumes that you cleared the MPFR flags and then
13404 calculated M to see if anything subsequently set a flag prior to
13405 entering this function. Return NULL_TREE if any checks fail, if
13406 FORCE_CONVERT is true, then bypass the checks. */
13407
13408 static tree
13409 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13410 {
13411 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13412 overflow/underflow occurred. If -frounding-math, proceed iff the
13413 result of calling FUNC was exact. */
13414 if (force_convert
13415 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13416 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13417 && (!flag_rounding_math || !inexact)))
13418 {
13419 REAL_VALUE_TYPE re, im;
13420
13421 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13422 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13423 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13424 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13425 but the mpft_t is not, then we underflowed in the
13426 conversion. */
13427 if (force_convert
13428 || (real_isfinite (&re) && real_isfinite (&im)
13429 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13430 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13431 {
13432 REAL_VALUE_TYPE re_mode, im_mode;
13433
13434 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13435 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13436 /* Proceed iff the specified mode can hold the value. */
13437 if (force_convert
13438 || (real_identical (&re_mode, &re)
13439 && real_identical (&im_mode, &im)))
13440 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13441 build_real (TREE_TYPE (type), im_mode));
13442 }
13443 }
13444 return NULL_TREE;
13445 }
13446
13447 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13448 FUNC on it and return the resulting value as a tree with type TYPE.
13449 If MIN and/or MAX are not NULL, then the supplied ARG must be
13450 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13451 acceptable values, otherwise they are not. The mpfr precision is
13452 set to the precision of TYPE. We assume that function FUNC returns
13453 zero if the result could be calculated exactly within the requested
13454 precision. */
13455
13456 static tree
13457 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13458 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13459 bool inclusive)
13460 {
13461 tree result = NULL_TREE;
13462
13463 STRIP_NOPS (arg);
13464
13465 /* To proceed, MPFR must exactly represent the target floating point
13466 format, which only happens when the target base equals two. */
13467 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13468 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13469 {
13470 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13471
13472 if (real_isfinite (ra)
13473 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13474 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13475 {
13476 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13477 const int prec = fmt->p;
13478 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13479 int inexact;
13480 mpfr_t m;
13481
13482 mpfr_init2 (m, prec);
13483 mpfr_from_real (m, ra, GMP_RNDN);
13484 mpfr_clear_flags ();
13485 inexact = func (m, m, rnd);
13486 result = do_mpfr_ckconv (m, type, inexact);
13487 mpfr_clear (m);
13488 }
13489 }
13490
13491 return result;
13492 }
13493
13494 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13495 FUNC on it and return the resulting value as a tree with type TYPE.
13496 The mpfr precision is set to the precision of TYPE. We assume that
13497 function FUNC returns zero if the result could be calculated
13498 exactly within the requested precision. */
13499
13500 static tree
13501 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13502 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13503 {
13504 tree result = NULL_TREE;
13505
13506 STRIP_NOPS (arg1);
13507 STRIP_NOPS (arg2);
13508
13509 /* To proceed, MPFR must exactly represent the target floating point
13510 format, which only happens when the target base equals two. */
13511 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13512 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13513 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13514 {
13515 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13516 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13517
13518 if (real_isfinite (ra1) && real_isfinite (ra2))
13519 {
13520 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13521 const int prec = fmt->p;
13522 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13523 int inexact;
13524 mpfr_t m1, m2;
13525
13526 mpfr_inits2 (prec, m1, m2, NULL);
13527 mpfr_from_real (m1, ra1, GMP_RNDN);
13528 mpfr_from_real (m2, ra2, GMP_RNDN);
13529 mpfr_clear_flags ();
13530 inexact = func (m1, m1, m2, rnd);
13531 result = do_mpfr_ckconv (m1, type, inexact);
13532 mpfr_clears (m1, m2, NULL);
13533 }
13534 }
13535
13536 return result;
13537 }
13538
13539 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13540 FUNC on it and return the resulting value as a tree with type TYPE.
13541 The mpfr precision is set to the precision of TYPE. We assume that
13542 function FUNC returns zero if the result could be calculated
13543 exactly within the requested precision. */
13544
13545 static tree
13546 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13547 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13548 {
13549 tree result = NULL_TREE;
13550
13551 STRIP_NOPS (arg1);
13552 STRIP_NOPS (arg2);
13553 STRIP_NOPS (arg3);
13554
13555 /* To proceed, MPFR must exactly represent the target floating point
13556 format, which only happens when the target base equals two. */
13557 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13558 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13559 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13560 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13561 {
13562 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13563 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13564 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13565
13566 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13567 {
13568 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13569 const int prec = fmt->p;
13570 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13571 int inexact;
13572 mpfr_t m1, m2, m3;
13573
13574 mpfr_inits2 (prec, m1, m2, m3, NULL);
13575 mpfr_from_real (m1, ra1, GMP_RNDN);
13576 mpfr_from_real (m2, ra2, GMP_RNDN);
13577 mpfr_from_real (m3, ra3, GMP_RNDN);
13578 mpfr_clear_flags ();
13579 inexact = func (m1, m1, m2, m3, rnd);
13580 result = do_mpfr_ckconv (m1, type, inexact);
13581 mpfr_clears (m1, m2, m3, NULL);
13582 }
13583 }
13584
13585 return result;
13586 }
13587
13588 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13589 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13590 If ARG_SINP and ARG_COSP are NULL then the result is returned
13591 as a complex value.
13592 The type is taken from the type of ARG and is used for setting the
13593 precision of the calculation and results. */
13594
13595 static tree
13596 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13597 {
13598 tree const type = TREE_TYPE (arg);
13599 tree result = NULL_TREE;
13600
13601 STRIP_NOPS (arg);
13602
13603 /* To proceed, MPFR must exactly represent the target floating point
13604 format, which only happens when the target base equals two. */
13605 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13606 && TREE_CODE (arg) == REAL_CST
13607 && !TREE_OVERFLOW (arg))
13608 {
13609 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13610
13611 if (real_isfinite (ra))
13612 {
13613 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13614 const int prec = fmt->p;
13615 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13616 tree result_s, result_c;
13617 int inexact;
13618 mpfr_t m, ms, mc;
13619
13620 mpfr_inits2 (prec, m, ms, mc, NULL);
13621 mpfr_from_real (m, ra, GMP_RNDN);
13622 mpfr_clear_flags ();
13623 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13624 result_s = do_mpfr_ckconv (ms, type, inexact);
13625 result_c = do_mpfr_ckconv (mc, type, inexact);
13626 mpfr_clears (m, ms, mc, NULL);
13627 if (result_s && result_c)
13628 {
13629 /* If we are to return in a complex value do so. */
13630 if (!arg_sinp && !arg_cosp)
13631 return build_complex (build_complex_type (type),
13632 result_c, result_s);
13633
13634 /* Dereference the sin/cos pointer arguments. */
13635 arg_sinp = build_fold_indirect_ref (arg_sinp);
13636 arg_cosp = build_fold_indirect_ref (arg_cosp);
13637 /* Proceed if valid pointer type were passed in. */
13638 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13639 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13640 {
13641 /* Set the values. */
13642 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13643 result_s);
13644 TREE_SIDE_EFFECTS (result_s) = 1;
13645 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13646 result_c);
13647 TREE_SIDE_EFFECTS (result_c) = 1;
13648 /* Combine the assignments into a compound expr. */
13649 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13650 result_s, result_c));
13651 }
13652 }
13653 }
13654 }
13655 return result;
13656 }
13657
13658 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13659 two-argument mpfr order N Bessel function FUNC on them and return
13660 the resulting value as a tree with type TYPE. The mpfr precision
13661 is set to the precision of TYPE. We assume that function FUNC
13662 returns zero if the result could be calculated exactly within the
13663 requested precision. */
13664 static tree
13665 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13666 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13667 const REAL_VALUE_TYPE *min, bool inclusive)
13668 {
13669 tree result = NULL_TREE;
13670
13671 STRIP_NOPS (arg1);
13672 STRIP_NOPS (arg2);
13673
13674 /* To proceed, MPFR must exactly represent the target floating point
13675 format, which only happens when the target base equals two. */
13676 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13677 && tree_fits_shwi_p (arg1)
13678 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13679 {
13680 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13681 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13682
13683 if (n == (long)n
13684 && real_isfinite (ra)
13685 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13686 {
13687 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13688 const int prec = fmt->p;
13689 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13690 int inexact;
13691 mpfr_t m;
13692
13693 mpfr_init2 (m, prec);
13694 mpfr_from_real (m, ra, GMP_RNDN);
13695 mpfr_clear_flags ();
13696 inexact = func (m, n, m, rnd);
13697 result = do_mpfr_ckconv (m, type, inexact);
13698 mpfr_clear (m);
13699 }
13700 }
13701
13702 return result;
13703 }
13704
13705 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13706 the pointer *(ARG_QUO) and return the result. The type is taken
13707 from the type of ARG0 and is used for setting the precision of the
13708 calculation and results. */
13709
13710 static tree
13711 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13712 {
13713 tree const type = TREE_TYPE (arg0);
13714 tree result = NULL_TREE;
13715
13716 STRIP_NOPS (arg0);
13717 STRIP_NOPS (arg1);
13718
13719 /* To proceed, MPFR must exactly represent the target floating point
13720 format, which only happens when the target base equals two. */
13721 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13722 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13723 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13724 {
13725 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13726 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13727
13728 if (real_isfinite (ra0) && real_isfinite (ra1))
13729 {
13730 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13731 const int prec = fmt->p;
13732 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13733 tree result_rem;
13734 long integer_quo;
13735 mpfr_t m0, m1;
13736
13737 mpfr_inits2 (prec, m0, m1, NULL);
13738 mpfr_from_real (m0, ra0, GMP_RNDN);
13739 mpfr_from_real (m1, ra1, GMP_RNDN);
13740 mpfr_clear_flags ();
13741 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13742 /* Remquo is independent of the rounding mode, so pass
13743 inexact=0 to do_mpfr_ckconv(). */
13744 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13745 mpfr_clears (m0, m1, NULL);
13746 if (result_rem)
13747 {
13748 /* MPFR calculates quo in the host's long so it may
13749 return more bits in quo than the target int can hold
13750 if sizeof(host long) > sizeof(target int). This can
13751 happen even for native compilers in LP64 mode. In
13752 these cases, modulo the quo value with the largest
13753 number that the target int can hold while leaving one
13754 bit for the sign. */
13755 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13756 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13757
13758 /* Dereference the quo pointer argument. */
13759 arg_quo = build_fold_indirect_ref (arg_quo);
13760 /* Proceed iff a valid pointer type was passed in. */
13761 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13762 {
13763 /* Set the value. */
13764 tree result_quo
13765 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13766 build_int_cst (TREE_TYPE (arg_quo),
13767 integer_quo));
13768 TREE_SIDE_EFFECTS (result_quo) = 1;
13769 /* Combine the quo assignment with the rem. */
13770 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13771 result_quo, result_rem));
13772 }
13773 }
13774 }
13775 }
13776 return result;
13777 }
13778
13779 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13780 resulting value as a tree with type TYPE. The mpfr precision is
13781 set to the precision of TYPE. We assume that this mpfr function
13782 returns zero if the result could be calculated exactly within the
13783 requested precision. In addition, the integer pointer represented
13784 by ARG_SG will be dereferenced and set to the appropriate signgam
13785 (-1,1) value. */
13786
13787 static tree
13788 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13789 {
13790 tree result = NULL_TREE;
13791
13792 STRIP_NOPS (arg);
13793
13794 /* To proceed, MPFR must exactly represent the target floating point
13795 format, which only happens when the target base equals two. Also
13796 verify ARG is a constant and that ARG_SG is an int pointer. */
13797 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13798 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13799 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13800 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13801 {
13802 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13803
13804 /* In addition to NaN and Inf, the argument cannot be zero or a
13805 negative integer. */
13806 if (real_isfinite (ra)
13807 && ra->cl != rvc_zero
13808 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13809 {
13810 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13811 const int prec = fmt->p;
13812 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13813 int inexact, sg;
13814 mpfr_t m;
13815 tree result_lg;
13816
13817 mpfr_init2 (m, prec);
13818 mpfr_from_real (m, ra, GMP_RNDN);
13819 mpfr_clear_flags ();
13820 inexact = mpfr_lgamma (m, &sg, m, rnd);
13821 result_lg = do_mpfr_ckconv (m, type, inexact);
13822 mpfr_clear (m);
13823 if (result_lg)
13824 {
13825 tree result_sg;
13826
13827 /* Dereference the arg_sg pointer argument. */
13828 arg_sg = build_fold_indirect_ref (arg_sg);
13829 /* Assign the signgam value into *arg_sg. */
13830 result_sg = fold_build2 (MODIFY_EXPR,
13831 TREE_TYPE (arg_sg), arg_sg,
13832 build_int_cst (TREE_TYPE (arg_sg), sg));
13833 TREE_SIDE_EFFECTS (result_sg) = 1;
13834 /* Combine the signgam assignment with the lgamma result. */
13835 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13836 result_sg, result_lg));
13837 }
13838 }
13839 }
13840
13841 return result;
13842 }
13843
13844 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13845 function FUNC on it and return the resulting value as a tree with
13846 type TYPE. The mpfr precision is set to the precision of TYPE. We
13847 assume that function FUNC returns zero if the result could be
13848 calculated exactly within the requested precision. */
13849
13850 static tree
13851 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13852 {
13853 tree result = NULL_TREE;
13854
13855 STRIP_NOPS (arg);
13856
13857 /* To proceed, MPFR must exactly represent the target floating point
13858 format, which only happens when the target base equals two. */
13859 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13860 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13861 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13862 {
13863 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13864 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13865
13866 if (real_isfinite (re) && real_isfinite (im))
13867 {
13868 const struct real_format *const fmt =
13869 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13870 const int prec = fmt->p;
13871 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13872 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13873 int inexact;
13874 mpc_t m;
13875
13876 mpc_init2 (m, prec);
13877 mpfr_from_real (mpc_realref (m), re, rnd);
13878 mpfr_from_real (mpc_imagref (m), im, rnd);
13879 mpfr_clear_flags ();
13880 inexact = func (m, m, crnd);
13881 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13882 mpc_clear (m);
13883 }
13884 }
13885
13886 return result;
13887 }
13888
13889 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13890 mpc function FUNC on it and return the resulting value as a tree
13891 with type TYPE. The mpfr precision is set to the precision of
13892 TYPE. We assume that function FUNC returns zero if the result
13893 could be calculated exactly within the requested precision. If
13894 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13895 in the arguments and/or results. */
13896
13897 tree
13898 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13899 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13900 {
13901 tree result = NULL_TREE;
13902
13903 STRIP_NOPS (arg0);
13904 STRIP_NOPS (arg1);
13905
13906 /* To proceed, MPFR must exactly represent the target floating point
13907 format, which only happens when the target base equals two. */
13908 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13909 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13910 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13911 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13912 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13913 {
13914 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13915 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13916 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13917 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13918
13919 if (do_nonfinite
13920 || (real_isfinite (re0) && real_isfinite (im0)
13921 && real_isfinite (re1) && real_isfinite (im1)))
13922 {
13923 const struct real_format *const fmt =
13924 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13925 const int prec = fmt->p;
13926 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13927 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13928 int inexact;
13929 mpc_t m0, m1;
13930
13931 mpc_init2 (m0, prec);
13932 mpc_init2 (m1, prec);
13933 mpfr_from_real (mpc_realref (m0), re0, rnd);
13934 mpfr_from_real (mpc_imagref (m0), im0, rnd);
13935 mpfr_from_real (mpc_realref (m1), re1, rnd);
13936 mpfr_from_real (mpc_imagref (m1), im1, rnd);
13937 mpfr_clear_flags ();
13938 inexact = func (m0, m0, m1, crnd);
13939 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13940 mpc_clear (m0);
13941 mpc_clear (m1);
13942 }
13943 }
13944
13945 return result;
13946 }
13947
13948 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13949 a normal call should be emitted rather than expanding the function
13950 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13951
13952 static tree
13953 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13954 {
13955 int nargs = gimple_call_num_args (stmt);
13956
13957 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13958 (nargs > 0
13959 ? gimple_call_arg_ptr (stmt, 0)
13960 : &error_mark_node), fcode);
13961 }
13962
13963 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13964 a normal call should be emitted rather than expanding the function
13965 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13966 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13967 passed as second argument. */
13968
13969 tree
13970 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13971 enum built_in_function fcode)
13972 {
13973 int nargs = gimple_call_num_args (stmt);
13974
13975 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13976 (nargs > 0
13977 ? gimple_call_arg_ptr (stmt, 0)
13978 : &error_mark_node), maxlen, fcode);
13979 }
13980
13981 /* Builtins with folding operations that operate on "..." arguments
13982 need special handling; we need to store the arguments in a convenient
13983 data structure before attempting any folding. Fortunately there are
13984 only a few builtins that fall into this category. FNDECL is the
13985 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13986 result of the function call is ignored. */
13987
13988 static tree
13989 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13990 bool ignore ATTRIBUTE_UNUSED)
13991 {
13992 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13993 tree ret = NULL_TREE;
13994
13995 switch (fcode)
13996 {
13997 case BUILT_IN_SPRINTF_CHK:
13998 case BUILT_IN_VSPRINTF_CHK:
13999 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14000 break;
14001
14002 case BUILT_IN_SNPRINTF_CHK:
14003 case BUILT_IN_VSNPRINTF_CHK:
14004 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14005
14006 default:
14007 break;
14008 }
14009 if (ret)
14010 {
14011 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14012 TREE_NO_WARNING (ret) = 1;
14013 return ret;
14014 }
14015 return NULL_TREE;
14016 }
14017
14018 /* A wrapper function for builtin folding that prevents warnings for
14019 "statement without effect" and the like, caused by removing the
14020 call node earlier than the warning is generated. */
14021
14022 tree
14023 fold_call_stmt (gimple stmt, bool ignore)
14024 {
14025 tree ret = NULL_TREE;
14026 tree fndecl = gimple_call_fndecl (stmt);
14027 location_t loc = gimple_location (stmt);
14028 if (fndecl
14029 && TREE_CODE (fndecl) == FUNCTION_DECL
14030 && DECL_BUILT_IN (fndecl)
14031 && !gimple_call_va_arg_pack_p (stmt))
14032 {
14033 int nargs = gimple_call_num_args (stmt);
14034 tree *args = (nargs > 0
14035 ? gimple_call_arg_ptr (stmt, 0)
14036 : &error_mark_node);
14037
14038 if (avoid_folding_inline_builtin (fndecl))
14039 return NULL_TREE;
14040 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14041 {
14042 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14043 }
14044 else
14045 {
14046 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14047 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14048 if (!ret)
14049 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14050 if (ret)
14051 {
14052 /* Propagate location information from original call to
14053 expansion of builtin. Otherwise things like
14054 maybe_emit_chk_warning, that operate on the expansion
14055 of a builtin, will use the wrong location information. */
14056 if (gimple_has_location (stmt))
14057 {
14058 tree realret = ret;
14059 if (TREE_CODE (ret) == NOP_EXPR)
14060 realret = TREE_OPERAND (ret, 0);
14061 if (CAN_HAVE_LOCATION_P (realret)
14062 && !EXPR_HAS_LOCATION (realret))
14063 SET_EXPR_LOCATION (realret, loc);
14064 return realret;
14065 }
14066 return ret;
14067 }
14068 }
14069 }
14070 return NULL_TREE;
14071 }
14072
14073 /* Look up the function in builtin_decl that corresponds to DECL
14074 and set ASMSPEC as its user assembler name. DECL must be a
14075 function decl that declares a builtin. */
14076
14077 void
14078 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14079 {
14080 tree builtin;
14081 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14082 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14083 && asmspec != 0);
14084
14085 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14086 set_user_assembler_name (builtin, asmspec);
14087 switch (DECL_FUNCTION_CODE (decl))
14088 {
14089 case BUILT_IN_MEMCPY:
14090 init_block_move_fn (asmspec);
14091 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14092 break;
14093 case BUILT_IN_MEMSET:
14094 init_block_clear_fn (asmspec);
14095 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14096 break;
14097 case BUILT_IN_MEMMOVE:
14098 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14099 break;
14100 case BUILT_IN_MEMCMP:
14101 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14102 break;
14103 case BUILT_IN_ABORT:
14104 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14105 break;
14106 case BUILT_IN_FFS:
14107 if (INT_TYPE_SIZE < BITS_PER_WORD)
14108 {
14109 set_user_assembler_libfunc ("ffs", asmspec);
14110 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14111 MODE_INT, 0), "ffs");
14112 }
14113 break;
14114 default:
14115 break;
14116 }
14117 }
14118
14119 /* Return true if DECL is a builtin that expands to a constant or similarly
14120 simple code. */
14121 bool
14122 is_simple_builtin (tree decl)
14123 {
14124 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14125 switch (DECL_FUNCTION_CODE (decl))
14126 {
14127 /* Builtins that expand to constants. */
14128 case BUILT_IN_CONSTANT_P:
14129 case BUILT_IN_EXPECT:
14130 case BUILT_IN_OBJECT_SIZE:
14131 case BUILT_IN_UNREACHABLE:
14132 /* Simple register moves or loads from stack. */
14133 case BUILT_IN_ASSUME_ALIGNED:
14134 case BUILT_IN_RETURN_ADDRESS:
14135 case BUILT_IN_EXTRACT_RETURN_ADDR:
14136 case BUILT_IN_FROB_RETURN_ADDR:
14137 case BUILT_IN_RETURN:
14138 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14139 case BUILT_IN_FRAME_ADDRESS:
14140 case BUILT_IN_VA_END:
14141 case BUILT_IN_STACK_SAVE:
14142 case BUILT_IN_STACK_RESTORE:
14143 /* Exception state returns or moves registers around. */
14144 case BUILT_IN_EH_FILTER:
14145 case BUILT_IN_EH_POINTER:
14146 case BUILT_IN_EH_COPY_VALUES:
14147 return true;
14148
14149 default:
14150 return false;
14151 }
14152
14153 return false;
14154 }
14155
14156 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14157 most probably expanded inline into reasonably simple code. This is a
14158 superset of is_simple_builtin. */
14159 bool
14160 is_inexpensive_builtin (tree decl)
14161 {
14162 if (!decl)
14163 return false;
14164 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14165 return true;
14166 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14167 switch (DECL_FUNCTION_CODE (decl))
14168 {
14169 case BUILT_IN_ABS:
14170 case BUILT_IN_ALLOCA:
14171 case BUILT_IN_ALLOCA_WITH_ALIGN:
14172 case BUILT_IN_BSWAP16:
14173 case BUILT_IN_BSWAP32:
14174 case BUILT_IN_BSWAP64:
14175 case BUILT_IN_CLZ:
14176 case BUILT_IN_CLZIMAX:
14177 case BUILT_IN_CLZL:
14178 case BUILT_IN_CLZLL:
14179 case BUILT_IN_CTZ:
14180 case BUILT_IN_CTZIMAX:
14181 case BUILT_IN_CTZL:
14182 case BUILT_IN_CTZLL:
14183 case BUILT_IN_FFS:
14184 case BUILT_IN_FFSIMAX:
14185 case BUILT_IN_FFSL:
14186 case BUILT_IN_FFSLL:
14187 case BUILT_IN_IMAXABS:
14188 case BUILT_IN_FINITE:
14189 case BUILT_IN_FINITEF:
14190 case BUILT_IN_FINITEL:
14191 case BUILT_IN_FINITED32:
14192 case BUILT_IN_FINITED64:
14193 case BUILT_IN_FINITED128:
14194 case BUILT_IN_FPCLASSIFY:
14195 case BUILT_IN_ISFINITE:
14196 case BUILT_IN_ISINF_SIGN:
14197 case BUILT_IN_ISINF:
14198 case BUILT_IN_ISINFF:
14199 case BUILT_IN_ISINFL:
14200 case BUILT_IN_ISINFD32:
14201 case BUILT_IN_ISINFD64:
14202 case BUILT_IN_ISINFD128:
14203 case BUILT_IN_ISNAN:
14204 case BUILT_IN_ISNANF:
14205 case BUILT_IN_ISNANL:
14206 case BUILT_IN_ISNAND32:
14207 case BUILT_IN_ISNAND64:
14208 case BUILT_IN_ISNAND128:
14209 case BUILT_IN_ISNORMAL:
14210 case BUILT_IN_ISGREATER:
14211 case BUILT_IN_ISGREATEREQUAL:
14212 case BUILT_IN_ISLESS:
14213 case BUILT_IN_ISLESSEQUAL:
14214 case BUILT_IN_ISLESSGREATER:
14215 case BUILT_IN_ISUNORDERED:
14216 case BUILT_IN_VA_ARG_PACK:
14217 case BUILT_IN_VA_ARG_PACK_LEN:
14218 case BUILT_IN_VA_COPY:
14219 case BUILT_IN_TRAP:
14220 case BUILT_IN_SAVEREGS:
14221 case BUILT_IN_POPCOUNTL:
14222 case BUILT_IN_POPCOUNTLL:
14223 case BUILT_IN_POPCOUNTIMAX:
14224 case BUILT_IN_POPCOUNT:
14225 case BUILT_IN_PARITYL:
14226 case BUILT_IN_PARITYLL:
14227 case BUILT_IN_PARITYIMAX:
14228 case BUILT_IN_PARITY:
14229 case BUILT_IN_LABS:
14230 case BUILT_IN_LLABS:
14231 case BUILT_IN_PREFETCH:
14232 return true;
14233
14234 default:
14235 return is_simple_builtin (decl);
14236 }
14237
14238 return false;
14239 }