PR c/78673 - sprintf missing attribute nonnull on destination argument
[gcc.git] / gcc / builtins.c
1 /* Expand builtin functions.
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.c instead. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "memmodel.h"
32 #include "gimple.h"
33 #include "predict.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "tree-vrp.h"
37 #include "tree-ssanames.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "diagnostic-core.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "fold-const-call.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "varasm.h"
49 #include "tree-object-size.h"
50 #include "realmpfr.h"
51 #include "cfgrtl.h"
52 #include "except.h"
53 #include "dojump.h"
54 #include "explow.h"
55 #include "stmt.h"
56 #include "expr.h"
57 #include "libfuncs.h"
58 #include "output.h"
59 #include "typeclass.h"
60 #include "langhooks.h"
61 #include "value-prof.h"
62 #include "builtins.h"
63 #include "asan.h"
64 #include "cilk.h"
65 #include "tree-chkp.h"
66 #include "rtl-chkp.h"
67 #include "internal-fn.h"
68 #include "case-cfn-macros.h"
69 #include "gimple-fold.h"
70 #include "intl.h"
71
72 struct target_builtins default_target_builtins;
73 #if SWITCHABLE_TARGET
74 struct target_builtins *this_target_builtins = &default_target_builtins;
75 #endif
76
77 /* Define the names of the builtin function types and codes. */
78 const char *const built_in_class_names[BUILT_IN_LAST]
79 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
80
81 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
82 const char * built_in_names[(int) END_BUILTINS] =
83 {
84 #include "builtins.def"
85 };
86
87 /* Setup an array of builtin_info_type, make sure each element decl is
88 initialized to NULL_TREE. */
89 builtin_info_type builtin_info[(int)END_BUILTINS];
90
91 /* Non-zero if __builtin_constant_p should be folded right away. */
92 bool force_folding_builtin_constant_p;
93
94 static rtx c_readstr (const char *, machine_mode);
95 static int target_char_cast (tree, char *);
96 static rtx get_memory_rtx (tree, tree);
97 static int apply_args_size (void);
98 static int apply_result_size (void);
99 static rtx result_vector (int, rtx);
100 static void expand_builtin_prefetch (tree);
101 static rtx expand_builtin_apply_args (void);
102 static rtx expand_builtin_apply_args_1 (void);
103 static rtx expand_builtin_apply (rtx, rtx, rtx);
104 static void expand_builtin_return (rtx);
105 static enum type_class type_to_class (tree);
106 static rtx expand_builtin_classify_type (tree);
107 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
109 static rtx expand_builtin_interclass_mathfn (tree, rtx);
110 static rtx expand_builtin_sincos (tree);
111 static rtx expand_builtin_cexpi (tree, rtx);
112 static rtx expand_builtin_int_roundingfn (tree, rtx);
113 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
114 static rtx expand_builtin_next_arg (void);
115 static rtx expand_builtin_va_start (tree);
116 static rtx expand_builtin_va_end (tree);
117 static rtx expand_builtin_va_copy (tree);
118 static rtx expand_builtin_strcmp (tree, rtx);
119 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
120 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
121 static rtx expand_builtin_memcpy (tree, rtx);
122 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
123 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
124 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
125 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
127 machine_mode, int, tree);
128 static rtx expand_builtin_strcat (tree, rtx);
129 static rtx expand_builtin_strcpy (tree, rtx);
130 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
131 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
132 static rtx expand_builtin_strncat (tree, rtx);
133 static rtx expand_builtin_strncpy (tree, rtx);
134 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
135 static rtx expand_builtin_memset (tree, rtx, machine_mode);
136 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
137 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
138 static rtx expand_builtin_bzero (tree);
139 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
140 static rtx expand_builtin_alloca (tree, bool);
141 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
142 static rtx expand_builtin_frame_address (tree, tree);
143 static tree stabilize_va_list_loc (location_t, tree, int);
144 static rtx expand_builtin_expect (tree, rtx);
145 static tree fold_builtin_constant_p (tree);
146 static tree fold_builtin_classify_type (tree);
147 static tree fold_builtin_strlen (location_t, tree, tree);
148 static tree fold_builtin_inf (location_t, tree, int);
149 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
150 static bool validate_arg (const_tree, enum tree_code code, bool = false);
151 static rtx expand_builtin_fabs (tree, rtx, rtx);
152 static rtx expand_builtin_signbit (tree, rtx);
153 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
154 static tree fold_builtin_isascii (location_t, tree);
155 static tree fold_builtin_toascii (location_t, tree);
156 static tree fold_builtin_isdigit (location_t, tree);
157 static tree fold_builtin_fabs (location_t, tree, tree);
158 static tree fold_builtin_abs (location_t, tree, tree);
159 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
160 enum tree_code);
161 static tree fold_builtin_0 (location_t, tree);
162 static tree fold_builtin_1 (location_t, tree, tree);
163 static tree fold_builtin_2 (location_t, tree, tree, tree);
164 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
165 static tree fold_builtin_varargs (location_t, tree, tree*, int);
166
167 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
168 static tree fold_builtin_strspn (location_t, tree, tree);
169 static tree fold_builtin_strcspn (location_t, tree, tree);
170
171 static rtx expand_builtin_object_size (tree);
172 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
173 enum built_in_function);
174 static void maybe_emit_chk_warning (tree, enum built_in_function);
175 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
176 static void maybe_emit_free_warning (tree);
177 static tree fold_builtin_object_size (tree, tree);
178
179 unsigned HOST_WIDE_INT target_newline;
180 unsigned HOST_WIDE_INT target_percent;
181 static unsigned HOST_WIDE_INT target_c;
182 static unsigned HOST_WIDE_INT target_s;
183 char target_percent_c[3];
184 char target_percent_s[3];
185 char target_percent_s_newline[4];
186 static tree do_mpfr_remquo (tree, tree, tree);
187 static tree do_mpfr_lgamma_r (tree, tree, tree);
188 static void expand_builtin_sync_synchronize (void);
189
190 /* Return true if NAME starts with __builtin_ or __sync_. */
191
192 static bool
193 is_builtin_name (const char *name)
194 {
195 if (strncmp (name, "__builtin_", 10) == 0)
196 return true;
197 if (strncmp (name, "__sync_", 7) == 0)
198 return true;
199 if (strncmp (name, "__atomic_", 9) == 0)
200 return true;
201 if (flag_cilkplus
202 && (!strcmp (name, "__cilkrts_detach")
203 || !strcmp (name, "__cilkrts_pop_frame")))
204 return true;
205 return false;
206 }
207
208
209 /* Return true if DECL is a function symbol representing a built-in. */
210
211 bool
212 is_builtin_fn (tree decl)
213 {
214 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
215 }
216
217 /* Return true if NODE should be considered for inline expansion regardless
218 of the optimization level. This means whenever a function is invoked with
219 its "internal" name, which normally contains the prefix "__builtin". */
220
221 bool
222 called_as_built_in (tree node)
223 {
224 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
225 we want the name used to call the function, not the name it
226 will have. */
227 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
228 return is_builtin_name (name);
229 }
230
231 /* Compute values M and N such that M divides (address of EXP - N) and such
232 that N < M. If these numbers can be determined, store M in alignp and N in
233 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
234 *alignp and any bit-offset to *bitposp.
235
236 Note that the address (and thus the alignment) computed here is based
237 on the address to which a symbol resolves, whereas DECL_ALIGN is based
238 on the address at which an object is actually located. These two
239 addresses are not always the same. For example, on ARM targets,
240 the address &foo of a Thumb function foo() has the lowest bit set,
241 whereas foo() itself starts on an even address.
242
243 If ADDR_P is true we are taking the address of the memory reference EXP
244 and thus cannot rely on the access taking place. */
245
246 static bool
247 get_object_alignment_2 (tree exp, unsigned int *alignp,
248 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
249 {
250 HOST_WIDE_INT bitsize, bitpos;
251 tree offset;
252 machine_mode mode;
253 int unsignedp, reversep, volatilep;
254 unsigned int align = BITS_PER_UNIT;
255 bool known_alignment = false;
256
257 /* Get the innermost object and the constant (bitpos) and possibly
258 variable (offset) offset of the access. */
259 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
260 &unsignedp, &reversep, &volatilep);
261
262 /* Extract alignment information from the innermost object and
263 possibly adjust bitpos and offset. */
264 if (TREE_CODE (exp) == FUNCTION_DECL)
265 {
266 /* Function addresses can encode extra information besides their
267 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
268 allows the low bit to be used as a virtual bit, we know
269 that the address itself must be at least 2-byte aligned. */
270 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
271 align = 2 * BITS_PER_UNIT;
272 }
273 else if (TREE_CODE (exp) == LABEL_DECL)
274 ;
275 else if (TREE_CODE (exp) == CONST_DECL)
276 {
277 /* The alignment of a CONST_DECL is determined by its initializer. */
278 exp = DECL_INITIAL (exp);
279 align = TYPE_ALIGN (TREE_TYPE (exp));
280 if (CONSTANT_CLASS_P (exp))
281 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
282
283 known_alignment = true;
284 }
285 else if (DECL_P (exp))
286 {
287 align = DECL_ALIGN (exp);
288 known_alignment = true;
289 }
290 else if (TREE_CODE (exp) == INDIRECT_REF
291 || TREE_CODE (exp) == MEM_REF
292 || TREE_CODE (exp) == TARGET_MEM_REF)
293 {
294 tree addr = TREE_OPERAND (exp, 0);
295 unsigned ptr_align;
296 unsigned HOST_WIDE_INT ptr_bitpos;
297 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
298
299 /* If the address is explicitely aligned, handle that. */
300 if (TREE_CODE (addr) == BIT_AND_EXPR
301 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
302 {
303 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
304 ptr_bitmask *= BITS_PER_UNIT;
305 align = least_bit_hwi (ptr_bitmask);
306 addr = TREE_OPERAND (addr, 0);
307 }
308
309 known_alignment
310 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
311 align = MAX (ptr_align, align);
312
313 /* Re-apply explicit alignment to the bitpos. */
314 ptr_bitpos &= ptr_bitmask;
315
316 /* The alignment of the pointer operand in a TARGET_MEM_REF
317 has to take the variable offset parts into account. */
318 if (TREE_CODE (exp) == TARGET_MEM_REF)
319 {
320 if (TMR_INDEX (exp))
321 {
322 unsigned HOST_WIDE_INT step = 1;
323 if (TMR_STEP (exp))
324 step = TREE_INT_CST_LOW (TMR_STEP (exp));
325 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
326 }
327 if (TMR_INDEX2 (exp))
328 align = BITS_PER_UNIT;
329 known_alignment = false;
330 }
331
332 /* When EXP is an actual memory reference then we can use
333 TYPE_ALIGN of a pointer indirection to derive alignment.
334 Do so only if get_pointer_alignment_1 did not reveal absolute
335 alignment knowledge and if using that alignment would
336 improve the situation. */
337 if (!addr_p && !known_alignment
338 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
339 align = TYPE_ALIGN (TREE_TYPE (exp));
340 else
341 {
342 /* Else adjust bitpos accordingly. */
343 bitpos += ptr_bitpos;
344 if (TREE_CODE (exp) == MEM_REF
345 || TREE_CODE (exp) == TARGET_MEM_REF)
346 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
347 }
348 }
349 else if (TREE_CODE (exp) == STRING_CST)
350 {
351 /* STRING_CST are the only constant objects we allow to be not
352 wrapped inside a CONST_DECL. */
353 align = TYPE_ALIGN (TREE_TYPE (exp));
354 if (CONSTANT_CLASS_P (exp))
355 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
356
357 known_alignment = true;
358 }
359
360 /* If there is a non-constant offset part extract the maximum
361 alignment that can prevail. */
362 if (offset)
363 {
364 unsigned int trailing_zeros = tree_ctz (offset);
365 if (trailing_zeros < HOST_BITS_PER_INT)
366 {
367 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
368 if (inner)
369 align = MIN (align, inner);
370 }
371 }
372
373 *alignp = align;
374 *bitposp = bitpos & (*alignp - 1);
375 return known_alignment;
376 }
377
378 /* For a memory reference expression EXP compute values M and N such that M
379 divides (&EXP - N) and such that N < M. If these numbers can be determined,
380 store M in alignp and N in *BITPOSP and return true. Otherwise return false
381 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
382
383 bool
384 get_object_alignment_1 (tree exp, unsigned int *alignp,
385 unsigned HOST_WIDE_INT *bitposp)
386 {
387 return get_object_alignment_2 (exp, alignp, bitposp, false);
388 }
389
390 /* Return the alignment in bits of EXP, an object. */
391
392 unsigned int
393 get_object_alignment (tree exp)
394 {
395 unsigned HOST_WIDE_INT bitpos = 0;
396 unsigned int align;
397
398 get_object_alignment_1 (exp, &align, &bitpos);
399
400 /* align and bitpos now specify known low bits of the pointer.
401 ptr & (align - 1) == bitpos. */
402
403 if (bitpos != 0)
404 align = least_bit_hwi (bitpos);
405 return align;
406 }
407
408 /* For a pointer valued expression EXP compute values M and N such that M
409 divides (EXP - N) and such that N < M. If these numbers can be determined,
410 store M in alignp and N in *BITPOSP and return true. Return false if
411 the results are just a conservative approximation.
412
413 If EXP is not a pointer, false is returned too. */
414
415 bool
416 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
417 unsigned HOST_WIDE_INT *bitposp)
418 {
419 STRIP_NOPS (exp);
420
421 if (TREE_CODE (exp) == ADDR_EXPR)
422 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
423 alignp, bitposp, true);
424 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
425 {
426 unsigned int align;
427 unsigned HOST_WIDE_INT bitpos;
428 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
429 &align, &bitpos);
430 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
431 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
432 else
433 {
434 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
435 if (trailing_zeros < HOST_BITS_PER_INT)
436 {
437 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
438 if (inner)
439 align = MIN (align, inner);
440 }
441 }
442 *alignp = align;
443 *bitposp = bitpos & (align - 1);
444 return res;
445 }
446 else if (TREE_CODE (exp) == SSA_NAME
447 && POINTER_TYPE_P (TREE_TYPE (exp)))
448 {
449 unsigned int ptr_align, ptr_misalign;
450 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
451
452 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
453 {
454 *bitposp = ptr_misalign * BITS_PER_UNIT;
455 *alignp = ptr_align * BITS_PER_UNIT;
456 /* Make sure to return a sensible alignment when the multiplication
457 by BITS_PER_UNIT overflowed. */
458 if (*alignp == 0)
459 *alignp = 1u << (HOST_BITS_PER_INT - 1);
460 /* We cannot really tell whether this result is an approximation. */
461 return false;
462 }
463 else
464 {
465 *bitposp = 0;
466 *alignp = BITS_PER_UNIT;
467 return false;
468 }
469 }
470 else if (TREE_CODE (exp) == INTEGER_CST)
471 {
472 *alignp = BIGGEST_ALIGNMENT;
473 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
474 & (BIGGEST_ALIGNMENT - 1));
475 return true;
476 }
477
478 *bitposp = 0;
479 *alignp = BITS_PER_UNIT;
480 return false;
481 }
482
483 /* Return the alignment in bits of EXP, a pointer valued expression.
484 The alignment returned is, by default, the alignment of the thing that
485 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
486
487 Otherwise, look at the expression to see if we can do better, i.e., if the
488 expression is actually pointing at an object whose alignment is tighter. */
489
490 unsigned int
491 get_pointer_alignment (tree exp)
492 {
493 unsigned HOST_WIDE_INT bitpos = 0;
494 unsigned int align;
495
496 get_pointer_alignment_1 (exp, &align, &bitpos);
497
498 /* align and bitpos now specify known low bits of the pointer.
499 ptr & (align - 1) == bitpos. */
500
501 if (bitpos != 0)
502 align = least_bit_hwi (bitpos);
503
504 return align;
505 }
506
507 /* Return the number of non-zero elements in the sequence
508 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
509 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
510
511 static unsigned
512 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
513 {
514 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
515
516 unsigned n;
517
518 if (eltsize == 1)
519 {
520 /* Optimize the common case of plain char. */
521 for (n = 0; n < maxelts; n++)
522 {
523 const char *elt = (const char*) ptr + n;
524 if (!*elt)
525 break;
526 }
527 }
528 else
529 {
530 for (n = 0; n < maxelts; n++)
531 {
532 const char *elt = (const char*) ptr + n * eltsize;
533 if (!memcmp (elt, "\0\0\0\0", eltsize))
534 break;
535 }
536 }
537 return n;
538 }
539
540 /* Compute the length of a null-terminated character string or wide
541 character string handling character sizes of 1, 2, and 4 bytes.
542 TREE_STRING_LENGTH is not the right way because it evaluates to
543 the size of the character array in bytes (as opposed to characters)
544 and because it can contain a zero byte in the middle.
545
546 ONLY_VALUE should be nonzero if the result is not going to be emitted
547 into the instruction stream and zero if it is going to be expanded.
548 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
549 is returned, otherwise NULL, since
550 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
551 evaluate the side-effects.
552
553 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
554 accesses. Note that this implies the result is not going to be emitted
555 into the instruction stream.
556
557 The value returned is of type `ssizetype'.
558
559 Unfortunately, string_constant can't access the values of const char
560 arrays with initializers, so neither can we do so here. */
561
562 tree
563 c_strlen (tree src, int only_value)
564 {
565 STRIP_NOPS (src);
566 if (TREE_CODE (src) == COND_EXPR
567 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
568 {
569 tree len1, len2;
570
571 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
572 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
573 if (tree_int_cst_equal (len1, len2))
574 return len1;
575 }
576
577 if (TREE_CODE (src) == COMPOUND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
579 return c_strlen (TREE_OPERAND (src, 1), only_value);
580
581 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
582
583 /* Offset from the beginning of the string in bytes. */
584 tree byteoff;
585 src = string_constant (src, &byteoff);
586 if (src == 0)
587 return NULL_TREE;
588
589 /* Determine the size of the string element. */
590 unsigned eltsize
591 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))));
592
593 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
594 length of SRC. */
595 unsigned maxelts = TREE_STRING_LENGTH (src) / eltsize - 1;
596
597 /* PTR can point to the byte representation of any string type, including
598 char* and wchar_t*. */
599 const char *ptr = TREE_STRING_POINTER (src);
600
601 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
602 {
603 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
604 compute the offset to the following null if we don't know where to
605 start searching for it. */
606 if (string_length (ptr, eltsize, maxelts) < maxelts)
607 {
608 /* Return when an embedded null character is found. */
609 return NULL_TREE;
610 }
611
612 /* We don't know the starting offset, but we do know that the string
613 has no internal zero bytes. We can assume that the offset falls
614 within the bounds of the string; otherwise, the programmer deserves
615 what he gets. Subtract the offset from the length of the string,
616 and return that. This would perhaps not be valid if we were dealing
617 with named arrays in addition to literal string constants. */
618
619 return size_diffop_loc (loc, size_int (maxelts * eltsize), byteoff);
620 }
621
622 /* Offset from the beginning of the string in elements. */
623 HOST_WIDE_INT eltoff;
624
625 /* We have a known offset into the string. Start searching there for
626 a null character if we can represent it as a single HOST_WIDE_INT. */
627 if (byteoff == 0)
628 eltoff = 0;
629 else if (! tree_fits_shwi_p (byteoff))
630 eltoff = -1;
631 else
632 eltoff = tree_to_shwi (byteoff) / eltsize;
633
634 /* If the offset is known to be out of bounds, warn, and call strlen at
635 runtime. */
636 if (eltoff < 0 || eltoff > maxelts)
637 {
638 /* Suppress multiple warnings for propagated constant strings. */
639 if (only_value != 2
640 && !TREE_NO_WARNING (src))
641 {
642 warning_at (loc, 0, "offset %qwi outside bounds of constant string",
643 eltoff);
644 TREE_NO_WARNING (src) = 1;
645 }
646 return NULL_TREE;
647 }
648
649 /* Use strlen to search for the first zero byte. Since any strings
650 constructed with build_string will have nulls appended, we win even
651 if we get handed something like (char[4])"abcd".
652
653 Since ELTOFF is our starting index into the string, no further
654 calculation is needed. */
655 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
656 maxelts - eltoff);
657
658 return ssize_int (len);
659 }
660
661 /* Return a constant integer corresponding to target reading
662 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
663
664 static rtx
665 c_readstr (const char *str, machine_mode mode)
666 {
667 HOST_WIDE_INT ch;
668 unsigned int i, j;
669 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
670
671 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
672 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
673 / HOST_BITS_PER_WIDE_INT;
674
675 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
676 for (i = 0; i < len; i++)
677 tmp[i] = 0;
678
679 ch = 1;
680 for (i = 0; i < GET_MODE_SIZE (mode); i++)
681 {
682 j = i;
683 if (WORDS_BIG_ENDIAN)
684 j = GET_MODE_SIZE (mode) - i - 1;
685 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
686 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
687 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
688 j *= BITS_PER_UNIT;
689
690 if (ch)
691 ch = (unsigned char) str[i];
692 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
693 }
694
695 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
696 return immed_wide_int_const (c, mode);
697 }
698
699 /* Cast a target constant CST to target CHAR and if that value fits into
700 host char type, return zero and put that value into variable pointed to by
701 P. */
702
703 static int
704 target_char_cast (tree cst, char *p)
705 {
706 unsigned HOST_WIDE_INT val, hostval;
707
708 if (TREE_CODE (cst) != INTEGER_CST
709 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
710 return 1;
711
712 /* Do not care if it fits or not right here. */
713 val = TREE_INT_CST_LOW (cst);
714
715 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
716 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
717
718 hostval = val;
719 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
720 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
721
722 if (val != hostval)
723 return 1;
724
725 *p = hostval;
726 return 0;
727 }
728
729 /* Similar to save_expr, but assumes that arbitrary code is not executed
730 in between the multiple evaluations. In particular, we assume that a
731 non-addressable local variable will not be modified. */
732
733 static tree
734 builtin_save_expr (tree exp)
735 {
736 if (TREE_CODE (exp) == SSA_NAME
737 || (TREE_ADDRESSABLE (exp) == 0
738 && (TREE_CODE (exp) == PARM_DECL
739 || (VAR_P (exp) && !TREE_STATIC (exp)))))
740 return exp;
741
742 return save_expr (exp);
743 }
744
745 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
746 times to get the address of either a higher stack frame, or a return
747 address located within it (depending on FNDECL_CODE). */
748
749 static rtx
750 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
751 {
752 int i;
753 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
754 if (tem == NULL_RTX)
755 {
756 /* For a zero count with __builtin_return_address, we don't care what
757 frame address we return, because target-specific definitions will
758 override us. Therefore frame pointer elimination is OK, and using
759 the soft frame pointer is OK.
760
761 For a nonzero count, or a zero count with __builtin_frame_address,
762 we require a stable offset from the current frame pointer to the
763 previous one, so we must use the hard frame pointer, and
764 we must disable frame pointer elimination. */
765 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
766 tem = frame_pointer_rtx;
767 else
768 {
769 tem = hard_frame_pointer_rtx;
770
771 /* Tell reload not to eliminate the frame pointer. */
772 crtl->accesses_prior_frames = 1;
773 }
774 }
775
776 if (count > 0)
777 SETUP_FRAME_ADDRESSES ();
778
779 /* On the SPARC, the return address is not in the frame, it is in a
780 register. There is no way to access it off of the current frame
781 pointer, but it can be accessed off the previous frame pointer by
782 reading the value from the register window save area. */
783 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
784 count--;
785
786 /* Scan back COUNT frames to the specified frame. */
787 for (i = 0; i < count; i++)
788 {
789 /* Assume the dynamic chain pointer is in the word that the
790 frame address points to, unless otherwise specified. */
791 tem = DYNAMIC_CHAIN_ADDRESS (tem);
792 tem = memory_address (Pmode, tem);
793 tem = gen_frame_mem (Pmode, tem);
794 tem = copy_to_reg (tem);
795 }
796
797 /* For __builtin_frame_address, return what we've got. But, on
798 the SPARC for example, we may have to add a bias. */
799 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
800 return FRAME_ADDR_RTX (tem);
801
802 /* For __builtin_return_address, get the return address from that frame. */
803 #ifdef RETURN_ADDR_RTX
804 tem = RETURN_ADDR_RTX (count, tem);
805 #else
806 tem = memory_address (Pmode,
807 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
808 tem = gen_frame_mem (Pmode, tem);
809 #endif
810 return tem;
811 }
812
813 /* Alias set used for setjmp buffer. */
814 static alias_set_type setjmp_alias_set = -1;
815
816 /* Construct the leading half of a __builtin_setjmp call. Control will
817 return to RECEIVER_LABEL. This is also called directly by the SJLJ
818 exception handling code. */
819
820 void
821 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
822 {
823 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
824 rtx stack_save;
825 rtx mem;
826
827 if (setjmp_alias_set == -1)
828 setjmp_alias_set = new_alias_set ();
829
830 buf_addr = convert_memory_address (Pmode, buf_addr);
831
832 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
833
834 /* We store the frame pointer and the address of receiver_label in
835 the buffer and use the rest of it for the stack save area, which
836 is machine-dependent. */
837
838 mem = gen_rtx_MEM (Pmode, buf_addr);
839 set_mem_alias_set (mem, setjmp_alias_set);
840 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
841
842 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
843 GET_MODE_SIZE (Pmode))),
844 set_mem_alias_set (mem, setjmp_alias_set);
845
846 emit_move_insn (validize_mem (mem),
847 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
848
849 stack_save = gen_rtx_MEM (sa_mode,
850 plus_constant (Pmode, buf_addr,
851 2 * GET_MODE_SIZE (Pmode)));
852 set_mem_alias_set (stack_save, setjmp_alias_set);
853 emit_stack_save (SAVE_NONLOCAL, &stack_save);
854
855 /* If there is further processing to do, do it. */
856 if (targetm.have_builtin_setjmp_setup ())
857 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
858
859 /* We have a nonlocal label. */
860 cfun->has_nonlocal_label = 1;
861 }
862
863 /* Construct the trailing part of a __builtin_setjmp call. This is
864 also called directly by the SJLJ exception handling code.
865 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
866
867 void
868 expand_builtin_setjmp_receiver (rtx receiver_label)
869 {
870 rtx chain;
871
872 /* Mark the FP as used when we get here, so we have to make sure it's
873 marked as used by this function. */
874 emit_use (hard_frame_pointer_rtx);
875
876 /* Mark the static chain as clobbered here so life information
877 doesn't get messed up for it. */
878 chain = targetm.calls.static_chain (current_function_decl, true);
879 if (chain && REG_P (chain))
880 emit_clobber (chain);
881
882 /* Now put in the code to restore the frame pointer, and argument
883 pointer, if needed. */
884 if (! targetm.have_nonlocal_goto ())
885 {
886 /* First adjust our frame pointer to its actual value. It was
887 previously set to the start of the virtual area corresponding to
888 the stacked variables when we branched here and now needs to be
889 adjusted to the actual hardware fp value.
890
891 Assignments to virtual registers are converted by
892 instantiate_virtual_regs into the corresponding assignment
893 to the underlying register (fp in this case) that makes
894 the original assignment true.
895 So the following insn will actually be decrementing fp by
896 STARTING_FRAME_OFFSET. */
897 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
898
899 /* Restoring the frame pointer also modifies the hard frame pointer.
900 Mark it used (so that the previous assignment remains live once
901 the frame pointer is eliminated) and clobbered (to represent the
902 implicit update from the assignment). */
903 emit_use (hard_frame_pointer_rtx);
904 emit_clobber (hard_frame_pointer_rtx);
905 }
906
907 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
908 {
909 /* If the argument pointer can be eliminated in favor of the
910 frame pointer, we don't need to restore it. We assume here
911 that if such an elimination is present, it can always be used.
912 This is the case on all known machines; if we don't make this
913 assumption, we do unnecessary saving on many machines. */
914 size_t i;
915 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
916
917 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
918 if (elim_regs[i].from == ARG_POINTER_REGNUM
919 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
920 break;
921
922 if (i == ARRAY_SIZE (elim_regs))
923 {
924 /* Now restore our arg pointer from the address at which it
925 was saved in our stack frame. */
926 emit_move_insn (crtl->args.internal_arg_pointer,
927 copy_to_reg (get_arg_pointer_save_area ()));
928 }
929 }
930
931 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
932 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
933 else if (targetm.have_nonlocal_goto_receiver ())
934 emit_insn (targetm.gen_nonlocal_goto_receiver ());
935 else
936 { /* Nothing */ }
937
938 /* We must not allow the code we just generated to be reordered by
939 scheduling. Specifically, the update of the frame pointer must
940 happen immediately, not later. */
941 emit_insn (gen_blockage ());
942 }
943
944 /* __builtin_longjmp is passed a pointer to an array of five words (not
945 all will be used on all machines). It operates similarly to the C
946 library function of the same name, but is more efficient. Much of
947 the code below is copied from the handling of non-local gotos. */
948
949 static void
950 expand_builtin_longjmp (rtx buf_addr, rtx value)
951 {
952 rtx fp, lab, stack;
953 rtx_insn *insn, *last;
954 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
955
956 /* DRAP is needed for stack realign if longjmp is expanded to current
957 function */
958 if (SUPPORTS_STACK_ALIGNMENT)
959 crtl->need_drap = true;
960
961 if (setjmp_alias_set == -1)
962 setjmp_alias_set = new_alias_set ();
963
964 buf_addr = convert_memory_address (Pmode, buf_addr);
965
966 buf_addr = force_reg (Pmode, buf_addr);
967
968 /* We require that the user must pass a second argument of 1, because
969 that is what builtin_setjmp will return. */
970 gcc_assert (value == const1_rtx);
971
972 last = get_last_insn ();
973 if (targetm.have_builtin_longjmp ())
974 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
975 else
976 {
977 fp = gen_rtx_MEM (Pmode, buf_addr);
978 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
979 GET_MODE_SIZE (Pmode)));
980
981 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
982 2 * GET_MODE_SIZE (Pmode)));
983 set_mem_alias_set (fp, setjmp_alias_set);
984 set_mem_alias_set (lab, setjmp_alias_set);
985 set_mem_alias_set (stack, setjmp_alias_set);
986
987 /* Pick up FP, label, and SP from the block and jump. This code is
988 from expand_goto in stmt.c; see there for detailed comments. */
989 if (targetm.have_nonlocal_goto ())
990 /* We have to pass a value to the nonlocal_goto pattern that will
991 get copied into the static_chain pointer, but it does not matter
992 what that value is, because builtin_setjmp does not use it. */
993 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
994 else
995 {
996 lab = copy_to_reg (lab);
997
998 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
999 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1000
1001 emit_move_insn (hard_frame_pointer_rtx, fp);
1002 emit_stack_restore (SAVE_NONLOCAL, stack);
1003
1004 emit_use (hard_frame_pointer_rtx);
1005 emit_use (stack_pointer_rtx);
1006 emit_indirect_jump (lab);
1007 }
1008 }
1009
1010 /* Search backwards and mark the jump insn as a non-local goto.
1011 Note that this precludes the use of __builtin_longjmp to a
1012 __builtin_setjmp target in the same function. However, we've
1013 already cautioned the user that these functions are for
1014 internal exception handling use only. */
1015 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1016 {
1017 gcc_assert (insn != last);
1018
1019 if (JUMP_P (insn))
1020 {
1021 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1022 break;
1023 }
1024 else if (CALL_P (insn))
1025 break;
1026 }
1027 }
1028
1029 static inline bool
1030 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1031 {
1032 return (iter->i < iter->n);
1033 }
1034
1035 /* This function validates the types of a function call argument list
1036 against a specified list of tree_codes. If the last specifier is a 0,
1037 that represents an ellipsis, otherwise the last specifier must be a
1038 VOID_TYPE. */
1039
1040 static bool
1041 validate_arglist (const_tree callexpr, ...)
1042 {
1043 enum tree_code code;
1044 bool res = 0;
1045 va_list ap;
1046 const_call_expr_arg_iterator iter;
1047 const_tree arg;
1048
1049 va_start (ap, callexpr);
1050 init_const_call_expr_arg_iterator (callexpr, &iter);
1051
1052 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1053 bitmap argmap = get_nonnull_args (callexpr);
1054
1055 for (unsigned argno = 1; ; ++argno)
1056 {
1057 code = (enum tree_code) va_arg (ap, int);
1058 bool nonnull = false;
1059
1060 switch (code)
1061 {
1062 case 0:
1063 /* This signifies an ellipses, any further arguments are all ok. */
1064 res = true;
1065 goto end;
1066 case VOID_TYPE:
1067 /* This signifies an endlink, if no arguments remain, return
1068 true, otherwise return false. */
1069 res = !more_const_call_expr_args_p (&iter);
1070 goto end;
1071 case POINTER_TYPE:
1072 /* The actual argument must be nonnull when either the whole
1073 called function has been declared nonnull, or when the formal
1074 argument corresponding to the actual argument has been. */
1075 if (argmap)
1076 nonnull = bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno);
1077 /* FALLTHRU */
1078 default:
1079 /* If no parameters remain or the parameter's code does not
1080 match the specified code, return false. Otherwise continue
1081 checking any remaining arguments. */
1082 arg = next_const_call_expr_arg (&iter);
1083 if (!validate_arg (arg, code, nonnull))
1084 goto end;
1085 break;
1086 }
1087 }
1088
1089 /* We need gotos here since we can only have one VA_CLOSE in a
1090 function. */
1091 end: ;
1092 va_end (ap);
1093
1094 BITMAP_FREE (argmap);
1095
1096 return res;
1097 }
1098
1099 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1100 and the address of the save area. */
1101
1102 static rtx
1103 expand_builtin_nonlocal_goto (tree exp)
1104 {
1105 tree t_label, t_save_area;
1106 rtx r_label, r_save_area, r_fp, r_sp;
1107 rtx_insn *insn;
1108
1109 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1110 return NULL_RTX;
1111
1112 t_label = CALL_EXPR_ARG (exp, 0);
1113 t_save_area = CALL_EXPR_ARG (exp, 1);
1114
1115 r_label = expand_normal (t_label);
1116 r_label = convert_memory_address (Pmode, r_label);
1117 r_save_area = expand_normal (t_save_area);
1118 r_save_area = convert_memory_address (Pmode, r_save_area);
1119 /* Copy the address of the save location to a register just in case it was
1120 based on the frame pointer. */
1121 r_save_area = copy_to_reg (r_save_area);
1122 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1123 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1124 plus_constant (Pmode, r_save_area,
1125 GET_MODE_SIZE (Pmode)));
1126
1127 crtl->has_nonlocal_goto = 1;
1128
1129 /* ??? We no longer need to pass the static chain value, afaik. */
1130 if (targetm.have_nonlocal_goto ())
1131 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1132 else
1133 {
1134 r_label = copy_to_reg (r_label);
1135
1136 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1137 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1138
1139 /* Restore frame pointer for containing function. */
1140 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1141 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1142
1143 /* USE of hard_frame_pointer_rtx added for consistency;
1144 not clear if really needed. */
1145 emit_use (hard_frame_pointer_rtx);
1146 emit_use (stack_pointer_rtx);
1147
1148 /* If the architecture is using a GP register, we must
1149 conservatively assume that the target function makes use of it.
1150 The prologue of functions with nonlocal gotos must therefore
1151 initialize the GP register to the appropriate value, and we
1152 must then make sure that this value is live at the point
1153 of the jump. (Note that this doesn't necessarily apply
1154 to targets with a nonlocal_goto pattern; they are free
1155 to implement it in their own way. Note also that this is
1156 a no-op if the GP register is a global invariant.) */
1157 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1158 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1159 emit_use (pic_offset_table_rtx);
1160
1161 emit_indirect_jump (r_label);
1162 }
1163
1164 /* Search backwards to the jump insn and mark it as a
1165 non-local goto. */
1166 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1167 {
1168 if (JUMP_P (insn))
1169 {
1170 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1171 break;
1172 }
1173 else if (CALL_P (insn))
1174 break;
1175 }
1176
1177 return const0_rtx;
1178 }
1179
1180 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1181 (not all will be used on all machines) that was passed to __builtin_setjmp.
1182 It updates the stack pointer in that block to the current value. This is
1183 also called directly by the SJLJ exception handling code. */
1184
1185 void
1186 expand_builtin_update_setjmp_buf (rtx buf_addr)
1187 {
1188 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1189 rtx stack_save
1190 = gen_rtx_MEM (sa_mode,
1191 memory_address
1192 (sa_mode,
1193 plus_constant (Pmode, buf_addr,
1194 2 * GET_MODE_SIZE (Pmode))));
1195
1196 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1197 }
1198
1199 /* Expand a call to __builtin_prefetch. For a target that does not support
1200 data prefetch, evaluate the memory address argument in case it has side
1201 effects. */
1202
1203 static void
1204 expand_builtin_prefetch (tree exp)
1205 {
1206 tree arg0, arg1, arg2;
1207 int nargs;
1208 rtx op0, op1, op2;
1209
1210 if (!validate_arglist (exp, POINTER_TYPE, 0))
1211 return;
1212
1213 arg0 = CALL_EXPR_ARG (exp, 0);
1214
1215 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1216 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1217 locality). */
1218 nargs = call_expr_nargs (exp);
1219 if (nargs > 1)
1220 arg1 = CALL_EXPR_ARG (exp, 1);
1221 else
1222 arg1 = integer_zero_node;
1223 if (nargs > 2)
1224 arg2 = CALL_EXPR_ARG (exp, 2);
1225 else
1226 arg2 = integer_three_node;
1227
1228 /* Argument 0 is an address. */
1229 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1230
1231 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1232 if (TREE_CODE (arg1) != INTEGER_CST)
1233 {
1234 error ("second argument to %<__builtin_prefetch%> must be a constant");
1235 arg1 = integer_zero_node;
1236 }
1237 op1 = expand_normal (arg1);
1238 /* Argument 1 must be either zero or one. */
1239 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1240 {
1241 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1242 " using zero");
1243 op1 = const0_rtx;
1244 }
1245
1246 /* Argument 2 (locality) must be a compile-time constant int. */
1247 if (TREE_CODE (arg2) != INTEGER_CST)
1248 {
1249 error ("third argument to %<__builtin_prefetch%> must be a constant");
1250 arg2 = integer_zero_node;
1251 }
1252 op2 = expand_normal (arg2);
1253 /* Argument 2 must be 0, 1, 2, or 3. */
1254 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1255 {
1256 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1257 op2 = const0_rtx;
1258 }
1259
1260 if (targetm.have_prefetch ())
1261 {
1262 struct expand_operand ops[3];
1263
1264 create_address_operand (&ops[0], op0);
1265 create_integer_operand (&ops[1], INTVAL (op1));
1266 create_integer_operand (&ops[2], INTVAL (op2));
1267 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1268 return;
1269 }
1270
1271 /* Don't do anything with direct references to volatile memory, but
1272 generate code to handle other side effects. */
1273 if (!MEM_P (op0) && side_effects_p (op0))
1274 emit_insn (op0);
1275 }
1276
1277 /* Get a MEM rtx for expression EXP which is the address of an operand
1278 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1279 the maximum length of the block of memory that might be accessed or
1280 NULL if unknown. */
1281
1282 static rtx
1283 get_memory_rtx (tree exp, tree len)
1284 {
1285 tree orig_exp = exp;
1286 rtx addr, mem;
1287
1288 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1289 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1290 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1291 exp = TREE_OPERAND (exp, 0);
1292
1293 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1294 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1295
1296 /* Get an expression we can use to find the attributes to assign to MEM.
1297 First remove any nops. */
1298 while (CONVERT_EXPR_P (exp)
1299 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1300 exp = TREE_OPERAND (exp, 0);
1301
1302 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1303 (as builtin stringops may alias with anything). */
1304 exp = fold_build2 (MEM_REF,
1305 build_array_type (char_type_node,
1306 build_range_type (sizetype,
1307 size_one_node, len)),
1308 exp, build_int_cst (ptr_type_node, 0));
1309
1310 /* If the MEM_REF has no acceptable address, try to get the base object
1311 from the original address we got, and build an all-aliasing
1312 unknown-sized access to that one. */
1313 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1314 set_mem_attributes (mem, exp, 0);
1315 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1316 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1317 0))))
1318 {
1319 exp = build_fold_addr_expr (exp);
1320 exp = fold_build2 (MEM_REF,
1321 build_array_type (char_type_node,
1322 build_range_type (sizetype,
1323 size_zero_node,
1324 NULL)),
1325 exp, build_int_cst (ptr_type_node, 0));
1326 set_mem_attributes (mem, exp, 0);
1327 }
1328 set_mem_alias_set (mem, 0);
1329 return mem;
1330 }
1331 \f
1332 /* Built-in functions to perform an untyped call and return. */
1333
1334 #define apply_args_mode \
1335 (this_target_builtins->x_apply_args_mode)
1336 #define apply_result_mode \
1337 (this_target_builtins->x_apply_result_mode)
1338
1339 /* Return the size required for the block returned by __builtin_apply_args,
1340 and initialize apply_args_mode. */
1341
1342 static int
1343 apply_args_size (void)
1344 {
1345 static int size = -1;
1346 int align;
1347 unsigned int regno;
1348 machine_mode mode;
1349
1350 /* The values computed by this function never change. */
1351 if (size < 0)
1352 {
1353 /* The first value is the incoming arg-pointer. */
1354 size = GET_MODE_SIZE (Pmode);
1355
1356 /* The second value is the structure value address unless this is
1357 passed as an "invisible" first argument. */
1358 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1359 size += GET_MODE_SIZE (Pmode);
1360
1361 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1362 if (FUNCTION_ARG_REGNO_P (regno))
1363 {
1364 mode = targetm.calls.get_raw_arg_mode (regno);
1365
1366 gcc_assert (mode != VOIDmode);
1367
1368 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1369 if (size % align != 0)
1370 size = CEIL (size, align) * align;
1371 size += GET_MODE_SIZE (mode);
1372 apply_args_mode[regno] = mode;
1373 }
1374 else
1375 {
1376 apply_args_mode[regno] = VOIDmode;
1377 }
1378 }
1379 return size;
1380 }
1381
1382 /* Return the size required for the block returned by __builtin_apply,
1383 and initialize apply_result_mode. */
1384
1385 static int
1386 apply_result_size (void)
1387 {
1388 static int size = -1;
1389 int align, regno;
1390 machine_mode mode;
1391
1392 /* The values computed by this function never change. */
1393 if (size < 0)
1394 {
1395 size = 0;
1396
1397 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1398 if (targetm.calls.function_value_regno_p (regno))
1399 {
1400 mode = targetm.calls.get_raw_result_mode (regno);
1401
1402 gcc_assert (mode != VOIDmode);
1403
1404 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1405 if (size % align != 0)
1406 size = CEIL (size, align) * align;
1407 size += GET_MODE_SIZE (mode);
1408 apply_result_mode[regno] = mode;
1409 }
1410 else
1411 apply_result_mode[regno] = VOIDmode;
1412
1413 /* Allow targets that use untyped_call and untyped_return to override
1414 the size so that machine-specific information can be stored here. */
1415 #ifdef APPLY_RESULT_SIZE
1416 size = APPLY_RESULT_SIZE;
1417 #endif
1418 }
1419 return size;
1420 }
1421
1422 /* Create a vector describing the result block RESULT. If SAVEP is true,
1423 the result block is used to save the values; otherwise it is used to
1424 restore the values. */
1425
1426 static rtx
1427 result_vector (int savep, rtx result)
1428 {
1429 int regno, size, align, nelts;
1430 machine_mode mode;
1431 rtx reg, mem;
1432 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1433
1434 size = nelts = 0;
1435 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1436 if ((mode = apply_result_mode[regno]) != VOIDmode)
1437 {
1438 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1439 if (size % align != 0)
1440 size = CEIL (size, align) * align;
1441 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1442 mem = adjust_address (result, mode, size);
1443 savevec[nelts++] = (savep
1444 ? gen_rtx_SET (mem, reg)
1445 : gen_rtx_SET (reg, mem));
1446 size += GET_MODE_SIZE (mode);
1447 }
1448 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1449 }
1450
1451 /* Save the state required to perform an untyped call with the same
1452 arguments as were passed to the current function. */
1453
1454 static rtx
1455 expand_builtin_apply_args_1 (void)
1456 {
1457 rtx registers, tem;
1458 int size, align, regno;
1459 machine_mode mode;
1460 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1461
1462 /* Create a block where the arg-pointer, structure value address,
1463 and argument registers can be saved. */
1464 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1465
1466 /* Walk past the arg-pointer and structure value address. */
1467 size = GET_MODE_SIZE (Pmode);
1468 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1469 size += GET_MODE_SIZE (Pmode);
1470
1471 /* Save each register used in calling a function to the block. */
1472 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 if ((mode = apply_args_mode[regno]) != VOIDmode)
1474 {
1475 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1476 if (size % align != 0)
1477 size = CEIL (size, align) * align;
1478
1479 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1480
1481 emit_move_insn (adjust_address (registers, mode, size), tem);
1482 size += GET_MODE_SIZE (mode);
1483 }
1484
1485 /* Save the arg pointer to the block. */
1486 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1487 /* We need the pointer as the caller actually passed them to us, not
1488 as we might have pretended they were passed. Make sure it's a valid
1489 operand, as emit_move_insn isn't expected to handle a PLUS. */
1490 if (STACK_GROWS_DOWNWARD)
1491 tem
1492 = force_operand (plus_constant (Pmode, tem,
1493 crtl->args.pretend_args_size),
1494 NULL_RTX);
1495 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1496
1497 size = GET_MODE_SIZE (Pmode);
1498
1499 /* Save the structure value address unless this is passed as an
1500 "invisible" first argument. */
1501 if (struct_incoming_value)
1502 {
1503 emit_move_insn (adjust_address (registers, Pmode, size),
1504 copy_to_reg (struct_incoming_value));
1505 size += GET_MODE_SIZE (Pmode);
1506 }
1507
1508 /* Return the address of the block. */
1509 return copy_addr_to_reg (XEXP (registers, 0));
1510 }
1511
1512 /* __builtin_apply_args returns block of memory allocated on
1513 the stack into which is stored the arg pointer, structure
1514 value address, static chain, and all the registers that might
1515 possibly be used in performing a function call. The code is
1516 moved to the start of the function so the incoming values are
1517 saved. */
1518
1519 static rtx
1520 expand_builtin_apply_args (void)
1521 {
1522 /* Don't do __builtin_apply_args more than once in a function.
1523 Save the result of the first call and reuse it. */
1524 if (apply_args_value != 0)
1525 return apply_args_value;
1526 {
1527 /* When this function is called, it means that registers must be
1528 saved on entry to this function. So we migrate the
1529 call to the first insn of this function. */
1530 rtx temp;
1531
1532 start_sequence ();
1533 temp = expand_builtin_apply_args_1 ();
1534 rtx_insn *seq = get_insns ();
1535 end_sequence ();
1536
1537 apply_args_value = temp;
1538
1539 /* Put the insns after the NOTE that starts the function.
1540 If this is inside a start_sequence, make the outer-level insn
1541 chain current, so the code is placed at the start of the
1542 function. If internal_arg_pointer is a non-virtual pseudo,
1543 it needs to be placed after the function that initializes
1544 that pseudo. */
1545 push_topmost_sequence ();
1546 if (REG_P (crtl->args.internal_arg_pointer)
1547 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1548 emit_insn_before (seq, parm_birth_insn);
1549 else
1550 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1551 pop_topmost_sequence ();
1552 return temp;
1553 }
1554 }
1555
1556 /* Perform an untyped call and save the state required to perform an
1557 untyped return of whatever value was returned by the given function. */
1558
1559 static rtx
1560 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1561 {
1562 int size, align, regno;
1563 machine_mode mode;
1564 rtx incoming_args, result, reg, dest, src;
1565 rtx_call_insn *call_insn;
1566 rtx old_stack_level = 0;
1567 rtx call_fusage = 0;
1568 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1569
1570 arguments = convert_memory_address (Pmode, arguments);
1571
1572 /* Create a block where the return registers can be saved. */
1573 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1574
1575 /* Fetch the arg pointer from the ARGUMENTS block. */
1576 incoming_args = gen_reg_rtx (Pmode);
1577 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1578 if (!STACK_GROWS_DOWNWARD)
1579 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1580 incoming_args, 0, OPTAB_LIB_WIDEN);
1581
1582 /* Push a new argument block and copy the arguments. Do not allow
1583 the (potential) memcpy call below to interfere with our stack
1584 manipulations. */
1585 do_pending_stack_adjust ();
1586 NO_DEFER_POP;
1587
1588 /* Save the stack with nonlocal if available. */
1589 if (targetm.have_save_stack_nonlocal ())
1590 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1591 else
1592 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1593
1594 /* Allocate a block of memory onto the stack and copy the memory
1595 arguments to the outgoing arguments address. We can pass TRUE
1596 as the 4th argument because we just saved the stack pointer
1597 and will restore it right after the call. */
1598 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1599
1600 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1601 may have already set current_function_calls_alloca to true.
1602 current_function_calls_alloca won't be set if argsize is zero,
1603 so we have to guarantee need_drap is true here. */
1604 if (SUPPORTS_STACK_ALIGNMENT)
1605 crtl->need_drap = true;
1606
1607 dest = virtual_outgoing_args_rtx;
1608 if (!STACK_GROWS_DOWNWARD)
1609 {
1610 if (CONST_INT_P (argsize))
1611 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1612 else
1613 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1614 }
1615 dest = gen_rtx_MEM (BLKmode, dest);
1616 set_mem_align (dest, PARM_BOUNDARY);
1617 src = gen_rtx_MEM (BLKmode, incoming_args);
1618 set_mem_align (src, PARM_BOUNDARY);
1619 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1620
1621 /* Refer to the argument block. */
1622 apply_args_size ();
1623 arguments = gen_rtx_MEM (BLKmode, arguments);
1624 set_mem_align (arguments, PARM_BOUNDARY);
1625
1626 /* Walk past the arg-pointer and structure value address. */
1627 size = GET_MODE_SIZE (Pmode);
1628 if (struct_value)
1629 size += GET_MODE_SIZE (Pmode);
1630
1631 /* Restore each of the registers previously saved. Make USE insns
1632 for each of these registers for use in making the call. */
1633 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1634 if ((mode = apply_args_mode[regno]) != VOIDmode)
1635 {
1636 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1637 if (size % align != 0)
1638 size = CEIL (size, align) * align;
1639 reg = gen_rtx_REG (mode, regno);
1640 emit_move_insn (reg, adjust_address (arguments, mode, size));
1641 use_reg (&call_fusage, reg);
1642 size += GET_MODE_SIZE (mode);
1643 }
1644
1645 /* Restore the structure value address unless this is passed as an
1646 "invisible" first argument. */
1647 size = GET_MODE_SIZE (Pmode);
1648 if (struct_value)
1649 {
1650 rtx value = gen_reg_rtx (Pmode);
1651 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1652 emit_move_insn (struct_value, value);
1653 if (REG_P (struct_value))
1654 use_reg (&call_fusage, struct_value);
1655 size += GET_MODE_SIZE (Pmode);
1656 }
1657
1658 /* All arguments and registers used for the call are set up by now! */
1659 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1660
1661 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1662 and we don't want to load it into a register as an optimization,
1663 because prepare_call_address already did it if it should be done. */
1664 if (GET_CODE (function) != SYMBOL_REF)
1665 function = memory_address (FUNCTION_MODE, function);
1666
1667 /* Generate the actual call instruction and save the return value. */
1668 if (targetm.have_untyped_call ())
1669 {
1670 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1671 emit_call_insn (targetm.gen_untyped_call (mem, result,
1672 result_vector (1, result)));
1673 }
1674 else if (targetm.have_call_value ())
1675 {
1676 rtx valreg = 0;
1677
1678 /* Locate the unique return register. It is not possible to
1679 express a call that sets more than one return register using
1680 call_value; use untyped_call for that. In fact, untyped_call
1681 only needs to save the return registers in the given block. */
1682 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1683 if ((mode = apply_result_mode[regno]) != VOIDmode)
1684 {
1685 gcc_assert (!valreg); /* have_untyped_call required. */
1686
1687 valreg = gen_rtx_REG (mode, regno);
1688 }
1689
1690 emit_insn (targetm.gen_call_value (valreg,
1691 gen_rtx_MEM (FUNCTION_MODE, function),
1692 const0_rtx, NULL_RTX, const0_rtx));
1693
1694 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1695 }
1696 else
1697 gcc_unreachable ();
1698
1699 /* Find the CALL insn we just emitted, and attach the register usage
1700 information. */
1701 call_insn = last_call_insn ();
1702 add_function_usage_to (call_insn, call_fusage);
1703
1704 /* Restore the stack. */
1705 if (targetm.have_save_stack_nonlocal ())
1706 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1707 else
1708 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1709 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1710
1711 OK_DEFER_POP;
1712
1713 /* Return the address of the result block. */
1714 result = copy_addr_to_reg (XEXP (result, 0));
1715 return convert_memory_address (ptr_mode, result);
1716 }
1717
1718 /* Perform an untyped return. */
1719
1720 static void
1721 expand_builtin_return (rtx result)
1722 {
1723 int size, align, regno;
1724 machine_mode mode;
1725 rtx reg;
1726 rtx_insn *call_fusage = 0;
1727
1728 result = convert_memory_address (Pmode, result);
1729
1730 apply_result_size ();
1731 result = gen_rtx_MEM (BLKmode, result);
1732
1733 if (targetm.have_untyped_return ())
1734 {
1735 rtx vector = result_vector (0, result);
1736 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1737 emit_barrier ();
1738 return;
1739 }
1740
1741 /* Restore the return value and note that each value is used. */
1742 size = 0;
1743 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1744 if ((mode = apply_result_mode[regno]) != VOIDmode)
1745 {
1746 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1747 if (size % align != 0)
1748 size = CEIL (size, align) * align;
1749 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1750 emit_move_insn (reg, adjust_address (result, mode, size));
1751
1752 push_to_sequence (call_fusage);
1753 emit_use (reg);
1754 call_fusage = get_insns ();
1755 end_sequence ();
1756 size += GET_MODE_SIZE (mode);
1757 }
1758
1759 /* Put the USE insns before the return. */
1760 emit_insn (call_fusage);
1761
1762 /* Return whatever values was restored by jumping directly to the end
1763 of the function. */
1764 expand_naked_return ();
1765 }
1766
1767 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1768
1769 static enum type_class
1770 type_to_class (tree type)
1771 {
1772 switch (TREE_CODE (type))
1773 {
1774 case VOID_TYPE: return void_type_class;
1775 case INTEGER_TYPE: return integer_type_class;
1776 case ENUMERAL_TYPE: return enumeral_type_class;
1777 case BOOLEAN_TYPE: return boolean_type_class;
1778 case POINTER_TYPE: return pointer_type_class;
1779 case REFERENCE_TYPE: return reference_type_class;
1780 case OFFSET_TYPE: return offset_type_class;
1781 case REAL_TYPE: return real_type_class;
1782 case COMPLEX_TYPE: return complex_type_class;
1783 case FUNCTION_TYPE: return function_type_class;
1784 case METHOD_TYPE: return method_type_class;
1785 case RECORD_TYPE: return record_type_class;
1786 case UNION_TYPE:
1787 case QUAL_UNION_TYPE: return union_type_class;
1788 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1789 ? string_type_class : array_type_class);
1790 case LANG_TYPE: return lang_type_class;
1791 default: return no_type_class;
1792 }
1793 }
1794
1795 /* Expand a call EXP to __builtin_classify_type. */
1796
1797 static rtx
1798 expand_builtin_classify_type (tree exp)
1799 {
1800 if (call_expr_nargs (exp))
1801 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1802 return GEN_INT (no_type_class);
1803 }
1804
1805 /* This helper macro, meant to be used in mathfn_built_in below,
1806 determines which among a set of three builtin math functions is
1807 appropriate for a given type mode. The `F' and `L' cases are
1808 automatically generated from the `double' case. */
1809 #define CASE_MATHFN(MATHFN) \
1810 CASE_CFN_##MATHFN: \
1811 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1812 fcodel = BUILT_IN_##MATHFN##L ; break;
1813 /* Similar to above, but appends _R after any F/L suffix. */
1814 #define CASE_MATHFN_REENT(MATHFN) \
1815 case CFN_BUILT_IN_##MATHFN##_R: \
1816 case CFN_BUILT_IN_##MATHFN##F_R: \
1817 case CFN_BUILT_IN_##MATHFN##L_R: \
1818 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1819 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1820
1821 /* Return a function equivalent to FN but operating on floating-point
1822 values of type TYPE, or END_BUILTINS if no such function exists.
1823 This is purely an operation on function codes; it does not guarantee
1824 that the target actually has an implementation of the function. */
1825
1826 static built_in_function
1827 mathfn_built_in_2 (tree type, combined_fn fn)
1828 {
1829 built_in_function fcode, fcodef, fcodel;
1830
1831 switch (fn)
1832 {
1833 CASE_MATHFN (ACOS)
1834 CASE_MATHFN (ACOSH)
1835 CASE_MATHFN (ASIN)
1836 CASE_MATHFN (ASINH)
1837 CASE_MATHFN (ATAN)
1838 CASE_MATHFN (ATAN2)
1839 CASE_MATHFN (ATANH)
1840 CASE_MATHFN (CBRT)
1841 CASE_MATHFN (CEIL)
1842 CASE_MATHFN (CEXPI)
1843 CASE_MATHFN (COPYSIGN)
1844 CASE_MATHFN (COS)
1845 CASE_MATHFN (COSH)
1846 CASE_MATHFN (DREM)
1847 CASE_MATHFN (ERF)
1848 CASE_MATHFN (ERFC)
1849 CASE_MATHFN (EXP)
1850 CASE_MATHFN (EXP10)
1851 CASE_MATHFN (EXP2)
1852 CASE_MATHFN (EXPM1)
1853 CASE_MATHFN (FABS)
1854 CASE_MATHFN (FDIM)
1855 CASE_MATHFN (FLOOR)
1856 CASE_MATHFN (FMA)
1857 CASE_MATHFN (FMAX)
1858 CASE_MATHFN (FMIN)
1859 CASE_MATHFN (FMOD)
1860 CASE_MATHFN (FREXP)
1861 CASE_MATHFN (GAMMA)
1862 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1863 CASE_MATHFN (HUGE_VAL)
1864 CASE_MATHFN (HYPOT)
1865 CASE_MATHFN (ILOGB)
1866 CASE_MATHFN (ICEIL)
1867 CASE_MATHFN (IFLOOR)
1868 CASE_MATHFN (INF)
1869 CASE_MATHFN (IRINT)
1870 CASE_MATHFN (IROUND)
1871 CASE_MATHFN (ISINF)
1872 CASE_MATHFN (J0)
1873 CASE_MATHFN (J1)
1874 CASE_MATHFN (JN)
1875 CASE_MATHFN (LCEIL)
1876 CASE_MATHFN (LDEXP)
1877 CASE_MATHFN (LFLOOR)
1878 CASE_MATHFN (LGAMMA)
1879 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1880 CASE_MATHFN (LLCEIL)
1881 CASE_MATHFN (LLFLOOR)
1882 CASE_MATHFN (LLRINT)
1883 CASE_MATHFN (LLROUND)
1884 CASE_MATHFN (LOG)
1885 CASE_MATHFN (LOG10)
1886 CASE_MATHFN (LOG1P)
1887 CASE_MATHFN (LOG2)
1888 CASE_MATHFN (LOGB)
1889 CASE_MATHFN (LRINT)
1890 CASE_MATHFN (LROUND)
1891 CASE_MATHFN (MODF)
1892 CASE_MATHFN (NAN)
1893 CASE_MATHFN (NANS)
1894 CASE_MATHFN (NEARBYINT)
1895 CASE_MATHFN (NEXTAFTER)
1896 CASE_MATHFN (NEXTTOWARD)
1897 CASE_MATHFN (POW)
1898 CASE_MATHFN (POWI)
1899 CASE_MATHFN (POW10)
1900 CASE_MATHFN (REMAINDER)
1901 CASE_MATHFN (REMQUO)
1902 CASE_MATHFN (RINT)
1903 CASE_MATHFN (ROUND)
1904 CASE_MATHFN (SCALB)
1905 CASE_MATHFN (SCALBLN)
1906 CASE_MATHFN (SCALBN)
1907 CASE_MATHFN (SIGNBIT)
1908 CASE_MATHFN (SIGNIFICAND)
1909 CASE_MATHFN (SIN)
1910 CASE_MATHFN (SINCOS)
1911 CASE_MATHFN (SINH)
1912 CASE_MATHFN (SQRT)
1913 CASE_MATHFN (TAN)
1914 CASE_MATHFN (TANH)
1915 CASE_MATHFN (TGAMMA)
1916 CASE_MATHFN (TRUNC)
1917 CASE_MATHFN (Y0)
1918 CASE_MATHFN (Y1)
1919 CASE_MATHFN (YN)
1920
1921 default:
1922 return END_BUILTINS;
1923 }
1924
1925 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1926 return fcode;
1927 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1928 return fcodef;
1929 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1930 return fcodel;
1931 else
1932 return END_BUILTINS;
1933 }
1934
1935 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1936 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1937 otherwise use the explicit declaration. If we can't do the conversion,
1938 return null. */
1939
1940 static tree
1941 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
1942 {
1943 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
1944 if (fcode2 == END_BUILTINS)
1945 return NULL_TREE;
1946
1947 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1948 return NULL_TREE;
1949
1950 return builtin_decl_explicit (fcode2);
1951 }
1952
1953 /* Like mathfn_built_in_1, but always use the implicit array. */
1954
1955 tree
1956 mathfn_built_in (tree type, combined_fn fn)
1957 {
1958 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1959 }
1960
1961 /* Like mathfn_built_in_1, but take a built_in_function and
1962 always use the implicit array. */
1963
1964 tree
1965 mathfn_built_in (tree type, enum built_in_function fn)
1966 {
1967 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
1968 }
1969
1970 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
1971 return its code, otherwise return IFN_LAST. Note that this function
1972 only tests whether the function is defined in internals.def, not whether
1973 it is actually available on the target. */
1974
1975 internal_fn
1976 associated_internal_fn (tree fndecl)
1977 {
1978 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
1979 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
1980 switch (DECL_FUNCTION_CODE (fndecl))
1981 {
1982 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
1983 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1984 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
1985 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
1986 #include "internal-fn.def"
1987
1988 CASE_FLT_FN (BUILT_IN_POW10):
1989 return IFN_EXP10;
1990
1991 CASE_FLT_FN (BUILT_IN_DREM):
1992 return IFN_REMAINDER;
1993
1994 CASE_FLT_FN (BUILT_IN_SCALBN):
1995 CASE_FLT_FN (BUILT_IN_SCALBLN):
1996 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
1997 return IFN_LDEXP;
1998 return IFN_LAST;
1999
2000 default:
2001 return IFN_LAST;
2002 }
2003 }
2004
2005 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2006 on the current target by a call to an internal function, return the
2007 code of that internal function, otherwise return IFN_LAST. The caller
2008 is responsible for ensuring that any side-effects of the built-in
2009 call are dealt with correctly. E.g. if CALL sets errno, the caller
2010 must decide that the errno result isn't needed or make it available
2011 in some other way. */
2012
2013 internal_fn
2014 replacement_internal_fn (gcall *call)
2015 {
2016 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2017 {
2018 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2019 if (ifn != IFN_LAST)
2020 {
2021 tree_pair types = direct_internal_fn_types (ifn, call);
2022 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2023 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2024 return ifn;
2025 }
2026 }
2027 return IFN_LAST;
2028 }
2029
2030 /* Expand a call to the builtin trinary math functions (fma).
2031 Return NULL_RTX if a normal call should be emitted rather than expanding the
2032 function in-line. EXP is the expression that is a call to the builtin
2033 function; if convenient, the result should be placed in TARGET.
2034 SUBTARGET may be used as the target for computing one of EXP's
2035 operands. */
2036
2037 static rtx
2038 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2039 {
2040 optab builtin_optab;
2041 rtx op0, op1, op2, result;
2042 rtx_insn *insns;
2043 tree fndecl = get_callee_fndecl (exp);
2044 tree arg0, arg1, arg2;
2045 machine_mode mode;
2046
2047 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2048 return NULL_RTX;
2049
2050 arg0 = CALL_EXPR_ARG (exp, 0);
2051 arg1 = CALL_EXPR_ARG (exp, 1);
2052 arg2 = CALL_EXPR_ARG (exp, 2);
2053
2054 switch (DECL_FUNCTION_CODE (fndecl))
2055 {
2056 CASE_FLT_FN (BUILT_IN_FMA):
2057 builtin_optab = fma_optab; break;
2058 default:
2059 gcc_unreachable ();
2060 }
2061
2062 /* Make a suitable register to place result in. */
2063 mode = TYPE_MODE (TREE_TYPE (exp));
2064
2065 /* Before working hard, check whether the instruction is available. */
2066 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2067 return NULL_RTX;
2068
2069 result = gen_reg_rtx (mode);
2070
2071 /* Always stabilize the argument list. */
2072 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2073 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2074 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2075
2076 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2077 op1 = expand_normal (arg1);
2078 op2 = expand_normal (arg2);
2079
2080 start_sequence ();
2081
2082 /* Compute into RESULT.
2083 Set RESULT to wherever the result comes back. */
2084 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2085 result, 0);
2086
2087 /* If we were unable to expand via the builtin, stop the sequence
2088 (without outputting the insns) and call to the library function
2089 with the stabilized argument list. */
2090 if (result == 0)
2091 {
2092 end_sequence ();
2093 return expand_call (exp, target, target == const0_rtx);
2094 }
2095
2096 /* Output the entire sequence. */
2097 insns = get_insns ();
2098 end_sequence ();
2099 emit_insn (insns);
2100
2101 return result;
2102 }
2103
2104 /* Expand a call to the builtin sin and cos math functions.
2105 Return NULL_RTX if a normal call should be emitted rather than expanding the
2106 function in-line. EXP is the expression that is a call to the builtin
2107 function; if convenient, the result should be placed in TARGET.
2108 SUBTARGET may be used as the target for computing one of EXP's
2109 operands. */
2110
2111 static rtx
2112 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2113 {
2114 optab builtin_optab;
2115 rtx op0;
2116 rtx_insn *insns;
2117 tree fndecl = get_callee_fndecl (exp);
2118 machine_mode mode;
2119 tree arg;
2120
2121 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2122 return NULL_RTX;
2123
2124 arg = CALL_EXPR_ARG (exp, 0);
2125
2126 switch (DECL_FUNCTION_CODE (fndecl))
2127 {
2128 CASE_FLT_FN (BUILT_IN_SIN):
2129 CASE_FLT_FN (BUILT_IN_COS):
2130 builtin_optab = sincos_optab; break;
2131 default:
2132 gcc_unreachable ();
2133 }
2134
2135 /* Make a suitable register to place result in. */
2136 mode = TYPE_MODE (TREE_TYPE (exp));
2137
2138 /* Check if sincos insn is available, otherwise fallback
2139 to sin or cos insn. */
2140 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2141 switch (DECL_FUNCTION_CODE (fndecl))
2142 {
2143 CASE_FLT_FN (BUILT_IN_SIN):
2144 builtin_optab = sin_optab; break;
2145 CASE_FLT_FN (BUILT_IN_COS):
2146 builtin_optab = cos_optab; break;
2147 default:
2148 gcc_unreachable ();
2149 }
2150
2151 /* Before working hard, check whether the instruction is available. */
2152 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2153 {
2154 rtx result = gen_reg_rtx (mode);
2155
2156 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2157 need to expand the argument again. This way, we will not perform
2158 side-effects more the once. */
2159 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2160
2161 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2162
2163 start_sequence ();
2164
2165 /* Compute into RESULT.
2166 Set RESULT to wherever the result comes back. */
2167 if (builtin_optab == sincos_optab)
2168 {
2169 int ok;
2170
2171 switch (DECL_FUNCTION_CODE (fndecl))
2172 {
2173 CASE_FLT_FN (BUILT_IN_SIN):
2174 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2175 break;
2176 CASE_FLT_FN (BUILT_IN_COS):
2177 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2178 break;
2179 default:
2180 gcc_unreachable ();
2181 }
2182 gcc_assert (ok);
2183 }
2184 else
2185 result = expand_unop (mode, builtin_optab, op0, result, 0);
2186
2187 if (result != 0)
2188 {
2189 /* Output the entire sequence. */
2190 insns = get_insns ();
2191 end_sequence ();
2192 emit_insn (insns);
2193 return result;
2194 }
2195
2196 /* If we were unable to expand via the builtin, stop the sequence
2197 (without outputting the insns) and call to the library function
2198 with the stabilized argument list. */
2199 end_sequence ();
2200 }
2201
2202 return expand_call (exp, target, target == const0_rtx);
2203 }
2204
2205 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2206 return an RTL instruction code that implements the functionality.
2207 If that isn't possible or available return CODE_FOR_nothing. */
2208
2209 static enum insn_code
2210 interclass_mathfn_icode (tree arg, tree fndecl)
2211 {
2212 bool errno_set = false;
2213 optab builtin_optab = unknown_optab;
2214 machine_mode mode;
2215
2216 switch (DECL_FUNCTION_CODE (fndecl))
2217 {
2218 CASE_FLT_FN (BUILT_IN_ILOGB):
2219 errno_set = true; builtin_optab = ilogb_optab; break;
2220 CASE_FLT_FN (BUILT_IN_ISINF):
2221 builtin_optab = isinf_optab; break;
2222 case BUILT_IN_ISNORMAL:
2223 case BUILT_IN_ISFINITE:
2224 CASE_FLT_FN (BUILT_IN_FINITE):
2225 case BUILT_IN_FINITED32:
2226 case BUILT_IN_FINITED64:
2227 case BUILT_IN_FINITED128:
2228 case BUILT_IN_ISINFD32:
2229 case BUILT_IN_ISINFD64:
2230 case BUILT_IN_ISINFD128:
2231 /* These builtins have no optabs (yet). */
2232 break;
2233 default:
2234 gcc_unreachable ();
2235 }
2236
2237 /* There's no easy way to detect the case we need to set EDOM. */
2238 if (flag_errno_math && errno_set)
2239 return CODE_FOR_nothing;
2240
2241 /* Optab mode depends on the mode of the input argument. */
2242 mode = TYPE_MODE (TREE_TYPE (arg));
2243
2244 if (builtin_optab)
2245 return optab_handler (builtin_optab, mode);
2246 return CODE_FOR_nothing;
2247 }
2248
2249 /* Expand a call to one of the builtin math functions that operate on
2250 floating point argument and output an integer result (ilogb, isinf,
2251 isnan, etc).
2252 Return 0 if a normal call should be emitted rather than expanding the
2253 function in-line. EXP is the expression that is a call to the builtin
2254 function; if convenient, the result should be placed in TARGET. */
2255
2256 static rtx
2257 expand_builtin_interclass_mathfn (tree exp, rtx target)
2258 {
2259 enum insn_code icode = CODE_FOR_nothing;
2260 rtx op0;
2261 tree fndecl = get_callee_fndecl (exp);
2262 machine_mode mode;
2263 tree arg;
2264
2265 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2266 return NULL_RTX;
2267
2268 arg = CALL_EXPR_ARG (exp, 0);
2269 icode = interclass_mathfn_icode (arg, fndecl);
2270 mode = TYPE_MODE (TREE_TYPE (arg));
2271
2272 if (icode != CODE_FOR_nothing)
2273 {
2274 struct expand_operand ops[1];
2275 rtx_insn *last = get_last_insn ();
2276 tree orig_arg = arg;
2277
2278 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2279 need to expand the argument again. This way, we will not perform
2280 side-effects more the once. */
2281 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2282
2283 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2284
2285 if (mode != GET_MODE (op0))
2286 op0 = convert_to_mode (mode, op0, 0);
2287
2288 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2289 if (maybe_legitimize_operands (icode, 0, 1, ops)
2290 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2291 return ops[0].value;
2292
2293 delete_insns_since (last);
2294 CALL_EXPR_ARG (exp, 0) = orig_arg;
2295 }
2296
2297 return NULL_RTX;
2298 }
2299
2300 /* Expand a call to the builtin sincos math function.
2301 Return NULL_RTX if a normal call should be emitted rather than expanding the
2302 function in-line. EXP is the expression that is a call to the builtin
2303 function. */
2304
2305 static rtx
2306 expand_builtin_sincos (tree exp)
2307 {
2308 rtx op0, op1, op2, target1, target2;
2309 machine_mode mode;
2310 tree arg, sinp, cosp;
2311 int result;
2312 location_t loc = EXPR_LOCATION (exp);
2313 tree alias_type, alias_off;
2314
2315 if (!validate_arglist (exp, REAL_TYPE,
2316 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2317 return NULL_RTX;
2318
2319 arg = CALL_EXPR_ARG (exp, 0);
2320 sinp = CALL_EXPR_ARG (exp, 1);
2321 cosp = CALL_EXPR_ARG (exp, 2);
2322
2323 /* Make a suitable register to place result in. */
2324 mode = TYPE_MODE (TREE_TYPE (arg));
2325
2326 /* Check if sincos insn is available, otherwise emit the call. */
2327 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2328 return NULL_RTX;
2329
2330 target1 = gen_reg_rtx (mode);
2331 target2 = gen_reg_rtx (mode);
2332
2333 op0 = expand_normal (arg);
2334 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2335 alias_off = build_int_cst (alias_type, 0);
2336 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2337 sinp, alias_off));
2338 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2339 cosp, alias_off));
2340
2341 /* Compute into target1 and target2.
2342 Set TARGET to wherever the result comes back. */
2343 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2344 gcc_assert (result);
2345
2346 /* Move target1 and target2 to the memory locations indicated
2347 by op1 and op2. */
2348 emit_move_insn (op1, target1);
2349 emit_move_insn (op2, target2);
2350
2351 return const0_rtx;
2352 }
2353
2354 /* Expand a call to the internal cexpi builtin to the sincos math function.
2355 EXP is the expression that is a call to the builtin function; if convenient,
2356 the result should be placed in TARGET. */
2357
2358 static rtx
2359 expand_builtin_cexpi (tree exp, rtx target)
2360 {
2361 tree fndecl = get_callee_fndecl (exp);
2362 tree arg, type;
2363 machine_mode mode;
2364 rtx op0, op1, op2;
2365 location_t loc = EXPR_LOCATION (exp);
2366
2367 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2368 return NULL_RTX;
2369
2370 arg = CALL_EXPR_ARG (exp, 0);
2371 type = TREE_TYPE (arg);
2372 mode = TYPE_MODE (TREE_TYPE (arg));
2373
2374 /* Try expanding via a sincos optab, fall back to emitting a libcall
2375 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2376 is only generated from sincos, cexp or if we have either of them. */
2377 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2378 {
2379 op1 = gen_reg_rtx (mode);
2380 op2 = gen_reg_rtx (mode);
2381
2382 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2383
2384 /* Compute into op1 and op2. */
2385 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2386 }
2387 else if (targetm.libc_has_function (function_sincos))
2388 {
2389 tree call, fn = NULL_TREE;
2390 tree top1, top2;
2391 rtx op1a, op2a;
2392
2393 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2394 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2395 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2396 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2397 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2398 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2399 else
2400 gcc_unreachable ();
2401
2402 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2403 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2404 op1a = copy_addr_to_reg (XEXP (op1, 0));
2405 op2a = copy_addr_to_reg (XEXP (op2, 0));
2406 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2407 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2408
2409 /* Make sure not to fold the sincos call again. */
2410 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2411 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2412 call, 3, arg, top1, top2));
2413 }
2414 else
2415 {
2416 tree call, fn = NULL_TREE, narg;
2417 tree ctype = build_complex_type (type);
2418
2419 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2420 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2421 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2422 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2423 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2424 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2425 else
2426 gcc_unreachable ();
2427
2428 /* If we don't have a decl for cexp create one. This is the
2429 friendliest fallback if the user calls __builtin_cexpi
2430 without full target C99 function support. */
2431 if (fn == NULL_TREE)
2432 {
2433 tree fntype;
2434 const char *name = NULL;
2435
2436 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2437 name = "cexpf";
2438 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2439 name = "cexp";
2440 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2441 name = "cexpl";
2442
2443 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2444 fn = build_fn_decl (name, fntype);
2445 }
2446
2447 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2448 build_real (type, dconst0), arg);
2449
2450 /* Make sure not to fold the cexp call again. */
2451 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2452 return expand_expr (build_call_nary (ctype, call, 1, narg),
2453 target, VOIDmode, EXPAND_NORMAL);
2454 }
2455
2456 /* Now build the proper return type. */
2457 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2458 make_tree (TREE_TYPE (arg), op2),
2459 make_tree (TREE_TYPE (arg), op1)),
2460 target, VOIDmode, EXPAND_NORMAL);
2461 }
2462
2463 /* Conveniently construct a function call expression. FNDECL names the
2464 function to be called, N is the number of arguments, and the "..."
2465 parameters are the argument expressions. Unlike build_call_exr
2466 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2467
2468 static tree
2469 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2470 {
2471 va_list ap;
2472 tree fntype = TREE_TYPE (fndecl);
2473 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2474
2475 va_start (ap, n);
2476 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2477 va_end (ap);
2478 SET_EXPR_LOCATION (fn, loc);
2479 return fn;
2480 }
2481
2482 /* Expand a call to one of the builtin rounding functions gcc defines
2483 as an extension (lfloor and lceil). As these are gcc extensions we
2484 do not need to worry about setting errno to EDOM.
2485 If expanding via optab fails, lower expression to (int)(floor(x)).
2486 EXP is the expression that is a call to the builtin function;
2487 if convenient, the result should be placed in TARGET. */
2488
2489 static rtx
2490 expand_builtin_int_roundingfn (tree exp, rtx target)
2491 {
2492 convert_optab builtin_optab;
2493 rtx op0, tmp;
2494 rtx_insn *insns;
2495 tree fndecl = get_callee_fndecl (exp);
2496 enum built_in_function fallback_fn;
2497 tree fallback_fndecl;
2498 machine_mode mode;
2499 tree arg;
2500
2501 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2502 gcc_unreachable ();
2503
2504 arg = CALL_EXPR_ARG (exp, 0);
2505
2506 switch (DECL_FUNCTION_CODE (fndecl))
2507 {
2508 CASE_FLT_FN (BUILT_IN_ICEIL):
2509 CASE_FLT_FN (BUILT_IN_LCEIL):
2510 CASE_FLT_FN (BUILT_IN_LLCEIL):
2511 builtin_optab = lceil_optab;
2512 fallback_fn = BUILT_IN_CEIL;
2513 break;
2514
2515 CASE_FLT_FN (BUILT_IN_IFLOOR):
2516 CASE_FLT_FN (BUILT_IN_LFLOOR):
2517 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2518 builtin_optab = lfloor_optab;
2519 fallback_fn = BUILT_IN_FLOOR;
2520 break;
2521
2522 default:
2523 gcc_unreachable ();
2524 }
2525
2526 /* Make a suitable register to place result in. */
2527 mode = TYPE_MODE (TREE_TYPE (exp));
2528
2529 target = gen_reg_rtx (mode);
2530
2531 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2532 need to expand the argument again. This way, we will not perform
2533 side-effects more the once. */
2534 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2535
2536 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2537
2538 start_sequence ();
2539
2540 /* Compute into TARGET. */
2541 if (expand_sfix_optab (target, op0, builtin_optab))
2542 {
2543 /* Output the entire sequence. */
2544 insns = get_insns ();
2545 end_sequence ();
2546 emit_insn (insns);
2547 return target;
2548 }
2549
2550 /* If we were unable to expand via the builtin, stop the sequence
2551 (without outputting the insns). */
2552 end_sequence ();
2553
2554 /* Fall back to floating point rounding optab. */
2555 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2556
2557 /* For non-C99 targets we may end up without a fallback fndecl here
2558 if the user called __builtin_lfloor directly. In this case emit
2559 a call to the floor/ceil variants nevertheless. This should result
2560 in the best user experience for not full C99 targets. */
2561 if (fallback_fndecl == NULL_TREE)
2562 {
2563 tree fntype;
2564 const char *name = NULL;
2565
2566 switch (DECL_FUNCTION_CODE (fndecl))
2567 {
2568 case BUILT_IN_ICEIL:
2569 case BUILT_IN_LCEIL:
2570 case BUILT_IN_LLCEIL:
2571 name = "ceil";
2572 break;
2573 case BUILT_IN_ICEILF:
2574 case BUILT_IN_LCEILF:
2575 case BUILT_IN_LLCEILF:
2576 name = "ceilf";
2577 break;
2578 case BUILT_IN_ICEILL:
2579 case BUILT_IN_LCEILL:
2580 case BUILT_IN_LLCEILL:
2581 name = "ceill";
2582 break;
2583 case BUILT_IN_IFLOOR:
2584 case BUILT_IN_LFLOOR:
2585 case BUILT_IN_LLFLOOR:
2586 name = "floor";
2587 break;
2588 case BUILT_IN_IFLOORF:
2589 case BUILT_IN_LFLOORF:
2590 case BUILT_IN_LLFLOORF:
2591 name = "floorf";
2592 break;
2593 case BUILT_IN_IFLOORL:
2594 case BUILT_IN_LFLOORL:
2595 case BUILT_IN_LLFLOORL:
2596 name = "floorl";
2597 break;
2598 default:
2599 gcc_unreachable ();
2600 }
2601
2602 fntype = build_function_type_list (TREE_TYPE (arg),
2603 TREE_TYPE (arg), NULL_TREE);
2604 fallback_fndecl = build_fn_decl (name, fntype);
2605 }
2606
2607 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2608
2609 tmp = expand_normal (exp);
2610 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2611
2612 /* Truncate the result of floating point optab to integer
2613 via expand_fix (). */
2614 target = gen_reg_rtx (mode);
2615 expand_fix (target, tmp, 0);
2616
2617 return target;
2618 }
2619
2620 /* Expand a call to one of the builtin math functions doing integer
2621 conversion (lrint).
2622 Return 0 if a normal call should be emitted rather than expanding the
2623 function in-line. EXP is the expression that is a call to the builtin
2624 function; if convenient, the result should be placed in TARGET. */
2625
2626 static rtx
2627 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2628 {
2629 convert_optab builtin_optab;
2630 rtx op0;
2631 rtx_insn *insns;
2632 tree fndecl = get_callee_fndecl (exp);
2633 tree arg;
2634 machine_mode mode;
2635 enum built_in_function fallback_fn = BUILT_IN_NONE;
2636
2637 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2638 gcc_unreachable ();
2639
2640 arg = CALL_EXPR_ARG (exp, 0);
2641
2642 switch (DECL_FUNCTION_CODE (fndecl))
2643 {
2644 CASE_FLT_FN (BUILT_IN_IRINT):
2645 fallback_fn = BUILT_IN_LRINT;
2646 gcc_fallthrough ();
2647 CASE_FLT_FN (BUILT_IN_LRINT):
2648 CASE_FLT_FN (BUILT_IN_LLRINT):
2649 builtin_optab = lrint_optab;
2650 break;
2651
2652 CASE_FLT_FN (BUILT_IN_IROUND):
2653 fallback_fn = BUILT_IN_LROUND;
2654 gcc_fallthrough ();
2655 CASE_FLT_FN (BUILT_IN_LROUND):
2656 CASE_FLT_FN (BUILT_IN_LLROUND):
2657 builtin_optab = lround_optab;
2658 break;
2659
2660 default:
2661 gcc_unreachable ();
2662 }
2663
2664 /* There's no easy way to detect the case we need to set EDOM. */
2665 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2666 return NULL_RTX;
2667
2668 /* Make a suitable register to place result in. */
2669 mode = TYPE_MODE (TREE_TYPE (exp));
2670
2671 /* There's no easy way to detect the case we need to set EDOM. */
2672 if (!flag_errno_math)
2673 {
2674 rtx result = gen_reg_rtx (mode);
2675
2676 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2677 need to expand the argument again. This way, we will not perform
2678 side-effects more the once. */
2679 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2680
2681 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2682
2683 start_sequence ();
2684
2685 if (expand_sfix_optab (result, op0, builtin_optab))
2686 {
2687 /* Output the entire sequence. */
2688 insns = get_insns ();
2689 end_sequence ();
2690 emit_insn (insns);
2691 return result;
2692 }
2693
2694 /* If we were unable to expand via the builtin, stop the sequence
2695 (without outputting the insns) and call to the library function
2696 with the stabilized argument list. */
2697 end_sequence ();
2698 }
2699
2700 if (fallback_fn != BUILT_IN_NONE)
2701 {
2702 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2703 targets, (int) round (x) should never be transformed into
2704 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2705 a call to lround in the hope that the target provides at least some
2706 C99 functions. This should result in the best user experience for
2707 not full C99 targets. */
2708 tree fallback_fndecl = mathfn_built_in_1
2709 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2710
2711 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2712 fallback_fndecl, 1, arg);
2713
2714 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2715 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2716 return convert_to_mode (mode, target, 0);
2717 }
2718
2719 return expand_call (exp, target, target == const0_rtx);
2720 }
2721
2722 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2723 a normal call should be emitted rather than expanding the function
2724 in-line. EXP is the expression that is a call to the builtin
2725 function; if convenient, the result should be placed in TARGET. */
2726
2727 static rtx
2728 expand_builtin_powi (tree exp, rtx target)
2729 {
2730 tree arg0, arg1;
2731 rtx op0, op1;
2732 machine_mode mode;
2733 machine_mode mode2;
2734
2735 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2736 return NULL_RTX;
2737
2738 arg0 = CALL_EXPR_ARG (exp, 0);
2739 arg1 = CALL_EXPR_ARG (exp, 1);
2740 mode = TYPE_MODE (TREE_TYPE (exp));
2741
2742 /* Emit a libcall to libgcc. */
2743
2744 /* Mode of the 2nd argument must match that of an int. */
2745 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2746
2747 if (target == NULL_RTX)
2748 target = gen_reg_rtx (mode);
2749
2750 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2751 if (GET_MODE (op0) != mode)
2752 op0 = convert_to_mode (mode, op0, 0);
2753 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2754 if (GET_MODE (op1) != mode2)
2755 op1 = convert_to_mode (mode2, op1, 0);
2756
2757 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2758 target, LCT_CONST, mode, 2,
2759 op0, mode, op1, mode2);
2760
2761 return target;
2762 }
2763
2764 /* Expand expression EXP which is a call to the strlen builtin. Return
2765 NULL_RTX if we failed the caller should emit a normal call, otherwise
2766 try to get the result in TARGET, if convenient. */
2767
2768 static rtx
2769 expand_builtin_strlen (tree exp, rtx target,
2770 machine_mode target_mode)
2771 {
2772 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2773 return NULL_RTX;
2774 else
2775 {
2776 struct expand_operand ops[4];
2777 rtx pat;
2778 tree len;
2779 tree src = CALL_EXPR_ARG (exp, 0);
2780 rtx src_reg;
2781 rtx_insn *before_strlen;
2782 machine_mode insn_mode = target_mode;
2783 enum insn_code icode = CODE_FOR_nothing;
2784 unsigned int align;
2785
2786 /* If the length can be computed at compile-time, return it. */
2787 len = c_strlen (src, 0);
2788 if (len)
2789 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2790
2791 /* If the length can be computed at compile-time and is constant
2792 integer, but there are side-effects in src, evaluate
2793 src for side-effects, then return len.
2794 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2795 can be optimized into: i++; x = 3; */
2796 len = c_strlen (src, 1);
2797 if (len && TREE_CODE (len) == INTEGER_CST)
2798 {
2799 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2800 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2801 }
2802
2803 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2804
2805 /* If SRC is not a pointer type, don't do this operation inline. */
2806 if (align == 0)
2807 return NULL_RTX;
2808
2809 /* Bail out if we can't compute strlen in the right mode. */
2810 while (insn_mode != VOIDmode)
2811 {
2812 icode = optab_handler (strlen_optab, insn_mode);
2813 if (icode != CODE_FOR_nothing)
2814 break;
2815
2816 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2817 }
2818 if (insn_mode == VOIDmode)
2819 return NULL_RTX;
2820
2821 /* Make a place to hold the source address. We will not expand
2822 the actual source until we are sure that the expansion will
2823 not fail -- there are trees that cannot be expanded twice. */
2824 src_reg = gen_reg_rtx (Pmode);
2825
2826 /* Mark the beginning of the strlen sequence so we can emit the
2827 source operand later. */
2828 before_strlen = get_last_insn ();
2829
2830 create_output_operand (&ops[0], target, insn_mode);
2831 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2832 create_integer_operand (&ops[2], 0);
2833 create_integer_operand (&ops[3], align);
2834 if (!maybe_expand_insn (icode, 4, ops))
2835 return NULL_RTX;
2836
2837 /* Now that we are assured of success, expand the source. */
2838 start_sequence ();
2839 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2840 if (pat != src_reg)
2841 {
2842 #ifdef POINTERS_EXTEND_UNSIGNED
2843 if (GET_MODE (pat) != Pmode)
2844 pat = convert_to_mode (Pmode, pat,
2845 POINTERS_EXTEND_UNSIGNED);
2846 #endif
2847 emit_move_insn (src_reg, pat);
2848 }
2849 pat = get_insns ();
2850 end_sequence ();
2851
2852 if (before_strlen)
2853 emit_insn_after (pat, before_strlen);
2854 else
2855 emit_insn_before (pat, get_insns ());
2856
2857 /* Return the value in the proper mode for this function. */
2858 if (GET_MODE (ops[0].value) == target_mode)
2859 target = ops[0].value;
2860 else if (target != 0)
2861 convert_move (target, ops[0].value, 0);
2862 else
2863 target = convert_to_mode (target_mode, ops[0].value, 0);
2864
2865 return target;
2866 }
2867 }
2868
2869 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2870 bytes from constant string DATA + OFFSET and return it as target
2871 constant. */
2872
2873 static rtx
2874 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2875 machine_mode mode)
2876 {
2877 const char *str = (const char *) data;
2878
2879 gcc_assert (offset >= 0
2880 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2881 <= strlen (str) + 1));
2882
2883 return c_readstr (str + offset, mode);
2884 }
2885
2886 /* LEN specify length of the block of memcpy/memset operation.
2887 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
2888 In some cases we can make very likely guess on max size, then we
2889 set it into PROBABLE_MAX_SIZE. */
2890
2891 static void
2892 determine_block_size (tree len, rtx len_rtx,
2893 unsigned HOST_WIDE_INT *min_size,
2894 unsigned HOST_WIDE_INT *max_size,
2895 unsigned HOST_WIDE_INT *probable_max_size)
2896 {
2897 if (CONST_INT_P (len_rtx))
2898 {
2899 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
2900 return;
2901 }
2902 else
2903 {
2904 wide_int min, max;
2905 enum value_range_type range_type = VR_UNDEFINED;
2906
2907 /* Determine bounds from the type. */
2908 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
2909 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
2910 else
2911 *min_size = 0;
2912 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
2913 *probable_max_size = *max_size
2914 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
2915 else
2916 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
2917
2918 if (TREE_CODE (len) == SSA_NAME)
2919 range_type = get_range_info (len, &min, &max);
2920 if (range_type == VR_RANGE)
2921 {
2922 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
2923 *min_size = min.to_uhwi ();
2924 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
2925 *probable_max_size = *max_size = max.to_uhwi ();
2926 }
2927 else if (range_type == VR_ANTI_RANGE)
2928 {
2929 /* Anti range 0...N lets us to determine minimal size to N+1. */
2930 if (min == 0)
2931 {
2932 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
2933 *min_size = max.to_uhwi () + 1;
2934 }
2935 /* Code like
2936
2937 int n;
2938 if (n < 100)
2939 memcpy (a, b, n)
2940
2941 Produce anti range allowing negative values of N. We still
2942 can use the information and make a guess that N is not negative.
2943 */
2944 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
2945 *probable_max_size = min.to_uhwi () - 1;
2946 }
2947 }
2948 gcc_checking_assert (*max_size <=
2949 (unsigned HOST_WIDE_INT)
2950 GET_MODE_MASK (GET_MODE (len_rtx)));
2951 }
2952
2953 /* Helper function to do the actual work for expand_builtin_memcpy. */
2954
2955 static rtx
2956 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
2957 {
2958 const char *src_str;
2959 unsigned int src_align = get_pointer_alignment (src);
2960 unsigned int dest_align = get_pointer_alignment (dest);
2961 rtx dest_mem, src_mem, dest_addr, len_rtx;
2962 HOST_WIDE_INT expected_size = -1;
2963 unsigned int expected_align = 0;
2964 unsigned HOST_WIDE_INT min_size;
2965 unsigned HOST_WIDE_INT max_size;
2966 unsigned HOST_WIDE_INT probable_max_size;
2967
2968 /* If DEST is not a pointer type, call the normal function. */
2969 if (dest_align == 0)
2970 return NULL_RTX;
2971
2972 /* If either SRC is not a pointer type, don't do this
2973 operation in-line. */
2974 if (src_align == 0)
2975 return NULL_RTX;
2976
2977 if (currently_expanding_gimple_stmt)
2978 stringop_block_profile (currently_expanding_gimple_stmt,
2979 &expected_align, &expected_size);
2980
2981 if (expected_align < dest_align)
2982 expected_align = dest_align;
2983 dest_mem = get_memory_rtx (dest, len);
2984 set_mem_align (dest_mem, dest_align);
2985 len_rtx = expand_normal (len);
2986 determine_block_size (len, len_rtx, &min_size, &max_size,
2987 &probable_max_size);
2988 src_str = c_getstr (src);
2989
2990 /* If SRC is a string constant and block move would be done
2991 by pieces, we can avoid loading the string from memory
2992 and only stored the computed constants. */
2993 if (src_str
2994 && CONST_INT_P (len_rtx)
2995 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2996 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2997 CONST_CAST (char *, src_str),
2998 dest_align, false))
2999 {
3000 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3001 builtin_memcpy_read_str,
3002 CONST_CAST (char *, src_str),
3003 dest_align, false, 0);
3004 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3005 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3006 return dest_mem;
3007 }
3008
3009 src_mem = get_memory_rtx (src, len);
3010 set_mem_align (src_mem, src_align);
3011
3012 /* Copy word part most expediently. */
3013 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3014 CALL_EXPR_TAILCALL (exp)
3015 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3016 expected_align, expected_size,
3017 min_size, max_size, probable_max_size);
3018
3019 if (dest_addr == 0)
3020 {
3021 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3022 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3023 }
3024
3025 return dest_addr;
3026 }
3027
3028 /* Fill the 2-element RANGE array with the minimum and maximum values
3029 EXP is known to have and return true, otherwise null and return
3030 false. */
3031
3032 static bool
3033 get_size_range (tree exp, tree range[2])
3034 {
3035 if (tree_fits_uhwi_p (exp))
3036 {
3037 range[0] = range[1] = exp;
3038 return true;
3039 }
3040
3041 if (TREE_CODE (exp) == SSA_NAME)
3042 {
3043 wide_int min, max;
3044 enum value_range_type range_type = get_range_info (exp, &min, &max);
3045
3046 if (range_type == VR_RANGE)
3047 {
3048 /* Interpret the bound in the variable's type. */
3049 range[0] = wide_int_to_tree (TREE_TYPE (exp), min);
3050 range[1] = wide_int_to_tree (TREE_TYPE (exp), max);
3051 return true;
3052 }
3053 else if (range_type == VR_ANTI_RANGE)
3054 {
3055 /* FIXME: Handle anti-ranges. */
3056 }
3057 }
3058
3059 range[0] = NULL_TREE;
3060 range[1] = NULL_TREE;
3061 return false;
3062 }
3063
3064 /* Try to verify that the sizes and lengths of the arguments to a string
3065 manipulation function given by EXP are within valid bounds and that
3066 the operation does not lead to buffer overflow. Arguments other than
3067 EXP may be null. When non-null, the arguments have the following
3068 meaning:
3069 SIZE is the user-supplied size argument to the function (such as in
3070 memcpy(d, s, SIZE) or strncpy(d, s, SIZE). It specifies the exact
3071 number of bytes to write.
3072 MAXLEN is the user-supplied bound on the length of the source sequence
3073 (such as in strncat(d, s, N). It specifies the upper limit on the number
3074 of bytes to write.
3075 STR is the source string (such as in strcpy(d, s)) when the epxression
3076 EXP is a string function call (as opposed to a memory call like memcpy).
3077 As an exception, STR can also be an integer denoting the precomputed
3078 length of the source string.
3079 OBJSIZE is the size of the destination object specified by the last
3080 argument to the _chk builtins, typically resulting from the expansion
3081 of __builtin_object_size (such as in __builtin___strcpy_chk(d, s,
3082 OBJSIZE).
3083
3084 When SIZE is null LEN is checked to verify that it doesn't exceed
3085 SIZE_MAX.
3086
3087 If the call is successfully verified as safe from buffer overflow
3088 the function returns true, otherwise false.. */
3089
3090 static bool
3091 check_sizes (int opt, tree exp, tree size, tree maxlen, tree str, tree objsize)
3092 {
3093 /* The size of the largest object is half the address space, or
3094 SSIZE_MAX. (This is way too permissive.) */
3095 tree maxobjsize = TYPE_MAX_VALUE (ssizetype);
3096
3097 tree slen = NULL_TREE;
3098
3099 /* Set to true when the exact number of bytes written by a string
3100 function like strcpy is not known and the only thing that is
3101 known is that it must be at least one (for the terminating nul). */
3102 bool at_least_one = false;
3103 if (str)
3104 {
3105 /* STR is normally a pointer to string but as a special case
3106 it can be an integer denoting the length of a string. */
3107 if (POINTER_TYPE_P (TREE_TYPE (str)))
3108 {
3109 /* Try to determine the range of lengths the source string
3110 refers to. If it can be determined add one to it for
3111 the terminating nul. Otherwise, set it to one for
3112 the same reason. */
3113 tree lenrange[2];
3114 get_range_strlen (str, lenrange);
3115 if (lenrange[0])
3116 slen = fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3117 size_one_node);
3118 else
3119 {
3120 at_least_one = true;
3121 slen = size_one_node;
3122 }
3123 }
3124 else
3125 slen = str;
3126 }
3127
3128 if (!size && !maxlen)
3129 {
3130 /* When the only available piece of data is the object size
3131 there is nothing to do. */
3132 if (!slen)
3133 return true;
3134
3135 /* Otherwise, when the length of the source sequence is known
3136 (as with with strlen), set SIZE to it. */
3137 size = slen;
3138 }
3139
3140 if (!objsize)
3141 objsize = maxobjsize;
3142
3143 /* The SIZE is exact if it's non-null, constant, and in range of
3144 unsigned HOST_WIDE_INT. */
3145 bool exactsize = size && tree_fits_uhwi_p (size);
3146
3147 tree range[2] = { NULL_TREE, NULL_TREE };
3148 if (size)
3149 get_size_range (size, range);
3150
3151 /* First check the number of bytes to be written against the maximum
3152 object size. */
3153 if (range[0] && tree_int_cst_lt (maxobjsize, range[0]))
3154 {
3155 location_t loc = tree_nonartificial_location (exp);
3156
3157 if (range[0] == range[1])
3158 warning_at (loc, opt,
3159 "%K%qD: specified size %wu "
3160 "exceeds maximum object size %wu",
3161 exp, get_callee_fndecl (exp),
3162 tree_to_uhwi (range[0]),
3163 tree_to_uhwi (maxobjsize));
3164 else
3165 warning_at (loc, opt,
3166 "%K%qD: specified size between %wu and %wu "
3167 "exceeds maximum object size %wu",
3168 exp, get_callee_fndecl (exp),
3169 tree_to_uhwi (range[0]),
3170 tree_to_uhwi (range[1]),
3171 tree_to_uhwi (maxobjsize));
3172 return false;
3173 }
3174
3175 /* Next check the number of bytes to be written against the destination
3176 object size. */
3177 if (range[0] || !exactsize || integer_all_onesp (size))
3178 {
3179 if (range[0]
3180 && ((tree_fits_uhwi_p (objsize)
3181 && tree_int_cst_lt (objsize, range[0]))
3182 || (tree_fits_uhwi_p (size)
3183 && tree_int_cst_lt (size, range[0]))))
3184 {
3185 unsigned HOST_WIDE_INT uwir0 = tree_to_uhwi (range[0]);
3186
3187 location_t loc = tree_nonartificial_location (exp);
3188
3189 if (at_least_one)
3190 warning_at (loc, opt,
3191 "%K%qD: writing at least %wu byte into a region "
3192 "of size %wu overflows the destination",
3193 exp, get_callee_fndecl (exp), uwir0,
3194 tree_to_uhwi (objsize));
3195 else if (range[0] == range[1])
3196 warning_at (loc, opt,
3197 (uwir0 == 1
3198 ? G_("%K%qD: writing %wu byte into a region "
3199 "of size %wu overflows the destination")
3200 : G_("%K%qD writing %wu bytes into a region "
3201 "of size %wu overflows the destination")),
3202 exp, get_callee_fndecl (exp), uwir0,
3203 tree_to_uhwi (objsize));
3204 else
3205 warning_at (loc, opt,
3206 "%K%qD: writing between %wu and %wu bytes "
3207 "into a region of size %wu overflows "
3208 "the destination",
3209 exp, get_callee_fndecl (exp), uwir0,
3210 tree_to_uhwi (range[1]), tree_to_uhwi (objsize));
3211
3212 /* Return error when an overflow has been detected. */
3213 return false;
3214 }
3215 }
3216
3217 /* Check the maximum length of the source sequence against the size
3218 of the destination object if known, or against the maximum size
3219 of an object. */
3220 if (maxlen)
3221 {
3222 get_size_range (maxlen, range);
3223
3224 if (range[0] && objsize && tree_fits_uhwi_p (objsize))
3225 {
3226 location_t loc = tree_nonartificial_location (exp);
3227
3228 if (tree_int_cst_lt (maxobjsize, range[0]))
3229 {
3230 /* Warn about crazy big sizes first since that's more
3231 likely to be meaningful than saying that the bound
3232 is greater than the object size if both are big. */
3233 if (range[0] == range[1])
3234 warning_at (loc, opt,
3235 "%K%qD: specified bound %wu "
3236 "exceeds maximum object size %wu",
3237 exp, get_callee_fndecl (exp),
3238 tree_to_uhwi (range[0]),
3239 tree_to_uhwi (maxobjsize));
3240 else
3241 warning_at (loc, opt,
3242 "%K%qD: specified bound between %wu and %wu "
3243 " exceeds maximum object size %wu",
3244 exp, get_callee_fndecl (exp),
3245 tree_to_uhwi (range[0]),
3246 tree_to_uhwi (range[1]),
3247 tree_to_uhwi (maxobjsize));
3248
3249 return false;
3250 }
3251
3252 if (objsize != maxobjsize && tree_int_cst_lt (objsize, range[0]))
3253 {
3254 if (range[0] == range[1])
3255 warning_at (loc, opt,
3256 "%K%qD: specified bound %wu "
3257 "exceeds the size %wu of the destination",
3258 exp, get_callee_fndecl (exp),
3259 tree_to_uhwi (range[0]),
3260 tree_to_uhwi (objsize));
3261 else
3262 warning_at (loc, opt,
3263 "%K%qD: specified bound between %wu and %wu "
3264 " exceeds the size %wu of the destination",
3265 exp, get_callee_fndecl (exp),
3266 tree_to_uhwi (range[0]),
3267 tree_to_uhwi (range[1]),
3268 tree_to_uhwi (objsize));
3269 return false;
3270 }
3271 }
3272 }
3273
3274 return true;
3275 }
3276
3277 /* Helper to compute the size of the object referenced by the DEST
3278 expression which must of of pointer type, using Object Size type
3279 OSTYPE (only the least significant 2 bits are used). Return
3280 the size of the object if successful or NULL when the size cannot
3281 be determined. */
3282
3283 static inline tree
3284 compute_dest_size (tree dest, int ostype)
3285 {
3286 unsigned HOST_WIDE_INT size;
3287 if (compute_builtin_object_size (dest, ostype & 3, &size))
3288 return build_int_cst (sizetype, size);
3289
3290 return NULL_TREE;
3291 }
3292
3293 /* Helper to determine and check the sizes of the source and the destination
3294 of calls to __builtin_{bzero,memcpy,memset} calls. Use Object Size type-0
3295 regardless of the OPT_Wstringop_overflow_ setting. Returns true on success
3296 (no overflow or invalid sizes), false otherwise. */
3297
3298 static bool
3299 check_memop_sizes (tree exp, tree dest, tree size)
3300 {
3301 if (!warn_stringop_overflow)
3302 return true;
3303
3304 /* For functions like memset and memcpy that operate on raw memory
3305 try to determine the size of the largest destination object using
3306 type-0 Object Size regardless of the object size type specified
3307 by the option. */
3308 tree objsize = compute_dest_size (dest, 0);
3309
3310 return check_sizes (OPT_Wstringop_overflow_, exp,
3311 size, /*maxlen=*/NULL_TREE, /*str=*/NULL_TREE, objsize);
3312 }
3313
3314 /* Expand a call EXP to the memcpy builtin.
3315 Return NULL_RTX if we failed, the caller should emit a normal call,
3316 otherwise try to get the result in TARGET, if convenient (and in
3317 mode MODE if that's convenient). */
3318
3319 static rtx
3320 expand_builtin_memcpy (tree exp, rtx target)
3321 {
3322 if (!validate_arglist (exp,
3323 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3324 return NULL_RTX;
3325
3326 tree dest = CALL_EXPR_ARG (exp, 0);
3327 tree src = CALL_EXPR_ARG (exp, 1);
3328 tree len = CALL_EXPR_ARG (exp, 2);
3329
3330 check_memop_sizes (exp, dest, len);
3331
3332 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3333 }
3334
3335 /* Expand an instrumented call EXP to the memcpy builtin.
3336 Return NULL_RTX if we failed, the caller should emit a normal call,
3337 otherwise try to get the result in TARGET, if convenient (and in
3338 mode MODE if that's convenient). */
3339
3340 static rtx
3341 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3342 {
3343 if (!validate_arglist (exp,
3344 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3345 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3346 INTEGER_TYPE, VOID_TYPE))
3347 return NULL_RTX;
3348 else
3349 {
3350 tree dest = CALL_EXPR_ARG (exp, 0);
3351 tree src = CALL_EXPR_ARG (exp, 2);
3352 tree len = CALL_EXPR_ARG (exp, 4);
3353 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3354
3355 /* Return src bounds with the result. */
3356 if (res)
3357 {
3358 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3359 expand_normal (CALL_EXPR_ARG (exp, 1)));
3360 res = chkp_join_splitted_slot (res, bnd);
3361 }
3362 return res;
3363 }
3364 }
3365
3366 /* Expand a call EXP to the mempcpy builtin.
3367 Return NULL_RTX if we failed; the caller should emit a normal call,
3368 otherwise try to get the result in TARGET, if convenient (and in
3369 mode MODE if that's convenient). If ENDP is 0 return the
3370 destination pointer, if ENDP is 1 return the end pointer ala
3371 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3372 stpcpy. */
3373
3374 static rtx
3375 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3376 {
3377 if (!validate_arglist (exp,
3378 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3379 return NULL_RTX;
3380
3381 tree dest = CALL_EXPR_ARG (exp, 0);
3382 tree src = CALL_EXPR_ARG (exp, 1);
3383 tree len = CALL_EXPR_ARG (exp, 2);
3384
3385 /* Avoid expanding mempcpy into memcpy when the call is determined
3386 to overflow the buffer. This also prevents the same overflow
3387 from being diagnosed again when expanding memcpy. */
3388 if (!check_memop_sizes (exp, dest, len))
3389 return NULL_RTX;
3390
3391 return expand_builtin_mempcpy_args (dest, src, len,
3392 target, mode, /*endp=*/ 1,
3393 exp);
3394 }
3395
3396 /* Expand an instrumented call EXP to the mempcpy builtin.
3397 Return NULL_RTX if we failed, the caller should emit a normal call,
3398 otherwise try to get the result in TARGET, if convenient (and in
3399 mode MODE if that's convenient). */
3400
3401 static rtx
3402 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3403 {
3404 if (!validate_arglist (exp,
3405 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3406 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3407 INTEGER_TYPE, VOID_TYPE))
3408 return NULL_RTX;
3409 else
3410 {
3411 tree dest = CALL_EXPR_ARG (exp, 0);
3412 tree src = CALL_EXPR_ARG (exp, 2);
3413 tree len = CALL_EXPR_ARG (exp, 4);
3414 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3415 mode, 1, exp);
3416
3417 /* Return src bounds with the result. */
3418 if (res)
3419 {
3420 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3421 expand_normal (CALL_EXPR_ARG (exp, 1)));
3422 res = chkp_join_splitted_slot (res, bnd);
3423 }
3424 return res;
3425 }
3426 }
3427
3428 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3429 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3430 so that this can also be called without constructing an actual CALL_EXPR.
3431 The other arguments and return value are the same as for
3432 expand_builtin_mempcpy. */
3433
3434 static rtx
3435 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3436 rtx target, machine_mode mode, int endp,
3437 tree orig_exp)
3438 {
3439 tree fndecl = get_callee_fndecl (orig_exp);
3440
3441 /* If return value is ignored, transform mempcpy into memcpy. */
3442 if (target == const0_rtx
3443 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3444 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3445 {
3446 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3447 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3448 dest, src, len);
3449 return expand_expr (result, target, mode, EXPAND_NORMAL);
3450 }
3451 else if (target == const0_rtx
3452 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3453 {
3454 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3455 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3456 dest, src, len);
3457 return expand_expr (result, target, mode, EXPAND_NORMAL);
3458 }
3459 else
3460 {
3461 const char *src_str;
3462 unsigned int src_align = get_pointer_alignment (src);
3463 unsigned int dest_align = get_pointer_alignment (dest);
3464 rtx dest_mem, src_mem, len_rtx;
3465
3466 /* If either SRC or DEST is not a pointer type, don't do this
3467 operation in-line. */
3468 if (dest_align == 0 || src_align == 0)
3469 return NULL_RTX;
3470
3471 /* If LEN is not constant, call the normal function. */
3472 if (! tree_fits_uhwi_p (len))
3473 return NULL_RTX;
3474
3475 len_rtx = expand_normal (len);
3476 src_str = c_getstr (src);
3477
3478 /* If SRC is a string constant and block move would be done
3479 by pieces, we can avoid loading the string from memory
3480 and only stored the computed constants. */
3481 if (src_str
3482 && CONST_INT_P (len_rtx)
3483 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3484 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3485 CONST_CAST (char *, src_str),
3486 dest_align, false))
3487 {
3488 dest_mem = get_memory_rtx (dest, len);
3489 set_mem_align (dest_mem, dest_align);
3490 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3491 builtin_memcpy_read_str,
3492 CONST_CAST (char *, src_str),
3493 dest_align, false, endp);
3494 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3495 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3496 return dest_mem;
3497 }
3498
3499 if (CONST_INT_P (len_rtx)
3500 && can_move_by_pieces (INTVAL (len_rtx),
3501 MIN (dest_align, src_align)))
3502 {
3503 dest_mem = get_memory_rtx (dest, len);
3504 set_mem_align (dest_mem, dest_align);
3505 src_mem = get_memory_rtx (src, len);
3506 set_mem_align (src_mem, src_align);
3507 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3508 MIN (dest_align, src_align), endp);
3509 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3510 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3511 return dest_mem;
3512 }
3513
3514 return NULL_RTX;
3515 }
3516 }
3517
3518 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3519 we failed, the caller should emit a normal call, otherwise try to
3520 get the result in TARGET, if convenient. If ENDP is 0 return the
3521 destination pointer, if ENDP is 1 return the end pointer ala
3522 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3523 stpcpy. */
3524
3525 static rtx
3526 expand_movstr (tree dest, tree src, rtx target, int endp)
3527 {
3528 struct expand_operand ops[3];
3529 rtx dest_mem;
3530 rtx src_mem;
3531
3532 if (!targetm.have_movstr ())
3533 return NULL_RTX;
3534
3535 dest_mem = get_memory_rtx (dest, NULL);
3536 src_mem = get_memory_rtx (src, NULL);
3537 if (!endp)
3538 {
3539 target = force_reg (Pmode, XEXP (dest_mem, 0));
3540 dest_mem = replace_equiv_address (dest_mem, target);
3541 }
3542
3543 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3544 create_fixed_operand (&ops[1], dest_mem);
3545 create_fixed_operand (&ops[2], src_mem);
3546 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3547 return NULL_RTX;
3548
3549 if (endp && target != const0_rtx)
3550 {
3551 target = ops[0].value;
3552 /* movstr is supposed to set end to the address of the NUL
3553 terminator. If the caller requested a mempcpy-like return value,
3554 adjust it. */
3555 if (endp == 1)
3556 {
3557 rtx tem = plus_constant (GET_MODE (target),
3558 gen_lowpart (GET_MODE (target), target), 1);
3559 emit_move_insn (target, force_operand (tem, NULL_RTX));
3560 }
3561 }
3562 return target;
3563 }
3564
3565 /* Do some very basic size validation of a call to the strcpy builtin
3566 given by EXP. Return NULL_RTX to have the built-in expand to a call
3567 to the library function. */
3568
3569 static rtx
3570 expand_builtin_strcat (tree exp, rtx)
3571 {
3572 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
3573 || !warn_stringop_overflow)
3574 return NULL_RTX;
3575
3576 tree dest = CALL_EXPR_ARG (exp, 0);
3577 tree src = CALL_EXPR_ARG (exp, 1);
3578
3579 /* There is no way here to determine the length of the string in
3580 the destination to which the SRC string is being appended so
3581 just diagnose cases when the souce string is longer than
3582 the destination object. */
3583
3584 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3585
3586 check_sizes (OPT_Wstringop_overflow_,
3587 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, src, destsize);
3588
3589 return NULL_RTX;
3590 }
3591
3592 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3593 NULL_RTX if we failed the caller should emit a normal call, otherwise
3594 try to get the result in TARGET, if convenient (and in mode MODE if that's
3595 convenient). */
3596
3597 static rtx
3598 expand_builtin_strcpy (tree exp, rtx target)
3599 {
3600 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3601 return NULL_RTX;
3602
3603 tree dest = CALL_EXPR_ARG (exp, 0);
3604 tree src = CALL_EXPR_ARG (exp, 1);
3605
3606 if (warn_stringop_overflow)
3607 {
3608 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3609 check_sizes (OPT_Wstringop_overflow_,
3610 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, src, destsize);
3611 }
3612
3613 return expand_builtin_strcpy_args (dest, src, target);
3614 }
3615
3616 /* Helper function to do the actual work for expand_builtin_strcpy. The
3617 arguments to the builtin_strcpy call DEST and SRC are broken out
3618 so that this can also be called without constructing an actual CALL_EXPR.
3619 The other arguments and return value are the same as for
3620 expand_builtin_strcpy. */
3621
3622 static rtx
3623 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3624 {
3625 return expand_movstr (dest, src, target, /*endp=*/0);
3626 }
3627
3628 /* Expand a call EXP to the stpcpy builtin.
3629 Return NULL_RTX if we failed the caller should emit a normal call,
3630 otherwise try to get the result in TARGET, if convenient (and in
3631 mode MODE if that's convenient). */
3632
3633 static rtx
3634 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3635 {
3636 tree dst, src;
3637 location_t loc = EXPR_LOCATION (exp);
3638
3639 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3640 return NULL_RTX;
3641
3642 dst = CALL_EXPR_ARG (exp, 0);
3643 src = CALL_EXPR_ARG (exp, 1);
3644
3645 /* If return value is ignored, transform stpcpy into strcpy. */
3646 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3647 {
3648 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3649 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3650 return expand_expr (result, target, mode, EXPAND_NORMAL);
3651 }
3652 else
3653 {
3654 tree len, lenp1;
3655 rtx ret;
3656
3657 /* Ensure we get an actual string whose length can be evaluated at
3658 compile-time, not an expression containing a string. This is
3659 because the latter will potentially produce pessimized code
3660 when used to produce the return value. */
3661 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3662 return expand_movstr (dst, src, target, /*endp=*/2);
3663
3664 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3665 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3666 target, mode, /*endp=*/2,
3667 exp);
3668
3669 if (ret)
3670 return ret;
3671
3672 if (TREE_CODE (len) == INTEGER_CST)
3673 {
3674 rtx len_rtx = expand_normal (len);
3675
3676 if (CONST_INT_P (len_rtx))
3677 {
3678 ret = expand_builtin_strcpy_args (dst, src, target);
3679
3680 if (ret)
3681 {
3682 if (! target)
3683 {
3684 if (mode != VOIDmode)
3685 target = gen_reg_rtx (mode);
3686 else
3687 target = gen_reg_rtx (GET_MODE (ret));
3688 }
3689 if (GET_MODE (target) != GET_MODE (ret))
3690 ret = gen_lowpart (GET_MODE (target), ret);
3691
3692 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3693 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3694 gcc_assert (ret);
3695
3696 return target;
3697 }
3698 }
3699 }
3700
3701 return expand_movstr (dst, src, target, /*endp=*/2);
3702 }
3703 }
3704
3705 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3706 bytes from constant string DATA + OFFSET and return it as target
3707 constant. */
3708
3709 rtx
3710 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3711 machine_mode mode)
3712 {
3713 const char *str = (const char *) data;
3714
3715 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3716 return const0_rtx;
3717
3718 return c_readstr (str + offset, mode);
3719 }
3720
3721 /* Helper to check the sizes of sequences and the destination of calls
3722 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3723 success (no overflow or invalid sizes), false otherwise. */
3724
3725 static bool
3726 check_strncat_sizes (tree exp, tree objsize)
3727 {
3728 tree dest = CALL_EXPR_ARG (exp, 0);
3729 tree src = CALL_EXPR_ARG (exp, 1);
3730 tree maxlen = CALL_EXPR_ARG (exp, 2);
3731
3732 /* Try to determine the range of lengths that the source expression
3733 refers to. */
3734 tree lenrange[2];
3735 get_range_strlen (src, lenrange);
3736
3737 /* Try to verify that the destination is big enough for the shortest
3738 string. */
3739
3740 if (!objsize && warn_stringop_overflow)
3741 {
3742 /* If it hasn't been provided by __strncat_chk, try to determine
3743 the size of the destination object into which the source is
3744 being copied. */
3745 objsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3746 }
3747
3748 /* Add one for the terminating nul. */
3749 tree srclen = (lenrange[0]
3750 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3751 size_one_node)
3752 : NULL_TREE);
3753
3754 /* Strncat copies at most MAXLEN bytes and always appends the terminating
3755 nul so the specified upper bound should never be equal to (or greater
3756 than) the size of the destination. */
3757 if (tree_fits_uhwi_p (maxlen) && tree_fits_uhwi_p (objsize)
3758 && tree_int_cst_equal (objsize, maxlen))
3759 {
3760 warning_at (EXPR_LOCATION (exp), OPT_Wstringop_overflow_,
3761 "specified bound %wu "
3762 "equals the size of the destination",
3763 tree_to_uhwi (maxlen));
3764
3765 return false;
3766 }
3767
3768 if (!srclen
3769 || (maxlen && tree_fits_uhwi_p (maxlen)
3770 && tree_fits_uhwi_p (srclen)
3771 && tree_int_cst_lt (maxlen, srclen)))
3772 srclen = maxlen;
3773
3774 /* The number of bytes to write is LEN but check_sizes will also
3775 check SRCLEN if LEN's value isn't known. */
3776 return check_sizes (OPT_Wstringop_overflow_,
3777 exp, /*size=*/NULL_TREE, maxlen, srclen, objsize);
3778 }
3779
3780 /* Similar to expand_builtin_strcat, do some very basic size validation
3781 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
3782 the built-in expand to a call to the library function. */
3783
3784 static rtx
3785 expand_builtin_strncat (tree exp, rtx)
3786 {
3787 if (!validate_arglist (exp,
3788 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3789 || !warn_stringop_overflow)
3790 return NULL_RTX;
3791
3792 tree dest = CALL_EXPR_ARG (exp, 0);
3793 tree src = CALL_EXPR_ARG (exp, 1);
3794 /* The upper bound on the number of bytes to write. */
3795 tree maxlen = CALL_EXPR_ARG (exp, 2);
3796 /* The length of the source sequence. */
3797 tree slen = c_strlen (src, 1);
3798
3799 /* Try to determine the range of lengths that the source expression
3800 refers to. */
3801 tree lenrange[2];
3802 if (slen)
3803 lenrange[0] = lenrange[1] = slen;
3804 else
3805 get_range_strlen (src, lenrange);
3806
3807 /* Try to verify that the destination is big enough for the shortest
3808 string. First try to determine the size of the destination object
3809 into which the source is being copied. */
3810 tree destsize = compute_dest_size (dest, warn_stringop_overflow - 1);
3811
3812 /* Add one for the terminating nul. */
3813 tree srclen = (lenrange[0]
3814 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
3815 size_one_node)
3816 : NULL_TREE);
3817
3818 /* Strncat copies at most MAXLEN bytes and always appends the terminating
3819 nul so the specified upper bound should never be equal to (or greater
3820 than) the size of the destination. */
3821 if (tree_fits_uhwi_p (maxlen) && tree_fits_uhwi_p (destsize)
3822 && tree_int_cst_equal (destsize, maxlen))
3823 {
3824 warning_at (EXPR_LOCATION (exp), OPT_Wstringop_overflow_,
3825 "specified bound %wu "
3826 "equals the size of the destination",
3827 tree_to_uhwi (maxlen));
3828
3829 return NULL_RTX;
3830 }
3831
3832 if (!srclen
3833 || (maxlen && tree_fits_uhwi_p (maxlen)
3834 && tree_fits_uhwi_p (srclen)
3835 && tree_int_cst_lt (maxlen, srclen)))
3836 srclen = maxlen;
3837
3838 /* The number of bytes to write is LEN but check_sizes will also
3839 check SRCLEN if LEN's value isn't known. */
3840 check_sizes (OPT_Wstringop_overflow_,
3841 exp, /*size=*/NULL_TREE, maxlen, srclen, destsize);
3842
3843 return NULL_RTX;
3844 }
3845
3846 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3847 NULL_RTX if we failed the caller should emit a normal call. */
3848
3849 static rtx
3850 expand_builtin_strncpy (tree exp, rtx target)
3851 {
3852 location_t loc = EXPR_LOCATION (exp);
3853
3854 if (validate_arglist (exp,
3855 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3856 {
3857 tree dest = CALL_EXPR_ARG (exp, 0);
3858 tree src = CALL_EXPR_ARG (exp, 1);
3859 /* The number of bytes to write (not the maximum). */
3860 tree len = CALL_EXPR_ARG (exp, 2);
3861 /* The length of the source sequence. */
3862 tree slen = c_strlen (src, 1);
3863
3864 if (warn_stringop_overflow)
3865 {
3866 /* Try to determine the range of lengths that the source expression
3867 refers to. */
3868 tree lenrange[2];
3869 if (slen)
3870 lenrange[0] = lenrange[1] = slen;
3871 else
3872 {
3873 get_range_strlen (src, lenrange);
3874 slen = lenrange[0];
3875 }
3876
3877 tree destsize = compute_dest_size (dest,
3878 warn_stringop_overflow - 1);
3879
3880 /* The number of bytes to write is LEN but check_sizes will also
3881 check SLEN if LEN's value isn't known. */
3882 check_sizes (OPT_Wstringop_overflow_,
3883 exp, len, /*maxlen=*/NULL_TREE, slen, destsize);
3884 }
3885
3886 /* We must be passed a constant len and src parameter. */
3887 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3888 return NULL_RTX;
3889
3890 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3891
3892 /* We're required to pad with trailing zeros if the requested
3893 len is greater than strlen(s2)+1. In that case try to
3894 use store_by_pieces, if it fails, punt. */
3895 if (tree_int_cst_lt (slen, len))
3896 {
3897 unsigned int dest_align = get_pointer_alignment (dest);
3898 const char *p = c_getstr (src);
3899 rtx dest_mem;
3900
3901 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3902 || !can_store_by_pieces (tree_to_uhwi (len),
3903 builtin_strncpy_read_str,
3904 CONST_CAST (char *, p),
3905 dest_align, false))
3906 return NULL_RTX;
3907
3908 dest_mem = get_memory_rtx (dest, len);
3909 store_by_pieces (dest_mem, tree_to_uhwi (len),
3910 builtin_strncpy_read_str,
3911 CONST_CAST (char *, p), dest_align, false, 0);
3912 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3913 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3914 return dest_mem;
3915 }
3916 }
3917 return NULL_RTX;
3918 }
3919
3920 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3921 bytes from constant string DATA + OFFSET and return it as target
3922 constant. */
3923
3924 rtx
3925 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3926 machine_mode mode)
3927 {
3928 const char *c = (const char *) data;
3929 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3930
3931 memset (p, *c, GET_MODE_SIZE (mode));
3932
3933 return c_readstr (p, mode);
3934 }
3935
3936 /* Callback routine for store_by_pieces. Return the RTL of a register
3937 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3938 char value given in the RTL register data. For example, if mode is
3939 4 bytes wide, return the RTL for 0x01010101*data. */
3940
3941 static rtx
3942 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3943 machine_mode mode)
3944 {
3945 rtx target, coeff;
3946 size_t size;
3947 char *p;
3948
3949 size = GET_MODE_SIZE (mode);
3950 if (size == 1)
3951 return (rtx) data;
3952
3953 p = XALLOCAVEC (char, size);
3954 memset (p, 1, size);
3955 coeff = c_readstr (p, mode);
3956
3957 target = convert_to_mode (mode, (rtx) data, 1);
3958 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3959 return force_reg (mode, target);
3960 }
3961
3962 /* Expand expression EXP, which is a call to the memset builtin. Return
3963 NULL_RTX if we failed the caller should emit a normal call, otherwise
3964 try to get the result in TARGET, if convenient (and in mode MODE if that's
3965 convenient). */
3966
3967 static rtx
3968 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3969 {
3970 if (!validate_arglist (exp,
3971 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3972 return NULL_RTX;
3973
3974 tree dest = CALL_EXPR_ARG (exp, 0);
3975 tree val = CALL_EXPR_ARG (exp, 1);
3976 tree len = CALL_EXPR_ARG (exp, 2);
3977
3978 check_memop_sizes (exp, dest, len);
3979
3980 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3981 }
3982
3983 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3984 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3985 try to get the result in TARGET, if convenient (and in mode MODE if that's
3986 convenient). */
3987
3988 static rtx
3989 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3990 {
3991 if (!validate_arglist (exp,
3992 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3993 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3994 return NULL_RTX;
3995 else
3996 {
3997 tree dest = CALL_EXPR_ARG (exp, 0);
3998 tree val = CALL_EXPR_ARG (exp, 2);
3999 tree len = CALL_EXPR_ARG (exp, 3);
4000 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
4001
4002 /* Return src bounds with the result. */
4003 if (res)
4004 {
4005 rtx bnd = force_reg (targetm.chkp_bound_mode (),
4006 expand_normal (CALL_EXPR_ARG (exp, 1)));
4007 res = chkp_join_splitted_slot (res, bnd);
4008 }
4009 return res;
4010 }
4011 }
4012
4013 /* Helper function to do the actual work for expand_builtin_memset. The
4014 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4015 so that this can also be called without constructing an actual CALL_EXPR.
4016 The other arguments and return value are the same as for
4017 expand_builtin_memset. */
4018
4019 static rtx
4020 expand_builtin_memset_args (tree dest, tree val, tree len,
4021 rtx target, machine_mode mode, tree orig_exp)
4022 {
4023 tree fndecl, fn;
4024 enum built_in_function fcode;
4025 machine_mode val_mode;
4026 char c;
4027 unsigned int dest_align;
4028 rtx dest_mem, dest_addr, len_rtx;
4029 HOST_WIDE_INT expected_size = -1;
4030 unsigned int expected_align = 0;
4031 unsigned HOST_WIDE_INT min_size;
4032 unsigned HOST_WIDE_INT max_size;
4033 unsigned HOST_WIDE_INT probable_max_size;
4034
4035 dest_align = get_pointer_alignment (dest);
4036
4037 /* If DEST is not a pointer type, don't do this operation in-line. */
4038 if (dest_align == 0)
4039 return NULL_RTX;
4040
4041 if (currently_expanding_gimple_stmt)
4042 stringop_block_profile (currently_expanding_gimple_stmt,
4043 &expected_align, &expected_size);
4044
4045 if (expected_align < dest_align)
4046 expected_align = dest_align;
4047
4048 /* If the LEN parameter is zero, return DEST. */
4049 if (integer_zerop (len))
4050 {
4051 /* Evaluate and ignore VAL in case it has side-effects. */
4052 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4053 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4054 }
4055
4056 /* Stabilize the arguments in case we fail. */
4057 dest = builtin_save_expr (dest);
4058 val = builtin_save_expr (val);
4059 len = builtin_save_expr (len);
4060
4061 len_rtx = expand_normal (len);
4062 determine_block_size (len, len_rtx, &min_size, &max_size,
4063 &probable_max_size);
4064 dest_mem = get_memory_rtx (dest, len);
4065 val_mode = TYPE_MODE (unsigned_char_type_node);
4066
4067 if (TREE_CODE (val) != INTEGER_CST)
4068 {
4069 rtx val_rtx;
4070
4071 val_rtx = expand_normal (val);
4072 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4073
4074 /* Assume that we can memset by pieces if we can store
4075 * the coefficients by pieces (in the required modes).
4076 * We can't pass builtin_memset_gen_str as that emits RTL. */
4077 c = 1;
4078 if (tree_fits_uhwi_p (len)
4079 && can_store_by_pieces (tree_to_uhwi (len),
4080 builtin_memset_read_str, &c, dest_align,
4081 true))
4082 {
4083 val_rtx = force_reg (val_mode, val_rtx);
4084 store_by_pieces (dest_mem, tree_to_uhwi (len),
4085 builtin_memset_gen_str, val_rtx, dest_align,
4086 true, 0);
4087 }
4088 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4089 dest_align, expected_align,
4090 expected_size, min_size, max_size,
4091 probable_max_size))
4092 goto do_libcall;
4093
4094 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4095 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4096 return dest_mem;
4097 }
4098
4099 if (target_char_cast (val, &c))
4100 goto do_libcall;
4101
4102 if (c)
4103 {
4104 if (tree_fits_uhwi_p (len)
4105 && can_store_by_pieces (tree_to_uhwi (len),
4106 builtin_memset_read_str, &c, dest_align,
4107 true))
4108 store_by_pieces (dest_mem, tree_to_uhwi (len),
4109 builtin_memset_read_str, &c, dest_align, true, 0);
4110 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4111 gen_int_mode (c, val_mode),
4112 dest_align, expected_align,
4113 expected_size, min_size, max_size,
4114 probable_max_size))
4115 goto do_libcall;
4116
4117 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4118 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4119 return dest_mem;
4120 }
4121
4122 set_mem_align (dest_mem, dest_align);
4123 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4124 CALL_EXPR_TAILCALL (orig_exp)
4125 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4126 expected_align, expected_size,
4127 min_size, max_size,
4128 probable_max_size);
4129
4130 if (dest_addr == 0)
4131 {
4132 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4133 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4134 }
4135
4136 return dest_addr;
4137
4138 do_libcall:
4139 fndecl = get_callee_fndecl (orig_exp);
4140 fcode = DECL_FUNCTION_CODE (fndecl);
4141 if (fcode == BUILT_IN_MEMSET
4142 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
4143 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4144 dest, val, len);
4145 else if (fcode == BUILT_IN_BZERO)
4146 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4147 dest, len);
4148 else
4149 gcc_unreachable ();
4150 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4151 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4152 return expand_call (fn, target, target == const0_rtx);
4153 }
4154
4155 /* Expand expression EXP, which is a call to the bzero builtin. Return
4156 NULL_RTX if we failed the caller should emit a normal call. */
4157
4158 static rtx
4159 expand_builtin_bzero (tree exp)
4160 {
4161 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4162 return NULL_RTX;
4163
4164 tree dest = CALL_EXPR_ARG (exp, 0);
4165 tree size = CALL_EXPR_ARG (exp, 1);
4166
4167 check_memop_sizes (exp, dest, size);
4168
4169 /* New argument list transforming bzero(ptr x, int y) to
4170 memset(ptr x, int 0, size_t y). This is done this way
4171 so that if it isn't expanded inline, we fallback to
4172 calling bzero instead of memset. */
4173
4174 location_t loc = EXPR_LOCATION (exp);
4175
4176 return expand_builtin_memset_args (dest, integer_zero_node,
4177 fold_convert_loc (loc,
4178 size_type_node, size),
4179 const0_rtx, VOIDmode, exp);
4180 }
4181
4182 /* Try to expand cmpstr operation ICODE with the given operands.
4183 Return the result rtx on success, otherwise return null. */
4184
4185 static rtx
4186 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4187 HOST_WIDE_INT align)
4188 {
4189 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4190
4191 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4192 target = NULL_RTX;
4193
4194 struct expand_operand ops[4];
4195 create_output_operand (&ops[0], target, insn_mode);
4196 create_fixed_operand (&ops[1], arg1_rtx);
4197 create_fixed_operand (&ops[2], arg2_rtx);
4198 create_integer_operand (&ops[3], align);
4199 if (maybe_expand_insn (icode, 4, ops))
4200 return ops[0].value;
4201 return NULL_RTX;
4202 }
4203
4204 /* Expand expression EXP, which is a call to the memcmp built-in function.
4205 Return NULL_RTX if we failed and the caller should emit a normal call,
4206 otherwise try to get the result in TARGET, if convenient.
4207 RESULT_EQ is true if we can relax the returned value to be either zero
4208 or nonzero, without caring about the sign. */
4209
4210 static rtx
4211 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4212 {
4213 if (!validate_arglist (exp,
4214 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4215 return NULL_RTX;
4216
4217 tree arg1 = CALL_EXPR_ARG (exp, 0);
4218 tree arg2 = CALL_EXPR_ARG (exp, 1);
4219 tree len = CALL_EXPR_ARG (exp, 2);
4220 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4221 location_t loc = EXPR_LOCATION (exp);
4222
4223 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4224 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4225
4226 /* If we don't have POINTER_TYPE, call the function. */
4227 if (arg1_align == 0 || arg2_align == 0)
4228 return NULL_RTX;
4229
4230 rtx arg1_rtx = get_memory_rtx (arg1, len);
4231 rtx arg2_rtx = get_memory_rtx (arg2, len);
4232 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4233
4234 /* Set MEM_SIZE as appropriate. */
4235 if (CONST_INT_P (len_rtx))
4236 {
4237 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4238 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4239 }
4240
4241 by_pieces_constfn constfn = NULL;
4242
4243 const char *src_str = c_getstr (arg2);
4244 if (result_eq && src_str == NULL)
4245 {
4246 src_str = c_getstr (arg1);
4247 if (src_str != NULL)
4248 std::swap (arg1_rtx, arg2_rtx);
4249 }
4250
4251 /* If SRC is a string constant and block move would be done
4252 by pieces, we can avoid loading the string from memory
4253 and only stored the computed constants. */
4254 if (src_str
4255 && CONST_INT_P (len_rtx)
4256 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1)
4257 constfn = builtin_memcpy_read_str;
4258
4259 rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4260 TREE_TYPE (len), target,
4261 result_eq, constfn,
4262 CONST_CAST (char *, src_str));
4263
4264 if (result)
4265 {
4266 /* Return the value in the proper mode for this function. */
4267 if (GET_MODE (result) == mode)
4268 return result;
4269
4270 if (target != 0)
4271 {
4272 convert_move (target, result, 0);
4273 return target;
4274 }
4275
4276 return convert_to_mode (mode, result, 0);
4277 }
4278
4279 return NULL_RTX;
4280 }
4281
4282 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4283 if we failed the caller should emit a normal call, otherwise try to get
4284 the result in TARGET, if convenient. */
4285
4286 static rtx
4287 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4288 {
4289 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4290 return NULL_RTX;
4291
4292 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4293 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4294 if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing)
4295 {
4296 rtx arg1_rtx, arg2_rtx;
4297 tree fndecl, fn;
4298 tree arg1 = CALL_EXPR_ARG (exp, 0);
4299 tree arg2 = CALL_EXPR_ARG (exp, 1);
4300 rtx result = NULL_RTX;
4301
4302 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4303 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4304
4305 /* If we don't have POINTER_TYPE, call the function. */
4306 if (arg1_align == 0 || arg2_align == 0)
4307 return NULL_RTX;
4308
4309 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4310 arg1 = builtin_save_expr (arg1);
4311 arg2 = builtin_save_expr (arg2);
4312
4313 arg1_rtx = get_memory_rtx (arg1, NULL);
4314 arg2_rtx = get_memory_rtx (arg2, NULL);
4315
4316 /* Try to call cmpstrsi. */
4317 if (cmpstr_icode != CODE_FOR_nothing)
4318 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4319 MIN (arg1_align, arg2_align));
4320
4321 /* Try to determine at least one length and call cmpstrnsi. */
4322 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4323 {
4324 tree len;
4325 rtx arg3_rtx;
4326
4327 tree len1 = c_strlen (arg1, 1);
4328 tree len2 = c_strlen (arg2, 1);
4329
4330 if (len1)
4331 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4332 if (len2)
4333 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4334
4335 /* If we don't have a constant length for the first, use the length
4336 of the second, if we know it. We don't require a constant for
4337 this case; some cost analysis could be done if both are available
4338 but neither is constant. For now, assume they're equally cheap,
4339 unless one has side effects. If both strings have constant lengths,
4340 use the smaller. */
4341
4342 if (!len1)
4343 len = len2;
4344 else if (!len2)
4345 len = len1;
4346 else if (TREE_SIDE_EFFECTS (len1))
4347 len = len2;
4348 else if (TREE_SIDE_EFFECTS (len2))
4349 len = len1;
4350 else if (TREE_CODE (len1) != INTEGER_CST)
4351 len = len2;
4352 else if (TREE_CODE (len2) != INTEGER_CST)
4353 len = len1;
4354 else if (tree_int_cst_lt (len1, len2))
4355 len = len1;
4356 else
4357 len = len2;
4358
4359 /* If both arguments have side effects, we cannot optimize. */
4360 if (len && !TREE_SIDE_EFFECTS (len))
4361 {
4362 arg3_rtx = expand_normal (len);
4363 result = expand_cmpstrn_or_cmpmem
4364 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4365 arg3_rtx, MIN (arg1_align, arg2_align));
4366 }
4367 }
4368
4369 if (result)
4370 {
4371 /* Return the value in the proper mode for this function. */
4372 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4373 if (GET_MODE (result) == mode)
4374 return result;
4375 if (target == 0)
4376 return convert_to_mode (mode, result, 0);
4377 convert_move (target, result, 0);
4378 return target;
4379 }
4380
4381 /* Expand the library call ourselves using a stabilized argument
4382 list to avoid re-evaluating the function's arguments twice. */
4383 fndecl = get_callee_fndecl (exp);
4384 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4385 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4386 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4387 return expand_call (fn, target, target == const0_rtx);
4388 }
4389 return NULL_RTX;
4390 }
4391
4392 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4393 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4394 the result in TARGET, if convenient. */
4395
4396 static rtx
4397 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4398 ATTRIBUTE_UNUSED machine_mode mode)
4399 {
4400 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4401
4402 if (!validate_arglist (exp,
4403 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4404 return NULL_RTX;
4405
4406 /* If c_strlen can determine an expression for one of the string
4407 lengths, and it doesn't have side effects, then emit cmpstrnsi
4408 using length MIN(strlen(string)+1, arg3). */
4409 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4410 if (cmpstrn_icode != CODE_FOR_nothing)
4411 {
4412 tree len, len1, len2, len3;
4413 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4414 rtx result;
4415 tree fndecl, fn;
4416 tree arg1 = CALL_EXPR_ARG (exp, 0);
4417 tree arg2 = CALL_EXPR_ARG (exp, 1);
4418 tree arg3 = CALL_EXPR_ARG (exp, 2);
4419
4420 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4421 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4422
4423 len1 = c_strlen (arg1, 1);
4424 len2 = c_strlen (arg2, 1);
4425
4426 if (len1)
4427 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4428 if (len2)
4429 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4430
4431 len3 = fold_convert_loc (loc, sizetype, arg3);
4432
4433 /* If we don't have a constant length for the first, use the length
4434 of the second, if we know it. If neither string is constant length,
4435 use the given length argument. We don't require a constant for
4436 this case; some cost analysis could be done if both are available
4437 but neither is constant. For now, assume they're equally cheap,
4438 unless one has side effects. If both strings have constant lengths,
4439 use the smaller. */
4440
4441 if (!len1 && !len2)
4442 len = len3;
4443 else if (!len1)
4444 len = len2;
4445 else if (!len2)
4446 len = len1;
4447 else if (TREE_SIDE_EFFECTS (len1))
4448 len = len2;
4449 else if (TREE_SIDE_EFFECTS (len2))
4450 len = len1;
4451 else if (TREE_CODE (len1) != INTEGER_CST)
4452 len = len2;
4453 else if (TREE_CODE (len2) != INTEGER_CST)
4454 len = len1;
4455 else if (tree_int_cst_lt (len1, len2))
4456 len = len1;
4457 else
4458 len = len2;
4459
4460 /* If we are not using the given length, we must incorporate it here.
4461 The actual new length parameter will be MIN(len,arg3) in this case. */
4462 if (len != len3)
4463 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4464 arg1_rtx = get_memory_rtx (arg1, len);
4465 arg2_rtx = get_memory_rtx (arg2, len);
4466 arg3_rtx = expand_normal (len);
4467 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4468 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4469 MIN (arg1_align, arg2_align));
4470 if (result)
4471 {
4472 /* Return the value in the proper mode for this function. */
4473 mode = TYPE_MODE (TREE_TYPE (exp));
4474 if (GET_MODE (result) == mode)
4475 return result;
4476 if (target == 0)
4477 return convert_to_mode (mode, result, 0);
4478 convert_move (target, result, 0);
4479 return target;
4480 }
4481
4482 /* Expand the library call ourselves using a stabilized argument
4483 list to avoid re-evaluating the function's arguments twice. */
4484 fndecl = get_callee_fndecl (exp);
4485 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4486 arg1, arg2, len);
4487 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4488 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4489 return expand_call (fn, target, target == const0_rtx);
4490 }
4491 return NULL_RTX;
4492 }
4493
4494 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4495 if that's convenient. */
4496
4497 rtx
4498 expand_builtin_saveregs (void)
4499 {
4500 rtx val;
4501 rtx_insn *seq;
4502
4503 /* Don't do __builtin_saveregs more than once in a function.
4504 Save the result of the first call and reuse it. */
4505 if (saveregs_value != 0)
4506 return saveregs_value;
4507
4508 /* When this function is called, it means that registers must be
4509 saved on entry to this function. So we migrate the call to the
4510 first insn of this function. */
4511
4512 start_sequence ();
4513
4514 /* Do whatever the machine needs done in this case. */
4515 val = targetm.calls.expand_builtin_saveregs ();
4516
4517 seq = get_insns ();
4518 end_sequence ();
4519
4520 saveregs_value = val;
4521
4522 /* Put the insns after the NOTE that starts the function. If this
4523 is inside a start_sequence, make the outer-level insn chain current, so
4524 the code is placed at the start of the function. */
4525 push_topmost_sequence ();
4526 emit_insn_after (seq, entry_of_function ());
4527 pop_topmost_sequence ();
4528
4529 return val;
4530 }
4531
4532 /* Expand a call to __builtin_next_arg. */
4533
4534 static rtx
4535 expand_builtin_next_arg (void)
4536 {
4537 /* Checking arguments is already done in fold_builtin_next_arg
4538 that must be called before this function. */
4539 return expand_binop (ptr_mode, add_optab,
4540 crtl->args.internal_arg_pointer,
4541 crtl->args.arg_offset_rtx,
4542 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4543 }
4544
4545 /* Make it easier for the backends by protecting the valist argument
4546 from multiple evaluations. */
4547
4548 static tree
4549 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4550 {
4551 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4552
4553 /* The current way of determining the type of valist is completely
4554 bogus. We should have the information on the va builtin instead. */
4555 if (!vatype)
4556 vatype = targetm.fn_abi_va_list (cfun->decl);
4557
4558 if (TREE_CODE (vatype) == ARRAY_TYPE)
4559 {
4560 if (TREE_SIDE_EFFECTS (valist))
4561 valist = save_expr (valist);
4562
4563 /* For this case, the backends will be expecting a pointer to
4564 vatype, but it's possible we've actually been given an array
4565 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4566 So fix it. */
4567 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4568 {
4569 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4570 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4571 }
4572 }
4573 else
4574 {
4575 tree pt = build_pointer_type (vatype);
4576
4577 if (! needs_lvalue)
4578 {
4579 if (! TREE_SIDE_EFFECTS (valist))
4580 return valist;
4581
4582 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4583 TREE_SIDE_EFFECTS (valist) = 1;
4584 }
4585
4586 if (TREE_SIDE_EFFECTS (valist))
4587 valist = save_expr (valist);
4588 valist = fold_build2_loc (loc, MEM_REF,
4589 vatype, valist, build_int_cst (pt, 0));
4590 }
4591
4592 return valist;
4593 }
4594
4595 /* The "standard" definition of va_list is void*. */
4596
4597 tree
4598 std_build_builtin_va_list (void)
4599 {
4600 return ptr_type_node;
4601 }
4602
4603 /* The "standard" abi va_list is va_list_type_node. */
4604
4605 tree
4606 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4607 {
4608 return va_list_type_node;
4609 }
4610
4611 /* The "standard" type of va_list is va_list_type_node. */
4612
4613 tree
4614 std_canonical_va_list_type (tree type)
4615 {
4616 tree wtype, htype;
4617
4618 wtype = va_list_type_node;
4619 htype = type;
4620
4621 if (TREE_CODE (wtype) == ARRAY_TYPE)
4622 {
4623 /* If va_list is an array type, the argument may have decayed
4624 to a pointer type, e.g. by being passed to another function.
4625 In that case, unwrap both types so that we can compare the
4626 underlying records. */
4627 if (TREE_CODE (htype) == ARRAY_TYPE
4628 || POINTER_TYPE_P (htype))
4629 {
4630 wtype = TREE_TYPE (wtype);
4631 htype = TREE_TYPE (htype);
4632 }
4633 }
4634 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4635 return va_list_type_node;
4636
4637 return NULL_TREE;
4638 }
4639
4640 /* The "standard" implementation of va_start: just assign `nextarg' to
4641 the variable. */
4642
4643 void
4644 std_expand_builtin_va_start (tree valist, rtx nextarg)
4645 {
4646 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4647 convert_move (va_r, nextarg, 0);
4648
4649 /* We do not have any valid bounds for the pointer, so
4650 just store zero bounds for it. */
4651 if (chkp_function_instrumented_p (current_function_decl))
4652 chkp_expand_bounds_reset_for_mem (valist,
4653 make_tree (TREE_TYPE (valist),
4654 nextarg));
4655 }
4656
4657 /* Expand EXP, a call to __builtin_va_start. */
4658
4659 static rtx
4660 expand_builtin_va_start (tree exp)
4661 {
4662 rtx nextarg;
4663 tree valist;
4664 location_t loc = EXPR_LOCATION (exp);
4665
4666 if (call_expr_nargs (exp) < 2)
4667 {
4668 error_at (loc, "too few arguments to function %<va_start%>");
4669 return const0_rtx;
4670 }
4671
4672 if (fold_builtin_next_arg (exp, true))
4673 return const0_rtx;
4674
4675 nextarg = expand_builtin_next_arg ();
4676 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4677
4678 if (targetm.expand_builtin_va_start)
4679 targetm.expand_builtin_va_start (valist, nextarg);
4680 else
4681 std_expand_builtin_va_start (valist, nextarg);
4682
4683 return const0_rtx;
4684 }
4685
4686 /* Expand EXP, a call to __builtin_va_end. */
4687
4688 static rtx
4689 expand_builtin_va_end (tree exp)
4690 {
4691 tree valist = CALL_EXPR_ARG (exp, 0);
4692
4693 /* Evaluate for side effects, if needed. I hate macros that don't
4694 do that. */
4695 if (TREE_SIDE_EFFECTS (valist))
4696 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4697
4698 return const0_rtx;
4699 }
4700
4701 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4702 builtin rather than just as an assignment in stdarg.h because of the
4703 nastiness of array-type va_list types. */
4704
4705 static rtx
4706 expand_builtin_va_copy (tree exp)
4707 {
4708 tree dst, src, t;
4709 location_t loc = EXPR_LOCATION (exp);
4710
4711 dst = CALL_EXPR_ARG (exp, 0);
4712 src = CALL_EXPR_ARG (exp, 1);
4713
4714 dst = stabilize_va_list_loc (loc, dst, 1);
4715 src = stabilize_va_list_loc (loc, src, 0);
4716
4717 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4718
4719 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4720 {
4721 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4722 TREE_SIDE_EFFECTS (t) = 1;
4723 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4724 }
4725 else
4726 {
4727 rtx dstb, srcb, size;
4728
4729 /* Evaluate to pointers. */
4730 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4731 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4732 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4733 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4734
4735 dstb = convert_memory_address (Pmode, dstb);
4736 srcb = convert_memory_address (Pmode, srcb);
4737
4738 /* "Dereference" to BLKmode memories. */
4739 dstb = gen_rtx_MEM (BLKmode, dstb);
4740 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4741 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4742 srcb = gen_rtx_MEM (BLKmode, srcb);
4743 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4744 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4745
4746 /* Copy. */
4747 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4748 }
4749
4750 return const0_rtx;
4751 }
4752
4753 /* Expand a call to one of the builtin functions __builtin_frame_address or
4754 __builtin_return_address. */
4755
4756 static rtx
4757 expand_builtin_frame_address (tree fndecl, tree exp)
4758 {
4759 /* The argument must be a nonnegative integer constant.
4760 It counts the number of frames to scan up the stack.
4761 The value is either the frame pointer value or the return
4762 address saved in that frame. */
4763 if (call_expr_nargs (exp) == 0)
4764 /* Warning about missing arg was already issued. */
4765 return const0_rtx;
4766 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4767 {
4768 error ("invalid argument to %qD", fndecl);
4769 return const0_rtx;
4770 }
4771 else
4772 {
4773 /* Number of frames to scan up the stack. */
4774 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4775
4776 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4777
4778 /* Some ports cannot access arbitrary stack frames. */
4779 if (tem == NULL)
4780 {
4781 warning (0, "unsupported argument to %qD", fndecl);
4782 return const0_rtx;
4783 }
4784
4785 if (count)
4786 {
4787 /* Warn since no effort is made to ensure that any frame
4788 beyond the current one exists or can be safely reached. */
4789 warning (OPT_Wframe_address, "calling %qD with "
4790 "a nonzero argument is unsafe", fndecl);
4791 }
4792
4793 /* For __builtin_frame_address, return what we've got. */
4794 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4795 return tem;
4796
4797 if (!REG_P (tem)
4798 && ! CONSTANT_P (tem))
4799 tem = copy_addr_to_reg (tem);
4800 return tem;
4801 }
4802 }
4803
4804 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4805 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4806 is the same as for allocate_dynamic_stack_space. */
4807
4808 static rtx
4809 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4810 {
4811 rtx op0;
4812 rtx result;
4813 unsigned int align;
4814 tree fndecl = get_callee_fndecl (exp);
4815 bool alloca_with_align = (DECL_FUNCTION_CODE (fndecl)
4816 == BUILT_IN_ALLOCA_WITH_ALIGN);
4817
4818 bool valid_arglist
4819 = (alloca_with_align
4820 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4821 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4822
4823 if (!valid_arglist)
4824 return NULL_RTX;
4825
4826 if ((alloca_with_align && !warn_vla_limit)
4827 || (!alloca_with_align && !warn_alloca_limit))
4828 {
4829 /* -Walloca-larger-than and -Wvla-larger-than settings override
4830 the more general -Walloc-size-larger-than so unless either of
4831 the former options is specified check the alloca arguments for
4832 overflow. */
4833 tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE };
4834 int idx[] = { 0, -1 };
4835 maybe_warn_alloc_args_overflow (fndecl, exp, args, idx);
4836 }
4837
4838 /* Compute the argument. */
4839 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4840
4841 /* Compute the alignment. */
4842 align = (alloca_with_align
4843 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4844 : BIGGEST_ALIGNMENT);
4845
4846 /* Allocate the desired space. */
4847 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4848 result = convert_memory_address (ptr_mode, result);
4849
4850 return result;
4851 }
4852
4853 /* Expand a call to bswap builtin in EXP.
4854 Return NULL_RTX if a normal call should be emitted rather than expanding the
4855 function in-line. If convenient, the result should be placed in TARGET.
4856 SUBTARGET may be used as the target for computing one of EXP's operands. */
4857
4858 static rtx
4859 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4860 rtx subtarget)
4861 {
4862 tree arg;
4863 rtx op0;
4864
4865 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4866 return NULL_RTX;
4867
4868 arg = CALL_EXPR_ARG (exp, 0);
4869 op0 = expand_expr (arg,
4870 subtarget && GET_MODE (subtarget) == target_mode
4871 ? subtarget : NULL_RTX,
4872 target_mode, EXPAND_NORMAL);
4873 if (GET_MODE (op0) != target_mode)
4874 op0 = convert_to_mode (target_mode, op0, 1);
4875
4876 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4877
4878 gcc_assert (target);
4879
4880 return convert_to_mode (target_mode, target, 1);
4881 }
4882
4883 /* Expand a call to a unary builtin in EXP.
4884 Return NULL_RTX if a normal call should be emitted rather than expanding the
4885 function in-line. If convenient, the result should be placed in TARGET.
4886 SUBTARGET may be used as the target for computing one of EXP's operands. */
4887
4888 static rtx
4889 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4890 rtx subtarget, optab op_optab)
4891 {
4892 rtx op0;
4893
4894 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4895 return NULL_RTX;
4896
4897 /* Compute the argument. */
4898 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4899 (subtarget
4900 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4901 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4902 VOIDmode, EXPAND_NORMAL);
4903 /* Compute op, into TARGET if possible.
4904 Set TARGET to wherever the result comes back. */
4905 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4906 op_optab, op0, target, op_optab != clrsb_optab);
4907 gcc_assert (target);
4908
4909 return convert_to_mode (target_mode, target, 0);
4910 }
4911
4912 /* Expand a call to __builtin_expect. We just return our argument
4913 as the builtin_expect semantic should've been already executed by
4914 tree branch prediction pass. */
4915
4916 static rtx
4917 expand_builtin_expect (tree exp, rtx target)
4918 {
4919 tree arg;
4920
4921 if (call_expr_nargs (exp) < 2)
4922 return const0_rtx;
4923 arg = CALL_EXPR_ARG (exp, 0);
4924
4925 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4926 /* When guessing was done, the hints should be already stripped away. */
4927 gcc_assert (!flag_guess_branch_prob
4928 || optimize == 0 || seen_error ());
4929 return target;
4930 }
4931
4932 /* Expand a call to __builtin_assume_aligned. We just return our first
4933 argument as the builtin_assume_aligned semantic should've been already
4934 executed by CCP. */
4935
4936 static rtx
4937 expand_builtin_assume_aligned (tree exp, rtx target)
4938 {
4939 if (call_expr_nargs (exp) < 2)
4940 return const0_rtx;
4941 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4942 EXPAND_NORMAL);
4943 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4944 && (call_expr_nargs (exp) < 3
4945 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4946 return target;
4947 }
4948
4949 void
4950 expand_builtin_trap (void)
4951 {
4952 if (targetm.have_trap ())
4953 {
4954 rtx_insn *insn = emit_insn (targetm.gen_trap ());
4955 /* For trap insns when not accumulating outgoing args force
4956 REG_ARGS_SIZE note to prevent crossjumping of calls with
4957 different args sizes. */
4958 if (!ACCUMULATE_OUTGOING_ARGS)
4959 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4960 }
4961 else
4962 {
4963 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
4964 tree call_expr = build_call_expr (fn, 0);
4965 expand_call (call_expr, NULL_RTX, false);
4966 }
4967
4968 emit_barrier ();
4969 }
4970
4971 /* Expand a call to __builtin_unreachable. We do nothing except emit
4972 a barrier saying that control flow will not pass here.
4973
4974 It is the responsibility of the program being compiled to ensure
4975 that control flow does never reach __builtin_unreachable. */
4976 static void
4977 expand_builtin_unreachable (void)
4978 {
4979 emit_barrier ();
4980 }
4981
4982 /* Expand EXP, a call to fabs, fabsf or fabsl.
4983 Return NULL_RTX if a normal call should be emitted rather than expanding
4984 the function inline. If convenient, the result should be placed
4985 in TARGET. SUBTARGET may be used as the target for computing
4986 the operand. */
4987
4988 static rtx
4989 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4990 {
4991 machine_mode mode;
4992 tree arg;
4993 rtx op0;
4994
4995 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4996 return NULL_RTX;
4997
4998 arg = CALL_EXPR_ARG (exp, 0);
4999 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5000 mode = TYPE_MODE (TREE_TYPE (arg));
5001 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5002 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5003 }
5004
5005 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5006 Return NULL is a normal call should be emitted rather than expanding the
5007 function inline. If convenient, the result should be placed in TARGET.
5008 SUBTARGET may be used as the target for computing the operand. */
5009
5010 static rtx
5011 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5012 {
5013 rtx op0, op1;
5014 tree arg;
5015
5016 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5017 return NULL_RTX;
5018
5019 arg = CALL_EXPR_ARG (exp, 0);
5020 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5021
5022 arg = CALL_EXPR_ARG (exp, 1);
5023 op1 = expand_normal (arg);
5024
5025 return expand_copysign (op0, op1, target);
5026 }
5027
5028 /* Expand a call to __builtin___clear_cache. */
5029
5030 static rtx
5031 expand_builtin___clear_cache (tree exp)
5032 {
5033 if (!targetm.code_for_clear_cache)
5034 {
5035 #ifdef CLEAR_INSN_CACHE
5036 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5037 does something. Just do the default expansion to a call to
5038 __clear_cache(). */
5039 return NULL_RTX;
5040 #else
5041 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5042 does nothing. There is no need to call it. Do nothing. */
5043 return const0_rtx;
5044 #endif /* CLEAR_INSN_CACHE */
5045 }
5046
5047 /* We have a "clear_cache" insn, and it will handle everything. */
5048 tree begin, end;
5049 rtx begin_rtx, end_rtx;
5050
5051 /* We must not expand to a library call. If we did, any
5052 fallback library function in libgcc that might contain a call to
5053 __builtin___clear_cache() would recurse infinitely. */
5054 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5055 {
5056 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5057 return const0_rtx;
5058 }
5059
5060 if (targetm.have_clear_cache ())
5061 {
5062 struct expand_operand ops[2];
5063
5064 begin = CALL_EXPR_ARG (exp, 0);
5065 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5066
5067 end = CALL_EXPR_ARG (exp, 1);
5068 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5069
5070 create_address_operand (&ops[0], begin_rtx);
5071 create_address_operand (&ops[1], end_rtx);
5072 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5073 return const0_rtx;
5074 }
5075 return const0_rtx;
5076 }
5077
5078 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5079
5080 static rtx
5081 round_trampoline_addr (rtx tramp)
5082 {
5083 rtx temp, addend, mask;
5084
5085 /* If we don't need too much alignment, we'll have been guaranteed
5086 proper alignment by get_trampoline_type. */
5087 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5088 return tramp;
5089
5090 /* Round address up to desired boundary. */
5091 temp = gen_reg_rtx (Pmode);
5092 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5093 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5094
5095 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5096 temp, 0, OPTAB_LIB_WIDEN);
5097 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5098 temp, 0, OPTAB_LIB_WIDEN);
5099
5100 return tramp;
5101 }
5102
5103 static rtx
5104 expand_builtin_init_trampoline (tree exp, bool onstack)
5105 {
5106 tree t_tramp, t_func, t_chain;
5107 rtx m_tramp, r_tramp, r_chain, tmp;
5108
5109 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5110 POINTER_TYPE, VOID_TYPE))
5111 return NULL_RTX;
5112
5113 t_tramp = CALL_EXPR_ARG (exp, 0);
5114 t_func = CALL_EXPR_ARG (exp, 1);
5115 t_chain = CALL_EXPR_ARG (exp, 2);
5116
5117 r_tramp = expand_normal (t_tramp);
5118 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5119 MEM_NOTRAP_P (m_tramp) = 1;
5120
5121 /* If ONSTACK, the TRAMP argument should be the address of a field
5122 within the local function's FRAME decl. Either way, let's see if
5123 we can fill in the MEM_ATTRs for this memory. */
5124 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5125 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5126
5127 /* Creator of a heap trampoline is responsible for making sure the
5128 address is aligned to at least STACK_BOUNDARY. Normally malloc
5129 will ensure this anyhow. */
5130 tmp = round_trampoline_addr (r_tramp);
5131 if (tmp != r_tramp)
5132 {
5133 m_tramp = change_address (m_tramp, BLKmode, tmp);
5134 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5135 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5136 }
5137
5138 /* The FUNC argument should be the address of the nested function.
5139 Extract the actual function decl to pass to the hook. */
5140 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5141 t_func = TREE_OPERAND (t_func, 0);
5142 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5143
5144 r_chain = expand_normal (t_chain);
5145
5146 /* Generate insns to initialize the trampoline. */
5147 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5148
5149 if (onstack)
5150 {
5151 trampolines_created = 1;
5152
5153 if (targetm.calls.custom_function_descriptors != 0)
5154 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5155 "trampoline generated for nested function %qD", t_func);
5156 }
5157
5158 return const0_rtx;
5159 }
5160
5161 static rtx
5162 expand_builtin_adjust_trampoline (tree exp)
5163 {
5164 rtx tramp;
5165
5166 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5167 return NULL_RTX;
5168
5169 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5170 tramp = round_trampoline_addr (tramp);
5171 if (targetm.calls.trampoline_adjust_address)
5172 tramp = targetm.calls.trampoline_adjust_address (tramp);
5173
5174 return tramp;
5175 }
5176
5177 /* Expand a call to the builtin descriptor initialization routine.
5178 A descriptor is made up of a couple of pointers to the static
5179 chain and the code entry in this order. */
5180
5181 static rtx
5182 expand_builtin_init_descriptor (tree exp)
5183 {
5184 tree t_descr, t_func, t_chain;
5185 rtx m_descr, r_descr, r_func, r_chain;
5186
5187 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5188 VOID_TYPE))
5189 return NULL_RTX;
5190
5191 t_descr = CALL_EXPR_ARG (exp, 0);
5192 t_func = CALL_EXPR_ARG (exp, 1);
5193 t_chain = CALL_EXPR_ARG (exp, 2);
5194
5195 r_descr = expand_normal (t_descr);
5196 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5197 MEM_NOTRAP_P (m_descr) = 1;
5198
5199 r_func = expand_normal (t_func);
5200 r_chain = expand_normal (t_chain);
5201
5202 /* Generate insns to initialize the descriptor. */
5203 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5204 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5205 POINTER_SIZE / BITS_PER_UNIT), r_func);
5206
5207 return const0_rtx;
5208 }
5209
5210 /* Expand a call to the builtin descriptor adjustment routine. */
5211
5212 static rtx
5213 expand_builtin_adjust_descriptor (tree exp)
5214 {
5215 rtx tramp;
5216
5217 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5218 return NULL_RTX;
5219
5220 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5221
5222 /* Unalign the descriptor to allow runtime identification. */
5223 tramp = plus_constant (ptr_mode, tramp,
5224 targetm.calls.custom_function_descriptors);
5225
5226 return force_operand (tramp, NULL_RTX);
5227 }
5228
5229 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5230 function. The function first checks whether the back end provides
5231 an insn to implement signbit for the respective mode. If not, it
5232 checks whether the floating point format of the value is such that
5233 the sign bit can be extracted. If that is not the case, error out.
5234 EXP is the expression that is a call to the builtin function; if
5235 convenient, the result should be placed in TARGET. */
5236 static rtx
5237 expand_builtin_signbit (tree exp, rtx target)
5238 {
5239 const struct real_format *fmt;
5240 machine_mode fmode, imode, rmode;
5241 tree arg;
5242 int word, bitpos;
5243 enum insn_code icode;
5244 rtx temp;
5245 location_t loc = EXPR_LOCATION (exp);
5246
5247 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5248 return NULL_RTX;
5249
5250 arg = CALL_EXPR_ARG (exp, 0);
5251 fmode = TYPE_MODE (TREE_TYPE (arg));
5252 rmode = TYPE_MODE (TREE_TYPE (exp));
5253 fmt = REAL_MODE_FORMAT (fmode);
5254
5255 arg = builtin_save_expr (arg);
5256
5257 /* Expand the argument yielding a RTX expression. */
5258 temp = expand_normal (arg);
5259
5260 /* Check if the back end provides an insn that handles signbit for the
5261 argument's mode. */
5262 icode = optab_handler (signbit_optab, fmode);
5263 if (icode != CODE_FOR_nothing)
5264 {
5265 rtx_insn *last = get_last_insn ();
5266 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5267 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5268 return target;
5269 delete_insns_since (last);
5270 }
5271
5272 /* For floating point formats without a sign bit, implement signbit
5273 as "ARG < 0.0". */
5274 bitpos = fmt->signbit_ro;
5275 if (bitpos < 0)
5276 {
5277 /* But we can't do this if the format supports signed zero. */
5278 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5279
5280 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5281 build_real (TREE_TYPE (arg), dconst0));
5282 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5283 }
5284
5285 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5286 {
5287 imode = int_mode_for_mode (fmode);
5288 gcc_assert (imode != BLKmode);
5289 temp = gen_lowpart (imode, temp);
5290 }
5291 else
5292 {
5293 imode = word_mode;
5294 /* Handle targets with different FP word orders. */
5295 if (FLOAT_WORDS_BIG_ENDIAN)
5296 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5297 else
5298 word = bitpos / BITS_PER_WORD;
5299 temp = operand_subword_force (temp, word, fmode);
5300 bitpos = bitpos % BITS_PER_WORD;
5301 }
5302
5303 /* Force the intermediate word_mode (or narrower) result into a
5304 register. This avoids attempting to create paradoxical SUBREGs
5305 of floating point modes below. */
5306 temp = force_reg (imode, temp);
5307
5308 /* If the bitpos is within the "result mode" lowpart, the operation
5309 can be implement with a single bitwise AND. Otherwise, we need
5310 a right shift and an AND. */
5311
5312 if (bitpos < GET_MODE_BITSIZE (rmode))
5313 {
5314 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5315
5316 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5317 temp = gen_lowpart (rmode, temp);
5318 temp = expand_binop (rmode, and_optab, temp,
5319 immed_wide_int_const (mask, rmode),
5320 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5321 }
5322 else
5323 {
5324 /* Perform a logical right shift to place the signbit in the least
5325 significant bit, then truncate the result to the desired mode
5326 and mask just this bit. */
5327 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5328 temp = gen_lowpart (rmode, temp);
5329 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5330 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5331 }
5332
5333 return temp;
5334 }
5335
5336 /* Expand fork or exec calls. TARGET is the desired target of the
5337 call. EXP is the call. FN is the
5338 identificator of the actual function. IGNORE is nonzero if the
5339 value is to be ignored. */
5340
5341 static rtx
5342 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5343 {
5344 tree id, decl;
5345 tree call;
5346
5347 /* If we are not profiling, just call the function. */
5348 if (!profile_arc_flag)
5349 return NULL_RTX;
5350
5351 /* Otherwise call the wrapper. This should be equivalent for the rest of
5352 compiler, so the code does not diverge, and the wrapper may run the
5353 code necessary for keeping the profiling sane. */
5354
5355 switch (DECL_FUNCTION_CODE (fn))
5356 {
5357 case BUILT_IN_FORK:
5358 id = get_identifier ("__gcov_fork");
5359 break;
5360
5361 case BUILT_IN_EXECL:
5362 id = get_identifier ("__gcov_execl");
5363 break;
5364
5365 case BUILT_IN_EXECV:
5366 id = get_identifier ("__gcov_execv");
5367 break;
5368
5369 case BUILT_IN_EXECLP:
5370 id = get_identifier ("__gcov_execlp");
5371 break;
5372
5373 case BUILT_IN_EXECLE:
5374 id = get_identifier ("__gcov_execle");
5375 break;
5376
5377 case BUILT_IN_EXECVP:
5378 id = get_identifier ("__gcov_execvp");
5379 break;
5380
5381 case BUILT_IN_EXECVE:
5382 id = get_identifier ("__gcov_execve");
5383 break;
5384
5385 default:
5386 gcc_unreachable ();
5387 }
5388
5389 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5390 FUNCTION_DECL, id, TREE_TYPE (fn));
5391 DECL_EXTERNAL (decl) = 1;
5392 TREE_PUBLIC (decl) = 1;
5393 DECL_ARTIFICIAL (decl) = 1;
5394 TREE_NOTHROW (decl) = 1;
5395 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5396 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5397 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5398 return expand_call (call, target, ignore);
5399 }
5400
5401
5402 \f
5403 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5404 the pointer in these functions is void*, the tree optimizers may remove
5405 casts. The mode computed in expand_builtin isn't reliable either, due
5406 to __sync_bool_compare_and_swap.
5407
5408 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5409 group of builtins. This gives us log2 of the mode size. */
5410
5411 static inline machine_mode
5412 get_builtin_sync_mode (int fcode_diff)
5413 {
5414 /* The size is not negotiable, so ask not to get BLKmode in return
5415 if the target indicates that a smaller size would be better. */
5416 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5417 }
5418
5419 /* Expand the memory expression LOC and return the appropriate memory operand
5420 for the builtin_sync operations. */
5421
5422 static rtx
5423 get_builtin_sync_mem (tree loc, machine_mode mode)
5424 {
5425 rtx addr, mem;
5426
5427 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5428 addr = convert_memory_address (Pmode, addr);
5429
5430 /* Note that we explicitly do not want any alias information for this
5431 memory, so that we kill all other live memories. Otherwise we don't
5432 satisfy the full barrier semantics of the intrinsic. */
5433 mem = validize_mem (gen_rtx_MEM (mode, addr));
5434
5435 /* The alignment needs to be at least according to that of the mode. */
5436 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5437 get_pointer_alignment (loc)));
5438 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5439 MEM_VOLATILE_P (mem) = 1;
5440
5441 return mem;
5442 }
5443
5444 /* Make sure an argument is in the right mode.
5445 EXP is the tree argument.
5446 MODE is the mode it should be in. */
5447
5448 static rtx
5449 expand_expr_force_mode (tree exp, machine_mode mode)
5450 {
5451 rtx val;
5452 machine_mode old_mode;
5453
5454 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5455 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5456 of CONST_INTs, where we know the old_mode only from the call argument. */
5457
5458 old_mode = GET_MODE (val);
5459 if (old_mode == VOIDmode)
5460 old_mode = TYPE_MODE (TREE_TYPE (exp));
5461 val = convert_modes (mode, old_mode, val, 1);
5462 return val;
5463 }
5464
5465
5466 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5467 EXP is the CALL_EXPR. CODE is the rtx code
5468 that corresponds to the arithmetic or logical operation from the name;
5469 an exception here is that NOT actually means NAND. TARGET is an optional
5470 place for us to store the results; AFTER is true if this is the
5471 fetch_and_xxx form. */
5472
5473 static rtx
5474 expand_builtin_sync_operation (machine_mode mode, tree exp,
5475 enum rtx_code code, bool after,
5476 rtx target)
5477 {
5478 rtx val, mem;
5479 location_t loc = EXPR_LOCATION (exp);
5480
5481 if (code == NOT && warn_sync_nand)
5482 {
5483 tree fndecl = get_callee_fndecl (exp);
5484 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5485
5486 static bool warned_f_a_n, warned_n_a_f;
5487
5488 switch (fcode)
5489 {
5490 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5491 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5492 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5493 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5494 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5495 if (warned_f_a_n)
5496 break;
5497
5498 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5499 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5500 warned_f_a_n = true;
5501 break;
5502
5503 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5504 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5505 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5506 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5507 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5508 if (warned_n_a_f)
5509 break;
5510
5511 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5512 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5513 warned_n_a_f = true;
5514 break;
5515
5516 default:
5517 gcc_unreachable ();
5518 }
5519 }
5520
5521 /* Expand the operands. */
5522 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5523 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5524
5525 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5526 after);
5527 }
5528
5529 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5530 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5531 true if this is the boolean form. TARGET is a place for us to store the
5532 results; this is NOT optional if IS_BOOL is true. */
5533
5534 static rtx
5535 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5536 bool is_bool, rtx target)
5537 {
5538 rtx old_val, new_val, mem;
5539 rtx *pbool, *poval;
5540
5541 /* Expand the operands. */
5542 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5543 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5544 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5545
5546 pbool = poval = NULL;
5547 if (target != const0_rtx)
5548 {
5549 if (is_bool)
5550 pbool = &target;
5551 else
5552 poval = &target;
5553 }
5554 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5555 false, MEMMODEL_SYNC_SEQ_CST,
5556 MEMMODEL_SYNC_SEQ_CST))
5557 return NULL_RTX;
5558
5559 return target;
5560 }
5561
5562 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5563 general form is actually an atomic exchange, and some targets only
5564 support a reduced form with the second argument being a constant 1.
5565 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5566 the results. */
5567
5568 static rtx
5569 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5570 rtx target)
5571 {
5572 rtx val, mem;
5573
5574 /* Expand the operands. */
5575 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5576 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5577
5578 return expand_sync_lock_test_and_set (target, mem, val);
5579 }
5580
5581 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5582
5583 static void
5584 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5585 {
5586 rtx mem;
5587
5588 /* Expand the operands. */
5589 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5590
5591 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5592 }
5593
5594 /* Given an integer representing an ``enum memmodel'', verify its
5595 correctness and return the memory model enum. */
5596
5597 static enum memmodel
5598 get_memmodel (tree exp)
5599 {
5600 rtx op;
5601 unsigned HOST_WIDE_INT val;
5602 source_location loc
5603 = expansion_point_location_if_in_system_header (input_location);
5604
5605 /* If the parameter is not a constant, it's a run time value so we'll just
5606 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5607 if (TREE_CODE (exp) != INTEGER_CST)
5608 return MEMMODEL_SEQ_CST;
5609
5610 op = expand_normal (exp);
5611
5612 val = INTVAL (op);
5613 if (targetm.memmodel_check)
5614 val = targetm.memmodel_check (val);
5615 else if (val & ~MEMMODEL_MASK)
5616 {
5617 warning_at (loc, OPT_Winvalid_memory_model,
5618 "unknown architecture specifier in memory model to builtin");
5619 return MEMMODEL_SEQ_CST;
5620 }
5621
5622 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5623 if (memmodel_base (val) >= MEMMODEL_LAST)
5624 {
5625 warning_at (loc, OPT_Winvalid_memory_model,
5626 "invalid memory model argument to builtin");
5627 return MEMMODEL_SEQ_CST;
5628 }
5629
5630 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5631 be conservative and promote consume to acquire. */
5632 if (val == MEMMODEL_CONSUME)
5633 val = MEMMODEL_ACQUIRE;
5634
5635 return (enum memmodel) val;
5636 }
5637
5638 /* Expand the __atomic_exchange intrinsic:
5639 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5640 EXP is the CALL_EXPR.
5641 TARGET is an optional place for us to store the results. */
5642
5643 static rtx
5644 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5645 {
5646 rtx val, mem;
5647 enum memmodel model;
5648
5649 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5650
5651 if (!flag_inline_atomics)
5652 return NULL_RTX;
5653
5654 /* Expand the operands. */
5655 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5656 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5657
5658 return expand_atomic_exchange (target, mem, val, model);
5659 }
5660
5661 /* Expand the __atomic_compare_exchange intrinsic:
5662 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5663 TYPE desired, BOOL weak,
5664 enum memmodel success,
5665 enum memmodel failure)
5666 EXP is the CALL_EXPR.
5667 TARGET is an optional place for us to store the results. */
5668
5669 static rtx
5670 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5671 rtx target)
5672 {
5673 rtx expect, desired, mem, oldval;
5674 rtx_code_label *label;
5675 enum memmodel success, failure;
5676 tree weak;
5677 bool is_weak;
5678 source_location loc
5679 = expansion_point_location_if_in_system_header (input_location);
5680
5681 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5682 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5683
5684 if (failure > success)
5685 {
5686 warning_at (loc, OPT_Winvalid_memory_model,
5687 "failure memory model cannot be stronger than success "
5688 "memory model for %<__atomic_compare_exchange%>");
5689 success = MEMMODEL_SEQ_CST;
5690 }
5691
5692 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5693 {
5694 warning_at (loc, OPT_Winvalid_memory_model,
5695 "invalid failure memory model for "
5696 "%<__atomic_compare_exchange%>");
5697 failure = MEMMODEL_SEQ_CST;
5698 success = MEMMODEL_SEQ_CST;
5699 }
5700
5701
5702 if (!flag_inline_atomics)
5703 return NULL_RTX;
5704
5705 /* Expand the operands. */
5706 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5707
5708 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5709 expect = convert_memory_address (Pmode, expect);
5710 expect = gen_rtx_MEM (mode, expect);
5711 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5712
5713 weak = CALL_EXPR_ARG (exp, 3);
5714 is_weak = false;
5715 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5716 is_weak = true;
5717
5718 if (target == const0_rtx)
5719 target = NULL;
5720
5721 /* Lest the rtl backend create a race condition with an imporoper store
5722 to memory, always create a new pseudo for OLDVAL. */
5723 oldval = NULL;
5724
5725 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5726 is_weak, success, failure))
5727 return NULL_RTX;
5728
5729 /* Conditionally store back to EXPECT, lest we create a race condition
5730 with an improper store to memory. */
5731 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5732 the normal case where EXPECT is totally private, i.e. a register. At
5733 which point the store can be unconditional. */
5734 label = gen_label_rtx ();
5735 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5736 GET_MODE (target), 1, label);
5737 emit_move_insn (expect, oldval);
5738 emit_label (label);
5739
5740 return target;
5741 }
5742
5743 /* Helper function for expand_ifn_atomic_compare_exchange - expand
5744 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
5745 call. The weak parameter must be dropped to match the expected parameter
5746 list and the expected argument changed from value to pointer to memory
5747 slot. */
5748
5749 static void
5750 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
5751 {
5752 unsigned int z;
5753 vec<tree, va_gc> *vec;
5754
5755 vec_alloc (vec, 5);
5756 vec->quick_push (gimple_call_arg (call, 0));
5757 tree expected = gimple_call_arg (call, 1);
5758 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
5759 TREE_TYPE (expected));
5760 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
5761 if (expd != x)
5762 emit_move_insn (x, expd);
5763 tree v = make_tree (TREE_TYPE (expected), x);
5764 vec->quick_push (build1 (ADDR_EXPR,
5765 build_pointer_type (TREE_TYPE (expected)), v));
5766 vec->quick_push (gimple_call_arg (call, 2));
5767 /* Skip the boolean weak parameter. */
5768 for (z = 4; z < 6; z++)
5769 vec->quick_push (gimple_call_arg (call, z));
5770 built_in_function fncode
5771 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
5772 + exact_log2 (GET_MODE_SIZE (mode)));
5773 tree fndecl = builtin_decl_explicit (fncode);
5774 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
5775 fndecl);
5776 tree exp = build_call_vec (boolean_type_node, fn, vec);
5777 tree lhs = gimple_call_lhs (call);
5778 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
5779 if (lhs)
5780 {
5781 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5782 if (GET_MODE (boolret) != mode)
5783 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
5784 x = force_reg (mode, x);
5785 write_complex_part (target, boolret, true);
5786 write_complex_part (target, x, false);
5787 }
5788 }
5789
5790 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
5791
5792 void
5793 expand_ifn_atomic_compare_exchange (gcall *call)
5794 {
5795 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
5796 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
5797 machine_mode mode = mode_for_size (BITS_PER_UNIT * size, MODE_INT, 0);
5798 rtx expect, desired, mem, oldval, boolret;
5799 enum memmodel success, failure;
5800 tree lhs;
5801 bool is_weak;
5802 source_location loc
5803 = expansion_point_location_if_in_system_header (gimple_location (call));
5804
5805 success = get_memmodel (gimple_call_arg (call, 4));
5806 failure = get_memmodel (gimple_call_arg (call, 5));
5807
5808 if (failure > success)
5809 {
5810 warning_at (loc, OPT_Winvalid_memory_model,
5811 "failure memory model cannot be stronger than success "
5812 "memory model for %<__atomic_compare_exchange%>");
5813 success = MEMMODEL_SEQ_CST;
5814 }
5815
5816 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5817 {
5818 warning_at (loc, OPT_Winvalid_memory_model,
5819 "invalid failure memory model for "
5820 "%<__atomic_compare_exchange%>");
5821 failure = MEMMODEL_SEQ_CST;
5822 success = MEMMODEL_SEQ_CST;
5823 }
5824
5825 if (!flag_inline_atomics)
5826 {
5827 expand_ifn_atomic_compare_exchange_into_call (call, mode);
5828 return;
5829 }
5830
5831 /* Expand the operands. */
5832 mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
5833
5834 expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
5835 desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
5836
5837 is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
5838
5839 boolret = NULL;
5840 oldval = NULL;
5841
5842 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
5843 is_weak, success, failure))
5844 {
5845 expand_ifn_atomic_compare_exchange_into_call (call, mode);
5846 return;
5847 }
5848
5849 lhs = gimple_call_lhs (call);
5850 if (lhs)
5851 {
5852 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5853 if (GET_MODE (boolret) != mode)
5854 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
5855 write_complex_part (target, boolret, true);
5856 write_complex_part (target, oldval, false);
5857 }
5858 }
5859
5860 /* Expand the __atomic_load intrinsic:
5861 TYPE __atomic_load (TYPE *object, enum memmodel)
5862 EXP is the CALL_EXPR.
5863 TARGET is an optional place for us to store the results. */
5864
5865 static rtx
5866 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5867 {
5868 rtx mem;
5869 enum memmodel model;
5870
5871 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5872 if (is_mm_release (model) || is_mm_acq_rel (model))
5873 {
5874 source_location loc
5875 = expansion_point_location_if_in_system_header (input_location);
5876 warning_at (loc, OPT_Winvalid_memory_model,
5877 "invalid memory model for %<__atomic_load%>");
5878 model = MEMMODEL_SEQ_CST;
5879 }
5880
5881 if (!flag_inline_atomics)
5882 return NULL_RTX;
5883
5884 /* Expand the operand. */
5885 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5886
5887 return expand_atomic_load (target, mem, model);
5888 }
5889
5890
5891 /* Expand the __atomic_store intrinsic:
5892 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5893 EXP is the CALL_EXPR.
5894 TARGET is an optional place for us to store the results. */
5895
5896 static rtx
5897 expand_builtin_atomic_store (machine_mode mode, tree exp)
5898 {
5899 rtx mem, val;
5900 enum memmodel model;
5901
5902 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5903 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5904 || is_mm_release (model)))
5905 {
5906 source_location loc
5907 = expansion_point_location_if_in_system_header (input_location);
5908 warning_at (loc, OPT_Winvalid_memory_model,
5909 "invalid memory model for %<__atomic_store%>");
5910 model = MEMMODEL_SEQ_CST;
5911 }
5912
5913 if (!flag_inline_atomics)
5914 return NULL_RTX;
5915
5916 /* Expand the operands. */
5917 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5918 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5919
5920 return expand_atomic_store (mem, val, model, false);
5921 }
5922
5923 /* Expand the __atomic_fetch_XXX intrinsic:
5924 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5925 EXP is the CALL_EXPR.
5926 TARGET is an optional place for us to store the results.
5927 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5928 FETCH_AFTER is true if returning the result of the operation.
5929 FETCH_AFTER is false if returning the value before the operation.
5930 IGNORE is true if the result is not used.
5931 EXT_CALL is the correct builtin for an external call if this cannot be
5932 resolved to an instruction sequence. */
5933
5934 static rtx
5935 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5936 enum rtx_code code, bool fetch_after,
5937 bool ignore, enum built_in_function ext_call)
5938 {
5939 rtx val, mem, ret;
5940 enum memmodel model;
5941 tree fndecl;
5942 tree addr;
5943
5944 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5945
5946 /* Expand the operands. */
5947 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5948 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5949
5950 /* Only try generating instructions if inlining is turned on. */
5951 if (flag_inline_atomics)
5952 {
5953 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5954 if (ret)
5955 return ret;
5956 }
5957
5958 /* Return if a different routine isn't needed for the library call. */
5959 if (ext_call == BUILT_IN_NONE)
5960 return NULL_RTX;
5961
5962 /* Change the call to the specified function. */
5963 fndecl = get_callee_fndecl (exp);
5964 addr = CALL_EXPR_FN (exp);
5965 STRIP_NOPS (addr);
5966
5967 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5968 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5969
5970 /* Expand the call here so we can emit trailing code. */
5971 ret = expand_call (exp, target, ignore);
5972
5973 /* Replace the original function just in case it matters. */
5974 TREE_OPERAND (addr, 0) = fndecl;
5975
5976 /* Then issue the arithmetic correction to return the right result. */
5977 if (!ignore)
5978 {
5979 if (code == NOT)
5980 {
5981 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5982 OPTAB_LIB_WIDEN);
5983 ret = expand_simple_unop (mode, NOT, ret, target, true);
5984 }
5985 else
5986 ret = expand_simple_binop (mode, code, ret, val, target, true,
5987 OPTAB_LIB_WIDEN);
5988 }
5989 return ret;
5990 }
5991
5992 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
5993
5994 void
5995 expand_ifn_atomic_bit_test_and (gcall *call)
5996 {
5997 tree ptr = gimple_call_arg (call, 0);
5998 tree bit = gimple_call_arg (call, 1);
5999 tree flag = gimple_call_arg (call, 2);
6000 tree lhs = gimple_call_lhs (call);
6001 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6002 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6003 enum rtx_code code;
6004 optab optab;
6005 struct expand_operand ops[5];
6006
6007 gcc_assert (flag_inline_atomics);
6008
6009 if (gimple_call_num_args (call) == 4)
6010 model = get_memmodel (gimple_call_arg (call, 3));
6011
6012 rtx mem = get_builtin_sync_mem (ptr, mode);
6013 rtx val = expand_expr_force_mode (bit, mode);
6014
6015 switch (gimple_call_internal_fn (call))
6016 {
6017 case IFN_ATOMIC_BIT_TEST_AND_SET:
6018 code = IOR;
6019 optab = atomic_bit_test_and_set_optab;
6020 break;
6021 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6022 code = XOR;
6023 optab = atomic_bit_test_and_complement_optab;
6024 break;
6025 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6026 code = AND;
6027 optab = atomic_bit_test_and_reset_optab;
6028 break;
6029 default:
6030 gcc_unreachable ();
6031 }
6032
6033 if (lhs == NULL_TREE)
6034 {
6035 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6036 val, NULL_RTX, true, OPTAB_DIRECT);
6037 if (code == AND)
6038 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6039 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6040 return;
6041 }
6042
6043 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6044 enum insn_code icode = direct_optab_handler (optab, mode);
6045 gcc_assert (icode != CODE_FOR_nothing);
6046 create_output_operand (&ops[0], target, mode);
6047 create_fixed_operand (&ops[1], mem);
6048 create_convert_operand_to (&ops[2], val, mode, true);
6049 create_integer_operand (&ops[3], model);
6050 create_integer_operand (&ops[4], integer_onep (flag));
6051 if (maybe_expand_insn (icode, 5, ops))
6052 return;
6053
6054 rtx bitval = val;
6055 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6056 val, NULL_RTX, true, OPTAB_DIRECT);
6057 rtx maskval = val;
6058 if (code == AND)
6059 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6060 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6061 code, model, false);
6062 if (integer_onep (flag))
6063 {
6064 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6065 NULL_RTX, true, OPTAB_DIRECT);
6066 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6067 true, OPTAB_DIRECT);
6068 }
6069 else
6070 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6071 OPTAB_DIRECT);
6072 if (result != target)
6073 emit_move_insn (target, result);
6074 }
6075
6076 /* Expand an atomic clear operation.
6077 void _atomic_clear (BOOL *obj, enum memmodel)
6078 EXP is the call expression. */
6079
6080 static rtx
6081 expand_builtin_atomic_clear (tree exp)
6082 {
6083 machine_mode mode;
6084 rtx mem, ret;
6085 enum memmodel model;
6086
6087 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
6088 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6089 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6090
6091 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6092 {
6093 source_location loc
6094 = expansion_point_location_if_in_system_header (input_location);
6095 warning_at (loc, OPT_Winvalid_memory_model,
6096 "invalid memory model for %<__atomic_store%>");
6097 model = MEMMODEL_SEQ_CST;
6098 }
6099
6100 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6101 Failing that, a store is issued by __atomic_store. The only way this can
6102 fail is if the bool type is larger than a word size. Unlikely, but
6103 handle it anyway for completeness. Assume a single threaded model since
6104 there is no atomic support in this case, and no barriers are required. */
6105 ret = expand_atomic_store (mem, const0_rtx, model, true);
6106 if (!ret)
6107 emit_move_insn (mem, const0_rtx);
6108 return const0_rtx;
6109 }
6110
6111 /* Expand an atomic test_and_set operation.
6112 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6113 EXP is the call expression. */
6114
6115 static rtx
6116 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6117 {
6118 rtx mem;
6119 enum memmodel model;
6120 machine_mode mode;
6121
6122 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
6123 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6124 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6125
6126 return expand_atomic_test_and_set (target, mem, model);
6127 }
6128
6129
6130 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6131 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6132
6133 static tree
6134 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6135 {
6136 int size;
6137 machine_mode mode;
6138 unsigned int mode_align, type_align;
6139
6140 if (TREE_CODE (arg0) != INTEGER_CST)
6141 return NULL_TREE;
6142
6143 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6144 mode = mode_for_size (size, MODE_INT, 0);
6145 mode_align = GET_MODE_ALIGNMENT (mode);
6146
6147 if (TREE_CODE (arg1) == INTEGER_CST)
6148 {
6149 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6150
6151 /* Either this argument is null, or it's a fake pointer encoding
6152 the alignment of the object. */
6153 val = least_bit_hwi (val);
6154 val *= BITS_PER_UNIT;
6155
6156 if (val == 0 || mode_align < val)
6157 type_align = mode_align;
6158 else
6159 type_align = val;
6160 }
6161 else
6162 {
6163 tree ttype = TREE_TYPE (arg1);
6164
6165 /* This function is usually invoked and folded immediately by the front
6166 end before anything else has a chance to look at it. The pointer
6167 parameter at this point is usually cast to a void *, so check for that
6168 and look past the cast. */
6169 if (CONVERT_EXPR_P (arg1)
6170 && POINTER_TYPE_P (ttype)
6171 && VOID_TYPE_P (TREE_TYPE (ttype))
6172 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6173 arg1 = TREE_OPERAND (arg1, 0);
6174
6175 ttype = TREE_TYPE (arg1);
6176 gcc_assert (POINTER_TYPE_P (ttype));
6177
6178 /* Get the underlying type of the object. */
6179 ttype = TREE_TYPE (ttype);
6180 type_align = TYPE_ALIGN (ttype);
6181 }
6182
6183 /* If the object has smaller alignment, the lock free routines cannot
6184 be used. */
6185 if (type_align < mode_align)
6186 return boolean_false_node;
6187
6188 /* Check if a compare_and_swap pattern exists for the mode which represents
6189 the required size. The pattern is not allowed to fail, so the existence
6190 of the pattern indicates support is present. */
6191 if (can_compare_and_swap_p (mode, true))
6192 return boolean_true_node;
6193 else
6194 return boolean_false_node;
6195 }
6196
6197 /* Return true if the parameters to call EXP represent an object which will
6198 always generate lock free instructions. The first argument represents the
6199 size of the object, and the second parameter is a pointer to the object
6200 itself. If NULL is passed for the object, then the result is based on
6201 typical alignment for an object of the specified size. Otherwise return
6202 false. */
6203
6204 static rtx
6205 expand_builtin_atomic_always_lock_free (tree exp)
6206 {
6207 tree size;
6208 tree arg0 = CALL_EXPR_ARG (exp, 0);
6209 tree arg1 = CALL_EXPR_ARG (exp, 1);
6210
6211 if (TREE_CODE (arg0) != INTEGER_CST)
6212 {
6213 error ("non-constant argument 1 to __atomic_always_lock_free");
6214 return const0_rtx;
6215 }
6216
6217 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6218 if (size == boolean_true_node)
6219 return const1_rtx;
6220 return const0_rtx;
6221 }
6222
6223 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6224 is lock free on this architecture. */
6225
6226 static tree
6227 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6228 {
6229 if (!flag_inline_atomics)
6230 return NULL_TREE;
6231
6232 /* If it isn't always lock free, don't generate a result. */
6233 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6234 return boolean_true_node;
6235
6236 return NULL_TREE;
6237 }
6238
6239 /* Return true if the parameters to call EXP represent an object which will
6240 always generate lock free instructions. The first argument represents the
6241 size of the object, and the second parameter is a pointer to the object
6242 itself. If NULL is passed for the object, then the result is based on
6243 typical alignment for an object of the specified size. Otherwise return
6244 NULL*/
6245
6246 static rtx
6247 expand_builtin_atomic_is_lock_free (tree exp)
6248 {
6249 tree size;
6250 tree arg0 = CALL_EXPR_ARG (exp, 0);
6251 tree arg1 = CALL_EXPR_ARG (exp, 1);
6252
6253 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6254 {
6255 error ("non-integer argument 1 to __atomic_is_lock_free");
6256 return NULL_RTX;
6257 }
6258
6259 if (!flag_inline_atomics)
6260 return NULL_RTX;
6261
6262 /* If the value is known at compile time, return the RTX for it. */
6263 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6264 if (size == boolean_true_node)
6265 return const1_rtx;
6266
6267 return NULL_RTX;
6268 }
6269
6270 /* Expand the __atomic_thread_fence intrinsic:
6271 void __atomic_thread_fence (enum memmodel)
6272 EXP is the CALL_EXPR. */
6273
6274 static void
6275 expand_builtin_atomic_thread_fence (tree exp)
6276 {
6277 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6278 expand_mem_thread_fence (model);
6279 }
6280
6281 /* Expand the __atomic_signal_fence intrinsic:
6282 void __atomic_signal_fence (enum memmodel)
6283 EXP is the CALL_EXPR. */
6284
6285 static void
6286 expand_builtin_atomic_signal_fence (tree exp)
6287 {
6288 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6289 expand_mem_signal_fence (model);
6290 }
6291
6292 /* Expand the __sync_synchronize intrinsic. */
6293
6294 static void
6295 expand_builtin_sync_synchronize (void)
6296 {
6297 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6298 }
6299
6300 static rtx
6301 expand_builtin_thread_pointer (tree exp, rtx target)
6302 {
6303 enum insn_code icode;
6304 if (!validate_arglist (exp, VOID_TYPE))
6305 return const0_rtx;
6306 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6307 if (icode != CODE_FOR_nothing)
6308 {
6309 struct expand_operand op;
6310 /* If the target is not sutitable then create a new target. */
6311 if (target == NULL_RTX
6312 || !REG_P (target)
6313 || GET_MODE (target) != Pmode)
6314 target = gen_reg_rtx (Pmode);
6315 create_output_operand (&op, target, Pmode);
6316 expand_insn (icode, 1, &op);
6317 return target;
6318 }
6319 error ("__builtin_thread_pointer is not supported on this target");
6320 return const0_rtx;
6321 }
6322
6323 static void
6324 expand_builtin_set_thread_pointer (tree exp)
6325 {
6326 enum insn_code icode;
6327 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6328 return;
6329 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6330 if (icode != CODE_FOR_nothing)
6331 {
6332 struct expand_operand op;
6333 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6334 Pmode, EXPAND_NORMAL);
6335 create_input_operand (&op, val, Pmode);
6336 expand_insn (icode, 1, &op);
6337 return;
6338 }
6339 error ("__builtin_set_thread_pointer is not supported on this target");
6340 }
6341
6342 \f
6343 /* Emit code to restore the current value of stack. */
6344
6345 static void
6346 expand_stack_restore (tree var)
6347 {
6348 rtx_insn *prev;
6349 rtx sa = expand_normal (var);
6350
6351 sa = convert_memory_address (Pmode, sa);
6352
6353 prev = get_last_insn ();
6354 emit_stack_restore (SAVE_BLOCK, sa);
6355
6356 record_new_stack_level ();
6357
6358 fixup_args_size_notes (prev, get_last_insn (), 0);
6359 }
6360
6361 /* Emit code to save the current value of stack. */
6362
6363 static rtx
6364 expand_stack_save (void)
6365 {
6366 rtx ret = NULL_RTX;
6367
6368 emit_stack_save (SAVE_BLOCK, &ret);
6369 return ret;
6370 }
6371
6372
6373 /* Expand an expression EXP that calls a built-in function,
6374 with result going to TARGET if that's convenient
6375 (and in mode MODE if that's convenient).
6376 SUBTARGET may be used as the target for computing one of EXP's operands.
6377 IGNORE is nonzero if the value is to be ignored. */
6378
6379 rtx
6380 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6381 int ignore)
6382 {
6383 tree fndecl = get_callee_fndecl (exp);
6384 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6385 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6386 int flags;
6387
6388 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6389 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6390
6391 /* When ASan is enabled, we don't want to expand some memory/string
6392 builtins and rely on libsanitizer's hooks. This allows us to avoid
6393 redundant checks and be sure, that possible overflow will be detected
6394 by ASan. */
6395
6396 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6397 return expand_call (exp, target, ignore);
6398
6399 /* When not optimizing, generate calls to library functions for a certain
6400 set of builtins. */
6401 if (!optimize
6402 && !called_as_built_in (fndecl)
6403 && fcode != BUILT_IN_FORK
6404 && fcode != BUILT_IN_EXECL
6405 && fcode != BUILT_IN_EXECV
6406 && fcode != BUILT_IN_EXECLP
6407 && fcode != BUILT_IN_EXECLE
6408 && fcode != BUILT_IN_EXECVP
6409 && fcode != BUILT_IN_EXECVE
6410 && fcode != BUILT_IN_ALLOCA
6411 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
6412 && fcode != BUILT_IN_FREE
6413 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
6414 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
6415 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
6416 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
6417 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6418 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
6419 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
6420 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
6421 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
6422 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
6423 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
6424 && fcode != BUILT_IN_CHKP_BNDRET)
6425 return expand_call (exp, target, ignore);
6426
6427 /* The built-in function expanders test for target == const0_rtx
6428 to determine whether the function's result will be ignored. */
6429 if (ignore)
6430 target = const0_rtx;
6431
6432 /* If the result of a pure or const built-in function is ignored, and
6433 none of its arguments are volatile, we can avoid expanding the
6434 built-in call and just evaluate the arguments for side-effects. */
6435 if (target == const0_rtx
6436 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6437 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6438 {
6439 bool volatilep = false;
6440 tree arg;
6441 call_expr_arg_iterator iter;
6442
6443 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6444 if (TREE_THIS_VOLATILE (arg))
6445 {
6446 volatilep = true;
6447 break;
6448 }
6449
6450 if (! volatilep)
6451 {
6452 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6453 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6454 return const0_rtx;
6455 }
6456 }
6457
6458 /* expand_builtin_with_bounds is supposed to be used for
6459 instrumented builtin calls. */
6460 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6461
6462 switch (fcode)
6463 {
6464 CASE_FLT_FN (BUILT_IN_FABS):
6465 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
6466 case BUILT_IN_FABSD32:
6467 case BUILT_IN_FABSD64:
6468 case BUILT_IN_FABSD128:
6469 target = expand_builtin_fabs (exp, target, subtarget);
6470 if (target)
6471 return target;
6472 break;
6473
6474 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6475 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
6476 target = expand_builtin_copysign (exp, target, subtarget);
6477 if (target)
6478 return target;
6479 break;
6480
6481 /* Just do a normal library call if we were unable to fold
6482 the values. */
6483 CASE_FLT_FN (BUILT_IN_CABS):
6484 break;
6485
6486 CASE_FLT_FN (BUILT_IN_FMA):
6487 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6488 if (target)
6489 return target;
6490 break;
6491
6492 CASE_FLT_FN (BUILT_IN_ILOGB):
6493 if (! flag_unsafe_math_optimizations)
6494 break;
6495 gcc_fallthrough ();
6496 CASE_FLT_FN (BUILT_IN_ISINF):
6497 CASE_FLT_FN (BUILT_IN_FINITE):
6498 case BUILT_IN_ISFINITE:
6499 case BUILT_IN_ISNORMAL:
6500 target = expand_builtin_interclass_mathfn (exp, target);
6501 if (target)
6502 return target;
6503 break;
6504
6505 CASE_FLT_FN (BUILT_IN_ICEIL):
6506 CASE_FLT_FN (BUILT_IN_LCEIL):
6507 CASE_FLT_FN (BUILT_IN_LLCEIL):
6508 CASE_FLT_FN (BUILT_IN_LFLOOR):
6509 CASE_FLT_FN (BUILT_IN_IFLOOR):
6510 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6511 target = expand_builtin_int_roundingfn (exp, target);
6512 if (target)
6513 return target;
6514 break;
6515
6516 CASE_FLT_FN (BUILT_IN_IRINT):
6517 CASE_FLT_FN (BUILT_IN_LRINT):
6518 CASE_FLT_FN (BUILT_IN_LLRINT):
6519 CASE_FLT_FN (BUILT_IN_IROUND):
6520 CASE_FLT_FN (BUILT_IN_LROUND):
6521 CASE_FLT_FN (BUILT_IN_LLROUND):
6522 target = expand_builtin_int_roundingfn_2 (exp, target);
6523 if (target)
6524 return target;
6525 break;
6526
6527 CASE_FLT_FN (BUILT_IN_POWI):
6528 target = expand_builtin_powi (exp, target);
6529 if (target)
6530 return target;
6531 break;
6532
6533 CASE_FLT_FN (BUILT_IN_CEXPI):
6534 target = expand_builtin_cexpi (exp, target);
6535 gcc_assert (target);
6536 return target;
6537
6538 CASE_FLT_FN (BUILT_IN_SIN):
6539 CASE_FLT_FN (BUILT_IN_COS):
6540 if (! flag_unsafe_math_optimizations)
6541 break;
6542 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6543 if (target)
6544 return target;
6545 break;
6546
6547 CASE_FLT_FN (BUILT_IN_SINCOS):
6548 if (! flag_unsafe_math_optimizations)
6549 break;
6550 target = expand_builtin_sincos (exp);
6551 if (target)
6552 return target;
6553 break;
6554
6555 case BUILT_IN_APPLY_ARGS:
6556 return expand_builtin_apply_args ();
6557
6558 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6559 FUNCTION with a copy of the parameters described by
6560 ARGUMENTS, and ARGSIZE. It returns a block of memory
6561 allocated on the stack into which is stored all the registers
6562 that might possibly be used for returning the result of a
6563 function. ARGUMENTS is the value returned by
6564 __builtin_apply_args. ARGSIZE is the number of bytes of
6565 arguments that must be copied. ??? How should this value be
6566 computed? We'll also need a safe worst case value for varargs
6567 functions. */
6568 case BUILT_IN_APPLY:
6569 if (!validate_arglist (exp, POINTER_TYPE,
6570 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6571 && !validate_arglist (exp, REFERENCE_TYPE,
6572 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6573 return const0_rtx;
6574 else
6575 {
6576 rtx ops[3];
6577
6578 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6579 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6580 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6581
6582 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6583 }
6584
6585 /* __builtin_return (RESULT) causes the function to return the
6586 value described by RESULT. RESULT is address of the block of
6587 memory returned by __builtin_apply. */
6588 case BUILT_IN_RETURN:
6589 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6590 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6591 return const0_rtx;
6592
6593 case BUILT_IN_SAVEREGS:
6594 return expand_builtin_saveregs ();
6595
6596 case BUILT_IN_VA_ARG_PACK:
6597 /* All valid uses of __builtin_va_arg_pack () are removed during
6598 inlining. */
6599 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6600 return const0_rtx;
6601
6602 case BUILT_IN_VA_ARG_PACK_LEN:
6603 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6604 inlining. */
6605 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6606 return const0_rtx;
6607
6608 /* Return the address of the first anonymous stack arg. */
6609 case BUILT_IN_NEXT_ARG:
6610 if (fold_builtin_next_arg (exp, false))
6611 return const0_rtx;
6612 return expand_builtin_next_arg ();
6613
6614 case BUILT_IN_CLEAR_CACHE:
6615 target = expand_builtin___clear_cache (exp);
6616 if (target)
6617 return target;
6618 break;
6619
6620 case BUILT_IN_CLASSIFY_TYPE:
6621 return expand_builtin_classify_type (exp);
6622
6623 case BUILT_IN_CONSTANT_P:
6624 return const0_rtx;
6625
6626 case BUILT_IN_FRAME_ADDRESS:
6627 case BUILT_IN_RETURN_ADDRESS:
6628 return expand_builtin_frame_address (fndecl, exp);
6629
6630 /* Returns the address of the area where the structure is returned.
6631 0 otherwise. */
6632 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6633 if (call_expr_nargs (exp) != 0
6634 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6635 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6636 return const0_rtx;
6637 else
6638 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6639
6640 case BUILT_IN_ALLOCA:
6641 case BUILT_IN_ALLOCA_WITH_ALIGN:
6642 /* If the allocation stems from the declaration of a variable-sized
6643 object, it cannot accumulate. */
6644 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6645 if (target)
6646 return target;
6647 break;
6648
6649 case BUILT_IN_STACK_SAVE:
6650 return expand_stack_save ();
6651
6652 case BUILT_IN_STACK_RESTORE:
6653 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6654 return const0_rtx;
6655
6656 case BUILT_IN_BSWAP16:
6657 case BUILT_IN_BSWAP32:
6658 case BUILT_IN_BSWAP64:
6659 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6660 if (target)
6661 return target;
6662 break;
6663
6664 CASE_INT_FN (BUILT_IN_FFS):
6665 target = expand_builtin_unop (target_mode, exp, target,
6666 subtarget, ffs_optab);
6667 if (target)
6668 return target;
6669 break;
6670
6671 CASE_INT_FN (BUILT_IN_CLZ):
6672 target = expand_builtin_unop (target_mode, exp, target,
6673 subtarget, clz_optab);
6674 if (target)
6675 return target;
6676 break;
6677
6678 CASE_INT_FN (BUILT_IN_CTZ):
6679 target = expand_builtin_unop (target_mode, exp, target,
6680 subtarget, ctz_optab);
6681 if (target)
6682 return target;
6683 break;
6684
6685 CASE_INT_FN (BUILT_IN_CLRSB):
6686 target = expand_builtin_unop (target_mode, exp, target,
6687 subtarget, clrsb_optab);
6688 if (target)
6689 return target;
6690 break;
6691
6692 CASE_INT_FN (BUILT_IN_POPCOUNT):
6693 target = expand_builtin_unop (target_mode, exp, target,
6694 subtarget, popcount_optab);
6695 if (target)
6696 return target;
6697 break;
6698
6699 CASE_INT_FN (BUILT_IN_PARITY):
6700 target = expand_builtin_unop (target_mode, exp, target,
6701 subtarget, parity_optab);
6702 if (target)
6703 return target;
6704 break;
6705
6706 case BUILT_IN_STRLEN:
6707 target = expand_builtin_strlen (exp, target, target_mode);
6708 if (target)
6709 return target;
6710 break;
6711
6712 case BUILT_IN_STRCAT:
6713 target = expand_builtin_strcat (exp, target);
6714 if (target)
6715 return target;
6716 break;
6717
6718 case BUILT_IN_STRCPY:
6719 target = expand_builtin_strcpy (exp, target);
6720 if (target)
6721 return target;
6722 break;
6723
6724 case BUILT_IN_STRNCAT:
6725 target = expand_builtin_strncat (exp, target);
6726 if (target)
6727 return target;
6728 break;
6729
6730 case BUILT_IN_STRNCPY:
6731 target = expand_builtin_strncpy (exp, target);
6732 if (target)
6733 return target;
6734 break;
6735
6736 case BUILT_IN_STPCPY:
6737 target = expand_builtin_stpcpy (exp, target, mode);
6738 if (target)
6739 return target;
6740 break;
6741
6742 case BUILT_IN_MEMCPY:
6743 target = expand_builtin_memcpy (exp, target);
6744 if (target)
6745 return target;
6746 break;
6747
6748 case BUILT_IN_MEMPCPY:
6749 target = expand_builtin_mempcpy (exp, target, mode);
6750 if (target)
6751 return target;
6752 break;
6753
6754 case BUILT_IN_MEMSET:
6755 target = expand_builtin_memset (exp, target, mode);
6756 if (target)
6757 return target;
6758 break;
6759
6760 case BUILT_IN_BZERO:
6761 target = expand_builtin_bzero (exp);
6762 if (target)
6763 return target;
6764 break;
6765
6766 case BUILT_IN_STRCMP:
6767 target = expand_builtin_strcmp (exp, target);
6768 if (target)
6769 return target;
6770 break;
6771
6772 case BUILT_IN_STRNCMP:
6773 target = expand_builtin_strncmp (exp, target, mode);
6774 if (target)
6775 return target;
6776 break;
6777
6778 case BUILT_IN_BCMP:
6779 case BUILT_IN_MEMCMP:
6780 case BUILT_IN_MEMCMP_EQ:
6781 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
6782 if (target)
6783 return target;
6784 if (fcode == BUILT_IN_MEMCMP_EQ)
6785 {
6786 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
6787 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
6788 }
6789 break;
6790
6791 case BUILT_IN_SETJMP:
6792 /* This should have been lowered to the builtins below. */
6793 gcc_unreachable ();
6794
6795 case BUILT_IN_SETJMP_SETUP:
6796 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6797 and the receiver label. */
6798 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6799 {
6800 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6801 VOIDmode, EXPAND_NORMAL);
6802 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6803 rtx_insn *label_r = label_rtx (label);
6804
6805 /* This is copied from the handling of non-local gotos. */
6806 expand_builtin_setjmp_setup (buf_addr, label_r);
6807 nonlocal_goto_handler_labels
6808 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6809 nonlocal_goto_handler_labels);
6810 /* ??? Do not let expand_label treat us as such since we would
6811 not want to be both on the list of non-local labels and on
6812 the list of forced labels. */
6813 FORCED_LABEL (label) = 0;
6814 return const0_rtx;
6815 }
6816 break;
6817
6818 case BUILT_IN_SETJMP_RECEIVER:
6819 /* __builtin_setjmp_receiver is passed the receiver label. */
6820 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6821 {
6822 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6823 rtx_insn *label_r = label_rtx (label);
6824
6825 expand_builtin_setjmp_receiver (label_r);
6826 return const0_rtx;
6827 }
6828 break;
6829
6830 /* __builtin_longjmp is passed a pointer to an array of five words.
6831 It's similar to the C library longjmp function but works with
6832 __builtin_setjmp above. */
6833 case BUILT_IN_LONGJMP:
6834 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6835 {
6836 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6837 VOIDmode, EXPAND_NORMAL);
6838 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6839
6840 if (value != const1_rtx)
6841 {
6842 error ("%<__builtin_longjmp%> second argument must be 1");
6843 return const0_rtx;
6844 }
6845
6846 expand_builtin_longjmp (buf_addr, value);
6847 return const0_rtx;
6848 }
6849 break;
6850
6851 case BUILT_IN_NONLOCAL_GOTO:
6852 target = expand_builtin_nonlocal_goto (exp);
6853 if (target)
6854 return target;
6855 break;
6856
6857 /* This updates the setjmp buffer that is its argument with the value
6858 of the current stack pointer. */
6859 case BUILT_IN_UPDATE_SETJMP_BUF:
6860 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6861 {
6862 rtx buf_addr
6863 = expand_normal (CALL_EXPR_ARG (exp, 0));
6864
6865 expand_builtin_update_setjmp_buf (buf_addr);
6866 return const0_rtx;
6867 }
6868 break;
6869
6870 case BUILT_IN_TRAP:
6871 expand_builtin_trap ();
6872 return const0_rtx;
6873
6874 case BUILT_IN_UNREACHABLE:
6875 expand_builtin_unreachable ();
6876 return const0_rtx;
6877
6878 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6879 case BUILT_IN_SIGNBITD32:
6880 case BUILT_IN_SIGNBITD64:
6881 case BUILT_IN_SIGNBITD128:
6882 target = expand_builtin_signbit (exp, target);
6883 if (target)
6884 return target;
6885 break;
6886
6887 /* Various hooks for the DWARF 2 __throw routine. */
6888 case BUILT_IN_UNWIND_INIT:
6889 expand_builtin_unwind_init ();
6890 return const0_rtx;
6891 case BUILT_IN_DWARF_CFA:
6892 return virtual_cfa_rtx;
6893 #ifdef DWARF2_UNWIND_INFO
6894 case BUILT_IN_DWARF_SP_COLUMN:
6895 return expand_builtin_dwarf_sp_column ();
6896 case BUILT_IN_INIT_DWARF_REG_SIZES:
6897 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6898 return const0_rtx;
6899 #endif
6900 case BUILT_IN_FROB_RETURN_ADDR:
6901 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6902 case BUILT_IN_EXTRACT_RETURN_ADDR:
6903 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6904 case BUILT_IN_EH_RETURN:
6905 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6906 CALL_EXPR_ARG (exp, 1));
6907 return const0_rtx;
6908 case BUILT_IN_EH_RETURN_DATA_REGNO:
6909 return expand_builtin_eh_return_data_regno (exp);
6910 case BUILT_IN_EXTEND_POINTER:
6911 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6912 case BUILT_IN_EH_POINTER:
6913 return expand_builtin_eh_pointer (exp);
6914 case BUILT_IN_EH_FILTER:
6915 return expand_builtin_eh_filter (exp);
6916 case BUILT_IN_EH_COPY_VALUES:
6917 return expand_builtin_eh_copy_values (exp);
6918
6919 case BUILT_IN_VA_START:
6920 return expand_builtin_va_start (exp);
6921 case BUILT_IN_VA_END:
6922 return expand_builtin_va_end (exp);
6923 case BUILT_IN_VA_COPY:
6924 return expand_builtin_va_copy (exp);
6925 case BUILT_IN_EXPECT:
6926 return expand_builtin_expect (exp, target);
6927 case BUILT_IN_ASSUME_ALIGNED:
6928 return expand_builtin_assume_aligned (exp, target);
6929 case BUILT_IN_PREFETCH:
6930 expand_builtin_prefetch (exp);
6931 return const0_rtx;
6932
6933 case BUILT_IN_INIT_TRAMPOLINE:
6934 return expand_builtin_init_trampoline (exp, true);
6935 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6936 return expand_builtin_init_trampoline (exp, false);
6937 case BUILT_IN_ADJUST_TRAMPOLINE:
6938 return expand_builtin_adjust_trampoline (exp);
6939
6940 case BUILT_IN_INIT_DESCRIPTOR:
6941 return expand_builtin_init_descriptor (exp);
6942 case BUILT_IN_ADJUST_DESCRIPTOR:
6943 return expand_builtin_adjust_descriptor (exp);
6944
6945 case BUILT_IN_FORK:
6946 case BUILT_IN_EXECL:
6947 case BUILT_IN_EXECV:
6948 case BUILT_IN_EXECLP:
6949 case BUILT_IN_EXECLE:
6950 case BUILT_IN_EXECVP:
6951 case BUILT_IN_EXECVE:
6952 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6953 if (target)
6954 return target;
6955 break;
6956
6957 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6958 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6959 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6960 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6961 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6962 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6963 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6964 if (target)
6965 return target;
6966 break;
6967
6968 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6969 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6970 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6971 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6972 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6973 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6974 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6975 if (target)
6976 return target;
6977 break;
6978
6979 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6980 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6981 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6982 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6983 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6984 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6985 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6986 if (target)
6987 return target;
6988 break;
6989
6990 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6991 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6992 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6993 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6994 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6995 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6996 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6997 if (target)
6998 return target;
6999 break;
7000
7001 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7002 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7003 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7004 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7005 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7006 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7007 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7008 if (target)
7009 return target;
7010 break;
7011
7012 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7013 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7014 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7015 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7016 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7017 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7018 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7019 if (target)
7020 return target;
7021 break;
7022
7023 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7024 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7025 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7026 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7027 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7028 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7029 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7030 if (target)
7031 return target;
7032 break;
7033
7034 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7035 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7036 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7037 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7038 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7039 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7040 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7041 if (target)
7042 return target;
7043 break;
7044
7045 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7046 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7047 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7048 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7049 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7050 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7051 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7052 if (target)
7053 return target;
7054 break;
7055
7056 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7057 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7058 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7059 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7060 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7061 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7062 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7063 if (target)
7064 return target;
7065 break;
7066
7067 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7068 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7069 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7070 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7071 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7072 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7073 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7074 if (target)
7075 return target;
7076 break;
7077
7078 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7079 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7080 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7081 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7082 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7083 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7084 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7085 if (target)
7086 return target;
7087 break;
7088
7089 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7090 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7091 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7092 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7093 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7094 if (mode == VOIDmode)
7095 mode = TYPE_MODE (boolean_type_node);
7096 if (!target || !register_operand (target, mode))
7097 target = gen_reg_rtx (mode);
7098
7099 mode = get_builtin_sync_mode
7100 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7101 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7102 if (target)
7103 return target;
7104 break;
7105
7106 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7107 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7108 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7109 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7110 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7111 mode = get_builtin_sync_mode
7112 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7113 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7114 if (target)
7115 return target;
7116 break;
7117
7118 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7119 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7120 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7121 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7122 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7123 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7124 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7125 if (target)
7126 return target;
7127 break;
7128
7129 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7130 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7131 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7132 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7133 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7134 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7135 expand_builtin_sync_lock_release (mode, exp);
7136 return const0_rtx;
7137
7138 case BUILT_IN_SYNC_SYNCHRONIZE:
7139 expand_builtin_sync_synchronize ();
7140 return const0_rtx;
7141
7142 case BUILT_IN_ATOMIC_EXCHANGE_1:
7143 case BUILT_IN_ATOMIC_EXCHANGE_2:
7144 case BUILT_IN_ATOMIC_EXCHANGE_4:
7145 case BUILT_IN_ATOMIC_EXCHANGE_8:
7146 case BUILT_IN_ATOMIC_EXCHANGE_16:
7147 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7148 target = expand_builtin_atomic_exchange (mode, exp, target);
7149 if (target)
7150 return target;
7151 break;
7152
7153 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7154 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7155 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7156 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7157 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7158 {
7159 unsigned int nargs, z;
7160 vec<tree, va_gc> *vec;
7161
7162 mode =
7163 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7164 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7165 if (target)
7166 return target;
7167
7168 /* If this is turned into an external library call, the weak parameter
7169 must be dropped to match the expected parameter list. */
7170 nargs = call_expr_nargs (exp);
7171 vec_alloc (vec, nargs - 1);
7172 for (z = 0; z < 3; z++)
7173 vec->quick_push (CALL_EXPR_ARG (exp, z));
7174 /* Skip the boolean weak parameter. */
7175 for (z = 4; z < 6; z++)
7176 vec->quick_push (CALL_EXPR_ARG (exp, z));
7177 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7178 break;
7179 }
7180
7181 case BUILT_IN_ATOMIC_LOAD_1:
7182 case BUILT_IN_ATOMIC_LOAD_2:
7183 case BUILT_IN_ATOMIC_LOAD_4:
7184 case BUILT_IN_ATOMIC_LOAD_8:
7185 case BUILT_IN_ATOMIC_LOAD_16:
7186 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7187 target = expand_builtin_atomic_load (mode, exp, target);
7188 if (target)
7189 return target;
7190 break;
7191
7192 case BUILT_IN_ATOMIC_STORE_1:
7193 case BUILT_IN_ATOMIC_STORE_2:
7194 case BUILT_IN_ATOMIC_STORE_4:
7195 case BUILT_IN_ATOMIC_STORE_8:
7196 case BUILT_IN_ATOMIC_STORE_16:
7197 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7198 target = expand_builtin_atomic_store (mode, exp);
7199 if (target)
7200 return const0_rtx;
7201 break;
7202
7203 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7204 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7205 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7206 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7207 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7208 {
7209 enum built_in_function lib;
7210 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7211 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7212 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7213 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7214 ignore, lib);
7215 if (target)
7216 return target;
7217 break;
7218 }
7219 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7220 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7221 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7222 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7223 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7224 {
7225 enum built_in_function lib;
7226 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7227 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7228 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7229 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7230 ignore, lib);
7231 if (target)
7232 return target;
7233 break;
7234 }
7235 case BUILT_IN_ATOMIC_AND_FETCH_1:
7236 case BUILT_IN_ATOMIC_AND_FETCH_2:
7237 case BUILT_IN_ATOMIC_AND_FETCH_4:
7238 case BUILT_IN_ATOMIC_AND_FETCH_8:
7239 case BUILT_IN_ATOMIC_AND_FETCH_16:
7240 {
7241 enum built_in_function lib;
7242 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7243 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7244 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7245 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7246 ignore, lib);
7247 if (target)
7248 return target;
7249 break;
7250 }
7251 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7252 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7253 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7254 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7255 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7256 {
7257 enum built_in_function lib;
7258 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7259 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7260 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7261 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7262 ignore, lib);
7263 if (target)
7264 return target;
7265 break;
7266 }
7267 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7268 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7269 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7270 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7271 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7272 {
7273 enum built_in_function lib;
7274 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7275 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7276 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7277 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7278 ignore, lib);
7279 if (target)
7280 return target;
7281 break;
7282 }
7283 case BUILT_IN_ATOMIC_OR_FETCH_1:
7284 case BUILT_IN_ATOMIC_OR_FETCH_2:
7285 case BUILT_IN_ATOMIC_OR_FETCH_4:
7286 case BUILT_IN_ATOMIC_OR_FETCH_8:
7287 case BUILT_IN_ATOMIC_OR_FETCH_16:
7288 {
7289 enum built_in_function lib;
7290 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7291 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7292 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7293 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7294 ignore, lib);
7295 if (target)
7296 return target;
7297 break;
7298 }
7299 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7300 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7301 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7302 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7303 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7304 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7305 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7306 ignore, BUILT_IN_NONE);
7307 if (target)
7308 return target;
7309 break;
7310
7311 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7312 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7313 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7314 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7315 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7316 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7317 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7318 ignore, BUILT_IN_NONE);
7319 if (target)
7320 return target;
7321 break;
7322
7323 case BUILT_IN_ATOMIC_FETCH_AND_1:
7324 case BUILT_IN_ATOMIC_FETCH_AND_2:
7325 case BUILT_IN_ATOMIC_FETCH_AND_4:
7326 case BUILT_IN_ATOMIC_FETCH_AND_8:
7327 case BUILT_IN_ATOMIC_FETCH_AND_16:
7328 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7329 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7330 ignore, BUILT_IN_NONE);
7331 if (target)
7332 return target;
7333 break;
7334
7335 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7336 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7337 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7338 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7339 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7340 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7341 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7342 ignore, BUILT_IN_NONE);
7343 if (target)
7344 return target;
7345 break;
7346
7347 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7348 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7349 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7350 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7351 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7352 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7353 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7354 ignore, BUILT_IN_NONE);
7355 if (target)
7356 return target;
7357 break;
7358
7359 case BUILT_IN_ATOMIC_FETCH_OR_1:
7360 case BUILT_IN_ATOMIC_FETCH_OR_2:
7361 case BUILT_IN_ATOMIC_FETCH_OR_4:
7362 case BUILT_IN_ATOMIC_FETCH_OR_8:
7363 case BUILT_IN_ATOMIC_FETCH_OR_16:
7364 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7365 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7366 ignore, BUILT_IN_NONE);
7367 if (target)
7368 return target;
7369 break;
7370
7371 case BUILT_IN_ATOMIC_TEST_AND_SET:
7372 return expand_builtin_atomic_test_and_set (exp, target);
7373
7374 case BUILT_IN_ATOMIC_CLEAR:
7375 return expand_builtin_atomic_clear (exp);
7376
7377 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
7378 return expand_builtin_atomic_always_lock_free (exp);
7379
7380 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
7381 target = expand_builtin_atomic_is_lock_free (exp);
7382 if (target)
7383 return target;
7384 break;
7385
7386 case BUILT_IN_ATOMIC_THREAD_FENCE:
7387 expand_builtin_atomic_thread_fence (exp);
7388 return const0_rtx;
7389
7390 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
7391 expand_builtin_atomic_signal_fence (exp);
7392 return const0_rtx;
7393
7394 case BUILT_IN_OBJECT_SIZE:
7395 return expand_builtin_object_size (exp);
7396
7397 case BUILT_IN_MEMCPY_CHK:
7398 case BUILT_IN_MEMPCPY_CHK:
7399 case BUILT_IN_MEMMOVE_CHK:
7400 case BUILT_IN_MEMSET_CHK:
7401 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7402 if (target)
7403 return target;
7404 break;
7405
7406 case BUILT_IN_STRCPY_CHK:
7407 case BUILT_IN_STPCPY_CHK:
7408 case BUILT_IN_STRNCPY_CHK:
7409 case BUILT_IN_STPNCPY_CHK:
7410 case BUILT_IN_STRCAT_CHK:
7411 case BUILT_IN_STRNCAT_CHK:
7412 case BUILT_IN_SNPRINTF_CHK:
7413 case BUILT_IN_VSNPRINTF_CHK:
7414 maybe_emit_chk_warning (exp, fcode);
7415 break;
7416
7417 case BUILT_IN_SPRINTF_CHK:
7418 case BUILT_IN_VSPRINTF_CHK:
7419 maybe_emit_sprintf_chk_warning (exp, fcode);
7420 break;
7421
7422 case BUILT_IN_FREE:
7423 if (warn_free_nonheap_object)
7424 maybe_emit_free_warning (exp);
7425 break;
7426
7427 case BUILT_IN_THREAD_POINTER:
7428 return expand_builtin_thread_pointer (exp, target);
7429
7430 case BUILT_IN_SET_THREAD_POINTER:
7431 expand_builtin_set_thread_pointer (exp);
7432 return const0_rtx;
7433
7434 case BUILT_IN_CILK_DETACH:
7435 expand_builtin_cilk_detach (exp);
7436 return const0_rtx;
7437
7438 case BUILT_IN_CILK_POP_FRAME:
7439 expand_builtin_cilk_pop_frame (exp);
7440 return const0_rtx;
7441
7442 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7443 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7444 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7445 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7446 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7447 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7448 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7449 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7450 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7451 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7452 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7453 /* We allow user CHKP builtins if Pointer Bounds
7454 Checker is off. */
7455 if (!chkp_function_instrumented_p (current_function_decl))
7456 {
7457 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7458 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7459 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7460 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7461 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7462 return expand_normal (CALL_EXPR_ARG (exp, 0));
7463 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7464 return expand_normal (size_zero_node);
7465 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7466 return expand_normal (size_int (-1));
7467 else
7468 return const0_rtx;
7469 }
7470 /* FALLTHROUGH */
7471
7472 case BUILT_IN_CHKP_BNDMK:
7473 case BUILT_IN_CHKP_BNDSTX:
7474 case BUILT_IN_CHKP_BNDCL:
7475 case BUILT_IN_CHKP_BNDCU:
7476 case BUILT_IN_CHKP_BNDLDX:
7477 case BUILT_IN_CHKP_BNDRET:
7478 case BUILT_IN_CHKP_INTERSECT:
7479 case BUILT_IN_CHKP_NARROW:
7480 case BUILT_IN_CHKP_EXTRACT_LOWER:
7481 case BUILT_IN_CHKP_EXTRACT_UPPER:
7482 /* Software implementation of Pointer Bounds Checker is NYI.
7483 Target support is required. */
7484 error ("Your target platform does not support -fcheck-pointer-bounds");
7485 break;
7486
7487 case BUILT_IN_ACC_ON_DEVICE:
7488 /* Do library call, if we failed to expand the builtin when
7489 folding. */
7490 break;
7491
7492 default: /* just do library call, if unknown builtin */
7493 break;
7494 }
7495
7496 /* The switch statement above can drop through to cause the function
7497 to be called normally. */
7498 return expand_call (exp, target, ignore);
7499 }
7500
7501 /* Similar to expand_builtin but is used for instrumented calls. */
7502
7503 rtx
7504 expand_builtin_with_bounds (tree exp, rtx target,
7505 rtx subtarget ATTRIBUTE_UNUSED,
7506 machine_mode mode, int ignore)
7507 {
7508 tree fndecl = get_callee_fndecl (exp);
7509 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7510
7511 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7512
7513 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7514 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7515
7516 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7517 && fcode < END_CHKP_BUILTINS);
7518
7519 switch (fcode)
7520 {
7521 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7522 target = expand_builtin_memcpy_with_bounds (exp, target);
7523 if (target)
7524 return target;
7525 break;
7526
7527 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7528 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7529 if (target)
7530 return target;
7531 break;
7532
7533 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7534 target = expand_builtin_memset_with_bounds (exp, target, mode);
7535 if (target)
7536 return target;
7537 break;
7538
7539 default:
7540 break;
7541 }
7542
7543 /* The switch statement above can drop through to cause the function
7544 to be called normally. */
7545 return expand_call (exp, target, ignore);
7546 }
7547
7548 /* Determine whether a tree node represents a call to a built-in
7549 function. If the tree T is a call to a built-in function with
7550 the right number of arguments of the appropriate types, return
7551 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7552 Otherwise the return value is END_BUILTINS. */
7553
7554 enum built_in_function
7555 builtin_mathfn_code (const_tree t)
7556 {
7557 const_tree fndecl, arg, parmlist;
7558 const_tree argtype, parmtype;
7559 const_call_expr_arg_iterator iter;
7560
7561 if (TREE_CODE (t) != CALL_EXPR
7562 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7563 return END_BUILTINS;
7564
7565 fndecl = get_callee_fndecl (t);
7566 if (fndecl == NULL_TREE
7567 || TREE_CODE (fndecl) != FUNCTION_DECL
7568 || ! DECL_BUILT_IN (fndecl)
7569 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7570 return END_BUILTINS;
7571
7572 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7573 init_const_call_expr_arg_iterator (t, &iter);
7574 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7575 {
7576 /* If a function doesn't take a variable number of arguments,
7577 the last element in the list will have type `void'. */
7578 parmtype = TREE_VALUE (parmlist);
7579 if (VOID_TYPE_P (parmtype))
7580 {
7581 if (more_const_call_expr_args_p (&iter))
7582 return END_BUILTINS;
7583 return DECL_FUNCTION_CODE (fndecl);
7584 }
7585
7586 if (! more_const_call_expr_args_p (&iter))
7587 return END_BUILTINS;
7588
7589 arg = next_const_call_expr_arg (&iter);
7590 argtype = TREE_TYPE (arg);
7591
7592 if (SCALAR_FLOAT_TYPE_P (parmtype))
7593 {
7594 if (! SCALAR_FLOAT_TYPE_P (argtype))
7595 return END_BUILTINS;
7596 }
7597 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7598 {
7599 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7600 return END_BUILTINS;
7601 }
7602 else if (POINTER_TYPE_P (parmtype))
7603 {
7604 if (! POINTER_TYPE_P (argtype))
7605 return END_BUILTINS;
7606 }
7607 else if (INTEGRAL_TYPE_P (parmtype))
7608 {
7609 if (! INTEGRAL_TYPE_P (argtype))
7610 return END_BUILTINS;
7611 }
7612 else
7613 return END_BUILTINS;
7614 }
7615
7616 /* Variable-length argument list. */
7617 return DECL_FUNCTION_CODE (fndecl);
7618 }
7619
7620 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7621 evaluate to a constant. */
7622
7623 static tree
7624 fold_builtin_constant_p (tree arg)
7625 {
7626 /* We return 1 for a numeric type that's known to be a constant
7627 value at compile-time or for an aggregate type that's a
7628 literal constant. */
7629 STRIP_NOPS (arg);
7630
7631 /* If we know this is a constant, emit the constant of one. */
7632 if (CONSTANT_CLASS_P (arg)
7633 || (TREE_CODE (arg) == CONSTRUCTOR
7634 && TREE_CONSTANT (arg)))
7635 return integer_one_node;
7636 if (TREE_CODE (arg) == ADDR_EXPR)
7637 {
7638 tree op = TREE_OPERAND (arg, 0);
7639 if (TREE_CODE (op) == STRING_CST
7640 || (TREE_CODE (op) == ARRAY_REF
7641 && integer_zerop (TREE_OPERAND (op, 1))
7642 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7643 return integer_one_node;
7644 }
7645
7646 /* If this expression has side effects, show we don't know it to be a
7647 constant. Likewise if it's a pointer or aggregate type since in
7648 those case we only want literals, since those are only optimized
7649 when generating RTL, not later.
7650 And finally, if we are compiling an initializer, not code, we
7651 need to return a definite result now; there's not going to be any
7652 more optimization done. */
7653 if (TREE_SIDE_EFFECTS (arg)
7654 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7655 || POINTER_TYPE_P (TREE_TYPE (arg))
7656 || cfun == 0
7657 || folding_initializer
7658 || force_folding_builtin_constant_p)
7659 return integer_zero_node;
7660
7661 return NULL_TREE;
7662 }
7663
7664 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7665 return it as a truthvalue. */
7666
7667 static tree
7668 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7669 tree predictor)
7670 {
7671 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7672
7673 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7674 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7675 ret_type = TREE_TYPE (TREE_TYPE (fn));
7676 pred_type = TREE_VALUE (arg_types);
7677 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7678
7679 pred = fold_convert_loc (loc, pred_type, pred);
7680 expected = fold_convert_loc (loc, expected_type, expected);
7681 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7682 predictor);
7683
7684 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7685 build_int_cst (ret_type, 0));
7686 }
7687
7688 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7689 NULL_TREE if no simplification is possible. */
7690
7691 tree
7692 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7693 {
7694 tree inner, fndecl, inner_arg0;
7695 enum tree_code code;
7696
7697 /* Distribute the expected value over short-circuiting operators.
7698 See through the cast from truthvalue_type_node to long. */
7699 inner_arg0 = arg0;
7700 while (CONVERT_EXPR_P (inner_arg0)
7701 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7702 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7703 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7704
7705 /* If this is a builtin_expect within a builtin_expect keep the
7706 inner one. See through a comparison against a constant. It
7707 might have been added to create a thruthvalue. */
7708 inner = inner_arg0;
7709
7710 if (COMPARISON_CLASS_P (inner)
7711 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7712 inner = TREE_OPERAND (inner, 0);
7713
7714 if (TREE_CODE (inner) == CALL_EXPR
7715 && (fndecl = get_callee_fndecl (inner))
7716 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7717 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7718 return arg0;
7719
7720 inner = inner_arg0;
7721 code = TREE_CODE (inner);
7722 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7723 {
7724 tree op0 = TREE_OPERAND (inner, 0);
7725 tree op1 = TREE_OPERAND (inner, 1);
7726
7727 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7728 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7729 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7730
7731 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7732 }
7733
7734 /* If the argument isn't invariant then there's nothing else we can do. */
7735 if (!TREE_CONSTANT (inner_arg0))
7736 return NULL_TREE;
7737
7738 /* If we expect that a comparison against the argument will fold to
7739 a constant return the constant. In practice, this means a true
7740 constant or the address of a non-weak symbol. */
7741 inner = inner_arg0;
7742 STRIP_NOPS (inner);
7743 if (TREE_CODE (inner) == ADDR_EXPR)
7744 {
7745 do
7746 {
7747 inner = TREE_OPERAND (inner, 0);
7748 }
7749 while (TREE_CODE (inner) == COMPONENT_REF
7750 || TREE_CODE (inner) == ARRAY_REF);
7751 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
7752 return NULL_TREE;
7753 }
7754
7755 /* Otherwise, ARG0 already has the proper type for the return value. */
7756 return arg0;
7757 }
7758
7759 /* Fold a call to __builtin_classify_type with argument ARG. */
7760
7761 static tree
7762 fold_builtin_classify_type (tree arg)
7763 {
7764 if (arg == 0)
7765 return build_int_cst (integer_type_node, no_type_class);
7766
7767 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7768 }
7769
7770 /* Fold a call to __builtin_strlen with argument ARG. */
7771
7772 static tree
7773 fold_builtin_strlen (location_t loc, tree type, tree arg)
7774 {
7775 if (!validate_arg (arg, POINTER_TYPE))
7776 return NULL_TREE;
7777 else
7778 {
7779 tree len = c_strlen (arg, 0);
7780
7781 if (len)
7782 return fold_convert_loc (loc, type, len);
7783
7784 return NULL_TREE;
7785 }
7786 }
7787
7788 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7789
7790 static tree
7791 fold_builtin_inf (location_t loc, tree type, int warn)
7792 {
7793 REAL_VALUE_TYPE real;
7794
7795 /* __builtin_inff is intended to be usable to define INFINITY on all
7796 targets. If an infinity is not available, INFINITY expands "to a
7797 positive constant of type float that overflows at translation
7798 time", footnote "In this case, using INFINITY will violate the
7799 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7800 Thus we pedwarn to ensure this constraint violation is
7801 diagnosed. */
7802 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7803 pedwarn (loc, 0, "target format does not support infinity");
7804
7805 real_inf (&real);
7806 return build_real (type, real);
7807 }
7808
7809 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7810 NULL_TREE if no simplification can be made. */
7811
7812 static tree
7813 fold_builtin_sincos (location_t loc,
7814 tree arg0, tree arg1, tree arg2)
7815 {
7816 tree type;
7817 tree fndecl, call = NULL_TREE;
7818
7819 if (!validate_arg (arg0, REAL_TYPE)
7820 || !validate_arg (arg1, POINTER_TYPE)
7821 || !validate_arg (arg2, POINTER_TYPE))
7822 return NULL_TREE;
7823
7824 type = TREE_TYPE (arg0);
7825
7826 /* Calculate the result when the argument is a constant. */
7827 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
7828 if (fn == END_BUILTINS)
7829 return NULL_TREE;
7830
7831 /* Canonicalize sincos to cexpi. */
7832 if (TREE_CODE (arg0) == REAL_CST)
7833 {
7834 tree complex_type = build_complex_type (type);
7835 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
7836 }
7837 if (!call)
7838 {
7839 if (!targetm.libc_has_function (function_c99_math_complex)
7840 || !builtin_decl_implicit_p (fn))
7841 return NULL_TREE;
7842 fndecl = builtin_decl_explicit (fn);
7843 call = build_call_expr_loc (loc, fndecl, 1, arg0);
7844 call = builtin_save_expr (call);
7845 }
7846
7847 return build2 (COMPOUND_EXPR, void_type_node,
7848 build2 (MODIFY_EXPR, void_type_node,
7849 build_fold_indirect_ref_loc (loc, arg1),
7850 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
7851 build2 (MODIFY_EXPR, void_type_node,
7852 build_fold_indirect_ref_loc (loc, arg2),
7853 fold_build1_loc (loc, REALPART_EXPR, type, call)));
7854 }
7855
7856 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
7857 Return NULL_TREE if no simplification can be made. */
7858
7859 static tree
7860 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
7861 {
7862 if (!validate_arg (arg1, POINTER_TYPE)
7863 || !validate_arg (arg2, POINTER_TYPE)
7864 || !validate_arg (len, INTEGER_TYPE))
7865 return NULL_TREE;
7866
7867 /* If the LEN parameter is zero, return zero. */
7868 if (integer_zerop (len))
7869 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
7870 arg1, arg2);
7871
7872 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7873 if (operand_equal_p (arg1, arg2, 0))
7874 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
7875
7876 /* If len parameter is one, return an expression corresponding to
7877 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7878 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
7879 {
7880 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7881 tree cst_uchar_ptr_node
7882 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7883
7884 tree ind1
7885 = fold_convert_loc (loc, integer_type_node,
7886 build1 (INDIRECT_REF, cst_uchar_node,
7887 fold_convert_loc (loc,
7888 cst_uchar_ptr_node,
7889 arg1)));
7890 tree ind2
7891 = fold_convert_loc (loc, integer_type_node,
7892 build1 (INDIRECT_REF, cst_uchar_node,
7893 fold_convert_loc (loc,
7894 cst_uchar_ptr_node,
7895 arg2)));
7896 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
7897 }
7898
7899 return NULL_TREE;
7900 }
7901
7902 /* Fold a call to builtin isascii with argument ARG. */
7903
7904 static tree
7905 fold_builtin_isascii (location_t loc, tree arg)
7906 {
7907 if (!validate_arg (arg, INTEGER_TYPE))
7908 return NULL_TREE;
7909 else
7910 {
7911 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7912 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
7913 build_int_cst (integer_type_node,
7914 ~ (unsigned HOST_WIDE_INT) 0x7f));
7915 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
7916 arg, integer_zero_node);
7917 }
7918 }
7919
7920 /* Fold a call to builtin toascii with argument ARG. */
7921
7922 static tree
7923 fold_builtin_toascii (location_t loc, tree arg)
7924 {
7925 if (!validate_arg (arg, INTEGER_TYPE))
7926 return NULL_TREE;
7927
7928 /* Transform toascii(c) -> (c & 0x7f). */
7929 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
7930 build_int_cst (integer_type_node, 0x7f));
7931 }
7932
7933 /* Fold a call to builtin isdigit with argument ARG. */
7934
7935 static tree
7936 fold_builtin_isdigit (location_t loc, tree arg)
7937 {
7938 if (!validate_arg (arg, INTEGER_TYPE))
7939 return NULL_TREE;
7940 else
7941 {
7942 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7943 /* According to the C standard, isdigit is unaffected by locale.
7944 However, it definitely is affected by the target character set. */
7945 unsigned HOST_WIDE_INT target_digit0
7946 = lang_hooks.to_target_charset ('0');
7947
7948 if (target_digit0 == 0)
7949 return NULL_TREE;
7950
7951 arg = fold_convert_loc (loc, unsigned_type_node, arg);
7952 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
7953 build_int_cst (unsigned_type_node, target_digit0));
7954 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
7955 build_int_cst (unsigned_type_node, 9));
7956 }
7957 }
7958
7959 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
7960
7961 static tree
7962 fold_builtin_fabs (location_t loc, tree arg, tree type)
7963 {
7964 if (!validate_arg (arg, REAL_TYPE))
7965 return NULL_TREE;
7966
7967 arg = fold_convert_loc (loc, type, arg);
7968 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7969 }
7970
7971 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
7972
7973 static tree
7974 fold_builtin_abs (location_t loc, tree arg, tree type)
7975 {
7976 if (!validate_arg (arg, INTEGER_TYPE))
7977 return NULL_TREE;
7978
7979 arg = fold_convert_loc (loc, type, arg);
7980 return fold_build1_loc (loc, ABS_EXPR, type, arg);
7981 }
7982
7983 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
7984
7985 static tree
7986 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
7987 {
7988 /* ??? Only expand to FMA_EXPR if it's directly supported. */
7989 if (validate_arg (arg0, REAL_TYPE)
7990 && validate_arg (arg1, REAL_TYPE)
7991 && validate_arg (arg2, REAL_TYPE)
7992 && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
7993 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
7994
7995 return NULL_TREE;
7996 }
7997
7998 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
7999
8000 static tree
8001 fold_builtin_carg (location_t loc, tree arg, tree type)
8002 {
8003 if (validate_arg (arg, COMPLEX_TYPE)
8004 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8005 {
8006 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8007
8008 if (atan2_fn)
8009 {
8010 tree new_arg = builtin_save_expr (arg);
8011 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8012 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8013 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8014 }
8015 }
8016
8017 return NULL_TREE;
8018 }
8019
8020 /* Fold a call to builtin frexp, we can assume the base is 2. */
8021
8022 static tree
8023 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8024 {
8025 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8026 return NULL_TREE;
8027
8028 STRIP_NOPS (arg0);
8029
8030 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8031 return NULL_TREE;
8032
8033 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8034
8035 /* Proceed if a valid pointer type was passed in. */
8036 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8037 {
8038 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8039 tree frac, exp;
8040
8041 switch (value->cl)
8042 {
8043 case rvc_zero:
8044 /* For +-0, return (*exp = 0, +-0). */
8045 exp = integer_zero_node;
8046 frac = arg0;
8047 break;
8048 case rvc_nan:
8049 case rvc_inf:
8050 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8051 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8052 case rvc_normal:
8053 {
8054 /* Since the frexp function always expects base 2, and in
8055 GCC normalized significands are already in the range
8056 [0.5, 1.0), we have exactly what frexp wants. */
8057 REAL_VALUE_TYPE frac_rvt = *value;
8058 SET_REAL_EXP (&frac_rvt, 0);
8059 frac = build_real (rettype, frac_rvt);
8060 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8061 }
8062 break;
8063 default:
8064 gcc_unreachable ();
8065 }
8066
8067 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8068 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8069 TREE_SIDE_EFFECTS (arg1) = 1;
8070 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8071 }
8072
8073 return NULL_TREE;
8074 }
8075
8076 /* Fold a call to builtin modf. */
8077
8078 static tree
8079 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8080 {
8081 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8082 return NULL_TREE;
8083
8084 STRIP_NOPS (arg0);
8085
8086 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8087 return NULL_TREE;
8088
8089 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8090
8091 /* Proceed if a valid pointer type was passed in. */
8092 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8093 {
8094 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8095 REAL_VALUE_TYPE trunc, frac;
8096
8097 switch (value->cl)
8098 {
8099 case rvc_nan:
8100 case rvc_zero:
8101 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8102 trunc = frac = *value;
8103 break;
8104 case rvc_inf:
8105 /* For +-Inf, return (*arg1 = arg0, +-0). */
8106 frac = dconst0;
8107 frac.sign = value->sign;
8108 trunc = *value;
8109 break;
8110 case rvc_normal:
8111 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8112 real_trunc (&trunc, VOIDmode, value);
8113 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8114 /* If the original number was negative and already
8115 integral, then the fractional part is -0.0. */
8116 if (value->sign && frac.cl == rvc_zero)
8117 frac.sign = value->sign;
8118 break;
8119 }
8120
8121 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8122 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8123 build_real (rettype, trunc));
8124 TREE_SIDE_EFFECTS (arg1) = 1;
8125 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8126 build_real (rettype, frac));
8127 }
8128
8129 return NULL_TREE;
8130 }
8131
8132 /* Given a location LOC, an interclass builtin function decl FNDECL
8133 and its single argument ARG, return an folded expression computing
8134 the same, or NULL_TREE if we either couldn't or didn't want to fold
8135 (the latter happen if there's an RTL instruction available). */
8136
8137 static tree
8138 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8139 {
8140 machine_mode mode;
8141
8142 if (!validate_arg (arg, REAL_TYPE))
8143 return NULL_TREE;
8144
8145 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8146 return NULL_TREE;
8147
8148 mode = TYPE_MODE (TREE_TYPE (arg));
8149
8150 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8151
8152 /* If there is no optab, try generic code. */
8153 switch (DECL_FUNCTION_CODE (fndecl))
8154 {
8155 tree result;
8156
8157 CASE_FLT_FN (BUILT_IN_ISINF):
8158 {
8159 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8160 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8161 tree type = TREE_TYPE (arg);
8162 REAL_VALUE_TYPE r;
8163 char buf[128];
8164
8165 if (is_ibm_extended)
8166 {
8167 /* NaN and Inf are encoded in the high-order double value
8168 only. The low-order value is not significant. */
8169 type = double_type_node;
8170 mode = DFmode;
8171 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8172 }
8173 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8174 real_from_string (&r, buf);
8175 result = build_call_expr (isgr_fn, 2,
8176 fold_build1_loc (loc, ABS_EXPR, type, arg),
8177 build_real (type, r));
8178 return result;
8179 }
8180 CASE_FLT_FN (BUILT_IN_FINITE):
8181 case BUILT_IN_ISFINITE:
8182 {
8183 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8184 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8185 tree type = TREE_TYPE (arg);
8186 REAL_VALUE_TYPE r;
8187 char buf[128];
8188
8189 if (is_ibm_extended)
8190 {
8191 /* NaN and Inf are encoded in the high-order double value
8192 only. The low-order value is not significant. */
8193 type = double_type_node;
8194 mode = DFmode;
8195 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8196 }
8197 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8198 real_from_string (&r, buf);
8199 result = build_call_expr (isle_fn, 2,
8200 fold_build1_loc (loc, ABS_EXPR, type, arg),
8201 build_real (type, r));
8202 /*result = fold_build2_loc (loc, UNGT_EXPR,
8203 TREE_TYPE (TREE_TYPE (fndecl)),
8204 fold_build1_loc (loc, ABS_EXPR, type, arg),
8205 build_real (type, r));
8206 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8207 TREE_TYPE (TREE_TYPE (fndecl)),
8208 result);*/
8209 return result;
8210 }
8211 case BUILT_IN_ISNORMAL:
8212 {
8213 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8214 islessequal(fabs(x),DBL_MAX). */
8215 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8216 tree type = TREE_TYPE (arg);
8217 tree orig_arg, max_exp, min_exp;
8218 machine_mode orig_mode = mode;
8219 REAL_VALUE_TYPE rmax, rmin;
8220 char buf[128];
8221
8222 orig_arg = arg = builtin_save_expr (arg);
8223 if (is_ibm_extended)
8224 {
8225 /* Use double to test the normal range of IBM extended
8226 precision. Emin for IBM extended precision is
8227 different to emin for IEEE double, being 53 higher
8228 since the low double exponent is at least 53 lower
8229 than the high double exponent. */
8230 type = double_type_node;
8231 mode = DFmode;
8232 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8233 }
8234 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8235
8236 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8237 real_from_string (&rmax, buf);
8238 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8239 real_from_string (&rmin, buf);
8240 max_exp = build_real (type, rmax);
8241 min_exp = build_real (type, rmin);
8242
8243 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8244 if (is_ibm_extended)
8245 {
8246 /* Testing the high end of the range is done just using
8247 the high double, using the same test as isfinite().
8248 For the subnormal end of the range we first test the
8249 high double, then if its magnitude is equal to the
8250 limit of 0x1p-969, we test whether the low double is
8251 non-zero and opposite sign to the high double. */
8252 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8253 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8254 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8255 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8256 arg, min_exp);
8257 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8258 complex_double_type_node, orig_arg);
8259 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8260 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8261 tree zero = build_real (type, dconst0);
8262 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8263 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8264 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8265 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8266 fold_build3 (COND_EXPR,
8267 integer_type_node,
8268 hilt, logt, lolt));
8269 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8270 eq_min, ok_lo);
8271 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8272 gt_min, eq_min);
8273 }
8274 else
8275 {
8276 tree const isge_fn
8277 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8278 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8279 }
8280 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8281 max_exp, min_exp);
8282 return result;
8283 }
8284 default:
8285 break;
8286 }
8287
8288 return NULL_TREE;
8289 }
8290
8291 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8292 ARG is the argument for the call. */
8293
8294 static tree
8295 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8296 {
8297 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8298
8299 if (!validate_arg (arg, REAL_TYPE))
8300 return NULL_TREE;
8301
8302 switch (builtin_index)
8303 {
8304 case BUILT_IN_ISINF:
8305 if (!HONOR_INFINITIES (arg))
8306 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8307
8308 return NULL_TREE;
8309
8310 case BUILT_IN_ISINF_SIGN:
8311 {
8312 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8313 /* In a boolean context, GCC will fold the inner COND_EXPR to
8314 1. So e.g. "if (isinf_sign(x))" would be folded to just
8315 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8316 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8317 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8318 tree tmp = NULL_TREE;
8319
8320 arg = builtin_save_expr (arg);
8321
8322 if (signbit_fn && isinf_fn)
8323 {
8324 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8325 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8326
8327 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8328 signbit_call, integer_zero_node);
8329 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8330 isinf_call, integer_zero_node);
8331
8332 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8333 integer_minus_one_node, integer_one_node);
8334 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8335 isinf_call, tmp,
8336 integer_zero_node);
8337 }
8338
8339 return tmp;
8340 }
8341
8342 case BUILT_IN_ISFINITE:
8343 if (!HONOR_NANS (arg)
8344 && !HONOR_INFINITIES (arg))
8345 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8346
8347 return NULL_TREE;
8348
8349 case BUILT_IN_ISNAN:
8350 if (!HONOR_NANS (arg))
8351 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8352
8353 {
8354 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8355 if (is_ibm_extended)
8356 {
8357 /* NaN and Inf are encoded in the high-order double value
8358 only. The low-order value is not significant. */
8359 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8360 }
8361 }
8362 arg = builtin_save_expr (arg);
8363 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8364
8365 default:
8366 gcc_unreachable ();
8367 }
8368 }
8369
8370 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8371 This builtin will generate code to return the appropriate floating
8372 point classification depending on the value of the floating point
8373 number passed in. The possible return values must be supplied as
8374 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8375 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8376 one floating point argument which is "type generic". */
8377
8378 static tree
8379 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8380 {
8381 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8382 arg, type, res, tmp;
8383 machine_mode mode;
8384 REAL_VALUE_TYPE r;
8385 char buf[128];
8386
8387 /* Verify the required arguments in the original call. */
8388 if (nargs != 6
8389 || !validate_arg (args[0], INTEGER_TYPE)
8390 || !validate_arg (args[1], INTEGER_TYPE)
8391 || !validate_arg (args[2], INTEGER_TYPE)
8392 || !validate_arg (args[3], INTEGER_TYPE)
8393 || !validate_arg (args[4], INTEGER_TYPE)
8394 || !validate_arg (args[5], REAL_TYPE))
8395 return NULL_TREE;
8396
8397 fp_nan = args[0];
8398 fp_infinite = args[1];
8399 fp_normal = args[2];
8400 fp_subnormal = args[3];
8401 fp_zero = args[4];
8402 arg = args[5];
8403 type = TREE_TYPE (arg);
8404 mode = TYPE_MODE (type);
8405 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8406
8407 /* fpclassify(x) ->
8408 isnan(x) ? FP_NAN :
8409 (fabs(x) == Inf ? FP_INFINITE :
8410 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8411 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8412
8413 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8414 build_real (type, dconst0));
8415 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8416 tmp, fp_zero, fp_subnormal);
8417
8418 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8419 real_from_string (&r, buf);
8420 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8421 arg, build_real (type, r));
8422 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8423
8424 if (HONOR_INFINITIES (mode))
8425 {
8426 real_inf (&r);
8427 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8428 build_real (type, r));
8429 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8430 fp_infinite, res);
8431 }
8432
8433 if (HONOR_NANS (mode))
8434 {
8435 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8436 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8437 }
8438
8439 return res;
8440 }
8441
8442 /* Fold a call to an unordered comparison function such as
8443 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8444 being called and ARG0 and ARG1 are the arguments for the call.
8445 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8446 the opposite of the desired result. UNORDERED_CODE is used
8447 for modes that can hold NaNs and ORDERED_CODE is used for
8448 the rest. */
8449
8450 static tree
8451 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
8452 enum tree_code unordered_code,
8453 enum tree_code ordered_code)
8454 {
8455 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8456 enum tree_code code;
8457 tree type0, type1;
8458 enum tree_code code0, code1;
8459 tree cmp_type = NULL_TREE;
8460
8461 type0 = TREE_TYPE (arg0);
8462 type1 = TREE_TYPE (arg1);
8463
8464 code0 = TREE_CODE (type0);
8465 code1 = TREE_CODE (type1);
8466
8467 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8468 /* Choose the wider of two real types. */
8469 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8470 ? type0 : type1;
8471 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8472 cmp_type = type0;
8473 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8474 cmp_type = type1;
8475
8476 arg0 = fold_convert_loc (loc, cmp_type, arg0);
8477 arg1 = fold_convert_loc (loc, cmp_type, arg1);
8478
8479 if (unordered_code == UNORDERED_EXPR)
8480 {
8481 if (!HONOR_NANS (arg0))
8482 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
8483 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
8484 }
8485
8486 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
8487 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
8488 fold_build2_loc (loc, code, type, arg0, arg1));
8489 }
8490
8491 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8492 arithmetics if it can never overflow, or into internal functions that
8493 return both result of arithmetics and overflowed boolean flag in
8494 a complex integer result, or some other check for overflow.
8495 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
8496 checking part of that. */
8497
8498 static tree
8499 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
8500 tree arg0, tree arg1, tree arg2)
8501 {
8502 enum internal_fn ifn = IFN_LAST;
8503 /* The code of the expression corresponding to the type-generic
8504 built-in, or ERROR_MARK for the type-specific ones. */
8505 enum tree_code opcode = ERROR_MARK;
8506 bool ovf_only = false;
8507
8508 switch (fcode)
8509 {
8510 case BUILT_IN_ADD_OVERFLOW_P:
8511 ovf_only = true;
8512 /* FALLTHRU */
8513 case BUILT_IN_ADD_OVERFLOW:
8514 opcode = PLUS_EXPR;
8515 /* FALLTHRU */
8516 case BUILT_IN_SADD_OVERFLOW:
8517 case BUILT_IN_SADDL_OVERFLOW:
8518 case BUILT_IN_SADDLL_OVERFLOW:
8519 case BUILT_IN_UADD_OVERFLOW:
8520 case BUILT_IN_UADDL_OVERFLOW:
8521 case BUILT_IN_UADDLL_OVERFLOW:
8522 ifn = IFN_ADD_OVERFLOW;
8523 break;
8524 case BUILT_IN_SUB_OVERFLOW_P:
8525 ovf_only = true;
8526 /* FALLTHRU */
8527 case BUILT_IN_SUB_OVERFLOW:
8528 opcode = MINUS_EXPR;
8529 /* FALLTHRU */
8530 case BUILT_IN_SSUB_OVERFLOW:
8531 case BUILT_IN_SSUBL_OVERFLOW:
8532 case BUILT_IN_SSUBLL_OVERFLOW:
8533 case BUILT_IN_USUB_OVERFLOW:
8534 case BUILT_IN_USUBL_OVERFLOW:
8535 case BUILT_IN_USUBLL_OVERFLOW:
8536 ifn = IFN_SUB_OVERFLOW;
8537 break;
8538 case BUILT_IN_MUL_OVERFLOW_P:
8539 ovf_only = true;
8540 /* FALLTHRU */
8541 case BUILT_IN_MUL_OVERFLOW:
8542 opcode = MULT_EXPR;
8543 /* FALLTHRU */
8544 case BUILT_IN_SMUL_OVERFLOW:
8545 case BUILT_IN_SMULL_OVERFLOW:
8546 case BUILT_IN_SMULLL_OVERFLOW:
8547 case BUILT_IN_UMUL_OVERFLOW:
8548 case BUILT_IN_UMULL_OVERFLOW:
8549 case BUILT_IN_UMULLL_OVERFLOW:
8550 ifn = IFN_MUL_OVERFLOW;
8551 break;
8552 default:
8553 gcc_unreachable ();
8554 }
8555
8556 /* For the "generic" overloads, the first two arguments can have different
8557 types and the last argument determines the target type to use to check
8558 for overflow. The arguments of the other overloads all have the same
8559 type. */
8560 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
8561
8562 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
8563 arguments are constant, attempt to fold the built-in call into a constant
8564 expression indicating whether or not it detected an overflow. */
8565 if (ovf_only
8566 && TREE_CODE (arg0) == INTEGER_CST
8567 && TREE_CODE (arg1) == INTEGER_CST)
8568 /* Perform the computation in the target type and check for overflow. */
8569 return omit_one_operand_loc (loc, boolean_type_node,
8570 arith_overflowed_p (opcode, type, arg0, arg1)
8571 ? boolean_true_node : boolean_false_node,
8572 arg2);
8573
8574 tree ctype = build_complex_type (type);
8575 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
8576 2, arg0, arg1);
8577 tree tgt = save_expr (call);
8578 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
8579 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
8580 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
8581
8582 if (ovf_only)
8583 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
8584
8585 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
8586 tree store
8587 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
8588 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
8589 }
8590
8591 /* Fold a call to __builtin_FILE to a constant string. */
8592
8593 static inline tree
8594 fold_builtin_FILE (location_t loc)
8595 {
8596 if (const char *fname = LOCATION_FILE (loc))
8597 return build_string_literal (strlen (fname) + 1, fname);
8598
8599 return build_string_literal (1, "");
8600 }
8601
8602 /* Fold a call to __builtin_FUNCTION to a constant string. */
8603
8604 static inline tree
8605 fold_builtin_FUNCTION ()
8606 {
8607 if (current_function_decl)
8608 {
8609 const char *name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
8610 return build_string_literal (strlen (name) + 1, name);
8611 }
8612
8613 return build_string_literal (1, "");
8614 }
8615
8616 /* Fold a call to __builtin_LINE to an integer constant. */
8617
8618 static inline tree
8619 fold_builtin_LINE (location_t loc, tree type)
8620 {
8621 return build_int_cst (type, LOCATION_LINE (loc));
8622 }
8623
8624 /* Fold a call to built-in function FNDECL with 0 arguments.
8625 This function returns NULL_TREE if no simplification was possible. */
8626
8627 static tree
8628 fold_builtin_0 (location_t loc, tree fndecl)
8629 {
8630 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8631 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8632 switch (fcode)
8633 {
8634 case BUILT_IN_FILE:
8635 return fold_builtin_FILE (loc);
8636
8637 case BUILT_IN_FUNCTION:
8638 return fold_builtin_FUNCTION ();
8639
8640 case BUILT_IN_LINE:
8641 return fold_builtin_LINE (loc, type);
8642
8643 CASE_FLT_FN (BUILT_IN_INF):
8644 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
8645 case BUILT_IN_INFD32:
8646 case BUILT_IN_INFD64:
8647 case BUILT_IN_INFD128:
8648 return fold_builtin_inf (loc, type, true);
8649
8650 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
8651 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
8652 return fold_builtin_inf (loc, type, false);
8653
8654 case BUILT_IN_CLASSIFY_TYPE:
8655 return fold_builtin_classify_type (NULL_TREE);
8656
8657 default:
8658 break;
8659 }
8660 return NULL_TREE;
8661 }
8662
8663 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
8664 This function returns NULL_TREE if no simplification was possible. */
8665
8666 static tree
8667 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
8668 {
8669 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8670 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8671
8672 if (TREE_CODE (arg0) == ERROR_MARK)
8673 return NULL_TREE;
8674
8675 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
8676 return ret;
8677
8678 switch (fcode)
8679 {
8680 case BUILT_IN_CONSTANT_P:
8681 {
8682 tree val = fold_builtin_constant_p (arg0);
8683
8684 /* Gimplification will pull the CALL_EXPR for the builtin out of
8685 an if condition. When not optimizing, we'll not CSE it back.
8686 To avoid link error types of regressions, return false now. */
8687 if (!val && !optimize)
8688 val = integer_zero_node;
8689
8690 return val;
8691 }
8692
8693 case BUILT_IN_CLASSIFY_TYPE:
8694 return fold_builtin_classify_type (arg0);
8695
8696 case BUILT_IN_STRLEN:
8697 return fold_builtin_strlen (loc, type, arg0);
8698
8699 CASE_FLT_FN (BUILT_IN_FABS):
8700 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
8701 case BUILT_IN_FABSD32:
8702 case BUILT_IN_FABSD64:
8703 case BUILT_IN_FABSD128:
8704 return fold_builtin_fabs (loc, arg0, type);
8705
8706 case BUILT_IN_ABS:
8707 case BUILT_IN_LABS:
8708 case BUILT_IN_LLABS:
8709 case BUILT_IN_IMAXABS:
8710 return fold_builtin_abs (loc, arg0, type);
8711
8712 CASE_FLT_FN (BUILT_IN_CONJ):
8713 if (validate_arg (arg0, COMPLEX_TYPE)
8714 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8715 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
8716 break;
8717
8718 CASE_FLT_FN (BUILT_IN_CREAL):
8719 if (validate_arg (arg0, COMPLEX_TYPE)
8720 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8721 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
8722 break;
8723
8724 CASE_FLT_FN (BUILT_IN_CIMAG):
8725 if (validate_arg (arg0, COMPLEX_TYPE)
8726 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
8727 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
8728 break;
8729
8730 CASE_FLT_FN (BUILT_IN_CARG):
8731 return fold_builtin_carg (loc, arg0, type);
8732
8733 case BUILT_IN_ISASCII:
8734 return fold_builtin_isascii (loc, arg0);
8735
8736 case BUILT_IN_TOASCII:
8737 return fold_builtin_toascii (loc, arg0);
8738
8739 case BUILT_IN_ISDIGIT:
8740 return fold_builtin_isdigit (loc, arg0);
8741
8742 CASE_FLT_FN (BUILT_IN_FINITE):
8743 case BUILT_IN_FINITED32:
8744 case BUILT_IN_FINITED64:
8745 case BUILT_IN_FINITED128:
8746 case BUILT_IN_ISFINITE:
8747 {
8748 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
8749 if (ret)
8750 return ret;
8751 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8752 }
8753
8754 CASE_FLT_FN (BUILT_IN_ISINF):
8755 case BUILT_IN_ISINFD32:
8756 case BUILT_IN_ISINFD64:
8757 case BUILT_IN_ISINFD128:
8758 {
8759 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
8760 if (ret)
8761 return ret;
8762 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8763 }
8764
8765 case BUILT_IN_ISNORMAL:
8766 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
8767
8768 case BUILT_IN_ISINF_SIGN:
8769 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
8770
8771 CASE_FLT_FN (BUILT_IN_ISNAN):
8772 case BUILT_IN_ISNAND32:
8773 case BUILT_IN_ISNAND64:
8774 case BUILT_IN_ISNAND128:
8775 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
8776
8777 case BUILT_IN_FREE:
8778 if (integer_zerop (arg0))
8779 return build_empty_stmt (loc);
8780 break;
8781
8782 default:
8783 break;
8784 }
8785
8786 return NULL_TREE;
8787
8788 }
8789
8790 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
8791 This function returns NULL_TREE if no simplification was possible. */
8792
8793 static tree
8794 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
8795 {
8796 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8797 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8798
8799 if (TREE_CODE (arg0) == ERROR_MARK
8800 || TREE_CODE (arg1) == ERROR_MARK)
8801 return NULL_TREE;
8802
8803 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
8804 return ret;
8805
8806 switch (fcode)
8807 {
8808 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
8809 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
8810 if (validate_arg (arg0, REAL_TYPE)
8811 && validate_arg (arg1, POINTER_TYPE))
8812 return do_mpfr_lgamma_r (arg0, arg1, type);
8813 break;
8814
8815 CASE_FLT_FN (BUILT_IN_FREXP):
8816 return fold_builtin_frexp (loc, arg0, arg1, type);
8817
8818 CASE_FLT_FN (BUILT_IN_MODF):
8819 return fold_builtin_modf (loc, arg0, arg1, type);
8820
8821 case BUILT_IN_STRSPN:
8822 return fold_builtin_strspn (loc, arg0, arg1);
8823
8824 case BUILT_IN_STRCSPN:
8825 return fold_builtin_strcspn (loc, arg0, arg1);
8826
8827 case BUILT_IN_STRPBRK:
8828 return fold_builtin_strpbrk (loc, arg0, arg1, type);
8829
8830 case BUILT_IN_EXPECT:
8831 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
8832
8833 case BUILT_IN_ISGREATER:
8834 return fold_builtin_unordered_cmp (loc, fndecl,
8835 arg0, arg1, UNLE_EXPR, LE_EXPR);
8836 case BUILT_IN_ISGREATEREQUAL:
8837 return fold_builtin_unordered_cmp (loc, fndecl,
8838 arg0, arg1, UNLT_EXPR, LT_EXPR);
8839 case BUILT_IN_ISLESS:
8840 return fold_builtin_unordered_cmp (loc, fndecl,
8841 arg0, arg1, UNGE_EXPR, GE_EXPR);
8842 case BUILT_IN_ISLESSEQUAL:
8843 return fold_builtin_unordered_cmp (loc, fndecl,
8844 arg0, arg1, UNGT_EXPR, GT_EXPR);
8845 case BUILT_IN_ISLESSGREATER:
8846 return fold_builtin_unordered_cmp (loc, fndecl,
8847 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
8848 case BUILT_IN_ISUNORDERED:
8849 return fold_builtin_unordered_cmp (loc, fndecl,
8850 arg0, arg1, UNORDERED_EXPR,
8851 NOP_EXPR);
8852
8853 /* We do the folding for va_start in the expander. */
8854 case BUILT_IN_VA_START:
8855 break;
8856
8857 case BUILT_IN_OBJECT_SIZE:
8858 return fold_builtin_object_size (arg0, arg1);
8859
8860 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8861 return fold_builtin_atomic_always_lock_free (arg0, arg1);
8862
8863 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8864 return fold_builtin_atomic_is_lock_free (arg0, arg1);
8865
8866 default:
8867 break;
8868 }
8869 return NULL_TREE;
8870 }
8871
8872 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
8873 and ARG2.
8874 This function returns NULL_TREE if no simplification was possible. */
8875
8876 static tree
8877 fold_builtin_3 (location_t loc, tree fndecl,
8878 tree arg0, tree arg1, tree arg2)
8879 {
8880 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8881 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8882
8883 if (TREE_CODE (arg0) == ERROR_MARK
8884 || TREE_CODE (arg1) == ERROR_MARK
8885 || TREE_CODE (arg2) == ERROR_MARK)
8886 return NULL_TREE;
8887
8888 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
8889 arg0, arg1, arg2))
8890 return ret;
8891
8892 switch (fcode)
8893 {
8894
8895 CASE_FLT_FN (BUILT_IN_SINCOS):
8896 return fold_builtin_sincos (loc, arg0, arg1, arg2);
8897
8898 CASE_FLT_FN (BUILT_IN_FMA):
8899 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
8900
8901 CASE_FLT_FN (BUILT_IN_REMQUO):
8902 if (validate_arg (arg0, REAL_TYPE)
8903 && validate_arg (arg1, REAL_TYPE)
8904 && validate_arg (arg2, POINTER_TYPE))
8905 return do_mpfr_remquo (arg0, arg1, arg2);
8906 break;
8907
8908 case BUILT_IN_BCMP:
8909 case BUILT_IN_MEMCMP:
8910 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
8911
8912 case BUILT_IN_EXPECT:
8913 return fold_builtin_expect (loc, arg0, arg1, arg2);
8914
8915 case BUILT_IN_ADD_OVERFLOW:
8916 case BUILT_IN_SUB_OVERFLOW:
8917 case BUILT_IN_MUL_OVERFLOW:
8918 case BUILT_IN_ADD_OVERFLOW_P:
8919 case BUILT_IN_SUB_OVERFLOW_P:
8920 case BUILT_IN_MUL_OVERFLOW_P:
8921 case BUILT_IN_SADD_OVERFLOW:
8922 case BUILT_IN_SADDL_OVERFLOW:
8923 case BUILT_IN_SADDLL_OVERFLOW:
8924 case BUILT_IN_SSUB_OVERFLOW:
8925 case BUILT_IN_SSUBL_OVERFLOW:
8926 case BUILT_IN_SSUBLL_OVERFLOW:
8927 case BUILT_IN_SMUL_OVERFLOW:
8928 case BUILT_IN_SMULL_OVERFLOW:
8929 case BUILT_IN_SMULLL_OVERFLOW:
8930 case BUILT_IN_UADD_OVERFLOW:
8931 case BUILT_IN_UADDL_OVERFLOW:
8932 case BUILT_IN_UADDLL_OVERFLOW:
8933 case BUILT_IN_USUB_OVERFLOW:
8934 case BUILT_IN_USUBL_OVERFLOW:
8935 case BUILT_IN_USUBLL_OVERFLOW:
8936 case BUILT_IN_UMUL_OVERFLOW:
8937 case BUILT_IN_UMULL_OVERFLOW:
8938 case BUILT_IN_UMULLL_OVERFLOW:
8939 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
8940
8941 default:
8942 break;
8943 }
8944 return NULL_TREE;
8945 }
8946
8947 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
8948 arguments. IGNORE is true if the result of the
8949 function call is ignored. This function returns NULL_TREE if no
8950 simplification was possible. */
8951
8952 tree
8953 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
8954 {
8955 tree ret = NULL_TREE;
8956
8957 switch (nargs)
8958 {
8959 case 0:
8960 ret = fold_builtin_0 (loc, fndecl);
8961 break;
8962 case 1:
8963 ret = fold_builtin_1 (loc, fndecl, args[0]);
8964 break;
8965 case 2:
8966 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
8967 break;
8968 case 3:
8969 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
8970 break;
8971 default:
8972 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
8973 break;
8974 }
8975 if (ret)
8976 {
8977 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
8978 SET_EXPR_LOCATION (ret, loc);
8979 TREE_NO_WARNING (ret) = 1;
8980 return ret;
8981 }
8982 return NULL_TREE;
8983 }
8984
8985 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
8986 list ARGS along with N new arguments in NEWARGS. SKIP is the number
8987 of arguments in ARGS to be omitted. OLDNARGS is the number of
8988 elements in ARGS. */
8989
8990 static tree
8991 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
8992 int skip, tree fndecl, int n, va_list newargs)
8993 {
8994 int nargs = oldnargs - skip + n;
8995 tree *buffer;
8996
8997 if (n > 0)
8998 {
8999 int i, j;
9000
9001 buffer = XALLOCAVEC (tree, nargs);
9002 for (i = 0; i < n; i++)
9003 buffer[i] = va_arg (newargs, tree);
9004 for (j = skip; j < oldnargs; j++, i++)
9005 buffer[i] = args[j];
9006 }
9007 else
9008 buffer = args + skip;
9009
9010 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9011 }
9012
9013 /* Return true if FNDECL shouldn't be folded right now.
9014 If a built-in function has an inline attribute always_inline
9015 wrapper, defer folding it after always_inline functions have
9016 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9017 might not be performed. */
9018
9019 bool
9020 avoid_folding_inline_builtin (tree fndecl)
9021 {
9022 return (DECL_DECLARED_INLINE_P (fndecl)
9023 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9024 && cfun
9025 && !cfun->always_inline_functions_inlined
9026 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9027 }
9028
9029 /* A wrapper function for builtin folding that prevents warnings for
9030 "statement without effect" and the like, caused by removing the
9031 call node earlier than the warning is generated. */
9032
9033 tree
9034 fold_call_expr (location_t loc, tree exp, bool ignore)
9035 {
9036 tree ret = NULL_TREE;
9037 tree fndecl = get_callee_fndecl (exp);
9038 if (fndecl
9039 && TREE_CODE (fndecl) == FUNCTION_DECL
9040 && DECL_BUILT_IN (fndecl)
9041 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9042 yet. Defer folding until we see all the arguments
9043 (after inlining). */
9044 && !CALL_EXPR_VA_ARG_PACK (exp))
9045 {
9046 int nargs = call_expr_nargs (exp);
9047
9048 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9049 instead last argument is __builtin_va_arg_pack (). Defer folding
9050 even in that case, until arguments are finalized. */
9051 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9052 {
9053 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9054 if (fndecl2
9055 && TREE_CODE (fndecl2) == FUNCTION_DECL
9056 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9057 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9058 return NULL_TREE;
9059 }
9060
9061 if (avoid_folding_inline_builtin (fndecl))
9062 return NULL_TREE;
9063
9064 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9065 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9066 CALL_EXPR_ARGP (exp), ignore);
9067 else
9068 {
9069 tree *args = CALL_EXPR_ARGP (exp);
9070 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9071 if (ret)
9072 return ret;
9073 }
9074 }
9075 return NULL_TREE;
9076 }
9077
9078 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9079 N arguments are passed in the array ARGARRAY. Return a folded
9080 expression or NULL_TREE if no simplification was possible. */
9081
9082 tree
9083 fold_builtin_call_array (location_t loc, tree,
9084 tree fn,
9085 int n,
9086 tree *argarray)
9087 {
9088 if (TREE_CODE (fn) != ADDR_EXPR)
9089 return NULL_TREE;
9090
9091 tree fndecl = TREE_OPERAND (fn, 0);
9092 if (TREE_CODE (fndecl) == FUNCTION_DECL
9093 && DECL_BUILT_IN (fndecl))
9094 {
9095 /* If last argument is __builtin_va_arg_pack (), arguments to this
9096 function are not finalized yet. Defer folding until they are. */
9097 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9098 {
9099 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9100 if (fndecl2
9101 && TREE_CODE (fndecl2) == FUNCTION_DECL
9102 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9103 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9104 return NULL_TREE;
9105 }
9106 if (avoid_folding_inline_builtin (fndecl))
9107 return NULL_TREE;
9108 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9109 return targetm.fold_builtin (fndecl, n, argarray, false);
9110 else
9111 return fold_builtin_n (loc, fndecl, argarray, n, false);
9112 }
9113
9114 return NULL_TREE;
9115 }
9116
9117 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9118 along with N new arguments specified as the "..." parameters. SKIP
9119 is the number of arguments in EXP to be omitted. This function is used
9120 to do varargs-to-varargs transformations. */
9121
9122 static tree
9123 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9124 {
9125 va_list ap;
9126 tree t;
9127
9128 va_start (ap, n);
9129 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9130 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9131 va_end (ap);
9132
9133 return t;
9134 }
9135
9136 /* Validate a single argument ARG against a tree code CODE representing
9137 a type. When NONNULL is true consider a pointer argument valid only
9138 if it's non-null. Return true when argument is valid. */
9139
9140 static bool
9141 validate_arg (const_tree arg, enum tree_code code, bool nonnull /*= false*/)
9142 {
9143 if (!arg)
9144 return false;
9145 else if (code == POINTER_TYPE)
9146 return POINTER_TYPE_P (TREE_TYPE (arg))
9147 && (!nonnull || !integer_zerop (arg));
9148 else if (code == INTEGER_TYPE)
9149 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9150 return code == TREE_CODE (TREE_TYPE (arg));
9151 }
9152
9153 /* This function validates the types of a function call argument list
9154 against a specified list of tree_codes. If the last specifier is a 0,
9155 that represents an ellipses, otherwise the last specifier must be a
9156 VOID_TYPE.
9157
9158 This is the GIMPLE version of validate_arglist. Eventually we want to
9159 completely convert builtins.c to work from GIMPLEs and the tree based
9160 validate_arglist will then be removed. */
9161
9162 bool
9163 validate_gimple_arglist (const gcall *call, ...)
9164 {
9165 enum tree_code code;
9166 bool res = 0;
9167 va_list ap;
9168 const_tree arg;
9169 size_t i;
9170
9171 va_start (ap, call);
9172 i = 0;
9173
9174 do
9175 {
9176 code = (enum tree_code) va_arg (ap, int);
9177 switch (code)
9178 {
9179 case 0:
9180 /* This signifies an ellipses, any further arguments are all ok. */
9181 res = true;
9182 goto end;
9183 case VOID_TYPE:
9184 /* This signifies an endlink, if no arguments remain, return
9185 true, otherwise return false. */
9186 res = (i == gimple_call_num_args (call));
9187 goto end;
9188 default:
9189 /* If no parameters remain or the parameter's code does not
9190 match the specified code, return false. Otherwise continue
9191 checking any remaining arguments. */
9192 arg = gimple_call_arg (call, i++);
9193 if (!validate_arg (arg, code))
9194 goto end;
9195 break;
9196 }
9197 }
9198 while (1);
9199
9200 /* We need gotos here since we can only have one VA_CLOSE in a
9201 function. */
9202 end: ;
9203 va_end (ap);
9204
9205 return res;
9206 }
9207
9208 /* Default target-specific builtin expander that does nothing. */
9209
9210 rtx
9211 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9212 rtx target ATTRIBUTE_UNUSED,
9213 rtx subtarget ATTRIBUTE_UNUSED,
9214 machine_mode mode ATTRIBUTE_UNUSED,
9215 int ignore ATTRIBUTE_UNUSED)
9216 {
9217 return NULL_RTX;
9218 }
9219
9220 /* Returns true is EXP represents data that would potentially reside
9221 in a readonly section. */
9222
9223 bool
9224 readonly_data_expr (tree exp)
9225 {
9226 STRIP_NOPS (exp);
9227
9228 if (TREE_CODE (exp) != ADDR_EXPR)
9229 return false;
9230
9231 exp = get_base_address (TREE_OPERAND (exp, 0));
9232 if (!exp)
9233 return false;
9234
9235 /* Make sure we call decl_readonly_section only for trees it
9236 can handle (since it returns true for everything it doesn't
9237 understand). */
9238 if (TREE_CODE (exp) == STRING_CST
9239 || TREE_CODE (exp) == CONSTRUCTOR
9240 || (VAR_P (exp) && TREE_STATIC (exp)))
9241 return decl_readonly_section (exp, 0);
9242 else
9243 return false;
9244 }
9245
9246 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9247 to the call, and TYPE is its return type.
9248
9249 Return NULL_TREE if no simplification was possible, otherwise return the
9250 simplified form of the call as a tree.
9251
9252 The simplified form may be a constant or other expression which
9253 computes the same value, but in a more efficient manner (including
9254 calls to other builtin functions).
9255
9256 The call may contain arguments which need to be evaluated, but
9257 which are not useful to determine the result of the call. In
9258 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9259 COMPOUND_EXPR will be an argument which must be evaluated.
9260 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9261 COMPOUND_EXPR in the chain will contain the tree for the simplified
9262 form of the builtin function call. */
9263
9264 static tree
9265 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
9266 {
9267 if (!validate_arg (s1, POINTER_TYPE)
9268 || !validate_arg (s2, POINTER_TYPE))
9269 return NULL_TREE;
9270 else
9271 {
9272 tree fn;
9273 const char *p1, *p2;
9274
9275 p2 = c_getstr (s2);
9276 if (p2 == NULL)
9277 return NULL_TREE;
9278
9279 p1 = c_getstr (s1);
9280 if (p1 != NULL)
9281 {
9282 const char *r = strpbrk (p1, p2);
9283 tree tem;
9284
9285 if (r == NULL)
9286 return build_int_cst (TREE_TYPE (s1), 0);
9287
9288 /* Return an offset into the constant string argument. */
9289 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9290 return fold_convert_loc (loc, type, tem);
9291 }
9292
9293 if (p2[0] == '\0')
9294 /* strpbrk(x, "") == NULL.
9295 Evaluate and ignore s1 in case it had side-effects. */
9296 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
9297
9298 if (p2[1] != '\0')
9299 return NULL_TREE; /* Really call strpbrk. */
9300
9301 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9302 if (!fn)
9303 return NULL_TREE;
9304
9305 /* New argument list transforming strpbrk(s1, s2) to
9306 strchr(s1, s2[0]). */
9307 return build_call_expr_loc (loc, fn, 2, s1,
9308 build_int_cst (integer_type_node, p2[0]));
9309 }
9310 }
9311
9312 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9313 to the call.
9314
9315 Return NULL_TREE if no simplification was possible, otherwise return the
9316 simplified form of the call as a tree.
9317
9318 The simplified form may be a constant or other expression which
9319 computes the same value, but in a more efficient manner (including
9320 calls to other builtin functions).
9321
9322 The call may contain arguments which need to be evaluated, but
9323 which are not useful to determine the result of the call. In
9324 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9325 COMPOUND_EXPR will be an argument which must be evaluated.
9326 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9327 COMPOUND_EXPR in the chain will contain the tree for the simplified
9328 form of the builtin function call. */
9329
9330 static tree
9331 fold_builtin_strspn (location_t loc, tree s1, tree s2)
9332 {
9333 if (!validate_arg (s1, POINTER_TYPE)
9334 || !validate_arg (s2, POINTER_TYPE))
9335 return NULL_TREE;
9336 else
9337 {
9338 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9339
9340 /* If either argument is "", return NULL_TREE. */
9341 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9342 /* Evaluate and ignore both arguments in case either one has
9343 side-effects. */
9344 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9345 s1, s2);
9346 return NULL_TREE;
9347 }
9348 }
9349
9350 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9351 to the call.
9352
9353 Return NULL_TREE if no simplification was possible, otherwise return the
9354 simplified form of the call as a tree.
9355
9356 The simplified form may be a constant or other expression which
9357 computes the same value, but in a more efficient manner (including
9358 calls to other builtin functions).
9359
9360 The call may contain arguments which need to be evaluated, but
9361 which are not useful to determine the result of the call. In
9362 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9363 COMPOUND_EXPR will be an argument which must be evaluated.
9364 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9365 COMPOUND_EXPR in the chain will contain the tree for the simplified
9366 form of the builtin function call. */
9367
9368 static tree
9369 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
9370 {
9371 if (!validate_arg (s1, POINTER_TYPE)
9372 || !validate_arg (s2, POINTER_TYPE))
9373 return NULL_TREE;
9374 else
9375 {
9376 /* If the first argument is "", return NULL_TREE. */
9377 const char *p1 = c_getstr (s1);
9378 if (p1 && *p1 == '\0')
9379 {
9380 /* Evaluate and ignore argument s2 in case it has
9381 side-effects. */
9382 return omit_one_operand_loc (loc, size_type_node,
9383 size_zero_node, s2);
9384 }
9385
9386 /* If the second argument is "", return __builtin_strlen(s1). */
9387 const char *p2 = c_getstr (s2);
9388 if (p2 && *p2 == '\0')
9389 {
9390 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9391
9392 /* If the replacement _DECL isn't initialized, don't do the
9393 transformation. */
9394 if (!fn)
9395 return NULL_TREE;
9396
9397 return build_call_expr_loc (loc, fn, 1, s1);
9398 }
9399 return NULL_TREE;
9400 }
9401 }
9402
9403 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9404 produced. False otherwise. This is done so that we don't output the error
9405 or warning twice or three times. */
9406
9407 bool
9408 fold_builtin_next_arg (tree exp, bool va_start_p)
9409 {
9410 tree fntype = TREE_TYPE (current_function_decl);
9411 int nargs = call_expr_nargs (exp);
9412 tree arg;
9413 /* There is good chance the current input_location points inside the
9414 definition of the va_start macro (perhaps on the token for
9415 builtin) in a system header, so warnings will not be emitted.
9416 Use the location in real source code. */
9417 source_location current_location =
9418 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9419 NULL);
9420
9421 if (!stdarg_p (fntype))
9422 {
9423 error ("%<va_start%> used in function with fixed args");
9424 return true;
9425 }
9426
9427 if (va_start_p)
9428 {
9429 if (va_start_p && (nargs != 2))
9430 {
9431 error ("wrong number of arguments to function %<va_start%>");
9432 return true;
9433 }
9434 arg = CALL_EXPR_ARG (exp, 1);
9435 }
9436 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9437 when we checked the arguments and if needed issued a warning. */
9438 else
9439 {
9440 if (nargs == 0)
9441 {
9442 /* Evidently an out of date version of <stdarg.h>; can't validate
9443 va_start's second argument, but can still work as intended. */
9444 warning_at (current_location,
9445 OPT_Wvarargs,
9446 "%<__builtin_next_arg%> called without an argument");
9447 return true;
9448 }
9449 else if (nargs > 1)
9450 {
9451 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9452 return true;
9453 }
9454 arg = CALL_EXPR_ARG (exp, 0);
9455 }
9456
9457 if (TREE_CODE (arg) == SSA_NAME)
9458 arg = SSA_NAME_VAR (arg);
9459
9460 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9461 or __builtin_next_arg (0) the first time we see it, after checking
9462 the arguments and if needed issuing a warning. */
9463 if (!integer_zerop (arg))
9464 {
9465 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9466
9467 /* Strip off all nops for the sake of the comparison. This
9468 is not quite the same as STRIP_NOPS. It does more.
9469 We must also strip off INDIRECT_EXPR for C++ reference
9470 parameters. */
9471 while (CONVERT_EXPR_P (arg)
9472 || TREE_CODE (arg) == INDIRECT_REF)
9473 arg = TREE_OPERAND (arg, 0);
9474 if (arg != last_parm)
9475 {
9476 /* FIXME: Sometimes with the tree optimizers we can get the
9477 not the last argument even though the user used the last
9478 argument. We just warn and set the arg to be the last
9479 argument so that we will get wrong-code because of
9480 it. */
9481 warning_at (current_location,
9482 OPT_Wvarargs,
9483 "second parameter of %<va_start%> not last named argument");
9484 }
9485
9486 /* Undefined by C99 7.15.1.4p4 (va_start):
9487 "If the parameter parmN is declared with the register storage
9488 class, with a function or array type, or with a type that is
9489 not compatible with the type that results after application of
9490 the default argument promotions, the behavior is undefined."
9491 */
9492 else if (DECL_REGISTER (arg))
9493 {
9494 warning_at (current_location,
9495 OPT_Wvarargs,
9496 "undefined behavior when second parameter of "
9497 "%<va_start%> is declared with %<register%> storage");
9498 }
9499
9500 /* We want to verify the second parameter just once before the tree
9501 optimizers are run and then avoid keeping it in the tree,
9502 as otherwise we could warn even for correct code like:
9503 void foo (int i, ...)
9504 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9505 if (va_start_p)
9506 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
9507 else
9508 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
9509 }
9510 return false;
9511 }
9512
9513
9514 /* Expand a call EXP to __builtin_object_size. */
9515
9516 static rtx
9517 expand_builtin_object_size (tree exp)
9518 {
9519 tree ost;
9520 int object_size_type;
9521 tree fndecl = get_callee_fndecl (exp);
9522
9523 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9524 {
9525 error ("%Kfirst argument of %D must be a pointer, second integer constant",
9526 exp, fndecl);
9527 expand_builtin_trap ();
9528 return const0_rtx;
9529 }
9530
9531 ost = CALL_EXPR_ARG (exp, 1);
9532 STRIP_NOPS (ost);
9533
9534 if (TREE_CODE (ost) != INTEGER_CST
9535 || tree_int_cst_sgn (ost) < 0
9536 || compare_tree_int (ost, 3) > 0)
9537 {
9538 error ("%Klast argument of %D is not integer constant between 0 and 3",
9539 exp, fndecl);
9540 expand_builtin_trap ();
9541 return const0_rtx;
9542 }
9543
9544 object_size_type = tree_to_shwi (ost);
9545
9546 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9547 }
9548
9549 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9550 FCODE is the BUILT_IN_* to use.
9551 Return NULL_RTX if we failed; the caller should emit a normal call,
9552 otherwise try to get the result in TARGET, if convenient (and in
9553 mode MODE if that's convenient). */
9554
9555 static rtx
9556 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
9557 enum built_in_function fcode)
9558 {
9559 tree dest, src, len, size;
9560
9561 if (!validate_arglist (exp,
9562 POINTER_TYPE,
9563 fcode == BUILT_IN_MEMSET_CHK
9564 ? INTEGER_TYPE : POINTER_TYPE,
9565 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
9566 return NULL_RTX;
9567
9568 dest = CALL_EXPR_ARG (exp, 0);
9569 src = CALL_EXPR_ARG (exp, 1);
9570 len = CALL_EXPR_ARG (exp, 2);
9571 size = CALL_EXPR_ARG (exp, 3);
9572
9573 bool sizes_ok = check_sizes (OPT_Wstringop_overflow_,
9574 exp, len, /*maxlen=*/NULL_TREE,
9575 /*str=*/NULL_TREE, size);
9576
9577 if (!tree_fits_uhwi_p (size))
9578 return NULL_RTX;
9579
9580 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
9581 {
9582 /* Avoid transforming the checking call to an ordinary one when
9583 an overflow has been detected or when the call couldn't be
9584 validated because the size is not constant. */
9585 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
9586 return NULL_RTX;
9587
9588 tree fn = NULL_TREE;
9589 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9590 mem{cpy,pcpy,move,set} is available. */
9591 switch (fcode)
9592 {
9593 case BUILT_IN_MEMCPY_CHK:
9594 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
9595 break;
9596 case BUILT_IN_MEMPCPY_CHK:
9597 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
9598 break;
9599 case BUILT_IN_MEMMOVE_CHK:
9600 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9601 break;
9602 case BUILT_IN_MEMSET_CHK:
9603 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9604 break;
9605 default:
9606 break;
9607 }
9608
9609 if (! fn)
9610 return NULL_RTX;
9611
9612 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9613 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9614 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9615 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9616 }
9617 else if (fcode == BUILT_IN_MEMSET_CHK)
9618 return NULL_RTX;
9619 else
9620 {
9621 unsigned int dest_align = get_pointer_alignment (dest);
9622
9623 /* If DEST is not a pointer type, call the normal function. */
9624 if (dest_align == 0)
9625 return NULL_RTX;
9626
9627 /* If SRC and DEST are the same (and not volatile), do nothing. */
9628 if (operand_equal_p (src, dest, 0))
9629 {
9630 tree expr;
9631
9632 if (fcode != BUILT_IN_MEMPCPY_CHK)
9633 {
9634 /* Evaluate and ignore LEN in case it has side-effects. */
9635 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9636 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9637 }
9638
9639 expr = fold_build_pointer_plus (dest, len);
9640 return expand_expr (expr, target, mode, EXPAND_NORMAL);
9641 }
9642
9643 /* __memmove_chk special case. */
9644 if (fcode == BUILT_IN_MEMMOVE_CHK)
9645 {
9646 unsigned int src_align = get_pointer_alignment (src);
9647
9648 if (src_align == 0)
9649 return NULL_RTX;
9650
9651 /* If src is categorized for a readonly section we can use
9652 normal __memcpy_chk. */
9653 if (readonly_data_expr (src))
9654 {
9655 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
9656 if (!fn)
9657 return NULL_RTX;
9658 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
9659 dest, src, len, size);
9660 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9661 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9662 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9663 }
9664 }
9665 return NULL_RTX;
9666 }
9667 }
9668
9669 /* Emit warning if a buffer overflow is detected at compile time. */
9670
9671 static void
9672 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
9673 {
9674 /* The source string. */
9675 tree srcstr = NULL_TREE;
9676 /* The size of the destination object. */
9677 tree objsize = NULL_TREE;
9678 /* The string that is being concatenated with (as in __strcat_chk)
9679 or null if it isn't. */
9680 tree catstr = NULL_TREE;
9681 /* The maximum length of the source sequence in a bounded operation
9682 (such as __strncat_chk) or null if the operation isn't bounded
9683 (such as __strcat_chk). */
9684 tree maxlen = NULL_TREE;
9685
9686 switch (fcode)
9687 {
9688 case BUILT_IN_STRCPY_CHK:
9689 case BUILT_IN_STPCPY_CHK:
9690 srcstr = CALL_EXPR_ARG (exp, 1);
9691 objsize = CALL_EXPR_ARG (exp, 2);
9692 break;
9693
9694 case BUILT_IN_STRCAT_CHK:
9695 /* For __strcat_chk the warning will be emitted only if overflowing
9696 by at least strlen (dest) + 1 bytes. */
9697 catstr = CALL_EXPR_ARG (exp, 0);
9698 srcstr = CALL_EXPR_ARG (exp, 1);
9699 objsize = CALL_EXPR_ARG (exp, 2);
9700 break;
9701
9702 case BUILT_IN_STRNCAT_CHK:
9703 catstr = CALL_EXPR_ARG (exp, 0);
9704 srcstr = CALL_EXPR_ARG (exp, 1);
9705 maxlen = CALL_EXPR_ARG (exp, 2);
9706 objsize = CALL_EXPR_ARG (exp, 3);
9707 break;
9708
9709 case BUILT_IN_STRNCPY_CHK:
9710 case BUILT_IN_STPNCPY_CHK:
9711 srcstr = CALL_EXPR_ARG (exp, 1);
9712 maxlen = CALL_EXPR_ARG (exp, 2);
9713 objsize = CALL_EXPR_ARG (exp, 3);
9714 break;
9715
9716 case BUILT_IN_SNPRINTF_CHK:
9717 case BUILT_IN_VSNPRINTF_CHK:
9718 maxlen = CALL_EXPR_ARG (exp, 1);
9719 objsize = CALL_EXPR_ARG (exp, 3);
9720 break;
9721 default:
9722 gcc_unreachable ();
9723 }
9724
9725 if (catstr && maxlen)
9726 {
9727 /* Check __strncat_chk. There is no way to determine the length
9728 of the string to which the source string is being appended so
9729 just warn when the length of the source string is not known. */
9730 if (!check_strncat_sizes (exp, objsize))
9731 return;
9732 }
9733
9734 check_sizes (OPT_Wstringop_overflow_, exp,
9735 /*size=*/NULL_TREE, maxlen, srcstr, objsize);
9736 }
9737
9738 /* Emit warning if a buffer overflow is detected at compile time
9739 in __sprintf_chk/__vsprintf_chk calls. */
9740
9741 static void
9742 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
9743 {
9744 tree size, len, fmt;
9745 const char *fmt_str;
9746 int nargs = call_expr_nargs (exp);
9747
9748 /* Verify the required arguments in the original call. */
9749
9750 if (nargs < 4)
9751 return;
9752 size = CALL_EXPR_ARG (exp, 2);
9753 fmt = CALL_EXPR_ARG (exp, 3);
9754
9755 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
9756 return;
9757
9758 /* Check whether the format is a literal string constant. */
9759 fmt_str = c_getstr (fmt);
9760 if (fmt_str == NULL)
9761 return;
9762
9763 if (!init_target_chars ())
9764 return;
9765
9766 /* If the format doesn't contain % args or %%, we know its size. */
9767 if (strchr (fmt_str, target_percent) == 0)
9768 len = build_int_cstu (size_type_node, strlen (fmt_str));
9769 /* If the format is "%s" and first ... argument is a string literal,
9770 we know it too. */
9771 else if (fcode == BUILT_IN_SPRINTF_CHK
9772 && strcmp (fmt_str, target_percent_s) == 0)
9773 {
9774 tree arg;
9775
9776 if (nargs < 5)
9777 return;
9778 arg = CALL_EXPR_ARG (exp, 4);
9779 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
9780 return;
9781
9782 len = c_strlen (arg, 1);
9783 if (!len || ! tree_fits_uhwi_p (len))
9784 return;
9785 }
9786 else
9787 return;
9788
9789 /* Add one for the terminating nul. */
9790 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
9791 check_sizes (OPT_Wstringop_overflow_,
9792 exp, /*size=*/NULL_TREE, /*maxlen=*/NULL_TREE, len, size);
9793 }
9794
9795 /* Emit warning if a free is called with address of a variable. */
9796
9797 static void
9798 maybe_emit_free_warning (tree exp)
9799 {
9800 tree arg = CALL_EXPR_ARG (exp, 0);
9801
9802 STRIP_NOPS (arg);
9803 if (TREE_CODE (arg) != ADDR_EXPR)
9804 return;
9805
9806 arg = get_base_address (TREE_OPERAND (arg, 0));
9807 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
9808 return;
9809
9810 if (SSA_VAR_P (arg))
9811 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9812 "%Kattempt to free a non-heap object %qD", exp, arg);
9813 else
9814 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
9815 "%Kattempt to free a non-heap object", exp);
9816 }
9817
9818 /* Fold a call to __builtin_object_size with arguments PTR and OST,
9819 if possible. */
9820
9821 static tree
9822 fold_builtin_object_size (tree ptr, tree ost)
9823 {
9824 unsigned HOST_WIDE_INT bytes;
9825 int object_size_type;
9826
9827 if (!validate_arg (ptr, POINTER_TYPE)
9828 || !validate_arg (ost, INTEGER_TYPE))
9829 return NULL_TREE;
9830
9831 STRIP_NOPS (ost);
9832
9833 if (TREE_CODE (ost) != INTEGER_CST
9834 || tree_int_cst_sgn (ost) < 0
9835 || compare_tree_int (ost, 3) > 0)
9836 return NULL_TREE;
9837
9838 object_size_type = tree_to_shwi (ost);
9839
9840 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
9841 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
9842 and (size_t) 0 for types 2 and 3. */
9843 if (TREE_SIDE_EFFECTS (ptr))
9844 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
9845
9846 if (TREE_CODE (ptr) == ADDR_EXPR)
9847 {
9848 compute_builtin_object_size (ptr, object_size_type, &bytes);
9849 if (wi::fits_to_tree_p (bytes, size_type_node))
9850 return build_int_cstu (size_type_node, bytes);
9851 }
9852 else if (TREE_CODE (ptr) == SSA_NAME)
9853 {
9854 /* If object size is not known yet, delay folding until
9855 later. Maybe subsequent passes will help determining
9856 it. */
9857 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
9858 && wi::fits_to_tree_p (bytes, size_type_node))
9859 return build_int_cstu (size_type_node, bytes);
9860 }
9861
9862 return NULL_TREE;
9863 }
9864
9865 /* Builtins with folding operations that operate on "..." arguments
9866 need special handling; we need to store the arguments in a convenient
9867 data structure before attempting any folding. Fortunately there are
9868 only a few builtins that fall into this category. FNDECL is the
9869 function, EXP is the CALL_EXPR for the call. */
9870
9871 static tree
9872 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
9873 {
9874 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9875 tree ret = NULL_TREE;
9876
9877 switch (fcode)
9878 {
9879 case BUILT_IN_FPCLASSIFY:
9880 ret = fold_builtin_fpclassify (loc, args, nargs);
9881 break;
9882
9883 default:
9884 break;
9885 }
9886 if (ret)
9887 {
9888 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9889 SET_EXPR_LOCATION (ret, loc);
9890 TREE_NO_WARNING (ret) = 1;
9891 return ret;
9892 }
9893 return NULL_TREE;
9894 }
9895
9896 /* Initialize format string characters in the target charset. */
9897
9898 bool
9899 init_target_chars (void)
9900 {
9901 static bool init;
9902 if (!init)
9903 {
9904 target_newline = lang_hooks.to_target_charset ('\n');
9905 target_percent = lang_hooks.to_target_charset ('%');
9906 target_c = lang_hooks.to_target_charset ('c');
9907 target_s = lang_hooks.to_target_charset ('s');
9908 if (target_newline == 0 || target_percent == 0 || target_c == 0
9909 || target_s == 0)
9910 return false;
9911
9912 target_percent_c[0] = target_percent;
9913 target_percent_c[1] = target_c;
9914 target_percent_c[2] = '\0';
9915
9916 target_percent_s[0] = target_percent;
9917 target_percent_s[1] = target_s;
9918 target_percent_s[2] = '\0';
9919
9920 target_percent_s_newline[0] = target_percent;
9921 target_percent_s_newline[1] = target_s;
9922 target_percent_s_newline[2] = target_newline;
9923 target_percent_s_newline[3] = '\0';
9924
9925 init = true;
9926 }
9927 return true;
9928 }
9929
9930 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
9931 and no overflow/underflow occurred. INEXACT is true if M was not
9932 exactly calculated. TYPE is the tree type for the result. This
9933 function assumes that you cleared the MPFR flags and then
9934 calculated M to see if anything subsequently set a flag prior to
9935 entering this function. Return NULL_TREE if any checks fail. */
9936
9937 static tree
9938 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
9939 {
9940 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9941 overflow/underflow occurred. If -frounding-math, proceed iff the
9942 result of calling FUNC was exact. */
9943 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
9944 && (!flag_rounding_math || !inexact))
9945 {
9946 REAL_VALUE_TYPE rr;
9947
9948 real_from_mpfr (&rr, m, type, GMP_RNDN);
9949 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
9950 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9951 but the mpft_t is not, then we underflowed in the
9952 conversion. */
9953 if (real_isfinite (&rr)
9954 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
9955 {
9956 REAL_VALUE_TYPE rmode;
9957
9958 real_convert (&rmode, TYPE_MODE (type), &rr);
9959 /* Proceed iff the specified mode can hold the value. */
9960 if (real_identical (&rmode, &rr))
9961 return build_real (type, rmode);
9962 }
9963 }
9964 return NULL_TREE;
9965 }
9966
9967 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
9968 number and no overflow/underflow occurred. INEXACT is true if M
9969 was not exactly calculated. TYPE is the tree type for the result.
9970 This function assumes that you cleared the MPFR flags and then
9971 calculated M to see if anything subsequently set a flag prior to
9972 entering this function. Return NULL_TREE if any checks fail, if
9973 FORCE_CONVERT is true, then bypass the checks. */
9974
9975 static tree
9976 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
9977 {
9978 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9979 overflow/underflow occurred. If -frounding-math, proceed iff the
9980 result of calling FUNC was exact. */
9981 if (force_convert
9982 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
9983 && !mpfr_overflow_p () && !mpfr_underflow_p ()
9984 && (!flag_rounding_math || !inexact)))
9985 {
9986 REAL_VALUE_TYPE re, im;
9987
9988 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
9989 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
9990 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
9991 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9992 but the mpft_t is not, then we underflowed in the
9993 conversion. */
9994 if (force_convert
9995 || (real_isfinite (&re) && real_isfinite (&im)
9996 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
9997 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
9998 {
9999 REAL_VALUE_TYPE re_mode, im_mode;
10000
10001 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10002 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10003 /* Proceed iff the specified mode can hold the value. */
10004 if (force_convert
10005 || (real_identical (&re_mode, &re)
10006 && real_identical (&im_mode, &im)))
10007 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10008 build_real (TREE_TYPE (type), im_mode));
10009 }
10010 }
10011 return NULL_TREE;
10012 }
10013
10014 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10015 the pointer *(ARG_QUO) and return the result. The type is taken
10016 from the type of ARG0 and is used for setting the precision of the
10017 calculation and results. */
10018
10019 static tree
10020 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10021 {
10022 tree const type = TREE_TYPE (arg0);
10023 tree result = NULL_TREE;
10024
10025 STRIP_NOPS (arg0);
10026 STRIP_NOPS (arg1);
10027
10028 /* To proceed, MPFR must exactly represent the target floating point
10029 format, which only happens when the target base equals two. */
10030 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10031 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10032 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10033 {
10034 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10035 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10036
10037 if (real_isfinite (ra0) && real_isfinite (ra1))
10038 {
10039 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10040 const int prec = fmt->p;
10041 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10042 tree result_rem;
10043 long integer_quo;
10044 mpfr_t m0, m1;
10045
10046 mpfr_inits2 (prec, m0, m1, NULL);
10047 mpfr_from_real (m0, ra0, GMP_RNDN);
10048 mpfr_from_real (m1, ra1, GMP_RNDN);
10049 mpfr_clear_flags ();
10050 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10051 /* Remquo is independent of the rounding mode, so pass
10052 inexact=0 to do_mpfr_ckconv(). */
10053 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10054 mpfr_clears (m0, m1, NULL);
10055 if (result_rem)
10056 {
10057 /* MPFR calculates quo in the host's long so it may
10058 return more bits in quo than the target int can hold
10059 if sizeof(host long) > sizeof(target int). This can
10060 happen even for native compilers in LP64 mode. In
10061 these cases, modulo the quo value with the largest
10062 number that the target int can hold while leaving one
10063 bit for the sign. */
10064 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10065 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10066
10067 /* Dereference the quo pointer argument. */
10068 arg_quo = build_fold_indirect_ref (arg_quo);
10069 /* Proceed iff a valid pointer type was passed in. */
10070 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10071 {
10072 /* Set the value. */
10073 tree result_quo
10074 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10075 build_int_cst (TREE_TYPE (arg_quo),
10076 integer_quo));
10077 TREE_SIDE_EFFECTS (result_quo) = 1;
10078 /* Combine the quo assignment with the rem. */
10079 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10080 result_quo, result_rem));
10081 }
10082 }
10083 }
10084 }
10085 return result;
10086 }
10087
10088 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10089 resulting value as a tree with type TYPE. The mpfr precision is
10090 set to the precision of TYPE. We assume that this mpfr function
10091 returns zero if the result could be calculated exactly within the
10092 requested precision. In addition, the integer pointer represented
10093 by ARG_SG will be dereferenced and set to the appropriate signgam
10094 (-1,1) value. */
10095
10096 static tree
10097 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10098 {
10099 tree result = NULL_TREE;
10100
10101 STRIP_NOPS (arg);
10102
10103 /* To proceed, MPFR must exactly represent the target floating point
10104 format, which only happens when the target base equals two. Also
10105 verify ARG is a constant and that ARG_SG is an int pointer. */
10106 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10107 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10108 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10109 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10110 {
10111 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10112
10113 /* In addition to NaN and Inf, the argument cannot be zero or a
10114 negative integer. */
10115 if (real_isfinite (ra)
10116 && ra->cl != rvc_zero
10117 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10118 {
10119 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10120 const int prec = fmt->p;
10121 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10122 int inexact, sg;
10123 mpfr_t m;
10124 tree result_lg;
10125
10126 mpfr_init2 (m, prec);
10127 mpfr_from_real (m, ra, GMP_RNDN);
10128 mpfr_clear_flags ();
10129 inexact = mpfr_lgamma (m, &sg, m, rnd);
10130 result_lg = do_mpfr_ckconv (m, type, inexact);
10131 mpfr_clear (m);
10132 if (result_lg)
10133 {
10134 tree result_sg;
10135
10136 /* Dereference the arg_sg pointer argument. */
10137 arg_sg = build_fold_indirect_ref (arg_sg);
10138 /* Assign the signgam value into *arg_sg. */
10139 result_sg = fold_build2 (MODIFY_EXPR,
10140 TREE_TYPE (arg_sg), arg_sg,
10141 build_int_cst (TREE_TYPE (arg_sg), sg));
10142 TREE_SIDE_EFFECTS (result_sg) = 1;
10143 /* Combine the signgam assignment with the lgamma result. */
10144 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10145 result_sg, result_lg));
10146 }
10147 }
10148 }
10149
10150 return result;
10151 }
10152
10153 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10154 mpc function FUNC on it and return the resulting value as a tree
10155 with type TYPE. The mpfr precision is set to the precision of
10156 TYPE. We assume that function FUNC returns zero if the result
10157 could be calculated exactly within the requested precision. If
10158 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10159 in the arguments and/or results. */
10160
10161 tree
10162 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10163 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10164 {
10165 tree result = NULL_TREE;
10166
10167 STRIP_NOPS (arg0);
10168 STRIP_NOPS (arg1);
10169
10170 /* To proceed, MPFR must exactly represent the target floating point
10171 format, which only happens when the target base equals two. */
10172 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10173 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10174 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10175 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10176 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10177 {
10178 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10179 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10180 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10181 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10182
10183 if (do_nonfinite
10184 || (real_isfinite (re0) && real_isfinite (im0)
10185 && real_isfinite (re1) && real_isfinite (im1)))
10186 {
10187 const struct real_format *const fmt =
10188 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10189 const int prec = fmt->p;
10190 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
10191 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10192 int inexact;
10193 mpc_t m0, m1;
10194
10195 mpc_init2 (m0, prec);
10196 mpc_init2 (m1, prec);
10197 mpfr_from_real (mpc_realref (m0), re0, rnd);
10198 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10199 mpfr_from_real (mpc_realref (m1), re1, rnd);
10200 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10201 mpfr_clear_flags ();
10202 inexact = func (m0, m0, m1, crnd);
10203 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10204 mpc_clear (m0);
10205 mpc_clear (m1);
10206 }
10207 }
10208
10209 return result;
10210 }
10211
10212 /* A wrapper function for builtin folding that prevents warnings for
10213 "statement without effect" and the like, caused by removing the
10214 call node earlier than the warning is generated. */
10215
10216 tree
10217 fold_call_stmt (gcall *stmt, bool ignore)
10218 {
10219 tree ret = NULL_TREE;
10220 tree fndecl = gimple_call_fndecl (stmt);
10221 location_t loc = gimple_location (stmt);
10222 if (fndecl
10223 && TREE_CODE (fndecl) == FUNCTION_DECL
10224 && DECL_BUILT_IN (fndecl)
10225 && !gimple_call_va_arg_pack_p (stmt))
10226 {
10227 int nargs = gimple_call_num_args (stmt);
10228 tree *args = (nargs > 0
10229 ? gimple_call_arg_ptr (stmt, 0)
10230 : &error_mark_node);
10231
10232 if (avoid_folding_inline_builtin (fndecl))
10233 return NULL_TREE;
10234 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10235 {
10236 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10237 }
10238 else
10239 {
10240 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10241 if (ret)
10242 {
10243 /* Propagate location information from original call to
10244 expansion of builtin. Otherwise things like
10245 maybe_emit_chk_warning, that operate on the expansion
10246 of a builtin, will use the wrong location information. */
10247 if (gimple_has_location (stmt))
10248 {
10249 tree realret = ret;
10250 if (TREE_CODE (ret) == NOP_EXPR)
10251 realret = TREE_OPERAND (ret, 0);
10252 if (CAN_HAVE_LOCATION_P (realret)
10253 && !EXPR_HAS_LOCATION (realret))
10254 SET_EXPR_LOCATION (realret, loc);
10255 return realret;
10256 }
10257 return ret;
10258 }
10259 }
10260 }
10261 return NULL_TREE;
10262 }
10263
10264 /* Look up the function in builtin_decl that corresponds to DECL
10265 and set ASMSPEC as its user assembler name. DECL must be a
10266 function decl that declares a builtin. */
10267
10268 void
10269 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10270 {
10271 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
10272 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
10273 && asmspec != 0);
10274
10275 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10276 set_user_assembler_name (builtin, asmspec);
10277
10278 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10279 && INT_TYPE_SIZE < BITS_PER_WORD)
10280 {
10281 set_user_assembler_libfunc ("ffs", asmspec);
10282 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
10283 "ffs");
10284 }
10285 }
10286
10287 /* Return true if DECL is a builtin that expands to a constant or similarly
10288 simple code. */
10289 bool
10290 is_simple_builtin (tree decl)
10291 {
10292 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10293 switch (DECL_FUNCTION_CODE (decl))
10294 {
10295 /* Builtins that expand to constants. */
10296 case BUILT_IN_CONSTANT_P:
10297 case BUILT_IN_EXPECT:
10298 case BUILT_IN_OBJECT_SIZE:
10299 case BUILT_IN_UNREACHABLE:
10300 /* Simple register moves or loads from stack. */
10301 case BUILT_IN_ASSUME_ALIGNED:
10302 case BUILT_IN_RETURN_ADDRESS:
10303 case BUILT_IN_EXTRACT_RETURN_ADDR:
10304 case BUILT_IN_FROB_RETURN_ADDR:
10305 case BUILT_IN_RETURN:
10306 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10307 case BUILT_IN_FRAME_ADDRESS:
10308 case BUILT_IN_VA_END:
10309 case BUILT_IN_STACK_SAVE:
10310 case BUILT_IN_STACK_RESTORE:
10311 /* Exception state returns or moves registers around. */
10312 case BUILT_IN_EH_FILTER:
10313 case BUILT_IN_EH_POINTER:
10314 case BUILT_IN_EH_COPY_VALUES:
10315 return true;
10316
10317 default:
10318 return false;
10319 }
10320
10321 return false;
10322 }
10323
10324 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10325 most probably expanded inline into reasonably simple code. This is a
10326 superset of is_simple_builtin. */
10327 bool
10328 is_inexpensive_builtin (tree decl)
10329 {
10330 if (!decl)
10331 return false;
10332 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10333 return true;
10334 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10335 switch (DECL_FUNCTION_CODE (decl))
10336 {
10337 case BUILT_IN_ABS:
10338 case BUILT_IN_ALLOCA:
10339 case BUILT_IN_ALLOCA_WITH_ALIGN:
10340 case BUILT_IN_BSWAP16:
10341 case BUILT_IN_BSWAP32:
10342 case BUILT_IN_BSWAP64:
10343 case BUILT_IN_CLZ:
10344 case BUILT_IN_CLZIMAX:
10345 case BUILT_IN_CLZL:
10346 case BUILT_IN_CLZLL:
10347 case BUILT_IN_CTZ:
10348 case BUILT_IN_CTZIMAX:
10349 case BUILT_IN_CTZL:
10350 case BUILT_IN_CTZLL:
10351 case BUILT_IN_FFS:
10352 case BUILT_IN_FFSIMAX:
10353 case BUILT_IN_FFSL:
10354 case BUILT_IN_FFSLL:
10355 case BUILT_IN_IMAXABS:
10356 case BUILT_IN_FINITE:
10357 case BUILT_IN_FINITEF:
10358 case BUILT_IN_FINITEL:
10359 case BUILT_IN_FINITED32:
10360 case BUILT_IN_FINITED64:
10361 case BUILT_IN_FINITED128:
10362 case BUILT_IN_FPCLASSIFY:
10363 case BUILT_IN_ISFINITE:
10364 case BUILT_IN_ISINF_SIGN:
10365 case BUILT_IN_ISINF:
10366 case BUILT_IN_ISINFF:
10367 case BUILT_IN_ISINFL:
10368 case BUILT_IN_ISINFD32:
10369 case BUILT_IN_ISINFD64:
10370 case BUILT_IN_ISINFD128:
10371 case BUILT_IN_ISNAN:
10372 case BUILT_IN_ISNANF:
10373 case BUILT_IN_ISNANL:
10374 case BUILT_IN_ISNAND32:
10375 case BUILT_IN_ISNAND64:
10376 case BUILT_IN_ISNAND128:
10377 case BUILT_IN_ISNORMAL:
10378 case BUILT_IN_ISGREATER:
10379 case BUILT_IN_ISGREATEREQUAL:
10380 case BUILT_IN_ISLESS:
10381 case BUILT_IN_ISLESSEQUAL:
10382 case BUILT_IN_ISLESSGREATER:
10383 case BUILT_IN_ISUNORDERED:
10384 case BUILT_IN_VA_ARG_PACK:
10385 case BUILT_IN_VA_ARG_PACK_LEN:
10386 case BUILT_IN_VA_COPY:
10387 case BUILT_IN_TRAP:
10388 case BUILT_IN_SAVEREGS:
10389 case BUILT_IN_POPCOUNTL:
10390 case BUILT_IN_POPCOUNTLL:
10391 case BUILT_IN_POPCOUNTIMAX:
10392 case BUILT_IN_POPCOUNT:
10393 case BUILT_IN_PARITYL:
10394 case BUILT_IN_PARITYLL:
10395 case BUILT_IN_PARITYIMAX:
10396 case BUILT_IN_PARITY:
10397 case BUILT_IN_LABS:
10398 case BUILT_IN_LLABS:
10399 case BUILT_IN_PREFETCH:
10400 case BUILT_IN_ACC_ON_DEVICE:
10401 return true;
10402
10403 default:
10404 return is_simple_builtin (decl);
10405 }
10406
10407 return false;
10408 }
10409
10410 /* Return true if T is a constant and the value cast to a target char
10411 can be represented by a host char.
10412 Store the casted char constant in *P if so. */
10413
10414 bool
10415 target_char_cst_p (tree t, char *p)
10416 {
10417 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10418 return false;
10419
10420 *p = (char)tree_to_uhwi (t);
10421 return true;
10422 }