1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
31 #include "tree-gimple.h"
34 #include "hard-reg-set.h"
37 #include "insn-config.h"
43 #include "typeclass.h"
48 #include "langhooks.h"
49 #include "basic-block.h"
50 #include "tree-mudflap.h"
51 #include "tree-flow.h"
52 #include "value-prof.h"
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
65 #include "builtins.def"
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
77 static const char *c_getstr (tree);
78 static rtx c_readstr (const char *, enum machine_mode);
79 static int target_char_cast (tree, char *);
80 static rtx get_memory_rtx (tree, tree);
81 static int apply_args_size (void);
82 static int apply_result_size (void);
83 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
84 static rtx result_vector (int, rtx);
86 static void expand_builtin_update_setjmp_buf (rtx);
87 static void expand_builtin_prefetch (tree);
88 static rtx expand_builtin_apply_args (void);
89 static rtx expand_builtin_apply_args_1 (void);
90 static rtx expand_builtin_apply (rtx, rtx, rtx);
91 static void expand_builtin_return (rtx);
92 static enum type_class type_to_class (tree);
93 static rtx expand_builtin_classify_type (tree);
94 static void expand_errno_check (tree, rtx);
95 static rtx expand_builtin_mathfn (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
97 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
98 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_sincos (tree);
100 static rtx expand_builtin_cexpi (tree, rtx, rtx);
101 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
102 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
103 static rtx expand_builtin_args_info (tree);
104 static rtx expand_builtin_next_arg (void);
105 static rtx expand_builtin_va_start (tree);
106 static rtx expand_builtin_va_end (tree);
107 static rtx expand_builtin_va_copy (tree);
108 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
111 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
112 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx,
119 enum machine_mode, int);
120 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int);
121 static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx,
122 enum machine_mode, int);
123 static rtx expand_builtin_bcopy (tree, int);
124 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
127 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
128 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
129 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
130 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
132 static rtx expand_builtin_bzero (tree);
133 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
136 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_alloca (tree, rtx);
139 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
140 static rtx expand_builtin_frame_address (tree, tree);
141 static rtx expand_builtin_fputs (tree, rtx, bool);
142 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
143 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
144 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
145 static tree stabilize_va_list (tree, int);
146 static rtx expand_builtin_expect (tree, rtx);
147 static tree fold_builtin_constant_p (tree);
148 static tree fold_builtin_expect (tree);
149 static tree fold_builtin_classify_type (tree);
150 static tree fold_builtin_strlen (tree);
151 static tree fold_builtin_inf (tree, int);
152 static tree fold_builtin_nan (tree, tree, int);
153 static tree rewrite_call_expr (tree, int, tree, int, ...);
154 static bool validate_arg (tree, enum tree_code code);
155 static bool integer_valued_real_p (tree);
156 static tree fold_trunc_transparent_mathfn (tree, tree);
157 static bool readonly_data_expr (tree);
158 static rtx expand_builtin_fabs (tree, rtx, rtx);
159 static rtx expand_builtin_signbit (tree, rtx);
160 static tree fold_builtin_sqrt (tree, tree);
161 static tree fold_builtin_cbrt (tree, tree);
162 static tree fold_builtin_pow (tree, tree, tree, tree);
163 static tree fold_builtin_powi (tree, tree, tree, tree);
164 static tree fold_builtin_cos (tree, tree, tree);
165 static tree fold_builtin_cosh (tree, tree, tree);
166 static tree fold_builtin_tan (tree, tree);
167 static tree fold_builtin_trunc (tree, tree);
168 static tree fold_builtin_floor (tree, tree);
169 static tree fold_builtin_ceil (tree, tree);
170 static tree fold_builtin_round (tree, tree);
171 static tree fold_builtin_int_roundingfn (tree, tree);
172 static tree fold_builtin_bitop (tree, tree);
173 static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int);
174 static tree fold_builtin_strchr (tree, tree, tree);
175 static tree fold_builtin_memcmp (tree, tree, tree);
176 static tree fold_builtin_strcmp (tree, tree);
177 static tree fold_builtin_strncmp (tree, tree, tree);
178 static tree fold_builtin_signbit (tree, tree);
179 static tree fold_builtin_copysign (tree, tree, tree, tree);
180 static tree fold_builtin_isascii (tree);
181 static tree fold_builtin_toascii (tree);
182 static tree fold_builtin_isdigit (tree);
183 static tree fold_builtin_fabs (tree, tree);
184 static tree fold_builtin_abs (tree, tree);
185 static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code,
187 static tree fold_builtin_n (tree, tree *, int, bool);
188 static tree fold_builtin_0 (tree, bool);
189 static tree fold_builtin_1 (tree, tree, bool);
190 static tree fold_builtin_2 (tree, tree, tree, bool);
191 static tree fold_builtin_3 (tree, tree, tree, tree, bool);
192 static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool);
193 static tree fold_builtin_varargs (tree, tree, bool);
195 static tree fold_builtin_strpbrk (tree, tree, tree);
196 static tree fold_builtin_strstr (tree, tree, tree);
197 static tree fold_builtin_strrchr (tree, tree, tree);
198 static tree fold_builtin_strcat (tree, tree);
199 static tree fold_builtin_strncat (tree, tree, tree);
200 static tree fold_builtin_strspn (tree, tree);
201 static tree fold_builtin_strcspn (tree, tree);
202 static tree fold_builtin_sprintf (tree, tree, tree, int);
204 static rtx expand_builtin_object_size (tree);
205 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
206 enum built_in_function);
207 static void maybe_emit_chk_warning (tree, enum built_in_function);
208 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
209 static tree fold_builtin_object_size (tree, tree);
210 static tree fold_builtin_strcat_chk (tree, tree, tree, tree);
211 static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree);
212 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
213 static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function);
214 static tree fold_builtin_fprintf (tree, tree, tree, tree, bool,
215 enum built_in_function);
216 static bool init_target_chars (void);
218 static unsigned HOST_WIDE_INT target_newline;
219 static unsigned HOST_WIDE_INT target_percent;
220 static unsigned HOST_WIDE_INT target_c;
221 static unsigned HOST_WIDE_INT target_s;
222 static char target_percent_c[3];
223 static char target_percent_s[3];
224 static char target_percent_s_newline[4];
225 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
226 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
227 static tree do_mpfr_arg2 (tree, tree, tree,
228 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
229 static tree do_mpfr_arg3 (tree, tree, tree, tree,
230 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
231 static tree do_mpfr_sincos (tree, tree, tree);
233 /* Return true if NODE should be considered for inline expansion regardless
234 of the optimization level. This means whenever a function is invoked with
235 its "internal" name, which normally contains the prefix "__builtin". */
237 static bool called_as_built_in (tree node)
239 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
240 if (strncmp (name, "__builtin_", 10) == 0)
242 if (strncmp (name, "__sync_", 7) == 0)
247 /* Return the alignment in bits of EXP, a pointer valued expression.
248 But don't return more than MAX_ALIGN no matter what.
249 The alignment returned is, by default, the alignment of the thing that
250 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
252 Otherwise, look at the expression to see if we can do better, i.e., if the
253 expression is actually pointing at an object whose alignment is tighter. */
256 get_pointer_alignment (tree exp, unsigned int max_align)
258 unsigned int align, inner;
260 /* We rely on TER to compute accurate alignment information. */
261 if (!(optimize && flag_tree_ter))
264 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
267 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
268 align = MIN (align, max_align);
272 switch (TREE_CODE (exp))
276 case NON_LVALUE_EXPR:
277 exp = TREE_OPERAND (exp, 0);
278 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
281 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
282 align = MIN (inner, max_align);
286 /* If sum of pointer + int, restrict our maximum alignment to that
287 imposed by the integer. If not, we can't do any better than
289 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
292 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
293 & (max_align / BITS_PER_UNIT - 1))
297 exp = TREE_OPERAND (exp, 0);
301 /* See what we are pointing at and look at its alignment. */
302 exp = TREE_OPERAND (exp, 0);
304 if (handled_component_p (exp))
306 HOST_WIDE_INT bitsize, bitpos;
308 enum machine_mode mode;
309 int unsignedp, volatilep;
311 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
312 &mode, &unsignedp, &volatilep, true);
314 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
315 if (offset && TREE_CODE (offset) == PLUS_EXPR
316 && host_integerp (TREE_OPERAND (offset, 1), 1))
318 /* Any overflow in calculating offset_bits won't change
321 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
325 inner = MIN (inner, (offset_bits & -offset_bits));
326 offset = TREE_OPERAND (offset, 0);
328 if (offset && TREE_CODE (offset) == MULT_EXPR
329 && host_integerp (TREE_OPERAND (offset, 1), 1))
331 /* Any overflow in calculating offset_factor won't change
333 unsigned offset_factor
334 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
338 inner = MIN (inner, (offset_factor & -offset_factor));
341 inner = MIN (inner, BITS_PER_UNIT);
343 if (TREE_CODE (exp) == FUNCTION_DECL)
344 align = FUNCTION_BOUNDARY;
345 else if (DECL_P (exp))
346 align = MIN (inner, DECL_ALIGN (exp));
347 #ifdef CONSTANT_ALIGNMENT
348 else if (CONSTANT_CLASS_P (exp))
349 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
351 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
352 || TREE_CODE (exp) == INDIRECT_REF)
353 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
355 align = MIN (align, inner);
356 return MIN (align, max_align);
364 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
365 way, because it could contain a zero byte in the middle.
366 TREE_STRING_LENGTH is the size of the character array, not the string.
368 ONLY_VALUE should be nonzero if the result is not going to be emitted
369 into the instruction stream and zero if it is going to be expanded.
370 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
371 is returned, otherwise NULL, since
372 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
373 evaluate the side-effects.
375 The value returned is of type `ssizetype'.
377 Unfortunately, string_constant can't access the values of const char
378 arrays with initializers, so neither can we do so here. */
381 c_strlen (tree src, int only_value)
384 HOST_WIDE_INT offset;
389 if (TREE_CODE (src) == COND_EXPR
390 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
394 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
395 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
396 if (tree_int_cst_equal (len1, len2))
400 if (TREE_CODE (src) == COMPOUND_EXPR
401 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
402 return c_strlen (TREE_OPERAND (src, 1), only_value);
404 src = string_constant (src, &offset_node);
408 max = TREE_STRING_LENGTH (src) - 1;
409 ptr = TREE_STRING_POINTER (src);
411 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
413 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
414 compute the offset to the following null if we don't know where to
415 start searching for it. */
418 for (i = 0; i < max; i++)
422 /* We don't know the starting offset, but we do know that the string
423 has no internal zero bytes. We can assume that the offset falls
424 within the bounds of the string; otherwise, the programmer deserves
425 what he gets. Subtract the offset from the length of the string,
426 and return that. This would perhaps not be valid if we were dealing
427 with named arrays in addition to literal string constants. */
429 return size_diffop (size_int (max), offset_node);
432 /* We have a known offset into the string. Start searching there for
433 a null character if we can represent it as a single HOST_WIDE_INT. */
434 if (offset_node == 0)
436 else if (! host_integerp (offset_node, 0))
439 offset = tree_low_cst (offset_node, 0);
441 /* If the offset is known to be out of bounds, warn, and call strlen at
443 if (offset < 0 || offset > max)
445 warning (0, "offset outside bounds of constant string");
449 /* Use strlen to search for the first zero byte. Since any strings
450 constructed with build_string will have nulls appended, we win even
451 if we get handed something like (char[4])"abcd".
453 Since OFFSET is our starting index into the string, no further
454 calculation is needed. */
455 return ssize_int (strlen (ptr + offset));
458 /* Return a char pointer for a C string if it is a string constant
459 or sum of string constant and integer constant. */
466 src = string_constant (src, &offset_node);
470 if (offset_node == 0)
471 return TREE_STRING_POINTER (src);
472 else if (!host_integerp (offset_node, 1)
473 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
476 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
479 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
480 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
483 c_readstr (const char *str, enum machine_mode mode)
489 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
494 for (i = 0; i < GET_MODE_SIZE (mode); i++)
497 if (WORDS_BIG_ENDIAN)
498 j = GET_MODE_SIZE (mode) - i - 1;
499 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
500 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
501 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
503 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
506 ch = (unsigned char) str[i];
507 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
509 return immed_double_const (c[0], c[1], mode);
512 /* Cast a target constant CST to target CHAR and if that value fits into
513 host char type, return zero and put that value into variable pointed to by
517 target_char_cast (tree cst, char *p)
519 unsigned HOST_WIDE_INT val, hostval;
521 if (!host_integerp (cst, 1)
522 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
525 val = tree_low_cst (cst, 1);
526 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
527 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
530 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
531 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
540 /* Similar to save_expr, but assumes that arbitrary code is not executed
541 in between the multiple evaluations. In particular, we assume that a
542 non-addressable local variable will not be modified. */
545 builtin_save_expr (tree exp)
547 if (TREE_ADDRESSABLE (exp) == 0
548 && (TREE_CODE (exp) == PARM_DECL
549 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
552 return save_expr (exp);
555 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
556 times to get the address of either a higher stack frame, or a return
557 address located within it (depending on FNDECL_CODE). */
560 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
564 #ifdef INITIAL_FRAME_ADDRESS_RTX
565 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
569 /* For a zero count with __builtin_return_address, we don't care what
570 frame address we return, because target-specific definitions will
571 override us. Therefore frame pointer elimination is OK, and using
572 the soft frame pointer is OK.
574 For a nonzero count, or a zero count with __builtin_frame_address,
575 we require a stable offset from the current frame pointer to the
576 previous one, so we must use the hard frame pointer, and
577 we must disable frame pointer elimination. */
578 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
579 tem = frame_pointer_rtx;
582 tem = hard_frame_pointer_rtx;
584 /* Tell reload not to eliminate the frame pointer. */
585 current_function_accesses_prior_frames = 1;
589 /* Some machines need special handling before we can access
590 arbitrary frames. For example, on the SPARC, we must first flush
591 all register windows to the stack. */
592 #ifdef SETUP_FRAME_ADDRESSES
594 SETUP_FRAME_ADDRESSES ();
597 /* On the SPARC, the return address is not in the frame, it is in a
598 register. There is no way to access it off of the current frame
599 pointer, but it can be accessed off the previous frame pointer by
600 reading the value from the register window save area. */
601 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
602 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
606 /* Scan back COUNT frames to the specified frame. */
607 for (i = 0; i < count; i++)
609 /* Assume the dynamic chain pointer is in the word that the
610 frame address points to, unless otherwise specified. */
611 #ifdef DYNAMIC_CHAIN_ADDRESS
612 tem = DYNAMIC_CHAIN_ADDRESS (tem);
614 tem = memory_address (Pmode, tem);
615 tem = gen_frame_mem (Pmode, tem);
616 tem = copy_to_reg (tem);
619 /* For __builtin_frame_address, return what we've got. But, on
620 the SPARC for example, we may have to add a bias. */
621 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
622 #ifdef FRAME_ADDR_RTX
623 return FRAME_ADDR_RTX (tem);
628 /* For __builtin_return_address, get the return address from that frame. */
629 #ifdef RETURN_ADDR_RTX
630 tem = RETURN_ADDR_RTX (count, tem);
632 tem = memory_address (Pmode,
633 plus_constant (tem, GET_MODE_SIZE (Pmode)));
634 tem = gen_frame_mem (Pmode, tem);
639 /* Alias set used for setjmp buffer. */
640 static HOST_WIDE_INT setjmp_alias_set = -1;
642 /* Construct the leading half of a __builtin_setjmp call. Control will
643 return to RECEIVER_LABEL. This is also called directly by the SJLJ
644 exception handling code. */
647 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
649 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
653 if (setjmp_alias_set == -1)
654 setjmp_alias_set = new_alias_set ();
656 buf_addr = convert_memory_address (Pmode, buf_addr);
658 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
660 /* We store the frame pointer and the address of receiver_label in
661 the buffer and use the rest of it for the stack save area, which
662 is machine-dependent. */
664 mem = gen_rtx_MEM (Pmode, buf_addr);
665 set_mem_alias_set (mem, setjmp_alias_set);
666 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
668 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
669 set_mem_alias_set (mem, setjmp_alias_set);
671 emit_move_insn (validize_mem (mem),
672 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
674 stack_save = gen_rtx_MEM (sa_mode,
675 plus_constant (buf_addr,
676 2 * GET_MODE_SIZE (Pmode)));
677 set_mem_alias_set (stack_save, setjmp_alias_set);
678 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
680 /* If there is further processing to do, do it. */
681 #ifdef HAVE_builtin_setjmp_setup
682 if (HAVE_builtin_setjmp_setup)
683 emit_insn (gen_builtin_setjmp_setup (buf_addr));
686 /* Tell optimize_save_area_alloca that extra work is going to
687 need to go on during alloca. */
688 current_function_calls_setjmp = 1;
690 /* Set this so all the registers get saved in our frame; we need to be
691 able to copy the saved values for any registers from frames we unwind. */
692 current_function_has_nonlocal_label = 1;
695 /* Construct the trailing part of a __builtin_setjmp call. This is
696 also called directly by the SJLJ exception handling code. */
699 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
701 /* Clobber the FP when we get here, so we have to make sure it's
702 marked as used by this function. */
703 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
705 /* Mark the static chain as clobbered here so life information
706 doesn't get messed up for it. */
707 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
709 /* Now put in the code to restore the frame pointer, and argument
710 pointer, if needed. */
711 #ifdef HAVE_nonlocal_goto
712 if (! HAVE_nonlocal_goto)
715 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
716 /* This might change the hard frame pointer in ways that aren't
717 apparent to early optimization passes, so force a clobber. */
718 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
721 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
722 if (fixed_regs[ARG_POINTER_REGNUM])
724 #ifdef ELIMINABLE_REGS
726 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
728 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
729 if (elim_regs[i].from == ARG_POINTER_REGNUM
730 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
733 if (i == ARRAY_SIZE (elim_regs))
736 /* Now restore our arg pointer from the address at which it
737 was saved in our stack frame. */
738 emit_move_insn (virtual_incoming_args_rtx,
739 copy_to_reg (get_arg_pointer_save_area (cfun)));
744 #ifdef HAVE_builtin_setjmp_receiver
745 if (HAVE_builtin_setjmp_receiver)
746 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
749 #ifdef HAVE_nonlocal_goto_receiver
750 if (HAVE_nonlocal_goto_receiver)
751 emit_insn (gen_nonlocal_goto_receiver ());
756 /* @@@ This is a kludge. Not all machine descriptions define a blockage
757 insn, but we must not allow the code we just generated to be reordered
758 by scheduling. Specifically, the update of the frame pointer must
759 happen immediately, not later. So emit an ASM_INPUT to act as blockage
761 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
764 /* __builtin_longjmp is passed a pointer to an array of five words (not
765 all will be used on all machines). It operates similarly to the C
766 library function of the same name, but is more efficient. Much of
767 the code below is copied from the handling of non-local gotos. */
770 expand_builtin_longjmp (rtx buf_addr, rtx value)
772 rtx fp, lab, stack, insn, last;
773 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
775 if (setjmp_alias_set == -1)
776 setjmp_alias_set = new_alias_set ();
778 buf_addr = convert_memory_address (Pmode, buf_addr);
780 buf_addr = force_reg (Pmode, buf_addr);
782 /* We used to store value in static_chain_rtx, but that fails if pointers
783 are smaller than integers. We instead require that the user must pass
784 a second argument of 1, because that is what builtin_setjmp will
785 return. This also makes EH slightly more efficient, since we are no
786 longer copying around a value that we don't care about. */
787 gcc_assert (value == const1_rtx);
789 last = get_last_insn ();
790 #ifdef HAVE_builtin_longjmp
791 if (HAVE_builtin_longjmp)
792 emit_insn (gen_builtin_longjmp (buf_addr));
796 fp = gen_rtx_MEM (Pmode, buf_addr);
797 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
798 GET_MODE_SIZE (Pmode)));
800 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
801 2 * GET_MODE_SIZE (Pmode)));
802 set_mem_alias_set (fp, setjmp_alias_set);
803 set_mem_alias_set (lab, setjmp_alias_set);
804 set_mem_alias_set (stack, setjmp_alias_set);
806 /* Pick up FP, label, and SP from the block and jump. This code is
807 from expand_goto in stmt.c; see there for detailed comments. */
808 #ifdef HAVE_nonlocal_goto
809 if (HAVE_nonlocal_goto)
810 /* We have to pass a value to the nonlocal_goto pattern that will
811 get copied into the static_chain pointer, but it does not matter
812 what that value is, because builtin_setjmp does not use it. */
813 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
817 lab = copy_to_reg (lab);
819 emit_insn (gen_rtx_CLOBBER (VOIDmode,
820 gen_rtx_MEM (BLKmode,
821 gen_rtx_SCRATCH (VOIDmode))));
822 emit_insn (gen_rtx_CLOBBER (VOIDmode,
823 gen_rtx_MEM (BLKmode,
824 hard_frame_pointer_rtx)));
826 emit_move_insn (hard_frame_pointer_rtx, fp);
827 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
829 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
830 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
831 emit_indirect_jump (lab);
835 /* Search backwards and mark the jump insn as a non-local goto.
836 Note that this precludes the use of __builtin_longjmp to a
837 __builtin_setjmp target in the same function. However, we've
838 already cautioned the user that these functions are for
839 internal exception handling use only. */
840 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
842 gcc_assert (insn != last);
846 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
850 else if (CALL_P (insn))
855 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
856 and the address of the save area. */
859 expand_builtin_nonlocal_goto (tree exp)
861 tree t_label, t_save_area;
862 rtx r_label, r_save_area, r_fp, r_sp, insn;
864 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
867 t_label = CALL_EXPR_ARG (exp, 0);
868 t_save_area = CALL_EXPR_ARG (exp, 1);
870 r_label = expand_normal (t_label);
871 r_label = convert_memory_address (Pmode, r_label);
872 r_save_area = expand_normal (t_save_area);
873 r_save_area = convert_memory_address (Pmode, r_save_area);
874 r_fp = gen_rtx_MEM (Pmode, r_save_area);
875 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
876 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
878 current_function_has_nonlocal_goto = 1;
880 #ifdef HAVE_nonlocal_goto
881 /* ??? We no longer need to pass the static chain value, afaik. */
882 if (HAVE_nonlocal_goto)
883 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
887 r_label = copy_to_reg (r_label);
889 emit_insn (gen_rtx_CLOBBER (VOIDmode,
890 gen_rtx_MEM (BLKmode,
891 gen_rtx_SCRATCH (VOIDmode))));
893 emit_insn (gen_rtx_CLOBBER (VOIDmode,
894 gen_rtx_MEM (BLKmode,
895 hard_frame_pointer_rtx)));
897 /* Restore frame pointer for containing function.
898 This sets the actual hard register used for the frame pointer
899 to the location of the function's incoming static chain info.
900 The non-local goto handler will then adjust it to contain the
901 proper value and reload the argument pointer, if needed. */
902 emit_move_insn (hard_frame_pointer_rtx, r_fp);
903 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
905 /* USE of hard_frame_pointer_rtx added for consistency;
906 not clear if really needed. */
907 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
908 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
909 emit_indirect_jump (r_label);
912 /* Search backwards to the jump insn and mark it as a
914 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
918 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
919 const0_rtx, REG_NOTES (insn));
922 else if (CALL_P (insn))
929 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
930 (not all will be used on all machines) that was passed to __builtin_setjmp.
931 It updates the stack pointer in that block to correspond to the current
935 expand_builtin_update_setjmp_buf (rtx buf_addr)
937 enum machine_mode sa_mode = Pmode;
941 #ifdef HAVE_save_stack_nonlocal
942 if (HAVE_save_stack_nonlocal)
943 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
945 #ifdef STACK_SAVEAREA_MODE
946 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
950 = gen_rtx_MEM (sa_mode,
953 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
957 emit_insn (gen_setjmp ());
960 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
963 /* Expand a call to __builtin_prefetch. For a target that does not support
964 data prefetch, evaluate the memory address argument in case it has side
968 expand_builtin_prefetch (tree exp)
970 tree arg0, arg1, arg2;
974 if (!validate_arglist (exp, POINTER_TYPE, 0))
977 arg0 = CALL_EXPR_ARG (exp, 0);
979 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
980 zero (read) and argument 2 (locality) defaults to 3 (high degree of
982 nargs = call_expr_nargs (exp);
984 arg1 = CALL_EXPR_ARG (exp, 1);
986 arg1 = integer_zero_node;
988 arg2 = CALL_EXPR_ARG (exp, 2);
990 arg2 = build_int_cst (NULL_TREE, 3);
992 /* Argument 0 is an address. */
993 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
995 /* Argument 1 (read/write flag) must be a compile-time constant int. */
996 if (TREE_CODE (arg1) != INTEGER_CST)
998 error ("second argument to %<__builtin_prefetch%> must be a constant");
999 arg1 = integer_zero_node;
1001 op1 = expand_normal (arg1);
1002 /* Argument 1 must be either zero or one. */
1003 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1005 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1010 /* Argument 2 (locality) must be a compile-time constant int. */
1011 if (TREE_CODE (arg2) != INTEGER_CST)
1013 error ("third argument to %<__builtin_prefetch%> must be a constant");
1014 arg2 = integer_zero_node;
1016 op2 = expand_normal (arg2);
1017 /* Argument 2 must be 0, 1, 2, or 3. */
1018 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1020 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1024 #ifdef HAVE_prefetch
1027 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1029 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1030 || (GET_MODE (op0) != Pmode))
1032 op0 = convert_memory_address (Pmode, op0);
1033 op0 = force_reg (Pmode, op0);
1035 emit_insn (gen_prefetch (op0, op1, op2));
1039 /* Don't do anything with direct references to volatile memory, but
1040 generate code to handle other side effects. */
1041 if (!MEM_P (op0) && side_effects_p (op0))
1045 /* Get a MEM rtx for expression EXP which is the address of an operand
1046 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1047 the maximum length of the block of memory that might be accessed or
1051 get_memory_rtx (tree exp, tree len)
1053 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1054 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1056 /* Get an expression we can use to find the attributes to assign to MEM.
1057 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1058 we can. First remove any nops. */
1059 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1060 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1061 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1062 exp = TREE_OPERAND (exp, 0);
1064 if (TREE_CODE (exp) == ADDR_EXPR)
1065 exp = TREE_OPERAND (exp, 0);
1066 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1067 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1071 /* Honor attributes derived from exp, except for the alias set
1072 (as builtin stringops may alias with anything) and the size
1073 (as stringops may access multiple array elements). */
1076 set_mem_attributes (mem, exp, 0);
1078 /* Allow the string and memory builtins to overflow from one
1079 field into another, see http://gcc.gnu.org/PR23561.
1080 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1081 memory accessed by the string or memory builtin will fit
1082 within the field. */
1083 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1085 tree mem_expr = MEM_EXPR (mem);
1086 HOST_WIDE_INT offset = -1, length = -1;
1089 while (TREE_CODE (inner) == ARRAY_REF
1090 || TREE_CODE (inner) == NOP_EXPR
1091 || TREE_CODE (inner) == CONVERT_EXPR
1092 || TREE_CODE (inner) == NON_LVALUE_EXPR
1093 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1094 || TREE_CODE (inner) == SAVE_EXPR)
1095 inner = TREE_OPERAND (inner, 0);
1097 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1099 if (MEM_OFFSET (mem)
1100 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1101 offset = INTVAL (MEM_OFFSET (mem));
1103 if (offset >= 0 && len && host_integerp (len, 0))
1104 length = tree_low_cst (len, 0);
1106 while (TREE_CODE (inner) == COMPONENT_REF)
1108 tree field = TREE_OPERAND (inner, 1);
1109 gcc_assert (! DECL_BIT_FIELD (field));
1110 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1111 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1114 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1115 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1118 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1119 /* If we can prove the memory starting at XEXP (mem, 0)
1120 and ending at XEXP (mem, 0) + LENGTH will fit into
1121 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1124 && offset + length <= size)
1129 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1130 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1131 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1139 mem_expr = TREE_OPERAND (mem_expr, 0);
1140 inner = TREE_OPERAND (inner, 0);
1143 if (mem_expr == NULL)
1145 if (mem_expr != MEM_EXPR (mem))
1147 set_mem_expr (mem, mem_expr);
1148 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1151 set_mem_alias_set (mem, 0);
1152 set_mem_size (mem, NULL_RTX);
1158 /* Built-in functions to perform an untyped call and return. */
1160 /* For each register that may be used for calling a function, this
1161 gives a mode used to copy the register's value. VOIDmode indicates
1162 the register is not used for calling a function. If the machine
1163 has register windows, this gives only the outbound registers.
1164 INCOMING_REGNO gives the corresponding inbound register. */
1165 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1167 /* For each register that may be used for returning values, this gives
1168 a mode used to copy the register's value. VOIDmode indicates the
1169 register is not used for returning values. If the machine has
1170 register windows, this gives only the outbound registers.
1171 INCOMING_REGNO gives the corresponding inbound register. */
1172 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1174 /* For each register that may be used for calling a function, this
1175 gives the offset of that register into the block returned by
1176 __builtin_apply_args. 0 indicates that the register is not
1177 used for calling a function. */
1178 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1180 /* Return the size required for the block returned by __builtin_apply_args,
1181 and initialize apply_args_mode. */
1184 apply_args_size (void)
1186 static int size = -1;
1189 enum machine_mode mode;
1191 /* The values computed by this function never change. */
1194 /* The first value is the incoming arg-pointer. */
1195 size = GET_MODE_SIZE (Pmode);
1197 /* The second value is the structure value address unless this is
1198 passed as an "invisible" first argument. */
1199 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1200 size += GET_MODE_SIZE (Pmode);
1202 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1203 if (FUNCTION_ARG_REGNO_P (regno))
1205 mode = reg_raw_mode[regno];
1207 gcc_assert (mode != VOIDmode);
1209 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1210 if (size % align != 0)
1211 size = CEIL (size, align) * align;
1212 apply_args_reg_offset[regno] = size;
1213 size += GET_MODE_SIZE (mode);
1214 apply_args_mode[regno] = mode;
1218 apply_args_mode[regno] = VOIDmode;
1219 apply_args_reg_offset[regno] = 0;
1225 /* Return the size required for the block returned by __builtin_apply,
1226 and initialize apply_result_mode. */
1229 apply_result_size (void)
1231 static int size = -1;
1233 enum machine_mode mode;
1235 /* The values computed by this function never change. */
1240 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1241 if (FUNCTION_VALUE_REGNO_P (regno))
1243 mode = reg_raw_mode[regno];
1245 gcc_assert (mode != VOIDmode);
1247 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1248 if (size % align != 0)
1249 size = CEIL (size, align) * align;
1250 size += GET_MODE_SIZE (mode);
1251 apply_result_mode[regno] = mode;
1254 apply_result_mode[regno] = VOIDmode;
1256 /* Allow targets that use untyped_call and untyped_return to override
1257 the size so that machine-specific information can be stored here. */
1258 #ifdef APPLY_RESULT_SIZE
1259 size = APPLY_RESULT_SIZE;
1265 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1266 /* Create a vector describing the result block RESULT. If SAVEP is true,
1267 the result block is used to save the values; otherwise it is used to
1268 restore the values. */
1271 result_vector (int savep, rtx result)
1273 int regno, size, align, nelts;
1274 enum machine_mode mode;
1276 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1279 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1280 if ((mode = apply_result_mode[regno]) != VOIDmode)
1282 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1283 if (size % align != 0)
1284 size = CEIL (size, align) * align;
1285 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1286 mem = adjust_address (result, mode, size);
1287 savevec[nelts++] = (savep
1288 ? gen_rtx_SET (VOIDmode, mem, reg)
1289 : gen_rtx_SET (VOIDmode, reg, mem));
1290 size += GET_MODE_SIZE (mode);
1292 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1294 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1296 /* Save the state required to perform an untyped call with the same
1297 arguments as were passed to the current function. */
1300 expand_builtin_apply_args_1 (void)
1303 int size, align, regno;
1304 enum machine_mode mode;
1305 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1307 /* Create a block where the arg-pointer, structure value address,
1308 and argument registers can be saved. */
1309 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1311 /* Walk past the arg-pointer and structure value address. */
1312 size = GET_MODE_SIZE (Pmode);
1313 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1314 size += GET_MODE_SIZE (Pmode);
1316 /* Save each register used in calling a function to the block. */
1317 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1318 if ((mode = apply_args_mode[regno]) != VOIDmode)
1320 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1321 if (size % align != 0)
1322 size = CEIL (size, align) * align;
1324 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1326 emit_move_insn (adjust_address (registers, mode, size), tem);
1327 size += GET_MODE_SIZE (mode);
1330 /* Save the arg pointer to the block. */
1331 tem = copy_to_reg (virtual_incoming_args_rtx);
1332 #ifdef STACK_GROWS_DOWNWARD
1333 /* We need the pointer as the caller actually passed them to us, not
1334 as we might have pretended they were passed. Make sure it's a valid
1335 operand, as emit_move_insn isn't expected to handle a PLUS. */
1337 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1340 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1342 size = GET_MODE_SIZE (Pmode);
1344 /* Save the structure value address unless this is passed as an
1345 "invisible" first argument. */
1346 if (struct_incoming_value)
1348 emit_move_insn (adjust_address (registers, Pmode, size),
1349 copy_to_reg (struct_incoming_value));
1350 size += GET_MODE_SIZE (Pmode);
1353 /* Return the address of the block. */
1354 return copy_addr_to_reg (XEXP (registers, 0));
1357 /* __builtin_apply_args returns block of memory allocated on
1358 the stack into which is stored the arg pointer, structure
1359 value address, static chain, and all the registers that might
1360 possibly be used in performing a function call. The code is
1361 moved to the start of the function so the incoming values are
1365 expand_builtin_apply_args (void)
1367 /* Don't do __builtin_apply_args more than once in a function.
1368 Save the result of the first call and reuse it. */
1369 if (apply_args_value != 0)
1370 return apply_args_value;
1372 /* When this function is called, it means that registers must be
1373 saved on entry to this function. So we migrate the
1374 call to the first insn of this function. */
1379 temp = expand_builtin_apply_args_1 ();
1383 apply_args_value = temp;
1385 /* Put the insns after the NOTE that starts the function.
1386 If this is inside a start_sequence, make the outer-level insn
1387 chain current, so the code is placed at the start of the
1389 push_topmost_sequence ();
1390 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1391 pop_topmost_sequence ();
1396 /* Perform an untyped call and save the state required to perform an
1397 untyped return of whatever value was returned by the given function. */
1400 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1402 int size, align, regno;
1403 enum machine_mode mode;
1404 rtx incoming_args, result, reg, dest, src, call_insn;
1405 rtx old_stack_level = 0;
1406 rtx call_fusage = 0;
1407 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1409 arguments = convert_memory_address (Pmode, arguments);
1411 /* Create a block where the return registers can be saved. */
1412 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1414 /* Fetch the arg pointer from the ARGUMENTS block. */
1415 incoming_args = gen_reg_rtx (Pmode);
1416 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1417 #ifndef STACK_GROWS_DOWNWARD
1418 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1419 incoming_args, 0, OPTAB_LIB_WIDEN);
1422 /* Push a new argument block and copy the arguments. Do not allow
1423 the (potential) memcpy call below to interfere with our stack
1425 do_pending_stack_adjust ();
1428 /* Save the stack with nonlocal if available. */
1429 #ifdef HAVE_save_stack_nonlocal
1430 if (HAVE_save_stack_nonlocal)
1431 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1434 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1436 /* Allocate a block of memory onto the stack and copy the memory
1437 arguments to the outgoing arguments address. */
1438 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1439 dest = virtual_outgoing_args_rtx;
1440 #ifndef STACK_GROWS_DOWNWARD
1441 if (GET_CODE (argsize) == CONST_INT)
1442 dest = plus_constant (dest, -INTVAL (argsize));
1444 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1446 dest = gen_rtx_MEM (BLKmode, dest);
1447 set_mem_align (dest, PARM_BOUNDARY);
1448 src = gen_rtx_MEM (BLKmode, incoming_args);
1449 set_mem_align (src, PARM_BOUNDARY);
1450 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1452 /* Refer to the argument block. */
1454 arguments = gen_rtx_MEM (BLKmode, arguments);
1455 set_mem_align (arguments, PARM_BOUNDARY);
1457 /* Walk past the arg-pointer and structure value address. */
1458 size = GET_MODE_SIZE (Pmode);
1460 size += GET_MODE_SIZE (Pmode);
1462 /* Restore each of the registers previously saved. Make USE insns
1463 for each of these registers for use in making the call. */
1464 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1465 if ((mode = apply_args_mode[regno]) != VOIDmode)
1467 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1468 if (size % align != 0)
1469 size = CEIL (size, align) * align;
1470 reg = gen_rtx_REG (mode, regno);
1471 emit_move_insn (reg, adjust_address (arguments, mode, size));
1472 use_reg (&call_fusage, reg);
1473 size += GET_MODE_SIZE (mode);
1476 /* Restore the structure value address unless this is passed as an
1477 "invisible" first argument. */
1478 size = GET_MODE_SIZE (Pmode);
1481 rtx value = gen_reg_rtx (Pmode);
1482 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1483 emit_move_insn (struct_value, value);
1484 if (REG_P (struct_value))
1485 use_reg (&call_fusage, struct_value);
1486 size += GET_MODE_SIZE (Pmode);
1489 /* All arguments and registers used for the call are set up by now! */
1490 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1492 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1493 and we don't want to load it into a register as an optimization,
1494 because prepare_call_address already did it if it should be done. */
1495 if (GET_CODE (function) != SYMBOL_REF)
1496 function = memory_address (FUNCTION_MODE, function);
1498 /* Generate the actual call instruction and save the return value. */
1499 #ifdef HAVE_untyped_call
1500 if (HAVE_untyped_call)
1501 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1502 result, result_vector (1, result)));
1505 #ifdef HAVE_call_value
1506 if (HAVE_call_value)
1510 /* Locate the unique return register. It is not possible to
1511 express a call that sets more than one return register using
1512 call_value; use untyped_call for that. In fact, untyped_call
1513 only needs to save the return registers in the given block. */
1514 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1515 if ((mode = apply_result_mode[regno]) != VOIDmode)
1517 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1519 valreg = gen_rtx_REG (mode, regno);
1522 emit_call_insn (GEN_CALL_VALUE (valreg,
1523 gen_rtx_MEM (FUNCTION_MODE, function),
1524 const0_rtx, NULL_RTX, const0_rtx));
1526 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1532 /* Find the CALL insn we just emitted, and attach the register usage
1534 call_insn = last_call_insn ();
1535 add_function_usage_to (call_insn, call_fusage);
1537 /* Restore the stack. */
1538 #ifdef HAVE_save_stack_nonlocal
1539 if (HAVE_save_stack_nonlocal)
1540 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1543 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1547 /* Return the address of the result block. */
1548 result = copy_addr_to_reg (XEXP (result, 0));
1549 return convert_memory_address (ptr_mode, result);
1552 /* Perform an untyped return. */
1555 expand_builtin_return (rtx result)
1557 int size, align, regno;
1558 enum machine_mode mode;
1560 rtx call_fusage = 0;
1562 result = convert_memory_address (Pmode, result);
1564 apply_result_size ();
1565 result = gen_rtx_MEM (BLKmode, result);
1567 #ifdef HAVE_untyped_return
1568 if (HAVE_untyped_return)
1570 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1576 /* Restore the return value and note that each value is used. */
1578 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1579 if ((mode = apply_result_mode[regno]) != VOIDmode)
1581 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1582 if (size % align != 0)
1583 size = CEIL (size, align) * align;
1584 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1585 emit_move_insn (reg, adjust_address (result, mode, size));
1587 push_to_sequence (call_fusage);
1588 emit_insn (gen_rtx_USE (VOIDmode, reg));
1589 call_fusage = get_insns ();
1591 size += GET_MODE_SIZE (mode);
1594 /* Put the USE insns before the return. */
1595 emit_insn (call_fusage);
1597 /* Return whatever values was restored by jumping directly to the end
1599 expand_naked_return ();
1602 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1604 static enum type_class
1605 type_to_class (tree type)
1607 switch (TREE_CODE (type))
1609 case VOID_TYPE: return void_type_class;
1610 case INTEGER_TYPE: return integer_type_class;
1611 case ENUMERAL_TYPE: return enumeral_type_class;
1612 case BOOLEAN_TYPE: return boolean_type_class;
1613 case POINTER_TYPE: return pointer_type_class;
1614 case REFERENCE_TYPE: return reference_type_class;
1615 case OFFSET_TYPE: return offset_type_class;
1616 case REAL_TYPE: return real_type_class;
1617 case COMPLEX_TYPE: return complex_type_class;
1618 case FUNCTION_TYPE: return function_type_class;
1619 case METHOD_TYPE: return method_type_class;
1620 case RECORD_TYPE: return record_type_class;
1622 case QUAL_UNION_TYPE: return union_type_class;
1623 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1624 ? string_type_class : array_type_class);
1625 case LANG_TYPE: return lang_type_class;
1626 default: return no_type_class;
1630 /* Expand a call EXP to __builtin_classify_type. */
1633 expand_builtin_classify_type (tree exp)
1635 if (call_expr_nargs (exp))
1636 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1637 return GEN_INT (no_type_class);
1640 /* This helper macro, meant to be used in mathfn_built_in below,
1641 determines which among a set of three builtin math functions is
1642 appropriate for a given type mode. The `F' and `L' cases are
1643 automatically generated from the `double' case. */
1644 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1645 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1646 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1647 fcodel = BUILT_IN_MATHFN##L ; break;
1649 /* Return mathematic function equivalent to FN but operating directly
1650 on TYPE, if available. If we can't do the conversion, return zero. */
1652 mathfn_built_in (tree type, enum built_in_function fn)
1654 enum built_in_function fcode, fcodef, fcodel;
1658 CASE_MATHFN (BUILT_IN_ACOS)
1659 CASE_MATHFN (BUILT_IN_ACOSH)
1660 CASE_MATHFN (BUILT_IN_ASIN)
1661 CASE_MATHFN (BUILT_IN_ASINH)
1662 CASE_MATHFN (BUILT_IN_ATAN)
1663 CASE_MATHFN (BUILT_IN_ATAN2)
1664 CASE_MATHFN (BUILT_IN_ATANH)
1665 CASE_MATHFN (BUILT_IN_CBRT)
1666 CASE_MATHFN (BUILT_IN_CEIL)
1667 CASE_MATHFN (BUILT_IN_CEXPI)
1668 CASE_MATHFN (BUILT_IN_COPYSIGN)
1669 CASE_MATHFN (BUILT_IN_COS)
1670 CASE_MATHFN (BUILT_IN_COSH)
1671 CASE_MATHFN (BUILT_IN_DREM)
1672 CASE_MATHFN (BUILT_IN_ERF)
1673 CASE_MATHFN (BUILT_IN_ERFC)
1674 CASE_MATHFN (BUILT_IN_EXP)
1675 CASE_MATHFN (BUILT_IN_EXP10)
1676 CASE_MATHFN (BUILT_IN_EXP2)
1677 CASE_MATHFN (BUILT_IN_EXPM1)
1678 CASE_MATHFN (BUILT_IN_FABS)
1679 CASE_MATHFN (BUILT_IN_FDIM)
1680 CASE_MATHFN (BUILT_IN_FLOOR)
1681 CASE_MATHFN (BUILT_IN_FMA)
1682 CASE_MATHFN (BUILT_IN_FMAX)
1683 CASE_MATHFN (BUILT_IN_FMIN)
1684 CASE_MATHFN (BUILT_IN_FMOD)
1685 CASE_MATHFN (BUILT_IN_FREXP)
1686 CASE_MATHFN (BUILT_IN_GAMMA)
1687 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1688 CASE_MATHFN (BUILT_IN_HYPOT)
1689 CASE_MATHFN (BUILT_IN_ILOGB)
1690 CASE_MATHFN (BUILT_IN_INF)
1691 CASE_MATHFN (BUILT_IN_ISINF)
1692 CASE_MATHFN (BUILT_IN_J0)
1693 CASE_MATHFN (BUILT_IN_J1)
1694 CASE_MATHFN (BUILT_IN_JN)
1695 CASE_MATHFN (BUILT_IN_LCEIL)
1696 CASE_MATHFN (BUILT_IN_LDEXP)
1697 CASE_MATHFN (BUILT_IN_LFLOOR)
1698 CASE_MATHFN (BUILT_IN_LGAMMA)
1699 CASE_MATHFN (BUILT_IN_LLCEIL)
1700 CASE_MATHFN (BUILT_IN_LLFLOOR)
1701 CASE_MATHFN (BUILT_IN_LLRINT)
1702 CASE_MATHFN (BUILT_IN_LLROUND)
1703 CASE_MATHFN (BUILT_IN_LOG)
1704 CASE_MATHFN (BUILT_IN_LOG10)
1705 CASE_MATHFN (BUILT_IN_LOG1P)
1706 CASE_MATHFN (BUILT_IN_LOG2)
1707 CASE_MATHFN (BUILT_IN_LOGB)
1708 CASE_MATHFN (BUILT_IN_LRINT)
1709 CASE_MATHFN (BUILT_IN_LROUND)
1710 CASE_MATHFN (BUILT_IN_MODF)
1711 CASE_MATHFN (BUILT_IN_NAN)
1712 CASE_MATHFN (BUILT_IN_NANS)
1713 CASE_MATHFN (BUILT_IN_NEARBYINT)
1714 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1715 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1716 CASE_MATHFN (BUILT_IN_POW)
1717 CASE_MATHFN (BUILT_IN_POWI)
1718 CASE_MATHFN (BUILT_IN_POW10)
1719 CASE_MATHFN (BUILT_IN_REMAINDER)
1720 CASE_MATHFN (BUILT_IN_REMQUO)
1721 CASE_MATHFN (BUILT_IN_RINT)
1722 CASE_MATHFN (BUILT_IN_ROUND)
1723 CASE_MATHFN (BUILT_IN_SCALB)
1724 CASE_MATHFN (BUILT_IN_SCALBLN)
1725 CASE_MATHFN (BUILT_IN_SCALBN)
1726 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1727 CASE_MATHFN (BUILT_IN_SIN)
1728 CASE_MATHFN (BUILT_IN_SINCOS)
1729 CASE_MATHFN (BUILT_IN_SINH)
1730 CASE_MATHFN (BUILT_IN_SQRT)
1731 CASE_MATHFN (BUILT_IN_TAN)
1732 CASE_MATHFN (BUILT_IN_TANH)
1733 CASE_MATHFN (BUILT_IN_TGAMMA)
1734 CASE_MATHFN (BUILT_IN_TRUNC)
1735 CASE_MATHFN (BUILT_IN_Y0)
1736 CASE_MATHFN (BUILT_IN_Y1)
1737 CASE_MATHFN (BUILT_IN_YN)
1743 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1744 return implicit_built_in_decls[fcode];
1745 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1746 return implicit_built_in_decls[fcodef];
1747 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1748 return implicit_built_in_decls[fcodel];
1753 /* If errno must be maintained, expand the RTL to check if the result,
1754 TARGET, of a built-in function call, EXP, is NaN, and if so set
1758 expand_errno_check (tree exp, rtx target)
1760 rtx lab = gen_label_rtx ();
1762 /* Test the result; if it is NaN, set errno=EDOM because
1763 the argument was not in the domain. */
1764 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1768 /* If this built-in doesn't throw an exception, set errno directly. */
1769 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1771 #ifdef GEN_ERRNO_RTX
1772 rtx errno_rtx = GEN_ERRNO_RTX;
1775 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1777 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1783 /* We can't set errno=EDOM directly; let the library call do it.
1784 Pop the arguments right away in case the call gets deleted. */
1786 expand_call (exp, target, 0);
1791 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1792 Return NULL_RTX if a normal call should be emitted rather than expanding
1793 the function in-line. EXP is the expression that is a call to the builtin
1794 function; if convenient, the result should be placed in TARGET.
1795 SUBTARGET may be used as the target for computing one of EXP's operands. */
1798 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1800 optab builtin_optab;
1801 rtx op0, insns, before_call;
1802 tree fndecl = get_callee_fndecl (exp);
1803 enum machine_mode mode;
1804 bool errno_set = false;
1807 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1810 arg = CALL_EXPR_ARG (exp, 0);
1812 switch (DECL_FUNCTION_CODE (fndecl))
1814 CASE_FLT_FN (BUILT_IN_SQRT):
1815 errno_set = ! tree_expr_nonnegative_p (arg);
1816 builtin_optab = sqrt_optab;
1818 CASE_FLT_FN (BUILT_IN_EXP):
1819 errno_set = true; builtin_optab = exp_optab; break;
1820 CASE_FLT_FN (BUILT_IN_EXP10):
1821 CASE_FLT_FN (BUILT_IN_POW10):
1822 errno_set = true; builtin_optab = exp10_optab; break;
1823 CASE_FLT_FN (BUILT_IN_EXP2):
1824 errno_set = true; builtin_optab = exp2_optab; break;
1825 CASE_FLT_FN (BUILT_IN_EXPM1):
1826 errno_set = true; builtin_optab = expm1_optab; break;
1827 CASE_FLT_FN (BUILT_IN_LOGB):
1828 errno_set = true; builtin_optab = logb_optab; break;
1829 CASE_FLT_FN (BUILT_IN_LOG):
1830 errno_set = true; builtin_optab = log_optab; break;
1831 CASE_FLT_FN (BUILT_IN_LOG10):
1832 errno_set = true; builtin_optab = log10_optab; break;
1833 CASE_FLT_FN (BUILT_IN_LOG2):
1834 errno_set = true; builtin_optab = log2_optab; break;
1835 CASE_FLT_FN (BUILT_IN_LOG1P):
1836 errno_set = true; builtin_optab = log1p_optab; break;
1837 CASE_FLT_FN (BUILT_IN_ASIN):
1838 builtin_optab = asin_optab; break;
1839 CASE_FLT_FN (BUILT_IN_ACOS):
1840 builtin_optab = acos_optab; break;
1841 CASE_FLT_FN (BUILT_IN_TAN):
1842 builtin_optab = tan_optab; break;
1843 CASE_FLT_FN (BUILT_IN_ATAN):
1844 builtin_optab = atan_optab; break;
1845 CASE_FLT_FN (BUILT_IN_FLOOR):
1846 builtin_optab = floor_optab; break;
1847 CASE_FLT_FN (BUILT_IN_CEIL):
1848 builtin_optab = ceil_optab; break;
1849 CASE_FLT_FN (BUILT_IN_TRUNC):
1850 builtin_optab = btrunc_optab; break;
1851 CASE_FLT_FN (BUILT_IN_ROUND):
1852 builtin_optab = round_optab; break;
1853 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1854 builtin_optab = nearbyint_optab;
1855 if (flag_trapping_math)
1857 /* Else fallthrough and expand as rint. */
1858 CASE_FLT_FN (BUILT_IN_RINT):
1859 builtin_optab = rint_optab; break;
1864 /* Make a suitable register to place result in. */
1865 mode = TYPE_MODE (TREE_TYPE (exp));
1867 if (! flag_errno_math || ! HONOR_NANS (mode))
1870 /* Before working hard, check whether the instruction is available. */
1871 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1873 target = gen_reg_rtx (mode);
1875 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1876 need to expand the argument again. This way, we will not perform
1877 side-effects more the once. */
1878 narg = builtin_save_expr (arg);
1882 exp = build_call_expr (fndecl, 1, arg);
1885 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1889 /* Compute into TARGET.
1890 Set TARGET to wherever the result comes back. */
1891 target = expand_unop (mode, builtin_optab, op0, target, 0);
1896 expand_errno_check (exp, target);
1898 /* Output the entire sequence. */
1899 insns = get_insns ();
1905 /* If we were unable to expand via the builtin, stop the sequence
1906 (without outputting the insns) and call to the library function
1907 with the stabilized argument list. */
1911 before_call = get_last_insn ();
1913 target = expand_call (exp, target, target == const0_rtx);
1915 /* If this is a sqrt operation and we don't care about errno, try to
1916 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1917 This allows the semantics of the libcall to be visible to the RTL
1919 if (builtin_optab == sqrt_optab && !errno_set)
1921 /* Search backwards through the insns emitted by expand_call looking
1922 for the instruction with the REG_RETVAL note. */
1923 rtx last = get_last_insn ();
1924 while (last != before_call)
1926 if (find_reg_note (last, REG_RETVAL, NULL))
1928 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1929 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1930 two elements, i.e. symbol_ref(sqrt) and the operand. */
1932 && GET_CODE (note) == EXPR_LIST
1933 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1934 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1935 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1937 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1938 /* Check operand is a register with expected mode. */
1941 && GET_MODE (operand) == mode)
1943 /* Replace the REG_EQUAL note with a SQRT rtx. */
1944 rtx equiv = gen_rtx_SQRT (mode, operand);
1945 set_unique_reg_note (last, REG_EQUAL, equiv);
1950 last = PREV_INSN (last);
1957 /* Expand a call to the builtin binary math functions (pow and atan2).
1958 Return NULL_RTX if a normal call should be emitted rather than expanding the
1959 function in-line. EXP is the expression that is a call to the builtin
1960 function; if convenient, the result should be placed in TARGET.
1961 SUBTARGET may be used as the target for computing one of EXP's
1965 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1967 optab builtin_optab;
1968 rtx op0, op1, insns;
1969 int op1_type = REAL_TYPE;
1970 tree fndecl = get_callee_fndecl (exp);
1971 tree arg0, arg1, narg;
1972 enum machine_mode mode;
1973 bool errno_set = true;
1976 switch (DECL_FUNCTION_CODE (fndecl))
1978 CASE_FLT_FN (BUILT_IN_SCALBN):
1979 CASE_FLT_FN (BUILT_IN_SCALBLN):
1980 CASE_FLT_FN (BUILT_IN_LDEXP):
1981 op1_type = INTEGER_TYPE;
1986 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
1989 arg0 = CALL_EXPR_ARG (exp, 0);
1990 arg1 = CALL_EXPR_ARG (exp, 1);
1992 switch (DECL_FUNCTION_CODE (fndecl))
1994 CASE_FLT_FN (BUILT_IN_POW):
1995 builtin_optab = pow_optab; break;
1996 CASE_FLT_FN (BUILT_IN_ATAN2):
1997 builtin_optab = atan2_optab; break;
1998 CASE_FLT_FN (BUILT_IN_SCALB):
1999 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2001 builtin_optab = scalb_optab; break;
2002 CASE_FLT_FN (BUILT_IN_SCALBN):
2003 CASE_FLT_FN (BUILT_IN_SCALBLN):
2004 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2006 /* Fall through... */
2007 CASE_FLT_FN (BUILT_IN_LDEXP):
2008 builtin_optab = ldexp_optab; break;
2009 CASE_FLT_FN (BUILT_IN_FMOD):
2010 builtin_optab = fmod_optab; break;
2011 CASE_FLT_FN (BUILT_IN_REMAINDER):
2012 CASE_FLT_FN (BUILT_IN_DREM):
2013 builtin_optab = remainder_optab; break;
2018 /* Make a suitable register to place result in. */
2019 mode = TYPE_MODE (TREE_TYPE (exp));
2021 /* Before working hard, check whether the instruction is available. */
2022 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2025 target = gen_reg_rtx (mode);
2027 if (! flag_errno_math || ! HONOR_NANS (mode))
2030 /* Always stabilize the argument list. */
2031 narg = builtin_save_expr (arg1);
2037 narg = builtin_save_expr (arg0);
2045 exp = build_call_expr (fndecl, 2, arg0, arg1);
2047 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2048 op1 = expand_normal (arg1);
2052 /* Compute into TARGET.
2053 Set TARGET to wherever the result comes back. */
2054 target = expand_binop (mode, builtin_optab, op0, op1,
2055 target, 0, OPTAB_DIRECT);
2057 /* If we were unable to expand via the builtin, stop the sequence
2058 (without outputting the insns) and call to the library function
2059 with the stabilized argument list. */
2063 return expand_call (exp, target, target == const0_rtx);
2067 expand_errno_check (exp, target);
2069 /* Output the entire sequence. */
2070 insns = get_insns ();
2077 /* Expand a call to the builtin sin and cos math functions.
2078 Return NULL_RTX if a normal call should be emitted rather than expanding the
2079 function in-line. EXP is the expression that is a call to the builtin
2080 function; if convenient, the result should be placed in TARGET.
2081 SUBTARGET may be used as the target for computing one of EXP's
2085 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2087 optab builtin_optab;
2089 tree fndecl = get_callee_fndecl (exp);
2090 enum machine_mode mode;
2093 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2096 arg = CALL_EXPR_ARG (exp, 0);
2098 switch (DECL_FUNCTION_CODE (fndecl))
2100 CASE_FLT_FN (BUILT_IN_SIN):
2101 CASE_FLT_FN (BUILT_IN_COS):
2102 builtin_optab = sincos_optab; break;
2107 /* Make a suitable register to place result in. */
2108 mode = TYPE_MODE (TREE_TYPE (exp));
2110 /* Check if sincos insn is available, otherwise fallback
2111 to sin or cos insn. */
2112 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2113 switch (DECL_FUNCTION_CODE (fndecl))
2115 CASE_FLT_FN (BUILT_IN_SIN):
2116 builtin_optab = sin_optab; break;
2117 CASE_FLT_FN (BUILT_IN_COS):
2118 builtin_optab = cos_optab; break;
2123 /* Before working hard, check whether the instruction is available. */
2124 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2126 target = gen_reg_rtx (mode);
2128 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2129 need to expand the argument again. This way, we will not perform
2130 side-effects more the once. */
2131 narg = save_expr (arg);
2135 exp = build_call_expr (fndecl, 1, arg);
2138 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2142 /* Compute into TARGET.
2143 Set TARGET to wherever the result comes back. */
2144 if (builtin_optab == sincos_optab)
2148 switch (DECL_FUNCTION_CODE (fndecl))
2150 CASE_FLT_FN (BUILT_IN_SIN):
2151 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2153 CASE_FLT_FN (BUILT_IN_COS):
2154 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2159 gcc_assert (result);
2163 target = expand_unop (mode, builtin_optab, op0, target, 0);
2168 /* Output the entire sequence. */
2169 insns = get_insns ();
2175 /* If we were unable to expand via the builtin, stop the sequence
2176 (without outputting the insns) and call to the library function
2177 with the stabilized argument list. */
2181 target = expand_call (exp, target, target == const0_rtx);
2186 /* Expand a call to one of the builtin math functions that operate on
2187 floating point argument and output an integer result (ilogb, isinf,
2189 Return 0 if a normal call should be emitted rather than expanding the
2190 function in-line. EXP is the expression that is a call to the builtin
2191 function; if convenient, the result should be placed in TARGET.
2192 SUBTARGET may be used as the target for computing one of EXP's operands. */
2195 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2197 optab builtin_optab;
2198 enum insn_code icode;
2200 tree fndecl = get_callee_fndecl (exp);
2201 enum machine_mode mode;
2202 bool errno_set = false;
2205 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2208 arg = CALL_EXPR_ARG (exp, 0);
2210 switch (DECL_FUNCTION_CODE (fndecl))
2212 CASE_FLT_FN (BUILT_IN_ILOGB):
2213 errno_set = true; builtin_optab = ilogb_optab; break;
2214 CASE_FLT_FN (BUILT_IN_ISINF):
2215 builtin_optab = isinf_optab; break;
2220 /* There's no easy way to detect the case we need to set EDOM. */
2221 if (flag_errno_math && errno_set)
2224 /* Optab mode depends on the mode of the input argument. */
2225 mode = TYPE_MODE (TREE_TYPE (arg));
2227 icode = builtin_optab->handlers[(int) mode].insn_code;
2229 /* Before working hard, check whether the instruction is available. */
2230 if (icode != CODE_FOR_nothing)
2232 /* Make a suitable register to place result in. */
2234 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2235 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2237 gcc_assert (insn_data[icode].operand[0].predicate
2238 (target, GET_MODE (target)));
2240 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2241 need to expand the argument again. This way, we will not perform
2242 side-effects more the once. */
2243 narg = builtin_save_expr (arg);
2247 exp = build_call_expr (fndecl, 1, arg);
2250 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2252 if (mode != GET_MODE (op0))
2253 op0 = convert_to_mode (mode, op0, 0);
2255 /* Compute into TARGET.
2256 Set TARGET to wherever the result comes back. */
2257 emit_unop_insn (icode, target, op0, UNKNOWN);
2261 target = expand_call (exp, target, target == const0_rtx);
2266 /* Expand a call to the builtin sincos math function.
2267 Return NULL_RTX if a normal call should be emitted rather than expanding the
2268 function in-line. EXP is the expression that is a call to the builtin
2272 expand_builtin_sincos (tree exp)
2274 rtx op0, op1, op2, target1, target2;
2275 enum machine_mode mode;
2276 tree arg, sinp, cosp;
2279 if (!validate_arglist (exp, REAL_TYPE,
2280 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2283 arg = CALL_EXPR_ARG (exp, 0);
2284 sinp = CALL_EXPR_ARG (exp, 1);
2285 cosp = CALL_EXPR_ARG (exp, 2);
2287 /* Make a suitable register to place result in. */
2288 mode = TYPE_MODE (TREE_TYPE (arg));
2290 /* Check if sincos insn is available, otherwise emit the call. */
2291 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2294 target1 = gen_reg_rtx (mode);
2295 target2 = gen_reg_rtx (mode);
2297 op0 = expand_normal (arg);
2298 op1 = expand_normal (build_fold_indirect_ref (sinp));
2299 op2 = expand_normal (build_fold_indirect_ref (cosp));
2301 /* Compute into target1 and target2.
2302 Set TARGET to wherever the result comes back. */
2303 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2304 gcc_assert (result);
2306 /* Move target1 and target2 to the memory locations indicated
2308 emit_move_insn (op1, target1);
2309 emit_move_insn (op2, target2);
2314 /* Expand a call to the internal cexpi builtin to the sincos math function.
2315 EXP is the expression that is a call to the builtin function; if convenient,
2316 the result should be placed in TARGET. SUBTARGET may be used as the target
2317 for computing one of EXP's operands. */
2320 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2322 tree fndecl = get_callee_fndecl (exp);
2324 enum machine_mode mode;
2327 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2330 arg = CALL_EXPR_ARG (exp, 0);
2331 type = TREE_TYPE (arg);
2332 mode = TYPE_MODE (TREE_TYPE (arg));
2334 /* Try expanding via a sincos optab, fall back to emitting a libcall
2335 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2336 is only generated from sincos, cexp or if we have either of them. */
2337 if (sincos_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2339 op1 = gen_reg_rtx (mode);
2340 op2 = gen_reg_rtx (mode);
2342 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2344 /* Compute into op1 and op2. */
2345 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2347 else if (TARGET_HAS_SINCOS)
2349 tree call, fn = NULL_TREE;
2353 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2354 fn = built_in_decls[BUILT_IN_SINCOSF];
2355 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2356 fn = built_in_decls[BUILT_IN_SINCOS];
2357 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2358 fn = built_in_decls[BUILT_IN_SINCOSL];
2362 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2363 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2364 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2365 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2366 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2367 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2369 /* Make sure not to fold the sincos call again. */
2370 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2371 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2372 call, 3, arg, top1, top2));
2376 tree call, fn = NULL_TREE, narg;
2377 tree ctype = build_complex_type (type);
2379 /* We can expand via the C99 cexp function. */
2380 gcc_assert (TARGET_C99_FUNCTIONS);
2382 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2383 fn = built_in_decls[BUILT_IN_CEXPF];
2384 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2385 fn = built_in_decls[BUILT_IN_CEXP];
2386 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2387 fn = built_in_decls[BUILT_IN_CEXPL];
2390 narg = fold_build2 (COMPLEX_EXPR, ctype,
2391 build_real (type, dconst0), arg);
2393 /* Make sure not to fold the cexp call again. */
2394 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2395 return expand_expr (build_call_nary (ctype, call, 1, arg),
2396 target, VOIDmode, 0);
2399 /* Now build the proper return type. */
2400 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2401 make_tree (TREE_TYPE (arg), op2),
2402 make_tree (TREE_TYPE (arg), op1)),
2403 target, VOIDmode, 0);
2406 /* Expand a call to one of the builtin rounding functions gcc defines
2407 as an extension (lfloor and lceil). As these are gcc extensions we
2408 do not need to worry about setting errno to EDOM.
2409 If expanding via optab fails, lower expression to (int)(floor(x)).
2410 EXP is the expression that is a call to the builtin function;
2411 if convenient, the result should be placed in TARGET. SUBTARGET may
2412 be used as the target for computing one of EXP's operands. */
2415 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2417 convert_optab builtin_optab;
2418 rtx op0, insns, tmp;
2419 tree fndecl = get_callee_fndecl (exp);
2420 enum built_in_function fallback_fn;
2421 tree fallback_fndecl;
2422 enum machine_mode mode;
2425 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2428 arg = CALL_EXPR_ARG (exp, 0);
2430 switch (DECL_FUNCTION_CODE (fndecl))
2432 CASE_FLT_FN (BUILT_IN_LCEIL):
2433 CASE_FLT_FN (BUILT_IN_LLCEIL):
2434 builtin_optab = lceil_optab;
2435 fallback_fn = BUILT_IN_CEIL;
2438 CASE_FLT_FN (BUILT_IN_LFLOOR):
2439 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2440 builtin_optab = lfloor_optab;
2441 fallback_fn = BUILT_IN_FLOOR;
2448 /* Make a suitable register to place result in. */
2449 mode = TYPE_MODE (TREE_TYPE (exp));
2451 target = gen_reg_rtx (mode);
2453 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2454 need to expand the argument again. This way, we will not perform
2455 side-effects more the once. */
2456 narg = builtin_save_expr (arg);
2460 exp = build_call_expr (fndecl, 1, arg);
2463 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2467 /* Compute into TARGET. */
2468 if (expand_sfix_optab (target, op0, builtin_optab))
2470 /* Output the entire sequence. */
2471 insns = get_insns ();
2477 /* If we were unable to expand via the builtin, stop the sequence
2478 (without outputting the insns). */
2481 /* Fall back to floating point rounding optab. */
2482 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2483 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2484 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2485 gcc_assert (fallback_fndecl != NULL_TREE);
2486 exp = build_call_expr (fallback_fndecl, 1, arg);
2488 tmp = expand_normal (exp);
2490 /* Truncate the result of floating point optab to integer
2491 via expand_fix (). */
2492 target = gen_reg_rtx (mode);
2493 expand_fix (target, tmp, 0);
2498 /* Expand a call to one of the builtin math functions doing integer
2500 Return 0 if a normal call should be emitted rather than expanding the
2501 function in-line. EXP is the expression that is a call to the builtin
2502 function; if convenient, the result should be placed in TARGET.
2503 SUBTARGET may be used as the target for computing one of EXP's operands. */
2506 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2508 convert_optab builtin_optab;
2510 tree fndecl = get_callee_fndecl (exp);
2512 enum machine_mode mode;
2514 /* There's no easy way to detect the case we need to set EDOM. */
2515 if (flag_errno_math)
2518 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2521 arg = CALL_EXPR_ARG (exp, 0);
2523 switch (DECL_FUNCTION_CODE (fndecl))
2525 CASE_FLT_FN (BUILT_IN_LRINT):
2526 CASE_FLT_FN (BUILT_IN_LLRINT):
2527 builtin_optab = lrint_optab; break;
2528 CASE_FLT_FN (BUILT_IN_LROUND):
2529 CASE_FLT_FN (BUILT_IN_LLROUND):
2530 builtin_optab = lround_optab; break;
2535 /* Make a suitable register to place result in. */
2536 mode = TYPE_MODE (TREE_TYPE (exp));
2538 target = gen_reg_rtx (mode);
2540 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2541 need to expand the argument again. This way, we will not perform
2542 side-effects more the once. */
2543 narg = builtin_save_expr (arg);
2547 exp = build_call_expr (fndecl, 1, arg);
2550 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2554 if (expand_sfix_optab (target, op0, builtin_optab))
2556 /* Output the entire sequence. */
2557 insns = get_insns ();
2563 /* If we were unable to expand via the builtin, stop the sequence
2564 (without outputting the insns) and call to the library function
2565 with the stabilized argument list. */
2568 target = expand_call (exp, target, target == const0_rtx);
2573 /* To evaluate powi(x,n), the floating point value x raised to the
2574 constant integer exponent n, we use a hybrid algorithm that
2575 combines the "window method" with look-up tables. For an
2576 introduction to exponentiation algorithms and "addition chains",
2577 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2578 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2579 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2580 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2582 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2583 multiplications to inline before calling the system library's pow
2584 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2585 so this default never requires calling pow, powf or powl. */
2587 #ifndef POWI_MAX_MULTS
2588 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2591 /* The size of the "optimal power tree" lookup table. All
2592 exponents less than this value are simply looked up in the
2593 powi_table below. This threshold is also used to size the
2594 cache of pseudo registers that hold intermediate results. */
2595 #define POWI_TABLE_SIZE 256
2597 /* The size, in bits of the window, used in the "window method"
2598 exponentiation algorithm. This is equivalent to a radix of
2599 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2600 #define POWI_WINDOW_SIZE 3
2602 /* The following table is an efficient representation of an
2603 "optimal power tree". For each value, i, the corresponding
2604 value, j, in the table states than an optimal evaluation
2605 sequence for calculating pow(x,i) can be found by evaluating
2606 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2607 100 integers is given in Knuth's "Seminumerical algorithms". */
2609 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2611 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2612 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2613 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2614 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2615 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2616 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2617 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2618 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2619 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2620 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2621 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2622 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2623 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2624 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2625 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2626 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2627 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2628 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2629 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2630 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2631 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2632 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2633 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2634 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2635 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2636 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2637 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2638 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2639 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2640 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2641 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2642 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2646 /* Return the number of multiplications required to calculate
2647 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2648 subroutine of powi_cost. CACHE is an array indicating
2649 which exponents have already been calculated. */
2652 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2654 /* If we've already calculated this exponent, then this evaluation
2655 doesn't require any additional multiplications. */
2660 return powi_lookup_cost (n - powi_table[n], cache)
2661 + powi_lookup_cost (powi_table[n], cache) + 1;
2664 /* Return the number of multiplications required to calculate
2665 powi(x,n) for an arbitrary x, given the exponent N. This
2666 function needs to be kept in sync with expand_powi below. */
2669 powi_cost (HOST_WIDE_INT n)
2671 bool cache[POWI_TABLE_SIZE];
2672 unsigned HOST_WIDE_INT digit;
2673 unsigned HOST_WIDE_INT val;
2679 /* Ignore the reciprocal when calculating the cost. */
2680 val = (n < 0) ? -n : n;
2682 /* Initialize the exponent cache. */
2683 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2688 while (val >= POWI_TABLE_SIZE)
2692 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2693 result += powi_lookup_cost (digit, cache)
2694 + POWI_WINDOW_SIZE + 1;
2695 val >>= POWI_WINDOW_SIZE;
2704 return result + powi_lookup_cost (val, cache);
2707 /* Recursive subroutine of expand_powi. This function takes the array,
2708 CACHE, of already calculated exponents and an exponent N and returns
2709 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2712 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2714 unsigned HOST_WIDE_INT digit;
2718 if (n < POWI_TABLE_SIZE)
2723 target = gen_reg_rtx (mode);
2726 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2727 op1 = expand_powi_1 (mode, powi_table[n], cache);
2731 target = gen_reg_rtx (mode);
2732 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2733 op0 = expand_powi_1 (mode, n - digit, cache);
2734 op1 = expand_powi_1 (mode, digit, cache);
2738 target = gen_reg_rtx (mode);
2739 op0 = expand_powi_1 (mode, n >> 1, cache);
2743 result = expand_mult (mode, op0, op1, target, 0);
2744 if (result != target)
2745 emit_move_insn (target, result);
2749 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2750 floating point operand in mode MODE, and N is the exponent. This
2751 function needs to be kept in sync with powi_cost above. */
2754 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2756 unsigned HOST_WIDE_INT val;
2757 rtx cache[POWI_TABLE_SIZE];
2761 return CONST1_RTX (mode);
2763 val = (n < 0) ? -n : n;
2765 memset (cache, 0, sizeof (cache));
2768 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2770 /* If the original exponent was negative, reciprocate the result. */
2772 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2773 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2778 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2779 a normal call should be emitted rather than expanding the function
2780 in-line. EXP is the expression that is a call to the builtin
2781 function; if convenient, the result should be placed in TARGET. */
2784 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2788 tree type = TREE_TYPE (exp);
2789 REAL_VALUE_TYPE cint, c, c2;
2792 enum machine_mode mode = TYPE_MODE (type);
2794 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2797 arg0 = CALL_EXPR_ARG (exp, 0);
2798 arg1 = CALL_EXPR_ARG (exp, 1);
2800 if (TREE_CODE (arg1) != REAL_CST
2801 || TREE_OVERFLOW (arg1))
2802 return expand_builtin_mathfn_2 (exp, target, subtarget);
2804 /* Handle constant exponents. */
2806 /* For integer valued exponents we can expand to an optimal multiplication
2807 sequence using expand_powi. */
2808 c = TREE_REAL_CST (arg1);
2809 n = real_to_integer (&c);
2810 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2811 if (real_identical (&c, &cint)
2812 && ((n >= -1 && n <= 2)
2813 || (flag_unsafe_math_optimizations
2815 && powi_cost (n) <= POWI_MAX_MULTS)))
2817 op = expand_expr (arg0, subtarget, VOIDmode, 0);
2820 op = force_reg (mode, op);
2821 op = expand_powi (op, mode, n);
2826 narg0 = builtin_save_expr (arg0);
2828 /* If the exponent is not integer valued, check if it is half of an integer.
2829 In this case we can expand to sqrt (x) * x**(n/2). */
2830 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2831 if (fn != NULL_TREE)
2833 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2834 n = real_to_integer (&c2);
2835 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2836 if (real_identical (&c2, &cint)
2837 && ((flag_unsafe_math_optimizations
2839 && powi_cost (n/2) <= POWI_MAX_MULTS)
2842 tree call_expr = build_call_expr (fn, 1, narg0);
2843 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2846 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2847 op2 = force_reg (mode, op2);
2848 op2 = expand_powi (op2, mode, abs (n / 2));
2849 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2850 0, OPTAB_LIB_WIDEN);
2851 /* If the original exponent was negative, reciprocate the
2854 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2855 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2861 /* Try if the exponent is a third of an integer. In this case
2862 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2863 different from pow (x, 1./3.) due to rounding and behavior
2864 with negative x we need to constrain this transformation to
2865 unsafe math and positive x or finite math. */
2866 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2868 && flag_unsafe_math_optimizations
2869 && (tree_expr_nonnegative_p (arg0)
2870 || !HONOR_NANS (mode)))
2872 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2873 real_round (&c2, mode, &c2);
2874 n = real_to_integer (&c2);
2875 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2876 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2877 real_convert (&c2, mode, &c2);
2878 if (real_identical (&c2, &c)
2880 && powi_cost (n/3) <= POWI_MAX_MULTS)
2883 tree call_expr = build_call_expr (fn, 1,narg0);
2884 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2885 if (abs (n) % 3 == 2)
2886 op = expand_simple_binop (mode, MULT, op, op, op,
2887 0, OPTAB_LIB_WIDEN);
2890 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2891 op2 = force_reg (mode, op2);
2892 op2 = expand_powi (op2, mode, abs (n / 3));
2893 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2894 0, OPTAB_LIB_WIDEN);
2895 /* If the original exponent was negative, reciprocate the
2898 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2899 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2905 /* Fall back to optab expansion. */
2906 return expand_builtin_mathfn_2 (exp, target, subtarget);
2909 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2910 a normal call should be emitted rather than expanding the function
2911 in-line. EXP is the expression that is a call to the builtin
2912 function; if convenient, the result should be placed in TARGET. */
2915 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2919 enum machine_mode mode;
2920 enum machine_mode mode2;
2922 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2925 arg0 = CALL_EXPR_ARG (exp, 0);
2926 arg1 = CALL_EXPR_ARG (exp, 1);
2927 mode = TYPE_MODE (TREE_TYPE (exp));
2929 /* Handle constant power. */
2931 if (TREE_CODE (arg1) == INTEGER_CST
2932 && !TREE_OVERFLOW (arg1))
2934 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2936 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2937 Otherwise, check the number of multiplications required. */
2938 if ((TREE_INT_CST_HIGH (arg1) == 0
2939 || TREE_INT_CST_HIGH (arg1) == -1)
2940 && ((n >= -1 && n <= 2)
2942 && powi_cost (n) <= POWI_MAX_MULTS)))
2944 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2945 op0 = force_reg (mode, op0);
2946 return expand_powi (op0, mode, n);
2950 /* Emit a libcall to libgcc. */
2952 /* Mode of the 2nd argument must match that of an int. */
2953 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2955 if (target == NULL_RTX)
2956 target = gen_reg_rtx (mode);
2958 op0 = expand_expr (arg0, subtarget, mode, 0);
2959 if (GET_MODE (op0) != mode)
2960 op0 = convert_to_mode (mode, op0, 0);
2961 op1 = expand_expr (arg1, 0, mode2, 0);
2962 if (GET_MODE (op1) != mode2)
2963 op1 = convert_to_mode (mode2, op1, 0);
2965 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2966 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2967 op0, mode, op1, mode2);
2972 /* Expand expression EXP which is a call to the strlen builtin. Return
2973 NULL_RTX if we failed the caller should emit a normal call, otherwise
2974 try to get the result in TARGET, if convenient. */
2977 expand_builtin_strlen (tree exp, rtx target,
2978 enum machine_mode target_mode)
2980 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2986 tree src = CALL_EXPR_ARG (exp, 0);
2987 rtx result, src_reg, char_rtx, before_strlen;
2988 enum machine_mode insn_mode = target_mode, char_mode;
2989 enum insn_code icode = CODE_FOR_nothing;
2992 /* If the length can be computed at compile-time, return it. */
2993 len = c_strlen (src, 0);
2995 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2997 /* If the length can be computed at compile-time and is constant
2998 integer, but there are side-effects in src, evaluate
2999 src for side-effects, then return len.
3000 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3001 can be optimized into: i++; x = 3; */
3002 len = c_strlen (src, 1);
3003 if (len && TREE_CODE (len) == INTEGER_CST)
3005 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3006 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3009 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3011 /* If SRC is not a pointer type, don't do this operation inline. */
3015 /* Bail out if we can't compute strlen in the right mode. */
3016 while (insn_mode != VOIDmode)
3018 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
3019 if (icode != CODE_FOR_nothing)
3022 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3024 if (insn_mode == VOIDmode)
3027 /* Make a place to write the result of the instruction. */
3031 && GET_MODE (result) == insn_mode
3032 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3033 result = gen_reg_rtx (insn_mode);
3035 /* Make a place to hold the source address. We will not expand
3036 the actual source until we are sure that the expansion will
3037 not fail -- there are trees that cannot be expanded twice. */
3038 src_reg = gen_reg_rtx (Pmode);
3040 /* Mark the beginning of the strlen sequence so we can emit the
3041 source operand later. */
3042 before_strlen = get_last_insn ();
3044 char_rtx = const0_rtx;
3045 char_mode = insn_data[(int) icode].operand[2].mode;
3046 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3048 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3050 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3051 char_rtx, GEN_INT (align));
3056 /* Now that we are assured of success, expand the source. */
3058 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3060 emit_move_insn (src_reg, pat);
3065 emit_insn_after (pat, before_strlen);
3067 emit_insn_before (pat, get_insns ());
3069 /* Return the value in the proper mode for this function. */
3070 if (GET_MODE (result) == target_mode)
3072 else if (target != 0)
3073 convert_move (target, result, 0);
3075 target = convert_to_mode (target_mode, result, 0);
3081 /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the
3082 caller should emit a normal call, otherwise try to get the result
3083 in TARGET, if convenient (and in mode MODE if that's convenient). */
3086 expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode)
3088 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3090 tree type = TREE_TYPE (exp);
3091 tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0),
3092 CALL_EXPR_ARG (exp, 1), type);
3094 return expand_expr (result, target, mode, EXPAND_NORMAL);
3099 /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the
3100 caller should emit a normal call, otherwise try to get the result
3101 in TARGET, if convenient (and in mode MODE if that's convenient). */
3104 expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode)
3106 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3108 tree type = TREE_TYPE (exp);
3109 tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0),
3110 CALL_EXPR_ARG (exp, 1), type);
3112 return expand_expr (result, target, mode, EXPAND_NORMAL);
3114 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3119 /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the
3120 caller should emit a normal call, otherwise try to get the result
3121 in TARGET, if convenient (and in mode MODE if that's convenient). */
3124 expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode)
3126 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3128 tree type = TREE_TYPE (exp);
3129 tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0),
3130 CALL_EXPR_ARG (exp, 1), type);
3132 return expand_expr (result, target, mode, EXPAND_NORMAL);
3137 /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the
3138 caller should emit a normal call, otherwise try to get the result
3139 in TARGET, if convenient (and in mode MODE if that's convenient). */
3142 expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode)
3144 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3146 tree type = TREE_TYPE (exp);
3147 tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0),
3148 CALL_EXPR_ARG (exp, 1), type);
3150 return expand_expr (result, target, mode, EXPAND_NORMAL);
3155 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3156 bytes from constant string DATA + OFFSET and return it as target
3160 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3161 enum machine_mode mode)
3163 const char *str = (const char *) data;
3165 gcc_assert (offset >= 0
3166 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3167 <= strlen (str) + 1));
3169 return c_readstr (str + offset, mode);
3172 /* Expand a call EXP to the memcpy builtin.
3173 Return NULL_RTX if we failed, the caller should emit a normal call,
3174 otherwise try to get the result in TARGET, if convenient (and in
3175 mode MODE if that's convenient). */
3178 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3180 tree fndecl = get_callee_fndecl (exp);
3182 if (!validate_arglist (exp,
3183 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3187 tree dest = CALL_EXPR_ARG (exp, 0);
3188 tree src = CALL_EXPR_ARG (exp, 1);
3189 tree len = CALL_EXPR_ARG (exp, 2);
3190 const char *src_str;
3191 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3192 unsigned int dest_align
3193 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3194 rtx dest_mem, src_mem, dest_addr, len_rtx;
3195 tree result = fold_builtin_memory_op (dest, src, len,
3196 TREE_TYPE (TREE_TYPE (fndecl)),
3198 HOST_WIDE_INT expected_size = -1;
3199 unsigned int expected_align = 0;
3203 while (TREE_CODE (result) == COMPOUND_EXPR)
3205 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3207 result = TREE_OPERAND (result, 1);
3209 return expand_expr (result, target, mode, EXPAND_NORMAL);
3212 /* If DEST is not a pointer type, call the normal function. */
3213 if (dest_align == 0)
3216 /* If either SRC is not a pointer type, don't do this
3217 operation in-line. */
3221 stringop_block_profile (exp, &expected_align, &expected_size);
3222 if (expected_align < dest_align)
3223 expected_align = dest_align;
3224 dest_mem = get_memory_rtx (dest, len);
3225 set_mem_align (dest_mem, dest_align);
3226 len_rtx = expand_normal (len);
3227 src_str = c_getstr (src);
3229 /* If SRC is a string constant and block move would be done
3230 by pieces, we can avoid loading the string from memory
3231 and only stored the computed constants. */
3233 && GET_CODE (len_rtx) == CONST_INT
3234 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3235 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3236 (void *) src_str, dest_align))
3238 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3239 builtin_memcpy_read_str,
3240 (void *) src_str, dest_align, 0);
3241 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3242 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3246 src_mem = get_memory_rtx (src, len);
3247 set_mem_align (src_mem, src_align);
3249 /* Copy word part most expediently. */
3250 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3251 CALL_EXPR_TAILCALL (exp)
3252 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3253 expected_align, expected_size);
3257 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3258 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3264 /* Expand a call EXP to the mempcpy builtin.
3265 Return NULL_RTX if we failed; the caller should emit a normal call,
3266 otherwise try to get the result in TARGET, if convenient (and in
3267 mode MODE if that's convenient). If ENDP is 0 return the
3268 destination pointer, if ENDP is 1 return the end pointer ala
3269 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3273 expand_builtin_mempcpy(tree exp, rtx target, enum machine_mode mode)
3275 if (!validate_arglist (exp,
3276 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3280 tree dest = CALL_EXPR_ARG (exp, 0);
3281 tree src = CALL_EXPR_ARG (exp, 1);
3282 tree len = CALL_EXPR_ARG (exp, 2);
3283 return expand_builtin_mempcpy_args (dest, src, len,
3285 target, mode, /*endp=*/ 1);
3289 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3290 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3291 so that this can also be called without constructing an actual CALL_EXPR.
3292 TYPE is the return type of the call. The other arguments and return value
3293 are the same as for expand_builtin_mempcpy. */
3296 expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
3297 rtx target, enum machine_mode mode, int endp)
3299 /* If return value is ignored, transform mempcpy into memcpy. */
3300 if (target == const0_rtx)
3302 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3307 return expand_expr (build_call_expr (fn, 3, dest, src, len),
3308 target, mode, EXPAND_NORMAL);
3312 const char *src_str;
3313 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3314 unsigned int dest_align
3315 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3316 rtx dest_mem, src_mem, len_rtx;
3317 tree result = fold_builtin_memory_op (dest, src, len, type, false, endp);
3321 while (TREE_CODE (result) == COMPOUND_EXPR)
3323 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3325 result = TREE_OPERAND (result, 1);
3327 return expand_expr (result, target, mode, EXPAND_NORMAL);
3330 /* If either SRC or DEST is not a pointer type, don't do this
3331 operation in-line. */
3332 if (dest_align == 0 || src_align == 0)
3335 /* If LEN is not constant, call the normal function. */
3336 if (! host_integerp (len, 1))
3339 len_rtx = expand_normal (len);
3340 src_str = c_getstr (src);
3342 /* If SRC is a string constant and block move would be done
3343 by pieces, we can avoid loading the string from memory
3344 and only stored the computed constants. */
3346 && GET_CODE (len_rtx) == CONST_INT
3347 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3348 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3349 (void *) src_str, dest_align))
3351 dest_mem = get_memory_rtx (dest, len);
3352 set_mem_align (dest_mem, dest_align);
3353 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3354 builtin_memcpy_read_str,
3355 (void *) src_str, dest_align, endp);
3356 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3357 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3361 if (GET_CODE (len_rtx) == CONST_INT
3362 && can_move_by_pieces (INTVAL (len_rtx),
3363 MIN (dest_align, src_align)))
3365 dest_mem = get_memory_rtx (dest, len);
3366 set_mem_align (dest_mem, dest_align);
3367 src_mem = get_memory_rtx (src, len);
3368 set_mem_align (src_mem, src_align);
3369 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3370 MIN (dest_align, src_align), endp);
3371 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3372 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3380 /* Expand expression EXP, which is a call to the memmove builtin. Return
3381 NULL_RTX if we failed; the caller should emit a normal call. */
3384 expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore)
3386 if (!validate_arglist (exp,
3387 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3391 tree dest = CALL_EXPR_ARG (exp, 0);
3392 tree src = CALL_EXPR_ARG (exp, 1);
3393 tree len = CALL_EXPR_ARG (exp, 2);
3394 return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp),
3395 target, mode, ignore);
3399 /* Helper function to do the actual work for expand_builtin_memmove. The
3400 arguments to the builtin_memmove call DEST, SRC, and LEN are broken out
3401 so that this can also be called without constructing an actual CALL_EXPR.
3402 TYPE is the return type of the call. The other arguments and return value
3403 are the same as for expand_builtin_memmove. */
3406 expand_builtin_memmove_args (tree dest, tree src, tree len,
3407 tree type, rtx target, enum machine_mode mode,
3410 tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3);
3414 while (TREE_CODE (result) == COMPOUND_EXPR)
3416 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3418 result = TREE_OPERAND (result, 1);
3420 return expand_expr (result, target, mode, EXPAND_NORMAL);
3423 /* Otherwise, call the normal function. */
3427 /* Expand expression EXP, which is a call to the bcopy builtin. Return
3428 NULL_RTX if we failed the caller should emit a normal call. */
3431 expand_builtin_bcopy (tree exp, int ignore)
3433 tree type = TREE_TYPE (exp);
3434 tree src, dest, size;
3436 if (!validate_arglist (exp,
3437 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3440 src = CALL_EXPR_ARG (exp, 0);
3441 dest = CALL_EXPR_ARG (exp, 1);
3442 size = CALL_EXPR_ARG (exp, 2);
3444 /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z).
3445 This is done this way so that if it isn't expanded inline, we fall
3446 back to calling bcopy instead of memmove. */
3447 return expand_builtin_memmove_args (dest, src,
3448 fold_convert (sizetype, size),
3449 type, const0_rtx, VOIDmode,
3454 # define HAVE_movstr 0
3455 # define CODE_FOR_movstr CODE_FOR_nothing
3458 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3459 we failed, the caller should emit a normal call, otherwise try to
3460 get the result in TARGET, if convenient. If ENDP is 0 return the
3461 destination pointer, if ENDP is 1 return the end pointer ala
3462 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3466 expand_movstr (tree dest, tree src, rtx target, int endp)
3472 const struct insn_data * data;
3477 dest_mem = get_memory_rtx (dest, NULL);
3478 src_mem = get_memory_rtx (src, NULL);
3481 target = force_reg (Pmode, XEXP (dest_mem, 0));
3482 dest_mem = replace_equiv_address (dest_mem, target);
3483 end = gen_reg_rtx (Pmode);
3487 if (target == 0 || target == const0_rtx)
3489 end = gen_reg_rtx (Pmode);
3497 data = insn_data + CODE_FOR_movstr;
3499 if (data->operand[0].mode != VOIDmode)
3500 end = gen_lowpart (data->operand[0].mode, end);
3502 insn = data->genfun (end, dest_mem, src_mem);
3508 /* movstr is supposed to set end to the address of the NUL
3509 terminator. If the caller requested a mempcpy-like return value,
3511 if (endp == 1 && target != const0_rtx)
3513 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3514 emit_move_insn (target, force_operand (tem, NULL_RTX));
3520 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3521 NULL_RTX if we failed the caller should emit a normal call, otherwise
3522 try to get the result in TARGET, if convenient (and in mode MODE if that's
3526 expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode)
3528 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3530 tree dest = CALL_EXPR_ARG (exp, 0);
3531 tree src = CALL_EXPR_ARG (exp, 1);
3532 return expand_builtin_strcpy_args (fndecl, dest, src, target, mode);
3537 /* Helper function to do the actual work for expand_builtin_strcpy. The
3538 arguments to the builtin_strcpy call DEST and SRC are broken out
3539 so that this can also be called without constructing an actual CALL_EXPR.
3540 The other arguments and return value are the same as for
3541 expand_builtin_strcpy. */
3544 expand_builtin_strcpy_args&nbs