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