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