* dwarf2out.c (loc_descriptor_from_tree, case CONSTRUCTOR): New case.
[official-gcc.git] / gcc / builtins.c
bloba8c1d47277f7824a722952aadce5cdf40ec92a59
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 /* Trigonometric and mathematical constants used in builtin folding. */
83 static bool builtin_dconsts_init = 0;
84 static REAL_VALUE_TYPE dconstpi;
85 static REAL_VALUE_TYPE dconste;
87 static int get_pointer_alignment (tree, unsigned int);
88 static tree c_strlen (tree, int);
89 static const char *c_getstr (tree);
90 static rtx c_readstr (const char *, enum machine_mode);
91 static int target_char_cast (tree, char *);
92 static rtx get_memory_rtx (tree);
93 static tree build_string_literal (int, const char *);
94 static int apply_args_size (void);
95 static int apply_result_size (void);
96 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
97 static rtx result_vector (int, rtx);
98 #endif
99 static rtx expand_builtin_setjmp (tree, rtx);
100 static void expand_builtin_prefetch (tree);
101 static rtx expand_builtin_apply_args (void);
102 static rtx expand_builtin_apply_args_1 (void);
103 static rtx expand_builtin_apply (rtx, rtx, rtx);
104 static void expand_builtin_return (rtx);
105 static enum type_class type_to_class (tree);
106 static rtx expand_builtin_classify_type (tree);
107 static void expand_errno_check (tree, rtx);
108 static rtx expand_builtin_mathfn (tree, rtx, rtx);
109 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
110 static rtx expand_builtin_constant_p (tree, enum machine_mode);
111 static rtx expand_builtin_args_info (tree);
112 static rtx expand_builtin_next_arg (tree);
113 static rtx expand_builtin_va_start (tree);
114 static rtx expand_builtin_va_end (tree);
115 static rtx expand_builtin_va_copy (tree);
116 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
119 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
120 static rtx expand_builtin_strcat (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
123 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int);
126 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_bcopy (tree);
128 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
130 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
131 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
132 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
133 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
134 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_bzero (tree);
136 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
139 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
140 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
141 static rtx expand_builtin_alloca (tree, rtx);
142 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
143 static rtx expand_builtin_frame_address (tree, tree);
144 static rtx expand_builtin_fputs (tree, rtx, bool);
145 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
146 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
147 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
148 static tree stabilize_va_list (tree, int);
149 static rtx expand_builtin_expect (tree, rtx);
150 static tree fold_builtin_constant_p (tree);
151 static tree fold_builtin_classify_type (tree);
152 static tree fold_builtin_inf (tree, int);
153 static tree fold_builtin_nan (tree, tree, int);
154 static int validate_arglist (tree, ...);
155 static bool integer_valued_real_p (tree);
156 static tree fold_trunc_transparent_mathfn (tree);
157 static bool readonly_data_expr (tree);
158 static rtx expand_builtin_fabs (tree, rtx, rtx);
159 static rtx expand_builtin_cabs (tree, rtx);
160 static void init_builtin_dconsts (void);
161 static tree fold_builtin_cabs (tree, tree, tree);
162 static tree fold_builtin_trunc (tree);
163 static tree fold_builtin_floor (tree);
164 static tree fold_builtin_ceil (tree);
166 /* Initialize mathematical constants for constant folding builtins.
167 These constants need to be given to at least 160 bits precision. */
169 static void
170 init_builtin_dconsts (void)
172 real_from_string (&dconstpi,
173 "3.1415926535897932384626433832795028841971693993751058209749445923078");
174 real_from_string (&dconste,
175 "2.7182818284590452353602874713526624977572470936999595749669676277241");
177 builtin_dconsts_init = true;
180 /* Return the alignment in bits of EXP, a pointer valued expression.
181 But don't return more than MAX_ALIGN no matter what.
182 The alignment returned is, by default, the alignment of the thing that
183 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
185 Otherwise, look at the expression to see if we can do better, i.e., if the
186 expression is actually pointing at an object whose alignment is tighter. */
188 static int
189 get_pointer_alignment (tree exp, unsigned int max_align)
191 unsigned int align, inner;
193 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
194 return 0;
196 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
197 align = MIN (align, max_align);
199 while (1)
201 switch (TREE_CODE (exp))
203 case NOP_EXPR:
204 case CONVERT_EXPR:
205 case NON_LVALUE_EXPR:
206 exp = TREE_OPERAND (exp, 0);
207 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
208 return align;
210 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
211 align = MIN (inner, max_align);
212 break;
214 case PLUS_EXPR:
215 /* If sum of pointer + int, restrict our maximum alignment to that
216 imposed by the integer. If not, we can't do any better than
217 ALIGN. */
218 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
219 return align;
221 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
222 & (max_align / BITS_PER_UNIT - 1))
223 != 0)
224 max_align >>= 1;
226 exp = TREE_OPERAND (exp, 0);
227 break;
229 case ADDR_EXPR:
230 /* See what we are pointing at and look at its alignment. */
231 exp = TREE_OPERAND (exp, 0);
232 if (TREE_CODE (exp) == FUNCTION_DECL)
233 align = FUNCTION_BOUNDARY;
234 else if (DECL_P (exp))
235 align = DECL_ALIGN (exp);
236 #ifdef CONSTANT_ALIGNMENT
237 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
238 align = CONSTANT_ALIGNMENT (exp, align);
239 #endif
240 return MIN (align, max_align);
242 default:
243 return align;
248 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
249 way, because it could contain a zero byte in the middle.
250 TREE_STRING_LENGTH is the size of the character array, not the string.
252 ONLY_VALUE should be nonzero if the result is not going to be emitted
253 into the instruction stream and zero if it is going to be expanded.
254 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
255 is returned, otherwise NULL, since
256 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
257 evaluate the side-effects.
259 The value returned is of type `ssizetype'.
261 Unfortunately, string_constant can't access the values of const char
262 arrays with initializers, so neither can we do so here. */
264 static tree
265 c_strlen (tree src, int only_value)
267 tree offset_node;
268 HOST_WIDE_INT offset;
269 int max;
270 const char *ptr;
272 STRIP_NOPS (src);
273 if (TREE_CODE (src) == COND_EXPR
274 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
276 tree len1, len2;
278 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
279 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
280 if (tree_int_cst_equal (len1, len2))
281 return len1;
284 if (TREE_CODE (src) == COMPOUND_EXPR
285 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
286 return c_strlen (TREE_OPERAND (src, 1), only_value);
288 src = string_constant (src, &offset_node);
289 if (src == 0)
290 return 0;
292 max = TREE_STRING_LENGTH (src) - 1;
293 ptr = TREE_STRING_POINTER (src);
295 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
297 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
298 compute the offset to the following null if we don't know where to
299 start searching for it. */
300 int i;
302 for (i = 0; i < max; i++)
303 if (ptr[i] == 0)
304 return 0;
306 /* We don't know the starting offset, but we do know that the string
307 has no internal zero bytes. We can assume that the offset falls
308 within the bounds of the string; otherwise, the programmer deserves
309 what he gets. Subtract the offset from the length of the string,
310 and return that. This would perhaps not be valid if we were dealing
311 with named arrays in addition to literal string constants. */
313 return size_diffop (size_int (max), offset_node);
316 /* We have a known offset into the string. Start searching there for
317 a null character if we can represent it as a single HOST_WIDE_INT. */
318 if (offset_node == 0)
319 offset = 0;
320 else if (! host_integerp (offset_node, 0))
321 offset = -1;
322 else
323 offset = tree_low_cst (offset_node, 0);
325 /* If the offset is known to be out of bounds, warn, and call strlen at
326 runtime. */
327 if (offset < 0 || offset > max)
329 warning ("offset outside bounds of constant string");
330 return 0;
333 /* Use strlen to search for the first zero byte. Since any strings
334 constructed with build_string will have nulls appended, we win even
335 if we get handed something like (char[4])"abcd".
337 Since OFFSET is our starting index into the string, no further
338 calculation is needed. */
339 return ssize_int (strlen (ptr + offset));
342 /* Return a char pointer for a C string if it is a string constant
343 or sum of string constant and integer constant. */
345 static const char *
346 c_getstr (tree src)
348 tree offset_node;
350 src = string_constant (src, &offset_node);
351 if (src == 0)
352 return 0;
354 if (offset_node == 0)
355 return TREE_STRING_POINTER (src);
356 else if (!host_integerp (offset_node, 1)
357 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
358 return 0;
360 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
363 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
364 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
366 static rtx
367 c_readstr (const char *str, enum machine_mode mode)
369 HOST_WIDE_INT c[2];
370 HOST_WIDE_INT ch;
371 unsigned int i, j;
373 if (GET_MODE_CLASS (mode) != MODE_INT)
374 abort ();
375 c[0] = 0;
376 c[1] = 0;
377 ch = 1;
378 for (i = 0; i < GET_MODE_SIZE (mode); i++)
380 j = i;
381 if (WORDS_BIG_ENDIAN)
382 j = GET_MODE_SIZE (mode) - i - 1;
383 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
384 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
385 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
386 j *= BITS_PER_UNIT;
387 if (j > 2 * HOST_BITS_PER_WIDE_INT)
388 abort ();
389 if (ch)
390 ch = (unsigned char) str[i];
391 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
393 return immed_double_const (c[0], c[1], mode);
396 /* Cast a target constant CST to target CHAR and if that value fits into
397 host char type, return zero and put that value into variable pointed by
398 P. */
400 static int
401 target_char_cast (tree cst, char *p)
403 unsigned HOST_WIDE_INT val, hostval;
405 if (!host_integerp (cst, 1)
406 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
407 return 1;
409 val = tree_low_cst (cst, 1);
410 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
411 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
413 hostval = val;
414 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
415 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
417 if (val != hostval)
418 return 1;
420 *p = hostval;
421 return 0;
424 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
425 times to get the address of either a higher stack frame, or a return
426 address located within it (depending on FNDECL_CODE). */
429 expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
430 rtx tem)
432 int i;
434 /* Some machines need special handling before we can access
435 arbitrary frames. For example, on the sparc, we must first flush
436 all register windows to the stack. */
437 #ifdef SETUP_FRAME_ADDRESSES
438 if (count > 0)
439 SETUP_FRAME_ADDRESSES ();
440 #endif
442 /* On the sparc, the return address is not in the frame, it is in a
443 register. There is no way to access it off of the current frame
444 pointer, but it can be accessed off the previous frame pointer by
445 reading the value from the register window save area. */
446 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
447 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
448 count--;
449 #endif
451 /* Scan back COUNT frames to the specified frame. */
452 for (i = 0; i < count; i++)
454 /* Assume the dynamic chain pointer is in the word that the
455 frame address points to, unless otherwise specified. */
456 #ifdef DYNAMIC_CHAIN_ADDRESS
457 tem = DYNAMIC_CHAIN_ADDRESS (tem);
458 #endif
459 tem = memory_address (Pmode, tem);
460 tem = gen_rtx_MEM (Pmode, tem);
461 set_mem_alias_set (tem, get_frame_alias_set ());
462 tem = copy_to_reg (tem);
465 /* For __builtin_frame_address, return what we've got. */
466 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
467 return tem;
469 /* For __builtin_return_address, Get the return address from that
470 frame. */
471 #ifdef RETURN_ADDR_RTX
472 tem = RETURN_ADDR_RTX (count, tem);
473 #else
474 tem = memory_address (Pmode,
475 plus_constant (tem, GET_MODE_SIZE (Pmode)));
476 tem = gen_rtx_MEM (Pmode, tem);
477 set_mem_alias_set (tem, get_frame_alias_set ());
478 #endif
479 return tem;
482 /* Alias set used for setjmp buffer. */
483 static HOST_WIDE_INT setjmp_alias_set = -1;
485 /* Construct the leading half of a __builtin_setjmp call. Control will
486 return to RECEIVER_LABEL. This is used directly by sjlj exception
487 handling code. */
489 void
490 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
492 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
493 rtx stack_save;
494 rtx mem;
496 if (setjmp_alias_set == -1)
497 setjmp_alias_set = new_alias_set ();
499 #ifdef POINTERS_EXTEND_UNSIGNED
500 if (GET_MODE (buf_addr) != Pmode)
501 buf_addr = convert_memory_address (Pmode, buf_addr);
502 #endif
504 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
506 emit_queue ();
508 /* We store the frame pointer and the address of receiver_label in
509 the buffer and use the rest of it for the stack save area, which
510 is machine-dependent. */
512 #ifndef BUILTIN_SETJMP_FRAME_VALUE
513 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
514 #endif
516 mem = gen_rtx_MEM (Pmode, buf_addr);
517 set_mem_alias_set (mem, setjmp_alias_set);
518 emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE);
520 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
521 set_mem_alias_set (mem, setjmp_alias_set);
523 emit_move_insn (validize_mem (mem),
524 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
526 stack_save = gen_rtx_MEM (sa_mode,
527 plus_constant (buf_addr,
528 2 * GET_MODE_SIZE (Pmode)));
529 set_mem_alias_set (stack_save, setjmp_alias_set);
530 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
532 /* If there is further processing to do, do it. */
533 #ifdef HAVE_builtin_setjmp_setup
534 if (HAVE_builtin_setjmp_setup)
535 emit_insn (gen_builtin_setjmp_setup (buf_addr));
536 #endif
538 /* Tell optimize_save_area_alloca that extra work is going to
539 need to go on during alloca. */
540 current_function_calls_setjmp = 1;
542 /* Set this so all the registers get saved in our frame; we need to be
543 able to copy the saved values for any registers from frames we unwind. */
544 current_function_has_nonlocal_label = 1;
547 /* Construct the trailing part of a __builtin_setjmp call.
548 This is used directly by sjlj exception handling code. */
550 void
551 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
553 /* Clobber the FP when we get here, so we have to make sure it's
554 marked as used by this function. */
555 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
557 /* Mark the static chain as clobbered here so life information
558 doesn't get messed up for it. */
559 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
561 /* Now put in the code to restore the frame pointer, and argument
562 pointer, if needed. The code below is from expand_end_bindings
563 in stmt.c; see detailed documentation there. */
564 #ifdef HAVE_nonlocal_goto
565 if (! HAVE_nonlocal_goto)
566 #endif
567 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
569 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
570 if (fixed_regs[ARG_POINTER_REGNUM])
572 #ifdef ELIMINABLE_REGS
573 size_t i;
574 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
576 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
577 if (elim_regs[i].from == ARG_POINTER_REGNUM
578 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
579 break;
581 if (i == ARRAY_SIZE (elim_regs))
582 #endif
584 /* Now restore our arg pointer from the address at which it
585 was saved in our stack frame. */
586 emit_move_insn (virtual_incoming_args_rtx,
587 copy_to_reg (get_arg_pointer_save_area (cfun)));
590 #endif
592 #ifdef HAVE_builtin_setjmp_receiver
593 if (HAVE_builtin_setjmp_receiver)
594 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
595 else
596 #endif
597 #ifdef HAVE_nonlocal_goto_receiver
598 if (HAVE_nonlocal_goto_receiver)
599 emit_insn (gen_nonlocal_goto_receiver ());
600 else
601 #endif
602 { /* Nothing */ }
604 /* @@@ This is a kludge. Not all machine descriptions define a blockage
605 insn, but we must not allow the code we just generated to be reordered
606 by scheduling. Specifically, the update of the frame pointer must
607 happen immediately, not later. So emit an ASM_INPUT to act as blockage
608 insn. */
609 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
612 /* __builtin_setjmp is passed a pointer to an array of five words (not
613 all will be used on all machines). It operates similarly to the C
614 library function of the same name, but is more efficient. Much of
615 the code below (and for longjmp) is copied from the handling of
616 non-local gotos.
618 NOTE: This is intended for use by GNAT and the exception handling
619 scheme in the compiler and will only work in the method used by
620 them. */
622 static rtx
623 expand_builtin_setjmp (tree arglist, rtx target)
625 rtx buf_addr, next_lab, cont_lab;
627 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
628 return NULL_RTX;
630 if (target == 0 || GET_CODE (target) != REG
631 || REGNO (target) < FIRST_PSEUDO_REGISTER)
632 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
634 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
636 next_lab = gen_label_rtx ();
637 cont_lab = gen_label_rtx ();
639 expand_builtin_setjmp_setup (buf_addr, next_lab);
641 /* Set TARGET to zero and branch to the continue label. */
642 emit_move_insn (target, const0_rtx);
643 emit_jump_insn (gen_jump (cont_lab));
644 emit_barrier ();
645 emit_label (next_lab);
647 expand_builtin_setjmp_receiver (next_lab);
649 /* Set TARGET to one. */
650 emit_move_insn (target, const1_rtx);
651 emit_label (cont_lab);
653 /* Tell flow about the strange goings on. Putting `next_lab' on
654 `nonlocal_goto_handler_labels' to indicates that function
655 calls may traverse the arc back to this label. */
657 current_function_has_nonlocal_label = 1;
658 nonlocal_goto_handler_labels
659 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
661 return target;
664 /* __builtin_longjmp is passed a pointer to an array of five words (not
665 all will be used on all machines). It operates similarly to the C
666 library function of the same name, but is more efficient. Much of
667 the code below is copied from the handling of non-local gotos.
669 NOTE: This is intended for use by GNAT and the exception handling
670 scheme in the compiler and will only work in the method used by
671 them. */
673 void
674 expand_builtin_longjmp (rtx buf_addr, rtx value)
676 rtx fp, lab, stack, insn, last;
677 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
679 if (setjmp_alias_set == -1)
680 setjmp_alias_set = new_alias_set ();
682 #ifdef POINTERS_EXTEND_UNSIGNED
683 if (GET_MODE (buf_addr) != Pmode)
684 buf_addr = convert_memory_address (Pmode, buf_addr);
685 #endif
687 buf_addr = force_reg (Pmode, buf_addr);
689 /* We used to store value in static_chain_rtx, but that fails if pointers
690 are smaller than integers. We instead require that the user must pass
691 a second argument of 1, because that is what builtin_setjmp will
692 return. This also makes EH slightly more efficient, since we are no
693 longer copying around a value that we don't care about. */
694 if (value != const1_rtx)
695 abort ();
697 current_function_calls_longjmp = 1;
699 last = get_last_insn ();
700 #ifdef HAVE_builtin_longjmp
701 if (HAVE_builtin_longjmp)
702 emit_insn (gen_builtin_longjmp (buf_addr));
703 else
704 #endif
706 fp = gen_rtx_MEM (Pmode, buf_addr);
707 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
708 GET_MODE_SIZE (Pmode)));
710 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
711 2 * GET_MODE_SIZE (Pmode)));
712 set_mem_alias_set (fp, setjmp_alias_set);
713 set_mem_alias_set (lab, setjmp_alias_set);
714 set_mem_alias_set (stack, setjmp_alias_set);
716 /* Pick up FP, label, and SP from the block and jump. This code is
717 from expand_goto in stmt.c; see there for detailed comments. */
718 #if HAVE_nonlocal_goto
719 if (HAVE_nonlocal_goto)
720 /* We have to pass a value to the nonlocal_goto pattern that will
721 get copied into the static_chain pointer, but it does not matter
722 what that value is, because builtin_setjmp does not use it. */
723 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
724 else
725 #endif
727 lab = copy_to_reg (lab);
729 emit_move_insn (hard_frame_pointer_rtx, fp);
730 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
732 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
733 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
734 emit_indirect_jump (lab);
738 /* Search backwards and mark the jump insn as a non-local goto.
739 Note that this precludes the use of __builtin_longjmp to a
740 __builtin_setjmp target in the same function. However, we've
741 already cautioned the user that these functions are for
742 internal exception handling use only. */
743 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
745 if (insn == last)
746 abort ();
747 if (GET_CODE (insn) == JUMP_INSN)
749 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
750 REG_NOTES (insn));
751 break;
753 else if (GET_CODE (insn) == CALL_INSN)
754 break;
758 /* Expand a call to __builtin_prefetch. For a target that does not support
759 data prefetch, evaluate the memory address argument in case it has side
760 effects. */
762 static void
763 expand_builtin_prefetch (tree arglist)
765 tree arg0, arg1, arg2;
766 rtx op0, op1, op2;
768 if (!validate_arglist (arglist, POINTER_TYPE, 0))
769 return;
771 arg0 = TREE_VALUE (arglist);
772 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
773 zero (read) and argument 2 (locality) defaults to 3 (high degree of
774 locality). */
775 if (TREE_CHAIN (arglist))
777 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
778 if (TREE_CHAIN (TREE_CHAIN (arglist)))
779 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
780 else
781 arg2 = build_int_2 (3, 0);
783 else
785 arg1 = integer_zero_node;
786 arg2 = build_int_2 (3, 0);
789 /* Argument 0 is an address. */
790 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
792 /* Argument 1 (read/write flag) must be a compile-time constant int. */
793 if (TREE_CODE (arg1) != INTEGER_CST)
795 error ("second arg to `__builtin_prefetch' must be a constant");
796 arg1 = integer_zero_node;
798 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
799 /* Argument 1 must be either zero or one. */
800 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
802 warning ("invalid second arg to __builtin_prefetch; using zero");
803 op1 = const0_rtx;
806 /* Argument 2 (locality) must be a compile-time constant int. */
807 if (TREE_CODE (arg2) != INTEGER_CST)
809 error ("third arg to `__builtin_prefetch' must be a constant");
810 arg2 = integer_zero_node;
812 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
813 /* Argument 2 must be 0, 1, 2, or 3. */
814 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
816 warning ("invalid third arg to __builtin_prefetch; using zero");
817 op2 = const0_rtx;
820 #ifdef HAVE_prefetch
821 if (HAVE_prefetch)
823 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
824 (op0,
825 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
826 || (GET_MODE (op0) != Pmode))
828 #ifdef POINTERS_EXTEND_UNSIGNED
829 if (GET_MODE (op0) != Pmode)
830 op0 = convert_memory_address (Pmode, op0);
831 #endif
832 op0 = force_reg (Pmode, op0);
834 emit_insn (gen_prefetch (op0, op1, op2));
836 else
837 #endif
838 op0 = protect_from_queue (op0, 0);
839 /* Don't do anything with direct references to volatile memory, but
840 generate code to handle other side effects. */
841 if (GET_CODE (op0) != MEM && side_effects_p (op0))
842 emit_insn (op0);
845 /* Get a MEM rtx for expression EXP which is the address of an operand
846 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
848 static rtx
849 get_memory_rtx (tree exp)
851 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
852 rtx mem;
854 #ifdef POINTERS_EXTEND_UNSIGNED
855 if (GET_MODE (addr) != Pmode)
856 addr = convert_memory_address (Pmode, addr);
857 #endif
859 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
861 /* Get an expression we can use to find the attributes to assign to MEM.
862 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
863 we can. First remove any nops. */
864 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
865 || TREE_CODE (exp) == NON_LVALUE_EXPR)
866 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
867 exp = TREE_OPERAND (exp, 0);
869 if (TREE_CODE (exp) == ADDR_EXPR)
871 exp = TREE_OPERAND (exp, 0);
872 set_mem_attributes (mem, exp, 0);
874 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
876 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
877 /* memcpy, memset and other builtin stringops can alias with anything. */
878 set_mem_alias_set (mem, 0);
881 return mem;
884 /* Built-in functions to perform an untyped call and return. */
886 /* For each register that may be used for calling a function, this
887 gives a mode used to copy the register's value. VOIDmode indicates
888 the register is not used for calling a function. If the machine
889 has register windows, this gives only the outbound registers.
890 INCOMING_REGNO gives the corresponding inbound register. */
891 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
893 /* For each register that may be used for returning values, this gives
894 a mode used to copy the register's value. VOIDmode indicates the
895 register is not used for returning values. If the machine has
896 register windows, this gives only the outbound registers.
897 INCOMING_REGNO gives the corresponding inbound register. */
898 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
900 /* For each register that may be used for calling a function, this
901 gives the offset of that register into the block returned by
902 __builtin_apply_args. 0 indicates that the register is not
903 used for calling a function. */
904 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
906 /* Return the offset of register REGNO into the block returned by
907 __builtin_apply_args. This is not declared static, since it is
908 needed in objc-act.c. */
911 apply_args_register_offset (int regno)
913 apply_args_size ();
915 /* Arguments are always put in outgoing registers (in the argument
916 block) if such make sense. */
917 #ifdef OUTGOING_REGNO
918 regno = OUTGOING_REGNO (regno);
919 #endif
920 return apply_args_reg_offset[regno];
923 /* Return the size required for the block returned by __builtin_apply_args,
924 and initialize apply_args_mode. */
926 static int
927 apply_args_size (void)
929 static int size = -1;
930 int align;
931 unsigned int regno;
932 enum machine_mode mode;
934 /* The values computed by this function never change. */
935 if (size < 0)
937 /* The first value is the incoming arg-pointer. */
938 size = GET_MODE_SIZE (Pmode);
940 /* The second value is the structure value address unless this is
941 passed as an "invisible" first argument. */
942 if (struct_value_rtx)
943 size += GET_MODE_SIZE (Pmode);
945 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
946 if (FUNCTION_ARG_REGNO_P (regno))
948 /* Search for the proper mode for copying this register's
949 value. I'm not sure this is right, but it works so far. */
950 enum machine_mode best_mode = VOIDmode;
952 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
953 mode != VOIDmode;
954 mode = GET_MODE_WIDER_MODE (mode))
955 if (HARD_REGNO_MODE_OK (regno, mode)
956 && HARD_REGNO_NREGS (regno, mode) == 1)
957 best_mode = mode;
959 if (best_mode == VOIDmode)
960 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
961 mode != VOIDmode;
962 mode = GET_MODE_WIDER_MODE (mode))
963 if (HARD_REGNO_MODE_OK (regno, mode)
964 && have_insn_for (SET, mode))
965 best_mode = mode;
967 if (best_mode == VOIDmode)
968 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
969 mode != VOIDmode;
970 mode = GET_MODE_WIDER_MODE (mode))
971 if (HARD_REGNO_MODE_OK (regno, mode)
972 && have_insn_for (SET, mode))
973 best_mode = mode;
975 if (best_mode == VOIDmode)
976 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
977 mode != VOIDmode;
978 mode = GET_MODE_WIDER_MODE (mode))
979 if (HARD_REGNO_MODE_OK (regno, mode)
980 && have_insn_for (SET, mode))
981 best_mode = mode;
983 mode = best_mode;
984 if (mode == VOIDmode)
985 abort ();
987 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
988 if (size % align != 0)
989 size = CEIL (size, align) * align;
990 apply_args_reg_offset[regno] = size;
991 size += GET_MODE_SIZE (mode);
992 apply_args_mode[regno] = mode;
994 else
996 apply_args_mode[regno] = VOIDmode;
997 apply_args_reg_offset[regno] = 0;
1000 return size;
1003 /* Return the size required for the block returned by __builtin_apply,
1004 and initialize apply_result_mode. */
1006 static int
1007 apply_result_size (void)
1009 static int size = -1;
1010 int align, regno;
1011 enum machine_mode mode;
1013 /* The values computed by this function never change. */
1014 if (size < 0)
1016 size = 0;
1018 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1019 if (FUNCTION_VALUE_REGNO_P (regno))
1021 /* Search for the proper mode for copying this register's
1022 value. I'm not sure this is right, but it works so far. */
1023 enum machine_mode best_mode = VOIDmode;
1025 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1026 mode != TImode;
1027 mode = GET_MODE_WIDER_MODE (mode))
1028 if (HARD_REGNO_MODE_OK (regno, mode))
1029 best_mode = mode;
1031 if (best_mode == VOIDmode)
1032 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1033 mode != VOIDmode;
1034 mode = GET_MODE_WIDER_MODE (mode))
1035 if (HARD_REGNO_MODE_OK (regno, mode)
1036 && have_insn_for (SET, mode))
1037 best_mode = mode;
1039 if (best_mode == VOIDmode)
1040 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
1041 mode != VOIDmode;
1042 mode = GET_MODE_WIDER_MODE (mode))
1043 if (HARD_REGNO_MODE_OK (regno, mode)
1044 && have_insn_for (SET, mode))
1045 best_mode = mode;
1047 if (best_mode == VOIDmode)
1048 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
1049 mode != VOIDmode;
1050 mode = GET_MODE_WIDER_MODE (mode))
1051 if (HARD_REGNO_MODE_OK (regno, mode)
1052 && have_insn_for (SET, mode))
1053 best_mode = mode;
1055 mode = best_mode;
1056 if (mode == VOIDmode)
1057 abort ();
1059 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1060 if (size % align != 0)
1061 size = CEIL (size, align) * align;
1062 size += GET_MODE_SIZE (mode);
1063 apply_result_mode[regno] = mode;
1065 else
1066 apply_result_mode[regno] = VOIDmode;
1068 /* Allow targets that use untyped_call and untyped_return to override
1069 the size so that machine-specific information can be stored here. */
1070 #ifdef APPLY_RESULT_SIZE
1071 size = APPLY_RESULT_SIZE;
1072 #endif
1074 return size;
1077 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1078 /* Create a vector describing the result block RESULT. If SAVEP is true,
1079 the result block is used to save the values; otherwise it is used to
1080 restore the values. */
1082 static rtx
1083 result_vector (int savep, rtx result)
1085 int regno, size, align, nelts;
1086 enum machine_mode mode;
1087 rtx reg, mem;
1088 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1090 size = nelts = 0;
1091 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1092 if ((mode = apply_result_mode[regno]) != VOIDmode)
1094 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1095 if (size % align != 0)
1096 size = CEIL (size, align) * align;
1097 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1098 mem = adjust_address (result, mode, size);
1099 savevec[nelts++] = (savep
1100 ? gen_rtx_SET (VOIDmode, mem, reg)
1101 : gen_rtx_SET (VOIDmode, reg, mem));
1102 size += GET_MODE_SIZE (mode);
1104 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1106 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1108 /* Save the state required to perform an untyped call with the same
1109 arguments as were passed to the current function. */
1111 static rtx
1112 expand_builtin_apply_args_1 (void)
1114 rtx registers;
1115 int size, align, regno;
1116 enum machine_mode mode;
1118 /* Create a block where the arg-pointer, structure value address,
1119 and argument registers can be saved. */
1120 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1122 /* Walk past the arg-pointer and structure value address. */
1123 size = GET_MODE_SIZE (Pmode);
1124 if (struct_value_rtx)
1125 size += GET_MODE_SIZE (Pmode);
1127 /* Save each register used in calling a function to the block. */
1128 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1129 if ((mode = apply_args_mode[regno]) != VOIDmode)
1131 rtx tem;
1133 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1134 if (size % align != 0)
1135 size = CEIL (size, align) * align;
1137 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1139 emit_move_insn (adjust_address (registers, mode, size), tem);
1140 size += GET_MODE_SIZE (mode);
1143 /* Save the arg pointer to the block. */
1144 emit_move_insn (adjust_address (registers, Pmode, 0),
1145 copy_to_reg (virtual_incoming_args_rtx));
1146 size = GET_MODE_SIZE (Pmode);
1148 /* Save the structure value address unless this is passed as an
1149 "invisible" first argument. */
1150 if (struct_value_incoming_rtx)
1152 emit_move_insn (adjust_address (registers, Pmode, size),
1153 copy_to_reg (struct_value_incoming_rtx));
1154 size += GET_MODE_SIZE (Pmode);
1157 /* Return the address of the block. */
1158 return copy_addr_to_reg (XEXP (registers, 0));
1161 /* __builtin_apply_args returns block of memory allocated on
1162 the stack into which is stored the arg pointer, structure
1163 value address, static chain, and all the registers that might
1164 possibly be used in performing a function call. The code is
1165 moved to the start of the function so the incoming values are
1166 saved. */
1168 static rtx
1169 expand_builtin_apply_args (void)
1171 /* Don't do __builtin_apply_args more than once in a function.
1172 Save the result of the first call and reuse it. */
1173 if (apply_args_value != 0)
1174 return apply_args_value;
1176 /* When this function is called, it means that registers must be
1177 saved on entry to this function. So we migrate the
1178 call to the first insn of this function. */
1179 rtx temp;
1180 rtx seq;
1182 start_sequence ();
1183 temp = expand_builtin_apply_args_1 ();
1184 seq = get_insns ();
1185 end_sequence ();
1187 apply_args_value = temp;
1189 /* Put the insns after the NOTE that starts the function.
1190 If this is inside a start_sequence, make the outer-level insn
1191 chain current, so the code is placed at the start of the
1192 function. */
1193 push_topmost_sequence ();
1194 emit_insn_before (seq, NEXT_INSN (get_insns ()));
1195 pop_topmost_sequence ();
1196 return temp;
1200 /* Perform an untyped call and save the state required to perform an
1201 untyped return of whatever value was returned by the given function. */
1203 static rtx
1204 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1206 int size, align, regno;
1207 enum machine_mode mode;
1208 rtx incoming_args, result, reg, dest, src, call_insn;
1209 rtx old_stack_level = 0;
1210 rtx call_fusage = 0;
1212 #ifdef POINTERS_EXTEND_UNSIGNED
1213 if (GET_MODE (arguments) != Pmode)
1214 arguments = convert_memory_address (Pmode, arguments);
1215 #endif
1217 /* Create a block where the return registers can be saved. */
1218 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1220 /* Fetch the arg pointer from the ARGUMENTS block. */
1221 incoming_args = gen_reg_rtx (Pmode);
1222 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1223 #ifndef STACK_GROWS_DOWNWARD
1224 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1225 incoming_args, 0, OPTAB_LIB_WIDEN);
1226 #endif
1228 /* Perform postincrements before actually calling the function. */
1229 emit_queue ();
1231 /* Push a new argument block and copy the arguments. Do not allow
1232 the (potential) memcpy call below to interfere with our stack
1233 manipulations. */
1234 do_pending_stack_adjust ();
1235 NO_DEFER_POP;
1237 /* Save the stack with nonlocal if available. */
1238 #ifdef HAVE_save_stack_nonlocal
1239 if (HAVE_save_stack_nonlocal)
1240 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1241 else
1242 #endif
1243 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1245 /* Push a block of memory onto the stack to store the memory arguments.
1246 Save the address in a register, and copy the memory arguments. ??? I
1247 haven't figured out how the calling convention macros effect this,
1248 but it's likely that the source and/or destination addresses in
1249 the block copy will need updating in machine specific ways. */
1250 dest = allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1251 dest = gen_rtx_MEM (BLKmode, dest);
1252 set_mem_align (dest, PARM_BOUNDARY);
1253 src = gen_rtx_MEM (BLKmode, incoming_args);
1254 set_mem_align (src, PARM_BOUNDARY);
1255 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1257 /* Refer to the argument block. */
1258 apply_args_size ();
1259 arguments = gen_rtx_MEM (BLKmode, arguments);
1260 set_mem_align (arguments, PARM_BOUNDARY);
1262 /* Walk past the arg-pointer and structure value address. */
1263 size = GET_MODE_SIZE (Pmode);
1264 if (struct_value_rtx)
1265 size += GET_MODE_SIZE (Pmode);
1267 /* Restore each of the registers previously saved. Make USE insns
1268 for each of these registers for use in making the call. */
1269 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1270 if ((mode = apply_args_mode[regno]) != VOIDmode)
1272 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1273 if (size % align != 0)
1274 size = CEIL (size, align) * align;
1275 reg = gen_rtx_REG (mode, regno);
1276 emit_move_insn (reg, adjust_address (arguments, mode, size));
1277 use_reg (&call_fusage, reg);
1278 size += GET_MODE_SIZE (mode);
1281 /* Restore the structure value address unless this is passed as an
1282 "invisible" first argument. */
1283 size = GET_MODE_SIZE (Pmode);
1284 if (struct_value_rtx)
1286 rtx value = gen_reg_rtx (Pmode);
1287 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1288 emit_move_insn (struct_value_rtx, value);
1289 if (GET_CODE (struct_value_rtx) == REG)
1290 use_reg (&call_fusage, struct_value_rtx);
1291 size += GET_MODE_SIZE (Pmode);
1294 /* All arguments and registers used for the call are set up by now! */
1295 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0, 0);
1297 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1298 and we don't want to load it into a register as an optimization,
1299 because prepare_call_address already did it if it should be done. */
1300 if (GET_CODE (function) != SYMBOL_REF)
1301 function = memory_address (FUNCTION_MODE, function);
1303 /* Generate the actual call instruction and save the return value. */
1304 #ifdef HAVE_untyped_call
1305 if (HAVE_untyped_call)
1306 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1307 result, result_vector (1, result)));
1308 else
1309 #endif
1310 #ifdef HAVE_call_value
1311 if (HAVE_call_value)
1313 rtx valreg = 0;
1315 /* Locate the unique return register. It is not possible to
1316 express a call that sets more than one return register using
1317 call_value; use untyped_call for that. In fact, untyped_call
1318 only needs to save the return registers in the given block. */
1319 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1320 if ((mode = apply_result_mode[regno]) != VOIDmode)
1322 if (valreg)
1323 abort (); /* HAVE_untyped_call required. */
1324 valreg = gen_rtx_REG (mode, regno);
1327 emit_call_insn (GEN_CALL_VALUE (valreg,
1328 gen_rtx_MEM (FUNCTION_MODE, function),
1329 const0_rtx, NULL_RTX, const0_rtx));
1331 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1333 else
1334 #endif
1335 abort ();
1337 /* Find the CALL insn we just emitted, and attach the register usage
1338 information. */
1339 call_insn = last_call_insn ();
1340 add_function_usage_to (call_insn, call_fusage);
1342 /* Restore the stack. */
1343 #ifdef HAVE_save_stack_nonlocal
1344 if (HAVE_save_stack_nonlocal)
1345 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1346 else
1347 #endif
1348 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1350 OK_DEFER_POP;
1352 /* Return the address of the result block. */
1353 return copy_addr_to_reg (XEXP (result, 0));
1356 /* Perform an untyped return. */
1358 static void
1359 expand_builtin_return (rtx result)
1361 int size, align, regno;
1362 enum machine_mode mode;
1363 rtx reg;
1364 rtx call_fusage = 0;
1366 #ifdef POINTERS_EXTEND_UNSIGNED
1367 if (GET_MODE (result) != Pmode)
1368 result = convert_memory_address (Pmode, result);
1369 #endif
1371 apply_result_size ();
1372 result = gen_rtx_MEM (BLKmode, result);
1374 #ifdef HAVE_untyped_return
1375 if (HAVE_untyped_return)
1377 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1378 emit_barrier ();
1379 return;
1381 #endif
1383 /* Restore the return value and note that each value is used. */
1384 size = 0;
1385 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1386 if ((mode = apply_result_mode[regno]) != VOIDmode)
1388 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1389 if (size % align != 0)
1390 size = CEIL (size, align) * align;
1391 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1392 emit_move_insn (reg, adjust_address (result, mode, size));
1394 push_to_sequence (call_fusage);
1395 emit_insn (gen_rtx_USE (VOIDmode, reg));
1396 call_fusage = get_insns ();
1397 end_sequence ();
1398 size += GET_MODE_SIZE (mode);
1401 /* Put the USE insns before the return. */
1402 emit_insn (call_fusage);
1404 /* Return whatever values was restored by jumping directly to the end
1405 of the function. */
1406 expand_null_return ();
1409 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1411 static enum type_class
1412 type_to_class (tree type)
1414 switch (TREE_CODE (type))
1416 case VOID_TYPE: return void_type_class;
1417 case INTEGER_TYPE: return integer_type_class;
1418 case CHAR_TYPE: return char_type_class;
1419 case ENUMERAL_TYPE: return enumeral_type_class;
1420 case BOOLEAN_TYPE: return boolean_type_class;
1421 case POINTER_TYPE: return pointer_type_class;
1422 case REFERENCE_TYPE: return reference_type_class;
1423 case OFFSET_TYPE: return offset_type_class;
1424 case REAL_TYPE: return real_type_class;
1425 case COMPLEX_TYPE: return complex_type_class;
1426 case FUNCTION_TYPE: return function_type_class;
1427 case METHOD_TYPE: return method_type_class;
1428 case RECORD_TYPE: return record_type_class;
1429 case UNION_TYPE:
1430 case QUAL_UNION_TYPE: return union_type_class;
1431 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1432 ? string_type_class : array_type_class);
1433 case SET_TYPE: return set_type_class;
1434 case FILE_TYPE: return file_type_class;
1435 case LANG_TYPE: return lang_type_class;
1436 default: return no_type_class;
1440 /* Expand a call to __builtin_classify_type with arguments found in
1441 ARGLIST. */
1443 static rtx
1444 expand_builtin_classify_type (tree arglist)
1446 if (arglist != 0)
1447 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1448 return GEN_INT (no_type_class);
1451 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1453 static rtx
1454 expand_builtin_constant_p (tree arglist, enum machine_mode target_mode)
1456 rtx tmp;
1458 if (arglist == 0)
1459 return const0_rtx;
1460 arglist = TREE_VALUE (arglist);
1462 /* We have taken care of the easy cases during constant folding. This
1463 case is not obvious, so emit (constant_p_rtx (ARGLIST)) and let CSE
1464 get a chance to see if it can deduce whether ARGLIST is constant. */
1466 current_function_calls_constant_p = 1;
1468 tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0);
1469 tmp = gen_rtx_CONSTANT_P_RTX (target_mode, tmp);
1470 return tmp;
1473 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1474 if available. */
1475 tree
1476 mathfn_built_in (tree type, enum built_in_function fn)
1478 enum built_in_function fcode = NOT_BUILT_IN;
1479 if (TYPE_MODE (type) == TYPE_MODE (double_type_node))
1480 switch (fn)
1482 case BUILT_IN_SQRT:
1483 case BUILT_IN_SQRTF:
1484 case BUILT_IN_SQRTL:
1485 fcode = BUILT_IN_SQRT;
1486 break;
1487 case BUILT_IN_SIN:
1488 case BUILT_IN_SINF:
1489 case BUILT_IN_SINL:
1490 fcode = BUILT_IN_SIN;
1491 break;
1492 case BUILT_IN_COS:
1493 case BUILT_IN_COSF:
1494 case BUILT_IN_COSL:
1495 fcode = BUILT_IN_COS;
1496 break;
1497 case BUILT_IN_EXP:
1498 case BUILT_IN_EXPF:
1499 case BUILT_IN_EXPL:
1500 fcode = BUILT_IN_EXP;
1501 break;
1502 case BUILT_IN_LOG:
1503 case BUILT_IN_LOGF:
1504 case BUILT_IN_LOGL:
1505 fcode = BUILT_IN_LOG;
1506 break;
1507 case BUILT_IN_TAN:
1508 case BUILT_IN_TANF:
1509 case BUILT_IN_TANL:
1510 fcode = BUILT_IN_TAN;
1511 break;
1512 case BUILT_IN_ATAN:
1513 case BUILT_IN_ATANF:
1514 case BUILT_IN_ATANL:
1515 fcode = BUILT_IN_ATAN;
1516 break;
1517 case BUILT_IN_FLOOR:
1518 case BUILT_IN_FLOORF:
1519 case BUILT_IN_FLOORL:
1520 fcode = BUILT_IN_FLOOR;
1521 break;
1522 case BUILT_IN_CEIL:
1523 case BUILT_IN_CEILF:
1524 case BUILT_IN_CEILL:
1525 fcode = BUILT_IN_CEIL;
1526 break;
1527 case BUILT_IN_TRUNC:
1528 case BUILT_IN_TRUNCF:
1529 case BUILT_IN_TRUNCL:
1530 fcode = BUILT_IN_TRUNC;
1531 break;
1532 case BUILT_IN_ROUND:
1533 case BUILT_IN_ROUNDF:
1534 case BUILT_IN_ROUNDL:
1535 fcode = BUILT_IN_ROUND;
1536 break;
1537 case BUILT_IN_NEARBYINT:
1538 case BUILT_IN_NEARBYINTF:
1539 case BUILT_IN_NEARBYINTL:
1540 fcode = BUILT_IN_NEARBYINT;
1541 break;
1542 default:
1543 abort ();
1545 else if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
1546 switch (fn)
1548 case BUILT_IN_SQRT:
1549 case BUILT_IN_SQRTF:
1550 case BUILT_IN_SQRTL:
1551 fcode = BUILT_IN_SQRTF;
1552 break;
1553 case BUILT_IN_SIN:
1554 case BUILT_IN_SINF:
1555 case BUILT_IN_SINL:
1556 fcode = BUILT_IN_SINF;
1557 break;
1558 case BUILT_IN_COS:
1559 case BUILT_IN_COSF:
1560 case BUILT_IN_COSL:
1561 fcode = BUILT_IN_COSF;
1562 break;
1563 case BUILT_IN_EXP:
1564 case BUILT_IN_EXPF:
1565 case BUILT_IN_EXPL:
1566 fcode = BUILT_IN_EXPF;
1567 break;
1568 case BUILT_IN_LOG:
1569 case BUILT_IN_LOGF:
1570 case BUILT_IN_LOGL:
1571 fcode = BUILT_IN_LOGF;
1572 break;
1573 case BUILT_IN_TAN:
1574 case BUILT_IN_TANF:
1575 case BUILT_IN_TANL:
1576 fcode = BUILT_IN_TANF;
1577 break;
1578 case BUILT_IN_ATAN:
1579 case BUILT_IN_ATANF:
1580 case BUILT_IN_ATANL:
1581 fcode = BUILT_IN_ATANF;
1582 break;
1583 case BUILT_IN_FLOOR:
1584 case BUILT_IN_FLOORF:
1585 case BUILT_IN_FLOORL:
1586 fcode = BUILT_IN_FLOORF;
1587 break;
1588 case BUILT_IN_CEIL:
1589 case BUILT_IN_CEILF:
1590 case BUILT_IN_CEILL:
1591 fcode = BUILT_IN_CEILF;
1592 break;
1593 case BUILT_IN_TRUNC:
1594 case BUILT_IN_TRUNCF:
1595 case BUILT_IN_TRUNCL:
1596 fcode = BUILT_IN_TRUNCF;
1597 break;
1598 case BUILT_IN_ROUND:
1599 case BUILT_IN_ROUNDF:
1600 case BUILT_IN_ROUNDL:
1601 fcode = BUILT_IN_ROUNDF;
1602 break;
1603 case BUILT_IN_NEARBYINT:
1604 case BUILT_IN_NEARBYINTF:
1605 case BUILT_IN_NEARBYINTL:
1606 fcode = BUILT_IN_NEARBYINTF;
1607 break;
1608 default:
1609 abort ();
1611 else if (TYPE_MODE (type) == TYPE_MODE (long_double_type_node))
1612 switch (fn)
1614 case BUILT_IN_SQRT:
1615 case BUILT_IN_SQRTF:
1616 case BUILT_IN_SQRTL:
1617 fcode = BUILT_IN_SQRTL;
1618 break;
1619 case BUILT_IN_SIN:
1620 case BUILT_IN_SINF:
1621 case BUILT_IN_SINL:
1622 fcode = BUILT_IN_SINL;
1623 break;
1624 case BUILT_IN_COS:
1625 case BUILT_IN_COSF:
1626 case BUILT_IN_COSL:
1627 fcode = BUILT_IN_COSL;
1628 break;
1629 case BUILT_IN_EXP:
1630 case BUILT_IN_EXPF:
1631 case BUILT_IN_EXPL:
1632 fcode = BUILT_IN_EXPL;
1633 break;
1634 case BUILT_IN_LOG:
1635 case BUILT_IN_LOGF:
1636 case BUILT_IN_LOGL:
1637 fcode = BUILT_IN_LOGL;
1638 break;
1639 case BUILT_IN_TAN:
1640 case BUILT_IN_TANF:
1641 case BUILT_IN_TANL:
1642 fcode = BUILT_IN_TANL;
1643 break;
1644 case BUILT_IN_ATAN:
1645 case BUILT_IN_ATANF:
1646 case BUILT_IN_ATANL:
1647 fcode = BUILT_IN_ATANL;
1648 break;
1649 case BUILT_IN_FLOOR:
1650 case BUILT_IN_FLOORF:
1651 case BUILT_IN_FLOORL:
1652 fcode = BUILT_IN_FLOORL;
1653 break;
1654 case BUILT_IN_CEIL:
1655 case BUILT_IN_CEILF:
1656 case BUILT_IN_CEILL:
1657 fcode = BUILT_IN_CEILL;
1658 break;
1659 case BUILT_IN_TRUNC:
1660 case BUILT_IN_TRUNCF:
1661 case BUILT_IN_TRUNCL:
1662 fcode = BUILT_IN_TRUNCL;
1663 break;
1664 case BUILT_IN_ROUND:
1665 case BUILT_IN_ROUNDF:
1666 case BUILT_IN_ROUNDL:
1667 fcode = BUILT_IN_ROUNDL;
1668 break;
1669 case BUILT_IN_NEARBYINT:
1670 case BUILT_IN_NEARBYINTF:
1671 case BUILT_IN_NEARBYINTL:
1672 fcode = BUILT_IN_NEARBYINTL;
1673 break;
1674 default:
1675 abort ();
1677 return implicit_built_in_decls[fcode];
1680 /* If errno must be maintained, expand the RTL to check if the result,
1681 TARGET, of a built-in function call, EXP, is NaN, and if so set
1682 errno to EDOM. */
1684 static void
1685 expand_errno_check (tree exp, rtx target)
1687 rtx lab = gen_label_rtx ();
1689 /* Test the result; if it is NaN, set errno=EDOM because
1690 the argument was not in the domain. */
1691 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1692 0, lab);
1694 #ifdef TARGET_EDOM
1695 /* If this built-in doesn't throw an exception, set errno directly. */
1696 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1698 #ifdef GEN_ERRNO_RTX
1699 rtx errno_rtx = GEN_ERRNO_RTX;
1700 #else
1701 rtx errno_rtx
1702 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1703 #endif
1704 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1705 emit_label (lab);
1706 return;
1708 #endif
1710 /* We can't set errno=EDOM directly; let the library call do it.
1711 Pop the arguments right away in case the call gets deleted. */
1712 NO_DEFER_POP;
1713 expand_call (exp, target, 0);
1714 OK_DEFER_POP;
1715 emit_label (lab);
1719 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1720 Return 0 if a normal call should be emitted rather than expanding the
1721 function in-line. EXP is the expression that is a call to the builtin
1722 function; if convenient, the result should be placed in TARGET.
1723 SUBTARGET may be used as the target for computing one of EXP's operands. */
1725 static rtx
1726 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1728 optab builtin_optab;
1729 rtx op0, insns;
1730 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1731 tree arglist = TREE_OPERAND (exp, 1);
1732 enum machine_mode mode;
1733 bool errno_set = false;
1734 tree arg, narg;
1736 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1737 return 0;
1739 arg = TREE_VALUE (arglist);
1741 switch (DECL_FUNCTION_CODE (fndecl))
1743 case BUILT_IN_SIN:
1744 case BUILT_IN_SINF:
1745 case BUILT_IN_SINL:
1746 builtin_optab = sin_optab; break;
1747 case BUILT_IN_COS:
1748 case BUILT_IN_COSF:
1749 case BUILT_IN_COSL:
1750 builtin_optab = cos_optab; break;
1751 case BUILT_IN_SQRT:
1752 case BUILT_IN_SQRTF:
1753 case BUILT_IN_SQRTL:
1754 errno_set = ! tree_expr_nonnegative_p (arg);
1755 builtin_optab = sqrt_optab;
1756 break;
1757 case BUILT_IN_EXP:
1758 case BUILT_IN_EXPF:
1759 case BUILT_IN_EXPL:
1760 errno_set = true; builtin_optab = exp_optab; break;
1761 case BUILT_IN_LOG:
1762 case BUILT_IN_LOGF:
1763 case BUILT_IN_LOGL:
1764 errno_set = true; builtin_optab = log_optab; break;
1765 case BUILT_IN_TAN:
1766 case BUILT_IN_TANF:
1767 case BUILT_IN_TANL:
1768 builtin_optab = tan_optab; break;
1769 case BUILT_IN_ATAN:
1770 case BUILT_IN_ATANF:
1771 case BUILT_IN_ATANL:
1772 builtin_optab = atan_optab; break;
1773 case BUILT_IN_FLOOR:
1774 case BUILT_IN_FLOORF:
1775 case BUILT_IN_FLOORL:
1776 builtin_optab = floor_optab; break;
1777 case BUILT_IN_CEIL:
1778 case BUILT_IN_CEILF:
1779 case BUILT_IN_CEILL:
1780 builtin_optab = ceil_optab; break;
1781 case BUILT_IN_TRUNC:
1782 case BUILT_IN_TRUNCF:
1783 case BUILT_IN_TRUNCL:
1784 builtin_optab = trunc_optab; break;
1785 case BUILT_IN_ROUND:
1786 case BUILT_IN_ROUNDF:
1787 case BUILT_IN_ROUNDL:
1788 builtin_optab = round_optab; break;
1789 case BUILT_IN_NEARBYINT:
1790 case BUILT_IN_NEARBYINTF:
1791 case BUILT_IN_NEARBYINTL:
1792 builtin_optab = nearbyint_optab; break;
1793 default:
1794 abort ();
1797 /* Make a suitable register to place result in. */
1798 mode = TYPE_MODE (TREE_TYPE (exp));
1800 /* Before working hard, check whether the instruction is available. */
1801 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1802 return 0;
1803 target = gen_reg_rtx (mode);
1805 if (! flag_errno_math || ! HONOR_NANS (mode))
1806 errno_set = false;
1808 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1809 need to expand the argument again. This way, we will not perform
1810 side-effects more the once. */
1811 narg = save_expr (arg);
1812 if (narg != arg)
1814 arglist = build_tree_list (NULL_TREE, arg);
1815 exp = build_function_call_expr (fndecl, arglist);
1818 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1820 emit_queue ();
1821 start_sequence ();
1823 /* Compute into TARGET.
1824 Set TARGET to wherever the result comes back. */
1825 target = expand_unop (mode, builtin_optab, op0, target, 0);
1827 /* If we were unable to expand via the builtin, stop the sequence
1828 (without outputting the insns) and call to the library function
1829 with the stabilized argument list. */
1830 if (target == 0)
1832 end_sequence ();
1833 return expand_call (exp, target, target == const0_rtx);
1836 if (errno_set)
1837 expand_errno_check (exp, target);
1839 /* Output the entire sequence. */
1840 insns = get_insns ();
1841 end_sequence ();
1842 emit_insn (insns);
1844 return target;
1847 /* Expand a call to the builtin binary math functions (pow and atan2).
1848 Return 0 if a normal call should be emitted rather than expanding the
1849 function in-line. EXP is the expression that is a call to the builtin
1850 function; if convenient, the result should be placed in TARGET.
1851 SUBTARGET may be used as the target for computing one of EXP's
1852 operands. */
1854 static rtx
1855 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1857 optab builtin_optab;
1858 rtx op0, op1, insns;
1859 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1860 tree arglist = TREE_OPERAND (exp, 1);
1861 tree arg0, arg1, temp, narg;
1862 enum machine_mode mode;
1863 bool errno_set = true;
1864 bool stable = true;
1866 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1867 return 0;
1869 arg0 = TREE_VALUE (arglist);
1870 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1872 switch (DECL_FUNCTION_CODE (fndecl))
1874 case BUILT_IN_POW:
1875 case BUILT_IN_POWF:
1876 case BUILT_IN_POWL:
1877 builtin_optab = pow_optab; break;
1878 case BUILT_IN_ATAN2:
1879 case BUILT_IN_ATAN2F:
1880 case BUILT_IN_ATAN2L:
1881 builtin_optab = atan2_optab; break;
1882 default:
1883 abort ();
1886 /* Make a suitable register to place result in. */
1887 mode = TYPE_MODE (TREE_TYPE (exp));
1889 /* Before working hard, check whether the instruction is available. */
1890 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1891 return 0;
1893 target = gen_reg_rtx (mode);
1895 if (! flag_errno_math || ! HONOR_NANS (mode))
1896 errno_set = false;
1898 /* Alway stabilize the argument list. */
1899 narg = save_expr (arg1);
1900 if (narg != arg1)
1902 temp = build_tree_list (NULL_TREE, narg);
1903 stable = false;
1905 else
1906 temp = TREE_CHAIN (arglist);
1908 narg = save_expr (arg0);
1909 if (narg != arg0)
1911 arglist = tree_cons (NULL_TREE, narg, temp);
1912 stable = false;
1914 else if (! stable)
1915 arglist = tree_cons (NULL_TREE, arg0, temp);
1917 if (! stable)
1918 exp = build_function_call_expr (fndecl, arglist);
1920 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1921 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1923 emit_queue ();
1924 start_sequence ();
1926 /* Compute into TARGET.
1927 Set TARGET to wherever the result comes back. */
1928 target = expand_binop (mode, builtin_optab, op0, op1,
1929 target, 0, OPTAB_DIRECT);
1931 /* If we were unable to expand via the builtin, stop the sequence
1932 (without outputting the insns) and call to the library function
1933 with the stabilized argument list. */
1934 if (target == 0)
1936 end_sequence ();
1937 return expand_call (exp, target, target == const0_rtx);
1940 if (errno_set)
1941 expand_errno_check (exp, target);
1943 /* Output the entire sequence. */
1944 insns = get_insns ();
1945 end_sequence ();
1946 emit_insn (insns);
1948 return target;
1951 /* To evaluate powi(x,n), the floating point value x raised to the
1952 constant integer exponent n, we use a hybrid algorithm that
1953 combines the "window method" with look-up tables. For an
1954 introduction to exponentiation algorithms and "addition chains",
1955 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1956 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1957 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1958 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1960 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
1961 multiplications to inline before calling the system library's pow
1962 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1963 so this default never requires calling pow, powf or powl. */
1965 #ifndef POWI_MAX_MULTS
1966 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1967 #endif
1969 /* The size of the "optimal power tree" lookup table. All
1970 exponents less than this value are simply looked up in the
1971 powi_table below. This threshold is also used to size the
1972 cache of pseudo registers that hold intermediate results. */
1973 #define POWI_TABLE_SIZE 256
1975 /* The size, in bits of the window, used in the "window method"
1976 exponentiation algorithm. This is equivalent to a radix of
1977 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1978 #define POWI_WINDOW_SIZE 3
1980 /* The following table is an efficient representation of an
1981 "optimal power tree". For each value, i, the corresponding
1982 value, j, in the table states than an optimal evaluation
1983 sequence for calculating pow(x,i) can be found by evaluating
1984 pow(x,j)*pow(x,i-j). An optimal power tree for the first
1985 100 integers is given in Knuth's "Seminumerical algorithms". */
1987 static const unsigned char powi_table[POWI_TABLE_SIZE] =
1989 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1990 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1991 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1992 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1993 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1994 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1995 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1996 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1997 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1998 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1999 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2000 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2001 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2002 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2003 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2004 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2005 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2006 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2007 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2008 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2009 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2010 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2011 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2012 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2013 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2014 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2015 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2016 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2017 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2018 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2019 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2020 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2024 /* Return the number of multiplications required to calculate
2025 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2026 subroutine of powi_cost. CACHE is an array indicating
2027 which exponents have already been calculated. */
2029 static int
2030 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2032 /* If we've already calculated this exponent, then this evaluation
2033 doesn't require any additional multiplications. */
2034 if (cache[n])
2035 return 0;
2037 cache[n] = true;
2038 return powi_lookup_cost (n - powi_table[n], cache)
2039 + powi_lookup_cost (powi_table[n], cache) + 1;
2042 /* Return the number of multiplications required to calculate
2043 powi(x,n) for an arbitrary x, given the exponent N. This
2044 function needs to be kept in sync with expand_powi below. */
2046 static int
2047 powi_cost (HOST_WIDE_INT n)
2049 bool cache[POWI_TABLE_SIZE];
2050 unsigned HOST_WIDE_INT digit;
2051 unsigned HOST_WIDE_INT val;
2052 int result;
2054 if (n == 0)
2055 return 0;
2057 /* Ignore the reciprocal when calculating the cost. */
2058 val = (n < 0) ? -n : n;
2060 /* Initialize the exponent cache. */
2061 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2062 cache[1] = true;
2064 result = 0;
2066 while (val >= POWI_TABLE_SIZE)
2068 if (val & 1)
2070 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2071 result += powi_lookup_cost (digit, cache)
2072 + POWI_WINDOW_SIZE + 1;
2073 val >>= POWI_WINDOW_SIZE;
2075 else
2077 val >>= 1;
2078 result++;
2082 return result + powi_lookup_cost (val, cache);
2085 /* Recursive subroutine of expand_powi. This function takes the array,
2086 CACHE, of already calculated exponents and an exponent N and returns
2087 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2089 static rtx
2090 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2092 unsigned HOST_WIDE_INT digit;
2093 rtx target, result;
2094 rtx op0, op1;
2096 if (n < POWI_TABLE_SIZE)
2098 if (cache[n])
2099 return cache[n];
2101 target = gen_reg_rtx (mode);
2102 cache[n] = target;
2104 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2105 op1 = expand_powi_1 (mode, powi_table[n], cache);
2107 else if (n & 1)
2109 target = gen_reg_rtx (mode);
2110 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2111 op0 = expand_powi_1 (mode, n - digit, cache);
2112 op1 = expand_powi_1 (mode, digit, cache);
2114 else
2116 target = gen_reg_rtx (mode);
2117 op0 = expand_powi_1 (mode, n >> 1, cache);
2118 op1 = op0;
2121 result = expand_mult (mode, op0, op1, target, 0);
2122 if (result != target)
2123 emit_move_insn (target, result);
2124 return target;
2127 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2128 floating point operand in mode MODE, and N is the exponent. This
2129 function needs to be kept in sync with powi_cost above. */
2131 static rtx
2132 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2134 unsigned HOST_WIDE_INT val;
2135 rtx cache[POWI_TABLE_SIZE];
2136 rtx result;
2138 if (n == 0)
2139 return CONST1_RTX (mode);
2141 val = (n < 0) ? -n : n;
2143 memset (cache, 0, sizeof (cache));
2144 cache[1] = x;
2146 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2148 /* If the original exponent was negative, reciprocate the result. */
2149 if (n < 0)
2150 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2151 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2153 return result;
2156 /* Expand a call to the pow built-in mathematical function. Return 0 if
2157 a normal call should be emitted rather than expanding the function
2158 in-line. EXP is the expression that is a call to the builtin
2159 function; if convenient, the result should be placed in TARGET. */
2161 static rtx
2162 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2164 tree arglist = TREE_OPERAND (exp, 1);
2165 tree arg0, arg1;
2167 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2168 return 0;
2170 arg0 = TREE_VALUE (arglist);
2171 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2173 if (flag_unsafe_math_optimizations
2174 && ! flag_errno_math
2175 && ! optimize_size
2176 && TREE_CODE (arg1) == REAL_CST
2177 && ! TREE_CONSTANT_OVERFLOW (arg1))
2179 REAL_VALUE_TYPE cint;
2180 REAL_VALUE_TYPE c;
2181 HOST_WIDE_INT n;
2183 c = TREE_REAL_CST (arg1);
2184 n = real_to_integer (&c);
2185 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2186 if (real_identical (&c, &cint)
2187 && powi_cost (n) <= POWI_MAX_MULTS)
2189 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2190 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2191 op = force_reg (mode, op);
2192 return expand_powi (op, mode, n);
2195 return expand_builtin_mathfn_2 (exp, target, NULL_RTX);
2198 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2199 if we failed the caller should emit a normal call, otherwise
2200 try to get the result in TARGET, if convenient. */
2202 static rtx
2203 expand_builtin_strlen (tree arglist, rtx target,
2204 enum machine_mode target_mode)
2206 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2207 return 0;
2208 else
2210 rtx pat;
2211 tree len, src = TREE_VALUE (arglist);
2212 rtx result, src_reg, char_rtx, before_strlen;
2213 enum machine_mode insn_mode = target_mode, char_mode;
2214 enum insn_code icode = CODE_FOR_nothing;
2215 int align;
2217 /* If the length can be computed at compile-time, return it. */
2218 len = c_strlen (src, 0);
2219 if (len)
2220 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2222 /* If the length can be computed at compile-time and is constant
2223 integer, but there are side-effects in src, evaluate
2224 src for side-effects, then return len.
2225 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2226 can be optimized into: i++; x = 3; */
2227 len = c_strlen (src, 1);
2228 if (len && TREE_CODE (len) == INTEGER_CST)
2230 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2231 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2234 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2236 /* If SRC is not a pointer type, don't do this operation inline. */
2237 if (align == 0)
2238 return 0;
2240 /* Bail out if we can't compute strlen in the right mode. */
2241 while (insn_mode != VOIDmode)
2243 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2244 if (icode != CODE_FOR_nothing)
2245 break;
2247 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2249 if (insn_mode == VOIDmode)
2250 return 0;
2252 /* Make a place to write the result of the instruction. */
2253 result = target;
2254 if (! (result != 0
2255 && GET_CODE (result) == REG
2256 && GET_MODE (result) == insn_mode
2257 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2258 result = gen_reg_rtx (insn_mode);
2260 /* Make a place to hold the source address. We will not expand
2261 the actual source until we are sure that the expansion will
2262 not fail -- there are trees that cannot be expanded twice. */
2263 src_reg = gen_reg_rtx (Pmode);
2265 /* Mark the beginning of the strlen sequence so we can emit the
2266 source operand later. */
2267 before_strlen = get_last_insn ();
2269 char_rtx = const0_rtx;
2270 char_mode = insn_data[(int) icode].operand[2].mode;
2271 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2272 char_mode))
2273 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2275 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2276 char_rtx, GEN_INT (align));
2277 if (! pat)
2278 return 0;
2279 emit_insn (pat);
2281 /* Now that we are assured of success, expand the source. */
2282 start_sequence ();
2283 pat = memory_address (BLKmode,
2284 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
2285 if (pat != src_reg)
2286 emit_move_insn (src_reg, pat);
2287 pat = get_insns ();
2288 end_sequence ();
2290 if (before_strlen)
2291 emit_insn_after (pat, before_strlen);
2292 else
2293 emit_insn_before (pat, get_insns ());
2295 /* Return the value in the proper mode for this function. */
2296 if (GET_MODE (result) == target_mode)
2297 target = result;
2298 else if (target != 0)
2299 convert_move (target, result, 0);
2300 else
2301 target = convert_to_mode (target_mode, result, 0);
2303 return target;
2307 /* Expand a call to the strstr builtin. Return 0 if we failed the
2308 caller should emit a normal call, otherwise try to get the result
2309 in TARGET, if convenient (and in mode MODE if that's convenient). */
2311 static rtx
2312 expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
2314 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2315 return 0;
2316 else
2318 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2319 tree fn;
2320 const char *p1, *p2;
2322 p2 = c_getstr (s2);
2323 if (p2 == NULL)
2324 return 0;
2326 p1 = c_getstr (s1);
2327 if (p1 != NULL)
2329 const char *r = strstr (p1, p2);
2331 if (r == NULL)
2332 return const0_rtx;
2334 /* Return an offset into the constant string argument. */
2335 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2336 s1, ssize_int (r - p1))),
2337 target, mode, EXPAND_NORMAL);
2340 if (p2[0] == '\0')
2341 return expand_expr (s1, target, mode, EXPAND_NORMAL);
2343 if (p2[1] != '\0')
2344 return 0;
2346 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2347 if (!fn)
2348 return 0;
2350 /* New argument list transforming strstr(s1, s2) to
2351 strchr(s1, s2[0]). */
2352 arglist =
2353 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2354 arglist = tree_cons (NULL_TREE, s1, arglist);
2355 return expand_expr (build_function_call_expr (fn, arglist),
2356 target, mode, EXPAND_NORMAL);
2360 /* Expand a call to the strchr builtin. Return 0 if we failed the
2361 caller should emit a normal call, otherwise try to get the result
2362 in TARGET, if convenient (and in mode MODE if that's convenient). */
2364 static rtx
2365 expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
2367 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2368 return 0;
2369 else
2371 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2372 const char *p1;
2374 if (TREE_CODE (s2) != INTEGER_CST)
2375 return 0;
2377 p1 = c_getstr (s1);
2378 if (p1 != NULL)
2380 char c;
2381 const char *r;
2383 if (target_char_cast (s2, &c))
2384 return 0;
2386 r = strchr (p1, c);
2388 if (r == NULL)
2389 return const0_rtx;
2391 /* Return an offset into the constant string argument. */
2392 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2393 s1, ssize_int (r - p1))),
2394 target, mode, EXPAND_NORMAL);
2397 /* FIXME: Should use here strchrM optab so that ports can optimize
2398 this. */
2399 return 0;
2403 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2404 caller should emit a normal call, otherwise try to get the result
2405 in TARGET, if convenient (and in mode MODE if that's convenient). */
2407 static rtx
2408 expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode)
2410 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2411 return 0;
2412 else
2414 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2415 tree fn;
2416 const char *p1;
2418 if (TREE_CODE (s2) != INTEGER_CST)
2419 return 0;
2421 p1 = c_getstr (s1);
2422 if (p1 != NULL)
2424 char c;
2425 const char *r;
2427 if (target_char_cast (s2, &c))
2428 return 0;
2430 r = strrchr (p1, c);
2432 if (r == NULL)
2433 return const0_rtx;
2435 /* Return an offset into the constant string argument. */
2436 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2437 s1, ssize_int (r - p1))),
2438 target, mode, EXPAND_NORMAL);
2441 if (! integer_zerop (s2))
2442 return 0;
2444 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2445 if (!fn)
2446 return 0;
2448 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
2449 return expand_expr (build_function_call_expr (fn, arglist),
2450 target, mode, EXPAND_NORMAL);
2454 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2455 caller should emit a normal call, otherwise try to get the result
2456 in TARGET, if convenient (and in mode MODE if that's convenient). */
2458 static rtx
2459 expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
2461 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2462 return 0;
2463 else
2465 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2466 tree fn;
2467 const char *p1, *p2;
2469 p2 = c_getstr (s2);
2470 if (p2 == NULL)
2471 return 0;
2473 p1 = c_getstr (s1);
2474 if (p1 != NULL)
2476 const char *r = strpbrk (p1, p2);
2478 if (r == NULL)
2479 return const0_rtx;
2481 /* Return an offset into the constant string argument. */
2482 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2483 s1, ssize_int (r - p1))),
2484 target, mode, EXPAND_NORMAL);
2487 if (p2[0] == '\0')
2489 /* strpbrk(x, "") == NULL.
2490 Evaluate and ignore the arguments in case they had
2491 side-effects. */
2492 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2493 return const0_rtx;
2496 if (p2[1] != '\0')
2497 return 0; /* Really call strpbrk. */
2499 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2500 if (!fn)
2501 return 0;
2503 /* New argument list transforming strpbrk(s1, s2) to
2504 strchr(s1, s2[0]). */
2505 arglist =
2506 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2507 arglist = tree_cons (NULL_TREE, s1, arglist);
2508 return expand_expr (build_function_call_expr (fn, arglist),
2509 target, mode, EXPAND_NORMAL);
2513 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2514 bytes from constant string DATA + OFFSET and return it as target
2515 constant. */
2517 static rtx
2518 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2519 enum machine_mode mode)
2521 const char *str = (const char *) data;
2523 if (offset < 0
2524 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2525 > strlen (str) + 1))
2526 abort (); /* Attempt to read past the end of constant string. */
2528 return c_readstr (str + offset, mode);
2531 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2532 Return 0 if we failed, the caller should emit a normal call,
2533 otherwise try to get the result in TARGET, if convenient (and in
2534 mode MODE if that's convenient). */
2535 static rtx
2536 expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode)
2538 if (!validate_arglist (arglist,
2539 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2540 return 0;
2541 else
2543 tree dest = TREE_VALUE (arglist);
2544 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2545 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2546 const char *src_str;
2547 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2548 unsigned int dest_align
2549 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2550 rtx dest_mem, src_mem, dest_addr, len_rtx;
2552 /* If DEST is not a pointer type, call the normal function. */
2553 if (dest_align == 0)
2554 return 0;
2556 /* If the LEN parameter is zero, return DEST. */
2557 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2559 /* Evaluate and ignore SRC in case it has side-effects. */
2560 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2561 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2564 /* If either SRC is not a pointer type, don't do this
2565 operation in-line. */
2566 if (src_align == 0)
2567 return 0;
2569 dest_mem = get_memory_rtx (dest);
2570 set_mem_align (dest_mem, dest_align);
2571 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2572 src_str = c_getstr (src);
2574 /* If SRC is a string constant and block move would be done
2575 by pieces, we can avoid loading the string from memory
2576 and only stored the computed constants. */
2577 if (src_str
2578 && GET_CODE (len_rtx) == CONST_INT
2579 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2580 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2581 (void *) src_str, dest_align))
2583 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2584 builtin_memcpy_read_str,
2585 (void *) src_str, dest_align, 0);
2586 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2587 #ifdef POINTERS_EXTEND_UNSIGNED
2588 if (GET_MODE (dest_mem) != ptr_mode)
2589 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2590 #endif
2591 return dest_mem;
2594 src_mem = get_memory_rtx (src);
2595 set_mem_align (src_mem, src_align);
2597 /* Copy word part most expediently. */
2598 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2599 BLOCK_OP_NORMAL);
2601 if (dest_addr == 0)
2603 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2604 #ifdef POINTERS_EXTEND_UNSIGNED
2605 if (GET_MODE (dest_addr) != ptr_mode)
2606 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2607 #endif
2609 return dest_addr;
2613 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2614 Return 0 if we failed the caller should emit a normal call,
2615 otherwise try to get the result in TARGET, if convenient (and in
2616 mode MODE if that's convenient). If ENDP is 0 return the
2617 destination pointer, if ENDP is 1 return the end pointer ala
2618 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2619 stpcpy. */
2621 static rtx
2622 expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
2623 int endp)
2625 if (!validate_arglist (arglist,
2626 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2627 return 0;
2628 /* If return value is ignored, transform mempcpy into memcpy. */
2629 else if (target == const0_rtx)
2631 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2633 if (!fn)
2634 return 0;
2636 return expand_expr (build_function_call_expr (fn, arglist),
2637 target, mode, EXPAND_NORMAL);
2639 else
2641 tree dest = TREE_VALUE (arglist);
2642 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2643 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2644 const char *src_str;
2645 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2646 unsigned int dest_align
2647 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2648 rtx dest_mem, src_mem, len_rtx;
2650 /* If DEST is not a pointer type or LEN is not constant,
2651 call the normal function. */
2652 if (dest_align == 0 || !host_integerp (len, 1))
2653 return 0;
2655 /* If the LEN parameter is zero, return DEST. */
2656 if (tree_low_cst (len, 1) == 0)
2658 /* Evaluate and ignore SRC in case it has side-effects. */
2659 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2660 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2663 /* If either SRC is not a pointer type, don't do this
2664 operation in-line. */
2665 if (src_align == 0)
2666 return 0;
2668 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2669 src_str = c_getstr (src);
2671 /* If SRC is a string constant and block move would be done
2672 by pieces, we can avoid loading the string from memory
2673 and only stored the computed constants. */
2674 if (src_str
2675 && GET_CODE (len_rtx) == CONST_INT
2676 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2677 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2678 (void *) src_str, dest_align))
2680 dest_mem = get_memory_rtx (dest);
2681 set_mem_align (dest_mem, dest_align);
2682 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2683 builtin_memcpy_read_str,
2684 (void *) src_str, dest_align, endp);
2685 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2686 #ifdef POINTERS_EXTEND_UNSIGNED
2687 if (GET_MODE (dest_mem) != ptr_mode)
2688 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2689 #endif
2690 return dest_mem;
2693 if (GET_CODE (len_rtx) == CONST_INT
2694 && can_move_by_pieces (INTVAL (len_rtx),
2695 MIN (dest_align, src_align)))
2697 dest_mem = get_memory_rtx (dest);
2698 set_mem_align (dest_mem, dest_align);
2699 src_mem = get_memory_rtx (src);
2700 set_mem_align (src_mem, src_align);
2701 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2702 MIN (dest_align, src_align), endp);
2703 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2704 #ifdef POINTERS_EXTEND_UNSIGNED
2705 if (GET_MODE (dest_mem) != ptr_mode)
2706 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2707 #endif
2708 return dest_mem;
2711 return 0;
2715 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2716 if we failed the caller should emit a normal call. */
2718 static rtx
2719 expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode)
2721 if (!validate_arglist (arglist,
2722 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2723 return 0;
2724 else
2726 tree dest = TREE_VALUE (arglist);
2727 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2728 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2730 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2731 unsigned int dest_align
2732 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2734 /* If DEST is not a pointer type, call the normal function. */
2735 if (dest_align == 0)
2736 return 0;
2738 /* If the LEN parameter is zero, return DEST. */
2739 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2741 /* Evaluate and ignore SRC in case it has side-effects. */
2742 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2743 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2746 /* If either SRC is not a pointer type, don't do this
2747 operation in-line. */
2748 if (src_align == 0)
2749 return 0;
2751 /* If src is categorized for a readonly section we can use
2752 normal memcpy. */
2753 if (readonly_data_expr (src))
2755 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2756 if (!fn)
2757 return 0;
2758 return expand_expr (build_function_call_expr (fn, arglist),
2759 target, mode, EXPAND_NORMAL);
2762 /* Otherwise, call the normal function. */
2763 return 0;
2767 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2768 if we failed the caller should emit a normal call. */
2770 static rtx
2771 expand_builtin_bcopy (tree arglist)
2773 tree src, dest, size, newarglist;
2775 if (!validate_arglist (arglist,
2776 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2777 return NULL_RTX;
2779 src = TREE_VALUE (arglist);
2780 dest = TREE_VALUE (TREE_CHAIN (arglist));
2781 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2783 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2784 memmove(ptr y, ptr x, size_t z). This is done this way
2785 so that if it isn't expanded inline, we fallback to
2786 calling bcopy instead of memmove. */
2788 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2789 newarglist = tree_cons (NULL_TREE, src, newarglist);
2790 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2792 return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode);
2795 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2796 if we failed the caller should emit a normal call, otherwise try to get
2797 the result in TARGET, if convenient (and in mode MODE if that's
2798 convenient). */
2800 static rtx
2801 expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode)
2803 tree fn, len, src, dst;
2805 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2806 return 0;
2808 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2809 if (!fn)
2810 return 0;
2812 src = TREE_VALUE (TREE_CHAIN (arglist));
2813 len = c_strlen (src, 1);
2814 if (len == 0 || TREE_SIDE_EFFECTS (len))
2815 return 0;
2817 dst = TREE_VALUE (arglist);
2818 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2819 arglist = build_tree_list (NULL_TREE, len);
2820 arglist = tree_cons (NULL_TREE, src, arglist);
2821 arglist = tree_cons (NULL_TREE, dst, arglist);
2822 return expand_expr (build_function_call_expr (fn, arglist),
2823 target, mode, EXPAND_NORMAL);
2826 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2827 Return 0 if we failed the caller should emit a normal call,
2828 otherwise try to get the result in TARGET, if convenient (and in
2829 mode MODE if that's convenient). */
2831 static rtx
2832 expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode)
2834 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2835 return 0;
2836 else
2838 tree dst, src, len;
2840 /* If return value is ignored, transform stpcpy into strcpy. */
2841 if (target == const0_rtx)
2843 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2844 if (!fn)
2845 return 0;
2847 return expand_expr (build_function_call_expr (fn, arglist),
2848 target, mode, EXPAND_NORMAL);
2851 /* Ensure we get an actual string whose length can be evaluated at
2852 compile-time, not an expression containing a string. This is
2853 because the latter will potentially produce pessimized code
2854 when used to produce the return value. */
2855 src = TREE_VALUE (TREE_CHAIN (arglist));
2856 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2857 return 0;
2859 dst = TREE_VALUE (arglist);
2860 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2861 arglist = build_tree_list (NULL_TREE, len);
2862 arglist = tree_cons (NULL_TREE, src, arglist);
2863 arglist = tree_cons (NULL_TREE, dst, arglist);
2864 return expand_builtin_mempcpy (arglist, target, mode, /*endp=*/2);
2868 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2869 bytes from constant string DATA + OFFSET and return it as target
2870 constant. */
2872 static rtx
2873 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
2874 enum machine_mode mode)
2876 const char *str = (const char *) data;
2878 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2879 return const0_rtx;
2881 return c_readstr (str + offset, mode);
2884 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2885 if we failed the caller should emit a normal call. */
2887 static rtx
2888 expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode)
2890 if (!validate_arglist (arglist,
2891 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2892 return 0;
2893 else
2895 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
2896 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2897 tree fn;
2899 /* We must be passed a constant len parameter. */
2900 if (TREE_CODE (len) != INTEGER_CST)
2901 return 0;
2903 /* If the len parameter is zero, return the dst parameter. */
2904 if (integer_zerop (len))
2906 /* Evaluate and ignore the src argument in case it has
2907 side-effects. */
2908 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2909 VOIDmode, EXPAND_NORMAL);
2910 /* Return the dst parameter. */
2911 return expand_expr (TREE_VALUE (arglist), target, mode,
2912 EXPAND_NORMAL);
2915 /* Now, we must be passed a constant src ptr parameter. */
2916 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2917 return 0;
2919 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2921 /* We're required to pad with trailing zeros if the requested
2922 len is greater than strlen(s2)+1. In that case try to
2923 use store_by_pieces, if it fails, punt. */
2924 if (tree_int_cst_lt (slen, len))
2926 tree dest = TREE_VALUE (arglist);
2927 unsigned int dest_align
2928 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2929 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2930 rtx dest_mem;
2932 if (!p || dest_align == 0 || !host_integerp (len, 1)
2933 || !can_store_by_pieces (tree_low_cst (len, 1),
2934 builtin_strncpy_read_str,
2935 (void *) p, dest_align))
2936 return 0;
2938 dest_mem = get_memory_rtx (dest);
2939 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2940 builtin_strncpy_read_str,
2941 (void *) p, dest_align, 0);
2942 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2943 #ifdef POINTERS_EXTEND_UNSIGNED
2944 if (GET_MODE (dest_mem) != ptr_mode)
2945 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2946 #endif
2947 return dest_mem;
2950 /* OK transform into builtin memcpy. */
2951 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2952 if (!fn)
2953 return 0;
2954 return expand_expr (build_function_call_expr (fn, arglist),
2955 target, mode, EXPAND_NORMAL);
2959 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2960 bytes from constant string DATA + OFFSET and return it as target
2961 constant. */
2963 static rtx
2964 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2965 enum machine_mode mode)
2967 const char *c = (const char *) data;
2968 char *p = alloca (GET_MODE_SIZE (mode));
2970 memset (p, *c, GET_MODE_SIZE (mode));
2972 return c_readstr (p, mode);
2975 /* Callback routine for store_by_pieces. Return the RTL of a register
2976 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
2977 char value given in the RTL register data. For example, if mode is
2978 4 bytes wide, return the RTL for 0x01010101*data. */
2980 static rtx
2981 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2982 enum machine_mode mode)
2984 rtx target, coeff;
2985 size_t size;
2986 char *p;
2988 size = GET_MODE_SIZE (mode);
2989 if (size == 1)
2990 return (rtx) data;
2992 p = alloca (size);
2993 memset (p, 1, size);
2994 coeff = c_readstr (p, mode);
2996 target = convert_to_mode (mode, (rtx) data, 1);
2997 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
2998 return force_reg (mode, target);
3001 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3002 if we failed the caller should emit a normal call, otherwise try to get
3003 the result in TARGET, if convenient (and in mode MODE if that's
3004 convenient). */
3006 static rtx
3007 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3009 if (!validate_arglist (arglist,
3010 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3011 return 0;
3012 else
3014 tree dest = TREE_VALUE (arglist);
3015 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3016 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3017 char c;
3019 unsigned int dest_align
3020 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3021 rtx dest_mem, dest_addr, len_rtx;
3023 /* If DEST is not a pointer type, don't do this
3024 operation in-line. */
3025 if (dest_align == 0)
3026 return 0;
3028 /* If the LEN parameter is zero, return DEST. */
3029 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
3031 /* Evaluate and ignore VAL in case it has side-effects. */
3032 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3033 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3036 if (TREE_CODE (val) != INTEGER_CST)
3038 rtx val_rtx;
3040 if (!host_integerp (len, 1))
3041 return 0;
3043 if (optimize_size && tree_low_cst (len, 1) > 1)
3044 return 0;
3046 /* Assume that we can memset by pieces if we can store the
3047 * the coefficients by pieces (in the required modes).
3048 * We can't pass builtin_memset_gen_str as that emits RTL. */
3049 c = 1;
3050 if (!can_store_by_pieces (tree_low_cst (len, 1),
3051 builtin_memset_read_str,
3052 &c, dest_align))
3053 return 0;
3055 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3056 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3057 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3058 val_rtx);
3059 dest_mem = get_memory_rtx (dest);
3060 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3061 builtin_memset_gen_str,
3062 val_rtx, dest_align, 0);
3063 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3064 #ifdef POINTERS_EXTEND_UNSIGNED
3065 if (GET_MODE (dest_mem) != ptr_mode)
3066 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3067 #endif
3068 return dest_mem;
3071 if (target_char_cast (val, &c))
3072 return 0;
3074 if (c)
3076 if (!host_integerp (len, 1))
3077 return 0;
3078 if (!can_store_by_pieces (tree_low_cst (len, 1),
3079 builtin_memset_read_str, &c,
3080 dest_align))
3081 return 0;
3083 dest_mem = get_memory_rtx (dest);
3084 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3085 builtin_memset_read_str,
3086 &c, dest_align, 0);
3087 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3088 #ifdef POINTERS_EXTEND_UNSIGNED
3089 if (GET_MODE (dest_mem) != ptr_mode)
3090 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3091 #endif
3092 return dest_mem;
3095 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3097 dest_mem = get_memory_rtx (dest);
3098 set_mem_align (dest_mem, dest_align);
3099 dest_addr = clear_storage (dest_mem, len_rtx);
3101 if (dest_addr == 0)
3103 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3104 #ifdef POINTERS_EXTEND_UNSIGNED
3105 if (GET_MODE (dest_addr) != ptr_mode)
3106 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3107 #endif
3110 return dest_addr;
3114 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3115 if we failed the caller should emit a normal call. */
3117 static rtx
3118 expand_builtin_bzero (tree arglist)
3120 tree dest, size, newarglist;
3122 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3123 return NULL_RTX;
3125 dest = TREE_VALUE (arglist);
3126 size = TREE_VALUE (TREE_CHAIN (arglist));
3128 /* New argument list transforming bzero(ptr x, int y) to
3129 memset(ptr x, int 0, size_t y). This is done this way
3130 so that if it isn't expanded inline, we fallback to
3131 calling bzero instead of memset. */
3133 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
3134 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3135 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3137 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3140 /* Expand expression EXP, which is a call to the memcmp built-in function.
3141 ARGLIST is the argument list for this call. Return 0 if we failed and the
3142 caller should emit a normal call, otherwise try to get the result in
3143 TARGET, if convenient (and in mode MODE, if that's convenient). */
3145 static rtx
3146 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3147 enum machine_mode mode)
3149 tree arg1, arg2, len;
3150 const char *p1, *p2;
3152 if (!validate_arglist (arglist,
3153 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3154 return 0;
3156 arg1 = TREE_VALUE (arglist);
3157 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3158 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3160 /* If the len parameter is zero, return zero. */
3161 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
3163 /* Evaluate and ignore arg1 and arg2 in case they have
3164 side-effects. */
3165 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3166 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3167 return const0_rtx;
3170 p1 = c_getstr (arg1);
3171 p2 = c_getstr (arg2);
3173 /* If all arguments are constant, and the value of len is not greater
3174 than the lengths of arg1 and arg2, evaluate at compile-time. */
3175 if (host_integerp (len, 1) && p1 && p2
3176 && compare_tree_int (len, strlen (p1) + 1) <= 0
3177 && compare_tree_int (len, strlen (p2) + 1) <= 0)
3179 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
3181 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3184 /* If len parameter is one, return an expression corresponding to
3185 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3186 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
3188 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3189 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3190 tree ind1 =
3191 fold (build1 (CONVERT_EXPR, integer_type_node,
3192 build1 (INDIRECT_REF, cst_uchar_node,
3193 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3194 tree ind2 =
3195 fold (build1 (CONVERT_EXPR, integer_type_node,
3196 build1 (INDIRECT_REF, cst_uchar_node,
3197 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3198 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3199 return expand_expr (result, target, mode, EXPAND_NORMAL);
3202 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3204 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3205 rtx result;
3206 rtx insn;
3208 int arg1_align
3209 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3210 int arg2_align
3211 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3212 enum machine_mode insn_mode;
3214 #ifdef HAVE_cmpmemsi
3215 if (HAVE_cmpmemsi)
3216 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3217 else
3218 #endif
3219 #ifdef HAVE_cmpstrsi
3220 if (HAVE_cmpstrsi)
3221 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3222 else
3223 #endif
3224 return 0;
3226 /* If we don't have POINTER_TYPE, call the function. */
3227 if (arg1_align == 0 || arg2_align == 0)
3228 return 0;
3230 /* Make a place to write the result of the instruction. */
3231 result = target;
3232 if (! (result != 0
3233 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3234 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3235 result = gen_reg_rtx (insn_mode);
3237 arg1_rtx = get_memory_rtx (arg1);
3238 arg2_rtx = get_memory_rtx (arg2);
3239 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3240 #ifdef HAVE_cmpmemsi
3241 if (HAVE_cmpmemsi)
3242 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3243 GEN_INT (MIN (arg1_align, arg2_align)));
3244 else
3245 #endif
3246 #ifdef HAVE_cmpstrsi
3247 if (HAVE_cmpstrsi)
3248 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3249 GEN_INT (MIN (arg1_align, arg2_align)));
3250 else
3251 #endif
3252 abort ();
3254 if (insn)
3255 emit_insn (insn);
3256 else
3257 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3258 TYPE_MODE (integer_type_node), 3,
3259 XEXP (arg1_rtx, 0), Pmode,
3260 XEXP (arg2_rtx, 0), Pmode,
3261 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3262 TREE_UNSIGNED (sizetype)),
3263 TYPE_MODE (sizetype));
3265 /* Return the value in the proper mode for this function. */
3266 mode = TYPE_MODE (TREE_TYPE (exp));
3267 if (GET_MODE (result) == mode)
3268 return result;
3269 else if (target != 0)
3271 convert_move (target, result, 0);
3272 return target;
3274 else
3275 return convert_to_mode (mode, result, 0);
3277 #endif
3279 return 0;
3282 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3283 if we failed the caller should emit a normal call, otherwise try to get
3284 the result in TARGET, if convenient. */
3286 static rtx
3287 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3289 tree arglist = TREE_OPERAND (exp, 1);
3290 tree arg1, arg2;
3291 const char *p1, *p2;
3293 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3294 return 0;
3296 arg1 = TREE_VALUE (arglist);
3297 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3299 p1 = c_getstr (arg1);
3300 p2 = c_getstr (arg2);
3302 if (p1 && p2)
3304 const int i = strcmp (p1, p2);
3305 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
3308 /* If either arg is "", return an expression corresponding to
3309 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3310 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3312 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3313 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3314 tree ind1 =
3315 fold (build1 (CONVERT_EXPR, integer_type_node,
3316 build1 (INDIRECT_REF, cst_uchar_node,
3317 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3318 tree ind2 =
3319 fold (build1 (CONVERT_EXPR, integer_type_node,
3320 build1 (INDIRECT_REF, cst_uchar_node,
3321 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3322 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3323 return expand_expr (result, target, mode, EXPAND_NORMAL);
3326 #ifdef HAVE_cmpstrsi
3327 if (HAVE_cmpstrsi)
3329 tree len, len1, len2;
3330 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3331 rtx result, insn;
3333 int arg1_align
3334 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3335 int arg2_align
3336 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3337 enum machine_mode insn_mode
3338 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3340 len1 = c_strlen (arg1, 1);
3341 len2 = c_strlen (arg2, 1);
3343 if (len1)
3344 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3345 if (len2)
3346 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3348 /* If we don't have a constant length for the first, use the length
3349 of the second, if we know it. We don't require a constant for
3350 this case; some cost analysis could be done if both are available
3351 but neither is constant. For now, assume they're equally cheap,
3352 unless one has side effects. If both strings have constant lengths,
3353 use the smaller. */
3355 if (!len1)
3356 len = len2;
3357 else if (!len2)
3358 len = len1;
3359 else if (TREE_SIDE_EFFECTS (len1))
3360 len = len2;
3361 else if (TREE_SIDE_EFFECTS (len2))
3362 len = len1;
3363 else if (TREE_CODE (len1) != INTEGER_CST)
3364 len = len2;
3365 else if (TREE_CODE (len2) != INTEGER_CST)
3366 len = len1;
3367 else if (tree_int_cst_lt (len1, len2))
3368 len = len1;
3369 else
3370 len = len2;
3372 /* If both arguments have side effects, we cannot optimize. */
3373 if (!len || TREE_SIDE_EFFECTS (len))
3374 return 0;
3376 /* If we don't have POINTER_TYPE, call the function. */
3377 if (arg1_align == 0 || arg2_align == 0)
3378 return 0;
3380 /* Make a place to write the result of the instruction. */
3381 result = target;
3382 if (! (result != 0
3383 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3384 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3385 result = gen_reg_rtx (insn_mode);
3387 arg1_rtx = get_memory_rtx (arg1);
3388 arg2_rtx = get_memory_rtx (arg2);
3389 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3390 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3391 GEN_INT (MIN (arg1_align, arg2_align)));
3392 if (!insn)
3393 return 0;
3395 emit_insn (insn);
3397 /* Return the value in the proper mode for this function. */
3398 mode = TYPE_MODE (TREE_TYPE (exp));
3399 if (GET_MODE (result) == mode)
3400 return result;
3401 if (target == 0)
3402 return convert_to_mode (mode, result, 0);
3403 convert_move (target, result, 0);
3404 return target;
3406 #endif
3407 return 0;
3410 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3411 if we failed the caller should emit a normal call, otherwise try to get
3412 the result in TARGET, if convenient. */
3414 static rtx
3415 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3417 tree arglist = TREE_OPERAND (exp, 1);
3418 tree arg1, arg2, arg3;
3419 const char *p1, *p2;
3421 if (!validate_arglist (arglist,
3422 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3423 return 0;
3425 arg1 = TREE_VALUE (arglist);
3426 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3427 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3429 /* If the len parameter is zero, return zero. */
3430 if (host_integerp (arg3, 1) && tree_low_cst (arg3, 1) == 0)
3432 /* Evaluate and ignore arg1 and arg2 in case they have
3433 side-effects. */
3434 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3435 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3436 return const0_rtx;
3439 p1 = c_getstr (arg1);
3440 p2 = c_getstr (arg2);
3442 /* If all arguments are constant, evaluate at compile-time. */
3443 if (host_integerp (arg3, 1) && p1 && p2)
3445 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
3446 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3449 /* If len == 1 or (either string parameter is "" and (len >= 1)),
3450 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
3451 if (host_integerp (arg3, 1)
3452 && (tree_low_cst (arg3, 1) == 1
3453 || (tree_low_cst (arg3, 1) > 1
3454 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
3456 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3457 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3458 tree ind1 =
3459 fold (build1 (CONVERT_EXPR, integer_type_node,
3460 build1 (INDIRECT_REF, cst_uchar_node,
3461 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3462 tree ind2 =
3463 fold (build1 (CONVERT_EXPR, integer_type_node,
3464 build1 (INDIRECT_REF, cst_uchar_node,
3465 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3466 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3467 return expand_expr (result, target, mode, EXPAND_NORMAL);
3470 /* If c_strlen can determine an expression for one of the string
3471 lengths, and it doesn't have side effects, then emit cmpstrsi
3472 using length MIN(strlen(string)+1, arg3). */
3473 #ifdef HAVE_cmpstrsi
3474 if (HAVE_cmpstrsi)
3476 tree len, len1, len2;
3477 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3478 rtx result, insn;
3480 int arg1_align
3481 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3482 int arg2_align
3483 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3484 enum machine_mode insn_mode
3485 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3487 len1 = c_strlen (arg1, 1);
3488 len2 = c_strlen (arg2, 1);
3490 if (len1)
3491 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3492 if (len2)
3493 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3495 /* If we don't have a constant length for the first, use the length
3496 of the second, if we know it. We don't require a constant for
3497 this case; some cost analysis could be done if both are available
3498 but neither is constant. For now, assume they're equally cheap,
3499 unless one has side effects. If both strings have constant lengths,
3500 use the smaller. */
3502 if (!len1)
3503 len = len2;
3504 else if (!len2)
3505 len = len1;
3506 else if (TREE_SIDE_EFFECTS (len1))
3507 len = len2;
3508 else if (TREE_SIDE_EFFECTS (len2))
3509 len = len1;
3510 else if (TREE_CODE (len1) != INTEGER_CST)
3511 len = len2;
3512 else if (TREE_CODE (len2) != INTEGER_CST)
3513 len = len1;
3514 else if (tree_int_cst_lt (len1, len2))
3515 len = len1;
3516 else
3517 len = len2;
3519 /* If both arguments have side effects, we cannot optimize. */
3520 if (!len || TREE_SIDE_EFFECTS (len))
3521 return 0;
3523 /* The actual new length parameter is MIN(len,arg3). */
3524 len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
3526 /* If we don't have POINTER_TYPE, call the function. */
3527 if (arg1_align == 0 || arg2_align == 0)
3528 return 0;
3530 /* Make a place to write the result of the instruction. */
3531 result = target;
3532 if (! (result != 0
3533 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3534 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3535 result = gen_reg_rtx (insn_mode);
3537 arg1_rtx = get_memory_rtx (arg1);
3538 arg2_rtx = get_memory_rtx (arg2);
3539 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3540 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3541 GEN_INT (MIN (arg1_align, arg2_align)));
3542 if (!insn)
3543 return 0;
3545 emit_insn (insn);
3547 /* Return the value in the proper mode for this function. */
3548 mode = TYPE_MODE (TREE_TYPE (exp));
3549 if (GET_MODE (result) == mode)
3550 return result;
3551 if (target == 0)
3552 return convert_to_mode (mode, result, 0);
3553 convert_move (target, result, 0);
3554 return target;
3556 #endif
3557 return 0;
3560 /* Expand expression EXP, which is a call to the strcat builtin.
3561 Return 0 if we failed the caller should emit a normal call,
3562 otherwise try to get the result in TARGET, if convenient. */
3564 static rtx
3565 expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
3567 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3568 return 0;
3569 else
3571 tree dst = TREE_VALUE (arglist),
3572 src = TREE_VALUE (TREE_CHAIN (arglist));
3573 const char *p = c_getstr (src);
3575 /* If the string length is zero, return the dst parameter. */
3576 if (p && *p == '\0')
3577 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3579 return 0;
3583 /* Expand expression EXP, which is a call to the strncat builtin.
3584 Return 0 if we failed the caller should emit a normal call,
3585 otherwise try to get the result in TARGET, if convenient. */
3587 static rtx
3588 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3590 if (!validate_arglist (arglist,
3591 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3592 return 0;
3593 else
3595 tree dst = TREE_VALUE (arglist),
3596 src = TREE_VALUE (TREE_CHAIN (arglist)),
3597 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3598 const char *p = c_getstr (src);
3600 /* If the requested length is zero, or the src parameter string
3601 length is zero, return the dst parameter. */
3602 if (integer_zerop (len) || (p && *p == '\0'))
3604 /* Evaluate and ignore the src and len parameters in case
3605 they have side-effects. */
3606 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3607 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3608 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3611 /* If the requested len is greater than or equal to the string
3612 length, call strcat. */
3613 if (TREE_CODE (len) == INTEGER_CST && p
3614 && compare_tree_int (len, strlen (p)) >= 0)
3616 tree newarglist
3617 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
3618 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
3620 /* If the replacement _DECL isn't initialized, don't do the
3621 transformation. */
3622 if (!fn)
3623 return 0;
3625 return expand_expr (build_function_call_expr (fn, newarglist),
3626 target, mode, EXPAND_NORMAL);
3628 return 0;
3632 /* Expand expression EXP, which is a call to the strspn builtin.
3633 Return 0 if we failed the caller should emit a normal call,
3634 otherwise try to get the result in TARGET, if convenient. */
3636 static rtx
3637 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3639 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3640 return 0;
3641 else
3643 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3644 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3646 /* If both arguments are constants, evaluate at compile-time. */
3647 if (p1 && p2)
3649 const size_t r = strspn (p1, p2);
3650 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3653 /* If either argument is "", return 0. */
3654 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3656 /* Evaluate and ignore both arguments in case either one has
3657 side-effects. */
3658 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3659 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3660 return const0_rtx;
3662 return 0;
3666 /* Expand expression EXP, which is a call to the strcspn builtin.
3667 Return 0 if we failed the caller should emit a normal call,
3668 otherwise try to get the result in TARGET, if convenient. */
3670 static rtx
3671 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3673 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3674 return 0;
3675 else
3677 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3678 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3680 /* If both arguments are constants, evaluate at compile-time. */
3681 if (p1 && p2)
3683 const size_t r = strcspn (p1, p2);
3684 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3687 /* If the first argument is "", return 0. */
3688 if (p1 && *p1 == '\0')
3690 /* Evaluate and ignore argument s2 in case it has
3691 side-effects. */
3692 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3693 return const0_rtx;
3696 /* If the second argument is "", return __builtin_strlen(s1). */
3697 if (p2 && *p2 == '\0')
3699 tree newarglist = build_tree_list (NULL_TREE, s1),
3700 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3702 /* If the replacement _DECL isn't initialized, don't do the
3703 transformation. */
3704 if (!fn)
3705 return 0;
3707 return expand_expr (build_function_call_expr (fn, newarglist),
3708 target, mode, EXPAND_NORMAL);
3710 return 0;
3714 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3715 if that's convenient. */
3718 expand_builtin_saveregs (void)
3720 rtx val, seq;
3722 /* Don't do __builtin_saveregs more than once in a function.
3723 Save the result of the first call and reuse it. */
3724 if (saveregs_value != 0)
3725 return saveregs_value;
3727 /* When this function is called, it means that registers must be
3728 saved on entry to this function. So we migrate the call to the
3729 first insn of this function. */
3731 start_sequence ();
3733 #ifdef EXPAND_BUILTIN_SAVEREGS
3734 /* Do whatever the machine needs done in this case. */
3735 val = EXPAND_BUILTIN_SAVEREGS ();
3736 #else
3737 /* ??? We used to try and build up a call to the out of line function,
3738 guessing about what registers needed saving etc. This became much
3739 harder with __builtin_va_start, since we don't have a tree for a
3740 call to __builtin_saveregs to fall back on. There was exactly one
3741 port (i860) that used this code, and I'm unconvinced it could actually
3742 handle the general case. So we no longer try to handle anything
3743 weird and make the backend absorb the evil. */
3745 error ("__builtin_saveregs not supported by this target");
3746 val = const0_rtx;
3747 #endif
3749 seq = get_insns ();
3750 end_sequence ();
3752 saveregs_value = val;
3754 /* Put the insns after the NOTE that starts the function. If this
3755 is inside a start_sequence, make the outer-level insn chain current, so
3756 the code is placed at the start of the function. */
3757 push_topmost_sequence ();
3758 emit_insn_after (seq, get_insns ());
3759 pop_topmost_sequence ();
3761 return val;
3764 /* __builtin_args_info (N) returns word N of the arg space info
3765 for the current function. The number and meanings of words
3766 is controlled by the definition of CUMULATIVE_ARGS. */
3768 static rtx
3769 expand_builtin_args_info (tree arglist)
3771 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3772 int *word_ptr = (int *) &current_function_args_info;
3774 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
3775 abort ();
3777 if (arglist != 0)
3779 if (!host_integerp (TREE_VALUE (arglist), 0))
3780 error ("argument of `__builtin_args_info' must be constant");
3781 else
3783 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3785 if (wordnum < 0 || wordnum >= nwords)
3786 error ("argument of `__builtin_args_info' out of range");
3787 else
3788 return GEN_INT (word_ptr[wordnum]);
3791 else
3792 error ("missing argument in `__builtin_args_info'");
3794 return const0_rtx;
3797 /* Expand ARGLIST, from a call to __builtin_next_arg. */
3799 static rtx
3800 expand_builtin_next_arg (tree arglist)
3802 tree fntype = TREE_TYPE (current_function_decl);
3804 if (TYPE_ARG_TYPES (fntype) == 0
3805 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3806 == void_type_node))
3808 error ("`va_start' used in function with fixed args");
3809 return const0_rtx;
3812 if (arglist)
3814 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
3815 tree arg = TREE_VALUE (arglist);
3817 /* Strip off all nops for the sake of the comparison. This
3818 is not quite the same as STRIP_NOPS. It does more.
3819 We must also strip off INDIRECT_EXPR for C++ reference
3820 parameters. */
3821 while (TREE_CODE (arg) == NOP_EXPR
3822 || TREE_CODE (arg) == CONVERT_EXPR
3823 || TREE_CODE (arg) == NON_LVALUE_EXPR
3824 || TREE_CODE (arg) == INDIRECT_REF)
3825 arg = TREE_OPERAND (arg, 0);
3826 if (arg != last_parm)
3827 warning ("second parameter of `va_start' not last named argument");
3829 else
3830 /* Evidently an out of date version of <stdarg.h>; can't validate
3831 va_start's second argument, but can still work as intended. */
3832 warning ("`__builtin_next_arg' called without an argument");
3834 return expand_binop (Pmode, add_optab,
3835 current_function_internal_arg_pointer,
3836 current_function_arg_offset_rtx,
3837 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3840 /* Make it easier for the backends by protecting the valist argument
3841 from multiple evaluations. */
3843 static tree
3844 stabilize_va_list (tree valist, int needs_lvalue)
3846 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3848 if (TREE_SIDE_EFFECTS (valist))
3849 valist = save_expr (valist);
3851 /* For this case, the backends will be expecting a pointer to
3852 TREE_TYPE (va_list_type_node), but it's possible we've
3853 actually been given an array (an actual va_list_type_node).
3854 So fix it. */
3855 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3857 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3858 tree p2 = build_pointer_type (va_list_type_node);
3860 valist = build1 (ADDR_EXPR, p2, valist);
3861 valist = fold (build1 (NOP_EXPR, p1, valist));
3864 else
3866 tree pt;
3868 if (! needs_lvalue)
3870 if (! TREE_SIDE_EFFECTS (valist))
3871 return valist;
3873 pt = build_pointer_type (va_list_type_node);
3874 valist = fold (build1 (ADDR_EXPR, pt, valist));
3875 TREE_SIDE_EFFECTS (valist) = 1;
3878 if (TREE_SIDE_EFFECTS (valist))
3879 valist = save_expr (valist);
3880 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
3881 valist));
3884 return valist;
3887 /* The "standard" implementation of va_start: just assign `nextarg' to
3888 the variable. */
3890 void
3891 std_expand_builtin_va_start (tree valist, rtx nextarg)
3893 tree t;
3895 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3896 make_tree (ptr_type_node, nextarg));
3897 TREE_SIDE_EFFECTS (t) = 1;
3899 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3902 /* Expand ARGLIST, from a call to __builtin_va_start. */
3904 static rtx
3905 expand_builtin_va_start (tree arglist)
3907 rtx nextarg;
3908 tree chain, valist;
3910 chain = TREE_CHAIN (arglist);
3912 if (TREE_CHAIN (chain))
3913 error ("too many arguments to function `va_start'");
3915 nextarg = expand_builtin_next_arg (chain);
3916 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3918 #ifdef EXPAND_BUILTIN_VA_START
3919 EXPAND_BUILTIN_VA_START (valist, nextarg);
3920 #else
3921 std_expand_builtin_va_start (valist, nextarg);
3922 #endif
3924 return const0_rtx;
3927 /* The "standard" implementation of va_arg: read the value from the
3928 current (padded) address and increment by the (padded) size. */
3931 std_expand_builtin_va_arg (tree valist, tree type)
3933 tree addr_tree, t, type_size = NULL;
3934 tree align, alignm1;
3935 tree rounded_size;
3936 rtx addr;
3938 /* Compute the rounded size of the type. */
3939 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
3940 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
3941 if (type == error_mark_node
3942 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
3943 || TREE_OVERFLOW (type_size))
3944 rounded_size = size_zero_node;
3945 else
3946 rounded_size = fold (build (MULT_EXPR, sizetype,
3947 fold (build (TRUNC_DIV_EXPR, sizetype,
3948 fold (build (PLUS_EXPR, sizetype,
3949 type_size, alignm1)),
3950 align)),
3951 align));
3953 /* Get AP. */
3954 addr_tree = valist;
3955 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
3957 /* Small args are padded downward. */
3958 addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
3959 fold (build (COND_EXPR, sizetype,
3960 fold (build (GT_EXPR, sizetype,
3961 rounded_size,
3962 align)),
3963 size_zero_node,
3964 fold (build (MINUS_EXPR, sizetype,
3965 rounded_size,
3966 type_size))))));
3969 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3970 addr = copy_to_reg (addr);
3972 /* Compute new value for AP. */
3973 if (! integer_zerop (rounded_size))
3975 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3976 build (PLUS_EXPR, TREE_TYPE (valist), valist,
3977 rounded_size));
3978 TREE_SIDE_EFFECTS (t) = 1;
3979 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3982 return addr;
3985 /* Expand __builtin_va_arg, which is not really a builtin function, but
3986 a very special sort of operator. */
3989 expand_builtin_va_arg (tree valist, tree type)
3991 rtx addr, result;
3992 tree promoted_type, want_va_type, have_va_type;
3994 /* Verify that valist is of the proper type. */
3996 want_va_type = va_list_type_node;
3997 have_va_type = TREE_TYPE (valist);
3998 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4000 /* If va_list is an array type, the argument may have decayed
4001 to a pointer type, e.g. by being passed to another function.
4002 In that case, unwrap both types so that we can compare the
4003 underlying records. */
4004 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4005 || TREE_CODE (have_va_type) == POINTER_TYPE)
4007 want_va_type = TREE_TYPE (want_va_type);
4008 have_va_type = TREE_TYPE (have_va_type);
4011 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4013 error ("first argument to `va_arg' not of type `va_list'");
4014 addr = const0_rtx;
4017 /* Generate a diagnostic for requesting data of a type that cannot
4018 be passed through `...' due to type promotion at the call site. */
4019 else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
4020 != type)
4022 const char *name = "<anonymous type>", *pname = 0;
4023 static bool gave_help;
4025 if (TYPE_NAME (type))
4027 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4028 name = IDENTIFIER_POINTER (TYPE_NAME (type));
4029 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4030 && DECL_NAME (TYPE_NAME (type)))
4031 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
4033 if (TYPE_NAME (promoted_type))
4035 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
4036 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
4037 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
4038 && DECL_NAME (TYPE_NAME (promoted_type)))
4039 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
4042 /* Unfortunately, this is merely undefined, rather than a constraint
4043 violation, so we cannot make this an error. If this call is never
4044 executed, the program is still strictly conforming. */
4045 warning ("`%s' is promoted to `%s' when passed through `...'",
4046 name, pname);
4047 if (! gave_help)
4049 gave_help = true;
4050 warning ("(so you should pass `%s' not `%s' to `va_arg')",
4051 pname, name);
4054 /* We can, however, treat "undefined" any way we please.
4055 Call abort to encourage the user to fix the program. */
4056 expand_builtin_trap ();
4058 /* This is dead code, but go ahead and finish so that the
4059 mode of the result comes out right. */
4060 addr = const0_rtx;
4062 else
4064 /* Make it easier for the backends by protecting the valist argument
4065 from multiple evaluations. */
4066 valist = stabilize_va_list (valist, 0);
4068 #ifdef EXPAND_BUILTIN_VA_ARG
4069 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
4070 #else
4071 addr = std_expand_builtin_va_arg (valist, type);
4072 #endif
4075 #ifdef POINTERS_EXTEND_UNSIGNED
4076 if (GET_MODE (addr) != Pmode)
4077 addr = convert_memory_address (Pmode, addr);
4078 #endif
4080 result = gen_rtx_MEM (TYPE_MODE (type), addr);
4081 set_mem_alias_set (result, get_varargs_alias_set ());
4083 return result;
4086 /* Expand ARGLIST, from a call to __builtin_va_end. */
4088 static rtx
4089 expand_builtin_va_end (tree arglist)
4091 tree valist = TREE_VALUE (arglist);
4093 #ifdef EXPAND_BUILTIN_VA_END
4094 valist = stabilize_va_list (valist, 0);
4095 EXPAND_BUILTIN_VA_END (arglist);
4096 #else
4097 /* Evaluate for side effects, if needed. I hate macros that don't
4098 do that. */
4099 if (TREE_SIDE_EFFECTS (valist))
4100 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4101 #endif
4103 return const0_rtx;
4106 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4107 builtin rather than just as an assignment in stdarg.h because of the
4108 nastiness of array-type va_list types. */
4110 static rtx
4111 expand_builtin_va_copy (tree arglist)
4113 tree dst, src, t;
4115 dst = TREE_VALUE (arglist);
4116 src = TREE_VALUE (TREE_CHAIN (arglist));
4118 dst = stabilize_va_list (dst, 1);
4119 src = stabilize_va_list (src, 0);
4121 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4123 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
4124 TREE_SIDE_EFFECTS (t) = 1;
4125 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4127 else
4129 rtx dstb, srcb, size;
4131 /* Evaluate to pointers. */
4132 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4133 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4134 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4135 VOIDmode, EXPAND_NORMAL);
4137 #ifdef POINTERS_EXTEND_UNSIGNED
4138 if (GET_MODE (dstb) != Pmode)
4139 dstb = convert_memory_address (Pmode, dstb);
4141 if (GET_MODE (srcb) != Pmode)
4142 srcb = convert_memory_address (Pmode, srcb);
4143 #endif
4145 /* "Dereference" to BLKmode memories. */
4146 dstb = gen_rtx_MEM (BLKmode, dstb);
4147 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4148 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4149 srcb = gen_rtx_MEM (BLKmode, srcb);
4150 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4151 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4153 /* Copy. */
4154 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4157 return const0_rtx;
4160 /* Expand a call to one of the builtin functions __builtin_frame_address or
4161 __builtin_return_address. */
4163 static rtx
4164 expand_builtin_frame_address (tree fndecl, tree arglist)
4166 /* The argument must be a nonnegative integer constant.
4167 It counts the number of frames to scan up the stack.
4168 The value is the return address saved in that frame. */
4169 if (arglist == 0)
4170 /* Warning about missing arg was already issued. */
4171 return const0_rtx;
4172 else if (! host_integerp (TREE_VALUE (arglist), 1))
4174 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4175 error ("invalid arg to `__builtin_frame_address'");
4176 else
4177 error ("invalid arg to `__builtin_return_address'");
4178 return const0_rtx;
4180 else
4182 rtx tem
4183 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4184 tree_low_cst (TREE_VALUE (arglist), 1),
4185 hard_frame_pointer_rtx);
4187 /* Some ports cannot access arbitrary stack frames. */
4188 if (tem == NULL)
4190 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4191 warning ("unsupported arg to `__builtin_frame_address'");
4192 else
4193 warning ("unsupported arg to `__builtin_return_address'");
4194 return const0_rtx;
4197 /* For __builtin_frame_address, return what we've got. */
4198 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4199 return tem;
4201 if (GET_CODE (tem) != REG
4202 && ! CONSTANT_P (tem))
4203 tem = copy_to_mode_reg (Pmode, tem);
4204 return tem;
4208 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4209 we failed and the caller should emit a normal call, otherwise try to get
4210 the result in TARGET, if convenient. */
4212 static rtx
4213 expand_builtin_alloca (tree arglist, rtx target)
4215 rtx op0;
4216 rtx result;
4218 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4219 return 0;
4221 /* Compute the argument. */
4222 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4224 /* Allocate the desired space. */
4225 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4227 #ifdef POINTERS_EXTEND_UNSIGNED
4228 if (GET_MODE (result) != ptr_mode)
4229 result = convert_memory_address (ptr_mode, result);
4230 #endif
4232 return result;
4235 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4236 Return 0 if a normal call should be emitted rather than expanding the
4237 function in-line. If convenient, the result should be placed in TARGET.
4238 SUBTARGET may be used as the target for computing one of EXP's operands. */
4240 static rtx
4241 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4242 rtx subtarget, optab op_optab)
4244 rtx op0;
4245 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4246 return 0;
4248 /* Compute the argument. */
4249 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4250 /* Compute op, into TARGET if possible.
4251 Set TARGET to wherever the result comes back. */
4252 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4253 op_optab, op0, target, 1);
4254 if (target == 0)
4255 abort ();
4257 return convert_to_mode (target_mode, target, 0);
4260 /* If the string passed to fputs is a constant and is one character
4261 long, we attempt to transform this call into __builtin_fputc(). */
4263 static rtx
4264 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4266 tree len, fn;
4267 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4268 : implicit_built_in_decls[BUILT_IN_FPUTC];
4269 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
4270 : implicit_built_in_decls[BUILT_IN_FWRITE];
4272 /* If the return value is used, or the replacement _DECL isn't
4273 initialized, don't do the transformation. */
4274 if (target != const0_rtx || !fn_fputc || !fn_fwrite)
4275 return 0;
4277 /* Verify the arguments in the original call. */
4278 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4279 return 0;
4281 /* Get the length of the string passed to fputs. If the length
4282 can't be determined, punt. */
4283 if (!(len = c_strlen (TREE_VALUE (arglist), 1))
4284 || TREE_CODE (len) != INTEGER_CST)
4285 return 0;
4287 switch (compare_tree_int (len, 1))
4289 case -1: /* length is 0, delete the call entirely . */
4291 /* Evaluate and ignore the argument in case it has
4292 side-effects. */
4293 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
4294 VOIDmode, EXPAND_NORMAL);
4295 return const0_rtx;
4297 case 0: /* length is 1, call fputc. */
4299 const char *p = c_getstr (TREE_VALUE (arglist));
4301 if (p != NULL)
4303 /* New argument list transforming fputs(string, stream) to
4304 fputc(string[0], stream). */
4305 arglist =
4306 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4307 arglist =
4308 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
4309 fn = fn_fputc;
4310 break;
4313 /* FALLTHROUGH */
4314 case 1: /* length is greater than 1, call fwrite. */
4316 tree string_arg;
4318 /* If optimizing for size keep fputs. */
4319 if (optimize_size)
4320 return 0;
4321 string_arg = TREE_VALUE (arglist);
4322 /* New argument list transforming fputs(string, stream) to
4323 fwrite(string, 1, len, stream). */
4324 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4325 arglist = tree_cons (NULL_TREE, len, arglist);
4326 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
4327 arglist = tree_cons (NULL_TREE, string_arg, arglist);
4328 fn = fn_fwrite;
4329 break;
4331 default:
4332 abort ();
4335 return expand_expr (build_function_call_expr (fn, arglist),
4336 const0_rtx, VOIDmode, EXPAND_NORMAL);
4339 /* Expand a call to __builtin_expect. We return our argument and emit a
4340 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4341 a non-jump context. */
4343 static rtx
4344 expand_builtin_expect (tree arglist, rtx target)
4346 tree exp, c;
4347 rtx note, rtx_c;
4349 if (arglist == NULL_TREE
4350 || TREE_CHAIN (arglist) == NULL_TREE)
4351 return const0_rtx;
4352 exp = TREE_VALUE (arglist);
4353 c = TREE_VALUE (TREE_CHAIN (arglist));
4355 if (TREE_CODE (c) != INTEGER_CST)
4357 error ("second arg to `__builtin_expect' must be a constant");
4358 c = integer_zero_node;
4361 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4363 /* Don't bother with expected value notes for integral constants. */
4364 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4366 /* We do need to force this into a register so that we can be
4367 moderately sure to be able to correctly interpret the branch
4368 condition later. */
4369 target = force_reg (GET_MODE (target), target);
4371 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4373 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4374 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4377 return target;
4380 /* Like expand_builtin_expect, except do this in a jump context. This is
4381 called from do_jump if the conditional is a __builtin_expect. Return either
4382 a list of insns to emit the jump or NULL if we cannot optimize
4383 __builtin_expect. We need to optimize this at jump time so that machines
4384 like the PowerPC don't turn the test into a SCC operation, and then jump
4385 based on the test being 0/1. */
4388 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4390 tree arglist = TREE_OPERAND (exp, 1);
4391 tree arg0 = TREE_VALUE (arglist);
4392 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4393 rtx ret = NULL_RTX;
4395 /* Only handle __builtin_expect (test, 0) and
4396 __builtin_expect (test, 1). */
4397 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4398 && (integer_zerop (arg1) || integer_onep (arg1)))
4400 int num_jumps = 0;
4401 rtx insn;
4403 /* If we fail to locate an appropriate conditional jump, we'll
4404 fall back to normal evaluation. Ensure that the expression
4405 can be re-evaluated. */
4406 switch (unsafe_for_reeval (arg0))
4408 case 0: /* Safe. */
4409 break;
4411 case 1: /* Mildly unsafe. */
4412 arg0 = unsave_expr (arg0);
4413 break;
4415 case 2: /* Wildly unsafe. */
4416 return NULL_RTX;
4419 /* Expand the jump insns. */
4420 start_sequence ();
4421 do_jump (arg0, if_false_label, if_true_label);
4422 ret = get_insns ();
4423 end_sequence ();
4425 /* Now that the __builtin_expect has been validated, go through and add
4426 the expect's to each of the conditional jumps. If we run into an
4427 error, just give up and generate the 'safe' code of doing a SCC
4428 operation and then doing a branch on that. */
4429 insn = ret;
4430 while (insn != NULL_RTX)
4432 rtx next = NEXT_INSN (insn);
4434 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn))
4436 rtx ifelse = SET_SRC (pc_set (insn));
4437 rtx label;
4438 int taken;
4440 if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF)
4442 taken = 1;
4443 label = XEXP (XEXP (ifelse, 1), 0);
4445 /* An inverted jump reverses the probabilities. */
4446 else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF)
4448 taken = 0;
4449 label = XEXP (XEXP (ifelse, 2), 0);
4451 /* We shouldn't have to worry about conditional returns during
4452 the expansion stage, but handle it gracefully anyway. */
4453 else if (GET_CODE (XEXP (ifelse, 1)) == RETURN)
4455 taken = 1;
4456 label = NULL_RTX;
4458 /* An inverted return reverses the probabilities. */
4459 else if (GET_CODE (XEXP (ifelse, 2)) == RETURN)
4461 taken = 0;
4462 label = NULL_RTX;
4464 else
4465 goto do_next_insn;
4467 /* If the test is expected to fail, reverse the
4468 probabilities. */
4469 if (integer_zerop (arg1))
4470 taken = 1 - taken;
4472 /* If we are jumping to the false label, reverse the
4473 probabilities. */
4474 if (label == NULL_RTX)
4475 ; /* conditional return */
4476 else if (label == if_false_label)
4477 taken = 1 - taken;
4478 else if (label != if_true_label)
4479 goto do_next_insn;
4481 num_jumps++;
4482 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4485 do_next_insn:
4486 insn = next;
4489 /* If no jumps were modified, fail and do __builtin_expect the normal
4490 way. */
4491 if (num_jumps == 0)
4492 ret = NULL_RTX;
4495 return ret;
4498 void
4499 expand_builtin_trap (void)
4501 #ifdef HAVE_trap
4502 if (HAVE_trap)
4503 emit_insn (gen_trap ());
4504 else
4505 #endif
4506 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4507 emit_barrier ();
4510 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4511 Return 0 if a normal call should be emitted rather than expanding
4512 the function inline. If convenient, the result should be placed
4513 in TARGET. SUBTARGET may be used as the target for computing
4514 the operand. */
4516 static rtx
4517 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4519 enum machine_mode mode;
4520 tree arg;
4521 rtx op0;
4523 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4524 return 0;
4526 arg = TREE_VALUE (arglist);
4527 mode = TYPE_MODE (TREE_TYPE (arg));
4528 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4529 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4532 /* Expand a call to cabs, cabsf or cabsl with arguments ARGLIST.
4533 Return 0 if a normal call should be emitted rather than expanding
4534 the function inline. If convenient, the result should be placed
4535 in target. */
4537 static rtx
4538 expand_builtin_cabs (tree arglist, rtx target)
4540 enum machine_mode mode;
4541 tree arg;
4542 rtx op0;
4544 if (arglist == 0 || TREE_CHAIN (arglist))
4545 return 0;
4546 arg = TREE_VALUE (arglist);
4547 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
4548 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
4549 return 0;
4551 mode = TYPE_MODE (TREE_TYPE (arg));
4552 op0 = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4553 return expand_complex_abs (mode, op0, target, 0);
4556 /* Create a new constant string literal and return a char* pointer to it.
4557 The STRING_CST value is the LEN characters at STR. */
4558 static tree
4559 build_string_literal (int len, const char *str)
4561 tree t, elem, index, type;
4563 t = build_string (len, str);
4564 elem = build_type_variant (char_type_node, 1, 0);
4565 index = build_index_type (build_int_2 (len - 1, 0));
4566 type = build_array_type (elem, index);
4567 TREE_TYPE (t) = type;
4568 TREE_CONSTANT (t) = 1;
4569 TREE_READONLY (t) = 1;
4570 TREE_STATIC (t) = 1;
4572 type = build_pointer_type (type);
4573 t = build1 (ADDR_EXPR, type, t);
4575 type = build_pointer_type (elem);
4576 t = build1 (NOP_EXPR, type, t);
4577 return t;
4580 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4581 Return 0 if a normal call should be emitted rather than transforming
4582 the function inline. If convenient, the result should be placed in
4583 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4584 call. */
4585 static rtx
4586 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4587 bool unlocked)
4589 tree fn_putchar = unlocked
4590 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4591 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4592 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4593 : implicit_built_in_decls[BUILT_IN_PUTS];
4594 const char *fmt_str;
4595 tree fn, fmt, arg;
4597 /* If the return value is used, don't do the transformation. */
4598 if (target != const0_rtx)
4599 return 0;
4601 /* Verify the required arguments in the original call. */
4602 if (! arglist)
4603 return 0;
4604 fmt = TREE_VALUE (arglist);
4605 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4606 return 0;
4607 arglist = TREE_CHAIN (arglist);
4609 /* Check whether the format is a literal string constant. */
4610 fmt_str = c_getstr (fmt);
4611 if (fmt_str == NULL)
4612 return 0;
4614 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4615 if (strcmp (fmt_str, "%s\n") == 0)
4617 if (! arglist
4618 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4619 || TREE_CHAIN (arglist))
4620 return 0;
4621 fn = fn_puts;
4623 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4624 else if (strcmp (fmt_str, "%c") == 0)
4626 if (! arglist
4627 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4628 || TREE_CHAIN (arglist))
4629 return 0;
4630 fn = fn_putchar;
4632 else
4634 /* We can't handle anything else with % args or %% ... yet. */
4635 if (strchr (fmt_str, '%'))
4636 return 0;
4638 if (arglist)
4639 return 0;
4641 /* If the format specifier was "", printf does nothing. */
4642 if (fmt_str[0] == '\0')
4643 return const0_rtx;
4644 /* If the format specifier has length of 1, call putchar. */
4645 if (fmt_str[1] == '\0')
4647 /* Given printf("c"), (where c is any one character,)
4648 convert "c"[0] to an int and pass that to the replacement
4649 function. */
4650 arg = build_int_2 (fmt_str[0], 0);
4651 arglist = build_tree_list (NULL_TREE, arg);
4652 fn = fn_putchar;
4654 else
4656 /* If the format specifier was "string\n", call puts("string"). */
4657 size_t len = strlen (fmt_str);
4658 if (fmt_str[len - 1] == '\n')
4660 /* Create a NUL-terminalted string that's one char shorter
4661 than the original, stripping off the trailing '\n'. */
4662 char *newstr = (char *) alloca (len);
4663 memcpy (newstr, fmt_str, len - 1);
4664 newstr[len - 1] = 0;
4666 arg = build_string_literal (len, newstr);
4667 arglist = build_tree_list (NULL_TREE, arg);
4668 fn = fn_puts;
4670 else
4671 /* We'd like to arrange to call fputs(string,stdout) here,
4672 but we need stdout and don't have a way to get it yet. */
4673 return 0;
4677 if (!fn)
4678 return 0;
4679 return expand_expr (build_function_call_expr (fn, arglist),
4680 target, mode, EXPAND_NORMAL);
4683 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4684 Return 0 if a normal call should be emitted rather than transforming
4685 the function inline. If convenient, the result should be placed in
4686 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4687 call. */
4688 static rtx
4689 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4690 bool unlocked)
4692 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4693 : implicit_built_in_decls[BUILT_IN_FPUTC];
4694 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4695 : implicit_built_in_decls[BUILT_IN_FPUTS];
4696 const char *fmt_str;
4697 tree fn, fmt, fp, arg;
4699 /* If the return value is used, don't do the transformation. */
4700 if (target != const0_rtx)
4701 return 0;
4703 /* Verify the required arguments in the original call. */
4704 if (! arglist)
4705 return 0;
4706 fp = TREE_VALUE (arglist);
4707 if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE)
4708 return 0;
4709 arglist = TREE_CHAIN (arglist);
4710 if (! arglist)
4711 return 0;
4712 fmt = TREE_VALUE (arglist);
4713 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4714 return 0;
4715 arglist = TREE_CHAIN (arglist);
4717 /* Check whether the format is a literal string constant. */
4718 fmt_str = c_getstr (fmt);
4719 if (fmt_str == NULL)
4720 return 0;
4722 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4723 if (strcmp (fmt_str, "%s") == 0)
4725 if (! arglist
4726 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4727 || TREE_CHAIN (arglist))
4728 return 0;
4729 arg = TREE_VALUE (arglist);
4730 arglist = build_tree_list (NULL_TREE, fp);
4731 arglist = tree_cons (NULL_TREE, arg, arglist);
4732 fn = fn_fputs;
4734 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4735 else if (strcmp (fmt_str, "%c") == 0)
4737 if (! arglist
4738 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4739 || TREE_CHAIN (arglist))
4740 return 0;
4741 arg = TREE_VALUE (arglist);
4742 arglist = build_tree_list (NULL_TREE, fp);
4743 arglist = tree_cons (NULL_TREE, arg, arglist);
4744 fn = fn_fputc;
4746 else
4748 /* We can't handle anything else with % args or %% ... yet. */
4749 if (strchr (fmt_str, '%'))
4750 return 0;
4752 if (arglist)
4753 return 0;
4755 /* If the format specifier was "", fprintf does nothing. */
4756 if (fmt_str[0] == '\0')
4758 /* Evaluate and ignore FILE* argument for side-effects. */
4759 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4760 return const0_rtx;
4763 /* When "string" doesn't contain %, replace all cases of
4764 fprintf(stream,string) with fputs(string,stream). The fputs
4765 builtin will take care of special cases like length == 1. */
4766 arglist = build_tree_list (NULL_TREE, fp);
4767 arglist = tree_cons (NULL_TREE, fmt, arglist);
4768 fn = fn_fputs;
4771 if (!fn)
4772 return 0;
4773 return expand_expr (build_function_call_expr (fn, arglist),
4774 target, mode, EXPAND_NORMAL);
4777 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4778 a normal call should be emitted rather than expanding the function
4779 inline. If convenient, the result should be placed in TARGET with
4780 mode MODE. */
4782 static rtx
4783 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4785 tree orig_arglist, dest, fmt;
4786 const char *fmt_str;
4788 orig_arglist = arglist;
4790 /* Verify the required arguments in the original call. */
4791 if (! arglist)
4792 return 0;
4793 dest = TREE_VALUE (arglist);
4794 if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE)
4795 return 0;
4796 arglist = TREE_CHAIN (arglist);
4797 if (! arglist)
4798 return 0;
4799 fmt = TREE_VALUE (arglist);
4800 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4801 return 0;
4802 arglist = TREE_CHAIN (arglist);
4804 /* Check whether the format is a literal string constant. */
4805 fmt_str = c_getstr (fmt);
4806 if (fmt_str == NULL)
4807 return 0;
4809 /* If the format doesn't contain % args or %%, use strcpy. */
4810 if (strchr (fmt_str, '%') == 0)
4812 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4813 tree exp;
4815 if (arglist || ! fn)
4816 return 0;
4817 expand_expr (build_function_call_expr (fn, orig_arglist),
4818 const0_rtx, VOIDmode, EXPAND_NORMAL);
4819 if (target == const0_rtx)
4820 return const0_rtx;
4821 exp = build_int_2 (strlen (fmt_str), 0);
4822 exp = fold (build1 (NOP_EXPR, integer_type_node, exp));
4823 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4825 /* If the format is "%s", use strcpy if the result isn't used. */
4826 else if (strcmp (fmt_str, "%s") == 0)
4828 tree fn, arg, len;
4829 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4831 if (! fn)
4832 return 0;
4834 if (! arglist || TREE_CHAIN (arglist))
4835 return 0;
4836 arg = TREE_VALUE (arglist);
4837 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
4838 return 0;
4840 if (target != const0_rtx)
4842 len = c_strlen (arg, 1);
4843 if (! len || TREE_CODE (len) != INTEGER_CST)
4844 return 0;
4846 else
4847 len = NULL_TREE;
4849 arglist = build_tree_list (NULL_TREE, arg);
4850 arglist = tree_cons (NULL_TREE, dest, arglist);
4851 expand_expr (build_function_call_expr (fn, arglist),
4852 const0_rtx, VOIDmode, EXPAND_NORMAL);
4854 if (target == const0_rtx)
4855 return const0_rtx;
4856 return expand_expr (len, target, mode, EXPAND_NORMAL);
4859 return 0;
4862 /* Expand an expression EXP that calls a built-in function,
4863 with result going to TARGET if that's convenient
4864 (and in mode MODE if that's convenient).
4865 SUBTARGET may be used as the target for computing one of EXP's operands.
4866 IGNORE is nonzero if the value is to be ignored. */
4869 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
4870 int ignore)
4872 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4873 tree arglist = TREE_OPERAND (exp, 1);
4874 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4875 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
4877 /* Perform postincrements before expanding builtin functions.  */
4878 emit_queue ();
4880 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4881 return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
4883 /* When not optimizing, generate calls to library functions for a certain
4884 set of builtins. */
4885 if (!optimize && !CALLED_AS_BUILT_IN (fndecl))
4886 switch (fcode)
4888 case BUILT_IN_SQRT:
4889 case BUILT_IN_SQRTF:
4890 case BUILT_IN_SQRTL:
4891 case BUILT_IN_SIN:
4892 case BUILT_IN_SINF:
4893 case BUILT_IN_SINL:
4894 case BUILT_IN_COS:
4895 case BUILT_IN_COSF:
4896 case BUILT_IN_COSL:
4897 case BUILT_IN_EXP:
4898 case BUILT_IN_EXPF:
4899 case BUILT_IN_EXPL:
4900 case BUILT_IN_LOG:
4901 case BUILT_IN_LOGF:
4902 case BUILT_IN_LOGL:
4903 case BUILT_IN_TAN:
4904 case BUILT_IN_TANF:
4905 case BUILT_IN_TANL:
4906 case BUILT_IN_ATAN:
4907 case BUILT_IN_ATANF:
4908 case BUILT_IN_ATANL:
4909 case BUILT_IN_POW:
4910 case BUILT_IN_POWF:
4911 case BUILT_IN_POWL:
4912 case BUILT_IN_ATAN2:
4913 case BUILT_IN_ATAN2F:
4914 case BUILT_IN_ATAN2L:
4915 case BUILT_IN_MEMSET:
4916 case BUILT_IN_MEMCPY:
4917 case BUILT_IN_MEMCMP:
4918 case BUILT_IN_MEMPCPY:
4919 case BUILT_IN_MEMMOVE:
4920 case BUILT_IN_BCMP:
4921 case BUILT_IN_BZERO:
4922 case BUILT_IN_BCOPY:
4923 case BUILT_IN_INDEX:
4924 case BUILT_IN_RINDEX:
4925 case BUILT_IN_SPRINTF:
4926 case BUILT_IN_STPCPY:
4927 case BUILT_IN_STRCHR:
4928 case BUILT_IN_STRRCHR:
4929 case BUILT_IN_STRLEN:
4930 case BUILT_IN_STRCPY:
4931 case BUILT_IN_STRNCPY:
4932 case BUILT_IN_STRNCMP:
4933 case BUILT_IN_STRSTR:
4934 case BUILT_IN_STRPBRK:
4935 case BUILT_IN_STRCAT:
4936 case BUILT_IN_STRNCAT:
4937 case BUILT_IN_STRSPN:
4938 case BUILT_IN_STRCSPN:
4939 case BUILT_IN_STRCMP:
4940 case BUILT_IN_FFS:
4941 case BUILT_IN_PUTCHAR:
4942 case BUILT_IN_PUTS:
4943 case BUILT_IN_PRINTF:
4944 case BUILT_IN_FPUTC:
4945 case BUILT_IN_FPUTS:
4946 case BUILT_IN_FWRITE:
4947 case BUILT_IN_FPRINTF:
4948 case BUILT_IN_PUTCHAR_UNLOCKED:
4949 case BUILT_IN_PUTS_UNLOCKED:
4950 case BUILT_IN_PRINTF_UNLOCKED:
4951 case BUILT_IN_FPUTC_UNLOCKED:
4952 case BUILT_IN_FPUTS_UNLOCKED:
4953 case BUILT_IN_FWRITE_UNLOCKED:
4954 case BUILT_IN_FPRINTF_UNLOCKED:
4955 case BUILT_IN_FLOOR:
4956 case BUILT_IN_FLOORF:
4957 case BUILT_IN_FLOORL:
4958 case BUILT_IN_CEIL:
4959 case BUILT_IN_CEILF:
4960 case BUILT_IN_CEILL:
4961 case BUILT_IN_TRUNC:
4962 case BUILT_IN_TRUNCF:
4963 case BUILT_IN_TRUNCL:
4964 case BUILT_IN_ROUND:
4965 case BUILT_IN_ROUNDF:
4966 case BUILT_IN_ROUNDL:
4967 case BUILT_IN_NEARBYINT:
4968 case BUILT_IN_NEARBYINTF:
4969 case BUILT_IN_NEARBYINTL:
4970 return expand_call (exp, target, ignore);
4972 default:
4973 break;
4976 /* The built-in function expanders test for target == const0_rtx
4977 to determine whether the function's result will be ignored. */
4978 if (ignore)
4979 target = const0_rtx;
4981 /* If the result of a pure or const built-in function is ignored, and
4982 none of its arguments are volatile, we can avoid expanding the
4983 built-in call and just evaluate the arguments for side-effects. */
4984 if (target == const0_rtx
4985 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
4987 bool volatilep = false;
4988 tree arg;
4990 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
4991 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
4993 volatilep = true;
4994 break;
4997 if (! volatilep)
4999 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5000 expand_expr (TREE_VALUE (arg), const0_rtx,
5001 VOIDmode, EXPAND_NORMAL);
5002 return const0_rtx;
5006 switch (fcode)
5008 case BUILT_IN_ABS:
5009 case BUILT_IN_LABS:
5010 case BUILT_IN_LLABS:
5011 case BUILT_IN_IMAXABS:
5012 /* build_function_call changes these into ABS_EXPR. */
5013 abort ();
5015 case BUILT_IN_FABS:
5016 case BUILT_IN_FABSF:
5017 case BUILT_IN_FABSL:
5018 target = expand_builtin_fabs (arglist, target, subtarget);
5019 if (target)
5020 return target;
5021 break;
5023 case BUILT_IN_CABS:
5024 case BUILT_IN_CABSF:
5025 case BUILT_IN_CABSL:
5026 if (flag_unsafe_math_optimizations)
5028 target = expand_builtin_cabs (arglist, target);
5029 if (target)
5030 return target;
5032 break;
5034 case BUILT_IN_CONJ:
5035 case BUILT_IN_CONJF:
5036 case BUILT_IN_CONJL:
5037 case BUILT_IN_CREAL:
5038 case BUILT_IN_CREALF:
5039 case BUILT_IN_CREALL:
5040 case BUILT_IN_CIMAG:
5041 case BUILT_IN_CIMAGF:
5042 case BUILT_IN_CIMAGL:
5043 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
5044 and IMAGPART_EXPR. */
5045 abort ();
5047 case BUILT_IN_SIN:
5048 case BUILT_IN_SINF:
5049 case BUILT_IN_SINL:
5050 case BUILT_IN_COS:
5051 case BUILT_IN_COSF:
5052 case BUILT_IN_COSL:
5053 case BUILT_IN_EXP:
5054 case BUILT_IN_EXPF:
5055 case BUILT_IN_EXPL:
5056 case BUILT_IN_LOG:
5057 case BUILT_IN_LOGF:
5058 case BUILT_IN_LOGL:
5059 case BUILT_IN_TAN:
5060 case BUILT_IN_TANF:
5061 case BUILT_IN_TANL:
5062 case BUILT_IN_ATAN:
5063 case BUILT_IN_ATANF:
5064 case BUILT_IN_ATANL:
5065 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5066 because of possible accuracy problems. */
5067 if (! flag_unsafe_math_optimizations)
5068 break;
5069 case BUILT_IN_SQRT:
5070 case BUILT_IN_SQRTF:
5071 case BUILT_IN_SQRTL:
5072 case BUILT_IN_FLOOR:
5073 case BUILT_IN_FLOORF:
5074 case BUILT_IN_FLOORL:
5075 case BUILT_IN_CEIL:
5076 case BUILT_IN_CEILF:
5077 case BUILT_IN_CEILL:
5078 case BUILT_IN_TRUNC:
5079 case BUILT_IN_TRUNCF:
5080 case BUILT_IN_TRUNCL:
5081 case BUILT_IN_ROUND:
5082 case BUILT_IN_ROUNDF:
5083 case BUILT_IN_ROUNDL:
5084 case BUILT_IN_NEARBYINT:
5085 case BUILT_IN_NEARBYINTF:
5086 case BUILT_IN_NEARBYINTL:
5087 target = expand_builtin_mathfn (exp, target, subtarget);
5088 if (target)
5089 return target;
5090 break;
5092 case BUILT_IN_POW:
5093 case BUILT_IN_POWF:
5094 case BUILT_IN_POWL:
5095 if (! flag_unsafe_math_optimizations)
5096 break;
5097 target = expand_builtin_pow (exp, target, subtarget);
5098 if (target)
5099 return target;
5100 break;
5102 case BUILT_IN_ATAN2:
5103 case BUILT_IN_ATAN2F:
5104 case BUILT_IN_ATAN2L:
5105 if (! flag_unsafe_math_optimizations)
5106 break;
5107 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5108 if (target)
5109 return target;
5110 break;
5112 case BUILT_IN_APPLY_ARGS:
5113 return expand_builtin_apply_args ();
5115 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5116 FUNCTION with a copy of the parameters described by
5117 ARGUMENTS, and ARGSIZE. It returns a block of memory
5118 allocated on the stack into which is stored all the registers
5119 that might possibly be used for returning the result of a
5120 function. ARGUMENTS is the value returned by
5121 __builtin_apply_args. ARGSIZE is the number of bytes of
5122 arguments that must be copied. ??? How should this value be
5123 computed? We'll also need a safe worst case value for varargs
5124 functions. */
5125 case BUILT_IN_APPLY:
5126 if (!validate_arglist (arglist, POINTER_TYPE,
5127 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5128 && !validate_arglist (arglist, REFERENCE_TYPE,
5129 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5130 return const0_rtx;
5131 else
5133 int i;
5134 tree t;
5135 rtx ops[3];
5137 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5138 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5140 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5143 /* __builtin_return (RESULT) causes the function to return the
5144 value described by RESULT. RESULT is address of the block of
5145 memory returned by __builtin_apply. */
5146 case BUILT_IN_RETURN:
5147 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5148 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5149 NULL_RTX, VOIDmode, 0));
5150 return const0_rtx;
5152 case BUILT_IN_SAVEREGS:
5153 return expand_builtin_saveregs ();
5155 case BUILT_IN_ARGS_INFO:
5156 return expand_builtin_args_info (arglist);
5158 /* Return the address of the first anonymous stack arg. */
5159 case BUILT_IN_NEXT_ARG:
5160 return expand_builtin_next_arg (arglist);
5162 case BUILT_IN_CLASSIFY_TYPE:
5163 return expand_builtin_classify_type (arglist);
5165 case BUILT_IN_CONSTANT_P:
5166 return expand_builtin_constant_p (arglist, target_mode);
5168 case BUILT_IN_FRAME_ADDRESS:
5169 case BUILT_IN_RETURN_ADDRESS:
5170 return expand_builtin_frame_address (fndecl, arglist);
5172 /* Returns the address of the area where the structure is returned.
5173 0 otherwise. */
5174 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5175 if (arglist != 0
5176 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5177 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
5178 return const0_rtx;
5179 else
5180 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5182 case BUILT_IN_ALLOCA:
5183 target = expand_builtin_alloca (arglist, target);
5184 if (target)
5185 return target;
5186 break;
5188 case BUILT_IN_FFS:
5189 case BUILT_IN_FFSL:
5190 case BUILT_IN_FFSLL:
5191 target = expand_builtin_unop (target_mode, arglist, target,
5192 subtarget, ffs_optab);
5193 if (target)
5194 return target;
5195 break;
5197 case BUILT_IN_CLZ:
5198 case BUILT_IN_CLZL:
5199 case BUILT_IN_CLZLL:
5200 target = expand_builtin_unop (target_mode, arglist, target,
5201 subtarget, clz_optab);
5202 if (target)
5203 return target;
5204 break;
5206 case BUILT_IN_CTZ:
5207 case BUILT_IN_CTZL:
5208 case BUILT_IN_CTZLL:
5209 target = expand_builtin_unop (target_mode, arglist, target,
5210 subtarget, ctz_optab);
5211 if (target)
5212 return target;
5213 break;
5215 case BUILT_IN_POPCOUNT:
5216 case BUILT_IN_POPCOUNTL:
5217 case BUILT_IN_POPCOUNTLL:
5218 target = expand_builtin_unop (target_mode, arglist, target,
5219 subtarget, popcount_optab);
5220 if (target)
5221 return target;
5222 break;
5224 case BUILT_IN_PARITY:
5225 case BUILT_IN_PARITYL:
5226 case BUILT_IN_PARITYLL:
5227 target = expand_builtin_unop (target_mode, arglist, target,
5228 subtarget, parity_optab);
5229 if (target)
5230 return target;
5231 break;
5233 case BUILT_IN_STRLEN:
5234 target = expand_builtin_strlen (arglist, target, target_mode);
5235 if (target)
5236 return target;
5237 break;
5239 case BUILT_IN_STRCPY:
5240 target = expand_builtin_strcpy (arglist, target, mode);
5241 if (target)
5242 return target;
5243 break;
5245 case BUILT_IN_STRNCPY:
5246 target = expand_builtin_strncpy (arglist, target, mode);
5247 if (target)
5248 return target;
5249 break;
5251 case BUILT_IN_STPCPY:
5252 target = expand_builtin_stpcpy (arglist, target, mode);
5253 if (target)
5254 return target;
5255 break;
5257 case BUILT_IN_STRCAT:
5258 target = expand_builtin_strcat (arglist, target, mode);
5259 if (target)
5260 return target;
5261 break;
5263 case BUILT_IN_STRNCAT:
5264 target = expand_builtin_strncat (arglist, target, mode);
5265 if (target)
5266 return target;
5267 break;
5269 case BUILT_IN_STRSPN:
5270 target = expand_builtin_strspn (arglist, target, mode);
5271 if (target)
5272 return target;
5273 break;
5275 case BUILT_IN_STRCSPN:
5276 target = expand_builtin_strcspn (arglist, target, mode);
5277 if (target)
5278 return target;
5279 break;
5281 case BUILT_IN_STRSTR:
5282 target = expand_builtin_strstr (arglist, target, mode);
5283 if (target)
5284 return target;
5285 break;
5287 case BUILT_IN_STRPBRK:
5288 target = expand_builtin_strpbrk (arglist, target, mode);
5289 if (target)
5290 return target;
5291 break;
5293 case BUILT_IN_INDEX:
5294 case BUILT_IN_STRCHR:
5295 target = expand_builtin_strchr (arglist, target, mode);
5296 if (target)
5297 return target;
5298 break;
5300 case BUILT_IN_RINDEX:
5301 case BUILT_IN_STRRCHR:
5302 target = expand_builtin_strrchr (arglist, target, mode);
5303 if (target)
5304 return target;
5305 break;
5307 case BUILT_IN_MEMCPY:
5308 target = expand_builtin_memcpy (arglist, target, mode);
5309 if (target)
5310 return target;
5311 break;
5313 case BUILT_IN_MEMPCPY:
5314 target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
5315 if (target)
5316 return target;
5317 break;
5319 case BUILT_IN_MEMMOVE:
5320 target = expand_builtin_memmove (arglist, target, mode);
5321 if (target)
5322 return target;
5323 break;
5325 case BUILT_IN_BCOPY:
5326 target = expand_builtin_bcopy (arglist);
5327 if (target)
5328 return target;
5329 break;
5331 case BUILT_IN_MEMSET:
5332 target = expand_builtin_memset (arglist, target, mode);
5333 if (target)
5334 return target;
5335 break;
5337 case BUILT_IN_BZERO:
5338 target = expand_builtin_bzero (arglist);
5339 if (target)
5340 return target;
5341 break;
5343 case BUILT_IN_STRCMP:
5344 target = expand_builtin_strcmp (exp, target, mode);
5345 if (target)
5346 return target;
5347 break;
5349 case BUILT_IN_STRNCMP:
5350 target = expand_builtin_strncmp (exp, target, mode);
5351 if (target)
5352 return target;
5353 break;
5355 case BUILT_IN_BCMP:
5356 case BUILT_IN_MEMCMP:
5357 target = expand_builtin_memcmp (exp, arglist, target, mode);
5358 if (target)
5359 return target;
5360 break;
5362 case BUILT_IN_SETJMP:
5363 target = expand_builtin_setjmp (arglist, target);
5364 if (target)
5365 return target;
5366 break;
5368 /* __builtin_longjmp is passed a pointer to an array of five words.
5369 It's similar to the C library longjmp function but works with
5370 __builtin_setjmp above. */
5371 case BUILT_IN_LONGJMP:
5372 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5373 break;
5374 else
5376 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5377 VOIDmode, 0);
5378 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5379 NULL_RTX, VOIDmode, 0);
5381 if (value != const1_rtx)
5383 error ("__builtin_longjmp second argument must be 1");
5384 return const0_rtx;
5387 expand_builtin_longjmp (buf_addr, value);
5388 return const0_rtx;
5391 case BUILT_IN_TRAP:
5392 expand_builtin_trap ();
5393 return const0_rtx;
5395 case BUILT_IN_PRINTF:
5396 target = expand_builtin_printf (arglist, target, mode, false);
5397 if (target)
5398 return target;
5399 break;
5401 case BUILT_IN_PRINTF_UNLOCKED:
5402 target = expand_builtin_printf (arglist, target, mode, true);
5403 if (target)
5404 return target;
5405 break;
5407 case BUILT_IN_FPUTS:
5408 target = expand_builtin_fputs (arglist, target, false);
5409 if (target)
5410 return target;
5411 break;
5413 case BUILT_IN_FPUTS_UNLOCKED:
5414 target = expand_builtin_fputs (arglist, target, true);
5415 if (target)
5416 return target;
5417 break;
5419 case BUILT_IN_FPRINTF:
5420 target = expand_builtin_fprintf (arglist, target, mode, false);
5421 if (target)
5422 return target;
5423 break;
5425 case BUILT_IN_FPRINTF_UNLOCKED:
5426 target = expand_builtin_fprintf (arglist, target, mode, true);
5427 if (target)
5428 return target;
5429 break;
5431 case BUILT_IN_SPRINTF:
5432 target = expand_builtin_sprintf (arglist, target, mode);
5433 if (target)
5434 return target;
5435 break;
5437 /* Various hooks for the DWARF 2 __throw routine. */
5438 case BUILT_IN_UNWIND_INIT:
5439 expand_builtin_unwind_init ();
5440 return const0_rtx;
5441 case BUILT_IN_DWARF_CFA:
5442 return virtual_cfa_rtx;
5443 #ifdef DWARF2_UNWIND_INFO
5444 case BUILT_IN_DWARF_SP_COLUMN:
5445 return expand_builtin_dwarf_sp_column ();
5446 case BUILT_IN_INIT_DWARF_REG_SIZES:
5447 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5448 return const0_rtx;
5449 #endif
5450 case BUILT_IN_FROB_RETURN_ADDR:
5451 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5452 case BUILT_IN_EXTRACT_RETURN_ADDR:
5453 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5454 case BUILT_IN_EH_RETURN:
5455 expand_builtin_eh_return (TREE_VALUE (arglist),
5456 TREE_VALUE (TREE_CHAIN (arglist)));
5457 return const0_rtx;
5458 #ifdef EH_RETURN_DATA_REGNO
5459 case BUILT_IN_EH_RETURN_DATA_REGNO:
5460 return expand_builtin_eh_return_data_regno (arglist);
5461 #endif
5462 case BUILT_IN_VA_START:
5463 case BUILT_IN_STDARG_START:
5464 return expand_builtin_va_start (arglist);
5465 case BUILT_IN_VA_END:
5466 return expand_builtin_va_end (arglist);
5467 case BUILT_IN_VA_COPY:
5468 return expand_builtin_va_copy (arglist);
5469 case BUILT_IN_EXPECT:
5470 return expand_builtin_expect (arglist, target);
5471 case BUILT_IN_PREFETCH:
5472 expand_builtin_prefetch (arglist);
5473 return const0_rtx;
5476 default: /* just do library call, if unknown builtin */
5477 if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
5478 error ("built-in function `%s' not currently supported",
5479 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5482 /* The switch statement above can drop through to cause the function
5483 to be called normally. */
5484 return expand_call (exp, target, ignore);
5487 /* Determine whether a tree node represents a call to a built-in
5488 math function. If the tree T is a call to a built-in function
5489 taking a single real argument, then the return value is the
5490 DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. Otherwise
5491 the return value is END_BUILTINS. */
5493 enum built_in_function
5494 builtin_mathfn_code (tree t)
5496 tree fndecl, arglist;
5498 if (TREE_CODE (t) != CALL_EXPR
5499 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5500 return END_BUILTINS;
5502 fndecl = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5503 if (TREE_CODE (fndecl) != FUNCTION_DECL
5504 || ! DECL_BUILT_IN (fndecl)
5505 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5506 return END_BUILTINS;
5508 arglist = TREE_OPERAND (t, 1);
5509 if (! arglist
5510 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE)
5511 return END_BUILTINS;
5513 arglist = TREE_CHAIN (arglist);
5514 switch (DECL_FUNCTION_CODE (fndecl))
5516 case BUILT_IN_POW:
5517 case BUILT_IN_POWF:
5518 case BUILT_IN_POWL:
5519 case BUILT_IN_ATAN2:
5520 case BUILT_IN_ATAN2F:
5521 case BUILT_IN_ATAN2L:
5522 if (! arglist
5523 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE
5524 || TREE_CHAIN (arglist))
5525 return END_BUILTINS;
5526 break;
5528 default:
5529 if (arglist)
5530 return END_BUILTINS;
5531 break;
5534 return DECL_FUNCTION_CODE (fndecl);
5537 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5538 constant. ARGLIST is the argument list of the call. */
5540 static tree
5541 fold_builtin_constant_p (tree arglist)
5543 if (arglist == 0)
5544 return 0;
5546 arglist = TREE_VALUE (arglist);
5548 /* We return 1 for a numeric type that's known to be a constant
5549 value at compile-time or for an aggregate type that's a
5550 literal constant. */
5551 STRIP_NOPS (arglist);
5553 /* If we know this is a constant, emit the constant of one. */
5554 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
5555 || (TREE_CODE (arglist) == CONSTRUCTOR
5556 && TREE_CONSTANT (arglist))
5557 || (TREE_CODE (arglist) == ADDR_EXPR
5558 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5559 return integer_one_node;
5561 /* If we aren't going to be running CSE or this expression
5562 has side effects, show we don't know it to be a constant.
5563 Likewise if it's a pointer or aggregate type since in those
5564 case we only want literals, since those are only optimized
5565 when generating RTL, not later.
5566 And finally, if we are compiling an initializer, not code, we
5567 need to return a definite result now; there's not going to be any
5568 more optimization done. */
5569 if (TREE_SIDE_EFFECTS (arglist) || cse_not_expected
5570 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5571 || POINTER_TYPE_P (TREE_TYPE (arglist))
5572 || cfun == 0)
5573 return integer_zero_node;
5575 return 0;
5578 /* Fold a call to __builtin_classify_type. */
5580 static tree
5581 fold_builtin_classify_type (tree arglist)
5583 if (arglist == 0)
5584 return build_int_2 (no_type_class, 0);
5586 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
5589 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5591 static tree
5592 fold_builtin_inf (tree type, int warn)
5594 REAL_VALUE_TYPE real;
5596 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5597 warning ("target format does not support infinity");
5599 real_inf (&real);
5600 return build_real (type, real);
5603 /* Fold a call to __builtin_nan or __builtin_nans. */
5605 static tree
5606 fold_builtin_nan (tree arglist, tree type, int quiet)
5608 REAL_VALUE_TYPE real;
5609 const char *str;
5611 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5612 return 0;
5613 str = c_getstr (TREE_VALUE (arglist));
5614 if (!str)
5615 return 0;
5617 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5618 return 0;
5620 return build_real (type, real);
5623 /* Return true if the floating point expression T has an integer value.
5624 We also allow +Inf, -Inf and NaN to be considered integer values. */
5626 static bool
5627 integer_valued_real_p (tree t)
5629 switch (TREE_CODE (t))
5631 case FLOAT_EXPR:
5632 return true;
5634 case ABS_EXPR:
5635 case SAVE_EXPR:
5636 case NON_LVALUE_EXPR:
5637 return integer_valued_real_p (TREE_OPERAND (t, 0));
5639 case COMPOUND_EXPR:
5640 case MODIFY_EXPR:
5641 case BIND_EXPR:
5642 return integer_valued_real_p (TREE_OPERAND (t, 1));
5644 case PLUS_EXPR:
5645 case MINUS_EXPR:
5646 case MULT_EXPR:
5647 case MIN_EXPR:
5648 case MAX_EXPR:
5649 return integer_valued_real_p (TREE_OPERAND (t, 0))
5650 && integer_valued_real_p (TREE_OPERAND (t, 1));
5652 case COND_EXPR:
5653 return integer_valued_real_p (TREE_OPERAND (t, 1))
5654 && integer_valued_real_p (TREE_OPERAND (t, 2));
5656 case REAL_CST:
5657 if (! TREE_CONSTANT_OVERFLOW (t))
5659 REAL_VALUE_TYPE c, cint;
5661 c = TREE_REAL_CST (t);
5662 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
5663 return real_identical (&c, &cint);
5666 case NOP_EXPR:
5668 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5669 if (TREE_CODE (type) == INTEGER_TYPE)
5670 return true;
5671 if (TREE_CODE (type) == REAL_TYPE)
5672 return integer_valued_real_p (TREE_OPERAND (t, 0));
5673 break;
5676 case CALL_EXPR:
5677 switch (builtin_mathfn_code (t))
5679 case BUILT_IN_CEIL:
5680 case BUILT_IN_CEILF:
5681 case BUILT_IN_CEILL:
5682 case BUILT_IN_FLOOR:
5683 case BUILT_IN_FLOORF:
5684 case BUILT_IN_FLOORL:
5685 case BUILT_IN_NEARBYINT:
5686 case BUILT_IN_NEARBYINTF:
5687 case BUILT_IN_NEARBYINTL:
5688 case BUILT_IN_ROUND:
5689 case BUILT_IN_ROUNDF:
5690 case BUILT_IN_ROUNDL:
5691 case BUILT_IN_TRUNC:
5692 case BUILT_IN_TRUNCF:
5693 case BUILT_IN_TRUNCL:
5694 return true;
5696 default:
5697 break;
5699 break;
5701 default:
5702 break;
5704 return false;
5707 /* EXP is assumed to be builtin call where truncation can be propagated
5708 across (for instance floor((double)f) == (double)floorf (f).
5709 Do the transformation. */
5711 static tree
5712 fold_trunc_transparent_mathfn (tree exp)
5714 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5715 tree arglist = TREE_OPERAND (exp, 1);
5716 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5717 tree arg;
5719 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5720 return 0;
5722 arg = TREE_VALUE (arglist);
5723 /* Integer rounding functions are idempotent. */
5724 if (fcode == builtin_mathfn_code (arg))
5725 return arg;
5727 /* If argument is already integer valued, and we don't need to worry
5728 about setting errno, there's no need to perform rounding. */
5729 if (! flag_errno_math && integer_valued_real_p (arg))
5730 return arg;
5732 if (optimize)
5734 tree arg0 = strip_float_extensions (arg);
5735 tree ftype = TREE_TYPE (exp);
5736 tree newtype = TREE_TYPE (arg0);
5737 tree decl;
5739 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
5740 && (decl = mathfn_built_in (newtype, fcode)))
5742 arglist =
5743 build_tree_list (NULL_TREE, fold (convert (newtype, arg0)));
5744 return convert (ftype,
5745 build_function_call_expr (decl, arglist));
5748 return 0;
5751 /* Fold function call to builtin cabs, cabsf or cabsl. FNDECL is the
5752 function's DECL, ARGLIST is the argument list and TYPE is the return
5753 type. Return NULL_TREE if no simplification can be made. */
5755 static tree
5756 fold_builtin_cabs (tree fndecl, tree arglist, tree type)
5758 tree arg;
5760 if (!arglist || TREE_CHAIN (arglist))
5761 return NULL_TREE;
5763 arg = TREE_VALUE (arglist);
5764 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
5765 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
5766 return NULL_TREE;
5768 /* Evaluate cabs of a constant at compile-time. */
5769 if (flag_unsafe_math_optimizations
5770 && TREE_CODE (arg) == COMPLEX_CST
5771 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
5772 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
5773 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
5774 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
5776 REAL_VALUE_TYPE r, i;
5778 r = TREE_REAL_CST (TREE_REALPART (arg));
5779 i = TREE_REAL_CST (TREE_IMAGPART (arg));
5781 real_arithmetic (&r, MULT_EXPR, &r, &r);
5782 real_arithmetic (&i, MULT_EXPR, &i, &i);
5783 real_arithmetic (&r, PLUS_EXPR, &r, &i);
5784 if (real_sqrt (&r, TYPE_MODE (type), &r)
5785 || ! flag_trapping_math)
5786 return build_real (type, r);
5789 /* If either part is zero, cabs is fabs of the other. */
5790 if (TREE_CODE (arg) == COMPLEX_EXPR
5791 && real_zerop (TREE_OPERAND (arg, 0)))
5792 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
5793 if (TREE_CODE (arg) == COMPLEX_EXPR
5794 && real_zerop (TREE_OPERAND (arg, 1)))
5795 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
5797 if (flag_unsafe_math_optimizations)
5799 enum built_in_function fcode;
5800 tree sqrtfn;
5802 fcode = DECL_FUNCTION_CODE (fndecl);
5803 if (fcode == BUILT_IN_CABS)
5804 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT];
5805 else if (fcode == BUILT_IN_CABSF)
5806 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF];
5807 else if (fcode == BUILT_IN_CABSL)
5808 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL];
5809 else
5810 sqrtfn = NULL_TREE;
5812 if (sqrtfn != NULL_TREE)
5814 tree rpart, ipart, result, arglist;
5816 rpart = fold (build1 (REALPART_EXPR, type, arg));
5817 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
5819 rpart = save_expr (rpart);
5820 ipart = save_expr (ipart);
5822 result = fold (build (PLUS_EXPR, type,
5823 fold (build (MULT_EXPR, type,
5824 rpart, rpart)),
5825 fold (build (MULT_EXPR, type,
5826 ipart, ipart))));
5828 arglist = build_tree_list (NULL_TREE, result);
5829 return build_function_call_expr (sqrtfn, arglist);
5833 return NULL_TREE;
5836 /* Fold function call to builtin trunc, truncf or truncl. Return
5837 NULL_TREE if no simplification can be made. */
5839 static tree
5840 fold_builtin_trunc (tree exp)
5842 tree arglist = TREE_OPERAND (exp, 1);
5843 tree arg;
5845 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5846 return 0;
5848 /* Optimize trunc of constant value. */
5849 arg = TREE_VALUE (arglist);
5850 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5852 REAL_VALUE_TYPE r, x;
5853 tree type = TREE_TYPE (exp);
5855 x = TREE_REAL_CST (arg);
5856 real_trunc (&r, TYPE_MODE (type), &x);
5857 return build_real (type, r);
5860 return fold_trunc_transparent_mathfn (exp);
5863 /* Fold function call to builtin floor, floorf or floorl. Return
5864 NULL_TREE if no simplification can be made. */
5866 static tree
5867 fold_builtin_floor (tree exp)
5869 tree arglist = TREE_OPERAND (exp, 1);
5870 tree arg;
5872 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5873 return 0;
5875 /* Optimize floor of constant value. */
5876 arg = TREE_VALUE (arglist);
5877 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5879 REAL_VALUE_TYPE x;
5881 x = TREE_REAL_CST (arg);
5882 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
5884 tree type = TREE_TYPE (exp);
5885 REAL_VALUE_TYPE r;
5887 real_floor (&r, TYPE_MODE (type), &x);
5888 return build_real (type, r);
5892 return fold_trunc_transparent_mathfn (exp);
5895 /* Fold function call to builtin ceil, ceilf or ceill. Return
5896 NULL_TREE if no simplification can be made. */
5898 static tree
5899 fold_builtin_ceil (tree exp)
5901 tree arglist = TREE_OPERAND (exp, 1);
5902 tree arg;
5904 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5905 return 0;
5907 /* Optimize ceil of constant value. */
5908 arg = TREE_VALUE (arglist);
5909 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
5911 REAL_VALUE_TYPE x;
5913 x = TREE_REAL_CST (arg);
5914 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
5916 tree type = TREE_TYPE (exp);
5917 REAL_VALUE_TYPE r;
5919 real_ceil (&r, TYPE_MODE (type), &x);
5920 return build_real (type, r);
5924 return fold_trunc_transparent_mathfn (exp);
5927 /* Used by constant folding to eliminate some builtin calls early. EXP is
5928 the CALL_EXPR of a call to a builtin function. */
5930 tree
5931 fold_builtin (tree exp)
5933 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5934 tree arglist = TREE_OPERAND (exp, 1);
5935 tree type = TREE_TYPE (TREE_TYPE (fndecl));
5937 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5938 return 0;
5940 switch (DECL_FUNCTION_CODE (fndecl))
5942 case BUILT_IN_CONSTANT_P:
5943 return fold_builtin_constant_p (arglist);
5945 case BUILT_IN_CLASSIFY_TYPE:
5946 return fold_builtin_classify_type (arglist);
5948 case BUILT_IN_STRLEN:
5949 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5951 tree len = c_strlen (TREE_VALUE (arglist), 0);
5952 if (len)
5954 /* Convert from the internal "sizetype" type to "size_t". */
5955 if (size_type_node)
5956 len = convert (size_type_node, len);
5957 return len;
5960 break;
5962 case BUILT_IN_FABS:
5963 case BUILT_IN_FABSF:
5964 case BUILT_IN_FABSL:
5965 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5966 return fold (build1 (ABS_EXPR, type, TREE_VALUE (arglist)));
5967 break;
5969 case BUILT_IN_CABS:
5970 case BUILT_IN_CABSF:
5971 case BUILT_IN_CABSL:
5972 return fold_builtin_cabs (fndecl, arglist, type);
5974 case BUILT_IN_SQRT:
5975 case BUILT_IN_SQRTF:
5976 case BUILT_IN_SQRTL:
5977 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5979 enum built_in_function fcode;
5980 tree arg = TREE_VALUE (arglist);
5982 /* Optimize sqrt of constant value. */
5983 if (TREE_CODE (arg) == REAL_CST
5984 && ! TREE_CONSTANT_OVERFLOW (arg))
5986 REAL_VALUE_TYPE r, x;
5988 x = TREE_REAL_CST (arg);
5989 if (real_sqrt (&r, TYPE_MODE (type), &x)
5990 || (!flag_trapping_math && !flag_errno_math))
5991 return build_real (type, r);
5994 /* Optimize sqrt(exp(x)) = exp(x*0.5). */
5995 fcode = builtin_mathfn_code (arg);
5996 if (flag_unsafe_math_optimizations
5997 && (fcode == BUILT_IN_EXP
5998 || fcode == BUILT_IN_EXPF
5999 || fcode == BUILT_IN_EXPL))
6001 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6002 arg = fold (build (MULT_EXPR, type,
6003 TREE_VALUE (TREE_OPERAND (arg, 1)),
6004 build_real (type, dconsthalf)));
6005 arglist = build_tree_list (NULL_TREE, arg);
6006 return build_function_call_expr (expfn, arglist);
6009 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
6010 if (flag_unsafe_math_optimizations
6011 && (fcode == BUILT_IN_POW
6012 || fcode == BUILT_IN_POWF
6013 || fcode == BUILT_IN_POWL))
6015 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6016 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6017 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6018 tree narg1 = fold (build (MULT_EXPR, type, arg1,
6019 build_real (type, dconsthalf)));
6020 arglist = tree_cons (NULL_TREE, arg0,
6021 build_tree_list (NULL_TREE, narg1));
6022 return build_function_call_expr (powfn, arglist);
6025 break;
6027 case BUILT_IN_SIN:
6028 case BUILT_IN_SINF:
6029 case BUILT_IN_SINL:
6030 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6032 tree arg = TREE_VALUE (arglist);
6034 /* Optimize sin(0.0) = 0.0. */
6035 if (real_zerop (arg))
6036 return arg;
6038 break;
6040 case BUILT_IN_COS:
6041 case BUILT_IN_COSF:
6042 case BUILT_IN_COSL:
6043 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6045 tree arg = TREE_VALUE (arglist);
6047 /* Optimize cos(0.0) = 1.0. */
6048 if (real_zerop (arg))
6049 return build_real (type, dconst1);
6051 /* Optimize cos(-x) into cos(x). */
6052 if (TREE_CODE (arg) == NEGATE_EXPR)
6054 tree arglist = build_tree_list (NULL_TREE,
6055 TREE_OPERAND (arg, 0));
6056 return build_function_call_expr (fndecl, arglist);
6059 break;
6061 case BUILT_IN_EXP:
6062 case BUILT_IN_EXPF:
6063 case BUILT_IN_EXPL:
6064 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6066 enum built_in_function fcode;
6067 tree arg = TREE_VALUE (arglist);
6069 /* Optimize exp(0.0) = 1.0. */
6070 if (real_zerop (arg))
6071 return build_real (type, dconst1);
6073 /* Optimize exp(1.0) = e. */
6074 if (real_onep (arg))
6076 REAL_VALUE_TYPE cst;
6078 if (! builtin_dconsts_init)
6079 init_builtin_dconsts ();
6080 real_convert (&cst, TYPE_MODE (type), &dconste);
6081 return build_real (type, cst);
6084 /* Attempt to evaluate exp at compile-time. */
6085 if (flag_unsafe_math_optimizations
6086 && TREE_CODE (arg) == REAL_CST
6087 && ! TREE_CONSTANT_OVERFLOW (arg))
6089 REAL_VALUE_TYPE cint;
6090 REAL_VALUE_TYPE c;
6091 HOST_WIDE_INT n;
6093 c = TREE_REAL_CST (arg);
6094 n = real_to_integer (&c);
6095 real_from_integer (&cint, VOIDmode, n,
6096 n < 0 ? -1 : 0, 0);
6097 if (real_identical (&c, &cint))
6099 REAL_VALUE_TYPE x;
6101 if (! builtin_dconsts_init)
6102 init_builtin_dconsts ();
6103 real_powi (&x, TYPE_MODE (type), &dconste, n);
6104 return build_real (type, x);
6108 /* Optimize exp(log(x)) = x. */
6109 fcode = builtin_mathfn_code (arg);
6110 if (flag_unsafe_math_optimizations
6111 && (fcode == BUILT_IN_LOG
6112 || fcode == BUILT_IN_LOGF
6113 || fcode == BUILT_IN_LOGL))
6114 return TREE_VALUE (TREE_OPERAND (arg, 1));
6116 break;
6118 case BUILT_IN_LOG:
6119 case BUILT_IN_LOGF:
6120 case BUILT_IN_LOGL:
6121 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6123 enum built_in_function fcode;
6124 tree arg = TREE_VALUE (arglist);
6126 /* Optimize log(1.0) = 0.0. */
6127 if (real_onep (arg))
6128 return build_real (type, dconst0);
6130 /* Optimize log(exp(x)) = x. */
6131 fcode = builtin_mathfn_code (arg);
6132 if (flag_unsafe_math_optimizations
6133 && (fcode == BUILT_IN_EXP
6134 || fcode == BUILT_IN_EXPF
6135 || fcode == BUILT_IN_EXPL))
6136 return TREE_VALUE (TREE_OPERAND (arg, 1));
6138 /* Optimize log(sqrt(x)) = log(x)*0.5. */
6139 if (flag_unsafe_math_optimizations
6140 && (fcode == BUILT_IN_SQRT
6141 || fcode == BUILT_IN_SQRTF
6142 || fcode == BUILT_IN_SQRTL))
6144 tree logfn = build_function_call_expr (fndecl,
6145 TREE_OPERAND (arg, 1));
6146 return fold (build (MULT_EXPR, type, logfn,
6147 build_real (type, dconsthalf)));
6150 /* Optimize log(pow(x,y)) = y*log(x). */
6151 if (flag_unsafe_math_optimizations
6152 && (fcode == BUILT_IN_POW
6153 || fcode == BUILT_IN_POWF
6154 || fcode == BUILT_IN_POWL))
6156 tree arg0, arg1, logfn;
6158 arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6159 arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6160 arglist = build_tree_list (NULL_TREE, arg0);
6161 logfn = build_function_call_expr (fndecl, arglist);
6162 return fold (build (MULT_EXPR, type, arg1, logfn));
6165 break;
6167 case BUILT_IN_TAN:
6168 case BUILT_IN_TANF:
6169 case BUILT_IN_TANL:
6170 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6172 enum built_in_function fcode;
6173 tree arg = TREE_VALUE (arglist);
6175 /* Optimize tan(0.0) = 0.0. */
6176 if (real_zerop (arg))
6177 return arg;
6179 /* Optimize tan(atan(x)) = x. */
6180 fcode = builtin_mathfn_code (arg);
6181 if (flag_unsafe_math_optimizations
6182 && (fcode == BUILT_IN_ATAN
6183 || fcode == BUILT_IN_ATANF
6184 || fcode == BUILT_IN_ATANL))
6185 return TREE_VALUE (TREE_OPERAND (arg, 1));
6187 break;
6189 case BUILT_IN_ATAN:
6190 case BUILT_IN_ATANF:
6191 case BUILT_IN_ATANL:
6192 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6194 tree arg = TREE_VALUE (arglist);
6196 /* Optimize atan(0.0) = 0.0. */
6197 if (real_zerop (arg))
6198 return arg;
6200 /* Optimize atan(1.0) = pi/4. */
6201 if (real_onep (arg))
6203 REAL_VALUE_TYPE cst;
6205 if (! builtin_dconsts_init)
6206 init_builtin_dconsts ();
6207 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6208 cst.exp -= 2;
6209 return build_real (type, cst);
6212 break;
6214 case BUILT_IN_POW:
6215 case BUILT_IN_POWF:
6216 case BUILT_IN_POWL:
6217 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6219 enum built_in_function fcode;
6220 tree arg0 = TREE_VALUE (arglist);
6221 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6223 /* Optimize pow(1.0,y) = 1.0. */
6224 if (real_onep (arg0))
6225 return omit_one_operand (type, build_real (type, dconst1), arg1);
6227 if (TREE_CODE (arg1) == REAL_CST
6228 && ! TREE_CONSTANT_OVERFLOW (arg1))
6230 REAL_VALUE_TYPE c;
6231 c = TREE_REAL_CST (arg1);
6233 /* Optimize pow(x,0.0) = 1.0. */
6234 if (REAL_VALUES_EQUAL (c, dconst0))
6235 return omit_one_operand (type, build_real (type, dconst1),
6236 arg0);
6238 /* Optimize pow(x,1.0) = x. */
6239 if (REAL_VALUES_EQUAL (c, dconst1))
6240 return arg0;
6242 /* Optimize pow(x,-1.0) = 1.0/x. */
6243 if (REAL_VALUES_EQUAL (c, dconstm1))
6244 return fold (build (RDIV_EXPR, type,
6245 build_real (type, dconst1),
6246 arg0));
6248 /* Optimize pow(x,2.0) = x*x. */
6249 if (REAL_VALUES_EQUAL (c, dconst2)
6250 && (*lang_hooks.decls.global_bindings_p) () == 0
6251 && ! CONTAINS_PLACEHOLDER_P (arg0))
6253 arg0 = save_expr (arg0);
6254 return fold (build (MULT_EXPR, type, arg0, arg0));
6257 /* Optimize pow(x,-2.0) = 1.0/(x*x). */
6258 if (flag_unsafe_math_optimizations
6259 && REAL_VALUES_EQUAL (c, dconstm2)
6260 && (*lang_hooks.decls.global_bindings_p) () == 0
6261 && ! CONTAINS_PLACEHOLDER_P (arg0))
6263 arg0 = save_expr (arg0);
6264 return fold (build (RDIV_EXPR, type,
6265 build_real (type, dconst1),
6266 fold (build (MULT_EXPR, type,
6267 arg0, arg0))));
6270 /* Optimize pow(x,0.5) = sqrt(x). */
6271 if (flag_unsafe_math_optimizations
6272 && REAL_VALUES_EQUAL (c, dconsthalf))
6274 tree sqrtfn;
6276 fcode = DECL_FUNCTION_CODE (fndecl);
6277 if (fcode == BUILT_IN_POW)
6278 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT];
6279 else if (fcode == BUILT_IN_POWF)
6280 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF];
6281 else if (fcode == BUILT_IN_POWL)
6282 sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL];
6283 else
6284 sqrtfn = NULL_TREE;
6286 if (sqrtfn != NULL_TREE)
6288 tree arglist = build_tree_list (NULL_TREE, arg0);
6289 return build_function_call_expr (sqrtfn, arglist);
6293 /* Attempt to evaluate pow at compile-time. */
6294 if (TREE_CODE (arg0) == REAL_CST
6295 && ! TREE_CONSTANT_OVERFLOW (arg0))
6297 REAL_VALUE_TYPE cint;
6298 HOST_WIDE_INT n;
6300 n = real_to_integer (&c);
6301 real_from_integer (&cint, VOIDmode, n,
6302 n < 0 ? -1 : 0, 0);
6303 if (real_identical (&c, &cint))
6305 REAL_VALUE_TYPE x;
6306 bool inexact;
6308 x = TREE_REAL_CST (arg0);
6309 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6310 if (flag_unsafe_math_optimizations || !inexact)
6311 return build_real (type, x);
6316 /* Optimize pow(exp(x),y) = exp(x*y). */
6317 fcode = builtin_mathfn_code (arg0);
6318 if (flag_unsafe_math_optimizations
6319 && (fcode == BUILT_IN_EXP
6320 || fcode == BUILT_IN_EXPF
6321 || fcode == BUILT_IN_EXPL))
6323 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6324 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6325 arg = fold (build (MULT_EXPR, type, arg, arg1));
6326 arglist = build_tree_list (NULL_TREE, arg);
6327 return build_function_call_expr (expfn, arglist);
6330 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6331 if (flag_unsafe_math_optimizations
6332 && (fcode == BUILT_IN_SQRT
6333 || fcode == BUILT_IN_SQRTF
6334 || fcode == BUILT_IN_SQRTL))
6336 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6337 tree narg1 = fold (build (MULT_EXPR, type, arg1,
6338 build_real (type, dconsthalf)));
6340 arglist = tree_cons (NULL_TREE, narg0,
6341 build_tree_list (NULL_TREE, narg1));
6342 return build_function_call_expr (fndecl, arglist);
6345 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
6346 if (flag_unsafe_math_optimizations
6347 && (fcode == BUILT_IN_POW
6348 || fcode == BUILT_IN_POWF
6349 || fcode == BUILT_IN_POWL))
6351 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6352 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6353 tree narg1 = fold (build (MULT_EXPR, type, arg01, arg1));
6354 arglist = tree_cons (NULL_TREE, arg00,
6355 build_tree_list (NULL_TREE, narg1));
6356 return build_function_call_expr (fndecl, arglist);
6359 break;
6361 case BUILT_IN_INF:
6362 case BUILT_IN_INFF:
6363 case BUILT_IN_INFL:
6364 return fold_builtin_inf (type, true);
6366 case BUILT_IN_HUGE_VAL:
6367 case BUILT_IN_HUGE_VALF:
6368 case BUILT_IN_HUGE_VALL:
6369 return fold_builtin_inf (type, false);
6371 case BUILT_IN_NAN:
6372 case BUILT_IN_NANF:
6373 case BUILT_IN_NANL:
6374 return fold_builtin_nan (arglist, type, true);
6376 case BUILT_IN_NANS:
6377 case BUILT_IN_NANSF:
6378 case BUILT_IN_NANSL:
6379 return fold_builtin_nan (arglist, type, false);
6381 case BUILT_IN_FLOOR:
6382 case BUILT_IN_FLOORF:
6383 case BUILT_IN_FLOORL:
6384 return fold_builtin_floor (exp);
6386 case BUILT_IN_CEIL:
6387 case BUILT_IN_CEILF:
6388 case BUILT_IN_CEILL:
6389 return fold_builtin_ceil (exp);
6391 case BUILT_IN_TRUNC:
6392 case BUILT_IN_TRUNCF:
6393 case BUILT_IN_TRUNCL:
6394 return fold_builtin_trunc (exp);
6396 case BUILT_IN_ROUND:
6397 case BUILT_IN_ROUNDF:
6398 case BUILT_IN_ROUNDL:
6399 case BUILT_IN_NEARBYINT:
6400 case BUILT_IN_NEARBYINTF:
6401 case BUILT_IN_NEARBYINTL:
6402 return fold_trunc_transparent_mathfn (exp);
6404 default:
6405 break;
6408 return 0;
6411 /* Conveniently construct a function call expression. */
6413 tree
6414 build_function_call_expr (tree fn, tree arglist)
6416 tree call_expr;
6418 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
6419 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
6420 call_expr, arglist);
6421 TREE_SIDE_EFFECTS (call_expr) = 1;
6422 return fold (call_expr);
6425 /* This function validates the types of a function call argument list
6426 represented as a tree chain of parameters against a specified list
6427 of tree_codes. If the last specifier is a 0, that represents an
6428 ellipses, otherwise the last specifier must be a VOID_TYPE. */
6430 static int
6431 validate_arglist (tree arglist, ...)
6433 enum tree_code code;
6434 int res = 0;
6435 va_list ap;
6437 va_start (ap, arglist);
6441 code = va_arg (ap, enum tree_code);
6442 switch (code)
6444 case 0:
6445 /* This signifies an ellipses, any further arguments are all ok. */
6446 res = 1;
6447 goto end;
6448 case VOID_TYPE:
6449 /* This signifies an endlink, if no arguments remain, return
6450 true, otherwise return false. */
6451 res = arglist == 0;
6452 goto end;
6453 default:
6454 /* If no parameters remain or the parameter's code does not
6455 match the specified code, return false. Otherwise continue
6456 checking any remaining arguments. */
6457 if (arglist == 0
6458 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
6459 goto end;
6460 break;
6462 arglist = TREE_CHAIN (arglist);
6464 while (1);
6466 /* We need gotos here since we can only have one VA_CLOSE in a
6467 function. */
6468 end: ;
6469 va_end (ap);
6471 return res;
6474 /* Default version of target-specific builtin setup that does nothing. */
6476 void
6477 default_init_builtins (void)
6481 /* Default target-specific builtin expander that does nothing. */
6484 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
6485 rtx target ATTRIBUTE_UNUSED,
6486 rtx subtarget ATTRIBUTE_UNUSED,
6487 enum machine_mode mode ATTRIBUTE_UNUSED,
6488 int ignore ATTRIBUTE_UNUSED)
6490 return NULL_RTX;
6493 /* Instantiate all remaining CONSTANT_P_RTX nodes. */
6495 void
6496 purge_builtin_constant_p (void)
6498 rtx insn, set, arg, new, note;
6500 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6501 if (INSN_P (insn)
6502 && (set = single_set (insn)) != NULL_RTX
6503 && (GET_CODE (arg = SET_SRC (set)) == CONSTANT_P_RTX
6504 || (GET_CODE (arg) == SUBREG
6505 && (GET_CODE (arg = SUBREG_REG (arg))
6506 == CONSTANT_P_RTX))))
6508 arg = XEXP (arg, 0);
6509 new = CONSTANT_P (arg) ? const1_rtx : const0_rtx;
6510 validate_change (insn, &SET_SRC (set), new, 0);
6512 /* Remove the REG_EQUAL note from the insn. */
6513 if ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0)
6514 remove_note (insn, note);
6518 /* Returns true is EXP represents data that would potentially reside
6519 in a readonly section. */
6521 static bool
6522 readonly_data_expr (tree exp)
6524 STRIP_NOPS (exp);
6526 if (TREE_CODE (exp) == ADDR_EXPR)
6527 return decl_readonly_section (TREE_OPERAND (exp, 0), 0);
6528 else
6529 return false;