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