expr.h (expand_shift): Rename to ...
[gcc.git] / gcc / builtins.c
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
52 #include "builtins.h"
53
54
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 #endif
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
59
60 struct target_builtins default_target_builtins;
61 #if SWITCHABLE_TARGET
62 struct target_builtins *this_target_builtins = &default_target_builtins;
63 #endif
64
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
68
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
71 {
72 #include "builtins.def"
73 };
74 #undef DEF_BUILTIN
75
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 tree built_in_decls[(int) END_BUILTINS];
79 /* Declarations used when constructing the builtin implicitly in the compiler.
80 It may be NULL_TREE when this is invalid (for instance runtime is not
81 required to implement the function call in all cases). */
82 tree implicit_built_in_decls[(int) END_BUILTINS];
83
84 static const char *c_getstr (tree);
85 static rtx c_readstr (const char *, enum machine_mode);
86 static int target_char_cast (tree, char *);
87 static rtx get_memory_rtx (tree, tree);
88 static int apply_args_size (void);
89 static int apply_result_size (void);
90 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
91 static rtx result_vector (int, rtx);
92 #endif
93 static void expand_builtin_update_setjmp_buf (rtx);
94 static void expand_builtin_prefetch (tree);
95 static rtx expand_builtin_apply_args (void);
96 static rtx expand_builtin_apply_args_1 (void);
97 static rtx expand_builtin_apply (rtx, rtx, rtx);
98 static void expand_builtin_return (rtx);
99 static enum type_class type_to_class (tree);
100 static rtx expand_builtin_classify_type (tree);
101 static void expand_errno_check (tree, rtx);
102 static rtx expand_builtin_mathfn (tree, rtx, rtx);
103 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
104 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
105 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
106 static rtx expand_builtin_interclass_mathfn (tree, rtx);
107 static rtx expand_builtin_sincos (tree);
108 static rtx expand_builtin_cexpi (tree, rtx);
109 static rtx expand_builtin_int_roundingfn (tree, rtx);
110 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
111 static rtx expand_builtin_next_arg (void);
112 static rtx expand_builtin_va_start (tree);
113 static rtx expand_builtin_va_end (tree);
114 static rtx expand_builtin_va_copy (tree);
115 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_strcmp (tree, rtx);
117 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
118 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_memcpy (tree, rtx);
120 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
122 enum machine_mode, int);
123 static rtx expand_builtin_strcpy (tree, rtx);
124 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
125 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strncpy (tree, rtx);
127 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
128 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
130 static rtx expand_builtin_bzero (tree);
131 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_alloca (tree, bool);
133 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
134 static rtx expand_builtin_frame_address (tree, tree);
135 static tree stabilize_va_list_loc (location_t, tree, int);
136 static rtx expand_builtin_expect (tree, rtx);
137 static tree fold_builtin_constant_p (tree);
138 static tree fold_builtin_expect (location_t, tree, tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (location_t, tree, tree);
141 static tree fold_builtin_inf (location_t, tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
144 static bool validate_arg (const_tree, enum tree_code code);
145 static bool integer_valued_real_p (tree);
146 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
147 static bool readonly_data_expr (tree);
148 static rtx expand_builtin_fabs (tree, rtx, rtx);
149 static rtx expand_builtin_signbit (tree, rtx);
150 static tree fold_builtin_sqrt (location_t, tree, tree);
151 static tree fold_builtin_cbrt (location_t, tree, tree);
152 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
153 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
154 static tree fold_builtin_cos (location_t, tree, tree, tree);
155 static tree fold_builtin_cosh (location_t, tree, tree, tree);
156 static tree fold_builtin_tan (tree, tree);
157 static tree fold_builtin_trunc (location_t, tree, tree);
158 static tree fold_builtin_floor (location_t, tree, tree);
159 static tree fold_builtin_ceil (location_t, tree, tree);
160 static tree fold_builtin_round (location_t, tree, tree);
161 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
162 static tree fold_builtin_bitop (tree, tree);
163 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
164 static tree fold_builtin_strchr (location_t, tree, tree, tree);
165 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
166 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
167 static tree fold_builtin_strcmp (location_t, tree, tree);
168 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
169 static tree fold_builtin_signbit (location_t, tree, tree);
170 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_isascii (location_t, tree);
172 static tree fold_builtin_toascii (location_t, tree);
173 static tree fold_builtin_isdigit (location_t, tree);
174 static tree fold_builtin_fabs (location_t, tree, tree);
175 static tree fold_builtin_abs (location_t, tree, tree);
176 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
177 enum tree_code);
178 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
179 static tree fold_builtin_0 (location_t, tree, bool);
180 static tree fold_builtin_1 (location_t, tree, tree, bool);
181 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
182 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
183 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
184 static tree fold_builtin_varargs (location_t, tree, tree, bool);
185
186 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
187 static tree fold_builtin_strstr (location_t, tree, tree, tree);
188 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
189 static tree fold_builtin_strcat (location_t, tree, tree);
190 static tree fold_builtin_strncat (location_t, tree, tree, tree);
191 static tree fold_builtin_strspn (location_t, tree, tree);
192 static tree fold_builtin_strcspn (location_t, tree, tree);
193 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
194 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
195
196 static rtx expand_builtin_object_size (tree);
197 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
198 enum built_in_function);
199 static void maybe_emit_chk_warning (tree, enum built_in_function);
200 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
201 static void maybe_emit_free_warning (tree);
202 static tree fold_builtin_object_size (tree, tree);
203 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
204 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
205 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
206 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
207 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
208 enum built_in_function);
209 static bool init_target_chars (void);
210
211 static unsigned HOST_WIDE_INT target_newline;
212 static unsigned HOST_WIDE_INT target_percent;
213 static unsigned HOST_WIDE_INT target_c;
214 static unsigned HOST_WIDE_INT target_s;
215 static char target_percent_c[3];
216 static char target_percent_s[3];
217 static char target_percent_s_newline[4];
218 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
219 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
220 static tree do_mpfr_arg2 (tree, tree, tree,
221 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
222 static tree do_mpfr_arg3 (tree, tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_sincos (tree, tree, tree);
225 static tree do_mpfr_bessel_n (tree, tree, tree,
226 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
227 const REAL_VALUE_TYPE *, bool);
228 static tree do_mpfr_remquo (tree, tree, tree);
229 static tree do_mpfr_lgamma_r (tree, tree, tree);
230
231 /* Return true if NAME starts with __builtin_ or __sync_. */
232
233 bool
234 is_builtin_name (const char *name)
235 {
236 if (strncmp (name, "__builtin_", 10) == 0)
237 return true;
238 if (strncmp (name, "__sync_", 7) == 0)
239 return true;
240 return false;
241 }
242
243
244 /* Return true if DECL is a function symbol representing a built-in. */
245
246 bool
247 is_builtin_fn (tree decl)
248 {
249 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
250 }
251
252
253 /* Return true if NODE should be considered for inline expansion regardless
254 of the optimization level. This means whenever a function is invoked with
255 its "internal" name, which normally contains the prefix "__builtin". */
256
257 static bool
258 called_as_built_in (tree node)
259 {
260 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
261 we want the name used to call the function, not the name it
262 will have. */
263 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
264 return is_builtin_name (name);
265 }
266
267 /* Return the alignment in bits of EXP, an object.
268 Don't return more than MAX_ALIGN no matter what. */
269
270 unsigned int
271 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
272 {
273 HOST_WIDE_INT bitsize, bitpos;
274 tree offset;
275 enum machine_mode mode;
276 int unsignedp, volatilep;
277 unsigned int align, inner;
278
279 /* Get the innermost object and the constant (bitpos) and possibly
280 variable (offset) offset of the access. */
281 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
282 &mode, &unsignedp, &volatilep, true);
283
284 /* Extract alignment information from the innermost object and
285 possibly adjust bitpos and offset. */
286 if (TREE_CODE (exp) == CONST_DECL)
287 exp = DECL_INITIAL (exp);
288 if (DECL_P (exp)
289 && TREE_CODE (exp) != LABEL_DECL)
290 align = DECL_ALIGN (exp);
291 else if (CONSTANT_CLASS_P (exp))
292 {
293 align = TYPE_ALIGN (TREE_TYPE (exp));
294 #ifdef CONSTANT_ALIGNMENT
295 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
296 #endif
297 }
298 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
299 align = TYPE_ALIGN (TREE_TYPE (exp));
300 else if (TREE_CODE (exp) == INDIRECT_REF)
301 align = TYPE_ALIGN (TREE_TYPE (exp));
302 else if (TREE_CODE (exp) == MEM_REF)
303 {
304 tree addr = TREE_OPERAND (exp, 0);
305 struct ptr_info_def *pi;
306 if (TREE_CODE (addr) == BIT_AND_EXPR
307 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
308 {
309 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
310 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
311 align *= BITS_PER_UNIT;
312 addr = TREE_OPERAND (addr, 0);
313 }
314 else
315 align = BITS_PER_UNIT;
316 if (TREE_CODE (addr) == SSA_NAME
317 && (pi = SSA_NAME_PTR_INFO (addr)))
318 {
319 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
320 align = MAX (pi->align * BITS_PER_UNIT, align);
321 }
322 else if (TREE_CODE (addr) == ADDR_EXPR)
323 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0), ~0U));
324 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
325 }
326 else if (TREE_CODE (exp) == TARGET_MEM_REF)
327 {
328 struct ptr_info_def *pi;
329 tree addr = TMR_BASE (exp);
330 if (TREE_CODE (addr) == BIT_AND_EXPR
331 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
332 {
333 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
334 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
335 align *= BITS_PER_UNIT;
336 addr = TREE_OPERAND (addr, 0);
337 }
338 else
339 align = BITS_PER_UNIT;
340 if (TREE_CODE (addr) == SSA_NAME
341 && (pi = SSA_NAME_PTR_INFO (addr)))
342 {
343 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
344 align = MAX (pi->align * BITS_PER_UNIT, align);
345 }
346 else if (TREE_CODE (addr) == ADDR_EXPR)
347 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0), ~0U));
348 if (TMR_OFFSET (exp))
349 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
350 if (TMR_INDEX (exp) && TMR_STEP (exp))
351 {
352 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
353 align = MIN (align, (step & -step) * BITS_PER_UNIT);
354 }
355 else if (TMR_INDEX (exp))
356 align = BITS_PER_UNIT;
357 if (TMR_INDEX2 (exp))
358 align = BITS_PER_UNIT;
359 }
360 else
361 align = BITS_PER_UNIT;
362
363 /* If there is a non-constant offset part extract the maximum
364 alignment that can prevail. */
365 inner = ~0U;
366 while (offset)
367 {
368 tree next_offset;
369
370 if (TREE_CODE (offset) == PLUS_EXPR)
371 {
372 next_offset = TREE_OPERAND (offset, 0);
373 offset = TREE_OPERAND (offset, 1);
374 }
375 else
376 next_offset = NULL;
377 if (host_integerp (offset, 1))
378 {
379 /* Any overflow in calculating offset_bits won't change
380 the alignment. */
381 unsigned offset_bits
382 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
383
384 if (offset_bits)
385 inner = MIN (inner, (offset_bits & -offset_bits));
386 }
387 else if (TREE_CODE (offset) == MULT_EXPR
388 && host_integerp (TREE_OPERAND (offset, 1), 1))
389 {
390 /* Any overflow in calculating offset_factor won't change
391 the alignment. */
392 unsigned offset_factor
393 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
394 * BITS_PER_UNIT);
395
396 if (offset_factor)
397 inner = MIN (inner, (offset_factor & -offset_factor));
398 }
399 else
400 {
401 inner = MIN (inner, BITS_PER_UNIT);
402 break;
403 }
404 offset = next_offset;
405 }
406
407 /* Alignment is innermost object alignment adjusted by the constant
408 and non-constant offset parts. */
409 align = MIN (align, inner);
410 bitpos = bitpos & (align - 1);
411
412 *bitposp = bitpos;
413 return align;
414 }
415
416 /* Return the alignment in bits of EXP, an object.
417 Don't return more than MAX_ALIGN no matter what. */
418
419 unsigned int
420 get_object_alignment (tree exp, unsigned int max_align)
421 {
422 unsigned HOST_WIDE_INT bitpos = 0;
423 unsigned int align;
424
425 align = get_object_alignment_1 (exp, &bitpos);
426
427 /* align and bitpos now specify known low bits of the pointer.
428 ptr & (align - 1) == bitpos. */
429
430 if (bitpos != 0)
431 align = (bitpos & -bitpos);
432
433 return MIN (align, max_align);
434 }
435
436 /* Returns true iff we can trust that alignment information has been
437 calculated properly. */
438
439 bool
440 can_trust_pointer_alignment (void)
441 {
442 /* We rely on TER to compute accurate alignment information. */
443 return (optimize && flag_tree_ter);
444 }
445
446 /* Return the alignment in bits of EXP, a pointer valued expression.
447 But don't return more than MAX_ALIGN no matter what.
448 The alignment returned is, by default, the alignment of the thing that
449 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
450
451 Otherwise, look at the expression to see if we can do better, i.e., if the
452 expression is actually pointing at an object whose alignment is tighter. */
453
454 unsigned int
455 get_pointer_alignment (tree exp, unsigned int max_align)
456 {
457 STRIP_NOPS (exp);
458
459 if (TREE_CODE (exp) == ADDR_EXPR)
460 return get_object_alignment (TREE_OPERAND (exp, 0), max_align);
461 else if (TREE_CODE (exp) == SSA_NAME
462 && POINTER_TYPE_P (TREE_TYPE (exp)))
463 {
464 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
465 unsigned align;
466 if (!pi)
467 return BITS_PER_UNIT;
468 if (pi->misalign != 0)
469 align = (pi->misalign & -pi->misalign);
470 else
471 align = pi->align;
472 return MIN (max_align, align * BITS_PER_UNIT);
473 }
474
475 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
476 }
477
478 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
479 way, because it could contain a zero byte in the middle.
480 TREE_STRING_LENGTH is the size of the character array, not the string.
481
482 ONLY_VALUE should be nonzero if the result is not going to be emitted
483 into the instruction stream and zero if it is going to be expanded.
484 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
485 is returned, otherwise NULL, since
486 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
487 evaluate the side-effects.
488
489 The value returned is of type `ssizetype'.
490
491 Unfortunately, string_constant can't access the values of const char
492 arrays with initializers, so neither can we do so here. */
493
494 tree
495 c_strlen (tree src, int only_value)
496 {
497 tree offset_node;
498 HOST_WIDE_INT offset;
499 int max;
500 const char *ptr;
501 location_t loc;
502
503 STRIP_NOPS (src);
504 if (TREE_CODE (src) == COND_EXPR
505 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
506 {
507 tree len1, len2;
508
509 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
510 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
511 if (tree_int_cst_equal (len1, len2))
512 return len1;
513 }
514
515 if (TREE_CODE (src) == COMPOUND_EXPR
516 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
517 return c_strlen (TREE_OPERAND (src, 1), only_value);
518
519 loc = EXPR_LOC_OR_HERE (src);
520
521 src = string_constant (src, &offset_node);
522 if (src == 0)
523 return NULL_TREE;
524
525 max = TREE_STRING_LENGTH (src) - 1;
526 ptr = TREE_STRING_POINTER (src);
527
528 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
529 {
530 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
531 compute the offset to the following null if we don't know where to
532 start searching for it. */
533 int i;
534
535 for (i = 0; i < max; i++)
536 if (ptr[i] == 0)
537 return NULL_TREE;
538
539 /* We don't know the starting offset, but we do know that the string
540 has no internal zero bytes. We can assume that the offset falls
541 within the bounds of the string; otherwise, the programmer deserves
542 what he gets. Subtract the offset from the length of the string,
543 and return that. This would perhaps not be valid if we were dealing
544 with named arrays in addition to literal string constants. */
545
546 return size_diffop_loc (loc, size_int (max), offset_node);
547 }
548
549 /* We have a known offset into the string. Start searching there for
550 a null character if we can represent it as a single HOST_WIDE_INT. */
551 if (offset_node == 0)
552 offset = 0;
553 else if (! host_integerp (offset_node, 0))
554 offset = -1;
555 else
556 offset = tree_low_cst (offset_node, 0);
557
558 /* If the offset is known to be out of bounds, warn, and call strlen at
559 runtime. */
560 if (offset < 0 || offset > max)
561 {
562 /* Suppress multiple warnings for propagated constant strings. */
563 if (! TREE_NO_WARNING (src))
564 {
565 warning_at (loc, 0, "offset outside bounds of constant string");
566 TREE_NO_WARNING (src) = 1;
567 }
568 return NULL_TREE;
569 }
570
571 /* Use strlen to search for the first zero byte. Since any strings
572 constructed with build_string will have nulls appended, we win even
573 if we get handed something like (char[4])"abcd".
574
575 Since OFFSET is our starting index into the string, no further
576 calculation is needed. */
577 return ssize_int (strlen (ptr + offset));
578 }
579
580 /* Return a char pointer for a C string if it is a string constant
581 or sum of string constant and integer constant. */
582
583 static const char *
584 c_getstr (tree src)
585 {
586 tree offset_node;
587
588 src = string_constant (src, &offset_node);
589 if (src == 0)
590 return 0;
591
592 if (offset_node == 0)
593 return TREE_STRING_POINTER (src);
594 else if (!host_integerp (offset_node, 1)
595 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
596 return 0;
597
598 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
599 }
600
601 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
602 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
603
604 static rtx
605 c_readstr (const char *str, enum machine_mode mode)
606 {
607 HOST_WIDE_INT c[2];
608 HOST_WIDE_INT ch;
609 unsigned int i, j;
610
611 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
612
613 c[0] = 0;
614 c[1] = 0;
615 ch = 1;
616 for (i = 0; i < GET_MODE_SIZE (mode); i++)
617 {
618 j = i;
619 if (WORDS_BIG_ENDIAN)
620 j = GET_MODE_SIZE (mode) - i - 1;
621 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
622 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
623 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
624 j *= BITS_PER_UNIT;
625 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
626
627 if (ch)
628 ch = (unsigned char) str[i];
629 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
630 }
631 return immed_double_const (c[0], c[1], mode);
632 }
633
634 /* Cast a target constant CST to target CHAR and if that value fits into
635 host char type, return zero and put that value into variable pointed to by
636 P. */
637
638 static int
639 target_char_cast (tree cst, char *p)
640 {
641 unsigned HOST_WIDE_INT val, hostval;
642
643 if (TREE_CODE (cst) != INTEGER_CST
644 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
645 return 1;
646
647 val = TREE_INT_CST_LOW (cst);
648 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
649 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
650
651 hostval = val;
652 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
653 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
654
655 if (val != hostval)
656 return 1;
657
658 *p = hostval;
659 return 0;
660 }
661
662 /* Similar to save_expr, but assumes that arbitrary code is not executed
663 in between the multiple evaluations. In particular, we assume that a
664 non-addressable local variable will not be modified. */
665
666 static tree
667 builtin_save_expr (tree exp)
668 {
669 if (TREE_CODE (exp) == SSA_NAME
670 || (TREE_ADDRESSABLE (exp) == 0
671 && (TREE_CODE (exp) == PARM_DECL
672 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
673 return exp;
674
675 return save_expr (exp);
676 }
677
678 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
679 times to get the address of either a higher stack frame, or a return
680 address located within it (depending on FNDECL_CODE). */
681
682 static rtx
683 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
684 {
685 int i;
686
687 #ifdef INITIAL_FRAME_ADDRESS_RTX
688 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
689 #else
690 rtx tem;
691
692 /* For a zero count with __builtin_return_address, we don't care what
693 frame address we return, because target-specific definitions will
694 override us. Therefore frame pointer elimination is OK, and using
695 the soft frame pointer is OK.
696
697 For a nonzero count, or a zero count with __builtin_frame_address,
698 we require a stable offset from the current frame pointer to the
699 previous one, so we must use the hard frame pointer, and
700 we must disable frame pointer elimination. */
701 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
702 tem = frame_pointer_rtx;
703 else
704 {
705 tem = hard_frame_pointer_rtx;
706
707 /* Tell reload not to eliminate the frame pointer. */
708 crtl->accesses_prior_frames = 1;
709 }
710 #endif
711
712 /* Some machines need special handling before we can access
713 arbitrary frames. For example, on the SPARC, we must first flush
714 all register windows to the stack. */
715 #ifdef SETUP_FRAME_ADDRESSES
716 if (count > 0)
717 SETUP_FRAME_ADDRESSES ();
718 #endif
719
720 /* On the SPARC, the return address is not in the frame, it is in a
721 register. There is no way to access it off of the current frame
722 pointer, but it can be accessed off the previous frame pointer by
723 reading the value from the register window save area. */
724 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
725 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
726 count--;
727 #endif
728
729 /* Scan back COUNT frames to the specified frame. */
730 for (i = 0; i < count; i++)
731 {
732 /* Assume the dynamic chain pointer is in the word that the
733 frame address points to, unless otherwise specified. */
734 #ifdef DYNAMIC_CHAIN_ADDRESS
735 tem = DYNAMIC_CHAIN_ADDRESS (tem);
736 #endif
737 tem = memory_address (Pmode, tem);
738 tem = gen_frame_mem (Pmode, tem);
739 tem = copy_to_reg (tem);
740 }
741
742 /* For __builtin_frame_address, return what we've got. But, on
743 the SPARC for example, we may have to add a bias. */
744 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
745 #ifdef FRAME_ADDR_RTX
746 return FRAME_ADDR_RTX (tem);
747 #else
748 return tem;
749 #endif
750
751 /* For __builtin_return_address, get the return address from that frame. */
752 #ifdef RETURN_ADDR_RTX
753 tem = RETURN_ADDR_RTX (count, tem);
754 #else
755 tem = memory_address (Pmode,
756 plus_constant (tem, GET_MODE_SIZE (Pmode)));
757 tem = gen_frame_mem (Pmode, tem);
758 #endif
759 return tem;
760 }
761
762 /* Alias set used for setjmp buffer. */
763 static alias_set_type setjmp_alias_set = -1;
764
765 /* Construct the leading half of a __builtin_setjmp call. Control will
766 return to RECEIVER_LABEL. This is also called directly by the SJLJ
767 exception handling code. */
768
769 void
770 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
771 {
772 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
773 rtx stack_save;
774 rtx mem;
775
776 if (setjmp_alias_set == -1)
777 setjmp_alias_set = new_alias_set ();
778
779 buf_addr = convert_memory_address (Pmode, buf_addr);
780
781 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
782
783 /* We store the frame pointer and the address of receiver_label in
784 the buffer and use the rest of it for the stack save area, which
785 is machine-dependent. */
786
787 mem = gen_rtx_MEM (Pmode, buf_addr);
788 set_mem_alias_set (mem, setjmp_alias_set);
789 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
790
791 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
792 set_mem_alias_set (mem, setjmp_alias_set);
793
794 emit_move_insn (validize_mem (mem),
795 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
796
797 stack_save = gen_rtx_MEM (sa_mode,
798 plus_constant (buf_addr,
799 2 * GET_MODE_SIZE (Pmode)));
800 set_mem_alias_set (stack_save, setjmp_alias_set);
801 emit_stack_save (SAVE_NONLOCAL, &stack_save);
802
803 /* If there is further processing to do, do it. */
804 #ifdef HAVE_builtin_setjmp_setup
805 if (HAVE_builtin_setjmp_setup)
806 emit_insn (gen_builtin_setjmp_setup (buf_addr));
807 #endif
808
809 /* Tell optimize_save_area_alloca that extra work is going to
810 need to go on during alloca. */
811 cfun->calls_setjmp = 1;
812
813 /* We have a nonlocal label. */
814 cfun->has_nonlocal_label = 1;
815 }
816
817 /* Construct the trailing part of a __builtin_setjmp call. This is
818 also called directly by the SJLJ exception handling code. */
819
820 void
821 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
822 {
823 rtx chain;
824
825 /* Clobber the FP when we get here, so we have to make sure it's
826 marked as used by this function. */
827 emit_use (hard_frame_pointer_rtx);
828
829 /* Mark the static chain as clobbered here so life information
830 doesn't get messed up for it. */
831 chain = targetm.calls.static_chain (current_function_decl, true);
832 if (chain && REG_P (chain))
833 emit_clobber (chain);
834
835 /* Now put in the code to restore the frame pointer, and argument
836 pointer, if needed. */
837 #ifdef HAVE_nonlocal_goto
838 if (! HAVE_nonlocal_goto)
839 #endif
840 {
841 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
842 /* This might change the hard frame pointer in ways that aren't
843 apparent to early optimization passes, so force a clobber. */
844 emit_clobber (hard_frame_pointer_rtx);
845 }
846
847 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
848 if (fixed_regs[ARG_POINTER_REGNUM])
849 {
850 #ifdef ELIMINABLE_REGS
851 size_t i;
852 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
853
854 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
855 if (elim_regs[i].from == ARG_POINTER_REGNUM
856 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
857 break;
858
859 if (i == ARRAY_SIZE (elim_regs))
860 #endif
861 {
862 /* Now restore our arg pointer from the address at which it
863 was saved in our stack frame. */
864 emit_move_insn (crtl->args.internal_arg_pointer,
865 copy_to_reg (get_arg_pointer_save_area ()));
866 }
867 }
868 #endif
869
870 #ifdef HAVE_builtin_setjmp_receiver
871 if (HAVE_builtin_setjmp_receiver)
872 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
873 else
874 #endif
875 #ifdef HAVE_nonlocal_goto_receiver
876 if (HAVE_nonlocal_goto_receiver)
877 emit_insn (gen_nonlocal_goto_receiver ());
878 else
879 #endif
880 { /* Nothing */ }
881
882 /* We must not allow the code we just generated to be reordered by
883 scheduling. Specifically, the update of the frame pointer must
884 happen immediately, not later. */
885 emit_insn (gen_blockage ());
886 }
887
888 /* __builtin_longjmp is passed a pointer to an array of five words (not
889 all will be used on all machines). It operates similarly to the C
890 library function of the same name, but is more efficient. Much of
891 the code below is copied from the handling of non-local gotos. */
892
893 static void
894 expand_builtin_longjmp (rtx buf_addr, rtx value)
895 {
896 rtx fp, lab, stack, insn, last;
897 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
898
899 /* DRAP is needed for stack realign if longjmp is expanded to current
900 function */
901 if (SUPPORTS_STACK_ALIGNMENT)
902 crtl->need_drap = true;
903
904 if (setjmp_alias_set == -1)
905 setjmp_alias_set = new_alias_set ();
906
907 buf_addr = convert_memory_address (Pmode, buf_addr);
908
909 buf_addr = force_reg (Pmode, buf_addr);
910
911 /* We require that the user must pass a second argument of 1, because
912 that is what builtin_setjmp will return. */
913 gcc_assert (value == const1_rtx);
914
915 last = get_last_insn ();
916 #ifdef HAVE_builtin_longjmp
917 if (HAVE_builtin_longjmp)
918 emit_insn (gen_builtin_longjmp (buf_addr));
919 else
920 #endif
921 {
922 fp = gen_rtx_MEM (Pmode, buf_addr);
923 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
924 GET_MODE_SIZE (Pmode)));
925
926 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
927 2 * GET_MODE_SIZE (Pmode)));
928 set_mem_alias_set (fp, setjmp_alias_set);
929 set_mem_alias_set (lab, setjmp_alias_set);
930 set_mem_alias_set (stack, setjmp_alias_set);
931
932 /* Pick up FP, label, and SP from the block and jump. This code is
933 from expand_goto in stmt.c; see there for detailed comments. */
934 #ifdef HAVE_nonlocal_goto
935 if (HAVE_nonlocal_goto)
936 /* We have to pass a value to the nonlocal_goto pattern that will
937 get copied into the static_chain pointer, but it does not matter
938 what that value is, because builtin_setjmp does not use it. */
939 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
940 else
941 #endif
942 {
943 lab = copy_to_reg (lab);
944
945 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
946 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
947
948 emit_move_insn (hard_frame_pointer_rtx, fp);
949 emit_stack_restore (SAVE_NONLOCAL, stack);
950
951 emit_use (hard_frame_pointer_rtx);
952 emit_use (stack_pointer_rtx);
953 emit_indirect_jump (lab);
954 }
955 }
956
957 /* Search backwards and mark the jump insn as a non-local goto.
958 Note that this precludes the use of __builtin_longjmp to a
959 __builtin_setjmp target in the same function. However, we've
960 already cautioned the user that these functions are for
961 internal exception handling use only. */
962 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
963 {
964 gcc_assert (insn != last);
965
966 if (JUMP_P (insn))
967 {
968 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
969 break;
970 }
971 else if (CALL_P (insn))
972 break;
973 }
974 }
975
976 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
977 and the address of the save area. */
978
979 static rtx
980 expand_builtin_nonlocal_goto (tree exp)
981 {
982 tree t_label, t_save_area;
983 rtx r_label, r_save_area, r_fp, r_sp, insn;
984
985 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
986 return NULL_RTX;
987
988 t_label = CALL_EXPR_ARG (exp, 0);
989 t_save_area = CALL_EXPR_ARG (exp, 1);
990
991 r_label = expand_normal (t_label);
992 r_label = convert_memory_address (Pmode, r_label);
993 r_save_area = expand_normal (t_save_area);
994 r_save_area = convert_memory_address (Pmode, r_save_area);
995 /* Copy the address of the save location to a register just in case it was based
996 on the frame pointer. */
997 r_save_area = copy_to_reg (r_save_area);
998 r_fp = gen_rtx_MEM (Pmode, r_save_area);
999 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1000 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1001
1002 crtl->has_nonlocal_goto = 1;
1003
1004 #ifdef HAVE_nonlocal_goto
1005 /* ??? We no longer need to pass the static chain value, afaik. */
1006 if (HAVE_nonlocal_goto)
1007 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1008 else
1009 #endif
1010 {
1011 r_label = copy_to_reg (r_label);
1012
1013 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1014 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1015
1016 /* Restore frame pointer for containing function.
1017 This sets the actual hard register used for the frame pointer
1018 to the location of the function's incoming static chain info.
1019 The non-local goto handler will then adjust it to contain the
1020 proper value and reload the argument pointer, if needed. */
1021 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1022 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1023
1024 /* USE of hard_frame_pointer_rtx added for consistency;
1025 not clear if really needed. */
1026 emit_use (hard_frame_pointer_rtx);
1027 emit_use (stack_pointer_rtx);
1028
1029 /* If the architecture is using a GP register, we must
1030 conservatively assume that the target function makes use of it.
1031 The prologue of functions with nonlocal gotos must therefore
1032 initialize the GP register to the appropriate value, and we
1033 must then make sure that this value is live at the point
1034 of the jump. (Note that this doesn't necessarily apply
1035 to targets with a nonlocal_goto pattern; they are free
1036 to implement it in their own way. Note also that this is
1037 a no-op if the GP register is a global invariant.) */
1038 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1039 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1040 emit_use (pic_offset_table_rtx);
1041
1042 emit_indirect_jump (r_label);
1043 }
1044
1045 /* Search backwards to the jump insn and mark it as a
1046 non-local goto. */
1047 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1048 {
1049 if (JUMP_P (insn))
1050 {
1051 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1052 break;
1053 }
1054 else if (CALL_P (insn))
1055 break;
1056 }
1057
1058 return const0_rtx;
1059 }
1060
1061 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1062 (not all will be used on all machines) that was passed to __builtin_setjmp.
1063 It updates the stack pointer in that block to correspond to the current
1064 stack pointer. */
1065
1066 static void
1067 expand_builtin_update_setjmp_buf (rtx buf_addr)
1068 {
1069 enum machine_mode sa_mode = Pmode;
1070 rtx stack_save;
1071
1072
1073 #ifdef HAVE_save_stack_nonlocal
1074 if (HAVE_save_stack_nonlocal)
1075 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1076 #endif
1077 #ifdef STACK_SAVEAREA_MODE
1078 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1079 #endif
1080
1081 stack_save
1082 = gen_rtx_MEM (sa_mode,
1083 memory_address
1084 (sa_mode,
1085 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1086
1087 #ifdef HAVE_setjmp
1088 if (HAVE_setjmp)
1089 emit_insn (gen_setjmp ());
1090 #endif
1091
1092 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1093 }
1094
1095 /* Expand a call to __builtin_prefetch. For a target that does not support
1096 data prefetch, evaluate the memory address argument in case it has side
1097 effects. */
1098
1099 static void
1100 expand_builtin_prefetch (tree exp)
1101 {
1102 tree arg0, arg1, arg2;
1103 int nargs;
1104 rtx op0, op1, op2;
1105
1106 if (!validate_arglist (exp, POINTER_TYPE, 0))
1107 return;
1108
1109 arg0 = CALL_EXPR_ARG (exp, 0);
1110
1111 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1112 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1113 locality). */
1114 nargs = call_expr_nargs (exp);
1115 if (nargs > 1)
1116 arg1 = CALL_EXPR_ARG (exp, 1);
1117 else
1118 arg1 = integer_zero_node;
1119 if (nargs > 2)
1120 arg2 = CALL_EXPR_ARG (exp, 2);
1121 else
1122 arg2 = integer_three_node;
1123
1124 /* Argument 0 is an address. */
1125 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1126
1127 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1128 if (TREE_CODE (arg1) != INTEGER_CST)
1129 {
1130 error ("second argument to %<__builtin_prefetch%> must be a constant");
1131 arg1 = integer_zero_node;
1132 }
1133 op1 = expand_normal (arg1);
1134 /* Argument 1 must be either zero or one. */
1135 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1136 {
1137 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1138 " using zero");
1139 op1 = const0_rtx;
1140 }
1141
1142 /* Argument 2 (locality) must be a compile-time constant int. */
1143 if (TREE_CODE (arg2) != INTEGER_CST)
1144 {
1145 error ("third argument to %<__builtin_prefetch%> must be a constant");
1146 arg2 = integer_zero_node;
1147 }
1148 op2 = expand_normal (arg2);
1149 /* Argument 2 must be 0, 1, 2, or 3. */
1150 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1151 {
1152 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1153 op2 = const0_rtx;
1154 }
1155
1156 #ifdef HAVE_prefetch
1157 if (HAVE_prefetch)
1158 {
1159 struct expand_operand ops[3];
1160
1161 create_address_operand (&ops[0], op0);
1162 create_integer_operand (&ops[1], INTVAL (op1));
1163 create_integer_operand (&ops[2], INTVAL (op2));
1164 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1165 return;
1166 }
1167 #endif
1168
1169 /* Don't do anything with direct references to volatile memory, but
1170 generate code to handle other side effects. */
1171 if (!MEM_P (op0) && side_effects_p (op0))
1172 emit_insn (op0);
1173 }
1174
1175 /* Get a MEM rtx for expression EXP which is the address of an operand
1176 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1177 the maximum length of the block of memory that might be accessed or
1178 NULL if unknown. */
1179
1180 static rtx
1181 get_memory_rtx (tree exp, tree len)
1182 {
1183 tree orig_exp = exp;
1184 rtx addr, mem;
1185 HOST_WIDE_INT off;
1186
1187 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1188 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1189 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1190 exp = TREE_OPERAND (exp, 0);
1191
1192 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1193 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1194
1195 /* Get an expression we can use to find the attributes to assign to MEM.
1196 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1197 we can. First remove any nops. */
1198 while (CONVERT_EXPR_P (exp)
1199 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1200 exp = TREE_OPERAND (exp, 0);
1201
1202 off = 0;
1203 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1204 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1205 && host_integerp (TREE_OPERAND (exp, 1), 0)
1206 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1207 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1208 else if (TREE_CODE (exp) == ADDR_EXPR)
1209 exp = TREE_OPERAND (exp, 0);
1210 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1211 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1212 else
1213 exp = NULL;
1214
1215 /* Honor attributes derived from exp, except for the alias set
1216 (as builtin stringops may alias with anything) and the size
1217 (as stringops may access multiple array elements). */
1218 if (exp)
1219 {
1220 set_mem_attributes (mem, exp, 0);
1221
1222 if (off)
1223 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1224
1225 /* Allow the string and memory builtins to overflow from one
1226 field into another, see http://gcc.gnu.org/PR23561.
1227 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1228 memory accessed by the string or memory builtin will fit
1229 within the field. */
1230 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1231 {
1232 tree mem_expr = MEM_EXPR (mem);
1233 HOST_WIDE_INT offset = -1, length = -1;
1234 tree inner = exp;
1235
1236 while (TREE_CODE (inner) == ARRAY_REF
1237 || CONVERT_EXPR_P (inner)
1238 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1239 || TREE_CODE (inner) == SAVE_EXPR)
1240 inner = TREE_OPERAND (inner, 0);
1241
1242 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1243
1244 if (MEM_OFFSET (mem)
1245 && CONST_INT_P (MEM_OFFSET (mem)))
1246 offset = INTVAL (MEM_OFFSET (mem));
1247
1248 if (offset >= 0 && len && host_integerp (len, 0))
1249 length = tree_low_cst (len, 0);
1250
1251 while (TREE_CODE (inner) == COMPONENT_REF)
1252 {
1253 tree field = TREE_OPERAND (inner, 1);
1254 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1255 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1256
1257 /* Bitfields are generally not byte-addressable. */
1258 gcc_assert (!DECL_BIT_FIELD (field)
1259 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1260 % BITS_PER_UNIT) == 0
1261 && host_integerp (DECL_SIZE (field), 0)
1262 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1263 % BITS_PER_UNIT) == 0));
1264
1265 /* If we can prove that the memory starting at XEXP (mem, 0) and
1266 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1267 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1268 fields without DECL_SIZE_UNIT like flexible array members. */
1269 if (length >= 0
1270 && DECL_SIZE_UNIT (field)
1271 && host_integerp (DECL_SIZE_UNIT (field), 0))
1272 {
1273 HOST_WIDE_INT size
1274 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1275 if (offset <= size
1276 && length <= size
1277 && offset + length <= size)
1278 break;
1279 }
1280
1281 if (offset >= 0
1282 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1283 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1284 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1285 / BITS_PER_UNIT;
1286 else
1287 {
1288 offset = -1;
1289 length = -1;
1290 }
1291
1292 mem_expr = TREE_OPERAND (mem_expr, 0);
1293 inner = TREE_OPERAND (inner, 0);
1294 }
1295
1296 if (mem_expr == NULL)
1297 offset = -1;
1298 if (mem_expr != MEM_EXPR (mem))
1299 {
1300 set_mem_expr (mem, mem_expr);
1301 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1302 }
1303 }
1304 set_mem_alias_set (mem, 0);
1305 set_mem_size (mem, NULL_RTX);
1306 }
1307
1308 return mem;
1309 }
1310 \f
1311 /* Built-in functions to perform an untyped call and return. */
1312
1313 #define apply_args_mode \
1314 (this_target_builtins->x_apply_args_mode)
1315 #define apply_result_mode \
1316 (this_target_builtins->x_apply_result_mode)
1317
1318 /* Return the size required for the block returned by __builtin_apply_args,
1319 and initialize apply_args_mode. */
1320
1321 static int
1322 apply_args_size (void)
1323 {
1324 static int size = -1;
1325 int align;
1326 unsigned int regno;
1327 enum machine_mode mode;
1328
1329 /* The values computed by this function never change. */
1330 if (size < 0)
1331 {
1332 /* The first value is the incoming arg-pointer. */
1333 size = GET_MODE_SIZE (Pmode);
1334
1335 /* The second value is the structure value address unless this is
1336 passed as an "invisible" first argument. */
1337 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1338 size += GET_MODE_SIZE (Pmode);
1339
1340 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1341 if (FUNCTION_ARG_REGNO_P (regno))
1342 {
1343 mode = targetm.calls.get_raw_arg_mode (regno);
1344
1345 gcc_assert (mode != VOIDmode);
1346
1347 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1348 if (size % align != 0)
1349 size = CEIL (size, align) * align;
1350 size += GET_MODE_SIZE (mode);
1351 apply_args_mode[regno] = mode;
1352 }
1353 else
1354 {
1355 apply_args_mode[regno] = VOIDmode;
1356 }
1357 }
1358 return size;
1359 }
1360
1361 /* Return the size required for the block returned by __builtin_apply,
1362 and initialize apply_result_mode. */
1363
1364 static int
1365 apply_result_size (void)
1366 {
1367 static int size = -1;
1368 int align, regno;
1369 enum machine_mode mode;
1370
1371 /* The values computed by this function never change. */
1372 if (size < 0)
1373 {
1374 size = 0;
1375
1376 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1377 if (targetm.calls.function_value_regno_p (regno))
1378 {
1379 mode = targetm.calls.get_raw_result_mode (regno);
1380
1381 gcc_assert (mode != VOIDmode);
1382
1383 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1384 if (size % align != 0)
1385 size = CEIL (size, align) * align;
1386 size += GET_MODE_SIZE (mode);
1387 apply_result_mode[regno] = mode;
1388 }
1389 else
1390 apply_result_mode[regno] = VOIDmode;
1391
1392 /* Allow targets that use untyped_call and untyped_return to override
1393 the size so that machine-specific information can be stored here. */
1394 #ifdef APPLY_RESULT_SIZE
1395 size = APPLY_RESULT_SIZE;
1396 #endif
1397 }
1398 return size;
1399 }
1400
1401 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1402 /* Create a vector describing the result block RESULT. If SAVEP is true,
1403 the result block is used to save the values; otherwise it is used to
1404 restore the values. */
1405
1406 static rtx
1407 result_vector (int savep, rtx result)
1408 {
1409 int regno, size, align, nelts;
1410 enum machine_mode mode;
1411 rtx reg, mem;
1412 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1413
1414 size = nelts = 0;
1415 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1416 if ((mode = apply_result_mode[regno]) != VOIDmode)
1417 {
1418 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1419 if (size % align != 0)
1420 size = CEIL (size, align) * align;
1421 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1422 mem = adjust_address (result, mode, size);
1423 savevec[nelts++] = (savep
1424 ? gen_rtx_SET (VOIDmode, mem, reg)
1425 : gen_rtx_SET (VOIDmode, reg, mem));
1426 size += GET_MODE_SIZE (mode);
1427 }
1428 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1429 }
1430 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1431
1432 /* Save the state required to perform an untyped call with the same
1433 arguments as were passed to the current function. */
1434
1435 static rtx
1436 expand_builtin_apply_args_1 (void)
1437 {
1438 rtx registers, tem;
1439 int size, align, regno;
1440 enum machine_mode mode;
1441 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1442
1443 /* Create a block where the arg-pointer, structure value address,
1444 and argument registers can be saved. */
1445 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1446
1447 /* Walk past the arg-pointer and structure value address. */
1448 size = GET_MODE_SIZE (Pmode);
1449 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1450 size += GET_MODE_SIZE (Pmode);
1451
1452 /* Save each register used in calling a function to the block. */
1453 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1454 if ((mode = apply_args_mode[regno]) != VOIDmode)
1455 {
1456 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1457 if (size % align != 0)
1458 size = CEIL (size, align) * align;
1459
1460 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1461
1462 emit_move_insn (adjust_address (registers, mode, size), tem);
1463 size += GET_MODE_SIZE (mode);
1464 }
1465
1466 /* Save the arg pointer to the block. */
1467 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1468 #ifdef STACK_GROWS_DOWNWARD
1469 /* We need the pointer as the caller actually passed them to us, not
1470 as we might have pretended they were passed. Make sure it's a valid
1471 operand, as emit_move_insn isn't expected to handle a PLUS. */
1472 tem
1473 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1474 NULL_RTX);
1475 #endif
1476 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1477
1478 size = GET_MODE_SIZE (Pmode);
1479
1480 /* Save the structure value address unless this is passed as an
1481 "invisible" first argument. */
1482 if (struct_incoming_value)
1483 {
1484 emit_move_insn (adjust_address (registers, Pmode, size),
1485 copy_to_reg (struct_incoming_value));
1486 size += GET_MODE_SIZE (Pmode);
1487 }
1488
1489 /* Return the address of the block. */
1490 return copy_addr_to_reg (XEXP (registers, 0));
1491 }
1492
1493 /* __builtin_apply_args returns block of memory allocated on
1494 the stack into which is stored the arg pointer, structure
1495 value address, static chain, and all the registers that might
1496 possibly be used in performing a function call. The code is
1497 moved to the start of the function so the incoming values are
1498 saved. */
1499
1500 static rtx
1501 expand_builtin_apply_args (void)
1502 {
1503 /* Don't do __builtin_apply_args more than once in a function.
1504 Save the result of the first call and reuse it. */
1505 if (apply_args_value != 0)
1506 return apply_args_value;
1507 {
1508 /* When this function is called, it means that registers must be
1509 saved on entry to this function. So we migrate the
1510 call to the first insn of this function. */
1511 rtx temp;
1512 rtx seq;
1513
1514 start_sequence ();
1515 temp = expand_builtin_apply_args_1 ();
1516 seq = get_insns ();
1517 end_sequence ();
1518
1519 apply_args_value = temp;
1520
1521 /* Put the insns after the NOTE that starts the function.
1522 If this is inside a start_sequence, make the outer-level insn
1523 chain current, so the code is placed at the start of the
1524 function. If internal_arg_pointer is a non-virtual pseudo,
1525 it needs to be placed after the function that initializes
1526 that pseudo. */
1527 push_topmost_sequence ();
1528 if (REG_P (crtl->args.internal_arg_pointer)
1529 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1530 emit_insn_before (seq, parm_birth_insn);
1531 else
1532 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1533 pop_topmost_sequence ();
1534 return temp;
1535 }
1536 }
1537
1538 /* Perform an untyped call and save the state required to perform an
1539 untyped return of whatever value was returned by the given function. */
1540
1541 static rtx
1542 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1543 {
1544 int size, align, regno;
1545 enum machine_mode mode;
1546 rtx incoming_args, result, reg, dest, src, call_insn;
1547 rtx old_stack_level = 0;
1548 rtx call_fusage = 0;
1549 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1550
1551 arguments = convert_memory_address (Pmode, arguments);
1552
1553 /* Create a block where the return registers can be saved. */
1554 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1555
1556 /* Fetch the arg pointer from the ARGUMENTS block. */
1557 incoming_args = gen_reg_rtx (Pmode);
1558 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1559 #ifndef STACK_GROWS_DOWNWARD
1560 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1561 incoming_args, 0, OPTAB_LIB_WIDEN);
1562 #endif
1563
1564 /* Push a new argument block and copy the arguments. Do not allow
1565 the (potential) memcpy call below to interfere with our stack
1566 manipulations. */
1567 do_pending_stack_adjust ();
1568 NO_DEFER_POP;
1569
1570 /* Save the stack with nonlocal if available. */
1571 #ifdef HAVE_save_stack_nonlocal
1572 if (HAVE_save_stack_nonlocal)
1573 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1574 else
1575 #endif
1576 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1577
1578 /* Allocate a block of memory onto the stack and copy the memory
1579 arguments to the outgoing arguments address. We can pass TRUE
1580 as the 4th argument because we just saved the stack pointer
1581 and will restore it right after the call. */
1582 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1583
1584 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1585 may have already set current_function_calls_alloca to true.
1586 current_function_calls_alloca won't be set if argsize is zero,
1587 so we have to guarantee need_drap is true here. */
1588 if (SUPPORTS_STACK_ALIGNMENT)
1589 crtl->need_drap = true;
1590
1591 dest = virtual_outgoing_args_rtx;
1592 #ifndef STACK_GROWS_DOWNWARD
1593 if (CONST_INT_P (argsize))
1594 dest = plus_constant (dest, -INTVAL (argsize));
1595 else
1596 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1597 #endif
1598 dest = gen_rtx_MEM (BLKmode, dest);
1599 set_mem_align (dest, PARM_BOUNDARY);
1600 src = gen_rtx_MEM (BLKmode, incoming_args);
1601 set_mem_align (src, PARM_BOUNDARY);
1602 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1603
1604 /* Refer to the argument block. */
1605 apply_args_size ();
1606 arguments = gen_rtx_MEM (BLKmode, arguments);
1607 set_mem_align (arguments, PARM_BOUNDARY);
1608
1609 /* Walk past the arg-pointer and structure value address. */
1610 size = GET_MODE_SIZE (Pmode);
1611 if (struct_value)
1612 size += GET_MODE_SIZE (Pmode);
1613
1614 /* Restore each of the registers previously saved. Make USE insns
1615 for each of these registers for use in making the call. */
1616 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1617 if ((mode = apply_args_mode[regno]) != VOIDmode)
1618 {
1619 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1620 if (size % align != 0)
1621 size = CEIL (size, align) * align;
1622 reg = gen_rtx_REG (mode, regno);
1623 emit_move_insn (reg, adjust_address (arguments, mode, size));
1624 use_reg (&call_fusage, reg);
1625 size += GET_MODE_SIZE (mode);
1626 }
1627
1628 /* Restore the structure value address unless this is passed as an
1629 "invisible" first argument. */
1630 size = GET_MODE_SIZE (Pmode);
1631 if (struct_value)
1632 {
1633 rtx value = gen_reg_rtx (Pmode);
1634 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1635 emit_move_insn (struct_value, value);
1636 if (REG_P (struct_value))
1637 use_reg (&call_fusage, struct_value);
1638 size += GET_MODE_SIZE (Pmode);
1639 }
1640
1641 /* All arguments and registers used for the call are set up by now! */
1642 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1643
1644 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1645 and we don't want to load it into a register as an optimization,
1646 because prepare_call_address already did it if it should be done. */
1647 if (GET_CODE (function) != SYMBOL_REF)
1648 function = memory_address (FUNCTION_MODE, function);
1649
1650 /* Generate the actual call instruction and save the return value. */
1651 #ifdef HAVE_untyped_call
1652 if (HAVE_untyped_call)
1653 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1654 result, result_vector (1, result)));
1655 else
1656 #endif
1657 #ifdef HAVE_call_value
1658 if (HAVE_call_value)
1659 {
1660 rtx valreg = 0;
1661
1662 /* Locate the unique return register. It is not possible to
1663 express a call that sets more than one return register using
1664 call_value; use untyped_call for that. In fact, untyped_call
1665 only needs to save the return registers in the given block. */
1666 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1667 if ((mode = apply_result_mode[regno]) != VOIDmode)
1668 {
1669 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1670
1671 valreg = gen_rtx_REG (mode, regno);
1672 }
1673
1674 emit_call_insn (GEN_CALL_VALUE (valreg,
1675 gen_rtx_MEM (FUNCTION_MODE, function),
1676 const0_rtx, NULL_RTX, const0_rtx));
1677
1678 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1679 }
1680 else
1681 #endif
1682 gcc_unreachable ();
1683
1684 /* Find the CALL insn we just emitted, and attach the register usage
1685 information. */
1686 call_insn = last_call_insn ();
1687 add_function_usage_to (call_insn, call_fusage);
1688
1689 /* Restore the stack. */
1690 #ifdef HAVE_save_stack_nonlocal
1691 if (HAVE_save_stack_nonlocal)
1692 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1693 else
1694 #endif
1695 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1696
1697 OK_DEFER_POP;
1698
1699 /* Return the address of the result block. */
1700 result = copy_addr_to_reg (XEXP (result, 0));
1701 return convert_memory_address (ptr_mode, result);
1702 }
1703
1704 /* Perform an untyped return. */
1705
1706 static void
1707 expand_builtin_return (rtx result)
1708 {
1709 int size, align, regno;
1710 enum machine_mode mode;
1711 rtx reg;
1712 rtx call_fusage = 0;
1713
1714 result = convert_memory_address (Pmode, result);
1715
1716 apply_result_size ();
1717 result = gen_rtx_MEM (BLKmode, result);
1718
1719 #ifdef HAVE_untyped_return
1720 if (HAVE_untyped_return)
1721 {
1722 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1723 emit_barrier ();
1724 return;
1725 }
1726 #endif
1727
1728 /* Restore the return value and note that each value is used. */
1729 size = 0;
1730 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1731 if ((mode = apply_result_mode[regno]) != VOIDmode)
1732 {
1733 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1734 if (size % align != 0)
1735 size = CEIL (size, align) * align;
1736 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1737 emit_move_insn (reg, adjust_address (result, mode, size));
1738
1739 push_to_sequence (call_fusage);
1740 emit_use (reg);
1741 call_fusage = get_insns ();
1742 end_sequence ();
1743 size += GET_MODE_SIZE (mode);
1744 }
1745
1746 /* Put the USE insns before the return. */
1747 emit_insn (call_fusage);
1748
1749 /* Return whatever values was restored by jumping directly to the end
1750 of the function. */
1751 expand_naked_return ();
1752 }
1753
1754 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1755
1756 static enum type_class
1757 type_to_class (tree type)
1758 {
1759 switch (TREE_CODE (type))
1760 {
1761 case VOID_TYPE: return void_type_class;
1762 case INTEGER_TYPE: return integer_type_class;
1763 case ENUMERAL_TYPE: return enumeral_type_class;
1764 case BOOLEAN_TYPE: return boolean_type_class;
1765 case POINTER_TYPE: return pointer_type_class;
1766 case REFERENCE_TYPE: return reference_type_class;
1767 case OFFSET_TYPE: return offset_type_class;
1768 case REAL_TYPE: return real_type_class;
1769 case COMPLEX_TYPE: return complex_type_class;
1770 case FUNCTION_TYPE: return function_type_class;
1771 case METHOD_TYPE: return method_type_class;
1772 case RECORD_TYPE: return record_type_class;
1773 case UNION_TYPE:
1774 case QUAL_UNION_TYPE: return union_type_class;
1775 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1776 ? string_type_class : array_type_class);
1777 case LANG_TYPE: return lang_type_class;
1778 default: return no_type_class;
1779 }
1780 }
1781
1782 /* Expand a call EXP to __builtin_classify_type. */
1783
1784 static rtx
1785 expand_builtin_classify_type (tree exp)
1786 {
1787 if (call_expr_nargs (exp))
1788 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1789 return GEN_INT (no_type_class);
1790 }
1791
1792 /* This helper macro, meant to be used in mathfn_built_in below,
1793 determines which among a set of three builtin math functions is
1794 appropriate for a given type mode. The `F' and `L' cases are
1795 automatically generated from the `double' case. */
1796 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1797 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1798 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1799 fcodel = BUILT_IN_MATHFN##L ; break;
1800 /* Similar to above, but appends _R after any F/L suffix. */
1801 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1802 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1803 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1804 fcodel = BUILT_IN_MATHFN##L_R ; break;
1805
1806 /* Return mathematic function equivalent to FN but operating directly
1807 on TYPE, if available. If IMPLICIT is true find the function in
1808 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1809 can't do the conversion, return zero. */
1810
1811 static tree
1812 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1813 {
1814 tree const *const fn_arr
1815 = implicit ? implicit_built_in_decls : built_in_decls;
1816 enum built_in_function fcode, fcodef, fcodel;
1817
1818 switch (fn)
1819 {
1820 CASE_MATHFN (BUILT_IN_ACOS)
1821 CASE_MATHFN (BUILT_IN_ACOSH)
1822 CASE_MATHFN (BUILT_IN_ASIN)
1823 CASE_MATHFN (BUILT_IN_ASINH)
1824 CASE_MATHFN (BUILT_IN_ATAN)
1825 CASE_MATHFN (BUILT_IN_ATAN2)
1826 CASE_MATHFN (BUILT_IN_ATANH)
1827 CASE_MATHFN (BUILT_IN_CBRT)
1828 CASE_MATHFN (BUILT_IN_CEIL)
1829 CASE_MATHFN (BUILT_IN_CEXPI)
1830 CASE_MATHFN (BUILT_IN_COPYSIGN)
1831 CASE_MATHFN (BUILT_IN_COS)
1832 CASE_MATHFN (BUILT_IN_COSH)
1833 CASE_MATHFN (BUILT_IN_DREM)
1834 CASE_MATHFN (BUILT_IN_ERF)
1835 CASE_MATHFN (BUILT_IN_ERFC)
1836 CASE_MATHFN (BUILT_IN_EXP)
1837 CASE_MATHFN (BUILT_IN_EXP10)
1838 CASE_MATHFN (BUILT_IN_EXP2)
1839 CASE_MATHFN (BUILT_IN_EXPM1)
1840 CASE_MATHFN (BUILT_IN_FABS)
1841 CASE_MATHFN (BUILT_IN_FDIM)
1842 CASE_MATHFN (BUILT_IN_FLOOR)
1843 CASE_MATHFN (BUILT_IN_FMA)
1844 CASE_MATHFN (BUILT_IN_FMAX)
1845 CASE_MATHFN (BUILT_IN_FMIN)
1846 CASE_MATHFN (BUILT_IN_FMOD)
1847 CASE_MATHFN (BUILT_IN_FREXP)
1848 CASE_MATHFN (BUILT_IN_GAMMA)
1849 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1850 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1851 CASE_MATHFN (BUILT_IN_HYPOT)
1852 CASE_MATHFN (BUILT_IN_ILOGB)
1853 CASE_MATHFN (BUILT_IN_INF)
1854 CASE_MATHFN (BUILT_IN_ISINF)
1855 CASE_MATHFN (BUILT_IN_J0)
1856 CASE_MATHFN (BUILT_IN_J1)
1857 CASE_MATHFN (BUILT_IN_JN)
1858 CASE_MATHFN (BUILT_IN_LCEIL)
1859 CASE_MATHFN (BUILT_IN_LDEXP)
1860 CASE_MATHFN (BUILT_IN_LFLOOR)
1861 CASE_MATHFN (BUILT_IN_LGAMMA)
1862 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1863 CASE_MATHFN (BUILT_IN_LLCEIL)
1864 CASE_MATHFN (BUILT_IN_LLFLOOR)
1865 CASE_MATHFN (BUILT_IN_LLRINT)
1866 CASE_MATHFN (BUILT_IN_LLROUND)
1867 CASE_MATHFN (BUILT_IN_LOG)
1868 CASE_MATHFN (BUILT_IN_LOG10)
1869 CASE_MATHFN (BUILT_IN_LOG1P)
1870 CASE_MATHFN (BUILT_IN_LOG2)
1871 CASE_MATHFN (BUILT_IN_LOGB)
1872 CASE_MATHFN (BUILT_IN_LRINT)
1873 CASE_MATHFN (BUILT_IN_LROUND)
1874 CASE_MATHFN (BUILT_IN_MODF)
1875 CASE_MATHFN (BUILT_IN_NAN)
1876 CASE_MATHFN (BUILT_IN_NANS)
1877 CASE_MATHFN (BUILT_IN_NEARBYINT)
1878 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1879 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1880 CASE_MATHFN (BUILT_IN_POW)
1881 CASE_MATHFN (BUILT_IN_POWI)
1882 CASE_MATHFN (BUILT_IN_POW10)
1883 CASE_MATHFN (BUILT_IN_REMAINDER)
1884 CASE_MATHFN (BUILT_IN_REMQUO)
1885 CASE_MATHFN (BUILT_IN_RINT)
1886 CASE_MATHFN (BUILT_IN_ROUND)
1887 CASE_MATHFN (BUILT_IN_SCALB)
1888 CASE_MATHFN (BUILT_IN_SCALBLN)
1889 CASE_MATHFN (BUILT_IN_SCALBN)
1890 CASE_MATHFN (BUILT_IN_SIGNBIT)
1891 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1892 CASE_MATHFN (BUILT_IN_SIN)
1893 CASE_MATHFN (BUILT_IN_SINCOS)
1894 CASE_MATHFN (BUILT_IN_SINH)
1895 CASE_MATHFN (BUILT_IN_SQRT)
1896 CASE_MATHFN (BUILT_IN_TAN)
1897 CASE_MATHFN (BUILT_IN_TANH)
1898 CASE_MATHFN (BUILT_IN_TGAMMA)
1899 CASE_MATHFN (BUILT_IN_TRUNC)
1900 CASE_MATHFN (BUILT_IN_Y0)
1901 CASE_MATHFN (BUILT_IN_Y1)
1902 CASE_MATHFN (BUILT_IN_YN)
1903
1904 default:
1905 return NULL_TREE;
1906 }
1907
1908 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1909 return fn_arr[fcode];
1910 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1911 return fn_arr[fcodef];
1912 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1913 return fn_arr[fcodel];
1914 else
1915 return NULL_TREE;
1916 }
1917
1918 /* Like mathfn_built_in_1(), but always use the implicit array. */
1919
1920 tree
1921 mathfn_built_in (tree type, enum built_in_function fn)
1922 {
1923 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1924 }
1925
1926 /* If errno must be maintained, expand the RTL to check if the result,
1927 TARGET, of a built-in function call, EXP, is NaN, and if so set
1928 errno to EDOM. */
1929
1930 static void
1931 expand_errno_check (tree exp, rtx target)
1932 {
1933 rtx lab = gen_label_rtx ();
1934
1935 /* Test the result; if it is NaN, set errno=EDOM because
1936 the argument was not in the domain. */
1937 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1938 NULL_RTX, NULL_RTX, lab,
1939 /* The jump is very likely. */
1940 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1941
1942 #ifdef TARGET_EDOM
1943 /* If this built-in doesn't throw an exception, set errno directly. */
1944 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1945 {
1946 #ifdef GEN_ERRNO_RTX
1947 rtx errno_rtx = GEN_ERRNO_RTX;
1948 #else
1949 rtx errno_rtx
1950 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1951 #endif
1952 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1953 emit_label (lab);
1954 return;
1955 }
1956 #endif
1957
1958 /* Make sure the library call isn't expanded as a tail call. */
1959 CALL_EXPR_TAILCALL (exp) = 0;
1960
1961 /* We can't set errno=EDOM directly; let the library call do it.
1962 Pop the arguments right away in case the call gets deleted. */
1963 NO_DEFER_POP;
1964 expand_call (exp, target, 0);
1965 OK_DEFER_POP;
1966 emit_label (lab);
1967 }
1968
1969 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1970 Return NULL_RTX if a normal call should be emitted rather than expanding
1971 the function in-line. EXP is the expression that is a call to the builtin
1972 function; if convenient, the result should be placed in TARGET.
1973 SUBTARGET may be used as the target for computing one of EXP's operands. */
1974
1975 static rtx
1976 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1977 {
1978 optab builtin_optab;
1979 rtx op0, insns;
1980 tree fndecl = get_callee_fndecl (exp);
1981 enum machine_mode mode;
1982 bool errno_set = false;
1983 tree arg;
1984
1985 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1986 return NULL_RTX;
1987
1988 arg = CALL_EXPR_ARG (exp, 0);
1989
1990 switch (DECL_FUNCTION_CODE (fndecl))
1991 {
1992 CASE_FLT_FN (BUILT_IN_SQRT):
1993 errno_set = ! tree_expr_nonnegative_p (arg);
1994 builtin_optab = sqrt_optab;
1995 break;
1996 CASE_FLT_FN (BUILT_IN_EXP):
1997 errno_set = true; builtin_optab = exp_optab; break;
1998 CASE_FLT_FN (BUILT_IN_EXP10):
1999 CASE_FLT_FN (BUILT_IN_POW10):
2000 errno_set = true; builtin_optab = exp10_optab; break;
2001 CASE_FLT_FN (BUILT_IN_EXP2):
2002 errno_set = true; builtin_optab = exp2_optab; break;
2003 CASE_FLT_FN (BUILT_IN_EXPM1):
2004 errno_set = true; builtin_optab = expm1_optab; break;
2005 CASE_FLT_FN (BUILT_IN_LOGB):
2006 errno_set = true; builtin_optab = logb_optab; break;
2007 CASE_FLT_FN (BUILT_IN_LOG):
2008 errno_set = true; builtin_optab = log_optab; break;
2009 CASE_FLT_FN (BUILT_IN_LOG10):
2010 errno_set = true; builtin_optab = log10_optab; break;
2011 CASE_FLT_FN (BUILT_IN_LOG2):
2012 errno_set = true; builtin_optab = log2_optab; break;
2013 CASE_FLT_FN (BUILT_IN_LOG1P):
2014 errno_set = true; builtin_optab = log1p_optab; break;
2015 CASE_FLT_FN (BUILT_IN_ASIN):
2016 builtin_optab = asin_optab; break;
2017 CASE_FLT_FN (BUILT_IN_ACOS):
2018 builtin_optab = acos_optab; break;
2019 CASE_FLT_FN (BUILT_IN_TAN):
2020 builtin_optab = tan_optab; break;
2021 CASE_FLT_FN (BUILT_IN_ATAN):
2022 builtin_optab = atan_optab; break;
2023 CASE_FLT_FN (BUILT_IN_FLOOR):
2024 builtin_optab = floor_optab; break;
2025 CASE_FLT_FN (BUILT_IN_CEIL):
2026 builtin_optab = ceil_optab; break;
2027 CASE_FLT_FN (BUILT_IN_TRUNC):
2028 builtin_optab = btrunc_optab; break;
2029 CASE_FLT_FN (BUILT_IN_ROUND):
2030 builtin_optab = round_optab; break;
2031 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2032 builtin_optab = nearbyint_optab;
2033 if (flag_trapping_math)
2034 break;
2035 /* Else fallthrough and expand as rint. */
2036 CASE_FLT_FN (BUILT_IN_RINT):
2037 builtin_optab = rint_optab; break;
2038 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2039 builtin_optab = significand_optab; break;
2040 default:
2041 gcc_unreachable ();
2042 }
2043
2044 /* Make a suitable register to place result in. */
2045 mode = TYPE_MODE (TREE_TYPE (exp));
2046
2047 if (! flag_errno_math || ! HONOR_NANS (mode))
2048 errno_set = false;
2049
2050 /* Before working hard, check whether the instruction is available. */
2051 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2052 && (!errno_set || !optimize_insn_for_size_p ()))
2053 {
2054 target = gen_reg_rtx (mode);
2055
2056 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2057 need to expand the argument again. This way, we will not perform
2058 side-effects more the once. */
2059 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2060
2061 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2062
2063 start_sequence ();
2064
2065 /* Compute into TARGET.
2066 Set TARGET to wherever the result comes back. */
2067 target = expand_unop (mode, builtin_optab, op0, target, 0);
2068
2069 if (target != 0)
2070 {
2071 if (errno_set)
2072 expand_errno_check (exp, target);
2073
2074 /* Output the entire sequence. */
2075 insns = get_insns ();
2076 end_sequence ();
2077 emit_insn (insns);
2078 return target;
2079 }
2080
2081 /* If we were unable to expand via the builtin, stop the sequence
2082 (without outputting the insns) and call to the library function
2083 with the stabilized argument list. */
2084 end_sequence ();
2085 }
2086
2087 return expand_call (exp, target, target == const0_rtx);
2088 }
2089
2090 /* Expand a call to the builtin binary math functions (pow and atan2).
2091 Return NULL_RTX if a normal call should be emitted rather than expanding the
2092 function in-line. EXP is the expression that is a call to the builtin
2093 function; if convenient, the result should be placed in TARGET.
2094 SUBTARGET may be used as the target for computing one of EXP's
2095 operands. */
2096
2097 static rtx
2098 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2099 {
2100 optab builtin_optab;
2101 rtx op0, op1, insns;
2102 int op1_type = REAL_TYPE;
2103 tree fndecl = get_callee_fndecl (exp);
2104 tree arg0, arg1;
2105 enum machine_mode mode;
2106 bool errno_set = true;
2107
2108 switch (DECL_FUNCTION_CODE (fndecl))
2109 {
2110 CASE_FLT_FN (BUILT_IN_SCALBN):
2111 CASE_FLT_FN (BUILT_IN_SCALBLN):
2112 CASE_FLT_FN (BUILT_IN_LDEXP):
2113 op1_type = INTEGER_TYPE;
2114 default:
2115 break;
2116 }
2117
2118 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2119 return NULL_RTX;
2120
2121 arg0 = CALL_EXPR_ARG (exp, 0);
2122 arg1 = CALL_EXPR_ARG (exp, 1);
2123
2124 switch (DECL_FUNCTION_CODE (fndecl))
2125 {
2126 CASE_FLT_FN (BUILT_IN_POW):
2127 builtin_optab = pow_optab; break;
2128 CASE_FLT_FN (BUILT_IN_ATAN2):
2129 builtin_optab = atan2_optab; break;
2130 CASE_FLT_FN (BUILT_IN_SCALB):
2131 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2132 return 0;
2133 builtin_optab = scalb_optab; break;
2134 CASE_FLT_FN (BUILT_IN_SCALBN):
2135 CASE_FLT_FN (BUILT_IN_SCALBLN):
2136 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2137 return 0;
2138 /* Fall through... */
2139 CASE_FLT_FN (BUILT_IN_LDEXP):
2140 builtin_optab = ldexp_optab; break;
2141 CASE_FLT_FN (BUILT_IN_FMOD):
2142 builtin_optab = fmod_optab; break;
2143 CASE_FLT_FN (BUILT_IN_REMAINDER):
2144 CASE_FLT_FN (BUILT_IN_DREM):
2145 builtin_optab = remainder_optab; break;
2146 default:
2147 gcc_unreachable ();
2148 }
2149
2150 /* Make a suitable register to place result in. */
2151 mode = TYPE_MODE (TREE_TYPE (exp));
2152
2153 /* Before working hard, check whether the instruction is available. */
2154 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2155 return NULL_RTX;
2156
2157 target = gen_reg_rtx (mode);
2158
2159 if (! flag_errno_math || ! HONOR_NANS (mode))
2160 errno_set = false;
2161
2162 if (errno_set && optimize_insn_for_size_p ())
2163 return 0;
2164
2165 /* Always stabilize the argument list. */
2166 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2167 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2168
2169 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2170 op1 = expand_normal (arg1);
2171
2172 start_sequence ();
2173
2174 /* Compute into TARGET.
2175 Set TARGET to wherever the result comes back. */
2176 target = expand_binop (mode, builtin_optab, op0, op1,
2177 target, 0, OPTAB_DIRECT);
2178
2179 /* If we were unable to expand via the builtin, stop the sequence
2180 (without outputting the insns) and call to the library function
2181 with the stabilized argument list. */
2182 if (target == 0)
2183 {
2184 end_sequence ();
2185 return expand_call (exp, target, target == const0_rtx);
2186 }
2187
2188 if (errno_set)
2189 expand_errno_check (exp, target);
2190
2191 /* Output the entire sequence. */
2192 insns = get_insns ();
2193 end_sequence ();
2194 emit_insn (insns);
2195
2196 return target;
2197 }
2198
2199 /* Expand a call to the builtin trinary math functions (fma).
2200 Return NULL_RTX if a normal call should be emitted rather than expanding the
2201 function in-line. EXP is the expression that is a call to the builtin
2202 function; if convenient, the result should be placed in TARGET.
2203 SUBTARGET may be used as the target for computing one of EXP's
2204 operands. */
2205
2206 static rtx
2207 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2208 {
2209 optab builtin_optab;
2210 rtx op0, op1, op2, insns;
2211 tree fndecl = get_callee_fndecl (exp);
2212 tree arg0, arg1, arg2;
2213 enum machine_mode mode;
2214
2215 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2216 return NULL_RTX;
2217
2218 arg0 = CALL_EXPR_ARG (exp, 0);
2219 arg1 = CALL_EXPR_ARG (exp, 1);
2220 arg2 = CALL_EXPR_ARG (exp, 2);
2221
2222 switch (DECL_FUNCTION_CODE (fndecl))
2223 {
2224 CASE_FLT_FN (BUILT_IN_FMA):
2225 builtin_optab = fma_optab; break;
2226 default:
2227 gcc_unreachable ();
2228 }
2229
2230 /* Make a suitable register to place result in. */
2231 mode = TYPE_MODE (TREE_TYPE (exp));
2232
2233 /* Before working hard, check whether the instruction is available. */
2234 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2235 return NULL_RTX;
2236
2237 target = gen_reg_rtx (mode);
2238
2239 /* Always stabilize the argument list. */
2240 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2241 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2242 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2243
2244 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2245 op1 = expand_normal (arg1);
2246 op2 = expand_normal (arg2);
2247
2248 start_sequence ();
2249
2250 /* Compute into TARGET.
2251 Set TARGET to wherever the result comes back. */
2252 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2253 target, 0);
2254
2255 /* If we were unable to expand via the builtin, stop the sequence
2256 (without outputting the insns) and call to the library function
2257 with the stabilized argument list. */
2258 if (target == 0)
2259 {
2260 end_sequence ();
2261 return expand_call (exp, target, target == const0_rtx);
2262 }
2263
2264 /* Output the entire sequence. */
2265 insns = get_insns ();
2266 end_sequence ();
2267 emit_insn (insns);
2268
2269 return target;
2270 }
2271
2272 /* Expand a call to the builtin sin and cos math functions.
2273 Return NULL_RTX if a normal call should be emitted rather than expanding the
2274 function in-line. EXP is the expression that is a call to the builtin
2275 function; if convenient, the result should be placed in TARGET.
2276 SUBTARGET may be used as the target for computing one of EXP's
2277 operands. */
2278
2279 static rtx
2280 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2281 {
2282 optab builtin_optab;
2283 rtx op0, insns;
2284 tree fndecl = get_callee_fndecl (exp);
2285 enum machine_mode mode;
2286 tree arg;
2287
2288 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2289 return NULL_RTX;
2290
2291 arg = CALL_EXPR_ARG (exp, 0);
2292
2293 switch (DECL_FUNCTION_CODE (fndecl))
2294 {
2295 CASE_FLT_FN (BUILT_IN_SIN):
2296 CASE_FLT_FN (BUILT_IN_COS):
2297 builtin_optab = sincos_optab; break;
2298 default:
2299 gcc_unreachable ();
2300 }
2301
2302 /* Make a suitable register to place result in. */
2303 mode = TYPE_MODE (TREE_TYPE (exp));
2304
2305 /* Check if sincos insn is available, otherwise fallback
2306 to sin or cos insn. */
2307 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2308 switch (DECL_FUNCTION_CODE (fndecl))
2309 {
2310 CASE_FLT_FN (BUILT_IN_SIN):
2311 builtin_optab = sin_optab; break;
2312 CASE_FLT_FN (BUILT_IN_COS):
2313 builtin_optab = cos_optab; break;
2314 default:
2315 gcc_unreachable ();
2316 }
2317
2318 /* Before working hard, check whether the instruction is available. */
2319 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2320 {
2321 target = gen_reg_rtx (mode);
2322
2323 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2324 need to expand the argument again. This way, we will not perform
2325 side-effects more the once. */
2326 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2327
2328 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2329
2330 start_sequence ();
2331
2332 /* Compute into TARGET.
2333 Set TARGET to wherever the result comes back. */
2334 if (builtin_optab == sincos_optab)
2335 {
2336 int result;
2337
2338 switch (DECL_FUNCTION_CODE (fndecl))
2339 {
2340 CASE_FLT_FN (BUILT_IN_SIN):
2341 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2342 break;
2343 CASE_FLT_FN (BUILT_IN_COS):
2344 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2345 break;
2346 default:
2347 gcc_unreachable ();
2348 }
2349 gcc_assert (result);
2350 }
2351 else
2352 {
2353 target = expand_unop (mode, builtin_optab, op0, target, 0);
2354 }
2355
2356 if (target != 0)
2357 {
2358 /* Output the entire sequence. */
2359 insns = get_insns ();
2360 end_sequence ();
2361 emit_insn (insns);
2362 return target;
2363 }
2364
2365 /* If we were unable to expand via the builtin, stop the sequence
2366 (without outputting the insns) and call to the library function
2367 with the stabilized argument list. */
2368 end_sequence ();
2369 }
2370
2371 target = expand_call (exp, target, target == const0_rtx);
2372
2373 return target;
2374 }
2375
2376 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2377 return an RTL instruction code that implements the functionality.
2378 If that isn't possible or available return CODE_FOR_nothing. */
2379
2380 static enum insn_code
2381 interclass_mathfn_icode (tree arg, tree fndecl)
2382 {
2383 bool errno_set = false;
2384 optab builtin_optab = 0;
2385 enum machine_mode mode;
2386
2387 switch (DECL_FUNCTION_CODE (fndecl))
2388 {
2389 CASE_FLT_FN (BUILT_IN_ILOGB):
2390 errno_set = true; builtin_optab = ilogb_optab; break;
2391 CASE_FLT_FN (BUILT_IN_ISINF):
2392 builtin_optab = isinf_optab; break;
2393 case BUILT_IN_ISNORMAL:
2394 case BUILT_IN_ISFINITE:
2395 CASE_FLT_FN (BUILT_IN_FINITE):
2396 case BUILT_IN_FINITED32:
2397 case BUILT_IN_FINITED64:
2398 case BUILT_IN_FINITED128:
2399 case BUILT_IN_ISINFD32:
2400 case BUILT_IN_ISINFD64:
2401 case BUILT_IN_ISINFD128:
2402 /* These builtins have no optabs (yet). */
2403 break;
2404 default:
2405 gcc_unreachable ();
2406 }
2407
2408 /* There's no easy way to detect the case we need to set EDOM. */
2409 if (flag_errno_math && errno_set)
2410 return CODE_FOR_nothing;
2411
2412 /* Optab mode depends on the mode of the input argument. */
2413 mode = TYPE_MODE (TREE_TYPE (arg));
2414
2415 if (builtin_optab)
2416 return optab_handler (builtin_optab, mode);
2417 return CODE_FOR_nothing;
2418 }
2419
2420 /* Expand a call to one of the builtin math functions that operate on
2421 floating point argument and output an integer result (ilogb, isinf,
2422 isnan, etc).
2423 Return 0 if a normal call should be emitted rather than expanding the
2424 function in-line. EXP is the expression that is a call to the builtin
2425 function; if convenient, the result should be placed in TARGET. */
2426
2427 static rtx
2428 expand_builtin_interclass_mathfn (tree exp, rtx target)
2429 {
2430 enum insn_code icode = CODE_FOR_nothing;
2431 rtx op0;
2432 tree fndecl = get_callee_fndecl (exp);
2433 enum machine_mode mode;
2434 tree arg;
2435
2436 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2437 return NULL_RTX;
2438
2439 arg = CALL_EXPR_ARG (exp, 0);
2440 icode = interclass_mathfn_icode (arg, fndecl);
2441 mode = TYPE_MODE (TREE_TYPE (arg));
2442
2443 if (icode != CODE_FOR_nothing)
2444 {
2445 struct expand_operand ops[1];
2446 rtx last = get_last_insn ();
2447 tree orig_arg = arg;
2448
2449 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2450 need to expand the argument again. This way, we will not perform
2451 side-effects more the once. */
2452 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2453
2454 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2455
2456 if (mode != GET_MODE (op0))
2457 op0 = convert_to_mode (mode, op0, 0);
2458
2459 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2460 if (maybe_legitimize_operands (icode, 0, 1, ops)
2461 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2462 return ops[0].value;
2463
2464 delete_insns_since (last);
2465 CALL_EXPR_ARG (exp, 0) = orig_arg;
2466 }
2467
2468 return NULL_RTX;
2469 }
2470
2471 /* Expand a call to the builtin sincos math function.
2472 Return NULL_RTX if a normal call should be emitted rather than expanding the
2473 function in-line. EXP is the expression that is a call to the builtin
2474 function. */
2475
2476 static rtx
2477 expand_builtin_sincos (tree exp)
2478 {
2479 rtx op0, op1, op2, target1, target2;
2480 enum machine_mode mode;
2481 tree arg, sinp, cosp;
2482 int result;
2483 location_t loc = EXPR_LOCATION (exp);
2484 tree alias_type, alias_off;
2485
2486 if (!validate_arglist (exp, REAL_TYPE,
2487 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2488 return NULL_RTX;
2489
2490 arg = CALL_EXPR_ARG (exp, 0);
2491 sinp = CALL_EXPR_ARG (exp, 1);
2492 cosp = CALL_EXPR_ARG (exp, 2);
2493
2494 /* Make a suitable register to place result in. */
2495 mode = TYPE_MODE (TREE_TYPE (arg));
2496
2497 /* Check if sincos insn is available, otherwise emit the call. */
2498 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2499 return NULL_RTX;
2500
2501 target1 = gen_reg_rtx (mode);
2502 target2 = gen_reg_rtx (mode);
2503
2504 op0 = expand_normal (arg);
2505 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2506 alias_off = build_int_cst (alias_type, 0);
2507 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2508 sinp, alias_off));
2509 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2510 cosp, alias_off));
2511
2512 /* Compute into target1 and target2.
2513 Set TARGET to wherever the result comes back. */
2514 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2515 gcc_assert (result);
2516
2517 /* Move target1 and target2 to the memory locations indicated
2518 by op1 and op2. */
2519 emit_move_insn (op1, target1);
2520 emit_move_insn (op2, target2);
2521
2522 return const0_rtx;
2523 }
2524
2525 /* Expand a call to the internal cexpi builtin to the sincos math function.
2526 EXP is the expression that is a call to the builtin function; if convenient,
2527 the result should be placed in TARGET. */
2528
2529 static rtx
2530 expand_builtin_cexpi (tree exp, rtx target)
2531 {
2532 tree fndecl = get_callee_fndecl (exp);
2533 tree arg, type;
2534 enum machine_mode mode;
2535 rtx op0, op1, op2;
2536 location_t loc = EXPR_LOCATION (exp);
2537
2538 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2539 return NULL_RTX;
2540
2541 arg = CALL_EXPR_ARG (exp, 0);
2542 type = TREE_TYPE (arg);
2543 mode = TYPE_MODE (TREE_TYPE (arg));
2544
2545 /* Try expanding via a sincos optab, fall back to emitting a libcall
2546 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2547 is only generated from sincos, cexp or if we have either of them. */
2548 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2549 {
2550 op1 = gen_reg_rtx (mode);
2551 op2 = gen_reg_rtx (mode);
2552
2553 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2554
2555 /* Compute into op1 and op2. */
2556 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2557 }
2558 else if (TARGET_HAS_SINCOS)
2559 {
2560 tree call, fn = NULL_TREE;
2561 tree top1, top2;
2562 rtx op1a, op2a;
2563
2564 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2565 fn = built_in_decls[BUILT_IN_SINCOSF];
2566 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2567 fn = built_in_decls[BUILT_IN_SINCOS];
2568 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2569 fn = built_in_decls[BUILT_IN_SINCOSL];
2570 else
2571 gcc_unreachable ();
2572
2573 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2574 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2575 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2576 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2577 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2578 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2579
2580 /* Make sure not to fold the sincos call again. */
2581 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2582 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2583 call, 3, arg, top1, top2));
2584 }
2585 else
2586 {
2587 tree call, fn = NULL_TREE, narg;
2588 tree ctype = build_complex_type (type);
2589
2590 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2591 fn = built_in_decls[BUILT_IN_CEXPF];
2592 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2593 fn = built_in_decls[BUILT_IN_CEXP];
2594 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2595 fn = built_in_decls[BUILT_IN_CEXPL];
2596 else
2597 gcc_unreachable ();
2598
2599 /* If we don't have a decl for cexp create one. This is the
2600 friendliest fallback if the user calls __builtin_cexpi
2601 without full target C99 function support. */
2602 if (fn == NULL_TREE)
2603 {
2604 tree fntype;
2605 const char *name = NULL;
2606
2607 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2608 name = "cexpf";
2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2610 name = "cexp";
2611 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2612 name = "cexpl";
2613
2614 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2615 fn = build_fn_decl (name, fntype);
2616 }
2617
2618 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2619 build_real (type, dconst0), arg);
2620
2621 /* Make sure not to fold the cexp call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623 return expand_expr (build_call_nary (ctype, call, 1, narg),
2624 target, VOIDmode, EXPAND_NORMAL);
2625 }
2626
2627 /* Now build the proper return type. */
2628 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2629 make_tree (TREE_TYPE (arg), op2),
2630 make_tree (TREE_TYPE (arg), op1)),
2631 target, VOIDmode, EXPAND_NORMAL);
2632 }
2633
2634 /* Conveniently construct a function call expression. FNDECL names the
2635 function to be called, N is the number of arguments, and the "..."
2636 parameters are the argument expressions. Unlike build_call_exr
2637 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2638
2639 static tree
2640 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2641 {
2642 va_list ap;
2643 tree fntype = TREE_TYPE (fndecl);
2644 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2645
2646 va_start (ap, n);
2647 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2648 va_end (ap);
2649 SET_EXPR_LOCATION (fn, loc);
2650 return fn;
2651 }
2652
2653 /* Expand a call to one of the builtin rounding functions gcc defines
2654 as an extension (lfloor and lceil). As these are gcc extensions we
2655 do not need to worry about setting errno to EDOM.
2656 If expanding via optab fails, lower expression to (int)(floor(x)).
2657 EXP is the expression that is a call to the builtin function;
2658 if convenient, the result should be placed in TARGET. */
2659
2660 static rtx
2661 expand_builtin_int_roundingfn (tree exp, rtx target)
2662 {
2663 convert_optab builtin_optab;
2664 rtx op0, insns, tmp;
2665 tree fndecl = get_callee_fndecl (exp);
2666 enum built_in_function fallback_fn;
2667 tree fallback_fndecl;
2668 enum machine_mode mode;
2669 tree arg;
2670
2671 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2672 gcc_unreachable ();
2673
2674 arg = CALL_EXPR_ARG (exp, 0);
2675
2676 switch (DECL_FUNCTION_CODE (fndecl))
2677 {
2678 CASE_FLT_FN (BUILT_IN_LCEIL):
2679 CASE_FLT_FN (BUILT_IN_LLCEIL):
2680 builtin_optab = lceil_optab;
2681 fallback_fn = BUILT_IN_CEIL;
2682 break;
2683
2684 CASE_FLT_FN (BUILT_IN_LFLOOR):
2685 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2686 builtin_optab = lfloor_optab;
2687 fallback_fn = BUILT_IN_FLOOR;
2688 break;
2689
2690 default:
2691 gcc_unreachable ();
2692 }
2693
2694 /* Make a suitable register to place result in. */
2695 mode = TYPE_MODE (TREE_TYPE (exp));
2696
2697 target = gen_reg_rtx (mode);
2698
2699 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2700 need to expand the argument again. This way, we will not perform
2701 side-effects more the once. */
2702 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2703
2704 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2705
2706 start_sequence ();
2707
2708 /* Compute into TARGET. */
2709 if (expand_sfix_optab (target, op0, builtin_optab))
2710 {
2711 /* Output the entire sequence. */
2712 insns = get_insns ();
2713 end_sequence ();
2714 emit_insn (insns);
2715 return target;
2716 }
2717
2718 /* If we were unable to expand via the builtin, stop the sequence
2719 (without outputting the insns). */
2720 end_sequence ();
2721
2722 /* Fall back to floating point rounding optab. */
2723 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2724
2725 /* For non-C99 targets we may end up without a fallback fndecl here
2726 if the user called __builtin_lfloor directly. In this case emit
2727 a call to the floor/ceil variants nevertheless. This should result
2728 in the best user experience for not full C99 targets. */
2729 if (fallback_fndecl == NULL_TREE)
2730 {
2731 tree fntype;
2732 const char *name = NULL;
2733
2734 switch (DECL_FUNCTION_CODE (fndecl))
2735 {
2736 case BUILT_IN_LCEIL:
2737 case BUILT_IN_LLCEIL:
2738 name = "ceil";
2739 break;
2740 case BUILT_IN_LCEILF:
2741 case BUILT_IN_LLCEILF:
2742 name = "ceilf";
2743 break;
2744 case BUILT_IN_LCEILL:
2745 case BUILT_IN_LLCEILL:
2746 name = "ceill";
2747 break;
2748 case BUILT_IN_LFLOOR:
2749 case BUILT_IN_LLFLOOR:
2750 name = "floor";
2751 break;
2752 case BUILT_IN_LFLOORF:
2753 case BUILT_IN_LLFLOORF:
2754 name = "floorf";
2755 break;
2756 case BUILT_IN_LFLOORL:
2757 case BUILT_IN_LLFLOORL:
2758 name = "floorl";
2759 break;
2760 default:
2761 gcc_unreachable ();
2762 }
2763
2764 fntype = build_function_type_list (TREE_TYPE (arg),
2765 TREE_TYPE (arg), NULL_TREE);
2766 fallback_fndecl = build_fn_decl (name, fntype);
2767 }
2768
2769 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2770
2771 tmp = expand_normal (exp);
2772
2773 /* Truncate the result of floating point optab to integer
2774 via expand_fix (). */
2775 target = gen_reg_rtx (mode);
2776 expand_fix (target, tmp, 0);
2777
2778 return target;
2779 }
2780
2781 /* Expand a call to one of the builtin math functions doing integer
2782 conversion (lrint).
2783 Return 0 if a normal call should be emitted rather than expanding the
2784 function in-line. EXP is the expression that is a call to the builtin
2785 function; if convenient, the result should be placed in TARGET. */
2786
2787 static rtx
2788 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2789 {
2790 convert_optab builtin_optab;
2791 rtx op0, insns;
2792 tree fndecl = get_callee_fndecl (exp);
2793 tree arg;
2794 enum machine_mode mode;
2795
2796 /* There's no easy way to detect the case we need to set EDOM. */
2797 if (flag_errno_math)
2798 return NULL_RTX;
2799
2800 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2801 gcc_unreachable ();
2802
2803 arg = CALL_EXPR_ARG (exp, 0);
2804
2805 switch (DECL_FUNCTION_CODE (fndecl))
2806 {
2807 CASE_FLT_FN (BUILT_IN_LRINT):
2808 CASE_FLT_FN (BUILT_IN_LLRINT):
2809 builtin_optab = lrint_optab; break;
2810 CASE_FLT_FN (BUILT_IN_LROUND):
2811 CASE_FLT_FN (BUILT_IN_LLROUND):
2812 builtin_optab = lround_optab; break;
2813 default:
2814 gcc_unreachable ();
2815 }
2816
2817 /* Make a suitable register to place result in. */
2818 mode = TYPE_MODE (TREE_TYPE (exp));
2819
2820 target = gen_reg_rtx (mode);
2821
2822 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2823 need to expand the argument again. This way, we will not perform
2824 side-effects more the once. */
2825 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2826
2827 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2828
2829 start_sequence ();
2830
2831 if (expand_sfix_optab (target, op0, builtin_optab))
2832 {
2833 /* Output the entire sequence. */
2834 insns = get_insns ();
2835 end_sequence ();
2836 emit_insn (insns);
2837 return target;
2838 }
2839
2840 /* If we were unable to expand via the builtin, stop the sequence
2841 (without outputting the insns) and call to the library function
2842 with the stabilized argument list. */
2843 end_sequence ();
2844
2845 target = expand_call (exp, target, target == const0_rtx);
2846
2847 return target;
2848 }
2849
2850 /* To evaluate powi(x,n), the floating point value x raised to the
2851 constant integer exponent n, we use a hybrid algorithm that
2852 combines the "window method" with look-up tables. For an
2853 introduction to exponentiation algorithms and "addition chains",
2854 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2855 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2856 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2857 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2858
2859 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2860 multiplications to inline before calling the system library's pow
2861 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2862 so this default never requires calling pow, powf or powl. */
2863
2864 #ifndef POWI_MAX_MULTS
2865 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2866 #endif
2867
2868 /* The size of the "optimal power tree" lookup table. All
2869 exponents less than this value are simply looked up in the
2870 powi_table below. This threshold is also used to size the
2871 cache of pseudo registers that hold intermediate results. */
2872 #define POWI_TABLE_SIZE 256
2873
2874 /* The size, in bits of the window, used in the "window method"
2875 exponentiation algorithm. This is equivalent to a radix of
2876 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2877 #define POWI_WINDOW_SIZE 3
2878
2879 /* The following table is an efficient representation of an
2880 "optimal power tree". For each value, i, the corresponding
2881 value, j, in the table states than an optimal evaluation
2882 sequence for calculating pow(x,i) can be found by evaluating
2883 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2884 100 integers is given in Knuth's "Seminumerical algorithms". */
2885
2886 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2887 {
2888 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2889 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2890 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2891 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2892 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2893 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2894 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2895 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2896 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2897 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2898 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2899 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2900 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2901 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2902 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2903 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2904 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2905 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2906 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2907 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2908 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2909 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2910 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2911 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2912 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2913 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2914 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2915 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2916 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2917 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2918 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2919 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2920 };
2921
2922
2923 /* Return the number of multiplications required to calculate
2924 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2925 subroutine of powi_cost. CACHE is an array indicating
2926 which exponents have already been calculated. */
2927
2928 static int
2929 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2930 {
2931 /* If we've already calculated this exponent, then this evaluation
2932 doesn't require any additional multiplications. */
2933 if (cache[n])
2934 return 0;
2935
2936 cache[n] = true;
2937 return powi_lookup_cost (n - powi_table[n], cache)
2938 + powi_lookup_cost (powi_table[n], cache) + 1;
2939 }
2940
2941 /* Return the number of multiplications required to calculate
2942 powi(x,n) for an arbitrary x, given the exponent N. This
2943 function needs to be kept in sync with expand_powi below. */
2944
2945 static int
2946 powi_cost (HOST_WIDE_INT n)
2947 {
2948 bool cache[POWI_TABLE_SIZE];
2949 unsigned HOST_WIDE_INT digit;
2950 unsigned HOST_WIDE_INT val;
2951 int result;
2952
2953 if (n == 0)
2954 return 0;
2955
2956 /* Ignore the reciprocal when calculating the cost. */
2957 val = (n < 0) ? -n : n;
2958
2959 /* Initialize the exponent cache. */
2960 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2961 cache[1] = true;
2962
2963 result = 0;
2964
2965 while (val >= POWI_TABLE_SIZE)
2966 {
2967 if (val & 1)
2968 {
2969 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2970 result += powi_lookup_cost (digit, cache)
2971 + POWI_WINDOW_SIZE + 1;
2972 val >>= POWI_WINDOW_SIZE;
2973 }
2974 else
2975 {
2976 val >>= 1;
2977 result++;
2978 }
2979 }
2980
2981 return result + powi_lookup_cost (val, cache);
2982 }
2983
2984 /* Recursive subroutine of expand_powi. This function takes the array,
2985 CACHE, of already calculated exponents and an exponent N and returns
2986 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2987
2988 static rtx
2989 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2990 {
2991 unsigned HOST_WIDE_INT digit;
2992 rtx target, result;
2993 rtx op0, op1;
2994
2995 if (n < POWI_TABLE_SIZE)
2996 {
2997 if (cache[n])
2998 return cache[n];
2999
3000 target = gen_reg_rtx (mode);
3001 cache[n] = target;
3002
3003 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
3004 op1 = expand_powi_1 (mode, powi_table[n], cache);
3005 }
3006 else if (n & 1)
3007 {
3008 target = gen_reg_rtx (mode);
3009 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3010 op0 = expand_powi_1 (mode, n - digit, cache);
3011 op1 = expand_powi_1 (mode, digit, cache);
3012 }
3013 else
3014 {
3015 target = gen_reg_rtx (mode);
3016 op0 = expand_powi_1 (mode, n >> 1, cache);
3017 op1 = op0;
3018 }
3019
3020 result = expand_mult (mode, op0, op1, target, 0);
3021 if (result != target)
3022 emit_move_insn (target, result);
3023 return target;
3024 }
3025
3026 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
3027 floating point operand in mode MODE, and N is the exponent. This
3028 function needs to be kept in sync with powi_cost above. */
3029
3030 static rtx
3031 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
3032 {
3033 rtx cache[POWI_TABLE_SIZE];
3034 rtx result;
3035
3036 if (n == 0)
3037 return CONST1_RTX (mode);
3038
3039 memset (cache, 0, sizeof (cache));
3040 cache[1] = x;
3041
3042 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
3043
3044 /* If the original exponent was negative, reciprocate the result. */
3045 if (n < 0)
3046 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3047 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3048
3049 return result;
3050 }
3051
3052 /* Fold a builtin function call to pow, powf, or powl into a series of sqrts or
3053 cbrts. Return NULL_RTX if no simplification can be made or expand the tree
3054 if we can simplify it. */
3055 static rtx
3056 expand_builtin_pow_root (location_t loc, tree arg0, tree arg1, tree type,
3057 rtx subtarget)
3058 {
3059 if (TREE_CODE (arg1) == REAL_CST
3060 && !TREE_OVERFLOW (arg1)
3061 && flag_unsafe_math_optimizations)
3062 {
3063 enum machine_mode mode = TYPE_MODE (type);
3064 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
3065 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
3066 REAL_VALUE_TYPE c = TREE_REAL_CST (arg1);
3067 tree op = NULL_TREE;
3068
3069 if (sqrtfn)
3070 {
3071 /* Optimize pow (x, 0.5) into sqrt. */
3072 if (REAL_VALUES_EQUAL (c, dconsthalf))
3073 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3074
3075 /* Don't do this optimization if we don't have a sqrt insn. */
3076 else if (optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
3077 {
3078 REAL_VALUE_TYPE dconst1_4 = dconst1;
3079 REAL_VALUE_TYPE dconst3_4;
3080 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
3081
3082 real_from_integer (&dconst3_4, VOIDmode, 3, 0, 0);
3083 SET_REAL_EXP (&dconst3_4, REAL_EXP (&dconst3_4) - 2);
3084
3085 /* Optimize pow (x, 0.25) into sqrt (sqrt (x)). Assume on most
3086 machines that a builtin sqrt instruction is smaller than a
3087 call to pow with 0.25, so do this optimization even if
3088 -Os. */
3089 if (REAL_VALUES_EQUAL (c, dconst1_4))
3090 {
3091 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3092 op = build_call_nofold_loc (loc, sqrtfn, 1, op);
3093 }
3094
3095 /* Optimize pow (x, 0.75) = sqrt (x) * sqrt (sqrt (x)) unless we
3096 are optimizing for space. */
3097 else if (optimize_insn_for_speed_p ()
3098 && !TREE_SIDE_EFFECTS (arg0)
3099 && REAL_VALUES_EQUAL (c, dconst3_4))
3100 {
3101 tree sqrt1 = build_call_expr_loc (loc, sqrtfn, 1, arg0);
3102 tree sqrt2 = builtin_save_expr (sqrt1);
3103 tree sqrt3 = build_call_expr_loc (loc, sqrtfn, 1, sqrt1);
3104 op = fold_build2_loc (loc, MULT_EXPR, type, sqrt2, sqrt3);
3105 }
3106 }
3107 }
3108
3109 /* Check whether we can do cbrt insstead of pow (x, 1./3.) and
3110 cbrt/sqrts instead of pow (x, 1./6.). */
3111 if (cbrtfn && ! op
3112 && (tree_expr_nonnegative_p (arg0) || !HONOR_NANS (mode)))
3113 {
3114 /* First try 1/3. */
3115 REAL_VALUE_TYPE dconst1_3
3116 = real_value_truncate (mode, dconst_third ());
3117
3118 if (REAL_VALUES_EQUAL (c, dconst1_3))
3119 op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
3120
3121 /* Now try 1/6. */
3122 else if (optimize_insn_for_speed_p ()
3123 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
3124 {
3125 REAL_VALUE_TYPE dconst1_6 = dconst1_3;
3126 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
3127
3128 if (REAL_VALUES_EQUAL (c, dconst1_6))
3129 {
3130 op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3131 op = build_call_nofold_loc (loc, cbrtfn, 1, op);
3132 }
3133 }
3134 }
3135
3136 if (op)
3137 return expand_expr (op, subtarget, mode, EXPAND_NORMAL);
3138 }
3139
3140 return NULL_RTX;
3141 }
3142
3143 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
3144 a normal call should be emitted rather than expanding the function
3145 in-line. EXP is the expression that is a call to the builtin
3146 function; if convenient, the result should be placed in TARGET. */
3147
3148 static rtx
3149 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
3150 {
3151 tree arg0, arg1;
3152 tree fn, narg0;
3153 tree type = TREE_TYPE (exp);
3154 REAL_VALUE_TYPE cint, c, c2;
3155 HOST_WIDE_INT n;
3156 rtx op, op2;
3157 enum machine_mode mode = TYPE_MODE (type);
3158
3159 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
3160 return NULL_RTX;
3161
3162 arg0 = CALL_EXPR_ARG (exp, 0);
3163 arg1 = CALL_EXPR_ARG (exp, 1);
3164
3165 if (TREE_CODE (arg1) != REAL_CST
3166 || TREE_OVERFLOW (arg1))
3167 return expand_builtin_mathfn_2 (exp, target, subtarget);
3168
3169 /* Handle constant exponents. */
3170
3171 /* For integer valued exponents we can expand to an optimal multiplication
3172 sequence using expand_powi. */
3173 c = TREE_REAL_CST (arg1);
3174 n = real_to_integer (&c);
3175 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3176 if (real_identical (&c, &cint)
3177 && ((n >= -1 && n <= 2)
3178 || (flag_unsafe_math_optimizations
3179 && optimize_insn_for_speed_p ()
3180 && powi_cost (n) <= POWI_MAX_MULTS)))
3181 {
3182 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3183 if (n != 1)
3184 {
3185 op = force_reg (mode, op);
3186 op = expand_powi (op, mode, n);
3187 }
3188 return op;
3189 }
3190
3191 narg0 = builtin_save_expr (arg0);
3192
3193 /* If the exponent is not integer valued, check if it is half of an integer.
3194 In this case we can expand to sqrt (x) * x**(n/2). */
3195 fn = mathfn_built_in (type, BUILT_IN_SQRT);
3196 if (fn != NULL_TREE)
3197 {
3198 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3199 n = real_to_integer (&c2);
3200 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3201 if (real_identical (&c2, &cint)
3202 && ((flag_unsafe_math_optimizations
3203 && optimize_insn_for_speed_p ()
3204 && powi_cost (n/2) <= POWI_MAX_MULTS)
3205 /* Even the c == 0.5 case cannot be done unconditionally
3206 when we need to preserve signed zeros, as
3207 pow (-0, 0.5) is +0, while sqrt(-0) is -0. */
3208 || (!HONOR_SIGNED_ZEROS (mode) && n == 1)
3209 /* For c == 1.5 we can assume that x * sqrt (x) is always
3210 smaller than pow (x, 1.5) if sqrt will not be expanded
3211 as a call. */
3212 || (n == 3
3213 && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)))
3214 {
3215 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3216 narg0);
3217 /* Use expand_expr in case the newly built call expression
3218 was folded to a non-call. */
3219 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3220 if (n != 1)
3221 {
3222 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3223 op2 = force_reg (mode, op2);
3224 op2 = expand_powi (op2, mode, abs (n / 2));
3225 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3226 0, OPTAB_LIB_WIDEN);
3227 /* If the original exponent was negative, reciprocate the
3228 result. */
3229 if (n < 0)
3230 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3231 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3232 }
3233 return op;
3234 }
3235 }
3236
3237 /* Check whether we can do a series of sqrt or cbrt's instead of the pow
3238 call. */
3239 op = expand_builtin_pow_root (EXPR_LOCATION (exp), arg0, arg1, type,
3240 subtarget);
3241 if (op)
3242 return op;
3243
3244 /* Try if the exponent is a third of an integer. In this case
3245 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
3246 different from pow (x, 1./3.) due to rounding and behavior
3247 with negative x we need to constrain this transformation to
3248 unsafe math and positive x or finite math. */
3249 fn = mathfn_built_in (type, BUILT_IN_CBRT);
3250 if (fn != NULL_TREE
3251 && flag_unsafe_math_optimizations
3252 && (tree_expr_nonnegative_p (arg0)
3253 || !HONOR_NANS (mode)))
3254 {
3255 REAL_VALUE_TYPE dconst3;
3256 real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3257 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3258 real_round (&c2, mode, &c2);
3259 n = real_to_integer (&c2);
3260 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3261 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3262 real_convert (&c2, mode, &c2);
3263 if (real_identical (&c2, &c)
3264 && ((optimize_insn_for_speed_p ()
3265 && powi_cost (n/3) <= POWI_MAX_MULTS)
3266 || n == 1))
3267 {
3268 tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3269 narg0);
3270 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3271 if (abs (n) % 3 == 2)
3272 op = expand_simple_binop (mode, MULT, op, op, op,
3273 0, OPTAB_LIB_WIDEN);
3274 if (n != 1)
3275 {
3276 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3277 op2 = force_reg (mode, op2);
3278 op2 = expand_powi (op2, mode, abs (n / 3));
3279 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3280 0, OPTAB_LIB_WIDEN);
3281 /* If the original exponent was negative, reciprocate the
3282 result. */
3283 if (n < 0)
3284 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3285 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3286 }
3287 return op;
3288 }
3289 }
3290
3291 /* Fall back to optab expansion. */
3292 return expand_builtin_mathfn_2 (exp, target, subtarget);
3293 }
3294
3295 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3296 a normal call should be emitted rather than expanding the function
3297 in-line. EXP is the expression that is a call to the builtin
3298 function; if convenient, the result should be placed in TARGET. */
3299
3300 static rtx
3301 expand_builtin_powi (tree exp, rtx target)
3302 {
3303 tree arg0, arg1;
3304 rtx op0, op1;
3305 enum machine_mode mode;
3306 enum machine_mode mode2;
3307
3308 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3309 return NULL_RTX;
3310
3311 arg0 = CALL_EXPR_ARG (exp, 0);
3312 arg1 = CALL_EXPR_ARG (exp, 1);
3313 mode = TYPE_MODE (TREE_TYPE (exp));
3314
3315 /* Handle constant power. */
3316
3317 if (TREE_CODE (arg1) == INTEGER_CST
3318 && !TREE_OVERFLOW (arg1))
3319 {
3320 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3321
3322 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3323 Otherwise, check the number of multiplications required. */
3324 if ((TREE_INT_CST_HIGH (arg1) == 0
3325 || TREE_INT_CST_HIGH (arg1) == -1)
3326 && ((n >= -1 && n <= 2)
3327 || (optimize_insn_for_speed_p ()
3328 && powi_cost (n) <= POWI_MAX_MULTS)))
3329 {
3330 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3331 op0 = force_reg (mode, op0);
3332 return expand_powi (op0, mode, n);
3333 }
3334 }
3335
3336 /* Emit a libcall to libgcc. */
3337
3338 /* Mode of the 2nd argument must match that of an int. */
3339 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3340
3341 if (target == NULL_RTX)
3342 target = gen_reg_rtx (mode);
3343
3344 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3345 if (GET_MODE (op0) != mode)
3346 op0 = convert_to_mode (mode, op0, 0);
3347 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3348 if (GET_MODE (op1) != mode2)
3349 op1 = convert_to_mode (mode2, op1, 0);
3350
3351 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3352 target, LCT_CONST, mode, 2,
3353 op0, mode, op1, mode2);
3354
3355 return target;
3356 }
3357
3358 /* Expand expression EXP which is a call to the strlen builtin. Return
3359 NULL_RTX if we failed the caller should emit a normal call, otherwise
3360 try to get the result in TARGET, if convenient. */
3361
3362 static rtx
3363 expand_builtin_strlen (tree exp, rtx target,
3364 enum machine_mode target_mode)
3365 {
3366 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3367 return NULL_RTX;
3368 else
3369 {
3370 struct expand_operand ops[4];
3371 rtx pat;
3372 tree len;
3373 tree src = CALL_EXPR_ARG (exp, 0);
3374 rtx src_reg, before_strlen;
3375 enum machine_mode insn_mode = target_mode;
3376 enum insn_code icode = CODE_FOR_nothing;
3377 unsigned int align;
3378
3379 /* If the length can be computed at compile-time, return it. */
3380 len = c_strlen (src, 0);
3381 if (len)
3382 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3383
3384 /* If the length can be computed at compile-time and is constant
3385 integer, but there are side-effects in src, evaluate
3386 src for side-effects, then return len.
3387 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3388 can be optimized into: i++; x = 3; */
3389 len = c_strlen (src, 1);
3390 if (len && TREE_CODE (len) == INTEGER_CST)
3391 {
3392 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3393 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3394 }
3395
3396 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3397
3398 /* If SRC is not a pointer type, don't do this operation inline. */
3399 if (align == 0)
3400 return NULL_RTX;
3401
3402 /* Bail out if we can't compute strlen in the right mode. */
3403 while (insn_mode != VOIDmode)
3404 {
3405 icode = optab_handler (strlen_optab, insn_mode);
3406 if (icode != CODE_FOR_nothing)
3407 break;
3408
3409 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3410 }
3411 if (insn_mode == VOIDmode)
3412 return NULL_RTX;
3413
3414 /* Make a place to hold the source address. We will not expand
3415 the actual source until we are sure that the expansion will
3416 not fail -- there are trees that cannot be expanded twice. */
3417 src_reg = gen_reg_rtx (Pmode);
3418
3419 /* Mark the beginning of the strlen sequence so we can emit the
3420 source operand later. */
3421 before_strlen = get_last_insn ();
3422
3423 create_output_operand (&ops[0], target, insn_mode);
3424 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3425 create_integer_operand (&ops[2], 0);
3426 create_integer_operand (&ops[3], align);
3427 if (!maybe_expand_insn (icode, 4, ops))
3428 return NULL_RTX;
3429
3430 /* Now that we are assured of success, expand the source. */
3431 start_sequence ();
3432 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3433 if (pat != src_reg)
3434 emit_move_insn (src_reg, pat);
3435 pat = get_insns ();
3436 end_sequence ();
3437
3438 if (before_strlen)
3439 emit_insn_after (pat, before_strlen);
3440 else
3441 emit_insn_before (pat, get_insns ());
3442
3443 /* Return the value in the proper mode for this function. */
3444 if (GET_MODE (ops[0].value) == target_mode)
3445 target = ops[0].value;
3446 else if (target != 0)
3447 convert_move (target, ops[0].value, 0);
3448 else
3449 target = convert_to_mode (target_mode, ops[0].value, 0);
3450
3451 return target;
3452 }
3453 }
3454
3455 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3456 bytes from constant string DATA + OFFSET and return it as target
3457 constant. */
3458
3459 static rtx
3460 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3461 enum machine_mode mode)
3462 {
3463 const char *str = (const char *) data;
3464
3465 gcc_assert (offset >= 0
3466 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3467 <= strlen (str) + 1));
3468
3469 return c_readstr (str + offset, mode);
3470 }
3471
3472 /* Expand a call EXP to the memcpy builtin.
3473 Return NULL_RTX if we failed, the caller should emit a normal call,
3474 otherwise try to get the result in TARGET, if convenient (and in
3475 mode MODE if that's convenient). */
3476
3477 static rtx
3478 expand_builtin_memcpy (tree exp, rtx target)
3479 {
3480 if (!validate_arglist (exp,
3481 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3482 return NULL_RTX;
3483 else
3484 {
3485 tree dest = CALL_EXPR_ARG (exp, 0);
3486 tree src = CALL_EXPR_ARG (exp, 1);
3487 tree len = CALL_EXPR_ARG (exp, 2);
3488 const char *src_str;
3489 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3490 unsigned int dest_align
3491 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3492 rtx dest_mem, src_mem, dest_addr, len_rtx;
3493 HOST_WIDE_INT expected_size = -1;
3494 unsigned int expected_align = 0;
3495
3496 /* If DEST is not a pointer type, call the normal function. */
3497 if (dest_align == 0)
3498 return NULL_RTX;
3499
3500 /* If either SRC is not a pointer type, don't do this
3501 operation in-line. */
3502 if (src_align == 0)
3503 return NULL_RTX;
3504
3505 if (currently_expanding_gimple_stmt)
3506 stringop_block_profile (currently_expanding_gimple_stmt,
3507 &expected_align, &expected_size);
3508
3509 if (expected_align < dest_align)
3510 expected_align = dest_align;
3511 dest_mem = get_memory_rtx (dest, len);
3512 set_mem_align (dest_mem, dest_align);
3513 len_rtx = expand_normal (len);
3514 src_str = c_getstr (src);
3515
3516 /* If SRC is a string constant and block move would be done
3517 by pieces, we can avoid loading the string from memory
3518 and only stored the computed constants. */
3519 if (src_str
3520 && CONST_INT_P (len_rtx)
3521 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3522 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3523 CONST_CAST (char *, src_str),
3524 dest_align, false))
3525 {
3526 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3527 builtin_memcpy_read_str,
3528 CONST_CAST (char *, src_str),
3529 dest_align, false, 0);
3530 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3531 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3532 return dest_mem;
3533 }
3534
3535 src_mem = get_memory_rtx (src, len);
3536 set_mem_align (src_mem, src_align);
3537
3538 /* Copy word part most expediently. */
3539 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3540 CALL_EXPR_TAILCALL (exp)
3541 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3542 expected_align, expected_size);
3543
3544 if (dest_addr == 0)
3545 {
3546 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3547 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3548 }
3549 return dest_addr;
3550 }
3551 }
3552
3553 /* Expand a call EXP to the mempcpy builtin.
3554 Return NULL_RTX if we failed; the caller should emit a normal call,
3555 otherwise try to get the result in TARGET, if convenient (and in
3556 mode MODE if that's convenient). If ENDP is 0 return the
3557 destination pointer, if ENDP is 1 return the end pointer ala
3558 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3559 stpcpy. */
3560
3561 static rtx
3562 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3563 {
3564 if (!validate_arglist (exp,
3565 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3566 return NULL_RTX;
3567 else
3568 {
3569 tree dest = CALL_EXPR_ARG (exp, 0);
3570 tree src = CALL_EXPR_ARG (exp, 1);
3571 tree len = CALL_EXPR_ARG (exp, 2);
3572 return expand_builtin_mempcpy_args (dest, src, len,
3573 target, mode, /*endp=*/ 1);
3574 }
3575 }
3576
3577 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3578 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3579 so that this can also be called without constructing an actual CALL_EXPR.
3580 The other arguments and return value are the same as for
3581 expand_builtin_mempcpy. */
3582
3583 static rtx
3584 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3585 rtx target, enum machine_mode mode, int endp)
3586 {
3587 /* If return value is ignored, transform mempcpy into memcpy. */
3588 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3589 {
3590 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3591 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3592 dest, src, len);
3593 return expand_expr (result, target, mode, EXPAND_NORMAL);
3594 }
3595 else
3596 {
3597 const char *src_str;
3598 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3599 unsigned int dest_align
3600 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3601 rtx dest_mem, src_mem, len_rtx;
3602
3603 /* If either SRC or DEST is not a pointer type, don't do this
3604 operation in-line. */
3605 if (dest_align == 0 || src_align == 0)
3606 return NULL_RTX;
3607
3608 /* If LEN is not constant, call the normal function. */
3609 if (! host_integerp (len, 1))
3610 return NULL_RTX;
3611
3612 len_rtx = expand_normal (len);
3613 src_str = c_getstr (src);
3614
3615 /* If SRC is a string constant and block move would be done
3616 by pieces, we can avoid loading the string from memory
3617 and only stored the computed constants. */
3618 if (src_str
3619 && CONST_INT_P (len_rtx)
3620 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3621 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3622 CONST_CAST (char *, src_str),
3623 dest_align, false))
3624 {
3625 dest_mem = get_memory_rtx (dest, len);
3626 set_mem_align (dest_mem, dest_align);
3627 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3628 builtin_memcpy_read_str,
3629 CONST_CAST (char *, src_str),
3630 dest_align, false, endp);
3631 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3632 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3633 return dest_mem;
3634 }
3635
3636 if (CONST_INT_P (len_rtx)
3637 && can_move_by_pieces (INTVAL (len_rtx),
3638 MIN (dest_align, src_align)))
3639 {
3640 dest_mem = get_memory_rtx (dest, len);
3641 set_mem_align (dest_mem, dest_align);
3642 src_mem = get_memory_rtx (src, len);
3643 set_mem_align (src_mem, src_align);
3644 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3645 MIN (dest_align, src_align), endp);
3646 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3647 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3648 return dest_mem;
3649 }
3650
3651 return NULL_RTX;
3652 }
3653 }
3654
3655 #ifndef HAVE_movstr
3656 # define HAVE_movstr 0
3657 # define CODE_FOR_movstr CODE_FOR_nothing
3658 #endif
3659
3660 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3661 we failed, the caller should emit a normal call, otherwise try to
3662 get the result in TARGET, if convenient. If ENDP is 0 return the
3663 destination pointer, if ENDP is 1 return the end pointer ala
3664 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3665 stpcpy. */
3666
3667 static rtx
3668 expand_movstr (tree dest, tree src, rtx target, int endp)
3669 {
3670 struct expand_operand ops[3];
3671 rtx dest_mem;
3672 rtx src_mem;
3673
3674 if (!HAVE_movstr)
3675 return NULL_RTX;
3676
3677 dest_mem = get_memory_rtx (dest, NULL);
3678 src_mem = get_memory_rtx (src, NULL);
3679 if (!endp)
3680 {
3681 target = force_reg (Pmode, XEXP (dest_mem, 0));
3682 dest_mem = replace_equiv_address (dest_mem, target);
3683 }
3684
3685 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3686 create_fixed_operand (&ops[1], dest_mem);
3687 create_fixed_operand (&ops[2], src_mem);
3688 expand_insn (CODE_FOR_movstr, 3, ops);
3689
3690 if (endp && target != const0_rtx)
3691 {
3692 target = ops[0].value;
3693 /* movstr is supposed to set end to the address of the NUL
3694 terminator. If the caller requested a mempcpy-like return value,
3695 adjust it. */
3696 if (endp == 1)
3697 {
3698 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3699 emit_move_insn (target, force_operand (tem, NULL_RTX));
3700 }
3701 }
3702 return target;
3703 }
3704
3705 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3706 NULL_RTX if we failed the caller should emit a normal call, otherwise
3707 try to get the result in TARGET, if convenient (and in mode MODE if that's
3708 convenient). */
3709
3710 static rtx
3711 expand_builtin_strcpy (tree exp, rtx target)
3712 {
3713 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3714 {
3715 tree dest = CALL_EXPR_ARG (exp, 0);
3716 tree src = CALL_EXPR_ARG (exp, 1);
3717 return expand_builtin_strcpy_args (dest, src, target);
3718 }
3719 return NULL_RTX;
3720 }
3721
3722 /* Helper function to do the actual work for expand_builtin_strcpy. The
3723 arguments to the builtin_strcpy call DEST and SRC are broken out
3724 so that this can also be called without constructing an actual CALL_EXPR.
3725 The other arguments and return value are the same as for
3726 expand_builtin_strcpy. */
3727
3728 static rtx
3729 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3730 {
3731 return expand_movstr (dest, src, target, /*endp=*/0);
3732 }
3733
3734 /* Expand a call EXP to the stpcpy builtin.
3735 Return NULL_RTX if we failed the caller should emit a normal call,
3736 otherwise try to get the result in TARGET, if convenient (and in
3737 mode MODE if that's convenient). */
3738
3739 static rtx
3740 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3741 {
3742 tree dst, src;
3743 location_t loc = EXPR_LOCATION (exp);
3744
3745 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3746 return NULL_RTX;
3747
3748 dst = CALL_EXPR_ARG (exp, 0);
3749 src = CALL_EXPR_ARG (exp, 1);
3750
3751 /* If return value is ignored, transform stpcpy into strcpy. */
3752 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3753 {
3754 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3755 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3756 return expand_expr (result, target, mode, EXPAND_NORMAL);
3757 }
3758 else
3759 {
3760 tree len, lenp1;
3761 rtx ret;
3762
3763 /* Ensure we get an actual string whose length can be evaluated at
3764 compile-time, not an expression containing a string. This is
3765 because the latter will potentially produce pessimized code
3766 when used to produce the return value. */
3767 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3768 return expand_movstr (dst, src, target, /*endp=*/2);
3769
3770 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3771 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3772 target, mode, /*endp=*/2);
3773
3774 if (ret)
3775 return ret;
3776
3777 if (TREE_CODE (len) == INTEGER_CST)
3778 {
3779 rtx len_rtx = expand_normal (len);
3780
3781 if (CONST_INT_P (len_rtx))
3782 {
3783 ret = expand_builtin_strcpy_args (dst, src, target);
3784
3785 if (ret)
3786 {
3787 if (! target)
3788 {
3789 if (mode != VOIDmode)
3790 target = gen_reg_rtx (mode);
3791 else
3792 target = gen_reg_rtx (GET_MODE (ret));
3793 }
3794 if (GET_MODE (target) != GET_MODE (ret))
3795 ret = gen_lowpart (GET_MODE (target), ret);
3796
3797 ret = plus_constant (ret, INTVAL (len_rtx));
3798 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3799 gcc_assert (ret);
3800
3801 return target;
3802 }
3803 }
3804 }
3805
3806 return expand_movstr (dst, src, target, /*endp=*/2);
3807 }
3808 }
3809
3810 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3811 bytes from constant string DATA + OFFSET and return it as target
3812 constant. */
3813
3814 rtx
3815 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3816 enum machine_mode mode)
3817 {
3818 const char *str = (const char *) data;
3819
3820 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3821 return const0_rtx;
3822
3823 return c_readstr (str + offset, mode);
3824 }
3825
3826 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3827 NULL_RTX if we failed the caller should emit a normal call. */
3828
3829 static rtx
3830 expand_builtin_strncpy (tree exp, rtx target)
3831 {
3832 location_t loc = EXPR_LOCATION (exp);
3833
3834 if (validate_arglist (exp,
3835 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3836 {
3837 tree dest = CALL_EXPR_ARG (exp, 0);
3838 tree src = CALL_EXPR_ARG (exp, 1);
3839 tree len = CALL_EXPR_ARG (exp, 2);
3840 tree slen = c_strlen (src, 1);
3841
3842 /* We must be passed a constant len and src parameter. */
3843 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3844 return NULL_RTX;
3845
3846 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3847
3848 /* We're required to pad with trailing zeros if the requested
3849 len is greater than strlen(s2)+1. In that case try to
3850 use store_by_pieces, if it fails, punt. */
3851 if (tree_int_cst_lt (slen, len))
3852 {
3853 unsigned int dest_align
3854 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3855 const char *p = c_getstr (src);
3856 rtx dest_mem;
3857
3858 if (!p || dest_align == 0 || !host_integerp (len, 1)
3859 || !can_store_by_pieces (tree_low_cst (len, 1),
3860 builtin_strncpy_read_str,
3861 CONST_CAST (char *, p),
3862 dest_align, false))
3863 return NULL_RTX;
3864
3865 dest_mem = get_memory_rtx (dest, len);
3866 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3867 builtin_strncpy_read_str,
3868 CONST_CAST (char *, p), dest_align, false, 0);
3869 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3870 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3871 return dest_mem;
3872 }
3873 }
3874 return NULL_RTX;
3875 }
3876
3877 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3878 bytes from constant string DATA + OFFSET and return it as target
3879 constant. */
3880
3881 rtx
3882 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3883 enum machine_mode mode)
3884 {
3885 const char *c = (const char *) data;
3886 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3887
3888 memset (p, *c, GET_MODE_SIZE (mode));
3889
3890 return c_readstr (p, mode);
3891 }
3892
3893 /* Callback routine for store_by_pieces. Return the RTL of a register
3894 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3895 char value given in the RTL register data. For example, if mode is
3896 4 bytes wide, return the RTL for 0x01010101*data. */
3897
3898 static rtx
3899 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3900 enum machine_mode mode)
3901 {
3902 rtx target, coeff;
3903 size_t size;
3904 char *p;
3905
3906 size = GET_MODE_SIZE (mode);
3907 if (size == 1)
3908 return (rtx) data;
3909
3910 p = XALLOCAVEC (char, size);
3911 memset (p, 1, size);
3912 coeff = c_readstr (p, mode);
3913
3914 target = convert_to_mode (mode, (rtx) data, 1);
3915 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3916 return force_reg (mode, target);
3917 }
3918
3919 /* Expand expression EXP, which is a call to the memset builtin. Return
3920 NULL_RTX if we failed the caller should emit a normal call, otherwise
3921 try to get the result in TARGET, if convenient (and in mode MODE if that's
3922 convenient). */
3923
3924 static rtx
3925 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3926 {
3927 if (!validate_arglist (exp,
3928 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3929 return NULL_RTX;
3930 else
3931 {
3932 tree dest = CALL_EXPR_ARG (exp, 0);
3933 tree val = CALL_EXPR_ARG (exp, 1);
3934 tree len = CALL_EXPR_ARG (exp, 2);
3935 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3936 }
3937 }
3938
3939 /* Helper function to do the actual work for expand_builtin_memset. The
3940 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3941 so that this can also be called without constructing an actual CALL_EXPR.
3942 The other arguments and return value are the same as for
3943 expand_builtin_memset. */
3944
3945 static rtx
3946 expand_builtin_memset_args (tree dest, tree val, tree len,
3947 rtx target, enum machine_mode mode, tree orig_exp)
3948 {
3949 tree fndecl, fn;
3950 enum built_in_function fcode;
3951 enum machine_mode val_mode;
3952 char c;
3953 unsigned int dest_align;
3954 rtx dest_mem, dest_addr, len_rtx;
3955 HOST_WIDE_INT expected_size = -1;
3956 unsigned int expected_align = 0;
3957
3958 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3959
3960 /* If DEST is not a pointer type, don't do this operation in-line. */
3961 if (dest_align == 0)
3962 return NULL_RTX;
3963
3964 if (currently_expanding_gimple_stmt)
3965 stringop_block_profile (currently_expanding_gimple_stmt,
3966 &expected_align, &expected_size);
3967
3968 if (expected_align < dest_align)
3969 expected_align = dest_align;
3970
3971 /* If the LEN parameter is zero, return DEST. */
3972 if (integer_zerop (len))
3973 {
3974 /* Evaluate and ignore VAL in case it has side-effects. */
3975 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3976 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3977 }
3978
3979 /* Stabilize the arguments in case we fail. */
3980 dest = builtin_save_expr (dest);
3981 val = builtin_save_expr (val);
3982 len = builtin_save_expr (len);
3983
3984 len_rtx = expand_normal (len);
3985 dest_mem = get_memory_rtx (dest, len);
3986 val_mode = TYPE_MODE (unsigned_char_type_node);
3987
3988 if (TREE_CODE (val) != INTEGER_CST)
3989 {
3990 rtx val_rtx;
3991
3992 val_rtx = expand_normal (val);
3993 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3994
3995 /* Assume that we can memset by pieces if we can store
3996 * the coefficients by pieces (in the required modes).
3997 * We can't pass builtin_memset_gen_str as that emits RTL. */
3998 c = 1;
3999 if (host_integerp (len, 1)
4000 && can_store_by_pieces (tree_low_cst (len, 1),
4001 builtin_memset_read_str, &c, dest_align,
4002 true))
4003 {
4004 val_rtx = force_reg (val_mode, val_rtx);
4005 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4006 builtin_memset_gen_str, val_rtx, dest_align,
4007 true, 0);
4008 }
4009 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4010 dest_align, expected_align,
4011 expected_size))
4012 goto do_libcall;
4013
4014 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4015 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4016 return dest_mem;
4017 }
4018
4019 if (target_char_cast (val, &c))
4020 goto do_libcall;
4021
4022 if (c)
4023 {
4024 if (host_integerp (len, 1)
4025 && can_store_by_pieces (tree_low_cst (len, 1),
4026 builtin_memset_read_str, &c, dest_align,
4027 true))
4028 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4029 builtin_memset_read_str, &c, dest_align, true, 0);
4030 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4031 gen_int_mode (c, val_mode),
4032 dest_align, expected_align,
4033 expected_size))
4034 goto do_libcall;
4035
4036 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4037 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4038 return dest_mem;
4039 }
4040
4041 set_mem_align (dest_mem, dest_align);
4042 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4043 CALL_EXPR_TAILCALL (orig_exp)
4044 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4045 expected_align, expected_size);
4046
4047 if (dest_addr == 0)
4048 {
4049 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4050 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4051 }
4052
4053 return dest_addr;
4054
4055 do_libcall:
4056 fndecl = get_callee_fndecl (orig_exp);
4057 fcode = DECL_FUNCTION_CODE (fndecl);
4058 if (fcode == BUILT_IN_MEMSET)
4059 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4060 dest, val, len);
4061 else if (fcode == BUILT_IN_BZERO)
4062 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4063 dest, len);
4064 else
4065 gcc_unreachable ();
4066 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4067 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4068 return expand_call (fn, target, target == const0_rtx);
4069 }
4070
4071 /* Expand expression EXP, which is a call to the bzero builtin. Return
4072 NULL_RTX if we failed the caller should emit a normal call. */
4073
4074 static rtx
4075 expand_builtin_bzero (tree exp)
4076 {
4077 tree dest, size;
4078 location_t loc = EXPR_LOCATION (exp);
4079
4080 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4081 return NULL_RTX;
4082
4083 dest = CALL_EXPR_ARG (exp, 0);
4084 size = CALL_EXPR_ARG (exp, 1);
4085
4086 /* New argument list transforming bzero(ptr x, int y) to
4087 memset(ptr x, int 0, size_t y). This is done this way
4088 so that if it isn't expanded inline, we fallback to
4089 calling bzero instead of memset. */
4090
4091 return expand_builtin_memset_args (dest, integer_zero_node,
4092 fold_convert_loc (loc, sizetype, size),
4093 const0_rtx, VOIDmode, exp);
4094 }
4095
4096 /* Expand expression EXP, which is a call to the memcmp built-in function.
4097 Return NULL_RTX if we failed and the
4098 caller should emit a normal call, otherwise try to get the result in
4099 TARGET, if convenient (and in mode MODE, if that's convenient). */
4100
4101 static rtx
4102 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4103 ATTRIBUTE_UNUSED enum machine_mode mode)
4104 {
4105 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4106
4107 if (!validate_arglist (exp,
4108 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4109 return NULL_RTX;
4110
4111 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4112 {
4113 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4114 rtx result;
4115 rtx insn;
4116 tree arg1 = CALL_EXPR_ARG (exp, 0);
4117 tree arg2 = CALL_EXPR_ARG (exp, 1);
4118 tree len = CALL_EXPR_ARG (exp, 2);
4119
4120 unsigned int arg1_align
4121 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4122 unsigned int arg2_align
4123 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4124 enum machine_mode insn_mode;
4125
4126 #ifdef HAVE_cmpmemsi
4127 if (HAVE_cmpmemsi)
4128 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4129 else
4130 #endif
4131 #ifdef HAVE_cmpstrnsi
4132 if (HAVE_cmpstrnsi)
4133 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4134 else
4135 #endif
4136 return NULL_RTX;
4137
4138 /* If we don't have POINTER_TYPE, call the function. */
4139 if (arg1_align == 0 || arg2_align == 0)
4140 return NULL_RTX;
4141
4142 /* Make a place to write the result of the instruction. */
4143 result = target;
4144 if (! (result != 0
4145 && REG_P (result) && GET_MODE (result) == insn_mode
4146 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4147 result = gen_reg_rtx (insn_mode);
4148
4149 arg1_rtx = get_memory_rtx (arg1, len);
4150 arg2_rtx = get_memory_rtx (arg2, len);
4151 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4152
4153 /* Set MEM_SIZE as appropriate. */
4154 if (CONST_INT_P (arg3_rtx))
4155 {
4156 set_mem_size (arg1_rtx, arg3_rtx);
4157 set_mem_size (arg2_rtx, arg3_rtx);
4158 }
4159
4160 #ifdef HAVE_cmpmemsi
4161 if (HAVE_cmpmemsi)
4162 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4163 GEN_INT (MIN (arg1_align, arg2_align)));
4164 else
4165 #endif
4166 #ifdef HAVE_cmpstrnsi
4167 if (HAVE_cmpstrnsi)
4168 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4169 GEN_INT (MIN (arg1_align, arg2_align)));
4170 else
4171 #endif
4172 gcc_unreachable ();
4173
4174 if (insn)
4175 emit_insn (insn);
4176 else
4177 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4178 TYPE_MODE (integer_type_node), 3,
4179 XEXP (arg1_rtx, 0), Pmode,
4180 XEXP (arg2_rtx, 0), Pmode,
4181 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4182 TYPE_UNSIGNED (sizetype)),
4183 TYPE_MODE (sizetype));
4184
4185 /* Return the value in the proper mode for this function. */
4186 mode = TYPE_MODE (TREE_TYPE (exp));
4187 if (GET_MODE (result) == mode)
4188 return result;
4189 else if (target != 0)
4190 {
4191 convert_move (target, result, 0);
4192 return target;
4193 }
4194 else
4195 return convert_to_mode (mode, result, 0);
4196 }
4197 #endif
4198
4199 return NULL_RTX;
4200 }
4201
4202 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4203 if we failed the caller should emit a normal call, otherwise try to get
4204 the result in TARGET, if convenient. */
4205
4206 static rtx
4207 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4208 {
4209 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4210 return NULL_RTX;
4211
4212 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4213 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4214 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4215 {
4216 rtx arg1_rtx, arg2_rtx;
4217 rtx result, insn = NULL_RTX;
4218 tree fndecl, fn;
4219 tree arg1 = CALL_EXPR_ARG (exp, 0);
4220 tree arg2 = CALL_EXPR_ARG (exp, 1);
4221
4222 unsigned int arg1_align
4223 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4224 unsigned int arg2_align
4225 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4226
4227 /* If we don't have POINTER_TYPE, call the function. */
4228 if (arg1_align == 0 || arg2_align == 0)
4229 return NULL_RTX;
4230
4231 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4232 arg1 = builtin_save_expr (arg1);
4233 arg2 = builtin_save_expr (arg2);
4234
4235 arg1_rtx = get_memory_rtx (arg1, NULL);
4236 arg2_rtx = get_memory_rtx (arg2, NULL);
4237
4238 #ifdef HAVE_cmpstrsi
4239 /* Try to call cmpstrsi. */
4240 if (HAVE_cmpstrsi)
4241 {
4242 enum machine_mode insn_mode
4243 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4244
4245 /* Make a place to write the result of the instruction. */
4246 result = target;
4247 if (! (result != 0
4248 && REG_P (result) && GET_MODE (result) == insn_mode
4249 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4250 result = gen_reg_rtx (insn_mode);
4251
4252 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4253 GEN_INT (MIN (arg1_align, arg2_align)));
4254 }
4255 #endif
4256 #ifdef HAVE_cmpstrnsi
4257 /* Try to determine at least one length and call cmpstrnsi. */
4258 if (!insn && HAVE_cmpstrnsi)
4259 {
4260 tree len;
4261 rtx arg3_rtx;
4262
4263 enum machine_mode insn_mode
4264 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4265 tree len1 = c_strlen (arg1, 1);
4266 tree len2 = c_strlen (arg2, 1);
4267
4268 if (len1)
4269 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4270 if (len2)
4271 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4272
4273 /* If we don't have a constant length for the first, use the length
4274 of the second, if we know it. We don't require a constant for
4275 this case; some cost analysis could be done if both are available
4276 but neither is constant. For now, assume they're equally cheap,
4277 unless one has side effects. If both strings have constant lengths,
4278 use the smaller. */
4279
4280 if (!len1)
4281 len = len2;
4282 else if (!len2)
4283 len = len1;
4284 else if (TREE_SIDE_EFFECTS (len1))
4285 len = len2;
4286 else if (TREE_SIDE_EFFECTS (len2))
4287 len = len1;
4288 else if (TREE_CODE (len1) != INTEGER_CST)
4289 len = len2;
4290 else if (TREE_CODE (len2) != INTEGER_CST)
4291 len = len1;
4292 else if (tree_int_cst_lt (len1, len2))
4293 len = len1;
4294 else
4295 len = len2;
4296
4297 /* If both arguments have side effects, we cannot optimize. */
4298 if (!len || TREE_SIDE_EFFECTS (len))
4299 goto do_libcall;
4300
4301 arg3_rtx = expand_normal (len);
4302
4303 /* Make a place to write the result of the instruction. */
4304 result = target;
4305 if (! (result != 0
4306 && REG_P (result) && GET_MODE (result) == insn_mode
4307 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4308 result = gen_reg_rtx (insn_mode);
4309
4310 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4311 GEN_INT (MIN (arg1_align, arg2_align)));
4312 }
4313 #endif
4314
4315 if (insn)
4316 {
4317 enum machine_mode mode;
4318 emit_insn (insn);
4319
4320 /* Return the value in the proper mode for this function. */
4321 mode = TYPE_MODE (TREE_TYPE (exp));
4322 if (GET_MODE (result) == mode)
4323 return result;
4324 if (target == 0)
4325 return convert_to_mode (mode, result, 0);
4326 convert_move (target, result, 0);
4327 return target;
4328 }
4329
4330 /* Expand the library call ourselves using a stabilized argument
4331 list to avoid re-evaluating the function's arguments twice. */
4332 #ifdef HAVE_cmpstrnsi
4333 do_libcall:
4334 #endif
4335 fndecl = get_callee_fndecl (exp);
4336 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4337 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4338 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4339 return expand_call (fn, target, target == const0_rtx);
4340 }
4341 #endif
4342 return NULL_RTX;
4343 }
4344
4345 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4346 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4347 the result in TARGET, if convenient. */
4348
4349 static rtx
4350 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4351 ATTRIBUTE_UNUSED enum machine_mode mode)
4352 {
4353 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4354
4355 if (!validate_arglist (exp,
4356 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4357 return NULL_RTX;
4358
4359 /* If c_strlen can determine an expression for one of the string
4360 lengths, and it doesn't have side effects, then emit cmpstrnsi
4361 using length MIN(strlen(string)+1, arg3). */
4362 #ifdef HAVE_cmpstrnsi
4363 if (HAVE_cmpstrnsi)
4364 {
4365 tree len, len1, len2;
4366 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4367 rtx result, insn;
4368 tree fndecl, fn;
4369 tree arg1 = CALL_EXPR_ARG (exp, 0);
4370 tree arg2 = CALL_EXPR_ARG (exp, 1);
4371 tree arg3 = CALL_EXPR_ARG (exp, 2);
4372
4373 unsigned int arg1_align
4374 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4375 unsigned int arg2_align
4376 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4377 enum machine_mode insn_mode
4378 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4379
4380 len1 = c_strlen (arg1, 1);
4381 len2 = c_strlen (arg2, 1);
4382
4383 if (len1)
4384 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4385 if (len2)
4386 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4387
4388 /* If we don't have a constant length for the first, use the length
4389 of the second, if we know it. We don't require a constant for
4390 this case; some cost analysis could be done if both are available
4391 but neither is constant. For now, assume they're equally cheap,
4392 unless one has side effects. If both strings have constant lengths,
4393 use the smaller. */
4394
4395 if (!len1)
4396 len = len2;
4397 else if (!len2)
4398 len = len1;
4399 else if (TREE_SIDE_EFFECTS (len1))
4400 len = len2;
4401 else if (TREE_SIDE_EFFECTS (len2))
4402 len = len1;
4403 else if (TREE_CODE (len1) != INTEGER_CST)
4404 len = len2;
4405 else if (TREE_CODE (len2) != INTEGER_CST)
4406 len = len1;
4407 else if (tree_int_cst_lt (len1, len2))
4408 len = len1;
4409 else
4410 len = len2;
4411
4412 /* If both arguments have side effects, we cannot optimize. */
4413 if (!len || TREE_SIDE_EFFECTS (len))
4414 return NULL_RTX;
4415
4416 /* The actual new length parameter is MIN(len,arg3). */
4417 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4418 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4419
4420 /* If we don't have POINTER_TYPE, call the function. */
4421 if (arg1_align == 0 || arg2_align == 0)
4422 return NULL_RTX;
4423
4424 /* Make a place to write the result of the instruction. */
4425 result = target;
4426 if (! (result != 0
4427 && REG_P (result) && GET_MODE (result) == insn_mode
4428 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4429 result = gen_reg_rtx (insn_mode);
4430
4431 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4432 arg1 = builtin_save_expr (arg1);
4433 arg2 = builtin_save_expr (arg2);
4434 len = builtin_save_expr (len);
4435
4436 arg1_rtx = get_memory_rtx (arg1, len);
4437 arg2_rtx = get_memory_rtx (arg2, len);
4438 arg3_rtx = expand_normal (len);
4439 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4440 GEN_INT (MIN (arg1_align, arg2_align)));
4441 if (insn)
4442 {
4443 emit_insn (insn);
4444
4445 /* Return the value in the proper mode for this function. */
4446 mode = TYPE_MODE (TREE_TYPE (exp));
4447 if (GET_MODE (result) == mode)
4448 return result;
4449 if (target == 0)
4450 return convert_to_mode (mode, result, 0);
4451 convert_move (target, result, 0);
4452 return target;
4453 }
4454
4455 /* Expand the library call ourselves using a stabilized argument
4456 list to avoid re-evaluating the function's arguments twice. */
4457 fndecl = get_callee_fndecl (exp);
4458 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4459 arg1, arg2, len);
4460 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4461 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4462 return expand_call (fn, target, target == const0_rtx);
4463 }
4464 #endif
4465 return NULL_RTX;
4466 }
4467
4468 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4469 if that's convenient. */
4470
4471 rtx
4472 expand_builtin_saveregs (void)
4473 {
4474 rtx val, seq;
4475
4476 /* Don't do __builtin_saveregs more than once in a function.
4477 Save the result of the first call and reuse it. */
4478 if (saveregs_value != 0)
4479 return saveregs_value;
4480
4481 /* When this function is called, it means that registers must be
4482 saved on entry to this function. So we migrate the call to the
4483 first insn of this function. */
4484
4485 start_sequence ();
4486
4487 /* Do whatever the machine needs done in this case. */
4488 val = targetm.calls.expand_builtin_saveregs ();
4489
4490 seq = get_insns ();
4491 end_sequence ();
4492
4493 saveregs_value = val;
4494
4495 /* Put the insns after the NOTE that starts the function. If this
4496 is inside a start_sequence, make the outer-level insn chain current, so
4497 the code is placed at the start of the function. */
4498 push_topmost_sequence ();
4499 emit_insn_after (seq, entry_of_function ());
4500 pop_topmost_sequence ();
4501
4502 return val;
4503 }
4504
4505 /* Expand a call to __builtin_next_arg. */
4506
4507 static rtx
4508 expand_builtin_next_arg (void)
4509 {
4510 /* Checking arguments is already done in fold_builtin_next_arg
4511 that must be called before this function. */
4512 return expand_binop (ptr_mode, add_optab,
4513 crtl->args.internal_arg_pointer,
4514 crtl->args.arg_offset_rtx,
4515 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4516 }
4517
4518 /* Make it easier for the backends by protecting the valist argument
4519 from multiple evaluations. */
4520
4521 static tree
4522 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4523 {
4524 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4525
4526 /* The current way of determining the type of valist is completely
4527 bogus. We should have the information on the va builtin instead. */
4528 if (!vatype)
4529 vatype = targetm.fn_abi_va_list (cfun->decl);
4530
4531 if (TREE_CODE (vatype) == ARRAY_TYPE)
4532 {
4533 if (TREE_SIDE_EFFECTS (valist))
4534 valist = save_expr (valist);
4535
4536 /* For this case, the backends will be expecting a pointer to
4537 vatype, but it's possible we've actually been given an array
4538 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4539 So fix it. */
4540 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4541 {
4542 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4543 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4544 }
4545 }
4546 else
4547 {
4548 tree pt = build_pointer_type (vatype);
4549
4550 if (! needs_lvalue)
4551 {
4552 if (! TREE_SIDE_EFFECTS (valist))
4553 return valist;
4554
4555 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4556 TREE_SIDE_EFFECTS (valist) = 1;
4557 }
4558
4559 if (TREE_SIDE_EFFECTS (valist))
4560 valist = save_expr (valist);
4561 valist = fold_build2_loc (loc, MEM_REF,
4562 vatype, valist, build_int_cst (pt, 0));
4563 }
4564
4565 return valist;
4566 }
4567
4568 /* The "standard" definition of va_list is void*. */
4569
4570 tree
4571 std_build_builtin_va_list (void)
4572 {
4573 return ptr_type_node;
4574 }
4575
4576 /* The "standard" abi va_list is va_list_type_node. */
4577
4578 tree
4579 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4580 {
4581 return va_list_type_node;
4582 }
4583
4584 /* The "standard" type of va_list is va_list_type_node. */
4585
4586 tree
4587 std_canonical_va_list_type (tree type)
4588 {
4589 tree wtype, htype;
4590
4591 if (INDIRECT_REF_P (type))
4592 type = TREE_TYPE (type);
4593 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4594 type = TREE_TYPE (type);
4595 wtype = va_list_type_node;
4596 htype = type;
4597 /* Treat structure va_list types. */
4598 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4599 htype = TREE_TYPE (htype);
4600 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4601 {
4602 /* If va_list is an array type, the argument may have decayed
4603 to a pointer type, e.g. by being passed to another function.
4604 In that case, unwrap both types so that we can compare the
4605 underlying records. */
4606 if (TREE_CODE (htype) == ARRAY_TYPE
4607 || POINTER_TYPE_P (htype))
4608 {
4609 wtype = TREE_TYPE (wtype);
4610 htype = TREE_TYPE (htype);
4611 }
4612 }
4613 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4614 return va_list_type_node;
4615
4616 return NULL_TREE;
4617 }
4618
4619 /* The "standard" implementation of va_start: just assign `nextarg' to
4620 the variable. */
4621
4622 void
4623 std_expand_builtin_va_start (tree valist, rtx nextarg)
4624 {
4625 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4626 convert_move (va_r, nextarg, 0);
4627 }
4628
4629 /* Expand EXP, a call to __builtin_va_start. */
4630
4631 static rtx
4632 expand_builtin_va_start (tree exp)
4633 {
4634 rtx nextarg;
4635 tree valist;
4636 location_t loc = EXPR_LOCATION (exp);
4637
4638 if (call_expr_nargs (exp) < 2)
4639 {
4640 error_at (loc, "too few arguments to function %<va_start%>");
4641 return const0_rtx;
4642 }
4643
4644 if (fold_builtin_next_arg (exp, true))
4645 return const0_rtx;
4646
4647 nextarg = expand_builtin_next_arg ();
4648 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4649
4650 if (targetm.expand_builtin_va_start)
4651 targetm.expand_builtin_va_start (valist, nextarg);
4652 else
4653 std_expand_builtin_va_start (valist, nextarg);
4654
4655 return const0_rtx;
4656 }
4657
4658 /* The "standard" implementation of va_arg: read the value from the
4659 current (padded) address and increment by the (padded) size. */
4660
4661 tree
4662 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4663 gimple_seq *post_p)
4664 {
4665 tree addr, t, type_size, rounded_size, valist_tmp;
4666 unsigned HOST_WIDE_INT align, boundary;
4667 bool indirect;
4668
4669 #ifdef ARGS_GROW_DOWNWARD
4670 /* All of the alignment and movement below is for args-grow-up machines.
4671 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4672 implement their own specialized gimplify_va_arg_expr routines. */
4673 gcc_unreachable ();
4674 #endif
4675
4676 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4677 if (indirect)
4678 type = build_pointer_type (type);
4679
4680 align = PARM_BOUNDARY / BITS_PER_UNIT;
4681 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4682
4683 /* When we align parameter on stack for caller, if the parameter
4684 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4685 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4686 here with caller. */
4687 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4688 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4689
4690 boundary /= BITS_PER_UNIT;
4691
4692 /* Hoist the valist value into a temporary for the moment. */
4693 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4694
4695 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4696 requires greater alignment, we must perform dynamic alignment. */
4697 if (boundary > align
4698 && !integer_zerop (TYPE_SIZE (type)))
4699 {
4700 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4701 fold_build2 (POINTER_PLUS_EXPR,
4702 TREE_TYPE (valist),
4703 valist_tmp, size_int (boundary - 1)));
4704 gimplify_and_add (t, pre_p);
4705
4706 t = fold_convert (sizetype, valist_tmp);
4707 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4708 fold_convert (TREE_TYPE (valist),
4709 fold_build2 (BIT_AND_EXPR, sizetype, t,
4710 size_int (-boundary))));
4711 gimplify_and_add (t, pre_p);
4712 }
4713 else
4714 boundary = align;
4715
4716 /* If the actual alignment is less than the alignment of the type,
4717 adjust the type accordingly so that we don't assume strict alignment
4718 when dereferencing the pointer. */
4719 boundary *= BITS_PER_UNIT;
4720 if (boundary < TYPE_ALIGN (type))
4721 {
4722 type = build_variant_type_copy (type);
4723 TYPE_ALIGN (type) = boundary;
4724 }
4725
4726 /* Compute the rounded size of the type. */
4727 type_size = size_in_bytes (type);
4728 rounded_size = round_up (type_size, align);
4729
4730 /* Reduce rounded_size so it's sharable with the postqueue. */
4731 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4732
4733 /* Get AP. */
4734 addr = valist_tmp;
4735 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4736 {
4737 /* Small args are padded downward. */
4738 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4739 rounded_size, size_int (align));
4740 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4741 size_binop (MINUS_EXPR, rounded_size, type_size));
4742 addr = fold_build2 (POINTER_PLUS_EXPR,
4743 TREE_TYPE (addr), addr, t);
4744 }
4745
4746 /* Compute new value for AP. */
4747 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4748 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4749 gimplify_and_add (t, pre_p);
4750
4751 addr = fold_convert (build_pointer_type (type), addr);
4752
4753 if (indirect)
4754 addr = build_va_arg_indirect_ref (addr);
4755
4756 return build_va_arg_indirect_ref (addr);
4757 }
4758
4759 /* Build an indirect-ref expression over the given TREE, which represents a
4760 piece of a va_arg() expansion. */
4761 tree
4762 build_va_arg_indirect_ref (tree addr)
4763 {
4764 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4765
4766 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4767 mf_mark (addr);
4768
4769 return addr;
4770 }
4771
4772 /* Return a dummy expression of type TYPE in order to keep going after an
4773 error. */
4774
4775 static tree
4776 dummy_object (tree type)
4777 {
4778 tree t = build_int_cst (build_pointer_type (type), 0);
4779 return build2 (MEM_REF, type, t, t);
4780 }
4781
4782 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4783 builtin function, but a very special sort of operator. */
4784
4785 enum gimplify_status
4786 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4787 {
4788 tree promoted_type, have_va_type;
4789 tree valist = TREE_OPERAND (*expr_p, 0);
4790 tree type = TREE_TYPE (*expr_p);
4791 tree t;
4792 location_t loc = EXPR_LOCATION (*expr_p);
4793
4794 /* Verify that valist is of the proper type. */
4795 have_va_type = TREE_TYPE (valist);
4796 if (have_va_type == error_mark_node)
4797 return GS_ERROR;
4798 have_va_type = targetm.canonical_va_list_type (have_va_type);
4799
4800 if (have_va_type == NULL_TREE)
4801 {
4802 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4803 return GS_ERROR;
4804 }
4805
4806 /* Generate a diagnostic for requesting data of a type that cannot
4807 be passed through `...' due to type promotion at the call site. */
4808 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4809 != type)
4810 {
4811 static bool gave_help;
4812 bool warned;
4813
4814 /* Unfortunately, this is merely undefined, rather than a constraint
4815 violation, so we cannot make this an error. If this call is never
4816 executed, the program is still strictly conforming. */
4817 warned = warning_at (loc, 0,
4818 "%qT is promoted to %qT when passed through %<...%>",
4819 type, promoted_type);
4820 if (!gave_help && warned)
4821 {
4822 gave_help = true;
4823 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4824 promoted_type, type);
4825 }
4826
4827 /* We can, however, treat "undefined" any way we please.
4828 Call abort to encourage the user to fix the program. */
4829 if (warned)
4830 inform (loc, "if this code is reached, the program will abort");
4831 /* Before the abort, allow the evaluation of the va_list
4832 expression to exit or longjmp. */
4833 gimplify_and_add (valist, pre_p);
4834 t = build_call_expr_loc (loc,
4835 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4836 gimplify_and_add (t, pre_p);
4837
4838 /* This is dead code, but go ahead and finish so that the
4839 mode of the result comes out right. */
4840 *expr_p = dummy_object (type);
4841 return GS_ALL_DONE;
4842 }
4843 else
4844 {
4845 /* Make it easier for the backends by protecting the valist argument
4846 from multiple evaluations. */
4847 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4848 {
4849 /* For this case, the backends will be expecting a pointer to
4850 TREE_TYPE (abi), but it's possible we've
4851 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4852 So fix it. */
4853 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4854 {
4855 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4856 valist = fold_convert_loc (loc, p1,
4857 build_fold_addr_expr_loc (loc, valist));
4858 }
4859
4860 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4861 }
4862 else
4863 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4864
4865 if (!targetm.gimplify_va_arg_expr)
4866 /* FIXME: Once most targets are converted we should merely
4867 assert this is non-null. */
4868 return GS_ALL_DONE;
4869
4870 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4871 return GS_OK;
4872 }
4873 }
4874
4875 /* Expand EXP, a call to __builtin_va_end. */
4876
4877 static rtx
4878 expand_builtin_va_end (tree exp)
4879 {
4880 tree valist = CALL_EXPR_ARG (exp, 0);
4881
4882 /* Evaluate for side effects, if needed. I hate macros that don't
4883 do that. */
4884 if (TREE_SIDE_EFFECTS (valist))
4885 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4886
4887 return const0_rtx;
4888 }
4889
4890 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4891 builtin rather than just as an assignment in stdarg.h because of the
4892 nastiness of array-type va_list types. */
4893
4894 static rtx
4895 expand_builtin_va_copy (tree exp)
4896 {
4897 tree dst, src, t;
4898 location_t loc = EXPR_LOCATION (exp);
4899
4900 dst = CALL_EXPR_ARG (exp, 0);
4901 src = CALL_EXPR_ARG (exp, 1);
4902
4903 dst = stabilize_va_list_loc (loc, dst, 1);
4904 src = stabilize_va_list_loc (loc, src, 0);
4905
4906 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4907
4908 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4909 {
4910 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4911 TREE_SIDE_EFFECTS (t) = 1;
4912 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4913 }
4914 else
4915 {
4916 rtx dstb, srcb, size;
4917
4918 /* Evaluate to pointers. */
4919 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4920 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4921 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4922 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4923
4924 dstb = convert_memory_address (Pmode, dstb);
4925 srcb = convert_memory_address (Pmode, srcb);
4926
4927 /* "Dereference" to BLKmode memories. */
4928 dstb = gen_rtx_MEM (BLKmode, dstb);
4929 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4930 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4931 srcb = gen_rtx_MEM (BLKmode, srcb);
4932 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4933 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4934
4935 /* Copy. */
4936 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4937 }
4938
4939 return const0_rtx;
4940 }
4941
4942 /* Expand a call to one of the builtin functions __builtin_frame_address or
4943 __builtin_return_address. */
4944
4945 static rtx
4946 expand_builtin_frame_address (tree fndecl, tree exp)
4947 {
4948 /* The argument must be a nonnegative integer constant.
4949 It counts the number of frames to scan up the stack.
4950 The value is the return address saved in that frame. */
4951 if (call_expr_nargs (exp) == 0)
4952 /* Warning about missing arg was already issued. */
4953 return const0_rtx;
4954 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4955 {
4956 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4957 error ("invalid argument to %<__builtin_frame_address%>");
4958 else
4959 error ("invalid argument to %<__builtin_return_address%>");
4960 return const0_rtx;
4961 }
4962 else
4963 {
4964 rtx tem
4965 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4966 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4967
4968 /* Some ports cannot access arbitrary stack frames. */
4969 if (tem == NULL)
4970 {
4971 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4972 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4973 else
4974 warning (0, "unsupported argument to %<__builtin_return_address%>");
4975 return const0_rtx;
4976 }
4977
4978 /* For __builtin_frame_address, return what we've got. */
4979 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4980 return tem;
4981
4982 if (!REG_P (tem)
4983 && ! CONSTANT_P (tem))
4984 tem = copy_to_mode_reg (Pmode, tem);
4985 return tem;
4986 }
4987 }
4988
4989 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4990 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4991 is the same as for allocate_dynamic_stack_space. */
4992
4993 static rtx
4994 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4995 {
4996 rtx op0;
4997 rtx result;
4998
4999 /* Emit normal call if marked not-inlineable. */
5000 if (CALL_CANNOT_INLINE_P (exp))
5001 return NULL_RTX;
5002
5003 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5004 return NULL_RTX;
5005
5006 /* Compute the argument. */
5007 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5008
5009 /* Allocate the desired space. */
5010 result = allocate_dynamic_stack_space (op0, 0, BIGGEST_ALIGNMENT,
5011 cannot_accumulate);
5012 result = convert_memory_address (ptr_mode, result);
5013
5014 return result;
5015 }
5016
5017 /* Expand a call to a bswap builtin with argument ARG0. MODE
5018 is the mode to expand with. */
5019
5020 static rtx
5021 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5022 {
5023 enum machine_mode mode;
5024 tree arg;
5025 rtx op0;
5026
5027 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5028 return NULL_RTX;
5029
5030 arg = CALL_EXPR_ARG (exp, 0);
5031 mode = TYPE_MODE (TREE_TYPE (arg));
5032 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5033
5034 target = expand_unop (mode, bswap_optab, op0, target, 1);
5035
5036 gcc_assert (target);
5037
5038 return convert_to_mode (mode, target, 0);
5039 }
5040
5041 /* Expand a call to a unary builtin in EXP.
5042 Return NULL_RTX if a normal call should be emitted rather than expanding the
5043 function in-line. If convenient, the result should be placed in TARGET.
5044 SUBTARGET may be used as the target for computing one of EXP's operands. */
5045
5046 static rtx
5047 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5048 rtx subtarget, optab op_optab)
5049 {
5050 rtx op0;
5051
5052 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5053 return NULL_RTX;
5054
5055 /* Compute the argument. */
5056 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5057 (subtarget
5058 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5059 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5060 VOIDmode, EXPAND_NORMAL);
5061 /* Compute op, into TARGET if possible.
5062 Set TARGET to wherever the result comes back. */
5063 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5064 op_optab, op0, target, 1);
5065 gcc_assert (target);
5066
5067 return convert_to_mode (target_mode, target, 0);
5068 }
5069
5070 /* Expand a call to __builtin_expect. We just return our argument
5071 as the builtin_expect semantic should've been already executed by
5072 tree branch prediction pass. */
5073
5074 static rtx
5075 expand_builtin_expect (tree exp, rtx target)
5076 {
5077 tree arg;
5078
5079 if (call_expr_nargs (exp) < 2)
5080 return const0_rtx;
5081 arg = CALL_EXPR_ARG (exp, 0);
5082
5083 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5084 /* When guessing was done, the hints should be already stripped away. */
5085 gcc_assert (!flag_guess_branch_prob
5086 || optimize == 0 || seen_error ());
5087 return target;
5088 }
5089
5090 void
5091 expand_builtin_trap (void)
5092 {
5093 #ifdef HAVE_trap
5094 if (HAVE_trap)
5095 emit_insn (gen_trap ());
5096 else
5097 #endif
5098 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5099 emit_barrier ();
5100 }
5101
5102 /* Expand a call to __builtin_unreachable. We do nothing except emit
5103 a barrier saying that control flow will not pass here.
5104
5105 It is the responsibility of the program being compiled to ensure
5106 that control flow does never reach __builtin_unreachable. */
5107 static void
5108 expand_builtin_unreachable (void)
5109 {
5110 emit_barrier ();
5111 }
5112
5113 /* Expand EXP, a call to fabs, fabsf or fabsl.
5114 Return NULL_RTX if a normal call should be emitted rather than expanding
5115 the function inline. If convenient, the result should be placed
5116 in TARGET. SUBTARGET may be used as the target for computing
5117 the operand. */
5118
5119 static rtx
5120 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5121 {
5122 enum machine_mode mode;
5123 tree arg;
5124 rtx op0;
5125
5126 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5127 return NULL_RTX;
5128
5129 arg = CALL_EXPR_ARG (exp, 0);
5130 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5131 mode = TYPE_MODE (TREE_TYPE (arg));
5132 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5133 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5134 }
5135
5136 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5137 Return NULL is a normal call should be emitted rather than expanding the
5138 function inline. If convenient, the result should be placed in TARGET.
5139 SUBTARGET may be used as the target for computing the operand. */
5140
5141 static rtx
5142 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5143 {
5144 rtx op0, op1;
5145 tree arg;
5146
5147 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5148 return NULL_RTX;
5149
5150 arg = CALL_EXPR_ARG (exp, 0);
5151 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5152
5153 arg = CALL_EXPR_ARG (exp, 1);
5154 op1 = expand_normal (arg);
5155
5156 return expand_copysign (op0, op1, target);
5157 }
5158
5159 /* Create a new constant string literal and return a char* pointer to it.
5160 The STRING_CST value is the LEN characters at STR. */
5161 tree
5162 build_string_literal (int len, const char *str)
5163 {
5164 tree t, elem, index, type;
5165
5166 t = build_string (len, str);
5167 elem = build_type_variant (char_type_node, 1, 0);
5168 index = build_index_type (size_int (len - 1));
5169 type = build_array_type (elem, index);
5170 TREE_TYPE (t) = type;
5171 TREE_CONSTANT (t) = 1;
5172 TREE_READONLY (t) = 1;
5173 TREE_STATIC (t) = 1;
5174
5175 type = build_pointer_type (elem);
5176 t = build1 (ADDR_EXPR, type,
5177 build4 (ARRAY_REF, elem,
5178 t, integer_zero_node, NULL_TREE, NULL_TREE));
5179 return t;
5180 }
5181
5182 /* Expand a call to __builtin___clear_cache. */
5183
5184 static rtx
5185 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5186 {
5187 #ifndef HAVE_clear_cache
5188 #ifdef CLEAR_INSN_CACHE
5189 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5190 does something. Just do the default expansion to a call to
5191 __clear_cache(). */
5192 return NULL_RTX;
5193 #else
5194 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5195 does nothing. There is no need to call it. Do nothing. */
5196 return const0_rtx;
5197 #endif /* CLEAR_INSN_CACHE */
5198 #else
5199 /* We have a "clear_cache" insn, and it will handle everything. */
5200 tree begin, end;
5201 rtx begin_rtx, end_rtx;
5202
5203 /* We must not expand to a library call. If we did, any
5204 fallback library function in libgcc that might contain a call to
5205 __builtin___clear_cache() would recurse infinitely. */
5206 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5207 {
5208 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5209 return const0_rtx;
5210 }
5211
5212 if (HAVE_clear_cache)
5213 {
5214 struct expand_operand ops[2];
5215
5216 begin = CALL_EXPR_ARG (exp, 0);
5217 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5218
5219 end = CALL_EXPR_ARG (exp, 1);
5220 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5221
5222 create_address_operand (&ops[0], begin_rtx);
5223 create_address_operand (&ops[1], end_rtx);
5224 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
5225 return const0_rtx;
5226 }
5227 return const0_rtx;
5228 #endif /* HAVE_clear_cache */
5229 }
5230
5231 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5232
5233 static rtx
5234 round_trampoline_addr (rtx tramp)
5235 {
5236 rtx temp, addend, mask;
5237
5238 /* If we don't need too much alignment, we'll have been guaranteed
5239 proper alignment by get_trampoline_type. */
5240 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5241 return tramp;
5242
5243 /* Round address up to desired boundary. */
5244 temp = gen_reg_rtx (Pmode);
5245 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5246 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5247
5248 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5249 temp, 0, OPTAB_LIB_WIDEN);
5250 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5251 temp, 0, OPTAB_LIB_WIDEN);
5252
5253 return tramp;
5254 }
5255
5256 static rtx
5257 expand_builtin_init_trampoline (tree exp)
5258 {
5259 tree t_tramp, t_func, t_chain;
5260 rtx m_tramp, r_tramp, r_chain, tmp;
5261
5262 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5263 POINTER_TYPE, VOID_TYPE))
5264 return NULL_RTX;
5265
5266 t_tramp = CALL_EXPR_ARG (exp, 0);
5267 t_func = CALL_EXPR_ARG (exp, 1);
5268 t_chain = CALL_EXPR_ARG (exp, 2);
5269
5270 r_tramp = expand_normal (t_tramp);
5271 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5272 MEM_NOTRAP_P (m_tramp) = 1;
5273
5274 /* The TRAMP argument should be the address of a field within the
5275 local function's FRAME decl. Let's see if we can fill in the
5276 to fill in the MEM_ATTRs for this memory. */
5277 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5278 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5279 true, 0);
5280
5281 tmp = round_trampoline_addr (r_tramp);
5282 if (tmp != r_tramp)
5283 {
5284 m_tramp = change_address (m_tramp, BLKmode, tmp);
5285 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5286 set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5287 }
5288
5289 /* The FUNC argument should be the address of the nested function.
5290 Extract the actual function decl to pass to the hook. */
5291 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5292 t_func = TREE_OPERAND (t_func, 0);
5293 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5294
5295 r_chain = expand_normal (t_chain);
5296
5297 /* Generate insns to initialize the trampoline. */
5298 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5299
5300 trampolines_created = 1;
5301
5302 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5303 "trampoline generated for nested function %qD", t_func);
5304
5305 return const0_rtx;
5306 }
5307
5308 static rtx
5309 expand_builtin_adjust_trampoline (tree exp)
5310 {
5311 rtx tramp;
5312
5313 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5314 return NULL_RTX;
5315
5316 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5317 tramp = round_trampoline_addr (tramp);
5318 if (targetm.calls.trampoline_adjust_address)
5319 tramp = targetm.calls.trampoline_adjust_address (tramp);
5320
5321 return tramp;
5322 }
5323
5324 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5325 function. The function first checks whether the back end provides
5326 an insn to implement signbit for the respective mode. If not, it
5327 checks whether the floating point format of the value is such that
5328 the sign bit can be extracted. If that is not the case, the
5329 function returns NULL_RTX to indicate that a normal call should be
5330 emitted rather than expanding the function in-line. EXP is the
5331 expression that is a call to the builtin function; if convenient,
5332 the result should be placed in TARGET. */
5333 static rtx
5334 expand_builtin_signbit (tree exp, rtx target)
5335 {
5336 const struct real_format *fmt;
5337 enum machine_mode fmode, imode, rmode;
5338 tree arg;
5339 int word, bitpos;
5340 enum insn_code icode;
5341 rtx temp;
5342 location_t loc = EXPR_LOCATION (exp);
5343
5344 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5345 return NULL_RTX;
5346
5347 arg = CALL_EXPR_ARG (exp, 0);
5348 fmode = TYPE_MODE (TREE_TYPE (arg));
5349 rmode = TYPE_MODE (TREE_TYPE (exp));
5350 fmt = REAL_MODE_FORMAT (fmode);
5351
5352 arg = builtin_save_expr (arg);
5353
5354 /* Expand the argument yielding a RTX expression. */
5355 temp = expand_normal (arg);
5356
5357 /* Check if the back end provides an insn that handles signbit for the
5358 argument's mode. */
5359 icode = optab_handler (signbit_optab, fmode);
5360 if (icode != CODE_FOR_nothing)
5361 {
5362 rtx last = get_last_insn ();
5363 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5364 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5365 return target;
5366 delete_insns_since (last);
5367 }
5368
5369 /* For floating point formats without a sign bit, implement signbit
5370 as "ARG < 0.0". */
5371 bitpos = fmt->signbit_ro;
5372 if (bitpos < 0)
5373 {
5374 /* But we can't do this if the format supports signed zero. */
5375 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5376 return NULL_RTX;
5377
5378 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5379 build_real (TREE_TYPE (arg), dconst0));
5380 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5381 }
5382
5383 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5384 {
5385 imode = int_mode_for_mode (fmode);
5386 if (imode == BLKmode)
5387 return NULL_RTX;
5388 temp = gen_lowpart (imode, temp);
5389 }
5390 else
5391 {
5392 imode = word_mode;
5393 /* Handle targets with different FP word orders. */
5394 if (FLOAT_WORDS_BIG_ENDIAN)
5395 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5396 else
5397 word = bitpos / BITS_PER_WORD;
5398 temp = operand_subword_force (temp, word, fmode);
5399 bitpos = bitpos % BITS_PER_WORD;
5400 }
5401
5402 /* Force the intermediate word_mode (or narrower) result into a
5403 register. This avoids attempting to create paradoxical SUBREGs
5404 of floating point modes below. */
5405 temp = force_reg (imode, temp);
5406
5407 /* If the bitpos is within the "result mode" lowpart, the operation
5408 can be implement with a single bitwise AND. Otherwise, we need
5409 a right shift and an AND. */
5410
5411 if (bitpos < GET_MODE_BITSIZE (rmode))
5412 {
5413 double_int mask = double_int_setbit (double_int_zero, bitpos);
5414
5415 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5416 temp = gen_lowpart (rmode, temp);
5417 temp = expand_binop (rmode, and_optab, temp,
5418 immed_double_int_const (mask, rmode),
5419 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5420 }
5421 else
5422 {
5423 /* Perform a logical right shift to place the signbit in the least
5424 significant bit, then truncate the result to the desired mode
5425 and mask just this bit. */
5426 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5427 temp = gen_lowpart (rmode, temp);
5428 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5429 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5430 }
5431
5432 return temp;
5433 }
5434
5435 /* Expand fork or exec calls. TARGET is the desired target of the
5436 call. EXP is the call. FN is the
5437 identificator of the actual function. IGNORE is nonzero if the
5438 value is to be ignored. */
5439
5440 static rtx
5441 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5442 {
5443 tree id, decl;
5444 tree call;
5445
5446 /* If we are not profiling, just call the function. */
5447 if (!profile_arc_flag)
5448 return NULL_RTX;
5449
5450 /* Otherwise call the wrapper. This should be equivalent for the rest of
5451 compiler, so the code does not diverge, and the wrapper may run the
5452 code necessary for keeping the profiling sane. */
5453
5454 switch (DECL_FUNCTION_CODE (fn))
5455 {
5456 case BUILT_IN_FORK:
5457 id = get_identifier ("__gcov_fork");
5458 break;
5459
5460 case BUILT_IN_EXECL:
5461 id = get_identifier ("__gcov_execl");
5462 break;
5463
5464 case BUILT_IN_EXECV:
5465 id = get_identifier ("__gcov_execv");
5466 break;
5467
5468 case BUILT_IN_EXECLP:
5469 id = get_identifier ("__gcov_execlp");
5470 break;
5471
5472 case BUILT_IN_EXECLE:
5473 id = get_identifier ("__gcov_execle");
5474 break;
5475
5476 case BUILT_IN_EXECVP:
5477 id = get_identifier ("__gcov_execvp");
5478 break;
5479
5480 case BUILT_IN_EXECVE:
5481 id = get_identifier ("__gcov_execve");
5482 break;
5483
5484 default:
5485 gcc_unreachable ();
5486 }
5487
5488 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5489 FUNCTION_DECL, id, TREE_TYPE (fn));
5490 DECL_EXTERNAL (decl) = 1;
5491 TREE_PUBLIC (decl) = 1;
5492 DECL_ARTIFICIAL (decl) = 1;
5493 TREE_NOTHROW (decl) = 1;
5494 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5495 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5496 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5497 return expand_call (call, target, ignore);
5498 }
5499
5500
5501 \f
5502 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5503 the pointer in these functions is void*, the tree optimizers may remove
5504 casts. The mode computed in expand_builtin isn't reliable either, due
5505 to __sync_bool_compare_and_swap.
5506
5507 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5508 group of builtins. This gives us log2 of the mode size. */
5509
5510 static inline enum machine_mode
5511 get_builtin_sync_mode (int fcode_diff)
5512 {
5513 /* The size is not negotiable, so ask not to get BLKmode in return
5514 if the target indicates that a smaller size would be better. */
5515 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5516 }
5517
5518 /* Expand the memory expression LOC and return the appropriate memory operand
5519 for the builtin_sync operations. */
5520
5521 static rtx
5522 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5523 {
5524 rtx addr, mem;
5525
5526 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5527 addr = convert_memory_address (Pmode, addr);
5528
5529 /* Note that we explicitly do not want any alias information for this
5530 memory, so that we kill all other live memories. Otherwise we don't
5531 satisfy the full barrier semantics of the intrinsic. */
5532 mem = validize_mem (gen_rtx_MEM (mode, addr));
5533
5534 /* The alignment needs to be at least according to that of the mode. */
5535 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5536 get_pointer_alignment (loc, BIGGEST_ALIGNMENT)));
5537 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5538 MEM_VOLATILE_P (mem) = 1;
5539
5540 return mem;
5541 }
5542
5543 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5544 EXP is the CALL_EXPR. CODE is the rtx code
5545 that corresponds to the arithmetic or logical operation from the name;
5546 an exception here is that NOT actually means NAND. TARGET is an optional
5547 place for us to store the results; AFTER is true if this is the
5548 fetch_and_xxx form. IGNORE is true if we don't actually care about
5549 the result of the operation at all. */
5550
5551 static rtx
5552 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5553 enum rtx_code code, bool after,
5554 rtx target, bool ignore)
5555 {
5556 rtx val, mem;
5557 enum machine_mode old_mode;
5558 location_t loc = EXPR_LOCATION (exp);
5559
5560 if (code == NOT && warn_sync_nand)
5561 {
5562 tree fndecl = get_callee_fndecl (exp);
5563 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5564
5565 static bool warned_f_a_n, warned_n_a_f;
5566
5567 switch (fcode)
5568 {
5569 case BUILT_IN_FETCH_AND_NAND_1:
5570 case BUILT_IN_FETCH_AND_NAND_2:
5571 case BUILT_IN_FETCH_AND_NAND_4:
5572 case BUILT_IN_FETCH_AND_NAND_8:
5573 case BUILT_IN_FETCH_AND_NAND_16:
5574
5575 if (warned_f_a_n)
5576 break;
5577
5578 fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5579 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5580 warned_f_a_n = true;
5581 break;
5582
5583 case BUILT_IN_NAND_AND_FETCH_1:
5584 case BUILT_IN_NAND_AND_FETCH_2:
5585 case BUILT_IN_NAND_AND_FETCH_4:
5586 case BUILT_IN_NAND_AND_FETCH_8:
5587 case BUILT_IN_NAND_AND_FETCH_16:
5588
5589 if (warned_n_a_f)
5590 break;
5591
5592 fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5593 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5594 warned_n_a_f = true;
5595 break;
5596
5597 default:
5598 gcc_unreachable ();
5599 }
5600 }
5601
5602 /* Expand the operands. */
5603 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5604
5605 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5606 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5607 of CONST_INTs, where we know the old_mode only from the call argument. */
5608 old_mode = GET_MODE (val);
5609 if (old_mode == VOIDmode)
5610 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5611 val = convert_modes (mode, old_mode, val, 1);
5612
5613 if (ignore)
5614 return expand_sync_operation (mem, val, code);
5615 else
5616 return expand_sync_fetch_operation (mem, val, code, after, target);
5617 }
5618
5619 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5620 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5621 true if this is the boolean form. TARGET is a place for us to store the
5622 results; this is NOT optional if IS_BOOL is true. */
5623
5624 static rtx
5625 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5626 bool is_bool, rtx target)
5627 {
5628 rtx old_val, new_val, mem;
5629 enum machine_mode old_mode;
5630
5631 /* Expand the operands. */
5632 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5633
5634
5635 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5636 mode, EXPAND_NORMAL);
5637 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5638 of CONST_INTs, where we know the old_mode only from the call argument. */
5639 old_mode = GET_MODE (old_val);
5640 if (old_mode == VOIDmode)
5641 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5642 old_val = convert_modes (mode, old_mode, old_val, 1);
5643
5644 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5645 mode, EXPAND_NORMAL);
5646 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5647 of CONST_INTs, where we know the old_mode only from the call argument. */
5648 old_mode = GET_MODE (new_val);
5649 if (old_mode == VOIDmode)
5650 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5651 new_val = convert_modes (mode, old_mode, new_val, 1);
5652
5653 if (is_bool)
5654 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5655 else
5656 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5657 }
5658
5659 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5660 general form is actually an atomic exchange, and some targets only
5661 support a reduced form with the second argument being a constant 1.
5662 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5663 the results. */
5664
5665 static rtx
5666 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5667 rtx target)
5668 {
5669 rtx val, mem;
5670 enum machine_mode old_mode;
5671
5672 /* Expand the operands. */
5673 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5674 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5675 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5676 of CONST_INTs, where we know the old_mode only from the call argument. */
5677 old_mode = GET_MODE (val);
5678 if (old_mode == VOIDmode)
5679 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5680 val = convert_modes (mode, old_mode, val, 1);
5681
5682 return expand_sync_lock_test_and_set (mem, val, target);
5683 }
5684
5685 /* Expand the __sync_synchronize intrinsic. */
5686
5687 static void
5688 expand_builtin_synchronize (void)
5689 {
5690 gimple x;
5691 VEC (tree, gc) *v_clobbers;
5692
5693 #ifdef HAVE_memory_barrier
5694 if (HAVE_memory_barrier)
5695 {
5696 emit_insn (gen_memory_barrier ());
5697 return;
5698 }
5699 #endif
5700
5701 if (synchronize_libfunc != NULL_RTX)
5702 {
5703 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5704 return;
5705 }
5706
5707 /* If no explicit memory barrier instruction is available, create an
5708 empty asm stmt with a memory clobber. */
5709 v_clobbers = VEC_alloc (tree, gc, 1);
5710 VEC_quick_push (tree, v_clobbers,
5711 tree_cons (NULL, build_string (6, "memory"), NULL));
5712 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5713 gimple_asm_set_volatile (x, true);
5714 expand_asm_stmt (x);
5715 }
5716
5717 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5718
5719 static void
5720 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5721 {
5722 struct expand_operand ops[2];
5723 enum insn_code icode;
5724 rtx mem;
5725
5726 /* Expand the operands. */
5727 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5728
5729 /* If there is an explicit operation in the md file, use it. */
5730 icode = direct_optab_handler (sync_lock_release_optab, mode);
5731 if (icode != CODE_FOR_nothing)
5732 {
5733 create_fixed_operand (&ops[0], mem);
5734 create_input_operand (&ops[1], const0_rtx, mode);
5735 if (maybe_expand_insn (icode, 2, ops))
5736 return;
5737 }
5738
5739 /* Otherwise we can implement this operation by emitting a barrier
5740 followed by a store of zero. */
5741 expand_builtin_synchronize ();
5742 emit_move_insn (mem, const0_rtx);
5743 }
5744 \f
5745 /* Expand an expression EXP that calls a built-in function,
5746 with result going to TARGET if that's convenient
5747 (and in mode MODE if that's convenient).
5748 SUBTARGET may be used as the target for computing one of EXP's operands.
5749 IGNORE is nonzero if the value is to be ignored. */
5750
5751 rtx
5752 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5753 int ignore)
5754 {
5755 tree fndecl = get_callee_fndecl (exp);
5756 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5757 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5758 int flags;
5759
5760 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5761 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5762
5763 /* When not optimizing, generate calls to library functions for a certain
5764 set of builtins. */
5765 if (!optimize
5766 && !called_as_built_in (fndecl)
5767 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5768 && fcode != BUILT_IN_ALLOCA
5769 && fcode != BUILT_IN_FREE)
5770 return expand_call (exp, target, ignore);
5771
5772 /* The built-in function expanders test for target == const0_rtx
5773 to determine whether the function's result will be ignored. */
5774 if (ignore)
5775 target = const0_rtx;
5776
5777 /* If the result of a pure or const built-in function is ignored, and
5778 none of its arguments are volatile, we can avoid expanding the
5779 built-in call and just evaluate the arguments for side-effects. */
5780 if (target == const0_rtx
5781 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5782 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5783 {
5784 bool volatilep = false;
5785 tree arg;
5786 call_expr_arg_iterator iter;
5787
5788 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5789 if (TREE_THIS_VOLATILE (arg))
5790 {
5791 volatilep = true;
5792 break;
5793 }
5794
5795 if (! volatilep)
5796 {
5797 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5798 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5799 return const0_rtx;
5800 }
5801 }
5802
5803 switch (fcode)
5804 {
5805 CASE_FLT_FN (BUILT_IN_FABS):
5806 target = expand_builtin_fabs (exp, target, subtarget);
5807 if (target)
5808 return target;
5809 break;
5810
5811 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5812 target = expand_builtin_copysign (exp, target, subtarget);
5813 if (target)
5814 return target;
5815 break;
5816
5817 /* Just do a normal library call if we were unable to fold
5818 the values. */
5819 CASE_FLT_FN (BUILT_IN_CABS):
5820 break;
5821
5822 CASE_FLT_FN (BUILT_IN_EXP):
5823 CASE_FLT_FN (BUILT_IN_EXP10):
5824 CASE_FLT_FN (BUILT_IN_POW10):
5825 CASE_FLT_FN (BUILT_IN_EXP2):
5826 CASE_FLT_FN (BUILT_IN_EXPM1):
5827 CASE_FLT_FN (BUILT_IN_LOGB):
5828 CASE_FLT_FN (BUILT_IN_LOG):
5829 CASE_FLT_FN (BUILT_IN_LOG10):
5830 CASE_FLT_FN (BUILT_IN_LOG2):
5831 CASE_FLT_FN (BUILT_IN_LOG1P):
5832 CASE_FLT_FN (BUILT_IN_TAN):
5833 CASE_FLT_FN (BUILT_IN_ASIN):
5834 CASE_FLT_FN (BUILT_IN_ACOS):
5835 CASE_FLT_FN (BUILT_IN_ATAN):
5836 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5837 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5838 because of possible accuracy problems. */
5839 if (! flag_unsafe_math_optimizations)
5840 break;
5841 CASE_FLT_FN (BUILT_IN_SQRT):
5842 CASE_FLT_FN (BUILT_IN_FLOOR):
5843 CASE_FLT_FN (BUILT_IN_CEIL):
5844 CASE_FLT_FN (BUILT_IN_TRUNC):
5845 CASE_FLT_FN (BUILT_IN_ROUND):
5846 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5847 CASE_FLT_FN (BUILT_IN_RINT):
5848 target = expand_builtin_mathfn (exp, target, subtarget);
5849 if (target)
5850 return target;
5851 break;
5852
5853 CASE_FLT_FN (BUILT_IN_FMA):
5854 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5855 if (target)
5856 return target;
5857 break;
5858
5859 CASE_FLT_FN (BUILT_IN_ILOGB):
5860 if (! flag_unsafe_math_optimizations)
5861 break;
5862 CASE_FLT_FN (BUILT_IN_ISINF):
5863 CASE_FLT_FN (BUILT_IN_FINITE):
5864 case BUILT_IN_ISFINITE:
5865 case BUILT_IN_ISNORMAL:
5866 target = expand_builtin_interclass_mathfn (exp, target);
5867 if (target)
5868 return target;
5869 break;
5870
5871 CASE_FLT_FN (BUILT_IN_LCEIL):
5872 CASE_FLT_FN (BUILT_IN_LLCEIL):
5873 CASE_FLT_FN (BUILT_IN_LFLOOR):
5874 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5875 target = expand_builtin_int_roundingfn (exp, target);
5876 if (target)
5877 return target;
5878 break;
5879
5880 CASE_FLT_FN (BUILT_IN_LRINT):
5881 CASE_FLT_FN (BUILT_IN_LLRINT):
5882 CASE_FLT_FN (BUILT_IN_LROUND):
5883 CASE_FLT_FN (BUILT_IN_LLROUND):
5884 target = expand_builtin_int_roundingfn_2 (exp, target);
5885 if (target)
5886 return target;
5887 break;
5888
5889 CASE_FLT_FN (BUILT_IN_POW):
5890 target = expand_builtin_pow (exp, target, subtarget);
5891 if (target)
5892 return target;
5893 break;
5894
5895 CASE_FLT_FN (BUILT_IN_POWI):
5896 target = expand_builtin_powi (exp, target);
5897 if (target)
5898 return target;
5899 break;
5900
5901 CASE_FLT_FN (BUILT_IN_ATAN2):
5902 CASE_FLT_FN (BUILT_IN_LDEXP):
5903 CASE_FLT_FN (BUILT_IN_SCALB):
5904 CASE_FLT_FN (BUILT_IN_SCALBN):
5905 CASE_FLT_FN (BUILT_IN_SCALBLN):
5906 if (! flag_unsafe_math_optimizations)
5907 break;
5908
5909 CASE_FLT_FN (BUILT_IN_FMOD):
5910 CASE_FLT_FN (BUILT_IN_REMAINDER):
5911 CASE_FLT_FN (BUILT_IN_DREM):
5912 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5913 if (target)
5914 return target;
5915 break;
5916
5917 CASE_FLT_FN (BUILT_IN_CEXPI):
5918 target = expand_builtin_cexpi (exp, target);
5919 gcc_assert (target);
5920 return target;
5921
5922 CASE_FLT_FN (BUILT_IN_SIN):
5923 CASE_FLT_FN (BUILT_IN_COS):
5924 if (! flag_unsafe_math_optimizations)
5925 break;
5926 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5927 if (target)
5928 return target;
5929 break;
5930
5931 CASE_FLT_FN (BUILT_IN_SINCOS):
5932 if (! flag_unsafe_math_optimizations)
5933 break;
5934 target = expand_builtin_sincos (exp);
5935 if (target)
5936 return target;
5937 break;
5938
5939 case BUILT_IN_APPLY_ARGS:
5940 return expand_builtin_apply_args ();
5941
5942 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5943 FUNCTION with a copy of the parameters described by
5944 ARGUMENTS, and ARGSIZE. It returns a block of memory
5945 allocated on the stack into which is stored all the registers
5946 that might possibly be used for returning the result of a
5947 function. ARGUMENTS is the value returned by
5948 __builtin_apply_args. ARGSIZE is the number of bytes of
5949 arguments that must be copied. ??? How should this value be
5950 computed? We'll also need a safe worst case value for varargs
5951 functions. */
5952 case BUILT_IN_APPLY:
5953 if (!validate_arglist (exp, POINTER_TYPE,
5954 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5955 && !validate_arglist (exp, REFERENCE_TYPE,
5956 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5957 return const0_rtx;
5958 else
5959 {
5960 rtx ops[3];
5961
5962 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5963 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5964 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5965
5966 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5967 }
5968
5969 /* __builtin_return (RESULT) causes the function to return the
5970 value described by RESULT. RESULT is address of the block of
5971 memory returned by __builtin_apply. */
5972 case BUILT_IN_RETURN:
5973 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5974 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5975 return const0_rtx;
5976
5977 case BUILT_IN_SAVEREGS:
5978 return expand_builtin_saveregs ();
5979
5980 case BUILT_IN_VA_ARG_PACK:
5981 /* All valid uses of __builtin_va_arg_pack () are removed during
5982 inlining. */
5983 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5984 return const0_rtx;
5985
5986 case BUILT_IN_VA_ARG_PACK_LEN:
5987 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5988 inlining. */
5989 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5990 return const0_rtx;
5991
5992 /* Return the address of the first anonymous stack arg. */
5993 case BUILT_IN_NEXT_ARG:
5994 if (fold_builtin_next_arg (exp, false))
5995 return const0_rtx;
5996 return expand_builtin_next_arg ();
5997
5998 case BUILT_IN_CLEAR_CACHE:
5999 target = expand_builtin___clear_cache (exp);
6000 if (target)
6001 return target;
6002 break;
6003
6004 case BUILT_IN_CLASSIFY_TYPE:
6005 return expand_builtin_classify_type (exp);
6006
6007 case BUILT_IN_CONSTANT_P:
6008 return const0_rtx;
6009
6010 case BUILT_IN_FRAME_ADDRESS:
6011 case BUILT_IN_RETURN_ADDRESS:
6012 return expand_builtin_frame_address (fndecl, exp);
6013
6014 /* Returns the address of the area where the structure is returned.
6015 0 otherwise. */
6016 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6017 if (call_expr_nargs (exp) != 0
6018 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6019 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6020 return const0_rtx;
6021 else
6022 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6023
6024 case BUILT_IN_ALLOCA:
6025 /* If the allocation stems from the declaration of a variable-sized
6026 object, it cannot accumulate. */
6027 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6028 if (target)
6029 return target;
6030 break;
6031
6032 case BUILT_IN_STACK_SAVE:
6033 return expand_stack_save ();
6034
6035 case BUILT_IN_STACK_RESTORE:
6036 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6037 return const0_rtx;
6038
6039 case BUILT_IN_BSWAP32:
6040 case BUILT_IN_BSWAP64:
6041 target = expand_builtin_bswap (exp, target, subtarget);
6042
6043 if (target)
6044 return target;
6045 break;
6046
6047 CASE_INT_FN (BUILT_IN_FFS):
6048 case BUILT_IN_FFSIMAX:
6049 target = expand_builtin_unop (target_mode, exp, target,
6050 subtarget, ffs_optab);
6051 if (target)
6052 return target;
6053 break;
6054
6055 CASE_INT_FN (BUILT_IN_CLZ):
6056 case BUILT_IN_CLZIMAX:
6057 target = expand_builtin_unop (target_mode, exp, target,
6058 subtarget, clz_optab);
6059 if (target)
6060 return target;
6061 break;
6062
6063 CASE_INT_FN (BUILT_IN_CTZ):
6064 case BUILT_IN_CTZIMAX:
6065 target = expand_builtin_unop (target_mode, exp, target,
6066 subtarget, ctz_optab);
6067 if (target)
6068 return target;
6069 break;
6070
6071 CASE_INT_FN (BUILT_IN_POPCOUNT):
6072 case BUILT_IN_POPCOUNTIMAX:
6073 target = expand_builtin_unop (target_mode, exp, target,
6074 subtarget, popcount_optab);
6075 if (target)
6076 return target;
6077 break;
6078
6079 CASE_INT_FN (BUILT_IN_PARITY):
6080 case BUILT_IN_PARITYIMAX:
6081 target = expand_builtin_unop (target_mode, exp, target,
6082 subtarget, parity_optab);
6083 if (target)
6084 return target;
6085 break;
6086
6087 case BUILT_IN_STRLEN:
6088 target = expand_builtin_strlen (exp, target, target_mode);
6089 if (target)
6090 return target;
6091 break;
6092
6093 case BUILT_IN_STRCPY:
6094 target = expand_builtin_strcpy (exp, target);
6095 if (target)
6096 return target;
6097 break;
6098
6099 case BUILT_IN_STRNCPY:
6100 target = expand_builtin_strncpy (exp, target);
6101 if (target)
6102 return target;
6103 break;
6104
6105 case BUILT_IN_STPCPY:
6106 target = expand_builtin_stpcpy (exp, target, mode);
6107 if (target)
6108 return target;
6109 break;
6110
6111 case BUILT_IN_MEMCPY:
6112 target = expand_builtin_memcpy (exp, target);
6113 if (target)
6114 return target;
6115 break;
6116
6117 case BUILT_IN_MEMPCPY:
6118 target = expand_builtin_mempcpy (exp, target, mode);
6119 if (target)
6120 return target;
6121 break;
6122
6123 case BUILT_IN_MEMSET:
6124 target = expand_builtin_memset (exp, target, mode);
6125 if (target)
6126 return target;
6127 break;
6128
6129 case BUILT_IN_BZERO:
6130 target = expand_builtin_bzero (exp);
6131 if (target)
6132 return target;
6133 break;
6134
6135 case BUILT_IN_STRCMP:
6136 target = expand_builtin_strcmp (exp, target);
6137 if (target)
6138 return target;
6139 break;
6140
6141 case BUILT_IN_STRNCMP:
6142 target = expand_builtin_strncmp (exp, target, mode);
6143 if (target)
6144 return target;
6145 break;
6146
6147 case BUILT_IN_BCMP:
6148 case BUILT_IN_MEMCMP:
6149 target = expand_builtin_memcmp (exp, target, mode);
6150 if (target)
6151 return target;
6152 break;
6153
6154 case BUILT_IN_SETJMP:
6155 /* This should have been lowered to the builtins below. */
6156 gcc_unreachable ();
6157
6158 case BUILT_IN_SETJMP_SETUP:
6159 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6160 and the receiver label. */
6161 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6162 {
6163 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6164 VOIDmode, EXPAND_NORMAL);
6165 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6166 rtx label_r = label_rtx (label);
6167
6168 /* This is copied from the handling of non-local gotos. */
6169 expand_builtin_setjmp_setup (buf_addr, label_r);
6170 nonlocal_goto_handler_labels
6171 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6172 nonlocal_goto_handler_labels);
6173 /* ??? Do not let expand_label treat us as such since we would
6174 not want to be both on the list of non-local labels and on
6175 the list of forced labels. */
6176 FORCED_LABEL (label) = 0;
6177 return const0_rtx;
6178 }
6179 break;
6180
6181 case BUILT_IN_SETJMP_DISPATCHER:
6182 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6183 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6184 {
6185 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6186 rtx label_r = label_rtx (label);
6187
6188 /* Remove the dispatcher label from the list of non-local labels
6189 since the receiver labels have been added to it above. */
6190 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6191 return const0_rtx;
6192 }
6193 break;
6194
6195 case BUILT_IN_SETJMP_RECEIVER:
6196 /* __builtin_setjmp_receiver is passed the receiver label. */
6197 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6198 {
6199 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6200 rtx label_r = label_rtx (label);
6201
6202 expand_builtin_setjmp_receiver (label_r);
6203 return const0_rtx;
6204 }
6205 break;
6206
6207 /* __builtin_longjmp is passed a pointer to an array of five words.
6208 It's similar to the C library longjmp function but works with
6209 __builtin_setjmp above. */
6210 case BUILT_IN_LONGJMP:
6211 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6212 {
6213 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6214 VOIDmode, EXPAND_NORMAL);
6215 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6216
6217 if (value != const1_rtx)
6218 {
6219 error ("%<__builtin_longjmp%> second argument must be 1");
6220 return const0_rtx;
6221 }
6222
6223 expand_builtin_longjmp (buf_addr, value);
6224 return const0_rtx;
6225 }
6226 break;
6227
6228 case BUILT_IN_NONLOCAL_GOTO:
6229 target = expand_builtin_nonlocal_goto (exp);
6230 if (target)
6231 return target;
6232 break;
6233
6234 /* This updates the setjmp buffer that is its argument with the value
6235 of the current stack pointer. */
6236 case BUILT_IN_UPDATE_SETJMP_BUF:
6237 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6238 {
6239 rtx buf_addr
6240 = expand_normal (CALL_EXPR_ARG (exp, 0));
6241
6242 expand_builtin_update_setjmp_buf (buf_addr);
6243 return const0_rtx;
6244 }
6245 break;
6246
6247 case BUILT_IN_TRAP:
6248 expand_builtin_trap ();
6249 return const0_rtx;
6250
6251 case BUILT_IN_UNREACHABLE:
6252 expand_builtin_unreachable ();
6253 return const0_rtx;
6254
6255 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6256 case BUILT_IN_SIGNBITD32:
6257 case BUILT_IN_SIGNBITD64:
6258 case BUILT_IN_SIGNBITD128:
6259 target = expand_builtin_signbit (exp, target);
6260 if (target)
6261 return target;
6262 break;
6263
6264 /* Various hooks for the DWARF 2 __throw routine. */
6265 case BUILT_IN_UNWIND_INIT:
6266 expand_builtin_unwind_init ();
6267 return const0_rtx;
6268 case BUILT_IN_DWARF_CFA:
6269 return virtual_cfa_rtx;
6270 #ifdef DWARF2_UNWIND_INFO
6271 case BUILT_IN_DWARF_SP_COLUMN:
6272 return expand_builtin_dwarf_sp_column ();
6273 case BUILT_IN_INIT_DWARF_REG_SIZES:
6274 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6275 return const0_rtx;
6276 #endif
6277 case BUILT_IN_FROB_RETURN_ADDR:
6278 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6279 case BUILT_IN_EXTRACT_RETURN_ADDR:
6280 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6281 case BUILT_IN_EH_RETURN:
6282 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6283 CALL_EXPR_ARG (exp, 1));
6284 return const0_rtx;
6285 #ifdef EH_RETURN_DATA_REGNO
6286 case BUILT_IN_EH_RETURN_DATA_REGNO:
6287 return expand_builtin_eh_return_data_regno (exp);
6288 #endif
6289 case BUILT_IN_EXTEND_POINTER:
6290 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6291 case BUILT_IN_EH_POINTER:
6292 return expand_builtin_eh_pointer (exp);
6293 case BUILT_IN_EH_FILTER:
6294 return expand_builtin_eh_filter (exp);
6295 case BUILT_IN_EH_COPY_VALUES:
6296 return expand_builtin_eh_copy_values (exp);
6297
6298 case BUILT_IN_VA_START:
6299 return expand_builtin_va_start (exp);
6300 case BUILT_IN_VA_END:
6301 return expand_builtin_va_end (exp);
6302 case BUILT_IN_VA_COPY:
6303 return expand_builtin_va_copy (exp);
6304 case BUILT_IN_EXPECT:
6305 return expand_builtin_expect (exp, target);
6306 case BUILT_IN_PREFETCH:
6307 expand_builtin_prefetch (exp);
6308 return const0_rtx;
6309
6310 case BUILT_IN_INIT_TRAMPOLINE:
6311 return expand_builtin_init_trampoline (exp);
6312 case BUILT_IN_ADJUST_TRAMPOLINE:
6313 return expand_builtin_adjust_trampoline (exp);
6314
6315 case BUILT_IN_FORK:
6316 case BUILT_IN_EXECL:
6317 case BUILT_IN_EXECV:
6318 case BUILT_IN_EXECLP:
6319 case BUILT_IN_EXECLE:
6320 case BUILT_IN_EXECVP:
6321 case BUILT_IN_EXECVE:
6322 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6323 if (target)
6324 return target;
6325 break;
6326
6327 case BUILT_IN_FETCH_AND_ADD_1:
6328 case BUILT_IN_FETCH_AND_ADD_2:
6329 case BUILT_IN_FETCH_AND_ADD_4:
6330 case BUILT_IN_FETCH_AND_ADD_8:
6331 case BUILT_IN_FETCH_AND_ADD_16:
6332 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6333 target = expand_builtin_sync_operation (mode, exp, PLUS,
6334 false, target, ignore);
6335 if (target)
6336 return target;
6337 break;
6338
6339 case BUILT_IN_FETCH_AND_SUB_1:
6340 case BUILT_IN_FETCH_AND_SUB_2:
6341 case BUILT_IN_FETCH_AND_SUB_4:
6342 case BUILT_IN_FETCH_AND_SUB_8:
6343 case BUILT_IN_FETCH_AND_SUB_16:
6344 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6345 target = expand_builtin_sync_operation (mode, exp, MINUS,
6346 false, target, ignore);
6347 if (target)
6348 return target;
6349 break;
6350
6351 case BUILT_IN_FETCH_AND_OR_1:
6352 case BUILT_IN_FETCH_AND_OR_2:
6353 case BUILT_IN_FETCH_AND_OR_4:
6354 case BUILT_IN_FETCH_AND_OR_8:
6355 case BUILT_IN_FETCH_AND_OR_16:
6356 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6357 target = expand_builtin_sync_operation (mode, exp, IOR,
6358 false, target, ignore);
6359 if (target)
6360 return target;
6361 break;
6362
6363 case BUILT_IN_FETCH_AND_AND_1:
6364 case BUILT_IN_FETCH_AND_AND_2:
6365 case BUILT_IN_FETCH_AND_AND_4:
6366 case BUILT_IN_FETCH_AND_AND_8:
6367 case BUILT_IN_FETCH_AND_AND_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6369 target = expand_builtin_sync_operation (mode, exp, AND,
6370 false, target, ignore);
6371 if (target)
6372 return target;
6373 break;
6374
6375 case BUILT_IN_FETCH_AND_XOR_1:
6376 case BUILT_IN_FETCH_AND_XOR_2:
6377 case BUILT_IN_FETCH_AND_XOR_4:
6378 case BUILT_IN_FETCH_AND_XOR_8:
6379 case BUILT_IN_FETCH_AND_XOR_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6381 target = expand_builtin_sync_operation (mode, exp, XOR,
6382 false, target, ignore);
6383 if (target)
6384 return target;
6385 break;
6386
6387 case BUILT_IN_FETCH_AND_NAND_1:
6388 case BUILT_IN_FETCH_AND_NAND_2:
6389 case BUILT_IN_FETCH_AND_NAND_4:
6390 case BUILT_IN_FETCH_AND_NAND_8:
6391 case BUILT_IN_FETCH_AND_NAND_16:
6392 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6393 target = expand_builtin_sync_operation (mode, exp, NOT,
6394 false, target, ignore);
6395 if (target)
6396 return target;
6397 break;
6398
6399 case BUILT_IN_ADD_AND_FETCH_1:
6400 case BUILT_IN_ADD_AND_FETCH_2:
6401 case BUILT_IN_ADD_AND_FETCH_4:
6402 case BUILT_IN_ADD_AND_FETCH_8:
6403 case BUILT_IN_ADD_AND_FETCH_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6405 target = expand_builtin_sync_operation (mode, exp, PLUS,
6406 true, target, ignore);
6407 if (target)
6408 return target;
6409 break;
6410
6411 case BUILT_IN_SUB_AND_FETCH_1:
6412 case BUILT_IN_SUB_AND_FETCH_2:
6413 case BUILT_IN_SUB_AND_FETCH_4:
6414 case BUILT_IN_SUB_AND_FETCH_8:
6415 case BUILT_IN_SUB_AND_FETCH_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6417 target = expand_builtin_sync_operation (mode, exp, MINUS,
6418 true, target, ignore);
6419 if (target)
6420 return target;
6421 break;
6422
6423 case BUILT_IN_OR_AND_FETCH_1:
6424 case BUILT_IN_OR_AND_FETCH_2:
6425 case BUILT_IN_OR_AND_FETCH_4:
6426 case BUILT_IN_OR_AND_FETCH_8:
6427 case BUILT_IN_OR_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, exp, IOR,
6430 true, target, ignore);
6431 if (target)
6432 return target;
6433 break;
6434
6435 case BUILT_IN_AND_AND_FETCH_1:
6436 case BUILT_IN_AND_AND_FETCH_2:
6437 case BUILT_IN_AND_AND_FETCH_4:
6438 case BUILT_IN_AND_AND_FETCH_8:
6439 case BUILT_IN_AND_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, exp, AND,
6442 true, target, ignore);
6443 if (target)
6444 return target;
6445 break;
6446
6447 case BUILT_IN_XOR_AND_FETCH_1:
6448 case BUILT_IN_XOR_AND_FETCH_2:
6449 case BUILT_IN_XOR_AND_FETCH_4:
6450 case BUILT_IN_XOR_AND_FETCH_8:
6451 case BUILT_IN_XOR_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, exp, XOR,
6454 true, target, ignore);
6455 if (target)
6456 return target;
6457 break;
6458
6459 case BUILT_IN_NAND_AND_FETCH_1:
6460 case BUILT_IN_NAND_AND_FETCH_2:
6461 case BUILT_IN_NAND_AND_FETCH_4:
6462 case BUILT_IN_NAND_AND_FETCH_8:
6463 case BUILT_IN_NAND_AND_FETCH_16:
6464 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6465 target = expand_builtin_sync_operation (mode, exp, NOT,
6466 true, target, ignore);
6467 if (target)
6468 return target;
6469 break;
6470
6471 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6472 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6473 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6474 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6475 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6476 if (mode == VOIDmode)
6477 mode = TYPE_MODE (boolean_type_node);
6478 if (!target || !register_operand (target, mode))
6479 target = gen_reg_rtx (mode);
6480
6481 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6482 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6483 if (target)
6484 return target;
6485 break;
6486
6487 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6488 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6489 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6490 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6491 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6492 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6493 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6494 if (target)
6495 return target;
6496 break;
6497
6498 case BUILT_IN_LOCK_TEST_AND_SET_1:
6499 case BUILT_IN_LOCK_TEST_AND_SET_2:
6500 case BUILT_IN_LOCK_TEST_AND_SET_4:
6501 case BUILT_IN_LOCK_TEST_AND_SET_8:
6502 case BUILT_IN_LOCK_TEST_AND_SET_16:
6503 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6504 target = expand_builtin_lock_test_and_set (mode, exp, target);
6505 if (target)
6506 return target;
6507 break;
6508
6509 case BUILT_IN_LOCK_RELEASE_1:
6510 case BUILT_IN_LOCK_RELEASE_2:
6511 case BUILT_IN_LOCK_RELEASE_4:
6512 case BUILT_IN_LOCK_RELEASE_8:
6513 case BUILT_IN_LOCK_RELEASE_16:
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6515 expand_builtin_lock_release (mode, exp);
6516 return const0_rtx;
6517
6518 case BUILT_IN_SYNCHRONIZE:
6519 expand_builtin_synchronize ();
6520 return const0_rtx;
6521
6522 case BUILT_IN_OBJECT_SIZE:
6523 return expand_builtin_object_size (exp);
6524
6525 case BUILT_IN_MEMCPY_CHK:
6526 case BUILT_IN_MEMPCPY_CHK:
6527 case BUILT_IN_MEMMOVE_CHK:
6528 case BUILT_IN_MEMSET_CHK:
6529 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6530 if (target)
6531 return target;
6532 break;
6533
6534 case BUILT_IN_STRCPY_CHK:
6535 case BUILT_IN_STPCPY_CHK:
6536 case BUILT_IN_STRNCPY_CHK:
6537 case BUILT_IN_STRCAT_CHK:
6538 case BUILT_IN_STRNCAT_CHK:
6539 case BUILT_IN_SNPRINTF_CHK:
6540 case BUILT_IN_VSNPRINTF_CHK:
6541 maybe_emit_chk_warning (exp, fcode);
6542 break;
6543
6544 case BUILT_IN_SPRINTF_CHK:
6545 case BUILT_IN_VSPRINTF_CHK:
6546 maybe_emit_sprintf_chk_warning (exp, fcode);
6547 break;
6548
6549 case BUILT_IN_FREE:
6550 maybe_emit_free_warning (exp);
6551 break;
6552
6553 default: /* just do library call, if unknown builtin */
6554 break;
6555 }
6556
6557 /* The switch statement above can drop through to cause the function
6558 to be called normally. */
6559 return expand_call (exp, target, ignore);
6560 }
6561
6562 /* Determine whether a tree node represents a call to a built-in
6563 function. If the tree T is a call to a built-in function with
6564 the right number of arguments of the appropriate types, return
6565 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6566 Otherwise the return value is END_BUILTINS. */
6567
6568 enum built_in_function
6569 builtin_mathfn_code (const_tree t)
6570 {
6571 const_tree fndecl, arg, parmlist;
6572 const_tree argtype, parmtype;
6573 const_call_expr_arg_iterator iter;
6574
6575 if (TREE_CODE (t) != CALL_EXPR
6576 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6577 return END_BUILTINS;
6578
6579 fndecl = get_callee_fndecl (t);
6580 if (fndecl == NULL_TREE
6581 || TREE_CODE (fndecl) != FUNCTION_DECL
6582 || ! DECL_BUILT_IN (fndecl)
6583 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6584 return END_BUILTINS;
6585
6586 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6587 init_const_call_expr_arg_iterator (t, &iter);
6588 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6589 {
6590 /* If a function doesn't take a variable number of arguments,
6591 the last element in the list will have type `void'. */
6592 parmtype = TREE_VALUE (parmlist);
6593 if (VOID_TYPE_P (parmtype))
6594 {
6595 if (more_const_call_expr_args_p (&iter))
6596 return END_BUILTINS;
6597 return DECL_FUNCTION_CODE (fndecl);
6598 }
6599
6600 if (! more_const_call_expr_args_p (&iter))
6601 return END_BUILTINS;
6602
6603 arg = next_const_call_expr_arg (&iter);
6604 argtype = TREE_TYPE (arg);
6605
6606 if (SCALAR_FLOAT_TYPE_P (parmtype))
6607 {
6608 if (! SCALAR_FLOAT_TYPE_P (argtype))
6609 return END_BUILTINS;
6610 }
6611 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6612 {
6613 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6614 return END_BUILTINS;
6615 }
6616 else if (POINTER_TYPE_P (parmtype))
6617 {
6618 if (! POINTER_TYPE_P (argtype))
6619 return END_BUILTINS;
6620 }
6621 else if (INTEGRAL_TYPE_P (parmtype))
6622 {
6623 if (! INTEGRAL_TYPE_P (argtype))
6624 return END_BUILTINS;
6625 }
6626 else
6627 return END_BUILTINS;
6628 }
6629
6630 /* Variable-length argument list. */
6631 return DECL_FUNCTION_CODE (fndecl);
6632 }
6633
6634 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6635 evaluate to a constant. */
6636
6637 static tree
6638 fold_builtin_constant_p (tree arg)
6639 {
6640 /* We return 1 for a numeric type that's known to be a constant
6641 value at compile-time or for an aggregate type that's a
6642 literal constant. */
6643 STRIP_NOPS (arg);
6644
6645 /* If we know this is a constant, emit the constant of one. */
6646 if (CONSTANT_CLASS_P (arg)
6647 || (TREE_CODE (arg) == CONSTRUCTOR
6648 && TREE_CONSTANT (arg)))
6649 return integer_one_node;
6650 if (TREE_CODE (arg) == ADDR_EXPR)
6651 {
6652 tree op = TREE_OPERAND (arg, 0);
6653 if (TREE_CODE (op) == STRING_CST
6654 || (TREE_CODE (op) == ARRAY_REF
6655 && integer_zerop (TREE_OPERAND (op, 1))
6656 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6657 return integer_one_node;
6658 }
6659
6660 /* If this expression has side effects, show we don't know it to be a
6661 constant. Likewise if it's a pointer or aggregate type since in
6662 those case we only want literals, since those are only optimized
6663 when generating RTL, not later.
6664 And finally, if we are compiling an initializer, not code, we
6665 need to return a definite result now; there's not going to be any
6666 more optimization done. */
6667 if (TREE_SIDE_EFFECTS (arg)
6668 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6669 || POINTER_TYPE_P (TREE_TYPE (arg))
6670 || cfun == 0
6671 || folding_initializer)
6672 return integer_zero_node;
6673
6674 return NULL_TREE;
6675 }
6676
6677 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6678 return it as a truthvalue. */
6679
6680 static tree
6681 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6682 {
6683 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6684
6685 fn = built_in_decls[BUILT_IN_EXPECT];
6686 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6687 ret_type = TREE_TYPE (TREE_TYPE (fn));
6688 pred_type = TREE_VALUE (arg_types);
6689 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6690
6691 pred = fold_convert_loc (loc, pred_type, pred);
6692 expected = fold_convert_loc (loc, expected_type, expected);
6693 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6694
6695 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6696 build_int_cst (ret_type, 0));
6697 }
6698
6699 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6700 NULL_TREE if no simplification is possible. */
6701
6702 static tree
6703 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6704 {
6705 tree inner, fndecl;
6706 enum tree_code code;
6707
6708 /* If this is a builtin_expect within a builtin_expect keep the
6709 inner one. See through a comparison against a constant. It
6710 might have been added to create a thruthvalue. */
6711 inner = arg0;
6712 if (COMPARISON_CLASS_P (inner)
6713 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6714 inner = TREE_OPERAND (inner, 0);
6715
6716 if (TREE_CODE (inner) == CALL_EXPR
6717 && (fndecl = get_callee_fndecl (inner))
6718 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6719 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6720 return arg0;
6721
6722 /* Distribute the expected value over short-circuiting operators.
6723 See through the cast from truthvalue_type_node to long. */
6724 inner = arg0;
6725 while (TREE_CODE (inner) == NOP_EXPR
6726 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6727 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6728 inner = TREE_OPERAND (inner, 0);
6729
6730 code = TREE_CODE (inner);
6731 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6732 {
6733 tree op0 = TREE_OPERAND (inner, 0);
6734 tree op1 = TREE_OPERAND (inner, 1);
6735
6736 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6737 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6738 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6739
6740 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6741 }
6742
6743 /* If the argument isn't invariant then there's nothing else we can do. */
6744 if (!TREE_CONSTANT (arg0))
6745 return NULL_TREE;
6746
6747 /* If we expect that a comparison against the argument will fold to
6748 a constant return the constant. In practice, this means a true
6749 constant or the address of a non-weak symbol. */
6750 inner = arg0;
6751 STRIP_NOPS (inner);
6752 if (TREE_CODE (inner) == ADDR_EXPR)
6753 {
6754 do
6755 {
6756 inner = TREE_OPERAND (inner, 0);
6757 }
6758 while (TREE_CODE (inner) == COMPONENT_REF
6759 || TREE_CODE (inner) == ARRAY_REF);
6760 if ((TREE_CODE (inner) == VAR_DECL
6761 || TREE_CODE (inner) == FUNCTION_DECL)
6762 && DECL_WEAK (inner))
6763 return NULL_TREE;
6764 }
6765
6766 /* Otherwise, ARG0 already has the proper type for the return value. */
6767 return arg0;
6768 }
6769
6770 /* Fold a call to __builtin_classify_type with argument ARG. */
6771
6772 static tree
6773 fold_builtin_classify_type (tree arg)
6774 {
6775 if (arg == 0)
6776 return build_int_cst (NULL_TREE, no_type_class);
6777
6778 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
6779 }
6780
6781 /* Fold a call to __builtin_strlen with argument ARG. */
6782
6783 static tree
6784 fold_builtin_strlen (location_t loc, tree type, tree arg)
6785 {
6786 if (!validate_arg (arg, POINTER_TYPE))
6787 return NULL_TREE;
6788 else
6789 {
6790 tree len = c_strlen (arg, 0);
6791
6792 if (len)
6793 return fold_convert_loc (loc, type, len);
6794
6795 return NULL_TREE;
6796 }
6797 }
6798
6799 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6800
6801 static tree
6802 fold_builtin_inf (location_t loc, tree type, int warn)
6803 {
6804 REAL_VALUE_TYPE real;
6805
6806 /* __builtin_inff is intended to be usable to define INFINITY on all
6807 targets. If an infinity is not available, INFINITY expands "to a
6808 positive constant of type float that overflows at translation
6809 time", footnote "In this case, using INFINITY will violate the
6810 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6811 Thus we pedwarn to ensure this constraint violation is
6812 diagnosed. */
6813 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6814 pedwarn (loc, 0, "target format does not support infinity");
6815
6816 real_inf (&real);
6817 return build_real (type, real);
6818 }
6819
6820 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6821
6822 static tree
6823 fold_builtin_nan (tree arg, tree type, int quiet)
6824 {
6825 REAL_VALUE_TYPE real;
6826 const char *str;
6827
6828 if (!validate_arg (arg, POINTER_TYPE))
6829 return NULL_TREE;
6830 str = c_getstr (arg);
6831 if (!str)
6832 return NULL_TREE;
6833
6834 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6835 return NULL_TREE;
6836
6837 return build_real (type, real);
6838 }
6839
6840 /* Return true if the floating point expression T has an integer value.
6841 We also allow +Inf, -Inf and NaN to be considered integer values. */
6842
6843 static bool
6844 integer_valued_real_p (tree t)
6845 {
6846 switch (TREE_CODE (t))
6847 {
6848 case FLOAT_EXPR:
6849 return true;
6850
6851 case ABS_EXPR:
6852 case SAVE_EXPR:
6853 return integer_valued_real_p (TREE_OPERAND (t, 0));
6854
6855 case COMPOUND_EXPR:
6856 case MODIFY_EXPR:
6857 case BIND_EXPR:
6858 return integer_valued_real_p (TREE_OPERAND (t, 1));
6859
6860 case PLUS_EXPR:
6861 case MINUS_EXPR:
6862 case MULT_EXPR:
6863 case MIN_EXPR:
6864 case MAX_EXPR:
6865 return integer_valued_real_p (TREE_OPERAND (t, 0))
6866 && integer_valued_real_p (TREE_OPERAND (t, 1));
6867
6868 case COND_EXPR:
6869 return integer_valued_real_p (TREE_OPERAND (t, 1))
6870 && integer_valued_real_p (TREE_OPERAND (t, 2));
6871
6872 case REAL_CST:
6873 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6874
6875 case NOP_EXPR:
6876 {
6877 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6878 if (TREE_CODE (type) == INTEGER_TYPE)
6879 return true;
6880 if (TREE_CODE (type) == REAL_TYPE)
6881 return integer_valued_real_p (TREE_OPERAND (t, 0));
6882 break;
6883 }
6884
6885 case CALL_EXPR:
6886 switch (builtin_mathfn_code (t))
6887 {
6888 CASE_FLT_FN (BUILT_IN_CEIL):
6889 CASE_FLT_FN (BUILT_IN_FLOOR):
6890 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6891 CASE_FLT_FN (BUILT_IN_RINT):
6892 CASE_FLT_FN (BUILT_IN_ROUND):
6893 CASE_FLT_FN (BUILT_IN_TRUNC):
6894 return true;
6895
6896 CASE_FLT_FN (BUILT_IN_FMIN):
6897 CASE_FLT_FN (BUILT_IN_FMAX):
6898 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6899 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6900
6901 default:
6902 break;
6903 }
6904 break;
6905
6906 default:
6907 break;
6908 }
6909 return false;
6910 }
6911
6912 /* FNDECL is assumed to be a builtin where truncation can be propagated
6913 across (for instance floor((double)f) == (double)floorf (f).
6914 Do the transformation for a call with argument ARG. */
6915
6916 static tree
6917 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6918 {
6919 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6920
6921 if (!validate_arg (arg, REAL_TYPE))
6922 return NULL_TREE;
6923
6924 /* Integer rounding functions are idempotent. */
6925 if (fcode == builtin_mathfn_code (arg))
6926 return arg;
6927
6928 /* If argument is already integer valued, and we don't need to worry
6929 about setting errno, there's no need to perform rounding. */
6930 if (! flag_errno_math && integer_valued_real_p (arg))
6931 return arg;
6932
6933 if (optimize)
6934 {
6935 tree arg0 = strip_float_extensions (arg);
6936 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6937 tree newtype = TREE_TYPE (arg0);
6938 tree decl;
6939
6940 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6941 && (decl = mathfn_built_in (newtype, fcode)))
6942 return fold_convert_loc (loc, ftype,
6943 build_call_expr_loc (loc, decl, 1,
6944 fold_convert_loc (loc,
6945 newtype,
6946 arg0)));
6947 }
6948 return NULL_TREE;
6949 }
6950
6951 /* FNDECL is assumed to be builtin which can narrow the FP type of
6952 the argument, for instance lround((double)f) -> lroundf (f).
6953 Do the transformation for a call with argument ARG. */
6954
6955 static tree
6956 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
6957 {
6958 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6959
6960 if (!validate_arg (arg, REAL_TYPE))
6961 return NULL_TREE;
6962
6963 /* If argument is already integer valued, and we don't need to worry
6964 about setting errno, there's no need to perform rounding. */
6965 if (! flag_errno_math && integer_valued_real_p (arg))
6966 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
6967 TREE_TYPE (TREE_TYPE (fndecl)), arg);
6968
6969 if (optimize)
6970 {
6971 tree ftype = TREE_TYPE (arg);
6972 tree arg0 = strip_float_extensions (arg);
6973 tree newtype = TREE_TYPE (arg0);
6974 tree decl;
6975
6976 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6977 && (decl = mathfn_built_in (newtype, fcode)))
6978 return build_call_expr_loc (loc, decl, 1,
6979 fold_convert_loc (loc, newtype, arg0));
6980 }
6981
6982 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6983 sizeof (long long) == sizeof (long). */
6984 if (TYPE_PRECISION (long_long_integer_type_node)
6985 == TYPE_PRECISION (long_integer_type_node))
6986 {
6987 tree newfn = NULL_TREE;
6988 switch (fcode)
6989 {
6990 CASE_FLT_FN (BUILT_IN_LLCEIL):
6991 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6992 break;
6993
6994 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6995 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6996 break;
6997
6998 CASE_FLT_FN (BUILT_IN_LLROUND):
6999 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7000 break;
7001
7002 CASE_FLT_FN (BUILT_IN_LLRINT):
7003 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7004 break;
7005
7006 default:
7007 break;
7008 }
7009
7010 if (newfn)
7011 {
7012 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7013 return fold_convert_loc (loc,
7014 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7015 }
7016 }
7017
7018 return NULL_TREE;
7019 }
7020
7021 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7022 return type. Return NULL_TREE if no simplification can be made. */
7023
7024 static tree
7025 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7026 {
7027 tree res;
7028
7029 if (!validate_arg (arg, COMPLEX_TYPE)
7030 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7031 return NULL_TREE;
7032
7033 /* Calculate the result when the argument is a constant. */
7034 if (TREE_CODE (arg) == COMPLEX_CST
7035 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7036 type, mpfr_hypot)))
7037 return res;
7038
7039 if (TREE_CODE (arg) == COMPLEX_EXPR)
7040 {
7041 tree real = TREE_OPERAND (arg, 0);
7042 tree imag = TREE_OPERAND (arg, 1);
7043
7044 /* If either part is zero, cabs is fabs of the other. */
7045 if (real_zerop (real))
7046 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7047 if (real_zerop (imag))
7048 return fold_build1_loc (loc, ABS_EXPR, type, real);
7049
7050 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7051 if (flag_unsafe_math_optimizations
7052 && operand_equal_p (real, imag, OEP_PURE_SAME))
7053 {
7054 const REAL_VALUE_TYPE sqrt2_trunc
7055 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7056 STRIP_NOPS (real);
7057 return fold_build2_loc (loc, MULT_EXPR, type,
7058 fold_build1_loc (loc, ABS_EXPR, type, real),
7059 build_real (type, sqrt2_trunc));
7060 }
7061 }
7062
7063 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7064 if (TREE_CODE (arg) == NEGATE_EXPR
7065 || TREE_CODE (arg) == CONJ_EXPR)
7066 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7067
7068 /* Don't do this when optimizing for size. */
7069 if (flag_unsafe_math_optimizations
7070 && optimize && optimize_function_for_speed_p (cfun))
7071 {
7072 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7073
7074 if (sqrtfn != NULL_TREE)
7075 {
7076 tree rpart, ipart, result;
7077
7078 arg = builtin_save_expr (arg);
7079
7080 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7081 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7082
7083 rpart = builtin_save_expr (rpart);
7084 ipart = builtin_save_expr (ipart);
7085
7086 result = fold_build2_loc (loc, PLUS_EXPR, type,
7087 fold_build2_loc (loc, MULT_EXPR, type,
7088 rpart, rpart),
7089 fold_build2_loc (loc, MULT_EXPR, type,
7090 ipart, ipart));
7091
7092 return build_call_expr_loc (loc, sqrtfn, 1, result);
7093 }
7094 }
7095
7096 return NULL_TREE;
7097 }
7098
7099 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7100 complex tree type of the result. If NEG is true, the imaginary
7101 zero is negative. */
7102
7103 static tree
7104 build_complex_cproj (tree type, bool neg)
7105 {
7106 REAL_VALUE_TYPE rinf, rzero = dconst0;
7107
7108 real_inf (&rinf);
7109 rzero.sign = neg;
7110 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7111 build_real (TREE_TYPE (type), rzero));
7112 }
7113
7114 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7115 return type. Return NULL_TREE if no simplification can be made. */
7116
7117 static tree
7118 fold_builtin_cproj (location_t loc, tree arg, tree type)
7119 {
7120 if (!validate_arg (arg, COMPLEX_TYPE)
7121 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7122 return NULL_TREE;
7123
7124 /* If there are no infinities, return arg. */
7125 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7126 return non_lvalue_loc (loc, arg);
7127
7128 /* Calculate the result when the argument is a constant. */
7129 if (TREE_CODE (arg) == COMPLEX_CST)
7130 {
7131 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7132 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7133
7134 if (real_isinf (real) || real_isinf (imag))
7135 return build_complex_cproj (type, imag->sign);
7136 else
7137 return arg;
7138 }
7139 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7140 {
7141 tree real = TREE_OPERAND (arg, 0);
7142 tree imag = TREE_OPERAND (arg, 1);
7143
7144 STRIP_NOPS (real);
7145 STRIP_NOPS (imag);
7146
7147 /* If the real part is inf and the imag part is known to be
7148 nonnegative, return (inf + 0i). Remember side-effects are
7149 possible in the imag part. */
7150 if (TREE_CODE (real) == REAL_CST
7151 && real_isinf (TREE_REAL_CST_PTR (real))
7152 && tree_expr_nonnegative_p (imag))
7153 return omit_one_operand_loc (loc, type,
7154 build_complex_cproj (type, false),
7155 arg);
7156
7157 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7158 Remember side-effects are possible in the real part. */
7159 if (TREE_CODE (imag) == REAL_CST
7160 && real_isinf (TREE_REAL_CST_PTR (imag)))
7161 return
7162 omit_one_operand_loc (loc, type,
7163 build_complex_cproj (type, TREE_REAL_CST_PTR
7164 (imag)->sign), arg);
7165 }
7166
7167 return NULL_TREE;
7168 }
7169
7170 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7171 Return NULL_TREE if no simplification can be made. */
7172
7173 static tree
7174 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7175 {
7176
7177 enum built_in_function fcode;
7178 tree res;
7179
7180 if (!validate_arg (arg, REAL_TYPE))
7181 return NULL_TREE;
7182
7183 /* Calculate the result when the argument is a constant. */
7184 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7185 return res;
7186
7187 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7188 fcode = builtin_mathfn_code (arg);
7189 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7190 {
7191 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7192 arg = fold_build2_loc (loc, MULT_EXPR, type,
7193 CALL_EXPR_ARG (arg, 0),
7194 build_real (type, dconsthalf));
7195 return build_call_expr_loc (loc, expfn, 1, arg);
7196 }
7197
7198 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7199 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7200 {
7201 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7202
7203 if (powfn)
7204 {
7205 tree arg0 = CALL_EXPR_ARG (arg, 0);
7206 tree tree_root;
7207 /* The inner root was either sqrt or cbrt. */
7208 /* This was a conditional expression but it triggered a bug
7209 in Sun C 5.5. */
7210 REAL_VALUE_TYPE dconstroot;
7211 if (BUILTIN_SQRT_P (fcode))
7212 dconstroot = dconsthalf;
7213 else
7214 dconstroot = dconst_third ();
7215
7216 /* Adjust for the outer root. */
7217 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7218 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7219 tree_root = build_real (type, dconstroot);
7220 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7221 }
7222 }
7223
7224 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7225 if (flag_unsafe_math_optimizations
7226 && (fcode == BUILT_IN_POW
7227 || fcode == BUILT_IN_POWF
7228 || fcode == BUILT_IN_POWL))
7229 {
7230 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7231 tree arg0 = CALL_EXPR_ARG (arg, 0);
7232 tree arg1 = CALL_EXPR_ARG (arg, 1);
7233 tree narg1;
7234 if (!tree_expr_nonnegative_p (arg0))
7235 arg0 = build1 (ABS_EXPR, type, arg0);
7236 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7237 build_real (type, dconsthalf));
7238 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7239 }
7240
7241 return NULL_TREE;
7242 }
7243
7244 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7245 Return NULL_TREE if no simplification can be made. */
7246
7247 static tree
7248 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7249 {
7250 const enum built_in_function fcode = builtin_mathfn_code (arg);
7251 tree res;
7252
7253 if (!validate_arg (arg, REAL_TYPE))
7254 return NULL_TREE;
7255
7256 /* Calculate the result when the argument is a constant. */
7257 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7258 return res;
7259
7260 if (flag_unsafe_math_optimizations)
7261 {
7262 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7263 if (BUILTIN_EXPONENT_P (fcode))
7264 {
7265 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7266 const REAL_VALUE_TYPE third_trunc =
7267 real_value_truncate (TYPE_MODE (type), dconst_third ());
7268 arg = fold_build2_loc (loc, MULT_EXPR, type,
7269 CALL_EXPR_ARG (arg, 0),
7270 build_real (type, third_trunc));
7271 return build_call_expr_loc (loc, expfn, 1, arg);
7272 }
7273
7274 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7275 if (BUILTIN_SQRT_P (fcode))
7276 {
7277 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7278
7279 if (powfn)
7280 {
7281 tree arg0 = CALL_EXPR_ARG (arg, 0);
7282 tree tree_root;
7283 REAL_VALUE_TYPE dconstroot = dconst_third ();
7284
7285 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7286 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7287 tree_root = build_real (type, dconstroot);
7288 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7289 }
7290 }
7291
7292 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7293 if (BUILTIN_CBRT_P (fcode))
7294 {
7295 tree arg0 = CALL_EXPR_ARG (arg, 0);
7296 if (tree_expr_nonnegative_p (arg0))
7297 {
7298 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7299
7300 if (powfn)
7301 {
7302 tree tree_root;
7303 REAL_VALUE_TYPE dconstroot;
7304
7305 real_arithmetic (&dconstroot, MULT_EXPR,
7306 dconst_third_ptr (), dconst_third_ptr ());
7307 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7308 tree_root = build_real (type, dconstroot);
7309 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7310 }
7311 }
7312 }
7313
7314 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7315 if (fcode == BUILT_IN_POW
7316 || fcode == BUILT_IN_POWF
7317 || fcode == BUILT_IN_POWL)
7318 {
7319 tree arg00 = CALL_EXPR_ARG (arg, 0);
7320 tree arg01 = CALL_EXPR_ARG (arg, 1);
7321 if (tree_expr_nonnegative_p (arg00))
7322 {
7323 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7324 const REAL_VALUE_TYPE dconstroot
7325 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7326 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7327 build_real (type, dconstroot));
7328 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7329 }
7330 }
7331 }
7332 return NULL_TREE;
7333 }
7334
7335 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7336 TYPE is the type of the return value. Return NULL_TREE if no
7337 simplification can be made. */
7338
7339 static tree
7340 fold_builtin_cos (location_t loc,
7341 tree arg, tree type, tree fndecl)
7342 {
7343 tree res, narg;
7344
7345 if (!validate_arg (arg, REAL_TYPE))
7346 return NULL_TREE;
7347
7348 /* Calculate the result when the argument is a constant. */
7349 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7350 return res;
7351
7352 /* Optimize cos(-x) into cos (x). */
7353 if ((narg = fold_strip_sign_ops (arg)))
7354 return build_call_expr_loc (loc, fndecl, 1, narg);
7355
7356 return NULL_TREE;
7357 }
7358
7359 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7360 Return NULL_TREE if no simplification can be made. */
7361
7362 static tree
7363 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7364 {
7365 if (validate_arg (arg, REAL_TYPE))
7366 {
7367 tree res, narg;
7368
7369 /* Calculate the result when the argument is a constant. */
7370 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7371 return res;
7372
7373 /* Optimize cosh(-x) into cosh (x). */
7374 if ((narg = fold_strip_sign_ops (arg)))
7375 return build_call_expr_loc (loc, fndecl, 1, narg);
7376 }
7377
7378 return NULL_TREE;
7379 }
7380
7381 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7382 argument ARG. TYPE is the type of the return value. Return
7383 NULL_TREE if no simplification can be made. */
7384
7385 static tree
7386 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7387 bool hyper)
7388 {
7389 if (validate_arg (arg, COMPLEX_TYPE)
7390 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7391 {
7392 tree tmp;
7393
7394 /* Calculate the result when the argument is a constant. */
7395 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7396 return tmp;
7397
7398 /* Optimize fn(-x) into fn(x). */
7399 if ((tmp = fold_strip_sign_ops (arg)))
7400 return build_call_expr_loc (loc, fndecl, 1, tmp);
7401 }
7402
7403 return NULL_TREE;
7404 }
7405
7406 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7407 Return NULL_TREE if no simplification can be made. */
7408
7409 static tree
7410 fold_builtin_tan (tree arg, tree type)
7411 {
7412 enum built_in_function fcode;
7413 tree res;
7414
7415 if (!validate_arg (arg, REAL_TYPE))
7416 return NULL_TREE;
7417
7418 /* Calculate the result when the argument is a constant. */
7419 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7420 return res;
7421
7422 /* Optimize tan(atan(x)) = x. */
7423 fcode = builtin_mathfn_code (arg);
7424 if (flag_unsafe_math_optimizations
7425 && (fcode == BUILT_IN_ATAN
7426 || fcode == BUILT_IN_ATANF
7427 || fcode == BUILT_IN_ATANL))
7428 return CALL_EXPR_ARG (arg, 0);
7429
7430 return NULL_TREE;
7431 }
7432
7433 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7434 NULL_TREE if no simplification can be made. */
7435
7436 static tree
7437 fold_builtin_sincos (location_t loc,
7438 tree arg0, tree arg1, tree arg2)
7439 {
7440 tree type;
7441 tree res, fn, call;
7442
7443 if (!validate_arg (arg0, REAL_TYPE)
7444 || !validate_arg (arg1, POINTER_TYPE)
7445 || !validate_arg (arg2, POINTER_TYPE))
7446 return NULL_TREE;
7447
7448 type = TREE_TYPE (arg0);
7449
7450 /* Calculate the result when the argument is a constant. */
7451 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7452 return res;
7453
7454 /* Canonicalize sincos to cexpi. */
7455 if (!TARGET_C99_FUNCTIONS)
7456 return NULL_TREE;
7457 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7458 if (!fn)
7459 return NULL_TREE;
7460
7461 call = build_call_expr_loc (loc, fn, 1, arg0);
7462 call = builtin_save_expr (call);
7463
7464 return build2 (COMPOUND_EXPR, void_type_node,
7465 build2 (MODIFY_EXPR, void_type_node,
7466 build_fold_indirect_ref_loc (loc, arg1),
7467 build1 (IMAGPART_EXPR, type, call)),
7468 build2 (MODIFY_EXPR, void_type_node,
7469 build_fold_indirect_ref_loc (loc, arg2),
7470 build1 (REALPART_EXPR, type, call)));
7471 }
7472
7473 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7474 NULL_TREE if no simplification can be made. */
7475
7476 static tree
7477 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7478 {
7479 tree rtype;
7480 tree realp, imagp, ifn;
7481 tree res;
7482
7483 if (!validate_arg (arg0, COMPLEX_TYPE)
7484 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7485 return NULL_TREE;
7486
7487 /* Calculate the result when the argument is a constant. */
7488 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7489 return res;
7490
7491 rtype = TREE_TYPE (TREE_TYPE (arg0));
7492
7493 /* In case we can figure out the real part of arg0 and it is constant zero
7494 fold to cexpi. */
7495 if (!TARGET_C99_FUNCTIONS)
7496 return NULL_TREE;
7497 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7498 if (!ifn)
7499 return NULL_TREE;
7500
7501 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7502 && real_zerop (realp))
7503 {
7504 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7505 return build_call_expr_loc (loc, ifn, 1, narg);
7506 }
7507
7508 /* In case we can easily decompose real and imaginary parts split cexp
7509 to exp (r) * cexpi (i). */
7510 if (flag_unsafe_math_optimizations
7511 && realp)
7512 {
7513 tree rfn, rcall, icall;
7514
7515 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7516 if (!rfn)
7517 return NULL_TREE;
7518
7519 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7520 if (!imagp)
7521 return NULL_TREE;
7522
7523 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7524 icall = builtin_save_expr (icall);
7525 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7526 rcall = builtin_save_expr (rcall);
7527 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7528 fold_build2_loc (loc, MULT_EXPR, rtype,
7529 rcall,
7530 fold_build1_loc (loc, REALPART_EXPR,
7531 rtype, icall)),
7532 fold_build2_loc (loc, MULT_EXPR, rtype,
7533 rcall,
7534 fold_build1_loc (loc, IMAGPART_EXPR,
7535 rtype, icall)));
7536 }
7537
7538 return NULL_TREE;
7539 }
7540
7541 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7542 Return NULL_TREE if no simplification can be made. */
7543
7544 static tree
7545 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7546 {
7547 if (!validate_arg (arg, REAL_TYPE))
7548 return NULL_TREE;
7549
7550 /* Optimize trunc of constant value. */
7551 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7552 {
7553 REAL_VALUE_TYPE r, x;
7554 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7555
7556 x = TREE_REAL_CST (arg);
7557 real_trunc (&r, TYPE_MODE (type), &x);
7558 return build_real (type, r);
7559 }
7560
7561 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7562 }
7563
7564 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7565 Return NULL_TREE if no simplification can be made. */
7566
7567 static tree
7568 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7569 {
7570 if (!validate_arg (arg, REAL_TYPE))
7571 return NULL_TREE;
7572
7573 /* Optimize floor of constant value. */
7574 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7575 {
7576 REAL_VALUE_TYPE x;
7577
7578 x = TREE_REAL_CST (arg);
7579 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7580 {
7581 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7582 REAL_VALUE_TYPE r;
7583
7584 real_floor (&r, TYPE_MODE (type), &x);
7585 return build_real (type, r);
7586 }
7587 }
7588
7589 /* Fold floor (x) where x is nonnegative to trunc (x). */
7590 if (tree_expr_nonnegative_p (arg))
7591 {
7592 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7593 if (truncfn)
7594 return build_call_expr_loc (loc, truncfn, 1, arg);
7595 }
7596
7597 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7598 }
7599
7600 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7601 Return NULL_TREE if no simplification can be made. */
7602
7603 static tree
7604 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7605 {
7606 if (!validate_arg (arg, REAL_TYPE))
7607 return NULL_TREE;
7608
7609 /* Optimize ceil of constant value. */
7610 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7611 {
7612 REAL_VALUE_TYPE x;
7613
7614 x = TREE_REAL_CST (arg);
7615 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7616 {
7617 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7618 REAL_VALUE_TYPE r;
7619
7620 real_ceil (&r, TYPE_MODE (type), &x);
7621 return build_real (type, r);
7622 }
7623 }
7624
7625 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7626 }
7627
7628 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7629 Return NULL_TREE if no simplification can be made. */
7630
7631 static tree
7632 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7633 {
7634 if (!validate_arg (arg, REAL_TYPE))
7635 return NULL_TREE;
7636
7637 /* Optimize round of constant value. */
7638 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7639 {
7640 REAL_VALUE_TYPE x;
7641
7642 x = TREE_REAL_CST (arg);
7643 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7644 {
7645 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7646 REAL_VALUE_TYPE r;
7647
7648 real_round (&r, TYPE_MODE (type), &x);
7649 return build_real (type, r);
7650 }
7651 }
7652
7653 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7654 }
7655
7656 /* Fold function call to builtin lround, lroundf or lroundl (or the
7657 corresponding long long versions) and other rounding functions. ARG
7658 is the argument to the call. Return NULL_TREE if no simplification
7659 can be made. */
7660
7661 static tree
7662 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7663 {
7664 if (!validate_arg (arg, REAL_TYPE))
7665 return NULL_TREE;
7666
7667 /* Optimize lround of constant value. */
7668 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7669 {
7670 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7671
7672 if (real_isfinite (&x))
7673 {
7674 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7675 tree ftype = TREE_TYPE (arg);
7676 double_int val;
7677 REAL_VALUE_TYPE r;
7678
7679 switch (DECL_FUNCTION_CODE (fndecl))
7680 {
7681 CASE_FLT_FN (BUILT_IN_LFLOOR):
7682 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7683 real_floor (&r, TYPE_MODE (ftype), &x);
7684 break;
7685
7686 CASE_FLT_FN (BUILT_IN_LCEIL):
7687 CASE_FLT_FN (BUILT_IN_LLCEIL):
7688 real_ceil (&r, TYPE_MODE (ftype), &x);
7689 break;
7690
7691 CASE_FLT_FN (BUILT_IN_LROUND):
7692 CASE_FLT_FN (BUILT_IN_LLROUND):
7693 real_round (&r, TYPE_MODE (ftype), &x);
7694 break;
7695
7696 default:
7697 gcc_unreachable ();
7698 }
7699
7700 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7701 if (double_int_fits_to_tree_p (itype, val))
7702 return double_int_to_tree (itype, val);
7703 }
7704 }
7705
7706 switch (DECL_FUNCTION_CODE (fndecl))
7707 {
7708 CASE_FLT_FN (BUILT_IN_LFLOOR):
7709 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7710 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7711 if (tree_expr_nonnegative_p (arg))
7712 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7713 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7714 break;
7715 default:;
7716 }
7717
7718 return fold_fixed_mathfn (loc, fndecl, arg);
7719 }
7720
7721 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7722 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7723 the argument to the call. Return NULL_TREE if no simplification can
7724 be made. */
7725
7726 static tree
7727 fold_builtin_bitop (tree fndecl, tree arg)
7728 {
7729 if (!validate_arg (arg, INTEGER_TYPE))
7730 return NULL_TREE;
7731
7732 /* Optimize for constant argument. */
7733 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7734 {
7735 HOST_WIDE_INT hi, width, result;
7736 unsigned HOST_WIDE_INT lo;
7737 tree type;
7738
7739 type = TREE_TYPE (arg);
7740 width = TYPE_PRECISION (type);
7741 lo = TREE_INT_CST_LOW (arg);
7742
7743 /* Clear all the bits that are beyond the type's precision. */
7744 if (width > HOST_BITS_PER_WIDE_INT)
7745 {
7746 hi = TREE_INT_CST_HIGH (arg);
7747 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7748 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7749 }
7750 else
7751 {
7752 hi = 0;
7753 if (width < HOST_BITS_PER_WIDE_INT)
7754 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7755 }
7756
7757 switch (DECL_FUNCTION_CODE (fndecl))
7758 {
7759 CASE_INT_FN (BUILT_IN_FFS):
7760 if (lo != 0)
7761 result = ffs_hwi (lo);
7762 else if (hi != 0)
7763 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
7764 else
7765 result = 0;
7766 break;
7767
7768 CASE_INT_FN (BUILT_IN_CLZ):
7769 if (hi != 0)
7770 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7771 else if (lo != 0)
7772 result = width - floor_log2 (lo) - 1;
7773 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7774 result = width;
7775 break;
7776
7777 CASE_INT_FN (BUILT_IN_CTZ):
7778 if (lo != 0)
7779 result = ctz_hwi (lo);
7780 else if (hi != 0)
7781 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
7782 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7783 result = width;
7784 break;
7785
7786 CASE_INT_FN (BUILT_IN_POPCOUNT):
7787 result = 0;
7788 while (lo)
7789 result++, lo &= lo - 1;
7790 while (hi)
7791 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7792 break;
7793
7794 CASE_INT_FN (BUILT_IN_PARITY):
7795 result = 0;
7796 while (lo)
7797 result++, lo &= lo - 1;
7798 while (hi)
7799 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7800 result &= 1;
7801 break;
7802
7803 default:
7804 gcc_unreachable ();
7805 }
7806
7807 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7808 }
7809
7810 return NULL_TREE;
7811 }
7812
7813 /* Fold function call to builtin_bswap and the long and long long
7814 variants. Return NULL_TREE if no simplification can be made. */
7815 static tree
7816 fold_builtin_bswap (tree fndecl, tree arg)
7817 {
7818 if (! validate_arg (arg, INTEGER_TYPE))
7819 return NULL_TREE;
7820
7821 /* Optimize constant value. */
7822 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7823 {
7824 HOST_WIDE_INT hi, width, r_hi = 0;
7825 unsigned HOST_WIDE_INT lo, r_lo = 0;
7826 tree type;
7827
7828 type = TREE_TYPE (arg);
7829 width = TYPE_PRECISION (type);
7830 lo = TREE_INT_CST_LOW (arg);
7831 hi = TREE_INT_CST_HIGH (arg);
7832
7833 switch (DECL_FUNCTION_CODE (fndecl))
7834 {
7835 case BUILT_IN_BSWAP32:
7836 case BUILT_IN_BSWAP64:
7837 {
7838 int s;
7839
7840 for (s = 0; s < width; s += 8)
7841 {
7842 int d = width - s - 8;
7843 unsigned HOST_WIDE_INT byte;
7844
7845 if (s < HOST_BITS_PER_WIDE_INT)
7846 byte = (lo >> s) & 0xff;
7847 else
7848 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7849
7850 if (d < HOST_BITS_PER_WIDE_INT)
7851 r_lo |= byte << d;
7852 else
7853 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7854 }
7855 }
7856
7857 break;
7858
7859 default:
7860 gcc_unreachable ();
7861 }
7862
7863 if (width < HOST_BITS_PER_WIDE_INT)
7864 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7865 else
7866 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7867 }
7868
7869 return NULL_TREE;
7870 }
7871
7872 /* A subroutine of fold_builtin to fold the various logarithmic
7873 functions. Return NULL_TREE if no simplification can me made.
7874 FUNC is the corresponding MPFR logarithm function. */
7875
7876 static tree
7877 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7878 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7879 {
7880 if (validate_arg (arg, REAL_TYPE))
7881 {
7882 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7883 tree res;
7884 const enum built_in_function fcode = builtin_mathfn_code (arg);
7885
7886 /* Calculate the result when the argument is a constant. */
7887 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7888 return res;
7889
7890 /* Special case, optimize logN(expN(x)) = x. */
7891 if (flag_unsafe_math_optimizations
7892 && ((func == mpfr_log
7893 && (fcode == BUILT_IN_EXP
7894 || fcode == BUILT_IN_EXPF
7895 || fcode == BUILT_IN_EXPL))
7896 || (func == mpfr_log2
7897 && (fcode == BUILT_IN_EXP2
7898 || fcode == BUILT_IN_EXP2F
7899 || fcode == BUILT_IN_EXP2L))
7900 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7901 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7902
7903 /* Optimize logN(func()) for various exponential functions. We
7904 want to determine the value "x" and the power "exponent" in
7905 order to transform logN(x**exponent) into exponent*logN(x). */
7906 if (flag_unsafe_math_optimizations)
7907 {
7908 tree exponent = 0, x = 0;
7909
7910 switch (fcode)
7911 {
7912 CASE_FLT_FN (BUILT_IN_EXP):
7913 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7914 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7915 dconst_e ()));
7916 exponent = CALL_EXPR_ARG (arg, 0);
7917 break;
7918 CASE_FLT_FN (BUILT_IN_EXP2):
7919 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7920 x = build_real (type, dconst2);
7921 exponent = CALL_EXPR_ARG (arg, 0);
7922 break;
7923 CASE_FLT_FN (BUILT_IN_EXP10):
7924 CASE_FLT_FN (BUILT_IN_POW10):
7925 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7926 {
7927 REAL_VALUE_TYPE dconst10;
7928 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7929 x = build_real (type, dconst10);
7930 }
7931 exponent = CALL_EXPR_ARG (arg, 0);
7932 break;
7933 CASE_FLT_FN (BUILT_IN_SQRT):
7934 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7935 x = CALL_EXPR_ARG (arg, 0);
7936 exponent = build_real (type, dconsthalf);
7937 break;
7938 CASE_FLT_FN (BUILT_IN_CBRT):
7939 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7940 x = CALL_EXPR_ARG (arg, 0);
7941 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7942 dconst_third ()));
7943 break;
7944 CASE_FLT_FN (BUILT_IN_POW):
7945 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7946 x = CALL_EXPR_ARG (arg, 0);
7947 exponent = CALL_EXPR_ARG (arg, 1);
7948 break;
7949 default:
7950 break;
7951 }
7952
7953 /* Now perform the optimization. */
7954 if (x && exponent)
7955 {
7956 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
7957 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
7958 }
7959 }
7960 }
7961
7962 return NULL_TREE;
7963 }
7964
7965 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7966 NULL_TREE if no simplification can be made. */
7967
7968 static tree
7969 fold_builtin_hypot (location_t loc, tree fndecl,
7970 tree arg0, tree arg1, tree type)
7971 {
7972 tree res, narg0, narg1;
7973
7974 if (!validate_arg (arg0, REAL_TYPE)
7975 || !validate_arg (arg1, REAL_TYPE))
7976 return NULL_TREE;
7977
7978 /* Calculate the result when the argument is a constant. */
7979 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7980 return res;
7981
7982 /* If either argument to hypot has a negate or abs, strip that off.
7983 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7984 narg0 = fold_strip_sign_ops (arg0);
7985 narg1 = fold_strip_sign_ops (arg1);
7986 if (narg0 || narg1)
7987 {
7988 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
7989 narg1 ? narg1 : arg1);
7990 }
7991
7992 /* If either argument is zero, hypot is fabs of the other. */
7993 if (real_zerop (arg0))
7994 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
7995 else if (real_zerop (arg1))
7996 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
7997
7998 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7999 if (flag_unsafe_math_optimizations
8000 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8001 {
8002 const REAL_VALUE_TYPE sqrt2_trunc
8003 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8004 return fold_build2_loc (loc, MULT_EXPR, type,
8005 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8006 build_real (type, sqrt2_trunc));
8007 }
8008
8009 return NULL_TREE;
8010 }
8011
8012
8013 /* Fold a builtin function call to pow, powf, or powl. Return
8014 NULL_TREE if no simplification can be made. */
8015 static tree
8016 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8017 {
8018 tree res;
8019
8020 if (!validate_arg (arg0, REAL_TYPE)
8021 || !validate_arg (arg1, REAL_TYPE))
8022 return NULL_TREE;
8023
8024 /* Calculate the result when the argument is a constant. */
8025 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8026 return res;
8027
8028 /* Optimize pow(1.0,y) = 1.0. */
8029 if (real_onep (arg0))
8030 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8031
8032 if (TREE_CODE (arg1) == REAL_CST
8033 && !TREE_OVERFLOW (arg1))
8034 {
8035 REAL_VALUE_TYPE cint;
8036 REAL_VALUE_TYPE c;
8037 HOST_WIDE_INT n;
8038
8039 c = TREE_REAL_CST (arg1);
8040
8041 /* Optimize pow(x,0.0) = 1.0. */
8042 if (REAL_VALUES_EQUAL (c, dconst0))
8043 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8044 arg0);
8045
8046 /* Optimize pow(x,1.0) = x. */
8047 if (REAL_VALUES_EQUAL (c, dconst1))
8048 return arg0;
8049
8050 /* Optimize pow(x,-1.0) = 1.0/x. */
8051 if (REAL_VALUES_EQUAL (c, dconstm1))
8052 return fold_build2_loc (loc, RDIV_EXPR, type,
8053 build_real (type, dconst1), arg0);
8054
8055 /* Optimize pow(x,0.5) = sqrt(x). */
8056 if (flag_unsafe_math_optimizations
8057 && REAL_VALUES_EQUAL (c, dconsthalf))
8058 {
8059 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8060
8061 if (sqrtfn != NULL_TREE)
8062 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8063 }
8064
8065 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8066 if (flag_unsafe_math_optimizations)
8067 {
8068 const REAL_VALUE_TYPE dconstroot
8069 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8070
8071 if (REAL_VALUES_EQUAL (c, dconstroot))
8072 {
8073 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8074 if (cbrtfn != NULL_TREE)
8075 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8076 }
8077 }
8078
8079 /* Check for an integer exponent. */
8080 n = real_to_integer (&c);
8081 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8082 if (real_identical (&c, &cint))
8083 {
8084 /* Attempt to evaluate pow at compile-time, unless this should
8085 raise an exception. */
8086 if (TREE_CODE (arg0) == REAL_CST
8087 && !TREE_OVERFLOW (arg0)
8088 && (n > 0
8089 || (!flag_trapping_math && !flag_errno_math)
8090 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8091 {
8092 REAL_VALUE_TYPE x;
8093 bool inexact;
8094
8095 x = TREE_REAL_CST (arg0);
8096 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8097 if (flag_unsafe_math_optimizations || !inexact)
8098 return build_real (type, x);
8099 }
8100
8101 /* Strip sign ops from even integer powers. */
8102 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8103 {
8104 tree narg0 = fold_strip_sign_ops (arg0);
8105 if (narg0)
8106 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8107 }
8108 }
8109 }
8110
8111 if (flag_unsafe_math_optimizations)
8112 {
8113 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8114
8115 /* Optimize pow(expN(x),y) = expN(x*y). */
8116 if (BUILTIN_EXPONENT_P (fcode))
8117 {
8118 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8119 tree arg = CALL_EXPR_ARG (arg0, 0);
8120 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8121 return build_call_expr_loc (loc, expfn, 1, arg);
8122 }
8123
8124 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8125 if (BUILTIN_SQRT_P (fcode))
8126 {
8127 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8128 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8129 build_real (type, dconsthalf));
8130 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8131 }
8132
8133 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8134 if (BUILTIN_CBRT_P (fcode))
8135 {
8136 tree arg = CALL_EXPR_ARG (arg0, 0);
8137 if (tree_expr_nonnegative_p (arg))
8138 {
8139 const REAL_VALUE_TYPE dconstroot
8140 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8141 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8142 build_real (type, dconstroot));
8143 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8144 }
8145 }
8146
8147 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8148 if (fcode == BUILT_IN_POW
8149 || fcode == BUILT_IN_POWF
8150 || fcode == BUILT_IN_POWL)
8151 {
8152 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8153 if (tree_expr_nonnegative_p (arg00))
8154 {
8155 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8156 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8157 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8158 }
8159 }
8160 }
8161
8162 return NULL_TREE;
8163 }
8164
8165 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8166 Return NULL_TREE if no simplification can be made. */
8167 static tree
8168 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8169 tree arg0, tree arg1, tree type)
8170 {
8171 if (!validate_arg (arg0, REAL_TYPE)
8172 || !validate_arg (arg1, INTEGER_TYPE))
8173 return NULL_TREE;
8174
8175 /* Optimize pow(1.0,y) = 1.0. */
8176 if (real_onep (arg0))
8177 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8178
8179 if (host_integerp (arg1, 0))
8180 {
8181 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8182
8183 /* Evaluate powi at compile-time. */
8184 if (TREE_CODE (arg0) == REAL_CST
8185 && !TREE_OVERFLOW (arg0))
8186 {
8187 REAL_VALUE_TYPE x;
8188 x = TREE_REAL_CST (arg0);
8189 real_powi (&x, TYPE_MODE (type), &x, c);
8190 return build_real (type, x);
8191 }
8192
8193 /* Optimize pow(x,0) = 1.0. */
8194 if (c == 0)
8195 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8196 arg0);
8197
8198 /* Optimize pow(x,1) = x. */
8199 if (c == 1)
8200 return arg0;
8201
8202 /* Optimize pow(x,-1) = 1.0/x. */
8203 if (c == -1)
8204 return fold_build2_loc (loc, RDIV_EXPR, type,
8205 build_real (type, dconst1), arg0);
8206 }
8207
8208 return NULL_TREE;
8209 }
8210
8211 /* A subroutine of fold_builtin to fold the various exponent
8212 functions. Return NULL_TREE if no simplification can be made.
8213 FUNC is the corresponding MPFR exponent function. */
8214
8215 static tree
8216 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8217 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8218 {
8219 if (validate_arg (arg, REAL_TYPE))
8220 {
8221 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8222 tree res;
8223
8224 /* Calculate the result when the argument is a constant. */
8225 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8226 return res;
8227
8228 /* Optimize expN(logN(x)) = x. */
8229 if (flag_unsafe_math_optimizations)
8230 {
8231 const enum built_in_function fcode = builtin_mathfn_code (arg);
8232
8233 if ((func == mpfr_exp
8234 && (fcode == BUILT_IN_LOG
8235 || fcode == BUILT_IN_LOGF
8236 || fcode == BUILT_IN_LOGL))
8237 || (func == mpfr_exp2
8238 && (fcode == BUILT_IN_LOG2
8239 || fcode == BUILT_IN_LOG2F
8240 || fcode == BUILT_IN_LOG2L))
8241 || (func == mpfr_exp10
8242 && (fcode == BUILT_IN_LOG10
8243 || fcode == BUILT_IN_LOG10F
8244 || fcode == BUILT_IN_LOG10L)))
8245 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8246 }
8247 }
8248
8249 return NULL_TREE;
8250 }
8251
8252 /* Return true if VAR is a VAR_DECL or a component thereof. */
8253
8254 static bool
8255 var_decl_component_p (tree var)
8256 {
8257 tree inner = var;
8258 while (handled_component_p (inner))
8259 inner = TREE_OPERAND (inner, 0);
8260 return SSA_VAR_P (inner);
8261 }
8262
8263 /* Fold function call to builtin memset. Return
8264 NULL_TREE if no simplification can be made. */
8265
8266 static tree
8267 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8268 tree type, bool ignore)
8269 {
8270 tree var, ret, etype;
8271 unsigned HOST_WIDE_INT length, cval;
8272
8273 if (! validate_arg (dest, POINTER_TYPE)
8274 || ! validate_arg (c, INTEGER_TYPE)
8275 || ! validate_arg (len, INTEGER_TYPE))
8276 return NULL_TREE;
8277
8278 if (! host_integerp (len, 1))
8279 return NULL_TREE;
8280
8281 /* If the LEN parameter is zero, return DEST. */
8282 if (integer_zerop (len))
8283 return omit_one_operand_loc (loc, type, dest, c);
8284
8285 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8286 return NULL_TREE;
8287
8288 var = dest;
8289 STRIP_NOPS (var);
8290 if (TREE_CODE (var) != ADDR_EXPR)
8291 return NULL_TREE;
8292
8293 var = TREE_OPERAND (var, 0);
8294 if (TREE_THIS_VOLATILE (var))
8295 return NULL_TREE;
8296
8297 etype = TREE_TYPE (var);
8298 if (TREE_CODE (etype) == ARRAY_TYPE)
8299 etype = TREE_TYPE (etype);
8300
8301 if (!INTEGRAL_TYPE_P (etype)
8302 && !POINTER_TYPE_P (etype))
8303 return NULL_TREE;
8304
8305 if (! var_decl_component_p (var))
8306 return NULL_TREE;
8307
8308 length = tree_low_cst (len, 1);
8309 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8310 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8311 < length)
8312 return NULL_TREE;
8313
8314 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8315 return NULL_TREE;
8316
8317 if (integer_zerop (c))
8318 cval = 0;
8319 else
8320 {
8321 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8322 return NULL_TREE;
8323
8324 cval = TREE_INT_CST_LOW (c);
8325 cval &= 0xff;
8326 cval |= cval << 8;
8327 cval |= cval << 16;
8328 cval |= (cval << 31) << 1;
8329 }
8330
8331 ret = build_int_cst_type (etype, cval);
8332 var = build_fold_indirect_ref_loc (loc,
8333 fold_convert_loc (loc,
8334 build_pointer_type (etype),
8335 dest));
8336 ret = build2 (MODIFY_EXPR, etype, var, ret);
8337 if (ignore)
8338 return ret;
8339
8340 return omit_one_operand_loc (loc, type, dest, ret);
8341 }
8342
8343 /* Fold function call to builtin memset. Return
8344 NULL_TREE if no simplification can be made. */
8345
8346 static tree
8347 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8348 {
8349 if (! validate_arg (dest, POINTER_TYPE)
8350 || ! validate_arg (size, INTEGER_TYPE))
8351 return NULL_TREE;
8352
8353 if (!ignore)
8354 return NULL_TREE;
8355
8356 /* New argument list transforming bzero(ptr x, int y) to
8357 memset(ptr x, int 0, size_t y). This is done this way
8358 so that if it isn't expanded inline, we fallback to
8359 calling bzero instead of memset. */
8360
8361 return fold_builtin_memset (loc, dest, integer_zero_node,
8362 fold_convert_loc (loc, sizetype, size),
8363 void_type_node, ignore);
8364 }
8365
8366 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8367 NULL_TREE if no simplification can be made.
8368 If ENDP is 0, return DEST (like memcpy).
8369 If ENDP is 1, return DEST+LEN (like mempcpy).
8370 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8371 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8372 (memmove). */
8373
8374 static tree
8375 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8376 tree len, tree type, bool ignore, int endp)
8377 {
8378 tree destvar, srcvar, expr;
8379
8380 if (! validate_arg (dest, POINTER_TYPE)
8381 || ! validate_arg (src, POINTER_TYPE)
8382 || ! validate_arg (len, INTEGER_TYPE))
8383 return NULL_TREE;
8384
8385 /* If the LEN parameter is zero, return DEST. */
8386 if (integer_zerop (len))
8387 return omit_one_operand_loc (loc, type, dest, src);
8388
8389 /* If SRC and DEST are the same (and not volatile), return
8390 DEST{,+LEN,+LEN-1}. */
8391 if (operand_equal_p (src, dest, 0))
8392 expr = len;
8393 else
8394 {
8395 tree srctype, desttype;
8396 unsigned int src_align, dest_align;
8397 tree off0;
8398
8399 if (endp == 3)
8400 {
8401 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8402 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8403
8404 /* Both DEST and SRC must be pointer types.
8405 ??? This is what old code did. Is the testing for pointer types
8406 really mandatory?
8407
8408 If either SRC is readonly or length is 1, we can use memcpy. */
8409 if (!dest_align || !src_align)
8410 return NULL_TREE;
8411 if (readonly_data_expr (src)
8412 || (host_integerp (len, 1)
8413 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8414 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8415 {
8416 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8417 if (!fn)
8418 return NULL_TREE;
8419 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8420 }
8421
8422 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8423 if (TREE_CODE (src) == ADDR_EXPR
8424 && TREE_CODE (dest) == ADDR_EXPR)
8425 {
8426 tree src_base, dest_base, fn;
8427 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8428 HOST_WIDE_INT size = -1;
8429 HOST_WIDE_INT maxsize = -1;
8430
8431 srcvar = TREE_OPERAND (src, 0);
8432 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8433 &size, &maxsize);
8434 destvar = TREE_OPERAND (dest, 0);
8435 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8436 &size, &maxsize);
8437 if (host_integerp (len, 1))
8438 maxsize = tree_low_cst (len, 1);
8439 else
8440 maxsize = -1;
8441 src_offset /= BITS_PER_UNIT;
8442 dest_offset /= BITS_PER_UNIT;
8443 if (SSA_VAR_P (src_base)
8444 && SSA_VAR_P (dest_base))
8445 {
8446 if (operand_equal_p (src_base, dest_base, 0)
8447 && ranges_overlap_p (src_offset, maxsize,
8448 dest_offset, maxsize))
8449 return NULL_TREE;
8450 }
8451 else if (TREE_CODE (src_base) == MEM_REF
8452 && TREE_CODE (dest_base) == MEM_REF)
8453 {
8454 double_int off;
8455 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8456 TREE_OPERAND (dest_base, 0), 0))
8457 return NULL_TREE;
8458 off = double_int_add (mem_ref_offset (src_base),
8459 shwi_to_double_int (src_offset));
8460 if (!double_int_fits_in_shwi_p (off))
8461 return NULL_TREE;
8462 src_offset = off.low;
8463 off = double_int_add (mem_ref_offset (dest_base),
8464 shwi_to_double_int (dest_offset));
8465 if (!double_int_fits_in_shwi_p (off))
8466 return NULL_TREE;
8467 dest_offset = off.low;
8468 if (ranges_overlap_p (src_offset, maxsize,
8469 dest_offset, maxsize))
8470 return NULL_TREE;
8471 }
8472 else
8473 return NULL_TREE;
8474
8475 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8476 if (!fn)
8477 return NULL_TREE;
8478 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8479 }
8480
8481 /* If the destination and source do not alias optimize into
8482 memcpy as well. */
8483 if ((is_gimple_min_invariant (dest)
8484 || TREE_CODE (dest) == SSA_NAME)
8485 && (is_gimple_min_invariant (src)
8486 || TREE_CODE (src) == SSA_NAME))
8487 {
8488 ao_ref destr, srcr;
8489 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8490 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8491 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8492 {
8493 tree fn;
8494 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8495 if (!fn)
8496 return NULL_TREE;
8497 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8498 }
8499 }
8500
8501 return NULL_TREE;
8502 }
8503
8504 if (!host_integerp (len, 0))
8505 return NULL_TREE;
8506 /* FIXME:
8507 This logic lose for arguments like (type *)malloc (sizeof (type)),
8508 since we strip the casts of up to VOID return value from malloc.
8509 Perhaps we ought to inherit type from non-VOID argument here? */
8510 STRIP_NOPS (src);
8511 STRIP_NOPS (dest);
8512 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8513 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8514 {
8515 tree tem = TREE_OPERAND (src, 0);
8516 STRIP_NOPS (tem);
8517 if (tem != TREE_OPERAND (src, 0))
8518 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8519 }
8520 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8521 {
8522 tree tem = TREE_OPERAND (dest, 0);
8523 STRIP_NOPS (tem);
8524 if (tem != TREE_OPERAND (dest, 0))
8525 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8526 }
8527 srctype = TREE_TYPE (TREE_TYPE (src));
8528 if (srctype
8529 && TREE_CODE (srctype) == ARRAY_TYPE
8530 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8531 {
8532 srctype = TREE_TYPE (srctype);
8533 STRIP_NOPS (src);
8534 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8535 }
8536 desttype = TREE_TYPE (TREE_TYPE (dest));
8537 if (desttype
8538 && TREE_CODE (desttype) == ARRAY_TYPE
8539 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8540 {
8541 desttype = TREE_TYPE (desttype);
8542 STRIP_NOPS (dest);
8543 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8544 }
8545 if (!srctype || !desttype
8546 || TREE_ADDRESSABLE (srctype)
8547 || TREE_ADDRESSABLE (desttype)
8548 || !TYPE_SIZE_UNIT (srctype)
8549 || !TYPE_SIZE_UNIT (desttype)
8550 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8551 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8552 return NULL_TREE;
8553
8554 src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8555 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8556 if (dest_align < TYPE_ALIGN (desttype)
8557 || src_align < TYPE_ALIGN (srctype))
8558 return NULL_TREE;
8559
8560 if (!ignore)
8561 dest = builtin_save_expr (dest);
8562
8563 /* Build accesses at offset zero with a ref-all character type. */
8564 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8565 ptr_mode, true), 0);
8566
8567 destvar = dest;
8568 STRIP_NOPS (destvar);
8569 if (TREE_CODE (destvar) == ADDR_EXPR
8570 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8571 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8572 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8573 else
8574 destvar = NULL_TREE;
8575
8576 srcvar = src;
8577 STRIP_NOPS (srcvar);
8578 if (TREE_CODE (srcvar) == ADDR_EXPR
8579 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8580 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8581 {
8582 if (!destvar
8583 || src_align >= TYPE_ALIGN (desttype))
8584 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8585 srcvar, off0);
8586 else if (!STRICT_ALIGNMENT)
8587 {
8588 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8589 src_align);
8590 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8591 }
8592 else
8593 srcvar = NULL_TREE;
8594 }
8595 else
8596 srcvar = NULL_TREE;
8597
8598 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8599 return NULL_TREE;
8600
8601 if (srcvar == NULL_TREE)
8602 {
8603 STRIP_NOPS (src);
8604 if (src_align >= TYPE_ALIGN (desttype))
8605 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8606 else
8607 {
8608 if (STRICT_ALIGNMENT)
8609 return NULL_TREE;
8610 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8611 src_align);
8612 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8613 }
8614 }
8615 else if (destvar == NULL_TREE)
8616 {
8617 STRIP_NOPS (dest);
8618 if (dest_align >= TYPE_ALIGN (srctype))
8619 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8620 else
8621 {
8622 if (STRICT_ALIGNMENT)
8623 return NULL_TREE;
8624 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8625 dest_align);
8626 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8627 }
8628 }
8629
8630 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8631 }
8632
8633 if (ignore)
8634 return expr;
8635
8636 if (endp == 0 || endp == 3)
8637 return omit_one_operand_loc (loc, type, dest, expr);
8638
8639 if (expr == len)
8640 expr = NULL_TREE;
8641
8642 if (endp == 2)
8643 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8644 ssize_int (1));
8645
8646 len = fold_convert_loc (loc, sizetype, len);
8647 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8648 dest = fold_convert_loc (loc, type, dest);
8649 if (expr)
8650 dest = omit_one_operand_loc (loc, type, dest, expr);
8651 return dest;
8652 }
8653
8654 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8655 If LEN is not NULL, it represents the length of the string to be
8656 copied. Return NULL_TREE if no simplification can be made. */
8657
8658 tree
8659 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8660 {
8661 tree fn;
8662
8663 if (!validate_arg (dest, POINTER_TYPE)
8664 || !validate_arg (src, POINTER_TYPE))
8665 return NULL_TREE;
8666
8667 /* If SRC and DEST are the same (and not volatile), return DEST. */
8668 if (operand_equal_p (src, dest, 0))
8669 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8670
8671 if (optimize_function_for_size_p (cfun))
8672 return NULL_TREE;
8673
8674 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8675 if (!fn)
8676 return NULL_TREE;
8677
8678 if (!len)
8679 {
8680 len = c_strlen (src, 1);
8681 if (! len || TREE_SIDE_EFFECTS (len))
8682 return NULL_TREE;
8683 }
8684
8685 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8686 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8687 build_call_expr_loc (loc, fn, 3, dest, src, len));
8688 }
8689
8690 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8691 Return NULL_TREE if no simplification can be made. */
8692
8693 static tree
8694 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8695 {
8696 tree fn, len, lenp1, call, type;
8697
8698 if (!validate_arg (dest, POINTER_TYPE)
8699 || !validate_arg (src, POINTER_TYPE))
8700 return NULL_TREE;
8701
8702 len = c_strlen (src, 1);
8703 if (!len
8704 || TREE_CODE (len) != INTEGER_CST)
8705 return NULL_TREE;
8706
8707 if (optimize_function_for_size_p (cfun)
8708 /* If length is zero it's small enough. */
8709 && !integer_zerop (len))
8710 return NULL_TREE;
8711
8712 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8713 if (!fn)
8714 return NULL_TREE;
8715
8716 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8717 /* We use dest twice in building our expression. Save it from
8718 multiple expansions. */
8719 dest = builtin_save_expr (dest);
8720 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8721
8722 type = TREE_TYPE (TREE_TYPE (fndecl));
8723 len = fold_convert_loc (loc, sizetype, len);
8724 dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8725 dest = fold_convert_loc (loc, type, dest);
8726 dest = omit_one_operand_loc (loc, type, dest, call);
8727 return dest;
8728 }
8729
8730 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8731 If SLEN is not NULL, it represents the length of the source string.
8732 Return NULL_TREE if no simplification can be made. */
8733
8734 tree
8735 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8736 tree src, tree len, tree slen)
8737 {
8738 tree fn;
8739
8740 if (!validate_arg (dest, POINTER_TYPE)
8741 || !validate_arg (src, POINTER_TYPE)
8742 || !validate_arg (len, INTEGER_TYPE))
8743 return NULL_TREE;
8744
8745 /* If the LEN parameter is zero, return DEST. */
8746 if (integer_zerop (len))
8747 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8748
8749 /* We can't compare slen with len as constants below if len is not a
8750 constant. */
8751 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8752 return NULL_TREE;
8753
8754 if (!slen)
8755 slen = c_strlen (src, 1);
8756
8757 /* Now, we must be passed a constant src ptr parameter. */
8758 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8759 return NULL_TREE;
8760
8761 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8762
8763 /* We do not support simplification of this case, though we do
8764 support it when expanding trees into RTL. */
8765 /* FIXME: generate a call to __builtin_memset. */
8766 if (tree_int_cst_lt (slen, len))
8767 return NULL_TREE;
8768
8769 /* OK transform into builtin memcpy. */
8770 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8771 if (!fn)
8772 return NULL_TREE;
8773 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8774 build_call_expr_loc (loc, fn, 3, dest, src, len));
8775 }
8776
8777 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8778 arguments to the call, and TYPE is its return type.
8779 Return NULL_TREE if no simplification can be made. */
8780
8781 static tree
8782 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8783 {
8784 if (!validate_arg (arg1, POINTER_TYPE)
8785 || !validate_arg (arg2, INTEGER_TYPE)
8786 || !validate_arg (len, INTEGER_TYPE))
8787 return NULL_TREE;
8788 else
8789 {
8790 const char *p1;
8791
8792 if (TREE_CODE (arg2) != INTEGER_CST
8793 || !host_integerp (len, 1))
8794 return NULL_TREE;
8795
8796 p1 = c_getstr (arg1);
8797 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8798 {
8799 char c;
8800 const char *r;
8801 tree tem;
8802
8803 if (target_char_cast (arg2, &c))
8804 return NULL_TREE;
8805
8806 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8807
8808 if (r == NULL)
8809 return build_int_cst (TREE_TYPE (arg1), 0);
8810
8811 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8812 size_int (r - p1));
8813 return fold_convert_loc (loc, type, tem);
8814 }
8815 return NULL_TREE;
8816 }
8817 }
8818
8819 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8820 Return NULL_TREE if no simplification can be made. */
8821
8822 static tree
8823 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8824 {
8825 const char *p1, *p2;
8826
8827 if (!validate_arg (arg1, POINTER_TYPE)
8828 || !validate_arg (arg2, POINTER_TYPE)
8829 || !validate_arg (len, INTEGER_TYPE))
8830 return NULL_TREE;
8831
8832 /* If the LEN parameter is zero, return zero. */
8833 if (integer_zerop (len))
8834 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8835 arg1, arg2);
8836
8837 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8838 if (operand_equal_p (arg1, arg2, 0))
8839 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8840
8841 p1 = c_getstr (arg1);
8842 p2 = c_getstr (arg2);
8843
8844 /* If all arguments are constant, and the value of len is not greater
8845 than the lengths of arg1 and arg2, evaluate at compile-time. */
8846 if (host_integerp (len, 1) && p1 && p2
8847 && compare_tree_int (len, strlen (p1) + 1) <= 0
8848 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8849 {
8850 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8851
8852 if (r > 0)
8853 return integer_one_node;
8854 else if (r < 0)
8855 return integer_minus_one_node;
8856 else
8857 return integer_zero_node;
8858 }
8859
8860 /* If len parameter is one, return an expression corresponding to
8861 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8862 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8863 {
8864 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8865 tree cst_uchar_ptr_node
8866 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8867
8868 tree ind1
8869 = fold_convert_loc (loc, integer_type_node,
8870 build1 (INDIRECT_REF, cst_uchar_node,
8871 fold_convert_loc (loc,
8872 cst_uchar_ptr_node,
8873 arg1)));
8874 tree ind2
8875 = fold_convert_loc (loc, integer_type_node,
8876 build1 (INDIRECT_REF, cst_uchar_node,
8877 fold_convert_loc (loc,
8878 cst_uchar_ptr_node,
8879 arg2)));
8880 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8881 }
8882
8883 return NULL_TREE;
8884 }
8885
8886 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8887 Return NULL_TREE if no simplification can be made. */
8888
8889 static tree
8890 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8891 {
8892 const char *p1, *p2;
8893
8894 if (!validate_arg (arg1, POINTER_TYPE)
8895 || !validate_arg (arg2, POINTER_TYPE))
8896 return NULL_TREE;
8897
8898 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8899 if (operand_equal_p (arg1, arg2, 0))
8900 return integer_zero_node;
8901
8902 p1 = c_getstr (arg1);
8903 p2 = c_getstr (arg2);
8904
8905 if (p1 && p2)
8906 {
8907 const int i = strcmp (p1, p2);
8908 if (i < 0)
8909 return integer_minus_one_node;
8910 else if (i > 0)
8911 return integer_one_node;
8912 else
8913 return integer_zero_node;
8914 }
8915
8916 /* If the second arg is "", return *(const unsigned char*)arg1. */
8917 if (p2 && *p2 == '\0')
8918 {
8919 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8920 tree cst_uchar_ptr_node
8921 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8922
8923 return fold_convert_loc (loc, integer_type_node,
8924 build1 (INDIRECT_REF, cst_uchar_node,
8925 fold_convert_loc (loc,
8926 cst_uchar_ptr_node,
8927 arg1)));
8928 }
8929
8930 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8931 if (p1 && *p1 == '\0')
8932 {
8933 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8934 tree cst_uchar_ptr_node
8935 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8936
8937 tree temp
8938 = fold_convert_loc (loc, integer_type_node,
8939 build1 (INDIRECT_REF, cst_uchar_node,
8940 fold_convert_loc (loc,
8941 cst_uchar_ptr_node,
8942 arg2)));
8943 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8944 }
8945
8946 return NULL_TREE;
8947 }
8948
8949 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8950 Return NULL_TREE if no simplification can be made. */
8951
8952 static tree
8953 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8954 {
8955 const char *p1, *p2;
8956
8957 if (!validate_arg (arg1, POINTER_TYPE)
8958 || !validate_arg (arg2, POINTER_TYPE)
8959 || !validate_arg (len, INTEGER_TYPE))
8960 return NULL_TREE;
8961
8962 /* If the LEN parameter is zero, return zero. */
8963 if (integer_zerop (len))
8964 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8965 arg1, arg2);
8966
8967 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8968 if (operand_equal_p (arg1, arg2, 0))
8969 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8970
8971 p1 = c_getstr (arg1);
8972 p2 = c_getstr (arg2);
8973
8974 if (host_integerp (len, 1) && p1 && p2)
8975 {
8976 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8977 if (i > 0)
8978 return integer_one_node;
8979 else if (i < 0)
8980 return integer_minus_one_node;
8981 else
8982 return integer_zero_node;
8983 }
8984
8985 /* If the second arg is "", and the length is greater than zero,
8986 return *(const unsigned char*)arg1. */
8987 if (p2 && *p2 == '\0'
8988 && TREE_CODE (len) == INTEGER_CST
8989 && tree_int_cst_sgn (len) == 1)
8990 {
8991 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8992 tree cst_uchar_ptr_node
8993 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8994
8995 return fold_convert_loc (loc, integer_type_node,
8996 build1 (INDIRECT_REF, cst_uchar_node,
8997 fold_convert_loc (loc,
8998 cst_uchar_ptr_node,
8999 arg1)));
9000 }
9001
9002 /* If the first arg is "", and the length is greater than zero,
9003 return -*(const unsigned char*)arg2. */
9004 if (p1 && *p1 == '\0'
9005 && TREE_CODE (len) == INTEGER_CST
9006 && tree_int_cst_sgn (len) == 1)
9007 {
9008 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9009 tree cst_uchar_ptr_node
9010 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9011
9012 tree temp = fold_convert_loc (loc, integer_type_node,
9013 build1 (INDIRECT_REF, cst_uchar_node,
9014 fold_convert_loc (loc,
9015 cst_uchar_ptr_node,
9016 arg2)));
9017 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9018 }
9019
9020 /* If len parameter is one, return an expression corresponding to
9021 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9022 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9023 {
9024 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9025 tree cst_uchar_ptr_node
9026 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9027
9028 tree ind1 = fold_convert_loc (loc, integer_type_node,
9029 build1 (INDIRECT_REF, cst_uchar_node,
9030 fold_convert_loc (loc,
9031 cst_uchar_ptr_node,
9032 arg1)));
9033 tree ind2 = fold_convert_loc (loc, integer_type_node,
9034 build1 (INDIRECT_REF, cst_uchar_node,
9035 fold_convert_loc (loc,
9036 cst_uchar_ptr_node,
9037 arg2)));
9038 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9039 }
9040
9041 return NULL_TREE;
9042 }
9043
9044 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9045 ARG. Return NULL_TREE if no simplification can be made. */
9046
9047 static tree
9048 fold_builtin_signbit (location_t loc, tree arg, tree type)
9049 {
9050 if (!validate_arg (arg, REAL_TYPE))
9051 return NULL_TREE;
9052
9053 /* If ARG is a compile-time constant, determine the result. */
9054 if (TREE_CODE (arg) == REAL_CST
9055 && !TREE_OVERFLOW (arg))
9056 {
9057 REAL_VALUE_TYPE c;
9058
9059 c = TREE_REAL_CST (arg);
9060 return (REAL_VALUE_NEGATIVE (c)
9061 ? build_one_cst (type)
9062 : build_zero_cst (type));
9063 }
9064
9065 /* If ARG is non-negative, the result is always zero. */
9066 if (tree_expr_nonnegative_p (arg))
9067 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9068
9069 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9070 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9071 return fold_build2_loc (loc, LT_EXPR, type, arg,
9072 build_real (TREE_TYPE (arg), dconst0));
9073
9074 return NULL_TREE;
9075 }
9076
9077 /* Fold function call to builtin copysign, copysignf or copysignl with
9078 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9079 be made. */
9080
9081 static tree
9082 fold_builtin_copysign (location_t loc, tree fndecl,
9083 tree arg1, tree arg2, tree type)
9084 {
9085 tree tem;
9086
9087 if (!validate_arg (arg1, REAL_TYPE)
9088 || !validate_arg (arg2, REAL_TYPE))
9089 return NULL_TREE;
9090
9091 /* copysign(X,X) is X. */
9092 if (operand_equal_p (arg1, arg2, 0))
9093 return fold_convert_loc (loc, type, arg1);
9094
9095 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9096 if (TREE_CODE (arg1) == REAL_CST
9097 && TREE_CODE (arg2) == REAL_CST
9098 && !TREE_OVERFLOW (arg1)
9099 && !TREE_OVERFLOW (arg2))
9100 {
9101 REAL_VALUE_TYPE c1, c2;
9102
9103 c1 = TREE_REAL_CST (arg1);
9104 c2 = TREE_REAL_CST (arg2);
9105 /* c1.sign := c2.sign. */
9106 real_copysign (&c1, &c2);
9107 return build_real (type, c1);
9108 }
9109
9110 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9111 Remember to evaluate Y for side-effects. */
9112 if (tree_expr_nonnegative_p (arg2))
9113 return omit_one_operand_loc (loc, type,
9114 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9115 arg2);
9116
9117 /* Strip sign changing operations for the first argument. */
9118 tem = fold_strip_sign_ops (arg1);
9119 if (tem)
9120 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9121
9122 return NULL_TREE;
9123 }
9124
9125 /* Fold a call to builtin isascii with argument ARG. */
9126
9127 static tree
9128 fold_builtin_isascii (location_t loc, tree arg)
9129 {
9130 if (!validate_arg (arg, INTEGER_TYPE))
9131 return NULL_TREE;
9132 else
9133 {
9134 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9135 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9136 build_int_cst (NULL_TREE,
9137 ~ (unsigned HOST_WIDE_INT) 0x7f));
9138 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9139 arg, integer_zero_node);
9140 }
9141 }
9142
9143 /* Fold a call to builtin toascii with argument ARG. */
9144
9145 static tree
9146 fold_builtin_toascii (location_t loc, tree arg)
9147 {
9148 if (!validate_arg (arg, INTEGER_TYPE))
9149 return NULL_TREE;
9150
9151 /* Transform toascii(c) -> (c & 0x7f). */
9152 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9153 build_int_cst (NULL_TREE, 0x7f));
9154 }
9155
9156 /* Fold a call to builtin isdigit with argument ARG. */
9157
9158 static tree
9159 fold_builtin_isdigit (location_t loc, tree arg)
9160 {
9161 if (!validate_arg (arg, INTEGER_TYPE))
9162 return NULL_TREE;
9163 else
9164 {
9165 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9166 /* According to the C standard, isdigit is unaffected by locale.
9167 However, it definitely is affected by the target character set. */
9168 unsigned HOST_WIDE_INT target_digit0
9169 = lang_hooks.to_target_charset ('0');
9170
9171 if (target_digit0 == 0)
9172 return NULL_TREE;
9173
9174 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9175 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9176 build_int_cst (unsigned_type_node, target_digit0));
9177 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9178 build_int_cst (unsigned_type_node, 9));
9179 }
9180 }
9181
9182 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9183
9184 static tree
9185 fold_builtin_fabs (location_t loc, tree arg, tree type)
9186 {
9187 if (!validate_arg (arg, REAL_TYPE))
9188 return NULL_TREE;
9189
9190 arg = fold_convert_loc (loc, type, arg);
9191 if (TREE_CODE (arg) == REAL_CST)
9192 return fold_abs_const (arg, type);
9193 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9194 }
9195
9196 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9197
9198 static tree
9199 fold_builtin_abs (location_t loc, tree arg, tree type)
9200 {
9201 if (!validate_arg (arg, INTEGER_TYPE))
9202 return NULL_TREE;
9203
9204 arg = fold_convert_loc (loc, type, arg);
9205 if (TREE_CODE (arg) == INTEGER_CST)
9206 return fold_abs_const (arg, type);
9207 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9208 }
9209
9210 /* Fold a fma operation with arguments ARG[012]. */
9211
9212 tree
9213 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9214 tree type, tree arg0, tree arg1, tree arg2)
9215 {
9216 if (TREE_CODE (arg0) == REAL_CST
9217 && TREE_CODE (arg1) == REAL_CST
9218 && TREE_CODE (arg2) == REAL_CST)
9219 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9220
9221 return NULL_TREE;
9222 }
9223
9224 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9225
9226 static tree
9227 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9228 {
9229 if (validate_arg (arg0, REAL_TYPE)
9230 && validate_arg(arg1, REAL_TYPE)
9231 && validate_arg(arg2, REAL_TYPE))
9232 {
9233 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9234 if (tem)
9235 return tem;
9236
9237 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9238 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9239 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9240 }
9241 return NULL_TREE;
9242 }
9243
9244 /* Fold a call to builtin fmin or fmax. */
9245
9246 static tree
9247 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9248 tree type, bool max)
9249 {
9250 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9251 {
9252 /* Calculate the result when the argument is a constant. */
9253 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9254
9255 if (res)
9256 return res;
9257
9258 /* If either argument is NaN, return the other one. Avoid the
9259 transformation if we get (and honor) a signalling NaN. Using
9260 omit_one_operand() ensures we create a non-lvalue. */
9261 if (TREE_CODE (arg0) == REAL_CST
9262 && real_isnan (&TREE_REAL_CST (arg0))
9263 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9264 || ! TREE_REAL_CST (arg0).signalling))
9265 return omit_one_operand_loc (loc, type, arg1, arg0);
9266 if (TREE_CODE (arg1) == REAL_CST
9267 && real_isnan (&TREE_REAL_CST (arg1))
9268 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9269 || ! TREE_REAL_CST (arg1).signalling))
9270 return omit_one_operand_loc (loc, type, arg0, arg1);
9271
9272 /* Transform fmin/fmax(x,x) -> x. */
9273 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9274 return omit_one_operand_loc (loc, type, arg0, arg1);
9275
9276 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9277 functions to return the numeric arg if the other one is NaN.
9278 These tree codes don't honor that, so only transform if
9279 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9280 handled, so we don't have to worry about it either. */
9281 if (flag_finite_math_only)
9282 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9283 fold_convert_loc (loc, type, arg0),
9284 fold_convert_loc (loc, type, arg1));
9285 }
9286 return NULL_TREE;
9287 }
9288
9289 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9290
9291 static tree
9292 fold_builtin_carg (location_t loc, tree arg, tree type)
9293 {
9294 if (validate_arg (arg, COMPLEX_TYPE)
9295 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9296 {
9297 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9298
9299 if (atan2_fn)
9300 {
9301 tree new_arg = builtin_save_expr (arg);
9302 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9303 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9304 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9305 }
9306 }
9307
9308 return NULL_TREE;
9309 }
9310
9311 /* Fold a call to builtin logb/ilogb. */
9312
9313 static tree
9314 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9315 {
9316 if (! validate_arg (arg, REAL_TYPE))
9317 return NULL_TREE;
9318
9319 STRIP_NOPS (arg);
9320
9321 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9322 {
9323 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9324
9325 switch (value->cl)
9326 {
9327 case rvc_nan:
9328 case rvc_inf:
9329 /* If arg is Inf or NaN and we're logb, return it. */
9330 if (TREE_CODE (rettype) == REAL_TYPE)
9331 return fold_convert_loc (loc, rettype, arg);
9332 /* Fall through... */
9333 case rvc_zero:
9334 /* Zero may set errno and/or raise an exception for logb, also
9335 for ilogb we don't know FP_ILOGB0. */
9336 return NULL_TREE;
9337 case rvc_normal:
9338 /* For normal numbers, proceed iff radix == 2. In GCC,
9339 normalized significands are in the range [0.5, 1.0). We
9340 want the exponent as if they were [1.0, 2.0) so get the
9341 exponent and subtract 1. */
9342 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9343 return fold_convert_loc (loc, rettype,
9344 build_int_cst (NULL_TREE,
9345 REAL_EXP (value)-1));
9346 break;
9347 }
9348 }
9349
9350 return NULL_TREE;
9351 }
9352
9353 /* Fold a call to builtin significand, if radix == 2. */
9354
9355 static tree
9356 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9357 {
9358 if (! validate_arg (arg, REAL_TYPE))
9359 return NULL_TREE;
9360
9361 STRIP_NOPS (arg);
9362
9363 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9364 {
9365 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9366
9367 switch (value->cl)
9368 {
9369 case rvc_zero:
9370 case rvc_nan:
9371 case rvc_inf:
9372 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9373 return fold_convert_loc (loc, rettype, arg);
9374 case rvc_normal:
9375 /* For normal numbers, proceed iff radix == 2. */
9376 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9377 {
9378 REAL_VALUE_TYPE result = *value;
9379 /* In GCC, normalized significands are in the range [0.5,
9380 1.0). We want them to be [1.0, 2.0) so set the
9381 exponent to 1. */
9382 SET_REAL_EXP (&result, 1);
9383 return build_real (rettype, result);
9384 }
9385 break;
9386 }
9387 }
9388
9389 return NULL_TREE;
9390 }
9391
9392 /* Fold a call to builtin frexp, we can assume the base is 2. */
9393
9394 static tree
9395 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9396 {
9397 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9398 return NULL_TREE;
9399
9400 STRIP_NOPS (arg0);
9401
9402 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9403 return NULL_TREE;
9404
9405 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9406
9407 /* Proceed if a valid pointer type was passed in. */
9408 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9409 {
9410 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9411 tree frac, exp;
9412
9413 switch (value->cl)
9414 {
9415 case rvc_zero:
9416 /* For +-0, return (*exp = 0, +-0). */
9417 exp = integer_zero_node;
9418 frac = arg0;
9419 break;
9420 case rvc_nan:
9421 case rvc_inf:
9422 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9423 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9424 case rvc_normal:
9425 {
9426 /* Since the frexp function always expects base 2, and in
9427 GCC normalized significands are already in the range
9428 [0.5, 1.0), we have exactly what frexp wants. */
9429 REAL_VALUE_TYPE frac_rvt = *value;
9430 SET_REAL_EXP (&frac_rvt, 0);
9431 frac = build_real (rettype, frac_rvt);
9432 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9433 }
9434 break;
9435 default:
9436 gcc_unreachable ();
9437 }
9438
9439 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9440 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9441 TREE_SIDE_EFFECTS (arg1) = 1;
9442 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9443 }
9444
9445 return NULL_TREE;
9446 }
9447
9448 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9449 then we can assume the base is two. If it's false, then we have to
9450 check the mode of the TYPE parameter in certain cases. */
9451
9452 static tree
9453 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9454 tree type, bool ldexp)
9455 {
9456 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9457 {
9458 STRIP_NOPS (arg0);
9459 STRIP_NOPS (arg1);
9460
9461 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9462 if (real_zerop (arg0) || integer_zerop (arg1)
9463 || (TREE_CODE (arg0) == REAL_CST
9464 && !real_isfinite (&TREE_REAL_CST (arg0))))
9465 return omit_one_operand_loc (loc, type, arg0, arg1);
9466
9467 /* If both arguments are constant, then try to evaluate it. */
9468 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9469 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9470 && host_integerp (arg1, 0))
9471 {
9472 /* Bound the maximum adjustment to twice the range of the
9473 mode's valid exponents. Use abs to ensure the range is
9474 positive as a sanity check. */
9475 const long max_exp_adj = 2 *
9476 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9477 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9478
9479 /* Get the user-requested adjustment. */
9480 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9481
9482 /* The requested adjustment must be inside this range. This
9483 is a preliminary cap to avoid things like overflow, we
9484 may still fail to compute the result for other reasons. */
9485 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9486 {
9487 REAL_VALUE_TYPE initial_result;
9488
9489 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9490
9491 /* Ensure we didn't overflow. */
9492 if (! real_isinf (&initial_result))
9493 {
9494 const REAL_VALUE_TYPE trunc_result
9495 = real_value_truncate (TYPE_MODE (type), initial_result);
9496
9497 /* Only proceed if the target mode can hold the
9498 resulting value. */
9499 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9500 return build_real (type, trunc_result);
9501 }
9502 }
9503 }
9504 }
9505
9506 return NULL_TREE;
9507 }
9508
9509 /* Fold a call to builtin modf. */
9510
9511 static tree
9512 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9513 {
9514 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9515 return NULL_TREE;
9516
9517 STRIP_NOPS (arg0);
9518
9519 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9520 return NULL_TREE;
9521
9522 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9523
9524 /* Proceed if a valid pointer type was passed in. */
9525 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9526 {
9527 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9528 REAL_VALUE_TYPE trunc, frac;
9529
9530 switch (value->cl)
9531 {
9532 case rvc_nan:
9533 case rvc_zero:
9534 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9535 trunc = frac = *value;
9536 break;
9537 case rvc_inf:
9538 /* For +-Inf, return (*arg1 = arg0, +-0). */
9539 frac = dconst0;
9540 frac.sign = value->sign;
9541 trunc = *value;
9542 break;
9543 case rvc_normal:
9544 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9545 real_trunc (&trunc, VOIDmode, value);
9546 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9547 /* If the original number was negative and already
9548 integral, then the fractional part is -0.0. */
9549 if (value->sign && frac.cl == rvc_zero)
9550 frac.sign = value->sign;
9551 break;
9552 }
9553
9554 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9555 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9556 build_real (rettype, trunc));
9557 TREE_SIDE_EFFECTS (arg1) = 1;
9558 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9559 build_real (rettype, frac));
9560 }
9561
9562 return NULL_TREE;
9563 }
9564
9565 /* Given a location LOC, an interclass builtin function decl FNDECL
9566 and its single argument ARG, return an folded expression computing
9567 the same, or NULL_TREE if we either couldn't or didn't want to fold
9568 (the latter happen if there's an RTL instruction available). */
9569
9570 static tree
9571 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9572 {
9573 enum machine_mode mode;
9574
9575 if (!validate_arg (arg, REAL_TYPE))
9576 return NULL_TREE;
9577
9578 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9579 return NULL_TREE;
9580
9581 mode = TYPE_MODE (TREE_TYPE (arg));
9582
9583 /* If there is no optab, try generic code. */
9584 switch (DECL_FUNCTION_CODE (fndecl))
9585 {
9586 tree result;
9587
9588 CASE_FLT_FN (BUILT_IN_ISINF):
9589 {
9590 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9591 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9592 tree const type = TREE_TYPE (arg);
9593 REAL_VALUE_TYPE r;
9594 char buf[128];
9595
9596 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9597 real_from_string (&r, buf);
9598 result = build_call_expr (isgr_fn, 2,
9599 fold_build1_loc (loc, ABS_EXPR, type, arg),
9600 build_real (type, r));
9601 return result;
9602 }
9603 CASE_FLT_FN (BUILT_IN_FINITE):
9604 case BUILT_IN_ISFINITE:
9605 {
9606 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9607 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9608 tree const type = TREE_TYPE (arg);
9609 REAL_VALUE_TYPE r;
9610 char buf[128];
9611
9612 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9613 real_from_string (&r, buf);
9614 result = build_call_expr (isle_fn, 2,
9615 fold_build1_loc (loc, ABS_EXPR, type, arg),
9616 build_real (type, r));
9617 /*result = fold_build2_loc (loc, UNGT_EXPR,
9618 TREE_TYPE (TREE_TYPE (fndecl)),
9619 fold_build1_loc (loc, ABS_EXPR, type, arg),
9620 build_real (type, r));
9621 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9622 TREE_TYPE (TREE_TYPE (fndecl)),
9623 result);*/
9624 return result;
9625 }
9626 case BUILT_IN_ISNORMAL:
9627 {
9628 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9629 islessequal(fabs(x),DBL_MAX). */
9630 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9631 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9632 tree const type = TREE_TYPE (arg);
9633 REAL_VALUE_TYPE rmax, rmin;
9634 char buf[128];
9635
9636 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9637 real_from_string (&rmax, buf);
9638 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9639 real_from_string (&rmin, buf);
9640 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9641 result = build_call_expr (isle_fn, 2, arg,
9642 build_real (type, rmax));
9643 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9644 build_call_expr (isge_fn, 2, arg,
9645 build_real (type, rmin)));
9646 return result;
9647 }
9648 default:
9649 break;
9650 }
9651
9652 return NULL_TREE;
9653 }
9654
9655 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9656 ARG is the argument for the call. */
9657
9658 static tree
9659 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9660 {
9661 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9662 REAL_VALUE_TYPE r;
9663
9664 if (!validate_arg (arg, REAL_TYPE))
9665 return NULL_TREE;
9666
9667 switch (builtin_index)
9668 {
9669 case BUILT_IN_ISINF:
9670 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9671 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9672
9673 if (TREE_CODE (arg) == REAL_CST)
9674 {
9675 r = TREE_REAL_CST (arg);
9676 if (real_isinf (&r))
9677 return real_compare (GT_EXPR, &r, &dconst0)
9678 ? integer_one_node : integer_minus_one_node;
9679 else
9680 return integer_zero_node;
9681 }
9682
9683 return NULL_TREE;
9684
9685 case BUILT_IN_ISINF_SIGN:
9686 {
9687 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9688 /* In a boolean context, GCC will fold the inner COND_EXPR to
9689 1. So e.g. "if (isinf_sign(x))" would be folded to just
9690 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9691 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9692 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9693 tree tmp = NULL_TREE;
9694
9695 arg = builtin_save_expr (arg);
9696
9697 if (signbit_fn && isinf_fn)
9698 {
9699 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9700 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9701
9702 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9703 signbit_call, integer_zero_node);
9704 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9705 isinf_call, integer_zero_node);
9706
9707 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9708 integer_minus_one_node, integer_one_node);
9709 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9710 isinf_call, tmp,
9711 integer_zero_node);
9712 }
9713
9714 return tmp;
9715 }
9716
9717 case BUILT_IN_ISFINITE:
9718 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9719 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9720 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9721
9722 if (TREE_CODE (arg) == REAL_CST)
9723 {
9724 r = TREE_REAL_CST (arg);
9725 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9726 }
9727
9728 return NULL_TREE;
9729
9730 case BUILT_IN_ISNAN:
9731 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9732 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9733
9734 if (TREE_CODE (arg) == REAL_CST)
9735 {
9736 r = TREE_REAL_CST (arg);
9737 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9738 }
9739
9740 arg = builtin_save_expr (arg);
9741 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9742
9743 default:
9744 gcc_unreachable ();
9745 }
9746 }
9747
9748 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9749 This builtin will generate code to return the appropriate floating
9750 point classification depending on the value of the floating point
9751 number passed in. The possible return values must be supplied as
9752 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9753 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9754 one floating point argument which is "type generic". */
9755
9756 static tree
9757 fold_builtin_fpclassify (location_t loc, tree exp)
9758 {
9759 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9760 arg, type, res, tmp;
9761 enum machine_mode mode;
9762 REAL_VALUE_TYPE r;
9763 char buf[128];
9764
9765 /* Verify the required arguments in the original call. */
9766 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9767 INTEGER_TYPE, INTEGER_TYPE,
9768 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9769 return NULL_TREE;
9770
9771 fp_nan = CALL_EXPR_ARG (exp, 0);
9772 fp_infinite = CALL_EXPR_ARG (exp, 1);
9773 fp_normal = CALL_EXPR_ARG (exp, 2);
9774 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9775 fp_zero = CALL_EXPR_ARG (exp, 4);
9776 arg = CALL_EXPR_ARG (exp, 5);
9777 type = TREE_TYPE (arg);
9778 mode = TYPE_MODE (type);
9779 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9780
9781 /* fpclassify(x) ->
9782 isnan(x) ? FP_NAN :
9783 (fabs(x) == Inf ? FP_INFINITE :
9784 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9785 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9786
9787 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9788 build_real (type, dconst0));
9789 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9790 tmp, fp_zero, fp_subnormal);
9791
9792 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9793 real_from_string (&r, buf);
9794 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9795 arg, build_real (type, r));
9796 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9797
9798 if (HONOR_INFINITIES (mode))
9799 {
9800 real_inf (&r);
9801 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9802 build_real (type, r));
9803 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9804 fp_infinite, res);
9805 }
9806
9807 if (HONOR_NANS (mode))
9808 {
9809 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9810 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9811 }
9812
9813 return res;
9814 }
9815
9816 /* Fold a call to an unordered comparison function such as
9817 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9818 being called and ARG0 and ARG1 are the arguments for the call.
9819 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9820 the opposite of the desired result. UNORDERED_CODE is used
9821 for modes that can hold NaNs and ORDERED_CODE is used for
9822 the rest. */
9823
9824 static tree
9825 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9826 enum tree_code unordered_code,
9827 enum tree_code ordered_code)
9828 {
9829 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9830 enum tree_code code;
9831 tree type0, type1;
9832 enum tree_code code0, code1;
9833 tree cmp_type = NULL_TREE;
9834
9835 type0 = TREE_TYPE (arg0);
9836 type1 = TREE_TYPE (arg1);
9837
9838 code0 = TREE_CODE (type0);
9839 code1 = TREE_CODE (type1);
9840
9841 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9842 /* Choose the wider of two real types. */
9843 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9844 ? type0 : type1;
9845 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9846 cmp_type = type0;
9847 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9848 cmp_type = type1;
9849
9850 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9851 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9852
9853 if (unordered_code == UNORDERED_EXPR)
9854 {
9855 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9856 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9857 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9858 }
9859
9860 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9861 : ordered_code;
9862 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9863 fold_build2_loc (loc, code, type, arg0, arg1));
9864 }
9865
9866 /* Fold a call to built-in function FNDECL with 0 arguments.
9867 IGNORE is true if the result of the function call is ignored. This
9868 function returns NULL_TREE if no simplification was possible. */
9869
9870 static tree
9871 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9872 {
9873 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9874 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9875 switch (fcode)
9876 {
9877 CASE_FLT_FN (BUILT_IN_INF):
9878 case BUILT_IN_INFD32:
9879 case BUILT_IN_INFD64:
9880 case BUILT_IN_INFD128:
9881 return fold_builtin_inf (loc, type, true);
9882
9883 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9884 return fold_builtin_inf (loc, type, false);
9885
9886 case BUILT_IN_CLASSIFY_TYPE:
9887 return fold_builtin_classify_type (NULL_TREE);
9888
9889 default:
9890 break;
9891 }
9892 return NULL_TREE;
9893 }
9894
9895 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9896 IGNORE is true if the result of the function call is ignored. This
9897 function returns NULL_TREE if no simplification was possible. */
9898
9899 static tree
9900 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9901 {
9902 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9903 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9904 switch (fcode)
9905 {
9906 case BUILT_IN_CONSTANT_P:
9907 {
9908 tree val = fold_builtin_constant_p (arg0);
9909
9910 /* Gimplification will pull the CALL_EXPR for the builtin out of
9911 an if condition. When not optimizing, we'll not CSE it back.
9912 To avoid link error types of regressions, return false now. */
9913 if (!val && !optimize)
9914 val = integer_zero_node;
9915
9916 return val;
9917 }
9918
9919 case BUILT_IN_CLASSIFY_TYPE:
9920 return fold_builtin_classify_type (arg0);
9921
9922 case BUILT_IN_STRLEN:
9923 return fold_builtin_strlen (loc, type, arg0);
9924
9925 CASE_FLT_FN (BUILT_IN_FABS):
9926 return fold_builtin_fabs (loc, arg0, type);
9927
9928 case BUILT_IN_ABS:
9929 case BUILT_IN_LABS:
9930 case BUILT_IN_LLABS:
9931 case BUILT_IN_IMAXABS:
9932 return fold_builtin_abs (loc, arg0, type);
9933
9934 CASE_FLT_FN (BUILT_IN_CONJ):
9935 if (validate_arg (arg0, COMPLEX_TYPE)
9936 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9937 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9938 break;
9939
9940 CASE_FLT_FN (BUILT_IN_CREAL):
9941 if (validate_arg (arg0, COMPLEX_TYPE)
9942 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9943 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9944 break;
9945
9946 CASE_FLT_FN (BUILT_IN_CIMAG):
9947 if (validate_arg (arg0, COMPLEX_TYPE)
9948 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9949 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9950 break;
9951
9952 CASE_FLT_FN (BUILT_IN_CCOS):
9953 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9954
9955 CASE_FLT_FN (BUILT_IN_CCOSH):
9956 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9957
9958 CASE_FLT_FN (BUILT_IN_CPROJ):
9959 return fold_builtin_cproj(loc, arg0, type);
9960
9961 CASE_FLT_FN (BUILT_IN_CSIN):
9962 if (validate_arg (arg0, COMPLEX_TYPE)
9963 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9964 return do_mpc_arg1 (arg0, type, mpc_sin);
9965 break;
9966
9967 CASE_FLT_FN (BUILT_IN_CSINH):
9968 if (validate_arg (arg0, COMPLEX_TYPE)
9969 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9970 return do_mpc_arg1 (arg0, type, mpc_sinh);
9971 break;
9972
9973 CASE_FLT_FN (BUILT_IN_CTAN):
9974 if (validate_arg (arg0, COMPLEX_TYPE)
9975 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9976 return do_mpc_arg1 (arg0, type, mpc_tan);
9977 break;
9978
9979 CASE_FLT_FN (BUILT_IN_CTANH):
9980 if (validate_arg (arg0, COMPLEX_TYPE)
9981 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9982 return do_mpc_arg1 (arg0, type, mpc_tanh);
9983 break;
9984
9985 CASE_FLT_FN (BUILT_IN_CLOG):
9986 if (validate_arg (arg0, COMPLEX_TYPE)
9987 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9988 return do_mpc_arg1 (arg0, type, mpc_log);
9989 break;
9990
9991 CASE_FLT_FN (BUILT_IN_CSQRT):
9992 if (validate_arg (arg0, COMPLEX_TYPE)
9993 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9994 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9995 break;
9996
9997 CASE_FLT_FN (BUILT_IN_CASIN):
9998 if (validate_arg (arg0, COMPLEX_TYPE)
9999 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10000 return do_mpc_arg1 (arg0, type, mpc_asin);
10001 break;
10002
10003 CASE_FLT_FN (BUILT_IN_CACOS):
10004 if (validate_arg (arg0, COMPLEX_TYPE)
10005 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10006 return do_mpc_arg1 (arg0, type, mpc_acos);
10007 break;
10008
10009 CASE_FLT_FN (BUILT_IN_CATAN):
10010 if (validate_arg (arg0, COMPLEX_TYPE)
10011 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10012 return do_mpc_arg1 (arg0, type, mpc_atan);
10013 break;
10014
10015 CASE_FLT_FN (BUILT_IN_CASINH):
10016 if (validate_arg (arg0, COMPLEX_TYPE)
10017 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10018 return do_mpc_arg1 (arg0, type, mpc_asinh);
10019 break;
10020
10021 CASE_FLT_FN (BUILT_IN_CACOSH):
10022 if (validate_arg (arg0, COMPLEX_TYPE)
10023 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10024 return do_mpc_arg1 (arg0, type, mpc_acosh);
10025 break;
10026
10027 CASE_FLT_FN (BUILT_IN_CATANH):
10028 if (validate_arg (arg0, COMPLEX_TYPE)
10029 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10030 return do_mpc_arg1 (arg0, type, mpc_atanh);
10031 break;
10032
10033 CASE_FLT_FN (BUILT_IN_CABS):
10034 return fold_builtin_cabs (loc, arg0, type, fndecl);
10035
10036 CASE_FLT_FN (BUILT_IN_CARG):
10037 return fold_builtin_carg (loc, arg0, type);
10038
10039 CASE_FLT_FN (BUILT_IN_SQRT):
10040 return fold_builtin_sqrt (loc, arg0, type);
10041
10042 CASE_FLT_FN (BUILT_IN_CBRT):
10043 return fold_builtin_cbrt (loc, arg0, type);
10044
10045 CASE_FLT_FN (BUILT_IN_ASIN):
10046 if (validate_arg (arg0, REAL_TYPE))
10047 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10048 &dconstm1, &dconst1, true);
10049 break;
10050
10051 CASE_FLT_FN (BUILT_IN_ACOS):
10052 if (validate_arg (arg0, REAL_TYPE))
10053 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10054 &dconstm1, &dconst1, true);
10055 break;
10056
10057 CASE_FLT_FN (BUILT_IN_ATAN):
10058 if (validate_arg (arg0, REAL_TYPE))
10059 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10060 break;
10061
10062 CASE_FLT_FN (BUILT_IN_ASINH):
10063 if (validate_arg (arg0, REAL_TYPE))
10064 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10065 break;
10066
10067 CASE_FLT_FN (BUILT_IN_ACOSH):
10068 if (validate_arg (arg0, REAL_TYPE))
10069 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10070 &dconst1, NULL, true);
10071 break;
10072
10073 CASE_FLT_FN (BUILT_IN_ATANH):
10074 if (validate_arg (arg0, REAL_TYPE))
10075 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10076 &dconstm1, &dconst1, false);
10077 break;
10078
10079 CASE_FLT_FN (BUILT_IN_SIN):
10080 if (validate_arg (arg0, REAL_TYPE))
10081 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10082 break;
10083
10084 CASE_FLT_FN (BUILT_IN_COS):
10085 return fold_builtin_cos (loc, arg0, type, fndecl);
10086
10087 CASE_FLT_FN (BUILT_IN_TAN):
10088 return fold_builtin_tan (arg0, type);
10089
10090 CASE_FLT_FN (BUILT_IN_CEXP):
10091 return fold_builtin_cexp (loc, arg0, type);
10092
10093 CASE_FLT_FN (BUILT_IN_CEXPI):
10094 if (validate_arg (arg0, REAL_TYPE))
10095 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10096 break;
10097
10098 CASE_FLT_FN (BUILT_IN_SINH):
10099 if (validate_arg (arg0, REAL_TYPE))
10100 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10101 break;
10102
10103 CASE_FLT_FN (BUILT_IN_COSH):
10104 return fold_builtin_cosh (loc, arg0, type, fndecl);
10105
10106 CASE_FLT_FN (BUILT_IN_TANH):
10107 if (validate_arg (arg0, REAL_TYPE))
10108 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10109 break;
10110
10111 CASE_FLT_FN (BUILT_IN_ERF):
10112 if (validate_arg (arg0, REAL_TYPE))
10113 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10114 break;
10115
10116 CASE_FLT_FN (BUILT_IN_ERFC):
10117 if (validate_arg (arg0, REAL_TYPE))
10118 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10119 break;
10120
10121 CASE_FLT_FN (BUILT_IN_TGAMMA):
10122 if (validate_arg (arg0, REAL_TYPE))
10123 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10124 break;
10125
10126 CASE_FLT_FN (BUILT_IN_EXP):
10127 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10128
10129 CASE_FLT_FN (BUILT_IN_EXP2):
10130 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10131
10132 CASE_FLT_FN (BUILT_IN_EXP10):
10133 CASE_FLT_FN (BUILT_IN_POW10):
10134 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10135
10136 CASE_FLT_FN (BUILT_IN_EXPM1):
10137 if (validate_arg (arg0, REAL_TYPE))
10138 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10139 break;
10140
10141 CASE_FLT_FN (BUILT_IN_LOG):
10142 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10143
10144 CASE_FLT_FN (BUILT_IN_LOG2):
10145 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10146
10147 CASE_FLT_FN (BUILT_IN_LOG10):
10148 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10149
10150 CASE_FLT_FN (BUILT_IN_LOG1P):
10151 if (validate_arg (arg0, REAL_TYPE))
10152 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10153 &dconstm1, NULL, false);
10154 break;
10155
10156 CASE_FLT_FN (BUILT_IN_J0):
10157 if (validate_arg (arg0, REAL_TYPE))
10158 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10159 NULL, NULL, 0);
10160 break;
10161
10162 CASE_FLT_FN (BUILT_IN_J1):
10163 if (validate_arg (arg0, REAL_TYPE))
10164 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10165 NULL, NULL, 0);
10166 break;
10167
10168 CASE_FLT_FN (BUILT_IN_Y0):
10169 if (validate_arg (arg0, REAL_TYPE))
10170 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10171 &dconst0, NULL, false);
10172 break;
10173
10174 CASE_FLT_FN (BUILT_IN_Y1):
10175 if (validate_arg (arg0, REAL_TYPE))
10176 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10177 &dconst0, NULL, false);
10178 break;
10179
10180 CASE_FLT_FN (BUILT_IN_NAN):
10181 case BUILT_IN_NAND32:
10182 case BUILT_IN_NAND64:
10183 case BUILT_IN_NAND128:
10184 return fold_builtin_nan (arg0, type, true);
10185
10186 CASE_FLT_FN (BUILT_IN_NANS):
10187 return fold_builtin_nan (arg0, type, false);
10188
10189 CASE_FLT_FN (BUILT_IN_FLOOR):
10190 return fold_builtin_floor (loc, fndecl, arg0);
10191
10192 CASE_FLT_FN (BUILT_IN_CEIL):
10193 return fold_builtin_ceil (loc, fndecl, arg0);
10194
10195 CASE_FLT_FN (BUILT_IN_TRUNC):
10196 return fold_builtin_trunc (loc, fndecl, arg0);
10197
10198 CASE_FLT_FN (BUILT_IN_ROUND):
10199 return fold_builtin_round (loc, fndecl, arg0);
10200
10201 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10202 CASE_FLT_FN (BUILT_IN_RINT):
10203 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10204
10205 CASE_FLT_FN (BUILT_IN_LCEIL):
10206 CASE_FLT_FN (BUILT_IN_LLCEIL):
10207 CASE_FLT_FN (BUILT_IN_LFLOOR):
10208 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10209 CASE_FLT_FN (BUILT_IN_LROUND):
10210 CASE_FLT_FN (BUILT_IN_LLROUND):
10211 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10212
10213 CASE_FLT_FN (BUILT_IN_LRINT):
10214 CASE_FLT_FN (BUILT_IN_LLRINT):
10215 return fold_fixed_mathfn (loc, fndecl, arg0);
10216
10217 case BUILT_IN_BSWAP32:
10218 case BUILT_IN_BSWAP64:
10219 return fold_builtin_bswap (fndecl, arg0);
10220
10221 CASE_INT_FN (BUILT_IN_FFS):
10222 CASE_INT_FN (BUILT_IN_CLZ):
10223 CASE_INT_FN (BUILT_IN_CTZ):
10224 CASE_INT_FN (BUILT_IN_POPCOUNT):
10225 CASE_INT_FN (BUILT_IN_PARITY):
10226 return fold_builtin_bitop (fndecl, arg0);
10227
10228 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10229 return fold_builtin_signbit (loc, arg0, type);
10230
10231 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10232 return fold_builtin_significand (loc, arg0, type);
10233
10234 CASE_FLT_FN (BUILT_IN_ILOGB):
10235 CASE_FLT_FN (BUILT_IN_LOGB):
10236 return fold_builtin_logb (loc, arg0, type);
10237
10238 case BUILT_IN_ISASCII:
10239 return fold_builtin_isascii (loc, arg0);
10240
10241 case BUILT_IN_TOASCII:
10242 return fold_builtin_toascii (loc, arg0);
10243
10244 case BUILT_IN_ISDIGIT:
10245 return fold_builtin_isdigit (loc, arg0);
10246
10247 CASE_FLT_FN (BUILT_IN_FINITE):
10248 case BUILT_IN_FINITED32:
10249 case BUILT_IN_FINITED64:
10250 case BUILT_IN_FINITED128:
10251 case BUILT_IN_ISFINITE:
10252 {
10253 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10254 if (ret)
10255 return ret;
10256 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10257 }
10258
10259 CASE_FLT_FN (BUILT_IN_ISINF):
10260 case BUILT_IN_ISINFD32:
10261 case BUILT_IN_ISINFD64:
10262 case BUILT_IN_ISINFD128:
10263 {
10264 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10265 if (ret)
10266 return ret;
10267 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10268 }
10269
10270 case BUILT_IN_ISNORMAL:
10271 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10272
10273 case BUILT_IN_ISINF_SIGN:
10274 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10275
10276 CASE_FLT_FN (BUILT_IN_ISNAN):
10277 case BUILT_IN_ISNAND32:
10278 case BUILT_IN_ISNAND64:
10279 case BUILT_IN_ISNAND128:
10280 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10281
10282 case BUILT_IN_PRINTF:
10283 case BUILT_IN_PRINTF_UNLOCKED:
10284 case BUILT_IN_VPRINTF:
10285 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10286
10287 case BUILT_IN_FREE:
10288 if (integer_zerop (arg0))
10289 return build_empty_stmt (loc);
10290 break;
10291
10292 default:
10293 break;
10294 }
10295
10296 return NULL_TREE;
10297
10298 }
10299
10300 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10301 IGNORE is true if the result of the function call is ignored. This
10302 function returns NULL_TREE if no simplification was possible. */
10303
10304 static tree
10305 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10306 {
10307 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10308 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10309
10310 switch (fcode)
10311 {
10312 CASE_FLT_FN (BUILT_IN_JN):
10313 if (validate_arg (arg0, INTEGER_TYPE)
10314 && validate_arg (arg1, REAL_TYPE))
10315 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10316 break;
10317
10318 CASE_FLT_FN (BUILT_IN_YN):
10319 if (validate_arg (arg0, INTEGER_TYPE)
10320 && validate_arg (arg1, REAL_TYPE))
10321 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10322 &dconst0, false);
10323 break;
10324
10325 CASE_FLT_FN (BUILT_IN_DREM):
10326 CASE_FLT_FN (BUILT_IN_REMAINDER):
10327 if (validate_arg (arg0, REAL_TYPE)
10328 && validate_arg(arg1, REAL_TYPE))
10329 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10330 break;
10331
10332 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10333 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10334 if (validate_arg (arg0, REAL_TYPE)
10335 && validate_arg(arg1, POINTER_TYPE))
10336 return do_mpfr_lgamma_r (arg0, arg1, type);
10337 break;
10338
10339 CASE_FLT_FN (BUILT_IN_ATAN2):
10340 if (validate_arg (arg0, REAL_TYPE)
10341 && validate_arg(arg1, REAL_TYPE))
10342 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10343 break;
10344
10345 CASE_FLT_FN (BUILT_IN_FDIM):
10346 if (validate_arg (arg0, REAL_TYPE)
10347 && validate_arg(arg1, REAL_TYPE))
10348 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10349 break;
10350
10351 CASE_FLT_FN (BUILT_IN_HYPOT):
10352 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10353
10354 CASE_FLT_FN (BUILT_IN_CPOW):
10355 if (validate_arg (arg0, COMPLEX_TYPE)
10356 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10357 && validate_arg (arg1, COMPLEX_TYPE)
10358 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10359 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10360 break;
10361
10362 CASE_FLT_FN (BUILT_IN_LDEXP):
10363 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10364 CASE_FLT_FN (BUILT_IN_SCALBN):
10365 CASE_FLT_FN (BUILT_IN_SCALBLN):
10366 return fold_builtin_load_exponent (loc, arg0, arg1,
10367 type, /*ldexp=*/false);
10368
10369 CASE_FLT_FN (BUILT_IN_FREXP):
10370 return fold_builtin_frexp (loc, arg0, arg1, type);
10371
10372 CASE_FLT_FN (BUILT_IN_MODF):
10373 return fold_builtin_modf (loc, arg0, arg1, type);
10374
10375 case BUILT_IN_BZERO:
10376 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10377
10378 case BUILT_IN_FPUTS:
10379 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10380
10381 case BUILT_IN_FPUTS_UNLOCKED:
10382 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10383
10384 case BUILT_IN_STRSTR:
10385 return fold_builtin_strstr (loc, arg0, arg1, type);
10386
10387 case BUILT_IN_STRCAT:
10388 return fold_builtin_strcat (loc, arg0, arg1);
10389
10390 case BUILT_IN_STRSPN:
10391 return fold_builtin_strspn (loc, arg0, arg1);
10392
10393 case BUILT_IN_STRCSPN:
10394 return fold_builtin_strcspn (loc, arg0, arg1);
10395
10396 case BUILT_IN_STRCHR:
10397 case BUILT_IN_INDEX:
10398 return fold_builtin_strchr (loc, arg0, arg1, type);
10399
10400 case BUILT_IN_STRRCHR:
10401 case BUILT_IN_RINDEX:
10402 return fold_builtin_strrchr (loc, arg0, arg1, type);
10403
10404 case BUILT_IN_STRCPY:
10405 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10406
10407 case BUILT_IN_STPCPY:
10408 if (ignore)
10409 {
10410 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10411 if (!fn)
10412 break;
10413
10414 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10415 }
10416 else
10417 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10418 break;
10419
10420 case BUILT_IN_STRCMP:
10421 return fold_builtin_strcmp (loc, arg0, arg1);
10422
10423 case BUILT_IN_STRPBRK:
10424 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10425
10426 case BUILT_IN_EXPECT:
10427 return fold_builtin_expect (loc, arg0, arg1);
10428
10429 CASE_FLT_FN (BUILT_IN_POW):
10430 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10431
10432 CASE_FLT_FN (BUILT_IN_POWI):
10433 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10434
10435 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10436 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10437
10438 CASE_FLT_FN (BUILT_IN_FMIN):
10439 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10440
10441 CASE_FLT_FN (BUILT_IN_FMAX):
10442 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10443
10444 case BUILT_IN_ISGREATER:
10445 return fold_builtin_unordered_cmp (loc, fndecl,
10446 arg0, arg1, UNLE_EXPR, LE_EXPR);
10447 case BUILT_IN_ISGREATEREQUAL:
10448 return fold_builtin_unordered_cmp (loc, fndecl,
10449 arg0, arg1, UNLT_EXPR, LT_EXPR);
10450 case BUILT_IN_ISLESS:
10451 return fold_builtin_unordered_cmp (loc, fndecl,
10452 arg0, arg1, UNGE_EXPR, GE_EXPR);
10453 case BUILT_IN_ISLESSEQUAL:
10454 return fold_builtin_unordered_cmp (loc, fndecl,
10455 arg0, arg1, UNGT_EXPR, GT_EXPR);
10456 case BUILT_IN_ISLESSGREATER:
10457 return fold_builtin_unordered_cmp (loc, fndecl,
10458 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10459 case BUILT_IN_ISUNORDERED:
10460 return fold_builtin_unordered_cmp (loc, fndecl,
10461 arg0, arg1, UNORDERED_EXPR,
10462 NOP_EXPR);
10463
10464 /* We do the folding for va_start in the expander. */
10465 case BUILT_IN_VA_START:
10466 break;
10467
10468 case BUILT_IN_SPRINTF:
10469 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10470
10471 case BUILT_IN_OBJECT_SIZE:
10472 return fold_builtin_object_size (arg0, arg1);
10473
10474 case BUILT_IN_PRINTF:
10475 case BUILT_IN_PRINTF_UNLOCKED:
10476 case BUILT_IN_VPRINTF:
10477 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10478
10479 case BUILT_IN_PRINTF_CHK:
10480 case BUILT_IN_VPRINTF_CHK:
10481 if (!validate_arg (arg0, INTEGER_TYPE)
10482 || TREE_SIDE_EFFECTS (arg0))
10483 return NULL_TREE;
10484 else
10485 return fold_builtin_printf (loc, fndecl,
10486 arg1, NULL_TREE, ignore, fcode);
10487 break;
10488
10489 case BUILT_IN_FPRINTF:
10490 case BUILT_IN_FPRINTF_UNLOCKED:
10491 case BUILT_IN_VFPRINTF:
10492 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10493 ignore, fcode);
10494
10495 default:
10496 break;
10497 }
10498 return NULL_TREE;
10499 }
10500
10501 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10502 and ARG2. IGNORE is true if the result of the function call is ignored.
10503 This function returns NULL_TREE if no simplification was possible. */
10504
10505 static tree
10506 fold_builtin_3 (location_t loc, tree fndecl,
10507 tree arg0, tree arg1, tree arg2, bool ignore)
10508 {
10509 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10510 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10511 switch (fcode)
10512 {
10513
10514 CASE_FLT_FN (BUILT_IN_SINCOS):
10515 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10516
10517 CASE_FLT_FN (BUILT_IN_FMA):
10518 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10519 break;
10520
10521 CASE_FLT_FN (BUILT_IN_REMQUO):
10522 if (validate_arg (arg0, REAL_TYPE)
10523 && validate_arg(arg1, REAL_TYPE)
10524 && validate_arg(arg2, POINTER_TYPE))
10525 return do_mpfr_remquo (arg0, arg1, arg2);
10526 break;
10527
10528 case BUILT_IN_MEMSET:
10529 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10530
10531 case BUILT_IN_BCOPY:
10532 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10533 void_type_node, true, /*endp=*/3);
10534
10535 case BUILT_IN_MEMCPY:
10536 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10537 type, ignore, /*endp=*/0);
10538
10539 case BUILT_IN_MEMPCPY:
10540 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10541 type, ignore, /*endp=*/1);
10542
10543 case BUILT_IN_MEMMOVE:
10544 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10545 type, ignore, /*endp=*/3);
10546
10547 case BUILT_IN_STRNCAT:
10548 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10549
10550 case BUILT_IN_STRNCPY:
10551 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10552
10553 case BUILT_IN_STRNCMP:
10554 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10555
10556 case BUILT_IN_MEMCHR:
10557 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10558
10559 case BUILT_IN_BCMP:
10560 case BUILT_IN_MEMCMP:
10561 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10562
10563 case BUILT_IN_SPRINTF:
10564 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10565
10566 case BUILT_IN_SNPRINTF:
10567 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10568
10569 case BUILT_IN_STRCPY_CHK:
10570 case BUILT_IN_STPCPY_CHK:
10571 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10572 ignore, fcode);
10573
10574 case BUILT_IN_STRCAT_CHK:
10575 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10576
10577 case BUILT_IN_PRINTF_CHK:
10578 case BUILT_IN_VPRINTF_CHK:
10579 if (!validate_arg (arg0, INTEGER_TYPE)
10580 || TREE_SIDE_EFFECTS (arg0))
10581 return NULL_TREE;
10582 else
10583 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10584 break;
10585
10586 case BUILT_IN_FPRINTF:
10587 case BUILT_IN_FPRINTF_UNLOCKED:
10588 case BUILT_IN_VFPRINTF:
10589 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10590 ignore, fcode);
10591
10592 case BUILT_IN_FPRINTF_CHK:
10593 case BUILT_IN_VFPRINTF_CHK:
10594 if (!validate_arg (arg1, INTEGER_TYPE)
10595 || TREE_SIDE_EFFECTS (arg1))
10596 return NULL_TREE;
10597 else
10598 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10599 ignore, fcode);
10600
10601 default:
10602 break;
10603 }
10604 return NULL_TREE;
10605 }
10606
10607 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10608 ARG2, and ARG3. IGNORE is true if the result of the function call is
10609 ignored. This function returns NULL_TREE if no simplification was
10610 possible. */
10611
10612 static tree
10613 fold_builtin_4 (location_t loc, tree fndecl,
10614 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10615 {
10616 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10617
10618 switch (fcode)
10619 {
10620 case BUILT_IN_MEMCPY_CHK:
10621 case BUILT_IN_MEMPCPY_CHK:
10622 case BUILT_IN_MEMMOVE_CHK:
10623 case BUILT_IN_MEMSET_CHK:
10624 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10625 NULL_TREE, ignore,
10626 DECL_FUNCTION_CODE (fndecl));
10627
10628 case BUILT_IN_STRNCPY_CHK:
10629 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10630
10631 case BUILT_IN_STRNCAT_CHK:
10632 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10633
10634 case BUILT_IN_SNPRINTF:
10635 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10636
10637 case BUILT_IN_FPRINTF_CHK:
10638 case BUILT_IN_VFPRINTF_CHK:
10639 if (!validate_arg (arg1, INTEGER_TYPE)
10640 || TREE_SIDE_EFFECTS (arg1))
10641 return NULL_TREE;
10642 else
10643 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10644 ignore, fcode);
10645 break;
10646
10647 default:
10648 break;
10649 }
10650 return NULL_TREE;
10651 }
10652
10653 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10654 arguments, where NARGS <= 4. IGNORE is true if the result of the
10655 function call is ignored. This function returns NULL_TREE if no
10656 simplification was possible. Note that this only folds builtins with
10657 fixed argument patterns. Foldings that do varargs-to-varargs
10658 transformations, or that match calls with more than 4 arguments,
10659 need to be handled with fold_builtin_varargs instead. */
10660
10661 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10662
10663 static tree
10664 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10665 {
10666 tree ret = NULL_TREE;
10667
10668 switch (nargs)
10669 {
10670 case 0:
10671 ret = fold_builtin_0 (loc, fndecl, ignore);
10672 break;
10673 case 1:
10674 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10675 break;
10676 case 2:
10677 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10678 break;
10679 case 3:
10680 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10681 break;
10682 case 4:
10683 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10684 ignore);
10685 break;
10686 default:
10687 break;
10688 }
10689 if (ret)
10690 {
10691 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10692 SET_EXPR_LOCATION (ret, loc);
10693 TREE_NO_WARNING (ret) = 1;
10694 return ret;
10695 }
10696 return NULL_TREE;
10697 }
10698
10699 /* Builtins with folding operations that operate on "..." arguments
10700 need special handling; we need to store the arguments in a convenient
10701 data structure before attempting any folding. Fortunately there are
10702 only a few builtins that fall into this category. FNDECL is the
10703 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10704 result of the function call is ignored. */
10705
10706 static tree
10707 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10708 bool ignore ATTRIBUTE_UNUSED)
10709 {
10710 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10711 tree ret = NULL_TREE;
10712
10713 switch (fcode)
10714 {
10715 case BUILT_IN_SPRINTF_CHK:
10716 case BUILT_IN_VSPRINTF_CHK:
10717 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10718 break;
10719
10720 case BUILT_IN_SNPRINTF_CHK:
10721 case BUILT_IN_VSNPRINTF_CHK:
10722 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10723 break;
10724
10725 case BUILT_IN_FPCLASSIFY:
10726 ret = fold_builtin_fpclassify (loc, exp);
10727 break;
10728
10729 default:
10730 break;
10731 }
10732 if (ret)
10733 {
10734 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10735 SET_EXPR_LOCATION (ret, loc);
10736 TREE_NO_WARNING (ret) = 1;
10737 return ret;
10738 }
10739 return NULL_TREE;
10740 }
10741
10742 /* Return true if FNDECL shouldn't be folded right now.
10743 If a built-in function has an inline attribute always_inline
10744 wrapper, defer folding it after always_inline functions have
10745 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10746 might not be performed. */
10747
10748 static bool
10749 avoid_folding_inline_builtin (tree fndecl)
10750 {
10751 return (DECL_DECLARED_INLINE_P (fndecl)
10752 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10753 && cfun
10754 && !cfun->always_inline_functions_inlined
10755 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10756 }
10757
10758 /* A wrapper function for builtin folding that prevents warnings for
10759 "statement without effect" and the like, caused by removing the
10760 call node earlier than the warning is generated. */
10761
10762 tree
10763 fold_call_expr (location_t loc, tree exp, bool ignore)
10764 {
10765 tree ret = NULL_TREE;
10766 tree fndecl = get_callee_fndecl (exp);
10767 if (fndecl
10768 && TREE_CODE (fndecl) == FUNCTION_DECL
10769 && DECL_BUILT_IN (fndecl)
10770 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10771 yet. Defer folding until we see all the arguments
10772 (after inlining). */
10773 && !CALL_EXPR_VA_ARG_PACK (exp))
10774 {
10775 int nargs = call_expr_nargs (exp);
10776
10777 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10778 instead last argument is __builtin_va_arg_pack (). Defer folding
10779 even in that case, until arguments are finalized. */
10780 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10781 {
10782 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10783 if (fndecl2
10784 && TREE_CODE (fndecl2) == FUNCTION_DECL
10785 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10786 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10787 return NULL_TREE;
10788 }
10789
10790 if (avoid_folding_inline_builtin (fndecl))
10791 return NULL_TREE;
10792
10793 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10794 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10795 CALL_EXPR_ARGP (exp), ignore);
10796 else
10797 {
10798 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10799 {
10800 tree *args = CALL_EXPR_ARGP (exp);
10801 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10802 }
10803 if (!ret)
10804 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10805 if (ret)
10806 return ret;
10807 }
10808 }
10809 return NULL_TREE;
10810 }
10811
10812 /* Conveniently construct a function call expression. FNDECL names the
10813 function to be called and N arguments are passed in the array
10814 ARGARRAY. */
10815
10816 tree
10817 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10818 {
10819 tree fntype = TREE_TYPE (fndecl);
10820 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10821
10822 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10823 }
10824
10825 /* Conveniently construct a function call expression. FNDECL names the
10826 function to be called and the arguments are passed in the vector
10827 VEC. */
10828
10829 tree
10830 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10831 {
10832 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10833 VEC_address (tree, vec));
10834 }
10835
10836
10837 /* Conveniently construct a function call expression. FNDECL names the
10838 function to be called, N is the number of arguments, and the "..."
10839 parameters are the argument expressions. */
10840
10841 tree
10842 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10843 {
10844 va_list ap;
10845 tree *argarray = XALLOCAVEC (tree, n);
10846 int i;
10847
10848 va_start (ap, n);
10849 for (i = 0; i < n; i++)
10850 argarray[i] = va_arg (ap, tree);
10851 va_end (ap);
10852 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10853 }
10854
10855 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10856 varargs macros aren't supported by all bootstrap compilers. */
10857
10858 tree
10859 build_call_expr (tree fndecl, int n, ...)
10860 {
10861 va_list ap;
10862 tree *argarray = XALLOCAVEC (tree, n);
10863 int i;
10864
10865 va_start (ap, n);
10866 for (i = 0; i < n; i++)
10867 argarray[i] = va_arg (ap, tree);
10868 va_end (ap);
10869 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10870 }
10871
10872 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10873 N arguments are passed in the array ARGARRAY. */
10874
10875 tree
10876 fold_builtin_call_array (location_t loc, tree type,
10877 tree fn,
10878 int n,
10879 tree *argarray)
10880 {
10881 tree ret = NULL_TREE;
10882 tree exp;
10883
10884 if (TREE_CODE (fn) == ADDR_EXPR)
10885 {
10886 tree fndecl = TREE_OPERAND (fn, 0);
10887 if (TREE_CODE (fndecl) == FUNCTION_DECL
10888 && DECL_BUILT_IN (fndecl))
10889 {
10890 /* If last argument is __builtin_va_arg_pack (), arguments to this
10891 function are not finalized yet. Defer folding until they are. */
10892 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10893 {
10894 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10895 if (fndecl2
10896 && TREE_CODE (fndecl2) == FUNCTION_DECL
10897 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10898 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10899 return build_call_array_loc (loc, type, fn, n, argarray);
10900 }
10901 if (avoid_folding_inline_builtin (fndecl))
10902 return build_call_array_loc (loc, type, fn, n, argarray);
10903 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10904 {
10905 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10906 if (ret)
10907 return ret;
10908
10909 return build_call_array_loc (loc, type, fn, n, argarray);
10910 }
10911 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10912 {
10913 /* First try the transformations that don't require consing up
10914 an exp. */
10915 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10916 if (ret)
10917 return ret;
10918 }
10919
10920 /* If we got this far, we need to build an exp. */
10921 exp = build_call_array_loc (loc, type, fn, n, argarray);
10922 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10923 return ret ? ret : exp;
10924 }
10925 }
10926
10927 return build_call_array_loc (loc, type, fn, n, argarray);
10928 }
10929
10930 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10931 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10932 of arguments in ARGS to be omitted. OLDNARGS is the number of
10933 elements in ARGS. */
10934
10935 static tree
10936 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10937 int skip, tree fndecl, int n, va_list newargs)
10938 {
10939 int nargs = oldnargs - skip + n;
10940 tree *buffer;
10941
10942 if (n > 0)
10943 {
10944 int i, j;
10945
10946 buffer = XALLOCAVEC (tree, nargs);
10947 for (i = 0; i < n; i++)
10948 buffer[i] = va_arg (newargs, tree);
10949 for (j = skip; j < oldnargs; j++, i++)
10950 buffer[i] = args[j];
10951 }
10952 else
10953 buffer = args + skip;
10954
10955 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10956 }
10957
10958 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10959 list ARGS along with N new arguments specified as the "..."
10960 parameters. SKIP is the number of arguments in ARGS to be omitted.
10961 OLDNARGS is the number of elements in ARGS. */
10962
10963 static tree
10964 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10965 int skip, tree fndecl, int n, ...)
10966 {
10967 va_list ap;
10968 tree t;
10969
10970 va_start (ap, n);
10971 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10972 va_end (ap);
10973
10974 return t;
10975 }
10976
10977 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10978 along with N new arguments specified as the "..." parameters. SKIP
10979 is the number of arguments in EXP to be omitted. This function is used
10980 to do varargs-to-varargs transformations. */
10981
10982 static tree
10983 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10984 {
10985 va_list ap;
10986 tree t;
10987
10988 va_start (ap, n);
10989 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10990 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10991 va_end (ap);
10992
10993 return t;
10994 }
10995
10996 /* Validate a single argument ARG against a tree code CODE representing
10997 a type. */
10998
10999 static bool
11000 validate_arg (const_tree arg, enum tree_code code)
11001 {
11002 if (!arg)
11003 return false;
11004 else if (code == POINTER_TYPE)
11005 return POINTER_TYPE_P (TREE_TYPE (arg));
11006 else if (code == INTEGER_TYPE)
11007 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11008 return code == TREE_CODE (TREE_TYPE (arg));
11009 }
11010
11011 /* This function validates the types of a function call argument list
11012 against a specified list of tree_codes. If the last specifier is a 0,
11013 that represents an ellipses, otherwise the last specifier must be a
11014 VOID_TYPE.
11015
11016 This is the GIMPLE version of validate_arglist. Eventually we want to
11017 completely convert builtins.c to work from GIMPLEs and the tree based
11018 validate_arglist will then be removed. */
11019
11020 bool
11021 validate_gimple_arglist (const_gimple call, ...)
11022 {
11023 enum tree_code code;
11024 bool res = 0;
11025 va_list ap;
11026 const_tree arg;
11027 size_t i;
11028
11029 va_start (ap, call);
11030 i = 0;
11031
11032 do
11033 {
11034 code = (enum tree_code) va_arg (ap, int);
11035 switch (code)
11036 {
11037 case 0:
11038 /* This signifies an ellipses, any further arguments are all ok. */
11039 res = true;
11040 goto end;
11041 case VOID_TYPE:
11042 /* This signifies an endlink, if no arguments remain, return
11043 true, otherwise return false. */
11044 res = (i == gimple_call_num_args (call));
11045 goto end;
11046 default:
11047 /* If no parameters remain or the parameter's code does not
11048 match the specified code, return false. Otherwise continue
11049 checking any remaining arguments. */
11050 arg = gimple_call_arg (call, i++);
11051 if (!validate_arg (arg, code))
11052 goto end;
11053 break;
11054 }
11055 }
11056 while (1);
11057
11058 /* We need gotos here since we can only have one VA_CLOSE in a
11059 function. */
11060 end: ;
11061 va_end (ap);
11062
11063 return res;
11064 }
11065
11066 /* This function validates the types of a function call argument list
11067 against a specified list of tree_codes. If the last specifier is a 0,
11068 that represents an ellipses, otherwise the last specifier must be a
11069 VOID_TYPE. */
11070
11071 bool
11072 validate_arglist (const_tree callexpr, ...)
11073 {
11074 enum tree_code code;
11075 bool res = 0;
11076 va_list ap;
11077 const_call_expr_arg_iterator iter;
11078 const_tree arg;
11079
11080 va_start (ap, callexpr);
11081 init_const_call_expr_arg_iterator (callexpr, &iter);
11082
11083 do
11084 {
11085 code = (enum tree_code) va_arg (ap, int);
11086 switch (code)
11087 {
11088 case 0:
11089 /* This signifies an ellipses, any further arguments are all ok. */
11090 res = true;
11091 goto end;
11092 case VOID_TYPE:
11093 /* This signifies an endlink, if no arguments remain, return
11094 true, otherwise return false. */
11095 res = !more_const_call_expr_args_p (&iter);
11096 goto end;
11097 default:
11098 /* If no parameters remain or the parameter's code does not
11099 match the specified code, return false. Otherwise continue
11100 checking any remaining arguments. */
11101 arg = next_const_call_expr_arg (&iter);
11102 if (!validate_arg (arg, code))
11103 goto end;
11104 break;
11105 }
11106 }
11107 while (1);
11108
11109 /* We need gotos here since we can only have one VA_CLOSE in a
11110 function. */
11111 end: ;
11112 va_end (ap);
11113
11114 return res;
11115 }
11116
11117 /* Default target-specific builtin expander that does nothing. */
11118
11119 rtx
11120 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11121 rtx target ATTRIBUTE_UNUSED,
11122 rtx subtarget ATTRIBUTE_UNUSED,
11123 enum machine_mode mode ATTRIBUTE_UNUSED,
11124 int ignore ATTRIBUTE_UNUSED)
11125 {
11126 return NULL_RTX;
11127 }
11128
11129 /* Returns true is EXP represents data that would potentially reside
11130 in a readonly section. */
11131
11132 static bool
11133 readonly_data_expr (tree exp)
11134 {
11135 STRIP_NOPS (exp);
11136
11137 if (TREE_CODE (exp) != ADDR_EXPR)
11138 return false;
11139
11140 exp = get_base_address (TREE_OPERAND (exp, 0));
11141 if (!exp)
11142 return false;
11143
11144 /* Make sure we call decl_readonly_section only for trees it
11145 can handle (since it returns true for everything it doesn't
11146 understand). */
11147 if (TREE_CODE (exp) == STRING_CST
11148 || TREE_CODE (exp) == CONSTRUCTOR
11149 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11150 return decl_readonly_section (exp, 0);
11151 else
11152 return false;
11153 }
11154
11155 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11156 to the call, and TYPE is its return type.
11157
11158 Return NULL_TREE if no simplification was possible, otherwise return the
11159 simplified form of the call as a tree.
11160
11161 The simplified form may be a constant or other expression which
11162 computes the same value, but in a more efficient manner (including
11163 calls to other builtin functions).
11164
11165 The call may contain arguments which need to be evaluated, but
11166 which are not useful to determine the result of the call. In
11167 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11168 COMPOUND_EXPR will be an argument which must be evaluated.
11169 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11170 COMPOUND_EXPR in the chain will contain the tree for the simplified
11171 form of the builtin function call. */
11172
11173 static tree
11174 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11175 {
11176 if (!validate_arg (s1, POINTER_TYPE)
11177 || !validate_arg (s2, POINTER_TYPE))
11178 return NULL_TREE;
11179 else
11180 {
11181 tree fn;
11182 const char *p1, *p2;
11183
11184 p2 = c_getstr (s2);
11185 if (p2 == NULL)
11186 return NULL_TREE;
11187
11188 p1 = c_getstr (s1);
11189 if (p1 != NULL)
11190 {
11191 const char *r = strstr (p1, p2);
11192 tree tem;
11193
11194 if (r == NULL)
11195 return build_int_cst (TREE_TYPE (s1), 0);
11196
11197 /* Return an offset into the constant string argument. */
11198 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11199 s1, size_int (r - p1));
11200 return fold_convert_loc (loc, type, tem);
11201 }
11202
11203 /* The argument is const char *, and the result is char *, so we need
11204 a type conversion here to avoid a warning. */
11205 if (p2[0] == '\0')
11206 return fold_convert_loc (loc, type, s1);
11207
11208 if (p2[1] != '\0')
11209 return NULL_TREE;
11210
11211 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11212 if (!fn)
11213 return NULL_TREE;
11214
11215 /* New argument list transforming strstr(s1, s2) to
11216 strchr(s1, s2[0]). */
11217 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11218 }
11219 }
11220
11221 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11222 the call, and TYPE is its return type.
11223
11224 Return NULL_TREE if no simplification was possible, otherwise return the
11225 simplified form of the call as a tree.
11226
11227 The simplified form may be a constant or other expression which
11228 computes the same value, but in a more efficient manner (including
11229 calls to other builtin functions).
11230
11231 The call may contain arguments which need to be evaluated, but
11232 which are not useful to determine the result of the call. In
11233 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11234 COMPOUND_EXPR will be an argument which must be evaluated.
11235 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11236 COMPOUND_EXPR in the chain will contain the tree for the simplified
11237 form of the builtin function call. */
11238
11239 static tree
11240 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11241 {
11242 if (!validate_arg (s1, POINTER_TYPE)
11243 || !validate_arg (s2, INTEGER_TYPE))
11244 return NULL_TREE;
11245 else
11246 {
11247 const char *p1;
11248
11249 if (TREE_CODE (s2) != INTEGER_CST)
11250 return NULL_TREE;
11251
11252 p1 = c_getstr (s1);
11253 if (p1 != NULL)
11254 {
11255 char c;
11256 const char *r;
11257 tree tem;
11258
11259 if (target_char_cast (s2, &c))
11260 return NULL_TREE;
11261
11262 r = strchr (p1, c);
11263
11264 if (r == NULL)
11265 return build_int_cst (TREE_TYPE (s1), 0);
11266
11267 /* Return an offset into the constant string argument. */
11268 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11269 s1, size_int (r - p1));
11270 return fold_convert_loc (loc, type, tem);
11271 }
11272 return NULL_TREE;
11273 }
11274 }
11275
11276 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11277 the call, and TYPE is its return type.
11278
11279 Return NULL_TREE if no simplification was possible, otherwise return the
11280 simplified form of the call as a tree.
11281
11282 The simplified form may be a constant or other expression which
11283 computes the same value, but in a more efficient manner (including
11284 calls to other builtin functions).
11285
11286 The call may contain arguments which need to be evaluated, but
11287 which are not useful to determine the result of the call. In
11288 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11289 COMPOUND_EXPR will be an argument which must be evaluated.
11290 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11291 COMPOUND_EXPR in the chain will contain the tree for the simplified
11292 form of the builtin function call. */
11293
11294 static tree
11295 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11296 {
11297 if (!validate_arg (s1, POINTER_TYPE)
11298 || !validate_arg (s2, INTEGER_TYPE))
11299 return NULL_TREE;
11300 else
11301 {
11302 tree fn;
11303 const char *p1;
11304
11305 if (TREE_CODE (s2) != INTEGER_CST)
11306 return NULL_TREE;
11307
11308 p1 = c_getstr (s1);
11309 if (p1 != NULL)
11310 {
11311 char c;
11312 const char *r;
11313 tree tem;
11314
11315 if (target_char_cast (s2, &c))
11316 return NULL_TREE;
11317
11318 r = strrchr (p1, c);
11319
11320 if (r == NULL)
11321 return build_int_cst (TREE_TYPE (s1), 0);
11322
11323 /* Return an offset into the constant string argument. */
11324 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11325 s1, size_int (r - p1));
11326 return fold_convert_loc (loc, type, tem);
11327 }
11328
11329 if (! integer_zerop (s2))
11330 return NULL_TREE;
11331
11332 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11333 if (!fn)
11334 return NULL_TREE;
11335
11336 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11337 return build_call_expr_loc (loc, fn, 2, s1, s2);
11338 }
11339 }
11340
11341 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11342 to the call, and TYPE is its return type.
11343
11344 Return NULL_TREE if no simplification was possible, otherwise return the
11345 simplified form of the call as a tree.
11346
11347 The simplified form may be a constant or other expression which
11348 computes the same value, but in a more efficient manner (including
11349 calls to other builtin functions).
11350
11351 The call may contain arguments which need to be evaluated, but
11352 which are not useful to determine the result of the call. In
11353 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11354 COMPOUND_EXPR will be an argument which must be evaluated.
11355 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11356 COMPOUND_EXPR in the chain will contain the tree for the simplified
11357 form of the builtin function call. */
11358
11359 static tree
11360 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11361 {
11362 if (!validate_arg (s1, POINTER_TYPE)
11363 || !validate_arg (s2, POINTER_TYPE))
11364 return NULL_TREE;
11365 else
11366 {
11367 tree fn;
11368 const char *p1, *p2;
11369
11370 p2 = c_getstr (s2);
11371 if (p2 == NULL)
11372 return NULL_TREE;
11373
11374 p1 = c_getstr (s1);
11375 if (p1 != NULL)
11376 {
11377 const char *r = strpbrk (p1, p2);
11378 tree tem;
11379
11380 if (r == NULL)
11381 return build_int_cst (TREE_TYPE (s1), 0);
11382
11383 /* Return an offset into the constant string argument. */
11384 tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11385 s1, size_int (r - p1));
11386 return fold_convert_loc (loc, type, tem);
11387 }
11388
11389 if (p2[0] == '\0')
11390 /* strpbrk(x, "") == NULL.
11391 Evaluate and ignore s1 in case it had side-effects. */
11392 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11393
11394 if (p2[1] != '\0')
11395 return NULL_TREE; /* Really call strpbrk. */
11396
11397 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11398 if (!fn)
11399 return NULL_TREE;
11400
11401 /* New argument list transforming strpbrk(s1, s2) to
11402 strchr(s1, s2[0]). */
11403 return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11404 }
11405 }
11406
11407 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11408 to the call.
11409
11410 Return NULL_TREE if no simplification was possible, otherwise return the
11411 simplified form of the call as a tree.
11412
11413 The simplified form may be a constant or other expression which
11414 computes the same value, but in a more efficient manner (including
11415 calls to other builtin functions).
11416
11417 The call may contain arguments which need to be evaluated, but
11418 which are not useful to determine the result of the call. In
11419 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11420 COMPOUND_EXPR will be an argument which must be evaluated.
11421 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11422 COMPOUND_EXPR in the chain will contain the tree for the simplified
11423 form of the builtin function call. */
11424
11425 static tree
11426 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11427 {
11428 if (!validate_arg (dst, POINTER_TYPE)
11429 || !validate_arg (src, POINTER_TYPE))
11430 return NULL_TREE;
11431 else
11432 {
11433 const char *p = c_getstr (src);
11434
11435 /* If the string length is zero, return the dst parameter. */
11436 if (p && *p == '\0')
11437 return dst;
11438
11439 if (optimize_insn_for_speed_p ())
11440 {
11441 /* See if we can store by pieces into (dst + strlen(dst)). */
11442 tree newdst, call;
11443 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11444 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11445
11446 if (!strlen_fn || !strcpy_fn)
11447 return NULL_TREE;
11448
11449 /* If we don't have a movstr we don't want to emit an strcpy
11450 call. We have to do that if the length of the source string
11451 isn't computable (in that case we can use memcpy probably
11452 later expanding to a sequence of mov instructions). If we
11453 have movstr instructions we can emit strcpy calls. */
11454 if (!HAVE_movstr)
11455 {
11456 tree len = c_strlen (src, 1);
11457 if (! len || TREE_SIDE_EFFECTS (len))
11458 return NULL_TREE;
11459 }
11460
11461 /* Stabilize the argument list. */
11462 dst = builtin_save_expr (dst);
11463
11464 /* Create strlen (dst). */
11465 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11466 /* Create (dst p+ strlen (dst)). */
11467
11468 newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11469 TREE_TYPE (dst), dst, newdst);
11470 newdst = builtin_save_expr (newdst);
11471
11472 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11473 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11474 }
11475 return NULL_TREE;
11476 }
11477 }
11478
11479 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11480 arguments to the call.
11481
11482 Return NULL_TREE if no simplification was possible, otherwise return the
11483 simplified form of the call as a tree.
11484
11485 The simplified form may be a constant or other expression which
11486 computes the same value, but in a more efficient manner (including
11487 calls to other builtin functions).
11488
11489 The call may contain arguments which need to be evaluated, but
11490 which are not useful to determine the result of the call. In
11491 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11492 COMPOUND_EXPR will be an argument which must be evaluated.
11493 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11494 COMPOUND_EXPR in the chain will contain the tree for the simplified
11495 form of the builtin function call. */
11496
11497 static tree
11498 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11499 {
11500 if (!validate_arg (dst, POINTER_TYPE)
11501 || !validate_arg (src, POINTER_TYPE)
11502 || !validate_arg (len, INTEGER_TYPE))
11503 return NULL_TREE;
11504 else
11505 {
11506 const char *p = c_getstr (src);
11507
11508 /* If the requested length is zero, or the src parameter string
11509 length is zero, return the dst parameter. */
11510 if (integer_zerop (len) || (p && *p == '\0'))
11511 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11512
11513 /* If the requested len is greater than or equal to the string
11514 length, call strcat. */
11515 if (TREE_CODE (len) == INTEGER_CST && p
11516 && compare_tree_int (len, strlen (p)) >= 0)
11517 {
11518 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11519
11520 /* If the replacement _DECL isn't initialized, don't do the
11521 transformation. */
11522 if (!fn)
11523 return NULL_TREE;
11524
11525 return build_call_expr_loc (loc, fn, 2, dst, src);
11526 }
11527 return NULL_TREE;
11528 }
11529 }
11530
11531 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11532 to the call.
11533
11534 Return NULL_TREE if no simplification was possible, otherwise return the
11535 simplified form of the call as a tree.
11536
11537 The simplified form may be a constant or other expression which
11538 computes the same value, but in a more efficient manner (including
11539 calls to other builtin functions).
11540
11541 The call may contain arguments which need to be evaluated, but
11542 which are not useful to determine the result of the call. In
11543 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11544 COMPOUND_EXPR will be an argument which must be evaluated.
11545 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11546 COMPOUND_EXPR in the chain will contain the tree for the simplified
11547 form of the builtin function call. */
11548
11549 static tree
11550 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11551 {
11552 if (!validate_arg (s1, POINTER_TYPE)
11553 || !validate_arg (s2, POINTER_TYPE))
11554 return NULL_TREE;
11555 else
11556 {
11557 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11558
11559 /* If both arguments are constants, evaluate at compile-time. */
11560 if (p1 && p2)
11561 {
11562 const size_t r = strspn (p1, p2);
11563 return size_int (r);
11564 }
11565
11566 /* If either argument is "", return NULL_TREE. */
11567 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11568 /* Evaluate and ignore both arguments in case either one has
11569 side-effects. */
11570 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11571 s1, s2);
11572 return NULL_TREE;
11573 }
11574 }
11575
11576 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11577 to the call.
11578
11579 Return NULL_TREE if no simplification was possible, otherwise return the
11580 simplified form of the call as a tree.
11581
11582 The simplified form may be a constant or other expression which
11583 computes the same value, but in a more efficient manner (including
11584 calls to other builtin functions).
11585
11586 The call may contain arguments which need to be evaluated, but
11587 which are not useful to determine the result of the call. In
11588 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11589 COMPOUND_EXPR will be an argument which must be evaluated.
11590 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11591 COMPOUND_EXPR in the chain will contain the tree for the simplified
11592 form of the builtin function call. */
11593
11594 static tree
11595 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11596 {
11597 if (!validate_arg (s1, POINTER_TYPE)
11598 || !validate_arg (s2, POINTER_TYPE))
11599 return NULL_TREE;
11600 else
11601 {
11602 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11603
11604 /* If both arguments are constants, evaluate at compile-time. */
11605 if (p1 && p2)
11606 {
11607 const size_t r = strcspn (p1, p2);
11608 return size_int (r);
11609 }
11610
11611 /* If the first argument is "", return NULL_TREE. */
11612 if (p1 && *p1 == '\0')
11613 {
11614 /* Evaluate and ignore argument s2 in case it has
11615 side-effects. */
11616 return omit_one_operand_loc (loc, size_type_node,
11617 size_zero_node, s2);
11618 }
11619
11620 /* If the second argument is "", return __builtin_strlen(s1). */
11621 if (p2 && *p2 == '\0')
11622 {
11623 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11624
11625 /* If the replacement _DECL isn't initialized, don't do the
11626 transformation. */
11627 if (!fn)
11628 return NULL_TREE;
11629
11630 return build_call_expr_loc (loc, fn, 1, s1);
11631 }
11632 return NULL_TREE;
11633 }
11634 }
11635
11636 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11637 to the call. IGNORE is true if the value returned
11638 by the builtin will be ignored. UNLOCKED is true is true if this
11639 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11640 the known length of the string. Return NULL_TREE if no simplification
11641 was possible. */
11642
11643 tree
11644 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11645 bool ignore, bool unlocked, tree len)
11646 {
11647 /* If we're using an unlocked function, assume the other unlocked
11648 functions exist explicitly. */
11649 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11650 : implicit_built_in_decls[BUILT_IN_FPUTC];
11651 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11652 : implicit_built_in_decls[BUILT_IN_FWRITE];
11653
11654 /* If the return value is used, don't do the transformation. */
11655 if (!ignore)
11656 return NULL_TREE;
11657
11658 /* Verify the arguments in the original call. */
11659 if (!validate_arg (arg0, POINTER_TYPE)
11660 || !validate_arg (arg1, POINTER_TYPE))
11661 return NULL_TREE;
11662
11663 if (! len)
11664 len = c_strlen (arg0, 0);
11665
11666 /* Get the length of the string passed to fputs. If the length
11667 can't be determined, punt. */
11668 if (!len
11669 || TREE_CODE (len) != INTEGER_CST)
11670 return NULL_TREE;
11671
11672 switch (compare_tree_int (len, 1))
11673 {
11674 case -1: /* length is 0, delete the call entirely . */
11675 return omit_one_operand_loc (loc, integer_type_node,
11676 integer_zero_node, arg1);;
11677
11678 case 0: /* length is 1, call fputc. */
11679 {
11680 const char *p = c_getstr (arg0);
11681
11682 if (p != NULL)
11683 {
11684 if (fn_fputc)
11685 return build_call_expr_loc (loc, fn_fputc, 2,
11686 build_int_cst (NULL_TREE, p[0]), arg1);
11687 else
11688 return NULL_TREE;
11689 }
11690 }
11691 /* FALLTHROUGH */
11692 case 1: /* length is greater than 1, call fwrite. */
11693 {
11694 /* If optimizing for size keep fputs. */
11695 if (optimize_function_for_size_p (cfun))
11696 return NULL_TREE;
11697 /* New argument list transforming fputs(string, stream) to
11698 fwrite(string, 1, len, stream). */
11699 if (fn_fwrite)
11700 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11701 size_one_node, len, arg1);
11702 else
11703 return NULL_TREE;
11704 }
11705 default:
11706 gcc_unreachable ();
11707 }
11708 return NULL_TREE;
11709 }
11710
11711 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11712 produced. False otherwise. This is done so that we don't output the error
11713 or warning twice or three times. */
11714
11715 bool
11716 fold_builtin_next_arg (tree exp, bool va_start_p)
11717 {
11718 tree fntype = TREE_TYPE (current_function_decl);
11719 int nargs = call_expr_nargs (exp);
11720 tree arg;
11721
11722 if (!stdarg_p (fntype))
11723 {
11724 error ("%<va_start%> used in function with fixed args");
11725 return true;
11726 }
11727
11728 if (va_start_p)
11729 {
11730 if (va_start_p && (nargs != 2))
11731 {
11732 error ("wrong number of arguments to function %<va_start%>");
11733 return true;
11734 }
11735 arg = CALL_EXPR_ARG (exp, 1);
11736 }
11737 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11738 when we checked the arguments and if needed issued a warning. */
11739 else
11740 {
11741 if (nargs == 0)
11742 {
11743 /* Evidently an out of date version of <stdarg.h>; can't validate
11744 va_start's second argument, but can still work as intended. */
11745 warning (0, "%<__builtin_next_arg%> called without an argument");
11746 return true;
11747 }
11748 else if (nargs > 1)
11749 {
11750 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11751 return true;
11752 }
11753 arg = CALL_EXPR_ARG (exp, 0);
11754 }
11755
11756 if (TREE_CODE (arg) == SSA_NAME)
11757 arg = SSA_NAME_VAR (arg);
11758
11759 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11760 or __builtin_next_arg (0) the first time we see it, after checking
11761 the arguments and if needed issuing a warning. */
11762 if (!integer_zerop (arg))
11763 {
11764 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11765
11766 /* Strip off all nops for the sake of the comparison. This
11767 is not quite the same as STRIP_NOPS. It does more.
11768 We must also strip off INDIRECT_EXPR for C++ reference
11769 parameters. */
11770 while (CONVERT_EXPR_P (arg)
11771 || TREE_CODE (arg) == INDIRECT_REF)
11772 arg = TREE_OPERAND (arg, 0);
11773 if (arg != last_parm)
11774 {
11775 /* FIXME: Sometimes with the tree optimizers we can get the
11776 not the last argument even though the user used the last
11777 argument. We just warn and set the arg to be the last
11778 argument so that we will get wrong-code because of
11779 it. */
11780 warning (0, "second parameter of %<va_start%> not last named argument");
11781 }
11782
11783 /* Undefined by C99 7.15.1.4p4 (va_start):
11784 "If the parameter parmN is declared with the register storage
11785 class, with a function or array type, or with a type that is
11786 not compatible with the type that results after application of
11787 the default argument promotions, the behavior is undefined."
11788 */
11789 else if (DECL_REGISTER (arg))
11790 warning (0, "undefined behaviour when second parameter of "
11791 "%<va_start%> is declared with %<register%> storage");
11792
11793 /* We want to verify the second parameter just once before the tree
11794 optimizers are run and then avoid keeping it in the tree,
11795 as otherwise we could warn even for correct code like:
11796 void foo (int i, ...)
11797 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11798 if (va_start_p)
11799 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11800 else
11801 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11802 }
11803 return false;
11804 }
11805
11806
11807 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11808 ORIG may be null if this is a 2-argument call. We don't attempt to
11809 simplify calls with more than 3 arguments.
11810
11811 Return NULL_TREE if no simplification was possible, otherwise return the
11812 simplified form of the call as a tree. If IGNORED is true, it means that
11813 the caller does not use the returned value of the function. */
11814
11815 static tree
11816 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11817 tree orig, int ignored)
11818 {
11819 tree call, retval;
11820 const char *fmt_str = NULL;
11821
11822 /* Verify the required arguments in the original call. We deal with two
11823 types of sprintf() calls: 'sprintf (str, fmt)' and
11824 'sprintf (dest, "%s", orig)'. */
11825 if (!validate_arg (dest, POINTER_TYPE)
11826 || !validate_arg (fmt, POINTER_TYPE))
11827 return NULL_TREE;
11828 if (orig && !validate_arg (orig, POINTER_TYPE))
11829 return NULL_TREE;
11830
11831 /* Check whether the format is a literal string constant. */
11832 fmt_str = c_getstr (fmt);
11833 if (fmt_str == NULL)
11834 return NULL_TREE;
11835
11836 call = NULL_TREE;
11837 retval = NULL_TREE;
11838
11839 if (!init_target_chars ())
11840 return NULL_TREE;
11841
11842 /* If the format doesn't contain % args or %%, use strcpy. */
11843 if (strchr (fmt_str, target_percent) == NULL)
11844 {
11845 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11846
11847 if (!fn)
11848 return NULL_TREE;
11849
11850 /* Don't optimize sprintf (buf, "abc", ptr++). */
11851 if (orig)
11852 return NULL_TREE;
11853
11854 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11855 'format' is known to contain no % formats. */
11856 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11857 if (!ignored)
11858 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11859 }
11860
11861 /* If the format is "%s", use strcpy if the result isn't used. */
11862 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11863 {
11864 tree fn;
11865 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11866
11867 if (!fn)
11868 return NULL_TREE;
11869
11870 /* Don't crash on sprintf (str1, "%s"). */
11871 if (!orig)
11872 return NULL_TREE;
11873
11874 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11875 if (!ignored)
11876 {
11877 retval = c_strlen (orig, 1);
11878 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11879 return NULL_TREE;
11880 }
11881 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11882 }
11883
11884 if (call && retval)
11885 {
11886 retval = fold_convert_loc
11887 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11888 retval);
11889 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11890 }
11891 else
11892 return call;
11893 }
11894
11895 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
11896 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
11897 attempt to simplify calls with more than 4 arguments.
11898
11899 Return NULL_TREE if no simplification was possible, otherwise return the
11900 simplified form of the call as a tree. If IGNORED is true, it means that
11901 the caller does not use the returned value of the function. */
11902
11903 static tree
11904 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
11905 tree orig, int ignored)
11906 {
11907 tree call, retval;
11908 const char *fmt_str = NULL;
11909 unsigned HOST_WIDE_INT destlen;
11910
11911 /* Verify the required arguments in the original call. We deal with two
11912 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
11913 'snprintf (dest, cst, "%s", orig)'. */
11914 if (!validate_arg (dest, POINTER_TYPE)
11915 || !validate_arg (destsize, INTEGER_TYPE)
11916 || !validate_arg (fmt, POINTER_TYPE))
11917 return NULL_TREE;
11918 if (orig && !validate_arg (orig, POINTER_TYPE))
11919 return NULL_TREE;
11920
11921 if (!host_integerp (destsize, 1))
11922 return NULL_TREE;
11923
11924 /* Check whether the format is a literal string constant. */
11925 fmt_str = c_getstr (fmt);
11926 if (fmt_str == NULL)
11927 return NULL_TREE;
11928
11929 call = NULL_TREE;
11930 retval = NULL_TREE;
11931
11932 if (!init_target_chars ())
11933 return NULL_TREE;
11934
11935 destlen = tree_low_cst (destsize, 1);
11936
11937 /* If the format doesn't contain % args or %%, use strcpy. */
11938 if (strchr (fmt_str, target_percent) == NULL)
11939 {
11940 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11941 size_t len = strlen (fmt_str);
11942
11943 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
11944 if (orig)
11945 return NULL_TREE;
11946
11947 /* We could expand this as
11948 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
11949 or to
11950 memcpy (str, fmt_with_nul_at_cstm1, cst);
11951 but in the former case that might increase code size
11952 and in the latter case grow .rodata section too much.
11953 So punt for now. */
11954 if (len >= destlen)
11955 return NULL_TREE;
11956
11957 if (!fn)
11958 return NULL_TREE;
11959
11960 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
11961 'format' is known to contain no % formats and
11962 strlen (fmt) < cst. */
11963 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11964
11965 if (!ignored)
11966 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11967 }
11968
11969 /* If the format is "%s", use strcpy if the result isn't used. */
11970 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11971 {
11972 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11973 unsigned HOST_WIDE_INT origlen;
11974
11975 /* Don't crash on snprintf (str1, cst, "%s"). */
11976 if (!orig)
11977 return NULL_TREE;
11978
11979 retval = c_strlen (orig, 1);
11980 if (!retval || !host_integerp (retval, 1))
11981 return NULL_TREE;
11982
11983 origlen = tree_low_cst (retval, 1);
11984 /* We could expand this as
11985 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
11986 or to
11987 memcpy (str1, str2_with_nul_at_cstm1, cst);
11988 but in the former case that might increase code size
11989 and in the latter case grow .rodata section too much.
11990 So punt for now. */
11991 if (origlen >= destlen)
11992 return NULL_TREE;
11993
11994 /* Convert snprintf (str1, cst, "%s", str2) into
11995 strcpy (str1, str2) if strlen (str2) < cst. */
11996 if (!fn)
11997 return NULL_TREE;
11998
11999 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12000
12001 if (ignored)
12002 retval = NULL_TREE;
12003 }
12004
12005 if (call && retval)
12006 {
12007 tree fn = built_in_decls[BUILT_IN_SNPRINTF];
12008 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12009 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12010 }
12011 else
12012 return call;
12013 }
12014
12015 /* Expand a call EXP to __builtin_object_size. */
12016
12017 rtx
12018 expand_builtin_object_size (tree exp)
12019 {
12020 tree ost;
12021 int object_size_type;
12022 tree fndecl = get_callee_fndecl (exp);
12023
12024 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12025 {
12026 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12027 exp, fndecl);
12028 expand_builtin_trap ();
12029 return const0_rtx;
12030 }
12031
12032 ost = CALL_EXPR_ARG (exp, 1);
12033 STRIP_NOPS (ost);
12034
12035 if (TREE_CODE (ost) != INTEGER_CST
12036 || tree_int_cst_sgn (ost) < 0
12037 || compare_tree_int (ost, 3) > 0)
12038 {
12039 error ("%Klast argument of %D is not integer constant between 0 and 3",
12040 exp, fndecl);
12041 expand_builtin_trap ();
12042 return const0_rtx;
12043 }
12044
12045 object_size_type = tree_low_cst (ost, 0);
12046
12047 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12048 }
12049
12050 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12051 FCODE is the BUILT_IN_* to use.
12052 Return NULL_RTX if we failed; the caller should emit a normal call,
12053 otherwise try to get the result in TARGET, if convenient (and in
12054 mode MODE if that's convenient). */
12055
12056 static rtx
12057 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12058 enum built_in_function fcode)
12059 {
12060 tree dest, src, len, size;
12061
12062 if (!validate_arglist (exp,
12063 POINTER_TYPE,
12064 fcode == BUILT_IN_MEMSET_CHK
12065 ? INTEGER_TYPE : POINTER_TYPE,
12066 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12067 return NULL_RTX;
12068
12069 dest = CALL_EXPR_ARG (exp, 0);
12070 src = CALL_EXPR_ARG (exp, 1);
12071 len = CALL_EXPR_ARG (exp, 2);
12072 size = CALL_EXPR_ARG (exp, 3);
12073
12074 if (! host_integerp (size, 1))
12075 return NULL_RTX;
12076
12077 if (host_integerp (len, 1) || integer_all_onesp (size))
12078 {
12079 tree fn;
12080
12081 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12082 {
12083 warning_at (tree_nonartificial_location (exp),
12084 0, "%Kcall to %D will always overflow destination buffer",
12085 exp, get_callee_fndecl (exp));
12086 return NULL_RTX;
12087 }
12088
12089 fn = NULL_TREE;
12090 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12091 mem{cpy,pcpy,move,set} is available. */
12092 switch (fcode)
12093 {
12094 case BUILT_IN_MEMCPY_CHK:
12095 fn = built_in_decls[BUILT_IN_MEMCPY];
12096 break;
12097 case BUILT_IN_MEMPCPY_CHK:
12098 fn = built_in_decls[BUILT_IN_MEMPCPY];
12099 break;
12100 case BUILT_IN_MEMMOVE_CHK:
12101 fn = built_in_decls[BUILT_IN_MEMMOVE];
12102 break;
12103 case BUILT_IN_MEMSET_CHK:
12104 fn = built_in_decls[BUILT_IN_MEMSET];
12105 break;
12106 default:
12107 break;
12108 }
12109
12110 if (! fn)
12111 return NULL_RTX;
12112
12113 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12114 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12115 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12116 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12117 }
12118 else if (fcode == BUILT_IN_MEMSET_CHK)
12119 return NULL_RTX;
12120 else
12121 {
12122 unsigned int dest_align
12123 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12124
12125 /* If DEST is not a pointer type, call the normal function. */
12126 if (dest_align == 0)
12127 return NULL_RTX;
12128
12129 /* If SRC and DEST are the same (and not volatile), do nothing. */
12130 if (operand_equal_p (src, dest, 0))
12131 {
12132 tree expr;
12133
12134 if (fcode != BUILT_IN_MEMPCPY_CHK)
12135 {
12136 /* Evaluate and ignore LEN in case it has side-effects. */
12137 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12138 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12139 }
12140
12141 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12142 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12143 }
12144
12145 /* __memmove_chk special case. */
12146 if (fcode == BUILT_IN_MEMMOVE_CHK)
12147 {
12148 unsigned int src_align
12149 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12150
12151 if (src_align == 0)
12152 return NULL_RTX;
12153
12154 /* If src is categorized for a readonly section we can use
12155 normal __memcpy_chk. */
12156 if (readonly_data_expr (src))
12157 {
12158 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12159 if (!fn)
12160 return NULL_RTX;
12161 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12162 dest, src, len, size);
12163 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12164 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12165 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12166 }
12167 }
12168 return NULL_RTX;
12169 }
12170 }
12171
12172 /* Emit warning if a buffer overflow is detected at compile time. */
12173
12174 static void
12175 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12176 {
12177 int is_strlen = 0;
12178 tree len, size;
12179 location_t loc = tree_nonartificial_location (exp);
12180
12181 switch (fcode)
12182 {
12183 case BUILT_IN_STRCPY_CHK:
12184 case BUILT_IN_STPCPY_CHK:
12185 /* For __strcat_chk the warning will be emitted only if overflowing
12186 by at least strlen (dest) + 1 bytes. */
12187 case BUILT_IN_STRCAT_CHK:
12188 len = CALL_EXPR_ARG (exp, 1);
12189 size = CALL_EXPR_ARG (exp, 2);
12190 is_strlen = 1;
12191 break;
12192 case BUILT_IN_STRNCAT_CHK:
12193 case BUILT_IN_STRNCPY_CHK:
12194 len = CALL_EXPR_ARG (exp, 2);
12195 size = CALL_EXPR_ARG (exp, 3);
12196 break;
12197 case BUILT_IN_SNPRINTF_CHK:
12198 case BUILT_IN_VSNPRINTF_CHK:
12199 len = CALL_EXPR_ARG (exp, 1);
12200 size = CALL_EXPR_ARG (exp, 3);
12201 break;
12202 default:
12203 gcc_unreachable ();
12204 }
12205
12206 if (!len || !size)
12207 return;
12208
12209 if (! host_integerp (size, 1) || integer_all_onesp (size))
12210 return;
12211
12212 if (is_strlen)
12213 {
12214 len = c_strlen (len, 1);
12215 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12216 return;
12217 }
12218 else if (fcode == BUILT_IN_STRNCAT_CHK)
12219 {
12220 tree src = CALL_EXPR_ARG (exp, 1);
12221 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12222 return;
12223 src = c_strlen (src, 1);
12224 if (! src || ! host_integerp (src, 1))
12225 {
12226 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12227 exp, get_callee_fndecl (exp));
12228 return;
12229 }
12230 else if (tree_int_cst_lt (src, size))
12231 return;
12232 }
12233 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12234 return;
12235
12236 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12237 exp, get_callee_fndecl (exp));
12238 }
12239
12240 /* Emit warning if a buffer overflow is detected at compile time
12241 in __sprintf_chk/__vsprintf_chk calls. */
12242
12243 static void
12244 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12245 {
12246 tree size, len, fmt;
12247 const char *fmt_str;
12248 int nargs = call_expr_nargs (exp);
12249
12250 /* Verify the required arguments in the original call. */
12251
12252 if (nargs < 4)
12253 return;
12254 size = CALL_EXPR_ARG (exp, 2);
12255 fmt = CALL_EXPR_ARG (exp, 3);
12256
12257 if (! host_integerp (size, 1) || integer_all_onesp (size))
12258 return;
12259
12260 /* Check whether the format is a literal string constant. */
12261 fmt_str = c_getstr (fmt);
12262 if (fmt_str == NULL)
12263 return;
12264
12265 if (!init_target_chars ())
12266 return;
12267
12268 /* If the format doesn't contain % args or %%, we know its size. */
12269 if (strchr (fmt_str, target_percent) == 0)
12270 len = build_int_cstu (size_type_node, strlen (fmt_str));
12271 /* If the format is "%s" and first ... argument is a string literal,
12272 we know it too. */
12273 else if (fcode == BUILT_IN_SPRINTF_CHK
12274 && strcmp (fmt_str, target_percent_s) == 0)
12275 {
12276 tree arg;
12277
12278 if (nargs < 5)
12279 return;
12280 arg = CALL_EXPR_ARG (exp, 4);
12281 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12282 return;
12283
12284 len = c_strlen (arg, 1);
12285 if (!len || ! host_integerp (len, 1))
12286 return;
12287 }
12288 else
12289 return;
12290
12291 if (! tree_int_cst_lt (len, size))
12292 warning_at (tree_nonartificial_location (exp),
12293 0, "%Kcall to %D will always overflow destination buffer",
12294 exp, get_callee_fndecl (exp));
12295 }
12296
12297 /* Emit warning if a free is called with address of a variable. */
12298
12299 static void
12300 maybe_emit_free_warning (tree exp)
12301 {
12302 tree arg = CALL_EXPR_ARG (exp, 0);
12303
12304 STRIP_NOPS (arg);
12305 if (TREE_CODE (arg) != ADDR_EXPR)
12306 return;
12307
12308 arg = get_base_address (TREE_OPERAND (arg, 0));
12309 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12310 return;
12311
12312 if (SSA_VAR_P (arg))
12313 warning_at (tree_nonartificial_location (exp),
12314 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12315 else
12316 warning_at (tree_nonartificial_location (exp),
12317 0, "%Kattempt to free a non-heap object", exp);
12318 }
12319
12320 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12321 if possible. */
12322
12323 tree
12324 fold_builtin_object_size (tree ptr, tree ost)
12325 {
12326 unsigned HOST_WIDE_INT bytes;
12327 int object_size_type;
12328
12329 if (!validate_arg (ptr, POINTER_TYPE)
12330 || !validate_arg (ost, INTEGER_TYPE))
12331 return NULL_TREE;
12332
12333 STRIP_NOPS (ost);
12334
12335 if (TREE_CODE (ost) != INTEGER_CST
12336 || tree_int_cst_sgn (ost) < 0
12337 || compare_tree_int (ost, 3) > 0)
12338 return NULL_TREE;
12339
12340 object_size_type = tree_low_cst (ost, 0);
12341
12342 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12343 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12344 and (size_t) 0 for types 2 and 3. */
12345 if (TREE_SIDE_EFFECTS (ptr))
12346 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12347
12348 if (TREE_CODE (ptr) == ADDR_EXPR)
12349 {
12350 bytes = compute_builtin_object_size (ptr, object_size_type);
12351 if (double_int_fits_to_tree_p (size_type_node,
12352 uhwi_to_double_int (bytes)))
12353 return build_int_cstu (size_type_node, bytes);
12354 }
12355 else if (TREE_CODE (ptr) == SSA_NAME)
12356 {
12357 /* If object size is not known yet, delay folding until
12358 later. Maybe subsequent passes will help determining
12359 it. */
12360 bytes = compute_builtin_object_size (ptr, object_size_type);
12361 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12362 && double_int_fits_to_tree_p (size_type_node,
12363 uhwi_to_double_int (bytes)))
12364 return build_int_cstu (size_type_node, bytes);
12365 }
12366
12367 return NULL_TREE;
12368 }
12369
12370 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12371 DEST, SRC, LEN, and SIZE are the arguments to the call.
12372 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12373 code of the builtin. If MAXLEN is not NULL, it is maximum length
12374 passed as third argument. */
12375
12376 tree
12377 fold_builtin_memory_chk (location_t loc, tree fndecl,
12378 tree dest, tree src, tree len, tree size,
12379 tree maxlen, bool ignore,
12380 enum built_in_function fcode)
12381 {
12382 tree fn;
12383
12384 if (!validate_arg (dest, POINTER_TYPE)
12385 || !validate_arg (src,
12386 (fcode == BUILT_IN_MEMSET_CHK
12387 ? INTEGER_TYPE : POINTER_TYPE))
12388 || !validate_arg (len, INTEGER_TYPE)
12389 || !validate_arg (size, INTEGER_TYPE))
12390 return NULL_TREE;
12391
12392 /* If SRC and DEST are the same (and not volatile), return DEST
12393 (resp. DEST+LEN for __mempcpy_chk). */
12394 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12395 {
12396 if (fcode != BUILT_IN_MEMPCPY_CHK)
12397 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12398 dest, len);
12399 else
12400 {
12401 tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12402 dest, len);
12403 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12404 }
12405 }
12406
12407 if (! host_integerp (size, 1))
12408 return NULL_TREE;
12409
12410 if (! integer_all_onesp (size))
12411 {
12412 if (! host_integerp (len, 1))
12413 {
12414 /* If LEN is not constant, try MAXLEN too.
12415 For MAXLEN only allow optimizing into non-_ocs function
12416 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12417 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12418 {
12419 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12420 {
12421 /* (void) __mempcpy_chk () can be optimized into
12422 (void) __memcpy_chk (). */
12423 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12424 if (!fn)
12425 return NULL_TREE;
12426
12427 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12428 }
12429 return NULL_TREE;
12430 }
12431 }
12432 else
12433 maxlen = len;
12434
12435 if (tree_int_cst_lt (size, maxlen))
12436 return NULL_TREE;
12437 }
12438
12439 fn = NULL_TREE;
12440 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12441 mem{cpy,pcpy,move,set} is available. */
12442 switch (fcode)
12443 {
12444 case BUILT_IN_MEMCPY_CHK:
12445 fn = built_in_decls[BUILT_IN_MEMCPY];
12446 break;
12447 case BUILT_IN_MEMPCPY_CHK:
12448 fn = built_in_decls[BUILT_IN_MEMPCPY];
12449 break;
12450 case BUILT_IN_MEMMOVE_CHK:
12451 fn = built_in_decls[BUILT_IN_MEMMOVE];
12452 break;
12453 case BUILT_IN_MEMSET_CHK:
12454 fn = built_in_decls[BUILT_IN_MEMSET];
12455 break;
12456 default:
12457 break;
12458 }
12459
12460 if (!fn)
12461 return NULL_TREE;
12462
12463 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12464 }
12465
12466 /* Fold a call to the __st[rp]cpy_chk builtin.
12467 DEST, SRC, and SIZE are the arguments to the call.
12468 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12469 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12470 strings passed as second argument. */
12471
12472 tree
12473 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12474 tree src, tree size,
12475 tree maxlen, bool ignore,
12476 enum built_in_function fcode)
12477 {
12478 tree len, fn;
12479
12480 if (!validate_arg (dest, POINTER_TYPE)
12481 || !validate_arg (src, POINTER_TYPE)
12482 || !validate_arg (size, INTEGER_TYPE))
12483 return NULL_TREE;
12484
12485 /* If SRC and DEST are the same (and not volatile), return DEST. */
12486 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12487 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12488
12489 if (! host_integerp (size, 1))
12490 return NULL_TREE;
12491
12492 if (! integer_all_onesp (size))
12493 {
12494 len = c_strlen (src, 1);
12495 if (! len || ! host_integerp (len, 1))
12496 {
12497 /* If LEN is not constant, try MAXLEN too.
12498 For MAXLEN only allow optimizing into non-_ocs function
12499 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12500 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12501 {
12502 if (fcode == BUILT_IN_STPCPY_CHK)
12503 {
12504 if (! ignore)
12505 return NULL_TREE;
12506
12507 /* If return value of __stpcpy_chk is ignored,
12508 optimize into __strcpy_chk. */
12509 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12510 if (!fn)
12511 return NULL_TREE;
12512
12513 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12514 }
12515
12516 if (! len || TREE_SIDE_EFFECTS (len))
12517 return NULL_TREE;
12518
12519 /* If c_strlen returned something, but not a constant,
12520 transform __strcpy_chk into __memcpy_chk. */
12521 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12522 if (!fn)
12523 return NULL_TREE;
12524
12525 len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12526 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12527 build_call_expr_loc (loc, fn, 4,
12528 dest, src, len, size));
12529 }
12530 }
12531 else
12532 maxlen = len;
12533
12534 if (! tree_int_cst_lt (maxlen, size))
12535 return NULL_TREE;
12536 }
12537
12538 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12539 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12540 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12541 if (!fn)
12542 return NULL_TREE;
12543
12544 return build_call_expr_loc (loc, fn, 2, dest, src);
12545 }
12546
12547 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12548 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12549 length passed as third argument. */
12550
12551 tree
12552 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12553 tree len, tree size, tree maxlen)
12554 {
12555 tree fn;
12556
12557 if (!validate_arg (dest, POINTER_TYPE)
12558 || !validate_arg (src, POINTER_TYPE)
12559 || !validate_arg (len, INTEGER_TYPE)
12560 || !validate_arg (size, INTEGER_TYPE))
12561 return NULL_TREE;
12562
12563 if (! host_integerp (size, 1))
12564 return NULL_TREE;
12565
12566 if (! integer_all_onesp (size))
12567 {
12568 if (! host_integerp (len, 1))
12569 {
12570 /* If LEN is not constant, try MAXLEN too.
12571 For MAXLEN only allow optimizing into non-_ocs function
12572 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12573 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12574 return NULL_TREE;
12575 }
12576 else
12577 maxlen = len;
12578
12579 if (tree_int_cst_lt (size, maxlen))
12580 return NULL_TREE;
12581 }
12582
12583 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12584 fn = built_in_decls[BUILT_IN_STRNCPY];
12585 if (!fn)
12586 return NULL_TREE;
12587
12588 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12589 }
12590
12591 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12592 are the arguments to the call. */
12593
12594 static tree
12595 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12596 tree src, tree size)
12597 {
12598 tree fn;
12599 const char *p;
12600
12601 if (!validate_arg (dest, POINTER_TYPE)
12602 || !validate_arg (src, POINTER_TYPE)
12603 || !validate_arg (size, INTEGER_TYPE))
12604 return NULL_TREE;
12605
12606 p = c_getstr (src);
12607 /* If the SRC parameter is "", return DEST. */
12608 if (p && *p == '\0')
12609 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12610
12611 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12612 return NULL_TREE;
12613
12614 /* If __builtin_strcat_chk is used, assume strcat is available. */
12615 fn = built_in_decls[BUILT_IN_STRCAT];
12616 if (!fn)
12617 return NULL_TREE;
12618
12619 return build_call_expr_loc (loc, fn, 2, dest, src);
12620 }
12621
12622 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12623 LEN, and SIZE. */
12624
12625 static tree
12626 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12627 tree dest, tree src, tree len, tree size)
12628 {
12629 tree fn;
12630 const char *p;
12631
12632 if (!validate_arg (dest, POINTER_TYPE)
12633 || !validate_arg (src, POINTER_TYPE)
12634 || !validate_arg (size, INTEGER_TYPE)
12635 || !validate_arg (size, INTEGER_TYPE))
12636 return NULL_TREE;
12637
12638 p = c_getstr (src);
12639 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12640 if (p && *p == '\0')
12641 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12642 else if (integer_zerop (len))
12643 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12644
12645 if (! host_integerp (size, 1))
12646 return NULL_TREE;
12647
12648 if (! integer_all_onesp (size))
12649 {
12650 tree src_len = c_strlen (src, 1);
12651 if (src_len
12652 && host_integerp (src_len, 1)
12653 && host_integerp (len, 1)
12654 && ! tree_int_cst_lt (len, src_len))
12655 {
12656 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12657 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12658 if (!fn)
12659 return NULL_TREE;
12660
12661 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12662 }
12663 return NULL_TREE;
12664 }
12665
12666 /* If __builtin_strncat_chk is used, assume strncat is available. */
12667 fn = built_in_decls[BUILT_IN_STRNCAT];
12668 if (!fn)
12669 return NULL_TREE;
12670
12671 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12672 }
12673
12674 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12675 Return NULL_TREE if a normal call should be emitted rather than
12676 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12677 or BUILT_IN_VSPRINTF_CHK. */
12678
12679 static tree
12680 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12681 enum built_in_function fcode)
12682 {
12683 tree dest, size, len, fn, fmt, flag;
12684 const char *fmt_str;
12685
12686 /* Verify the required arguments in the original call. */
12687 if (nargs < 4)
12688 return NULL_TREE;
12689 dest = args[0];
12690 if (!validate_arg (dest, POINTER_TYPE))
12691 return NULL_TREE;
12692 flag = args[1];
12693 if (!validate_arg (flag, INTEGER_TYPE))
12694 return NULL_TREE;
12695 size = args[2];
12696 if (!validate_arg (size, INTEGER_TYPE))
12697 return NULL_TREE;
12698 fmt = args[3];
12699 if (!validate_arg (fmt, POINTER_TYPE))
12700 return NULL_TREE;
12701
12702 if (! host_integerp (size, 1))
12703 return NULL_TREE;
12704
12705 len = NULL_TREE;
12706
12707 if (!init_target_chars ())
12708 return NULL_TREE;
12709
12710 /* Check whether the format is a literal string constant. */
12711 fmt_str = c_getstr (fmt);
12712 if (fmt_str != NULL)
12713 {
12714 /* If the format doesn't contain % args or %%, we know the size. */
12715 if (strchr (fmt_str, target_percent) == 0)
12716 {
12717 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12718 len = build_int_cstu (size_type_node, strlen (fmt_str));
12719 }
12720 /* If the format is "%s" and first ... argument is a string literal,
12721 we know the size too. */
12722 else if (fcode == BUILT_IN_SPRINTF_CHK
12723 && strcmp (fmt_str, target_percent_s) == 0)
12724 {
12725 tree arg;
12726
12727 if (nargs == 5)
12728 {
12729 arg = args[4];
12730 if (validate_arg (arg, POINTER_TYPE))
12731 {
12732 len = c_strlen (arg, 1);
12733 if (! len || ! host_integerp (len, 1))
12734 len = NULL_TREE;
12735 }
12736 }
12737 }
12738 }
12739
12740 if (! integer_all_onesp (size))
12741 {
12742 if (! len || ! tree_int_cst_lt (len, size))
12743 return NULL_TREE;
12744 }
12745
12746 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12747 or if format doesn't contain % chars or is "%s". */
12748 if (! integer_zerop (flag))
12749 {
12750 if (fmt_str == NULL)
12751 return NULL_TREE;
12752 if (strchr (fmt_str, target_percent) != NULL
12753 && strcmp (fmt_str, target_percent_s))
12754 return NULL_TREE;
12755 }
12756
12757 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12758 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12759 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12760 if (!fn)
12761 return NULL_TREE;
12762
12763 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12764 }
12765
12766 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12767 a normal call should be emitted rather than expanding the function
12768 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12769
12770 static tree
12771 fold_builtin_sprintf_chk (location_t loc, tree exp,
12772 enum built_in_function fcode)
12773 {
12774 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12775 CALL_EXPR_ARGP (exp), fcode);
12776 }
12777
12778 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12779 NULL_TREE if a normal call should be emitted rather than expanding
12780 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12781 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12782 passed as second argument. */
12783
12784 static tree
12785 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12786 tree maxlen, enum built_in_function fcode)
12787 {
12788 tree dest, size, len, fn, fmt, flag;
12789 const char *fmt_str;
12790
12791 /* Verify the required arguments in the original call. */
12792 if (nargs < 5)
12793 return NULL_TREE;
12794 dest = args[0];
12795 if (!validate_arg (dest, POINTER_TYPE))
12796 return NULL_TREE;
12797 len = args[1];
12798 if (!validate_arg (len, INTEGER_TYPE))
12799 return NULL_TREE;
12800 flag = args[2];
12801 if (!validate_arg (flag, INTEGER_TYPE))
12802 return NULL_TREE;
12803 size = args[3];
12804 if (!validate_arg (size, INTEGER_TYPE))
12805 return NULL_TREE;
12806 fmt = args[4];
12807 if (!validate_arg (fmt, POINTER_TYPE))
12808 return NULL_TREE;
12809
12810 if (! host_integerp (size, 1))
12811 return NULL_TREE;
12812
12813 if (! integer_all_onesp (size))
12814 {
12815 if (! host_integerp (len, 1))
12816 {
12817 /* If LEN is not constant, try MAXLEN too.
12818 For MAXLEN only allow optimizing into non-_ocs function
12819 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12820 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12821 return NULL_TREE;
12822 }
12823 else
12824 maxlen = len;
12825
12826 if (tree_int_cst_lt (size, maxlen))
12827 return NULL_TREE;
12828 }
12829
12830 if (!init_target_chars ())
12831 return NULL_TREE;
12832
12833 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12834 or if format doesn't contain % chars or is "%s". */
12835 if (! integer_zerop (flag))
12836 {
12837 fmt_str = c_getstr (fmt);
12838 if (fmt_str == NULL)
12839 return NULL_TREE;
12840 if (strchr (fmt_str, target_percent) != NULL
12841 && strcmp (fmt_str, target_percent_s))
12842 return NULL_TREE;
12843 }
12844
12845 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12846 available. */
12847 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12848 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12849 if (!fn)
12850 return NULL_TREE;
12851
12852 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12853 }
12854
12855 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12856 a normal call should be emitted rather than expanding the function
12857 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12858 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12859 passed as second argument. */
12860
12861 tree
12862 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12863 enum built_in_function fcode)
12864 {
12865 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12866 CALL_EXPR_ARGP (exp), maxlen, fcode);
12867 }
12868
12869 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12870 FMT and ARG are the arguments to the call; we don't fold cases with
12871 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12872
12873 Return NULL_TREE if no simplification was possible, otherwise return the
12874 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12875 code of the function to be simplified. */
12876
12877 static tree
12878 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12879 tree arg, bool ignore,
12880 enum built_in_function fcode)
12881 {
12882 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12883 const char *fmt_str = NULL;
12884
12885 /* If the return value is used, don't do the transformation. */
12886 if (! ignore)
12887 return NULL_TREE;
12888
12889 /* Verify the required arguments in the original call. */
12890 if (!validate_arg (fmt, POINTER_TYPE))
12891 return NULL_TREE;
12892
12893 /* Check whether the format is a literal string constant. */
12894 fmt_str = c_getstr (fmt);
12895 if (fmt_str == NULL)
12896 return NULL_TREE;
12897
12898 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12899 {
12900 /* If we're using an unlocked function, assume the other
12901 unlocked functions exist explicitly. */
12902 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12903 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12904 }
12905 else
12906 {
12907 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12908 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12909 }
12910
12911 if (!init_target_chars ())
12912 return NULL_TREE;
12913
12914 if (strcmp (fmt_str, target_percent_s) == 0
12915 || strchr (fmt_str, target_percent) == NULL)
12916 {
12917 const char *str;
12918
12919 if (strcmp (fmt_str, target_percent_s) == 0)
12920 {
12921 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12922 return NULL_TREE;
12923
12924 if (!arg || !validate_arg (arg, POINTER_TYPE))
12925 return NULL_TREE;
12926
12927 str = c_getstr (arg);
12928 if (str == NULL)
12929 return NULL_TREE;
12930 }
12931 else
12932 {
12933 /* The format specifier doesn't contain any '%' characters. */
12934 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12935 && arg)
12936 return NULL_TREE;
12937 str = fmt_str;
12938 }
12939
12940 /* If the string was "", printf does nothing. */
12941 if (str[0] == '\0')
12942 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12943
12944 /* If the string has length of 1, call putchar. */
12945 if (str[1] == '\0')
12946 {
12947 /* Given printf("c"), (where c is any one character,)
12948 convert "c"[0] to an int and pass that to the replacement
12949 function. */
12950 newarg = build_int_cst (NULL_TREE, str[0]);
12951 if (fn_putchar)
12952 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12953 }
12954 else
12955 {
12956 /* If the string was "string\n", call puts("string"). */
12957 size_t len = strlen (str);
12958 if ((unsigned char)str[len - 1] == target_newline
12959 && (size_t) (int) len == len
12960 && (int) len > 0)
12961 {
12962 char *newstr;
12963 tree offset_node, string_cst;
12964
12965 /* Create a NUL-terminated string that's one char shorter
12966 than the original, stripping off the trailing '\n'. */
12967 newarg = build_string_literal (len, str);
12968 string_cst = string_constant (newarg, &offset_node);
12969 gcc_checking_assert (string_cst
12970 && (TREE_STRING_LENGTH (string_cst)
12971 == (int) len)
12972 && integer_zerop (offset_node)
12973 && (unsigned char)
12974 TREE_STRING_POINTER (string_cst)[len - 1]
12975 == target_newline);
12976 /* build_string_literal creates a new STRING_CST,
12977 modify it in place to avoid double copying. */
12978 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
12979 newstr[len - 1] = '\0';
12980 if (fn_puts)
12981 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12982 }
12983 else
12984 /* We'd like to arrange to call fputs(string,stdout) here,
12985 but we need stdout and don't have a way to get it yet. */
12986 return NULL_TREE;
12987 }
12988 }
12989
12990 /* The other optimizations can be done only on the non-va_list variants. */
12991 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12992 return NULL_TREE;
12993
12994 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12995 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12996 {
12997 if (!arg || !validate_arg (arg, POINTER_TYPE))
12998 return NULL_TREE;
12999 if (fn_puts)
13000 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13001 }
13002
13003 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13004 else if (strcmp (fmt_str, target_percent_c) == 0)
13005 {
13006 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13007 return NULL_TREE;
13008 if (fn_putchar)
13009 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13010 }
13011
13012 if (!call)
13013 return NULL_TREE;
13014
13015 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13016 }
13017
13018 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13019 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13020 more than 3 arguments, and ARG may be null in the 2-argument case.
13021
13022 Return NULL_TREE if no simplification was possible, otherwise return the
13023 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13024 code of the function to be simplified. */
13025
13026 static tree
13027 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13028 tree fmt, tree arg, bool ignore,
13029 enum built_in_function fcode)
13030 {
13031 tree fn_fputc, fn_fputs, call = NULL_TREE;
13032 const char *fmt_str = NULL;
13033
13034 /* If the return value is used, don't do the transformation. */
13035 if (! ignore)
13036 return NULL_TREE;
13037
13038 /* Verify the required arguments in the original call. */
13039 if (!validate_arg (fp, POINTER_TYPE))
13040 return NULL_TREE;
13041 if (!validate_arg (fmt, POINTER_TYPE))
13042 return NULL_TREE;
13043
13044 /* Check whether the format is a literal string constant. */
13045 fmt_str = c_getstr (fmt);
13046 if (fmt_str == NULL)
13047 return NULL_TREE;
13048
13049 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13050 {
13051 /* If we're using an unlocked function, assume the other
13052 unlocked functions exist explicitly. */
13053 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
13054 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
13055 }
13056 else
13057 {
13058 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
13059 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
13060 }
13061
13062 if (!init_target_chars ())
13063 return NULL_TREE;
13064
13065 /* If the format doesn't contain % args or %%, use strcpy. */
13066 if (strchr (fmt_str, target_percent) == NULL)
13067 {
13068 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13069 && arg)
13070 return NULL_TREE;
13071
13072 /* If the format specifier was "", fprintf does nothing. */
13073 if (fmt_str[0] == '\0')
13074 {
13075 /* If FP has side-effects, just wait until gimplification is
13076 done. */
13077 if (TREE_SIDE_EFFECTS (fp))
13078 return NULL_TREE;
13079
13080 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13081 }
13082
13083 /* When "string" doesn't contain %, replace all cases of
13084 fprintf (fp, string) with fputs (string, fp). The fputs
13085 builtin will take care of special cases like length == 1. */
13086 if (fn_fputs)
13087 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13088 }
13089
13090 /* The other optimizations can be done only on the non-va_list variants. */
13091 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13092 return NULL_TREE;
13093
13094 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13095 else if (strcmp (fmt_str, target_percent_s) == 0)
13096 {
13097 if (!arg || !validate_arg (arg, POINTER_TYPE))
13098 return NULL_TREE;
13099 if (fn_fputs)
13100 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13101 }
13102
13103 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13104 else if (strcmp (fmt_str, target_percent_c) == 0)
13105 {
13106 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13107 return NULL_TREE;
13108 if (fn_fputc)
13109 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13110 }
13111
13112 if (!call)
13113 return NULL_TREE;
13114 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13115 }
13116
13117 /* Initialize format string characters in the target charset. */
13118
13119 static bool
13120 init_target_chars (void)
13121 {
13122 static bool init;
13123 if (!init)
13124 {
13125 target_newline = lang_hooks.to_target_charset ('\n');
13126 target_percent = lang_hooks.to_target_charset ('%');
13127 target_c = lang_hooks.to_target_charset ('c');
13128 target_s = lang_hooks.to_target_charset ('s');
13129 if (target_newline == 0 || target_percent == 0 || target_c == 0
13130 || target_s == 0)
13131 return false;
13132
13133 target_percent_c[0] = target_percent;
13134 target_percent_c[1] = target_c;
13135 target_percent_c[2] = '\0';
13136
13137 target_percent_s[0] = target_percent;
13138 target_percent_s[1] = target_s;
13139 target_percent_s[2] = '\0';
13140
13141 target_percent_s_newline[0] = target_percent;
13142 target_percent_s_newline[1] = target_s;
13143 target_percent_s_newline[2] = target_newline;
13144 target_percent_s_newline[3] = '\0';
13145
13146 init = true;
13147 }
13148 return true;
13149 }
13150
13151 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13152 and no overflow/underflow occurred. INEXACT is true if M was not
13153 exactly calculated. TYPE is the tree type for the result. This
13154 function assumes that you cleared the MPFR flags and then
13155 calculated M to see if anything subsequently set a flag prior to
13156 entering this function. Return NULL_TREE if any checks fail. */
13157
13158 static tree
13159 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13160 {
13161 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13162 overflow/underflow occurred. If -frounding-math, proceed iff the
13163 result of calling FUNC was exact. */
13164 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13165 && (!flag_rounding_math || !inexact))
13166 {
13167 REAL_VALUE_TYPE rr;
13168
13169 real_from_mpfr (&rr, m, type, GMP_RNDN);
13170 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13171 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13172 but the mpft_t is not, then we underflowed in the
13173 conversion. */
13174 if (real_isfinite (&rr)
13175 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13176 {
13177 REAL_VALUE_TYPE rmode;
13178
13179 real_convert (&rmode, TYPE_MODE (type), &rr);
13180 /* Proceed iff the specified mode can hold the value. */
13181 if (real_identical (&rmode, &rr))
13182 return build_real (type, rmode);
13183 }
13184 }
13185 return NULL_TREE;
13186 }
13187
13188 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13189 number and no overflow/underflow occurred. INEXACT is true if M
13190 was not exactly calculated. TYPE is the tree type for the result.
13191 This function assumes that you cleared the MPFR flags and then
13192 calculated M to see if anything subsequently set a flag prior to
13193 entering this function. Return NULL_TREE if any checks fail, if
13194 FORCE_CONVERT is true, then bypass the checks. */
13195
13196 static tree
13197 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13198 {
13199 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13200 overflow/underflow occurred. If -frounding-math, proceed iff the
13201 result of calling FUNC was exact. */
13202 if (force_convert
13203 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13204 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13205 && (!flag_rounding_math || !inexact)))
13206 {
13207 REAL_VALUE_TYPE re, im;
13208
13209 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13210 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13211 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13212 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13213 but the mpft_t is not, then we underflowed in the
13214 conversion. */
13215 if (force_convert
13216 || (real_isfinite (&re) && real_isfinite (&im)
13217 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13218 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13219 {
13220 REAL_VALUE_TYPE re_mode, im_mode;
13221
13222 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13223 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13224 /* Proceed iff the specified mode can hold the value. */
13225 if (force_convert
13226 || (real_identical (&re_mode, &re)
13227 && real_identical (&im_mode, &im)))
13228 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13229 build_real (TREE_TYPE (type), im_mode));
13230 }
13231 }
13232 return NULL_TREE;
13233 }
13234
13235 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13236 FUNC on it and return the resulting value as a tree with type TYPE.
13237 If MIN and/or MAX are not NULL, then the supplied ARG must be
13238 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13239 acceptable values, otherwise they are not. The mpfr precision is
13240 set to the precision of TYPE. We assume that function FUNC returns
13241 zero if the result could be calculated exactly within the requested
13242 precision. */
13243
13244 static tree
13245 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13246 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13247 bool inclusive)
13248 {
13249 tree result = NULL_TREE;
13250
13251 STRIP_NOPS (arg);
13252
13253 /* To proceed, MPFR must exactly represent the target floating point
13254 format, which only happens when the target base equals two. */
13255 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13256 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13257 {
13258 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13259
13260 if (real_isfinite (ra)
13261 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13262 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13263 {
13264 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13265 const int prec = fmt->p;
13266 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13267 int inexact;
13268 mpfr_t m;
13269
13270 mpfr_init2 (m, prec);
13271 mpfr_from_real (m, ra, GMP_RNDN);
13272 mpfr_clear_flags ();
13273 inexact = func (m, m, rnd);
13274 result = do_mpfr_ckconv (m, type, inexact);
13275 mpfr_clear (m);
13276 }
13277 }
13278
13279 return result;
13280 }
13281
13282 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13283 FUNC on it and return the resulting value as a tree with type TYPE.
13284 The mpfr precision is set to the precision of TYPE. We assume that
13285 function FUNC returns zero if the result could be calculated
13286 exactly within the requested precision. */
13287
13288 static tree
13289 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13290 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13291 {
13292 tree result = NULL_TREE;
13293
13294 STRIP_NOPS (arg1);
13295 STRIP_NOPS (arg2);
13296
13297 /* To proceed, MPFR must exactly represent the target floating point
13298 format, which only happens when the target base equals two. */
13299 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13300 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13301 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13302 {
13303 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13304 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13305
13306 if (real_isfinite (ra1) && real_isfinite (ra2))
13307 {
13308 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13309 const int prec = fmt->p;
13310 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13311 int inexact;
13312 mpfr_t m1, m2;
13313
13314 mpfr_inits2 (prec, m1, m2, NULL);
13315 mpfr_from_real (m1, ra1, GMP_RNDN);
13316 mpfr_from_real (m2, ra2, GMP_RNDN);
13317 mpfr_clear_flags ();
13318 inexact = func (m1, m1, m2, rnd);
13319 result = do_mpfr_ckconv (m1, type, inexact);
13320 mpfr_clears (m1, m2, NULL);
13321 }
13322 }
13323
13324 return result;
13325 }
13326
13327 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13328 FUNC on it and return the resulting value as a tree with type TYPE.
13329 The mpfr precision is set to the precision of TYPE. We assume that
13330 function FUNC returns zero if the result could be calculated
13331 exactly within the requested precision. */
13332
13333 static tree
13334 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13335 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13336 {
13337 tree result = NULL_TREE;
13338
13339 STRIP_NOPS (arg1);
13340 STRIP_NOPS (arg2);
13341 STRIP_NOPS (arg3);
13342
13343 /* To proceed, MPFR must exactly represent the target floating point
13344 format, which only happens when the target base equals two. */
13345 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13346 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13347 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13348 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13349 {
13350 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13351 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13352 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13353
13354 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13355 {
13356 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13357 const int prec = fmt->p;
13358 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13359 int inexact;
13360 mpfr_t m1, m2, m3;
13361
13362 mpfr_inits2 (prec, m1, m2, m3, NULL);
13363 mpfr_from_real (m1, ra1, GMP_RNDN);
13364 mpfr_from_real (m2, ra2, GMP_RNDN);
13365 mpfr_from_real (m3, ra3, GMP_RNDN);
13366 mpfr_clear_flags ();
13367 inexact = func (m1, m1, m2, m3, rnd);
13368 result = do_mpfr_ckconv (m1, type, inexact);
13369 mpfr_clears (m1, m2, m3, NULL);
13370 }
13371 }
13372
13373 return result;
13374 }
13375
13376 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13377 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13378 If ARG_SINP and ARG_COSP are NULL then the result is returned
13379 as a complex value.
13380 The type is taken from the type of ARG and is used for setting the
13381 precision of the calculation and results. */
13382
13383 static tree
13384 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13385 {
13386 tree const type = TREE_TYPE (arg);
13387 tree result = NULL_TREE;
13388
13389 STRIP_NOPS (arg);
13390
13391 /* To proceed, MPFR must exactly represent the target floating point
13392 format, which only happens when the target base equals two. */
13393 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13394 && TREE_CODE (arg) == REAL_CST
13395 && !TREE_OVERFLOW (arg))
13396 {
13397 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13398
13399 if (real_isfinite (ra))
13400 {
13401 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13402 const int prec = fmt->p;
13403 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13404 tree result_s, result_c;
13405 int inexact;
13406 mpfr_t m, ms, mc;
13407
13408 mpfr_inits2 (prec, m, ms, mc, NULL);
13409 mpfr_from_real (m, ra, GMP_RNDN);
13410 mpfr_clear_flags ();
13411 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13412 result_s = do_mpfr_ckconv (ms, type, inexact);
13413 result_c = do_mpfr_ckconv (mc, type, inexact);
13414 mpfr_clears (m, ms, mc, NULL);
13415 if (result_s && result_c)
13416 {
13417 /* If we are to return in a complex value do so. */
13418 if (!arg_sinp && !arg_cosp)
13419 return build_complex (build_complex_type (type),
13420 result_c, result_s);
13421
13422 /* Dereference the sin/cos pointer arguments. */
13423 arg_sinp = build_fold_indirect_ref (arg_sinp);
13424 arg_cosp = build_fold_indirect_ref (arg_cosp);
13425 /* Proceed if valid pointer type were passed in. */
13426 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13427 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13428 {
13429 /* Set the values. */
13430 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13431 result_s);
13432 TREE_SIDE_EFFECTS (result_s) = 1;
13433 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13434 result_c);
13435 TREE_SIDE_EFFECTS (result_c) = 1;
13436 /* Combine the assignments into a compound expr. */
13437 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13438 result_s, result_c));
13439 }
13440 }
13441 }
13442 }
13443 return result;
13444 }
13445
13446 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13447 two-argument mpfr order N Bessel function FUNC on them and return
13448 the resulting value as a tree with type TYPE. The mpfr precision
13449 is set to the precision of TYPE. We assume that function FUNC
13450 returns zero if the result could be calculated exactly within the
13451 requested precision. */
13452 static tree
13453 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13454 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13455 const REAL_VALUE_TYPE *min, bool inclusive)
13456 {
13457 tree result = NULL_TREE;
13458
13459 STRIP_NOPS (arg1);
13460 STRIP_NOPS (arg2);
13461
13462 /* To proceed, MPFR must exactly represent the target floating point
13463 format, which only happens when the target base equals two. */
13464 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13465 && host_integerp (arg1, 0)
13466 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13467 {
13468 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13469 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13470
13471 if (n == (long)n
13472 && real_isfinite (ra)
13473 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13474 {
13475 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13476 const int prec = fmt->p;
13477 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13478 int inexact;
13479 mpfr_t m;
13480
13481 mpfr_init2 (m, prec);
13482 mpfr_from_real (m, ra, GMP_RNDN);
13483 mpfr_clear_flags ();
13484 inexact = func (m, n, m, rnd);
13485 result = do_mpfr_ckconv (m, type, inexact);
13486 mpfr_clear (m);
13487 }
13488 }
13489
13490 return result;
13491 }
13492
13493 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13494 the pointer *(ARG_QUO) and return the result. The type is taken
13495 from the type of ARG0 and is used for setting the precision of the
13496 calculation and results. */
13497
13498 static tree
13499 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13500 {
13501 tree const type = TREE_TYPE (arg0);
13502 tree result = NULL_TREE;
13503
13504 STRIP_NOPS (arg0);
13505 STRIP_NOPS (arg1);
13506
13507 /* To proceed, MPFR must exactly represent the target floating point
13508 format, which only happens when the target base equals two. */
13509 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13510 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13511 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13512 {
13513 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13514 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13515
13516 if (real_isfinite (ra0) && real_isfinite (ra1))
13517 {
13518 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13519 const int prec = fmt->p;
13520 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13521 tree result_rem;
13522 long integer_quo;
13523 mpfr_t m0, m1;
13524
13525 mpfr_inits2 (prec, m0, m1, NULL);
13526 mpfr_from_real (m0, ra0, GMP_RNDN);
13527 mpfr_from_real (m1, ra1, GMP_RNDN);
13528 mpfr_clear_flags ();
13529 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13530 /* Remquo is independent of the rounding mode, so pass
13531 inexact=0 to do_mpfr_ckconv(). */
13532 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13533 mpfr_clears (m0, m1, NULL);
13534 if (result_rem)
13535 {
13536 /* MPFR calculates quo in the host's long so it may
13537 return more bits in quo than the target int can hold
13538 if sizeof(host long) > sizeof(target int). This can
13539 happen even for native compilers in LP64 mode. In
13540 these cases, modulo the quo value with the largest
13541 number that the target int can hold while leaving one
13542 bit for the sign. */
13543 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13544 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13545
13546 /* Dereference the quo pointer argument. */
13547 arg_quo = build_fold_indirect_ref (arg_quo);
13548 /* Proceed iff a valid pointer type was passed in. */
13549 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13550 {
13551 /* Set the value. */
13552 tree result_quo = fold_build2 (MODIFY_EXPR,
13553 TREE_TYPE (arg_quo), arg_quo,
13554 build_int_cst (NULL, integer_quo));
13555 TREE_SIDE_EFFECTS (result_quo) = 1;
13556 /* Combine the quo assignment with the rem. */
13557 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13558 result_quo, result_rem));
13559 }
13560 }
13561 }
13562 }
13563 return result;
13564 }
13565
13566 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13567 resulting value as a tree with type TYPE. The mpfr precision is
13568 set to the precision of TYPE. We assume that this mpfr function
13569 returns zero if the result could be calculated exactly within the
13570 requested precision. In addition, the integer pointer represented
13571 by ARG_SG will be dereferenced and set to the appropriate signgam
13572 (-1,1) value. */
13573
13574 static tree
13575 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13576 {
13577 tree result = NULL_TREE;
13578
13579 STRIP_NOPS (arg);
13580
13581 /* To proceed, MPFR must exactly represent the target floating point
13582 format, which only happens when the target base equals two. Also
13583 verify ARG is a constant and that ARG_SG is an int pointer. */
13584 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13585 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13586 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13587 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13588 {
13589 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13590
13591 /* In addition to NaN and Inf, the argument cannot be zero or a
13592 negative integer. */
13593 if (real_isfinite (ra)
13594 && ra->cl != rvc_zero
13595 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13596 {
13597 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13598 const int prec = fmt->p;
13599 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13600 int inexact, sg;
13601 mpfr_t m;
13602 tree result_lg;
13603
13604 mpfr_init2 (m, prec);
13605 mpfr_from_real (m, ra, GMP_RNDN);
13606 mpfr_clear_flags ();
13607 inexact = mpfr_lgamma (m, &sg, m, rnd);
13608 result_lg = do_mpfr_ckconv (m, type, inexact);
13609 mpfr_clear (m);
13610 if (result_lg)
13611 {
13612 tree result_sg;
13613
13614 /* Dereference the arg_sg pointer argument. */
13615 arg_sg = build_fold_indirect_ref (arg_sg);
13616 /* Assign the signgam value into *arg_sg. */
13617 result_sg = fold_build2 (MODIFY_EXPR,
13618 TREE_TYPE (arg_sg), arg_sg,
13619 build_int_cst (NULL, sg));
13620 TREE_SIDE_EFFECTS (result_sg) = 1;
13621 /* Combine the signgam assignment with the lgamma result. */
13622 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13623 result_sg, result_lg));
13624 }
13625 }
13626 }
13627
13628 return result;
13629 }
13630
13631 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13632 function FUNC on it and return the resulting value as a tree with
13633 type TYPE. The mpfr precision is set to the precision of TYPE. We
13634 assume that function FUNC returns zero if the result could be
13635 calculated exactly within the requested precision. */
13636
13637 static tree
13638 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13639 {
13640 tree result = NULL_TREE;
13641
13642 STRIP_NOPS (arg);
13643
13644 /* To proceed, MPFR must exactly represent the target floating point
13645 format, which only happens when the target base equals two. */
13646 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13647 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13648 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13649 {
13650 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13651 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13652
13653 if (real_isfinite (re) && real_isfinite (im))
13654 {
13655 const struct real_format *const fmt =
13656 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13657 const int prec = fmt->p;
13658 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13659 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13660 int inexact;
13661 mpc_t m;
13662
13663 mpc_init2 (m, prec);
13664 mpfr_from_real (mpc_realref(m), re, rnd);
13665 mpfr_from_real (mpc_imagref(m), im, rnd);
13666 mpfr_clear_flags ();
13667 inexact = func (m, m, crnd);
13668 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13669 mpc_clear (m);
13670 }
13671 }
13672
13673 return result;
13674 }
13675
13676 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13677 mpc function FUNC on it and return the resulting value as a tree
13678 with type TYPE. The mpfr precision is set to the precision of
13679 TYPE. We assume that function FUNC returns zero if the result
13680 could be calculated exactly within the requested precision. If
13681 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13682 in the arguments and/or results. */
13683
13684 tree
13685 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13686 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13687 {
13688 tree result = NULL_TREE;
13689
13690 STRIP_NOPS (arg0);
13691 STRIP_NOPS (arg1);
13692
13693 /* To proceed, MPFR must exactly represent the target floating point
13694 format, which only happens when the target base equals two. */
13695 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13696 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13697 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13698 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13699 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13700 {
13701 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13702 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13703 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13704 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13705
13706 if (do_nonfinite
13707 || (real_isfinite (re0) && real_isfinite (im0)
13708 && real_isfinite (re1) && real_isfinite (im1)))
13709 {
13710 const struct real_format *const fmt =
13711 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13712 const int prec = fmt->p;
13713 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13714 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13715 int inexact;
13716 mpc_t m0, m1;
13717
13718 mpc_init2 (m0, prec);
13719 mpc_init2 (m1, prec);
13720 mpfr_from_real (mpc_realref(m0), re0, rnd);
13721 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13722 mpfr_from_real (mpc_realref(m1), re1, rnd);
13723 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13724 mpfr_clear_flags ();
13725 inexact = func (m0, m0, m1, crnd);
13726 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13727 mpc_clear (m0);
13728 mpc_clear (m1);
13729 }
13730 }
13731
13732 return result;
13733 }
13734
13735 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13736 a normal call should be emitted rather than expanding the function
13737 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13738
13739 static tree
13740 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13741 {
13742 int nargs = gimple_call_num_args (stmt);
13743
13744 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13745 (nargs > 0
13746 ? gimple_call_arg_ptr (stmt, 0)
13747 : &error_mark_node), fcode);
13748 }
13749
13750 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13751 a normal call should be emitted rather than expanding the function
13752 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13753 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13754 passed as second argument. */
13755
13756 tree
13757 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13758 enum built_in_function fcode)
13759 {
13760 int nargs = gimple_call_num_args (stmt);
13761
13762 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13763 (nargs > 0
13764 ? gimple_call_arg_ptr (stmt, 0)
13765 : &error_mark_node), maxlen, fcode);
13766 }
13767
13768 /* Builtins with folding operations that operate on "..." arguments
13769 need special handling; we need to store the arguments in a convenient
13770 data structure before attempting any folding. Fortunately there are
13771 only a few builtins that fall into this category. FNDECL is the
13772 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13773 result of the function call is ignored. */
13774
13775 static tree
13776 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13777 bool ignore ATTRIBUTE_UNUSED)
13778 {
13779 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13780 tree ret = NULL_TREE;
13781
13782 switch (fcode)
13783 {
13784 case BUILT_IN_SPRINTF_CHK:
13785 case BUILT_IN_VSPRINTF_CHK:
13786 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13787 break;
13788
13789 case BUILT_IN_SNPRINTF_CHK:
13790 case BUILT_IN_VSNPRINTF_CHK:
13791 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13792
13793 default:
13794 break;
13795 }
13796 if (ret)
13797 {
13798 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13799 TREE_NO_WARNING (ret) = 1;
13800 return ret;
13801 }
13802 return NULL_TREE;
13803 }
13804
13805 /* A wrapper function for builtin folding that prevents warnings for
13806 "statement without effect" and the like, caused by removing the
13807 call node earlier than the warning is generated. */
13808
13809 tree
13810 fold_call_stmt (gimple stmt, bool ignore)
13811 {
13812 tree ret = NULL_TREE;
13813 tree fndecl = gimple_call_fndecl (stmt);
13814 location_t loc = gimple_location (stmt);
13815 if (fndecl
13816 && TREE_CODE (fndecl) == FUNCTION_DECL
13817 && DECL_BUILT_IN (fndecl)
13818 && !gimple_call_va_arg_pack_p (stmt))
13819 {
13820 int nargs = gimple_call_num_args (stmt);
13821 tree *args = (nargs > 0
13822 ? gimple_call_arg_ptr (stmt, 0)
13823 : &error_mark_node);
13824
13825 if (avoid_folding_inline_builtin (fndecl))
13826 return NULL_TREE;
13827 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13828 {
13829 return targetm.fold_builtin (fndecl, nargs, args, ignore);
13830 }
13831 else
13832 {
13833 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13834 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13835 if (!ret)
13836 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13837 if (ret)
13838 {
13839 /* Propagate location information from original call to
13840 expansion of builtin. Otherwise things like
13841 maybe_emit_chk_warning, that operate on the expansion
13842 of a builtin, will use the wrong location information. */
13843 if (gimple_has_location (stmt))
13844 {
13845 tree realret = ret;
13846 if (TREE_CODE (ret) == NOP_EXPR)
13847 realret = TREE_OPERAND (ret, 0);
13848 if (CAN_HAVE_LOCATION_P (realret)
13849 && !EXPR_HAS_LOCATION (realret))
13850 SET_EXPR_LOCATION (realret, loc);
13851 return realret;
13852 }
13853 return ret;
13854 }
13855 }
13856 }
13857 return NULL_TREE;
13858 }
13859
13860 /* Look up the function in built_in_decls that corresponds to DECL
13861 and set ASMSPEC as its user assembler name. DECL must be a
13862 function decl that declares a builtin. */
13863
13864 void
13865 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13866 {
13867 tree builtin;
13868 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13869 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13870 && asmspec != 0);
13871
13872 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13873 set_user_assembler_name (builtin, asmspec);
13874 switch (DECL_FUNCTION_CODE (decl))
13875 {
13876 case BUILT_IN_MEMCPY:
13877 init_block_move_fn (asmspec);
13878 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13879 break;
13880 case BUILT_IN_MEMSET:
13881 init_block_clear_fn (asmspec);
13882 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13883 break;
13884 case BUILT_IN_MEMMOVE:
13885 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13886 break;
13887 case BUILT_IN_MEMCMP:
13888 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13889 break;
13890 case BUILT_IN_ABORT:
13891 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13892 break;
13893 case BUILT_IN_FFS:
13894 if (INT_TYPE_SIZE < BITS_PER_WORD)
13895 {
13896 set_user_assembler_libfunc ("ffs", asmspec);
13897 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13898 MODE_INT, 0), "ffs");
13899 }
13900 break;
13901 default:
13902 break;
13903 }
13904 }
13905
13906 /* Return true if DECL is a builtin that expands to a constant or similarly
13907 simple code. */
13908 bool
13909 is_simple_builtin (tree decl)
13910 {
13911 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13912 switch (DECL_FUNCTION_CODE (decl))
13913 {
13914 /* Builtins that expand to constants. */
13915 case BUILT_IN_CONSTANT_P:
13916 case BUILT_IN_EXPECT:
13917 case BUILT_IN_OBJECT_SIZE:
13918 case BUILT_IN_UNREACHABLE:
13919 /* Simple register moves or loads from stack. */
13920 case BUILT_IN_RETURN_ADDRESS:
13921 case BUILT_IN_EXTRACT_RETURN_ADDR:
13922 case BUILT_IN_FROB_RETURN_ADDR:
13923 case BUILT_IN_RETURN:
13924 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13925 case BUILT_IN_FRAME_ADDRESS:
13926 case BUILT_IN_VA_END:
13927 case BUILT_IN_STACK_SAVE:
13928 case BUILT_IN_STACK_RESTORE:
13929 /* Exception state returns or moves registers around. */
13930 case BUILT_IN_EH_FILTER:
13931 case BUILT_IN_EH_POINTER:
13932 case BUILT_IN_EH_COPY_VALUES:
13933 return true;
13934
13935 default:
13936 return false;
13937 }
13938
13939 return false;
13940 }
13941
13942 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13943 most probably expanded inline into reasonably simple code. This is a
13944 superset of is_simple_builtin. */
13945 bool
13946 is_inexpensive_builtin (tree decl)
13947 {
13948 if (!decl)
13949 return false;
13950 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13951 return true;
13952 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13953 switch (DECL_FUNCTION_CODE (decl))
13954 {
13955 case BUILT_IN_ABS:
13956 case BUILT_IN_ALLOCA:
13957 case BUILT_IN_BSWAP32:
13958 case BUILT_IN_BSWAP64:
13959 case BUILT_IN_CLZ:
13960 case BUILT_IN_CLZIMAX:
13961 case BUILT_IN_CLZL:
13962 case BUILT_IN_CLZLL:
13963 case BUILT_IN_CTZ:
13964 case BUILT_IN_CTZIMAX:
13965 case BUILT_IN_CTZL:
13966 case BUILT_IN_CTZLL:
13967 case BUILT_IN_FFS:
13968 case BUILT_IN_FFSIMAX:
13969 case BUILT_IN_FFSL:
13970 case BUILT_IN_FFSLL:
13971 case BUILT_IN_IMAXABS:
13972 case BUILT_IN_FINITE:
13973 case BUILT_IN_FINITEF:
13974 case BUILT_IN_FINITEL:
13975 case BUILT_IN_FINITED32:
13976 case BUILT_IN_FINITED64:
13977 case BUILT_IN_FINITED128:
13978 case BUILT_IN_FPCLASSIFY:
13979 case BUILT_IN_ISFINITE:
13980 case BUILT_IN_ISINF_SIGN:
13981 case BUILT_IN_ISINF:
13982 case BUILT_IN_ISINFF:
13983 case BUILT_IN_ISINFL:
13984 case BUILT_IN_ISINFD32:
13985 case BUILT_IN_ISINFD64:
13986 case BUILT_IN_ISINFD128:
13987 case BUILT_IN_ISNAN:
13988 case BUILT_IN_ISNANF:
13989 case BUILT_IN_ISNANL:
13990 case BUILT_IN_ISNAND32:
13991 case BUILT_IN_ISNAND64:
13992 case BUILT_IN_ISNAND128:
13993 case BUILT_IN_ISNORMAL:
13994 case BUILT_IN_ISGREATER:
13995 case BUILT_IN_ISGREATEREQUAL:
13996 case BUILT_IN_ISLESS:
13997 case BUILT_IN_ISLESSEQUAL:
13998 case BUILT_IN_ISLESSGREATER:
13999 case BUILT_IN_ISUNORDERED:
14000 case BUILT_IN_VA_ARG_PACK:
14001 case BUILT_IN_VA_ARG_PACK_LEN:
14002 case BUILT_IN_VA_COPY:
14003 case BUILT_IN_TRAP:
14004 case BUILT_IN_SAVEREGS:
14005 case BUILT_IN_POPCOUNTL:
14006 case BUILT_IN_POPCOUNTLL:
14007 case BUILT_IN_POPCOUNTIMAX:
14008 case BUILT_IN_POPCOUNT:
14009 case BUILT_IN_PARITYL:
14010 case BUILT_IN_PARITYLL:
14011 case BUILT_IN_PARITYIMAX:
14012 case BUILT_IN_PARITY:
14013 case BUILT_IN_LABS:
14014 case BUILT_IN_LLABS:
14015 case BUILT_IN_PREFETCH:
14016 return true;
14017
14018 default:
14019 return is_simple_builtin (decl);
14020 }
14021
14022 return false;
14023 }