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