ubsan.c: Include tree-ssanames.h, asan.h and gimplify-me.h.
[gcc.git] / gcc / builtins.c
1 /* Expand builtin functions.
2 Copyright (C) 1988-2013 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_expect (location_t, tree, tree);
144 static tree fold_builtin_classify_type (tree);
145 static tree fold_builtin_strlen (location_t, tree, tree);
146 static tree fold_builtin_inf (location_t, tree, int);
147 static tree fold_builtin_nan (tree, tree, int);
148 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
149 static bool validate_arg (const_tree, enum tree_code code);
150 static bool integer_valued_real_p (tree);
151 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
152 static bool readonly_data_expr (tree);
153 static rtx expand_builtin_fabs (tree, rtx, rtx);
154 static rtx expand_builtin_signbit (tree, rtx);
155 static tree fold_builtin_sqrt (location_t, tree, tree);
156 static tree fold_builtin_cbrt (location_t, tree, tree);
157 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
158 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
159 static tree fold_builtin_cos (location_t, tree, tree, tree);
160 static tree fold_builtin_cosh (location_t, tree, tree, tree);
161 static tree fold_builtin_tan (tree, tree);
162 static tree fold_builtin_trunc (location_t, tree, tree);
163 static tree fold_builtin_floor (location_t, tree, tree);
164 static tree fold_builtin_ceil (location_t, tree, tree);
165 static tree fold_builtin_round (location_t, tree, tree);
166 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
167 static tree fold_builtin_bitop (tree, tree);
168 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
169 static tree fold_builtin_strchr (location_t, tree, tree, tree);
170 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
172 static tree fold_builtin_strcmp (location_t, tree, tree);
173 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
174 static tree fold_builtin_signbit (location_t, tree, tree);
175 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
176 static tree fold_builtin_isascii (location_t, tree);
177 static tree fold_builtin_toascii (location_t, tree);
178 static tree fold_builtin_isdigit (location_t, tree);
179 static tree fold_builtin_fabs (location_t, tree, tree);
180 static tree fold_builtin_abs (location_t, tree, tree);
181 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
182 enum tree_code);
183 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
184 static tree fold_builtin_0 (location_t, tree, bool);
185 static tree fold_builtin_1 (location_t, tree, tree, bool);
186 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
187 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
188 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
189 static tree fold_builtin_varargs (location_t, tree, tree, bool);
190
191 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
192 static tree fold_builtin_strstr (location_t, tree, tree, tree);
193 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
194 static tree fold_builtin_strcat (location_t, tree, tree);
195 static tree fold_builtin_strncat (location_t, tree, tree, tree);
196 static tree fold_builtin_strspn (location_t, tree, tree);
197 static tree fold_builtin_strcspn (location_t, tree, tree);
198 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
199 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
200
201 static rtx expand_builtin_object_size (tree);
202 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
203 enum built_in_function);
204 static void maybe_emit_chk_warning (tree, enum built_in_function);
205 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
206 static void maybe_emit_free_warning (tree);
207 static tree fold_builtin_object_size (tree, tree);
208 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
209 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
210 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
211 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
212 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
213 enum built_in_function);
214 static bool init_target_chars (void);
215
216 static unsigned HOST_WIDE_INT target_newline;
217 static unsigned HOST_WIDE_INT target_percent;
218 static unsigned HOST_WIDE_INT target_c;
219 static unsigned HOST_WIDE_INT target_s;
220 static char target_percent_c[3];
221 static char target_percent_s[3];
222 static char target_percent_s_newline[4];
223 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
224 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
225 static tree do_mpfr_arg2 (tree, tree, tree,
226 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
227 static tree do_mpfr_arg3 (tree, tree, tree, tree,
228 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
229 static tree do_mpfr_sincos (tree, tree, tree);
230 static tree do_mpfr_bessel_n (tree, tree, tree,
231 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
232 const REAL_VALUE_TYPE *, bool);
233 static tree do_mpfr_remquo (tree, tree, tree);
234 static tree do_mpfr_lgamma_r (tree, tree, tree);
235 static void expand_builtin_sync_synchronize (void);
236
237 /* Return true if NAME starts with __builtin_ or __sync_. */
238
239 static bool
240 is_builtin_name (const char *name)
241 {
242 if (strncmp (name, "__builtin_", 10) == 0)
243 return true;
244 if (strncmp (name, "__sync_", 7) == 0)
245 return true;
246 if (strncmp (name, "__atomic_", 9) == 0)
247 return true;
248 if (flag_enable_cilkplus
249 && (!strcmp (name, "__cilkrts_detach")
250 || !strcmp (name, "__cilkrts_pop_frame")))
251 return true;
252 return false;
253 }
254
255
256 /* Return true if DECL is a function symbol representing a built-in. */
257
258 bool
259 is_builtin_fn (tree decl)
260 {
261 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
262 }
263
264 /* By default we assume that c99 functions are present at the runtime,
265 but sincos is not. */
266 bool
267 default_libc_has_function (enum function_class fn_class)
268 {
269 if (fn_class == function_c94
270 || fn_class == function_c99_misc
271 || fn_class == function_c99_math_complex)
272 return true;
273
274 return false;
275 }
276
277 bool
278 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
279 {
280 return true;
281 }
282
283 bool
284 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
285 {
286 return false;
287 }
288
289 /* Return true if NODE should be considered for inline expansion regardless
290 of the optimization level. This means whenever a function is invoked with
291 its "internal" name, which normally contains the prefix "__builtin". */
292
293 static bool
294 called_as_built_in (tree node)
295 {
296 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
297 we want the name used to call the function, not the name it
298 will have. */
299 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
300 return is_builtin_name (name);
301 }
302
303 /* Compute values M and N such that M divides (address of EXP - N) and such
304 that N < M. If these numbers can be determined, store M in alignp and N in
305 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
306 *alignp and any bit-offset to *bitposp.
307
308 Note that the address (and thus the alignment) computed here is based
309 on the address to which a symbol resolves, whereas DECL_ALIGN is based
310 on the address at which an object is actually located. These two
311 addresses are not always the same. For example, on ARM targets,
312 the address &foo of a Thumb function foo() has the lowest bit set,
313 whereas foo() itself starts on an even address.
314
315 If ADDR_P is true we are taking the address of the memory reference EXP
316 and thus cannot rely on the access taking place. */
317
318 static bool
319 get_object_alignment_2 (tree exp, unsigned int *alignp,
320 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
321 {
322 HOST_WIDE_INT bitsize, bitpos;
323 tree offset;
324 enum machine_mode mode;
325 int unsignedp, volatilep;
326 unsigned int align = BITS_PER_UNIT;
327 bool known_alignment = false;
328
329 /* Get the innermost object and the constant (bitpos) and possibly
330 variable (offset) offset of the access. */
331 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
332 &mode, &unsignedp, &volatilep, true);
333
334 /* Extract alignment information from the innermost object and
335 possibly adjust bitpos and offset. */
336 if (TREE_CODE (exp) == FUNCTION_DECL)
337 {
338 /* Function addresses can encode extra information besides their
339 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
340 allows the low bit to be used as a virtual bit, we know
341 that the address itself must be at least 2-byte aligned. */
342 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
343 align = 2 * BITS_PER_UNIT;
344 }
345 else if (TREE_CODE (exp) == LABEL_DECL)
346 ;
347 else if (TREE_CODE (exp) == CONST_DECL)
348 {
349 /* The alignment of a CONST_DECL is determined by its initializer. */
350 exp = DECL_INITIAL (exp);
351 align = TYPE_ALIGN (TREE_TYPE (exp));
352 #ifdef CONSTANT_ALIGNMENT
353 if (CONSTANT_CLASS_P (exp))
354 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
355 #endif
356 known_alignment = true;
357 }
358 else if (DECL_P (exp))
359 {
360 align = DECL_ALIGN (exp);
361 known_alignment = true;
362 }
363 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
364 {
365 align = TYPE_ALIGN (TREE_TYPE (exp));
366 }
367 else if (TREE_CODE (exp) == INDIRECT_REF
368 || TREE_CODE (exp) == MEM_REF
369 || TREE_CODE (exp) == TARGET_MEM_REF)
370 {
371 tree addr = TREE_OPERAND (exp, 0);
372 unsigned ptr_align;
373 unsigned HOST_WIDE_INT ptr_bitpos;
374
375 if (TREE_CODE (addr) == BIT_AND_EXPR
376 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
377 {
378 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
379 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
380 align *= BITS_PER_UNIT;
381 addr = TREE_OPERAND (addr, 0);
382 }
383
384 known_alignment
385 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
386 align = MAX (ptr_align, align);
387
388 /* The alignment of the pointer operand in a TARGET_MEM_REF
389 has to take the variable offset parts into account. */
390 if (TREE_CODE (exp) == TARGET_MEM_REF)
391 {
392 if (TMR_INDEX (exp))
393 {
394 unsigned HOST_WIDE_INT step = 1;
395 if (TMR_STEP (exp))
396 step = TREE_INT_CST_LOW (TMR_STEP (exp));
397 align = MIN (align, (step & -step) * BITS_PER_UNIT);
398 }
399 if (TMR_INDEX2 (exp))
400 align = BITS_PER_UNIT;
401 known_alignment = false;
402 }
403
404 /* When EXP is an actual memory reference then we can use
405 TYPE_ALIGN of a pointer indirection to derive alignment.
406 Do so only if get_pointer_alignment_1 did not reveal absolute
407 alignment knowledge and if using that alignment would
408 improve the situation. */
409 if (!addr_p && !known_alignment
410 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
411 align = TYPE_ALIGN (TREE_TYPE (exp));
412 else
413 {
414 /* Else adjust bitpos accordingly. */
415 bitpos += ptr_bitpos;
416 if (TREE_CODE (exp) == MEM_REF
417 || TREE_CODE (exp) == TARGET_MEM_REF)
418 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
419 }
420 }
421 else if (TREE_CODE (exp) == STRING_CST)
422 {
423 /* STRING_CST are the only constant objects we allow to be not
424 wrapped inside a CONST_DECL. */
425 align = TYPE_ALIGN (TREE_TYPE (exp));
426 #ifdef CONSTANT_ALIGNMENT
427 if (CONSTANT_CLASS_P (exp))
428 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
429 #endif
430 known_alignment = true;
431 }
432
433 /* If there is a non-constant offset part extract the maximum
434 alignment that can prevail. */
435 if (offset)
436 {
437 int trailing_zeros = tree_ctz (offset);
438 if (trailing_zeros < HOST_BITS_PER_INT)
439 {
440 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
441 if (inner)
442 align = MIN (align, inner);
443 }
444 }
445
446 *alignp = align;
447 *bitposp = bitpos & (*alignp - 1);
448 return known_alignment;
449 }
450
451 /* For a memory reference expression EXP compute values M and N such that M
452 divides (&EXP - N) and such that N < M. If these numbers can be determined,
453 store M in alignp and N in *BITPOSP and return true. Otherwise return false
454 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
455
456 bool
457 get_object_alignment_1 (tree exp, unsigned int *alignp,
458 unsigned HOST_WIDE_INT *bitposp)
459 {
460 return get_object_alignment_2 (exp, alignp, bitposp, false);
461 }
462
463 /* Return the alignment in bits of EXP, an object. */
464
465 unsigned int
466 get_object_alignment (tree exp)
467 {
468 unsigned HOST_WIDE_INT bitpos = 0;
469 unsigned int align;
470
471 get_object_alignment_1 (exp, &align, &bitpos);
472
473 /* align and bitpos now specify known low bits of the pointer.
474 ptr & (align - 1) == bitpos. */
475
476 if (bitpos != 0)
477 align = (bitpos & -bitpos);
478 return align;
479 }
480
481 /* For a pointer valued expression EXP compute values M and N such that M
482 divides (EXP - N) and such that N < M. If these numbers can be determined,
483 store M in alignp and N in *BITPOSP and return true. Return false if
484 the results are just a conservative approximation.
485
486 If EXP is not a pointer, false is returned too. */
487
488 bool
489 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
490 unsigned HOST_WIDE_INT *bitposp)
491 {
492 STRIP_NOPS (exp);
493
494 if (TREE_CODE (exp) == ADDR_EXPR)
495 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
496 alignp, bitposp, true);
497 else if (TREE_CODE (exp) == SSA_NAME
498 && POINTER_TYPE_P (TREE_TYPE (exp)))
499 {
500 unsigned int ptr_align, ptr_misalign;
501 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
502
503 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
504 {
505 *bitposp = ptr_misalign * BITS_PER_UNIT;
506 *alignp = ptr_align * BITS_PER_UNIT;
507 /* We cannot really tell whether this result is an approximation. */
508 return true;
509 }
510 else
511 {
512 *bitposp = 0;
513 *alignp = BITS_PER_UNIT;
514 return false;
515 }
516 }
517 else if (TREE_CODE (exp) == INTEGER_CST)
518 {
519 *alignp = BIGGEST_ALIGNMENT;
520 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
521 & (BIGGEST_ALIGNMENT - 1));
522 return true;
523 }
524
525 *bitposp = 0;
526 *alignp = BITS_PER_UNIT;
527 return false;
528 }
529
530 /* Return the alignment in bits of EXP, a pointer valued expression.
531 The alignment returned is, by default, the alignment of the thing that
532 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
533
534 Otherwise, look at the expression to see if we can do better, i.e., if the
535 expression is actually pointing at an object whose alignment is tighter. */
536
537 unsigned int
538 get_pointer_alignment (tree exp)
539 {
540 unsigned HOST_WIDE_INT bitpos = 0;
541 unsigned int align;
542
543 get_pointer_alignment_1 (exp, &align, &bitpos);
544
545 /* align and bitpos now specify known low bits of the pointer.
546 ptr & (align - 1) == bitpos. */
547
548 if (bitpos != 0)
549 align = (bitpos & -bitpos);
550
551 return align;
552 }
553
554 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
555 way, because it could contain a zero byte in the middle.
556 TREE_STRING_LENGTH is the size of the character array, not the string.
557
558 ONLY_VALUE should be nonzero if the result is not going to be emitted
559 into the instruction stream and zero if it is going to be expanded.
560 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
561 is returned, otherwise NULL, since
562 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
563 evaluate the side-effects.
564
565 The value returned is of type `ssizetype'.
566
567 Unfortunately, string_constant can't access the values of const char
568 arrays with initializers, so neither can we do so here. */
569
570 tree
571 c_strlen (tree src, int only_value)
572 {
573 tree offset_node;
574 HOST_WIDE_INT offset;
575 int max;
576 const char *ptr;
577 location_t loc;
578
579 STRIP_NOPS (src);
580 if (TREE_CODE (src) == COND_EXPR
581 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
582 {
583 tree len1, len2;
584
585 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
586 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
587 if (tree_int_cst_equal (len1, len2))
588 return len1;
589 }
590
591 if (TREE_CODE (src) == COMPOUND_EXPR
592 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
593 return c_strlen (TREE_OPERAND (src, 1), only_value);
594
595 loc = EXPR_LOC_OR_LOC (src, input_location);
596
597 src = string_constant (src, &offset_node);
598 if (src == 0)
599 return NULL_TREE;
600
601 max = TREE_STRING_LENGTH (src) - 1;
602 ptr = TREE_STRING_POINTER (src);
603
604 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
605 {
606 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
607 compute the offset to the following null if we don't know where to
608 start searching for it. */
609 int i;
610
611 for (i = 0; i < max; i++)
612 if (ptr[i] == 0)
613 return NULL_TREE;
614
615 /* We don't know the starting offset, but we do know that the string
616 has no internal zero bytes. We can assume that the offset falls
617 within the bounds of the string; otherwise, the programmer deserves
618 what he gets. Subtract the offset from the length of the string,
619 and return that. This would perhaps not be valid if we were dealing
620 with named arrays in addition to literal string constants. */
621
622 return size_diffop_loc (loc, size_int (max), offset_node);
623 }
624
625 /* We have a known offset into the string. Start searching there for
626 a null character if we can represent it as a single HOST_WIDE_INT. */
627 if (offset_node == 0)
628 offset = 0;
629 else if (! tree_fits_shwi_p (offset_node))
630 offset = -1;
631 else
632 offset = tree_to_shwi (offset_node);
633
634 /* If the offset is known to be out of bounds, warn, and call strlen at
635 runtime. */
636 if (offset < 0 || offset > max)
637 {
638 /* Suppress multiple warnings for propagated constant strings. */
639 if (! TREE_NO_WARNING (src))
640 {
641 warning_at (loc, 0, "offset outside bounds of constant string");
642 TREE_NO_WARNING (src) = 1;
643 }
644 return NULL_TREE;
645 }
646
647 /* Use strlen to search for the first zero byte. Since any strings
648 constructed with build_string will have nulls appended, we win even
649 if we get handed something like (char[4])"abcd".
650
651 Since OFFSET is our starting index into the string, no further
652 calculation is needed. */
653 return ssize_int (strlen (ptr + offset));
654 }
655
656 /* Return a char pointer for a C string if it is a string constant
657 or sum of string constant and integer constant. */
658
659 static const char *
660 c_getstr (tree src)
661 {
662 tree offset_node;
663
664 src = string_constant (src, &offset_node);
665 if (src == 0)
666 return 0;
667
668 if (offset_node == 0)
669 return TREE_STRING_POINTER (src);
670 else if (!tree_fits_uhwi_p (offset_node)
671 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
672 return 0;
673
674 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
675 }
676
677 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
678 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
679
680 static rtx
681 c_readstr (const char *str, enum machine_mode mode)
682 {
683 HOST_WIDE_INT c[2];
684 HOST_WIDE_INT ch;
685 unsigned int i, j;
686
687 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
688
689 c[0] = 0;
690 c[1] = 0;
691 ch = 1;
692 for (i = 0; i < GET_MODE_SIZE (mode); i++)
693 {
694 j = i;
695 if (WORDS_BIG_ENDIAN)
696 j = GET_MODE_SIZE (mode) - i - 1;
697 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
698 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
699 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
700 j *= BITS_PER_UNIT;
701 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
702
703 if (ch)
704 ch = (unsigned char) str[i];
705 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
706 }
707 return immed_double_const (c[0], c[1], mode);
708 }
709
710 /* Cast a target constant CST to target CHAR and if that value fits into
711 host char type, return zero and put that value into variable pointed to by
712 P. */
713
714 static int
715 target_char_cast (tree cst, char *p)
716 {
717 unsigned HOST_WIDE_INT val, hostval;
718
719 if (TREE_CODE (cst) != INTEGER_CST
720 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
721 return 1;
722
723 val = TREE_INT_CST_LOW (cst);
724 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
725 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
726
727 hostval = val;
728 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
729 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
730
731 if (val != hostval)
732 return 1;
733
734 *p = hostval;
735 return 0;
736 }
737
738 /* Similar to save_expr, but assumes that arbitrary code is not executed
739 in between the multiple evaluations. In particular, we assume that a
740 non-addressable local variable will not be modified. */
741
742 static tree
743 builtin_save_expr (tree exp)
744 {
745 if (TREE_CODE (exp) == SSA_NAME
746 || (TREE_ADDRESSABLE (exp) == 0
747 && (TREE_CODE (exp) == PARM_DECL
748 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
749 return exp;
750
751 return save_expr (exp);
752 }
753
754 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
755 times to get the address of either a higher stack frame, or a return
756 address located within it (depending on FNDECL_CODE). */
757
758 static rtx
759 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
760 {
761 int i;
762
763 #ifdef INITIAL_FRAME_ADDRESS_RTX
764 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
765 #else
766 rtx tem;
767
768 /* For a zero count with __builtin_return_address, we don't care what
769 frame address we return, because target-specific definitions will
770 override us. Therefore frame pointer elimination is OK, and using
771 the soft frame pointer is OK.
772
773 For a nonzero count, or a zero count with __builtin_frame_address,
774 we require a stable offset from the current frame pointer to the
775 previous one, so we must use the hard frame pointer, and
776 we must disable frame pointer elimination. */
777 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
778 tem = frame_pointer_rtx;
779 else
780 {
781 tem = hard_frame_pointer_rtx;
782
783 /* Tell reload not to eliminate the frame pointer. */
784 crtl->accesses_prior_frames = 1;
785 }
786 #endif
787
788 /* Some machines need special handling before we can access
789 arbitrary frames. For example, on the SPARC, we must first flush
790 all register windows to the stack. */
791 #ifdef SETUP_FRAME_ADDRESSES
792 if (count > 0)
793 SETUP_FRAME_ADDRESSES ();
794 #endif
795
796 /* On the SPARC, the return address is not in the frame, it is in a
797 register. There is no way to access it off of the current frame
798 pointer, but it can be accessed off the previous frame pointer by
799 reading the value from the register window save area. */
800 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
801 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
802 count--;
803 #endif
804
805 /* Scan back COUNT frames to the specified frame. */
806 for (i = 0; i < count; i++)
807 {
808 /* Assume the dynamic chain pointer is in the word that the
809 frame address points to, unless otherwise specified. */
810 #ifdef DYNAMIC_CHAIN_ADDRESS
811 tem = DYNAMIC_CHAIN_ADDRESS (tem);
812 #endif
813 tem = memory_address (Pmode, tem);
814 tem = gen_frame_mem (Pmode, tem);
815 tem = copy_to_reg (tem);
816 }
817
818 /* For __builtin_frame_address, return what we've got. But, on
819 the SPARC for example, we may have to add a bias. */
820 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
821 #ifdef FRAME_ADDR_RTX
822 return FRAME_ADDR_RTX (tem);
823 #else
824 return tem;
825 #endif
826
827 /* For __builtin_return_address, get the return address from that frame. */
828 #ifdef RETURN_ADDR_RTX
829 tem = RETURN_ADDR_RTX (count, tem);
830 #else
831 tem = memory_address (Pmode,
832 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
833 tem = gen_frame_mem (Pmode, tem);
834 #endif
835 return tem;
836 }
837
838 /* Alias set used for setjmp buffer. */
839 static alias_set_type setjmp_alias_set = -1;
840
841 /* Construct the leading half of a __builtin_setjmp call. Control will
842 return to RECEIVER_LABEL. This is also called directly by the SJLJ
843 exception handling code. */
844
845 void
846 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
847 {
848 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
849 rtx stack_save;
850 rtx mem;
851
852 if (setjmp_alias_set == -1)
853 setjmp_alias_set = new_alias_set ();
854
855 buf_addr = convert_memory_address (Pmode, buf_addr);
856
857 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
858
859 /* We store the frame pointer and the address of receiver_label in
860 the buffer and use the rest of it for the stack save area, which
861 is machine-dependent. */
862
863 mem = gen_rtx_MEM (Pmode, buf_addr);
864 set_mem_alias_set (mem, setjmp_alias_set);
865 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
866
867 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
868 GET_MODE_SIZE (Pmode))),
869 set_mem_alias_set (mem, setjmp_alias_set);
870
871 emit_move_insn (validize_mem (mem),
872 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
873
874 stack_save = gen_rtx_MEM (sa_mode,
875 plus_constant (Pmode, buf_addr,
876 2 * GET_MODE_SIZE (Pmode)));
877 set_mem_alias_set (stack_save, setjmp_alias_set);
878 emit_stack_save (SAVE_NONLOCAL, &stack_save);
879
880 /* If there is further processing to do, do it. */
881 #ifdef HAVE_builtin_setjmp_setup
882 if (HAVE_builtin_setjmp_setup)
883 emit_insn (gen_builtin_setjmp_setup (buf_addr));
884 #endif
885
886 /* We have a nonlocal label. */
887 cfun->has_nonlocal_label = 1;
888 }
889
890 /* Construct the trailing part of a __builtin_setjmp call. This is
891 also called directly by the SJLJ exception handling code.
892 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
893
894 void
895 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
896 {
897 rtx chain;
898
899 /* Mark the FP as used when we get here, so we have to make sure it's
900 marked as used by this function. */
901 emit_use (hard_frame_pointer_rtx);
902
903 /* Mark the static chain as clobbered here so life information
904 doesn't get messed up for it. */
905 chain = targetm.calls.static_chain (current_function_decl, true);
906 if (chain && REG_P (chain))
907 emit_clobber (chain);
908
909 /* Now put in the code to restore the frame pointer, and argument
910 pointer, if needed. */
911 #ifdef HAVE_nonlocal_goto
912 if (! HAVE_nonlocal_goto)
913 #endif
914 /* First adjust our frame pointer to its actual value. It was
915 previously set to the start of the virtual area corresponding to
916 the stacked variables when we branched here and now needs to be
917 adjusted to the actual hardware fp value.
918
919 Assignments to virtual registers are converted by
920 instantiate_virtual_regs into the corresponding assignment
921 to the underlying register (fp in this case) that makes
922 the original assignment true.
923 So the following insn will actually be decrementing fp by
924 STARTING_FRAME_OFFSET. */
925 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
926
927 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
928 if (fixed_regs[ARG_POINTER_REGNUM])
929 {
930 #ifdef ELIMINABLE_REGS
931 /* If the argument pointer can be eliminated in favor of the
932 frame pointer, we don't need to restore it. We assume here
933 that if such an elimination is present, it can always be used.
934 This is the case on all known machines; if we don't make this
935 assumption, we do unnecessary saving on many machines. */
936 size_t i;
937 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
938
939 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
940 if (elim_regs[i].from == ARG_POINTER_REGNUM
941 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
942 break;
943
944 if (i == ARRAY_SIZE (elim_regs))
945 #endif
946 {
947 /* Now restore our arg pointer from the address at which it
948 was saved in our stack frame. */
949 emit_move_insn (crtl->args.internal_arg_pointer,
950 copy_to_reg (get_arg_pointer_save_area ()));
951 }
952 }
953 #endif
954
955 #ifdef HAVE_builtin_setjmp_receiver
956 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
957 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
958 else
959 #endif
960 #ifdef HAVE_nonlocal_goto_receiver
961 if (HAVE_nonlocal_goto_receiver)
962 emit_insn (gen_nonlocal_goto_receiver ());
963 else
964 #endif
965 { /* Nothing */ }
966
967 /* We must not allow the code we just generated to be reordered by
968 scheduling. Specifically, the update of the frame pointer must
969 happen immediately, not later. Similarly, we must block
970 (frame-related) register values to be used across this code. */
971 emit_insn (gen_blockage ());
972 }
973
974 /* __builtin_longjmp is passed a pointer to an array of five words (not
975 all will be used on all machines). It operates similarly to the C
976 library function of the same name, but is more efficient. Much of
977 the code below is copied from the handling of non-local gotos. */
978
979 static void
980 expand_builtin_longjmp (rtx buf_addr, rtx value)
981 {
982 rtx fp, lab, stack, insn, last;
983 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
984
985 /* DRAP is needed for stack realign if longjmp is expanded to current
986 function */
987 if (SUPPORTS_STACK_ALIGNMENT)
988 crtl->need_drap = true;
989
990 if (setjmp_alias_set == -1)
991 setjmp_alias_set = new_alias_set ();
992
993 buf_addr = convert_memory_address (Pmode, buf_addr);
994
995 buf_addr = force_reg (Pmode, buf_addr);
996
997 /* We require that the user must pass a second argument of 1, because
998 that is what builtin_setjmp will return. */
999 gcc_assert (value == const1_rtx);
1000
1001 last = get_last_insn ();
1002 #ifdef HAVE_builtin_longjmp
1003 if (HAVE_builtin_longjmp)
1004 emit_insn (gen_builtin_longjmp (buf_addr));
1005 else
1006 #endif
1007 {
1008 fp = gen_rtx_MEM (Pmode, buf_addr);
1009 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1010 GET_MODE_SIZE (Pmode)));
1011
1012 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1013 2 * GET_MODE_SIZE (Pmode)));
1014 set_mem_alias_set (fp, setjmp_alias_set);
1015 set_mem_alias_set (lab, setjmp_alias_set);
1016 set_mem_alias_set (stack, setjmp_alias_set);
1017
1018 /* Pick up FP, label, and SP from the block and jump. This code is
1019 from expand_goto in stmt.c; see there for detailed comments. */
1020 #ifdef HAVE_nonlocal_goto
1021 if (HAVE_nonlocal_goto)
1022 /* We have to pass a value to the nonlocal_goto pattern that will
1023 get copied into the static_chain pointer, but it does not matter
1024 what that value is, because builtin_setjmp does not use it. */
1025 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1026 else
1027 #endif
1028 {
1029 lab = copy_to_reg (lab);
1030
1031 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1032 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1033
1034 emit_move_insn (hard_frame_pointer_rtx, fp);
1035 emit_stack_restore (SAVE_NONLOCAL, stack);
1036
1037 emit_use (hard_frame_pointer_rtx);
1038 emit_use (stack_pointer_rtx);
1039 emit_indirect_jump (lab);
1040 }
1041 }
1042
1043 /* Search backwards and mark the jump insn as a non-local goto.
1044 Note that this precludes the use of __builtin_longjmp to a
1045 __builtin_setjmp target in the same function. However, we've
1046 already cautioned the user that these functions are for
1047 internal exception handling use only. */
1048 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1049 {
1050 gcc_assert (insn != last);
1051
1052 if (JUMP_P (insn))
1053 {
1054 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1055 break;
1056 }
1057 else if (CALL_P (insn))
1058 break;
1059 }
1060 }
1061
1062 static inline bool
1063 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1064 {
1065 return (iter->i < iter->n);
1066 }
1067
1068 /* This function validates the types of a function call argument list
1069 against a specified list of tree_codes. If the last specifier is a 0,
1070 that represents an ellipses, otherwise the last specifier must be a
1071 VOID_TYPE. */
1072
1073 static bool
1074 validate_arglist (const_tree callexpr, ...)
1075 {
1076 enum tree_code code;
1077 bool res = 0;
1078 va_list ap;
1079 const_call_expr_arg_iterator iter;
1080 const_tree arg;
1081
1082 va_start (ap, callexpr);
1083 init_const_call_expr_arg_iterator (callexpr, &iter);
1084
1085 do
1086 {
1087 code = (enum tree_code) va_arg (ap, int);
1088 switch (code)
1089 {
1090 case 0:
1091 /* This signifies an ellipses, any further arguments are all ok. */
1092 res = true;
1093 goto end;
1094 case VOID_TYPE:
1095 /* This signifies an endlink, if no arguments remain, return
1096 true, otherwise return false. */
1097 res = !more_const_call_expr_args_p (&iter);
1098 goto end;
1099 default:
1100 /* If no parameters remain or the parameter's code does not
1101 match the specified code, return false. Otherwise continue
1102 checking any remaining arguments. */
1103 arg = next_const_call_expr_arg (&iter);
1104 if (!validate_arg (arg, code))
1105 goto end;
1106 break;
1107 }
1108 }
1109 while (1);
1110
1111 /* We need gotos here since we can only have one VA_CLOSE in a
1112 function. */
1113 end: ;
1114 va_end (ap);
1115
1116 return res;
1117 }
1118
1119 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1120 and the address of the save area. */
1121
1122 static rtx
1123 expand_builtin_nonlocal_goto (tree exp)
1124 {
1125 tree t_label, t_save_area;
1126 rtx r_label, r_save_area, r_fp, r_sp, insn;
1127
1128 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1129 return NULL_RTX;
1130
1131 t_label = CALL_EXPR_ARG (exp, 0);
1132 t_save_area = CALL_EXPR_ARG (exp, 1);
1133
1134 r_label = expand_normal (t_label);
1135 r_label = convert_memory_address (Pmode, r_label);
1136 r_save_area = expand_normal (t_save_area);
1137 r_save_area = convert_memory_address (Pmode, r_save_area);
1138 /* Copy the address of the save location to a register just in case it was
1139 based on the frame pointer. */
1140 r_save_area = copy_to_reg (r_save_area);
1141 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1142 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1143 plus_constant (Pmode, r_save_area,
1144 GET_MODE_SIZE (Pmode)));
1145
1146 crtl->has_nonlocal_goto = 1;
1147
1148 #ifdef HAVE_nonlocal_goto
1149 /* ??? We no longer need to pass the static chain value, afaik. */
1150 if (HAVE_nonlocal_goto)
1151 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1152 else
1153 #endif
1154 {
1155 r_label = copy_to_reg (r_label);
1156
1157 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1158 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1159
1160 /* Restore frame pointer for containing function. */
1161 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1162 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1163
1164 /* USE of hard_frame_pointer_rtx added for consistency;
1165 not clear if really needed. */
1166 emit_use (hard_frame_pointer_rtx);
1167 emit_use (stack_pointer_rtx);
1168
1169 /* If the architecture is using a GP register, we must
1170 conservatively assume that the target function makes use of it.
1171 The prologue of functions with nonlocal gotos must therefore
1172 initialize the GP register to the appropriate value, and we
1173 must then make sure that this value is live at the point
1174 of the jump. (Note that this doesn't necessarily apply
1175 to targets with a nonlocal_goto pattern; they are free
1176 to implement it in their own way. Note also that this is
1177 a no-op if the GP register is a global invariant.) */
1178 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1179 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1180 emit_use (pic_offset_table_rtx);
1181
1182 emit_indirect_jump (r_label);
1183 }
1184
1185 /* Search backwards to the jump insn and mark it as a
1186 non-local goto. */
1187 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1188 {
1189 if (JUMP_P (insn))
1190 {
1191 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1192 break;
1193 }
1194 else if (CALL_P (insn))
1195 break;
1196 }
1197
1198 return const0_rtx;
1199 }
1200
1201 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1202 (not all will be used on all machines) that was passed to __builtin_setjmp.
1203 It updates the stack pointer in that block to correspond to the current
1204 stack pointer. */
1205
1206 static void
1207 expand_builtin_update_setjmp_buf (rtx buf_addr)
1208 {
1209 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1210 rtx stack_save
1211 = gen_rtx_MEM (sa_mode,
1212 memory_address
1213 (sa_mode,
1214 plus_constant (Pmode, buf_addr,
1215 2 * GET_MODE_SIZE (Pmode))));
1216
1217 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1218 }
1219
1220 /* Expand a call to __builtin_prefetch. For a target that does not support
1221 data prefetch, evaluate the memory address argument in case it has side
1222 effects. */
1223
1224 static void
1225 expand_builtin_prefetch (tree exp)
1226 {
1227 tree arg0, arg1, arg2;
1228 int nargs;
1229 rtx op0, op1, op2;
1230
1231 if (!validate_arglist (exp, POINTER_TYPE, 0))
1232 return;
1233
1234 arg0 = CALL_EXPR_ARG (exp, 0);
1235
1236 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1237 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1238 locality). */
1239 nargs = call_expr_nargs (exp);
1240 if (nargs > 1)
1241 arg1 = CALL_EXPR_ARG (exp, 1);
1242 else
1243 arg1 = integer_zero_node;
1244 if (nargs > 2)
1245 arg2 = CALL_EXPR_ARG (exp, 2);
1246 else
1247 arg2 = integer_three_node;
1248
1249 /* Argument 0 is an address. */
1250 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1251
1252 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1253 if (TREE_CODE (arg1) != INTEGER_CST)
1254 {
1255 error ("second argument to %<__builtin_prefetch%> must be a constant");
1256 arg1 = integer_zero_node;
1257 }
1258 op1 = expand_normal (arg1);
1259 /* Argument 1 must be either zero or one. */
1260 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1261 {
1262 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1263 " using zero");
1264 op1 = const0_rtx;
1265 }
1266
1267 /* Argument 2 (locality) must be a compile-time constant int. */
1268 if (TREE_CODE (arg2) != INTEGER_CST)
1269 {
1270 error ("third argument to %<__builtin_prefetch%> must be a constant");
1271 arg2 = integer_zero_node;
1272 }
1273 op2 = expand_normal (arg2);
1274 /* Argument 2 must be 0, 1, 2, or 3. */
1275 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1276 {
1277 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1278 op2 = const0_rtx;
1279 }
1280
1281 #ifdef HAVE_prefetch
1282 if (HAVE_prefetch)
1283 {
1284 struct expand_operand ops[3];
1285
1286 create_address_operand (&ops[0], op0);
1287 create_integer_operand (&ops[1], INTVAL (op1));
1288 create_integer_operand (&ops[2], INTVAL (op2));
1289 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1290 return;
1291 }
1292 #endif
1293
1294 /* Don't do anything with direct references to volatile memory, but
1295 generate code to handle other side effects. */
1296 if (!MEM_P (op0) && side_effects_p (op0))
1297 emit_insn (op0);
1298 }
1299
1300 /* Get a MEM rtx for expression EXP which is the address of an operand
1301 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1302 the maximum length of the block of memory that might be accessed or
1303 NULL if unknown. */
1304
1305 static rtx
1306 get_memory_rtx (tree exp, tree len)
1307 {
1308 tree orig_exp = exp;
1309 rtx addr, mem;
1310
1311 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1312 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1313 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1314 exp = TREE_OPERAND (exp, 0);
1315
1316 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1317 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1318
1319 /* Get an expression we can use to find the attributes to assign to MEM.
1320 First remove any nops. */
1321 while (CONVERT_EXPR_P (exp)
1322 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1323 exp = TREE_OPERAND (exp, 0);
1324
1325 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1326 (as builtin stringops may alias with anything). */
1327 exp = fold_build2 (MEM_REF,
1328 build_array_type (char_type_node,
1329 build_range_type (sizetype,
1330 size_one_node, len)),
1331 exp, build_int_cst (ptr_type_node, 0));
1332
1333 /* If the MEM_REF has no acceptable address, try to get the base object
1334 from the original address we got, and build an all-aliasing
1335 unknown-sized access to that one. */
1336 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1337 set_mem_attributes (mem, exp, 0);
1338 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1339 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1340 0))))
1341 {
1342 exp = build_fold_addr_expr (exp);
1343 exp = fold_build2 (MEM_REF,
1344 build_array_type (char_type_node,
1345 build_range_type (sizetype,
1346 size_zero_node,
1347 NULL)),
1348 exp, build_int_cst (ptr_type_node, 0));
1349 set_mem_attributes (mem, exp, 0);
1350 }
1351 set_mem_alias_set (mem, 0);
1352 return mem;
1353 }
1354 \f
1355 /* Built-in functions to perform an untyped call and return. */
1356
1357 #define apply_args_mode \
1358 (this_target_builtins->x_apply_args_mode)
1359 #define apply_result_mode \
1360 (this_target_builtins->x_apply_result_mode)
1361
1362 /* Return the size required for the block returned by __builtin_apply_args,
1363 and initialize apply_args_mode. */
1364
1365 static int
1366 apply_args_size (void)
1367 {
1368 static int size = -1;
1369 int align;
1370 unsigned int regno;
1371 enum machine_mode mode;
1372
1373 /* The values computed by this function never change. */
1374 if (size < 0)
1375 {
1376 /* The first value is the incoming arg-pointer. */
1377 size = GET_MODE_SIZE (Pmode);
1378
1379 /* The second value is the structure value address unless this is
1380 passed as an "invisible" first argument. */
1381 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1382 size += GET_MODE_SIZE (Pmode);
1383
1384 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1385 if (FUNCTION_ARG_REGNO_P (regno))
1386 {
1387 mode = targetm.calls.get_raw_arg_mode (regno);
1388
1389 gcc_assert (mode != VOIDmode);
1390
1391 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1392 if (size % align != 0)
1393 size = CEIL (size, align) * align;
1394 size += GET_MODE_SIZE (mode);
1395 apply_args_mode[regno] = mode;
1396 }
1397 else
1398 {
1399 apply_args_mode[regno] = VOIDmode;
1400 }
1401 }
1402 return size;
1403 }
1404
1405 /* Return the size required for the block returned by __builtin_apply,
1406 and initialize apply_result_mode. */
1407
1408 static int
1409 apply_result_size (void)
1410 {
1411 static int size = -1;
1412 int align, regno;
1413 enum machine_mode mode;
1414
1415 /* The values computed by this function never change. */
1416 if (size < 0)
1417 {
1418 size = 0;
1419
1420 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1421 if (targetm.calls.function_value_regno_p (regno))
1422 {
1423 mode = targetm.calls.get_raw_result_mode (regno);
1424
1425 gcc_assert (mode != VOIDmode);
1426
1427 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1428 if (size % align != 0)
1429 size = CEIL (size, align) * align;
1430 size += GET_MODE_SIZE (mode);
1431 apply_result_mode[regno] = mode;
1432 }
1433 else
1434 apply_result_mode[regno] = VOIDmode;
1435
1436 /* Allow targets that use untyped_call and untyped_return to override
1437 the size so that machine-specific information can be stored here. */
1438 #ifdef APPLY_RESULT_SIZE
1439 size = APPLY_RESULT_SIZE;
1440 #endif
1441 }
1442 return size;
1443 }
1444
1445 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1446 /* Create a vector describing the result block RESULT. If SAVEP is true,
1447 the result block is used to save the values; otherwise it is used to
1448 restore the values. */
1449
1450 static rtx
1451 result_vector (int savep, rtx result)
1452 {
1453 int regno, size, align, nelts;
1454 enum machine_mode mode;
1455 rtx reg, mem;
1456 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1457
1458 size = nelts = 0;
1459 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1460 if ((mode = apply_result_mode[regno]) != VOIDmode)
1461 {
1462 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1463 if (size % align != 0)
1464 size = CEIL (size, align) * align;
1465 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1466 mem = adjust_address (result, mode, size);
1467 savevec[nelts++] = (savep
1468 ? gen_rtx_SET (VOIDmode, mem, reg)
1469 : gen_rtx_SET (VOIDmode, reg, mem));
1470 size += GET_MODE_SIZE (mode);
1471 }
1472 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1473 }
1474 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1475
1476 /* Save the state required to perform an untyped call with the same
1477 arguments as were passed to the current function. */
1478
1479 static rtx
1480 expand_builtin_apply_args_1 (void)
1481 {
1482 rtx registers, tem;
1483 int size, align, regno;
1484 enum machine_mode mode;
1485 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1486
1487 /* Create a block where the arg-pointer, structure value address,
1488 and argument registers can be saved. */
1489 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1490
1491 /* Walk past the arg-pointer and structure value address. */
1492 size = GET_MODE_SIZE (Pmode);
1493 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1494 size += GET_MODE_SIZE (Pmode);
1495
1496 /* Save each register used in calling a function to the block. */
1497 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1498 if ((mode = apply_args_mode[regno]) != VOIDmode)
1499 {
1500 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1501 if (size % align != 0)
1502 size = CEIL (size, align) * align;
1503
1504 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1505
1506 emit_move_insn (adjust_address (registers, mode, size), tem);
1507 size += GET_MODE_SIZE (mode);
1508 }
1509
1510 /* Save the arg pointer to the block. */
1511 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1512 #ifdef STACK_GROWS_DOWNWARD
1513 /* We need the pointer as the caller actually passed them to us, not
1514 as we might have pretended they were passed. Make sure it's a valid
1515 operand, as emit_move_insn isn't expected to handle a PLUS. */
1516 tem
1517 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1518 NULL_RTX);
1519 #endif
1520 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1521
1522 size = GET_MODE_SIZE (Pmode);
1523
1524 /* Save the structure value address unless this is passed as an
1525 "invisible" first argument. */
1526 if (struct_incoming_value)
1527 {
1528 emit_move_insn (adjust_address (registers, Pmode, size),
1529 copy_to_reg (struct_incoming_value));
1530 size += GET_MODE_SIZE (Pmode);
1531 }
1532
1533 /* Return the address of the block. */
1534 return copy_addr_to_reg (XEXP (registers, 0));
1535 }
1536
1537 /* __builtin_apply_args returns block of memory allocated on
1538 the stack into which is stored the arg pointer, structure
1539 value address, static chain, and all the registers that might
1540 possibly be used in performing a function call. The code is
1541 moved to the start of the function so the incoming values are
1542 saved. */
1543
1544 static rtx
1545 expand_builtin_apply_args (void)
1546 {
1547 /* Don't do __builtin_apply_args more than once in a function.
1548 Save the result of the first call and reuse it. */
1549 if (apply_args_value != 0)
1550 return apply_args_value;
1551 {
1552 /* When this function is called, it means that registers must be
1553 saved on entry to this function. So we migrate the
1554 call to the first insn of this function. */
1555 rtx temp;
1556 rtx seq;
1557
1558 start_sequence ();
1559 temp = expand_builtin_apply_args_1 ();
1560 seq = get_insns ();
1561 end_sequence ();
1562
1563 apply_args_value = temp;
1564
1565 /* Put the insns after the NOTE that starts the function.
1566 If this is inside a start_sequence, make the outer-level insn
1567 chain current, so the code is placed at the start of the
1568 function. If internal_arg_pointer is a non-virtual pseudo,
1569 it needs to be placed after the function that initializes
1570 that pseudo. */
1571 push_topmost_sequence ();
1572 if (REG_P (crtl->args.internal_arg_pointer)
1573 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1574 emit_insn_before (seq, parm_birth_insn);
1575 else
1576 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1577 pop_topmost_sequence ();
1578 return temp;
1579 }
1580 }
1581
1582 /* Perform an untyped call and save the state required to perform an
1583 untyped return of whatever value was returned by the given function. */
1584
1585 static rtx
1586 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1587 {
1588 int size, align, regno;
1589 enum machine_mode mode;
1590 rtx incoming_args, result, reg, dest, src, call_insn;
1591 rtx old_stack_level = 0;
1592 rtx call_fusage = 0;
1593 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1594
1595 arguments = convert_memory_address (Pmode, arguments);
1596
1597 /* Create a block where the return registers can be saved. */
1598 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1599
1600 /* Fetch the arg pointer from the ARGUMENTS block. */
1601 incoming_args = gen_reg_rtx (Pmode);
1602 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1603 #ifndef STACK_GROWS_DOWNWARD
1604 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1605 incoming_args, 0, OPTAB_LIB_WIDEN);
1606 #endif
1607
1608 /* Push a new argument block and copy the arguments. Do not allow
1609 the (potential) memcpy call below to interfere with our stack
1610 manipulations. */
1611 do_pending_stack_adjust ();
1612 NO_DEFER_POP;
1613
1614 /* Save the stack with nonlocal if available. */
1615 #ifdef HAVE_save_stack_nonlocal
1616 if (HAVE_save_stack_nonlocal)
1617 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1618 else
1619 #endif
1620 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1621
1622 /* Allocate a block of memory onto the stack and copy the memory
1623 arguments to the outgoing arguments address. We can pass TRUE
1624 as the 4th argument because we just saved the stack pointer
1625 and will restore it right after the call. */
1626 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1627
1628 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1629 may have already set current_function_calls_alloca to true.
1630 current_function_calls_alloca won't be set if argsize is zero,
1631 so we have to guarantee need_drap is true here. */
1632 if (SUPPORTS_STACK_ALIGNMENT)
1633 crtl->need_drap = true;
1634
1635 dest = virtual_outgoing_args_rtx;
1636 #ifndef STACK_GROWS_DOWNWARD
1637 if (CONST_INT_P (argsize))
1638 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1639 else
1640 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1641 #endif
1642 dest = gen_rtx_MEM (BLKmode, dest);
1643 set_mem_align (dest, PARM_BOUNDARY);
1644 src = gen_rtx_MEM (BLKmode, incoming_args);
1645 set_mem_align (src, PARM_BOUNDARY);
1646 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1647
1648 /* Refer to the argument block. */
1649 apply_args_size ();
1650 arguments = gen_rtx_MEM (BLKmode, arguments);
1651 set_mem_align (arguments, PARM_BOUNDARY);
1652
1653 /* Walk past the arg-pointer and structure value address. */
1654 size = GET_MODE_SIZE (Pmode);
1655 if (struct_value)
1656 size += GET_MODE_SIZE (Pmode);
1657
1658 /* Restore each of the registers previously saved. Make USE insns
1659 for each of these registers for use in making the call. */
1660 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1661 if ((mode = apply_args_mode[regno]) != VOIDmode)
1662 {
1663 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1664 if (size % align != 0)
1665 size = CEIL (size, align) * align;
1666 reg = gen_rtx_REG (mode, regno);
1667 emit_move_insn (reg, adjust_address (arguments, mode, size));
1668 use_reg (&call_fusage, reg);
1669 size += GET_MODE_SIZE (mode);
1670 }
1671
1672 /* Restore the structure value address unless this is passed as an
1673 "invisible" first argument. */
1674 size = GET_MODE_SIZE (Pmode);
1675 if (struct_value)
1676 {
1677 rtx value = gen_reg_rtx (Pmode);
1678 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1679 emit_move_insn (struct_value, value);
1680 if (REG_P (struct_value))
1681 use_reg (&call_fusage, struct_value);
1682 size += GET_MODE_SIZE (Pmode);
1683 }
1684
1685 /* All arguments and registers used for the call are set up by now! */
1686 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1687
1688 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1689 and we don't want to load it into a register as an optimization,
1690 because prepare_call_address already did it if it should be done. */
1691 if (GET_CODE (function) != SYMBOL_REF)
1692 function = memory_address (FUNCTION_MODE, function);
1693
1694 /* Generate the actual call instruction and save the return value. */
1695 #ifdef HAVE_untyped_call
1696 if (HAVE_untyped_call)
1697 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1698 result, result_vector (1, result)));
1699 else
1700 #endif
1701 #ifdef HAVE_call_value
1702 if (HAVE_call_value)
1703 {
1704 rtx valreg = 0;
1705
1706 /* Locate the unique return register. It is not possible to
1707 express a call that sets more than one return register using
1708 call_value; use untyped_call for that. In fact, untyped_call
1709 only needs to save the return registers in the given block. */
1710 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1711 if ((mode = apply_result_mode[regno]) != VOIDmode)
1712 {
1713 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1714
1715 valreg = gen_rtx_REG (mode, regno);
1716 }
1717
1718 emit_call_insn (GEN_CALL_VALUE (valreg,
1719 gen_rtx_MEM (FUNCTION_MODE, function),
1720 const0_rtx, NULL_RTX, const0_rtx));
1721
1722 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1723 }
1724 else
1725 #endif
1726 gcc_unreachable ();
1727
1728 /* Find the CALL insn we just emitted, and attach the register usage
1729 information. */
1730 call_insn = last_call_insn ();
1731 add_function_usage_to (call_insn, call_fusage);
1732
1733 /* Restore the stack. */
1734 #ifdef HAVE_save_stack_nonlocal
1735 if (HAVE_save_stack_nonlocal)
1736 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1737 else
1738 #endif
1739 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1740 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1741
1742 OK_DEFER_POP;
1743
1744 /* Return the address of the result block. */
1745 result = copy_addr_to_reg (XEXP (result, 0));
1746 return convert_memory_address (ptr_mode, result);
1747 }
1748
1749 /* Perform an untyped return. */
1750
1751 static void
1752 expand_builtin_return (rtx result)
1753 {
1754 int size, align, regno;
1755 enum machine_mode mode;
1756 rtx reg;
1757 rtx call_fusage = 0;
1758
1759 result = convert_memory_address (Pmode, result);
1760
1761 apply_result_size ();
1762 result = gen_rtx_MEM (BLKmode, result);
1763
1764 #ifdef HAVE_untyped_return
1765 if (HAVE_untyped_return)
1766 {
1767 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1768 emit_barrier ();
1769 return;
1770 }
1771 #endif
1772
1773 /* Restore the return value and note that each value is used. */
1774 size = 0;
1775 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1776 if ((mode = apply_result_mode[regno]) != VOIDmode)
1777 {
1778 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1779 if (size % align != 0)
1780 size = CEIL (size, align) * align;
1781 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1782 emit_move_insn (reg, adjust_address (result, mode, size));
1783
1784 push_to_sequence (call_fusage);
1785 emit_use (reg);
1786 call_fusage = get_insns ();
1787 end_sequence ();
1788 size += GET_MODE_SIZE (mode);
1789 }
1790
1791 /* Put the USE insns before the return. */
1792 emit_insn (call_fusage);
1793
1794 /* Return whatever values was restored by jumping directly to the end
1795 of the function. */
1796 expand_naked_return ();
1797 }
1798
1799 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1800
1801 static enum type_class
1802 type_to_class (tree type)
1803 {
1804 switch (TREE_CODE (type))
1805 {
1806 case VOID_TYPE: return void_type_class;
1807 case INTEGER_TYPE: return integer_type_class;
1808 case ENUMERAL_TYPE: return enumeral_type_class;
1809 case BOOLEAN_TYPE: return boolean_type_class;
1810 case POINTER_TYPE: return pointer_type_class;
1811 case REFERENCE_TYPE: return reference_type_class;
1812 case OFFSET_TYPE: return offset_type_class;
1813 case REAL_TYPE: return real_type_class;
1814 case COMPLEX_TYPE: return complex_type_class;
1815 case FUNCTION_TYPE: return function_type_class;
1816 case METHOD_TYPE: return method_type_class;
1817 case RECORD_TYPE: return record_type_class;
1818 case UNION_TYPE:
1819 case QUAL_UNION_TYPE: return union_type_class;
1820 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1821 ? string_type_class : array_type_class);
1822 case LANG_TYPE: return lang_type_class;
1823 default: return no_type_class;
1824 }
1825 }
1826
1827 /* Expand a call EXP to __builtin_classify_type. */
1828
1829 static rtx
1830 expand_builtin_classify_type (tree exp)
1831 {
1832 if (call_expr_nargs (exp))
1833 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1834 return GEN_INT (no_type_class);
1835 }
1836
1837 /* This helper macro, meant to be used in mathfn_built_in below,
1838 determines which among a set of three builtin math functions is
1839 appropriate for a given type mode. The `F' and `L' cases are
1840 automatically generated from the `double' case. */
1841 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1842 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1843 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1844 fcodel = BUILT_IN_MATHFN##L ; break;
1845 /* Similar to above, but appends _R after any F/L suffix. */
1846 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1847 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1848 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1849 fcodel = BUILT_IN_MATHFN##L_R ; break;
1850
1851 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1852 if available. If IMPLICIT is true use the implicit builtin declaration,
1853 otherwise use the explicit declaration. If we can't do the conversion,
1854 return zero. */
1855
1856 static tree
1857 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1858 {
1859 enum built_in_function fcode, fcodef, fcodel, fcode2;
1860
1861 switch (fn)
1862 {
1863 CASE_MATHFN (BUILT_IN_ACOS)
1864 CASE_MATHFN (BUILT_IN_ACOSH)
1865 CASE_MATHFN (BUILT_IN_ASIN)
1866 CASE_MATHFN (BUILT_IN_ASINH)
1867 CASE_MATHFN (BUILT_IN_ATAN)
1868 CASE_MATHFN (BUILT_IN_ATAN2)
1869 CASE_MATHFN (BUILT_IN_ATANH)
1870 CASE_MATHFN (BUILT_IN_CBRT)
1871 CASE_MATHFN (BUILT_IN_CEIL)
1872 CASE_MATHFN (BUILT_IN_CEXPI)
1873 CASE_MATHFN (BUILT_IN_COPYSIGN)
1874 CASE_MATHFN (BUILT_IN_COS)
1875 CASE_MATHFN (BUILT_IN_COSH)
1876 CASE_MATHFN (BUILT_IN_DREM)
1877 CASE_MATHFN (BUILT_IN_ERF)
1878 CASE_MATHFN (BUILT_IN_ERFC)
1879 CASE_MATHFN (BUILT_IN_EXP)
1880 CASE_MATHFN (BUILT_IN_EXP10)
1881 CASE_MATHFN (BUILT_IN_EXP2)
1882 CASE_MATHFN (BUILT_IN_EXPM1)
1883 CASE_MATHFN (BUILT_IN_FABS)
1884 CASE_MATHFN (BUILT_IN_FDIM)
1885 CASE_MATHFN (BUILT_IN_FLOOR)
1886 CASE_MATHFN (BUILT_IN_FMA)
1887 CASE_MATHFN (BUILT_IN_FMAX)
1888 CASE_MATHFN (BUILT_IN_FMIN)
1889 CASE_MATHFN (BUILT_IN_FMOD)
1890 CASE_MATHFN (BUILT_IN_FREXP)
1891 CASE_MATHFN (BUILT_IN_GAMMA)
1892 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1893 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1894 CASE_MATHFN (BUILT_IN_HYPOT)
1895 CASE_MATHFN (BUILT_IN_ILOGB)
1896 CASE_MATHFN (BUILT_IN_ICEIL)
1897 CASE_MATHFN (BUILT_IN_IFLOOR)
1898 CASE_MATHFN (BUILT_IN_INF)
1899 CASE_MATHFN (BUILT_IN_IRINT)
1900 CASE_MATHFN (BUILT_IN_IROUND)
1901 CASE_MATHFN (BUILT_IN_ISINF)
1902 CASE_MATHFN (BUILT_IN_J0)
1903 CASE_MATHFN (BUILT_IN_J1)
1904 CASE_MATHFN (BUILT_IN_JN)
1905 CASE_MATHFN (BUILT_IN_LCEIL)
1906 CASE_MATHFN (BUILT_IN_LDEXP)
1907 CASE_MATHFN (BUILT_IN_LFLOOR)
1908 CASE_MATHFN (BUILT_IN_LGAMMA)
1909 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1910 CASE_MATHFN (BUILT_IN_LLCEIL)
1911 CASE_MATHFN (BUILT_IN_LLFLOOR)
1912 CASE_MATHFN (BUILT_IN_LLRINT)
1913 CASE_MATHFN (BUILT_IN_LLROUND)
1914 CASE_MATHFN (BUILT_IN_LOG)
1915 CASE_MATHFN (BUILT_IN_LOG10)
1916 CASE_MATHFN (BUILT_IN_LOG1P)
1917 CASE_MATHFN (BUILT_IN_LOG2)
1918 CASE_MATHFN (BUILT_IN_LOGB)
1919 CASE_MATHFN (BUILT_IN_LRINT)
1920 CASE_MATHFN (BUILT_IN_LROUND)
1921 CASE_MATHFN (BUILT_IN_MODF)
1922 CASE_MATHFN (BUILT_IN_NAN)
1923 CASE_MATHFN (BUILT_IN_NANS)
1924 CASE_MATHFN (BUILT_IN_NEARBYINT)
1925 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1926 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1927 CASE_MATHFN (BUILT_IN_POW)
1928 CASE_MATHFN (BUILT_IN_POWI)
1929 CASE_MATHFN (BUILT_IN_POW10)
1930 CASE_MATHFN (BUILT_IN_REMAINDER)
1931 CASE_MATHFN (BUILT_IN_REMQUO)
1932 CASE_MATHFN (BUILT_IN_RINT)
1933 CASE_MATHFN (BUILT_IN_ROUND)
1934 CASE_MATHFN (BUILT_IN_SCALB)
1935 CASE_MATHFN (BUILT_IN_SCALBLN)
1936 CASE_MATHFN (BUILT_IN_SCALBN)
1937 CASE_MATHFN (BUILT_IN_SIGNBIT)
1938 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1939 CASE_MATHFN (BUILT_IN_SIN)
1940 CASE_MATHFN (BUILT_IN_SINCOS)
1941 CASE_MATHFN (BUILT_IN_SINH)
1942 CASE_MATHFN (BUILT_IN_SQRT)
1943 CASE_MATHFN (BUILT_IN_TAN)
1944 CASE_MATHFN (BUILT_IN_TANH)
1945 CASE_MATHFN (BUILT_IN_TGAMMA)
1946 CASE_MATHFN (BUILT_IN_TRUNC)
1947 CASE_MATHFN (BUILT_IN_Y0)
1948 CASE_MATHFN (BUILT_IN_Y1)
1949 CASE_MATHFN (BUILT_IN_YN)
1950
1951 default:
1952 return NULL_TREE;
1953 }
1954
1955 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1956 fcode2 = fcode;
1957 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1958 fcode2 = fcodef;
1959 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1960 fcode2 = fcodel;
1961 else
1962 return NULL_TREE;
1963
1964 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1965 return NULL_TREE;
1966
1967 return builtin_decl_explicit (fcode2);
1968 }
1969
1970 /* Like mathfn_built_in_1(), but always use the implicit array. */
1971
1972 tree
1973 mathfn_built_in (tree type, enum built_in_function fn)
1974 {
1975 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1976 }
1977
1978 /* If errno must be maintained, expand the RTL to check if the result,
1979 TARGET, of a built-in function call, EXP, is NaN, and if so set
1980 errno to EDOM. */
1981
1982 static void
1983 expand_errno_check (tree exp, rtx target)
1984 {
1985 rtx lab = gen_label_rtx ();
1986
1987 /* Test the result; if it is NaN, set errno=EDOM because
1988 the argument was not in the domain. */
1989 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1990 NULL_RTX, NULL_RTX, lab,
1991 /* The jump is very likely. */
1992 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1993
1994 #ifdef TARGET_EDOM
1995 /* If this built-in doesn't throw an exception, set errno directly. */
1996 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1997 {
1998 #ifdef GEN_ERRNO_RTX
1999 rtx errno_rtx = GEN_ERRNO_RTX;
2000 #else
2001 rtx errno_rtx
2002 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2003 #endif
2004 emit_move_insn (errno_rtx,
2005 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2006 emit_label (lab);
2007 return;
2008 }
2009 #endif
2010
2011 /* Make sure the library call isn't expanded as a tail call. */
2012 CALL_EXPR_TAILCALL (exp) = 0;
2013
2014 /* We can't set errno=EDOM directly; let the library call do it.
2015 Pop the arguments right away in case the call gets deleted. */
2016 NO_DEFER_POP;
2017 expand_call (exp, target, 0);
2018 OK_DEFER_POP;
2019 emit_label (lab);
2020 }
2021
2022 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2023 Return NULL_RTX if a normal call should be emitted rather than expanding
2024 the function in-line. EXP is the expression that is a call to the builtin
2025 function; if convenient, the result should be placed in TARGET.
2026 SUBTARGET may be used as the target for computing one of EXP's operands. */
2027
2028 static rtx
2029 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2030 {
2031 optab builtin_optab;
2032 rtx op0, insns;
2033 tree fndecl = get_callee_fndecl (exp);
2034 enum machine_mode mode;
2035 bool errno_set = false;
2036 bool try_widening = false;
2037 tree arg;
2038
2039 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2040 return NULL_RTX;
2041
2042 arg = CALL_EXPR_ARG (exp, 0);
2043
2044 switch (DECL_FUNCTION_CODE (fndecl))
2045 {
2046 CASE_FLT_FN (BUILT_IN_SQRT):
2047 errno_set = ! tree_expr_nonnegative_p (arg);
2048 try_widening = true;
2049 builtin_optab = sqrt_optab;
2050 break;
2051 CASE_FLT_FN (BUILT_IN_EXP):
2052 errno_set = true; builtin_optab = exp_optab; break;
2053 CASE_FLT_FN (BUILT_IN_EXP10):
2054 CASE_FLT_FN (BUILT_IN_POW10):
2055 errno_set = true; builtin_optab = exp10_optab; break;
2056 CASE_FLT_FN (BUILT_IN_EXP2):
2057 errno_set = true; builtin_optab = exp2_optab; break;
2058 CASE_FLT_FN (BUILT_IN_EXPM1):
2059 errno_set = true; builtin_optab = expm1_optab; break;
2060 CASE_FLT_FN (BUILT_IN_LOGB):
2061 errno_set = true; builtin_optab = logb_optab; break;
2062 CASE_FLT_FN (BUILT_IN_LOG):
2063 errno_set = true; builtin_optab = log_optab; break;
2064 CASE_FLT_FN (BUILT_IN_LOG10):
2065 errno_set = true; builtin_optab = log10_optab; break;
2066 CASE_FLT_FN (BUILT_IN_LOG2):
2067 errno_set = true; builtin_optab = log2_optab; break;
2068 CASE_FLT_FN (BUILT_IN_LOG1P):
2069 errno_set = true; builtin_optab = log1p_optab; break;
2070 CASE_FLT_FN (BUILT_IN_ASIN):
2071 builtin_optab = asin_optab; break;
2072 CASE_FLT_FN (BUILT_IN_ACOS):
2073 builtin_optab = acos_optab; break;
2074 CASE_FLT_FN (BUILT_IN_TAN):
2075 builtin_optab = tan_optab; break;
2076 CASE_FLT_FN (BUILT_IN_ATAN):
2077 builtin_optab = atan_optab; break;
2078 CASE_FLT_FN (BUILT_IN_FLOOR):
2079 builtin_optab = floor_optab; break;
2080 CASE_FLT_FN (BUILT_IN_CEIL):
2081 builtin_optab = ceil_optab; break;
2082 CASE_FLT_FN (BUILT_IN_TRUNC):
2083 builtin_optab = btrunc_optab; break;
2084 CASE_FLT_FN (BUILT_IN_ROUND):
2085 builtin_optab = round_optab; break;
2086 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2087 builtin_optab = nearbyint_optab;
2088 if (flag_trapping_math)
2089 break;
2090 /* Else fallthrough and expand as rint. */
2091 CASE_FLT_FN (BUILT_IN_RINT):
2092 builtin_optab = rint_optab; break;
2093 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2094 builtin_optab = significand_optab; break;
2095 default:
2096 gcc_unreachable ();
2097 }
2098
2099 /* Make a suitable register to place result in. */
2100 mode = TYPE_MODE (TREE_TYPE (exp));
2101
2102 if (! flag_errno_math || ! HONOR_NANS (mode))
2103 errno_set = false;
2104
2105 /* Before working hard, check whether the instruction is available, but try
2106 to widen the mode for specific operations. */
2107 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2108 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2109 && (!errno_set || !optimize_insn_for_size_p ()))
2110 {
2111 rtx result = gen_reg_rtx (mode);
2112
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2117
2118 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2119
2120 start_sequence ();
2121
2122 /* Compute into RESULT.
2123 Set RESULT to wherever the result comes back. */
2124 result = expand_unop (mode, builtin_optab, op0, result, 0);
2125
2126 if (result != 0)
2127 {
2128 if (errno_set)
2129 expand_errno_check (exp, result);
2130
2131 /* Output the entire sequence. */
2132 insns = get_insns ();
2133 end_sequence ();
2134 emit_insn (insns);
2135 return result;
2136 }
2137
2138 /* If we were unable to expand via the builtin, stop the sequence
2139 (without outputting the insns) and call to the library function
2140 with the stabilized argument list. */
2141 end_sequence ();
2142 }
2143
2144 return expand_call (exp, target, target == const0_rtx);
2145 }
2146
2147 /* Expand a call to the builtin binary math functions (pow and atan2).
2148 Return NULL_RTX if a normal call should be emitted rather than expanding the
2149 function in-line. EXP is the expression that is a call to the builtin
2150 function; if convenient, the result should be placed in TARGET.
2151 SUBTARGET may be used as the target for computing one of EXP's
2152 operands. */
2153
2154 static rtx
2155 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2156 {
2157 optab builtin_optab;
2158 rtx op0, op1, insns, result;
2159 int op1_type = REAL_TYPE;
2160 tree fndecl = get_callee_fndecl (exp);
2161 tree arg0, arg1;
2162 enum machine_mode mode;
2163 bool errno_set = true;
2164
2165 switch (DECL_FUNCTION_CODE (fndecl))
2166 {
2167 CASE_FLT_FN (BUILT_IN_SCALBN):
2168 CASE_FLT_FN (BUILT_IN_SCALBLN):
2169 CASE_FLT_FN (BUILT_IN_LDEXP):
2170 op1_type = INTEGER_TYPE;
2171 default:
2172 break;
2173 }
2174
2175 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2176 return NULL_RTX;
2177
2178 arg0 = CALL_EXPR_ARG (exp, 0);
2179 arg1 = CALL_EXPR_ARG (exp, 1);
2180
2181 switch (DECL_FUNCTION_CODE (fndecl))
2182 {
2183 CASE_FLT_FN (BUILT_IN_POW):
2184 builtin_optab = pow_optab; break;
2185 CASE_FLT_FN (BUILT_IN_ATAN2):
2186 builtin_optab = atan2_optab; break;
2187 CASE_FLT_FN (BUILT_IN_SCALB):
2188 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2189 return 0;
2190 builtin_optab = scalb_optab; break;
2191 CASE_FLT_FN (BUILT_IN_SCALBN):
2192 CASE_FLT_FN (BUILT_IN_SCALBLN):
2193 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2194 return 0;
2195 /* Fall through... */
2196 CASE_FLT_FN (BUILT_IN_LDEXP):
2197 builtin_optab = ldexp_optab; break;
2198 CASE_FLT_FN (BUILT_IN_FMOD):
2199 builtin_optab = fmod_optab; break;
2200 CASE_FLT_FN (BUILT_IN_REMAINDER):
2201 CASE_FLT_FN (BUILT_IN_DREM):
2202 builtin_optab = remainder_optab; break;
2203 default:
2204 gcc_unreachable ();
2205 }
2206
2207 /* Make a suitable register to place result in. */
2208 mode = TYPE_MODE (TREE_TYPE (exp));
2209
2210 /* Before working hard, check whether the instruction is available. */
2211 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2212 return NULL_RTX;
2213
2214 result = gen_reg_rtx (mode);
2215
2216 if (! flag_errno_math || ! HONOR_NANS (mode))
2217 errno_set = false;
2218
2219 if (errno_set && optimize_insn_for_size_p ())
2220 return 0;
2221
2222 /* Always stabilize the argument list. */
2223 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2224 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2225
2226 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2227 op1 = expand_normal (arg1);
2228
2229 start_sequence ();
2230
2231 /* Compute into RESULT.
2232 Set RESULT to wherever the result comes back. */
2233 result = expand_binop (mode, builtin_optab, op0, op1,
2234 result, 0, OPTAB_DIRECT);
2235
2236 /* If we were unable to expand via the builtin, stop the sequence
2237 (without outputting the insns) and call to the library function
2238 with the stabilized argument list. */
2239 if (result == 0)
2240 {
2241 end_sequence ();
2242 return expand_call (exp, target, target == const0_rtx);
2243 }
2244
2245 if (errno_set)
2246 expand_errno_check (exp, result);
2247
2248 /* Output the entire sequence. */
2249 insns = get_insns ();
2250 end_sequence ();
2251 emit_insn (insns);
2252
2253 return result;
2254 }
2255
2256 /* Expand a call to the builtin trinary math functions (fma).
2257 Return NULL_RTX if a normal call should be emitted rather than expanding the
2258 function in-line. EXP is the expression that is a call to the builtin
2259 function; if convenient, the result should be placed in TARGET.
2260 SUBTARGET may be used as the target for computing one of EXP's
2261 operands. */
2262
2263 static rtx
2264 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2265 {
2266 optab builtin_optab;
2267 rtx op0, op1, op2, insns, result;
2268 tree fndecl = get_callee_fndecl (exp);
2269 tree arg0, arg1, arg2;
2270 enum machine_mode mode;
2271
2272 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2273 return NULL_RTX;
2274
2275 arg0 = CALL_EXPR_ARG (exp, 0);
2276 arg1 = CALL_EXPR_ARG (exp, 1);
2277 arg2 = CALL_EXPR_ARG (exp, 2);
2278
2279 switch (DECL_FUNCTION_CODE (fndecl))
2280 {
2281 CASE_FLT_FN (BUILT_IN_FMA):
2282 builtin_optab = fma_optab; break;
2283 default:
2284 gcc_unreachable ();
2285 }
2286
2287 /* Make a suitable register to place result in. */
2288 mode = TYPE_MODE (TREE_TYPE (exp));
2289
2290 /* Before working hard, check whether the instruction is available. */
2291 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2292 return NULL_RTX;
2293
2294 result = gen_reg_rtx (mode);
2295
2296 /* Always stabilize the argument list. */
2297 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2298 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2299 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2300
2301 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2302 op1 = expand_normal (arg1);
2303 op2 = expand_normal (arg2);
2304
2305 start_sequence ();
2306
2307 /* Compute into RESULT.
2308 Set RESULT to wherever the result comes back. */
2309 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2310 result, 0);
2311
2312 /* If we were unable to expand via the builtin, stop the sequence
2313 (without outputting the insns) and call to the library function
2314 with the stabilized argument list. */
2315 if (result == 0)
2316 {
2317 end_sequence ();
2318 return expand_call (exp, target, target == const0_rtx);
2319 }
2320
2321 /* Output the entire sequence. */
2322 insns = get_insns ();
2323 end_sequence ();
2324 emit_insn (insns);
2325
2326 return result;
2327 }
2328
2329 /* Expand a call to the builtin sin and cos math functions.
2330 Return NULL_RTX if a normal call should be emitted rather than expanding the
2331 function in-line. EXP is the expression that is a call to the builtin
2332 function; if convenient, the result should be placed in TARGET.
2333 SUBTARGET may be used as the target for computing one of EXP's
2334 operands. */
2335
2336 static rtx
2337 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2338 {
2339 optab builtin_optab;
2340 rtx op0, insns;
2341 tree fndecl = get_callee_fndecl (exp);
2342 enum machine_mode mode;
2343 tree arg;
2344
2345 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2346 return NULL_RTX;
2347
2348 arg = CALL_EXPR_ARG (exp, 0);
2349
2350 switch (DECL_FUNCTION_CODE (fndecl))
2351 {
2352 CASE_FLT_FN (BUILT_IN_SIN):
2353 CASE_FLT_FN (BUILT_IN_COS):
2354 builtin_optab = sincos_optab; break;
2355 default:
2356 gcc_unreachable ();
2357 }
2358
2359 /* Make a suitable register to place result in. */
2360 mode = TYPE_MODE (TREE_TYPE (exp));
2361
2362 /* Check if sincos insn is available, otherwise fallback
2363 to sin or cos insn. */
2364 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2365 switch (DECL_FUNCTION_CODE (fndecl))
2366 {
2367 CASE_FLT_FN (BUILT_IN_SIN):
2368 builtin_optab = sin_optab; break;
2369 CASE_FLT_FN (BUILT_IN_COS):
2370 builtin_optab = cos_optab; break;
2371 default:
2372 gcc_unreachable ();
2373 }
2374
2375 /* Before working hard, check whether the instruction is available. */
2376 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2377 {
2378 rtx result = gen_reg_rtx (mode);
2379
2380 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2381 need to expand the argument again. This way, we will not perform
2382 side-effects more the once. */
2383 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2384
2385 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2386
2387 start_sequence ();
2388
2389 /* Compute into RESULT.
2390 Set RESULT to wherever the result comes back. */
2391 if (builtin_optab == sincos_optab)
2392 {
2393 int ok;
2394
2395 switch (DECL_FUNCTION_CODE (fndecl))
2396 {
2397 CASE_FLT_FN (BUILT_IN_SIN):
2398 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2399 break;
2400 CASE_FLT_FN (BUILT_IN_COS):
2401 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2402 break;
2403 default:
2404 gcc_unreachable ();
2405 }
2406 gcc_assert (ok);
2407 }
2408 else
2409 result = expand_unop (mode, builtin_optab, op0, result, 0);
2410
2411 if (result != 0)
2412 {
2413 /* Output the entire sequence. */
2414 insns = get_insns ();
2415 end_sequence ();
2416 emit_insn (insns);
2417 return result;
2418 }
2419
2420 /* If we were unable to expand via the builtin, stop the sequence
2421 (without outputting the insns) and call to the library function
2422 with the stabilized argument list. */
2423 end_sequence ();
2424 }
2425
2426 return expand_call (exp, target, target == const0_rtx);
2427 }
2428
2429 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2430 return an RTL instruction code that implements the functionality.
2431 If that isn't possible or available return CODE_FOR_nothing. */
2432
2433 static enum insn_code
2434 interclass_mathfn_icode (tree arg, tree fndecl)
2435 {
2436 bool errno_set = false;
2437 optab builtin_optab = unknown_optab;
2438 enum machine_mode mode;
2439
2440 switch (DECL_FUNCTION_CODE (fndecl))
2441 {
2442 CASE_FLT_FN (BUILT_IN_ILOGB):
2443 errno_set = true; builtin_optab = ilogb_optab; break;
2444 CASE_FLT_FN (BUILT_IN_ISINF):
2445 builtin_optab = isinf_optab; break;
2446 case BUILT_IN_ISNORMAL:
2447 case BUILT_IN_ISFINITE:
2448 CASE_FLT_FN (BUILT_IN_FINITE):
2449 case BUILT_IN_FINITED32:
2450 case BUILT_IN_FINITED64:
2451 case BUILT_IN_FINITED128:
2452 case BUILT_IN_ISINFD32:
2453 case BUILT_IN_ISINFD64:
2454 case BUILT_IN_ISINFD128:
2455 /* These builtins have no optabs (yet). */
2456 break;
2457 default:
2458 gcc_unreachable ();
2459 }
2460
2461 /* There's no easy way to detect the case we need to set EDOM. */
2462 if (flag_errno_math && errno_set)
2463 return CODE_FOR_nothing;
2464
2465 /* Optab mode depends on the mode of the input argument. */
2466 mode = TYPE_MODE (TREE_TYPE (arg));
2467
2468 if (builtin_optab)
2469 return optab_handler (builtin_optab, mode);
2470 return CODE_FOR_nothing;
2471 }
2472
2473 /* Expand a call to one of the builtin math functions that operate on
2474 floating point argument and output an integer result (ilogb, isinf,
2475 isnan, etc).
2476 Return 0 if a normal call should be emitted rather than expanding the
2477 function in-line. EXP is the expression that is a call to the builtin
2478 function; if convenient, the result should be placed in TARGET. */
2479
2480 static rtx
2481 expand_builtin_interclass_mathfn (tree exp, rtx target)
2482 {
2483 enum insn_code icode = CODE_FOR_nothing;
2484 rtx op0;
2485 tree fndecl = get_callee_fndecl (exp);
2486 enum machine_mode mode;
2487 tree arg;
2488
2489 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2490 return NULL_RTX;
2491
2492 arg = CALL_EXPR_ARG (exp, 0);
2493 icode = interclass_mathfn_icode (arg, fndecl);
2494 mode = TYPE_MODE (TREE_TYPE (arg));
2495
2496 if (icode != CODE_FOR_nothing)
2497 {
2498 struct expand_operand ops[1];
2499 rtx last = get_last_insn ();
2500 tree orig_arg = arg;
2501
2502 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2503 need to expand the argument again. This way, we will not perform
2504 side-effects more the once. */
2505 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2506
2507 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2508
2509 if (mode != GET_MODE (op0))
2510 op0 = convert_to_mode (mode, op0, 0);
2511
2512 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2513 if (maybe_legitimize_operands (icode, 0, 1, ops)
2514 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2515 return ops[0].value;
2516
2517 delete_insns_since (last);
2518 CALL_EXPR_ARG (exp, 0) = orig_arg;
2519 }
2520
2521 return NULL_RTX;
2522 }
2523
2524 /* Expand a call to the builtin sincos math function.
2525 Return NULL_RTX if a normal call should be emitted rather than expanding the
2526 function in-line. EXP is the expression that is a call to the builtin
2527 function. */
2528
2529 static rtx
2530 expand_builtin_sincos (tree exp)
2531 {
2532 rtx op0, op1, op2, target1, target2;
2533 enum machine_mode mode;
2534 tree arg, sinp, cosp;
2535 int result;
2536 location_t loc = EXPR_LOCATION (exp);
2537 tree alias_type, alias_off;
2538
2539 if (!validate_arglist (exp, REAL_TYPE,
2540 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2541 return NULL_RTX;
2542
2543 arg = CALL_EXPR_ARG (exp, 0);
2544 sinp = CALL_EXPR_ARG (exp, 1);
2545 cosp = CALL_EXPR_ARG (exp, 2);
2546
2547 /* Make a suitable register to place result in. */
2548 mode = TYPE_MODE (TREE_TYPE (arg));
2549
2550 /* Check if sincos insn is available, otherwise emit the call. */
2551 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2552 return NULL_RTX;
2553
2554 target1 = gen_reg_rtx (mode);
2555 target2 = gen_reg_rtx (mode);
2556
2557 op0 = expand_normal (arg);
2558 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2559 alias_off = build_int_cst (alias_type, 0);
2560 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2561 sinp, alias_off));
2562 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2563 cosp, alias_off));
2564
2565 /* Compute into target1 and target2.
2566 Set TARGET to wherever the result comes back. */
2567 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2568 gcc_assert (result);
2569
2570 /* Move target1 and target2 to the memory locations indicated
2571 by op1 and op2. */
2572 emit_move_insn (op1, target1);
2573 emit_move_insn (op2, target2);
2574
2575 return const0_rtx;
2576 }
2577
2578 /* Expand a call to the internal cexpi builtin to the sincos math function.
2579 EXP is the expression that is a call to the builtin function; if convenient,
2580 the result should be placed in TARGET. */
2581
2582 static rtx
2583 expand_builtin_cexpi (tree exp, rtx target)
2584 {
2585 tree fndecl = get_callee_fndecl (exp);
2586 tree arg, type;
2587 enum machine_mode mode;
2588 rtx op0, op1, op2;
2589 location_t loc = EXPR_LOCATION (exp);
2590
2591 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2592 return NULL_RTX;
2593
2594 arg = CALL_EXPR_ARG (exp, 0);
2595 type = TREE_TYPE (arg);
2596 mode = TYPE_MODE (TREE_TYPE (arg));
2597
2598 /* Try expanding via a sincos optab, fall back to emitting a libcall
2599 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2600 is only generated from sincos, cexp or if we have either of them. */
2601 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2602 {
2603 op1 = gen_reg_rtx (mode);
2604 op2 = gen_reg_rtx (mode);
2605
2606 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2607
2608 /* Compute into op1 and op2. */
2609 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2610 }
2611 else if (targetm.libc_has_function (function_sincos))
2612 {
2613 tree call, fn = NULL_TREE;
2614 tree top1, top2;
2615 rtx op1a, op2a;
2616
2617 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2618 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2619 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2620 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2621 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2622 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2623 else
2624 gcc_unreachable ();
2625
2626 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2627 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2628 op1a = copy_addr_to_reg (XEXP (op1, 0));
2629 op2a = copy_addr_to_reg (XEXP (op2, 0));
2630 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2631 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2632
2633 /* Make sure not to fold the sincos call again. */
2634 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2635 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2636 call, 3, arg, top1, top2));
2637 }
2638 else
2639 {
2640 tree call, fn = NULL_TREE, narg;
2641 tree ctype = build_complex_type (type);
2642
2643 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2644 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2645 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2646 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2647 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2648 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2649 else
2650 gcc_unreachable ();
2651
2652 /* If we don't have a decl for cexp create one. This is the
2653 friendliest fallback if the user calls __builtin_cexpi
2654 without full target C99 function support. */
2655 if (fn == NULL_TREE)
2656 {
2657 tree fntype;
2658 const char *name = NULL;
2659
2660 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2661 name = "cexpf";
2662 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2663 name = "cexp";
2664 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2665 name = "cexpl";
2666
2667 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2668 fn = build_fn_decl (name, fntype);
2669 }
2670
2671 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2672 build_real (type, dconst0), arg);
2673
2674 /* Make sure not to fold the cexp call again. */
2675 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2676 return expand_expr (build_call_nary (ctype, call, 1, narg),
2677 target, VOIDmode, EXPAND_NORMAL);
2678 }
2679
2680 /* Now build the proper return type. */
2681 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2682 make_tree (TREE_TYPE (arg), op2),
2683 make_tree (TREE_TYPE (arg), op1)),
2684 target, VOIDmode, EXPAND_NORMAL);
2685 }
2686
2687 /* Conveniently construct a function call expression. FNDECL names the
2688 function to be called, N is the number of arguments, and the "..."
2689 parameters are the argument expressions. Unlike build_call_exr
2690 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2691
2692 static tree
2693 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2694 {
2695 va_list ap;
2696 tree fntype = TREE_TYPE (fndecl);
2697 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2698
2699 va_start (ap, n);
2700 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2701 va_end (ap);
2702 SET_EXPR_LOCATION (fn, loc);
2703 return fn;
2704 }
2705
2706 /* Expand a call to one of the builtin rounding functions gcc defines
2707 as an extension (lfloor and lceil). As these are gcc extensions we
2708 do not need to worry about setting errno to EDOM.
2709 If expanding via optab fails, lower expression to (int)(floor(x)).
2710 EXP is the expression that is a call to the builtin function;
2711 if convenient, the result should be placed in TARGET. */
2712
2713 static rtx
2714 expand_builtin_int_roundingfn (tree exp, rtx target)
2715 {
2716 convert_optab builtin_optab;
2717 rtx op0, insns, tmp;
2718 tree fndecl = get_callee_fndecl (exp);
2719 enum built_in_function fallback_fn;
2720 tree fallback_fndecl;
2721 enum machine_mode mode;
2722 tree arg;
2723
2724 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2725 gcc_unreachable ();
2726
2727 arg = CALL_EXPR_ARG (exp, 0);
2728
2729 switch (DECL_FUNCTION_CODE (fndecl))
2730 {
2731 CASE_FLT_FN (BUILT_IN_ICEIL):
2732 CASE_FLT_FN (BUILT_IN_LCEIL):
2733 CASE_FLT_FN (BUILT_IN_LLCEIL):
2734 builtin_optab = lceil_optab;
2735 fallback_fn = BUILT_IN_CEIL;
2736 break;
2737
2738 CASE_FLT_FN (BUILT_IN_IFLOOR):
2739 CASE_FLT_FN (BUILT_IN_LFLOOR):
2740 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2741 builtin_optab = lfloor_optab;
2742 fallback_fn = BUILT_IN_FLOOR;
2743 break;
2744
2745 default:
2746 gcc_unreachable ();
2747 }
2748
2749 /* Make a suitable register to place result in. */
2750 mode = TYPE_MODE (TREE_TYPE (exp));
2751
2752 target = gen_reg_rtx (mode);
2753
2754 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2755 need to expand the argument again. This way, we will not perform
2756 side-effects more the once. */
2757 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2758
2759 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2760
2761 start_sequence ();
2762
2763 /* Compute into TARGET. */
2764 if (expand_sfix_optab (target, op0, builtin_optab))
2765 {
2766 /* Output the entire sequence. */
2767 insns = get_insns ();
2768 end_sequence ();
2769 emit_insn (insns);
2770 return target;
2771 }
2772
2773 /* If we were unable to expand via the builtin, stop the sequence
2774 (without outputting the insns). */
2775 end_sequence ();
2776
2777 /* Fall back to floating point rounding optab. */
2778 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2779
2780 /* For non-C99 targets we may end up without a fallback fndecl here
2781 if the user called __builtin_lfloor directly. In this case emit
2782 a call to the floor/ceil variants nevertheless. This should result
2783 in the best user experience for not full C99 targets. */
2784 if (fallback_fndecl == NULL_TREE)
2785 {
2786 tree fntype;
2787 const char *name = NULL;
2788
2789 switch (DECL_FUNCTION_CODE (fndecl))
2790 {
2791 case BUILT_IN_ICEIL:
2792 case BUILT_IN_LCEIL:
2793 case BUILT_IN_LLCEIL:
2794 name = "ceil";
2795 break;
2796 case BUILT_IN_ICEILF:
2797 case BUILT_IN_LCEILF:
2798 case BUILT_IN_LLCEILF:
2799 name = "ceilf";
2800 break;
2801 case BUILT_IN_ICEILL:
2802 case BUILT_IN_LCEILL:
2803 case BUILT_IN_LLCEILL:
2804 name = "ceill";
2805 break;
2806 case BUILT_IN_IFLOOR:
2807 case BUILT_IN_LFLOOR:
2808 case BUILT_IN_LLFLOOR:
2809 name = "floor";
2810 break;
2811 case BUILT_IN_IFLOORF:
2812 case BUILT_IN_LFLOORF:
2813 case BUILT_IN_LLFLOORF:
2814 name = "floorf";
2815 break;
2816 case BUILT_IN_IFLOORL:
2817 case BUILT_IN_LFLOORL:
2818 case BUILT_IN_LLFLOORL:
2819 name = "floorl";
2820 break;
2821 default:
2822 gcc_unreachable ();
2823 }
2824
2825 fntype = build_function_type_list (TREE_TYPE (arg),
2826 TREE_TYPE (arg), NULL_TREE);
2827 fallback_fndecl = build_fn_decl (name, fntype);
2828 }
2829
2830 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2831
2832 tmp = expand_normal (exp);
2833 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2834
2835 /* Truncate the result of floating point optab to integer
2836 via expand_fix (). */
2837 target = gen_reg_rtx (mode);
2838 expand_fix (target, tmp, 0);
2839
2840 return target;
2841 }
2842
2843 /* Expand a call to one of the builtin math functions doing integer
2844 conversion (lrint).
2845 Return 0 if a normal call should be emitted rather than expanding the
2846 function in-line. EXP is the expression that is a call to the builtin
2847 function; if convenient, the result should be placed in TARGET. */
2848
2849 static rtx
2850 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2851 {
2852 convert_optab builtin_optab;
2853 rtx op0, insns;
2854 tree fndecl = get_callee_fndecl (exp);
2855 tree arg;
2856 enum machine_mode mode;
2857 enum built_in_function fallback_fn = BUILT_IN_NONE;
2858
2859 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2860 gcc_unreachable ();
2861
2862 arg = CALL_EXPR_ARG (exp, 0);
2863
2864 switch (DECL_FUNCTION_CODE (fndecl))
2865 {
2866 CASE_FLT_FN (BUILT_IN_IRINT):
2867 fallback_fn = BUILT_IN_LRINT;
2868 /* FALLTHRU */
2869 CASE_FLT_FN (BUILT_IN_LRINT):
2870 CASE_FLT_FN (BUILT_IN_LLRINT):
2871 builtin_optab = lrint_optab;
2872 break;
2873
2874 CASE_FLT_FN (BUILT_IN_IROUND):
2875 fallback_fn = BUILT_IN_LROUND;
2876 /* FALLTHRU */
2877 CASE_FLT_FN (BUILT_IN_LROUND):
2878 CASE_FLT_FN (BUILT_IN_LLROUND):
2879 builtin_optab = lround_optab;
2880 break;
2881
2882 default:
2883 gcc_unreachable ();
2884 }
2885
2886 /* There's no easy way to detect the case we need to set EDOM. */
2887 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2888 return NULL_RTX;
2889
2890 /* Make a suitable register to place result in. */
2891 mode = TYPE_MODE (TREE_TYPE (exp));
2892
2893 /* There's no easy way to detect the case we need to set EDOM. */
2894 if (!flag_errno_math)
2895 {
2896 rtx result = gen_reg_rtx (mode);
2897
2898 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2899 need to expand the argument again. This way, we will not perform
2900 side-effects more the once. */
2901 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2902
2903 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2904
2905 start_sequence ();
2906
2907 if (expand_sfix_optab (result, op0, builtin_optab))
2908 {
2909 /* Output the entire sequence. */
2910 insns = get_insns ();
2911 end_sequence ();
2912 emit_insn (insns);
2913 return result;
2914 }
2915
2916 /* If we were unable to expand via the builtin, stop the sequence
2917 (without outputting the insns) and call to the library function
2918 with the stabilized argument list. */
2919 end_sequence ();
2920 }
2921
2922 if (fallback_fn != BUILT_IN_NONE)
2923 {
2924 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2925 targets, (int) round (x) should never be transformed into
2926 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2927 a call to lround in the hope that the target provides at least some
2928 C99 functions. This should result in the best user experience for
2929 not full C99 targets. */
2930 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2931 fallback_fn, 0);
2932
2933 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2934 fallback_fndecl, 1, arg);
2935
2936 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2937 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2938 return convert_to_mode (mode, target, 0);
2939 }
2940
2941 return expand_call (exp, target, target == const0_rtx);
2942 }
2943
2944 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2945 a normal call should be emitted rather than expanding the function
2946 in-line. EXP is the expression that is a call to the builtin
2947 function; if convenient, the result should be placed in TARGET. */
2948
2949 static rtx
2950 expand_builtin_powi (tree exp, rtx target)
2951 {
2952 tree arg0, arg1;
2953 rtx op0, op1;
2954 enum machine_mode mode;
2955 enum machine_mode mode2;
2956
2957 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2958 return NULL_RTX;
2959
2960 arg0 = CALL_EXPR_ARG (exp, 0);
2961 arg1 = CALL_EXPR_ARG (exp, 1);
2962 mode = TYPE_MODE (TREE_TYPE (exp));
2963
2964 /* Emit a libcall to libgcc. */
2965
2966 /* Mode of the 2nd argument must match that of an int. */
2967 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2968
2969 if (target == NULL_RTX)
2970 target = gen_reg_rtx (mode);
2971
2972 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2973 if (GET_MODE (op0) != mode)
2974 op0 = convert_to_mode (mode, op0, 0);
2975 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2976 if (GET_MODE (op1) != mode2)
2977 op1 = convert_to_mode (mode2, op1, 0);
2978
2979 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2980 target, LCT_CONST, mode, 2,
2981 op0, mode, op1, mode2);
2982
2983 return target;
2984 }
2985
2986 /* Expand expression EXP which is a call to the strlen builtin. Return
2987 NULL_RTX if we failed the caller should emit a normal call, otherwise
2988 try to get the result in TARGET, if convenient. */
2989
2990 static rtx
2991 expand_builtin_strlen (tree exp, rtx target,
2992 enum machine_mode target_mode)
2993 {
2994 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2995 return NULL_RTX;
2996 else
2997 {
2998 struct expand_operand ops[4];
2999 rtx pat;
3000 tree len;
3001 tree src = CALL_EXPR_ARG (exp, 0);
3002 rtx src_reg, before_strlen;
3003 enum machine_mode insn_mode = target_mode;
3004 enum insn_code icode = CODE_FOR_nothing;
3005 unsigned int align;
3006
3007 /* If the length can be computed at compile-time, return it. */
3008 len = c_strlen (src, 0);
3009 if (len)
3010 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3011
3012 /* If the length can be computed at compile-time and is constant
3013 integer, but there are side-effects in src, evaluate
3014 src for side-effects, then return len.
3015 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3016 can be optimized into: i++; x = 3; */
3017 len = c_strlen (src, 1);
3018 if (len && TREE_CODE (len) == INTEGER_CST)
3019 {
3020 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3021 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3022 }
3023
3024 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3025
3026 /* If SRC is not a pointer type, don't do this operation inline. */
3027 if (align == 0)
3028 return NULL_RTX;
3029
3030 /* Bail out if we can't compute strlen in the right mode. */
3031 while (insn_mode != VOIDmode)
3032 {
3033 icode = optab_handler (strlen_optab, insn_mode);
3034 if (icode != CODE_FOR_nothing)
3035 break;
3036
3037 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3038 }
3039 if (insn_mode == VOIDmode)
3040 return NULL_RTX;
3041
3042 /* Make a place to hold the source address. We will not expand
3043 the actual source until we are sure that the expansion will
3044 not fail -- there are trees that cannot be expanded twice. */
3045 src_reg = gen_reg_rtx (Pmode);
3046
3047 /* Mark the beginning of the strlen sequence so we can emit the
3048 source operand later. */
3049 before_strlen = get_last_insn ();
3050
3051 create_output_operand (&ops[0], target, insn_mode);
3052 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3053 create_integer_operand (&ops[2], 0);
3054 create_integer_operand (&ops[3], align);
3055 if (!maybe_expand_insn (icode, 4, ops))
3056 return NULL_RTX;
3057
3058 /* Now that we are assured of success, expand the source. */
3059 start_sequence ();
3060 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3061 if (pat != src_reg)
3062 {
3063 #ifdef POINTERS_EXTEND_UNSIGNED
3064 if (GET_MODE (pat) != Pmode)
3065 pat = convert_to_mode (Pmode, pat,
3066 POINTERS_EXTEND_UNSIGNED);
3067 #endif
3068 emit_move_insn (src_reg, pat);
3069 }
3070 pat = get_insns ();
3071 end_sequence ();
3072
3073 if (before_strlen)
3074 emit_insn_after (pat, before_strlen);
3075 else
3076 emit_insn_before (pat, get_insns ());
3077
3078 /* Return the value in the proper mode for this function. */
3079 if (GET_MODE (ops[0].value) == target_mode)
3080 target = ops[0].value;
3081 else if (target != 0)
3082 convert_move (target, ops[0].value, 0);
3083 else
3084 target = convert_to_mode (target_mode, ops[0].value, 0);
3085
3086 return target;
3087 }
3088 }
3089
3090 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3091 bytes from constant string DATA + OFFSET and return it as target
3092 constant. */
3093
3094 static rtx
3095 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3096 enum machine_mode mode)
3097 {
3098 const char *str = (const char *) data;
3099
3100 gcc_assert (offset >= 0
3101 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3102 <= strlen (str) + 1));
3103
3104 return c_readstr (str + offset, mode);
3105 }
3106
3107 /* LEN specify length of the block of memcpy/memset operation.
3108 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3109 In some cases we can make very likely guess on max size, then we
3110 set it into PROBABLE_MAX_SIZE. */
3111
3112 static void
3113 determine_block_size (tree len, rtx len_rtx,
3114 unsigned HOST_WIDE_INT *min_size,
3115 unsigned HOST_WIDE_INT *max_size,
3116 unsigned HOST_WIDE_INT *probable_max_size)
3117 {
3118 if (CONST_INT_P (len_rtx))
3119 {
3120 *min_size = *max_size = UINTVAL (len_rtx);
3121 return;
3122 }
3123 else
3124 {
3125 double_int min, max;
3126 enum value_range_type range_type = VR_UNDEFINED;
3127
3128 /* Determine bounds from the type. */
3129 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3130 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3131 else
3132 *min_size = 0;
3133 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3134 *probable_max_size = *max_size = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3135 else
3136 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3137
3138 if (TREE_CODE (len) == SSA_NAME)
3139 range_type = get_range_info (len, &min, &max);
3140 if (range_type == VR_RANGE)
3141 {
3142 if (min.fits_uhwi () && *min_size < min.to_uhwi ())
3143 *min_size = min.to_uhwi ();
3144 if (max.fits_uhwi () && *max_size > max.to_uhwi ())
3145 *probable_max_size = *max_size = max.to_uhwi ();
3146 }
3147 else if (range_type == VR_ANTI_RANGE)
3148 {
3149 /* Anti range 0...N lets us to determine minimal size to N+1. */
3150 if (min.is_zero ())
3151 {
3152 if ((max + double_int_one).fits_uhwi ())
3153 *min_size = (max + double_int_one).to_uhwi ();
3154 }
3155 /* Code like
3156
3157 int n;
3158 if (n < 100)
3159 memcpy (a, b, n)
3160
3161 Produce anti range allowing negative values of N. We still
3162 can use the information and make a guess that N is not negative.
3163 */
3164 else if (!max.ule (double_int_one.lshift (30))
3165 && min.fits_uhwi ())
3166 *probable_max_size = min.to_uhwi () - 1;
3167 }
3168 }
3169 gcc_checking_assert (*max_size <=
3170 (unsigned HOST_WIDE_INT)
3171 GET_MODE_MASK (GET_MODE (len_rtx)));
3172 }
3173
3174 /* Expand a call EXP to the memcpy builtin.
3175 Return NULL_RTX if we failed, the caller should emit a normal call,
3176 otherwise try to get the result in TARGET, if convenient (and in
3177 mode MODE if that's convenient). */
3178
3179 static rtx
3180 expand_builtin_memcpy (tree exp, rtx target)
3181 {
3182 if (!validate_arglist (exp,
3183 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3184 return NULL_RTX;
3185 else
3186 {
3187 tree dest = CALL_EXPR_ARG (exp, 0);
3188 tree src = CALL_EXPR_ARG (exp, 1);
3189 tree len = CALL_EXPR_ARG (exp, 2);
3190 const char *src_str;
3191 unsigned int src_align = get_pointer_alignment (src);
3192 unsigned int dest_align = get_pointer_alignment (dest);
3193 rtx dest_mem, src_mem, dest_addr, len_rtx;
3194 HOST_WIDE_INT expected_size = -1;
3195 unsigned int expected_align = 0;
3196 unsigned HOST_WIDE_INT min_size;
3197 unsigned HOST_WIDE_INT max_size;
3198 unsigned HOST_WIDE_INT probable_max_size;
3199
3200 /* If DEST is not a pointer type, call the normal function. */
3201 if (dest_align == 0)
3202 return NULL_RTX;
3203
3204 /* If either SRC is not a pointer type, don't do this
3205 operation in-line. */
3206 if (src_align == 0)
3207 return NULL_RTX;
3208
3209 if (currently_expanding_gimple_stmt)
3210 stringop_block_profile (currently_expanding_gimple_stmt,
3211 &expected_align, &expected_size);
3212
3213 if (expected_align < dest_align)
3214 expected_align = dest_align;
3215 dest_mem = get_memory_rtx (dest, len);
3216 set_mem_align (dest_mem, dest_align);
3217 len_rtx = expand_normal (len);
3218 determine_block_size (len, len_rtx, &min_size, &max_size,
3219 &probable_max_size);
3220 src_str = c_getstr (src);
3221
3222 /* If SRC is a string constant and block move would be done
3223 by pieces, we can avoid loading the string from memory
3224 and only stored the computed constants. */
3225 if (src_str
3226 && CONST_INT_P (len_rtx)
3227 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3228 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3229 CONST_CAST (char *, src_str),
3230 dest_align, false))
3231 {
3232 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3233 builtin_memcpy_read_str,
3234 CONST_CAST (char *, src_str),
3235 dest_align, false, 0);
3236 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3237 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3238 return dest_mem;
3239 }
3240
3241 src_mem = get_memory_rtx (src, len);
3242 set_mem_align (src_mem, src_align);
3243
3244 /* Copy word part most expediently. */
3245 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3246 CALL_EXPR_TAILCALL (exp)
3247 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3248 expected_align, expected_size,
3249 min_size, max_size, probable_max_size);
3250
3251 if (dest_addr == 0)
3252 {
3253 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3254 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3255 }
3256 return dest_addr;
3257 }
3258 }
3259
3260 /* Expand a call EXP to the mempcpy builtin.
3261 Return NULL_RTX if we failed; the caller should emit a normal call,
3262 otherwise try to get the result in TARGET, if convenient (and in
3263 mode MODE if that's convenient). If ENDP is 0 return the
3264 destination pointer, if ENDP is 1 return the end pointer ala
3265 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3266 stpcpy. */
3267
3268 static rtx
3269 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3270 {
3271 if (!validate_arglist (exp,
3272 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3273 return NULL_RTX;
3274 else
3275 {
3276 tree dest = CALL_EXPR_ARG (exp, 0);
3277 tree src = CALL_EXPR_ARG (exp, 1);
3278 tree len = CALL_EXPR_ARG (exp, 2);
3279 return expand_builtin_mempcpy_args (dest, src, len,
3280 target, mode, /*endp=*/ 1);
3281 }
3282 }
3283
3284 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3285 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3286 so that this can also be called without constructing an actual CALL_EXPR.
3287 The other arguments and return value are the same as for
3288 expand_builtin_mempcpy. */
3289
3290 static rtx
3291 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3292 rtx target, enum machine_mode mode, int endp)
3293 {
3294 /* If return value is ignored, transform mempcpy into memcpy. */
3295 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3296 {
3297 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3298 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3299 dest, src, len);
3300 return expand_expr (result, target, mode, EXPAND_NORMAL);
3301 }
3302 else
3303 {
3304 const char *src_str;
3305 unsigned int src_align = get_pointer_alignment (src);
3306 unsigned int dest_align = get_pointer_alignment (dest);
3307 rtx dest_mem, src_mem, len_rtx;
3308
3309 /* If either SRC or DEST is not a pointer type, don't do this
3310 operation in-line. */
3311 if (dest_align == 0 || src_align == 0)
3312 return NULL_RTX;
3313
3314 /* If LEN is not constant, call the normal function. */
3315 if (! tree_fits_uhwi_p (len))
3316 return NULL_RTX;
3317
3318 len_rtx = expand_normal (len);
3319 src_str = c_getstr (src);
3320
3321 /* If SRC is a string constant and block move would be done
3322 by pieces, we can avoid loading the string from memory
3323 and only stored the computed constants. */
3324 if (src_str
3325 && CONST_INT_P (len_rtx)
3326 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3327 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3328 CONST_CAST (char *, src_str),
3329 dest_align, false))
3330 {
3331 dest_mem = get_memory_rtx (dest, len);
3332 set_mem_align (dest_mem, dest_align);
3333 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3334 builtin_memcpy_read_str,
3335 CONST_CAST (char *, src_str),
3336 dest_align, false, endp);
3337 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3338 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3339 return dest_mem;
3340 }
3341
3342 if (CONST_INT_P (len_rtx)
3343 && can_move_by_pieces (INTVAL (len_rtx),
3344 MIN (dest_align, src_align)))
3345 {
3346 dest_mem = get_memory_rtx (dest, len);
3347 set_mem_align (dest_mem, dest_align);
3348 src_mem = get_memory_rtx (src, len);
3349 set_mem_align (src_mem, src_align);
3350 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3351 MIN (dest_align, src_align), endp);
3352 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3353 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3354 return dest_mem;
3355 }
3356
3357 return NULL_RTX;
3358 }
3359 }
3360
3361 #ifndef HAVE_movstr
3362 # define HAVE_movstr 0
3363 # define CODE_FOR_movstr CODE_FOR_nothing
3364 #endif
3365
3366 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3367 we failed, the caller should emit a normal call, otherwise try to
3368 get the result in TARGET, if convenient. If ENDP is 0 return the
3369 destination pointer, if ENDP is 1 return the end pointer ala
3370 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3371 stpcpy. */
3372
3373 static rtx
3374 expand_movstr (tree dest, tree src, rtx target, int endp)
3375 {
3376 struct expand_operand ops[3];
3377 rtx dest_mem;
3378 rtx src_mem;
3379
3380 if (!HAVE_movstr)
3381 return NULL_RTX;
3382
3383 dest_mem = get_memory_rtx (dest, NULL);
3384 src_mem = get_memory_rtx (src, NULL);
3385 if (!endp)
3386 {
3387 target = force_reg (Pmode, XEXP (dest_mem, 0));
3388 dest_mem = replace_equiv_address (dest_mem, target);
3389 }
3390
3391 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3392 create_fixed_operand (&ops[1], dest_mem);
3393 create_fixed_operand (&ops[2], src_mem);
3394 expand_insn (CODE_FOR_movstr, 3, ops);
3395
3396 if (endp && target != const0_rtx)
3397 {
3398 target = ops[0].value;
3399 /* movstr is supposed to set end to the address of the NUL
3400 terminator. If the caller requested a mempcpy-like return value,
3401 adjust it. */
3402 if (endp == 1)
3403 {
3404 rtx tem = plus_constant (GET_MODE (target),
3405 gen_lowpart (GET_MODE (target), target), 1);
3406 emit_move_insn (target, force_operand (tem, NULL_RTX));
3407 }
3408 }
3409 return target;
3410 }
3411
3412 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3413 NULL_RTX if we failed the caller should emit a normal call, otherwise
3414 try to get the result in TARGET, if convenient (and in mode MODE if that's
3415 convenient). */
3416
3417 static rtx
3418 expand_builtin_strcpy (tree exp, rtx target)
3419 {
3420 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3421 {
3422 tree dest = CALL_EXPR_ARG (exp, 0);
3423 tree src = CALL_EXPR_ARG (exp, 1);
3424 return expand_builtin_strcpy_args (dest, src, target);
3425 }
3426 return NULL_RTX;
3427 }
3428
3429 /* Helper function to do the actual work for expand_builtin_strcpy. The
3430 arguments to the builtin_strcpy call DEST and SRC are broken out
3431 so that this can also be called without constructing an actual CALL_EXPR.
3432 The other arguments and return value are the same as for
3433 expand_builtin_strcpy. */
3434
3435 static rtx
3436 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3437 {
3438 return expand_movstr (dest, src, target, /*endp=*/0);
3439 }
3440
3441 /* Expand a call EXP to the stpcpy builtin.
3442 Return NULL_RTX if we failed the caller should emit a normal call,
3443 otherwise try to get the result in TARGET, if convenient (and in
3444 mode MODE if that's convenient). */
3445
3446 static rtx
3447 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3448 {
3449 tree dst, src;
3450 location_t loc = EXPR_LOCATION (exp);
3451
3452 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3453 return NULL_RTX;
3454
3455 dst = CALL_EXPR_ARG (exp, 0);
3456 src = CALL_EXPR_ARG (exp, 1);
3457
3458 /* If return value is ignored, transform stpcpy into strcpy. */
3459 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3460 {
3461 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3462 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3463 return expand_expr (result, target, mode, EXPAND_NORMAL);
3464 }
3465 else
3466 {
3467 tree len, lenp1;
3468 rtx ret;
3469
3470 /* Ensure we get an actual string whose length can be evaluated at
3471 compile-time, not an expression containing a string. This is
3472 because the latter will potentially produce pessimized code
3473 when used to produce the return value. */
3474 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3475 return expand_movstr (dst, src, target, /*endp=*/2);
3476
3477 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3478 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3479 target, mode, /*endp=*/2);
3480
3481 if (ret)
3482 return ret;
3483
3484 if (TREE_CODE (len) == INTEGER_CST)
3485 {
3486 rtx len_rtx = expand_normal (len);
3487
3488 if (CONST_INT_P (len_rtx))
3489 {
3490 ret = expand_builtin_strcpy_args (dst, src, target);
3491
3492 if (ret)
3493 {
3494 if (! target)
3495 {
3496 if (mode != VOIDmode)
3497 target = gen_reg_rtx (mode);
3498 else
3499 target = gen_reg_rtx (GET_MODE (ret));
3500 }
3501 if (GET_MODE (target) != GET_MODE (ret))
3502 ret = gen_lowpart (GET_MODE (target), ret);
3503
3504 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3505 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3506 gcc_assert (ret);
3507
3508 return target;
3509 }
3510 }
3511 }
3512
3513 return expand_movstr (dst, src, target, /*endp=*/2);
3514 }
3515 }
3516
3517 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3518 bytes from constant string DATA + OFFSET and return it as target
3519 constant. */
3520
3521 rtx
3522 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3523 enum machine_mode mode)
3524 {
3525 const char *str = (const char *) data;
3526
3527 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3528 return const0_rtx;
3529
3530 return c_readstr (str + offset, mode);
3531 }
3532
3533 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3534 NULL_RTX if we failed the caller should emit a normal call. */
3535
3536 static rtx
3537 expand_builtin_strncpy (tree exp, rtx target)
3538 {
3539 location_t loc = EXPR_LOCATION (exp);
3540
3541 if (validate_arglist (exp,
3542 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3543 {
3544 tree dest = CALL_EXPR_ARG (exp, 0);
3545 tree src = CALL_EXPR_ARG (exp, 1);
3546 tree len = CALL_EXPR_ARG (exp, 2);
3547 tree slen = c_strlen (src, 1);
3548
3549 /* We must be passed a constant len and src parameter. */
3550 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3551 return NULL_RTX;
3552
3553 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3554
3555 /* We're required to pad with trailing zeros if the requested
3556 len is greater than strlen(s2)+1. In that case try to
3557 use store_by_pieces, if it fails, punt. */
3558 if (tree_int_cst_lt (slen, len))
3559 {
3560 unsigned int dest_align = get_pointer_alignment (dest);
3561 const char *p = c_getstr (src);
3562 rtx dest_mem;
3563
3564 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3565 || !can_store_by_pieces (tree_to_uhwi (len),
3566 builtin_strncpy_read_str,
3567 CONST_CAST (char *, p),
3568 dest_align, false))
3569 return NULL_RTX;
3570
3571 dest_mem = get_memory_rtx (dest, len);
3572 store_by_pieces (dest_mem, tree_to_uhwi (len),
3573 builtin_strncpy_read_str,
3574 CONST_CAST (char *, p), dest_align, false, 0);
3575 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3576 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3577 return dest_mem;
3578 }
3579 }
3580 return NULL_RTX;
3581 }
3582
3583 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3584 bytes from constant string DATA + OFFSET and return it as target
3585 constant. */
3586
3587 rtx
3588 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3589 enum machine_mode mode)
3590 {
3591 const char *c = (const char *) data;
3592 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3593
3594 memset (p, *c, GET_MODE_SIZE (mode));
3595
3596 return c_readstr (p, mode);
3597 }
3598
3599 /* Callback routine for store_by_pieces. Return the RTL of a register
3600 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3601 char value given in the RTL register data. For example, if mode is
3602 4 bytes wide, return the RTL for 0x01010101*data. */
3603
3604 static rtx
3605 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3606 enum machine_mode mode)
3607 {
3608 rtx target, coeff;
3609 size_t size;
3610 char *p;
3611
3612 size = GET_MODE_SIZE (mode);
3613 if (size == 1)
3614 return (rtx) data;
3615
3616 p = XALLOCAVEC (char, size);
3617 memset (p, 1, size);
3618 coeff = c_readstr (p, mode);
3619
3620 target = convert_to_mode (mode, (rtx) data, 1);
3621 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3622 return force_reg (mode, target);
3623 }
3624
3625 /* Expand expression EXP, which is a call to the memset builtin. Return
3626 NULL_RTX if we failed the caller should emit a normal call, otherwise
3627 try to get the result in TARGET, if convenient (and in mode MODE if that's
3628 convenient). */
3629
3630 static rtx
3631 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3632 {
3633 if (!validate_arglist (exp,
3634 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3635 return NULL_RTX;
3636 else
3637 {
3638 tree dest = CALL_EXPR_ARG (exp, 0);
3639 tree val = CALL_EXPR_ARG (exp, 1);
3640 tree len = CALL_EXPR_ARG (exp, 2);
3641 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3642 }
3643 }
3644
3645 /* Helper function to do the actual work for expand_builtin_memset. The
3646 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3647 so that this can also be called without constructing an actual CALL_EXPR.
3648 The other arguments and return value are the same as for
3649 expand_builtin_memset. */
3650
3651 static rtx
3652 expand_builtin_memset_args (tree dest, tree val, tree len,
3653 rtx target, enum machine_mode mode, tree orig_exp)
3654 {
3655 tree fndecl, fn;
3656 enum built_in_function fcode;
3657 enum machine_mode val_mode;
3658 char c;
3659 unsigned int dest_align;
3660 rtx dest_mem, dest_addr, len_rtx;
3661 HOST_WIDE_INT expected_size = -1;
3662 unsigned int expected_align = 0;
3663 unsigned HOST_WIDE_INT min_size;
3664 unsigned HOST_WIDE_INT max_size;
3665 unsigned HOST_WIDE_INT probable_max_size;
3666
3667 dest_align = get_pointer_alignment (dest);
3668
3669 /* If DEST is not a pointer type, don't do this operation in-line. */
3670 if (dest_align == 0)
3671 return NULL_RTX;
3672
3673 if (currently_expanding_gimple_stmt)
3674 stringop_block_profile (currently_expanding_gimple_stmt,
3675 &expected_align, &expected_size);
3676
3677 if (expected_align < dest_align)
3678 expected_align = dest_align;
3679
3680 /* If the LEN parameter is zero, return DEST. */
3681 if (integer_zerop (len))
3682 {
3683 /* Evaluate and ignore VAL in case it has side-effects. */
3684 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3685 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3686 }
3687
3688 /* Stabilize the arguments in case we fail. */
3689 dest = builtin_save_expr (dest);
3690 val = builtin_save_expr (val);
3691 len = builtin_save_expr (len);
3692
3693 len_rtx = expand_normal (len);
3694 determine_block_size (len, len_rtx, &min_size, &max_size,
3695 &probable_max_size);
3696 dest_mem = get_memory_rtx (dest, len);
3697 val_mode = TYPE_MODE (unsigned_char_type_node);
3698
3699 if (TREE_CODE (val) != INTEGER_CST)
3700 {
3701 rtx val_rtx;
3702
3703 val_rtx = expand_normal (val);
3704 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3705
3706 /* Assume that we can memset by pieces if we can store
3707 * the coefficients by pieces (in the required modes).
3708 * We can't pass builtin_memset_gen_str as that emits RTL. */
3709 c = 1;
3710 if (tree_fits_uhwi_p (len)
3711 && can_store_by_pieces (tree_to_uhwi (len),
3712 builtin_memset_read_str, &c, dest_align,
3713 true))
3714 {
3715 val_rtx = force_reg (val_mode, val_rtx);
3716 store_by_pieces (dest_mem, tree_to_uhwi (len),
3717 builtin_memset_gen_str, val_rtx, dest_align,
3718 true, 0);
3719 }
3720 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3721 dest_align, expected_align,
3722 expected_size, min_size, max_size,
3723 probable_max_size))
3724 goto do_libcall;
3725
3726 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3727 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3728 return dest_mem;
3729 }
3730
3731 if (target_char_cast (val, &c))
3732 goto do_libcall;
3733
3734 if (c)
3735 {
3736 if (tree_fits_uhwi_p (len)
3737 && can_store_by_pieces (tree_to_uhwi (len),
3738 builtin_memset_read_str, &c, dest_align,
3739 true))
3740 store_by_pieces (dest_mem, tree_to_uhwi (len),
3741 builtin_memset_read_str, &c, dest_align, true, 0);
3742 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3743 gen_int_mode (c, val_mode),
3744 dest_align, expected_align,
3745 expected_size, min_size, max_size,
3746 probable_max_size))
3747 goto do_libcall;
3748
3749 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3750 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3751 return dest_mem;
3752 }
3753
3754 set_mem_align (dest_mem, dest_align);
3755 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3756 CALL_EXPR_TAILCALL (orig_exp)
3757 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3758 expected_align, expected_size,
3759 min_size, max_size,
3760 probable_max_size);
3761
3762 if (dest_addr == 0)
3763 {
3764 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3765 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3766 }
3767
3768 return dest_addr;
3769
3770 do_libcall:
3771 fndecl = get_callee_fndecl (orig_exp);
3772 fcode = DECL_FUNCTION_CODE (fndecl);
3773 if (fcode == BUILT_IN_MEMSET)
3774 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3775 dest, val, len);
3776 else if (fcode == BUILT_IN_BZERO)
3777 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3778 dest, len);
3779 else
3780 gcc_unreachable ();
3781 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3782 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3783 return expand_call (fn, target, target == const0_rtx);
3784 }
3785
3786 /* Expand expression EXP, which is a call to the bzero builtin. Return
3787 NULL_RTX if we failed the caller should emit a normal call. */
3788
3789 static rtx
3790 expand_builtin_bzero (tree exp)
3791 {
3792 tree dest, size;
3793 location_t loc = EXPR_LOCATION (exp);
3794
3795 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3796 return NULL_RTX;
3797
3798 dest = CALL_EXPR_ARG (exp, 0);
3799 size = CALL_EXPR_ARG (exp, 1);
3800
3801 /* New argument list transforming bzero(ptr x, int y) to
3802 memset(ptr x, int 0, size_t y). This is done this way
3803 so that if it isn't expanded inline, we fallback to
3804 calling bzero instead of memset. */
3805
3806 return expand_builtin_memset_args (dest, integer_zero_node,
3807 fold_convert_loc (loc,
3808 size_type_node, size),
3809 const0_rtx, VOIDmode, exp);
3810 }
3811
3812 /* Expand expression EXP, which is a call to the memcmp built-in function.
3813 Return NULL_RTX if we failed and the caller should emit a normal call,
3814 otherwise try to get the result in TARGET, if convenient (and in mode
3815 MODE, if that's convenient). */
3816
3817 static rtx
3818 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3819 ATTRIBUTE_UNUSED enum machine_mode mode)
3820 {
3821 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3822
3823 if (!validate_arglist (exp,
3824 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3825 return NULL_RTX;
3826
3827 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3828 implementing memcmp because it will stop if it encounters two
3829 zero bytes. */
3830 #if defined HAVE_cmpmemsi
3831 {
3832 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3833 rtx result;
3834 rtx insn;
3835 tree arg1 = CALL_EXPR_ARG (exp, 0);
3836 tree arg2 = CALL_EXPR_ARG (exp, 1);
3837 tree len = CALL_EXPR_ARG (exp, 2);
3838
3839 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3840 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3841 enum machine_mode insn_mode;
3842
3843 if (HAVE_cmpmemsi)
3844 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3845 else
3846 return NULL_RTX;
3847
3848 /* If we don't have POINTER_TYPE, call the function. */
3849 if (arg1_align == 0 || arg2_align == 0)
3850 return NULL_RTX;
3851
3852 /* Make a place to write the result of the instruction. */
3853 result = target;
3854 if (! (result != 0
3855 && REG_P (result) && GET_MODE (result) == insn_mode
3856 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3857 result = gen_reg_rtx (insn_mode);
3858
3859 arg1_rtx = get_memory_rtx (arg1, len);
3860 arg2_rtx = get_memory_rtx (arg2, len);
3861 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3862
3863 /* Set MEM_SIZE as appropriate. */
3864 if (CONST_INT_P (arg3_rtx))
3865 {
3866 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3867 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3868 }
3869
3870 if (HAVE_cmpmemsi)
3871 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3872 GEN_INT (MIN (arg1_align, arg2_align)));
3873 else
3874 gcc_unreachable ();
3875
3876 if (insn)
3877 emit_insn (insn);
3878 else
3879 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3880 TYPE_MODE (integer_type_node), 3,
3881 XEXP (arg1_rtx, 0), Pmode,
3882 XEXP (arg2_rtx, 0), Pmode,
3883 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3884 TYPE_UNSIGNED (sizetype)),
3885 TYPE_MODE (sizetype));
3886
3887 /* Return the value in the proper mode for this function. */
3888 mode = TYPE_MODE (TREE_TYPE (exp));
3889 if (GET_MODE (result) == mode)
3890 return result;
3891 else if (target != 0)
3892 {
3893 convert_move (target, result, 0);
3894 return target;
3895 }
3896 else
3897 return convert_to_mode (mode, result, 0);
3898 }
3899 #endif /* HAVE_cmpmemsi. */
3900
3901 return NULL_RTX;
3902 }
3903
3904 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3905 if we failed the caller should emit a normal call, otherwise try to get
3906 the result in TARGET, if convenient. */
3907
3908 static rtx
3909 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3910 {
3911 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3912 return NULL_RTX;
3913
3914 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3915 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3916 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3917 {
3918 rtx arg1_rtx, arg2_rtx;
3919 rtx result, insn = NULL_RTX;
3920 tree fndecl, fn;
3921 tree arg1 = CALL_EXPR_ARG (exp, 0);
3922 tree arg2 = CALL_EXPR_ARG (exp, 1);
3923
3924 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3925 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3926
3927 /* If we don't have POINTER_TYPE, call the function. */
3928 if (arg1_align == 0 || arg2_align == 0)
3929 return NULL_RTX;
3930
3931 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3932 arg1 = builtin_save_expr (arg1);
3933 arg2 = builtin_save_expr (arg2);
3934
3935 arg1_rtx = get_memory_rtx (arg1, NULL);
3936 arg2_rtx = get_memory_rtx (arg2, NULL);
3937
3938 #ifdef HAVE_cmpstrsi
3939 /* Try to call cmpstrsi. */
3940 if (HAVE_cmpstrsi)
3941 {
3942 enum machine_mode insn_mode
3943 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3944
3945 /* Make a place to write the result of the instruction. */
3946 result = target;
3947 if (! (result != 0
3948 && REG_P (result) && GET_MODE (result) == insn_mode
3949 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3950 result = gen_reg_rtx (insn_mode);
3951
3952 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3953 GEN_INT (MIN (arg1_align, arg2_align)));
3954 }
3955 #endif
3956 #ifdef HAVE_cmpstrnsi
3957 /* Try to determine at least one length and call cmpstrnsi. */
3958 if (!insn && HAVE_cmpstrnsi)
3959 {
3960 tree len;
3961 rtx arg3_rtx;
3962
3963 enum machine_mode insn_mode
3964 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3965 tree len1 = c_strlen (arg1, 1);
3966 tree len2 = c_strlen (arg2, 1);
3967
3968 if (len1)
3969 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3970 if (len2)
3971 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3972
3973 /* If we don't have a constant length for the first, use the length
3974 of the second, if we know it. We don't require a constant for
3975 this case; some cost analysis could be done if both are available
3976 but neither is constant. For now, assume they're equally cheap,
3977 unless one has side effects. If both strings have constant lengths,
3978 use the smaller. */
3979
3980 if (!len1)
3981 len = len2;
3982 else if (!len2)
3983 len = len1;
3984 else if (TREE_SIDE_EFFECTS (len1))
3985 len = len2;
3986 else if (TREE_SIDE_EFFECTS (len2))
3987 len = len1;
3988 else if (TREE_CODE (len1) != INTEGER_CST)
3989 len = len2;
3990 else if (TREE_CODE (len2) != INTEGER_CST)
3991 len = len1;
3992 else if (tree_int_cst_lt (len1, len2))
3993 len = len1;
3994 else
3995 len = len2;
3996
3997 /* If both arguments have side effects, we cannot optimize. */
3998 if (!len || TREE_SIDE_EFFECTS (len))
3999 goto do_libcall;
4000
4001 arg3_rtx = expand_normal (len);
4002
4003 /* Make a place to write the result of the instruction. */
4004 result = target;
4005 if (! (result != 0
4006 && REG_P (result) && GET_MODE (result) == insn_mode
4007 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4008 result = gen_reg_rtx (insn_mode);
4009
4010 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4011 GEN_INT (MIN (arg1_align, arg2_align)));
4012 }
4013 #endif
4014
4015 if (insn)
4016 {
4017 enum machine_mode mode;
4018 emit_insn (insn);
4019
4020 /* Return the value in the proper mode for this function. */
4021 mode = TYPE_MODE (TREE_TYPE (exp));
4022 if (GET_MODE (result) == mode)
4023 return result;
4024 if (target == 0)
4025 return convert_to_mode (mode, result, 0);
4026 convert_move (target, result, 0);
4027 return target;
4028 }
4029
4030 /* Expand the library call ourselves using a stabilized argument
4031 list to avoid re-evaluating the function's arguments twice. */
4032 #ifdef HAVE_cmpstrnsi
4033 do_libcall:
4034 #endif
4035 fndecl = get_callee_fndecl (exp);
4036 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4037 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4038 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4039 return expand_call (fn, target, target == const0_rtx);
4040 }
4041 #endif
4042 return NULL_RTX;
4043 }
4044
4045 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4046 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4047 the result in TARGET, if convenient. */
4048
4049 static rtx
4050 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4051 ATTRIBUTE_UNUSED enum machine_mode mode)
4052 {
4053 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4054
4055 if (!validate_arglist (exp,
4056 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4057 return NULL_RTX;
4058
4059 /* If c_strlen can determine an expression for one of the string
4060 lengths, and it doesn't have side effects, then emit cmpstrnsi
4061 using length MIN(strlen(string)+1, arg3). */
4062 #ifdef HAVE_cmpstrnsi
4063 if (HAVE_cmpstrnsi)
4064 {
4065 tree len, len1, len2;
4066 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4067 rtx result, insn;
4068 tree fndecl, fn;
4069 tree arg1 = CALL_EXPR_ARG (exp, 0);
4070 tree arg2 = CALL_EXPR_ARG (exp, 1);
4071 tree arg3 = CALL_EXPR_ARG (exp, 2);
4072
4073 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4074 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4075 enum machine_mode insn_mode
4076 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4077
4078 len1 = c_strlen (arg1, 1);
4079 len2 = c_strlen (arg2, 1);
4080
4081 if (len1)
4082 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4083 if (len2)
4084 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4085
4086 /* If we don't have a constant length for the first, use the length
4087 of the second, if we know it. We don't require a constant for
4088 this case; some cost analysis could be done if both are available
4089 but neither is constant. For now, assume they're equally cheap,
4090 unless one has side effects. If both strings have constant lengths,
4091 use the smaller. */
4092
4093 if (!len1)
4094 len = len2;
4095 else if (!len2)
4096 len = len1;
4097 else if (TREE_SIDE_EFFECTS (len1))
4098 len = len2;
4099 else if (TREE_SIDE_EFFECTS (len2))
4100 len = len1;
4101 else if (TREE_CODE (len1) != INTEGER_CST)
4102 len = len2;
4103 else if (TREE_CODE (len2) != INTEGER_CST)
4104 len = len1;
4105 else if (tree_int_cst_lt (len1, len2))
4106 len = len1;
4107 else
4108 len = len2;
4109
4110 /* If both arguments have side effects, we cannot optimize. */
4111 if (!len || TREE_SIDE_EFFECTS (len))
4112 return NULL_RTX;
4113
4114 /* The actual new length parameter is MIN(len,arg3). */
4115 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4116 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4117
4118 /* If we don't have POINTER_TYPE, call the function. */
4119 if (arg1_align == 0 || arg2_align == 0)
4120 return NULL_RTX;
4121
4122 /* Make a place to write the result of the instruction. */
4123 result = target;
4124 if (! (result != 0
4125 && REG_P (result) && GET_MODE (result) == insn_mode
4126 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4127 result = gen_reg_rtx (insn_mode);
4128
4129 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4130 arg1 = builtin_save_expr (arg1);
4131 arg2 = builtin_save_expr (arg2);
4132 len = builtin_save_expr (len);
4133
4134 arg1_rtx = get_memory_rtx (arg1, len);
4135 arg2_rtx = get_memory_rtx (arg2, len);
4136 arg3_rtx = expand_normal (len);
4137 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4138 GEN_INT (MIN (arg1_align, arg2_align)));
4139 if (insn)
4140 {
4141 emit_insn (insn);
4142
4143 /* Return the value in the proper mode for this function. */
4144 mode = TYPE_MODE (TREE_TYPE (exp));
4145 if (GET_MODE (result) == mode)
4146 return result;
4147 if (target == 0)
4148 return convert_to_mode (mode, result, 0);
4149 convert_move (target, result, 0);
4150 return target;
4151 }
4152
4153 /* Expand the library call ourselves using a stabilized argument
4154 list to avoid re-evaluating the function's arguments twice. */
4155 fndecl = get_callee_fndecl (exp);
4156 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4157 arg1, arg2, len);
4158 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4159 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4160 return expand_call (fn, target, target == const0_rtx);
4161 }
4162 #endif
4163 return NULL_RTX;
4164 }
4165
4166 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4167 if that's convenient. */
4168
4169 rtx
4170 expand_builtin_saveregs (void)
4171 {
4172 rtx val, seq;
4173
4174 /* Don't do __builtin_saveregs more than once in a function.
4175 Save the result of the first call and reuse it. */
4176 if (saveregs_value != 0)
4177 return saveregs_value;
4178
4179 /* When this function is called, it means that registers must be
4180 saved on entry to this function. So we migrate the call to the
4181 first insn of this function. */
4182
4183 start_sequence ();
4184
4185 /* Do whatever the machine needs done in this case. */
4186 val = targetm.calls.expand_builtin_saveregs ();
4187
4188 seq = get_insns ();
4189 end_sequence ();
4190
4191 saveregs_value = val;
4192
4193 /* Put the insns after the NOTE that starts the function. If this
4194 is inside a start_sequence, make the outer-level insn chain current, so
4195 the code is placed at the start of the function. */
4196 push_topmost_sequence ();
4197 emit_insn_after (seq, entry_of_function ());
4198 pop_topmost_sequence ();
4199
4200 return val;
4201 }
4202
4203 /* Expand a call to __builtin_next_arg. */
4204
4205 static rtx
4206 expand_builtin_next_arg (void)
4207 {
4208 /* Checking arguments is already done in fold_builtin_next_arg
4209 that must be called before this function. */
4210 return expand_binop (ptr_mode, add_optab,
4211 crtl->args.internal_arg_pointer,
4212 crtl->args.arg_offset_rtx,
4213 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4214 }
4215
4216 /* Make it easier for the backends by protecting the valist argument
4217 from multiple evaluations. */
4218
4219 static tree
4220 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4221 {
4222 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4223
4224 /* The current way of determining the type of valist is completely
4225 bogus. We should have the information on the va builtin instead. */
4226 if (!vatype)
4227 vatype = targetm.fn_abi_va_list (cfun->decl);
4228
4229 if (TREE_CODE (vatype) == ARRAY_TYPE)
4230 {
4231 if (TREE_SIDE_EFFECTS (valist))
4232 valist = save_expr (valist);
4233
4234 /* For this case, the backends will be expecting a pointer to
4235 vatype, but it's possible we've actually been given an array
4236 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4237 So fix it. */
4238 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4239 {
4240 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4241 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4242 }
4243 }
4244 else
4245 {
4246 tree pt = build_pointer_type (vatype);
4247
4248 if (! needs_lvalue)
4249 {
4250 if (! TREE_SIDE_EFFECTS (valist))
4251 return valist;
4252
4253 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4254 TREE_SIDE_EFFECTS (valist) = 1;
4255 }
4256
4257 if (TREE_SIDE_EFFECTS (valist))
4258 valist = save_expr (valist);
4259 valist = fold_build2_loc (loc, MEM_REF,
4260 vatype, valist, build_int_cst (pt, 0));
4261 }
4262
4263 return valist;
4264 }
4265
4266 /* The "standard" definition of va_list is void*. */
4267
4268 tree
4269 std_build_builtin_va_list (void)
4270 {
4271 return ptr_type_node;
4272 }
4273
4274 /* The "standard" abi va_list is va_list_type_node. */
4275
4276 tree
4277 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4278 {
4279 return va_list_type_node;
4280 }
4281
4282 /* The "standard" type of va_list is va_list_type_node. */
4283
4284 tree
4285 std_canonical_va_list_type (tree type)
4286 {
4287 tree wtype, htype;
4288
4289 if (INDIRECT_REF_P (type))
4290 type = TREE_TYPE (type);
4291 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4292 type = TREE_TYPE (type);
4293 wtype = va_list_type_node;
4294 htype = type;
4295 /* Treat structure va_list types. */
4296 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4297 htype = TREE_TYPE (htype);
4298 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4299 {
4300 /* If va_list is an array type, the argument may have decayed
4301 to a pointer type, e.g. by being passed to another function.
4302 In that case, unwrap both types so that we can compare the
4303 underlying records. */
4304 if (TREE_CODE (htype) == ARRAY_TYPE
4305 || POINTER_TYPE_P (htype))
4306 {
4307 wtype = TREE_TYPE (wtype);
4308 htype = TREE_TYPE (htype);
4309 }
4310 }
4311 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4312 return va_list_type_node;
4313
4314 return NULL_TREE;
4315 }
4316
4317 /* The "standard" implementation of va_start: just assign `nextarg' to
4318 the variable. */
4319
4320 void
4321 std_expand_builtin_va_start (tree valist, rtx nextarg)
4322 {
4323 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4324 convert_move (va_r, nextarg, 0);
4325 }
4326
4327 /* Expand EXP, a call to __builtin_va_start. */
4328
4329 static rtx
4330 expand_builtin_va_start (tree exp)
4331 {
4332 rtx nextarg;
4333 tree valist;
4334 location_t loc = EXPR_LOCATION (exp);
4335
4336 if (call_expr_nargs (exp) < 2)
4337 {
4338 error_at (loc, "too few arguments to function %<va_start%>");
4339 return const0_rtx;
4340 }
4341
4342 if (fold_builtin_next_arg (exp, true))
4343 return const0_rtx;
4344
4345 nextarg = expand_builtin_next_arg ();
4346 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4347
4348 if (targetm.expand_builtin_va_start)
4349 targetm.expand_builtin_va_start (valist, nextarg);
4350 else
4351 std_expand_builtin_va_start (valist, nextarg);
4352
4353 return const0_rtx;
4354 }
4355
4356 /* Expand EXP, a call to __builtin_va_end. */
4357
4358 static rtx
4359 expand_builtin_va_end (tree exp)
4360 {
4361 tree valist = CALL_EXPR_ARG (exp, 0);
4362
4363 /* Evaluate for side effects, if needed. I hate macros that don't
4364 do that. */
4365 if (TREE_SIDE_EFFECTS (valist))
4366 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4367
4368 return const0_rtx;
4369 }
4370
4371 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4372 builtin rather than just as an assignment in stdarg.h because of the
4373 nastiness of array-type va_list types. */
4374
4375 static rtx
4376 expand_builtin_va_copy (tree exp)
4377 {
4378 tree dst, src, t;
4379 location_t loc = EXPR_LOCATION (exp);
4380
4381 dst = CALL_EXPR_ARG (exp, 0);
4382 src = CALL_EXPR_ARG (exp, 1);
4383
4384 dst = stabilize_va_list_loc (loc, dst, 1);
4385 src = stabilize_va_list_loc (loc, src, 0);
4386
4387 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4388
4389 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4390 {
4391 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4392 TREE_SIDE_EFFECTS (t) = 1;
4393 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4394 }
4395 else
4396 {
4397 rtx dstb, srcb, size;
4398
4399 /* Evaluate to pointers. */
4400 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4401 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4402 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4403 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4404
4405 dstb = convert_memory_address (Pmode, dstb);
4406 srcb = convert_memory_address (Pmode, srcb);
4407
4408 /* "Dereference" to BLKmode memories. */
4409 dstb = gen_rtx_MEM (BLKmode, dstb);
4410 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4411 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4412 srcb = gen_rtx_MEM (BLKmode, srcb);
4413 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4414 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4415
4416 /* Copy. */
4417 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4418 }
4419
4420 return const0_rtx;
4421 }
4422
4423 /* Expand a call to one of the builtin functions __builtin_frame_address or
4424 __builtin_return_address. */
4425
4426 static rtx
4427 expand_builtin_frame_address (tree fndecl, tree exp)
4428 {
4429 /* The argument must be a nonnegative integer constant.
4430 It counts the number of frames to scan up the stack.
4431 The value is the return address saved in that frame. */
4432 if (call_expr_nargs (exp) == 0)
4433 /* Warning about missing arg was already issued. */
4434 return const0_rtx;
4435 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4436 {
4437 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4438 error ("invalid argument to %<__builtin_frame_address%>");
4439 else
4440 error ("invalid argument to %<__builtin_return_address%>");
4441 return const0_rtx;
4442 }
4443 else
4444 {
4445 rtx tem
4446 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4447 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4448
4449 /* Some ports cannot access arbitrary stack frames. */
4450 if (tem == NULL)
4451 {
4452 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4453 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4454 else
4455 warning (0, "unsupported argument to %<__builtin_return_address%>");
4456 return const0_rtx;
4457 }
4458
4459 /* For __builtin_frame_address, return what we've got. */
4460 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4461 return tem;
4462
4463 if (!REG_P (tem)
4464 && ! CONSTANT_P (tem))
4465 tem = copy_addr_to_reg (tem);
4466 return tem;
4467 }
4468 }
4469
4470 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4471 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4472 is the same as for allocate_dynamic_stack_space. */
4473
4474 static rtx
4475 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4476 {
4477 rtx op0;
4478 rtx result;
4479 bool valid_arglist;
4480 unsigned int align;
4481 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4482 == BUILT_IN_ALLOCA_WITH_ALIGN);
4483
4484 valid_arglist
4485 = (alloca_with_align
4486 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4487 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4488
4489 if (!valid_arglist)
4490 return NULL_RTX;
4491
4492 /* Compute the argument. */
4493 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4494
4495 /* Compute the alignment. */
4496 align = (alloca_with_align
4497 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4498 : BIGGEST_ALIGNMENT);
4499
4500 /* Allocate the desired space. */
4501 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4502 result = convert_memory_address (ptr_mode, result);
4503
4504 return result;
4505 }
4506
4507 /* Expand a call to bswap builtin in EXP.
4508 Return NULL_RTX if a normal call should be emitted rather than expanding the
4509 function in-line. If convenient, the result should be placed in TARGET.
4510 SUBTARGET may be used as the target for computing one of EXP's operands. */
4511
4512 static rtx
4513 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4514 rtx subtarget)
4515 {
4516 tree arg;
4517 rtx op0;
4518
4519 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4520 return NULL_RTX;
4521
4522 arg = CALL_EXPR_ARG (exp, 0);
4523 op0 = expand_expr (arg,
4524 subtarget && GET_MODE (subtarget) == target_mode
4525 ? subtarget : NULL_RTX,
4526 target_mode, EXPAND_NORMAL);
4527 if (GET_MODE (op0) != target_mode)
4528 op0 = convert_to_mode (target_mode, op0, 1);
4529
4530 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4531
4532 gcc_assert (target);
4533
4534 return convert_to_mode (target_mode, target, 1);
4535 }
4536
4537 /* Expand a call to a unary builtin in EXP.
4538 Return NULL_RTX if a normal call should be emitted rather than expanding the
4539 function in-line. If convenient, the result should be placed in TARGET.
4540 SUBTARGET may be used as the target for computing one of EXP's operands. */
4541
4542 static rtx
4543 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4544 rtx subtarget, optab op_optab)
4545 {
4546 rtx op0;
4547
4548 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4549 return NULL_RTX;
4550
4551 /* Compute the argument. */
4552 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4553 (subtarget
4554 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4555 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4556 VOIDmode, EXPAND_NORMAL);
4557 /* Compute op, into TARGET if possible.
4558 Set TARGET to wherever the result comes back. */
4559 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4560 op_optab, op0, target, op_optab != clrsb_optab);
4561 gcc_assert (target);
4562
4563 return convert_to_mode (target_mode, target, 0);
4564 }
4565
4566 /* Expand a call to __builtin_expect. We just return our argument
4567 as the builtin_expect semantic should've been already executed by
4568 tree branch prediction pass. */
4569
4570 static rtx
4571 expand_builtin_expect (tree exp, rtx target)
4572 {
4573 tree arg;
4574
4575 if (call_expr_nargs (exp) < 2)
4576 return const0_rtx;
4577 arg = CALL_EXPR_ARG (exp, 0);
4578
4579 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4580 /* When guessing was done, the hints should be already stripped away. */
4581 gcc_assert (!flag_guess_branch_prob
4582 || optimize == 0 || seen_error ());
4583 return target;
4584 }
4585
4586 /* Expand a call to __builtin_assume_aligned. We just return our first
4587 argument as the builtin_assume_aligned semantic should've been already
4588 executed by CCP. */
4589
4590 static rtx
4591 expand_builtin_assume_aligned (tree exp, rtx target)
4592 {
4593 if (call_expr_nargs (exp) < 2)
4594 return const0_rtx;
4595 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4596 EXPAND_NORMAL);
4597 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4598 && (call_expr_nargs (exp) < 3
4599 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4600 return target;
4601 }
4602
4603 void
4604 expand_builtin_trap (void)
4605 {
4606 #ifdef HAVE_trap
4607 if (HAVE_trap)
4608 {
4609 rtx insn = emit_insn (gen_trap ());
4610 /* For trap insns when not accumulating outgoing args force
4611 REG_ARGS_SIZE note to prevent crossjumping of calls with
4612 different args sizes. */
4613 if (!ACCUMULATE_OUTGOING_ARGS)
4614 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4615 }
4616 else
4617 #endif
4618 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4619 emit_barrier ();
4620 }
4621
4622 /* Expand a call to __builtin_unreachable. We do nothing except emit
4623 a barrier saying that control flow will not pass here.
4624
4625 It is the responsibility of the program being compiled to ensure
4626 that control flow does never reach __builtin_unreachable. */
4627 static void
4628 expand_builtin_unreachable (void)
4629 {
4630 emit_barrier ();
4631 }
4632
4633 /* Expand EXP, a call to fabs, fabsf or fabsl.
4634 Return NULL_RTX if a normal call should be emitted rather than expanding
4635 the function inline. If convenient, the result should be placed
4636 in TARGET. SUBTARGET may be used as the target for computing
4637 the operand. */
4638
4639 static rtx
4640 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4641 {
4642 enum machine_mode mode;
4643 tree arg;
4644 rtx op0;
4645
4646 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4647 return NULL_RTX;
4648
4649 arg = CALL_EXPR_ARG (exp, 0);
4650 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4651 mode = TYPE_MODE (TREE_TYPE (arg));
4652 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4653 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4654 }
4655
4656 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4657 Return NULL is a normal call should be emitted rather than expanding the
4658 function inline. If convenient, the result should be placed in TARGET.
4659 SUBTARGET may be used as the target for computing the operand. */
4660
4661 static rtx
4662 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4663 {
4664 rtx op0, op1;
4665 tree arg;
4666
4667 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4668 return NULL_RTX;
4669
4670 arg = CALL_EXPR_ARG (exp, 0);
4671 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4672
4673 arg = CALL_EXPR_ARG (exp, 1);
4674 op1 = expand_normal (arg);
4675
4676 return expand_copysign (op0, op1, target);
4677 }
4678
4679 /* Create a new constant string literal and return a char* pointer to it.
4680 The STRING_CST value is the LEN characters at STR. */
4681 tree
4682 build_string_literal (int len, const char *str)
4683 {
4684 tree t, elem, index, type;
4685
4686 t = build_string (len, str);
4687 elem = build_type_variant (char_type_node, 1, 0);
4688 index = build_index_type (size_int (len - 1));
4689 type = build_array_type (elem, index);
4690 TREE_TYPE (t) = type;
4691 TREE_CONSTANT (t) = 1;
4692 TREE_READONLY (t) = 1;
4693 TREE_STATIC (t) = 1;
4694
4695 type = build_pointer_type (elem);
4696 t = build1 (ADDR_EXPR, type,
4697 build4 (ARRAY_REF, elem,
4698 t, integer_zero_node, NULL_TREE, NULL_TREE));
4699 return t;
4700 }
4701
4702 /* Expand a call to __builtin___clear_cache. */
4703
4704 static rtx
4705 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4706 {
4707 #ifndef HAVE_clear_cache
4708 #ifdef CLEAR_INSN_CACHE
4709 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4710 does something. Just do the default expansion to a call to
4711 __clear_cache(). */
4712 return NULL_RTX;
4713 #else
4714 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4715 does nothing. There is no need to call it. Do nothing. */
4716 return const0_rtx;
4717 #endif /* CLEAR_INSN_CACHE */
4718 #else
4719 /* We have a "clear_cache" insn, and it will handle everything. */
4720 tree begin, end;
4721 rtx begin_rtx, end_rtx;
4722
4723 /* We must not expand to a library call. If we did, any
4724 fallback library function in libgcc that might contain a call to
4725 __builtin___clear_cache() would recurse infinitely. */
4726 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4727 {
4728 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4729 return const0_rtx;
4730 }
4731
4732 if (HAVE_clear_cache)
4733 {
4734 struct expand_operand ops[2];
4735
4736 begin = CALL_EXPR_ARG (exp, 0);
4737 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4738
4739 end = CALL_EXPR_ARG (exp, 1);
4740 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4741
4742 create_address_operand (&ops[0], begin_rtx);
4743 create_address_operand (&ops[1], end_rtx);
4744 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4745 return const0_rtx;
4746 }
4747 return const0_rtx;
4748 #endif /* HAVE_clear_cache */
4749 }
4750
4751 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4752
4753 static rtx
4754 round_trampoline_addr (rtx tramp)
4755 {
4756 rtx temp, addend, mask;
4757
4758 /* If we don't need too much alignment, we'll have been guaranteed
4759 proper alignment by get_trampoline_type. */
4760 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4761 return tramp;
4762
4763 /* Round address up to desired boundary. */
4764 temp = gen_reg_rtx (Pmode);
4765 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4766 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4767
4768 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4769 temp, 0, OPTAB_LIB_WIDEN);
4770 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4771 temp, 0, OPTAB_LIB_WIDEN);
4772
4773 return tramp;
4774 }
4775
4776 static rtx
4777 expand_builtin_init_trampoline (tree exp, bool onstack)
4778 {
4779 tree t_tramp, t_func, t_chain;
4780 rtx m_tramp, r_tramp, r_chain, tmp;
4781
4782 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4783 POINTER_TYPE, VOID_TYPE))
4784 return NULL_RTX;
4785
4786 t_tramp = CALL_EXPR_ARG (exp, 0);
4787 t_func = CALL_EXPR_ARG (exp, 1);
4788 t_chain = CALL_EXPR_ARG (exp, 2);
4789
4790 r_tramp = expand_normal (t_tramp);
4791 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4792 MEM_NOTRAP_P (m_tramp) = 1;
4793
4794 /* If ONSTACK, the TRAMP argument should be the address of a field
4795 within the local function's FRAME decl. Either way, let's see if
4796 we can fill in the MEM_ATTRs for this memory. */
4797 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4798 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4799
4800 /* Creator of a heap trampoline is responsible for making sure the
4801 address is aligned to at least STACK_BOUNDARY. Normally malloc
4802 will ensure this anyhow. */
4803 tmp = round_trampoline_addr (r_tramp);
4804 if (tmp != r_tramp)
4805 {
4806 m_tramp = change_address (m_tramp, BLKmode, tmp);
4807 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4808 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4809 }
4810
4811 /* The FUNC argument should be the address of the nested function.
4812 Extract the actual function decl to pass to the hook. */
4813 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4814 t_func = TREE_OPERAND (t_func, 0);
4815 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4816
4817 r_chain = expand_normal (t_chain);
4818
4819 /* Generate insns to initialize the trampoline. */
4820 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4821
4822 if (onstack)
4823 {
4824 trampolines_created = 1;
4825
4826 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4827 "trampoline generated for nested function %qD", t_func);
4828 }
4829
4830 return const0_rtx;
4831 }
4832
4833 static rtx
4834 expand_builtin_adjust_trampoline (tree exp)
4835 {
4836 rtx tramp;
4837
4838 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4839 return NULL_RTX;
4840
4841 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4842 tramp = round_trampoline_addr (tramp);
4843 if (targetm.calls.trampoline_adjust_address)
4844 tramp = targetm.calls.trampoline_adjust_address (tramp);
4845
4846 return tramp;
4847 }
4848
4849 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4850 function. The function first checks whether the back end provides
4851 an insn to implement signbit for the respective mode. If not, it
4852 checks whether the floating point format of the value is such that
4853 the sign bit can be extracted. If that is not the case, the
4854 function returns NULL_RTX to indicate that a normal call should be
4855 emitted rather than expanding the function in-line. EXP is the
4856 expression that is a call to the builtin function; if convenient,
4857 the result should be placed in TARGET. */
4858 static rtx
4859 expand_builtin_signbit (tree exp, rtx target)
4860 {
4861 const struct real_format *fmt;
4862 enum machine_mode fmode, imode, rmode;
4863 tree arg;
4864 int word, bitpos;
4865 enum insn_code icode;
4866 rtx temp;
4867 location_t loc = EXPR_LOCATION (exp);
4868
4869 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4870 return NULL_RTX;
4871
4872 arg = CALL_EXPR_ARG (exp, 0);
4873 fmode = TYPE_MODE (TREE_TYPE (arg));
4874 rmode = TYPE_MODE (TREE_TYPE (exp));
4875 fmt = REAL_MODE_FORMAT (fmode);
4876
4877 arg = builtin_save_expr (arg);
4878
4879 /* Expand the argument yielding a RTX expression. */
4880 temp = expand_normal (arg);
4881
4882 /* Check if the back end provides an insn that handles signbit for the
4883 argument's mode. */
4884 icode = optab_handler (signbit_optab, fmode);
4885 if (icode != CODE_FOR_nothing)
4886 {
4887 rtx last = get_last_insn ();
4888 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4889 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4890 return target;
4891 delete_insns_since (last);
4892 }
4893
4894 /* For floating point formats without a sign bit, implement signbit
4895 as "ARG < 0.0". */
4896 bitpos = fmt->signbit_ro;
4897 if (bitpos < 0)
4898 {
4899 /* But we can't do this if the format supports signed zero. */
4900 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4901 return NULL_RTX;
4902
4903 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4904 build_real (TREE_TYPE (arg), dconst0));
4905 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4906 }
4907
4908 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4909 {
4910 imode = int_mode_for_mode (fmode);
4911 if (imode == BLKmode)
4912 return NULL_RTX;
4913 temp = gen_lowpart (imode, temp);
4914 }
4915 else
4916 {
4917 imode = word_mode;
4918 /* Handle targets with different FP word orders. */
4919 if (FLOAT_WORDS_BIG_ENDIAN)
4920 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4921 else
4922 word = bitpos / BITS_PER_WORD;
4923 temp = operand_subword_force (temp, word, fmode);
4924 bitpos = bitpos % BITS_PER_WORD;
4925 }
4926
4927 /* Force the intermediate word_mode (or narrower) result into a
4928 register. This avoids attempting to create paradoxical SUBREGs
4929 of floating point modes below. */
4930 temp = force_reg (imode, temp);
4931
4932 /* If the bitpos is within the "result mode" lowpart, the operation
4933 can be implement with a single bitwise AND. Otherwise, we need
4934 a right shift and an AND. */
4935
4936 if (bitpos < GET_MODE_BITSIZE (rmode))
4937 {
4938 double_int mask = double_int_zero.set_bit (bitpos);
4939
4940 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4941 temp = gen_lowpart (rmode, temp);
4942 temp = expand_binop (rmode, and_optab, temp,
4943 immed_double_int_const (mask, rmode),
4944 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4945 }
4946 else
4947 {
4948 /* Perform a logical right shift to place the signbit in the least
4949 significant bit, then truncate the result to the desired mode
4950 and mask just this bit. */
4951 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4952 temp = gen_lowpart (rmode, temp);
4953 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4954 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4955 }
4956
4957 return temp;
4958 }
4959
4960 /* Expand fork or exec calls. TARGET is the desired target of the
4961 call. EXP is the call. FN is the
4962 identificator of the actual function. IGNORE is nonzero if the
4963 value is to be ignored. */
4964
4965 static rtx
4966 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4967 {
4968 tree id, decl;
4969 tree call;
4970
4971 /* If we are not profiling, just call the function. */
4972 if (!profile_arc_flag)
4973 return NULL_RTX;
4974
4975 /* Otherwise call the wrapper. This should be equivalent for the rest of
4976 compiler, so the code does not diverge, and the wrapper may run the
4977 code necessary for keeping the profiling sane. */
4978
4979 switch (DECL_FUNCTION_CODE (fn))
4980 {
4981 case BUILT_IN_FORK:
4982 id = get_identifier ("__gcov_fork");
4983 break;
4984
4985 case BUILT_IN_EXECL:
4986 id = get_identifier ("__gcov_execl");
4987 break;
4988
4989 case BUILT_IN_EXECV:
4990 id = get_identifier ("__gcov_execv");
4991 break;
4992
4993 case BUILT_IN_EXECLP:
4994 id = get_identifier ("__gcov_execlp");
4995 break;
4996
4997 case BUILT_IN_EXECLE:
4998 id = get_identifier ("__gcov_execle");
4999 break;
5000
5001 case BUILT_IN_EXECVP:
5002 id = get_identifier ("__gcov_execvp");
5003 break;
5004
5005 case BUILT_IN_EXECVE:
5006 id = get_identifier ("__gcov_execve");
5007 break;
5008
5009 default:
5010 gcc_unreachable ();
5011 }
5012
5013 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5014 FUNCTION_DECL, id, TREE_TYPE (fn));
5015 DECL_EXTERNAL (decl) = 1;
5016 TREE_PUBLIC (decl) = 1;
5017 DECL_ARTIFICIAL (decl) = 1;
5018 TREE_NOTHROW (decl) = 1;
5019 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5020 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5021 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5022 return expand_call (call, target, ignore);
5023 }
5024
5025
5026 \f
5027 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5028 the pointer in these functions is void*, the tree optimizers may remove
5029 casts. The mode computed in expand_builtin isn't reliable either, due
5030 to __sync_bool_compare_and_swap.
5031
5032 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5033 group of builtins. This gives us log2 of the mode size. */
5034
5035 static inline enum machine_mode
5036 get_builtin_sync_mode (int fcode_diff)
5037 {
5038 /* The size is not negotiable, so ask not to get BLKmode in return
5039 if the target indicates that a smaller size would be better. */
5040 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5041 }
5042
5043 /* Expand the memory expression LOC and return the appropriate memory operand
5044 for the builtin_sync operations. */
5045
5046 static rtx
5047 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5048 {
5049 rtx addr, mem;
5050
5051 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5052 addr = convert_memory_address (Pmode, addr);
5053
5054 /* Note that we explicitly do not want any alias information for this
5055 memory, so that we kill all other live memories. Otherwise we don't
5056 satisfy the full barrier semantics of the intrinsic. */
5057 mem = validize_mem (gen_rtx_MEM (mode, addr));
5058
5059 /* The alignment needs to be at least according to that of the mode. */
5060 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5061 get_pointer_alignment (loc)));
5062 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5063 MEM_VOLATILE_P (mem) = 1;
5064
5065 return mem;
5066 }
5067
5068 /* Make sure an argument is in the right mode.
5069 EXP is the tree argument.
5070 MODE is the mode it should be in. */
5071
5072 static rtx
5073 expand_expr_force_mode (tree exp, enum machine_mode mode)
5074 {
5075 rtx val;
5076 enum machine_mode old_mode;
5077
5078 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5079 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5080 of CONST_INTs, where we know the old_mode only from the call argument. */
5081
5082 old_mode = GET_MODE (val);
5083 if (old_mode == VOIDmode)
5084 old_mode = TYPE_MODE (TREE_TYPE (exp));
5085 val = convert_modes (mode, old_mode, val, 1);
5086 return val;
5087 }
5088
5089
5090 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5091 EXP is the CALL_EXPR. CODE is the rtx code
5092 that corresponds to the arithmetic or logical operation from the name;
5093 an exception here is that NOT actually means NAND. TARGET is an optional
5094 place for us to store the results; AFTER is true if this is the
5095 fetch_and_xxx form. */
5096
5097 static rtx
5098 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5099 enum rtx_code code, bool after,
5100 rtx target)
5101 {
5102 rtx val, mem;
5103 location_t loc = EXPR_LOCATION (exp);
5104
5105 if (code == NOT && warn_sync_nand)
5106 {
5107 tree fndecl = get_callee_fndecl (exp);
5108 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5109
5110 static bool warned_f_a_n, warned_n_a_f;
5111
5112 switch (fcode)
5113 {
5114 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5115 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5116 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5117 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5118 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5119 if (warned_f_a_n)
5120 break;
5121
5122 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5123 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5124 warned_f_a_n = true;
5125 break;
5126
5127 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5128 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5129 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5130 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5131 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5132 if (warned_n_a_f)
5133 break;
5134
5135 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5136 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5137 warned_n_a_f = true;
5138 break;
5139
5140 default:
5141 gcc_unreachable ();
5142 }
5143 }
5144
5145 /* Expand the operands. */
5146 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5147 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5148
5149 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5150 after);
5151 }
5152
5153 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5154 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5155 true if this is the boolean form. TARGET is a place for us to store the
5156 results; this is NOT optional if IS_BOOL is true. */
5157
5158 static rtx
5159 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5160 bool is_bool, rtx target)
5161 {
5162 rtx old_val, new_val, mem;
5163 rtx *pbool, *poval;
5164
5165 /* Expand the operands. */
5166 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5167 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5168 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5169
5170 pbool = poval = NULL;
5171 if (target != const0_rtx)
5172 {
5173 if (is_bool)
5174 pbool = &target;
5175 else
5176 poval = &target;
5177 }
5178 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5179 false, MEMMODEL_SEQ_CST,
5180 MEMMODEL_SEQ_CST))
5181 return NULL_RTX;
5182
5183 return target;
5184 }
5185
5186 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5187 general form is actually an atomic exchange, and some targets only
5188 support a reduced form with the second argument being a constant 1.
5189 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5190 the results. */
5191
5192 static rtx
5193 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5194 rtx target)
5195 {
5196 rtx val, mem;
5197
5198 /* Expand the operands. */
5199 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5200 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5201
5202 return expand_sync_lock_test_and_set (target, mem, val);
5203 }
5204
5205 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5206
5207 static void
5208 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5209 {
5210 rtx mem;
5211
5212 /* Expand the operands. */
5213 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5214
5215 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5216 }
5217
5218 /* Given an integer representing an ``enum memmodel'', verify its
5219 correctness and return the memory model enum. */
5220
5221 static enum memmodel
5222 get_memmodel (tree exp)
5223 {
5224 rtx op;
5225 unsigned HOST_WIDE_INT val;
5226
5227 /* If the parameter is not a constant, it's a run time value so we'll just
5228 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5229 if (TREE_CODE (exp) != INTEGER_CST)
5230 return MEMMODEL_SEQ_CST;
5231
5232 op = expand_normal (exp);
5233
5234 val = INTVAL (op);
5235 if (targetm.memmodel_check)
5236 val = targetm.memmodel_check (val);
5237 else if (val & ~MEMMODEL_MASK)
5238 {
5239 warning (OPT_Winvalid_memory_model,
5240 "Unknown architecture specifier in memory model to builtin.");
5241 return MEMMODEL_SEQ_CST;
5242 }
5243
5244 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5245 {
5246 warning (OPT_Winvalid_memory_model,
5247 "invalid memory model argument to builtin");
5248 return MEMMODEL_SEQ_CST;
5249 }
5250
5251 return (enum memmodel) val;
5252 }
5253
5254 /* Expand the __atomic_exchange intrinsic:
5255 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5256 EXP is the CALL_EXPR.
5257 TARGET is an optional place for us to store the results. */
5258
5259 static rtx
5260 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5261 {
5262 rtx val, mem;
5263 enum memmodel model;
5264
5265 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5266 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5267 {
5268 error ("invalid memory model for %<__atomic_exchange%>");
5269 return NULL_RTX;
5270 }
5271
5272 if (!flag_inline_atomics)
5273 return NULL_RTX;
5274
5275 /* Expand the operands. */
5276 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5277 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5278
5279 return expand_atomic_exchange (target, mem, val, model);
5280 }
5281
5282 /* Expand the __atomic_compare_exchange intrinsic:
5283 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5284 TYPE desired, BOOL weak,
5285 enum memmodel success,
5286 enum memmodel failure)
5287 EXP is the CALL_EXPR.
5288 TARGET is an optional place for us to store the results. */
5289
5290 static rtx
5291 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5292 rtx target)
5293 {
5294 rtx expect, desired, mem, oldval;
5295 enum memmodel success, failure;
5296 tree weak;
5297 bool is_weak;
5298
5299 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5300 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5301
5302 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5303 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5304 {
5305 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5306 return NULL_RTX;
5307 }
5308
5309 if (failure > success)
5310 {
5311 error ("failure memory model cannot be stronger than success "
5312 "memory model for %<__atomic_compare_exchange%>");
5313 return NULL_RTX;
5314 }
5315
5316 if (!flag_inline_atomics)
5317 return NULL_RTX;
5318
5319 /* Expand the operands. */
5320 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5321
5322 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5323 expect = convert_memory_address (Pmode, expect);
5324 expect = gen_rtx_MEM (mode, expect);
5325 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5326
5327 weak = CALL_EXPR_ARG (exp, 3);
5328 is_weak = false;
5329 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5330 is_weak = true;
5331
5332 oldval = expect;
5333 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5334 &oldval, mem, oldval, desired,
5335 is_weak, success, failure))
5336 return NULL_RTX;
5337
5338 if (oldval != expect)
5339 emit_move_insn (expect, oldval);
5340
5341 return target;
5342 }
5343
5344 /* Expand the __atomic_load intrinsic:
5345 TYPE __atomic_load (TYPE *object, enum memmodel)
5346 EXP is the CALL_EXPR.
5347 TARGET is an optional place for us to store the results. */
5348
5349 static rtx
5350 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5351 {
5352 rtx mem;
5353 enum memmodel model;
5354
5355 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5356 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5357 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5358 {
5359 error ("invalid memory model for %<__atomic_load%>");
5360 return NULL_RTX;
5361 }
5362
5363 if (!flag_inline_atomics)
5364 return NULL_RTX;
5365
5366 /* Expand the operand. */
5367 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5368
5369 return expand_atomic_load (target, mem, model);
5370 }
5371
5372
5373 /* Expand the __atomic_store intrinsic:
5374 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5375 EXP is the CALL_EXPR.
5376 TARGET is an optional place for us to store the results. */
5377
5378 static rtx
5379 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5380 {
5381 rtx mem, val;
5382 enum memmodel model;
5383
5384 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5385 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5386 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5387 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5388 {
5389 error ("invalid memory model for %<__atomic_store%>");
5390 return NULL_RTX;
5391 }
5392
5393 if (!flag_inline_atomics)
5394 return NULL_RTX;
5395
5396 /* Expand the operands. */
5397 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5398 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5399
5400 return expand_atomic_store (mem, val, model, false);
5401 }
5402
5403 /* Expand the __atomic_fetch_XXX intrinsic:
5404 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5405 EXP is the CALL_EXPR.
5406 TARGET is an optional place for us to store the results.
5407 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5408 FETCH_AFTER is true if returning the result of the operation.
5409 FETCH_AFTER is false if returning the value before the operation.
5410 IGNORE is true if the result is not used.
5411 EXT_CALL is the correct builtin for an external call if this cannot be
5412 resolved to an instruction sequence. */
5413
5414 static rtx
5415 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5416 enum rtx_code code, bool fetch_after,
5417 bool ignore, enum built_in_function ext_call)
5418 {
5419 rtx val, mem, ret;
5420 enum memmodel model;
5421 tree fndecl;
5422 tree addr;
5423
5424 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5425
5426 /* Expand the operands. */
5427 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5428 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5429
5430 /* Only try generating instructions if inlining is turned on. */
5431 if (flag_inline_atomics)
5432 {
5433 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5434 if (ret)
5435 return ret;
5436 }
5437
5438 /* Return if a different routine isn't needed for the library call. */
5439 if (ext_call == BUILT_IN_NONE)
5440 return NULL_RTX;
5441
5442 /* Change the call to the specified function. */
5443 fndecl = get_callee_fndecl (exp);
5444 addr = CALL_EXPR_FN (exp);
5445 STRIP_NOPS (addr);
5446
5447 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5448 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5449
5450 /* Expand the call here so we can emit trailing code. */
5451 ret = expand_call (exp, target, ignore);
5452
5453 /* Replace the original function just in case it matters. */
5454 TREE_OPERAND (addr, 0) = fndecl;
5455
5456 /* Then issue the arithmetic correction to return the right result. */
5457 if (!ignore)
5458 {
5459 if (code == NOT)
5460 {
5461 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5462 OPTAB_LIB_WIDEN);
5463 ret = expand_simple_unop (mode, NOT, ret, target, true);
5464 }
5465 else
5466 ret = expand_simple_binop (mode, code, ret, val, target, true,
5467 OPTAB_LIB_WIDEN);
5468 }
5469 return ret;
5470 }
5471
5472
5473 #ifndef HAVE_atomic_clear
5474 # define HAVE_atomic_clear 0
5475 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5476 #endif
5477
5478 /* Expand an atomic clear operation.
5479 void _atomic_clear (BOOL *obj, enum memmodel)
5480 EXP is the call expression. */
5481
5482 static rtx
5483 expand_builtin_atomic_clear (tree exp)
5484 {
5485 enum machine_mode mode;
5486 rtx mem, ret;
5487 enum memmodel model;
5488
5489 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5490 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5491 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5492
5493 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5494 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5495 {
5496 error ("invalid memory model for %<__atomic_store%>");
5497 return const0_rtx;
5498 }
5499
5500 if (HAVE_atomic_clear)
5501 {
5502 emit_insn (gen_atomic_clear (mem, model));
5503 return const0_rtx;
5504 }
5505
5506 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5507 Failing that, a store is issued by __atomic_store. The only way this can
5508 fail is if the bool type is larger than a word size. Unlikely, but
5509 handle it anyway for completeness. Assume a single threaded model since
5510 there is no atomic support in this case, and no barriers are required. */
5511 ret = expand_atomic_store (mem, const0_rtx, model, true);
5512 if (!ret)
5513 emit_move_insn (mem, const0_rtx);
5514 return const0_rtx;
5515 }
5516
5517 /* Expand an atomic test_and_set operation.
5518 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5519 EXP is the call expression. */
5520
5521 static rtx
5522 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5523 {
5524 rtx mem;
5525 enum memmodel model;
5526 enum machine_mode mode;
5527
5528 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5529 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5530 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5531
5532 return expand_atomic_test_and_set (target, mem, model);
5533 }
5534
5535
5536 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5537 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5538
5539 static tree
5540 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5541 {
5542 int size;
5543 enum machine_mode mode;
5544 unsigned int mode_align, type_align;
5545
5546 if (TREE_CODE (arg0) != INTEGER_CST)
5547 return NULL_TREE;
5548
5549 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5550 mode = mode_for_size (size, MODE_INT, 0);
5551 mode_align = GET_MODE_ALIGNMENT (mode);
5552
5553 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5554 type_align = mode_align;
5555 else
5556 {
5557 tree ttype = TREE_TYPE (arg1);
5558
5559 /* This function is usually invoked and folded immediately by the front
5560 end before anything else has a chance to look at it. The pointer
5561 parameter at this point is usually cast to a void *, so check for that
5562 and look past the cast. */
5563 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5564 && VOID_TYPE_P (TREE_TYPE (ttype)))
5565 arg1 = TREE_OPERAND (arg1, 0);
5566
5567 ttype = TREE_TYPE (arg1);
5568 gcc_assert (POINTER_TYPE_P (ttype));
5569
5570 /* Get the underlying type of the object. */
5571 ttype = TREE_TYPE (ttype);
5572 type_align = TYPE_ALIGN (ttype);
5573 }
5574
5575 /* If the object has smaller alignment, the the lock free routines cannot
5576 be used. */
5577 if (type_align < mode_align)
5578 return boolean_false_node;
5579
5580 /* Check if a compare_and_swap pattern exists for the mode which represents
5581 the required size. The pattern is not allowed to fail, so the existence
5582 of the pattern indicates support is present. */
5583 if (can_compare_and_swap_p (mode, true))
5584 return boolean_true_node;
5585 else
5586 return boolean_false_node;
5587 }
5588
5589 /* Return true if the parameters to call EXP represent an object which will
5590 always generate lock free instructions. The first argument represents the
5591 size of the object, and the second parameter is a pointer to the object
5592 itself. If NULL is passed for the object, then the result is based on
5593 typical alignment for an object of the specified size. Otherwise return
5594 false. */
5595
5596 static rtx
5597 expand_builtin_atomic_always_lock_free (tree exp)
5598 {
5599 tree size;
5600 tree arg0 = CALL_EXPR_ARG (exp, 0);
5601 tree arg1 = CALL_EXPR_ARG (exp, 1);
5602
5603 if (TREE_CODE (arg0) != INTEGER_CST)
5604 {
5605 error ("non-constant argument 1 to __atomic_always_lock_free");
5606 return const0_rtx;
5607 }
5608
5609 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5610 if (size == boolean_true_node)
5611 return const1_rtx;
5612 return const0_rtx;
5613 }
5614
5615 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5616 is lock free on this architecture. */
5617
5618 static tree
5619 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5620 {
5621 if (!flag_inline_atomics)
5622 return NULL_TREE;
5623
5624 /* If it isn't always lock free, don't generate a result. */
5625 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5626 return boolean_true_node;
5627
5628 return NULL_TREE;
5629 }
5630
5631 /* Return true if the parameters to call EXP represent an object which will
5632 always generate lock free instructions. The first argument represents the
5633 size of the object, and the second parameter is a pointer to the object
5634 itself. If NULL is passed for the object, then the result is based on
5635 typical alignment for an object of the specified size. Otherwise return
5636 NULL*/
5637
5638 static rtx
5639 expand_builtin_atomic_is_lock_free (tree exp)
5640 {
5641 tree size;
5642 tree arg0 = CALL_EXPR_ARG (exp, 0);
5643 tree arg1 = CALL_EXPR_ARG (exp, 1);
5644
5645 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5646 {
5647 error ("non-integer argument 1 to __atomic_is_lock_free");
5648 return NULL_RTX;
5649 }
5650
5651 if (!flag_inline_atomics)
5652 return NULL_RTX;
5653
5654 /* If the value is known at compile time, return the RTX for it. */
5655 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5656 if (size == boolean_true_node)
5657 return const1_rtx;
5658
5659 return NULL_RTX;
5660 }
5661
5662 /* Expand the __atomic_thread_fence intrinsic:
5663 void __atomic_thread_fence (enum memmodel)
5664 EXP is the CALL_EXPR. */
5665
5666 static void
5667 expand_builtin_atomic_thread_fence (tree exp)
5668 {
5669 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5670 expand_mem_thread_fence (model);
5671 }
5672
5673 /* Expand the __atomic_signal_fence intrinsic:
5674 void __atomic_signal_fence (enum memmodel)
5675 EXP is the CALL_EXPR. */
5676
5677 static void
5678 expand_builtin_atomic_signal_fence (tree exp)
5679 {
5680 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5681 expand_mem_signal_fence (model);
5682 }
5683
5684 /* Expand the __sync_synchronize intrinsic. */
5685
5686 static void
5687 expand_builtin_sync_synchronize (void)
5688 {
5689 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5690 }
5691
5692 static rtx
5693 expand_builtin_thread_pointer (tree exp, rtx target)
5694 {
5695 enum insn_code icode;
5696 if (!validate_arglist (exp, VOID_TYPE))
5697 return const0_rtx;
5698 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5699 if (icode != CODE_FOR_nothing)
5700 {
5701 struct expand_operand op;
5702 if (!REG_P (target) || GET_MODE (target) != Pmode)
5703 target = gen_reg_rtx (Pmode);
5704 create_output_operand (&op, target, Pmode);
5705 expand_insn (icode, 1, &op);
5706 return target;
5707 }
5708 error ("__builtin_thread_pointer is not supported on this target");
5709 return const0_rtx;
5710 }
5711
5712 static void
5713 expand_builtin_set_thread_pointer (tree exp)
5714 {
5715 enum insn_code icode;
5716 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5717 return;
5718 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5719 if (icode != CODE_FOR_nothing)
5720 {
5721 struct expand_operand op;
5722 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5723 Pmode, EXPAND_NORMAL);
5724 create_input_operand (&op, val, Pmode);
5725 expand_insn (icode, 1, &op);
5726 return;
5727 }
5728 error ("__builtin_set_thread_pointer is not supported on this target");
5729 }
5730
5731 \f
5732 /* Emit code to restore the current value of stack. */
5733
5734 static void
5735 expand_stack_restore (tree var)
5736 {
5737 rtx prev, sa = expand_normal (var);
5738
5739 sa = convert_memory_address (Pmode, sa);
5740
5741 prev = get_last_insn ();
5742 emit_stack_restore (SAVE_BLOCK, sa);
5743 fixup_args_size_notes (prev, get_last_insn (), 0);
5744 }
5745
5746
5747 /* Emit code to save the current value of stack. */
5748
5749 static rtx
5750 expand_stack_save (void)
5751 {
5752 rtx ret = NULL_RTX;
5753
5754 do_pending_stack_adjust ();
5755 emit_stack_save (SAVE_BLOCK, &ret);
5756 return ret;
5757 }
5758
5759 /* Expand an expression EXP that calls a built-in function,
5760 with result going to TARGET if that's convenient
5761 (and in mode MODE if that's convenient).
5762 SUBTARGET may be used as the target for computing one of EXP's operands.
5763 IGNORE is nonzero if the value is to be ignored. */
5764
5765 rtx
5766 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5767 int ignore)
5768 {
5769 tree fndecl = get_callee_fndecl (exp);
5770 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5771 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5772 int flags;
5773
5774 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5775 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5776
5777 /* When not optimizing, generate calls to library functions for a certain
5778 set of builtins. */
5779 if (!optimize
5780 && !called_as_built_in (fndecl)
5781 && fcode != BUILT_IN_FORK
5782 && fcode != BUILT_IN_EXECL
5783 && fcode != BUILT_IN_EXECV
5784 && fcode != BUILT_IN_EXECLP
5785 && fcode != BUILT_IN_EXECLE
5786 && fcode != BUILT_IN_EXECVP
5787 && fcode != BUILT_IN_EXECVE
5788 && fcode != BUILT_IN_ALLOCA
5789 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5790 && fcode != BUILT_IN_FREE)
5791 return expand_call (exp, target, ignore);
5792
5793 /* The built-in function expanders test for target == const0_rtx
5794 to determine whether the function's result will be ignored. */
5795 if (ignore)
5796 target = const0_rtx;
5797
5798 /* If the result of a pure or const built-in function is ignored, and
5799 none of its arguments are volatile, we can avoid expanding the
5800 built-in call and just evaluate the arguments for side-effects. */
5801 if (target == const0_rtx
5802 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5803 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5804 {
5805 bool volatilep = false;
5806 tree arg;
5807 call_expr_arg_iterator iter;
5808
5809 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5810 if (TREE_THIS_VOLATILE (arg))
5811 {
5812 volatilep = true;
5813 break;
5814 }
5815
5816 if (! volatilep)
5817 {
5818 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5819 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5820 return const0_rtx;
5821 }
5822 }
5823
5824 switch (fcode)
5825 {
5826 CASE_FLT_FN (BUILT_IN_FABS):
5827 case BUILT_IN_FABSD32:
5828 case BUILT_IN_FABSD64:
5829 case BUILT_IN_FABSD128:
5830 target = expand_builtin_fabs (exp, target, subtarget);
5831 if (target)
5832 return target;
5833 break;
5834
5835 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5836 target = expand_builtin_copysign (exp, target, subtarget);
5837 if (target)
5838 return target;
5839 break;
5840
5841 /* Just do a normal library call if we were unable to fold
5842 the values. */
5843 CASE_FLT_FN (BUILT_IN_CABS):
5844 break;
5845
5846 CASE_FLT_FN (BUILT_IN_EXP):
5847 CASE_FLT_FN (BUILT_IN_EXP10):
5848 CASE_FLT_FN (BUILT_IN_POW10):
5849 CASE_FLT_FN (BUILT_IN_EXP2):
5850 CASE_FLT_FN (BUILT_IN_EXPM1):
5851 CASE_FLT_FN (BUILT_IN_LOGB):
5852 CASE_FLT_FN (BUILT_IN_LOG):
5853 CASE_FLT_FN (BUILT_IN_LOG10):
5854 CASE_FLT_FN (BUILT_IN_LOG2):
5855 CASE_FLT_FN (BUILT_IN_LOG1P):
5856 CASE_FLT_FN (BUILT_IN_TAN):
5857 CASE_FLT_FN (BUILT_IN_ASIN):
5858 CASE_FLT_FN (BUILT_IN_ACOS):
5859 CASE_FLT_FN (BUILT_IN_ATAN):
5860 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5861 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5862 because of possible accuracy problems. */
5863 if (! flag_unsafe_math_optimizations)
5864 break;
5865 CASE_FLT_FN (BUILT_IN_SQRT):
5866 CASE_FLT_FN (BUILT_IN_FLOOR):
5867 CASE_FLT_FN (BUILT_IN_CEIL):
5868 CASE_FLT_FN (BUILT_IN_TRUNC):
5869 CASE_FLT_FN (BUILT_IN_ROUND):
5870 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5871 CASE_FLT_FN (BUILT_IN_RINT):
5872 target = expand_builtin_mathfn (exp, target, subtarget);
5873 if (target)
5874 return target;
5875 break;
5876
5877 CASE_FLT_FN (BUILT_IN_FMA):
5878 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5879 if (target)
5880 return target;
5881 break;
5882
5883 CASE_FLT_FN (BUILT_IN_ILOGB):
5884 if (! flag_unsafe_math_optimizations)
5885 break;
5886 CASE_FLT_FN (BUILT_IN_ISINF):
5887 CASE_FLT_FN (BUILT_IN_FINITE):
5888 case BUILT_IN_ISFINITE:
5889 case BUILT_IN_ISNORMAL:
5890 target = expand_builtin_interclass_mathfn (exp, target);
5891 if (target)
5892 return target;
5893 break;
5894
5895 CASE_FLT_FN (BUILT_IN_ICEIL):
5896 CASE_FLT_FN (BUILT_IN_LCEIL):
5897 CASE_FLT_FN (BUILT_IN_LLCEIL):
5898 CASE_FLT_FN (BUILT_IN_LFLOOR):
5899 CASE_FLT_FN (BUILT_IN_IFLOOR):
5900 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5901 target = expand_builtin_int_roundingfn (exp, target);
5902 if (target)
5903 return target;
5904 break;
5905
5906 CASE_FLT_FN (BUILT_IN_IRINT):
5907 CASE_FLT_FN (BUILT_IN_LRINT):
5908 CASE_FLT_FN (BUILT_IN_LLRINT):
5909 CASE_FLT_FN (BUILT_IN_IROUND):
5910 CASE_FLT_FN (BUILT_IN_LROUND):
5911 CASE_FLT_FN (BUILT_IN_LLROUND):
5912 target = expand_builtin_int_roundingfn_2 (exp, target);
5913 if (target)
5914 return target;
5915 break;
5916
5917 CASE_FLT_FN (BUILT_IN_POWI):
5918 target = expand_builtin_powi (exp, target);
5919 if (target)
5920 return target;
5921 break;
5922
5923 CASE_FLT_FN (BUILT_IN_ATAN2):
5924 CASE_FLT_FN (BUILT_IN_LDEXP):
5925 CASE_FLT_FN (BUILT_IN_SCALB):
5926 CASE_FLT_FN (BUILT_IN_SCALBN):
5927 CASE_FLT_FN (BUILT_IN_SCALBLN):
5928 if (! flag_unsafe_math_optimizations)
5929 break;
5930
5931 CASE_FLT_FN (BUILT_IN_FMOD):
5932 CASE_FLT_FN (BUILT_IN_REMAINDER):
5933 CASE_FLT_FN (BUILT_IN_DREM):
5934 CASE_FLT_FN (BUILT_IN_POW):
5935 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5936 if (target)
5937 return target;
5938 break;
5939
5940 CASE_FLT_FN (BUILT_IN_CEXPI):
5941 target = expand_builtin_cexpi (exp, target);
5942 gcc_assert (target);
5943 return target;
5944
5945 CASE_FLT_FN (BUILT_IN_SIN):
5946 CASE_FLT_FN (BUILT_IN_COS):
5947 if (! flag_unsafe_math_optimizations)
5948 break;
5949 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5950 if (target)
5951 return target;
5952 break;
5953
5954 CASE_FLT_FN (BUILT_IN_SINCOS):
5955 if (! flag_unsafe_math_optimizations)
5956 break;
5957 target = expand_builtin_sincos (exp);
5958 if (target)
5959 return target;
5960 break;
5961
5962 case BUILT_IN_APPLY_ARGS:
5963 return expand_builtin_apply_args ();
5964
5965 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5966 FUNCTION with a copy of the parameters described by
5967 ARGUMENTS, and ARGSIZE. It returns a block of memory
5968 allocated on the stack into which is stored all the registers
5969 that might possibly be used for returning the result of a
5970 function. ARGUMENTS is the value returned by
5971 __builtin_apply_args. ARGSIZE is the number of bytes of
5972 arguments that must be copied. ??? How should this value be
5973 computed? We'll also need a safe worst case value for varargs
5974 functions. */
5975 case BUILT_IN_APPLY:
5976 if (!validate_arglist (exp, POINTER_TYPE,
5977 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5978 && !validate_arglist (exp, REFERENCE_TYPE,
5979 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5980 return const0_rtx;
5981 else
5982 {
5983 rtx ops[3];
5984
5985 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5986 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5987 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5988
5989 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5990 }
5991
5992 /* __builtin_return (RESULT) causes the function to return the
5993 value described by RESULT. RESULT is address of the block of
5994 memory returned by __builtin_apply. */
5995 case BUILT_IN_RETURN:
5996 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5997 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5998 return const0_rtx;
5999
6000 case BUILT_IN_SAVEREGS:
6001 return expand_builtin_saveregs ();
6002
6003 case BUILT_IN_VA_ARG_PACK:
6004 /* All valid uses of __builtin_va_arg_pack () are removed during
6005 inlining. */
6006 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6007 return const0_rtx;
6008
6009 case BUILT_IN_VA_ARG_PACK_LEN:
6010 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6011 inlining. */
6012 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6013 return const0_rtx;
6014
6015 /* Return the address of the first anonymous stack arg. */
6016 case BUILT_IN_NEXT_ARG:
6017 if (fold_builtin_next_arg (exp, false))
6018 return const0_rtx;
6019 return expand_builtin_next_arg ();
6020
6021 case BUILT_IN_CLEAR_CACHE:
6022 target = expand_builtin___clear_cache (exp);
6023 if (target)
6024 return target;
6025 break;
6026
6027 case BUILT_IN_CLASSIFY_TYPE:
6028 return expand_builtin_classify_type (exp);
6029
6030 case BUILT_IN_CONSTANT_P:
6031 return const0_rtx;
6032
6033 case BUILT_IN_FRAME_ADDRESS:
6034 case BUILT_IN_RETURN_ADDRESS:
6035 return expand_builtin_frame_address (fndecl, exp);
6036
6037 /* Returns the address of the area where the structure is returned.
6038 0 otherwise. */
6039 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6040 if (call_expr_nargs (exp) != 0
6041 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6042 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6043 return const0_rtx;
6044 else
6045 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6046
6047 case BUILT_IN_ALLOCA:
6048 case BUILT_IN_ALLOCA_WITH_ALIGN:
6049 /* If the allocation stems from the declaration of a variable-sized
6050 object, it cannot accumulate. */
6051 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6052 if (target)
6053 return target;
6054 break;
6055
6056 case BUILT_IN_STACK_SAVE:
6057 return expand_stack_save ();
6058
6059 case BUILT_IN_STACK_RESTORE:
6060 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6061 return const0_rtx;
6062
6063 case BUILT_IN_BSWAP16:
6064 case BUILT_IN_BSWAP32:
6065 case BUILT_IN_BSWAP64:
6066 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6067 if (target)
6068 return target;
6069 break;
6070
6071 CASE_INT_FN (BUILT_IN_FFS):
6072 target = expand_builtin_unop (target_mode, exp, target,
6073 subtarget, ffs_optab);
6074 if (target)
6075 return target;
6076 break;
6077
6078 CASE_INT_FN (BUILT_IN_CLZ):
6079 target = expand_builtin_unop (target_mode, exp, target,
6080 subtarget, clz_optab);
6081 if (target)
6082 return target;
6083 break;
6084
6085 CASE_INT_FN (BUILT_IN_CTZ):
6086 target = expand_builtin_unop (target_mode, exp, target,
6087 subtarget, ctz_optab);
6088 if (target)
6089 return target;
6090 break;
6091
6092 CASE_INT_FN (BUILT_IN_CLRSB):
6093 target = expand_builtin_unop (target_mode, exp, target,
6094 subtarget, clrsb_optab);
6095 if (target)
6096 return target;
6097 break;
6098
6099 CASE_INT_FN (BUILT_IN_POPCOUNT):
6100 target = expand_builtin_unop (target_mode, exp, target,
6101 subtarget, popcount_optab);
6102 if (target)
6103 return target;
6104 break;
6105
6106 CASE_INT_FN (BUILT_IN_PARITY):
6107 target = expand_builtin_unop (target_mode, exp, target,
6108 subtarget, parity_optab);
6109 if (target)
6110 return target;
6111 break;
6112
6113 case BUILT_IN_STRLEN:
6114 target = expand_builtin_strlen (exp, target, target_mode);
6115 if (target)
6116 return target;
6117 break;
6118
6119 case BUILT_IN_STRCPY:
6120 target = expand_builtin_strcpy (exp, target);
6121 if (target)
6122 return target;
6123 break;
6124
6125 case BUILT_IN_STRNCPY:
6126 target = expand_builtin_strncpy (exp, target);
6127 if (target)
6128 return target;
6129 break;
6130
6131 case BUILT_IN_STPCPY:
6132 target = expand_builtin_stpcpy (exp, target, mode);
6133 if (target)
6134 return target;
6135 break;
6136
6137 case BUILT_IN_MEMCPY:
6138 target = expand_builtin_memcpy (exp, target);
6139 if (target)
6140 return target;
6141 break;
6142
6143 case BUILT_IN_MEMPCPY:
6144 target = expand_builtin_mempcpy (exp, target, mode);
6145 if (target)
6146 return target;
6147 break;
6148
6149 case BUILT_IN_MEMSET:
6150 target = expand_builtin_memset (exp, target, mode);
6151 if (target)
6152 return target;
6153 break;
6154
6155 case BUILT_IN_BZERO:
6156 target = expand_builtin_bzero (exp);
6157 if (target)
6158 return target;
6159 break;
6160
6161 case BUILT_IN_STRCMP:
6162 target = expand_builtin_strcmp (exp, target);
6163 if (target)
6164 return target;
6165 break;
6166
6167 case BUILT_IN_STRNCMP:
6168 target = expand_builtin_strncmp (exp, target, mode);
6169 if (target)
6170 return target;
6171 break;
6172
6173 case BUILT_IN_BCMP:
6174 case BUILT_IN_MEMCMP:
6175 target = expand_builtin_memcmp (exp, target, mode);
6176 if (target)
6177 return target;
6178 break;
6179
6180 case BUILT_IN_SETJMP:
6181 /* This should have been lowered to the builtins below. */
6182 gcc_unreachable ();
6183
6184 case BUILT_IN_SETJMP_SETUP:
6185 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6186 and the receiver label. */
6187 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6188 {
6189 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6190 VOIDmode, EXPAND_NORMAL);
6191 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6192 rtx label_r = label_rtx (label);
6193
6194 /* This is copied from the handling of non-local gotos. */
6195 expand_builtin_setjmp_setup (buf_addr, label_r);
6196 nonlocal_goto_handler_labels
6197 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6198 nonlocal_goto_handler_labels);
6199 /* ??? Do not let expand_label treat us as such since we would
6200 not want to be both on the list of non-local labels and on
6201 the list of forced labels. */
6202 FORCED_LABEL (label) = 0;
6203 return const0_rtx;
6204 }
6205 break;
6206
6207 case BUILT_IN_SETJMP_DISPATCHER:
6208 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6209 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6210 {
6211 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6212 rtx label_r = label_rtx (label);
6213
6214 /* Remove the dispatcher label from the list of non-local labels
6215 since the receiver labels have been added to it above. */
6216 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6217 return const0_rtx;
6218 }
6219 break;
6220
6221 case BUILT_IN_SETJMP_RECEIVER:
6222 /* __builtin_setjmp_receiver is passed the receiver label. */
6223 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6224 {
6225 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6226 rtx label_r = label_rtx (label);
6227
6228 expand_builtin_setjmp_receiver (label_r);
6229 return const0_rtx;
6230 }
6231 break;
6232
6233 /* __builtin_longjmp is passed a pointer to an array of five words.
6234 It's similar to the C library longjmp function but works with
6235 __builtin_setjmp above. */
6236 case BUILT_IN_LONGJMP:
6237 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6238 {
6239 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6240 VOIDmode, EXPAND_NORMAL);
6241 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6242
6243 if (value != const1_rtx)
6244 {
6245 error ("%<__builtin_longjmp%> second argument must be 1");
6246 return const0_rtx;
6247 }
6248
6249 expand_builtin_longjmp (buf_addr, value);
6250 return const0_rtx;
6251 }
6252 break;
6253
6254 case BUILT_IN_NONLOCAL_GOTO:
6255 target = expand_builtin_nonlocal_goto (exp);
6256 if (target)
6257 return target;
6258 break;
6259
6260 /* This updates the setjmp buffer that is its argument with the value
6261 of the current stack pointer. */
6262 case BUILT_IN_UPDATE_SETJMP_BUF:
6263 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6264 {
6265 rtx buf_addr
6266 = expand_normal (CALL_EXPR_ARG (exp, 0));
6267
6268 expand_builtin_update_setjmp_buf (buf_addr);
6269 return const0_rtx;
6270 }
6271 break;
6272
6273 case BUILT_IN_TRAP:
6274 expand_builtin_trap ();
6275 return const0_rtx;
6276
6277 case BUILT_IN_UNREACHABLE:
6278 expand_builtin_unreachable ();
6279 return const0_rtx;
6280
6281 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6282 case BUILT_IN_SIGNBITD32:
6283 case BUILT_IN_SIGNBITD64:
6284 case BUILT_IN_SIGNBITD128:
6285 target = expand_builtin_signbit (exp, target);
6286 if (target)
6287 return target;
6288 break;
6289
6290 /* Various hooks for the DWARF 2 __throw routine. */
6291 case BUILT_IN_UNWIND_INIT:
6292 expand_builtin_unwind_init ();
6293 return const0_rtx;
6294 case BUILT_IN_DWARF_CFA:
6295 return virtual_cfa_rtx;
6296 #ifdef DWARF2_UNWIND_INFO
6297 case BUILT_IN_DWARF_SP_COLUMN:
6298 return expand_builtin_dwarf_sp_column ();
6299 case BUILT_IN_INIT_DWARF_REG_SIZES:
6300 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6301 return const0_rtx;
6302 #endif
6303 case BUILT_IN_FROB_RETURN_ADDR:
6304 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6305 case BUILT_IN_EXTRACT_RETURN_ADDR:
6306 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6307 case BUILT_IN_EH_RETURN:
6308 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6309 CALL_EXPR_ARG (exp, 1));
6310 return const0_rtx;
6311 #ifdef EH_RETURN_DATA_REGNO
6312 case BUILT_IN_EH_RETURN_DATA_REGNO:
6313 return expand_builtin_eh_return_data_regno (exp);
6314 #endif
6315 case BUILT_IN_EXTEND_POINTER:
6316 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6317 case BUILT_IN_EH_POINTER:
6318 return expand_builtin_eh_pointer (exp);
6319 case BUILT_IN_EH_FILTER:
6320 return expand_builtin_eh_filter (exp);
6321 case BUILT_IN_EH_COPY_VALUES:
6322 return expand_builtin_eh_copy_values (exp);
6323
6324 case BUILT_IN_VA_START:
6325 return expand_builtin_va_start (exp);
6326 case BUILT_IN_VA_END:
6327 return expand_builtin_va_end (exp);
6328 case BUILT_IN_VA_COPY:
6329 return expand_builtin_va_copy (exp);
6330 case BUILT_IN_EXPECT:
6331 return expand_builtin_expect (exp, target);
6332 case BUILT_IN_ASSUME_ALIGNED:
6333 return expand_builtin_assume_aligned (exp, target);
6334 case BUILT_IN_PREFETCH:
6335 expand_builtin_prefetch (exp);
6336 return const0_rtx;
6337
6338 case BUILT_IN_INIT_TRAMPOLINE:
6339 return expand_builtin_init_trampoline (exp, true);
6340 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6341 return expand_builtin_init_trampoline (exp, false);
6342 case BUILT_IN_ADJUST_TRAMPOLINE:
6343 return expand_builtin_adjust_trampoline (exp);
6344
6345 case BUILT_IN_FORK:
6346 case BUILT_IN_EXECL:
6347 case BUILT_IN_EXECV:
6348 case BUILT_IN_EXECLP:
6349 case BUILT_IN_EXECLE:
6350 case BUILT_IN_EXECVP:
6351 case BUILT_IN_EXECVE:
6352 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6353 if (target)
6354 return target;
6355 break;
6356
6357 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6358 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6359 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6360 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6361 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6362 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6363 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6364 if (target)
6365 return target;
6366 break;
6367
6368 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6369 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6370 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6371 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6372 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6373 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6374 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6375 if (target)
6376 return target;
6377 break;
6378
6379 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6380 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6381 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6382 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6383 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6384 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6385 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6386 if (target)
6387 return target;
6388 break;
6389
6390 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6391 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6392 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6393 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6394 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6395 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6396 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6397 if (target)
6398 return target;
6399 break;
6400
6401 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6402 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6403 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6404 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6405 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6406 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6407 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6408 if (target)
6409 return target;
6410 break;
6411
6412 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6413 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6414 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6415 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6416 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6417 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6418 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6419 if (target)
6420 return target;
6421 break;
6422
6423 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6424 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6425 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6426 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6427 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6430 if (target)
6431 return target;
6432 break;
6433
6434 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6435 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6436 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6437 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6438 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6439 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6440 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6441 if (target)
6442 return target;
6443 break;
6444
6445 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6446 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6447 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6448 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6449 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6450 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6451 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6452 if (target)
6453 return target;
6454 break;
6455
6456 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6457 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6458 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6459 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6460 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6461 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6462 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6463 if (target)
6464 return target;
6465 break;
6466
6467 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6468 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6469 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6470 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6471 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6472 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6473 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6474 if (target)
6475 return target;
6476 break;
6477
6478 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6479 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6480 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6481 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6482 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6483 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6484 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6485 if (target)
6486 return target;
6487 break;
6488
6489 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6490 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6491 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6492 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6493 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6494 if (mode == VOIDmode)
6495 mode = TYPE_MODE (boolean_type_node);
6496 if (!target || !register_operand (target, mode))
6497 target = gen_reg_rtx (mode);
6498
6499 mode = get_builtin_sync_mode
6500 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6501 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6502 if (target)
6503 return target;
6504 break;
6505
6506 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6507 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6508 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6509 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6510 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6511 mode = get_builtin_sync_mode
6512 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6513 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6514 if (target)
6515 return target;
6516 break;
6517
6518 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6519 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6520 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6521 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6522 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6523 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6524 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6525 if (target)
6526 return target;
6527 break;
6528
6529 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6530 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6531 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6532 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6533 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6534 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6535 expand_builtin_sync_lock_release (mode, exp);
6536 return const0_rtx;
6537
6538 case BUILT_IN_SYNC_SYNCHRONIZE:
6539 expand_builtin_sync_synchronize ();
6540 return const0_rtx;
6541
6542 case BUILT_IN_ATOMIC_EXCHANGE_1:
6543 case BUILT_IN_ATOMIC_EXCHANGE_2:
6544 case BUILT_IN_ATOMIC_EXCHANGE_4:
6545 case BUILT_IN_ATOMIC_EXCHANGE_8:
6546 case BUILT_IN_ATOMIC_EXCHANGE_16:
6547 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6548 target = expand_builtin_atomic_exchange (mode, exp, target);
6549 if (target)
6550 return target;
6551 break;
6552
6553 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6554 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6555 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6556 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6557 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6558 {
6559 unsigned int nargs, z;
6560 vec<tree, va_gc> *vec;
6561
6562 mode =
6563 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6564 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6565 if (target)
6566 return target;
6567
6568 /* If this is turned into an external library call, the weak parameter
6569 must be dropped to match the expected parameter list. */
6570 nargs = call_expr_nargs (exp);
6571 vec_alloc (vec, nargs - 1);
6572 for (z = 0; z < 3; z++)
6573 vec->quick_push (CALL_EXPR_ARG (exp, z));
6574 /* Skip the boolean weak parameter. */
6575 for (z = 4; z < 6; z++)
6576 vec->quick_push (CALL_EXPR_ARG (exp, z));
6577 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6578 break;
6579 }
6580
6581 case BUILT_IN_ATOMIC_LOAD_1:
6582 case BUILT_IN_ATOMIC_LOAD_2:
6583 case BUILT_IN_ATOMIC_LOAD_4:
6584 case BUILT_IN_ATOMIC_LOAD_8:
6585 case BUILT_IN_ATOMIC_LOAD_16:
6586 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6587 target = expand_builtin_atomic_load (mode, exp, target);
6588 if (target)
6589 return target;
6590 break;
6591
6592 case BUILT_IN_ATOMIC_STORE_1:
6593 case BUILT_IN_ATOMIC_STORE_2:
6594 case BUILT_IN_ATOMIC_STORE_4:
6595 case BUILT_IN_ATOMIC_STORE_8:
6596 case BUILT_IN_ATOMIC_STORE_16:
6597 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6598 target = expand_builtin_atomic_store (mode, exp);
6599 if (target)
6600 return const0_rtx;
6601 break;
6602
6603 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6604 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6605 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6606 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6607 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6608 {
6609 enum built_in_function lib;
6610 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6611 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6612 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6613 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6614 ignore, lib);
6615 if (target)
6616 return target;
6617 break;
6618 }
6619 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6620 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6621 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6622 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6623 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6624 {
6625 enum built_in_function lib;
6626 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6627 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6628 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6629 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6630 ignore, lib);
6631 if (target)
6632 return target;
6633 break;
6634 }
6635 case BUILT_IN_ATOMIC_AND_FETCH_1:
6636 case BUILT_IN_ATOMIC_AND_FETCH_2:
6637 case BUILT_IN_ATOMIC_AND_FETCH_4:
6638 case BUILT_IN_ATOMIC_AND_FETCH_8:
6639 case BUILT_IN_ATOMIC_AND_FETCH_16:
6640 {
6641 enum built_in_function lib;
6642 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6643 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6644 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6645 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6646 ignore, lib);
6647 if (target)
6648 return target;
6649 break;
6650 }
6651 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6652 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6653 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6654 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6655 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6656 {
6657 enum built_in_function lib;
6658 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6659 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6660 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6661 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6662 ignore, lib);
6663 if (target)
6664 return target;
6665 break;
6666 }
6667 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6668 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6669 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6670 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6671 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6672 {
6673 enum built_in_function lib;
6674 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6675 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6676 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6677 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6678 ignore, lib);
6679 if (target)
6680 return target;
6681 break;
6682 }
6683 case BUILT_IN_ATOMIC_OR_FETCH_1:
6684 case BUILT_IN_ATOMIC_OR_FETCH_2:
6685 case BUILT_IN_ATOMIC_OR_FETCH_4:
6686 case BUILT_IN_ATOMIC_OR_FETCH_8:
6687 case BUILT_IN_ATOMIC_OR_FETCH_16:
6688 {
6689 enum built_in_function lib;
6690 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6691 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6692 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6693 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6694 ignore, lib);
6695 if (target)
6696 return target;
6697 break;
6698 }
6699 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6700 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6701 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6702 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6703 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6704 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6705 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6706 ignore, BUILT_IN_NONE);
6707 if (target)
6708 return target;
6709 break;
6710
6711 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6712 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6713 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6714 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6715 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6716 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6717 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6718 ignore, BUILT_IN_NONE);
6719 if (target)
6720 return target;
6721 break;
6722
6723 case BUILT_IN_ATOMIC_FETCH_AND_1:
6724 case BUILT_IN_ATOMIC_FETCH_AND_2:
6725 case BUILT_IN_ATOMIC_FETCH_AND_4:
6726 case BUILT_IN_ATOMIC_FETCH_AND_8:
6727 case BUILT_IN_ATOMIC_FETCH_AND_16:
6728 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6729 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6730 ignore, BUILT_IN_NONE);
6731 if (target)
6732 return target;
6733 break;
6734
6735 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6736 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6737 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6738 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6739 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6740 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6741 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6742 ignore, BUILT_IN_NONE);
6743 if (target)
6744 return target;
6745 break;
6746
6747 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6748 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6749 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6750 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6751 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6752 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6753 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6754 ignore, BUILT_IN_NONE);
6755 if (target)
6756 return target;
6757 break;
6758
6759 case BUILT_IN_ATOMIC_FETCH_OR_1:
6760 case BUILT_IN_ATOMIC_FETCH_OR_2:
6761 case BUILT_IN_ATOMIC_FETCH_OR_4:
6762 case BUILT_IN_ATOMIC_FETCH_OR_8:
6763 case BUILT_IN_ATOMIC_FETCH_OR_16:
6764 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6765 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6766 ignore, BUILT_IN_NONE);
6767 if (target)
6768 return target;
6769 break;
6770
6771 case BUILT_IN_ATOMIC_TEST_AND_SET:
6772 return expand_builtin_atomic_test_and_set (exp, target);
6773
6774 case BUILT_IN_ATOMIC_CLEAR:
6775 return expand_builtin_atomic_clear (exp);
6776
6777 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6778 return expand_builtin_atomic_always_lock_free (exp);
6779
6780 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6781 target = expand_builtin_atomic_is_lock_free (exp);
6782 if (target)
6783 return target;
6784 break;
6785
6786 case BUILT_IN_ATOMIC_THREAD_FENCE:
6787 expand_builtin_atomic_thread_fence (exp);
6788 return const0_rtx;
6789
6790 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6791 expand_builtin_atomic_signal_fence (exp);
6792 return const0_rtx;
6793
6794 case BUILT_IN_OBJECT_SIZE:
6795 return expand_builtin_object_size (exp);
6796
6797 case BUILT_IN_MEMCPY_CHK:
6798 case BUILT_IN_MEMPCPY_CHK:
6799 case BUILT_IN_MEMMOVE_CHK:
6800 case BUILT_IN_MEMSET_CHK:
6801 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6802 if (target)
6803 return target;
6804 break;
6805
6806 case BUILT_IN_STRCPY_CHK:
6807 case BUILT_IN_STPCPY_CHK:
6808 case BUILT_IN_STRNCPY_CHK:
6809 case BUILT_IN_STPNCPY_CHK:
6810 case BUILT_IN_STRCAT_CHK:
6811 case BUILT_IN_STRNCAT_CHK:
6812 case BUILT_IN_SNPRINTF_CHK:
6813 case BUILT_IN_VSNPRINTF_CHK:
6814 maybe_emit_chk_warning (exp, fcode);
6815 break;
6816
6817 case BUILT_IN_SPRINTF_CHK:
6818 case BUILT_IN_VSPRINTF_CHK:
6819 maybe_emit_sprintf_chk_warning (exp, fcode);
6820 break;
6821
6822 case BUILT_IN_FREE:
6823 if (warn_free_nonheap_object)
6824 maybe_emit_free_warning (exp);
6825 break;
6826
6827 case BUILT_IN_THREAD_POINTER:
6828 return expand_builtin_thread_pointer (exp, target);
6829
6830 case BUILT_IN_SET_THREAD_POINTER:
6831 expand_builtin_set_thread_pointer (exp);
6832 return const0_rtx;
6833
6834 case BUILT_IN_CILK_DETACH:
6835 expand_builtin_cilk_detach (exp);
6836 return const0_rtx;
6837
6838 case BUILT_IN_CILK_POP_FRAME:
6839 expand_builtin_cilk_pop_frame (exp);
6840 return const0_rtx;
6841
6842 default: /* just do library call, if unknown builtin */
6843 break;
6844 }
6845
6846 /* The switch statement above can drop through to cause the function
6847 to be called normally. */
6848 return expand_call (exp, target, ignore);
6849 }
6850
6851 /* Determine whether a tree node represents a call to a built-in
6852 function. If the tree T is a call to a built-in function with
6853 the right number of arguments of the appropriate types, return
6854 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6855 Otherwise the return value is END_BUILTINS. */
6856
6857 enum built_in_function
6858 builtin_mathfn_code (const_tree t)
6859 {
6860 const_tree fndecl, arg, parmlist;
6861 const_tree argtype, parmtype;
6862 const_call_expr_arg_iterator iter;
6863
6864 if (TREE_CODE (t) != CALL_EXPR
6865 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6866 return END_BUILTINS;
6867
6868 fndecl = get_callee_fndecl (t);
6869 if (fndecl == NULL_TREE
6870 || TREE_CODE (fndecl) != FUNCTION_DECL
6871 || ! DECL_BUILT_IN (fndecl)
6872 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6873 return END_BUILTINS;
6874
6875 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6876 init_const_call_expr_arg_iterator (t, &iter);
6877 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6878 {
6879 /* If a function doesn't take a variable number of arguments,
6880 the last element in the list will have type `void'. */
6881 parmtype = TREE_VALUE (parmlist);
6882 if (VOID_TYPE_P (parmtype))
6883 {
6884 if (more_const_call_expr_args_p (&iter))
6885 return END_BUILTINS;
6886 return DECL_FUNCTION_CODE (fndecl);
6887 }
6888
6889 if (! more_const_call_expr_args_p (&iter))
6890 return END_BUILTINS;
6891
6892 arg = next_const_call_expr_arg (&iter);
6893 argtype = TREE_TYPE (arg);
6894
6895 if (SCALAR_FLOAT_TYPE_P (parmtype))
6896 {
6897 if (! SCALAR_FLOAT_TYPE_P (argtype))
6898 return END_BUILTINS;
6899 }
6900 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6901 {
6902 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6903 return END_BUILTINS;
6904 }
6905 else if (POINTER_TYPE_P (parmtype))
6906 {
6907 if (! POINTER_TYPE_P (argtype))
6908 return END_BUILTINS;
6909 }
6910 else if (INTEGRAL_TYPE_P (parmtype))
6911 {
6912 if (! INTEGRAL_TYPE_P (argtype))
6913 return END_BUILTINS;
6914 }
6915 else
6916 return END_BUILTINS;
6917 }
6918
6919 /* Variable-length argument list. */
6920 return DECL_FUNCTION_CODE (fndecl);
6921 }
6922
6923 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6924 evaluate to a constant. */
6925
6926 static tree
6927 fold_builtin_constant_p (tree arg)
6928 {
6929 /* We return 1 for a numeric type that's known to be a constant
6930 value at compile-time or for an aggregate type that's a
6931 literal constant. */
6932 STRIP_NOPS (arg);
6933
6934 /* If we know this is a constant, emit the constant of one. */
6935 if (CONSTANT_CLASS_P (arg)
6936 || (TREE_CODE (arg) == CONSTRUCTOR
6937 && TREE_CONSTANT (arg)))
6938 return integer_one_node;
6939 if (TREE_CODE (arg) == ADDR_EXPR)
6940 {
6941 tree op = TREE_OPERAND (arg, 0);
6942 if (TREE_CODE (op) == STRING_CST
6943 || (TREE_CODE (op) == ARRAY_REF
6944 && integer_zerop (TREE_OPERAND (op, 1))
6945 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6946 return integer_one_node;
6947 }
6948
6949 /* If this expression has side effects, show we don't know it to be a
6950 constant. Likewise if it's a pointer or aggregate type since in
6951 those case we only want literals, since those are only optimized
6952 when generating RTL, not later.
6953 And finally, if we are compiling an initializer, not code, we
6954 need to return a definite result now; there's not going to be any
6955 more optimization done. */
6956 if (TREE_SIDE_EFFECTS (arg)
6957 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6958 || POINTER_TYPE_P (TREE_TYPE (arg))
6959 || cfun == 0
6960 || folding_initializer
6961 || force_folding_builtin_constant_p)
6962 return integer_zero_node;
6963
6964 return NULL_TREE;
6965 }
6966
6967 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6968 return it as a truthvalue. */
6969
6970 static tree
6971 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6972 {
6973 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6974
6975 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6976 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6977 ret_type = TREE_TYPE (TREE_TYPE (fn));
6978 pred_type = TREE_VALUE (arg_types);
6979 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6980
6981 pred = fold_convert_loc (loc, pred_type, pred);
6982 expected = fold_convert_loc (loc, expected_type, expected);
6983 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6984
6985 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6986 build_int_cst (ret_type, 0));
6987 }
6988
6989 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6990 NULL_TREE if no simplification is possible. */
6991
6992 static tree
6993 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6994 {
6995 tree inner, fndecl, inner_arg0;
6996 enum tree_code code;
6997
6998 /* Distribute the expected value over short-circuiting operators.
6999 See through the cast from truthvalue_type_node to long. */
7000 inner_arg0 = arg0;
7001 while (TREE_CODE (inner_arg0) == NOP_EXPR
7002 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7003 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7004 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7005
7006 /* If this is a builtin_expect within a builtin_expect keep the
7007 inner one. See through a comparison against a constant. It
7008 might have been added to create a thruthvalue. */
7009 inner = inner_arg0;
7010
7011 if (COMPARISON_CLASS_P (inner)
7012 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7013 inner = TREE_OPERAND (inner, 0);
7014
7015 if (TREE_CODE (inner) == CALL_EXPR
7016 && (fndecl = get_callee_fndecl (inner))
7017 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7018 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7019 return arg0;
7020
7021 inner = inner_arg0;
7022 code = TREE_CODE (inner);
7023 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7024 {
7025 tree op0 = TREE_OPERAND (inner, 0);
7026 tree op1 = TREE_OPERAND (inner, 1);
7027
7028 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7029 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7030 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7031
7032 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7033 }
7034
7035 /* If the argument isn't invariant then there's nothing else we can do. */
7036 if (!TREE_CONSTANT (inner_arg0))
7037 return NULL_TREE;
7038
7039 /* If we expect that a comparison against the argument will fold to
7040 a constant return the constant. In practice, this means a true
7041 constant or the address of a non-weak symbol. */
7042 inner = inner_arg0;
7043 STRIP_NOPS (inner);
7044 if (TREE_CODE (inner) == ADDR_EXPR)
7045 {
7046 do
7047 {
7048 inner = TREE_OPERAND (inner, 0);
7049 }
7050 while (TREE_CODE (inner) == COMPONENT_REF
7051 || TREE_CODE (inner) == ARRAY_REF);
7052 if ((TREE_CODE (inner) == VAR_DECL
7053 || TREE_CODE (inner) == FUNCTION_DECL)
7054 && DECL_WEAK (inner))
7055 return NULL_TREE;
7056 }
7057
7058 /* Otherwise, ARG0 already has the proper type for the return value. */
7059 return arg0;
7060 }
7061
7062 /* Fold a call to __builtin_classify_type with argument ARG. */
7063
7064 static tree
7065 fold_builtin_classify_type (tree arg)
7066 {
7067 if (arg == 0)
7068 return build_int_cst (integer_type_node, no_type_class);
7069
7070 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7071 }
7072
7073 /* Fold a call to __builtin_strlen with argument ARG. */
7074
7075 static tree
7076 fold_builtin_strlen (location_t loc, tree type, tree arg)
7077 {
7078 if (!validate_arg (arg, POINTER_TYPE))
7079 return NULL_TREE;
7080 else
7081 {
7082 tree len = c_strlen (arg, 0);
7083
7084 if (len)
7085 return fold_convert_loc (loc, type, len);
7086
7087 return NULL_TREE;
7088 }
7089 }
7090
7091 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7092
7093 static tree
7094 fold_builtin_inf (location_t loc, tree type, int warn)
7095 {
7096 REAL_VALUE_TYPE real;
7097
7098 /* __builtin_inff is intended to be usable to define INFINITY on all
7099 targets. If an infinity is not available, INFINITY expands "to a
7100 positive constant of type float that overflows at translation
7101 time", footnote "In this case, using INFINITY will violate the
7102 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7103 Thus we pedwarn to ensure this constraint violation is
7104 diagnosed. */
7105 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7106 pedwarn (loc, 0, "target format does not support infinity");
7107
7108 real_inf (&real);
7109 return build_real (type, real);
7110 }
7111
7112 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7113
7114 static tree
7115 fold_builtin_nan (tree arg, tree type, int quiet)
7116 {
7117 REAL_VALUE_TYPE real;
7118 const char *str;
7119
7120 if (!validate_arg (arg, POINTER_TYPE))
7121 return NULL_TREE;
7122 str = c_getstr (arg);
7123 if (!str)
7124 return NULL_TREE;
7125
7126 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7127 return NULL_TREE;
7128
7129 return build_real (type, real);
7130 }
7131
7132 /* Return true if the floating point expression T has an integer value.
7133 We also allow +Inf, -Inf and NaN to be considered integer values. */
7134
7135 static bool
7136 integer_valued_real_p (tree t)
7137 {
7138 switch (TREE_CODE (t))
7139 {
7140 case FLOAT_EXPR:
7141 return true;
7142
7143 case ABS_EXPR:
7144 case SAVE_EXPR:
7145 return integer_valued_real_p (TREE_OPERAND (t, 0));
7146
7147 case COMPOUND_EXPR:
7148 case MODIFY_EXPR:
7149 case BIND_EXPR:
7150 return integer_valued_real_p (TREE_OPERAND (t, 1));
7151
7152 case PLUS_EXPR:
7153 case MINUS_EXPR:
7154 case MULT_EXPR:
7155 case MIN_EXPR:
7156 case MAX_EXPR:
7157 return integer_valued_real_p (TREE_OPERAND (t, 0))
7158 && integer_valued_real_p (TREE_OPERAND (t, 1));
7159
7160 case COND_EXPR:
7161 return integer_valued_real_p (TREE_OPERAND (t, 1))
7162 && integer_valued_real_p (TREE_OPERAND (t, 2));
7163
7164 case REAL_CST:
7165 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7166
7167 case NOP_EXPR:
7168 {
7169 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7170 if (TREE_CODE (type) == INTEGER_TYPE)
7171 return true;
7172 if (TREE_CODE (type) == REAL_TYPE)
7173 return integer_valued_real_p (TREE_OPERAND (t, 0));
7174 break;
7175 }
7176
7177 case CALL_EXPR:
7178 switch (builtin_mathfn_code (t))
7179 {
7180 CASE_FLT_FN (BUILT_IN_CEIL):
7181 CASE_FLT_FN (BUILT_IN_FLOOR):
7182 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7183 CASE_FLT_FN (BUILT_IN_RINT):
7184 CASE_FLT_FN (BUILT_IN_ROUND):
7185 CASE_FLT_FN (BUILT_IN_TRUNC):
7186 return true;
7187
7188 CASE_FLT_FN (BUILT_IN_FMIN):
7189 CASE_FLT_FN (BUILT_IN_FMAX):
7190 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7191 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7192
7193 default:
7194 break;
7195 }
7196 break;
7197
7198 default:
7199 break;
7200 }
7201 return false;
7202 }
7203
7204 /* FNDECL is assumed to be a builtin where truncation can be propagated
7205 across (for instance floor((double)f) == (double)floorf (f).
7206 Do the transformation for a call with argument ARG. */
7207
7208 static tree
7209 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7210 {
7211 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7212
7213 if (!validate_arg (arg, REAL_TYPE))
7214 return NULL_TREE;
7215
7216 /* Integer rounding functions are idempotent. */
7217 if (fcode == builtin_mathfn_code (arg))
7218 return arg;
7219
7220 /* If argument is already integer valued, and we don't need to worry
7221 about setting errno, there's no need to perform rounding. */
7222 if (! flag_errno_math && integer_valued_real_p (arg))
7223 return arg;
7224
7225 if (optimize)
7226 {
7227 tree arg0 = strip_float_extensions (arg);
7228 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7229 tree newtype = TREE_TYPE (arg0);
7230 tree decl;
7231
7232 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7233 && (decl = mathfn_built_in (newtype, fcode)))
7234 return fold_convert_loc (loc, ftype,
7235 build_call_expr_loc (loc, decl, 1,
7236 fold_convert_loc (loc,
7237 newtype,
7238 arg0)));
7239 }
7240 return NULL_TREE;
7241 }
7242
7243 /* FNDECL is assumed to be builtin which can narrow the FP type of
7244 the argument, for instance lround((double)f) -> lroundf (f).
7245 Do the transformation for a call with argument ARG. */
7246
7247 static tree
7248 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7249 {
7250 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7251
7252 if (!validate_arg (arg, REAL_TYPE))
7253 return NULL_TREE;
7254
7255 /* If argument is already integer valued, and we don't need to worry
7256 about setting errno, there's no need to perform rounding. */
7257 if (! flag_errno_math && integer_valued_real_p (arg))
7258 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7259 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7260
7261 if (optimize)
7262 {
7263 tree ftype = TREE_TYPE (arg);
7264 tree arg0 = strip_float_extensions (arg);
7265 tree newtype = TREE_TYPE (arg0);
7266 tree decl;
7267
7268 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7269 && (decl = mathfn_built_in (newtype, fcode)))
7270 return build_call_expr_loc (loc, decl, 1,
7271 fold_convert_loc (loc, newtype, arg0));
7272 }
7273
7274 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7275 sizeof (int) == sizeof (long). */
7276 if (TYPE_PRECISION (integer_type_node)
7277 == TYPE_PRECISION (long_integer_type_node))
7278 {
7279 tree newfn = NULL_TREE;
7280 switch (fcode)
7281 {
7282 CASE_FLT_FN (BUILT_IN_ICEIL):
7283 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7284 break;
7285
7286 CASE_FLT_FN (BUILT_IN_IFLOOR):
7287 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7288 break;
7289
7290 CASE_FLT_FN (BUILT_IN_IROUND):
7291 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7292 break;
7293
7294 CASE_FLT_FN (BUILT_IN_IRINT):
7295 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7296 break;
7297
7298 default:
7299 break;
7300 }
7301
7302 if (newfn)
7303 {
7304 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7305 return fold_convert_loc (loc,
7306 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7307 }
7308 }
7309
7310 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7311 sizeof (long long) == sizeof (long). */
7312 if (TYPE_PRECISION (long_long_integer_type_node)
7313 == TYPE_PRECISION (long_integer_type_node))
7314 {
7315 tree newfn = NULL_TREE;
7316 switch (fcode)
7317 {
7318 CASE_FLT_FN (BUILT_IN_LLCEIL):
7319 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7320 break;
7321
7322 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7323 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7324 break;
7325
7326 CASE_FLT_FN (BUILT_IN_LLROUND):
7327 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7328 break;
7329
7330 CASE_FLT_FN (BUILT_IN_LLRINT):
7331 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7332 break;
7333
7334 default:
7335 break;
7336 }
7337
7338 if (newfn)
7339 {
7340 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7341 return fold_convert_loc (loc,
7342 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7343 }
7344 }
7345
7346 return NULL_TREE;
7347 }
7348
7349 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7350 return type. Return NULL_TREE if no simplification can be made. */
7351
7352 static tree
7353 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7354 {
7355 tree res;
7356
7357 if (!validate_arg (arg, COMPLEX_TYPE)
7358 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7359 return NULL_TREE;
7360
7361 /* Calculate the result when the argument is a constant. */
7362 if (TREE_CODE (arg) == COMPLEX_CST
7363 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7364 type, mpfr_hypot)))
7365 return res;
7366
7367 if (TREE_CODE (arg) == COMPLEX_EXPR)
7368 {
7369 tree real = TREE_OPERAND (arg, 0);
7370 tree imag = TREE_OPERAND (arg, 1);
7371
7372 /* If either part is zero, cabs is fabs of the other. */
7373 if (real_zerop (real))
7374 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7375 if (real_zerop (imag))
7376 return fold_build1_loc (loc, ABS_EXPR, type, real);
7377
7378 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7379 if (flag_unsafe_math_optimizations
7380 && operand_equal_p (real, imag, OEP_PURE_SAME))
7381 {
7382 const REAL_VALUE_TYPE sqrt2_trunc
7383 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7384 STRIP_NOPS (real);
7385 return fold_build2_loc (loc, MULT_EXPR, type,
7386 fold_build1_loc (loc, ABS_EXPR, type, real),
7387 build_real (type, sqrt2_trunc));
7388 }
7389 }
7390
7391 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7392 if (TREE_CODE (arg) == NEGATE_EXPR
7393 || TREE_CODE (arg) == CONJ_EXPR)
7394 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7395
7396 /* Don't do this when optimizing for size. */
7397 if (flag_unsafe_math_optimizations
7398 && optimize && optimize_function_for_speed_p (cfun))
7399 {
7400 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7401
7402 if (sqrtfn != NULL_TREE)
7403 {
7404 tree rpart, ipart, result;
7405
7406 arg = builtin_save_expr (arg);
7407
7408 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7409 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7410
7411 rpart = builtin_save_expr (rpart);
7412 ipart = builtin_save_expr (ipart);
7413
7414 result = fold_build2_loc (loc, PLUS_EXPR, type,
7415 fold_build2_loc (loc, MULT_EXPR, type,
7416 rpart, rpart),
7417 fold_build2_loc (loc, MULT_EXPR, type,
7418 ipart, ipart));
7419
7420 return build_call_expr_loc (loc, sqrtfn, 1, result);
7421 }
7422 }
7423
7424 return NULL_TREE;
7425 }
7426
7427 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7428 complex tree type of the result. If NEG is true, the imaginary
7429 zero is negative. */
7430
7431 static tree
7432 build_complex_cproj (tree type, bool neg)
7433 {
7434 REAL_VALUE_TYPE rinf, rzero = dconst0;
7435
7436 real_inf (&rinf);
7437 rzero.sign = neg;
7438 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7439 build_real (TREE_TYPE (type), rzero));
7440 }
7441
7442 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7443 return type. Return NULL_TREE if no simplification can be made. */
7444
7445 static tree
7446 fold_builtin_cproj (location_t loc, tree arg, tree type)
7447 {
7448 if (!validate_arg (arg, COMPLEX_TYPE)
7449 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7450 return NULL_TREE;
7451
7452 /* If there are no infinities, return arg. */
7453 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7454 return non_lvalue_loc (loc, arg);
7455
7456 /* Calculate the result when the argument is a constant. */
7457 if (TREE_CODE (arg) == COMPLEX_CST)
7458 {
7459 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7460 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7461
7462 if (real_isinf (real) || real_isinf (imag))
7463 return build_complex_cproj (type, imag->sign);
7464 else
7465 return arg;
7466 }
7467 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7468 {
7469 tree real = TREE_OPERAND (arg, 0);
7470 tree imag = TREE_OPERAND (arg, 1);
7471
7472 STRIP_NOPS (real);
7473 STRIP_NOPS (imag);
7474
7475 /* If the real part is inf and the imag part is known to be
7476 nonnegative, return (inf + 0i). Remember side-effects are
7477 possible in the imag part. */
7478 if (TREE_CODE (real) == REAL_CST
7479 && real_isinf (TREE_REAL_CST_PTR (real))
7480 && tree_expr_nonnegative_p (imag))
7481 return omit_one_operand_loc (loc, type,
7482 build_complex_cproj (type, false),
7483 arg);
7484
7485 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7486 Remember side-effects are possible in the real part. */
7487 if (TREE_CODE (imag) == REAL_CST
7488 && real_isinf (TREE_REAL_CST_PTR (imag)))
7489 return
7490 omit_one_operand_loc (loc, type,
7491 build_complex_cproj (type, TREE_REAL_CST_PTR
7492 (imag)->sign), arg);
7493 }
7494
7495 return NULL_TREE;
7496 }
7497
7498 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7499 Return NULL_TREE if no simplification can be made. */
7500
7501 static tree
7502 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7503 {
7504
7505 enum built_in_function fcode;
7506 tree res;
7507
7508 if (!validate_arg (arg, REAL_TYPE))
7509 return NULL_TREE;
7510
7511 /* Calculate the result when the argument is a constant. */
7512 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7513 return res;
7514
7515 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7516 fcode = builtin_mathfn_code (arg);
7517 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7518 {
7519 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7520 arg = fold_build2_loc (loc, MULT_EXPR, type,
7521 CALL_EXPR_ARG (arg, 0),
7522 build_real (type, dconsthalf));
7523 return build_call_expr_loc (loc, expfn, 1, arg);
7524 }
7525
7526 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7527 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7528 {
7529 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7530
7531 if (powfn)
7532 {
7533 tree arg0 = CALL_EXPR_ARG (arg, 0);
7534 tree tree_root;
7535 /* The inner root was either sqrt or cbrt. */
7536 /* This was a conditional expression but it triggered a bug
7537 in Sun C 5.5. */
7538 REAL_VALUE_TYPE dconstroot;
7539 if (BUILTIN_SQRT_P (fcode))
7540 dconstroot = dconsthalf;
7541 else
7542 dconstroot = dconst_third ();
7543
7544 /* Adjust for the outer root. */
7545 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7546 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7547 tree_root = build_real (type, dconstroot);
7548 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7549 }
7550 }
7551
7552 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7553 if (flag_unsafe_math_optimizations
7554 && (fcode == BUILT_IN_POW
7555 || fcode == BUILT_IN_POWF
7556 || fcode == BUILT_IN_POWL))
7557 {
7558 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7559 tree arg0 = CALL_EXPR_ARG (arg, 0);
7560 tree arg1 = CALL_EXPR_ARG (arg, 1);
7561 tree narg1;
7562 if (!tree_expr_nonnegative_p (arg0))
7563 arg0 = build1 (ABS_EXPR, type, arg0);
7564 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7565 build_real (type, dconsthalf));
7566 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7567 }
7568
7569 return NULL_TREE;
7570 }
7571
7572 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7573 Return NULL_TREE if no simplification can be made. */
7574
7575 static tree
7576 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7577 {
7578 const enum built_in_function fcode = builtin_mathfn_code (arg);
7579 tree res;
7580
7581 if (!validate_arg (arg, REAL_TYPE))
7582 return NULL_TREE;
7583
7584 /* Calculate the result when the argument is a constant. */
7585 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7586 return res;
7587
7588 if (flag_unsafe_math_optimizations)
7589 {
7590 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7591 if (BUILTIN_EXPONENT_P (fcode))
7592 {
7593 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7594 const REAL_VALUE_TYPE third_trunc =
7595 real_value_truncate (TYPE_MODE (type), dconst_third ());
7596 arg = fold_build2_loc (loc, MULT_EXPR, type,
7597 CALL_EXPR_ARG (arg, 0),
7598 build_real (type, third_trunc));
7599 return build_call_expr_loc (loc, expfn, 1, arg);
7600 }
7601
7602 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7603 if (BUILTIN_SQRT_P (fcode))
7604 {
7605 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7606
7607 if (powfn)
7608 {
7609 tree arg0 = CALL_EXPR_ARG (arg, 0);
7610 tree tree_root;
7611 REAL_VALUE_TYPE dconstroot = dconst_third ();
7612
7613 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7614 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7615 tree_root = build_real (type, dconstroot);
7616 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7617 }
7618 }
7619
7620 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7621 if (BUILTIN_CBRT_P (fcode))
7622 {
7623 tree arg0 = CALL_EXPR_ARG (arg, 0);
7624 if (tree_expr_nonnegative_p (arg0))
7625 {
7626 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7627
7628 if (powfn)
7629 {
7630 tree tree_root;
7631 REAL_VALUE_TYPE dconstroot;
7632
7633 real_arithmetic (&dconstroot, MULT_EXPR,
7634 dconst_third_ptr (), dconst_third_ptr ());
7635 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7636 tree_root = build_real (type, dconstroot);
7637 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7638 }
7639 }
7640 }
7641
7642 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7643 if (fcode == BUILT_IN_POW
7644 || fcode == BUILT_IN_POWF
7645 || fcode == BUILT_IN_POWL)
7646 {
7647 tree arg00 = CALL_EXPR_ARG (arg, 0);
7648 tree arg01 = CALL_EXPR_ARG (arg, 1);
7649 if (tree_expr_nonnegative_p (arg00))
7650 {
7651 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7652 const REAL_VALUE_TYPE dconstroot
7653 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7654 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7655 build_real (type, dconstroot));
7656 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7657 }
7658 }
7659 }
7660 return NULL_TREE;
7661 }
7662
7663 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7664 TYPE is the type of the return value. Return NULL_TREE if no
7665 simplification can be made. */
7666
7667 static tree
7668 fold_builtin_cos (location_t loc,
7669 tree arg, tree type, tree fndecl)
7670 {
7671 tree res, narg;
7672
7673 if (!validate_arg (arg, REAL_TYPE))
7674 return NULL_TREE;
7675
7676 /* Calculate the result when the argument is a constant. */
7677 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7678 return res;
7679
7680 /* Optimize cos(-x) into cos (x). */
7681 if ((narg = fold_strip_sign_ops (arg)))
7682 return build_call_expr_loc (loc, fndecl, 1, narg);
7683
7684 return NULL_TREE;
7685 }
7686
7687 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7688 Return NULL_TREE if no simplification can be made. */
7689
7690 static tree
7691 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7692 {
7693 if (validate_arg (arg, REAL_TYPE))
7694 {
7695 tree res, narg;
7696
7697 /* Calculate the result when the argument is a constant. */
7698 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7699 return res;
7700
7701 /* Optimize cosh(-x) into cosh (x). */
7702 if ((narg = fold_strip_sign_ops (arg)))
7703 return build_call_expr_loc (loc, fndecl, 1, narg);
7704 }
7705
7706 return NULL_TREE;
7707 }
7708
7709 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7710 argument ARG. TYPE is the type of the return value. Return
7711 NULL_TREE if no simplification can be made. */
7712
7713 static tree
7714 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7715 bool hyper)
7716 {
7717 if (validate_arg (arg, COMPLEX_TYPE)
7718 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7719 {
7720 tree tmp;
7721
7722 /* Calculate the result when the argument is a constant. */
7723 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7724 return tmp;
7725
7726 /* Optimize fn(-x) into fn(x). */
7727 if ((tmp = fold_strip_sign_ops (arg)))
7728 return build_call_expr_loc (loc, fndecl, 1, tmp);
7729 }
7730
7731 return NULL_TREE;
7732 }
7733
7734 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7735 Return NULL_TREE if no simplification can be made. */
7736
7737 static tree
7738 fold_builtin_tan (tree arg, tree type)
7739 {
7740 enum built_in_function fcode;
7741 tree res;
7742
7743 if (!validate_arg (arg, REAL_TYPE))
7744 return NULL_TREE;
7745
7746 /* Calculate the result when the argument is a constant. */
7747 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7748 return res;
7749
7750 /* Optimize tan(atan(x)) = x. */
7751 fcode = builtin_mathfn_code (arg);
7752 if (flag_unsafe_math_optimizations
7753 && (fcode == BUILT_IN_ATAN
7754 || fcode == BUILT_IN_ATANF
7755 || fcode == BUILT_IN_ATANL))
7756 return CALL_EXPR_ARG (arg, 0);
7757
7758 return NULL_TREE;
7759 }
7760
7761 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7762 NULL_TREE if no simplification can be made. */
7763
7764 static tree
7765 fold_builtin_sincos (location_t loc,
7766 tree arg0, tree arg1, tree arg2)
7767 {
7768 tree type;
7769 tree res, fn, call;
7770
7771 if (!validate_arg (arg0, REAL_TYPE)
7772 || !validate_arg (arg1, POINTER_TYPE)
7773 || !validate_arg (arg2, POINTER_TYPE))
7774 return NULL_TREE;
7775
7776 type = TREE_TYPE (arg0);
7777
7778 /* Calculate the result when the argument is a constant. */
7779 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7780 return res;
7781
7782 /* Canonicalize sincos to cexpi. */
7783 if (!targetm.libc_has_function (function_c99_math_complex))
7784 return NULL_TREE;
7785 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7786 if (!fn)
7787 return NULL_TREE;
7788
7789 call = build_call_expr_loc (loc, fn, 1, arg0);
7790 call = builtin_save_expr (call);
7791
7792 return build2 (COMPOUND_EXPR, void_type_node,
7793 build2 (MODIFY_EXPR, void_type_node,
7794 build_fold_indirect_ref_loc (loc, arg1),
7795 build1 (IMAGPART_EXPR, type, call)),
7796 build2 (MODIFY_EXPR, void_type_node,
7797 build_fold_indirect_ref_loc (loc, arg2),
7798 build1 (REALPART_EXPR, type, call)));
7799 }
7800
7801 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7802 NULL_TREE if no simplification can be made. */
7803
7804 static tree
7805 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7806 {
7807 tree rtype;
7808 tree realp, imagp, ifn;
7809 tree res;
7810
7811 if (!validate_arg (arg0, COMPLEX_TYPE)
7812 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7813 return NULL_TREE;
7814
7815 /* Calculate the result when the argument is a constant. */
7816 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7817 return res;
7818
7819 rtype = TREE_TYPE (TREE_TYPE (arg0));
7820
7821 /* In case we can figure out the real part of arg0 and it is constant zero
7822 fold to cexpi. */
7823 if (!targetm.libc_has_function (function_c99_math_complex))
7824 return NULL_TREE;
7825 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7826 if (!ifn)
7827 return NULL_TREE;
7828
7829 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7830 && real_zerop (realp))
7831 {
7832 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7833 return build_call_expr_loc (loc, ifn, 1, narg);
7834 }
7835
7836 /* In case we can easily decompose real and imaginary parts split cexp
7837 to exp (r) * cexpi (i). */
7838 if (flag_unsafe_math_optimizations
7839 && realp)
7840 {
7841 tree rfn, rcall, icall;
7842
7843 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7844 if (!rfn)
7845 return NULL_TREE;
7846
7847 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7848 if (!imagp)
7849 return NULL_TREE;
7850
7851 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7852 icall = builtin_save_expr (icall);
7853 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7854 rcall = builtin_save_expr (rcall);
7855 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7856 fold_build2_loc (loc, MULT_EXPR, rtype,
7857 rcall,
7858 fold_build1_loc (loc, REALPART_EXPR,
7859 rtype, icall)),
7860 fold_build2_loc (loc, MULT_EXPR, rtype,
7861 rcall,
7862 fold_build1_loc (loc, IMAGPART_EXPR,
7863 rtype, icall)));
7864 }
7865
7866 return NULL_TREE;
7867 }
7868
7869 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7870 Return NULL_TREE if no simplification can be made. */
7871
7872 static tree
7873 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7874 {
7875 if (!validate_arg (arg, REAL_TYPE))
7876 return NULL_TREE;
7877
7878 /* Optimize trunc of constant value. */
7879 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7880 {
7881 REAL_VALUE_TYPE r, x;
7882 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7883
7884 x = TREE_REAL_CST (arg);
7885 real_trunc (&r, TYPE_MODE (type), &x);
7886 return build_real (type, r);
7887 }
7888
7889 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7890 }
7891
7892 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7893 Return NULL_TREE if no simplification can be made. */
7894
7895 static tree
7896 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7897 {
7898 if (!validate_arg (arg, REAL_TYPE))
7899 return NULL_TREE;
7900
7901 /* Optimize floor of constant value. */
7902 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7903 {
7904 REAL_VALUE_TYPE x;
7905
7906 x = TREE_REAL_CST (arg);
7907 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7908 {
7909 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7910 REAL_VALUE_TYPE r;
7911
7912 real_floor (&r, TYPE_MODE (type), &x);
7913 return build_real (type, r);
7914 }
7915 }
7916
7917 /* Fold floor (x) where x is nonnegative to trunc (x). */
7918 if (tree_expr_nonnegative_p (arg))
7919 {
7920 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7921 if (truncfn)
7922 return build_call_expr_loc (loc, truncfn, 1, arg);
7923 }
7924
7925 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7926 }
7927
7928 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7929 Return NULL_TREE if no simplification can be made. */
7930
7931 static tree
7932 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7933 {
7934 if (!validate_arg (arg, REAL_TYPE))
7935 return NULL_TREE;
7936
7937 /* Optimize ceil of constant value. */
7938 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7939 {
7940 REAL_VALUE_TYPE x;
7941
7942 x = TREE_REAL_CST (arg);
7943 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7944 {
7945 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7946 REAL_VALUE_TYPE r;
7947
7948 real_ceil (&r, TYPE_MODE (type), &x);
7949 return build_real (type, r);
7950 }
7951 }
7952
7953 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7954 }
7955
7956 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7957 Return NULL_TREE if no simplification can be made. */
7958
7959 static tree
7960 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7961 {
7962 if (!validate_arg (arg, REAL_TYPE))
7963 return NULL_TREE;
7964
7965 /* Optimize round of constant value. */
7966 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7967 {
7968 REAL_VALUE_TYPE x;
7969
7970 x = TREE_REAL_CST (arg);
7971 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7972 {
7973 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7974 REAL_VALUE_TYPE r;
7975
7976 real_round (&r, TYPE_MODE (type), &x);
7977 return build_real (type, r);
7978 }
7979 }
7980
7981 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7982 }
7983
7984 /* Fold function call to builtin lround, lroundf or lroundl (or the
7985 corresponding long long versions) and other rounding functions. ARG
7986 is the argument to the call. Return NULL_TREE if no simplification
7987 can be made. */
7988
7989 static tree
7990 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7991 {
7992 if (!validate_arg (arg, REAL_TYPE))
7993 return NULL_TREE;
7994
7995 /* Optimize lround of constant value. */
7996 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7997 {
7998 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7999
8000 if (real_isfinite (&x))
8001 {
8002 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8003 tree ftype = TREE_TYPE (arg);
8004 double_int val;
8005 REAL_VALUE_TYPE r;
8006
8007 switch (DECL_FUNCTION_CODE (fndecl))
8008 {
8009 CASE_FLT_FN (BUILT_IN_IFLOOR):
8010 CASE_FLT_FN (BUILT_IN_LFLOOR):
8011 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8012 real_floor (&r, TYPE_MODE (ftype), &x);
8013 break;
8014
8015 CASE_FLT_FN (BUILT_IN_ICEIL):
8016 CASE_FLT_FN (BUILT_IN_LCEIL):
8017 CASE_FLT_FN (BUILT_IN_LLCEIL):
8018 real_ceil (&r, TYPE_MODE (ftype), &x);
8019 break;
8020
8021 CASE_FLT_FN (BUILT_IN_IROUND):
8022 CASE_FLT_FN (BUILT_IN_LROUND):
8023 CASE_FLT_FN (BUILT_IN_LLROUND):
8024 real_round (&r, TYPE_MODE (ftype), &x);
8025 break;
8026
8027 default:
8028 gcc_unreachable ();
8029 }
8030
8031 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8032 if (double_int_fits_to_tree_p (itype, val))
8033 return double_int_to_tree (itype, val);
8034 }
8035 }
8036
8037 switch (DECL_FUNCTION_CODE (fndecl))
8038 {
8039 CASE_FLT_FN (BUILT_IN_LFLOOR):
8040 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8041 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8042 if (tree_expr_nonnegative_p (arg))
8043 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8044 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8045 break;
8046 default:;
8047 }
8048
8049 return fold_fixed_mathfn (loc, fndecl, arg);
8050 }
8051
8052 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8053 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8054 the argument to the call. Return NULL_TREE if no simplification can
8055 be made. */
8056
8057 static tree
8058 fold_builtin_bitop (tree fndecl, tree arg)
8059 {
8060 if (!validate_arg (arg, INTEGER_TYPE))
8061 return NULL_TREE;
8062
8063 /* Optimize for constant argument. */
8064 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8065 {
8066 HOST_WIDE_INT hi, width, result;
8067 unsigned HOST_WIDE_INT lo;
8068 tree type;
8069
8070 type = TREE_TYPE (arg);
8071 width = TYPE_PRECISION (type);
8072 lo = TREE_INT_CST_LOW (arg);
8073
8074 /* Clear all the bits that are beyond the type's precision. */
8075 if (width > HOST_BITS_PER_WIDE_INT)
8076 {
8077 hi = TREE_INT_CST_HIGH (arg);
8078 if (width < HOST_BITS_PER_DOUBLE_INT)
8079 hi &= ~(HOST_WIDE_INT_M1U << (width - HOST_BITS_PER_WIDE_INT));
8080 }
8081 else
8082 {
8083 hi = 0;
8084 if (width < HOST_BITS_PER_WIDE_INT)
8085 lo &= ~(HOST_WIDE_INT_M1U << width);
8086 }
8087
8088 switch (DECL_FUNCTION_CODE (fndecl))
8089 {
8090 CASE_INT_FN (BUILT_IN_FFS):
8091 if (lo != 0)
8092 result = ffs_hwi (lo);
8093 else if (hi != 0)
8094 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8095 else
8096 result = 0;
8097 break;
8098
8099 CASE_INT_FN (BUILT_IN_CLZ):
8100 if (hi != 0)
8101 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8102 else if (lo != 0)
8103 result = width - floor_log2 (lo) - 1;
8104 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8105 result = width;
8106 break;
8107
8108 CASE_INT_FN (BUILT_IN_CTZ):
8109 if (lo != 0)
8110 result = ctz_hwi (lo);
8111 else if (hi != 0)
8112 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8113 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8114 result = width;
8115 break;
8116
8117 CASE_INT_FN (BUILT_IN_CLRSB):
8118 if (width > 2 * HOST_BITS_PER_WIDE_INT)
8119 return NULL_TREE;
8120 if (width > HOST_BITS_PER_WIDE_INT
8121 && (hi & ((unsigned HOST_WIDE_INT) 1
8122 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8123 {
8124 hi = ~hi & ~(HOST_WIDE_INT_M1U
8125 << (width - HOST_BITS_PER_WIDE_INT - 1));
8126 lo = ~lo;
8127 }
8128 else if (width <= HOST_BITS_PER_WIDE_INT
8129 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8130 lo = ~lo & ~(HOST_WIDE_INT_M1U << (width - 1));
8131 if (hi != 0)
8132 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8133 else if (lo != 0)
8134 result = width - floor_log2 (lo) - 2;
8135 else
8136 result = width - 1;
8137 break;
8138
8139 CASE_INT_FN (BUILT_IN_POPCOUNT):
8140 result = 0;
8141 while (lo)
8142 result++, lo &= lo - 1;
8143 while (hi)
8144 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8145 break;
8146
8147 CASE_INT_FN (BUILT_IN_PARITY):
8148 result = 0;
8149 while (lo)
8150 result++, lo &= lo - 1;
8151 while (hi)
8152 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8153 result &= 1;
8154 break;
8155
8156 default:
8157 gcc_unreachable ();
8158 }
8159
8160 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8161 }
8162
8163 return NULL_TREE;
8164 }
8165
8166 /* Fold function call to builtin_bswap and the short, long and long long
8167 variants. Return NULL_TREE if no simplification can be made. */
8168 static tree
8169 fold_builtin_bswap (tree fndecl, tree arg)
8170 {
8171 if (! validate_arg (arg, INTEGER_TYPE))
8172 return NULL_TREE;
8173
8174 /* Optimize constant value. */
8175 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8176 {
8177 HOST_WIDE_INT hi, width, r_hi = 0;
8178 unsigned HOST_WIDE_INT lo, r_lo = 0;
8179 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8180
8181 width = TYPE_PRECISION (type);
8182 lo = TREE_INT_CST_LOW (arg);
8183 hi = TREE_INT_CST_HIGH (arg);
8184
8185 switch (DECL_FUNCTION_CODE (fndecl))
8186 {
8187 case BUILT_IN_BSWAP16:
8188 case BUILT_IN_BSWAP32:
8189 case BUILT_IN_BSWAP64:
8190 {
8191 int s;
8192
8193 for (s = 0; s < width; s += 8)
8194 {
8195 int d = width - s - 8;
8196 unsigned HOST_WIDE_INT byte;
8197
8198 if (s < HOST_BITS_PER_WIDE_INT)
8199 byte = (lo >> s) & 0xff;
8200 else
8201 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8202
8203 if (d < HOST_BITS_PER_WIDE_INT)
8204 r_lo |= byte << d;
8205 else
8206 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8207 }
8208 }
8209
8210 break;
8211
8212 default:
8213 gcc_unreachable ();
8214 }
8215
8216 if (width < HOST_BITS_PER_WIDE_INT)
8217 return build_int_cst (type, r_lo);
8218 else
8219 return build_int_cst_wide (type, r_lo, r_hi);
8220 }
8221
8222 return NULL_TREE;
8223 }
8224
8225 /* A subroutine of fold_builtin to fold the various logarithmic
8226 functions. Return NULL_TREE if no simplification can me made.
8227 FUNC is the corresponding MPFR logarithm function. */
8228
8229 static tree
8230 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8231 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8232 {
8233 if (validate_arg (arg, REAL_TYPE))
8234 {
8235 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8236 tree res;
8237 const enum built_in_function fcode = builtin_mathfn_code (arg);
8238
8239 /* Calculate the result when the argument is a constant. */
8240 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8241 return res;
8242
8243 /* Special case, optimize logN(expN(x)) = x. */
8244 if (flag_unsafe_math_optimizations
8245 && ((func == mpfr_log
8246 && (fcode == BUILT_IN_EXP
8247 || fcode == BUILT_IN_EXPF
8248 || fcode == BUILT_IN_EXPL))
8249 || (func == mpfr_log2
8250 && (fcode == BUILT_IN_EXP2
8251 || fcode == BUILT_IN_EXP2F
8252 || fcode == BUILT_IN_EXP2L))
8253 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8254 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8255
8256 /* Optimize logN(func()) for various exponential functions. We
8257 want to determine the value "x" and the power "exponent" in
8258 order to transform logN(x**exponent) into exponent*logN(x). */
8259 if (flag_unsafe_math_optimizations)
8260 {
8261 tree exponent = 0, x = 0;
8262
8263 switch (fcode)
8264 {
8265 CASE_FLT_FN (BUILT_IN_EXP):
8266 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8267 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8268 dconst_e ()));
8269 exponent = CALL_EXPR_ARG (arg, 0);
8270 break;
8271 CASE_FLT_FN (BUILT_IN_EXP2):
8272 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8273 x = build_real (type, dconst2);
8274 exponent = CALL_EXPR_ARG (arg, 0);
8275 break;
8276 CASE_FLT_FN (BUILT_IN_EXP10):
8277 CASE_FLT_FN (BUILT_IN_POW10):
8278 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8279 {
8280 REAL_VALUE_TYPE dconst10;
8281 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8282 x = build_real (type, dconst10);
8283 }
8284 exponent = CALL_EXPR_ARG (arg, 0);
8285 break;
8286 CASE_FLT_FN (BUILT_IN_SQRT):
8287 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8288 x = CALL_EXPR_ARG (arg, 0);
8289 exponent = build_real (type, dconsthalf);
8290 break;
8291 CASE_FLT_FN (BUILT_IN_CBRT):
8292 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8293 x = CALL_EXPR_ARG (arg, 0);
8294 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8295 dconst_third ()));
8296 break;
8297 CASE_FLT_FN (BUILT_IN_POW):
8298 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8299 x = CALL_EXPR_ARG (arg, 0);
8300 exponent = CALL_EXPR_ARG (arg, 1);
8301 break;
8302 default:
8303 break;
8304 }
8305
8306 /* Now perform the optimization. */
8307 if (x && exponent)
8308 {
8309 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8310 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8311 }
8312 }
8313 }
8314
8315 return NULL_TREE;
8316 }
8317
8318 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8319 NULL_TREE if no simplification can be made. */
8320
8321 static tree
8322 fold_builtin_hypot (location_t loc, tree fndecl,
8323 tree arg0, tree arg1, tree type)
8324 {
8325 tree res, narg0, narg1;
8326
8327 if (!validate_arg (arg0, REAL_TYPE)
8328 || !validate_arg (arg1, REAL_TYPE))
8329 return NULL_TREE;
8330
8331 /* Calculate the result when the argument is a constant. */
8332 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8333 return res;
8334
8335 /* If either argument to hypot has a negate or abs, strip that off.
8336 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8337 narg0 = fold_strip_sign_ops (arg0);
8338 narg1 = fold_strip_sign_ops (arg1);
8339 if (narg0 || narg1)
8340 {
8341 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8342 narg1 ? narg1 : arg1);
8343 }
8344
8345 /* If either argument is zero, hypot is fabs of the other. */
8346 if (real_zerop (arg0))
8347 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8348 else if (real_zerop (arg1))
8349 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8350
8351 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8352 if (flag_unsafe_math_optimizations
8353 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8354 {
8355 const REAL_VALUE_TYPE sqrt2_trunc
8356 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8357 return fold_build2_loc (loc, MULT_EXPR, type,
8358 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8359 build_real (type, sqrt2_trunc));
8360 }
8361
8362 return NULL_TREE;
8363 }
8364
8365
8366 /* Fold a builtin function call to pow, powf, or powl. Return
8367 NULL_TREE if no simplification can be made. */
8368 static tree
8369 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8370 {
8371 tree res;
8372
8373 if (!validate_arg (arg0, REAL_TYPE)
8374 || !validate_arg (arg1, REAL_TYPE))
8375 return NULL_TREE;
8376
8377 /* Calculate the result when the argument is a constant. */
8378 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8379 return res;
8380
8381 /* Optimize pow(1.0,y) = 1.0. */
8382 if (real_onep (arg0))
8383 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8384
8385 if (TREE_CODE (arg1) == REAL_CST
8386 && !TREE_OVERFLOW (arg1))
8387 {
8388 REAL_VALUE_TYPE cint;
8389 REAL_VALUE_TYPE c;
8390 HOST_WIDE_INT n;
8391
8392 c = TREE_REAL_CST (arg1);
8393
8394 /* Optimize pow(x,0.0) = 1.0. */
8395 if (REAL_VALUES_EQUAL (c, dconst0))
8396 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8397 arg0);
8398
8399 /* Optimize pow(x,1.0) = x. */
8400 if (REAL_VALUES_EQUAL (c, dconst1))
8401 return arg0;
8402
8403 /* Optimize pow(x,-1.0) = 1.0/x. */
8404 if (REAL_VALUES_EQUAL (c, dconstm1))
8405 return fold_build2_loc (loc, RDIV_EXPR, type,
8406 build_real (type, dconst1), arg0);
8407
8408 /* Optimize pow(x,0.5) = sqrt(x). */
8409 if (flag_unsafe_math_optimizations
8410 && REAL_VALUES_EQUAL (c, dconsthalf))
8411 {
8412 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8413
8414 if (sqrtfn != NULL_TREE)
8415 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8416 }
8417
8418 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8419 if (flag_unsafe_math_optimizations)
8420 {
8421 const REAL_VALUE_TYPE dconstroot
8422 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8423
8424 if (REAL_VALUES_EQUAL (c, dconstroot))
8425 {
8426 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8427 if (cbrtfn != NULL_TREE)
8428 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8429 }
8430 }
8431
8432 /* Check for an integer exponent. */
8433 n = real_to_integer (&c);
8434 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8435 if (real_identical (&c, &cint))
8436 {
8437 /* Attempt to evaluate pow at compile-time, unless this should
8438 raise an exception. */
8439 if (TREE_CODE (arg0) == REAL_CST
8440 && !TREE_OVERFLOW (arg0)
8441 && (n > 0
8442 || (!flag_trapping_math && !flag_errno_math)
8443 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8444 {
8445 REAL_VALUE_TYPE x;
8446 bool inexact;
8447
8448 x = TREE_REAL_CST (arg0);
8449 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8450 if (flag_unsafe_math_optimizations || !inexact)
8451 return build_real (type, x);
8452 }
8453
8454 /* Strip sign ops from even integer powers. */
8455 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8456 {
8457 tree narg0 = fold_strip_sign_ops (arg0);
8458 if (narg0)
8459 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8460 }
8461 }
8462 }
8463
8464 if (flag_unsafe_math_optimizations)
8465 {
8466 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8467
8468 /* Optimize pow(expN(x),y) = expN(x*y). */
8469 if (BUILTIN_EXPONENT_P (fcode))
8470 {
8471 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8472 tree arg = CALL_EXPR_ARG (arg0, 0);
8473 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8474 return build_call_expr_loc (loc, expfn, 1, arg);
8475 }
8476
8477 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8478 if (BUILTIN_SQRT_P (fcode))
8479 {
8480 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8481 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8482 build_real (type, dconsthalf));
8483 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8484 }
8485
8486 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8487 if (BUILTIN_CBRT_P (fcode))
8488 {
8489 tree arg = CALL_EXPR_ARG (arg0, 0);
8490 if (tree_expr_nonnegative_p (arg))
8491 {
8492 const REAL_VALUE_TYPE dconstroot
8493 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8494 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8495 build_real (type, dconstroot));
8496 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8497 }
8498 }
8499
8500 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8501 if (fcode == BUILT_IN_POW
8502 || fcode == BUILT_IN_POWF
8503 || fcode == BUILT_IN_POWL)
8504 {
8505 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8506 if (tree_expr_nonnegative_p (arg00))
8507 {
8508 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8509 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8510 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8511 }
8512 }
8513 }
8514
8515 return NULL_TREE;
8516 }
8517
8518 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8519 Return NULL_TREE if no simplification can be made. */
8520 static tree
8521 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8522 tree arg0, tree arg1, tree type)
8523 {
8524 if (!validate_arg (arg0, REAL_TYPE)
8525 || !validate_arg (arg1, INTEGER_TYPE))
8526 return NULL_TREE;
8527
8528 /* Optimize pow(1.0,y) = 1.0. */
8529 if (real_onep (arg0))
8530 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8531
8532 if (tree_fits_shwi_p (arg1))
8533 {
8534 HOST_WIDE_INT c = tree_to_shwi (arg1);
8535
8536 /* Evaluate powi at compile-time. */
8537 if (TREE_CODE (arg0) == REAL_CST
8538 && !TREE_OVERFLOW (arg0))
8539 {
8540 REAL_VALUE_TYPE x;
8541 x = TREE_REAL_CST (arg0);
8542 real_powi (&x, TYPE_MODE (type), &x, c);
8543 return build_real (type, x);
8544 }
8545
8546 /* Optimize pow(x,0) = 1.0. */
8547 if (c == 0)
8548 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8549 arg0);
8550
8551 /* Optimize pow(x,1) = x. */
8552 if (c == 1)
8553 return arg0;
8554
8555 /* Optimize pow(x,-1) = 1.0/x. */
8556 if (c == -1)
8557 return fold_build2_loc (loc, RDIV_EXPR, type,
8558 build_real (type, dconst1), arg0);
8559 }
8560
8561 return NULL_TREE;
8562 }
8563
8564 /* A subroutine of fold_builtin to fold the various exponent
8565 functions. Return NULL_TREE if no simplification can be made.
8566 FUNC is the corresponding MPFR exponent function. */
8567
8568 static tree
8569 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8570 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8571 {
8572 if (validate_arg (arg, REAL_TYPE))
8573 {
8574 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8575 tree res;
8576
8577 /* Calculate the result when the argument is a constant. */
8578 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8579 return res;
8580
8581 /* Optimize expN(logN(x)) = x. */
8582 if (flag_unsafe_math_optimizations)
8583 {
8584 const enum built_in_function fcode = builtin_mathfn_code (arg);
8585
8586 if ((func == mpfr_exp
8587 && (fcode == BUILT_IN_LOG
8588 || fcode == BUILT_IN_LOGF
8589 || fcode == BUILT_IN_LOGL))
8590 || (func == mpfr_exp2
8591 && (fcode == BUILT_IN_LOG2
8592 || fcode == BUILT_IN_LOG2F
8593 || fcode == BUILT_IN_LOG2L))
8594 || (func == mpfr_exp10
8595 && (fcode == BUILT_IN_LOG10
8596 || fcode == BUILT_IN_LOG10F
8597 || fcode == BUILT_IN_LOG10L)))
8598 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8599 }
8600 }
8601
8602 return NULL_TREE;
8603 }
8604
8605 /* Return true if VAR is a VAR_DECL or a component thereof. */
8606
8607 static bool
8608 var_decl_component_p (tree var)
8609 {
8610 tree inner = var;
8611 while (handled_component_p (inner))
8612 inner = TREE_OPERAND (inner, 0);
8613 return SSA_VAR_P (inner);
8614 }
8615
8616 /* Fold function call to builtin memset. Return
8617 NULL_TREE if no simplification can be made. */
8618
8619 static tree
8620 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8621 tree type, bool ignore)
8622 {
8623 tree var, ret, etype;
8624 unsigned HOST_WIDE_INT length, cval;
8625
8626 if (! validate_arg (dest, POINTER_TYPE)
8627 || ! validate_arg (c, INTEGER_TYPE)
8628 || ! validate_arg (len, INTEGER_TYPE))
8629 return NULL_TREE;
8630
8631 if (! tree_fits_uhwi_p (len))
8632 return NULL_TREE;
8633
8634 /* If the LEN parameter is zero, return DEST. */
8635 if (integer_zerop (len))
8636 return omit_one_operand_loc (loc, type, dest, c);
8637
8638 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8639 return NULL_TREE;
8640
8641 var = dest;
8642 STRIP_NOPS (var);
8643 if (TREE_CODE (var) != ADDR_EXPR)
8644 return NULL_TREE;
8645
8646 var = TREE_OPERAND (var, 0);
8647 if (TREE_THIS_VOLATILE (var))
8648 return NULL_TREE;
8649
8650 etype = TREE_TYPE (var);
8651 if (TREE_CODE (etype) == ARRAY_TYPE)
8652 etype = TREE_TYPE (etype);
8653
8654 if (!INTEGRAL_TYPE_P (etype)
8655 && !POINTER_TYPE_P (etype))
8656 return NULL_TREE;
8657
8658 if (! var_decl_component_p (var))
8659 return NULL_TREE;
8660
8661 length = tree_to_uhwi (len);
8662 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8663 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8664 return NULL_TREE;
8665
8666 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8667 return NULL_TREE;
8668
8669 if (integer_zerop (c))
8670 cval = 0;
8671 else
8672 {
8673 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8674 return NULL_TREE;
8675
8676 cval = TREE_INT_CST_LOW (c);
8677 cval &= 0xff;
8678 cval |= cval << 8;
8679 cval |= cval << 16;
8680 cval |= (cval << 31) << 1;
8681 }
8682
8683 ret = build_int_cst_type (etype, cval);
8684 var = build_fold_indirect_ref_loc (loc,
8685 fold_convert_loc (loc,
8686 build_pointer_type (etype),
8687 dest));
8688 ret = build2 (MODIFY_EXPR, etype, var, ret);
8689 if (ignore)
8690 return ret;
8691
8692 return omit_one_operand_loc (loc, type, dest, ret);
8693 }
8694
8695 /* Fold function call to builtin memset. Return
8696 NULL_TREE if no simplification can be made. */
8697
8698 static tree
8699 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8700 {
8701 if (! validate_arg (dest, POINTER_TYPE)
8702 || ! validate_arg (size, INTEGER_TYPE))
8703 return NULL_TREE;
8704
8705 if (!ignore)
8706 return NULL_TREE;
8707
8708 /* New argument list transforming bzero(ptr x, int y) to
8709 memset(ptr x, int 0, size_t y). This is done this way
8710 so that if it isn't expanded inline, we fallback to
8711 calling bzero instead of memset. */
8712
8713 return fold_builtin_memset (loc, dest, integer_zero_node,
8714 fold_convert_loc (loc, size_type_node, size),
8715 void_type_node, ignore);
8716 }
8717
8718 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8719 NULL_TREE if no simplification can be made.
8720 If ENDP is 0, return DEST (like memcpy).
8721 If ENDP is 1, return DEST+LEN (like mempcpy).
8722 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8723 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8724 (memmove). */
8725
8726 static tree
8727 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8728 tree len, tree type, bool ignore, int endp)
8729 {
8730 tree destvar, srcvar, expr;
8731
8732 if (! validate_arg (dest, POINTER_TYPE)
8733 || ! validate_arg (src, POINTER_TYPE)
8734 || ! validate_arg (len, INTEGER_TYPE))
8735 return NULL_TREE;
8736
8737 /* If the LEN parameter is zero, return DEST. */
8738 if (integer_zerop (len))
8739 return omit_one_operand_loc (loc, type, dest, src);
8740
8741 /* If SRC and DEST are the same (and not volatile), return
8742 DEST{,+LEN,+LEN-1}. */
8743 if (operand_equal_p (src, dest, 0))
8744 expr = len;
8745 else
8746 {
8747 tree srctype, desttype;
8748 unsigned int src_align, dest_align;
8749 tree off0;
8750
8751 if (endp == 3)
8752 {
8753 src_align = get_pointer_alignment (src);
8754 dest_align = get_pointer_alignment (dest);
8755
8756 /* Both DEST and SRC must be pointer types.
8757 ??? This is what old code did. Is the testing for pointer types
8758 really mandatory?
8759
8760 If either SRC is readonly or length is 1, we can use memcpy. */
8761 if (!dest_align || !src_align)
8762 return NULL_TREE;
8763 if (readonly_data_expr (src)
8764 || (tree_fits_uhwi_p (len)
8765 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8766 >= tree_to_uhwi (len))))
8767 {
8768 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8769 if (!fn)
8770 return NULL_TREE;
8771 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8772 }
8773
8774 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8775 if (TREE_CODE (src) == ADDR_EXPR
8776 && TREE_CODE (dest) == ADDR_EXPR)
8777 {
8778 tree src_base, dest_base, fn;
8779 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8780 HOST_WIDE_INT size = -1;
8781 HOST_WIDE_INT maxsize = -1;
8782
8783 srcvar = TREE_OPERAND (src, 0);
8784 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8785 &size, &maxsize);
8786 destvar = TREE_OPERAND (dest, 0);
8787 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8788 &size, &maxsize);
8789 if (tree_fits_uhwi_p (len))
8790 maxsize = tree_to_uhwi (len);
8791 else
8792 maxsize = -1;
8793 src_offset /= BITS_PER_UNIT;
8794 dest_offset /= BITS_PER_UNIT;
8795 if (SSA_VAR_P (src_base)
8796 && SSA_VAR_P (dest_base))
8797 {
8798 if (operand_equal_p (src_base, dest_base, 0)
8799 && ranges_overlap_p (src_offset, maxsize,
8800 dest_offset, maxsize))
8801 return NULL_TREE;
8802 }
8803 else if (TREE_CODE (src_base) == MEM_REF
8804 && TREE_CODE (dest_base) == MEM_REF)
8805 {
8806 double_int off;
8807 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8808 TREE_OPERAND (dest_base, 0), 0))
8809 return NULL_TREE;
8810 off = mem_ref_offset (src_base) +
8811 double_int::from_shwi (src_offset);
8812 if (!off.fits_shwi ())
8813 return NULL_TREE;
8814 src_offset = off.low;
8815 off = mem_ref_offset (dest_base) +
8816 double_int::from_shwi (dest_offset);
8817 if (!off.fits_shwi ())
8818 return NULL_TREE;
8819 dest_offset = off.low;
8820 if (ranges_overlap_p (src_offset, maxsize,
8821 dest_offset, maxsize))
8822 return NULL_TREE;
8823 }
8824 else
8825 return NULL_TREE;
8826
8827 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8828 if (!fn)
8829 return NULL_TREE;
8830 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8831 }
8832
8833 /* If the destination and source do not alias optimize into
8834 memcpy as well. */
8835 if ((is_gimple_min_invariant (dest)
8836 || TREE_CODE (dest) == SSA_NAME)
8837 && (is_gimple_min_invariant (src)
8838 || TREE_CODE (src) == SSA_NAME))
8839 {
8840 ao_ref destr, srcr;
8841 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8842 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8843 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8844 {
8845 tree fn;
8846 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8847 if (!fn)
8848 return NULL_TREE;
8849 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8850 }
8851 }
8852
8853 return NULL_TREE;
8854 }
8855
8856 if (!tree_fits_shwi_p (len))
8857 return NULL_TREE;
8858 /* FIXME:
8859 This logic lose for arguments like (type *)malloc (sizeof (type)),
8860 since we strip the casts of up to VOID return value from malloc.
8861 Perhaps we ought to inherit type from non-VOID argument here? */
8862 STRIP_NOPS (src);
8863 STRIP_NOPS (dest);
8864 if (!POINTER_TYPE_P (TREE_TYPE (src))
8865 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8866 return NULL_TREE;
8867 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8868 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8869 {
8870 tree tem = TREE_OPERAND (src, 0);
8871 STRIP_NOPS (tem);
8872 if (tem != TREE_OPERAND (src, 0))
8873 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8874 }
8875 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8876 {
8877 tree tem = TREE_OPERAND (dest, 0);
8878 STRIP_NOPS (tem);
8879 if (tem != TREE_OPERAND (dest, 0))
8880 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8881 }
8882 srctype = TREE_TYPE (TREE_TYPE (src));
8883 if (TREE_CODE (srctype) == ARRAY_TYPE
8884 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8885 {
8886 srctype = TREE_TYPE (srctype);
8887 STRIP_NOPS (src);
8888 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8889 }
8890 desttype = TREE_TYPE (TREE_TYPE (dest));
8891 if (TREE_CODE (desttype) == ARRAY_TYPE
8892 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8893 {
8894 desttype = TREE_TYPE (desttype);
8895 STRIP_NOPS (dest);
8896 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8897 }
8898 if (TREE_ADDRESSABLE (srctype)
8899 || TREE_ADDRESSABLE (desttype))
8900 return NULL_TREE;
8901
8902 src_align = get_pointer_alignment (src);
8903 dest_align = get_pointer_alignment (dest);
8904 if (dest_align < TYPE_ALIGN (desttype)
8905 || src_align < TYPE_ALIGN (srctype))
8906 return NULL_TREE;
8907
8908 if (!ignore)
8909 dest = builtin_save_expr (dest);
8910
8911 /* Build accesses at offset zero with a ref-all character type. */
8912 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8913 ptr_mode, true), 0);
8914
8915 /* For -fsanitize={bool,enum} make sure the load isn't performed in
8916 the bool or enum type. */
8917 if (((flag_sanitize & SANITIZE_BOOL)
8918 && TREE_CODE (desttype) == BOOLEAN_TYPE)
8919 || ((flag_sanitize & SANITIZE_ENUM)
8920 && TREE_CODE (desttype) == ENUMERAL_TYPE))
8921 {
8922 tree destitype
8923 = lang_hooks.types.type_for_mode (TYPE_MODE (desttype),
8924 TYPE_UNSIGNED (desttype));
8925 desttype = build_aligned_type (destitype, TYPE_ALIGN (desttype));
8926 }
8927 if (((flag_sanitize & SANITIZE_BOOL)
8928 && TREE_CODE (srctype) == BOOLEAN_TYPE)
8929 || ((flag_sanitize & SANITIZE_ENUM)
8930 && TREE_CODE (srctype) == ENUMERAL_TYPE))
8931 {
8932 tree srcitype
8933 = lang_hooks.types.type_for_mode (TYPE_MODE (srctype),
8934 TYPE_UNSIGNED (srctype));
8935 srctype = build_aligned_type (srcitype, TYPE_ALIGN (srctype));
8936 }
8937
8938 destvar = dest;
8939 STRIP_NOPS (destvar);
8940 if (TREE_CODE (destvar) == ADDR_EXPR
8941 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8942 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8943 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8944 else
8945 destvar = NULL_TREE;
8946
8947 srcvar = src;
8948 STRIP_NOPS (srcvar);
8949 if (TREE_CODE (srcvar) == ADDR_EXPR
8950 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8951 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8952 {
8953 if (!destvar
8954 || src_align >= TYPE_ALIGN (desttype))
8955 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8956 srcvar, off0);
8957 else if (!STRICT_ALIGNMENT)
8958 {
8959 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8960 src_align);
8961 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8962 }
8963 else
8964 srcvar = NULL_TREE;
8965 }
8966 else
8967 srcvar = NULL_TREE;
8968
8969 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8970 return NULL_TREE;
8971
8972 if (srcvar == NULL_TREE)
8973 {
8974 STRIP_NOPS (src);
8975 if (src_align >= TYPE_ALIGN (desttype))
8976 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8977 else
8978 {
8979 if (STRICT_ALIGNMENT)
8980 return NULL_TREE;
8981 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8982 src_align);
8983 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8984 }
8985 }
8986 else if (destvar == NULL_TREE)
8987 {
8988 STRIP_NOPS (dest);
8989 if (dest_align >= TYPE_ALIGN (srctype))
8990 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8991 else
8992 {
8993 if (STRICT_ALIGNMENT)
8994 return NULL_TREE;
8995 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8996 dest_align);
8997 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8998 }
8999 }
9000
9001 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9002 }
9003
9004 if (ignore)
9005 return expr;
9006
9007 if (endp == 0 || endp == 3)
9008 return omit_one_operand_loc (loc, type, dest, expr);
9009
9010 if (expr == len)
9011 expr = NULL_TREE;
9012
9013 if (endp == 2)
9014 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9015 ssize_int (1));
9016
9017 dest = fold_build_pointer_plus_loc (loc, dest, len);
9018 dest = fold_convert_loc (loc, type, dest);
9019 if (expr)
9020 dest = omit_one_operand_loc (loc, type, dest, expr);
9021 return dest;
9022 }
9023
9024 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9025 If LEN is not NULL, it represents the length of the string to be
9026 copied. Return NULL_TREE if no simplification can be made. */
9027
9028 tree
9029 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9030 {
9031 tree fn;
9032
9033 if (!validate_arg (dest, POINTER_TYPE)
9034 || !validate_arg (src, POINTER_TYPE))
9035 return NULL_TREE;
9036
9037 /* If SRC and DEST are the same (and not volatile), return DEST. */
9038 if (operand_equal_p (src, dest, 0))
9039 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9040
9041 if (optimize_function_for_size_p (cfun))
9042 return NULL_TREE;
9043
9044 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9045 if (!fn)
9046 return NULL_TREE;
9047
9048 if (!len)
9049 {
9050 len = c_strlen (src, 1);
9051 if (! len || TREE_SIDE_EFFECTS (len))
9052 return NULL_TREE;
9053 }
9054
9055 len = fold_convert_loc (loc, size_type_node, len);
9056 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9057 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9058 build_call_expr_loc (loc, fn, 3, dest, src, len));
9059 }
9060
9061 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9062 Return NULL_TREE if no simplification can be made. */
9063
9064 static tree
9065 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9066 {
9067 tree fn, len, lenp1, call, type;
9068
9069 if (!validate_arg (dest, POINTER_TYPE)
9070 || !validate_arg (src, POINTER_TYPE))
9071 return NULL_TREE;
9072
9073 len = c_strlen (src, 1);
9074 if (!len
9075 || TREE_CODE (len) != INTEGER_CST)
9076 return NULL_TREE;
9077
9078 if (optimize_function_for_size_p (cfun)
9079 /* If length is zero it's small enough. */
9080 && !integer_zerop (len))
9081 return NULL_TREE;
9082
9083 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9084 if (!fn)
9085 return NULL_TREE;
9086
9087 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9088 fold_convert_loc (loc, size_type_node, len),
9089 build_int_cst (size_type_node, 1));
9090 /* We use dest twice in building our expression. Save it from
9091 multiple expansions. */
9092 dest = builtin_save_expr (dest);
9093 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9094
9095 type = TREE_TYPE (TREE_TYPE (fndecl));
9096 dest = fold_build_pointer_plus_loc (loc, dest, len);
9097 dest = fold_convert_loc (loc, type, dest);
9098 dest = omit_one_operand_loc (loc, type, dest, call);
9099 return dest;
9100 }
9101
9102 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9103 If SLEN is not NULL, it represents the length of the source string.
9104 Return NULL_TREE if no simplification can be made. */
9105
9106 tree
9107 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9108 tree src, tree len, tree slen)
9109 {
9110 tree fn;
9111
9112 if (!validate_arg (dest, POINTER_TYPE)
9113 || !validate_arg (src, POINTER_TYPE)
9114 || !validate_arg (len, INTEGER_TYPE))
9115 return NULL_TREE;
9116
9117 /* If the LEN parameter is zero, return DEST. */
9118 if (integer_zerop (len))
9119 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9120
9121 /* We can't compare slen with len as constants below if len is not a
9122 constant. */
9123 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9124 return NULL_TREE;
9125
9126 if (!slen)
9127 slen = c_strlen (src, 1);
9128
9129 /* Now, we must be passed a constant src ptr parameter. */
9130 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9131 return NULL_TREE;
9132
9133 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9134
9135 /* We do not support simplification of this case, though we do
9136 support it when expanding trees into RTL. */
9137 /* FIXME: generate a call to __builtin_memset. */
9138 if (tree_int_cst_lt (slen, len))
9139 return NULL_TREE;
9140
9141 /* OK transform into builtin memcpy. */
9142 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9143 if (!fn)
9144 return NULL_TREE;
9145
9146 len = fold_convert_loc (loc, size_type_node, len);
9147 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9148 build_call_expr_loc (loc, fn, 3, dest, src, len));
9149 }
9150
9151 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9152 arguments to the call, and TYPE is its return type.
9153 Return NULL_TREE if no simplification can be made. */
9154
9155 static tree
9156 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9157 {
9158 if (!validate_arg (arg1, POINTER_TYPE)
9159 || !validate_arg (arg2, INTEGER_TYPE)
9160 || !validate_arg (len, INTEGER_TYPE))
9161 return NULL_TREE;
9162 else
9163 {
9164 const char *p1;
9165
9166 if (TREE_CODE (arg2) != INTEGER_CST
9167 || !tree_fits_uhwi_p (len))
9168 return NULL_TREE;
9169
9170 p1 = c_getstr (arg1);
9171 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9172 {
9173 char c;
9174 const char *r;
9175 tree tem;
9176
9177 if (target_char_cast (arg2, &c))
9178 return NULL_TREE;
9179
9180 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9181
9182 if (r == NULL)
9183 return build_int_cst (TREE_TYPE (arg1), 0);
9184
9185 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9186 return fold_convert_loc (loc, type, tem);
9187 }
9188 return NULL_TREE;
9189 }
9190 }
9191
9192 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9193 Return NULL_TREE if no simplification can be made. */
9194
9195 static tree
9196 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9197 {
9198 const char *p1, *p2;
9199
9200 if (!validate_arg (arg1, POINTER_TYPE)
9201 || !validate_arg (arg2, POINTER_TYPE)
9202 || !validate_arg (len, INTEGER_TYPE))
9203 return NULL_TREE;
9204
9205 /* If the LEN parameter is zero, return zero. */
9206 if (integer_zerop (len))
9207 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9208 arg1, arg2);
9209
9210 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9211 if (operand_equal_p (arg1, arg2, 0))
9212 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9213
9214 p1 = c_getstr (arg1);
9215 p2 = c_getstr (arg2);
9216
9217 /* If all arguments are constant, and the value of len is not greater
9218 than the lengths of arg1 and arg2, evaluate at compile-time. */
9219 if (tree_fits_uhwi_p (len) && p1 && p2
9220 && compare_tree_int (len, strlen (p1) + 1) <= 0
9221 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9222 {
9223 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9224
9225 if (r > 0)
9226 return integer_one_node;
9227 else if (r < 0)
9228 return integer_minus_one_node;
9229 else
9230 return integer_zero_node;
9231 }
9232
9233 /* If len parameter is one, return an expression corresponding to
9234 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9235 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9236 {
9237 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9238 tree cst_uchar_ptr_node
9239 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9240
9241 tree ind1
9242 = fold_convert_loc (loc, integer_type_node,
9243 build1 (INDIRECT_REF, cst_uchar_node,
9244 fold_convert_loc (loc,
9245 cst_uchar_ptr_node,
9246 arg1)));
9247 tree ind2
9248 = fold_convert_loc (loc, integer_type_node,
9249 build1 (INDIRECT_REF, cst_uchar_node,
9250 fold_convert_loc (loc,
9251 cst_uchar_ptr_node,
9252 arg2)));
9253 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9254 }
9255
9256 return NULL_TREE;
9257 }
9258
9259 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9260 Return NULL_TREE if no simplification can be made. */
9261
9262 static tree
9263 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9264 {
9265 const char *p1, *p2;
9266
9267 if (!validate_arg (arg1, POINTER_TYPE)
9268 || !validate_arg (arg2, POINTER_TYPE))
9269 return NULL_TREE;
9270
9271 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9272 if (operand_equal_p (arg1, arg2, 0))
9273 return integer_zero_node;
9274
9275 p1 = c_getstr (arg1);
9276 p2 = c_getstr (arg2);
9277
9278 if (p1 && p2)
9279 {
9280 const int i = strcmp (p1, p2);
9281 if (i < 0)
9282 return integer_minus_one_node;
9283 else if (i > 0)
9284 return integer_one_node;
9285 else
9286 return integer_zero_node;
9287 }
9288
9289 /* If the second arg is "", return *(const unsigned char*)arg1. */
9290 if (p2 && *p2 == '\0')
9291 {
9292 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9293 tree cst_uchar_ptr_node
9294 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9295
9296 return fold_convert_loc (loc, integer_type_node,
9297 build1 (INDIRECT_REF, cst_uchar_node,
9298 fold_convert_loc (loc,
9299 cst_uchar_ptr_node,
9300 arg1)));
9301 }
9302
9303 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9304 if (p1 && *p1 == '\0')
9305 {
9306 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9307 tree cst_uchar_ptr_node
9308 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9309
9310 tree temp
9311 = fold_convert_loc (loc, integer_type_node,
9312 build1 (INDIRECT_REF, cst_uchar_node,
9313 fold_convert_loc (loc,
9314 cst_uchar_ptr_node,
9315 arg2)));
9316 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9317 }
9318
9319 return NULL_TREE;
9320 }
9321
9322 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9323 Return NULL_TREE if no simplification can be made. */
9324
9325 static tree
9326 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9327 {
9328 const char *p1, *p2;
9329
9330 if (!validate_arg (arg1, POINTER_TYPE)
9331 || !validate_arg (arg2, POINTER_TYPE)
9332 || !validate_arg (len, INTEGER_TYPE))
9333 return NULL_TREE;
9334
9335 /* If the LEN parameter is zero, return zero. */
9336 if (integer_zerop (len))
9337 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9338 arg1, arg2);
9339
9340 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9341 if (operand_equal_p (arg1, arg2, 0))
9342 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9343
9344 p1 = c_getstr (arg1);
9345 p2 = c_getstr (arg2);
9346
9347 if (tree_fits_uhwi_p (len) && p1 && p2)
9348 {
9349 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9350 if (i > 0)
9351 return integer_one_node;
9352 else if (i < 0)
9353 return integer_minus_one_node;
9354 else
9355 return integer_zero_node;
9356 }
9357
9358 /* If the second arg is "", and the length is greater than zero,
9359 return *(const unsigned char*)arg1. */
9360 if (p2 && *p2 == '\0'
9361 && TREE_CODE (len) == INTEGER_CST
9362 && tree_int_cst_sgn (len) == 1)
9363 {
9364 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9365 tree cst_uchar_ptr_node
9366 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9367
9368 return fold_convert_loc (loc, integer_type_node,
9369 build1 (INDIRECT_REF, cst_uchar_node,
9370 fold_convert_loc (loc,
9371 cst_uchar_ptr_node,
9372 arg1)));
9373 }
9374
9375 /* If the first arg is "", and the length is greater than zero,
9376 return -*(const unsigned char*)arg2. */
9377 if (p1 && *p1 == '\0'
9378 && TREE_CODE (len) == INTEGER_CST
9379 && tree_int_cst_sgn (len) == 1)
9380 {
9381 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9382 tree cst_uchar_ptr_node
9383 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9384
9385 tree temp = fold_convert_loc (loc, integer_type_node,
9386 build1 (INDIRECT_REF, cst_uchar_node,
9387 fold_convert_loc (loc,
9388 cst_uchar_ptr_node,
9389 arg2)));
9390 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9391 }
9392
9393 /* If len parameter is one, return an expression corresponding to
9394 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9395 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9396 {
9397 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9398 tree cst_uchar_ptr_node
9399 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9400
9401 tree ind1 = fold_convert_loc (loc, integer_type_node,
9402 build1 (INDIRECT_REF, cst_uchar_node,
9403 fold_convert_loc (loc,
9404 cst_uchar_ptr_node,
9405 arg1)));
9406 tree ind2 = fold_convert_loc (loc, integer_type_node,
9407 build1 (INDIRECT_REF, cst_uchar_node,
9408 fold_convert_loc (loc,
9409 cst_uchar_ptr_node,
9410 arg2)));
9411 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9412 }
9413
9414 return NULL_TREE;
9415 }
9416
9417 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9418 ARG. Return NULL_TREE if no simplification can be made. */
9419
9420 static tree
9421 fold_builtin_signbit (location_t loc, tree arg, tree type)
9422 {
9423 if (!validate_arg (arg, REAL_TYPE))
9424 return NULL_TREE;
9425
9426 /* If ARG is a compile-time constant, determine the result. */
9427 if (TREE_CODE (arg) == REAL_CST
9428 && !TREE_OVERFLOW (arg))
9429 {
9430 REAL_VALUE_TYPE c;
9431
9432 c = TREE_REAL_CST (arg);
9433 return (REAL_VALUE_NEGATIVE (c)
9434 ? build_one_cst (type)
9435 : build_zero_cst (type));
9436 }
9437
9438 /* If ARG is non-negative, the result is always zero. */
9439 if (tree_expr_nonnegative_p (arg))
9440 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9441
9442 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9443 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9444 return fold_convert (type,
9445 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9446 build_real (TREE_TYPE (arg), dconst0)));
9447
9448 return NULL_TREE;
9449 }
9450
9451 /* Fold function call to builtin copysign, copysignf or copysignl with
9452 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9453 be made. */
9454
9455 static tree
9456 fold_builtin_copysign (location_t loc, tree fndecl,
9457 tree arg1, tree arg2, tree type)
9458 {
9459 tree tem;
9460
9461 if (!validate_arg (arg1, REAL_TYPE)
9462 || !validate_arg (arg2, REAL_TYPE))
9463 return NULL_TREE;
9464
9465 /* copysign(X,X) is X. */
9466 if (operand_equal_p (arg1, arg2, 0))
9467 return fold_convert_loc (loc, type, arg1);
9468
9469 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9470 if (TREE_CODE (arg1) == REAL_CST
9471 && TREE_CODE (arg2) == REAL_CST
9472 && !TREE_OVERFLOW (arg1)
9473 && !TREE_OVERFLOW (arg2))
9474 {
9475 REAL_VALUE_TYPE c1, c2;
9476
9477 c1 = TREE_REAL_CST (arg1);
9478 c2 = TREE_REAL_CST (arg2);
9479 /* c1.sign := c2.sign. */
9480 real_copysign (&c1, &c2);
9481 return build_real (type, c1);
9482 }
9483
9484 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9485 Remember to evaluate Y for side-effects. */
9486 if (tree_expr_nonnegative_p (arg2))
9487 return omit_one_operand_loc (loc, type,
9488 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9489 arg2);
9490
9491 /* Strip sign changing operations for the first argument. */
9492 tem = fold_strip_sign_ops (arg1);
9493 if (tem)
9494 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9495
9496 return NULL_TREE;
9497 }
9498
9499 /* Fold a call to builtin isascii with argument ARG. */
9500
9501 static tree
9502 fold_builtin_isascii (location_t loc, tree arg)
9503 {
9504 if (!validate_arg (arg, INTEGER_TYPE))
9505 return NULL_TREE;
9506 else
9507 {
9508 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9509 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9510 build_int_cst (integer_type_node,
9511 ~ (unsigned HOST_WIDE_INT) 0x7f));
9512 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9513 arg, integer_zero_node);
9514 }
9515 }
9516
9517 /* Fold a call to builtin toascii with argument ARG. */
9518
9519 static tree
9520 fold_builtin_toascii (location_t loc, tree arg)
9521 {
9522 if (!validate_arg (arg, INTEGER_TYPE))
9523 return NULL_TREE;
9524
9525 /* Transform toascii(c) -> (c & 0x7f). */
9526 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9527 build_int_cst (integer_type_node, 0x7f));
9528 }
9529
9530 /* Fold a call to builtin isdigit with argument ARG. */
9531
9532 static tree
9533 fold_builtin_isdigit (location_t loc, tree arg)
9534 {
9535 if (!validate_arg (arg, INTEGER_TYPE))
9536 return NULL_TREE;
9537 else
9538 {
9539 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9540 /* According to the C standard, isdigit is unaffected by locale.
9541 However, it definitely is affected by the target character set. */
9542 unsigned HOST_WIDE_INT target_digit0
9543 = lang_hooks.to_target_charset ('0');
9544
9545 if (target_digit0 == 0)
9546 return NULL_TREE;
9547
9548 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9549 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9550 build_int_cst (unsigned_type_node, target_digit0));
9551 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9552 build_int_cst (unsigned_type_node, 9));
9553 }
9554 }
9555
9556 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9557
9558 static tree
9559 fold_builtin_fabs (location_t loc, tree arg, tree type)
9560 {
9561 if (!validate_arg (arg, REAL_TYPE))
9562 return NULL_TREE;
9563
9564 arg = fold_convert_loc (loc, type, arg);
9565 if (TREE_CODE (arg) == REAL_CST)
9566 return fold_abs_const (arg, type);
9567 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9568 }
9569
9570 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9571
9572 static tree
9573 fold_builtin_abs (location_t loc, tree arg, tree type)
9574 {
9575 if (!validate_arg (arg, INTEGER_TYPE))
9576 return NULL_TREE;
9577
9578 arg = fold_convert_loc (loc, type, arg);
9579 if (TREE_CODE (arg) == INTEGER_CST)
9580 return fold_abs_const (arg, type);
9581 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9582 }
9583
9584 /* Fold a fma operation with arguments ARG[012]. */
9585
9586 tree
9587 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9588 tree type, tree arg0, tree arg1, tree arg2)
9589 {
9590 if (TREE_CODE (arg0) == REAL_CST
9591 && TREE_CODE (arg1) == REAL_CST
9592 && TREE_CODE (arg2) == REAL_CST)
9593 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9594
9595 return NULL_TREE;
9596 }
9597
9598 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9599
9600 static tree
9601 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9602 {
9603 if (validate_arg (arg0, REAL_TYPE)
9604 && validate_arg (arg1, REAL_TYPE)
9605 && validate_arg (arg2, REAL_TYPE))
9606 {
9607 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9608 if (tem)
9609 return tem;
9610
9611 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9612 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9613 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9614 }
9615 return NULL_TREE;
9616 }
9617
9618 /* Fold a call to builtin fmin or fmax. */
9619
9620 static tree
9621 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9622 tree type, bool max)
9623 {
9624 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9625 {
9626 /* Calculate the result when the argument is a constant. */
9627 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9628
9629 if (res)
9630 return res;
9631
9632 /* If either argument is NaN, return the other one. Avoid the
9633 transformation if we get (and honor) a signalling NaN. Using
9634 omit_one_operand() ensures we create a non-lvalue. */
9635 if (TREE_CODE (arg0) == REAL_CST
9636 && real_isnan (&TREE_REAL_CST (arg0))
9637 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9638 || ! TREE_REAL_CST (arg0).signalling))
9639 return omit_one_operand_loc (loc, type, arg1, arg0);
9640 if (TREE_CODE (arg1) == REAL_CST
9641 && real_isnan (&TREE_REAL_CST (arg1))
9642 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9643 || ! TREE_REAL_CST (arg1).signalling))
9644 return omit_one_operand_loc (loc, type, arg0, arg1);
9645
9646 /* Transform fmin/fmax(x,x) -> x. */
9647 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9648 return omit_one_operand_loc (loc, type, arg0, arg1);
9649
9650 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9651 functions to return the numeric arg if the other one is NaN.
9652 These tree codes don't honor that, so only transform if
9653 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9654 handled, so we don't have to worry about it either. */
9655 if (flag_finite_math_only)
9656 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9657 fold_convert_loc (loc, type, arg0),
9658 fold_convert_loc (loc, type, arg1));
9659 }
9660 return NULL_TREE;
9661 }
9662
9663 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9664
9665 static tree
9666 fold_builtin_carg (location_t loc, tree arg, tree type)
9667 {
9668 if (validate_arg (arg, COMPLEX_TYPE)
9669 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9670 {
9671 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9672
9673 if (atan2_fn)
9674 {
9675 tree new_arg = builtin_save_expr (arg);
9676 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9677 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9678 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9679 }
9680 }
9681
9682 return NULL_TREE;
9683 }
9684
9685 /* Fold a call to builtin logb/ilogb. */
9686
9687 static tree
9688 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9689 {
9690 if (! validate_arg (arg, REAL_TYPE))
9691 return NULL_TREE;
9692
9693 STRIP_NOPS (arg);
9694
9695 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9696 {
9697 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9698
9699 switch (value->cl)
9700 {
9701 case rvc_nan:
9702 case rvc_inf:
9703 /* If arg is Inf or NaN and we're logb, return it. */
9704 if (TREE_CODE (rettype) == REAL_TYPE)
9705 {
9706 /* For logb(-Inf) we have to return +Inf. */
9707 if (real_isinf (value) && real_isneg (value))
9708 {
9709 REAL_VALUE_TYPE tem;
9710 real_inf (&tem);
9711 return build_real (rettype, tem);
9712 }
9713 return fold_convert_loc (loc, rettype, arg);
9714 }
9715 /* Fall through... */
9716 case rvc_zero:
9717 /* Zero may set errno and/or raise an exception for logb, also
9718 for ilogb we don't know FP_ILOGB0. */
9719 return NULL_TREE;
9720 case rvc_normal:
9721 /* For normal numbers, proceed iff radix == 2. In GCC,
9722 normalized significands are in the range [0.5, 1.0). We
9723 want the exponent as if they were [1.0, 2.0) so get the
9724 exponent and subtract 1. */
9725 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9726 return fold_convert_loc (loc, rettype,
9727 build_int_cst (integer_type_node,
9728 REAL_EXP (value)-1));
9729 break;
9730 }
9731 }
9732
9733 return NULL_TREE;
9734 }
9735
9736 /* Fold a call to builtin significand, if radix == 2. */
9737
9738 static tree
9739 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9740 {
9741 if (! validate_arg (arg, REAL_TYPE))
9742 return NULL_TREE;
9743
9744 STRIP_NOPS (arg);
9745
9746 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9747 {
9748 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9749
9750 switch (value->cl)
9751 {
9752 case rvc_zero:
9753 case rvc_nan:
9754 case rvc_inf:
9755 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9756 return fold_convert_loc (loc, rettype, arg);
9757 case rvc_normal:
9758 /* For normal numbers, proceed iff radix == 2. */
9759 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9760 {
9761 REAL_VALUE_TYPE result = *value;
9762 /* In GCC, normalized significands are in the range [0.5,
9763 1.0). We want them to be [1.0, 2.0) so set the
9764 exponent to 1. */
9765 SET_REAL_EXP (&result, 1);
9766 return build_real (rettype, result);
9767 }
9768 break;
9769 }
9770 }
9771
9772 return NULL_TREE;
9773 }
9774
9775 /* Fold a call to builtin frexp, we can assume the base is 2. */
9776
9777 static tree
9778 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9779 {
9780 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9781 return NULL_TREE;
9782
9783 STRIP_NOPS (arg0);
9784
9785 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9786 return NULL_TREE;
9787
9788 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9789
9790 /* Proceed if a valid pointer type was passed in. */
9791 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9792 {
9793 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9794 tree frac, exp;
9795
9796 switch (value->cl)
9797 {
9798 case rvc_zero:
9799 /* For +-0, return (*exp = 0, +-0). */
9800 exp = integer_zero_node;
9801 frac = arg0;
9802 break;
9803 case rvc_nan:
9804 case rvc_inf:
9805 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9806 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9807 case rvc_normal:
9808 {
9809 /* Since the frexp function always expects base 2, and in
9810 GCC normalized significands are already in the range
9811 [0.5, 1.0), we have exactly what frexp wants. */
9812 REAL_VALUE_TYPE frac_rvt = *value;
9813 SET_REAL_EXP (&frac_rvt, 0);
9814 frac = build_real (rettype, frac_rvt);
9815 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9816 }
9817 break;
9818 default:
9819 gcc_unreachable ();
9820 }
9821
9822 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9823 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9824 TREE_SIDE_EFFECTS (arg1) = 1;
9825 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9826 }
9827
9828 return NULL_TREE;
9829 }
9830
9831 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9832 then we can assume the base is two. If it's false, then we have to
9833 check the mode of the TYPE parameter in certain cases. */
9834
9835 static tree
9836 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9837 tree type, bool ldexp)
9838 {
9839 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9840 {
9841 STRIP_NOPS (arg0);
9842 STRIP_NOPS (arg1);
9843
9844 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9845 if (real_zerop (arg0) || integer_zerop (arg1)
9846 || (TREE_CODE (arg0) == REAL_CST
9847 && !real_isfinite (&TREE_REAL_CST (arg0))))
9848 return omit_one_operand_loc (loc, type, arg0, arg1);
9849
9850 /* If both arguments are constant, then try to evaluate it. */
9851 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9852 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9853 && tree_fits_shwi_p (arg1))
9854 {
9855 /* Bound the maximum adjustment to twice the range of the
9856 mode's valid exponents. Use abs to ensure the range is
9857 positive as a sanity check. */
9858 const long max_exp_adj = 2 *
9859 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9860 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9861
9862 /* Get the user-requested adjustment. */
9863 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9864
9865 /* The requested adjustment must be inside this range. This
9866 is a preliminary cap to avoid things like overflow, we
9867 may still fail to compute the result for other reasons. */
9868 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9869 {
9870 REAL_VALUE_TYPE initial_result;
9871
9872 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9873
9874 /* Ensure we didn't overflow. */
9875 if (! real_isinf (&initial_result))
9876 {
9877 const REAL_VALUE_TYPE trunc_result
9878 = real_value_truncate (TYPE_MODE (type), initial_result);
9879
9880 /* Only proceed if the target mode can hold the
9881 resulting value. */
9882 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9883 return build_real (type, trunc_result);
9884 }
9885 }
9886 }
9887 }
9888
9889 return NULL_TREE;
9890 }
9891
9892 /* Fold a call to builtin modf. */
9893
9894 static tree
9895 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9896 {
9897 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9898 return NULL_TREE;
9899
9900 STRIP_NOPS (arg0);
9901
9902 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9903 return NULL_TREE;
9904
9905 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9906
9907 /* Proceed if a valid pointer type was passed in. */
9908 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9909 {
9910 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9911 REAL_VALUE_TYPE trunc, frac;
9912
9913 switch (value->cl)
9914 {
9915 case rvc_nan:
9916 case rvc_zero:
9917 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9918 trunc = frac = *value;
9919 break;
9920 case rvc_inf:
9921 /* For +-Inf, return (*arg1 = arg0, +-0). */
9922 frac = dconst0;
9923 frac.sign = value->sign;
9924 trunc = *value;
9925 break;
9926 case rvc_normal:
9927 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9928 real_trunc (&trunc, VOIDmode, value);
9929 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9930 /* If the original number was negative and already
9931 integral, then the fractional part is -0.0. */
9932 if (value->sign && frac.cl == rvc_zero)
9933 frac.sign = value->sign;
9934 break;
9935 }
9936
9937 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9938 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9939 build_real (rettype, trunc));
9940 TREE_SIDE_EFFECTS (arg1) = 1;
9941 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9942 build_real (rettype, frac));
9943 }
9944
9945 return NULL_TREE;
9946 }
9947
9948 /* Given a location LOC, an interclass builtin function decl FNDECL
9949 and its single argument ARG, return an folded expression computing
9950 the same, or NULL_TREE if we either couldn't or didn't want to fold
9951 (the latter happen if there's an RTL instruction available). */
9952
9953 static tree
9954 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9955 {
9956 enum machine_mode mode;
9957
9958 if (!validate_arg (arg, REAL_TYPE))
9959 return NULL_TREE;
9960
9961 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9962 return NULL_TREE;
9963
9964 mode = TYPE_MODE (TREE_TYPE (arg));
9965
9966 /* If there is no optab, try generic code. */
9967 switch (DECL_FUNCTION_CODE (fndecl))
9968 {
9969 tree result;
9970
9971 CASE_FLT_FN (BUILT_IN_ISINF):
9972 {
9973 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9974 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9975 tree const type = TREE_TYPE (arg);
9976 REAL_VALUE_TYPE r;
9977 char buf[128];
9978
9979 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9980 real_from_string (&r, buf);
9981 result = build_call_expr (isgr_fn, 2,
9982 fold_build1_loc (loc, ABS_EXPR, type, arg),
9983 build_real (type, r));
9984 return result;
9985 }
9986 CASE_FLT_FN (BUILT_IN_FINITE):
9987 case BUILT_IN_ISFINITE:
9988 {
9989 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9990 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9991 tree const type = TREE_TYPE (arg);
9992 REAL_VALUE_TYPE r;
9993 char buf[128];
9994
9995 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9996 real_from_string (&r, buf);
9997 result = build_call_expr (isle_fn, 2,
9998 fold_build1_loc (loc, ABS_EXPR, type, arg),
9999 build_real (type, r));
10000 /*result = fold_build2_loc (loc, UNGT_EXPR,
10001 TREE_TYPE (TREE_TYPE (fndecl)),
10002 fold_build1_loc (loc, ABS_EXPR, type, arg),
10003 build_real (type, r));
10004 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10005 TREE_TYPE (TREE_TYPE (fndecl)),
10006 result);*/
10007 return result;
10008 }
10009 case BUILT_IN_ISNORMAL:
10010 {
10011 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10012 islessequal(fabs(x),DBL_MAX). */
10013 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10014 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10015 tree const type = TREE_TYPE (arg);
10016 REAL_VALUE_TYPE rmax, rmin;
10017 char buf[128];
10018
10019 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10020 real_from_string (&rmax, buf);
10021 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10022 real_from_string (&rmin, buf);
10023 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10024 result = build_call_expr (isle_fn, 2, arg,
10025 build_real (type, rmax));
10026 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10027 build_call_expr (isge_fn, 2, arg,
10028 build_real (type, rmin)));
10029 return result;
10030 }
10031 default:
10032 break;
10033 }
10034
10035 return NULL_TREE;
10036 }
10037
10038 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10039 ARG is the argument for the call. */
10040
10041 static tree
10042 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10043 {
10044 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10045 REAL_VALUE_TYPE r;
10046
10047 if (!validate_arg (arg, REAL_TYPE))
10048 return NULL_TREE;
10049
10050 switch (builtin_index)
10051 {
10052 case BUILT_IN_ISINF:
10053 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10054 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10055
10056 if (TREE_CODE (arg) == REAL_CST)
10057 {
10058 r = TREE_REAL_CST (arg);
10059 if (real_isinf (&r))
10060 return real_compare (GT_EXPR, &r, &dconst0)
10061 ? integer_one_node : integer_minus_one_node;
10062 else
10063 return integer_zero_node;
10064 }
10065
10066 return NULL_TREE;
10067
10068 case BUILT_IN_ISINF_SIGN:
10069 {
10070 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10071 /* In a boolean context, GCC will fold the inner COND_EXPR to
10072 1. So e.g. "if (isinf_sign(x))" would be folded to just
10073 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10074 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10075 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10076 tree tmp = NULL_TREE;
10077
10078 arg = builtin_save_expr (arg);
10079
10080 if (signbit_fn && isinf_fn)
10081 {
10082 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10083 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10084
10085 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10086 signbit_call, integer_zero_node);
10087 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10088 isinf_call, integer_zero_node);
10089
10090 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10091 integer_minus_one_node, integer_one_node);
10092 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10093 isinf_call, tmp,
10094 integer_zero_node);
10095 }
10096
10097 return tmp;
10098 }
10099
10100 case BUILT_IN_ISFINITE:
10101 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10102 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10103 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10104
10105 if (TREE_CODE (arg) == REAL_CST)
10106 {
10107 r = TREE_REAL_CST (arg);
10108 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10109 }
10110
10111 return NULL_TREE;
10112
10113 case BUILT_IN_ISNAN:
10114 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10115 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10116
10117 if (TREE_CODE (arg) == REAL_CST)
10118 {
10119 r = TREE_REAL_CST (arg);
10120 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10121 }
10122
10123 arg = builtin_save_expr (arg);
10124 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10125
10126 default:
10127 gcc_unreachable ();
10128 }
10129 }
10130
10131 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10132 This builtin will generate code to return the appropriate floating
10133 point classification depending on the value of the floating point
10134 number passed in. The possible return values must be supplied as
10135 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10136 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10137 one floating point argument which is "type generic". */
10138
10139 static tree
10140 fold_builtin_fpclassify (location_t loc, tree exp)
10141 {
10142 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10143 arg, type, res, tmp;
10144 enum machine_mode mode;
10145 REAL_VALUE_TYPE r;
10146 char buf[128];
10147
10148 /* Verify the required arguments in the original call. */
10149 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10150 INTEGER_TYPE, INTEGER_TYPE,
10151 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10152 return NULL_TREE;
10153
10154 fp_nan = CALL_EXPR_ARG (exp, 0);
10155 fp_infinite = CALL_EXPR_ARG (exp, 1);
10156 fp_normal = CALL_EXPR_ARG (exp, 2);
10157 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10158 fp_zero = CALL_EXPR_ARG (exp, 4);
10159 arg = CALL_EXPR_ARG (exp, 5);
10160 type = TREE_TYPE (arg);
10161 mode = TYPE_MODE (type);
10162 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10163
10164 /* fpclassify(x) ->
10165 isnan(x) ? FP_NAN :
10166 (fabs(x) == Inf ? FP_INFINITE :
10167 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10168 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10169
10170 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10171 build_real (type, dconst0));
10172 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10173 tmp, fp_zero, fp_subnormal);
10174
10175 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10176 real_from_string (&r, buf);
10177 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10178 arg, build_real (type, r));
10179 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10180
10181 if (HONOR_INFINITIES (mode))
10182 {
10183 real_inf (&r);
10184 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10185 build_real (type, r));
10186 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10187 fp_infinite, res);
10188 }
10189
10190 if (HONOR_NANS (mode))
10191 {
10192 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10193 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10194 }
10195
10196 return res;
10197 }
10198
10199 /* Fold a call to an unordered comparison function such as
10200 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10201 being called and ARG0 and ARG1 are the arguments for the call.
10202 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10203 the opposite of the desired result. UNORDERED_CODE is used
10204 for modes that can hold NaNs and ORDERED_CODE is used for
10205 the rest. */
10206
10207 static tree
10208 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10209 enum tree_code unordered_code,
10210 enum tree_code ordered_code)
10211 {
10212 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10213 enum tree_code code;
10214 tree type0, type1;
10215 enum tree_code code0, code1;
10216 tree cmp_type = NULL_TREE;
10217
10218 type0 = TREE_TYPE (arg0);
10219 type1 = TREE_TYPE (arg1);
10220
10221 code0 = TREE_CODE (type0);
10222 code1 = TREE_CODE (type1);
10223
10224 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10225 /* Choose the wider of two real types. */
10226 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10227 ? type0 : type1;
10228 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10229 cmp_type = type0;
10230 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10231 cmp_type = type1;
10232
10233 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10234 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10235
10236 if (unordered_code == UNORDERED_EXPR)
10237 {
10238 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10239 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10240 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10241 }
10242
10243 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10244 : ordered_code;
10245 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10246 fold_build2_loc (loc, code, type, arg0, arg1));
10247 }
10248
10249 /* Fold a call to built-in function FNDECL with 0 arguments.
10250 IGNORE is true if the result of the function call is ignored. This
10251 function returns NULL_TREE if no simplification was possible. */
10252
10253 static tree
10254 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10255 {
10256 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10257 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10258 switch (fcode)
10259 {
10260 CASE_FLT_FN (BUILT_IN_INF):
10261 case BUILT_IN_INFD32:
10262 case BUILT_IN_INFD64:
10263 case BUILT_IN_INFD128:
10264 return fold_builtin_inf (loc, type, true);
10265
10266 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10267 return fold_builtin_inf (loc, type, false);
10268
10269 case BUILT_IN_CLASSIFY_TYPE:
10270 return fold_builtin_classify_type (NULL_TREE);
10271
10272 case BUILT_IN_UNREACHABLE:
10273 if (flag_sanitize & SANITIZE_UNREACHABLE
10274 && (current_function_decl == NULL
10275 || !lookup_attribute ("no_sanitize_undefined",
10276 DECL_ATTRIBUTES (current_function_decl))))
10277 return ubsan_instrument_unreachable (loc);
10278 break;
10279
10280 default:
10281 break;
10282 }
10283 return NULL_TREE;
10284 }
10285
10286 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10287 IGNORE is true if the result of the function call is ignored. This
10288 function returns NULL_TREE if no simplification was possible. */
10289
10290 static tree
10291 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10292 {
10293 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10294 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10295 switch (fcode)
10296 {
10297 case BUILT_IN_CONSTANT_P:
10298 {
10299 tree val = fold_builtin_constant_p (arg0);
10300
10301 /* Gimplification will pull the CALL_EXPR for the builtin out of
10302 an if condition. When not optimizing, we'll not CSE it back.
10303 To avoid link error types of regressions, return false now. */
10304 if (!val && !optimize)
10305 val = integer_zero_node;
10306
10307 return val;
10308 }
10309
10310 case BUILT_IN_CLASSIFY_TYPE:
10311 return fold_builtin_classify_type (arg0);
10312
10313 case BUILT_IN_STRLEN:
10314 return fold_builtin_strlen (loc, type, arg0);
10315
10316 CASE_FLT_FN (BUILT_IN_FABS):
10317 case BUILT_IN_FABSD32:
10318 case BUILT_IN_FABSD64:
10319 case BUILT_IN_FABSD128:
10320 return fold_builtin_fabs (loc, arg0, type);
10321
10322 case BUILT_IN_ABS:
10323 case BUILT_IN_LABS:
10324 case BUILT_IN_LLABS:
10325 case BUILT_IN_IMAXABS:
10326 return fold_builtin_abs (loc, arg0, type);
10327
10328 CASE_FLT_FN (BUILT_IN_CONJ):
10329 if (validate_arg (arg0, COMPLEX_TYPE)
10330 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10331 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10332 break;
10333
10334 CASE_FLT_FN (BUILT_IN_CREAL):
10335 if (validate_arg (arg0, COMPLEX_TYPE)
10336 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10337 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10338 break;
10339
10340 CASE_FLT_FN (BUILT_IN_CIMAG):
10341 if (validate_arg (arg0, COMPLEX_TYPE)
10342 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10343 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10344 break;
10345
10346 CASE_FLT_FN (BUILT_IN_CCOS):
10347 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10348
10349 CASE_FLT_FN (BUILT_IN_CCOSH):
10350 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10351
10352 CASE_FLT_FN (BUILT_IN_CPROJ):
10353 return fold_builtin_cproj (loc, arg0, type);
10354
10355 CASE_FLT_FN (BUILT_IN_CSIN):
10356 if (validate_arg (arg0, COMPLEX_TYPE)
10357 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10358 return do_mpc_arg1 (arg0, type, mpc_sin);
10359 break;
10360
10361 CASE_FLT_FN (BUILT_IN_CSINH):
10362 if (validate_arg (arg0, COMPLEX_TYPE)
10363 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10364 return do_mpc_arg1 (arg0, type, mpc_sinh);
10365 break;
10366
10367 CASE_FLT_FN (BUILT_IN_CTAN):
10368 if (validate_arg (arg0, COMPLEX_TYPE)
10369 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10370 return do_mpc_arg1 (arg0, type, mpc_tan);
10371 break;
10372
10373 CASE_FLT_FN (BUILT_IN_CTANH):
10374 if (validate_arg (arg0, COMPLEX_TYPE)
10375 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10376 return do_mpc_arg1 (arg0, type, mpc_tanh);
10377 break;
10378
10379 CASE_FLT_FN (BUILT_IN_CLOG):
10380 if (validate_arg (arg0, COMPLEX_TYPE)
10381 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10382 return do_mpc_arg1 (arg0, type, mpc_log);
10383 break;
10384
10385 CASE_FLT_FN (BUILT_IN_CSQRT):
10386 if (validate_arg (arg0, COMPLEX_TYPE)
10387 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10388 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10389 break;
10390
10391 CASE_FLT_FN (BUILT_IN_CASIN):
10392 if (validate_arg (arg0, COMPLEX_TYPE)
10393 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10394 return do_mpc_arg1 (arg0, type, mpc_asin);
10395 break;
10396
10397 CASE_FLT_FN (BUILT_IN_CACOS):
10398 if (validate_arg (arg0, COMPLEX_TYPE)
10399 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10400 return do_mpc_arg1 (arg0, type, mpc_acos);
10401 break;
10402
10403 CASE_FLT_FN (BUILT_IN_CATAN):
10404 if (validate_arg (arg0, COMPLEX_TYPE)
10405 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10406 return do_mpc_arg1 (arg0, type, mpc_atan);
10407 break;
10408
10409 CASE_FLT_FN (BUILT_IN_CASINH):
10410 if (validate_arg (arg0, COMPLEX_TYPE)
10411 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10412 return do_mpc_arg1 (arg0, type, mpc_asinh);
10413 break;
10414
10415 CASE_FLT_FN (BUILT_IN_CACOSH):
10416 if (validate_arg (arg0, COMPLEX_TYPE)
10417 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10418 return do_mpc_arg1 (arg0, type, mpc_acosh);
10419 break;
10420
10421 CASE_FLT_FN (BUILT_IN_CATANH):
10422 if (validate_arg (arg0, COMPLEX_TYPE)
10423 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10424 return do_mpc_arg1 (arg0, type, mpc_atanh);
10425 break;
10426
10427 CASE_FLT_FN (BUILT_IN_CABS):
10428 return fold_builtin_cabs (loc, arg0, type, fndecl);
10429
10430 CASE_FLT_FN (BUILT_IN_CARG):
10431 return fold_builtin_carg (loc, arg0, type);
10432
10433 CASE_FLT_FN (BUILT_IN_SQRT):
10434 return fold_builtin_sqrt (loc, arg0, type);
10435
10436 CASE_FLT_FN (BUILT_IN_CBRT):
10437 return fold_builtin_cbrt (loc, arg0, type);
10438
10439 CASE_FLT_FN (BUILT_IN_ASIN):
10440 if (validate_arg (arg0, REAL_TYPE))
10441 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10442 &dconstm1, &dconst1, true);
10443 break;
10444
10445 CASE_FLT_FN (BUILT_IN_ACOS):
10446 if (validate_arg (arg0, REAL_TYPE))
10447 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10448 &dconstm1, &dconst1, true);
10449 break;
10450
10451 CASE_FLT_FN (BUILT_IN_ATAN):
10452 if (validate_arg (arg0, REAL_TYPE))
10453 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10454 break;
10455
10456 CASE_FLT_FN (BUILT_IN_ASINH):
10457 if (validate_arg (arg0, REAL_TYPE))
10458 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10459 break;
10460
10461 CASE_FLT_FN (BUILT_IN_ACOSH):
10462 if (validate_arg (arg0, REAL_TYPE))
10463 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10464 &dconst1, NULL, true);
10465 break;
10466
10467 CASE_FLT_FN (BUILT_IN_ATANH):
10468 if (validate_arg (arg0, REAL_TYPE))
10469 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10470 &dconstm1, &dconst1, false);
10471 break;
10472
10473 CASE_FLT_FN (BUILT_IN_SIN):
10474 if (validate_arg (arg0, REAL_TYPE))
10475 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10476 break;
10477
10478 CASE_FLT_FN (BUILT_IN_COS):
10479 return fold_builtin_cos (loc, arg0, type, fndecl);
10480
10481 CASE_FLT_FN (BUILT_IN_TAN):
10482 return fold_builtin_tan (arg0, type);
10483
10484 CASE_FLT_FN (BUILT_IN_CEXP):
10485 return fold_builtin_cexp (loc, arg0, type);
10486
10487 CASE_FLT_FN (BUILT_IN_CEXPI):
10488 if (validate_arg (arg0, REAL_TYPE))
10489 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10490 break;
10491
10492 CASE_FLT_FN (BUILT_IN_SINH):
10493 if (validate_arg (arg0, REAL_TYPE))
10494 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10495 break;
10496
10497 CASE_FLT_FN (BUILT_IN_COSH):
10498 return fold_builtin_cosh (loc, arg0, type, fndecl);
10499
10500 CASE_FLT_FN (BUILT_IN_TANH):
10501 if (validate_arg (arg0, REAL_TYPE))
10502 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10503 break;
10504
10505 CASE_FLT_FN (BUILT_IN_ERF):
10506 if (validate_arg (arg0, REAL_TYPE))
10507 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10508 break;
10509
10510 CASE_FLT_FN (BUILT_IN_ERFC):
10511 if (validate_arg (arg0, REAL_TYPE))
10512 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10513 break;
10514
10515 CASE_FLT_FN (BUILT_IN_TGAMMA):
10516 if (validate_arg (arg0, REAL_TYPE))
10517 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10518 break;
10519
10520 CASE_FLT_FN (BUILT_IN_EXP):
10521 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10522
10523 CASE_FLT_FN (BUILT_IN_EXP2):
10524 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10525
10526 CASE_FLT_FN (BUILT_IN_EXP10):
10527 CASE_FLT_FN (BUILT_IN_POW10):
10528 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10529
10530 CASE_FLT_FN (BUILT_IN_EXPM1):
10531 if (validate_arg (arg0, REAL_TYPE))
10532 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10533 break;
10534
10535 CASE_FLT_FN (BUILT_IN_LOG):
10536 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10537
10538 CASE_FLT_FN (BUILT_IN_LOG2):
10539 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10540
10541 CASE_FLT_FN (BUILT_IN_LOG10):
10542 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10543
10544 CASE_FLT_FN (BUILT_IN_LOG1P):
10545 if (validate_arg (arg0, REAL_TYPE))
10546 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10547 &dconstm1, NULL, false);
10548 break;
10549
10550 CASE_FLT_FN (BUILT_IN_J0):
10551 if (validate_arg (arg0, REAL_TYPE))
10552 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10553 NULL, NULL, 0);
10554 break;
10555
10556 CASE_FLT_FN (BUILT_IN_J1):
10557 if (validate_arg (arg0, REAL_TYPE))
10558 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10559 NULL, NULL, 0);
10560 break;
10561
10562 CASE_FLT_FN (BUILT_IN_Y0):
10563 if (validate_arg (arg0, REAL_TYPE))
10564 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10565 &dconst0, NULL, false);
10566 break;
10567
10568 CASE_FLT_FN (BUILT_IN_Y1):
10569 if (validate_arg (arg0, REAL_TYPE))
10570 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10571 &dconst0, NULL, false);
10572 break;
10573
10574 CASE_FLT_FN (BUILT_IN_NAN):
10575 case BUILT_IN_NAND32:
10576 case BUILT_IN_NAND64:
10577 case BUILT_IN_NAND128:
10578 return fold_builtin_nan (arg0, type, true);
10579
10580 CASE_FLT_FN (BUILT_IN_NANS):
10581 return fold_builtin_nan (arg0, type, false);
10582
10583 CASE_FLT_FN (BUILT_IN_FLOOR):
10584 return fold_builtin_floor (loc, fndecl, arg0);
10585
10586 CASE_FLT_FN (BUILT_IN_CEIL):
10587 return fold_builtin_ceil (loc, fndecl, arg0);
10588
10589 CASE_FLT_FN (BUILT_IN_TRUNC):
10590 return fold_builtin_trunc (loc, fndecl, arg0);
10591
10592 CASE_FLT_FN (BUILT_IN_ROUND):
10593 return fold_builtin_round (loc, fndecl, arg0);
10594
10595 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10596 CASE_FLT_FN (BUILT_IN_RINT):
10597 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10598
10599 CASE_FLT_FN (BUILT_IN_ICEIL):
10600 CASE_FLT_FN (BUILT_IN_LCEIL):
10601 CASE_FLT_FN (BUILT_IN_LLCEIL):
10602 CASE_FLT_FN (BUILT_IN_LFLOOR):
10603 CASE_FLT_FN (BUILT_IN_IFLOOR):
10604 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10605 CASE_FLT_FN (BUILT_IN_IROUND):
10606 CASE_FLT_FN (BUILT_IN_LROUND):
10607 CASE_FLT_FN (BUILT_IN_LLROUND):
10608 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10609
10610 CASE_FLT_FN (BUILT_IN_IRINT):
10611 CASE_FLT_FN (BUILT_IN_LRINT):
10612 CASE_FLT_FN (BUILT_IN_LLRINT):
10613 return fold_fixed_mathfn (loc, fndecl, arg0);
10614
10615 case BUILT_IN_BSWAP16:
10616 case BUILT_IN_BSWAP32:
10617 case BUILT_IN_BSWAP64:
10618 return fold_builtin_bswap (fndecl, arg0);
10619
10620 CASE_INT_FN (BUILT_IN_FFS):
10621 CASE_INT_FN (BUILT_IN_CLZ):
10622 CASE_INT_FN (BUILT_IN_CTZ):
10623 CASE_INT_FN (BUILT_IN_CLRSB):
10624 CASE_INT_FN (BUILT_IN_POPCOUNT):
10625 CASE_INT_FN (BUILT_IN_PARITY):
10626 return fold_builtin_bitop (fndecl, arg0);
10627
10628 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10629 return fold_builtin_signbit (loc, arg0, type);
10630
10631 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10632 return fold_builtin_significand (loc, arg0, type);
10633
10634 CASE_FLT_FN (BUILT_IN_ILOGB):
10635 CASE_FLT_FN (BUILT_IN_LOGB):
10636 return fold_builtin_logb (loc, arg0, type);
10637
10638 case BUILT_IN_ISASCII:
10639 return fold_builtin_isascii (loc, arg0);
10640
10641 case BUILT_IN_TOASCII:
10642 return fold_builtin_toascii (loc, arg0);
10643
10644 case BUILT_IN_ISDIGIT:
10645 return fold_builtin_isdigit (loc, arg0);
10646
10647 CASE_FLT_FN (BUILT_IN_FINITE):
10648 case BUILT_IN_FINITED32:
10649 case BUILT_IN_FINITED64:
10650 case BUILT_IN_FINITED128:
10651 case BUILT_IN_ISFINITE:
10652 {
10653 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10654 if (ret)
10655 return ret;
10656 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10657 }
10658
10659 CASE_FLT_FN (BUILT_IN_ISINF):
10660 case BUILT_IN_ISINFD32:
10661 case BUILT_IN_ISINFD64:
10662 case BUILT_IN_ISINFD128:
10663 {
10664 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10665 if (ret)
10666 return ret;
10667 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10668 }
10669
10670 case BUILT_IN_ISNORMAL:
10671 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10672
10673 case BUILT_IN_ISINF_SIGN:
10674 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10675
10676 CASE_FLT_FN (BUILT_IN_ISNAN):
10677 case BUILT_IN_ISNAND32:
10678 case BUILT_IN_ISNAND64:
10679 case BUILT_IN_ISNAND128:
10680 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10681
10682 case BUILT_IN_PRINTF:
10683 case BUILT_IN_PRINTF_UNLOCKED:
10684 case BUILT_IN_VPRINTF:
10685 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10686
10687 case BUILT_IN_FREE:
10688 if (integer_zerop (arg0))
10689 return build_empty_stmt (loc);
10690 break;
10691
10692 default:
10693 break;
10694 }
10695
10696 return NULL_TREE;
10697
10698 }
10699
10700 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10701 IGNORE is true if the result of the function call is ignored. This
10702 function returns NULL_TREE if no simplification was possible. */
10703
10704 static tree
10705 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10706 {
10707 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10708 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10709
10710 switch (fcode)
10711 {
10712 CASE_FLT_FN (BUILT_IN_JN):
10713 if (validate_arg (arg0, INTEGER_TYPE)
10714 && validate_arg (arg1, REAL_TYPE))
10715 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10716 break;
10717
10718 CASE_FLT_FN (BUILT_IN_YN):
10719 if (validate_arg (arg0, INTEGER_TYPE)
10720 && validate_arg (arg1, REAL_TYPE))
10721 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10722 &dconst0, false);
10723 break;
10724
10725 CASE_FLT_FN (BUILT_IN_DREM):
10726 CASE_FLT_FN (BUILT_IN_REMAINDER):
10727 if (validate_arg (arg0, REAL_TYPE)
10728 && validate_arg (arg1, REAL_TYPE))
10729 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10730 break;
10731
10732 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10733 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10734 if (validate_arg (arg0, REAL_TYPE)
10735 && validate_arg (arg1, POINTER_TYPE))
10736 return do_mpfr_lgamma_r (arg0, arg1, type);
10737 break;
10738
10739 CASE_FLT_FN (BUILT_IN_ATAN2):
10740 if (validate_arg (arg0, REAL_TYPE)
10741 && validate_arg (arg1, REAL_TYPE))
10742 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10743 break;
10744
10745 CASE_FLT_FN (BUILT_IN_FDIM):
10746 if (validate_arg (arg0, REAL_TYPE)
10747 && validate_arg (arg1, REAL_TYPE))
10748 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10749 break;
10750
10751 CASE_FLT_FN (BUILT_IN_HYPOT):
10752 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10753
10754 CASE_FLT_FN (BUILT_IN_CPOW):
10755 if (validate_arg (arg0, COMPLEX_TYPE)
10756 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10757 && validate_arg (arg1, COMPLEX_TYPE)
10758 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10759 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10760 break;
10761
10762 CASE_FLT_FN (BUILT_IN_LDEXP):
10763 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10764 CASE_FLT_FN (BUILT_IN_SCALBN):
10765 CASE_FLT_FN (BUILT_IN_SCALBLN):
10766 return fold_builtin_load_exponent (loc, arg0, arg1,
10767 type, /*ldexp=*/false);
10768
10769 CASE_FLT_FN (BUILT_IN_FREXP):
10770 return fold_builtin_frexp (loc, arg0, arg1, type);
10771
10772 CASE_FLT_FN (BUILT_IN_MODF):
10773 return fold_builtin_modf (loc, arg0, arg1, type);
10774
10775 case BUILT_IN_BZERO:
10776 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10777
10778 case BUILT_IN_FPUTS:
10779 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10780
10781 case BUILT_IN_FPUTS_UNLOCKED:
10782 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10783
10784 case BUILT_IN_STRSTR:
10785 return fold_builtin_strstr (loc, arg0, arg1, type);
10786
10787 case BUILT_IN_STRCAT:
10788 return fold_builtin_strcat (loc, arg0, arg1);
10789
10790 case BUILT_IN_STRSPN:
10791 return fold_builtin_strspn (loc, arg0, arg1);
10792
10793 case BUILT_IN_STRCSPN:
10794 return fold_builtin_strcspn (loc, arg0, arg1);
10795
10796 case BUILT_IN_STRCHR:
10797 case BUILT_IN_INDEX:
10798 return fold_builtin_strchr (loc, arg0, arg1, type);
10799
10800 case BUILT_IN_STRRCHR:
10801 case BUILT_IN_RINDEX:
10802 return fold_builtin_strrchr (loc, arg0, arg1, type);
10803
10804 case BUILT_IN_STRCPY:
10805 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10806
10807 case BUILT_IN_STPCPY:
10808 if (ignore)
10809 {
10810 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10811 if (!fn)
10812 break;
10813
10814 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10815 }
10816 else
10817 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10818 break;
10819
10820 case BUILT_IN_STRCMP:
10821 return fold_builtin_strcmp (loc, arg0, arg1);
10822
10823 case BUILT_IN_STRPBRK:
10824 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10825
10826 case BUILT_IN_EXPECT:
10827 return fold_builtin_expect (loc, arg0, arg1);
10828
10829 CASE_FLT_FN (BUILT_IN_POW):
10830 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10831
10832 CASE_FLT_FN (BUILT_IN_POWI):
10833 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10834
10835 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10836 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10837
10838 CASE_FLT_FN (BUILT_IN_FMIN):
10839 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10840
10841 CASE_FLT_FN (BUILT_IN_FMAX):
10842 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10843
10844 case BUILT_IN_ISGREATER:
10845 return fold_builtin_unordered_cmp (loc, fndecl,
10846 arg0, arg1, UNLE_EXPR, LE_EXPR);
10847 case BUILT_IN_ISGREATEREQUAL:
10848 return fold_builtin_unordered_cmp (loc, fndecl,
10849 arg0, arg1, UNLT_EXPR, LT_EXPR);
10850 case BUILT_IN_ISLESS:
10851 return fold_builtin_unordered_cmp (loc, fndecl,
10852 arg0, arg1, UNGE_EXPR, GE_EXPR);
10853 case BUILT_IN_ISLESSEQUAL:
10854 return fold_builtin_unordered_cmp (loc, fndecl,
10855 arg0, arg1, UNGT_EXPR, GT_EXPR);
10856 case BUILT_IN_ISLESSGREATER:
10857 return fold_builtin_unordered_cmp (loc, fndecl,
10858 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10859 case BUILT_IN_ISUNORDERED:
10860 return fold_builtin_unordered_cmp (loc, fndecl,
10861 arg0, arg1, UNORDERED_EXPR,
10862 NOP_EXPR);
10863
10864 /* We do the folding for va_start in the expander. */
10865 case BUILT_IN_VA_START:
10866 break;
10867
10868 case BUILT_IN_SPRINTF:
10869 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10870
10871 case BUILT_IN_OBJECT_SIZE:
10872 return fold_builtin_object_size (arg0, arg1);
10873
10874 case BUILT_IN_PRINTF:
10875 case BUILT_IN_PRINTF_UNLOCKED:
10876 case BUILT_IN_VPRINTF:
10877 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10878
10879 case BUILT_IN_PRINTF_CHK:
10880 case BUILT_IN_VPRINTF_CHK:
10881 if (!validate_arg (arg0, INTEGER_TYPE)
10882 || TREE_SIDE_EFFECTS (arg0))
10883 return NULL_TREE;
10884 else
10885 return fold_builtin_printf (loc, fndecl,
10886 arg1, NULL_TREE, ignore, fcode);
10887 break;
10888
10889 case BUILT_IN_FPRINTF:
10890 case BUILT_IN_FPRINTF_UNLOCKED:
10891 case BUILT_IN_VFPRINTF:
10892 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10893 ignore, fcode);
10894
10895 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10896 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10897
10898 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10899 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10900
10901 default:
10902 break;
10903 }
10904 return NULL_TREE;
10905 }
10906
10907 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10908 and ARG2. IGNORE is true if the result of the function call is ignored.
10909 This function returns NULL_TREE if no simplification was possible. */
10910
10911 static tree
10912 fold_builtin_3 (location_t loc, tree fndecl,
10913 tree arg0, tree arg1, tree arg2, bool ignore)
10914 {
10915 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10916 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10917 switch (fcode)
10918 {
10919
10920 CASE_FLT_FN (BUILT_IN_SINCOS):
10921 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10922
10923 CASE_FLT_FN (BUILT_IN_FMA):
10924 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10925 break;
10926
10927 CASE_FLT_FN (BUILT_IN_REMQUO):
10928 if (validate_arg (arg0, REAL_TYPE)
10929 && validate_arg (arg1, REAL_TYPE)
10930 && validate_arg (arg2, POINTER_TYPE))
10931 return do_mpfr_remquo (arg0, arg1, arg2);
10932 break;
10933
10934 case BUILT_IN_MEMSET:
10935 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10936
10937 case BUILT_IN_BCOPY:
10938 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10939 void_type_node, true, /*endp=*/3);
10940
10941 case BUILT_IN_MEMCPY:
10942 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10943 type, ignore, /*endp=*/0);
10944
10945 case BUILT_IN_MEMPCPY:
10946 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10947 type, ignore, /*endp=*/1);
10948
10949 case BUILT_IN_MEMMOVE:
10950 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10951 type, ignore, /*endp=*/3);
10952
10953 case BUILT_IN_STRNCAT:
10954 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10955
10956 case BUILT_IN_STRNCPY:
10957 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10958
10959 case BUILT_IN_STRNCMP:
10960 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10961
10962 case BUILT_IN_MEMCHR:
10963 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10964
10965 case BUILT_IN_BCMP:
10966 case BUILT_IN_MEMCMP:
10967 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10968
10969 case BUILT_IN_SPRINTF:
10970 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10971
10972 case BUILT_IN_SNPRINTF:
10973 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10974
10975 case BUILT_IN_STRCPY_CHK:
10976 case BUILT_IN_STPCPY_CHK:
10977 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10978 ignore, fcode);
10979
10980 case BUILT_IN_STRCAT_CHK:
10981 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10982
10983 case BUILT_IN_PRINTF_CHK:
10984 case BUILT_IN_VPRINTF_CHK:
10985 if (!validate_arg (arg0, INTEGER_TYPE)
10986 || TREE_SIDE_EFFECTS (arg0))
10987 return NULL_TREE;
10988 else
10989 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10990 break;
10991
10992 case BUILT_IN_FPRINTF:
10993 case BUILT_IN_FPRINTF_UNLOCKED:
10994 case BUILT_IN_VFPRINTF:
10995 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10996 ignore, fcode);
10997
10998 case BUILT_IN_FPRINTF_CHK:
10999 case BUILT_IN_VFPRINTF_CHK:
11000 if (!validate_arg (arg1, INTEGER_TYPE)
11001 || TREE_SIDE_EFFECTS (arg1))
11002 return NULL_TREE;
11003 else
11004 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
11005 ignore, fcode);
11006
11007 default:
11008 break;
11009 }
11010 return NULL_TREE;
11011 }
11012
11013 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
11014 ARG2, and ARG3. IGNORE is true if the result of the function call is
11015 ignored. This function returns NULL_TREE if no simplification was
11016 possible. */
11017
11018 static tree
11019 fold_builtin_4 (location_t loc, tree fndecl,
11020 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11021 {
11022 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11023
11024 switch (fcode)
11025 {
11026 case BUILT_IN_MEMCPY_CHK:
11027 case BUILT_IN_MEMPCPY_CHK:
11028 case BUILT_IN_MEMMOVE_CHK:
11029 case BUILT_IN_MEMSET_CHK:
11030 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11031 NULL_TREE, ignore,
11032 DECL_FUNCTION_CODE (fndecl));
11033
11034 case BUILT_IN_STRNCPY_CHK:
11035 case BUILT_IN_STPNCPY_CHK:
11036 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11037 ignore, fcode);
11038
11039 case BUILT_IN_STRNCAT_CHK:
11040 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11041
11042 case BUILT_IN_SNPRINTF:
11043 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11044
11045 case BUILT_IN_FPRINTF_CHK:
11046 case BUILT_IN_VFPRINTF_CHK:
11047 if (!validate_arg (arg1, INTEGER_TYPE)
11048 || TREE_SIDE_EFFECTS (arg1))
11049 return NULL_TREE;
11050 else
11051 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11052 ignore, fcode);
11053 break;
11054
11055 default:
11056 break;
11057 }
11058 return NULL_TREE;
11059 }
11060
11061 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11062 arguments, where NARGS <= 4. IGNORE is true if the result of the
11063 function call is ignored. This function returns NULL_TREE if no
11064 simplification was possible. Note that this only folds builtins with
11065 fixed argument patterns. Foldings that do varargs-to-varargs
11066 transformations, or that match calls with more than 4 arguments,
11067 need to be handled with fold_builtin_varargs instead. */
11068
11069 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11070
11071 static tree
11072 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11073 {
11074 tree ret = NULL_TREE;
11075
11076 switch (nargs)
11077 {
11078 case 0:
11079 ret = fold_builtin_0 (loc, fndecl, ignore);
11080 break;
11081 case 1:
11082 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11083 break;
11084 case 2:
11085 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11086 break;
11087 case 3:
11088 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11089 break;
11090 case 4:
11091 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11092 ignore);
11093 break;
11094 default:
11095 break;
11096 }
11097 if (ret)
11098 {
11099 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11100 SET_EXPR_LOCATION (ret, loc);
11101 TREE_NO_WARNING (ret) = 1;
11102 return ret;
11103 }
11104 return NULL_TREE;
11105 }
11106
11107 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11108 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11109 of arguments in ARGS to be omitted. OLDNARGS is the number of
11110 elements in ARGS. */
11111
11112 static tree
11113 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11114 int skip, tree fndecl, int n, va_list newargs)
11115 {
11116 int nargs = oldnargs - skip + n;
11117 tree *buffer;
11118
11119 if (n > 0)
11120 {
11121 int i, j;
11122
11123 buffer = XALLOCAVEC (tree, nargs);
11124 for (i = 0; i < n; i++)
11125 buffer[i] = va_arg (newargs, tree);
11126 for (j = skip; j < oldnargs; j++, i++)
11127 buffer[i] = args[j];
11128 }
11129 else
11130 buffer = args + skip;
11131
11132 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11133 }
11134
11135 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11136 list ARGS along with N new arguments specified as the "..."
11137 parameters. SKIP is the number of arguments in ARGS to be omitted.
11138 OLDNARGS is the number of elements in ARGS. */
11139
11140 static tree
11141 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11142 int skip, tree fndecl, int n, ...)
11143 {
11144 va_list ap;
11145 tree t;
11146
11147 va_start (ap, n);
11148 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11149 va_end (ap);
11150
11151 return t;
11152 }
11153
11154 /* Return true if FNDECL shouldn't be folded right now.
11155 If a built-in function has an inline attribute always_inline
11156 wrapper, defer folding it after always_inline functions have
11157 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11158 might not be performed. */
11159
11160 bool
11161 avoid_folding_inline_builtin (tree fndecl)
11162 {
11163 return (DECL_DECLARED_INLINE_P (fndecl)
11164 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11165 && cfun
11166 && !cfun->always_inline_functions_inlined
11167 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11168 }
11169
11170 /* A wrapper function for builtin folding that prevents warnings for
11171 "statement without effect" and the like, caused by removing the
11172 call node earlier than the warning is generated. */
11173
11174 tree
11175 fold_call_expr (location_t loc, tree exp, bool ignore)
11176 {
11177 tree ret = NULL_TREE;
11178 tree fndecl = get_callee_fndecl (exp);
11179 if (fndecl
11180 && TREE_CODE (fndecl) == FUNCTION_DECL
11181 && DECL_BUILT_IN (fndecl)
11182 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11183 yet. Defer folding until we see all the arguments
11184 (after inlining). */
11185 && !CALL_EXPR_VA_ARG_PACK (exp))
11186 {
11187 int nargs = call_expr_nargs (exp);
11188
11189 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11190 instead last argument is __builtin_va_arg_pack (). Defer folding
11191 even in that case, until arguments are finalized. */
11192 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11193 {
11194 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11195 if (fndecl2
11196 && TREE_CODE (fndecl2) == FUNCTION_DECL
11197 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11198 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11199 return NULL_TREE;
11200 }
11201
11202 if (avoid_folding_inline_builtin (fndecl))
11203 return NULL_TREE;
11204
11205 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11206 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11207 CALL_EXPR_ARGP (exp), ignore);
11208 else
11209 {
11210 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11211 {
11212 tree *args = CALL_EXPR_ARGP (exp);
11213 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11214 }
11215 if (!ret)
11216 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11217 if (ret)
11218 return ret;
11219 }
11220 }
11221 return NULL_TREE;
11222 }
11223
11224 /* Conveniently construct a function call expression. FNDECL names the
11225 function to be called and N arguments are passed in the array
11226 ARGARRAY. */
11227
11228 tree
11229 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11230 {
11231 tree fntype = TREE_TYPE (fndecl);
11232 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11233
11234 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11235 }
11236
11237 /* Conveniently construct a function call expression. FNDECL names the
11238 function to be called and the arguments are passed in the vector
11239 VEC. */
11240
11241 tree
11242 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11243 {
11244 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11245 vec_safe_address (vec));
11246 }
11247
11248
11249 /* Conveniently construct a function call expression. FNDECL names the
11250 function to be called, N is the number of arguments, and the "..."
11251 parameters are the argument expressions. */
11252
11253 tree
11254 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11255 {
11256 va_list ap;
11257 tree *argarray = XALLOCAVEC (tree, n);
11258 int i;
11259
11260 va_start (ap, n);
11261 for (i = 0; i < n; i++)
11262 argarray[i] = va_arg (ap, tree);
11263 va_end (ap);
11264 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11265 }
11266
11267 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11268 varargs macros aren't supported by all bootstrap compilers. */
11269
11270 tree
11271 build_call_expr (tree fndecl, int n, ...)
11272 {
11273 va_list ap;
11274 tree *argarray = XALLOCAVEC (tree, n);
11275 int i;
11276
11277 va_start (ap, n);
11278 for (i = 0; i < n; i++)
11279 argarray[i] = va_arg (ap, tree);
11280 va_end (ap);
11281 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11282 }
11283
11284 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11285 N arguments are passed in the array ARGARRAY. */
11286
11287 tree
11288 fold_builtin_call_array (location_t loc, tree type,
11289 tree fn,
11290 int n,
11291 tree *argarray)
11292 {
11293 tree ret = NULL_TREE;
11294 tree exp;
11295
11296 if (TREE_CODE (fn) == ADDR_EXPR)
11297 {
11298 tree fndecl = TREE_OPERAND (fn, 0);
11299 if (TREE_CODE (fndecl) == FUNCTION_DECL
11300 && DECL_BUILT_IN (fndecl))
11301 {
11302 /* If last argument is __builtin_va_arg_pack (), arguments to this
11303 function are not finalized yet. Defer folding until they are. */
11304 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11305 {
11306 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11307 if (fndecl2
11308 && TREE_CODE (fndecl2) == FUNCTION_DECL
11309 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11310 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11311 return build_call_array_loc (loc, type, fn, n, argarray);
11312 }
11313 if (avoid_folding_inline_builtin (fndecl))
11314 return build_call_array_loc (loc, type, fn, n, argarray);
11315 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11316 {
11317 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11318 if (ret)
11319 return ret;
11320
11321 return build_call_array_loc (loc, type, fn, n, argarray);
11322 }
11323 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11324 {
11325 /* First try the transformations that don't require consing up
11326 an exp. */
11327 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11328 if (ret)
11329 return ret;
11330 }
11331
11332 /* If we got this far, we need to build an exp. */
11333 exp = build_call_array_loc (loc, type, fn, n, argarray);
11334 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11335 return ret ? ret : exp;
11336 }
11337 }
11338
11339 return build_call_array_loc (loc, type, fn, n, argarray);
11340 }
11341
11342 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11343 along with N new arguments specified as the "..." parameters. SKIP
11344 is the number of arguments in EXP to be omitted. This function is used
11345 to do varargs-to-varargs transformations. */
11346
11347 static tree
11348 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11349 {
11350 va_list ap;
11351 tree t;
11352
11353 va_start (ap, n);
11354 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11355 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11356 va_end (ap);
11357
11358 return t;
11359 }
11360
11361 /* Validate a single argument ARG against a tree code CODE representing
11362 a type. */
11363
11364 static bool
11365 validate_arg (const_tree arg, enum tree_code code)
11366 {
11367 if (!arg)
11368 return false;
11369 else if (code == POINTER_TYPE)
11370 return POINTER_TYPE_P (TREE_TYPE (arg));
11371 else if (code == INTEGER_TYPE)
11372 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11373 return code == TREE_CODE (TREE_TYPE (arg));
11374 }
11375
11376 /* This function validates the types of a function call argument list
11377 against a specified list of tree_codes. If the last specifier is a 0,
11378 that represents an ellipses, otherwise the last specifier must be a
11379 VOID_TYPE.
11380
11381 This is the GIMPLE version of validate_arglist. Eventually we want to
11382 completely convert builtins.c to work from GIMPLEs and the tree based
11383 validate_arglist will then be removed. */
11384
11385 bool
11386 validate_gimple_arglist (const_gimple call, ...)
11387 {
11388 enum tree_code code;
11389 bool res = 0;
11390 va_list ap;
11391 const_tree arg;
11392 size_t i;
11393
11394 va_start (ap, call);
11395 i = 0;
11396
11397 do
11398 {
11399 code = (enum tree_code) va_arg (ap, int);
11400 switch (code)
11401 {
11402 case 0:
11403 /* This signifies an ellipses, any further arguments are all ok. */
11404 res = true;
11405 goto end;
11406 case VOID_TYPE:
11407 /* This signifies an endlink, if no arguments remain, return
11408 true, otherwise return false. */
11409 res = (i == gimple_call_num_args (call));
11410 goto end;
11411 default:
11412 /* If no parameters remain or the parameter's code does not
11413 match the specified code, return false. Otherwise continue
11414 checking any remaining arguments. */
11415 arg = gimple_call_arg (call, i++);
11416 if (!validate_arg (arg, code))
11417 goto end;
11418 break;
11419 }
11420 }
11421 while (1);
11422
11423 /* We need gotos here since we can only have one VA_CLOSE in a
11424 function. */
11425 end: ;
11426 va_end (ap);
11427
11428 return res;
11429 }
11430
11431 /* Default target-specific builtin expander that does nothing. */
11432
11433 rtx
11434 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11435 rtx target ATTRIBUTE_UNUSED,
11436 rtx subtarget ATTRIBUTE_UNUSED,
11437 enum machine_mode mode ATTRIBUTE_UNUSED,
11438 int ignore ATTRIBUTE_UNUSED)
11439 {
11440 return NULL_RTX;
11441 }
11442
11443 /* Returns true is EXP represents data that would potentially reside
11444 in a readonly section. */
11445
11446 static bool
11447 readonly_data_expr (tree exp)
11448 {
11449 STRIP_NOPS (exp);
11450
11451 if (TREE_CODE (exp) != ADDR_EXPR)
11452 return false;
11453
11454 exp = get_base_address (TREE_OPERAND (exp, 0));
11455 if (!exp)
11456 return false;
11457
11458 /* Make sure we call decl_readonly_section only for trees it
11459 can handle (since it returns true for everything it doesn't
11460 understand). */
11461 if (TREE_CODE (exp) == STRING_CST
11462 || TREE_CODE (exp) == CONSTRUCTOR
11463 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11464 return decl_readonly_section (exp, 0);
11465 else
11466 return false;
11467 }
11468
11469 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11470 to the call, and TYPE is its return type.
11471
11472 Return NULL_TREE if no simplification was possible, otherwise return the
11473 simplified form of the call as a tree.
11474
11475 The simplified form may be a constant or other expression which
11476 computes the same value, but in a more efficient manner (including
11477 calls to other builtin functions).
11478
11479 The call may contain arguments which need to be evaluated, but
11480 which are not useful to determine the result of the call. In
11481 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11482 COMPOUND_EXPR will be an argument which must be evaluated.
11483 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11484 COMPOUND_EXPR in the chain will contain the tree for the simplified
11485 form of the builtin function call. */
11486
11487 static tree
11488 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11489 {
11490 if (!validate_arg (s1, POINTER_TYPE)
11491 || !validate_arg (s2, POINTER_TYPE))
11492 return NULL_TREE;
11493 else
11494 {
11495 tree fn;
11496 const char *p1, *p2;
11497
11498 p2 = c_getstr (s2);
11499 if (p2 == NULL)
11500 return NULL_TREE;
11501
11502 p1 = c_getstr (s1);
11503 if (p1 != NULL)
11504 {
11505 const char *r = strstr (p1, p2);
11506 tree tem;
11507
11508 if (r == NULL)
11509 return build_int_cst (TREE_TYPE (s1), 0);
11510
11511 /* Return an offset into the constant string argument. */
11512 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11513 return fold_convert_loc (loc, type, tem);
11514 }
11515
11516 /* The argument is const char *, and the result is char *, so we need
11517 a type conversion here to avoid a warning. */
11518 if (p2[0] == '\0')
11519 return fold_convert_loc (loc, type, s1);
11520
11521 if (p2[1] != '\0')
11522 return NULL_TREE;
11523
11524 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11525 if (!fn)
11526 return NULL_TREE;
11527
11528 /* New argument list transforming strstr(s1, s2) to
11529 strchr(s1, s2[0]). */
11530 return build_call_expr_loc (loc, fn, 2, s1,
11531 build_int_cst (integer_type_node, p2[0]));
11532 }
11533 }
11534
11535 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11536 the call, and TYPE is its return type.
11537
11538 Return NULL_TREE if no simplification was possible, otherwise return the
11539 simplified form of the call as a tree.
11540
11541 The simplified form may be a constant or other expression which
11542 computes the same value, but in a more efficient manner (including
11543 calls to other builtin functions).
11544
11545 The call may contain arguments which need to be evaluated, but
11546 which are not useful to determine the result of the call. In
11547 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11548 COMPOUND_EXPR will be an argument which must be evaluated.
11549 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11550 COMPOUND_EXPR in the chain will contain the tree for the simplified
11551 form of the builtin function call. */
11552
11553 static tree
11554 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11555 {
11556 if (!validate_arg (s1, POINTER_TYPE)
11557 || !validate_arg (s2, INTEGER_TYPE))
11558 return NULL_TREE;
11559 else
11560 {
11561 const char *p1;
11562
11563 if (TREE_CODE (s2) != INTEGER_CST)
11564 return NULL_TREE;
11565
11566 p1 = c_getstr (s1);
11567 if (p1 != NULL)
11568 {
11569 char c;
11570 const char *r;
11571 tree tem;
11572
11573 if (target_char_cast (s2, &c))
11574 return NULL_TREE;
11575
11576 r = strchr (p1, c);
11577
11578 if (r == NULL)
11579 return build_int_cst (TREE_TYPE (s1), 0);
11580
11581 /* Return an offset into the constant string argument. */
11582 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11583 return fold_convert_loc (loc, type, tem);
11584 }
11585 return NULL_TREE;
11586 }
11587 }
11588
11589 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11590 the call, and TYPE is its return type.
11591
11592 Return NULL_TREE if no simplification was possible, otherwise return the
11593 simplified form of the call as a tree.
11594
11595 The simplified form may be a constant or other expression which
11596 computes the same value, but in a more efficient manner (including
11597 calls to other builtin functions).
11598
11599 The call may contain arguments which need to be evaluated, but
11600 which are not useful to determine the result of the call. In
11601 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11602 COMPOUND_EXPR will be an argument which must be evaluated.
11603 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11604 COMPOUND_EXPR in the chain will contain the tree for the simplified
11605 form of the builtin function call. */
11606
11607 static tree
11608 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11609 {
11610 if (!validate_arg (s1, POINTER_TYPE)
11611 || !validate_arg (s2, INTEGER_TYPE))
11612 return NULL_TREE;
11613 else
11614 {
11615 tree fn;
11616 const char *p1;
11617
11618 if (TREE_CODE (s2) != INTEGER_CST)
11619 return NULL_TREE;
11620
11621 p1 = c_getstr (s1);
11622 if (p1 != NULL)
11623 {
11624 char c;
11625 const char *r;
11626 tree tem;
11627
11628 if (target_char_cast (s2, &c))
11629 return NULL_TREE;
11630
11631 r = strrchr (p1, c);
11632
11633 if (r == NULL)
11634 return build_int_cst (TREE_TYPE (s1), 0);
11635
11636 /* Return an offset into the constant string argument. */
11637 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11638 return fold_convert_loc (loc, type, tem);
11639 }
11640
11641 if (! integer_zerop (s2))
11642 return NULL_TREE;
11643
11644 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11645 if (!fn)
11646 return NULL_TREE;
11647
11648 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11649 return build_call_expr_loc (loc, fn, 2, s1, s2);
11650 }
11651 }
11652
11653 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11654 to the call, and TYPE is its return type.
11655
11656 Return NULL_TREE if no simplification was possible, otherwise return the
11657 simplified form of the call as a tree.
11658
11659 The simplified form may be a constant or other expression which
11660 computes the same value, but in a more efficient manner (including
11661 calls to other builtin functions).
11662
11663 The call may contain arguments which need to be evaluated, but
11664 which are not useful to determine the result of the call. In
11665 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11666 COMPOUND_EXPR will be an argument which must be evaluated.
11667 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11668 COMPOUND_EXPR in the chain will contain the tree for the simplified
11669 form of the builtin function call. */
11670
11671 static tree
11672 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11673 {
11674 if (!validate_arg (s1, POINTER_TYPE)
11675 || !validate_arg (s2, POINTER_TYPE))
11676 return NULL_TREE;
11677 else
11678 {
11679 tree fn;
11680 const char *p1, *p2;
11681
11682 p2 = c_getstr (s2);
11683 if (p2 == NULL)
11684 return NULL_TREE;
11685
11686 p1 = c_getstr (s1);
11687 if (p1 != NULL)
11688 {
11689 const char *r = strpbrk (p1, p2);
11690 tree tem;
11691
11692 if (r == NULL)
11693 return build_int_cst (TREE_TYPE (s1), 0);
11694
11695 /* Return an offset into the constant string argument. */
11696 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11697 return fold_convert_loc (loc, type, tem);
11698 }
11699
11700 if (p2[0] == '\0')
11701 /* strpbrk(x, "") == NULL.
11702 Evaluate and ignore s1 in case it had side-effects. */
11703 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11704
11705 if (p2[1] != '\0')
11706 return NULL_TREE; /* Really call strpbrk. */
11707
11708 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11709 if (!fn)
11710 return NULL_TREE;
11711
11712 /* New argument list transforming strpbrk(s1, s2) to
11713 strchr(s1, s2[0]). */
11714 return build_call_expr_loc (loc, fn, 2, s1,
11715 build_int_cst (integer_type_node, p2[0]));
11716 }
11717 }
11718
11719 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11720 to the call.
11721
11722 Return NULL_TREE if no simplification was possible, otherwise return the
11723 simplified form of the call as a tree.
11724
11725 The simplified form may be a constant or other expression which
11726 computes the same value, but in a more efficient manner (including
11727 calls to other builtin functions).
11728
11729 The call may contain arguments which need to be evaluated, but
11730 which are not useful to determine the result of the call. In
11731 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11732 COMPOUND_EXPR will be an argument which must be evaluated.
11733 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11734 COMPOUND_EXPR in the chain will contain the tree for the simplified
11735 form of the builtin function call. */
11736
11737 static tree
11738 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11739 {
11740 if (!validate_arg (dst, POINTER_TYPE)
11741 || !validate_arg (src, POINTER_TYPE))
11742 return NULL_TREE;
11743 else
11744 {
11745 const char *p = c_getstr (src);
11746
11747 /* If the string length is zero, return the dst parameter. */
11748 if (p && *p == '\0')
11749 return dst;
11750
11751 if (optimize_insn_for_speed_p ())
11752 {
11753 /* See if we can store by pieces into (dst + strlen(dst)). */
11754 tree newdst, call;
11755 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11756 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11757
11758 if (!strlen_fn || !strcpy_fn)
11759 return NULL_TREE;
11760
11761 /* If we don't have a movstr we don't want to emit an strcpy
11762 call. We have to do that if the length of the source string
11763 isn't computable (in that case we can use memcpy probably
11764 later expanding to a sequence of mov instructions). If we
11765 have movstr instructions we can emit strcpy calls. */
11766 if (!HAVE_movstr)
11767 {
11768 tree len = c_strlen (src, 1);
11769 if (! len || TREE_SIDE_EFFECTS (len))
11770 return NULL_TREE;
11771 }
11772
11773 /* Stabilize the argument list. */
11774 dst = builtin_save_expr (dst);
11775
11776 /* Create strlen (dst). */
11777 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11778 /* Create (dst p+ strlen (dst)). */
11779
11780 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11781 newdst = builtin_save_expr (newdst);
11782
11783 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11784 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11785 }
11786 return NULL_TREE;
11787 }
11788 }
11789
11790 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11791 arguments to the call.
11792
11793 Return NULL_TREE if no simplification was possible, otherwise return the
11794 simplified form of the call as a tree.
11795
11796 The simplified form may be a constant or other expression which
11797 computes the same value, but in a more efficient manner (including
11798 calls to other builtin functions).
11799
11800 The call may contain arguments which need to be evaluated, but
11801 which are not useful to determine the result of the call. In
11802 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11803 COMPOUND_EXPR will be an argument which must be evaluated.
11804 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11805 COMPOUND_EXPR in the chain will contain the tree for the simplified
11806 form of the builtin function call. */
11807
11808 static tree
11809 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11810 {
11811 if (!validate_arg (dst, POINTER_TYPE)
11812 || !validate_arg (src, POINTER_TYPE)
11813 || !validate_arg (len, INTEGER_TYPE))
11814 return NULL_TREE;
11815 else
11816 {
11817 const char *p = c_getstr (src);
11818
11819 /* If the requested length is zero, or the src parameter string
11820 length is zero, return the dst parameter. */
11821 if (integer_zerop (len) || (p && *p == '\0'))
11822 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11823
11824 /* If the requested len is greater than or equal to the string
11825 length, call strcat. */
11826 if (TREE_CODE (len) == INTEGER_CST && p
11827 && compare_tree_int (len, strlen (p)) >= 0)
11828 {
11829 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11830
11831 /* If the replacement _DECL isn't initialized, don't do the
11832 transformation. */
11833 if (!fn)
11834 return NULL_TREE;
11835
11836 return build_call_expr_loc (loc, fn, 2, dst, src);
11837 }
11838 return NULL_TREE;
11839 }
11840 }
11841
11842 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11843 to the call.
11844
11845 Return NULL_TREE if no simplification was possible, otherwise return the
11846 simplified form of the call as a tree.
11847
11848 The simplified form may be a constant or other expression which
11849 computes the same value, but in a more efficient manner (including
11850 calls to other builtin functions).
11851
11852 The call may contain arguments which need to be evaluated, but
11853 which are not useful to determine the result of the call. In
11854 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11855 COMPOUND_EXPR will be an argument which must be evaluated.
11856 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11857 COMPOUND_EXPR in the chain will contain the tree for the simplified
11858 form of the builtin function call. */
11859
11860 static tree
11861 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11862 {
11863 if (!validate_arg (s1, POINTER_TYPE)
11864 || !validate_arg (s2, POINTER_TYPE))
11865 return NULL_TREE;
11866 else
11867 {
11868 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11869
11870 /* If both arguments are constants, evaluate at compile-time. */
11871 if (p1 && p2)
11872 {
11873 const size_t r = strspn (p1, p2);
11874 return build_int_cst (size_type_node, r);
11875 }
11876
11877 /* If either argument is "", return NULL_TREE. */
11878 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11879 /* Evaluate and ignore both arguments in case either one has
11880 side-effects. */
11881 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11882 s1, s2);
11883 return NULL_TREE;
11884 }
11885 }
11886
11887 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11888 to the call.
11889
11890 Return NULL_TREE if no simplification was possible, otherwise return the
11891 simplified form of the call as a tree.
11892
11893 The simplified form may be a constant or other expression which
11894 computes the same value, but in a more efficient manner (including
11895 calls to other builtin functions).
11896
11897 The call may contain arguments which need to be evaluated, but
11898 which are not useful to determine the result of the call. In
11899 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11900 COMPOUND_EXPR will be an argument which must be evaluated.
11901 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11902 COMPOUND_EXPR in the chain will contain the tree for the simplified
11903 form of the builtin function call. */
11904
11905 static tree
11906 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11907 {
11908 if (!validate_arg (s1, POINTER_TYPE)
11909 || !validate_arg (s2, POINTER_TYPE))
11910 return NULL_TREE;
11911 else
11912 {
11913 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11914
11915 /* If both arguments are constants, evaluate at compile-time. */
11916 if (p1 && p2)
11917 {
11918 const size_t r = strcspn (p1, p2);
11919 return build_int_cst (size_type_node, r);
11920 }
11921
11922 /* If the first argument is "", return NULL_TREE. */
11923 if (p1 && *p1 == '\0')
11924 {
11925 /* Evaluate and ignore argument s2 in case it has
11926 side-effects. */
11927 return omit_one_operand_loc (loc, size_type_node,
11928 size_zero_node, s2);
11929 }
11930
11931 /* If the second argument is "", return __builtin_strlen(s1). */
11932 if (p2 && *p2 == '\0')
11933 {
11934 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11935
11936 /* If the replacement _DECL isn't initialized, don't do the
11937 transformation. */
11938 if (!fn)
11939 return NULL_TREE;
11940
11941 return build_call_expr_loc (loc, fn, 1, s1);
11942 }
11943 return NULL_TREE;
11944 }
11945 }
11946
11947 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11948 to the call. IGNORE is true if the value returned
11949 by the builtin will be ignored. UNLOCKED is true is true if this
11950 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11951 the known length of the string. Return NULL_TREE if no simplification
11952 was possible. */
11953
11954 tree
11955 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11956 bool ignore, bool unlocked, tree len)
11957 {
11958 /* If we're using an unlocked function, assume the other unlocked
11959 functions exist explicitly. */
11960 tree const fn_fputc = (unlocked
11961 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11962 : builtin_decl_implicit (BUILT_IN_FPUTC));
11963 tree const fn_fwrite = (unlocked
11964 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11965 : builtin_decl_implicit (BUILT_IN_FWRITE));
11966
11967 /* If the return value is used, don't do the transformation. */
11968 if (!ignore)
11969 return NULL_TREE;
11970
11971 /* Verify the arguments in the original call. */
11972 if (!validate_arg (arg0, POINTER_TYPE)
11973 || !validate_arg (arg1, POINTER_TYPE))
11974 return NULL_TREE;
11975
11976 if (! len)
11977 len = c_strlen (arg0, 0);
11978
11979 /* Get the length of the string passed to fputs. If the length
11980 can't be determined, punt. */
11981 if (!len
11982 || TREE_CODE (len) != INTEGER_CST)
11983 return NULL_TREE;
11984
11985 switch (compare_tree_int (len, 1))
11986 {
11987 case -1: /* length is 0, delete the call entirely . */
11988 return omit_one_operand_loc (loc, integer_type_node,
11989 integer_zero_node, arg1);;
11990
11991 case 0: /* length is 1, call fputc. */
11992 {
11993 const char *p = c_getstr (arg0);
11994
11995 if (p != NULL)
11996 {
11997 if (fn_fputc)
11998 return build_call_expr_loc (loc, fn_fputc, 2,
11999 build_int_cst
12000 (integer_type_node, p[0]), arg1);
12001 else
12002 return NULL_TREE;
12003 }
12004 }
12005 /* FALLTHROUGH */
12006 case 1: /* length is greater than 1, call fwrite. */
12007 {
12008 /* If optimizing for size keep fputs. */
12009 if (optimize_function_for_size_p (cfun))
12010 return NULL_TREE;
12011 /* New argument list transforming fputs(string, stream) to
12012 fwrite(string, 1, len, stream). */
12013 if (fn_fwrite)
12014 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12015 size_one_node, len, arg1);
12016 else
12017 return NULL_TREE;
12018 }
12019 default:
12020 gcc_unreachable ();
12021 }
12022 return NULL_TREE;
12023 }
12024
12025 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12026 produced. False otherwise. This is done so that we don't output the error
12027 or warning twice or three times. */
12028
12029 bool
12030 fold_builtin_next_arg (tree exp, bool va_start_p)
12031 {
12032 tree fntype = TREE_TYPE (current_function_decl);
12033 int nargs = call_expr_nargs (exp);
12034 tree arg;
12035 /* There is good chance the current input_location points inside the
12036 definition of the va_start macro (perhaps on the token for
12037 builtin) in a system header, so warnings will not be emitted.
12038 Use the location in real source code. */
12039 source_location current_location =
12040 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12041 NULL);
12042
12043 if (!stdarg_p (fntype))
12044 {
12045 error ("%<va_start%> used in function with fixed args");
12046 return true;
12047 }
12048
12049 if (va_start_p)
12050 {
12051 if (va_start_p && (nargs != 2))
12052 {
12053 error ("wrong number of arguments to function %<va_start%>");
12054 return true;
12055 }
12056 arg = CALL_EXPR_ARG (exp, 1);
12057 }
12058 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12059 when we checked the arguments and if needed issued a warning. */
12060 else
12061 {
12062 if (nargs == 0)
12063 {
12064 /* Evidently an out of date version of <stdarg.h>; can't validate
12065 va_start's second argument, but can still work as intended. */
12066 warning_at (current_location,
12067 OPT_Wvarargs,
12068 "%<__builtin_next_arg%> called without an argument");
12069 return true;
12070 }
12071 else if (nargs > 1)
12072 {
12073 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12074 return true;
12075 }
12076 arg = CALL_EXPR_ARG (exp, 0);
12077 }
12078
12079 if (TREE_CODE (arg) == SSA_NAME)
12080 arg = SSA_NAME_VAR (arg);
12081
12082 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12083 or __builtin_next_arg (0) the first time we see it, after checking
12084 the arguments and if needed issuing a warning. */
12085 if (!integer_zerop (arg))
12086 {
12087 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12088
12089 /* Strip off all nops for the sake of the comparison. This
12090 is not quite the same as STRIP_NOPS. It does more.
12091 We must also strip off INDIRECT_EXPR for C++ reference
12092 parameters. */
12093 while (CONVERT_EXPR_P (arg)
12094 || TREE_CODE (arg) == INDIRECT_REF)
12095 arg = TREE_OPERAND (arg, 0);
12096 if (arg != last_parm)
12097 {
12098 /* FIXME: Sometimes with the tree optimizers we can get the
12099 not the last argument even though the user used the last
12100 argument. We just warn and set the arg to be the last
12101 argument so that we will get wrong-code because of
12102 it. */
12103 warning_at (current_location,
12104 OPT_Wvarargs,
12105 "second parameter of %<va_start%> not last named argument");
12106 }
12107
12108 /* Undefined by C99 7.15.1.4p4 (va_start):
12109 "If the parameter parmN is declared with the register storage
12110 class, with a function or array type, or with a type that is
12111 not compatible with the type that results after application of
12112 the default argument promotions, the behavior is undefined."
12113 */
12114 else if (DECL_REGISTER (arg))
12115 {
12116 warning_at (current_location,
12117 OPT_Wvarargs,
12118 "undefined behaviour when second parameter of "
12119 "%<va_start%> is declared with %<register%> storage");
12120 }
12121
12122 /* We want to verify the second parameter just once before the tree
12123 optimizers are run and then avoid keeping it in the tree,
12124 as otherwise we could warn even for correct code like:
12125 void foo (int i, ...)
12126 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12127 if (va_start_p)
12128 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12129 else
12130 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12131 }
12132 return false;
12133 }
12134
12135
12136 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12137 ORIG may be null if this is a 2-argument call. We don't attempt to
12138 simplify calls with more than 3 arguments.
12139
12140 Return NULL_TREE if no simplification was possible, otherwise return the
12141 simplified form of the call as a tree. If IGNORED is true, it means that
12142 the caller does not use the returned value of the function. */
12143
12144 static tree
12145 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12146 tree orig, int ignored)
12147 {
12148 tree call, retval;
12149 const char *fmt_str = NULL;
12150
12151 /* Verify the required arguments in the original call. We deal with two
12152 types of sprintf() calls: 'sprintf (str, fmt)' and
12153 'sprintf (dest, "%s", orig)'. */
12154 if (!validate_arg (dest, POINTER_TYPE)
12155 || !validate_arg (fmt, POINTER_TYPE))
12156 return NULL_TREE;
12157 if (orig && !validate_arg (orig, POINTER_TYPE))
12158 return NULL_TREE;
12159
12160 /* Check whether the format is a literal string constant. */
12161 fmt_str = c_getstr (fmt);
12162 if (fmt_str == NULL)
12163 return NULL_TREE;
12164
12165 call = NULL_TREE;
12166 retval = NULL_TREE;
12167
12168 if (!init_target_chars ())
12169 return NULL_TREE;
12170
12171 /* If the format doesn't contain % args or %%, use strcpy. */
12172 if (strchr (fmt_str, target_percent) == NULL)
12173 {
12174 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12175
12176 if (!fn)
12177 return NULL_TREE;
12178
12179 /* Don't optimize sprintf (buf, "abc", ptr++). */
12180 if (orig)
12181 return NULL_TREE;
12182
12183 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12184 'format' is known to contain no % formats. */
12185 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12186 if (!ignored)
12187 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12188 }
12189
12190 /* If the format is "%s", use strcpy if the result isn't used. */
12191 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12192 {
12193 tree fn;
12194 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12195
12196 if (!fn)
12197 return NULL_TREE;
12198
12199 /* Don't crash on sprintf (str1, "%s"). */
12200 if (!orig)
12201 return NULL_TREE;
12202
12203 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12204 if (!ignored)
12205 {
12206 retval = c_strlen (orig, 1);
12207 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12208 return NULL_TREE;
12209 }
12210 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12211 }
12212
12213 if (call && retval)
12214 {
12215 retval = fold_convert_loc
12216 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12217 retval);
12218 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12219 }
12220 else
12221 return call;
12222 }
12223
12224 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12225 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12226 attempt to simplify calls with more than 4 arguments.
12227
12228 Return NULL_TREE if no simplification was possible, otherwise return the
12229 simplified form of the call as a tree. If IGNORED is true, it means that
12230 the caller does not use the returned value of the function. */
12231
12232 static tree
12233 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12234 tree orig, int ignored)
12235 {
12236 tree call, retval;
12237 const char *fmt_str = NULL;
12238 unsigned HOST_WIDE_INT destlen;
12239
12240 /* Verify the required arguments in the original call. We deal with two
12241 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12242 'snprintf (dest, cst, "%s", orig)'. */
12243 if (!validate_arg (dest, POINTER_TYPE)
12244 || !validate_arg (destsize, INTEGER_TYPE)
12245 || !validate_arg (fmt, POINTER_TYPE))
12246 return NULL_TREE;
12247 if (orig && !validate_arg (orig, POINTER_TYPE))
12248 return NULL_TREE;
12249
12250 if (!tree_fits_uhwi_p (destsize))
12251 return NULL_TREE;
12252
12253 /* Check whether the format is a literal string constant. */
12254 fmt_str = c_getstr (fmt);
12255 if (fmt_str == NULL)
12256 return NULL_TREE;
12257
12258 call = NULL_TREE;
12259 retval = NULL_TREE;
12260
12261 if (!init_target_chars ())
12262 return NULL_TREE;
12263
12264 destlen = tree_to_uhwi (destsize);
12265
12266 /* If the format doesn't contain % args or %%, use strcpy. */
12267 if (strchr (fmt_str, target_percent) == NULL)
12268 {
12269 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12270 size_t len = strlen (fmt_str);
12271
12272 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12273 if (orig)
12274 return NULL_TREE;
12275
12276 /* We could expand this as
12277 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12278 or to
12279 memcpy (str, fmt_with_nul_at_cstm1, cst);
12280 but in the former case that might increase code size
12281 and in the latter case grow .rodata section too much.
12282 So punt for now. */
12283 if (len >= destlen)
12284 return NULL_TREE;
12285
12286 if (!fn)
12287 return NULL_TREE;
12288
12289 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12290 'format' is known to contain no % formats and
12291 strlen (fmt) < cst. */
12292 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12293
12294 if (!ignored)
12295 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12296 }
12297
12298 /* If the format is "%s", use strcpy if the result isn't used. */
12299 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12300 {
12301 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12302 unsigned HOST_WIDE_INT origlen;
12303
12304 /* Don't crash on snprintf (str1, cst, "%s"). */
12305 if (!orig)
12306 return NULL_TREE;
12307
12308 retval = c_strlen (orig, 1);
12309 if (!retval || !tree_fits_uhwi_p (retval))
12310 return NULL_TREE;
12311
12312 origlen = tree_to_uhwi (retval);
12313 /* We could expand this as
12314 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12315 or to
12316 memcpy (str1, str2_with_nul_at_cstm1, cst);
12317 but in the former case that might increase code size
12318 and in the latter case grow .rodata section too much.
12319 So punt for now. */
12320 if (origlen >= destlen)
12321 return NULL_TREE;
12322
12323 /* Convert snprintf (str1, cst, "%s", str2) into
12324 strcpy (str1, str2) if strlen (str2) < cst. */
12325 if (!fn)
12326 return NULL_TREE;
12327
12328 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12329
12330 if (ignored)
12331 retval = NULL_TREE;
12332 }
12333
12334 if (call && retval)
12335 {
12336 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12337 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12338 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12339 }
12340 else
12341 return call;
12342 }
12343
12344 /* Expand a call EXP to __builtin_object_size. */
12345
12346 rtx
12347 expand_builtin_object_size (tree exp)
12348 {
12349 tree ost;
12350 int object_size_type;
12351 tree fndecl = get_callee_fndecl (exp);
12352
12353 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12354 {
12355 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12356 exp, fndecl);
12357 expand_builtin_trap ();
12358 return const0_rtx;
12359 }
12360
12361 ost = CALL_EXPR_ARG (exp, 1);
12362 STRIP_NOPS (ost);
12363
12364 if (TREE_CODE (ost) != INTEGER_CST
12365 || tree_int_cst_sgn (ost) < 0
12366 || compare_tree_int (ost, 3) > 0)
12367 {
12368 error ("%Klast argument of %D is not integer constant between 0 and 3",
12369 exp, fndecl);
12370 expand_builtin_trap ();
12371 return const0_rtx;
12372 }
12373
12374 object_size_type = tree_to_shwi (ost);
12375
12376 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12377 }
12378
12379 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12380 FCODE is the BUILT_IN_* to use.
12381 Return NULL_RTX if we failed; the caller should emit a normal call,
12382 otherwise try to get the result in TARGET, if convenient (and in
12383 mode MODE if that's convenient). */
12384
12385 static rtx
12386 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12387 enum built_in_function fcode)
12388 {
12389 tree dest, src, len, size;
12390
12391 if (!validate_arglist (exp,
12392 POINTER_TYPE,
12393 fcode == BUILT_IN_MEMSET_CHK
12394 ? INTEGER_TYPE : POINTER_TYPE,
12395 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12396 return NULL_RTX;
12397
12398 dest = CALL_EXPR_ARG (exp, 0);
12399 src = CALL_EXPR_ARG (exp, 1);
12400 len = CALL_EXPR_ARG (exp, 2);
12401 size = CALL_EXPR_ARG (exp, 3);
12402
12403 if (! tree_fits_uhwi_p (size))
12404 return NULL_RTX;
12405
12406 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12407 {
12408 tree fn;
12409
12410 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12411 {
12412 warning_at (tree_nonartificial_location (exp),
12413 0, "%Kcall to %D will always overflow destination buffer",
12414 exp, get_callee_fndecl (exp));
12415 return NULL_RTX;
12416 }
12417
12418 fn = NULL_TREE;
12419 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12420 mem{cpy,pcpy,move,set} is available. */
12421 switch (fcode)
12422 {
12423 case BUILT_IN_MEMCPY_CHK:
12424 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12425 break;
12426 case BUILT_IN_MEMPCPY_CHK:
12427 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12428 break;
12429 case BUILT_IN_MEMMOVE_CHK:
12430 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12431 break;
12432 case BUILT_IN_MEMSET_CHK:
12433 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12434 break;
12435 default:
12436 break;
12437 }
12438
12439 if (! fn)
12440 return NULL_RTX;
12441
12442 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12443 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12444 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12445 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12446 }
12447 else if (fcode == BUILT_IN_MEMSET_CHK)
12448 return NULL_RTX;
12449 else
12450 {
12451 unsigned int dest_align = get_pointer_alignment (dest);
12452
12453 /* If DEST is not a pointer type, call the normal function. */
12454 if (dest_align == 0)
12455 return NULL_RTX;
12456
12457 /* If SRC and DEST are the same (and not volatile), do nothing. */
12458 if (operand_equal_p (src, dest, 0))
12459 {
12460 tree expr;
12461
12462 if (fcode != BUILT_IN_MEMPCPY_CHK)
12463 {
12464 /* Evaluate and ignore LEN in case it has side-effects. */
12465 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12466 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12467 }
12468
12469 expr = fold_build_pointer_plus (dest, len);
12470 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12471 }
12472
12473 /* __memmove_chk special case. */
12474 if (fcode == BUILT_IN_MEMMOVE_CHK)
12475 {
12476 unsigned int src_align = get_pointer_alignment (src);
12477
12478 if (src_align == 0)
12479 return NULL_RTX;
12480
12481 /* If src is categorized for a readonly section we can use
12482 normal __memcpy_chk. */
12483 if (readonly_data_expr (src))
12484 {
12485 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12486 if (!fn)
12487 return NULL_RTX;
12488 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12489 dest, src, len, size);
12490 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12491 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12492 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12493 }
12494 }
12495 return NULL_RTX;
12496 }
12497 }
12498
12499 /* Emit warning if a buffer overflow is detected at compile time. */
12500
12501 static void
12502 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12503 {
12504 int is_strlen = 0;
12505 tree len, size;
12506 location_t loc = tree_nonartificial_location (exp);
12507
12508 switch (fcode)
12509 {
12510 case BUILT_IN_STRCPY_CHK:
12511 case BUILT_IN_STPCPY_CHK:
12512 /* For __strcat_chk the warning will be emitted only if overflowing
12513 by at least strlen (dest) + 1 bytes. */
12514 case BUILT_IN_STRCAT_CHK:
12515 len = CALL_EXPR_ARG (exp, 1);
12516 size = CALL_EXPR_ARG (exp, 2);
12517 is_strlen = 1;
12518 break;
12519 case BUILT_IN_STRNCAT_CHK:
12520 case BUILT_IN_STRNCPY_CHK:
12521 case BUILT_IN_STPNCPY_CHK:
12522 len = CALL_EXPR_ARG (exp, 2);
12523 size = CALL_EXPR_ARG (exp, 3);
12524 break;
12525 case BUILT_IN_SNPRINTF_CHK:
12526 case BUILT_IN_VSNPRINTF_CHK:
12527 len = CALL_EXPR_ARG (exp, 1);
12528 size = CALL_EXPR_ARG (exp, 3);
12529 break;
12530 default:
12531 gcc_unreachable ();
12532 }
12533
12534 if (!len || !size)
12535 return;
12536
12537 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12538 return;
12539
12540 if (is_strlen)
12541 {
12542 len = c_strlen (len, 1);
12543 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12544 return;
12545 }
12546 else if (fcode == BUILT_IN_STRNCAT_CHK)
12547 {
12548 tree src = CALL_EXPR_ARG (exp, 1);
12549 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12550 return;
12551 src = c_strlen (src, 1);
12552 if (! src || ! tree_fits_uhwi_p (src))
12553 {
12554 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12555 exp, get_callee_fndecl (exp));
12556 return;
12557 }
12558 else if (tree_int_cst_lt (src, size))
12559 return;
12560 }
12561 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12562 return;
12563
12564 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12565 exp, get_callee_fndecl (exp));
12566 }
12567
12568 /* Emit warning if a buffer overflow is detected at compile time
12569 in __sprintf_chk/__vsprintf_chk calls. */
12570
12571 static void
12572 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12573 {
12574 tree size, len, fmt;
12575 const char *fmt_str;
12576 int nargs = call_expr_nargs (exp);
12577
12578 /* Verify the required arguments in the original call. */
12579
12580 if (nargs < 4)
12581 return;
12582 size = CALL_EXPR_ARG (exp, 2);
12583 fmt = CALL_EXPR_ARG (exp, 3);
12584
12585 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12586 return;
12587
12588 /* Check whether the format is a literal string constant. */
12589 fmt_str = c_getstr (fmt);
12590 if (fmt_str == NULL)
12591 return;
12592
12593 if (!init_target_chars ())
12594 return;
12595
12596 /* If the format doesn't contain % args or %%, we know its size. */
12597 if (strchr (fmt_str, target_percent) == 0)
12598 len = build_int_cstu (size_type_node, strlen (fmt_str));
12599 /* If the format is "%s" and first ... argument is a string literal,
12600 we know it too. */
12601 else if (fcode == BUILT_IN_SPRINTF_CHK
12602 && strcmp (fmt_str, target_percent_s) == 0)
12603 {
12604 tree arg;
12605
12606 if (nargs < 5)
12607 return;
12608 arg = CALL_EXPR_ARG (exp, 4);
12609 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12610 return;
12611
12612 len = c_strlen (arg, 1);
12613 if (!len || ! tree_fits_uhwi_p (len))
12614 return;
12615 }
12616 else
12617 return;
12618
12619 if (! tree_int_cst_lt (len, size))
12620 warning_at (tree_nonartificial_location (exp),
12621 0, "%Kcall to %D will always overflow destination buffer",
12622 exp, get_callee_fndecl (exp));
12623 }
12624
12625 /* Emit warning if a free is called with address of a variable. */
12626
12627 static void
12628 maybe_emit_free_warning (tree exp)
12629 {
12630 tree arg = CALL_EXPR_ARG (exp, 0);
12631
12632 STRIP_NOPS (arg);
12633 if (TREE_CODE (arg) != ADDR_EXPR)
12634 return;
12635
12636 arg = get_base_address (TREE_OPERAND (arg, 0));
12637 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12638 return;
12639
12640 if (SSA_VAR_P (arg))
12641 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12642 "%Kattempt to free a non-heap object %qD", exp, arg);
12643 else
12644 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12645 "%Kattempt to free a non-heap object", exp);
12646 }
12647
12648 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12649 if possible. */
12650
12651 tree
12652 fold_builtin_object_size (tree ptr, tree ost)
12653 {
12654 unsigned HOST_WIDE_INT bytes;
12655 int object_size_type;
12656
12657 if (!validate_arg (ptr, POINTER_TYPE)
12658 || !validate_arg (ost, INTEGER_TYPE))
12659 return NULL_TREE;
12660
12661 STRIP_NOPS (ost);
12662
12663 if (TREE_CODE (ost) != INTEGER_CST
12664 || tree_int_cst_sgn (ost) < 0
12665 || compare_tree_int (ost, 3) > 0)
12666 return NULL_TREE;
12667
12668 object_size_type = tree_to_shwi (ost);
12669
12670 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12671 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12672 and (size_t) 0 for types 2 and 3. */
12673 if (TREE_SIDE_EFFECTS (ptr))
12674 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12675
12676 if (TREE_CODE (ptr) == ADDR_EXPR)
12677 {
12678 bytes = compute_builtin_object_size (ptr, object_size_type);
12679 if (double_int_fits_to_tree_p (size_type_node,
12680 double_int::from_uhwi (bytes)))
12681 return build_int_cstu (size_type_node, bytes);
12682 }
12683 else if (TREE_CODE (ptr) == SSA_NAME)
12684 {
12685 /* If object size is not known yet, delay folding until
12686 later. Maybe subsequent passes will help determining
12687 it. */
12688 bytes = compute_builtin_object_size (ptr, object_size_type);
12689 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12690 && double_int_fits_to_tree_p (size_type_node,
12691 double_int::from_uhwi (bytes)))
12692 return build_int_cstu (size_type_node, bytes);
12693 }
12694
12695 return NULL_TREE;
12696 }
12697
12698 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12699 DEST, SRC, LEN, and SIZE are the arguments to the call.
12700 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12701 code of the builtin. If MAXLEN is not NULL, it is maximum length
12702 passed as third argument. */
12703
12704 tree
12705 fold_builtin_memory_chk (location_t loc, tree fndecl,
12706 tree dest, tree src, tree len, tree size,
12707 tree maxlen, bool ignore,
12708 enum built_in_function fcode)
12709 {
12710 tree fn;
12711
12712 if (!validate_arg (dest, POINTER_TYPE)
12713 || !validate_arg (src,
12714 (fcode == BUILT_IN_MEMSET_CHK
12715 ? INTEGER_TYPE : POINTER_TYPE))
12716 || !validate_arg (len, INTEGER_TYPE)
12717 || !validate_arg (size, INTEGER_TYPE))
12718 return NULL_TREE;
12719
12720 /* If SRC and DEST are the same (and not volatile), return DEST
12721 (resp. DEST+LEN for __mempcpy_chk). */
12722 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12723 {
12724 if (fcode != BUILT_IN_MEMPCPY_CHK)
12725 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12726 dest, len);
12727 else
12728 {
12729 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12730 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12731 }
12732 }
12733
12734 if (! tree_fits_uhwi_p (size))
12735 return NULL_TREE;
12736
12737 if (! integer_all_onesp (size))
12738 {
12739 if (! tree_fits_uhwi_p (len))
12740 {
12741 /* If LEN is not constant, try MAXLEN too.
12742 For MAXLEN only allow optimizing into non-_ocs function
12743 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12744 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12745 {
12746 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12747 {
12748 /* (void) __mempcpy_chk () can be optimized into
12749 (void) __memcpy_chk (). */
12750 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12751 if (!fn)
12752 return NULL_TREE;
12753
12754 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12755 }
12756 return NULL_TREE;
12757 }
12758 }
12759 else
12760 maxlen = len;
12761
12762 if (tree_int_cst_lt (size, maxlen))
12763 return NULL_TREE;
12764 }
12765
12766 fn = NULL_TREE;
12767 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12768 mem{cpy,pcpy,move,set} is available. */
12769 switch (fcode)
12770 {
12771 case BUILT_IN_MEMCPY_CHK:
12772 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12773 break;
12774 case BUILT_IN_MEMPCPY_CHK:
12775 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12776 break;
12777 case BUILT_IN_MEMMOVE_CHK:
12778 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12779 break;
12780 case BUILT_IN_MEMSET_CHK:
12781 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12782 break;
12783 default:
12784 break;
12785 }
12786
12787 if (!fn)
12788 return NULL_TREE;
12789
12790 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12791 }
12792
12793 /* Fold a call to the __st[rp]cpy_chk builtin.
12794 DEST, SRC, and SIZE are the arguments to the call.
12795 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12796 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12797 strings passed as second argument. */
12798
12799 tree
12800 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12801 tree src, tree size,
12802 tree maxlen, bool ignore,
12803 enum built_in_function fcode)
12804 {
12805 tree len, fn;
12806
12807 if (!validate_arg (dest, POINTER_TYPE)
12808 || !validate_arg (src, POINTER_TYPE)
12809 || !validate_arg (size, INTEGER_TYPE))
12810 return NULL_TREE;
12811
12812 /* If SRC and DEST are the same (and not volatile), return DEST. */
12813 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12814 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12815
12816 if (! tree_fits_uhwi_p (size))
12817 return NULL_TREE;
12818
12819 if (! integer_all_onesp (size))
12820 {
12821 len = c_strlen (src, 1);
12822 if (! len || ! tree_fits_uhwi_p (len))
12823 {
12824 /* If LEN is not constant, try MAXLEN too.
12825 For MAXLEN only allow optimizing into non-_ocs function
12826 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12827 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12828 {
12829 if (fcode == BUILT_IN_STPCPY_CHK)
12830 {
12831 if (! ignore)
12832 return NULL_TREE;
12833
12834 /* If return value of __stpcpy_chk is ignored,
12835 optimize into __strcpy_chk. */
12836 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12837 if (!fn)
12838 return NULL_TREE;
12839
12840 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12841 }
12842
12843 if (! len || TREE_SIDE_EFFECTS (len))
12844 return NULL_TREE;
12845
12846 /* If c_strlen returned something, but not a constant,
12847 transform __strcpy_chk into __memcpy_chk. */
12848 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12849 if (!fn)
12850 return NULL_TREE;
12851
12852 len = fold_convert_loc (loc, size_type_node, len);
12853 len = size_binop_loc (loc, PLUS_EXPR, len,
12854 build_int_cst (size_type_node, 1));
12855 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12856 build_call_expr_loc (loc, fn, 4,
12857 dest, src, len, size));
12858 }
12859 }
12860 else
12861 maxlen = len;
12862
12863 if (! tree_int_cst_lt (maxlen, size))
12864 return NULL_TREE;
12865 }
12866
12867 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12868 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12869 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12870 if (!fn)
12871 return NULL_TREE;
12872
12873 return build_call_expr_loc (loc, fn, 2, dest, src);
12874 }
12875
12876 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12877 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12878 length passed as third argument. IGNORE is true if return value can be
12879 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12880
12881 tree
12882 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12883 tree len, tree size, tree maxlen, bool ignore,
12884 enum built_in_function fcode)
12885 {
12886 tree fn;
12887
12888 if (!validate_arg (dest, POINTER_TYPE)
12889 || !validate_arg (src, POINTER_TYPE)
12890 || !validate_arg (len, INTEGER_TYPE)
12891 || !validate_arg (size, INTEGER_TYPE))
12892 return NULL_TREE;
12893
12894 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12895 {
12896 /* If return value of __stpncpy_chk is ignored,
12897 optimize into __strncpy_chk. */
12898 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12899 if (fn)
12900 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12901 }
12902
12903 if (! tree_fits_uhwi_p (size))
12904 return NULL_TREE;
12905
12906 if (! integer_all_onesp (size))
12907 {
12908 if (! tree_fits_uhwi_p (len))
12909 {
12910 /* If LEN is not constant, try MAXLEN too.
12911 For MAXLEN only allow optimizing into non-_ocs function
12912 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12913 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12914 return NULL_TREE;
12915 }
12916 else
12917 maxlen = len;
12918
12919 if (tree_int_cst_lt (size, maxlen))
12920 return NULL_TREE;
12921 }
12922
12923 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12924 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12925 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12926 if (!fn)
12927 return NULL_TREE;
12928
12929 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12930 }
12931
12932 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12933 are the arguments to the call. */
12934
12935 static tree
12936 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12937 tree src, tree size)
12938 {
12939 tree fn;
12940 const char *p;
12941
12942 if (!validate_arg (dest, POINTER_TYPE)
12943 || !validate_arg (src, POINTER_TYPE)
12944 || !validate_arg (size, INTEGER_TYPE))
12945 return NULL_TREE;
12946
12947 p = c_getstr (src);
12948 /* If the SRC parameter is "", return DEST. */
12949 if (p && *p == '\0')
12950 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12951
12952 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12953 return NULL_TREE;
12954
12955 /* If __builtin_strcat_chk is used, assume strcat is available. */
12956 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12957 if (!fn)
12958 return NULL_TREE;
12959
12960 return build_call_expr_loc (loc, fn, 2, dest, src);
12961 }
12962
12963 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12964 LEN, and SIZE. */
12965
12966 static tree
12967 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12968 tree dest, tree src, tree len, tree size)
12969 {
12970 tree fn;
12971 const char *p;
12972
12973 if (!validate_arg (dest, POINTER_TYPE)
12974 || !validate_arg (src, POINTER_TYPE)
12975 || !validate_arg (size, INTEGER_TYPE)
12976 || !validate_arg (size, INTEGER_TYPE))
12977 return NULL_TREE;
12978
12979 p = c_getstr (src);
12980 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12981 if (p && *p == '\0')
12982 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12983 else if (integer_zerop (len))
12984 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12985
12986 if (! tree_fits_uhwi_p (size))
12987 return NULL_TREE;
12988
12989 if (! integer_all_onesp (size))
12990 {
12991 tree src_len = c_strlen (src, 1);
12992 if (src_len
12993 && tree_fits_uhwi_p (src_len)
12994 && tree_fits_uhwi_p (len)
12995 && ! tree_int_cst_lt (len, src_len))
12996 {
12997 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12998 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12999 if (!fn)
13000 return NULL_TREE;
13001
13002 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13003 }
13004 return NULL_TREE;
13005 }
13006
13007 /* If __builtin_strncat_chk is used, assume strncat is available. */
13008 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13009 if (!fn)
13010 return NULL_TREE;
13011
13012 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13013 }
13014
13015 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13016 Return NULL_TREE if a normal call should be emitted rather than
13017 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13018 or BUILT_IN_VSPRINTF_CHK. */
13019
13020 static tree
13021 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13022 enum built_in_function fcode)
13023 {
13024 tree dest, size, len, fn, fmt, flag;
13025 const char *fmt_str;
13026
13027 /* Verify the required arguments in the original call. */
13028 if (nargs < 4)
13029 return NULL_TREE;
13030 dest = args[0];
13031 if (!validate_arg (dest, POINTER_TYPE))
13032 return NULL_TREE;
13033 flag = args[1];
13034 if (!validate_arg (flag, INTEGER_TYPE))
13035 return NULL_TREE;
13036 size = args[2];
13037 if (!validate_arg (size, INTEGER_TYPE))
13038 return NULL_TREE;
13039 fmt = args[3];
13040 if (!validate_arg (fmt, POINTER_TYPE))
13041 return NULL_TREE;
13042
13043 if (! tree_fits_uhwi_p (size))
13044 return NULL_TREE;
13045
13046 len = NULL_TREE;
13047
13048 if (!init_target_chars ())
13049 return NULL_TREE;
13050
13051 /* Check whether the format is a literal string constant. */
13052 fmt_str = c_getstr (fmt);
13053 if (fmt_str != NULL)
13054 {
13055 /* If the format doesn't contain % args or %%, we know the size. */
13056 if (strchr (fmt_str, target_percent) == 0)
13057 {
13058 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13059 len = build_int_cstu (size_type_node, strlen (fmt_str));
13060 }
13061 /* If the format is "%s" and first ... argument is a string literal,
13062 we know the size too. */
13063 else if (fcode == BUILT_IN_SPRINTF_CHK
13064 && strcmp (fmt_str, target_percent_s) == 0)
13065 {
13066 tree arg;
13067
13068 if (nargs == 5)
13069 {
13070 arg = args[4];
13071 if (validate_arg (arg, POINTER_TYPE))
13072 {
13073 len = c_strlen (arg, 1);
13074 if (! len || ! tree_fits_uhwi_p (len))
13075 len = NULL_TREE;
13076 }
13077 }
13078 }
13079 }
13080
13081 if (! integer_all_onesp (size))
13082 {
13083 if (! len || ! tree_int_cst_lt (len, size))
13084 return NULL_TREE;
13085 }
13086
13087 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13088 or if format doesn't contain % chars or is "%s". */
13089 if (! integer_zerop (flag))
13090 {
13091 if (fmt_str == NULL)
13092 return NULL_TREE;
13093 if (strchr (fmt_str, target_percent) != NULL
13094 && strcmp (fmt_str, target_percent_s))
13095 return NULL_TREE;
13096 }
13097
13098 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13099 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13100 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13101 if (!fn)
13102 return NULL_TREE;
13103
13104 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13105 }
13106
13107 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13108 a normal call should be emitted rather than expanding the function
13109 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13110
13111 static tree
13112 fold_builtin_sprintf_chk (location_t loc, tree exp,
13113 enum built_in_function fcode)
13114 {
13115 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13116 CALL_EXPR_ARGP (exp), fcode);
13117 }
13118
13119 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13120 NULL_TREE if a normal call should be emitted rather than expanding
13121 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13122 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13123 passed as second argument. */
13124
13125 static tree
13126 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13127 tree maxlen, enum built_in_function fcode)
13128 {
13129 tree dest, size, len, fn, fmt, flag;
13130 const char *fmt_str;
13131
13132 /* Verify the required arguments in the original call. */
13133 if (nargs < 5)
13134 return NULL_TREE;
13135 dest = args[0];
13136 if (!validate_arg (dest, POINTER_TYPE))
13137 return NULL_TREE;
13138 len = args[1];
13139 if (!validate_arg (len, INTEGER_TYPE))
13140 return NULL_TREE;
13141 flag = args[2];
13142 if (!validate_arg (flag, INTEGER_TYPE))
13143 return NULL_TREE;
13144 size = args[3];
13145 if (!validate_arg (size, INTEGER_TYPE))
13146 return NULL_TREE;
13147 fmt = args[4];
13148 if (!validate_arg (fmt, POINTER_TYPE))
13149 return NULL_TREE;
13150
13151 if (! tree_fits_uhwi_p (size))
13152 return NULL_TREE;
13153
13154 if (! integer_all_onesp (size))
13155 {
13156 if (! tree_fits_uhwi_p (len))
13157 {
13158 /* If LEN is not constant, try MAXLEN too.
13159 For MAXLEN only allow optimizing into non-_ocs function
13160 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13161 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
13162 return NULL_TREE;
13163 }
13164 else
13165 maxlen = len;
13166
13167 if (tree_int_cst_lt (size, maxlen))
13168 return NULL_TREE;
13169 }
13170
13171 if (!init_target_chars ())
13172 return NULL_TREE;
13173
13174 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13175 or if format doesn't contain % chars or is "%s". */
13176 if (! integer_zerop (flag))
13177 {
13178 fmt_str = c_getstr (fmt);
13179 if (fmt_str == NULL)
13180 return NULL_TREE;
13181 if (strchr (fmt_str, target_percent) != NULL
13182 && strcmp (fmt_str, target_percent_s))
13183 return NULL_TREE;
13184 }
13185
13186 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13187 available. */
13188 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13189 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13190 if (!fn)
13191 return NULL_TREE;
13192
13193 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13194 }
13195
13196 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13197 a normal call should be emitted rather than expanding the function
13198 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13199 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13200 passed as second argument. */
13201
13202 static tree
13203 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13204 enum built_in_function fcode)
13205 {
13206 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13207 CALL_EXPR_ARGP (exp), maxlen, fcode);
13208 }
13209
13210 /* Builtins with folding operations that operate on "..." arguments
13211 need special handling; we need to store the arguments in a convenient
13212 data structure before attempting any folding. Fortunately there are
13213 only a few builtins that fall into this category. FNDECL is the
13214 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13215 result of the function call is ignored. */
13216
13217 static tree
13218 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13219 bool ignore ATTRIBUTE_UNUSED)
13220 {
13221 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13222 tree ret = NULL_TREE;
13223
13224 switch (fcode)
13225 {
13226 case BUILT_IN_SPRINTF_CHK:
13227 case BUILT_IN_VSPRINTF_CHK:
13228 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13229 break;
13230
13231 case BUILT_IN_SNPRINTF_CHK:
13232 case BUILT_IN_VSNPRINTF_CHK:
13233 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13234 break;
13235
13236 case BUILT_IN_FPCLASSIFY:
13237 ret = fold_builtin_fpclassify (loc, exp);
13238 break;
13239
13240 default:
13241 break;
13242 }
13243 if (ret)
13244 {
13245 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13246 SET_EXPR_LOCATION (ret, loc);
13247 TREE_NO_WARNING (ret) = 1;
13248 return ret;
13249 }
13250 return NULL_TREE;
13251 }
13252
13253 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13254 FMT and ARG are the arguments to the call; we don't fold cases with
13255 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13256
13257 Return NULL_TREE if no simplification was possible, otherwise return the
13258 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13259 code of the function to be simplified. */
13260
13261 static tree
13262 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13263 tree arg, bool ignore,
13264 enum built_in_function fcode)
13265 {
13266 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13267 const char *fmt_str = NULL;
13268
13269 /* If the return value is used, don't do the transformation. */
13270 if (! ignore)
13271 return NULL_TREE;
13272
13273 /* Verify the required arguments in the original call. */
13274 if (!validate_arg (fmt, POINTER_TYPE))
13275 return NULL_TREE;
13276
13277 /* Check whether the format is a literal string constant. */
13278 fmt_str = c_getstr (fmt);
13279 if (fmt_str == NULL)
13280 return NULL_TREE;
13281
13282 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13283 {
13284 /* If we're using an unlocked function, assume the other
13285 unlocked functions exist explicitly. */
13286 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13287 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13288 }
13289 else
13290 {
13291 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13292 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13293 }
13294
13295 if (!init_target_chars ())
13296 return NULL_TREE;
13297
13298 if (strcmp (fmt_str, target_percent_s) == 0
13299 || strchr (fmt_str, target_percent) == NULL)
13300 {
13301 const char *str;
13302
13303 if (strcmp (fmt_str, target_percent_s) == 0)
13304 {
13305 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13306 return NULL_TREE;
13307
13308 if (!arg || !validate_arg (arg, POINTER_TYPE))
13309 return NULL_TREE;
13310
13311 str = c_getstr (arg);
13312 if (str == NULL)
13313 return NULL_TREE;
13314 }
13315 else
13316 {
13317 /* The format specifier doesn't contain any '%' characters. */
13318 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13319 && arg)
13320 return NULL_TREE;
13321 str = fmt_str;
13322 }
13323
13324 /* If the string was "", printf does nothing. */
13325 if (str[0] == '\0')
13326 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13327
13328 /* If the string has length of 1, call putchar. */
13329 if (str[1] == '\0')
13330 {
13331 /* Given printf("c"), (where c is any one character,)
13332 convert "c"[0] to an int and pass that to the replacement
13333 function. */
13334 newarg = build_int_cst (integer_type_node, str[0]);
13335 if (fn_putchar)
13336 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13337 }
13338 else
13339 {
13340 /* If the string was "string\n", call puts("string"). */
13341 size_t len = strlen (str);
13342 if ((unsigned char)str[len - 1] == target_newline
13343 && (size_t) (int) len == len
13344 && (int) len > 0)
13345 {
13346 char *newstr;
13347 tree offset_node, string_cst;
13348
13349 /* Create a NUL-terminated string that's one char shorter
13350 than the original, stripping off the trailing '\n'. */
13351 newarg = build_string_literal (len, str);
13352 string_cst = string_constant (newarg, &offset_node);
13353 gcc_checking_assert (string_cst
13354 && (TREE_STRING_LENGTH (string_cst)
13355 == (int) len)
13356 && integer_zerop (offset_node)
13357 && (unsigned char)
13358 TREE_STRING_POINTER (string_cst)[len - 1]
13359 == target_newline);
13360 /* build_string_literal creates a new STRING_CST,
13361 modify it in place to avoid double copying. */
13362 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13363 newstr[len - 1] = '\0';
13364 if (fn_puts)
13365 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13366 }
13367 else
13368 /* We'd like to arrange to call fputs(string,stdout) here,
13369 but we need stdout and don't have a way to get it yet. */
13370 return NULL_TREE;
13371 }
13372 }
13373
13374 /* The other optimizations can be done only on the non-va_list variants. */
13375 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13376 return NULL_TREE;
13377
13378 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13379 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13380 {
13381 if (!arg || !validate_arg (arg, POINTER_TYPE))
13382 return NULL_TREE;
13383 if (fn_puts)
13384 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13385 }
13386
13387 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13388 else if (strcmp (fmt_str, target_percent_c) == 0)
13389 {
13390 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13391 return NULL_TREE;
13392 if (fn_putchar)
13393 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13394 }
13395
13396 if (!call)
13397 return NULL_TREE;
13398
13399 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13400 }
13401
13402 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13403 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13404 more than 3 arguments, and ARG may be null in the 2-argument case.
13405
13406 Return NULL_TREE if no simplification was possible, otherwise return the
13407 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13408 code of the function to be simplified. */
13409
13410 static tree
13411 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13412 tree fmt, tree arg, bool ignore,
13413 enum built_in_function fcode)
13414 {
13415 tree fn_fputc, fn_fputs, call = NULL_TREE;
13416 const char *fmt_str = NULL;
13417
13418 /* If the return value is used, don't do the transformation. */
13419 if (! ignore)
13420 return NULL_TREE;
13421
13422 /* Verify the required arguments in the original call. */
13423 if (!validate_arg (fp, POINTER_TYPE))
13424 return NULL_TREE;
13425 if (!validate_arg (fmt, POINTER_TYPE))
13426 return NULL_TREE;
13427
13428 /* Check whether the format is a literal string constant. */
13429 fmt_str = c_getstr (fmt);
13430 if (fmt_str == NULL)
13431 return NULL_TREE;
13432
13433 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13434 {
13435 /* If we're using an unlocked function, assume the other
13436 unlocked functions exist explicitly. */
13437 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13438 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13439 }
13440 else
13441 {
13442 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13443 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13444 }
13445
13446 if (!init_target_chars ())
13447 return NULL_TREE;
13448
13449 /* If the format doesn't contain % args or %%, use strcpy. */
13450 if (strchr (fmt_str, target_percent) == NULL)
13451 {
13452 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13453 && arg)
13454 return NULL_TREE;
13455
13456 /* If the format specifier was "", fprintf does nothing. */
13457 if (fmt_str[0] == '\0')
13458 {
13459 /* If FP has side-effects, just wait until gimplification is
13460 done. */
13461 if (TREE_SIDE_EFFECTS (fp))
13462 return NULL_TREE;
13463
13464 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13465 }
13466
13467 /* When "string" doesn't contain %, replace all cases of
13468 fprintf (fp, string) with fputs (string, fp). The fputs
13469 builtin will take care of special cases like length == 1. */
13470 if (fn_fputs)
13471 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13472 }
13473
13474 /* The other optimizations can be done only on the non-va_list variants. */
13475 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13476 return NULL_TREE;
13477
13478 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13479 else if (strcmp (fmt_str, target_percent_s) == 0)
13480 {
13481 if (!arg || !validate_arg (arg, POINTER_TYPE))
13482 return NULL_TREE;
13483 if (fn_fputs)
13484 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13485 }
13486
13487 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13488 else if (strcmp (fmt_str, target_percent_c) == 0)
13489 {
13490 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13491 return NULL_TREE;
13492 if (fn_fputc)
13493 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13494 }
13495
13496 if (!call)
13497 return NULL_TREE;
13498 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13499 }
13500
13501 /* Initialize format string characters in the target charset. */
13502
13503 static bool
13504 init_target_chars (void)
13505 {
13506 static bool init;
13507 if (!init)
13508 {
13509 target_newline = lang_hooks.to_target_charset ('\n');
13510 target_percent = lang_hooks.to_target_charset ('%');
13511 target_c = lang_hooks.to_target_charset ('c');
13512 target_s = lang_hooks.to_target_charset ('s');
13513 if (target_newline == 0 || target_percent == 0 || target_c == 0
13514 || target_s == 0)
13515 return false;
13516
13517 target_percent_c[0] = target_percent;
13518 target_percent_c[1] = target_c;
13519 target_percent_c[2] = '\0';
13520
13521 target_percent_s[0] = target_percent;
13522 target_percent_s[1] = target_s;
13523 target_percent_s[2] = '\0';
13524
13525 target_percent_s_newline[0] = target_percent;
13526 target_percent_s_newline[1] = target_s;
13527 target_percent_s_newline[2] = target_newline;
13528 target_percent_s_newline[3] = '\0';
13529
13530 init = true;
13531 }
13532 return true;
13533 }
13534
13535 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13536 and no overflow/underflow occurred. INEXACT is true if M was not
13537 exactly calculated. TYPE is the tree type for the result. This
13538 function assumes that you cleared the MPFR flags and then
13539 calculated M to see if anything subsequently set a flag prior to
13540 entering this function. Return NULL_TREE if any checks fail. */
13541
13542 static tree
13543 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13544 {
13545 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13546 overflow/underflow occurred. If -frounding-math, proceed iff the
13547 result of calling FUNC was exact. */
13548 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13549 && (!flag_rounding_math || !inexact))
13550 {
13551 REAL_VALUE_TYPE rr;
13552
13553 real_from_mpfr (&rr, m, type, GMP_RNDN);
13554 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13555 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13556 but the mpft_t is not, then we underflowed in the
13557 conversion. */
13558 if (real_isfinite (&rr)
13559 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13560 {
13561 REAL_VALUE_TYPE rmode;
13562
13563 real_convert (&rmode, TYPE_MODE (type), &rr);
13564 /* Proceed iff the specified mode can hold the value. */
13565 if (real_identical (&rmode, &rr))
13566 return build_real (type, rmode);
13567 }
13568 }
13569 return NULL_TREE;
13570 }
13571
13572 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13573 number and no overflow/underflow occurred. INEXACT is true if M
13574 was not exactly calculated. TYPE is the tree type for the result.
13575 This function assumes that you cleared the MPFR flags and then
13576 calculated M to see if anything subsequently set a flag prior to
13577 entering this function. Return NULL_TREE if any checks fail, if
13578 FORCE_CONVERT is true, then bypass the checks. */
13579
13580 static tree
13581 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13582 {
13583 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13584 overflow/underflow occurred. If -frounding-math, proceed iff the
13585 result of calling FUNC was exact. */
13586 if (force_convert
13587 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13588 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13589 && (!flag_rounding_math || !inexact)))
13590 {
13591 REAL_VALUE_TYPE re, im;
13592
13593 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13594 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13595 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13596 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13597 but the mpft_t is not, then we underflowed in the
13598 conversion. */
13599 if (force_convert
13600 || (real_isfinite (&re) && real_isfinite (&im)
13601 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13602 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13603 {
13604 REAL_VALUE_TYPE re_mode, im_mode;
13605
13606 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13607 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13608 /* Proceed iff the specified mode can hold the value. */
13609 if (force_convert
13610 || (real_identical (&re_mode, &re)
13611 && real_identical (&im_mode, &im)))
13612 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13613 build_real (TREE_TYPE (type), im_mode));
13614 }
13615 }
13616 return NULL_TREE;
13617 }
13618
13619 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13620 FUNC on it and return the resulting value as a tree with type TYPE.
13621 If MIN and/or MAX are not NULL, then the supplied ARG must be
13622 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13623 acceptable values, otherwise they are not. The mpfr precision is
13624 set to the precision of TYPE. We assume that function FUNC returns
13625 zero if the result could be calculated exactly within the requested
13626 precision. */
13627
13628 static tree
13629 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13630 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13631 bool inclusive)
13632 {
13633 tree result = NULL_TREE;
13634
13635 STRIP_NOPS (arg);
13636
13637 /* To proceed, MPFR must exactly represent the target floating point
13638 format, which only happens when the target base equals two. */
13639 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13640 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13641 {
13642 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13643
13644 if (real_isfinite (ra)
13645 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13646 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13647 {
13648 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13649 const int prec = fmt->p;
13650 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13651 int inexact;
13652 mpfr_t m;
13653
13654 mpfr_init2 (m, prec);
13655 mpfr_from_real (m, ra, GMP_RNDN);
13656 mpfr_clear_flags ();
13657 inexact = func (m, m, rnd);
13658 result = do_mpfr_ckconv (m, type, inexact);
13659 mpfr_clear (m);
13660 }
13661 }
13662
13663 return result;
13664 }
13665
13666 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13667 FUNC on it and return the resulting value as a tree with type TYPE.
13668 The mpfr precision is set to the precision of TYPE. We assume that
13669 function FUNC returns zero if the result could be calculated
13670 exactly within the requested precision. */
13671
13672 static tree
13673 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13674 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13675 {
13676 tree result = NULL_TREE;
13677
13678 STRIP_NOPS (arg1);
13679 STRIP_NOPS (arg2);
13680
13681 /* To proceed, MPFR must exactly represent the target floating point
13682 format, which only happens when the target base equals two. */
13683 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13684 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13685 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13686 {
13687 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13688 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13689
13690 if (real_isfinite (ra1) && real_isfinite (ra2))
13691 {
13692 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13693 const int prec = fmt->p;
13694 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13695 int inexact;
13696 mpfr_t m1, m2;
13697
13698 mpfr_inits2 (prec, m1, m2, NULL);
13699 mpfr_from_real (m1, ra1, GMP_RNDN);
13700 mpfr_from_real (m2, ra2, GMP_RNDN);
13701 mpfr_clear_flags ();
13702 inexact = func (m1, m1, m2, rnd);
13703 result = do_mpfr_ckconv (m1, type, inexact);
13704 mpfr_clears (m1, m2, NULL);
13705 }
13706 }
13707
13708 return result;
13709 }
13710
13711 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13712 FUNC on it and return the resulting value as a tree with type TYPE.
13713 The mpfr precision is set to the precision of TYPE. We assume that
13714 function FUNC returns zero if the result could be calculated
13715 exactly within the requested precision. */
13716
13717 static tree
13718 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13719 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13720 {
13721 tree result = NULL_TREE;
13722
13723 STRIP_NOPS (arg1);
13724 STRIP_NOPS (arg2);
13725 STRIP_NOPS (arg3);
13726
13727 /* To proceed, MPFR must exactly represent the target floating point
13728 format, which only happens when the target base equals two. */
13729 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13730 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13731 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13732 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13733 {
13734 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13735 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13736 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13737
13738 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13739 {
13740 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13741 const int prec = fmt->p;
13742 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13743 int inexact;
13744 mpfr_t m1, m2, m3;
13745
13746 mpfr_inits2 (prec, m1, m2, m3, NULL);
13747 mpfr_from_real (m1, ra1, GMP_RNDN);
13748 mpfr_from_real (m2, ra2, GMP_RNDN);
13749 mpfr_from_real (m3, ra3, GMP_RNDN);
13750 mpfr_clear_flags ();
13751 inexact = func (m1, m1, m2, m3, rnd);
13752 result = do_mpfr_ckconv (m1, type, inexact);
13753 mpfr_clears (m1, m2, m3, NULL);
13754 }
13755 }
13756
13757 return result;
13758 }
13759
13760 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13761 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13762 If ARG_SINP and ARG_COSP are NULL then the result is returned
13763 as a complex value.
13764 The type is taken from the type of ARG and is used for setting the
13765 precision of the calculation and results. */
13766
13767 static tree
13768 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13769 {
13770 tree const type = TREE_TYPE (arg);
13771 tree result = NULL_TREE;
13772
13773 STRIP_NOPS (arg);
13774
13775 /* To proceed, MPFR must exactly represent the target floating point
13776 format, which only happens when the target base equals two. */
13777 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13778 && TREE_CODE (arg) == REAL_CST
13779 && !TREE_OVERFLOW (arg))
13780 {
13781 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13782
13783 if (real_isfinite (ra))
13784 {
13785 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13786 const int prec = fmt->p;
13787 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13788 tree result_s, result_c;
13789 int inexact;
13790 mpfr_t m, ms, mc;
13791
13792 mpfr_inits2 (prec, m, ms, mc, NULL);
13793 mpfr_from_real (m, ra, GMP_RNDN);
13794 mpfr_clear_flags ();
13795 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13796 result_s = do_mpfr_ckconv (ms, type, inexact);
13797 result_c = do_mpfr_ckconv (mc, type, inexact);
13798 mpfr_clears (m, ms, mc, NULL);
13799 if (result_s && result_c)
13800 {
13801 /* If we are to return in a complex value do so. */
13802 if (!arg_sinp && !arg_cosp)
13803 return build_complex (build_complex_type (type),
13804 result_c, result_s);
13805
13806 /* Dereference the sin/cos pointer arguments. */
13807 arg_sinp = build_fold_indirect_ref (arg_sinp);
13808 arg_cosp = build_fold_indirect_ref (arg_cosp);
13809 /* Proceed if valid pointer type were passed in. */
13810 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13811 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13812 {
13813 /* Set the values. */
13814 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13815 result_s);
13816 TREE_SIDE_EFFECTS (result_s) = 1;
13817 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13818 result_c);
13819 TREE_SIDE_EFFECTS (result_c) = 1;
13820 /* Combine the assignments into a compound expr. */
13821 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13822 result_s, result_c));
13823 }
13824 }
13825 }
13826 }
13827 return result;
13828 }
13829
13830 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13831 two-argument mpfr order N Bessel function FUNC on them and return
13832 the resulting value as a tree with type TYPE. The mpfr precision
13833 is set to the precision of TYPE. We assume that function FUNC
13834 returns zero if the result could be calculated exactly within the
13835 requested precision. */
13836 static tree
13837 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13838 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13839 const REAL_VALUE_TYPE *min, bool inclusive)
13840 {
13841 tree result = NULL_TREE;
13842
13843 STRIP_NOPS (arg1);
13844 STRIP_NOPS (arg2);
13845
13846 /* To proceed, MPFR must exactly represent the target floating point
13847 format, which only happens when the target base equals two. */
13848 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13849 && tree_fits_shwi_p (arg1)
13850 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13851 {
13852 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13853 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13854
13855 if (n == (long)n
13856 && real_isfinite (ra)
13857 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13858 {
13859 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13860 const int prec = fmt->p;
13861 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13862 int inexact;
13863 mpfr_t m;
13864
13865 mpfr_init2 (m, prec);
13866 mpfr_from_real (m, ra, GMP_RNDN);
13867 mpfr_clear_flags ();
13868 inexact = func (m, n, m, rnd);
13869 result = do_mpfr_ckconv (m, type, inexact);
13870 mpfr_clear (m);
13871 }
13872 }
13873
13874 return result;
13875 }
13876
13877 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13878 the pointer *(ARG_QUO) and return the result. The type is taken
13879 from the type of ARG0 and is used for setting the precision of the
13880 calculation and results. */
13881
13882 static tree
13883 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13884 {
13885 tree const type = TREE_TYPE (arg0);
13886 tree result = NULL_TREE;
13887
13888 STRIP_NOPS (arg0);
13889 STRIP_NOPS (arg1);
13890
13891 /* To proceed, MPFR must exactly represent the target floating point
13892 format, which only happens when the target base equals two. */
13893 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13894 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13895 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13896 {
13897 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13898 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13899
13900 if (real_isfinite (ra0) && real_isfinite (ra1))
13901 {
13902 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13903 const int prec = fmt->p;
13904 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13905 tree result_rem;
13906 long integer_quo;
13907 mpfr_t m0, m1;
13908
13909 mpfr_inits2 (prec, m0, m1, NULL);
13910 mpfr_from_real (m0, ra0, GMP_RNDN);
13911 mpfr_from_real (m1, ra1, GMP_RNDN);
13912 mpfr_clear_flags ();
13913 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13914 /* Remquo is independent of the rounding mode, so pass
13915 inexact=0 to do_mpfr_ckconv(). */
13916 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13917 mpfr_clears (m0, m1, NULL);
13918 if (result_rem)
13919 {
13920 /* MPFR calculates quo in the host's long so it may
13921 return more bits in quo than the target int can hold
13922 if sizeof(host long) > sizeof(target int). This can
13923 happen even for native compilers in LP64 mode. In
13924 these cases, modulo the quo value with the largest
13925 number that the target int can hold while leaving one
13926 bit for the sign. */
13927 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13928 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13929
13930 /* Dereference the quo pointer argument. */
13931 arg_quo = build_fold_indirect_ref (arg_quo);
13932 /* Proceed iff a valid pointer type was passed in. */
13933 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13934 {
13935 /* Set the value. */
13936 tree result_quo
13937 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13938 build_int_cst (TREE_TYPE (arg_quo),
13939 integer_quo));
13940 TREE_SIDE_EFFECTS (result_quo) = 1;
13941 /* Combine the quo assignment with the rem. */
13942 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13943 result_quo, result_rem));
13944 }
13945 }
13946 }
13947 }
13948 return result;
13949 }
13950
13951 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13952 resulting value as a tree with type TYPE. The mpfr precision is
13953 set to the precision of TYPE. We assume that this mpfr function
13954 returns zero if the result could be calculated exactly within the
13955 requested precision. In addition, the integer pointer represented
13956 by ARG_SG will be dereferenced and set to the appropriate signgam
13957 (-1,1) value. */
13958
13959 static tree
13960 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13961 {
13962 tree result = NULL_TREE;
13963
13964 STRIP_NOPS (arg);
13965
13966 /* To proceed, MPFR must exactly represent the target floating point
13967 format, which only happens when the target base equals two. Also
13968 verify ARG is a constant and that ARG_SG is an int pointer. */
13969 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13970 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13971 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13972 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13973 {
13974 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13975
13976 /* In addition to NaN and Inf, the argument cannot be zero or a
13977 negative integer. */
13978 if (real_isfinite (ra)
13979 && ra->cl != rvc_zero
13980 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13981 {
13982 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13983 const int prec = fmt->p;
13984 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13985 int inexact, sg;
13986 mpfr_t m;
13987 tree result_lg;
13988
13989 mpfr_init2 (m, prec);
13990 mpfr_from_real (m, ra, GMP_RNDN);
13991 mpfr_clear_flags ();
13992 inexact = mpfr_lgamma (m, &sg, m, rnd);
13993 result_lg = do_mpfr_ckconv (m, type, inexact);
13994 mpfr_clear (m);
13995 if (result_lg)
13996 {
13997 tree result_sg;
13998
13999 /* Dereference the arg_sg pointer argument. */
14000 arg_sg = build_fold_indirect_ref (arg_sg);
14001 /* Assign the signgam value into *arg_sg. */
14002 result_sg = fold_build2 (MODIFY_EXPR,
14003 TREE_TYPE (arg_sg), arg_sg,
14004 build_int_cst (TREE_TYPE (arg_sg), sg));
14005 TREE_SIDE_EFFECTS (result_sg) = 1;
14006 /* Combine the signgam assignment with the lgamma result. */
14007 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14008 result_sg, result_lg));
14009 }
14010 }
14011 }
14012
14013 return result;
14014 }
14015
14016 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14017 function FUNC on it and return the resulting value as a tree with
14018 type TYPE. The mpfr precision is set to the precision of TYPE. We
14019 assume that function FUNC returns zero if the result could be
14020 calculated exactly within the requested precision. */
14021
14022 static tree
14023 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14024 {
14025 tree result = NULL_TREE;
14026
14027 STRIP_NOPS (arg);
14028
14029 /* To proceed, MPFR must exactly represent the target floating point
14030 format, which only happens when the target base equals two. */
14031 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14032 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14033 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14034 {
14035 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14036 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14037
14038 if (real_isfinite (re) && real_isfinite (im))
14039 {
14040 const struct real_format *const fmt =
14041 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14042 const int prec = fmt->p;
14043 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14044 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14045 int inexact;
14046 mpc_t m;
14047
14048 mpc_init2 (m, prec);
14049 mpfr_from_real (mpc_realref (m), re, rnd);
14050 mpfr_from_real (mpc_imagref (m), im, rnd);
14051 mpfr_clear_flags ();
14052 inexact = func (m, m, crnd);
14053 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14054 mpc_clear (m);
14055 }
14056 }
14057
14058 return result;
14059 }
14060
14061 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14062 mpc function FUNC on it and return the resulting value as a tree
14063 with type TYPE. The mpfr precision is set to the precision of
14064 TYPE. We assume that function FUNC returns zero if the result
14065 could be calculated exactly within the requested precision. If
14066 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14067 in the arguments and/or results. */
14068
14069 tree
14070 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14071 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14072 {
14073 tree result = NULL_TREE;
14074
14075 STRIP_NOPS (arg0);
14076 STRIP_NOPS (arg1);
14077
14078 /* To proceed, MPFR must exactly represent the target floating point
14079 format, which only happens when the target base equals two. */
14080 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14081 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14082 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14083 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14084 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14085 {
14086 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14087 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14088 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14089 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14090
14091 if (do_nonfinite
14092 || (real_isfinite (re0) && real_isfinite (im0)
14093 && real_isfinite (re1) && real_isfinite (im1)))
14094 {
14095 const struct real_format *const fmt =
14096 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14097 const int prec = fmt->p;
14098 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14099 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14100 int inexact;
14101 mpc_t m0, m1;
14102
14103 mpc_init2 (m0, prec);
14104 mpc_init2 (m1, prec);
14105 mpfr_from_real (mpc_realref (m0), re0, rnd);
14106 mpfr_from_real (mpc_imagref (m0), im0, rnd);
14107 mpfr_from_real (mpc_realref (m1), re1, rnd);
14108 mpfr_from_real (mpc_imagref (m1), im1, rnd);
14109 mpfr_clear_flags ();
14110 inexact = func (m0, m0, m1, crnd);
14111 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14112 mpc_clear (m0);
14113 mpc_clear (m1);
14114 }
14115 }
14116
14117 return result;
14118 }
14119
14120 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14121 a normal call should be emitted rather than expanding the function
14122 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14123
14124 static tree
14125 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14126 {
14127 int nargs = gimple_call_num_args (stmt);
14128
14129 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14130 (nargs > 0
14131 ? gimple_call_arg_ptr (stmt, 0)
14132 : &error_mark_node), fcode);
14133 }
14134
14135 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14136 a normal call should be emitted rather than expanding the function
14137 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14138 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14139 passed as second argument. */
14140
14141 tree
14142 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14143 enum built_in_function fcode)
14144 {
14145 int nargs = gimple_call_num_args (stmt);
14146
14147 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14148 (nargs > 0
14149 ? gimple_call_arg_ptr (stmt, 0)
14150 : &error_mark_node), maxlen, fcode);
14151 }
14152
14153 /* Builtins with folding operations that operate on "..." arguments
14154 need special handling; we need to store the arguments in a convenient
14155 data structure before attempting any folding. Fortunately there are
14156 only a few builtins that fall into this category. FNDECL is the
14157 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14158 result of the function call is ignored. */
14159
14160 static tree
14161 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14162 bool ignore ATTRIBUTE_UNUSED)
14163 {
14164 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14165 tree ret = NULL_TREE;
14166
14167 switch (fcode)
14168 {
14169 case BUILT_IN_SPRINTF_CHK:
14170 case BUILT_IN_VSPRINTF_CHK:
14171 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14172 break;
14173
14174 case BUILT_IN_SNPRINTF_CHK:
14175 case BUILT_IN_VSNPRINTF_CHK:
14176 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14177
14178 default:
14179 break;
14180 }
14181 if (ret)
14182 {
14183 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14184 TREE_NO_WARNING (ret) = 1;
14185 return ret;
14186 }
14187 return NULL_TREE;
14188 }
14189
14190 /* A wrapper function for builtin folding that prevents warnings for
14191 "statement without effect" and the like, caused by removing the
14192 call node earlier than the warning is generated. */
14193
14194 tree
14195 fold_call_stmt (gimple stmt, bool ignore)
14196 {
14197 tree ret = NULL_TREE;
14198 tree fndecl = gimple_call_fndecl (stmt);
14199 location_t loc = gimple_location (stmt);
14200 if (fndecl
14201 && TREE_CODE (fndecl) == FUNCTION_DECL
14202 && DECL_BUILT_IN (fndecl)
14203 && !gimple_call_va_arg_pack_p (stmt))
14204 {
14205 int nargs = gimple_call_num_args (stmt);
14206 tree *args = (nargs > 0
14207 ? gimple_call_arg_ptr (stmt, 0)
14208 : &error_mark_node);
14209
14210 if (avoid_folding_inline_builtin (fndecl))
14211 return NULL_TREE;
14212 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14213 {
14214 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14215 }
14216 else
14217 {
14218 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14219 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14220 if (!ret)
14221 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14222 if (ret)
14223 {
14224 /* Propagate location information from original call to
14225 expansion of builtin. Otherwise things like
14226 maybe_emit_chk_warning, that operate on the expansion
14227 of a builtin, will use the wrong location information. */
14228 if (gimple_has_location (stmt))
14229 {
14230 tree realret = ret;
14231 if (TREE_CODE (ret) == NOP_EXPR)
14232 realret = TREE_OPERAND (ret, 0);
14233 if (CAN_HAVE_LOCATION_P (realret)
14234 && !EXPR_HAS_LOCATION (realret))
14235 SET_EXPR_LOCATION (realret, loc);
14236 return realret;
14237 }
14238 return ret;
14239 }
14240 }
14241 }
14242 return NULL_TREE;
14243 }
14244
14245 /* Look up the function in builtin_decl that corresponds to DECL
14246 and set ASMSPEC as its user assembler name. DECL must be a
14247 function decl that declares a builtin. */
14248
14249 void
14250 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14251 {
14252 tree builtin;
14253 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14254 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14255 && asmspec != 0);
14256
14257 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14258 set_user_assembler_name (builtin, asmspec);
14259 switch (DECL_FUNCTION_CODE (decl))
14260 {
14261 case BUILT_IN_MEMCPY:
14262 init_block_move_fn (asmspec);
14263 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14264 break;
14265 case BUILT_IN_MEMSET:
14266 init_block_clear_fn (asmspec);
14267 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14268 break;
14269 case BUILT_IN_MEMMOVE:
14270 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14271 break;
14272 case BUILT_IN_MEMCMP:
14273 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14274 break;
14275 case BUILT_IN_ABORT:
14276 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14277 break;
14278 case BUILT_IN_FFS:
14279 if (INT_TYPE_SIZE < BITS_PER_WORD)
14280 {
14281 set_user_assembler_libfunc ("ffs", asmspec);
14282 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14283 MODE_INT, 0), "ffs");
14284 }
14285 break;
14286 default:
14287 break;
14288 }
14289 }
14290
14291 /* Return true if DECL is a builtin that expands to a constant or similarly
14292 simple code. */
14293 bool
14294 is_simple_builtin (tree decl)
14295 {
14296 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14297 switch (DECL_FUNCTION_CODE (decl))
14298 {
14299 /* Builtins that expand to constants. */
14300 case BUILT_IN_CONSTANT_P:
14301 case BUILT_IN_EXPECT:
14302 case BUILT_IN_OBJECT_SIZE:
14303 case BUILT_IN_UNREACHABLE:
14304 /* Simple register moves or loads from stack. */
14305 case BUILT_IN_ASSUME_ALIGNED:
14306 case BUILT_IN_RETURN_ADDRESS:
14307 case BUILT_IN_EXTRACT_RETURN_ADDR:
14308 case BUILT_IN_FROB_RETURN_ADDR:
14309 case BUILT_IN_RETURN:
14310 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14311 case BUILT_IN_FRAME_ADDRESS:
14312 case BUILT_IN_VA_END:
14313 case BUILT_IN_STACK_SAVE:
14314 case BUILT_IN_STACK_RESTORE:
14315 /* Exception state returns or moves registers around. */
14316 case BUILT_IN_EH_FILTER:
14317 case BUILT_IN_EH_POINTER:
14318 case BUILT_IN_EH_COPY_VALUES:
14319 return true;
14320
14321 default:
14322 return false;
14323 }
14324
14325 return false;
14326 }
14327
14328 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14329 most probably expanded inline into reasonably simple code. This is a
14330 superset of is_simple_builtin. */
14331 bool
14332 is_inexpensive_builtin (tree decl)
14333 {
14334 if (!decl)
14335 return false;
14336 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14337 return true;
14338 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14339 switch (DECL_FUNCTION_CODE (decl))
14340 {
14341 case BUILT_IN_ABS:
14342 case BUILT_IN_ALLOCA:
14343 case BUILT_IN_ALLOCA_WITH_ALIGN:
14344 case BUILT_IN_BSWAP16:
14345 case BUILT_IN_BSWAP32:
14346 case BUILT_IN_BSWAP64:
14347 case BUILT_IN_CLZ:
14348 case BUILT_IN_CLZIMAX:
14349 case BUILT_IN_CLZL:
14350 case BUILT_IN_CLZLL:
14351 case BUILT_IN_CTZ:
14352 case BUILT_IN_CTZIMAX:
14353 case BUILT_IN_CTZL:
14354 case BUILT_IN_CTZLL:
14355 case BUILT_IN_FFS:
14356 case BUILT_IN_FFSIMAX:
14357 case BUILT_IN_FFSL:
14358 case BUILT_IN_FFSLL:
14359 case BUILT_IN_IMAXABS:
14360 case BUILT_IN_FINITE:
14361 case BUILT_IN_FINITEF:
14362 case BUILT_IN_FINITEL:
14363 case BUILT_IN_FINITED32:
14364 case BUILT_IN_FINITED64:
14365 case BUILT_IN_FINITED128:
14366 case BUILT_IN_FPCLASSIFY:
14367 case BUILT_IN_ISFINITE:
14368 case BUILT_IN_ISINF_SIGN:
14369 case BUILT_IN_ISINF:
14370 case BUILT_IN_ISINFF:
14371 case BUILT_IN_ISINFL:
14372 case BUILT_IN_ISINFD32:
14373 case BUILT_IN_ISINFD64:
14374 case BUILT_IN_ISINFD128:
14375 case BUILT_IN_ISNAN:
14376 case BUILT_IN_ISNANF:
14377 case BUILT_IN_ISNANL:
14378 case BUILT_IN_ISNAND32:
14379 case BUILT_IN_ISNAND64:
14380 case BUILT_IN_ISNAND128:
14381 case BUILT_IN_ISNORMAL:
14382 case BUILT_IN_ISGREATER:
14383 case BUILT_IN_ISGREATEREQUAL:
14384 case BUILT_IN_ISLESS:
14385 case BUILT_IN_ISLESSEQUAL:
14386 case BUILT_IN_ISLESSGREATER:
14387 case BUILT_IN_ISUNORDERED:
14388 case BUILT_IN_VA_ARG_PACK:
14389 case BUILT_IN_VA_ARG_PACK_LEN:
14390 case BUILT_IN_VA_COPY:
14391 case BUILT_IN_TRAP:
14392 case BUILT_IN_SAVEREGS:
14393 case BUILT_IN_POPCOUNTL:
14394 case BUILT_IN_POPCOUNTLL:
14395 case BUILT_IN_POPCOUNTIMAX:
14396 case BUILT_IN_POPCOUNT:
14397 case BUILT_IN_PARITYL:
14398 case BUILT_IN_PARITYLL:
14399 case BUILT_IN_PARITYIMAX:
14400 case BUILT_IN_PARITY:
14401 case BUILT_IN_LABS:
14402 case BUILT_IN_LLABS:
14403 case BUILT_IN_PREFETCH:
14404 return true;
14405
14406 default:
14407 return is_simple_builtin (decl);
14408 }
14409
14410 return false;
14411 }