re PR c++/60272 (atomic<>::compare_exchange_weak has spurious store and can cause...
[gcc.git] / gcc / builtins.c
1 /* Expand builtin functions.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "varasm.h"
31 #include "tree-object-size.h"
32 #include "realmpfr.h"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "flags.h"
40 #include "regs.h"
41 #include "hard-reg-set.h"
42 #include "except.h"
43 #include "function.h"
44 #include "insn-config.h"
45 #include "expr.h"
46 #include "optabs.h"
47 #include "libfuncs.h"
48 #include "recog.h"
49 #include "output.h"
50 #include "typeclass.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "target.h"
54 #include "langhooks.h"
55 #include "tree-ssanames.h"
56 #include "tree-dfa.h"
57 #include "value-prof.h"
58 #include "diagnostic-core.h"
59 #include "builtins.h"
60 #include "ubsan.h"
61 #include "cilk.h"
62
63
64 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
65
66 struct target_builtins default_target_builtins;
67 #if SWITCHABLE_TARGET
68 struct target_builtins *this_target_builtins = &default_target_builtins;
69 #endif
70
71 /* Define the names of the builtin function types and codes. */
72 const char *const built_in_class_names[BUILT_IN_LAST]
73 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
74
75 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
76 const char * built_in_names[(int) END_BUILTINS] =
77 {
78 #include "builtins.def"
79 };
80 #undef DEF_BUILTIN
81
82 /* Setup an array of _DECL trees, make sure each element is
83 initialized to NULL_TREE. */
84 builtin_info_type builtin_info;
85
86 /* Non-zero if __builtin_constant_p should be folded right away. */
87 bool force_folding_builtin_constant_p;
88
89 static const char *c_getstr (tree);
90 static rtx c_readstr (const char *, enum machine_mode);
91 static int target_char_cast (tree, char *);
92 static rtx get_memory_rtx (tree, tree);
93 static int apply_args_size (void);
94 static int apply_result_size (void);
95 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
96 static rtx result_vector (int, rtx);
97 #endif
98 static void expand_builtin_update_setjmp_buf (rtx);
99 static void expand_builtin_prefetch (tree);
100 static rtx expand_builtin_apply_args (void);
101 static rtx expand_builtin_apply_args_1 (void);
102 static rtx expand_builtin_apply (rtx, rtx, rtx);
103 static void expand_builtin_return (rtx);
104 static enum type_class type_to_class (tree);
105 static rtx expand_builtin_classify_type (tree);
106 static void expand_errno_check (tree, rtx);
107 static rtx expand_builtin_mathfn (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
109 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
110 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
111 static rtx expand_builtin_interclass_mathfn (tree, rtx);
112 static rtx expand_builtin_sincos (tree);
113 static rtx expand_builtin_cexpi (tree, rtx);
114 static rtx expand_builtin_int_roundingfn (tree, rtx);
115 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
116 static rtx expand_builtin_next_arg (void);
117 static rtx expand_builtin_va_start (tree);
118 static rtx expand_builtin_va_end (tree);
119 static rtx expand_builtin_va_copy (tree);
120 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strcmp (tree, rtx);
122 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
123 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memcpy (tree, rtx);
125 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
127 enum machine_mode, int);
128 static rtx expand_builtin_strcpy (tree, rtx);
129 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
130 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_strncpy (tree, rtx);
132 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
133 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
135 static rtx expand_builtin_bzero (tree);
136 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_alloca (tree, bool);
138 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
139 static rtx expand_builtin_frame_address (tree, tree);
140 static tree stabilize_va_list_loc (location_t, tree, int);
141 static rtx expand_builtin_expect (tree, rtx);
142 static tree fold_builtin_constant_p (tree);
143 static tree fold_builtin_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_strncat (location_t, tree, tree, tree);
195 static tree fold_builtin_strspn (location_t, tree, tree);
196 static tree fold_builtin_strcspn (location_t, tree, tree);
197 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
199
200 static rtx expand_builtin_object_size (tree);
201 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
202 enum built_in_function);
203 static void maybe_emit_chk_warning (tree, enum built_in_function);
204 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
205 static void maybe_emit_free_warning (tree);
206 static tree fold_builtin_object_size (tree, tree);
207 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
208 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
209 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
210 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
211 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
212 enum built_in_function);
213 static bool init_target_chars (void);
214
215 static unsigned HOST_WIDE_INT target_newline;
216 static unsigned HOST_WIDE_INT target_percent;
217 static unsigned HOST_WIDE_INT target_c;
218 static unsigned HOST_WIDE_INT target_s;
219 static char target_percent_c[3];
220 static char target_percent_s[3];
221 static char target_percent_s_newline[4];
222 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_arg2 (tree, tree, tree,
225 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
226 static tree do_mpfr_arg3 (tree, tree, tree, tree,
227 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
228 static tree do_mpfr_sincos (tree, tree, tree);
229 static tree do_mpfr_bessel_n (tree, tree, tree,
230 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
231 const REAL_VALUE_TYPE *, bool);
232 static tree do_mpfr_remquo (tree, tree, tree);
233 static tree do_mpfr_lgamma_r (tree, tree, tree);
234 static void expand_builtin_sync_synchronize (void);
235
236 /* Return true if NAME starts with __builtin_ or __sync_. */
237
238 static bool
239 is_builtin_name (const char *name)
240 {
241 if (strncmp (name, "__builtin_", 10) == 0)
242 return true;
243 if (strncmp (name, "__sync_", 7) == 0)
244 return true;
245 if (strncmp (name, "__atomic_", 9) == 0)
246 return true;
247 if (flag_cilkplus
248 && (!strcmp (name, "__cilkrts_detach")
249 || !strcmp (name, "__cilkrts_pop_frame")))
250 return true;
251 return false;
252 }
253
254
255 /* Return true if DECL is a function symbol representing a built-in. */
256
257 bool
258 is_builtin_fn (tree decl)
259 {
260 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
261 }
262
263 /* By default we assume that c99 functions are present at the runtime,
264 but sincos is not. */
265 bool
266 default_libc_has_function (enum function_class fn_class)
267 {
268 if (fn_class == function_c94
269 || fn_class == function_c99_misc
270 || fn_class == function_c99_math_complex)
271 return true;
272
273 return false;
274 }
275
276 bool
277 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
278 {
279 return true;
280 }
281
282 bool
283 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
284 {
285 return false;
286 }
287
288 /* Return true if NODE should be considered for inline expansion regardless
289 of the optimization level. This means whenever a function is invoked with
290 its "internal" name, which normally contains the prefix "__builtin". */
291
292 static bool
293 called_as_built_in (tree node)
294 {
295 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
296 we want the name used to call the function, not the name it
297 will have. */
298 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
299 return is_builtin_name (name);
300 }
301
302 /* Compute values M and N such that M divides (address of EXP - N) and such
303 that N < M. If these numbers can be determined, store M in alignp and N in
304 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
305 *alignp and any bit-offset to *bitposp.
306
307 Note that the address (and thus the alignment) computed here is based
308 on the address to which a symbol resolves, whereas DECL_ALIGN is based
309 on the address at which an object is actually located. These two
310 addresses are not always the same. For example, on ARM targets,
311 the address &foo of a Thumb function foo() has the lowest bit set,
312 whereas foo() itself starts on an even address.
313
314 If ADDR_P is true we are taking the address of the memory reference EXP
315 and thus cannot rely on the access taking place. */
316
317 static bool
318 get_object_alignment_2 (tree exp, unsigned int *alignp,
319 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
320 {
321 HOST_WIDE_INT bitsize, bitpos;
322 tree offset;
323 enum machine_mode mode;
324 int unsignedp, volatilep;
325 unsigned int align = BITS_PER_UNIT;
326 bool known_alignment = false;
327
328 /* Get the innermost object and the constant (bitpos) and possibly
329 variable (offset) offset of the access. */
330 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
331 &mode, &unsignedp, &volatilep, true);
332
333 /* Extract alignment information from the innermost object and
334 possibly adjust bitpos and offset. */
335 if (TREE_CODE (exp) == FUNCTION_DECL)
336 {
337 /* Function addresses can encode extra information besides their
338 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
339 allows the low bit to be used as a virtual bit, we know
340 that the address itself must be at least 2-byte aligned. */
341 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
342 align = 2 * BITS_PER_UNIT;
343 }
344 else if (TREE_CODE (exp) == LABEL_DECL)
345 ;
346 else if (TREE_CODE (exp) == CONST_DECL)
347 {
348 /* The alignment of a CONST_DECL is determined by its initializer. */
349 exp = DECL_INITIAL (exp);
350 align = TYPE_ALIGN (TREE_TYPE (exp));
351 #ifdef CONSTANT_ALIGNMENT
352 if (CONSTANT_CLASS_P (exp))
353 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
354 #endif
355 known_alignment = true;
356 }
357 else if (DECL_P (exp))
358 {
359 align = DECL_ALIGN (exp);
360 known_alignment = true;
361 }
362 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
363 {
364 align = TYPE_ALIGN (TREE_TYPE (exp));
365 }
366 else if (TREE_CODE (exp) == INDIRECT_REF
367 || TREE_CODE (exp) == MEM_REF
368 || TREE_CODE (exp) == TARGET_MEM_REF)
369 {
370 tree addr = TREE_OPERAND (exp, 0);
371 unsigned ptr_align;
372 unsigned HOST_WIDE_INT ptr_bitpos;
373
374 if (TREE_CODE (addr) == BIT_AND_EXPR
375 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
376 {
377 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
378 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
379 align *= BITS_PER_UNIT;
380 addr = TREE_OPERAND (addr, 0);
381 }
382
383 known_alignment
384 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
385 align = MAX (ptr_align, align);
386
387 /* The alignment of the pointer operand in a TARGET_MEM_REF
388 has to take the variable offset parts into account. */
389 if (TREE_CODE (exp) == TARGET_MEM_REF)
390 {
391 if (TMR_INDEX (exp))
392 {
393 unsigned HOST_WIDE_INT step = 1;
394 if (TMR_STEP (exp))
395 step = TREE_INT_CST_LOW (TMR_STEP (exp));
396 align = MIN (align, (step & -step) * BITS_PER_UNIT);
397 }
398 if (TMR_INDEX2 (exp))
399 align = BITS_PER_UNIT;
400 known_alignment = false;
401 }
402
403 /* When EXP is an actual memory reference then we can use
404 TYPE_ALIGN of a pointer indirection to derive alignment.
405 Do so only if get_pointer_alignment_1 did not reveal absolute
406 alignment knowledge and if using that alignment would
407 improve the situation. */
408 if (!addr_p && !known_alignment
409 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
410 align = TYPE_ALIGN (TREE_TYPE (exp));
411 else
412 {
413 /* Else adjust bitpos accordingly. */
414 bitpos += ptr_bitpos;
415 if (TREE_CODE (exp) == MEM_REF
416 || TREE_CODE (exp) == TARGET_MEM_REF)
417 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
418 }
419 }
420 else if (TREE_CODE (exp) == STRING_CST)
421 {
422 /* STRING_CST are the only constant objects we allow to be not
423 wrapped inside a CONST_DECL. */
424 align = TYPE_ALIGN (TREE_TYPE (exp));
425 #ifdef CONSTANT_ALIGNMENT
426 if (CONSTANT_CLASS_P (exp))
427 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
428 #endif
429 known_alignment = true;
430 }
431
432 /* If there is a non-constant offset part extract the maximum
433 alignment that can prevail. */
434 if (offset)
435 {
436 unsigned int trailing_zeros = tree_ctz (offset);
437 if (trailing_zeros < HOST_BITS_PER_INT)
438 {
439 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
440 if (inner)
441 align = MIN (align, inner);
442 }
443 }
444
445 *alignp = align;
446 *bitposp = bitpos & (*alignp - 1);
447 return known_alignment;
448 }
449
450 /* For a memory reference expression EXP compute values M and N such that M
451 divides (&EXP - N) and such that N < M. If these numbers can be determined,
452 store M in alignp and N in *BITPOSP and return true. Otherwise return false
453 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
454
455 bool
456 get_object_alignment_1 (tree exp, unsigned int *alignp,
457 unsigned HOST_WIDE_INT *bitposp)
458 {
459 return get_object_alignment_2 (exp, alignp, bitposp, false);
460 }
461
462 /* Return the alignment in bits of EXP, an object. */
463
464 unsigned int
465 get_object_alignment (tree exp)
466 {
467 unsigned HOST_WIDE_INT bitpos = 0;
468 unsigned int align;
469
470 get_object_alignment_1 (exp, &align, &bitpos);
471
472 /* align and bitpos now specify known low bits of the pointer.
473 ptr & (align - 1) == bitpos. */
474
475 if (bitpos != 0)
476 align = (bitpos & -bitpos);
477 return align;
478 }
479
480 /* For a pointer valued expression EXP compute values M and N such that M
481 divides (EXP - N) and such that N < M. If these numbers can be determined,
482 store M in alignp and N in *BITPOSP and return true. Return false if
483 the results are just a conservative approximation.
484
485 If EXP is not a pointer, false is returned too. */
486
487 bool
488 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
489 unsigned HOST_WIDE_INT *bitposp)
490 {
491 STRIP_NOPS (exp);
492
493 if (TREE_CODE (exp) == ADDR_EXPR)
494 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
495 alignp, bitposp, true);
496 else if (TREE_CODE (exp) == SSA_NAME
497 && POINTER_TYPE_P (TREE_TYPE (exp)))
498 {
499 unsigned int ptr_align, ptr_misalign;
500 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
501
502 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
503 {
504 *bitposp = ptr_misalign * BITS_PER_UNIT;
505 *alignp = ptr_align * BITS_PER_UNIT;
506 /* We cannot really tell whether this result is an approximation. */
507 return true;
508 }
509 else
510 {
511 *bitposp = 0;
512 *alignp = BITS_PER_UNIT;
513 return false;
514 }
515 }
516 else if (TREE_CODE (exp) == INTEGER_CST)
517 {
518 *alignp = BIGGEST_ALIGNMENT;
519 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
520 & (BIGGEST_ALIGNMENT - 1));
521 return true;
522 }
523
524 *bitposp = 0;
525 *alignp = BITS_PER_UNIT;
526 return false;
527 }
528
529 /* Return the alignment in bits of EXP, a pointer valued expression.
530 The alignment returned is, by default, the alignment of the thing that
531 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
532
533 Otherwise, look at the expression to see if we can do better, i.e., if the
534 expression is actually pointing at an object whose alignment is tighter. */
535
536 unsigned int
537 get_pointer_alignment (tree exp)
538 {
539 unsigned HOST_WIDE_INT bitpos = 0;
540 unsigned int align;
541
542 get_pointer_alignment_1 (exp, &align, &bitpos);
543
544 /* align and bitpos now specify known low bits of the pointer.
545 ptr & (align - 1) == bitpos. */
546
547 if (bitpos != 0)
548 align = (bitpos & -bitpos);
549
550 return align;
551 }
552
553 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
554 way, because it could contain a zero byte in the middle.
555 TREE_STRING_LENGTH is the size of the character array, not the string.
556
557 ONLY_VALUE should be nonzero if the result is not going to be emitted
558 into the instruction stream and zero if it is going to be expanded.
559 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
560 is returned, otherwise NULL, since
561 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
562 evaluate the side-effects.
563
564 The value returned is of type `ssizetype'.
565
566 Unfortunately, string_constant can't access the values of const char
567 arrays with initializers, so neither can we do so here. */
568
569 tree
570 c_strlen (tree src, int only_value)
571 {
572 tree offset_node;
573 HOST_WIDE_INT offset;
574 int max;
575 const char *ptr;
576 location_t loc;
577
578 STRIP_NOPS (src);
579 if (TREE_CODE (src) == COND_EXPR
580 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
581 {
582 tree len1, len2;
583
584 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
585 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
586 if (tree_int_cst_equal (len1, len2))
587 return len1;
588 }
589
590 if (TREE_CODE (src) == COMPOUND_EXPR
591 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 return c_strlen (TREE_OPERAND (src, 1), only_value);
593
594 loc = EXPR_LOC_OR_LOC (src, input_location);
595
596 src = string_constant (src, &offset_node);
597 if (src == 0)
598 return NULL_TREE;
599
600 max = TREE_STRING_LENGTH (src) - 1;
601 ptr = TREE_STRING_POINTER (src);
602
603 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
604 {
605 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
606 compute the offset to the following null if we don't know where to
607 start searching for it. */
608 int i;
609
610 for (i = 0; i < max; i++)
611 if (ptr[i] == 0)
612 return NULL_TREE;
613
614 /* We don't know the starting offset, but we do know that the string
615 has no internal zero bytes. We can assume that the offset falls
616 within the bounds of the string; otherwise, the programmer deserves
617 what he gets. Subtract the offset from the length of the string,
618 and return that. This would perhaps not be valid if we were dealing
619 with named arrays in addition to literal string constants. */
620
621 return size_diffop_loc (loc, size_int (max), offset_node);
622 }
623
624 /* We have a known offset into the string. Start searching there for
625 a null character if we can represent it as a single HOST_WIDE_INT. */
626 if (offset_node == 0)
627 offset = 0;
628 else if (! tree_fits_shwi_p (offset_node))
629 offset = -1;
630 else
631 offset = tree_to_shwi (offset_node);
632
633 /* If the offset is known to be out of bounds, warn, and call strlen at
634 runtime. */
635 if (offset < 0 || offset > max)
636 {
637 /* Suppress multiple warnings for propagated constant strings. */
638 if (! TREE_NO_WARNING (src))
639 {
640 warning_at (loc, 0, "offset outside bounds of constant string");
641 TREE_NO_WARNING (src) = 1;
642 }
643 return NULL_TREE;
644 }
645
646 /* Use strlen to search for the first zero byte. Since any strings
647 constructed with build_string will have nulls appended, we win even
648 if we get handed something like (char[4])"abcd".
649
650 Since OFFSET is our starting index into the string, no further
651 calculation is needed. */
652 return ssize_int (strlen (ptr + offset));
653 }
654
655 /* Return a char pointer for a C string if it is a string constant
656 or sum of string constant and integer constant. */
657
658 static const char *
659 c_getstr (tree src)
660 {
661 tree offset_node;
662
663 src = string_constant (src, &offset_node);
664 if (src == 0)
665 return 0;
666
667 if (offset_node == 0)
668 return TREE_STRING_POINTER (src);
669 else if (!tree_fits_uhwi_p (offset_node)
670 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
671 return 0;
672
673 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
674 }
675
676 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
677 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
678
679 static rtx
680 c_readstr (const char *str, enum machine_mode mode)
681 {
682 HOST_WIDE_INT c[2];
683 HOST_WIDE_INT ch;
684 unsigned int i, j;
685
686 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
687
688 c[0] = 0;
689 c[1] = 0;
690 ch = 1;
691 for (i = 0; i < GET_MODE_SIZE (mode); i++)
692 {
693 j = i;
694 if (WORDS_BIG_ENDIAN)
695 j = GET_MODE_SIZE (mode) - i - 1;
696 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
697 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
698 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
699 j *= BITS_PER_UNIT;
700 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
701
702 if (ch)
703 ch = (unsigned char) str[i];
704 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
705 }
706 return immed_double_const (c[0], c[1], mode);
707 }
708
709 /* Cast a target constant CST to target CHAR and if that value fits into
710 host char type, return zero and put that value into variable pointed to by
711 P. */
712
713 static int
714 target_char_cast (tree cst, char *p)
715 {
716 unsigned HOST_WIDE_INT val, hostval;
717
718 if (TREE_CODE (cst) != INTEGER_CST
719 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
720 return 1;
721
722 val = TREE_INT_CST_LOW (cst);
723 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
724 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
725
726 hostval = val;
727 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
728 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
729
730 if (val != hostval)
731 return 1;
732
733 *p = hostval;
734 return 0;
735 }
736
737 /* Similar to save_expr, but assumes that arbitrary code is not executed
738 in between the multiple evaluations. In particular, we assume that a
739 non-addressable local variable will not be modified. */
740
741 static tree
742 builtin_save_expr (tree exp)
743 {
744 if (TREE_CODE (exp) == SSA_NAME
745 || (TREE_ADDRESSABLE (exp) == 0
746 && (TREE_CODE (exp) == PARM_DECL
747 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
748 return exp;
749
750 return save_expr (exp);
751 }
752
753 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
754 times to get the address of either a higher stack frame, or a return
755 address located within it (depending on FNDECL_CODE). */
756
757 static rtx
758 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
759 {
760 int i;
761
762 #ifdef INITIAL_FRAME_ADDRESS_RTX
763 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
764 #else
765 rtx tem;
766
767 /* For a zero count with __builtin_return_address, we don't care what
768 frame address we return, because target-specific definitions will
769 override us. Therefore frame pointer elimination is OK, and using
770 the soft frame pointer is OK.
771
772 For a nonzero count, or a zero count with __builtin_frame_address,
773 we require a stable offset from the current frame pointer to the
774 previous one, so we must use the hard frame pointer, and
775 we must disable frame pointer elimination. */
776 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
777 tem = frame_pointer_rtx;
778 else
779 {
780 tem = hard_frame_pointer_rtx;
781
782 /* Tell reload not to eliminate the frame pointer. */
783 crtl->accesses_prior_frames = 1;
784 }
785 #endif
786
787 /* Some machines need special handling before we can access
788 arbitrary frames. For example, on the SPARC, we must first flush
789 all register windows to the stack. */
790 #ifdef SETUP_FRAME_ADDRESSES
791 if (count > 0)
792 SETUP_FRAME_ADDRESSES ();
793 #endif
794
795 /* On the SPARC, the return address is not in the frame, it is in a
796 register. There is no way to access it off of the current frame
797 pointer, but it can be accessed off the previous frame pointer by
798 reading the value from the register window save area. */
799 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
800 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
801 count--;
802 #endif
803
804 /* Scan back COUNT frames to the specified frame. */
805 for (i = 0; i < count; i++)
806 {
807 /* Assume the dynamic chain pointer is in the word that the
808 frame address points to, unless otherwise specified. */
809 #ifdef DYNAMIC_CHAIN_ADDRESS
810 tem = DYNAMIC_CHAIN_ADDRESS (tem);
811 #endif
812 tem = memory_address (Pmode, tem);
813 tem = gen_frame_mem (Pmode, tem);
814 tem = copy_to_reg (tem);
815 }
816
817 /* For __builtin_frame_address, return what we've got. But, on
818 the SPARC for example, we may have to add a bias. */
819 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
820 #ifdef FRAME_ADDR_RTX
821 return FRAME_ADDR_RTX (tem);
822 #else
823 return tem;
824 #endif
825
826 /* For __builtin_return_address, get the return address from that frame. */
827 #ifdef RETURN_ADDR_RTX
828 tem = RETURN_ADDR_RTX (count, tem);
829 #else
830 tem = memory_address (Pmode,
831 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
832 tem = gen_frame_mem (Pmode, tem);
833 #endif
834 return tem;
835 }
836
837 /* Alias set used for setjmp buffer. */
838 static alias_set_type setjmp_alias_set = -1;
839
840 /* Construct the leading half of a __builtin_setjmp call. Control will
841 return to RECEIVER_LABEL. This is also called directly by the SJLJ
842 exception handling code. */
843
844 void
845 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
846 {
847 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
848 rtx stack_save;
849 rtx mem;
850
851 if (setjmp_alias_set == -1)
852 setjmp_alias_set = new_alias_set ();
853
854 buf_addr = convert_memory_address (Pmode, buf_addr);
855
856 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
857
858 /* We store the frame pointer and the address of receiver_label in
859 the buffer and use the rest of it for the stack save area, which
860 is machine-dependent. */
861
862 mem = gen_rtx_MEM (Pmode, buf_addr);
863 set_mem_alias_set (mem, setjmp_alias_set);
864 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
865
866 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
867 GET_MODE_SIZE (Pmode))),
868 set_mem_alias_set (mem, setjmp_alias_set);
869
870 emit_move_insn (validize_mem (mem),
871 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
872
873 stack_save = gen_rtx_MEM (sa_mode,
874 plus_constant (Pmode, buf_addr,
875 2 * GET_MODE_SIZE (Pmode)));
876 set_mem_alias_set (stack_save, setjmp_alias_set);
877 emit_stack_save (SAVE_NONLOCAL, &stack_save);
878
879 /* If there is further processing to do, do it. */
880 #ifdef HAVE_builtin_setjmp_setup
881 if (HAVE_builtin_setjmp_setup)
882 emit_insn (gen_builtin_setjmp_setup (buf_addr));
883 #endif
884
885 /* We have a nonlocal label. */
886 cfun->has_nonlocal_label = 1;
887 }
888
889 /* Construct the trailing part of a __builtin_setjmp call. This is
890 also called directly by the SJLJ exception handling code.
891 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
892
893 void
894 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
895 {
896 rtx chain;
897
898 /* Mark the FP as used when we get here, so we have to make sure it's
899 marked as used by this function. */
900 emit_use (hard_frame_pointer_rtx);
901
902 /* Mark the static chain as clobbered here so life information
903 doesn't get messed up for it. */
904 chain = targetm.calls.static_chain (current_function_decl, true);
905 if (chain && REG_P (chain))
906 emit_clobber (chain);
907
908 /* Now put in the code to restore the frame pointer, and argument
909 pointer, if needed. */
910 #ifdef HAVE_nonlocal_goto
911 if (! HAVE_nonlocal_goto)
912 #endif
913 /* First adjust our frame pointer to its actual value. It was
914 previously set to the start of the virtual area corresponding to
915 the stacked variables when we branched here and now needs to be
916 adjusted to the actual hardware fp value.
917
918 Assignments to virtual registers are converted by
919 instantiate_virtual_regs into the corresponding assignment
920 to the underlying register (fp in this case) that makes
921 the original assignment true.
922 So the following insn will actually be decrementing fp by
923 STARTING_FRAME_OFFSET. */
924 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
925
926 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
927 if (fixed_regs[ARG_POINTER_REGNUM])
928 {
929 #ifdef ELIMINABLE_REGS
930 /* If the argument pointer can be eliminated in favor of the
931 frame pointer, we don't need to restore it. We assume here
932 that if such an elimination is present, it can always be used.
933 This is the case on all known machines; if we don't make this
934 assumption, we do unnecessary saving on many machines. */
935 size_t i;
936 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
937
938 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
939 if (elim_regs[i].from == ARG_POINTER_REGNUM
940 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
941 break;
942
943 if (i == ARRAY_SIZE (elim_regs))
944 #endif
945 {
946 /* Now restore our arg pointer from the address at which it
947 was saved in our stack frame. */
948 emit_move_insn (crtl->args.internal_arg_pointer,
949 copy_to_reg (get_arg_pointer_save_area ()));
950 }
951 }
952 #endif
953
954 #ifdef HAVE_builtin_setjmp_receiver
955 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
956 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
957 else
958 #endif
959 #ifdef HAVE_nonlocal_goto_receiver
960 if (HAVE_nonlocal_goto_receiver)
961 emit_insn (gen_nonlocal_goto_receiver ());
962 else
963 #endif
964 { /* Nothing */ }
965
966 /* We must not allow the code we just generated to be reordered by
967 scheduling. Specifically, the update of the frame pointer must
968 happen immediately, not later. Similarly, we must block
969 (frame-related) register values to be used across this code. */
970 emit_insn (gen_blockage ());
971 }
972
973 /* __builtin_longjmp is passed a pointer to an array of five words (not
974 all will be used on all machines). It operates similarly to the C
975 library function of the same name, but is more efficient. Much of
976 the code below is copied from the handling of non-local gotos. */
977
978 static void
979 expand_builtin_longjmp (rtx buf_addr, rtx value)
980 {
981 rtx fp, lab, stack, insn, last;
982 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
983
984 /* DRAP is needed for stack realign if longjmp is expanded to current
985 function */
986 if (SUPPORTS_STACK_ALIGNMENT)
987 crtl->need_drap = true;
988
989 if (setjmp_alias_set == -1)
990 setjmp_alias_set = new_alias_set ();
991
992 buf_addr = convert_memory_address (Pmode, buf_addr);
993
994 buf_addr = force_reg (Pmode, buf_addr);
995
996 /* We require that the user must pass a second argument of 1, because
997 that is what builtin_setjmp will return. */
998 gcc_assert (value == const1_rtx);
999
1000 last = get_last_insn ();
1001 #ifdef HAVE_builtin_longjmp
1002 if (HAVE_builtin_longjmp)
1003 emit_insn (gen_builtin_longjmp (buf_addr));
1004 else
1005 #endif
1006 {
1007 fp = gen_rtx_MEM (Pmode, buf_addr);
1008 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1009 GET_MODE_SIZE (Pmode)));
1010
1011 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1012 2 * GET_MODE_SIZE (Pmode)));
1013 set_mem_alias_set (fp, setjmp_alias_set);
1014 set_mem_alias_set (lab, setjmp_alias_set);
1015 set_mem_alias_set (stack, setjmp_alias_set);
1016
1017 /* Pick up FP, label, and SP from the block and jump. This code is
1018 from expand_goto in stmt.c; see there for detailed comments. */
1019 #ifdef HAVE_nonlocal_goto
1020 if (HAVE_nonlocal_goto)
1021 /* We have to pass a value to the nonlocal_goto pattern that will
1022 get copied into the static_chain pointer, but it does not matter
1023 what that value is, because builtin_setjmp does not use it. */
1024 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1025 else
1026 #endif
1027 {
1028 lab = copy_to_reg (lab);
1029
1030 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1031 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1032
1033 emit_move_insn (hard_frame_pointer_rtx, fp);
1034 emit_stack_restore (SAVE_NONLOCAL, stack);
1035
1036 emit_use (hard_frame_pointer_rtx);
1037 emit_use (stack_pointer_rtx);
1038 emit_indirect_jump (lab);
1039 }
1040 }
1041
1042 /* Search backwards and mark the jump insn as a non-local goto.
1043 Note that this precludes the use of __builtin_longjmp to a
1044 __builtin_setjmp target in the same function. However, we've
1045 already cautioned the user that these functions are for
1046 internal exception handling use only. */
1047 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1048 {
1049 gcc_assert (insn != last);
1050
1051 if (JUMP_P (insn))
1052 {
1053 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1054 break;
1055 }
1056 else if (CALL_P (insn))
1057 break;
1058 }
1059 }
1060
1061 static inline bool
1062 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1063 {
1064 return (iter->i < iter->n);
1065 }
1066
1067 /* This function validates the types of a function call argument list
1068 against a specified list of tree_codes. If the last specifier is a 0,
1069 that represents an ellipses, otherwise the last specifier must be a
1070 VOID_TYPE. */
1071
1072 static bool
1073 validate_arglist (const_tree callexpr, ...)
1074 {
1075 enum tree_code code;
1076 bool res = 0;
1077 va_list ap;
1078 const_call_expr_arg_iterator iter;
1079 const_tree arg;
1080
1081 va_start (ap, callexpr);
1082 init_const_call_expr_arg_iterator (callexpr, &iter);
1083
1084 do
1085 {
1086 code = (enum tree_code) va_arg (ap, int);
1087 switch (code)
1088 {
1089 case 0:
1090 /* This signifies an ellipses, any further arguments are all ok. */
1091 res = true;
1092 goto end;
1093 case VOID_TYPE:
1094 /* This signifies an endlink, if no arguments remain, return
1095 true, otherwise return false. */
1096 res = !more_const_call_expr_args_p (&iter);
1097 goto end;
1098 default:
1099 /* If no parameters remain or the parameter's code does not
1100 match the specified code, return false. Otherwise continue
1101 checking any remaining arguments. */
1102 arg = next_const_call_expr_arg (&iter);
1103 if (!validate_arg (arg, code))
1104 goto end;
1105 break;
1106 }
1107 }
1108 while (1);
1109
1110 /* We need gotos here since we can only have one VA_CLOSE in a
1111 function. */
1112 end: ;
1113 va_end (ap);
1114
1115 return res;
1116 }
1117
1118 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1119 and the address of the save area. */
1120
1121 static rtx
1122 expand_builtin_nonlocal_goto (tree exp)
1123 {
1124 tree t_label, t_save_area;
1125 rtx r_label, r_save_area, r_fp, r_sp, insn;
1126
1127 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1128 return NULL_RTX;
1129
1130 t_label = CALL_EXPR_ARG (exp, 0);
1131 t_save_area = CALL_EXPR_ARG (exp, 1);
1132
1133 r_label = expand_normal (t_label);
1134 r_label = convert_memory_address (Pmode, r_label);
1135 r_save_area = expand_normal (t_save_area);
1136 r_save_area = convert_memory_address (Pmode, r_save_area);
1137 /* Copy the address of the save location to a register just in case it was
1138 based on the frame pointer. */
1139 r_save_area = copy_to_reg (r_save_area);
1140 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1141 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1142 plus_constant (Pmode, r_save_area,
1143 GET_MODE_SIZE (Pmode)));
1144
1145 crtl->has_nonlocal_goto = 1;
1146
1147 #ifdef HAVE_nonlocal_goto
1148 /* ??? We no longer need to pass the static chain value, afaik. */
1149 if (HAVE_nonlocal_goto)
1150 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1151 else
1152 #endif
1153 {
1154 r_label = copy_to_reg (r_label);
1155
1156 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1157 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1158
1159 /* Restore frame pointer for containing function. */
1160 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1161 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1162
1163 /* USE of hard_frame_pointer_rtx added for consistency;
1164 not clear if really needed. */
1165 emit_use (hard_frame_pointer_rtx);
1166 emit_use (stack_pointer_rtx);
1167
1168 /* If the architecture is using a GP register, we must
1169 conservatively assume that the target function makes use of it.
1170 The prologue of functions with nonlocal gotos must therefore
1171 initialize the GP register to the appropriate value, and we
1172 must then make sure that this value is live at the point
1173 of the jump. (Note that this doesn't necessarily apply
1174 to targets with a nonlocal_goto pattern; they are free
1175 to implement it in their own way. Note also that this is
1176 a no-op if the GP register is a global invariant.) */
1177 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1178 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1179 emit_use (pic_offset_table_rtx);
1180
1181 emit_indirect_jump (r_label);
1182 }
1183
1184 /* Search backwards to the jump insn and mark it as a
1185 non-local goto. */
1186 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1187 {
1188 if (JUMP_P (insn))
1189 {
1190 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1191 break;
1192 }
1193 else if (CALL_P (insn))
1194 break;
1195 }
1196
1197 return const0_rtx;
1198 }
1199
1200 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1201 (not all will be used on all machines) that was passed to __builtin_setjmp.
1202 It updates the stack pointer in that block to correspond to the current
1203 stack pointer. */
1204
1205 static void
1206 expand_builtin_update_setjmp_buf (rtx buf_addr)
1207 {
1208 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1209 rtx stack_save
1210 = gen_rtx_MEM (sa_mode,
1211 memory_address
1212 (sa_mode,
1213 plus_constant (Pmode, buf_addr,
1214 2 * GET_MODE_SIZE (Pmode))));
1215
1216 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1217 }
1218
1219 /* Expand a call to __builtin_prefetch. For a target that does not support
1220 data prefetch, evaluate the memory address argument in case it has side
1221 effects. */
1222
1223 static void
1224 expand_builtin_prefetch (tree exp)
1225 {
1226 tree arg0, arg1, arg2;
1227 int nargs;
1228 rtx op0, op1, op2;
1229
1230 if (!validate_arglist (exp, POINTER_TYPE, 0))
1231 return;
1232
1233 arg0 = CALL_EXPR_ARG (exp, 0);
1234
1235 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1236 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1237 locality). */
1238 nargs = call_expr_nargs (exp);
1239 if (nargs > 1)
1240 arg1 = CALL_EXPR_ARG (exp, 1);
1241 else
1242 arg1 = integer_zero_node;
1243 if (nargs > 2)
1244 arg2 = CALL_EXPR_ARG (exp, 2);
1245 else
1246 arg2 = integer_three_node;
1247
1248 /* Argument 0 is an address. */
1249 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1250
1251 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1252 if (TREE_CODE (arg1) != INTEGER_CST)
1253 {
1254 error ("second argument to %<__builtin_prefetch%> must be a constant");
1255 arg1 = integer_zero_node;
1256 }
1257 op1 = expand_normal (arg1);
1258 /* Argument 1 must be either zero or one. */
1259 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1260 {
1261 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1262 " using zero");
1263 op1 = const0_rtx;
1264 }
1265
1266 /* Argument 2 (locality) must be a compile-time constant int. */
1267 if (TREE_CODE (arg2) != INTEGER_CST)
1268 {
1269 error ("third argument to %<__builtin_prefetch%> must be a constant");
1270 arg2 = integer_zero_node;
1271 }
1272 op2 = expand_normal (arg2);
1273 /* Argument 2 must be 0, 1, 2, or 3. */
1274 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1275 {
1276 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1277 op2 = const0_rtx;
1278 }
1279
1280 #ifdef HAVE_prefetch
1281 if (HAVE_prefetch)
1282 {
1283 struct expand_operand ops[3];
1284
1285 create_address_operand (&ops[0], op0);
1286 create_integer_operand (&ops[1], INTVAL (op1));
1287 create_integer_operand (&ops[2], INTVAL (op2));
1288 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1289 return;
1290 }
1291 #endif
1292
1293 /* Don't do anything with direct references to volatile memory, but
1294 generate code to handle other side effects. */
1295 if (!MEM_P (op0) && side_effects_p (op0))
1296 emit_insn (op0);
1297 }
1298
1299 /* Get a MEM rtx for expression EXP which is the address of an operand
1300 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1301 the maximum length of the block of memory that might be accessed or
1302 NULL if unknown. */
1303
1304 static rtx
1305 get_memory_rtx (tree exp, tree len)
1306 {
1307 tree orig_exp = exp;
1308 rtx addr, mem;
1309
1310 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1311 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1312 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1313 exp = TREE_OPERAND (exp, 0);
1314
1315 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1316 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1317
1318 /* Get an expression we can use to find the attributes to assign to MEM.
1319 First remove any nops. */
1320 while (CONVERT_EXPR_P (exp)
1321 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1322 exp = TREE_OPERAND (exp, 0);
1323
1324 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1325 (as builtin stringops may alias with anything). */
1326 exp = fold_build2 (MEM_REF,
1327 build_array_type (char_type_node,
1328 build_range_type (sizetype,
1329 size_one_node, len)),
1330 exp, build_int_cst (ptr_type_node, 0));
1331
1332 /* If the MEM_REF has no acceptable address, try to get the base object
1333 from the original address we got, and build an all-aliasing
1334 unknown-sized access to that one. */
1335 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1336 set_mem_attributes (mem, exp, 0);
1337 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1338 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1339 0))))
1340 {
1341 exp = build_fold_addr_expr (exp);
1342 exp = fold_build2 (MEM_REF,
1343 build_array_type (char_type_node,
1344 build_range_type (sizetype,
1345 size_zero_node,
1346 NULL)),
1347 exp, build_int_cst (ptr_type_node, 0));
1348 set_mem_attributes (mem, exp, 0);
1349 }
1350 set_mem_alias_set (mem, 0);
1351 return mem;
1352 }
1353 \f
1354 /* Built-in functions to perform an untyped call and return. */
1355
1356 #define apply_args_mode \
1357 (this_target_builtins->x_apply_args_mode)
1358 #define apply_result_mode \
1359 (this_target_builtins->x_apply_result_mode)
1360
1361 /* Return the size required for the block returned by __builtin_apply_args,
1362 and initialize apply_args_mode. */
1363
1364 static int
1365 apply_args_size (void)
1366 {
1367 static int size = -1;
1368 int align;
1369 unsigned int regno;
1370 enum machine_mode mode;
1371
1372 /* The values computed by this function never change. */
1373 if (size < 0)
1374 {
1375 /* The first value is the incoming arg-pointer. */
1376 size = GET_MODE_SIZE (Pmode);
1377
1378 /* The second value is the structure value address unless this is
1379 passed as an "invisible" first argument. */
1380 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1381 size += GET_MODE_SIZE (Pmode);
1382
1383 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1384 if (FUNCTION_ARG_REGNO_P (regno))
1385 {
1386 mode = targetm.calls.get_raw_arg_mode (regno);
1387
1388 gcc_assert (mode != VOIDmode);
1389
1390 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1391 if (size % align != 0)
1392 size = CEIL (size, align) * align;
1393 size += GET_MODE_SIZE (mode);
1394 apply_args_mode[regno] = mode;
1395 }
1396 else
1397 {
1398 apply_args_mode[regno] = VOIDmode;
1399 }
1400 }
1401 return size;
1402 }
1403
1404 /* Return the size required for the block returned by __builtin_apply,
1405 and initialize apply_result_mode. */
1406
1407 static int
1408 apply_result_size (void)
1409 {
1410 static int size = -1;
1411 int align, regno;
1412 enum machine_mode mode;
1413
1414 /* The values computed by this function never change. */
1415 if (size < 0)
1416 {
1417 size = 0;
1418
1419 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1420 if (targetm.calls.function_value_regno_p (regno))
1421 {
1422 mode = targetm.calls.get_raw_result_mode (regno);
1423
1424 gcc_assert (mode != VOIDmode);
1425
1426 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1427 if (size % align != 0)
1428 size = CEIL (size, align) * align;
1429 size += GET_MODE_SIZE (mode);
1430 apply_result_mode[regno] = mode;
1431 }
1432 else
1433 apply_result_mode[regno] = VOIDmode;
1434
1435 /* Allow targets that use untyped_call and untyped_return to override
1436 the size so that machine-specific information can be stored here. */
1437 #ifdef APPLY_RESULT_SIZE
1438 size = APPLY_RESULT_SIZE;
1439 #endif
1440 }
1441 return size;
1442 }
1443
1444 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1445 /* Create a vector describing the result block RESULT. If SAVEP is true,
1446 the result block is used to save the values; otherwise it is used to
1447 restore the values. */
1448
1449 static rtx
1450 result_vector (int savep, rtx result)
1451 {
1452 int regno, size, align, nelts;
1453 enum machine_mode mode;
1454 rtx reg, mem;
1455 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1456
1457 size = nelts = 0;
1458 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1459 if ((mode = apply_result_mode[regno]) != VOIDmode)
1460 {
1461 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1462 if (size % align != 0)
1463 size = CEIL (size, align) * align;
1464 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1465 mem = adjust_address (result, mode, size);
1466 savevec[nelts++] = (savep
1467 ? gen_rtx_SET (VOIDmode, mem, reg)
1468 : gen_rtx_SET (VOIDmode, reg, mem));
1469 size += GET_MODE_SIZE (mode);
1470 }
1471 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1472 }
1473 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1474
1475 /* Save the state required to perform an untyped call with the same
1476 arguments as were passed to the current function. */
1477
1478 static rtx
1479 expand_builtin_apply_args_1 (void)
1480 {
1481 rtx registers, tem;
1482 int size, align, regno;
1483 enum machine_mode mode;
1484 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1485
1486 /* Create a block where the arg-pointer, structure value address,
1487 and argument registers can be saved. */
1488 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1489
1490 /* Walk past the arg-pointer and structure value address. */
1491 size = GET_MODE_SIZE (Pmode);
1492 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1493 size += GET_MODE_SIZE (Pmode);
1494
1495 /* Save each register used in calling a function to the block. */
1496 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1497 if ((mode = apply_args_mode[regno]) != VOIDmode)
1498 {
1499 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1500 if (size % align != 0)
1501 size = CEIL (size, align) * align;
1502
1503 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1504
1505 emit_move_insn (adjust_address (registers, mode, size), tem);
1506 size += GET_MODE_SIZE (mode);
1507 }
1508
1509 /* Save the arg pointer to the block. */
1510 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1511 #ifdef STACK_GROWS_DOWNWARD
1512 /* We need the pointer as the caller actually passed them to us, not
1513 as we might have pretended they were passed. Make sure it's a valid
1514 operand, as emit_move_insn isn't expected to handle a PLUS. */
1515 tem
1516 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1517 NULL_RTX);
1518 #endif
1519 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1520
1521 size = GET_MODE_SIZE (Pmode);
1522
1523 /* Save the structure value address unless this is passed as an
1524 "invisible" first argument. */
1525 if (struct_incoming_value)
1526 {
1527 emit_move_insn (adjust_address (registers, Pmode, size),
1528 copy_to_reg (struct_incoming_value));
1529 size += GET_MODE_SIZE (Pmode);
1530 }
1531
1532 /* Return the address of the block. */
1533 return copy_addr_to_reg (XEXP (registers, 0));
1534 }
1535
1536 /* __builtin_apply_args returns block of memory allocated on
1537 the stack into which is stored the arg pointer, structure
1538 value address, static chain, and all the registers that might
1539 possibly be used in performing a function call. The code is
1540 moved to the start of the function so the incoming values are
1541 saved. */
1542
1543 static rtx
1544 expand_builtin_apply_args (void)
1545 {
1546 /* Don't do __builtin_apply_args more than once in a function.
1547 Save the result of the first call and reuse it. */
1548 if (apply_args_value != 0)
1549 return apply_args_value;
1550 {
1551 /* When this function is called, it means that registers must be
1552 saved on entry to this function. So we migrate the
1553 call to the first insn of this function. */
1554 rtx temp;
1555 rtx seq;
1556
1557 start_sequence ();
1558 temp = expand_builtin_apply_args_1 ();
1559 seq = get_insns ();
1560 end_sequence ();
1561
1562 apply_args_value = temp;
1563
1564 /* Put the insns after the NOTE that starts the function.
1565 If this is inside a start_sequence, make the outer-level insn
1566 chain current, so the code is placed at the start of the
1567 function. If internal_arg_pointer is a non-virtual pseudo,
1568 it needs to be placed after the function that initializes
1569 that pseudo. */
1570 push_topmost_sequence ();
1571 if (REG_P (crtl->args.internal_arg_pointer)
1572 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1573 emit_insn_before (seq, parm_birth_insn);
1574 else
1575 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1576 pop_topmost_sequence ();
1577 return temp;
1578 }
1579 }
1580
1581 /* Perform an untyped call and save the state required to perform an
1582 untyped return of whatever value was returned by the given function. */
1583
1584 static rtx
1585 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1586 {
1587 int size, align, regno;
1588 enum machine_mode mode;
1589 rtx incoming_args, result, reg, dest, src, call_insn;
1590 rtx old_stack_level = 0;
1591 rtx call_fusage = 0;
1592 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1593
1594 arguments = convert_memory_address (Pmode, arguments);
1595
1596 /* Create a block where the return registers can be saved. */
1597 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1598
1599 /* Fetch the arg pointer from the ARGUMENTS block. */
1600 incoming_args = gen_reg_rtx (Pmode);
1601 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1602 #ifndef STACK_GROWS_DOWNWARD
1603 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1604 incoming_args, 0, OPTAB_LIB_WIDEN);
1605 #endif
1606
1607 /* Push a new argument block and copy the arguments. Do not allow
1608 the (potential) memcpy call below to interfere with our stack
1609 manipulations. */
1610 do_pending_stack_adjust ();
1611 NO_DEFER_POP;
1612
1613 /* Save the stack with nonlocal if available. */
1614 #ifdef HAVE_save_stack_nonlocal
1615 if (HAVE_save_stack_nonlocal)
1616 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1617 else
1618 #endif
1619 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1620
1621 /* Allocate a block of memory onto the stack and copy the memory
1622 arguments to the outgoing arguments address. We can pass TRUE
1623 as the 4th argument because we just saved the stack pointer
1624 and will restore it right after the call. */
1625 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1626
1627 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1628 may have already set current_function_calls_alloca to true.
1629 current_function_calls_alloca won't be set if argsize is zero,
1630 so we have to guarantee need_drap is true here. */
1631 if (SUPPORTS_STACK_ALIGNMENT)
1632 crtl->need_drap = true;
1633
1634 dest = virtual_outgoing_args_rtx;
1635 #ifndef STACK_GROWS_DOWNWARD
1636 if (CONST_INT_P (argsize))
1637 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1638 else
1639 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1640 #endif
1641 dest = gen_rtx_MEM (BLKmode, dest);
1642 set_mem_align (dest, PARM_BOUNDARY);
1643 src = gen_rtx_MEM (BLKmode, incoming_args);
1644 set_mem_align (src, PARM_BOUNDARY);
1645 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1646
1647 /* Refer to the argument block. */
1648 apply_args_size ();
1649 arguments = gen_rtx_MEM (BLKmode, arguments);
1650 set_mem_align (arguments, PARM_BOUNDARY);
1651
1652 /* Walk past the arg-pointer and structure value address. */
1653 size = GET_MODE_SIZE (Pmode);
1654 if (struct_value)
1655 size += GET_MODE_SIZE (Pmode);
1656
1657 /* Restore each of the registers previously saved. Make USE insns
1658 for each of these registers for use in making the call. */
1659 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1660 if ((mode = apply_args_mode[regno]) != VOIDmode)
1661 {
1662 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1663 if (size % align != 0)
1664 size = CEIL (size, align) * align;
1665 reg = gen_rtx_REG (mode, regno);
1666 emit_move_insn (reg, adjust_address (arguments, mode, size));
1667 use_reg (&call_fusage, reg);
1668 size += GET_MODE_SIZE (mode);
1669 }
1670
1671 /* Restore the structure value address unless this is passed as an
1672 "invisible" first argument. */
1673 size = GET_MODE_SIZE (Pmode);
1674 if (struct_value)
1675 {
1676 rtx value = gen_reg_rtx (Pmode);
1677 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1678 emit_move_insn (struct_value, value);
1679 if (REG_P (struct_value))
1680 use_reg (&call_fusage, struct_value);
1681 size += GET_MODE_SIZE (Pmode);
1682 }
1683
1684 /* All arguments and registers used for the call are set up by now! */
1685 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1686
1687 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1688 and we don't want to load it into a register as an optimization,
1689 because prepare_call_address already did it if it should be done. */
1690 if (GET_CODE (function) != SYMBOL_REF)
1691 function = memory_address (FUNCTION_MODE, function);
1692
1693 /* Generate the actual call instruction and save the return value. */
1694 #ifdef HAVE_untyped_call
1695 if (HAVE_untyped_call)
1696 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1697 result, result_vector (1, result)));
1698 else
1699 #endif
1700 #ifdef HAVE_call_value
1701 if (HAVE_call_value)
1702 {
1703 rtx valreg = 0;
1704
1705 /* Locate the unique return register. It is not possible to
1706 express a call that sets more than one return register using
1707 call_value; use untyped_call for that. In fact, untyped_call
1708 only needs to save the return registers in the given block. */
1709 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1710 if ((mode = apply_result_mode[regno]) != VOIDmode)
1711 {
1712 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1713
1714 valreg = gen_rtx_REG (mode, regno);
1715 }
1716
1717 emit_call_insn (GEN_CALL_VALUE (valreg,
1718 gen_rtx_MEM (FUNCTION_MODE, function),
1719 const0_rtx, NULL_RTX, const0_rtx));
1720
1721 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1722 }
1723 else
1724 #endif
1725 gcc_unreachable ();
1726
1727 /* Find the CALL insn we just emitted, and attach the register usage
1728 information. */
1729 call_insn = last_call_insn ();
1730 add_function_usage_to (call_insn, call_fusage);
1731
1732 /* Restore the stack. */
1733 #ifdef HAVE_save_stack_nonlocal
1734 if (HAVE_save_stack_nonlocal)
1735 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1736 else
1737 #endif
1738 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1739 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1740
1741 OK_DEFER_POP;
1742
1743 /* Return the address of the result block. */
1744 result = copy_addr_to_reg (XEXP (result, 0));
1745 return convert_memory_address (ptr_mode, result);
1746 }
1747
1748 /* Perform an untyped return. */
1749
1750 static void
1751 expand_builtin_return (rtx result)
1752 {
1753 int size, align, regno;
1754 enum machine_mode mode;
1755 rtx reg;
1756 rtx call_fusage = 0;
1757
1758 result = convert_memory_address (Pmode, result);
1759
1760 apply_result_size ();
1761 result = gen_rtx_MEM (BLKmode, result);
1762
1763 #ifdef HAVE_untyped_return
1764 if (HAVE_untyped_return)
1765 {
1766 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1767 emit_barrier ();
1768 return;
1769 }
1770 #endif
1771
1772 /* Restore the return value and note that each value is used. */
1773 size = 0;
1774 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1775 if ((mode = apply_result_mode[regno]) != VOIDmode)
1776 {
1777 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1778 if (size % align != 0)
1779 size = CEIL (size, align) * align;
1780 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1781 emit_move_insn (reg, adjust_address (result, mode, size));
1782
1783 push_to_sequence (call_fusage);
1784 emit_use (reg);
1785 call_fusage = get_insns ();
1786 end_sequence ();
1787 size += GET_MODE_SIZE (mode);
1788 }
1789
1790 /* Put the USE insns before the return. */
1791 emit_insn (call_fusage);
1792
1793 /* Return whatever values was restored by jumping directly to the end
1794 of the function. */
1795 expand_naked_return ();
1796 }
1797
1798 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1799
1800 static enum type_class
1801 type_to_class (tree type)
1802 {
1803 switch (TREE_CODE (type))
1804 {
1805 case VOID_TYPE: return void_type_class;
1806 case INTEGER_TYPE: return integer_type_class;
1807 case ENUMERAL_TYPE: return enumeral_type_class;
1808 case BOOLEAN_TYPE: return boolean_type_class;
1809 case POINTER_TYPE: return pointer_type_class;
1810 case REFERENCE_TYPE: return reference_type_class;
1811 case OFFSET_TYPE: return offset_type_class;
1812 case REAL_TYPE: return real_type_class;
1813 case COMPLEX_TYPE: return complex_type_class;
1814 case FUNCTION_TYPE: return function_type_class;
1815 case METHOD_TYPE: return method_type_class;
1816 case RECORD_TYPE: return record_type_class;
1817 case UNION_TYPE:
1818 case QUAL_UNION_TYPE: return union_type_class;
1819 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1820 ? string_type_class : array_type_class);
1821 case LANG_TYPE: return lang_type_class;
1822 default: return no_type_class;
1823 }
1824 }
1825
1826 /* Expand a call EXP to __builtin_classify_type. */
1827
1828 static rtx
1829 expand_builtin_classify_type (tree exp)
1830 {
1831 if (call_expr_nargs (exp))
1832 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1833 return GEN_INT (no_type_class);
1834 }
1835
1836 /* This helper macro, meant to be used in mathfn_built_in below,
1837 determines which among a set of three builtin math functions is
1838 appropriate for a given type mode. The `F' and `L' cases are
1839 automatically generated from the `double' case. */
1840 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1841 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1842 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1843 fcodel = BUILT_IN_MATHFN##L ; break;
1844 /* Similar to above, but appends _R after any F/L suffix. */
1845 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1846 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1847 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1848 fcodel = BUILT_IN_MATHFN##L_R ; break;
1849
1850 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1851 if available. If IMPLICIT is true use the implicit builtin declaration,
1852 otherwise use the explicit declaration. If we can't do the conversion,
1853 return zero. */
1854
1855 static tree
1856 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1857 {
1858 enum built_in_function fcode, fcodef, fcodel, fcode2;
1859
1860 switch (fn)
1861 {
1862 CASE_MATHFN (BUILT_IN_ACOS)
1863 CASE_MATHFN (BUILT_IN_ACOSH)
1864 CASE_MATHFN (BUILT_IN_ASIN)
1865 CASE_MATHFN (BUILT_IN_ASINH)
1866 CASE_MATHFN (BUILT_IN_ATAN)
1867 CASE_MATHFN (BUILT_IN_ATAN2)
1868 CASE_MATHFN (BUILT_IN_ATANH)
1869 CASE_MATHFN (BUILT_IN_CBRT)
1870 CASE_MATHFN (BUILT_IN_CEIL)
1871 CASE_MATHFN (BUILT_IN_CEXPI)
1872 CASE_MATHFN (BUILT_IN_COPYSIGN)
1873 CASE_MATHFN (BUILT_IN_COS)
1874 CASE_MATHFN (BUILT_IN_COSH)
1875 CASE_MATHFN (BUILT_IN_DREM)
1876 CASE_MATHFN (BUILT_IN_ERF)
1877 CASE_MATHFN (BUILT_IN_ERFC)
1878 CASE_MATHFN (BUILT_IN_EXP)
1879 CASE_MATHFN (BUILT_IN_EXP10)
1880 CASE_MATHFN (BUILT_IN_EXP2)
1881 CASE_MATHFN (BUILT_IN_EXPM1)
1882 CASE_MATHFN (BUILT_IN_FABS)
1883 CASE_MATHFN (BUILT_IN_FDIM)
1884 CASE_MATHFN (BUILT_IN_FLOOR)
1885 CASE_MATHFN (BUILT_IN_FMA)
1886 CASE_MATHFN (BUILT_IN_FMAX)
1887 CASE_MATHFN (BUILT_IN_FMIN)
1888 CASE_MATHFN (BUILT_IN_FMOD)
1889 CASE_MATHFN (BUILT_IN_FREXP)
1890 CASE_MATHFN (BUILT_IN_GAMMA)
1891 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1892 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1893 CASE_MATHFN (BUILT_IN_HYPOT)
1894 CASE_MATHFN (BUILT_IN_ILOGB)
1895 CASE_MATHFN (BUILT_IN_ICEIL)
1896 CASE_MATHFN (BUILT_IN_IFLOOR)
1897 CASE_MATHFN (BUILT_IN_INF)
1898 CASE_MATHFN (BUILT_IN_IRINT)
1899 CASE_MATHFN (BUILT_IN_IROUND)
1900 CASE_MATHFN (BUILT_IN_ISINF)
1901 CASE_MATHFN (BUILT_IN_J0)
1902 CASE_MATHFN (BUILT_IN_J1)
1903 CASE_MATHFN (BUILT_IN_JN)
1904 CASE_MATHFN (BUILT_IN_LCEIL)
1905 CASE_MATHFN (BUILT_IN_LDEXP)
1906 CASE_MATHFN (BUILT_IN_LFLOOR)
1907 CASE_MATHFN (BUILT_IN_LGAMMA)
1908 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1909 CASE_MATHFN (BUILT_IN_LLCEIL)
1910 CASE_MATHFN (BUILT_IN_LLFLOOR)
1911 CASE_MATHFN (BUILT_IN_LLRINT)
1912 CASE_MATHFN (BUILT_IN_LLROUND)
1913 CASE_MATHFN (BUILT_IN_LOG)
1914 CASE_MATHFN (BUILT_IN_LOG10)
1915 CASE_MATHFN (BUILT_IN_LOG1P)
1916 CASE_MATHFN (BUILT_IN_LOG2)
1917 CASE_MATHFN (BUILT_IN_LOGB)
1918 CASE_MATHFN (BUILT_IN_LRINT)
1919 CASE_MATHFN (BUILT_IN_LROUND)
1920 CASE_MATHFN (BUILT_IN_MODF)
1921 CASE_MATHFN (BUILT_IN_NAN)
1922 CASE_MATHFN (BUILT_IN_NANS)
1923 CASE_MATHFN (BUILT_IN_NEARBYINT)
1924 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1925 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1926 CASE_MATHFN (BUILT_IN_POW)
1927 CASE_MATHFN (BUILT_IN_POWI)
1928 CASE_MATHFN (BUILT_IN_POW10)
1929 CASE_MATHFN (BUILT_IN_REMAINDER)
1930 CASE_MATHFN (BUILT_IN_REMQUO)
1931 CASE_MATHFN (BUILT_IN_RINT)
1932 CASE_MATHFN (BUILT_IN_ROUND)
1933 CASE_MATHFN (BUILT_IN_SCALB)
1934 CASE_MATHFN (BUILT_IN_SCALBLN)
1935 CASE_MATHFN (BUILT_IN_SCALBN)
1936 CASE_MATHFN (BUILT_IN_SIGNBIT)
1937 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1938 CASE_MATHFN (BUILT_IN_SIN)
1939 CASE_MATHFN (BUILT_IN_SINCOS)
1940 CASE_MATHFN (BUILT_IN_SINH)
1941 CASE_MATHFN (BUILT_IN_SQRT)
1942 CASE_MATHFN (BUILT_IN_TAN)
1943 CASE_MATHFN (BUILT_IN_TANH)
1944 CASE_MATHFN (BUILT_IN_TGAMMA)
1945 CASE_MATHFN (BUILT_IN_TRUNC)
1946 CASE_MATHFN (BUILT_IN_Y0)
1947 CASE_MATHFN (BUILT_IN_Y1)
1948 CASE_MATHFN (BUILT_IN_YN)
1949
1950 default:
1951 return NULL_TREE;
1952 }
1953
1954 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1955 fcode2 = fcode;
1956 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1957 fcode2 = fcodef;
1958 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1959 fcode2 = fcodel;
1960 else
1961 return NULL_TREE;
1962
1963 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1964 return NULL_TREE;
1965
1966 return builtin_decl_explicit (fcode2);
1967 }
1968
1969 /* Like mathfn_built_in_1(), but always use the implicit array. */
1970
1971 tree
1972 mathfn_built_in (tree type, enum built_in_function fn)
1973 {
1974 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1975 }
1976
1977 /* If errno must be maintained, expand the RTL to check if the result,
1978 TARGET, of a built-in function call, EXP, is NaN, and if so set
1979 errno to EDOM. */
1980
1981 static void
1982 expand_errno_check (tree exp, rtx target)
1983 {
1984 rtx lab = gen_label_rtx ();
1985
1986 /* Test the result; if it is NaN, set errno=EDOM because
1987 the argument was not in the domain. */
1988 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1989 NULL_RTX, NULL_RTX, lab,
1990 /* The jump is very likely. */
1991 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1992
1993 #ifdef TARGET_EDOM
1994 /* If this built-in doesn't throw an exception, set errno directly. */
1995 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1996 {
1997 #ifdef GEN_ERRNO_RTX
1998 rtx errno_rtx = GEN_ERRNO_RTX;
1999 #else
2000 rtx errno_rtx
2001 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2002 #endif
2003 emit_move_insn (errno_rtx,
2004 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2005 emit_label (lab);
2006 return;
2007 }
2008 #endif
2009
2010 /* Make sure the library call isn't expanded as a tail call. */
2011 CALL_EXPR_TAILCALL (exp) = 0;
2012
2013 /* We can't set errno=EDOM directly; let the library call do it.
2014 Pop the arguments right away in case the call gets deleted. */
2015 NO_DEFER_POP;
2016 expand_call (exp, target, 0);
2017 OK_DEFER_POP;
2018 emit_label (lab);
2019 }
2020
2021 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2022 Return NULL_RTX if a normal call should be emitted rather than expanding
2023 the function in-line. EXP is the expression that is a call to the builtin
2024 function; if convenient, the result should be placed in TARGET.
2025 SUBTARGET may be used as the target for computing one of EXP's operands. */
2026
2027 static rtx
2028 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2029 {
2030 optab builtin_optab;
2031 rtx op0, insns;
2032 tree fndecl = get_callee_fndecl (exp);
2033 enum machine_mode mode;
2034 bool errno_set = false;
2035 bool try_widening = false;
2036 tree arg;
2037
2038 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2039 return NULL_RTX;
2040
2041 arg = CALL_EXPR_ARG (exp, 0);
2042
2043 switch (DECL_FUNCTION_CODE (fndecl))
2044 {
2045 CASE_FLT_FN (BUILT_IN_SQRT):
2046 errno_set = ! tree_expr_nonnegative_p (arg);
2047 try_widening = true;
2048 builtin_optab = sqrt_optab;
2049 break;
2050 CASE_FLT_FN (BUILT_IN_EXP):
2051 errno_set = true; builtin_optab = exp_optab; break;
2052 CASE_FLT_FN (BUILT_IN_EXP10):
2053 CASE_FLT_FN (BUILT_IN_POW10):
2054 errno_set = true; builtin_optab = exp10_optab; break;
2055 CASE_FLT_FN (BUILT_IN_EXP2):
2056 errno_set = true; builtin_optab = exp2_optab; break;
2057 CASE_FLT_FN (BUILT_IN_EXPM1):
2058 errno_set = true; builtin_optab = expm1_optab; break;
2059 CASE_FLT_FN (BUILT_IN_LOGB):
2060 errno_set = true; builtin_optab = logb_optab; break;
2061 CASE_FLT_FN (BUILT_IN_LOG):
2062 errno_set = true; builtin_optab = log_optab; break;
2063 CASE_FLT_FN (BUILT_IN_LOG10):
2064 errno_set = true; builtin_optab = log10_optab; break;
2065 CASE_FLT_FN (BUILT_IN_LOG2):
2066 errno_set = true; builtin_optab = log2_optab; break;
2067 CASE_FLT_FN (BUILT_IN_LOG1P):
2068 errno_set = true; builtin_optab = log1p_optab; break;
2069 CASE_FLT_FN (BUILT_IN_ASIN):
2070 builtin_optab = asin_optab; break;
2071 CASE_FLT_FN (BUILT_IN_ACOS):
2072 builtin_optab = acos_optab; break;
2073 CASE_FLT_FN (BUILT_IN_TAN):
2074 builtin_optab = tan_optab; break;
2075 CASE_FLT_FN (BUILT_IN_ATAN):
2076 builtin_optab = atan_optab; break;
2077 CASE_FLT_FN (BUILT_IN_FLOOR):
2078 builtin_optab = floor_optab; break;
2079 CASE_FLT_FN (BUILT_IN_CEIL):
2080 builtin_optab = ceil_optab; break;
2081 CASE_FLT_FN (BUILT_IN_TRUNC):
2082 builtin_optab = btrunc_optab; break;
2083 CASE_FLT_FN (BUILT_IN_ROUND):
2084 builtin_optab = round_optab; break;
2085 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2086 builtin_optab = nearbyint_optab;
2087 if (flag_trapping_math)
2088 break;
2089 /* Else fallthrough and expand as rint. */
2090 CASE_FLT_FN (BUILT_IN_RINT):
2091 builtin_optab = rint_optab; break;
2092 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2093 builtin_optab = significand_optab; break;
2094 default:
2095 gcc_unreachable ();
2096 }
2097
2098 /* Make a suitable register to place result in. */
2099 mode = TYPE_MODE (TREE_TYPE (exp));
2100
2101 if (! flag_errno_math || ! HONOR_NANS (mode))
2102 errno_set = false;
2103
2104 /* Before working hard, check whether the instruction is available, but try
2105 to widen the mode for specific operations. */
2106 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2107 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2108 && (!errno_set || !optimize_insn_for_size_p ()))
2109 {
2110 rtx result = gen_reg_rtx (mode);
2111
2112 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2113 need to expand the argument again. This way, we will not perform
2114 side-effects more the once. */
2115 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2116
2117 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2118
2119 start_sequence ();
2120
2121 /* Compute into RESULT.
2122 Set RESULT to wherever the result comes back. */
2123 result = expand_unop (mode, builtin_optab, op0, result, 0);
2124
2125 if (result != 0)
2126 {
2127 if (errno_set)
2128 expand_errno_check (exp, result);
2129
2130 /* Output the entire sequence. */
2131 insns = get_insns ();
2132 end_sequence ();
2133 emit_insn (insns);
2134 return result;
2135 }
2136
2137 /* If we were unable to expand via the builtin, stop the sequence
2138 (without outputting the insns) and call to the library function
2139 with the stabilized argument list. */
2140 end_sequence ();
2141 }
2142
2143 return expand_call (exp, target, target == const0_rtx);
2144 }
2145
2146 /* Expand a call to the builtin binary math functions (pow and atan2).
2147 Return NULL_RTX if a normal call should be emitted rather than expanding the
2148 function in-line. EXP is the expression that is a call to the builtin
2149 function; if convenient, the result should be placed in TARGET.
2150 SUBTARGET may be used as the target for computing one of EXP's
2151 operands. */
2152
2153 static rtx
2154 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2155 {
2156 optab builtin_optab;
2157 rtx op0, op1, insns, result;
2158 int op1_type = REAL_TYPE;
2159 tree fndecl = get_callee_fndecl (exp);
2160 tree arg0, arg1;
2161 enum machine_mode mode;
2162 bool errno_set = true;
2163
2164 switch (DECL_FUNCTION_CODE (fndecl))
2165 {
2166 CASE_FLT_FN (BUILT_IN_SCALBN):
2167 CASE_FLT_FN (BUILT_IN_SCALBLN):
2168 CASE_FLT_FN (BUILT_IN_LDEXP):
2169 op1_type = INTEGER_TYPE;
2170 default:
2171 break;
2172 }
2173
2174 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2175 return NULL_RTX;
2176
2177 arg0 = CALL_EXPR_ARG (exp, 0);
2178 arg1 = CALL_EXPR_ARG (exp, 1);
2179
2180 switch (DECL_FUNCTION_CODE (fndecl))
2181 {
2182 CASE_FLT_FN (BUILT_IN_POW):
2183 builtin_optab = pow_optab; break;
2184 CASE_FLT_FN (BUILT_IN_ATAN2):
2185 builtin_optab = atan2_optab; break;
2186 CASE_FLT_FN (BUILT_IN_SCALB):
2187 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2188 return 0;
2189 builtin_optab = scalb_optab; break;
2190 CASE_FLT_FN (BUILT_IN_SCALBN):
2191 CASE_FLT_FN (BUILT_IN_SCALBLN):
2192 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2193 return 0;
2194 /* Fall through... */
2195 CASE_FLT_FN (BUILT_IN_LDEXP):
2196 builtin_optab = ldexp_optab; break;
2197 CASE_FLT_FN (BUILT_IN_FMOD):
2198 builtin_optab = fmod_optab; break;
2199 CASE_FLT_FN (BUILT_IN_REMAINDER):
2200 CASE_FLT_FN (BUILT_IN_DREM):
2201 builtin_optab = remainder_optab; break;
2202 default:
2203 gcc_unreachable ();
2204 }
2205
2206 /* Make a suitable register to place result in. */
2207 mode = TYPE_MODE (TREE_TYPE (exp));
2208
2209 /* Before working hard, check whether the instruction is available. */
2210 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2211 return NULL_RTX;
2212
2213 result = gen_reg_rtx (mode);
2214
2215 if (! flag_errno_math || ! HONOR_NANS (mode))
2216 errno_set = false;
2217
2218 if (errno_set && optimize_insn_for_size_p ())
2219 return 0;
2220
2221 /* Always stabilize the argument list. */
2222 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2223 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2224
2225 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2226 op1 = expand_normal (arg1);
2227
2228 start_sequence ();
2229
2230 /* Compute into RESULT.
2231 Set RESULT to wherever the result comes back. */
2232 result = expand_binop (mode, builtin_optab, op0, op1,
2233 result, 0, OPTAB_DIRECT);
2234
2235 /* If we were unable to expand via the builtin, stop the sequence
2236 (without outputting the insns) and call to the library function
2237 with the stabilized argument list. */
2238 if (result == 0)
2239 {
2240 end_sequence ();
2241 return expand_call (exp, target, target == const0_rtx);
2242 }
2243
2244 if (errno_set)
2245 expand_errno_check (exp, result);
2246
2247 /* Output the entire sequence. */
2248 insns = get_insns ();
2249 end_sequence ();
2250 emit_insn (insns);
2251
2252 return result;
2253 }
2254
2255 /* Expand a call to the builtin trinary math functions (fma).
2256 Return NULL_RTX if a normal call should be emitted rather than expanding the
2257 function in-line. EXP is the expression that is a call to the builtin
2258 function; if convenient, the result should be placed in TARGET.
2259 SUBTARGET may be used as the target for computing one of EXP's
2260 operands. */
2261
2262 static rtx
2263 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2264 {
2265 optab builtin_optab;
2266 rtx op0, op1, op2, insns, result;
2267 tree fndecl = get_callee_fndecl (exp);
2268 tree arg0, arg1, arg2;
2269 enum machine_mode mode;
2270
2271 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2272 return NULL_RTX;
2273
2274 arg0 = CALL_EXPR_ARG (exp, 0);
2275 arg1 = CALL_EXPR_ARG (exp, 1);
2276 arg2 = CALL_EXPR_ARG (exp, 2);
2277
2278 switch (DECL_FUNCTION_CODE (fndecl))
2279 {
2280 CASE_FLT_FN (BUILT_IN_FMA):
2281 builtin_optab = fma_optab; break;
2282 default:
2283 gcc_unreachable ();
2284 }
2285
2286 /* Make a suitable register to place result in. */
2287 mode = TYPE_MODE (TREE_TYPE (exp));
2288
2289 /* Before working hard, check whether the instruction is available. */
2290 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2291 return NULL_RTX;
2292
2293 result = gen_reg_rtx (mode);
2294
2295 /* Always stabilize the argument list. */
2296 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2297 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2298 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2299
2300 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2301 op1 = expand_normal (arg1);
2302 op2 = expand_normal (arg2);
2303
2304 start_sequence ();
2305
2306 /* Compute into RESULT.
2307 Set RESULT to wherever the result comes back. */
2308 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2309 result, 0);
2310
2311 /* If we were unable to expand via the builtin, stop the sequence
2312 (without outputting the insns) and call to the library function
2313 with the stabilized argument list. */
2314 if (result == 0)
2315 {
2316 end_sequence ();
2317 return expand_call (exp, target, target == const0_rtx);
2318 }
2319
2320 /* Output the entire sequence. */
2321 insns = get_insns ();
2322 end_sequence ();
2323 emit_insn (insns);
2324
2325 return result;
2326 }
2327
2328 /* Expand a call to the builtin sin and cos math functions.
2329 Return NULL_RTX if a normal call should be emitted rather than expanding the
2330 function in-line. EXP is the expression that is a call to the builtin
2331 function; if convenient, the result should be placed in TARGET.
2332 SUBTARGET may be used as the target for computing one of EXP's
2333 operands. */
2334
2335 static rtx
2336 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2337 {
2338 optab builtin_optab;
2339 rtx op0, insns;
2340 tree fndecl = get_callee_fndecl (exp);
2341 enum machine_mode mode;
2342 tree arg;
2343
2344 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2345 return NULL_RTX;
2346
2347 arg = CALL_EXPR_ARG (exp, 0);
2348
2349 switch (DECL_FUNCTION_CODE (fndecl))
2350 {
2351 CASE_FLT_FN (BUILT_IN_SIN):
2352 CASE_FLT_FN (BUILT_IN_COS):
2353 builtin_optab = sincos_optab; break;
2354 default:
2355 gcc_unreachable ();
2356 }
2357
2358 /* Make a suitable register to place result in. */
2359 mode = TYPE_MODE (TREE_TYPE (exp));
2360
2361 /* Check if sincos insn is available, otherwise fallback
2362 to sin or cos insn. */
2363 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2364 switch (DECL_FUNCTION_CODE (fndecl))
2365 {
2366 CASE_FLT_FN (BUILT_IN_SIN):
2367 builtin_optab = sin_optab; break;
2368 CASE_FLT_FN (BUILT_IN_COS):
2369 builtin_optab = cos_optab; break;
2370 default:
2371 gcc_unreachable ();
2372 }
2373
2374 /* Before working hard, check whether the instruction is available. */
2375 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2376 {
2377 rtx result = gen_reg_rtx (mode);
2378
2379 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2380 need to expand the argument again. This way, we will not perform
2381 side-effects more the once. */
2382 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2383
2384 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2385
2386 start_sequence ();
2387
2388 /* Compute into RESULT.
2389 Set RESULT to wherever the result comes back. */
2390 if (builtin_optab == sincos_optab)
2391 {
2392 int ok;
2393
2394 switch (DECL_FUNCTION_CODE (fndecl))
2395 {
2396 CASE_FLT_FN (BUILT_IN_SIN):
2397 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2398 break;
2399 CASE_FLT_FN (BUILT_IN_COS):
2400 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2401 break;
2402 default:
2403 gcc_unreachable ();
2404 }
2405 gcc_assert (ok);
2406 }
2407 else
2408 result = expand_unop (mode, builtin_optab, op0, result, 0);
2409
2410 if (result != 0)
2411 {
2412 /* Output the entire sequence. */
2413 insns = get_insns ();
2414 end_sequence ();
2415 emit_insn (insns);
2416 return result;
2417 }
2418
2419 /* If we were unable to expand via the builtin, stop the sequence
2420 (without outputting the insns) and call to the library function
2421 with the stabilized argument list. */
2422 end_sequence ();
2423 }
2424
2425 return expand_call (exp, target, target == const0_rtx);
2426 }
2427
2428 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2429 return an RTL instruction code that implements the functionality.
2430 If that isn't possible or available return CODE_FOR_nothing. */
2431
2432 static enum insn_code
2433 interclass_mathfn_icode (tree arg, tree fndecl)
2434 {
2435 bool errno_set = false;
2436 optab builtin_optab = unknown_optab;
2437 enum machine_mode mode;
2438
2439 switch (DECL_FUNCTION_CODE (fndecl))
2440 {
2441 CASE_FLT_FN (BUILT_IN_ILOGB):
2442 errno_set = true; builtin_optab = ilogb_optab; break;
2443 CASE_FLT_FN (BUILT_IN_ISINF):
2444 builtin_optab = isinf_optab; break;
2445 case BUILT_IN_ISNORMAL:
2446 case BUILT_IN_ISFINITE:
2447 CASE_FLT_FN (BUILT_IN_FINITE):
2448 case BUILT_IN_FINITED32:
2449 case BUILT_IN_FINITED64:
2450 case BUILT_IN_FINITED128:
2451 case BUILT_IN_ISINFD32:
2452 case BUILT_IN_ISINFD64:
2453 case BUILT_IN_ISINFD128:
2454 /* These builtins have no optabs (yet). */
2455 break;
2456 default:
2457 gcc_unreachable ();
2458 }
2459
2460 /* There's no easy way to detect the case we need to set EDOM. */
2461 if (flag_errno_math && errno_set)
2462 return CODE_FOR_nothing;
2463
2464 /* Optab mode depends on the mode of the input argument. */
2465 mode = TYPE_MODE (TREE_TYPE (arg));
2466
2467 if (builtin_optab)
2468 return optab_handler (builtin_optab, mode);
2469 return CODE_FOR_nothing;
2470 }
2471
2472 /* Expand a call to one of the builtin math functions that operate on
2473 floating point argument and output an integer result (ilogb, isinf,
2474 isnan, etc).
2475 Return 0 if a normal call should be emitted rather than expanding the
2476 function in-line. EXP is the expression that is a call to the builtin
2477 function; if convenient, the result should be placed in TARGET. */
2478
2479 static rtx
2480 expand_builtin_interclass_mathfn (tree exp, rtx target)
2481 {
2482 enum insn_code icode = CODE_FOR_nothing;
2483 rtx op0;
2484 tree fndecl = get_callee_fndecl (exp);
2485 enum machine_mode mode;
2486 tree arg;
2487
2488 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2489 return NULL_RTX;
2490
2491 arg = CALL_EXPR_ARG (exp, 0);
2492 icode = interclass_mathfn_icode (arg, fndecl);
2493 mode = TYPE_MODE (TREE_TYPE (arg));
2494
2495 if (icode != CODE_FOR_nothing)
2496 {
2497 struct expand_operand ops[1];
2498 rtx last = get_last_insn ();
2499 tree orig_arg = arg;
2500
2501 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2502 need to expand the argument again. This way, we will not perform
2503 side-effects more the once. */
2504 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2505
2506 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2507
2508 if (mode != GET_MODE (op0))
2509 op0 = convert_to_mode (mode, op0, 0);
2510
2511 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2512 if (maybe_legitimize_operands (icode, 0, 1, ops)
2513 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2514 return ops[0].value;
2515
2516 delete_insns_since (last);
2517 CALL_EXPR_ARG (exp, 0) = orig_arg;
2518 }
2519
2520 return NULL_RTX;
2521 }
2522
2523 /* Expand a call to the builtin sincos math function.
2524 Return NULL_RTX if a normal call should be emitted rather than expanding the
2525 function in-line. EXP is the expression that is a call to the builtin
2526 function. */
2527
2528 static rtx
2529 expand_builtin_sincos (tree exp)
2530 {
2531 rtx op0, op1, op2, target1, target2;
2532 enum machine_mode mode;
2533 tree arg, sinp, cosp;
2534 int result;
2535 location_t loc = EXPR_LOCATION (exp);
2536 tree alias_type, alias_off;
2537
2538 if (!validate_arglist (exp, REAL_TYPE,
2539 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2540 return NULL_RTX;
2541
2542 arg = CALL_EXPR_ARG (exp, 0);
2543 sinp = CALL_EXPR_ARG (exp, 1);
2544 cosp = CALL_EXPR_ARG (exp, 2);
2545
2546 /* Make a suitable register to place result in. */
2547 mode = TYPE_MODE (TREE_TYPE (arg));
2548
2549 /* Check if sincos insn is available, otherwise emit the call. */
2550 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2551 return NULL_RTX;
2552
2553 target1 = gen_reg_rtx (mode);
2554 target2 = gen_reg_rtx (mode);
2555
2556 op0 = expand_normal (arg);
2557 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2558 alias_off = build_int_cst (alias_type, 0);
2559 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2560 sinp, alias_off));
2561 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2562 cosp, alias_off));
2563
2564 /* Compute into target1 and target2.
2565 Set TARGET to wherever the result comes back. */
2566 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2567 gcc_assert (result);
2568
2569 /* Move target1 and target2 to the memory locations indicated
2570 by op1 and op2. */
2571 emit_move_insn (op1, target1);
2572 emit_move_insn (op2, target2);
2573
2574 return const0_rtx;
2575 }
2576
2577 /* Expand a call to the internal cexpi builtin to the sincos math function.
2578 EXP is the expression that is a call to the builtin function; if convenient,
2579 the result should be placed in TARGET. */
2580
2581 static rtx
2582 expand_builtin_cexpi (tree exp, rtx target)
2583 {
2584 tree fndecl = get_callee_fndecl (exp);
2585 tree arg, type;
2586 enum machine_mode mode;
2587 rtx op0, op1, op2;
2588 location_t loc = EXPR_LOCATION (exp);
2589
2590 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2591 return NULL_RTX;
2592
2593 arg = CALL_EXPR_ARG (exp, 0);
2594 type = TREE_TYPE (arg);
2595 mode = TYPE_MODE (TREE_TYPE (arg));
2596
2597 /* Try expanding via a sincos optab, fall back to emitting a libcall
2598 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2599 is only generated from sincos, cexp or if we have either of them. */
2600 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2601 {
2602 op1 = gen_reg_rtx (mode);
2603 op2 = gen_reg_rtx (mode);
2604
2605 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2606
2607 /* Compute into op1 and op2. */
2608 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2609 }
2610 else if (targetm.libc_has_function (function_sincos))
2611 {
2612 tree call, fn = NULL_TREE;
2613 tree top1, top2;
2614 rtx op1a, op2a;
2615
2616 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2617 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2618 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2619 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2620 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2621 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2622 else
2623 gcc_unreachable ();
2624
2625 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2626 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2627 op1a = copy_addr_to_reg (XEXP (op1, 0));
2628 op2a = copy_addr_to_reg (XEXP (op2, 0));
2629 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2630 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2631
2632 /* Make sure not to fold the sincos call again. */
2633 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2634 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2635 call, 3, arg, top1, top2));
2636 }
2637 else
2638 {
2639 tree call, fn = NULL_TREE, narg;
2640 tree ctype = build_complex_type (type);
2641
2642 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2643 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2644 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2645 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2646 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2647 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2648 else
2649 gcc_unreachable ();
2650
2651 /* If we don't have a decl for cexp create one. This is the
2652 friendliest fallback if the user calls __builtin_cexpi
2653 without full target C99 function support. */
2654 if (fn == NULL_TREE)
2655 {
2656 tree fntype;
2657 const char *name = NULL;
2658
2659 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2660 name = "cexpf";
2661 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2662 name = "cexp";
2663 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2664 name = "cexpl";
2665
2666 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2667 fn = build_fn_decl (name, fntype);
2668 }
2669
2670 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2671 build_real (type, dconst0), arg);
2672
2673 /* Make sure not to fold the cexp call again. */
2674 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2675 return expand_expr (build_call_nary (ctype, call, 1, narg),
2676 target, VOIDmode, EXPAND_NORMAL);
2677 }
2678
2679 /* Now build the proper return type. */
2680 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2681 make_tree (TREE_TYPE (arg), op2),
2682 make_tree (TREE_TYPE (arg), op1)),
2683 target, VOIDmode, EXPAND_NORMAL);
2684 }
2685
2686 /* Conveniently construct a function call expression. FNDECL names the
2687 function to be called, N is the number of arguments, and the "..."
2688 parameters are the argument expressions. Unlike build_call_exr
2689 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2690
2691 static tree
2692 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2693 {
2694 va_list ap;
2695 tree fntype = TREE_TYPE (fndecl);
2696 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2697
2698 va_start (ap, n);
2699 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2700 va_end (ap);
2701 SET_EXPR_LOCATION (fn, loc);
2702 return fn;
2703 }
2704
2705 /* Expand a call to one of the builtin rounding functions gcc defines
2706 as an extension (lfloor and lceil). As these are gcc extensions we
2707 do not need to worry about setting errno to EDOM.
2708 If expanding via optab fails, lower expression to (int)(floor(x)).
2709 EXP is the expression that is a call to the builtin function;
2710 if convenient, the result should be placed in TARGET. */
2711
2712 static rtx
2713 expand_builtin_int_roundingfn (tree exp, rtx target)
2714 {
2715 convert_optab builtin_optab;
2716 rtx op0, insns, tmp;
2717 tree fndecl = get_callee_fndecl (exp);
2718 enum built_in_function fallback_fn;
2719 tree fallback_fndecl;
2720 enum machine_mode mode;
2721 tree arg;
2722
2723 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2724 gcc_unreachable ();
2725
2726 arg = CALL_EXPR_ARG (exp, 0);
2727
2728 switch (DECL_FUNCTION_CODE (fndecl))
2729 {
2730 CASE_FLT_FN (BUILT_IN_ICEIL):
2731 CASE_FLT_FN (BUILT_IN_LCEIL):
2732 CASE_FLT_FN (BUILT_IN_LLCEIL):
2733 builtin_optab = lceil_optab;
2734 fallback_fn = BUILT_IN_CEIL;
2735 break;
2736
2737 CASE_FLT_FN (BUILT_IN_IFLOOR):
2738 CASE_FLT_FN (BUILT_IN_LFLOOR):
2739 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2740 builtin_optab = lfloor_optab;
2741 fallback_fn = BUILT_IN_FLOOR;
2742 break;
2743
2744 default:
2745 gcc_unreachable ();
2746 }
2747
2748 /* Make a suitable register to place result in. */
2749 mode = TYPE_MODE (TREE_TYPE (exp));
2750
2751 target = gen_reg_rtx (mode);
2752
2753 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2754 need to expand the argument again. This way, we will not perform
2755 side-effects more the once. */
2756 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2757
2758 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2759
2760 start_sequence ();
2761
2762 /* Compute into TARGET. */
2763 if (expand_sfix_optab (target, op0, builtin_optab))
2764 {
2765 /* Output the entire sequence. */
2766 insns = get_insns ();
2767 end_sequence ();
2768 emit_insn (insns);
2769 return target;
2770 }
2771
2772 /* If we were unable to expand via the builtin, stop the sequence
2773 (without outputting the insns). */
2774 end_sequence ();
2775
2776 /* Fall back to floating point rounding optab. */
2777 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2778
2779 /* For non-C99 targets we may end up without a fallback fndecl here
2780 if the user called __builtin_lfloor directly. In this case emit
2781 a call to the floor/ceil variants nevertheless. This should result
2782 in the best user experience for not full C99 targets. */
2783 if (fallback_fndecl == NULL_TREE)
2784 {
2785 tree fntype;
2786 const char *name = NULL;
2787
2788 switch (DECL_FUNCTION_CODE (fndecl))
2789 {
2790 case BUILT_IN_ICEIL:
2791 case BUILT_IN_LCEIL:
2792 case BUILT_IN_LLCEIL:
2793 name = "ceil";
2794 break;
2795 case BUILT_IN_ICEILF:
2796 case BUILT_IN_LCEILF:
2797 case BUILT_IN_LLCEILF:
2798 name = "ceilf";
2799 break;
2800 case BUILT_IN_ICEILL:
2801 case BUILT_IN_LCEILL:
2802 case BUILT_IN_LLCEILL:
2803 name = "ceill";
2804 break;
2805 case BUILT_IN_IFLOOR:
2806 case BUILT_IN_LFLOOR:
2807 case BUILT_IN_LLFLOOR:
2808 name = "floor";
2809 break;
2810 case BUILT_IN_IFLOORF:
2811 case BUILT_IN_LFLOORF:
2812 case BUILT_IN_LLFLOORF:
2813 name = "floorf";
2814 break;
2815 case BUILT_IN_IFLOORL:
2816 case BUILT_IN_LFLOORL:
2817 case BUILT_IN_LLFLOORL:
2818 name = "floorl";
2819 break;
2820 default:
2821 gcc_unreachable ();
2822 }
2823
2824 fntype = build_function_type_list (TREE_TYPE (arg),
2825 TREE_TYPE (arg), NULL_TREE);
2826 fallback_fndecl = build_fn_decl (name, fntype);
2827 }
2828
2829 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2830
2831 tmp = expand_normal (exp);
2832 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2833
2834 /* Truncate the result of floating point optab to integer
2835 via expand_fix (). */
2836 target = gen_reg_rtx (mode);
2837 expand_fix (target, tmp, 0);
2838
2839 return target;
2840 }
2841
2842 /* Expand a call to one of the builtin math functions doing integer
2843 conversion (lrint).
2844 Return 0 if a normal call should be emitted rather than expanding the
2845 function in-line. EXP is the expression that is a call to the builtin
2846 function; if convenient, the result should be placed in TARGET. */
2847
2848 static rtx
2849 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2850 {
2851 convert_optab builtin_optab;
2852 rtx op0, insns;
2853 tree fndecl = get_callee_fndecl (exp);
2854 tree arg;
2855 enum machine_mode mode;
2856 enum built_in_function fallback_fn = BUILT_IN_NONE;
2857
2858 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2859 gcc_unreachable ();
2860
2861 arg = CALL_EXPR_ARG (exp, 0);
2862
2863 switch (DECL_FUNCTION_CODE (fndecl))
2864 {
2865 CASE_FLT_FN (BUILT_IN_IRINT):
2866 fallback_fn = BUILT_IN_LRINT;
2867 /* FALLTHRU */
2868 CASE_FLT_FN (BUILT_IN_LRINT):
2869 CASE_FLT_FN (BUILT_IN_LLRINT):
2870 builtin_optab = lrint_optab;
2871 break;
2872
2873 CASE_FLT_FN (BUILT_IN_IROUND):
2874 fallback_fn = BUILT_IN_LROUND;
2875 /* FALLTHRU */
2876 CASE_FLT_FN (BUILT_IN_LROUND):
2877 CASE_FLT_FN (BUILT_IN_LLROUND):
2878 builtin_optab = lround_optab;
2879 break;
2880
2881 default:
2882 gcc_unreachable ();
2883 }
2884
2885 /* There's no easy way to detect the case we need to set EDOM. */
2886 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2887 return NULL_RTX;
2888
2889 /* Make a suitable register to place result in. */
2890 mode = TYPE_MODE (TREE_TYPE (exp));
2891
2892 /* There's no easy way to detect the case we need to set EDOM. */
2893 if (!flag_errno_math)
2894 {
2895 rtx result = gen_reg_rtx (mode);
2896
2897 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2898 need to expand the argument again. This way, we will not perform
2899 side-effects more the once. */
2900 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2901
2902 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2903
2904 start_sequence ();
2905
2906 if (expand_sfix_optab (result, op0, builtin_optab))
2907 {
2908 /* Output the entire sequence. */
2909 insns = get_insns ();
2910 end_sequence ();
2911 emit_insn (insns);
2912 return result;
2913 }
2914
2915 /* If we were unable to expand via the builtin, stop the sequence
2916 (without outputting the insns) and call to the library function
2917 with the stabilized argument list. */
2918 end_sequence ();
2919 }
2920
2921 if (fallback_fn != BUILT_IN_NONE)
2922 {
2923 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2924 targets, (int) round (x) should never be transformed into
2925 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2926 a call to lround in the hope that the target provides at least some
2927 C99 functions. This should result in the best user experience for
2928 not full C99 targets. */
2929 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2930 fallback_fn, 0);
2931
2932 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2933 fallback_fndecl, 1, arg);
2934
2935 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2936 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2937 return convert_to_mode (mode, target, 0);
2938 }
2939
2940 return expand_call (exp, target, target == const0_rtx);
2941 }
2942
2943 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2944 a normal call should be emitted rather than expanding the function
2945 in-line. EXP is the expression that is a call to the builtin
2946 function; if convenient, the result should be placed in TARGET. */
2947
2948 static rtx
2949 expand_builtin_powi (tree exp, rtx target)
2950 {
2951 tree arg0, arg1;
2952 rtx op0, op1;
2953 enum machine_mode mode;
2954 enum machine_mode mode2;
2955
2956 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2957 return NULL_RTX;
2958
2959 arg0 = CALL_EXPR_ARG (exp, 0);
2960 arg1 = CALL_EXPR_ARG (exp, 1);
2961 mode = TYPE_MODE (TREE_TYPE (exp));
2962
2963 /* Emit a libcall to libgcc. */
2964
2965 /* Mode of the 2nd argument must match that of an int. */
2966 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2967
2968 if (target == NULL_RTX)
2969 target = gen_reg_rtx (mode);
2970
2971 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2972 if (GET_MODE (op0) != mode)
2973 op0 = convert_to_mode (mode, op0, 0);
2974 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2975 if (GET_MODE (op1) != mode2)
2976 op1 = convert_to_mode (mode2, op1, 0);
2977
2978 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2979 target, LCT_CONST, mode, 2,
2980 op0, mode, op1, mode2);
2981
2982 return target;
2983 }
2984
2985 /* Expand expression EXP which is a call to the strlen builtin. Return
2986 NULL_RTX if we failed the caller should emit a normal call, otherwise
2987 try to get the result in TARGET, if convenient. */
2988
2989 static rtx
2990 expand_builtin_strlen (tree exp, rtx target,
2991 enum machine_mode target_mode)
2992 {
2993 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2994 return NULL_RTX;
2995 else
2996 {
2997 struct expand_operand ops[4];
2998 rtx pat;
2999 tree len;
3000 tree src = CALL_EXPR_ARG (exp, 0);
3001 rtx src_reg, before_strlen;
3002 enum machine_mode insn_mode = target_mode;
3003 enum insn_code icode = CODE_FOR_nothing;
3004 unsigned int align;
3005
3006 /* If the length can be computed at compile-time, return it. */
3007 len = c_strlen (src, 0);
3008 if (len)
3009 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3010
3011 /* If the length can be computed at compile-time and is constant
3012 integer, but there are side-effects in src, evaluate
3013 src for side-effects, then return len.
3014 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3015 can be optimized into: i++; x = 3; */
3016 len = c_strlen (src, 1);
3017 if (len && TREE_CODE (len) == INTEGER_CST)
3018 {
3019 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3020 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3021 }
3022
3023 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3024
3025 /* If SRC is not a pointer type, don't do this operation inline. */
3026 if (align == 0)
3027 return NULL_RTX;
3028
3029 /* Bail out if we can't compute strlen in the right mode. */
3030 while (insn_mode != VOIDmode)
3031 {
3032 icode = optab_handler (strlen_optab, insn_mode);
3033 if (icode != CODE_FOR_nothing)
3034 break;
3035
3036 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3037 }
3038 if (insn_mode == VOIDmode)
3039 return NULL_RTX;
3040
3041 /* Make a place to hold the source address. We will not expand
3042 the actual source until we are sure that the expansion will
3043 not fail -- there are trees that cannot be expanded twice. */
3044 src_reg = gen_reg_rtx (Pmode);
3045
3046 /* Mark the beginning of the strlen sequence so we can emit the
3047 source operand later. */
3048 before_strlen = get_last_insn ();
3049
3050 create_output_operand (&ops[0], target, insn_mode);
3051 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3052 create_integer_operand (&ops[2], 0);
3053 create_integer_operand (&ops[3], align);
3054 if (!maybe_expand_insn (icode, 4, ops))
3055 return NULL_RTX;
3056
3057 /* Now that we are assured of success, expand the source. */
3058 start_sequence ();
3059 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3060 if (pat != src_reg)
3061 {
3062 #ifdef POINTERS_EXTEND_UNSIGNED
3063 if (GET_MODE (pat) != Pmode)
3064 pat = convert_to_mode (Pmode, pat,
3065 POINTERS_EXTEND_UNSIGNED);
3066 #endif
3067 emit_move_insn (src_reg, pat);
3068 }
3069 pat = get_insns ();
3070 end_sequence ();
3071
3072 if (before_strlen)
3073 emit_insn_after (pat, before_strlen);
3074 else
3075 emit_insn_before (pat, get_insns ());
3076
3077 /* Return the value in the proper mode for this function. */
3078 if (GET_MODE (ops[0].value) == target_mode)
3079 target = ops[0].value;
3080 else if (target != 0)
3081 convert_move (target, ops[0].value, 0);
3082 else
3083 target = convert_to_mode (target_mode, ops[0].value, 0);
3084
3085 return target;
3086 }
3087 }
3088
3089 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3090 bytes from constant string DATA + OFFSET and return it as target
3091 constant. */
3092
3093 static rtx
3094 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3095 enum machine_mode mode)
3096 {
3097 const char *str = (const char *) data;
3098
3099 gcc_assert (offset >= 0
3100 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3101 <= strlen (str) + 1));
3102
3103 return c_readstr (str + offset, mode);
3104 }
3105
3106 /* LEN specify length of the block of memcpy/memset operation.
3107 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3108 In some cases we can make very likely guess on max size, then we
3109 set it into PROBABLE_MAX_SIZE. */
3110
3111 static void
3112 determine_block_size (tree len, rtx len_rtx,
3113 unsigned HOST_WIDE_INT *min_size,
3114 unsigned HOST_WIDE_INT *max_size,
3115 unsigned HOST_WIDE_INT *probable_max_size)
3116 {
3117 if (CONST_INT_P (len_rtx))
3118 {
3119 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3120 return;
3121 }
3122 else
3123 {
3124 double_int min, max;
3125 enum value_range_type range_type = VR_UNDEFINED;
3126
3127 /* Determine bounds from the type. */
3128 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3129 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3130 else
3131 *min_size = 0;
3132 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3133 *probable_max_size = *max_size
3134 = 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 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3395 return NULL_RTX;
3396
3397 if (endp && target != const0_rtx)
3398 {
3399 target = ops[0].value;
3400 /* movstr is supposed to set end to the address of the NUL
3401 terminator. If the caller requested a mempcpy-like return value,
3402 adjust it. */
3403 if (endp == 1)
3404 {
3405 rtx tem = plus_constant (GET_MODE (target),
3406 gen_lowpart (GET_MODE (target), target), 1);
3407 emit_move_insn (target, force_operand (tem, NULL_RTX));
3408 }
3409 }
3410 return target;
3411 }
3412
3413 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3414 NULL_RTX if we failed the caller should emit a normal call, otherwise
3415 try to get the result in TARGET, if convenient (and in mode MODE if that's
3416 convenient). */
3417
3418 static rtx
3419 expand_builtin_strcpy (tree exp, rtx target)
3420 {
3421 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3422 {
3423 tree dest = CALL_EXPR_ARG (exp, 0);
3424 tree src = CALL_EXPR_ARG (exp, 1);
3425 return expand_builtin_strcpy_args (dest, src, target);
3426 }
3427 return NULL_RTX;
3428 }
3429
3430 /* Helper function to do the actual work for expand_builtin_strcpy. The
3431 arguments to the builtin_strcpy call DEST and SRC are broken out
3432 so that this can also be called without constructing an actual CALL_EXPR.
3433 The other arguments and return value are the same as for
3434 expand_builtin_strcpy. */
3435
3436 static rtx
3437 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3438 {
3439 return expand_movstr (dest, src, target, /*endp=*/0);
3440 }
3441
3442 /* Expand a call EXP to the stpcpy builtin.
3443 Return NULL_RTX if we failed the caller should emit a normal call,
3444 otherwise try to get the result in TARGET, if convenient (and in
3445 mode MODE if that's convenient). */
3446
3447 static rtx
3448 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3449 {
3450 tree dst, src;
3451 location_t loc = EXPR_LOCATION (exp);
3452
3453 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3454 return NULL_RTX;
3455
3456 dst = CALL_EXPR_ARG (exp, 0);
3457 src = CALL_EXPR_ARG (exp, 1);
3458
3459 /* If return value is ignored, transform stpcpy into strcpy. */
3460 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3461 {
3462 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3463 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3464 return expand_expr (result, target, mode, EXPAND_NORMAL);
3465 }
3466 else
3467 {
3468 tree len, lenp1;
3469 rtx ret;
3470
3471 /* Ensure we get an actual string whose length can be evaluated at
3472 compile-time, not an expression containing a string. This is
3473 because the latter will potentially produce pessimized code
3474 when used to produce the return value. */
3475 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3476 return expand_movstr (dst, src, target, /*endp=*/2);
3477
3478 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3479 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3480 target, mode, /*endp=*/2);
3481
3482 if (ret)
3483 return ret;
3484
3485 if (TREE_CODE (len) == INTEGER_CST)
3486 {
3487 rtx len_rtx = expand_normal (len);
3488
3489 if (CONST_INT_P (len_rtx))
3490 {
3491 ret = expand_builtin_strcpy_args (dst, src, target);
3492
3493 if (ret)
3494 {
3495 if (! target)
3496 {
3497 if (mode != VOIDmode)
3498 target = gen_reg_rtx (mode);
3499 else
3500 target = gen_reg_rtx (GET_MODE (ret));
3501 }
3502 if (GET_MODE (target) != GET_MODE (ret))
3503 ret = gen_lowpart (GET_MODE (target), ret);
3504
3505 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3506 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3507 gcc_assert (ret);
3508
3509 return target;
3510 }
3511 }
3512 }
3513
3514 return expand_movstr (dst, src, target, /*endp=*/2);
3515 }
3516 }
3517
3518 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3519 bytes from constant string DATA + OFFSET and return it as target
3520 constant. */
3521
3522 rtx
3523 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3524 enum machine_mode mode)
3525 {
3526 const char *str = (const char *) data;
3527
3528 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3529 return const0_rtx;
3530
3531 return c_readstr (str + offset, mode);
3532 }
3533
3534 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3535 NULL_RTX if we failed the caller should emit a normal call. */
3536
3537 static rtx
3538 expand_builtin_strncpy (tree exp, rtx target)
3539 {
3540 location_t loc = EXPR_LOCATION (exp);
3541
3542 if (validate_arglist (exp,
3543 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3544 {
3545 tree dest = CALL_EXPR_ARG (exp, 0);
3546 tree src = CALL_EXPR_ARG (exp, 1);
3547 tree len = CALL_EXPR_ARG (exp, 2);
3548 tree slen = c_strlen (src, 1);
3549
3550 /* We must be passed a constant len and src parameter. */
3551 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3552 return NULL_RTX;
3553
3554 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3555
3556 /* We're required to pad with trailing zeros if the requested
3557 len is greater than strlen(s2)+1. In that case try to
3558 use store_by_pieces, if it fails, punt. */
3559 if (tree_int_cst_lt (slen, len))
3560 {
3561 unsigned int dest_align = get_pointer_alignment (dest);
3562 const char *p = c_getstr (src);
3563 rtx dest_mem;
3564
3565 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3566 || !can_store_by_pieces (tree_to_uhwi (len),
3567 builtin_strncpy_read_str,
3568 CONST_CAST (char *, p),
3569 dest_align, false))
3570 return NULL_RTX;
3571
3572 dest_mem = get_memory_rtx (dest, len);
3573 store_by_pieces (dest_mem, tree_to_uhwi (len),
3574 builtin_strncpy_read_str,
3575 CONST_CAST (char *, p), dest_align, false, 0);
3576 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3577 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3578 return dest_mem;
3579 }
3580 }
3581 return NULL_RTX;
3582 }
3583
3584 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3585 bytes from constant string DATA + OFFSET and return it as target
3586 constant. */
3587
3588 rtx
3589 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3590 enum machine_mode mode)
3591 {
3592 const char *c = (const char *) data;
3593 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3594
3595 memset (p, *c, GET_MODE_SIZE (mode));
3596
3597 return c_readstr (p, mode);
3598 }
3599
3600 /* Callback routine for store_by_pieces. Return the RTL of a register
3601 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3602 char value given in the RTL register data. For example, if mode is
3603 4 bytes wide, return the RTL for 0x01010101*data. */
3604
3605 static rtx
3606 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3607 enum machine_mode mode)
3608 {
3609 rtx target, coeff;
3610 size_t size;
3611 char *p;
3612
3613 size = GET_MODE_SIZE (mode);
3614 if (size == 1)
3615 return (rtx) data;
3616
3617 p = XALLOCAVEC (char, size);
3618 memset (p, 1, size);
3619 coeff = c_readstr (p, mode);
3620
3621 target = convert_to_mode (mode, (rtx) data, 1);
3622 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3623 return force_reg (mode, target);
3624 }
3625
3626 /* Expand expression EXP, which is a call to the memset builtin. Return
3627 NULL_RTX if we failed the caller should emit a normal call, otherwise
3628 try to get the result in TARGET, if convenient (and in mode MODE if that's
3629 convenient). */
3630
3631 static rtx
3632 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3633 {
3634 if (!validate_arglist (exp,
3635 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3636 return NULL_RTX;
3637 else
3638 {
3639 tree dest = CALL_EXPR_ARG (exp, 0);
3640 tree val = CALL_EXPR_ARG (exp, 1);
3641 tree len = CALL_EXPR_ARG (exp, 2);
3642 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3643 }
3644 }
3645
3646 /* Helper function to do the actual work for expand_builtin_memset. The
3647 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3648 so that this can also be called without constructing an actual CALL_EXPR.
3649 The other arguments and return value are the same as for
3650 expand_builtin_memset. */
3651
3652 static rtx
3653 expand_builtin_memset_args (tree dest, tree val, tree len,
3654 rtx target, enum machine_mode mode, tree orig_exp)
3655 {
3656 tree fndecl, fn;
3657 enum built_in_function fcode;
3658 enum machine_mode val_mode;
3659 char c;
3660 unsigned int dest_align;
3661 rtx dest_mem, dest_addr, len_rtx;
3662 HOST_WIDE_INT expected_size = -1;
3663 unsigned int expected_align = 0;
3664 unsigned HOST_WIDE_INT min_size;
3665 unsigned HOST_WIDE_INT max_size;
3666 unsigned HOST_WIDE_INT probable_max_size;
3667
3668 dest_align = get_pointer_alignment (dest);
3669
3670 /* If DEST is not a pointer type, don't do this operation in-line. */
3671 if (dest_align == 0)
3672 return NULL_RTX;
3673
3674 if (currently_expanding_gimple_stmt)
3675 stringop_block_profile (currently_expanding_gimple_stmt,
3676 &expected_align, &expected_size);
3677
3678 if (expected_align < dest_align)
3679 expected_align = dest_align;
3680
3681 /* If the LEN parameter is zero, return DEST. */
3682 if (integer_zerop (len))
3683 {
3684 /* Evaluate and ignore VAL in case it has side-effects. */
3685 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3686 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3687 }
3688
3689 /* Stabilize the arguments in case we fail. */
3690 dest = builtin_save_expr (dest);
3691 val = builtin_save_expr (val);
3692 len = builtin_save_expr (len);
3693
3694 len_rtx = expand_normal (len);
3695 determine_block_size (len, len_rtx, &min_size, &max_size,
3696 &probable_max_size);
3697 dest_mem = get_memory_rtx (dest, len);
3698 val_mode = TYPE_MODE (unsigned_char_type_node);
3699
3700 if (TREE_CODE (val) != INTEGER_CST)
3701 {
3702 rtx val_rtx;
3703
3704 val_rtx = expand_normal (val);
3705 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3706
3707 /* Assume that we can memset by pieces if we can store
3708 * the coefficients by pieces (in the required modes).
3709 * We can't pass builtin_memset_gen_str as that emits RTL. */
3710 c = 1;
3711 if (tree_fits_uhwi_p (len)
3712 && can_store_by_pieces (tree_to_uhwi (len),
3713 builtin_memset_read_str, &c, dest_align,
3714 true))
3715 {
3716 val_rtx = force_reg (val_mode, val_rtx);
3717 store_by_pieces (dest_mem, tree_to_uhwi (len),
3718 builtin_memset_gen_str, val_rtx, dest_align,
3719 true, 0);
3720 }
3721 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3722 dest_align, expected_align,
3723 expected_size, min_size, max_size,
3724 probable_max_size))
3725 goto do_libcall;
3726
3727 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3728 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3729 return dest_mem;
3730 }
3731
3732 if (target_char_cast (val, &c))
3733 goto do_libcall;
3734
3735 if (c)
3736 {
3737 if (tree_fits_uhwi_p (len)
3738 && can_store_by_pieces (tree_to_uhwi (len),
3739 builtin_memset_read_str, &c, dest_align,
3740 true))
3741 store_by_pieces (dest_mem, tree_to_uhwi (len),
3742 builtin_memset_read_str, &c, dest_align, true, 0);
3743 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3744 gen_int_mode (c, val_mode),
3745 dest_align, expected_align,
3746 expected_size, min_size, max_size,
3747 probable_max_size))
3748 goto do_libcall;
3749
3750 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3751 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3752 return dest_mem;
3753 }
3754
3755 set_mem_align (dest_mem, dest_align);
3756 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3757 CALL_EXPR_TAILCALL (orig_exp)
3758 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3759 expected_align, expected_size,
3760 min_size, max_size,
3761 probable_max_size);
3762
3763 if (dest_addr == 0)
3764 {
3765 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3766 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3767 }
3768
3769 return dest_addr;
3770
3771 do_libcall:
3772 fndecl = get_callee_fndecl (orig_exp);
3773 fcode = DECL_FUNCTION_CODE (fndecl);
3774 if (fcode == BUILT_IN_MEMSET)
3775 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3776 dest, val, len);
3777 else if (fcode == BUILT_IN_BZERO)
3778 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3779 dest, len);
3780 else
3781 gcc_unreachable ();
3782 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3783 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3784 return expand_call (fn, target, target == const0_rtx);
3785 }
3786
3787 /* Expand expression EXP, which is a call to the bzero builtin. Return
3788 NULL_RTX if we failed the caller should emit a normal call. */
3789
3790 static rtx
3791 expand_builtin_bzero (tree exp)
3792 {
3793 tree dest, size;
3794 location_t loc = EXPR_LOCATION (exp);
3795
3796 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3797 return NULL_RTX;
3798
3799 dest = CALL_EXPR_ARG (exp, 0);
3800 size = CALL_EXPR_ARG (exp, 1);
3801
3802 /* New argument list transforming bzero(ptr x, int y) to
3803 memset(ptr x, int 0, size_t y). This is done this way
3804 so that if it isn't expanded inline, we fallback to
3805 calling bzero instead of memset. */
3806
3807 return expand_builtin_memset_args (dest, integer_zero_node,
3808 fold_convert_loc (loc,
3809 size_type_node, size),
3810 const0_rtx, VOIDmode, exp);
3811 }
3812
3813 /* Expand expression EXP, which is a call to the memcmp built-in function.
3814 Return NULL_RTX if we failed and the caller should emit a normal call,
3815 otherwise try to get the result in TARGET, if convenient (and in mode
3816 MODE, if that's convenient). */
3817
3818 static rtx
3819 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3820 ATTRIBUTE_UNUSED enum machine_mode mode)
3821 {
3822 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3823
3824 if (!validate_arglist (exp,
3825 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3826 return NULL_RTX;
3827
3828 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3829 implementing memcmp because it will stop if it encounters two
3830 zero bytes. */
3831 #if defined HAVE_cmpmemsi
3832 {
3833 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3834 rtx result;
3835 rtx insn;
3836 tree arg1 = CALL_EXPR_ARG (exp, 0);
3837 tree arg2 = CALL_EXPR_ARG (exp, 1);
3838 tree len = CALL_EXPR_ARG (exp, 2);
3839
3840 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3841 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3842 enum machine_mode insn_mode;
3843
3844 if (HAVE_cmpmemsi)
3845 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3846 else
3847 return NULL_RTX;
3848
3849 /* If we don't have POINTER_TYPE, call the function. */
3850 if (arg1_align == 0 || arg2_align == 0)
3851 return NULL_RTX;
3852
3853 /* Make a place to write the result of the instruction. */
3854 result = target;
3855 if (! (result != 0
3856 && REG_P (result) && GET_MODE (result) == insn_mode
3857 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3858 result = gen_reg_rtx (insn_mode);
3859
3860 arg1_rtx = get_memory_rtx (arg1, len);
3861 arg2_rtx = get_memory_rtx (arg2, len);
3862 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3863
3864 /* Set MEM_SIZE as appropriate. */
3865 if (CONST_INT_P (arg3_rtx))
3866 {
3867 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3868 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3869 }
3870
3871 if (HAVE_cmpmemsi)
3872 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3873 GEN_INT (MIN (arg1_align, arg2_align)));
3874 else
3875 gcc_unreachable ();
3876
3877 if (insn)
3878 emit_insn (insn);
3879 else
3880 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3881 TYPE_MODE (integer_type_node), 3,
3882 XEXP (arg1_rtx, 0), Pmode,
3883 XEXP (arg2_rtx, 0), Pmode,
3884 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3885 TYPE_UNSIGNED (sizetype)),
3886 TYPE_MODE (sizetype));
3887
3888 /* Return the value in the proper mode for this function. */
3889 mode = TYPE_MODE (TREE_TYPE (exp));
3890 if (GET_MODE (result) == mode)
3891 return result;
3892 else if (target != 0)
3893 {
3894 convert_move (target, result, 0);
3895 return target;
3896 }
3897 else
3898 return convert_to_mode (mode, result, 0);
3899 }
3900 #endif /* HAVE_cmpmemsi. */
3901
3902 return NULL_RTX;
3903 }
3904
3905 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3906 if we failed the caller should emit a normal call, otherwise try to get
3907 the result in TARGET, if convenient. */
3908
3909 static rtx
3910 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3911 {
3912 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3913 return NULL_RTX;
3914
3915 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3916 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3917 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3918 {
3919 rtx arg1_rtx, arg2_rtx;
3920 rtx result, insn = NULL_RTX;
3921 tree fndecl, fn;
3922 tree arg1 = CALL_EXPR_ARG (exp, 0);
3923 tree arg2 = CALL_EXPR_ARG (exp, 1);
3924
3925 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3926 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3927
3928 /* If we don't have POINTER_TYPE, call the function. */
3929 if (arg1_align == 0 || arg2_align == 0)
3930 return NULL_RTX;
3931
3932 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3933 arg1 = builtin_save_expr (arg1);
3934 arg2 = builtin_save_expr (arg2);
3935
3936 arg1_rtx = get_memory_rtx (arg1, NULL);
3937 arg2_rtx = get_memory_rtx (arg2, NULL);
3938
3939 #ifdef HAVE_cmpstrsi
3940 /* Try to call cmpstrsi. */
3941 if (HAVE_cmpstrsi)
3942 {
3943 enum machine_mode insn_mode
3944 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3945
3946 /* Make a place to write the result of the instruction. */
3947 result = target;
3948 if (! (result != 0
3949 && REG_P (result) && GET_MODE (result) == insn_mode
3950 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3951 result = gen_reg_rtx (insn_mode);
3952
3953 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3954 GEN_INT (MIN (arg1_align, arg2_align)));
3955 }
3956 #endif
3957 #ifdef HAVE_cmpstrnsi
3958 /* Try to determine at least one length and call cmpstrnsi. */
3959 if (!insn && HAVE_cmpstrnsi)
3960 {
3961 tree len;
3962 rtx arg3_rtx;
3963
3964 enum machine_mode insn_mode
3965 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3966 tree len1 = c_strlen (arg1, 1);
3967 tree len2 = c_strlen (arg2, 1);
3968
3969 if (len1)
3970 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3971 if (len2)
3972 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3973
3974 /* If we don't have a constant length for the first, use the length
3975 of the second, if we know it. We don't require a constant for
3976 this case; some cost analysis could be done if both are available
3977 but neither is constant. For now, assume they're equally cheap,
3978 unless one has side effects. If both strings have constant lengths,
3979 use the smaller. */
3980
3981 if (!len1)
3982 len = len2;
3983 else if (!len2)
3984 len = len1;
3985 else if (TREE_SIDE_EFFECTS (len1))
3986 len = len2;
3987 else if (TREE_SIDE_EFFECTS (len2))
3988 len = len1;
3989 else if (TREE_CODE (len1) != INTEGER_CST)
3990 len = len2;
3991 else if (TREE_CODE (len2) != INTEGER_CST)
3992 len = len1;
3993 else if (tree_int_cst_lt (len1, len2))
3994 len = len1;
3995 else
3996 len = len2;
3997
3998 /* If both arguments have side effects, we cannot optimize. */
3999 if (!len || TREE_SIDE_EFFECTS (len))
4000 goto do_libcall;
4001
4002 arg3_rtx = expand_normal (len);
4003
4004 /* Make a place to write the result of the instruction. */
4005 result = target;
4006 if (! (result != 0
4007 && REG_P (result) && GET_MODE (result) == insn_mode
4008 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4009 result = gen_reg_rtx (insn_mode);
4010
4011 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4012 GEN_INT (MIN (arg1_align, arg2_align)));
4013 }
4014 #endif
4015
4016 if (insn)
4017 {
4018 enum machine_mode mode;
4019 emit_insn (insn);
4020
4021 /* Return the value in the proper mode for this function. */
4022 mode = TYPE_MODE (TREE_TYPE (exp));
4023 if (GET_MODE (result) == mode)
4024 return result;
4025 if (target == 0)
4026 return convert_to_mode (mode, result, 0);
4027 convert_move (target, result, 0);
4028 return target;
4029 }
4030
4031 /* Expand the library call ourselves using a stabilized argument
4032 list to avoid re-evaluating the function's arguments twice. */
4033 #ifdef HAVE_cmpstrnsi
4034 do_libcall:
4035 #endif
4036 fndecl = get_callee_fndecl (exp);
4037 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4038 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4039 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4040 return expand_call (fn, target, target == const0_rtx);
4041 }
4042 #endif
4043 return NULL_RTX;
4044 }
4045
4046 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4047 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4048 the result in TARGET, if convenient. */
4049
4050 static rtx
4051 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4052 ATTRIBUTE_UNUSED enum machine_mode mode)
4053 {
4054 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4055
4056 if (!validate_arglist (exp,
4057 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4058 return NULL_RTX;
4059
4060 /* If c_strlen can determine an expression for one of the string
4061 lengths, and it doesn't have side effects, then emit cmpstrnsi
4062 using length MIN(strlen(string)+1, arg3). */
4063 #ifdef HAVE_cmpstrnsi
4064 if (HAVE_cmpstrnsi)
4065 {
4066 tree len, len1, len2;
4067 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4068 rtx result, insn;
4069 tree fndecl, fn;
4070 tree arg1 = CALL_EXPR_ARG (exp, 0);
4071 tree arg2 = CALL_EXPR_ARG (exp, 1);
4072 tree arg3 = CALL_EXPR_ARG (exp, 2);
4073
4074 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4075 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4076 enum machine_mode insn_mode
4077 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4078
4079 len1 = c_strlen (arg1, 1);
4080 len2 = c_strlen (arg2, 1);
4081
4082 if (len1)
4083 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4084 if (len2)
4085 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4086
4087 /* If we don't have a constant length for the first, use the length
4088 of the second, if we know it. We don't require a constant for
4089 this case; some cost analysis could be done if both are available
4090 but neither is constant. For now, assume they're equally cheap,
4091 unless one has side effects. If both strings have constant lengths,
4092 use the smaller. */
4093
4094 if (!len1)
4095 len = len2;
4096 else if (!len2)
4097 len = len1;
4098 else if (TREE_SIDE_EFFECTS (len1))
4099 len = len2;
4100 else if (TREE_SIDE_EFFECTS (len2))
4101 len = len1;
4102 else if (TREE_CODE (len1) != INTEGER_CST)
4103 len = len2;
4104 else if (TREE_CODE (len2) != INTEGER_CST)
4105 len = len1;
4106 else if (tree_int_cst_lt (len1, len2))
4107 len = len1;
4108 else
4109 len = len2;
4110
4111 /* If both arguments have side effects, we cannot optimize. */
4112 if (!len || TREE_SIDE_EFFECTS (len))
4113 return NULL_RTX;
4114
4115 /* The actual new length parameter is MIN(len,arg3). */
4116 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4117 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4118
4119 /* If we don't have POINTER_TYPE, call the function. */
4120 if (arg1_align == 0 || arg2_align == 0)
4121 return NULL_RTX;
4122
4123 /* Make a place to write the result of the instruction. */
4124 result = target;
4125 if (! (result != 0
4126 && REG_P (result) && GET_MODE (result) == insn_mode
4127 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4128 result = gen_reg_rtx (insn_mode);
4129
4130 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4131 arg1 = builtin_save_expr (arg1);
4132 arg2 = builtin_save_expr (arg2);
4133 len = builtin_save_expr (len);
4134
4135 arg1_rtx = get_memory_rtx (arg1, len);
4136 arg2_rtx = get_memory_rtx (arg2, len);
4137 arg3_rtx = expand_normal (len);
4138 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4139 GEN_INT (MIN (arg1_align, arg2_align)));
4140 if (insn)
4141 {
4142 emit_insn (insn);
4143
4144 /* Return the value in the proper mode for this function. */
4145 mode = TYPE_MODE (TREE_TYPE (exp));
4146 if (GET_MODE (result) == mode)
4147 return result;
4148 if (target == 0)
4149 return convert_to_mode (mode, result, 0);
4150 convert_move (target, result, 0);
4151 return target;
4152 }
4153
4154 /* Expand the library call ourselves using a stabilized argument
4155 list to avoid re-evaluating the function's arguments twice. */
4156 fndecl = get_callee_fndecl (exp);
4157 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4158 arg1, arg2, len);
4159 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4160 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4161 return expand_call (fn, target, target == const0_rtx);
4162 }
4163 #endif
4164 return NULL_RTX;
4165 }
4166
4167 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4168 if that's convenient. */
4169
4170 rtx
4171 expand_builtin_saveregs (void)
4172 {
4173 rtx val, seq;
4174
4175 /* Don't do __builtin_saveregs more than once in a function.
4176 Save the result of the first call and reuse it. */
4177 if (saveregs_value != 0)
4178 return saveregs_value;
4179
4180 /* When this function is called, it means that registers must be
4181 saved on entry to this function. So we migrate the call to the
4182 first insn of this function. */
4183
4184 start_sequence ();
4185
4186 /* Do whatever the machine needs done in this case. */
4187 val = targetm.calls.expand_builtin_saveregs ();
4188
4189 seq = get_insns ();
4190 end_sequence ();
4191
4192 saveregs_value = val;
4193
4194 /* Put the insns after the NOTE that starts the function. If this
4195 is inside a start_sequence, make the outer-level insn chain current, so
4196 the code is placed at the start of the function. */
4197 push_topmost_sequence ();
4198 emit_insn_after (seq, entry_of_function ());
4199 pop_topmost_sequence ();
4200
4201 return val;
4202 }
4203
4204 /* Expand a call to __builtin_next_arg. */
4205
4206 static rtx
4207 expand_builtin_next_arg (void)
4208 {
4209 /* Checking arguments is already done in fold_builtin_next_arg
4210 that must be called before this function. */
4211 return expand_binop (ptr_mode, add_optab,
4212 crtl->args.internal_arg_pointer,
4213 crtl->args.arg_offset_rtx,
4214 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4215 }
4216
4217 /* Make it easier for the backends by protecting the valist argument
4218 from multiple evaluations. */
4219
4220 static tree
4221 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4222 {
4223 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4224
4225 /* The current way of determining the type of valist is completely
4226 bogus. We should have the information on the va builtin instead. */
4227 if (!vatype)
4228 vatype = targetm.fn_abi_va_list (cfun->decl);
4229
4230 if (TREE_CODE (vatype) == ARRAY_TYPE)
4231 {
4232 if (TREE_SIDE_EFFECTS (valist))
4233 valist = save_expr (valist);
4234
4235 /* For this case, the backends will be expecting a pointer to
4236 vatype, but it's possible we've actually been given an array
4237 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4238 So fix it. */
4239 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4240 {
4241 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4242 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4243 }
4244 }
4245 else
4246 {
4247 tree pt = build_pointer_type (vatype);
4248
4249 if (! needs_lvalue)
4250 {
4251 if (! TREE_SIDE_EFFECTS (valist))
4252 return valist;
4253
4254 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4255 TREE_SIDE_EFFECTS (valist) = 1;
4256 }
4257
4258 if (TREE_SIDE_EFFECTS (valist))
4259 valist = save_expr (valist);
4260 valist = fold_build2_loc (loc, MEM_REF,
4261 vatype, valist, build_int_cst (pt, 0));
4262 }
4263
4264 return valist;
4265 }
4266
4267 /* The "standard" definition of va_list is void*. */
4268
4269 tree
4270 std_build_builtin_va_list (void)
4271 {
4272 return ptr_type_node;
4273 }
4274
4275 /* The "standard" abi va_list is va_list_type_node. */
4276
4277 tree
4278 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4279 {
4280 return va_list_type_node;
4281 }
4282
4283 /* The "standard" type of va_list is va_list_type_node. */
4284
4285 tree
4286 std_canonical_va_list_type (tree type)
4287 {
4288 tree wtype, htype;
4289
4290 if (INDIRECT_REF_P (type))
4291 type = TREE_TYPE (type);
4292 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4293 type = TREE_TYPE (type);
4294 wtype = va_list_type_node;
4295 htype = type;
4296 /* Treat structure va_list types. */
4297 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4298 htype = TREE_TYPE (htype);
4299 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4300 {
4301 /* If va_list is an array type, the argument may have decayed
4302 to a pointer type, e.g. by being passed to another function.
4303 In that case, unwrap both types so that we can compare the
4304 underlying records. */
4305 if (TREE_CODE (htype) == ARRAY_TYPE
4306 || POINTER_TYPE_P (htype))
4307 {
4308 wtype = TREE_TYPE (wtype);
4309 htype = TREE_TYPE (htype);
4310 }
4311 }
4312 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4313 return va_list_type_node;
4314
4315 return NULL_TREE;
4316 }
4317
4318 /* The "standard" implementation of va_start: just assign `nextarg' to
4319 the variable. */
4320
4321 void
4322 std_expand_builtin_va_start (tree valist, rtx nextarg)
4323 {
4324 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4325 convert_move (va_r, nextarg, 0);
4326 }
4327
4328 /* Expand EXP, a call to __builtin_va_start. */
4329
4330 static rtx
4331 expand_builtin_va_start (tree exp)
4332 {
4333 rtx nextarg;
4334 tree valist;
4335 location_t loc = EXPR_LOCATION (exp);
4336
4337 if (call_expr_nargs (exp) < 2)
4338 {
4339 error_at (loc, "too few arguments to function %<va_start%>");
4340 return const0_rtx;
4341 }
4342
4343 if (fold_builtin_next_arg (exp, true))
4344 return const0_rtx;
4345
4346 nextarg = expand_builtin_next_arg ();
4347 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4348
4349 if (targetm.expand_builtin_va_start)
4350 targetm.expand_builtin_va_start (valist, nextarg);
4351 else
4352 std_expand_builtin_va_start (valist, nextarg);
4353
4354 return const0_rtx;
4355 }
4356
4357 /* Expand EXP, a call to __builtin_va_end. */
4358
4359 static rtx
4360 expand_builtin_va_end (tree exp)
4361 {
4362 tree valist = CALL_EXPR_ARG (exp, 0);
4363
4364 /* Evaluate for side effects, if needed. I hate macros that don't
4365 do that. */
4366 if (TREE_SIDE_EFFECTS (valist))
4367 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4368
4369 return const0_rtx;
4370 }
4371
4372 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4373 builtin rather than just as an assignment in stdarg.h because of the
4374 nastiness of array-type va_list types. */
4375
4376 static rtx
4377 expand_builtin_va_copy (tree exp)
4378 {
4379 tree dst, src, t;
4380 location_t loc = EXPR_LOCATION (exp);
4381
4382 dst = CALL_EXPR_ARG (exp, 0);
4383 src = CALL_EXPR_ARG (exp, 1);
4384
4385 dst = stabilize_va_list_loc (loc, dst, 1);
4386 src = stabilize_va_list_loc (loc, src, 0);
4387
4388 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4389
4390 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4391 {
4392 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4393 TREE_SIDE_EFFECTS (t) = 1;
4394 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4395 }
4396 else
4397 {
4398 rtx dstb, srcb, size;
4399
4400 /* Evaluate to pointers. */
4401 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4402 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4403 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4404 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4405
4406 dstb = convert_memory_address (Pmode, dstb);
4407 srcb = convert_memory_address (Pmode, srcb);
4408
4409 /* "Dereference" to BLKmode memories. */
4410 dstb = gen_rtx_MEM (BLKmode, dstb);
4411 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4412 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4413 srcb = gen_rtx_MEM (BLKmode, srcb);
4414 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4415 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4416
4417 /* Copy. */
4418 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4419 }
4420
4421 return const0_rtx;
4422 }
4423
4424 /* Expand a call to one of the builtin functions __builtin_frame_address or
4425 __builtin_return_address. */
4426
4427 static rtx
4428 expand_builtin_frame_address (tree fndecl, tree exp)
4429 {
4430 /* The argument must be a nonnegative integer constant.
4431 It counts the number of frames to scan up the stack.
4432 The value is the return address saved in that frame. */
4433 if (call_expr_nargs (exp) == 0)
4434 /* Warning about missing arg was already issued. */
4435 return const0_rtx;
4436 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4437 {
4438 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4439 error ("invalid argument to %<__builtin_frame_address%>");
4440 else
4441 error ("invalid argument to %<__builtin_return_address%>");
4442 return const0_rtx;
4443 }
4444 else
4445 {
4446 rtx tem
4447 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4448 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4449
4450 /* Some ports cannot access arbitrary stack frames. */
4451 if (tem == NULL)
4452 {
4453 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4454 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4455 else
4456 warning (0, "unsupported argument to %<__builtin_return_address%>");
4457 return const0_rtx;
4458 }
4459
4460 /* For __builtin_frame_address, return what we've got. */
4461 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4462 return tem;
4463
4464 if (!REG_P (tem)
4465 && ! CONSTANT_P (tem))
4466 tem = copy_addr_to_reg (tem);
4467 return tem;
4468 }
4469 }
4470
4471 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4472 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4473 is the same as for allocate_dynamic_stack_space. */
4474
4475 static rtx
4476 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4477 {
4478 rtx op0;
4479 rtx result;
4480 bool valid_arglist;
4481 unsigned int align;
4482 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4483 == BUILT_IN_ALLOCA_WITH_ALIGN);
4484
4485 valid_arglist
4486 = (alloca_with_align
4487 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4488 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4489
4490 if (!valid_arglist)
4491 return NULL_RTX;
4492
4493 /* Compute the argument. */
4494 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4495
4496 /* Compute the alignment. */
4497 align = (alloca_with_align
4498 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4499 : BIGGEST_ALIGNMENT);
4500
4501 /* Allocate the desired space. */
4502 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4503 result = convert_memory_address (ptr_mode, result);
4504
4505 return result;
4506 }
4507
4508 /* Expand a call to bswap builtin in EXP.
4509 Return NULL_RTX if a normal call should be emitted rather than expanding the
4510 function in-line. If convenient, the result should be placed in TARGET.
4511 SUBTARGET may be used as the target for computing one of EXP's operands. */
4512
4513 static rtx
4514 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4515 rtx subtarget)
4516 {
4517 tree arg;
4518 rtx op0;
4519
4520 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4521 return NULL_RTX;
4522
4523 arg = CALL_EXPR_ARG (exp, 0);
4524 op0 = expand_expr (arg,
4525 subtarget && GET_MODE (subtarget) == target_mode
4526 ? subtarget : NULL_RTX,
4527 target_mode, EXPAND_NORMAL);
4528 if (GET_MODE (op0) != target_mode)
4529 op0 = convert_to_mode (target_mode, op0, 1);
4530
4531 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4532
4533 gcc_assert (target);
4534
4535 return convert_to_mode (target_mode, target, 1);
4536 }
4537
4538 /* Expand a call to a unary builtin in EXP.
4539 Return NULL_RTX if a normal call should be emitted rather than expanding the
4540 function in-line. If convenient, the result should be placed in TARGET.
4541 SUBTARGET may be used as the target for computing one of EXP's operands. */
4542
4543 static rtx
4544 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4545 rtx subtarget, optab op_optab)
4546 {
4547 rtx op0;
4548
4549 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4550 return NULL_RTX;
4551
4552 /* Compute the argument. */
4553 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4554 (subtarget
4555 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4556 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4557 VOIDmode, EXPAND_NORMAL);
4558 /* Compute op, into TARGET if possible.
4559 Set TARGET to wherever the result comes back. */
4560 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4561 op_optab, op0, target, op_optab != clrsb_optab);
4562 gcc_assert (target);
4563
4564 return convert_to_mode (target_mode, target, 0);
4565 }
4566
4567 /* Expand a call to __builtin_expect. We just return our argument
4568 as the builtin_expect semantic should've been already executed by
4569 tree branch prediction pass. */
4570
4571 static rtx
4572 expand_builtin_expect (tree exp, rtx target)
4573 {
4574 tree arg;
4575
4576 if (call_expr_nargs (exp) < 2)
4577 return const0_rtx;
4578 arg = CALL_EXPR_ARG (exp, 0);
4579
4580 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4581 /* When guessing was done, the hints should be already stripped away. */
4582 gcc_assert (!flag_guess_branch_prob
4583 || optimize == 0 || seen_error ());
4584 return target;
4585 }
4586
4587 /* Expand a call to __builtin_assume_aligned. We just return our first
4588 argument as the builtin_assume_aligned semantic should've been already
4589 executed by CCP. */
4590
4591 static rtx
4592 expand_builtin_assume_aligned (tree exp, rtx target)
4593 {
4594 if (call_expr_nargs (exp) < 2)
4595 return const0_rtx;
4596 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4597 EXPAND_NORMAL);
4598 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4599 && (call_expr_nargs (exp) < 3
4600 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4601 return target;
4602 }
4603
4604 void
4605 expand_builtin_trap (void)
4606 {
4607 #ifdef HAVE_trap
4608 if (HAVE_trap)
4609 {
4610 rtx insn = emit_insn (gen_trap ());
4611 /* For trap insns when not accumulating outgoing args force
4612 REG_ARGS_SIZE note to prevent crossjumping of calls with
4613 different args sizes. */
4614 if (!ACCUMULATE_OUTGOING_ARGS)
4615 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4616 }
4617 else
4618 #endif
4619 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4620 emit_barrier ();
4621 }
4622
4623 /* Expand a call to __builtin_unreachable. We do nothing except emit
4624 a barrier saying that control flow will not pass here.
4625
4626 It is the responsibility of the program being compiled to ensure
4627 that control flow does never reach __builtin_unreachable. */
4628 static void
4629 expand_builtin_unreachable (void)
4630 {
4631 emit_barrier ();
4632 }
4633
4634 /* Expand EXP, a call to fabs, fabsf or fabsl.
4635 Return NULL_RTX if a normal call should be emitted rather than expanding
4636 the function inline. If convenient, the result should be placed
4637 in TARGET. SUBTARGET may be used as the target for computing
4638 the operand. */
4639
4640 static rtx
4641 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4642 {
4643 enum machine_mode mode;
4644 tree arg;
4645 rtx op0;
4646
4647 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4648 return NULL_RTX;
4649
4650 arg = CALL_EXPR_ARG (exp, 0);
4651 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4652 mode = TYPE_MODE (TREE_TYPE (arg));
4653 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4654 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4655 }
4656
4657 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4658 Return NULL is a normal call should be emitted rather than expanding the
4659 function inline. If convenient, the result should be placed in TARGET.
4660 SUBTARGET may be used as the target for computing the operand. */
4661
4662 static rtx
4663 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4664 {
4665 rtx op0, op1;
4666 tree arg;
4667
4668 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4669 return NULL_RTX;
4670
4671 arg = CALL_EXPR_ARG (exp, 0);
4672 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4673
4674 arg = CALL_EXPR_ARG (exp, 1);
4675 op1 = expand_normal (arg);
4676
4677 return expand_copysign (op0, op1, target);
4678 }
4679
4680 /* Create a new constant string literal and return a char* pointer to it.
4681 The STRING_CST value is the LEN characters at STR. */
4682 tree
4683 build_string_literal (int len, const char *str)
4684 {
4685 tree t, elem, index, type;
4686
4687 t = build_string (len, str);
4688 elem = build_type_variant (char_type_node, 1, 0);
4689 index = build_index_type (size_int (len - 1));
4690 type = build_array_type (elem, index);
4691 TREE_TYPE (t) = type;
4692 TREE_CONSTANT (t) = 1;
4693 TREE_READONLY (t) = 1;
4694 TREE_STATIC (t) = 1;
4695
4696 type = build_pointer_type (elem);
4697 t = build1 (ADDR_EXPR, type,
4698 build4 (ARRAY_REF, elem,
4699 t, integer_zero_node, NULL_TREE, NULL_TREE));
4700 return t;
4701 }
4702
4703 /* Expand a call to __builtin___clear_cache. */
4704
4705 static rtx
4706 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4707 {
4708 #ifndef HAVE_clear_cache
4709 #ifdef CLEAR_INSN_CACHE
4710 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4711 does something. Just do the default expansion to a call to
4712 __clear_cache(). */
4713 return NULL_RTX;
4714 #else
4715 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4716 does nothing. There is no need to call it. Do nothing. */
4717 return const0_rtx;
4718 #endif /* CLEAR_INSN_CACHE */
4719 #else
4720 /* We have a "clear_cache" insn, and it will handle everything. */
4721 tree begin, end;
4722 rtx begin_rtx, end_rtx;
4723
4724 /* We must not expand to a library call. If we did, any
4725 fallback library function in libgcc that might contain a call to
4726 __builtin___clear_cache() would recurse infinitely. */
4727 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4728 {
4729 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4730 return const0_rtx;
4731 }
4732
4733 if (HAVE_clear_cache)
4734 {
4735 struct expand_operand ops[2];
4736
4737 begin = CALL_EXPR_ARG (exp, 0);
4738 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4739
4740 end = CALL_EXPR_ARG (exp, 1);
4741 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4742
4743 create_address_operand (&ops[0], begin_rtx);
4744 create_address_operand (&ops[1], end_rtx);
4745 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4746 return const0_rtx;
4747 }
4748 return const0_rtx;
4749 #endif /* HAVE_clear_cache */
4750 }
4751
4752 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4753
4754 static rtx
4755 round_trampoline_addr (rtx tramp)
4756 {
4757 rtx temp, addend, mask;
4758
4759 /* If we don't need too much alignment, we'll have been guaranteed
4760 proper alignment by get_trampoline_type. */
4761 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4762 return tramp;
4763
4764 /* Round address up to desired boundary. */
4765 temp = gen_reg_rtx (Pmode);
4766 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4767 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4768
4769 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4770 temp, 0, OPTAB_LIB_WIDEN);
4771 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4772 temp, 0, OPTAB_LIB_WIDEN);
4773
4774 return tramp;
4775 }
4776
4777 static rtx
4778 expand_builtin_init_trampoline (tree exp, bool onstack)
4779 {
4780 tree t_tramp, t_func, t_chain;
4781 rtx m_tramp, r_tramp, r_chain, tmp;
4782
4783 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4784 POINTER_TYPE, VOID_TYPE))
4785 return NULL_RTX;
4786
4787 t_tramp = CALL_EXPR_ARG (exp, 0);
4788 t_func = CALL_EXPR_ARG (exp, 1);
4789 t_chain = CALL_EXPR_ARG (exp, 2);
4790
4791 r_tramp = expand_normal (t_tramp);
4792 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4793 MEM_NOTRAP_P (m_tramp) = 1;
4794
4795 /* If ONSTACK, the TRAMP argument should be the address of a field
4796 within the local function's FRAME decl. Either way, let's see if
4797 we can fill in the MEM_ATTRs for this memory. */
4798 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4799 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4800
4801 /* Creator of a heap trampoline is responsible for making sure the
4802 address is aligned to at least STACK_BOUNDARY. Normally malloc
4803 will ensure this anyhow. */
4804 tmp = round_trampoline_addr (r_tramp);
4805 if (tmp != r_tramp)
4806 {
4807 m_tramp = change_address (m_tramp, BLKmode, tmp);
4808 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4809 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4810 }
4811
4812 /* The FUNC argument should be the address of the nested function.
4813 Extract the actual function decl to pass to the hook. */
4814 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4815 t_func = TREE_OPERAND (t_func, 0);
4816 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4817
4818 r_chain = expand_normal (t_chain);
4819
4820 /* Generate insns to initialize the trampoline. */
4821 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4822
4823 if (onstack)
4824 {
4825 trampolines_created = 1;
4826
4827 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4828 "trampoline generated for nested function %qD", t_func);
4829 }
4830
4831 return const0_rtx;
4832 }
4833
4834 static rtx
4835 expand_builtin_adjust_trampoline (tree exp)
4836 {
4837 rtx tramp;
4838
4839 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4840 return NULL_RTX;
4841
4842 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4843 tramp = round_trampoline_addr (tramp);
4844 if (targetm.calls.trampoline_adjust_address)
4845 tramp = targetm.calls.trampoline_adjust_address (tramp);
4846
4847 return tramp;
4848 }
4849
4850 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4851 function. The function first checks whether the back end provides
4852 an insn to implement signbit for the respective mode. If not, it
4853 checks whether the floating point format of the value is such that
4854 the sign bit can be extracted. If that is not the case, the
4855 function returns NULL_RTX to indicate that a normal call should be
4856 emitted rather than expanding the function in-line. EXP is the
4857 expression that is a call to the builtin function; if convenient,
4858 the result should be placed in TARGET. */
4859 static rtx
4860 expand_builtin_signbit (tree exp, rtx target)
4861 {
4862 const struct real_format *fmt;
4863 enum machine_mode fmode, imode, rmode;
4864 tree arg;
4865 int word, bitpos;
4866 enum insn_code icode;
4867 rtx temp;
4868 location_t loc = EXPR_LOCATION (exp);
4869
4870 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4871 return NULL_RTX;
4872
4873 arg = CALL_EXPR_ARG (exp, 0);
4874 fmode = TYPE_MODE (TREE_TYPE (arg));
4875 rmode = TYPE_MODE (TREE_TYPE (exp));
4876 fmt = REAL_MODE_FORMAT (fmode);
4877
4878 arg = builtin_save_expr (arg);
4879
4880 /* Expand the argument yielding a RTX expression. */
4881 temp = expand_normal (arg);
4882
4883 /* Check if the back end provides an insn that handles signbit for the
4884 argument's mode. */
4885 icode = optab_handler (signbit_optab, fmode);
4886 if (icode != CODE_FOR_nothing)
4887 {
4888 rtx last = get_last_insn ();
4889 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4890 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4891 return target;
4892 delete_insns_since (last);
4893 }
4894
4895 /* For floating point formats without a sign bit, implement signbit
4896 as "ARG < 0.0". */
4897 bitpos = fmt->signbit_ro;
4898 if (bitpos < 0)
4899 {
4900 /* But we can't do this if the format supports signed zero. */
4901 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4902 return NULL_RTX;
4903
4904 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4905 build_real (TREE_TYPE (arg), dconst0));
4906 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4907 }
4908
4909 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4910 {
4911 imode = int_mode_for_mode (fmode);
4912 if (imode == BLKmode)
4913 return NULL_RTX;
4914 temp = gen_lowpart (imode, temp);
4915 }
4916 else
4917 {
4918 imode = word_mode;
4919 /* Handle targets with different FP word orders. */
4920 if (FLOAT_WORDS_BIG_ENDIAN)
4921 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4922 else
4923 word = bitpos / BITS_PER_WORD;
4924 temp = operand_subword_force (temp, word, fmode);
4925 bitpos = bitpos % BITS_PER_WORD;
4926 }
4927
4928 /* Force the intermediate word_mode (or narrower) result into a
4929 register. This avoids attempting to create paradoxical SUBREGs
4930 of floating point modes below. */
4931 temp = force_reg (imode, temp);
4932
4933 /* If the bitpos is within the "result mode" lowpart, the operation
4934 can be implement with a single bitwise AND. Otherwise, we need
4935 a right shift and an AND. */
4936
4937 if (bitpos < GET_MODE_BITSIZE (rmode))
4938 {
4939 double_int mask = double_int_zero.set_bit (bitpos);
4940
4941 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4942 temp = gen_lowpart (rmode, temp);
4943 temp = expand_binop (rmode, and_optab, temp,
4944 immed_double_int_const (mask, rmode),
4945 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4946 }
4947 else
4948 {
4949 /* Perform a logical right shift to place the signbit in the least
4950 significant bit, then truncate the result to the desired mode
4951 and mask just this bit. */
4952 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4953 temp = gen_lowpart (rmode, temp);
4954 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4955 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4956 }
4957
4958 return temp;
4959 }
4960
4961 /* Expand fork or exec calls. TARGET is the desired target of the
4962 call. EXP is the call. FN is the
4963 identificator of the actual function. IGNORE is nonzero if the
4964 value is to be ignored. */
4965
4966 static rtx
4967 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4968 {
4969 tree id, decl;
4970 tree call;
4971
4972 /* If we are not profiling, just call the function. */
4973 if (!profile_arc_flag)
4974 return NULL_RTX;
4975
4976 /* Otherwise call the wrapper. This should be equivalent for the rest of
4977 compiler, so the code does not diverge, and the wrapper may run the
4978 code necessary for keeping the profiling sane. */
4979
4980 switch (DECL_FUNCTION_CODE (fn))
4981 {
4982 case BUILT_IN_FORK:
4983 id = get_identifier ("__gcov_fork");
4984 break;
4985
4986 case BUILT_IN_EXECL:
4987 id = get_identifier ("__gcov_execl");
4988 break;
4989
4990 case BUILT_IN_EXECV:
4991 id = get_identifier ("__gcov_execv");
4992 break;
4993
4994 case BUILT_IN_EXECLP:
4995 id = get_identifier ("__gcov_execlp");
4996 break;
4997
4998 case BUILT_IN_EXECLE:
4999 id = get_identifier ("__gcov_execle");
5000 break;
5001
5002 case BUILT_IN_EXECVP:
5003 id = get_identifier ("__gcov_execvp");
5004 break;
5005
5006 case BUILT_IN_EXECVE:
5007 id = get_identifier ("__gcov_execve");
5008 break;
5009
5010 default:
5011 gcc_unreachable ();
5012 }
5013
5014 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5015 FUNCTION_DECL, id, TREE_TYPE (fn));
5016 DECL_EXTERNAL (decl) = 1;
5017 TREE_PUBLIC (decl) = 1;
5018 DECL_ARTIFICIAL (decl) = 1;
5019 TREE_NOTHROW (decl) = 1;
5020 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5021 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5022 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5023 return expand_call (call, target, ignore);
5024 }
5025
5026
5027 \f
5028 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5029 the pointer in these functions is void*, the tree optimizers may remove
5030 casts. The mode computed in expand_builtin isn't reliable either, due
5031 to __sync_bool_compare_and_swap.
5032
5033 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5034 group of builtins. This gives us log2 of the mode size. */
5035
5036 static inline enum machine_mode
5037 get_builtin_sync_mode (int fcode_diff)
5038 {
5039 /* The size is not negotiable, so ask not to get BLKmode in return
5040 if the target indicates that a smaller size would be better. */
5041 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5042 }
5043
5044 /* Expand the memory expression LOC and return the appropriate memory operand
5045 for the builtin_sync operations. */
5046
5047 static rtx
5048 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5049 {
5050 rtx addr, mem;
5051
5052 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5053 addr = convert_memory_address (Pmode, addr);
5054
5055 /* Note that we explicitly do not want any alias information for this
5056 memory, so that we kill all other live memories. Otherwise we don't
5057 satisfy the full barrier semantics of the intrinsic. */
5058 mem = validize_mem (gen_rtx_MEM (mode, addr));
5059
5060 /* The alignment needs to be at least according to that of the mode. */
5061 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5062 get_pointer_alignment (loc)));
5063 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5064 MEM_VOLATILE_P (mem) = 1;
5065
5066 return mem;
5067 }
5068
5069 /* Make sure an argument is in the right mode.
5070 EXP is the tree argument.
5071 MODE is the mode it should be in. */
5072
5073 static rtx
5074 expand_expr_force_mode (tree exp, enum machine_mode mode)
5075 {
5076 rtx val;
5077 enum machine_mode old_mode;
5078
5079 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5080 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5081 of CONST_INTs, where we know the old_mode only from the call argument. */
5082
5083 old_mode = GET_MODE (val);
5084 if (old_mode == VOIDmode)
5085 old_mode = TYPE_MODE (TREE_TYPE (exp));
5086 val = convert_modes (mode, old_mode, val, 1);
5087 return val;
5088 }
5089
5090
5091 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5092 EXP is the CALL_EXPR. CODE is the rtx code
5093 that corresponds to the arithmetic or logical operation from the name;
5094 an exception here is that NOT actually means NAND. TARGET is an optional
5095 place for us to store the results; AFTER is true if this is the
5096 fetch_and_xxx form. */
5097
5098 static rtx
5099 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5100 enum rtx_code code, bool after,
5101 rtx target)
5102 {
5103 rtx val, mem;
5104 location_t loc = EXPR_LOCATION (exp);
5105
5106 if (code == NOT && warn_sync_nand)
5107 {
5108 tree fndecl = get_callee_fndecl (exp);
5109 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5110
5111 static bool warned_f_a_n, warned_n_a_f;
5112
5113 switch (fcode)
5114 {
5115 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5116 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5117 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5118 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5119 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5120 if (warned_f_a_n)
5121 break;
5122
5123 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5124 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5125 warned_f_a_n = true;
5126 break;
5127
5128 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5129 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5130 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5131 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5132 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5133 if (warned_n_a_f)
5134 break;
5135
5136 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5137 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5138 warned_n_a_f = true;
5139 break;
5140
5141 default:
5142 gcc_unreachable ();
5143 }
5144 }
5145
5146 /* Expand the operands. */
5147 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5148 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5149
5150 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5151 after);
5152 }
5153
5154 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5155 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5156 true if this is the boolean form. TARGET is a place for us to store the
5157 results; this is NOT optional if IS_BOOL is true. */
5158
5159 static rtx
5160 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5161 bool is_bool, rtx target)
5162 {
5163 rtx old_val, new_val, mem;
5164 rtx *pbool, *poval;
5165
5166 /* Expand the operands. */
5167 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5168 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5169 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5170
5171 pbool = poval = NULL;
5172 if (target != const0_rtx)
5173 {
5174 if (is_bool)
5175 pbool = &target;
5176 else
5177 poval = &target;
5178 }
5179 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5180 false, MEMMODEL_SEQ_CST,
5181 MEMMODEL_SEQ_CST))
5182 return NULL_RTX;
5183
5184 return target;
5185 }
5186
5187 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5188 general form is actually an atomic exchange, and some targets only
5189 support a reduced form with the second argument being a constant 1.
5190 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5191 the results. */
5192
5193 static rtx
5194 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5195 rtx target)
5196 {
5197 rtx val, mem;
5198
5199 /* Expand the operands. */
5200 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5201 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5202
5203 return expand_sync_lock_test_and_set (target, mem, val);
5204 }
5205
5206 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5207
5208 static void
5209 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5210 {
5211 rtx mem;
5212
5213 /* Expand the operands. */
5214 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5215
5216 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5217 }
5218
5219 /* Given an integer representing an ``enum memmodel'', verify its
5220 correctness and return the memory model enum. */
5221
5222 static enum memmodel
5223 get_memmodel (tree exp)
5224 {
5225 rtx op;
5226 unsigned HOST_WIDE_INT val;
5227
5228 /* If the parameter is not a constant, it's a run time value so we'll just
5229 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5230 if (TREE_CODE (exp) != INTEGER_CST)
5231 return MEMMODEL_SEQ_CST;
5232
5233 op = expand_normal (exp);
5234
5235 val = INTVAL (op);
5236 if (targetm.memmodel_check)
5237 val = targetm.memmodel_check (val);
5238 else if (val & ~MEMMODEL_MASK)
5239 {
5240 warning (OPT_Winvalid_memory_model,
5241 "Unknown architecture specifier in memory model to builtin.");
5242 return MEMMODEL_SEQ_CST;
5243 }
5244
5245 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5246 {
5247 warning (OPT_Winvalid_memory_model,
5248 "invalid memory model argument to builtin");
5249 return MEMMODEL_SEQ_CST;
5250 }
5251
5252 return (enum memmodel) val;
5253 }
5254
5255 /* Expand the __atomic_exchange intrinsic:
5256 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5257 EXP is the CALL_EXPR.
5258 TARGET is an optional place for us to store the results. */
5259
5260 static rtx
5261 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5262 {
5263 rtx val, mem;
5264 enum memmodel model;
5265
5266 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5267 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5268 {
5269 error ("invalid memory model for %<__atomic_exchange%>");
5270 return NULL_RTX;
5271 }
5272
5273 if (!flag_inline_atomics)
5274 return NULL_RTX;
5275
5276 /* Expand the operands. */
5277 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5278 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5279
5280 return expand_atomic_exchange (target, mem, val, model);
5281 }
5282
5283 /* Expand the __atomic_compare_exchange intrinsic:
5284 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5285 TYPE desired, BOOL weak,
5286 enum memmodel success,
5287 enum memmodel failure)
5288 EXP is the CALL_EXPR.
5289 TARGET is an optional place for us to store the results. */
5290
5291 static rtx
5292 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5293 rtx target)
5294 {
5295 rtx expect, desired, mem, oldval, label;
5296 enum memmodel success, failure;
5297 tree weak;
5298 bool is_weak;
5299
5300 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5301 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5302
5303 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5304 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5305 {
5306 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5307 return NULL_RTX;
5308 }
5309
5310 if (failure > success)
5311 {
5312 error ("failure memory model cannot be stronger than success "
5313 "memory model for %<__atomic_compare_exchange%>");
5314 return NULL_RTX;
5315 }
5316
5317 if (!flag_inline_atomics)
5318 return NULL_RTX;
5319
5320 /* Expand the operands. */
5321 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5322
5323 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5324 expect = convert_memory_address (Pmode, expect);
5325 expect = gen_rtx_MEM (mode, expect);
5326 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5327
5328 weak = CALL_EXPR_ARG (exp, 3);
5329 is_weak = false;
5330 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5331 is_weak = true;
5332
5333 if (target == const0_rtx)
5334 target = NULL;
5335
5336 /* Lest the rtl backend create a race condition with an imporoper store
5337 to memory, always create a new pseudo for OLDVAL. */
5338 oldval = NULL;
5339
5340 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5341 is_weak, success, failure))
5342 return NULL_RTX;
5343
5344 /* Conditionally store back to EXPECT, lest we create a race condition
5345 with an improper store to memory. */
5346 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5347 the normal case where EXPECT is totally private, i.e. a register. At
5348 which point the store can be unconditional. */
5349 label = gen_label_rtx ();
5350 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5351 emit_move_insn (expect, oldval);
5352 emit_label (label);
5353
5354 return target;
5355 }
5356
5357 /* Expand the __atomic_load intrinsic:
5358 TYPE __atomic_load (TYPE *object, enum memmodel)
5359 EXP is the CALL_EXPR.
5360 TARGET is an optional place for us to store the results. */
5361
5362 static rtx
5363 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5364 {
5365 rtx mem;
5366 enum memmodel model;
5367
5368 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5369 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5370 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5371 {
5372 error ("invalid memory model for %<__atomic_load%>");
5373 return NULL_RTX;
5374 }
5375
5376 if (!flag_inline_atomics)
5377 return NULL_RTX;
5378
5379 /* Expand the operand. */
5380 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5381
5382 return expand_atomic_load (target, mem, model);
5383 }
5384
5385
5386 /* Expand the __atomic_store intrinsic:
5387 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5388 EXP is the CALL_EXPR.
5389 TARGET is an optional place for us to store the results. */
5390
5391 static rtx
5392 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5393 {
5394 rtx mem, val;
5395 enum memmodel model;
5396
5397 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5398 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5399 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5400 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5401 {
5402 error ("invalid memory model for %<__atomic_store%>");
5403 return NULL_RTX;
5404 }
5405
5406 if (!flag_inline_atomics)
5407 return NULL_RTX;
5408
5409 /* Expand the operands. */
5410 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5411 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5412
5413 return expand_atomic_store (mem, val, model, false);
5414 }
5415
5416 /* Expand the __atomic_fetch_XXX intrinsic:
5417 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5418 EXP is the CALL_EXPR.
5419 TARGET is an optional place for us to store the results.
5420 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5421 FETCH_AFTER is true if returning the result of the operation.
5422 FETCH_AFTER is false if returning the value before the operation.
5423 IGNORE is true if the result is not used.
5424 EXT_CALL is the correct builtin for an external call if this cannot be
5425 resolved to an instruction sequence. */
5426
5427 static rtx
5428 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5429 enum rtx_code code, bool fetch_after,
5430 bool ignore, enum built_in_function ext_call)
5431 {
5432 rtx val, mem, ret;
5433 enum memmodel model;
5434 tree fndecl;
5435 tree addr;
5436
5437 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5438
5439 /* Expand the operands. */
5440 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5441 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5442
5443 /* Only try generating instructions if inlining is turned on. */
5444 if (flag_inline_atomics)
5445 {
5446 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5447 if (ret)
5448 return ret;
5449 }
5450
5451 /* Return if a different routine isn't needed for the library call. */
5452 if (ext_call == BUILT_IN_NONE)
5453 return NULL_RTX;
5454
5455 /* Change the call to the specified function. */
5456 fndecl = get_callee_fndecl (exp);
5457 addr = CALL_EXPR_FN (exp);
5458 STRIP_NOPS (addr);
5459
5460 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5461 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5462
5463 /* Expand the call here so we can emit trailing code. */
5464 ret = expand_call (exp, target, ignore);
5465
5466 /* Replace the original function just in case it matters. */
5467 TREE_OPERAND (addr, 0) = fndecl;
5468
5469 /* Then issue the arithmetic correction to return the right result. */
5470 if (!ignore)
5471 {
5472 if (code == NOT)
5473 {
5474 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5475 OPTAB_LIB_WIDEN);
5476 ret = expand_simple_unop (mode, NOT, ret, target, true);
5477 }
5478 else
5479 ret = expand_simple_binop (mode, code, ret, val, target, true,
5480 OPTAB_LIB_WIDEN);
5481 }
5482 return ret;
5483 }
5484
5485
5486 #ifndef HAVE_atomic_clear
5487 # define HAVE_atomic_clear 0
5488 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5489 #endif
5490
5491 /* Expand an atomic clear operation.
5492 void _atomic_clear (BOOL *obj, enum memmodel)
5493 EXP is the call expression. */
5494
5495 static rtx
5496 expand_builtin_atomic_clear (tree exp)
5497 {
5498 enum machine_mode mode;
5499 rtx mem, ret;
5500 enum memmodel model;
5501
5502 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5503 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5504 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5505
5506 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5507 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5508 {
5509 error ("invalid memory model for %<__atomic_store%>");
5510 return const0_rtx;
5511 }
5512
5513 if (HAVE_atomic_clear)
5514 {
5515 emit_insn (gen_atomic_clear (mem, model));
5516 return const0_rtx;
5517 }
5518
5519 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5520 Failing that, a store is issued by __atomic_store. The only way this can
5521 fail is if the bool type is larger than a word size. Unlikely, but
5522 handle it anyway for completeness. Assume a single threaded model since
5523 there is no atomic support in this case, and no barriers are required. */
5524 ret = expand_atomic_store (mem, const0_rtx, model, true);
5525 if (!ret)
5526 emit_move_insn (mem, const0_rtx);
5527 return const0_rtx;
5528 }
5529
5530 /* Expand an atomic test_and_set operation.
5531 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5532 EXP is the call expression. */
5533
5534 static rtx
5535 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5536 {
5537 rtx mem;
5538 enum memmodel model;
5539 enum machine_mode mode;
5540
5541 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5542 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5543 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5544
5545 return expand_atomic_test_and_set (target, mem, model);
5546 }
5547
5548
5549 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5550 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5551
5552 static tree
5553 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5554 {
5555 int size;
5556 enum machine_mode mode;
5557 unsigned int mode_align, type_align;
5558
5559 if (TREE_CODE (arg0) != INTEGER_CST)
5560 return NULL_TREE;
5561
5562 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5563 mode = mode_for_size (size, MODE_INT, 0);
5564 mode_align = GET_MODE_ALIGNMENT (mode);
5565
5566 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5567 type_align = mode_align;
5568 else
5569 {
5570 tree ttype = TREE_TYPE (arg1);
5571
5572 /* This function is usually invoked and folded immediately by the front
5573 end before anything else has a chance to look at it. The pointer
5574 parameter at this point is usually cast to a void *, so check for that
5575 and look past the cast. */
5576 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5577 && VOID_TYPE_P (TREE_TYPE (ttype)))
5578 arg1 = TREE_OPERAND (arg1, 0);
5579
5580 ttype = TREE_TYPE (arg1);
5581 gcc_assert (POINTER_TYPE_P (ttype));
5582
5583 /* Get the underlying type of the object. */
5584 ttype = TREE_TYPE (ttype);
5585 type_align = TYPE_ALIGN (ttype);
5586 }
5587
5588 /* If the object has smaller alignment, the the lock free routines cannot
5589 be used. */
5590 if (type_align < mode_align)
5591 return boolean_false_node;
5592
5593 /* Check if a compare_and_swap pattern exists for the mode which represents
5594 the required size. The pattern is not allowed to fail, so the existence
5595 of the pattern indicates support is present. */
5596 if (can_compare_and_swap_p (mode, true))
5597 return boolean_true_node;
5598 else
5599 return boolean_false_node;
5600 }
5601
5602 /* Return true if the parameters to call EXP represent an object which will
5603 always generate lock free instructions. The first argument represents the
5604 size of the object, and the second parameter is a pointer to the object
5605 itself. If NULL is passed for the object, then the result is based on
5606 typical alignment for an object of the specified size. Otherwise return
5607 false. */
5608
5609 static rtx
5610 expand_builtin_atomic_always_lock_free (tree exp)
5611 {
5612 tree size;
5613 tree arg0 = CALL_EXPR_ARG (exp, 0);
5614 tree arg1 = CALL_EXPR_ARG (exp, 1);
5615
5616 if (TREE_CODE (arg0) != INTEGER_CST)
5617 {
5618 error ("non-constant argument 1 to __atomic_always_lock_free");
5619 return const0_rtx;
5620 }
5621
5622 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5623 if (size == boolean_true_node)
5624 return const1_rtx;
5625 return const0_rtx;
5626 }
5627
5628 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5629 is lock free on this architecture. */
5630
5631 static tree
5632 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5633 {
5634 if (!flag_inline_atomics)
5635 return NULL_TREE;
5636
5637 /* If it isn't always lock free, don't generate a result. */
5638 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5639 return boolean_true_node;
5640
5641 return NULL_TREE;
5642 }
5643
5644 /* Return true if the parameters to call EXP represent an object which will
5645 always generate lock free instructions. The first argument represents the
5646 size of the object, and the second parameter is a pointer to the object
5647 itself. If NULL is passed for the object, then the result is based on
5648 typical alignment for an object of the specified size. Otherwise return
5649 NULL*/
5650
5651 static rtx
5652 expand_builtin_atomic_is_lock_free (tree exp)
5653 {
5654 tree size;
5655 tree arg0 = CALL_EXPR_ARG (exp, 0);
5656 tree arg1 = CALL_EXPR_ARG (exp, 1);
5657
5658 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5659 {
5660 error ("non-integer argument 1 to __atomic_is_lock_free");
5661 return NULL_RTX;
5662 }
5663
5664 if (!flag_inline_atomics)
5665 return NULL_RTX;
5666
5667 /* If the value is known at compile time, return the RTX for it. */
5668 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5669 if (size == boolean_true_node)
5670 return const1_rtx;
5671
5672 return NULL_RTX;
5673 }
5674
5675 /* Expand the __atomic_thread_fence intrinsic:
5676 void __atomic_thread_fence (enum memmodel)
5677 EXP is the CALL_EXPR. */
5678
5679 static void
5680 expand_builtin_atomic_thread_fence (tree exp)
5681 {
5682 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5683 expand_mem_thread_fence (model);
5684 }
5685
5686 /* Expand the __atomic_signal_fence intrinsic:
5687 void __atomic_signal_fence (enum memmodel)
5688 EXP is the CALL_EXPR. */
5689
5690 static void
5691 expand_builtin_atomic_signal_fence (tree exp)
5692 {
5693 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5694 expand_mem_signal_fence (model);
5695 }
5696
5697 /* Expand the __sync_synchronize intrinsic. */
5698
5699 static void
5700 expand_builtin_sync_synchronize (void)
5701 {
5702 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5703 }
5704
5705 static rtx
5706 expand_builtin_thread_pointer (tree exp, rtx target)
5707 {
5708 enum insn_code icode;
5709 if (!validate_arglist (exp, VOID_TYPE))
5710 return const0_rtx;
5711 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5712 if (icode != CODE_FOR_nothing)
5713 {
5714 struct expand_operand op;
5715 if (!REG_P (target) || GET_MODE (target) != Pmode)
5716 target = gen_reg_rtx (Pmode);
5717 create_output_operand (&op, target, Pmode);
5718 expand_insn (icode, 1, &op);
5719 return target;
5720 }
5721 error ("__builtin_thread_pointer is not supported on this target");
5722 return const0_rtx;
5723 }
5724
5725 static void
5726 expand_builtin_set_thread_pointer (tree exp)
5727 {
5728 enum insn_code icode;
5729 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5730 return;
5731 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5732 if (icode != CODE_FOR_nothing)
5733 {
5734 struct expand_operand op;
5735 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5736 Pmode, EXPAND_NORMAL);
5737 create_input_operand (&op, val, Pmode);
5738 expand_insn (icode, 1, &op);
5739 return;
5740 }
5741 error ("__builtin_set_thread_pointer is not supported on this target");
5742 }
5743
5744 \f
5745 /* Emit code to restore the current value of stack. */
5746
5747 static void
5748 expand_stack_restore (tree var)
5749 {
5750 rtx prev, sa = expand_normal (var);
5751
5752 sa = convert_memory_address (Pmode, sa);
5753
5754 prev = get_last_insn ();
5755 emit_stack_restore (SAVE_BLOCK, sa);
5756 fixup_args_size_notes (prev, get_last_insn (), 0);
5757 }
5758
5759
5760 /* Emit code to save the current value of stack. */
5761
5762 static rtx
5763 expand_stack_save (void)
5764 {
5765 rtx ret = NULL_RTX;
5766
5767 do_pending_stack_adjust ();
5768 emit_stack_save (SAVE_BLOCK, &ret);
5769 return ret;
5770 }
5771
5772 /* Expand an expression EXP that calls a built-in function,
5773 with result going to TARGET if that's convenient
5774 (and in mode MODE if that's convenient).
5775 SUBTARGET may be used as the target for computing one of EXP's operands.
5776 IGNORE is nonzero if the value is to be ignored. */
5777
5778 rtx
5779 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5780 int ignore)
5781 {
5782 tree fndecl = get_callee_fndecl (exp);
5783 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5784 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5785 int flags;
5786
5787 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5788 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5789
5790 /* When not optimizing, generate calls to library functions for a certain
5791 set of builtins. */
5792 if (!optimize
5793 && !called_as_built_in (fndecl)
5794 && fcode != BUILT_IN_FORK
5795 && fcode != BUILT_IN_EXECL
5796 && fcode != BUILT_IN_EXECV
5797 && fcode != BUILT_IN_EXECLP
5798 && fcode != BUILT_IN_EXECLE
5799 && fcode != BUILT_IN_EXECVP
5800 && fcode != BUILT_IN_EXECVE
5801 && fcode != BUILT_IN_ALLOCA
5802 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5803 && fcode != BUILT_IN_FREE)
5804 return expand_call (exp, target, ignore);
5805
5806 /* The built-in function expanders test for target == const0_rtx
5807 to determine whether the function's result will be ignored. */
5808 if (ignore)
5809 target = const0_rtx;
5810
5811 /* If the result of a pure or const built-in function is ignored, and
5812 none of its arguments are volatile, we can avoid expanding the
5813 built-in call and just evaluate the arguments for side-effects. */
5814 if (target == const0_rtx
5815 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5816 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5817 {
5818 bool volatilep = false;
5819 tree arg;
5820 call_expr_arg_iterator iter;
5821
5822 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5823 if (TREE_THIS_VOLATILE (arg))
5824 {
5825 volatilep = true;
5826 break;
5827 }
5828
5829 if (! volatilep)
5830 {
5831 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5832 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5833 return const0_rtx;
5834 }
5835 }
5836
5837 switch (fcode)
5838 {
5839 CASE_FLT_FN (BUILT_IN_FABS):
5840 case BUILT_IN_FABSD32:
5841 case BUILT_IN_FABSD64:
5842 case BUILT_IN_FABSD128:
5843 target = expand_builtin_fabs (exp, target, subtarget);
5844 if (target)
5845 return target;
5846 break;
5847
5848 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5849 target = expand_builtin_copysign (exp, target, subtarget);
5850 if (target)
5851 return target;
5852 break;
5853
5854 /* Just do a normal library call if we were unable to fold
5855 the values. */
5856 CASE_FLT_FN (BUILT_IN_CABS):
5857 break;
5858
5859 CASE_FLT_FN (BUILT_IN_EXP):
5860 CASE_FLT_FN (BUILT_IN_EXP10):
5861 CASE_FLT_FN (BUILT_IN_POW10):
5862 CASE_FLT_FN (BUILT_IN_EXP2):
5863 CASE_FLT_FN (BUILT_IN_EXPM1):
5864 CASE_FLT_FN (BUILT_IN_LOGB):
5865 CASE_FLT_FN (BUILT_IN_LOG):
5866 CASE_FLT_FN (BUILT_IN_LOG10):
5867 CASE_FLT_FN (BUILT_IN_LOG2):
5868 CASE_FLT_FN (BUILT_IN_LOG1P):
5869 CASE_FLT_FN (BUILT_IN_TAN):
5870 CASE_FLT_FN (BUILT_IN_ASIN):
5871 CASE_FLT_FN (BUILT_IN_ACOS):
5872 CASE_FLT_FN (BUILT_IN_ATAN):
5873 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5874 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5875 because of possible accuracy problems. */
5876 if (! flag_unsafe_math_optimizations)
5877 break;
5878 CASE_FLT_FN (BUILT_IN_SQRT):
5879 CASE_FLT_FN (BUILT_IN_FLOOR):
5880 CASE_FLT_FN (BUILT_IN_CEIL):
5881 CASE_FLT_FN (BUILT_IN_TRUNC):
5882 CASE_FLT_FN (BUILT_IN_ROUND):
5883 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5884 CASE_FLT_FN (BUILT_IN_RINT):
5885 target = expand_builtin_mathfn (exp, target, subtarget);
5886 if (target)
5887 return target;
5888 break;
5889
5890 CASE_FLT_FN (BUILT_IN_FMA):
5891 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5892 if (target)
5893 return target;
5894 break;
5895
5896 CASE_FLT_FN (BUILT_IN_ILOGB):
5897 if (! flag_unsafe_math_optimizations)
5898 break;
5899 CASE_FLT_FN (BUILT_IN_ISINF):
5900 CASE_FLT_FN (BUILT_IN_FINITE):
5901 case BUILT_IN_ISFINITE:
5902 case BUILT_IN_ISNORMAL:
5903 target = expand_builtin_interclass_mathfn (exp, target);
5904 if (target)
5905 return target;
5906 break;
5907
5908 CASE_FLT_FN (BUILT_IN_ICEIL):
5909 CASE_FLT_FN (BUILT_IN_LCEIL):
5910 CASE_FLT_FN (BUILT_IN_LLCEIL):
5911 CASE_FLT_FN (BUILT_IN_LFLOOR):
5912 CASE_FLT_FN (BUILT_IN_IFLOOR):
5913 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5914 target = expand_builtin_int_roundingfn (exp, target);
5915 if (target)
5916 return target;
5917 break;
5918
5919 CASE_FLT_FN (BUILT_IN_IRINT):
5920 CASE_FLT_FN (BUILT_IN_LRINT):
5921 CASE_FLT_FN (BUILT_IN_LLRINT):
5922 CASE_FLT_FN (BUILT_IN_IROUND):
5923 CASE_FLT_FN (BUILT_IN_LROUND):
5924 CASE_FLT_FN (BUILT_IN_LLROUND):
5925 target = expand_builtin_int_roundingfn_2 (exp, target);
5926 if (target)
5927 return target;
5928 break;
5929
5930 CASE_FLT_FN (BUILT_IN_POWI):
5931 target = expand_builtin_powi (exp, target);
5932 if (target)
5933 return target;
5934 break;
5935
5936 CASE_FLT_FN (BUILT_IN_ATAN2):
5937 CASE_FLT_FN (BUILT_IN_LDEXP):
5938 CASE_FLT_FN (BUILT_IN_SCALB):
5939 CASE_FLT_FN (BUILT_IN_SCALBN):
5940 CASE_FLT_FN (BUILT_IN_SCALBLN):
5941 if (! flag_unsafe_math_optimizations)
5942 break;
5943
5944 CASE_FLT_FN (BUILT_IN_FMOD):
5945 CASE_FLT_FN (BUILT_IN_REMAINDER):
5946 CASE_FLT_FN (BUILT_IN_DREM):
5947 CASE_FLT_FN (BUILT_IN_POW):
5948 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5949 if (target)
5950 return target;
5951 break;
5952
5953 CASE_FLT_FN (BUILT_IN_CEXPI):
5954 target = expand_builtin_cexpi (exp, target);
5955 gcc_assert (target);
5956 return target;
5957
5958 CASE_FLT_FN (BUILT_IN_SIN):
5959 CASE_FLT_FN (BUILT_IN_COS):
5960 if (! flag_unsafe_math_optimizations)
5961 break;
5962 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5963 if (target)
5964 return target;
5965 break;
5966
5967 CASE_FLT_FN (BUILT_IN_SINCOS):
5968 if (! flag_unsafe_math_optimizations)
5969 break;
5970 target = expand_builtin_sincos (exp);
5971 if (target)
5972 return target;
5973 break;
5974
5975 case BUILT_IN_APPLY_ARGS:
5976 return expand_builtin_apply_args ();
5977
5978 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5979 FUNCTION with a copy of the parameters described by
5980 ARGUMENTS, and ARGSIZE. It returns a block of memory
5981 allocated on the stack into which is stored all the registers
5982 that might possibly be used for returning the result of a
5983 function. ARGUMENTS is the value returned by
5984 __builtin_apply_args. ARGSIZE is the number of bytes of
5985 arguments that must be copied. ??? How should this value be
5986 computed? We'll also need a safe worst case value for varargs
5987 functions. */
5988 case BUILT_IN_APPLY:
5989 if (!validate_arglist (exp, POINTER_TYPE,
5990 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5991 && !validate_arglist (exp, REFERENCE_TYPE,
5992 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5993 return const0_rtx;
5994 else
5995 {
5996 rtx ops[3];
5997
5998 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5999 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6000 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6001
6002 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6003 }
6004
6005 /* __builtin_return (RESULT) causes the function to return the
6006 value described by RESULT. RESULT is address of the block of
6007 memory returned by __builtin_apply. */
6008 case BUILT_IN_RETURN:
6009 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6010 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6011 return const0_rtx;
6012
6013 case BUILT_IN_SAVEREGS:
6014 return expand_builtin_saveregs ();
6015
6016 case BUILT_IN_VA_ARG_PACK:
6017 /* All valid uses of __builtin_va_arg_pack () are removed during
6018 inlining. */
6019 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6020 return const0_rtx;
6021
6022 case BUILT_IN_VA_ARG_PACK_LEN:
6023 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6024 inlining. */
6025 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6026 return const0_rtx;
6027
6028 /* Return the address of the first anonymous stack arg. */
6029 case BUILT_IN_NEXT_ARG:
6030 if (fold_builtin_next_arg (exp, false))
6031 return const0_rtx;
6032 return expand_builtin_next_arg ();
6033
6034 case BUILT_IN_CLEAR_CACHE:
6035 target = expand_builtin___clear_cache (exp);
6036 if (target)
6037 return target;
6038 break;
6039
6040 case BUILT_IN_CLASSIFY_TYPE:
6041 return expand_builtin_classify_type (exp);
6042
6043 case BUILT_IN_CONSTANT_P:
6044 return const0_rtx;
6045
6046 case BUILT_IN_FRAME_ADDRESS:
6047 case BUILT_IN_RETURN_ADDRESS:
6048 return expand_builtin_frame_address (fndecl, exp);
6049
6050 /* Returns the address of the area where the structure is returned.
6051 0 otherwise. */
6052 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6053 if (call_expr_nargs (exp) != 0
6054 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6055 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6056 return const0_rtx;
6057 else
6058 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6059
6060 case BUILT_IN_ALLOCA:
6061 case BUILT_IN_ALLOCA_WITH_ALIGN:
6062 /* If the allocation stems from the declaration of a variable-sized
6063 object, it cannot accumulate. */
6064 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6065 if (target)
6066 return target;
6067 break;
6068
6069 case BUILT_IN_STACK_SAVE:
6070 return expand_stack_save ();
6071
6072 case BUILT_IN_STACK_RESTORE:
6073 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6074 return const0_rtx;
6075
6076 case BUILT_IN_BSWAP16:
6077 case BUILT_IN_BSWAP32:
6078 case BUILT_IN_BSWAP64:
6079 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6080 if (target)
6081 return target;
6082 break;
6083
6084 CASE_INT_FN (BUILT_IN_FFS):
6085 target = expand_builtin_unop (target_mode, exp, target,
6086 subtarget, ffs_optab);
6087 if (target)
6088 return target;
6089 break;
6090
6091 CASE_INT_FN (BUILT_IN_CLZ):
6092 target = expand_builtin_unop (target_mode, exp, target,
6093 subtarget, clz_optab);
6094 if (target)
6095 return target;
6096 break;
6097
6098 CASE_INT_FN (BUILT_IN_CTZ):
6099 target = expand_builtin_unop (target_mode, exp, target,
6100 subtarget, ctz_optab);
6101 if (target)
6102 return target;
6103 break;
6104
6105 CASE_INT_FN (BUILT_IN_CLRSB):
6106 target = expand_builtin_unop (target_mode, exp, target,
6107 subtarget, clrsb_optab);
6108 if (target)
6109 return target;
6110 break;
6111
6112 CASE_INT_FN (BUILT_IN_POPCOUNT):
6113 target = expand_builtin_unop (target_mode, exp, target,
6114 subtarget, popcount_optab);
6115 if (target)
6116 return target;
6117 break;
6118
6119 CASE_INT_FN (BUILT_IN_PARITY):
6120 target = expand_builtin_unop (target_mode, exp, target,
6121 subtarget, parity_optab);
6122 if (target)
6123 return target;
6124 break;
6125
6126 case BUILT_IN_STRLEN:
6127 target = expand_builtin_strlen (exp, target, target_mode);
6128 if (target)
6129 return target;
6130 break;
6131
6132 case BUILT_IN_STRCPY:
6133 target = expand_builtin_strcpy (exp, target);
6134 if (target)
6135 return target;
6136 break;
6137
6138 case BUILT_IN_STRNCPY:
6139 target = expand_builtin_strncpy (exp, target);
6140 if (target)
6141 return target;
6142 break;
6143
6144 case BUILT_IN_STPCPY:
6145 target = expand_builtin_stpcpy (exp, target, mode);
6146 if (target)
6147 return target;
6148 break;
6149
6150 case BUILT_IN_MEMCPY:
6151 target = expand_builtin_memcpy (exp, target);
6152 if (target)
6153 return target;
6154 break;
6155
6156 case BUILT_IN_MEMPCPY:
6157 target = expand_builtin_mempcpy (exp, target, mode);
6158 if (target)
6159 return target;
6160 break;
6161
6162 case BUILT_IN_MEMSET:
6163 target = expand_builtin_memset (exp, target, mode);
6164 if (target)
6165 return target;
6166 break;
6167
6168 case BUILT_IN_BZERO:
6169 target = expand_builtin_bzero (exp);
6170 if (target)
6171 return target;
6172 break;
6173
6174 case BUILT_IN_STRCMP:
6175 target = expand_builtin_strcmp (exp, target);
6176 if (target)
6177 return target;
6178 break;
6179
6180 case BUILT_IN_STRNCMP:
6181 target = expand_builtin_strncmp (exp, target, mode);
6182 if (target)
6183 return target;
6184 break;
6185
6186 case BUILT_IN_BCMP:
6187 case BUILT_IN_MEMCMP:
6188 target = expand_builtin_memcmp (exp, target, mode);
6189 if (target)
6190 return target;
6191 break;
6192
6193 case BUILT_IN_SETJMP:
6194 /* This should have been lowered to the builtins below. */
6195 gcc_unreachable ();
6196
6197 case BUILT_IN_SETJMP_SETUP:
6198 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6199 and the receiver label. */
6200 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6201 {
6202 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6203 VOIDmode, EXPAND_NORMAL);
6204 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6205 rtx label_r = label_rtx (label);
6206
6207 /* This is copied from the handling of non-local gotos. */
6208 expand_builtin_setjmp_setup (buf_addr, label_r);
6209 nonlocal_goto_handler_labels
6210 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6211 nonlocal_goto_handler_labels);
6212 /* ??? Do not let expand_label treat us as such since we would
6213 not want to be both on the list of non-local labels and on
6214 the list of forced labels. */
6215 FORCED_LABEL (label) = 0;
6216 return const0_rtx;
6217 }
6218 break;
6219
6220 case BUILT_IN_SETJMP_RECEIVER:
6221 /* __builtin_setjmp_receiver is passed the receiver label. */
6222 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6223 {
6224 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6225 rtx label_r = label_rtx (label);
6226
6227 expand_builtin_setjmp_receiver (label_r);
6228 return const0_rtx;
6229 }
6230 break;
6231
6232 /* __builtin_longjmp is passed a pointer to an array of five words.
6233 It's similar to the C library longjmp function but works with
6234 __builtin_setjmp above. */
6235 case BUILT_IN_LONGJMP:
6236 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6237 {
6238 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6239 VOIDmode, EXPAND_NORMAL);
6240 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6241
6242 if (value != const1_rtx)
6243 {
6244 error ("%<__builtin_longjmp%> second argument must be 1");
6245 return const0_rtx;
6246 }
6247
6248 expand_builtin_longjmp (buf_addr, value);
6249 return const0_rtx;
6250 }
6251 break;
6252
6253 case BUILT_IN_NONLOCAL_GOTO:
6254 target = expand_builtin_nonlocal_goto (exp);
6255 if (target)
6256 return target;
6257 break;
6258
6259 /* This updates the setjmp buffer that is its argument with the value
6260 of the current stack pointer. */
6261 case BUILT_IN_UPDATE_SETJMP_BUF:
6262 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6263 {
6264 rtx buf_addr
6265 = expand_normal (CALL_EXPR_ARG (exp, 0));
6266
6267 expand_builtin_update_setjmp_buf (buf_addr);
6268 return const0_rtx;
6269 }
6270 break;
6271
6272 case BUILT_IN_TRAP:
6273 expand_builtin_trap ();
6274 return const0_rtx;
6275
6276 case BUILT_IN_UNREACHABLE:
6277 expand_builtin_unreachable ();
6278 return const0_rtx;
6279
6280 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6281 case BUILT_IN_SIGNBITD32:
6282 case BUILT_IN_SIGNBITD64:
6283 case BUILT_IN_SIGNBITD128:
6284 target = expand_builtin_signbit (exp, target);
6285 if (target)
6286 return target;
6287 break;
6288
6289 /* Various hooks for the DWARF 2 __throw routine. */
6290 case BUILT_IN_UNWIND_INIT:
6291 expand_builtin_unwind_init ();
6292 return const0_rtx;
6293 case BUILT_IN_DWARF_CFA:
6294 return virtual_cfa_rtx;
6295 #ifdef DWARF2_UNWIND_INFO
6296 case BUILT_IN_DWARF_SP_COLUMN:
6297 return expand_builtin_dwarf_sp_column ();
6298 case BUILT_IN_INIT_DWARF_REG_SIZES:
6299 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6300 return const0_rtx;
6301 #endif
6302 case BUILT_IN_FROB_RETURN_ADDR:
6303 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6304 case BUILT_IN_EXTRACT_RETURN_ADDR:
6305 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6306 case BUILT_IN_EH_RETURN:
6307 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6308 CALL_EXPR_ARG (exp, 1));
6309 return const0_rtx;
6310 #ifdef EH_RETURN_DATA_REGNO
6311 case BUILT_IN_EH_RETURN_DATA_REGNO:
6312 return expand_builtin_eh_return_data_regno (exp);
6313 #endif
6314 case BUILT_IN_EXTEND_POINTER:
6315 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6316 case BUILT_IN_EH_POINTER:
6317 return expand_builtin_eh_pointer (exp);
6318 case BUILT_IN_EH_FILTER:
6319 return expand_builtin_eh_filter (exp);
6320 case BUILT_IN_EH_COPY_VALUES:
6321 return expand_builtin_eh_copy_values (exp);
6322
6323 case BUILT_IN_VA_START:
6324 return expand_builtin_va_start (exp);
6325 case BUILT_IN_VA_END:
6326 return expand_builtin_va_end (exp);
6327 case BUILT_IN_VA_COPY:
6328 return expand_builtin_va_copy (exp);
6329 case BUILT_IN_EXPECT:
6330 return expand_builtin_expect (exp, target);
6331 case BUILT_IN_ASSUME_ALIGNED:
6332 return expand_builtin_assume_aligned (exp, target);
6333 case BUILT_IN_PREFETCH:
6334 expand_builtin_prefetch (exp);
6335 return const0_rtx;
6336
6337 case BUILT_IN_INIT_TRAMPOLINE:
6338 return expand_builtin_init_trampoline (exp, true);
6339 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6340 return expand_builtin_init_trampoline (exp, false);
6341 case BUILT_IN_ADJUST_TRAMPOLINE:
6342 return expand_builtin_adjust_trampoline (exp);
6343
6344 case BUILT_IN_FORK:
6345 case BUILT_IN_EXECL:
6346 case BUILT_IN_EXECV:
6347 case BUILT_IN_EXECLP:
6348 case BUILT_IN_EXECLE:
6349 case BUILT_IN_EXECVP:
6350 case BUILT_IN_EXECVE:
6351 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6352 if (target)
6353 return target;
6354 break;
6355
6356 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6357 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6358 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6359 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6360 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6361 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6362 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6363 if (target)
6364 return target;
6365 break;
6366
6367 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6368 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6369 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6370 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6371 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6372 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6373 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6374 if (target)
6375 return target;
6376 break;
6377
6378 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6379 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6380 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6381 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6382 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6383 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6384 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6385 if (target)
6386 return target;
6387 break;
6388
6389 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6390 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6391 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6392 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6393 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6394 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6395 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6396 if (target)
6397 return target;
6398 break;
6399
6400 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6401 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6402 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6403 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6404 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6405 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6406 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6407 if (target)
6408 return target;
6409 break;
6410
6411 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6412 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6413 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6414 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6415 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6417 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6418 if (target)
6419 return target;
6420 break;
6421
6422 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6423 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6424 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6425 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6426 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6427 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6428 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6429 if (target)
6430 return target;
6431 break;
6432
6433 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6434 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6435 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6436 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6437 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6438 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6439 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6440 if (target)
6441 return target;
6442 break;
6443
6444 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6445 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6446 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6447 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6448 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6449 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6450 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6451 if (target)
6452 return target;
6453 break;
6454
6455 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6456 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6457 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6458 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6459 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6460 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6461 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6462 if (target)
6463 return target;
6464 break;
6465
6466 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6467 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6468 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6469 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6470 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6471 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6472 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6473 if (target)
6474 return target;
6475 break;
6476
6477 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6478 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6479 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6480 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6481 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6482 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6483 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6484 if (target)
6485 return target;
6486 break;
6487
6488 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6489 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6490 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6491 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6492 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6493 if (mode == VOIDmode)
6494 mode = TYPE_MODE (boolean_type_node);
6495 if (!target || !register_operand (target, mode))
6496 target = gen_reg_rtx (mode);
6497
6498 mode = get_builtin_sync_mode
6499 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6500 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6501 if (target)
6502 return target;
6503 break;
6504
6505 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6506 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6507 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6508 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6509 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6510 mode = get_builtin_sync_mode
6511 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6512 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6513 if (target)
6514 return target;
6515 break;
6516
6517 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6518 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6519 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6520 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6521 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6522 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6523 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6524 if (target)
6525 return target;
6526 break;
6527
6528 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6529 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6530 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6531 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6532 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6533 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6534 expand_builtin_sync_lock_release (mode, exp);
6535 return const0_rtx;
6536
6537 case BUILT_IN_SYNC_SYNCHRONIZE:
6538 expand_builtin_sync_synchronize ();
6539 return const0_rtx;
6540
6541 case BUILT_IN_ATOMIC_EXCHANGE_1:
6542 case BUILT_IN_ATOMIC_EXCHANGE_2:
6543 case BUILT_IN_ATOMIC_EXCHANGE_4:
6544 case BUILT_IN_ATOMIC_EXCHANGE_8:
6545 case BUILT_IN_ATOMIC_EXCHANGE_16:
6546 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6547 target = expand_builtin_atomic_exchange (mode, exp, target);
6548 if (target)
6549 return target;
6550 break;
6551
6552 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6553 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6554 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6555 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6556 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6557 {
6558 unsigned int nargs, z;
6559 vec<tree, va_gc> *vec;
6560
6561 mode =
6562 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6563 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6564 if (target)
6565 return target;
6566
6567 /* If this is turned into an external library call, the weak parameter
6568 must be dropped to match the expected parameter list. */
6569 nargs = call_expr_nargs (exp);
6570 vec_alloc (vec, nargs - 1);
6571 for (z = 0; z < 3; z++)
6572 vec->quick_push (CALL_EXPR_ARG (exp, z));
6573 /* Skip the boolean weak parameter. */
6574 for (z = 4; z < 6; z++)
6575 vec->quick_push (CALL_EXPR_ARG (exp, z));
6576 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6577 break;
6578 }
6579
6580 case BUILT_IN_ATOMIC_LOAD_1:
6581 case BUILT_IN_ATOMIC_LOAD_2:
6582 case BUILT_IN_ATOMIC_LOAD_4:
6583 case BUILT_IN_ATOMIC_LOAD_8:
6584 case BUILT_IN_ATOMIC_LOAD_16:
6585 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6586 target = expand_builtin_atomic_load (mode, exp, target);
6587 if (target)
6588 return target;
6589 break;
6590
6591 case BUILT_IN_ATOMIC_STORE_1:
6592 case BUILT_IN_ATOMIC_STORE_2:
6593 case BUILT_IN_ATOMIC_STORE_4:
6594 case BUILT_IN_ATOMIC_STORE_8:
6595 case BUILT_IN_ATOMIC_STORE_16:
6596 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6597 target = expand_builtin_atomic_store (mode, exp);
6598 if (target)
6599 return const0_rtx;
6600 break;
6601
6602 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6603 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6604 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6605 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6606 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6607 {
6608 enum built_in_function lib;
6609 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6610 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6611 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6612 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6613 ignore, lib);
6614 if (target)
6615 return target;
6616 break;
6617 }
6618 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6619 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6620 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6621 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6622 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6623 {
6624 enum built_in_function lib;
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6626 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6627 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6628 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6629 ignore, lib);
6630 if (target)
6631 return target;
6632 break;
6633 }
6634 case BUILT_IN_ATOMIC_AND_FETCH_1:
6635 case BUILT_IN_ATOMIC_AND_FETCH_2:
6636 case BUILT_IN_ATOMIC_AND_FETCH_4:
6637 case BUILT_IN_ATOMIC_AND_FETCH_8:
6638 case BUILT_IN_ATOMIC_AND_FETCH_16:
6639 {
6640 enum built_in_function lib;
6641 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6642 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6643 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6644 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6645 ignore, lib);
6646 if (target)
6647 return target;
6648 break;
6649 }
6650 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6651 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6652 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6653 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6654 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6655 {
6656 enum built_in_function lib;
6657 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6658 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6659 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6660 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6661 ignore, lib);
6662 if (target)
6663 return target;
6664 break;
6665 }
6666 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6667 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6668 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6669 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6670 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6671 {
6672 enum built_in_function lib;
6673 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6674 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6675 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6676 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6677 ignore, lib);
6678 if (target)
6679 return target;
6680 break;
6681 }
6682 case BUILT_IN_ATOMIC_OR_FETCH_1:
6683 case BUILT_IN_ATOMIC_OR_FETCH_2:
6684 case BUILT_IN_ATOMIC_OR_FETCH_4:
6685 case BUILT_IN_ATOMIC_OR_FETCH_8:
6686 case BUILT_IN_ATOMIC_OR_FETCH_16:
6687 {
6688 enum built_in_function lib;
6689 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6690 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6691 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6692 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6693 ignore, lib);
6694 if (target)
6695 return target;
6696 break;
6697 }
6698 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6699 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6700 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6701 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6702 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6703 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6704 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6705 ignore, BUILT_IN_NONE);
6706 if (target)
6707 return target;
6708 break;
6709
6710 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6711 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6712 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6713 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6714 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6715 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6716 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6717 ignore, BUILT_IN_NONE);
6718 if (target)
6719 return target;
6720 break;
6721
6722 case BUILT_IN_ATOMIC_FETCH_AND_1:
6723 case BUILT_IN_ATOMIC_FETCH_AND_2:
6724 case BUILT_IN_ATOMIC_FETCH_AND_4:
6725 case BUILT_IN_ATOMIC_FETCH_AND_8:
6726 case BUILT_IN_ATOMIC_FETCH_AND_16:
6727 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6728 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6729 ignore, BUILT_IN_NONE);
6730 if (target)
6731 return target;
6732 break;
6733
6734 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6735 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6736 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6737 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6738 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6739 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6740 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6741 ignore, BUILT_IN_NONE);
6742 if (target)
6743 return target;
6744 break;
6745
6746 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6747 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6748 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6749 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6750 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6751 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6752 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6753 ignore, BUILT_IN_NONE);
6754 if (target)
6755 return target;
6756 break;
6757
6758 case BUILT_IN_ATOMIC_FETCH_OR_1:
6759 case BUILT_IN_ATOMIC_FETCH_OR_2:
6760 case BUILT_IN_ATOMIC_FETCH_OR_4:
6761 case BUILT_IN_ATOMIC_FETCH_OR_8:
6762 case BUILT_IN_ATOMIC_FETCH_OR_16:
6763 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6764 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6765 ignore, BUILT_IN_NONE);
6766 if (target)
6767 return target;
6768 break;
6769
6770 case BUILT_IN_ATOMIC_TEST_AND_SET:
6771 return expand_builtin_atomic_test_and_set (exp, target);
6772
6773 case BUILT_IN_ATOMIC_CLEAR:
6774 return expand_builtin_atomic_clear (exp);
6775
6776 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6777 return expand_builtin_atomic_always_lock_free (exp);
6778
6779 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6780 target = expand_builtin_atomic_is_lock_free (exp);
6781 if (target)
6782 return target;
6783 break;
6784
6785 case BUILT_IN_ATOMIC_THREAD_FENCE:
6786 expand_builtin_atomic_thread_fence (exp);
6787 return const0_rtx;
6788
6789 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6790 expand_builtin_atomic_signal_fence (exp);
6791 return const0_rtx;
6792
6793 case BUILT_IN_OBJECT_SIZE:
6794 return expand_builtin_object_size (exp);
6795
6796 case BUILT_IN_MEMCPY_CHK:
6797 case BUILT_IN_MEMPCPY_CHK:
6798 case BUILT_IN_MEMMOVE_CHK:
6799 case BUILT_IN_MEMSET_CHK:
6800 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6801 if (target)
6802 return target;
6803 break;
6804
6805 case BUILT_IN_STRCPY_CHK:
6806 case BUILT_IN_STPCPY_CHK:
6807 case BUILT_IN_STRNCPY_CHK:
6808 case BUILT_IN_STPNCPY_CHK:
6809 case BUILT_IN_STRCAT_CHK:
6810 case BUILT_IN_STRNCAT_CHK:
6811 case BUILT_IN_SNPRINTF_CHK:
6812 case BUILT_IN_VSNPRINTF_CHK:
6813 maybe_emit_chk_warning (exp, fcode);
6814 break;
6815
6816 case BUILT_IN_SPRINTF_CHK:
6817 case BUILT_IN_VSPRINTF_CHK:
6818 maybe_emit_sprintf_chk_warning (exp, fcode);
6819 break;
6820
6821 case BUILT_IN_FREE:
6822 if (warn_free_nonheap_object)
6823 maybe_emit_free_warning (exp);
6824 break;
6825
6826 case BUILT_IN_THREAD_POINTER:
6827 return expand_builtin_thread_pointer (exp, target);
6828
6829 case BUILT_IN_SET_THREAD_POINTER:
6830 expand_builtin_set_thread_pointer (exp);
6831 return const0_rtx;
6832
6833 case BUILT_IN_CILK_DETACH:
6834 expand_builtin_cilk_detach (exp);
6835 return const0_rtx;
6836
6837 case BUILT_IN_CILK_POP_FRAME:
6838 expand_builtin_cilk_pop_frame (exp);
6839 return const0_rtx;
6840
6841 default: /* just do library call, if unknown builtin */
6842 break;
6843 }
6844
6845 /* The switch statement above can drop through to cause the function
6846 to be called normally. */
6847 return expand_call (exp, target, ignore);
6848 }
6849
6850 /* Determine whether a tree node represents a call to a built-in
6851 function. If the tree T is a call to a built-in function with
6852 the right number of arguments of the appropriate types, return
6853 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6854 Otherwise the return value is END_BUILTINS. */
6855
6856 enum built_in_function
6857 builtin_mathfn_code (const_tree t)
6858 {
6859 const_tree fndecl, arg, parmlist;
6860 const_tree argtype, parmtype;
6861 const_call_expr_arg_iterator iter;
6862
6863 if (TREE_CODE (t) != CALL_EXPR
6864 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6865 return END_BUILTINS;
6866
6867 fndecl = get_callee_fndecl (t);
6868 if (fndecl == NULL_TREE
6869 || TREE_CODE (fndecl) != FUNCTION_DECL
6870 || ! DECL_BUILT_IN (fndecl)
6871 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6872 return END_BUILTINS;
6873
6874 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6875 init_const_call_expr_arg_iterator (t, &iter);
6876 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6877 {
6878 /* If a function doesn't take a variable number of arguments,
6879 the last element in the list will have type `void'. */
6880 parmtype = TREE_VALUE (parmlist);
6881 if (VOID_TYPE_P (parmtype))
6882 {
6883 if (more_const_call_expr_args_p (&iter))
6884 return END_BUILTINS;
6885 return DECL_FUNCTION_CODE (fndecl);
6886 }
6887
6888 if (! more_const_call_expr_args_p (&iter))
6889 return END_BUILTINS;
6890
6891 arg = next_const_call_expr_arg (&iter);
6892 argtype = TREE_TYPE (arg);
6893
6894 if (SCALAR_FLOAT_TYPE_P (parmtype))
6895 {
6896 if (! SCALAR_FLOAT_TYPE_P (argtype))
6897 return END_BUILTINS;
6898 }
6899 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6900 {
6901 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6902 return END_BUILTINS;
6903 }
6904 else if (POINTER_TYPE_P (parmtype))
6905 {
6906 if (! POINTER_TYPE_P (argtype))
6907 return END_BUILTINS;
6908 }
6909 else if (INTEGRAL_TYPE_P (parmtype))
6910 {
6911 if (! INTEGRAL_TYPE_P (argtype))
6912 return END_BUILTINS;
6913 }
6914 else
6915 return END_BUILTINS;
6916 }
6917
6918 /* Variable-length argument list. */
6919 return DECL_FUNCTION_CODE (fndecl);
6920 }
6921
6922 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6923 evaluate to a constant. */
6924
6925 static tree
6926 fold_builtin_constant_p (tree arg)
6927 {
6928 /* We return 1 for a numeric type that's known to be a constant
6929 value at compile-time or for an aggregate type that's a
6930 literal constant. */
6931 STRIP_NOPS (arg);
6932
6933 /* If we know this is a constant, emit the constant of one. */
6934 if (CONSTANT_CLASS_P (arg)
6935 || (TREE_CODE (arg) == CONSTRUCTOR
6936 && TREE_CONSTANT (arg)))
6937 return integer_one_node;
6938 if (TREE_CODE (arg) == ADDR_EXPR)
6939 {
6940 tree op = TREE_OPERAND (arg, 0);
6941 if (TREE_CODE (op) == STRING_CST
6942 || (TREE_CODE (op) == ARRAY_REF
6943 && integer_zerop (TREE_OPERAND (op, 1))
6944 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6945 return integer_one_node;
6946 }
6947
6948 /* If this expression has side effects, show we don't know it to be a
6949 constant. Likewise if it's a pointer or aggregate type since in
6950 those case we only want literals, since those are only optimized
6951 when generating RTL, not later.
6952 And finally, if we are compiling an initializer, not code, we
6953 need to return a definite result now; there's not going to be any
6954 more optimization done. */
6955 if (TREE_SIDE_EFFECTS (arg)
6956 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6957 || POINTER_TYPE_P (TREE_TYPE (arg))
6958 || cfun == 0
6959 || folding_initializer
6960 || force_folding_builtin_constant_p)
6961 return integer_zero_node;
6962
6963 return NULL_TREE;
6964 }
6965
6966 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6967 return it as a truthvalue. */
6968
6969 static tree
6970 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6971 {
6972 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6973
6974 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6975 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6976 ret_type = TREE_TYPE (TREE_TYPE (fn));
6977 pred_type = TREE_VALUE (arg_types);
6978 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6979
6980 pred = fold_convert_loc (loc, pred_type, pred);
6981 expected = fold_convert_loc (loc, expected_type, expected);
6982 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6983
6984 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6985 build_int_cst (ret_type, 0));
6986 }
6987
6988 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6989 NULL_TREE if no simplification is possible. */
6990
6991 static tree
6992 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6993 {
6994 tree inner, fndecl, inner_arg0;
6995 enum tree_code code;
6996
6997 /* Distribute the expected value over short-circuiting operators.
6998 See through the cast from truthvalue_type_node to long. */
6999 inner_arg0 = arg0;
7000 while (TREE_CODE (inner_arg0) == NOP_EXPR
7001 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7002 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7003 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7004
7005 /* If this is a builtin_expect within a builtin_expect keep the
7006 inner one. See through a comparison against a constant. It
7007 might have been added to create a thruthvalue. */
7008 inner = inner_arg0;
7009
7010 if (COMPARISON_CLASS_P (inner)
7011 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7012 inner = TREE_OPERAND (inner, 0);
7013
7014 if (TREE_CODE (inner) == CALL_EXPR
7015 && (fndecl = get_callee_fndecl (inner))
7016 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7017 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7018 return arg0;
7019
7020 inner = inner_arg0;
7021 code = TREE_CODE (inner);
7022 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7023 {
7024 tree op0 = TREE_OPERAND (inner, 0);
7025 tree op1 = TREE_OPERAND (inner, 1);
7026
7027 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7028 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7029 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7030
7031 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7032 }
7033
7034 /* If the argument isn't invariant then there's nothing else we can do. */
7035 if (!TREE_CONSTANT (inner_arg0))
7036 return NULL_TREE;
7037
7038 /* If we expect that a comparison against the argument will fold to
7039 a constant return the constant. In practice, this means a true
7040 constant or the address of a non-weak symbol. */
7041 inner = inner_arg0;
7042 STRIP_NOPS (inner);
7043 if (TREE_CODE (inner) == ADDR_EXPR)
7044 {
7045 do
7046 {
7047 inner = TREE_OPERAND (inner, 0);
7048 }
7049 while (TREE_CODE (inner) == COMPONENT_REF
7050 || TREE_CODE (inner) == ARRAY_REF);
7051 if ((TREE_CODE (inner) == VAR_DECL
7052 || TREE_CODE (inner) == FUNCTION_DECL)
7053 && DECL_WEAK (inner))
7054 return NULL_TREE;
7055 }
7056
7057 /* Otherwise, ARG0 already has the proper type for the return value. */
7058 return arg0;
7059 }
7060
7061 /* Fold a call to __builtin_classify_type with argument ARG. */
7062
7063 static tree
7064 fold_builtin_classify_type (tree arg)
7065 {
7066 if (arg == 0)
7067 return build_int_cst (integer_type_node, no_type_class);
7068
7069 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7070 }
7071
7072 /* Fold a call to __builtin_strlen with argument ARG. */
7073
7074 static tree
7075 fold_builtin_strlen (location_t loc, tree type, tree arg)
7076 {
7077 if (!validate_arg (arg, POINTER_TYPE))
7078 return NULL_TREE;
7079 else
7080 {
7081 tree len = c_strlen (arg, 0);
7082
7083 if (len)
7084 return fold_convert_loc (loc, type, len);
7085
7086 return NULL_TREE;
7087 }
7088 }
7089
7090 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7091
7092 static tree
7093 fold_builtin_inf (location_t loc, tree type, int warn)
7094 {
7095 REAL_VALUE_TYPE real;
7096
7097 /* __builtin_inff is intended to be usable to define INFINITY on all
7098 targets. If an infinity is not available, INFINITY expands "to a
7099 positive constant of type float that overflows at translation
7100 time", footnote "In this case, using INFINITY will violate the
7101 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7102 Thus we pedwarn to ensure this constraint violation is
7103 diagnosed. */
7104 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7105 pedwarn (loc, 0, "target format does not support infinity");
7106
7107 real_inf (&real);
7108 return build_real (type, real);
7109 }
7110
7111 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7112
7113 static tree
7114 fold_builtin_nan (tree arg, tree type, int quiet)
7115 {
7116 REAL_VALUE_TYPE real;
7117 const char *str;
7118
7119 if (!validate_arg (arg, POINTER_TYPE))
7120 return NULL_TREE;
7121 str = c_getstr (arg);
7122 if (!str)
7123 return NULL_TREE;
7124
7125 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7126 return NULL_TREE;
7127
7128 return build_real (type, real);
7129 }
7130
7131 /* Return true if the floating point expression T has an integer value.
7132 We also allow +Inf, -Inf and NaN to be considered integer values. */
7133
7134 static bool
7135 integer_valued_real_p (tree t)
7136 {
7137 switch (TREE_CODE (t))
7138 {
7139 case FLOAT_EXPR:
7140 return true;
7141
7142 case ABS_EXPR:
7143 case SAVE_EXPR:
7144 return integer_valued_real_p (TREE_OPERAND (t, 0));
7145
7146 case COMPOUND_EXPR:
7147 case MODIFY_EXPR:
7148 case BIND_EXPR:
7149 return integer_valued_real_p (TREE_OPERAND (t, 1));
7150
7151 case PLUS_EXPR:
7152 case MINUS_EXPR:
7153 case MULT_EXPR:
7154 case MIN_EXPR:
7155 case MAX_EXPR:
7156 return integer_valued_real_p (TREE_OPERAND (t, 0))
7157 && integer_valued_real_p (TREE_OPERAND (t, 1));
7158
7159 case COND_EXPR:
7160 return integer_valued_real_p (TREE_OPERAND (t, 1))
7161 && integer_valued_real_p (TREE_OPERAND (t, 2));
7162
7163 case REAL_CST:
7164 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7165
7166 case NOP_EXPR:
7167 {
7168 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7169 if (TREE_CODE (type) == INTEGER_TYPE)
7170 return true;
7171 if (TREE_CODE (type) == REAL_TYPE)
7172 return integer_valued_real_p (TREE_OPERAND (t, 0));
7173 break;
7174 }
7175
7176 case CALL_EXPR:
7177 switch (builtin_mathfn_code (t))
7178 {
7179 CASE_FLT_FN (BUILT_IN_CEIL):
7180 CASE_FLT_FN (BUILT_IN_FLOOR):
7181 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7182 CASE_FLT_FN (BUILT_IN_RINT):
7183 CASE_FLT_FN (BUILT_IN_ROUND):
7184 CASE_FLT_FN (BUILT_IN_TRUNC):
7185 return true;
7186
7187 CASE_FLT_FN (BUILT_IN_FMIN):
7188 CASE_FLT_FN (BUILT_IN_FMAX):
7189 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7190 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7191
7192 default:
7193 break;
7194 }
7195 break;
7196
7197 default:
7198 break;
7199 }
7200 return false;
7201 }
7202
7203 /* FNDECL is assumed to be a builtin where truncation can be propagated
7204 across (for instance floor((double)f) == (double)floorf (f).
7205 Do the transformation for a call with argument ARG. */
7206
7207 static tree
7208 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7209 {
7210 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7211
7212 if (!validate_arg (arg, REAL_TYPE))
7213 return NULL_TREE;
7214
7215 /* Integer rounding functions are idempotent. */
7216 if (fcode == builtin_mathfn_code (arg))
7217 return arg;
7218
7219 /* If argument is already integer valued, and we don't need to worry
7220 about setting errno, there's no need to perform rounding. */
7221 if (! flag_errno_math && integer_valued_real_p (arg))
7222 return arg;
7223
7224 if (optimize)
7225 {
7226 tree arg0 = strip_float_extensions (arg);
7227 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7228 tree newtype = TREE_TYPE (arg0);
7229 tree decl;
7230
7231 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7232 && (decl = mathfn_built_in (newtype, fcode)))
7233 return fold_convert_loc (loc, ftype,
7234 build_call_expr_loc (loc, decl, 1,
7235 fold_convert_loc (loc,
7236 newtype,
7237 arg0)));
7238 }
7239 return NULL_TREE;
7240 }
7241
7242 /* FNDECL is assumed to be builtin which can narrow the FP type of
7243 the argument, for instance lround((double)f) -> lroundf (f).
7244 Do the transformation for a call with argument ARG. */
7245
7246 static tree
7247 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7248 {
7249 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7250
7251 if (!validate_arg (arg, REAL_TYPE))
7252 return NULL_TREE;
7253
7254 /* If argument is already integer valued, and we don't need to worry
7255 about setting errno, there's no need to perform rounding. */
7256 if (! flag_errno_math && integer_valued_real_p (arg))
7257 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7258 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7259
7260 if (optimize)
7261 {
7262 tree ftype = TREE_TYPE (arg);
7263 tree arg0 = strip_float_extensions (arg);
7264 tree newtype = TREE_TYPE (arg0);
7265 tree decl;
7266
7267 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7268 && (decl = mathfn_built_in (newtype, fcode)))
7269 return build_call_expr_loc (loc, decl, 1,
7270 fold_convert_loc (loc, newtype, arg0));
7271 }
7272
7273 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7274 sizeof (int) == sizeof (long). */
7275 if (TYPE_PRECISION (integer_type_node)
7276 == TYPE_PRECISION (long_integer_type_node))
7277 {
7278 tree newfn = NULL_TREE;
7279 switch (fcode)
7280 {
7281 CASE_FLT_FN (BUILT_IN_ICEIL):
7282 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7283 break;
7284
7285 CASE_FLT_FN (BUILT_IN_IFLOOR):
7286 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7287 break;
7288
7289 CASE_FLT_FN (BUILT_IN_IROUND):
7290 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7291 break;
7292
7293 CASE_FLT_FN (BUILT_IN_IRINT):
7294 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7295 break;
7296
7297 default:
7298 break;
7299 }
7300
7301 if (newfn)
7302 {
7303 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7304 return fold_convert_loc (loc,
7305 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7306 }
7307 }
7308
7309 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7310 sizeof (long long) == sizeof (long). */
7311 if (TYPE_PRECISION (long_long_integer_type_node)
7312 == TYPE_PRECISION (long_integer_type_node))
7313 {
7314 tree newfn = NULL_TREE;
7315 switch (fcode)
7316 {
7317 CASE_FLT_FN (BUILT_IN_LLCEIL):
7318 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7319 break;
7320
7321 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7322 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7323 break;
7324
7325 CASE_FLT_FN (BUILT_IN_LLROUND):
7326 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7327 break;
7328
7329 CASE_FLT_FN (BUILT_IN_LLRINT):
7330 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7331 break;
7332
7333 default:
7334 break;
7335 }
7336
7337 if (newfn)
7338 {
7339 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7340 return fold_convert_loc (loc,
7341 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7342 }
7343 }
7344
7345 return NULL_TREE;
7346 }
7347
7348 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7349 return type. Return NULL_TREE if no simplification can be made. */
7350
7351 static tree
7352 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7353 {
7354 tree res;
7355
7356 if (!validate_arg (arg, COMPLEX_TYPE)
7357 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7358 return NULL_TREE;
7359
7360 /* Calculate the result when the argument is a constant. */
7361 if (TREE_CODE (arg) == COMPLEX_CST
7362 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7363 type, mpfr_hypot)))
7364 return res;
7365
7366 if (TREE_CODE (arg) == COMPLEX_EXPR)
7367 {
7368 tree real = TREE_OPERAND (arg, 0);
7369 tree imag = TREE_OPERAND (arg, 1);
7370
7371 /* If either part is zero, cabs is fabs of the other. */
7372 if (real_zerop (real))
7373 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7374 if (real_zerop (imag))
7375 return fold_build1_loc (loc, ABS_EXPR, type, real);
7376
7377 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7378 if (flag_unsafe_math_optimizations
7379 && operand_equal_p (real, imag, OEP_PURE_SAME))
7380 {
7381 const REAL_VALUE_TYPE sqrt2_trunc
7382 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7383 STRIP_NOPS (real);
7384 return fold_build2_loc (loc, MULT_EXPR, type,
7385 fold_build1_loc (loc, ABS_EXPR, type, real),
7386 build_real (type, sqrt2_trunc));
7387 }
7388 }
7389
7390 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7391 if (TREE_CODE (arg) == NEGATE_EXPR
7392 || TREE_CODE (arg) == CONJ_EXPR)
7393 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7394
7395 /* Don't do this when optimizing for size. */
7396 if (flag_unsafe_math_optimizations
7397 && optimize && optimize_function_for_speed_p (cfun))
7398 {
7399 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7400
7401 if (sqrtfn != NULL_TREE)
7402 {
7403 tree rpart, ipart, result;
7404
7405 arg = builtin_save_expr (arg);
7406
7407 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7408 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7409
7410 rpart = builtin_save_expr (rpart);
7411 ipart = builtin_save_expr (ipart);
7412
7413 result = fold_build2_loc (loc, PLUS_EXPR, type,
7414 fold_build2_loc (loc, MULT_EXPR, type,
7415 rpart, rpart),
7416 fold_build2_loc (loc, MULT_EXPR, type,
7417 ipart, ipart));
7418
7419 return build_call_expr_loc (loc, sqrtfn, 1, result);
7420 }
7421 }
7422
7423 return NULL_TREE;
7424 }
7425
7426 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7427 complex tree type of the result. If NEG is true, the imaginary
7428 zero is negative. */
7429
7430 static tree
7431 build_complex_cproj (tree type, bool neg)
7432 {
7433 REAL_VALUE_TYPE rinf, rzero = dconst0;
7434
7435 real_inf (&rinf);
7436 rzero.sign = neg;
7437 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7438 build_real (TREE_TYPE (type), rzero));
7439 }
7440
7441 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7442 return type. Return NULL_TREE if no simplification can be made. */
7443
7444 static tree
7445 fold_builtin_cproj (location_t loc, tree arg, tree type)
7446 {
7447 if (!validate_arg (arg, COMPLEX_TYPE)
7448 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7449 return NULL_TREE;
7450
7451 /* If there are no infinities, return arg. */
7452 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7453 return non_lvalue_loc (loc, arg);
7454
7455 /* Calculate the result when the argument is a constant. */
7456 if (TREE_CODE (arg) == COMPLEX_CST)
7457 {
7458 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7459 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7460
7461 if (real_isinf (real) || real_isinf (imag))
7462 return build_complex_cproj (type, imag->sign);
7463 else
7464 return arg;
7465 }
7466 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7467 {
7468 tree real = TREE_OPERAND (arg, 0);
7469 tree imag = TREE_OPERAND (arg, 1);
7470
7471 STRIP_NOPS (real);
7472 STRIP_NOPS (imag);
7473
7474 /* If the real part is inf and the imag part is known to be
7475 nonnegative, return (inf + 0i). Remember side-effects are
7476 possible in the imag part. */
7477 if (TREE_CODE (real) == REAL_CST
7478 && real_isinf (TREE_REAL_CST_PTR (real))
7479 && tree_expr_nonnegative_p (imag))
7480 return omit_one_operand_loc (loc, type,
7481 build_complex_cproj (type, false),
7482 arg);
7483
7484 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7485 Remember side-effects are possible in the real part. */
7486 if (TREE_CODE (imag) == REAL_CST
7487 && real_isinf (TREE_REAL_CST_PTR (imag)))
7488 return
7489 omit_one_operand_loc (loc, type,
7490 build_complex_cproj (type, TREE_REAL_CST_PTR
7491 (imag)->sign), arg);
7492 }
7493
7494 return NULL_TREE;
7495 }
7496
7497 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7498 Return NULL_TREE if no simplification can be made. */
7499
7500 static tree
7501 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7502 {
7503
7504 enum built_in_function fcode;
7505 tree res;
7506
7507 if (!validate_arg (arg, REAL_TYPE))
7508 return NULL_TREE;
7509
7510 /* Calculate the result when the argument is a constant. */
7511 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7512 return res;
7513
7514 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7515 fcode = builtin_mathfn_code (arg);
7516 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7517 {
7518 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7519 arg = fold_build2_loc (loc, MULT_EXPR, type,
7520 CALL_EXPR_ARG (arg, 0),
7521 build_real (type, dconsthalf));
7522 return build_call_expr_loc (loc, expfn, 1, arg);
7523 }
7524
7525 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7526 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7527 {
7528 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7529
7530 if (powfn)
7531 {
7532 tree arg0 = CALL_EXPR_ARG (arg, 0);
7533 tree tree_root;
7534 /* The inner root was either sqrt or cbrt. */
7535 /* This was a conditional expression but it triggered a bug
7536 in Sun C 5.5. */
7537 REAL_VALUE_TYPE dconstroot;
7538 if (BUILTIN_SQRT_P (fcode))
7539 dconstroot = dconsthalf;
7540 else
7541 dconstroot = dconst_third ();
7542
7543 /* Adjust for the outer root. */
7544 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7545 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7546 tree_root = build_real (type, dconstroot);
7547 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7548 }
7549 }
7550
7551 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7552 if (flag_unsafe_math_optimizations
7553 && (fcode == BUILT_IN_POW
7554 || fcode == BUILT_IN_POWF
7555 || fcode == BUILT_IN_POWL))
7556 {
7557 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7558 tree arg0 = CALL_EXPR_ARG (arg, 0);
7559 tree arg1 = CALL_EXPR_ARG (arg, 1);
7560 tree narg1;
7561 if (!tree_expr_nonnegative_p (arg0))
7562 arg0 = build1 (ABS_EXPR, type, arg0);
7563 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7564 build_real (type, dconsthalf));
7565 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7566 }
7567
7568 return NULL_TREE;
7569 }
7570
7571 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7572 Return NULL_TREE if no simplification can be made. */
7573
7574 static tree
7575 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7576 {
7577 const enum built_in_function fcode = builtin_mathfn_code (arg);
7578 tree res;
7579
7580 if (!validate_arg (arg, REAL_TYPE))
7581 return NULL_TREE;
7582
7583 /* Calculate the result when the argument is a constant. */
7584 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7585 return res;
7586
7587 if (flag_unsafe_math_optimizations)
7588 {
7589 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7590 if (BUILTIN_EXPONENT_P (fcode))
7591 {
7592 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7593 const REAL_VALUE_TYPE third_trunc =
7594 real_value_truncate (TYPE_MODE (type), dconst_third ());
7595 arg = fold_build2_loc (loc, MULT_EXPR, type,
7596 CALL_EXPR_ARG (arg, 0),
7597 build_real (type, third_trunc));
7598 return build_call_expr_loc (loc, expfn, 1, arg);
7599 }
7600
7601 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7602 if (BUILTIN_SQRT_P (fcode))
7603 {
7604 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7605
7606 if (powfn)
7607 {
7608 tree arg0 = CALL_EXPR_ARG (arg, 0);
7609 tree tree_root;
7610 REAL_VALUE_TYPE dconstroot = dconst_third ();
7611
7612 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7613 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7614 tree_root = build_real (type, dconstroot);
7615 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7616 }
7617 }
7618
7619 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7620 if (BUILTIN_CBRT_P (fcode))
7621 {
7622 tree arg0 = CALL_EXPR_ARG (arg, 0);
7623 if (tree_expr_nonnegative_p (arg0))
7624 {
7625 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7626
7627 if (powfn)
7628 {
7629 tree tree_root;
7630 REAL_VALUE_TYPE dconstroot;
7631
7632 real_arithmetic (&dconstroot, MULT_EXPR,
7633 dconst_third_ptr (), dconst_third_ptr ());
7634 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7635 tree_root = build_real (type, dconstroot);
7636 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7637 }
7638 }
7639 }
7640
7641 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7642 if (fcode == BUILT_IN_POW
7643 || fcode == BUILT_IN_POWF
7644 || fcode == BUILT_IN_POWL)
7645 {
7646 tree arg00 = CALL_EXPR_ARG (arg, 0);
7647 tree arg01 = CALL_EXPR_ARG (arg, 1);
7648 if (tree_expr_nonnegative_p (arg00))
7649 {
7650 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7651 const REAL_VALUE_TYPE dconstroot
7652 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7653 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7654 build_real (type, dconstroot));
7655 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7656 }
7657 }
7658 }
7659 return NULL_TREE;
7660 }
7661
7662 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7663 TYPE is the type of the return value. Return NULL_TREE if no
7664 simplification can be made. */
7665
7666 static tree
7667 fold_builtin_cos (location_t loc,
7668 tree arg, tree type, tree fndecl)
7669 {
7670 tree res, narg;
7671
7672 if (!validate_arg (arg, REAL_TYPE))
7673 return NULL_TREE;
7674
7675 /* Calculate the result when the argument is a constant. */
7676 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7677 return res;
7678
7679 /* Optimize cos(-x) into cos (x). */
7680 if ((narg = fold_strip_sign_ops (arg)))
7681 return build_call_expr_loc (loc, fndecl, 1, narg);
7682
7683 return NULL_TREE;
7684 }
7685
7686 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7687 Return NULL_TREE if no simplification can be made. */
7688
7689 static tree
7690 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7691 {
7692 if (validate_arg (arg, REAL_TYPE))
7693 {
7694 tree res, narg;
7695
7696 /* Calculate the result when the argument is a constant. */
7697 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7698 return res;
7699
7700 /* Optimize cosh(-x) into cosh (x). */
7701 if ((narg = fold_strip_sign_ops (arg)))
7702 return build_call_expr_loc (loc, fndecl, 1, narg);
7703 }
7704
7705 return NULL_TREE;
7706 }
7707
7708 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7709 argument ARG. TYPE is the type of the return value. Return
7710 NULL_TREE if no simplification can be made. */
7711
7712 static tree
7713 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7714 bool hyper)
7715 {
7716 if (validate_arg (arg, COMPLEX_TYPE)
7717 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7718 {
7719 tree tmp;
7720
7721 /* Calculate the result when the argument is a constant. */
7722 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7723 return tmp;
7724
7725 /* Optimize fn(-x) into fn(x). */
7726 if ((tmp = fold_strip_sign_ops (arg)))
7727 return build_call_expr_loc (loc, fndecl, 1, tmp);
7728 }
7729
7730 return NULL_TREE;
7731 }
7732
7733 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7734 Return NULL_TREE if no simplification can be made. */
7735
7736 static tree
7737 fold_builtin_tan (tree arg, tree type)
7738 {
7739 enum built_in_function fcode;
7740 tree res;
7741
7742 if (!validate_arg (arg, REAL_TYPE))
7743 return NULL_TREE;
7744
7745 /* Calculate the result when the argument is a constant. */
7746 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7747 return res;
7748
7749 /* Optimize tan(atan(x)) = x. */
7750 fcode = builtin_mathfn_code (arg);
7751 if (flag_unsafe_math_optimizations
7752 && (fcode == BUILT_IN_ATAN
7753 || fcode == BUILT_IN_ATANF
7754 || fcode == BUILT_IN_ATANL))
7755 return CALL_EXPR_ARG (arg, 0);
7756
7757 return NULL_TREE;
7758 }
7759
7760 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7761 NULL_TREE if no simplification can be made. */
7762
7763 static tree
7764 fold_builtin_sincos (location_t loc,
7765 tree arg0, tree arg1, tree arg2)
7766 {
7767 tree type;
7768 tree res, fn, call;
7769
7770 if (!validate_arg (arg0, REAL_TYPE)
7771 || !validate_arg (arg1, POINTER_TYPE)
7772 || !validate_arg (arg2, POINTER_TYPE))
7773 return NULL_TREE;
7774
7775 type = TREE_TYPE (arg0);
7776
7777 /* Calculate the result when the argument is a constant. */
7778 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7779 return res;
7780
7781 /* Canonicalize sincos to cexpi. */
7782 if (!targetm.libc_has_function (function_c99_math_complex))
7783 return NULL_TREE;
7784 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7785 if (!fn)
7786 return NULL_TREE;
7787
7788 call = build_call_expr_loc (loc, fn, 1, arg0);
7789 call = builtin_save_expr (call);
7790
7791 return build2 (COMPOUND_EXPR, void_type_node,
7792 build2 (MODIFY_EXPR, void_type_node,
7793 build_fold_indirect_ref_loc (loc, arg1),
7794 build1 (IMAGPART_EXPR, type, call)),
7795 build2 (MODIFY_EXPR, void_type_node,
7796 build_fold_indirect_ref_loc (loc, arg2),
7797 build1 (REALPART_EXPR, type, call)));
7798 }
7799
7800 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7801 NULL_TREE if no simplification can be made. */
7802
7803 static tree
7804 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7805 {
7806 tree rtype;
7807 tree realp, imagp, ifn;
7808 tree res;
7809
7810 if (!validate_arg (arg0, COMPLEX_TYPE)
7811 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7812 return NULL_TREE;
7813
7814 /* Calculate the result when the argument is a constant. */
7815 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7816 return res;
7817
7818 rtype = TREE_TYPE (TREE_TYPE (arg0));
7819
7820 /* In case we can figure out the real part of arg0 and it is constant zero
7821 fold to cexpi. */
7822 if (!targetm.libc_has_function (function_c99_math_complex))
7823 return NULL_TREE;
7824 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7825 if (!ifn)
7826 return NULL_TREE;
7827
7828 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7829 && real_zerop (realp))
7830 {
7831 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7832 return build_call_expr_loc (loc, ifn, 1, narg);
7833 }
7834
7835 /* In case we can easily decompose real and imaginary parts split cexp
7836 to exp (r) * cexpi (i). */
7837 if (flag_unsafe_math_optimizations
7838 && realp)
7839 {
7840 tree rfn, rcall, icall;
7841
7842 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7843 if (!rfn)
7844 return NULL_TREE;
7845
7846 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7847 if (!imagp)
7848 return NULL_TREE;
7849
7850 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7851 icall = builtin_save_expr (icall);
7852 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7853 rcall = builtin_save_expr (rcall);
7854 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7855 fold_build2_loc (loc, MULT_EXPR, rtype,
7856 rcall,
7857 fold_build1_loc (loc, REALPART_EXPR,
7858 rtype, icall)),
7859 fold_build2_loc (loc, MULT_EXPR, rtype,
7860 rcall,
7861 fold_build1_loc (loc, IMAGPART_EXPR,
7862 rtype, icall)));
7863 }
7864
7865 return NULL_TREE;
7866 }
7867
7868 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7869 Return NULL_TREE if no simplification can be made. */
7870
7871 static tree
7872 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7873 {
7874 if (!validate_arg (arg, REAL_TYPE))
7875 return NULL_TREE;
7876
7877 /* Optimize trunc of constant value. */
7878 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7879 {
7880 REAL_VALUE_TYPE r, x;
7881 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7882
7883 x = TREE_REAL_CST (arg);
7884 real_trunc (&r, TYPE_MODE (type), &x);
7885 return build_real (type, r);
7886 }
7887
7888 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7889 }
7890
7891 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7892 Return NULL_TREE if no simplification can be made. */
7893
7894 static tree
7895 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7896 {
7897 if (!validate_arg (arg, REAL_TYPE))
7898 return NULL_TREE;
7899
7900 /* Optimize floor of constant value. */
7901 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7902 {
7903 REAL_VALUE_TYPE x;
7904
7905 x = TREE_REAL_CST (arg);
7906 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7907 {
7908 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7909 REAL_VALUE_TYPE r;
7910
7911 real_floor (&r, TYPE_MODE (type), &x);
7912 return build_real (type, r);
7913 }
7914 }
7915
7916 /* Fold floor (x) where x is nonnegative to trunc (x). */
7917 if (tree_expr_nonnegative_p (arg))
7918 {
7919 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7920 if (truncfn)
7921 return build_call_expr_loc (loc, truncfn, 1, arg);
7922 }
7923
7924 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7925 }
7926
7927 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7928 Return NULL_TREE if no simplification can be made. */
7929
7930 static tree
7931 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7932 {
7933 if (!validate_arg (arg, REAL_TYPE))
7934 return NULL_TREE;
7935
7936 /* Optimize ceil of constant value. */
7937 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7938 {
7939 REAL_VALUE_TYPE x;
7940
7941 x = TREE_REAL_CST (arg);
7942 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7943 {
7944 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7945 REAL_VALUE_TYPE r;
7946
7947 real_ceil (&r, TYPE_MODE (type), &x);
7948 return build_real (type, r);
7949 }
7950 }
7951
7952 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7953 }
7954
7955 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7956 Return NULL_TREE if no simplification can be made. */
7957
7958 static tree
7959 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7960 {
7961 if (!validate_arg (arg, REAL_TYPE))
7962 return NULL_TREE;
7963
7964 /* Optimize round of constant value. */
7965 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7966 {
7967 REAL_VALUE_TYPE x;
7968
7969 x = TREE_REAL_CST (arg);
7970 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7971 {
7972 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7973 REAL_VALUE_TYPE r;
7974
7975 real_round (&r, TYPE_MODE (type), &x);
7976 return build_real (type, r);
7977 }
7978 }
7979
7980 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7981 }
7982
7983 /* Fold function call to builtin lround, lroundf or lroundl (or the
7984 corresponding long long versions) and other rounding functions. ARG
7985 is the argument to the call. Return NULL_TREE if no simplification
7986 can be made. */
7987
7988 static tree
7989 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7990 {
7991 if (!validate_arg (arg, REAL_TYPE))
7992 return NULL_TREE;
7993
7994 /* Optimize lround of constant value. */
7995 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7996 {
7997 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7998
7999 if (real_isfinite (&x))
8000 {
8001 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8002 tree ftype = TREE_TYPE (arg);
8003 double_int val;
8004 REAL_VALUE_TYPE r;
8005
8006 switch (DECL_FUNCTION_CODE (fndecl))
8007 {
8008 CASE_FLT_FN (BUILT_IN_IFLOOR):
8009 CASE_FLT_FN (BUILT_IN_LFLOOR):
8010 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8011 real_floor (&r, TYPE_MODE (ftype), &x);
8012 break;
8013
8014 CASE_FLT_FN (BUILT_IN_ICEIL):
8015 CASE_FLT_FN (BUILT_IN_LCEIL):
8016 CASE_FLT_FN (BUILT_IN_LLCEIL):
8017 real_ceil (&r, TYPE_MODE (ftype), &x);
8018 break;
8019
8020 CASE_FLT_FN (BUILT_IN_IROUND):
8021 CASE_FLT_FN (BUILT_IN_LROUND):
8022 CASE_FLT_FN (BUILT_IN_LLROUND):
8023 real_round (&r, TYPE_MODE (ftype), &x);
8024 break;
8025
8026 default:
8027 gcc_unreachable ();
8028 }
8029
8030 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8031 if (double_int_fits_to_tree_p (itype, val))
8032 return double_int_to_tree (itype, val);
8033 }
8034 }
8035
8036 switch (DECL_FUNCTION_CODE (fndecl))
8037 {
8038 CASE_FLT_FN (BUILT_IN_LFLOOR):
8039 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8040 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8041 if (tree_expr_nonnegative_p (arg))
8042 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8043 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8044 break;
8045 default:;
8046 }
8047
8048 return fold_fixed_mathfn (loc, fndecl, arg);
8049 }
8050
8051 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8052 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8053 the argument to the call. Return NULL_TREE if no simplification can
8054 be made. */
8055
8056 static tree
8057 fold_builtin_bitop (tree fndecl, tree arg)
8058 {
8059 if (!validate_arg (arg, INTEGER_TYPE))
8060 return NULL_TREE;
8061
8062 /* Optimize for constant argument. */
8063 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8064 {
8065 HOST_WIDE_INT hi, width, result;
8066 unsigned HOST_WIDE_INT lo;
8067 tree type;
8068
8069 type = TREE_TYPE (arg);
8070 width = TYPE_PRECISION (type);
8071 lo = TREE_INT_CST_LOW (arg);
8072
8073 /* Clear all the bits that are beyond the type's precision. */
8074 if (width > HOST_BITS_PER_WIDE_INT)
8075 {
8076 hi = TREE_INT_CST_HIGH (arg);
8077 if (width < HOST_BITS_PER_DOUBLE_INT)
8078 hi &= ~(HOST_WIDE_INT_M1U << (width - HOST_BITS_PER_WIDE_INT));
8079 }
8080 else
8081 {
8082 hi = 0;
8083 if (width < HOST_BITS_PER_WIDE_INT)
8084 lo &= ~(HOST_WIDE_INT_M1U << width);
8085 }
8086
8087 switch (DECL_FUNCTION_CODE (fndecl))
8088 {
8089 CASE_INT_FN (BUILT_IN_FFS):
8090 if (lo != 0)
8091 result = ffs_hwi (lo);
8092 else if (hi != 0)
8093 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8094 else
8095 result = 0;
8096 break;
8097
8098 CASE_INT_FN (BUILT_IN_CLZ):
8099 if (hi != 0)
8100 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8101 else if (lo != 0)
8102 result = width - floor_log2 (lo) - 1;
8103 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8104 result = width;
8105 break;
8106
8107 CASE_INT_FN (BUILT_IN_CTZ):
8108 if (lo != 0)
8109 result = ctz_hwi (lo);
8110 else if (hi != 0)
8111 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8112 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8113 result = width;
8114 break;
8115
8116 CASE_INT_FN (BUILT_IN_CLRSB):
8117 if (width > 2 * HOST_BITS_PER_WIDE_INT)
8118 return NULL_TREE;
8119 if (width > HOST_BITS_PER_WIDE_INT
8120 && (hi & ((unsigned HOST_WIDE_INT) 1
8121 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8122 {
8123 hi = ~hi & ~(HOST_WIDE_INT_M1U
8124 << (width - HOST_BITS_PER_WIDE_INT - 1));
8125 lo = ~lo;
8126 }
8127 else if (width <= HOST_BITS_PER_WIDE_INT
8128 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8129 lo = ~lo & ~(HOST_WIDE_INT_M1U << (width - 1));
8130 if (hi != 0)
8131 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8132 else if (lo != 0)
8133 result = width - floor_log2 (lo) - 2;
8134 else
8135 result = width - 1;
8136 break;
8137
8138 CASE_INT_FN (BUILT_IN_POPCOUNT):
8139 result = 0;
8140 while (lo)
8141 result++, lo &= lo - 1;
8142 while (hi)
8143 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8144 break;
8145
8146 CASE_INT_FN (BUILT_IN_PARITY):
8147 result = 0;
8148 while (lo)
8149 result++, lo &= lo - 1;
8150 while (hi)
8151 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8152 result &= 1;
8153 break;
8154
8155 default:
8156 gcc_unreachable ();
8157 }
8158
8159 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8160 }
8161
8162 return NULL_TREE;
8163 }
8164
8165 /* Fold function call to builtin_bswap and the short, long and long long
8166 variants. Return NULL_TREE if no simplification can be made. */
8167 static tree
8168 fold_builtin_bswap (tree fndecl, tree arg)
8169 {
8170 if (! validate_arg (arg, INTEGER_TYPE))
8171 return NULL_TREE;
8172
8173 /* Optimize constant value. */
8174 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8175 {
8176 HOST_WIDE_INT hi, width, r_hi = 0;
8177 unsigned HOST_WIDE_INT lo, r_lo = 0;
8178 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8179
8180 width = TYPE_PRECISION (type);
8181 lo = TREE_INT_CST_LOW (arg);
8182 hi = TREE_INT_CST_HIGH (arg);
8183
8184 switch (DECL_FUNCTION_CODE (fndecl))
8185 {
8186 case BUILT_IN_BSWAP16:
8187 case BUILT_IN_BSWAP32:
8188 case BUILT_IN_BSWAP64:
8189 {
8190 int s;
8191
8192 for (s = 0; s < width; s += 8)
8193 {
8194 int d = width - s - 8;
8195 unsigned HOST_WIDE_INT byte;
8196
8197 if (s < HOST_BITS_PER_WIDE_INT)
8198 byte = (lo >> s) & 0xff;
8199 else
8200 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8201
8202 if (d < HOST_BITS_PER_WIDE_INT)
8203 r_lo |= byte << d;
8204 else
8205 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8206 }
8207 }
8208
8209 break;
8210
8211 default:
8212 gcc_unreachable ();
8213 }
8214
8215 if (width < HOST_BITS_PER_WIDE_INT)
8216 return build_int_cst (type, r_lo);
8217 else
8218 return build_int_cst_wide (type, r_lo, r_hi);
8219 }
8220
8221 return NULL_TREE;
8222 }
8223
8224 /* A subroutine of fold_builtin to fold the various logarithmic
8225 functions. Return NULL_TREE if no simplification can me made.
8226 FUNC is the corresponding MPFR logarithm function. */
8227
8228 static tree
8229 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8230 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8231 {
8232 if (validate_arg (arg, REAL_TYPE))
8233 {
8234 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8235 tree res;
8236 const enum built_in_function fcode = builtin_mathfn_code (arg);
8237
8238 /* Calculate the result when the argument is a constant. */
8239 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8240 return res;
8241
8242 /* Special case, optimize logN(expN(x)) = x. */
8243 if (flag_unsafe_math_optimizations
8244 && ((func == mpfr_log
8245 && (fcode == BUILT_IN_EXP
8246 || fcode == BUILT_IN_EXPF
8247 || fcode == BUILT_IN_EXPL))
8248 || (func == mpfr_log2
8249 && (fcode == BUILT_IN_EXP2
8250 || fcode == BUILT_IN_EXP2F
8251 || fcode == BUILT_IN_EXP2L))
8252 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8253 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8254
8255 /* Optimize logN(func()) for various exponential functions. We
8256 want to determine the value "x" and the power "exponent" in
8257 order to transform logN(x**exponent) into exponent*logN(x). */
8258 if (flag_unsafe_math_optimizations)
8259 {
8260 tree exponent = 0, x = 0;
8261
8262 switch (fcode)
8263 {
8264 CASE_FLT_FN (BUILT_IN_EXP):
8265 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8266 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8267 dconst_e ()));
8268 exponent = CALL_EXPR_ARG (arg, 0);
8269 break;
8270 CASE_FLT_FN (BUILT_IN_EXP2):
8271 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8272 x = build_real (type, dconst2);
8273 exponent = CALL_EXPR_ARG (arg, 0);
8274 break;
8275 CASE_FLT_FN (BUILT_IN_EXP10):
8276 CASE_FLT_FN (BUILT_IN_POW10):
8277 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8278 {
8279 REAL_VALUE_TYPE dconst10;
8280 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8281 x = build_real (type, dconst10);
8282 }
8283 exponent = CALL_EXPR_ARG (arg, 0);
8284 break;
8285 CASE_FLT_FN (BUILT_IN_SQRT):
8286 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8287 x = CALL_EXPR_ARG (arg, 0);
8288 exponent = build_real (type, dconsthalf);
8289 break;
8290 CASE_FLT_FN (BUILT_IN_CBRT):
8291 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8292 x = CALL_EXPR_ARG (arg, 0);
8293 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8294 dconst_third ()));
8295 break;
8296 CASE_FLT_FN (BUILT_IN_POW):
8297 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8298 x = CALL_EXPR_ARG (arg, 0);
8299 exponent = CALL_EXPR_ARG (arg, 1);
8300 break;
8301 default:
8302 break;
8303 }
8304
8305 /* Now perform the optimization. */
8306 if (x && exponent)
8307 {
8308 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8309 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8310 }
8311 }
8312 }
8313
8314 return NULL_TREE;
8315 }
8316
8317 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8318 NULL_TREE if no simplification can be made. */
8319
8320 static tree
8321 fold_builtin_hypot (location_t loc, tree fndecl,
8322 tree arg0, tree arg1, tree type)
8323 {
8324 tree res, narg0, narg1;
8325
8326 if (!validate_arg (arg0, REAL_TYPE)
8327 || !validate_arg (arg1, REAL_TYPE))
8328 return NULL_TREE;
8329
8330 /* Calculate the result when the argument is a constant. */
8331 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8332 return res;
8333
8334 /* If either argument to hypot has a negate or abs, strip that off.
8335 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8336 narg0 = fold_strip_sign_ops (arg0);
8337 narg1 = fold_strip_sign_ops (arg1);
8338 if (narg0 || narg1)
8339 {
8340 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8341 narg1 ? narg1 : arg1);
8342 }
8343
8344 /* If either argument is zero, hypot is fabs of the other. */
8345 if (real_zerop (arg0))
8346 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8347 else if (real_zerop (arg1))
8348 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8349
8350 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8351 if (flag_unsafe_math_optimizations
8352 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8353 {
8354 const REAL_VALUE_TYPE sqrt2_trunc
8355 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8356 return fold_build2_loc (loc, MULT_EXPR, type,
8357 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8358 build_real (type, sqrt2_trunc));
8359 }
8360
8361 return NULL_TREE;
8362 }
8363
8364
8365 /* Fold a builtin function call to pow, powf, or powl. Return
8366 NULL_TREE if no simplification can be made. */
8367 static tree
8368 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8369 {
8370 tree res;
8371
8372 if (!validate_arg (arg0, REAL_TYPE)
8373 || !validate_arg (arg1, REAL_TYPE))
8374 return NULL_TREE;
8375
8376 /* Calculate the result when the argument is a constant. */
8377 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8378 return res;
8379
8380 /* Optimize pow(1.0,y) = 1.0. */
8381 if (real_onep (arg0))
8382 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8383
8384 if (TREE_CODE (arg1) == REAL_CST
8385 && !TREE_OVERFLOW (arg1))
8386 {
8387 REAL_VALUE_TYPE cint;
8388 REAL_VALUE_TYPE c;
8389 HOST_WIDE_INT n;
8390
8391 c = TREE_REAL_CST (arg1);
8392
8393 /* Optimize pow(x,0.0) = 1.0. */
8394 if (REAL_VALUES_EQUAL (c, dconst0))
8395 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8396 arg0);
8397
8398 /* Optimize pow(x,1.0) = x. */
8399 if (REAL_VALUES_EQUAL (c, dconst1))
8400 return arg0;
8401
8402 /* Optimize pow(x,-1.0) = 1.0/x. */
8403 if (REAL_VALUES_EQUAL (c, dconstm1))
8404 return fold_build2_loc (loc, RDIV_EXPR, type,
8405 build_real (type, dconst1), arg0);
8406
8407 /* Optimize pow(x,0.5) = sqrt(x). */
8408 if (flag_unsafe_math_optimizations
8409 && REAL_VALUES_EQUAL (c, dconsthalf))
8410 {
8411 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8412
8413 if (sqrtfn != NULL_TREE)
8414 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8415 }
8416
8417 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8418 if (flag_unsafe_math_optimizations)
8419 {
8420 const REAL_VALUE_TYPE dconstroot
8421 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8422
8423 if (REAL_VALUES_EQUAL (c, dconstroot))
8424 {
8425 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8426 if (cbrtfn != NULL_TREE)
8427 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8428 }
8429 }
8430
8431 /* Check for an integer exponent. */
8432 n = real_to_integer (&c);
8433 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8434 if (real_identical (&c, &cint))
8435 {
8436 /* Attempt to evaluate pow at compile-time, unless this should
8437 raise an exception. */
8438 if (TREE_CODE (arg0) == REAL_CST
8439 && !TREE_OVERFLOW (arg0)
8440 && (n > 0
8441 || (!flag_trapping_math && !flag_errno_math)
8442 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8443 {
8444 REAL_VALUE_TYPE x;
8445 bool inexact;
8446
8447 x = TREE_REAL_CST (arg0);
8448 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8449 if (flag_unsafe_math_optimizations || !inexact)
8450 return build_real (type, x);
8451 }
8452
8453 /* Strip sign ops from even integer powers. */
8454 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8455 {
8456 tree narg0 = fold_strip_sign_ops (arg0);
8457 if (narg0)
8458 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8459 }
8460 }
8461 }
8462
8463 if (flag_unsafe_math_optimizations)
8464 {
8465 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8466
8467 /* Optimize pow(expN(x),y) = expN(x*y). */
8468 if (BUILTIN_EXPONENT_P (fcode))
8469 {
8470 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8471 tree arg = CALL_EXPR_ARG (arg0, 0);
8472 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8473 return build_call_expr_loc (loc, expfn, 1, arg);
8474 }
8475
8476 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8477 if (BUILTIN_SQRT_P (fcode))
8478 {
8479 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8480 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8481 build_real (type, dconsthalf));
8482 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8483 }
8484
8485 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8486 if (BUILTIN_CBRT_P (fcode))
8487 {
8488 tree arg = CALL_EXPR_ARG (arg0, 0);
8489 if (tree_expr_nonnegative_p (arg))
8490 {
8491 const REAL_VALUE_TYPE dconstroot
8492 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8493 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8494 build_real (type, dconstroot));
8495 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8496 }
8497 }
8498
8499 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8500 if (fcode == BUILT_IN_POW
8501 || fcode == BUILT_IN_POWF
8502 || fcode == BUILT_IN_POWL)
8503 {
8504 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8505 if (tree_expr_nonnegative_p (arg00))
8506 {
8507 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8508 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8509 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8510 }
8511 }
8512 }
8513
8514 return NULL_TREE;
8515 }
8516
8517 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8518 Return NULL_TREE if no simplification can be made. */
8519 static tree
8520 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8521 tree arg0, tree arg1, tree type)
8522 {
8523 if (!validate_arg (arg0, REAL_TYPE)
8524 || !validate_arg (arg1, INTEGER_TYPE))
8525 return NULL_TREE;
8526
8527 /* Optimize pow(1.0,y) = 1.0. */
8528 if (real_onep (arg0))
8529 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8530
8531 if (tree_fits_shwi_p (arg1))
8532 {
8533 HOST_WIDE_INT c = tree_to_shwi (arg1);
8534
8535 /* Evaluate powi at compile-time. */
8536 if (TREE_CODE (arg0) == REAL_CST
8537 && !TREE_OVERFLOW (arg0))
8538 {
8539 REAL_VALUE_TYPE x;
8540 x = TREE_REAL_CST (arg0);
8541 real_powi (&x, TYPE_MODE (type), &x, c);
8542 return build_real (type, x);
8543 }
8544
8545 /* Optimize pow(x,0) = 1.0. */
8546 if (c == 0)
8547 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8548 arg0);
8549
8550 /* Optimize pow(x,1) = x. */
8551 if (c == 1)
8552 return arg0;
8553
8554 /* Optimize pow(x,-1) = 1.0/x. */
8555 if (c == -1)
8556 return fold_build2_loc (loc, RDIV_EXPR, type,
8557 build_real (type, dconst1), arg0);
8558 }
8559
8560 return NULL_TREE;
8561 }
8562
8563 /* A subroutine of fold_builtin to fold the various exponent
8564 functions. Return NULL_TREE if no simplification can be made.
8565 FUNC is the corresponding MPFR exponent function. */
8566
8567 static tree
8568 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8569 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8570 {
8571 if (validate_arg (arg, REAL_TYPE))
8572 {
8573 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8574 tree res;
8575
8576 /* Calculate the result when the argument is a constant. */
8577 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8578 return res;
8579
8580 /* Optimize expN(logN(x)) = x. */
8581 if (flag_unsafe_math_optimizations)
8582 {
8583 const enum built_in_function fcode = builtin_mathfn_code (arg);
8584
8585 if ((func == mpfr_exp
8586 && (fcode == BUILT_IN_LOG
8587 || fcode == BUILT_IN_LOGF
8588 || fcode == BUILT_IN_LOGL))
8589 || (func == mpfr_exp2
8590 && (fcode == BUILT_IN_LOG2
8591 || fcode == BUILT_IN_LOG2F
8592 || fcode == BUILT_IN_LOG2L))
8593 || (func == mpfr_exp10
8594 && (fcode == BUILT_IN_LOG10
8595 || fcode == BUILT_IN_LOG10F
8596 || fcode == BUILT_IN_LOG10L)))
8597 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8598 }
8599 }
8600
8601 return NULL_TREE;
8602 }
8603
8604 /* Return true if VAR is a VAR_DECL or a component thereof. */
8605
8606 static bool
8607 var_decl_component_p (tree var)
8608 {
8609 tree inner = var;
8610 while (handled_component_p (inner))
8611 inner = TREE_OPERAND (inner, 0);
8612 return SSA_VAR_P (inner);
8613 }
8614
8615 /* Fold function call to builtin memset. Return
8616 NULL_TREE if no simplification can be made. */
8617
8618 static tree
8619 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8620 tree type, bool ignore)
8621 {
8622 tree var, ret, etype;
8623 unsigned HOST_WIDE_INT length, cval;
8624
8625 if (! validate_arg (dest, POINTER_TYPE)
8626 || ! validate_arg (c, INTEGER_TYPE)
8627 || ! validate_arg (len, INTEGER_TYPE))
8628 return NULL_TREE;
8629
8630 if (! tree_fits_uhwi_p (len))
8631 return NULL_TREE;
8632
8633 /* If the LEN parameter is zero, return DEST. */
8634 if (integer_zerop (len))
8635 return omit_one_operand_loc (loc, type, dest, c);
8636
8637 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8638 return NULL_TREE;
8639
8640 var = dest;
8641 STRIP_NOPS (var);
8642 if (TREE_CODE (var) != ADDR_EXPR)
8643 return NULL_TREE;
8644
8645 var = TREE_OPERAND (var, 0);
8646 if (TREE_THIS_VOLATILE (var))
8647 return NULL_TREE;
8648
8649 etype = TREE_TYPE (var);
8650 if (TREE_CODE (etype) == ARRAY_TYPE)
8651 etype = TREE_TYPE (etype);
8652
8653 if (!INTEGRAL_TYPE_P (etype)
8654 && !POINTER_TYPE_P (etype))
8655 return NULL_TREE;
8656
8657 if (! var_decl_component_p (var))
8658 return NULL_TREE;
8659
8660 length = tree_to_uhwi (len);
8661 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8662 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8663 return NULL_TREE;
8664
8665 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8666 return NULL_TREE;
8667
8668 if (integer_zerop (c))
8669 cval = 0;
8670 else
8671 {
8672 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8673 return NULL_TREE;
8674
8675 cval = TREE_INT_CST_LOW (c);
8676 cval &= 0xff;
8677 cval |= cval << 8;
8678 cval |= cval << 16;
8679 cval |= (cval << 31) << 1;
8680 }
8681
8682 ret = build_int_cst_type (etype, cval);
8683 var = build_fold_indirect_ref_loc (loc,
8684 fold_convert_loc (loc,
8685 build_pointer_type (etype),
8686 dest));
8687 ret = build2 (MODIFY_EXPR, etype, var, ret);
8688 if (ignore)
8689 return ret;
8690
8691 return omit_one_operand_loc (loc, type, dest, ret);
8692 }
8693
8694 /* Fold function call to builtin memset. Return
8695 NULL_TREE if no simplification can be made. */
8696
8697 static tree
8698 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8699 {
8700 if (! validate_arg (dest, POINTER_TYPE)
8701 || ! validate_arg (size, INTEGER_TYPE))
8702 return NULL_TREE;
8703
8704 if (!ignore)
8705 return NULL_TREE;
8706
8707 /* New argument list transforming bzero(ptr x, int y) to
8708 memset(ptr x, int 0, size_t y). This is done this way
8709 so that if it isn't expanded inline, we fallback to
8710 calling bzero instead of memset. */
8711
8712 return fold_builtin_memset (loc, dest, integer_zero_node,
8713 fold_convert_loc (loc, size_type_node, size),
8714 void_type_node, ignore);
8715 }
8716
8717 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8718 NULL_TREE if no simplification can be made.
8719 If ENDP is 0, return DEST (like memcpy).
8720 If ENDP is 1, return DEST+LEN (like mempcpy).
8721 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8722 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8723 (memmove). */
8724
8725 static tree
8726 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8727 tree len, tree type, bool ignore, int endp)
8728 {
8729 tree destvar, srcvar, expr;
8730
8731 if (! validate_arg (dest, POINTER_TYPE)
8732 || ! validate_arg (src, POINTER_TYPE)
8733 || ! validate_arg (len, INTEGER_TYPE))
8734 return NULL_TREE;
8735
8736 /* If the LEN parameter is zero, return DEST. */
8737 if (integer_zerop (len))
8738 return omit_one_operand_loc (loc, type, dest, src);
8739
8740 /* If SRC and DEST are the same (and not volatile), return
8741 DEST{,+LEN,+LEN-1}. */
8742 if (operand_equal_p (src, dest, 0))
8743 expr = len;
8744 else
8745 {
8746 tree srctype, desttype;
8747 unsigned int src_align, dest_align;
8748 tree off0;
8749
8750 if (endp == 3)
8751 {
8752 src_align = get_pointer_alignment (src);
8753 dest_align = get_pointer_alignment (dest);
8754
8755 /* Both DEST and SRC must be pointer types.
8756 ??? This is what old code did. Is the testing for pointer types
8757 really mandatory?
8758
8759 If either SRC is readonly or length is 1, we can use memcpy. */
8760 if (!dest_align || !src_align)
8761 return NULL_TREE;
8762 if (readonly_data_expr (src)
8763 || (tree_fits_uhwi_p (len)
8764 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8765 >= tree_to_uhwi (len))))
8766 {
8767 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8768 if (!fn)
8769 return NULL_TREE;
8770 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8771 }
8772
8773 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8774 if (TREE_CODE (src) == ADDR_EXPR
8775 && TREE_CODE (dest) == ADDR_EXPR)
8776 {
8777 tree src_base, dest_base, fn;
8778 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8779 HOST_WIDE_INT size = -1;
8780 HOST_WIDE_INT maxsize = -1;
8781
8782 srcvar = TREE_OPERAND (src, 0);
8783 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8784 &size, &maxsize);
8785 destvar = TREE_OPERAND (dest, 0);
8786 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8787 &size, &maxsize);
8788 if (tree_fits_uhwi_p (len))
8789 maxsize = tree_to_uhwi (len);
8790 else
8791 maxsize = -1;
8792 src_offset /= BITS_PER_UNIT;
8793 dest_offset /= BITS_PER_UNIT;
8794 if (SSA_VAR_P (src_base)
8795 && SSA_VAR_P (dest_base))
8796 {
8797 if (operand_equal_p (src_base, dest_base, 0)
8798 && ranges_overlap_p (src_offset, maxsize,
8799 dest_offset, maxsize))
8800 return NULL_TREE;
8801 }
8802 else if (TREE_CODE (src_base) == MEM_REF
8803 && TREE_CODE (dest_base) == MEM_REF)
8804 {
8805 double_int off;
8806 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8807 TREE_OPERAND (dest_base, 0), 0))
8808 return NULL_TREE;
8809 off = mem_ref_offset (src_base) +
8810 double_int::from_shwi (src_offset);
8811 if (!off.fits_shwi ())
8812 return NULL_TREE;
8813 src_offset = off.low;
8814 off = mem_ref_offset (dest_base) +
8815 double_int::from_shwi (dest_offset);
8816 if (!off.fits_shwi ())
8817 return NULL_TREE;
8818 dest_offset = off.low;
8819 if (ranges_overlap_p (src_offset, maxsize,
8820 dest_offset, maxsize))
8821 return NULL_TREE;
8822 }
8823 else
8824 return NULL_TREE;
8825
8826 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8827 if (!fn)
8828 return NULL_TREE;
8829 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8830 }
8831
8832 /* If the destination and source do not alias optimize into
8833 memcpy as well. */
8834 if ((is_gimple_min_invariant (dest)
8835 || TREE_CODE (dest) == SSA_NAME)
8836 && (is_gimple_min_invariant (src)
8837 || TREE_CODE (src) == SSA_NAME))
8838 {
8839 ao_ref destr, srcr;
8840 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8841 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8842 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8843 {
8844 tree fn;
8845 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8846 if (!fn)
8847 return NULL_TREE;
8848 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8849 }
8850 }
8851
8852 return NULL_TREE;
8853 }
8854
8855 if (!tree_fits_shwi_p (len))
8856 return NULL_TREE;
8857 /* FIXME:
8858 This logic lose for arguments like (type *)malloc (sizeof (type)),
8859 since we strip the casts of up to VOID return value from malloc.
8860 Perhaps we ought to inherit type from non-VOID argument here? */
8861 STRIP_NOPS (src);
8862 STRIP_NOPS (dest);
8863 if (!POINTER_TYPE_P (TREE_TYPE (src))
8864 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8865 return NULL_TREE;
8866 /* In the following try to find a type that is most natural to be
8867 used for the memcpy source and destination and that allows
8868 the most optimization when memcpy is turned into a plain assignment
8869 using that type. In theory we could always use a char[len] type
8870 but that only gains us that the destination and source possibly
8871 no longer will have their address taken. */
8872 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8873 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8874 {
8875 tree tem = TREE_OPERAND (src, 0);
8876 STRIP_NOPS (tem);
8877 if (tem != TREE_OPERAND (src, 0))
8878 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8879 }
8880 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8881 {
8882 tree tem = TREE_OPERAND (dest, 0);
8883 STRIP_NOPS (tem);
8884 if (tem != TREE_OPERAND (dest, 0))
8885 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8886 }
8887 srctype = TREE_TYPE (TREE_TYPE (src));
8888 if (TREE_CODE (srctype) == ARRAY_TYPE
8889 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8890 {
8891 srctype = TREE_TYPE (srctype);
8892 STRIP_NOPS (src);
8893 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8894 }
8895 desttype = TREE_TYPE (TREE_TYPE (dest));
8896 if (TREE_CODE (desttype) == ARRAY_TYPE
8897 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8898 {
8899 desttype = TREE_TYPE (desttype);
8900 STRIP_NOPS (dest);
8901 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8902 }
8903 if (TREE_ADDRESSABLE (srctype)
8904 || TREE_ADDRESSABLE (desttype))
8905 return NULL_TREE;
8906
8907 /* Make sure we are not copying using a floating-point mode or
8908 a type whose size possibly does not match its precision. */
8909 if (FLOAT_MODE_P (TYPE_MODE (desttype))
8910 || TREE_CODE (desttype) == BOOLEAN_TYPE
8911 || TREE_CODE (desttype) == ENUMERAL_TYPE)
8912 {
8913 /* A more suitable int_mode_for_mode would return a vector
8914 integer mode for a vector float mode or a integer complex
8915 mode for a float complex mode if there isn't a regular
8916 integer mode covering the mode of desttype. */
8917 enum machine_mode mode = int_mode_for_mode (TYPE_MODE (desttype));
8918 if (mode == BLKmode)
8919 desttype = NULL_TREE;
8920 else
8921 desttype = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode),
8922 1);
8923 }
8924 if (FLOAT_MODE_P (TYPE_MODE (srctype))
8925 || TREE_CODE (srctype) == BOOLEAN_TYPE
8926 || TREE_CODE (srctype) == ENUMERAL_TYPE)
8927 {
8928 enum machine_mode mode = int_mode_for_mode (TYPE_MODE (srctype));
8929 if (mode == BLKmode)
8930 srctype = NULL_TREE;
8931 else
8932 srctype = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode),
8933 1);
8934 }
8935 if (!srctype)
8936 srctype = desttype;
8937 if (!desttype)
8938 desttype = srctype;
8939 if (!srctype)
8940 return NULL_TREE;
8941
8942 src_align = get_pointer_alignment (src);
8943 dest_align = get_pointer_alignment (dest);
8944 if (dest_align < TYPE_ALIGN (desttype)
8945 || src_align < TYPE_ALIGN (srctype))
8946 return NULL_TREE;
8947
8948 if (!ignore)
8949 dest = builtin_save_expr (dest);
8950
8951 /* Build accesses at offset zero with a ref-all character type. */
8952 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8953 ptr_mode, true), 0);
8954
8955 destvar = dest;
8956 STRIP_NOPS (destvar);
8957 if (TREE_CODE (destvar) == ADDR_EXPR
8958 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8959 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8960 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8961 else
8962 destvar = NULL_TREE;
8963
8964 srcvar = src;
8965 STRIP_NOPS (srcvar);
8966 if (TREE_CODE (srcvar) == ADDR_EXPR
8967 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8968 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8969 {
8970 if (!destvar
8971 || src_align >= TYPE_ALIGN (desttype))
8972 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8973 srcvar, off0);
8974 else if (!STRICT_ALIGNMENT)
8975 {
8976 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8977 src_align);
8978 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8979 }
8980 else
8981 srcvar = NULL_TREE;
8982 }
8983 else
8984 srcvar = NULL_TREE;
8985
8986 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8987 return NULL_TREE;
8988
8989 if (srcvar == NULL_TREE)
8990 {
8991 STRIP_NOPS (src);
8992 if (src_align >= TYPE_ALIGN (desttype))
8993 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8994 else
8995 {
8996 if (STRICT_ALIGNMENT)
8997 return NULL_TREE;
8998 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8999 src_align);
9000 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
9001 }
9002 }
9003 else if (destvar == NULL_TREE)
9004 {
9005 STRIP_NOPS (dest);
9006 if (dest_align >= TYPE_ALIGN (srctype))
9007 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
9008 else
9009 {
9010 if (STRICT_ALIGNMENT)
9011 return NULL_TREE;
9012 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
9013 dest_align);
9014 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
9015 }
9016 }
9017
9018 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9019 }
9020
9021 if (ignore)
9022 return expr;
9023
9024 if (endp == 0 || endp == 3)
9025 return omit_one_operand_loc (loc, type, dest, expr);
9026
9027 if (expr == len)
9028 expr = NULL_TREE;
9029
9030 if (endp == 2)
9031 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9032 ssize_int (1));
9033
9034 dest = fold_build_pointer_plus_loc (loc, dest, len);
9035 dest = fold_convert_loc (loc, type, dest);
9036 if (expr)
9037 dest = omit_one_operand_loc (loc, type, dest, expr);
9038 return dest;
9039 }
9040
9041 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9042 If LEN is not NULL, it represents the length of the string to be
9043 copied. Return NULL_TREE if no simplification can be made. */
9044
9045 tree
9046 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9047 {
9048 tree fn;
9049
9050 if (!validate_arg (dest, POINTER_TYPE)
9051 || !validate_arg (src, POINTER_TYPE))
9052 return NULL_TREE;
9053
9054 /* If SRC and DEST are the same (and not volatile), return DEST. */
9055 if (operand_equal_p (src, dest, 0))
9056 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9057
9058 if (optimize_function_for_size_p (cfun))
9059 return NULL_TREE;
9060
9061 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9062 if (!fn)
9063 return NULL_TREE;
9064
9065 if (!len)
9066 {
9067 len = c_strlen (src, 1);
9068 if (! len || TREE_SIDE_EFFECTS (len))
9069 return NULL_TREE;
9070 }
9071
9072 len = fold_convert_loc (loc, size_type_node, len);
9073 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9074 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9075 build_call_expr_loc (loc, fn, 3, dest, src, len));
9076 }
9077
9078 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9079 Return NULL_TREE if no simplification can be made. */
9080
9081 static tree
9082 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9083 {
9084 tree fn, len, lenp1, call, type;
9085
9086 if (!validate_arg (dest, POINTER_TYPE)
9087 || !validate_arg (src, POINTER_TYPE))
9088 return NULL_TREE;
9089
9090 len = c_strlen (src, 1);
9091 if (!len
9092 || TREE_CODE (len) != INTEGER_CST)
9093 return NULL_TREE;
9094
9095 if (optimize_function_for_size_p (cfun)
9096 /* If length is zero it's small enough. */
9097 && !integer_zerop (len))
9098 return NULL_TREE;
9099
9100 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9101 if (!fn)
9102 return NULL_TREE;
9103
9104 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9105 fold_convert_loc (loc, size_type_node, len),
9106 build_int_cst (size_type_node, 1));
9107 /* We use dest twice in building our expression. Save it from
9108 multiple expansions. */
9109 dest = builtin_save_expr (dest);
9110 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9111
9112 type = TREE_TYPE (TREE_TYPE (fndecl));
9113 dest = fold_build_pointer_plus_loc (loc, dest, len);
9114 dest = fold_convert_loc (loc, type, dest);
9115 dest = omit_one_operand_loc (loc, type, dest, call);
9116 return dest;
9117 }
9118
9119 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9120 If SLEN is not NULL, it represents the length of the source string.
9121 Return NULL_TREE if no simplification can be made. */
9122
9123 tree
9124 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9125 tree src, tree len, tree slen)
9126 {
9127 tree fn;
9128
9129 if (!validate_arg (dest, POINTER_TYPE)
9130 || !validate_arg (src, POINTER_TYPE)
9131 || !validate_arg (len, INTEGER_TYPE))
9132 return NULL_TREE;
9133
9134 /* If the LEN parameter is zero, return DEST. */
9135 if (integer_zerop (len))
9136 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9137
9138 /* We can't compare slen with len as constants below if len is not a
9139 constant. */
9140 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9141 return NULL_TREE;
9142
9143 if (!slen)
9144 slen = c_strlen (src, 1);
9145
9146 /* Now, we must be passed a constant src ptr parameter. */
9147 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9148 return NULL_TREE;
9149
9150 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9151
9152 /* We do not support simplification of this case, though we do
9153 support it when expanding trees into RTL. */
9154 /* FIXME: generate a call to __builtin_memset. */
9155 if (tree_int_cst_lt (slen, len))
9156 return NULL_TREE;
9157
9158 /* OK transform into builtin memcpy. */
9159 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9160 if (!fn)
9161 return NULL_TREE;
9162
9163 len = fold_convert_loc (loc, size_type_node, len);
9164 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9165 build_call_expr_loc (loc, fn, 3, dest, src, len));
9166 }
9167
9168 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9169 arguments to the call, and TYPE is its return type.
9170 Return NULL_TREE if no simplification can be made. */
9171
9172 static tree
9173 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9174 {
9175 if (!validate_arg (arg1, POINTER_TYPE)
9176 || !validate_arg (arg2, INTEGER_TYPE)
9177 || !validate_arg (len, INTEGER_TYPE))
9178 return NULL_TREE;
9179 else
9180 {
9181 const char *p1;
9182
9183 if (TREE_CODE (arg2) != INTEGER_CST
9184 || !tree_fits_uhwi_p (len))
9185 return NULL_TREE;
9186
9187 p1 = c_getstr (arg1);
9188 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9189 {
9190 char c;
9191 const char *r;
9192 tree tem;
9193
9194 if (target_char_cast (arg2, &c))
9195 return NULL_TREE;
9196
9197 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9198
9199 if (r == NULL)
9200 return build_int_cst (TREE_TYPE (arg1), 0);
9201
9202 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9203 return fold_convert_loc (loc, type, tem);
9204 }
9205 return NULL_TREE;
9206 }
9207 }
9208
9209 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9210 Return NULL_TREE if no simplification can be made. */
9211
9212 static tree
9213 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9214 {
9215 const char *p1, *p2;
9216
9217 if (!validate_arg (arg1, POINTER_TYPE)
9218 || !validate_arg (arg2, POINTER_TYPE)
9219 || !validate_arg (len, INTEGER_TYPE))
9220 return NULL_TREE;
9221
9222 /* If the LEN parameter is zero, return zero. */
9223 if (integer_zerop (len))
9224 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9225 arg1, arg2);
9226
9227 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9228 if (operand_equal_p (arg1, arg2, 0))
9229 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9230
9231 p1 = c_getstr (arg1);
9232 p2 = c_getstr (arg2);
9233
9234 /* If all arguments are constant, and the value of len is not greater
9235 than the lengths of arg1 and arg2, evaluate at compile-time. */
9236 if (tree_fits_uhwi_p (len) && p1 && p2
9237 && compare_tree_int (len, strlen (p1) + 1) <= 0
9238 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9239 {
9240 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9241
9242 if (r > 0)
9243 return integer_one_node;
9244 else if (r < 0)
9245 return integer_minus_one_node;
9246 else
9247 return integer_zero_node;
9248 }
9249
9250 /* If len parameter is one, return an expression corresponding to
9251 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9252 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9253 {
9254 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9255 tree cst_uchar_ptr_node
9256 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9257
9258 tree ind1
9259 = fold_convert_loc (loc, integer_type_node,
9260 build1 (INDIRECT_REF, cst_uchar_node,
9261 fold_convert_loc (loc,
9262 cst_uchar_ptr_node,
9263 arg1)));
9264 tree ind2
9265 = fold_convert_loc (loc, integer_type_node,
9266 build1 (INDIRECT_REF, cst_uchar_node,
9267 fold_convert_loc (loc,
9268 cst_uchar_ptr_node,
9269 arg2)));
9270 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9271 }
9272
9273 return NULL_TREE;
9274 }
9275
9276 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9277 Return NULL_TREE if no simplification can be made. */
9278
9279 static tree
9280 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9281 {
9282 const char *p1, *p2;
9283
9284 if (!validate_arg (arg1, POINTER_TYPE)
9285 || !validate_arg (arg2, POINTER_TYPE))
9286 return NULL_TREE;
9287
9288 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9289 if (operand_equal_p (arg1, arg2, 0))
9290 return integer_zero_node;
9291
9292 p1 = c_getstr (arg1);
9293 p2 = c_getstr (arg2);
9294
9295 if (p1 && p2)
9296 {
9297 const int i = strcmp (p1, p2);
9298 if (i < 0)
9299 return integer_minus_one_node;
9300 else if (i > 0)
9301 return integer_one_node;
9302 else
9303 return integer_zero_node;
9304 }
9305
9306 /* If the second arg is "", return *(const unsigned char*)arg1. */
9307 if (p2 && *p2 == '\0')
9308 {
9309 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9310 tree cst_uchar_ptr_node
9311 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9312
9313 return fold_convert_loc (loc, integer_type_node,
9314 build1 (INDIRECT_REF, cst_uchar_node,
9315 fold_convert_loc (loc,
9316 cst_uchar_ptr_node,
9317 arg1)));
9318 }
9319
9320 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9321 if (p1 && *p1 == '\0')
9322 {
9323 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9324 tree cst_uchar_ptr_node
9325 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9326
9327 tree temp
9328 = fold_convert_loc (loc, integer_type_node,
9329 build1 (INDIRECT_REF, cst_uchar_node,
9330 fold_convert_loc (loc,
9331 cst_uchar_ptr_node,
9332 arg2)));
9333 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9334 }
9335
9336 return NULL_TREE;
9337 }
9338
9339 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9340 Return NULL_TREE if no simplification can be made. */
9341
9342 static tree
9343 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9344 {
9345 const char *p1, *p2;
9346
9347 if (!validate_arg (arg1, POINTER_TYPE)
9348 || !validate_arg (arg2, POINTER_TYPE)
9349 || !validate_arg (len, INTEGER_TYPE))
9350 return NULL_TREE;
9351
9352 /* If the LEN parameter is zero, return zero. */
9353 if (integer_zerop (len))
9354 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9355 arg1, arg2);
9356
9357 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9358 if (operand_equal_p (arg1, arg2, 0))
9359 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9360
9361 p1 = c_getstr (arg1);
9362 p2 = c_getstr (arg2);
9363
9364 if (tree_fits_uhwi_p (len) && p1 && p2)
9365 {
9366 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9367 if (i > 0)
9368 return integer_one_node;
9369 else if (i < 0)
9370 return integer_minus_one_node;
9371 else
9372 return integer_zero_node;
9373 }
9374
9375 /* If the second arg is "", and the length is greater than zero,
9376 return *(const unsigned char*)arg1. */
9377 if (p2 && *p2 == '\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 return 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 arg1)));
9390 }
9391
9392 /* If the first arg is "", and the length is greater than zero,
9393 return -*(const unsigned char*)arg2. */
9394 if (p1 && *p1 == '\0'
9395 && TREE_CODE (len) == INTEGER_CST
9396 && tree_int_cst_sgn (len) == 1)
9397 {
9398 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9399 tree cst_uchar_ptr_node
9400 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9401
9402 tree temp = fold_convert_loc (loc, integer_type_node,
9403 build1 (INDIRECT_REF, cst_uchar_node,
9404 fold_convert_loc (loc,
9405 cst_uchar_ptr_node,
9406 arg2)));
9407 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9408 }
9409
9410 /* If len parameter is one, return an expression corresponding to
9411 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9412 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9413 {
9414 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9415 tree cst_uchar_ptr_node
9416 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9417
9418 tree ind1 = fold_convert_loc (loc, integer_type_node,
9419 build1 (INDIRECT_REF, cst_uchar_node,
9420 fold_convert_loc (loc,
9421 cst_uchar_ptr_node,
9422 arg1)));
9423 tree ind2 = fold_convert_loc (loc, integer_type_node,
9424 build1 (INDIRECT_REF, cst_uchar_node,
9425 fold_convert_loc (loc,
9426 cst_uchar_ptr_node,
9427 arg2)));
9428 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9429 }
9430
9431 return NULL_TREE;
9432 }
9433
9434 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9435 ARG. Return NULL_TREE if no simplification can be made. */
9436
9437 static tree
9438 fold_builtin_signbit (location_t loc, tree arg, tree type)
9439 {
9440 if (!validate_arg (arg, REAL_TYPE))
9441 return NULL_TREE;
9442
9443 /* If ARG is a compile-time constant, determine the result. */
9444 if (TREE_CODE (arg) == REAL_CST
9445 && !TREE_OVERFLOW (arg))
9446 {
9447 REAL_VALUE_TYPE c;
9448
9449 c = TREE_REAL_CST (arg);
9450 return (REAL_VALUE_NEGATIVE (c)
9451 ? build_one_cst (type)
9452 : build_zero_cst (type));
9453 }
9454
9455 /* If ARG is non-negative, the result is always zero. */
9456 if (tree_expr_nonnegative_p (arg))
9457 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9458
9459 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9460 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9461 return fold_convert (type,
9462 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9463 build_real (TREE_TYPE (arg), dconst0)));
9464
9465 return NULL_TREE;
9466 }
9467
9468 /* Fold function call to builtin copysign, copysignf or copysignl with
9469 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9470 be made. */
9471
9472 static tree
9473 fold_builtin_copysign (location_t loc, tree fndecl,
9474 tree arg1, tree arg2, tree type)
9475 {
9476 tree tem;
9477
9478 if (!validate_arg (arg1, REAL_TYPE)
9479 || !validate_arg (arg2, REAL_TYPE))
9480 return NULL_TREE;
9481
9482 /* copysign(X,X) is X. */
9483 if (operand_equal_p (arg1, arg2, 0))
9484 return fold_convert_loc (loc, type, arg1);
9485
9486 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9487 if (TREE_CODE (arg1) == REAL_CST
9488 && TREE_CODE (arg2) == REAL_CST
9489 && !TREE_OVERFLOW (arg1)
9490 && !TREE_OVERFLOW (arg2))
9491 {
9492 REAL_VALUE_TYPE c1, c2;
9493
9494 c1 = TREE_REAL_CST (arg1);
9495 c2 = TREE_REAL_CST (arg2);
9496 /* c1.sign := c2.sign. */
9497 real_copysign (&c1, &c2);
9498 return build_real (type, c1);
9499 }
9500
9501 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9502 Remember to evaluate Y for side-effects. */
9503 if (tree_expr_nonnegative_p (arg2))
9504 return omit_one_operand_loc (loc, type,
9505 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9506 arg2);
9507
9508 /* Strip sign changing operations for the first argument. */
9509 tem = fold_strip_sign_ops (arg1);
9510 if (tem)
9511 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9512
9513 return NULL_TREE;
9514 }
9515
9516 /* Fold a call to builtin isascii with argument ARG. */
9517
9518 static tree
9519 fold_builtin_isascii (location_t loc, tree arg)
9520 {
9521 if (!validate_arg (arg, INTEGER_TYPE))
9522 return NULL_TREE;
9523 else
9524 {
9525 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9526 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9527 build_int_cst (integer_type_node,
9528 ~ (unsigned HOST_WIDE_INT) 0x7f));
9529 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9530 arg, integer_zero_node);
9531 }
9532 }
9533
9534 /* Fold a call to builtin toascii with argument ARG. */
9535
9536 static tree
9537 fold_builtin_toascii (location_t loc, tree arg)
9538 {
9539 if (!validate_arg (arg, INTEGER_TYPE))
9540 return NULL_TREE;
9541
9542 /* Transform toascii(c) -> (c & 0x7f). */
9543 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9544 build_int_cst (integer_type_node, 0x7f));
9545 }
9546
9547 /* Fold a call to builtin isdigit with argument ARG. */
9548
9549 static tree
9550 fold_builtin_isdigit (location_t loc, tree arg)
9551 {
9552 if (!validate_arg (arg, INTEGER_TYPE))
9553 return NULL_TREE;
9554 else
9555 {
9556 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9557 /* According to the C standard, isdigit is unaffected by locale.
9558 However, it definitely is affected by the target character set. */
9559 unsigned HOST_WIDE_INT target_digit0
9560 = lang_hooks.to_target_charset ('0');
9561
9562 if (target_digit0 == 0)
9563 return NULL_TREE;
9564
9565 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9566 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9567 build_int_cst (unsigned_type_node, target_digit0));
9568 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9569 build_int_cst (unsigned_type_node, 9));
9570 }
9571 }
9572
9573 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9574
9575 static tree
9576 fold_builtin_fabs (location_t loc, tree arg, tree type)
9577 {
9578 if (!validate_arg (arg, REAL_TYPE))
9579 return NULL_TREE;
9580
9581 arg = fold_convert_loc (loc, type, arg);
9582 if (TREE_CODE (arg) == REAL_CST)
9583 return fold_abs_const (arg, type);
9584 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9585 }
9586
9587 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9588
9589 static tree
9590 fold_builtin_abs (location_t loc, tree arg, tree type)
9591 {
9592 if (!validate_arg (arg, INTEGER_TYPE))
9593 return NULL_TREE;
9594
9595 arg = fold_convert_loc (loc, type, arg);
9596 if (TREE_CODE (arg) == INTEGER_CST)
9597 return fold_abs_const (arg, type);
9598 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9599 }
9600
9601 /* Fold a fma operation with arguments ARG[012]. */
9602
9603 tree
9604 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9605 tree type, tree arg0, tree arg1, tree arg2)
9606 {
9607 if (TREE_CODE (arg0) == REAL_CST
9608 && TREE_CODE (arg1) == REAL_CST
9609 && TREE_CODE (arg2) == REAL_CST)
9610 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9611
9612 return NULL_TREE;
9613 }
9614
9615 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9616
9617 static tree
9618 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9619 {
9620 if (validate_arg (arg0, REAL_TYPE)
9621 && validate_arg (arg1, REAL_TYPE)
9622 && validate_arg (arg2, REAL_TYPE))
9623 {
9624 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9625 if (tem)
9626 return tem;
9627
9628 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9629 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9630 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9631 }
9632 return NULL_TREE;
9633 }
9634
9635 /* Fold a call to builtin fmin or fmax. */
9636
9637 static tree
9638 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9639 tree type, bool max)
9640 {
9641 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9642 {
9643 /* Calculate the result when the argument is a constant. */
9644 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9645
9646 if (res)
9647 return res;
9648
9649 /* If either argument is NaN, return the other one. Avoid the
9650 transformation if we get (and honor) a signalling NaN. Using
9651 omit_one_operand() ensures we create a non-lvalue. */
9652 if (TREE_CODE (arg0) == REAL_CST
9653 && real_isnan (&TREE_REAL_CST (arg0))
9654 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9655 || ! TREE_REAL_CST (arg0).signalling))
9656 return omit_one_operand_loc (loc, type, arg1, arg0);
9657 if (TREE_CODE (arg1) == REAL_CST
9658 && real_isnan (&TREE_REAL_CST (arg1))
9659 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9660 || ! TREE_REAL_CST (arg1).signalling))
9661 return omit_one_operand_loc (loc, type, arg0, arg1);
9662
9663 /* Transform fmin/fmax(x,x) -> x. */
9664 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9665 return omit_one_operand_loc (loc, type, arg0, arg1);
9666
9667 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9668 functions to return the numeric arg if the other one is NaN.
9669 These tree codes don't honor that, so only transform if
9670 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9671 handled, so we don't have to worry about it either. */
9672 if (flag_finite_math_only)
9673 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9674 fold_convert_loc (loc, type, arg0),
9675 fold_convert_loc (loc, type, arg1));
9676 }
9677 return NULL_TREE;
9678 }
9679
9680 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9681
9682 static tree
9683 fold_builtin_carg (location_t loc, tree arg, tree type)
9684 {
9685 if (validate_arg (arg, COMPLEX_TYPE)
9686 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9687 {
9688 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9689
9690 if (atan2_fn)
9691 {
9692 tree new_arg = builtin_save_expr (arg);
9693 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9694 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9695 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9696 }
9697 }
9698
9699 return NULL_TREE;
9700 }
9701
9702 /* Fold a call to builtin logb/ilogb. */
9703
9704 static tree
9705 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9706 {
9707 if (! validate_arg (arg, REAL_TYPE))
9708 return NULL_TREE;
9709
9710 STRIP_NOPS (arg);
9711
9712 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9713 {
9714 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9715
9716 switch (value->cl)
9717 {
9718 case rvc_nan:
9719 case rvc_inf:
9720 /* If arg is Inf or NaN and we're logb, return it. */
9721 if (TREE_CODE (rettype) == REAL_TYPE)
9722 {
9723 /* For logb(-Inf) we have to return +Inf. */
9724 if (real_isinf (value) && real_isneg (value))
9725 {
9726 REAL_VALUE_TYPE tem;
9727 real_inf (&tem);
9728 return build_real (rettype, tem);
9729 }
9730 return fold_convert_loc (loc, rettype, arg);
9731 }
9732 /* Fall through... */
9733 case rvc_zero:
9734 /* Zero may set errno and/or raise an exception for logb, also
9735 for ilogb we don't know FP_ILOGB0. */
9736 return NULL_TREE;
9737 case rvc_normal:
9738 /* For normal numbers, proceed iff radix == 2. In GCC,
9739 normalized significands are in the range [0.5, 1.0). We
9740 want the exponent as if they were [1.0, 2.0) so get the
9741 exponent and subtract 1. */
9742 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9743 return fold_convert_loc (loc, rettype,
9744 build_int_cst (integer_type_node,
9745 REAL_EXP (value)-1));
9746 break;
9747 }
9748 }
9749
9750 return NULL_TREE;
9751 }
9752
9753 /* Fold a call to builtin significand, if radix == 2. */
9754
9755 static tree
9756 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9757 {
9758 if (! validate_arg (arg, REAL_TYPE))
9759 return NULL_TREE;
9760
9761 STRIP_NOPS (arg);
9762
9763 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9764 {
9765 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9766
9767 switch (value->cl)
9768 {
9769 case rvc_zero:
9770 case rvc_nan:
9771 case rvc_inf:
9772 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9773 return fold_convert_loc (loc, rettype, arg);
9774 case rvc_normal:
9775 /* For normal numbers, proceed iff radix == 2. */
9776 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9777 {
9778 REAL_VALUE_TYPE result = *value;
9779 /* In GCC, normalized significands are in the range [0.5,
9780 1.0). We want them to be [1.0, 2.0) so set the
9781 exponent to 1. */
9782 SET_REAL_EXP (&result, 1);
9783 return build_real (rettype, result);
9784 }
9785 break;
9786 }
9787 }
9788
9789 return NULL_TREE;
9790 }
9791
9792 /* Fold a call to builtin frexp, we can assume the base is 2. */
9793
9794 static tree
9795 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9796 {
9797 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9798 return NULL_TREE;
9799
9800 STRIP_NOPS (arg0);
9801
9802 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9803 return NULL_TREE;
9804
9805 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9806
9807 /* Proceed if a valid pointer type was passed in. */
9808 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9809 {
9810 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9811 tree frac, exp;
9812
9813 switch (value->cl)
9814 {
9815 case rvc_zero:
9816 /* For +-0, return (*exp = 0, +-0). */
9817 exp = integer_zero_node;
9818 frac = arg0;
9819 break;
9820 case rvc_nan:
9821 case rvc_inf:
9822 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9823 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9824 case rvc_normal:
9825 {
9826 /* Since the frexp function always expects base 2, and in
9827 GCC normalized significands are already in the range
9828 [0.5, 1.0), we have exactly what frexp wants. */
9829 REAL_VALUE_TYPE frac_rvt = *value;
9830 SET_REAL_EXP (&frac_rvt, 0);
9831 frac = build_real (rettype, frac_rvt);
9832 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9833 }
9834 break;
9835 default:
9836 gcc_unreachable ();
9837 }
9838
9839 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9840 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9841 TREE_SIDE_EFFECTS (arg1) = 1;
9842 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9843 }
9844
9845 return NULL_TREE;
9846 }
9847
9848 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9849 then we can assume the base is two. If it's false, then we have to
9850 check the mode of the TYPE parameter in certain cases. */
9851
9852 static tree
9853 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9854 tree type, bool ldexp)
9855 {
9856 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9857 {
9858 STRIP_NOPS (arg0);
9859 STRIP_NOPS (arg1);
9860
9861 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9862 if (real_zerop (arg0) || integer_zerop (arg1)
9863 || (TREE_CODE (arg0) == REAL_CST
9864 && !real_isfinite (&TREE_REAL_CST (arg0))))
9865 return omit_one_operand_loc (loc, type, arg0, arg1);
9866
9867 /* If both arguments are constant, then try to evaluate it. */
9868 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9869 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9870 && tree_fits_shwi_p (arg1))
9871 {
9872 /* Bound the maximum adjustment to twice the range of the
9873 mode's valid exponents. Use abs to ensure the range is
9874 positive as a sanity check. */
9875 const long max_exp_adj = 2 *
9876 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9877 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9878
9879 /* Get the user-requested adjustment. */
9880 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9881
9882 /* The requested adjustment must be inside this range. This
9883 is a preliminary cap to avoid things like overflow, we
9884 may still fail to compute the result for other reasons. */
9885 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9886 {
9887 REAL_VALUE_TYPE initial_result;
9888
9889 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9890
9891 /* Ensure we didn't overflow. */
9892 if (! real_isinf (&initial_result))
9893 {
9894 const REAL_VALUE_TYPE trunc_result
9895 = real_value_truncate (TYPE_MODE (type), initial_result);
9896
9897 /* Only proceed if the target mode can hold the
9898 resulting value. */
9899 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9900 return build_real (type, trunc_result);
9901 }
9902 }
9903 }
9904 }
9905
9906 return NULL_TREE;
9907 }
9908
9909 /* Fold a call to builtin modf. */
9910
9911 static tree
9912 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9913 {
9914 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9915 return NULL_TREE;
9916
9917 STRIP_NOPS (arg0);
9918
9919 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9920 return NULL_TREE;
9921
9922 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9923
9924 /* Proceed if a valid pointer type was passed in. */
9925 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9926 {
9927 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9928 REAL_VALUE_TYPE trunc, frac;
9929
9930 switch (value->cl)
9931 {
9932 case rvc_nan:
9933 case rvc_zero:
9934 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9935 trunc = frac = *value;
9936 break;
9937 case rvc_inf:
9938 /* For +-Inf, return (*arg1 = arg0, +-0). */
9939 frac = dconst0;
9940 frac.sign = value->sign;
9941 trunc = *value;
9942 break;
9943 case rvc_normal:
9944 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9945 real_trunc (&trunc, VOIDmode, value);
9946 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9947 /* If the original number was negative and already
9948 integral, then the fractional part is -0.0. */
9949 if (value->sign && frac.cl == rvc_zero)
9950 frac.sign = value->sign;
9951 break;
9952 }
9953
9954 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9955 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9956 build_real (rettype, trunc));
9957 TREE_SIDE_EFFECTS (arg1) = 1;
9958 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9959 build_real (rettype, frac));
9960 }
9961
9962 return NULL_TREE;
9963 }
9964
9965 /* Given a location LOC, an interclass builtin function decl FNDECL
9966 and its single argument ARG, return an folded expression computing
9967 the same, or NULL_TREE if we either couldn't or didn't want to fold
9968 (the latter happen if there's an RTL instruction available). */
9969
9970 static tree
9971 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9972 {
9973 enum machine_mode mode;
9974
9975 if (!validate_arg (arg, REAL_TYPE))
9976 return NULL_TREE;
9977
9978 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9979 return NULL_TREE;
9980
9981 mode = TYPE_MODE (TREE_TYPE (arg));
9982
9983 /* If there is no optab, try generic code. */
9984 switch (DECL_FUNCTION_CODE (fndecl))
9985 {
9986 tree result;
9987
9988 CASE_FLT_FN (BUILT_IN_ISINF):
9989 {
9990 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9991 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9992 tree const type = TREE_TYPE (arg);
9993 REAL_VALUE_TYPE r;
9994 char buf[128];
9995
9996 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9997 real_from_string (&r, buf);
9998 result = build_call_expr (isgr_fn, 2,
9999 fold_build1_loc (loc, ABS_EXPR, type, arg),
10000 build_real (type, r));
10001 return result;
10002 }
10003 CASE_FLT_FN (BUILT_IN_FINITE):
10004 case BUILT_IN_ISFINITE:
10005 {
10006 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
10007 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10008 tree const type = TREE_TYPE (arg);
10009 REAL_VALUE_TYPE r;
10010 char buf[128];
10011
10012 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10013 real_from_string (&r, buf);
10014 result = build_call_expr (isle_fn, 2,
10015 fold_build1_loc (loc, ABS_EXPR, type, arg),
10016 build_real (type, r));
10017 /*result = fold_build2_loc (loc, UNGT_EXPR,
10018 TREE_TYPE (TREE_TYPE (fndecl)),
10019 fold_build1_loc (loc, ABS_EXPR, type, arg),
10020 build_real (type, r));
10021 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10022 TREE_TYPE (TREE_TYPE (fndecl)),
10023 result);*/
10024 return result;
10025 }
10026 case BUILT_IN_ISNORMAL:
10027 {
10028 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10029 islessequal(fabs(x),DBL_MAX). */
10030 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10031 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10032 tree const type = TREE_TYPE (arg);
10033 REAL_VALUE_TYPE rmax, rmin;
10034 char buf[128];
10035
10036 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10037 real_from_string (&rmax, buf);
10038 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10039 real_from_string (&rmin, buf);
10040 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10041 result = build_call_expr (isle_fn, 2, arg,
10042 build_real (type, rmax));
10043 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10044 build_call_expr (isge_fn, 2, arg,
10045 build_real (type, rmin)));
10046 return result;
10047 }
10048 default:
10049 break;
10050 }
10051
10052 return NULL_TREE;
10053 }
10054
10055 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10056 ARG is the argument for the call. */
10057
10058 static tree
10059 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10060 {
10061 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10062 REAL_VALUE_TYPE r;
10063
10064 if (!validate_arg (arg, REAL_TYPE))
10065 return NULL_TREE;
10066
10067 switch (builtin_index)
10068 {
10069 case BUILT_IN_ISINF:
10070 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10071 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10072
10073 if (TREE_CODE (arg) == REAL_CST)
10074 {
10075 r = TREE_REAL_CST (arg);
10076 if (real_isinf (&r))
10077 return real_compare (GT_EXPR, &r, &dconst0)
10078 ? integer_one_node : integer_minus_one_node;
10079 else
10080 return integer_zero_node;
10081 }
10082
10083 return NULL_TREE;
10084
10085 case BUILT_IN_ISINF_SIGN:
10086 {
10087 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10088 /* In a boolean context, GCC will fold the inner COND_EXPR to
10089 1. So e.g. "if (isinf_sign(x))" would be folded to just
10090 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10091 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10092 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10093 tree tmp = NULL_TREE;
10094
10095 arg = builtin_save_expr (arg);
10096
10097 if (signbit_fn && isinf_fn)
10098 {
10099 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10100 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10101
10102 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10103 signbit_call, integer_zero_node);
10104 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10105 isinf_call, integer_zero_node);
10106
10107 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10108 integer_minus_one_node, integer_one_node);
10109 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10110 isinf_call, tmp,
10111 integer_zero_node);
10112 }
10113
10114 return tmp;
10115 }
10116
10117 case BUILT_IN_ISFINITE:
10118 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10119 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10120 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10121
10122 if (TREE_CODE (arg) == REAL_CST)
10123 {
10124 r = TREE_REAL_CST (arg);
10125 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10126 }
10127
10128 return NULL_TREE;
10129
10130 case BUILT_IN_ISNAN:
10131 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10132 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10133
10134 if (TREE_CODE (arg) == REAL_CST)
10135 {
10136 r = TREE_REAL_CST (arg);
10137 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10138 }
10139
10140 arg = builtin_save_expr (arg);
10141 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10142
10143 default:
10144 gcc_unreachable ();
10145 }
10146 }
10147
10148 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10149 This builtin will generate code to return the appropriate floating
10150 point classification depending on the value of the floating point
10151 number passed in. The possible return values must be supplied as
10152 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10153 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10154 one floating point argument which is "type generic". */
10155
10156 static tree
10157 fold_builtin_fpclassify (location_t loc, tree exp)
10158 {
10159 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10160 arg, type, res, tmp;
10161 enum machine_mode mode;
10162 REAL_VALUE_TYPE r;
10163 char buf[128];
10164
10165 /* Verify the required arguments in the original call. */
10166 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10167 INTEGER_TYPE, INTEGER_TYPE,
10168 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10169 return NULL_TREE;
10170
10171 fp_nan = CALL_EXPR_ARG (exp, 0);
10172 fp_infinite = CALL_EXPR_ARG (exp, 1);
10173 fp_normal = CALL_EXPR_ARG (exp, 2);
10174 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10175 fp_zero = CALL_EXPR_ARG (exp, 4);
10176 arg = CALL_EXPR_ARG (exp, 5);
10177 type = TREE_TYPE (arg);
10178 mode = TYPE_MODE (type);
10179 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10180
10181 /* fpclassify(x) ->
10182 isnan(x) ? FP_NAN :
10183 (fabs(x) == Inf ? FP_INFINITE :
10184 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10185 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10186
10187 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10188 build_real (type, dconst0));
10189 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10190 tmp, fp_zero, fp_subnormal);
10191
10192 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10193 real_from_string (&r, buf);
10194 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10195 arg, build_real (type, r));
10196 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10197
10198 if (HONOR_INFINITIES (mode))
10199 {
10200 real_inf (&r);
10201 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10202 build_real (type, r));
10203 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10204 fp_infinite, res);
10205 }
10206
10207 if (HONOR_NANS (mode))
10208 {
10209 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10210 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10211 }
10212
10213 return res;
10214 }
10215
10216 /* Fold a call to an unordered comparison function such as
10217 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10218 being called and ARG0 and ARG1 are the arguments for the call.
10219 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10220 the opposite of the desired result. UNORDERED_CODE is used
10221 for modes that can hold NaNs and ORDERED_CODE is used for
10222 the rest. */
10223
10224 static tree
10225 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10226 enum tree_code unordered_code,
10227 enum tree_code ordered_code)
10228 {
10229 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10230 enum tree_code code;
10231 tree type0, type1;
10232 enum tree_code code0, code1;
10233 tree cmp_type = NULL_TREE;
10234
10235 type0 = TREE_TYPE (arg0);
10236 type1 = TREE_TYPE (arg1);
10237
10238 code0 = TREE_CODE (type0);
10239 code1 = TREE_CODE (type1);
10240
10241 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10242 /* Choose the wider of two real types. */
10243 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10244 ? type0 : type1;
10245 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10246 cmp_type = type0;
10247 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10248 cmp_type = type1;
10249
10250 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10251 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10252
10253 if (unordered_code == UNORDERED_EXPR)
10254 {
10255 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10256 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10257 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10258 }
10259
10260 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10261 : ordered_code;
10262 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10263 fold_build2_loc (loc, code, type, arg0, arg1));
10264 }
10265
10266 /* Fold a call to built-in function FNDECL with 0 arguments.
10267 IGNORE is true if the result of the function call is ignored. This
10268 function returns NULL_TREE if no simplification was possible. */
10269
10270 static tree
10271 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10272 {
10273 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10274 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10275 switch (fcode)
10276 {
10277 CASE_FLT_FN (BUILT_IN_INF):
10278 case BUILT_IN_INFD32:
10279 case BUILT_IN_INFD64:
10280 case BUILT_IN_INFD128:
10281 return fold_builtin_inf (loc, type, true);
10282
10283 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10284 return fold_builtin_inf (loc, type, false);
10285
10286 case BUILT_IN_CLASSIFY_TYPE:
10287 return fold_builtin_classify_type (NULL_TREE);
10288
10289 case BUILT_IN_UNREACHABLE:
10290 if (flag_sanitize & SANITIZE_UNREACHABLE
10291 && (current_function_decl == NULL
10292 || !lookup_attribute ("no_sanitize_undefined",
10293 DECL_ATTRIBUTES (current_function_decl))))
10294 return ubsan_instrument_unreachable (loc);
10295 break;
10296
10297 default:
10298 break;
10299 }
10300 return NULL_TREE;
10301 }
10302
10303 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10304 IGNORE is true if the result of the function call is ignored. This
10305 function returns NULL_TREE if no simplification was possible. */
10306
10307 static tree
10308 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10309 {
10310 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10311 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10312 switch (fcode)
10313 {
10314 case BUILT_IN_CONSTANT_P:
10315 {
10316 tree val = fold_builtin_constant_p (arg0);
10317
10318 /* Gimplification will pull the CALL_EXPR for the builtin out of
10319 an if condition. When not optimizing, we'll not CSE it back.
10320 To avoid link error types of regressions, return false now. */
10321 if (!val && !optimize)
10322 val = integer_zero_node;
10323
10324 return val;
10325 }
10326
10327 case BUILT_IN_CLASSIFY_TYPE:
10328 return fold_builtin_classify_type (arg0);
10329
10330 case BUILT_IN_STRLEN:
10331 return fold_builtin_strlen (loc, type, arg0);
10332
10333 CASE_FLT_FN (BUILT_IN_FABS):
10334 case BUILT_IN_FABSD32:
10335 case BUILT_IN_FABSD64:
10336 case BUILT_IN_FABSD128:
10337 return fold_builtin_fabs (loc, arg0, type);
10338
10339 case BUILT_IN_ABS:
10340 case BUILT_IN_LABS:
10341 case BUILT_IN_LLABS:
10342 case BUILT_IN_IMAXABS:
10343 return fold_builtin_abs (loc, arg0, type);
10344
10345 CASE_FLT_FN (BUILT_IN_CONJ):
10346 if (validate_arg (arg0, COMPLEX_TYPE)
10347 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10348 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10349 break;
10350
10351 CASE_FLT_FN (BUILT_IN_CREAL):
10352 if (validate_arg (arg0, COMPLEX_TYPE)
10353 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10354 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10355 break;
10356
10357 CASE_FLT_FN (BUILT_IN_CIMAG):
10358 if (validate_arg (arg0, COMPLEX_TYPE)
10359 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10360 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10361 break;
10362
10363 CASE_FLT_FN (BUILT_IN_CCOS):
10364 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10365
10366 CASE_FLT_FN (BUILT_IN_CCOSH):
10367 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10368
10369 CASE_FLT_FN (BUILT_IN_CPROJ):
10370 return fold_builtin_cproj (loc, arg0, type);
10371
10372 CASE_FLT_FN (BUILT_IN_CSIN):
10373 if (validate_arg (arg0, COMPLEX_TYPE)
10374 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10375 return do_mpc_arg1 (arg0, type, mpc_sin);
10376 break;
10377
10378 CASE_FLT_FN (BUILT_IN_CSINH):
10379 if (validate_arg (arg0, COMPLEX_TYPE)
10380 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10381 return do_mpc_arg1 (arg0, type, mpc_sinh);
10382 break;
10383
10384 CASE_FLT_FN (BUILT_IN_CTAN):
10385 if (validate_arg (arg0, COMPLEX_TYPE)
10386 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10387 return do_mpc_arg1 (arg0, type, mpc_tan);
10388 break;
10389
10390 CASE_FLT_FN (BUILT_IN_CTANH):
10391 if (validate_arg (arg0, COMPLEX_TYPE)
10392 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10393 return do_mpc_arg1 (arg0, type, mpc_tanh);
10394 break;
10395
10396 CASE_FLT_FN (BUILT_IN_CLOG):
10397 if (validate_arg (arg0, COMPLEX_TYPE)
10398 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10399 return do_mpc_arg1 (arg0, type, mpc_log);
10400 break;
10401
10402 CASE_FLT_FN (BUILT_IN_CSQRT):
10403 if (validate_arg (arg0, COMPLEX_TYPE)
10404 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10405 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10406 break;
10407
10408 CASE_FLT_FN (BUILT_IN_CASIN):
10409 if (validate_arg (arg0, COMPLEX_TYPE)
10410 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10411 return do_mpc_arg1 (arg0, type, mpc_asin);
10412 break;
10413
10414 CASE_FLT_FN (BUILT_IN_CACOS):
10415 if (validate_arg (arg0, COMPLEX_TYPE)
10416 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10417 return do_mpc_arg1 (arg0, type, mpc_acos);
10418 break;
10419
10420 CASE_FLT_FN (BUILT_IN_CATAN):
10421 if (validate_arg (arg0, COMPLEX_TYPE)
10422 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10423 return do_mpc_arg1 (arg0, type, mpc_atan);
10424 break;
10425
10426 CASE_FLT_FN (BUILT_IN_CASINH):
10427 if (validate_arg (arg0, COMPLEX_TYPE)
10428 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10429 return do_mpc_arg1 (arg0, type, mpc_asinh);
10430 break;
10431
10432 CASE_FLT_FN (BUILT_IN_CACOSH):
10433 if (validate_arg (arg0, COMPLEX_TYPE)
10434 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10435 return do_mpc_arg1 (arg0, type, mpc_acosh);
10436 break;
10437
10438 CASE_FLT_FN (BUILT_IN_CATANH):
10439 if (validate_arg (arg0, COMPLEX_TYPE)
10440 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10441 return do_mpc_arg1 (arg0, type, mpc_atanh);
10442 break;
10443
10444 CASE_FLT_FN (BUILT_IN_CABS):
10445 return fold_builtin_cabs (loc, arg0, type, fndecl);
10446
10447 CASE_FLT_FN (BUILT_IN_CARG):
10448 return fold_builtin_carg (loc, arg0, type);
10449
10450 CASE_FLT_FN (BUILT_IN_SQRT):
10451 return fold_builtin_sqrt (loc, arg0, type);
10452
10453 CASE_FLT_FN (BUILT_IN_CBRT):
10454 return fold_builtin_cbrt (loc, arg0, type);
10455
10456 CASE_FLT_FN (BUILT_IN_ASIN):
10457 if (validate_arg (arg0, REAL_TYPE))
10458 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10459 &dconstm1, &dconst1, true);
10460 break;
10461
10462 CASE_FLT_FN (BUILT_IN_ACOS):
10463 if (validate_arg (arg0, REAL_TYPE))
10464 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10465 &dconstm1, &dconst1, true);
10466 break;
10467
10468 CASE_FLT_FN (BUILT_IN_ATAN):
10469 if (validate_arg (arg0, REAL_TYPE))
10470 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10471 break;
10472
10473 CASE_FLT_FN (BUILT_IN_ASINH):
10474 if (validate_arg (arg0, REAL_TYPE))
10475 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10476 break;
10477
10478 CASE_FLT_FN (BUILT_IN_ACOSH):
10479 if (validate_arg (arg0, REAL_TYPE))
10480 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10481 &dconst1, NULL, true);
10482 break;
10483
10484 CASE_FLT_FN (BUILT_IN_ATANH):
10485 if (validate_arg (arg0, REAL_TYPE))
10486 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10487 &dconstm1, &dconst1, false);
10488 break;
10489
10490 CASE_FLT_FN (BUILT_IN_SIN):
10491 if (validate_arg (arg0, REAL_TYPE))
10492 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10493 break;
10494
10495 CASE_FLT_FN (BUILT_IN_COS):
10496 return fold_builtin_cos (loc, arg0, type, fndecl);
10497
10498 CASE_FLT_FN (BUILT_IN_TAN):
10499 return fold_builtin_tan (arg0, type);
10500
10501 CASE_FLT_FN (BUILT_IN_CEXP):
10502 return fold_builtin_cexp (loc, arg0, type);
10503
10504 CASE_FLT_FN (BUILT_IN_CEXPI):
10505 if (validate_arg (arg0, REAL_TYPE))
10506 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10507 break;
10508
10509 CASE_FLT_FN (BUILT_IN_SINH):
10510 if (validate_arg (arg0, REAL_TYPE))
10511 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10512 break;
10513
10514 CASE_FLT_FN (BUILT_IN_COSH):
10515 return fold_builtin_cosh (loc, arg0, type, fndecl);
10516
10517 CASE_FLT_FN (BUILT_IN_TANH):
10518 if (validate_arg (arg0, REAL_TYPE))
10519 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10520 break;
10521
10522 CASE_FLT_FN (BUILT_IN_ERF):
10523 if (validate_arg (arg0, REAL_TYPE))
10524 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10525 break;
10526
10527 CASE_FLT_FN (BUILT_IN_ERFC):
10528 if (validate_arg (arg0, REAL_TYPE))
10529 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10530 break;
10531
10532 CASE_FLT_FN (BUILT_IN_TGAMMA):
10533 if (validate_arg (arg0, REAL_TYPE))
10534 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10535 break;
10536
10537 CASE_FLT_FN (BUILT_IN_EXP):
10538 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10539
10540 CASE_FLT_FN (BUILT_IN_EXP2):
10541 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10542
10543 CASE_FLT_FN (BUILT_IN_EXP10):
10544 CASE_FLT_FN (BUILT_IN_POW10):
10545 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10546
10547 CASE_FLT_FN (BUILT_IN_EXPM1):
10548 if (validate_arg (arg0, REAL_TYPE))
10549 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10550 break;
10551
10552 CASE_FLT_FN (BUILT_IN_LOG):
10553 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10554
10555 CASE_FLT_FN (BUILT_IN_LOG2):
10556 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10557
10558 CASE_FLT_FN (BUILT_IN_LOG10):
10559 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10560
10561 CASE_FLT_FN (BUILT_IN_LOG1P):
10562 if (validate_arg (arg0, REAL_TYPE))
10563 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10564 &dconstm1, NULL, false);
10565 break;
10566
10567 CASE_FLT_FN (BUILT_IN_J0):
10568 if (validate_arg (arg0, REAL_TYPE))
10569 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10570 NULL, NULL, 0);
10571 break;
10572
10573 CASE_FLT_FN (BUILT_IN_J1):
10574 if (validate_arg (arg0, REAL_TYPE))
10575 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10576 NULL, NULL, 0);
10577 break;
10578
10579 CASE_FLT_FN (BUILT_IN_Y0):
10580 if (validate_arg (arg0, REAL_TYPE))
10581 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10582 &dconst0, NULL, false);
10583 break;
10584
10585 CASE_FLT_FN (BUILT_IN_Y1):
10586 if (validate_arg (arg0, REAL_TYPE))
10587 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10588 &dconst0, NULL, false);
10589 break;
10590
10591 CASE_FLT_FN (BUILT_IN_NAN):
10592 case BUILT_IN_NAND32:
10593 case BUILT_IN_NAND64:
10594 case BUILT_IN_NAND128:
10595 return fold_builtin_nan (arg0, type, true);
10596
10597 CASE_FLT_FN (BUILT_IN_NANS):
10598 return fold_builtin_nan (arg0, type, false);
10599
10600 CASE_FLT_FN (BUILT_IN_FLOOR):
10601 return fold_builtin_floor (loc, fndecl, arg0);
10602
10603 CASE_FLT_FN (BUILT_IN_CEIL):
10604 return fold_builtin_ceil (loc, fndecl, arg0);
10605
10606 CASE_FLT_FN (BUILT_IN_TRUNC):
10607 return fold_builtin_trunc (loc, fndecl, arg0);
10608
10609 CASE_FLT_FN (BUILT_IN_ROUND):
10610 return fold_builtin_round (loc, fndecl, arg0);
10611
10612 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10613 CASE_FLT_FN (BUILT_IN_RINT):
10614 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10615
10616 CASE_FLT_FN (BUILT_IN_ICEIL):
10617 CASE_FLT_FN (BUILT_IN_LCEIL):
10618 CASE_FLT_FN (BUILT_IN_LLCEIL):
10619 CASE_FLT_FN (BUILT_IN_LFLOOR):
10620 CASE_FLT_FN (BUILT_IN_IFLOOR):
10621 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10622 CASE_FLT_FN (BUILT_IN_IROUND):
10623 CASE_FLT_FN (BUILT_IN_LROUND):
10624 CASE_FLT_FN (BUILT_IN_LLROUND):
10625 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10626
10627 CASE_FLT_FN (BUILT_IN_IRINT):
10628 CASE_FLT_FN (BUILT_IN_LRINT):
10629 CASE_FLT_FN (BUILT_IN_LLRINT):
10630 return fold_fixed_mathfn (loc, fndecl, arg0);
10631
10632 case BUILT_IN_BSWAP16:
10633 case BUILT_IN_BSWAP32:
10634 case BUILT_IN_BSWAP64:
10635 return fold_builtin_bswap (fndecl, arg0);
10636
10637 CASE_INT_FN (BUILT_IN_FFS):
10638 CASE_INT_FN (BUILT_IN_CLZ):
10639 CASE_INT_FN (BUILT_IN_CTZ):
10640 CASE_INT_FN (BUILT_IN_CLRSB):
10641 CASE_INT_FN (BUILT_IN_POPCOUNT):
10642 CASE_INT_FN (BUILT_IN_PARITY):
10643 return fold_builtin_bitop (fndecl, arg0);
10644
10645 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10646 return fold_builtin_signbit (loc, arg0, type);
10647
10648 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10649 return fold_builtin_significand (loc, arg0, type);
10650
10651 CASE_FLT_FN (BUILT_IN_ILOGB):
10652 CASE_FLT_FN (BUILT_IN_LOGB):
10653 return fold_builtin_logb (loc, arg0, type);
10654
10655 case BUILT_IN_ISASCII:
10656 return fold_builtin_isascii (loc, arg0);
10657
10658 case BUILT_IN_TOASCII:
10659 return fold_builtin_toascii (loc, arg0);
10660
10661 case BUILT_IN_ISDIGIT:
10662 return fold_builtin_isdigit (loc, arg0);
10663
10664 CASE_FLT_FN (BUILT_IN_FINITE):
10665 case BUILT_IN_FINITED32:
10666 case BUILT_IN_FINITED64:
10667 case BUILT_IN_FINITED128:
10668 case BUILT_IN_ISFINITE:
10669 {
10670 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10671 if (ret)
10672 return ret;
10673 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10674 }
10675
10676 CASE_FLT_FN (BUILT_IN_ISINF):
10677 case BUILT_IN_ISINFD32:
10678 case BUILT_IN_ISINFD64:
10679 case BUILT_IN_ISINFD128:
10680 {
10681 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10682 if (ret)
10683 return ret;
10684 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10685 }
10686
10687 case BUILT_IN_ISNORMAL:
10688 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10689
10690 case BUILT_IN_ISINF_SIGN:
10691 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10692
10693 CASE_FLT_FN (BUILT_IN_ISNAN):
10694 case BUILT_IN_ISNAND32:
10695 case BUILT_IN_ISNAND64:
10696 case BUILT_IN_ISNAND128:
10697 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10698
10699 case BUILT_IN_PRINTF:
10700 case BUILT_IN_PRINTF_UNLOCKED:
10701 case BUILT_IN_VPRINTF:
10702 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10703
10704 case BUILT_IN_FREE:
10705 if (integer_zerop (arg0))
10706 return build_empty_stmt (loc);
10707 break;
10708
10709 default:
10710 break;
10711 }
10712
10713 return NULL_TREE;
10714
10715 }
10716
10717 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10718 IGNORE is true if the result of the function call is ignored. This
10719 function returns NULL_TREE if no simplification was possible. */
10720
10721 static tree
10722 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10723 {
10724 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10725 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10726
10727 switch (fcode)
10728 {
10729 CASE_FLT_FN (BUILT_IN_JN):
10730 if (validate_arg (arg0, INTEGER_TYPE)
10731 && validate_arg (arg1, REAL_TYPE))
10732 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10733 break;
10734
10735 CASE_FLT_FN (BUILT_IN_YN):
10736 if (validate_arg (arg0, INTEGER_TYPE)
10737 && validate_arg (arg1, REAL_TYPE))
10738 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10739 &dconst0, false);
10740 break;
10741
10742 CASE_FLT_FN (BUILT_IN_DREM):
10743 CASE_FLT_FN (BUILT_IN_REMAINDER):
10744 if (validate_arg (arg0, REAL_TYPE)
10745 && validate_arg (arg1, REAL_TYPE))
10746 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10747 break;
10748
10749 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10750 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10751 if (validate_arg (arg0, REAL_TYPE)
10752 && validate_arg (arg1, POINTER_TYPE))
10753 return do_mpfr_lgamma_r (arg0, arg1, type);
10754 break;
10755
10756 CASE_FLT_FN (BUILT_IN_ATAN2):
10757 if (validate_arg (arg0, REAL_TYPE)
10758 && validate_arg (arg1, REAL_TYPE))
10759 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10760 break;
10761
10762 CASE_FLT_FN (BUILT_IN_FDIM):
10763 if (validate_arg (arg0, REAL_TYPE)
10764 && validate_arg (arg1, REAL_TYPE))
10765 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10766 break;
10767
10768 CASE_FLT_FN (BUILT_IN_HYPOT):
10769 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10770
10771 CASE_FLT_FN (BUILT_IN_CPOW):
10772 if (validate_arg (arg0, COMPLEX_TYPE)
10773 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10774 && validate_arg (arg1, COMPLEX_TYPE)
10775 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10776 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10777 break;
10778
10779 CASE_FLT_FN (BUILT_IN_LDEXP):
10780 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10781 CASE_FLT_FN (BUILT_IN_SCALBN):
10782 CASE_FLT_FN (BUILT_IN_SCALBLN):
10783 return fold_builtin_load_exponent (loc, arg0, arg1,
10784 type, /*ldexp=*/false);
10785
10786 CASE_FLT_FN (BUILT_IN_FREXP):
10787 return fold_builtin_frexp (loc, arg0, arg1, type);
10788
10789 CASE_FLT_FN (BUILT_IN_MODF):
10790 return fold_builtin_modf (loc, arg0, arg1, type);
10791
10792 case BUILT_IN_BZERO:
10793 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10794
10795 case BUILT_IN_FPUTS:
10796 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10797
10798 case BUILT_IN_FPUTS_UNLOCKED:
10799 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10800
10801 case BUILT_IN_STRSTR:
10802 return fold_builtin_strstr (loc, arg0, arg1, type);
10803
10804 case BUILT_IN_STRCAT:
10805 return fold_builtin_strcat (loc, arg0, arg1, NULL_TREE);
10806
10807 case BUILT_IN_STRSPN:
10808 return fold_builtin_strspn (loc, arg0, arg1);
10809
10810 case BUILT_IN_STRCSPN:
10811 return fold_builtin_strcspn (loc, arg0, arg1);
10812
10813 case BUILT_IN_STRCHR:
10814 case BUILT_IN_INDEX:
10815 return fold_builtin_strchr (loc, arg0, arg1, type);
10816
10817 case BUILT_IN_STRRCHR:
10818 case BUILT_IN_RINDEX:
10819 return fold_builtin_strrchr (loc, arg0, arg1, type);
10820
10821 case BUILT_IN_STRCPY:
10822 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10823
10824 case BUILT_IN_STPCPY:
10825 if (ignore)
10826 {
10827 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10828 if (!fn)
10829 break;
10830
10831 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10832 }
10833 else
10834 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10835 break;
10836
10837 case BUILT_IN_STRCMP:
10838 return fold_builtin_strcmp (loc, arg0, arg1);
10839
10840 case BUILT_IN_STRPBRK:
10841 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10842
10843 case BUILT_IN_EXPECT:
10844 return fold_builtin_expect (loc, arg0, arg1);
10845
10846 CASE_FLT_FN (BUILT_IN_POW):
10847 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10848
10849 CASE_FLT_FN (BUILT_IN_POWI):
10850 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10851
10852 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10853 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10854
10855 CASE_FLT_FN (BUILT_IN_FMIN):
10856 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10857
10858 CASE_FLT_FN (BUILT_IN_FMAX):
10859 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10860
10861 case BUILT_IN_ISGREATER:
10862 return fold_builtin_unordered_cmp (loc, fndecl,
10863 arg0, arg1, UNLE_EXPR, LE_EXPR);
10864 case BUILT_IN_ISGREATEREQUAL:
10865 return fold_builtin_unordered_cmp (loc, fndecl,
10866 arg0, arg1, UNLT_EXPR, LT_EXPR);
10867 case BUILT_IN_ISLESS:
10868 return fold_builtin_unordered_cmp (loc, fndecl,
10869 arg0, arg1, UNGE_EXPR, GE_EXPR);
10870 case BUILT_IN_ISLESSEQUAL:
10871 return fold_builtin_unordered_cmp (loc, fndecl,
10872 arg0, arg1, UNGT_EXPR, GT_EXPR);
10873 case BUILT_IN_ISLESSGREATER:
10874 return fold_builtin_unordered_cmp (loc, fndecl,
10875 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10876 case BUILT_IN_ISUNORDERED:
10877 return fold_builtin_unordered_cmp (loc, fndecl,
10878 arg0, arg1, UNORDERED_EXPR,
10879 NOP_EXPR);
10880
10881 /* We do the folding for va_start in the expander. */
10882 case BUILT_IN_VA_START:
10883 break;
10884
10885 case BUILT_IN_SPRINTF:
10886 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10887
10888 case BUILT_IN_OBJECT_SIZE:
10889 return fold_builtin_object_size (arg0, arg1);
10890
10891 case BUILT_IN_PRINTF:
10892 case BUILT_IN_PRINTF_UNLOCKED:
10893 case BUILT_IN_VPRINTF:
10894 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10895
10896 case BUILT_IN_PRINTF_CHK:
10897 case BUILT_IN_VPRINTF_CHK:
10898 if (!validate_arg (arg0, INTEGER_TYPE)
10899 || TREE_SIDE_EFFECTS (arg0))
10900 return NULL_TREE;
10901 else
10902 return fold_builtin_printf (loc, fndecl,
10903 arg1, NULL_TREE, ignore, fcode);
10904 break;
10905
10906 case BUILT_IN_FPRINTF:
10907 case BUILT_IN_FPRINTF_UNLOCKED:
10908 case BUILT_IN_VFPRINTF:
10909 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10910 ignore, fcode);
10911
10912 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10913 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10914
10915 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10916 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10917
10918 default:
10919 break;
10920 }
10921 return NULL_TREE;
10922 }
10923
10924 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10925 and ARG2. IGNORE is true if the result of the function call is ignored.
10926 This function returns NULL_TREE if no simplification was possible. */
10927
10928 static tree
10929 fold_builtin_3 (location_t loc, tree fndecl,
10930 tree arg0, tree arg1, tree arg2, bool ignore)
10931 {
10932 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10933 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10934 switch (fcode)
10935 {
10936
10937 CASE_FLT_FN (BUILT_IN_SINCOS):
10938 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10939
10940 CASE_FLT_FN (BUILT_IN_FMA):
10941 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10942 break;
10943
10944 CASE_FLT_FN (BUILT_IN_REMQUO):
10945 if (validate_arg (arg0, REAL_TYPE)
10946 && validate_arg (arg1, REAL_TYPE)
10947 && validate_arg (arg2, POINTER_TYPE))
10948 return do_mpfr_remquo (arg0, arg1, arg2);
10949 break;
10950
10951 case BUILT_IN_MEMSET:
10952 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10953
10954 case BUILT_IN_BCOPY:
10955 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10956 void_type_node, true, /*endp=*/3);
10957
10958 case BUILT_IN_MEMCPY:
10959 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10960 type, ignore, /*endp=*/0);
10961
10962 case BUILT_IN_MEMPCPY:
10963 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10964 type, ignore, /*endp=*/1);
10965
10966 case BUILT_IN_MEMMOVE:
10967 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10968 type, ignore, /*endp=*/3);
10969
10970 case BUILT_IN_STRNCAT:
10971 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10972
10973 case BUILT_IN_STRNCPY:
10974 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10975
10976 case BUILT_IN_STRNCMP:
10977 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10978
10979 case BUILT_IN_MEMCHR:
10980 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10981
10982 case BUILT_IN_BCMP:
10983 case BUILT_IN_MEMCMP:
10984 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10985
10986 case BUILT_IN_SPRINTF:
10987 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10988
10989 case BUILT_IN_SNPRINTF:
10990 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10991
10992 case BUILT_IN_STRCPY_CHK:
10993 case BUILT_IN_STPCPY_CHK:
10994 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10995 ignore, fcode);
10996
10997 case BUILT_IN_STRCAT_CHK:
10998 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10999
11000 case BUILT_IN_PRINTF_CHK:
11001 case BUILT_IN_VPRINTF_CHK:
11002 if (!validate_arg (arg0, INTEGER_TYPE)
11003 || TREE_SIDE_EFFECTS (arg0))
11004 return NULL_TREE;
11005 else
11006 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
11007 break;
11008
11009 case BUILT_IN_FPRINTF:
11010 case BUILT_IN_FPRINTF_UNLOCKED:
11011 case BUILT_IN_VFPRINTF:
11012 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
11013 ignore, fcode);
11014
11015 case BUILT_IN_FPRINTF_CHK:
11016 case BUILT_IN_VFPRINTF_CHK:
11017 if (!validate_arg (arg1, INTEGER_TYPE)
11018 || TREE_SIDE_EFFECTS (arg1))
11019 return NULL_TREE;
11020 else
11021 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
11022 ignore, fcode);
11023
11024 default:
11025 break;
11026 }
11027 return NULL_TREE;
11028 }
11029
11030 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
11031 ARG2, and ARG3. IGNORE is true if the result of the function call is
11032 ignored. This function returns NULL_TREE if no simplification was
11033 possible. */
11034
11035 static tree
11036 fold_builtin_4 (location_t loc, tree fndecl,
11037 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11038 {
11039 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11040
11041 switch (fcode)
11042 {
11043 case BUILT_IN_MEMCPY_CHK:
11044 case BUILT_IN_MEMPCPY_CHK:
11045 case BUILT_IN_MEMMOVE_CHK:
11046 case BUILT_IN_MEMSET_CHK:
11047 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11048 NULL_TREE, ignore,
11049 DECL_FUNCTION_CODE (fndecl));
11050
11051 case BUILT_IN_STRNCPY_CHK:
11052 case BUILT_IN_STPNCPY_CHK:
11053 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11054 ignore, fcode);
11055
11056 case BUILT_IN_STRNCAT_CHK:
11057 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11058
11059 case BUILT_IN_SNPRINTF:
11060 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11061
11062 case BUILT_IN_FPRINTF_CHK:
11063 case BUILT_IN_VFPRINTF_CHK:
11064 if (!validate_arg (arg1, INTEGER_TYPE)
11065 || TREE_SIDE_EFFECTS (arg1))
11066 return NULL_TREE;
11067 else
11068 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11069 ignore, fcode);
11070 break;
11071
11072 default:
11073 break;
11074 }
11075 return NULL_TREE;
11076 }
11077
11078 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11079 arguments, where NARGS <= 4. IGNORE is true if the result of the
11080 function call is ignored. This function returns NULL_TREE if no
11081 simplification was possible. Note that this only folds builtins with
11082 fixed argument patterns. Foldings that do varargs-to-varargs
11083 transformations, or that match calls with more than 4 arguments,
11084 need to be handled with fold_builtin_varargs instead. */
11085
11086 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11087
11088 static tree
11089 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11090 {
11091 tree ret = NULL_TREE;
11092
11093 switch (nargs)
11094 {
11095 case 0:
11096 ret = fold_builtin_0 (loc, fndecl, ignore);
11097 break;
11098 case 1:
11099 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11100 break;
11101 case 2:
11102 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11103 break;
11104 case 3:
11105 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11106 break;
11107 case 4:
11108 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11109 ignore);
11110 break;
11111 default:
11112 break;
11113 }
11114 if (ret)
11115 {
11116 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11117 SET_EXPR_LOCATION (ret, loc);
11118 TREE_NO_WARNING (ret) = 1;
11119 return ret;
11120 }
11121 return NULL_TREE;
11122 }
11123
11124 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11125 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11126 of arguments in ARGS to be omitted. OLDNARGS is the number of
11127 elements in ARGS. */
11128
11129 static tree
11130 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11131 int skip, tree fndecl, int n, va_list newargs)
11132 {
11133 int nargs = oldnargs - skip + n;
11134 tree *buffer;
11135
11136 if (n > 0)
11137 {
11138 int i, j;
11139
11140 buffer = XALLOCAVEC (tree, nargs);
11141 for (i = 0; i < n; i++)
11142 buffer[i] = va_arg (newargs, tree);
11143 for (j = skip; j < oldnargs; j++, i++)
11144 buffer[i] = args[j];
11145 }
11146 else
11147 buffer = args + skip;
11148
11149 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11150 }
11151
11152 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11153 list ARGS along with N new arguments specified as the "..."
11154 parameters. SKIP is the number of arguments in ARGS to be omitted.
11155 OLDNARGS is the number of elements in ARGS. */
11156
11157 static tree
11158 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11159 int skip, tree fndecl, int n, ...)
11160 {
11161 va_list ap;
11162 tree t;
11163
11164 va_start (ap, n);
11165 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11166 va_end (ap);
11167
11168 return t;
11169 }
11170
11171 /* Return true if FNDECL shouldn't be folded right now.
11172 If a built-in function has an inline attribute always_inline
11173 wrapper, defer folding it after always_inline functions have
11174 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11175 might not be performed. */
11176
11177 bool
11178 avoid_folding_inline_builtin (tree fndecl)
11179 {
11180 return (DECL_DECLARED_INLINE_P (fndecl)
11181 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11182 && cfun
11183 && !cfun->always_inline_functions_inlined
11184 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11185 }
11186
11187 /* A wrapper function for builtin folding that prevents warnings for
11188 "statement without effect" and the like, caused by removing the
11189 call node earlier than the warning is generated. */
11190
11191 tree
11192 fold_call_expr (location_t loc, tree exp, bool ignore)
11193 {
11194 tree ret = NULL_TREE;
11195 tree fndecl = get_callee_fndecl (exp);
11196 if (fndecl
11197 && TREE_CODE (fndecl) == FUNCTION_DECL
11198 && DECL_BUILT_IN (fndecl)
11199 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11200 yet. Defer folding until we see all the arguments
11201 (after inlining). */
11202 && !CALL_EXPR_VA_ARG_PACK (exp))
11203 {
11204 int nargs = call_expr_nargs (exp);
11205
11206 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11207 instead last argument is __builtin_va_arg_pack (). Defer folding
11208 even in that case, until arguments are finalized. */
11209 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11210 {
11211 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11212 if (fndecl2
11213 && TREE_CODE (fndecl2) == FUNCTION_DECL
11214 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11215 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11216 return NULL_TREE;
11217 }
11218
11219 if (avoid_folding_inline_builtin (fndecl))
11220 return NULL_TREE;
11221
11222 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11223 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11224 CALL_EXPR_ARGP (exp), ignore);
11225 else
11226 {
11227 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11228 {
11229 tree *args = CALL_EXPR_ARGP (exp);
11230 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11231 }
11232 if (!ret)
11233 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11234 if (ret)
11235 return ret;
11236 }
11237 }
11238 return NULL_TREE;
11239 }
11240
11241 /* Conveniently construct a function call expression. FNDECL names the
11242 function to be called and N arguments are passed in the array
11243 ARGARRAY. */
11244
11245 tree
11246 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11247 {
11248 tree fntype = TREE_TYPE (fndecl);
11249 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11250
11251 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11252 }
11253
11254 /* Conveniently construct a function call expression. FNDECL names the
11255 function to be called and the arguments are passed in the vector
11256 VEC. */
11257
11258 tree
11259 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11260 {
11261 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11262 vec_safe_address (vec));
11263 }
11264
11265
11266 /* Conveniently construct a function call expression. FNDECL names the
11267 function to be called, N is the number of arguments, and the "..."
11268 parameters are the argument expressions. */
11269
11270 tree
11271 build_call_expr_loc (location_t loc, 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 (loc, fndecl, n, argarray);
11282 }
11283
11284 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11285 varargs macros aren't supported by all bootstrap compilers. */
11286
11287 tree
11288 build_call_expr (tree fndecl, int n, ...)
11289 {
11290 va_list ap;
11291 tree *argarray = XALLOCAVEC (tree, n);
11292 int i;
11293
11294 va_start (ap, n);
11295 for (i = 0; i < n; i++)
11296 argarray[i] = va_arg (ap, tree);
11297 va_end (ap);
11298 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11299 }
11300
11301 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11302 N arguments are passed in the array ARGARRAY. */
11303
11304 tree
11305 fold_builtin_call_array (location_t loc, tree type,
11306 tree fn,
11307 int n,
11308 tree *argarray)
11309 {
11310 tree ret = NULL_TREE;
11311 tree exp;
11312
11313 if (TREE_CODE (fn) == ADDR_EXPR)
11314 {
11315 tree fndecl = TREE_OPERAND (fn, 0);
11316 if (TREE_CODE (fndecl) == FUNCTION_DECL
11317 && DECL_BUILT_IN (fndecl))
11318 {
11319 /* If last argument is __builtin_va_arg_pack (), arguments to this
11320 function are not finalized yet. Defer folding until they are. */
11321 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11322 {
11323 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11324 if (fndecl2
11325 && TREE_CODE (fndecl2) == FUNCTION_DECL
11326 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11327 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11328 return build_call_array_loc (loc, type, fn, n, argarray);
11329 }
11330 if (avoid_folding_inline_builtin (fndecl))
11331 return build_call_array_loc (loc, type, fn, n, argarray);
11332 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11333 {
11334 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11335 if (ret)
11336 return ret;
11337
11338 return build_call_array_loc (loc, type, fn, n, argarray);
11339 }
11340 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11341 {
11342 /* First try the transformations that don't require consing up
11343 an exp. */
11344 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11345 if (ret)
11346 return ret;
11347 }
11348
11349 /* If we got this far, we need to build an exp. */
11350 exp = build_call_array_loc (loc, type, fn, n, argarray);
11351 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11352 return ret ? ret : exp;
11353 }
11354 }
11355
11356 return build_call_array_loc (loc, type, fn, n, argarray);
11357 }
11358
11359 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11360 along with N new arguments specified as the "..." parameters. SKIP
11361 is the number of arguments in EXP to be omitted. This function is used
11362 to do varargs-to-varargs transformations. */
11363
11364 static tree
11365 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11366 {
11367 va_list ap;
11368 tree t;
11369
11370 va_start (ap, n);
11371 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11372 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11373 va_end (ap);
11374
11375 return t;
11376 }
11377
11378 /* Validate a single argument ARG against a tree code CODE representing
11379 a type. */
11380
11381 static bool
11382 validate_arg (const_tree arg, enum tree_code code)
11383 {
11384 if (!arg)
11385 return false;
11386 else if (code == POINTER_TYPE)
11387 return POINTER_TYPE_P (TREE_TYPE (arg));
11388 else if (code == INTEGER_TYPE)
11389 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11390 return code == TREE_CODE (TREE_TYPE (arg));
11391 }
11392
11393 /* This function validates the types of a function call argument list
11394 against a specified list of tree_codes. If the last specifier is a 0,
11395 that represents an ellipses, otherwise the last specifier must be a
11396 VOID_TYPE.
11397
11398 This is the GIMPLE version of validate_arglist. Eventually we want to
11399 completely convert builtins.c to work from GIMPLEs and the tree based
11400 validate_arglist will then be removed. */
11401
11402 bool
11403 validate_gimple_arglist (const_gimple call, ...)
11404 {
11405 enum tree_code code;
11406 bool res = 0;
11407 va_list ap;
11408 const_tree arg;
11409 size_t i;
11410
11411 va_start (ap, call);
11412 i = 0;
11413
11414 do
11415 {
11416 code = (enum tree_code) va_arg (ap, int);
11417 switch (code)
11418 {
11419 case 0:
11420 /* This signifies an ellipses, any further arguments are all ok. */
11421 res = true;
11422 goto end;
11423 case VOID_TYPE:
11424 /* This signifies an endlink, if no arguments remain, return
11425 true, otherwise return false. */
11426 res = (i == gimple_call_num_args (call));
11427 goto end;
11428 default:
11429 /* If no parameters remain or the parameter's code does not
11430 match the specified code, return false. Otherwise continue
11431 checking any remaining arguments. */
11432 arg = gimple_call_arg (call, i++);
11433 if (!validate_arg (arg, code))
11434 goto end;
11435 break;
11436 }
11437 }
11438 while (1);
11439
11440 /* We need gotos here since we can only have one VA_CLOSE in a
11441 function. */
11442 end: ;
11443 va_end (ap);
11444
11445 return res;
11446 }
11447
11448 /* Default target-specific builtin expander that does nothing. */
11449
11450 rtx
11451 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11452 rtx target ATTRIBUTE_UNUSED,
11453 rtx subtarget ATTRIBUTE_UNUSED,
11454 enum machine_mode mode ATTRIBUTE_UNUSED,
11455 int ignore ATTRIBUTE_UNUSED)
11456 {
11457 return NULL_RTX;
11458 }
11459
11460 /* Returns true is EXP represents data that would potentially reside
11461 in a readonly section. */
11462
11463 static bool
11464 readonly_data_expr (tree exp)
11465 {
11466 STRIP_NOPS (exp);
11467
11468 if (TREE_CODE (exp) != ADDR_EXPR)
11469 return false;
11470
11471 exp = get_base_address (TREE_OPERAND (exp, 0));
11472 if (!exp)
11473 return false;
11474
11475 /* Make sure we call decl_readonly_section only for trees it
11476 can handle (since it returns true for everything it doesn't
11477 understand). */
11478 if (TREE_CODE (exp) == STRING_CST
11479 || TREE_CODE (exp) == CONSTRUCTOR
11480 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11481 return decl_readonly_section (exp, 0);
11482 else
11483 return false;
11484 }
11485
11486 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11487 to the call, and TYPE is its return type.
11488
11489 Return NULL_TREE if no simplification was possible, otherwise return the
11490 simplified form of the call as a tree.
11491
11492 The simplified form may be a constant or other expression which
11493 computes the same value, but in a more efficient manner (including
11494 calls to other builtin functions).
11495
11496 The call may contain arguments which need to be evaluated, but
11497 which are not useful to determine the result of the call. In
11498 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11499 COMPOUND_EXPR will be an argument which must be evaluated.
11500 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11501 COMPOUND_EXPR in the chain will contain the tree for the simplified
11502 form of the builtin function call. */
11503
11504 static tree
11505 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11506 {
11507 if (!validate_arg (s1, POINTER_TYPE)
11508 || !validate_arg (s2, POINTER_TYPE))
11509 return NULL_TREE;
11510 else
11511 {
11512 tree fn;
11513 const char *p1, *p2;
11514
11515 p2 = c_getstr (s2);
11516 if (p2 == NULL)
11517 return NULL_TREE;
11518
11519 p1 = c_getstr (s1);
11520 if (p1 != NULL)
11521 {
11522 const char *r = strstr (p1, p2);
11523 tree tem;
11524
11525 if (r == NULL)
11526 return build_int_cst (TREE_TYPE (s1), 0);
11527
11528 /* Return an offset into the constant string argument. */
11529 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11530 return fold_convert_loc (loc, type, tem);
11531 }
11532
11533 /* The argument is const char *, and the result is char *, so we need
11534 a type conversion here to avoid a warning. */
11535 if (p2[0] == '\0')
11536 return fold_convert_loc (loc, type, s1);
11537
11538 if (p2[1] != '\0')
11539 return NULL_TREE;
11540
11541 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11542 if (!fn)
11543 return NULL_TREE;
11544
11545 /* New argument list transforming strstr(s1, s2) to
11546 strchr(s1, s2[0]). */
11547 return build_call_expr_loc (loc, fn, 2, s1,
11548 build_int_cst (integer_type_node, p2[0]));
11549 }
11550 }
11551
11552 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11553 the call, and TYPE is its return type.
11554
11555 Return NULL_TREE if no simplification was possible, otherwise return the
11556 simplified form of the call as a tree.
11557
11558 The simplified form may be a constant or other expression which
11559 computes the same value, but in a more efficient manner (including
11560 calls to other builtin functions).
11561
11562 The call may contain arguments which need to be evaluated, but
11563 which are not useful to determine the result of the call. In
11564 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11565 COMPOUND_EXPR will be an argument which must be evaluated.
11566 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11567 COMPOUND_EXPR in the chain will contain the tree for the simplified
11568 form of the builtin function call. */
11569
11570 static tree
11571 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11572 {
11573 if (!validate_arg (s1, POINTER_TYPE)
11574 || !validate_arg (s2, INTEGER_TYPE))
11575 return NULL_TREE;
11576 else
11577 {
11578 const char *p1;
11579
11580 if (TREE_CODE (s2) != INTEGER_CST)
11581 return NULL_TREE;
11582
11583 p1 = c_getstr (s1);
11584 if (p1 != NULL)
11585 {
11586 char c;
11587 const char *r;
11588 tree tem;
11589
11590 if (target_char_cast (s2, &c))
11591 return NULL_TREE;
11592
11593 r = strchr (p1, c);
11594
11595 if (r == NULL)
11596 return build_int_cst (TREE_TYPE (s1), 0);
11597
11598 /* Return an offset into the constant string argument. */
11599 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11600 return fold_convert_loc (loc, type, tem);
11601 }
11602 return NULL_TREE;
11603 }
11604 }
11605
11606 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11607 the call, and TYPE is its return type.
11608
11609 Return NULL_TREE if no simplification was possible, otherwise return the
11610 simplified form of the call as a tree.
11611
11612 The simplified form may be a constant or other expression which
11613 computes the same value, but in a more efficient manner (including
11614 calls to other builtin functions).
11615
11616 The call may contain arguments which need to be evaluated, but
11617 which are not useful to determine the result of the call. In
11618 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11619 COMPOUND_EXPR will be an argument which must be evaluated.
11620 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11621 COMPOUND_EXPR in the chain will contain the tree for the simplified
11622 form of the builtin function call. */
11623
11624 static tree
11625 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11626 {
11627 if (!validate_arg (s1, POINTER_TYPE)
11628 || !validate_arg (s2, INTEGER_TYPE))
11629 return NULL_TREE;
11630 else
11631 {
11632 tree fn;
11633 const char *p1;
11634
11635 if (TREE_CODE (s2) != INTEGER_CST)
11636 return NULL_TREE;
11637
11638 p1 = c_getstr (s1);
11639 if (p1 != NULL)
11640 {
11641 char c;
11642 const char *r;
11643 tree tem;
11644
11645 if (target_char_cast (s2, &c))
11646 return NULL_TREE;
11647
11648 r = strrchr (p1, c);
11649
11650 if (r == NULL)
11651 return build_int_cst (TREE_TYPE (s1), 0);
11652
11653 /* Return an offset into the constant string argument. */
11654 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11655 return fold_convert_loc (loc, type, tem);
11656 }
11657
11658 if (! integer_zerop (s2))
11659 return NULL_TREE;
11660
11661 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11662 if (!fn)
11663 return NULL_TREE;
11664
11665 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11666 return build_call_expr_loc (loc, fn, 2, s1, s2);
11667 }
11668 }
11669
11670 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11671 to the call, and TYPE is its return type.
11672
11673 Return NULL_TREE if no simplification was possible, otherwise return the
11674 simplified form of the call as a tree.
11675
11676 The simplified form may be a constant or other expression which
11677 computes the same value, but in a more efficient manner (including
11678 calls to other builtin functions).
11679
11680 The call may contain arguments which need to be evaluated, but
11681 which are not useful to determine the result of the call. In
11682 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11683 COMPOUND_EXPR will be an argument which must be evaluated.
11684 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11685 COMPOUND_EXPR in the chain will contain the tree for the simplified
11686 form of the builtin function call. */
11687
11688 static tree
11689 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11690 {
11691 if (!validate_arg (s1, POINTER_TYPE)
11692 || !validate_arg (s2, POINTER_TYPE))
11693 return NULL_TREE;
11694 else
11695 {
11696 tree fn;
11697 const char *p1, *p2;
11698
11699 p2 = c_getstr (s2);
11700 if (p2 == NULL)
11701 return NULL_TREE;
11702
11703 p1 = c_getstr (s1);
11704 if (p1 != NULL)
11705 {
11706 const char *r = strpbrk (p1, p2);
11707 tree tem;
11708
11709 if (r == NULL)
11710 return build_int_cst (TREE_TYPE (s1), 0);
11711
11712 /* Return an offset into the constant string argument. */
11713 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11714 return fold_convert_loc (loc, type, tem);
11715 }
11716
11717 if (p2[0] == '\0')
11718 /* strpbrk(x, "") == NULL.
11719 Evaluate and ignore s1 in case it had side-effects. */
11720 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11721
11722 if (p2[1] != '\0')
11723 return NULL_TREE; /* Really call strpbrk. */
11724
11725 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11726 if (!fn)
11727 return NULL_TREE;
11728
11729 /* New argument list transforming strpbrk(s1, s2) to
11730 strchr(s1, s2[0]). */
11731 return build_call_expr_loc (loc, fn, 2, s1,
11732 build_int_cst (integer_type_node, p2[0]));
11733 }
11734 }
11735
11736 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11737 to the call.
11738
11739 Return NULL_TREE if no simplification was possible, otherwise return the
11740 simplified form of the call as a tree.
11741
11742 The simplified form may be a constant or other expression which
11743 computes the same value, but in a more efficient manner (including
11744 calls to other builtin functions).
11745
11746 The call may contain arguments which need to be evaluated, but
11747 which are not useful to determine the result of the call. In
11748 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11749 COMPOUND_EXPR will be an argument which must be evaluated.
11750 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11751 COMPOUND_EXPR in the chain will contain the tree for the simplified
11752 form of the builtin function call. */
11753
11754 tree
11755 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src,
11756 tree len)
11757 {
11758 if (!validate_arg (dst, POINTER_TYPE)
11759 || !validate_arg (src, POINTER_TYPE))
11760 return NULL_TREE;
11761 else
11762 {
11763 const char *p = c_getstr (src);
11764
11765 /* If the string length is zero, return the dst parameter. */
11766 if (p && *p == '\0')
11767 return dst;
11768
11769 if (optimize_insn_for_speed_p ())
11770 {
11771 /* See if we can store by pieces into (dst + strlen(dst)). */
11772 tree newdst, call;
11773 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11774 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
11775
11776 if (!strlen_fn || !memcpy_fn)
11777 return NULL_TREE;
11778
11779 /* If the length of the source string isn't computable don't
11780 split strcat into strlen and memcpy. */
11781 if (! len)
11782 len = c_strlen (src, 1);
11783 if (! len || TREE_SIDE_EFFECTS (len))
11784 return NULL_TREE;
11785
11786 /* Stabilize the argument list. */
11787 dst = builtin_save_expr (dst);
11788
11789 /* Create strlen (dst). */
11790 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11791 /* Create (dst p+ strlen (dst)). */
11792
11793 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11794 newdst = builtin_save_expr (newdst);
11795
11796 len = fold_convert_loc (loc, size_type_node, len);
11797 len = size_binop_loc (loc, PLUS_EXPR, len,
11798 build_int_cst (size_type_node, 1));
11799
11800 call = build_call_expr_loc (loc, memcpy_fn, 3, newdst, src, len);
11801 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11802 }
11803 return NULL_TREE;
11804 }
11805 }
11806
11807 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11808 arguments to the call.
11809
11810 Return NULL_TREE if no simplification was possible, otherwise return the
11811 simplified form of the call as a tree.
11812
11813 The simplified form may be a constant or other expression which
11814 computes the same value, but in a more efficient manner (including
11815 calls to other builtin functions).
11816
11817 The call may contain arguments which need to be evaluated, but
11818 which are not useful to determine the result of the call. In
11819 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11820 COMPOUND_EXPR will be an argument which must be evaluated.
11821 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11822 COMPOUND_EXPR in the chain will contain the tree for the simplified
11823 form of the builtin function call. */
11824
11825 static tree
11826 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11827 {
11828 if (!validate_arg (dst, POINTER_TYPE)
11829 || !validate_arg (src, POINTER_TYPE)
11830 || !validate_arg (len, INTEGER_TYPE))
11831 return NULL_TREE;
11832 else
11833 {
11834 const char *p = c_getstr (src);
11835
11836 /* If the requested length is zero, or the src parameter string
11837 length is zero, return the dst parameter. */
11838 if (integer_zerop (len) || (p && *p == '\0'))
11839 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11840
11841 /* If the requested len is greater than or equal to the string
11842 length, call strcat. */
11843 if (TREE_CODE (len) == INTEGER_CST && p
11844 && compare_tree_int (len, strlen (p)) >= 0)
11845 {
11846 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11847
11848 /* If the replacement _DECL isn't initialized, don't do the
11849 transformation. */
11850 if (!fn)
11851 return NULL_TREE;
11852
11853 return build_call_expr_loc (loc, fn, 2, dst, src);
11854 }
11855 return NULL_TREE;
11856 }
11857 }
11858
11859 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11860 to the call.
11861
11862 Return NULL_TREE if no simplification was possible, otherwise return the
11863 simplified form of the call as a tree.
11864
11865 The simplified form may be a constant or other expression which
11866 computes the same value, but in a more efficient manner (including
11867 calls to other builtin functions).
11868
11869 The call may contain arguments which need to be evaluated, but
11870 which are not useful to determine the result of the call. In
11871 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11872 COMPOUND_EXPR will be an argument which must be evaluated.
11873 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11874 COMPOUND_EXPR in the chain will contain the tree for the simplified
11875 form of the builtin function call. */
11876
11877 static tree
11878 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11879 {
11880 if (!validate_arg (s1, POINTER_TYPE)
11881 || !validate_arg (s2, POINTER_TYPE))
11882 return NULL_TREE;
11883 else
11884 {
11885 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11886
11887 /* If both arguments are constants, evaluate at compile-time. */
11888 if (p1 && p2)
11889 {
11890 const size_t r = strspn (p1, p2);
11891 return build_int_cst (size_type_node, r);
11892 }
11893
11894 /* If either argument is "", return NULL_TREE. */
11895 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11896 /* Evaluate and ignore both arguments in case either one has
11897 side-effects. */
11898 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11899 s1, s2);
11900 return NULL_TREE;
11901 }
11902 }
11903
11904 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11905 to the call.
11906
11907 Return NULL_TREE if no simplification was possible, otherwise return the
11908 simplified form of the call as a tree.
11909
11910 The simplified form may be a constant or other expression which
11911 computes the same value, but in a more efficient manner (including
11912 calls to other builtin functions).
11913
11914 The call may contain arguments which need to be evaluated, but
11915 which are not useful to determine the result of the call. In
11916 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11917 COMPOUND_EXPR will be an argument which must be evaluated.
11918 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11919 COMPOUND_EXPR in the chain will contain the tree for the simplified
11920 form of the builtin function call. */
11921
11922 static tree
11923 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11924 {
11925 if (!validate_arg (s1, POINTER_TYPE)
11926 || !validate_arg (s2, POINTER_TYPE))
11927 return NULL_TREE;
11928 else
11929 {
11930 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11931
11932 /* If both arguments are constants, evaluate at compile-time. */
11933 if (p1 && p2)
11934 {
11935 const size_t r = strcspn (p1, p2);
11936 return build_int_cst (size_type_node, r);
11937 }
11938
11939 /* If the first argument is "", return NULL_TREE. */
11940 if (p1 && *p1 == '\0')
11941 {
11942 /* Evaluate and ignore argument s2 in case it has
11943 side-effects. */
11944 return omit_one_operand_loc (loc, size_type_node,
11945 size_zero_node, s2);
11946 }
11947
11948 /* If the second argument is "", return __builtin_strlen(s1). */
11949 if (p2 && *p2 == '\0')
11950 {
11951 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11952
11953 /* If the replacement _DECL isn't initialized, don't do the
11954 transformation. */
11955 if (!fn)
11956 return NULL_TREE;
11957
11958 return build_call_expr_loc (loc, fn, 1, s1);
11959 }
11960 return NULL_TREE;
11961 }
11962 }
11963
11964 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11965 to the call. IGNORE is true if the value returned
11966 by the builtin will be ignored. UNLOCKED is true is true if this
11967 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11968 the known length of the string. Return NULL_TREE if no simplification
11969 was possible. */
11970
11971 tree
11972 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11973 bool ignore, bool unlocked, tree len)
11974 {
11975 /* If we're using an unlocked function, assume the other unlocked
11976 functions exist explicitly. */
11977 tree const fn_fputc = (unlocked
11978 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11979 : builtin_decl_implicit (BUILT_IN_FPUTC));
11980 tree const fn_fwrite = (unlocked
11981 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11982 : builtin_decl_implicit (BUILT_IN_FWRITE));
11983
11984 /* If the return value is used, don't do the transformation. */
11985 if (!ignore)
11986 return NULL_TREE;
11987
11988 /* Verify the arguments in the original call. */
11989 if (!validate_arg (arg0, POINTER_TYPE)
11990 || !validate_arg (arg1, POINTER_TYPE))
11991 return NULL_TREE;
11992
11993 if (! len)
11994 len = c_strlen (arg0, 0);
11995
11996 /* Get the length of the string passed to fputs. If the length
11997 can't be determined, punt. */
11998 if (!len
11999 || TREE_CODE (len) != INTEGER_CST)
12000 return NULL_TREE;
12001
12002 switch (compare_tree_int (len, 1))
12003 {
12004 case -1: /* length is 0, delete the call entirely . */
12005 return omit_one_operand_loc (loc, integer_type_node,
12006 integer_zero_node, arg1);;
12007
12008 case 0: /* length is 1, call fputc. */
12009 {
12010 const char *p = c_getstr (arg0);
12011
12012 if (p != NULL)
12013 {
12014 if (fn_fputc)
12015 return build_call_expr_loc (loc, fn_fputc, 2,
12016 build_int_cst
12017 (integer_type_node, p[0]), arg1);
12018 else
12019 return NULL_TREE;
12020 }
12021 }
12022 /* FALLTHROUGH */
12023 case 1: /* length is greater than 1, call fwrite. */
12024 {
12025 /* If optimizing for size keep fputs. */
12026 if (optimize_function_for_size_p (cfun))
12027 return NULL_TREE;
12028 /* New argument list transforming fputs(string, stream) to
12029 fwrite(string, 1, len, stream). */
12030 if (fn_fwrite)
12031 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12032 size_one_node, len, arg1);
12033 else
12034 return NULL_TREE;
12035 }
12036 default:
12037 gcc_unreachable ();
12038 }
12039 return NULL_TREE;
12040 }
12041
12042 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12043 produced. False otherwise. This is done so that we don't output the error
12044 or warning twice or three times. */
12045
12046 bool
12047 fold_builtin_next_arg (tree exp, bool va_start_p)
12048 {
12049 tree fntype = TREE_TYPE (current_function_decl);
12050 int nargs = call_expr_nargs (exp);
12051 tree arg;
12052 /* There is good chance the current input_location points inside the
12053 definition of the va_start macro (perhaps on the token for
12054 builtin) in a system header, so warnings will not be emitted.
12055 Use the location in real source code. */
12056 source_location current_location =
12057 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12058 NULL);
12059
12060 if (!stdarg_p (fntype))
12061 {
12062 error ("%<va_start%> used in function with fixed args");
12063 return true;
12064 }
12065
12066 if (va_start_p)
12067 {
12068 if (va_start_p && (nargs != 2))
12069 {
12070 error ("wrong number of arguments to function %<va_start%>");
12071 return true;
12072 }
12073 arg = CALL_EXPR_ARG (exp, 1);
12074 }
12075 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12076 when we checked the arguments and if needed issued a warning. */
12077 else
12078 {
12079 if (nargs == 0)
12080 {
12081 /* Evidently an out of date version of <stdarg.h>; can't validate
12082 va_start's second argument, but can still work as intended. */
12083 warning_at (current_location,
12084 OPT_Wvarargs,
12085 "%<__builtin_next_arg%> called without an argument");
12086 return true;
12087 }
12088 else if (nargs > 1)
12089 {
12090 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12091 return true;
12092 }
12093 arg = CALL_EXPR_ARG (exp, 0);
12094 }
12095
12096 if (TREE_CODE (arg) == SSA_NAME)
12097 arg = SSA_NAME_VAR (arg);
12098
12099 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12100 or __builtin_next_arg (0) the first time we see it, after checking
12101 the arguments and if needed issuing a warning. */
12102 if (!integer_zerop (arg))
12103 {
12104 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12105
12106 /* Strip off all nops for the sake of the comparison. This
12107 is not quite the same as STRIP_NOPS. It does more.
12108 We must also strip off INDIRECT_EXPR for C++ reference
12109 parameters. */
12110 while (CONVERT_EXPR_P (arg)
12111 || TREE_CODE (arg) == INDIRECT_REF)
12112 arg = TREE_OPERAND (arg, 0);
12113 if (arg != last_parm)
12114 {
12115 /* FIXME: Sometimes with the tree optimizers we can get the
12116 not the last argument even though the user used the last
12117 argument. We just warn and set the arg to be the last
12118 argument so that we will get wrong-code because of
12119 it. */
12120 warning_at (current_location,
12121 OPT_Wvarargs,
12122 "second parameter of %<va_start%> not last named argument");
12123 }
12124
12125 /* Undefined by C99 7.15.1.4p4 (va_start):
12126 "If the parameter parmN is declared with the register storage
12127 class, with a function or array type, or with a type that is
12128 not compatible with the type that results after application of
12129 the default argument promotions, the behavior is undefined."
12130 */
12131 else if (DECL_REGISTER (arg))
12132 {
12133 warning_at (current_location,
12134 OPT_Wvarargs,
12135 "undefined behaviour when second parameter of "
12136 "%<va_start%> is declared with %<register%> storage");
12137 }
12138
12139 /* We want to verify the second parameter just once before the tree
12140 optimizers are run and then avoid keeping it in the tree,
12141 as otherwise we could warn even for correct code like:
12142 void foo (int i, ...)
12143 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12144 if (va_start_p)
12145 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12146 else
12147 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12148 }
12149 return false;
12150 }
12151
12152
12153 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12154 ORIG may be null if this is a 2-argument call. We don't attempt to
12155 simplify calls with more than 3 arguments.
12156
12157 Return NULL_TREE if no simplification was possible, otherwise return the
12158 simplified form of the call as a tree. If IGNORED is true, it means that
12159 the caller does not use the returned value of the function. */
12160
12161 static tree
12162 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12163 tree orig, int ignored)
12164 {
12165 tree call, retval;
12166 const char *fmt_str = NULL;
12167
12168 /* Verify the required arguments in the original call. We deal with two
12169 types of sprintf() calls: 'sprintf (str, fmt)' and
12170 'sprintf (dest, "%s", orig)'. */
12171 if (!validate_arg (dest, POINTER_TYPE)
12172 || !validate_arg (fmt, POINTER_TYPE))
12173 return NULL_TREE;
12174 if (orig && !validate_arg (orig, POINTER_TYPE))
12175 return NULL_TREE;
12176
12177 /* Check whether the format is a literal string constant. */
12178 fmt_str = c_getstr (fmt);
12179 if (fmt_str == NULL)
12180 return NULL_TREE;
12181
12182 call = NULL_TREE;
12183 retval = NULL_TREE;
12184
12185 if (!init_target_chars ())
12186 return NULL_TREE;
12187
12188 /* If the format doesn't contain % args or %%, use strcpy. */
12189 if (strchr (fmt_str, target_percent) == NULL)
12190 {
12191 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12192
12193 if (!fn)
12194 return NULL_TREE;
12195
12196 /* Don't optimize sprintf (buf, "abc", ptr++). */
12197 if (orig)
12198 return NULL_TREE;
12199
12200 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12201 'format' is known to contain no % formats. */
12202 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12203 if (!ignored)
12204 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12205 }
12206
12207 /* If the format is "%s", use strcpy if the result isn't used. */
12208 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12209 {
12210 tree fn;
12211 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12212
12213 if (!fn)
12214 return NULL_TREE;
12215
12216 /* Don't crash on sprintf (str1, "%s"). */
12217 if (!orig)
12218 return NULL_TREE;
12219
12220 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12221 if (!ignored)
12222 {
12223 retval = c_strlen (orig, 1);
12224 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12225 return NULL_TREE;
12226 }
12227 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12228 }
12229
12230 if (call && retval)
12231 {
12232 retval = fold_convert_loc
12233 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12234 retval);
12235 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12236 }
12237 else
12238 return call;
12239 }
12240
12241 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12242 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12243 attempt to simplify calls with more than 4 arguments.
12244
12245 Return NULL_TREE if no simplification was possible, otherwise return the
12246 simplified form of the call as a tree. If IGNORED is true, it means that
12247 the caller does not use the returned value of the function. */
12248
12249 static tree
12250 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12251 tree orig, int ignored)
12252 {
12253 tree call, retval;
12254 const char *fmt_str = NULL;
12255 unsigned HOST_WIDE_INT destlen;
12256
12257 /* Verify the required arguments in the original call. We deal with two
12258 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12259 'snprintf (dest, cst, "%s", orig)'. */
12260 if (!validate_arg (dest, POINTER_TYPE)
12261 || !validate_arg (destsize, INTEGER_TYPE)
12262 || !validate_arg (fmt, POINTER_TYPE))
12263 return NULL_TREE;
12264 if (orig && !validate_arg (orig, POINTER_TYPE))
12265 return NULL_TREE;
12266
12267 if (!tree_fits_uhwi_p (destsize))
12268 return NULL_TREE;
12269
12270 /* Check whether the format is a literal string constant. */
12271 fmt_str = c_getstr (fmt);
12272 if (fmt_str == NULL)
12273 return NULL_TREE;
12274
12275 call = NULL_TREE;
12276 retval = NULL_TREE;
12277
12278 if (!init_target_chars ())
12279 return NULL_TREE;
12280
12281 destlen = tree_to_uhwi (destsize);
12282
12283 /* If the format doesn't contain % args or %%, use strcpy. */
12284 if (strchr (fmt_str, target_percent) == NULL)
12285 {
12286 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12287 size_t len = strlen (fmt_str);
12288
12289 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12290 if (orig)
12291 return NULL_TREE;
12292
12293 /* We could expand this as
12294 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12295 or to
12296 memcpy (str, fmt_with_nul_at_cstm1, cst);
12297 but in the former case that might increase code size
12298 and in the latter case grow .rodata section too much.
12299 So punt for now. */
12300 if (len >= destlen)
12301 return NULL_TREE;
12302
12303 if (!fn)
12304 return NULL_TREE;
12305
12306 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12307 'format' is known to contain no % formats and
12308 strlen (fmt) < cst. */
12309 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12310
12311 if (!ignored)
12312 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12313 }
12314
12315 /* If the format is "%s", use strcpy if the result isn't used. */
12316 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12317 {
12318 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12319 unsigned HOST_WIDE_INT origlen;
12320
12321 /* Don't crash on snprintf (str1, cst, "%s"). */
12322 if (!orig)
12323 return NULL_TREE;
12324
12325 retval = c_strlen (orig, 1);
12326 if (!retval || !tree_fits_uhwi_p (retval))
12327 return NULL_TREE;
12328
12329 origlen = tree_to_uhwi (retval);
12330 /* We could expand this as
12331 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12332 or to
12333 memcpy (str1, str2_with_nul_at_cstm1, cst);
12334 but in the former case that might increase code size
12335 and in the latter case grow .rodata section too much.
12336 So punt for now. */
12337 if (origlen >= destlen)
12338 return NULL_TREE;
12339
12340 /* Convert snprintf (str1, cst, "%s", str2) into
12341 strcpy (str1, str2) if strlen (str2) < cst. */
12342 if (!fn)
12343 return NULL_TREE;
12344
12345 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12346
12347 if (ignored)
12348 retval = NULL_TREE;
12349 }
12350
12351 if (call && retval)
12352 {
12353 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12354 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12355 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12356 }
12357 else
12358 return call;
12359 }
12360
12361 /* Expand a call EXP to __builtin_object_size. */
12362
12363 rtx
12364 expand_builtin_object_size (tree exp)
12365 {
12366 tree ost;
12367 int object_size_type;
12368 tree fndecl = get_callee_fndecl (exp);
12369
12370 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12371 {
12372 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12373 exp, fndecl);
12374 expand_builtin_trap ();
12375 return const0_rtx;
12376 }
12377
12378 ost = CALL_EXPR_ARG (exp, 1);
12379 STRIP_NOPS (ost);
12380
12381 if (TREE_CODE (ost) != INTEGER_CST
12382 || tree_int_cst_sgn (ost) < 0
12383 || compare_tree_int (ost, 3) > 0)
12384 {
12385 error ("%Klast argument of %D is not integer constant between 0 and 3",
12386 exp, fndecl);
12387 expand_builtin_trap ();
12388 return const0_rtx;
12389 }
12390
12391 object_size_type = tree_to_shwi (ost);
12392
12393 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12394 }
12395
12396 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12397 FCODE is the BUILT_IN_* to use.
12398 Return NULL_RTX if we failed; the caller should emit a normal call,
12399 otherwise try to get the result in TARGET, if convenient (and in
12400 mode MODE if that's convenient). */
12401
12402 static rtx
12403 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12404 enum built_in_function fcode)
12405 {
12406 tree dest, src, len, size;
12407
12408 if (!validate_arglist (exp,
12409 POINTER_TYPE,
12410 fcode == BUILT_IN_MEMSET_CHK
12411 ? INTEGER_TYPE : POINTER_TYPE,
12412 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12413 return NULL_RTX;
12414
12415 dest = CALL_EXPR_ARG (exp, 0);
12416 src = CALL_EXPR_ARG (exp, 1);
12417 len = CALL_EXPR_ARG (exp, 2);
12418 size = CALL_EXPR_ARG (exp, 3);
12419
12420 if (! tree_fits_uhwi_p (size))
12421 return NULL_RTX;
12422
12423 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12424 {
12425 tree fn;
12426
12427 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12428 {
12429 warning_at (tree_nonartificial_location (exp),
12430 0, "%Kcall to %D will always overflow destination buffer",
12431 exp, get_callee_fndecl (exp));
12432 return NULL_RTX;
12433 }
12434
12435 fn = NULL_TREE;
12436 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12437 mem{cpy,pcpy,move,set} is available. */
12438 switch (fcode)
12439 {
12440 case BUILT_IN_MEMCPY_CHK:
12441 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12442 break;
12443 case BUILT_IN_MEMPCPY_CHK:
12444 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12445 break;
12446 case BUILT_IN_MEMMOVE_CHK:
12447 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12448 break;
12449 case BUILT_IN_MEMSET_CHK:
12450 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12451 break;
12452 default:
12453 break;
12454 }
12455
12456 if (! fn)
12457 return NULL_RTX;
12458
12459 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12460 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12461 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12462 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12463 }
12464 else if (fcode == BUILT_IN_MEMSET_CHK)
12465 return NULL_RTX;
12466 else
12467 {
12468 unsigned int dest_align = get_pointer_alignment (dest);
12469
12470 /* If DEST is not a pointer type, call the normal function. */
12471 if (dest_align == 0)
12472 return NULL_RTX;
12473
12474 /* If SRC and DEST are the same (and not volatile), do nothing. */
12475 if (operand_equal_p (src, dest, 0))
12476 {
12477 tree expr;
12478
12479 if (fcode != BUILT_IN_MEMPCPY_CHK)
12480 {
12481 /* Evaluate and ignore LEN in case it has side-effects. */
12482 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12483 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12484 }
12485
12486 expr = fold_build_pointer_plus (dest, len);
12487 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12488 }
12489
12490 /* __memmove_chk special case. */
12491 if (fcode == BUILT_IN_MEMMOVE_CHK)
12492 {
12493 unsigned int src_align = get_pointer_alignment (src);
12494
12495 if (src_align == 0)
12496 return NULL_RTX;
12497
12498 /* If src is categorized for a readonly section we can use
12499 normal __memcpy_chk. */
12500 if (readonly_data_expr (src))
12501 {
12502 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12503 if (!fn)
12504 return NULL_RTX;
12505 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12506 dest, src, len, size);
12507 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12508 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12509 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12510 }
12511 }
12512 return NULL_RTX;
12513 }
12514 }
12515
12516 /* Emit warning if a buffer overflow is detected at compile time. */
12517
12518 static void
12519 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12520 {
12521 int is_strlen = 0;
12522 tree len, size;
12523 location_t loc = tree_nonartificial_location (exp);
12524
12525 switch (fcode)
12526 {
12527 case BUILT_IN_STRCPY_CHK:
12528 case BUILT_IN_STPCPY_CHK:
12529 /* For __strcat_chk the warning will be emitted only if overflowing
12530 by at least strlen (dest) + 1 bytes. */
12531 case BUILT_IN_STRCAT_CHK:
12532 len = CALL_EXPR_ARG (exp, 1);
12533 size = CALL_EXPR_ARG (exp, 2);
12534 is_strlen = 1;
12535 break;
12536 case BUILT_IN_STRNCAT_CHK:
12537 case BUILT_IN_STRNCPY_CHK:
12538 case BUILT_IN_STPNCPY_CHK:
12539 len = CALL_EXPR_ARG (exp, 2);
12540 size = CALL_EXPR_ARG (exp, 3);
12541 break;
12542 case BUILT_IN_SNPRINTF_CHK:
12543 case BUILT_IN_VSNPRINTF_CHK:
12544 len = CALL_EXPR_ARG (exp, 1);
12545 size = CALL_EXPR_ARG (exp, 3);
12546 break;
12547 default:
12548 gcc_unreachable ();
12549 }
12550
12551 if (!len || !size)
12552 return;
12553
12554 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12555 return;
12556
12557 if (is_strlen)
12558 {
12559 len = c_strlen (len, 1);
12560 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12561 return;
12562 }
12563 else if (fcode == BUILT_IN_STRNCAT_CHK)
12564 {
12565 tree src = CALL_EXPR_ARG (exp, 1);
12566 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12567 return;
12568 src = c_strlen (src, 1);
12569 if (! src || ! tree_fits_uhwi_p (src))
12570 {
12571 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12572 exp, get_callee_fndecl (exp));
12573 return;
12574 }
12575 else if (tree_int_cst_lt (src, size))
12576 return;
12577 }
12578 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12579 return;
12580
12581 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12582 exp, get_callee_fndecl (exp));
12583 }
12584
12585 /* Emit warning if a buffer overflow is detected at compile time
12586 in __sprintf_chk/__vsprintf_chk calls. */
12587
12588 static void
12589 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12590 {
12591 tree size, len, fmt;
12592 const char *fmt_str;
12593 int nargs = call_expr_nargs (exp);
12594
12595 /* Verify the required arguments in the original call. */
12596
12597 if (nargs < 4)
12598 return;
12599 size = CALL_EXPR_ARG (exp, 2);
12600 fmt = CALL_EXPR_ARG (exp, 3);
12601
12602 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12603 return;
12604
12605 /* Check whether the format is a literal string constant. */
12606 fmt_str = c_getstr (fmt);
12607 if (fmt_str == NULL)
12608 return;
12609
12610 if (!init_target_chars ())
12611 return;
12612
12613 /* If the format doesn't contain % args or %%, we know its size. */
12614 if (strchr (fmt_str, target_percent) == 0)
12615 len = build_int_cstu (size_type_node, strlen (fmt_str));
12616 /* If the format is "%s" and first ... argument is a string literal,
12617 we know it too. */
12618 else if (fcode == BUILT_IN_SPRINTF_CHK
12619 && strcmp (fmt_str, target_percent_s) == 0)
12620 {
12621 tree arg;
12622
12623 if (nargs < 5)
12624 return;
12625 arg = CALL_EXPR_ARG (exp, 4);
12626 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12627 return;
12628
12629 len = c_strlen (arg, 1);
12630 if (!len || ! tree_fits_uhwi_p (len))
12631 return;
12632 }
12633 else
12634 return;
12635
12636 if (! tree_int_cst_lt (len, size))
12637 warning_at (tree_nonartificial_location (exp),
12638 0, "%Kcall to %D will always overflow destination buffer",
12639 exp, get_callee_fndecl (exp));
12640 }
12641
12642 /* Emit warning if a free is called with address of a variable. */
12643
12644 static void
12645 maybe_emit_free_warning (tree exp)
12646 {
12647 tree arg = CALL_EXPR_ARG (exp, 0);
12648
12649 STRIP_NOPS (arg);
12650 if (TREE_CODE (arg) != ADDR_EXPR)
12651 return;
12652
12653 arg = get_base_address (TREE_OPERAND (arg, 0));
12654 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12655 return;
12656
12657 if (SSA_VAR_P (arg))
12658 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12659 "%Kattempt to free a non-heap object %qD", exp, arg);
12660 else
12661 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12662 "%Kattempt to free a non-heap object", exp);
12663 }
12664
12665 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12666 if possible. */
12667
12668 tree
12669 fold_builtin_object_size (tree ptr, tree ost)
12670 {
12671 unsigned HOST_WIDE_INT bytes;
12672 int object_size_type;
12673
12674 if (!validate_arg (ptr, POINTER_TYPE)
12675 || !validate_arg (ost, INTEGER_TYPE))
12676 return NULL_TREE;
12677
12678 STRIP_NOPS (ost);
12679
12680 if (TREE_CODE (ost) != INTEGER_CST
12681 || tree_int_cst_sgn (ost) < 0
12682 || compare_tree_int (ost, 3) > 0)
12683 return NULL_TREE;
12684
12685 object_size_type = tree_to_shwi (ost);
12686
12687 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12688 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12689 and (size_t) 0 for types 2 and 3. */
12690 if (TREE_SIDE_EFFECTS (ptr))
12691 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12692
12693 if (TREE_CODE (ptr) == ADDR_EXPR)
12694 {
12695 bytes = compute_builtin_object_size (ptr, object_size_type);
12696 if (double_int_fits_to_tree_p (size_type_node,
12697 double_int::from_uhwi (bytes)))
12698 return build_int_cstu (size_type_node, bytes);
12699 }
12700 else if (TREE_CODE (ptr) == SSA_NAME)
12701 {
12702 /* If object size is not known yet, delay folding until
12703 later. Maybe subsequent passes will help determining
12704 it. */
12705 bytes = compute_builtin_object_size (ptr, object_size_type);
12706 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12707 && double_int_fits_to_tree_p (size_type_node,
12708 double_int::from_uhwi (bytes)))
12709 return build_int_cstu (size_type_node, bytes);
12710 }
12711
12712 return NULL_TREE;
12713 }
12714
12715 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12716 DEST, SRC, LEN, and SIZE are the arguments to the call.
12717 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12718 code of the builtin. If MAXLEN is not NULL, it is maximum length
12719 passed as third argument. */
12720
12721 tree
12722 fold_builtin_memory_chk (location_t loc, tree fndecl,
12723 tree dest, tree src, tree len, tree size,
12724 tree maxlen, bool ignore,
12725 enum built_in_function fcode)
12726 {
12727 tree fn;
12728
12729 if (!validate_arg (dest, POINTER_TYPE)
12730 || !validate_arg (src,
12731 (fcode == BUILT_IN_MEMSET_CHK
12732 ? INTEGER_TYPE : POINTER_TYPE))
12733 || !validate_arg (len, INTEGER_TYPE)
12734 || !validate_arg (size, INTEGER_TYPE))
12735 return NULL_TREE;
12736
12737 /* If SRC and DEST are the same (and not volatile), return DEST
12738 (resp. DEST+LEN for __mempcpy_chk). */
12739 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12740 {
12741 if (fcode != BUILT_IN_MEMPCPY_CHK)
12742 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12743 dest, len);
12744 else
12745 {
12746 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12747 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12748 }
12749 }
12750
12751 if (! tree_fits_uhwi_p (size))
12752 return NULL_TREE;
12753
12754 if (! integer_all_onesp (size))
12755 {
12756 if (! tree_fits_uhwi_p (len))
12757 {
12758 /* If LEN is not constant, try MAXLEN too.
12759 For MAXLEN only allow optimizing into non-_ocs function
12760 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12761 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12762 {
12763 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12764 {
12765 /* (void) __mempcpy_chk () can be optimized into
12766 (void) __memcpy_chk (). */
12767 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12768 if (!fn)
12769 return NULL_TREE;
12770
12771 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12772 }
12773 return NULL_TREE;
12774 }
12775 }
12776 else
12777 maxlen = len;
12778
12779 if (tree_int_cst_lt (size, maxlen))
12780 return NULL_TREE;
12781 }
12782
12783 fn = NULL_TREE;
12784 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12785 mem{cpy,pcpy,move,set} is available. */
12786 switch (fcode)
12787 {
12788 case BUILT_IN_MEMCPY_CHK:
12789 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12790 break;
12791 case BUILT_IN_MEMPCPY_CHK:
12792 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12793 break;
12794 case BUILT_IN_MEMMOVE_CHK:
12795 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12796 break;
12797 case BUILT_IN_MEMSET_CHK:
12798 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12799 break;
12800 default:
12801 break;
12802 }
12803
12804 if (!fn)
12805 return NULL_TREE;
12806
12807 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12808 }
12809
12810 /* Fold a call to the __st[rp]cpy_chk builtin.
12811 DEST, SRC, and SIZE are the arguments to the call.
12812 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12813 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12814 strings passed as second argument. */
12815
12816 tree
12817 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12818 tree src, tree size,
12819 tree maxlen, bool ignore,
12820 enum built_in_function fcode)
12821 {
12822 tree len, fn;
12823
12824 if (!validate_arg (dest, POINTER_TYPE)
12825 || !validate_arg (src, POINTER_TYPE)
12826 || !validate_arg (size, INTEGER_TYPE))
12827 return NULL_TREE;
12828
12829 /* If SRC and DEST are the same (and not volatile), return DEST. */
12830 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12831 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12832
12833 if (! tree_fits_uhwi_p (size))
12834 return NULL_TREE;
12835
12836 if (! integer_all_onesp (size))
12837 {
12838 len = c_strlen (src, 1);
12839 if (! len || ! tree_fits_uhwi_p (len))
12840 {
12841 /* If LEN is not constant, try MAXLEN too.
12842 For MAXLEN only allow optimizing into non-_ocs function
12843 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12844 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12845 {
12846 if (fcode == BUILT_IN_STPCPY_CHK)
12847 {
12848 if (! ignore)
12849 return NULL_TREE;
12850
12851 /* If return value of __stpcpy_chk is ignored,
12852 optimize into __strcpy_chk. */
12853 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12854 if (!fn)
12855 return NULL_TREE;
12856
12857 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12858 }
12859
12860 if (! len || TREE_SIDE_EFFECTS (len))
12861 return NULL_TREE;
12862
12863 /* If c_strlen returned something, but not a constant,
12864 transform __strcpy_chk into __memcpy_chk. */
12865 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12866 if (!fn)
12867 return NULL_TREE;
12868
12869 len = fold_convert_loc (loc, size_type_node, len);
12870 len = size_binop_loc (loc, PLUS_EXPR, len,
12871 build_int_cst (size_type_node, 1));
12872 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12873 build_call_expr_loc (loc, fn, 4,
12874 dest, src, len, size));
12875 }
12876 }
12877 else
12878 maxlen = len;
12879
12880 if (! tree_int_cst_lt (maxlen, size))
12881 return NULL_TREE;
12882 }
12883
12884 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12885 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12886 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12887 if (!fn)
12888 return NULL_TREE;
12889
12890 return build_call_expr_loc (loc, fn, 2, dest, src);
12891 }
12892
12893 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12894 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12895 length passed as third argument. IGNORE is true if return value can be
12896 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12897
12898 tree
12899 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12900 tree len, tree size, tree maxlen, bool ignore,
12901 enum built_in_function fcode)
12902 {
12903 tree fn;
12904
12905 if (!validate_arg (dest, POINTER_TYPE)
12906 || !validate_arg (src, POINTER_TYPE)
12907 || !validate_arg (len, INTEGER_TYPE)
12908 || !validate_arg (size, INTEGER_TYPE))
12909 return NULL_TREE;
12910
12911 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12912 {
12913 /* If return value of __stpncpy_chk is ignored,
12914 optimize into __strncpy_chk. */
12915 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12916 if (fn)
12917 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12918 }
12919
12920 if (! tree_fits_uhwi_p (size))
12921 return NULL_TREE;
12922
12923 if (! integer_all_onesp (size))
12924 {
12925 if (! tree_fits_uhwi_p (len))
12926 {
12927 /* If LEN is not constant, try MAXLEN too.
12928 For MAXLEN only allow optimizing into non-_ocs function
12929 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12930 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12931 return NULL_TREE;
12932 }
12933 else
12934 maxlen = len;
12935
12936 if (tree_int_cst_lt (size, maxlen))
12937 return NULL_TREE;
12938 }
12939
12940 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12941 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12942 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12943 if (!fn)
12944 return NULL_TREE;
12945
12946 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12947 }
12948
12949 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12950 are the arguments to the call. */
12951
12952 static tree
12953 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12954 tree src, tree size)
12955 {
12956 tree fn;
12957 const char *p;
12958
12959 if (!validate_arg (dest, POINTER_TYPE)
12960 || !validate_arg (src, POINTER_TYPE)
12961 || !validate_arg (size, INTEGER_TYPE))
12962 return NULL_TREE;
12963
12964 p = c_getstr (src);
12965 /* If the SRC parameter is "", return DEST. */
12966 if (p && *p == '\0')
12967 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12968
12969 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12970 return NULL_TREE;
12971
12972 /* If __builtin_strcat_chk is used, assume strcat is available. */
12973 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12974 if (!fn)
12975 return NULL_TREE;
12976
12977 return build_call_expr_loc (loc, fn, 2, dest, src);
12978 }
12979
12980 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12981 LEN, and SIZE. */
12982
12983 static tree
12984 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12985 tree dest, tree src, tree len, tree size)
12986 {
12987 tree fn;
12988 const char *p;
12989
12990 if (!validate_arg (dest, POINTER_TYPE)
12991 || !validate_arg (src, POINTER_TYPE)
12992 || !validate_arg (size, INTEGER_TYPE)
12993 || !validate_arg (size, INTEGER_TYPE))
12994 return NULL_TREE;
12995
12996 p = c_getstr (src);
12997 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12998 if (p && *p == '\0')
12999 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13000 else if (integer_zerop (len))
13001 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13002
13003 if (! tree_fits_uhwi_p (size))
13004 return NULL_TREE;
13005
13006 if (! integer_all_onesp (size))
13007 {
13008 tree src_len = c_strlen (src, 1);
13009 if (src_len
13010 && tree_fits_uhwi_p (src_len)
13011 && tree_fits_uhwi_p (len)
13012 && ! tree_int_cst_lt (len, src_len))
13013 {
13014 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13015 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13016 if (!fn)
13017 return NULL_TREE;
13018
13019 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13020 }
13021 return NULL_TREE;
13022 }
13023
13024 /* If __builtin_strncat_chk is used, assume strncat is available. */
13025 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13026 if (!fn)
13027 return NULL_TREE;
13028
13029 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13030 }
13031
13032 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13033 Return NULL_TREE if a normal call should be emitted rather than
13034 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13035 or BUILT_IN_VSPRINTF_CHK. */
13036
13037 static tree
13038 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13039 enum built_in_function fcode)
13040 {
13041 tree dest, size, len, fn, fmt, flag;
13042 const char *fmt_str;
13043
13044 /* Verify the required arguments in the original call. */
13045 if (nargs < 4)
13046 return NULL_TREE;
13047 dest = args[0];
13048 if (!validate_arg (dest, POINTER_TYPE))
13049 return NULL_TREE;
13050 flag = args[1];
13051 if (!validate_arg (flag, INTEGER_TYPE))
13052 return NULL_TREE;
13053 size = args[2];
13054 if (!validate_arg (size, INTEGER_TYPE))
13055 return NULL_TREE;
13056 fmt = args[3];
13057 if (!validate_arg (fmt, POINTER_TYPE))
13058 return NULL_TREE;
13059
13060 if (! tree_fits_uhwi_p (size))
13061 return NULL_TREE;
13062
13063 len = NULL_TREE;
13064
13065 if (!init_target_chars ())
13066 return NULL_TREE;
13067
13068 /* Check whether the format is a literal string constant. */
13069 fmt_str = c_getstr (fmt);
13070 if (fmt_str != NULL)
13071 {
13072 /* If the format doesn't contain % args or %%, we know the size. */
13073 if (strchr (fmt_str, target_percent) == 0)
13074 {
13075 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13076 len = build_int_cstu (size_type_node, strlen (fmt_str));
13077 }
13078 /* If the format is "%s" and first ... argument is a string literal,
13079 we know the size too. */
13080 else if (fcode == BUILT_IN_SPRINTF_CHK
13081 && strcmp (fmt_str, target_percent_s) == 0)
13082 {
13083 tree arg;
13084
13085 if (nargs == 5)
13086 {
13087 arg = args[4];
13088 if (validate_arg (arg, POINTER_TYPE))
13089 {
13090 len = c_strlen (arg, 1);
13091 if (! len || ! tree_fits_uhwi_p (len))
13092 len = NULL_TREE;
13093 }
13094 }
13095 }
13096 }
13097
13098 if (! integer_all_onesp (size))
13099 {
13100 if (! len || ! tree_int_cst_lt (len, size))
13101 return NULL_TREE;
13102 }
13103
13104 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13105 or if format doesn't contain % chars or is "%s". */
13106 if (! integer_zerop (flag))
13107 {
13108 if (fmt_str == NULL)
13109 return NULL_TREE;
13110 if (strchr (fmt_str, target_percent) != NULL
13111 && strcmp (fmt_str, target_percent_s))
13112 return NULL_TREE;
13113 }
13114
13115 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13116 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13117 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13118 if (!fn)
13119 return NULL_TREE;
13120
13121 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13122 }
13123
13124 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13125 a normal call should be emitted rather than expanding the function
13126 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13127
13128 static tree
13129 fold_builtin_sprintf_chk (location_t loc, tree exp,
13130 enum built_in_function fcode)
13131 {
13132 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13133 CALL_EXPR_ARGP (exp), fcode);
13134 }
13135
13136 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13137 NULL_TREE if a normal call should be emitted rather than expanding
13138 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13139 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13140 passed as second argument. */
13141
13142 static tree
13143 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13144 tree maxlen, enum built_in_function fcode)
13145 {
13146 tree dest, size, len, fn, fmt, flag;
13147 const char *fmt_str;
13148
13149 /* Verify the required arguments in the original call. */
13150 if (nargs < 5)
13151 return NULL_TREE;
13152 dest = args[0];
13153 if (!validate_arg (dest, POINTER_TYPE))
13154 return NULL_TREE;
13155 len = args[1];
13156 if (!validate_arg (len, INTEGER_TYPE))
13157 return NULL_TREE;
13158 flag = args[2];
13159 if (!validate_arg (flag, INTEGER_TYPE))
13160 return NULL_TREE;
13161 size = args[3];
13162 if (!validate_arg (size, INTEGER_TYPE))
13163 return NULL_TREE;
13164 fmt = args[4];
13165 if (!validate_arg (fmt, POINTER_TYPE))
13166 return NULL_TREE;
13167
13168 if (! tree_fits_uhwi_p (size))
13169 return NULL_TREE;
13170
13171 if (! integer_all_onesp (size))
13172 {
13173 if (! tree_fits_uhwi_p (len))
13174 {
13175 /* If LEN is not constant, try MAXLEN too.
13176 For MAXLEN only allow optimizing into non-_ocs function
13177 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13178 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
13179 return NULL_TREE;
13180 }
13181 else
13182 maxlen = len;
13183
13184 if (tree_int_cst_lt (size, maxlen))
13185 return NULL_TREE;
13186 }
13187
13188 if (!init_target_chars ())
13189 return NULL_TREE;
13190
13191 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13192 or if format doesn't contain % chars or is "%s". */
13193 if (! integer_zerop (flag))
13194 {
13195 fmt_str = c_getstr (fmt);
13196 if (fmt_str == NULL)
13197 return NULL_TREE;
13198 if (strchr (fmt_str, target_percent) != NULL
13199 && strcmp (fmt_str, target_percent_s))
13200 return NULL_TREE;
13201 }
13202
13203 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13204 available. */
13205 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13206 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13207 if (!fn)
13208 return NULL_TREE;
13209
13210 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13211 }
13212
13213 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13214 a normal call should be emitted rather than expanding the function
13215 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13216 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13217 passed as second argument. */
13218
13219 static tree
13220 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13221 enum built_in_function fcode)
13222 {
13223 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13224 CALL_EXPR_ARGP (exp), maxlen, fcode);
13225 }
13226
13227 /* Builtins with folding operations that operate on "..." arguments
13228 need special handling; we need to store the arguments in a convenient
13229 data structure before attempting any folding. Fortunately there are
13230 only a few builtins that fall into this category. FNDECL is the
13231 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13232 result of the function call is ignored. */
13233
13234 static tree
13235 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13236 bool ignore ATTRIBUTE_UNUSED)
13237 {
13238 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13239 tree ret = NULL_TREE;
13240
13241 switch (fcode)
13242 {
13243 case BUILT_IN_SPRINTF_CHK:
13244 case BUILT_IN_VSPRINTF_CHK:
13245 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13246 break;
13247
13248 case BUILT_IN_SNPRINTF_CHK:
13249 case BUILT_IN_VSNPRINTF_CHK:
13250 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13251 break;
13252
13253 case BUILT_IN_FPCLASSIFY:
13254 ret = fold_builtin_fpclassify (loc, exp);
13255 break;
13256
13257 default:
13258 break;
13259 }
13260 if (ret)
13261 {
13262 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13263 SET_EXPR_LOCATION (ret, loc);
13264 TREE_NO_WARNING (ret) = 1;
13265 return ret;
13266 }
13267 return NULL_TREE;
13268 }
13269
13270 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13271 FMT and ARG are the arguments to the call; we don't fold cases with
13272 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13273
13274 Return NULL_TREE if no simplification was possible, otherwise return the
13275 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13276 code of the function to be simplified. */
13277
13278 static tree
13279 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13280 tree arg, bool ignore,
13281 enum built_in_function fcode)
13282 {
13283 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13284 const char *fmt_str = NULL;
13285
13286 /* If the return value is used, don't do the transformation. */
13287 if (! ignore)
13288 return NULL_TREE;
13289
13290 /* Verify the required arguments in the original call. */
13291 if (!validate_arg (fmt, POINTER_TYPE))
13292 return NULL_TREE;
13293
13294 /* Check whether the format is a literal string constant. */
13295 fmt_str = c_getstr (fmt);
13296 if (fmt_str == NULL)
13297 return NULL_TREE;
13298
13299 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13300 {
13301 /* If we're using an unlocked function, assume the other
13302 unlocked functions exist explicitly. */
13303 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13304 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13305 }
13306 else
13307 {
13308 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13309 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13310 }
13311
13312 if (!init_target_chars ())
13313 return NULL_TREE;
13314
13315 if (strcmp (fmt_str, target_percent_s) == 0
13316 || strchr (fmt_str, target_percent) == NULL)
13317 {
13318 const char *str;
13319
13320 if (strcmp (fmt_str, target_percent_s) == 0)
13321 {
13322 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13323 return NULL_TREE;
13324
13325 if (!arg || !validate_arg (arg, POINTER_TYPE))
13326 return NULL_TREE;
13327
13328 str = c_getstr (arg);
13329 if (str == NULL)
13330 return NULL_TREE;
13331 }
13332 else
13333 {
13334 /* The format specifier doesn't contain any '%' characters. */
13335 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13336 && arg)
13337 return NULL_TREE;
13338 str = fmt_str;
13339 }
13340
13341 /* If the string was "", printf does nothing. */
13342 if (str[0] == '\0')
13343 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13344
13345 /* If the string has length of 1, call putchar. */
13346 if (str[1] == '\0')
13347 {
13348 /* Given printf("c"), (where c is any one character,)
13349 convert "c"[0] to an int and pass that to the replacement
13350 function. */
13351 newarg = build_int_cst (integer_type_node, str[0]);
13352 if (fn_putchar)
13353 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13354 }
13355 else
13356 {
13357 /* If the string was "string\n", call puts("string"). */
13358 size_t len = strlen (str);
13359 if ((unsigned char)str[len - 1] == target_newline
13360 && (size_t) (int) len == len
13361 && (int) len > 0)
13362 {
13363 char *newstr;
13364 tree offset_node, string_cst;
13365
13366 /* Create a NUL-terminated string that's one char shorter
13367 than the original, stripping off the trailing '\n'. */
13368 newarg = build_string_literal (len, str);
13369 string_cst = string_constant (newarg, &offset_node);
13370 gcc_checking_assert (string_cst
13371 && (TREE_STRING_LENGTH (string_cst)
13372 == (int) len)
13373 && integer_zerop (offset_node)
13374 && (unsigned char)
13375 TREE_STRING_POINTER (string_cst)[len - 1]
13376 == target_newline);
13377 /* build_string_literal creates a new STRING_CST,
13378 modify it in place to avoid double copying. */
13379 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13380 newstr[len - 1] = '\0';
13381 if (fn_puts)
13382 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13383 }
13384 else
13385 /* We'd like to arrange to call fputs(string,stdout) here,
13386 but we need stdout and don't have a way to get it yet. */
13387 return NULL_TREE;
13388 }
13389 }
13390
13391 /* The other optimizations can be done only on the non-va_list variants. */
13392 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13393 return NULL_TREE;
13394
13395 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13396 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13397 {
13398 if (!arg || !validate_arg (arg, POINTER_TYPE))
13399 return NULL_TREE;
13400 if (fn_puts)
13401 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13402 }
13403
13404 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13405 else if (strcmp (fmt_str, target_percent_c) == 0)
13406 {
13407 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13408 return NULL_TREE;
13409 if (fn_putchar)
13410 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13411 }
13412
13413 if (!call)
13414 return NULL_TREE;
13415
13416 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13417 }
13418
13419 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13420 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13421 more than 3 arguments, and ARG may be null in the 2-argument case.
13422
13423 Return NULL_TREE if no simplification was possible, otherwise return the
13424 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13425 code of the function to be simplified. */
13426
13427 static tree
13428 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13429 tree fmt, tree arg, bool ignore,
13430 enum built_in_function fcode)
13431 {
13432 tree fn_fputc, fn_fputs, call = NULL_TREE;
13433 const char *fmt_str = NULL;
13434
13435 /* If the return value is used, don't do the transformation. */
13436 if (! ignore)
13437 return NULL_TREE;
13438
13439 /* Verify the required arguments in the original call. */
13440 if (!validate_arg (fp, POINTER_TYPE))
13441 return NULL_TREE;
13442 if (!validate_arg (fmt, POINTER_TYPE))
13443 return NULL_TREE;
13444
13445 /* Check whether the format is a literal string constant. */
13446 fmt_str = c_getstr (fmt);
13447 if (fmt_str == NULL)
13448 return NULL_TREE;
13449
13450 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13451 {
13452 /* If we're using an unlocked function, assume the other
13453 unlocked functions exist explicitly. */
13454 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13455 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13456 }
13457 else
13458 {
13459 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13460 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13461 }
13462
13463 if (!init_target_chars ())
13464 return NULL_TREE;
13465
13466 /* If the format doesn't contain % args or %%, use strcpy. */
13467 if (strchr (fmt_str, target_percent) == NULL)
13468 {
13469 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13470 && arg)
13471 return NULL_TREE;
13472
13473 /* If the format specifier was "", fprintf does nothing. */
13474 if (fmt_str[0] == '\0')
13475 {
13476 /* If FP has side-effects, just wait until gimplification is
13477 done. */
13478 if (TREE_SIDE_EFFECTS (fp))
13479 return NULL_TREE;
13480
13481 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13482 }
13483
13484 /* When "string" doesn't contain %, replace all cases of
13485 fprintf (fp, string) with fputs (string, fp). The fputs
13486 builtin will take care of special cases like length == 1. */
13487 if (fn_fputs)
13488 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13489 }
13490
13491 /* The other optimizations can be done only on the non-va_list variants. */
13492 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13493 return NULL_TREE;
13494
13495 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13496 else if (strcmp (fmt_str, target_percent_s) == 0)
13497 {
13498 if (!arg || !validate_arg (arg, POINTER_TYPE))
13499 return NULL_TREE;
13500 if (fn_fputs)
13501 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13502 }
13503
13504 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13505 else if (strcmp (fmt_str, target_percent_c) == 0)
13506 {
13507 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13508 return NULL_TREE;
13509 if (fn_fputc)
13510 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13511 }
13512
13513 if (!call)
13514 return NULL_TREE;
13515 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13516 }
13517
13518 /* Initialize format string characters in the target charset. */
13519
13520 static bool
13521 init_target_chars (void)
13522 {
13523 static bool init;
13524 if (!init)
13525 {
13526 target_newline = lang_hooks.to_target_charset ('\n');
13527 target_percent = lang_hooks.to_target_charset ('%');
13528 target_c = lang_hooks.to_target_charset ('c');
13529 target_s = lang_hooks.to_target_charset ('s');
13530 if (target_newline == 0 || target_percent == 0 || target_c == 0
13531 || target_s == 0)
13532 return false;
13533
13534 target_percent_c[0] = target_percent;
13535 target_percent_c[1] = target_c;
13536 target_percent_c[2] = '\0';
13537
13538 target_percent_s[0] = target_percent;
13539 target_percent_s[1] = target_s;
13540 target_percent_s[2] = '\0';
13541
13542 target_percent_s_newline[0] = target_percent;
13543 target_percent_s_newline[1] = target_s;
13544 target_percent_s_newline[2] = target_newline;
13545 target_percent_s_newline[3] = '\0';
13546
13547 init = true;
13548 }
13549 return true;
13550 }
13551
13552 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13553 and no overflow/underflow occurred. INEXACT is true if M was not
13554 exactly calculated. TYPE is the tree type for the result. This
13555 function assumes that you cleared the MPFR flags and then
13556 calculated M to see if anything subsequently set a flag prior to
13557 entering this function. Return NULL_TREE if any checks fail. */
13558
13559 static tree
13560 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13561 {
13562 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13563 overflow/underflow occurred. If -frounding-math, proceed iff the
13564 result of calling FUNC was exact. */
13565 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13566 && (!flag_rounding_math || !inexact))
13567 {
13568 REAL_VALUE_TYPE rr;
13569
13570 real_from_mpfr (&rr, m, type, GMP_RNDN);
13571 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13572 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13573 but the mpft_t is not, then we underflowed in the
13574 conversion. */
13575 if (real_isfinite (&rr)
13576 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13577 {
13578 REAL_VALUE_TYPE rmode;
13579
13580 real_convert (&rmode, TYPE_MODE (type), &rr);
13581 /* Proceed iff the specified mode can hold the value. */
13582 if (real_identical (&rmode, &rr))
13583 return build_real (type, rmode);
13584 }
13585 }
13586 return NULL_TREE;
13587 }
13588
13589 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13590 number and no overflow/underflow occurred. INEXACT is true if M
13591 was not exactly calculated. TYPE is the tree type for the result.
13592 This function assumes that you cleared the MPFR flags and then
13593 calculated M to see if anything subsequently set a flag prior to
13594 entering this function. Return NULL_TREE if any checks fail, if
13595 FORCE_CONVERT is true, then bypass the checks. */
13596
13597 static tree
13598 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13599 {
13600 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13601 overflow/underflow occurred. If -frounding-math, proceed iff the
13602 result of calling FUNC was exact. */
13603 if (force_convert
13604 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13605 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13606 && (!flag_rounding_math || !inexact)))
13607 {
13608 REAL_VALUE_TYPE re, im;
13609
13610 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13611 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13612 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13613 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13614 but the mpft_t is not, then we underflowed in the
13615 conversion. */
13616 if (force_convert
13617 || (real_isfinite (&re) && real_isfinite (&im)
13618 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13619 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13620 {
13621 REAL_VALUE_TYPE re_mode, im_mode;
13622
13623 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13624 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13625 /* Proceed iff the specified mode can hold the value. */
13626 if (force_convert
13627 || (real_identical (&re_mode, &re)
13628 && real_identical (&im_mode, &im)))
13629 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13630 build_real (TREE_TYPE (type), im_mode));
13631 }
13632 }
13633 return NULL_TREE;
13634 }
13635
13636 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13637 FUNC on it and return the resulting value as a tree with type TYPE.
13638 If MIN and/or MAX are not NULL, then the supplied ARG must be
13639 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13640 acceptable values, otherwise they are not. The mpfr precision is
13641 set to the precision of TYPE. We assume that function FUNC returns
13642 zero if the result could be calculated exactly within the requested
13643 precision. */
13644
13645 static tree
13646 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13647 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13648 bool inclusive)
13649 {
13650 tree result = NULL_TREE;
13651
13652 STRIP_NOPS (arg);
13653
13654 /* To proceed, MPFR must exactly represent the target floating point
13655 format, which only happens when the target base equals two. */
13656 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13657 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13658 {
13659 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13660
13661 if (real_isfinite (ra)
13662 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13663 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13664 {
13665 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13666 const int prec = fmt->p;
13667 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13668 int inexact;
13669 mpfr_t m;
13670
13671 mpfr_init2 (m, prec);
13672 mpfr_from_real (m, ra, GMP_RNDN);
13673 mpfr_clear_flags ();
13674 inexact = func (m, m, rnd);
13675 result = do_mpfr_ckconv (m, type, inexact);
13676 mpfr_clear (m);
13677 }
13678 }
13679
13680 return result;
13681 }
13682
13683 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13684 FUNC on it and return the resulting value as a tree with type TYPE.
13685 The mpfr precision is set to the precision of TYPE. We assume that
13686 function FUNC returns zero if the result could be calculated
13687 exactly within the requested precision. */
13688
13689 static tree
13690 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13691 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13692 {
13693 tree result = NULL_TREE;
13694
13695 STRIP_NOPS (arg1);
13696 STRIP_NOPS (arg2);
13697
13698 /* To proceed, MPFR must exactly represent the target floating point
13699 format, which only happens when the target base equals two. */
13700 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13701 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13702 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13703 {
13704 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13705 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13706
13707 if (real_isfinite (ra1) && real_isfinite (ra2))
13708 {
13709 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13710 const int prec = fmt->p;
13711 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13712 int inexact;
13713 mpfr_t m1, m2;
13714
13715 mpfr_inits2 (prec, m1, m2, NULL);
13716 mpfr_from_real (m1, ra1, GMP_RNDN);
13717 mpfr_from_real (m2, ra2, GMP_RNDN);
13718 mpfr_clear_flags ();
13719 inexact = func (m1, m1, m2, rnd);
13720 result = do_mpfr_ckconv (m1, type, inexact);
13721 mpfr_clears (m1, m2, NULL);
13722 }
13723 }
13724
13725 return result;
13726 }
13727
13728 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13729 FUNC on it and return the resulting value as a tree with type TYPE.
13730 The mpfr precision is set to the precision of TYPE. We assume that
13731 function FUNC returns zero if the result could be calculated
13732 exactly within the requested precision. */
13733
13734 static tree
13735 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13736 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13737 {
13738 tree result = NULL_TREE;
13739
13740 STRIP_NOPS (arg1);
13741 STRIP_NOPS (arg2);
13742 STRIP_NOPS (arg3);
13743
13744 /* To proceed, MPFR must exactly represent the target floating point
13745 format, which only happens when the target base equals two. */
13746 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13747 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13748 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13749 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13750 {
13751 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13752 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13753 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13754
13755 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13756 {
13757 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13758 const int prec = fmt->p;
13759 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13760 int inexact;
13761 mpfr_t m1, m2, m3;
13762
13763 mpfr_inits2 (prec, m1, m2, m3, NULL);
13764 mpfr_from_real (m1, ra1, GMP_RNDN);
13765 mpfr_from_real (m2, ra2, GMP_RNDN);
13766 mpfr_from_real (m3, ra3, GMP_RNDN);
13767 mpfr_clear_flags ();
13768 inexact = func (m1, m1, m2, m3, rnd);
13769 result = do_mpfr_ckconv (m1, type, inexact);
13770 mpfr_clears (m1, m2, m3, NULL);
13771 }
13772 }
13773
13774 return result;
13775 }
13776
13777 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13778 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13779 If ARG_SINP and ARG_COSP are NULL then the result is returned
13780 as a complex value.
13781 The type is taken from the type of ARG and is used for setting the
13782 precision of the calculation and results. */
13783
13784 static tree
13785 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13786 {
13787 tree const type = TREE_TYPE (arg);
13788 tree result = NULL_TREE;
13789
13790 STRIP_NOPS (arg);
13791
13792 /* To proceed, MPFR must exactly represent the target floating point
13793 format, which only happens when the target base equals two. */
13794 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13795 && TREE_CODE (arg) == REAL_CST
13796 && !TREE_OVERFLOW (arg))
13797 {
13798 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13799
13800 if (real_isfinite (ra))
13801 {
13802 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13803 const int prec = fmt->p;
13804 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13805 tree result_s, result_c;
13806 int inexact;
13807 mpfr_t m, ms, mc;
13808
13809 mpfr_inits2 (prec, m, ms, mc, NULL);
13810 mpfr_from_real (m, ra, GMP_RNDN);
13811 mpfr_clear_flags ();
13812 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13813 result_s = do_mpfr_ckconv (ms, type, inexact);
13814 result_c = do_mpfr_ckconv (mc, type, inexact);
13815 mpfr_clears (m, ms, mc, NULL);
13816 if (result_s && result_c)
13817 {
13818 /* If we are to return in a complex value do so. */
13819 if (!arg_sinp && !arg_cosp)
13820 return build_complex (build_complex_type (type),
13821 result_c, result_s);
13822
13823 /* Dereference the sin/cos pointer arguments. */
13824 arg_sinp = build_fold_indirect_ref (arg_sinp);
13825 arg_cosp = build_fold_indirect_ref (arg_cosp);
13826 /* Proceed if valid pointer type were passed in. */
13827 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13828 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13829 {
13830 /* Set the values. */
13831 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13832 result_s);
13833 TREE_SIDE_EFFECTS (result_s) = 1;
13834 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13835 result_c);
13836 TREE_SIDE_EFFECTS (result_c) = 1;
13837 /* Combine the assignments into a compound expr. */
13838 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13839 result_s, result_c));
13840 }
13841 }
13842 }
13843 }
13844 return result;
13845 }
13846
13847 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13848 two-argument mpfr order N Bessel function FUNC on them and return
13849 the resulting value as a tree with type TYPE. The mpfr precision
13850 is set to the precision of TYPE. We assume that function FUNC
13851 returns zero if the result could be calculated exactly within the
13852 requested precision. */
13853 static tree
13854 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13855 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13856 const REAL_VALUE_TYPE *min, bool inclusive)
13857 {
13858 tree result = NULL_TREE;
13859
13860 STRIP_NOPS (arg1);
13861 STRIP_NOPS (arg2);
13862
13863 /* To proceed, MPFR must exactly represent the target floating point
13864 format, which only happens when the target base equals two. */
13865 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13866 && tree_fits_shwi_p (arg1)
13867 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13868 {
13869 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13870 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13871
13872 if (n == (long)n
13873 && real_isfinite (ra)
13874 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13875 {
13876 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13877 const int prec = fmt->p;
13878 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13879 int inexact;
13880 mpfr_t m;
13881
13882 mpfr_init2 (m, prec);
13883 mpfr_from_real (m, ra, GMP_RNDN);
13884 mpfr_clear_flags ();
13885 inexact = func (m, n, m, rnd);
13886 result = do_mpfr_ckconv (m, type, inexact);
13887 mpfr_clear (m);
13888 }
13889 }
13890
13891 return result;
13892 }
13893
13894 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13895 the pointer *(ARG_QUO) and return the result. The type is taken
13896 from the type of ARG0 and is used for setting the precision of the
13897 calculation and results. */
13898
13899 static tree
13900 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13901 {
13902 tree const type = TREE_TYPE (arg0);
13903 tree result = NULL_TREE;
13904
13905 STRIP_NOPS (arg0);
13906 STRIP_NOPS (arg1);
13907
13908 /* To proceed, MPFR must exactly represent the target floating point
13909 format, which only happens when the target base equals two. */
13910 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13911 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13912 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13913 {
13914 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13915 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13916
13917 if (real_isfinite (ra0) && real_isfinite (ra1))
13918 {
13919 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13920 const int prec = fmt->p;
13921 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13922 tree result_rem;
13923 long integer_quo;
13924 mpfr_t m0, m1;
13925
13926 mpfr_inits2 (prec, m0, m1, NULL);
13927 mpfr_from_real (m0, ra0, GMP_RNDN);
13928 mpfr_from_real (m1, ra1, GMP_RNDN);
13929 mpfr_clear_flags ();
13930 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13931 /* Remquo is independent of the rounding mode, so pass
13932 inexact=0 to do_mpfr_ckconv(). */
13933 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13934 mpfr_clears (m0, m1, NULL);
13935 if (result_rem)
13936 {
13937 /* MPFR calculates quo in the host's long so it may
13938 return more bits in quo than the target int can hold
13939 if sizeof(host long) > sizeof(target int). This can
13940 happen even for native compilers in LP64 mode. In
13941 these cases, modulo the quo value with the largest
13942 number that the target int can hold while leaving one
13943 bit for the sign. */
13944 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13945 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13946
13947 /* Dereference the quo pointer argument. */
13948 arg_quo = build_fold_indirect_ref (arg_quo);
13949 /* Proceed iff a valid pointer type was passed in. */
13950 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13951 {
13952 /* Set the value. */
13953 tree result_quo
13954 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13955 build_int_cst (TREE_TYPE (arg_quo),
13956 integer_quo));
13957 TREE_SIDE_EFFECTS (result_quo) = 1;
13958 /* Combine the quo assignment with the rem. */
13959 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13960 result_quo, result_rem));
13961 }
13962 }
13963 }
13964 }
13965 return result;
13966 }
13967
13968 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13969 resulting value as a tree with type TYPE. The mpfr precision is
13970 set to the precision of TYPE. We assume that this mpfr function
13971 returns zero if the result could be calculated exactly within the
13972 requested precision. In addition, the integer pointer represented
13973 by ARG_SG will be dereferenced and set to the appropriate signgam
13974 (-1,1) value. */
13975
13976 static tree
13977 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13978 {
13979 tree result = NULL_TREE;
13980
13981 STRIP_NOPS (arg);
13982
13983 /* To proceed, MPFR must exactly represent the target floating point
13984 format, which only happens when the target base equals two. Also
13985 verify ARG is a constant and that ARG_SG is an int pointer. */
13986 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13987 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13988 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13989 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13990 {
13991 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13992
13993 /* In addition to NaN and Inf, the argument cannot be zero or a
13994 negative integer. */
13995 if (real_isfinite (ra)
13996 && ra->cl != rvc_zero
13997 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13998 {
13999 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14000 const int prec = fmt->p;
14001 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14002 int inexact, sg;
14003 mpfr_t m;
14004 tree result_lg;
14005
14006 mpfr_init2 (m, prec);
14007 mpfr_from_real (m, ra, GMP_RNDN);
14008 mpfr_clear_flags ();
14009 inexact = mpfr_lgamma (m, &sg, m, rnd);
14010 result_lg = do_mpfr_ckconv (m, type, inexact);
14011 mpfr_clear (m);
14012 if (result_lg)
14013 {
14014 tree result_sg;
14015
14016 /* Dereference the arg_sg pointer argument. */
14017 arg_sg = build_fold_indirect_ref (arg_sg);
14018 /* Assign the signgam value into *arg_sg. */
14019 result_sg = fold_build2 (MODIFY_EXPR,
14020 TREE_TYPE (arg_sg), arg_sg,
14021 build_int_cst (TREE_TYPE (arg_sg), sg));
14022 TREE_SIDE_EFFECTS (result_sg) = 1;
14023 /* Combine the signgam assignment with the lgamma result. */
14024 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14025 result_sg, result_lg));
14026 }
14027 }
14028 }
14029
14030 return result;
14031 }
14032
14033 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14034 function FUNC on it and return the resulting value as a tree with
14035 type TYPE. The mpfr precision is set to the precision of TYPE. We
14036 assume that function FUNC returns zero if the result could be
14037 calculated exactly within the requested precision. */
14038
14039 static tree
14040 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14041 {
14042 tree result = NULL_TREE;
14043
14044 STRIP_NOPS (arg);
14045
14046 /* To proceed, MPFR must exactly represent the target floating point
14047 format, which only happens when the target base equals two. */
14048 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14049 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14050 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14051 {
14052 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14053 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14054
14055 if (real_isfinite (re) && real_isfinite (im))
14056 {
14057 const struct real_format *const fmt =
14058 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14059 const int prec = fmt->p;
14060 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14061 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14062 int inexact;
14063 mpc_t m;
14064
14065 mpc_init2 (m, prec);
14066 mpfr_from_real (mpc_realref (m), re, rnd);
14067 mpfr_from_real (mpc_imagref (m), im, rnd);
14068 mpfr_clear_flags ();
14069 inexact = func (m, m, crnd);
14070 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14071 mpc_clear (m);
14072 }
14073 }
14074
14075 return result;
14076 }
14077
14078 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14079 mpc function FUNC on it and return the resulting value as a tree
14080 with type TYPE. The mpfr precision is set to the precision of
14081 TYPE. We assume that function FUNC returns zero if the result
14082 could be calculated exactly within the requested precision. If
14083 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14084 in the arguments and/or results. */
14085
14086 tree
14087 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14088 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14089 {
14090 tree result = NULL_TREE;
14091
14092 STRIP_NOPS (arg0);
14093 STRIP_NOPS (arg1);
14094
14095 /* To proceed, MPFR must exactly represent the target floating point
14096 format, which only happens when the target base equals two. */
14097 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14098 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14099 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14100 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14101 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14102 {
14103 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14104 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14105 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14106 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14107
14108 if (do_nonfinite
14109 || (real_isfinite (re0) && real_isfinite (im0)
14110 && real_isfinite (re1) && real_isfinite (im1)))
14111 {
14112 const struct real_format *const fmt =
14113 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14114 const int prec = fmt->p;
14115 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14116 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14117 int inexact;
14118 mpc_t m0, m1;
14119
14120 mpc_init2 (m0, prec);
14121 mpc_init2 (m1, prec);
14122 mpfr_from_real (mpc_realref (m0), re0, rnd);
14123 mpfr_from_real (mpc_imagref (m0), im0, rnd);
14124 mpfr_from_real (mpc_realref (m1), re1, rnd);
14125 mpfr_from_real (mpc_imagref (m1), im1, rnd);
14126 mpfr_clear_flags ();
14127 inexact = func (m0, m0, m1, crnd);
14128 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14129 mpc_clear (m0);
14130 mpc_clear (m1);
14131 }
14132 }
14133
14134 return result;
14135 }
14136
14137 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14138 a normal call should be emitted rather than expanding the function
14139 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14140
14141 static tree
14142 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14143 {
14144 int nargs = gimple_call_num_args (stmt);
14145
14146 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14147 (nargs > 0
14148 ? gimple_call_arg_ptr (stmt, 0)
14149 : &error_mark_node), fcode);
14150 }
14151
14152 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14153 a normal call should be emitted rather than expanding the function
14154 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14155 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14156 passed as second argument. */
14157
14158 tree
14159 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14160 enum built_in_function fcode)
14161 {
14162 int nargs = gimple_call_num_args (stmt);
14163
14164 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14165 (nargs > 0
14166 ? gimple_call_arg_ptr (stmt, 0)
14167 : &error_mark_node), maxlen, fcode);
14168 }
14169
14170 /* Builtins with folding operations that operate on "..." arguments
14171 need special handling; we need to store the arguments in a convenient
14172 data structure before attempting any folding. Fortunately there are
14173 only a few builtins that fall into this category. FNDECL is the
14174 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14175 result of the function call is ignored. */
14176
14177 static tree
14178 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14179 bool ignore ATTRIBUTE_UNUSED)
14180 {
14181 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14182 tree ret = NULL_TREE;
14183
14184 switch (fcode)
14185 {
14186 case BUILT_IN_SPRINTF_CHK:
14187 case BUILT_IN_VSPRINTF_CHK:
14188 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14189 break;
14190
14191 case BUILT_IN_SNPRINTF_CHK:
14192 case BUILT_IN_VSNPRINTF_CHK:
14193 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14194
14195 default:
14196 break;
14197 }
14198 if (ret)
14199 {
14200 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14201 TREE_NO_WARNING (ret) = 1;
14202 return ret;
14203 }
14204 return NULL_TREE;
14205 }
14206
14207 /* A wrapper function for builtin folding that prevents warnings for
14208 "statement without effect" and the like, caused by removing the
14209 call node earlier than the warning is generated. */
14210
14211 tree
14212 fold_call_stmt (gimple stmt, bool ignore)
14213 {
14214 tree ret = NULL_TREE;
14215 tree fndecl = gimple_call_fndecl (stmt);
14216 location_t loc = gimple_location (stmt);
14217 if (fndecl
14218 && TREE_CODE (fndecl) == FUNCTION_DECL
14219 && DECL_BUILT_IN (fndecl)
14220 && !gimple_call_va_arg_pack_p (stmt))
14221 {
14222 int nargs = gimple_call_num_args (stmt);
14223 tree *args = (nargs > 0
14224 ? gimple_call_arg_ptr (stmt, 0)
14225 : &error_mark_node);
14226
14227 if (avoid_folding_inline_builtin (fndecl))
14228 return NULL_TREE;
14229 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14230 {
14231 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14232 }
14233 else
14234 {
14235 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14236 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14237 if (!ret)
14238 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14239 if (ret)
14240 {
14241 /* Propagate location information from original call to
14242 expansion of builtin. Otherwise things like
14243 maybe_emit_chk_warning, that operate on the expansion
14244 of a builtin, will use the wrong location information. */
14245 if (gimple_has_location (stmt))
14246 {
14247 tree realret = ret;
14248 if (TREE_CODE (ret) == NOP_EXPR)
14249 realret = TREE_OPERAND (ret, 0);
14250 if (CAN_HAVE_LOCATION_P (realret)
14251 && !EXPR_HAS_LOCATION (realret))
14252 SET_EXPR_LOCATION (realret, loc);
14253 return realret;
14254 }
14255 return ret;
14256 }
14257 }
14258 }
14259 return NULL_TREE;
14260 }
14261
14262 /* Look up the function in builtin_decl that corresponds to DECL
14263 and set ASMSPEC as its user assembler name. DECL must be a
14264 function decl that declares a builtin. */
14265
14266 void
14267 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14268 {
14269 tree builtin;
14270 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14271 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14272 && asmspec != 0);
14273
14274 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14275 set_user_assembler_name (builtin, asmspec);
14276 switch (DECL_FUNCTION_CODE (decl))
14277 {
14278 case BUILT_IN_MEMCPY:
14279 init_block_move_fn (asmspec);
14280 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14281 break;
14282 case BUILT_IN_MEMSET:
14283 init_block_clear_fn (asmspec);
14284 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14285 break;
14286 case BUILT_IN_MEMMOVE:
14287 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14288 break;
14289 case BUILT_IN_MEMCMP:
14290 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14291 break;
14292 case BUILT_IN_ABORT:
14293 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14294 break;
14295 case BUILT_IN_FFS:
14296 if (INT_TYPE_SIZE < BITS_PER_WORD)
14297 {
14298 set_user_assembler_libfunc ("ffs", asmspec);
14299 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14300 MODE_INT, 0), "ffs");
14301 }
14302 break;
14303 default:
14304 break;
14305 }
14306 }
14307
14308 /* Return true if DECL is a builtin that expands to a constant or similarly
14309 simple code. */
14310 bool
14311 is_simple_builtin (tree decl)
14312 {
14313 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14314 switch (DECL_FUNCTION_CODE (decl))
14315 {
14316 /* Builtins that expand to constants. */
14317 case BUILT_IN_CONSTANT_P:
14318 case BUILT_IN_EXPECT:
14319 case BUILT_IN_OBJECT_SIZE:
14320 case BUILT_IN_UNREACHABLE:
14321 /* Simple register moves or loads from stack. */
14322 case BUILT_IN_ASSUME_ALIGNED:
14323 case BUILT_IN_RETURN_ADDRESS:
14324 case BUILT_IN_EXTRACT_RETURN_ADDR:
14325 case BUILT_IN_FROB_RETURN_ADDR:
14326 case BUILT_IN_RETURN:
14327 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14328 case BUILT_IN_FRAME_ADDRESS:
14329 case BUILT_IN_VA_END:
14330 case BUILT_IN_STACK_SAVE:
14331 case BUILT_IN_STACK_RESTORE:
14332 /* Exception state returns or moves registers around. */
14333 case BUILT_IN_EH_FILTER:
14334 case BUILT_IN_EH_POINTER:
14335 case BUILT_IN_EH_COPY_VALUES:
14336 return true;
14337
14338 default:
14339 return false;
14340 }
14341
14342 return false;
14343 }
14344
14345 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14346 most probably expanded inline into reasonably simple code. This is a
14347 superset of is_simple_builtin. */
14348 bool
14349 is_inexpensive_builtin (tree decl)
14350 {
14351 if (!decl)
14352 return false;
14353 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14354 return true;
14355 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14356 switch (DECL_FUNCTION_CODE (decl))
14357 {
14358 case BUILT_IN_ABS:
14359 case BUILT_IN_ALLOCA:
14360 case BUILT_IN_ALLOCA_WITH_ALIGN:
14361 case BUILT_IN_BSWAP16:
14362 case BUILT_IN_BSWAP32:
14363 case BUILT_IN_BSWAP64:
14364 case BUILT_IN_CLZ:
14365 case BUILT_IN_CLZIMAX:
14366 case BUILT_IN_CLZL:
14367 case BUILT_IN_CLZLL:
14368 case BUILT_IN_CTZ:
14369 case BUILT_IN_CTZIMAX:
14370 case BUILT_IN_CTZL:
14371 case BUILT_IN_CTZLL:
14372 case BUILT_IN_FFS:
14373 case BUILT_IN_FFSIMAX:
14374 case BUILT_IN_FFSL:
14375 case BUILT_IN_FFSLL:
14376 case BUILT_IN_IMAXABS:
14377 case BUILT_IN_FINITE:
14378 case BUILT_IN_FINITEF:
14379 case BUILT_IN_FINITEL:
14380 case BUILT_IN_FINITED32:
14381 case BUILT_IN_FINITED64:
14382 case BUILT_IN_FINITED128:
14383 case BUILT_IN_FPCLASSIFY:
14384 case BUILT_IN_ISFINITE:
14385 case BUILT_IN_ISINF_SIGN:
14386 case BUILT_IN_ISINF:
14387 case BUILT_IN_ISINFF:
14388 case BUILT_IN_ISINFL:
14389 case BUILT_IN_ISINFD32:
14390 case BUILT_IN_ISINFD64:
14391 case BUILT_IN_ISINFD128:
14392 case BUILT_IN_ISNAN:
14393 case BUILT_IN_ISNANF:
14394 case BUILT_IN_ISNANL:
14395 case BUILT_IN_ISNAND32:
14396 case BUILT_IN_ISNAND64:
14397 case BUILT_IN_ISNAND128:
14398 case BUILT_IN_ISNORMAL:
14399 case BUILT_IN_ISGREATER:
14400 case BUILT_IN_ISGREATEREQUAL:
14401 case BUILT_IN_ISLESS:
14402 case BUILT_IN_ISLESSEQUAL:
14403 case BUILT_IN_ISLESSGREATER:
14404 case BUILT_IN_ISUNORDERED:
14405 case BUILT_IN_VA_ARG_PACK:
14406 case BUILT_IN_VA_ARG_PACK_LEN:
14407 case BUILT_IN_VA_COPY:
14408 case BUILT_IN_TRAP:
14409 case BUILT_IN_SAVEREGS:
14410 case BUILT_IN_POPCOUNTL:
14411 case BUILT_IN_POPCOUNTLL:
14412 case BUILT_IN_POPCOUNTIMAX:
14413 case BUILT_IN_POPCOUNT:
14414 case BUILT_IN_PARITYL:
14415 case BUILT_IN_PARITYLL:
14416 case BUILT_IN_PARITYIMAX:
14417 case BUILT_IN_PARITY:
14418 case BUILT_IN_LABS:
14419 case BUILT_IN_LLABS:
14420 case BUILT_IN_PREFETCH:
14421 return true;
14422
14423 default:
14424 return is_simple_builtin (decl);
14425 }
14426
14427 return false;
14428 }