* doc/c-tree.texi (Function Bodies): Update HANDLER documentation.
[official-gcc.git] / gcc / builtins.c
bloba48c5af9a5437787c761ff85e54ab410f3f616ac
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
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 "flags.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "except.h"
34 #include "function.h"
35 #include "insn-config.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "libfuncs.h"
39 #include "recog.h"
40 #include "output.h"
41 #include "typeclass.h"
42 #include "toplev.h"
43 #include "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
48 #define CALLED_AS_BUILT_IN(NODE) \
49 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
51 /* Register mappings for target machines without register windows. */
52 #ifndef INCOMING_REGNO
53 #define INCOMING_REGNO(OUT) (OUT)
54 #endif
55 #ifndef OUTGOING_REGNO
56 #define OUTGOING_REGNO(IN) (IN)
57 #endif
59 #ifndef PAD_VARARGS_DOWN
60 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
61 #endif
63 /* Define the names of the builtin function types and codes. */
64 const char *const built_in_class_names[4]
65 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
67 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X,
68 const char *const built_in_names[(int) END_BUILTINS] =
70 #include "builtins.def"
72 #undef DEF_BUILTIN
74 /* Setup an array of _DECL trees, make sure each element is
75 initialized to NULL_TREE. */
76 tree built_in_decls[(int) END_BUILTINS];
77 /* Declarations used when constructing the builtin implicitly in the compiler.
78 It may be NULL_TREE when this is invalid (for instance runtime is not
79 required to implement the function call in all cases. */
80 tree implicit_built_in_decls[(int) END_BUILTINS];
82 static int get_pointer_alignment (tree, unsigned int);
83 static tree c_strlen (tree, int);
84 static const char *c_getstr (tree);
85 static rtx c_readstr (const char *, enum machine_mode);
86 static int target_char_cast (tree, char *);
87 static rtx get_memory_rtx (tree);
88 static tree build_string_literal (int, const char *);
89 static int apply_args_size (void);
90 static int apply_result_size (void);
91 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
92 static rtx result_vector (int, rtx);
93 #endif
94 static rtx expand_builtin_setjmp (tree, rtx);
95 static void expand_builtin_prefetch (tree);
96 static rtx expand_builtin_apply_args (void);
97 static rtx expand_builtin_apply_args_1 (void);
98 static rtx expand_builtin_apply (rtx, rtx, rtx);
99 static void expand_builtin_return (rtx);
100 static enum type_class type_to_class (tree);
101 static rtx expand_builtin_classify_type (tree);
102 static void expand_errno_check (tree, rtx);
103 static rtx expand_builtin_mathfn (tree, rtx, rtx);
104 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
105 static rtx expand_builtin_constant_p (tree, enum machine_mode);
106 static rtx expand_builtin_args_info (tree);
107 static rtx expand_builtin_next_arg (tree);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_strcat (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
120 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int);
121 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_bcopy (tree);
123 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
125 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
126 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
127 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
128 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
129 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
130 static rtx expand_builtin_bzero (tree);
131 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
133 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
136 static rtx expand_builtin_alloca (tree, rtx);
137 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
138 static rtx expand_builtin_frame_address (tree, tree);
139 static rtx expand_builtin_fputs (tree, rtx, bool);
140 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
141 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
142 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
143 static tree stabilize_va_list (tree, int);
144 static rtx expand_builtin_expect (tree, rtx);
145 static tree fold_builtin_constant_p (tree);
146 static tree fold_builtin_classify_type (tree);
147 static tree fold_builtin_inf (tree, int);
148 static tree fold_builtin_nan (tree, tree, int);
149 static int validate_arglist (tree, ...);
150 static bool integer_valued_real_p (tree);
151 static tree fold_trunc_transparent_mathfn (tree);
152 static bool readonly_data_expr (tree);
153 static rtx expand_builtin_fabs (tree, rtx, rtx);
154 static rtx expand_builtin_cabs (tree, rtx);
155 static tree fold_builtin_cabs (tree, tree, tree);
156 static tree fold_builtin_trunc (tree);
157 static tree fold_builtin_floor (tree);
158 static tree fold_builtin_ceil (tree);
159 static tree fold_builtin_bitop (tree);
160 static tree fold_builtin_memcpy (tree);
161 static tree fold_builtin_mempcpy (tree);
162 static tree fold_builtin_memmove (tree);
163 static tree fold_builtin_strcpy (tree);
164 static tree fold_builtin_strncpy (tree);
165 static tree fold_builtin_memcmp (tree);
166 static tree fold_builtin_strcmp (tree);
167 static tree fold_builtin_strncmp (tree);
169 /* Return the alignment in bits of EXP, a pointer valued expression.
170 But don't return more than MAX_ALIGN no matter what.
171 The alignment returned is, by default, the alignment of the thing that
172 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
174 Otherwise, look at the expression to see if we can do better, i.e., if the
175 expression is actually pointing at an object whose alignment is tighter. */
177 static int
178 get_pointer_alignment (tree exp, unsigned int max_align)
180 unsigned int align, inner;
182 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
183 return 0;
185 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
186 align = MIN (align, max_align);
188 while (1)
190 switch (TREE_CODE (exp))
192 case NOP_EXPR:
193 case CONVERT_EXPR:
194 case NON_LVALUE_EXPR:
195 exp = TREE_OPERAND (exp, 0);
196 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
197 return align;
199 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
200 align = MIN (inner, max_align);
201 break;
203 case PLUS_EXPR:
204 /* If sum of pointer + int, restrict our maximum alignment to that
205 imposed by the integer. If not, we can't do any better than
206 ALIGN. */
207 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
208 return align;
210 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
211 & (max_align / BITS_PER_UNIT - 1))
212 != 0)
213 max_align >>= 1;
215 exp = TREE_OPERAND (exp, 0);
216 break;
218 case ADDR_EXPR:
219 /* See what we are pointing at and look at its alignment. */
220 exp = TREE_OPERAND (exp, 0);
221 if (TREE_CODE (exp) == FUNCTION_DECL)
222 align = FUNCTION_BOUNDARY;
223 else if (DECL_P (exp))
224 align = DECL_ALIGN (exp);
225 #ifdef CONSTANT_ALIGNMENT
226 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
227 align = CONSTANT_ALIGNMENT (exp, align);
228 #endif
229 return MIN (align, max_align);
231 default:
232 return align;
237 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
238 way, because it could contain a zero byte in the middle.
239 TREE_STRING_LENGTH is the size of the character array, not the string.
241 ONLY_VALUE should be nonzero if the result is not going to be emitted
242 into the instruction stream and zero if it is going to be expanded.
243 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
244 is returned, otherwise NULL, since
245 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
246 evaluate the side-effects.
248 The value returned is of type `ssizetype'.
250 Unfortunately, string_constant can't access the values of const char
251 arrays with initializers, so neither can we do so here. */
253 static tree
254 c_strlen (tree src, int only_value)
256 tree offset_node;
257 HOST_WIDE_INT offset;
258 int max;
259 const char *ptr;
261 STRIP_NOPS (src);
262 if (TREE_CODE (src) == COND_EXPR
263 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
265 tree len1, len2;
267 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
268 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
269 if (tree_int_cst_equal (len1, len2))
270 return len1;
273 if (TREE_CODE (src) == COMPOUND_EXPR
274 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
275 return c_strlen (TREE_OPERAND (src, 1), only_value);
277 src = string_constant (src, &offset_node);
278 if (src == 0)
279 return 0;
281 max = TREE_STRING_LENGTH (src) - 1;
282 ptr = TREE_STRING_POINTER (src);
284 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
286 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
287 compute the offset to the following null if we don't know where to
288 start searching for it. */
289 int i;
291 for (i = 0; i < max; i++)
292 if (ptr[i] == 0)
293 return 0;
295 /* We don't know the starting offset, but we do know that the string
296 has no internal zero bytes. We can assume that the offset falls
297 within the bounds of the string; otherwise, the programmer deserves
298 what he gets. Subtract the offset from the length of the string,
299 and return that. This would perhaps not be valid if we were dealing
300 with named arrays in addition to literal string constants. */
302 return size_diffop (size_int (max), offset_node);
305 /* We have a known offset into the string. Start searching there for
306 a null character if we can represent it as a single HOST_WIDE_INT. */
307 if (offset_node == 0)
308 offset = 0;
309 else if (! host_integerp (offset_node, 0))
310 offset = -1;
311 else
312 offset = tree_low_cst (offset_node, 0);
314 /* If the offset is known to be out of bounds, warn, and call strlen at
315 runtime. */
316 if (offset < 0 || offset > max)
318 warning ("offset outside bounds of constant string");
319 return 0;
322 /* Use strlen to search for the first zero byte. Since any strings
323 constructed with build_string will have nulls appended, we win even
324 if we get handed something like (char[4])"abcd".
326 Since OFFSET is our starting index into the string, no further
327 calculation is needed. */
328 return ssize_int (strlen (ptr + offset));
331 /* Return a char pointer for a C string if it is a string constant
332 or sum of string constant and integer constant. */
334 static const char *
335 c_getstr (tree src)
337 tree offset_node;
339 src = string_constant (src, &offset_node);
340 if (src == 0)
341 return 0;
343 if (offset_node == 0)
344 return TREE_STRING_POINTER (src);
345 else if (!host_integerp (offset_node, 1)
346 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
347 return 0;
349 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
352 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
353 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
355 static rtx
356 c_readstr (const char *str, enum machine_mode mode)
358 HOST_WIDE_INT c[2];
359 HOST_WIDE_INT ch;
360 unsigned int i, j;
362 if (GET_MODE_CLASS (mode) != MODE_INT)
363 abort ();
364 c[0] = 0;
365 c[1] = 0;
366 ch = 1;
367 for (i = 0; i < GET_MODE_SIZE (mode); i++)
369 j = i;
370 if (WORDS_BIG_ENDIAN)
371 j = GET_MODE_SIZE (mode) - i - 1;
372 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
373 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
374 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
375 j *= BITS_PER_UNIT;
376 if (j > 2 * HOST_BITS_PER_WIDE_INT)
377 abort ();
378 if (ch)
379 ch = (unsigned char) str[i];
380 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
382 return immed_double_const (c[0], c[1], mode);
385 /* Cast a target constant CST to target CHAR and if that value fits into
386 host char type, return zero and put that value into variable pointed by
387 P. */
389 static int
390 target_char_cast (tree cst, char *p)
392 unsigned HOST_WIDE_INT val, hostval;
394 if (!host_integerp (cst, 1)
395 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
396 return 1;
398 val = tree_low_cst (cst, 1);
399 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
400 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
402 hostval = val;
403 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
404 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
406 if (val != hostval)
407 return 1;
409 *p = hostval;
410 return 0;
413 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
414 times to get the address of either a higher stack frame, or a return
415 address located within it (depending on FNDECL_CODE). */
418 expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
419 rtx tem)
421 int i;
423 /* Some machines need special handling before we can access
424 arbitrary frames. For example, on the sparc, we must first flush
425 all register windows to the stack. */
426 #ifdef SETUP_FRAME_ADDRESSES
427 if (count > 0)
428 SETUP_FRAME_ADDRESSES ();
429 #endif
431 /* On the sparc, the return address is not in the frame, it is in a
432 register. There is no way to access it off of the current frame
433 pointer, but it can be accessed off the previous frame pointer by
434 reading the value from the register window save area. */
435 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
436 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
437 count--;
438 #endif
440 /* Scan back COUNT frames to the specified frame. */
441 for (i = 0; i < count; i++)
443 /* Assume the dynamic chain pointer is in the word that the
444 frame address points to, unless otherwise specified. */
445 #ifdef DYNAMIC_CHAIN_ADDRESS
446 tem = DYNAMIC_CHAIN_ADDRESS (tem);
447 #endif
448 tem = memory_address (Pmode, tem);
449 tem = gen_rtx_MEM (Pmode, tem);
450 set_mem_alias_set (tem, get_frame_alias_set ());
451 tem = copy_to_reg (tem);
454 /* For __builtin_frame_address, return what we've got. */
455 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
456 return tem;
458 /* For __builtin_return_address, Get the return address from that
459 frame. */
460 #ifdef RETURN_ADDR_RTX
461 tem = RETURN_ADDR_RTX (count, tem);
462 #else
463 tem = memory_address (Pmode,
464 plus_constant (tem, GET_MODE_SIZE (Pmode)));
465 tem = gen_rtx_MEM (Pmode, tem);
466 set_mem_alias_set (tem, get_frame_alias_set ());
467 #endif
468 return tem;
471 /* Alias set used for setjmp buffer. */
472 static HOST_WIDE_INT setjmp_alias_set = -1;
474 /* Construct the leading half of a __builtin_setjmp call. Control will
475 return to RECEIVER_LABEL. This is used directly by sjlj exception
476 handling code. */
478 void
479 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
481 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
482 rtx stack_save;
483 rtx mem;
485 if (setjmp_alias_set == -1)
486 setjmp_alias_set = new_alias_set ();
488 buf_addr = convert_memory_address (Pmode, buf_addr);
490 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
492 emit_queue ();
494 /* We store the frame pointer and the address of receiver_label in
495 the buffer and use the rest of it for the stack save area, which
496 is machine-dependent. */
498 #ifndef BUILTIN_SETJMP_FRAME_VALUE
499 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
500 #endif
502 mem = gen_rtx_MEM (Pmode, buf_addr);
503 set_mem_alias_set (mem, setjmp_alias_set);
504 emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE);
506 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
507 set_mem_alias_set (mem, setjmp_alias_set);
509 emit_move_insn (validize_mem (mem),
510 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
512 stack_save = gen_rtx_MEM (sa_mode,
513 plus_constant (buf_addr,
514 2 * GET_MODE_SIZE (Pmode)));
515 set_mem_alias_set (stack_save, setjmp_alias_set);
516 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
518 /* If there is further processing to do, do it. */
519 #ifdef HAVE_builtin_setjmp_setup
520 if (HAVE_builtin_setjmp_setup)
521 emit_insn (gen_builtin_setjmp_setup (buf_addr));
522 #endif
524 /* Tell optimize_save_area_alloca that extra work is going to
525 need to go on during alloca. */
526 current_function_calls_setjmp = 1;
528 /* Set this so all the registers get saved in our frame; we need to be
529 able to copy the saved values for any registers from frames we unwind. */
530 current_function_has_nonlocal_label = 1;
533 /* Construct the trailing part of a __builtin_setjmp call.
534 This is used directly by sjlj exception handling code. */
536 void
537 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
539 /* Clobber the FP when we get here, so we have to make sure it's
540 marked as used by this function. */
541 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
543 /* Mark the static chain as clobbered here so life information
544 doesn't get messed up for it. */
545 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
547 /* Now put in the code to restore the frame pointer, and argument
548 pointer, if needed. The code below is from expand_end_bindings
549 in stmt.c; see detailed documentation there. */
550 #ifdef HAVE_nonlocal_goto
551 if (! HAVE_nonlocal_goto)
552 #endif
553 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
555 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
556 if (fixed_regs[ARG_POINTER_REGNUM])
558 #ifdef ELIMINABLE_REGS
559 size_t i;
560 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
562 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
563 if (elim_regs[i].from == ARG_POINTER_REGNUM
564 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
565 break;
567 if (i == ARRAY_SIZE (elim_regs))
568 #endif
570 /* Now restore our arg pointer from the address at which it
571 was saved in our stack frame. */
572 emit_move_insn (virtual_incoming_args_rtx,
573 copy_to_reg (get_arg_pointer_save_area (cfun)));
576 #endif
578 #ifdef HAVE_builtin_setjmp_receiver
579 if (HAVE_builtin_setjmp_receiver)
580 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
581 else
582 #endif
583 #ifdef HAVE_nonlocal_goto_receiver
584 if (HAVE_nonlocal_goto_receiver)
585 emit_insn (gen_nonlocal_goto_receiver ());
586 else
587 #endif
588 { /* Nothing */ }
590 /* @@@ This is a kludge. Not all machine descriptions define a blockage
591 insn, but we must not allow the code we just generated to be reordered
592 by scheduling. Specifically, the update of the frame pointer must
593 happen immediately, not later. So emit an ASM_INPUT to act as blockage
594 insn. */
595 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
598 /* __builtin_setjmp is passed a pointer to an array of five words (not
599 all will be used on all machines). It operates similarly to the C
600 library function of the same name, but is more efficient. Much of
601 the code below (and for longjmp) is copied from the handling of
602 non-local gotos.
604 NOTE: This is intended for use by GNAT and the exception handling
605 scheme in the compiler and will only work in the method used by
606 them. */
608 static rtx
609 expand_builtin_setjmp (tree arglist, rtx target)
611 rtx buf_addr, next_lab, cont_lab;
613 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
614 return NULL_RTX;
616 if (target == 0 || GET_CODE (target) != REG
617 || REGNO (target) < FIRST_PSEUDO_REGISTER)
618 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
620 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
622 next_lab = gen_label_rtx ();
623 cont_lab = gen_label_rtx ();
625 expand_builtin_setjmp_setup (buf_addr, next_lab);
627 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
628 ensure that pending stack adjustments are flushed. */
629 emit_move_insn (target, const0_rtx);
630 emit_jump (cont_lab);
632 emit_label (next_lab);
634 expand_builtin_setjmp_receiver (next_lab);
636 /* Set TARGET to one. */
637 emit_move_insn (target, const1_rtx);
638 emit_label (cont_lab);
640 /* Tell flow about the strange goings on. Putting `next_lab' on
641 `nonlocal_goto_handler_labels' to indicates that function
642 calls may traverse the arc back to this label. */
644 current_function_has_nonlocal_label = 1;
645 nonlocal_goto_handler_labels
646 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
648 return target;
651 /* __builtin_longjmp is passed a pointer to an array of five words (not
652 all will be used on all machines). It operates similarly to the C
653 library function of the same name, but is more efficient. Much of
654 the code below is copied from the handling of non-local gotos.
656 NOTE: This is intended for use by GNAT and the exception handling
657 scheme in the compiler and will only work in the method used by
658 them. */
660 void
661 expand_builtin_longjmp (rtx buf_addr, rtx value)
663 rtx fp, lab, stack, insn, last;
664 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
666 if (setjmp_alias_set == -1)
667 setjmp_alias_set = new_alias_set ();
669 buf_addr = convert_memory_address (Pmode, buf_addr);
671 buf_addr = force_reg (Pmode, buf_addr);
673 /* We used to store value in static_chain_rtx, but that fails if pointers
674 are smaller than integers. We instead require that the user must pass
675 a second argument of 1, because that is what builtin_setjmp will
676 return. This also makes EH slightly more efficient, since we are no
677 longer copying around a value that we don't care about. */
678 if (value != const1_rtx)
679 abort ();
681 current_function_calls_longjmp = 1;
683 last = get_last_insn ();
684 #ifdef HAVE_builtin_longjmp
685 if (HAVE_builtin_longjmp)
686 emit_insn (gen_builtin_longjmp (buf_addr));
687 else
688 #endif
690 fp = gen_rtx_MEM (Pmode, buf_addr);
691 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
692 GET_MODE_SIZE (Pmode)));
694 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
695 2 * GET_MODE_SIZE (Pmode)));
696 set_mem_alias_set (fp, setjmp_alias_set);
697 set_mem_alias_set (lab, setjmp_alias_set);
698 set_mem_alias_set (stack, setjmp_alias_set);
700 /* Pick up FP, label, and SP from the block and jump. This code is
701 from expand_goto in stmt.c; see there for detailed comments. */
702 #if HAVE_nonlocal_goto
703 if (HAVE_nonlocal_goto)
704 /* We have to pass a value to the nonlocal_goto pattern that will
705 get copied into the static_chain pointer, but it does not matter
706 what that value is, because builtin_setjmp does not use it. */
707 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
708 else
709 #endif
711 lab = copy_to_reg (lab);
713 emit_move_insn (hard_frame_pointer_rtx, fp);
714 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
716 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
717 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
718 emit_indirect_jump (lab);
722 /* Search backwards and mark the jump insn as a non-local goto.
723 Note that this precludes the use of __builtin_longjmp to a
724 __builtin_setjmp target in the same function. However, we've
725 already cautioned the user that these functions are for
726 internal exception handling use only. */
727 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
729 if (insn == last)
730 abort ();
731 if (GET_CODE (insn) == JUMP_INSN)
733 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
734 REG_NOTES (insn));
735 break;
737 else if (GET_CODE (insn) == CALL_INSN)
738 break;
742 /* Expand a call to __builtin_prefetch. For a target that does not support
743 data prefetch, evaluate the memory address argument in case it has side
744 effects. */
746 static void
747 expand_builtin_prefetch (tree arglist)
749 tree arg0, arg1, arg2;
750 rtx op0, op1, op2;
752 if (!validate_arglist (arglist, POINTER_TYPE, 0))
753 return;
755 arg0 = TREE_VALUE (arglist);
756 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
757 zero (read) and argument 2 (locality) defaults to 3 (high degree of
758 locality). */
759 if (TREE_CHAIN (arglist))
761 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
762 if (TREE_CHAIN (TREE_CHAIN (arglist)))
763 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
764 else
765 arg2 = build_int_2 (3, 0);
767 else
769 arg1 = integer_zero_node;
770 arg2 = build_int_2 (3, 0);
773 /* Argument 0 is an address. */
774 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
776 /* Argument 1 (read/write flag) must be a compile-time constant int. */
777 if (TREE_CODE (arg1) != INTEGER_CST)
779 error ("second arg to `__builtin_prefetch' must be a constant");
780 arg1 = integer_zero_node;
782 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
783 /* Argument 1 must be either zero or one. */
784 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
786 warning ("invalid second arg to __builtin_prefetch; using zero");
787 op1 = const0_rtx;
790 /* Argument 2 (locality) must be a compile-time constant int. */
791 if (TREE_CODE (arg2) != INTEGER_CST)
793 error ("third arg to `__builtin_prefetch' must be a constant");
794 arg2 = integer_zero_node;
796 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
797 /* Argument 2 must be 0, 1, 2, or 3. */
798 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
800 warning ("invalid third arg to __builtin_prefetch; using zero");
801 op2 = const0_rtx;
804 #ifdef HAVE_prefetch
805 if (HAVE_prefetch)
807 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
808 (op0,
809 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
810 || (GET_MODE (op0) != Pmode))
812 op0 = convert_memory_address (Pmode, op0);
813 op0 = force_reg (Pmode, op0);
815 emit_insn (gen_prefetch (op0, op1, op2));
817 else
818 #endif
819 op0 = protect_from_queue (op0, 0);
820 /* Don't do anything with direct references to volatile memory, but
821 generate code to handle other side effects. */
822 if (GET_CODE (op0) != MEM && side_effects_p (op0))
823 emit_insn (op0);
826 /* Get a MEM rtx for expression EXP which is the address of an operand
827 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
829 static rtx
830 get_memory_rtx (tree exp)
832 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
833 rtx mem;
835 addr = convert_memory_address (Pmode, addr);
837 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
839 /* Get an expression we can use to find the attributes to assign to MEM.
840 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
841 we can. First remove any nops. */
842 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
843 || TREE_CODE (exp) == NON_LVALUE_EXPR)
844 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
845 exp = TREE_OPERAND (exp, 0);
847 if (TREE_CODE (exp) == ADDR_EXPR)
849 exp = TREE_OPERAND (exp, 0);
850 set_mem_attributes (mem, exp, 0);
852 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
854 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
855 /* memcpy, memset and other builtin stringops can alias with anything. */
856 set_mem_alias_set (mem, 0);
859 return mem;
862 /* Built-in functions to perform an untyped call and return. */
864 /* For each register that may be used for calling a function, this
865 gives a mode used to copy the register's value. VOIDmode indicates
866 the register is not used for calling a function. If the machine
867 has register windows, this gives only the outbound registers.
868 INCOMING_REGNO gives the corresponding inbound register. */
869 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
871 /* For each register that may be used for returning values, this gives
872 a mode used to copy the register's value. VOIDmode indicates the
873 register is not used for returning values. If the machine has
874 register windows, this gives only the outbound registers.
875 INCOMING_REGNO gives the corresponding inbound register. */
876 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
878 /* For each register that may be used for calling a function, this
879 gives the offset of that register into the block returned by
880 __builtin_apply_args. 0 indicates that the register is not
881 used for calling a function. */
882 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
884 /* Return the offset of register REGNO into the block returned by
885 __builtin_apply_args. This is not declared static, since it is
886 needed in objc-act.c. */
889 apply_args_register_offset (int regno)
891 apply_args_size ();
893 /* Arguments are always put in outgoing registers (in the argument
894 block) if such make sense. */
895 #ifdef OUTGOING_REGNO
896 regno = OUTGOING_REGNO (regno);
897 #endif
898 return apply_args_reg_offset[regno];
901 /* Return the size required for the block returned by __builtin_apply_args,
902 and initialize apply_args_mode. */
904 static int
905 apply_args_size (void)
907 static int size = -1;
908 int align;
909 unsigned int regno;
910 enum machine_mode mode;
912 /* The values computed by this function never change. */
913 if (size < 0)
915 /* The first value is the incoming arg-pointer. */
916 size = GET_MODE_SIZE (Pmode);
918 /* The second value is the structure value address unless this is
919 passed as an "invisible" first argument. */
920 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
921 size += GET_MODE_SIZE (Pmode);
923 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
924 if (FUNCTION_ARG_REGNO_P (regno))
926 /* Search for the proper mode for copying this register's
927 value. I'm not sure this is right, but it works so far. */
928 enum machine_mode best_mode = VOIDmode;
930 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
931 mode != VOIDmode;
932 mode = GET_MODE_WIDER_MODE (mode))
933 if (HARD_REGNO_MODE_OK (regno, mode)
934 && HARD_REGNO_NREGS (regno, mode) == 1)
935 best_mode = mode;
937 if (best_mode == VOIDmode)
938 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
939 mode != VOIDmode;
940 mode = GET_MODE_WIDER_MODE (mode))
941 if (HARD_REGNO_MODE_OK (regno, mode)
942 && have_insn_for (SET, mode))
943 best_mode = mode;
945 if (best_mode == VOIDmode)
946 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
947 mode != VOIDmode;
948 mode = GET_MODE_WIDER_MODE (mode))
949 if (HARD_REGNO_MODE_OK (regno, mode)
950 && have_insn_for (SET, mode))
951 best_mode = mode;
953 if (best_mode == VOIDmode)
954 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
955 mode != VOIDmode;
956 mode = GET_MODE_WIDER_MODE (mode))
957 if (HARD_REGNO_MODE_OK (regno, mode)
958 && have_insn_for (SET, mode))
959 best_mode = mode;
961 mode = best_mode;
962 if (mode == VOIDmode)
963 abort ();
965 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
966 if (size % align != 0)
967 size = CEIL (size, align) * align;
968 apply_args_reg_offset[regno] = size;
969 size += GET_MODE_SIZE (mode);
970 apply_args_mode[regno] = mode;
972 else
974 apply_args_mode[regno] = VOIDmode;
975 apply_args_reg_offset[regno] = 0;
978 return size;
981 /* Return the size required for the block returned by __builtin_apply,
982 and initialize apply_result_mode. */
984 static int
985 apply_result_size (void)
987 static int size = -1;
988 int align, regno;
989 enum machine_mode mode;
991 /* The values computed by this function never change. */
992 if (size < 0)
994 size = 0;
996 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
997 if (FUNCTION_VALUE_REGNO_P (regno))
999 /* Search for the proper mode for copying this register's
1000 value. I'm not sure this is right, but it works so far. */
1001 enum machine_mode best_mode = VOIDmode;
1003 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1004 mode != TImode;
1005 mode = GET_MODE_WIDER_MODE (mode))
1006 if (HARD_REGNO_MODE_OK (regno, mode))
1007 best_mode = mode;
1009 if (best_mode == VOIDmode)
1010 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1011 mode != VOIDmode;
1012 mode = GET_MODE_WIDER_MODE (mode))
1013 if (HARD_REGNO_MODE_OK (regno, mode)
1014 && have_insn_for (SET, mode))
1015 best_mode = mode;
1017 if (best_mode == VOIDmode)
1018 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
1019 mode != VOIDmode;
1020 mode = GET_MODE_WIDER_MODE (mode))
1021 if (HARD_REGNO_MODE_OK (regno, mode)
1022 && have_insn_for (SET, mode))
1023 best_mode = mode;
1025 if (best_mode == VOIDmode)
1026 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
1027 mode != VOIDmode;
1028 mode = GET_MODE_WIDER_MODE (mode))
1029 if (HARD_REGNO_MODE_OK (regno, mode)
1030 && have_insn_for (SET, mode))
1031 best_mode = mode;
1033 mode = best_mode;
1034 if (mode == VOIDmode)
1035 abort ();
1037 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1038 if (size % align != 0)
1039 size = CEIL (size, align) * align;
1040 size += GET_MODE_SIZE (mode);
1041 apply_result_mode[regno] = mode;
1043 else
1044 apply_result_mode[regno] = VOIDmode;
1046 /* Allow targets that use untyped_call and untyped_return to override
1047 the size so that machine-specific information can be stored here. */
1048 #ifdef APPLY_RESULT_SIZE
1049 size = APPLY_RESULT_SIZE;
1050 #endif
1052 return size;
1055 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1056 /* Create a vector describing the result block RESULT. If SAVEP is true,
1057 the result block is used to save the values; otherwise it is used to
1058 restore the values. */
1060 static rtx
1061 result_vector (int savep, rtx result)
1063 int regno, size, align, nelts;
1064 enum machine_mode mode;
1065 rtx reg, mem;
1066 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1068 size = nelts = 0;
1069 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1070 if ((mode = apply_result_mode[regno]) != VOIDmode)
1072 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1073 if (size % align != 0)
1074 size = CEIL (size, align) * align;
1075 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1076 mem = adjust_address (result, mode, size);
1077 savevec[nelts++] = (savep
1078 ? gen_rtx_SET (VOIDmode, mem, reg)
1079 : gen_rtx_SET (VOIDmode, reg, mem));
1080 size += GET_MODE_SIZE (mode);
1082 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1084 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1086 /* Save the state required to perform an untyped call with the same
1087 arguments as were passed to the current function. */
1089 static rtx
1090 expand_builtin_apply_args_1 (void)
1092 rtx registers;
1093 int size, align, regno;
1094 enum machine_mode mode;
1095 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1097 /* Create a block where the arg-pointer, structure value address,
1098 and argument registers can be saved. */
1099 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1101 /* Walk past the arg-pointer and structure value address. */
1102 size = GET_MODE_SIZE (Pmode);
1103 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1104 size += GET_MODE_SIZE (Pmode);
1106 /* Save each register used in calling a function to the block. */
1107 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1108 if ((mode = apply_args_mode[regno]) != VOIDmode)
1110 rtx tem;
1112 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1113 if (size % align != 0)
1114 size = CEIL (size, align) * align;
1116 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1118 emit_move_insn (adjust_address (registers, mode, size), tem);
1119 size += GET_MODE_SIZE (mode);
1122 /* Save the arg pointer to the block. */
1123 emit_move_insn (adjust_address (registers, Pmode, 0),
1124 copy_to_reg (virtual_incoming_args_rtx));
1125 size = GET_MODE_SIZE (Pmode);
1127 /* Save the structure value address unless this is passed as an
1128 "invisible" first argument. */
1129 if (struct_incoming_value)
1131 emit_move_insn (adjust_address (registers, Pmode, size),
1132 copy_to_reg (struct_incoming_value));
1133 size += GET_MODE_SIZE (Pmode);
1136 /* Return the address of the block. */
1137 return copy_addr_to_reg (XEXP (registers, 0));
1140 /* __builtin_apply_args returns block of memory allocated on
1141 the stack into which is stored the arg pointer, structure
1142 value address, static chain, and all the registers that might
1143 possibly be used in performing a function call. The code is
1144 moved to the start of the function so the incoming values are
1145 saved. */
1147 static rtx
1148 expand_builtin_apply_args (void)
1150 /* Don't do __builtin_apply_args more than once in a function.
1151 Save the result of the first call and reuse it. */
1152 if (apply_args_value != 0)
1153 return apply_args_value;
1155 /* When this function is called, it means that registers must be
1156 saved on entry to this function. So we migrate the
1157 call to the first insn of this function. */
1158 rtx temp;
1159 rtx seq;
1161 start_sequence ();
1162 temp = expand_builtin_apply_args_1 ();
1163 seq = get_insns ();
1164 end_sequence ();
1166 apply_args_value = temp;
1168 /* Put the insns after the NOTE that starts the function.
1169 If this is inside a start_sequence, make the outer-level insn
1170 chain current, so the code is placed at the start of the
1171 function. */
1172 push_topmost_sequence ();
1173 emit_insn_before (seq, NEXT_INSN (get_insns ()));
1174 pop_topmost_sequence ();
1175 return temp;
1179 /* Perform an untyped call and save the state required to perform an
1180 untyped return of whatever value was returned by the given function. */
1182 static rtx
1183 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1185 int size, align, regno;
1186 enum machine_mode mode;
1187 rtx incoming_args, result, reg, dest, src, call_insn;
1188 rtx old_stack_level = 0;
1189 rtx call_fusage = 0;
1190 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1192 arguments = convert_memory_address (Pmode, arguments);
1194 /* Create a block where the return registers can be saved. */
1195 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1197 /* Fetch the arg pointer from the ARGUMENTS block. */
1198 incoming_args = gen_reg_rtx (Pmode);
1199 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1200 #ifndef STACK_GROWS_DOWNWARD
1201 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1202 incoming_args, 0, OPTAB_LIB_WIDEN);
1203 #endif
1205 /* Perform postincrements before actually calling the function. */
1206 emit_queue ();
1208 /* Push a new argument block and copy the arguments. Do not allow
1209 the (potential) memcpy call below to interfere with our stack
1210 manipulations. */
1211 do_pending_stack_adjust ();
1212 NO_DEFER_POP;
1214 /* Save the stack with nonlocal if available. */
1215 #ifdef HAVE_save_stack_nonlocal
1216 if (HAVE_save_stack_nonlocal)
1217 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1218 else
1219 #endif
1220 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1222 /* Allocate a block of memory onto the stack and copy the memory
1223 arguments to the outgoing arguments address. */
1224 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1225 dest = virtual_outgoing_args_rtx;
1226 #ifndef STACK_GROWS_DOWNWARD
1227 if (GET_CODE (argsize) == CONST_INT)
1228 dest = plus_constant (dest, -INTVAL (argsize));
1229 else
1230 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1231 #endif
1232 dest = gen_rtx_MEM (BLKmode, dest);
1233 set_mem_align (dest, PARM_BOUNDARY);
1234 src = gen_rtx_MEM (BLKmode, incoming_args);
1235 set_mem_align (src, PARM_BOUNDARY);
1236 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1238 /* Refer to the argument block. */
1239 apply_args_size ();
1240 arguments = gen_rtx_MEM (BLKmode, arguments);
1241 set_mem_align (arguments, PARM_BOUNDARY);
1243 /* Walk past the arg-pointer and structure value address. */
1244 size = GET_MODE_SIZE (Pmode);
1245 if (struct_value)
1246 size += GET_MODE_SIZE (Pmode);
1248 /* Restore each of the registers previously saved. Make USE insns
1249 for each of these registers for use in making the call. */
1250 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1251 if ((mode = apply_args_mode[regno]) != VOIDmode)
1253 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1254 if (size % align != 0)
1255 size = CEIL (size, align) * align;
1256 reg = gen_rtx_REG (mode, regno);
1257 emit_move_insn (reg, adjust_address (arguments, mode, size));
1258 use_reg (&call_fusage, reg);
1259 size += GET_MODE_SIZE (mode);
1262 /* Restore the structure value address unless this is passed as an
1263 "invisible" first argument. */
1264 size = GET_MODE_SIZE (Pmode);
1265 if (struct_value)
1267 rtx value = gen_reg_rtx (Pmode);
1268 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1269 emit_move_insn (struct_value, value);
1270 if (GET_CODE (struct_value) == REG)
1271 use_reg (&call_fusage, struct_value);
1272 size += GET_MODE_SIZE (Pmode);
1275 /* All arguments and registers used for the call are set up by now! */
1276 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0, 0);
1278 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1279 and we don't want to load it into a register as an optimization,
1280 because prepare_call_address already did it if it should be done. */
1281 if (GET_CODE (function) != SYMBOL_REF)
1282 function = memory_address (FUNCTION_MODE, function);
1284 /* Generate the actual call instruction and save the return value. */
1285 #ifdef HAVE_untyped_call
1286 if (HAVE_untyped_call)
1287 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1288 result, result_vector (1, result)));
1289 else
1290 #endif
1291 #ifdef HAVE_call_value
1292 if (HAVE_call_value)
1294 rtx valreg = 0;
1296 /* Locate the unique return register. It is not possible to
1297 express a call that sets more than one return register using
1298 call_value; use untyped_call for that. In fact, untyped_call
1299 only needs to save the return registers in the given block. */
1300 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1301 if ((mode = apply_result_mode[regno]) != VOIDmode)
1303 if (valreg)
1304 abort (); /* HAVE_untyped_call required. */
1305 valreg = gen_rtx_REG (mode, regno);
1308 emit_call_insn (GEN_CALL_VALUE (valreg,
1309 gen_rtx_MEM (FUNCTION_MODE, function),
1310 const0_rtx, NULL_RTX, const0_rtx));
1312 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1314 else
1315 #endif
1316 abort ();
1318 /* Find the CALL insn we just emitted, and attach the register usage
1319 information. */
1320 call_insn = last_call_insn ();
1321 add_function_usage_to (call_insn, call_fusage);
1323 /* Restore the stack. */
1324 #ifdef HAVE_save_stack_nonlocal
1325 if (HAVE_save_stack_nonlocal)
1326 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1327 else
1328 #endif
1329 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1331 OK_DEFER_POP;
1333 /* Return the address of the result block. */
1334 result = copy_addr_to_reg (XEXP (result, 0));
1335 return convert_memory_address (ptr_mode, result);
1338 /* Perform an untyped return. */
1340 static void
1341 expand_builtin_return (rtx result)
1343 int size, align, regno;
1344 enum machine_mode mode;
1345 rtx reg;
1346 rtx call_fusage = 0;
1348 result = convert_memory_address (Pmode, result);
1350 apply_result_size ();
1351 result = gen_rtx_MEM (BLKmode, result);
1353 #ifdef HAVE_untyped_return
1354 if (HAVE_untyped_return)
1356 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1357 emit_barrier ();
1358 return;
1360 #endif
1362 /* Restore the return value and note that each value is used. */
1363 size = 0;
1364 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1365 if ((mode = apply_result_mode[regno]) != VOIDmode)
1367 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1368 if (size % align != 0)
1369 size = CEIL (size, align) * align;
1370 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1371 emit_move_insn (reg, adjust_address (result, mode, size));
1373 push_to_sequence (call_fusage);
1374 emit_insn (gen_rtx_USE (VOIDmode, reg));
1375 call_fusage = get_insns ();
1376 end_sequence ();
1377 size += GET_MODE_SIZE (mode);
1380 /* Put the USE insns before the return. */
1381 emit_insn (call_fusage);
1383 /* Return whatever values was restored by jumping directly to the end
1384 of the function. */
1385 expand_null_return ();
1388 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1390 static enum type_class
1391 type_to_class (tree type)
1393 switch (TREE_CODE (type))
1395 case VOID_TYPE: return void_type_class;
1396 case INTEGER_TYPE: return integer_type_class;
1397 case CHAR_TYPE: return char_type_class;
1398 case ENUMERAL_TYPE: return enumeral_type_class;
1399 case BOOLEAN_TYPE: return boolean_type_class;
1400 case POINTER_TYPE: return pointer_type_class;
1401 case REFERENCE_TYPE: return reference_type_class;
1402 case OFFSET_TYPE: return offset_type_class;
1403 case REAL_TYPE: return real_type_class;
1404 case COMPLEX_TYPE: return complex_type_class;
1405 case FUNCTION_TYPE: return function_type_class;
1406 case METHOD_TYPE: return method_type_class;
1407 case RECORD_TYPE: return record_type_class;
1408 case UNION_TYPE:
1409 case QUAL_UNION_TYPE: return union_type_class;
1410 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1411 ? string_type_class : array_type_class);
1412 case SET_TYPE: return set_type_class;
1413 case FILE_TYPE: return file_type_class;
1414 case LANG_TYPE: return lang_type_class;
1415 default: return no_type_class;
1419 /* Expand a call to __builtin_classify_type with arguments found in
1420 ARGLIST. */
1422 static rtx
1423 expand_builtin_classify_type (tree arglist)
1425 if (arglist != 0)
1426 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1427 return GEN_INT (no_type_class);
1430 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1432 static rtx
1433 expand_builtin_constant_p (tree arglist, enum machine_mode target_mode)
1435 rtx tmp;
1437 if (arglist == 0)
1438 return const0_rtx;
1439 arglist = TREE_VALUE (arglist);
1441 /* We have taken care of the easy cases during constant folding. This
1442 case is not obvious, so emit (constant_p_rtx (ARGLIST)) and let CSE
1443 get a chance to see if it can deduce whether ARGLIST is constant.
1444 If CSE isn't going to run, of course, don't bother waiting. */
1446 if (cse_not_expected)
1447 return const0_rtx;
1449 current_function_calls_constant_p = 1;
1451 tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0);
1452 tmp = gen_rtx_CONSTANT_P_RTX (target_mode, tmp);
1453 return tmp;
1456 /* This helper macro, meant to be used in mathfn_built_in below,
1457 determines which among a set of three builtin math functions is
1458 appropriate for a given type mode. The `F' and `L' cases are
1459 automatically generated from the `double' case. */
1460 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1461 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1462 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1463 fcodel = BUILT_IN_MATHFN##L ; break;
1465 /* Return mathematic function equivalent to FN but operating directly
1466 on TYPE, if available. If we can't do the conversion, return zero. */
1467 tree
1468 mathfn_built_in (tree type, enum built_in_function fn)
1470 const enum machine_mode type_mode = TYPE_MODE (type);
1471 enum built_in_function fcode, fcodef, fcodel;
1473 switch (fn)
1475 CASE_MATHFN (BUILT_IN_ACOS)
1476 CASE_MATHFN (BUILT_IN_ACOSH)
1477 CASE_MATHFN (BUILT_IN_ASIN)
1478 CASE_MATHFN (BUILT_IN_ASINH)
1479 CASE_MATHFN (BUILT_IN_ATAN)
1480 CASE_MATHFN (BUILT_IN_ATAN2)
1481 CASE_MATHFN (BUILT_IN_ATANH)
1482 CASE_MATHFN (BUILT_IN_CBRT)
1483 CASE_MATHFN (BUILT_IN_CEIL)
1484 CASE_MATHFN (BUILT_IN_COPYSIGN)
1485 CASE_MATHFN (BUILT_IN_COS)
1486 CASE_MATHFN (BUILT_IN_COSH)
1487 CASE_MATHFN (BUILT_IN_DREM)
1488 CASE_MATHFN (BUILT_IN_ERF)
1489 CASE_MATHFN (BUILT_IN_ERFC)
1490 CASE_MATHFN (BUILT_IN_EXP)
1491 CASE_MATHFN (BUILT_IN_EXP10)
1492 CASE_MATHFN (BUILT_IN_EXP2)
1493 CASE_MATHFN (BUILT_IN_EXPM1)
1494 CASE_MATHFN (BUILT_IN_FABS)
1495 CASE_MATHFN (BUILT_IN_FDIM)
1496 CASE_MATHFN (BUILT_IN_FLOOR)
1497 CASE_MATHFN (BUILT_IN_FMA)
1498 CASE_MATHFN (BUILT_IN_FMAX)
1499 CASE_MATHFN (BUILT_IN_FMIN)
1500 CASE_MATHFN (BUILT_IN_FMOD)
1501 CASE_MATHFN (BUILT_IN_FREXP)
1502 CASE_MATHFN (BUILT_IN_GAMMA)
1503 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1504 CASE_MATHFN (BUILT_IN_HYPOT)
1505 CASE_MATHFN (BUILT_IN_ILOGB)
1506 CASE_MATHFN (BUILT_IN_INF)
1507 CASE_MATHFN (BUILT_IN_J0)
1508 CASE_MATHFN (BUILT_IN_J1)
1509 CASE_MATHFN (BUILT_IN_JN)
1510 CASE_MATHFN (BUILT_IN_LDEXP)
1511 CASE_MATHFN (BUILT_IN_LGAMMA)
1512 CASE_MATHFN (BUILT_IN_LLRINT)
1513 CASE_MATHFN (BUILT_IN_LLROUND)
1514 CASE_MATHFN (BUILT_IN_LOG)
1515 CASE_MATHFN (BUILT_IN_LOG10)
1516 CASE_MATHFN (BUILT_IN_LOG1P)
1517 CASE_MATHFN (BUILT_IN_LOG2)
1518 CASE_MATHFN (BUILT_IN_LOGB)
1519 CASE_MATHFN (BUILT_IN_LRINT)
1520 CASE_MATHFN (BUILT_IN_LROUND)
1521 CASE_MATHFN (BUILT_IN_MODF)
1522 CASE_MATHFN (BUILT_IN_NAN)
1523 CASE_MATHFN (BUILT_IN_NANS)
1524 CASE_MATHFN (BUILT_IN_NEARBYINT)
1525 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1526 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1527 CASE_MATHFN (BUILT_IN_POW)
1528 CASE_MATHFN (BUILT_IN_POW10)
1529 CASE_MATHFN (BUILT_IN_REMAINDER)
1530 CASE_MATHFN (BUILT_IN_REMQUO)
1531 CASE_MATHFN (BUILT_IN_RINT)
1532 CASE_MATHFN (BUILT_IN_ROUND)
1533 CASE_MATHFN (BUILT_IN_SCALB)
1534 CASE_MATHFN (BUILT_IN_SCALBLN)
1535 CASE_MATHFN (BUILT_IN_SCALBN)
1536 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1537 CASE_MATHFN (BUILT_IN_SIN)
1538 CASE_MATHFN (BUILT_IN_SINCOS)
1539 CASE_MATHFN (BUILT_IN_SINH)
1540 CASE_MATHFN (BUILT_IN_SQRT)
1541 CASE_MATHFN (BUILT_IN_TAN)
1542 CASE_MATHFN (BUILT_IN_TANH)
1543 CASE_MATHFN (BUILT_IN_TGAMMA)
1544 CASE_MATHFN (BUILT_IN_TRUNC)
1545 CASE_MATHFN (BUILT_IN_Y0)
1546 CASE_MATHFN (BUILT_IN_Y1)
1547 CASE_MATHFN (BUILT_IN_YN)
1549 default:
1550 return 0;
1553 if (type_mode == TYPE_MODE (double_type_node))
1554 return implicit_built_in_decls[fcode];
1555 else if (type_mode == TYPE_MODE (float_type_node))
1556 return implicit_built_in_decls[fcodef];
1557 else if (type_mode == TYPE_MODE (long_double_type_node))
1558 return implicit_built_in_decls[fcodel];
1559 else
1560 return 0;
1563 /* If errno must be maintained, expand the RTL to check if the result,
1564 TARGET, of a built-in function call, EXP, is NaN, and if so set
1565 errno to EDOM. */
1567 static void
1568 expand_errno_check (tree exp, rtx target)
1570 rtx lab = gen_label_rtx ();
1572 /* Test the result; if it is NaN, set errno=EDOM because
1573 the argument was not in the domain. */
1574 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1575 0, lab);
1577 #ifdef TARGET_EDOM
1578 /* If this built-in doesn't throw an exception, set errno directly. */
1579 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1581 #ifdef GEN_ERRNO_RTX
1582 rtx errno_rtx = GEN_ERRNO_RTX;
1583 #else
1584 rtx errno_rtx
1585 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1586 #endif
1587 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1588 emit_label (lab);
1589 return;
1591 #endif
1593 /* We can't set errno=EDOM directly; let the library call do it.
1594 Pop the arguments right away in case the call gets deleted. */
1595 NO_DEFER_POP;
1596 expand_call (exp, target, 0);
1597 OK_DEFER_POP;
1598 emit_label (lab);
1602 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1603 Return 0 if a normal call should be emitted rather than expanding the
1604 function in-line. EXP is the expression that is a call to the builtin
1605 function; if convenient, the result should be placed in TARGET.
1606 SUBTARGET may be used as the target for computing one of EXP's operands. */
1608 static rtx
1609 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1611 optab builtin_optab;
1612 rtx op0, insns, before_call;
1613 tree fndecl = get_callee_fndecl (exp);
1614 tree arglist = TREE_OPERAND (exp, 1);
1615 enum machine_mode mode;
1616 bool errno_set = false;
1617 tree arg, narg;
1619 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1620 return 0;
1622 arg = TREE_VALUE (arglist);
1624 switch (DECL_FUNCTION_CODE (fndecl))
1626 case BUILT_IN_SIN:
1627 case BUILT_IN_SINF:
1628 case BUILT_IN_SINL:
1629 builtin_optab = sin_optab; break;
1630 case BUILT_IN_COS:
1631 case BUILT_IN_COSF:
1632 case BUILT_IN_COSL:
1633 builtin_optab = cos_optab; break;
1634 case BUILT_IN_SQRT:
1635 case BUILT_IN_SQRTF:
1636 case BUILT_IN_SQRTL:
1637 errno_set = ! tree_expr_nonnegative_p (arg);
1638 builtin_optab = sqrt_optab;
1639 break;
1640 case BUILT_IN_EXP:
1641 case BUILT_IN_EXPF:
1642 case BUILT_IN_EXPL:
1643 errno_set = true; builtin_optab = exp_optab; break;
1644 case BUILT_IN_LOG:
1645 case BUILT_IN_LOGF:
1646 case BUILT_IN_LOGL:
1647 errno_set = true; builtin_optab = log_optab; break;
1648 case BUILT_IN_TAN:
1649 case BUILT_IN_TANF:
1650 case BUILT_IN_TANL:
1651 builtin_optab = tan_optab; break;
1652 case BUILT_IN_ATAN:
1653 case BUILT_IN_ATANF:
1654 case BUILT_IN_ATANL:
1655 builtin_optab = atan_optab; break;
1656 case BUILT_IN_FLOOR:
1657 case BUILT_IN_FLOORF:
1658 case BUILT_IN_FLOORL:
1659 builtin_optab = floor_optab; break;
1660 case BUILT_IN_CEIL:
1661 case BUILT_IN_CEILF:
1662 case BUILT_IN_CEILL:
1663 builtin_optab = ceil_optab; break;
1664 case BUILT_IN_TRUNC:
1665 case BUILT_IN_TRUNCF:
1666 case BUILT_IN_TRUNCL:
1667 builtin_optab = btrunc_optab; break;
1668 case BUILT_IN_ROUND:
1669 case BUILT_IN_ROUNDF:
1670 case BUILT_IN_ROUNDL:
1671 builtin_optab = round_optab; break;
1672 case BUILT_IN_NEARBYINT:
1673 case BUILT_IN_NEARBYINTF:
1674 case BUILT_IN_NEARBYINTL:
1675 builtin_optab = nearbyint_optab; break;
1676 default:
1677 abort ();
1680 /* Make a suitable register to place result in. */
1681 mode = TYPE_MODE (TREE_TYPE (exp));
1683 if (! flag_errno_math || ! HONOR_NANS (mode))
1684 errno_set = false;
1686 /* Before working hard, check whether the instruction is available. */
1687 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1689 target = gen_reg_rtx (mode);
1691 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1692 need to expand the argument again. This way, we will not perform
1693 side-effects more the once. */
1694 narg = save_expr (arg);
1695 if (narg != arg)
1697 arglist = build_tree_list (NULL_TREE, arg);
1698 exp = build_function_call_expr (fndecl, arglist);
1701 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1703 emit_queue ();
1704 start_sequence ();
1706 /* Compute into TARGET.
1707 Set TARGET to wherever the result comes back. */
1708 target = expand_unop (mode, builtin_optab, op0, target, 0);
1710 if (target != 0)
1712 if (errno_set)
1713 expand_errno_check (exp, target);
1715 /* Output the entire sequence. */
1716 insns = get_insns ();
1717 end_sequence ();
1718 emit_insn (insns);
1719 return target;
1722 /* If we were unable to expand via the builtin, stop the sequence
1723 (without outputting the insns) and call to the library function
1724 with the stabilized argument list. */
1725 end_sequence ();
1728 before_call = get_last_insn ();
1730 target = expand_call (exp, target, target == const0_rtx);
1732 /* If this is a sqrt operation and we don't care about errno, try to
1733 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1734 This allows the semantics of the libcall to be visible to the RTL
1735 optimizers. */
1736 if (builtin_optab == sqrt_optab && !errno_set)
1738 /* Search backwards through the insns emitted by expand_call looking
1739 for the instruction with the REG_RETVAL note. */
1740 rtx last = get_last_insn ();
1741 while (last != before_call)
1743 if (find_reg_note (last, REG_RETVAL, NULL))
1745 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1746 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1747 two elements, i.e. symbol_ref(sqrt) and the operand. */
1748 if (note
1749 && GET_CODE (note) == EXPR_LIST
1750 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1751 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1752 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1754 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1755 /* Check operand is a register with expected mode. */
1756 if (operand
1757 && GET_CODE (operand) == REG
1758 && GET_MODE (operand) == mode)
1760 /* Replace the REG_EQUAL note with a SQRT rtx. */
1761 rtx equiv = gen_rtx_SQRT (mode, operand);
1762 set_unique_reg_note (last, REG_EQUAL, equiv);
1765 break;
1767 last = PREV_INSN (last);
1771 return target;
1774 /* Expand a call to the builtin binary math functions (pow and atan2).
1775 Return 0 if a normal call should be emitted rather than expanding the
1776 function in-line. EXP is the expression that is a call to the builtin
1777 function; if convenient, the result should be placed in TARGET.
1778 SUBTARGET may be used as the target for computing one of EXP's
1779 operands. */
1781 static rtx
1782 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1784 optab builtin_optab;
1785 rtx op0, op1, insns;
1786 tree fndecl = get_callee_fndecl (exp);
1787 tree arglist = TREE_OPERAND (exp, 1);
1788 tree arg0, arg1, temp, narg;
1789 enum machine_mode mode;
1790 bool errno_set = true;
1791 bool stable = true;
1793 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1794 return 0;
1796 arg0 = TREE_VALUE (arglist);
1797 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1799 switch (DECL_FUNCTION_CODE (fndecl))
1801 case BUILT_IN_POW:
1802 case BUILT_IN_POWF:
1803 case BUILT_IN_POWL:
1804 builtin_optab = pow_optab; break;
1805 case BUILT_IN_ATAN2:
1806 case BUILT_IN_ATAN2F:
1807 case BUILT_IN_ATAN2L:
1808 builtin_optab = atan2_optab; break;
1809 default:
1810 abort ();
1813 /* Make a suitable register to place result in. */
1814 mode = TYPE_MODE (TREE_TYPE (exp));
1816 /* Before working hard, check whether the instruction is available. */
1817 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1818 return 0;
1820 target = gen_reg_rtx (mode);
1822 if (! flag_errno_math || ! HONOR_NANS (mode))
1823 errno_set = false;
1825 /* Alway stabilize the argument list. */
1826 narg = save_expr (arg1);
1827 if (narg != arg1)
1829 temp = build_tree_list (NULL_TREE, narg);
1830 stable = false;
1832 else
1833 temp = TREE_CHAIN (arglist);
1835 narg = save_expr (arg0);
1836 if (narg != arg0)
1838 arglist = tree_cons (NULL_TREE, narg, temp);
1839 stable = false;
1841 else if (! stable)
1842 arglist = tree_cons (NULL_TREE, arg0, temp);
1844 if (! stable)
1845 exp = build_function_call_expr (fndecl, arglist);
1847 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1848 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1850 emit_queue ();
1851 start_sequence ();
1853 /* Compute into TARGET.
1854 Set TARGET to wherever the result comes back. */
1855 target = expand_binop (mode, builtin_optab, op0, op1,
1856 target, 0, OPTAB_DIRECT);
1858 /* If we were unable to expand via the builtin, stop the sequence
1859 (without outputting the insns) and call to the library function
1860 with the stabilized argument list. */
1861 if (target == 0)
1863 end_sequence ();
1864 return expand_call (exp, target, target == const0_rtx);
1867 if (errno_set)
1868 expand_errno_check (exp, target);
1870 /* Output the entire sequence. */
1871 insns = get_insns ();
1872 end_sequence ();
1873 emit_insn (insns);
1875 return target;
1878 /* To evaluate powi(x,n), the floating point value x raised to the
1879 constant integer exponent n, we use a hybrid algorithm that
1880 combines the "window method" with look-up tables. For an
1881 introduction to exponentiation algorithms and "addition chains",
1882 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1883 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1884 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1885 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1887 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
1888 multiplications to inline before calling the system library's pow
1889 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1890 so this default never requires calling pow, powf or powl. */
1892 #ifndef POWI_MAX_MULTS
1893 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1894 #endif
1896 /* The size of the "optimal power tree" lookup table. All
1897 exponents less than this value are simply looked up in the
1898 powi_table below. This threshold is also used to size the
1899 cache of pseudo registers that hold intermediate results. */
1900 #define POWI_TABLE_SIZE 256
1902 /* The size, in bits of the window, used in the "window method"
1903 exponentiation algorithm. This is equivalent to a radix of
1904 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1905 #define POWI_WINDOW_SIZE 3
1907 /* The following table is an efficient representation of an
1908 "optimal power tree". For each value, i, the corresponding
1909 value, j, in the table states than an optimal evaluation
1910 sequence for calculating pow(x,i) can be found by evaluating
1911 pow(x,j)*pow(x,i-j). An optimal power tree for the first
1912 100 integers is given in Knuth's "Seminumerical algorithms". */
1914 static const unsigned char powi_table[POWI_TABLE_SIZE] =
1916 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1917 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1918 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1919 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1920 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1921 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1922 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1923 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1924 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1925 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1926 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
1927 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
1928 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
1929 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
1930 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
1931 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
1932 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
1933 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
1934 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
1935 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
1936 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
1937 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
1938 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
1939 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
1940 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
1941 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
1942 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
1943 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
1944 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
1945 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
1946 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
1947 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
1951 /* Return the number of multiplications required to calculate
1952 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
1953 subroutine of powi_cost. CACHE is an array indicating
1954 which exponents have already been calculated. */
1956 static int
1957 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
1959 /* If we've already calculated this exponent, then this evaluation
1960 doesn't require any additional multiplications. */
1961 if (cache[n])
1962 return 0;
1964 cache[n] = true;
1965 return powi_lookup_cost (n - powi_table[n], cache)
1966 + powi_lookup_cost (powi_table[n], cache) + 1;
1969 /* Return the number of multiplications required to calculate
1970 powi(x,n) for an arbitrary x, given the exponent N. This
1971 function needs to be kept in sync with expand_powi below. */
1973 static int
1974 powi_cost (HOST_WIDE_INT n)
1976 bool cache[POWI_TABLE_SIZE];
1977 unsigned HOST_WIDE_INT digit;
1978 unsigned HOST_WIDE_INT val;
1979 int result;
1981 if (n == 0)
1982 return 0;
1984 /* Ignore the reciprocal when calculating the cost. */
1985 val = (n < 0) ? -n : n;
1987 /* Initialize the exponent cache. */
1988 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
1989 cache[1] = true;
1991 result = 0;
1993 while (val >= POWI_TABLE_SIZE)
1995 if (val & 1)
1997 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
1998 result += powi_lookup_cost (digit, cache)
1999 + POWI_WINDOW_SIZE + 1;
2000 val >>= POWI_WINDOW_SIZE;
2002 else
2004 val >>= 1;
2005 result++;
2009 return result + powi_lookup_cost (val, cache);
2012 /* Recursive subroutine of expand_powi. This function takes the array,
2013 CACHE, of already calculated exponents and an exponent N and returns
2014 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2016 static rtx
2017 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2019 unsigned HOST_WIDE_INT digit;
2020 rtx target, result;
2021 rtx op0, op1;
2023 if (n < POWI_TABLE_SIZE)
2025 if (cache[n])
2026 return cache[n];
2028 target = gen_reg_rtx (mode);
2029 cache[n] = target;
2031 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2032 op1 = expand_powi_1 (mode, powi_table[n], cache);
2034 else if (n & 1)
2036 target = gen_reg_rtx (mode);
2037 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2038 op0 = expand_powi_1 (mode, n - digit, cache);
2039 op1 = expand_powi_1 (mode, digit, cache);
2041 else
2043 target = gen_reg_rtx (mode);
2044 op0 = expand_powi_1 (mode, n >> 1, cache);
2045 op1 = op0;
2048 result = expand_mult (mode, op0, op1, target, 0);
2049 if (result != target)
2050 emit_move_insn (target, result);
2051 return target;
2054 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2055 floating point operand in mode MODE, and N is the exponent. This
2056 function needs to be kept in sync with powi_cost above. */
2058 static rtx
2059 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2061 unsigned HOST_WIDE_INT val;
2062 rtx cache[POWI_TABLE_SIZE];
2063 rtx result;
2065 if (n == 0)
2066 return CONST1_RTX (mode);
2068 val = (n < 0) ? -n : n;
2070 memset (cache, 0, sizeof (cache));
2071 cache[1] = x;
2073 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2075 /* If the original exponent was negative, reciprocate the result. */
2076 if (n < 0)
2077 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2078 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2080 return result;
2083 /* Expand a call to the pow built-in mathematical function. Return 0 if
2084 a normal call should be emitted rather than expanding the function
2085 in-line. EXP is the expression that is a call to the builtin
2086 function; if convenient, the result should be placed in TARGET. */
2088 static rtx
2089 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2091 tree arglist = TREE_OPERAND (exp, 1);
2092 tree arg0, arg1;
2094 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2095 return 0;
2097 arg0 = TREE_VALUE (arglist);
2098 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2100 if (TREE_CODE (arg1) == REAL_CST
2101 && ! TREE_CONSTANT_OVERFLOW (arg1))
2103 REAL_VALUE_TYPE cint;
2104 REAL_VALUE_TYPE c;
2105 HOST_WIDE_INT n;
2107 c = TREE_REAL_CST (arg1);
2108 n = real_to_integer (&c);
2109 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2110 if (real_identical (&c, &cint))
2112 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2113 Otherwise, check the number of multiplications required.
2114 Note that pow never sets errno for an integer exponent. */
2115 if ((n >= -1 && n <= 2)
2116 || (flag_unsafe_math_optimizations
2117 && ! optimize_size
2118 && powi_cost (n) <= POWI_MAX_MULTS))
2120 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2121 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2122 op = force_reg (mode, op);
2123 return expand_powi (op, mode, n);
2127 return expand_builtin_mathfn_2 (exp, target, NULL_RTX);
2130 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2131 if we failed the caller should emit a normal call, otherwise
2132 try to get the result in TARGET, if convenient. */
2134 static rtx
2135 expand_builtin_strlen (tree arglist, rtx target,
2136 enum machine_mode target_mode)
2138 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2139 return 0;
2140 else
2142 rtx pat;
2143 tree len, src = TREE_VALUE (arglist);
2144 rtx result, src_reg, char_rtx, before_strlen;
2145 enum machine_mode insn_mode = target_mode, char_mode;
2146 enum insn_code icode = CODE_FOR_nothing;
2147 int align;
2149 /* If the length can be computed at compile-time, return it. */
2150 len = c_strlen (src, 0);
2151 if (len)
2152 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2154 /* If the length can be computed at compile-time and is constant
2155 integer, but there are side-effects in src, evaluate
2156 src for side-effects, then return len.
2157 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2158 can be optimized into: i++; x = 3; */
2159 len = c_strlen (src, 1);
2160 if (len && TREE_CODE (len) == INTEGER_CST)
2162 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2163 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2166 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2168 /* If SRC is not a pointer type, don't do this operation inline. */
2169 if (align == 0)
2170 return 0;
2172 /* Bail out if we can't compute strlen in the right mode. */
2173 while (insn_mode != VOIDmode)
2175 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2176 if (icode != CODE_FOR_nothing)
2177 break;
2179 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2181 if (insn_mode == VOIDmode)
2182 return 0;
2184 /* Make a place to write the result of the instruction. */
2185 result = target;
2186 if (! (result != 0
2187 && GET_CODE (result) == REG
2188 && GET_MODE (result) == insn_mode
2189 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2190 result = gen_reg_rtx (insn_mode);
2192 /* Make a place to hold the source address. We will not expand
2193 the actual source until we are sure that the expansion will
2194 not fail -- there are trees that cannot be expanded twice. */
2195 src_reg = gen_reg_rtx (Pmode);
2197 /* Mark the beginning of the strlen sequence so we can emit the
2198 source operand later. */
2199 before_strlen = get_last_insn ();
2201 char_rtx = const0_rtx;
2202 char_mode = insn_data[(int) icode].operand[2].mode;
2203 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2204 char_mode))
2205 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2207 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2208 char_rtx, GEN_INT (align));
2209 if (! pat)
2210 return 0;
2211 emit_insn (pat);
2213 /* Now that we are assured of success, expand the source. */
2214 start_sequence ();
2215 pat = memory_address (BLKmode,
2216 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
2217 if (pat != src_reg)
2218 emit_move_insn (src_reg, pat);
2219 pat = get_insns ();
2220 end_sequence ();
2222 if (before_strlen)
2223 emit_insn_after (pat, before_strlen);
2224 else
2225 emit_insn_before (pat, get_insns ());
2227 /* Return the value in the proper mode for this function. */
2228 if (GET_MODE (result) == target_mode)
2229 target = result;
2230 else if (target != 0)
2231 convert_move (target, result, 0);
2232 else
2233 target = convert_to_mode (target_mode, result, 0);
2235 return target;
2239 /* Expand a call to the strstr builtin. Return 0 if we failed the
2240 caller should emit a normal call, otherwise try to get the result
2241 in TARGET, if convenient (and in mode MODE if that's convenient). */
2243 static rtx
2244 expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
2246 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2247 return 0;
2248 else
2250 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2251 tree fn;
2252 const char *p1, *p2;
2254 p2 = c_getstr (s2);
2255 if (p2 == NULL)
2256 return 0;
2258 p1 = c_getstr (s1);
2259 if (p1 != NULL)
2261 const char *r = strstr (p1, p2);
2263 if (r == NULL)
2264 return const0_rtx;
2266 /* Return an offset into the constant string argument. */
2267 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2268 s1, convert (TREE_TYPE (s1),
2269 ssize_int (r - p1)))),
2270 target, mode, EXPAND_NORMAL);
2273 if (p2[0] == '\0')
2274 return expand_expr (s1, target, mode, EXPAND_NORMAL);
2276 if (p2[1] != '\0')
2277 return 0;
2279 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2280 if (!fn)
2281 return 0;
2283 /* New argument list transforming strstr(s1, s2) to
2284 strchr(s1, s2[0]). */
2285 arglist =
2286 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2287 arglist = tree_cons (NULL_TREE, s1, arglist);
2288 return expand_expr (build_function_call_expr (fn, arglist),
2289 target, mode, EXPAND_NORMAL);
2293 /* Expand a call to the strchr builtin. Return 0 if we failed the
2294 caller should emit a normal call, otherwise try to get the result
2295 in TARGET, if convenient (and in mode MODE if that's convenient). */
2297 static rtx
2298 expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
2300 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2301 return 0;
2302 else
2304 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2305 const char *p1;
2307 if (TREE_CODE (s2) != INTEGER_CST)
2308 return 0;
2310 p1 = c_getstr (s1);
2311 if (p1 != NULL)
2313 char c;
2314 const char *r;
2316 if (target_char_cast (s2, &c))
2317 return 0;
2319 r = strchr (p1, c);
2321 if (r == NULL)
2322 return const0_rtx;
2324 /* Return an offset into the constant string argument. */
2325 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2326 s1, convert (TREE_TYPE (s1),
2327 ssize_int (r - p1)))),
2328 target, mode, EXPAND_NORMAL);
2331 /* FIXME: Should use here strchrM optab so that ports can optimize
2332 this. */
2333 return 0;
2337 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2338 caller should emit a normal call, otherwise try to get the result
2339 in TARGET, if convenient (and in mode MODE if that's convenient). */
2341 static rtx
2342 expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode)
2344 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2345 return 0;
2346 else
2348 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2349 tree fn;
2350 const char *p1;
2352 if (TREE_CODE (s2) != INTEGER_CST)
2353 return 0;
2355 p1 = c_getstr (s1);
2356 if (p1 != NULL)
2358 char c;
2359 const char *r;
2361 if (target_char_cast (s2, &c))
2362 return 0;
2364 r = strrchr (p1, c);
2366 if (r == NULL)
2367 return const0_rtx;
2369 /* Return an offset into the constant string argument. */
2370 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2371 s1, convert (TREE_TYPE (s1),
2372 ssize_int (r - p1)))),
2373 target, mode, EXPAND_NORMAL);
2376 if (! integer_zerop (s2))
2377 return 0;
2379 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2380 if (!fn)
2381 return 0;
2383 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
2384 return expand_expr (build_function_call_expr (fn, arglist),
2385 target, mode, EXPAND_NORMAL);
2389 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2390 caller should emit a normal call, otherwise try to get the result
2391 in TARGET, if convenient (and in mode MODE if that's convenient). */
2393 static rtx
2394 expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
2396 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2397 return 0;
2398 else
2400 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2401 tree fn;
2402 const char *p1, *p2;
2404 p2 = c_getstr (s2);
2405 if (p2 == NULL)
2406 return 0;
2408 p1 = c_getstr (s1);
2409 if (p1 != NULL)
2411 const char *r = strpbrk (p1, p2);
2413 if (r == NULL)
2414 return const0_rtx;
2416 /* Return an offset into the constant string argument. */
2417 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2418 s1, convert (TREE_TYPE (s1),
2419 ssize_int (r - p1)))),
2420 target, mode, EXPAND_NORMAL);
2423 if (p2[0] == '\0')
2425 /* strpbrk(x, "") == NULL.
2426 Evaluate and ignore the arguments in case they had
2427 side-effects. */
2428 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2429 return const0_rtx;
2432 if (p2[1] != '\0')
2433 return 0; /* Really call strpbrk. */
2435 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2436 if (!fn)
2437 return 0;
2439 /* New argument list transforming strpbrk(s1, s2) to
2440 strchr(s1, s2[0]). */
2441 arglist =
2442 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2443 arglist = tree_cons (NULL_TREE, s1, arglist);
2444 return expand_expr (build_function_call_expr (fn, arglist),
2445 target, mode, EXPAND_NORMAL);
2449 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2450 bytes from constant string DATA + OFFSET and return it as target
2451 constant. */
2453 static rtx
2454 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2455 enum machine_mode mode)
2457 const char *str = (const char *) data;
2459 if (offset < 0
2460 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2461 > strlen (str) + 1))
2462 abort (); /* Attempt to read past the end of constant string. */
2464 return c_readstr (str + offset, mode);
2467 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2468 Return 0 if we failed, the caller should emit a normal call,
2469 otherwise try to get the result in TARGET, if convenient (and in
2470 mode MODE if that's convenient). */
2471 static rtx
2472 expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode)
2474 if (!validate_arglist (arglist,
2475 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2476 return 0;
2477 else
2479 tree dest = TREE_VALUE (arglist);
2480 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2481 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2482 const char *src_str;
2483 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2484 unsigned int dest_align
2485 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2486 rtx dest_mem, src_mem, dest_addr, len_rtx;
2488 /* If DEST is not a pointer type, call the normal function. */
2489 if (dest_align == 0)
2490 return 0;
2492 /* If the LEN parameter is zero, return DEST. */
2493 if (integer_zerop (len))
2495 /* Evaluate and ignore SRC in case it has side-effects. */
2496 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2497 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2500 /* If SRC and DEST are the same (and not volatile), return DEST. */
2501 if (operand_equal_p (src, dest, 0))
2503 /* Evaluate and ignore LEN in case it has side-effects. */
2504 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2505 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2508 /* If either SRC is not a pointer type, don't do this
2509 operation in-line. */
2510 if (src_align == 0)
2511 return 0;
2513 dest_mem = get_memory_rtx (dest);
2514 set_mem_align (dest_mem, dest_align);
2515 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2516 src_str = c_getstr (src);
2518 /* If SRC is a string constant and block move would be done
2519 by pieces, we can avoid loading the string from memory
2520 and only stored the computed constants. */
2521 if (src_str
2522 && GET_CODE (len_rtx) == CONST_INT
2523 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2524 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2525 (void *) src_str, dest_align))
2527 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2528 builtin_memcpy_read_str,
2529 (void *) src_str, dest_align, 0);
2530 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2531 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2532 return dest_mem;
2535 src_mem = get_memory_rtx (src);
2536 set_mem_align (src_mem, src_align);
2538 /* Copy word part most expediently. */
2539 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2540 BLOCK_OP_NORMAL);
2542 if (dest_addr == 0)
2544 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2545 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2547 return dest_addr;
2551 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2552 Return 0 if we failed the caller should emit a normal call,
2553 otherwise try to get the result in TARGET, if convenient (and in
2554 mode MODE if that's convenient). If ENDP is 0 return the
2555 destination pointer, if ENDP is 1 return the end pointer ala
2556 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2557 stpcpy. */
2559 static rtx
2560 expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
2561 int endp)
2563 if (!validate_arglist (arglist,
2564 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2565 return 0;
2566 /* If return value is ignored, transform mempcpy into memcpy. */
2567 else if (target == const0_rtx)
2569 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2571 if (!fn)
2572 return 0;
2574 return expand_expr (build_function_call_expr (fn, arglist),
2575 target, mode, EXPAND_NORMAL);
2577 else
2579 tree dest = TREE_VALUE (arglist);
2580 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2581 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2582 const char *src_str;
2583 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2584 unsigned int dest_align
2585 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2586 rtx dest_mem, src_mem, len_rtx;
2588 /* If DEST is not a pointer type, call the normal function. */
2589 if (dest_align == 0)
2590 return 0;
2592 /* If SRC and DEST are the same (and not volatile), do nothing. */
2593 if (operand_equal_p (src, dest, 0))
2595 tree expr;
2597 if (endp == 0)
2599 /* Evaluate and ignore LEN in case it has side-effects. */
2600 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2601 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2604 if (endp == 2)
2605 len = fold (build (MINUS_EXPR, TREE_TYPE (len), dest,
2606 integer_one_node));
2607 len = convert (TREE_TYPE (dest), len);
2608 expr = fold (build (PLUS_EXPR, TREE_TYPE (dest), dest, len));
2609 return expand_expr (expr, target, mode, EXPAND_NORMAL);
2612 /* If LEN is not constant, call the normal function. */
2613 if (! host_integerp (len, 1))
2614 return 0;
2616 /* If the LEN parameter is zero, return DEST. */
2617 if (tree_low_cst (len, 1) == 0)
2619 /* Evaluate and ignore SRC in case it has side-effects. */
2620 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2621 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2624 /* If either SRC is not a pointer type, don't do this
2625 operation in-line. */
2626 if (src_align == 0)
2627 return 0;
2629 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2630 src_str = c_getstr (src);
2632 /* If SRC is a string constant and block move would be done
2633 by pieces, we can avoid loading the string from memory
2634 and only stored the computed constants. */
2635 if (src_str
2636 && GET_CODE (len_rtx) == CONST_INT
2637 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2638 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2639 (void *) src_str, dest_align))
2641 dest_mem = get_memory_rtx (dest);
2642 set_mem_align (dest_mem, dest_align);
2643 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2644 builtin_memcpy_read_str,
2645 (void *) src_str, dest_align, endp);
2646 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2647 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2648 return dest_mem;
2651 if (GET_CODE (len_rtx) == CONST_INT
2652 && can_move_by_pieces (INTVAL (len_rtx),
2653 MIN (dest_align, src_align)))
2655 dest_mem = get_memory_rtx (dest);
2656 set_mem_align (dest_mem, dest_align);
2657 src_mem = get_memory_rtx (src);
2658 set_mem_align (src_mem, src_align);
2659 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2660 MIN (dest_align, src_align), endp);
2661 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2662 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2663 return dest_mem;
2666 return 0;
2670 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2671 if we failed the caller should emit a normal call. */
2673 static rtx
2674 expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode)
2676 if (!validate_arglist (arglist,
2677 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2678 return 0;
2679 else
2681 tree dest = TREE_VALUE (arglist);
2682 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2683 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2685 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2686 unsigned int dest_align
2687 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2689 /* If DEST is not a pointer type, call the normal function. */
2690 if (dest_align == 0)
2691 return 0;
2693 /* If the LEN parameter is zero, return DEST. */
2694 if (integer_zerop (len))
2696 /* Evaluate and ignore SRC in case it has side-effects. */
2697 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2698 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2701 /* If SRC and DEST are the same (and not volatile), return DEST. */
2702 if (operand_equal_p (src, dest, 0))
2704 /* Evaluate and ignore LEN in case it has side-effects. */
2705 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2706 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2709 /* If either SRC is not a pointer type, don't do this
2710 operation in-line. */
2711 if (src_align == 0)
2712 return 0;
2714 /* If src is categorized for a readonly section we can use
2715 normal memcpy. */
2716 if (readonly_data_expr (src))
2718 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2719 if (!fn)
2720 return 0;
2721 return expand_expr (build_function_call_expr (fn, arglist),
2722 target, mode, EXPAND_NORMAL);
2725 /* Otherwise, call the normal function. */
2726 return 0;
2730 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2731 if we failed the caller should emit a normal call. */
2733 static rtx
2734 expand_builtin_bcopy (tree arglist)
2736 tree src, dest, size, newarglist;
2738 if (!validate_arglist (arglist,
2739 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2740 return NULL_RTX;
2742 src = TREE_VALUE (arglist);
2743 dest = TREE_VALUE (TREE_CHAIN (arglist));
2744 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2746 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2747 memmove(ptr y, ptr x, size_t z). This is done this way
2748 so that if it isn't expanded inline, we fallback to
2749 calling bcopy instead of memmove. */
2751 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2752 newarglist = tree_cons (NULL_TREE, src, newarglist);
2753 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2755 return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode);
2758 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2759 if we failed the caller should emit a normal call, otherwise try to get
2760 the result in TARGET, if convenient (and in mode MODE if that's
2761 convenient). */
2763 static rtx
2764 expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode)
2766 tree fn, len, src, dst;
2768 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2769 return 0;
2771 src = TREE_VALUE (TREE_CHAIN (arglist));
2772 dst = TREE_VALUE (arglist);
2774 /* If SRC and DST are equal (and not volatile), return DST. */
2775 if (operand_equal_p (src, dst, 0))
2776 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2778 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2779 if (!fn)
2780 return 0;
2782 len = c_strlen (src, 1);
2783 if (len == 0 || TREE_SIDE_EFFECTS (len))
2784 return 0;
2786 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2787 arglist = build_tree_list (NULL_TREE, len);
2788 arglist = tree_cons (NULL_TREE, src, arglist);
2789 arglist = tree_cons (NULL_TREE, dst, arglist);
2790 return expand_expr (build_function_call_expr (fn, arglist),
2791 target, mode, EXPAND_NORMAL);
2794 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2795 Return 0 if we failed the caller should emit a normal call,
2796 otherwise try to get the result in TARGET, if convenient (and in
2797 mode MODE if that's convenient). */
2799 static rtx
2800 expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode)
2802 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2803 return 0;
2804 else
2806 tree dst, src, len;
2808 /* If return value is ignored, transform stpcpy into strcpy. */
2809 if (target == const0_rtx)
2811 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2812 if (!fn)
2813 return 0;
2815 return expand_expr (build_function_call_expr (fn, arglist),
2816 target, mode, EXPAND_NORMAL);
2819 /* Ensure we get an actual string whose length can be evaluated at
2820 compile-time, not an expression containing a string. This is
2821 because the latter will potentially produce pessimized code
2822 when used to produce the return value. */
2823 src = TREE_VALUE (TREE_CHAIN (arglist));
2824 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2825 return 0;
2827 dst = TREE_VALUE (arglist);
2828 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2829 arglist = build_tree_list (NULL_TREE, len);
2830 arglist = tree_cons (NULL_TREE, src, arglist);
2831 arglist = tree_cons (NULL_TREE, dst, arglist);
2832 return expand_builtin_mempcpy (arglist, target, mode, /*endp=*/2);
2836 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2837 bytes from constant string DATA + OFFSET and return it as target
2838 constant. */
2840 static rtx
2841 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
2842 enum machine_mode mode)
2844 const char *str = (const char *) data;
2846 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2847 return const0_rtx;
2849 return c_readstr (str + offset, mode);
2852 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2853 if we failed the caller should emit a normal call. */
2855 static rtx
2856 expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode)
2858 if (!validate_arglist (arglist,
2859 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2860 return 0;
2861 else
2863 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
2864 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2865 tree fn;
2867 /* We must be passed a constant len parameter. */
2868 if (TREE_CODE (len) != INTEGER_CST)
2869 return 0;
2871 /* If the len parameter is zero, return the dst parameter. */
2872 if (integer_zerop (len))
2874 /* Evaluate and ignore the src argument in case it has
2875 side-effects. */
2876 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2877 VOIDmode, EXPAND_NORMAL);
2878 /* Return the dst parameter. */
2879 return expand_expr (TREE_VALUE (arglist), target, mode,
2880 EXPAND_NORMAL);
2883 /* Now, we must be passed a constant src ptr parameter. */
2884 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2885 return 0;
2887 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2889 /* We're required to pad with trailing zeros if the requested
2890 len is greater than strlen(s2)+1. In that case try to
2891 use store_by_pieces, if it fails, punt. */
2892 if (tree_int_cst_lt (slen, len))
2894 tree dest = TREE_VALUE (arglist);
2895 unsigned int dest_align
2896 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2897 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2898 rtx dest_mem;
2900 if (!p || dest_align == 0 || !host_integerp (len, 1)
2901 || !can_store_by_pieces (tree_low_cst (len, 1),
2902 builtin_strncpy_read_str,
2903 (void *) p, dest_align))
2904 return 0;
2906 dest_mem = get_memory_rtx (dest);
2907 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2908 builtin_strncpy_read_str,
2909 (void *) p, dest_align, 0);
2910 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2911 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2912 return dest_mem;
2915 /* OK transform into builtin memcpy. */
2916 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2917 if (!fn)
2918 return 0;
2919 return expand_expr (build_function_call_expr (fn, arglist),
2920 target, mode, EXPAND_NORMAL);
2924 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2925 bytes from constant string DATA + OFFSET and return it as target
2926 constant. */
2928 static rtx
2929 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2930 enum machine_mode mode)
2932 const char *c = (const char *) data;
2933 char *p = alloca (GET_MODE_SIZE (mode));
2935 memset (p, *c, GET_MODE_SIZE (mode));
2937 return c_readstr (p, mode);
2940 /* Callback routine for store_by_pieces. Return the RTL of a register
2941 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
2942 char value given in the RTL register data. For example, if mode is
2943 4 bytes wide, return the RTL for 0x01010101*data. */
2945 static rtx
2946 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2947 enum machine_mode mode)
2949 rtx target, coeff;
2950 size_t size;
2951 char *p;
2953 size = GET_MODE_SIZE (mode);
2954 if (size == 1)
2955 return (rtx) data;
2957 p = alloca (size);
2958 memset (p, 1, size);
2959 coeff = c_readstr (p, mode);
2961 target = convert_to_mode (mode, (rtx) data, 1);
2962 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
2963 return force_reg (mode, target);
2966 /* Expand expression EXP, which is a call to the memset builtin. Return 0
2967 if we failed the caller should emit a normal call, otherwise try to get
2968 the result in TARGET, if convenient (and in mode MODE if that's
2969 convenient). */
2971 static rtx
2972 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
2974 if (!validate_arglist (arglist,
2975 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
2976 return 0;
2977 else
2979 tree dest = TREE_VALUE (arglist);
2980 tree val = TREE_VALUE (TREE_CHAIN (arglist));
2981 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2982 char c;
2984 unsigned int dest_align
2985 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2986 rtx dest_mem, dest_addr, len_rtx;
2988 /* If DEST is not a pointer type, don't do this
2989 operation in-line. */
2990 if (dest_align == 0)
2991 return 0;
2993 /* If the LEN parameter is zero, return DEST. */
2994 if (integer_zerop (len))
2996 /* Evaluate and ignore VAL in case it has side-effects. */
2997 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
2998 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3001 if (TREE_CODE (val) != INTEGER_CST)
3003 rtx val_rtx;
3005 if (!host_integerp (len, 1))
3006 return 0;
3008 if (optimize_size && tree_low_cst (len, 1) > 1)
3009 return 0;
3011 /* Assume that we can memset by pieces if we can store the
3012 * the coefficients by pieces (in the required modes).
3013 * We can't pass builtin_memset_gen_str as that emits RTL. */
3014 c = 1;
3015 if (!can_store_by_pieces (tree_low_cst (len, 1),
3016 builtin_memset_read_str,
3017 &c, dest_align))
3018 return 0;
3020 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3021 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3022 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3023 val_rtx);
3024 dest_mem = get_memory_rtx (dest);
3025 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3026 builtin_memset_gen_str,
3027 val_rtx, dest_align, 0);
3028 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3029 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3030 return dest_mem;
3033 if (target_char_cast (val, &c))
3034 return 0;
3036 if (c)
3038 if (!host_integerp (len, 1))
3039 return 0;
3040 if (!can_store_by_pieces (tree_low_cst (len, 1),
3041 builtin_memset_read_str, &c,
3042 dest_align))
3043 return 0;
3045 dest_mem = get_memory_rtx (dest);
3046 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3047 builtin_memset_read_str,
3048 &c, dest_align, 0);
3049 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3050 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3051 return dest_mem;
3054 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3056 dest_mem = get_memory_rtx (dest);
3057 set_mem_align (dest_mem, dest_align);
3058 dest_addr = clear_storage (dest_mem, len_rtx);
3060 if (dest_addr == 0)
3062 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3063 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3066 return dest_addr;
3070 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3071 if we failed the caller should emit a normal call. */
3073 static rtx
3074 expand_builtin_bzero (tree arglist)
3076 tree dest, size, newarglist;
3078 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3079 return NULL_RTX;
3081 dest = TREE_VALUE (arglist);
3082 size = TREE_VALUE (TREE_CHAIN (arglist));
3084 /* New argument list transforming bzero(ptr x, int y) to
3085 memset(ptr x, int 0, size_t y). This is done this way
3086 so that if it isn't expanded inline, we fallback to
3087 calling bzero instead of memset. */
3089 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
3090 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3091 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3093 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3096 /* Expand expression EXP, which is a call to the memcmp built-in function.
3097 ARGLIST is the argument list for this call. Return 0 if we failed and the
3098 caller should emit a normal call, otherwise try to get the result in
3099 TARGET, if convenient (and in mode MODE, if that's convenient). */
3101 static rtx
3102 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3103 enum machine_mode mode)
3105 tree arg1, arg2, len;
3106 const char *p1, *p2;
3108 if (!validate_arglist (arglist,
3109 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3110 return 0;
3112 arg1 = TREE_VALUE (arglist);
3113 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3114 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3116 /* If the len parameter is zero, return zero. */
3117 if (integer_zerop (len))
3119 /* Evaluate and ignore arg1 and arg2 in case they have
3120 side-effects. */
3121 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3122 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3123 return const0_rtx;
3126 /* If both arguments are equal (and not volatile), return zero. */
3127 if (operand_equal_p (arg1, arg2, 0))
3129 /* Evaluate and ignore len in case it has side-effects. */
3130 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3131 return const0_rtx;
3134 p1 = c_getstr (arg1);
3135 p2 = c_getstr (arg2);
3137 /* If all arguments are constant, and the value of len is not greater
3138 than the lengths of arg1 and arg2, evaluate at compile-time. */
3139 if (host_integerp (len, 1) && p1 && p2
3140 && compare_tree_int (len, strlen (p1) + 1) <= 0
3141 && compare_tree_int (len, strlen (p2) + 1) <= 0)
3143 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
3145 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3148 /* If len parameter is one, return an expression corresponding to
3149 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3150 if (integer_onep (len))
3152 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3153 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3154 tree ind1 =
3155 fold (build1 (CONVERT_EXPR, integer_type_node,
3156 build1 (INDIRECT_REF, cst_uchar_node,
3157 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3158 tree ind2 =
3159 fold (build1 (CONVERT_EXPR, integer_type_node,
3160 build1 (INDIRECT_REF, cst_uchar_node,
3161 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3162 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3163 return expand_expr (result, target, mode, EXPAND_NORMAL);
3166 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3168 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3169 rtx result;
3170 rtx insn;
3172 int arg1_align
3173 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3174 int arg2_align
3175 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3176 enum machine_mode insn_mode;
3178 #ifdef HAVE_cmpmemsi
3179 if (HAVE_cmpmemsi)
3180 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3181 else
3182 #endif
3183 #ifdef HAVE_cmpstrsi
3184 if (HAVE_cmpstrsi)
3185 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3186 else
3187 #endif
3188 return 0;
3190 /* If we don't have POINTER_TYPE, call the function. */
3191 if (arg1_align == 0 || arg2_align == 0)
3192 return 0;
3194 /* Make a place to write the result of the instruction. */
3195 result = target;
3196 if (! (result != 0
3197 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3198 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3199 result = gen_reg_rtx (insn_mode);
3201 arg1_rtx = get_memory_rtx (arg1);
3202 arg2_rtx = get_memory_rtx (arg2);
3203 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3204 #ifdef HAVE_cmpmemsi
3205 if (HAVE_cmpmemsi)
3206 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3207 GEN_INT (MIN (arg1_align, arg2_align)));
3208 else
3209 #endif
3210 #ifdef HAVE_cmpstrsi
3211 if (HAVE_cmpstrsi)
3212 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3213 GEN_INT (MIN (arg1_align, arg2_align)));
3214 else
3215 #endif
3216 abort ();
3218 if (insn)
3219 emit_insn (insn);
3220 else
3221 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3222 TYPE_MODE (integer_type_node), 3,
3223 XEXP (arg1_rtx, 0), Pmode,
3224 XEXP (arg2_rtx, 0), Pmode,
3225 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3226 TREE_UNSIGNED (sizetype)),
3227 TYPE_MODE (sizetype));
3229 /* Return the value in the proper mode for this function. */
3230 mode = TYPE_MODE (TREE_TYPE (exp));
3231 if (GET_MODE (result) == mode)
3232 return result;
3233 else if (target != 0)
3235 convert_move (target, result, 0);
3236 return target;
3238 else
3239 return convert_to_mode (mode, result, 0);
3241 #endif
3243 return 0;
3246 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3247 if we failed the caller should emit a normal call, otherwise try to get
3248 the result in TARGET, if convenient. */
3250 static rtx
3251 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3253 tree arglist = TREE_OPERAND (exp, 1);
3254 tree arg1, arg2;
3255 const char *p1, *p2;
3257 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3258 return 0;
3260 arg1 = TREE_VALUE (arglist);
3261 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3263 /* If both arguments are equal (and not volatile), return zero. */
3264 if (operand_equal_p (arg1, arg2, 0))
3265 return const0_rtx;
3267 p1 = c_getstr (arg1);
3268 p2 = c_getstr (arg2);
3270 if (p1 && p2)
3272 const int i = strcmp (p1, p2);
3273 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
3276 /* If either arg is "", return an expression corresponding to
3277 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3278 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3280 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3281 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3282 tree ind1 =
3283 fold (build1 (CONVERT_EXPR, integer_type_node,
3284 build1 (INDIRECT_REF, cst_uchar_node,
3285 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3286 tree ind2 =
3287 fold (build1 (CONVERT_EXPR, integer_type_node,
3288 build1 (INDIRECT_REF, cst_uchar_node,
3289 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3290 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3291 return expand_expr (result, target, mode, EXPAND_NORMAL);
3294 #ifdef HAVE_cmpstrsi
3295 if (HAVE_cmpstrsi)
3297 tree len, len1, len2;
3298 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3299 rtx result, insn;
3300 tree fndecl;
3302 int arg1_align
3303 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3304 int arg2_align
3305 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3306 enum machine_mode insn_mode
3307 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3309 len1 = c_strlen (arg1, 1);
3310 len2 = c_strlen (arg2, 1);
3312 if (len1)
3313 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3314 if (len2)
3315 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3317 /* If we don't have a constant length for the first, use the length
3318 of the second, if we know it. We don't require a constant for
3319 this case; some cost analysis could be done if both are available
3320 but neither is constant. For now, assume they're equally cheap,
3321 unless one has side effects. If both strings have constant lengths,
3322 use the smaller. */
3324 if (!len1)
3325 len = len2;
3326 else if (!len2)
3327 len = len1;
3328 else if (TREE_SIDE_EFFECTS (len1))
3329 len = len2;
3330 else if (TREE_SIDE_EFFECTS (len2))
3331 len = len1;
3332 else if (TREE_CODE (len1) != INTEGER_CST)
3333 len = len2;
3334 else if (TREE_CODE (len2) != INTEGER_CST)
3335 len = len1;
3336 else if (tree_int_cst_lt (len1, len2))
3337 len = len1;
3338 else
3339 len = len2;
3341 /* If both arguments have side effects, we cannot optimize. */
3342 if (!len || TREE_SIDE_EFFECTS (len))
3343 return 0;
3345 /* If we don't have POINTER_TYPE, call the function. */
3346 if (arg1_align == 0 || arg2_align == 0)
3347 return 0;
3349 /* Make a place to write the result of the instruction. */
3350 result = target;
3351 if (! (result != 0
3352 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3353 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3354 result = gen_reg_rtx (insn_mode);
3356 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3357 arg1 = save_expr (arg1);
3358 arg2 = save_expr (arg2);
3360 arg1_rtx = get_memory_rtx (arg1);
3361 arg2_rtx = get_memory_rtx (arg2);
3362 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3363 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3364 GEN_INT (MIN (arg1_align, arg2_align)));
3365 if (insn)
3367 emit_insn (insn);
3369 /* Return the value in the proper mode for this function. */
3370 mode = TYPE_MODE (TREE_TYPE (exp));
3371 if (GET_MODE (result) == mode)
3372 return result;
3373 if (target == 0)
3374 return convert_to_mode (mode, result, 0);
3375 convert_move (target, result, 0);
3376 return target;
3379 /* Expand the library call ourselves using a stabilized argument
3380 list to avoid re-evaluating the function's arguments twice. */
3381 arglist = build_tree_list (NULL_TREE, arg2);
3382 arglist = tree_cons (NULL_TREE, arg1, arglist);
3383 fndecl = get_callee_fndecl (exp);
3384 exp = build_function_call_expr (fndecl, arglist);
3385 return expand_call (exp, target, target == const0_rtx);
3387 #endif
3388 return 0;
3391 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3392 if we failed the caller should emit a normal call, otherwise try to get
3393 the result in TARGET, if convenient. */
3395 static rtx
3396 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3398 tree arglist = TREE_OPERAND (exp, 1);
3399 tree arg1, arg2, arg3;
3400 const char *p1, *p2;
3402 if (!validate_arglist (arglist,
3403 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3404 return 0;
3406 arg1 = TREE_VALUE (arglist);
3407 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3408 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3410 /* If the len parameter is zero, return zero. */
3411 if (integer_zerop (arg3))
3413 /* Evaluate and ignore arg1 and arg2 in case they have
3414 side-effects. */
3415 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3416 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3417 return const0_rtx;
3420 /* If arg1 and arg2 are equal (and not volatile), return zero. */
3421 if (operand_equal_p (arg1, arg2, 0))
3423 /* Evaluate and ignore arg3 in case it has side-effects. */
3424 expand_expr (arg3, const0_rtx, VOIDmode, EXPAND_NORMAL);
3425 return const0_rtx;
3428 p1 = c_getstr (arg1);
3429 p2 = c_getstr (arg2);
3431 /* If all arguments are constant, evaluate at compile-time. */
3432 if (host_integerp (arg3, 1) && p1 && p2)
3434 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
3435 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3438 /* If len == 1 or (either string parameter is "" and (len >= 1)),
3439 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
3440 if (host_integerp (arg3, 1)
3441 && (tree_low_cst (arg3, 1) == 1
3442 || (tree_low_cst (arg3, 1) > 1
3443 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
3445 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3446 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3447 tree ind1 =
3448 fold (build1 (CONVERT_EXPR, integer_type_node,
3449 build1 (INDIRECT_REF, cst_uchar_node,
3450 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3451 tree ind2 =
3452 fold (build1 (CONVERT_EXPR, integer_type_node,
3453 build1 (INDIRECT_REF, cst_uchar_node,
3454 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3455 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3456 return expand_expr (result, target, mode, EXPAND_NORMAL);
3459 /* If c_strlen can determine an expression for one of the string
3460 lengths, and it doesn't have side effects, then emit cmpstrsi
3461 using length MIN(strlen(string)+1, arg3). */
3462 #ifdef HAVE_cmpstrsi
3463 if (HAVE_cmpstrsi)
3465 tree len, len1, len2;
3466 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3467 rtx result, insn;
3468 tree fndecl;
3470 int arg1_align
3471 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3472 int arg2_align
3473 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3474 enum machine_mode insn_mode
3475 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3477 len1 = c_strlen (arg1, 1);
3478 len2 = c_strlen (arg2, 1);
3480 if (len1)
3481 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3482 if (len2)
3483 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3485 /* If we don't have a constant length for the first, use the length
3486 of the second, if we know it. We don't require a constant for
3487 this case; some cost analysis could be done if both are available
3488 but neither is constant. For now, assume they're equally cheap,
3489 unless one has side effects. If both strings have constant lengths,
3490 use the smaller. */
3492 if (!len1)
3493 len = len2;
3494 else if (!len2)
3495 len = len1;
3496 else if (TREE_SIDE_EFFECTS (len1))
3497 len = len2;
3498 else if (TREE_SIDE_EFFECTS (len2))
3499 len = len1;
3500 else if (TREE_CODE (len1) != INTEGER_CST)
3501 len = len2;
3502 else if (TREE_CODE (len2) != INTEGER_CST)
3503 len = len1;
3504 else if (tree_int_cst_lt (len1, len2))
3505 len = len1;
3506 else
3507 len = len2;
3509 /* If both arguments have side effects, we cannot optimize. */
3510 if (!len || TREE_SIDE_EFFECTS (len))
3511 return 0;
3513 /* The actual new length parameter is MIN(len,arg3). */
3514 len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
3516 /* If we don't have POINTER_TYPE, call the function. */
3517 if (arg1_align == 0 || arg2_align == 0)
3518 return 0;
3520 /* Make a place to write the result of the instruction. */
3521 result = target;
3522 if (! (result != 0
3523 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3524 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3525 result = gen_reg_rtx (insn_mode);
3527 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3528 arg1 = save_expr (arg1);
3529 arg2 = save_expr (arg2);
3530 len = save_expr (len);
3532 arg1_rtx = get_memory_rtx (arg1);
3533 arg2_rtx = get_memory_rtx (arg2);
3534 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3535 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3536 GEN_INT (MIN (arg1_align, arg2_align)));
3537 if (insn)
3539 emit_insn (insn);
3541 /* Return the value in the proper mode for this function. */
3542 mode = TYPE_MODE (TREE_TYPE (exp));
3543 if (GET_MODE (result) == mode)
3544 return result;
3545 if (target == 0)
3546 return convert_to_mode (mode, result, 0);
3547 convert_move (target, result, 0);
3548 return target;
3551 /* Expand the library call ourselves using a stabilized argument
3552 list to avoid re-evaluating the function's arguments twice. */
3553 arglist = build_tree_list (NULL_TREE, len);
3554 arglist = tree_cons (NULL_TREE, arg2, arglist);
3555 arglist = tree_cons (NULL_TREE, arg1, arglist);
3556 fndecl = get_callee_fndecl (exp);
3557 exp = build_function_call_expr (fndecl, arglist);
3558 return expand_call (exp, target, target == const0_rtx);
3560 #endif
3561 return 0;
3564 /* Expand expression EXP, which is a call to the strcat builtin.
3565 Return 0 if we failed the caller should emit a normal call,
3566 otherwise try to get the result in TARGET, if convenient. */
3568 static rtx
3569 expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
3571 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3572 return 0;
3573 else
3575 tree dst = TREE_VALUE (arglist),
3576 src = TREE_VALUE (TREE_CHAIN (arglist));
3577 const char *p = c_getstr (src);
3579 if (p)
3581 /* If the string length is zero, return the dst parameter. */
3582 if (*p == '\0')
3583 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3584 else if (!optimize_size)
3586 /* Otherwise if !optimize_size, see if we can store by
3587 pieces into (dst + strlen(dst)). */
3588 tree newdst, arglist,
3589 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3591 /* This is the length argument. */
3592 arglist = build_tree_list (NULL_TREE,
3593 fold (size_binop (PLUS_EXPR,
3594 c_strlen (src, 0),
3595 ssize_int (1))));
3596 /* Prepend src argument. */
3597 arglist = tree_cons (NULL_TREE, src, arglist);
3599 /* We're going to use dst more than once. */
3600 dst = save_expr (dst);
3602 /* Create strlen (dst). */
3603 newdst =
3604 fold (build_function_call_expr (strlen_fn,
3605 build_tree_list (NULL_TREE,
3606 dst)));
3607 /* Create (dst + strlen (dst)). */
3608 newdst = fold (build (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3610 /* Prepend the new dst argument. */
3611 arglist = tree_cons (NULL_TREE, newdst, arglist);
3613 /* We don't want to get turned into a memcpy if the
3614 target is const0_rtx, i.e. when the return value
3615 isn't used. That would produce pessimized code so
3616 pass in a target of zero, it should never actually be
3617 used. If this was successful return the original
3618 dst, not the result of mempcpy. */
3619 if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0))
3620 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3621 else
3622 return 0;
3626 return 0;
3630 /* Expand expression EXP, which is a call to the strncat builtin.
3631 Return 0 if we failed the caller should emit a normal call,
3632 otherwise try to get the result in TARGET, if convenient. */
3634 static rtx
3635 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3637 if (!validate_arglist (arglist,
3638 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3639 return 0;
3640 else
3642 tree dst = TREE_VALUE (arglist),
3643 src = TREE_VALUE (TREE_CHAIN (arglist)),
3644 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3645 const char *p = c_getstr (src);
3647 /* If the requested length is zero, or the src parameter string
3648 length is zero, return the dst parameter. */
3649 if (integer_zerop (len) || (p && *p == '\0'))
3651 /* Evaluate and ignore the src and len parameters in case
3652 they have side-effects. */
3653 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3654 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3655 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3658 /* If the requested len is greater than or equal to the string
3659 length, call strcat. */
3660 if (TREE_CODE (len) == INTEGER_CST && p
3661 && compare_tree_int (len, strlen (p)) >= 0)
3663 tree newarglist
3664 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
3665 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
3667 /* If the replacement _DECL isn't initialized, don't do the
3668 transformation. */
3669 if (!fn)
3670 return 0;
3672 return expand_expr (build_function_call_expr (fn, newarglist),
3673 target, mode, EXPAND_NORMAL);
3675 return 0;
3679 /* Expand expression EXP, which is a call to the strspn builtin.
3680 Return 0 if we failed the caller should emit a normal call,
3681 otherwise try to get the result in TARGET, if convenient. */
3683 static rtx
3684 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3686 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3687 return 0;
3688 else
3690 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3691 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3693 /* If both arguments are constants, evaluate at compile-time. */
3694 if (p1 && p2)
3696 const size_t r = strspn (p1, p2);
3697 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3700 /* If either argument is "", return 0. */
3701 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3703 /* Evaluate and ignore both arguments in case either one has
3704 side-effects. */
3705 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3706 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3707 return const0_rtx;
3709 return 0;
3713 /* Expand expression EXP, which is a call to the strcspn builtin.
3714 Return 0 if we failed the caller should emit a normal call,
3715 otherwise try to get the result in TARGET, if convenient. */
3717 static rtx
3718 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3720 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3721 return 0;
3722 else
3724 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3725 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3727 /* If both arguments are constants, evaluate at compile-time. */
3728 if (p1 && p2)
3730 const size_t r = strcspn (p1, p2);
3731 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3734 /* If the first argument is "", return 0. */
3735 if (p1 && *p1 == '\0')
3737 /* Evaluate and ignore argument s2 in case it has
3738 side-effects. */
3739 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3740 return const0_rtx;
3743 /* If the second argument is "", return __builtin_strlen(s1). */
3744 if (p2 && *p2 == '\0')
3746 tree newarglist = build_tree_list (NULL_TREE, s1),
3747 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3749 /* If the replacement _DECL isn't initialized, don't do the
3750 transformation. */
3751 if (!fn)
3752 return 0;
3754 return expand_expr (build_function_call_expr (fn, newarglist),
3755 target, mode, EXPAND_NORMAL);
3757 return 0;
3761 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3762 if that's convenient. */
3765 expand_builtin_saveregs (void)
3767 rtx val, seq;
3769 /* Don't do __builtin_saveregs more than once in a function.
3770 Save the result of the first call and reuse it. */
3771 if (saveregs_value != 0)
3772 return saveregs_value;
3774 /* When this function is called, it means that registers must be
3775 saved on entry to this function. So we migrate the call to the
3776 first insn of this function. */
3778 start_sequence ();
3780 /* Do whatever the machine needs done in this case. */
3781 val = targetm.calls.expand_builtin_saveregs ();
3783 seq = get_insns ();
3784 end_sequence ();
3786 saveregs_value = val;
3788 /* Put the insns after the NOTE that starts the function. If this
3789 is inside a start_sequence, make the outer-level insn chain current, so
3790 the code is placed at the start of the function. */
3791 push_topmost_sequence ();
3792 emit_insn_after (seq, get_insns ());
3793 pop_topmost_sequence ();
3795 return val;
3798 /* __builtin_args_info (N) returns word N of the arg space info
3799 for the current function. The number and meanings of words
3800 is controlled by the definition of CUMULATIVE_ARGS. */
3802 static rtx
3803 expand_builtin_args_info (tree arglist)
3805 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3806 int *word_ptr = (int *) &current_function_args_info;
3808 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
3809 abort ();
3811 if (arglist != 0)
3813 if (!host_integerp (TREE_VALUE (arglist), 0))
3814 error ("argument of `__builtin_args_info' must be constant");
3815 else
3817 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3819 if (wordnum < 0 || wordnum >= nwords)
3820 error ("argument of `__builtin_args_info' out of range");
3821 else
3822 return GEN_INT (word_ptr[wordnum]);
3825 else
3826 error ("missing argument in `__builtin_args_info'");
3828 return const0_rtx;
3831 /* Expand ARGLIST, from a call to __builtin_next_arg. */
3833 static rtx
3834 expand_builtin_next_arg (tree arglist)
3836 tree fntype = TREE_TYPE (current_function_decl);
3838 if (TYPE_ARG_TYPES (fntype) == 0
3839 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3840 == void_type_node))
3842 error ("`va_start' used in function with fixed args");
3843 return const0_rtx;
3846 if (arglist)
3848 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
3849 tree arg = TREE_VALUE (arglist);
3851 /* Strip off all nops for the sake of the comparison. This
3852 is not quite the same as STRIP_NOPS. It does more.
3853 We must also strip off INDIRECT_EXPR for C++ reference
3854 parameters. */
3855 while (TREE_CODE (arg) == NOP_EXPR
3856 || TREE_CODE (arg) == CONVERT_EXPR
3857 || TREE_CODE (arg) == NON_LVALUE_EXPR
3858 || TREE_CODE (arg) == INDIRECT_REF)
3859 arg = TREE_OPERAND (arg, 0);
3860 if (arg != last_parm)
3861 warning ("second parameter of `va_start' not last named argument");
3863 else
3864 /* Evidently an out of date version of <stdarg.h>; can't validate
3865 va_start's second argument, but can still work as intended. */
3866 warning ("`__builtin_next_arg' called without an argument");
3868 return expand_binop (Pmode, add_optab,
3869 current_function_internal_arg_pointer,
3870 current_function_arg_offset_rtx,
3871 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3874 /* Make it easier for the backends by protecting the valist argument
3875 from multiple evaluations. */
3877 static tree
3878 stabilize_va_list (tree valist, int needs_lvalue)
3880 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3882 if (TREE_SIDE_EFFECTS (valist))
3883 valist = save_expr (valist);
3885 /* For this case, the backends will be expecting a pointer to
3886 TREE_TYPE (va_list_type_node), but it's possible we've
3887 actually been given an array (an actual va_list_type_node).
3888 So fix it. */
3889 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3891 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3892 tree p2 = build_pointer_type (va_list_type_node);
3894 valist = build1 (ADDR_EXPR, p2, valist);
3895 valist = fold (build1 (NOP_EXPR, p1, valist));
3898 else
3900 tree pt;
3902 if (! needs_lvalue)
3904 if (! TREE_SIDE_EFFECTS (valist))
3905 return valist;
3907 pt = build_pointer_type (va_list_type_node);
3908 valist = fold (build1 (ADDR_EXPR, pt, valist));
3909 TREE_SIDE_EFFECTS (valist) = 1;
3912 if (TREE_SIDE_EFFECTS (valist))
3913 valist = save_expr (valist);
3914 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
3915 valist));
3918 return valist;
3921 /* The "standard" definition of va_list is void*. */
3923 tree
3924 std_build_builtin_va_list (void)
3926 return ptr_type_node;
3929 /* The "standard" implementation of va_start: just assign `nextarg' to
3930 the variable. */
3932 void
3933 std_expand_builtin_va_start (tree valist, rtx nextarg)
3935 tree t;
3937 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3938 make_tree (ptr_type_node, nextarg));
3939 TREE_SIDE_EFFECTS (t) = 1;
3941 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3944 /* Expand ARGLIST, from a call to __builtin_va_start. */
3946 static rtx
3947 expand_builtin_va_start (tree arglist)
3949 rtx nextarg;
3950 tree chain, valist;
3952 chain = TREE_CHAIN (arglist);
3954 if (TREE_CHAIN (chain))
3955 error ("too many arguments to function `va_start'");
3957 nextarg = expand_builtin_next_arg (chain);
3958 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3960 #ifdef EXPAND_BUILTIN_VA_START
3961 EXPAND_BUILTIN_VA_START (valist, nextarg);
3962 #else
3963 std_expand_builtin_va_start (valist, nextarg);
3964 #endif
3966 return const0_rtx;
3969 /* The "standard" implementation of va_arg: read the value from the
3970 current (padded) address and increment by the (padded) size. */
3973 std_expand_builtin_va_arg (tree valist, tree type)
3975 tree addr_tree, t, type_size = NULL;
3976 tree align, alignm1;
3977 tree rounded_size;
3978 rtx addr;
3980 /* Compute the rounded size of the type. */
3981 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
3982 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
3983 if (type == error_mark_node
3984 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
3985 || TREE_OVERFLOW (type_size))
3986 rounded_size = size_zero_node;
3987 else
3988 rounded_size = fold (build (MULT_EXPR, sizetype,
3989 fold (build (TRUNC_DIV_EXPR, sizetype,
3990 fold (build (PLUS_EXPR, sizetype,
3991 type_size, alignm1)),
3992 align)),
3993 align));
3995 /* Get AP. */
3996 addr_tree = valist;
3997 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
3999 /* Small args are padded downward. */
4000 addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
4001 fold (build (COND_EXPR, sizetype,
4002 fold (build (GT_EXPR, sizetype,
4003 rounded_size,
4004 align)),
4005 size_zero_node,
4006 fold (build (MINUS_EXPR, sizetype,
4007 rounded_size,
4008 type_size))))));
4011 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
4012 addr = copy_to_reg (addr);
4014 /* Compute new value for AP. */
4015 if (! integer_zerop (rounded_size))
4017 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4018 build (PLUS_EXPR, TREE_TYPE (valist), valist,
4019 rounded_size));
4020 TREE_SIDE_EFFECTS (t) = 1;
4021 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4024 return addr;
4027 /* Expand __builtin_va_arg, which is not really a builtin function, but
4028 a very special sort of operator. */
4031 expand_builtin_va_arg (tree valist, tree type)
4033 rtx addr, result;
4034 tree promoted_type, want_va_type, have_va_type;
4036 /* Verify that valist is of the proper type. */
4038 want_va_type = va_list_type_node;
4039 have_va_type = TREE_TYPE (valist);
4040 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4042 /* If va_list is an array type, the argument may have decayed
4043 to a pointer type, e.g. by being passed to another function.
4044 In that case, unwrap both types so that we can compare the
4045 underlying records. */
4046 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4047 || TREE_CODE (have_va_type) == POINTER_TYPE)
4049 want_va_type = TREE_TYPE (want_va_type);
4050 have_va_type = TREE_TYPE (have_va_type);
4053 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4055 error ("first argument to `va_arg' not of type `va_list'");
4056 addr = const0_rtx;
4059 /* Generate a diagnostic for requesting data of a type that cannot
4060 be passed through `...' due to type promotion at the call site. */
4061 else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
4062 != type)
4064 const char *name = "<anonymous type>", *pname = 0;
4065 static bool gave_help;
4067 if (TYPE_NAME (type))
4069 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4070 name = IDENTIFIER_POINTER (TYPE_NAME (type));
4071 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4072 && DECL_NAME (TYPE_NAME (type)))
4073 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
4075 if (TYPE_NAME (promoted_type))
4077 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
4078 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
4079 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
4080 && DECL_NAME (TYPE_NAME (promoted_type)))
4081 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
4084 /* Unfortunately, this is merely undefined, rather than a constraint
4085 violation, so we cannot make this an error. If this call is never
4086 executed, the program is still strictly conforming. */
4087 warning ("`%s' is promoted to `%s' when passed through `...'",
4088 name, pname);
4089 if (! gave_help)
4091 gave_help = true;
4092 warning ("(so you should pass `%s' not `%s' to `va_arg')",
4093 pname, name);
4096 /* We can, however, treat "undefined" any way we please.
4097 Call abort to encourage the user to fix the program. */
4098 expand_builtin_trap ();
4100 /* This is dead code, but go ahead and finish so that the
4101 mode of the result comes out right. */
4102 addr = const0_rtx;
4104 else
4106 /* Make it easier for the backends by protecting the valist argument
4107 from multiple evaluations. */
4108 valist = stabilize_va_list (valist, 0);
4110 #ifdef EXPAND_BUILTIN_VA_ARG
4111 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
4112 #else
4113 addr = std_expand_builtin_va_arg (valist, type);
4114 #endif
4117 addr = convert_memory_address (Pmode, addr);
4119 result = gen_rtx_MEM (TYPE_MODE (type), addr);
4120 set_mem_alias_set (result, get_varargs_alias_set ());
4122 return result;
4125 /* Expand ARGLIST, from a call to __builtin_va_end. */
4127 static rtx
4128 expand_builtin_va_end (tree arglist)
4130 tree valist = TREE_VALUE (arglist);
4132 #ifdef EXPAND_BUILTIN_VA_END
4133 valist = stabilize_va_list (valist, 0);
4134 EXPAND_BUILTIN_VA_END (arglist);
4135 #else
4136 /* Evaluate for side effects, if needed. I hate macros that don't
4137 do that. */
4138 if (TREE_SIDE_EFFECTS (valist))
4139 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4140 #endif
4142 return const0_rtx;
4145 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4146 builtin rather than just as an assignment in stdarg.h because of the
4147 nastiness of array-type va_list types. */
4149 static rtx
4150 expand_builtin_va_copy (tree arglist)
4152 tree dst, src, t;
4154 dst = TREE_VALUE (arglist);
4155 src = TREE_VALUE (TREE_CHAIN (arglist));
4157 dst = stabilize_va_list (dst, 1);
4158 src = stabilize_va_list (src, 0);
4160 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4162 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
4163 TREE_SIDE_EFFECTS (t) = 1;
4164 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4166 else
4168 rtx dstb, srcb, size;
4170 /* Evaluate to pointers. */
4171 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4172 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4173 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4174 VOIDmode, EXPAND_NORMAL);
4176 dstb = convert_memory_address (Pmode, dstb);
4177 srcb = convert_memory_address (Pmode, srcb);
4179 /* "Dereference" to BLKmode memories. */
4180 dstb = gen_rtx_MEM (BLKmode, dstb);
4181 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4182 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4183 srcb = gen_rtx_MEM (BLKmode, srcb);
4184 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4185 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4187 /* Copy. */
4188 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4191 return const0_rtx;
4194 /* Expand a call to one of the builtin functions __builtin_frame_address or
4195 __builtin_return_address. */
4197 static rtx
4198 expand_builtin_frame_address (tree fndecl, tree arglist)
4200 /* The argument must be a nonnegative integer constant.
4201 It counts the number of frames to scan up the stack.
4202 The value is the return address saved in that frame. */
4203 if (arglist == 0)
4204 /* Warning about missing arg was already issued. */
4205 return const0_rtx;
4206 else if (! host_integerp (TREE_VALUE (arglist), 1))
4208 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4209 error ("invalid arg to `__builtin_frame_address'");
4210 else
4211 error ("invalid arg to `__builtin_return_address'");
4212 return const0_rtx;
4214 else
4216 rtx tem
4217 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4218 tree_low_cst (TREE_VALUE (arglist), 1),
4219 hard_frame_pointer_rtx);
4221 /* Some ports cannot access arbitrary stack frames. */
4222 if (tem == NULL)
4224 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4225 warning ("unsupported arg to `__builtin_frame_address'");
4226 else
4227 warning ("unsupported arg to `__builtin_return_address'");
4228 return const0_rtx;
4231 /* For __builtin_frame_address, return what we've got. */
4232 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4233 return tem;
4235 if (GET_CODE (tem) != REG
4236 && ! CONSTANT_P (tem))
4237 tem = copy_to_mode_reg (Pmode, tem);
4238 return tem;
4242 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4243 we failed and the caller should emit a normal call, otherwise try to get
4244 the result in TARGET, if convenient. */
4246 static rtx
4247 expand_builtin_alloca (tree arglist, rtx target)
4249 rtx op0;
4250 rtx result;
4252 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4253 return 0;
4255 /* Compute the argument. */
4256 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4258 /* Allocate the desired space. */
4259 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4260 result = convert_memory_address (ptr_mode, result);
4262 return result;
4265 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4266 Return 0 if a normal call should be emitted rather than expanding the
4267 function in-line. If convenient, the result should be placed in TARGET.
4268 SUBTARGET may be used as the target for computing one of EXP's operands. */
4270 static rtx
4271 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4272 rtx subtarget, optab op_optab)
4274 rtx op0;
4275 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4276 return 0;
4278 /* Compute the argument. */
4279 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4280 /* Compute op, into TARGET if possible.
4281 Set TARGET to wherever the result comes back. */
4282 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4283 op_optab, op0, target, 1);
4284 if (target == 0)
4285 abort ();
4287 return convert_to_mode (target_mode, target, 0);
4290 /* If the string passed to fputs is a constant and is one character
4291 long, we attempt to transform this call into __builtin_fputc(). */
4293 static rtx
4294 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4296 tree len, fn;
4297 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4298 : implicit_built_in_decls[BUILT_IN_FPUTC];
4299 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
4300 : implicit_built_in_decls[BUILT_IN_FWRITE];
4302 /* If the return value is used, or the replacement _DECL isn't
4303 initialized, don't do the transformation. */
4304 if (target != const0_rtx || !fn_fputc || !fn_fwrite)
4305 return 0;
4307 /* Verify the arguments in the original call. */
4308 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4309 return 0;
4311 /* Get the length of the string passed to fputs. If the length
4312 can't be determined, punt. */
4313 if (!(len = c_strlen (TREE_VALUE (arglist), 1))
4314 || TREE_CODE (len) != INTEGER_CST)
4315 return 0;
4317 switch (compare_tree_int (len, 1))
4319 case -1: /* length is 0, delete the call entirely . */
4321 /* Evaluate and ignore the argument in case it has
4322 side-effects. */
4323 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
4324 VOIDmode, EXPAND_NORMAL);
4325 return const0_rtx;
4327 case 0: /* length is 1, call fputc. */
4329 const char *p = c_getstr (TREE_VALUE (arglist));
4331 if (p != NULL)
4333 /* New argument list transforming fputs(string, stream) to
4334 fputc(string[0], stream). */
4335 arglist =
4336 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4337 arglist =
4338 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
4339 fn = fn_fputc;
4340 break;
4343 /* FALLTHROUGH */
4344 case 1: /* length is greater than 1, call fwrite. */
4346 tree string_arg;
4348 /* If optimizing for size keep fputs. */
4349 if (optimize_size)
4350 return 0;
4351 string_arg = TREE_VALUE (arglist);
4352 /* New argument list transforming fputs(string, stream) to
4353 fwrite(string, 1, len, stream). */
4354 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4355 arglist = tree_cons (NULL_TREE, len, arglist);
4356 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
4357 arglist = tree_cons (NULL_TREE, string_arg, arglist);
4358 fn = fn_fwrite;
4359 break;
4361 default:
4362 abort ();
4365 return expand_expr (build_function_call_expr (fn, arglist),
4366 const0_rtx, VOIDmode, EXPAND_NORMAL);
4369 /* Expand a call to __builtin_expect. We return our argument and emit a
4370 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4371 a non-jump context. */
4373 static rtx
4374 expand_builtin_expect (tree arglist, rtx target)
4376 tree exp, c;
4377 rtx note, rtx_c;
4379 if (arglist == NULL_TREE
4380 || TREE_CHAIN (arglist) == NULL_TREE)
4381 return const0_rtx;
4382 exp = TREE_VALUE (arglist);
4383 c = TREE_VALUE (TREE_CHAIN (arglist));
4385 if (TREE_CODE (c) != INTEGER_CST)
4387 error ("second arg to `__builtin_expect' must be a constant");
4388 c = integer_zero_node;
4391 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4393 /* Don't bother with expected value notes for integral constants. */
4394 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4396 /* We do need to force this into a register so that we can be
4397 moderately sure to be able to correctly interpret the branch
4398 condition later. */
4399 target = force_reg (GET_MODE (target), target);
4401 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4403 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4404 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4407 return target;
4410 /* Like expand_builtin_expect, except do this in a jump context. This is
4411 called from do_jump if the conditional is a __builtin_expect. Return either
4412 a list of insns to emit the jump or NULL if we cannot optimize
4413 __builtin_expect. We need to optimize this at jump time so that machines
4414 like the PowerPC don't turn the test into a SCC operation, and then jump
4415 based on the test being 0/1. */
4418 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4420 tree arglist = TREE_OPERAND (exp, 1);
4421 tree arg0 = TREE_VALUE (arglist);
4422 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4423 rtx ret = NULL_RTX;
4425 /* Only handle __builtin_expect (test, 0) and
4426 __builtin_expect (test, 1). */
4427 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4428 && (integer_zerop (arg1) || integer_onep (arg1)))
4430 int num_jumps = 0;
4431 int save_pending_stack_adjust = pending_stack_adjust;
4432 rtx insn;
4434 /* If we fail to locate an appropriate conditional jump, we'll
4435 fall back to normal evaluation. Ensure that the expression
4436 can be re-evaluated. */
4437 switch (unsafe_for_reeval (arg0))
4439 case 0: /* Safe. */
4440 break;
4442 case 1: /* Mildly unsafe. */
4443 arg0 = unsave_expr (arg0);
4444 break;
4446 case 2: /* Wildly unsafe. */
4447 return NULL_RTX;
4450 /* Expand the jump insns. */
4451 start_sequence ();
4452 do_jump (arg0, if_false_label, if_true_label);
4453 ret = get_insns ();
4454 end_sequence ();
4456 /* Now that the __builtin_expect has been validated, go through and add
4457 the expect's to each of the conditional jumps. If we run into an
4458 error, just give up and generate the 'safe' code of doing a SCC
4459 operation and then doing a branch on that. */
4460 insn = ret;
4461 while (insn != NULL_RTX)
4463 rtx next = NEXT_INSN (insn);
4465 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn))
4467 rtx ifelse = SET_SRC (pc_set (insn));
4468 rtx label;
4469 int taken;
4471 if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF)
4473 taken = 1;
4474 label = XEXP (XEXP (ifelse, 1), 0);
4476 /* An inverted jump reverses the probabilities. */
4477 else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF)
4479 taken = 0;
4480 label = XEXP (XEXP (ifelse, 2), 0);
4482 /* We shouldn't have to worry about conditional returns during
4483 the expansion stage, but handle it gracefully anyway. */
4484 else if (GET_CODE (XEXP (ifelse, 1)) == RETURN)
4486 taken = 1;
4487 label = NULL_RTX;
4489 /* An inverted return reverses the probabilities. */
4490 else if (GET_CODE (XEXP (ifelse, 2)) == RETURN)
4492 taken = 0;
4493 label = NULL_RTX;
4495 else
4496 goto do_next_insn;
4498 /* If the test is expected to fail, reverse the
4499 probabilities. */
4500 if (integer_zerop (arg1))
4501 taken = 1 - taken;
4503 /* If we are jumping to the false label, reverse the
4504 probabilities. */
4505 if (label == NULL_RTX)
4506 ; /* conditional return */
4507 else if (label == if_false_label)
4508 taken = 1 - taken;
4509 else if (label != if_true_label)
4510 goto do_next_insn;
4512 num_jumps++;
4513 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4516 do_next_insn:
4517 insn = next;
4520 /* If no jumps were modified, fail and do __builtin_expect the normal
4521 way. */
4522 if (num_jumps == 0)
4524 ret = NULL_RTX;
4525 pending_stack_adjust = save_pending_stack_adjust;
4529 return ret;
4532 void
4533 expand_builtin_trap (void)
4535 #ifdef HAVE_trap
4536 if (HAVE_trap)
4537 emit_insn (gen_trap ());
4538 else
4539 #endif
4540 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4541 emit_barrier ();
4544 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4545 Return 0 if a normal call should be emitted rather than expanding
4546 the function inline. If convenient, the result should be placed
4547 in TARGET. SUBTARGET may be used as the target for computing
4548 the operand. */
4550 static rtx
4551 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4553 enum machine_mode mode;
4554 tree arg;
4555 rtx op0;
4557 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4558 return 0;
4560 arg = TREE_VALUE (arglist);
4561 mode = TYPE_MODE (TREE_TYPE (arg));
4562 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4563 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4566 /* Expand a call to cabs, cabsf or cabsl with arguments ARGLIST.
4567 Return 0 if a normal call should be emitted rather than expanding
4568 the function inline. If convenient, the result should be placed
4569 in target. */
4571 static rtx
4572 expand_builtin_cabs (tree arglist, rtx target)
4574 enum machine_mode mode;
4575 tree arg;
4576 rtx op0;
4578 if (arglist == 0 || TREE_CHAIN (arglist))
4579 return 0;
4580 arg = TREE_VALUE (arglist);
4581 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
4582 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
4583 return 0;
4585 mode = TYPE_MODE (TREE_TYPE (arg));
4586 op0 = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4587 return expand_complex_abs (mode, op0, target, 0);
4590 /* Create a new constant string literal and return a char* pointer to it.
4591 The STRING_CST value is the LEN characters at STR. */
4592 static tree
4593 build_string_literal (int len, const char *str)
4595 tree t, elem, index, type;
4597 t = build_string (len, str);
4598 elem = build_type_variant (char_type_node, 1, 0);
4599 index = build_index_type (build_int_2 (len - 1, 0));
4600 type = build_array_type (elem, index);
4601 TREE_TYPE (t) = type;
4602 TREE_CONSTANT (t) = 1;
4603 TREE_READONLY (t) = 1;
4604 TREE_STATIC (t) = 1;
4606 type = build_pointer_type (type);
4607 t = build1 (ADDR_EXPR, type, t);
4609 type = build_pointer_type (elem);
4610 t = build1 (NOP_EXPR, type, t);
4611 return t;
4614 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4615 Return 0 if a normal call should be emitted rather than transforming
4616 the function inline. If convenient, the result should be placed in
4617 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4618 call. */
4619 static rtx
4620 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4621 bool unlocked)
4623 tree fn_putchar = unlocked
4624 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4625 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4626 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4627 : implicit_built_in_decls[BUILT_IN_PUTS];
4628 const char *fmt_str;
4629 tree fn, fmt, arg;
4631 /* If the return value is used, don't do the transformation. */
4632 if (target != const0_rtx)
4633 return 0;
4635 /* Verify the required arguments in the original call. */
4636 if (! arglist)
4637 return 0;
4638 fmt = TREE_VALUE (arglist);
4639 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4640 return 0;
4641 arglist = TREE_CHAIN (arglist);
4643 /* Check whether the format is a literal string constant. */
4644 fmt_str = c_getstr (fmt);
4645 if (fmt_str == NULL)
4646 return 0;
4648 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4649 if (strcmp (fmt_str, "%s\n") == 0)
4651 if (! arglist
4652 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4653 || TREE_CHAIN (arglist))
4654 return 0;
4655 fn = fn_puts;
4657 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4658 else if (strcmp (fmt_str, "%c") == 0)
4660 if (! arglist
4661 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4662 || TREE_CHAIN (arglist))
4663 return 0;
4664 fn = fn_putchar;
4666 else
4668 /* We can't handle anything else with % args or %% ... yet. */
4669 if (strchr (fmt_str, '%'))
4670 return 0;
4672 if (arglist)
4673 return 0;
4675 /* If the format specifier was "", printf does nothing. */
4676 if (fmt_str[0] == '\0')
4677 return const0_rtx;
4678 /* If the format specifier has length of 1, call putchar. */
4679 if (fmt_str[1] == '\0')
4681 /* Given printf("c"), (where c is any one character,)
4682 convert "c"[0] to an int and pass that to the replacement
4683 function. */
4684 arg = build_int_2 (fmt_str[0], 0);
4685 arglist = build_tree_list (NULL_TREE, arg);
4686 fn = fn_putchar;
4688 else
4690 /* If the format specifier was "string\n", call puts("string"). */
4691 size_t len = strlen (fmt_str);
4692 if (fmt_str[len - 1] == '\n')
4694 /* Create a NUL-terminated string that's one char shorter
4695 than the original, stripping off the trailing '\n'. */
4696 char *newstr = (char *) alloca (len);
4697 memcpy (newstr, fmt_str, len - 1);
4698 newstr[len - 1] = 0;
4700 arg = build_string_literal (len, newstr);
4701 arglist = build_tree_list (NULL_TREE, arg);
4702 fn = fn_puts;
4704 else
4705 /* We'd like to arrange to call fputs(string,stdout) here,
4706 but we need stdout and don't have a way to get it yet. */
4707 return 0;
4711 if (!fn)
4712 return 0;
4713 return expand_expr (build_function_call_expr (fn, arglist),
4714 target, mode, EXPAND_NORMAL);
4717 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4718 Return 0 if a normal call should be emitted rather than transforming
4719 the function inline. If convenient, the result should be placed in
4720 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4721 call. */
4722 static rtx
4723 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4724 bool unlocked)
4726 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4727 : implicit_built_in_decls[BUILT_IN_FPUTC];
4728 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4729 : implicit_built_in_decls[BUILT_IN_FPUTS];
4730 const char *fmt_str;
4731 tree fn, fmt, fp, arg;
4733 /* If the return value is used, don't do the transformation. */
4734 if (target != const0_rtx)
4735 return 0;
4737 /* Verify the required arguments in the original call. */
4738 if (! arglist)
4739 return 0;
4740 fp = TREE_VALUE (arglist);
4741 if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE)
4742 return 0;
4743 arglist = TREE_CHAIN (arglist);
4744 if (! arglist)
4745 return 0;
4746 fmt = TREE_VALUE (arglist);
4747 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4748 return 0;
4749 arglist = TREE_CHAIN (arglist);
4751 /* Check whether the format is a literal string constant. */
4752 fmt_str = c_getstr (fmt);
4753 if (fmt_str == NULL)
4754 return 0;
4756 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4757 if (strcmp (fmt_str, "%s") == 0)
4759 if (! arglist
4760 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4761 || TREE_CHAIN (arglist))
4762 return 0;
4763 arg = TREE_VALUE (arglist);
4764 arglist = build_tree_list (NULL_TREE, fp);
4765 arglist = tree_cons (NULL_TREE, arg, arglist);
4766 fn = fn_fputs;
4768 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4769 else if (strcmp (fmt_str, "%c") == 0)
4771 if (! arglist
4772 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4773 || TREE_CHAIN (arglist))
4774 return 0;
4775 arg = TREE_VALUE (arglist);
4776 arglist = build_tree_list (NULL_TREE, fp);
4777 arglist = tree_cons (NULL_TREE, arg, arglist);
4778 fn = fn_fputc;
4780 else
4782 /* We can't handle anything else with % args or %% ... yet. */
4783 if (strchr (fmt_str, '%'))
4784 return 0;
4786 if (arglist)
4787 return 0;
4789 /* If the format specifier was "", fprintf does nothing. */
4790 if (fmt_str[0] == '\0')
4792 /* Evaluate and ignore FILE* argument for side-effects. */
4793 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4794 return const0_rtx;
4797 /* When "string" doesn't contain %, replace all cases of
4798 fprintf(stream,string) with fputs(string,stream). The fputs
4799 builtin will take care of special cases like length == 1. */
4800 arglist = build_tree_list (NULL_TREE, fp);
4801 arglist = tree_cons (NULL_TREE, fmt, arglist);
4802 fn = fn_fputs;
4805 if (!fn)
4806 return 0;
4807 return expand_expr (build_function_call_expr (fn, arglist),
4808 target, mode, EXPAND_NORMAL);
4811 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4812 a normal call should be emitted rather than expanding the function
4813 inline. If convenient, the result should be placed in TARGET with
4814 mode MODE. */
4816 static rtx
4817 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4819 tree orig_arglist, dest, fmt;
4820 const char *fmt_str;
4822 orig_arglist = arglist;
4824 /* Verify the required arguments in the original call. */
4825 if (! arglist)
4826 return 0;
4827 dest = TREE_VALUE (arglist);
4828 if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE)
4829 return 0;
4830 arglist = TREE_CHAIN (arglist);
4831 if (! arglist)
4832 return 0;
4833 fmt = TREE_VALUE (arglist);
4834 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4835 return 0;
4836 arglist = TREE_CHAIN (arglist);
4838 /* Check whether the format is a literal string constant. */
4839 fmt_str = c_getstr (fmt);
4840 if (fmt_str == NULL)
4841 return 0;
4843 /* If the format doesn't contain % args or %%, use strcpy. */
4844 if (strchr (fmt_str, '%') == 0)
4846 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4847 tree exp;
4849 if (arglist || ! fn)
4850 return 0;
4851 expand_expr (build_function_call_expr (fn, orig_arglist),
4852 const0_rtx, VOIDmode, EXPAND_NORMAL);
4853 if (target == const0_rtx)
4854 return const0_rtx;
4855 exp = build_int_2 (strlen (fmt_str), 0);
4856 exp = fold (build1 (NOP_EXPR, integer_type_node, exp));
4857 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4859 /* If the format is "%s", use strcpy if the result isn't used. */
4860 else if (strcmp (fmt_str, "%s") == 0)
4862 tree fn, arg, len;
4863 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4865 if (! fn)
4866 return 0;
4868 if (! arglist || TREE_CHAIN (arglist))
4869 return 0;
4870 arg = TREE_VALUE (arglist);
4871 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
4872 return 0;
4874 if (target != const0_rtx)
4876 len = c_strlen (arg, 1);
4877 if (! len || TREE_CODE (len) != INTEGER_CST)
4878 return 0;
4880 else
4881 len = NULL_TREE;
4883 arglist = build_tree_list (NULL_TREE, arg);
4884 arglist = tree_cons (NULL_TREE, dest, arglist);
4885 expand_expr (build_function_call_expr (fn, arglist),
4886 const0_rtx, VOIDmode, EXPAND_NORMAL);
4888 if (target == const0_rtx)
4889 return const0_rtx;
4890 return expand_expr (len, target, mode, EXPAND_NORMAL);
4893 return 0;
4896 /* Expand an expression EXP that calls a built-in function,
4897 with result going to TARGET if that's convenient
4898 (and in mode MODE if that's convenient).
4899 SUBTARGET may be used as the target for computing one of EXP's operands.
4900 IGNORE is nonzero if the value is to be ignored. */
4903 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
4904 int ignore)
4906 tree fndecl = get_callee_fndecl (exp);
4907 tree arglist = TREE_OPERAND (exp, 1);
4908 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4909 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
4911 /* Perform postincrements before expanding builtin functions. */
4912 emit_queue ();
4914 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4915 return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
4917 /* When not optimizing, generate calls to library functions for a certain
4918 set of builtins. */
4919 if (!optimize
4920 && !CALLED_AS_BUILT_IN (fndecl)
4921 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
4922 && fcode != BUILT_IN_ALLOCA)
4923 return expand_call (exp, target, ignore);
4925 /* The built-in function expanders test for target == const0_rtx
4926 to determine whether the function's result will be ignored. */
4927 if (ignore)
4928 target = const0_rtx;
4930 /* If the result of a pure or const built-in function is ignored, and
4931 none of its arguments are volatile, we can avoid expanding the
4932 built-in call and just evaluate the arguments for side-effects. */
4933 if (target == const0_rtx
4934 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
4936 bool volatilep = false;
4937 tree arg;
4939 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
4940 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
4942 volatilep = true;
4943 break;
4946 if (! volatilep)
4948 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
4949 expand_expr (TREE_VALUE (arg), const0_rtx,
4950 VOIDmode, EXPAND_NORMAL);
4951 return const0_rtx;
4955 switch (fcode)
4957 case BUILT_IN_ABS:
4958 case BUILT_IN_LABS:
4959 case BUILT_IN_LLABS:
4960 case BUILT_IN_IMAXABS:
4961 /* build_function_call changes these into ABS_EXPR. */
4962 abort ();
4964 case BUILT_IN_FABS:
4965 case BUILT_IN_FABSF:
4966 case BUILT_IN_FABSL:
4967 target = expand_builtin_fabs (arglist, target, subtarget);
4968 if (target)
4969 return target;
4970 break;
4972 case BUILT_IN_CABS:
4973 case BUILT_IN_CABSF:
4974 case BUILT_IN_CABSL:
4975 if (flag_unsafe_math_optimizations)
4977 target = expand_builtin_cabs (arglist, target);
4978 if (target)
4979 return target;
4981 break;
4983 case BUILT_IN_CONJ:
4984 case BUILT_IN_CONJF:
4985 case BUILT_IN_CONJL:
4986 case BUILT_IN_CREAL:
4987 case BUILT_IN_CREALF:
4988 case BUILT_IN_CREALL:
4989 case BUILT_IN_CIMAG:
4990 case BUILT_IN_CIMAGF:
4991 case BUILT_IN_CIMAGL:
4992 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
4993 and IMAGPART_EXPR. */
4994 abort ();
4996 case BUILT_IN_SIN:
4997 case BUILT_IN_SINF:
4998 case BUILT_IN_SINL:
4999 case BUILT_IN_COS:
5000 case BUILT_IN_COSF:
5001 case BUILT_IN_COSL:
5002 case BUILT_IN_EXP:
5003 case BUILT_IN_EXPF:
5004 case BUILT_IN_EXPL:
5005 case BUILT_IN_LOG:
5006 case BUILT_IN_LOGF:
5007 case BUILT_IN_LOGL:
5008 case BUILT_IN_TAN:
5009 case BUILT_IN_TANF:
5010 case BUILT_IN_TANL:
5011 case BUILT_IN_ATAN:
5012 case BUILT_IN_ATANF:
5013 case BUILT_IN_ATANL:
5014 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5015 because of possible accuracy problems. */
5016 if (! flag_unsafe_math_optimizations)
5017 break;
5018 case BUILT_IN_SQRT:
5019 case BUILT_IN_SQRTF:
5020 case BUILT_IN_SQRTL:
5021 case BUILT_IN_FLOOR:
5022 case BUILT_IN_FLOORF:
5023 case BUILT_IN_FLOORL:
5024 case BUILT_IN_CEIL:
5025 case BUILT_IN_CEILF:
5026 case BUILT_IN_CEILL:
5027 case BUILT_IN_TRUNC:
5028 case BUILT_IN_TRUNCF:
5029 case BUILT_IN_TRUNCL:
5030 case BUILT_IN_ROUND:
5031 case BUILT_IN_ROUNDF:
5032 case BUILT_IN_ROUNDL:
5033 case BUILT_IN_NEARBYINT:
5034 case BUILT_IN_NEARBYINTF:
5035 case BUILT_IN_NEARBYINTL:
5036 target = expand_builtin_mathfn (exp, target, subtarget);
5037 if (target)
5038 return target;
5039 break;
5041 case BUILT_IN_POW:
5042 case BUILT_IN_POWF:
5043 case BUILT_IN_POWL:
5044 if (! flag_unsafe_math_optimizations)
5045 break;
5046 target = expand_builtin_pow (exp, target, subtarget);
5047 if (target)
5048 return target;
5049 break;
5051 case BUILT_IN_ATAN2:
5052 case BUILT_IN_ATAN2F:
5053 case BUILT_IN_ATAN2L:
5054 if (! flag_unsafe_math_optimizations)
5055 break;
5056 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5057 if (target)
5058 return target;
5059 break;
5061 case BUILT_IN_APPLY_ARGS:
5062 return expand_builtin_apply_args ();
5064 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5065 FUNCTION with a copy of the parameters described by
5066 ARGUMENTS, and ARGSIZE. It returns a block of memory
5067 allocated on the stack into which is stored all the registers
5068 that might possibly be used for returning the result of a
5069 function. ARGUMENTS is the value returned by
5070 __builtin_apply_args. ARGSIZE is the number of bytes of
5071 arguments that must be copied. ??? How should this value be
5072 computed? We'll also need a safe worst case value for varargs
5073 functions. */
5074 case BUILT_IN_APPLY:
5075 if (!validate_arglist (arglist, POINTER_TYPE,
5076 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5077 && !validate_arglist (arglist, REFERENCE_TYPE,
5078 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5079 return const0_rtx;
5080 else
5082 int i;
5083 tree t;
5084 rtx ops[3];
5086 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5087 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5089 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5092 /* __builtin_return (RESULT) causes the function to return the
5093 value described by RESULT. RESULT is address of the block of
5094 memory returned by __builtin_apply. */
5095 case BUILT_IN_RETURN:
5096 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5097 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5098 NULL_RTX, VOIDmode, 0));
5099 return const0_rtx;
5101 case BUILT_IN_SAVEREGS:
5102 return expand_builtin_saveregs ();
5104 case BUILT_IN_ARGS_INFO:
5105 return expand_builtin_args_info (arglist);
5107 /* Return the address of the first anonymous stack arg. */
5108 case BUILT_IN_NEXT_ARG:
5109 return expand_builtin_next_arg (arglist);
5111 case BUILT_IN_CLASSIFY_TYPE:
5112 return expand_builtin_classify_type (arglist);
5114 case BUILT_IN_CONSTANT_P:
5115 return expand_builtin_constant_p (arglist, target_mode);
5117 case BUILT_IN_FRAME_ADDRESS:
5118 case BUILT_IN_RETURN_ADDRESS:
5119 return expand_builtin_frame_address (fndecl, arglist);
5121 /* Returns the address of the area where the structure is returned.
5122 0 otherwise. */
5123 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5124 if (arglist != 0
5125 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5126 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
5127 return const0_rtx;
5128 else
5129 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5131 case BUILT_IN_ALLOCA:
5132 target = expand_builtin_alloca (arglist, target);
5133 if (target)
5134 return target;
5135 break;
5137 case BUILT_IN_FFS:
5138 case BUILT_IN_FFSL:
5139 case BUILT_IN_FFSLL:
5140 target = expand_builtin_unop (target_mode, arglist, target,
5141 subtarget, ffs_optab);
5142 if (target)
5143 return target;
5144 break;
5146 case BUILT_IN_CLZ:
5147 case BUILT_IN_CLZL:
5148 case BUILT_IN_CLZLL:
5149 target = expand_builtin_unop (target_mode, arglist, target,
5150 subtarget, clz_optab);
5151 if (target)
5152 return target;
5153 break;
5155 case BUILT_IN_CTZ:
5156 case BUILT_IN_CTZL:
5157 case BUILT_IN_CTZLL:
5158 target = expand_builtin_unop (target_mode, arglist, target,
5159 subtarget, ctz_optab);
5160 if (target)
5161 return target;
5162 break;
5164 case BUILT_IN_POPCOUNT:
5165 case BUILT_IN_POPCOUNTL:
5166 case BUILT_IN_POPCOUNTLL:
5167 target = expand_builtin_unop (target_mode, arglist, target,
5168 subtarget, popcount_optab);
5169 if (target)
5170 return target;
5171 break;
5173 case BUILT_IN_PARITY:
5174 case BUILT_IN_PARITYL:
5175 case BUILT_IN_PARITYLL:
5176 target = expand_builtin_unop (target_mode, arglist, target,
5177 subtarget, parity_optab);
5178 if (target)
5179 return target;
5180 break;
5182 case BUILT_IN_STRLEN:
5183 target = expand_builtin_strlen (arglist, target, target_mode);
5184 if (target)
5185 return target;
5186 break;
5188 case BUILT_IN_STRCPY:
5189 target = expand_builtin_strcpy (arglist, target, mode);
5190 if (target)
5191 return target;
5192 break;
5194 case BUILT_IN_STRNCPY:
5195 target = expand_builtin_strncpy (arglist, target, mode);
5196 if (target)
5197 return target;
5198 break;
5200 case BUILT_IN_STPCPY:
5201 target = expand_builtin_stpcpy (arglist, target, mode);
5202 if (target)
5203 return target;
5204 break;
5206 case BUILT_IN_STRCAT:
5207 target = expand_builtin_strcat (arglist, target, mode);
5208 if (target)
5209 return target;
5210 break;
5212 case BUILT_IN_STRNCAT:
5213 target = expand_builtin_strncat (arglist, target, mode);
5214 if (target)
5215 return target;
5216 break;
5218 case BUILT_IN_STRSPN:
5219 target = expand_builtin_strspn (arglist, target, mode);
5220 if (target)
5221 return target;
5222 break;
5224 case BUILT_IN_STRCSPN:
5225 target = expand_builtin_strcspn (arglist, target, mode);
5226 if (target)
5227 return target;
5228 break;
5230 case BUILT_IN_STRSTR:
5231 target = expand_builtin_strstr (arglist, target, mode);
5232 if (target)
5233 return target;
5234 break;
5236 case BUILT_IN_STRPBRK:
5237 target = expand_builtin_strpbrk (arglist, target, mode);
5238 if (target)
5239 return target;
5240 break;
5242 case BUILT_IN_INDEX:
5243 case BUILT_IN_STRCHR:
5244 target = expand_builtin_strchr (arglist, target, mode);
5245 if (target)
5246 return target;
5247 break;
5249 case BUILT_IN_RINDEX:
5250 case BUILT_IN_STRRCHR:
5251 target = expand_builtin_strrchr (arglist, target, mode);
5252 if (target)
5253 return target;
5254 break;
5256 case BUILT_IN_MEMCPY:
5257 target = expand_builtin_memcpy (arglist, target, mode);
5258 if (target)
5259 return target;
5260 break;
5262 case BUILT_IN_MEMPCPY:
5263 target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
5264 if (target)
5265 return target;
5266 break;
5268 case BUILT_IN_MEMMOVE:
5269 target = expand_builtin_memmove (arglist, target, mode);
5270 if (target)
5271 return target;
5272 break;
5274 case BUILT_IN_BCOPY:
5275 target = expand_builtin_bcopy (arglist);
5276 if (target)
5277 return target;
5278 break;
5280 case BUILT_IN_MEMSET:
5281 target = expand_builtin_memset (arglist, target, mode);
5282 if (target)
5283 return target;
5284 break;
5286 case BUILT_IN_BZERO:
5287 target = expand_builtin_bzero (arglist);
5288 if (target)
5289 return target;
5290 break;
5292 case BUILT_IN_STRCMP:
5293 target = expand_builtin_strcmp (exp, target, mode);
5294 if (target)
5295 return target;
5296 break;
5298 case BUILT_IN_STRNCMP:
5299 target = expand_builtin_strncmp (exp, target, mode);
5300 if (target)
5301 return target;
5302 break;
5304 case BUILT_IN_BCMP:
5305 case BUILT_IN_MEMCMP:
5306 target = expand_builtin_memcmp (exp, arglist, target, mode);
5307 if (target)
5308 return target;
5309 break;
5311 case BUILT_IN_SETJMP:
5312 target = expand_builtin_setjmp (arglist, target);
5313 if (target)
5314 return target;
5315 break;
5317 /* __builtin_longjmp is passed a pointer to an array of five words.
5318 It's similar to the C library longjmp function but works with
5319 __builtin_setjmp above. */
5320 case BUILT_IN_LONGJMP:
5321 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5322 break;
5323 else
5325 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5326 VOIDmode, 0);
5327 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5328 NULL_RTX, VOIDmode, 0);
5330 if (value != const1_rtx)
5332 error ("__builtin_longjmp second argument must be 1");
5333 return const0_rtx;
5336 expand_builtin_longjmp (buf_addr, value);
5337 return const0_rtx;
5340 case BUILT_IN_TRAP:
5341 expand_builtin_trap ();
5342 return const0_rtx;
5344 case BUILT_IN_PRINTF:
5345 target = expand_builtin_printf (arglist, target, mode, false);
5346 if (target)
5347 return target;
5348 break;
5350 case BUILT_IN_PRINTF_UNLOCKED:
5351 target = expand_builtin_printf (arglist, target, mode, true);
5352 if (target)
5353 return target;
5354 break;
5356 case BUILT_IN_FPUTS:
5357 target = expand_builtin_fputs (arglist, target, false);
5358 if (target)
5359 return target;
5360 break;
5362 case BUILT_IN_FPUTS_UNLOCKED:
5363 target = expand_builtin_fputs (arglist, target, true);
5364 if (target)
5365 return target;
5366 break;
5368 case BUILT_IN_FPRINTF:
5369 target = expand_builtin_fprintf (arglist, target, mode, false);
5370 if (target)
5371 return target;
5372 break;
5374 case BUILT_IN_FPRINTF_UNLOCKED:
5375 target = expand_builtin_fprintf (arglist, target, mode, true);
5376 if (target)
5377 return target;
5378 break;
5380 case BUILT_IN_SPRINTF:
5381 target = expand_builtin_sprintf (arglist, target, mode);
5382 if (target)
5383 return target;
5384 break;
5386 /* Various hooks for the DWARF 2 __throw routine. */
5387 case BUILT_IN_UNWIND_INIT:
5388 expand_builtin_unwind_init ();
5389 return const0_rtx;
5390 case BUILT_IN_DWARF_CFA:
5391 return virtual_cfa_rtx;
5392 #ifdef DWARF2_UNWIND_INFO
5393 case BUILT_IN_DWARF_SP_COLUMN:
5394 return expand_builtin_dwarf_sp_column ();
5395 case BUILT_IN_INIT_DWARF_REG_SIZES:
5396 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5397 return const0_rtx;
5398 #endif
5399 case BUILT_IN_FROB_RETURN_ADDR:
5400 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5401 case BUILT_IN_EXTRACT_RETURN_ADDR:
5402 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5403 case BUILT_IN_EH_RETURN:
5404 expand_builtin_eh_return (TREE_VALUE (arglist),
5405 TREE_VALUE (TREE_CHAIN (arglist)));
5406 return const0_rtx;
5407 #ifdef EH_RETURN_DATA_REGNO
5408 case BUILT_IN_EH_RETURN_DATA_REGNO:
5409 return expand_builtin_eh_return_data_regno (arglist);
5410 #endif
5411 case BUILT_IN_VA_START:
5412 case BUILT_IN_STDARG_START:
5413 return expand_builtin_va_start (arglist);
5414 case BUILT_IN_VA_END:
5415 return expand_builtin_va_end (arglist);
5416 case BUILT_IN_VA_COPY:
5417 return expand_builtin_va_copy (arglist);
5418 case BUILT_IN_EXPECT:
5419 return expand_builtin_expect (arglist, target);
5420 case BUILT_IN_PREFETCH:
5421 expand_builtin_prefetch (arglist);
5422 return const0_rtx;
5425 default: /* just do library call, if unknown builtin */
5426 if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
5427 error ("built-in function `%s' not currently supported",
5428 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5431 /* The switch statement above can drop through to cause the function
5432 to be called normally. */
5433 return expand_call (exp, target, ignore);
5436 /* Determine whether a tree node represents a call to a built-in
5437 function. If the tree T is a call to a built-in function with
5438 the right number of arguments of the appropriate types, return
5439 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5440 Otherwise the return value is END_BUILTINS. */
5442 enum built_in_function
5443 builtin_mathfn_code (tree t)
5445 tree fndecl, arglist, parmlist;
5446 tree argtype, parmtype;
5448 if (TREE_CODE (t) != CALL_EXPR
5449 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5450 return END_BUILTINS;
5452 fndecl = get_callee_fndecl (t);
5453 if (fndecl == NULL_TREE
5454 || TREE_CODE (fndecl) != FUNCTION_DECL
5455 || ! DECL_BUILT_IN (fndecl)
5456 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5457 return END_BUILTINS;
5459 arglist = TREE_OPERAND (t, 1);
5460 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5461 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5463 /* If a function doesn't take a variable number of arguments,
5464 the last element in the list will have type `void'. */
5465 parmtype = TREE_VALUE (parmlist);
5466 if (VOID_TYPE_P (parmtype))
5468 if (arglist)
5469 return END_BUILTINS;
5470 return DECL_FUNCTION_CODE (fndecl);
5473 if (! arglist)
5474 return END_BUILTINS;
5476 argtype = TREE_TYPE (TREE_VALUE (arglist));
5478 if (SCALAR_FLOAT_TYPE_P (parmtype))
5480 if (! SCALAR_FLOAT_TYPE_P (argtype))
5481 return END_BUILTINS;
5483 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5485 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5486 return END_BUILTINS;
5488 else if (POINTER_TYPE_P (parmtype))
5490 if (! POINTER_TYPE_P (argtype))
5491 return END_BUILTINS;
5493 else if (INTEGRAL_TYPE_P (parmtype))
5495 if (! INTEGRAL_TYPE_P (argtype))
5496 return END_BUILTINS;
5498 else
5499 return END_BUILTINS;
5501 arglist = TREE_CHAIN (arglist);
5504 /* Variable-length argument list. */
5505 return DECL_FUNCTION_CODE (fndecl);
5508 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5509 constant. ARGLIST is the argument list of the call. */
5511 static tree
5512 fold_builtin_constant_p (tree arglist)
5514 if (arglist == 0)
5515 return 0;
5517 arglist = TREE_VALUE (arglist);
5519 /* We return 1 for a numeric type that's known to be a constant
5520 value at compile-time or for an aggregate type that's a
5521 literal constant. */
5522 STRIP_NOPS (arglist);
5524 /* If we know this is a constant, emit the constant of one. */
5525 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
5526 || (TREE_CODE (arglist) == CONSTRUCTOR
5527 && TREE_CONSTANT (arglist))
5528 || (TREE_CODE (arglist) == ADDR_EXPR
5529 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5530 return integer_one_node;
5532 /* If this expression has side effects, show we don't know it to be a
5533 constant. Likewise if it's a pointer or aggregate type since in
5534 those case we only want literals, since those are only optimized
5535 when generating RTL, not later.
5536 And finally, if we are compiling an initializer, not code, we
5537 need to return a definite result now; there's not going to be any
5538 more optimization done. */
5539 if (TREE_SIDE_EFFECTS (arglist)
5540 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5541 || POINTER_TYPE_P (TREE_TYPE (arglist))
5542 || cfun == 0)
5543 return integer_zero_node;
5545 return 0;
5548 /* Fold a call to __builtin_classify_type. */
5550 static tree
5551 fold_builtin_classify_type (tree arglist)
5553 if (arglist == 0)
5554 return build_int_2 (no_type_class, 0);
5556 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
5559 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5561 static tree
5562 fold_builtin_inf (tree type, int warn)
5564 REAL_VALUE_TYPE real;
5566 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5567 warning ("target format does not support infinity");
5569 real_inf (&real);
5570 return build_real (type, real);
5573 /* Fold a call to __builtin_nan or __builtin_nans. */
5575 static tree
5576 fold_builtin_nan (tree arglist, tree type, int quiet)
5578 REAL_VALUE_TYPE real;
5579 const char *str;
5581 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5582 return 0;
5583 str = c_getstr (TREE_VALUE (arglist));
5584 if (!str)
5585 return 0;
5587 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5588 return 0;
5590 return build_real (type, real);
5593 /* Return true if the floating point expression T has an integer value.
5594 We also allow +Inf, -Inf and NaN to be considered integer values. */
5596 static bool
5597 integer_valued_real_p (tree t)
5599 switch (TREE_CODE (t))
5601 case FLOAT_EXPR:
5602 return true;
5604 case ABS_EXPR:
5605 case SAVE_EXPR:
5606 case NON_LVALUE_EXPR:
5607 return integer_valued_real_p (TREE_OPERAND (t, 0));
5609 case COMPOUND_EXPR:
5610 case MODIFY_EXPR:
5611 case BIND_EXPR:
5612 return integer_valued_real_p (TREE_OPERAND (t, 1));
5614 case PLUS_EXPR:
5615 case MINUS_EXPR:
5616 case MULT_EXPR:
5617 case MIN_EXPR:
5618 case MAX_EXPR:
5619 return integer_valued_real_p (TREE_OPERAND (t, 0))
5620 && integer_valued_real_p (TREE_OPERAND (t, 1));
5622 case COND_EXPR:
5623 return integer_valued_real_p (TREE_OPERAND (t, 1))
5624 && integer_valued_real_p (TREE_OPERAND (t, 2));
5626 case REAL_CST:
5627 if (! TREE_CONSTANT_OVERFLOW (t))
5629 REAL_VALUE_TYPE c, cint;
5631 c = TREE_REAL_CST (t);
5632 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
5633 return real_identical (&c, &cint);
5636 case NOP_EXPR:
5638 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5639 if (TREE_CODE (type) == INTEGER_TYPE)
5640 return true;
5641 if (TREE_CODE (type) == REAL_TYPE)
5642 return integer_valued_real_p (TREE_OPERAND (t, 0));
5643 break;
5646 case CALL_EXPR:
5647 switch (builtin_mathfn_code (t))
5649 case BUILT_IN_CEIL:
5650 case BUILT_IN_CEILF:
5651 case BUILT_IN_CEILL:
5652 case BUILT_IN_FLOOR:
5653 case BUILT_IN_FLOORF:
5654 case BUILT_IN_FLOORL:
5655 case BUILT_IN_NEARBYINT:
5656 case BUILT_IN_NEARBYINTF:
5657 case BUILT_IN_NEARBYINTL:
5658 case BUILT_IN_ROUND:
5659 case BUILT_IN_ROUNDF:
5660 case BUILT_IN_ROUNDL:
5661 case BUILT_IN_TRUNC:
5662 case BUILT_IN_TRUNCF:
5663 case BUILT_IN_TRUNCL:
5664 return true;
5666 default:
5667 break;
5669 break;
5671 default:
5672 break;
5674 return false;
5677 /* EXP is assumed to be builtin call where truncation can be propagated
5678 across (for instance floor((double)f) == (double)floorf (f).
5679 Do the transformation. */
5681 static tree
5682 fold_trunc_transparent_mathfn (tree exp)
5684 tree fndecl = get_callee_fndecl (exp);
5685 tree arglist = TREE_OPERAND (exp, 1);
5686 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5687 tree arg;
5689 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5690 return 0;
5692 arg = TREE_VALUE (arglist);
5693 /* Integer rounding functions are idempotent. */
5694 if (fcode == builtin_mathfn_code (arg))
5695 return arg;
5697 /* If argument is already integer valued, and we don't need to worry
5698 about setting errno, there's no need to perform rounding. */
5699 if (! flag_errno_math && integer_valued_real_p (arg))
5700 return arg;
5702 if (optimize)
5704 tree arg0 = strip_float_extensions (arg);
5705 tree ftype = TREE_TYPE (exp);
5706 tree newtype = TREE_TYPE (arg0);
5707 tree decl;
5709 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
5710 && (decl = mathfn_built_in (newtype, fcode)))
5712 arglist =
5713 build_tree_list (NULL_TREE, fold (convert (newtype, arg0)));
5714 return convert (ftype,
5715 build_function_call_expr (decl, arglist));
5718 return 0;
5721 /* Fold function call to builtin cabs, cabsf or cabsl. FNDECL is the
5722 function's DECL, ARGLIST is the argument list and TYPE is the return
5723 type. Return NULL_TREE if no simplification can be made. */
5725 static tree
5726 fold_builtin_cabs (tree fndecl, tree arglist, tree type)
5728 tree arg;
5730 if (!arglist || TREE_CHAIN (arglist))
5731 return NULL_TREE;
5733 arg = TREE_VALUE (arglist);
5734 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
5735 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
5736 return NULL_TREE;
5738 /* Evaluate cabs of a constant at compile-time. */
5739 if (flag_unsafe_math_optimizations
5740 && TREE_CODE (arg) == COMPLEX_CST
5741 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
5742 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
5743 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
5744 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
5746 REAL_VALUE_TYPE r, i;
5748 r = TREE_REAL_CST (TREE_REALPART (arg));
5749 i = TREE_REAL_CST (TREE_IMAGPART (arg));
5751 real_arithmetic (&r, MULT_EXPR, &r, &r);
5752 real_arithmetic (&i, MULT_EXPR, &i, &i);
5753 real_arithmetic (&r, PLUS_EXPR, &r, &i);
5754 if (real_sqrt (&r, TYPE_MODE (type), &r)
5755 || ! flag_trapping_math)
5756 return build_real (type, r);
5759 /* If either part is zero, cabs is fabs of the other. */
5760 if (TREE_CODE (arg) == COMPLEX_EXPR
5761 && real_zerop (TREE_OPERAND (arg, 0)))
5762 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
5763 if (TREE_CODE (arg) == COMPLEX_EXPR
5764 && real_zerop (TREE_OPERAND (arg, 1)))
5765 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
5767 if (flag_unsafe_math_optimizations)
5769 enum built_in_function fcode;
5770 tree sqrtfn;
5772 fcode = DECL_FUNCTION_CODE (fndecl);
5773 if (fcode == BUILT_IN_CABS)
5774 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT];
5775 else if (fcode == BUILT_IN_CABSF)
5776 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF];
5777 else if (fcode == BUILT_IN_CABSL)
5778 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL];
5779 else
5780 sqrtfn = NULL_TREE;
5782 if (sqrtfn != NULL_TREE)
5784 tree rpart, ipart, result, arglist;
5786 arg = save_expr (arg);
5788 rpart = fold (build1 (REALPART_EXPR, type, arg));
5789 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
5791 rpart = save_expr (rpart);
5792 ipart = save_expr (ipart);
5794 result = fold (build (PLUS_EXPR, type,
5795 fold (build (MULT_EXPR, type,
5796 rpart, rpart)),
5797 fold (build (MULT_EXPR, type,
5798 ipart, ipart))));
5800 arglist = build_tree_list (NULL_TREE, result);
5801 return build_function_call_expr (sqrtfn, arglist);
5805 return NULL_TREE;
5808 /* Fold function call to builtin trunc, truncf or truncl. Return
5809 NULL_TREE if no simplification can be made. */
5811 static tree
5812 fold_builtin_trunc (tree exp)
5814 tree arglist = TREE_OPERAND (exp, 1);
5815 tree arg;
5817 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5818 return 0;
5820 /* Optimize trunc of constant value. */
5821 arg = TREE_VALUE (arglist);
5822 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5824 REAL_VALUE_TYPE r, x;
5825 tree type = TREE_TYPE (exp);
5827 x = TREE_REAL_CST (arg);
5828 real_trunc (&r, TYPE_MODE (type), &x);
5829 return build_real (type, r);
5832 return fold_trunc_transparent_mathfn (exp);
5835 /* Fold function call to builtin floor, floorf or floorl. Return
5836 NULL_TREE if no simplification can be made. */
5838 static tree
5839 fold_builtin_floor (tree exp)
5841 tree arglist = TREE_OPERAND (exp, 1);
5842 tree arg;
5844 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5845 return 0;
5847 /* Optimize floor of constant value. */
5848 arg = TREE_VALUE (arglist);
5849 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5851 REAL_VALUE_TYPE x;
5853 x = TREE_REAL_CST (arg);
5854 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
5856 tree type = TREE_TYPE (exp);
5857 REAL_VALUE_TYPE r;
5859 real_floor (&r, TYPE_MODE (type), &x);
5860 return build_real (type, r);
5864 return fold_trunc_transparent_mathfn (exp);
5867 /* Fold function call to builtin ceil, ceilf or ceill. Return
5868 NULL_TREE if no simplification can be made. */
5870 static tree
5871 fold_builtin_ceil (tree exp)
5873 tree arglist = TREE_OPERAND (exp, 1);
5874 tree arg;
5876 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5877 return 0;
5879 /* Optimize ceil of constant value. */
5880 arg = TREE_VALUE (arglist);
5881 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5883 REAL_VALUE_TYPE x;
5885 x = TREE_REAL_CST (arg);
5886 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
5888 tree type = TREE_TYPE (exp);
5889 REAL_VALUE_TYPE r;
5891 real_ceil (&r, TYPE_MODE (type), &x);
5892 return build_real (type, r);
5896 return fold_trunc_transparent_mathfn (exp);
5899 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
5900 and their long and long long variants (i.e. ffsl and ffsll).
5901 Return NULL_TREE if no simplification can be made. */
5903 static tree
5904 fold_builtin_bitop (tree exp)
5906 tree fndecl = get_callee_fndecl (exp);
5907 tree arglist = TREE_OPERAND (exp, 1);
5908 tree arg;
5910 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
5911 return NULL_TREE;
5913 /* Optimize for constant argument. */
5914 arg = TREE_VALUE (arglist);
5915 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5917 HOST_WIDE_INT hi, width, result;
5918 unsigned HOST_WIDE_INT lo;
5919 tree type, t;
5921 type = TREE_TYPE (arg);
5922 width = TYPE_PRECISION (type);
5923 lo = TREE_INT_CST_LOW (arg);
5925 /* Clear all the bits that are beyond the type's precision. */
5926 if (width > HOST_BITS_PER_WIDE_INT)
5928 hi = TREE_INT_CST_HIGH (arg);
5929 if (width < 2 * HOST_BITS_PER_WIDE_INT)
5930 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
5932 else
5934 hi = 0;
5935 if (width < HOST_BITS_PER_WIDE_INT)
5936 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
5939 switch (DECL_FUNCTION_CODE (fndecl))
5941 case BUILT_IN_FFS:
5942 case BUILT_IN_FFSL:
5943 case BUILT_IN_FFSLL:
5944 if (lo != 0)
5945 result = exact_log2 (lo & -lo) + 1;
5946 else if (hi != 0)
5947 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
5948 else
5949 result = 0;
5950 break;
5952 case BUILT_IN_CLZ:
5953 case BUILT_IN_CLZL:
5954 case BUILT_IN_CLZLL:
5955 if (hi != 0)
5956 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
5957 else if (lo != 0)
5958 result = width - floor_log2 (lo) - 1;
5959 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
5960 result = width;
5961 break;
5963 case BUILT_IN_CTZ:
5964 case BUILT_IN_CTZL:
5965 case BUILT_IN_CTZLL:
5966 if (lo != 0)
5967 result = exact_log2 (lo & -lo);
5968 else if (hi != 0)
5969 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
5970 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
5971 result = width;
5972 break;
5974 case BUILT_IN_POPCOUNT:
5975 case BUILT_IN_POPCOUNTL:
5976 case BUILT_IN_POPCOUNTLL:
5977 result = 0;
5978 while (lo)
5979 result++, lo &= lo - 1;
5980 while (hi)
5981 result++, hi &= hi - 1;
5982 break;
5984 case BUILT_IN_PARITY:
5985 case BUILT_IN_PARITYL:
5986 case BUILT_IN_PARITYLL:
5987 result = 0;
5988 while (lo)
5989 result++, lo &= lo - 1;
5990 while (hi)
5991 result++, hi &= hi - 1;
5992 result &= 1;
5993 break;
5995 default:
5996 abort();
5999 t = build_int_2 (result, 0);
6000 TREE_TYPE (t) = TREE_TYPE (exp);
6001 return t;
6004 return NULL_TREE;
6007 /* Return true if EXPR is the real constant contained in VALUE. */
6009 static bool
6010 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6012 STRIP_NOPS (expr);
6014 return ((TREE_CODE (expr) == REAL_CST
6015 && ! TREE_CONSTANT_OVERFLOW (expr)
6016 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6017 || (TREE_CODE (expr) == COMPLEX_CST
6018 && real_dconstp (TREE_REALPART (expr), value)
6019 && real_zerop (TREE_IMAGPART (expr))));
6022 /* A subroutine of fold_builtin to fold the various logarithmic
6023 functions. EXP is the CALL_EXPR of a call to a builtin log*
6024 function. VALUE is the base of the log* function. */
6026 static tree
6027 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6029 tree arglist = TREE_OPERAND (exp, 1);
6031 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6033 tree fndecl = get_callee_fndecl (exp);
6034 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6035 tree arg = TREE_VALUE (arglist);
6036 const enum built_in_function fcode = builtin_mathfn_code (arg);
6038 /* Optimize log*(1.0) = 0.0. */
6039 if (real_onep (arg))
6040 return build_real (type, dconst0);
6042 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6043 exactly, then only do this if flag_unsafe_math_optimizations. */
6044 if (exact_real_truncate (TYPE_MODE (type), value)
6045 || flag_unsafe_math_optimizations)
6047 const REAL_VALUE_TYPE value_truncate =
6048 real_value_truncate (TYPE_MODE (type), *value);
6049 if (real_dconstp (arg, &value_truncate))
6050 return build_real (type, dconst1);
6053 /* Special case, optimize logN(expN(x)) = x. */
6054 if (flag_unsafe_math_optimizations
6055 && ((value == &dconste
6056 && (fcode == BUILT_IN_EXP
6057 || fcode == BUILT_IN_EXPF
6058 || fcode == BUILT_IN_EXPL))
6059 || (value == &dconst2
6060 && (fcode == BUILT_IN_EXP2
6061 || fcode == BUILT_IN_EXP2F
6062 || fcode == BUILT_IN_EXP2L))
6063 || (value == &dconst10
6064 && (fcode == BUILT_IN_EXP10
6065 || fcode == BUILT_IN_EXP10F
6066 || fcode == BUILT_IN_EXP10L))))
6067 return convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6069 /* Optimize log*(func()) for various exponential functions. We
6070 want to determine the value "x" and the power "exponent" in
6071 order to transform logN(x**exponent) into exponent*logN(x). */
6072 if (flag_unsafe_math_optimizations)
6074 tree exponent = 0, x = 0;
6076 switch (fcode)
6078 case BUILT_IN_EXP:
6079 case BUILT_IN_EXPF:
6080 case BUILT_IN_EXPL:
6081 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6082 x = build_real (type,
6083 real_value_truncate (TYPE_MODE (type), dconste));
6084 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6085 break;
6086 case BUILT_IN_EXP2:
6087 case BUILT_IN_EXP2F:
6088 case BUILT_IN_EXP2L:
6089 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6090 x = build_real (type, dconst2);
6091 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6092 break;
6093 case BUILT_IN_EXP10:
6094 case BUILT_IN_EXP10F:
6095 case BUILT_IN_EXP10L:
6096 case BUILT_IN_POW10:
6097 case BUILT_IN_POW10F:
6098 case BUILT_IN_POW10L:
6099 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6100 x = build_real (type, dconst10);
6101 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6102 break;
6103 case BUILT_IN_SQRT:
6104 case BUILT_IN_SQRTF:
6105 case BUILT_IN_SQRTL:
6106 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6107 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6108 exponent = build_real (type, dconsthalf);
6109 break;
6110 case BUILT_IN_CBRT:
6111 case BUILT_IN_CBRTF:
6112 case BUILT_IN_CBRTL:
6113 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6114 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6115 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6116 dconstthird));
6117 break;
6118 case BUILT_IN_POW:
6119 case BUILT_IN_POWF:
6120 case BUILT_IN_POWL:
6121 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6122 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6123 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6124 break;
6125 default:
6126 break;
6129 /* Now perform the optimization. */
6130 if (x && exponent)
6132 tree logfn;
6133 arglist = build_tree_list (NULL_TREE, x);
6134 logfn = build_function_call_expr (fndecl, arglist);
6135 return fold (build (MULT_EXPR, type, exponent, logfn));
6140 return 0;
6143 /* A subroutine of fold_builtin to fold the various exponent
6144 functions. EXP is the CALL_EXPR of a call to a builtin function.
6145 VALUE is the value which will be raised to a power. */
6147 static tree
6148 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
6150 tree arglist = TREE_OPERAND (exp, 1);
6152 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6154 tree fndecl = get_callee_fndecl (exp);
6155 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6156 tree arg = TREE_VALUE (arglist);
6158 /* Optimize exp*(0.0) = 1.0. */
6159 if (real_zerop (arg))
6160 return build_real (type, dconst1);
6162 /* Optimize expN(1.0) = N. */
6163 if (real_onep (arg))
6165 REAL_VALUE_TYPE cst;
6167 real_convert (&cst, TYPE_MODE (type), value);
6168 return build_real (type, cst);
6171 /* Attempt to evaluate expN(integer) at compile-time. */
6172 if (flag_unsafe_math_optimizations
6173 && TREE_CODE (arg) == REAL_CST
6174 && ! TREE_CONSTANT_OVERFLOW (arg))
6176 REAL_VALUE_TYPE cint;
6177 REAL_VALUE_TYPE c;
6178 HOST_WIDE_INT n;
6180 c = TREE_REAL_CST (arg);
6181 n = real_to_integer (&c);
6182 real_from_integer (&cint, VOIDmode, n,
6183 n < 0 ? -1 : 0, 0);
6184 if (real_identical (&c, &cint))
6186 REAL_VALUE_TYPE x;
6188 real_powi (&x, TYPE_MODE (type), value, n);
6189 return build_real (type, x);
6193 /* Optimize expN(logN(x)) = x. */
6194 if (flag_unsafe_math_optimizations)
6196 const enum built_in_function fcode = builtin_mathfn_code (arg);
6198 if ((value == &dconste
6199 && (fcode == BUILT_IN_LOG
6200 || fcode == BUILT_IN_LOGF
6201 || fcode == BUILT_IN_LOGL))
6202 || (value == &dconst2
6203 && (fcode == BUILT_IN_LOG2
6204 || fcode == BUILT_IN_LOG2F
6205 || fcode == BUILT_IN_LOG2L))
6206 || (value == &dconst10
6207 && (fcode == BUILT_IN_LOG10
6208 || fcode == BUILT_IN_LOG10F
6209 || fcode == BUILT_IN_LOG10L)))
6210 return convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6214 return 0;
6217 /* Fold function call to builtin memcpy. Return
6218 NULL_TREE if no simplification can be made. */
6220 static tree
6221 fold_builtin_memcpy (tree exp)
6223 tree arglist = TREE_OPERAND (exp, 1);
6224 tree dest, src, len;
6226 if (!validate_arglist (arglist,
6227 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6228 return 0;
6230 dest = TREE_VALUE (arglist);
6231 src = TREE_VALUE (TREE_CHAIN (arglist));
6232 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6234 /* If the LEN parameter is zero, return DEST. */
6235 if (integer_zerop (len))
6236 return omit_one_operand (TREE_TYPE (exp), dest, src);
6238 /* If SRC and DEST are the same (and not volatile), return DEST. */
6239 if (operand_equal_p (src, dest, 0))
6240 return omit_one_operand (TREE_TYPE (exp), dest, len);
6242 return 0;
6245 /* Fold function call to builtin mempcpy. Return
6246 NULL_TREE if no simplification can be made. */
6248 static tree
6249 fold_builtin_mempcpy (tree exp)
6251 tree arglist = TREE_OPERAND (exp, 1);
6252 tree dest, src, len;
6254 if (!validate_arglist (arglist,
6255 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6256 return 0;
6258 dest = TREE_VALUE (arglist);
6259 src = TREE_VALUE (TREE_CHAIN (arglist));
6260 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6262 /* If the LEN parameter is zero, return DEST. */
6263 if (integer_zerop (len))
6264 return omit_one_operand (TREE_TYPE (exp), dest, src);
6266 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
6267 if (operand_equal_p (src, dest, 0))
6269 tree temp = convert (TREE_TYPE (dest), len);
6270 temp = fold (build (PLUS_EXPR, TREE_TYPE (dest), dest, len));
6271 return convert (TREE_TYPE (exp), temp);
6274 return 0;
6277 /* Fold function call to builtin memmove. Return
6278 NULL_TREE if no simplification can be made. */
6280 static tree
6281 fold_builtin_memmove (tree exp)
6283 tree arglist = TREE_OPERAND (exp, 1);
6284 tree dest, src, len;
6286 if (!validate_arglist (arglist,
6287 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6288 return 0;
6290 dest = TREE_VALUE (arglist);
6291 src = TREE_VALUE (TREE_CHAIN (arglist));
6292 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6294 /* If the LEN parameter is zero, return DEST. */
6295 if (integer_zerop (len))
6296 return omit_one_operand (TREE_TYPE (exp), dest, src);
6298 /* If SRC and DEST are the same (and not volatile), return DEST. */
6299 if (operand_equal_p (src, dest, 0))
6300 return omit_one_operand (TREE_TYPE (exp), dest, len);
6302 return 0;
6305 /* Fold function call to builtin strcpy. Return
6306 NULL_TREE if no simplification can be made. */
6308 static tree
6309 fold_builtin_strcpy (tree exp)
6311 tree arglist = TREE_OPERAND (exp, 1);
6312 tree dest, src;
6314 if (!validate_arglist (arglist,
6315 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6316 return 0;
6318 dest = TREE_VALUE (arglist);
6319 src = TREE_VALUE (TREE_CHAIN (arglist));
6321 /* If SRC and DEST are the same (and not volatile), return DEST. */
6322 if (operand_equal_p (src, dest, 0))
6323 return convert (TREE_TYPE (exp), dest);
6325 return 0;
6328 /* Fold function call to builtin strncpy. Return
6329 NULL_TREE if no simplification can be made. */
6331 static tree
6332 fold_builtin_strncpy (tree exp)
6334 tree arglist = TREE_OPERAND (exp, 1);
6335 tree dest, src, len;
6337 if (!validate_arglist (arglist,
6338 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6339 return 0;
6341 dest = TREE_VALUE (arglist);
6342 src = TREE_VALUE (TREE_CHAIN (arglist));
6343 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6345 /* If the LEN parameter is zero, return DEST. */
6346 if (integer_zerop (len))
6347 return omit_one_operand (TREE_TYPE (exp), dest, src);
6349 return 0;
6352 /* Fold function call to builtin memcmp. Return
6353 NULL_TREE if no simplification can be made. */
6355 static tree
6356 fold_builtin_memcmp (tree exp)
6358 tree arglist = TREE_OPERAND (exp, 1);
6359 tree arg1, arg2, len;
6361 if (!validate_arglist (arglist,
6362 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6363 return 0;
6365 arg1 = TREE_VALUE (arglist);
6366 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6367 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6369 /* If the LEN parameter is zero, return zero. */
6370 if (integer_zerop (len))
6372 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
6373 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
6376 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6377 if (operand_equal_p (arg1, arg2, 0))
6378 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
6380 return 0;
6383 /* Fold function call to builtin strcmp. Return
6384 NULL_TREE if no simplification can be made. */
6386 static tree
6387 fold_builtin_strcmp (tree exp)
6389 tree arglist = TREE_OPERAND (exp, 1);
6390 tree arg1, arg2;
6391 const char *p1, *p2;
6393 if (!validate_arglist (arglist,
6394 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6395 return 0;
6397 arg1 = TREE_VALUE (arglist);
6398 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6400 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6401 if (operand_equal_p (arg1, arg2, 0))
6402 return convert (TREE_TYPE (exp), integer_zero_node);
6404 p1 = c_getstr (arg1);
6405 p2 = c_getstr (arg2);
6407 if (p1 && p2)
6409 tree temp;
6410 const int i = strcmp (p1, p2);
6411 if (i < 0)
6412 temp = integer_minus_one_node;
6413 else if (i > 0)
6414 temp = integer_one_node;
6415 else
6416 temp = integer_zero_node;
6417 return convert (TREE_TYPE (exp), temp);
6420 return 0;
6423 /* Fold function call to builtin strncmp. Return
6424 NULL_TREE if no simplification can be made. */
6426 static tree
6427 fold_builtin_strncmp (tree exp)
6429 tree arglist = TREE_OPERAND (exp, 1);
6430 tree arg1, arg2, len;
6431 const char *p1, *p2;
6433 if (!validate_arglist (arglist,
6434 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6435 return 0;
6437 arg1 = TREE_VALUE (arglist);
6438 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6439 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6441 /* If the LEN parameter is zero, return zero. */
6442 if (integer_zerop (len))
6444 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
6445 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
6448 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6449 if (operand_equal_p (arg1, arg2, 0))
6450 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
6452 p1 = c_getstr (arg1);
6453 p2 = c_getstr (arg2);
6455 if (host_integerp (len, 1) && p1 && p2)
6457 tree temp;
6458 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
6459 if (i < 0)
6460 temp = integer_minus_one_node;
6461 else if (i > 0)
6462 temp = integer_one_node;
6463 else
6464 temp = integer_zero_node;
6465 return convert (TREE_TYPE (exp), temp);
6468 return 0;
6471 /* Used by constant folding to eliminate some builtin calls early. EXP is
6472 the CALL_EXPR of a call to a builtin function. */
6474 tree
6475 fold_builtin (tree exp)
6477 tree fndecl = get_callee_fndecl (exp);
6478 tree arglist = TREE_OPERAND (exp, 1);
6479 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6481 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6482 return 0;
6484 switch (DECL_FUNCTION_CODE (fndecl))
6486 case BUILT_IN_CONSTANT_P:
6487 return fold_builtin_constant_p (arglist);
6489 case BUILT_IN_CLASSIFY_TYPE:
6490 return fold_builtin_classify_type (arglist);
6492 case BUILT_IN_STRLEN:
6493 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6495 tree len = c_strlen (TREE_VALUE (arglist), 0);
6496 if (len)
6498 /* Convert from the internal "sizetype" type to "size_t". */
6499 if (size_type_node)
6500 len = convert (size_type_node, len);
6501 return len;
6504 break;
6506 case BUILT_IN_FABS:
6507 case BUILT_IN_FABSF:
6508 case BUILT_IN_FABSL:
6509 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6510 return fold (build1 (ABS_EXPR, type, TREE_VALUE (arglist)));
6511 break;
6513 case BUILT_IN_CABS:
6514 case BUILT_IN_CABSF:
6515 case BUILT_IN_CABSL:
6516 return fold_builtin_cabs (fndecl, arglist, type);
6518 case BUILT_IN_SQRT:
6519 case BUILT_IN_SQRTF:
6520 case BUILT_IN_SQRTL:
6521 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6523 enum built_in_function fcode;
6524 tree arg = TREE_VALUE (arglist);
6526 /* Optimize sqrt of constant value. */
6527 if (TREE_CODE (arg) == REAL_CST
6528 && ! TREE_CONSTANT_OVERFLOW (arg))
6530 REAL_VALUE_TYPE r, x;
6532 x = TREE_REAL_CST (arg);
6533 if (real_sqrt (&r, TYPE_MODE (type), &x)
6534 || (!flag_trapping_math && !flag_errno_math))
6535 return build_real (type, r);
6538 /* Optimize sqrt(exp(x)) = exp(x*0.5). */
6539 fcode = builtin_mathfn_code (arg);
6540 if (flag_unsafe_math_optimizations
6541 && (fcode == BUILT_IN_EXP
6542 || fcode == BUILT_IN_EXPF
6543 || fcode == BUILT_IN_EXPL))
6545 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6546 arg = fold (build (MULT_EXPR, type,
6547 TREE_VALUE (TREE_OPERAND (arg, 1)),
6548 build_real (type, dconsthalf)));
6549 arglist = build_tree_list (NULL_TREE, arg);
6550 return build_function_call_expr (expfn, arglist);
6553 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
6554 if (flag_unsafe_math_optimizations
6555 && (fcode == BUILT_IN_POW
6556 || fcode == BUILT_IN_POWF
6557 || fcode == BUILT_IN_POWL))
6559 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6560 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6561 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6562 tree narg1 = fold (build (MULT_EXPR, type, arg1,
6563 build_real (type, dconsthalf)));
6564 arglist = tree_cons (NULL_TREE, arg0,
6565 build_tree_list (NULL_TREE, narg1));
6566 return build_function_call_expr (powfn, arglist);
6569 break;
6571 case BUILT_IN_SIN:
6572 case BUILT_IN_SINF:
6573 case BUILT_IN_SINL:
6574 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6576 tree arg = TREE_VALUE (arglist);
6578 /* Optimize sin(0.0) = 0.0. */
6579 if (real_zerop (arg))
6580 return arg;
6582 break;
6584 case BUILT_IN_COS:
6585 case BUILT_IN_COSF:
6586 case BUILT_IN_COSL:
6587 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6589 tree arg = TREE_VALUE (arglist);
6591 /* Optimize cos(0.0) = 1.0. */
6592 if (real_zerop (arg))
6593 return build_real (type, dconst1);
6595 /* Optimize cos(-x) into cos(x). */
6596 if (TREE_CODE (arg) == NEGATE_EXPR)
6598 tree arglist = build_tree_list (NULL_TREE,
6599 TREE_OPERAND (arg, 0));
6600 return build_function_call_expr (fndecl, arglist);
6603 break;
6605 case BUILT_IN_EXP:
6606 case BUILT_IN_EXPF:
6607 case BUILT_IN_EXPL:
6608 return fold_builtin_exponent (exp, &dconste);
6609 case BUILT_IN_EXP2:
6610 case BUILT_IN_EXP2F:
6611 case BUILT_IN_EXP2L:
6612 return fold_builtin_exponent (exp, &dconst2);
6613 case BUILT_IN_EXP10:
6614 case BUILT_IN_EXP10F:
6615 case BUILT_IN_EXP10L:
6616 case BUILT_IN_POW10:
6617 case BUILT_IN_POW10F:
6618 case BUILT_IN_POW10L:
6619 return fold_builtin_exponent (exp, &dconst10);
6620 case BUILT_IN_LOG:
6621 case BUILT_IN_LOGF:
6622 case BUILT_IN_LOGL:
6623 return fold_builtin_logarithm (exp, &dconste);
6624 break;
6625 case BUILT_IN_LOG2:
6626 case BUILT_IN_LOG2F:
6627 case BUILT_IN_LOG2L:
6628 return fold_builtin_logarithm (exp, &dconst2);
6629 break;
6630 case BUILT_IN_LOG10:
6631 case BUILT_IN_LOG10F:
6632 case BUILT_IN_LOG10L:
6633 return fold_builtin_logarithm (exp, &dconst10);
6634 break;
6636 case BUILT_IN_TAN:
6637 case BUILT_IN_TANF:
6638 case BUILT_IN_TANL:
6639 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6641 enum built_in_function fcode;
6642 tree arg = TREE_VALUE (arglist);
6644 /* Optimize tan(0.0) = 0.0. */
6645 if (real_zerop (arg))
6646 return arg;
6648 /* Optimize tan(atan(x)) = x. */
6649 fcode = builtin_mathfn_code (arg);
6650 if (flag_unsafe_math_optimizations
6651 && (fcode == BUILT_IN_ATAN
6652 || fcode == BUILT_IN_ATANF
6653 || fcode == BUILT_IN_ATANL))
6654 return TREE_VALUE (TREE_OPERAND (arg, 1));
6656 break;
6658 case BUILT_IN_ATAN:
6659 case BUILT_IN_ATANF:
6660 case BUILT_IN_ATANL:
6661 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6663 tree arg = TREE_VALUE (arglist);
6665 /* Optimize atan(0.0) = 0.0. */
6666 if (real_zerop (arg))
6667 return arg;
6669 /* Optimize atan(1.0) = pi/4. */
6670 if (real_onep (arg))
6672 REAL_VALUE_TYPE cst;
6674 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6675 cst.exp -= 2;
6676 return build_real (type, cst);
6679 break;
6681 case BUILT_IN_POW:
6682 case BUILT_IN_POWF:
6683 case BUILT_IN_POWL:
6684 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6686 enum built_in_function fcode;
6687 tree arg0 = TREE_VALUE (arglist);
6688 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6690 /* Optimize pow(1.0,y) = 1.0. */
6691 if (real_onep (arg0))
6692 return omit_one_operand (type, build_real (type, dconst1), arg1);
6694 if (TREE_CODE (arg1) == REAL_CST
6695 && ! TREE_CONSTANT_OVERFLOW (arg1))
6697 REAL_VALUE_TYPE c;
6698 c = TREE_REAL_CST (arg1);
6700 /* Optimize pow(x,0.0) = 1.0. */
6701 if (REAL_VALUES_EQUAL (c, dconst0))
6702 return omit_one_operand (type, build_real (type, dconst1),
6703 arg0);
6705 /* Optimize pow(x,1.0) = x. */
6706 if (REAL_VALUES_EQUAL (c, dconst1))
6707 return arg0;
6709 /* Optimize pow(x,-1.0) = 1.0/x. */
6710 if (REAL_VALUES_EQUAL (c, dconstm1))
6711 return fold (build (RDIV_EXPR, type,
6712 build_real (type, dconst1),
6713 arg0));
6715 /* Optimize pow(x,0.5) = sqrt(x). */
6716 if (flag_unsafe_math_optimizations
6717 && REAL_VALUES_EQUAL (c, dconsthalf))
6719 tree sqrtfn;
6721 fcode = DECL_FUNCTION_CODE (fndecl);
6722 if (fcode == BUILT_IN_POW)
6723 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT];
6724 else if (fcode == BUILT_IN_POWF)
6725 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF];
6726 else if (fcode == BUILT_IN_POWL)
6727 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL];
6728 else
6729 sqrtfn = NULL_TREE;
6731 if (sqrtfn != NULL_TREE)
6733 tree arglist = build_tree_list (NULL_TREE, arg0);
6734 return build_function_call_expr (sqrtfn, arglist);
6738 /* Attempt to evaluate pow at compile-time. */
6739 if (TREE_CODE (arg0) == REAL_CST
6740 && ! TREE_CONSTANT_OVERFLOW (arg0))
6742 REAL_VALUE_TYPE cint;
6743 HOST_WIDE_INT n;
6745 n = real_to_integer (&c);
6746 real_from_integer (&cint, VOIDmode, n,
6747 n < 0 ? -1 : 0, 0);
6748 if (real_identical (&c, &cint))
6750 REAL_VALUE_TYPE x;
6751 bool inexact;
6753 x = TREE_REAL_CST (arg0);
6754 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6755 if (flag_unsafe_math_optimizations || !inexact)
6756 return build_real (type, x);
6761 /* Optimize pow(exp(x),y) = exp(x*y). */
6762 fcode = builtin_mathfn_code (arg0);
6763 if (flag_unsafe_math_optimizations
6764 && (fcode == BUILT_IN_EXP
6765 || fcode == BUILT_IN_EXPF
6766 || fcode == BUILT_IN_EXPL))
6768 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6769 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6770 arg = fold (build (MULT_EXPR, type, arg, arg1));
6771 arglist = build_tree_list (NULL_TREE, arg);
6772 return build_function_call_expr (expfn, arglist);
6775 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6776 if (flag_unsafe_math_optimizations
6777 && (fcode == BUILT_IN_SQRT
6778 || fcode == BUILT_IN_SQRTF
6779 || fcode == BUILT_IN_SQRTL))
6781 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6782 tree narg1 = fold (build (MULT_EXPR, type, arg1,
6783 build_real (type, dconsthalf)));
6785 arglist = tree_cons (NULL_TREE, narg0,
6786 build_tree_list (NULL_TREE, narg1));
6787 return build_function_call_expr (fndecl, arglist);
6790 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
6791 if (flag_unsafe_math_optimizations
6792 && (fcode == BUILT_IN_POW
6793 || fcode == BUILT_IN_POWF
6794 || fcode == BUILT_IN_POWL))
6796 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6797 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6798 tree narg1 = fold (build (MULT_EXPR, type, arg01, arg1));
6799 arglist = tree_cons (NULL_TREE, arg00,
6800 build_tree_list (NULL_TREE, narg1));
6801 return build_function_call_expr (fndecl, arglist);
6804 break;
6806 case BUILT_IN_INF:
6807 case BUILT_IN_INFF:
6808 case BUILT_IN_INFL:
6809 return fold_builtin_inf (type, true);
6811 case BUILT_IN_HUGE_VAL:
6812 case BUILT_IN_HUGE_VALF:
6813 case BUILT_IN_HUGE_VALL:
6814 return fold_builtin_inf (type, false);
6816 case BUILT_IN_NAN:
6817 case BUILT_IN_NANF:
6818 case BUILT_IN_NANL:
6819 return fold_builtin_nan (arglist, type, true);
6821 case BUILT_IN_NANS:
6822 case BUILT_IN_NANSF:
6823 case BUILT_IN_NANSL:
6824 return fold_builtin_nan (arglist, type, false);
6826 case BUILT_IN_FLOOR:
6827 case BUILT_IN_FLOORF:
6828 case BUILT_IN_FLOORL:
6829 return fold_builtin_floor (exp);
6831 case BUILT_IN_CEIL:
6832 case BUILT_IN_CEILF:
6833 case BUILT_IN_CEILL:
6834 return fold_builtin_ceil (exp);
6836 case BUILT_IN_TRUNC:
6837 case BUILT_IN_TRUNCF:
6838 case BUILT_IN_TRUNCL:
6839 return fold_builtin_trunc (exp);
6841 case BUILT_IN_ROUND:
6842 case BUILT_IN_ROUNDF:
6843 case BUILT_IN_ROUNDL:
6844 case BUILT_IN_NEARBYINT:
6845 case BUILT_IN_NEARBYINTF:
6846 case BUILT_IN_NEARBYINTL:
6847 return fold_trunc_transparent_mathfn (exp);
6849 case BUILT_IN_FFS:
6850 case BUILT_IN_FFSL:
6851 case BUILT_IN_FFSLL:
6852 case BUILT_IN_CLZ:
6853 case BUILT_IN_CLZL:
6854 case BUILT_IN_CLZLL:
6855 case BUILT_IN_CTZ:
6856 case BUILT_IN_CTZL:
6857 case BUILT_IN_CTZLL:
6858 case BUILT_IN_POPCOUNT:
6859 case BUILT_IN_POPCOUNTL:
6860 case BUILT_IN_POPCOUNTLL:
6861 case BUILT_IN_PARITY:
6862 case BUILT_IN_PARITYL:
6863 case BUILT_IN_PARITYLL:
6864 return fold_builtin_bitop (exp);
6866 case BUILT_IN_MEMCPY:
6867 return fold_builtin_memcpy (exp);
6869 case BUILT_IN_MEMPCPY:
6870 return fold_builtin_mempcpy (exp);
6872 case BUILT_IN_MEMMOVE:
6873 return fold_builtin_memmove (exp);
6875 case BUILT_IN_STRCPY:
6876 return fold_builtin_strcpy (exp);
6878 case BUILT_IN_STRNCPY:
6879 return fold_builtin_strncpy (exp);
6881 case BUILT_IN_MEMCMP:
6882 return fold_builtin_memcmp (exp);
6884 case BUILT_IN_STRCMP:
6885 return fold_builtin_strcmp (exp);
6887 case BUILT_IN_STRNCMP:
6888 return fold_builtin_strncmp (exp);
6890 default:
6891 break;
6894 return 0;
6897 /* Conveniently construct a function call expression. */
6899 tree
6900 build_function_call_expr (tree fn, tree arglist)
6902 tree call_expr;
6904 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
6905 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
6906 call_expr, arglist);
6907 return fold (call_expr);
6910 /* This function validates the types of a function call argument list
6911 represented as a tree chain of parameters against a specified list
6912 of tree_codes. If the last specifier is a 0, that represents an
6913 ellipses, otherwise the last specifier must be a VOID_TYPE. */
6915 static int
6916 validate_arglist (tree arglist, ...)
6918 enum tree_code code;
6919 int res = 0;
6920 va_list ap;
6922 va_start (ap, arglist);
6926 code = va_arg (ap, enum tree_code);
6927 switch (code)
6929 case 0:
6930 /* This signifies an ellipses, any further arguments are all ok. */
6931 res = 1;
6932 goto end;
6933 case VOID_TYPE:
6934 /* This signifies an endlink, if no arguments remain, return
6935 true, otherwise return false. */
6936 res = arglist == 0;
6937 goto end;
6938 default:
6939 /* If no parameters remain or the parameter's code does not
6940 match the specified code, return false. Otherwise continue
6941 checking any remaining arguments. */
6942 if (arglist == 0
6943 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
6944 goto end;
6945 break;
6947 arglist = TREE_CHAIN (arglist);
6949 while (1);
6951 /* We need gotos here since we can only have one VA_CLOSE in a
6952 function. */
6953 end: ;
6954 va_end (ap);
6956 return res;
6959 /* Default target-specific builtin expander that does nothing. */
6962 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
6963 rtx target ATTRIBUTE_UNUSED,
6964 rtx subtarget ATTRIBUTE_UNUSED,
6965 enum machine_mode mode ATTRIBUTE_UNUSED,
6966 int ignore ATTRIBUTE_UNUSED)
6968 return NULL_RTX;
6971 /* Instantiate all remaining CONSTANT_P_RTX nodes. */
6973 void
6974 purge_builtin_constant_p (void)
6976 rtx insn, set, arg, new, note;
6978 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6979 if (INSN_P (insn)
6980 && (set = single_set (insn)) != NULL_RTX
6981 && (GET_CODE (arg = SET_SRC (set)) == CONSTANT_P_RTX
6982 || (GET_CODE (arg) == SUBREG
6983 && (GET_CODE (arg = SUBREG_REG (arg))
6984 == CONSTANT_P_RTX))))
6986 arg = XEXP (arg, 0);
6987 new = CONSTANT_P (arg) ? const1_rtx : const0_rtx;
6988 validate_change (insn, &SET_SRC (set), new, 0);
6990 /* Remove the REG_EQUAL note from the insn. */
6991 if ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0)
6992 remove_note (insn, note);
6996 /* Returns true is EXP represents data that would potentially reside
6997 in a readonly section. */
6999 static bool
7000 readonly_data_expr (tree exp)
7002 STRIP_NOPS (exp);
7004 if (TREE_CODE (exp) == ADDR_EXPR)
7005 return decl_readonly_section (TREE_OPERAND (exp, 0), 0);
7006 else
7007 return false;