builtins.c (expand_builtin_return_addr): Remove tem parameter.
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "real.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tree-gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "toplev.h"
44 #include "predict.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
50
51 #define CALLED_AS_BUILT_IN(NODE) \
52 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
53
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 #endif
57
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
61
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
64 {
65 #include "builtins.def"
66 };
67 #undef DEF_BUILTIN
68
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
76
77 static int get_pointer_alignment (tree, unsigned int);
78 static const char *c_getstr (tree);
79 static rtx c_readstr (const char *, enum machine_mode);
80 static int target_char_cast (tree, char *);
81 static rtx get_memory_rtx (tree);
82 static tree build_string_literal (int, const char *);
83 static int apply_args_size (void);
84 static int apply_result_size (void);
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector (int, rtx);
87 #endif
88 static rtx expand_builtin_setjmp (tree, rtx);
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (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, tree);
118 static rtx expand_builtin_strcpy (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_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_bzero (tree);
126 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_alloca (tree, rtx);
132 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133 static rtx expand_builtin_frame_address (tree, tree);
134 static rtx expand_builtin_fputs (tree, rtx, bool);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list (tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (tree);
143 static tree fold_builtin_inf (tree, int);
144 static tree fold_builtin_nan (tree, tree, int);
145 static int validate_arglist (tree, ...);
146 static bool integer_valued_real_p (tree);
147 static tree fold_trunc_transparent_mathfn (tree);
148 static bool readonly_data_expr (tree);
149 static rtx expand_builtin_fabs (tree, rtx, rtx);
150 static rtx expand_builtin_signbit (tree, rtx);
151 static tree fold_builtin_cabs (tree, tree);
152 static tree fold_builtin_sqrt (tree, tree);
153 static tree fold_builtin_cbrt (tree, tree);
154 static tree fold_builtin_pow (tree, tree, tree);
155 static tree fold_builtin_powi (tree, tree, tree);
156 static tree fold_builtin_sin (tree);
157 static tree fold_builtin_cos (tree, tree, tree);
158 static tree fold_builtin_tan (tree);
159 static tree fold_builtin_atan (tree, tree);
160 static tree fold_builtin_trunc (tree);
161 static tree fold_builtin_floor (tree);
162 static tree fold_builtin_ceil (tree);
163 static tree fold_builtin_round (tree);
164 static tree fold_builtin_bitop (tree);
165 static tree fold_builtin_memcpy (tree);
166 static tree fold_builtin_mempcpy (tree, tree, int);
167 static tree fold_builtin_memmove (tree, tree);
168 static tree fold_builtin_strchr (tree, tree);
169 static tree fold_builtin_memcmp (tree);
170 static tree fold_builtin_strcmp (tree);
171 static tree fold_builtin_strncmp (tree);
172 static tree fold_builtin_signbit (tree);
173 static tree fold_builtin_copysign (tree, tree, tree);
174 static tree fold_builtin_isascii (tree);
175 static tree fold_builtin_toascii (tree);
176 static tree fold_builtin_isdigit (tree);
177 static tree fold_builtin_fabs (tree, tree);
178 static tree fold_builtin_abs (tree, tree);
179 static tree fold_builtin_unordered_cmp (tree, enum tree_code, enum tree_code);
180 static tree fold_builtin_1 (tree, bool);
181
182 static tree fold_builtin_strpbrk (tree, tree);
183 static tree fold_builtin_strstr (tree, tree);
184 static tree fold_builtin_strrchr (tree, tree);
185 static tree fold_builtin_strcat (tree);
186 static tree fold_builtin_strncat (tree);
187 static tree fold_builtin_strspn (tree);
188 static tree fold_builtin_strcspn (tree);
189 static tree fold_builtin_sprintf (tree, int);
190
191
192 /* Return the alignment in bits of EXP, a pointer valued expression.
193 But don't return more than MAX_ALIGN no matter what.
194 The alignment returned is, by default, the alignment of the thing that
195 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
196
197 Otherwise, look at the expression to see if we can do better, i.e., if the
198 expression is actually pointing at an object whose alignment is tighter. */
199
200 static int
201 get_pointer_alignment (tree exp, unsigned int max_align)
202 {
203 unsigned int align, inner;
204
205 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
206 return 0;
207
208 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
209 align = MIN (align, max_align);
210
211 while (1)
212 {
213 switch (TREE_CODE (exp))
214 {
215 case NOP_EXPR:
216 case CONVERT_EXPR:
217 case NON_LVALUE_EXPR:
218 exp = TREE_OPERAND (exp, 0);
219 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
220 return align;
221
222 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
223 align = MIN (inner, max_align);
224 break;
225
226 case PLUS_EXPR:
227 /* If sum of pointer + int, restrict our maximum alignment to that
228 imposed by the integer. If not, we can't do any better than
229 ALIGN. */
230 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
231 return align;
232
233 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
234 & (max_align / BITS_PER_UNIT - 1))
235 != 0)
236 max_align >>= 1;
237
238 exp = TREE_OPERAND (exp, 0);
239 break;
240
241 case ADDR_EXPR:
242 /* See what we are pointing at and look at its alignment. */
243 exp = TREE_OPERAND (exp, 0);
244 if (TREE_CODE (exp) == FUNCTION_DECL)
245 align = FUNCTION_BOUNDARY;
246 else if (DECL_P (exp))
247 align = DECL_ALIGN (exp);
248 #ifdef CONSTANT_ALIGNMENT
249 else if (CONSTANT_CLASS_P (exp))
250 align = CONSTANT_ALIGNMENT (exp, align);
251 #endif
252 return MIN (align, max_align);
253
254 default:
255 return align;
256 }
257 }
258 }
259
260 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
261 way, because it could contain a zero byte in the middle.
262 TREE_STRING_LENGTH is the size of the character array, not the string.
263
264 ONLY_VALUE should be nonzero if the result is not going to be emitted
265 into the instruction stream and zero if it is going to be expanded.
266 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
267 is returned, otherwise NULL, since
268 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
269 evaluate the side-effects.
270
271 The value returned is of type `ssizetype'.
272
273 Unfortunately, string_constant can't access the values of const char
274 arrays with initializers, so neither can we do so here. */
275
276 tree
277 c_strlen (tree src, int only_value)
278 {
279 tree offset_node;
280 HOST_WIDE_INT offset;
281 int max;
282 const char *ptr;
283
284 STRIP_NOPS (src);
285 if (TREE_CODE (src) == COND_EXPR
286 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
287 {
288 tree len1, len2;
289
290 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
291 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
292 if (tree_int_cst_equal (len1, len2))
293 return len1;
294 }
295
296 if (TREE_CODE (src) == COMPOUND_EXPR
297 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
298 return c_strlen (TREE_OPERAND (src, 1), only_value);
299
300 src = string_constant (src, &offset_node);
301 if (src == 0)
302 return 0;
303
304 max = TREE_STRING_LENGTH (src) - 1;
305 ptr = TREE_STRING_POINTER (src);
306
307 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
308 {
309 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
310 compute the offset to the following null if we don't know where to
311 start searching for it. */
312 int i;
313
314 for (i = 0; i < max; i++)
315 if (ptr[i] == 0)
316 return 0;
317
318 /* We don't know the starting offset, but we do know that the string
319 has no internal zero bytes. We can assume that the offset falls
320 within the bounds of the string; otherwise, the programmer deserves
321 what he gets. Subtract the offset from the length of the string,
322 and return that. This would perhaps not be valid if we were dealing
323 with named arrays in addition to literal string constants. */
324
325 return size_diffop (size_int (max), offset_node);
326 }
327
328 /* We have a known offset into the string. Start searching there for
329 a null character if we can represent it as a single HOST_WIDE_INT. */
330 if (offset_node == 0)
331 offset = 0;
332 else if (! host_integerp (offset_node, 0))
333 offset = -1;
334 else
335 offset = tree_low_cst (offset_node, 0);
336
337 /* If the offset is known to be out of bounds, warn, and call strlen at
338 runtime. */
339 if (offset < 0 || offset > max)
340 {
341 warning ("offset outside bounds of constant string");
342 return 0;
343 }
344
345 /* Use strlen to search for the first zero byte. Since any strings
346 constructed with build_string will have nulls appended, we win even
347 if we get handed something like (char[4])"abcd".
348
349 Since OFFSET is our starting index into the string, no further
350 calculation is needed. */
351 return ssize_int (strlen (ptr + offset));
352 }
353
354 /* Return a char pointer for a C string if it is a string constant
355 or sum of string constant and integer constant. */
356
357 static const char *
358 c_getstr (tree src)
359 {
360 tree offset_node;
361
362 src = string_constant (src, &offset_node);
363 if (src == 0)
364 return 0;
365
366 if (offset_node == 0)
367 return TREE_STRING_POINTER (src);
368 else if (!host_integerp (offset_node, 1)
369 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
370 return 0;
371
372 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
373 }
374
375 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
376 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
377
378 static rtx
379 c_readstr (const char *str, enum machine_mode mode)
380 {
381 HOST_WIDE_INT c[2];
382 HOST_WIDE_INT ch;
383 unsigned int i, j;
384
385 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
386
387 c[0] = 0;
388 c[1] = 0;
389 ch = 1;
390 for (i = 0; i < GET_MODE_SIZE (mode); i++)
391 {
392 j = i;
393 if (WORDS_BIG_ENDIAN)
394 j = GET_MODE_SIZE (mode) - i - 1;
395 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
396 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
397 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
398 j *= BITS_PER_UNIT;
399 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
400
401 if (ch)
402 ch = (unsigned char) str[i];
403 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
404 }
405 return immed_double_const (c[0], c[1], mode);
406 }
407
408 /* Cast a target constant CST to target CHAR and if that value fits into
409 host char type, return zero and put that value into variable pointed by
410 P. */
411
412 static int
413 target_char_cast (tree cst, char *p)
414 {
415 unsigned HOST_WIDE_INT val, hostval;
416
417 if (!host_integerp (cst, 1)
418 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
419 return 1;
420
421 val = tree_low_cst (cst, 1);
422 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
423 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
424
425 hostval = val;
426 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
427 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
428
429 if (val != hostval)
430 return 1;
431
432 *p = hostval;
433 return 0;
434 }
435
436 /* Similar to save_expr, but assumes that arbitrary code is not executed
437 in between the multiple evaluations. In particular, we assume that a
438 non-addressable local variable will not be modified. */
439
440 static tree
441 builtin_save_expr (tree exp)
442 {
443 if (TREE_ADDRESSABLE (exp) == 0
444 && (TREE_CODE (exp) == PARM_DECL
445 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
446 return exp;
447
448 return save_expr (exp);
449 }
450
451 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
452 times to get the address of either a higher stack frame, or a return
453 address located within it (depending on FNDECL_CODE). */
454
455 static rtx
456 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
457 {
458 int i;
459
460 #ifdef INITIAL_FRAME_ADDRESS_RTX
461 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
462 #else
463 rtx tem = hard_frame_pointer_rtx;
464 #endif
465
466 /* Some machines need special handling before we can access
467 arbitrary frames. For example, on the sparc, we must first flush
468 all register windows to the stack. */
469 #ifdef SETUP_FRAME_ADDRESSES
470 if (count > 0)
471 SETUP_FRAME_ADDRESSES ();
472 #endif
473
474 /* On the sparc, the return address is not in the frame, it is in a
475 register. There is no way to access it off of the current frame
476 pointer, but it can be accessed off the previous frame pointer by
477 reading the value from the register window save area. */
478 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
479 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
480 count--;
481 #endif
482
483 /* Scan back COUNT frames to the specified frame. */
484 for (i = 0; i < count; i++)
485 {
486 /* Assume the dynamic chain pointer is in the word that the
487 frame address points to, unless otherwise specified. */
488 #ifdef DYNAMIC_CHAIN_ADDRESS
489 tem = DYNAMIC_CHAIN_ADDRESS (tem);
490 #endif
491 tem = memory_address (Pmode, tem);
492 tem = gen_rtx_MEM (Pmode, tem);
493 set_mem_alias_set (tem, get_frame_alias_set ());
494 tem = copy_to_reg (tem);
495 }
496
497 /* For __builtin_frame_address, return what we've got. */
498 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
499 return tem;
500
501 /* For __builtin_return_address, Get the return address from that
502 frame. */
503 #ifdef RETURN_ADDR_RTX
504 tem = RETURN_ADDR_RTX (count, tem);
505 #else
506 tem = memory_address (Pmode,
507 plus_constant (tem, GET_MODE_SIZE (Pmode)));
508 tem = gen_rtx_MEM (Pmode, tem);
509 set_mem_alias_set (tem, get_frame_alias_set ());
510 #endif
511 return tem;
512 }
513
514 /* Alias set used for setjmp buffer. */
515 static HOST_WIDE_INT setjmp_alias_set = -1;
516
517 /* Construct the leading half of a __builtin_setjmp call. Control will
518 return to RECEIVER_LABEL. This is used directly by sjlj exception
519 handling code. */
520
521 void
522 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
523 {
524 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
525 rtx stack_save;
526 rtx mem;
527
528 if (setjmp_alias_set == -1)
529 setjmp_alias_set = new_alias_set ();
530
531 buf_addr = convert_memory_address (Pmode, buf_addr);
532
533 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
534
535 /* We store the frame pointer and the address of receiver_label in
536 the buffer and use the rest of it for the stack save area, which
537 is machine-dependent. */
538
539 mem = gen_rtx_MEM (Pmode, buf_addr);
540 set_mem_alias_set (mem, setjmp_alias_set);
541 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
542
543 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
544 set_mem_alias_set (mem, setjmp_alias_set);
545
546 emit_move_insn (validize_mem (mem),
547 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
548
549 stack_save = gen_rtx_MEM (sa_mode,
550 plus_constant (buf_addr,
551 2 * GET_MODE_SIZE (Pmode)));
552 set_mem_alias_set (stack_save, setjmp_alias_set);
553 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
554
555 /* If there is further processing to do, do it. */
556 #ifdef HAVE_builtin_setjmp_setup
557 if (HAVE_builtin_setjmp_setup)
558 emit_insn (gen_builtin_setjmp_setup (buf_addr));
559 #endif
560
561 /* Tell optimize_save_area_alloca that extra work is going to
562 need to go on during alloca. */
563 current_function_calls_setjmp = 1;
564
565 /* Set this so all the registers get saved in our frame; we need to be
566 able to copy the saved values for any registers from frames we unwind. */
567 current_function_has_nonlocal_label = 1;
568 }
569
570 /* Construct the trailing part of a __builtin_setjmp call.
571 This is used directly by sjlj exception handling code. */
572
573 void
574 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
575 {
576 /* Clobber the FP when we get here, so we have to make sure it's
577 marked as used by this function. */
578 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
579
580 /* Mark the static chain as clobbered here so life information
581 doesn't get messed up for it. */
582 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
583
584 /* Now put in the code to restore the frame pointer, and argument
585 pointer, if needed. */
586 #ifdef HAVE_nonlocal_goto
587 if (! HAVE_nonlocal_goto)
588 #endif
589 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
590
591 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
592 if (fixed_regs[ARG_POINTER_REGNUM])
593 {
594 #ifdef ELIMINABLE_REGS
595 size_t i;
596 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
597
598 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
599 if (elim_regs[i].from == ARG_POINTER_REGNUM
600 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
601 break;
602
603 if (i == ARRAY_SIZE (elim_regs))
604 #endif
605 {
606 /* Now restore our arg pointer from the address at which it
607 was saved in our stack frame. */
608 emit_move_insn (virtual_incoming_args_rtx,
609 copy_to_reg (get_arg_pointer_save_area (cfun)));
610 }
611 }
612 #endif
613
614 #ifdef HAVE_builtin_setjmp_receiver
615 if (HAVE_builtin_setjmp_receiver)
616 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
617 else
618 #endif
619 #ifdef HAVE_nonlocal_goto_receiver
620 if (HAVE_nonlocal_goto_receiver)
621 emit_insn (gen_nonlocal_goto_receiver ());
622 else
623 #endif
624 { /* Nothing */ }
625
626 /* @@@ This is a kludge. Not all machine descriptions define a blockage
627 insn, but we must not allow the code we just generated to be reordered
628 by scheduling. Specifically, the update of the frame pointer must
629 happen immediately, not later. So emit an ASM_INPUT to act as blockage
630 insn. */
631 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
632 }
633
634 /* __builtin_setjmp is passed a pointer to an array of five words (not
635 all will be used on all machines). It operates similarly to the C
636 library function of the same name, but is more efficient. Much of
637 the code below (and for longjmp) is copied from the handling of
638 non-local gotos.
639
640 NOTE: This is intended for use by GNAT and the exception handling
641 scheme in the compiler and will only work in the method used by
642 them. */
643
644 static rtx
645 expand_builtin_setjmp (tree arglist, rtx target)
646 {
647 rtx buf_addr, next_lab, cont_lab;
648
649 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
650 return NULL_RTX;
651
652 if (target == 0 || !REG_P (target)
653 || REGNO (target) < FIRST_PSEUDO_REGISTER)
654 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
655
656 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
657
658 next_lab = gen_label_rtx ();
659 cont_lab = gen_label_rtx ();
660
661 expand_builtin_setjmp_setup (buf_addr, next_lab);
662
663 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
664 ensure that pending stack adjustments are flushed. */
665 emit_move_insn (target, const0_rtx);
666 emit_jump (cont_lab);
667
668 emit_label (next_lab);
669
670 expand_builtin_setjmp_receiver (next_lab);
671
672 /* Set TARGET to one. */
673 emit_move_insn (target, const1_rtx);
674 emit_label (cont_lab);
675
676 /* Tell flow about the strange goings on. Putting `next_lab' on
677 `nonlocal_goto_handler_labels' to indicates that function
678 calls may traverse the arc back to this label. */
679
680 current_function_has_nonlocal_label = 1;
681 nonlocal_goto_handler_labels
682 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
683
684 return target;
685 }
686
687 /* __builtin_longjmp is passed a pointer to an array of five words (not
688 all will be used on all machines). It operates similarly to the C
689 library function of the same name, but is more efficient. Much of
690 the code below is copied from the handling of non-local gotos.
691
692 NOTE: This is intended for use by GNAT and the exception handling
693 scheme in the compiler and will only work in the method used by
694 them. */
695
696 static void
697 expand_builtin_longjmp (rtx buf_addr, rtx value)
698 {
699 rtx fp, lab, stack, insn, last;
700 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
701
702 if (setjmp_alias_set == -1)
703 setjmp_alias_set = new_alias_set ();
704
705 buf_addr = convert_memory_address (Pmode, buf_addr);
706
707 buf_addr = force_reg (Pmode, buf_addr);
708
709 /* We used to store value in static_chain_rtx, but that fails if pointers
710 are smaller than integers. We instead require that the user must pass
711 a second argument of 1, because that is what builtin_setjmp will
712 return. This also makes EH slightly more efficient, since we are no
713 longer copying around a value that we don't care about. */
714 gcc_assert (value == const1_rtx);
715
716 last = get_last_insn ();
717 #ifdef HAVE_builtin_longjmp
718 if (HAVE_builtin_longjmp)
719 emit_insn (gen_builtin_longjmp (buf_addr));
720 else
721 #endif
722 {
723 fp = gen_rtx_MEM (Pmode, buf_addr);
724 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
725 GET_MODE_SIZE (Pmode)));
726
727 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
728 2 * GET_MODE_SIZE (Pmode)));
729 set_mem_alias_set (fp, setjmp_alias_set);
730 set_mem_alias_set (lab, setjmp_alias_set);
731 set_mem_alias_set (stack, setjmp_alias_set);
732
733 /* Pick up FP, label, and SP from the block and jump. This code is
734 from expand_goto in stmt.c; see there for detailed comments. */
735 #if HAVE_nonlocal_goto
736 if (HAVE_nonlocal_goto)
737 /* We have to pass a value to the nonlocal_goto pattern that will
738 get copied into the static_chain pointer, but it does not matter
739 what that value is, because builtin_setjmp does not use it. */
740 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
741 else
742 #endif
743 {
744 lab = copy_to_reg (lab);
745
746 emit_insn (gen_rtx_CLOBBER (VOIDmode,
747 gen_rtx_MEM (BLKmode,
748 gen_rtx_SCRATCH (VOIDmode))));
749 emit_insn (gen_rtx_CLOBBER (VOIDmode,
750 gen_rtx_MEM (BLKmode,
751 hard_frame_pointer_rtx)));
752
753 emit_move_insn (hard_frame_pointer_rtx, fp);
754 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
755
756 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
757 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
758 emit_indirect_jump (lab);
759 }
760 }
761
762 /* Search backwards and mark the jump insn as a non-local goto.
763 Note that this precludes the use of __builtin_longjmp to a
764 __builtin_setjmp target in the same function. However, we've
765 already cautioned the user that these functions are for
766 internal exception handling use only. */
767 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
768 {
769 gcc_assert (insn != last);
770
771 if (JUMP_P (insn))
772 {
773 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
774 REG_NOTES (insn));
775 break;
776 }
777 else if (CALL_P (insn))
778 break;
779 }
780 }
781
782 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
783 and the address of the save area. */
784
785 static rtx
786 expand_builtin_nonlocal_goto (tree arglist)
787 {
788 tree t_label, t_save_area;
789 rtx r_label, r_save_area, r_fp, r_sp, insn;
790
791 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
792 return NULL_RTX;
793
794 t_label = TREE_VALUE (arglist);
795 arglist = TREE_CHAIN (arglist);
796 t_save_area = TREE_VALUE (arglist);
797
798 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
799 r_label = convert_memory_address (Pmode, r_label);
800 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
801 r_save_area = convert_memory_address (Pmode, r_save_area);
802 r_fp = gen_rtx_MEM (Pmode, r_save_area);
803 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
804 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
805
806 current_function_has_nonlocal_goto = 1;
807
808 #if HAVE_nonlocal_goto
809 /* ??? We no longer need to pass the static chain value, afaik. */
810 if (HAVE_nonlocal_goto)
811 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
812 else
813 #endif
814 {
815 r_label = copy_to_reg (r_label);
816
817 emit_insn (gen_rtx_CLOBBER (VOIDmode,
818 gen_rtx_MEM (BLKmode,
819 gen_rtx_SCRATCH (VOIDmode))));
820
821 emit_insn (gen_rtx_CLOBBER (VOIDmode,
822 gen_rtx_MEM (BLKmode,
823 hard_frame_pointer_rtx)));
824
825 /* Restore frame pointer for containing function.
826 This sets the actual hard register used for the frame pointer
827 to the location of the function's incoming static chain info.
828 The non-local goto handler will then adjust it to contain the
829 proper value and reload the argument pointer, if needed. */
830 emit_move_insn (hard_frame_pointer_rtx, r_fp);
831 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
832
833 /* USE of hard_frame_pointer_rtx added for consistency;
834 not clear if really needed. */
835 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
836 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
837 emit_indirect_jump (r_label);
838 }
839
840 /* Search backwards to the jump insn and mark it as a
841 non-local goto. */
842 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
843 {
844 if (JUMP_P (insn))
845 {
846 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
847 const0_rtx, REG_NOTES (insn));
848 break;
849 }
850 else if (CALL_P (insn))
851 break;
852 }
853
854 return const0_rtx;
855 }
856
857 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
858 (not all will be used on all machines) that was passed to __builtin_setjmp.
859 It updates the stack pointer in that block to correspond to the current
860 stack pointer. */
861
862 static void
863 expand_builtin_update_setjmp_buf (rtx buf_addr)
864 {
865 enum machine_mode sa_mode = Pmode;
866 rtx stack_save;
867
868
869 #ifdef HAVE_save_stack_nonlocal
870 if (HAVE_save_stack_nonlocal)
871 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
872 #endif
873 #ifdef STACK_SAVEAREA_MODE
874 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
875 #endif
876
877 stack_save
878 = gen_rtx_MEM (sa_mode,
879 memory_address
880 (sa_mode,
881 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
882
883 #ifdef HAVE_setjmp
884 if (HAVE_setjmp)
885 emit_insn (gen_setjmp ());
886 #endif
887
888 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
889 }
890
891 /* Expand a call to __builtin_prefetch. For a target that does not support
892 data prefetch, evaluate the memory address argument in case it has side
893 effects. */
894
895 static void
896 expand_builtin_prefetch (tree arglist)
897 {
898 tree arg0, arg1, arg2;
899 rtx op0, op1, op2;
900
901 if (!validate_arglist (arglist, POINTER_TYPE, 0))
902 return;
903
904 arg0 = TREE_VALUE (arglist);
905 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
906 zero (read) and argument 2 (locality) defaults to 3 (high degree of
907 locality). */
908 if (TREE_CHAIN (arglist))
909 {
910 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
911 if (TREE_CHAIN (TREE_CHAIN (arglist)))
912 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
913 else
914 arg2 = build_int_cst (NULL_TREE, 3);
915 }
916 else
917 {
918 arg1 = integer_zero_node;
919 arg2 = build_int_cst (NULL_TREE, 3);
920 }
921
922 /* Argument 0 is an address. */
923 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
924
925 /* Argument 1 (read/write flag) must be a compile-time constant int. */
926 if (TREE_CODE (arg1) != INTEGER_CST)
927 {
928 error ("second argument to %<__builtin_prefetch%> must be a constant");
929 arg1 = integer_zero_node;
930 }
931 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
932 /* Argument 1 must be either zero or one. */
933 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
934 {
935 warning ("invalid second argument to %<__builtin_prefetch%>;"
936 " using zero");
937 op1 = const0_rtx;
938 }
939
940 /* Argument 2 (locality) must be a compile-time constant int. */
941 if (TREE_CODE (arg2) != INTEGER_CST)
942 {
943 error ("third argument to %<__builtin_prefetch%> must be a constant");
944 arg2 = integer_zero_node;
945 }
946 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
947 /* Argument 2 must be 0, 1, 2, or 3. */
948 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
949 {
950 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
951 op2 = const0_rtx;
952 }
953
954 #ifdef HAVE_prefetch
955 if (HAVE_prefetch)
956 {
957 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
958 (op0,
959 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
960 || (GET_MODE (op0) != Pmode))
961 {
962 op0 = convert_memory_address (Pmode, op0);
963 op0 = force_reg (Pmode, op0);
964 }
965 emit_insn (gen_prefetch (op0, op1, op2));
966 }
967 #endif
968
969 /* Don't do anything with direct references to volatile memory, but
970 generate code to handle other side effects. */
971 if (!MEM_P (op0) && side_effects_p (op0))
972 emit_insn (op0);
973 }
974
975 /* Get a MEM rtx for expression EXP which is the address of an operand
976 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
977
978 static rtx
979 get_memory_rtx (tree exp)
980 {
981 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
982 rtx mem;
983
984 addr = convert_memory_address (Pmode, addr);
985
986 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
987
988 /* Get an expression we can use to find the attributes to assign to MEM.
989 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
990 we can. First remove any nops. */
991 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
992 || TREE_CODE (exp) == NON_LVALUE_EXPR)
993 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
994 exp = TREE_OPERAND (exp, 0);
995
996 if (TREE_CODE (exp) == ADDR_EXPR)
997 exp = TREE_OPERAND (exp, 0);
998 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
999 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1000 else
1001 exp = NULL;
1002
1003 /* Honor attributes derived from exp, except for the alias set
1004 (as builtin stringops may alias with anything) and the size
1005 (as stringops may access multiple array elements). */
1006 if (exp)
1007 {
1008 set_mem_attributes (mem, exp, 0);
1009 set_mem_alias_set (mem, 0);
1010 set_mem_size (mem, NULL_RTX);
1011 }
1012
1013 return mem;
1014 }
1015 \f
1016 /* Built-in functions to perform an untyped call and return. */
1017
1018 /* For each register that may be used for calling a function, this
1019 gives a mode used to copy the register's value. VOIDmode indicates
1020 the register is not used for calling a function. If the machine
1021 has register windows, this gives only the outbound registers.
1022 INCOMING_REGNO gives the corresponding inbound register. */
1023 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1024
1025 /* For each register that may be used for returning values, this gives
1026 a mode used to copy the register's value. VOIDmode indicates the
1027 register is not used for returning values. If the machine has
1028 register windows, this gives only the outbound registers.
1029 INCOMING_REGNO gives the corresponding inbound register. */
1030 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1031
1032 /* For each register that may be used for calling a function, this
1033 gives the offset of that register into the block returned by
1034 __builtin_apply_args. 0 indicates that the register is not
1035 used for calling a function. */
1036 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1037
1038 /* Return the size required for the block returned by __builtin_apply_args,
1039 and initialize apply_args_mode. */
1040
1041 static int
1042 apply_args_size (void)
1043 {
1044 static int size = -1;
1045 int align;
1046 unsigned int regno;
1047 enum machine_mode mode;
1048
1049 /* The values computed by this function never change. */
1050 if (size < 0)
1051 {
1052 /* The first value is the incoming arg-pointer. */
1053 size = GET_MODE_SIZE (Pmode);
1054
1055 /* The second value is the structure value address unless this is
1056 passed as an "invisible" first argument. */
1057 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1058 size += GET_MODE_SIZE (Pmode);
1059
1060 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1061 if (FUNCTION_ARG_REGNO_P (regno))
1062 {
1063 mode = reg_raw_mode[regno];
1064
1065 gcc_assert (mode != VOIDmode);
1066
1067 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1068 if (size % align != 0)
1069 size = CEIL (size, align) * align;
1070 apply_args_reg_offset[regno] = size;
1071 size += GET_MODE_SIZE (mode);
1072 apply_args_mode[regno] = mode;
1073 }
1074 else
1075 {
1076 apply_args_mode[regno] = VOIDmode;
1077 apply_args_reg_offset[regno] = 0;
1078 }
1079 }
1080 return size;
1081 }
1082
1083 /* Return the size required for the block returned by __builtin_apply,
1084 and initialize apply_result_mode. */
1085
1086 static int
1087 apply_result_size (void)
1088 {
1089 static int size = -1;
1090 int align, regno;
1091 enum machine_mode mode;
1092
1093 /* The values computed by this function never change. */
1094 if (size < 0)
1095 {
1096 size = 0;
1097
1098 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1099 if (FUNCTION_VALUE_REGNO_P (regno))
1100 {
1101 mode = reg_raw_mode[regno];
1102
1103 gcc_assert (mode != VOIDmode);
1104
1105 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1106 if (size % align != 0)
1107 size = CEIL (size, align) * align;
1108 size += GET_MODE_SIZE (mode);
1109 apply_result_mode[regno] = mode;
1110 }
1111 else
1112 apply_result_mode[regno] = VOIDmode;
1113
1114 /* Allow targets that use untyped_call and untyped_return to override
1115 the size so that machine-specific information can be stored here. */
1116 #ifdef APPLY_RESULT_SIZE
1117 size = APPLY_RESULT_SIZE;
1118 #endif
1119 }
1120 return size;
1121 }
1122
1123 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1124 /* Create a vector describing the result block RESULT. If SAVEP is true,
1125 the result block is used to save the values; otherwise it is used to
1126 restore the values. */
1127
1128 static rtx
1129 result_vector (int savep, rtx result)
1130 {
1131 int regno, size, align, nelts;
1132 enum machine_mode mode;
1133 rtx reg, mem;
1134 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1135
1136 size = nelts = 0;
1137 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1138 if ((mode = apply_result_mode[regno]) != VOIDmode)
1139 {
1140 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1141 if (size % align != 0)
1142 size = CEIL (size, align) * align;
1143 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1144 mem = adjust_address (result, mode, size);
1145 savevec[nelts++] = (savep
1146 ? gen_rtx_SET (VOIDmode, mem, reg)
1147 : gen_rtx_SET (VOIDmode, reg, mem));
1148 size += GET_MODE_SIZE (mode);
1149 }
1150 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1151 }
1152 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1153
1154 /* Save the state required to perform an untyped call with the same
1155 arguments as were passed to the current function. */
1156
1157 static rtx
1158 expand_builtin_apply_args_1 (void)
1159 {
1160 rtx registers, tem;
1161 int size, align, regno;
1162 enum machine_mode mode;
1163 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1164
1165 /* Create a block where the arg-pointer, structure value address,
1166 and argument registers can be saved. */
1167 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1168
1169 /* Walk past the arg-pointer and structure value address. */
1170 size = GET_MODE_SIZE (Pmode);
1171 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1172 size += GET_MODE_SIZE (Pmode);
1173
1174 /* Save each register used in calling a function to the block. */
1175 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1176 if ((mode = apply_args_mode[regno]) != VOIDmode)
1177 {
1178 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1179 if (size % align != 0)
1180 size = CEIL (size, align) * align;
1181
1182 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1183
1184 emit_move_insn (adjust_address (registers, mode, size), tem);
1185 size += GET_MODE_SIZE (mode);
1186 }
1187
1188 /* Save the arg pointer to the block. */
1189 tem = copy_to_reg (virtual_incoming_args_rtx);
1190 #ifdef STACK_GROWS_DOWNWARD
1191 /* We need the pointer as the caller actually passed them to us, not
1192 as we might have pretended they were passed. Make sure it's a valid
1193 operand, as emit_move_insn isn't expected to handle a PLUS. */
1194 tem
1195 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1196 NULL_RTX);
1197 #endif
1198 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1199
1200 size = GET_MODE_SIZE (Pmode);
1201
1202 /* Save the structure value address unless this is passed as an
1203 "invisible" first argument. */
1204 if (struct_incoming_value)
1205 {
1206 emit_move_insn (adjust_address (registers, Pmode, size),
1207 copy_to_reg (struct_incoming_value));
1208 size += GET_MODE_SIZE (Pmode);
1209 }
1210
1211 /* Return the address of the block. */
1212 return copy_addr_to_reg (XEXP (registers, 0));
1213 }
1214
1215 /* __builtin_apply_args returns block of memory allocated on
1216 the stack into which is stored the arg pointer, structure
1217 value address, static chain, and all the registers that might
1218 possibly be used in performing a function call. The code is
1219 moved to the start of the function so the incoming values are
1220 saved. */
1221
1222 static rtx
1223 expand_builtin_apply_args (void)
1224 {
1225 /* Don't do __builtin_apply_args more than once in a function.
1226 Save the result of the first call and reuse it. */
1227 if (apply_args_value != 0)
1228 return apply_args_value;
1229 {
1230 /* When this function is called, it means that registers must be
1231 saved on entry to this function. So we migrate the
1232 call to the first insn of this function. */
1233 rtx temp;
1234 rtx seq;
1235
1236 start_sequence ();
1237 temp = expand_builtin_apply_args_1 ();
1238 seq = get_insns ();
1239 end_sequence ();
1240
1241 apply_args_value = temp;
1242
1243 /* Put the insns after the NOTE that starts the function.
1244 If this is inside a start_sequence, make the outer-level insn
1245 chain current, so the code is placed at the start of the
1246 function. */
1247 push_topmost_sequence ();
1248 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1249 pop_topmost_sequence ();
1250 return temp;
1251 }
1252 }
1253
1254 /* Perform an untyped call and save the state required to perform an
1255 untyped return of whatever value was returned by the given function. */
1256
1257 static rtx
1258 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1259 {
1260 int size, align, regno;
1261 enum machine_mode mode;
1262 rtx incoming_args, result, reg, dest, src, call_insn;
1263 rtx old_stack_level = 0;
1264 rtx call_fusage = 0;
1265 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1266
1267 arguments = convert_memory_address (Pmode, arguments);
1268
1269 /* Create a block where the return registers can be saved. */
1270 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1271
1272 /* Fetch the arg pointer from the ARGUMENTS block. */
1273 incoming_args = gen_reg_rtx (Pmode);
1274 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1275 #ifndef STACK_GROWS_DOWNWARD
1276 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1277 incoming_args, 0, OPTAB_LIB_WIDEN);
1278 #endif
1279
1280 /* Push a new argument block and copy the arguments. Do not allow
1281 the (potential) memcpy call below to interfere with our stack
1282 manipulations. */
1283 do_pending_stack_adjust ();
1284 NO_DEFER_POP;
1285
1286 /* Save the stack with nonlocal if available. */
1287 #ifdef HAVE_save_stack_nonlocal
1288 if (HAVE_save_stack_nonlocal)
1289 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1290 else
1291 #endif
1292 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1293
1294 /* Allocate a block of memory onto the stack and copy the memory
1295 arguments to the outgoing arguments address. */
1296 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1297 dest = virtual_outgoing_args_rtx;
1298 #ifndef STACK_GROWS_DOWNWARD
1299 if (GET_CODE (argsize) == CONST_INT)
1300 dest = plus_constant (dest, -INTVAL (argsize));
1301 else
1302 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1303 #endif
1304 dest = gen_rtx_MEM (BLKmode, dest);
1305 set_mem_align (dest, PARM_BOUNDARY);
1306 src = gen_rtx_MEM (BLKmode, incoming_args);
1307 set_mem_align (src, PARM_BOUNDARY);
1308 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1309
1310 /* Refer to the argument block. */
1311 apply_args_size ();
1312 arguments = gen_rtx_MEM (BLKmode, arguments);
1313 set_mem_align (arguments, PARM_BOUNDARY);
1314
1315 /* Walk past the arg-pointer and structure value address. */
1316 size = GET_MODE_SIZE (Pmode);
1317 if (struct_value)
1318 size += GET_MODE_SIZE (Pmode);
1319
1320 /* Restore each of the registers previously saved. Make USE insns
1321 for each of these registers for use in making the call. */
1322 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1323 if ((mode = apply_args_mode[regno]) != VOIDmode)
1324 {
1325 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1326 if (size % align != 0)
1327 size = CEIL (size, align) * align;
1328 reg = gen_rtx_REG (mode, regno);
1329 emit_move_insn (reg, adjust_address (arguments, mode, size));
1330 use_reg (&call_fusage, reg);
1331 size += GET_MODE_SIZE (mode);
1332 }
1333
1334 /* Restore the structure value address unless this is passed as an
1335 "invisible" first argument. */
1336 size = GET_MODE_SIZE (Pmode);
1337 if (struct_value)
1338 {
1339 rtx value = gen_reg_rtx (Pmode);
1340 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1341 emit_move_insn (struct_value, value);
1342 if (REG_P (struct_value))
1343 use_reg (&call_fusage, struct_value);
1344 size += GET_MODE_SIZE (Pmode);
1345 }
1346
1347 /* All arguments and registers used for the call are set up by now! */
1348 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1349
1350 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1351 and we don't want to load it into a register as an optimization,
1352 because prepare_call_address already did it if it should be done. */
1353 if (GET_CODE (function) != SYMBOL_REF)
1354 function = memory_address (FUNCTION_MODE, function);
1355
1356 /* Generate the actual call instruction and save the return value. */
1357 #ifdef HAVE_untyped_call
1358 if (HAVE_untyped_call)
1359 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1360 result, result_vector (1, result)));
1361 else
1362 #endif
1363 #ifdef HAVE_call_value
1364 if (HAVE_call_value)
1365 {
1366 rtx valreg = 0;
1367
1368 /* Locate the unique return register. It is not possible to
1369 express a call that sets more than one return register using
1370 call_value; use untyped_call for that. In fact, untyped_call
1371 only needs to save the return registers in the given block. */
1372 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1373 if ((mode = apply_result_mode[regno]) != VOIDmode)
1374 {
1375 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1376
1377 valreg = gen_rtx_REG (mode, regno);
1378 }
1379
1380 emit_call_insn (GEN_CALL_VALUE (valreg,
1381 gen_rtx_MEM (FUNCTION_MODE, function),
1382 const0_rtx, NULL_RTX, const0_rtx));
1383
1384 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1385 }
1386 else
1387 #endif
1388 gcc_unreachable ();
1389
1390 /* Find the CALL insn we just emitted, and attach the register usage
1391 information. */
1392 call_insn = last_call_insn ();
1393 add_function_usage_to (call_insn, call_fusage);
1394
1395 /* Restore the stack. */
1396 #ifdef HAVE_save_stack_nonlocal
1397 if (HAVE_save_stack_nonlocal)
1398 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1399 else
1400 #endif
1401 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1402
1403 OK_DEFER_POP;
1404
1405 /* Return the address of the result block. */
1406 result = copy_addr_to_reg (XEXP (result, 0));
1407 return convert_memory_address (ptr_mode, result);
1408 }
1409
1410 /* Perform an untyped return. */
1411
1412 static void
1413 expand_builtin_return (rtx result)
1414 {
1415 int size, align, regno;
1416 enum machine_mode mode;
1417 rtx reg;
1418 rtx call_fusage = 0;
1419
1420 result = convert_memory_address (Pmode, result);
1421
1422 apply_result_size ();
1423 result = gen_rtx_MEM (BLKmode, result);
1424
1425 #ifdef HAVE_untyped_return
1426 if (HAVE_untyped_return)
1427 {
1428 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1429 emit_barrier ();
1430 return;
1431 }
1432 #endif
1433
1434 /* Restore the return value and note that each value is used. */
1435 size = 0;
1436 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1437 if ((mode = apply_result_mode[regno]) != VOIDmode)
1438 {
1439 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1440 if (size % align != 0)
1441 size = CEIL (size, align) * align;
1442 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1443 emit_move_insn (reg, adjust_address (result, mode, size));
1444
1445 push_to_sequence (call_fusage);
1446 emit_insn (gen_rtx_USE (VOIDmode, reg));
1447 call_fusage = get_insns ();
1448 end_sequence ();
1449 size += GET_MODE_SIZE (mode);
1450 }
1451
1452 /* Put the USE insns before the return. */
1453 emit_insn (call_fusage);
1454
1455 /* Return whatever values was restored by jumping directly to the end
1456 of the function. */
1457 expand_naked_return ();
1458 }
1459
1460 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1461
1462 static enum type_class
1463 type_to_class (tree type)
1464 {
1465 switch (TREE_CODE (type))
1466 {
1467 case VOID_TYPE: return void_type_class;
1468 case INTEGER_TYPE: return integer_type_class;
1469 case CHAR_TYPE: return char_type_class;
1470 case ENUMERAL_TYPE: return enumeral_type_class;
1471 case BOOLEAN_TYPE: return boolean_type_class;
1472 case POINTER_TYPE: return pointer_type_class;
1473 case REFERENCE_TYPE: return reference_type_class;
1474 case OFFSET_TYPE: return offset_type_class;
1475 case REAL_TYPE: return real_type_class;
1476 case COMPLEX_TYPE: return complex_type_class;
1477 case FUNCTION_TYPE: return function_type_class;
1478 case METHOD_TYPE: return method_type_class;
1479 case RECORD_TYPE: return record_type_class;
1480 case UNION_TYPE:
1481 case QUAL_UNION_TYPE: return union_type_class;
1482 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1483 ? string_type_class : array_type_class);
1484 case FILE_TYPE: return file_type_class;
1485 case LANG_TYPE: return lang_type_class;
1486 default: return no_type_class;
1487 }
1488 }
1489
1490 /* Expand a call to __builtin_classify_type with arguments found in
1491 ARGLIST. */
1492
1493 static rtx
1494 expand_builtin_classify_type (tree arglist)
1495 {
1496 if (arglist != 0)
1497 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1498 return GEN_INT (no_type_class);
1499 }
1500
1501 /* This helper macro, meant to be used in mathfn_built_in below,
1502 determines which among a set of three builtin math functions is
1503 appropriate for a given type mode. The `F' and `L' cases are
1504 automatically generated from the `double' case. */
1505 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1506 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1507 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1508 fcodel = BUILT_IN_MATHFN##L ; break;
1509
1510 /* Return mathematic function equivalent to FN but operating directly
1511 on TYPE, if available. If we can't do the conversion, return zero. */
1512 tree
1513 mathfn_built_in (tree type, enum built_in_function fn)
1514 {
1515 enum built_in_function fcode, fcodef, fcodel;
1516
1517 switch (fn)
1518 {
1519 CASE_MATHFN (BUILT_IN_ACOS)
1520 CASE_MATHFN (BUILT_IN_ACOSH)
1521 CASE_MATHFN (BUILT_IN_ASIN)
1522 CASE_MATHFN (BUILT_IN_ASINH)
1523 CASE_MATHFN (BUILT_IN_ATAN)
1524 CASE_MATHFN (BUILT_IN_ATAN2)
1525 CASE_MATHFN (BUILT_IN_ATANH)
1526 CASE_MATHFN (BUILT_IN_CBRT)
1527 CASE_MATHFN (BUILT_IN_CEIL)
1528 CASE_MATHFN (BUILT_IN_COPYSIGN)
1529 CASE_MATHFN (BUILT_IN_COS)
1530 CASE_MATHFN (BUILT_IN_COSH)
1531 CASE_MATHFN (BUILT_IN_DREM)
1532 CASE_MATHFN (BUILT_IN_ERF)
1533 CASE_MATHFN (BUILT_IN_ERFC)
1534 CASE_MATHFN (BUILT_IN_EXP)
1535 CASE_MATHFN (BUILT_IN_EXP10)
1536 CASE_MATHFN (BUILT_IN_EXP2)
1537 CASE_MATHFN (BUILT_IN_EXPM1)
1538 CASE_MATHFN (BUILT_IN_FABS)
1539 CASE_MATHFN (BUILT_IN_FDIM)
1540 CASE_MATHFN (BUILT_IN_FLOOR)
1541 CASE_MATHFN (BUILT_IN_FMA)
1542 CASE_MATHFN (BUILT_IN_FMAX)
1543 CASE_MATHFN (BUILT_IN_FMIN)
1544 CASE_MATHFN (BUILT_IN_FMOD)
1545 CASE_MATHFN (BUILT_IN_FREXP)
1546 CASE_MATHFN (BUILT_IN_GAMMA)
1547 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1548 CASE_MATHFN (BUILT_IN_HYPOT)
1549 CASE_MATHFN (BUILT_IN_ILOGB)
1550 CASE_MATHFN (BUILT_IN_INF)
1551 CASE_MATHFN (BUILT_IN_J0)
1552 CASE_MATHFN (BUILT_IN_J1)
1553 CASE_MATHFN (BUILT_IN_JN)
1554 CASE_MATHFN (BUILT_IN_LDEXP)
1555 CASE_MATHFN (BUILT_IN_LGAMMA)
1556 CASE_MATHFN (BUILT_IN_LLRINT)
1557 CASE_MATHFN (BUILT_IN_LLROUND)
1558 CASE_MATHFN (BUILT_IN_LOG)
1559 CASE_MATHFN (BUILT_IN_LOG10)
1560 CASE_MATHFN (BUILT_IN_LOG1P)
1561 CASE_MATHFN (BUILT_IN_LOG2)
1562 CASE_MATHFN (BUILT_IN_LOGB)
1563 CASE_MATHFN (BUILT_IN_LRINT)
1564 CASE_MATHFN (BUILT_IN_LROUND)
1565 CASE_MATHFN (BUILT_IN_MODF)
1566 CASE_MATHFN (BUILT_IN_NAN)
1567 CASE_MATHFN (BUILT_IN_NANS)
1568 CASE_MATHFN (BUILT_IN_NEARBYINT)
1569 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1570 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1571 CASE_MATHFN (BUILT_IN_POW)
1572 CASE_MATHFN (BUILT_IN_POWI)
1573 CASE_MATHFN (BUILT_IN_POW10)
1574 CASE_MATHFN (BUILT_IN_REMAINDER)
1575 CASE_MATHFN (BUILT_IN_REMQUO)
1576 CASE_MATHFN (BUILT_IN_RINT)
1577 CASE_MATHFN (BUILT_IN_ROUND)
1578 CASE_MATHFN (BUILT_IN_SCALB)
1579 CASE_MATHFN (BUILT_IN_SCALBLN)
1580 CASE_MATHFN (BUILT_IN_SCALBN)
1581 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1582 CASE_MATHFN (BUILT_IN_SIN)
1583 CASE_MATHFN (BUILT_IN_SINCOS)
1584 CASE_MATHFN (BUILT_IN_SINH)
1585 CASE_MATHFN (BUILT_IN_SQRT)
1586 CASE_MATHFN (BUILT_IN_TAN)
1587 CASE_MATHFN (BUILT_IN_TANH)
1588 CASE_MATHFN (BUILT_IN_TGAMMA)
1589 CASE_MATHFN (BUILT_IN_TRUNC)
1590 CASE_MATHFN (BUILT_IN_Y0)
1591 CASE_MATHFN (BUILT_IN_Y1)
1592 CASE_MATHFN (BUILT_IN_YN)
1593
1594 default:
1595 return 0;
1596 }
1597
1598 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1599 return implicit_built_in_decls[fcode];
1600 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1601 return implicit_built_in_decls[fcodef];
1602 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1603 return implicit_built_in_decls[fcodel];
1604 else
1605 return 0;
1606 }
1607
1608 /* If errno must be maintained, expand the RTL to check if the result,
1609 TARGET, of a built-in function call, EXP, is NaN, and if so set
1610 errno to EDOM. */
1611
1612 static void
1613 expand_errno_check (tree exp, rtx target)
1614 {
1615 rtx lab = gen_label_rtx ();
1616
1617 /* Test the result; if it is NaN, set errno=EDOM because
1618 the argument was not in the domain. */
1619 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1620 0, lab);
1621
1622 #ifdef TARGET_EDOM
1623 /* If this built-in doesn't throw an exception, set errno directly. */
1624 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1625 {
1626 #ifdef GEN_ERRNO_RTX
1627 rtx errno_rtx = GEN_ERRNO_RTX;
1628 #else
1629 rtx errno_rtx
1630 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1631 #endif
1632 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1633 emit_label (lab);
1634 return;
1635 }
1636 #endif
1637
1638 /* We can't set errno=EDOM directly; let the library call do it.
1639 Pop the arguments right away in case the call gets deleted. */
1640 NO_DEFER_POP;
1641 expand_call (exp, target, 0);
1642 OK_DEFER_POP;
1643 emit_label (lab);
1644 }
1645
1646
1647 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1648 Return 0 if a normal call should be emitted rather than expanding the
1649 function in-line. EXP is the expression that is a call to the builtin
1650 function; if convenient, the result should be placed in TARGET.
1651 SUBTARGET may be used as the target for computing one of EXP's operands. */
1652
1653 static rtx
1654 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1655 {
1656 optab builtin_optab;
1657 rtx op0, insns, before_call;
1658 tree fndecl = get_callee_fndecl (exp);
1659 tree arglist = TREE_OPERAND (exp, 1);
1660 enum machine_mode mode;
1661 bool errno_set = false;
1662 tree arg, narg;
1663
1664 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1665 return 0;
1666
1667 arg = TREE_VALUE (arglist);
1668
1669 switch (DECL_FUNCTION_CODE (fndecl))
1670 {
1671 case BUILT_IN_SQRT:
1672 case BUILT_IN_SQRTF:
1673 case BUILT_IN_SQRTL:
1674 errno_set = ! tree_expr_nonnegative_p (arg);
1675 builtin_optab = sqrt_optab;
1676 break;
1677 case BUILT_IN_EXP:
1678 case BUILT_IN_EXPF:
1679 case BUILT_IN_EXPL:
1680 errno_set = true; builtin_optab = exp_optab; break;
1681 case BUILT_IN_EXP10:
1682 case BUILT_IN_EXP10F:
1683 case BUILT_IN_EXP10L:
1684 case BUILT_IN_POW10:
1685 case BUILT_IN_POW10F:
1686 case BUILT_IN_POW10L:
1687 errno_set = true; builtin_optab = exp10_optab; break;
1688 case BUILT_IN_EXP2:
1689 case BUILT_IN_EXP2F:
1690 case BUILT_IN_EXP2L:
1691 errno_set = true; builtin_optab = exp2_optab; break;
1692 case BUILT_IN_EXPM1:
1693 case BUILT_IN_EXPM1F:
1694 case BUILT_IN_EXPM1L:
1695 errno_set = true; builtin_optab = expm1_optab; break;
1696 case BUILT_IN_LOGB:
1697 case BUILT_IN_LOGBF:
1698 case BUILT_IN_LOGBL:
1699 errno_set = true; builtin_optab = logb_optab; break;
1700 case BUILT_IN_ILOGB:
1701 case BUILT_IN_ILOGBF:
1702 case BUILT_IN_ILOGBL:
1703 errno_set = true; builtin_optab = ilogb_optab; break;
1704 case BUILT_IN_LOG:
1705 case BUILT_IN_LOGF:
1706 case BUILT_IN_LOGL:
1707 errno_set = true; builtin_optab = log_optab; break;
1708 case BUILT_IN_LOG10:
1709 case BUILT_IN_LOG10F:
1710 case BUILT_IN_LOG10L:
1711 errno_set = true; builtin_optab = log10_optab; break;
1712 case BUILT_IN_LOG2:
1713 case BUILT_IN_LOG2F:
1714 case BUILT_IN_LOG2L:
1715 errno_set = true; builtin_optab = log2_optab; break;
1716 case BUILT_IN_LOG1P:
1717 case BUILT_IN_LOG1PF:
1718 case BUILT_IN_LOG1PL:
1719 errno_set = true; builtin_optab = log1p_optab; break;
1720 case BUILT_IN_ASIN:
1721 case BUILT_IN_ASINF:
1722 case BUILT_IN_ASINL:
1723 builtin_optab = asin_optab; break;
1724 case BUILT_IN_ACOS:
1725 case BUILT_IN_ACOSF:
1726 case BUILT_IN_ACOSL:
1727 builtin_optab = acos_optab; break;
1728 case BUILT_IN_TAN:
1729 case BUILT_IN_TANF:
1730 case BUILT_IN_TANL:
1731 builtin_optab = tan_optab; break;
1732 case BUILT_IN_ATAN:
1733 case BUILT_IN_ATANF:
1734 case BUILT_IN_ATANL:
1735 builtin_optab = atan_optab; break;
1736 case BUILT_IN_FLOOR:
1737 case BUILT_IN_FLOORF:
1738 case BUILT_IN_FLOORL:
1739 builtin_optab = floor_optab; break;
1740 case BUILT_IN_CEIL:
1741 case BUILT_IN_CEILF:
1742 case BUILT_IN_CEILL:
1743 builtin_optab = ceil_optab; break;
1744 case BUILT_IN_TRUNC:
1745 case BUILT_IN_TRUNCF:
1746 case BUILT_IN_TRUNCL:
1747 builtin_optab = btrunc_optab; break;
1748 case BUILT_IN_ROUND:
1749 case BUILT_IN_ROUNDF:
1750 case BUILT_IN_ROUNDL:
1751 builtin_optab = round_optab; break;
1752 case BUILT_IN_NEARBYINT:
1753 case BUILT_IN_NEARBYINTF:
1754 case BUILT_IN_NEARBYINTL:
1755 builtin_optab = nearbyint_optab; break;
1756 case BUILT_IN_RINT:
1757 case BUILT_IN_RINTF:
1758 case BUILT_IN_RINTL:
1759 builtin_optab = rint_optab; break;
1760 default:
1761 gcc_unreachable ();
1762 }
1763
1764 /* Make a suitable register to place result in. */
1765 mode = TYPE_MODE (TREE_TYPE (exp));
1766
1767 if (! flag_errno_math || ! HONOR_NANS (mode))
1768 errno_set = false;
1769
1770 /* Before working hard, check whether the instruction is available. */
1771 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1772 {
1773 target = gen_reg_rtx (mode);
1774
1775 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1776 need to expand the argument again. This way, we will not perform
1777 side-effects more the once. */
1778 narg = builtin_save_expr (arg);
1779 if (narg != arg)
1780 {
1781 arg = narg;
1782 arglist = build_tree_list (NULL_TREE, arg);
1783 exp = build_function_call_expr (fndecl, arglist);
1784 }
1785
1786 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1787
1788 start_sequence ();
1789
1790 /* Compute into TARGET.
1791 Set TARGET to wherever the result comes back. */
1792 target = expand_unop (mode, builtin_optab, op0, target, 0);
1793
1794 if (target != 0)
1795 {
1796 if (errno_set)
1797 expand_errno_check (exp, target);
1798
1799 /* Output the entire sequence. */
1800 insns = get_insns ();
1801 end_sequence ();
1802 emit_insn (insns);
1803 return target;
1804 }
1805
1806 /* If we were unable to expand via the builtin, stop the sequence
1807 (without outputting the insns) and call to the library function
1808 with the stabilized argument list. */
1809 end_sequence ();
1810 }
1811
1812 before_call = get_last_insn ();
1813
1814 target = expand_call (exp, target, target == const0_rtx);
1815
1816 /* If this is a sqrt operation and we don't care about errno, try to
1817 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1818 This allows the semantics of the libcall to be visible to the RTL
1819 optimizers. */
1820 if (builtin_optab == sqrt_optab && !errno_set)
1821 {
1822 /* Search backwards through the insns emitted by expand_call looking
1823 for the instruction with the REG_RETVAL note. */
1824 rtx last = get_last_insn ();
1825 while (last != before_call)
1826 {
1827 if (find_reg_note (last, REG_RETVAL, NULL))
1828 {
1829 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1830 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1831 two elements, i.e. symbol_ref(sqrt) and the operand. */
1832 if (note
1833 && GET_CODE (note) == EXPR_LIST
1834 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1835 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1836 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1837 {
1838 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1839 /* Check operand is a register with expected mode. */
1840 if (operand
1841 && REG_P (operand)
1842 && GET_MODE (operand) == mode)
1843 {
1844 /* Replace the REG_EQUAL note with a SQRT rtx. */
1845 rtx equiv = gen_rtx_SQRT (mode, operand);
1846 set_unique_reg_note (last, REG_EQUAL, equiv);
1847 }
1848 }
1849 break;
1850 }
1851 last = PREV_INSN (last);
1852 }
1853 }
1854
1855 return target;
1856 }
1857
1858 /* Expand a call to the builtin binary math functions (pow and atan2).
1859 Return 0 if a normal call should be emitted rather than expanding the
1860 function in-line. EXP is the expression that is a call to the builtin
1861 function; if convenient, the result should be placed in TARGET.
1862 SUBTARGET may be used as the target for computing one of EXP's
1863 operands. */
1864
1865 static rtx
1866 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1867 {
1868 optab builtin_optab;
1869 rtx op0, op1, insns;
1870 int op1_type = REAL_TYPE;
1871 tree fndecl = get_callee_fndecl (exp);
1872 tree arglist = TREE_OPERAND (exp, 1);
1873 tree arg0, arg1, temp, narg;
1874 enum machine_mode mode;
1875 bool errno_set = true;
1876 bool stable = true;
1877
1878 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1879 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1880 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1881 op1_type = INTEGER_TYPE;
1882
1883 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1884 return 0;
1885
1886 arg0 = TREE_VALUE (arglist);
1887 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1888
1889 switch (DECL_FUNCTION_CODE (fndecl))
1890 {
1891 case BUILT_IN_POW:
1892 case BUILT_IN_POWF:
1893 case BUILT_IN_POWL:
1894 builtin_optab = pow_optab; break;
1895 case BUILT_IN_ATAN2:
1896 case BUILT_IN_ATAN2F:
1897 case BUILT_IN_ATAN2L:
1898 builtin_optab = atan2_optab; break;
1899 case BUILT_IN_LDEXP:
1900 case BUILT_IN_LDEXPF:
1901 case BUILT_IN_LDEXPL:
1902 builtin_optab = ldexp_optab; break;
1903 case BUILT_IN_FMOD:
1904 case BUILT_IN_FMODF:
1905 case BUILT_IN_FMODL:
1906 builtin_optab = fmod_optab; break;
1907 case BUILT_IN_DREM:
1908 case BUILT_IN_DREMF:
1909 case BUILT_IN_DREML:
1910 builtin_optab = drem_optab; break;
1911 default:
1912 gcc_unreachable ();
1913 }
1914
1915 /* Make a suitable register to place result in. */
1916 mode = TYPE_MODE (TREE_TYPE (exp));
1917
1918 /* Before working hard, check whether the instruction is available. */
1919 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1920 return 0;
1921
1922 target = gen_reg_rtx (mode);
1923
1924 if (! flag_errno_math || ! HONOR_NANS (mode))
1925 errno_set = false;
1926
1927 /* Always stabilize the argument list. */
1928 narg = builtin_save_expr (arg1);
1929 if (narg != arg1)
1930 {
1931 arg1 = narg;
1932 temp = build_tree_list (NULL_TREE, narg);
1933 stable = false;
1934 }
1935 else
1936 temp = TREE_CHAIN (arglist);
1937
1938 narg = builtin_save_expr (arg0);
1939 if (narg != arg0)
1940 {
1941 arg0 = narg;
1942 arglist = tree_cons (NULL_TREE, narg, temp);
1943 stable = false;
1944 }
1945 else if (! stable)
1946 arglist = tree_cons (NULL_TREE, arg0, temp);
1947
1948 if (! stable)
1949 exp = build_function_call_expr (fndecl, arglist);
1950
1951 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1952 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1953
1954 start_sequence ();
1955
1956 /* Compute into TARGET.
1957 Set TARGET to wherever the result comes back. */
1958 target = expand_binop (mode, builtin_optab, op0, op1,
1959 target, 0, OPTAB_DIRECT);
1960
1961 /* If we were unable to expand via the builtin, stop the sequence
1962 (without outputting the insns) and call to the library function
1963 with the stabilized argument list. */
1964 if (target == 0)
1965 {
1966 end_sequence ();
1967 return expand_call (exp, target, target == const0_rtx);
1968 }
1969
1970 if (errno_set)
1971 expand_errno_check (exp, target);
1972
1973 /* Output the entire sequence. */
1974 insns = get_insns ();
1975 end_sequence ();
1976 emit_insn (insns);
1977
1978 return target;
1979 }
1980
1981 /* Expand a call to the builtin sin and cos math functions.
1982 Return 0 if a normal call should be emitted rather than expanding the
1983 function in-line. EXP is the expression that is a call to the builtin
1984 function; if convenient, the result should be placed in TARGET.
1985 SUBTARGET may be used as the target for computing one of EXP's
1986 operands. */
1987
1988 static rtx
1989 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1990 {
1991 optab builtin_optab;
1992 rtx op0, insns, before_call;
1993 tree fndecl = get_callee_fndecl (exp);
1994 tree arglist = TREE_OPERAND (exp, 1);
1995 enum machine_mode mode;
1996 bool errno_set = false;
1997 tree arg, narg;
1998
1999 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2000 return 0;
2001
2002 arg = TREE_VALUE (arglist);
2003
2004 switch (DECL_FUNCTION_CODE (fndecl))
2005 {
2006 case BUILT_IN_SIN:
2007 case BUILT_IN_SINF:
2008 case BUILT_IN_SINL:
2009 case BUILT_IN_COS:
2010 case BUILT_IN_COSF:
2011 case BUILT_IN_COSL:
2012 builtin_optab = sincos_optab; break;
2013 default:
2014 gcc_unreachable ();
2015 }
2016
2017 /* Make a suitable register to place result in. */
2018 mode = TYPE_MODE (TREE_TYPE (exp));
2019
2020 if (! flag_errno_math || ! HONOR_NANS (mode))
2021 errno_set = false;
2022
2023 /* Check if sincos insn is available, otherwise fallback
2024 to sin or cos insn. */
2025 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2026 switch (DECL_FUNCTION_CODE (fndecl))
2027 {
2028 case BUILT_IN_SIN:
2029 case BUILT_IN_SINF:
2030 case BUILT_IN_SINL:
2031 builtin_optab = sin_optab; break;
2032 case BUILT_IN_COS:
2033 case BUILT_IN_COSF:
2034 case BUILT_IN_COSL:
2035 builtin_optab = cos_optab; break;
2036 default:
2037 gcc_unreachable ();
2038 }
2039 }
2040
2041 /* Before working hard, check whether the instruction is available. */
2042 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2043 {
2044 target = gen_reg_rtx (mode);
2045
2046 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2047 need to expand the argument again. This way, we will not perform
2048 side-effects more the once. */
2049 narg = save_expr (arg);
2050 if (narg != arg)
2051 {
2052 arg = narg;
2053 arglist = build_tree_list (NULL_TREE, arg);
2054 exp = build_function_call_expr (fndecl, arglist);
2055 }
2056
2057 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2058
2059 start_sequence ();
2060
2061 /* Compute into TARGET.
2062 Set TARGET to wherever the result comes back. */
2063 if (builtin_optab == sincos_optab)
2064 {
2065 int result;
2066
2067 switch (DECL_FUNCTION_CODE (fndecl))
2068 {
2069 case BUILT_IN_SIN:
2070 case BUILT_IN_SINF:
2071 case BUILT_IN_SINL:
2072 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2073 break;
2074 case BUILT_IN_COS:
2075 case BUILT_IN_COSF:
2076 case BUILT_IN_COSL:
2077 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2078 break;
2079 default:
2080 gcc_unreachable ();
2081 }
2082 gcc_assert (result);
2083 }
2084 else
2085 {
2086 target = expand_unop (mode, builtin_optab, op0, target, 0);
2087 }
2088
2089 if (target != 0)
2090 {
2091 if (errno_set)
2092 expand_errno_check (exp, target);
2093
2094 /* Output the entire sequence. */
2095 insns = get_insns ();
2096 end_sequence ();
2097 emit_insn (insns);
2098 return target;
2099 }
2100
2101 /* If we were unable to expand via the builtin, stop the sequence
2102 (without outputting the insns) and call to the library function
2103 with the stabilized argument list. */
2104 end_sequence ();
2105 }
2106
2107 before_call = get_last_insn ();
2108
2109 target = expand_call (exp, target, target == const0_rtx);
2110
2111 return target;
2112 }
2113
2114 /* To evaluate powi(x,n), the floating point value x raised to the
2115 constant integer exponent n, we use a hybrid algorithm that
2116 combines the "window method" with look-up tables. For an
2117 introduction to exponentiation algorithms and "addition chains",
2118 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2119 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2120 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2121 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2122
2123 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2124 multiplications to inline before calling the system library's pow
2125 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2126 so this default never requires calling pow, powf or powl. */
2127
2128 #ifndef POWI_MAX_MULTS
2129 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2130 #endif
2131
2132 /* The size of the "optimal power tree" lookup table. All
2133 exponents less than this value are simply looked up in the
2134 powi_table below. This threshold is also used to size the
2135 cache of pseudo registers that hold intermediate results. */
2136 #define POWI_TABLE_SIZE 256
2137
2138 /* The size, in bits of the window, used in the "window method"
2139 exponentiation algorithm. This is equivalent to a radix of
2140 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2141 #define POWI_WINDOW_SIZE 3
2142
2143 /* The following table is an efficient representation of an
2144 "optimal power tree". For each value, i, the corresponding
2145 value, j, in the table states than an optimal evaluation
2146 sequence for calculating pow(x,i) can be found by evaluating
2147 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2148 100 integers is given in Knuth's "Seminumerical algorithms". */
2149
2150 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2151 {
2152 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2153 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2154 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2155 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2156 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2157 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2158 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2159 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2160 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2161 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2162 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2163 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2164 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2165 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2166 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2167 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2168 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2169 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2170 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2171 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2172 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2173 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2174 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2175 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2176 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2177 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2178 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2179 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2180 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2181 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2182 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2183 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2184 };
2185
2186
2187 /* Return the number of multiplications required to calculate
2188 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2189 subroutine of powi_cost. CACHE is an array indicating
2190 which exponents have already been calculated. */
2191
2192 static int
2193 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2194 {
2195 /* If we've already calculated this exponent, then this evaluation
2196 doesn't require any additional multiplications. */
2197 if (cache[n])
2198 return 0;
2199
2200 cache[n] = true;
2201 return powi_lookup_cost (n - powi_table[n], cache)
2202 + powi_lookup_cost (powi_table[n], cache) + 1;
2203 }
2204
2205 /* Return the number of multiplications required to calculate
2206 powi(x,n) for an arbitrary x, given the exponent N. This
2207 function needs to be kept in sync with expand_powi below. */
2208
2209 static int
2210 powi_cost (HOST_WIDE_INT n)
2211 {
2212 bool cache[POWI_TABLE_SIZE];
2213 unsigned HOST_WIDE_INT digit;
2214 unsigned HOST_WIDE_INT val;
2215 int result;
2216
2217 if (n == 0)
2218 return 0;
2219
2220 /* Ignore the reciprocal when calculating the cost. */
2221 val = (n < 0) ? -n : n;
2222
2223 /* Initialize the exponent cache. */
2224 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2225 cache[1] = true;
2226
2227 result = 0;
2228
2229 while (val >= POWI_TABLE_SIZE)
2230 {
2231 if (val & 1)
2232 {
2233 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2234 result += powi_lookup_cost (digit, cache)
2235 + POWI_WINDOW_SIZE + 1;
2236 val >>= POWI_WINDOW_SIZE;
2237 }
2238 else
2239 {
2240 val >>= 1;
2241 result++;
2242 }
2243 }
2244
2245 return result + powi_lookup_cost (val, cache);
2246 }
2247
2248 /* Recursive subroutine of expand_powi. This function takes the array,
2249 CACHE, of already calculated exponents and an exponent N and returns
2250 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2251
2252 static rtx
2253 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2254 {
2255 unsigned HOST_WIDE_INT digit;
2256 rtx target, result;
2257 rtx op0, op1;
2258
2259 if (n < POWI_TABLE_SIZE)
2260 {
2261 if (cache[n])
2262 return cache[n];
2263
2264 target = gen_reg_rtx (mode);
2265 cache[n] = target;
2266
2267 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2268 op1 = expand_powi_1 (mode, powi_table[n], cache);
2269 }
2270 else if (n & 1)
2271 {
2272 target = gen_reg_rtx (mode);
2273 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2274 op0 = expand_powi_1 (mode, n - digit, cache);
2275 op1 = expand_powi_1 (mode, digit, cache);
2276 }
2277 else
2278 {
2279 target = gen_reg_rtx (mode);
2280 op0 = expand_powi_1 (mode, n >> 1, cache);
2281 op1 = op0;
2282 }
2283
2284 result = expand_mult (mode, op0, op1, target, 0);
2285 if (result != target)
2286 emit_move_insn (target, result);
2287 return target;
2288 }
2289
2290 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2291 floating point operand in mode MODE, and N is the exponent. This
2292 function needs to be kept in sync with powi_cost above. */
2293
2294 static rtx
2295 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2296 {
2297 unsigned HOST_WIDE_INT val;
2298 rtx cache[POWI_TABLE_SIZE];
2299 rtx result;
2300
2301 if (n == 0)
2302 return CONST1_RTX (mode);
2303
2304 val = (n < 0) ? -n : n;
2305
2306 memset (cache, 0, sizeof (cache));
2307 cache[1] = x;
2308
2309 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2310
2311 /* If the original exponent was negative, reciprocate the result. */
2312 if (n < 0)
2313 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2314 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2315
2316 return result;
2317 }
2318
2319 /* Expand a call to the pow built-in mathematical function. Return 0 if
2320 a normal call should be emitted rather than expanding the function
2321 in-line. EXP is the expression that is a call to the builtin
2322 function; if convenient, the result should be placed in TARGET. */
2323
2324 static rtx
2325 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2326 {
2327 tree arglist = TREE_OPERAND (exp, 1);
2328 tree arg0, arg1;
2329
2330 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2331 return 0;
2332
2333 arg0 = TREE_VALUE (arglist);
2334 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2335
2336 if (TREE_CODE (arg1) == REAL_CST
2337 && ! TREE_CONSTANT_OVERFLOW (arg1))
2338 {
2339 REAL_VALUE_TYPE cint;
2340 REAL_VALUE_TYPE c;
2341 HOST_WIDE_INT n;
2342
2343 c = TREE_REAL_CST (arg1);
2344 n = real_to_integer (&c);
2345 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2346 if (real_identical (&c, &cint))
2347 {
2348 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2349 Otherwise, check the number of multiplications required.
2350 Note that pow never sets errno for an integer exponent. */
2351 if ((n >= -1 && n <= 2)
2352 || (flag_unsafe_math_optimizations
2353 && ! optimize_size
2354 && powi_cost (n) <= POWI_MAX_MULTS))
2355 {
2356 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2357 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2358 op = force_reg (mode, op);
2359 return expand_powi (op, mode, n);
2360 }
2361 }
2362 }
2363
2364 if (! flag_unsafe_math_optimizations)
2365 return NULL_RTX;
2366 return expand_builtin_mathfn_2 (exp, target, subtarget);
2367 }
2368
2369 /* Expand a call to the powi built-in mathematical function. Return 0 if
2370 a normal call should be emitted rather than expanding the function
2371 in-line. EXP is the expression that is a call to the builtin
2372 function; if convenient, the result should be placed in TARGET. */
2373
2374 static rtx
2375 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2376 {
2377 tree arglist = TREE_OPERAND (exp, 1);
2378 tree arg0, arg1;
2379 rtx op0, op1;
2380 enum machine_mode mode;
2381
2382 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2383 return 0;
2384
2385 arg0 = TREE_VALUE (arglist);
2386 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2387 mode = TYPE_MODE (TREE_TYPE (exp));
2388
2389 /* Handle constant power. */
2390
2391 if (TREE_CODE (arg1) == INTEGER_CST
2392 && ! TREE_CONSTANT_OVERFLOW (arg1))
2393 {
2394 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2395
2396 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2397 Otherwise, check the number of multiplications required. */
2398 if ((TREE_INT_CST_HIGH (arg1) == 0
2399 || TREE_INT_CST_HIGH (arg1) == -1)
2400 && ((n >= -1 && n <= 2)
2401 || (! optimize_size
2402 && powi_cost (n) <= POWI_MAX_MULTS)))
2403 {
2404 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2405 op0 = force_reg (mode, op0);
2406 return expand_powi (op0, mode, n);
2407 }
2408 }
2409
2410 /* Emit a libcall to libgcc. */
2411
2412 if (target == NULL_RTX)
2413 target = gen_reg_rtx (mode);
2414
2415 op0 = expand_expr (arg0, subtarget, mode, 0);
2416 if (GET_MODE (op0) != mode)
2417 op0 = convert_to_mode (mode, op0, 0);
2418 op1 = expand_expr (arg1, 0, word_mode, 0);
2419 if (GET_MODE (op1) != word_mode)
2420 op1 = convert_to_mode (word_mode, op1, 0);
2421
2422 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2423 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2424 op0, mode, op1, word_mode);
2425
2426 return target;
2427 }
2428
2429 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2430 if we failed the caller should emit a normal call, otherwise
2431 try to get the result in TARGET, if convenient. */
2432
2433 static rtx
2434 expand_builtin_strlen (tree arglist, rtx target,
2435 enum machine_mode target_mode)
2436 {
2437 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2438 return 0;
2439 else
2440 {
2441 rtx pat;
2442 tree len, src = TREE_VALUE (arglist);
2443 rtx result, src_reg, char_rtx, before_strlen;
2444 enum machine_mode insn_mode = target_mode, char_mode;
2445 enum insn_code icode = CODE_FOR_nothing;
2446 int align;
2447
2448 /* If the length can be computed at compile-time, return it. */
2449 len = c_strlen (src, 0);
2450 if (len)
2451 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2452
2453 /* If the length can be computed at compile-time and is constant
2454 integer, but there are side-effects in src, evaluate
2455 src for side-effects, then return len.
2456 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2457 can be optimized into: i++; x = 3; */
2458 len = c_strlen (src, 1);
2459 if (len && TREE_CODE (len) == INTEGER_CST)
2460 {
2461 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2462 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2463 }
2464
2465 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2466
2467 /* If SRC is not a pointer type, don't do this operation inline. */
2468 if (align == 0)
2469 return 0;
2470
2471 /* Bail out if we can't compute strlen in the right mode. */
2472 while (insn_mode != VOIDmode)
2473 {
2474 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2475 if (icode != CODE_FOR_nothing)
2476 break;
2477
2478 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2479 }
2480 if (insn_mode == VOIDmode)
2481 return 0;
2482
2483 /* Make a place to write the result of the instruction. */
2484 result = target;
2485 if (! (result != 0
2486 && REG_P (result)
2487 && GET_MODE (result) == insn_mode
2488 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2489 result = gen_reg_rtx (insn_mode);
2490
2491 /* Make a place to hold the source address. We will not expand
2492 the actual source until we are sure that the expansion will
2493 not fail -- there are trees that cannot be expanded twice. */
2494 src_reg = gen_reg_rtx (Pmode);
2495
2496 /* Mark the beginning of the strlen sequence so we can emit the
2497 source operand later. */
2498 before_strlen = get_last_insn ();
2499
2500 char_rtx = const0_rtx;
2501 char_mode = insn_data[(int) icode].operand[2].mode;
2502 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2503 char_mode))
2504 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2505
2506 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2507 char_rtx, GEN_INT (align));
2508 if (! pat)
2509 return 0;
2510 emit_insn (pat);
2511
2512 /* Now that we are assured of success, expand the source. */
2513 start_sequence ();
2514 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2515 if (pat != src_reg)
2516 emit_move_insn (src_reg, pat);
2517 pat = get_insns ();
2518 end_sequence ();
2519
2520 if (before_strlen)
2521 emit_insn_after (pat, before_strlen);
2522 else
2523 emit_insn_before (pat, get_insns ());
2524
2525 /* Return the value in the proper mode for this function. */
2526 if (GET_MODE (result) == target_mode)
2527 target = result;
2528 else if (target != 0)
2529 convert_move (target, result, 0);
2530 else
2531 target = convert_to_mode (target_mode, result, 0);
2532
2533 return target;
2534 }
2535 }
2536
2537 /* Expand a call to the strstr builtin. Return 0 if we failed the
2538 caller should emit a normal call, otherwise try to get the result
2539 in TARGET, if convenient (and in mode MODE if that's convenient). */
2540
2541 static rtx
2542 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2543 {
2544 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2545 {
2546 tree result = fold_builtin_strstr (arglist, type);
2547 if (result)
2548 return expand_expr (result, target, mode, EXPAND_NORMAL);
2549 }
2550 return 0;
2551 }
2552
2553 /* Expand a call to the strchr builtin. Return 0 if we failed the
2554 caller should emit a normal call, otherwise try to get the result
2555 in TARGET, if convenient (and in mode MODE if that's convenient). */
2556
2557 static rtx
2558 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2559 {
2560 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2561 {
2562 tree result = fold_builtin_strchr (arglist, type);
2563 if (result)
2564 return expand_expr (result, target, mode, EXPAND_NORMAL);
2565
2566 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2567 }
2568 return 0;
2569 }
2570
2571 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2572 caller should emit a normal call, otherwise try to get the result
2573 in TARGET, if convenient (and in mode MODE if that's convenient). */
2574
2575 static rtx
2576 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2577 {
2578 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2579 {
2580 tree result = fold_builtin_strrchr (arglist, type);
2581 if (result)
2582 return expand_expr (result, target, mode, EXPAND_NORMAL);
2583 }
2584 return 0;
2585 }
2586
2587 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2588 caller should emit a normal call, otherwise try to get the result
2589 in TARGET, if convenient (and in mode MODE if that's convenient). */
2590
2591 static rtx
2592 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2593 {
2594 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2595 {
2596 tree result = fold_builtin_strpbrk (arglist, type);
2597 if (result)
2598 return expand_expr (result, target, mode, EXPAND_NORMAL);
2599 }
2600 return 0;
2601 }
2602
2603 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2604 bytes from constant string DATA + OFFSET and return it as target
2605 constant. */
2606
2607 static rtx
2608 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2609 enum machine_mode mode)
2610 {
2611 const char *str = (const char *) data;
2612
2613 gcc_assert (offset >= 0
2614 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2615 <= strlen (str) + 1));
2616
2617 return c_readstr (str + offset, mode);
2618 }
2619
2620 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2621 Return 0 if we failed, the caller should emit a normal call,
2622 otherwise try to get the result in TARGET, if convenient (and in
2623 mode MODE if that's convenient). */
2624 static rtx
2625 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2626 {
2627 tree arglist = TREE_OPERAND (exp, 1);
2628 if (!validate_arglist (arglist,
2629 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2630 return 0;
2631 else
2632 {
2633 tree dest = TREE_VALUE (arglist);
2634 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2635 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2636 const char *src_str;
2637 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2638 unsigned int dest_align
2639 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2640 rtx dest_mem, src_mem, dest_addr, len_rtx;
2641 tree result = fold_builtin_memcpy (exp);
2642
2643 if (result)
2644 return expand_expr (result, target, mode, EXPAND_NORMAL);
2645
2646 /* If DEST is not a pointer type, call the normal function. */
2647 if (dest_align == 0)
2648 return 0;
2649
2650 /* If either SRC is not a pointer type, don't do this
2651 operation in-line. */
2652 if (src_align == 0)
2653 return 0;
2654
2655 dest_mem = get_memory_rtx (dest);
2656 set_mem_align (dest_mem, dest_align);
2657 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2658 src_str = c_getstr (src);
2659
2660 /* If SRC is a string constant and block move would be done
2661 by pieces, we can avoid loading the string from memory
2662 and only stored the computed constants. */
2663 if (src_str
2664 && GET_CODE (len_rtx) == CONST_INT
2665 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2666 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2667 (void *) src_str, dest_align))
2668 {
2669 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2670 builtin_memcpy_read_str,
2671 (void *) src_str, dest_align, 0);
2672 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2673 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2674 return dest_mem;
2675 }
2676
2677 src_mem = get_memory_rtx (src);
2678 set_mem_align (src_mem, src_align);
2679
2680 /* Copy word part most expediently. */
2681 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2682 BLOCK_OP_NORMAL);
2683
2684 if (dest_addr == 0)
2685 {
2686 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2687 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2688 }
2689 return dest_addr;
2690 }
2691 }
2692
2693 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2694 Return 0 if we failed the caller should emit a normal call,
2695 otherwise try to get the result in TARGET, if convenient (and in
2696 mode MODE if that's convenient). If ENDP is 0 return the
2697 destination pointer, if ENDP is 1 return the end pointer ala
2698 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2699 stpcpy. */
2700
2701 static rtx
2702 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2703 int endp)
2704 {
2705 if (!validate_arglist (arglist,
2706 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2707 return 0;
2708 /* If return value is ignored, transform mempcpy into memcpy. */
2709 else if (target == const0_rtx)
2710 {
2711 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2712
2713 if (!fn)
2714 return 0;
2715
2716 return expand_expr (build_function_call_expr (fn, arglist),
2717 target, mode, EXPAND_NORMAL);
2718 }
2719 else
2720 {
2721 tree dest = TREE_VALUE (arglist);
2722 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2723 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2724 const char *src_str;
2725 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2726 unsigned int dest_align
2727 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2728 rtx dest_mem, src_mem, len_rtx;
2729 tree result = fold_builtin_mempcpy (arglist, type, endp);
2730
2731 if (result)
2732 return expand_expr (result, target, mode, EXPAND_NORMAL);
2733
2734 /* If either SRC or DEST is not a pointer type, don't do this
2735 operation in-line. */
2736 if (dest_align == 0 || src_align == 0)
2737 return 0;
2738
2739 /* If LEN is not constant, call the normal function. */
2740 if (! host_integerp (len, 1))
2741 return 0;
2742
2743 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2744 src_str = c_getstr (src);
2745
2746 /* If SRC is a string constant and block move would be done
2747 by pieces, we can avoid loading the string from memory
2748 and only stored the computed constants. */
2749 if (src_str
2750 && GET_CODE (len_rtx) == CONST_INT
2751 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2752 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2753 (void *) src_str, dest_align))
2754 {
2755 dest_mem = get_memory_rtx (dest);
2756 set_mem_align (dest_mem, dest_align);
2757 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2758 builtin_memcpy_read_str,
2759 (void *) src_str, dest_align, endp);
2760 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2761 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2762 return dest_mem;
2763 }
2764
2765 if (GET_CODE (len_rtx) == CONST_INT
2766 && can_move_by_pieces (INTVAL (len_rtx),
2767 MIN (dest_align, src_align)))
2768 {
2769 dest_mem = get_memory_rtx (dest);
2770 set_mem_align (dest_mem, dest_align);
2771 src_mem = get_memory_rtx (src);
2772 set_mem_align (src_mem, src_align);
2773 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2774 MIN (dest_align, src_align), endp);
2775 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2776 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2777 return dest_mem;
2778 }
2779
2780 return 0;
2781 }
2782 }
2783
2784 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2785 if we failed the caller should emit a normal call. */
2786
2787 static rtx
2788 expand_builtin_memmove (tree arglist, tree type, rtx target,
2789 enum machine_mode mode)
2790 {
2791 if (!validate_arglist (arglist,
2792 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2793 return 0;
2794 else
2795 {
2796 tree dest = TREE_VALUE (arglist);
2797 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2798 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2799
2800 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2801 unsigned int dest_align
2802 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2803 tree result = fold_builtin_memmove (arglist, type);
2804
2805 if (result)
2806 return expand_expr (result, target, mode, EXPAND_NORMAL);
2807
2808 /* If DEST is not a pointer type, call the normal function. */
2809 if (dest_align == 0)
2810 return 0;
2811
2812 /* If either SRC is not a pointer type, don't do this
2813 operation in-line. */
2814 if (src_align == 0)
2815 return 0;
2816
2817 /* If src is categorized for a readonly section we can use
2818 normal memcpy. */
2819 if (readonly_data_expr (src))
2820 {
2821 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2822 if (!fn)
2823 return 0;
2824 return expand_expr (build_function_call_expr (fn, arglist),
2825 target, mode, EXPAND_NORMAL);
2826 }
2827
2828 /* If length is 1 and we can expand memcpy call inline,
2829 it is ok to use memcpy as well. */
2830 if (integer_onep (len))
2831 {
2832 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2833 /*endp=*/0);
2834 if (ret)
2835 return ret;
2836 }
2837
2838 /* Otherwise, call the normal function. */
2839 return 0;
2840 }
2841 }
2842
2843 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2844 if we failed the caller should emit a normal call. */
2845
2846 static rtx
2847 expand_builtin_bcopy (tree arglist, tree type)
2848 {
2849 tree src, dest, size, newarglist;
2850
2851 if (!validate_arglist (arglist,
2852 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2853 return NULL_RTX;
2854
2855 src = TREE_VALUE (arglist);
2856 dest = TREE_VALUE (TREE_CHAIN (arglist));
2857 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2858
2859 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2860 memmove(ptr y, ptr x, size_t z). This is done this way
2861 so that if it isn't expanded inline, we fallback to
2862 calling bcopy instead of memmove. */
2863
2864 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2865 newarglist = tree_cons (NULL_TREE, src, newarglist);
2866 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2867
2868 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2869 }
2870
2871 #ifndef HAVE_movstr
2872 # define HAVE_movstr 0
2873 # define CODE_FOR_movstr CODE_FOR_nothing
2874 #endif
2875
2876 /* Expand into a movstr instruction, if one is available. Return 0 if
2877 we failed, the caller should emit a normal call, otherwise try to
2878 get the result in TARGET, if convenient. If ENDP is 0 return the
2879 destination pointer, if ENDP is 1 return the end pointer ala
2880 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2881 stpcpy. */
2882
2883 static rtx
2884 expand_movstr (tree dest, tree src, rtx target, int endp)
2885 {
2886 rtx end;
2887 rtx dest_mem;
2888 rtx src_mem;
2889 rtx insn;
2890 const struct insn_data * data;
2891
2892 if (!HAVE_movstr)
2893 return 0;
2894
2895 dest_mem = get_memory_rtx (dest);
2896 src_mem = get_memory_rtx (src);
2897 if (!endp)
2898 {
2899 target = force_reg (Pmode, XEXP (dest_mem, 0));
2900 dest_mem = replace_equiv_address (dest_mem, target);
2901 end = gen_reg_rtx (Pmode);
2902 }
2903 else
2904 {
2905 if (target == 0 || target == const0_rtx)
2906 {
2907 end = gen_reg_rtx (Pmode);
2908 if (target == 0)
2909 target = end;
2910 }
2911 else
2912 end = target;
2913 }
2914
2915 data = insn_data + CODE_FOR_movstr;
2916
2917 if (data->operand[0].mode != VOIDmode)
2918 end = gen_lowpart (data->operand[0].mode, end);
2919
2920 insn = data->genfun (end, dest_mem, src_mem);
2921
2922 gcc_assert (insn);
2923
2924 emit_insn (insn);
2925
2926 /* movstr is supposed to set end to the address of the NUL
2927 terminator. If the caller requested a mempcpy-like return value,
2928 adjust it. */
2929 if (endp == 1 && target != const0_rtx)
2930 {
2931 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
2932 emit_move_insn (target, force_operand (tem, NULL_RTX));
2933 }
2934
2935 return target;
2936 }
2937
2938 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2939 if we failed the caller should emit a normal call, otherwise try to get
2940 the result in TARGET, if convenient (and in mode MODE if that's
2941 convenient). */
2942
2943 static rtx
2944 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
2945 {
2946 tree arglist = TREE_OPERAND (exp, 1);
2947 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2948 {
2949 tree result = fold_builtin_strcpy (exp, 0);
2950 if (result)
2951 return expand_expr (result, target, mode, EXPAND_NORMAL);
2952
2953 return expand_movstr (TREE_VALUE (arglist),
2954 TREE_VALUE (TREE_CHAIN (arglist)),
2955 target, /*endp=*/0);
2956 }
2957 return 0;
2958 }
2959
2960 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2961 Return 0 if we failed the caller should emit a normal call,
2962 otherwise try to get the result in TARGET, if convenient (and in
2963 mode MODE if that's convenient). */
2964
2965 static rtx
2966 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
2967 {
2968 tree arglist = TREE_OPERAND (exp, 1);
2969 /* If return value is ignored, transform stpcpy into strcpy. */
2970 if (target == const0_rtx)
2971 {
2972 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2973 if (!fn)
2974 return 0;
2975
2976 return expand_expr (build_function_call_expr (fn, arglist),
2977 target, mode, EXPAND_NORMAL);
2978 }
2979
2980 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2981 return 0;
2982 else
2983 {
2984 tree dst, src, len, lenp1;
2985 tree narglist;
2986 rtx ret;
2987
2988 /* Ensure we get an actual string whose length can be evaluated at
2989 compile-time, not an expression containing a string. This is
2990 because the latter will potentially produce pessimized code
2991 when used to produce the return value. */
2992 src = TREE_VALUE (TREE_CHAIN (arglist));
2993 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2994 return expand_movstr (TREE_VALUE (arglist),
2995 TREE_VALUE (TREE_CHAIN (arglist)),
2996 target, /*endp=*/2);
2997
2998 dst = TREE_VALUE (arglist);
2999 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3000 narglist = build_tree_list (NULL_TREE, lenp1);
3001 narglist = tree_cons (NULL_TREE, src, narglist);
3002 narglist = tree_cons (NULL_TREE, dst, narglist);
3003 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3004 target, mode, /*endp=*/2);
3005
3006 if (ret)
3007 return ret;
3008
3009 if (TREE_CODE (len) == INTEGER_CST)
3010 {
3011 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3012
3013 if (GET_CODE (len_rtx) == CONST_INT)
3014 {
3015 ret = expand_builtin_strcpy (exp, target, mode);
3016
3017 if (ret)
3018 {
3019 if (! target)
3020 {
3021 if (mode != VOIDmode)
3022 target = gen_reg_rtx (mode);
3023 else
3024 target = gen_reg_rtx (GET_MODE (ret));
3025 }
3026 if (GET_MODE (target) != GET_MODE (ret))
3027 ret = gen_lowpart (GET_MODE (target), ret);
3028
3029 ret = plus_constant (ret, INTVAL (len_rtx));
3030 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3031 gcc_assert (ret);
3032
3033 return target;
3034 }
3035 }
3036 }
3037
3038 return expand_movstr (TREE_VALUE (arglist),
3039 TREE_VALUE (TREE_CHAIN (arglist)),
3040 target, /*endp=*/2);
3041 }
3042 }
3043
3044 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3045 bytes from constant string DATA + OFFSET and return it as target
3046 constant. */
3047
3048 static rtx
3049 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3050 enum machine_mode mode)
3051 {
3052 const char *str = (const char *) data;
3053
3054 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3055 return const0_rtx;
3056
3057 return c_readstr (str + offset, mode);
3058 }
3059
3060 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3061 if we failed the caller should emit a normal call. */
3062
3063 static rtx
3064 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3065 {
3066 tree arglist = TREE_OPERAND (exp, 1);
3067 if (validate_arglist (arglist,
3068 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3069 {
3070 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3071 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3072 tree result = fold_builtin_strncpy (exp, slen);
3073
3074 if (result)
3075 return expand_expr (result, target, mode, EXPAND_NORMAL);
3076
3077 /* We must be passed a constant len and src parameter. */
3078 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3079 return 0;
3080
3081 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3082
3083 /* We're required to pad with trailing zeros if the requested
3084 len is greater than strlen(s2)+1. In that case try to
3085 use store_by_pieces, if it fails, punt. */
3086 if (tree_int_cst_lt (slen, len))
3087 {
3088 tree dest = TREE_VALUE (arglist);
3089 unsigned int dest_align
3090 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3091 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3092 rtx dest_mem;
3093
3094 if (!p || dest_align == 0 || !host_integerp (len, 1)
3095 || !can_store_by_pieces (tree_low_cst (len, 1),
3096 builtin_strncpy_read_str,
3097 (void *) p, dest_align))
3098 return 0;
3099
3100 dest_mem = get_memory_rtx (dest);
3101 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3102 builtin_strncpy_read_str,
3103 (void *) p, dest_align, 0);
3104 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3105 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3106 return dest_mem;
3107 }
3108 }
3109 return 0;
3110 }
3111
3112 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3113 bytes from constant string DATA + OFFSET and return it as target
3114 constant. */
3115
3116 static rtx
3117 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3118 enum machine_mode mode)
3119 {
3120 const char *c = (const char *) data;
3121 char *p = alloca (GET_MODE_SIZE (mode));
3122
3123 memset (p, *c, GET_MODE_SIZE (mode));
3124
3125 return c_readstr (p, mode);
3126 }
3127
3128 /* Callback routine for store_by_pieces. Return the RTL of a register
3129 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3130 char value given in the RTL register data. For example, if mode is
3131 4 bytes wide, return the RTL for 0x01010101*data. */
3132
3133 static rtx
3134 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3135 enum machine_mode mode)
3136 {
3137 rtx target, coeff;
3138 size_t size;
3139 char *p;
3140
3141 size = GET_MODE_SIZE (mode);
3142 if (size == 1)
3143 return (rtx) data;
3144
3145 p = alloca (size);
3146 memset (p, 1, size);
3147 coeff = c_readstr (p, mode);
3148
3149 target = convert_to_mode (mode, (rtx) data, 1);
3150 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3151 return force_reg (mode, target);
3152 }
3153
3154 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3155 if we failed the caller should emit a normal call, otherwise try to get
3156 the result in TARGET, if convenient (and in mode MODE if that's
3157 convenient). */
3158
3159 static rtx
3160 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3161 {
3162 if (!validate_arglist (arglist,
3163 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3164 return 0;
3165 else
3166 {
3167 tree dest = TREE_VALUE (arglist);
3168 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3169 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3170 char c;
3171
3172 unsigned int dest_align
3173 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3174 rtx dest_mem, dest_addr, len_rtx;
3175
3176 /* If DEST is not a pointer type, don't do this
3177 operation in-line. */
3178 if (dest_align == 0)
3179 return 0;
3180
3181 /* If the LEN parameter is zero, return DEST. */
3182 if (integer_zerop (len))
3183 {
3184 /* Evaluate and ignore VAL in case it has side-effects. */
3185 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3186 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3187 }
3188
3189 if (TREE_CODE (val) != INTEGER_CST)
3190 {
3191 rtx val_rtx;
3192
3193 if (!host_integerp (len, 1))
3194 return 0;
3195
3196 if (optimize_size && tree_low_cst (len, 1) > 1)
3197 return 0;
3198
3199 /* Assume that we can memset by pieces if we can store the
3200 * the coefficients by pieces (in the required modes).
3201 * We can't pass builtin_memset_gen_str as that emits RTL. */
3202 c = 1;
3203 if (!can_store_by_pieces (tree_low_cst (len, 1),
3204 builtin_memset_read_str,
3205 &c, dest_align))
3206 return 0;
3207
3208 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3209 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3210 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3211 val_rtx);
3212 dest_mem = get_memory_rtx (dest);
3213 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3214 builtin_memset_gen_str,
3215 val_rtx, dest_align, 0);
3216 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3217 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3218 return dest_mem;
3219 }
3220
3221 if (target_char_cast (val, &c))
3222 return 0;
3223
3224 if (c)
3225 {
3226 if (!host_integerp (len, 1))
3227 return 0;
3228 if (!can_store_by_pieces (tree_low_cst (len, 1),
3229 builtin_memset_read_str, &c,
3230 dest_align))
3231 return 0;
3232
3233 dest_mem = get_memory_rtx (dest);
3234 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3235 builtin_memset_read_str,
3236 &c, dest_align, 0);
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 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3243
3244 dest_mem = get_memory_rtx (dest);
3245 set_mem_align (dest_mem, dest_align);
3246 dest_addr = clear_storage (dest_mem, len_rtx);
3247
3248 if (dest_addr == 0)
3249 {
3250 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3251 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3252 }
3253
3254 return dest_addr;
3255 }
3256 }
3257
3258 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3259 if we failed the caller should emit a normal call. */
3260
3261 static rtx
3262 expand_builtin_bzero (tree arglist)
3263 {
3264 tree dest, size, newarglist;
3265
3266 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3267 return NULL_RTX;
3268
3269 dest = TREE_VALUE (arglist);
3270 size = TREE_VALUE (TREE_CHAIN (arglist));
3271
3272 /* New argument list transforming bzero(ptr x, int y) to
3273 memset(ptr x, int 0, size_t y). This is done this way
3274 so that if it isn't expanded inline, we fallback to
3275 calling bzero instead of memset. */
3276
3277 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3278 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3279 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3280
3281 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3282 }
3283
3284 /* Expand expression EXP, which is a call to the memcmp built-in function.
3285 ARGLIST is the argument list for this call. Return 0 if we failed and the
3286 caller should emit a normal call, otherwise try to get the result in
3287 TARGET, if convenient (and in mode MODE, if that's convenient). */
3288
3289 static rtx
3290 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3291 enum machine_mode mode)
3292 {
3293 if (!validate_arglist (arglist,
3294 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3295 return 0;
3296 else
3297 {
3298 tree result = fold_builtin_memcmp (arglist);
3299 if (result)
3300 return expand_expr (result, target, mode, EXPAND_NORMAL);
3301 }
3302
3303 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3304 {
3305 tree arg1 = TREE_VALUE (arglist);
3306 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3307 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3308 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3309 rtx result;
3310 rtx insn;
3311
3312 int arg1_align
3313 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3314 int arg2_align
3315 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3316 enum machine_mode insn_mode;
3317
3318 #ifdef HAVE_cmpmemsi
3319 if (HAVE_cmpmemsi)
3320 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3321 else
3322 #endif
3323 #ifdef HAVE_cmpstrsi
3324 if (HAVE_cmpstrsi)
3325 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3326 else
3327 #endif
3328 return 0;
3329
3330 /* If we don't have POINTER_TYPE, call the function. */
3331 if (arg1_align == 0 || arg2_align == 0)
3332 return 0;
3333
3334 /* Make a place to write the result of the instruction. */
3335 result = target;
3336 if (! (result != 0
3337 && REG_P (result) && GET_MODE (result) == insn_mode
3338 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3339 result = gen_reg_rtx (insn_mode);
3340
3341 arg1_rtx = get_memory_rtx (arg1);
3342 arg2_rtx = get_memory_rtx (arg2);
3343 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3344
3345 /* Set MEM_SIZE as appropriate. */
3346 if (GET_CODE (arg3_rtx) == CONST_INT)
3347 {
3348 set_mem_size (arg1_rtx, arg3_rtx);
3349 set_mem_size (arg2_rtx, arg3_rtx);
3350 }
3351
3352 #ifdef HAVE_cmpmemsi
3353 if (HAVE_cmpmemsi)
3354 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3355 GEN_INT (MIN (arg1_align, arg2_align)));
3356 else
3357 #endif
3358 #ifdef HAVE_cmpstrsi
3359 if (HAVE_cmpstrsi)
3360 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3361 GEN_INT (MIN (arg1_align, arg2_align)));
3362 else
3363 #endif
3364 gcc_unreachable ();
3365
3366 if (insn)
3367 emit_insn (insn);
3368 else
3369 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3370 TYPE_MODE (integer_type_node), 3,
3371 XEXP (arg1_rtx, 0), Pmode,
3372 XEXP (arg2_rtx, 0), Pmode,
3373 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3374 TYPE_UNSIGNED (sizetype)),
3375 TYPE_MODE (sizetype));
3376
3377 /* Return the value in the proper mode for this function. */
3378 mode = TYPE_MODE (TREE_TYPE (exp));
3379 if (GET_MODE (result) == mode)
3380 return result;
3381 else if (target != 0)
3382 {
3383 convert_move (target, result, 0);
3384 return target;
3385 }
3386 else
3387 return convert_to_mode (mode, result, 0);
3388 }
3389 #endif
3390
3391 return 0;
3392 }
3393
3394 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3395 if we failed the caller should emit a normal call, otherwise try to get
3396 the result in TARGET, if convenient. */
3397
3398 static rtx
3399 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3400 {
3401 tree arglist = TREE_OPERAND (exp, 1);
3402
3403 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3404 return 0;
3405 else
3406 {
3407 tree result = fold_builtin_strcmp (arglist);
3408 if (result)
3409 return expand_expr (result, target, mode, EXPAND_NORMAL);
3410 }
3411
3412 #ifdef HAVE_cmpstrsi
3413 if (HAVE_cmpstrsi)
3414 {
3415 tree arg1 = TREE_VALUE (arglist);
3416 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3417 tree len, len1, len2;
3418 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3419 rtx result, insn;
3420 tree fndecl;
3421
3422 int arg1_align
3423 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3424 int arg2_align
3425 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3426 enum machine_mode insn_mode
3427 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3428
3429 len1 = c_strlen (arg1, 1);
3430 len2 = c_strlen (arg2, 1);
3431
3432 if (len1)
3433 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3434 if (len2)
3435 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3436
3437 /* If we don't have a constant length for the first, use the length
3438 of the second, if we know it. We don't require a constant for
3439 this case; some cost analysis could be done if both are available
3440 but neither is constant. For now, assume they're equally cheap,
3441 unless one has side effects. If both strings have constant lengths,
3442 use the smaller. */
3443
3444 if (!len1)
3445 len = len2;
3446 else if (!len2)
3447 len = len1;
3448 else if (TREE_SIDE_EFFECTS (len1))
3449 len = len2;
3450 else if (TREE_SIDE_EFFECTS (len2))
3451 len = len1;
3452 else if (TREE_CODE (len1) != INTEGER_CST)
3453 len = len2;
3454 else if (TREE_CODE (len2) != INTEGER_CST)
3455 len = len1;
3456 else if (tree_int_cst_lt (len1, len2))
3457 len = len1;
3458 else
3459 len = len2;
3460
3461 /* If both arguments have side effects, we cannot optimize. */
3462 if (!len || TREE_SIDE_EFFECTS (len))
3463 return 0;
3464
3465 /* If we don't have POINTER_TYPE, call the function. */
3466 if (arg1_align == 0 || arg2_align == 0)
3467 return 0;
3468
3469 /* Make a place to write the result of the instruction. */
3470 result = target;
3471 if (! (result != 0
3472 && REG_P (result) && GET_MODE (result) == insn_mode
3473 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3474 result = gen_reg_rtx (insn_mode);
3475
3476 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3477 arg1 = builtin_save_expr (arg1);
3478 arg2 = builtin_save_expr (arg2);
3479
3480 arg1_rtx = get_memory_rtx (arg1);
3481 arg2_rtx = get_memory_rtx (arg2);
3482 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3483 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3484 GEN_INT (MIN (arg1_align, arg2_align)));
3485 if (insn)
3486 {
3487 emit_insn (insn);
3488
3489 /* Return the value in the proper mode for this function. */
3490 mode = TYPE_MODE (TREE_TYPE (exp));
3491 if (GET_MODE (result) == mode)
3492 return result;
3493 if (target == 0)
3494 return convert_to_mode (mode, result, 0);
3495 convert_move (target, result, 0);
3496 return target;
3497 }
3498
3499 /* Expand the library call ourselves using a stabilized argument
3500 list to avoid re-evaluating the function's arguments twice. */
3501 arglist = build_tree_list (NULL_TREE, arg2);
3502 arglist = tree_cons (NULL_TREE, arg1, arglist);
3503 fndecl = get_callee_fndecl (exp);
3504 exp = build_function_call_expr (fndecl, arglist);
3505 return expand_call (exp, target, target == const0_rtx);
3506 }
3507 #endif
3508 return 0;
3509 }
3510
3511 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3512 if we failed the caller should emit a normal call, otherwise try to get
3513 the result in TARGET, if convenient. */
3514
3515 static rtx
3516 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3517 {
3518 tree arglist = TREE_OPERAND (exp, 1);
3519
3520 if (!validate_arglist (arglist,
3521 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3522 return 0;
3523 else
3524 {
3525 tree result = fold_builtin_strncmp (arglist);
3526 if (result)
3527 return expand_expr (result, target, mode, EXPAND_NORMAL);
3528 }
3529
3530 /* If c_strlen can determine an expression for one of the string
3531 lengths, and it doesn't have side effects, then emit cmpstrsi
3532 using length MIN(strlen(string)+1, arg3). */
3533 #ifdef HAVE_cmpstrsi
3534 if (HAVE_cmpstrsi)
3535 {
3536 tree arg1 = TREE_VALUE (arglist);
3537 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3538 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3539 tree len, len1, len2;
3540 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3541 rtx result, insn;
3542 tree fndecl;
3543
3544 int arg1_align
3545 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3546 int arg2_align
3547 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3548 enum machine_mode insn_mode
3549 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3550
3551 len1 = c_strlen (arg1, 1);
3552 len2 = c_strlen (arg2, 1);
3553
3554 if (len1)
3555 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3556 if (len2)
3557 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3558
3559 /* If we don't have a constant length for the first, use the length
3560 of the second, if we know it. We don't require a constant for
3561 this case; some cost analysis could be done if both are available
3562 but neither is constant. For now, assume they're equally cheap,
3563 unless one has side effects. If both strings have constant lengths,
3564 use the smaller. */
3565
3566 if (!len1)
3567 len = len2;
3568 else if (!len2)
3569 len = len1;
3570 else if (TREE_SIDE_EFFECTS (len1))
3571 len = len2;
3572 else if (TREE_SIDE_EFFECTS (len2))
3573 len = len1;
3574 else if (TREE_CODE (len1) != INTEGER_CST)
3575 len = len2;
3576 else if (TREE_CODE (len2) != INTEGER_CST)
3577 len = len1;
3578 else if (tree_int_cst_lt (len1, len2))
3579 len = len1;
3580 else
3581 len = len2;
3582
3583 /* If both arguments have side effects, we cannot optimize. */
3584 if (!len || TREE_SIDE_EFFECTS (len))
3585 return 0;
3586
3587 /* The actual new length parameter is MIN(len,arg3). */
3588 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3589 fold_convert (TREE_TYPE (len), arg3)));
3590
3591 /* If we don't have POINTER_TYPE, call the function. */
3592 if (arg1_align == 0 || arg2_align == 0)
3593 return 0;
3594
3595 /* Make a place to write the result of the instruction. */
3596 result = target;
3597 if (! (result != 0
3598 && REG_P (result) && GET_MODE (result) == insn_mode
3599 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3600 result = gen_reg_rtx (insn_mode);
3601
3602 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3603 arg1 = builtin_save_expr (arg1);
3604 arg2 = builtin_save_expr (arg2);
3605 len = builtin_save_expr (len);
3606
3607 arg1_rtx = get_memory_rtx (arg1);
3608 arg2_rtx = get_memory_rtx (arg2);
3609 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3610 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3611 GEN_INT (MIN (arg1_align, arg2_align)));
3612 if (insn)
3613 {
3614 emit_insn (insn);
3615
3616 /* Return the value in the proper mode for this function. */
3617 mode = TYPE_MODE (TREE_TYPE (exp));
3618 if (GET_MODE (result) == mode)
3619 return result;
3620 if (target == 0)
3621 return convert_to_mode (mode, result, 0);
3622 convert_move (target, result, 0);
3623 return target;
3624 }
3625
3626 /* Expand the library call ourselves using a stabilized argument
3627 list to avoid re-evaluating the function's arguments twice. */
3628 arglist = build_tree_list (NULL_TREE, len);
3629 arglist = tree_cons (NULL_TREE, arg2, arglist);
3630 arglist = tree_cons (NULL_TREE, arg1, arglist);
3631 fndecl = get_callee_fndecl (exp);
3632 exp = build_function_call_expr (fndecl, arglist);
3633 return expand_call (exp, target, target == const0_rtx);
3634 }
3635 #endif
3636 return 0;
3637 }
3638
3639 /* Expand expression EXP, which is a call to the strcat builtin.
3640 Return 0 if we failed the caller should emit a normal call,
3641 otherwise try to get the result in TARGET, if convenient. */
3642
3643 static rtx
3644 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3645 {
3646 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3647 return 0;
3648 else
3649 {
3650 tree dst = TREE_VALUE (arglist),
3651 src = TREE_VALUE (TREE_CHAIN (arglist));
3652 const char *p = c_getstr (src);
3653
3654 if (p)
3655 {
3656 /* If the string length is zero, return the dst parameter. */
3657 if (*p == '\0')
3658 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3659 else if (!optimize_size)
3660 {
3661 /* Otherwise if !optimize_size, see if we can store by
3662 pieces into (dst + strlen(dst)). */
3663 tree newdst, arglist,
3664 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3665
3666 /* This is the length argument. */
3667 arglist = build_tree_list (NULL_TREE,
3668 fold (size_binop (PLUS_EXPR,
3669 c_strlen (src, 0),
3670 ssize_int (1))));
3671 /* Prepend src argument. */
3672 arglist = tree_cons (NULL_TREE, src, arglist);
3673
3674 /* We're going to use dst more than once. */
3675 dst = builtin_save_expr (dst);
3676
3677 /* Create strlen (dst). */
3678 newdst =
3679 fold (build_function_call_expr (strlen_fn,
3680 build_tree_list (NULL_TREE,
3681 dst)));
3682 /* Create (dst + strlen (dst)). */
3683 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3684
3685 /* Prepend the new dst argument. */
3686 arglist = tree_cons (NULL_TREE, newdst, arglist);
3687
3688 /* We don't want to get turned into a memcpy if the
3689 target is const0_rtx, i.e. when the return value
3690 isn't used. That would produce pessimized code so
3691 pass in a target of zero, it should never actually be
3692 used. If this was successful return the original
3693 dst, not the result of mempcpy. */
3694 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3695 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3696 else
3697 return 0;
3698 }
3699 }
3700
3701 return 0;
3702 }
3703 }
3704
3705 /* Expand expression EXP, which is a call to the strncat builtin.
3706 Return 0 if we failed the caller should emit a normal call,
3707 otherwise try to get the result in TARGET, if convenient. */
3708
3709 static rtx
3710 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3711 {
3712 if (validate_arglist (arglist,
3713 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3714 {
3715 tree result = fold_builtin_strncat (arglist);
3716 if (result)
3717 return expand_expr (result, target, mode, EXPAND_NORMAL);
3718 }
3719 return 0;
3720 }
3721
3722 /* Expand expression EXP, which is a call to the strspn builtin.
3723 Return 0 if we failed the caller should emit a normal call,
3724 otherwise try to get the result in TARGET, if convenient. */
3725
3726 static rtx
3727 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3728 {
3729 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3730 {
3731 tree result = fold_builtin_strspn (arglist);
3732 if (result)
3733 return expand_expr (result, target, mode, EXPAND_NORMAL);
3734 }
3735 return 0;
3736 }
3737
3738 /* Expand expression EXP, which is a call to the strcspn builtin.
3739 Return 0 if we failed the caller should emit a normal call,
3740 otherwise try to get the result in TARGET, if convenient. */
3741
3742 static rtx
3743 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3744 {
3745 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3746 {
3747 tree result = fold_builtin_strcspn (arglist);
3748 if (result)
3749 return expand_expr (result, target, mode, EXPAND_NORMAL);
3750 }
3751 return 0;
3752 }
3753
3754 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3755 if that's convenient. */
3756
3757 rtx
3758 expand_builtin_saveregs (void)
3759 {
3760 rtx val, seq;
3761
3762 /* Don't do __builtin_saveregs more than once in a function.
3763 Save the result of the first call and reuse it. */
3764 if (saveregs_value != 0)
3765 return saveregs_value;
3766
3767 /* When this function is called, it means that registers must be
3768 saved on entry to this function. So we migrate the call to the
3769 first insn of this function. */
3770
3771 start_sequence ();
3772
3773 /* Do whatever the machine needs done in this case. */
3774 val = targetm.calls.expand_builtin_saveregs ();
3775
3776 seq = get_insns ();
3777 end_sequence ();
3778
3779 saveregs_value = val;
3780
3781 /* Put the insns after the NOTE that starts the function. If this
3782 is inside a start_sequence, make the outer-level insn chain current, so
3783 the code is placed at the start of the function. */
3784 push_topmost_sequence ();
3785 emit_insn_after (seq, entry_of_function ());
3786 pop_topmost_sequence ();
3787
3788 return val;
3789 }
3790
3791 /* __builtin_args_info (N) returns word N of the arg space info
3792 for the current function. The number and meanings of words
3793 is controlled by the definition of CUMULATIVE_ARGS. */
3794
3795 static rtx
3796 expand_builtin_args_info (tree arglist)
3797 {
3798 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3799 int *word_ptr = (int *) &current_function_args_info;
3800
3801 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3802
3803 if (arglist != 0)
3804 {
3805 if (!host_integerp (TREE_VALUE (arglist), 0))
3806 error ("argument of %<__builtin_args_info%> must be constant");
3807 else
3808 {
3809 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3810
3811 if (wordnum < 0 || wordnum >= nwords)
3812 error ("argument of %<__builtin_args_info%> out of range");
3813 else
3814 return GEN_INT (word_ptr[wordnum]);
3815 }
3816 }
3817 else
3818 error ("missing argument in %<__builtin_args_info%>");
3819
3820 return const0_rtx;
3821 }
3822
3823 /* Expand a call to __builtin_next_arg. */
3824
3825 static rtx
3826 expand_builtin_next_arg (void)
3827 {
3828 /* Checking arguments is already done in fold_builtin_next_arg
3829 that must be called before this function. */
3830 return expand_binop (Pmode, add_optab,
3831 current_function_internal_arg_pointer,
3832 current_function_arg_offset_rtx,
3833 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3834 }
3835
3836 /* Make it easier for the backends by protecting the valist argument
3837 from multiple evaluations. */
3838
3839 static tree
3840 stabilize_va_list (tree valist, int needs_lvalue)
3841 {
3842 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3843 {
3844 if (TREE_SIDE_EFFECTS (valist))
3845 valist = save_expr (valist);
3846
3847 /* For this case, the backends will be expecting a pointer to
3848 TREE_TYPE (va_list_type_node), but it's possible we've
3849 actually been given an array (an actual va_list_type_node).
3850 So fix it. */
3851 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3852 {
3853 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3854 valist = build_fold_addr_expr_with_type (valist, p1);
3855 }
3856 }
3857 else
3858 {
3859 tree pt;
3860
3861 if (! needs_lvalue)
3862 {
3863 if (! TREE_SIDE_EFFECTS (valist))
3864 return valist;
3865
3866 pt = build_pointer_type (va_list_type_node);
3867 valist = fold (build1 (ADDR_EXPR, pt, valist));
3868 TREE_SIDE_EFFECTS (valist) = 1;
3869 }
3870
3871 if (TREE_SIDE_EFFECTS (valist))
3872 valist = save_expr (valist);
3873 valist = build_fold_indirect_ref (valist);
3874 }
3875
3876 return valist;
3877 }
3878
3879 /* The "standard" definition of va_list is void*. */
3880
3881 tree
3882 std_build_builtin_va_list (void)
3883 {
3884 return ptr_type_node;
3885 }
3886
3887 /* The "standard" implementation of va_start: just assign `nextarg' to
3888 the variable. */
3889
3890 void
3891 std_expand_builtin_va_start (tree valist, rtx nextarg)
3892 {
3893 tree t;
3894
3895 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
3896 make_tree (ptr_type_node, nextarg));
3897 TREE_SIDE_EFFECTS (t) = 1;
3898
3899 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3900 }
3901
3902 /* Expand ARGLIST, from a call to __builtin_va_start. */
3903
3904 static rtx
3905 expand_builtin_va_start (tree arglist)
3906 {
3907 rtx nextarg;
3908 tree chain, valist;
3909
3910 chain = TREE_CHAIN (arglist);
3911
3912 if (!chain)
3913 {
3914 error ("too few arguments to function %<va_start%>");
3915 return const0_rtx;
3916 }
3917
3918 if (fold_builtin_next_arg (chain))
3919 return const0_rtx;
3920
3921 nextarg = expand_builtin_next_arg ();
3922 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3923
3924 #ifdef EXPAND_BUILTIN_VA_START
3925 EXPAND_BUILTIN_VA_START (valist, nextarg);
3926 #else
3927 std_expand_builtin_va_start (valist, nextarg);
3928 #endif
3929
3930 return const0_rtx;
3931 }
3932
3933 /* The "standard" implementation of va_arg: read the value from the
3934 current (padded) address and increment by the (padded) size. */
3935
3936 tree
3937 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
3938 {
3939 tree addr, t, type_size, rounded_size, valist_tmp;
3940 unsigned HOST_WIDE_INT align, boundary;
3941 bool indirect;
3942
3943 #ifdef ARGS_GROW_DOWNWARD
3944 /* All of the alignment and movement below is for args-grow-up machines.
3945 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3946 implement their own specialized gimplify_va_arg_expr routines. */
3947 gcc_unreachable ();
3948 #endif
3949
3950 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3951 if (indirect)
3952 type = build_pointer_type (type);
3953
3954 align = PARM_BOUNDARY / BITS_PER_UNIT;
3955 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
3956
3957 /* Hoist the valist value into a temporary for the moment. */
3958 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
3959
3960 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
3961 requires greater alignment, we must perform dynamic alignment. */
3962 if (boundary > align)
3963 {
3964 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
3965 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3966 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
3967 gimplify_and_add (t, pre_p);
3968
3969 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
3970 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3971 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
3972 gimplify_and_add (t, pre_p);
3973 }
3974 else
3975 boundary = align;
3976
3977 /* If the actual alignment is less than the alignment of the type,
3978 adjust the type accordingly so that we don't assume strict alignment
3979 when deferencing the pointer. */
3980 boundary *= BITS_PER_UNIT;
3981 if (boundary < TYPE_ALIGN (type))
3982 {
3983 type = build_variant_type_copy (type);
3984 TYPE_ALIGN (type) = boundary;
3985 }
3986
3987 /* Compute the rounded size of the type. */
3988 type_size = size_in_bytes (type);
3989 rounded_size = round_up (type_size, align);
3990
3991 /* Reduce rounded_size so it's sharable with the postqueue. */
3992 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
3993
3994 /* Get AP. */
3995 addr = valist_tmp;
3996 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
3997 {
3998 /* Small args are padded downward. */
3999 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4000 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4001 size_binop (MINUS_EXPR, rounded_size, type_size)));
4002 t = fold_convert (TREE_TYPE (addr), t);
4003 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4004 }
4005
4006 /* Compute new value for AP. */
4007 t = fold_convert (TREE_TYPE (valist), rounded_size);
4008 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4009 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4010 gimplify_and_add (t, pre_p);
4011
4012 addr = fold_convert (build_pointer_type (type), addr);
4013
4014 if (indirect)
4015 addr = build_va_arg_indirect_ref (addr);
4016
4017 return build_va_arg_indirect_ref (addr);
4018 }
4019
4020 /* Build an indirect-ref expression over the given TREE, which represents a
4021 piece of a va_arg() expansion. */
4022 tree
4023 build_va_arg_indirect_ref (tree addr)
4024 {
4025 addr = build_fold_indirect_ref (addr);
4026
4027 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4028 mf_mark (addr);
4029
4030 return addr;
4031 }
4032
4033 /* Return a dummy expression of type TYPE in order to keep going after an
4034 error. */
4035
4036 static tree
4037 dummy_object (tree type)
4038 {
4039 tree t = convert (build_pointer_type (type), null_pointer_node);
4040 return build1 (INDIRECT_REF, type, t);
4041 }
4042
4043 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4044 builtin function, but a very special sort of operator. */
4045
4046 enum gimplify_status
4047 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4048 {
4049 tree promoted_type, want_va_type, have_va_type;
4050 tree valist = TREE_OPERAND (*expr_p, 0);
4051 tree type = TREE_TYPE (*expr_p);
4052 tree t;
4053
4054 /* Verify that valist is of the proper type. */
4055 want_va_type = va_list_type_node;
4056 have_va_type = TREE_TYPE (valist);
4057
4058 if (have_va_type == error_mark_node)
4059 return GS_ERROR;
4060
4061 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4062 {
4063 /* If va_list is an array type, the argument may have decayed
4064 to a pointer type, e.g. by being passed to another function.
4065 In that case, unwrap both types so that we can compare the
4066 underlying records. */
4067 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4068 || POINTER_TYPE_P (have_va_type))
4069 {
4070 want_va_type = TREE_TYPE (want_va_type);
4071 have_va_type = TREE_TYPE (have_va_type);
4072 }
4073 }
4074
4075 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4076 {
4077 error ("first argument to %<va_arg%> not of type %<va_list%>");
4078 return GS_ERROR;
4079 }
4080
4081 /* Generate a diagnostic for requesting data of a type that cannot
4082 be passed through `...' due to type promotion at the call site. */
4083 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4084 != type)
4085 {
4086 static bool gave_help;
4087
4088 /* Unfortunately, this is merely undefined, rather than a constraint
4089 violation, so we cannot make this an error. If this call is never
4090 executed, the program is still strictly conforming. */
4091 warning ("%qT is promoted to %qT when passed through %<...%>",
4092 type, promoted_type);
4093 if (! gave_help)
4094 {
4095 gave_help = true;
4096 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4097 promoted_type, type);
4098 }
4099
4100 /* We can, however, treat "undefined" any way we please.
4101 Call abort to encourage the user to fix the program. */
4102 inform ("if this code is reached, the program will abort");
4103 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4104 NULL);
4105 append_to_statement_list (t, pre_p);
4106
4107 /* This is dead code, but go ahead and finish so that the
4108 mode of the result comes out right. */
4109 *expr_p = dummy_object (type);
4110 return GS_ALL_DONE;
4111 }
4112 else
4113 {
4114 /* Make it easier for the backends by protecting the valist argument
4115 from multiple evaluations. */
4116 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4117 {
4118 /* For this case, the backends will be expecting a pointer to
4119 TREE_TYPE (va_list_type_node), but it's possible we've
4120 actually been given an array (an actual va_list_type_node).
4121 So fix it. */
4122 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4123 {
4124 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4125 valist = build_fold_addr_expr_with_type (valist, p1);
4126 }
4127 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4128 }
4129 else
4130 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4131
4132 if (!targetm.gimplify_va_arg_expr)
4133 /* Once most targets are converted this should abort. */
4134 return GS_ALL_DONE;
4135
4136 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4137 return GS_OK;
4138 }
4139 }
4140
4141 /* Expand ARGLIST, from a call to __builtin_va_end. */
4142
4143 static rtx
4144 expand_builtin_va_end (tree arglist)
4145 {
4146 tree valist = TREE_VALUE (arglist);
4147
4148 /* Evaluate for side effects, if needed. I hate macros that don't
4149 do that. */
4150 if (TREE_SIDE_EFFECTS (valist))
4151 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4152
4153 return const0_rtx;
4154 }
4155
4156 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4157 builtin rather than just as an assignment in stdarg.h because of the
4158 nastiness of array-type va_list types. */
4159
4160 static rtx
4161 expand_builtin_va_copy (tree arglist)
4162 {
4163 tree dst, src, t;
4164
4165 dst = TREE_VALUE (arglist);
4166 src = TREE_VALUE (TREE_CHAIN (arglist));
4167
4168 dst = stabilize_va_list (dst, 1);
4169 src = stabilize_va_list (src, 0);
4170
4171 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4172 {
4173 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4174 TREE_SIDE_EFFECTS (t) = 1;
4175 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4176 }
4177 else
4178 {
4179 rtx dstb, srcb, size;
4180
4181 /* Evaluate to pointers. */
4182 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4183 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4184 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4185 VOIDmode, EXPAND_NORMAL);
4186
4187 dstb = convert_memory_address (Pmode, dstb);
4188 srcb = convert_memory_address (Pmode, srcb);
4189
4190 /* "Dereference" to BLKmode memories. */
4191 dstb = gen_rtx_MEM (BLKmode, dstb);
4192 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4193 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4194 srcb = gen_rtx_MEM (BLKmode, srcb);
4195 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4196 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4197
4198 /* Copy. */
4199 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4200 }
4201
4202 return const0_rtx;
4203 }
4204
4205 /* Expand a call to one of the builtin functions __builtin_frame_address or
4206 __builtin_return_address. */
4207
4208 static rtx
4209 expand_builtin_frame_address (tree fndecl, tree arglist)
4210 {
4211 /* The argument must be a nonnegative integer constant.
4212 It counts the number of frames to scan up the stack.
4213 The value is the return address saved in that frame. */
4214 if (arglist == 0)
4215 /* Warning about missing arg was already issued. */
4216 return const0_rtx;
4217 else if (! host_integerp (TREE_VALUE (arglist), 1))
4218 {
4219 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4220 error ("invalid argument to %<__builtin_frame_address%>");
4221 else
4222 error ("invalid argument to %<__builtin_return_address%>");
4223 return const0_rtx;
4224 }
4225 else
4226 {
4227 rtx tem
4228 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4229 tree_low_cst (TREE_VALUE (arglist), 1));
4230
4231 /* Some ports cannot access arbitrary stack frames. */
4232 if (tem == NULL)
4233 {
4234 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4235 warning ("unsupported argument to %<__builtin_frame_address%>");
4236 else
4237 warning ("unsupported argument to %<__builtin_return_address%>");
4238 return const0_rtx;
4239 }
4240
4241 /* For __builtin_frame_address, return what we've got. */
4242 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4243 return tem;
4244
4245 if (!REG_P (tem)
4246 && ! CONSTANT_P (tem))
4247 tem = copy_to_mode_reg (Pmode, tem);
4248 return tem;
4249 }
4250 }
4251
4252 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4253 we failed and the caller should emit a normal call, otherwise try to get
4254 the result in TARGET, if convenient. */
4255
4256 static rtx
4257 expand_builtin_alloca (tree arglist, rtx target)
4258 {
4259 rtx op0;
4260 rtx result;
4261
4262 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4263 should always expand to function calls. These can be intercepted
4264 in libmudflap. */
4265 if (flag_mudflap)
4266 return 0;
4267
4268 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4269 return 0;
4270
4271 /* Compute the argument. */
4272 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4273
4274 /* Allocate the desired space. */
4275 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4276 result = convert_memory_address (ptr_mode, result);
4277
4278 return result;
4279 }
4280
4281 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4282 Return 0 if a normal call should be emitted rather than expanding the
4283 function in-line. If convenient, the result should be placed in TARGET.
4284 SUBTARGET may be used as the target for computing one of EXP's operands. */
4285
4286 static rtx
4287 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4288 rtx subtarget, optab op_optab)
4289 {
4290 rtx op0;
4291 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4292 return 0;
4293
4294 /* Compute the argument. */
4295 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4296 /* Compute op, into TARGET if possible.
4297 Set TARGET to wherever the result comes back. */
4298 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4299 op_optab, op0, target, 1);
4300 gcc_assert (target);
4301
4302 return convert_to_mode (target_mode, target, 0);
4303 }
4304
4305 /* If the string passed to fputs is a constant and is one character
4306 long, we attempt to transform this call into __builtin_fputc(). */
4307
4308 static rtx
4309 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4310 {
4311 /* Verify the arguments in the original call. */
4312 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4313 {
4314 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4315 unlocked, NULL_TREE);
4316 if (result)
4317 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4318 }
4319 return 0;
4320 }
4321
4322 /* Expand a call to __builtin_expect. We return our argument and emit a
4323 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4324 a non-jump context. */
4325
4326 static rtx
4327 expand_builtin_expect (tree arglist, rtx target)
4328 {
4329 tree exp, c;
4330 rtx note, rtx_c;
4331
4332 if (arglist == NULL_TREE
4333 || TREE_CHAIN (arglist) == NULL_TREE)
4334 return const0_rtx;
4335 exp = TREE_VALUE (arglist);
4336 c = TREE_VALUE (TREE_CHAIN (arglist));
4337
4338 if (TREE_CODE (c) != INTEGER_CST)
4339 {
4340 error ("second argument to %<__builtin_expect%> must be a constant");
4341 c = integer_zero_node;
4342 }
4343
4344 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4345
4346 /* Don't bother with expected value notes for integral constants. */
4347 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4348 {
4349 /* We do need to force this into a register so that we can be
4350 moderately sure to be able to correctly interpret the branch
4351 condition later. */
4352 target = force_reg (GET_MODE (target), target);
4353
4354 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4355
4356 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4357 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4358 }
4359
4360 return target;
4361 }
4362
4363 /* Like expand_builtin_expect, except do this in a jump context. This is
4364 called from do_jump if the conditional is a __builtin_expect. Return either
4365 a list of insns to emit the jump or NULL if we cannot optimize
4366 __builtin_expect. We need to optimize this at jump time so that machines
4367 like the PowerPC don't turn the test into a SCC operation, and then jump
4368 based on the test being 0/1. */
4369
4370 rtx
4371 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4372 {
4373 tree arglist = TREE_OPERAND (exp, 1);
4374 tree arg0 = TREE_VALUE (arglist);
4375 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4376 rtx ret = NULL_RTX;
4377
4378 /* Only handle __builtin_expect (test, 0) and
4379 __builtin_expect (test, 1). */
4380 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4381 && (integer_zerop (arg1) || integer_onep (arg1)))
4382 {
4383 rtx insn, drop_through_label, temp;
4384
4385 /* Expand the jump insns. */
4386 start_sequence ();
4387 do_jump (arg0, if_false_label, if_true_label);
4388 ret = get_insns ();
4389
4390 drop_through_label = get_last_insn ();
4391 if (drop_through_label && NOTE_P (drop_through_label))
4392 drop_through_label = prev_nonnote_insn (drop_through_label);
4393 if (drop_through_label && !LABEL_P (drop_through_label))
4394 drop_through_label = NULL_RTX;
4395 end_sequence ();
4396
4397 if (! if_true_label)
4398 if_true_label = drop_through_label;
4399 if (! if_false_label)
4400 if_false_label = drop_through_label;
4401
4402 /* Go through and add the expect's to each of the conditional jumps. */
4403 insn = ret;
4404 while (insn != NULL_RTX)
4405 {
4406 rtx next = NEXT_INSN (insn);
4407
4408 if (JUMP_P (insn) && any_condjump_p (insn))
4409 {
4410 rtx ifelse = SET_SRC (pc_set (insn));
4411 rtx then_dest = XEXP (ifelse, 1);
4412 rtx else_dest = XEXP (ifelse, 2);
4413 int taken = -1;
4414
4415 /* First check if we recognize any of the labels. */
4416 if (GET_CODE (then_dest) == LABEL_REF
4417 && XEXP (then_dest, 0) == if_true_label)
4418 taken = 1;
4419 else if (GET_CODE (then_dest) == LABEL_REF
4420 && XEXP (then_dest, 0) == if_false_label)
4421 taken = 0;
4422 else if (GET_CODE (else_dest) == LABEL_REF
4423 && XEXP (else_dest, 0) == if_false_label)
4424 taken = 1;
4425 else if (GET_CODE (else_dest) == LABEL_REF
4426 && XEXP (else_dest, 0) == if_true_label)
4427 taken = 0;
4428 /* Otherwise check where we drop through. */
4429 else if (else_dest == pc_rtx)
4430 {
4431 if (next && NOTE_P (next))
4432 next = next_nonnote_insn (next);
4433
4434 if (next && JUMP_P (next)
4435 && any_uncondjump_p (next))
4436 temp = XEXP (SET_SRC (pc_set (next)), 0);
4437 else
4438 temp = next;
4439
4440 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4441 else that can't possibly match either target label. */
4442 if (temp == if_false_label)
4443 taken = 1;
4444 else if (temp == if_true_label)
4445 taken = 0;
4446 }
4447 else if (then_dest == pc_rtx)
4448 {
4449 if (next && NOTE_P (next))
4450 next = next_nonnote_insn (next);
4451
4452 if (next && JUMP_P (next)
4453 && any_uncondjump_p (next))
4454 temp = XEXP (SET_SRC (pc_set (next)), 0);
4455 else
4456 temp = next;
4457
4458 if (temp == if_false_label)
4459 taken = 0;
4460 else if (temp == if_true_label)
4461 taken = 1;
4462 }
4463
4464 if (taken != -1)
4465 {
4466 /* If the test is expected to fail, reverse the
4467 probabilities. */
4468 if (integer_zerop (arg1))
4469 taken = 1 - taken;
4470 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4471 }
4472 }
4473
4474 insn = next;
4475 }
4476 }
4477
4478 return ret;
4479 }
4480
4481 static void
4482 expand_builtin_trap (void)
4483 {
4484 #ifdef HAVE_trap
4485 if (HAVE_trap)
4486 emit_insn (gen_trap ());
4487 else
4488 #endif
4489 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4490 emit_barrier ();
4491 }
4492
4493 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4494 Return 0 if a normal call should be emitted rather than expanding
4495 the function inline. If convenient, the result should be placed
4496 in TARGET. SUBTARGET may be used as the target for computing
4497 the operand. */
4498
4499 static rtx
4500 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4501 {
4502 enum machine_mode mode;
4503 tree arg;
4504 rtx op0;
4505
4506 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4507 return 0;
4508
4509 arg = TREE_VALUE (arglist);
4510 mode = TYPE_MODE (TREE_TYPE (arg));
4511 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4512 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4513 }
4514
4515 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4516 Return NULL is a normal call should be emitted rather than expanding the
4517 function inline. If convenient, the result should be placed in TARGET.
4518 SUBTARGET may be used as the target for computing the operand. */
4519
4520 static rtx
4521 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4522 {
4523 rtx op0, op1;
4524 tree arg;
4525
4526 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4527 return 0;
4528
4529 arg = TREE_VALUE (arglist);
4530 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4531
4532 arg = TREE_VALUE (TREE_CHAIN (arglist));
4533 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4534
4535 return expand_copysign (op0, op1, target);
4536 }
4537
4538 /* Create a new constant string literal and return a char* pointer to it.
4539 The STRING_CST value is the LEN characters at STR. */
4540 static tree
4541 build_string_literal (int len, const char *str)
4542 {
4543 tree t, elem, index, type;
4544
4545 t = build_string (len, str);
4546 elem = build_type_variant (char_type_node, 1, 0);
4547 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4548 type = build_array_type (elem, index);
4549 TREE_TYPE (t) = type;
4550 TREE_CONSTANT (t) = 1;
4551 TREE_INVARIANT (t) = 1;
4552 TREE_READONLY (t) = 1;
4553 TREE_STATIC (t) = 1;
4554
4555 type = build_pointer_type (type);
4556 t = build1 (ADDR_EXPR, type, t);
4557
4558 type = build_pointer_type (elem);
4559 t = build1 (NOP_EXPR, type, t);
4560 return t;
4561 }
4562
4563 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4564 Return 0 if a normal call should be emitted rather than transforming
4565 the function inline. If convenient, the result should be placed in
4566 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4567 call. */
4568 static rtx
4569 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4570 bool unlocked)
4571 {
4572 tree fn_putchar = unlocked
4573 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4574 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4575 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4576 : implicit_built_in_decls[BUILT_IN_PUTS];
4577 const char *fmt_str;
4578 tree fn, fmt, arg;
4579
4580 /* If the return value is used, don't do the transformation. */
4581 if (target != const0_rtx)
4582 return 0;
4583
4584 /* Verify the required arguments in the original call. */
4585 if (! arglist)
4586 return 0;
4587 fmt = TREE_VALUE (arglist);
4588 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4589 return 0;
4590 arglist = TREE_CHAIN (arglist);
4591
4592 /* Check whether the format is a literal string constant. */
4593 fmt_str = c_getstr (fmt);
4594 if (fmt_str == NULL)
4595 return 0;
4596
4597 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4598 if (strcmp (fmt_str, "%s\n") == 0)
4599 {
4600 if (! arglist
4601 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4602 || TREE_CHAIN (arglist))
4603 return 0;
4604 fn = fn_puts;
4605 }
4606 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4607 else if (strcmp (fmt_str, "%c") == 0)
4608 {
4609 if (! arglist
4610 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4611 || TREE_CHAIN (arglist))
4612 return 0;
4613 fn = fn_putchar;
4614 }
4615 else
4616 {
4617 /* We can't handle anything else with % args or %% ... yet. */
4618 if (strchr (fmt_str, '%'))
4619 return 0;
4620
4621 if (arglist)
4622 return 0;
4623
4624 /* If the format specifier was "", printf does nothing. */
4625 if (fmt_str[0] == '\0')
4626 return const0_rtx;
4627 /* If the format specifier has length of 1, call putchar. */
4628 if (fmt_str[1] == '\0')
4629 {
4630 /* Given printf("c"), (where c is any one character,)
4631 convert "c"[0] to an int and pass that to the replacement
4632 function. */
4633 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4634 arglist = build_tree_list (NULL_TREE, arg);
4635 fn = fn_putchar;
4636 }
4637 else
4638 {
4639 /* If the format specifier was "string\n", call puts("string"). */
4640 size_t len = strlen (fmt_str);
4641 if (fmt_str[len - 1] == '\n')
4642 {
4643 /* Create a NUL-terminated string that's one char shorter
4644 than the original, stripping off the trailing '\n'. */
4645 char *newstr = alloca (len);
4646 memcpy (newstr, fmt_str, len - 1);
4647 newstr[len - 1] = 0;
4648
4649 arg = build_string_literal (len, newstr);
4650 arglist = build_tree_list (NULL_TREE, arg);
4651 fn = fn_puts;
4652 }
4653 else
4654 /* We'd like to arrange to call fputs(string,stdout) here,
4655 but we need stdout and don't have a way to get it yet. */
4656 return 0;
4657 }
4658 }
4659
4660 if (!fn)
4661 return 0;
4662 return expand_expr (build_function_call_expr (fn, arglist),
4663 target, mode, EXPAND_NORMAL);
4664 }
4665
4666 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4667 Return 0 if a normal call should be emitted rather than transforming
4668 the function inline. If convenient, the result should be placed in
4669 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4670 call. */
4671 static rtx
4672 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4673 bool unlocked)
4674 {
4675 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4676 : implicit_built_in_decls[BUILT_IN_FPUTC];
4677 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4678 : implicit_built_in_decls[BUILT_IN_FPUTS];
4679 const char *fmt_str;
4680 tree fn, fmt, fp, arg;
4681
4682 /* If the return value is used, don't do the transformation. */
4683 if (target != const0_rtx)
4684 return 0;
4685
4686 /* Verify the required arguments in the original call. */
4687 if (! arglist)
4688 return 0;
4689 fp = TREE_VALUE (arglist);
4690 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4691 return 0;
4692 arglist = TREE_CHAIN (arglist);
4693 if (! arglist)
4694 return 0;
4695 fmt = TREE_VALUE (arglist);
4696 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4697 return 0;
4698 arglist = TREE_CHAIN (arglist);
4699
4700 /* Check whether the format is a literal string constant. */
4701 fmt_str = c_getstr (fmt);
4702 if (fmt_str == NULL)
4703 return 0;
4704
4705 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4706 if (strcmp (fmt_str, "%s") == 0)
4707 {
4708 if (! arglist
4709 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4710 || TREE_CHAIN (arglist))
4711 return 0;
4712 arg = TREE_VALUE (arglist);
4713 arglist = build_tree_list (NULL_TREE, fp);
4714 arglist = tree_cons (NULL_TREE, arg, arglist);
4715 fn = fn_fputs;
4716 }
4717 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4718 else if (strcmp (fmt_str, "%c") == 0)
4719 {
4720 if (! arglist
4721 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4722 || TREE_CHAIN (arglist))
4723 return 0;
4724 arg = TREE_VALUE (arglist);
4725 arglist = build_tree_list (NULL_TREE, fp);
4726 arglist = tree_cons (NULL_TREE, arg, arglist);
4727 fn = fn_fputc;
4728 }
4729 else
4730 {
4731 /* We can't handle anything else with % args or %% ... yet. */
4732 if (strchr (fmt_str, '%'))
4733 return 0;
4734
4735 if (arglist)
4736 return 0;
4737
4738 /* If the format specifier was "", fprintf does nothing. */
4739 if (fmt_str[0] == '\0')
4740 {
4741 /* Evaluate and ignore FILE* argument for side-effects. */
4742 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4743 return const0_rtx;
4744 }
4745
4746 /* When "string" doesn't contain %, replace all cases of
4747 fprintf(stream,string) with fputs(string,stream). The fputs
4748 builtin will take care of special cases like length == 1. */
4749 arglist = build_tree_list (NULL_TREE, fp);
4750 arglist = tree_cons (NULL_TREE, fmt, arglist);
4751 fn = fn_fputs;
4752 }
4753
4754 if (!fn)
4755 return 0;
4756 return expand_expr (build_function_call_expr (fn, arglist),
4757 target, mode, EXPAND_NORMAL);
4758 }
4759
4760 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4761 a normal call should be emitted rather than expanding the function
4762 inline. If convenient, the result should be placed in TARGET with
4763 mode MODE. */
4764
4765 static rtx
4766 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4767 {
4768 tree orig_arglist, dest, fmt;
4769 const char *fmt_str;
4770
4771 orig_arglist = arglist;
4772
4773 /* Verify the required arguments in the original call. */
4774 if (! arglist)
4775 return 0;
4776 dest = TREE_VALUE (arglist);
4777 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4778 return 0;
4779 arglist = TREE_CHAIN (arglist);
4780 if (! arglist)
4781 return 0;
4782 fmt = TREE_VALUE (arglist);
4783 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4784 return 0;
4785 arglist = TREE_CHAIN (arglist);
4786
4787 /* Check whether the format is a literal string constant. */
4788 fmt_str = c_getstr (fmt);
4789 if (fmt_str == NULL)
4790 return 0;
4791
4792 /* If the format doesn't contain % args or %%, use strcpy. */
4793 if (strchr (fmt_str, '%') == 0)
4794 {
4795 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4796 tree exp;
4797
4798 if (arglist || ! fn)
4799 return 0;
4800 expand_expr (build_function_call_expr (fn, orig_arglist),
4801 const0_rtx, VOIDmode, EXPAND_NORMAL);
4802 if (target == const0_rtx)
4803 return const0_rtx;
4804 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4805 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4806 }
4807 /* If the format is "%s", use strcpy if the result isn't used. */
4808 else if (strcmp (fmt_str, "%s") == 0)
4809 {
4810 tree fn, arg, len;
4811 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4812
4813 if (! fn)
4814 return 0;
4815
4816 if (! arglist || TREE_CHAIN (arglist))
4817 return 0;
4818 arg = TREE_VALUE (arglist);
4819 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4820 return 0;
4821
4822 if (target != const0_rtx)
4823 {
4824 len = c_strlen (arg, 1);
4825 if (! len || TREE_CODE (len) != INTEGER_CST)
4826 return 0;
4827 }
4828 else
4829 len = NULL_TREE;
4830
4831 arglist = build_tree_list (NULL_TREE, arg);
4832 arglist = tree_cons (NULL_TREE, dest, arglist);
4833 expand_expr (build_function_call_expr (fn, arglist),
4834 const0_rtx, VOIDmode, EXPAND_NORMAL);
4835
4836 if (target == const0_rtx)
4837 return const0_rtx;
4838 return expand_expr (len, target, mode, EXPAND_NORMAL);
4839 }
4840
4841 return 0;
4842 }
4843
4844 /* Expand a call to either the entry or exit function profiler. */
4845
4846 static rtx
4847 expand_builtin_profile_func (bool exitp)
4848 {
4849 rtx this, which;
4850
4851 this = DECL_RTL (current_function_decl);
4852 gcc_assert (MEM_P (this));
4853 this = XEXP (this, 0);
4854
4855 if (exitp)
4856 which = profile_function_exit_libfunc;
4857 else
4858 which = profile_function_entry_libfunc;
4859
4860 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4861 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4862 0),
4863 Pmode);
4864
4865 return const0_rtx;
4866 }
4867
4868 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4869
4870 static rtx
4871 round_trampoline_addr (rtx tramp)
4872 {
4873 rtx temp, addend, mask;
4874
4875 /* If we don't need too much alignment, we'll have been guaranteed
4876 proper alignment by get_trampoline_type. */
4877 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4878 return tramp;
4879
4880 /* Round address up to desired boundary. */
4881 temp = gen_reg_rtx (Pmode);
4882 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4883 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4884
4885 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4886 temp, 0, OPTAB_LIB_WIDEN);
4887 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4888 temp, 0, OPTAB_LIB_WIDEN);
4889
4890 return tramp;
4891 }
4892
4893 static rtx
4894 expand_builtin_init_trampoline (tree arglist)
4895 {
4896 tree t_tramp, t_func, t_chain;
4897 rtx r_tramp, r_func, r_chain;
4898 #ifdef TRAMPOLINE_TEMPLATE
4899 rtx blktramp;
4900 #endif
4901
4902 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
4903 POINTER_TYPE, VOID_TYPE))
4904 return NULL_RTX;
4905
4906 t_tramp = TREE_VALUE (arglist);
4907 arglist = TREE_CHAIN (arglist);
4908 t_func = TREE_VALUE (arglist);
4909 arglist = TREE_CHAIN (arglist);
4910 t_chain = TREE_VALUE (arglist);
4911
4912 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
4913 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
4914 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
4915
4916 /* Generate insns to initialize the trampoline. */
4917 r_tramp = round_trampoline_addr (r_tramp);
4918 #ifdef TRAMPOLINE_TEMPLATE
4919 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
4920 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
4921 emit_block_move (blktramp, assemble_trampoline_template (),
4922 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4923 #endif
4924 trampolines_created = 1;
4925 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
4926
4927 return const0_rtx;
4928 }
4929
4930 static rtx
4931 expand_builtin_adjust_trampoline (tree arglist)
4932 {
4933 rtx tramp;
4934
4935 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4936 return NULL_RTX;
4937
4938 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4939 tramp = round_trampoline_addr (tramp);
4940 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4941 TRAMPOLINE_ADJUST_ADDRESS (tramp);
4942 #endif
4943
4944 return tramp;
4945 }
4946
4947 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4948 Return NULL_RTX if a normal call should be emitted rather than expanding
4949 the function in-line. EXP is the expression that is a call to the builtin
4950 function; if convenient, the result should be placed in TARGET. */
4951
4952 static rtx
4953 expand_builtin_signbit (tree exp, rtx target)
4954 {
4955 const struct real_format *fmt;
4956 enum machine_mode fmode, imode, rmode;
4957 HOST_WIDE_INT hi, lo;
4958 tree arg, arglist;
4959 int bitpos;
4960 rtx temp;
4961
4962 arglist = TREE_OPERAND (exp, 1);
4963 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4964 return 0;
4965
4966 arg = TREE_VALUE (arglist);
4967 fmode = TYPE_MODE (TREE_TYPE (arg));
4968 rmode = TYPE_MODE (TREE_TYPE (exp));
4969 fmt = REAL_MODE_FORMAT (fmode);
4970
4971 /* For floating point formats without a sign bit, implement signbit
4972 as "ARG < 0.0". */
4973 if (fmt->signbit < 0)
4974 {
4975 /* But we can't do this if the format supports signed zero. */
4976 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4977 return 0;
4978
4979 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
4980 build_real (TREE_TYPE (arg), dconst0)));
4981 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4982 }
4983
4984 imode = int_mode_for_mode (fmode);
4985 if (imode == BLKmode)
4986 return 0;
4987
4988 bitpos = fmt->signbit;
4989 /* Handle targets with different FP word orders. */
4990 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4991 {
4992 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
4993 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
4994 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
4995 }
4996
4997 /* If the sign bit is not in the lowpart and the floating point format
4998 is wider than an integer, check that is twice the size of an integer
4999 so that we can use gen_highpart below. */
5000 if (bitpos >= GET_MODE_BITSIZE (rmode)
5001 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5002 return 0;
5003
5004 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5005 temp = gen_lowpart (imode, temp);
5006
5007 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
5008 {
5009 if (BYTES_BIG_ENDIAN)
5010 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5011 temp = copy_to_mode_reg (imode, temp);
5012 temp = extract_bit_field (temp, 1, bitpos, 1,
5013 NULL_RTX, rmode, rmode);
5014 }
5015 else
5016 {
5017 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5018 temp = gen_lowpart (rmode, temp);
5019 if (bitpos < HOST_BITS_PER_WIDE_INT)
5020 {
5021 hi = 0;
5022 lo = (HOST_WIDE_INT) 1 << bitpos;
5023 }
5024 else
5025 {
5026 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5027 lo = 0;
5028 }
5029
5030 temp = force_reg (rmode, temp);
5031 temp = expand_binop (rmode, and_optab, temp,
5032 immed_double_const (lo, hi, rmode),
5033 target, 1, OPTAB_LIB_WIDEN);
5034 }
5035 return temp;
5036 }
5037
5038 /* Expand fork or exec calls. TARGET is the desired target of the
5039 call. ARGLIST is the list of arguments of the call. FN is the
5040 identificator of the actual function. IGNORE is nonzero if the
5041 value is to be ignored. */
5042
5043 static rtx
5044 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5045 {
5046 tree id, decl;
5047 tree call;
5048
5049 /* If we are not profiling, just call the function. */
5050 if (!profile_arc_flag)
5051 return NULL_RTX;
5052
5053 /* Otherwise call the wrapper. This should be equivalent for the rest of
5054 compiler, so the code does not diverge, and the wrapper may run the
5055 code necessary for keeping the profiling sane. */
5056
5057 switch (DECL_FUNCTION_CODE (fn))
5058 {
5059 case BUILT_IN_FORK:
5060 id = get_identifier ("__gcov_fork");
5061 break;
5062
5063 case BUILT_IN_EXECL:
5064 id = get_identifier ("__gcov_execl");
5065 break;
5066
5067 case BUILT_IN_EXECV:
5068 id = get_identifier ("__gcov_execv");
5069 break;
5070
5071 case BUILT_IN_EXECLP:
5072 id = get_identifier ("__gcov_execlp");
5073 break;
5074
5075 case BUILT_IN_EXECLE:
5076 id = get_identifier ("__gcov_execle");
5077 break;
5078
5079 case BUILT_IN_EXECVP:
5080 id = get_identifier ("__gcov_execvp");
5081 break;
5082
5083 case BUILT_IN_EXECVE:
5084 id = get_identifier ("__gcov_execve");
5085 break;
5086
5087 default:
5088 gcc_unreachable ();
5089 }
5090
5091 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5092 DECL_EXTERNAL (decl) = 1;
5093 TREE_PUBLIC (decl) = 1;
5094 DECL_ARTIFICIAL (decl) = 1;
5095 TREE_NOTHROW (decl) = 1;
5096 call = build_function_call_expr (decl, arglist);
5097
5098 return expand_call (call, target, ignore);
5099 }
5100 \f
5101 /* Expand an expression EXP that calls a built-in function,
5102 with result going to TARGET if that's convenient
5103 (and in mode MODE if that's convenient).
5104 SUBTARGET may be used as the target for computing one of EXP's operands.
5105 IGNORE is nonzero if the value is to be ignored. */
5106
5107 rtx
5108 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5109 int ignore)
5110 {
5111 tree fndecl = get_callee_fndecl (exp);
5112 tree arglist = TREE_OPERAND (exp, 1);
5113 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5114 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5115
5116 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5117 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5118
5119 /* When not optimizing, generate calls to library functions for a certain
5120 set of builtins. */
5121 if (!optimize
5122 && !CALLED_AS_BUILT_IN (fndecl)
5123 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5124 && fcode != BUILT_IN_ALLOCA)
5125 return expand_call (exp, target, ignore);
5126
5127 /* The built-in function expanders test for target == const0_rtx
5128 to determine whether the function's result will be ignored. */
5129 if (ignore)
5130 target = const0_rtx;
5131
5132 /* If the result of a pure or const built-in function is ignored, and
5133 none of its arguments are volatile, we can avoid expanding the
5134 built-in call and just evaluate the arguments for side-effects. */
5135 if (target == const0_rtx
5136 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5137 {
5138 bool volatilep = false;
5139 tree arg;
5140
5141 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5142 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5143 {
5144 volatilep = true;
5145 break;
5146 }
5147
5148 if (! volatilep)
5149 {
5150 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5151 expand_expr (TREE_VALUE (arg), const0_rtx,
5152 VOIDmode, EXPAND_NORMAL);
5153 return const0_rtx;
5154 }
5155 }
5156
5157 switch (fcode)
5158 {
5159 case BUILT_IN_FABS:
5160 case BUILT_IN_FABSF:
5161 case BUILT_IN_FABSL:
5162 target = expand_builtin_fabs (arglist, target, subtarget);
5163 if (target)
5164 return target;
5165 break;
5166
5167 case BUILT_IN_COPYSIGN:
5168 case BUILT_IN_COPYSIGNF:
5169 case BUILT_IN_COPYSIGNL:
5170 target = expand_builtin_copysign (arglist, target, subtarget);
5171 if (target)
5172 return target;
5173 break;
5174
5175 /* Just do a normal library call if we were unable to fold
5176 the values. */
5177 case BUILT_IN_CABS:
5178 case BUILT_IN_CABSF:
5179 case BUILT_IN_CABSL:
5180 break;
5181
5182 case BUILT_IN_EXP:
5183 case BUILT_IN_EXPF:
5184 case BUILT_IN_EXPL:
5185 case BUILT_IN_EXP10:
5186 case BUILT_IN_EXP10F:
5187 case BUILT_IN_EXP10L:
5188 case BUILT_IN_POW10:
5189 case BUILT_IN_POW10F:
5190 case BUILT_IN_POW10L:
5191 case BUILT_IN_EXP2:
5192 case BUILT_IN_EXP2F:
5193 case BUILT_IN_EXP2L:
5194 case BUILT_IN_EXPM1:
5195 case BUILT_IN_EXPM1F:
5196 case BUILT_IN_EXPM1L:
5197 case BUILT_IN_LOGB:
5198 case BUILT_IN_LOGBF:
5199 case BUILT_IN_LOGBL:
5200 case BUILT_IN_ILOGB:
5201 case BUILT_IN_ILOGBF:
5202 case BUILT_IN_ILOGBL:
5203 case BUILT_IN_LOG:
5204 case BUILT_IN_LOGF:
5205 case BUILT_IN_LOGL:
5206 case BUILT_IN_LOG10:
5207 case BUILT_IN_LOG10F:
5208 case BUILT_IN_LOG10L:
5209 case BUILT_IN_LOG2:
5210 case BUILT_IN_LOG2F:
5211 case BUILT_IN_LOG2L:
5212 case BUILT_IN_LOG1P:
5213 case BUILT_IN_LOG1PF:
5214 case BUILT_IN_LOG1PL:
5215 case BUILT_IN_TAN:
5216 case BUILT_IN_TANF:
5217 case BUILT_IN_TANL:
5218 case BUILT_IN_ASIN:
5219 case BUILT_IN_ASINF:
5220 case BUILT_IN_ASINL:
5221 case BUILT_IN_ACOS:
5222 case BUILT_IN_ACOSF:
5223 case BUILT_IN_ACOSL:
5224 case BUILT_IN_ATAN:
5225 case BUILT_IN_ATANF:
5226 case BUILT_IN_ATANL:
5227 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5228 because of possible accuracy problems. */
5229 if (! flag_unsafe_math_optimizations)
5230 break;
5231 case BUILT_IN_SQRT:
5232 case BUILT_IN_SQRTF:
5233 case BUILT_IN_SQRTL:
5234 case BUILT_IN_FLOOR:
5235 case BUILT_IN_FLOORF:
5236 case BUILT_IN_FLOORL:
5237 case BUILT_IN_CEIL:
5238 case BUILT_IN_CEILF:
5239 case BUILT_IN_CEILL:
5240 case BUILT_IN_TRUNC:
5241 case BUILT_IN_TRUNCF:
5242 case BUILT_IN_TRUNCL:
5243 case BUILT_IN_ROUND:
5244 case BUILT_IN_ROUNDF:
5245 case BUILT_IN_ROUNDL:
5246 case BUILT_IN_NEARBYINT:
5247 case BUILT_IN_NEARBYINTF:
5248 case BUILT_IN_NEARBYINTL:
5249 case BUILT_IN_RINT:
5250 case BUILT_IN_RINTF:
5251 case BUILT_IN_RINTL:
5252 target = expand_builtin_mathfn (exp, target, subtarget);
5253 if (target)
5254 return target;
5255 break;
5256
5257 case BUILT_IN_POW:
5258 case BUILT_IN_POWF:
5259 case BUILT_IN_POWL:
5260 target = expand_builtin_pow (exp, target, subtarget);
5261 if (target)
5262 return target;
5263 break;
5264
5265 case BUILT_IN_POWI:
5266 case BUILT_IN_POWIF:
5267 case BUILT_IN_POWIL:
5268 target = expand_builtin_powi (exp, target, subtarget);
5269 if (target)
5270 return target;
5271 break;
5272
5273 case BUILT_IN_ATAN2:
5274 case BUILT_IN_ATAN2F:
5275 case BUILT_IN_ATAN2L:
5276 case BUILT_IN_LDEXP:
5277 case BUILT_IN_LDEXPF:
5278 case BUILT_IN_LDEXPL:
5279 case BUILT_IN_FMOD:
5280 case BUILT_IN_FMODF:
5281 case BUILT_IN_FMODL:
5282 case BUILT_IN_DREM:
5283 case BUILT_IN_DREMF:
5284 case BUILT_IN_DREML:
5285 if (! flag_unsafe_math_optimizations)
5286 break;
5287 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5288 if (target)
5289 return target;
5290 break;
5291
5292 case BUILT_IN_SIN:
5293 case BUILT_IN_SINF:
5294 case BUILT_IN_SINL:
5295 case BUILT_IN_COS:
5296 case BUILT_IN_COSF:
5297 case BUILT_IN_COSL:
5298 if (! flag_unsafe_math_optimizations)
5299 break;
5300 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5301 if (target)
5302 return target;
5303 break;
5304
5305 case BUILT_IN_APPLY_ARGS:
5306 return expand_builtin_apply_args ();
5307
5308 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5309 FUNCTION with a copy of the parameters described by
5310 ARGUMENTS, and ARGSIZE. It returns a block of memory
5311 allocated on the stack into which is stored all the registers
5312 that might possibly be used for returning the result of a
5313 function. ARGUMENTS is the value returned by
5314 __builtin_apply_args. ARGSIZE is the number of bytes of
5315 arguments that must be copied. ??? How should this value be
5316 computed? We'll also need a safe worst case value for varargs
5317 functions. */
5318 case BUILT_IN_APPLY:
5319 if (!validate_arglist (arglist, POINTER_TYPE,
5320 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5321 && !validate_arglist (arglist, REFERENCE_TYPE,
5322 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5323 return const0_rtx;
5324 else
5325 {
5326 int i;
5327 tree t;
5328 rtx ops[3];
5329
5330 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5331 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5332
5333 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5334 }
5335
5336 /* __builtin_return (RESULT) causes the function to return the
5337 value described by RESULT. RESULT is address of the block of
5338 memory returned by __builtin_apply. */
5339 case BUILT_IN_RETURN:
5340 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5341 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5342 NULL_RTX, VOIDmode, 0));
5343 return const0_rtx;
5344
5345 case BUILT_IN_SAVEREGS:
5346 return expand_builtin_saveregs ();
5347
5348 case BUILT_IN_ARGS_INFO:
5349 return expand_builtin_args_info (arglist);
5350
5351 /* Return the address of the first anonymous stack arg. */
5352 case BUILT_IN_NEXT_ARG:
5353 if (fold_builtin_next_arg (arglist))
5354 return const0_rtx;
5355 return expand_builtin_next_arg ();
5356
5357 case BUILT_IN_CLASSIFY_TYPE:
5358 return expand_builtin_classify_type (arglist);
5359
5360 case BUILT_IN_CONSTANT_P:
5361 return const0_rtx;
5362
5363 case BUILT_IN_FRAME_ADDRESS:
5364 case BUILT_IN_RETURN_ADDRESS:
5365 return expand_builtin_frame_address (fndecl, arglist);
5366
5367 /* Returns the address of the area where the structure is returned.
5368 0 otherwise. */
5369 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5370 if (arglist != 0
5371 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5372 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5373 return const0_rtx;
5374 else
5375 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5376
5377 case BUILT_IN_ALLOCA:
5378 target = expand_builtin_alloca (arglist, target);
5379 if (target)
5380 return target;
5381 break;
5382
5383 case BUILT_IN_STACK_SAVE:
5384 return expand_stack_save ();
5385
5386 case BUILT_IN_STACK_RESTORE:
5387 expand_stack_restore (TREE_VALUE (arglist));
5388 return const0_rtx;
5389
5390 case BUILT_IN_FFS:
5391 case BUILT_IN_FFSL:
5392 case BUILT_IN_FFSLL:
5393 case BUILT_IN_FFSIMAX:
5394 target = expand_builtin_unop (target_mode, arglist, target,
5395 subtarget, ffs_optab);
5396 if (target)
5397 return target;
5398 break;
5399
5400 case BUILT_IN_CLZ:
5401 case BUILT_IN_CLZL:
5402 case BUILT_IN_CLZLL:
5403 case BUILT_IN_CLZIMAX:
5404 target = expand_builtin_unop (target_mode, arglist, target,
5405 subtarget, clz_optab);
5406 if (target)
5407 return target;
5408 break;
5409
5410 case BUILT_IN_CTZ:
5411 case BUILT_IN_CTZL:
5412 case BUILT_IN_CTZLL:
5413 case BUILT_IN_CTZIMAX:
5414 target = expand_builtin_unop (target_mode, arglist, target,
5415 subtarget, ctz_optab);
5416 if (target)
5417 return target;
5418 break;
5419
5420 case BUILT_IN_POPCOUNT:
5421 case BUILT_IN_POPCOUNTL:
5422 case BUILT_IN_POPCOUNTLL:
5423 case BUILT_IN_POPCOUNTIMAX:
5424 target = expand_builtin_unop (target_mode, arglist, target,
5425 subtarget, popcount_optab);
5426 if (target)
5427 return target;
5428 break;
5429
5430 case BUILT_IN_PARITY:
5431 case BUILT_IN_PARITYL:
5432 case BUILT_IN_PARITYLL:
5433 case BUILT_IN_PARITYIMAX:
5434 target = expand_builtin_unop (target_mode, arglist, target,
5435 subtarget, parity_optab);
5436 if (target)
5437 return target;
5438 break;
5439
5440 case BUILT_IN_STRLEN:
5441 target = expand_builtin_strlen (arglist, target, target_mode);
5442 if (target)
5443 return target;
5444 break;
5445
5446 case BUILT_IN_STRCPY:
5447 target = expand_builtin_strcpy (exp, target, mode);
5448 if (target)
5449 return target;
5450 break;
5451
5452 case BUILT_IN_STRNCPY:
5453 target = expand_builtin_strncpy (exp, target, mode);
5454 if (target)
5455 return target;
5456 break;
5457
5458 case BUILT_IN_STPCPY:
5459 target = expand_builtin_stpcpy (exp, target, mode);
5460 if (target)
5461 return target;
5462 break;
5463
5464 case BUILT_IN_STRCAT:
5465 target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5466 if (target)
5467 return target;
5468 break;
5469
5470 case BUILT_IN_STRNCAT:
5471 target = expand_builtin_strncat (arglist, target, mode);
5472 if (target)
5473 return target;
5474 break;
5475
5476 case BUILT_IN_STRSPN:
5477 target = expand_builtin_strspn (arglist, target, mode);
5478 if (target)
5479 return target;
5480 break;
5481
5482 case BUILT_IN_STRCSPN:
5483 target = expand_builtin_strcspn (arglist, target, mode);
5484 if (target)
5485 return target;
5486 break;
5487
5488 case BUILT_IN_STRSTR:
5489 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5490 if (target)
5491 return target;
5492 break;
5493
5494 case BUILT_IN_STRPBRK:
5495 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5496 if (target)
5497 return target;
5498 break;
5499
5500 case BUILT_IN_INDEX:
5501 case BUILT_IN_STRCHR:
5502 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5503 if (target)
5504 return target;
5505 break;
5506
5507 case BUILT_IN_RINDEX:
5508 case BUILT_IN_STRRCHR:
5509 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5510 if (target)
5511 return target;
5512 break;
5513
5514 case BUILT_IN_MEMCPY:
5515 target = expand_builtin_memcpy (exp, target, mode);
5516 if (target)
5517 return target;
5518 break;
5519
5520 case BUILT_IN_MEMPCPY:
5521 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5522 if (target)
5523 return target;
5524 break;
5525
5526 case BUILT_IN_MEMMOVE:
5527 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, mode);
5528 if (target)
5529 return target;
5530 break;
5531
5532 case BUILT_IN_BCOPY:
5533 target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5534 if (target)
5535 return target;
5536 break;
5537
5538 case BUILT_IN_MEMSET:
5539 target = expand_builtin_memset (arglist, target, mode);
5540 if (target)
5541 return target;
5542 break;
5543
5544 case BUILT_IN_BZERO:
5545 target = expand_builtin_bzero (arglist);
5546 if (target)
5547 return target;
5548 break;
5549
5550 case BUILT_IN_STRCMP:
5551 target = expand_builtin_strcmp (exp, target, mode);
5552 if (target)
5553 return target;
5554 break;
5555
5556 case BUILT_IN_STRNCMP:
5557 target = expand_builtin_strncmp (exp, target, mode);
5558 if (target)
5559 return target;
5560 break;
5561
5562 case BUILT_IN_BCMP:
5563 case BUILT_IN_MEMCMP:
5564 target = expand_builtin_memcmp (exp, arglist, target, mode);
5565 if (target)
5566 return target;
5567 break;
5568
5569 case BUILT_IN_SETJMP:
5570 target = expand_builtin_setjmp (arglist, target);
5571 if (target)
5572 return target;
5573 break;
5574
5575 /* __builtin_longjmp is passed a pointer to an array of five words.
5576 It's similar to the C library longjmp function but works with
5577 __builtin_setjmp above. */
5578 case BUILT_IN_LONGJMP:
5579 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5580 break;
5581 else
5582 {
5583 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5584 VOIDmode, 0);
5585 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5586 NULL_RTX, VOIDmode, 0);
5587
5588 if (value != const1_rtx)
5589 {
5590 error ("%<__builtin_longjmp%> second argument must be 1");
5591 return const0_rtx;
5592 }
5593
5594 expand_builtin_longjmp (buf_addr, value);
5595 return const0_rtx;
5596 }
5597
5598 case BUILT_IN_NONLOCAL_GOTO:
5599 target = expand_builtin_nonlocal_goto (arglist);
5600 if (target)
5601 return target;
5602 break;
5603
5604 /* This updates the setjmp buffer that is its argument with the value
5605 of the current stack pointer. */
5606 case BUILT_IN_UPDATE_SETJMP_BUF:
5607 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5608 {
5609 rtx buf_addr
5610 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5611
5612 expand_builtin_update_setjmp_buf (buf_addr);
5613 return const0_rtx;
5614 }
5615 break;
5616
5617 case BUILT_IN_TRAP:
5618 expand_builtin_trap ();
5619 return const0_rtx;
5620
5621 case BUILT_IN_PRINTF:
5622 target = expand_builtin_printf (arglist, target, mode, false);
5623 if (target)
5624 return target;
5625 break;
5626
5627 case BUILT_IN_PRINTF_UNLOCKED:
5628 target = expand_builtin_printf (arglist, target, mode, true);
5629 if (target)
5630 return target;
5631 break;
5632
5633 case BUILT_IN_FPUTS:
5634 target = expand_builtin_fputs (arglist, target, false);
5635 if (target)
5636 return target;
5637 break;
5638 case BUILT_IN_FPUTS_UNLOCKED:
5639 target = expand_builtin_fputs (arglist, target, true);
5640 if (target)
5641 return target;
5642 break;
5643
5644 case BUILT_IN_FPRINTF:
5645 target = expand_builtin_fprintf (arglist, target, mode, false);
5646 if (target)
5647 return target;
5648 break;
5649
5650 case BUILT_IN_FPRINTF_UNLOCKED:
5651 target = expand_builtin_fprintf (arglist, target, mode, true);
5652 if (target)
5653 return target;
5654 break;
5655
5656 case BUILT_IN_SPRINTF:
5657 target = expand_builtin_sprintf (arglist, target, mode);
5658 if (target)
5659 return target;
5660 break;
5661
5662 case BUILT_IN_SIGNBIT:
5663 case BUILT_IN_SIGNBITF:
5664 case BUILT_IN_SIGNBITL:
5665 target = expand_builtin_signbit (exp, target);
5666 if (target)
5667 return target;
5668 break;
5669
5670 /* Various hooks for the DWARF 2 __throw routine. */
5671 case BUILT_IN_UNWIND_INIT:
5672 expand_builtin_unwind_init ();
5673 return const0_rtx;
5674 case BUILT_IN_DWARF_CFA:
5675 return virtual_cfa_rtx;
5676 #ifdef DWARF2_UNWIND_INFO
5677 case BUILT_IN_DWARF_SP_COLUMN:
5678 return expand_builtin_dwarf_sp_column ();
5679 case BUILT_IN_INIT_DWARF_REG_SIZES:
5680 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5681 return const0_rtx;
5682 #endif
5683 case BUILT_IN_FROB_RETURN_ADDR:
5684 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5685 case BUILT_IN_EXTRACT_RETURN_ADDR:
5686 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5687 case BUILT_IN_EH_RETURN:
5688 expand_builtin_eh_return (TREE_VALUE (arglist),
5689 TREE_VALUE (TREE_CHAIN (arglist)));
5690 return const0_rtx;
5691 #ifdef EH_RETURN_DATA_REGNO
5692 case BUILT_IN_EH_RETURN_DATA_REGNO:
5693 return expand_builtin_eh_return_data_regno (arglist);
5694 #endif
5695 case BUILT_IN_EXTEND_POINTER:
5696 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5697
5698 case BUILT_IN_VA_START:
5699 case BUILT_IN_STDARG_START:
5700 return expand_builtin_va_start (arglist);
5701 case BUILT_IN_VA_END:
5702 return expand_builtin_va_end (arglist);
5703 case BUILT_IN_VA_COPY:
5704 return expand_builtin_va_copy (arglist);
5705 case BUILT_IN_EXPECT:
5706 return expand_builtin_expect (arglist, target);
5707 case BUILT_IN_PREFETCH:
5708 expand_builtin_prefetch (arglist);
5709 return const0_rtx;
5710
5711 case BUILT_IN_PROFILE_FUNC_ENTER:
5712 return expand_builtin_profile_func (false);
5713 case BUILT_IN_PROFILE_FUNC_EXIT:
5714 return expand_builtin_profile_func (true);
5715
5716 case BUILT_IN_INIT_TRAMPOLINE:
5717 return expand_builtin_init_trampoline (arglist);
5718 case BUILT_IN_ADJUST_TRAMPOLINE:
5719 return expand_builtin_adjust_trampoline (arglist);
5720
5721 case BUILT_IN_FORK:
5722 case BUILT_IN_EXECL:
5723 case BUILT_IN_EXECV:
5724 case BUILT_IN_EXECLP:
5725 case BUILT_IN_EXECLE:
5726 case BUILT_IN_EXECVP:
5727 case BUILT_IN_EXECVE:
5728 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5729 if (target)
5730 return target;
5731 break;
5732
5733 default: /* just do library call, if unknown builtin */
5734 break;
5735 }
5736
5737 /* The switch statement above can drop through to cause the function
5738 to be called normally. */
5739 return expand_call (exp, target, ignore);
5740 }
5741
5742 /* Determine whether a tree node represents a call to a built-in
5743 function. If the tree T is a call to a built-in function with
5744 the right number of arguments of the appropriate types, return
5745 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5746 Otherwise the return value is END_BUILTINS. */
5747
5748 enum built_in_function
5749 builtin_mathfn_code (tree t)
5750 {
5751 tree fndecl, arglist, parmlist;
5752 tree argtype, parmtype;
5753
5754 if (TREE_CODE (t) != CALL_EXPR
5755 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5756 return END_BUILTINS;
5757
5758 fndecl = get_callee_fndecl (t);
5759 if (fndecl == NULL_TREE
5760 || TREE_CODE (fndecl) != FUNCTION_DECL
5761 || ! DECL_BUILT_IN (fndecl)
5762 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5763 return END_BUILTINS;
5764
5765 arglist = TREE_OPERAND (t, 1);
5766 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5767 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5768 {
5769 /* If a function doesn't take a variable number of arguments,
5770 the last element in the list will have type `void'. */
5771 parmtype = TREE_VALUE (parmlist);
5772 if (VOID_TYPE_P (parmtype))
5773 {
5774 if (arglist)
5775 return END_BUILTINS;
5776 return DECL_FUNCTION_CODE (fndecl);
5777 }
5778
5779 if (! arglist)
5780 return END_BUILTINS;
5781
5782 argtype = TREE_TYPE (TREE_VALUE (arglist));
5783
5784 if (SCALAR_FLOAT_TYPE_P (parmtype))
5785 {
5786 if (! SCALAR_FLOAT_TYPE_P (argtype))
5787 return END_BUILTINS;
5788 }
5789 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5790 {
5791 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5792 return END_BUILTINS;
5793 }
5794 else if (POINTER_TYPE_P (parmtype))
5795 {
5796 if (! POINTER_TYPE_P (argtype))
5797 return END_BUILTINS;
5798 }
5799 else if (INTEGRAL_TYPE_P (parmtype))
5800 {
5801 if (! INTEGRAL_TYPE_P (argtype))
5802 return END_BUILTINS;
5803 }
5804 else
5805 return END_BUILTINS;
5806
5807 arglist = TREE_CHAIN (arglist);
5808 }
5809
5810 /* Variable-length argument list. */
5811 return DECL_FUNCTION_CODE (fndecl);
5812 }
5813
5814 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5815 constant. ARGLIST is the argument list of the call. */
5816
5817 static tree
5818 fold_builtin_constant_p (tree arglist)
5819 {
5820 if (arglist == 0)
5821 return 0;
5822
5823 arglist = TREE_VALUE (arglist);
5824
5825 /* We return 1 for a numeric type that's known to be a constant
5826 value at compile-time or for an aggregate type that's a
5827 literal constant. */
5828 STRIP_NOPS (arglist);
5829
5830 /* If we know this is a constant, emit the constant of one. */
5831 if (CONSTANT_CLASS_P (arglist)
5832 || (TREE_CODE (arglist) == CONSTRUCTOR
5833 && TREE_CONSTANT (arglist))
5834 || (TREE_CODE (arglist) == ADDR_EXPR
5835 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5836 return integer_one_node;
5837
5838 /* If this expression has side effects, show we don't know it to be a
5839 constant. Likewise if it's a pointer or aggregate type since in
5840 those case we only want literals, since those are only optimized
5841 when generating RTL, not later.
5842 And finally, if we are compiling an initializer, not code, we
5843 need to return a definite result now; there's not going to be any
5844 more optimization done. */
5845 if (TREE_SIDE_EFFECTS (arglist)
5846 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5847 || POINTER_TYPE_P (TREE_TYPE (arglist))
5848 || cfun == 0)
5849 return integer_zero_node;
5850
5851 return 0;
5852 }
5853
5854 /* Fold a call to __builtin_expect, if we expect that a comparison against
5855 the argument will fold to a constant. In practice, this means a true
5856 constant or the address of a non-weak symbol. ARGLIST is the argument
5857 list of the call. */
5858
5859 static tree
5860 fold_builtin_expect (tree arglist)
5861 {
5862 tree arg, inner;
5863
5864 if (arglist == 0)
5865 return 0;
5866
5867 arg = TREE_VALUE (arglist);
5868
5869 /* If the argument isn't invariant, then there's nothing we can do. */
5870 if (!TREE_INVARIANT (arg))
5871 return 0;
5872
5873 /* If we're looking at an address of a weak decl, then do not fold. */
5874 inner = arg;
5875 STRIP_NOPS (inner);
5876 if (TREE_CODE (inner) == ADDR_EXPR)
5877 {
5878 do
5879 {
5880 inner = TREE_OPERAND (inner, 0);
5881 }
5882 while (TREE_CODE (inner) == COMPONENT_REF
5883 || TREE_CODE (inner) == ARRAY_REF);
5884 if (DECL_P (inner) && DECL_WEAK (inner))
5885 return 0;
5886 }
5887
5888 /* Otherwise, ARG already has the proper type for the return value. */
5889 return arg;
5890 }
5891
5892 /* Fold a call to __builtin_classify_type. */
5893
5894 static tree
5895 fold_builtin_classify_type (tree arglist)
5896 {
5897 if (arglist == 0)
5898 return build_int_cst (NULL_TREE, no_type_class);
5899
5900 return build_int_cst (NULL_TREE,
5901 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
5902 }
5903
5904 /* Fold a call to __builtin_strlen. */
5905
5906 static tree
5907 fold_builtin_strlen (tree arglist)
5908 {
5909 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5910 return NULL_TREE;
5911 else
5912 {
5913 tree len = c_strlen (TREE_VALUE (arglist), 0);
5914
5915 if (len)
5916 {
5917 /* Convert from the internal "sizetype" type to "size_t". */
5918 if (size_type_node)
5919 len = fold_convert (size_type_node, len);
5920 return len;
5921 }
5922
5923 return NULL_TREE;
5924 }
5925 }
5926
5927 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5928
5929 static tree
5930 fold_builtin_inf (tree type, int warn)
5931 {
5932 REAL_VALUE_TYPE real;
5933
5934 /* __builtin_inff is intended to be usable to define INFINITY on all
5935 targets. If an infinity is not available, INFINITY expands "to a
5936 positive constant of type float that overflows at translation
5937 time", footnote "In this case, using INFINITY will violate the
5938 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5939 Thus we pedwarn to ensure this constraint violation is
5940 diagnosed. */
5941 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5942 pedwarn ("target format does not support infinity");
5943
5944 real_inf (&real);
5945 return build_real (type, real);
5946 }
5947
5948 /* Fold a call to __builtin_nan or __builtin_nans. */
5949
5950 static tree
5951 fold_builtin_nan (tree arglist, tree type, int quiet)
5952 {
5953 REAL_VALUE_TYPE real;
5954 const char *str;
5955
5956 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5957 return 0;
5958 str = c_getstr (TREE_VALUE (arglist));
5959 if (!str)
5960 return 0;
5961
5962 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5963 return 0;
5964
5965 return build_real (type, real);
5966 }
5967
5968 /* Return true if the floating point expression T has an integer value.
5969 We also allow +Inf, -Inf and NaN to be considered integer values. */
5970
5971 static bool
5972 integer_valued_real_p (tree t)
5973 {
5974 switch (TREE_CODE (t))
5975 {
5976 case FLOAT_EXPR:
5977 return true;
5978
5979 case ABS_EXPR:
5980 case SAVE_EXPR:
5981 case NON_LVALUE_EXPR:
5982 return integer_valued_real_p (TREE_OPERAND (t, 0));
5983
5984 case COMPOUND_EXPR:
5985 case MODIFY_EXPR:
5986 case BIND_EXPR:
5987 return integer_valued_real_p (TREE_OPERAND (t, 1));
5988
5989 case PLUS_EXPR:
5990 case MINUS_EXPR:
5991 case MULT_EXPR:
5992 case MIN_EXPR:
5993 case MAX_EXPR:
5994 return integer_valued_real_p (TREE_OPERAND (t, 0))
5995 && integer_valued_real_p (TREE_OPERAND (t, 1));
5996
5997 case COND_EXPR:
5998 return integer_valued_real_p (TREE_OPERAND (t, 1))
5999 && integer_valued_real_p (TREE_OPERAND (t, 2));
6000
6001 case REAL_CST:
6002 if (! TREE_CONSTANT_OVERFLOW (t))
6003 {
6004 REAL_VALUE_TYPE c, cint;
6005
6006 c = TREE_REAL_CST (t);
6007 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6008 return real_identical (&c, &cint);
6009 }
6010
6011 case NOP_EXPR:
6012 {
6013 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6014 if (TREE_CODE (type) == INTEGER_TYPE)
6015 return true;
6016 if (TREE_CODE (type) == REAL_TYPE)
6017 return integer_valued_real_p (TREE_OPERAND (t, 0));
6018 break;
6019 }
6020
6021 case CALL_EXPR:
6022 switch (builtin_mathfn_code (t))
6023 {
6024 case BUILT_IN_CEIL:
6025 case BUILT_IN_CEILF:
6026 case BUILT_IN_CEILL:
6027 case BUILT_IN_FLOOR:
6028 case BUILT_IN_FLOORF:
6029 case BUILT_IN_FLOORL:
6030 case BUILT_IN_NEARBYINT:
6031 case BUILT_IN_NEARBYINTF:
6032 case BUILT_IN_NEARBYINTL:
6033 case BUILT_IN_RINT:
6034 case BUILT_IN_RINTF:
6035 case BUILT_IN_RINTL:
6036 case BUILT_IN_ROUND:
6037 case BUILT_IN_ROUNDF:
6038 case BUILT_IN_ROUNDL:
6039 case BUILT_IN_TRUNC:
6040 case BUILT_IN_TRUNCF:
6041 case BUILT_IN_TRUNCL:
6042 return true;
6043
6044 default:
6045 break;
6046 }
6047 break;
6048
6049 default:
6050 break;
6051 }
6052 return false;
6053 }
6054
6055 /* EXP is assumed to be builtin call where truncation can be propagated
6056 across (for instance floor((double)f) == (double)floorf (f).
6057 Do the transformation. */
6058
6059 static tree
6060 fold_trunc_transparent_mathfn (tree exp)
6061 {
6062 tree fndecl = get_callee_fndecl (exp);
6063 tree arglist = TREE_OPERAND (exp, 1);
6064 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6065 tree arg;
6066
6067 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6068 return 0;
6069
6070 arg = TREE_VALUE (arglist);
6071 /* Integer rounding functions are idempotent. */
6072 if (fcode == builtin_mathfn_code (arg))
6073 return arg;
6074
6075 /* If argument is already integer valued, and we don't need to worry
6076 about setting errno, there's no need to perform rounding. */
6077 if (! flag_errno_math && integer_valued_real_p (arg))
6078 return arg;
6079
6080 if (optimize)
6081 {
6082 tree arg0 = strip_float_extensions (arg);
6083 tree ftype = TREE_TYPE (exp);
6084 tree newtype = TREE_TYPE (arg0);
6085 tree decl;
6086
6087 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6088 && (decl = mathfn_built_in (newtype, fcode)))
6089 {
6090 arglist =
6091 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6092 return fold_convert (ftype,
6093 build_function_call_expr (decl, arglist));
6094 }
6095 }
6096 return 0;
6097 }
6098
6099 /* EXP is assumed to be builtin call which can narrow the FP type of
6100 the argument, for instance lround((double)f) -> lroundf (f). */
6101
6102 static tree
6103 fold_fixed_mathfn (tree exp)
6104 {
6105 tree fndecl = get_callee_fndecl (exp);
6106 tree arglist = TREE_OPERAND (exp, 1);
6107 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6108 tree arg;
6109
6110 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6111 return 0;
6112
6113 arg = TREE_VALUE (arglist);
6114
6115 /* If argument is already integer valued, and we don't need to worry
6116 about setting errno, there's no need to perform rounding. */
6117 if (! flag_errno_math && integer_valued_real_p (arg))
6118 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6119
6120 if (optimize)
6121 {
6122 tree ftype = TREE_TYPE (arg);
6123 tree arg0 = strip_float_extensions (arg);
6124 tree newtype = TREE_TYPE (arg0);
6125 tree decl;
6126
6127 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6128 && (decl = mathfn_built_in (newtype, fcode)))
6129 {
6130 arglist =
6131 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6132 return build_function_call_expr (decl, arglist);
6133 }
6134 }
6135 return 0;
6136 }
6137
6138 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6139 is the argument list and TYPE is the return type. Return
6140 NULL_TREE if no if no simplification can be made. */
6141
6142 static tree
6143 fold_builtin_cabs (tree arglist, tree type)
6144 {
6145 tree arg;
6146
6147 if (!arglist || TREE_CHAIN (arglist))
6148 return NULL_TREE;
6149
6150 arg = TREE_VALUE (arglist);
6151 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6152 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6153 return NULL_TREE;
6154
6155 /* Evaluate cabs of a constant at compile-time. */
6156 if (flag_unsafe_math_optimizations
6157 && TREE_CODE (arg) == COMPLEX_CST
6158 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6159 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6160 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6161 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6162 {
6163 REAL_VALUE_TYPE r, i;
6164
6165 r = TREE_REAL_CST (TREE_REALPART (arg));
6166 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6167
6168 real_arithmetic (&r, MULT_EXPR, &r, &r);
6169 real_arithmetic (&i, MULT_EXPR, &i, &i);
6170 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6171 if (real_sqrt (&r, TYPE_MODE (type), &r)
6172 || ! flag_trapping_math)
6173 return build_real (type, r);
6174 }
6175
6176 /* If either part is zero, cabs is fabs of the other. */
6177 if (TREE_CODE (arg) == COMPLEX_EXPR
6178 && real_zerop (TREE_OPERAND (arg, 0)))
6179 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6180 if (TREE_CODE (arg) == COMPLEX_EXPR
6181 && real_zerop (TREE_OPERAND (arg, 1)))
6182 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6183
6184 /* Don't do this when optimizing for size. */
6185 if (flag_unsafe_math_optimizations
6186 && optimize && !optimize_size)
6187 {
6188 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6189
6190 if (sqrtfn != NULL_TREE)
6191 {
6192 tree rpart, ipart, result, arglist;
6193
6194 arg = builtin_save_expr (arg);
6195
6196 rpart = fold (build1 (REALPART_EXPR, type, arg));
6197 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6198
6199 rpart = builtin_save_expr (rpart);
6200 ipart = builtin_save_expr (ipart);
6201
6202 result = fold (build2 (PLUS_EXPR, type,
6203 fold (build2 (MULT_EXPR, type,
6204 rpart, rpart)),
6205 fold (build2 (MULT_EXPR, type,
6206 ipart, ipart))));
6207
6208 arglist = build_tree_list (NULL_TREE, result);
6209 return build_function_call_expr (sqrtfn, arglist);
6210 }
6211 }
6212
6213 return NULL_TREE;
6214 }
6215
6216 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6217 NULL_TREE if no simplification can be made. */
6218
6219 static tree
6220 fold_builtin_sqrt (tree arglist, tree type)
6221 {
6222
6223 enum built_in_function fcode;
6224 tree arg = TREE_VALUE (arglist);
6225
6226 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6227 return NULL_TREE;
6228
6229 /* Optimize sqrt of constant value. */
6230 if (TREE_CODE (arg) == REAL_CST
6231 && ! TREE_CONSTANT_OVERFLOW (arg))
6232 {
6233 REAL_VALUE_TYPE r, x;
6234
6235 x = TREE_REAL_CST (arg);
6236 if (real_sqrt (&r, TYPE_MODE (type), &x)
6237 || (!flag_trapping_math && !flag_errno_math))
6238 return build_real (type, r);
6239 }
6240
6241 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6242 fcode = builtin_mathfn_code (arg);
6243 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6244 {
6245 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6246 arg = fold (build2 (MULT_EXPR, type,
6247 TREE_VALUE (TREE_OPERAND (arg, 1)),
6248 build_real (type, dconsthalf)));
6249 arglist = build_tree_list (NULL_TREE, arg);
6250 return build_function_call_expr (expfn, arglist);
6251 }
6252
6253 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6254 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6255 {
6256 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6257
6258 if (powfn)
6259 {
6260 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6261 tree tree_root;
6262 /* The inner root was either sqrt or cbrt. */
6263 REAL_VALUE_TYPE dconstroot =
6264 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6265
6266 /* Adjust for the outer root. */
6267 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6268 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6269 tree_root = build_real (type, dconstroot);
6270 arglist = tree_cons (NULL_TREE, arg0,
6271 build_tree_list (NULL_TREE, tree_root));
6272 return build_function_call_expr (powfn, arglist);
6273 }
6274 }
6275
6276 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6277 if (flag_unsafe_math_optimizations
6278 && (fcode == BUILT_IN_POW
6279 || fcode == BUILT_IN_POWF
6280 || fcode == BUILT_IN_POWL))
6281 {
6282 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6283 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6284 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6285 tree narg1;
6286 if (!tree_expr_nonnegative_p (arg0))
6287 arg0 = build1 (ABS_EXPR, type, arg0);
6288 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6289 build_real (type, dconsthalf)));
6290 arglist = tree_cons (NULL_TREE, arg0,
6291 build_tree_list (NULL_TREE, narg1));
6292 return build_function_call_expr (powfn, arglist);
6293 }
6294
6295 return NULL_TREE;
6296 }
6297
6298 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6299 NULL_TREE if no simplification can be made. */
6300 static tree
6301 fold_builtin_cbrt (tree arglist, tree type)
6302 {
6303 tree arg = TREE_VALUE (arglist);
6304 const enum built_in_function fcode = builtin_mathfn_code (arg);
6305
6306 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6307 return NULL_TREE;
6308
6309 /* Optimize cbrt of constant value. */
6310 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6311 return arg;
6312
6313 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6314 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6315 {
6316 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6317 const REAL_VALUE_TYPE third_trunc =
6318 real_value_truncate (TYPE_MODE (type), dconstthird);
6319 arg = fold (build2 (MULT_EXPR, type,
6320 TREE_VALUE (TREE_OPERAND (arg, 1)),
6321 build_real (type, third_trunc)));
6322 arglist = build_tree_list (NULL_TREE, arg);
6323 return build_function_call_expr (expfn, arglist);
6324 }
6325
6326 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6327 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6328 x is negative pow will error but cbrt won't. */
6329 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6330 {
6331 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6332
6333 if (powfn)
6334 {
6335 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6336 tree tree_root;
6337 REAL_VALUE_TYPE dconstroot = dconstthird;
6338
6339 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6340 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6341 tree_root = build_real (type, dconstroot);
6342 arglist = tree_cons (NULL_TREE, arg0,
6343 build_tree_list (NULL_TREE, tree_root));
6344 return build_function_call_expr (powfn, arglist);
6345 }
6346
6347 }
6348 return NULL_TREE;
6349 }
6350
6351 /* Fold function call to builtin sin, sinf, or sinl. Return
6352 NULL_TREE if no simplification can be made. */
6353 static tree
6354 fold_builtin_sin (tree arglist)
6355 {
6356 tree arg = TREE_VALUE (arglist);
6357
6358 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6359 return NULL_TREE;
6360
6361 /* Optimize sin (0.0) = 0.0. */
6362 if (real_zerop (arg))
6363 return arg;
6364
6365 return NULL_TREE;
6366 }
6367
6368 /* Fold function call to builtin cos, cosf, or cosl. Return
6369 NULL_TREE if no simplification can be made. */
6370 static tree
6371 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6372 {
6373 tree arg = TREE_VALUE (arglist);
6374
6375 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6376 return NULL_TREE;
6377
6378 /* Optimize cos (0.0) = 1.0. */
6379 if (real_zerop (arg))
6380 return build_real (type, dconst1);
6381
6382 /* Optimize cos(-x) into cos (x). */
6383 if (TREE_CODE (arg) == NEGATE_EXPR)
6384 {
6385 tree args = build_tree_list (NULL_TREE,
6386 TREE_OPERAND (arg, 0));
6387 return build_function_call_expr (fndecl, args);
6388 }
6389
6390 return NULL_TREE;
6391 }
6392
6393 /* Fold function call to builtin tan, tanf, or tanl. Return
6394 NULL_TREE if no simplification can be made. */
6395 static tree
6396 fold_builtin_tan (tree arglist)
6397 {
6398 enum built_in_function fcode;
6399 tree arg = TREE_VALUE (arglist);
6400
6401 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6402 return NULL_TREE;
6403
6404 /* Optimize tan(0.0) = 0.0. */
6405 if (real_zerop (arg))
6406 return arg;
6407
6408 /* Optimize tan(atan(x)) = x. */
6409 fcode = builtin_mathfn_code (arg);
6410 if (flag_unsafe_math_optimizations
6411 && (fcode == BUILT_IN_ATAN
6412 || fcode == BUILT_IN_ATANF
6413 || fcode == BUILT_IN_ATANL))
6414 return TREE_VALUE (TREE_OPERAND (arg, 1));
6415
6416 return NULL_TREE;
6417 }
6418
6419 /* Fold function call to builtin atan, atanf, or atanl. Return
6420 NULL_TREE if no simplification can be made. */
6421
6422 static tree
6423 fold_builtin_atan (tree arglist, tree type)
6424 {
6425
6426 tree arg = TREE_VALUE (arglist);
6427
6428 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6429 return NULL_TREE;
6430
6431 /* Optimize atan(0.0) = 0.0. */
6432 if (real_zerop (arg))
6433 return arg;
6434
6435 /* Optimize atan(1.0) = pi/4. */
6436 if (real_onep (arg))
6437 {
6438 REAL_VALUE_TYPE cst;
6439
6440 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6441 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6442 return build_real (type, cst);
6443 }
6444
6445 return NULL_TREE;
6446 }
6447
6448 /* Fold function call to builtin trunc, truncf or truncl. Return
6449 NULL_TREE if no simplification can be made. */
6450
6451 static tree
6452 fold_builtin_trunc (tree exp)
6453 {
6454 tree arglist = TREE_OPERAND (exp, 1);
6455 tree arg;
6456
6457 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6458 return 0;
6459
6460 /* Optimize trunc of constant value. */
6461 arg = TREE_VALUE (arglist);
6462 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6463 {
6464 REAL_VALUE_TYPE r, x;
6465 tree type = TREE_TYPE (exp);
6466
6467 x = TREE_REAL_CST (arg);
6468 real_trunc (&r, TYPE_MODE (type), &x);
6469 return build_real (type, r);
6470 }
6471
6472 return fold_trunc_transparent_mathfn (exp);
6473 }
6474
6475 /* Fold function call to builtin floor, floorf or floorl. Return
6476 NULL_TREE if no simplification can be made. */
6477
6478 static tree
6479 fold_builtin_floor (tree exp)
6480 {
6481 tree arglist = TREE_OPERAND (exp, 1);
6482 tree arg;
6483
6484 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6485 return 0;
6486
6487 /* Optimize floor of constant value. */
6488 arg = TREE_VALUE (arglist);
6489 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6490 {
6491 REAL_VALUE_TYPE x;
6492
6493 x = TREE_REAL_CST (arg);
6494 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6495 {
6496 tree type = TREE_TYPE (exp);
6497 REAL_VALUE_TYPE r;
6498
6499 real_floor (&r, TYPE_MODE (type), &x);
6500 return build_real (type, r);
6501 }
6502 }
6503
6504 return fold_trunc_transparent_mathfn (exp);
6505 }
6506
6507 /* Fold function call to builtin ceil, ceilf or ceill. Return
6508 NULL_TREE if no simplification can be made. */
6509
6510 static tree
6511 fold_builtin_ceil (tree exp)
6512 {
6513 tree arglist = TREE_OPERAND (exp, 1);
6514 tree arg;
6515
6516 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6517 return 0;
6518
6519 /* Optimize ceil of constant value. */
6520 arg = TREE_VALUE (arglist);
6521 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6522 {
6523 REAL_VALUE_TYPE x;
6524
6525 x = TREE_REAL_CST (arg);
6526 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6527 {
6528 tree type = TREE_TYPE (exp);
6529 REAL_VALUE_TYPE r;
6530
6531 real_ceil (&r, TYPE_MODE (type), &x);
6532 return build_real (type, r);
6533 }
6534 }
6535
6536 return fold_trunc_transparent_mathfn (exp);
6537 }
6538
6539 /* Fold function call to builtin round, roundf or roundl. Return
6540 NULL_TREE if no simplification can be made. */
6541
6542 static tree
6543 fold_builtin_round (tree exp)
6544 {
6545 tree arglist = TREE_OPERAND (exp, 1);
6546 tree arg;
6547
6548 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6549 return 0;
6550
6551 /* Optimize round of constant value. */
6552 arg = TREE_VALUE (arglist);
6553 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6554 {
6555 REAL_VALUE_TYPE x;
6556
6557 x = TREE_REAL_CST (arg);
6558 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6559 {
6560 tree type = TREE_TYPE (exp);
6561 REAL_VALUE_TYPE r;
6562
6563 real_round (&r, TYPE_MODE (type), &x);
6564 return build_real (type, r);
6565 }
6566 }
6567
6568 return fold_trunc_transparent_mathfn (exp);
6569 }
6570
6571 /* Fold function call to builtin lround, lroundf or lroundl (or the
6572 corresponding long long versions). Return NULL_TREE if no
6573 simplification can be made. */
6574
6575 static tree
6576 fold_builtin_lround (tree exp)
6577 {
6578 tree arglist = TREE_OPERAND (exp, 1);
6579 tree arg;
6580
6581 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6582 return 0;
6583
6584 /* Optimize lround of constant value. */
6585 arg = TREE_VALUE (arglist);
6586 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6587 {
6588 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6589
6590 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6591 {
6592 tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6593 HOST_WIDE_INT hi, lo;
6594 REAL_VALUE_TYPE r;
6595
6596 real_round (&r, TYPE_MODE (ftype), &x);
6597 REAL_VALUE_TO_INT (&lo, &hi, r);
6598 result = build_int_cst_wide (NULL_TREE, lo, hi);
6599 if (int_fits_type_p (result, itype))
6600 return fold_convert (itype, result);
6601 }
6602 }
6603
6604 return fold_fixed_mathfn (exp);
6605 }
6606
6607 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6608 and their long and long long variants (i.e. ffsl and ffsll).
6609 Return NULL_TREE if no simplification can be made. */
6610
6611 static tree
6612 fold_builtin_bitop (tree exp)
6613 {
6614 tree fndecl = get_callee_fndecl (exp);
6615 tree arglist = TREE_OPERAND (exp, 1);
6616 tree arg;
6617
6618 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6619 return NULL_TREE;
6620
6621 /* Optimize for constant argument. */
6622 arg = TREE_VALUE (arglist);
6623 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6624 {
6625 HOST_WIDE_INT hi, width, result;
6626 unsigned HOST_WIDE_INT lo;
6627 tree type;
6628
6629 type = TREE_TYPE (arg);
6630 width = TYPE_PRECISION (type);
6631 lo = TREE_INT_CST_LOW (arg);
6632
6633 /* Clear all the bits that are beyond the type's precision. */
6634 if (width > HOST_BITS_PER_WIDE_INT)
6635 {
6636 hi = TREE_INT_CST_HIGH (arg);
6637 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6638 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6639 }
6640 else
6641 {
6642 hi = 0;
6643 if (width < HOST_BITS_PER_WIDE_INT)
6644 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6645 }
6646
6647 switch (DECL_FUNCTION_CODE (fndecl))
6648 {
6649 case BUILT_IN_FFS:
6650 case BUILT_IN_FFSL:
6651 case BUILT_IN_FFSLL:
6652 if (lo != 0)
6653 result = exact_log2 (lo & -lo) + 1;
6654 else if (hi != 0)
6655 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6656 else
6657 result = 0;
6658 break;
6659
6660 case BUILT_IN_CLZ:
6661 case BUILT_IN_CLZL:
6662 case BUILT_IN_CLZLL:
6663 if (hi != 0)
6664 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6665 else if (lo != 0)
6666 result = width - floor_log2 (lo) - 1;
6667 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6668 result = width;
6669 break;
6670
6671 case BUILT_IN_CTZ:
6672 case BUILT_IN_CTZL:
6673 case BUILT_IN_CTZLL:
6674 if (lo != 0)
6675 result = exact_log2 (lo & -lo);
6676 else if (hi != 0)
6677 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6678 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6679 result = width;
6680 break;
6681
6682 case BUILT_IN_POPCOUNT:
6683 case BUILT_IN_POPCOUNTL:
6684 case BUILT_IN_POPCOUNTLL:
6685 result = 0;
6686 while (lo)
6687 result++, lo &= lo - 1;
6688 while (hi)
6689 result++, hi &= hi - 1;
6690 break;
6691
6692 case BUILT_IN_PARITY:
6693 case BUILT_IN_PARITYL:
6694 case BUILT_IN_PARITYLL:
6695 result = 0;
6696 while (lo)
6697 result++, lo &= lo - 1;
6698 while (hi)
6699 result++, hi &= hi - 1;
6700 result &= 1;
6701 break;
6702
6703 default:
6704 gcc_unreachable ();
6705 }
6706
6707 return build_int_cst (TREE_TYPE (exp), result);
6708 }
6709
6710 return NULL_TREE;
6711 }
6712
6713 /* Return true if EXPR is the real constant contained in VALUE. */
6714
6715 static bool
6716 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6717 {
6718 STRIP_NOPS (expr);
6719
6720 return ((TREE_CODE (expr) == REAL_CST
6721 && ! TREE_CONSTANT_OVERFLOW (expr)
6722 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6723 || (TREE_CODE (expr) == COMPLEX_CST
6724 && real_dconstp (TREE_REALPART (expr), value)
6725 && real_zerop (TREE_IMAGPART (expr))));
6726 }
6727
6728 /* A subroutine of fold_builtin to fold the various logarithmic
6729 functions. EXP is the CALL_EXPR of a call to a builtin logN
6730 function. VALUE is the base of the logN function. */
6731
6732 static tree
6733 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6734 {
6735 tree arglist = TREE_OPERAND (exp, 1);
6736
6737 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6738 {
6739 tree fndecl = get_callee_fndecl (exp);
6740 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6741 tree arg = TREE_VALUE (arglist);
6742 const enum built_in_function fcode = builtin_mathfn_code (arg);
6743
6744 /* Optimize logN(1.0) = 0.0. */
6745 if (real_onep (arg))
6746 return build_real (type, dconst0);
6747
6748 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6749 exactly, then only do this if flag_unsafe_math_optimizations. */
6750 if (exact_real_truncate (TYPE_MODE (type), value)
6751 || flag_unsafe_math_optimizations)
6752 {
6753 const REAL_VALUE_TYPE value_truncate =
6754 real_value_truncate (TYPE_MODE (type), *value);
6755 if (real_dconstp (arg, &value_truncate))
6756 return build_real (type, dconst1);
6757 }
6758
6759 /* Special case, optimize logN(expN(x)) = x. */
6760 if (flag_unsafe_math_optimizations
6761 && ((value == &dconste
6762 && (fcode == BUILT_IN_EXP
6763 || fcode == BUILT_IN_EXPF
6764 || fcode == BUILT_IN_EXPL))
6765 || (value == &dconst2
6766 && (fcode == BUILT_IN_EXP2
6767 || fcode == BUILT_IN_EXP2F
6768 || fcode == BUILT_IN_EXP2L))
6769 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6770 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6771
6772 /* Optimize logN(func()) for various exponential functions. We
6773 want to determine the value "x" and the power "exponent" in
6774 order to transform logN(x**exponent) into exponent*logN(x). */
6775 if (flag_unsafe_math_optimizations)
6776 {
6777 tree exponent = 0, x = 0;
6778
6779 switch (fcode)
6780 {
6781 case BUILT_IN_EXP:
6782 case BUILT_IN_EXPF:
6783 case BUILT_IN_EXPL:
6784 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6785 x = build_real (type,
6786 real_value_truncate (TYPE_MODE (type), dconste));
6787 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6788 break;
6789 case BUILT_IN_EXP2:
6790 case BUILT_IN_EXP2F:
6791 case BUILT_IN_EXP2L:
6792 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6793 x = build_real (type, dconst2);
6794 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6795 break;
6796 case BUILT_IN_EXP10:
6797 case BUILT_IN_EXP10F:
6798 case BUILT_IN_EXP10L:
6799 case BUILT_IN_POW10:
6800 case BUILT_IN_POW10F:
6801 case BUILT_IN_POW10L:
6802 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6803 x = build_real (type, dconst10);
6804 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6805 break;
6806 case BUILT_IN_SQRT:
6807 case BUILT_IN_SQRTF:
6808 case BUILT_IN_SQRTL:
6809 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6810 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6811 exponent = build_real (type, dconsthalf);
6812 break;
6813 case BUILT_IN_CBRT:
6814 case BUILT_IN_CBRTF:
6815 case BUILT_IN_CBRTL:
6816 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6817 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6818 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6819 dconstthird));
6820 break;
6821 case BUILT_IN_POW:
6822 case BUILT_IN_POWF:
6823 case BUILT_IN_POWL:
6824 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6825 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6826 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6827 break;
6828 default:
6829 break;
6830 }
6831
6832 /* Now perform the optimization. */
6833 if (x && exponent)
6834 {
6835 tree logfn;
6836 arglist = build_tree_list (NULL_TREE, x);
6837 logfn = build_function_call_expr (fndecl, arglist);
6838 return fold (build2 (MULT_EXPR, type, exponent, logfn));
6839 }
6840 }
6841 }
6842
6843 return 0;
6844 }
6845
6846 /* Fold a builtin function call to pow, powf, or powl. Return
6847 NULL_TREE if no simplification can be made. */
6848 static tree
6849 fold_builtin_pow (tree fndecl, tree arglist, tree type)
6850 {
6851 enum built_in_function fcode;
6852 tree arg0 = TREE_VALUE (arglist);
6853 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6854
6855 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6856 return NULL_TREE;
6857
6858 /* Optimize pow(1.0,y) = 1.0. */
6859 if (real_onep (arg0))
6860 return omit_one_operand (type, build_real (type, dconst1), arg1);
6861
6862 if (TREE_CODE (arg1) == REAL_CST
6863 && ! TREE_CONSTANT_OVERFLOW (arg1))
6864 {
6865 REAL_VALUE_TYPE cint;
6866 REAL_VALUE_TYPE c;
6867 HOST_WIDE_INT n;
6868
6869 c = TREE_REAL_CST (arg1);
6870
6871 /* Optimize pow(x,0.0) = 1.0. */
6872 if (REAL_VALUES_EQUAL (c, dconst0))
6873 return omit_one_operand (type, build_real (type, dconst1),
6874 arg0);
6875
6876 /* Optimize pow(x,1.0) = x. */
6877 if (REAL_VALUES_EQUAL (c, dconst1))
6878 return arg0;
6879
6880 /* Optimize pow(x,-1.0) = 1.0/x. */
6881 if (REAL_VALUES_EQUAL (c, dconstm1))
6882 return fold (build2 (RDIV_EXPR, type,
6883 build_real (type, dconst1), arg0));
6884
6885 /* Optimize pow(x,0.5) = sqrt(x). */
6886 if (flag_unsafe_math_optimizations
6887 && REAL_VALUES_EQUAL (c, dconsthalf))
6888 {
6889 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6890
6891 if (sqrtfn != NULL_TREE)
6892 {
6893 tree arglist = build_tree_list (NULL_TREE, arg0);
6894 return build_function_call_expr (sqrtfn, arglist);
6895 }
6896 }
6897
6898 /* Check for an integer exponent. */
6899 n = real_to_integer (&c);
6900 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
6901 if (real_identical (&c, &cint))
6902 {
6903 /* Attempt to evaluate pow at compile-time. */
6904 if (TREE_CODE (arg0) == REAL_CST
6905 && ! TREE_CONSTANT_OVERFLOW (arg0))
6906 {
6907 REAL_VALUE_TYPE x;
6908 bool inexact;
6909
6910 x = TREE_REAL_CST (arg0);
6911 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6912 if (flag_unsafe_math_optimizations || !inexact)
6913 return build_real (type, x);
6914 }
6915
6916 /* Strip sign ops from even integer powers. */
6917 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
6918 {
6919 tree narg0 = fold_strip_sign_ops (arg0);
6920 if (narg0)
6921 {
6922 arglist = build_tree_list (NULL_TREE, arg1);
6923 arglist = tree_cons (NULL_TREE, narg0, arglist);
6924 return build_function_call_expr (fndecl, arglist);
6925 }
6926 }
6927 }
6928 }
6929
6930 /* Optimize pow(expN(x),y) = expN(x*y). */
6931 fcode = builtin_mathfn_code (arg0);
6932 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6933 {
6934 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6935 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6936 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
6937 arglist = build_tree_list (NULL_TREE, arg);
6938 return build_function_call_expr (expfn, arglist);
6939 }
6940
6941 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6942 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6943 {
6944 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6945 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
6946 build_real (type, dconsthalf)));
6947
6948 arglist = tree_cons (NULL_TREE, narg0,
6949 build_tree_list (NULL_TREE, narg1));
6950 return build_function_call_expr (fndecl, arglist);
6951 }
6952
6953 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
6954 if (flag_unsafe_math_optimizations
6955 && (fcode == BUILT_IN_POW
6956 || fcode == BUILT_IN_POWF
6957 || fcode == BUILT_IN_POWL))
6958 {
6959 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6960 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6961 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
6962 arglist = tree_cons (NULL_TREE, arg00,
6963 build_tree_list (NULL_TREE, narg1));
6964 return build_function_call_expr (fndecl, arglist);
6965 }
6966 return NULL_TREE;
6967 }
6968
6969 /* Fold a builtin function call to powi, powif, or powil. Return
6970 NULL_TREE if no simplification can be made. */
6971 static tree
6972 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
6973 {
6974 tree arg0 = TREE_VALUE (arglist);
6975 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6976
6977 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
6978 return NULL_TREE;
6979
6980 /* Optimize pow(1.0,y) = 1.0. */
6981 if (real_onep (arg0))
6982 return omit_one_operand (type, build_real (type, dconst1), arg1);
6983
6984 if (host_integerp (arg1, 0))
6985 {
6986 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
6987
6988 /* Evaluate powi at compile-time. */
6989 if (TREE_CODE (arg0) == REAL_CST
6990 && ! TREE_CONSTANT_OVERFLOW (arg0))
6991 {
6992 REAL_VALUE_TYPE x;
6993 x = TREE_REAL_CST (arg0);
6994 real_powi (&x, TYPE_MODE (type), &x, c);
6995 return build_real (type, x);
6996 }
6997
6998 /* Optimize pow(x,0) = 1.0. */
6999 if (c == 0)
7000 return omit_one_operand (type, build_real (type, dconst1),
7001 arg0);
7002
7003 /* Optimize pow(x,1) = x. */
7004 if (c == 1)
7005 return arg0;
7006
7007 /* Optimize pow(x,-1) = 1.0/x. */
7008 if (c == -1)
7009 return fold (build2 (RDIV_EXPR, type,
7010 build_real (type, dconst1), arg0));
7011 }
7012
7013 return NULL_TREE;
7014 }
7015
7016 /* A subroutine of fold_builtin to fold the various exponent
7017 functions. EXP is the CALL_EXPR of a call to a builtin function.
7018 VALUE is the value which will be raised to a power. */
7019
7020 static tree
7021 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
7022 {
7023 tree arglist = TREE_OPERAND (exp, 1);
7024
7025 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7026 {
7027 tree fndecl = get_callee_fndecl (exp);
7028 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7029 tree arg = TREE_VALUE (arglist);
7030
7031 /* Optimize exp*(0.0) = 1.0. */
7032 if (real_zerop (arg))
7033 return build_real (type, dconst1);
7034
7035 /* Optimize expN(1.0) = N. */
7036 if (real_onep (arg))
7037 {
7038 REAL_VALUE_TYPE cst;
7039
7040 real_convert (&cst, TYPE_MODE (type), value);
7041 return build_real (type, cst);
7042 }
7043
7044 /* Attempt to evaluate expN(integer) at compile-time. */
7045 if (flag_unsafe_math_optimizations
7046 && TREE_CODE (arg) == REAL_CST
7047 && ! TREE_CONSTANT_OVERFLOW (arg))
7048 {
7049 REAL_VALUE_TYPE cint;
7050 REAL_VALUE_TYPE c;
7051 HOST_WIDE_INT n;
7052
7053 c = TREE_REAL_CST (arg);
7054 n = real_to_integer (&c);
7055 real_from_integer (&cint, VOIDmode, n,
7056 n < 0 ? -1 : 0, 0);
7057 if (real_identical (&c, &cint))
7058 {
7059 REAL_VALUE_TYPE x;
7060
7061 real_powi (&x, TYPE_MODE (type), value, n);
7062 return build_real (type, x);
7063 }
7064 }
7065
7066 /* Optimize expN(logN(x)) = x. */
7067 if (flag_unsafe_math_optimizations)
7068 {
7069 const enum built_in_function fcode = builtin_mathfn_code (arg);
7070
7071 if ((value == &dconste
7072 && (fcode == BUILT_IN_LOG
7073 || fcode == BUILT_IN_LOGF
7074 || fcode == BUILT_IN_LOGL))
7075 || (value == &dconst2
7076 && (fcode == BUILT_IN_LOG2
7077 || fcode == BUILT_IN_LOG2F
7078 || fcode == BUILT_IN_LOG2L))
7079 || (value == &dconst10
7080 && (fcode == BUILT_IN_LOG10
7081 || fcode == BUILT_IN_LOG10F
7082 || fcode == BUILT_IN_LOG10L)))
7083 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7084 }
7085 }
7086
7087 return 0;
7088 }
7089
7090 /* Fold function call to builtin memcpy. Return
7091 NULL_TREE if no simplification can be made. */
7092
7093 static tree
7094 fold_builtin_memcpy (tree exp)
7095 {
7096 tree arglist = TREE_OPERAND (exp, 1);
7097 tree dest, src, len;
7098
7099 if (!validate_arglist (arglist,
7100 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7101 return 0;
7102
7103 dest = TREE_VALUE (arglist);
7104 src = TREE_VALUE (TREE_CHAIN (arglist));
7105 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7106
7107 /* If the LEN parameter is zero, return DEST. */
7108 if (integer_zerop (len))
7109 return omit_one_operand (TREE_TYPE (exp), dest, src);
7110
7111 /* If SRC and DEST are the same (and not volatile), return DEST. */
7112 if (operand_equal_p (src, dest, 0))
7113 return omit_one_operand (TREE_TYPE (exp), dest, len);
7114
7115 return 0;
7116 }
7117
7118 /* Fold function call to builtin mempcpy. Return
7119 NULL_TREE if no simplification can be made. */
7120
7121 static tree
7122 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7123 {
7124 if (validate_arglist (arglist,
7125 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7126 {
7127 tree dest = TREE_VALUE (arglist);
7128 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7129 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7130
7131 /* If the LEN parameter is zero, return DEST. */
7132 if (integer_zerop (len))
7133 return omit_one_operand (type, dest, src);
7134
7135 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7136 if (operand_equal_p (src, dest, 0))
7137 {
7138 if (endp == 0)
7139 return omit_one_operand (type, dest, len);
7140
7141 if (endp == 2)
7142 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7143 ssize_int (1)));
7144
7145 len = fold_convert (TREE_TYPE (dest), len);
7146 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7147 return fold_convert (type, len);
7148 }
7149 }
7150 return 0;
7151 }
7152
7153 /* Fold function call to builtin memmove. Return
7154 NULL_TREE if no simplification can be made. */
7155
7156 static tree
7157 fold_builtin_memmove (tree arglist, tree type)
7158 {
7159 tree dest, src, len;
7160
7161 if (!validate_arglist (arglist,
7162 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7163 return 0;
7164
7165 dest = TREE_VALUE (arglist);
7166 src = TREE_VALUE (TREE_CHAIN (arglist));
7167 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7168
7169 /* If the LEN parameter is zero, return DEST. */
7170 if (integer_zerop (len))
7171 return omit_one_operand (type, dest, src);
7172
7173 /* If SRC and DEST are the same (and not volatile), return DEST. */
7174 if (operand_equal_p (src, dest, 0))
7175 return omit_one_operand (type, dest, len);
7176
7177 return 0;
7178 }
7179
7180 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7181 the length of the string to be copied. Return NULL_TREE if no
7182 simplification can be made. */
7183
7184 tree
7185 fold_builtin_strcpy (tree exp, tree len)
7186 {
7187 tree arglist = TREE_OPERAND (exp, 1);
7188 tree dest, src, fn;
7189
7190 if (!validate_arglist (arglist,
7191 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7192 return 0;
7193
7194 dest = TREE_VALUE (arglist);
7195 src = TREE_VALUE (TREE_CHAIN (arglist));
7196
7197 /* If SRC and DEST are the same (and not volatile), return DEST. */
7198 if (operand_equal_p (src, dest, 0))
7199 return fold_convert (TREE_TYPE (exp), dest);
7200
7201 if (optimize_size)
7202 return 0;
7203
7204 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7205 if (!fn)
7206 return 0;
7207
7208 if (!len)
7209 {
7210 len = c_strlen (src, 1);
7211 if (! len || TREE_SIDE_EFFECTS (len))
7212 return 0;
7213 }
7214
7215 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7216 arglist = build_tree_list (NULL_TREE, len);
7217 arglist = tree_cons (NULL_TREE, src, arglist);
7218 arglist = tree_cons (NULL_TREE, dest, arglist);
7219 return fold_convert (TREE_TYPE (exp),
7220 build_function_call_expr (fn, arglist));
7221 }
7222
7223 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7224 the length of the source string. Return NULL_TREE if no simplification
7225 can be made. */
7226
7227 tree
7228 fold_builtin_strncpy (tree exp, tree slen)
7229 {
7230 tree arglist = TREE_OPERAND (exp, 1);
7231 tree dest, src, len, fn;
7232
7233 if (!validate_arglist (arglist,
7234 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7235 return 0;
7236
7237 dest = TREE_VALUE (arglist);
7238 src = TREE_VALUE (TREE_CHAIN (arglist));
7239 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7240
7241 /* If the LEN parameter is zero, return DEST. */
7242 if (integer_zerop (len))
7243 return omit_one_operand (TREE_TYPE (exp), dest, src);
7244
7245 /* We can't compare slen with len as constants below if len is not a
7246 constant. */
7247 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7248 return 0;
7249
7250 if (!slen)
7251 slen = c_strlen (src, 1);
7252
7253 /* Now, we must be passed a constant src ptr parameter. */
7254 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7255 return 0;
7256
7257 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7258
7259 /* We do not support simplification of this case, though we do
7260 support it when expanding trees into RTL. */
7261 /* FIXME: generate a call to __builtin_memset. */
7262 if (tree_int_cst_lt (slen, len))
7263 return 0;
7264
7265 /* OK transform into builtin memcpy. */
7266 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7267 if (!fn)
7268 return 0;
7269 return fold_convert (TREE_TYPE (exp),
7270 build_function_call_expr (fn, arglist));
7271 }
7272
7273 /* Fold function call to builtin memcmp. Return
7274 NULL_TREE if no simplification can be made. */
7275
7276 static tree
7277 fold_builtin_memcmp (tree arglist)
7278 {
7279 tree arg1, arg2, len;
7280 const char *p1, *p2;
7281
7282 if (!validate_arglist (arglist,
7283 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7284 return 0;
7285
7286 arg1 = TREE_VALUE (arglist);
7287 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7288 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7289
7290 /* If the LEN parameter is zero, return zero. */
7291 if (integer_zerop (len))
7292 return omit_two_operands (integer_type_node, integer_zero_node,
7293 arg1, arg2);
7294
7295 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7296 if (operand_equal_p (arg1, arg2, 0))
7297 return omit_one_operand (integer_type_node, integer_zero_node, len);
7298
7299 p1 = c_getstr (arg1);
7300 p2 = c_getstr (arg2);
7301
7302 /* If all arguments are constant, and the value of len is not greater
7303 than the lengths of arg1 and arg2, evaluate at compile-time. */
7304 if (host_integerp (len, 1) && p1 && p2
7305 && compare_tree_int (len, strlen (p1) + 1) <= 0
7306 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7307 {
7308 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7309
7310 if (r > 0)
7311 return integer_one_node;
7312 else if (r < 0)
7313 return integer_minus_one_node;
7314 else
7315 return integer_zero_node;
7316 }
7317
7318 /* If len parameter is one, return an expression corresponding to
7319 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7320 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7321 {
7322 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7323 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7324 tree ind1 = fold_convert (integer_type_node,
7325 build1 (INDIRECT_REF, cst_uchar_node,
7326 fold_convert (cst_uchar_ptr_node,
7327 arg1)));
7328 tree ind2 = fold_convert (integer_type_node,
7329 build1 (INDIRECT_REF, cst_uchar_node,
7330 fold_convert (cst_uchar_ptr_node,
7331 arg2)));
7332 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7333 }
7334
7335 return 0;
7336 }
7337
7338 /* Fold function call to builtin strcmp. Return
7339 NULL_TREE if no simplification can be made. */
7340
7341 static tree
7342 fold_builtin_strcmp (tree arglist)
7343 {
7344 tree arg1, arg2;
7345 const char *p1, *p2;
7346
7347 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7348 return 0;
7349
7350 arg1 = TREE_VALUE (arglist);
7351 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7352
7353 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7354 if (operand_equal_p (arg1, arg2, 0))
7355 return integer_zero_node;
7356
7357 p1 = c_getstr (arg1);
7358 p2 = c_getstr (arg2);
7359
7360 if (p1 && p2)
7361 {
7362 const int i = strcmp (p1, p2);
7363 if (i < 0)
7364 return integer_minus_one_node;
7365 else if (i > 0)
7366 return integer_one_node;
7367 else
7368 return integer_zero_node;
7369 }
7370
7371 /* If the second arg is "", return *(const unsigned char*)arg1. */
7372 if (p2 && *p2 == '\0')
7373 {
7374 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7375 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7376 return fold_convert (integer_type_node,
7377 build1 (INDIRECT_REF, cst_uchar_node,
7378 fold_convert (cst_uchar_ptr_node,
7379 arg1)));
7380 }
7381
7382 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7383 if (p1 && *p1 == '\0')
7384 {
7385 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7386 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7387 tree temp = fold_convert (integer_type_node,
7388 build1 (INDIRECT_REF, cst_uchar_node,
7389 fold_convert (cst_uchar_ptr_node,
7390 arg2)));
7391 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7392 }
7393
7394 return 0;
7395 }
7396
7397 /* Fold function call to builtin strncmp. Return
7398 NULL_TREE if no simplification can be made. */
7399
7400 static tree
7401 fold_builtin_strncmp (tree arglist)
7402 {
7403 tree arg1, arg2, len;
7404 const char *p1, *p2;
7405
7406 if (!validate_arglist (arglist,
7407 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7408 return 0;
7409
7410 arg1 = TREE_VALUE (arglist);
7411 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7412 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7413
7414 /* If the LEN parameter is zero, return zero. */
7415 if (integer_zerop (len))
7416 return omit_two_operands (integer_type_node, integer_zero_node,
7417 arg1, arg2);
7418
7419 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7420 if (operand_equal_p (arg1, arg2, 0))
7421 return omit_one_operand (integer_type_node, integer_zero_node, len);
7422
7423 p1 = c_getstr (arg1);
7424 p2 = c_getstr (arg2);
7425
7426 if (host_integerp (len, 1) && p1 && p2)
7427 {
7428 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7429 if (i > 0)
7430 return integer_one_node;
7431 else if (i < 0)
7432 return integer_minus_one_node;
7433 else
7434 return integer_zero_node;
7435 }
7436
7437 /* If the second arg is "", and the length is greater than zero,
7438 return *(const unsigned char*)arg1. */
7439 if (p2 && *p2 == '\0'
7440 && TREE_CODE (len) == INTEGER_CST
7441 && tree_int_cst_sgn (len) == 1)
7442 {
7443 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7444 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7445 return fold_convert (integer_type_node,
7446 build1 (INDIRECT_REF, cst_uchar_node,
7447 fold_convert (cst_uchar_ptr_node,
7448 arg1)));
7449 }
7450
7451 /* If the first arg is "", and the length is greater than zero,
7452 return -*(const unsigned char*)arg2. */
7453 if (p1 && *p1 == '\0'
7454 && TREE_CODE (len) == INTEGER_CST
7455 && tree_int_cst_sgn (len) == 1)
7456 {
7457 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7458 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7459 tree temp = fold_convert (integer_type_node,
7460 build1 (INDIRECT_REF, cst_uchar_node,
7461 fold_convert (cst_uchar_ptr_node,
7462 arg2)));
7463 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7464 }
7465
7466 /* If len parameter is one, return an expression corresponding to
7467 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7468 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7469 {
7470 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7471 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7472 tree ind1 = fold_convert (integer_type_node,
7473 build1 (INDIRECT_REF, cst_uchar_node,
7474 fold_convert (cst_uchar_ptr_node,
7475 arg1)));
7476 tree ind2 = fold_convert (integer_type_node,
7477 build1 (INDIRECT_REF, cst_uchar_node,
7478 fold_convert (cst_uchar_ptr_node,
7479 arg2)));
7480 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7481 }
7482
7483 return 0;
7484 }
7485
7486 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7487 NULL_TREE if no simplification can be made. */
7488
7489 static tree
7490 fold_builtin_signbit (tree exp)
7491 {
7492 tree arglist = TREE_OPERAND (exp, 1);
7493 tree arg, temp;
7494
7495 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7496 return NULL_TREE;
7497
7498 arg = TREE_VALUE (arglist);
7499
7500 /* If ARG is a compile-time constant, determine the result. */
7501 if (TREE_CODE (arg) == REAL_CST
7502 && !TREE_CONSTANT_OVERFLOW (arg))
7503 {
7504 REAL_VALUE_TYPE c;
7505
7506 c = TREE_REAL_CST (arg);
7507 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7508 return fold_convert (TREE_TYPE (exp), temp);
7509 }
7510
7511 /* If ARG is non-negative, the result is always zero. */
7512 if (tree_expr_nonnegative_p (arg))
7513 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7514
7515 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7516 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7517 return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7518 build_real (TREE_TYPE (arg), dconst0)));
7519
7520 return NULL_TREE;
7521 }
7522
7523 /* Fold function call to builtin copysign, copysignf or copysignl.
7524 Return NULL_TREE if no simplification can be made. */
7525
7526 static tree
7527 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7528 {
7529 tree arg1, arg2, tem;
7530
7531 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7532 return NULL_TREE;
7533
7534 arg1 = TREE_VALUE (arglist);
7535 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7536
7537 /* copysign(X,X) is X. */
7538 if (operand_equal_p (arg1, arg2, 0))
7539 return fold_convert (type, arg1);
7540
7541 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7542 if (TREE_CODE (arg1) == REAL_CST
7543 && TREE_CODE (arg2) == REAL_CST
7544 && !TREE_CONSTANT_OVERFLOW (arg1)
7545 && !TREE_CONSTANT_OVERFLOW (arg2))
7546 {
7547 REAL_VALUE_TYPE c1, c2;
7548
7549 c1 = TREE_REAL_CST (arg1);
7550 c2 = TREE_REAL_CST (arg2);
7551 real_copysign (&c1, &c2);
7552 return build_real (type, c1);
7553 c1.sign = c2.sign;
7554 }
7555
7556 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7557 Remember to evaluate Y for side-effects. */
7558 if (tree_expr_nonnegative_p (arg2))
7559 return omit_one_operand (type,
7560 fold (build1 (ABS_EXPR, type, arg1)),
7561 arg2);
7562
7563 /* Strip sign changing operations for the first argument. */
7564 tem = fold_strip_sign_ops (arg1);
7565 if (tem)
7566 {
7567 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7568 return build_function_call_expr (fndecl, arglist);
7569 }
7570
7571 return NULL_TREE;
7572 }
7573
7574 /* Fold a call to builtin isascii. */
7575
7576 static tree
7577 fold_builtin_isascii (tree arglist)
7578 {
7579 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7580 return 0;
7581 else
7582 {
7583 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7584 tree arg = TREE_VALUE (arglist);
7585
7586 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7587 build_int_cst (NULL_TREE,
7588 ~ (unsigned HOST_WIDE_INT) 0x7f));
7589 arg = fold (build2 (EQ_EXPR, integer_type_node,
7590 arg, integer_zero_node));
7591
7592 if (in_gimple_form && !TREE_CONSTANT (arg))
7593 return NULL_TREE;
7594 else
7595 return arg;
7596 }
7597 }
7598
7599 /* Fold a call to builtin toascii. */
7600
7601 static tree
7602 fold_builtin_toascii (tree arglist)
7603 {
7604 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7605 return 0;
7606 else
7607 {
7608 /* Transform toascii(c) -> (c & 0x7f). */
7609 tree arg = TREE_VALUE (arglist);
7610
7611 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7612 build_int_cst (NULL_TREE, 0x7f)));
7613 }
7614 }
7615
7616 /* Fold a call to builtin isdigit. */
7617
7618 static tree
7619 fold_builtin_isdigit (tree arglist)
7620 {
7621 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7622 return 0;
7623 else
7624 {
7625 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7626 /* According to the C standard, isdigit is unaffected by locale. */
7627 tree arg = TREE_VALUE (arglist);
7628 arg = fold_convert (unsigned_type_node, arg);
7629 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7630 build_int_cst (unsigned_type_node, TARGET_DIGIT0));
7631 arg = build2 (LE_EXPR, integer_type_node, arg,
7632 build_int_cst (unsigned_type_node, 9));
7633 arg = fold (arg);
7634 if (in_gimple_form && !TREE_CONSTANT (arg))
7635 return NULL_TREE;
7636 else
7637 return arg;
7638 }
7639 }
7640
7641 /* Fold a call to fabs, fabsf or fabsl. */
7642
7643 static tree
7644 fold_builtin_fabs (tree arglist, tree type)
7645 {
7646 tree arg;
7647
7648 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7649 return 0;
7650
7651 arg = TREE_VALUE (arglist);
7652 arg = fold_convert (type, arg);
7653 if (TREE_CODE (arg) == REAL_CST)
7654 return fold_abs_const (arg, type);
7655 return fold (build1 (ABS_EXPR, type, arg));
7656 }
7657
7658 /* Fold a call to abs, labs, llabs or imaxabs. */
7659
7660 static tree
7661 fold_builtin_abs (tree arglist, tree type)
7662 {
7663 tree arg;
7664
7665 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7666 return 0;
7667
7668 arg = TREE_VALUE (arglist);
7669 arg = fold_convert (type, arg);
7670 if (TREE_CODE (arg) == INTEGER_CST)
7671 return fold_abs_const (arg, type);
7672 return fold (build1 (ABS_EXPR, type, arg));
7673 }
7674
7675 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7676 EXP is the CALL_EXPR for the call. */
7677
7678 static tree
7679 fold_builtin_classify (tree exp, int builtin_index)
7680 {
7681 tree fndecl = get_callee_fndecl (exp);
7682 tree arglist = TREE_OPERAND (exp, 1);
7683 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7684 tree arg;
7685 REAL_VALUE_TYPE r;
7686
7687 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7688 {
7689 /* Check that we have exactly one argument. */
7690 if (arglist == 0)
7691 {
7692 error ("too few arguments to function %qs",
7693 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7694 return error_mark_node;
7695 }
7696 else if (TREE_CHAIN (arglist) != 0)
7697 {
7698 error ("too many arguments to function %qs",
7699 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7700 return error_mark_node;
7701 }
7702 else
7703 {
7704 error ("non-floating-point argument to function %qs",
7705 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7706 return error_mark_node;
7707 }
7708 }
7709
7710 arg = TREE_VALUE (arglist);
7711 switch (builtin_index)
7712 {
7713 case BUILT_IN_ISINF:
7714 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7715 return omit_one_operand (type, integer_zero_node, arg);
7716
7717 if (TREE_CODE (arg) == REAL_CST)
7718 {
7719 r = TREE_REAL_CST (arg);
7720 if (real_isinf (&r))
7721 return real_compare (GT_EXPR, &r, &dconst0)
7722 ? integer_one_node : integer_minus_one_node;
7723 else
7724 return integer_zero_node;
7725 }
7726
7727 return NULL_TREE;
7728
7729 case BUILT_IN_FINITE:
7730 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7731 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7732 return omit_one_operand (type, integer_zero_node, arg);
7733
7734 if (TREE_CODE (arg) == REAL_CST)
7735 {
7736 r = TREE_REAL_CST (arg);
7737 return real_isinf (&r) || real_isnan (&r)
7738 ? integer_zero_node : integer_one_node;
7739 }
7740
7741 return NULL_TREE;
7742
7743 case BUILT_IN_ISNAN:
7744 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7745 return omit_one_operand (type, integer_zero_node, arg);
7746
7747 if (TREE_CODE (arg) == REAL_CST)
7748 {
7749 r = TREE_REAL_CST (arg);
7750 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7751 }
7752
7753 arg = builtin_save_expr (arg);
7754 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7755
7756 default:
7757 gcc_unreachable ();
7758 }
7759 }
7760
7761 /* Fold a call to an unordered comparison function such as
7762 __builtin_isgreater(). EXP is the CALL_EXPR for the call.
7763 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7764 the opposite of the desired result. UNORDERED_CODE is used
7765 for modes that can hold NaNs and ORDERED_CODE is used for
7766 the rest. */
7767
7768 static tree
7769 fold_builtin_unordered_cmp (tree exp,
7770 enum tree_code unordered_code,
7771 enum tree_code ordered_code)
7772 {
7773 tree fndecl = get_callee_fndecl (exp);
7774 tree arglist = TREE_OPERAND (exp, 1);
7775 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7776 enum tree_code code;
7777 tree arg0, arg1;
7778 tree type0, type1;
7779 enum tree_code code0, code1;
7780 tree cmp_type = NULL_TREE;
7781
7782 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7783 {
7784 /* Check that we have exactly two arguments. */
7785 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7786 {
7787 error ("too few arguments to function %qs",
7788 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7789 return error_mark_node;
7790 }
7791 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7792 {
7793 error ("too many arguments to function %qs",
7794 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7795 return error_mark_node;
7796 }
7797 }
7798
7799 arg0 = TREE_VALUE (arglist);
7800 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7801
7802 type0 = TREE_TYPE (arg0);
7803 type1 = TREE_TYPE (arg1);
7804
7805 code0 = TREE_CODE (type0);
7806 code1 = TREE_CODE (type1);
7807
7808 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7809 /* Choose the wider of two real types. */
7810 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7811 ? type0 : type1;
7812 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7813 cmp_type = type0;
7814 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7815 cmp_type = type1;
7816 else
7817 {
7818 error ("non-floating-point argument to function %qs",
7819 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7820 return error_mark_node;
7821 }
7822
7823 arg0 = fold_convert (cmp_type, arg0);
7824 arg1 = fold_convert (cmp_type, arg1);
7825
7826 if (unordered_code == UNORDERED_EXPR)
7827 {
7828 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7829 return omit_two_operands (type, integer_zero_node, arg0, arg1);
7830 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
7831 }
7832
7833 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
7834 : ordered_code;
7835 return fold (build1 (TRUTH_NOT_EXPR, type,
7836 fold (build2 (code, type, arg0, arg1))));
7837 }
7838
7839 /* Used by constant folding to simplify calls to builtin functions. EXP is
7840 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
7841 result of the function call is ignored. This function returns NULL_TREE
7842 if no simplification was possible. */
7843
7844 static tree
7845 fold_builtin_1 (tree exp, bool ignore)
7846 {
7847 tree fndecl = get_callee_fndecl (exp);
7848 tree arglist = TREE_OPERAND (exp, 1);
7849 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7850
7851 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7852 return targetm.fold_builtin (exp, ignore);
7853
7854 switch (DECL_FUNCTION_CODE (fndecl))
7855 {
7856 case BUILT_IN_FPUTS:
7857 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
7858
7859 case BUILT_IN_FPUTS_UNLOCKED:
7860 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
7861
7862 case BUILT_IN_STRSTR:
7863 return fold_builtin_strstr (arglist, type);
7864
7865 case BUILT_IN_STRCAT:
7866 return fold_builtin_strcat (arglist);
7867
7868 case BUILT_IN_STRNCAT:
7869 return fold_builtin_strncat (arglist);
7870
7871 case BUILT_IN_STRSPN:
7872 return fold_builtin_strspn (arglist);
7873
7874 case BUILT_IN_STRCSPN:
7875 return fold_builtin_strcspn (arglist);
7876
7877 case BUILT_IN_STRCHR:
7878 case BUILT_IN_INDEX:
7879 return fold_builtin_strchr (arglist, type);
7880
7881 case BUILT_IN_STRRCHR:
7882 case BUILT_IN_RINDEX:
7883 return fold_builtin_strrchr (arglist, type);
7884
7885 case BUILT_IN_STRCPY:
7886 return fold_builtin_strcpy (exp, NULL_TREE);
7887
7888 case BUILT_IN_STRNCPY:
7889 return fold_builtin_strncpy (exp, NULL_TREE);
7890
7891 case BUILT_IN_STRCMP:
7892 return fold_builtin_strcmp (arglist);
7893
7894 case BUILT_IN_STRNCMP:
7895 return fold_builtin_strncmp (arglist);
7896
7897 case BUILT_IN_STRPBRK:
7898 return fold_builtin_strpbrk (arglist, type);
7899
7900 case BUILT_IN_BCMP:
7901 case BUILT_IN_MEMCMP:
7902 return fold_builtin_memcmp (arglist);
7903
7904 case BUILT_IN_SPRINTF:
7905 return fold_builtin_sprintf (arglist, ignore);
7906
7907 case BUILT_IN_CONSTANT_P:
7908 {
7909 tree val;
7910
7911 val = fold_builtin_constant_p (arglist);
7912 /* Gimplification will pull the CALL_EXPR for the builtin out of
7913 an if condition. When not optimizing, we'll not CSE it back.
7914 To avoid link error types of regressions, return false now. */
7915 if (!val && !optimize)
7916 val = integer_zero_node;
7917
7918 return val;
7919 }
7920
7921 case BUILT_IN_EXPECT:
7922 return fold_builtin_expect (arglist);
7923
7924 case BUILT_IN_CLASSIFY_TYPE:
7925 return fold_builtin_classify_type (arglist);
7926
7927 case BUILT_IN_STRLEN:
7928 return fold_builtin_strlen (arglist);
7929
7930 case BUILT_IN_FABS:
7931 case BUILT_IN_FABSF:
7932 case BUILT_IN_FABSL:
7933 return fold_builtin_fabs (arglist, type);
7934
7935 case BUILT_IN_ABS:
7936 case BUILT_IN_LABS:
7937 case BUILT_IN_LLABS:
7938 case BUILT_IN_IMAXABS:
7939 return fold_builtin_abs (arglist, type);
7940
7941 case BUILT_IN_CONJ:
7942 case BUILT_IN_CONJF:
7943 case BUILT_IN_CONJL:
7944 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7945 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
7946 break;
7947
7948 case BUILT_IN_CREAL:
7949 case BUILT_IN_CREALF:
7950 case BUILT_IN_CREALL:
7951 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7952 return non_lvalue (fold (build1 (REALPART_EXPR, type,
7953 TREE_VALUE (arglist))));
7954 break;
7955
7956 case BUILT_IN_CIMAG:
7957 case BUILT_IN_CIMAGF:
7958 case BUILT_IN_CIMAGL:
7959 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7960 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
7961 TREE_VALUE (arglist))));
7962 break;
7963
7964 case BUILT_IN_CABS:
7965 case BUILT_IN_CABSF:
7966 case BUILT_IN_CABSL:
7967 return fold_builtin_cabs (arglist, type);
7968
7969 case BUILT_IN_SQRT:
7970 case BUILT_IN_SQRTF:
7971 case BUILT_IN_SQRTL:
7972 return fold_builtin_sqrt (arglist, type);
7973
7974 case BUILT_IN_CBRT:
7975 case BUILT_IN_CBRTF:
7976 case BUILT_IN_CBRTL:
7977 return fold_builtin_cbrt (arglist, type);
7978
7979 case BUILT_IN_SIN:
7980 case BUILT_IN_SINF:
7981 case BUILT_IN_SINL:
7982 return fold_builtin_sin (arglist);
7983
7984 case BUILT_IN_COS:
7985 case BUILT_IN_COSF:
7986 case BUILT_IN_COSL:
7987 return fold_builtin_cos (arglist, type, fndecl);
7988
7989 case BUILT_IN_EXP:
7990 case BUILT_IN_EXPF:
7991 case BUILT_IN_EXPL:
7992 return fold_builtin_exponent (exp, &dconste);
7993
7994 case BUILT_IN_EXP2:
7995 case BUILT_IN_EXP2F:
7996 case BUILT_IN_EXP2L:
7997 return fold_builtin_exponent (exp, &dconst2);
7998
7999 case BUILT_IN_EXP10:
8000 case BUILT_IN_EXP10F:
8001 case BUILT_IN_EXP10L:
8002 case BUILT_IN_POW10:
8003 case BUILT_IN_POW10F:
8004 case BUILT_IN_POW10L:
8005 return fold_builtin_exponent (exp, &dconst10);
8006
8007 case BUILT_IN_LOG:
8008 case BUILT_IN_LOGF:
8009 case BUILT_IN_LOGL:
8010 return fold_builtin_logarithm (exp, &dconste);
8011
8012 case BUILT_IN_LOG2:
8013 case BUILT_IN_LOG2F:
8014 case BUILT_IN_LOG2L:
8015 return fold_builtin_logarithm (exp, &dconst2);
8016
8017 case BUILT_IN_LOG10:
8018 case BUILT_IN_LOG10F:
8019 case BUILT_IN_LOG10L:
8020 return fold_builtin_logarithm (exp, &dconst10);
8021
8022 case BUILT_IN_TAN:
8023 case BUILT_IN_TANF:
8024 case BUILT_IN_TANL:
8025 return fold_builtin_tan (arglist);
8026
8027 case BUILT_IN_ATAN:
8028 case BUILT_IN_ATANF:
8029 case BUILT_IN_ATANL:
8030 return fold_builtin_atan (arglist, type);
8031
8032 case BUILT_IN_POW:
8033 case BUILT_IN_POWF:
8034 case BUILT_IN_POWL:
8035 return fold_builtin_pow (fndecl, arglist, type);
8036
8037 case BUILT_IN_POWI:
8038 case BUILT_IN_POWIF:
8039 case BUILT_IN_POWIL:
8040 return fold_builtin_powi (fndecl, arglist, type);
8041
8042 case BUILT_IN_INF:
8043 case BUILT_IN_INFF:
8044 case BUILT_IN_INFL:
8045 return fold_builtin_inf (type, true);
8046
8047 case BUILT_IN_HUGE_VAL:
8048 case BUILT_IN_HUGE_VALF:
8049 case BUILT_IN_HUGE_VALL:
8050 return fold_builtin_inf (type, false);
8051
8052 case BUILT_IN_NAN:
8053 case BUILT_IN_NANF:
8054 case BUILT_IN_NANL:
8055 return fold_builtin_nan (arglist, type, true);
8056
8057 case BUILT_IN_NANS:
8058 case BUILT_IN_NANSF:
8059 case BUILT_IN_NANSL:
8060 return fold_builtin_nan (arglist, type, false);
8061
8062 case BUILT_IN_FLOOR:
8063 case BUILT_IN_FLOORF:
8064 case BUILT_IN_FLOORL:
8065 return fold_builtin_floor (exp);
8066
8067 case BUILT_IN_CEIL:
8068 case BUILT_IN_CEILF:
8069 case BUILT_IN_CEILL:
8070 return fold_builtin_ceil (exp);
8071
8072 case BUILT_IN_TRUNC:
8073 case BUILT_IN_TRUNCF:
8074 case BUILT_IN_TRUNCL:
8075 return fold_builtin_trunc (exp);
8076
8077 case BUILT_IN_ROUND:
8078 case BUILT_IN_ROUNDF:
8079 case BUILT_IN_ROUNDL:
8080 return fold_builtin_round (exp);
8081
8082 case BUILT_IN_NEARBYINT:
8083 case BUILT_IN_NEARBYINTF:
8084 case BUILT_IN_NEARBYINTL:
8085 case BUILT_IN_RINT:
8086 case BUILT_IN_RINTF:
8087 case BUILT_IN_RINTL:
8088 return fold_trunc_transparent_mathfn (exp);
8089
8090 case BUILT_IN_LROUND:
8091 case BUILT_IN_LROUNDF:
8092 case BUILT_IN_LROUNDL:
8093 case BUILT_IN_LLROUND:
8094 case BUILT_IN_LLROUNDF:
8095 case BUILT_IN_LLROUNDL:
8096 return fold_builtin_lround (exp);
8097
8098 case BUILT_IN_LRINT:
8099 case BUILT_IN_LRINTF:
8100 case BUILT_IN_LRINTL:
8101 case BUILT_IN_LLRINT:
8102 case BUILT_IN_LLRINTF:
8103 case BUILT_IN_LLRINTL:
8104 return fold_fixed_mathfn (exp);
8105
8106 case BUILT_IN_FFS:
8107 case BUILT_IN_FFSL:
8108 case BUILT_IN_FFSLL:
8109 case BUILT_IN_CLZ:
8110 case BUILT_IN_CLZL:
8111 case BUILT_IN_CLZLL:
8112 case BUILT_IN_CTZ:
8113 case BUILT_IN_CTZL:
8114 case BUILT_IN_CTZLL:
8115 case BUILT_IN_POPCOUNT:
8116 case BUILT_IN_POPCOUNTL:
8117 case BUILT_IN_POPCOUNTLL:
8118 case BUILT_IN_PARITY:
8119 case BUILT_IN_PARITYL:
8120 case BUILT_IN_PARITYLL:
8121 return fold_builtin_bitop (exp);
8122
8123 case BUILT_IN_MEMCPY:
8124 return fold_builtin_memcpy (exp);
8125
8126 case BUILT_IN_MEMPCPY:
8127 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8128
8129 case BUILT_IN_MEMMOVE:
8130 return fold_builtin_memmove (arglist, type);
8131
8132 case BUILT_IN_SIGNBIT:
8133 case BUILT_IN_SIGNBITF:
8134 case BUILT_IN_SIGNBITL:
8135 return fold_builtin_signbit (exp);
8136
8137 case BUILT_IN_ISASCII:
8138 return fold_builtin_isascii (arglist);
8139
8140 case BUILT_IN_TOASCII:
8141 return fold_builtin_toascii (arglist);
8142
8143 case BUILT_IN_ISDIGIT:
8144 return fold_builtin_isdigit (arglist);
8145
8146 case BUILT_IN_COPYSIGN:
8147 case BUILT_IN_COPYSIGNF:
8148 case BUILT_IN_COPYSIGNL:
8149 return fold_builtin_copysign (fndecl, arglist, type);
8150
8151 case BUILT_IN_FINITE:
8152 case BUILT_IN_FINITEF:
8153 case BUILT_IN_FINITEL:
8154 return fold_builtin_classify (exp, BUILT_IN_FINITE);
8155
8156 case BUILT_IN_ISINF:
8157 case BUILT_IN_ISINFF:
8158 case BUILT_IN_ISINFL:
8159 return fold_builtin_classify (exp, BUILT_IN_ISINF);
8160
8161 case BUILT_IN_ISNAN:
8162 case BUILT_IN_ISNANF:
8163 case BUILT_IN_ISNANL:
8164 return fold_builtin_classify (exp, BUILT_IN_ISNAN);
8165
8166 case BUILT_IN_ISGREATER:
8167 return fold_builtin_unordered_cmp (exp, UNLE_EXPR, LE_EXPR);
8168 case BUILT_IN_ISGREATEREQUAL:
8169 return fold_builtin_unordered_cmp (exp, UNLT_EXPR, LT_EXPR);
8170 case BUILT_IN_ISLESS:
8171 return fold_builtin_unordered_cmp (exp, UNGE_EXPR, GE_EXPR);
8172 case BUILT_IN_ISLESSEQUAL:
8173 return fold_builtin_unordered_cmp (exp, UNGT_EXPR, GT_EXPR);
8174 case BUILT_IN_ISLESSGREATER:
8175 return fold_builtin_unordered_cmp (exp, UNEQ_EXPR, EQ_EXPR);
8176 case BUILT_IN_ISUNORDERED:
8177 return fold_builtin_unordered_cmp (exp, UNORDERED_EXPR, NOP_EXPR);
8178
8179 /* We do the folding for va_start in the expander. */
8180 case BUILT_IN_VA_START:
8181 break;
8182
8183 default:
8184 break;
8185 }
8186
8187 return 0;
8188 }
8189
8190 /* A wrapper function for builtin folding that prevents warnings for
8191 "statement without effect" and the like, caused by removing the
8192 call node earlier than the warning is generated. */
8193
8194 tree
8195 fold_builtin (tree exp, bool ignore)
8196 {
8197 exp = fold_builtin_1 (exp, ignore);
8198 if (exp)
8199 {
8200 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8201 if (CONSTANT_CLASS_P (exp))
8202 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8203 TREE_NO_WARNING (exp) = 1;
8204 }
8205
8206 return exp;
8207 }
8208
8209 /* Conveniently construct a function call expression. */
8210
8211 tree
8212 build_function_call_expr (tree fn, tree arglist)
8213 {
8214 tree call_expr;
8215
8216 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8217 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8218 call_expr, arglist, NULL_TREE);
8219 return fold (call_expr);
8220 }
8221
8222 /* This function validates the types of a function call argument list
8223 represented as a tree chain of parameters against a specified list
8224 of tree_codes. If the last specifier is a 0, that represents an
8225 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8226
8227 static int
8228 validate_arglist (tree arglist, ...)
8229 {
8230 enum tree_code code;
8231 int res = 0;
8232 va_list ap;
8233
8234 va_start (ap, arglist);
8235
8236 do
8237 {
8238 code = va_arg (ap, enum tree_code);
8239 switch (code)
8240 {
8241 case 0:
8242 /* This signifies an ellipses, any further arguments are all ok. */
8243 res = 1;
8244 goto end;
8245 case VOID_TYPE:
8246 /* This signifies an endlink, if no arguments remain, return
8247 true, otherwise return false. */
8248 res = arglist == 0;
8249 goto end;
8250 default:
8251 /* If no parameters remain or the parameter's code does not
8252 match the specified code, return false. Otherwise continue
8253 checking any remaining arguments. */
8254 if (arglist == 0)
8255 goto end;
8256 if (code == POINTER_TYPE)
8257 {
8258 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8259 goto end;
8260 }
8261 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8262 goto end;
8263 break;
8264 }
8265 arglist = TREE_CHAIN (arglist);
8266 }
8267 while (1);
8268
8269 /* We need gotos here since we can only have one VA_CLOSE in a
8270 function. */
8271 end: ;
8272 va_end (ap);
8273
8274 return res;
8275 }
8276
8277 /* Default target-specific builtin expander that does nothing. */
8278
8279 rtx
8280 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8281 rtx target ATTRIBUTE_UNUSED,
8282 rtx subtarget ATTRIBUTE_UNUSED,
8283 enum machine_mode mode ATTRIBUTE_UNUSED,
8284 int ignore ATTRIBUTE_UNUSED)
8285 {
8286 return NULL_RTX;
8287 }
8288
8289 /* Returns true is EXP represents data that would potentially reside
8290 in a readonly section. */
8291
8292 static bool
8293 readonly_data_expr (tree exp)
8294 {
8295 STRIP_NOPS (exp);
8296
8297 if (TREE_CODE (exp) != ADDR_EXPR)
8298 return false;
8299
8300 exp = get_base_address (TREE_OPERAND (exp, 0));
8301 if (!exp)
8302 return false;
8303
8304 /* Make sure we call decl_readonly_section only for trees it
8305 can handle (since it returns true for everything it doesn't
8306 understand). */
8307 if (TREE_CODE (exp) == STRING_CST
8308 || TREE_CODE (exp) == CONSTRUCTOR
8309 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8310 return decl_readonly_section (exp, 0);
8311 else
8312 return false;
8313 }
8314
8315 /* Simplify a call to the strstr builtin.
8316
8317 Return 0 if no simplification was possible, otherwise return the
8318 simplified form of the call as a tree.
8319
8320 The simplified form may be a constant or other expression which
8321 computes the same value, but in a more efficient manner (including
8322 calls to other builtin functions).
8323
8324 The call may contain arguments which need to be evaluated, but
8325 which are not useful to determine the result of the call. In
8326 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8327 COMPOUND_EXPR will be an argument which must be evaluated.
8328 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8329 COMPOUND_EXPR in the chain will contain the tree for the simplified
8330 form of the builtin function call. */
8331
8332 static tree
8333 fold_builtin_strstr (tree arglist, tree type)
8334 {
8335 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8336 return 0;
8337 else
8338 {
8339 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8340 tree fn;
8341 const char *p1, *p2;
8342
8343 p2 = c_getstr (s2);
8344 if (p2 == NULL)
8345 return 0;
8346
8347 p1 = c_getstr (s1);
8348 if (p1 != NULL)
8349 {
8350 const char *r = strstr (p1, p2);
8351 tree tem;
8352
8353 if (r == NULL)
8354 return build_int_cst (TREE_TYPE (s1), 0);
8355
8356 /* Return an offset into the constant string argument. */
8357 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8358 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8359 return fold_convert (type, tem);
8360 }
8361
8362 if (p2[0] == '\0')
8363 return s1;
8364
8365 if (p2[1] != '\0')
8366 return 0;
8367
8368 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8369 if (!fn)
8370 return 0;
8371
8372 /* New argument list transforming strstr(s1, s2) to
8373 strchr(s1, s2[0]). */
8374 arglist = build_tree_list (NULL_TREE,
8375 build_int_cst (NULL_TREE, p2[0]));
8376 arglist = tree_cons (NULL_TREE, s1, arglist);
8377 return build_function_call_expr (fn, arglist);
8378 }
8379 }
8380
8381 /* Simplify a call to the strchr builtin.
8382
8383 Return 0 if no simplification was possible, otherwise return the
8384 simplified form of the call as a tree.
8385
8386 The simplified form may be a constant or other expression which
8387 computes the same value, but in a more efficient manner (including
8388 calls to other builtin functions).
8389
8390 The call may contain arguments which need to be evaluated, but
8391 which are not useful to determine the result of the call. In
8392 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8393 COMPOUND_EXPR will be an argument which must be evaluated.
8394 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8395 COMPOUND_EXPR in the chain will contain the tree for the simplified
8396 form of the builtin function call. */
8397
8398 static tree
8399 fold_builtin_strchr (tree arglist, tree type)
8400 {
8401 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8402 return 0;
8403 else
8404 {
8405 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8406 const char *p1;
8407
8408 if (TREE_CODE (s2) != INTEGER_CST)
8409 return 0;
8410
8411 p1 = c_getstr (s1);
8412 if (p1 != NULL)
8413 {
8414 char c;
8415 const char *r;
8416 tree tem;
8417
8418 if (target_char_cast (s2, &c))
8419 return 0;
8420
8421 r = strchr (p1, c);
8422
8423 if (r == NULL)
8424 return build_int_cst (TREE_TYPE (s1), 0);
8425
8426 /* Return an offset into the constant string argument. */
8427 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8428 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8429 return fold_convert (type, tem);
8430 }
8431 return 0;
8432 }
8433 }
8434
8435 /* Simplify a call to the strrchr builtin.
8436
8437 Return 0 if no simplification was possible, otherwise return the
8438 simplified form of the call as a tree.
8439
8440 The simplified form may be a constant or other expression which
8441 computes the same value, but in a more efficient manner (including
8442 calls to other builtin functions).
8443
8444 The call may contain arguments which need to be evaluated, but
8445 which are not useful to determine the result of the call. In
8446 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8447 COMPOUND_EXPR will be an argument which must be evaluated.
8448 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8449 COMPOUND_EXPR in the chain will contain the tree for the simplified
8450 form of the builtin function call. */
8451
8452 static tree
8453 fold_builtin_strrchr (tree arglist, tree type)
8454 {
8455 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8456 return 0;
8457 else
8458 {
8459 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8460 tree fn;
8461 const char *p1;
8462
8463 if (TREE_CODE (s2) != INTEGER_CST)
8464 return 0;
8465
8466 p1 = c_getstr (s1);
8467 if (p1 != NULL)
8468 {
8469 char c;
8470 const char *r;
8471 tree tem;
8472
8473 if (target_char_cast (s2, &c))
8474 return 0;
8475
8476 r = strrchr (p1, c);
8477
8478 if (r == NULL)
8479 return build_int_cst (TREE_TYPE (s1), 0);
8480
8481 /* Return an offset into the constant string argument. */
8482 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8483 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8484 return fold_convert (type, tem);
8485 }
8486
8487 if (! integer_zerop (s2))
8488 return 0;
8489
8490 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8491 if (!fn)
8492 return 0;
8493
8494 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8495 return build_function_call_expr (fn, arglist);
8496 }
8497 }
8498
8499 /* Simplify a call to the strpbrk builtin.
8500
8501 Return 0 if no simplification was possible, otherwise return the
8502 simplified form of the call as a tree.
8503
8504 The simplified form may be a constant or other expression which
8505 computes the same value, but in a more efficient manner (including
8506 calls to other builtin functions).
8507
8508 The call may contain arguments which need to be evaluated, but
8509 which are not useful to determine the result of the call. In
8510 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8511 COMPOUND_EXPR will be an argument which must be evaluated.
8512 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8513 COMPOUND_EXPR in the chain will contain the tree for the simplified
8514 form of the builtin function call. */
8515
8516 static tree
8517 fold_builtin_strpbrk (tree arglist, tree type)
8518 {
8519 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8520 return 0;
8521 else
8522 {
8523 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8524 tree fn;
8525 const char *p1, *p2;
8526
8527 p2 = c_getstr (s2);
8528 if (p2 == NULL)
8529 return 0;
8530
8531 p1 = c_getstr (s1);
8532 if (p1 != NULL)
8533 {
8534 const char *r = strpbrk (p1, p2);
8535 tree tem;
8536
8537 if (r == NULL)
8538 return build_int_cst (TREE_TYPE (s1), 0);
8539
8540 /* Return an offset into the constant string argument. */
8541 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8542 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8543 return fold_convert (type, tem);
8544 }
8545
8546 if (p2[0] == '\0')
8547 /* strpbrk(x, "") == NULL.
8548 Evaluate and ignore s1 in case it had side-effects. */
8549 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8550
8551 if (p2[1] != '\0')
8552 return 0; /* Really call strpbrk. */
8553
8554 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8555 if (!fn)
8556 return 0;
8557
8558 /* New argument list transforming strpbrk(s1, s2) to
8559 strchr(s1, s2[0]). */
8560 arglist = build_tree_list (NULL_TREE,
8561 build_int_cst (NULL_TREE, p2[0]));
8562 arglist = tree_cons (NULL_TREE, s1, arglist);
8563 return build_function_call_expr (fn, arglist);
8564 }
8565 }
8566
8567 /* Simplify a call to the strcat builtin.
8568
8569 Return 0 if no simplification was possible, otherwise return the
8570 simplified form of the call as a tree.
8571
8572 The simplified form may be a constant or other expression which
8573 computes the same value, but in a more efficient manner (including
8574 calls to other builtin functions).
8575
8576 The call may contain arguments which need to be evaluated, but
8577 which are not useful to determine the result of the call. In
8578 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8579 COMPOUND_EXPR will be an argument which must be evaluated.
8580 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8581 COMPOUND_EXPR in the chain will contain the tree for the simplified
8582 form of the builtin function call. */
8583
8584 static tree
8585 fold_builtin_strcat (tree arglist)
8586 {
8587 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8588 return 0;
8589 else
8590 {
8591 tree dst = TREE_VALUE (arglist),
8592 src = TREE_VALUE (TREE_CHAIN (arglist));
8593 const char *p = c_getstr (src);
8594
8595 /* If the string length is zero, return the dst parameter. */
8596 if (p && *p == '\0')
8597 return dst;
8598
8599 return 0;
8600 }
8601 }
8602
8603 /* Simplify a call to the strncat builtin.
8604
8605 Return 0 if no simplification was possible, otherwise return the
8606 simplified form of the call as a tree.
8607
8608 The simplified form may be a constant or other expression which
8609 computes the same value, but in a more efficient manner (including
8610 calls to other builtin functions).
8611
8612 The call may contain arguments which need to be evaluated, but
8613 which are not useful to determine the result of the call. In
8614 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8615 COMPOUND_EXPR will be an argument which must be evaluated.
8616 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8617 COMPOUND_EXPR in the chain will contain the tree for the simplified
8618 form of the builtin function call. */
8619
8620 static tree
8621 fold_builtin_strncat (tree arglist)
8622 {
8623 if (!validate_arglist (arglist,
8624 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8625 return 0;
8626 else
8627 {
8628 tree dst = TREE_VALUE (arglist);
8629 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8630 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8631 const char *p = c_getstr (src);
8632
8633 /* If the requested length is zero, or the src parameter string
8634 length is zero, return the dst parameter. */
8635 if (integer_zerop (len) || (p && *p == '\0'))
8636 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8637
8638 /* If the requested len is greater than or equal to the string
8639 length, call strcat. */
8640 if (TREE_CODE (len) == INTEGER_CST && p
8641 && compare_tree_int (len, strlen (p)) >= 0)
8642 {
8643 tree newarglist
8644 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8645 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8646
8647 /* If the replacement _DECL isn't initialized, don't do the
8648 transformation. */
8649 if (!fn)
8650 return 0;
8651
8652 return build_function_call_expr (fn, newarglist);
8653 }
8654 return 0;
8655 }
8656 }
8657
8658 /* Simplify a call to the strspn builtin.
8659
8660 Return 0 if no simplification was possible, otherwise return the
8661 simplified form of the call as a tree.
8662
8663 The simplified form may be a constant or other expression which
8664 computes the same value, but in a more efficient manner (including
8665 calls to other builtin functions).
8666
8667 The call may contain arguments which need to be evaluated, but
8668 which are not useful to determine the result of the call. In
8669 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8670 COMPOUND_EXPR will be an argument which must be evaluated.
8671 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8672 COMPOUND_EXPR in the chain will contain the tree for the simplified
8673 form of the builtin function call. */
8674
8675 static tree
8676 fold_builtin_strspn (tree arglist)
8677 {
8678 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8679 return 0;
8680 else
8681 {
8682 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8683 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8684
8685 /* If both arguments are constants, evaluate at compile-time. */
8686 if (p1 && p2)
8687 {
8688 const size_t r = strspn (p1, p2);
8689 return size_int (r);
8690 }
8691
8692 /* If either argument is "", return 0. */
8693 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8694 /* Evaluate and ignore both arguments in case either one has
8695 side-effects. */
8696 return omit_two_operands (integer_type_node, integer_zero_node,
8697 s1, s2);
8698 return 0;
8699 }
8700 }
8701
8702 /* Simplify a call to the strcspn builtin.
8703
8704 Return 0 if no simplification was possible, otherwise return the
8705 simplified form of the call as a tree.
8706
8707 The simplified form may be a constant or other expression which
8708 computes the same value, but in a more efficient manner (including
8709 calls to other builtin functions).
8710
8711 The call may contain arguments which need to be evaluated, but
8712 which are not useful to determine the result of the call. In
8713 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8714 COMPOUND_EXPR will be an argument which must be evaluated.
8715 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8716 COMPOUND_EXPR in the chain will contain the tree for the simplified
8717 form of the builtin function call. */
8718
8719 static tree
8720 fold_builtin_strcspn (tree arglist)
8721 {
8722 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8723 return 0;
8724 else
8725 {
8726 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8727 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8728
8729 /* If both arguments are constants, evaluate at compile-time. */
8730 if (p1 && p2)
8731 {
8732 const size_t r = strcspn (p1, p2);
8733 return size_int (r);
8734 }
8735
8736 /* If the first argument is "", return 0. */
8737 if (p1 && *p1 == '\0')
8738 {
8739 /* Evaluate and ignore argument s2 in case it has
8740 side-effects. */
8741 return omit_one_operand (integer_type_node,
8742 integer_zero_node, s2);
8743 }
8744
8745 /* If the second argument is "", return __builtin_strlen(s1). */
8746 if (p2 && *p2 == '\0')
8747 {
8748 tree newarglist = build_tree_list (NULL_TREE, s1),
8749 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8750
8751 /* If the replacement _DECL isn't initialized, don't do the
8752 transformation. */
8753 if (!fn)
8754 return 0;
8755
8756 return build_function_call_expr (fn, newarglist);
8757 }
8758 return 0;
8759 }
8760 }
8761
8762 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
8763 by the builtin will be ignored. UNLOCKED is true is true if this
8764 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
8765 the known length of the string. Return NULL_TREE if no simplification
8766 was possible. */
8767
8768 tree
8769 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
8770 {
8771 tree fn;
8772 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8773 : implicit_built_in_decls[BUILT_IN_FPUTC];
8774 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8775 : implicit_built_in_decls[BUILT_IN_FWRITE];
8776
8777 /* If the return value is used, or the replacement _DECL isn't
8778 initialized, don't do the transformation. */
8779 if (!ignore || !fn_fputc || !fn_fwrite)
8780 return 0;
8781
8782 /* Verify the arguments in the original call. */
8783 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8784 return 0;
8785
8786 if (! len)
8787 len = c_strlen (TREE_VALUE (arglist), 0);
8788
8789 /* Get the length of the string passed to fputs. If the length
8790 can't be determined, punt. */
8791 if (!len
8792 || TREE_CODE (len) != INTEGER_CST)
8793 return 0;
8794
8795 switch (compare_tree_int (len, 1))
8796 {
8797 case -1: /* length is 0, delete the call entirely . */
8798 return omit_one_operand (integer_type_node, integer_zero_node,
8799 TREE_VALUE (TREE_CHAIN (arglist)));
8800
8801 case 0: /* length is 1, call fputc. */
8802 {
8803 const char *p = c_getstr (TREE_VALUE (arglist));
8804
8805 if (p != NULL)
8806 {
8807 /* New argument list transforming fputs(string, stream) to
8808 fputc(string[0], stream). */
8809 arglist = build_tree_list (NULL_TREE,
8810 TREE_VALUE (TREE_CHAIN (arglist)));
8811 arglist = tree_cons (NULL_TREE,
8812 build_int_cst (NULL_TREE, p[0]),
8813 arglist);
8814 fn = fn_fputc;
8815 break;
8816 }
8817 }
8818 /* FALLTHROUGH */
8819 case 1: /* length is greater than 1, call fwrite. */
8820 {
8821 tree string_arg;
8822
8823 /* If optimizing for size keep fputs. */
8824 if (optimize_size)
8825 return 0;
8826 string_arg = TREE_VALUE (arglist);
8827 /* New argument list transforming fputs(string, stream) to
8828 fwrite(string, 1, len, stream). */
8829 arglist = build_tree_list (NULL_TREE,
8830 TREE_VALUE (TREE_CHAIN (arglist)));
8831 arglist = tree_cons (NULL_TREE, len, arglist);
8832 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8833 arglist = tree_cons (NULL_TREE, string_arg, arglist);
8834 fn = fn_fwrite;
8835 break;
8836 }
8837 default:
8838 gcc_unreachable ();
8839 }
8840
8841 /* These optimizations are only performed when the result is ignored,
8842 hence there's no need to cast the result to integer_type_node. */
8843 return build_function_call_expr (fn, arglist);
8844 }
8845
8846 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8847 produced. False otherwise. This is done so that we don't output the error
8848 or warning twice or three times. */
8849 bool
8850 fold_builtin_next_arg (tree arglist)
8851 {
8852 tree fntype = TREE_TYPE (current_function_decl);
8853
8854 if (TYPE_ARG_TYPES (fntype) == 0
8855 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8856 == void_type_node))
8857 {
8858 error ("%<va_start%> used in function with fixed args");
8859 return true;
8860 }
8861 else if (!arglist)
8862 {
8863 /* Evidently an out of date version of <stdarg.h>; can't validate
8864 va_start's second argument, but can still work as intended. */
8865 warning ("%<__builtin_next_arg%> called without an argument");
8866 return true;
8867 }
8868 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8869 when we checked the arguments and if needed issued a warning. */
8870 else if (!TREE_CHAIN (arglist)
8871 || !integer_zerop (TREE_VALUE (arglist))
8872 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
8873 || TREE_CHAIN (TREE_CHAIN (arglist)))
8874 {
8875 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8876 tree arg = TREE_VALUE (arglist);
8877
8878 if (TREE_CHAIN (arglist))
8879 {
8880 error ("%<va_start%> used with too many arguments");
8881 return true;
8882 }
8883
8884 /* Strip off all nops for the sake of the comparison. This
8885 is not quite the same as STRIP_NOPS. It does more.
8886 We must also strip off INDIRECT_EXPR for C++ reference
8887 parameters. */
8888 while (TREE_CODE (arg) == NOP_EXPR
8889 || TREE_CODE (arg) == CONVERT_EXPR
8890 || TREE_CODE (arg) == NON_LVALUE_EXPR
8891 || TREE_CODE (arg) == INDIRECT_REF)
8892 arg = TREE_OPERAND (arg, 0);
8893 if (arg != last_parm)
8894 {
8895 /* FIXME: Sometimes with the tree optimizers we can get the
8896 not the last argument even though the user used the last
8897 argument. We just warn and set the arg to be the last
8898 argument so that we will get wrong-code because of
8899 it. */
8900 warning ("second parameter of %<va_start%> not last named argument");
8901 }
8902 /* We want to verify the second parameter just once before the tree
8903 optimizers are run and then avoid keeping it in the tree,
8904 as otherwise we could warn even for correct code like:
8905 void foo (int i, ...)
8906 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
8907 TREE_VALUE (arglist) = integer_zero_node;
8908 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
8909 }
8910 return false;
8911 }
8912
8913
8914 /* Simplify a call to the sprintf builtin.
8915
8916 Return 0 if no simplification was possible, otherwise return the
8917 simplified form of the call as a tree. If IGNORED is true, it means that
8918 the caller does not use the returned value of the function. */
8919
8920 static tree
8921 fold_builtin_sprintf (tree arglist, int ignored)
8922 {
8923 tree call, retval, dest, fmt;
8924 const char *fmt_str = NULL;
8925
8926 /* Verify the required arguments in the original call. We deal with two
8927 types of sprintf() calls: 'sprintf (str, fmt)' and
8928 'sprintf (dest, "%s", orig)'. */
8929 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
8930 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
8931 VOID_TYPE))
8932 return NULL_TREE;
8933
8934 /* Get the destination string and the format specifier. */
8935 dest = TREE_VALUE (arglist);
8936 fmt = TREE_VALUE (TREE_CHAIN (arglist));
8937
8938 /* Check whether the format is a literal string constant. */
8939 fmt_str = c_getstr (fmt);
8940 if (fmt_str == NULL)
8941 return NULL_TREE;
8942
8943 call = NULL_TREE;
8944 retval = NULL_TREE;
8945
8946 /* If the format doesn't contain % args or %%, use strcpy. */
8947 if (strchr (fmt_str, '%') == NULL)
8948 {
8949 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
8950
8951 if (!fn)
8952 return NULL_TREE;
8953
8954 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
8955 'format' is known to contain no % formats. */
8956 arglist = build_tree_list (NULL_TREE, fmt);
8957 arglist = tree_cons (NULL_TREE, dest, arglist);
8958 call = build_function_call_expr (fn, arglist);
8959 if (!ignored)
8960 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
8961 }
8962
8963 /* If the format is "%s", use strcpy if the result isn't used. */
8964 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
8965 {
8966 tree fn, orig;
8967 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
8968
8969 if (!fn)
8970 return NULL_TREE;
8971
8972 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
8973 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8974 arglist = build_tree_list (NULL_TREE, orig);
8975 arglist = tree_cons (NULL_TREE, dest, arglist);
8976 if (!ignored)
8977 {
8978 retval = c_strlen (orig, 1);
8979 if (!retval || TREE_CODE (retval) != INTEGER_CST)
8980 return NULL_TREE;
8981 }
8982 call = build_function_call_expr (fn, arglist);
8983 }
8984
8985 if (call && retval)
8986 {
8987 retval = convert
8988 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
8989 retval);
8990 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
8991 }
8992 else
8993 return call;
8994 }