target.h (globalize_decl_name): New.
[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
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "machmode.h"
28 #include "real.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "tree-gimple.h"
32 #include "flags.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "except.h"
36 #include "function.h"
37 #include "insn-config.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "typeclass.h"
44 #include "toplev.h"
45 #include "predict.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "langhooks.h"
49 #include "basic-block.h"
50 #include "tree-mudflap.h"
51 #include "tree-flow.h"
52
53 #ifndef PAD_VARARGS_DOWN
54 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 #endif
56
57 /* Define the names of the builtin function types and codes. */
58 const char *const built_in_class_names[4]
59 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
60
61 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
62 const char * built_in_names[(int) END_BUILTINS] =
63 {
64 #include "builtins.def"
65 };
66 #undef DEF_BUILTIN
67
68 /* Setup an array of _DECL trees, make sure each element is
69 initialized to NULL_TREE. */
70 tree built_in_decls[(int) END_BUILTINS];
71 /* Declarations used when constructing the builtin implicitly in the compiler.
72 It may be NULL_TREE when this is invalid (for instance runtime is not
73 required to implement the function call in all cases). */
74 tree implicit_built_in_decls[(int) END_BUILTINS];
75
76 static const char *c_getstr (tree);
77 static rtx c_readstr (const char *, enum machine_mode);
78 static int target_char_cast (tree, char *);
79 static rtx get_memory_rtx (tree, tree);
80 static int apply_args_size (void);
81 static int apply_result_size (void);
82 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
83 static rtx result_vector (int, rtx);
84 #endif
85 static void expand_builtin_update_setjmp_buf (rtx);
86 static void expand_builtin_prefetch (tree);
87 static rtx expand_builtin_apply_args (void);
88 static rtx expand_builtin_apply_args_1 (void);
89 static rtx expand_builtin_apply (rtx, rtx, rtx);
90 static void expand_builtin_return (rtx);
91 static enum type_class type_to_class (tree);
92 static rtx expand_builtin_classify_type (tree);
93 static void expand_errno_check (tree, rtx);
94 static rtx expand_builtin_mathfn (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
97 static rtx expand_builtin_sincos (tree);
98 static rtx expand_builtin_cexpi (tree, rtx, rtx);
99 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
100 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
101 static rtx expand_builtin_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree);
118 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
124 static rtx expand_builtin_bzero (tree);
125 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_alloca (tree, rtx);
131 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
132 static rtx expand_builtin_frame_address (tree, tree);
133 static rtx expand_builtin_fputs (tree, rtx, bool);
134 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
137 static tree stabilize_va_list (tree, int);
138 static rtx expand_builtin_expect (tree, rtx);
139 static tree fold_builtin_constant_p (tree);
140 static tree fold_builtin_classify_type (tree);
141 static tree fold_builtin_strlen (tree);
142 static tree fold_builtin_inf (tree, int);
143 static tree fold_builtin_nan (tree, tree, int);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_cosh (tree, tree, tree);
155 static tree fold_builtin_tan (tree, tree);
156 static tree fold_builtin_trunc (tree, tree);
157 static tree fold_builtin_floor (tree, tree);
158 static tree fold_builtin_ceil (tree, tree);
159 static tree fold_builtin_round (tree, tree);
160 static tree fold_builtin_int_roundingfn (tree, tree);
161 static tree fold_builtin_bitop (tree, tree);
162 static tree fold_builtin_memory_op (tree, tree, bool, int);
163 static tree fold_builtin_strchr (tree, tree);
164 static tree fold_builtin_memcmp (tree);
165 static tree fold_builtin_strcmp (tree);
166 static tree fold_builtin_strncmp (tree);
167 static tree fold_builtin_signbit (tree, tree);
168 static tree fold_builtin_copysign (tree, tree, tree);
169 static tree fold_builtin_isascii (tree);
170 static tree fold_builtin_toascii (tree);
171 static tree fold_builtin_isdigit (tree);
172 static tree fold_builtin_fabs (tree, tree);
173 static tree fold_builtin_abs (tree, tree);
174 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
175 enum tree_code);
176 static tree fold_builtin_1 (tree, tree, bool);
177
178 static tree fold_builtin_strpbrk (tree, tree);
179 static tree fold_builtin_strstr (tree, tree);
180 static tree fold_builtin_strrchr (tree, tree);
181 static tree fold_builtin_strcat (tree);
182 static tree fold_builtin_strncat (tree);
183 static tree fold_builtin_strspn (tree);
184 static tree fold_builtin_strcspn (tree);
185 static tree fold_builtin_sprintf (tree, int);
186
187 static rtx expand_builtin_object_size (tree);
188 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
189 enum built_in_function);
190 static void maybe_emit_chk_warning (tree, enum built_in_function);
191 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
192 static tree fold_builtin_object_size (tree);
193 static tree fold_builtin_strcat_chk (tree, tree);
194 static tree fold_builtin_strncat_chk (tree, tree);
195 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
196 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
197 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
198 static bool init_target_chars (void);
199
200 static unsigned HOST_WIDE_INT target_newline;
201 static unsigned HOST_WIDE_INT target_percent;
202 static unsigned HOST_WIDE_INT target_c;
203 static unsigned HOST_WIDE_INT target_s;
204 static char target_percent_c[3];
205 static char target_percent_s[3];
206 static char target_percent_s_newline[4];
207 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
208 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
209 static tree do_mpfr_arg2 (tree, tree, tree,
210 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
211 static tree do_mpfr_arg3 (tree, tree, tree, tree,
212 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
213 static tree do_mpfr_sincos (tree, tree, tree);
214
215 /* Return true if NODE should be considered for inline expansion regardless
216 of the optimization level. This means whenever a function is invoked with
217 its "internal" name, which normally contains the prefix "__builtin". */
218
219 static bool called_as_built_in (tree node)
220 {
221 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
222 if (strncmp (name, "__builtin_", 10) == 0)
223 return true;
224 if (strncmp (name, "__sync_", 7) == 0)
225 return true;
226 return false;
227 }
228
229 /* Return the alignment in bits of EXP, a pointer valued expression.
230 But don't return more than MAX_ALIGN no matter what.
231 The alignment returned is, by default, the alignment of the thing that
232 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
233
234 Otherwise, look at the expression to see if we can do better, i.e., if the
235 expression is actually pointing at an object whose alignment is tighter. */
236
237 int
238 get_pointer_alignment (tree exp, unsigned int max_align)
239 {
240 unsigned int align, inner;
241
242 /* We rely on TER to compute accurate alignment information. */
243 if (!(optimize && flag_tree_ter))
244 return 0;
245
246 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
247 return 0;
248
249 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
250 align = MIN (align, max_align);
251
252 while (1)
253 {
254 switch (TREE_CODE (exp))
255 {
256 case NOP_EXPR:
257 case CONVERT_EXPR:
258 case NON_LVALUE_EXPR:
259 exp = TREE_OPERAND (exp, 0);
260 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
261 return align;
262
263 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
264 align = MIN (inner, max_align);
265 break;
266
267 case PLUS_EXPR:
268 /* If sum of pointer + int, restrict our maximum alignment to that
269 imposed by the integer. If not, we can't do any better than
270 ALIGN. */
271 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
272 return align;
273
274 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
275 & (max_align / BITS_PER_UNIT - 1))
276 != 0)
277 max_align >>= 1;
278
279 exp = TREE_OPERAND (exp, 0);
280 break;
281
282 case ADDR_EXPR:
283 /* See what we are pointing at and look at its alignment. */
284 exp = TREE_OPERAND (exp, 0);
285 inner = max_align;
286 if (handled_component_p (exp))
287 {
288 HOST_WIDE_INT bitsize, bitpos;
289 tree offset;
290 enum machine_mode mode;
291 int unsignedp, volatilep;
292
293 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
294 &mode, &unsignedp, &volatilep, true);
295 if (bitpos)
296 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
297 if (offset && TREE_CODE (offset) == PLUS_EXPR
298 && host_integerp (TREE_OPERAND (offset, 1), 1))
299 {
300 /* Any overflow in calculating offset_bits won't change
301 the alignment. */
302 unsigned offset_bits
303 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
304 * BITS_PER_UNIT);
305
306 if (offset_bits)
307 inner = MIN (inner, (offset_bits & -offset_bits));
308 offset = TREE_OPERAND (offset, 0);
309 }
310 if (offset && TREE_CODE (offset) == MULT_EXPR
311 && host_integerp (TREE_OPERAND (offset, 1), 1))
312 {
313 /* Any overflow in calculating offset_factor won't change
314 the alignment. */
315 unsigned offset_factor
316 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
317 * BITS_PER_UNIT);
318
319 if (offset_factor)
320 inner = MIN (inner, (offset_factor & -offset_factor));
321 }
322 else if (offset)
323 inner = MIN (inner, BITS_PER_UNIT);
324 }
325 if (TREE_CODE (exp) == FUNCTION_DECL)
326 align = FUNCTION_BOUNDARY;
327 else if (DECL_P (exp))
328 align = MIN (inner, DECL_ALIGN (exp));
329 #ifdef CONSTANT_ALIGNMENT
330 else if (CONSTANT_CLASS_P (exp))
331 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
332 #endif
333 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
334 || TREE_CODE (exp) == INDIRECT_REF)
335 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
336 else
337 align = MIN (align, inner);
338 return MIN (align, max_align);
339
340 default:
341 return align;
342 }
343 }
344 }
345
346 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
347 way, because it could contain a zero byte in the middle.
348 TREE_STRING_LENGTH is the size of the character array, not the string.
349
350 ONLY_VALUE should be nonzero if the result is not going to be emitted
351 into the instruction stream and zero if it is going to be expanded.
352 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
353 is returned, otherwise NULL, since
354 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
355 evaluate the side-effects.
356
357 The value returned is of type `ssizetype'.
358
359 Unfortunately, string_constant can't access the values of const char
360 arrays with initializers, so neither can we do so here. */
361
362 tree
363 c_strlen (tree src, int only_value)
364 {
365 tree offset_node;
366 HOST_WIDE_INT offset;
367 int max;
368 const char *ptr;
369
370 STRIP_NOPS (src);
371 if (TREE_CODE (src) == COND_EXPR
372 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
373 {
374 tree len1, len2;
375
376 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
377 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
378 if (tree_int_cst_equal (len1, len2))
379 return len1;
380 }
381
382 if (TREE_CODE (src) == COMPOUND_EXPR
383 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
384 return c_strlen (TREE_OPERAND (src, 1), only_value);
385
386 src = string_constant (src, &offset_node);
387 if (src == 0)
388 return 0;
389
390 max = TREE_STRING_LENGTH (src) - 1;
391 ptr = TREE_STRING_POINTER (src);
392
393 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
394 {
395 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
396 compute the offset to the following null if we don't know where to
397 start searching for it. */
398 int i;
399
400 for (i = 0; i < max; i++)
401 if (ptr[i] == 0)
402 return 0;
403
404 /* We don't know the starting offset, but we do know that the string
405 has no internal zero bytes. We can assume that the offset falls
406 within the bounds of the string; otherwise, the programmer deserves
407 what he gets. Subtract the offset from the length of the string,
408 and return that. This would perhaps not be valid if we were dealing
409 with named arrays in addition to literal string constants. */
410
411 return size_diffop (size_int (max), offset_node);
412 }
413
414 /* We have a known offset into the string. Start searching there for
415 a null character if we can represent it as a single HOST_WIDE_INT. */
416 if (offset_node == 0)
417 offset = 0;
418 else if (! host_integerp (offset_node, 0))
419 offset = -1;
420 else
421 offset = tree_low_cst (offset_node, 0);
422
423 /* If the offset is known to be out of bounds, warn, and call strlen at
424 runtime. */
425 if (offset < 0 || offset > max)
426 {
427 warning (0, "offset outside bounds of constant string");
428 return 0;
429 }
430
431 /* Use strlen to search for the first zero byte. Since any strings
432 constructed with build_string will have nulls appended, we win even
433 if we get handed something like (char[4])"abcd".
434
435 Since OFFSET is our starting index into the string, no further
436 calculation is needed. */
437 return ssize_int (strlen (ptr + offset));
438 }
439
440 /* Return a char pointer for a C string if it is a string constant
441 or sum of string constant and integer constant. */
442
443 static const char *
444 c_getstr (tree src)
445 {
446 tree offset_node;
447
448 src = string_constant (src, &offset_node);
449 if (src == 0)
450 return 0;
451
452 if (offset_node == 0)
453 return TREE_STRING_POINTER (src);
454 else if (!host_integerp (offset_node, 1)
455 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
456 return 0;
457
458 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
459 }
460
461 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
462 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
463
464 static rtx
465 c_readstr (const char *str, enum machine_mode mode)
466 {
467 HOST_WIDE_INT c[2];
468 HOST_WIDE_INT ch;
469 unsigned int i, j;
470
471 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
472
473 c[0] = 0;
474 c[1] = 0;
475 ch = 1;
476 for (i = 0; i < GET_MODE_SIZE (mode); i++)
477 {
478 j = i;
479 if (WORDS_BIG_ENDIAN)
480 j = GET_MODE_SIZE (mode) - i - 1;
481 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
482 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
483 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
484 j *= BITS_PER_UNIT;
485 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
486
487 if (ch)
488 ch = (unsigned char) str[i];
489 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
490 }
491 return immed_double_const (c[0], c[1], mode);
492 }
493
494 /* Cast a target constant CST to target CHAR and if that value fits into
495 host char type, return zero and put that value into variable pointed to by
496 P. */
497
498 static int
499 target_char_cast (tree cst, char *p)
500 {
501 unsigned HOST_WIDE_INT val, hostval;
502
503 if (!host_integerp (cst, 1)
504 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
505 return 1;
506
507 val = tree_low_cst (cst, 1);
508 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
509 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
510
511 hostval = val;
512 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
513 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
514
515 if (val != hostval)
516 return 1;
517
518 *p = hostval;
519 return 0;
520 }
521
522 /* Similar to save_expr, but assumes that arbitrary code is not executed
523 in between the multiple evaluations. In particular, we assume that a
524 non-addressable local variable will not be modified. */
525
526 static tree
527 builtin_save_expr (tree exp)
528 {
529 if (TREE_ADDRESSABLE (exp) == 0
530 && (TREE_CODE (exp) == PARM_DECL
531 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
532 return exp;
533
534 return save_expr (exp);
535 }
536
537 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
538 times to get the address of either a higher stack frame, or a return
539 address located within it (depending on FNDECL_CODE). */
540
541 static rtx
542 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
543 {
544 int i;
545
546 #ifdef INITIAL_FRAME_ADDRESS_RTX
547 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
548 #else
549 rtx tem;
550
551 /* For a zero count with __builtin_return_address, we don't care what
552 frame address we return, because target-specific definitions will
553 override us. Therefore frame pointer elimination is OK, and using
554 the soft frame pointer is OK.
555
556 For a nonzero count, or a zero count with __builtin_frame_address,
557 we require a stable offset from the current frame pointer to the
558 previous one, so we must use the hard frame pointer, and
559 we must disable frame pointer elimination. */
560 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
561 tem = frame_pointer_rtx;
562 else
563 {
564 tem = hard_frame_pointer_rtx;
565
566 /* Tell reload not to eliminate the frame pointer. */
567 current_function_accesses_prior_frames = 1;
568 }
569 #endif
570
571 /* Some machines need special handling before we can access
572 arbitrary frames. For example, on the SPARC, we must first flush
573 all register windows to the stack. */
574 #ifdef SETUP_FRAME_ADDRESSES
575 if (count > 0)
576 SETUP_FRAME_ADDRESSES ();
577 #endif
578
579 /* On the SPARC, the return address is not in the frame, it is in a
580 register. There is no way to access it off of the current frame
581 pointer, but it can be accessed off the previous frame pointer by
582 reading the value from the register window save area. */
583 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
584 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
585 count--;
586 #endif
587
588 /* Scan back COUNT frames to the specified frame. */
589 for (i = 0; i < count; i++)
590 {
591 /* Assume the dynamic chain pointer is in the word that the
592 frame address points to, unless otherwise specified. */
593 #ifdef DYNAMIC_CHAIN_ADDRESS
594 tem = DYNAMIC_CHAIN_ADDRESS (tem);
595 #endif
596 tem = memory_address (Pmode, tem);
597 tem = gen_frame_mem (Pmode, tem);
598 tem = copy_to_reg (tem);
599 }
600
601 /* For __builtin_frame_address, return what we've got. But, on
602 the SPARC for example, we may have to add a bias. */
603 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
604 #ifdef FRAME_ADDR_RTX
605 return FRAME_ADDR_RTX (tem);
606 #else
607 return tem;
608 #endif
609
610 /* For __builtin_return_address, get the return address from that frame. */
611 #ifdef RETURN_ADDR_RTX
612 tem = RETURN_ADDR_RTX (count, tem);
613 #else
614 tem = memory_address (Pmode,
615 plus_constant (tem, GET_MODE_SIZE (Pmode)));
616 tem = gen_frame_mem (Pmode, tem);
617 #endif
618 return tem;
619 }
620
621 /* Alias set used for setjmp buffer. */
622 static HOST_WIDE_INT setjmp_alias_set = -1;
623
624 /* Construct the leading half of a __builtin_setjmp call. Control will
625 return to RECEIVER_LABEL. This is also called directly by the SJLJ
626 exception handling code. */
627
628 void
629 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
630 {
631 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
632 rtx stack_save;
633 rtx mem;
634
635 if (setjmp_alias_set == -1)
636 setjmp_alias_set = new_alias_set ();
637
638 buf_addr = convert_memory_address (Pmode, buf_addr);
639
640 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
641
642 /* We store the frame pointer and the address of receiver_label in
643 the buffer and use the rest of it for the stack save area, which
644 is machine-dependent. */
645
646 mem = gen_rtx_MEM (Pmode, buf_addr);
647 set_mem_alias_set (mem, setjmp_alias_set);
648 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
649
650 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
651 set_mem_alias_set (mem, setjmp_alias_set);
652
653 emit_move_insn (validize_mem (mem),
654 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
655
656 stack_save = gen_rtx_MEM (sa_mode,
657 plus_constant (buf_addr,
658 2 * GET_MODE_SIZE (Pmode)));
659 set_mem_alias_set (stack_save, setjmp_alias_set);
660 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
661
662 /* If there is further processing to do, do it. */
663 #ifdef HAVE_builtin_setjmp_setup
664 if (HAVE_builtin_setjmp_setup)
665 emit_insn (gen_builtin_setjmp_setup (buf_addr));
666 #endif
667
668 /* Tell optimize_save_area_alloca that extra work is going to
669 need to go on during alloca. */
670 current_function_calls_setjmp = 1;
671
672 /* Set this so all the registers get saved in our frame; we need to be
673 able to copy the saved values for any registers from frames we unwind. */
674 current_function_has_nonlocal_label = 1;
675 }
676
677 /* Construct the trailing part of a __builtin_setjmp call. This is
678 also called directly by the SJLJ exception handling code. */
679
680 void
681 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
682 {
683 /* Clobber the FP when we get here, so we have to make sure it's
684 marked as used by this function. */
685 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
686
687 /* Mark the static chain as clobbered here so life information
688 doesn't get messed up for it. */
689 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
690
691 /* Now put in the code to restore the frame pointer, and argument
692 pointer, if needed. */
693 #ifdef HAVE_nonlocal_goto
694 if (! HAVE_nonlocal_goto)
695 #endif
696 {
697 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
698 /* This might change the hard frame pointer in ways that aren't
699 apparent to early optimization passes, so force a clobber. */
700 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
701 }
702
703 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
704 if (fixed_regs[ARG_POINTER_REGNUM])
705 {
706 #ifdef ELIMINABLE_REGS
707 size_t i;
708 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
709
710 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
711 if (elim_regs[i].from == ARG_POINTER_REGNUM
712 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
713 break;
714
715 if (i == ARRAY_SIZE (elim_regs))
716 #endif
717 {
718 /* Now restore our arg pointer from the address at which it
719 was saved in our stack frame. */
720 emit_move_insn (virtual_incoming_args_rtx,
721 copy_to_reg (get_arg_pointer_save_area (cfun)));
722 }
723 }
724 #endif
725
726 #ifdef HAVE_builtin_setjmp_receiver
727 if (HAVE_builtin_setjmp_receiver)
728 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
729 else
730 #endif
731 #ifdef HAVE_nonlocal_goto_receiver
732 if (HAVE_nonlocal_goto_receiver)
733 emit_insn (gen_nonlocal_goto_receiver ());
734 else
735 #endif
736 { /* Nothing */ }
737
738 /* @@@ This is a kludge. Not all machine descriptions define a blockage
739 insn, but we must not allow the code we just generated to be reordered
740 by scheduling. Specifically, the update of the frame pointer must
741 happen immediately, not later. So emit an ASM_INPUT to act as blockage
742 insn. */
743 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
744 }
745
746 /* __builtin_longjmp is passed a pointer to an array of five words (not
747 all will be used on all machines). It operates similarly to the C
748 library function of the same name, but is more efficient. Much of
749 the code below is copied from the handling of non-local gotos. */
750
751 static void
752 expand_builtin_longjmp (rtx buf_addr, rtx value)
753 {
754 rtx fp, lab, stack, insn, last;
755 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
756
757 if (setjmp_alias_set == -1)
758 setjmp_alias_set = new_alias_set ();
759
760 buf_addr = convert_memory_address (Pmode, buf_addr);
761
762 buf_addr = force_reg (Pmode, buf_addr);
763
764 /* We used to store value in static_chain_rtx, but that fails if pointers
765 are smaller than integers. We instead require that the user must pass
766 a second argument of 1, because that is what builtin_setjmp will
767 return. This also makes EH slightly more efficient, since we are no
768 longer copying around a value that we don't care about. */
769 gcc_assert (value == const1_rtx);
770
771 last = get_last_insn ();
772 #ifdef HAVE_builtin_longjmp
773 if (HAVE_builtin_longjmp)
774 emit_insn (gen_builtin_longjmp (buf_addr));
775 else
776 #endif
777 {
778 fp = gen_rtx_MEM (Pmode, buf_addr);
779 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
780 GET_MODE_SIZE (Pmode)));
781
782 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
783 2 * GET_MODE_SIZE (Pmode)));
784 set_mem_alias_set (fp, setjmp_alias_set);
785 set_mem_alias_set (lab, setjmp_alias_set);
786 set_mem_alias_set (stack, setjmp_alias_set);
787
788 /* Pick up FP, label, and SP from the block and jump. This code is
789 from expand_goto in stmt.c; see there for detailed comments. */
790 #ifdef HAVE_nonlocal_goto
791 if (HAVE_nonlocal_goto)
792 /* We have to pass a value to the nonlocal_goto pattern that will
793 get copied into the static_chain pointer, but it does not matter
794 what that value is, because builtin_setjmp does not use it. */
795 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
796 else
797 #endif
798 {
799 lab = copy_to_reg (lab);
800
801 emit_insn (gen_rtx_CLOBBER (VOIDmode,
802 gen_rtx_MEM (BLKmode,
803 gen_rtx_SCRATCH (VOIDmode))));
804 emit_insn (gen_rtx_CLOBBER (VOIDmode,
805 gen_rtx_MEM (BLKmode,
806 hard_frame_pointer_rtx)));
807
808 emit_move_insn (hard_frame_pointer_rtx, fp);
809 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
810
811 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
812 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
813 emit_indirect_jump (lab);
814 }
815 }
816
817 /* Search backwards and mark the jump insn as a non-local goto.
818 Note that this precludes the use of __builtin_longjmp to a
819 __builtin_setjmp target in the same function. However, we've
820 already cautioned the user that these functions are for
821 internal exception handling use only. */
822 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
823 {
824 gcc_assert (insn != last);
825
826 if (JUMP_P (insn))
827 {
828 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
829 REG_NOTES (insn));
830 break;
831 }
832 else if (CALL_P (insn))
833 break;
834 }
835 }
836
837 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
838 and the address of the save area. */
839
840 static rtx
841 expand_builtin_nonlocal_goto (tree arglist)
842 {
843 tree t_label, t_save_area;
844 rtx r_label, r_save_area, r_fp, r_sp, insn;
845
846 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
847 return NULL_RTX;
848
849 t_label = TREE_VALUE (arglist);
850 arglist = TREE_CHAIN (arglist);
851 t_save_area = TREE_VALUE (arglist);
852
853 r_label = expand_normal (t_label);
854 r_label = convert_memory_address (Pmode, r_label);
855 r_save_area = expand_normal (t_save_area);
856 r_save_area = convert_memory_address (Pmode, r_save_area);
857 r_fp = gen_rtx_MEM (Pmode, r_save_area);
858 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
859 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
860
861 current_function_has_nonlocal_goto = 1;
862
863 #ifdef HAVE_nonlocal_goto
864 /* ??? We no longer need to pass the static chain value, afaik. */
865 if (HAVE_nonlocal_goto)
866 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
867 else
868 #endif
869 {
870 r_label = copy_to_reg (r_label);
871
872 emit_insn (gen_rtx_CLOBBER (VOIDmode,
873 gen_rtx_MEM (BLKmode,
874 gen_rtx_SCRATCH (VOIDmode))));
875
876 emit_insn (gen_rtx_CLOBBER (VOIDmode,
877 gen_rtx_MEM (BLKmode,
878 hard_frame_pointer_rtx)));
879
880 /* Restore frame pointer for containing function.
881 This sets the actual hard register used for the frame pointer
882 to the location of the function's incoming static chain info.
883 The non-local goto handler will then adjust it to contain the
884 proper value and reload the argument pointer, if needed. */
885 emit_move_insn (hard_frame_pointer_rtx, r_fp);
886 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
887
888 /* USE of hard_frame_pointer_rtx added for consistency;
889 not clear if really needed. */
890 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
891 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
892 emit_indirect_jump (r_label);
893 }
894
895 /* Search backwards to the jump insn and mark it as a
896 non-local goto. */
897 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
898 {
899 if (JUMP_P (insn))
900 {
901 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
902 const0_rtx, REG_NOTES (insn));
903 break;
904 }
905 else if (CALL_P (insn))
906 break;
907 }
908
909 return const0_rtx;
910 }
911
912 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
913 (not all will be used on all machines) that was passed to __builtin_setjmp.
914 It updates the stack pointer in that block to correspond to the current
915 stack pointer. */
916
917 static void
918 expand_builtin_update_setjmp_buf (rtx buf_addr)
919 {
920 enum machine_mode sa_mode = Pmode;
921 rtx stack_save;
922
923
924 #ifdef HAVE_save_stack_nonlocal
925 if (HAVE_save_stack_nonlocal)
926 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
927 #endif
928 #ifdef STACK_SAVEAREA_MODE
929 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
930 #endif
931
932 stack_save
933 = gen_rtx_MEM (sa_mode,
934 memory_address
935 (sa_mode,
936 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
937
938 #ifdef HAVE_setjmp
939 if (HAVE_setjmp)
940 emit_insn (gen_setjmp ());
941 #endif
942
943 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
944 }
945
946 /* Expand a call to __builtin_prefetch. For a target that does not support
947 data prefetch, evaluate the memory address argument in case it has side
948 effects. */
949
950 static void
951 expand_builtin_prefetch (tree arglist)
952 {
953 tree arg0, arg1, arg2;
954 rtx op0, op1, op2;
955
956 if (!validate_arglist (arglist, POINTER_TYPE, 0))
957 return;
958
959 arg0 = TREE_VALUE (arglist);
960 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
961 zero (read) and argument 2 (locality) defaults to 3 (high degree of
962 locality). */
963 if (TREE_CHAIN (arglist))
964 {
965 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
966 if (TREE_CHAIN (TREE_CHAIN (arglist)))
967 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
968 else
969 arg2 = build_int_cst (NULL_TREE, 3);
970 }
971 else
972 {
973 arg1 = integer_zero_node;
974 arg2 = build_int_cst (NULL_TREE, 3);
975 }
976
977 /* Argument 0 is an address. */
978 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
979
980 /* Argument 1 (read/write flag) must be a compile-time constant int. */
981 if (TREE_CODE (arg1) != INTEGER_CST)
982 {
983 error ("second argument to %<__builtin_prefetch%> must be a constant");
984 arg1 = integer_zero_node;
985 }
986 op1 = expand_normal (arg1);
987 /* Argument 1 must be either zero or one. */
988 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
989 {
990 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
991 " using zero");
992 op1 = const0_rtx;
993 }
994
995 /* Argument 2 (locality) must be a compile-time constant int. */
996 if (TREE_CODE (arg2) != INTEGER_CST)
997 {
998 error ("third argument to %<__builtin_prefetch%> must be a constant");
999 arg2 = integer_zero_node;
1000 }
1001 op2 = expand_normal (arg2);
1002 /* Argument 2 must be 0, 1, 2, or 3. */
1003 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1004 {
1005 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1006 op2 = const0_rtx;
1007 }
1008
1009 #ifdef HAVE_prefetch
1010 if (HAVE_prefetch)
1011 {
1012 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1013 (op0,
1014 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1015 || (GET_MODE (op0) != Pmode))
1016 {
1017 op0 = convert_memory_address (Pmode, op0);
1018 op0 = force_reg (Pmode, op0);
1019 }
1020 emit_insn (gen_prefetch (op0, op1, op2));
1021 }
1022 #endif
1023
1024 /* Don't do anything with direct references to volatile memory, but
1025 generate code to handle other side effects. */
1026 if (!MEM_P (op0) && side_effects_p (op0))
1027 emit_insn (op0);
1028 }
1029
1030 /* Get a MEM rtx for expression EXP which is the address of an operand
1031 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1032 the maximum length of the block of memory that might be accessed or
1033 NULL if unknown. */
1034
1035 static rtx
1036 get_memory_rtx (tree exp, tree len)
1037 {
1038 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1039 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1040
1041 /* Get an expression we can use to find the attributes to assign to MEM.
1042 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1043 we can. First remove any nops. */
1044 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1045 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1046 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1047 exp = TREE_OPERAND (exp, 0);
1048
1049 if (TREE_CODE (exp) == ADDR_EXPR)
1050 exp = TREE_OPERAND (exp, 0);
1051 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1052 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1053 else
1054 exp = NULL;
1055
1056 /* Honor attributes derived from exp, except for the alias set
1057 (as builtin stringops may alias with anything) and the size
1058 (as stringops may access multiple array elements). */
1059 if (exp)
1060 {
1061 set_mem_attributes (mem, exp, 0);
1062
1063 /* Allow the string and memory builtins to overflow from one
1064 field into another, see http://gcc.gnu.org/PR23561.
1065 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1066 memory accessed by the string or memory builtin will fit
1067 within the field. */
1068 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1069 {
1070 tree mem_expr = MEM_EXPR (mem);
1071 HOST_WIDE_INT offset = -1, length = -1;
1072 tree inner = exp;
1073
1074 while (TREE_CODE (inner) == ARRAY_REF
1075 || TREE_CODE (inner) == NOP_EXPR
1076 || TREE_CODE (inner) == CONVERT_EXPR
1077 || TREE_CODE (inner) == NON_LVALUE_EXPR
1078 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1079 || TREE_CODE (inner) == SAVE_EXPR)
1080 inner = TREE_OPERAND (inner, 0);
1081
1082 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1083
1084 if (MEM_OFFSET (mem)
1085 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1086 offset = INTVAL (MEM_OFFSET (mem));
1087
1088 if (offset >= 0 && len && host_integerp (len, 0))
1089 length = tree_low_cst (len, 0);
1090
1091 while (TREE_CODE (inner) == COMPONENT_REF)
1092 {
1093 tree field = TREE_OPERAND (inner, 1);
1094 gcc_assert (! DECL_BIT_FIELD (field));
1095 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1096 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1097
1098 if (length >= 0
1099 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1100 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1101 {
1102 HOST_WIDE_INT size
1103 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1104 /* If we can prove the memory starting at XEXP (mem, 0)
1105 and ending at XEXP (mem, 0) + LENGTH will fit into
1106 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1107 if (offset <= size
1108 && length <= size
1109 && offset + length <= size)
1110 break;
1111 }
1112
1113 if (offset >= 0
1114 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1115 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1116 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1117 / BITS_PER_UNIT;
1118 else
1119 {
1120 offset = -1;
1121 length = -1;
1122 }
1123
1124 mem_expr = TREE_OPERAND (mem_expr, 0);
1125 inner = TREE_OPERAND (inner, 0);
1126 }
1127
1128 if (mem_expr == NULL)
1129 offset = -1;
1130 if (mem_expr != MEM_EXPR (mem))
1131 {
1132 set_mem_expr (mem, mem_expr);
1133 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1134 }
1135 }
1136 set_mem_alias_set (mem, 0);
1137 set_mem_size (mem, NULL_RTX);
1138 }
1139
1140 return mem;
1141 }
1142 \f
1143 /* Built-in functions to perform an untyped call and return. */
1144
1145 /* For each register that may be used for calling a function, this
1146 gives a mode used to copy the register's value. VOIDmode indicates
1147 the register is not used for calling a function. If the machine
1148 has register windows, this gives only the outbound registers.
1149 INCOMING_REGNO gives the corresponding inbound register. */
1150 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1151
1152 /* For each register that may be used for returning values, this gives
1153 a mode used to copy the register's value. VOIDmode indicates the
1154 register is not used for returning values. If the machine has
1155 register windows, this gives only the outbound registers.
1156 INCOMING_REGNO gives the corresponding inbound register. */
1157 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1158
1159 /* For each register that may be used for calling a function, this
1160 gives the offset of that register into the block returned by
1161 __builtin_apply_args. 0 indicates that the register is not
1162 used for calling a function. */
1163 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1164
1165 /* Return the size required for the block returned by __builtin_apply_args,
1166 and initialize apply_args_mode. */
1167
1168 static int
1169 apply_args_size (void)
1170 {
1171 static int size = -1;
1172 int align;
1173 unsigned int regno;
1174 enum machine_mode mode;
1175
1176 /* The values computed by this function never change. */
1177 if (size < 0)
1178 {
1179 /* The first value is the incoming arg-pointer. */
1180 size = GET_MODE_SIZE (Pmode);
1181
1182 /* The second value is the structure value address unless this is
1183 passed as an "invisible" first argument. */
1184 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1185 size += GET_MODE_SIZE (Pmode);
1186
1187 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1188 if (FUNCTION_ARG_REGNO_P (regno))
1189 {
1190 mode = reg_raw_mode[regno];
1191
1192 gcc_assert (mode != VOIDmode);
1193
1194 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1195 if (size % align != 0)
1196 size = CEIL (size, align) * align;
1197 apply_args_reg_offset[regno] = size;
1198 size += GET_MODE_SIZE (mode);
1199 apply_args_mode[regno] = mode;
1200 }
1201 else
1202 {
1203 apply_args_mode[regno] = VOIDmode;
1204 apply_args_reg_offset[regno] = 0;
1205 }
1206 }
1207 return size;
1208 }
1209
1210 /* Return the size required for the block returned by __builtin_apply,
1211 and initialize apply_result_mode. */
1212
1213 static int
1214 apply_result_size (void)
1215 {
1216 static int size = -1;
1217 int align, regno;
1218 enum machine_mode mode;
1219
1220 /* The values computed by this function never change. */
1221 if (size < 0)
1222 {
1223 size = 0;
1224
1225 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1226 if (FUNCTION_VALUE_REGNO_P (regno))
1227 {
1228 mode = reg_raw_mode[regno];
1229
1230 gcc_assert (mode != VOIDmode);
1231
1232 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1233 if (size % align != 0)
1234 size = CEIL (size, align) * align;
1235 size += GET_MODE_SIZE (mode);
1236 apply_result_mode[regno] = mode;
1237 }
1238 else
1239 apply_result_mode[regno] = VOIDmode;
1240
1241 /* Allow targets that use untyped_call and untyped_return to override
1242 the size so that machine-specific information can be stored here. */
1243 #ifdef APPLY_RESULT_SIZE
1244 size = APPLY_RESULT_SIZE;
1245 #endif
1246 }
1247 return size;
1248 }
1249
1250 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1251 /* Create a vector describing the result block RESULT. If SAVEP is true,
1252 the result block is used to save the values; otherwise it is used to
1253 restore the values. */
1254
1255 static rtx
1256 result_vector (int savep, rtx result)
1257 {
1258 int regno, size, align, nelts;
1259 enum machine_mode mode;
1260 rtx reg, mem;
1261 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1262
1263 size = nelts = 0;
1264 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1265 if ((mode = apply_result_mode[regno]) != VOIDmode)
1266 {
1267 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1268 if (size % align != 0)
1269 size = CEIL (size, align) * align;
1270 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1271 mem = adjust_address (result, mode, size);
1272 savevec[nelts++] = (savep
1273 ? gen_rtx_SET (VOIDmode, mem, reg)
1274 : gen_rtx_SET (VOIDmode, reg, mem));
1275 size += GET_MODE_SIZE (mode);
1276 }
1277 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1278 }
1279 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1280
1281 /* Save the state required to perform an untyped call with the same
1282 arguments as were passed to the current function. */
1283
1284 static rtx
1285 expand_builtin_apply_args_1 (void)
1286 {
1287 rtx registers, tem;
1288 int size, align, regno;
1289 enum machine_mode mode;
1290 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1291
1292 /* Create a block where the arg-pointer, structure value address,
1293 and argument registers can be saved. */
1294 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1295
1296 /* Walk past the arg-pointer and structure value address. */
1297 size = GET_MODE_SIZE (Pmode);
1298 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1299 size += GET_MODE_SIZE (Pmode);
1300
1301 /* Save each register used in calling a function to the block. */
1302 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1303 if ((mode = apply_args_mode[regno]) != VOIDmode)
1304 {
1305 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1306 if (size % align != 0)
1307 size = CEIL (size, align) * align;
1308
1309 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1310
1311 emit_move_insn (adjust_address (registers, mode, size), tem);
1312 size += GET_MODE_SIZE (mode);
1313 }
1314
1315 /* Save the arg pointer to the block. */
1316 tem = copy_to_reg (virtual_incoming_args_rtx);
1317 #ifdef STACK_GROWS_DOWNWARD
1318 /* We need the pointer as the caller actually passed them to us, not
1319 as we might have pretended they were passed. Make sure it's a valid
1320 operand, as emit_move_insn isn't expected to handle a PLUS. */
1321 tem
1322 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1323 NULL_RTX);
1324 #endif
1325 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1326
1327 size = GET_MODE_SIZE (Pmode);
1328
1329 /* Save the structure value address unless this is passed as an
1330 "invisible" first argument. */
1331 if (struct_incoming_value)
1332 {
1333 emit_move_insn (adjust_address (registers, Pmode, size),
1334 copy_to_reg (struct_incoming_value));
1335 size += GET_MODE_SIZE (Pmode);
1336 }
1337
1338 /* Return the address of the block. */
1339 return copy_addr_to_reg (XEXP (registers, 0));
1340 }
1341
1342 /* __builtin_apply_args returns block of memory allocated on
1343 the stack into which is stored the arg pointer, structure
1344 value address, static chain, and all the registers that might
1345 possibly be used in performing a function call. The code is
1346 moved to the start of the function so the incoming values are
1347 saved. */
1348
1349 static rtx
1350 expand_builtin_apply_args (void)
1351 {
1352 /* Don't do __builtin_apply_args more than once in a function.
1353 Save the result of the first call and reuse it. */
1354 if (apply_args_value != 0)
1355 return apply_args_value;
1356 {
1357 /* When this function is called, it means that registers must be
1358 saved on entry to this function. So we migrate the
1359 call to the first insn of this function. */
1360 rtx temp;
1361 rtx seq;
1362
1363 start_sequence ();
1364 temp = expand_builtin_apply_args_1 ();
1365 seq = get_insns ();
1366 end_sequence ();
1367
1368 apply_args_value = temp;
1369
1370 /* Put the insns after the NOTE that starts the function.
1371 If this is inside a start_sequence, make the outer-level insn
1372 chain current, so the code is placed at the start of the
1373 function. */
1374 push_topmost_sequence ();
1375 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1376 pop_topmost_sequence ();
1377 return temp;
1378 }
1379 }
1380
1381 /* Perform an untyped call and save the state required to perform an
1382 untyped return of whatever value was returned by the given function. */
1383
1384 static rtx
1385 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1386 {
1387 int size, align, regno;
1388 enum machine_mode mode;
1389 rtx incoming_args, result, reg, dest, src, call_insn;
1390 rtx old_stack_level = 0;
1391 rtx call_fusage = 0;
1392 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1393
1394 arguments = convert_memory_address (Pmode, arguments);
1395
1396 /* Create a block where the return registers can be saved. */
1397 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1398
1399 /* Fetch the arg pointer from the ARGUMENTS block. */
1400 incoming_args = gen_reg_rtx (Pmode);
1401 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1402 #ifndef STACK_GROWS_DOWNWARD
1403 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1404 incoming_args, 0, OPTAB_LIB_WIDEN);
1405 #endif
1406
1407 /* Push a new argument block and copy the arguments. Do not allow
1408 the (potential) memcpy call below to interfere with our stack
1409 manipulations. */
1410 do_pending_stack_adjust ();
1411 NO_DEFER_POP;
1412
1413 /* Save the stack with nonlocal if available. */
1414 #ifdef HAVE_save_stack_nonlocal
1415 if (HAVE_save_stack_nonlocal)
1416 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1417 else
1418 #endif
1419 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1420
1421 /* Allocate a block of memory onto the stack and copy the memory
1422 arguments to the outgoing arguments address. */
1423 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1424 dest = virtual_outgoing_args_rtx;
1425 #ifndef STACK_GROWS_DOWNWARD
1426 if (GET_CODE (argsize) == CONST_INT)
1427 dest = plus_constant (dest, -INTVAL (argsize));
1428 else
1429 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1430 #endif
1431 dest = gen_rtx_MEM (BLKmode, dest);
1432 set_mem_align (dest, PARM_BOUNDARY);
1433 src = gen_rtx_MEM (BLKmode, incoming_args);
1434 set_mem_align (src, PARM_BOUNDARY);
1435 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1436
1437 /* Refer to the argument block. */
1438 apply_args_size ();
1439 arguments = gen_rtx_MEM (BLKmode, arguments);
1440 set_mem_align (arguments, PARM_BOUNDARY);
1441
1442 /* Walk past the arg-pointer and structure value address. */
1443 size = GET_MODE_SIZE (Pmode);
1444 if (struct_value)
1445 size += GET_MODE_SIZE (Pmode);
1446
1447 /* Restore each of the registers previously saved. Make USE insns
1448 for each of these registers for use in making the call. */
1449 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1450 if ((mode = apply_args_mode[regno]) != VOIDmode)
1451 {
1452 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1453 if (size % align != 0)
1454 size = CEIL (size, align) * align;
1455 reg = gen_rtx_REG (mode, regno);
1456 emit_move_insn (reg, adjust_address (arguments, mode, size));
1457 use_reg (&call_fusage, reg);
1458 size += GET_MODE_SIZE (mode);
1459 }
1460
1461 /* Restore the structure value address unless this is passed as an
1462 "invisible" first argument. */
1463 size = GET_MODE_SIZE (Pmode);
1464 if (struct_value)
1465 {
1466 rtx value = gen_reg_rtx (Pmode);
1467 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1468 emit_move_insn (struct_value, value);
1469 if (REG_P (struct_value))
1470 use_reg (&call_fusage, struct_value);
1471 size += GET_MODE_SIZE (Pmode);
1472 }
1473
1474 /* All arguments and registers used for the call are set up by now! */
1475 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1476
1477 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1478 and we don't want to load it into a register as an optimization,
1479 because prepare_call_address already did it if it should be done. */
1480 if (GET_CODE (function) != SYMBOL_REF)
1481 function = memory_address (FUNCTION_MODE, function);
1482
1483 /* Generate the actual call instruction and save the return value. */
1484 #ifdef HAVE_untyped_call
1485 if (HAVE_untyped_call)
1486 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1487 result, result_vector (1, result)));
1488 else
1489 #endif
1490 #ifdef HAVE_call_value
1491 if (HAVE_call_value)
1492 {
1493 rtx valreg = 0;
1494
1495 /* Locate the unique return register. It is not possible to
1496 express a call that sets more than one return register using
1497 call_value; use untyped_call for that. In fact, untyped_call
1498 only needs to save the return registers in the given block. */
1499 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1500 if ((mode = apply_result_mode[regno]) != VOIDmode)
1501 {
1502 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1503
1504 valreg = gen_rtx_REG (mode, regno);
1505 }
1506
1507 emit_call_insn (GEN_CALL_VALUE (valreg,
1508 gen_rtx_MEM (FUNCTION_MODE, function),
1509 const0_rtx, NULL_RTX, const0_rtx));
1510
1511 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1512 }
1513 else
1514 #endif
1515 gcc_unreachable ();
1516
1517 /* Find the CALL insn we just emitted, and attach the register usage
1518 information. */
1519 call_insn = last_call_insn ();
1520 add_function_usage_to (call_insn, call_fusage);
1521
1522 /* Restore the stack. */
1523 #ifdef HAVE_save_stack_nonlocal
1524 if (HAVE_save_stack_nonlocal)
1525 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1526 else
1527 #endif
1528 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1529
1530 OK_DEFER_POP;
1531
1532 /* Return the address of the result block. */
1533 result = copy_addr_to_reg (XEXP (result, 0));
1534 return convert_memory_address (ptr_mode, result);
1535 }
1536
1537 /* Perform an untyped return. */
1538
1539 static void
1540 expand_builtin_return (rtx result)
1541 {
1542 int size, align, regno;
1543 enum machine_mode mode;
1544 rtx reg;
1545 rtx call_fusage = 0;
1546
1547 result = convert_memory_address (Pmode, result);
1548
1549 apply_result_size ();
1550 result = gen_rtx_MEM (BLKmode, result);
1551
1552 #ifdef HAVE_untyped_return
1553 if (HAVE_untyped_return)
1554 {
1555 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1556 emit_barrier ();
1557 return;
1558 }
1559 #endif
1560
1561 /* Restore the return value and note that each value is used. */
1562 size = 0;
1563 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1564 if ((mode = apply_result_mode[regno]) != VOIDmode)
1565 {
1566 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1567 if (size % align != 0)
1568 size = CEIL (size, align) * align;
1569 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1570 emit_move_insn (reg, adjust_address (result, mode, size));
1571
1572 push_to_sequence (call_fusage);
1573 emit_insn (gen_rtx_USE (VOIDmode, reg));
1574 call_fusage = get_insns ();
1575 end_sequence ();
1576 size += GET_MODE_SIZE (mode);
1577 }
1578
1579 /* Put the USE insns before the return. */
1580 emit_insn (call_fusage);
1581
1582 /* Return whatever values was restored by jumping directly to the end
1583 of the function. */
1584 expand_naked_return ();
1585 }
1586
1587 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1588
1589 static enum type_class
1590 type_to_class (tree type)
1591 {
1592 switch (TREE_CODE (type))
1593 {
1594 case VOID_TYPE: return void_type_class;
1595 case INTEGER_TYPE: return integer_type_class;
1596 case ENUMERAL_TYPE: return enumeral_type_class;
1597 case BOOLEAN_TYPE: return boolean_type_class;
1598 case POINTER_TYPE: return pointer_type_class;
1599 case REFERENCE_TYPE: return reference_type_class;
1600 case OFFSET_TYPE: return offset_type_class;
1601 case REAL_TYPE: return real_type_class;
1602 case COMPLEX_TYPE: return complex_type_class;
1603 case FUNCTION_TYPE: return function_type_class;
1604 case METHOD_TYPE: return method_type_class;
1605 case RECORD_TYPE: return record_type_class;
1606 case UNION_TYPE:
1607 case QUAL_UNION_TYPE: return union_type_class;
1608 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1609 ? string_type_class : array_type_class);
1610 case LANG_TYPE: return lang_type_class;
1611 default: return no_type_class;
1612 }
1613 }
1614
1615 /* Expand a call to __builtin_classify_type with arguments found in
1616 ARGLIST. */
1617
1618 static rtx
1619 expand_builtin_classify_type (tree arglist)
1620 {
1621 if (arglist != 0)
1622 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1623 return GEN_INT (no_type_class);
1624 }
1625
1626 /* This helper macro, meant to be used in mathfn_built_in below,
1627 determines which among a set of three builtin math functions is
1628 appropriate for a given type mode. The `F' and `L' cases are
1629 automatically generated from the `double' case. */
1630 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1631 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1632 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1633 fcodel = BUILT_IN_MATHFN##L ; break;
1634
1635 /* Return mathematic function equivalent to FN but operating directly
1636 on TYPE, if available. If we can't do the conversion, return zero. */
1637 tree
1638 mathfn_built_in (tree type, enum built_in_function fn)
1639 {
1640 enum built_in_function fcode, fcodef, fcodel;
1641
1642 switch (fn)
1643 {
1644 CASE_MATHFN (BUILT_IN_ACOS)
1645 CASE_MATHFN (BUILT_IN_ACOSH)
1646 CASE_MATHFN (BUILT_IN_ASIN)
1647 CASE_MATHFN (BUILT_IN_ASINH)
1648 CASE_MATHFN (BUILT_IN_ATAN)
1649 CASE_MATHFN (BUILT_IN_ATAN2)
1650 CASE_MATHFN (BUILT_IN_ATANH)
1651 CASE_MATHFN (BUILT_IN_CBRT)
1652 CASE_MATHFN (BUILT_IN_CEIL)
1653 CASE_MATHFN (BUILT_IN_CEXPI)
1654 CASE_MATHFN (BUILT_IN_COPYSIGN)
1655 CASE_MATHFN (BUILT_IN_COS)
1656 CASE_MATHFN (BUILT_IN_COSH)
1657 CASE_MATHFN (BUILT_IN_DREM)
1658 CASE_MATHFN (BUILT_IN_ERF)
1659 CASE_MATHFN (BUILT_IN_ERFC)
1660 CASE_MATHFN (BUILT_IN_EXP)
1661 CASE_MATHFN (BUILT_IN_EXP10)
1662 CASE_MATHFN (BUILT_IN_EXP2)
1663 CASE_MATHFN (BUILT_IN_EXPM1)
1664 CASE_MATHFN (BUILT_IN_FABS)
1665 CASE_MATHFN (BUILT_IN_FDIM)
1666 CASE_MATHFN (BUILT_IN_FLOOR)
1667 CASE_MATHFN (BUILT_IN_FMA)
1668 CASE_MATHFN (BUILT_IN_FMAX)
1669 CASE_MATHFN (BUILT_IN_FMIN)
1670 CASE_MATHFN (BUILT_IN_FMOD)
1671 CASE_MATHFN (BUILT_IN_FREXP)
1672 CASE_MATHFN (BUILT_IN_GAMMA)
1673 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1674 CASE_MATHFN (BUILT_IN_HYPOT)
1675 CASE_MATHFN (BUILT_IN_ILOGB)
1676 CASE_MATHFN (BUILT_IN_INF)
1677 CASE_MATHFN (BUILT_IN_J0)
1678 CASE_MATHFN (BUILT_IN_J1)
1679 CASE_MATHFN (BUILT_IN_JN)
1680 CASE_MATHFN (BUILT_IN_LCEIL)
1681 CASE_MATHFN (BUILT_IN_LDEXP)
1682 CASE_MATHFN (BUILT_IN_LFLOOR)
1683 CASE_MATHFN (BUILT_IN_LGAMMA)
1684 CASE_MATHFN (BUILT_IN_LLCEIL)
1685 CASE_MATHFN (BUILT_IN_LLFLOOR)
1686 CASE_MATHFN (BUILT_IN_LLRINT)
1687 CASE_MATHFN (BUILT_IN_LLROUND)
1688 CASE_MATHFN (BUILT_IN_LOG)
1689 CASE_MATHFN (BUILT_IN_LOG10)
1690 CASE_MATHFN (BUILT_IN_LOG1P)
1691 CASE_MATHFN (BUILT_IN_LOG2)
1692 CASE_MATHFN (BUILT_IN_LOGB)
1693 CASE_MATHFN (BUILT_IN_LRINT)
1694 CASE_MATHFN (BUILT_IN_LROUND)
1695 CASE_MATHFN (BUILT_IN_MODF)
1696 CASE_MATHFN (BUILT_IN_NAN)
1697 CASE_MATHFN (BUILT_IN_NANS)
1698 CASE_MATHFN (BUILT_IN_NEARBYINT)
1699 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1700 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1701 CASE_MATHFN (BUILT_IN_POW)
1702 CASE_MATHFN (BUILT_IN_POWI)
1703 CASE_MATHFN (BUILT_IN_POW10)
1704 CASE_MATHFN (BUILT_IN_REMAINDER)
1705 CASE_MATHFN (BUILT_IN_REMQUO)
1706 CASE_MATHFN (BUILT_IN_RINT)
1707 CASE_MATHFN (BUILT_IN_ROUND)
1708 CASE_MATHFN (BUILT_IN_SCALB)
1709 CASE_MATHFN (BUILT_IN_SCALBLN)
1710 CASE_MATHFN (BUILT_IN_SCALBN)
1711 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1712 CASE_MATHFN (BUILT_IN_SIN)
1713 CASE_MATHFN (BUILT_IN_SINCOS)
1714 CASE_MATHFN (BUILT_IN_SINH)
1715 CASE_MATHFN (BUILT_IN_SQRT)
1716 CASE_MATHFN (BUILT_IN_TAN)
1717 CASE_MATHFN (BUILT_IN_TANH)
1718 CASE_MATHFN (BUILT_IN_TGAMMA)
1719 CASE_MATHFN (BUILT_IN_TRUNC)
1720 CASE_MATHFN (BUILT_IN_Y0)
1721 CASE_MATHFN (BUILT_IN_Y1)
1722 CASE_MATHFN (BUILT_IN_YN)
1723
1724 default:
1725 return 0;
1726 }
1727
1728 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1729 return implicit_built_in_decls[fcode];
1730 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1731 return implicit_built_in_decls[fcodef];
1732 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1733 return implicit_built_in_decls[fcodel];
1734 else
1735 return 0;
1736 }
1737
1738 /* If errno must be maintained, expand the RTL to check if the result,
1739 TARGET, of a built-in function call, EXP, is NaN, and if so set
1740 errno to EDOM. */
1741
1742 static void
1743 expand_errno_check (tree exp, rtx target)
1744 {
1745 rtx lab = gen_label_rtx ();
1746
1747 /* Test the result; if it is NaN, set errno=EDOM because
1748 the argument was not in the domain. */
1749 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1750 0, lab);
1751
1752 #ifdef TARGET_EDOM
1753 /* If this built-in doesn't throw an exception, set errno directly. */
1754 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1755 {
1756 #ifdef GEN_ERRNO_RTX
1757 rtx errno_rtx = GEN_ERRNO_RTX;
1758 #else
1759 rtx errno_rtx
1760 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1761 #endif
1762 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1763 emit_label (lab);
1764 return;
1765 }
1766 #endif
1767
1768 /* We can't set errno=EDOM directly; let the library call do it.
1769 Pop the arguments right away in case the call gets deleted. */
1770 NO_DEFER_POP;
1771 expand_call (exp, target, 0);
1772 OK_DEFER_POP;
1773 emit_label (lab);
1774 }
1775
1776
1777 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1778 Return 0 if a normal call should be emitted rather than expanding the
1779 function in-line. EXP is the expression that is a call to the builtin
1780 function; if convenient, the result should be placed in TARGET.
1781 SUBTARGET may be used as the target for computing one of EXP's operands. */
1782
1783 static rtx
1784 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1785 {
1786 optab builtin_optab;
1787 rtx op0, insns, before_call;
1788 tree fndecl = get_callee_fndecl (exp);
1789 tree arglist = TREE_OPERAND (exp, 1);
1790 enum machine_mode mode;
1791 bool errno_set = false;
1792 tree arg, narg;
1793
1794 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1795 return 0;
1796
1797 arg = TREE_VALUE (arglist);
1798
1799 switch (DECL_FUNCTION_CODE (fndecl))
1800 {
1801 CASE_FLT_FN (BUILT_IN_SQRT):
1802 errno_set = ! tree_expr_nonnegative_p (arg);
1803 builtin_optab = sqrt_optab;
1804 break;
1805 CASE_FLT_FN (BUILT_IN_EXP):
1806 errno_set = true; builtin_optab = exp_optab; break;
1807 CASE_FLT_FN (BUILT_IN_EXP10):
1808 CASE_FLT_FN (BUILT_IN_POW10):
1809 errno_set = true; builtin_optab = exp10_optab; break;
1810 CASE_FLT_FN (BUILT_IN_EXP2):
1811 errno_set = true; builtin_optab = exp2_optab; break;
1812 CASE_FLT_FN (BUILT_IN_EXPM1):
1813 errno_set = true; builtin_optab = expm1_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOGB):
1815 errno_set = true; builtin_optab = logb_optab; break;
1816 CASE_FLT_FN (BUILT_IN_ILOGB):
1817 errno_set = true; builtin_optab = ilogb_optab; break;
1818 CASE_FLT_FN (BUILT_IN_LOG):
1819 errno_set = true; builtin_optab = log_optab; break;
1820 CASE_FLT_FN (BUILT_IN_LOG10):
1821 errno_set = true; builtin_optab = log10_optab; break;
1822 CASE_FLT_FN (BUILT_IN_LOG2):
1823 errno_set = true; builtin_optab = log2_optab; break;
1824 CASE_FLT_FN (BUILT_IN_LOG1P):
1825 errno_set = true; builtin_optab = log1p_optab; break;
1826 CASE_FLT_FN (BUILT_IN_ASIN):
1827 builtin_optab = asin_optab; break;
1828 CASE_FLT_FN (BUILT_IN_ACOS):
1829 builtin_optab = acos_optab; break;
1830 CASE_FLT_FN (BUILT_IN_TAN):
1831 builtin_optab = tan_optab; break;
1832 CASE_FLT_FN (BUILT_IN_ATAN):
1833 builtin_optab = atan_optab; break;
1834 CASE_FLT_FN (BUILT_IN_FLOOR):
1835 builtin_optab = floor_optab; break;
1836 CASE_FLT_FN (BUILT_IN_CEIL):
1837 builtin_optab = ceil_optab; break;
1838 CASE_FLT_FN (BUILT_IN_TRUNC):
1839 builtin_optab = btrunc_optab; break;
1840 CASE_FLT_FN (BUILT_IN_ROUND):
1841 builtin_optab = round_optab; break;
1842 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1843 builtin_optab = nearbyint_optab;
1844 if (flag_trapping_math)
1845 break;
1846 /* Else fallthrough and expand as rint. */
1847 CASE_FLT_FN (BUILT_IN_RINT):
1848 builtin_optab = rint_optab; break;
1849 default:
1850 gcc_unreachable ();
1851 }
1852
1853 /* Make a suitable register to place result in. */
1854 mode = TYPE_MODE (TREE_TYPE (exp));
1855
1856 if (! flag_errno_math || ! HONOR_NANS (mode))
1857 errno_set = false;
1858
1859 /* Before working hard, check whether the instruction is available. */
1860 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1861 {
1862 target = gen_reg_rtx (mode);
1863
1864 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1865 need to expand the argument again. This way, we will not perform
1866 side-effects more the once. */
1867 narg = builtin_save_expr (arg);
1868 if (narg != arg)
1869 {
1870 arg = narg;
1871 arglist = build_tree_list (NULL_TREE, arg);
1872 exp = build_function_call_expr (fndecl, arglist);
1873 }
1874
1875 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1876
1877 start_sequence ();
1878
1879 /* Compute into TARGET.
1880 Set TARGET to wherever the result comes back. */
1881 target = expand_unop (mode, builtin_optab, op0, target, 0);
1882
1883 if (target != 0)
1884 {
1885 if (errno_set)
1886 expand_errno_check (exp, target);
1887
1888 /* Output the entire sequence. */
1889 insns = get_insns ();
1890 end_sequence ();
1891 emit_insn (insns);
1892 return target;
1893 }
1894
1895 /* If we were unable to expand via the builtin, stop the sequence
1896 (without outputting the insns) and call to the library function
1897 with the stabilized argument list. */
1898 end_sequence ();
1899 }
1900
1901 before_call = get_last_insn ();
1902
1903 target = expand_call (exp, target, target == const0_rtx);
1904
1905 /* If this is a sqrt operation and we don't care about errno, try to
1906 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1907 This allows the semantics of the libcall to be visible to the RTL
1908 optimizers. */
1909 if (builtin_optab == sqrt_optab && !errno_set)
1910 {
1911 /* Search backwards through the insns emitted by expand_call looking
1912 for the instruction with the REG_RETVAL note. */
1913 rtx last = get_last_insn ();
1914 while (last != before_call)
1915 {
1916 if (find_reg_note (last, REG_RETVAL, NULL))
1917 {
1918 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1919 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1920 two elements, i.e. symbol_ref(sqrt) and the operand. */
1921 if (note
1922 && GET_CODE (note) == EXPR_LIST
1923 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1924 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1925 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1926 {
1927 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1928 /* Check operand is a register with expected mode. */
1929 if (operand
1930 && REG_P (operand)
1931 && GET_MODE (operand) == mode)
1932 {
1933 /* Replace the REG_EQUAL note with a SQRT rtx. */
1934 rtx equiv = gen_rtx_SQRT (mode, operand);
1935 set_unique_reg_note (last, REG_EQUAL, equiv);
1936 }
1937 }
1938 break;
1939 }
1940 last = PREV_INSN (last);
1941 }
1942 }
1943
1944 return target;
1945 }
1946
1947 /* Expand a call to the builtin binary math functions (pow and atan2).
1948 Return 0 if a normal call should be emitted rather than expanding the
1949 function in-line. EXP is the expression that is a call to the builtin
1950 function; if convenient, the result should be placed in TARGET.
1951 SUBTARGET may be used as the target for computing one of EXP's
1952 operands. */
1953
1954 static rtx
1955 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1956 {
1957 optab builtin_optab;
1958 rtx op0, op1, insns;
1959 int op1_type = REAL_TYPE;
1960 tree fndecl = get_callee_fndecl (exp);
1961 tree arglist = TREE_OPERAND (exp, 1);
1962 tree arg0, arg1, temp, narg;
1963 enum machine_mode mode;
1964 bool errno_set = true;
1965 bool stable = true;
1966
1967 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1968 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1969 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1970 op1_type = INTEGER_TYPE;
1971
1972 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1973 return 0;
1974
1975 arg0 = TREE_VALUE (arglist);
1976 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1977
1978 switch (DECL_FUNCTION_CODE (fndecl))
1979 {
1980 CASE_FLT_FN (BUILT_IN_POW):
1981 builtin_optab = pow_optab; break;
1982 CASE_FLT_FN (BUILT_IN_ATAN2):
1983 builtin_optab = atan2_optab; break;
1984 CASE_FLT_FN (BUILT_IN_LDEXP):
1985 builtin_optab = ldexp_optab; break;
1986 CASE_FLT_FN (BUILT_IN_FMOD):
1987 builtin_optab = fmod_optab; break;
1988 CASE_FLT_FN (BUILT_IN_REMAINDER):
1989 CASE_FLT_FN (BUILT_IN_DREM):
1990 builtin_optab = remainder_optab; break;
1991 default:
1992 gcc_unreachable ();
1993 }
1994
1995 /* Make a suitable register to place result in. */
1996 mode = TYPE_MODE (TREE_TYPE (exp));
1997
1998 /* Before working hard, check whether the instruction is available. */
1999 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2000 return 0;
2001
2002 target = gen_reg_rtx (mode);
2003
2004 if (! flag_errno_math || ! HONOR_NANS (mode))
2005 errno_set = false;
2006
2007 /* Always stabilize the argument list. */
2008 narg = builtin_save_expr (arg1);
2009 if (narg != arg1)
2010 {
2011 arg1 = narg;
2012 temp = build_tree_list (NULL_TREE, narg);
2013 stable = false;
2014 }
2015 else
2016 temp = TREE_CHAIN (arglist);
2017
2018 narg = builtin_save_expr (arg0);
2019 if (narg != arg0)
2020 {
2021 arg0 = narg;
2022 arglist = tree_cons (NULL_TREE, narg, temp);
2023 stable = false;
2024 }
2025 else if (! stable)
2026 arglist = tree_cons (NULL_TREE, arg0, temp);
2027
2028 if (! stable)
2029 exp = build_function_call_expr (fndecl, arglist);
2030
2031 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2032 op1 = expand_normal (arg1);
2033
2034 start_sequence ();
2035
2036 /* Compute into TARGET.
2037 Set TARGET to wherever the result comes back. */
2038 target = expand_binop (mode, builtin_optab, op0, op1,
2039 target, 0, OPTAB_DIRECT);
2040
2041 /* If we were unable to expand via the builtin, stop the sequence
2042 (without outputting the insns) and call to the library function
2043 with the stabilized argument list. */
2044 if (target == 0)
2045 {
2046 end_sequence ();
2047 return expand_call (exp, target, target == const0_rtx);
2048 }
2049
2050 if (errno_set)
2051 expand_errno_check (exp, target);
2052
2053 /* Output the entire sequence. */
2054 insns = get_insns ();
2055 end_sequence ();
2056 emit_insn (insns);
2057
2058 return target;
2059 }
2060
2061 /* Expand a call to the builtin sin and cos math functions.
2062 Return 0 if a normal call should be emitted rather than expanding the
2063 function in-line. EXP is the expression that is a call to the builtin
2064 function; if convenient, the result should be placed in TARGET.
2065 SUBTARGET may be used as the target for computing one of EXP's
2066 operands. */
2067
2068 static rtx
2069 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2070 {
2071 optab builtin_optab;
2072 rtx op0, insns;
2073 tree fndecl = get_callee_fndecl (exp);
2074 tree arglist = TREE_OPERAND (exp, 1);
2075 enum machine_mode mode;
2076 tree arg, narg;
2077
2078 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2079 return 0;
2080
2081 arg = TREE_VALUE (arglist);
2082
2083 switch (DECL_FUNCTION_CODE (fndecl))
2084 {
2085 CASE_FLT_FN (BUILT_IN_SIN):
2086 CASE_FLT_FN (BUILT_IN_COS):
2087 builtin_optab = sincos_optab; break;
2088 default:
2089 gcc_unreachable ();
2090 }
2091
2092 /* Make a suitable register to place result in. */
2093 mode = TYPE_MODE (TREE_TYPE (exp));
2094
2095 /* Check if sincos insn is available, otherwise fallback
2096 to sin or cos insn. */
2097 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2098 switch (DECL_FUNCTION_CODE (fndecl))
2099 {
2100 CASE_FLT_FN (BUILT_IN_SIN):
2101 builtin_optab = sin_optab; break;
2102 CASE_FLT_FN (BUILT_IN_COS):
2103 builtin_optab = cos_optab; break;
2104 default:
2105 gcc_unreachable ();
2106 }
2107
2108 /* Before working hard, check whether the instruction is available. */
2109 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2110 {
2111 target = gen_reg_rtx (mode);
2112
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 narg = save_expr (arg);
2117 if (narg != arg)
2118 {
2119 arg = narg;
2120 arglist = build_tree_list (NULL_TREE, arg);
2121 exp = build_function_call_expr (fndecl, arglist);
2122 }
2123
2124 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2125
2126 start_sequence ();
2127
2128 /* Compute into TARGET.
2129 Set TARGET to wherever the result comes back. */
2130 if (builtin_optab == sincos_optab)
2131 {
2132 int result;
2133
2134 switch (DECL_FUNCTION_CODE (fndecl))
2135 {
2136 CASE_FLT_FN (BUILT_IN_SIN):
2137 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2138 break;
2139 CASE_FLT_FN (BUILT_IN_COS):
2140 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2141 break;
2142 default:
2143 gcc_unreachable ();
2144 }
2145 gcc_assert (result);
2146 }
2147 else
2148 {
2149 target = expand_unop (mode, builtin_optab, op0, target, 0);
2150 }
2151
2152 if (target != 0)
2153 {
2154 /* Output the entire sequence. */
2155 insns = get_insns ();
2156 end_sequence ();
2157 emit_insn (insns);
2158 return target;
2159 }
2160
2161 /* If we were unable to expand via the builtin, stop the sequence
2162 (without outputting the insns) and call to the library function
2163 with the stabilized argument list. */
2164 end_sequence ();
2165 }
2166
2167 target = expand_call (exp, target, target == const0_rtx);
2168
2169 return target;
2170 }
2171
2172 /* Expand a call to the builtin sincos math function.
2173 Return 0 if a normal call should be emitted rather than expanding the
2174 function in-line. EXP is the expression that is a call to the builtin
2175 function. */
2176
2177 static rtx
2178 expand_builtin_sincos (tree exp)
2179 {
2180 rtx op0, op1, op2, target1, target2;
2181 tree arglist = TREE_OPERAND (exp, 1);
2182 enum machine_mode mode;
2183 tree arg, sinp, cosp;
2184 int result;
2185
2186 if (!validate_arglist (arglist, REAL_TYPE,
2187 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2188 return 0;
2189
2190 arg = TREE_VALUE (arglist);
2191 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2192 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2193
2194 /* Make a suitable register to place result in. */
2195 mode = TYPE_MODE (TREE_TYPE (arg));
2196
2197 /* Check if sincos insn is available, otherwise emit the call. */
2198 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2199 return NULL_RTX;
2200
2201 target1 = gen_reg_rtx (mode);
2202 target2 = gen_reg_rtx (mode);
2203
2204 op0 = expand_normal (arg);
2205 op1 = expand_normal (build_fold_indirect_ref (sinp));
2206 op2 = expand_normal (build_fold_indirect_ref (cosp));
2207
2208 /* Compute into target1 and target2.
2209 Set TARGET to wherever the result comes back. */
2210 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2211 gcc_assert (result);
2212
2213 /* Move target1 and target2 to the memory locations indicated
2214 by op1 and op2. */
2215 emit_move_insn (op1, target1);
2216 emit_move_insn (op2, target2);
2217
2218 return const0_rtx;
2219 }
2220
2221 /* Expand a call to the internal cexpi builtin to the sincos math function.
2222 EXP is the expression that is a call to the builtin function; if convenient,
2223 the result should be placed in TARGET. SUBTARGET may be used as the target
2224 for computing one of EXP's operands. */
2225
2226 static rtx
2227 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2228 {
2229 tree fndecl = get_callee_fndecl (exp);
2230 tree arglist = TREE_OPERAND (exp, 1);
2231 enum machine_mode mode;
2232 tree arg, type;
2233 rtx op0, op1, op2;
2234
2235 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2236 return 0;
2237
2238 arg = TREE_VALUE (arglist);
2239 type = TREE_TYPE (arg);
2240 mode = TYPE_MODE (TREE_TYPE (arg));
2241
2242 /* Try expanding via a sincos optab, fall back to emitting a libcall
2243 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2244 is only generated from sincos, cexp or if we have either of them. */
2245 if (sincos_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2246 {
2247 op1 = gen_reg_rtx (mode);
2248 op2 = gen_reg_rtx (mode);
2249
2250 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2251
2252 /* Compute into op1 and op2. */
2253 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2254 }
2255 else if (TARGET_HAS_SINCOS)
2256 {
2257 tree call, narglist, fn = NULL_TREE;
2258 tree top1, top2;
2259 rtx op1a, op2a;
2260
2261 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2262 fn = built_in_decls[BUILT_IN_SINCOSF];
2263 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2264 fn = built_in_decls[BUILT_IN_SINCOS];
2265 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2266 fn = built_in_decls[BUILT_IN_SINCOSL];
2267
2268 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2269 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2270 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2271 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2272 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2273 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2274
2275 narglist = build_tree_list (NULL_TREE, top2);
2276 narglist = tree_cons (NULL_TREE, top1, narglist);
2277 narglist = tree_cons (NULL_TREE, arg, narglist);
2278
2279 /* Make sure not to fold the sincos call again. */
2280 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2281 expand_normal (build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
2282 call, narglist, NULL_TREE));
2283 }
2284 else
2285 {
2286 tree call, fn = NULL_TREE, narg;
2287 tree ctype = build_complex_type (type);
2288
2289 /* We can expand via the C99 cexp function. */
2290 gcc_assert (TARGET_C99_FUNCTIONS);
2291
2292 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2293 fn = built_in_decls[BUILT_IN_CEXPF];
2294 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2295 fn = built_in_decls[BUILT_IN_CEXP];
2296 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2297 fn = built_in_decls[BUILT_IN_CEXPL];
2298 narg = fold_build2 (COMPLEX_EXPR, ctype,
2299 build_real (type, dconst0), arg);
2300
2301 /* Make sure not to fold the cexp call again. */
2302 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2303 return expand_expr (build3 (CALL_EXPR, ctype, call,
2304 build_tree_list (NULL_TREE, narg),
2305 NULL_TREE), target, VOIDmode, 0);
2306 }
2307
2308 /* Now build the proper return type. */
2309 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2310 make_tree (TREE_TYPE (arg), op2),
2311 make_tree (TREE_TYPE (arg), op1)),
2312 target, VOIDmode, 0);
2313 }
2314
2315 /* Expand a call to one of the builtin rounding functions gcc defines
2316 as an extension (lfloor and lceil). As these are gcc extensions we
2317 do not need to worry about setting errno to EDOM.
2318 If expanding via optab fails, lower expression to (int)(floor(x)).
2319 EXP is the expression that is a call to the builtin function;
2320 if convenient, the result should be placed in TARGET. SUBTARGET may
2321 be used as the target for computing one of EXP's operands. */
2322
2323 static rtx
2324 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2325 {
2326 convert_optab builtin_optab;
2327 rtx op0, insns, tmp;
2328 tree fndecl = get_callee_fndecl (exp);
2329 tree arglist = TREE_OPERAND (exp, 1);
2330 enum built_in_function fallback_fn;
2331 tree fallback_fndecl;
2332 enum machine_mode mode;
2333 tree arg, narg;
2334
2335 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2336 gcc_unreachable ();
2337
2338 arg = TREE_VALUE (arglist);
2339
2340 switch (DECL_FUNCTION_CODE (fndecl))
2341 {
2342 CASE_FLT_FN (BUILT_IN_LCEIL):
2343 CASE_FLT_FN (BUILT_IN_LLCEIL):
2344 builtin_optab = lceil_optab;
2345 fallback_fn = BUILT_IN_CEIL;
2346 break;
2347
2348 CASE_FLT_FN (BUILT_IN_LFLOOR):
2349 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2350 builtin_optab = lfloor_optab;
2351 fallback_fn = BUILT_IN_FLOOR;
2352 break;
2353
2354 default:
2355 gcc_unreachable ();
2356 }
2357
2358 /* Make a suitable register to place result in. */
2359 mode = TYPE_MODE (TREE_TYPE (exp));
2360
2361 target = gen_reg_rtx (mode);
2362
2363 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2364 need to expand the argument again. This way, we will not perform
2365 side-effects more the once. */
2366 narg = builtin_save_expr (arg);
2367 if (narg != arg)
2368 {
2369 arg = narg;
2370 arglist = build_tree_list (NULL_TREE, arg);
2371 exp = build_function_call_expr (fndecl, arglist);
2372 }
2373
2374 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2375
2376 start_sequence ();
2377
2378 /* Compute into TARGET. */
2379 if (expand_sfix_optab (target, op0, builtin_optab))
2380 {
2381 /* Output the entire sequence. */
2382 insns = get_insns ();
2383 end_sequence ();
2384 emit_insn (insns);
2385 return target;
2386 }
2387
2388 /* If we were unable to expand via the builtin, stop the sequence
2389 (without outputting the insns). */
2390 end_sequence ();
2391
2392 /* Fall back to floating point rounding optab. */
2393 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2394 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2395 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2396 gcc_assert (fallback_fndecl != NULL_TREE);
2397 exp = build_function_call_expr (fallback_fndecl, arglist);
2398
2399 tmp = expand_normal (exp);
2400
2401 /* Truncate the result of floating point optab to integer
2402 via expand_fix (). */
2403 target = gen_reg_rtx (mode);
2404 expand_fix (target, tmp, 0);
2405
2406 return target;
2407 }
2408
2409 /* Expand a call to one of the builtin math functions doing integer
2410 conversion (lrint).
2411 Return 0 if a normal call should be emitted rather than expanding the
2412 function in-line. EXP is the expression that is a call to the builtin
2413 function; if convenient, the result should be placed in TARGET.
2414 SUBTARGET may be used as the target for computing one of EXP's operands. */
2415
2416 static rtx
2417 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2418 {
2419 convert_optab builtin_optab;
2420 rtx op0, insns;
2421 tree fndecl = get_callee_fndecl (exp);
2422 tree arglist = TREE_OPERAND (exp, 1);
2423 enum machine_mode mode;
2424 tree arg, narg;
2425
2426 /* There's no easy way to detect the case we need to set EDOM. */
2427 if (flag_errno_math)
2428 return NULL_RTX;
2429
2430 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2431 return NULL_RTX;
2432
2433 arg = TREE_VALUE (arglist);
2434
2435 switch (DECL_FUNCTION_CODE (fndecl))
2436 {
2437 CASE_FLT_FN (BUILT_IN_LRINT):
2438 CASE_FLT_FN (BUILT_IN_LLRINT):
2439 builtin_optab = lrint_optab; break;
2440 CASE_FLT_FN (BUILT_IN_LROUND):
2441 CASE_FLT_FN (BUILT_IN_LLROUND):
2442 builtin_optab = lround_optab; break;
2443 default:
2444 gcc_unreachable ();
2445 }
2446
2447 /* Make a suitable register to place result in. */
2448 mode = TYPE_MODE (TREE_TYPE (exp));
2449
2450 target = gen_reg_rtx (mode);
2451
2452 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2453 need to expand the argument again. This way, we will not perform
2454 side-effects more the once. */
2455 narg = builtin_save_expr (arg);
2456 if (narg != arg)
2457 {
2458 arg = narg;
2459 arglist = build_tree_list (NULL_TREE, arg);
2460 exp = build_function_call_expr (fndecl, arglist);
2461 }
2462
2463 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2464
2465 start_sequence ();
2466
2467 if (expand_sfix_optab (target, op0, builtin_optab))
2468 {
2469 /* Output the entire sequence. */
2470 insns = get_insns ();
2471 end_sequence ();
2472 emit_insn (insns);
2473 return target;
2474 }
2475
2476 /* If we were unable to expand via the builtin, stop the sequence
2477 (without outputting the insns) and call to the library function
2478 with the stabilized argument list. */
2479 end_sequence ();
2480
2481 target = expand_call (exp, target, target == const0_rtx);
2482
2483 return target;
2484 }
2485
2486 /* To evaluate powi(x,n), the floating point value x raised to the
2487 constant integer exponent n, we use a hybrid algorithm that
2488 combines the "window method" with look-up tables. For an
2489 introduction to exponentiation algorithms and "addition chains",
2490 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2491 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2492 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2493 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2494
2495 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2496 multiplications to inline before calling the system library's pow
2497 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2498 so this default never requires calling pow, powf or powl. */
2499
2500 #ifndef POWI_MAX_MULTS
2501 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2502 #endif
2503
2504 /* The size of the "optimal power tree" lookup table. All
2505 exponents less than this value are simply looked up in the
2506 powi_table below. This threshold is also used to size the
2507 cache of pseudo registers that hold intermediate results. */
2508 #define POWI_TABLE_SIZE 256
2509
2510 /* The size, in bits of the window, used in the "window method"
2511 exponentiation algorithm. This is equivalent to a radix of
2512 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2513 #define POWI_WINDOW_SIZE 3
2514
2515 /* The following table is an efficient representation of an
2516 "optimal power tree". For each value, i, the corresponding
2517 value, j, in the table states than an optimal evaluation
2518 sequence for calculating pow(x,i) can be found by evaluating
2519 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2520 100 integers is given in Knuth's "Seminumerical algorithms". */
2521
2522 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2523 {
2524 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2525 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2526 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2527 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2528 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2529 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2530 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2531 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2532 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2533 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2534 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2535 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2536 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2537 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2538 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2539 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2540 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2541 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2542 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2543 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2544 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2545 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2546 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2547 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2548 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2549 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2550 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2551 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2552 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2553 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2554 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2555 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2556 };
2557
2558
2559 /* Return the number of multiplications required to calculate
2560 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2561 subroutine of powi_cost. CACHE is an array indicating
2562 which exponents have already been calculated. */
2563
2564 static int
2565 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2566 {
2567 /* If we've already calculated this exponent, then this evaluation
2568 doesn't require any additional multiplications. */
2569 if (cache[n])
2570 return 0;
2571
2572 cache[n] = true;
2573 return powi_lookup_cost (n - powi_table[n], cache)
2574 + powi_lookup_cost (powi_table[n], cache) + 1;
2575 }
2576
2577 /* Return the number of multiplications required to calculate
2578 powi(x,n) for an arbitrary x, given the exponent N. This
2579 function needs to be kept in sync with expand_powi below. */
2580
2581 static int
2582 powi_cost (HOST_WIDE_INT n)
2583 {
2584 bool cache[POWI_TABLE_SIZE];
2585 unsigned HOST_WIDE_INT digit;
2586 unsigned HOST_WIDE_INT val;
2587 int result;
2588
2589 if (n == 0)
2590 return 0;
2591
2592 /* Ignore the reciprocal when calculating the cost. */
2593 val = (n < 0) ? -n : n;
2594
2595 /* Initialize the exponent cache. */
2596 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2597 cache[1] = true;
2598
2599 result = 0;
2600
2601 while (val >= POWI_TABLE_SIZE)
2602 {
2603 if (val & 1)
2604 {
2605 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2606 result += powi_lookup_cost (digit, cache)
2607 + POWI_WINDOW_SIZE + 1;
2608 val >>= POWI_WINDOW_SIZE;
2609 }
2610 else
2611 {
2612 val >>= 1;
2613 result++;
2614 }
2615 }
2616
2617 return result + powi_lookup_cost (val, cache);
2618 }
2619
2620 /* Recursive subroutine of expand_powi. This function takes the array,
2621 CACHE, of already calculated exponents and an exponent N and returns
2622 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2623
2624 static rtx
2625 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2626 {
2627 unsigned HOST_WIDE_INT digit;
2628 rtx target, result;
2629 rtx op0, op1;
2630
2631 if (n < POWI_TABLE_SIZE)
2632 {
2633 if (cache[n])
2634 return cache[n];
2635
2636 target = gen_reg_rtx (mode);
2637 cache[n] = target;
2638
2639 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2640 op1 = expand_powi_1 (mode, powi_table[n], cache);
2641 }
2642 else if (n & 1)
2643 {
2644 target = gen_reg_rtx (mode);
2645 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2646 op0 = expand_powi_1 (mode, n - digit, cache);
2647 op1 = expand_powi_1 (mode, digit, cache);
2648 }
2649 else
2650 {
2651 target = gen_reg_rtx (mode);
2652 op0 = expand_powi_1 (mode, n >> 1, cache);
2653 op1 = op0;
2654 }
2655
2656 result = expand_mult (mode, op0, op1, target, 0);
2657 if (result != target)
2658 emit_move_insn (target, result);
2659 return target;
2660 }
2661
2662 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2663 floating point operand in mode MODE, and N is the exponent. This
2664 function needs to be kept in sync with powi_cost above. */
2665
2666 static rtx
2667 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2668 {
2669 unsigned HOST_WIDE_INT val;
2670 rtx cache[POWI_TABLE_SIZE];
2671 rtx result;
2672
2673 if (n == 0)
2674 return CONST1_RTX (mode);
2675
2676 val = (n < 0) ? -n : n;
2677
2678 memset (cache, 0, sizeof (cache));
2679 cache[1] = x;
2680
2681 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2682
2683 /* If the original exponent was negative, reciprocate the result. */
2684 if (n < 0)
2685 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2686 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2687
2688 return result;
2689 }
2690
2691 /* Expand a call to the pow built-in mathematical function. Return 0 if
2692 a normal call should be emitted rather than expanding the function
2693 in-line. EXP is the expression that is a call to the builtin
2694 function; if convenient, the result should be placed in TARGET. */
2695
2696 static rtx
2697 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2698 {
2699 tree arg0, arg1, fn, narg0, narglist;
2700 tree arglist = TREE_OPERAND (exp, 1);
2701 tree type = TREE_TYPE (exp);
2702 REAL_VALUE_TYPE cint, c, c2;
2703 HOST_WIDE_INT n;
2704 rtx op, op2;
2705 enum machine_mode mode = TYPE_MODE (type);
2706
2707 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2708 return 0;
2709
2710 arg0 = TREE_VALUE (arglist);
2711 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2712
2713 if (TREE_CODE (arg1) != REAL_CST
2714 || TREE_OVERFLOW (arg1))
2715 return expand_builtin_mathfn_2 (exp, target, subtarget);
2716
2717 /* Handle constant exponents. */
2718
2719 /* For integer valued exponents we can expand to an optimal multiplication
2720 sequence using expand_powi. */
2721 c = TREE_REAL_CST (arg1);
2722 n = real_to_integer (&c);
2723 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2724 if (real_identical (&c, &cint)
2725 && ((n >= -1 && n <= 2)
2726 || (flag_unsafe_math_optimizations
2727 && !optimize_size
2728 && powi_cost (n) <= POWI_MAX_MULTS)))
2729 {
2730 op = expand_expr (arg0, subtarget, VOIDmode, 0);
2731 if (n != 1)
2732 {
2733 op = force_reg (mode, op);
2734 op = expand_powi (op, mode, n);
2735 }
2736 return op;
2737 }
2738
2739 narg0 = builtin_save_expr (arg0);
2740 narglist = build_tree_list (NULL_TREE, narg0);
2741
2742 /* If the exponent is not integer valued, check if it is half of an integer.
2743 In this case we can expand to sqrt (x) * x**(n/2). */
2744 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2745 if (fn != NULL_TREE)
2746 {
2747 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2748 n = real_to_integer (&c2);
2749 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2750 if (real_identical (&c2, &cint)
2751 && ((flag_unsafe_math_optimizations
2752 && !optimize_size
2753 && powi_cost (n/2) <= POWI_MAX_MULTS)
2754 || n == 1))
2755 {
2756 tree call_expr = build_function_call_expr (fn, narglist);
2757 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2758 if (n != 1)
2759 {
2760 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2761 op2 = force_reg (mode, op2);
2762 op2 = expand_powi (op2, mode, abs (n / 2));
2763 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2764 0, OPTAB_LIB_WIDEN);
2765 /* If the original exponent was negative, reciprocate the
2766 result. */
2767 if (n < 0)
2768 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2769 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2770 }
2771 return op;
2772 }
2773 }
2774
2775 /* Try if the exponent is a third of an integer. In this case
2776 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2777 different from pow (x, 1./3.) due to rounding and behavior
2778 with negative x we need to constrain this transformation to
2779 unsafe math and positive x or finite math. */
2780 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2781 if (fn != NULL_TREE
2782 && flag_unsafe_math_optimizations
2783 && (tree_expr_nonnegative_p (arg0)
2784 || !HONOR_NANS (mode)))
2785 {
2786 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2787 real_round (&c2, mode, &c2);
2788 n = real_to_integer (&c2);
2789 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2790 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2791 real_convert (&c2, mode, &c2);
2792 if (real_identical (&c2, &c)
2793 && ((!optimize_size
2794 && powi_cost (n/3) <= POWI_MAX_MULTS)
2795 || n == 1))
2796 {
2797 tree call_expr = build_function_call_expr (fn, narglist);
2798 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2799 if (abs (n) % 3 == 2)
2800 op = expand_simple_binop (mode, MULT, op, op, op,
2801 0, OPTAB_LIB_WIDEN);
2802 if (n != 1)
2803 {
2804 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2805 op2 = force_reg (mode, op2);
2806 op2 = expand_powi (op2, mode, abs (n / 3));
2807 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2808 0, OPTAB_LIB_WIDEN);
2809 /* If the original exponent was negative, reciprocate the
2810 result. */
2811 if (n < 0)
2812 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2813 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2814 }
2815 return op;
2816 }
2817 }
2818
2819 /* Fall back to optab expansion. */
2820 return expand_builtin_mathfn_2 (exp, target, subtarget);
2821 }
2822
2823 /* Expand a call to the powi built-in mathematical function. Return 0 if
2824 a normal call should be emitted rather than expanding the function
2825 in-line. EXP is the expression that is a call to the builtin
2826 function; if convenient, the result should be placed in TARGET. */
2827
2828 static rtx
2829 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2830 {
2831 tree arglist = TREE_OPERAND (exp, 1);
2832 tree arg0, arg1;
2833 rtx op0, op1;
2834 enum machine_mode mode;
2835 enum machine_mode mode2;
2836
2837 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2838 return 0;
2839
2840 arg0 = TREE_VALUE (arglist);
2841 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2842 mode = TYPE_MODE (TREE_TYPE (exp));
2843
2844 /* Handle constant power. */
2845
2846 if (TREE_CODE (arg1) == INTEGER_CST
2847 && !TREE_OVERFLOW (arg1))
2848 {
2849 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2850
2851 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2852 Otherwise, check the number of multiplications required. */
2853 if ((TREE_INT_CST_HIGH (arg1) == 0
2854 || TREE_INT_CST_HIGH (arg1) == -1)
2855 && ((n >= -1 && n <= 2)
2856 || (! optimize_size
2857 && powi_cost (n) <= POWI_MAX_MULTS)))
2858 {
2859 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2860 op0 = force_reg (mode, op0);
2861 return expand_powi (op0, mode, n);
2862 }
2863 }
2864
2865 /* Emit a libcall to libgcc. */
2866
2867 /* Mode of the 2nd argument must match that of an int. */
2868 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2869
2870 if (target == NULL_RTX)
2871 target = gen_reg_rtx (mode);
2872
2873 op0 = expand_expr (arg0, subtarget, mode, 0);
2874 if (GET_MODE (op0) != mode)
2875 op0 = convert_to_mode (mode, op0, 0);
2876 op1 = expand_expr (arg1, 0, mode2, 0);
2877 if (GET_MODE (op1) != mode2)
2878 op1 = convert_to_mode (mode2, op1, 0);
2879
2880 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2881 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2882 op0, mode, op1, mode2);
2883
2884 return target;
2885 }
2886
2887 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2888 if we failed the caller should emit a normal call, otherwise
2889 try to get the result in TARGET, if convenient. */
2890
2891 static rtx
2892 expand_builtin_strlen (tree arglist, rtx target,
2893 enum machine_mode target_mode)
2894 {
2895 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2896 return 0;
2897 else
2898 {
2899 rtx pat;
2900 tree len, src = TREE_VALUE (arglist);
2901 rtx result, src_reg, char_rtx, before_strlen;
2902 enum machine_mode insn_mode = target_mode, char_mode;
2903 enum insn_code icode = CODE_FOR_nothing;
2904 int align;
2905
2906 /* If the length can be computed at compile-time, return it. */
2907 len = c_strlen (src, 0);
2908 if (len)
2909 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2910
2911 /* If the length can be computed at compile-time and is constant
2912 integer, but there are side-effects in src, evaluate
2913 src for side-effects, then return len.
2914 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2915 can be optimized into: i++; x = 3; */
2916 len = c_strlen (src, 1);
2917 if (len && TREE_CODE (len) == INTEGER_CST)
2918 {
2919 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2920 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2921 }
2922
2923 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2924
2925 /* If SRC is not a pointer type, don't do this operation inline. */
2926 if (align == 0)
2927 return 0;
2928
2929 /* Bail out if we can't compute strlen in the right mode. */
2930 while (insn_mode != VOIDmode)
2931 {
2932 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2933 if (icode != CODE_FOR_nothing)
2934 break;
2935
2936 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2937 }
2938 if (insn_mode == VOIDmode)
2939 return 0;
2940
2941 /* Make a place to write the result of the instruction. */
2942 result = target;
2943 if (! (result != 0
2944 && REG_P (result)
2945 && GET_MODE (result) == insn_mode
2946 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2947 result = gen_reg_rtx (insn_mode);
2948
2949 /* Make a place to hold the source address. We will not expand
2950 the actual source until we are sure that the expansion will
2951 not fail -- there are trees that cannot be expanded twice. */
2952 src_reg = gen_reg_rtx (Pmode);
2953
2954 /* Mark the beginning of the strlen sequence so we can emit the
2955 source operand later. */
2956 before_strlen = get_last_insn ();
2957
2958 char_rtx = const0_rtx;
2959 char_mode = insn_data[(int) icode].operand[2].mode;
2960 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2961 char_mode))
2962 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2963
2964 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2965 char_rtx, GEN_INT (align));
2966 if (! pat)
2967 return 0;
2968 emit_insn (pat);
2969
2970 /* Now that we are assured of success, expand the source. */
2971 start_sequence ();
2972 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2973 if (pat != src_reg)
2974 emit_move_insn (src_reg, pat);
2975 pat = get_insns ();
2976 end_sequence ();
2977
2978 if (before_strlen)
2979 emit_insn_after (pat, before_strlen);
2980 else
2981 emit_insn_before (pat, get_insns ());
2982
2983 /* Return the value in the proper mode for this function. */
2984 if (GET_MODE (result) == target_mode)
2985 target = result;
2986 else if (target != 0)
2987 convert_move (target, result, 0);
2988 else
2989 target = convert_to_mode (target_mode, result, 0);
2990
2991 return target;
2992 }
2993 }
2994
2995 /* Expand a call to the strstr builtin. Return 0 if we failed the
2996 caller should emit a normal call, otherwise try to get the result
2997 in TARGET, if convenient (and in mode MODE if that's convenient). */
2998
2999 static rtx
3000 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
3001 {
3002 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3003 {
3004 tree result = fold_builtin_strstr (arglist, type);
3005 if (result)
3006 return expand_expr (result, target, mode, EXPAND_NORMAL);
3007 }
3008 return 0;
3009 }
3010
3011 /* Expand a call to the strchr builtin. Return 0 if we failed the
3012 caller should emit a normal call, otherwise try to get the result
3013 in TARGET, if convenient (and in mode MODE if that's convenient). */
3014
3015 static rtx
3016 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3017 {
3018 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3019 {
3020 tree result = fold_builtin_strchr (arglist, type);
3021 if (result)
3022 return expand_expr (result, target, mode, EXPAND_NORMAL);
3023
3024 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3025 }
3026 return 0;
3027 }
3028
3029 /* Expand a call to the strrchr builtin. Return 0 if we failed the
3030 caller should emit a normal call, otherwise try to get the result
3031 in TARGET, if convenient (and in mode MODE if that's convenient). */
3032
3033 static rtx
3034 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3035 {
3036 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3037 {
3038 tree result = fold_builtin_strrchr (arglist, type);
3039 if (result)
3040 return expand_expr (result, target, mode, EXPAND_NORMAL);
3041 }
3042 return 0;
3043 }
3044
3045 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
3046 caller should emit a normal call, otherwise try to get the result
3047 in TARGET, if convenient (and in mode MODE if that's convenient). */
3048
3049 static rtx
3050 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
3051 {
3052 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3053 {
3054 tree result = fold_builtin_strpbrk (arglist, type);
3055 if (result)
3056 return expand_expr (result, target, mode, EXPAND_NORMAL);
3057 }
3058 return 0;
3059 }
3060
3061 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3062 bytes from constant string DATA + OFFSET and return it as target
3063 constant. */
3064
3065 static rtx
3066 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3067 enum machine_mode mode)
3068 {
3069 const char *str = (const char *) data;
3070
3071 gcc_assert (offset >= 0
3072 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3073 <= strlen (str) + 1));
3074
3075 return c_readstr (str + offset, mode);
3076 }
3077
3078 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
3079 Return 0 if we failed, the caller should emit a normal call,
3080 otherwise try to get the result in TARGET, if convenient (and in
3081 mode MODE if that's convenient). */
3082 static rtx
3083 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3084 {
3085 tree fndecl = get_callee_fndecl (exp);
3086 tree arglist = TREE_OPERAND (exp, 1);
3087 if (!validate_arglist (arglist,
3088 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3089 return 0;
3090 else
3091 {
3092 tree dest = TREE_VALUE (arglist);
3093 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3094 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3095 const char *src_str;
3096 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3097 unsigned int dest_align
3098 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3099 rtx dest_mem, src_mem, dest_addr, len_rtx;
3100 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
3101 false, /*endp=*/0);
3102
3103 if (result)
3104 {
3105 while (TREE_CODE (result) == COMPOUND_EXPR)
3106 {
3107 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3108 EXPAND_NORMAL);
3109 result = TREE_OPERAND (result, 1);
3110 }
3111 return expand_expr (result, target, mode, EXPAND_NORMAL);
3112 }
3113
3114 /* If DEST is not a pointer type, call the normal function. */
3115 if (dest_align == 0)
3116 return 0;
3117
3118 /* If either SRC is not a pointer type, don't do this
3119 operation in-line. */
3120 if (src_align == 0)
3121 return 0;
3122
3123 dest_mem = get_memory_rtx (dest, len);
3124 set_mem_align (dest_mem, dest_align);
3125 len_rtx = expand_normal (len);
3126 src_str = c_getstr (src);
3127
3128 /* If SRC is a string constant and block move would be done
3129 by pieces, we can avoid loading the string from memory
3130 and only stored the computed constants. */
3131 if (src_str
3132 && GET_CODE (len_rtx) == CONST_INT
3133 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3134 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3135 (void *) src_str, dest_align))
3136 {
3137 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3138 builtin_memcpy_read_str,
3139 (void *) src_str, dest_align, 0);
3140 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3141 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3142 return dest_mem;
3143 }
3144
3145 src_mem = get_memory_rtx (src, len);
3146 set_mem_align (src_mem, src_align);
3147
3148 /* Copy word part most expediently. */
3149 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
3150 CALL_EXPR_TAILCALL (exp)
3151 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3152
3153 if (dest_addr == 0)
3154 {
3155 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3156 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3157 }
3158 return dest_addr;
3159 }
3160 }
3161
3162 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
3163 Return 0 if we failed; the caller should emit a normal call,
3164 otherwise try to get the result in TARGET, if convenient (and in
3165 mode MODE if that's convenient). If ENDP is 0 return the
3166 destination pointer, if ENDP is 1 return the end pointer ala
3167 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3168 stpcpy. */
3169
3170 static rtx
3171 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
3172 int endp)
3173 {
3174 if (!validate_arglist (arglist,
3175 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3176 return 0;
3177 /* If return value is ignored, transform mempcpy into memcpy. */
3178 else if (target == const0_rtx)
3179 {
3180 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3181
3182 if (!fn)
3183 return 0;
3184
3185 return expand_expr (build_function_call_expr (fn, arglist),
3186 target, mode, EXPAND_NORMAL);
3187 }
3188 else
3189 {
3190 tree dest = TREE_VALUE (arglist);
3191 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3192 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3193 const char *src_str;
3194 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3195 unsigned int dest_align
3196 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3197 rtx dest_mem, src_mem, len_rtx;
3198 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3199
3200 if (result)
3201 {
3202 while (TREE_CODE (result) == COMPOUND_EXPR)
3203 {
3204 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3205 EXPAND_NORMAL);
3206 result = TREE_OPERAND (result, 1);
3207 }
3208 return expand_expr (result, target, mode, EXPAND_NORMAL);
3209 }
3210
3211 /* If either SRC or DEST is not a pointer type, don't do this
3212 operation in-line. */
3213 if (dest_align == 0 || src_align == 0)
3214 return 0;
3215
3216 /* If LEN is not constant, call the normal function. */
3217 if (! host_integerp (len, 1))
3218 return 0;
3219
3220 len_rtx = expand_normal (len);
3221 src_str = c_getstr (src);
3222
3223 /* If SRC is a string constant and block move would be done
3224 by pieces, we can avoid loading the string from memory
3225 and only stored the computed constants. */
3226 if (src_str
3227 && GET_CODE (len_rtx) == CONST_INT
3228 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3229 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3230 (void *) src_str, dest_align))
3231 {
3232 dest_mem = get_memory_rtx (dest, len);
3233 set_mem_align (dest_mem, dest_align);
3234 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3235 builtin_memcpy_read_str,
3236 (void *) src_str, dest_align, endp);
3237 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3238 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3239 return dest_mem;
3240 }
3241
3242 if (GET_CODE (len_rtx) == CONST_INT
3243 && can_move_by_pieces (INTVAL (len_rtx),
3244 MIN (dest_align, src_align)))
3245 {
3246 dest_mem = get_memory_rtx (dest, len);
3247 set_mem_align (dest_mem, dest_align);
3248 src_mem = get_memory_rtx (src, len);
3249 set_mem_align (src_mem, src_align);
3250 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3251 MIN (dest_align, src_align), endp);
3252 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3253 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3254 return dest_mem;
3255 }
3256
3257 return 0;
3258 }
3259 }
3260
3261 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3262 if we failed; the caller should emit a normal call. */
3263
3264 static rtx
3265 expand_builtin_memmove (tree arglist, tree type, rtx target,
3266 enum machine_mode mode)
3267 {
3268 if (!validate_arglist (arglist,
3269 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3270 return 0;
3271 else
3272 {
3273 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3274
3275 if (result)
3276 {
3277 while (TREE_CODE (result) == COMPOUND_EXPR)
3278 {
3279 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3280 EXPAND_NORMAL);
3281 result = TREE_OPERAND (result, 1);
3282 }
3283 return expand_expr (result, target, mode, EXPAND_NORMAL);
3284 }
3285
3286 /* Otherwise, call the normal function. */
3287 return 0;
3288 }
3289 }
3290
3291 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3292 if we failed the caller should emit a normal call. */
3293
3294 static rtx
3295 expand_builtin_bcopy (tree exp)
3296 {
3297 tree arglist = TREE_OPERAND (exp, 1);
3298 tree type = TREE_TYPE (exp);
3299 tree src, dest, size, newarglist;
3300
3301 if (!validate_arglist (arglist,
3302 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3303 return NULL_RTX;
3304
3305 src = TREE_VALUE (arglist);
3306 dest = TREE_VALUE (TREE_CHAIN (arglist));
3307 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3308
3309 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3310 memmove(ptr y, ptr x, size_t z). This is done this way
3311 so that if it isn't expanded inline, we fallback to
3312 calling bcopy instead of memmove. */
3313
3314 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3315 newarglist = tree_cons (NULL_TREE, src, newarglist);
3316 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3317
3318 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3319 }
3320
3321 #ifndef HAVE_movstr
3322 # define HAVE_movstr 0
3323 # define CODE_FOR_movstr CODE_FOR_nothing
3324 #endif
3325
3326 /* Expand into a movstr instruction, if one is available. Return 0 if
3327 we failed, the caller should emit a normal call, otherwise try to
3328 get the result in TARGET, if convenient. If ENDP is 0 return the
3329 destination pointer, if ENDP is 1 return the end pointer ala
3330 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3331 stpcpy. */
3332
3333 static rtx
3334 expand_movstr (tree dest, tree src, rtx target, int endp)
3335 {
3336 rtx end;
3337 rtx dest_mem;
3338 rtx src_mem;
3339 rtx insn;
3340 const struct insn_data * data;
3341
3342 if (!HAVE_movstr)
3343 return 0;
3344
3345 dest_mem = get_memory_rtx (dest, NULL);
3346 src_mem = get_memory_rtx (src, NULL);
3347 if (!endp)
3348 {
3349 target = force_reg (Pmode, XEXP (dest_mem, 0));
3350 dest_mem = replace_equiv_address (dest_mem, target);
3351 end = gen_reg_rtx (Pmode);
3352 }
3353 else
3354 {
3355 if (target == 0 || target == const0_rtx)
3356 {
3357 end = gen_reg_rtx (Pmode);
3358 if (target == 0)
3359 target = end;
3360 }
3361 else
3362 end = target;
3363 }
3364
3365 data = insn_data + CODE_FOR_movstr;
3366
3367 if (data->operand[0].mode != VOIDmode)
3368 end = gen_lowpart (data->operand[0].mode, end);
3369
3370 insn = data->genfun (end, dest_mem, src_mem);
3371
3372 gcc_assert (insn);
3373
3374 emit_insn (insn);
3375
3376 /* movstr is supposed to set end to the address of the NUL
3377 terminator. If the caller requested a mempcpy-like return value,
3378 adjust it. */
3379 if (endp == 1 && target != const0_rtx)
3380 {
3381 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3382 emit_move_insn (target, force_operand (tem, NULL_RTX));
3383 }
3384
3385 return target;
3386 }
3387
3388 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3389 if we failed the caller should emit a normal call, otherwise try to get
3390 the result in TARGET, if convenient (and in mode MODE if that's
3391 convenient). */
3392
3393 static rtx
3394 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3395 {
3396 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3397 {
3398 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3399 if (result)
3400 {
3401 while (TREE_CODE (result) == COMPOUND_EXPR)
3402 {
3403 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3404 EXPAND_NORMAL);
3405 result = TREE_OPERAND (result, 1);
3406 }
3407 return expand_expr (result, target, mode, EXPAND_NORMAL);
3408 }
3409
3410 return expand_movstr (TREE_VALUE (arglist),
3411 TREE_VALUE (TREE_CHAIN (arglist)),
3412 target, /*endp=*/0);
3413 }
3414 return 0;
3415 }
3416
3417 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3418 Return 0 if we failed the caller should emit a normal call,
3419 otherwise try to get the result in TARGET, if convenient (and in
3420 mode MODE if that's convenient). */
3421
3422 static rtx
3423 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3424 {
3425 tree arglist = TREE_OPERAND (exp, 1);
3426 /* If return value is ignored, transform stpcpy into strcpy. */
3427 if (target == const0_rtx)
3428 {
3429 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3430 if (!fn)
3431 return 0;
3432
3433 return expand_expr (build_function_call_expr (fn, arglist),
3434 target, mode, EXPAND_NORMAL);
3435 }
3436
3437 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3438 return 0;
3439 else
3440 {
3441 tree dst, src, len, lenp1;
3442 tree narglist;
3443 rtx ret;
3444
3445 /* Ensure we get an actual string whose length can be evaluated at
3446 compile-time, not an expression containing a string. This is
3447 because the latter will potentially produce pessimized code
3448 when used to produce the return value. */
3449 src = TREE_VALUE (TREE_CHAIN (arglist));
3450 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3451 return expand_movstr (TREE_VALUE (arglist),
3452 TREE_VALUE (TREE_CHAIN (arglist)),
3453 target, /*endp=*/2);
3454
3455 dst = TREE_VALUE (arglist);
3456 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3457 narglist = build_tree_list (NULL_TREE, lenp1);
3458 narglist = tree_cons (NULL_TREE, src, narglist);
3459 narglist = tree_cons (NULL_TREE, dst, narglist);
3460 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3461 target, mode, /*endp=*/2);
3462
3463 if (ret)
3464 return ret;
3465
3466 if (TREE_CODE (len) == INTEGER_CST)
3467 {
3468 rtx len_rtx = expand_normal (len);
3469
3470 if (GET_CODE (len_rtx) == CONST_INT)
3471 {
3472 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3473 arglist, target, mode);
3474
3475 if (ret)
3476 {
3477 if (! target)
3478 {
3479 if (mode != VOIDmode)
3480 target = gen_reg_rtx (mode);
3481 else
3482 target = gen_reg_rtx (GET_MODE (ret));
3483 }
3484 if (GET_MODE (target) != GET_MODE (ret))
3485 ret = gen_lowpart (GET_MODE (target), ret);
3486
3487 ret = plus_constant (ret, INTVAL (len_rtx));
3488 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3489 gcc_assert (ret);
3490
3491 return target;
3492 }
3493 }
3494 }
3495
3496 return expand_movstr (TREE_VALUE (arglist),
3497 TREE_VALUE (TREE_CHAIN (arglist)),
3498 target, /*endp=*/2);
3499 }
3500 }
3501
3502 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3503 bytes from constant string DATA + OFFSET and return it as target
3504 constant. */
3505
3506 static rtx
3507 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3508 enum machine_mode mode)
3509 {
3510 const char *str = (const char *) data;
3511
3512 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3513 return const0_rtx;
3514
3515 return c_readstr (str + offset, mode);
3516 }
3517
3518 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3519 if we failed the caller should emit a normal call. */
3520
3521 static rtx
3522 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3523 {
3524 tree fndecl = get_callee_fndecl (exp);
3525 tree arglist = TREE_OPERAND (exp, 1);
3526 if (validate_arglist (arglist,
3527 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3528 {
3529 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3530 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3531 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3532
3533 if (result)
3534 {
3535 while (TREE_CODE (result) == COMPOUND_EXPR)
3536 {
3537 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3538 EXPAND_NORMAL);
3539 result = TREE_OPERAND (result, 1);
3540 }
3541 return expand_expr (result, target, mode, EXPAND_NORMAL);
3542 }
3543
3544 /* We must be passed a constant len and src parameter. */
3545 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3546 return 0;
3547
3548 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3549
3550 /* We're required to pad with trailing zeros if the requested
3551 len is greater than strlen(s2)+1. In that case try to
3552 use store_by_pieces, if it fails, punt. */
3553 if (tree_int_cst_lt (slen, len))
3554 {
3555 tree dest = TREE_VALUE (arglist);
3556 unsigned int dest_align
3557 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3558 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3559 rtx dest_mem;
3560
3561 if (!p || dest_align == 0 || !host_integerp (len, 1)
3562 || !can_store_by_pieces (tree_low_cst (len, 1),
3563 builtin_strncpy_read_str,
3564 (void *) p, dest_align))
3565 return 0;
3566
3567 dest_mem = get_memory_rtx (dest, len);
3568 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3569 builtin_strncpy_read_str,
3570 (void *) p, dest_align, 0);
3571 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3572 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3573 return dest_mem;
3574 }
3575 }
3576 return 0;
3577 }
3578
3579 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3580 bytes from constant string DATA + OFFSET and return it as target
3581 constant. */
3582
3583 rtx
3584 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3585 enum machine_mode mode)
3586 {
3587 const char *c = (const char *) data;
3588 char *p = alloca (GET_MODE_SIZE (mode));
3589
3590 memset (p, *c, GET_MODE_SIZE (mode));
3591
3592 return c_readstr (p, mode);
3593 }
3594
3595 /* Callback routine for store_by_pieces. Return the RTL of a register
3596 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3597 char value given in the RTL register data. For example, if mode is
3598 4 bytes wide, return the RTL for 0x01010101*data. */
3599
3600 static rtx
3601 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3602 enum machine_mode mode)
3603 {
3604 rtx target, coeff;
3605 size_t size;
3606 char *p;
3607
3608 size = GET_MODE_SIZE (mode);
3609 if (size == 1)
3610 return (rtx) data;
3611
3612 p = alloca (size);
3613 memset (p, 1, size);
3614 coeff = c_readstr (p, mode);
3615
3616 target = convert_to_mode (mode, (rtx) data, 1);
3617 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3618 return force_reg (mode, target);
3619 }
3620
3621 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3622 if we failed the caller should emit a normal call, otherwise try to get
3623 the result in TARGET, if convenient (and in mode MODE if that's
3624 convenient). */
3625
3626 static rtx
3627 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3628 tree orig_exp)
3629 {
3630 if (!validate_arglist (arglist,
3631 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3632 return 0;
3633 else
3634 {
3635 tree dest = TREE_VALUE (arglist);
3636 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3637 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3638 tree fndecl, fn;
3639 enum built_in_function fcode;
3640 char c;
3641 unsigned int dest_align;
3642 rtx dest_mem, dest_addr, len_rtx;
3643
3644 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3645
3646 /* If DEST is not a pointer type, don't do this
3647 operation in-line. */
3648 if (dest_align == 0)
3649 return 0;
3650
3651 /* If the LEN parameter is zero, return DEST. */
3652 if (integer_zerop (len))
3653 {
3654 /* Evaluate and ignore VAL in case it has side-effects. */
3655 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3656 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3657 }
3658
3659 /* Stabilize the arguments in case we fail. */
3660 dest = builtin_save_expr (dest);
3661 val = builtin_save_expr (val);
3662 len = builtin_save_expr (len);
3663
3664 len_rtx = expand_normal (len);
3665 dest_mem = get_memory_rtx (dest, len);
3666
3667 if (TREE_CODE (val) != INTEGER_CST)
3668 {
3669 rtx val_rtx;
3670
3671 val_rtx = expand_normal (val);
3672 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3673 val_rtx, 0);
3674
3675 /* Assume that we can memset by pieces if we can store the
3676 * the coefficients by pieces (in the required modes).
3677 * We can't pass builtin_memset_gen_str as that emits RTL. */
3678 c = 1;
3679 if (host_integerp (len, 1)
3680 && !(optimize_size && tree_low_cst (len, 1) > 1)
3681 && can_store_by_pieces (tree_low_cst (len, 1),
3682 builtin_memset_read_str, &c, dest_align))
3683 {
3684 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3685 val_rtx);
3686 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3687 builtin_memset_gen_str, val_rtx, dest_align, 0);
3688 }
3689 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3690 dest_align))
3691 goto do_libcall;
3692
3693 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3694 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3695 return dest_mem;
3696 }
3697
3698 if (target_char_cast (val, &c))
3699 goto do_libcall;
3700
3701 if (c)
3702 {
3703 if (host_integerp (len, 1)
3704 && !(optimize_size && tree_low_cst (len, 1) > 1)
3705 && can_store_by_pieces (tree_low_cst (len, 1),
3706 builtin_memset_read_str, &c, dest_align))
3707 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3708 builtin_memset_read_str, &c, dest_align, 0);
3709 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3710 dest_align))
3711 goto do_libcall;
3712
3713 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3714 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3715 return dest_mem;
3716 }
3717
3718 set_mem_align (dest_mem, dest_align);
3719 dest_addr = clear_storage (dest_mem, len_rtx,
3720 CALL_EXPR_TAILCALL (orig_exp)
3721 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3722
3723 if (dest_addr == 0)
3724 {
3725 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3726 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3727 }
3728
3729 return dest_addr;
3730
3731 do_libcall:
3732 fndecl = get_callee_fndecl (orig_exp);
3733 fcode = DECL_FUNCTION_CODE (fndecl);
3734 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3735 arglist = build_tree_list (NULL_TREE, len);
3736 if (fcode == BUILT_IN_MEMSET)
3737 arglist = tree_cons (NULL_TREE, val, arglist);
3738 arglist = tree_cons (NULL_TREE, dest, arglist);
3739 fn = build_function_call_expr (fndecl, arglist);
3740 if (TREE_CODE (fn) == CALL_EXPR)
3741 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3742 return expand_call (fn, target, target == const0_rtx);
3743 }
3744 }
3745
3746 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3747 if we failed the caller should emit a normal call. */
3748
3749 static rtx
3750 expand_builtin_bzero (tree exp)
3751 {
3752 tree arglist = TREE_OPERAND (exp, 1);
3753 tree dest, size, newarglist;
3754
3755 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3756 return NULL_RTX;
3757
3758 dest = TREE_VALUE (arglist);
3759 size = TREE_VALUE (TREE_CHAIN (arglist));
3760
3761 /* New argument list transforming bzero(ptr x, int y) to
3762 memset(ptr x, int 0, size_t y). This is done this way
3763 so that if it isn't expanded inline, we fallback to
3764 calling bzero instead of memset. */
3765
3766 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3767 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3768 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3769
3770 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3771 }
3772
3773 /* Expand expression EXP, which is a call to the memcmp built-in function.
3774 ARGLIST is the argument list for this call. Return 0 if we failed and the
3775 caller should emit a normal call, otherwise try to get the result in
3776 TARGET, if convenient (and in mode MODE, if that's convenient). */
3777
3778 static rtx
3779 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3780 enum machine_mode mode)
3781 {
3782 if (!validate_arglist (arglist,
3783 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3784 return 0;
3785 else
3786 {
3787 tree result = fold_builtin_memcmp (arglist);
3788 if (result)
3789 return expand_expr (result, target, mode, EXPAND_NORMAL);
3790 }
3791
3792 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3793 {
3794 tree arg1 = TREE_VALUE (arglist);
3795 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3796 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3797 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3798 rtx result;
3799 rtx insn;
3800
3801 int arg1_align
3802 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3803 int arg2_align
3804 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3805 enum machine_mode insn_mode;
3806
3807 #ifdef HAVE_cmpmemsi
3808 if (HAVE_cmpmemsi)
3809 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3810 else
3811 #endif
3812 #ifdef HAVE_cmpstrnsi
3813 if (HAVE_cmpstrnsi)
3814 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3815 else
3816 #endif
3817 return 0;
3818
3819 /* If we don't have POINTER_TYPE, call the function. */
3820 if (arg1_align == 0 || arg2_align == 0)
3821 return 0;
3822
3823 /* Make a place to write the result of the instruction. */
3824 result = target;
3825 if (! (result != 0
3826 && REG_P (result) && GET_MODE (result) == insn_mode
3827 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3828 result = gen_reg_rtx (insn_mode);
3829
3830 arg1_rtx = get_memory_rtx (arg1, len);
3831 arg2_rtx = get_memory_rtx (arg2, len);
3832 arg3_rtx = expand_normal (len);
3833
3834 /* Set MEM_SIZE as appropriate. */
3835 if (GET_CODE (arg3_rtx) == CONST_INT)
3836 {
3837 set_mem_size (arg1_rtx, arg3_rtx);
3838 set_mem_size (arg2_rtx, arg3_rtx);
3839 }
3840
3841 #ifdef HAVE_cmpmemsi
3842 if (HAVE_cmpmemsi)
3843 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3844 GEN_INT (MIN (arg1_align, arg2_align)));
3845 else
3846 #endif
3847 #ifdef HAVE_cmpstrnsi
3848 if (HAVE_cmpstrnsi)
3849 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3850 GEN_INT (MIN (arg1_align, arg2_align)));
3851 else
3852 #endif
3853 gcc_unreachable ();
3854
3855 if (insn)
3856 emit_insn (insn);
3857 else
3858 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3859 TYPE_MODE (integer_type_node), 3,
3860 XEXP (arg1_rtx, 0), Pmode,
3861 XEXP (arg2_rtx, 0), Pmode,
3862 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3863 TYPE_UNSIGNED (sizetype)),
3864 TYPE_MODE (sizetype));
3865
3866 /* Return the value in the proper mode for this function. */
3867 mode = TYPE_MODE (TREE_TYPE (exp));
3868 if (GET_MODE (result) == mode)
3869 return result;
3870 else if (target != 0)
3871 {
3872 convert_move (target, result, 0);
3873 return target;
3874 }
3875 else
3876 return convert_to_mode (mode, result, 0);
3877 }
3878 #endif
3879
3880 return 0;
3881 }
3882
3883 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3884 if we failed the caller should emit a normal call, otherwise try to get
3885 the result in TARGET, if convenient. */
3886
3887 static rtx
3888 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3889 {
3890 tree arglist = TREE_OPERAND (exp, 1);
3891
3892 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3893 return 0;
3894 else
3895 {
3896 tree result = fold_builtin_strcmp (arglist);
3897 if (result)
3898 return expand_expr (result, target, mode, EXPAND_NORMAL);
3899 }
3900
3901 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3902 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3903 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3904 {
3905 rtx arg1_rtx, arg2_rtx;
3906 rtx result, insn = NULL_RTX;
3907 tree fndecl, fn;
3908
3909 tree arg1 = TREE_VALUE (arglist);
3910 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3911 int arg1_align
3912 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3913 int arg2_align
3914 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3915
3916 /* If we don't have POINTER_TYPE, call the function. */
3917 if (arg1_align == 0 || arg2_align == 0)
3918 return 0;
3919
3920 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3921 arg1 = builtin_save_expr (arg1);
3922 arg2 = builtin_save_expr (arg2);
3923
3924 arg1_rtx = get_memory_rtx (arg1, NULL);
3925 arg2_rtx = get_memory_rtx (arg2, NULL);
3926
3927 #ifdef HAVE_cmpstrsi
3928 /* Try to call cmpstrsi. */
3929 if (HAVE_cmpstrsi)
3930 {
3931 enum machine_mode insn_mode
3932 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3933
3934 /* Make a place to write the result of the instruction. */
3935 result = target;
3936 if (! (result != 0
3937 && REG_P (result) && GET_MODE (result) == insn_mode
3938 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3939 result = gen_reg_rtx (insn_mode);
3940
3941 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3942 GEN_INT (MIN (arg1_align, arg2_align)));
3943 }
3944 #endif
3945 #ifdef HAVE_cmpstrnsi
3946 /* Try to determine at least one length and call cmpstrnsi. */
3947 if (!insn && HAVE_cmpstrnsi)
3948 {
3949 tree len;
3950 rtx arg3_rtx;
3951
3952 enum machine_mode insn_mode
3953 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3954 tree len1 = c_strlen (arg1, 1);
3955 tree len2 = c_strlen (arg2, 1);
3956
3957 if (len1)
3958 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3959 if (len2)
3960 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3961
3962 /* If we don't have a constant length for the first, use the length
3963 of the second, if we know it. We don't require a constant for
3964 this case; some cost analysis could be done if both are available
3965 but neither is constant. For now, assume they're equally cheap,
3966 unless one has side effects. If both strings have constant lengths,
3967 use the smaller. */
3968
3969 if (!len1)
3970 len = len2;
3971 else if (!len2)
3972 len = len1;
3973 else if (TREE_SIDE_EFFECTS (len1))
3974 len = len2;
3975 else if (TREE_SIDE_EFFECTS (len2))
3976 len = len1;
3977 else if (TREE_CODE (len1) != INTEGER_CST)
3978 len = len2;
3979 else if (TREE_CODE (len2) != INTEGER_CST)
3980 len = len1;
3981 else if (tree_int_cst_lt (len1, len2))
3982 len = len1;
3983 else
3984 len = len2;
3985
3986 /* If both arguments have side effects, we cannot optimize. */
3987 if (!len || TREE_SIDE_EFFECTS (len))
3988 goto do_libcall;
3989
3990 arg3_rtx = expand_normal (len);
3991
3992 /* Make a place to write the result of the instruction. */
3993 result = target;
3994 if (! (result != 0
3995 && REG_P (result) && GET_MODE (result) == insn_mode
3996 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3997 result = gen_reg_rtx (insn_mode);
3998
3999 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4000 GEN_INT (MIN (arg1_align, arg2_align)));
4001 }
4002 #endif
4003
4004 if (insn)
4005 {
4006 emit_insn (insn);
4007
4008 /* Return the value in the proper mode for this function. */
4009 mode = TYPE_MODE (TREE_TYPE (exp));
4010 if (GET_MODE (result) == mode)
4011 return result;
4012 if (target == 0)
4013 return convert_to_mode (mode, result, 0);
4014 convert_move (target, result, 0);
4015 return target;
4016 }
4017
4018 /* Expand the library call ourselves using a stabilized argument
4019 list to avoid re-evaluating the function's arguments twice. */
4020 #ifdef HAVE_cmpstrnsi
4021 do_libcall:
4022 #endif
4023 arglist = build_tree_list (NULL_TREE, arg2);
4024 arglist = tree_cons (NULL_TREE, arg1, arglist);
4025 fndecl = get_callee_fndecl (exp);
4026 fn = build_function_call_expr (fndecl, arglist);
4027 if (TREE_CODE (fn) == CALL_EXPR)
4028 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4029 return expand_call (fn, target, target == const0_rtx);
4030 }
4031 #endif
4032 return 0;
4033 }
4034
4035 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
4036 if we failed the caller should emit a normal call, otherwise try to get
4037 the result in TARGET, if convenient. */
4038
4039 static rtx
4040 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4041 {
4042 tree arglist = TREE_OPERAND (exp, 1);
4043
4044 if (!validate_arglist (arglist,
4045 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4046 return 0;
4047 else
4048 {
4049 tree result = fold_builtin_strncmp (arglist);
4050 if (result)
4051 return expand_expr (result, target, mode, EXPAND_NORMAL);
4052 }
4053
4054 /* If c_strlen can determine an expression for one of the string
4055 lengths, and it doesn't have side effects, then emit cmpstrnsi
4056 using length MIN(strlen(string)+1, arg3). */
4057 #ifdef HAVE_cmpstrnsi
4058 if (HAVE_cmpstrnsi)
4059 {
4060 tree arg1 = TREE_VALUE (arglist);
4061 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
4062 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4063 tree len, len1, len2;
4064 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4065 rtx result, insn;
4066 tree fndecl, fn;
4067
4068 int arg1_align
4069 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4070 int arg2_align
4071 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4072 enum machine_mode insn_mode
4073 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4074
4075 len1 = c_strlen (arg1, 1);
4076 len2 = c_strlen (arg2, 1);
4077
4078 if (len1)
4079 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4080 if (len2)
4081 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4082
4083 /* If we don't have a constant length for the first, use the length
4084 of the second, if we know it. We don't require a constant for
4085 this case; some cost analysis could be done if both are available
4086 but neither is constant. For now, assume they're equally cheap,
4087 unless one has side effects. If both strings have constant lengths,
4088 use the smaller. */
4089
4090 if (!len1)
4091 len = len2;
4092 else if (!len2)
4093 len = len1;
4094 else if (TREE_SIDE_EFFECTS (len1))
4095 len = len2;
4096 else if (TREE_SIDE_EFFECTS (len2))
4097 len = len1;
4098 else if (TREE_CODE (len1) != INTEGER_CST)
4099 len = len2;
4100 else if (TREE_CODE (len2) != INTEGER_CST)
4101 len = len1;
4102 else if (tree_int_cst_lt (len1, len2))
4103 len = len1;
4104 else
4105 len = len2;
4106
4107 /* If both arguments have side effects, we cannot optimize. */
4108 if (!len || TREE_SIDE_EFFECTS (len))
4109 return 0;
4110
4111 /* The actual new length parameter is MIN(len,arg3). */
4112 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4113 fold_convert (TREE_TYPE (len), arg3));
4114
4115 /* If we don't have POINTER_TYPE, call the function. */
4116 if (arg1_align == 0 || arg2_align == 0)
4117 return 0;
4118
4119 /* Make a place to write the result of the instruction. */
4120 result = target;
4121 if (! (result != 0
4122 && REG_P (result) && GET_MODE (result) == insn_mode
4123 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4124 result = gen_reg_rtx (insn_mode);
4125
4126 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4127 arg1 = builtin_save_expr (arg1);
4128 arg2 = builtin_save_expr (arg2);
4129 len = builtin_save_expr (len);
4130
4131 arg1_rtx = get_memory_rtx (arg1, len);
4132 arg2_rtx = get_memory_rtx (arg2, len);
4133 arg3_rtx = expand_normal (len);
4134 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4135 GEN_INT (MIN (arg1_align, arg2_align)));
4136 if (insn)
4137 {
4138 emit_insn (insn);
4139
4140 /* Return the value in the proper mode for this function. */
4141 mode = TYPE_MODE (TREE_TYPE (exp));
4142 if (GET_MODE (result) == mode)
4143 return result;
4144 if (target == 0)
4145 return convert_to_mode (mode, result, 0);
4146 convert_move (target, result, 0);
4147 return target;
4148 }
4149
4150 /* Expand the library call ourselves using a stabilized argument
4151 list to avoid re-evaluating the function's arguments twice. */
4152 arglist = build_tree_list (NULL_TREE, len);
4153 arglist = tree_cons (NULL_TREE, arg2, arglist);
4154 arglist = tree_cons (NULL_TREE, arg1, arglist);
4155 fndecl = get_callee_fndecl (exp);
4156 fn = build_function_call_expr (fndecl, arglist);
4157 if (TREE_CODE (fn) == CALL_EXPR)
4158 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4159 return expand_call (fn, target, target == const0_rtx);
4160 }
4161 #endif
4162 return 0;
4163 }
4164
4165 /* Expand expression EXP, which is a call to the strcat builtin.
4166 Return 0 if we failed the caller should emit a normal call,
4167 otherwise try to get the result in TARGET, if convenient. */
4168
4169 static rtx
4170 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
4171 {
4172 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4173 return 0;
4174 else
4175 {
4176 tree dst = TREE_VALUE (arglist),
4177 src = TREE_VALUE (TREE_CHAIN (arglist));
4178 const char *p = c_getstr (src);
4179
4180 /* If the string length is zero, return the dst parameter. */
4181 if (p && *p == '\0')
4182 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4183
4184 if (!optimize_size)
4185 {
4186 /* See if we can store by pieces into (dst + strlen(dst)). */
4187 tree newsrc, newdst,
4188 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4189 rtx insns;
4190
4191 /* Stabilize the argument list. */
4192 newsrc = builtin_save_expr (src);
4193 if (newsrc != src)
4194 arglist = build_tree_list (NULL_TREE, newsrc);
4195 else
4196 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4197
4198 dst = builtin_save_expr (dst);
4199
4200 start_sequence ();
4201
4202 /* Create strlen (dst). */
4203 newdst =
4204 build_function_call_expr (strlen_fn,
4205 build_tree_list (NULL_TREE, dst));
4206 /* Create (dst + (cast) strlen (dst)). */
4207 newdst = fold_convert (TREE_TYPE (dst), newdst);
4208 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4209
4210 newdst = builtin_save_expr (newdst);
4211 arglist = tree_cons (NULL_TREE, newdst, arglist);
4212
4213 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4214 {
4215 end_sequence (); /* Stop sequence. */
4216 return 0;
4217 }
4218
4219 /* Output the entire sequence. */
4220 insns = get_insns ();
4221 end_sequence ();
4222 emit_insn (insns);
4223
4224 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4225 }
4226
4227 return 0;
4228 }
4229 }
4230
4231 /* Expand expression EXP, which is a call to the strncat builtin.
4232 Return 0 if we failed the caller should emit a normal call,
4233 otherwise try to get the result in TARGET, if convenient. */
4234
4235 static rtx
4236 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4237 {
4238 if (validate_arglist (arglist,
4239 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4240 {
4241 tree result = fold_builtin_strncat (arglist);
4242 if (result)
4243 return expand_expr (result, target, mode, EXPAND_NORMAL);
4244 }
4245 return 0;
4246 }
4247
4248 /* Expand expression EXP, which is a call to the strspn builtin.
4249 Return 0 if we failed the caller should emit a normal call,
4250 otherwise try to get the result in TARGET, if convenient. */
4251
4252 static rtx
4253 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4254 {
4255 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4256 {
4257 tree result = fold_builtin_strspn (arglist);
4258 if (result)
4259 return expand_expr (result, target, mode, EXPAND_NORMAL);
4260 }
4261 return 0;
4262 }
4263
4264 /* Expand expression EXP, which is a call to the strcspn builtin.
4265 Return 0 if we failed the caller should emit a normal call,
4266 otherwise try to get the result in TARGET, if convenient. */
4267
4268 static rtx
4269 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4270 {
4271 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4272 {
4273 tree result = fold_builtin_strcspn (arglist);
4274 if (result)
4275 return expand_expr (result, target, mode, EXPAND_NORMAL);
4276 }
4277 return 0;
4278 }
4279
4280 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4281 if that's convenient. */
4282
4283 rtx
4284 expand_builtin_saveregs (void)
4285 {
4286 rtx val, seq;
4287
4288 /* Don't do __builtin_saveregs more than once in a function.
4289 Save the result of the first call and reuse it. */
4290 if (saveregs_value != 0)
4291 return saveregs_value;
4292
4293 /* When this function is called, it means that registers must be
4294 saved on entry to this function. So we migrate the call to the
4295 first insn of this function. */
4296
4297 start_sequence ();
4298
4299 /* Do whatever the machine needs done in this case. */
4300 val = targetm.calls.expand_builtin_saveregs ();
4301
4302 seq = get_insns ();
4303 end_sequence ();
4304
4305 saveregs_value = val;
4306
4307 /* Put the insns after the NOTE that starts the function. If this
4308 is inside a start_sequence, make the outer-level insn chain current, so
4309 the code is placed at the start of the function. */
4310 push_topmost_sequence ();
4311 emit_insn_after (seq, entry_of_function ());
4312 pop_topmost_sequence ();
4313
4314 return val;
4315 }
4316
4317 /* __builtin_args_info (N) returns word N of the arg space info
4318 for the current function. The number and meanings of words
4319 is controlled by the definition of CUMULATIVE_ARGS. */
4320
4321 static rtx
4322 expand_builtin_args_info (tree arglist)
4323 {
4324 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4325 int *word_ptr = (int *) &current_function_args_info;
4326
4327 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4328
4329 if (arglist != 0)
4330 {
4331 if (!host_integerp (TREE_VALUE (arglist), 0))
4332 error ("argument of %<__builtin_args_info%> must be constant");
4333 else
4334 {
4335 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4336
4337 if (wordnum < 0 || wordnum >= nwords)
4338 error ("argument of %<__builtin_args_info%> out of range");
4339 else
4340 return GEN_INT (word_ptr[wordnum]);
4341 }
4342 }
4343 else
4344 error ("missing argument in %<__builtin_args_info%>");
4345
4346 return const0_rtx;
4347 }
4348
4349 /* Expand a call to __builtin_next_arg. */
4350
4351 static rtx
4352 expand_builtin_next_arg (void)
4353 {
4354 /* Checking arguments is already done in fold_builtin_next_arg
4355 that must be called before this function. */
4356 return expand_binop (Pmode, add_optab,
4357 current_function_internal_arg_pointer,
4358 current_function_arg_offset_rtx,
4359 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4360 }
4361
4362 /* Make it easier for the backends by protecting the valist argument
4363 from multiple evaluations. */
4364
4365 static tree
4366 stabilize_va_list (tree valist, int needs_lvalue)
4367 {
4368 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4369 {
4370 if (TREE_SIDE_EFFECTS (valist))
4371 valist = save_expr (valist);
4372
4373 /* For this case, the backends will be expecting a pointer to
4374 TREE_TYPE (va_list_type_node), but it's possible we've
4375 actually been given an array (an actual va_list_type_node).
4376 So fix it. */
4377 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4378 {
4379 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4380 valist = build_fold_addr_expr_with_type (valist, p1);
4381 }
4382 }
4383 else
4384 {
4385 tree pt;
4386
4387 if (! needs_lvalue)
4388 {
4389 if (! TREE_SIDE_EFFECTS (valist))
4390 return valist;
4391
4392 pt = build_pointer_type (va_list_type_node);
4393 valist = fold_build1 (ADDR_EXPR, pt, valist);
4394 TREE_SIDE_EFFECTS (valist) = 1;
4395 }
4396
4397 if (TREE_SIDE_EFFECTS (valist))
4398 valist = save_expr (valist);
4399 valist = build_fold_indirect_ref (valist);
4400 }
4401
4402 return valist;
4403 }
4404
4405 /* The "standard" definition of va_list is void*. */
4406
4407 tree
4408 std_build_builtin_va_list (void)
4409 {
4410 return ptr_type_node;
4411 }
4412
4413 /* The "standard" implementation of va_start: just assign `nextarg' to
4414 the variable. */
4415
4416 void
4417 std_expand_builtin_va_start (tree valist, rtx nextarg)
4418 {
4419 tree t;
4420
4421 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist,
4422 make_tree (ptr_type_node, nextarg));
4423 TREE_SIDE_EFFECTS (t) = 1;
4424
4425 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4426 }
4427
4428 /* Expand ARGLIST, from a call to __builtin_va_start. */
4429
4430 static rtx
4431 expand_builtin_va_start (tree arglist)
4432 {
4433 rtx nextarg;
4434 tree chain, valist;
4435
4436 chain = TREE_CHAIN (arglist);
4437
4438 if (!chain)
4439 {
4440 error ("too few arguments to function %<va_start%>");
4441 return const0_rtx;
4442 }
4443
4444 if (fold_builtin_next_arg (chain))
4445 return const0_rtx;
4446
4447 nextarg = expand_builtin_next_arg ();
4448 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4449
4450 #ifdef EXPAND_BUILTIN_VA_START
4451 EXPAND_BUILTIN_VA_START (valist, nextarg);
4452 #else
4453 std_expand_builtin_va_start (valist, nextarg);
4454 #endif
4455
4456 return const0_rtx;
4457 }
4458
4459 /* The "standard" implementation of va_arg: read the value from the
4460 current (padded) address and increment by the (padded) size. */
4461
4462 tree
4463 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4464 {
4465 tree addr, t, type_size, rounded_size, valist_tmp;
4466 unsigned HOST_WIDE_INT align, boundary;
4467 bool indirect;
4468
4469 #ifdef ARGS_GROW_DOWNWARD
4470 /* All of the alignment and movement below is for args-grow-up machines.
4471 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4472 implement their own specialized gimplify_va_arg_expr routines. */
4473 gcc_unreachable ();
4474 #endif
4475
4476 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4477 if (indirect)
4478 type = build_pointer_type (type);
4479
4480 align = PARM_BOUNDARY / BITS_PER_UNIT;
4481 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4482
4483 /* Hoist the valist value into a temporary for the moment. */
4484 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4485
4486 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4487 requires greater alignment, we must perform dynamic alignment. */
4488 if (boundary > align
4489 && !integer_zerop (TYPE_SIZE (type)))
4490 {
4491 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4492 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4493 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4494 gimplify_and_add (t, pre_p);
4495
4496 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4497 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4498 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4499 gimplify_and_add (t, pre_p);
4500 }
4501 else
4502 boundary = align;
4503
4504 /* If the actual alignment is less than the alignment of the type,
4505 adjust the type accordingly so that we don't assume strict alignment
4506 when deferencing the pointer. */
4507 boundary *= BITS_PER_UNIT;
4508 if (boundary < TYPE_ALIGN (type))
4509 {
4510 type = build_variant_type_copy (type);
4511 TYPE_ALIGN (type) = boundary;
4512 }
4513
4514 /* Compute the rounded size of the type. */
4515 type_size = size_in_bytes (type);
4516 rounded_size = round_up (type_size, align);
4517
4518 /* Reduce rounded_size so it's sharable with the postqueue. */
4519 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4520
4521 /* Get AP. */
4522 addr = valist_tmp;
4523 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4524 {
4525 /* Small args are padded downward. */
4526 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4527 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4528 size_binop (MINUS_EXPR, rounded_size, type_size));
4529 t = fold_convert (TREE_TYPE (addr), t);
4530 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4531 }
4532
4533 /* Compute new value for AP. */
4534 t = fold_convert (TREE_TYPE (valist), rounded_size);
4535 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4536 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist, t);
4537 gimplify_and_add (t, pre_p);
4538
4539 addr = fold_convert (build_pointer_type (type), addr);
4540
4541 if (indirect)
4542 addr = build_va_arg_indirect_ref (addr);
4543
4544 return build_va_arg_indirect_ref (addr);
4545 }
4546
4547 /* Build an indirect-ref expression over the given TREE, which represents a
4548 piece of a va_arg() expansion. */
4549 tree
4550 build_va_arg_indirect_ref (tree addr)
4551 {
4552 addr = build_fold_indirect_ref (addr);
4553
4554 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4555 mf_mark (addr);
4556
4557 return addr;
4558 }
4559
4560 /* Return a dummy expression of type TYPE in order to keep going after an
4561 error. */
4562
4563 static tree
4564 dummy_object (tree type)
4565 {
4566 tree t = build_int_cst (build_pointer_type (type), 0);
4567 return build1 (INDIRECT_REF, type, t);
4568 }
4569
4570 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4571 builtin function, but a very special sort of operator. */
4572
4573 enum gimplify_status
4574 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4575 {
4576 tree promoted_type, want_va_type, have_va_type;
4577 tree valist = TREE_OPERAND (*expr_p, 0);
4578 tree type = TREE_TYPE (*expr_p);
4579 tree t;
4580
4581 /* Verify that valist is of the proper type. */
4582 want_va_type = va_list_type_node;
4583 have_va_type = TREE_TYPE (valist);
4584
4585 if (have_va_type == error_mark_node)
4586 return GS_ERROR;
4587
4588 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4589 {
4590 /* If va_list is an array type, the argument may have decayed
4591 to a pointer type, e.g. by being passed to another function.
4592 In that case, unwrap both types so that we can compare the
4593 underlying records. */
4594 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4595 || POINTER_TYPE_P (have_va_type))
4596 {
4597 want_va_type = TREE_TYPE (want_va_type);
4598 have_va_type = TREE_TYPE (have_va_type);
4599 }
4600 }
4601
4602 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4603 {
4604 error ("first argument to %<va_arg%> not of type %<va_list%>");
4605 return GS_ERROR;
4606 }
4607
4608 /* Generate a diagnostic for requesting data of a type that cannot
4609 be passed through `...' due to type promotion at the call site. */
4610 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4611 != type)
4612 {
4613 static bool gave_help;
4614
4615 /* Unfortunately, this is merely undefined, rather than a constraint
4616 violation, so we cannot make this an error. If this call is never
4617 executed, the program is still strictly conforming. */
4618 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4619 type, promoted_type);
4620 if (! gave_help)
4621 {
4622 gave_help = true;
4623 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4624 promoted_type, type);
4625 }
4626
4627 /* We can, however, treat "undefined" any way we please.
4628 Call abort to encourage the user to fix the program. */
4629 inform ("if this code is reached, the program will abort");
4630 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4631 NULL);
4632 append_to_statement_list (t, pre_p);
4633
4634 /* This is dead code, but go ahead and finish so that the
4635 mode of the result comes out right. */
4636 *expr_p = dummy_object (type);
4637 return GS_ALL_DONE;
4638 }
4639 else
4640 {
4641 /* Make it easier for the backends by protecting the valist argument
4642 from multiple evaluations. */
4643 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4644 {
4645 /* For this case, the backends will be expecting a pointer to
4646 TREE_TYPE (va_list_type_node), but it's possible we've
4647 actually been given an array (an actual va_list_type_node).
4648 So fix it. */
4649 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4650 {
4651 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4652 valist = build_fold_addr_expr_with_type (valist, p1);
4653 }
4654 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4655 }
4656 else
4657 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4658
4659 if (!targetm.gimplify_va_arg_expr)
4660 /* FIXME:Once most targets are converted we should merely
4661 assert this is non-null. */
4662 return GS_ALL_DONE;
4663
4664 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4665 return GS_OK;
4666 }
4667 }
4668
4669 /* Expand ARGLIST, from a call to __builtin_va_end. */
4670
4671 static rtx
4672 expand_builtin_va_end (tree arglist)
4673 {
4674 tree valist = TREE_VALUE (arglist);
4675
4676 /* Evaluate for side effects, if needed. I hate macros that don't
4677 do that. */
4678 if (TREE_SIDE_EFFECTS (valist))
4679 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4680
4681 return const0_rtx;
4682 }
4683
4684 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4685 builtin rather than just as an assignment in stdarg.h because of the
4686 nastiness of array-type va_list types. */
4687
4688 static rtx
4689 expand_builtin_va_copy (tree arglist)
4690 {
4691 tree dst, src, t;
4692
4693 dst = TREE_VALUE (arglist);
4694 src = TREE_VALUE (TREE_CHAIN (arglist));
4695
4696 dst = stabilize_va_list (dst, 1);
4697 src = stabilize_va_list (src, 0);
4698
4699 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4700 {
4701 t = build2 (GIMPLE_MODIFY_STMT, va_list_type_node, dst, src);
4702 TREE_SIDE_EFFECTS (t) = 1;
4703 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4704 }
4705 else
4706 {
4707 rtx dstb, srcb, size;
4708
4709 /* Evaluate to pointers. */
4710 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4711 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4712 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4713 VOIDmode, EXPAND_NORMAL);
4714
4715 dstb = convert_memory_address (Pmode, dstb);
4716 srcb = convert_memory_address (Pmode, srcb);
4717
4718 /* "Dereference" to BLKmode memories. */
4719 dstb = gen_rtx_MEM (BLKmode, dstb);
4720 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4721 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4722 srcb = gen_rtx_MEM (BLKmode, srcb);
4723 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4724 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4725
4726 /* Copy. */
4727 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4728 }
4729
4730 return const0_rtx;
4731 }
4732
4733 /* Expand a call to one of the builtin functions __builtin_frame_address or
4734 __builtin_return_address. */
4735
4736 static rtx
4737 expand_builtin_frame_address (tree fndecl, tree arglist)
4738 {
4739 /* The argument must be a nonnegative integer constant.
4740 It counts the number of frames to scan up the stack.
4741 The value is the return address saved in that frame. */
4742 if (arglist == 0)
4743 /* Warning about missing arg was already issued. */
4744 return const0_rtx;
4745 else if (! host_integerp (TREE_VALUE (arglist), 1))
4746 {
4747 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4748 error ("invalid argument to %<__builtin_frame_address%>");
4749 else
4750 error ("invalid argument to %<__builtin_return_address%>");
4751 return const0_rtx;
4752 }
4753 else
4754 {
4755 rtx tem
4756 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4757 tree_low_cst (TREE_VALUE (arglist), 1));
4758
4759 /* Some ports cannot access arbitrary stack frames. */
4760 if (tem == NULL)
4761 {
4762 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4763 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4764 else
4765 warning (0, "unsupported argument to %<__builtin_return_address%>");
4766 return const0_rtx;
4767 }
4768
4769 /* For __builtin_frame_address, return what we've got. */
4770 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4771 return tem;
4772
4773 if (!REG_P (tem)
4774 && ! CONSTANT_P (tem))
4775 tem = copy_to_mode_reg (Pmode, tem);
4776 return tem;
4777 }
4778 }
4779
4780 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4781 we failed and the caller should emit a normal call, otherwise try to get
4782 the result in TARGET, if convenient. */
4783
4784 static rtx
4785 expand_builtin_alloca (tree arglist, rtx target)
4786 {
4787 rtx op0;
4788 rtx result;
4789
4790 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4791 should always expand to function calls. These can be intercepted
4792 in libmudflap. */
4793 if (flag_mudflap)
4794 return 0;
4795
4796 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4797 return 0;
4798
4799 /* Compute the argument. */
4800 op0 = expand_normal (TREE_VALUE (arglist));
4801
4802 /* Allocate the desired space. */
4803 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4804 result = convert_memory_address (ptr_mode, result);
4805
4806 return result;
4807 }
4808
4809 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4810 is the mode to expand with. */
4811
4812 static rtx
4813 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4814 {
4815 enum machine_mode mode;
4816 tree arg;
4817 rtx op0;
4818
4819 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4820 return 0;
4821
4822 arg = TREE_VALUE (arglist);
4823 mode = TYPE_MODE (TREE_TYPE (arg));
4824 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4825
4826 target = expand_unop (mode, bswap_optab, op0, target, 1);
4827
4828 gcc_assert (target);
4829
4830 return convert_to_mode (mode, target, 0);
4831 }
4832
4833 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4834 Return 0 if a normal call should be emitted rather than expanding the
4835 function in-line. If convenient, the result should be placed in TARGET.
4836 SUBTARGET may be used as the target for computing one of EXP's operands. */
4837
4838 static rtx
4839 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4840 rtx subtarget, optab op_optab)
4841 {
4842 rtx op0;
4843 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4844 return 0;
4845
4846 /* Compute the argument. */
4847 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4848 /* Compute op, into TARGET if possible.
4849 Set TARGET to wherever the result comes back. */
4850 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4851 op_optab, op0, target, 1);
4852 gcc_assert (target);
4853
4854 return convert_to_mode (target_mode, target, 0);
4855 }
4856
4857 /* If the string passed to fputs is a constant and is one character
4858 long, we attempt to transform this call into __builtin_fputc(). */
4859
4860 static rtx
4861 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4862 {
4863 /* Verify the arguments in the original call. */
4864 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4865 {
4866 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4867 unlocked, NULL_TREE);
4868 if (result)
4869 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4870 }
4871 return 0;
4872 }
4873
4874 /* Expand a call to __builtin_expect. We just return our argument
4875 as the builtin_expect semantic should've been already executed by
4876 tree branch prediction pass. */
4877
4878 static rtx
4879 expand_builtin_expect (tree arglist, rtx target)
4880 {
4881 tree exp, c;
4882
4883 if (arglist == NULL_TREE
4884 || TREE_CHAIN (arglist) == NULL_TREE)
4885 return const0_rtx;
4886 exp = TREE_VALUE (arglist);
4887 c = TREE_VALUE (TREE_CHAIN (arglist));
4888
4889 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4890 /* When guessing was done, the hints should be already stripped away. */
4891 gcc_assert (!flag_guess_branch_prob);
4892 return target;
4893 }
4894
4895 void
4896 expand_builtin_trap (void)
4897 {
4898 #ifdef HAVE_trap
4899 if (HAVE_trap)
4900 emit_insn (gen_trap ());
4901 else
4902 #endif
4903 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4904 emit_barrier ();
4905 }
4906
4907 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4908 Return 0 if a normal call should be emitted rather than expanding
4909 the function inline. If convenient, the result should be placed
4910 in TARGET. SUBTARGET may be used as the target for computing
4911 the operand. */
4912
4913 static rtx
4914 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4915 {
4916 enum machine_mode mode;
4917 tree arg;
4918 rtx op0;
4919
4920 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4921 return 0;
4922
4923 arg = TREE_VALUE (arglist);
4924 mode = TYPE_MODE (TREE_TYPE (arg));
4925 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4926 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4927 }
4928
4929 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4930 Return NULL is a normal call should be emitted rather than expanding the
4931 function inline. If convenient, the result should be placed in TARGET.
4932 SUBTARGET may be used as the target for computing the operand. */
4933
4934 static rtx
4935 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4936 {
4937 rtx op0, op1;
4938 tree arg;
4939
4940 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4941 return 0;
4942
4943 arg = TREE_VALUE (arglist);
4944 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4945
4946 arg = TREE_VALUE (TREE_CHAIN (arglist));
4947 op1 = expand_normal (arg);
4948
4949 return expand_copysign (op0, op1, target);
4950 }
4951
4952 /* Create a new constant string literal and return a char* pointer to it.
4953 The STRING_CST value is the LEN characters at STR. */
4954 tree
4955 build_string_literal (int len, const char *str)
4956 {
4957 tree t, elem, index, type;
4958
4959 t = build_string (len, str);
4960 elem = build_type_variant (char_type_node, 1, 0);
4961 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4962 type = build_array_type (elem, index);
4963 TREE_TYPE (t) = type;
4964 TREE_CONSTANT (t) = 1;
4965 TREE_INVARIANT (t) = 1;
4966 TREE_READONLY (t) = 1;
4967 TREE_STATIC (t) = 1;
4968
4969 type = build_pointer_type (type);
4970 t = build1 (ADDR_EXPR, type, t);
4971
4972 type = build_pointer_type (elem);
4973 t = build1 (NOP_EXPR, type, t);
4974 return t;
4975 }
4976
4977 /* Expand EXP, a call to printf or printf_unlocked.
4978 Return 0 if a normal call should be emitted rather than transforming
4979 the function inline. If convenient, the result should be placed in
4980 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4981 call. */
4982 static rtx
4983 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4984 bool unlocked)
4985 {
4986 tree arglist = TREE_OPERAND (exp, 1);
4987 /* If we're using an unlocked function, assume the other unlocked
4988 functions exist explicitly. */
4989 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4990 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4991 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4992 : implicit_built_in_decls[BUILT_IN_PUTS];
4993 const char *fmt_str;
4994 tree fn, fmt, arg;
4995
4996 /* If the return value is used, don't do the transformation. */
4997 if (target != const0_rtx)
4998 return 0;
4999
5000 /* Verify the required arguments in the original call. */
5001 if (! arglist)
5002 return 0;
5003 fmt = TREE_VALUE (arglist);
5004 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5005 return 0;
5006 arglist = TREE_CHAIN (arglist);
5007
5008 /* Check whether the format is a literal string constant. */
5009 fmt_str = c_getstr (fmt);
5010 if (fmt_str == NULL)
5011 return 0;
5012
5013 if (!init_target_chars())
5014 return 0;
5015
5016 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5017 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5018 {
5019 if (! arglist
5020 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5021 || TREE_CHAIN (arglist))
5022 return 0;
5023 fn = fn_puts;
5024 }
5025 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5026 else if (strcmp (fmt_str, target_percent_c) == 0)
5027 {
5028 if (! arglist
5029 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5030 || TREE_CHAIN (arglist))
5031 return 0;
5032 fn = fn_putchar;
5033 }
5034 else
5035 {
5036 /* We can't handle anything else with % args or %% ... yet. */
5037 if (strchr (fmt_str, target_percent))
5038 return 0;
5039
5040 if (arglist)
5041 return 0;
5042
5043 /* If the format specifier was "", printf does nothing. */
5044 if (fmt_str[0] == '\0')
5045 return const0_rtx;
5046 /* If the format specifier has length of 1, call putchar. */
5047 if (fmt_str[1] == '\0')
5048 {
5049 /* Given printf("c"), (where c is any one character,)
5050 convert "c"[0] to an int and pass that to the replacement
5051 function. */
5052 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5053 arglist = build_tree_list (NULL_TREE, arg);
5054 fn = fn_putchar;
5055 }
5056 else
5057 {
5058 /* If the format specifier was "string\n", call puts("string"). */
5059 size_t len = strlen (fmt_str);
5060 if ((unsigned char)fmt_str[len - 1] == target_newline)
5061 {
5062 /* Create a NUL-terminated string that's one char shorter
5063 than the original, stripping off the trailing '\n'. */
5064 char *newstr = alloca (len);
5065 memcpy (newstr, fmt_str, len - 1);
5066 newstr[len - 1] = 0;
5067
5068 arg = build_string_literal (len, newstr);
5069 arglist = build_tree_list (NULL_TREE, arg);
5070 fn = fn_puts;
5071 }
5072 else
5073 /* We'd like to arrange to call fputs(string,stdout) here,
5074 but we need stdout and don't have a way to get it yet. */
5075 return 0;
5076 }
5077 }
5078
5079 if (!fn)
5080 return 0;
5081 fn = build_function_call_expr (fn, arglist);
5082 if (TREE_CODE (fn) == CALL_EXPR)
5083 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5084 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5085 }
5086
5087 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5088 Return 0 if a normal call should be emitted rather than transforming
5089 the function inline. If convenient, the result should be placed in
5090 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5091 call. */
5092 static rtx
5093 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5094 bool unlocked)
5095 {
5096 tree arglist = TREE_OPERAND (exp, 1);
5097 /* If we're using an unlocked function, assume the other unlocked
5098 functions exist explicitly. */
5099 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5100 : implicit_built_in_decls[BUILT_IN_FPUTC];
5101 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5102 : implicit_built_in_decls[BUILT_IN_FPUTS];
5103 const char *fmt_str;
5104 tree fn, fmt, fp, arg;
5105
5106 /* If the return value is used, don't do the transformation. */
5107 if (target != const0_rtx)
5108 return 0;
5109
5110 /* Verify the required arguments in the original call. */
5111 if (! arglist)
5112 return 0;
5113 fp = TREE_VALUE (arglist);
5114 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5115 return 0;
5116 arglist = TREE_CHAIN (arglist);
5117 if (! arglist)
5118 return 0;
5119 fmt = TREE_VALUE (arglist);
5120 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5121 return 0;
5122 arglist = TREE_CHAIN (arglist);
5123
5124 /* Check whether the format is a literal string constant. */
5125 fmt_str = c_getstr (fmt);
5126 if (fmt_str == NULL)
5127 return 0;
5128
5129 if (!init_target_chars())
5130 return 0;
5131
5132 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5133 if (strcmp (fmt_str, target_percent_s) == 0)
5134 {
5135 if (! arglist
5136 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5137 || TREE_CHAIN (arglist))
5138 return 0;
5139 arg = TREE_VALUE (arglist);
5140 arglist = build_tree_list (NULL_TREE, fp);
5141 arglist = tree_cons (NULL_TREE, arg, arglist);
5142 fn = fn_fputs;
5143 }
5144 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5145 else if (strcmp (fmt_str, target_percent_c) == 0)
5146 {
5147 if (! arglist
5148 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5149 || TREE_CHAIN (arglist))
5150 return 0;
5151 arg = TREE_VALUE (arglist);
5152 arglist = build_tree_list (NULL_TREE, fp);
5153 arglist = tree_cons (NULL_TREE, arg, arglist);
5154 fn = fn_fputc;
5155 }
5156 else
5157 {
5158 /* We can't handle anything else with % args or %% ... yet. */
5159 if (strchr (fmt_str, target_percent))
5160 return 0;
5161
5162 if (arglist)
5163 return 0;
5164
5165 /* If the format specifier was "", fprintf does nothing. */
5166 if (fmt_str[0] == '\0')
5167 {
5168 /* Evaluate and ignore FILE* argument for side-effects. */
5169 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5170 return const0_rtx;
5171 }
5172
5173 /* When "string" doesn't contain %, replace all cases of
5174 fprintf(stream,string) with fputs(string,stream). The fputs
5175 builtin will take care of special cases like length == 1. */
5176 arglist = build_tree_list (NULL_TREE, fp);
5177 arglist = tree_cons (NULL_TREE, fmt, arglist);
5178 fn = fn_fputs;
5179 }
5180
5181 if (!fn)
5182 return 0;
5183 fn = build_function_call_expr (fn, arglist);
5184 if (TREE_CODE (fn) == CALL_EXPR)
5185 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5186 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5187 }
5188
5189 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5190 a normal call should be emitted rather than expanding the function
5191 inline. If convenient, the result should be placed in TARGET with
5192 mode MODE. */
5193
5194 static rtx
5195 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5196 {
5197 tree orig_arglist, dest, fmt;
5198 const char *fmt_str;
5199
5200 orig_arglist = arglist;
5201
5202 /* Verify the required arguments in the original call. */
5203 if (! arglist)
5204 return 0;
5205 dest = TREE_VALUE (arglist);
5206 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5207 return 0;
5208 arglist = TREE_CHAIN (arglist);
5209 if (! arglist)
5210 return 0;
5211 fmt = TREE_VALUE (arglist);
5212 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5213 return 0;
5214 arglist = TREE_CHAIN (arglist);
5215
5216 /* Check whether the format is a literal string constant. */
5217 fmt_str = c_getstr (fmt);
5218 if (fmt_str == NULL)
5219 return 0;
5220
5221 if (!init_target_chars())
5222 return 0;
5223
5224 /* If the format doesn't contain % args or %%, use strcpy. */
5225 if (strchr (fmt_str, target_percent) == 0)
5226 {
5227 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5228 tree exp;
5229
5230 if (arglist || ! fn)
5231 return 0;
5232 expand_expr (build_function_call_expr (fn, orig_arglist),
5233 const0_rtx, VOIDmode, EXPAND_NORMAL);
5234 if (target == const0_rtx)
5235 return const0_rtx;
5236 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5237 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5238 }
5239 /* If the format is "%s", use strcpy if the result isn't used. */
5240 else if (strcmp (fmt_str, target_percent_s) == 0)
5241 {
5242 tree fn, arg, len;
5243 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5244
5245 if (! fn)
5246 return 0;
5247
5248 if (! arglist || TREE_CHAIN (arglist))
5249 return 0;
5250 arg = TREE_VALUE (arglist);
5251 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5252 return 0;
5253
5254 if (target != const0_rtx)
5255 {
5256 len = c_strlen (arg, 1);
5257 if (! len || TREE_CODE (len) != INTEGER_CST)
5258 return 0;
5259 }
5260 else
5261 len = NULL_TREE;
5262
5263 arglist = build_tree_list (NULL_TREE, arg);
5264 arglist = tree_cons (NULL_TREE, dest, arglist);
5265 expand_expr (build_function_call_expr (fn, arglist),
5266 const0_rtx, VOIDmode, EXPAND_NORMAL);
5267
5268 if (target == const0_rtx)
5269 return const0_rtx;
5270 return expand_expr (len, target, mode, EXPAND_NORMAL);
5271 }
5272
5273 return 0;
5274 }
5275
5276 /* Expand a call to either the entry or exit function profiler. */
5277
5278 static rtx
5279 expand_builtin_profile_func (bool exitp)
5280 {
5281 rtx this, which;
5282
5283 this = DECL_RTL (current_function_decl);
5284 gcc_assert (MEM_P (this));
5285 this = XEXP (this, 0);
5286
5287 if (exitp)
5288 which = profile_function_exit_libfunc;
5289 else
5290 which = profile_function_entry_libfunc;
5291
5292 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5293 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5294 0),
5295 Pmode);
5296
5297 return const0_rtx;
5298 }
5299
5300 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5301
5302 static rtx
5303 round_trampoline_addr (rtx tramp)
5304 {
5305 rtx temp, addend, mask;
5306
5307 /* If we don't need too much alignment, we'll have been guaranteed
5308 proper alignment by get_trampoline_type. */
5309 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5310 return tramp;
5311
5312 /* Round address up to desired boundary. */
5313 temp = gen_reg_rtx (Pmode);
5314 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5315 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5316
5317 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5318 temp, 0, OPTAB_LIB_WIDEN);
5319 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5320 temp, 0, OPTAB_LIB_WIDEN);
5321
5322 return tramp;
5323 }
5324
5325 static rtx
5326 expand_builtin_init_trampoline (tree arglist)
5327 {
5328 tree t_tramp, t_func, t_chain;
5329 rtx r_tramp, r_func, r_chain;
5330 #ifdef TRAMPOLINE_TEMPLATE
5331 rtx blktramp;
5332 #endif
5333
5334 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5335 POINTER_TYPE, VOID_TYPE))
5336 return NULL_RTX;
5337
5338 t_tramp = TREE_VALUE (arglist);
5339 arglist = TREE_CHAIN (arglist);
5340 t_func = TREE_VALUE (arglist);
5341 arglist = TREE_CHAIN (arglist);
5342 t_chain = TREE_VALUE (arglist);
5343
5344 r_tramp = expand_normal (t_tramp);
5345 r_func = expand_normal (t_func);
5346 r_chain = expand_normal (t_chain);
5347
5348 /* Generate insns to initialize the trampoline. */
5349 r_tramp = round_trampoline_addr (r_tramp);
5350 #ifdef TRAMPOLINE_TEMPLATE
5351 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5352 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5353 emit_block_move (blktramp, assemble_trampoline_template (),
5354 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5355 #endif
5356 trampolines_created = 1;
5357 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5358
5359 return const0_rtx;
5360 }
5361
5362 static rtx
5363 expand_builtin_adjust_trampoline (tree arglist)
5364 {
5365 rtx tramp;
5366
5367 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5368 return NULL_RTX;
5369
5370 tramp = expand_normal (TREE_VALUE (arglist));
5371 tramp = round_trampoline_addr (tramp);
5372 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5373 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5374 #endif
5375
5376 return tramp;
5377 }
5378
5379 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5380 Return NULL_RTX if a normal call should be emitted rather than expanding
5381 the function in-line. EXP is the expression that is a call to the builtin
5382 function; if convenient, the result should be placed in TARGET. */
5383
5384 static rtx
5385 expand_builtin_signbit (tree exp, rtx target)
5386 {
5387 const struct real_format *fmt;
5388 enum machine_mode fmode, imode, rmode;
5389 HOST_WIDE_INT hi, lo;
5390 tree arg, arglist;
5391 int word, bitpos;
5392 rtx temp;
5393
5394 arglist = TREE_OPERAND (exp, 1);
5395 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5396 return 0;
5397
5398 arg = TREE_VALUE (arglist);
5399 fmode = TYPE_MODE (TREE_TYPE (arg));
5400 rmode = TYPE_MODE (TREE_TYPE (exp));
5401 fmt = REAL_MODE_FORMAT (fmode);
5402
5403 /* For floating point formats without a sign bit, implement signbit
5404 as "ARG < 0.0". */
5405 bitpos = fmt->signbit_ro;
5406 if (bitpos < 0)
5407 {
5408 /* But we can't do this if the format supports signed zero. */
5409 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5410 return 0;
5411
5412 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5413 build_real (TREE_TYPE (arg), dconst0));
5414 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5415 }
5416
5417 temp = expand_normal (arg);
5418 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5419 {
5420 imode = int_mode_for_mode (fmode);
5421 if (imode == BLKmode)
5422 return 0;
5423 temp = gen_lowpart (imode, temp);
5424 }
5425 else
5426 {
5427 imode = word_mode;
5428 /* Handle targets with different FP word orders. */
5429 if (FLOAT_WORDS_BIG_ENDIAN)
5430 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5431 else
5432 word = bitpos / BITS_PER_WORD;
5433 temp = operand_subword_force (temp, word, fmode);
5434 bitpos = bitpos % BITS_PER_WORD;
5435 }
5436
5437 /* Force the intermediate word_mode (or narrower) result into a
5438 register. This avoids attempting to create paradoxical SUBREGs
5439 of floating point modes below. */
5440 temp = force_reg (imode, temp);
5441
5442 /* If the bitpos is within the "result mode" lowpart, the operation
5443 can be implement with a single bitwise AND. Otherwise, we need
5444 a right shift and an AND. */
5445
5446 if (bitpos < GET_MODE_BITSIZE (rmode))
5447 {
5448 if (bitpos < HOST_BITS_PER_WIDE_INT)
5449 {
5450 hi = 0;
5451 lo = (HOST_WIDE_INT) 1 << bitpos;
5452 }
5453 else
5454 {
5455 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5456 lo = 0;
5457 }
5458
5459 if (imode != rmode)
5460 temp = gen_lowpart (rmode, temp);
5461 temp = expand_binop (rmode, and_optab, temp,
5462 immed_double_const (lo, hi, rmode),
5463 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5464 }
5465 else
5466 {
5467 /* Perform a logical right shift to place the signbit in the least
5468 significant bit, then truncate the result to the desired mode
5469 and mask just this bit. */
5470 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5471 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5472 temp = gen_lowpart (rmode, temp);
5473 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5474 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5475 }
5476
5477 return temp;
5478 }
5479
5480 /* Expand fork or exec calls. TARGET is the desired target of the
5481 call. ARGLIST is the list of arguments of the call. FN is the
5482 identificator of the actual function. IGNORE is nonzero if the
5483 value is to be ignored. */
5484
5485 static rtx
5486 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5487 {
5488 tree id, decl;
5489 tree call;
5490
5491 /* If we are not profiling, just call the function. */
5492 if (!profile_arc_flag)
5493 return NULL_RTX;
5494
5495 /* Otherwise call the wrapper. This should be equivalent for the rest of
5496 compiler, so the code does not diverge, and the wrapper may run the
5497 code necessary for keeping the profiling sane. */
5498
5499 switch (DECL_FUNCTION_CODE (fn))
5500 {
5501 case BUILT_IN_FORK:
5502 id = get_identifier ("__gcov_fork");
5503 break;
5504
5505 case BUILT_IN_EXECL:
5506 id = get_identifier ("__gcov_execl");
5507 break;
5508
5509 case BUILT_IN_EXECV:
5510 id = get_identifier ("__gcov_execv");
5511 break;
5512
5513 case BUILT_IN_EXECLP:
5514 id = get_identifier ("__gcov_execlp");
5515 break;
5516
5517 case BUILT_IN_EXECLE:
5518 id = get_identifier ("__gcov_execle");
5519 break;
5520
5521 case BUILT_IN_EXECVP:
5522 id = get_identifier ("__gcov_execvp");
5523 break;
5524
5525 case BUILT_IN_EXECVE:
5526 id = get_identifier ("__gcov_execve");
5527 break;
5528
5529 default:
5530 gcc_unreachable ();
5531 }
5532
5533 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5534 DECL_EXTERNAL (decl) = 1;
5535 TREE_PUBLIC (decl) = 1;
5536 DECL_ARTIFICIAL (decl) = 1;
5537 TREE_NOTHROW (decl) = 1;
5538 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5539 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5540 call = build_function_call_expr (decl, arglist);
5541
5542 return expand_call (call, target, ignore);
5543 }
5544
5545 \f
5546 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5547 the pointer in these functions is void*, the tree optimizers may remove
5548 casts. The mode computed in expand_builtin isn't reliable either, due
5549 to __sync_bool_compare_and_swap.
5550
5551 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5552 group of builtins. This gives us log2 of the mode size. */
5553
5554 static inline enum machine_mode
5555 get_builtin_sync_mode (int fcode_diff)
5556 {
5557 /* The size is not negotiable, so ask not to get BLKmode in return
5558 if the target indicates that a smaller size would be better. */
5559 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5560 }
5561
5562 /* Expand the memory expression LOC and return the appropriate memory operand
5563 for the builtin_sync operations. */
5564
5565 static rtx
5566 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5567 {
5568 rtx addr, mem;
5569
5570 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5571
5572 /* Note that we explicitly do not want any alias information for this
5573 memory, so that we kill all other live memories. Otherwise we don't
5574 satisfy the full barrier semantics of the intrinsic. */
5575 mem = validize_mem (gen_rtx_MEM (mode, addr));
5576
5577 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5578 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5579 MEM_VOLATILE_P (mem) = 1;
5580
5581 return mem;
5582 }
5583
5584 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5585 ARGLIST is the operands list to the function. CODE is the rtx code
5586 that corresponds to the arithmetic or logical operation from the name;
5587 an exception here is that NOT actually means NAND. TARGET is an optional
5588 place for us to store the results; AFTER is true if this is the
5589 fetch_and_xxx form. IGNORE is true if we don't actually care about
5590 the result of the operation at all. */
5591
5592 static rtx
5593 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5594 enum rtx_code code, bool after,
5595 rtx target, bool ignore)
5596 {
5597 rtx val, mem;
5598
5599 /* Expand the operands. */
5600 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5601
5602 arglist = TREE_CHAIN (arglist);
5603 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5604 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5605 val = convert_to_mode (mode, val, 1);
5606
5607 if (ignore)
5608 return expand_sync_operation (mem, val, code);
5609 else
5610 return expand_sync_fetch_operation (mem, val, code, after, target);
5611 }
5612
5613 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5614 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5615 true if this is the boolean form. TARGET is a place for us to store the
5616 results; this is NOT optional if IS_BOOL is true. */
5617
5618 static rtx
5619 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5620 bool is_bool, rtx target)
5621 {
5622 rtx old_val, new_val, mem;
5623
5624 /* Expand the operands. */
5625 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5626
5627 arglist = TREE_CHAIN (arglist);
5628 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5629 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5630 old_val = convert_to_mode (mode, old_val, 1);
5631
5632 arglist = TREE_CHAIN (arglist);
5633 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5634 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5635 new_val = convert_to_mode (mode, new_val, 1);
5636
5637 if (is_bool)
5638 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5639 else
5640 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5641 }
5642
5643 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5644 general form is actually an atomic exchange, and some targets only
5645 support a reduced form with the second argument being a constant 1.
5646 ARGLIST is the operands list to the function; TARGET is an optional
5647 place for us to store the results. */
5648
5649 static rtx
5650 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5651 rtx target)
5652 {
5653 rtx val, mem;
5654
5655 /* Expand the operands. */
5656 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5657
5658 arglist = TREE_CHAIN (arglist);
5659 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5660 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5661 val = convert_to_mode (mode, val, 1);
5662
5663 return expand_sync_lock_test_and_set (mem, val, target);
5664 }
5665
5666 /* Expand the __sync_synchronize intrinsic. */
5667
5668 static void
5669 expand_builtin_synchronize (void)
5670 {
5671 tree x;
5672
5673 #ifdef HAVE_memory_barrier
5674 if (HAVE_memory_barrier)
5675 {
5676 emit_insn (gen_memory_barrier ());
5677 return;
5678 }
5679 #endif
5680
5681 /* If no explicit memory barrier instruction is available, create an
5682 empty asm stmt with a memory clobber. */
5683 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5684 tree_cons (NULL, build_string (6, "memory"), NULL));
5685 ASM_VOLATILE_P (x) = 1;
5686 expand_asm_expr (x);
5687 }
5688
5689 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5690 to the function. */
5691
5692 static void
5693 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5694 {
5695 enum insn_code icode;
5696 rtx mem, insn;
5697 rtx val = const0_rtx;
5698
5699 /* Expand the operands. */
5700 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5701
5702 /* If there is an explicit operation in the md file, use it. */
5703 icode = sync_lock_release[mode];
5704 if (icode != CODE_FOR_nothing)
5705 {
5706 if (!insn_data[icode].operand[1].predicate (val, mode))
5707 val = force_reg (mode, val);
5708
5709 insn = GEN_FCN (icode) (mem, val);
5710 if (insn)
5711 {
5712 emit_insn (insn);
5713 return;
5714 }
5715 }
5716
5717 /* Otherwise we can implement this operation by emitting a barrier
5718 followed by a store of zero. */
5719 expand_builtin_synchronize ();
5720 emit_move_insn (mem, val);
5721 }
5722 \f
5723 /* Expand an expression EXP that calls a built-in function,
5724 with result going to TARGET if that's convenient
5725 (and in mode MODE if that's convenient).
5726 SUBTARGET may be used as the target for computing one of EXP's operands.
5727 IGNORE is nonzero if the value is to be ignored. */
5728
5729 rtx
5730 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5731 int ignore)
5732 {
5733 tree fndecl = get_callee_fndecl (exp);
5734 tree arglist = TREE_OPERAND (exp, 1);
5735 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5736 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5737
5738 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5739 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5740
5741 /* When not optimizing, generate calls to library functions for a certain
5742 set of builtins. */
5743 if (!optimize
5744 && !called_as_built_in (fndecl)
5745 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5746 && fcode != BUILT_IN_ALLOCA)
5747 return expand_call (exp, target, ignore);
5748
5749 /* The built-in function expanders test for target == const0_rtx
5750 to determine whether the function's result will be ignored. */
5751 if (ignore)
5752 target = const0_rtx;
5753
5754 /* If the result of a pure or const built-in function is ignored, and
5755 none of its arguments are volatile, we can avoid expanding the
5756 built-in call and just evaluate the arguments for side-effects. */
5757 if (target == const0_rtx
5758 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5759 {
5760 bool volatilep = false;
5761 tree arg;
5762
5763 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5764 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5765 {
5766 volatilep = true;
5767 break;
5768 }
5769
5770 if (! volatilep)
5771 {
5772 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5773 expand_expr (TREE_VALUE (arg), const0_rtx,
5774 VOIDmode, EXPAND_NORMAL);
5775 return const0_rtx;
5776 }
5777 }
5778
5779 switch (fcode)
5780 {
5781 CASE_FLT_FN (BUILT_IN_FABS):
5782 target = expand_builtin_fabs (arglist, target, subtarget);
5783 if (target)
5784 return target;
5785 break;
5786
5787 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5788 target = expand_builtin_copysign (arglist, target, subtarget);
5789 if (target)
5790 return target;
5791 break;
5792
5793 /* Just do a normal library call if we were unable to fold
5794 the values. */
5795 CASE_FLT_FN (BUILT_IN_CABS):
5796 break;
5797
5798 CASE_FLT_FN (BUILT_IN_EXP):
5799 CASE_FLT_FN (BUILT_IN_EXP10):
5800 CASE_FLT_FN (BUILT_IN_POW10):
5801 CASE_FLT_FN (BUILT_IN_EXP2):
5802 CASE_FLT_FN (BUILT_IN_EXPM1):
5803 CASE_FLT_FN (BUILT_IN_LOGB):
5804 CASE_FLT_FN (BUILT_IN_ILOGB):
5805 CASE_FLT_FN (BUILT_IN_LOG):
5806 CASE_FLT_FN (BUILT_IN_LOG10):
5807 CASE_FLT_FN (BUILT_IN_LOG2):
5808 CASE_FLT_FN (BUILT_IN_LOG1P):
5809 CASE_FLT_FN (BUILT_IN_TAN):
5810 CASE_FLT_FN (BUILT_IN_ASIN):
5811 CASE_FLT_FN (BUILT_IN_ACOS):
5812 CASE_FLT_FN (BUILT_IN_ATAN):
5813 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5814 because of possible accuracy problems. */
5815 if (! flag_unsafe_math_optimizations)
5816 break;
5817 CASE_FLT_FN (BUILT_IN_SQRT):
5818 CASE_FLT_FN (BUILT_IN_FLOOR):
5819 CASE_FLT_FN (BUILT_IN_CEIL):
5820 CASE_FLT_FN (BUILT_IN_TRUNC):
5821 CASE_FLT_FN (BUILT_IN_ROUND):
5822 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5823 CASE_FLT_FN (BUILT_IN_RINT):
5824 target = expand_builtin_mathfn (exp, target, subtarget);
5825 if (target)
5826 return target;
5827 break;
5828
5829 CASE_FLT_FN (BUILT_IN_LCEIL):
5830 CASE_FLT_FN (BUILT_IN_LLCEIL):
5831 CASE_FLT_FN (BUILT_IN_LFLOOR):
5832 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5833 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5834 if (target)
5835 return target;
5836 break;
5837
5838 CASE_FLT_FN (BUILT_IN_LRINT):
5839 CASE_FLT_FN (BUILT_IN_LLRINT):
5840 CASE_FLT_FN (BUILT_IN_LROUND):
5841 CASE_FLT_FN (BUILT_IN_LLROUND):
5842 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5843 if (target)
5844 return target;
5845 break;
5846
5847 CASE_FLT_FN (BUILT_IN_POW):
5848 target = expand_builtin_pow (exp, target, subtarget);
5849 if (target)
5850 return target;
5851 break;
5852
5853 CASE_FLT_FN (BUILT_IN_POWI):
5854 target = expand_builtin_powi (exp, target, subtarget);
5855 if (target)
5856 return target;
5857 break;
5858
5859 CASE_FLT_FN (BUILT_IN_ATAN2):
5860 CASE_FLT_FN (BUILT_IN_LDEXP):
5861 if (! flag_unsafe_math_optimizations)
5862 break;
5863
5864 CASE_FLT_FN (BUILT_IN_FMOD):
5865 CASE_FLT_FN (BUILT_IN_REMAINDER):
5866 CASE_FLT_FN (BUILT_IN_DREM):
5867 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5868 if (target)
5869 return target;
5870 break;
5871
5872 CASE_FLT_FN (BUILT_IN_CEXPI):
5873 target = expand_builtin_cexpi (exp, target, subtarget);
5874 gcc_assert (target);
5875 return target;
5876
5877 CASE_FLT_FN (BUILT_IN_SIN):
5878 CASE_FLT_FN (BUILT_IN_COS):
5879 if (! flag_unsafe_math_optimizations)
5880 break;
5881 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5882 if (target)
5883 return target;
5884 break;
5885
5886 CASE_FLT_FN (BUILT_IN_SINCOS):
5887 if (! flag_unsafe_math_optimizations)
5888 break;
5889 target = expand_builtin_sincos (exp);
5890 if (target)
5891 return target;
5892 break;
5893
5894 case BUILT_IN_APPLY_ARGS:
5895 return expand_builtin_apply_args ();
5896
5897 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5898 FUNCTION with a copy of the parameters described by
5899 ARGUMENTS, and ARGSIZE. It returns a block of memory
5900 allocated on the stack into which is stored all the registers
5901 that might possibly be used for returning the result of a
5902 function. ARGUMENTS is the value returned by
5903 __builtin_apply_args. ARGSIZE is the number of bytes of
5904 arguments that must be copied. ??? How should this value be
5905 computed? We'll also need a safe worst case value for varargs
5906 functions. */
5907 case BUILT_IN_APPLY:
5908 if (!validate_arglist (arglist, POINTER_TYPE,
5909 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5910 && !validate_arglist (arglist, REFERENCE_TYPE,
5911 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5912 return const0_rtx;
5913 else
5914 {
5915 int i;
5916 tree t;
5917 rtx ops[3];
5918
5919 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5920 ops[i] = expand_normal (TREE_VALUE (t));
5921
5922 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5923 }
5924
5925 /* __builtin_return (RESULT) causes the function to return the
5926 value described by RESULT. RESULT is address of the block of
5927 memory returned by __builtin_apply. */
5928 case BUILT_IN_RETURN:
5929 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5930 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5931 return const0_rtx;
5932
5933 case BUILT_IN_SAVEREGS:
5934 return expand_builtin_saveregs ();
5935
5936 case BUILT_IN_ARGS_INFO:
5937 return expand_builtin_args_info (arglist);
5938
5939 /* Return the address of the first anonymous stack arg. */
5940 case BUILT_IN_NEXT_ARG:
5941 if (fold_builtin_next_arg (arglist))
5942 return const0_rtx;
5943 return expand_builtin_next_arg ();
5944
5945 case BUILT_IN_CLASSIFY_TYPE:
5946 return expand_builtin_classify_type (arglist);
5947
5948 case BUILT_IN_CONSTANT_P:
5949 return const0_rtx;
5950
5951 case BUILT_IN_FRAME_ADDRESS:
5952 case BUILT_IN_RETURN_ADDRESS:
5953 return expand_builtin_frame_address (fndecl, arglist);
5954
5955 /* Returns the address of the area where the structure is returned.
5956 0 otherwise. */
5957 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5958 if (arglist != 0
5959 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5960 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5961 return const0_rtx;
5962 else
5963 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5964
5965 case BUILT_IN_ALLOCA:
5966 target = expand_builtin_alloca (arglist, target);
5967 if (target)
5968 return target;
5969 break;
5970
5971 case BUILT_IN_STACK_SAVE:
5972 return expand_stack_save ();
5973
5974 case BUILT_IN_STACK_RESTORE:
5975 expand_stack_restore (TREE_VALUE (arglist));
5976 return const0_rtx;
5977
5978 case BUILT_IN_BSWAP32:
5979 case BUILT_IN_BSWAP64:
5980 target = expand_builtin_bswap (arglist, target, subtarget);
5981
5982 if (target)
5983 return target;
5984 break;
5985
5986 CASE_INT_FN (BUILT_IN_FFS):
5987 case BUILT_IN_FFSIMAX:
5988 target = expand_builtin_unop (target_mode, arglist, target,
5989 subtarget, ffs_optab);
5990 if (target)
5991 return target;
5992 break;
5993
5994 CASE_INT_FN (BUILT_IN_CLZ):
5995 case BUILT_IN_CLZIMAX:
5996 target = expand_builtin_unop (target_mode, arglist, target,
5997 subtarget, clz_optab);
5998 if (target)
5999 return target;
6000 break;
6001
6002 CASE_INT_FN (BUILT_IN_CTZ):
6003 case BUILT_IN_CTZIMAX:
6004 target = expand_builtin_unop (target_mode, arglist, target,
6005 subtarget, ctz_optab);
6006 if (target)
6007 return target;
6008 break;
6009
6010 CASE_INT_FN (BUILT_IN_POPCOUNT):
6011 case BUILT_IN_POPCOUNTIMAX:
6012 target = expand_builtin_unop (target_mode, arglist, target,
6013 subtarget, popcount_optab);
6014 if (target)
6015 return target;
6016 break;
6017
6018 CASE_INT_FN (BUILT_IN_PARITY):
6019 case BUILT_IN_PARITYIMAX:
6020 target = expand_builtin_unop (target_mode, arglist, target,
6021 subtarget, parity_optab);
6022 if (target)
6023 return target;
6024 break;
6025
6026 case BUILT_IN_STRLEN:
6027 target = expand_builtin_strlen (arglist, target, target_mode);
6028 if (target)
6029 return target;
6030 break;
6031
6032 case BUILT_IN_STRCPY:
6033 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
6034 if (target)
6035 return target;
6036 break;
6037
6038 case BUILT_IN_STRNCPY:
6039 target = expand_builtin_strncpy (exp, target, mode);
6040 if (target)
6041 return target;
6042 break;
6043
6044 case BUILT_IN_STPCPY:
6045 target = expand_builtin_stpcpy (exp, target, mode);
6046 if (target)
6047 return target;
6048 break;
6049
6050 case BUILT_IN_STRCAT:
6051 target = expand_builtin_strcat (fndecl, arglist, target, mode);
6052 if (target)
6053 return target;
6054 break;
6055
6056 case BUILT_IN_STRNCAT:
6057 target = expand_builtin_strncat (arglist, target, mode);
6058 if (target)
6059 return target;
6060 break;
6061
6062 case BUILT_IN_STRSPN:
6063 target = expand_builtin_strspn (arglist, target, mode);
6064 if (target)
6065 return target;
6066 break;
6067
6068 case BUILT_IN_STRCSPN:
6069 target = expand_builtin_strcspn (arglist, target, mode);
6070 if (target)
6071 return target;
6072 break;
6073
6074 case BUILT_IN_STRSTR:
6075 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6076 if (target)
6077 return target;
6078 break;
6079
6080 case BUILT_IN_STRPBRK:
6081 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6082 if (target)
6083 return target;
6084 break;
6085
6086 case BUILT_IN_INDEX:
6087 case BUILT_IN_STRCHR:
6088 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6089 if (target)
6090 return target;
6091 break;
6092
6093 case BUILT_IN_RINDEX:
6094 case BUILT_IN_STRRCHR:
6095 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6096 if (target)
6097 return target;
6098 break;
6099
6100 case BUILT_IN_MEMCPY:
6101 target = expand_builtin_memcpy (exp, target, mode);
6102 if (target)
6103 return target;
6104 break;
6105
6106 case BUILT_IN_MEMPCPY:
6107 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6108 if (target)
6109 return target;
6110 break;
6111
6112 case BUILT_IN_MEMMOVE:
6113 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6114 mode);
6115 if (target)
6116 return target;
6117 break;
6118
6119 case BUILT_IN_BCOPY:
6120 target = expand_builtin_bcopy (exp);
6121 if (target)
6122 return target;
6123 break;
6124
6125 case BUILT_IN_MEMSET:
6126 target = expand_builtin_memset (arglist, target, mode, exp);
6127 if (target)
6128 return target;
6129 break;
6130
6131 case BUILT_IN_BZERO:
6132 target = expand_builtin_bzero (exp);
6133 if (target)
6134 return target;
6135 break;
6136
6137 case BUILT_IN_STRCMP:
6138 target = expand_builtin_strcmp (exp, target, mode);
6139 if (target)
6140 return target;
6141 break;
6142
6143 case BUILT_IN_STRNCMP:
6144 target = expand_builtin_strncmp (exp, target, mode);
6145 if (target)
6146 return target;
6147 break;
6148
6149 case BUILT_IN_BCMP:
6150 case BUILT_IN_MEMCMP:
6151 target = expand_builtin_memcmp (exp, arglist, target, mode);
6152 if (target)
6153 return target;
6154 break;
6155
6156 case BUILT_IN_SETJMP:
6157 /* This should have been lowered to the builtins below. */
6158 gcc_unreachable ();
6159
6160 case BUILT_IN_SETJMP_SETUP:
6161 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6162 and the receiver label. */
6163 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6164 {
6165 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6166 VOIDmode, EXPAND_NORMAL);
6167 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6168 rtx label_r = label_rtx (label);
6169
6170 /* This is copied from the handling of non-local gotos. */
6171 expand_builtin_setjmp_setup (buf_addr, label_r);
6172 nonlocal_goto_handler_labels
6173 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6174 nonlocal_goto_handler_labels);
6175 /* ??? Do not let expand_label treat us as such since we would
6176 not want to be both on the list of non-local labels and on
6177 the list of forced labels. */
6178 FORCED_LABEL (label) = 0;
6179 return const0_rtx;
6180 }
6181 break;
6182
6183 case BUILT_IN_SETJMP_DISPATCHER:
6184 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6185 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6186 {
6187 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6188 rtx label_r = label_rtx (label);
6189
6190 /* Remove the dispatcher label from the list of non-local labels
6191 since the receiver labels have been added to it above. */
6192 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6193 return const0_rtx;
6194 }
6195 break;
6196
6197 case BUILT_IN_SETJMP_RECEIVER:
6198 /* __builtin_setjmp_receiver is passed the receiver label. */
6199 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6200 {
6201 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6202 rtx label_r = label_rtx (label);
6203
6204 expand_builtin_setjmp_receiver (label_r);
6205 return const0_rtx;
6206 }
6207 break;
6208
6209 /* __builtin_longjmp is passed a pointer to an array of five words.
6210 It's similar to the C library longjmp function but works with
6211 __builtin_setjmp above. */
6212 case BUILT_IN_LONGJMP:
6213 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6214 {
6215 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6216 VOIDmode, EXPAND_NORMAL);
6217 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6218
6219 if (value != const1_rtx)
6220 {
6221 error ("%<__builtin_longjmp%> second argument must be 1");
6222 return const0_rtx;
6223 }
6224
6225 expand_builtin_longjmp (buf_addr, value);
6226 return const0_rtx;
6227 }
6228 break;
6229
6230 case BUILT_IN_NONLOCAL_GOTO:
6231 target = expand_builtin_nonlocal_goto (arglist);
6232 if (target)
6233 return target;
6234 break;
6235
6236 /* This updates the setjmp buffer that is its argument with the value
6237 of the current stack pointer. */
6238 case BUILT_IN_UPDATE_SETJMP_BUF:
6239 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6240 {
6241 rtx buf_addr
6242 = expand_normal (TREE_VALUE (arglist));
6243
6244 expand_builtin_update_setjmp_buf (buf_addr);
6245 return const0_rtx;
6246 }
6247 break;
6248
6249 case BUILT_IN_TRAP:
6250 expand_builtin_trap ();
6251 return const0_rtx;
6252
6253 case BUILT_IN_PRINTF:
6254 target = expand_builtin_printf (exp, target, mode, false);
6255 if (target)
6256 return target;
6257 break;
6258
6259 case BUILT_IN_PRINTF_UNLOCKED:
6260 target = expand_builtin_printf (exp, target, mode, true);
6261 if (target)
6262 return target;
6263 break;
6264
6265 case BUILT_IN_FPUTS:
6266 target = expand_builtin_fputs (arglist, target, false);
6267 if (target)
6268 return target;
6269 break;
6270 case BUILT_IN_FPUTS_UNLOCKED:
6271 target = expand_builtin_fputs (arglist, target, true);
6272 if (target)
6273 return target;
6274 break;
6275
6276 case BUILT_IN_FPRINTF:
6277 target = expand_builtin_fprintf (exp, target, mode, false);
6278 if (target)
6279 return target;
6280 break;
6281
6282 case BUILT_IN_FPRINTF_UNLOCKED:
6283 target = expand_builtin_fprintf (exp, target, mode, true);
6284 if (target)
6285 return target;
6286 break;
6287
6288 case BUILT_IN_SPRINTF:
6289 target = expand_builtin_sprintf (arglist, target, mode);
6290 if (target)
6291 return target;
6292 break;
6293
6294 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6295 target = expand_builtin_signbit (exp, target);
6296 if (target)
6297 return target;
6298 break;
6299
6300 /* Various hooks for the DWARF 2 __throw routine. */
6301 case BUILT_IN_UNWIND_INIT:
6302 expand_builtin_unwind_init ();
6303 return const0_rtx;
6304 case BUILT_IN_DWARF_CFA:
6305 return virtual_cfa_rtx;
6306 #ifdef DWARF2_UNWIND_INFO
6307 case BUILT_IN_DWARF_SP_COLUMN:
6308 return expand_builtin_dwarf_sp_column ();
6309 case BUILT_IN_INIT_DWARF_REG_SIZES:
6310 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6311 return const0_rtx;
6312 #endif
6313 case BUILT_IN_FROB_RETURN_ADDR:
6314 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6315 case BUILT_IN_EXTRACT_RETURN_ADDR:
6316 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6317 case BUILT_IN_EH_RETURN:
6318 expand_builtin_eh_return (TREE_VALUE (arglist),
6319 TREE_VALUE (TREE_CHAIN (arglist)));
6320 return const0_rtx;
6321 #ifdef EH_RETURN_DATA_REGNO
6322 case BUILT_IN_EH_RETURN_DATA_REGNO:
6323 return expand_builtin_eh_return_data_regno (arglist);
6324 #endif
6325 case BUILT_IN_EXTEND_POINTER:
6326 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6327
6328 case BUILT_IN_VA_START:
6329 case BUILT_IN_STDARG_START:
6330 return expand_builtin_va_start (arglist);
6331 case BUILT_IN_VA_END:
6332 return expand_builtin_va_end (arglist);
6333 case BUILT_IN_VA_COPY:
6334 return expand_builtin_va_copy (arglist);
6335 case BUILT_IN_EXPECT:
6336 return expand_builtin_expect (arglist, target);
6337 case BUILT_IN_PREFETCH:
6338 expand_builtin_prefetch (arglist);
6339 return const0_rtx;
6340
6341 case BUILT_IN_PROFILE_FUNC_ENTER:
6342 return expand_builtin_profile_func (false);
6343 case BUILT_IN_PROFILE_FUNC_EXIT:
6344 return expand_builtin_profile_func (true);
6345
6346 case BUILT_IN_INIT_TRAMPOLINE:
6347 return expand_builtin_init_trampoline (arglist);
6348 case BUILT_IN_ADJUST_TRAMPOLINE:
6349 return expand_builtin_adjust_trampoline (arglist);
6350
6351 case BUILT_IN_FORK:
6352 case BUILT_IN_EXECL:
6353 case BUILT_IN_EXECV:
6354 case BUILT_IN_EXECLP:
6355 case BUILT_IN_EXECLE:
6356 case BUILT_IN_EXECVP:
6357 case BUILT_IN_EXECVE:
6358 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6359 if (target)
6360 return target;
6361 break;
6362
6363 case BUILT_IN_FETCH_AND_ADD_1:
6364 case BUILT_IN_FETCH_AND_ADD_2:
6365 case BUILT_IN_FETCH_AND_ADD_4:
6366 case BUILT_IN_FETCH_AND_ADD_8:
6367 case BUILT_IN_FETCH_AND_ADD_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6369 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6370 false, target, ignore);
6371 if (target)
6372 return target;
6373 break;
6374
6375 case BUILT_IN_FETCH_AND_SUB_1:
6376 case BUILT_IN_FETCH_AND_SUB_2:
6377 case BUILT_IN_FETCH_AND_SUB_4:
6378 case BUILT_IN_FETCH_AND_SUB_8:
6379 case BUILT_IN_FETCH_AND_SUB_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6381 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6382 false, target, ignore);
6383 if (target)
6384 return target;
6385 break;
6386
6387 case BUILT_IN_FETCH_AND_OR_1:
6388 case BUILT_IN_FETCH_AND_OR_2:
6389 case BUILT_IN_FETCH_AND_OR_4:
6390 case BUILT_IN_FETCH_AND_OR_8:
6391 case BUILT_IN_FETCH_AND_OR_16:
6392 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6393 target = expand_builtin_sync_operation (mode, arglist, IOR,
6394 false, target, ignore);
6395 if (target)
6396 return target;
6397 break;
6398
6399 case BUILT_IN_FETCH_AND_AND_1:
6400 case BUILT_IN_FETCH_AND_AND_2:
6401 case BUILT_IN_FETCH_AND_AND_4:
6402 case BUILT_IN_FETCH_AND_AND_8:
6403 case BUILT_IN_FETCH_AND_AND_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6405 target = expand_builtin_sync_operation (mode, arglist, AND,
6406 false, target, ignore);
6407 if (target)
6408 return target;
6409 break;
6410
6411 case BUILT_IN_FETCH_AND_XOR_1:
6412 case BUILT_IN_FETCH_AND_XOR_2:
6413 case BUILT_IN_FETCH_AND_XOR_4:
6414 case BUILT_IN_FETCH_AND_XOR_8:
6415 case BUILT_IN_FETCH_AND_XOR_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6417 target = expand_builtin_sync_operation (mode, arglist, XOR,
6418 false, target, ignore);
6419 if (target)
6420 return target;
6421 break;
6422
6423 case BUILT_IN_FETCH_AND_NAND_1:
6424 case BUILT_IN_FETCH_AND_NAND_2:
6425 case BUILT_IN_FETCH_AND_NAND_4:
6426 case BUILT_IN_FETCH_AND_NAND_8:
6427 case BUILT_IN_FETCH_AND_NAND_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6429 target = expand_builtin_sync_operation (mode, arglist, NOT,
6430 false, target, ignore);
6431 if (target)
6432 return target;
6433 break;
6434
6435 case BUILT_IN_ADD_AND_FETCH_1:
6436 case BUILT_IN_ADD_AND_FETCH_2:
6437 case BUILT_IN_ADD_AND_FETCH_4:
6438 case BUILT_IN_ADD_AND_FETCH_8:
6439 case BUILT_IN_ADD_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6442 true, target, ignore);
6443 if (target)
6444 return target;
6445 break;
6446
6447 case BUILT_IN_SUB_AND_FETCH_1:
6448 case BUILT_IN_SUB_AND_FETCH_2:
6449 case BUILT_IN_SUB_AND_FETCH_4:
6450 case BUILT_IN_SUB_AND_FETCH_8:
6451 case BUILT_IN_SUB_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6454 true, target, ignore);
6455 if (target)
6456 return target;
6457 break;
6458
6459 case BUILT_IN_OR_AND_FETCH_1:
6460 case BUILT_IN_OR_AND_FETCH_2:
6461 case BUILT_IN_OR_AND_FETCH_4:
6462 case BUILT_IN_OR_AND_FETCH_8:
6463 case BUILT_IN_OR_AND_FETCH_16:
6464 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6465 target = expand_builtin_sync_operation (mode, arglist, IOR,
6466 true, target, ignore);
6467 if (target)
6468 return target;
6469 break;
6470
6471 case BUILT_IN_AND_AND_FETCH_1:
6472 case BUILT_IN_AND_AND_FETCH_2:
6473 case BUILT_IN_AND_AND_FETCH_4:
6474 case BUILT_IN_AND_AND_FETCH_8:
6475 case BUILT_IN_AND_AND_FETCH_16:
6476 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6477 target = expand_builtin_sync_operation (mode, arglist, AND,
6478 true, target, ignore);
6479 if (target)
6480 return target;
6481 break;
6482
6483 case BUILT_IN_XOR_AND_FETCH_1:
6484 case BUILT_IN_XOR_AND_FETCH_2:
6485 case BUILT_IN_XOR_AND_FETCH_4:
6486 case BUILT_IN_XOR_AND_FETCH_8:
6487 case BUILT_IN_XOR_AND_FETCH_16:
6488 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6489 target = expand_builtin_sync_operation (mode, arglist, XOR,
6490 true, target, ignore);
6491 if (target)
6492 return target;
6493 break;
6494
6495 case BUILT_IN_NAND_AND_FETCH_1:
6496 case BUILT_IN_NAND_AND_FETCH_2:
6497 case BUILT_IN_NAND_AND_FETCH_4:
6498 case BUILT_IN_NAND_AND_FETCH_8:
6499 case BUILT_IN_NAND_AND_FETCH_16:
6500 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6501 target = expand_builtin_sync_operation (mode, arglist, NOT,
6502 true, target, ignore);
6503 if (target)
6504 return target;
6505 break;
6506
6507 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6508 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6509 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6510 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6511 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6512 if (mode == VOIDmode)
6513 mode = TYPE_MODE (boolean_type_node);
6514 if (!target || !register_operand (target, mode))
6515 target = gen_reg_rtx (mode);
6516
6517 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6518 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6519 if (target)
6520 return target;
6521 break;
6522
6523 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6524 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6525 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6526 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6527 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6528 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6529 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6530 if (target)
6531 return target;
6532 break;
6533
6534 case BUILT_IN_LOCK_TEST_AND_SET_1:
6535 case BUILT_IN_LOCK_TEST_AND_SET_2:
6536 case BUILT_IN_LOCK_TEST_AND_SET_4:
6537 case BUILT_IN_LOCK_TEST_AND_SET_8:
6538 case BUILT_IN_LOCK_TEST_AND_SET_16:
6539 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6540 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6541 if (target)
6542 return target;
6543 break;
6544
6545 case BUILT_IN_LOCK_RELEASE_1:
6546 case BUILT_IN_LOCK_RELEASE_2:
6547 case BUILT_IN_LOCK_RELEASE_4:
6548 case BUILT_IN_LOCK_RELEASE_8:
6549 case BUILT_IN_LOCK_RELEASE_16:
6550 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6551 expand_builtin_lock_release (mode, arglist);
6552 return const0_rtx;
6553
6554 case BUILT_IN_SYNCHRONIZE:
6555 expand_builtin_synchronize ();
6556 return const0_rtx;
6557
6558 case BUILT_IN_OBJECT_SIZE:
6559 return expand_builtin_object_size (exp);
6560
6561 case BUILT_IN_MEMCPY_CHK:
6562 case BUILT_IN_MEMPCPY_CHK:
6563 case BUILT_IN_MEMMOVE_CHK:
6564 case BUILT_IN_MEMSET_CHK:
6565 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6566 if (target)
6567 return target;
6568 break;
6569
6570 case BUILT_IN_STRCPY_CHK:
6571 case BUILT_IN_STPCPY_CHK:
6572 case BUILT_IN_STRNCPY_CHK:
6573 case BUILT_IN_STRCAT_CHK:
6574 case BUILT_IN_STRNCAT_CHK:
6575 case BUILT_IN_SNPRINTF_CHK:
6576 case BUILT_IN_VSNPRINTF_CHK:
6577 maybe_emit_chk_warning (exp, fcode);
6578 break;
6579
6580 case BUILT_IN_SPRINTF_CHK:
6581 case BUILT_IN_VSPRINTF_CHK:
6582 maybe_emit_sprintf_chk_warning (exp, fcode);
6583 break;
6584
6585 default: /* just do library call, if unknown builtin */
6586 break;
6587 }
6588
6589 /* The switch statement above can drop through to cause the function
6590 to be called normally. */
6591 return expand_call (exp, target, ignore);
6592 }
6593
6594 /* Determine whether a tree node represents a call to a built-in
6595 function. If the tree T is a call to a built-in function with
6596 the right number of arguments of the appropriate types, return
6597 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6598 Otherwise the return value is END_BUILTINS. */
6599
6600 enum built_in_function
6601 builtin_mathfn_code (tree t)
6602 {
6603 tree fndecl, arglist, parmlist;
6604 tree argtype, parmtype;
6605
6606 if (TREE_CODE (t) != CALL_EXPR
6607 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6608 return END_BUILTINS;
6609
6610 fndecl = get_callee_fndecl (t);
6611 if (fndecl == NULL_TREE
6612 || TREE_CODE (fndecl) != FUNCTION_DECL
6613 || ! DECL_BUILT_IN (fndecl)
6614 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6615 return END_BUILTINS;
6616
6617 arglist = TREE_OPERAND (t, 1);
6618 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6619 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6620 {
6621 /* If a function doesn't take a variable number of arguments,
6622 the last element in the list will have type `void'. */
6623 parmtype = TREE_VALUE (parmlist);
6624 if (VOID_TYPE_P (parmtype))
6625 {
6626 if (arglist)
6627 return END_BUILTINS;
6628 return DECL_FUNCTION_CODE (fndecl);
6629 }
6630
6631 if (! arglist)
6632 return END_BUILTINS;
6633
6634 argtype = TREE_TYPE (TREE_VALUE (arglist));
6635
6636 if (SCALAR_FLOAT_TYPE_P (parmtype))
6637 {
6638 if (! SCALAR_FLOAT_TYPE_P (argtype))
6639 return END_BUILTINS;
6640 }
6641 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6642 {
6643 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6644 return END_BUILTINS;
6645 }
6646 else if (POINTER_TYPE_P (parmtype))
6647 {
6648 if (! POINTER_TYPE_P (argtype))
6649 return END_BUILTINS;
6650 }
6651 else if (INTEGRAL_TYPE_P (parmtype))
6652 {
6653 if (! INTEGRAL_TYPE_P (argtype))
6654 return END_BUILTINS;
6655 }
6656 else
6657 return END_BUILTINS;
6658
6659 arglist = TREE_CHAIN (arglist);
6660 }
6661
6662 /* Variable-length argument list. */
6663 return DECL_FUNCTION_CODE (fndecl);
6664 }
6665
6666 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6667 constant. ARGLIST is the argument list of the call. */
6668
6669 static tree
6670 fold_builtin_constant_p (tree arglist)
6671 {
6672 if (arglist == 0)
6673 return 0;
6674
6675 arglist = TREE_VALUE (arglist);
6676
6677 /* We return 1 for a numeric type that's known to be a constant
6678 value at compile-time or for an aggregate type that's a
6679 literal constant. */
6680 STRIP_NOPS (arglist);
6681
6682 /* If we know this is a constant, emit the constant of one. */
6683 if (CONSTANT_CLASS_P (arglist)
6684 || (TREE_CODE (arglist) == CONSTRUCTOR
6685 && TREE_CONSTANT (arglist)))
6686 return integer_one_node;
6687 if (TREE_CODE (arglist) == ADDR_EXPR)
6688 {
6689 tree op = TREE_OPERAND (arglist, 0);
6690 if (TREE_CODE (op) == STRING_CST
6691 || (TREE_CODE (op) == ARRAY_REF
6692 && integer_zerop (TREE_OPERAND (op, 1))
6693 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6694 return integer_one_node;
6695 }
6696
6697 /* If this expression has side effects, show we don't know it to be a
6698 constant. Likewise if it's a pointer or aggregate type since in
6699 those case we only want literals, since those are only optimized
6700 when generating RTL, not later.
6701 And finally, if we are compiling an initializer, not code, we
6702 need to return a definite result now; there's not going to be any
6703 more optimization done. */
6704 if (TREE_SIDE_EFFECTS (arglist)
6705 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6706 || POINTER_TYPE_P (TREE_TYPE (arglist))
6707 || cfun == 0
6708 || folding_initializer)
6709 return integer_zero_node;
6710
6711 return 0;
6712 }
6713
6714 /* Fold a call to __builtin_expect, if we expect that a comparison against
6715 the argument will fold to a constant. In practice, this means a true
6716 constant or the address of a non-weak symbol. ARGLIST is the argument
6717 list of the call. */
6718
6719 static tree
6720 fold_builtin_expect (tree arglist)
6721 {
6722 tree arg, inner;
6723
6724 if (arglist == 0)
6725 return 0;
6726
6727 arg = TREE_VALUE (arglist);
6728
6729 /* If the argument isn't invariant, then there's nothing we can do. */
6730 if (!TREE_INVARIANT (arg))
6731 return 0;
6732
6733 /* If we're looking at an address of a weak decl, then do not fold. */
6734 inner = arg;
6735 STRIP_NOPS (inner);
6736 if (TREE_CODE (inner) == ADDR_EXPR)
6737 {
6738 do
6739 {
6740 inner = TREE_OPERAND (inner, 0);
6741 }
6742 while (TREE_CODE (inner) == COMPONENT_REF
6743 || TREE_CODE (inner) == ARRAY_REF);
6744 if (DECL_P (inner) && DECL_WEAK (inner))
6745 return 0;
6746 }
6747
6748 /* Otherwise, ARG already has the proper type for the return value. */
6749 return arg;
6750 }
6751
6752 /* Fold a call to __builtin_classify_type. */
6753
6754 static tree
6755 fold_builtin_classify_type (tree arglist)
6756 {
6757 if (arglist == 0)
6758 return build_int_cst (NULL_TREE, no_type_class);
6759
6760 return build_int_cst (NULL_TREE,
6761 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6762 }
6763
6764 /* Fold a call to __builtin_strlen. */
6765
6766 static tree
6767 fold_builtin_strlen (tree arglist)
6768 {
6769 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6770 return NULL_TREE;
6771 else
6772 {
6773 tree len = c_strlen (TREE_VALUE (arglist), 0);
6774
6775 if (len)
6776 {
6777 /* Convert from the internal "sizetype" type to "size_t". */
6778 if (size_type_node)
6779 len = fold_convert (size_type_node, len);
6780 return len;
6781 }
6782
6783 return NULL_TREE;
6784 }
6785 }
6786
6787 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6788
6789 static tree
6790 fold_builtin_inf (tree type, int warn)
6791 {
6792 REAL_VALUE_TYPE real;
6793
6794 /* __builtin_inff is intended to be usable to define INFINITY on all
6795 targets. If an infinity is not available, INFINITY expands "to a
6796 positive constant of type float that overflows at translation
6797 time", footnote "In this case, using INFINITY will violate the
6798 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6799 Thus we pedwarn to ensure this constraint violation is
6800 diagnosed. */
6801 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6802 pedwarn ("target format does not support infinity");
6803
6804 real_inf (&real);
6805 return build_real (type, real);
6806 }
6807
6808 /* Fold a call to __builtin_nan or __builtin_nans. */
6809
6810 static tree
6811 fold_builtin_nan (tree arglist, tree type, int quiet)
6812 {
6813 REAL_VALUE_TYPE real;
6814 const char *str;
6815
6816 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6817 return 0;
6818 str = c_getstr (TREE_VALUE (arglist));
6819 if (!str)
6820 return 0;
6821
6822 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6823 return 0;
6824
6825 return build_real (type, real);
6826 }
6827
6828 /* Return true if the floating point expression T has an integer value.
6829 We also allow +Inf, -Inf and NaN to be considered integer values. */
6830
6831 static bool
6832 integer_valued_real_p (tree t)
6833 {
6834 switch (TREE_CODE (t))
6835 {
6836 case FLOAT_EXPR:
6837 return true;
6838
6839 case ABS_EXPR:
6840 case SAVE_EXPR:
6841 case NON_LVALUE_EXPR:
6842 return integer_valued_real_p (TREE_OPERAND (t, 0));
6843
6844 case COMPOUND_EXPR:
6845 case GIMPLE_MODIFY_STMT:
6846 case BIND_EXPR:
6847 return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1));
6848
6849 case PLUS_EXPR:
6850 case MINUS_EXPR:
6851 case MULT_EXPR:
6852 case MIN_EXPR:
6853 case MAX_EXPR:
6854 return integer_valued_real_p (TREE_OPERAND (t, 0))
6855 && integer_valued_real_p (TREE_OPERAND (t, 1));
6856
6857 case COND_EXPR:
6858 return integer_valued_real_p (TREE_OPERAND (t, 1))
6859 && integer_valued_real_p (TREE_OPERAND (t, 2));
6860
6861 case REAL_CST:
6862 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6863
6864 case NOP_EXPR:
6865 {
6866 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6867 if (TREE_CODE (type) == INTEGER_TYPE)
6868 return true;
6869 if (TREE_CODE (type) == REAL_TYPE)
6870 return integer_valued_real_p (TREE_OPERAND (t, 0));
6871 break;
6872 }
6873
6874 case CALL_EXPR:
6875 switch (builtin_mathfn_code (t))
6876 {
6877 CASE_FLT_FN (BUILT_IN_CEIL):
6878 CASE_FLT_FN (BUILT_IN_FLOOR):
6879 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6880 CASE_FLT_FN (BUILT_IN_RINT):
6881 CASE_FLT_FN (BUILT_IN_ROUND):
6882 CASE_FLT_FN (BUILT_IN_TRUNC):
6883 return true;
6884
6885 CASE_FLT_FN (BUILT_IN_FMIN):
6886 CASE_FLT_FN (BUILT_IN_FMAX):
6887 return integer_valued_real_p (TREE_VALUE (TREE_OPERAND (t, 1)))
6888 && integer_valued_real_p (TREE_VALUE (TREE_CHAIN (TREE_OPERAND (t, 1))));
6889
6890 default:
6891 break;
6892 }
6893 break;
6894
6895 default:
6896 break;
6897 }
6898 return false;
6899 }
6900
6901 /* EXP is assumed to be builtin call where truncation can be propagated
6902 across (for instance floor((double)f) == (double)floorf (f).
6903 Do the transformation. */
6904
6905 static tree
6906 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6907 {
6908 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6909 tree arg;
6910
6911 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6912 return 0;
6913
6914 arg = TREE_VALUE (arglist);
6915 /* Integer rounding functions are idempotent. */
6916 if (fcode == builtin_mathfn_code (arg))
6917 return arg;
6918
6919 /* If argument is already integer valued, and we don't need to worry
6920 about setting errno, there's no need to perform rounding. */
6921 if (! flag_errno_math && integer_valued_real_p (arg))
6922 return arg;
6923
6924 if (optimize)
6925 {
6926 tree arg0 = strip_float_extensions (arg);
6927 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6928 tree newtype = TREE_TYPE (arg0);
6929 tree decl;
6930
6931 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6932 && (decl = mathfn_built_in (newtype, fcode)))
6933 {
6934 arglist =
6935 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6936 return fold_convert (ftype,
6937 build_function_call_expr (decl, arglist));
6938 }
6939 }
6940 return 0;
6941 }
6942
6943 /* EXP is assumed to be builtin call which can narrow the FP type of
6944 the argument, for instance lround((double)f) -> lroundf (f). */
6945
6946 static tree
6947 fold_fixed_mathfn (tree fndecl, tree arglist)
6948 {
6949 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6950 tree arg;
6951
6952 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6953 return 0;
6954
6955 arg = TREE_VALUE (arglist);
6956
6957 /* If argument is already integer valued, and we don't need to worry
6958 about setting errno, there's no need to perform rounding. */
6959 if (! flag_errno_math && integer_valued_real_p (arg))
6960 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6961
6962 if (optimize)
6963 {
6964 tree ftype = TREE_TYPE (arg);
6965 tree arg0 = strip_float_extensions (arg);
6966 tree newtype = TREE_TYPE (arg0);
6967 tree decl;
6968
6969 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6970 && (decl = mathfn_built_in (newtype, fcode)))
6971 {
6972 arglist =
6973 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6974 return build_function_call_expr (decl, arglist);
6975 }
6976 }
6977
6978 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6979 sizeof (long long) == sizeof (long). */
6980 if (TYPE_PRECISION (long_long_integer_type_node)
6981 == TYPE_PRECISION (long_integer_type_node))
6982 {
6983 tree newfn = NULL_TREE;
6984 switch (fcode)
6985 {
6986 CASE_FLT_FN (BUILT_IN_LLCEIL):
6987 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6988 break;
6989
6990 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6991 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6992 break;
6993
6994 CASE_FLT_FN (BUILT_IN_LLROUND):
6995 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6996 break;
6997
6998 CASE_FLT_FN (BUILT_IN_LLRINT):
6999 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7000 break;
7001
7002 default:
7003 break;
7004 }
7005
7006 if (newfn)
7007 {
7008 tree newcall = build_function_call_expr (newfn, arglist);
7009 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7010 }
7011 }
7012
7013 return 0;
7014 }
7015
7016 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
7017 is the argument list, TYPE is the return type and FNDECL is the
7018 original function DECL. Return NULL_TREE if no if no simplification
7019 can be made. */
7020
7021 static tree
7022 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
7023 {
7024 tree arg;
7025
7026 if (!arglist || TREE_CHAIN (arglist))
7027 return NULL_TREE;
7028
7029 arg = TREE_VALUE (arglist);
7030 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7031 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7032 return NULL_TREE;
7033
7034 /* Evaluate cabs of a constant at compile-time. */
7035 if (flag_unsafe_math_optimizations
7036 && TREE_CODE (arg) == COMPLEX_CST
7037 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
7038 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
7039 && !TREE_OVERFLOW (TREE_REALPART (arg))
7040 && !TREE_OVERFLOW (TREE_IMAGPART (arg)))
7041 {
7042 REAL_VALUE_TYPE r, i;
7043
7044 r = TREE_REAL_CST (TREE_REALPART (arg));
7045 i = TREE_REAL_CST (TREE_IMAGPART (arg));
7046
7047 real_arithmetic (&r, MULT_EXPR, &r, &r);
7048 real_arithmetic (&i, MULT_EXPR, &i, &i);
7049 real_arithmetic (&r, PLUS_EXPR, &r, &i);
7050 if (real_sqrt (&r, TYPE_MODE (type), &r)
7051 || ! flag_trapping_math)
7052 return build_real (type, r);
7053 }
7054
7055 /* If either part is zero, cabs is fabs of the other. */
7056 if (TREE_CODE (arg) == COMPLEX_EXPR
7057 && real_zerop (TREE_OPERAND (arg, 0)))
7058 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
7059 if (TREE_CODE (arg) == COMPLEX_EXPR
7060 && real_zerop (TREE_OPERAND (arg, 1)))
7061 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
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 {
7067 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
7068 return build_function_call_expr (fndecl, arglist);
7069 }
7070
7071 /* Don't do this when optimizing for size. */
7072 if (flag_unsafe_math_optimizations
7073 && optimize && !optimize_size)
7074 {
7075 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7076
7077 if (sqrtfn != NULL_TREE)
7078 {
7079 tree rpart, ipart, result, arglist;
7080
7081 arg = builtin_save_expr (arg);
7082
7083 rpart = fold_build1 (REALPART_EXPR, type, arg);
7084 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7085
7086 rpart = builtin_save_expr (rpart);
7087 ipart = builtin_save_expr (ipart);
7088
7089 result = fold_build2 (PLUS_EXPR, type,
7090 fold_build2 (MULT_EXPR, type,
7091 rpart, rpart),
7092 fold_build2 (MULT_EXPR, type,
7093 ipart, ipart));
7094
7095 arglist = build_tree_list (NULL_TREE, result);
7096 return build_function_call_expr (sqrtfn, arglist);
7097 }
7098 }
7099
7100 return NULL_TREE;
7101 }
7102
7103 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7104 NULL_TREE if no simplification can be made. */
7105
7106 static tree
7107 fold_builtin_sqrt (tree arglist, tree type)
7108 {
7109
7110 enum built_in_function fcode;
7111 tree arg = TREE_VALUE (arglist);
7112
7113 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7114 return NULL_TREE;
7115
7116 /* Optimize sqrt of constant value. */
7117 if (TREE_CODE (arg) == REAL_CST
7118 && !TREE_OVERFLOW (arg))
7119 {
7120 REAL_VALUE_TYPE r, x;
7121
7122 x = TREE_REAL_CST (arg);
7123 if (real_sqrt (&r, TYPE_MODE (type), &x)
7124 || (!flag_trapping_math && !flag_errno_math))
7125 return build_real (type, r);
7126 }
7127
7128 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7129 fcode = builtin_mathfn_code (arg);
7130 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7131 {
7132 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7133 arg = fold_build2 (MULT_EXPR, type,
7134 TREE_VALUE (TREE_OPERAND (arg, 1)),
7135 build_real (type, dconsthalf));
7136 arglist = build_tree_list (NULL_TREE, arg);
7137 return build_function_call_expr (expfn, arglist);
7138 }
7139
7140 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7141 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7142 {
7143 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7144
7145 if (powfn)
7146 {
7147 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7148 tree tree_root;
7149 /* The inner root was either sqrt or cbrt. */
7150 REAL_VALUE_TYPE dconstroot =
7151 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7152
7153 /* Adjust for the outer root. */
7154 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7155 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7156 tree_root = build_real (type, dconstroot);
7157 arglist = tree_cons (NULL_TREE, arg0,
7158 build_tree_list (NULL_TREE, tree_root));
7159 return build_function_call_expr (powfn, arglist);
7160 }
7161 }
7162
7163 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7164 if (flag_unsafe_math_optimizations
7165 && (fcode == BUILT_IN_POW
7166 || fcode == BUILT_IN_POWF
7167 || fcode == BUILT_IN_POWL))
7168 {
7169 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7170 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7171 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7172 tree narg1;
7173 if (!tree_expr_nonnegative_p (arg0))
7174 arg0 = build1 (ABS_EXPR, type, arg0);
7175 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7176 build_real (type, dconsthalf));
7177 arglist = tree_cons (NULL_TREE, arg0,
7178 build_tree_list (NULL_TREE, narg1));
7179 return build_function_call_expr (powfn, arglist);
7180 }
7181
7182 return NULL_TREE;
7183 }
7184
7185 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7186 NULL_TREE if no simplification can be made. */
7187 static tree
7188 fold_builtin_cbrt (tree arglist, tree type)
7189 {
7190 tree arg = TREE_VALUE (arglist);
7191 const enum built_in_function fcode = builtin_mathfn_code (arg);
7192 tree res;
7193
7194 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7195 return NULL_TREE;
7196
7197 /* Calculate the result when the argument is a constant. */
7198 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7199 return res;
7200
7201 if (flag_unsafe_math_optimizations)
7202 {
7203 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7204 if (BUILTIN_EXPONENT_P (fcode))
7205 {
7206 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7207 const REAL_VALUE_TYPE third_trunc =
7208 real_value_truncate (TYPE_MODE (type), dconstthird);
7209 arg = fold_build2 (MULT_EXPR, type,
7210 TREE_VALUE (TREE_OPERAND (arg, 1)),
7211 build_real (type, third_trunc));
7212 arglist = build_tree_list (NULL_TREE, arg);
7213 return build_function_call_expr (expfn, arglist);
7214 }
7215
7216 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7217 if (BUILTIN_SQRT_P (fcode))
7218 {
7219 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7220
7221 if (powfn)
7222 {
7223 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7224 tree tree_root;
7225 REAL_VALUE_TYPE dconstroot = dconstthird;
7226
7227 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7228 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7229 tree_root = build_real (type, dconstroot);
7230 arglist = tree_cons (NULL_TREE, arg0,
7231 build_tree_list (NULL_TREE, tree_root));
7232 return build_function_call_expr (powfn, arglist);
7233 }
7234 }
7235
7236 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7237 if (BUILTIN_CBRT_P (fcode))
7238 {
7239 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7240 if (tree_expr_nonnegative_p (arg0))
7241 {
7242 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7243
7244 if (powfn)
7245 {
7246 tree tree_root;
7247 REAL_VALUE_TYPE dconstroot;
7248
7249 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7250 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7251 tree_root = build_real (type, dconstroot);
7252 arglist = tree_cons (NULL_TREE, arg0,
7253 build_tree_list (NULL_TREE, tree_root));
7254 return build_function_call_expr (powfn, arglist);
7255 }
7256 }
7257 }
7258
7259 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7260 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7261 || fcode == BUILT_IN_POWL)
7262 {
7263 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7264 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7265 if (tree_expr_nonnegative_p (arg00))
7266 {
7267 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7268 const REAL_VALUE_TYPE dconstroot
7269 = real_value_truncate (TYPE_MODE (type), dconstthird);
7270 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7271 build_real (type, dconstroot));
7272 arglist = tree_cons (NULL_TREE, arg00,
7273 build_tree_list (NULL_TREE, narg01));
7274 return build_function_call_expr (powfn, arglist);
7275 }
7276 }
7277 }
7278 return NULL_TREE;
7279 }
7280
7281 /* Fold function call to builtin cos, cosf, or cosl. Return
7282 NULL_TREE if no simplification can be made. */
7283 static tree
7284 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7285 {
7286 tree arg = TREE_VALUE (arglist);
7287 tree res, narg;
7288
7289 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7290 return NULL_TREE;
7291
7292 /* Calculate the result when the argument is a constant. */
7293 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7294 return res;
7295
7296 /* Optimize cos(-x) into cos (x). */
7297 if ((narg = fold_strip_sign_ops (arg)))
7298 return build_function_call_expr (fndecl,
7299 build_tree_list (NULL_TREE, narg));
7300
7301 return NULL_TREE;
7302 }
7303
7304 /* Fold function call to builtin cosh, coshf, or coshl. Return
7305 NULL_TREE if no simplification can be made. */
7306 static tree
7307 fold_builtin_cosh (tree arglist, tree type, tree fndecl)
7308 {
7309 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7310 {
7311 tree arg = TREE_VALUE (arglist);
7312 tree res, narg;
7313
7314 /* Calculate the result when the argument is a constant. */
7315 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7316 return res;
7317
7318 /* Optimize cosh(-x) into cosh (x). */
7319 if ((narg = fold_strip_sign_ops (arg)))
7320 return build_function_call_expr (fndecl,
7321 build_tree_list (NULL_TREE, narg));
7322 }
7323
7324 return NULL_TREE;
7325 }
7326
7327 /* Fold function call to builtin tan, tanf, or tanl. Return
7328 NULL_TREE if no simplification can be made. */
7329 static tree
7330 fold_builtin_tan (tree arglist, tree type)
7331 {
7332 enum built_in_function fcode;
7333 tree arg = TREE_VALUE (arglist);
7334 tree res;
7335
7336 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7337 return NULL_TREE;
7338
7339 /* Calculate the result when the argument is a constant. */
7340 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7341 return res;
7342
7343 /* Optimize tan(atan(x)) = x. */
7344 fcode = builtin_mathfn_code (arg);
7345 if (flag_unsafe_math_optimizations
7346 && (fcode == BUILT_IN_ATAN
7347 || fcode == BUILT_IN_ATANF
7348 || fcode == BUILT_IN_ATANL))
7349 return TREE_VALUE (TREE_OPERAND (arg, 1));
7350
7351 return NULL_TREE;
7352 }
7353
7354 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7355 NULL_TREE if no simplification can be made. */
7356
7357 static tree
7358 fold_builtin_sincos (tree arglist)
7359 {
7360 tree type, arg0, arg1, arg2;
7361 tree res, fn, call;
7362
7363 if (!validate_arglist (arglist, REAL_TYPE, POINTER_TYPE,
7364 POINTER_TYPE, VOID_TYPE))
7365 return NULL_TREE;
7366
7367 arg0 = TREE_VALUE (arglist);
7368 type = TREE_TYPE (arg0);
7369 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7370 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7371
7372 /* Calculate the result when the argument is a constant. */
7373 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7374 return res;
7375
7376 /* Canonicalize sincos to cexpi. */
7377 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7378 if (!fn)
7379 return NULL_TREE;
7380
7381 call = build_function_call_expr (fn, build_tree_list (NULL_TREE, arg0));
7382 call = builtin_save_expr (call);
7383
7384 return build2 (COMPOUND_EXPR, type,
7385 build2 (MODIFY_EXPR, void_type_node,
7386 build_fold_indirect_ref (arg1),
7387 build1 (IMAGPART_EXPR, type, call)),
7388 build2 (MODIFY_EXPR, void_type_node,
7389 build_fold_indirect_ref (arg2),
7390 build1 (REALPART_EXPR, type, call)));
7391 }
7392
7393 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7394 NULL_TREE if no simplification can be made. */
7395
7396 static tree
7397 fold_builtin_cexp (tree arglist, tree type)
7398 {
7399 tree arg0, rtype;
7400 tree realp, imagp, ifn;
7401
7402 if (!validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7403 return NULL_TREE;
7404
7405 arg0 = TREE_VALUE (arglist);
7406 rtype = TREE_TYPE (TREE_TYPE (arg0));
7407
7408 /* In case we can figure out the real part of arg0 and it is constant zero
7409 fold to cexpi. */
7410 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7411 if (!ifn)
7412 return NULL_TREE;
7413
7414 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
7415 && real_zerop (realp))
7416 {
7417 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
7418 return build_function_call_expr (ifn, build_tree_list (NULL_TREE, narg));
7419 }
7420
7421 /* In case we can easily decompose real and imaginary parts split cexp
7422 to exp (r) * cexpi (i). */
7423 if (flag_unsafe_math_optimizations
7424 && realp)
7425 {
7426 tree rfn, rcall, icall;
7427
7428 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7429 if (!rfn)
7430 return NULL_TREE;
7431
7432 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
7433 if (!imagp)
7434 return NULL_TREE;
7435
7436 icall = build_function_call_expr (ifn,
7437 build_tree_list (NULL_TREE, imagp));
7438 icall = builtin_save_expr (icall);
7439 rcall = build_function_call_expr (rfn,
7440 build_tree_list (NULL_TREE, realp));
7441 rcall = builtin_save_expr (rcall);
7442 return build2 (COMPLEX_EXPR, type,
7443 build2 (MULT_EXPR, rtype,
7444 rcall,
7445 build1 (REALPART_EXPR, rtype, icall)),
7446 build2 (MULT_EXPR, rtype,
7447 rcall,
7448 build1 (IMAGPART_EXPR, rtype, icall)));
7449 }
7450
7451 return NULL_TREE;
7452 }
7453
7454 /* Fold function call to builtin trunc, truncf or truncl. Return
7455 NULL_TREE if no simplification can be made. */
7456
7457 static tree
7458 fold_builtin_trunc (tree fndecl, tree arglist)
7459 {
7460 tree arg;
7461
7462 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7463 return 0;
7464
7465 /* Optimize trunc of constant value. */
7466 arg = TREE_VALUE (arglist);
7467 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7468 {
7469 REAL_VALUE_TYPE r, x;
7470 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7471
7472 x = TREE_REAL_CST (arg);
7473 real_trunc (&r, TYPE_MODE (type), &x);
7474 return build_real (type, r);
7475 }
7476
7477 return fold_trunc_transparent_mathfn (fndecl, arglist);
7478 }
7479
7480 /* Fold function call to builtin floor, floorf or floorl. Return
7481 NULL_TREE if no simplification can be made. */
7482
7483 static tree
7484 fold_builtin_floor (tree fndecl, tree arglist)
7485 {
7486 tree arg;
7487
7488 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7489 return 0;
7490
7491 /* Optimize floor of constant value. */
7492 arg = TREE_VALUE (arglist);
7493 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7494 {
7495 REAL_VALUE_TYPE x;
7496
7497 x = TREE_REAL_CST (arg);
7498 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7499 {
7500 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7501 REAL_VALUE_TYPE r;
7502
7503 real_floor (&r, TYPE_MODE (type), &x);
7504 return build_real (type, r);
7505 }
7506 }
7507
7508 /* Fold floor (x) where x is nonnegative to trunc (x). */
7509 if (tree_expr_nonnegative_p (arg))
7510 {
7511 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7512 if (truncfn)
7513 return build_function_call_expr (truncfn, arglist);
7514 }
7515
7516 return fold_trunc_transparent_mathfn (fndecl, arglist);
7517 }
7518
7519 /* Fold function call to builtin ceil, ceilf or ceill. Return
7520 NULL_TREE if no simplification can be made. */
7521
7522 static tree
7523 fold_builtin_ceil (tree fndecl, tree arglist)
7524 {
7525 tree arg;
7526
7527 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7528 return 0;
7529
7530 /* Optimize ceil of constant value. */
7531 arg = TREE_VALUE (arglist);
7532 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7533 {
7534 REAL_VALUE_TYPE x;
7535
7536 x = TREE_REAL_CST (arg);
7537 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7538 {
7539 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7540 REAL_VALUE_TYPE r;
7541
7542 real_ceil (&r, TYPE_MODE (type), &x);
7543 return build_real (type, r);
7544 }
7545 }
7546
7547 return fold_trunc_transparent_mathfn (fndecl, arglist);
7548 }
7549
7550 /* Fold function call to builtin round, roundf or roundl. Return
7551 NULL_TREE if no simplification can be made. */
7552
7553 static tree
7554 fold_builtin_round (tree fndecl, tree arglist)
7555 {
7556 tree arg;
7557
7558 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7559 return 0;
7560
7561 /* Optimize round of constant value. */
7562 arg = TREE_VALUE (arglist);
7563 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7564 {
7565 REAL_VALUE_TYPE x;
7566
7567 x = TREE_REAL_CST (arg);
7568 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7569 {
7570 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7571 REAL_VALUE_TYPE r;
7572
7573 real_round (&r, TYPE_MODE (type), &x);
7574 return build_real (type, r);
7575 }
7576 }
7577
7578 return fold_trunc_transparent_mathfn (fndecl, arglist);
7579 }
7580
7581 /* Fold function call to builtin lround, lroundf or lroundl (or the
7582 corresponding long long versions) and other rounding functions.
7583 Return NULL_TREE if no simplification can be made. */
7584
7585 static tree
7586 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7587 {
7588 tree arg;
7589
7590 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7591 return 0;
7592
7593 /* Optimize lround of constant value. */
7594 arg = TREE_VALUE (arglist);
7595 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7596 {
7597 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7598
7599 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7600 {
7601 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7602 tree ftype = TREE_TYPE (arg);
7603 unsigned HOST_WIDE_INT lo2;
7604 HOST_WIDE_INT hi, lo;
7605 REAL_VALUE_TYPE r;
7606
7607 switch (DECL_FUNCTION_CODE (fndecl))
7608 {
7609 CASE_FLT_FN (BUILT_IN_LFLOOR):
7610 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7611 real_floor (&r, TYPE_MODE (ftype), &x);
7612 break;
7613
7614 CASE_FLT_FN (BUILT_IN_LCEIL):
7615 CASE_FLT_FN (BUILT_IN_LLCEIL):
7616 real_ceil (&r, TYPE_MODE (ftype), &x);
7617 break;
7618
7619 CASE_FLT_FN (BUILT_IN_LROUND):
7620 CASE_FLT_FN (BUILT_IN_LLROUND):
7621 real_round (&r, TYPE_MODE (ftype), &x);
7622 break;
7623
7624 default:
7625 gcc_unreachable ();
7626 }
7627
7628 REAL_VALUE_TO_INT (&lo, &hi, r);
7629 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
7630 return build_int_cst_wide (itype, lo2, hi);
7631 }
7632 }
7633
7634 switch (DECL_FUNCTION_CODE (fndecl))
7635 {
7636 CASE_FLT_FN (BUILT_IN_LFLOOR):
7637 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7638 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7639 if (tree_expr_nonnegative_p (arg))
7640 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7641 arg);
7642 break;
7643 default:;
7644 }
7645
7646 return fold_fixed_mathfn (fndecl, arglist);
7647 }
7648
7649 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7650 and their long and long long variants (i.e. ffsl and ffsll).
7651 Return NULL_TREE if no simplification can be made. */
7652
7653 static tree
7654 fold_builtin_bitop (tree fndecl, tree arglist)
7655 {
7656 tree arg;
7657
7658 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7659 return NULL_TREE;
7660
7661 /* Optimize for constant argument. */
7662 arg = TREE_VALUE (arglist);
7663 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7664 {
7665 HOST_WIDE_INT hi, width, result;
7666 unsigned HOST_WIDE_INT lo;
7667 tree type;
7668
7669 type = TREE_TYPE (arg);
7670 width = TYPE_PRECISION (type);
7671 lo = TREE_INT_CST_LOW (arg);
7672
7673 /* Clear all the bits that are beyond the type's precision. */
7674 if (width > HOST_BITS_PER_WIDE_INT)
7675 {
7676 hi = TREE_INT_CST_HIGH (arg);
7677 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7678 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7679 }
7680 else
7681 {
7682 hi = 0;
7683 if (width < HOST_BITS_PER_WIDE_INT)
7684 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7685 }
7686
7687 switch (DECL_FUNCTION_CODE (fndecl))
7688 {
7689 CASE_INT_FN (BUILT_IN_FFS):
7690 if (lo != 0)
7691 result = exact_log2 (lo & -lo) + 1;
7692 else if (hi != 0)
7693 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7694 else
7695 result = 0;
7696 break;
7697
7698 CASE_INT_FN (BUILT_IN_CLZ):
7699 if (hi != 0)
7700 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7701 else if (lo != 0)
7702 result = width - floor_log2 (lo) - 1;
7703 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7704 result = width;
7705 break;
7706
7707 CASE_INT_FN (BUILT_IN_CTZ):
7708 if (lo != 0)
7709 result = exact_log2 (lo & -lo);
7710 else if (hi != 0)
7711 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7712 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7713 result = width;
7714 break;
7715
7716 CASE_INT_FN (BUILT_IN_POPCOUNT):
7717 result = 0;
7718 while (lo)
7719 result++, lo &= lo - 1;
7720 while (hi)
7721 result++, hi &= hi - 1;
7722 break;
7723
7724 CASE_INT_FN (BUILT_IN_PARITY):
7725 result = 0;
7726 while (lo)
7727 result++, lo &= lo - 1;
7728 while (hi)
7729 result++, hi &= hi - 1;
7730 result &= 1;
7731 break;
7732
7733 default:
7734 gcc_unreachable ();
7735 }
7736
7737 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7738 }
7739
7740 return NULL_TREE;
7741 }
7742
7743 /* Fold function call to builtin_bswap and the long and long long
7744 variants. Return NULL_TREE if no simplification can be made. */
7745 static tree
7746 fold_builtin_bswap (tree fndecl, tree arglist)
7747 {
7748 tree arg;
7749
7750 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7751 return 0;
7752
7753 /* Optimize constant value. */
7754 arg = TREE_VALUE (arglist);
7755 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7756 {
7757 HOST_WIDE_INT hi, width, r_hi = 0;
7758 unsigned HOST_WIDE_INT lo, r_lo = 0;
7759 tree type;
7760
7761 type = TREE_TYPE (arg);
7762 width = TYPE_PRECISION (type);
7763 lo = TREE_INT_CST_LOW (arg);
7764 hi = TREE_INT_CST_HIGH (arg);
7765
7766 switch (DECL_FUNCTION_CODE (fndecl))
7767 {
7768 case BUILT_IN_BSWAP32:
7769 case BUILT_IN_BSWAP64:
7770 {
7771 int s;
7772
7773 for (s = 0; s < width; s += 8)
7774 {
7775 int d = width - s - 8;
7776 unsigned HOST_WIDE_INT byte;
7777
7778 if (s < HOST_BITS_PER_WIDE_INT)
7779 byte = (lo >> s) & 0xff;
7780 else
7781 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7782
7783 if (d < HOST_BITS_PER_WIDE_INT)
7784 r_lo |= byte << d;
7785 else
7786 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7787 }
7788 }
7789
7790 break;
7791
7792 default:
7793 gcc_unreachable ();
7794 }
7795
7796 if (width < HOST_BITS_PER_WIDE_INT)
7797 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7798 else
7799 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7800 }
7801
7802 return NULL_TREE;
7803 }
7804 /* Return true if EXPR is the real constant contained in VALUE. */
7805
7806 static bool
7807 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7808 {
7809 STRIP_NOPS (expr);
7810
7811 return ((TREE_CODE (expr) == REAL_CST
7812 && !TREE_OVERFLOW (expr)
7813 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7814 || (TREE_CODE (expr) == COMPLEX_CST
7815 && real_dconstp (TREE_REALPART (expr), value)
7816 && real_zerop (TREE_IMAGPART (expr))));
7817 }
7818
7819 /* A subroutine of fold_builtin to fold the various logarithmic
7820 functions. Return NULL_TREE if no simplification can me made.
7821 FUNC is the corresponding MPFR logarithm function. */
7822
7823 static tree
7824 fold_builtin_logarithm (tree fndecl, tree arglist,
7825 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7826 {
7827 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7828 {
7829 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7830 tree arg = TREE_VALUE (arglist);
7831 tree res;
7832 const enum built_in_function fcode = builtin_mathfn_code (arg);
7833
7834 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7835 instead we'll look for 'e' truncated to MODE. So only do
7836 this if flag_unsafe_math_optimizations is set. */
7837 if (flag_unsafe_math_optimizations && func == mpfr_log)
7838 {
7839 const REAL_VALUE_TYPE e_truncated =
7840 real_value_truncate (TYPE_MODE (type), dconste);
7841 if (real_dconstp (arg, &e_truncated))
7842 return build_real (type, dconst1);
7843 }
7844
7845 /* Calculate the result when the argument is a constant. */
7846 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7847 return res;
7848
7849 /* Special case, optimize logN(expN(x)) = x. */
7850 if (flag_unsafe_math_optimizations
7851 && ((func == mpfr_log
7852 && (fcode == BUILT_IN_EXP
7853 || fcode == BUILT_IN_EXPF
7854 || fcode == BUILT_IN_EXPL))
7855 || (func == mpfr_log2
7856 && (fcode == BUILT_IN_EXP2
7857 || fcode == BUILT_IN_EXP2F
7858 || fcode == BUILT_IN_EXP2L))
7859 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7860 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7861
7862 /* Optimize logN(func()) for various exponential functions. We
7863 want to determine the value "x" and the power "exponent" in
7864 order to transform logN(x**exponent) into exponent*logN(x). */
7865 if (flag_unsafe_math_optimizations)
7866 {
7867 tree exponent = 0, x = 0;
7868
7869 switch (fcode)
7870 {
7871 CASE_FLT_FN (BUILT_IN_EXP):
7872 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7873 x = build_real (type,
7874 real_value_truncate (TYPE_MODE (type), dconste));
7875 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7876 break;
7877 CASE_FLT_FN (BUILT_IN_EXP2):
7878 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7879 x = build_real (type, dconst2);
7880 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7881 break;
7882 CASE_FLT_FN (BUILT_IN_EXP10):
7883 CASE_FLT_FN (BUILT_IN_POW10):
7884 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7885 x = build_real (type, dconst10);
7886 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7887 break;
7888 CASE_FLT_FN (BUILT_IN_SQRT):
7889 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7890 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7891 exponent = build_real (type, dconsthalf);
7892 break;
7893 CASE_FLT_FN (BUILT_IN_CBRT):
7894 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7895 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7896 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7897 dconstthird));
7898 break;
7899 CASE_FLT_FN (BUILT_IN_POW):
7900 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7901 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7902 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7903 break;
7904 default:
7905 break;
7906 }
7907
7908 /* Now perform the optimization. */
7909 if (x && exponent)
7910 {
7911 tree logfn;
7912 arglist = build_tree_list (NULL_TREE, x);
7913 logfn = build_function_call_expr (fndecl, arglist);
7914 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7915 }
7916 }
7917 }
7918
7919 return 0;
7920 }
7921
7922 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7923 NULL_TREE if no simplification can be made. */
7924
7925 static tree
7926 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7927 {
7928 tree arg0 = TREE_VALUE (arglist);
7929 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7930 tree res, narg0, narg1;
7931
7932 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7933 return NULL_TREE;
7934
7935 /* Calculate the result when the argument is a constant. */
7936 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7937 return res;
7938
7939 /* If either argument to hypot has a negate or abs, strip that off.
7940 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7941 narg0 = fold_strip_sign_ops (arg0);
7942 narg1 = fold_strip_sign_ops (arg1);
7943 if (narg0 || narg1)
7944 {
7945 tree narglist = tree_cons (NULL_TREE, narg0 ? narg0 : arg0,
7946 build_tree_list (NULL_TREE,
7947 narg1 ? narg1 : arg1));
7948 return build_function_call_expr (fndecl, narglist);
7949 }
7950
7951 /* If either argument is zero, hypot is fabs of the other. */
7952 if (real_zerop (arg0))
7953 return fold_build1 (ABS_EXPR, type, arg1);
7954 else if (real_zerop (arg1))
7955 return fold_build1 (ABS_EXPR, type, arg0);
7956
7957 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7958 if (flag_unsafe_math_optimizations
7959 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7960 {
7961 REAL_VALUE_TYPE sqrt2;
7962
7963 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7964 return fold_build2 (MULT_EXPR, type,
7965 fold_build1 (ABS_EXPR, type, arg0),
7966 build_real (type, sqrt2));
7967 }
7968
7969 return NULL_TREE;
7970 }
7971
7972
7973 /* Fold a builtin function call to pow, powf, or powl. Return
7974 NULL_TREE if no simplification can be made. */
7975 static tree
7976 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7977 {
7978 tree arg0 = TREE_VALUE (arglist);
7979 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7980 tree res;
7981
7982 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7983 return NULL_TREE;
7984
7985 /* Calculate the result when the argument is a constant. */
7986 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7987 return res;
7988
7989 /* Optimize pow(1.0,y) = 1.0. */
7990 if (real_onep (arg0))
7991 return omit_one_operand (type, build_real (type, dconst1), arg1);
7992
7993 if (TREE_CODE (arg1) == REAL_CST
7994 && !TREE_OVERFLOW (arg1))
7995 {
7996 REAL_VALUE_TYPE cint;
7997 REAL_VALUE_TYPE c;
7998 HOST_WIDE_INT n;
7999
8000 c = TREE_REAL_CST (arg1);
8001
8002 /* Optimize pow(x,0.0) = 1.0. */
8003 if (REAL_VALUES_EQUAL (c, dconst0))
8004 return omit_one_operand (type, build_real (type, dconst1),
8005 arg0);
8006
8007 /* Optimize pow(x,1.0) = x. */
8008 if (REAL_VALUES_EQUAL (c, dconst1))
8009 return arg0;
8010
8011 /* Optimize pow(x,-1.0) = 1.0/x. */
8012 if (REAL_VALUES_EQUAL (c, dconstm1))
8013 return fold_build2 (RDIV_EXPR, type,
8014 build_real (type, dconst1), arg0);
8015
8016 /* Optimize pow(x,0.5) = sqrt(x). */
8017 if (flag_unsafe_math_optimizations
8018 && REAL_VALUES_EQUAL (c, dconsthalf))
8019 {
8020 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8021
8022 if (sqrtfn != NULL_TREE)
8023 {
8024 tree arglist = build_tree_list (NULL_TREE, arg0);
8025 return build_function_call_expr (sqrtfn, arglist);
8026 }
8027 }
8028
8029 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8030 if (flag_unsafe_math_optimizations)
8031 {
8032 const REAL_VALUE_TYPE dconstroot
8033 = real_value_truncate (TYPE_MODE (type), dconstthird);
8034
8035 if (REAL_VALUES_EQUAL (c, dconstroot))
8036 {
8037 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8038 if (cbrtfn != NULL_TREE)
8039 {
8040 tree arglist = build_tree_list (NULL_TREE, arg0);
8041 return build_function_call_expr (cbrtfn, arglist);
8042 }
8043 }
8044 }
8045
8046 /* Check for an integer exponent. */
8047 n = real_to_integer (&c);
8048 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8049 if (real_identical (&c, &cint))
8050 {
8051 /* Attempt to evaluate pow at compile-time. */
8052 if (TREE_CODE (arg0) == REAL_CST
8053 && !TREE_OVERFLOW (arg0))
8054 {
8055 REAL_VALUE_TYPE x;
8056 bool inexact;
8057
8058 x = TREE_REAL_CST (arg0);
8059 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8060 if (flag_unsafe_math_optimizations || !inexact)
8061 return build_real (type, x);
8062 }
8063
8064 /* Strip sign ops from even integer powers. */
8065 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8066 {
8067 tree narg0 = fold_strip_sign_ops (arg0);
8068 if (narg0)
8069 {
8070 arglist = build_tree_list (NULL_TREE, arg1);
8071 arglist = tree_cons (NULL_TREE, narg0, arglist);
8072 return build_function_call_expr (fndecl, arglist);
8073 }
8074 }
8075 }
8076 }
8077
8078 if (flag_unsafe_math_optimizations)
8079 {
8080 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8081
8082 /* Optimize pow(expN(x),y) = expN(x*y). */
8083 if (BUILTIN_EXPONENT_P (fcode))
8084 {
8085 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8086 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8087 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8088 arglist = build_tree_list (NULL_TREE, arg);
8089 return build_function_call_expr (expfn, arglist);
8090 }
8091
8092 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8093 if (BUILTIN_SQRT_P (fcode))
8094 {
8095 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8096 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8097 build_real (type, dconsthalf));
8098
8099 arglist = tree_cons (NULL_TREE, narg0,
8100 build_tree_list (NULL_TREE, narg1));
8101 return build_function_call_expr (fndecl, arglist);
8102 }
8103
8104 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8105 if (BUILTIN_CBRT_P (fcode))
8106 {
8107 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8108 if (tree_expr_nonnegative_p (arg))
8109 {
8110 const REAL_VALUE_TYPE dconstroot
8111 = real_value_truncate (TYPE_MODE (type), dconstthird);
8112 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8113 build_real (type, dconstroot));
8114 arglist = tree_cons (NULL_TREE, arg,
8115 build_tree_list (NULL_TREE, narg1));
8116 return build_function_call_expr (fndecl, arglist);
8117 }
8118 }
8119
8120 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8121 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
8122 || fcode == BUILT_IN_POWL)
8123 {
8124 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8125 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8126 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8127 arglist = tree_cons (NULL_TREE, arg00,
8128 build_tree_list (NULL_TREE, narg1));
8129 return build_function_call_expr (fndecl, arglist);
8130 }
8131 }
8132
8133 return NULL_TREE;
8134 }
8135
8136 /* Fold a builtin function call to powi, powif, or powil. Return
8137 NULL_TREE if no simplification can be made. */
8138 static tree
8139 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
8140 {
8141 tree arg0 = TREE_VALUE (arglist);
8142 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8143
8144 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
8145 return NULL_TREE;
8146
8147 /* Optimize pow(1.0,y) = 1.0. */
8148 if (real_onep (arg0))
8149 return omit_one_operand (type, build_real (type, dconst1), arg1);
8150
8151 if (host_integerp (arg1, 0))
8152 {
8153 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8154
8155 /* Evaluate powi at compile-time. */
8156 if (TREE_CODE (arg0) == REAL_CST
8157 && !TREE_OVERFLOW (arg0))
8158 {
8159 REAL_VALUE_TYPE x;
8160 x = TREE_REAL_CST (arg0);
8161 real_powi (&x, TYPE_MODE (type), &x, c);
8162 return build_real (type, x);
8163 }
8164
8165 /* Optimize pow(x,0) = 1.0. */
8166 if (c == 0)
8167 return omit_one_operand (type, build_real (type, dconst1),
8168 arg0);
8169
8170 /* Optimize pow(x,1) = x. */
8171 if (c == 1)
8172 return arg0;
8173
8174 /* Optimize pow(x,-1) = 1.0/x. */
8175 if (c == -1)
8176 return fold_build2 (RDIV_EXPR, type,
8177 build_real (type, dconst1), arg0);
8178 }
8179
8180 return NULL_TREE;
8181 }
8182
8183 /* A subroutine of fold_builtin to fold the various exponent
8184 functions. Return NULL_TREE if no simplification can me made.
8185 FUNC is the corresponding MPFR exponent function. */
8186
8187 static tree
8188 fold_builtin_exponent (tree fndecl, tree arglist,
8189 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8190 {
8191 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8192 {
8193 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8194 tree arg = TREE_VALUE (arglist);
8195 tree res;
8196
8197 /* Calculate the result when the argument is a constant. */
8198 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8199 return res;
8200
8201 /* Optimize expN(logN(x)) = x. */
8202 if (flag_unsafe_math_optimizations)
8203 {
8204 const enum built_in_function fcode = builtin_mathfn_code (arg);
8205
8206 if ((func == mpfr_exp
8207 && (fcode == BUILT_IN_LOG
8208 || fcode == BUILT_IN_LOGF
8209 || fcode == BUILT_IN_LOGL))
8210 || (func == mpfr_exp2
8211 && (fcode == BUILT_IN_LOG2
8212 || fcode == BUILT_IN_LOG2F
8213 || fcode == BUILT_IN_LOG2L))
8214 || (func == mpfr_exp10
8215 && (fcode == BUILT_IN_LOG10
8216 || fcode == BUILT_IN_LOG10F
8217 || fcode == BUILT_IN_LOG10L)))
8218 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8219 }
8220 }
8221
8222 return 0;
8223 }
8224
8225 /* Return true if VAR is a VAR_DECL or a component thereof. */
8226
8227 static bool
8228 var_decl_component_p (tree var)
8229 {
8230 tree inner = var;
8231 while (handled_component_p (inner))
8232 inner = TREE_OPERAND (inner, 0);
8233 return SSA_VAR_P (inner);
8234 }
8235
8236 /* Fold function call to builtin memset. Return
8237 NULL_TREE if no simplification can be made. */
8238
8239 static tree
8240 fold_builtin_memset (tree arglist, tree type, bool ignore)
8241 {
8242 tree dest, c, len, var, ret;
8243 unsigned HOST_WIDE_INT length, cval;
8244
8245 if (!validate_arglist (arglist,
8246 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8247 return 0;
8248
8249 dest = TREE_VALUE (arglist);
8250 c = TREE_VALUE (TREE_CHAIN (arglist));
8251 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8252
8253 if (! host_integerp (len, 1))
8254 return 0;
8255
8256 /* If the LEN parameter is zero, return DEST. */
8257 if (integer_zerop (len))
8258 return omit_one_operand (type, dest, c);
8259
8260 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8261 return 0;
8262
8263 var = dest;
8264 STRIP_NOPS (var);
8265 if (TREE_CODE (var) != ADDR_EXPR)
8266 return 0;
8267
8268 var = TREE_OPERAND (var, 0);
8269 if (TREE_THIS_VOLATILE (var))
8270 return 0;
8271
8272 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8273 && !POINTER_TYPE_P (TREE_TYPE (var)))
8274 return 0;
8275
8276 if (! var_decl_component_p (var))
8277 return 0;
8278
8279 length = tree_low_cst (len, 1);
8280 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8281 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8282 < (int) length)
8283 return 0;
8284
8285 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8286 return 0;
8287
8288 if (integer_zerop (c))
8289 cval = 0;
8290 else
8291 {
8292 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8293 return 0;
8294
8295 cval = tree_low_cst (c, 1);
8296 cval &= 0xff;
8297 cval |= cval << 8;
8298 cval |= cval << 16;
8299 cval |= (cval << 31) << 1;
8300 }
8301
8302 ret = build_int_cst_type (TREE_TYPE (var), cval);
8303 ret = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, ret);
8304 if (ignore)
8305 return ret;
8306
8307 return omit_one_operand (type, dest, ret);
8308 }
8309
8310 /* Fold function call to builtin memset. Return
8311 NULL_TREE if no simplification can be made. */
8312
8313 static tree
8314 fold_builtin_bzero (tree arglist, bool ignore)
8315 {
8316 tree dest, size, newarglist;
8317
8318 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8319 return 0;
8320
8321 if (!ignore)
8322 return 0;
8323
8324 dest = TREE_VALUE (arglist);
8325 size = TREE_VALUE (TREE_CHAIN (arglist));
8326
8327 /* New argument list transforming bzero(ptr x, int y) to
8328 memset(ptr x, int 0, size_t y). This is done this way
8329 so that if it isn't expanded inline, we fallback to
8330 calling bzero instead of memset. */
8331
8332 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8333 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8334 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8335 return fold_builtin_memset (newarglist, void_type_node, ignore);
8336 }
8337
8338 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8339 NULL_TREE if no simplification can be made.
8340 If ENDP is 0, return DEST (like memcpy).
8341 If ENDP is 1, return DEST+LEN (like mempcpy).
8342 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8343 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8344 (memmove). */
8345
8346 static tree
8347 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8348 {
8349 tree dest, src, len, destvar, srcvar, expr;
8350
8351 if (! validate_arglist (arglist,
8352 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8353 return 0;
8354
8355 dest = TREE_VALUE (arglist);
8356 src = TREE_VALUE (TREE_CHAIN (arglist));
8357 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8358
8359 /* If the LEN parameter is zero, return DEST. */
8360 if (integer_zerop (len))
8361 return omit_one_operand (type, dest, src);
8362
8363 /* If SRC and DEST are the same (and not volatile), return
8364 DEST{,+LEN,+LEN-1}. */
8365 if (operand_equal_p (src, dest, 0))
8366 expr = len;
8367 else
8368 {
8369 tree srctype, desttype;
8370 if (endp == 3)
8371 {
8372 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8373 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8374
8375 /* Both DEST and SRC must be pointer types.
8376 ??? This is what old code did. Is the testing for pointer types
8377 really mandatory?
8378
8379 If either SRC is readonly or length is 1, we can use memcpy. */
8380 if (dest_align && src_align
8381 && (readonly_data_expr (src)
8382 || (host_integerp (len, 1)
8383 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8384 tree_low_cst (len, 1)))))
8385 {
8386 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8387 if (!fn)
8388 return 0;
8389 return build_function_call_expr (fn, arglist);
8390 }
8391 return 0;
8392 }
8393
8394 if (!host_integerp (len, 0))
8395 return 0;
8396 /* FIXME:
8397 This logic lose for arguments like (type *)malloc (sizeof (type)),
8398 since we strip the casts of up to VOID return value from malloc.
8399 Perhaps we ought to inherit type from non-VOID argument here? */
8400 STRIP_NOPS (src);
8401 STRIP_NOPS (dest);
8402 srctype = TREE_TYPE (TREE_TYPE (src));
8403 desttype = TREE_TYPE (TREE_TYPE (dest));
8404 if (!srctype || !desttype
8405 || !TYPE_SIZE_UNIT (srctype)
8406 || !TYPE_SIZE_UNIT (desttype)
8407 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8408 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8409 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
8410 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8411 return 0;
8412
8413 if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT)
8414 < (int) TYPE_ALIGN (desttype)
8415 || (get_pointer_alignment (src, BIGGEST_ALIGNMENT)
8416 < (int) TYPE_ALIGN (srctype)))
8417 return 0;
8418
8419 if (!ignore)
8420 dest = builtin_save_expr (dest);
8421
8422 srcvar = build_fold_indirect_ref (src);
8423 if (TREE_THIS_VOLATILE (srcvar))
8424 return 0;
8425 if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8426 return 0;
8427 /* With memcpy, it is possible to bypass aliasing rules, so without
8428 this check i. e. execute/20060930-2.c would be misoptimized, because
8429 it use conflicting alias set to hold argument for the memcpy call.
8430 This check is probably unnecesary with -fno-strict-aliasing.
8431 Similarly for destvar. See also PR29286. */
8432 if (!var_decl_component_p (srcvar)
8433 /* Accept: memcpy (*char_var, "test", 1); that simplify
8434 to char_var='t'; */
8435 || is_gimple_min_invariant (srcvar)
8436 || readonly_data_expr (src))
8437 return 0;
8438
8439 destvar = build_fold_indirect_ref (dest);
8440 if (TREE_THIS_VOLATILE (destvar))
8441 return 0;
8442 if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8443 return 0;
8444 if (!var_decl_component_p (destvar))
8445 return 0;
8446
8447 if (srctype == desttype
8448 || (gimple_in_ssa_p (cfun)
8449 && tree_ssa_useless_type_conversion_1 (desttype, srctype)))
8450 expr = srcvar;
8451 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8452 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8453 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8454 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8455 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8456 else
8457 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8458 expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (destvar), destvar, expr);
8459 }
8460
8461 if (ignore)
8462 return expr;
8463
8464 if (endp == 0 || endp == 3)
8465 return omit_one_operand (type, dest, expr);
8466
8467 if (expr == len)
8468 expr = 0;
8469
8470 if (endp == 2)
8471 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8472 ssize_int (1));
8473
8474 len = fold_convert (TREE_TYPE (dest), len);
8475 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8476 dest = fold_convert (type, dest);
8477 if (expr)
8478 dest = omit_one_operand (type, dest, expr);
8479 return dest;
8480 }
8481
8482 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8483 simplification can be made. */
8484
8485 static tree
8486 fold_builtin_bcopy (tree arglist, bool ignore)
8487 {
8488 tree src, dest, size, newarglist;
8489
8490 if (!validate_arglist (arglist,
8491 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8492 return 0;
8493
8494 if (! ignore)
8495 return 0;
8496
8497 src = TREE_VALUE (arglist);
8498 dest = TREE_VALUE (TREE_CHAIN (arglist));
8499 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8500
8501 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8502 memmove(ptr y, ptr x, size_t z). This is done this way
8503 so that if it isn't expanded inline, we fallback to
8504 calling bcopy instead of memmove. */
8505
8506 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8507 newarglist = tree_cons (NULL_TREE, src, newarglist);
8508 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8509
8510 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8511 }
8512
8513 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8514 the length of the string to be copied. Return NULL_TREE if no
8515 simplification can be made. */
8516
8517 tree
8518 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8519 {
8520 tree dest, src, fn;
8521
8522 if (!validate_arglist (arglist,
8523 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8524 return 0;
8525
8526 dest = TREE_VALUE (arglist);
8527 src = TREE_VALUE (TREE_CHAIN (arglist));
8528
8529 /* If SRC and DEST are the same (and not volatile), return DEST. */
8530 if (operand_equal_p (src, dest, 0))
8531 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8532
8533 if (optimize_size)
8534 return 0;
8535
8536 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8537 if (!fn)
8538 return 0;
8539
8540 if (!len)
8541 {
8542 len = c_strlen (src, 1);
8543 if (! len || TREE_SIDE_EFFECTS (len))
8544 return 0;
8545 }
8546
8547 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8548 arglist = build_tree_list (NULL_TREE, len);
8549 arglist = tree_cons (NULL_TREE, src, arglist);
8550 arglist = tree_cons (NULL_TREE, dest, arglist);
8551 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8552 build_function_call_expr (fn, arglist));
8553 }
8554
8555 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8556 the length of the source string. Return NULL_TREE if no simplification
8557 can be made. */
8558
8559 tree
8560 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8561 {
8562 tree dest, src, len, fn;
8563
8564 if (!validate_arglist (arglist,
8565 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8566 return 0;
8567
8568 dest = TREE_VALUE (arglist);
8569 src = TREE_VALUE (TREE_CHAIN (arglist));
8570 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8571
8572 /* If the LEN parameter is zero, return DEST. */
8573 if (integer_zerop (len))
8574 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8575
8576 /* We can't compare slen with len as constants below if len is not a
8577 constant. */
8578 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8579 return 0;
8580
8581 if (!slen)
8582 slen = c_strlen (src, 1);
8583
8584 /* Now, we must be passed a constant src ptr parameter. */
8585 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8586 return 0;
8587
8588 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8589
8590 /* We do not support simplification of this case, though we do
8591 support it when expanding trees into RTL. */
8592 /* FIXME: generate a call to __builtin_memset. */
8593 if (tree_int_cst_lt (slen, len))
8594 return 0;
8595
8596 /* OK transform into builtin memcpy. */
8597 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8598 if (!fn)
8599 return 0;
8600 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8601 build_function_call_expr (fn, arglist));
8602 }
8603
8604 /* Fold function call to builtin memcmp. Return
8605 NULL_TREE if no simplification can be made. */
8606
8607 static tree
8608 fold_builtin_memcmp (tree arglist)
8609 {
8610 tree arg1, arg2, len;
8611 const char *p1, *p2;
8612
8613 if (!validate_arglist (arglist,
8614 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8615 return 0;
8616
8617 arg1 = TREE_VALUE (arglist);
8618 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8619 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8620
8621 /* If the LEN parameter is zero, return zero. */
8622 if (integer_zerop (len))
8623 return omit_two_operands (integer_type_node, integer_zero_node,
8624 arg1, arg2);
8625
8626 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8627 if (operand_equal_p (arg1, arg2, 0))
8628 return omit_one_operand (integer_type_node, integer_zero_node, len);
8629
8630 p1 = c_getstr (arg1);
8631 p2 = c_getstr (arg2);
8632
8633 /* If all arguments are constant, and the value of len is not greater
8634 than the lengths of arg1 and arg2, evaluate at compile-time. */
8635 if (host_integerp (len, 1) && p1 && p2
8636 && compare_tree_int (len, strlen (p1) + 1) <= 0
8637 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8638 {
8639 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8640
8641 if (r > 0)
8642 return integer_one_node;
8643 else if (r < 0)
8644 return integer_minus_one_node;
8645 else
8646 return integer_zero_node;
8647 }
8648
8649 /* If len parameter is one, return an expression corresponding to
8650 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8651 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8652 {
8653 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8654 tree cst_uchar_ptr_node
8655 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8656
8657 tree ind1 = fold_convert (integer_type_node,
8658 build1 (INDIRECT_REF, cst_uchar_node,
8659 fold_convert (cst_uchar_ptr_node,
8660 arg1)));
8661 tree ind2 = fold_convert (integer_type_node,
8662 build1 (INDIRECT_REF, cst_uchar_node,
8663 fold_convert (cst_uchar_ptr_node,
8664 arg2)));
8665 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8666 }
8667
8668 return 0;
8669 }
8670
8671 /* Fold function call to builtin strcmp. Return
8672 NULL_TREE if no simplification can be made. */
8673
8674 static tree
8675 fold_builtin_strcmp (tree arglist)
8676 {
8677 tree arg1, arg2;
8678 const char *p1, *p2;
8679
8680 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8681 return 0;
8682
8683 arg1 = TREE_VALUE (arglist);
8684 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8685
8686 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8687 if (operand_equal_p (arg1, arg2, 0))
8688 return integer_zero_node;
8689
8690 p1 = c_getstr (arg1);
8691 p2 = c_getstr (arg2);
8692
8693 if (p1 && p2)
8694 {
8695 const int i = strcmp (p1, p2);
8696 if (i < 0)
8697 return integer_minus_one_node;
8698 else if (i > 0)
8699 return integer_one_node;
8700 else
8701 return integer_zero_node;
8702 }
8703
8704 /* If the second arg is "", return *(const unsigned char*)arg1. */
8705 if (p2 && *p2 == '\0')
8706 {
8707 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8708 tree cst_uchar_ptr_node
8709 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8710
8711 return fold_convert (integer_type_node,
8712 build1 (INDIRECT_REF, cst_uchar_node,
8713 fold_convert (cst_uchar_ptr_node,
8714 arg1)));
8715 }
8716
8717 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8718 if (p1 && *p1 == '\0')
8719 {
8720 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8721 tree cst_uchar_ptr_node
8722 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8723
8724 tree temp = fold_convert (integer_type_node,
8725 build1 (INDIRECT_REF, cst_uchar_node,
8726 fold_convert (cst_uchar_ptr_node,
8727 arg2)));
8728 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8729 }
8730
8731 return 0;
8732 }
8733
8734 /* Fold function call to builtin strncmp. Return
8735 NULL_TREE if no simplification can be made. */
8736
8737 static tree
8738 fold_builtin_strncmp (tree arglist)
8739 {
8740 tree arg1, arg2, len;
8741 const char *p1, *p2;
8742
8743 if (!validate_arglist (arglist,
8744 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8745 return 0;
8746
8747 arg1 = TREE_VALUE (arglist);
8748 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8749 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8750
8751 /* If the LEN parameter is zero, return zero. */
8752 if (integer_zerop (len))
8753 return omit_two_operands (integer_type_node, integer_zero_node,
8754 arg1, arg2);
8755
8756 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8757 if (operand_equal_p (arg1, arg2, 0))
8758 return omit_one_operand (integer_type_node, integer_zero_node, len);
8759
8760 p1 = c_getstr (arg1);
8761 p2 = c_getstr (arg2);
8762
8763 if (host_integerp (len, 1) && p1 && p2)
8764 {
8765 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8766 if (i > 0)
8767 return integer_one_node;
8768 else if (i < 0)
8769 return integer_minus_one_node;
8770 else
8771 return integer_zero_node;
8772 }
8773
8774 /* If the second arg is "", and the length is greater than zero,
8775 return *(const unsigned char*)arg1. */
8776 if (p2 && *p2 == '\0'
8777 && TREE_CODE (len) == INTEGER_CST
8778 && tree_int_cst_sgn (len) == 1)
8779 {
8780 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8781 tree cst_uchar_ptr_node
8782 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8783
8784 return fold_convert (integer_type_node,
8785 build1 (INDIRECT_REF, cst_uchar_node,
8786 fold_convert (cst_uchar_ptr_node,
8787 arg1)));
8788 }
8789
8790 /* If the first arg is "", and the length is greater than zero,
8791 return -*(const unsigned char*)arg2. */
8792 if (p1 && *p1 == '\0'
8793 && TREE_CODE (len) == INTEGER_CST
8794 && tree_int_cst_sgn (len) == 1)
8795 {
8796 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8797 tree cst_uchar_ptr_node
8798 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8799
8800 tree temp = fold_convert (integer_type_node,
8801 build1 (INDIRECT_REF, cst_uchar_node,
8802 fold_convert (cst_uchar_ptr_node,
8803 arg2)));
8804 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8805 }
8806
8807 /* If len parameter is one, return an expression corresponding to
8808 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8809 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8810 {
8811 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8812 tree cst_uchar_ptr_node
8813 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8814
8815 tree ind1 = fold_convert (integer_type_node,
8816 build1 (INDIRECT_REF, cst_uchar_node,
8817 fold_convert (cst_uchar_ptr_node,
8818 arg1)));
8819 tree ind2 = fold_convert (integer_type_node,
8820 build1 (INDIRECT_REF, cst_uchar_node,
8821 fold_convert (cst_uchar_ptr_node,
8822 arg2)));
8823 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8824 }
8825
8826 return 0;
8827 }
8828
8829 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8830 NULL_TREE if no simplification can be made. */
8831
8832 static tree
8833 fold_builtin_signbit (tree fndecl, tree arglist)
8834 {
8835 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8836 tree arg, temp;
8837
8838 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8839 return NULL_TREE;
8840
8841 arg = TREE_VALUE (arglist);
8842
8843 /* If ARG is a compile-time constant, determine the result. */
8844 if (TREE_CODE (arg) == REAL_CST
8845 && !TREE_OVERFLOW (arg))
8846 {
8847 REAL_VALUE_TYPE c;
8848
8849 c = TREE_REAL_CST (arg);
8850 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8851 return fold_convert (type, temp);
8852 }
8853
8854 /* If ARG is non-negative, the result is always zero. */
8855 if (tree_expr_nonnegative_p (arg))
8856 return omit_one_operand (type, integer_zero_node, arg);
8857
8858 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8859 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8860 return fold_build2 (LT_EXPR, type, arg,
8861 build_real (TREE_TYPE (arg), dconst0));
8862
8863 return NULL_TREE;
8864 }
8865
8866 /* Fold function call to builtin copysign, copysignf or copysignl.
8867 Return NULL_TREE if no simplification can be made. */
8868
8869 static tree
8870 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8871 {
8872 tree arg1, arg2, tem;
8873
8874 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8875 return NULL_TREE;
8876
8877 arg1 = TREE_VALUE (arglist);
8878 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8879
8880 /* copysign(X,X) is X. */
8881 if (operand_equal_p (arg1, arg2, 0))
8882 return fold_convert (type, arg1);
8883
8884 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8885 if (TREE_CODE (arg1) == REAL_CST
8886 && TREE_CODE (arg2) == REAL_CST
8887 && !TREE_OVERFLOW (arg1)
8888 && !TREE_OVERFLOW (arg2))
8889 {
8890 REAL_VALUE_TYPE c1, c2;
8891
8892 c1 = TREE_REAL_CST (arg1);
8893 c2 = TREE_REAL_CST (arg2);
8894 /* c1.sign := c2.sign. */
8895 real_copysign (&c1, &c2);
8896 return build_real (type, c1);
8897 }
8898
8899 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8900 Remember to evaluate Y for side-effects. */
8901 if (tree_expr_nonnegative_p (arg2))
8902 return omit_one_operand (type,
8903 fold_build1 (ABS_EXPR, type, arg1),
8904 arg2);
8905
8906 /* Strip sign changing operations for the first argument. */
8907 tem = fold_strip_sign_ops (arg1);
8908 if (tem)
8909 {
8910 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8911 return build_function_call_expr (fndecl, arglist);
8912 }
8913
8914 return NULL_TREE;
8915 }
8916
8917 /* Fold a call to builtin isascii. */
8918
8919 static tree
8920 fold_builtin_isascii (tree arglist)
8921 {
8922 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8923 return 0;
8924 else
8925 {
8926 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8927 tree arg = TREE_VALUE (arglist);
8928
8929 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8930 build_int_cst (NULL_TREE,
8931 ~ (unsigned HOST_WIDE_INT) 0x7f));
8932 return fold_build2 (EQ_EXPR, integer_type_node,
8933 arg, integer_zero_node);
8934 }
8935 }
8936
8937 /* Fold a call to builtin toascii. */
8938
8939 static tree
8940 fold_builtin_toascii (tree arglist)
8941 {
8942 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8943 return 0;
8944 else
8945 {
8946 /* Transform toascii(c) -> (c & 0x7f). */
8947 tree arg = TREE_VALUE (arglist);
8948
8949 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8950 build_int_cst (NULL_TREE, 0x7f));
8951 }
8952 }
8953
8954 /* Fold a call to builtin isdigit. */
8955
8956 static tree
8957 fold_builtin_isdigit (tree arglist)
8958 {
8959 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8960 return 0;
8961 else
8962 {
8963 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8964 /* According to the C standard, isdigit is unaffected by locale.
8965 However, it definitely is affected by the target character set. */
8966 tree arg;
8967 unsigned HOST_WIDE_INT target_digit0
8968 = lang_hooks.to_target_charset ('0');
8969
8970 if (target_digit0 == 0)
8971 return NULL_TREE;
8972
8973 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8974 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8975 build_int_cst (unsigned_type_node, target_digit0));
8976 return fold_build2 (LE_EXPR, integer_type_node, arg,
8977 build_int_cst (unsigned_type_node, 9));
8978 }
8979 }
8980
8981 /* Fold a call to fabs, fabsf or fabsl. */
8982
8983 static tree
8984 fold_builtin_fabs (tree arglist, tree type)
8985 {
8986 tree arg;
8987
8988 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8989 return 0;
8990
8991 arg = TREE_VALUE (arglist);
8992 arg = fold_convert (type, arg);
8993 if (TREE_CODE (arg) == REAL_CST)
8994 return fold_abs_const (arg, type);
8995 return fold_build1 (ABS_EXPR, type, arg);
8996 }
8997
8998 /* Fold a call to abs, labs, llabs or imaxabs. */
8999
9000 static tree
9001 fold_builtin_abs (tree arglist, tree type)
9002 {
9003 tree arg;
9004
9005 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
9006 return 0;
9007
9008 arg = TREE_VALUE (arglist);
9009 arg = fold_convert (type, arg);
9010 if (TREE_CODE (arg) == INTEGER_CST)
9011 return fold_abs_const (arg, type);
9012 return fold_build1 (ABS_EXPR, type, arg);
9013 }
9014
9015 /* Fold a call to builtin fmin or fmax. */
9016
9017 static tree
9018 fold_builtin_fmin_fmax (tree arglist, tree type, bool max)
9019 {
9020 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9021 {
9022 tree arg0 = TREE_VALUE (arglist);
9023 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9024 /* Calculate the result when the argument is a constant. */
9025 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9026
9027 if (res)
9028 return res;
9029
9030 /* If either argument is NaN, return the other one. Avoid the
9031 transformation if we get (and honor) a signalling NaN. Using
9032 omit_one_operand() ensures we create a non-lvalue. */
9033 if (TREE_CODE (arg0) == REAL_CST
9034 && real_isnan (&TREE_REAL_CST (arg0))
9035 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9036 || ! TREE_REAL_CST (arg0).signalling))
9037 return omit_one_operand (type, arg1, arg0);
9038 if (TREE_CODE (arg1) == REAL_CST
9039 && real_isnan (&TREE_REAL_CST (arg1))
9040 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9041 || ! TREE_REAL_CST (arg1).signalling))
9042 return omit_one_operand (type, arg0, arg1);
9043
9044 /* Transform fmin/fmax(x,x) -> x. */
9045 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9046 return omit_one_operand (type, arg0, arg1);
9047
9048 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9049 functions to return the numeric arg if the other one is NaN.
9050 These tree codes don't honor that, so only transform if
9051 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9052 handled, so we don't have to worry about it either. */
9053 if (flag_finite_math_only)
9054 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9055 fold_convert (type, arg0),
9056 fold_convert (type, arg1));
9057 }
9058 return NULL_TREE;
9059 }
9060
9061 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9062 EXP is the CALL_EXPR for the call. */
9063
9064 static tree
9065 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
9066 {
9067 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9068 tree arg;
9069 REAL_VALUE_TYPE r;
9070
9071 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9072 {
9073 /* Check that we have exactly one argument. */
9074 if (arglist == 0)
9075 {
9076 error ("too few arguments to function %qs",
9077 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9078 return error_mark_node;
9079 }
9080 else if (TREE_CHAIN (arglist) != 0)
9081 {
9082 error ("too many arguments to function %qs",
9083 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9084 return error_mark_node;
9085 }
9086 else
9087 {
9088 error ("non-floating-point argument to function %qs",
9089 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9090 return error_mark_node;
9091 }
9092 }
9093
9094 arg = TREE_VALUE (arglist);
9095 switch (builtin_index)
9096 {
9097 case BUILT_IN_ISINF:
9098 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9099 return omit_one_operand (type, integer_zero_node, arg);
9100
9101 if (TREE_CODE (arg) == REAL_CST)
9102 {
9103 r = TREE_REAL_CST (arg);
9104 if (real_isinf (&r))
9105 return real_compare (GT_EXPR, &r, &dconst0)
9106 ? integer_one_node : integer_minus_one_node;
9107 else
9108 return integer_zero_node;
9109 }
9110
9111 return NULL_TREE;
9112
9113 case BUILT_IN_FINITE:
9114 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9115 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9116 return omit_one_operand (type, integer_one_node, arg);
9117
9118 if (TREE_CODE (arg) == REAL_CST)
9119 {
9120 r = TREE_REAL_CST (arg);
9121 return real_isinf (&r) || real_isnan (&r)
9122 ? integer_zero_node : integer_one_node;
9123 }
9124
9125 return NULL_TREE;
9126
9127 case BUILT_IN_ISNAN:
9128 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9129 return omit_one_operand (type, integer_zero_node, arg);
9130
9131 if (TREE_CODE (arg) == REAL_CST)
9132 {
9133 r = TREE_REAL_CST (arg);
9134 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9135 }
9136
9137 arg = builtin_save_expr (arg);
9138 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
9139
9140 default:
9141 gcc_unreachable ();
9142 }
9143 }
9144
9145 /* Fold a call to an unordered comparison function such as
9146 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9147 being called and ARGLIST is the argument list for the call.
9148 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9149 the opposite of the desired result. UNORDERED_CODE is used
9150 for modes that can hold NaNs and ORDERED_CODE is used for
9151 the rest. */
9152
9153 static tree
9154 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
9155 enum tree_code unordered_code,
9156 enum tree_code ordered_code)
9157 {
9158 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9159 enum tree_code code;
9160 tree arg0, arg1;
9161 tree type0, type1;
9162 enum tree_code code0, code1;
9163 tree cmp_type = NULL_TREE;
9164
9165 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9166 {
9167 /* Check that we have exactly two arguments. */
9168 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
9169 {
9170 error ("too few arguments to function %qs",
9171 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9172 return error_mark_node;
9173 }
9174 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
9175 {
9176 error ("too many arguments to function %qs",
9177 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9178 return error_mark_node;
9179 }
9180 }
9181
9182 arg0 = TREE_VALUE (arglist);
9183 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9184
9185 type0 = TREE_TYPE (arg0);
9186 type1 = TREE_TYPE (arg1);
9187
9188 code0 = TREE_CODE (type0);
9189 code1 = TREE_CODE (type1);
9190
9191 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9192 /* Choose the wider of two real types. */
9193 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9194 ? type0 : type1;
9195 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9196 cmp_type = type0;
9197 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9198 cmp_type = type1;
9199 else
9200 {
9201 error ("non-floating-point argument to function %qs",
9202 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9203 return error_mark_node;
9204 }
9205
9206 arg0 = fold_convert (cmp_type, arg0);
9207 arg1 = fold_convert (cmp_type, arg1);
9208
9209 if (unordered_code == UNORDERED_EXPR)
9210 {
9211 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9212 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9213 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9214 }
9215
9216 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9217 : ordered_code;
9218 return fold_build1 (TRUTH_NOT_EXPR, type,
9219 fold_build2 (code, type, arg0, arg1));
9220 }
9221
9222 /* Used by constant folding to simplify calls to builtin functions. EXP is
9223 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9224 result of the function call is ignored. This function returns NULL_TREE
9225 if no simplification was possible. */
9226
9227 static tree
9228 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9229 {
9230 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9231 enum built_in_function fcode;
9232
9233 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9234 return targetm.fold_builtin (fndecl, arglist, ignore);
9235
9236 fcode = DECL_FUNCTION_CODE (fndecl);
9237 switch (fcode)
9238 {
9239 case BUILT_IN_FPUTS:
9240 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9241
9242 case BUILT_IN_FPUTS_UNLOCKED:
9243 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9244
9245 case BUILT_IN_STRSTR:
9246 return fold_builtin_strstr (arglist, type);
9247
9248 case BUILT_IN_STRCAT:
9249 return fold_builtin_strcat (arglist);
9250
9251 case BUILT_IN_STRNCAT:
9252 return fold_builtin_strncat (arglist);
9253
9254 case BUILT_IN_STRSPN:
9255 return fold_builtin_strspn (arglist);
9256
9257 case BUILT_IN_STRCSPN:
9258 return fold_builtin_strcspn (arglist);
9259
9260 case BUILT_IN_STRCHR:
9261 case BUILT_IN_INDEX:
9262 return fold_builtin_strchr (arglist, type);
9263
9264 case BUILT_IN_STRRCHR:
9265 case BUILT_IN_RINDEX:
9266 return fold_builtin_strrchr (arglist, type);
9267
9268 case BUILT_IN_STRCPY:
9269 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9270
9271 case BUILT_IN_STRNCPY:
9272 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9273
9274 case BUILT_IN_STRCMP:
9275 return fold_builtin_strcmp (arglist);
9276
9277 case BUILT_IN_STRNCMP:
9278 return fold_builtin_strncmp (arglist);
9279
9280 case BUILT_IN_STRPBRK:
9281 return fold_builtin_strpbrk (arglist, type);
9282
9283 case BUILT_IN_BCMP:
9284 case BUILT_IN_MEMCMP:
9285 return fold_builtin_memcmp (arglist);
9286
9287 case BUILT_IN_SPRINTF:
9288 return fold_builtin_sprintf (arglist, ignore);
9289
9290 case BUILT_IN_CONSTANT_P:
9291 {
9292 tree val;
9293
9294 val = fold_builtin_constant_p (arglist);
9295 /* Gimplification will pull the CALL_EXPR for the builtin out of
9296 an if condition. When not optimizing, we'll not CSE it back.
9297 To avoid link error types of regressions, return false now. */
9298 if (!val && !optimize)
9299 val = integer_zero_node;
9300
9301 return val;
9302 }
9303
9304 case BUILT_IN_EXPECT:
9305 return fold_builtin_expect (arglist);
9306
9307 case BUILT_IN_CLASSIFY_TYPE:
9308 return fold_builtin_classify_type (arglist);
9309
9310 case BUILT_IN_STRLEN:
9311 return fold_builtin_strlen (arglist);
9312
9313 CASE_FLT_FN (BUILT_IN_FABS):
9314 return fold_builtin_fabs (arglist, type);
9315
9316 case BUILT_IN_ABS:
9317 case BUILT_IN_LABS:
9318 case BUILT_IN_LLABS:
9319 case BUILT_IN_IMAXABS:
9320 return fold_builtin_abs (arglist, type);
9321
9322 CASE_FLT_FN (BUILT_IN_CONJ):
9323 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9324 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9325 break;
9326
9327 CASE_FLT_FN (BUILT_IN_CREAL):
9328 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9329 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9330 TREE_VALUE (arglist)));
9331 break;
9332
9333 CASE_FLT_FN (BUILT_IN_CIMAG):
9334 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9335 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9336 TREE_VALUE (arglist)));
9337 break;
9338
9339 CASE_FLT_FN (BUILT_IN_CABS):
9340 return fold_builtin_cabs (arglist, type, fndecl);
9341
9342 CASE_FLT_FN (BUILT_IN_SQRT):
9343 return fold_builtin_sqrt (arglist, type);
9344
9345 CASE_FLT_FN (BUILT_IN_CBRT):
9346 return fold_builtin_cbrt (arglist, type);
9347
9348 CASE_FLT_FN (BUILT_IN_ASIN):
9349 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9350 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9351 &dconstm1, &dconst1, true);
9352 break;
9353
9354 CASE_FLT_FN (BUILT_IN_ACOS):
9355 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9356 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9357 &dconstm1, &dconst1, true);
9358 break;
9359
9360 CASE_FLT_FN (BUILT_IN_ATAN):
9361 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9362 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9363 NULL, NULL, 0);
9364 break;
9365
9366 CASE_FLT_FN (BUILT_IN_ASINH):
9367 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9368 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9369 NULL, NULL, 0);
9370 break;
9371
9372 CASE_FLT_FN (BUILT_IN_ACOSH):
9373 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9374 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9375 &dconst1, NULL, true);
9376 break;
9377
9378 CASE_FLT_FN (BUILT_IN_ATANH):
9379 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9380 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9381 &dconstm1, &dconst1, false);
9382 break;
9383
9384 CASE_FLT_FN (BUILT_IN_SIN):
9385 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9386 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9387 NULL, NULL, 0);
9388 break;
9389
9390 CASE_FLT_FN (BUILT_IN_COS):
9391 return fold_builtin_cos (arglist, type, fndecl);
9392
9393 CASE_FLT_FN (BUILT_IN_TAN):
9394 return fold_builtin_tan (arglist, type);
9395
9396 CASE_FLT_FN (BUILT_IN_SINCOS):
9397 return fold_builtin_sincos (arglist);
9398
9399 CASE_FLT_FN (BUILT_IN_CEXP):
9400 return fold_builtin_cexp (arglist, type);
9401
9402 CASE_FLT_FN (BUILT_IN_CEXPI):
9403 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9404 return do_mpfr_sincos (TREE_VALUE (arglist), NULL_TREE, NULL_TREE);
9405
9406 CASE_FLT_FN (BUILT_IN_SINH):
9407 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9408 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9409 NULL, NULL, 0);
9410 break;
9411
9412 CASE_FLT_FN (BUILT_IN_COSH):
9413 return fold_builtin_cosh (arglist, type, fndecl);
9414
9415 CASE_FLT_FN (BUILT_IN_TANH):
9416 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9417 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9418 NULL, NULL, 0);
9419 break;
9420
9421 CASE_FLT_FN (BUILT_IN_ERF):
9422 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9423 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9424 NULL, NULL, 0);
9425 break;
9426
9427 CASE_FLT_FN (BUILT_IN_ERFC):
9428 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9429 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9430 NULL, NULL, 0);
9431 break;
9432
9433 CASE_FLT_FN (BUILT_IN_TGAMMA):
9434 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9435 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9436 NULL, NULL, 0);
9437 break;
9438
9439 CASE_FLT_FN (BUILT_IN_EXP):
9440 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9441
9442 CASE_FLT_FN (BUILT_IN_EXP2):
9443 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9444
9445 CASE_FLT_FN (BUILT_IN_EXP10):
9446 CASE_FLT_FN (BUILT_IN_POW10):
9447 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9448
9449 CASE_FLT_FN (BUILT_IN_EXPM1):
9450 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9451 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9452 NULL, NULL, 0);
9453 break;
9454
9455 CASE_FLT_FN (BUILT_IN_LOG):
9456 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9457
9458 CASE_FLT_FN (BUILT_IN_LOG2):
9459 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9460
9461 CASE_FLT_FN (BUILT_IN_LOG10):
9462 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9463
9464 CASE_FLT_FN (BUILT_IN_LOG1P):
9465 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9466 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9467 &dconstm1, NULL, false);
9468 break;
9469
9470 CASE_FLT_FN (BUILT_IN_ATAN2):
9471 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9472 return do_mpfr_arg2 (TREE_VALUE (arglist),
9473 TREE_VALUE (TREE_CHAIN (arglist)),
9474 type, mpfr_atan2);
9475 break;
9476
9477 CASE_FLT_FN (BUILT_IN_FDIM):
9478 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9479 return do_mpfr_arg2 (TREE_VALUE (arglist),
9480 TREE_VALUE (TREE_CHAIN (arglist)),
9481 type, mpfr_dim);
9482 break;
9483
9484 CASE_FLT_FN (BUILT_IN_FMA):
9485 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9486 return do_mpfr_arg3 (TREE_VALUE (arglist),
9487 TREE_VALUE (TREE_CHAIN (arglist)),
9488 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
9489 type, mpfr_fma);
9490 break;
9491
9492 CASE_FLT_FN (BUILT_IN_FMIN):
9493 return fold_builtin_fmin_fmax (arglist, type, /*max=*/false);
9494
9495 CASE_FLT_FN (BUILT_IN_FMAX):
9496 return fold_builtin_fmin_fmax (arglist, type, /*max=*/true);
9497
9498 CASE_FLT_FN (BUILT_IN_HYPOT):
9499 return fold_builtin_hypot (fndecl, arglist, type);
9500
9501 CASE_FLT_FN (BUILT_IN_POW):
9502 return fold_builtin_pow (fndecl, arglist, type);
9503
9504 CASE_FLT_FN (BUILT_IN_POWI):
9505 return fold_builtin_powi (fndecl, arglist, type);
9506
9507 CASE_FLT_FN (BUILT_IN_INF):
9508 case BUILT_IN_INFD32:
9509 case BUILT_IN_INFD64:
9510 case BUILT_IN_INFD128:
9511 return fold_builtin_inf (type, true);
9512
9513 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9514 return fold_builtin_inf (type, false);
9515
9516 CASE_FLT_FN (BUILT_IN_NAN):
9517 case BUILT_IN_NAND32:
9518 case BUILT_IN_NAND64:
9519 case BUILT_IN_NAND128:
9520 return fold_builtin_nan (arglist, type, true);
9521
9522 CASE_FLT_FN (BUILT_IN_NANS):
9523 return fold_builtin_nan (arglist, type, false);
9524
9525 CASE_FLT_FN (BUILT_IN_FLOOR):
9526 return fold_builtin_floor (fndecl, arglist);
9527
9528 CASE_FLT_FN (BUILT_IN_CEIL):
9529 return fold_builtin_ceil (fndecl, arglist);
9530
9531 CASE_FLT_FN (BUILT_IN_TRUNC):
9532 return fold_builtin_trunc (fndecl, arglist);
9533
9534 CASE_FLT_FN (BUILT_IN_ROUND):
9535 return fold_builtin_round (fndecl, arglist);
9536
9537 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9538 CASE_FLT_FN (BUILT_IN_RINT):
9539 return fold_trunc_transparent_mathfn (fndecl, arglist);
9540
9541 CASE_FLT_FN (BUILT_IN_LCEIL):
9542 CASE_FLT_FN (BUILT_IN_LLCEIL):
9543 CASE_FLT_FN (BUILT_IN_LFLOOR):
9544 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9545 CASE_FLT_FN (BUILT_IN_LROUND):
9546 CASE_FLT_FN (BUILT_IN_LLROUND):
9547 return fold_builtin_int_roundingfn (fndecl, arglist);
9548
9549 CASE_FLT_FN (BUILT_IN_LRINT):
9550 CASE_FLT_FN (BUILT_IN_LLRINT):
9551 return fold_fixed_mathfn (fndecl, arglist);
9552
9553 case BUILT_IN_BSWAP32:
9554 case BUILT_IN_BSWAP64:
9555 return fold_builtin_bswap (fndecl, arglist);
9556
9557 CASE_INT_FN (BUILT_IN_FFS):
9558 CASE_INT_FN (BUILT_IN_CLZ):
9559 CASE_INT_FN (BUILT_IN_CTZ):
9560 CASE_INT_FN (BUILT_IN_POPCOUNT):
9561 CASE_INT_FN (BUILT_IN_PARITY):
9562 return fold_builtin_bitop (fndecl, arglist);
9563
9564 case BUILT_IN_MEMSET:
9565 return fold_builtin_memset (arglist, type, ignore);
9566
9567 case BUILT_IN_MEMCPY:
9568 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9569
9570 case BUILT_IN_MEMPCPY:
9571 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9572
9573 case BUILT_IN_MEMMOVE:
9574 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9575
9576 case BUILT_IN_BZERO:
9577 return fold_builtin_bzero (arglist, ignore);
9578
9579 case BUILT_IN_BCOPY:
9580 return fold_builtin_bcopy (arglist, ignore);
9581
9582 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9583 return fold_builtin_signbit (fndecl, arglist);
9584
9585 case BUILT_IN_ISASCII:
9586 return fold_builtin_isascii (arglist);
9587
9588 case BUILT_IN_TOASCII:
9589 return fold_builtin_toascii (arglist);
9590
9591 case BUILT_IN_ISDIGIT:
9592 return fold_builtin_isdigit (arglist);
9593
9594 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9595 return fold_builtin_copysign (fndecl, arglist, type);
9596
9597 CASE_FLT_FN (BUILT_IN_FINITE):
9598 case BUILT_IN_FINITED32:
9599 case BUILT_IN_FINITED64:
9600 case BUILT_IN_FINITED128:
9601 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9602
9603 CASE_FLT_FN (BUILT_IN_ISINF):
9604 case BUILT_IN_ISINFD32:
9605 case BUILT_IN_ISINFD64:
9606 case BUILT_IN_ISINFD128:
9607 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9608
9609 CASE_FLT_FN (BUILT_IN_ISNAN):
9610 case BUILT_IN_ISNAND32:
9611 case BUILT_IN_ISNAND64:
9612 case BUILT_IN_ISNAND128:
9613 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9614
9615 case BUILT_IN_ISGREATER:
9616 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9617 case BUILT_IN_ISGREATEREQUAL:
9618 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9619 case BUILT_IN_ISLESS:
9620 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9621 case BUILT_IN_ISLESSEQUAL:
9622 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9623 case BUILT_IN_ISLESSGREATER:
9624 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9625 case BUILT_IN_ISUNORDERED:
9626 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9627 NOP_EXPR);
9628
9629 /* We do the folding for va_start in the expander. */
9630 case BUILT_IN_VA_START:
9631 break;
9632
9633 case BUILT_IN_OBJECT_SIZE:
9634 return fold_builtin_object_size (arglist);
9635 case BUILT_IN_MEMCPY_CHK:
9636 case BUILT_IN_MEMPCPY_CHK:
9637 case BUILT_IN_MEMMOVE_CHK:
9638 case BUILT_IN_MEMSET_CHK:
9639 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9640 DECL_FUNCTION_CODE (fndecl));
9641 case BUILT_IN_STRCPY_CHK:
9642 case BUILT_IN_STPCPY_CHK:
9643 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9644 DECL_FUNCTION_CODE (fndecl));
9645 case BUILT_IN_STRNCPY_CHK:
9646 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9647 case BUILT_IN_STRCAT_CHK:
9648 return fold_builtin_strcat_chk (fndecl, arglist);
9649 case BUILT_IN_STRNCAT_CHK:
9650 return fold_builtin_strncat_chk (fndecl, arglist);
9651 case BUILT_IN_SPRINTF_CHK:
9652 case BUILT_IN_VSPRINTF_CHK:
9653 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9654 case BUILT_IN_SNPRINTF_CHK:
9655 case BUILT_IN_VSNPRINTF_CHK:
9656 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9657 DECL_FUNCTION_CODE (fndecl));
9658
9659 case BUILT_IN_PRINTF:
9660 case BUILT_IN_PRINTF_UNLOCKED:
9661 case BUILT_IN_VPRINTF:
9662 case BUILT_IN_PRINTF_CHK:
9663 case BUILT_IN_VPRINTF_CHK:
9664 return fold_builtin_printf (fndecl, arglist, ignore,
9665 DECL_FUNCTION_CODE (fndecl));
9666
9667 case BUILT_IN_FPRINTF:
9668 case BUILT_IN_FPRINTF_UNLOCKED:
9669 case BUILT_IN_VFPRINTF:
9670 case BUILT_IN_FPRINTF_CHK:
9671 case BUILT_IN_VFPRINTF_CHK:
9672 return fold_builtin_fprintf (fndecl, arglist, ignore,
9673 DECL_FUNCTION_CODE (fndecl));
9674
9675 default:
9676 break;
9677 }
9678
9679 return 0;
9680 }
9681
9682 /* A wrapper function for builtin folding that prevents warnings for
9683 "statement without effect" and the like, caused by removing the
9684 call node earlier than the warning is generated. */
9685
9686 tree
9687 fold_builtin (tree fndecl, tree arglist, bool ignore)
9688 {
9689 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9690 if (exp && !ignore)
9691 {
9692 exp = build1 (NOP_EXPR, GENERIC_TREE_TYPE (exp), exp);
9693 TREE_NO_WARNING (exp) = 1;
9694 }
9695
9696 return exp;
9697 }
9698
9699 /* Conveniently construct a function call expression. */
9700
9701 tree
9702 build_function_call_expr (tree fn, tree arglist)
9703 {
9704 tree call_expr;
9705
9706 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9707 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9708 call_expr, arglist, NULL_TREE);
9709 }
9710
9711 /* This function validates the types of a function call argument list
9712 represented as a tree chain of parameters against a specified list
9713 of tree_codes. If the last specifier is a 0, that represents an
9714 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9715
9716 int
9717 validate_arglist (tree arglist, ...)
9718 {
9719 enum tree_code code;
9720 int res = 0;
9721 va_list ap;
9722
9723 va_start (ap, arglist);
9724
9725 do
9726 {
9727 code = va_arg (ap, enum tree_code);
9728 switch (code)
9729 {
9730 case 0:
9731 /* This signifies an ellipses, any further arguments are all ok. */
9732 res = 1;
9733 goto end;
9734 case VOID_TYPE:
9735 /* This signifies an endlink, if no arguments remain, return
9736 true, otherwise return false. */
9737 res = arglist == 0;
9738 goto end;
9739 default:
9740 /* If no parameters remain or the parameter's code does not
9741 match the specified code, return false. Otherwise continue
9742 checking any remaining arguments. */
9743 if (arglist == 0)
9744 goto end;
9745 if (code == POINTER_TYPE)
9746 {
9747 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9748 goto end;
9749 }
9750 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9751 goto end;
9752 break;
9753 }
9754 arglist = TREE_CHAIN (arglist);
9755 }
9756 while (1);
9757
9758 /* We need gotos here since we can only have one VA_CLOSE in a
9759 function. */
9760 end: ;
9761 va_end (ap);
9762
9763 return res;
9764 }
9765
9766 /* Default target-specific builtin expander that does nothing. */
9767
9768 rtx
9769 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9770 rtx target ATTRIBUTE_UNUSED,
9771 rtx subtarget ATTRIBUTE_UNUSED,
9772 enum machine_mode mode ATTRIBUTE_UNUSED,
9773 int ignore ATTRIBUTE_UNUSED)
9774 {
9775 return NULL_RTX;
9776 }
9777
9778 /* Returns true is EXP represents data that would potentially reside
9779 in a readonly section. */
9780
9781 static bool
9782 readonly_data_expr (tree exp)
9783 {
9784 STRIP_NOPS (exp);
9785
9786 if (TREE_CODE (exp) != ADDR_EXPR)
9787 return false;
9788
9789 exp = get_base_address (TREE_OPERAND (exp, 0));
9790 if (!exp)
9791 return false;
9792
9793 /* Make sure we call decl_readonly_section only for trees it
9794 can handle (since it returns true for everything it doesn't
9795 understand). */
9796 if (TREE_CODE (exp) == STRING_CST
9797 || TREE_CODE (exp) == CONSTRUCTOR
9798 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9799 return decl_readonly_section (exp, 0);
9800 else
9801 return false;
9802 }
9803
9804 /* Simplify a call to the strstr builtin.
9805
9806 Return 0 if no simplification was possible, otherwise return the
9807 simplified form of the call as a tree.
9808
9809 The simplified form may be a constant or other expression which
9810 computes the same value, but in a more efficient manner (including
9811 calls to other builtin functions).
9812
9813 The call may contain arguments which need to be evaluated, but
9814 which are not useful to determine the result of the call. In
9815 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9816 COMPOUND_EXPR will be an argument which must be evaluated.
9817 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9818 COMPOUND_EXPR in the chain will contain the tree for the simplified
9819 form of the builtin function call. */
9820
9821 static tree
9822 fold_builtin_strstr (tree arglist, tree type)
9823 {
9824 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9825 return 0;
9826 else
9827 {
9828 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9829 tree fn;
9830 const char *p1, *p2;
9831
9832 p2 = c_getstr (s2);
9833 if (p2 == NULL)
9834 return 0;
9835
9836 p1 = c_getstr (s1);
9837 if (p1 != NULL)
9838 {
9839 const char *r = strstr (p1, p2);
9840 tree tem;
9841
9842 if (r == NULL)
9843 return build_int_cst (TREE_TYPE (s1), 0);
9844
9845 /* Return an offset into the constant string argument. */
9846 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9847 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9848 return fold_convert (type, tem);
9849 }
9850
9851 /* The argument is const char *, and the result is char *, so we need
9852 a type conversion here to avoid a warning. */
9853 if (p2[0] == '\0')
9854 return fold_convert (type, s1);
9855
9856 if (p2[1] != '\0')
9857 return 0;
9858
9859 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9860 if (!fn)
9861 return 0;
9862
9863 /* New argument list transforming strstr(s1, s2) to
9864 strchr(s1, s2[0]). */
9865 arglist = build_tree_list (NULL_TREE,
9866 build_int_cst (NULL_TREE, p2[0]));
9867 arglist = tree_cons (NULL_TREE, s1, arglist);
9868 return build_function_call_expr (fn, arglist);
9869 }
9870 }
9871
9872 /* Simplify a call to the strchr builtin.
9873
9874 Return 0 if no simplification was possible, otherwise return the
9875 simplified form of the call as a tree.
9876
9877 The simplified form may be a constant or other expression which
9878 computes the same value, but in a more efficient manner (including
9879 calls to other builtin functions).
9880
9881 The call may contain arguments which need to be evaluated, but
9882 which are not useful to determine the result of the call. In
9883 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9884 COMPOUND_EXPR will be an argument which must be evaluated.
9885 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9886 COMPOUND_EXPR in the chain will contain the tree for the simplified
9887 form of the builtin function call. */
9888
9889 static tree
9890 fold_builtin_strchr (tree arglist, tree type)
9891 {
9892 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9893 return 0;
9894 else
9895 {
9896 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9897 const char *p1;
9898
9899 if (TREE_CODE (s2) != INTEGER_CST)
9900 return 0;
9901
9902 p1 = c_getstr (s1);
9903 if (p1 != NULL)
9904 {
9905 char c;
9906 const char *r;
9907 tree tem;
9908
9909 if (target_char_cast (s2, &c))
9910 return 0;
9911
9912 r = strchr (p1, c);
9913
9914 if (r == NULL)
9915 return build_int_cst (TREE_TYPE (s1), 0);
9916
9917 /* Return an offset into the constant string argument. */
9918 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9919 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9920 return fold_convert (type, tem);
9921 }
9922 return 0;
9923 }
9924 }
9925
9926 /* Simplify a call to the strrchr builtin.
9927
9928 Return 0 if no simplification was possible, otherwise return the
9929 simplified form of the call as a tree.
9930
9931 The simplified form may be a constant or other expression which
9932 computes the same value, but in a more efficient manner (including
9933 calls to other builtin functions).
9934
9935 The call may contain arguments which need to be evaluated, but
9936 which are not useful to determine the result of the call. In
9937 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9938 COMPOUND_EXPR will be an argument which must be evaluated.
9939 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9940 COMPOUND_EXPR in the chain will contain the tree for the simplified
9941 form of the builtin function call. */
9942
9943 static tree
9944 fold_builtin_strrchr (tree arglist, tree type)
9945 {
9946 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9947 return 0;
9948 else
9949 {
9950 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9951 tree fn;
9952 const char *p1;
9953
9954 if (TREE_CODE (s2) != INTEGER_CST)
9955 return 0;
9956
9957 p1 = c_getstr (s1);
9958 if (p1 != NULL)
9959 {
9960 char c;
9961 const char *r;
9962 tree tem;
9963
9964 if (target_char_cast (s2, &c))
9965 return 0;
9966
9967 r = strrchr (p1, c);
9968
9969 if (r == NULL)
9970 return build_int_cst (TREE_TYPE (s1), 0);
9971
9972 /* Return an offset into the constant string argument. */
9973 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9974 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9975 return fold_convert (type, tem);
9976 }
9977
9978 if (! integer_zerop (s2))
9979 return 0;
9980
9981 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9982 if (!fn)
9983 return 0;
9984
9985 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9986 return build_function_call_expr (fn, arglist);
9987 }
9988 }
9989
9990 /* Simplify a call to the strpbrk builtin.
9991
9992 Return 0 if no simplification was possible, otherwise return the
9993 simplified form of the call as a tree.
9994
9995 The simplified form may be a constant or other expression which
9996 computes the same value, but in a more efficient manner (including
9997 calls to other builtin functions).
9998
9999 The call may contain arguments which need to be evaluated, but
10000 which are not useful to determine the result of the call. In
10001 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10002 COMPOUND_EXPR will be an argument which must be evaluated.
10003 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10004 COMPOUND_EXPR in the chain will contain the tree for the simplified
10005 form of the builtin function call. */
10006
10007 static tree
10008 fold_builtin_strpbrk (tree arglist, tree type)
10009 {
10010 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10011 return 0;
10012 else
10013 {
10014 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10015 tree fn;
10016 const char *p1, *p2;
10017
10018 p2 = c_getstr (s2);
10019 if (p2 == NULL)
10020 return 0;
10021
10022 p1 = c_getstr (s1);
10023 if (p1 != NULL)
10024 {
10025 const char *r = strpbrk (p1, p2);
10026 tree tem;
10027
10028 if (r == NULL)
10029 return build_int_cst (TREE_TYPE (s1), 0);
10030
10031 /* Return an offset into the constant string argument. */
10032 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
10033 s1, build_int_cst (TREE_TYPE (s1), r - p1));
10034 return fold_convert (type, tem);
10035 }
10036
10037 if (p2[0] == '\0')
10038 /* strpbrk(x, "") == NULL.
10039 Evaluate and ignore s1 in case it had side-effects. */
10040 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
10041
10042 if (p2[1] != '\0')
10043 return 0; /* Really call strpbrk. */
10044
10045 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10046 if (!fn)
10047 return 0;
10048
10049 /* New argument list transforming strpbrk(s1, s2) to
10050 strchr(s1, s2[0]). */
10051 arglist = build_tree_list (NULL_TREE,
10052 build_int_cst (NULL_TREE, p2[0]));
10053 arglist = tree_cons (NULL_TREE, s1, arglist);
10054 return build_function_call_expr (fn, arglist);
10055 }
10056 }
10057
10058 /* Simplify a call to the strcat builtin.
10059
10060 Return 0 if no simplification was possible, otherwise return the
10061 simplified form of the call as a tree.
10062
10063 The simplified form may be a constant or other expression which
10064 computes the same value, but in a more efficient manner (including
10065 calls to other builtin functions).
10066
10067 The call may contain arguments which need to be evaluated, but
10068 which are not useful to determine the result of the call. In
10069 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10070 COMPOUND_EXPR will be an argument which must be evaluated.
10071 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10072 COMPOUND_EXPR in the chain will contain the tree for the simplified
10073 form of the builtin function call. */
10074
10075 static tree
10076 fold_builtin_strcat (tree arglist)
10077 {
10078 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10079 return 0;
10080 else
10081 {
10082 tree dst = TREE_VALUE (arglist),
10083 src = TREE_VALUE (TREE_CHAIN (arglist));
10084 const char *p = c_getstr (src);
10085
10086 /* If the string length is zero, return the dst parameter. */
10087 if (p && *p == '\0')
10088 return dst;
10089
10090 return 0;
10091 }
10092 }
10093
10094 /* Simplify a call to the strncat builtin.
10095
10096 Return 0 if no simplification was possible, otherwise return the
10097 simplified form of the call as a tree.
10098
10099 The simplified form may be a constant or other expression which
10100 computes the same value, but in a more efficient manner (including
10101 calls to other builtin functions).
10102
10103 The call may contain arguments which need to be evaluated, but
10104 which are not useful to determine the result of the call. In
10105 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10106 COMPOUND_EXPR will be an argument which must be evaluated.
10107 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10108 COMPOUND_EXPR in the chain will contain the tree for the simplified
10109 form of the builtin function call. */
10110
10111 static tree
10112 fold_builtin_strncat (tree arglist)
10113 {
10114 if (!validate_arglist (arglist,
10115 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10116 return 0;
10117 else
10118 {
10119 tree dst = TREE_VALUE (arglist);
10120 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10121 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10122 const char *p = c_getstr (src);
10123
10124 /* If the requested length is zero, or the src parameter string
10125 length is zero, return the dst parameter. */
10126 if (integer_zerop (len) || (p && *p == '\0'))
10127 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
10128
10129 /* If the requested len is greater than or equal to the string
10130 length, call strcat. */
10131 if (TREE_CODE (len) == INTEGER_CST && p
10132 && compare_tree_int (len, strlen (p)) >= 0)
10133 {
10134 tree newarglist
10135 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
10136 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
10137
10138 /* If the replacement _DECL isn't initialized, don't do the
10139 transformation. */
10140 if (!fn)
10141 return 0;
10142
10143 return build_function_call_expr (fn, newarglist);
10144 }
10145 return 0;
10146 }
10147 }
10148
10149 /* Simplify a call to the strspn builtin.
10150
10151 Return 0 if no simplification was possible, otherwise return the
10152 simplified form of the call as a tree.
10153
10154 The simplified form may be a constant or other expression which
10155 computes the same value, but in a more efficient manner (including
10156 calls to other builtin functions).
10157
10158 The call may contain arguments which need to be evaluated, but
10159 which are not useful to determine the result of the call. In
10160 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10161 COMPOUND_EXPR will be an argument which must be evaluated.
10162 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10163 COMPOUND_EXPR in the chain will contain the tree for the simplified
10164 form of the builtin function call. */
10165
10166 static tree
10167 fold_builtin_strspn (tree arglist)
10168 {
10169 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10170 return 0;
10171 else
10172 {
10173 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10174 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10175
10176 /* If both arguments are constants, evaluate at compile-time. */
10177 if (p1 && p2)
10178 {
10179 const size_t r = strspn (p1, p2);
10180 return size_int (r);
10181 }
10182
10183 /* If either argument is "", return 0. */
10184 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10185 /* Evaluate and ignore both arguments in case either one has
10186 side-effects. */
10187 return omit_two_operands (integer_type_node, integer_zero_node,
10188 s1, s2);
10189 return 0;
10190 }
10191 }
10192
10193 /* Simplify a call to the strcspn builtin.
10194
10195 Return 0 if no simplification was possible, otherwise return the
10196 simplified form of the call as a tree.
10197
10198 The simplified form may be a constant or other expression which
10199 computes the same value, but in a more efficient manner (including
10200 calls to other builtin functions).
10201
10202 The call may contain arguments which need to be evaluated, but
10203 which are not useful to determine the result of the call. In
10204 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10205 COMPOUND_EXPR will be an argument which must be evaluated.
10206 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10207 COMPOUND_EXPR in the chain will contain the tree for the simplified
10208 form of the builtin function call. */
10209
10210 static tree
10211 fold_builtin_strcspn (tree arglist)
10212 {
10213 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10214 return 0;
10215 else
10216 {
10217 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10218 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10219
10220 /* If both arguments are constants, evaluate at compile-time. */
10221 if (p1 && p2)
10222 {
10223 const size_t r = strcspn (p1, p2);
10224 return size_int (r);
10225 }
10226
10227 /* If the first argument is "", return 0. */
10228 if (p1 && *p1 == '\0')
10229 {
10230 /* Evaluate and ignore argument s2 in case it has
10231 side-effects. */
10232 return omit_one_operand (integer_type_node,
10233 integer_zero_node, s2);
10234 }
10235
10236 /* If the second argument is "", return __builtin_strlen(s1). */
10237 if (p2 && *p2 == '\0')
10238 {
10239 tree newarglist = build_tree_list (NULL_TREE, s1),
10240 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10241
10242 /* If the replacement _DECL isn't initialized, don't do the
10243 transformation. */
10244 if (!fn)
10245 return 0;
10246
10247 return build_function_call_expr (fn, newarglist);
10248 }
10249 return 0;
10250 }
10251 }
10252
10253 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10254 by the builtin will be ignored. UNLOCKED is true is true if this
10255 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10256 the known length of the string. Return NULL_TREE if no simplification
10257 was possible. */
10258
10259 tree
10260 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10261 {
10262 tree fn;
10263 /* If we're using an unlocked function, assume the other unlocked
10264 functions exist explicitly. */
10265 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10266 : implicit_built_in_decls[BUILT_IN_FPUTC];
10267 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10268 : implicit_built_in_decls[BUILT_IN_FWRITE];
10269
10270 /* If the return value is used, don't do the transformation. */
10271 if (!ignore)
10272 return 0;
10273
10274 /* Verify the arguments in the original call. */
10275 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10276 return 0;
10277
10278 if (! len)
10279 len = c_strlen (TREE_VALUE (arglist), 0);
10280
10281 /* Get the length of the string passed to fputs. If the length
10282 can't be determined, punt. */
10283 if (!len
10284 || TREE_CODE (len) != INTEGER_CST)
10285 return 0;
10286
10287 switch (compare_tree_int (len, 1))
10288 {
10289 case -1: /* length is 0, delete the call entirely . */
10290 return omit_one_operand (integer_type_node, integer_zero_node,
10291 TREE_VALUE (TREE_CHAIN (arglist)));
10292
10293 case 0: /* length is 1, call fputc. */
10294 {
10295 const char *p = c_getstr (TREE_VALUE (arglist));
10296
10297 if (p != NULL)
10298 {
10299 /* New argument list transforming fputs(string, stream) to
10300 fputc(string[0], stream). */
10301 arglist = build_tree_list (NULL_TREE,
10302 TREE_VALUE (TREE_CHAIN (arglist)));
10303 arglist = tree_cons (NULL_TREE,
10304 build_int_cst (NULL_TREE, p[0]),
10305 arglist);
10306 fn = fn_fputc;
10307 break;
10308 }
10309 }
10310 /* FALLTHROUGH */
10311 case 1: /* length is greater than 1, call fwrite. */
10312 {
10313 tree string_arg;
10314
10315 /* If optimizing for size keep fputs. */
10316 if (optimize_size)
10317 return 0;
10318 string_arg = TREE_VALUE (arglist);
10319 /* New argument list transforming fputs(string, stream) to
10320 fwrite(string, 1, len, stream). */
10321 arglist = build_tree_list (NULL_TREE,
10322 TREE_VALUE (TREE_CHAIN (arglist)));
10323 arglist = tree_cons (NULL_TREE, len, arglist);
10324 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10325 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10326 fn = fn_fwrite;
10327 break;
10328 }
10329 default:
10330 gcc_unreachable ();
10331 }
10332
10333 /* If the replacement _DECL isn't initialized, don't do the
10334 transformation. */
10335 if (!fn)
10336 return 0;
10337
10338 /* These optimizations are only performed when the result is ignored,
10339 hence there's no need to cast the result to integer_type_node. */
10340 return build_function_call_expr (fn, arglist);
10341 }
10342
10343 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10344 produced. False otherwise. This is done so that we don't output the error
10345 or warning twice or three times. */
10346 bool
10347 fold_builtin_next_arg (tree arglist)
10348 {
10349 tree fntype = TREE_TYPE (current_function_decl);
10350
10351 if (TYPE_ARG_TYPES (fntype) == 0
10352 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10353 == void_type_node))
10354 {
10355 error ("%<va_start%> used in function with fixed args");
10356 return true;
10357 }
10358 else if (!arglist)
10359 {
10360 /* Evidently an out of date version of <stdarg.h>; can't validate
10361 va_start's second argument, but can still work as intended. */
10362 warning (0, "%<__builtin_next_arg%> called without an argument");
10363 return true;
10364 }
10365 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10366 when we checked the arguments and if needed issued a warning. */
10367 else if (!TREE_CHAIN (arglist)
10368 || !integer_zerop (TREE_VALUE (arglist))
10369 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10370 || TREE_CHAIN (TREE_CHAIN (arglist)))
10371 {
10372 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10373 tree arg = TREE_VALUE (arglist);
10374
10375 if (TREE_CHAIN (arglist))
10376 {
10377 error ("%<va_start%> used with too many arguments");
10378 return true;
10379 }
10380
10381 /* Strip off all nops for the sake of the comparison. This
10382 is not quite the same as STRIP_NOPS. It does more.
10383 We must also strip off INDIRECT_EXPR for C++ reference
10384 parameters. */
10385 while (TREE_CODE (arg) == NOP_EXPR
10386 || TREE_CODE (arg) == CONVERT_EXPR
10387 || TREE_CODE (arg) == NON_LVALUE_EXPR
10388 || TREE_CODE (arg) == INDIRECT_REF)
10389 arg = TREE_OPERAND (arg, 0);
10390 if (arg != last_parm)
10391 {
10392 /* FIXME: Sometimes with the tree optimizers we can get the
10393 not the last argument even though the user used the last
10394 argument. We just warn and set the arg to be the last
10395 argument so that we will get wrong-code because of
10396 it. */
10397 warning (0, "second parameter of %<va_start%> not last named argument");
10398 }
10399 /* We want to verify the second parameter just once before the tree
10400 optimizers are run and then avoid keeping it in the tree,
10401 as otherwise we could warn even for correct code like:
10402 void foo (int i, ...)
10403 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10404 TREE_VALUE (arglist) = integer_zero_node;
10405 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10406 }
10407 return false;
10408 }
10409
10410
10411 /* Simplify a call to the sprintf builtin.
10412
10413 Return 0 if no simplification was possible, otherwise return the
10414 simplified form of the call as a tree. If IGNORED is true, it means that
10415 the caller does not use the returned value of the function. */
10416
10417 static tree
10418 fold_builtin_sprintf (tree arglist, int ignored)
10419 {
10420 tree call, retval, dest, fmt;
10421 const char *fmt_str = NULL;
10422
10423 /* Verify the required arguments in the original call. We deal with two
10424 types of sprintf() calls: 'sprintf (str, fmt)' and
10425 'sprintf (dest, "%s", orig)'. */
10426 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10427 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10428 VOID_TYPE))
10429 return NULL_TREE;
10430
10431 /* Get the destination string and the format specifier. */
10432 dest = TREE_VALUE (arglist);
10433 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10434
10435 /* Check whether the format is a literal string constant. */
10436 fmt_str = c_getstr (fmt);
10437 if (fmt_str == NULL)
10438 return NULL_TREE;
10439
10440 call = NULL_TREE;
10441 retval = NULL_TREE;
10442
10443 if (!init_target_chars())
10444 return 0;
10445
10446 /* If the format doesn't contain % args or %%, use strcpy. */
10447 if (strchr (fmt_str, target_percent) == NULL)
10448 {
10449 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10450
10451 if (!fn)
10452 return NULL_TREE;
10453
10454 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10455 'format' is known to contain no % formats. */
10456 arglist = build_tree_list (NULL_TREE, fmt);
10457 arglist = tree_cons (NULL_TREE, dest, arglist);
10458 call = build_function_call_expr (fn, arglist);
10459 if (!ignored)
10460 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10461 }
10462
10463 /* If the format is "%s", use strcpy if the result isn't used. */
10464 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10465 {
10466 tree fn, orig;
10467 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10468
10469 if (!fn)
10470 return NULL_TREE;
10471
10472 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10473 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10474 arglist = build_tree_list (NULL_TREE, orig);
10475 arglist = tree_cons (NULL_TREE, dest, arglist);
10476 if (!ignored)
10477 {
10478 retval = c_strlen (orig, 1);
10479 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10480 return NULL_TREE;
10481 }
10482 call = build_function_call_expr (fn, arglist);
10483 }
10484
10485 if (call && retval)
10486 {
10487 retval = fold_convert
10488 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10489 retval);
10490 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10491 }
10492 else
10493 return call;
10494 }
10495
10496 /* Expand a call to __builtin_object_size. */
10497
10498 rtx
10499 expand_builtin_object_size (tree exp)
10500 {
10501 tree ost;
10502 int object_size_type;
10503 tree fndecl = get_callee_fndecl (exp);
10504 tree arglist = TREE_OPERAND (exp, 1);
10505 location_t locus = EXPR_LOCATION (exp);
10506
10507 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10508 {
10509 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10510 &locus, fndecl);
10511 expand_builtin_trap ();
10512 return const0_rtx;
10513 }
10514
10515 ost = TREE_VALUE (TREE_CHAIN (arglist));
10516 STRIP_NOPS (ost);
10517
10518 if (TREE_CODE (ost) != INTEGER_CST
10519 || tree_int_cst_sgn (ost) < 0
10520 || compare_tree_int (ost, 3) > 0)
10521 {
10522 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10523 &locus, fndecl);
10524 expand_builtin_trap ();
10525 return const0_rtx;
10526 }
10527
10528 object_size_type = tree_low_cst (ost, 0);
10529
10530 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10531 }
10532
10533 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10534 FCODE is the BUILT_IN_* to use.
10535 Return 0 if we failed; the caller should emit a normal call,
10536 otherwise try to get the result in TARGET, if convenient (and in
10537 mode MODE if that's convenient). */
10538
10539 static rtx
10540 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10541 enum built_in_function fcode)
10542 {
10543 tree arglist = TREE_OPERAND (exp, 1);
10544 tree dest, src, len, size;
10545
10546 if (!validate_arglist (arglist,
10547 POINTER_TYPE,
10548 fcode == BUILT_IN_MEMSET_CHK
10549 ? INTEGER_TYPE : POINTER_TYPE,
10550 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10551 return 0;
10552
10553 dest = TREE_VALUE (arglist);
10554 src = TREE_VALUE (TREE_CHAIN (arglist));
10555 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10556 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10557
10558 if (! host_integerp (size, 1))
10559 return 0;
10560
10561 if (host_integerp (len, 1) || integer_all_onesp (size))
10562 {
10563 tree fn;
10564
10565 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10566 {
10567 location_t locus = EXPR_LOCATION (exp);
10568 warning (0, "%Hcall to %D will always overflow destination buffer",
10569 &locus, get_callee_fndecl (exp));
10570 return 0;
10571 }
10572
10573 arglist = build_tree_list (NULL_TREE, len);
10574 arglist = tree_cons (NULL_TREE, src, arglist);
10575 arglist = tree_cons (NULL_TREE, dest, arglist);
10576
10577 fn = NULL_TREE;
10578 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10579 mem{cpy,pcpy,move,set} is available. */
10580 switch (fcode)
10581 {
10582 case BUILT_IN_MEMCPY_CHK:
10583 fn = built_in_decls[BUILT_IN_MEMCPY];
10584 break;
10585 case BUILT_IN_MEMPCPY_CHK:
10586 fn = built_in_decls[BUILT_IN_MEMPCPY];
10587 break;
10588 case BUILT_IN_MEMMOVE_CHK:
10589 fn = built_in_decls[BUILT_IN_MEMMOVE];
10590 break;
10591 case BUILT_IN_MEMSET_CHK:
10592 fn = built_in_decls[BUILT_IN_MEMSET];
10593 break;
10594 default:
10595 break;
10596 }
10597
10598 if (! fn)
10599 return 0;
10600
10601 fn = build_function_call_expr (fn, arglist);
10602 if (TREE_CODE (fn) == CALL_EXPR)
10603 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10604 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10605 }
10606 else if (fcode == BUILT_IN_MEMSET_CHK)
10607 return 0;
10608 else
10609 {
10610 unsigned int dest_align
10611 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10612
10613 /* If DEST is not a pointer type, call the normal function. */
10614 if (dest_align == 0)
10615 return 0;
10616
10617 /* If SRC and DEST are the same (and not volatile), do nothing. */
10618 if (operand_equal_p (src, dest, 0))
10619 {
10620 tree expr;
10621
10622 if (fcode != BUILT_IN_MEMPCPY_CHK)
10623 {
10624 /* Evaluate and ignore LEN in case it has side-effects. */
10625 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10626 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10627 }
10628
10629 len = fold_convert (TREE_TYPE (dest), len);
10630 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10631 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10632 }
10633
10634 /* __memmove_chk special case. */
10635 if (fcode == BUILT_IN_MEMMOVE_CHK)
10636 {
10637 unsigned int src_align
10638 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10639
10640 if (src_align == 0)
10641 return 0;
10642
10643 /* If src is categorized for a readonly section we can use
10644 normal __memcpy_chk. */
10645 if (readonly_data_expr (src))
10646 {
10647 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10648 if (!fn)
10649 return 0;
10650 fn = build_function_call_expr (fn, arglist);
10651 if (TREE_CODE (fn) == CALL_EXPR)
10652 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10653 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10654 }
10655 }
10656 return 0;
10657 }
10658 }
10659
10660 /* Emit warning if a buffer overflow is detected at compile time. */
10661
10662 static void
10663 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10664 {
10665 int arg_mask, is_strlen = 0;
10666 tree arglist = TREE_OPERAND (exp, 1), a;
10667 tree len, size;
10668 location_t locus;
10669
10670 switch (fcode)
10671 {
10672 case BUILT_IN_STRCPY_CHK:
10673 case BUILT_IN_STPCPY_CHK:
10674 /* For __strcat_chk the warning will be emitted only if overflowing
10675 by at least strlen (dest) + 1 bytes. */
10676 case BUILT_IN_STRCAT_CHK:
10677 arg_mask = 6;
10678 is_strlen = 1;
10679 break;
10680 case BUILT_IN_STRNCAT_CHK:
10681 /* For __strncat_chk the warning will be emitted only if overflowing
10682 by at least strlen (dest) + 1 bytes. */
10683 arg_mask = 12;
10684 break;
10685 case BUILT_IN_STRNCPY_CHK:
10686 arg_mask = 12;
10687 break;
10688 case BUILT_IN_SNPRINTF_CHK:
10689 case BUILT_IN_VSNPRINTF_CHK:
10690 arg_mask = 10;
10691 break;
10692 default:
10693 gcc_unreachable ();
10694 }
10695
10696 len = NULL_TREE;
10697 size = NULL_TREE;
10698 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10699 if (arg_mask & 1)
10700 {
10701 if (len)
10702 size = a;
10703 else
10704 len = a;
10705 }
10706
10707 if (!len || !size)
10708 return;
10709
10710 len = TREE_VALUE (len);
10711 size = TREE_VALUE (size);
10712
10713 if (! host_integerp (size, 1) || integer_all_onesp (size))
10714 return;
10715
10716 if (is_strlen)
10717 {
10718 len = c_strlen (len, 1);
10719 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10720 return;
10721 }
10722 else if (fcode == BUILT_IN_STRNCAT_CHK)
10723 {
10724 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10725 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10726 return;
10727 src = c_strlen (src, 1);
10728 if (! src || ! host_integerp (src, 1))
10729 {
10730 locus = EXPR_LOCATION (exp);
10731 warning (0, "%Hcall to %D might overflow destination buffer",
10732 &locus, get_callee_fndecl (exp));
10733 return;
10734 }
10735 else if (tree_int_cst_lt (src, size))
10736 return;
10737 }
10738 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10739 return;
10740
10741 locus = EXPR_LOCATION (exp);
10742 warning (0, "%Hcall to %D will always overflow destination buffer",
10743 &locus, get_callee_fndecl (exp));
10744 }
10745
10746 /* Emit warning if a buffer overflow is detected at compile time
10747 in __sprintf_chk/__vsprintf_chk calls. */
10748
10749 static void
10750 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10751 {
10752 tree arglist = TREE_OPERAND (exp, 1);
10753 tree dest, size, len, fmt, flag;
10754 const char *fmt_str;
10755
10756 /* Verify the required arguments in the original call. */
10757 if (! arglist)
10758 return;
10759 dest = TREE_VALUE (arglist);
10760 arglist = TREE_CHAIN (arglist);
10761 if (! arglist)
10762 return;
10763 flag = TREE_VALUE (arglist);
10764 arglist = TREE_CHAIN (arglist);
10765 if (! arglist)
10766 return;
10767 size = TREE_VALUE (arglist);
10768 arglist = TREE_CHAIN (arglist);
10769 if (! arglist)
10770 return;
10771 fmt = TREE_VALUE (arglist);
10772 arglist = TREE_CHAIN (arglist);
10773
10774 if (! host_integerp (size, 1) || integer_all_onesp (size))
10775 return;
10776
10777 /* Check whether the format is a literal string constant. */
10778 fmt_str = c_getstr (fmt);
10779 if (fmt_str == NULL)
10780 return;
10781
10782 if (!init_target_chars())
10783 return;
10784
10785 /* If the format doesn't contain % args or %%, we know its size. */
10786 if (strchr (fmt_str, target_percent) == 0)
10787 len = build_int_cstu (size_type_node, strlen (fmt_str));
10788 /* If the format is "%s" and first ... argument is a string literal,
10789 we know it too. */
10790 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10791 {
10792 tree arg;
10793
10794 if (! arglist)
10795 return;
10796 arg = TREE_VALUE (arglist);
10797 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10798 return;
10799
10800 len = c_strlen (arg, 1);
10801 if (!len || ! host_integerp (len, 1))
10802 return;
10803 }
10804 else
10805 return;
10806
10807 if (! tree_int_cst_lt (len, size))
10808 {
10809 location_t locus = EXPR_LOCATION (exp);
10810 warning (0, "%Hcall to %D will always overflow destination buffer",
10811 &locus, get_callee_fndecl (exp));
10812 }
10813 }
10814
10815 /* Fold a call to __builtin_object_size, if possible. */
10816
10817 tree
10818 fold_builtin_object_size (tree arglist)
10819 {
10820 tree ptr, ost, ret = 0;
10821 int object_size_type;
10822
10823 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10824 return 0;
10825
10826 ptr = TREE_VALUE (arglist);
10827 ost = TREE_VALUE (TREE_CHAIN (arglist));
10828 STRIP_NOPS (ost);
10829
10830 if (TREE_CODE (ost) != INTEGER_CST
10831 || tree_int_cst_sgn (ost) < 0
10832 || compare_tree_int (ost, 3) > 0)
10833 return 0;
10834
10835 object_size_type = tree_low_cst (ost, 0);
10836
10837 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10838 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10839 and (size_t) 0 for types 2 and 3. */
10840 if (TREE_SIDE_EFFECTS (ptr))
10841 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10842
10843 if (TREE_CODE (ptr) == ADDR_EXPR)
10844 ret = build_int_cstu (size_type_node,
10845 compute_builtin_object_size (ptr, object_size_type));
10846
10847 else if (TREE_CODE (ptr) == SSA_NAME)
10848 {
10849 unsigned HOST_WIDE_INT bytes;
10850
10851 /* If object size is not known yet, delay folding until
10852 later. Maybe subsequent passes will help determining
10853 it. */
10854 bytes = compute_builtin_object_size (ptr, object_size_type);
10855 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10856 ? -1 : 0))
10857 ret = build_int_cstu (size_type_node, bytes);
10858 }
10859
10860 if (ret)
10861 {
10862 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
10863 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
10864 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
10865 ret = NULL_TREE;
10866 }
10867
10868 return ret;
10869 }
10870
10871 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10872 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10873 code of the builtin. If MAXLEN is not NULL, it is maximum length
10874 passed as third argument. */
10875
10876 tree
10877 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10878 enum built_in_function fcode)
10879 {
10880 tree dest, src, len, size, fn;
10881
10882 if (!validate_arglist (arglist,
10883 POINTER_TYPE,
10884 fcode == BUILT_IN_MEMSET_CHK
10885 ? INTEGER_TYPE : POINTER_TYPE,
10886 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10887 return 0;
10888
10889 dest = TREE_VALUE (arglist);
10890 /* Actually val for __memset_chk, but it doesn't matter. */
10891 src = TREE_VALUE (TREE_CHAIN (arglist));
10892 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10893 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10894
10895 /* If SRC and DEST are the same (and not volatile), return DEST
10896 (resp. DEST+LEN for __mempcpy_chk). */
10897 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10898 {
10899 if (fcode != BUILT_IN_MEMPCPY_CHK)
10900 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10901 else
10902 {
10903 tree temp = fold_convert (TREE_TYPE (dest), len);
10904 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10905 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10906 }
10907 }
10908
10909 if (! host_integerp (size, 1))
10910 return 0;
10911
10912 if (! integer_all_onesp (size))
10913 {
10914 if (! host_integerp (len, 1))
10915 {
10916 /* If LEN is not constant, try MAXLEN too.
10917 For MAXLEN only allow optimizing into non-_ocs function
10918 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10919 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10920 {
10921 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10922 {
10923 /* (void) __mempcpy_chk () can be optimized into
10924 (void) __memcpy_chk (). */
10925 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10926 if (!fn)
10927 return 0;
10928
10929 return build_function_call_expr (fn, arglist);
10930 }
10931 return 0;
10932 }
10933 }
10934 else
10935 maxlen = len;
10936
10937 if (tree_int_cst_lt (size, maxlen))
10938 return 0;
10939 }
10940
10941 arglist = build_tree_list (NULL_TREE, len);
10942 arglist = tree_cons (NULL_TREE, src, arglist);
10943 arglist = tree_cons (NULL_TREE, dest, arglist);
10944
10945 fn = NULL_TREE;
10946 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10947 mem{cpy,pcpy,move,set} is available. */
10948 switch (fcode)
10949 {
10950 case BUILT_IN_MEMCPY_CHK:
10951 fn = built_in_decls[BUILT_IN_MEMCPY];
10952 break;
10953 case BUILT_IN_MEMPCPY_CHK:
10954 fn = built_in_decls[BUILT_IN_MEMPCPY];
10955 break;
10956 case BUILT_IN_MEMMOVE_CHK:
10957 fn = built_in_decls[BUILT_IN_MEMMOVE];
10958 break;
10959 case BUILT_IN_MEMSET_CHK:
10960 fn = built_in_decls[BUILT_IN_MEMSET];
10961 break;
10962 default:
10963 break;
10964 }
10965
10966 if (!fn)
10967 return 0;
10968
10969 return build_function_call_expr (fn, arglist);
10970 }
10971
10972 /* Fold a call to the __st[rp]cpy_chk builtin.
10973 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10974 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10975 strings passed as second argument. */
10976
10977 tree
10978 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10979 enum built_in_function fcode)
10980 {
10981 tree dest, src, size, len, fn;
10982
10983 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10984 VOID_TYPE))
10985 return 0;
10986
10987 dest = TREE_VALUE (arglist);
10988 src = TREE_VALUE (TREE_CHAIN (arglist));
10989 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10990
10991 /* If SRC and DEST are the same (and not volatile), return DEST. */
10992 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10993 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10994
10995 if (! host_integerp (size, 1))
10996 return 0;
10997
10998 if (! integer_all_onesp (size))
10999 {
11000 len = c_strlen (src, 1);
11001 if (! len || ! host_integerp (len, 1))
11002 {
11003 /* If LEN is not constant, try MAXLEN too.
11004 For MAXLEN only allow optimizing into non-_ocs function
11005 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11006 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11007 {
11008 if (fcode == BUILT_IN_STPCPY_CHK)
11009 {
11010 if (! ignore)
11011 return 0;
11012
11013 /* If return value of __stpcpy_chk is ignored,
11014 optimize into __strcpy_chk. */
11015 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
11016 if (!fn)
11017 return 0;
11018
11019 return build_function_call_expr (fn, arglist);
11020 }
11021
11022 if (! len || TREE_SIDE_EFFECTS (len))
11023 return 0;
11024
11025 /* If c_strlen returned something, but not a constant,
11026 transform __strcpy_chk into __memcpy_chk. */
11027 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11028 if (!fn)
11029 return 0;
11030
11031 len = size_binop (PLUS_EXPR, len, ssize_int (1));
11032 arglist = build_tree_list (NULL_TREE, size);
11033 arglist = tree_cons (NULL_TREE, len, arglist);
11034 arglist = tree_cons (NULL_TREE, src, arglist);
11035 arglist = tree_cons (NULL_TREE, dest, arglist);
11036 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
11037 build_function_call_expr (fn, arglist));
11038 }
11039 }
11040 else
11041 maxlen = len;
11042
11043 if (! tree_int_cst_lt (maxlen, size))
11044 return 0;
11045 }
11046
11047 arglist = build_tree_list (NULL_TREE, src);
11048 arglist = tree_cons (NULL_TREE, dest, arglist);
11049
11050 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
11051 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
11052 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
11053 if (!fn)
11054 return 0;
11055
11056 return build_function_call_expr (fn, arglist);
11057 }
11058
11059 /* Fold a call to the __strncpy_chk builtin.
11060 If MAXLEN is not NULL, it is maximum length passed as third argument. */
11061
11062 tree
11063 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
11064 {
11065 tree dest, src, size, len, fn;
11066
11067 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11068 INTEGER_TYPE, VOID_TYPE))
11069 return 0;
11070
11071 dest = TREE_VALUE (arglist);
11072 src = TREE_VALUE (TREE_CHAIN (arglist));
11073 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11074 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11075
11076 if (! host_integerp (size, 1))
11077 return 0;
11078
11079 if (! integer_all_onesp (size))
11080 {
11081 if (! host_integerp (len, 1))
11082 {
11083 /* If LEN is not constant, try MAXLEN too.
11084 For MAXLEN only allow optimizing into non-_ocs function
11085 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11086 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11087 return 0;
11088 }
11089 else
11090 maxlen = len;
11091
11092 if (tree_int_cst_lt (size, maxlen))
11093 return 0;
11094 }
11095
11096 arglist = build_tree_list (NULL_TREE, len);
11097 arglist = tree_cons (NULL_TREE, src, arglist);
11098 arglist = tree_cons (NULL_TREE, dest, arglist);
11099
11100 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
11101 fn = built_in_decls[BUILT_IN_STRNCPY];
11102 if (!fn)
11103 return 0;
11104
11105 return build_function_call_expr (fn, arglist);
11106 }
11107
11108 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
11109
11110 static tree
11111 fold_builtin_strcat_chk (tree fndecl, tree arglist)
11112 {
11113 tree dest, src, size, fn;
11114 const char *p;
11115
11116 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11117 VOID_TYPE))
11118 return 0;
11119
11120 dest = TREE_VALUE (arglist);
11121 src = TREE_VALUE (TREE_CHAIN (arglist));
11122 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11123
11124 p = c_getstr (src);
11125 /* If the SRC parameter is "", return DEST. */
11126 if (p && *p == '\0')
11127 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11128
11129 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
11130 return 0;
11131
11132 arglist = build_tree_list (NULL_TREE, src);
11133 arglist = tree_cons (NULL_TREE, dest, arglist);
11134
11135 /* If __builtin_strcat_chk is used, assume strcat is available. */
11136 fn = built_in_decls[BUILT_IN_STRCAT];
11137 if (!fn)
11138 return 0;
11139
11140 return build_function_call_expr (fn, arglist);
11141 }
11142
11143 /* Fold a call to the __strncat_chk builtin EXP. */
11144
11145 static tree
11146 fold_builtin_strncat_chk (tree fndecl, tree arglist)
11147 {
11148 tree dest, src, size, len, fn;
11149 const char *p;
11150
11151 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11152 INTEGER_TYPE, VOID_TYPE))
11153 return 0;
11154
11155 dest = TREE_VALUE (arglist);
11156 src = TREE_VALUE (TREE_CHAIN (arglist));
11157 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11158 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11159
11160 p = c_getstr (src);
11161 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
11162 if (p && *p == '\0')
11163 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
11164 else if (integer_zerop (len))
11165 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11166
11167 if (! host_integerp (size, 1))
11168 return 0;
11169
11170 if (! integer_all_onesp (size))
11171 {
11172 tree src_len = c_strlen (src, 1);
11173 if (src_len
11174 && host_integerp (src_len, 1)
11175 && host_integerp (len, 1)
11176 && ! tree_int_cst_lt (len, src_len))
11177 {
11178 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
11179 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
11180 if (!fn)
11181 return 0;
11182
11183 arglist = build_tree_list (NULL_TREE, size);
11184 arglist = tree_cons (NULL_TREE, src, arglist);
11185 arglist = tree_cons (NULL_TREE, dest, arglist);
11186 return build_function_call_expr (fn, arglist);
11187 }
11188 return 0;
11189 }
11190
11191 arglist = build_tree_list (NULL_TREE, len);
11192 arglist = tree_cons (NULL_TREE, src, arglist);
11193 arglist = tree_cons (NULL_TREE, dest, arglist);
11194
11195 /* If __builtin_strncat_chk is used, assume strncat is available. */
11196 fn = built_in_decls[BUILT_IN_STRNCAT];
11197 if (!fn)
11198 return 0;
11199
11200 return build_function_call_expr (fn, arglist);
11201 }
11202
11203 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
11204 a normal call should be emitted rather than expanding the function
11205 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
11206
11207 static tree
11208 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
11209 {
11210 tree dest, size, len, fn, fmt, flag;
11211 const char *fmt_str;
11212
11213 /* Verify the required arguments in the original call. */
11214 if (! arglist)
11215 return 0;
11216 dest = TREE_VALUE (arglist);
11217 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11218 return 0;
11219 arglist = TREE_CHAIN (arglist);
11220 if (! arglist)
11221 return 0;
11222 flag = TREE_VALUE (arglist);
11223 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
11224 return 0;
11225 arglist = TREE_CHAIN (arglist);
11226 if (! arglist)
11227 return 0;
11228 size = TREE_VALUE (arglist);
11229 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11230 return 0;
11231 arglist = TREE_CHAIN (arglist);
11232 if (! arglist)
11233 return 0;
11234 fmt = TREE_VALUE (arglist);
11235 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11236 return 0;
11237 arglist = TREE_CHAIN (arglist);
11238
11239 if (! host_integerp (size, 1))
11240 return 0;
11241
11242 len = NULL_TREE;
11243
11244 if (!init_target_chars())
11245 return 0;
11246
11247 /* Check whether the format is a literal string constant. */
11248 fmt_str = c_getstr (fmt);
11249 if (fmt_str != NULL)
11250 {
11251 /* If the format doesn't contain % args or %%, we know the size. */
11252 if (strchr (fmt_str, target_percent) == 0)
11253 {
11254 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11255 len = build_int_cstu (size_type_node, strlen (fmt_str));
11256 }
11257 /* If the format is "%s" and first ... argument is a string literal,
11258 we know the size too. */
11259 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11260 {
11261 tree arg;
11262
11263 if (arglist && !TREE_CHAIN (arglist))
11264 {
11265 arg = TREE_VALUE (arglist);
11266 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11267 {
11268 len = c_strlen (arg, 1);
11269 if (! len || ! host_integerp (len, 1))
11270 len = NULL_TREE;
11271 }
11272 }
11273 }
11274 }
11275
11276 if (! integer_all_onesp (size))
11277 {
11278 if (! len || ! tree_int_cst_lt (len, size))
11279 return 0;
11280 }
11281
11282 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11283 or if format doesn't contain % chars or is "%s". */
11284 if (! integer_zerop (flag))
11285 {
11286 if (fmt_str == NULL)
11287 return 0;
11288 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11289 return 0;
11290 }
11291
11292 arglist = tree_cons (NULL_TREE, fmt, arglist);
11293 arglist = tree_cons (NULL_TREE, dest, arglist);
11294
11295 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11296 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11297 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11298 if (!fn)
11299 return 0;
11300
11301 return build_function_call_expr (fn, arglist);
11302 }
11303
11304 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11305 a normal call should be emitted rather than expanding the function
11306 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11307 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11308 passed as second argument. */
11309
11310 tree
11311 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11312 enum built_in_function fcode)
11313 {
11314 tree dest, size, len, fn, fmt, flag;
11315 const char *fmt_str;
11316
11317 /* Verify the required arguments in the original call. */
11318 if (! arglist)
11319 return 0;
11320 dest = TREE_VALUE (arglist);
11321 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11322 return 0;
11323 arglist = TREE_CHAIN (arglist);
11324 if (! arglist)
11325 return 0;
11326 len = TREE_VALUE (arglist);
11327 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11328 return 0;
11329 arglist = TREE_CHAIN (arglist);
11330 if (! arglist)
11331 return 0;
11332 flag = TREE_VALUE (arglist);
11333 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11334 return 0;
11335 arglist = TREE_CHAIN (arglist);
11336 if (! arglist)
11337 return 0;
11338 size = TREE_VALUE (arglist);
11339 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11340 return 0;
11341 arglist = TREE_CHAIN (arglist);
11342 if (! arglist)
11343 return 0;
11344 fmt = TREE_VALUE (arglist);
11345 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11346 return 0;
11347 arglist = TREE_CHAIN (arglist);
11348
11349 if (! host_integerp (size, 1))
11350 return 0;
11351
11352 if (! integer_all_onesp (size))
11353 {
11354 if (! host_integerp (len, 1))
11355 {
11356 /* If LEN is not constant, try MAXLEN too.
11357 For MAXLEN only allow optimizing into non-_ocs function
11358 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11359 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11360 return 0;
11361 }
11362 else
11363 maxlen = len;
11364
11365 if (tree_int_cst_lt (size, maxlen))
11366 return 0;
11367 }
11368
11369 if (!init_target_chars())
11370 return 0;
11371
11372 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11373 or if format doesn't contain % chars or is "%s". */
11374 if (! integer_zerop (flag))
11375 {
11376 fmt_str = c_getstr (fmt);
11377 if (fmt_str == NULL)
11378 return 0;
11379 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11380 return 0;
11381 }
11382
11383 arglist = tree_cons (NULL_TREE, fmt, arglist);
11384 arglist = tree_cons (NULL_TREE, len, arglist);
11385 arglist = tree_cons (NULL_TREE, dest, arglist);
11386
11387 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11388 available. */
11389 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11390 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11391 if (!fn)
11392 return 0;
11393
11394 return build_function_call_expr (fn, arglist);
11395 }
11396
11397 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11398
11399 Return 0 if no simplification was possible, otherwise return the
11400 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11401 code of the function to be simplified. */
11402
11403 static tree
11404 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11405 enum built_in_function fcode)
11406 {
11407 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11408 const char *fmt_str = NULL;
11409
11410 /* If the return value is used, don't do the transformation. */
11411 if (! ignore)
11412 return 0;
11413
11414 /* Verify the required arguments in the original call. */
11415 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11416 {
11417 tree flag;
11418
11419 if (! arglist)
11420 return 0;
11421 flag = TREE_VALUE (arglist);
11422 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11423 || TREE_SIDE_EFFECTS (flag))
11424 return 0;
11425 arglist = TREE_CHAIN (arglist);
11426 }
11427
11428 if (! arglist)
11429 return 0;
11430 fmt = TREE_VALUE (arglist);
11431 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11432 return 0;
11433 arglist = TREE_CHAIN (arglist);
11434
11435 /* Check whether the format is a literal string constant. */
11436 fmt_str = c_getstr (fmt);
11437 if (fmt_str == NULL)
11438 return NULL_TREE;
11439
11440 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11441 {
11442 /* If we're using an unlocked function, assume the other
11443 unlocked functions exist explicitly. */
11444 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11445 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11446 }
11447 else
11448 {
11449 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11450 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11451 }
11452
11453 if (!init_target_chars())
11454 return 0;
11455
11456 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11457 {
11458 const char *str;
11459
11460 if (strcmp (fmt_str, target_percent_s) == 0)
11461 {
11462 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11463 return 0;
11464
11465 if (! arglist
11466 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11467 || TREE_CHAIN (arglist))
11468 return 0;
11469
11470 str = c_getstr (TREE_VALUE (arglist));
11471 if (str == NULL)
11472 return 0;
11473 }
11474 else
11475 {
11476 /* The format specifier doesn't contain any '%' characters. */
11477 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11478 && arglist)
11479 return 0;
11480 str = fmt_str;
11481 }
11482
11483 /* If the string was "", printf does nothing. */
11484 if (str[0] == '\0')
11485 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11486
11487 /* If the string has length of 1, call putchar. */
11488 if (str[1] == '\0')
11489 {
11490 /* Given printf("c"), (where c is any one character,)
11491 convert "c"[0] to an int and pass that to the replacement
11492 function. */
11493 arg = build_int_cst (NULL_TREE, str[0]);
11494 arglist = build_tree_list (NULL_TREE, arg);
11495 fn = fn_putchar;
11496 }
11497 else
11498 {
11499 /* If the string was "string\n", call puts("string"). */
11500 size_t len = strlen (str);
11501 if ((unsigned char)str[len - 1] == target_newline)
11502 {
11503 /* Create a NUL-terminated string that's one char shorter
11504 than the original, stripping off the trailing '\n'. */
11505 char *newstr = alloca (len);
11506 memcpy (newstr, str, len - 1);
11507 newstr[len - 1] = 0;
11508
11509 arg = build_string_literal (len, newstr);
11510 arglist = build_tree_list (NULL_TREE, arg);
11511 fn = fn_puts;
11512 }
11513 else
11514 /* We'd like to arrange to call fputs(string,stdout) here,
11515 but we need stdout and don't have a way to get it yet. */
11516 return 0;
11517 }
11518 }
11519
11520 /* The other optimizations can be done only on the non-va_list variants. */
11521 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11522 return 0;
11523
11524 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11525 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11526 {
11527 if (! arglist
11528 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11529 || TREE_CHAIN (arglist))
11530 return 0;
11531 fn = fn_puts;
11532 }
11533
11534 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11535 else if (strcmp (fmt_str, target_percent_c) == 0)
11536 {
11537 if (! arglist
11538 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11539 || TREE_CHAIN (arglist))
11540 return 0;
11541 fn = fn_putchar;
11542 }
11543
11544 if (!fn)
11545 return 0;
11546
11547 call = build_function_call_expr (fn, arglist);
11548 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11549 }
11550
11551 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11552
11553 Return 0 if no simplification was possible, otherwise return the
11554 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11555 code of the function to be simplified. */
11556
11557 static tree
11558 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11559 enum built_in_function fcode)
11560 {
11561 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11562 const char *fmt_str = NULL;
11563
11564 /* If the return value is used, don't do the transformation. */
11565 if (! ignore)
11566 return 0;
11567
11568 /* Verify the required arguments in the original call. */
11569 if (! arglist)
11570 return 0;
11571 fp = TREE_VALUE (arglist);
11572 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11573 return 0;
11574 arglist = TREE_CHAIN (arglist);
11575
11576 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11577 {
11578 tree flag;
11579
11580 if (! arglist)
11581 return 0;
11582 flag = TREE_VALUE (arglist);
11583 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11584 || TREE_SIDE_EFFECTS (flag))
11585 return 0;
11586 arglist = TREE_CHAIN (arglist);
11587 }
11588
11589 if (! arglist)
11590 return 0;
11591 fmt = TREE_VALUE (arglist);
11592 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11593 return 0;
11594 arglist = TREE_CHAIN (arglist);
11595
11596 /* Check whether the format is a literal string constant. */
11597 fmt_str = c_getstr (fmt);
11598 if (fmt_str == NULL)
11599 return NULL_TREE;
11600
11601 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11602 {
11603 /* If we're using an unlocked function, assume the other
11604 unlocked functions exist explicitly. */
11605 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11606 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11607 }
11608 else
11609 {
11610 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11611 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11612 }
11613
11614 if (!init_target_chars())
11615 return 0;
11616
11617 /* If the format doesn't contain % args or %%, use strcpy. */
11618 if (strchr (fmt_str, target_percent) == NULL)
11619 {
11620 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11621 && arglist)
11622 return 0;
11623
11624 /* If the format specifier was "", fprintf does nothing. */
11625 if (fmt_str[0] == '\0')
11626 {
11627 /* If FP has side-effects, just wait until gimplification is
11628 done. */
11629 if (TREE_SIDE_EFFECTS (fp))
11630 return 0;
11631
11632 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11633 }
11634
11635 /* When "string" doesn't contain %, replace all cases of
11636 fprintf (fp, string) with fputs (string, fp). The fputs
11637 builtin will take care of special cases like length == 1. */
11638 arglist = build_tree_list (NULL_TREE, fp);
11639 arglist = tree_cons (NULL_TREE, fmt, arglist);
11640 fn = fn_fputs;
11641 }
11642
11643 /* The other optimizations can be done only on the non-va_list variants. */
11644 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11645 return 0;
11646
11647 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11648 else if (strcmp (fmt_str, target_percent_s) == 0)
11649 {
11650 if (! arglist
11651 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11652 || TREE_CHAIN (arglist))
11653 return 0;
11654 arg = TREE_VALUE (arglist);
11655 arglist = build_tree_list (NULL_TREE, fp);
11656 arglist = tree_cons (NULL_TREE, arg, arglist);
11657 fn = fn_fputs;
11658 }
11659
11660 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11661 else if (strcmp (fmt_str, target_percent_c) == 0)
11662 {
11663 if (! arglist
11664 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11665 || TREE_CHAIN (arglist))
11666 return 0;
11667 arg = TREE_VALUE (arglist);
11668 arglist = build_tree_list (NULL_TREE, fp);
11669 arglist = tree_cons (NULL_TREE, arg, arglist);
11670 fn = fn_fputc;
11671 }
11672
11673 if (!fn)
11674 return 0;
11675
11676 call = build_function_call_expr (fn, arglist);
11677 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11678 }
11679
11680 /* Initialize format string characters in the target charset. */
11681
11682 static bool
11683 init_target_chars (void)
11684 {
11685 static bool init;
11686 if (!init)
11687 {
11688 target_newline = lang_hooks.to_target_charset ('\n');
11689 target_percent = lang_hooks.to_target_charset ('%');
11690 target_c = lang_hooks.to_target_charset ('c');
11691 target_s = lang_hooks.to_target_charset ('s');
11692 if (target_newline == 0 || target_percent == 0 || target_c == 0
11693 || target_s == 0)
11694 return false;
11695
11696 target_percent_c[0] = target_percent;
11697 target_percent_c[1] = target_c;
11698 target_percent_c[2] = '\0';
11699
11700 target_percent_s[0] = target_percent;
11701 target_percent_s[1] = target_s;
11702 target_percent_s[2] = '\0';
11703
11704 target_percent_s_newline[0] = target_percent;
11705 target_percent_s_newline[1] = target_s;
11706 target_percent_s_newline[2] = target_newline;
11707 target_percent_s_newline[3] = '\0';
11708
11709 init = true;
11710 }
11711 return true;
11712 }
11713
11714 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11715 and no overflow/underflow occurred. INEXACT is true if M was not
11716 exactly calculated. TYPE is the tree type for the result. This
11717 function assumes that you cleared the MPFR flags and then
11718 calculated M to see if anything subsequently set a flag prior to
11719 entering this function. Return NULL_TREE if any checks fail. */
11720
11721 static tree
11722 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11723 {
11724 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11725 overflow/underflow occurred. If -frounding-math, proceed iff the
11726 result of calling FUNC was exact. */
11727 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11728 && (!flag_rounding_math || !inexact))
11729 {
11730 REAL_VALUE_TYPE rr;
11731
11732 real_from_mpfr (&rr, m);
11733 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11734 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11735 but the mpft_t is not, then we underflowed in the
11736 conversion. */
11737 if (!real_isnan (&rr) && !real_isinf (&rr)
11738 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11739 {
11740 REAL_VALUE_TYPE rmode;
11741
11742 real_convert (&rmode, TYPE_MODE (type), &rr);
11743 /* Proceed iff the specified mode can hold the value. */
11744 if (real_identical (&rmode, &rr))
11745 return build_real (type, rmode);
11746 }
11747 }
11748 return NULL_TREE;
11749 }
11750
11751 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11752 FUNC on it and return the resulting value as a tree with type TYPE.
11753 If MIN and/or MAX are not NULL, then the supplied ARG must be
11754 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11755 acceptable values, otherwise they are not. The mpfr precision is
11756 set to the precision of TYPE. We assume that function FUNC returns
11757 zero if the result could be calculated exactly within the requested
11758 precision. */
11759
11760 static tree
11761 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11762 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11763 bool inclusive)
11764 {
11765 tree result = NULL_TREE;
11766
11767 STRIP_NOPS (arg);
11768
11769 /* To proceed, MPFR must exactly represent the target floating point
11770 format, which only happens when the target base equals two. */
11771 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11772 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11773 {
11774 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11775
11776 if (!real_isnan (ra) && !real_isinf (ra)
11777 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11778 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11779 {
11780 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11781 int inexact;
11782 mpfr_t m;
11783
11784 mpfr_init2 (m, prec);
11785 mpfr_from_real (m, ra);
11786 mpfr_clear_flags();
11787 inexact = func (m, m, GMP_RNDN);
11788 result = do_mpfr_ckconv (m, type, inexact);
11789 mpfr_clear (m);
11790 }
11791 }
11792
11793 return result;
11794 }
11795
11796 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11797 FUNC on it and return the resulting value as a tree with type TYPE.
11798 The mpfr precision is set to the precision of TYPE. We assume that
11799 function FUNC returns zero if the result could be calculated
11800 exactly within the requested precision. */
11801
11802 static tree
11803 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11804 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11805 {
11806 tree result = NULL_TREE;
11807
11808 STRIP_NOPS (arg1);
11809 STRIP_NOPS (arg2);
11810
11811 /* To proceed, MPFR must exactly represent the target floating point
11812 format, which only happens when the target base equals two. */
11813 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11814 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11815 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11816 {
11817 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11818 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11819
11820 if (!real_isnan (ra1) && !real_isinf (ra1)
11821 && !real_isnan (ra2) && !real_isinf (ra2))
11822 {
11823 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11824 int inexact;
11825 mpfr_t m1, m2;
11826
11827 mpfr_inits2 (prec, m1, m2, NULL);
11828 mpfr_from_real (m1, ra1);
11829 mpfr_from_real (m2, ra2);
11830 mpfr_clear_flags();
11831 inexact = func (m1, m1, m2, GMP_RNDN);
11832 result = do_mpfr_ckconv (m1, type, inexact);
11833 mpfr_clears (m1, m2, NULL);
11834 }
11835 }
11836
11837 return result;
11838 }
11839
11840 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11841 FUNC on it and return the resulting value as a tree with type TYPE.
11842 The mpfr precision is set to the precision of TYPE. We assume that
11843 function FUNC returns zero if the result could be calculated
11844 exactly within the requested precision. */
11845
11846 static tree
11847 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11848 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11849 {
11850 tree result = NULL_TREE;
11851
11852 STRIP_NOPS (arg1);
11853 STRIP_NOPS (arg2);
11854 STRIP_NOPS (arg3);
11855
11856 /* To proceed, MPFR must exactly represent the target floating point
11857 format, which only happens when the target base equals two. */
11858 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11859 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11860 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11861 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11862 {
11863 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11864 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11865 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11866
11867 if (!real_isnan (ra1) && !real_isinf (ra1)
11868 && !real_isnan (ra2) && !real_isinf (ra2)
11869 && !real_isnan (ra3) && !real_isinf (ra3))
11870 {
11871 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11872 int inexact;
11873 mpfr_t m1, m2, m3;
11874
11875 mpfr_inits2 (prec, m1, m2, m3, NULL);
11876 mpfr_from_real (m1, ra1);
11877 mpfr_from_real (m2, ra2);
11878 mpfr_from_real (m3, ra3);
11879 mpfr_clear_flags();
11880 inexact = func (m1, m1, m2, m3, GMP_RNDN);
11881 result = do_mpfr_ckconv (m1, type, inexact);
11882 mpfr_clears (m1, m2, m3, NULL);
11883 }
11884 }
11885
11886 return result;
11887 }
11888
11889 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11890 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11891 If ARG_SINP and ARG_COSP are NULL then the result is returned
11892 as a complex value.
11893 The type is taken from the type of ARG and is used for setting the
11894 precision of the calculation and results. */
11895
11896 static tree
11897 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11898 {
11899 tree const type = TREE_TYPE (arg);
11900 tree result = NULL_TREE;
11901
11902 STRIP_NOPS (arg);
11903
11904 /* To proceed, MPFR must exactly represent the target floating point
11905 format, which only happens when the target base equals two. */
11906 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11907 && TREE_CODE (arg) == REAL_CST
11908 && !TREE_OVERFLOW (arg))
11909 {
11910 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11911
11912 if (!real_isnan (ra) && !real_isinf (ra))
11913 {
11914 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11915 tree result_s, result_c;
11916 int inexact;
11917 mpfr_t m, ms, mc;
11918
11919 mpfr_inits2 (prec, m, ms, mc, NULL);
11920 mpfr_from_real (m, ra);
11921 mpfr_clear_flags();
11922 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
11923 result_s = do_mpfr_ckconv (ms, type, inexact);
11924 result_c = do_mpfr_ckconv (mc, type, inexact);
11925 mpfr_clears (m, ms, mc, NULL);
11926 if (result_s && result_c)
11927 {
11928 /* If we are to return in a complex value do so. */
11929 if (!arg_sinp && !arg_cosp)
11930 return build_complex (build_complex_type (type),
11931 result_c, result_s);
11932
11933 /* Dereference the sin/cos pointer arguments. */
11934 arg_sinp = build_fold_indirect_ref (arg_sinp);
11935 arg_cosp = build_fold_indirect_ref (arg_cosp);
11936 /* Proceed if valid pointer type were passed in. */
11937 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11938 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11939 {
11940 /* Set the values. */
11941 result_s = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_sinp,
11942 result_s);
11943 TREE_SIDE_EFFECTS (result_s) = 1;
11944 result_c = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_cosp,
11945 result_c);
11946 TREE_SIDE_EFFECTS (result_c) = 1;
11947 /* Combine the assignments into a compound expr. */
11948 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11949 result_s, result_c));
11950 }
11951 }
11952 }
11953 }
11954 return result;
11955 }