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