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