Line data Source code
1 : /* Expand builtin functions.
2 : Copyright (C) 1988-2026 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.cc instead. */
23 :
24 : #include "config.h"
25 : #include "system.h"
26 : #include "coretypes.h"
27 : #include "backend.h"
28 : #include "target.h"
29 : #include "rtl.h"
30 : #include "tree.h"
31 : #include "memmodel.h"
32 : #include "gimple.h"
33 : #include "tree-eh.h"
34 : #include "predict.h"
35 : #include "tm_p.h"
36 : #include "stringpool.h"
37 : #include "tree-vrp.h"
38 : #include "tree-ssanames.h"
39 : #include "expmed.h"
40 : #include "optabs.h"
41 : #include "emit-rtl.h"
42 : #include "recog.h"
43 : #include "diagnostic-core.h"
44 : #include "alias.h"
45 : #include "fold-const.h"
46 : #include "fold-const-call.h"
47 : #include "gimple-ssa-warn-access.h"
48 : #include "stor-layout.h"
49 : #include "calls.h"
50 : #include "varasm.h"
51 : #include "tree-object-size.h"
52 : #include "tree-ssa-strlen.h"
53 : #include "realmpfr.h"
54 : #include "cfgrtl.h"
55 : #include "except.h"
56 : #include "dojump.h"
57 : #include "explow.h"
58 : #include "stmt.h"
59 : #include "expr.h"
60 : #include "libfuncs.h"
61 : #include "output.h"
62 : #include "typeclass.h"
63 : #include "langhooks.h"
64 : #include "value-prof.h"
65 : #include "builtins.h"
66 : #include "attribs.h"
67 : #include "asan.h"
68 : #include "internal-fn.h"
69 : #include "case-cfn-macros.h"
70 : #include "gimple-iterator.h"
71 : #include "gimple-fold.h"
72 : #include "intl.h"
73 : #include "file-prefix-map.h" /* remap_macro_filename() */
74 : #include "ipa-strub.h" /* strub_watermark_parm() */
75 : #include "gomp-constants.h"
76 : #include "omp-general.h"
77 : #include "tree-dfa.h"
78 : #include "gimple-ssa.h"
79 : #include "tree-ssa-live.h"
80 : #include "tree-outof-ssa.h"
81 : #include "attr-fnspec.h"
82 : #include "demangle.h"
83 : #include "gimple-range.h"
84 : #include "pointer-query.h"
85 :
86 : struct target_builtins default_target_builtins;
87 : #if SWITCHABLE_TARGET
88 : struct target_builtins *this_target_builtins = &default_target_builtins;
89 : #endif
90 :
91 : /* Define the names of the builtin function types and codes. */
92 : const char *const built_in_class_names[BUILT_IN_LAST]
93 : = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
94 :
95 : #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
96 : const char * built_in_names[(int) END_BUILTINS] =
97 : {
98 : #include "builtins.def"
99 : };
100 :
101 : /* Setup an array of builtin_info_type, make sure each element decl is
102 : initialized to NULL_TREE. */
103 : builtin_info_type builtin_info[(int)END_BUILTINS];
104 :
105 : /* Non-zero if __builtin_constant_p should be folded right away. */
106 : bool force_folding_builtin_constant_p;
107 :
108 : static int target_char_cast (tree, char *);
109 : static int apply_args_size (void);
110 : static int apply_result_size (void);
111 : static rtx result_vector (int, rtx);
112 : static void expand_builtin_prefetch (tree);
113 : static rtx expand_builtin_apply_args (void);
114 : static rtx expand_builtin_apply_args_1 (void);
115 : static rtx expand_builtin_apply (rtx, rtx, rtx);
116 : static void expand_builtin_return (rtx);
117 : static rtx expand_builtin_classify_type (tree);
118 : static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
119 : static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
120 : static rtx expand_builtin_interclass_mathfn (tree, rtx);
121 : static rtx expand_builtin_sincos (tree);
122 : static rtx expand_builtin_fegetround (tree, rtx, machine_mode);
123 : static rtx expand_builtin_feclear_feraise_except (tree, rtx, machine_mode,
124 : optab);
125 : static rtx expand_builtin_cexpi (tree, rtx);
126 : static rtx expand_builtin_issignaling (tree, rtx);
127 : static rtx expand_builtin_int_roundingfn (tree, rtx);
128 : static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
129 : static rtx expand_builtin_next_arg (void);
130 : static rtx expand_builtin_va_start (tree);
131 : static rtx expand_builtin_va_end (tree);
132 : static rtx expand_builtin_va_copy (tree);
133 : static rtx inline_expand_builtin_bytecmp (tree, rtx);
134 : static rtx expand_builtin_strcmp (tree, rtx);
135 : static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
136 : static rtx expand_builtin_memcpy (tree, rtx);
137 : static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
138 : rtx target, tree exp,
139 : memop_ret retmode,
140 : bool might_overlap);
141 : static rtx expand_builtin_memmove (tree, rtx);
142 : static rtx expand_builtin_mempcpy (tree, rtx);
143 : static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, memop_ret);
144 : static rtx expand_builtin_strcpy (tree, rtx);
145 : static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx);
146 : static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
147 : static rtx expand_builtin_strncpy (tree, rtx);
148 : static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
149 : static rtx expand_builtin_bzero (tree);
150 : static rtx expand_builtin_strlen (tree, rtx, machine_mode);
151 : static rtx expand_builtin_strnlen (tree, rtx, machine_mode);
152 : static rtx expand_builtin_alloca (tree);
153 : static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
154 : static rtx expand_builtin_frame_address (tree, tree);
155 : static rtx expand_builtin_stack_address ();
156 : static tree stabilize_va_list_loc (location_t, tree, int);
157 : static rtx expand_builtin_expect (tree, rtx);
158 : static rtx expand_builtin_expect_with_probability (tree, rtx);
159 : static tree fold_builtin_classify_type (tree);
160 : static tree fold_builtin_strlen (location_t, tree, tree, tree);
161 : static tree fold_builtin_inf (location_t, tree, int);
162 : static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
163 : static bool validate_arg (const_tree, enum tree_code code);
164 : static rtx expand_builtin_fabs (tree, rtx, rtx);
165 : static rtx expand_builtin_signbit (tree, rtx);
166 : static tree fold_builtin_memcmp (location_t, tree, tree, tree);
167 : static tree fold_builtin_isascii (location_t, tree);
168 : static tree fold_builtin_toascii (location_t, tree);
169 : static tree fold_builtin_isdigit (location_t, tree);
170 : static tree fold_builtin_fabs (location_t, tree, tree);
171 : static tree fold_builtin_abs (location_t, tree, tree);
172 : static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173 : enum tree_code);
174 : static tree fold_builtin_iseqsig (location_t, tree, tree);
175 : static tree fold_builtin_varargs (location_t, tree, tree*, int);
176 :
177 : static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
178 : static tree fold_builtin_strspn (location_t, tree, tree, tree, tree);
179 : static tree fold_builtin_strcspn (location_t, tree, tree, tree, tree);
180 :
181 : static rtx expand_builtin_object_size (tree);
182 : static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
183 : enum built_in_function);
184 : static void maybe_emit_chk_warning (tree, enum built_in_function);
185 : static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
186 : static tree fold_builtin_object_size (tree, tree, enum built_in_function);
187 :
188 : unsigned HOST_WIDE_INT target_newline;
189 : unsigned HOST_WIDE_INT target_percent;
190 : static unsigned HOST_WIDE_INT target_c;
191 : static unsigned HOST_WIDE_INT target_s;
192 : char target_percent_c[3];
193 : char target_percent_s[3];
194 : char target_percent_s_newline[4];
195 : static tree do_mpfr_remquo (tree, tree, tree);
196 : static tree do_mpfr_lgamma_r (tree, tree, tree);
197 : static void expand_builtin_sync_synchronize (void);
198 :
199 : /* Return true if NAME starts with __builtin_ or __sync_. */
200 :
201 : static bool
202 548340 : is_builtin_name (const char *name)
203 : {
204 548340 : return (startswith (name, "__builtin_")
205 196719 : || startswith (name, "__sync_")
206 744194 : || startswith (name, "__atomic_"));
207 : }
208 :
209 : /* Return true if NODE should be considered for inline expansion regardless
210 : of the optimization level. This means whenever a function is invoked with
211 : its "internal" name, which normally contains the prefix "__builtin". */
212 :
213 : bool
214 548340 : called_as_built_in (tree node)
215 : {
216 : /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
217 : we want the name used to call the function, not the name it
218 : will have. */
219 548340 : const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
220 548340 : return is_builtin_name (name);
221 : }
222 :
223 : /* Compute values M and N such that M divides (address of EXP - N) and such
224 : that N < M. If these numbers can be determined, store M in alignp and N in
225 : *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
226 : *alignp and any bit-offset to *bitposp.
227 :
228 : Note that the address (and thus the alignment) computed here is based
229 : on the address to which a symbol resolves, whereas DECL_ALIGN is based
230 : on the address at which an object is actually located. These two
231 : addresses are not always the same. For example, on ARM targets,
232 : the address &foo of a Thumb function foo() has the lowest bit set,
233 : whereas foo() itself starts on an even address.
234 :
235 : If ADDR_P is true we are taking the address of the memory reference EXP
236 : and thus cannot rely on the access taking place. */
237 :
238 : bool
239 102457641 : get_object_alignment_2 (tree exp, unsigned int *alignp,
240 : unsigned HOST_WIDE_INT *bitposp, bool addr_p)
241 : {
242 102457641 : poly_int64 bitsize, bitpos;
243 102457641 : tree offset;
244 102457641 : machine_mode mode;
245 102457641 : int unsignedp, reversep, volatilep;
246 102457641 : unsigned int align = BITS_PER_UNIT;
247 102457641 : bool known_alignment = false;
248 :
249 : /* Get the innermost object and the constant (bitpos) and possibly
250 : variable (offset) offset of the access. */
251 102457641 : exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
252 : &unsignedp, &reversep, &volatilep);
253 :
254 : /* Extract alignment information from the innermost object and
255 : possibly adjust bitpos and offset. */
256 102457641 : if (TREE_CODE (exp) == FUNCTION_DECL)
257 : {
258 : /* Function addresses can encode extra information besides their
259 : alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
260 : allows the low bit to be used as a virtual bit, we know
261 : that the address itself must be at least 2-byte aligned. */
262 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
263 : align = 2 * BITS_PER_UNIT;
264 : }
265 102004660 : else if (TREE_CODE (exp) == LABEL_DECL)
266 : ;
267 101967642 : else if (TREE_CODE (exp) == CONST_DECL)
268 : {
269 : /* The alignment of a CONST_DECL is determined by its initializer. */
270 55598 : exp = DECL_INITIAL (exp);
271 55598 : align = TYPE_ALIGN (TREE_TYPE (exp));
272 55598 : if (CONSTANT_CLASS_P (exp))
273 55597 : align = targetm.constant_alignment (exp, align);
274 :
275 : known_alignment = true;
276 : }
277 101912044 : else if (DECL_P (exp))
278 : {
279 61026232 : align = DECL_ALIGN (exp);
280 : known_alignment = true;
281 : }
282 40885812 : else if (TREE_CODE (exp) == INDIRECT_REF
283 40884756 : || TREE_CODE (exp) == MEM_REF
284 4893338 : || TREE_CODE (exp) == TARGET_MEM_REF)
285 : {
286 39220613 : tree addr = TREE_OPERAND (exp, 0);
287 39220613 : unsigned ptr_align;
288 39220613 : unsigned HOST_WIDE_INT ptr_bitpos;
289 39220613 : unsigned HOST_WIDE_INT ptr_bitmask = ~0;
290 :
291 : /* If the address is explicitly aligned, handle that. */
292 39220613 : if (TREE_CODE (addr) == BIT_AND_EXPR
293 39220613 : && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
294 : {
295 84 : ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
296 84 : ptr_bitmask *= BITS_PER_UNIT;
297 84 : align = least_bit_hwi (ptr_bitmask);
298 84 : addr = TREE_OPERAND (addr, 0);
299 : }
300 :
301 39220613 : known_alignment
302 39220613 : = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
303 39220613 : align = MAX (ptr_align, align);
304 :
305 : /* Re-apply explicit alignment to the bitpos. */
306 39220613 : ptr_bitpos &= ptr_bitmask;
307 :
308 : /* The alignment of the pointer operand in a TARGET_MEM_REF
309 : has to take the variable offset parts into account. */
310 39220613 : if (TREE_CODE (exp) == TARGET_MEM_REF)
311 : {
312 3228139 : if (TMR_INDEX (exp))
313 : {
314 1474348 : unsigned HOST_WIDE_INT step = 1;
315 1474348 : if (TMR_STEP (exp))
316 1343913 : step = TREE_INT_CST_LOW (TMR_STEP (exp));
317 1474348 : align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
318 : }
319 3228139 : if (TMR_INDEX2 (exp))
320 36502 : align = BITS_PER_UNIT;
321 : known_alignment = false;
322 : }
323 :
324 : /* When EXP is an actual memory reference then we can use
325 : TYPE_ALIGN of a pointer indirection to derive alignment.
326 : Do so only if get_pointer_alignment_1 did not reveal absolute
327 : alignment knowledge and if using that alignment would
328 : improve the situation. */
329 39220613 : unsigned int talign;
330 39220613 : if (!addr_p && !known_alignment
331 36794871 : && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
332 76015478 : && talign > align)
333 : align = talign;
334 : else
335 : {
336 : /* Else adjust bitpos accordingly. */
337 10078508 : bitpos += ptr_bitpos;
338 10078508 : if (TREE_CODE (exp) == MEM_REF
339 10078508 : || TREE_CODE (exp) == TARGET_MEM_REF)
340 10077452 : bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
341 : }
342 39220613 : }
343 1665199 : else if (TREE_CODE (exp) == STRING_CST)
344 : {
345 : /* STRING_CST are the only constant objects we allow to be not
346 : wrapped inside a CONST_DECL. */
347 1664579 : align = TYPE_ALIGN (TREE_TYPE (exp));
348 1664579 : if (CONSTANT_CLASS_P (exp))
349 1664579 : align = targetm.constant_alignment (exp, align);
350 :
351 : known_alignment = true;
352 : }
353 :
354 : /* If there is a non-constant offset part extract the maximum
355 : alignment that can prevail. */
356 102457641 : if (offset)
357 : {
358 6913154 : unsigned int trailing_zeros = tree_ctz (offset);
359 6913154 : if (trailing_zeros < HOST_BITS_PER_INT)
360 : {
361 6913076 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
362 6913076 : if (inner)
363 6913076 : align = MIN (align, inner);
364 : }
365 : }
366 :
367 : /* Account for the alignment of runtime coefficients, so that the constant
368 : bitpos is guaranteed to be accurate. */
369 102457641 : unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
370 102457641 : if (alt_align != 0 && alt_align < align)
371 : {
372 : align = alt_align;
373 : known_alignment = false;
374 : }
375 :
376 102457641 : *alignp = align;
377 102457641 : *bitposp = bitpos.coeffs[0] & (align - 1);
378 102457641 : return known_alignment;
379 : }
380 :
381 : /* For a memory reference expression EXP compute values M and N such that M
382 : divides (&EXP - N) and such that N < M. If these numbers can be determined,
383 : store M in alignp and N in *BITPOSP and return true. Otherwise return false
384 : and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
385 :
386 : bool
387 79222623 : get_object_alignment_1 (tree exp, unsigned int *alignp,
388 : unsigned HOST_WIDE_INT *bitposp)
389 : {
390 : /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
391 : with it. */
392 79222623 : if (TREE_CODE (exp) == WITH_SIZE_EXPR)
393 0 : exp = TREE_OPERAND (exp, 0);
394 79222623 : return get_object_alignment_2 (exp, alignp, bitposp, false);
395 : }
396 :
397 : /* Return the alignment in bits of EXP, an object. */
398 :
399 : unsigned int
400 26553442 : get_object_alignment (tree exp)
401 : {
402 26553442 : unsigned HOST_WIDE_INT bitpos = 0;
403 26553442 : unsigned int align;
404 :
405 26553442 : get_object_alignment_1 (exp, &align, &bitpos);
406 :
407 : /* align and bitpos now specify known low bits of the pointer.
408 : ptr & (align - 1) == bitpos. */
409 :
410 26553442 : if (bitpos != 0)
411 1440152 : align = least_bit_hwi (bitpos);
412 26553442 : return align;
413 : }
414 :
415 : /* For a pointer valued expression EXP compute values M and N such that M
416 : divides (EXP - N) and such that N < M. If these numbers can be determined,
417 : store M in alignp and N in *BITPOSP and return true. Return false if
418 : the results are just a conservative approximation.
419 :
420 : If EXP is not a pointer, false is returned too. */
421 :
422 : bool
423 81442046 : get_pointer_alignment_1 (tree exp, unsigned int *alignp,
424 : unsigned HOST_WIDE_INT *bitposp)
425 : {
426 81442046 : STRIP_NOPS (exp);
427 :
428 81442046 : if (TREE_CODE (exp) == ADDR_EXPR)
429 23233361 : return get_object_alignment_2 (TREE_OPERAND (exp, 0),
430 23233361 : alignp, bitposp, true);
431 58208685 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
432 : {
433 1028393 : unsigned int align;
434 1028393 : unsigned HOST_WIDE_INT bitpos;
435 1028393 : bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
436 : &align, &bitpos);
437 1028393 : if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
438 3766 : bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
439 : else
440 : {
441 1024627 : unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
442 1024627 : if (trailing_zeros < HOST_BITS_PER_INT)
443 : {
444 1024619 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
445 1024619 : if (inner)
446 1024619 : align = MIN (align, inner);
447 : }
448 : }
449 1028393 : *alignp = align;
450 1028393 : *bitposp = bitpos & (align - 1);
451 1028393 : return res;
452 : }
453 57180292 : else if (TREE_CODE (exp) == SSA_NAME
454 57180292 : && POINTER_TYPE_P (TREE_TYPE (exp)))
455 : {
456 54851726 : unsigned int ptr_align, ptr_misalign;
457 :
458 54851726 : if (get_ssa_name_alignment (exp, &ptr_align, &ptr_misalign))
459 : {
460 5293204 : *bitposp = ptr_misalign * BITS_PER_UNIT;
461 5293204 : *alignp = ptr_align * BITS_PER_UNIT;
462 : /* Make sure to return a sensible alignment when the multiplication
463 : by BITS_PER_UNIT overflowed. */
464 5293204 : if (*alignp == 0)
465 128 : *alignp = 1u << (HOST_BITS_PER_INT - 1);
466 : /* We cannot really tell whether this result is an approximation. */
467 : return false;
468 : }
469 : else
470 : {
471 49558522 : *bitposp = 0;
472 49558522 : *alignp = BITS_PER_UNIT;
473 49558522 : return false;
474 : }
475 : }
476 2328566 : else if (TREE_CODE (exp) == INTEGER_CST)
477 : {
478 17191 : *alignp = BIGGEST_ALIGNMENT;
479 17191 : *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
480 17191 : & (BIGGEST_ALIGNMENT - 1));
481 17191 : return true;
482 : }
483 :
484 2311375 : *bitposp = 0;
485 2311375 : *alignp = BITS_PER_UNIT;
486 2311375 : return false;
487 : }
488 :
489 : /* Return the alignment in bits of EXP, a pointer valued expression.
490 : The alignment returned is, by default, the alignment of the thing that
491 : EXP points to. If it is not a POINTER_TYPE, 0 is returned.
492 :
493 : Otherwise, look at the expression to see if we can do better, i.e., if the
494 : expression is actually pointing at an object whose alignment is tighter. */
495 :
496 : unsigned int
497 11590467 : get_pointer_alignment (tree exp)
498 : {
499 11590467 : unsigned HOST_WIDE_INT bitpos = 0;
500 11590467 : unsigned int align;
501 :
502 11590467 : get_pointer_alignment_1 (exp, &align, &bitpos);
503 :
504 : /* align and bitpos now specify known low bits of the pointer.
505 : ptr & (align - 1) == bitpos. */
506 :
507 11590467 : if (bitpos != 0)
508 82016 : align = least_bit_hwi (bitpos);
509 :
510 11590467 : return align;
511 : }
512 :
513 : /* Return the number of leading non-zero elements in the sequence
514 : [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
515 : ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
516 :
517 : unsigned
518 767557 : string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
519 : {
520 767557 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
521 :
522 767557 : unsigned n;
523 :
524 767557 : if (eltsize == 1)
525 : {
526 : /* Optimize the common case of plain char. */
527 244805195 : for (n = 0; n < maxelts; n++)
528 : {
529 244714138 : const char *elt = (const char*) ptr + n;
530 244714138 : if (!*elt)
531 : break;
532 : }
533 : }
534 : else
535 : {
536 590 : for (n = 0; n < maxelts; n++)
537 : {
538 566 : const char *elt = (const char*) ptr + n * eltsize;
539 566 : if (!memcmp (elt, "\0\0\0\0", eltsize))
540 : break;
541 : }
542 : }
543 767557 : return n;
544 : }
545 :
546 : /* Compute the length of a null-terminated character string or wide
547 : character string handling character sizes of 1, 2, and 4 bytes.
548 : TREE_STRING_LENGTH is not the right way because it evaluates to
549 : the size of the character array in bytes (as opposed to characters)
550 : and because it can contain a zero byte in the middle.
551 :
552 : ONLY_VALUE should be nonzero if the result is not going to be emitted
553 : into the instruction stream and zero if it is going to be expanded.
554 : E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
555 : is returned, otherwise NULL, since
556 : len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
557 : evaluate the side-effects.
558 :
559 : If ONLY_VALUE is two then we do not emit warnings about out-of-bound
560 : accesses. Note that this implies the result is not going to be emitted
561 : into the instruction stream.
562 :
563 : Additional information about the string accessed may be recorded
564 : in DATA. For example, if ARG references an unterminated string,
565 : then the declaration will be stored in the DECL field. If the
566 : length of the unterminated string can be determined, it'll be
567 : stored in the LEN field. Note this length could well be different
568 : than what a C strlen call would return.
569 :
570 : ELTSIZE is 1 for normal single byte character strings, and 2 or
571 : 4 for wide character strings. ELTSIZE is by default 1.
572 :
573 : The value returned is of type `ssizetype'. */
574 :
575 : tree
576 2593610 : c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
577 : {
578 : /* If we were not passed a DATA pointer, then get one to a local
579 : structure. That avoids having to check DATA for NULL before
580 : each time we want to use it. */
581 2593610 : c_strlen_data local_strlen_data = { };
582 2593610 : if (!data)
583 598021 : data = &local_strlen_data;
584 :
585 2593610 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
586 :
587 2593610 : tree src = STRIP_NOPS (arg);
588 2593610 : if (TREE_CODE (src) == COND_EXPR
589 2593610 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
590 : {
591 690 : tree len1, len2;
592 :
593 690 : len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
594 690 : len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
595 690 : if (tree_int_cst_equal (len1, len2))
596 : return len1;
597 : }
598 :
599 2593444 : if (TREE_CODE (src) == COMPOUND_EXPR
600 2593444 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
601 45 : return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
602 :
603 2593399 : location_t loc = EXPR_LOC_OR_LOC (src, input_location);
604 :
605 : /* Offset from the beginning of the string in bytes. */
606 2593399 : tree byteoff;
607 2593399 : tree memsize;
608 2593399 : tree decl;
609 2593399 : src = string_constant (src, &byteoff, &memsize, &decl);
610 2593399 : if (src == 0)
611 : return NULL_TREE;
612 :
613 : /* Determine the size of the string element. */
614 782966 : if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src)))))
615 : return NULL_TREE;
616 :
617 : /* Set MAXELTS to ARRAY_SIZE (SRC) - 1, the maximum possible
618 : length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
619 : in case the latter is less than the size of the array, such as when
620 : SRC refers to a short string literal used to initialize a large array.
621 : In that case, the elements of the array after the terminating NUL are
622 : all NUL. */
623 771385 : HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
624 771385 : strelts = strelts / eltsize;
625 :
626 771385 : if (!tree_fits_uhwi_p (memsize))
627 : return NULL_TREE;
628 :
629 771385 : HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize;
630 :
631 : /* PTR can point to the byte representation of any string type, including
632 : char* and wchar_t*. */
633 771385 : const char *ptr = TREE_STRING_POINTER (src);
634 :
635 771385 : if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
636 : {
637 : /* The code below works only for single byte character types. */
638 5050 : if (eltsize != 1)
639 : return NULL_TREE;
640 :
641 : /* If the string has an internal NUL character followed by any
642 : non-NUL characters (e.g., "foo\0bar"), we can't compute
643 : the offset to the following NUL if we don't know where to
644 : start searching for it. */
645 5050 : unsigned len = string_length (ptr, eltsize, strelts);
646 :
647 : /* Return when an embedded null character is found or none at all.
648 : In the latter case, set the DECL/LEN field in the DATA structure
649 : so that callers may examine them. */
650 5050 : if (len + 1 < strelts)
651 : return NULL_TREE;
652 4480 : else if (len >= maxelts)
653 : {
654 1102 : data->decl = decl;
655 1102 : data->off = byteoff;
656 1102 : data->minlen = ssize_int (len);
657 1102 : return NULL_TREE;
658 : }
659 :
660 : /* For empty strings the result should be zero. */
661 3378 : if (len == 0)
662 39 : return ssize_int (0);
663 :
664 : /* We don't know the starting offset, but we do know that the string
665 : has no internal zero bytes. If the offset falls within the bounds
666 : of the string subtract the offset from the length of the string,
667 : and return that. Otherwise the length is zero. Take care to
668 : use SAVE_EXPR in case the OFFSET has side-effects. */
669 3339 : tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
670 : : byteoff;
671 3339 : offsave = fold_convert_loc (loc, sizetype, offsave);
672 3339 : tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
673 3339 : size_int (len));
674 3339 : tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
675 : offsave);
676 3339 : lenexp = fold_convert_loc (loc, ssizetype, lenexp);
677 3339 : return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
678 3339 : build_zero_cst (ssizetype));
679 : }
680 :
681 : /* Offset from the beginning of the string in elements. */
682 766335 : HOST_WIDE_INT eltoff;
683 :
684 : /* We have a known offset into the string. Start searching there for
685 : a null character if we can represent it as a single HOST_WIDE_INT. */
686 766335 : if (byteoff == 0)
687 : eltoff = 0;
688 766335 : else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
689 : eltoff = -1;
690 : else
691 765147 : eltoff = tree_to_uhwi (byteoff) / eltsize;
692 :
693 : /* If the offset is known to be out of bounds, warn, and call strlen at
694 : runtime. */
695 766335 : if (eltoff < 0 || eltoff >= maxelts)
696 : {
697 : /* Suppress multiple warnings for propagated constant strings. */
698 3828 : if (only_value != 2
699 3828 : && !warning_suppressed_p (arg, OPT_Warray_bounds_)
700 7656 : && warning_at (loc, OPT_Warray_bounds_,
701 : "offset %qwi outside bounds of constant string",
702 : eltoff))
703 : {
704 679 : if (decl)
705 678 : inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
706 679 : suppress_warning (arg, OPT_Warray_bounds_);
707 : }
708 : return NULL_TREE;
709 : }
710 :
711 : /* If eltoff is larger than strelts but less than maxelts the
712 : string length is zero, since the excess memory will be zero. */
713 762507 : if (eltoff > strelts)
714 0 : return ssize_int (0);
715 :
716 : /* Use strlen to search for the first zero byte. Since any strings
717 : constructed with build_string will have nulls appended, we win even
718 : if we get handed something like (char[4])"abcd".
719 :
720 : Since ELTOFF is our starting index into the string, no further
721 : calculation is needed. */
722 762507 : unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
723 762507 : strelts - eltoff);
724 :
725 : /* Don't know what to return if there was no zero termination.
726 : Ideally this would turn into a gcc_checking_assert over time.
727 : Set DECL/LEN so callers can examine them. */
728 762507 : if (len >= maxelts - eltoff)
729 : {
730 85459 : data->decl = decl;
731 85459 : data->off = byteoff;
732 85459 : data->minlen = ssize_int (len);
733 85459 : return NULL_TREE;
734 : }
735 :
736 677048 : return ssize_int (len);
737 : }
738 :
739 : /* Return a constant integer corresponding to target reading
740 : GET_MODE_BITSIZE (MODE) bits from string constant STR. If
741 : NULL_TERMINATED_P, reading stops after '\0' character, all further ones
742 : are assumed to be zero, otherwise it reads as many characters
743 : as needed. */
744 :
745 : rtx
746 291997 : c_readstr (const char *str, fixed_size_mode mode,
747 : bool null_terminated_p/*=true*/)
748 : {
749 291997 : auto_vec<target_unit, MAX_BITSIZE_MODE_ANY_INT / BITS_PER_UNIT> bytes;
750 :
751 583994 : bytes.reserve (GET_MODE_SIZE (mode));
752 :
753 291997 : target_unit ch = 1;
754 4853276 : for (unsigned int i = 0; i < GET_MODE_SIZE (mode); ++i)
755 : {
756 2134641 : if (ch || !null_terminated_p)
757 2042146 : ch = (unsigned char) str[i];
758 2134641 : bytes.quick_push (ch);
759 : }
760 :
761 291997 : return native_decode_rtx (mode, bytes, 0);
762 291997 : }
763 :
764 : /* Cast a target constant CST to target CHAR and if that value fits into
765 : host char type, return zero and put that value into variable pointed to by
766 : P. */
767 :
768 : static int
769 62936 : target_char_cast (tree cst, char *p)
770 : {
771 62936 : unsigned HOST_WIDE_INT val, hostval;
772 :
773 62936 : if (TREE_CODE (cst) != INTEGER_CST
774 : || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
775 : return 1;
776 :
777 : /* Do not care if it fits or not right here. */
778 62936 : val = TREE_INT_CST_LOW (cst);
779 :
780 62936 : if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
781 62936 : val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
782 :
783 62936 : hostval = val;
784 62936 : if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
785 62936 : hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
786 :
787 62936 : if (val != hostval)
788 : return 1;
789 :
790 62936 : *p = hostval;
791 62936 : return 0;
792 : }
793 :
794 : /* Similar to save_expr, but assumes that arbitrary code is not executed
795 : in between the multiple evaluations. In particular, we assume that a
796 : non-addressable local variable will not be modified. */
797 :
798 : static tree
799 1138957 : builtin_save_expr (tree exp)
800 : {
801 1138957 : if (TREE_CODE (exp) == SSA_NAME
802 974312 : || (TREE_ADDRESSABLE (exp) == 0
803 974120 : && (TREE_CODE (exp) == PARM_DECL
804 439839 : || (VAR_P (exp) && !TREE_STATIC (exp)))))
805 : return exp;
806 :
807 438030 : return save_expr (exp);
808 : }
809 :
810 : /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
811 : times to get the address of either a higher stack frame, or a return
812 : address located within it (depending on FNDECL_CODE). */
813 :
814 : static rtx
815 15936 : expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
816 : {
817 15936 : int i;
818 15936 : rtx tem = INITIAL_FRAME_ADDRESS_RTX;
819 15936 : if (tem == NULL_RTX)
820 : {
821 : /* For a zero count with __builtin_return_address, we don't care what
822 : frame address we return, because target-specific definitions will
823 : override us. Therefore frame pointer elimination is OK, and using
824 : the soft frame pointer is OK.
825 :
826 : For a nonzero count, or a zero count with __builtin_frame_address,
827 : we require a stable offset from the current frame pointer to the
828 : previous one, so we must use the hard frame pointer, and
829 : we must disable frame pointer elimination. */
830 15936 : if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
831 4333 : tem = frame_pointer_rtx;
832 : else
833 : {
834 11603 : tem = hard_frame_pointer_rtx;
835 :
836 : /* Tell reload not to eliminate the frame pointer. */
837 11603 : crtl->accesses_prior_frames = 1;
838 : }
839 : }
840 :
841 15936 : if (count > 0)
842 938 : SETUP_FRAME_ADDRESSES ();
843 :
844 : /* On the SPARC, the return address is not in the frame, it is in a
845 : register. There is no way to access it off of the current frame
846 : pointer, but it can be accessed off the previous frame pointer by
847 : reading the value from the register window save area. */
848 15936 : if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
849 : count--;
850 :
851 : /* Scan back COUNT frames to the specified frame. */
852 51818 : for (i = 0; i < count; i++)
853 : {
854 : /* Assume the dynamic chain pointer is in the word that the
855 : frame address points to, unless otherwise specified. */
856 35882 : tem = DYNAMIC_CHAIN_ADDRESS (tem);
857 35882 : tem = memory_address (Pmode, tem);
858 35882 : tem = gen_frame_mem (Pmode, tem);
859 35882 : tem = copy_to_reg (tem);
860 : }
861 :
862 : /* For __builtin_frame_address, return what we've got. But, on
863 : the SPARC for example, we may have to add a bias. */
864 15936 : if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
865 : return FRAME_ADDR_RTX (count, tem);
866 :
867 : /* For __builtin_return_address, get the return address from that frame. */
868 : #ifdef RETURN_ADDR_RTX
869 7402 : tem = RETURN_ADDR_RTX (count, tem);
870 : #else
871 : tem = memory_address (Pmode,
872 : plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
873 : tem = gen_frame_mem (Pmode, tem);
874 : #endif
875 5173 : return tem;
876 : }
877 :
878 : /* Alias set used for setjmp buffer. */
879 : static alias_set_type setjmp_alias_set = -1;
880 :
881 : /* Construct the leading half of a __builtin_setjmp call. Control will
882 : return to RECEIVER_LABEL. This is also called directly by the SJLJ
883 : exception handling code. */
884 :
885 : void
886 842 : expand_builtin_setjmp_setup (rtx buf_addr, rtx_insn *receiver_label)
887 : {
888 842 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
889 842 : rtx stack_save;
890 842 : rtx mem;
891 :
892 842 : if (setjmp_alias_set == -1)
893 241 : setjmp_alias_set = new_alias_set ();
894 :
895 843 : buf_addr = convert_memory_address (Pmode, buf_addr);
896 :
897 843 : buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
898 :
899 : /* We store the frame pointer and the address of receiver_label in
900 : the buffer and use the rest of it for the stack save area, which
901 : is machine-dependent. */
902 :
903 843 : mem = gen_rtx_MEM (Pmode, buf_addr);
904 842 : set_mem_alias_set (mem, setjmp_alias_set);
905 842 : emit_move_insn (mem, hard_frame_pointer_rtx);
906 :
907 843 : mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
908 1684 : GET_MODE_SIZE (Pmode))),
909 842 : set_mem_alias_set (mem, setjmp_alias_set);
910 :
911 1684 : emit_move_insn (validize_mem (mem),
912 1684 : force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
913 :
914 842 : stack_save = gen_rtx_MEM (sa_mode,
915 : plus_constant (Pmode, buf_addr,
916 1684 : 2 * GET_MODE_SIZE (Pmode)));
917 842 : set_mem_alias_set (stack_save, setjmp_alias_set);
918 842 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
919 :
920 : /* If there is further processing to do, do it. */
921 842 : if (targetm.have_builtin_setjmp_setup ())
922 0 : emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
923 :
924 : /* We have a nonlocal label. */
925 842 : cfun->has_nonlocal_label = 1;
926 842 : }
927 :
928 : /* Construct the trailing part of a __builtin_setjmp call. This is
929 : also called directly by the SJLJ exception handling code.
930 : If RECEIVER_LABEL is NULL, instead construct a nonlocal goto handler. */
931 :
932 : void
933 1368 : expand_builtin_setjmp_receiver (rtx_insn *receiver_label)
934 : {
935 1368 : rtx chain;
936 :
937 : /* Mark the FP as used when we get here, so we have to make sure it's
938 : marked as used by this function. */
939 1368 : emit_use (hard_frame_pointer_rtx);
940 :
941 : /* Mark the static chain as clobbered here so life information
942 : doesn't get messed up for it. */
943 1368 : chain = rtx_for_static_chain (current_function_decl, true);
944 1368 : if (chain && REG_P (chain))
945 2 : emit_clobber (chain);
946 :
947 1368 : if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
948 : {
949 : /* If the argument pointer can be eliminated in favor of the
950 : frame pointer, we don't need to restore it. We assume here
951 : that if such an elimination is present, it can always be used.
952 : This is the case on all known machines; if we don't make this
953 : assumption, we do unnecessary saving on many machines. */
954 : size_t i;
955 : static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
956 :
957 2736 : for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
958 2736 : if (elim_regs[i].from == ARG_POINTER_REGNUM
959 2736 : && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
960 : break;
961 :
962 1368 : if (i == ARRAY_SIZE (elim_regs))
963 : {
964 : /* Now restore our arg pointer from the address at which it
965 : was saved in our stack frame. */
966 0 : emit_move_insn (crtl->args.internal_arg_pointer,
967 : copy_to_reg (get_arg_pointer_save_area ()));
968 : }
969 : }
970 :
971 1368 : if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
972 0 : emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
973 1368 : else if (targetm.have_nonlocal_goto_receiver ())
974 0 : emit_insn (targetm.gen_nonlocal_goto_receiver ());
975 : else
976 : { /* Nothing */ }
977 :
978 : /* We must not allow the code we just generated to be reordered by
979 : scheduling. Specifically, the update of the frame pointer must
980 : happen immediately, not later. */
981 1368 : emit_insn (gen_blockage ());
982 1368 : }
983 :
984 : /* __builtin_longjmp is passed a pointer to an array of five words (not
985 : all will be used on all machines). It operates similarly to the C
986 : library function of the same name, but is more efficient. Much of
987 : the code below is copied from the handling of non-local gotos. */
988 :
989 : static void
990 392 : expand_builtin_longjmp (rtx buf_addr, rtx value)
991 : {
992 392 : rtx fp, lab, stack;
993 392 : rtx_insn *insn, *last;
994 392 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
995 :
996 : /* DRAP is needed for stack realign if longjmp is expanded to current
997 : function */
998 392 : if (SUPPORTS_STACK_ALIGNMENT)
999 392 : crtl->need_drap = true;
1000 :
1001 392 : if (setjmp_alias_set == -1)
1002 331 : setjmp_alias_set = new_alias_set ();
1003 :
1004 393 : buf_addr = convert_memory_address (Pmode, buf_addr);
1005 :
1006 393 : buf_addr = force_reg (Pmode, buf_addr);
1007 :
1008 : /* We require that the user must pass a second argument of 1, because
1009 : that is what builtin_setjmp will return. */
1010 392 : gcc_assert (value == const1_rtx);
1011 :
1012 392 : last = get_last_insn ();
1013 392 : if (targetm.have_builtin_longjmp ())
1014 0 : emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1015 : else
1016 : {
1017 393 : fp = gen_rtx_MEM (Pmode, buf_addr);
1018 393 : lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1019 784 : GET_MODE_SIZE (Pmode)));
1020 :
1021 392 : stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1022 784 : 2 * GET_MODE_SIZE (Pmode)));
1023 392 : set_mem_alias_set (fp, setjmp_alias_set);
1024 392 : set_mem_alias_set (lab, setjmp_alias_set);
1025 392 : set_mem_alias_set (stack, setjmp_alias_set);
1026 :
1027 : /* Pick up FP, label, and SP from the block and jump. This code is
1028 : from expand_goto in stmt.cc; see there for detailed comments. */
1029 392 : if (targetm.have_nonlocal_goto ())
1030 : /* We have to pass a value to the nonlocal_goto pattern that will
1031 : get copied into the static_chain pointer, but it does not matter
1032 : what that value is, because builtin_setjmp does not use it. */
1033 0 : emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1034 : else
1035 : {
1036 392 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1037 392 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1038 :
1039 392 : lab = copy_to_reg (lab);
1040 :
1041 : /* Restore the frame pointer and stack pointer. We must use a
1042 : temporary since the setjmp buffer may be a local. */
1043 392 : fp = copy_to_reg (fp);
1044 392 : emit_stack_restore (SAVE_NONLOCAL, stack);
1045 :
1046 : /* Ensure the frame pointer move is not optimized. */
1047 392 : emit_insn (gen_blockage ());
1048 392 : emit_clobber (hard_frame_pointer_rtx);
1049 392 : emit_clobber (frame_pointer_rtx);
1050 392 : emit_move_insn (hard_frame_pointer_rtx, fp);
1051 :
1052 392 : emit_use (hard_frame_pointer_rtx);
1053 392 : emit_use (stack_pointer_rtx);
1054 392 : emit_indirect_jump (lab);
1055 : }
1056 : }
1057 :
1058 : /* Search backwards and mark the jump insn as a non-local goto.
1059 : Note that this precludes the use of __builtin_longjmp to a
1060 : __builtin_setjmp target in the same function. However, we've
1061 : already cautioned the user that these functions are for
1062 : internal exception handling use only. */
1063 784 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1064 : {
1065 784 : gcc_assert (insn != last);
1066 :
1067 784 : if (JUMP_P (insn))
1068 : {
1069 392 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1070 392 : break;
1071 : }
1072 392 : else if (CALL_P (insn))
1073 : break;
1074 : }
1075 392 : }
1076 :
1077 : static inline bool
1078 1957126 : more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1079 : {
1080 1957126 : return (iter->i < iter->n);
1081 : }
1082 :
1083 : /* This function validates the types of a function call argument list
1084 : against a specified list of tree_codes. If the last specifier is a 0,
1085 : that represents an ellipsis, otherwise the last specifier must be a
1086 : VOID_TYPE. */
1087 :
1088 : static bool
1089 505807 : validate_arglist (const_tree callexpr, ...)
1090 : {
1091 505807 : enum tree_code code;
1092 505807 : bool res = 0;
1093 505807 : va_list ap;
1094 505807 : const_call_expr_arg_iterator iter;
1095 505807 : const_tree arg;
1096 :
1097 505807 : va_start (ap, callexpr);
1098 505807 : init_const_call_expr_arg_iterator (callexpr, &iter);
1099 :
1100 : /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1101 505807 : tree fn = CALL_EXPR_FN (callexpr);
1102 505807 : bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1103 :
1104 1766685 : for (unsigned argno = 1; ; ++argno)
1105 : {
1106 1766685 : code = (enum tree_code) va_arg (ap, int);
1107 :
1108 1766685 : switch (code)
1109 : {
1110 2061 : case 0:
1111 : /* This signifies an ellipses, any further arguments are all ok. */
1112 2061 : res = true;
1113 2061 : goto end;
1114 503514 : case VOID_TYPE:
1115 : /* This signifies an endlink, if no arguments remain, return
1116 : true, otherwise return false. */
1117 503514 : res = !more_const_call_expr_args_p (&iter);
1118 503514 : goto end;
1119 828299 : case POINTER_TYPE:
1120 : /* The actual argument must be nonnull when either the whole
1121 : called function has been declared nonnull, or when the formal
1122 : argument corresponding to the actual argument has been. */
1123 828299 : if (argmap
1124 828299 : && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1125 : {
1126 280929 : arg = next_const_call_expr_arg (&iter);
1127 280929 : if (!validate_arg (arg, code) || integer_zerop (arg))
1128 58 : goto end;
1129 : break;
1130 : }
1131 : /* FALLTHRU */
1132 980181 : default:
1133 : /* If no parameters remain or the parameter's code does not
1134 : match the specified code, return false. Otherwise continue
1135 : checking any remaining arguments. */
1136 980181 : arg = next_const_call_expr_arg (&iter);
1137 980181 : if (!validate_arg (arg, code))
1138 174 : goto end;
1139 : break;
1140 : }
1141 : }
1142 :
1143 : /* We need gotos here since we can only have one VA_CLOSE in a
1144 : function. */
1145 505807 : end: ;
1146 505807 : va_end (ap);
1147 :
1148 505807 : BITMAP_FREE (argmap);
1149 :
1150 505807 : if (res)
1151 505575 : for (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (TREE_TYPE (fn)));
1152 731846 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
1153 226271 : attrs = TREE_CHAIN (attrs))
1154 : {
1155 226293 : tree args = TREE_VALUE (attrs);
1156 226293 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
1157 226293 : unsigned int idx2
1158 226293 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
1159 226293 : unsigned int idx3 = idx2;
1160 226293 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
1161 0 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
1162 226293 : if (idx < (unsigned) call_expr_nargs (callexpr)
1163 226293 : && idx2 < (unsigned) call_expr_nargs (callexpr)
1164 226293 : && idx3 < (unsigned) call_expr_nargs (callexpr)
1165 226293 : && POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx)))
1166 226293 : && integer_zerop (CALL_EXPR_ARG (callexpr, idx))
1167 103 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx2)))
1168 103 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx2))
1169 22 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx3)))
1170 226315 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx3)))
1171 : return false;
1172 : }
1173 :
1174 : return res;
1175 : }
1176 :
1177 : /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1178 : and the address of the save area. */
1179 :
1180 : static rtx
1181 525 : expand_builtin_nonlocal_goto (tree exp)
1182 : {
1183 525 : tree t_label, t_save_area;
1184 525 : rtx r_label, r_save_area, r_fp, r_sp;
1185 525 : rtx_insn *insn;
1186 :
1187 525 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1188 : return NULL_RTX;
1189 :
1190 525 : t_label = CALL_EXPR_ARG (exp, 0);
1191 525 : t_save_area = CALL_EXPR_ARG (exp, 1);
1192 :
1193 525 : r_label = expand_normal (t_label);
1194 525 : r_label = convert_memory_address (Pmode, r_label);
1195 525 : r_save_area = expand_normal (t_save_area);
1196 525 : r_save_area = convert_memory_address (Pmode, r_save_area);
1197 : /* Copy the address of the save location to a register just in case it was
1198 : based on the frame pointer. */
1199 525 : r_save_area = copy_to_reg (r_save_area);
1200 525 : r_fp = gen_rtx_MEM (Pmode, r_save_area);
1201 525 : r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1202 : plus_constant (Pmode, r_save_area,
1203 1050 : GET_MODE_SIZE (Pmode)));
1204 :
1205 525 : crtl->has_nonlocal_goto = 1;
1206 :
1207 : /* ??? We no longer need to pass the static chain value, afaik. */
1208 525 : if (targetm.have_nonlocal_goto ())
1209 0 : emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1210 : else
1211 : {
1212 525 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1213 525 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1214 :
1215 525 : r_label = copy_to_reg (r_label);
1216 :
1217 : /* Restore the frame pointer and stack pointer. We must use a
1218 : temporary since the setjmp buffer may be a local. */
1219 525 : r_fp = copy_to_reg (r_fp);
1220 525 : emit_stack_restore (SAVE_NONLOCAL, r_sp);
1221 :
1222 : /* Ensure the frame pointer move is not optimized. */
1223 525 : emit_insn (gen_blockage ());
1224 525 : emit_clobber (hard_frame_pointer_rtx);
1225 525 : emit_clobber (frame_pointer_rtx);
1226 525 : emit_move_insn (hard_frame_pointer_rtx, r_fp);
1227 :
1228 : /* USE of hard_frame_pointer_rtx added for consistency;
1229 : not clear if really needed. */
1230 525 : emit_use (hard_frame_pointer_rtx);
1231 525 : emit_use (stack_pointer_rtx);
1232 :
1233 : /* If the architecture is using a GP register, we must
1234 : conservatively assume that the target function makes use of it.
1235 : The prologue of functions with nonlocal gotos must therefore
1236 : initialize the GP register to the appropriate value, and we
1237 : must then make sure that this value is live at the point
1238 : of the jump. (Note that this doesn't necessarily apply
1239 : to targets with a nonlocal_goto pattern; they are free
1240 : to implement it in their own way. Note also that this is
1241 : a no-op if the GP register is a global invariant.) */
1242 525 : unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1243 0 : if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1244 0 : emit_use (pic_offset_table_rtx);
1245 :
1246 525 : emit_indirect_jump (r_label);
1247 : }
1248 :
1249 : /* Search backwards to the jump insn and mark it as a
1250 : non-local goto. */
1251 1050 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1252 : {
1253 1050 : if (JUMP_P (insn))
1254 : {
1255 525 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1256 525 : break;
1257 : }
1258 525 : else if (CALL_P (insn))
1259 : break;
1260 : }
1261 :
1262 525 : return const0_rtx;
1263 : }
1264 :
1265 : /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1266 : (not all will be used on all machines) that was passed to __builtin_setjmp.
1267 : It updates the stack pointer in that block to the current value. This is
1268 : also called directly by the SJLJ exception handling code. */
1269 :
1270 : void
1271 0 : expand_builtin_update_setjmp_buf (rtx buf_addr)
1272 : {
1273 0 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1274 0 : buf_addr = convert_memory_address (Pmode, buf_addr);
1275 0 : rtx stack_save
1276 0 : = gen_rtx_MEM (sa_mode,
1277 0 : memory_address
1278 : (sa_mode,
1279 : plus_constant (Pmode, buf_addr,
1280 0 : 2 * GET_MODE_SIZE (Pmode))));
1281 :
1282 0 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
1283 0 : }
1284 :
1285 : /* Expand a call to __builtin_prefetch. For a target that does not support
1286 : data prefetch, evaluate the memory address argument in case it has side
1287 : effects. */
1288 :
1289 : static void
1290 2061 : expand_builtin_prefetch (tree exp)
1291 : {
1292 2061 : tree arg0, arg1, arg2;
1293 2061 : int nargs;
1294 2061 : rtx op0, op1, op2;
1295 :
1296 2061 : if (!validate_arglist (exp, POINTER_TYPE, 0))
1297 : return;
1298 :
1299 2061 : arg0 = CALL_EXPR_ARG (exp, 0);
1300 :
1301 : /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1302 : zero (read) and argument 2 (locality) defaults to 3 (high degree of
1303 : locality). */
1304 2061 : nargs = call_expr_nargs (exp);
1305 2061 : arg1 = nargs > 1 ? CALL_EXPR_ARG (exp, 1) : NULL_TREE;
1306 2021 : arg2 = nargs > 2 ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
1307 :
1308 : /* Argument 0 is an address. */
1309 2157 : op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1310 :
1311 : /* Argument 1 (read/write flag) must be a compile-time constant int. */
1312 2061 : if (arg1 == NULL_TREE)
1313 40 : op1 = const0_rtx;
1314 2021 : else if (TREE_CODE (arg1) != INTEGER_CST)
1315 : {
1316 0 : error ("second argument to %<__builtin_prefetch%> must be a constant");
1317 0 : op1 = const0_rtx;
1318 : }
1319 : else
1320 2021 : op1 = expand_normal (arg1);
1321 : /* Argument 1 must be 0, 1 or 2. */
1322 2061 : if (!IN_RANGE (INTVAL (op1), 0, 2))
1323 : {
1324 3 : warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1325 : " using zero");
1326 3 : op1 = const0_rtx;
1327 : }
1328 :
1329 : /* Argument 2 (locality) must be a compile-time constant int. */
1330 2061 : if (arg2 == NULL_TREE)
1331 77 : op2 = GEN_INT (3);
1332 1984 : else if (TREE_CODE (arg2) != INTEGER_CST)
1333 : {
1334 0 : error ("third argument to %<__builtin_prefetch%> must be a constant");
1335 0 : op2 = const0_rtx;
1336 : }
1337 : else
1338 1984 : op2 = expand_normal (arg2);
1339 : /* Argument 2 must be 0, 1, 2, or 3. */
1340 2061 : if (!IN_RANGE (INTVAL (op2), 0, 3))
1341 : {
1342 3 : warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1343 3 : op2 = const0_rtx;
1344 : }
1345 :
1346 2061 : if (targetm.have_prefetch ())
1347 : {
1348 2061 : class expand_operand ops[3];
1349 :
1350 2061 : create_address_operand (&ops[0], op0);
1351 2061 : create_integer_operand (&ops[1], INTVAL (op1));
1352 2061 : create_integer_operand (&ops[2], INTVAL (op2));
1353 2061 : if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1354 2061 : return;
1355 : }
1356 :
1357 : /* Don't do anything with direct references to volatile memory, but
1358 : generate code to handle other side effects. */
1359 0 : if (!MEM_P (op0) && side_effects_p (op0))
1360 0 : emit_insn (op0);
1361 : }
1362 :
1363 : /* Get a MEM rtx for expression EXP which is the address of an operand
1364 : to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1365 : the maximum length of the block of memory that might be accessed or
1366 : NULL if unknown. STORE_P is true when EXP is the destination. */
1367 :
1368 : rtx
1369 759881 : get_memory_rtx (tree exp, tree len, bool store_p)
1370 : {
1371 759881 : tree orig_exp = exp, base;
1372 759881 : rtx addr, mem;
1373 :
1374 759881 : gcc_checking_assert
1375 : (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))));
1376 :
1377 : /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1378 : from its expression, for expr->a.b only <variable>.a.b is recorded. */
1379 759881 : if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1380 1696 : exp = TREE_OPERAND (exp, 0);
1381 :
1382 759881 : addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1383 759881 : mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1384 :
1385 : /* Get an expression we can use to find the attributes to assign to MEM.
1386 : First remove any nops. */
1387 1519762 : while (CONVERT_EXPR_P (exp)
1388 759884 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1389 3 : exp = TREE_OPERAND (exp, 0);
1390 :
1391 : /* Build a MEM_REF representing the whole accessed area as a byte blob,
1392 : (as builtin stringops may alias with anything). */
1393 759881 : exp = fold_build2 (MEM_REF,
1394 : build_array_type (char_type_node,
1395 : build_range_type (sizetype,
1396 : size_one_node, len)),
1397 : exp, build_int_cst (ptr_type_node, 0));
1398 :
1399 : /* If the MEM_REF has no acceptable address, try to get the base object
1400 : from the original address we got, and build an all-aliasing
1401 : unknown-sized access to that one. */
1402 759881 : if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1403 759869 : set_mem_attributes (mem, exp, 0, store_p);
1404 12 : else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1405 12 : && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1406 : 0))))
1407 : {
1408 12 : unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1409 12 : exp = build_fold_addr_expr (base);
1410 12 : exp = fold_build2 (MEM_REF,
1411 : build_array_type (char_type_node,
1412 : build_range_type (sizetype,
1413 : size_zero_node,
1414 : NULL)),
1415 : exp, build_int_cst (ptr_type_node, 0));
1416 12 : set_mem_attributes (mem, exp, 0, store_p);
1417 : /* Since we stripped parts make sure the offset is unknown and the
1418 : alignment is computed from the original address. */
1419 12 : clear_mem_offset (mem);
1420 12 : set_mem_align (mem, align);
1421 : }
1422 759881 : set_mem_alias_set (mem, 0);
1423 759881 : return mem;
1424 : }
1425 :
1426 : /* Built-in functions to perform an untyped call and return. */
1427 :
1428 : /* Wrapper that implicitly applies a delta when getting or setting the
1429 : enclosed value. */
1430 : template <typename T>
1431 : class delta_type
1432 : {
1433 : T &value; T const delta;
1434 : public:
1435 2524 : delta_type (T &val, T dlt) : value (val), delta (dlt) {}
1436 1809 : operator T () const { return value + delta; }
1437 715 : T operator = (T val) const { value = val - delta; return val; }
1438 : };
1439 :
1440 : #define saved_apply_args_size \
1441 : (delta_type<int> (this_target_builtins->x_apply_args_size_plus_one, -1))
1442 : #define apply_args_mode \
1443 : (this_target_builtins->x_apply_args_mode)
1444 : #define saved_apply_result_size \
1445 : (delta_type<int> (this_target_builtins->x_apply_result_size_plus_one, -1))
1446 : #define apply_result_mode \
1447 : (this_target_builtins->x_apply_result_mode)
1448 :
1449 : /* Return the size required for the block returned by __builtin_apply_args,
1450 : and initialize apply_args_mode. */
1451 :
1452 : static int
1453 947 : apply_args_size (void)
1454 : {
1455 947 : int size = saved_apply_args_size;
1456 947 : int align;
1457 947 : unsigned int regno;
1458 :
1459 : /* The values computed by this function never change. */
1460 947 : if (size < 0)
1461 : {
1462 : /* The first value is the incoming arg-pointer. */
1463 360 : size = GET_MODE_SIZE (Pmode);
1464 :
1465 : /* The second value is the structure value address unless this is
1466 : passed as an "invisible" first argument. */
1467 360 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1468 0 : size += GET_MODE_SIZE (Pmode);
1469 :
1470 34200 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1471 33840 : if (FUNCTION_ARG_REGNO_P (regno))
1472 : {
1473 5400 : fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1474 :
1475 5400 : if (mode != VOIDmode)
1476 : {
1477 5400 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1478 5400 : if (size % align != 0)
1479 1 : size = CEIL (size, align) * align;
1480 5400 : size += GET_MODE_SIZE (mode);
1481 5400 : apply_args_mode[regno] = mode;
1482 : }
1483 : else
1484 0 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1485 : }
1486 : else
1487 28440 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1488 :
1489 360 : saved_apply_args_size = size;
1490 : }
1491 947 : return size;
1492 : }
1493 :
1494 : /* Return the size required for the block returned by __builtin_apply,
1495 : and initialize apply_result_mode. */
1496 :
1497 : static int
1498 862 : apply_result_size (void)
1499 : {
1500 862 : int size = saved_apply_result_size;
1501 862 : int align, regno;
1502 :
1503 : /* The values computed by this function never change. */
1504 862 : if (size < 0)
1505 : {
1506 : size = 0;
1507 :
1508 33725 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1509 33370 : if (targetm.calls.function_value_regno_p (regno))
1510 : {
1511 2130 : fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1512 :
1513 2130 : if (mode != VOIDmode)
1514 : {
1515 2130 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1516 2130 : if (size % align != 0)
1517 : size = CEIL (size, align) * align;
1518 2130 : size += GET_MODE_SIZE (mode);
1519 2130 : apply_result_mode[regno] = mode;
1520 : }
1521 : else
1522 0 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1523 : }
1524 : else
1525 31240 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1526 :
1527 : /* Allow targets that use untyped_call and untyped_return to override
1528 : the size so that machine-specific information can be stored here. */
1529 : #ifdef APPLY_RESULT_SIZE
1530 355 : size = APPLY_RESULT_SIZE;
1531 : #endif
1532 :
1533 355 : saved_apply_result_size = size;
1534 : }
1535 862 : return size;
1536 : }
1537 :
1538 : /* Create a vector describing the result block RESULT. If SAVEP is true,
1539 : the result block is used to save the values; otherwise it is used to
1540 : restore the values. */
1541 :
1542 : static rtx
1543 483 : result_vector (int savep, rtx result)
1544 : {
1545 483 : int regno, size, align, nelts;
1546 483 : fixed_size_mode mode;
1547 483 : rtx reg, mem;
1548 483 : rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1549 :
1550 483 : size = nelts = 0;
1551 45885 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1552 45402 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1553 : {
1554 2898 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1555 2898 : if (size % align != 0)
1556 0 : size = CEIL (size, align) * align;
1557 2898 : reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1558 2898 : mem = adjust_address (result, mode, size);
1559 5796 : savevec[nelts++] = (savep
1560 5796 : ? gen_rtx_SET (mem, reg)
1561 0 : : gen_rtx_SET (reg, mem));
1562 5796 : size += GET_MODE_SIZE (mode);
1563 : }
1564 483 : return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1565 : }
1566 :
1567 : /* Save the state required to perform an untyped call with the same
1568 : arguments as were passed to the current function. */
1569 :
1570 : static rtx
1571 464 : expand_builtin_apply_args_1 (void)
1572 : {
1573 464 : rtx registers, tem;
1574 464 : int size, align, regno;
1575 464 : fixed_size_mode mode;
1576 464 : rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1577 :
1578 : /* Create a block where the arg-pointer, structure value address,
1579 : and argument registers can be saved. */
1580 464 : registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1581 :
1582 : /* Walk past the arg-pointer and structure value address. */
1583 464 : size = GET_MODE_SIZE (Pmode);
1584 464 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1585 0 : size += GET_MODE_SIZE (Pmode);
1586 :
1587 : /* Save each register used in calling a function to the block. */
1588 44080 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1589 43616 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1590 : {
1591 6960 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1592 6960 : if (size % align != 0)
1593 1 : size = CEIL (size, align) * align;
1594 :
1595 6960 : tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1596 :
1597 6960 : emit_move_insn (adjust_address (registers, mode, size), tem);
1598 13920 : size += GET_MODE_SIZE (mode);
1599 : }
1600 :
1601 : /* Save the arg pointer to the block. */
1602 464 : tem = copy_to_reg (crtl->args.internal_arg_pointer);
1603 : /* We need the pointer as the caller actually passed them to us, not
1604 : as we might have pretended they were passed. Make sure it's a valid
1605 : operand, as emit_move_insn isn't expected to handle a PLUS. */
1606 464 : if (STACK_GROWS_DOWNWARD)
1607 464 : tem
1608 464 : = force_operand (plus_constant (Pmode, tem,
1609 464 : crtl->args.pretend_args_size),
1610 : NULL_RTX);
1611 464 : emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1612 :
1613 464 : size = GET_MODE_SIZE (Pmode);
1614 :
1615 : /* Save the structure value address unless this is passed as an
1616 : "invisible" first argument. */
1617 464 : if (struct_incoming_value)
1618 0 : emit_move_insn (adjust_address (registers, Pmode, size),
1619 : copy_to_reg (struct_incoming_value));
1620 :
1621 : /* Return the address of the block. */
1622 464 : return copy_addr_to_reg (XEXP (registers, 0));
1623 : }
1624 :
1625 : /* __builtin_apply_args returns block of memory allocated on
1626 : the stack into which is stored the arg pointer, structure
1627 : value address, static chain, and all the registers that might
1628 : possibly be used in performing a function call. The code is
1629 : moved to the start of the function so the incoming values are
1630 : saved. */
1631 :
1632 : static rtx
1633 464 : expand_builtin_apply_args (void)
1634 : {
1635 : /* Don't do __builtin_apply_args more than once in a function.
1636 : Save the result of the first call and reuse it. */
1637 464 : if (apply_args_value != 0)
1638 : return apply_args_value;
1639 464 : {
1640 : /* When this function is called, it means that registers must be
1641 : saved on entry to this function. So we migrate the
1642 : call to the first insn of this function. */
1643 464 : rtx temp;
1644 :
1645 464 : start_sequence ();
1646 464 : temp = expand_builtin_apply_args_1 ();
1647 464 : rtx_insn *seq = end_sequence ();
1648 :
1649 464 : apply_args_value = temp;
1650 :
1651 : /* Put the insns after the NOTE that starts the function.
1652 : If this is inside a start_sequence, make the outer-level insn
1653 : chain current, so the code is placed at the start of the
1654 : function. If internal_arg_pointer is a non-virtual pseudo,
1655 : it needs to be placed after the function that initializes
1656 : that pseudo. */
1657 464 : push_topmost_sequence ();
1658 464 : if (REG_P (crtl->args.internal_arg_pointer)
1659 464 : && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1660 0 : emit_insn_before (seq, parm_birth_insn);
1661 : else
1662 464 : emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1663 464 : pop_topmost_sequence ();
1664 464 : return temp;
1665 : }
1666 : }
1667 :
1668 : /* Perform an untyped call and save the state required to perform an
1669 : untyped return of whatever value was returned by the given function. */
1670 :
1671 : static rtx
1672 483 : expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1673 : {
1674 483 : int size, align, regno;
1675 483 : fixed_size_mode mode;
1676 483 : rtx incoming_args, result, reg, dest, src;
1677 483 : rtx_call_insn *call_insn;
1678 483 : rtx old_stack_level = 0;
1679 483 : rtx call_fusage = 0;
1680 483 : rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1681 :
1682 483 : arguments = convert_memory_address (Pmode, arguments);
1683 :
1684 : /* Create a block where the return registers can be saved. */
1685 483 : result = assign_stack_local (BLKmode, apply_result_size (), -1);
1686 :
1687 : /* Fetch the arg pointer from the ARGUMENTS block. */
1688 483 : incoming_args = gen_reg_rtx (Pmode);
1689 483 : emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1690 483 : if (!STACK_GROWS_DOWNWARD)
1691 : incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1692 : incoming_args, 0, OPTAB_LIB_WIDEN);
1693 :
1694 : /* Push a new argument block and copy the arguments. Do not allow
1695 : the (potential) memcpy call below to interfere with our stack
1696 : manipulations. */
1697 483 : do_pending_stack_adjust ();
1698 483 : NO_DEFER_POP;
1699 :
1700 : /* Save the stack with nonlocal if available. */
1701 483 : if (targetm.have_save_stack_nonlocal ())
1702 483 : emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1703 : else
1704 0 : emit_stack_save (SAVE_BLOCK, &old_stack_level);
1705 :
1706 : /* Allocate a block of memory onto the stack and copy the memory
1707 : arguments to the outgoing arguments address. We can pass TRUE
1708 : as the 4th argument because we just saved the stack pointer
1709 : and will restore it right after the call. */
1710 966 : allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1711 :
1712 : /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1713 : may have already set current_function_calls_alloca to true.
1714 : current_function_calls_alloca won't be set if argsize is zero,
1715 : so we have to guarantee need_drap is true here. */
1716 483 : if (SUPPORTS_STACK_ALIGNMENT)
1717 483 : crtl->need_drap = true;
1718 :
1719 483 : dest = virtual_outgoing_args_rtx;
1720 483 : if (!STACK_GROWS_DOWNWARD)
1721 : {
1722 : if (CONST_INT_P (argsize))
1723 : dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1724 : else
1725 : dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1726 : }
1727 483 : dest = gen_rtx_MEM (BLKmode, dest);
1728 483 : set_mem_align (dest, PARM_BOUNDARY);
1729 483 : src = gen_rtx_MEM (BLKmode, incoming_args);
1730 483 : set_mem_align (src, PARM_BOUNDARY);
1731 483 : emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1732 :
1733 : /* Refer to the argument block. */
1734 483 : apply_args_size ();
1735 483 : arguments = gen_rtx_MEM (BLKmode, arguments);
1736 483 : set_mem_align (arguments, PARM_BOUNDARY);
1737 :
1738 : /* Walk past the arg-pointer and structure value address. */
1739 483 : size = GET_MODE_SIZE (Pmode);
1740 483 : if (struct_value)
1741 0 : size += GET_MODE_SIZE (Pmode);
1742 :
1743 : /* Restore each of the registers previously saved. Make USE insns
1744 : for each of these registers for use in making the call. */
1745 45885 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1746 45402 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1747 : {
1748 7245 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1749 7245 : if (size % align != 0)
1750 0 : size = CEIL (size, align) * align;
1751 7245 : reg = gen_rtx_REG (mode, regno);
1752 7245 : emit_move_insn (reg, adjust_address (arguments, mode, size));
1753 7245 : use_reg (&call_fusage, reg);
1754 14490 : size += GET_MODE_SIZE (mode);
1755 : }
1756 :
1757 : /* Restore the structure value address unless this is passed as an
1758 : "invisible" first argument. */
1759 483 : size = GET_MODE_SIZE (Pmode);
1760 483 : if (struct_value)
1761 : {
1762 0 : rtx value = gen_reg_rtx (Pmode);
1763 0 : emit_move_insn (value, adjust_address (arguments, Pmode, size));
1764 0 : emit_move_insn (struct_value, value);
1765 0 : if (REG_P (struct_value))
1766 0 : use_reg (&call_fusage, struct_value);
1767 : }
1768 :
1769 : /* All arguments and registers used for the call are set up by now! */
1770 483 : function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1771 :
1772 : /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1773 : and we don't want to load it into a register as an optimization,
1774 : because prepare_call_address already did it if it should be done. */
1775 483 : if (GET_CODE (function) != SYMBOL_REF)
1776 29 : function = memory_address (FUNCTION_MODE, function);
1777 :
1778 : /* Generate the actual call instruction and save the return value. */
1779 483 : if (targetm.have_untyped_call ())
1780 : {
1781 483 : rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1782 483 : rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1783 : result_vector (1, result));
1784 5317 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1785 4351 : if (CALL_P (insn))
1786 483 : add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1787 483 : emit_insn (seq);
1788 : }
1789 0 : else if (targetm.have_call_value ())
1790 : {
1791 : rtx valreg = 0;
1792 :
1793 : /* Locate the unique return register. It is not possible to
1794 : express a call that sets more than one return register using
1795 : call_value; use untyped_call for that. In fact, untyped_call
1796 : only needs to save the return registers in the given block. */
1797 0 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1798 0 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1799 : {
1800 0 : gcc_assert (!valreg); /* have_untyped_call required. */
1801 :
1802 0 : valreg = gen_rtx_REG (mode, regno);
1803 : }
1804 :
1805 0 : emit_insn (targetm.gen_call_value (valreg,
1806 : gen_rtx_MEM (FUNCTION_MODE, function),
1807 : const0_rtx, NULL_RTX, const0_rtx));
1808 :
1809 0 : emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1810 : }
1811 : else
1812 0 : gcc_unreachable ();
1813 :
1814 : /* Find the CALL insn we just emitted, and attach the register usage
1815 : information. */
1816 483 : call_insn = last_call_insn ();
1817 483 : add_function_usage_to (call_insn, call_fusage);
1818 :
1819 : /* Restore the stack. */
1820 483 : if (targetm.have_save_stack_nonlocal ())
1821 483 : emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1822 : else
1823 0 : emit_stack_restore (SAVE_BLOCK, old_stack_level);
1824 483 : fixup_args_size_notes (call_insn, get_last_insn (), 0);
1825 :
1826 483 : OK_DEFER_POP;
1827 :
1828 : /* Return the address of the result block. */
1829 483 : result = copy_addr_to_reg (XEXP (result, 0));
1830 483 : return convert_memory_address (ptr_mode, result);
1831 : }
1832 :
1833 : /* Perform an untyped return. */
1834 :
1835 : static void
1836 379 : expand_builtin_return (rtx result)
1837 : {
1838 379 : int size, align, regno;
1839 379 : fixed_size_mode mode;
1840 379 : rtx reg;
1841 379 : rtx_insn *call_fusage = 0;
1842 :
1843 379 : result = convert_memory_address (Pmode, result);
1844 :
1845 379 : apply_result_size ();
1846 379 : result = gen_rtx_MEM (BLKmode, result);
1847 :
1848 379 : if (targetm.have_untyped_return ())
1849 : {
1850 0 : rtx vector = result_vector (0, result);
1851 0 : emit_jump_insn (targetm.gen_untyped_return (result, vector));
1852 0 : emit_barrier ();
1853 0 : return;
1854 : }
1855 :
1856 : /* Restore the return value and note that each value is used. */
1857 : size = 0;
1858 36005 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1859 35626 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1860 : {
1861 2274 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1862 2274 : if (size % align != 0)
1863 7 : size = CEIL (size, align) * align;
1864 2274 : reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1865 2274 : emit_move_insn (reg, adjust_address (result, mode, size));
1866 :
1867 2274 : push_to_sequence (call_fusage);
1868 2274 : emit_use (reg);
1869 2274 : call_fusage = end_sequence ();
1870 4548 : size += GET_MODE_SIZE (mode);
1871 : }
1872 :
1873 : /* Put the USE insns before the return. */
1874 379 : emit_insn (call_fusage);
1875 :
1876 : /* Return whatever values was restored by jumping directly to the end
1877 : of the function. */
1878 379 : expand_naked_return ();
1879 : }
1880 :
1881 : /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1882 :
1883 : int
1884 2383 : type_to_class (tree type)
1885 : {
1886 2383 : switch (TREE_CODE (type))
1887 : {
1888 : case VOID_TYPE: return void_type_class;
1889 905 : case INTEGER_TYPE: return integer_type_class;
1890 39 : case ENUMERAL_TYPE: return enumeral_type_class;
1891 38 : case BOOLEAN_TYPE: return boolean_type_class;
1892 991 : case POINTER_TYPE: return pointer_type_class;
1893 27 : case REFERENCE_TYPE: return reference_type_class;
1894 36 : case OFFSET_TYPE: return offset_type_class;
1895 132 : case REAL_TYPE: return real_type_class;
1896 40 : case COMPLEX_TYPE: return complex_type_class;
1897 20 : case FUNCTION_TYPE: return function_type_class;
1898 0 : case METHOD_TYPE: return method_type_class;
1899 44 : case RECORD_TYPE: return record_type_class;
1900 44 : case UNION_TYPE:
1901 44 : case QUAL_UNION_TYPE: return union_type_class;
1902 24 : case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1903 24 : ? string_type_class : array_type_class);
1904 0 : case LANG_TYPE: return lang_type_class;
1905 0 : case OPAQUE_TYPE: return opaque_type_class;
1906 0 : case BITINT_TYPE: return bitint_type_class;
1907 30 : case VECTOR_TYPE: return vector_type_class;
1908 3 : default: return no_type_class;
1909 : }
1910 : }
1911 :
1912 : /* Expand a call EXP to __builtin_classify_type. */
1913 :
1914 : static rtx
1915 0 : expand_builtin_classify_type (tree exp)
1916 : {
1917 0 : if (call_expr_nargs (exp))
1918 0 : return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1919 0 : return GEN_INT (no_type_class);
1920 : }
1921 :
1922 : /* This helper macro, meant to be used in mathfn_built_in below, determines
1923 : which among a set of builtin math functions is appropriate for a given type
1924 : mode. The `F' (float) and `L' (long double) are automatically generated
1925 : from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1926 : types, there are additional types that are considered with 'F32', 'F64',
1927 : 'F128', etc. suffixes. */
1928 : #define CASE_MATHFN(MATHFN) \
1929 : CASE_CFN_##MATHFN: \
1930 : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1931 : fcodel = BUILT_IN_##MATHFN##L ; break;
1932 : /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1933 : types. */
1934 : #define CASE_MATHFN_FLOATN(MATHFN) \
1935 : CASE_CFN_##MATHFN: \
1936 : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1937 : fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1938 : fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1939 : fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1940 : fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1941 : break;
1942 : /* Similar to above, but appends _R after any F/L suffix. */
1943 : #define CASE_MATHFN_REENT(MATHFN) \
1944 : case CFN_BUILT_IN_##MATHFN##_R: \
1945 : case CFN_BUILT_IN_##MATHFN##F_R: \
1946 : case CFN_BUILT_IN_##MATHFN##L_R: \
1947 : fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1948 : fcodel = BUILT_IN_##MATHFN##L_R ; break;
1949 :
1950 : /* Return a function equivalent to FN but operating on floating-point
1951 : values of type TYPE, or END_BUILTINS if no such function exists.
1952 : This is purely an operation on function codes; it does not guarantee
1953 : that the target actually has an implementation of the function. */
1954 :
1955 : static built_in_function
1956 614783 : mathfn_built_in_2 (tree type, combined_fn fn)
1957 : {
1958 614783 : tree mtype;
1959 614783 : built_in_function fcode, fcodef, fcodel;
1960 614783 : built_in_function fcodef16 = END_BUILTINS;
1961 614783 : built_in_function fcodef32 = END_BUILTINS;
1962 614783 : built_in_function fcodef64 = END_BUILTINS;
1963 614783 : built_in_function fcodef128 = END_BUILTINS;
1964 614783 : built_in_function fcodef32x = END_BUILTINS;
1965 614783 : built_in_function fcodef64x = END_BUILTINS;
1966 614783 : built_in_function fcodef128x = END_BUILTINS;
1967 :
1968 : /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1969 : break the uses below. */
1970 : #undef HUGE_VAL
1971 : #undef NAN
1972 :
1973 614783 : switch (fn)
1974 : {
1975 : #define SEQ_OF_CASE_MATHFN \
1976 : CASE_MATHFN_FLOATN (ACOS) \
1977 : CASE_MATHFN_FLOATN (ACOSH) \
1978 : CASE_MATHFN_FLOATN (ACOSPI) \
1979 : CASE_MATHFN_FLOATN (ASIN) \
1980 : CASE_MATHFN_FLOATN (ASINH) \
1981 : CASE_MATHFN_FLOATN (ASINPI) \
1982 : CASE_MATHFN_FLOATN (ATAN) \
1983 : CASE_MATHFN_FLOATN (ATAN2) \
1984 : CASE_MATHFN_FLOATN (ATAN2PI) \
1985 : CASE_MATHFN_FLOATN (ATANH) \
1986 : CASE_MATHFN_FLOATN (ATANPI) \
1987 : CASE_MATHFN_FLOATN (CBRT) \
1988 : CASE_MATHFN_FLOATN (CEIL) \
1989 : CASE_MATHFN (CEXPI) \
1990 : CASE_MATHFN_FLOATN (COPYSIGN) \
1991 : CASE_MATHFN_FLOATN (COS) \
1992 : CASE_MATHFN_FLOATN (COSH) \
1993 : CASE_MATHFN_FLOATN (COSPI) \
1994 : CASE_MATHFN (DREM) \
1995 : CASE_MATHFN_FLOATN (ERF) \
1996 : CASE_MATHFN_FLOATN (ERFC) \
1997 : CASE_MATHFN_FLOATN (EXP) \
1998 : CASE_MATHFN (EXP10) \
1999 : CASE_MATHFN_FLOATN (EXP2) \
2000 : CASE_MATHFN_FLOATN (EXPM1) \
2001 : CASE_MATHFN_FLOATN (FABS) \
2002 : CASE_MATHFN_FLOATN (FDIM) \
2003 : CASE_MATHFN_FLOATN (FLOOR) \
2004 : CASE_MATHFN_FLOATN (FMA) \
2005 : CASE_MATHFN_FLOATN (FMAX) \
2006 : CASE_MATHFN_FLOATN (FMIN) \
2007 : CASE_MATHFN_FLOATN (FMOD) \
2008 : CASE_MATHFN_FLOATN (FREXP) \
2009 : CASE_MATHFN (GAMMA) \
2010 : CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2011 : CASE_MATHFN_FLOATN (HUGE_VAL) \
2012 : CASE_MATHFN_FLOATN (HYPOT) \
2013 : CASE_MATHFN_FLOATN (ILOGB) \
2014 : CASE_MATHFN (ICEIL) \
2015 : CASE_MATHFN (IFLOOR) \
2016 : CASE_MATHFN_FLOATN (INF) \
2017 : CASE_MATHFN (IRINT) \
2018 : CASE_MATHFN (IROUND) \
2019 : CASE_MATHFN (ISINF) \
2020 : CASE_MATHFN (J0) \
2021 : CASE_MATHFN (J1) \
2022 : CASE_MATHFN (JN) \
2023 : CASE_MATHFN (LCEIL) \
2024 : CASE_MATHFN_FLOATN (LDEXP) \
2025 : CASE_MATHFN (LFLOOR) \
2026 : CASE_MATHFN_FLOATN (LGAMMA) \
2027 : CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2028 : CASE_MATHFN (LLCEIL) \
2029 : CASE_MATHFN (LLFLOOR) \
2030 : CASE_MATHFN_FLOATN (LLRINT) \
2031 : CASE_MATHFN_FLOATN (LLROUND) \
2032 : CASE_MATHFN_FLOATN (LOG) \
2033 : CASE_MATHFN_FLOATN (LOG10) \
2034 : CASE_MATHFN_FLOATN (LOG1P) \
2035 : CASE_MATHFN_FLOATN (LOG2) \
2036 : CASE_MATHFN_FLOATN (LOGB) \
2037 : CASE_MATHFN_FLOATN (LRINT) \
2038 : CASE_MATHFN_FLOATN (LROUND) \
2039 : CASE_MATHFN_FLOATN (MODF) \
2040 : CASE_MATHFN_FLOATN (NAN) \
2041 : CASE_MATHFN_FLOATN (NANS) \
2042 : CASE_MATHFN_FLOATN (NEARBYINT) \
2043 : CASE_MATHFN_FLOATN (NEXTAFTER) \
2044 : CASE_MATHFN (NEXTTOWARD) \
2045 : CASE_MATHFN_FLOATN (POW) \
2046 : CASE_MATHFN (POWI) \
2047 : CASE_MATHFN (POW10) \
2048 : CASE_MATHFN_FLOATN (REMAINDER) \
2049 : CASE_MATHFN_FLOATN (REMQUO) \
2050 : CASE_MATHFN_FLOATN (RINT) \
2051 : CASE_MATHFN_FLOATN (ROUND) \
2052 : CASE_MATHFN_FLOATN (ROUNDEVEN) \
2053 : CASE_MATHFN (SCALB) \
2054 : CASE_MATHFN_FLOATN (SCALBLN) \
2055 : CASE_MATHFN_FLOATN (SCALBN) \
2056 : CASE_MATHFN (SIGNBIT) \
2057 : CASE_MATHFN (SIGNIFICAND) \
2058 : CASE_MATHFN_FLOATN (SIN) \
2059 : CASE_MATHFN (SINCOS) \
2060 : CASE_MATHFN_FLOATN (SINH) \
2061 : CASE_MATHFN_FLOATN (SINPI) \
2062 : CASE_MATHFN_FLOATN (SQRT) \
2063 : CASE_MATHFN_FLOATN (TAN) \
2064 : CASE_MATHFN_FLOATN (TANH) \
2065 : CASE_MATHFN_FLOATN (TANPI) \
2066 : CASE_MATHFN_FLOATN (TGAMMA) \
2067 : CASE_MATHFN_FLOATN (TRUNC) \
2068 : CASE_MATHFN (Y0) \
2069 : CASE_MATHFN (Y1) \
2070 : CASE_MATHFN (YN)
2071 :
2072 614687 : SEQ_OF_CASE_MATHFN
2073 :
2074 : default:
2075 : return END_BUILTINS;
2076 : }
2077 :
2078 614783 : mtype = TYPE_MAIN_VARIANT (type);
2079 614783 : if (mtype == double_type_node)
2080 : return fcode;
2081 574906 : else if (mtype == float_type_node)
2082 : return fcodef;
2083 529001 : else if (mtype == long_double_type_node)
2084 : return fcodel;
2085 496670 : else if (mtype == float16_type_node)
2086 : return fcodef16;
2087 496620 : else if (mtype == float32_type_node)
2088 : return fcodef32;
2089 496620 : else if (mtype == float64_type_node)
2090 : return fcodef64;
2091 496619 : else if (mtype == float128_type_node)
2092 : return fcodef128;
2093 489073 : else if (mtype == float32x_type_node)
2094 : return fcodef32x;
2095 489073 : else if (mtype == float64x_type_node)
2096 : return fcodef64x;
2097 489073 : else if (mtype == float128x_type_node)
2098 : return fcodef128x;
2099 : else
2100 489073 : return END_BUILTINS;
2101 : }
2102 :
2103 : #undef CASE_MATHFN
2104 : #undef CASE_MATHFN_FLOATN
2105 : #undef CASE_MATHFN_REENT
2106 :
2107 : /* Return mathematic function equivalent to FN but operating directly on TYPE,
2108 : if available. If IMPLICIT_P is true use the implicit builtin declaration,
2109 : otherwise use the explicit declaration. If we can't do the conversion,
2110 : return null. */
2111 :
2112 : static tree
2113 614638 : mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2114 : {
2115 614638 : built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2116 614638 : if (fcode2 == END_BUILTINS)
2117 : return NULL_TREE;
2118 :
2119 121914 : if (implicit_p && !builtin_decl_implicit_p (fcode2))
2120 : return NULL_TREE;
2121 :
2122 117919 : return builtin_decl_explicit (fcode2);
2123 : }
2124 :
2125 : /* Like mathfn_built_in_1, but always use the implicit array. */
2126 :
2127 : tree
2128 1039 : mathfn_built_in (tree type, combined_fn fn)
2129 : {
2130 1039 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2131 : }
2132 :
2133 : /* Like mathfn_built_in_1, but always use the explicit array. */
2134 :
2135 : tree
2136 0 : mathfn_built_in_explicit (tree type, combined_fn fn)
2137 : {
2138 0 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2139 : }
2140 :
2141 : /* Like mathfn_built_in_1, but take a built_in_function and
2142 : always use the implicit array. */
2143 :
2144 : tree
2145 613392 : mathfn_built_in (tree type, enum built_in_function fn)
2146 : {
2147 613392 : return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2148 : }
2149 :
2150 : /* Return the type associated with a built in function, i.e., the one
2151 : to be passed to mathfn_built_in to get the type-specific
2152 : function. */
2153 :
2154 : tree
2155 1293 : mathfn_built_in_type (combined_fn fn)
2156 : {
2157 : #define CASE_MATHFN(MATHFN) \
2158 : case CFN_BUILT_IN_##MATHFN: \
2159 : return double_type_node; \
2160 : case CFN_BUILT_IN_##MATHFN##F: \
2161 : return float_type_node; \
2162 : case CFN_BUILT_IN_##MATHFN##L: \
2163 : return long_double_type_node;
2164 :
2165 : #define CASE_MATHFN_FLOATN(MATHFN) \
2166 : CASE_MATHFN(MATHFN) \
2167 : case CFN_BUILT_IN_##MATHFN##F16: \
2168 : return float16_type_node; \
2169 : case CFN_BUILT_IN_##MATHFN##F32: \
2170 : return float32_type_node; \
2171 : case CFN_BUILT_IN_##MATHFN##F64: \
2172 : return float64_type_node; \
2173 : case CFN_BUILT_IN_##MATHFN##F128: \
2174 : return float128_type_node; \
2175 : case CFN_BUILT_IN_##MATHFN##F32X: \
2176 : return float32x_type_node; \
2177 : case CFN_BUILT_IN_##MATHFN##F64X: \
2178 : return float64x_type_node; \
2179 : case CFN_BUILT_IN_##MATHFN##F128X: \
2180 : return float128x_type_node;
2181 :
2182 : /* Similar to above, but appends _R after any F/L suffix. */
2183 : #define CASE_MATHFN_REENT(MATHFN) \
2184 : case CFN_BUILT_IN_##MATHFN##_R: \
2185 : return double_type_node; \
2186 : case CFN_BUILT_IN_##MATHFN##F_R: \
2187 : return float_type_node; \
2188 : case CFN_BUILT_IN_##MATHFN##L_R: \
2189 : return long_double_type_node;
2190 :
2191 1293 : switch (fn)
2192 : {
2193 1291 : SEQ_OF_CASE_MATHFN
2194 :
2195 : default:
2196 : return NULL_TREE;
2197 : }
2198 :
2199 : #undef CASE_MATHFN
2200 : #undef CASE_MATHFN_FLOATN
2201 : #undef CASE_MATHFN_REENT
2202 : #undef SEQ_OF_CASE_MATHFN
2203 : }
2204 :
2205 : /* Check whether there is an internal function associated with function FN
2206 : and return type RETURN_TYPE. Return the function if so, otherwise return
2207 : IFN_LAST.
2208 :
2209 : Note that this function only tests whether the function is defined in
2210 : internals.def, not whether it is actually available on the target. */
2211 :
2212 : static internal_fn
2213 14121796 : associated_internal_fn (built_in_function fn, tree return_type)
2214 : {
2215 14121796 : switch (fn)
2216 : {
2217 : #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2218 : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2219 : #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2220 : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2221 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2222 : #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2223 : CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2224 : #define DEF_INTERNAL_INTSZ_EXT_FN(NAME, FLAGS, OPTAB, TYPE) \
2225 : CASE_INTSZ_FN (BUILT_IN_##NAME): return IFN_##NAME;
2226 : #include "internal-fn.def"
2227 : #undef DEF_INTERNAL_INTSZ_EXT_FN
2228 :
2229 : /* CASE_INTSZ_FN does not cover the 8-bit builtin: there is no
2230 : BUILT_IN_BSWAP8 (a one byte swap is the identity), but
2231 : BUILT_IN_BITREVERSE8 exists and must map to IFN_BITREVERSE. */
2232 : case BUILT_IN_BITREVERSE8:
2233 : return IFN_BITREVERSE;
2234 :
2235 : CASE_FLT_FN (BUILT_IN_POW10):
2236 : return IFN_EXP10;
2237 :
2238 : CASE_FLT_FN (BUILT_IN_DREM):
2239 : return IFN_REMAINDER;
2240 :
2241 19805 : CASE_FLT_FN (BUILT_IN_SCALBN):
2242 19805 : CASE_FLT_FN (BUILT_IN_SCALBLN):
2243 19805 : if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2244 : return IFN_LDEXP;
2245 : return IFN_LAST;
2246 727 : case BUILT_IN_CRC8_DATA8:
2247 727 : case BUILT_IN_CRC16_DATA8:
2248 727 : case BUILT_IN_CRC16_DATA16:
2249 727 : case BUILT_IN_CRC32_DATA8:
2250 727 : case BUILT_IN_CRC32_DATA16:
2251 727 : case BUILT_IN_CRC32_DATA32:
2252 727 : case BUILT_IN_CRC64_DATA8:
2253 727 : case BUILT_IN_CRC64_DATA16:
2254 727 : case BUILT_IN_CRC64_DATA32:
2255 727 : case BUILT_IN_CRC64_DATA64:
2256 727 : return IFN_CRC;
2257 565 : case BUILT_IN_REV_CRC8_DATA8:
2258 565 : case BUILT_IN_REV_CRC16_DATA8:
2259 565 : case BUILT_IN_REV_CRC16_DATA16:
2260 565 : case BUILT_IN_REV_CRC32_DATA8:
2261 565 : case BUILT_IN_REV_CRC32_DATA16:
2262 565 : case BUILT_IN_REV_CRC32_DATA32:
2263 565 : case BUILT_IN_REV_CRC64_DATA8:
2264 565 : case BUILT_IN_REV_CRC64_DATA16:
2265 565 : case BUILT_IN_REV_CRC64_DATA32:
2266 565 : case BUILT_IN_REV_CRC64_DATA64:
2267 565 : return IFN_CRC_REV;
2268 : default:
2269 : return IFN_LAST;
2270 : }
2271 : }
2272 :
2273 : /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2274 : return its code, otherwise return IFN_LAST. Note that this function
2275 : only tests whether the function is defined in internals.def, not whether
2276 : it is actually available on the target. */
2277 :
2278 : internal_fn
2279 644228 : associated_internal_fn (tree fndecl)
2280 : {
2281 644228 : gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2282 644228 : return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2283 644228 : TREE_TYPE (TREE_TYPE (fndecl)));
2284 : }
2285 :
2286 : /* Check whether there is an internal function associated with function CFN
2287 : and return type RETURN_TYPE. Return the function if so, otherwise return
2288 : IFN_LAST.
2289 :
2290 : Note that this function only tests whether the function is defined in
2291 : internals.def, not whether it is actually available on the target. */
2292 :
2293 : internal_fn
2294 34611874 : associated_internal_fn (combined_fn cfn, tree return_type)
2295 : {
2296 34611874 : if (internal_fn_p (cfn))
2297 21134306 : return as_internal_fn (cfn);
2298 13477568 : return associated_internal_fn (as_builtin_fn (cfn), return_type);
2299 : }
2300 :
2301 : /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2302 : on the current target by a call to an internal function, return the
2303 : code of that internal function, otherwise return IFN_LAST. The caller
2304 : is responsible for ensuring that any side-effects of the built-in
2305 : call are dealt with correctly. E.g. if CALL sets errno, the caller
2306 : must decide that the errno result isn't needed or make it available
2307 : in some other way. */
2308 :
2309 : internal_fn
2310 874098 : replacement_internal_fn (gcall *call)
2311 : {
2312 874098 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2313 : {
2314 639988 : internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2315 639988 : if (ifn != IFN_LAST)
2316 : {
2317 61303 : tree_pair types = direct_internal_fn_types (ifn, call);
2318 61303 : optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2319 61303 : if (direct_internal_fn_supported_p (ifn, types, opt_type))
2320 38908 : return ifn;
2321 : }
2322 : }
2323 : return IFN_LAST;
2324 : }
2325 :
2326 : /* Expand a call to the builtin trinary math functions (fma).
2327 : Return NULL_RTX if a normal call should be emitted rather than expanding the
2328 : function in-line. EXP is the expression that is a call to the builtin
2329 : function; if convenient, the result should be placed in TARGET.
2330 : SUBTARGET may be used as the target for computing one of EXP's
2331 : operands. */
2332 :
2333 : static rtx
2334 372 : expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2335 : {
2336 372 : optab builtin_optab;
2337 372 : rtx op0, op1, op2, result;
2338 372 : rtx_insn *insns;
2339 372 : tree fndecl = get_callee_fndecl (exp);
2340 372 : tree arg0, arg1, arg2;
2341 372 : machine_mode mode;
2342 :
2343 372 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2344 : return NULL_RTX;
2345 :
2346 372 : arg0 = CALL_EXPR_ARG (exp, 0);
2347 372 : arg1 = CALL_EXPR_ARG (exp, 1);
2348 372 : arg2 = CALL_EXPR_ARG (exp, 2);
2349 :
2350 372 : switch (DECL_FUNCTION_CODE (fndecl))
2351 : {
2352 372 : CASE_FLT_FN (BUILT_IN_FMA):
2353 372 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2354 372 : builtin_optab = fma_optab; break;
2355 0 : default:
2356 0 : gcc_unreachable ();
2357 : }
2358 :
2359 : /* Make a suitable register to place result in. */
2360 372 : mode = TYPE_MODE (TREE_TYPE (exp));
2361 :
2362 : /* Before working hard, check whether the instruction is available. */
2363 372 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2364 : return NULL_RTX;
2365 :
2366 0 : result = gen_reg_rtx (mode);
2367 :
2368 : /* Always stabilize the argument list. */
2369 0 : CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2370 0 : CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2371 0 : CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2372 :
2373 0 : op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2374 0 : op1 = expand_normal (arg1);
2375 0 : op2 = expand_normal (arg2);
2376 :
2377 0 : start_sequence ();
2378 :
2379 : /* Compute into RESULT.
2380 : Set RESULT to wherever the result comes back. */
2381 0 : result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2382 : result, 0);
2383 :
2384 : /* If we were unable to expand via the builtin, stop the sequence
2385 : (without outputting the insns) and call to the library function
2386 : with the stabilized argument list. */
2387 0 : if (result == 0)
2388 : {
2389 0 : end_sequence ();
2390 0 : return expand_call (exp, target, target == const0_rtx);
2391 : }
2392 :
2393 : /* Output the entire sequence. */
2394 0 : insns = end_sequence ();
2395 0 : emit_insn (insns);
2396 :
2397 0 : return result;
2398 : }
2399 :
2400 : /* Expand a call to the builtin sin and cos math functions.
2401 : Return NULL_RTX if a normal call should be emitted rather than expanding the
2402 : function in-line. EXP is the expression that is a call to the builtin
2403 : function; if convenient, the result should be placed in TARGET.
2404 : SUBTARGET may be used as the target for computing one of EXP's
2405 : operands. */
2406 :
2407 : static rtx
2408 47 : expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2409 : {
2410 47 : optab builtin_optab;
2411 47 : rtx op0;
2412 47 : rtx_insn *insns;
2413 47 : tree fndecl = get_callee_fndecl (exp);
2414 47 : machine_mode mode;
2415 47 : tree arg;
2416 :
2417 47 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2418 : return NULL_RTX;
2419 :
2420 39 : arg = CALL_EXPR_ARG (exp, 0);
2421 :
2422 39 : switch (DECL_FUNCTION_CODE (fndecl))
2423 : {
2424 39 : CASE_FLT_FN (BUILT_IN_SIN):
2425 39 : CASE_FLT_FN (BUILT_IN_COS):
2426 39 : builtin_optab = sincos_optab; break;
2427 0 : default:
2428 0 : gcc_unreachable ();
2429 : }
2430 :
2431 : /* Make a suitable register to place result in. */
2432 39 : mode = TYPE_MODE (TREE_TYPE (exp));
2433 :
2434 : /* Check if sincos insn is available, otherwise fallback
2435 : to sin or cos insn. */
2436 39 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2437 39 : switch (DECL_FUNCTION_CODE (fndecl))
2438 : {
2439 : CASE_FLT_FN (BUILT_IN_SIN):
2440 : builtin_optab = sin_optab; break;
2441 14 : CASE_FLT_FN (BUILT_IN_COS):
2442 14 : builtin_optab = cos_optab; break;
2443 0 : default:
2444 0 : gcc_unreachable ();
2445 : }
2446 :
2447 : /* Before working hard, check whether the instruction is available. */
2448 39 : if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2449 : {
2450 0 : rtx result = gen_reg_rtx (mode);
2451 :
2452 : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2453 : need to expand the argument again. This way, we will not perform
2454 : side-effects more the once. */
2455 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2456 :
2457 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2458 :
2459 0 : start_sequence ();
2460 :
2461 : /* Compute into RESULT.
2462 : Set RESULT to wherever the result comes back. */
2463 0 : if (builtin_optab == sincos_optab)
2464 : {
2465 0 : int ok;
2466 :
2467 0 : switch (DECL_FUNCTION_CODE (fndecl))
2468 : {
2469 0 : CASE_FLT_FN (BUILT_IN_SIN):
2470 0 : ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2471 0 : break;
2472 0 : CASE_FLT_FN (BUILT_IN_COS):
2473 0 : ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2474 0 : break;
2475 0 : default:
2476 0 : gcc_unreachable ();
2477 : }
2478 0 : gcc_assert (ok);
2479 : }
2480 : else
2481 0 : result = expand_unop (mode, builtin_optab, op0, result, 0);
2482 :
2483 0 : if (result != 0)
2484 : {
2485 : /* Output the entire sequence. */
2486 0 : insns = end_sequence ();
2487 0 : emit_insn (insns);
2488 0 : return result;
2489 : }
2490 :
2491 : /* If we were unable to expand via the builtin, stop the sequence
2492 : (without outputting the insns) and call to the library function
2493 : with the stabilized argument list. */
2494 0 : end_sequence ();
2495 : }
2496 :
2497 39 : return expand_call (exp, target, target == const0_rtx);
2498 : }
2499 :
2500 : /* Given an interclass math builtin decl FNDECL and it's argument ARG
2501 : return an RTL instruction code that implements the functionality.
2502 : If that isn't possible or available return CODE_FOR_nothing. */
2503 :
2504 : static enum insn_code
2505 1818142 : interclass_mathfn_icode (tree arg, tree fndecl)
2506 : {
2507 1818142 : bool errno_set = false;
2508 1818142 : optab builtin_optab = unknown_optab;
2509 1818142 : machine_mode mode;
2510 :
2511 1818142 : switch (DECL_FUNCTION_CODE (fndecl))
2512 : {
2513 4 : CASE_FLT_FN (BUILT_IN_ILOGB):
2514 4 : errno_set = true;
2515 4 : builtin_optab = ilogb_optab;
2516 4 : break;
2517 704982 : CASE_FLT_FN (BUILT_IN_ISINF):
2518 704982 : builtin_optab = isinf_optab;
2519 704982 : break;
2520 630037 : case BUILT_IN_ISFINITE:
2521 630037 : builtin_optab = isfinite_optab;
2522 630037 : break;
2523 239210 : case BUILT_IN_ISNORMAL:
2524 239210 : builtin_optab = isnormal_optab;
2525 239210 : break;
2526 241832 : CASE_FLT_FN (BUILT_IN_ISNAN):
2527 241832 : builtin_optab = isnan_optab;
2528 241832 : break;
2529 : CASE_FLT_FN (BUILT_IN_FINITE):
2530 : case BUILT_IN_FINITED32:
2531 : case BUILT_IN_FINITED64:
2532 : case BUILT_IN_FINITED128:
2533 : case BUILT_IN_ISINFD32:
2534 : case BUILT_IN_ISINFD64:
2535 : case BUILT_IN_ISINFD128:
2536 : case BUILT_IN_ISNAND32:
2537 : case BUILT_IN_ISNAND64:
2538 : case BUILT_IN_ISNAND128:
2539 : /* These builtins have no optabs (yet). */
2540 : break;
2541 0 : default:
2542 0 : gcc_unreachable ();
2543 : }
2544 :
2545 : /* There's no easy way to detect the case we need to set EDOM. */
2546 1818142 : if (flag_errno_math && errno_set)
2547 : return CODE_FOR_nothing;
2548 :
2549 : /* Optab mode depends on the mode of the input argument. */
2550 1818142 : mode = TYPE_MODE (TREE_TYPE (arg));
2551 :
2552 1818142 : if (builtin_optab)
2553 1816065 : return optab_handler (builtin_optab, mode);
2554 : return CODE_FOR_nothing;
2555 : }
2556 :
2557 : /* Expand a call to one of the builtin math functions that operate on
2558 : floating point argument and output an integer result (ilogb, isinf,
2559 : isnan, etc).
2560 : Return 0 if a normal call should be emitted rather than expanding the
2561 : function in-line. EXP is the expression that is a call to the builtin
2562 : function; if convenient, the result should be placed in TARGET. */
2563 :
2564 : static rtx
2565 4 : expand_builtin_interclass_mathfn (tree exp, rtx target)
2566 : {
2567 4 : enum insn_code icode = CODE_FOR_nothing;
2568 4 : rtx op0;
2569 4 : tree fndecl = get_callee_fndecl (exp);
2570 4 : machine_mode mode;
2571 4 : tree arg;
2572 :
2573 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2574 : return NULL_RTX;
2575 :
2576 4 : arg = CALL_EXPR_ARG (exp, 0);
2577 4 : icode = interclass_mathfn_icode (arg, fndecl);
2578 4 : mode = TYPE_MODE (TREE_TYPE (arg));
2579 :
2580 4 : if (icode != CODE_FOR_nothing)
2581 : {
2582 2 : class expand_operand ops[1];
2583 2 : rtx_insn *last = get_last_insn ();
2584 2 : tree orig_arg = arg;
2585 :
2586 : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2587 : need to expand the argument again. This way, we will not perform
2588 : side-effects more the once. */
2589 2 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2590 :
2591 2 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2592 :
2593 2 : if (mode != GET_MODE (op0))
2594 0 : op0 = convert_to_mode (mode, op0, 0);
2595 :
2596 2 : create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2597 2 : if (maybe_legitimize_operands (icode, 0, 1, ops)
2598 2 : && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2599 1 : return ops[0].value;
2600 :
2601 1 : delete_insns_since (last);
2602 1 : CALL_EXPR_ARG (exp, 0) = orig_arg;
2603 : }
2604 :
2605 : return NULL_RTX;
2606 : }
2607 :
2608 : /* Expand a call to the builtin sincos math function.
2609 : Return NULL_RTX if a normal call should be emitted rather than expanding the
2610 : function in-line. EXP is the expression that is a call to the builtin
2611 : function. */
2612 :
2613 : static rtx
2614 3 : expand_builtin_sincos (tree exp)
2615 : {
2616 3 : rtx op0, op1, op2, target1, target2;
2617 3 : machine_mode mode;
2618 3 : tree arg, sinp, cosp;
2619 3 : int result;
2620 3 : location_t loc = EXPR_LOCATION (exp);
2621 3 : tree alias_type, alias_off;
2622 :
2623 3 : if (!validate_arglist (exp, REAL_TYPE,
2624 : POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2625 : return NULL_RTX;
2626 :
2627 3 : arg = CALL_EXPR_ARG (exp, 0);
2628 3 : sinp = CALL_EXPR_ARG (exp, 1);
2629 3 : cosp = CALL_EXPR_ARG (exp, 2);
2630 :
2631 : /* Make a suitable register to place result in. */
2632 3 : mode = TYPE_MODE (TREE_TYPE (arg));
2633 :
2634 : /* Check if sincos insn is available, otherwise emit the call. */
2635 3 : if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2636 : return NULL_RTX;
2637 :
2638 0 : target1 = gen_reg_rtx (mode);
2639 0 : target2 = gen_reg_rtx (mode);
2640 :
2641 0 : op0 = expand_normal (arg);
2642 0 : alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2643 0 : alias_off = build_int_cst (alias_type, 0);
2644 0 : op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2645 : sinp, alias_off));
2646 0 : op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2647 : cosp, alias_off));
2648 :
2649 : /* Compute into target1 and target2.
2650 : Set TARGET to wherever the result comes back. */
2651 0 : result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2652 0 : gcc_assert (result);
2653 :
2654 : /* Move target1 and target2 to the memory locations indicated
2655 : by op1 and op2. */
2656 0 : emit_move_insn (op1, target1);
2657 0 : emit_move_insn (op2, target2);
2658 :
2659 0 : return const0_rtx;
2660 : }
2661 :
2662 : /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2663 : result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2664 : static rtx
2665 60 : expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2666 : {
2667 60 : if (!validate_arglist (exp, VOID_TYPE))
2668 : return NULL_RTX;
2669 :
2670 60 : insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2671 60 : if (icode == CODE_FOR_nothing)
2672 : return NULL_RTX;
2673 :
2674 0 : if (target == 0
2675 0 : || GET_MODE (target) != target_mode
2676 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2677 0 : target = gen_reg_rtx (target_mode);
2678 :
2679 0 : rtx pat = GEN_FCN (icode) (target);
2680 0 : if (!pat)
2681 : return NULL_RTX;
2682 0 : emit_insn (pat);
2683 :
2684 0 : return target;
2685 : }
2686 :
2687 : /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2688 : fenv.h), returning the result and setting it in TARGET. Otherwise return
2689 : NULL_RTX on failure. */
2690 : static rtx
2691 2066 : expand_builtin_feclear_feraise_except (tree exp, rtx target,
2692 : machine_mode target_mode, optab op_optab)
2693 : {
2694 2066 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2695 : return NULL_RTX;
2696 2066 : rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2697 :
2698 2066 : insn_code icode = direct_optab_handler (op_optab, SImode);
2699 2066 : if (icode == CODE_FOR_nothing)
2700 : return NULL_RTX;
2701 :
2702 0 : if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2703 : return NULL_RTX;
2704 :
2705 0 : if (target == 0
2706 0 : || GET_MODE (target) != target_mode
2707 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2708 0 : target = gen_reg_rtx (target_mode);
2709 :
2710 0 : rtx pat = GEN_FCN (icode) (target, op0);
2711 0 : if (!pat)
2712 : return NULL_RTX;
2713 0 : emit_insn (pat);
2714 :
2715 0 : return target;
2716 : }
2717 :
2718 : /* Expand a call to the internal cexpi builtin to the sincos math function.
2719 : EXP is the expression that is a call to the builtin function; if convenient,
2720 : the result should be placed in TARGET. */
2721 :
2722 : static rtx
2723 173 : expand_builtin_cexpi (tree exp, rtx target)
2724 : {
2725 173 : tree fndecl = get_callee_fndecl (exp);
2726 173 : tree arg, type;
2727 173 : machine_mode mode;
2728 173 : rtx op0, op1, op2;
2729 173 : location_t loc = EXPR_LOCATION (exp);
2730 :
2731 173 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2732 : return NULL_RTX;
2733 :
2734 173 : arg = CALL_EXPR_ARG (exp, 0);
2735 173 : type = TREE_TYPE (arg);
2736 173 : mode = TYPE_MODE (TREE_TYPE (arg));
2737 :
2738 : /* Try expanding via a sincos optab, fall back to emitting a libcall
2739 : to sincos or cexp. We are sure we have sincos or cexp because cexpi
2740 : is only generated from sincos, cexp or if we have either of them. */
2741 173 : if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2742 : {
2743 6 : op1 = gen_reg_rtx (mode);
2744 6 : op2 = gen_reg_rtx (mode);
2745 :
2746 6 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2747 :
2748 : /* Compute into op1 and op2. */
2749 6 : expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2750 : }
2751 167 : else if (targetm.libc_has_function (function_sincos, type))
2752 : {
2753 167 : tree call, fn = NULL_TREE;
2754 167 : tree top1, top2;
2755 167 : rtx op1a, op2a;
2756 :
2757 167 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2758 54 : fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2759 113 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2760 89 : fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2761 24 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2762 24 : fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2763 : else
2764 0 : gcc_unreachable ();
2765 :
2766 167 : op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2767 167 : op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2768 167 : op1a = copy_addr_to_reg (XEXP (op1, 0));
2769 167 : op2a = copy_addr_to_reg (XEXP (op2, 0));
2770 167 : top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2771 167 : top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2772 :
2773 : /* Make sure not to fold the sincos call again. */
2774 167 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2775 167 : expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2776 : call, 3, arg, top1, top2));
2777 : }
2778 : else
2779 : {
2780 0 : tree call, fn = NULL_TREE, narg;
2781 0 : tree ctype = build_complex_type (type);
2782 :
2783 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2784 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2785 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2786 0 : fn = builtin_decl_explicit (BUILT_IN_CEXP);
2787 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2788 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2789 : else
2790 0 : gcc_unreachable ();
2791 :
2792 : /* If we don't have a decl for cexp create one. This is the
2793 : friendliest fallback if the user calls __builtin_cexpi
2794 : without full target C99 function support. */
2795 0 : if (fn == NULL_TREE)
2796 : {
2797 0 : tree fntype;
2798 0 : const char *name = NULL;
2799 :
2800 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2801 : name = "cexpf";
2802 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2803 : name = "cexp";
2804 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2805 0 : name = "cexpl";
2806 :
2807 0 : fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2808 0 : fn = build_fn_decl (name, fntype);
2809 : }
2810 :
2811 0 : narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2812 : build_real (type, dconst0), arg);
2813 :
2814 : /* Make sure not to fold the cexp call again. */
2815 0 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2816 0 : return expand_expr (build_call_nary (ctype, call, 1, narg),
2817 0 : target, VOIDmode, EXPAND_NORMAL);
2818 : }
2819 :
2820 : /* Now build the proper return type. */
2821 346 : return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2822 173 : make_tree (TREE_TYPE (arg), op2),
2823 173 : make_tree (TREE_TYPE (arg), op1)),
2824 173 : target, VOIDmode, EXPAND_NORMAL);
2825 : }
2826 :
2827 : /* Conveniently construct a function call expression. FNDECL names the
2828 : function to be called, N is the number of arguments, and the "..."
2829 : parameters are the argument expressions. Unlike build_call_exr
2830 : this doesn't fold the call, hence it will always return a CALL_EXPR. */
2831 :
2832 : static tree
2833 136744 : build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2834 : {
2835 136744 : va_list ap;
2836 136744 : tree fntype = TREE_TYPE (fndecl);
2837 136744 : tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2838 :
2839 136744 : va_start (ap, n);
2840 136744 : fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2841 136744 : va_end (ap);
2842 136744 : SET_EXPR_LOCATION (fn, loc);
2843 136744 : return fn;
2844 : }
2845 :
2846 : /* Expand the __builtin_issignaling builtin. This needs to handle
2847 : all floating point formats that do support NaNs (for those that
2848 : don't it just sets target to 0). */
2849 :
2850 : static rtx
2851 784 : expand_builtin_issignaling (tree exp, rtx target)
2852 : {
2853 784 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2854 : return NULL_RTX;
2855 :
2856 784 : tree arg = CALL_EXPR_ARG (exp, 0);
2857 784 : scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2858 784 : const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2859 :
2860 : /* Expand the argument yielding a RTX expression. */
2861 784 : rtx temp = expand_normal (arg);
2862 :
2863 : /* If mode doesn't support NaN, always return 0.
2864 : Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2865 : __builtin_issignaling working without -fsignaling-nans. Especially
2866 : when -fno-signaling-nans is the default.
2867 : On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2868 : -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2869 : fold to 0 or non-NaN/Inf classification. */
2870 784 : if (!HONOR_NANS (fmode))
2871 : {
2872 0 : emit_move_insn (target, const0_rtx);
2873 0 : return target;
2874 : }
2875 :
2876 : /* Check if the back end provides an insn that handles issignaling for the
2877 : argument's mode. */
2878 784 : enum insn_code icode = optab_handler (issignaling_optab, fmode);
2879 784 : if (icode != CODE_FOR_nothing)
2880 : {
2881 142 : rtx_insn *last = get_last_insn ();
2882 142 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2883 142 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2884 : return this_target;
2885 0 : delete_insns_since (last);
2886 : }
2887 :
2888 642 : if (DECIMAL_FLOAT_MODE_P (fmode))
2889 : {
2890 63 : scalar_int_mode imode;
2891 63 : rtx hi;
2892 63 : switch (fmt->ieee_bits)
2893 : {
2894 42 : case 32:
2895 42 : case 64:
2896 42 : imode = int_mode_for_mode (fmode).require ();
2897 42 : temp = gen_lowpart (imode, temp);
2898 42 : break;
2899 21 : case 128:
2900 21 : imode = int_mode_for_size (64, 1).require ();
2901 21 : hi = NULL_RTX;
2902 : /* For decimal128, TImode support isn't always there and even when
2903 : it is, working on the DImode high part is usually better. */
2904 21 : if (!MEM_P (temp))
2905 : {
2906 6 : if (rtx t = force_highpart_subreg (imode, temp, fmode))
2907 : hi = t;
2908 : else
2909 : {
2910 0 : scalar_int_mode imode2;
2911 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2912 : {
2913 0 : rtx temp2 = gen_lowpart (imode2, temp);
2914 0 : if (rtx t = force_highpart_subreg (imode, temp2, imode2))
2915 : hi = t;
2916 : }
2917 : }
2918 0 : if (!hi)
2919 : {
2920 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2921 0 : emit_move_insn (mem, temp);
2922 0 : temp = mem;
2923 : }
2924 : }
2925 0 : if (!hi)
2926 : {
2927 15 : poly_int64 offset
2928 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
2929 15 : hi = adjust_address (temp, imode, offset);
2930 : }
2931 : temp = hi;
2932 : break;
2933 0 : default:
2934 0 : gcc_unreachable ();
2935 : }
2936 : /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2937 : have 6 bits below it all set. */
2938 63 : rtx val
2939 63 : = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2940 63 : temp = expand_binop (imode, and_optab, temp, val,
2941 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2942 63 : temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2943 63 : return temp;
2944 : }
2945 :
2946 : /* Only PDP11 has these defined differently but doesn't support NaNs. */
2947 579 : gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2948 579 : gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2949 4053 : gcc_assert (MODE_COMPOSITE_P (fmode)
2950 : || (fmt->pnan == fmt->p
2951 : && fmt->signbit_ro == fmt->signbit_rw));
2952 :
2953 579 : switch (fmt->p)
2954 : {
2955 0 : case 106: /* IBM double double */
2956 : /* For IBM double double, recurse on the most significant double. */
2957 0 : gcc_assert (MODE_COMPOSITE_P (fmode));
2958 0 : temp = convert_modes (DFmode, fmode, temp, 0);
2959 0 : fmode = DFmode;
2960 0 : fmt = REAL_MODE_FORMAT (DFmode);
2961 : /* FALLTHRU */
2962 479 : case 8: /* bfloat */
2963 479 : case 11: /* IEEE half */
2964 479 : case 24: /* IEEE single */
2965 479 : case 53: /* IEEE double or Intel extended with rounding to double */
2966 479 : if (fmt->p == 53 && fmt->signbit_ro == 79)
2967 0 : goto extended;
2968 479 : {
2969 479 : scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2970 479 : temp = gen_lowpart (imode, temp);
2971 479 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2972 : & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2973 479 : if (fmt->qnan_msb_set)
2974 : {
2975 479 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2976 479 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2977 : /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2978 : ((temp ^ bit) & mask) > val. */
2979 479 : temp = expand_binop (imode, xor_optab, temp, bit,
2980 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2981 479 : temp = expand_binop (imode, and_optab, temp, mask,
2982 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2983 479 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
2984 : 1, 1);
2985 : }
2986 : else
2987 : {
2988 : /* For MIPS/PA IEEE single/double, expand to:
2989 : (temp & val) == val. */
2990 0 : temp = expand_binop (imode, and_optab, temp, val,
2991 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2992 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
2993 : 1, 1);
2994 : }
2995 : }
2996 : break;
2997 100 : case 113: /* IEEE quad */
2998 100 : {
2999 100 : rtx hi = NULL_RTX, lo = NULL_RTX;
3000 100 : scalar_int_mode imode = int_mode_for_size (64, 1).require ();
3001 : /* For IEEE quad, TImode support isn't always there and even when
3002 : it is, working on DImode parts is usually better. */
3003 100 : if (!MEM_P (temp))
3004 : {
3005 85 : hi = force_highpart_subreg (imode, temp, fmode);
3006 85 : lo = force_lowpart_subreg (imode, temp, fmode);
3007 85 : if (!hi || !lo)
3008 : {
3009 0 : scalar_int_mode imode2;
3010 0 : if (int_mode_for_mode (fmode).exists (&imode2))
3011 : {
3012 0 : rtx temp2 = gen_lowpart (imode2, temp);
3013 0 : hi = force_highpart_subreg (imode, temp2, imode2);
3014 0 : lo = force_lowpart_subreg (imode, temp2, imode2);
3015 : }
3016 : }
3017 85 : if (!hi || !lo)
3018 : {
3019 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3020 0 : emit_move_insn (mem, temp);
3021 0 : temp = mem;
3022 : }
3023 : }
3024 100 : if (!hi || !lo)
3025 : {
3026 15 : poly_int64 offset
3027 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
3028 15 : hi = adjust_address (temp, imode, offset);
3029 15 : offset = subreg_lowpart_offset (imode, GET_MODE (temp));
3030 15 : lo = adjust_address (temp, imode, offset);
3031 : }
3032 100 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
3033 : & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
3034 100 : if (fmt->qnan_msb_set)
3035 : {
3036 100 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
3037 : - 64)));
3038 100 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
3039 : /* For non-MIPS/PA IEEE quad, expand to:
3040 : (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
3041 100 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3042 100 : lo = expand_binop (imode, ior_optab, lo, nlo,
3043 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3044 100 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
3045 100 : temp = expand_binop (imode, xor_optab, hi, bit,
3046 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3047 100 : temp = expand_binop (imode, ior_optab, temp, lo,
3048 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3049 100 : temp = expand_binop (imode, and_optab, temp, mask,
3050 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3051 100 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
3052 : 1, 1);
3053 : }
3054 : else
3055 : {
3056 : /* For MIPS/PA IEEE quad, expand to:
3057 : (hi & val) == val. */
3058 0 : temp = expand_binop (imode, and_optab, hi, val,
3059 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3060 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
3061 : 1, 1);
3062 : }
3063 : }
3064 : break;
3065 0 : case 64: /* Intel or Motorola extended */
3066 0 : extended:
3067 0 : {
3068 0 : rtx ex, hi, lo;
3069 0 : scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3070 0 : scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3071 0 : if (!MEM_P (temp))
3072 : {
3073 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3074 0 : emit_move_insn (mem, temp);
3075 0 : temp = mem;
3076 : }
3077 0 : if (fmt->signbit_ro == 95)
3078 : {
3079 : /* Motorola, always big endian, with 16-bit gap in between
3080 : 16-bit sign+exponent and 64-bit mantissa. */
3081 0 : ex = adjust_address (temp, iemode, 0);
3082 0 : hi = adjust_address (temp, imode, 4);
3083 0 : lo = adjust_address (temp, imode, 8);
3084 : }
3085 0 : else if (!WORDS_BIG_ENDIAN)
3086 : {
3087 : /* Intel little endian, 64-bit mantissa followed by 16-bit
3088 : sign+exponent and then either 16 or 48 bits of gap. */
3089 0 : ex = adjust_address (temp, iemode, 8);
3090 0 : hi = adjust_address (temp, imode, 4);
3091 0 : lo = adjust_address (temp, imode, 0);
3092 : }
3093 : else
3094 : {
3095 : /* Big endian Itanium. */
3096 : ex = adjust_address (temp, iemode, 0);
3097 : hi = adjust_address (temp, imode, 2);
3098 : lo = adjust_address (temp, imode, 6);
3099 : }
3100 0 : rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3101 0 : gcc_assert (fmt->qnan_msb_set);
3102 0 : rtx mask = GEN_INT (0x7fff);
3103 0 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3104 : /* For Intel/Motorola extended format, expand to:
3105 : (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3106 0 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3107 0 : lo = expand_binop (imode, ior_optab, lo, nlo,
3108 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3109 0 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3110 0 : temp = expand_binop (imode, xor_optab, hi, bit,
3111 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3112 0 : temp = expand_binop (imode, ior_optab, temp, lo,
3113 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3114 0 : temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3115 0 : ex = expand_binop (iemode, and_optab, ex, mask,
3116 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3117 0 : ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3118 : ex, mask, iemode, 1, 1);
3119 0 : temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3120 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3121 : }
3122 0 : break;
3123 0 : default:
3124 0 : gcc_unreachable ();
3125 : }
3126 :
3127 : return temp;
3128 : }
3129 :
3130 : /* Expand a call to one of the builtin rounding functions gcc defines
3131 : as an extension (lfloor and lceil). As these are gcc extensions we
3132 : do not need to worry about setting errno to EDOM.
3133 : If expanding via optab fails, lower expression to (int)(floor(x)).
3134 : EXP is the expression that is a call to the builtin function;
3135 : if convenient, the result should be placed in TARGET. */
3136 :
3137 : static rtx
3138 223 : expand_builtin_int_roundingfn (tree exp, rtx target)
3139 : {
3140 223 : convert_optab builtin_optab;
3141 223 : rtx op0, tmp;
3142 223 : rtx_insn *insns;
3143 223 : tree fndecl = get_callee_fndecl (exp);
3144 223 : enum built_in_function fallback_fn;
3145 223 : tree fallback_fndecl;
3146 223 : machine_mode mode;
3147 223 : tree arg;
3148 :
3149 223 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3150 : return NULL_RTX;
3151 :
3152 223 : arg = CALL_EXPR_ARG (exp, 0);
3153 :
3154 223 : switch (DECL_FUNCTION_CODE (fndecl))
3155 : {
3156 : CASE_FLT_FN (BUILT_IN_ICEIL):
3157 : CASE_FLT_FN (BUILT_IN_LCEIL):
3158 : CASE_FLT_FN (BUILT_IN_LLCEIL):
3159 : builtin_optab = lceil_optab;
3160 : fallback_fn = BUILT_IN_CEIL;
3161 : break;
3162 :
3163 112 : CASE_FLT_FN (BUILT_IN_IFLOOR):
3164 112 : CASE_FLT_FN (BUILT_IN_LFLOOR):
3165 112 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
3166 112 : builtin_optab = lfloor_optab;
3167 112 : fallback_fn = BUILT_IN_FLOOR;
3168 112 : break;
3169 :
3170 0 : default:
3171 0 : gcc_unreachable ();
3172 : }
3173 :
3174 : /* Make a suitable register to place result in. */
3175 223 : mode = TYPE_MODE (TREE_TYPE (exp));
3176 :
3177 223 : target = gen_reg_rtx (mode);
3178 :
3179 : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3180 : need to expand the argument again. This way, we will not perform
3181 : side-effects more the once. */
3182 223 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3183 :
3184 223 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3185 :
3186 223 : start_sequence ();
3187 :
3188 : /* Compute into TARGET. */
3189 223 : if (expand_sfix_optab (target, op0, builtin_optab))
3190 : {
3191 : /* Output the entire sequence. */
3192 32 : insns = end_sequence ();
3193 32 : emit_insn (insns);
3194 32 : return target;
3195 : }
3196 :
3197 : /* If we were unable to expand via the builtin, stop the sequence
3198 : (without outputting the insns). */
3199 191 : end_sequence ();
3200 :
3201 : /* Fall back to floating point rounding optab. */
3202 191 : fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3203 :
3204 : /* For non-C99 targets we may end up without a fallback fndecl here
3205 : if the user called __builtin_lfloor directly. In this case emit
3206 : a call to the floor/ceil variants nevertheless. This should result
3207 : in the best user experience for not full C99 targets. */
3208 191 : if (fallback_fndecl == NULL_TREE)
3209 : {
3210 0 : tree fntype;
3211 0 : const char *name = NULL;
3212 :
3213 0 : switch (DECL_FUNCTION_CODE (fndecl))
3214 : {
3215 : case BUILT_IN_ICEIL:
3216 : case BUILT_IN_LCEIL:
3217 : case BUILT_IN_LLCEIL:
3218 : name = "ceil";
3219 : break;
3220 0 : case BUILT_IN_ICEILF:
3221 0 : case BUILT_IN_LCEILF:
3222 0 : case BUILT_IN_LLCEILF:
3223 0 : name = "ceilf";
3224 0 : break;
3225 0 : case BUILT_IN_ICEILL:
3226 0 : case BUILT_IN_LCEILL:
3227 0 : case BUILT_IN_LLCEILL:
3228 0 : name = "ceill";
3229 0 : break;
3230 0 : case BUILT_IN_IFLOOR:
3231 0 : case BUILT_IN_LFLOOR:
3232 0 : case BUILT_IN_LLFLOOR:
3233 0 : name = "floor";
3234 0 : break;
3235 0 : case BUILT_IN_IFLOORF:
3236 0 : case BUILT_IN_LFLOORF:
3237 0 : case BUILT_IN_LLFLOORF:
3238 0 : name = "floorf";
3239 0 : break;
3240 0 : case BUILT_IN_IFLOORL:
3241 0 : case BUILT_IN_LFLOORL:
3242 0 : case BUILT_IN_LLFLOORL:
3243 0 : name = "floorl";
3244 0 : break;
3245 0 : default:
3246 0 : gcc_unreachable ();
3247 : }
3248 :
3249 0 : fntype = build_function_type_list (TREE_TYPE (arg),
3250 0 : TREE_TYPE (arg), NULL_TREE);
3251 0 : fallback_fndecl = build_fn_decl (name, fntype);
3252 : }
3253 :
3254 191 : exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3255 :
3256 191 : tmp = expand_normal (exp);
3257 191 : tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3258 :
3259 : /* Truncate the result of floating point optab to integer
3260 : via expand_fix (). */
3261 191 : target = gen_reg_rtx (mode);
3262 191 : expand_fix (target, tmp, 0);
3263 :
3264 191 : return target;
3265 : }
3266 :
3267 : /* Expand a call to one of the builtin math functions doing integer
3268 : conversion (lrint).
3269 : Return 0 if a normal call should be emitted rather than expanding the
3270 : function in-line. EXP is the expression that is a call to the builtin
3271 : function; if convenient, the result should be placed in TARGET. */
3272 :
3273 : static rtx
3274 572 : expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3275 : {
3276 572 : convert_optab builtin_optab;
3277 572 : rtx op0;
3278 572 : rtx_insn *insns;
3279 572 : tree fndecl = get_callee_fndecl (exp);
3280 572 : tree arg;
3281 572 : machine_mode mode;
3282 572 : enum built_in_function fallback_fn = BUILT_IN_NONE;
3283 :
3284 572 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3285 : return NULL_RTX;
3286 :
3287 472 : arg = CALL_EXPR_ARG (exp, 0);
3288 :
3289 472 : switch (DECL_FUNCTION_CODE (fndecl))
3290 : {
3291 4 : CASE_FLT_FN (BUILT_IN_IRINT):
3292 4 : fallback_fn = BUILT_IN_LRINT;
3293 : gcc_fallthrough ();
3294 : CASE_FLT_FN (BUILT_IN_LRINT):
3295 : CASE_FLT_FN (BUILT_IN_LLRINT):
3296 : builtin_optab = lrint_optab;
3297 : break;
3298 :
3299 207 : CASE_FLT_FN (BUILT_IN_IROUND):
3300 207 : fallback_fn = BUILT_IN_LROUND;
3301 : gcc_fallthrough ();
3302 : CASE_FLT_FN (BUILT_IN_LROUND):
3303 : CASE_FLT_FN (BUILT_IN_LLROUND):
3304 : builtin_optab = lround_optab;
3305 : break;
3306 :
3307 0 : default:
3308 0 : gcc_unreachable ();
3309 : }
3310 :
3311 : /* There's no easy way to detect the case we need to set EDOM. */
3312 472 : if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3313 : return NULL_RTX;
3314 :
3315 : /* Make a suitable register to place result in. */
3316 241 : mode = TYPE_MODE (TREE_TYPE (exp));
3317 :
3318 : /* There's no easy way to detect the case we need to set EDOM. */
3319 241 : if (!flag_errno_math)
3320 : {
3321 241 : rtx result = gen_reg_rtx (mode);
3322 :
3323 : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3324 : need to expand the argument again. This way, we will not perform
3325 : side-effects more the once. */
3326 241 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3327 :
3328 241 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3329 :
3330 241 : start_sequence ();
3331 :
3332 241 : if (expand_sfix_optab (result, op0, builtin_optab))
3333 : {
3334 : /* Output the entire sequence. */
3335 20 : insns = end_sequence ();
3336 20 : emit_insn (insns);
3337 20 : return result;
3338 : }
3339 :
3340 : /* If we were unable to expand via the builtin, stop the sequence
3341 : (without outputting the insns) and call to the library function
3342 : with the stabilized argument list. */
3343 221 : end_sequence ();
3344 : }
3345 :
3346 221 : if (fallback_fn != BUILT_IN_NONE)
3347 : {
3348 : /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3349 : targets, (int) round (x) should never be transformed into
3350 : BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3351 : a call to lround in the hope that the target provides at least some
3352 : C99 functions. This should result in the best user experience for
3353 : not full C99 targets.
3354 : As scalar float conversions with same mode are useless in GIMPLE,
3355 : we can end up e.g. with _Float32 argument passed to float builtin,
3356 : try to get the type from the builtin prototype first. */
3357 207 : tree fallback_fndecl = NULL_TREE;
3358 207 : if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3359 207 : fallback_fndecl
3360 207 : = mathfn_built_in_1 (TREE_VALUE (argtypes),
3361 : as_combined_fn (fallback_fn), 0);
3362 207 : if (fallback_fndecl == NULL_TREE)
3363 0 : fallback_fndecl
3364 0 : = mathfn_built_in_1 (TREE_TYPE (arg),
3365 : as_combined_fn (fallback_fn), 0);
3366 0 : if (fallback_fndecl)
3367 : {
3368 207 : exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3369 : fallback_fndecl, 1, arg);
3370 :
3371 207 : target = expand_call (exp, NULL_RTX, target == const0_rtx);
3372 207 : target = maybe_emit_group_store (target, TREE_TYPE (exp));
3373 207 : return convert_to_mode (mode, target, 0);
3374 : }
3375 : }
3376 :
3377 14 : return expand_call (exp, target, target == const0_rtx);
3378 : }
3379 :
3380 : /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3381 : a normal call should be emitted rather than expanding the function
3382 : in-line. EXP is the expression that is a call to the builtin
3383 : function; if convenient, the result should be placed in TARGET. */
3384 :
3385 : static rtx
3386 293 : expand_builtin_powi (tree exp, rtx target)
3387 : {
3388 293 : tree arg0, arg1;
3389 293 : rtx op0, op1;
3390 293 : machine_mode mode;
3391 293 : machine_mode mode2;
3392 :
3393 293 : if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3394 : return NULL_RTX;
3395 :
3396 293 : arg0 = CALL_EXPR_ARG (exp, 0);
3397 293 : arg1 = CALL_EXPR_ARG (exp, 1);
3398 293 : mode = TYPE_MODE (TREE_TYPE (exp));
3399 :
3400 : /* Emit a libcall to libgcc. */
3401 :
3402 : /* Mode of the 2nd argument must match that of an int. */
3403 293 : mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3404 :
3405 293 : if (target == NULL_RTX)
3406 0 : target = gen_reg_rtx (mode);
3407 :
3408 293 : op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3409 293 : if (GET_MODE (op0) != mode)
3410 0 : op0 = convert_to_mode (mode, op0, 0);
3411 293 : op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3412 293 : if (GET_MODE (op1) != mode2)
3413 52 : op1 = convert_to_mode (mode2, op1, 0);
3414 :
3415 293 : target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3416 : target, LCT_CONST, mode,
3417 : op0, mode, op1, mode2);
3418 :
3419 293 : return target;
3420 : }
3421 :
3422 : /* Expand expression EXP which is a call to the strlen builtin. Return
3423 : NULL_RTX if we failed and the caller should emit a normal call, otherwise
3424 : try to get the result in TARGET, if convenient. */
3425 :
3426 : static rtx
3427 13769 : expand_builtin_strlen (tree exp, rtx target,
3428 : machine_mode target_mode)
3429 : {
3430 13769 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3431 : return NULL_RTX;
3432 :
3433 13750 : tree src = CALL_EXPR_ARG (exp, 0);
3434 :
3435 : /* If the length can be computed at compile-time, return it. */
3436 13750 : if (tree len = c_strlen (src, 0))
3437 141 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3438 :
3439 : /* If the length can be computed at compile-time and is constant
3440 : integer, but there are side-effects in src, evaluate
3441 : src for side-effects, then return len.
3442 : E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3443 : can be optimized into: i++; x = 3; */
3444 13609 : tree len = c_strlen (src, 1);
3445 13609 : if (len && TREE_CODE (len) == INTEGER_CST)
3446 : {
3447 0 : expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3448 0 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3449 : }
3450 :
3451 13609 : unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3452 :
3453 : /* If SRC is not a pointer type, don't do this operation inline. */
3454 13609 : if (align == 0)
3455 : return NULL_RTX;
3456 :
3457 : /* Bail out if we can't compute strlen in the right mode. */
3458 : machine_mode insn_mode;
3459 : enum insn_code icode = CODE_FOR_nothing;
3460 13609 : FOR_EACH_MODE_FROM (insn_mode, target_mode)
3461 : {
3462 13609 : icode = optab_handler (strlen_optab, insn_mode);
3463 13609 : if (icode != CODE_FOR_nothing)
3464 : break;
3465 : }
3466 13609 : if (insn_mode == VOIDmode)
3467 : return NULL_RTX;
3468 :
3469 : /* Make a place to hold the source address. We will not expand
3470 : the actual source until we are sure that the expansion will
3471 : not fail -- there are trees that cannot be expanded twice. */
3472 14227 : rtx src_reg = gen_reg_rtx (Pmode);
3473 :
3474 : /* Mark the beginning of the strlen sequence so we can emit the
3475 : source operand later. */
3476 13609 : rtx_insn *before_strlen = get_last_insn ();
3477 :
3478 13609 : class expand_operand ops[4];
3479 13609 : create_output_operand (&ops[0], target, insn_mode);
3480 13609 : create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3481 13609 : create_integer_operand (&ops[2], 0);
3482 13609 : create_integer_operand (&ops[3], align);
3483 13609 : if (!maybe_expand_insn (icode, 4, ops))
3484 : return NULL_RTX;
3485 :
3486 : /* Check to see if the argument was declared attribute nonstring
3487 : and if so, issue a warning since at this point it's not known
3488 : to be nul-terminated. */
3489 11 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3490 :
3491 : /* Now that we are assured of success, expand the source. */
3492 11 : start_sequence ();
3493 15 : rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3494 11 : if (pat != src_reg)
3495 : {
3496 : #ifdef POINTERS_EXTEND_UNSIGNED
3497 15 : if (GET_MODE (pat) != Pmode)
3498 0 : pat = convert_to_mode (Pmode, pat,
3499 : POINTERS_EXTEND_UNSIGNED);
3500 : #endif
3501 11 : emit_move_insn (src_reg, pat);
3502 : }
3503 11 : pat = end_sequence ();
3504 :
3505 11 : if (before_strlen)
3506 11 : emit_insn_after (pat, before_strlen);
3507 : else
3508 0 : emit_insn_before (pat, get_insns ());
3509 :
3510 : /* Return the value in the proper mode for this function. */
3511 11 : if (GET_MODE (ops[0].value) == target_mode)
3512 : target = ops[0].value;
3513 0 : else if (target != 0)
3514 0 : convert_move (target, ops[0].value, 0);
3515 : else
3516 0 : target = convert_to_mode (target_mode, ops[0].value, 0);
3517 :
3518 : return target;
3519 : }
3520 :
3521 : /* Expand call EXP to the strnlen built-in, returning the result
3522 : and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3523 :
3524 : static rtx
3525 582 : expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3526 : {
3527 582 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3528 : return NULL_RTX;
3529 :
3530 573 : tree src = CALL_EXPR_ARG (exp, 0);
3531 573 : tree bound = CALL_EXPR_ARG (exp, 1);
3532 :
3533 573 : if (!bound)
3534 : return NULL_RTX;
3535 :
3536 573 : location_t loc = UNKNOWN_LOCATION;
3537 573 : if (EXPR_HAS_LOCATION (exp))
3538 573 : loc = EXPR_LOCATION (exp);
3539 :
3540 : /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3541 : so these conversions aren't necessary. */
3542 573 : c_strlen_data lendata = { };
3543 573 : tree len = c_strlen (src, 0, &lendata, 1);
3544 573 : if (len)
3545 63 : len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3546 :
3547 573 : if (TREE_CODE (bound) == INTEGER_CST)
3548 : {
3549 306 : if (!len)
3550 : return NULL_RTX;
3551 :
3552 23 : len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3553 23 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3554 : }
3555 :
3556 267 : if (TREE_CODE (bound) != SSA_NAME)
3557 : return NULL_RTX;
3558 :
3559 267 : wide_int min, max;
3560 267 : int_range_max r;
3561 534 : get_range_query (cfun)->range_of_expr (r, bound,
3562 : currently_expanding_gimple_stmt);
3563 267 : if (r.varying_p () || r.undefined_p ())
3564 : return NULL_RTX;
3565 152 : min = r.lower_bound ();
3566 152 : max = r.upper_bound ();
3567 :
3568 152 : if (!len || TREE_CODE (len) != INTEGER_CST)
3569 : {
3570 115 : bool exact;
3571 115 : lendata.decl = unterminated_array (src, &len, &exact);
3572 115 : if (!lendata.decl)
3573 95 : return NULL_RTX;
3574 : }
3575 :
3576 57 : if (lendata.decl)
3577 : return NULL_RTX;
3578 :
3579 37 : if (wi::gtu_p (min, wi::to_wide (len)))
3580 7 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3581 :
3582 30 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3583 30 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3584 267 : }
3585 :
3586 : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3587 : bytes from bytes at DATA + OFFSET and return it reinterpreted as
3588 : a target constant. */
3589 :
3590 : static rtx
3591 164817 : builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3592 : fixed_size_mode mode)
3593 : {
3594 : /* The REPresentation pointed to by DATA need not be a nul-terminated
3595 : string but the caller guarantees it's large enough for MODE. */
3596 164817 : const char *rep = (const char *) data;
3597 :
3598 164817 : return c_readstr (rep + offset, mode, /*nul_terminated=*/false);
3599 : }
3600 :
3601 : /* LEN specify length of the block of memcpy/memset operation.
3602 : Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3603 : In some cases we can make very likely guess on max size, then we
3604 : set it into PROBABLE_MAX_SIZE. */
3605 :
3606 : static void
3607 190471 : determine_block_size (tree len, rtx &len_rtx,
3608 : unsigned HOST_WIDE_INT *min_size,
3609 : unsigned HOST_WIDE_INT *max_size,
3610 : unsigned HOST_WIDE_INT *probable_max_size)
3611 : {
3612 190471 : if (CONST_INT_P (len_rtx))
3613 : {
3614 97111 : *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3615 97111 : return;
3616 : }
3617 : else
3618 : {
3619 93360 : wide_int min, max;
3620 93360 : enum value_range_kind range_type = VR_UNDEFINED;
3621 :
3622 : /* Determine bounds from the type. */
3623 93360 : if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3624 93359 : *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3625 : else
3626 : *min_size = 0;
3627 93360 : if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3628 93360 : *probable_max_size = *max_size
3629 93360 : = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3630 : else
3631 0 : *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3632 :
3633 93360 : if (TREE_CODE (len) == SSA_NAME)
3634 : {
3635 93360 : int_range_max r;
3636 93360 : tree tmin, tmax;
3637 93360 : gimple *cg = currently_expanding_gimple_stmt;
3638 186720 : get_range_query (cfun)->range_of_expr (r, len, cg);
3639 93360 : range_type = get_legacy_range (r, tmin, tmax);
3640 93360 : if (range_type != VR_UNDEFINED)
3641 : {
3642 93360 : min = wi::to_wide (tmin);
3643 93360 : max = wi::to_wide (tmax);
3644 : }
3645 93360 : }
3646 93360 : if (range_type == VR_RANGE)
3647 : {
3648 66400 : if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3649 52559 : *min_size = min.to_uhwi ();
3650 66400 : if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3651 60435 : *probable_max_size = *max_size = max.to_uhwi ();
3652 : }
3653 26960 : else if (range_type == VR_ANTI_RANGE)
3654 : {
3655 : /* Code like
3656 :
3657 : int n;
3658 : if (n < 100)
3659 : memcpy (a, b, n)
3660 :
3661 : Produce anti range allowing negative values of N. We still
3662 : can use the information and make a guess that N is not negative.
3663 : */
3664 3142 : if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3665 2993 : *probable_max_size = min.to_uhwi () - 1;
3666 : }
3667 93360 : }
3668 :
3669 93360 : if (*min_size == *max_size)
3670 18 : len_rtx = GEN_INT (*min_size);
3671 : else
3672 93342 : gcc_checking_assert (*max_size <=
3673 : (unsigned HOST_WIDE_INT)
3674 : GET_MODE_MASK (GET_MODE (len_rtx)));
3675 : }
3676 :
3677 : /* Expand a call EXP to the memcpy builtin.
3678 : Return NULL_RTX if we failed, the caller should emit a normal call,
3679 : otherwise try to get the result in TARGET, if convenient (and in
3680 : mode MODE if that's convenient). */
3681 :
3682 : static rtx
3683 107001 : expand_builtin_memcpy (tree exp, rtx target)
3684 : {
3685 107001 : if (!validate_arglist (exp,
3686 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3687 : return NULL_RTX;
3688 :
3689 106983 : tree dest = CALL_EXPR_ARG (exp, 0);
3690 106983 : tree src = CALL_EXPR_ARG (exp, 1);
3691 106983 : tree len = CALL_EXPR_ARG (exp, 2);
3692 :
3693 106983 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3694 106983 : /*retmode=*/ RETURN_BEGIN, false);
3695 : }
3696 :
3697 : /* Check a call EXP to the memmove built-in for validity.
3698 : Return NULL_RTX on both success and failure. */
3699 :
3700 : static rtx
3701 17420 : expand_builtin_memmove (tree exp, rtx target)
3702 : {
3703 17420 : if (!validate_arglist (exp,
3704 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3705 : return NULL_RTX;
3706 :
3707 17405 : tree dest = CALL_EXPR_ARG (exp, 0);
3708 17405 : tree src = CALL_EXPR_ARG (exp, 1);
3709 17405 : tree len = CALL_EXPR_ARG (exp, 2);
3710 :
3711 17405 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3712 17405 : /*retmode=*/ RETURN_BEGIN, true);
3713 : }
3714 :
3715 : /* Expand a call EXP to the mempcpy builtin.
3716 : Return NULL_RTX if we failed; the caller should emit a normal call,
3717 : otherwise try to get the result in TARGET, if convenient (and in
3718 : mode MODE if that's convenient). */
3719 :
3720 : static rtx
3721 1395 : expand_builtin_mempcpy (tree exp, rtx target)
3722 : {
3723 1395 : if (!validate_arglist (exp,
3724 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3725 : return NULL_RTX;
3726 :
3727 1390 : tree dest = CALL_EXPR_ARG (exp, 0);
3728 1390 : tree src = CALL_EXPR_ARG (exp, 1);
3729 1390 : tree len = CALL_EXPR_ARG (exp, 2);
3730 :
3731 : /* Policy does not generally allow using compute_objsize (which
3732 : is used internally by check_memop_size) to change code generation
3733 : or drive optimization decisions.
3734 :
3735 : In this instance it is safe because the code we generate has
3736 : the same semantics regardless of the return value of
3737 : check_memop_sizes. Exactly the same amount of data is copied
3738 : and the return value is exactly the same in both cases.
3739 :
3740 : Furthermore, check_memop_size always uses mode 0 for the call to
3741 : compute_objsize, so the imprecise nature of compute_objsize is
3742 : avoided. */
3743 :
3744 : /* Avoid expanding mempcpy into memcpy when the call is determined
3745 : to overflow the buffer. This also prevents the same overflow
3746 : from being diagnosed again when expanding memcpy. */
3747 :
3748 1390 : return expand_builtin_mempcpy_args (dest, src, len,
3749 1390 : target, exp, /*retmode=*/ RETURN_END);
3750 : }
3751 :
3752 : /* Helper function to do the actual work for expand of memory copy family
3753 : functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3754 : of memory from SRC to DEST and assign to TARGET if convenient. Return
3755 : value is based on RETMODE argument. */
3756 :
3757 : static rtx
3758 125801 : expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3759 : rtx target, tree exp, memop_ret retmode,
3760 : bool might_overlap)
3761 : {
3762 125801 : unsigned int src_align = get_pointer_alignment (src);
3763 125801 : unsigned int dest_align = get_pointer_alignment (dest);
3764 125801 : rtx dest_mem, src_mem, dest_addr, len_rtx;
3765 125801 : HOST_WIDE_INT expected_size = -1;
3766 125801 : unsigned int expected_align = 0;
3767 125801 : unsigned HOST_WIDE_INT min_size;
3768 125801 : unsigned HOST_WIDE_INT max_size;
3769 125801 : unsigned HOST_WIDE_INT probable_max_size;
3770 :
3771 125801 : bool is_move_done;
3772 :
3773 : /* If DEST is not a pointer type, call the normal function. */
3774 125801 : if (dest_align == 0)
3775 : return NULL_RTX;
3776 :
3777 : /* If either SRC is not a pointer type, don't do this
3778 : operation in-line. */
3779 125801 : if (src_align == 0)
3780 : return NULL_RTX;
3781 :
3782 125801 : if (currently_expanding_gimple_stmt)
3783 125801 : stringop_block_profile (currently_expanding_gimple_stmt,
3784 : &expected_align, &expected_size);
3785 :
3786 125801 : if (expected_align < dest_align)
3787 125779 : expected_align = dest_align;
3788 125801 : dest_mem = get_memory_rtx (dest, len, true);
3789 125801 : set_mem_align (dest_mem, dest_align);
3790 125801 : len_rtx = expand_normal (len);
3791 125801 : determine_block_size (len, len_rtx, &min_size, &max_size,
3792 : &probable_max_size);
3793 :
3794 : /* Try to get the byte representation of the constant SRC points to,
3795 : with its byte size in NBYTES. */
3796 125801 : unsigned HOST_WIDE_INT nbytes;
3797 125801 : const char *rep = getbyterep (src, &nbytes);
3798 :
3799 : /* If the function's constant bound LEN_RTX is less than or equal
3800 : to the byte size of the representation of the constant argument,
3801 : and if block move would be done by pieces, we can avoid loading
3802 : the bytes from memory and only store the computed constant.
3803 : This works in the overlap (memmove) case as well because
3804 : store_by_pieces just generates a series of stores of constants
3805 : from the representation returned by getbyterep(). */
3806 125801 : if (rep
3807 30389 : && CONST_INT_P (len_rtx)
3808 28554 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3809 154068 : && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3810 : const_cast<char *> (rep),
3811 : dest_align, false))
3812 : {
3813 27784 : dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3814 : builtin_memcpy_read_str,
3815 : const_cast<char *> (rep),
3816 : dest_align, false, retmode);
3817 27784 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
3818 27784 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
3819 27784 : return dest_mem;
3820 : }
3821 :
3822 98017 : src_mem = get_memory_rtx (src, len, false);
3823 98017 : set_mem_align (src_mem, src_align);
3824 :
3825 : /* Copy word part most expediently. */
3826 98017 : enum block_op_methods method = BLOCK_OP_NORMAL;
3827 98017 : if (CALL_EXPR_TAILCALL (exp)
3828 98017 : && (retmode == RETURN_BEGIN || target == const0_rtx))
3829 : method = BLOCK_OP_TAILCALL;
3830 98017 : bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3831 98017 : && retmode == RETURN_END
3832 98017 : && !might_overlap
3833 99092 : && target != const0_rtx);
3834 : if (use_mempcpy_call)
3835 98017 : method = BLOCK_OP_NO_LIBCALL_RET;
3836 98017 : dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3837 : expected_align, expected_size,
3838 : min_size, max_size, probable_max_size,
3839 : use_mempcpy_call, &is_move_done,
3840 : might_overlap, tree_ctz (len));
3841 :
3842 : /* Bail out when a mempcpy call would be expanded as libcall and when
3843 : we have a target that provides a fast implementation
3844 : of mempcpy routine. */
3845 98017 : if (!is_move_done)
3846 : return NULL_RTX;
3847 :
3848 85098 : if (dest_addr == pc_rtx)
3849 : return NULL_RTX;
3850 :
3851 85098 : if (dest_addr == 0)
3852 : {
3853 32940 : dest_addr = force_operand (XEXP (dest_mem, 0), target);
3854 32940 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
3855 : }
3856 :
3857 85098 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3858 : {
3859 815 : dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3860 : /* stpcpy pointer to last byte. */
3861 815 : if (retmode == RETURN_END_MINUS_ONE)
3862 0 : dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3863 : }
3864 :
3865 : return dest_addr;
3866 : }
3867 :
3868 : static rtx
3869 1413 : expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3870 : rtx target, tree orig_exp, memop_ret retmode)
3871 : {
3872 1390 : return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3873 0 : retmode, false);
3874 : }
3875 :
3876 : /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3877 : we failed, the caller should emit a normal call, otherwise try to
3878 : get the result in TARGET, if convenient.
3879 : Return value is based on RETMODE argument. */
3880 :
3881 : static rtx
3882 2268 : expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3883 : {
3884 2268 : class expand_operand ops[3];
3885 2268 : rtx dest_mem;
3886 2268 : rtx src_mem;
3887 :
3888 2268 : if (!targetm.have_movstr ())
3889 : return NULL_RTX;
3890 :
3891 0 : dest_mem = get_memory_rtx (dest, NULL, true);
3892 0 : src_mem = get_memory_rtx (src, NULL, false);
3893 0 : if (retmode == RETURN_BEGIN)
3894 : {
3895 0 : target = force_reg (Pmode, XEXP (dest_mem, 0));
3896 0 : dest_mem = replace_equiv_address (dest_mem, target);
3897 : }
3898 :
3899 0 : create_output_operand (&ops[0],
3900 0 : retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3901 0 : create_fixed_operand (&ops[1], dest_mem);
3902 0 : create_fixed_operand (&ops[2], src_mem);
3903 0 : if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3904 : return NULL_RTX;
3905 :
3906 0 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3907 : {
3908 0 : target = ops[0].value;
3909 : /* movstr is supposed to set end to the address of the NUL
3910 : terminator. If the caller requested a mempcpy-like return value,
3911 : adjust it. */
3912 0 : if (retmode == RETURN_END)
3913 : {
3914 0 : rtx tem = plus_constant (GET_MODE (target),
3915 0 : gen_lowpart (GET_MODE (target), target), 1);
3916 0 : emit_move_insn (target, force_operand (tem, NULL_RTX));
3917 : }
3918 : }
3919 : return target;
3920 : }
3921 :
3922 : /* Expand expression EXP, which is a call to the strcpy builtin. Return
3923 : NULL_RTX if we failed the caller should emit a normal call, otherwise
3924 : try to get the result in TARGET, if convenient (and in mode MODE if that's
3925 : convenient). */
3926 :
3927 : static rtx
3928 1861 : expand_builtin_strcpy (tree exp, rtx target)
3929 : {
3930 1861 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3931 : return NULL_RTX;
3932 :
3933 1846 : tree dest = CALL_EXPR_ARG (exp, 0);
3934 1846 : tree src = CALL_EXPR_ARG (exp, 1);
3935 :
3936 1846 : return expand_builtin_strcpy_args (exp, dest, src, target);
3937 : }
3938 :
3939 : /* Helper function to do the actual work for expand_builtin_strcpy. The
3940 : arguments to the builtin_strcpy call DEST and SRC are broken out
3941 : so that this can also be called without constructing an actual CALL_EXPR.
3942 : The other arguments and return value are the same as for
3943 : expand_builtin_strcpy. */
3944 :
3945 : static rtx
3946 1846 : expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3947 : {
3948 1846 : return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3949 : }
3950 :
3951 : /* Expand a call EXP to the stpcpy builtin.
3952 : Return NULL_RTX if we failed the caller should emit a normal call,
3953 : otherwise try to get the result in TARGET, if convenient (and in
3954 : mode MODE if that's convenient). */
3955 :
3956 : static rtx
3957 454 : expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3958 : {
3959 454 : tree dst, src;
3960 454 : location_t loc = EXPR_LOCATION (exp);
3961 :
3962 454 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3963 : return NULL_RTX;
3964 :
3965 445 : dst = CALL_EXPR_ARG (exp, 0);
3966 445 : src = CALL_EXPR_ARG (exp, 1);
3967 :
3968 : /* If return value is ignored, transform stpcpy into strcpy. */
3969 445 : if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3970 : {
3971 0 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3972 0 : tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3973 0 : return expand_expr (result, target, mode, EXPAND_NORMAL);
3974 : }
3975 : else
3976 : {
3977 445 : tree len, lenp1;
3978 445 : rtx ret;
3979 :
3980 : /* Ensure we get an actual string whose length can be evaluated at
3981 : compile-time, not an expression containing a string. This is
3982 : because the latter will potentially produce pessimized code
3983 : when used to produce the return value. */
3984 445 : c_strlen_data lendata = { };
3985 445 : if (!c_getstr (src)
3986 445 : || !(len = c_strlen (src, 0, &lendata, 1)))
3987 422 : return expand_movstr (dst, src, target,
3988 422 : /*retmode=*/ RETURN_END_MINUS_ONE);
3989 :
3990 23 : lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3991 23 : ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3992 : target, exp,
3993 : /*retmode=*/ RETURN_END_MINUS_ONE);
3994 :
3995 23 : if (ret)
3996 : return ret;
3997 :
3998 0 : if (TREE_CODE (len) == INTEGER_CST)
3999 : {
4000 0 : rtx len_rtx = expand_normal (len);
4001 :
4002 0 : if (CONST_INT_P (len_rtx))
4003 : {
4004 0 : ret = expand_builtin_strcpy_args (exp, dst, src, target);
4005 :
4006 0 : if (ret)
4007 : {
4008 0 : if (! target)
4009 : {
4010 0 : if (mode != VOIDmode)
4011 0 : target = gen_reg_rtx (mode);
4012 : else
4013 0 : target = gen_reg_rtx (GET_MODE (ret));
4014 : }
4015 0 : if (GET_MODE (target) != GET_MODE (ret))
4016 0 : ret = gen_lowpart (GET_MODE (target), ret);
4017 :
4018 0 : ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
4019 0 : ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
4020 0 : gcc_assert (ret);
4021 :
4022 : return target;
4023 : }
4024 : }
4025 : }
4026 :
4027 0 : return expand_movstr (dst, src, target,
4028 0 : /*retmode=*/ RETURN_END_MINUS_ONE);
4029 : }
4030 : }
4031 :
4032 : /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
4033 : arguments while being careful to avoid duplicate warnings (which could
4034 : be issued if the expander were to expand the call, resulting in it
4035 : being emitted in expand_call(). */
4036 :
4037 : static rtx
4038 454 : expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
4039 : {
4040 454 : if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
4041 : {
4042 : /* The call has been successfully expanded. Check for nonstring
4043 : arguments and issue warnings as appropriate. */
4044 23 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
4045 23 : return ret;
4046 : }
4047 :
4048 : return NULL_RTX;
4049 : }
4050 :
4051 : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4052 : bytes from constant string DATA + OFFSET and return it as target
4053 : constant. */
4054 :
4055 : rtx
4056 10055 : builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
4057 : fixed_size_mode mode)
4058 : {
4059 10055 : const char *str = (const char *) data;
4060 :
4061 10055 : if ((unsigned HOST_WIDE_INT) offset > strlen (str))
4062 260 : return const0_rtx;
4063 :
4064 9795 : return c_readstr (str + offset, mode);
4065 : }
4066 :
4067 : /* Helper to check the sizes of sequences and the destination of calls
4068 : to __builtin_strncat and __builtin___strncat_chk. Returns true on
4069 : success (no overflow or invalid sizes), false otherwise. */
4070 :
4071 : static bool
4072 108 : check_strncat_sizes (tree exp, tree objsize)
4073 : {
4074 108 : tree dest = CALL_EXPR_ARG (exp, 0);
4075 108 : tree src = CALL_EXPR_ARG (exp, 1);
4076 108 : tree maxread = CALL_EXPR_ARG (exp, 2);
4077 :
4078 : /* Try to determine the range of lengths that the source expression
4079 : refers to. */
4080 108 : c_strlen_data lendata = { };
4081 108 : get_range_strlen (src, &lendata, /* eltsize = */ 1);
4082 :
4083 : /* Try to verify that the destination is big enough for the shortest
4084 : string. */
4085 :
4086 108 : access_data data (nullptr, exp, access_read_write, maxread, true);
4087 108 : if (!objsize && warn_stringop_overflow)
4088 : {
4089 : /* If it hasn't been provided by __strncat_chk, try to determine
4090 : the size of the destination object into which the source is
4091 : being copied. */
4092 0 : objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4093 : }
4094 :
4095 : /* Add one for the terminating nul. */
4096 108 : tree srclen = (lendata.minlen
4097 108 : ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4098 : size_one_node)
4099 : : NULL_TREE);
4100 :
4101 : /* The strncat function copies at most MAXREAD bytes and always appends
4102 : the terminating nul so the specified upper bound should never be equal
4103 : to (or greater than) the size of the destination. */
4104 55 : if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4105 155 : && tree_int_cst_equal (objsize, maxread))
4106 : {
4107 23 : location_t loc = EXPR_LOCATION (exp);
4108 23 : warning_at (loc, OPT_Wstringop_overflow_,
4109 : "%qD specified bound %E equals destination size",
4110 : get_callee_fndecl (exp), maxread);
4111 :
4112 23 : return false;
4113 : }
4114 :
4115 85 : if (!srclen
4116 85 : || (maxread && tree_fits_uhwi_p (maxread)
4117 32 : && tree_fits_uhwi_p (srclen)
4118 32 : && tree_int_cst_lt (maxread, srclen)))
4119 : srclen = maxread;
4120 :
4121 : /* The number of bytes to write is LEN but check_access will alsoa
4122 : check SRCLEN if LEN's value isn't known. */
4123 85 : return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4124 85 : objsize, data.mode, &data);
4125 : }
4126 :
4127 : /* Expand expression EXP, which is a call to the strncpy builtin. Return
4128 : NULL_RTX if we failed the caller should emit a normal call. */
4129 :
4130 : static rtx
4131 2177 : expand_builtin_strncpy (tree exp, rtx target)
4132 : {
4133 2177 : location_t loc = EXPR_LOCATION (exp);
4134 :
4135 2177 : if (!validate_arglist (exp,
4136 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4137 : return NULL_RTX;
4138 2165 : tree dest = CALL_EXPR_ARG (exp, 0);
4139 2165 : tree src = CALL_EXPR_ARG (exp, 1);
4140 : /* The number of bytes to write (not the maximum). */
4141 2165 : tree len = CALL_EXPR_ARG (exp, 2);
4142 :
4143 : /* The length of the source sequence. */
4144 2165 : tree slen = c_strlen (src, 1);
4145 :
4146 : /* We must be passed a constant len and src parameter. */
4147 2165 : if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4148 : return NULL_RTX;
4149 :
4150 237 : slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4151 :
4152 : /* We're required to pad with trailing zeros if the requested
4153 : len is greater than strlen(s2)+1. In that case try to
4154 : use store_by_pieces, if it fails, punt. */
4155 237 : if (tree_int_cst_lt (slen, len))
4156 : {
4157 142 : unsigned int dest_align = get_pointer_alignment (dest);
4158 142 : const char *p = c_getstr (src);
4159 142 : rtx dest_mem;
4160 :
4161 136 : if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4162 278 : || !can_store_by_pieces (tree_to_uhwi (len),
4163 : builtin_strncpy_read_str,
4164 : const_cast<char *> (p),
4165 : dest_align, false))
4166 : return NULL_RTX;
4167 :
4168 128 : dest_mem = get_memory_rtx (dest, len, true);
4169 128 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4170 : builtin_strncpy_read_str,
4171 : const_cast<char *> (p), dest_align, false,
4172 : RETURN_BEGIN);
4173 128 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
4174 128 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4175 128 : return dest_mem;
4176 : }
4177 :
4178 : return NULL_RTX;
4179 : }
4180 :
4181 : /* Return the RTL of a register in MODE generated from PREV in the
4182 : previous iteration. */
4183 :
4184 : static rtx
4185 259270 : gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4186 : {
4187 259270 : rtx target = nullptr;
4188 259270 : if (prev != nullptr && prev->data != nullptr)
4189 : {
4190 : /* Use the previous data in the same mode. */
4191 143285 : if (prev->mode == mode)
4192 259270 : return prev->data;
4193 :
4194 32961 : fixed_size_mode prev_mode = prev->mode;
4195 :
4196 : /* Don't use the previous data to write QImode if it is in a
4197 : vector mode. */
4198 32961 : if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4199 : return target;
4200 :
4201 32562 : rtx prev_rtx = prev->data;
4202 :
4203 32562 : if (REG_P (prev_rtx)
4204 24123 : && HARD_REGISTER_P (prev_rtx)
4205 32562 : && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4206 : {
4207 : /* This case occurs when PREV_MODE is a vector and when
4208 : MODE is too small to store using vector operations.
4209 : After register allocation, the code will need to move the
4210 : lowpart of the vector register into a non-vector register.
4211 :
4212 : Also, the target has chosen to use a hard register
4213 : instead of going with the default choice of using a
4214 : pseudo register. We should respect that choice and try to
4215 : avoid creating a pseudo register with the same mode as the
4216 : current hard register.
4217 :
4218 : In principle, we could just use a lowpart MODE subreg of
4219 : the vector register. However, the vector register mode might
4220 : be too wide for non-vector registers, and we already know
4221 : that the non-vector mode is too small for vector registers.
4222 : It's therefore likely that we'd need to spill to memory in
4223 : the vector mode and reload the non-vector value from there.
4224 :
4225 : Try to avoid that by reducing the vector register to the
4226 : smallest size that it can hold. This should increase the
4227 : chances that non-vector registers can hold both the inner
4228 : and outer modes of the subreg that we generate later. */
4229 0 : machine_mode m;
4230 0 : fixed_size_mode candidate;
4231 0 : FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4232 0 : if (is_a<fixed_size_mode> (m, &candidate))
4233 : {
4234 0 : if (GET_MODE_SIZE (candidate)
4235 0 : >= GET_MODE_SIZE (prev_mode))
4236 : break;
4237 0 : if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4238 0 : && lowpart_subreg_regno (REGNO (prev_rtx),
4239 : prev_mode, candidate) >= 0)
4240 : {
4241 0 : target = lowpart_subreg (candidate, prev_rtx,
4242 : prev_mode);
4243 0 : prev_rtx = target;
4244 0 : prev_mode = candidate;
4245 0 : break;
4246 : }
4247 : }
4248 0 : if (target == nullptr)
4249 0 : prev_rtx = copy_to_reg (prev_rtx);
4250 : }
4251 :
4252 32562 : target = lowpart_subreg (mode, prev_rtx, prev_mode);
4253 : }
4254 : return target;
4255 : }
4256 :
4257 : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4258 : bytes from constant string DATA + OFFSET and return it as target
4259 : constant. If PREV isn't nullptr, it has the RTL info from the
4260 : previous iteration. */
4261 :
4262 : rtx
4263 257970 : builtin_memset_read_str (void *data, void *prev,
4264 : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4265 : fixed_size_mode mode)
4266 : {
4267 257970 : const char *c = (const char *) data;
4268 257970 : unsigned int size = GET_MODE_SIZE (mode);
4269 :
4270 257970 : rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4271 : mode);
4272 257970 : if (target != nullptr)
4273 : return target;
4274 115651 : rtx src = gen_int_mode (*c, QImode);
4275 :
4276 115651 : if (VECTOR_MODE_P (mode))
4277 : {
4278 167628 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4279 :
4280 83814 : rtx const_vec = gen_const_vec_duplicate (mode, src);
4281 83814 : if (prev == NULL)
4282 : /* Return CONST_VECTOR when called by a query function. */
4283 : return const_vec;
4284 :
4285 : /* Use the move expander with CONST_VECTOR. */
4286 61819 : target = gen_reg_rtx (mode);
4287 61819 : emit_move_insn (target, const_vec);
4288 61819 : return target;
4289 : }
4290 :
4291 31837 : char *p = XALLOCAVEC (char, size);
4292 :
4293 31837 : memset (p, *c, size);
4294 :
4295 31837 : return c_readstr (p, mode);
4296 : }
4297 :
4298 : /* Callback routine for store_by_pieces. Return the RTL of a register
4299 : containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4300 : char value given in the RTL register data. For example, if mode is
4301 : 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4302 : nullptr, it has the RTL info from the previous iteration. */
4303 :
4304 : static rtx
4305 1439 : builtin_memset_gen_str (void *data, void *prev,
4306 : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4307 : fixed_size_mode mode)
4308 : {
4309 1439 : rtx target, coeff;
4310 1439 : size_t size;
4311 1439 : char *p;
4312 :
4313 1439 : size = GET_MODE_SIZE (mode);
4314 1439 : if (size == 1)
4315 : return (rtx) data;
4316 :
4317 1300 : target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4318 1300 : if (target != nullptr)
4319 : return target;
4320 :
4321 733 : if (VECTOR_MODE_P (mode))
4322 : {
4323 378 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4324 :
4325 : /* vec_duplicate_optab is a precondition to pick a vector mode for
4326 : the memset expander. */
4327 189 : insn_code icode = optab_handler (vec_duplicate_optab, mode);
4328 :
4329 189 : target = gen_reg_rtx (mode);
4330 189 : class expand_operand ops[2];
4331 189 : create_output_operand (&ops[0], target, mode);
4332 189 : create_input_operand (&ops[1], (rtx) data, QImode);
4333 189 : expand_insn (icode, 2, ops);
4334 189 : if (!rtx_equal_p (target, ops[0].value))
4335 0 : emit_move_insn (target, ops[0].value);
4336 :
4337 189 : return target;
4338 : }
4339 :
4340 544 : p = XALLOCAVEC (char, size);
4341 544 : memset (p, 1, size);
4342 544 : coeff = c_readstr (p, mode);
4343 :
4344 544 : target = convert_to_mode (mode, (rtx) data, 1);
4345 544 : target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4346 544 : return force_reg (mode, target);
4347 : }
4348 :
4349 : /* Expand expression EXP, which is a call to the memset builtin. Return
4350 : NULL_RTX if we failed the caller should emit a normal call, otherwise
4351 : try to get the result in TARGET, if convenient (and in mode MODE if that's
4352 : convenient). */
4353 :
4354 : rtx
4355 64679 : expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4356 : {
4357 64679 : if (!validate_arglist (exp,
4358 : POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4359 : return NULL_RTX;
4360 :
4361 64670 : tree dest = CALL_EXPR_ARG (exp, 0);
4362 64670 : tree val = CALL_EXPR_ARG (exp, 1);
4363 64670 : tree len = CALL_EXPR_ARG (exp, 2);
4364 :
4365 64670 : return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4366 : }
4367 :
4368 : /* Check that store_by_pieces allows BITS + LEN (so that we don't
4369 : expand something too unreasonably long), and every power of 2 in
4370 : BITS. It is assumed that LEN has already been tested by
4371 : itself. */
4372 : static bool
4373 26429 : can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
4374 : by_pieces_constfn constfun,
4375 : void *constfundata, unsigned int align,
4376 : bool memsetp,
4377 : unsigned HOST_WIDE_INT len)
4378 : {
4379 26429 : if (bits
4380 26429 : && !can_store_by_pieces (bits + len, constfun, constfundata,
4381 : align, memsetp))
4382 : return false;
4383 :
4384 : /* BITS set are expected to be generally in the low range and
4385 : contiguous. We do NOT want to repeat the test above in case BITS
4386 : has a single bit set, so we terminate the loop when BITS == BIT.
4387 : In the unlikely case that BITS has the MSB set, also terminate in
4388 : case BIT gets shifted out. */
4389 1122 : for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
4390 : {
4391 840 : if ((bits & bit) == 0)
4392 504 : continue;
4393 :
4394 336 : if (!can_store_by_pieces (bit, constfun, constfundata,
4395 : align, memsetp))
4396 : return false;
4397 : }
4398 :
4399 : return true;
4400 : }
4401 :
4402 : /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4403 : Return TRUE if successful, FALSE otherwise. TO is assumed to be
4404 : aligned at an ALIGN-bits boundary. LEN must be a multiple of
4405 : 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4406 :
4407 : The strategy is to issue one store_by_pieces for each power of two,
4408 : from most to least significant, guarded by a test on whether there
4409 : are at least that many bytes left to copy in LEN.
4410 :
4411 : ??? Should we skip some powers of two in favor of loops? Maybe start
4412 : at the max of TO/LEN/word alignment, at least when optimizing for
4413 : size, instead of ensuring O(log len) dynamic compares? */
4414 :
4415 : bool
4416 24288 : try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4417 : unsigned HOST_WIDE_INT min_len,
4418 : unsigned HOST_WIDE_INT max_len,
4419 : rtx val, char valc, unsigned int align)
4420 : {
4421 24288 : int max_bits = floor_log2 (max_len);
4422 24288 : int min_bits = floor_log2 (min_len);
4423 24288 : int sctz_len = ctz_len;
4424 :
4425 24288 : gcc_checking_assert (sctz_len >= 0);
4426 :
4427 24288 : if (val)
4428 637 : valc = 1;
4429 :
4430 : /* Bits more significant than TST_BITS are part of the shared prefix
4431 : in the binary representation of both min_len and max_len. Since
4432 : they're identical, we don't need to test them in the loop. */
4433 24288 : int tst_bits = (max_bits != min_bits ? max_bits
4434 11600 : : floor_log2 (max_len ^ min_len));
4435 :
4436 : /* Save the pre-blksize values. */
4437 24288 : int orig_max_bits = max_bits;
4438 24288 : int orig_tst_bits = tst_bits;
4439 :
4440 : /* Check whether it's profitable to start by storing a fixed BLKSIZE
4441 : bytes, to lower max_bits. In the unlikely case of a constant LEN
4442 : (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4443 : single store_by_pieces, but otherwise, select the minimum multiple
4444 : of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4445 : brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4446 24288 : unsigned HOST_WIDE_INT blksize;
4447 24288 : if (max_len > min_len)
4448 : {
4449 12701 : unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4450 : align / BITS_PER_UNIT);
4451 12701 : blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4452 12701 : blksize &= ~(alrng - 1);
4453 : }
4454 11587 : else if (max_len == min_len)
4455 : blksize = max_len;
4456 : else
4457 : /* Huh, max_len < min_len? Punt. See pr100843.c. */
4458 : return false;
4459 24287 : if (min_len >= blksize
4460 : /* ??? Maybe try smaller fixed-prefix blksizes before
4461 : punting? */
4462 24287 : && can_store_by_pieces (blksize, builtin_memset_read_str,
4463 : &valc, align, true))
4464 : {
4465 308 : min_len -= blksize;
4466 308 : min_bits = floor_log2 (min_len);
4467 308 : max_len -= blksize;
4468 308 : max_bits = floor_log2 (max_len);
4469 :
4470 308 : tst_bits = (max_bits != min_bits ? max_bits
4471 175 : : floor_log2 (max_len ^ min_len));
4472 : }
4473 : else
4474 : blksize = 0;
4475 :
4476 : /* Check that we can use store by pieces for the maximum store count
4477 : we may issue (initial fixed-size block, plus conditional
4478 : power-of-two-sized from max_bits to ctz_len. */
4479 24287 : unsigned HOST_WIDE_INT xlenest = blksize;
4480 24287 : if (max_bits >= 0)
4481 24112 : xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4482 24112 : - (HOST_WIDE_INT_1U << ctz_len));
4483 24287 : bool max_loop = false;
4484 24287 : bool use_store_by_pieces = true;
4485 : /* Skip the test in case of overflow in xlenest. It shouldn't
4486 : happen because of the way max_bits and blksize are related, but
4487 : it doesn't hurt to test. */
4488 24287 : if (blksize > xlenest
4489 24287 : || !can_store_by_multiple_pieces (xlenest - blksize,
4490 : builtin_memset_read_str,
4491 : &valc, align, true, blksize))
4492 : {
4493 24112 : if (!(flag_inline_stringops & ILSOP_MEMSET))
4494 : return false;
4495 :
4496 2067 : for (max_bits = orig_max_bits;
4497 2174 : max_bits >= sctz_len;
4498 : --max_bits)
4499 : {
4500 2174 : xlenest = ((HOST_WIDE_INT_1U << max_bits) * 2
4501 2174 : - (HOST_WIDE_INT_1U << ctz_len));
4502 : /* Check that blksize plus the bits to be stored as blocks
4503 : sized at powers of two can be stored by pieces. This is
4504 : like the test above, but with smaller max_bits. Skip
4505 : orig_max_bits (it would be redundant). Also skip in case
4506 : of overflow. */
4507 2174 : if (max_bits < orig_max_bits
4508 2067 : && xlenest + blksize >= xlenest
4509 4241 : && can_store_by_multiple_pieces (xlenest,
4510 : builtin_memset_read_str,
4511 : &valc, align, true, blksize))
4512 : {
4513 : max_loop = true;
4514 : break;
4515 : }
4516 2067 : if (blksize
4517 2067 : && can_store_by_multiple_pieces (xlenest,
4518 : builtin_memset_read_str,
4519 : &valc, align, true, 0))
4520 : {
4521 0 : max_len += blksize;
4522 0 : min_len += blksize;
4523 0 : tst_bits = orig_tst_bits;
4524 0 : blksize = 0;
4525 0 : max_loop = true;
4526 0 : break;
4527 : }
4528 2067 : if (max_bits == sctz_len)
4529 : {
4530 : /* We'll get here if can_store_by_pieces refuses to
4531 : store even a single QImode. We'll fall back to
4532 : QImode stores then. */
4533 98 : if (!sctz_len)
4534 : {
4535 : blksize = 0;
4536 : max_loop = true;
4537 : use_store_by_pieces = false;
4538 : break;
4539 : }
4540 98 : --sctz_len;
4541 98 : --ctz_len;
4542 : }
4543 : }
4544 107 : if (!max_loop)
4545 : return false;
4546 : /* If the boundaries are such that min and max may run a
4547 : different number of trips in the initial loop, the remainder
4548 : needs not be between the moduli, so set tst_bits to cover all
4549 : bits. Otherwise, if the trip counts are the same, max_len
4550 : has the common prefix, and the previously-computed tst_bits
4551 : is usable. */
4552 107 : if (max_len >> max_bits > min_len >> max_bits)
4553 64 : tst_bits = max_bits;
4554 : }
4555 :
4556 282 : by_pieces_constfn constfun;
4557 282 : void *constfundata;
4558 282 : if (val)
4559 : {
4560 1 : constfun = builtin_memset_gen_str;
4561 1 : constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4562 : val);
4563 : }
4564 : else
4565 : {
4566 : constfun = builtin_memset_read_str;
4567 : constfundata = &valc;
4568 : }
4569 :
4570 282 : rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4571 282 : rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4572 282 : to = replace_equiv_address (to, ptr);
4573 282 : set_mem_align (to, align);
4574 :
4575 282 : if (blksize)
4576 : {
4577 363 : to = store_by_pieces (to, blksize,
4578 : constfun, constfundata,
4579 : align, true,
4580 : max_len != 0 ? RETURN_END : RETURN_BEGIN);
4581 189 : if (max_len == 0)
4582 : return true;
4583 :
4584 : /* Adjust PTR, TO and REM. Since TO's address is likely
4585 : PTR+offset, we have to replace it. */
4586 15 : emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4587 15 : to = replace_equiv_address (to, ptr);
4588 15 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4589 15 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4590 : }
4591 :
4592 : /* Iterate over power-of-two block sizes from the maximum length to
4593 : the least significant bit possibly set in the length. */
4594 493 : for (int i = max_bits; i >= sctz_len; i--)
4595 : {
4596 385 : rtx_code_label *loop_label = NULL;
4597 385 : rtx_code_label *label = NULL;
4598 :
4599 385 : blksize = HOST_WIDE_INT_1U << i;
4600 :
4601 : /* If we're past the bits shared between min_ and max_len, expand
4602 : a test on the dynamic length, comparing it with the
4603 : BLKSIZE. */
4604 385 : if (i <= tst_bits)
4605 : {
4606 275 : label = gen_label_rtx ();
4607 275 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4608 : ptr_mode, 1, label,
4609 : profile_probability::even ());
4610 : }
4611 : /* If we are at a bit that is in the prefix shared by min_ and
4612 : max_len, skip the current BLKSIZE if the bit is clear, but do
4613 : not skip the loop, even if it doesn't require
4614 : prechecking. */
4615 110 : else if ((max_len & blksize) == 0
4616 69 : && !(max_loop && i == max_bits))
4617 39 : continue;
4618 :
4619 346 : if (max_loop && i == max_bits)
4620 : {
4621 107 : loop_label = gen_label_rtx ();
4622 107 : emit_label (loop_label);
4623 : /* Since we may run this multiple times, don't assume we
4624 : know anything about the offset. */
4625 107 : clear_mem_offset (to);
4626 : }
4627 :
4628 346 : bool update_needed = i != sctz_len || loop_label;
4629 346 : rtx next_ptr = NULL_RTX;
4630 346 : if (!use_store_by_pieces)
4631 : {
4632 0 : gcc_checking_assert (blksize == 1);
4633 0 : if (!val)
4634 0 : val = gen_int_mode (valc, QImode);
4635 0 : to = change_address (to, QImode, 0);
4636 0 : emit_move_insn (to, val);
4637 0 : if (update_needed)
4638 0 : next_ptr = plus_constant (GET_MODE (ptr), ptr, blksize);
4639 : }
4640 : else
4641 : {
4642 : /* Issue a store of BLKSIZE bytes. */
4643 404 : to = store_by_pieces (to, blksize,
4644 : constfun, constfundata,
4645 : align, true,
4646 : update_needed ? RETURN_END : RETURN_BEGIN);
4647 346 : next_ptr = XEXP (to, 0);
4648 : }
4649 : /* Adjust REM and PTR, unless this is the last iteration. */
4650 346 : if (update_needed)
4651 : {
4652 288 : emit_move_insn (ptr, force_operand (next_ptr, NULL_RTX));
4653 288 : to = replace_equiv_address (to, ptr);
4654 288 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4655 288 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4656 : }
4657 :
4658 288 : if (loop_label)
4659 107 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), GE, NULL,
4660 : ptr_mode, 1, loop_label,
4661 : profile_probability::likely ());
4662 :
4663 346 : if (label)
4664 : {
4665 275 : emit_label (label);
4666 :
4667 : /* Given conditional stores, the offset can no longer be
4668 : known, so clear it. */
4669 275 : clear_mem_offset (to);
4670 : }
4671 : }
4672 :
4673 : return true;
4674 : }
4675 :
4676 : /* Helper function to do the actual work for expand_builtin_memset. The
4677 : arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4678 : so that this can also be called without constructing an actual CALL_EXPR.
4679 : The other arguments and return value are the same as for
4680 : expand_builtin_memset. */
4681 :
4682 : static rtx
4683 64670 : expand_builtin_memset_args (tree dest, tree val, tree len,
4684 : rtx target, machine_mode mode, tree orig_exp)
4685 : {
4686 64670 : tree fndecl, fn;
4687 64670 : enum built_in_function fcode;
4688 64670 : machine_mode val_mode;
4689 64670 : char c;
4690 64670 : unsigned int dest_align;
4691 64670 : rtx dest_mem, dest_addr, len_rtx;
4692 64670 : HOST_WIDE_INT expected_size = -1;
4693 64670 : unsigned int expected_align = 0;
4694 64670 : unsigned HOST_WIDE_INT min_size;
4695 64670 : unsigned HOST_WIDE_INT max_size;
4696 64670 : unsigned HOST_WIDE_INT probable_max_size;
4697 :
4698 64670 : dest_align = get_pointer_alignment (dest);
4699 :
4700 : /* If DEST is not a pointer type, don't do this operation in-line. */
4701 64670 : if (dest_align == 0)
4702 : return NULL_RTX;
4703 :
4704 64670 : if (currently_expanding_gimple_stmt)
4705 64670 : stringop_block_profile (currently_expanding_gimple_stmt,
4706 : &expected_align, &expected_size);
4707 :
4708 64670 : if (expected_align < dest_align)
4709 64662 : expected_align = dest_align;
4710 :
4711 : /* If the LEN parameter is zero, return DEST. */
4712 64670 : if (integer_zerop (len))
4713 : {
4714 : /* Evaluate and ignore VAL in case it has side-effects. */
4715 0 : expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4716 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
4717 : }
4718 :
4719 : /* Stabilize the arguments in case we fail. */
4720 64670 : dest = builtin_save_expr (dest);
4721 64670 : val = builtin_save_expr (val);
4722 64670 : len = builtin_save_expr (len);
4723 :
4724 64670 : len_rtx = expand_normal (len);
4725 64670 : determine_block_size (len, len_rtx, &min_size, &max_size,
4726 : &probable_max_size);
4727 64670 : dest_mem = get_memory_rtx (dest, len, true);
4728 64670 : val_mode = TYPE_MODE (unsigned_char_type_node);
4729 :
4730 64670 : if (TREE_CODE (val) != INTEGER_CST
4731 64670 : || target_char_cast (val, &c))
4732 : {
4733 1734 : rtx val_rtx;
4734 :
4735 1734 : val_rtx = expand_normal (val);
4736 1734 : val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4737 :
4738 : /* Assume that we can memset by pieces if we can store
4739 : * the coefficients by pieces (in the required modes).
4740 : * We can't pass builtin_memset_gen_str as that emits RTL. */
4741 1734 : c = 1;
4742 1734 : if (tree_fits_uhwi_p (len)
4743 1734 : && can_store_by_pieces (tree_to_uhwi (len),
4744 : builtin_memset_read_str, &c, dest_align,
4745 : true))
4746 : {
4747 736 : val_rtx = force_reg (val_mode, val_rtx);
4748 736 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4749 : builtin_memset_gen_str, val_rtx, dest_align,
4750 : true, RETURN_BEGIN);
4751 : }
4752 998 : else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4753 : dest_align, expected_align,
4754 : expected_size, min_size, max_size,
4755 : probable_max_size)
4756 998 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4757 : tree_ctz (len),
4758 : min_size, max_size,
4759 : val_rtx, 0,
4760 : dest_align))
4761 636 : goto do_libcall;
4762 :
4763 1098 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4764 1098 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4765 1098 : return dest_mem;
4766 : }
4767 :
4768 62936 : if (c)
4769 : {
4770 14281 : if (tree_fits_uhwi_p (len)
4771 14281 : && can_store_by_pieces (tree_to_uhwi (len),
4772 : builtin_memset_read_str, &c, dest_align,
4773 : true))
4774 5905 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4775 : builtin_memset_read_str, &c, dest_align, true,
4776 : RETURN_BEGIN);
4777 10323 : else if (!set_storage_via_setmem (dest_mem, len_rtx,
4778 8376 : gen_int_mode (c, val_mode),
4779 : dest_align, expected_align,
4780 : expected_size, min_size, max_size,
4781 : probable_max_size)
4782 8376 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4783 : tree_ctz (len),
4784 : min_size, max_size,
4785 : NULL_RTX, c,
4786 : dest_align))
4787 6429 : goto do_libcall;
4788 :
4789 7852 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4790 7852 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4791 7852 : return dest_mem;
4792 : }
4793 :
4794 48655 : set_mem_align (dest_mem, dest_align);
4795 97310 : dest_addr = clear_storage_hints (dest_mem, len_rtx,
4796 48655 : CALL_EXPR_TAILCALL (orig_exp)
4797 : ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4798 : expected_align, expected_size,
4799 : min_size, max_size,
4800 : probable_max_size, tree_ctz (len));
4801 :
4802 48655 : if (dest_addr == 0)
4803 : {
4804 37771 : dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4805 37771 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
4806 : }
4807 :
4808 : return dest_addr;
4809 :
4810 7065 : do_libcall:
4811 7065 : fndecl = get_callee_fndecl (orig_exp);
4812 7065 : fcode = DECL_FUNCTION_CODE (fndecl);
4813 7065 : if (fcode == BUILT_IN_MEMSET)
4814 7065 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4815 : dest, val, len);
4816 0 : else if (fcode == BUILT_IN_BZERO)
4817 0 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4818 : dest, len);
4819 : else
4820 0 : gcc_unreachable ();
4821 7065 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4822 7065 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4823 7065 : return expand_call (fn, target, target == const0_rtx);
4824 : }
4825 :
4826 : /* Expand expression EXP, which is a call to the bzero builtin. Return
4827 : NULL_RTX if we failed the caller should emit a normal call. */
4828 :
4829 : static rtx
4830 0 : expand_builtin_bzero (tree exp)
4831 : {
4832 0 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4833 : return NULL_RTX;
4834 :
4835 0 : tree dest = CALL_EXPR_ARG (exp, 0);
4836 0 : tree size = CALL_EXPR_ARG (exp, 1);
4837 :
4838 : /* New argument list transforming bzero(ptr x, int y) to
4839 : memset(ptr x, int 0, size_t y). This is done this way
4840 : so that if it isn't expanded inline, we fallback to
4841 : calling bzero instead of memset. */
4842 :
4843 0 : location_t loc = EXPR_LOCATION (exp);
4844 :
4845 0 : return expand_builtin_memset_args (dest, integer_zero_node,
4846 : fold_convert_loc (loc,
4847 : size_type_node, size),
4848 0 : const0_rtx, VOIDmode, exp);
4849 : }
4850 :
4851 : /* Try to expand cmpstr operation ICODE with the given operands.
4852 : Return the result rtx on success, otherwise return null. */
4853 :
4854 : static rtx
4855 0 : expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4856 : HOST_WIDE_INT align)
4857 : {
4858 0 : machine_mode insn_mode = insn_data[icode].operand[0].mode;
4859 :
4860 0 : if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4861 : target = NULL_RTX;
4862 :
4863 0 : class expand_operand ops[4];
4864 0 : create_output_operand (&ops[0], target, insn_mode);
4865 0 : create_fixed_operand (&ops[1], arg1_rtx);
4866 0 : create_fixed_operand (&ops[2], arg2_rtx);
4867 0 : create_integer_operand (&ops[3], align);
4868 0 : if (maybe_expand_insn (icode, 4, ops))
4869 0 : return ops[0].value;
4870 : return NULL_RTX;
4871 : }
4872 :
4873 : /* Expand expression EXP, which is a call to the memcmp built-in function.
4874 : Return NULL_RTX if we failed and the caller should emit a normal call,
4875 : otherwise try to get the result in TARGET, if convenient.
4876 : RESULT_EQ is true if we can relax the returned value to be either zero
4877 : or nonzero, without caring about the sign. */
4878 :
4879 : static rtx
4880 106002 : expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4881 : {
4882 106002 : if (!validate_arglist (exp,
4883 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4884 : return NULL_RTX;
4885 :
4886 105998 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4887 105998 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4888 105998 : tree len = CALL_EXPR_ARG (exp, 2);
4889 :
4890 : /* Due to the performance benefit, always inline the calls first
4891 : when result_eq is false. */
4892 105998 : rtx result = NULL_RTX;
4893 105998 : enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4894 105998 : if (!result_eq && fcode != BUILT_IN_BCMP)
4895 : {
4896 15266 : result = inline_expand_builtin_bytecmp (exp, target);
4897 15266 : if (result)
4898 : return result;
4899 : }
4900 :
4901 105980 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4902 105980 : location_t loc = EXPR_LOCATION (exp);
4903 :
4904 105980 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4905 105980 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4906 :
4907 : /* If we don't have POINTER_TYPE, call the function. */
4908 105980 : if (arg1_align == 0 || arg2_align == 0)
4909 : return NULL_RTX;
4910 :
4911 105980 : rtx arg1_rtx = get_memory_rtx (arg1, len, false);
4912 105980 : rtx arg2_rtx = get_memory_rtx (arg2, len, false);
4913 105980 : rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4914 :
4915 : /* Set MEM_SIZE as appropriate. */
4916 105980 : if (CONST_INT_P (len_rtx))
4917 : {
4918 81015 : set_mem_size (arg1_rtx, INTVAL (len_rtx));
4919 81015 : set_mem_size (arg2_rtx, INTVAL (len_rtx));
4920 : }
4921 :
4922 105980 : by_pieces_constfn constfn = NULL;
4923 :
4924 : /* Try to get the byte representation of the constant ARG2 (or, only
4925 : when the function's result is used for equality to zero, ARG1)
4926 : points to, with its byte size in NBYTES. */
4927 105980 : unsigned HOST_WIDE_INT nbytes;
4928 105980 : const char *rep = getbyterep (arg2, &nbytes);
4929 105980 : if (result_eq && rep == NULL)
4930 : {
4931 : /* For equality to zero the arguments are interchangeable. */
4932 63634 : rep = getbyterep (arg1, &nbytes);
4933 63634 : if (rep != NULL)
4934 : std::swap (arg1_rtx, arg2_rtx);
4935 : }
4936 :
4937 : /* If the function's constant bound LEN_RTX is less than or equal
4938 : to the byte size of the representation of the constant argument,
4939 : and if block move would be done by pieces, we can avoid loading
4940 : the bytes from memory and only store the computed constant result. */
4941 42346 : if (rep
4942 29185 : && CONST_INT_P (len_rtx)
4943 29092 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4944 105980 : constfn = builtin_memcpy_read_str;
4945 :
4946 211960 : result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4947 105980 : TREE_TYPE (len), target,
4948 : result_eq, constfn,
4949 : const_cast<char *> (rep),
4950 : tree_ctz (len));
4951 :
4952 105980 : if (result)
4953 : {
4954 : /* Return the value in the proper mode for this function. */
4955 68091 : if (GET_MODE (result) == mode)
4956 : return result;
4957 :
4958 0 : if (target != 0)
4959 : {
4960 0 : convert_move (target, result, 0);
4961 0 : return target;
4962 : }
4963 :
4964 0 : return convert_to_mode (mode, result, 0);
4965 : }
4966 :
4967 : return NULL_RTX;
4968 : }
4969 :
4970 : /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4971 : if we failed the caller should emit a normal call, otherwise try to get
4972 : the result in TARGET, if convenient. */
4973 :
4974 : static rtx
4975 127946 : expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4976 : {
4977 127946 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4978 : return NULL_RTX;
4979 :
4980 127931 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4981 127931 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4982 :
4983 : /* Due to the performance benefit, always inline the calls first. */
4984 127931 : rtx result = NULL_RTX;
4985 127931 : result = inline_expand_builtin_bytecmp (exp, target);
4986 127931 : if (result)
4987 : return result;
4988 :
4989 127532 : insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4990 127532 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4991 127532 : if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4992 : return NULL_RTX;
4993 :
4994 127532 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4995 127532 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4996 :
4997 : /* If we don't have POINTER_TYPE, call the function. */
4998 127532 : if (arg1_align == 0 || arg2_align == 0)
4999 : return NULL_RTX;
5000 :
5001 : /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
5002 127532 : arg1 = builtin_save_expr (arg1);
5003 127532 : arg2 = builtin_save_expr (arg2);
5004 :
5005 127532 : rtx arg1_rtx = get_memory_rtx (arg1, NULL, false);
5006 127532 : rtx arg2_rtx = get_memory_rtx (arg2, NULL, false);
5007 :
5008 : /* Try to call cmpstrsi. */
5009 127532 : if (cmpstr_icode != CODE_FOR_nothing)
5010 0 : result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
5011 0 : MIN (arg1_align, arg2_align));
5012 :
5013 : /* Try to determine at least one length and call cmpstrnsi. */
5014 127532 : if (!result && cmpstrn_icode != CODE_FOR_nothing)
5015 : {
5016 127532 : tree len;
5017 127532 : rtx arg3_rtx;
5018 :
5019 127532 : tree len1 = c_strlen (arg1, 1);
5020 127532 : tree len2 = c_strlen (arg2, 1);
5021 :
5022 127532 : if (len1)
5023 224 : len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
5024 127532 : if (len2)
5025 125908 : len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
5026 :
5027 : /* If we don't have a constant length for the first, use the length
5028 : of the second, if we know it. We don't require a constant for
5029 : this case; some cost analysis could be done if both are available
5030 : but neither is constant. For now, assume they're equally cheap,
5031 : unless one has side effects. If both strings have constant lengths,
5032 : use the smaller. */
5033 :
5034 127532 : if (!len1)
5035 : len = len2;
5036 224 : else if (!len2)
5037 : len = len1;
5038 17 : else if (TREE_SIDE_EFFECTS (len1))
5039 : len = len2;
5040 17 : else if (TREE_SIDE_EFFECTS (len2))
5041 : len = len1;
5042 17 : else if (TREE_CODE (len1) != INTEGER_CST)
5043 : len = len2;
5044 17 : else if (TREE_CODE (len2) != INTEGER_CST)
5045 : len = len1;
5046 10 : else if (tree_int_cst_lt (len1, len2))
5047 : len = len1;
5048 : else
5049 127532 : len = len2;
5050 :
5051 : /* If both arguments have side effects, we cannot optimize. */
5052 127532 : if (len && !TREE_SIDE_EFFECTS (len))
5053 : {
5054 126115 : arg3_rtx = expand_normal (len);
5055 126115 : result = expand_cmpstrn_or_cmpmem
5056 126115 : (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
5057 126115 : arg3_rtx, MIN (arg1_align, arg2_align));
5058 : }
5059 : }
5060 :
5061 127532 : tree fndecl = get_callee_fndecl (exp);
5062 127532 : if (result)
5063 : {
5064 : /* Return the value in the proper mode for this function. */
5065 52 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
5066 52 : if (GET_MODE (result) == mode)
5067 : return result;
5068 0 : if (target == 0)
5069 0 : return convert_to_mode (mode, result, 0);
5070 0 : convert_move (target, result, 0);
5071 0 : return target;
5072 : }
5073 :
5074 : /* Expand the library call ourselves using a stabilized argument
5075 : list to avoid re-evaluating the function's arguments twice. */
5076 127480 : tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
5077 127480 : copy_warning (fn, exp);
5078 127480 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5079 127480 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5080 127480 : return expand_call (fn, target, target == const0_rtx);
5081 : }
5082 :
5083 : /* Expand expression EXP, which is a call to the strncmp builtin. Return
5084 : NULL_RTX if we failed the caller should emit a normal call, otherwise
5085 : try to get the result in TARGET, if convenient. */
5086 :
5087 : static rtx
5088 2027 : expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
5089 : ATTRIBUTE_UNUSED machine_mode mode)
5090 : {
5091 2027 : if (!validate_arglist (exp,
5092 : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5093 : return NULL_RTX;
5094 :
5095 2021 : tree arg1 = CALL_EXPR_ARG (exp, 0);
5096 2021 : tree arg2 = CALL_EXPR_ARG (exp, 1);
5097 2021 : tree arg3 = CALL_EXPR_ARG (exp, 2);
5098 :
5099 2021 : location_t loc = EXPR_LOCATION (exp);
5100 2021 : tree len1 = c_strlen (arg1, 1);
5101 2021 : tree len2 = c_strlen (arg2, 1);
5102 :
5103 : /* Due to the performance benefit, always inline the calls first. */
5104 2021 : rtx result = NULL_RTX;
5105 2021 : result = inline_expand_builtin_bytecmp (exp, target);
5106 2021 : if (result)
5107 : return result;
5108 :
5109 : /* If c_strlen can determine an expression for one of the string
5110 : lengths, and it doesn't have side effects, then emit cmpstrnsi
5111 : using length MIN(strlen(string)+1, arg3). */
5112 1803 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
5113 1803 : if (cmpstrn_icode == CODE_FOR_nothing)
5114 : return NULL_RTX;
5115 :
5116 1803 : tree len;
5117 :
5118 1803 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
5119 1803 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
5120 :
5121 1803 : if (len1)
5122 160 : len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
5123 1803 : if (len2)
5124 674 : len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
5125 :
5126 1803 : tree len3 = fold_convert_loc (loc, sizetype, arg3);
5127 :
5128 : /* If we don't have a constant length for the first, use the length
5129 : of the second, if we know it. If neither string is constant length,
5130 : use the given length argument. We don't require a constant for
5131 : this case; some cost analysis could be done if both are available
5132 : but neither is constant. For now, assume they're equally cheap,
5133 : unless one has side effects. If both strings have constant lengths,
5134 : use the smaller. */
5135 :
5136 1803 : if (!len1 && !len2)
5137 : len = len3;
5138 717 : else if (!len1)
5139 : len = len2;
5140 160 : else if (!len2)
5141 : len = len1;
5142 117 : else if (TREE_SIDE_EFFECTS (len1))
5143 : len = len2;
5144 117 : else if (TREE_SIDE_EFFECTS (len2))
5145 : len = len1;
5146 117 : else if (TREE_CODE (len1) != INTEGER_CST)
5147 : len = len2;
5148 117 : else if (TREE_CODE (len2) != INTEGER_CST)
5149 : len = len1;
5150 117 : else if (tree_int_cst_lt (len1, len2))
5151 : len = len1;
5152 : else
5153 1803 : len = len2;
5154 :
5155 : /* If we are not using the given length, we must incorporate it here.
5156 : The actual new length parameter will be MIN(len,arg3) in this case. */
5157 1803 : if (len != len3)
5158 : {
5159 717 : len = fold_convert_loc (loc, sizetype, len);
5160 717 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
5161 : }
5162 1803 : rtx arg1_rtx = get_memory_rtx (arg1, len, false);
5163 1803 : rtx arg2_rtx = get_memory_rtx (arg2, len, false);
5164 1803 : rtx arg3_rtx = expand_normal (len);
5165 1803 : result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
5166 1803 : arg2_rtx, TREE_TYPE (len), arg3_rtx,
5167 1803 : MIN (arg1_align, arg2_align));
5168 :
5169 1803 : tree fndecl = get_callee_fndecl (exp);
5170 1803 : if (result)
5171 : {
5172 : /* Return the value in the proper mode for this function. */
5173 17 : mode = TYPE_MODE (TREE_TYPE (exp));
5174 17 : if (GET_MODE (result) == mode)
5175 : return result;
5176 0 : if (target == 0)
5177 0 : return convert_to_mode (mode, result, 0);
5178 0 : convert_move (target, result, 0);
5179 0 : return target;
5180 : }
5181 :
5182 : /* Expand the library call ourselves using a stabilized argument
5183 : list to avoid re-evaluating the function's arguments twice. */
5184 1786 : tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
5185 1786 : copy_warning (call, exp);
5186 1786 : gcc_assert (TREE_CODE (call) == CALL_EXPR);
5187 1786 : CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
5188 1786 : return expand_call (call, target, target == const0_rtx);
5189 : }
5190 :
5191 : /* Expand a call to __builtin_saveregs, generating the result in TARGET,
5192 : if that's convenient. */
5193 :
5194 : rtx
5195 0 : expand_builtin_saveregs (void)
5196 : {
5197 0 : rtx val;
5198 0 : rtx_insn *seq;
5199 :
5200 : /* Don't do __builtin_saveregs more than once in a function.
5201 : Save the result of the first call and reuse it. */
5202 0 : if (saveregs_value != 0)
5203 : return saveregs_value;
5204 :
5205 : /* When this function is called, it means that registers must be
5206 : saved on entry to this function. So we migrate the call to the
5207 : first insn of this function. */
5208 :
5209 0 : start_sequence ();
5210 :
5211 : /* Do whatever the machine needs done in this case. */
5212 0 : val = targetm.calls.expand_builtin_saveregs ();
5213 :
5214 0 : seq = end_sequence ();
5215 :
5216 0 : saveregs_value = val;
5217 :
5218 : /* Put the insns after the NOTE that starts the function. If this
5219 : is inside a start_sequence, make the outer-level insn chain current, so
5220 : the code is placed at the start of the function. */
5221 0 : push_topmost_sequence ();
5222 0 : emit_insn_after (seq, entry_of_function ());
5223 0 : pop_topmost_sequence ();
5224 :
5225 0 : return val;
5226 : }
5227 :
5228 : /* Expand a call to __builtin_next_arg. */
5229 :
5230 : static rtx
5231 21276 : expand_builtin_next_arg (void)
5232 : {
5233 : /* Checking arguments is already done in fold_builtin_next_arg
5234 : that must be called before this function. */
5235 21276 : return expand_binop (ptr_mode, add_optab,
5236 : crtl->args.internal_arg_pointer,
5237 : crtl->args.arg_offset_rtx,
5238 21276 : NULL_RTX, 0, OPTAB_LIB_WIDEN);
5239 : }
5240 :
5241 : /* Make it easier for the backends by protecting the valist argument
5242 : from multiple evaluations. */
5243 :
5244 : static tree
5245 21614 : stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5246 : {
5247 21614 : tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5248 :
5249 : /* The current way of determining the type of valist is completely
5250 : bogus. We should have the information on the va builtin instead. */
5251 21614 : if (!vatype)
5252 21490 : vatype = targetm.fn_abi_va_list (cfun->decl);
5253 :
5254 21614 : if (TREE_CODE (vatype) == ARRAY_TYPE)
5255 : {
5256 15946 : if (TREE_SIDE_EFFECTS (valist))
5257 0 : valist = save_expr (valist);
5258 :
5259 : /* For this case, the backends will be expecting a pointer to
5260 : vatype, but it's possible we've actually been given an array
5261 : (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5262 : So fix it. */
5263 15946 : if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5264 : {
5265 0 : tree p1 = build_pointer_type (TREE_TYPE (vatype));
5266 0 : valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5267 : }
5268 : }
5269 : else
5270 : {
5271 5668 : tree pt = build_pointer_type (vatype);
5272 :
5273 5668 : if (! needs_lvalue)
5274 : {
5275 6 : if (! TREE_SIDE_EFFECTS (valist))
5276 : return valist;
5277 :
5278 0 : valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5279 0 : TREE_SIDE_EFFECTS (valist) = 1;
5280 : }
5281 :
5282 5662 : if (TREE_SIDE_EFFECTS (valist))
5283 0 : valist = save_expr (valist);
5284 5662 : valist = fold_build2_loc (loc, MEM_REF,
5285 : vatype, valist, build_int_cst (pt, 0));
5286 : }
5287 :
5288 : return valist;
5289 : }
5290 :
5291 : /* The "standard" definition of va_list is void*. */
5292 :
5293 : tree
5294 0 : std_build_builtin_va_list (void)
5295 : {
5296 0 : return ptr_type_node;
5297 : }
5298 :
5299 : /* The "standard" abi va_list is va_list_type_node. */
5300 :
5301 : tree
5302 0 : std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5303 : {
5304 0 : return va_list_type_node;
5305 : }
5306 :
5307 : /* The "standard" type of va_list is va_list_type_node. */
5308 :
5309 : tree
5310 502 : std_canonical_va_list_type (tree type)
5311 : {
5312 502 : tree wtype, htype;
5313 :
5314 502 : wtype = va_list_type_node;
5315 502 : htype = type;
5316 :
5317 502 : if (TREE_CODE (wtype) == ARRAY_TYPE)
5318 : {
5319 : /* If va_list is an array type, the argument may have decayed
5320 : to a pointer type, e.g. by being passed to another function.
5321 : In that case, unwrap both types so that we can compare the
5322 : underlying records. */
5323 0 : if (TREE_CODE (htype) == ARRAY_TYPE
5324 0 : || POINTER_TYPE_P (htype))
5325 : {
5326 0 : wtype = TREE_TYPE (wtype);
5327 0 : htype = TREE_TYPE (htype);
5328 : }
5329 : }
5330 502 : if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5331 332 : return va_list_type_node;
5332 :
5333 : return NULL_TREE;
5334 : }
5335 :
5336 : /* The "standard" implementation of va_start: just assign `nextarg' to
5337 : the variable. */
5338 :
5339 : void
5340 5658 : std_expand_builtin_va_start (tree valist, rtx nextarg)
5341 : {
5342 5658 : rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5343 5658 : convert_move (va_r, nextarg, 0);
5344 5658 : }
5345 :
5346 : /* Expand EXP, a call to __builtin_va_start. */
5347 :
5348 : static rtx
5349 21118 : expand_builtin_va_start (tree exp)
5350 : {
5351 21118 : rtx nextarg;
5352 21118 : tree valist;
5353 21118 : location_t loc = EXPR_LOCATION (exp);
5354 :
5355 21118 : if (call_expr_nargs (exp) < 2)
5356 : {
5357 0 : error_at (loc, "too few arguments to function %<va_start%>");
5358 0 : return const0_rtx;
5359 : }
5360 :
5361 21118 : if (fold_builtin_next_arg (exp, true))
5362 0 : return const0_rtx;
5363 :
5364 21118 : nextarg = expand_builtin_next_arg ();
5365 21118 : valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5366 :
5367 21118 : if (targetm.expand_builtin_va_start)
5368 21118 : targetm.expand_builtin_va_start (valist, nextarg);
5369 : else
5370 0 : std_expand_builtin_va_start (valist, nextarg);
5371 :
5372 21118 : return const0_rtx;
5373 : }
5374 :
5375 : /* Expand EXP, a call to __builtin_va_end. */
5376 :
5377 : static rtx
5378 12206 : expand_builtin_va_end (tree exp)
5379 : {
5380 12206 : tree valist = CALL_EXPR_ARG (exp, 0);
5381 :
5382 : /* Evaluate for side effects, if needed. I hate macros that don't
5383 : do that. */
5384 12206 : if (TREE_SIDE_EFFECTS (valist))
5385 0 : expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5386 :
5387 12206 : return const0_rtx;
5388 : }
5389 :
5390 : /* Expand EXP, a call to __builtin_va_copy. We do this as a
5391 : builtin rather than just as an assignment in stdarg.h because of the
5392 : nastiness of array-type va_list types. */
5393 :
5394 : static rtx
5395 248 : expand_builtin_va_copy (tree exp)
5396 : {
5397 248 : tree dst, src, t;
5398 248 : location_t loc = EXPR_LOCATION (exp);
5399 :
5400 248 : dst = CALL_EXPR_ARG (exp, 0);
5401 248 : src = CALL_EXPR_ARG (exp, 1);
5402 :
5403 248 : dst = stabilize_va_list_loc (loc, dst, 1);
5404 248 : src = stabilize_va_list_loc (loc, src, 0);
5405 :
5406 248 : gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5407 :
5408 248 : if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5409 : {
5410 0 : t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5411 0 : TREE_SIDE_EFFECTS (t) = 1;
5412 0 : expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5413 : }
5414 : else
5415 : {
5416 248 : rtx dstb, srcb, size;
5417 :
5418 : /* Evaluate to pointers. */
5419 248 : dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5420 248 : srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5421 248 : size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5422 : NULL_RTX, VOIDmode, EXPAND_NORMAL);
5423 :
5424 248 : dstb = convert_memory_address (Pmode, dstb);
5425 248 : srcb = convert_memory_address (Pmode, srcb);
5426 :
5427 : /* "Dereference" to BLKmode memories. */
5428 248 : dstb = gen_rtx_MEM (BLKmode, dstb);
5429 248 : set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5430 248 : set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5431 248 : srcb = gen_rtx_MEM (BLKmode, srcb);
5432 248 : set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5433 248 : set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5434 :
5435 : /* Copy. */
5436 248 : emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5437 : }
5438 :
5439 248 : return const0_rtx;
5440 : }
5441 :
5442 : /* Expand a call to one of the builtin functions __builtin_frame_address or
5443 : __builtin_return_address. */
5444 :
5445 : static rtx
5446 15936 : expand_builtin_frame_address (tree fndecl, tree exp)
5447 : {
5448 : /* The argument must be a nonnegative integer constant.
5449 : It counts the number of frames to scan up the stack.
5450 : The value is either the frame pointer value or the return
5451 : address saved in that frame. */
5452 15936 : if (call_expr_nargs (exp) == 0)
5453 : /* Warning about missing arg was already issued. */
5454 0 : return const0_rtx;
5455 15936 : else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5456 : {
5457 0 : error ("invalid argument to %qD", fndecl);
5458 0 : return const0_rtx;
5459 : }
5460 : else
5461 : {
5462 : /* Number of frames to scan up the stack. */
5463 15936 : unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5464 :
5465 15936 : rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5466 :
5467 : /* Some ports cannot access arbitrary stack frames. */
5468 15936 : if (tem == NULL)
5469 : {
5470 0 : warning (0, "unsupported argument to %qD", fndecl);
5471 0 : return const0_rtx;
5472 : }
5473 :
5474 15936 : if (count)
5475 : {
5476 : /* Warn since no effort is made to ensure that any frame
5477 : beyond the current one exists or can be safely reached. */
5478 938 : warning (OPT_Wframe_address, "calling %qD with "
5479 : "a nonzero argument is unsafe", fndecl);
5480 : }
5481 :
5482 : /* For __builtin_frame_address, return what we've got. */
5483 15936 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5484 : return tem;
5485 :
5486 5173 : if (!REG_P (tem)
5487 5173 : && ! CONSTANT_P (tem))
5488 5173 : tem = copy_addr_to_reg (tem);
5489 : return tem;
5490 : }
5491 : }
5492 :
5493 : #if ! STACK_GROWS_DOWNWARD
5494 : # define STACK_TOPS GT
5495 : #else
5496 : # define STACK_TOPS LT
5497 : #endif
5498 :
5499 : #ifdef POINTERS_EXTEND_UNSIGNED
5500 : # define STACK_UNSIGNED POINTERS_EXTEND_UNSIGNED
5501 : #else
5502 : # define STACK_UNSIGNED true
5503 : #endif
5504 :
5505 : /* Expand a call to builtin function __builtin_stack_address. */
5506 :
5507 : static rtx
5508 3475 : expand_builtin_stack_address ()
5509 : {
5510 3475 : rtx ret = convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
5511 : STACK_UNSIGNED);
5512 :
5513 : #ifdef STACK_ADDRESS_OFFSET
5514 : /* Unbias the stack pointer, bringing it to the boundary between the
5515 : stack area claimed by the active function calling this builtin,
5516 : and stack ranges that could get clobbered if it called another
5517 : function. It should NOT encompass any stack red zone, that is
5518 : used in leaf functions.
5519 :
5520 : On SPARC, the register save area is *not* considered active or
5521 : used by the active function, but rather as akin to the area in
5522 : which call-preserved registers are saved by callees. This
5523 : enables __strub_leave to clear what would otherwise overlap with
5524 : its own register save area.
5525 :
5526 : If the address is computed too high or too low, parts of a stack
5527 : range that should be scrubbed may be left unscrubbed, scrubbing
5528 : may corrupt active portions of the stack frame, and stack ranges
5529 : may be doubly-scrubbed by caller and callee.
5530 :
5531 : In order for it to be just right, the area delimited by
5532 : @code{__builtin_stack_address} and @code{__builtin_frame_address
5533 : (0)} should encompass caller's registers saved by the function,
5534 : local on-stack variables and @code{alloca} stack areas.
5535 : Accumulated outgoing on-stack arguments, preallocated as part of
5536 : a function's own prologue, are to be regarded as part of the
5537 : (caller) function's active area as well, whereas those pushed or
5538 : allocated temporarily for a call are regarded as part of the
5539 : callee's stack range, rather than the caller's. */
5540 : ret = plus_constant (ptr_mode, ret, STACK_ADDRESS_OFFSET);
5541 : #endif
5542 :
5543 3475 : return force_reg (ptr_mode, ret);
5544 : }
5545 :
5546 : /* Expand a call to builtin function __builtin_strub_enter. */
5547 :
5548 : static rtx
5549 2175 : expand_builtin_strub_enter (tree exp)
5550 : {
5551 2175 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5552 : return NULL_RTX;
5553 :
5554 2175 : if (optimize < 1 || flag_no_inline)
5555 : return NULL_RTX;
5556 :
5557 1531 : rtx stktop = expand_builtin_stack_address ();
5558 :
5559 1531 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5560 1531 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5561 1531 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5562 : build_int_cst (TREE_TYPE (wmptr), 0));
5563 1531 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5564 :
5565 1531 : emit_move_insn (wmark, stktop);
5566 :
5567 1531 : return const0_rtx;
5568 : }
5569 :
5570 : /* Expand a call to builtin function __builtin_strub_update. */
5571 :
5572 : static rtx
5573 1091 : expand_builtin_strub_update (tree exp)
5574 : {
5575 1091 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5576 : return NULL_RTX;
5577 :
5578 1091 : if (optimize < 2 || flag_no_inline)
5579 : return NULL_RTX;
5580 :
5581 668 : rtx stktop = expand_builtin_stack_address ();
5582 :
5583 : #ifdef RED_ZONE_SIZE
5584 : /* Here's how the strub enter, update and leave functions deal with red zones.
5585 :
5586 : If it weren't for red zones, update, called from within a strub context,
5587 : would bump the watermark to the top of the stack. Enter and leave, running
5588 : in the caller, would use the caller's top of stack address both to
5589 : initialize the watermark passed to the callee, and to start strubbing the
5590 : stack afterwards.
5591 :
5592 : Ideally, we'd update the watermark so as to cover the used amount of red
5593 : zone, and strub starting at the caller's other end of the (presumably
5594 : unused) red zone. Normally, only leaf functions use the red zone, but at
5595 : this point we can't tell whether a function is a leaf, nor can we tell how
5596 : much of the red zone it uses. Furthermore, some strub contexts may have
5597 : been inlined so that update and leave are called from the same stack frame,
5598 : and the strub builtins may all have been inlined, turning a strub function
5599 : into a leaf.
5600 :
5601 : So cleaning the range from the caller's stack pointer (one end of the red
5602 : zone) to the (potentially inlined) callee's (other end of the) red zone
5603 : could scribble over the caller's own red zone.
5604 :
5605 : We avoid this possibility by arranging for callers that are strub contexts
5606 : to use their own watermark as the strub starting point. So, if A calls B,
5607 : and B calls C, B will tell A to strub up to the end of B's red zone, and
5608 : will strub itself only the part of C's stack frame and red zone that
5609 : doesn't overlap with B's. With that, we don't need to know who's leaf and
5610 : who isn't: inlined calls will shrink their strub window to zero, each
5611 : remaining call will strub some portion of the stack, and eventually the
5612 : strub context will return to a caller that isn't a strub context itself,
5613 : that will therefore use its own stack pointer as the strub starting point.
5614 : It's not a leaf, because strub contexts can't be inlined into non-strub
5615 : contexts, so it doesn't use the red zone, and it will therefore correctly
5616 : strub up the callee's stack frame up to the end of the callee's red zone.
5617 : Neat! */
5618 668 : if (true /* (flags_from_decl_or_type (current_function_decl) & ECF_LEAF) */)
5619 : {
5620 668 : poly_int64 red_zone_size = RED_ZONE_SIZE;
5621 : #if STACK_GROWS_DOWNWARD
5622 668 : red_zone_size = -red_zone_size;
5623 : #endif
5624 668 : stktop = plus_constant (ptr_mode, stktop, red_zone_size);
5625 668 : stktop = force_reg (ptr_mode, stktop);
5626 : }
5627 : #endif
5628 :
5629 668 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5630 668 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5631 668 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5632 : build_int_cst (TREE_TYPE (wmptr), 0));
5633 668 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5634 :
5635 668 : rtx wmark_load = shallow_copy_rtx (wmark);
5636 668 : MEM_NOTRAP_P (wmark_load) = !tree_could_trap_p (wmtree);
5637 668 : rtx wmarkr = force_reg (ptr_mode, wmark_load);
5638 :
5639 668 : rtx_code_label *lab = gen_label_rtx ();
5640 668 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5641 : ptr_mode, NULL_RTX, lab, NULL,
5642 : profile_probability::very_likely ());
5643 668 : emit_move_insn (wmark, stktop);
5644 :
5645 : /* If this is an inlined strub function, also bump the watermark for the
5646 : enclosing function. This avoids a problem with the following scenario: A
5647 : calls B and B calls C, and both B and C get inlined into A. B allocates
5648 : temporary stack space before calling C. If we don't update A's watermark,
5649 : we may use an outdated baseline for the post-C strub_leave, erasing B's
5650 : temporary stack allocation. We only need this if we're fully expanding
5651 : strub_leave inline. */
5652 668 : tree xwmptr = (optimize > 2
5653 668 : ? strub_watermark_parm (current_function_decl)
5654 : : wmptr);
5655 668 : if (wmptr != xwmptr)
5656 : {
5657 156 : wmptr = xwmptr;
5658 156 : wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5659 156 : wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5660 : build_int_cst (TREE_TYPE (wmptr), 0));
5661 156 : wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5662 156 : wmark_load = shallow_copy_rtx (wmark);
5663 156 : MEM_NOTRAP_P (wmark_load) = !tree_could_trap_p (wmtree);
5664 156 : wmarkr = force_reg (ptr_mode, wmark_load);
5665 :
5666 156 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5667 : ptr_mode, NULL_RTX, lab, NULL,
5668 : profile_probability::very_likely ());
5669 156 : emit_move_insn (wmark, stktop);
5670 : }
5671 :
5672 668 : emit_label (lab);
5673 :
5674 668 : return const0_rtx;
5675 : }
5676 :
5677 :
5678 : /* Expand a call to builtin function __builtin_strub_leave. */
5679 :
5680 : static rtx
5681 2745 : expand_builtin_strub_leave (tree exp)
5682 : {
5683 2745 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5684 : return NULL_RTX;
5685 :
5686 2745 : if (optimize < 2 || optimize_size || flag_no_inline)
5687 : return NULL_RTX;
5688 :
5689 1244 : rtx stktop = NULL_RTX;
5690 :
5691 1244 : if (tree wmptr = (optimize
5692 1244 : ? strub_watermark_parm (current_function_decl)
5693 : : NULL_TREE))
5694 : {
5695 513 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5696 513 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5697 : build_int_cst (TREE_TYPE (wmptr), 0));
5698 513 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5699 513 : MEM_NOTRAP_P (wmark) = !tree_could_trap_p (wmtree);
5700 513 : stktop = force_reg (ptr_mode, wmark);
5701 : }
5702 :
5703 513 : if (!stktop)
5704 731 : stktop = expand_builtin_stack_address ();
5705 :
5706 1244 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5707 1244 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5708 1244 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5709 : build_int_cst (TREE_TYPE (wmptr), 0));
5710 1244 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5711 1244 : MEM_NOTRAP_P (wmark) = !tree_could_trap_p (wmtree);
5712 :
5713 1244 : rtx wmarkr = force_reg (ptr_mode, wmark);
5714 :
5715 : #if ! STACK_GROWS_DOWNWARD
5716 : rtx base = stktop;
5717 : rtx end = wmarkr;
5718 : #else
5719 1244 : rtx base = wmarkr;
5720 1244 : rtx end = stktop;
5721 : #endif
5722 :
5723 : /* We're going to modify it, so make sure it's not e.g. the stack pointer. */
5724 1244 : base = copy_to_reg (base);
5725 :
5726 1244 : rtx_code_label *done = gen_label_rtx ();
5727 1244 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5728 : ptr_mode, NULL_RTX, done, NULL,
5729 : profile_probability::very_likely ());
5730 :
5731 1244 : if (optimize < 3)
5732 924 : expand_call (exp, NULL_RTX, true);
5733 : else
5734 : {
5735 : /* Ok, now we've determined we want to copy the block, so convert the
5736 : addresses to Pmode, as needed to dereference them to access ptr_mode
5737 : memory locations, so that we don't have to convert anything within the
5738 : loop. */
5739 320 : base = memory_address (ptr_mode, base);
5740 320 : end = memory_address (ptr_mode, end);
5741 :
5742 320 : rtx zero = force_operand (const0_rtx, NULL_RTX);
5743 320 : int ulen = GET_MODE_SIZE (ptr_mode);
5744 :
5745 : /* ??? It would be nice to use setmem or similar patterns here,
5746 : but they do not necessarily obey the stack growth direction,
5747 : which has security implications. We also have to avoid calls
5748 : (memset, bzero or any machine-specific ones), which are
5749 : likely unsafe here (see TARGET_STRUB_MAY_USE_MEMSET). */
5750 : #if ! STACK_GROWS_DOWNWARD
5751 : rtx incr = plus_constant (Pmode, base, ulen);
5752 : rtx dstm = gen_rtx_MEM (ptr_mode, base);
5753 :
5754 : rtx_code_label *loop = gen_label_rtx ();
5755 : emit_label (loop);
5756 : emit_move_insn (dstm, zero);
5757 : emit_move_insn (base, force_operand (incr, NULL_RTX));
5758 : #else
5759 320 : rtx decr = plus_constant (Pmode, end, -ulen);
5760 320 : rtx dstm = gen_rtx_MEM (ptr_mode, end);
5761 :
5762 320 : rtx_code_label *loop = gen_label_rtx ();
5763 320 : emit_label (loop);
5764 320 : emit_move_insn (end, force_operand (decr, NULL_RTX));
5765 320 : emit_move_insn (dstm, zero);
5766 : #endif
5767 640 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5768 320 : Pmode, NULL_RTX, NULL, loop,
5769 : profile_probability::very_likely ());
5770 : }
5771 :
5772 1244 : emit_label (done);
5773 :
5774 1244 : return const0_rtx;
5775 : }
5776 :
5777 : /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5778 : failed and the caller should emit a normal call. */
5779 :
5780 : static rtx
5781 28032 : expand_builtin_alloca (tree exp)
5782 : {
5783 28032 : rtx op0;
5784 28032 : rtx result;
5785 28032 : unsigned int align;
5786 28032 : tree fndecl = get_callee_fndecl (exp);
5787 28032 : HOST_WIDE_INT max_size;
5788 28032 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5789 28032 : bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5790 28032 : bool valid_arglist
5791 : = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5792 28032 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5793 : VOID_TYPE)
5794 : : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5795 28030 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5796 28032 : : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5797 :
5798 28032 : if (!valid_arglist)
5799 : return NULL_RTX;
5800 :
5801 : /* Compute the argument. */
5802 28027 : op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5803 :
5804 : /* Compute the alignment. */
5805 52043 : align = (fcode == BUILT_IN_ALLOCA
5806 24017 : ? BIGGEST_ALIGNMENT
5807 4010 : : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5808 :
5809 : /* Compute the maximum size. */
5810 2 : max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5811 28029 : ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5812 : : -1);
5813 :
5814 : /* Allocate the desired space. If the allocation stems from the declaration
5815 : of a variable-sized object, it cannot accumulate. */
5816 28027 : result
5817 28027 : = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5818 28027 : result = convert_memory_address (ptr_mode, result);
5819 :
5820 : /* Dynamic allocations for variables are recorded during gimplification. */
5821 28027 : if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5822 0 : record_dynamic_alloc (exp);
5823 :
5824 : return result;
5825 : }
5826 :
5827 : /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5828 : of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5829 : STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5830 : handle_builtin_stack_restore function. */
5831 :
5832 : static rtx
5833 204 : expand_asan_emit_allocas_unpoison (tree exp)
5834 : {
5835 204 : tree arg0 = CALL_EXPR_ARG (exp, 0);
5836 204 : tree arg1 = CALL_EXPR_ARG (exp, 1);
5837 204 : rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5838 204 : rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5839 204 : rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5840 : stack_pointer_rtx, NULL_RTX, 0,
5841 : OPTAB_LIB_WIDEN);
5842 204 : off = convert_modes (ptr_mode, Pmode, off, 0);
5843 204 : bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5844 : OPTAB_LIB_WIDEN);
5845 204 : rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5846 204 : ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5847 : top, ptr_mode, bot, ptr_mode);
5848 204 : return ret;
5849 : }
5850 :
5851 : /* Expand a call to bswap builtin in EXP.
5852 : Return NULL_RTX if a normal call should be emitted rather than expanding the
5853 : function in-line. If convenient, the result should be placed in TARGET.
5854 : SUBTARGET may be used as the target for computing one of EXP's operands. */
5855 :
5856 : static rtx
5857 159 : expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5858 : rtx subtarget)
5859 : {
5860 159 : tree arg;
5861 159 : rtx op0;
5862 :
5863 159 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5864 : return NULL_RTX;
5865 :
5866 159 : arg = CALL_EXPR_ARG (exp, 0);
5867 159 : op0 = expand_expr (arg,
5868 2 : subtarget && GET_MODE (subtarget) == target_mode
5869 : ? subtarget : NULL_RTX,
5870 : target_mode, EXPAND_NORMAL);
5871 159 : if (GET_MODE (op0) != target_mode)
5872 0 : op0 = convert_to_mode (target_mode, op0, 1);
5873 :
5874 159 : target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5875 :
5876 159 : gcc_assert (target);
5877 :
5878 159 : return convert_to_mode (target_mode, target, 1);
5879 : }
5880 :
5881 : /* Expand a call to a unary builtin in EXP.
5882 : Return NULL_RTX if a normal call should be emitted rather than expanding the
5883 : function in-line. If convenient, the result should be placed in TARGET.
5884 : SUBTARGET may be used as the target for computing one of EXP's operands. */
5885 :
5886 : static rtx
5887 961 : expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5888 : rtx subtarget, optab op_optab)
5889 : {
5890 961 : rtx op0;
5891 :
5892 961 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5893 : return NULL_RTX;
5894 :
5895 : /* Compute the argument. */
5896 1922 : op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5897 : (subtarget
5898 135 : && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5899 135 : == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5900 : VOIDmode, EXPAND_NORMAL);
5901 : /* Compute op, into TARGET if possible.
5902 : Set TARGET to wherever the result comes back. */
5903 961 : target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5904 : op_optab, op0, target, op_optab != clrsb_optab);
5905 961 : gcc_assert (target);
5906 :
5907 961 : return convert_to_mode (target_mode, target, 0);
5908 : }
5909 :
5910 : /* Expand a call to __builtin_expect. We just return our argument
5911 : as the builtin_expect semantic should've been already executed by
5912 : tree branch prediction pass. */
5913 :
5914 : static rtx
5915 1407 : expand_builtin_expect (tree exp, rtx target)
5916 : {
5917 1407 : tree arg;
5918 :
5919 1407 : if (call_expr_nargs (exp) < 2)
5920 0 : return const0_rtx;
5921 1407 : arg = CALL_EXPR_ARG (exp, 0);
5922 :
5923 1407 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5924 : /* When guessing was done, the hints should be already stripped away. */
5925 1407 : gcc_assert (!flag_guess_branch_prob
5926 : || optimize == 0 || seen_error ());
5927 : return target;
5928 : }
5929 :
5930 : /* Expand a call to __builtin_expect_with_probability. We just return our
5931 : argument as the builtin_expect semantic should've been already executed by
5932 : tree branch prediction pass. */
5933 :
5934 : static rtx
5935 5 : expand_builtin_expect_with_probability (tree exp, rtx target)
5936 : {
5937 5 : tree arg;
5938 :
5939 5 : if (call_expr_nargs (exp) < 3)
5940 0 : return const0_rtx;
5941 5 : arg = CALL_EXPR_ARG (exp, 0);
5942 :
5943 5 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5944 : /* When guessing was done, the hints should be already stripped away. */
5945 5 : gcc_assert (!flag_guess_branch_prob
5946 : || optimize == 0 || seen_error ());
5947 : return target;
5948 : }
5949 :
5950 :
5951 : /* Expand a call to __builtin_assume_aligned. We just return our first
5952 : argument as the builtin_assume_aligned semantic should've been already
5953 : executed by CCP. */
5954 :
5955 : static rtx
5956 662 : expand_builtin_assume_aligned (tree exp, rtx target)
5957 : {
5958 662 : if (call_expr_nargs (exp) < 2)
5959 0 : return const0_rtx;
5960 662 : target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5961 : EXPAND_NORMAL);
5962 662 : gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5963 : && (call_expr_nargs (exp) < 3
5964 : || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5965 : return target;
5966 : }
5967 :
5968 : void
5969 38958 : expand_builtin_trap (void)
5970 : {
5971 38958 : if (targetm.have_trap ())
5972 : {
5973 38958 : rtx_insn *insn = emit_insn (targetm.gen_trap ());
5974 : /* For trap insns when not accumulating outgoing args force
5975 : REG_ARGS_SIZE note to prevent crossjumping of calls with
5976 : different args sizes. */
5977 38958 : if (!ACCUMULATE_OUTGOING_ARGS)
5978 38956 : add_args_size_note (insn, stack_pointer_delta);
5979 : }
5980 : else
5981 : {
5982 0 : tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5983 0 : tree call_expr = build_call_expr (fn, 0);
5984 0 : expand_call (call_expr, NULL_RTX, false);
5985 : }
5986 :
5987 38958 : emit_barrier ();
5988 38958 : }
5989 :
5990 : /* Expand a call to __builtin_unreachable. We do nothing except emit
5991 : a barrier saying that control flow will not pass here.
5992 :
5993 : It is the responsibility of the program being compiled to ensure
5994 : that control flow does never reach __builtin_unreachable. */
5995 : static void
5996 2671 : expand_builtin_unreachable (void)
5997 : {
5998 : /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5999 : to avoid this. */
6000 2671 : gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
6001 2671 : emit_barrier ();
6002 2671 : }
6003 :
6004 : /* Expand EXP, a call to fabs, fabsf or fabsl.
6005 : Return NULL_RTX if a normal call should be emitted rather than expanding
6006 : the function inline. If convenient, the result should be placed
6007 : in TARGET. SUBTARGET may be used as the target for computing
6008 : the operand. */
6009 :
6010 : static rtx
6011 4 : expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
6012 : {
6013 4 : machine_mode mode;
6014 4 : tree arg;
6015 4 : rtx op0;
6016 :
6017 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6018 : return NULL_RTX;
6019 :
6020 0 : arg = CALL_EXPR_ARG (exp, 0);
6021 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
6022 0 : mode = TYPE_MODE (TREE_TYPE (arg));
6023 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6024 0 : return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
6025 : }
6026 :
6027 : /* Expand EXP, a call to copysign, copysignf, or copysignl.
6028 : Return NULL is a normal call should be emitted rather than expanding the
6029 : function inline. If convenient, the result should be placed in TARGET.
6030 : SUBTARGET may be used as the target for computing the operand. */
6031 :
6032 : static rtx
6033 11677 : expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
6034 : {
6035 11677 : rtx op0, op1;
6036 11677 : tree arg;
6037 :
6038 11677 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6039 : return NULL_RTX;
6040 :
6041 11676 : arg = CALL_EXPR_ARG (exp, 0);
6042 11676 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6043 :
6044 11676 : arg = CALL_EXPR_ARG (exp, 1);
6045 11676 : op1 = expand_normal (arg);
6046 :
6047 11676 : return expand_copysign (op0, op1, target);
6048 : }
6049 :
6050 : /* Emit a call to __builtin___clear_cache. */
6051 :
6052 : void
6053 0 : default_emit_call_builtin___clear_cache (rtx begin, rtx end)
6054 : {
6055 0 : rtx callee = gen_rtx_SYMBOL_REF (Pmode,
6056 : BUILTIN_ASM_NAME_PTR
6057 : (BUILT_IN_CLEAR_CACHE));
6058 :
6059 0 : emit_library_call (callee,
6060 : LCT_NORMAL, VOIDmode,
6061 : convert_memory_address (ptr_mode, begin), ptr_mode,
6062 : convert_memory_address (ptr_mode, end), ptr_mode);
6063 0 : }
6064 :
6065 : /* Emit a call to __builtin___clear_cache, unless the target specifies
6066 : it as do-nothing. This function can be used by trampoline
6067 : finalizers to duplicate the effects of expanding a call to the
6068 : clear_cache builtin. */
6069 :
6070 : void
6071 28 : maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
6072 : {
6073 28 : gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
6074 : || CONST_INT_P (begin))
6075 : && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
6076 : || CONST_INT_P (end)));
6077 :
6078 28 : if (targetm.have_clear_cache ())
6079 : {
6080 : /* We have a "clear_cache" insn, and it will handle everything. */
6081 0 : class expand_operand ops[2];
6082 :
6083 0 : create_address_operand (&ops[0], begin);
6084 0 : create_address_operand (&ops[1], end);
6085 :
6086 0 : if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
6087 0 : return;
6088 : }
6089 : else
6090 : {
6091 : #ifndef CLEAR_INSN_CACHE
6092 : /* There is no "clear_cache" insn, and __clear_cache() in libgcc
6093 : does nothing. There is no need to call it. Do nothing. */
6094 : return;
6095 : #endif /* CLEAR_INSN_CACHE */
6096 : }
6097 :
6098 0 : targetm.calls.emit_call_builtin___clear_cache (begin, end);
6099 : }
6100 :
6101 : /* Expand a call to __builtin___clear_cache. */
6102 :
6103 : static void
6104 28 : expand_builtin___clear_cache (tree exp)
6105 : {
6106 28 : tree begin, end;
6107 28 : rtx begin_rtx, end_rtx;
6108 :
6109 : /* We must not expand to a library call. If we did, any
6110 : fallback library function in libgcc that might contain a call to
6111 : __builtin___clear_cache() would recurse infinitely. */
6112 28 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6113 : {
6114 0 : error ("both arguments to %<__builtin___clear_cache%> must be pointers");
6115 0 : return;
6116 : }
6117 :
6118 28 : begin = CALL_EXPR_ARG (exp, 0);
6119 30 : begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
6120 :
6121 28 : end = CALL_EXPR_ARG (exp, 1);
6122 30 : end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
6123 :
6124 28 : maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
6125 : }
6126 :
6127 : /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
6128 :
6129 : static rtx
6130 666 : round_trampoline_addr (rtx tramp)
6131 : {
6132 666 : rtx temp, addend, mask;
6133 :
6134 : /* If we don't need too much alignment, we'll have been guaranteed
6135 : proper alignment by get_trampoline_type. */
6136 666 : if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
6137 : return tramp;
6138 :
6139 : /* Round address up to desired boundary. */
6140 0 : temp = gen_reg_rtx (Pmode);
6141 0 : addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
6142 0 : mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
6143 :
6144 0 : temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
6145 : temp, 0, OPTAB_LIB_WIDEN);
6146 0 : tramp = expand_simple_binop (Pmode, AND, temp, mask,
6147 : temp, 0, OPTAB_LIB_WIDEN);
6148 :
6149 0 : return tramp;
6150 : }
6151 :
6152 : static rtx
6153 309 : expand_builtin_init_trampoline (tree exp, bool onstack)
6154 : {
6155 309 : tree t_tramp, t_func, t_chain;
6156 309 : rtx m_tramp, r_tramp, r_chain, tmp;
6157 :
6158 309 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
6159 : POINTER_TYPE, VOID_TYPE))
6160 : return NULL_RTX;
6161 :
6162 309 : t_tramp = CALL_EXPR_ARG (exp, 0);
6163 309 : t_func = CALL_EXPR_ARG (exp, 1);
6164 309 : t_chain = CALL_EXPR_ARG (exp, 2);
6165 :
6166 309 : r_tramp = expand_normal (t_tramp);
6167 309 : m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
6168 309 : MEM_NOTRAP_P (m_tramp) = 1;
6169 :
6170 : /* If ONSTACK, the TRAMP argument should be the address of a field
6171 : within the local function's FRAME decl. Either way, let's see if
6172 : we can fill in the MEM_ATTRs for this memory. */
6173 309 : if (TREE_CODE (t_tramp) == ADDR_EXPR)
6174 309 : set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
6175 :
6176 : /* Creator of a heap trampoline is responsible for making sure the
6177 : address is aligned to at least STACK_BOUNDARY. Normally malloc
6178 : will ensure this anyhow. */
6179 309 : tmp = round_trampoline_addr (r_tramp);
6180 309 : if (tmp != r_tramp)
6181 : {
6182 0 : m_tramp = change_address (m_tramp, BLKmode, tmp);
6183 0 : set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
6184 0 : set_mem_size (m_tramp, TRAMPOLINE_SIZE);
6185 : }
6186 :
6187 : /* The FUNC argument should be the address of the nested function.
6188 : Extract the actual function decl to pass to the hook. */
6189 309 : gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
6190 309 : t_func = TREE_OPERAND (t_func, 0);
6191 309 : gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
6192 :
6193 309 : r_chain = expand_normal (t_chain);
6194 :
6195 : /* Generate insns to initialize the trampoline. */
6196 309 : targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
6197 :
6198 309 : if (onstack)
6199 : {
6200 309 : trampolines_created = 1;
6201 :
6202 309 : if (targetm.calls.custom_function_descriptors != 0)
6203 309 : warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
6204 : "trampoline generated for nested function %qD", t_func);
6205 : }
6206 :
6207 309 : return const0_rtx;
6208 : }
6209 :
6210 : static rtx
6211 357 : expand_builtin_adjust_trampoline (tree exp)
6212 : {
6213 357 : rtx tramp;
6214 :
6215 357 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6216 : return NULL_RTX;
6217 :
6218 357 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6219 357 : tramp = round_trampoline_addr (tramp);
6220 357 : if (targetm.calls.trampoline_adjust_address)
6221 0 : tramp = targetm.calls.trampoline_adjust_address (tramp);
6222 :
6223 : return tramp;
6224 : }
6225 :
6226 : /* Expand a call to the builtin descriptor initialization routine.
6227 : A descriptor is made up of a couple of pointers to the static
6228 : chain and the code entry in this order. */
6229 :
6230 : static rtx
6231 0 : expand_builtin_init_descriptor (tree exp)
6232 : {
6233 0 : tree t_descr, t_func, t_chain;
6234 0 : rtx m_descr, r_descr, r_func, r_chain;
6235 :
6236 0 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
6237 : VOID_TYPE))
6238 : return NULL_RTX;
6239 :
6240 0 : t_descr = CALL_EXPR_ARG (exp, 0);
6241 0 : t_func = CALL_EXPR_ARG (exp, 1);
6242 0 : t_chain = CALL_EXPR_ARG (exp, 2);
6243 :
6244 0 : r_descr = expand_normal (t_descr);
6245 0 : m_descr = gen_rtx_MEM (BLKmode, r_descr);
6246 0 : MEM_NOTRAP_P (m_descr) = 1;
6247 0 : set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
6248 :
6249 0 : r_func = expand_normal (t_func);
6250 0 : r_chain = expand_normal (t_chain);
6251 :
6252 : /* Generate insns to initialize the descriptor. */
6253 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
6254 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
6255 : POINTER_SIZE / BITS_PER_UNIT), r_func);
6256 :
6257 0 : return const0_rtx;
6258 : }
6259 :
6260 : /* Expand a call to the builtin descriptor adjustment routine. */
6261 :
6262 : static rtx
6263 0 : expand_builtin_adjust_descriptor (tree exp)
6264 : {
6265 0 : rtx tramp;
6266 :
6267 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6268 : return NULL_RTX;
6269 :
6270 0 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6271 :
6272 : /* Unalign the descriptor to allow runtime identification. */
6273 0 : tramp = plus_constant (ptr_mode, tramp,
6274 0 : targetm.calls.custom_function_descriptors);
6275 :
6276 0 : return force_operand (tramp, NULL_RTX);
6277 : }
6278 :
6279 : /* Expand the call EXP to the built-in signbit, signbitf or signbitl
6280 : function. The function first checks whether the back end provides
6281 : an insn to implement signbit for the respective mode. If not, it
6282 : checks whether the floating point format of the value is such that
6283 : the sign bit can be extracted. If that is not the case, error out.
6284 : EXP is the expression that is a call to the builtin function; if
6285 : convenient, the result should be placed in TARGET. */
6286 : static rtx
6287 1375 : expand_builtin_signbit (tree exp, rtx target)
6288 : {
6289 1375 : const struct real_format *fmt;
6290 1375 : scalar_float_mode fmode;
6291 1375 : scalar_int_mode rmode, imode;
6292 1375 : tree arg;
6293 1375 : int word, bitpos;
6294 1375 : enum insn_code icode;
6295 1375 : rtx temp;
6296 1375 : location_t loc = EXPR_LOCATION (exp);
6297 :
6298 1375 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6299 : return NULL_RTX;
6300 :
6301 1375 : arg = CALL_EXPR_ARG (exp, 0);
6302 1375 : fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
6303 1375 : rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
6304 1375 : fmt = REAL_MODE_FORMAT (fmode);
6305 :
6306 1375 : arg = builtin_save_expr (arg);
6307 :
6308 : /* Expand the argument yielding a RTX expression. */
6309 1375 : temp = expand_normal (arg);
6310 :
6311 : /* Check if the back end provides an insn that handles signbit for the
6312 : argument's mode. */
6313 1375 : icode = optab_handler (signbit_optab, fmode);
6314 1375 : if (icode != CODE_FOR_nothing)
6315 : {
6316 10 : rtx_insn *last = get_last_insn ();
6317 10 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6318 10 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
6319 : return this_target;
6320 0 : delete_insns_since (last);
6321 : }
6322 :
6323 : /* For floating point formats without a sign bit, implement signbit
6324 : as "ARG < 0.0". */
6325 1365 : bitpos = fmt->signbit_ro;
6326 1365 : if (bitpos < 0)
6327 : {
6328 : /* But we can't do this if the format supports signed zero. */
6329 0 : gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
6330 :
6331 0 : arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
6332 0 : build_real (TREE_TYPE (arg), dconst0));
6333 0 : return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
6334 : }
6335 :
6336 2734 : if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
6337 : {
6338 1261 : imode = int_mode_for_mode (fmode).require ();
6339 1261 : temp = gen_lowpart (imode, temp);
6340 : }
6341 : else
6342 : {
6343 104 : imode = word_mode;
6344 : /* Handle targets with different FP word orders. */
6345 104 : if (FLOAT_WORDS_BIG_ENDIAN)
6346 : word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
6347 : else
6348 104 : word = bitpos / BITS_PER_WORD;
6349 104 : temp = operand_subword_force (temp, word, fmode);
6350 104 : bitpos = bitpos % BITS_PER_WORD;
6351 : }
6352 :
6353 : /* Force the intermediate word_mode (or narrower) result into a
6354 : register. This avoids attempting to create paradoxical SUBREGs
6355 : of floating point modes below. */
6356 1365 : temp = force_reg (imode, temp);
6357 :
6358 : /* If the bitpos is within the "result mode" lowpart, the operation
6359 : can be implement with a single bitwise AND. Otherwise, we need
6360 : a right shift and an AND. */
6361 :
6362 2730 : if (bitpos < GET_MODE_BITSIZE (rmode))
6363 : {
6364 1157 : wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
6365 :
6366 3471 : if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
6367 0 : temp = gen_lowpart (rmode, temp);
6368 2314 : temp = expand_binop (rmode, and_optab, temp,
6369 2314 : immed_wide_int_const (mask, rmode),
6370 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6371 1157 : }
6372 : else
6373 : {
6374 : /* Perform a logical right shift to place the signbit in the least
6375 : significant bit, then truncate the result to the desired mode
6376 : and mask just this bit. */
6377 208 : temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
6378 208 : temp = gen_lowpart (rmode, temp);
6379 208 : temp = expand_binop (rmode, and_optab, temp, const1_rtx,
6380 : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6381 : }
6382 :
6383 : return temp;
6384 : }
6385 :
6386 : /* Expand fork or exec calls. TARGET is the desired target of the
6387 : call. EXP is the call. FN is the
6388 : identificator of the actual function. IGNORE is nonzero if the
6389 : value is to be ignored. */
6390 :
6391 : static rtx
6392 112 : expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
6393 : {
6394 112 : tree id, decl;
6395 112 : tree call;
6396 :
6397 : /* If we are not profiling, just call the function. */
6398 112 : if (!coverage_instrumentation_p ())
6399 : return NULL_RTX;
6400 :
6401 : /* Otherwise call the wrapper. This should be equivalent for the rest of
6402 : compiler, so the code does not diverge, and the wrapper may run the
6403 : code necessary for keeping the profiling sane. */
6404 :
6405 4 : switch (DECL_FUNCTION_CODE (fn))
6406 : {
6407 4 : case BUILT_IN_FORK:
6408 4 : id = get_identifier ("__gcov_fork");
6409 4 : break;
6410 :
6411 0 : case BUILT_IN_EXECL:
6412 0 : id = get_identifier ("__gcov_execl");
6413 0 : break;
6414 :
6415 0 : case BUILT_IN_EXECV:
6416 0 : id = get_identifier ("__gcov_execv");
6417 0 : break;
6418 :
6419 0 : case BUILT_IN_EXECLP:
6420 0 : id = get_identifier ("__gcov_execlp");
6421 0 : break;
6422 :
6423 0 : case BUILT_IN_EXECLE:
6424 0 : id = get_identifier ("__gcov_execle");
6425 0 : break;
6426 :
6427 0 : case BUILT_IN_EXECVP:
6428 0 : id = get_identifier ("__gcov_execvp");
6429 0 : break;
6430 :
6431 0 : case BUILT_IN_EXECVE:
6432 0 : id = get_identifier ("__gcov_execve");
6433 0 : break;
6434 :
6435 0 : default:
6436 0 : gcc_unreachable ();
6437 : }
6438 :
6439 4 : decl = build_decl (DECL_SOURCE_LOCATION (fn),
6440 4 : FUNCTION_DECL, id, TREE_TYPE (fn));
6441 4 : DECL_EXTERNAL (decl) = 1;
6442 4 : TREE_PUBLIC (decl) = 1;
6443 4 : DECL_ARTIFICIAL (decl) = 1;
6444 4 : TREE_NOTHROW (decl) = 1;
6445 4 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6446 4 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
6447 4 : call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
6448 4 : return expand_call (call, target, ignore);
6449 : }
6450 :
6451 :
6452 :
6453 : /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6454 : the pointer in these functions is void*, the tree optimizers may remove
6455 : casts. The mode computed in expand_builtin isn't reliable either, due
6456 : to __sync_bool_compare_and_swap.
6457 :
6458 : FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6459 : group of builtins. This gives us log2 of the mode size. */
6460 :
6461 : static inline machine_mode
6462 136062 : get_builtin_sync_mode (int fcode_diff)
6463 : {
6464 : /* The size is not negotiable, so ask not to get BLKmode in return
6465 : if the target indicates that a smaller size would be better. */
6466 136062 : return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
6467 : }
6468 :
6469 : /* Expand the memory expression LOC and return the appropriate memory operand
6470 : for the builtin_sync operations. */
6471 :
6472 : static rtx
6473 153035 : get_builtin_sync_mem (tree loc, machine_mode mode)
6474 : {
6475 153035 : rtx addr, mem;
6476 153035 : int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
6477 : ? TREE_TYPE (TREE_TYPE (loc))
6478 : : TREE_TYPE (loc));
6479 153035 : scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
6480 :
6481 153035 : addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
6482 153035 : addr = convert_memory_address (addr_mode, addr);
6483 :
6484 : /* Note that we explicitly do not want any alias information for this
6485 : memory, so that we kill all other live memories. Otherwise we don't
6486 : satisfy the full barrier semantics of the intrinsic. */
6487 153035 : mem = gen_rtx_MEM (mode, addr);
6488 :
6489 153035 : set_mem_addr_space (mem, addr_space);
6490 :
6491 153035 : mem = validize_mem (mem);
6492 :
6493 : /* The alignment needs to be at least according to that of the mode. */
6494 153035 : set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6495 : get_pointer_alignment (loc)));
6496 153035 : set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6497 153035 : MEM_VOLATILE_P (mem) = 1;
6498 :
6499 153035 : return mem;
6500 : }
6501 :
6502 : /* Make sure an argument is in the right mode.
6503 : EXP is the tree argument.
6504 : MODE is the mode it should be in. */
6505 :
6506 : static rtx
6507 95701 : expand_expr_force_mode (tree exp, machine_mode mode)
6508 : {
6509 95701 : rtx val;
6510 95701 : machine_mode old_mode;
6511 :
6512 95701 : if (TREE_CODE (exp) == SSA_NAME
6513 95701 : && TYPE_MODE (TREE_TYPE (exp)) != mode)
6514 : {
6515 : /* Undo argument promotion if possible, as combine might not
6516 : be able to do it later due to MEM_VOLATILE_P uses in the
6517 : patterns. */
6518 25 : gimple *g = get_gimple_for_ssa_name (exp);
6519 25 : if (g && gimple_assign_cast_p (g))
6520 : {
6521 0 : tree rhs = gimple_assign_rhs1 (g);
6522 0 : tree_code code = gimple_assign_rhs_code (g);
6523 0 : if (CONVERT_EXPR_CODE_P (code)
6524 0 : && TYPE_MODE (TREE_TYPE (rhs)) == mode
6525 0 : && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6526 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6527 0 : && (TYPE_PRECISION (TREE_TYPE (exp))
6528 0 : > TYPE_PRECISION (TREE_TYPE (rhs))))
6529 : exp = rhs;
6530 : }
6531 : }
6532 :
6533 95701 : val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6534 : /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6535 : of CONST_INTs, where we know the old_mode only from the call argument. */
6536 :
6537 95701 : old_mode = GET_MODE (val);
6538 95701 : if (old_mode == VOIDmode)
6539 44700 : old_mode = TYPE_MODE (TREE_TYPE (exp));
6540 95701 : val = convert_modes (mode, old_mode, val, 1);
6541 95701 : return val;
6542 : }
6543 :
6544 :
6545 : /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6546 : EXP is the CALL_EXPR. CODE is the rtx code
6547 : that corresponds to the arithmetic or logical operation from the name;
6548 : an exception here is that NOT actually means NAND. TARGET is an optional
6549 : place for us to store the results; AFTER is true if this is the
6550 : fetch_and_xxx form. */
6551 :
6552 : static rtx
6553 4028 : expand_builtin_sync_operation (machine_mode mode, tree exp,
6554 : enum rtx_code code, bool after,
6555 : rtx target)
6556 : {
6557 4028 : rtx val, mem;
6558 4028 : location_t loc = EXPR_LOCATION (exp);
6559 :
6560 4028 : if (code == NOT && warn_sync_nand)
6561 : {
6562 523 : tree fndecl = get_callee_fndecl (exp);
6563 523 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6564 :
6565 523 : static bool warned_f_a_n, warned_n_a_f;
6566 :
6567 523 : switch (fcode)
6568 : {
6569 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6570 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6571 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6572 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6573 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6574 434 : if (warned_f_a_n)
6575 : break;
6576 :
6577 30 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6578 30 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6579 30 : warned_f_a_n = true;
6580 30 : break;
6581 :
6582 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6583 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6584 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6585 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6586 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6587 89 : if (warned_n_a_f)
6588 : break;
6589 :
6590 22 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6591 22 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6592 22 : warned_n_a_f = true;
6593 22 : break;
6594 :
6595 0 : default:
6596 0 : gcc_unreachable ();
6597 : }
6598 : }
6599 :
6600 : /* Expand the operands. */
6601 4028 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6602 4028 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6603 :
6604 4028 : return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6605 4028 : after);
6606 : }
6607 :
6608 : /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6609 : intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6610 : true if this is the boolean form. TARGET is a place for us to store the
6611 : results; this is NOT optional if IS_BOOL is true. */
6612 :
6613 : static rtx
6614 461 : expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6615 : bool is_bool, rtx target)
6616 : {
6617 461 : rtx old_val, new_val, mem;
6618 461 : rtx *pbool, *poval;
6619 :
6620 : /* Expand the operands. */
6621 461 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6622 461 : old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6623 461 : new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6624 :
6625 461 : pbool = poval = NULL;
6626 461 : if (target != const0_rtx)
6627 : {
6628 435 : if (is_bool)
6629 : pbool = ⌖
6630 : else
6631 230 : poval = ⌖
6632 : }
6633 461 : if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6634 : false, MEMMODEL_SYNC_SEQ_CST,
6635 : MEMMODEL_SYNC_SEQ_CST))
6636 : return NULL_RTX;
6637 :
6638 458 : return target;
6639 : }
6640 :
6641 : /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6642 : general form is actually an atomic exchange, and some targets only
6643 : support a reduced form with the second argument being a constant 1.
6644 : EXP is the CALL_EXPR; TARGET is an optional place for us to store
6645 : the results. */
6646 :
6647 : static rtx
6648 326 : expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6649 : rtx target)
6650 : {
6651 326 : rtx val, mem;
6652 :
6653 : /* Expand the operands. */
6654 326 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6655 326 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6656 :
6657 326 : return expand_sync_lock_test_and_set (target, mem, val);
6658 : }
6659 :
6660 : /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6661 :
6662 : static rtx
6663 146 : expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6664 : {
6665 146 : rtx mem;
6666 :
6667 : /* Expand the operands. */
6668 146 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6669 :
6670 146 : return expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6671 : }
6672 :
6673 : /* Given an integer representing an ``enum memmodel'', verify its
6674 : correctness and return the memory model enum. */
6675 :
6676 : static enum memmodel
6677 172181 : get_memmodel (tree exp)
6678 : {
6679 : /* If the parameter is not a constant, it's a run time value so we'll just
6680 : convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6681 172181 : if (TREE_CODE (exp) != INTEGER_CST)
6682 : return MEMMODEL_SEQ_CST;
6683 :
6684 171335 : rtx op = expand_normal (exp);
6685 :
6686 171335 : unsigned HOST_WIDE_INT val = INTVAL (op);
6687 171335 : if (targetm.memmodel_check)
6688 171335 : val = targetm.memmodel_check (val);
6689 0 : else if (val & ~MEMMODEL_MASK)
6690 : return MEMMODEL_SEQ_CST;
6691 :
6692 : /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6693 171335 : if (memmodel_base (val) >= MEMMODEL_LAST)
6694 : return MEMMODEL_SEQ_CST;
6695 :
6696 : /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6697 : be conservative and promote consume to acquire. */
6698 171332 : if (val == MEMMODEL_CONSUME)
6699 755 : val = MEMMODEL_ACQUIRE;
6700 :
6701 171332 : return (enum memmodel) val;
6702 : }
6703 :
6704 : /* Expand the __atomic_exchange intrinsic:
6705 : TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6706 : EXP is the CALL_EXPR.
6707 : TARGET is an optional place for us to store the results. */
6708 :
6709 : static rtx
6710 2910 : expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6711 : {
6712 2910 : rtx val, mem;
6713 2910 : enum memmodel model;
6714 :
6715 2910 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6716 :
6717 2910 : if (!flag_inline_atomics)
6718 : return NULL_RTX;
6719 :
6720 : /* Expand the operands. */
6721 2859 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6722 2859 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6723 :
6724 2859 : return expand_atomic_exchange (target, mem, val, model);
6725 : }
6726 :
6727 : /* Expand the __atomic_compare_exchange intrinsic:
6728 : bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6729 : TYPE desired, BOOL weak,
6730 : enum memmodel success,
6731 : enum memmodel failure)
6732 : EXP is the CALL_EXPR.
6733 : TARGET is an optional place for us to store the results. */
6734 :
6735 : static rtx
6736 9414 : expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6737 : rtx target)
6738 : {
6739 9414 : rtx expect, desired, mem, oldval;
6740 9414 : rtx_code_label *label;
6741 9414 : tree weak;
6742 9414 : bool is_weak;
6743 :
6744 9414 : memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6745 9414 : memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6746 :
6747 9414 : if (failure > success)
6748 22 : success = MEMMODEL_SEQ_CST;
6749 :
6750 9414 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6751 : {
6752 : failure = MEMMODEL_SEQ_CST;
6753 : success = MEMMODEL_SEQ_CST;
6754 : }
6755 :
6756 :
6757 9414 : if (!flag_inline_atomics)
6758 : return NULL_RTX;
6759 :
6760 : /* Expand the operands. */
6761 9363 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6762 :
6763 9363 : expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6764 9399 : expect = convert_memory_address (Pmode, expect);
6765 9363 : expect = gen_rtx_MEM (mode, expect);
6766 9363 : desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6767 :
6768 9363 : weak = CALL_EXPR_ARG (exp, 3);
6769 9363 : is_weak = false;
6770 9363 : if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6771 9363 : is_weak = true;
6772 :
6773 9363 : if (target == const0_rtx)
6774 288 : target = NULL;
6775 :
6776 : /* Lest the rtl backend create a race condition with an imporoper store
6777 : to memory, always create a new pseudo for OLDVAL. */
6778 9363 : oldval = NULL;
6779 :
6780 9363 : if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6781 : is_weak, success, failure))
6782 : return NULL_RTX;
6783 :
6784 : /* Conditionally store back to EXPECT, lest we create a race condition
6785 : with an improper store to memory. */
6786 : /* ??? With a rearrangement of atomics at the gimple level, we can handle
6787 : the normal case where EXPECT is totally private, i.e. a register. At
6788 : which point the store can be unconditional. */
6789 7614 : label = gen_label_rtx ();
6790 7614 : emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6791 7614 : GET_MODE (target), 1, label);
6792 7614 : emit_move_insn (expect, oldval);
6793 7614 : emit_label (label);
6794 :
6795 7614 : return target;
6796 : }
6797 :
6798 : /* Helper function for expand_ifn_atomic_compare_exchange - expand
6799 : internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6800 : call. The weak parameter must be dropped to match the expected parameter
6801 : list and the expected argument changed from value to pointer to memory
6802 : slot. */
6803 :
6804 : static void
6805 0 : expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6806 : {
6807 0 : unsigned int z;
6808 0 : vec<tree, va_gc> *vec;
6809 :
6810 0 : vec_alloc (vec, 5);
6811 0 : vec->quick_push (gimple_call_arg (call, 0));
6812 0 : tree expected = gimple_call_arg (call, 1);
6813 0 : rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6814 0 : TREE_TYPE (expected));
6815 0 : rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6816 0 : if (expd != x)
6817 0 : emit_move_insn (x, expd);
6818 0 : tree v = make_tree (TREE_TYPE (expected), x);
6819 0 : vec->quick_push (build1 (ADDR_EXPR,
6820 0 : build_pointer_type (TREE_TYPE (expected)), v));
6821 0 : vec->quick_push (gimple_call_arg (call, 2));
6822 : /* Skip the boolean weak parameter. */
6823 0 : for (z = 4; z < 6; z++)
6824 0 : vec->quick_push (gimple_call_arg (call, z));
6825 : /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6826 0 : unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6827 0 : gcc_assert (bytes_log2 < 5);
6828 0 : built_in_function fncode
6829 : = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6830 : + bytes_log2);
6831 0 : tree fndecl = builtin_decl_explicit (fncode);
6832 0 : tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6833 : fndecl);
6834 0 : tree exp = build_call_vec (boolean_type_node, fn, vec);
6835 0 : tree lhs = gimple_call_lhs (call);
6836 0 : rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6837 0 : if (lhs)
6838 : {
6839 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6840 0 : if (GET_MODE (boolret) != mode)
6841 0 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6842 0 : x = force_reg (mode, x);
6843 0 : write_complex_part (target, boolret, true, true);
6844 0 : write_complex_part (target, x, false, false);
6845 : }
6846 0 : }
6847 :
6848 : /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6849 :
6850 : void
6851 14279 : expand_ifn_atomic_compare_exchange (gcall *call)
6852 : {
6853 14279 : int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6854 14279 : gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6855 14279 : machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6856 :
6857 14279 : memmodel success = get_memmodel (gimple_call_arg (call, 4));
6858 14279 : memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6859 :
6860 14279 : if (failure > success)
6861 0 : success = MEMMODEL_SEQ_CST;
6862 :
6863 14279 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6864 : {
6865 : failure = MEMMODEL_SEQ_CST;
6866 : success = MEMMODEL_SEQ_CST;
6867 : }
6868 :
6869 14279 : if (!flag_inline_atomics)
6870 : {
6871 0 : expand_ifn_atomic_compare_exchange_into_call (call, mode);
6872 0 : return;
6873 : }
6874 :
6875 : /* Expand the operands. */
6876 14279 : rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6877 :
6878 14279 : rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6879 14279 : rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6880 :
6881 14279 : bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6882 :
6883 14279 : rtx boolret = NULL;
6884 14279 : rtx oldval = NULL;
6885 :
6886 14279 : if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6887 : is_weak, success, failure))
6888 : {
6889 0 : expand_ifn_atomic_compare_exchange_into_call (call, mode);
6890 0 : return;
6891 : }
6892 :
6893 14279 : tree lhs = gimple_call_lhs (call);
6894 14279 : if (lhs)
6895 : {
6896 14070 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6897 14070 : if (GET_MODE (boolret) != mode)
6898 12535 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6899 14070 : write_complex_part (target, boolret, true, true);
6900 14070 : write_complex_part (target, oldval, false, false);
6901 : }
6902 : }
6903 :
6904 : /* Expand the __atomic_load intrinsic:
6905 : TYPE __atomic_load (TYPE *object, enum memmodel)
6906 : EXP is the CALL_EXPR.
6907 : TARGET is an optional place for us to store the results. */
6908 :
6909 : static rtx
6910 71650 : expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6911 : {
6912 71650 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6913 71650 : if (is_mm_release (model) || is_mm_acq_rel (model))
6914 : model = MEMMODEL_SEQ_CST;
6915 :
6916 71650 : if (!flag_inline_atomics)
6917 : return NULL_RTX;
6918 :
6919 : /* Expand the operand. */
6920 71609 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6921 :
6922 71609 : return expand_atomic_load (target, mem, model);
6923 : }
6924 :
6925 :
6926 : /* Expand the __atomic_store intrinsic:
6927 : void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6928 : EXP is the CALL_EXPR.
6929 : TARGET is an optional place for us to store the results. */
6930 :
6931 : static rtx
6932 17558 : expand_builtin_atomic_store (machine_mode mode, tree exp)
6933 : {
6934 17558 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6935 17558 : if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6936 1622 : || is_mm_release (model)))
6937 : model = MEMMODEL_SEQ_CST;
6938 :
6939 17558 : if (!flag_inline_atomics)
6940 : return NULL_RTX;
6941 :
6942 : /* Expand the operands. */
6943 17527 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6944 17527 : rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6945 :
6946 17527 : return expand_atomic_store (mem, val, model, false);
6947 : }
6948 :
6949 : /* Expand the __atomic_fetch_XXX intrinsic:
6950 : TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6951 : EXP is the CALL_EXPR.
6952 : TARGET is an optional place for us to store the results.
6953 : CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6954 : FETCH_AFTER is true if returning the result of the operation.
6955 : FETCH_AFTER is false if returning the value before the operation.
6956 : IGNORE is true if the result is not used.
6957 : EXT_CALL is the correct builtin for an external call if this cannot be
6958 : resolved to an instruction sequence. */
6959 :
6960 : static rtx
6961 29538 : expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6962 : enum rtx_code code, bool fetch_after,
6963 : bool ignore, enum built_in_function ext_call)
6964 : {
6965 29538 : rtx val, mem, ret;
6966 29538 : enum memmodel model;
6967 29538 : tree fndecl, addr, oldval;
6968 :
6969 29538 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6970 :
6971 : /* Expand the operands. */
6972 29538 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6973 29538 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6974 :
6975 : /* Only try generating instructions if inlining is turned on. */
6976 29538 : if (flag_inline_atomics)
6977 : {
6978 28995 : ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6979 28995 : if (ret)
6980 : return ret;
6981 : }
6982 :
6983 : /* Return if a different routine isn't needed for the library call. */
6984 1159 : if (ext_call == BUILT_IN_NONE)
6985 : return NULL_RTX;
6986 :
6987 : /* Change the call to the specified function. */
6988 288 : fndecl = get_callee_fndecl (exp);
6989 288 : addr = CALL_EXPR_FN (exp);
6990 288 : STRIP_NOPS (addr);
6991 :
6992 288 : gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6993 288 : TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6994 288 : oldval = CALL_EXPR_ARG (exp, 1);
6995 :
6996 288 : if (!ignore && reg_overlap_mentioned_p (mem, val))
6997 : {
6998 1 : val = force_reg (mode, val);
6999 1 : CALL_EXPR_ARG (exp, 1) = make_tree (TREE_TYPE (oldval), val);
7000 : }
7001 :
7002 : /* If we will emit code after the call, the call cannot be a tail call.
7003 : If it is emitted as a tail call, a barrier is emitted after it, and
7004 : then all trailing code is removed. */
7005 288 : if (!ignore)
7006 191 : CALL_EXPR_TAILCALL (exp) = 0;
7007 :
7008 : /* Expand the call here so we can emit trailing code. */
7009 288 : ret = expand_call (exp, target, ignore);
7010 :
7011 : /* Replace the original function just in case it matters. */
7012 288 : TREE_OPERAND (addr, 0) = fndecl;
7013 :
7014 : /* Then issue the arithmetic correction to return the right result. */
7015 288 : if (!ignore)
7016 : {
7017 191 : CALL_EXPR_ARG (exp, 1) = oldval;
7018 191 : if (code == NOT)
7019 : {
7020 33 : ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
7021 : OPTAB_LIB_WIDEN);
7022 33 : ret = expand_simple_unop (mode, NOT, ret, target, true);
7023 : }
7024 : else
7025 158 : ret = expand_simple_binop (mode, code, ret, val, target, true,
7026 : OPTAB_LIB_WIDEN);
7027 : }
7028 : return ret;
7029 : }
7030 :
7031 : /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
7032 :
7033 : void
7034 433 : expand_ifn_atomic_bit_test_and (gcall *call)
7035 : {
7036 433 : tree ptr = gimple_call_arg (call, 0);
7037 433 : tree bit = gimple_call_arg (call, 1);
7038 433 : tree flag = gimple_call_arg (call, 2);
7039 433 : tree lhs = gimple_call_lhs (call);
7040 433 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7041 433 : machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
7042 433 : enum rtx_code code;
7043 433 : optab optab;
7044 433 : class expand_operand ops[5];
7045 :
7046 433 : gcc_assert (flag_inline_atomics);
7047 :
7048 433 : if (gimple_call_num_args (call) == 5)
7049 302 : model = get_memmodel (gimple_call_arg (call, 3));
7050 :
7051 433 : rtx mem = get_builtin_sync_mem (ptr, mode);
7052 433 : rtx val = expand_expr_force_mode (bit, mode);
7053 :
7054 433 : switch (gimple_call_internal_fn (call))
7055 : {
7056 : case IFN_ATOMIC_BIT_TEST_AND_SET:
7057 : code = IOR;
7058 : optab = atomic_bit_test_and_set_optab;
7059 : break;
7060 : case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
7061 : code = XOR;
7062 : optab = atomic_bit_test_and_complement_optab;
7063 : break;
7064 : case IFN_ATOMIC_BIT_TEST_AND_RESET:
7065 : code = AND;
7066 : optab = atomic_bit_test_and_reset_optab;
7067 : break;
7068 0 : default:
7069 0 : gcc_unreachable ();
7070 : }
7071 :
7072 433 : if (lhs == NULL_TREE)
7073 : {
7074 0 : rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
7075 : val, NULL_RTX, true, OPTAB_DIRECT);
7076 0 : if (code == AND)
7077 0 : val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
7078 0 : if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
7079 433 : return;
7080 : }
7081 :
7082 0 : rtx target;
7083 0 : if (lhs)
7084 433 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7085 : else
7086 0 : target = gen_reg_rtx (mode);
7087 433 : enum insn_code icode = direct_optab_handler (optab, mode);
7088 433 : gcc_assert (icode != CODE_FOR_nothing);
7089 433 : create_output_operand (&ops[0], target, mode);
7090 433 : create_fixed_operand (&ops[1], mem);
7091 433 : create_convert_operand_to (&ops[2], val, mode, true);
7092 433 : create_integer_operand (&ops[3], model);
7093 433 : create_integer_operand (&ops[4], integer_onep (flag));
7094 433 : if (maybe_expand_insn (icode, 5, ops))
7095 : return;
7096 :
7097 0 : if (reg_overlap_mentioned_p (mem, val))
7098 0 : val = force_reg (mode, val);
7099 :
7100 0 : rtx bitval = val;
7101 0 : val = expand_simple_binop (mode, ASHIFT, const1_rtx,
7102 : val, NULL_RTX, true, OPTAB_DIRECT);
7103 0 : rtx maskval = val;
7104 0 : if (code == AND)
7105 0 : val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
7106 0 : rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
7107 : code, model, false);
7108 0 : if (!result)
7109 : {
7110 0 : bool is_atomic = gimple_call_num_args (call) == 5;
7111 0 : tree tcall = gimple_call_arg (call, 3 + is_atomic);
7112 0 : tree fndecl = gimple_call_addr_fndecl (tcall);
7113 0 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
7114 0 : tree exp;
7115 0 : if (is_atomic)
7116 0 : exp = build_call_nary (type, tcall, 3,
7117 : ptr, make_tree (type, val),
7118 : gimple_call_arg (call, 3));
7119 : else
7120 0 : exp = build_call_nary (type, tcall, 2, ptr, make_tree (type, val));
7121 0 : result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7122 : mode, !lhs);
7123 : }
7124 0 : if (!lhs)
7125 : return;
7126 0 : if (integer_onep (flag))
7127 : {
7128 0 : result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
7129 : NULL_RTX, true, OPTAB_DIRECT);
7130 0 : result = expand_simple_binop (mode, AND, result, const1_rtx, target,
7131 : true, OPTAB_DIRECT);
7132 : }
7133 : else
7134 0 : result = expand_simple_binop (mode, AND, result, maskval, target, true,
7135 : OPTAB_DIRECT);
7136 0 : if (result != target)
7137 0 : emit_move_insn (target, result);
7138 : }
7139 :
7140 : /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
7141 :
7142 : void
7143 2147 : expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
7144 : {
7145 2147 : tree cmp = gimple_call_arg (call, 0);
7146 2147 : tree ptr = gimple_call_arg (call, 1);
7147 2147 : tree arg = gimple_call_arg (call, 2);
7148 2147 : tree lhs = gimple_call_lhs (call);
7149 2147 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7150 2147 : machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
7151 2147 : optab optab;
7152 2147 : rtx_code code;
7153 2147 : class expand_operand ops[5];
7154 :
7155 2147 : gcc_assert (flag_inline_atomics);
7156 :
7157 2147 : if (gimple_call_num_args (call) == 5)
7158 1739 : model = get_memmodel (gimple_call_arg (call, 3));
7159 :
7160 2147 : rtx mem = get_builtin_sync_mem (ptr, mode);
7161 2147 : rtx op = expand_expr_force_mode (arg, mode);
7162 :
7163 2147 : switch (gimple_call_internal_fn (call))
7164 : {
7165 : case IFN_ATOMIC_ADD_FETCH_CMP_0:
7166 : code = PLUS;
7167 : optab = atomic_add_fetch_cmp_0_optab;
7168 : break;
7169 : case IFN_ATOMIC_SUB_FETCH_CMP_0:
7170 : code = MINUS;
7171 : optab = atomic_sub_fetch_cmp_0_optab;
7172 : break;
7173 : case IFN_ATOMIC_AND_FETCH_CMP_0:
7174 : code = AND;
7175 : optab = atomic_and_fetch_cmp_0_optab;
7176 : break;
7177 : case IFN_ATOMIC_OR_FETCH_CMP_0:
7178 : code = IOR;
7179 : optab = atomic_or_fetch_cmp_0_optab;
7180 : break;
7181 : case IFN_ATOMIC_XOR_FETCH_CMP_0:
7182 : code = XOR;
7183 : optab = atomic_xor_fetch_cmp_0_optab;
7184 : break;
7185 0 : default:
7186 0 : gcc_unreachable ();
7187 : }
7188 :
7189 2147 : enum rtx_code comp = UNKNOWN;
7190 2147 : switch (tree_to_uhwi (cmp))
7191 : {
7192 : case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
7193 : case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
7194 : case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
7195 : case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
7196 : case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
7197 : case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
7198 0 : default: gcc_unreachable ();
7199 : }
7200 :
7201 2147 : rtx target;
7202 2147 : if (lhs == NULL_TREE)
7203 0 : target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
7204 : else
7205 2147 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7206 2147 : enum insn_code icode = direct_optab_handler (optab, mode);
7207 2147 : gcc_assert (icode != CODE_FOR_nothing);
7208 2147 : create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
7209 2147 : create_fixed_operand (&ops[1], mem);
7210 2147 : create_convert_operand_to (&ops[2], op, mode, true);
7211 2147 : create_integer_operand (&ops[3], model);
7212 2147 : create_integer_operand (&ops[4], comp);
7213 2147 : if (maybe_expand_insn (icode, 5, ops))
7214 2115 : return;
7215 :
7216 32 : rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
7217 : code, model, true);
7218 32 : if (!result)
7219 : {
7220 0 : bool is_atomic = gimple_call_num_args (call) == 5;
7221 0 : tree tcall = gimple_call_arg (call, 3 + is_atomic);
7222 0 : tree fndecl = gimple_call_addr_fndecl (tcall);
7223 0 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
7224 0 : tree exp;
7225 0 : if (is_atomic)
7226 0 : exp = build_call_nary (type, tcall, 3,
7227 : ptr, arg,
7228 : gimple_call_arg (call, 3));
7229 : else
7230 0 : exp = build_call_nary (type, tcall, 2, ptr, arg);
7231 0 : result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7232 : mode, !lhs);
7233 : }
7234 :
7235 32 : if (lhs)
7236 : {
7237 32 : result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
7238 : 0, 1);
7239 32 : if (result != target)
7240 32 : emit_move_insn (target, result);
7241 : }
7242 : }
7243 :
7244 : /* Expand an atomic clear operation.
7245 : void _atomic_clear (BOOL *obj, enum memmodel)
7246 : EXP is the call expression. */
7247 :
7248 : static rtx
7249 53 : expand_builtin_atomic_clear (tree exp)
7250 : {
7251 53 : machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7252 53 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7253 53 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7254 :
7255 53 : if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
7256 : model = MEMMODEL_SEQ_CST;
7257 :
7258 : /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
7259 : Failing that, a store is issued by __atomic_store. The only way this can
7260 : fail is if the bool type is larger than a word size. Unlikely, but
7261 : handle it anyway for completeness. Assume a single threaded model since
7262 : there is no atomic support in this case, and no barriers are required. */
7263 53 : rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
7264 53 : if (!ret)
7265 0 : emit_move_insn (mem, const0_rtx);
7266 53 : return const0_rtx;
7267 : }
7268 :
7269 : /* Expand an atomic test_and_set operation.
7270 : bool _atomic_test_and_set (BOOL *obj, enum memmodel)
7271 : EXP is the call expression. */
7272 :
7273 : static rtx
7274 266 : expand_builtin_atomic_test_and_set (tree exp, rtx target)
7275 : {
7276 266 : rtx mem;
7277 266 : enum memmodel model;
7278 266 : machine_mode mode;
7279 :
7280 266 : mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7281 266 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7282 266 : model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7283 :
7284 266 : return expand_atomic_test_and_set (target, mem, model);
7285 : }
7286 :
7287 :
7288 : /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
7289 : this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
7290 :
7291 : static tree
7292 143354 : fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
7293 : {
7294 143354 : HOST_WIDE_INT size;
7295 143354 : machine_mode mode;
7296 143354 : unsigned int mode_align, type_align;
7297 :
7298 143354 : if (!tree_fits_shwi_p (arg0))
7299 : return NULL_TREE;
7300 :
7301 : /* We need a corresponding integer mode for the access to be lock-free. */
7302 143201 : size = tree_to_shwi (arg0) * BITS_PER_UNIT;
7303 143201 : if (!int_mode_for_size (size, 0).exists (&mode))
7304 9 : return boolean_false_node;
7305 :
7306 143192 : mode_align = GET_MODE_ALIGNMENT (mode);
7307 :
7308 143192 : if (tree_fits_uhwi_p (arg1))
7309 : {
7310 123584 : unsigned HOST_WIDE_INT val = tree_to_uhwi (arg1);
7311 :
7312 : /* Either this argument is null, or it's a fake pointer encoding
7313 : the alignment of the object. */
7314 123584 : val = least_bit_hwi (val);
7315 123584 : val *= BITS_PER_UNIT;
7316 :
7317 123584 : if (val == 0 || mode_align < val)
7318 : type_align = mode_align;
7319 : else
7320 18851 : type_align = val;
7321 : }
7322 : else
7323 : {
7324 19608 : tree ttype = TREE_TYPE (arg1);
7325 :
7326 : /* This function is usually invoked and folded immediately by the front
7327 : end before anything else has a chance to look at it. The pointer
7328 : parameter at this point is usually cast to a void *, so check for that
7329 : and look past the cast. */
7330 195 : if (CONVERT_EXPR_P (arg1)
7331 19444 : && POINTER_TYPE_P (ttype)
7332 19444 : && VOID_TYPE_P (TREE_TYPE (ttype))
7333 39052 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
7334 19413 : arg1 = TREE_OPERAND (arg1, 0);
7335 :
7336 19608 : ttype = TREE_TYPE (arg1);
7337 19608 : gcc_assert (POINTER_TYPE_P (ttype));
7338 :
7339 : /* Get the underlying type of the object. */
7340 19608 : ttype = TREE_TYPE (ttype);
7341 19608 : type_align = TYPE_ALIGN (ttype);
7342 : }
7343 :
7344 : /* If the object has smaller alignment, the lock free routines cannot
7345 : be used. */
7346 143192 : if (type_align < mode_align)
7347 247 : return boolean_false_node;
7348 :
7349 : /* Check if a compare_and_swap pattern exists for the mode which represents
7350 : the required size. The pattern is not allowed to fail, so the existence
7351 : of the pattern indicates support is present. Also require that an
7352 : atomic load exists for the required size. */
7353 142945 : if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
7354 142903 : return boolean_true_node;
7355 : else
7356 42 : return boolean_false_node;
7357 : }
7358 :
7359 : /* Return true if the parameters to call EXP represent an object which will
7360 : always generate lock free instructions. The first argument represents the
7361 : size of the object, and the second parameter is a pointer to the object
7362 : itself. If NULL is passed for the object, then the result is based on
7363 : typical alignment for an object of the specified size. Otherwise return
7364 : false. */
7365 :
7366 : static rtx
7367 1 : expand_builtin_atomic_always_lock_free (tree exp)
7368 : {
7369 1 : tree size;
7370 1 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7371 1 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7372 :
7373 1 : if (TREE_CODE (arg0) != INTEGER_CST)
7374 : {
7375 1 : error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
7376 1 : return const0_rtx;
7377 : }
7378 :
7379 0 : size = fold_builtin_atomic_always_lock_free (arg0, arg1);
7380 0 : if (size == boolean_true_node)
7381 0 : return const1_rtx;
7382 0 : return const0_rtx;
7383 : }
7384 :
7385 : /* Return a one or zero if it can be determined that object ARG1 of size ARG
7386 : is lock free on this architecture. */
7387 :
7388 : static tree
7389 38558 : fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
7390 : {
7391 38558 : if (!flag_inline_atomics)
7392 : return NULL_TREE;
7393 :
7394 : /* If it isn't always lock free, don't generate a result. */
7395 38549 : if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
7396 38190 : return boolean_true_node;
7397 :
7398 : return NULL_TREE;
7399 : }
7400 :
7401 : /* Return true if the parameters to call EXP represent an object which will
7402 : always generate lock free instructions. The first argument represents the
7403 : size of the object, and the second parameter is a pointer to the object
7404 : itself. If NULL is passed for the object, then the result is based on
7405 : typical alignment for an object of the specified size. Otherwise return
7406 : NULL*/
7407 :
7408 : static rtx
7409 10 : expand_builtin_atomic_is_lock_free (tree exp)
7410 : {
7411 10 : tree size;
7412 10 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7413 10 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7414 :
7415 10 : if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
7416 : {
7417 0 : error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
7418 0 : return NULL_RTX;
7419 : }
7420 :
7421 10 : if (!flag_inline_atomics)
7422 : return NULL_RTX;
7423 :
7424 : /* If the value is known at compile time, return the RTX for it. */
7425 9 : size = fold_builtin_atomic_is_lock_free (arg0, arg1);
7426 9 : if (size == boolean_true_node)
7427 0 : return const1_rtx;
7428 :
7429 : return NULL_RTX;
7430 : }
7431 :
7432 : /* Expand the __atomic_thread_fence intrinsic:
7433 : void __atomic_thread_fence (enum memmodel)
7434 : EXP is the CALL_EXPR. */
7435 :
7436 : static void
7437 719 : expand_builtin_atomic_thread_fence (tree exp)
7438 : {
7439 719 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7440 719 : expand_mem_thread_fence (model);
7441 719 : }
7442 :
7443 : /* Expand the __atomic_signal_fence intrinsic:
7444 : void __atomic_signal_fence (enum memmodel)
7445 : EXP is the CALL_EXPR. */
7446 :
7447 : static void
7448 60 : expand_builtin_atomic_signal_fence (tree exp)
7449 : {
7450 60 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7451 60 : expand_mem_signal_fence (model);
7452 60 : }
7453 :
7454 : /* Expand the __sync_synchronize intrinsic. */
7455 :
7456 : static void
7457 275 : expand_builtin_sync_synchronize (void)
7458 : {
7459 0 : expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
7460 0 : }
7461 :
7462 : static rtx
7463 3 : expand_builtin_thread_pointer (tree exp, rtx target)
7464 : {
7465 3 : enum insn_code icode;
7466 3 : if (!validate_arglist (exp, VOID_TYPE))
7467 0 : return const0_rtx;
7468 3 : icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
7469 3 : if (icode != CODE_FOR_nothing)
7470 : {
7471 3 : class expand_operand op;
7472 : /* If the target is not sutitable then create a new target. */
7473 3 : if (target == NULL_RTX
7474 3 : || !REG_P (target)
7475 6 : || GET_MODE (target) != Pmode)
7476 0 : target = gen_reg_rtx (Pmode);
7477 3 : create_output_operand (&op, target, Pmode);
7478 3 : expand_insn (icode, 1, &op);
7479 3 : return target;
7480 : }
7481 0 : error ("%<__builtin_thread_pointer%> is not supported on this target");
7482 0 : return const0_rtx;
7483 : }
7484 :
7485 : static void
7486 0 : expand_builtin_set_thread_pointer (tree exp)
7487 : {
7488 0 : enum insn_code icode;
7489 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7490 : return;
7491 0 : icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
7492 0 : if (icode != CODE_FOR_nothing)
7493 : {
7494 0 : class expand_operand op;
7495 0 : rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
7496 0 : Pmode, EXPAND_NORMAL);
7497 0 : create_input_operand (&op, val, Pmode);
7498 0 : expand_insn (icode, 1, &op);
7499 0 : return;
7500 : }
7501 0 : error ("%<__builtin_set_thread_pointer%> is not supported on this target");
7502 : }
7503 :
7504 :
7505 : /* Emit code to restore the current value of stack. */
7506 :
7507 : static void
7508 1773 : expand_stack_restore (tree var)
7509 : {
7510 1773 : rtx_insn *prev;
7511 1773 : rtx sa = expand_normal (var);
7512 :
7513 1829 : sa = convert_memory_address (Pmode, sa);
7514 :
7515 1773 : prev = get_last_insn ();
7516 1773 : emit_stack_restore (SAVE_BLOCK, sa);
7517 :
7518 1773 : record_new_stack_level ();
7519 :
7520 1773 : fixup_args_size_notes (prev, get_last_insn (), 0);
7521 1773 : }
7522 :
7523 : /* Emit code to save the current value of stack. */
7524 :
7525 : static rtx
7526 1878 : expand_stack_save (void)
7527 : {
7528 1878 : rtx ret = NULL_RTX;
7529 :
7530 0 : emit_stack_save (SAVE_BLOCK, &ret);
7531 1878 : return ret;
7532 : }
7533 :
7534 : /* Emit code to get the openacc gang, worker or vector id or size. */
7535 :
7536 : static rtx
7537 356 : expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7538 : {
7539 356 : const char *name;
7540 356 : rtx fallback_retval;
7541 356 : rtx_insn *(*gen_fn) (rtx, rtx);
7542 356 : switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7543 : {
7544 244 : case BUILT_IN_GOACC_PARLEVEL_ID:
7545 244 : name = "__builtin_goacc_parlevel_id";
7546 244 : fallback_retval = const0_rtx;
7547 244 : gen_fn = targetm.gen_oacc_dim_pos;
7548 244 : break;
7549 112 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
7550 112 : name = "__builtin_goacc_parlevel_size";
7551 112 : fallback_retval = const1_rtx;
7552 112 : gen_fn = targetm.gen_oacc_dim_size;
7553 112 : break;
7554 0 : default:
7555 0 : gcc_unreachable ();
7556 : }
7557 :
7558 356 : if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7559 : {
7560 8 : error ("%qs only supported in OpenACC code", name);
7561 8 : return const0_rtx;
7562 : }
7563 :
7564 348 : tree arg = CALL_EXPR_ARG (exp, 0);
7565 348 : if (TREE_CODE (arg) != INTEGER_CST)
7566 : {
7567 8 : error ("non-constant argument 0 to %qs", name);
7568 8 : return const0_rtx;
7569 : }
7570 :
7571 340 : int dim = TREE_INT_CST_LOW (arg);
7572 340 : switch (dim)
7573 : {
7574 324 : case GOMP_DIM_GANG:
7575 324 : case GOMP_DIM_WORKER:
7576 324 : case GOMP_DIM_VECTOR:
7577 324 : break;
7578 16 : default:
7579 16 : error ("illegal argument 0 to %qs", name);
7580 16 : return const0_rtx;
7581 : }
7582 :
7583 324 : if (ignore)
7584 : return target;
7585 :
7586 180 : if (target == NULL_RTX)
7587 0 : target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7588 :
7589 180 : if (!targetm.have_oacc_dim_size ())
7590 : {
7591 180 : emit_move_insn (target, fallback_retval);
7592 180 : return target;
7593 : }
7594 :
7595 0 : rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7596 0 : emit_insn (gen_fn (reg, GEN_INT (dim)));
7597 0 : if (reg != target)
7598 0 : emit_move_insn (target, reg);
7599 :
7600 : return target;
7601 : }
7602 :
7603 : /* Expand a string compare operation using a sequence of char comparison
7604 : to get rid of the calling overhead, with result going to TARGET if
7605 : that's convenient.
7606 :
7607 : VAR_STR is the variable string source;
7608 : CONST_STR is the constant string source;
7609 : LENGTH is the number of chars to compare;
7610 : CONST_STR_N indicates which source string is the constant string;
7611 : IS_MEMCMP indicates whether it's a memcmp or strcmp.
7612 :
7613 : to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7614 :
7615 : target = (int) (unsigned char) var_str[0]
7616 : - (int) (unsigned char) const_str[0];
7617 : if (target != 0)
7618 : goto ne_label;
7619 : ...
7620 : target = (int) (unsigned char) var_str[length - 2]
7621 : - (int) (unsigned char) const_str[length - 2];
7622 : if (target != 0)
7623 : goto ne_label;
7624 : target = (int) (unsigned char) var_str[length - 1]
7625 : - (int) (unsigned char) const_str[length - 1];
7626 : ne_label:
7627 : */
7628 :
7629 : static rtx
7630 635 : inline_string_cmp (rtx target, tree var_str, const char *const_str,
7631 : unsigned HOST_WIDE_INT length,
7632 : int const_str_n, machine_mode mode)
7633 : {
7634 635 : HOST_WIDE_INT offset = 0;
7635 635 : rtx var_rtx_array
7636 635 : = get_memory_rtx (var_str, build_int_cst (unsigned_type_node, length),
7637 : false);
7638 635 : rtx var_rtx = NULL_RTX;
7639 635 : rtx const_rtx = NULL_RTX;
7640 635 : rtx result = target ? target : gen_reg_rtx (mode);
7641 635 : rtx_code_label *ne_label = gen_label_rtx ();
7642 635 : tree unit_type_node = unsigned_char_type_node;
7643 635 : scalar_int_mode unit_mode
7644 635 : = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7645 :
7646 635 : start_sequence ();
7647 :
7648 2964 : for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7649 : {
7650 1694 : var_rtx
7651 1694 : = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7652 1694 : const_rtx = c_readstr (const_str + offset, unit_mode);
7653 1694 : rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7654 1544 : rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7655 :
7656 1694 : op0 = convert_modes (mode, unit_mode, op0, 1);
7657 1694 : op1 = convert_modes (mode, unit_mode, op1, 1);
7658 1694 : rtx diff = expand_simple_binop (mode, MINUS, op0, op1,
7659 : result, 1, OPTAB_WIDEN);
7660 :
7661 : /* Force the difference into result register. We cannot reassign
7662 : result here ("result = diff") or we may end up returning
7663 : uninitialized result when expand_simple_binop allocates a new
7664 : pseudo-register for returning. */
7665 1694 : if (diff != result)
7666 0 : emit_move_insn (result, diff);
7667 :
7668 1694 : if (i < length - 1)
7669 1059 : emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7670 : mode, true, ne_label);
7671 3388 : offset += GET_MODE_SIZE (unit_mode);
7672 : }
7673 :
7674 635 : emit_label (ne_label);
7675 635 : rtx_insn *insns = end_sequence ();
7676 635 : emit_insn (insns);
7677 :
7678 635 : return result;
7679 : }
7680 :
7681 : /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7682 : to TARGET if that's convenient.
7683 : If the call is not been inlined, return NULL_RTX. */
7684 :
7685 : static rtx
7686 145218 : inline_expand_builtin_bytecmp (tree exp, rtx target)
7687 : {
7688 145218 : tree fndecl = get_callee_fndecl (exp);
7689 145218 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7690 145218 : bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7691 :
7692 : /* Do NOT apply this inlining expansion when optimizing for size or
7693 : optimization level below 2 or if unused *cmp hasn't been DCEd. */
7694 145218 : if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7695 : return NULL_RTX;
7696 :
7697 23066 : gcc_checking_assert (fcode == BUILT_IN_STRCMP
7698 : || fcode == BUILT_IN_STRNCMP
7699 : || fcode == BUILT_IN_MEMCMP);
7700 :
7701 : /* On a target where the type of the call (int) has same or narrower precision
7702 : than unsigned char, give up the inlining expansion. */
7703 23066 : if (TYPE_PRECISION (unsigned_char_type_node)
7704 23066 : >= TYPE_PRECISION (TREE_TYPE (exp)))
7705 : return NULL_RTX;
7706 :
7707 23066 : tree arg1 = CALL_EXPR_ARG (exp, 0);
7708 23066 : tree arg2 = CALL_EXPR_ARG (exp, 1);
7709 23066 : tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7710 :
7711 23066 : unsigned HOST_WIDE_INT len1 = 0;
7712 23066 : unsigned HOST_WIDE_INT len2 = 0;
7713 23066 : unsigned HOST_WIDE_INT len3 = 0;
7714 :
7715 : /* Get the object representation of the initializers of ARG1 and ARG2
7716 : as strings, provided they refer to constant objects, with their byte
7717 : sizes in LEN1 and LEN2, respectively. */
7718 23066 : const char *bytes1 = getbyterep (arg1, &len1);
7719 23066 : const char *bytes2 = getbyterep (arg2, &len2);
7720 :
7721 : /* Fail if neither argument refers to an initialized constant. */
7722 23066 : if (!bytes1 && !bytes2)
7723 : return NULL_RTX;
7724 :
7725 18431 : if (is_ncmp)
7726 : {
7727 : /* Fail if the memcmp/strncmp bound is not a constant. */
7728 1322 : if (!tree_fits_uhwi_p (len3_tree))
7729 : return NULL_RTX;
7730 :
7731 962 : len3 = tree_to_uhwi (len3_tree);
7732 :
7733 962 : if (fcode == BUILT_IN_MEMCMP)
7734 : {
7735 : /* Fail if the memcmp bound is greater than the size of either
7736 : of the two constant objects. */
7737 462 : if ((bytes1 && len1 < len3)
7738 462 : || (bytes2 && len2 < len3))
7739 : return NULL_RTX;
7740 : }
7741 : }
7742 :
7743 : if (fcode != BUILT_IN_MEMCMP)
7744 : {
7745 : /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7746 : and LEN2 to the length of the nul-terminated string stored
7747 : in each. */
7748 17609 : if (bytes1 != NULL)
7749 236 : len1 = strnlen (bytes1, len1) + 1;
7750 17609 : if (bytes2 != NULL)
7751 17376 : len2 = strnlen (bytes2, len2) + 1;
7752 : }
7753 :
7754 : /* See inline_string_cmp. */
7755 18067 : int const_str_n;
7756 18067 : if (!len1)
7757 : const_str_n = 2;
7758 241 : else if (!len2)
7759 : const_str_n = 1;
7760 3 : else if (len2 > len1)
7761 : const_str_n = 1;
7762 : else
7763 : const_str_n = 2;
7764 :
7765 : /* For strncmp only, compute the new bound as the smallest of
7766 : the lengths of the two strings (plus 1) and the bound provided
7767 : to the function. */
7768 17828 : unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7769 18067 : if (is_ncmp && len3 < bound)
7770 534 : bound = len3;
7771 :
7772 : /* If the bound of the comparison is larger than the threshold,
7773 : do nothing. */
7774 18067 : if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7775 : return NULL_RTX;
7776 :
7777 635 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7778 :
7779 : /* Now, start inline expansion the call. */
7780 1213 : return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7781 : (const_str_n == 1) ? bytes1 : bytes2, bound,
7782 635 : const_str_n, mode);
7783 : }
7784 :
7785 : /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7786 : represents the size of the first argument to that call, or VOIDmode
7787 : if the argument is a pointer. IGNORE will be true if the result
7788 : isn't used. */
7789 : static rtx
7790 35 : expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7791 : bool ignore)
7792 : {
7793 35 : rtx val, failsafe;
7794 35 : unsigned nargs = call_expr_nargs (exp);
7795 :
7796 35 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7797 :
7798 35 : if (mode == VOIDmode)
7799 : {
7800 4 : mode = TYPE_MODE (TREE_TYPE (arg0));
7801 4 : gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7802 : }
7803 :
7804 35 : val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7805 :
7806 : /* An optional second argument can be used as a failsafe value on
7807 : some machines. If it isn't present, then the failsafe value is
7808 : assumed to be 0. */
7809 35 : if (nargs > 1)
7810 : {
7811 4 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7812 4 : failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7813 : }
7814 : else
7815 31 : failsafe = const0_rtx;
7816 :
7817 : /* If the result isn't used, the behavior is undefined. It would be
7818 : nice to emit a warning here, but path splitting means this might
7819 : happen with legitimate code. So simply drop the builtin
7820 : expansion in that case; we've handled any side-effects above. */
7821 35 : if (ignore)
7822 0 : return const0_rtx;
7823 :
7824 : /* If we don't have a suitable target, create one to hold the result. */
7825 35 : if (target == NULL || GET_MODE (target) != mode)
7826 0 : target = gen_reg_rtx (mode);
7827 :
7828 35 : if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7829 0 : val = convert_modes (mode, VOIDmode, val, false);
7830 :
7831 35 : return targetm.speculation_safe_value (mode, target, val, failsafe);
7832 : }
7833 :
7834 : /* Expand CRC* or REV_CRC* built-ins. */
7835 :
7836 : rtx
7837 46 : expand_builtin_crc_table_based (internal_fn fn, scalar_mode crc_mode,
7838 : scalar_mode data_mode, machine_mode mode,
7839 : tree exp, rtx target)
7840 : {
7841 46 : tree rhs1 = CALL_EXPR_ARG (exp, 0); // crc
7842 46 : tree rhs2 = CALL_EXPR_ARG (exp, 1); // data
7843 46 : tree rhs3 = CALL_EXPR_ARG (exp, 2); // polynomial
7844 :
7845 46 : if (!target || mode == VOIDmode)
7846 0 : target = gen_reg_rtx (crc_mode);
7847 :
7848 46 : rtx op1 = expand_normal (rhs1);
7849 46 : rtx op2 = expand_normal (rhs2);
7850 46 : rtx op3;
7851 46 : if (TREE_CODE (rhs3) != INTEGER_CST)
7852 : {
7853 1 : error ("third argument to %<crc%> builtins must be a constant");
7854 1 : op3 = const0_rtx;
7855 : }
7856 : else
7857 45 : op3 = convert_to_mode (crc_mode, expand_normal (rhs3), 0);
7858 :
7859 46 : if (CONST_INT_P (op2))
7860 32 : op2 = convert_to_mode (crc_mode, op2, 0);
7861 :
7862 46 : if (fn == IFN_CRC)
7863 58 : expand_crc_table_based (target, op1, op2, op3,
7864 : GET_MODE_BITSIZE (data_mode));
7865 : else
7866 : /* If it's IFN_CRC_REV generate bit-reversed CRC. */
7867 34 : expand_reversed_crc_table_based (target, op1, op2, op3,
7868 : GET_MODE_BITSIZE (data_mode));
7869 46 : return target;
7870 : }
7871 :
7872 : /* Expand an expression EXP that calls a built-in function,
7873 : with result going to TARGET if that's convenient
7874 : (and in mode MODE if that's convenient).
7875 : SUBTARGET may be used as the target for computing one of EXP's operands.
7876 : IGNORE is nonzero if the value is to be ignored. */
7877 :
7878 : rtx
7879 2053834 : expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7880 : int ignore)
7881 : {
7882 2053834 : tree fndecl = get_callee_fndecl (exp);
7883 2053834 : machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7884 2053834 : int flags;
7885 :
7886 2053834 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7887 177791 : return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7888 :
7889 : /* When ASan is enabled, we don't want to expand some memory/string
7890 : builtins and rely on libsanitizer's hooks. This allows us to avoid
7891 : redundant checks and be sure, that possible overflow will be detected
7892 : by ASan. */
7893 :
7894 1876043 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7895 1876043 : if (param_asan_kernel_mem_intrinsic_prefix
7896 1876043 : && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7897 : | SANITIZE_KERNEL_HWADDRESS))
7898 42 : switch (fcode)
7899 : {
7900 42 : rtx save_decl_rtl, ret;
7901 42 : case BUILT_IN_MEMCPY:
7902 42 : case BUILT_IN_MEMMOVE:
7903 42 : case BUILT_IN_MEMSET:
7904 42 : save_decl_rtl = DECL_RTL (fndecl);
7905 42 : DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7906 42 : ret = expand_call (exp, target, ignore);
7907 42 : DECL_RTL (fndecl) = save_decl_rtl;
7908 42 : return ret;
7909 : default:
7910 : break;
7911 : }
7912 1876001 : if (sanitize_flags_p (SANITIZE_ADDRESS | SANITIZE_HWADDRESS)
7913 1876001 : && asan_intercepted_p (fcode))
7914 1116 : return expand_call (exp, target, ignore);
7915 :
7916 : /* When not optimizing, generate calls to library functions for a certain
7917 : set of builtins. */
7918 1874885 : if (!optimize
7919 440989 : && !called_as_built_in (fndecl)
7920 147303 : && fcode != BUILT_IN_FORK
7921 147303 : && fcode != BUILT_IN_EXECL
7922 147289 : && fcode != BUILT_IN_EXECV
7923 147289 : && fcode != BUILT_IN_EXECLP
7924 147287 : && fcode != BUILT_IN_EXECLE
7925 147287 : && fcode != BUILT_IN_EXECVP
7926 147286 : && fcode != BUILT_IN_EXECVE
7927 147286 : && fcode != BUILT_IN_CLEAR_CACHE
7928 147283 : && !ALLOCA_FUNCTION_CODE_P (fcode)
7929 147229 : && fcode != BUILT_IN_FREE
7930 145061 : && (fcode != BUILT_IN_MEMSET
7931 55904 : || !(flag_inline_stringops & ILSOP_MEMSET))
7932 145058 : && (fcode != BUILT_IN_MEMCPY
7933 918 : || !(flag_inline_stringops & ILSOP_MEMCPY))
7934 144922 : && (fcode != BUILT_IN_MEMMOVE
7935 348 : || !(flag_inline_stringops & ILSOP_MEMMOVE))
7936 2019807 : && (fcode != BUILT_IN_MEMCMP
7937 2532 : || !(flag_inline_stringops & ILSOP_MEMCMP)))
7938 144874 : return expand_call (exp, target, ignore);
7939 :
7940 : /* The built-in function expanders test for target == const0_rtx
7941 : to determine whether the function's result will be ignored. */
7942 1730011 : if (ignore)
7943 1029285 : target = const0_rtx;
7944 :
7945 : /* If the result of a pure or const built-in function is ignored, and
7946 : none of its arguments are volatile, we can avoid expanding the
7947 : built-in call and just evaluate the arguments for side-effects. */
7948 1730011 : if (target == const0_rtx
7949 1029285 : && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7950 1733795 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
7951 : {
7952 10 : bool volatilep = false;
7953 10 : tree arg;
7954 10 : call_expr_arg_iterator iter;
7955 :
7956 32 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7957 12 : if (TREE_THIS_VOLATILE (arg))
7958 : {
7959 : volatilep = true;
7960 : break;
7961 : }
7962 :
7963 10 : if (! volatilep)
7964 : {
7965 32 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7966 12 : expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7967 10 : return const0_rtx;
7968 : }
7969 : }
7970 :
7971 1730001 : switch (fcode)
7972 : {
7973 4 : CASE_FLT_FN (BUILT_IN_FABS):
7974 4 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7975 4 : case BUILT_IN_FABSD32:
7976 4 : case BUILT_IN_FABSD64:
7977 4 : case BUILT_IN_FABSD128:
7978 4 : case BUILT_IN_FABSD64X:
7979 4 : target = expand_builtin_fabs (exp, target, subtarget);
7980 4 : if (target)
7981 850091 : return target;
7982 879910 : break;
7983 :
7984 11677 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
7985 11677 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7986 11677 : target = expand_builtin_copysign (exp, target, subtarget);
7987 11677 : if (target)
7988 : return target;
7989 : break;
7990 :
7991 : /* Just do a normal library call if we were unable to fold
7992 : the values. */
7993 : CASE_FLT_FN (BUILT_IN_CABS):
7994 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7995 : break;
7996 :
7997 372 : CASE_FLT_FN (BUILT_IN_FMA):
7998 372 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7999 372 : target = expand_builtin_mathfn_ternary (exp, target, subtarget);
8000 372 : if (target)
8001 : return target;
8002 : break;
8003 :
8004 232 : CASE_FLT_FN (BUILT_IN_ILOGB):
8005 232 : if (! flag_unsafe_math_optimizations)
8006 : break;
8007 4 : gcc_fallthrough ();
8008 4 : CASE_FLT_FN (BUILT_IN_ISINF):
8009 4 : CASE_FLT_FN (BUILT_IN_FINITE):
8010 4 : CASE_FLT_FN (BUILT_IN_ISNAN):
8011 4 : case BUILT_IN_ISFINITE:
8012 4 : case BUILT_IN_ISNORMAL:
8013 4 : target = expand_builtin_interclass_mathfn (exp, target);
8014 4 : if (target)
8015 : return target;
8016 : break;
8017 :
8018 784 : case BUILT_IN_ISSIGNALING:
8019 784 : target = expand_builtin_issignaling (exp, target);
8020 784 : if (target)
8021 : return target;
8022 : break;
8023 :
8024 223 : CASE_FLT_FN (BUILT_IN_ICEIL):
8025 223 : CASE_FLT_FN (BUILT_IN_LCEIL):
8026 223 : CASE_FLT_FN (BUILT_IN_LLCEIL):
8027 223 : CASE_FLT_FN (BUILT_IN_LFLOOR):
8028 223 : CASE_FLT_FN (BUILT_IN_IFLOOR):
8029 223 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
8030 223 : target = expand_builtin_int_roundingfn (exp, target);
8031 223 : if (target)
8032 : return target;
8033 : break;
8034 :
8035 572 : CASE_FLT_FN (BUILT_IN_IRINT):
8036 572 : CASE_FLT_FN (BUILT_IN_LRINT):
8037 572 : CASE_FLT_FN (BUILT_IN_LLRINT):
8038 572 : CASE_FLT_FN (BUILT_IN_IROUND):
8039 572 : CASE_FLT_FN (BUILT_IN_LROUND):
8040 572 : CASE_FLT_FN (BUILT_IN_LLROUND):
8041 572 : target = expand_builtin_int_roundingfn_2 (exp, target);
8042 572 : if (target)
8043 : return target;
8044 : break;
8045 :
8046 293 : CASE_FLT_FN (BUILT_IN_POWI):
8047 293 : target = expand_builtin_powi (exp, target);
8048 293 : if (target)
8049 : return target;
8050 : break;
8051 :
8052 173 : CASE_FLT_FN (BUILT_IN_CEXPI):
8053 173 : target = expand_builtin_cexpi (exp, target);
8054 173 : gcc_assert (target);
8055 : return target;
8056 :
8057 1010 : CASE_FLT_FN (BUILT_IN_SIN):
8058 1010 : CASE_FLT_FN (BUILT_IN_COS):
8059 1010 : if (! flag_unsafe_math_optimizations)
8060 : break;
8061 47 : target = expand_builtin_mathfn_3 (exp, target, subtarget);
8062 47 : if (target)
8063 : return target;
8064 : break;
8065 :
8066 167 : CASE_FLT_FN (BUILT_IN_SINCOS):
8067 167 : if (! flag_unsafe_math_optimizations)
8068 : break;
8069 3 : target = expand_builtin_sincos (exp);
8070 3 : if (target)
8071 : return target;
8072 : break;
8073 :
8074 60 : case BUILT_IN_FEGETROUND:
8075 60 : target = expand_builtin_fegetround (exp, target, target_mode);
8076 60 : if (target)
8077 : return target;
8078 : break;
8079 :
8080 1235 : case BUILT_IN_FECLEAREXCEPT:
8081 1235 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8082 : feclearexcept_optab);
8083 1235 : if (target)
8084 : return target;
8085 : break;
8086 :
8087 831 : case BUILT_IN_FERAISEEXCEPT:
8088 831 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8089 : feraiseexcept_optab);
8090 831 : if (target)
8091 : return target;
8092 : break;
8093 :
8094 464 : case BUILT_IN_APPLY_ARGS:
8095 464 : return expand_builtin_apply_args ();
8096 :
8097 : /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
8098 : FUNCTION with a copy of the parameters described by
8099 : ARGUMENTS, and ARGSIZE. It returns a block of memory
8100 : allocated on the stack into which is stored all the registers
8101 : that might possibly be used for returning the result of a
8102 : function. ARGUMENTS is the value returned by
8103 : __builtin_apply_args. ARGSIZE is the number of bytes of
8104 : arguments that must be copied. ??? How should this value be
8105 : computed? We'll also need a safe worst case value for varargs
8106 : functions. */
8107 483 : case BUILT_IN_APPLY:
8108 483 : if (!validate_arglist (exp, POINTER_TYPE,
8109 : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
8110 483 : && !validate_arglist (exp, REFERENCE_TYPE,
8111 : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8112 0 : return const0_rtx;
8113 : else
8114 : {
8115 483 : rtx ops[3];
8116 :
8117 483 : ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
8118 483 : ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
8119 483 : ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
8120 :
8121 483 : return expand_builtin_apply (ops[0], ops[1], ops[2]);
8122 : }
8123 :
8124 : /* __builtin_return (RESULT) causes the function to return the
8125 : value described by RESULT. RESULT is address of the block of
8126 : memory returned by __builtin_apply. */
8127 379 : case BUILT_IN_RETURN:
8128 379 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8129 379 : expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
8130 379 : return const0_rtx;
8131 :
8132 0 : case BUILT_IN_CALL_CODE_ADDRESS:
8133 : /* All valid uses of __builtin_call_code_address () are removed in
8134 : in tree-nested.cc or gimple-fold.cc. */
8135 0 : error ("argument to %<__builtin_call_code_address%> must be a function");
8136 0 : return const0_rtx;
8137 :
8138 0 : case BUILT_IN_CALL_STATIC_CHAIN:
8139 : /* All valid uses of __builtin_call_static_chain () are removed in
8140 : in tree-nested.cc or gimple-fold.cc. */
8141 0 : error ("argument to %<__builtin_call_static_chain%> must be a function");
8142 0 : return const0_rtx;
8143 :
8144 0 : case BUILT_IN_SAVEREGS:
8145 0 : return expand_builtin_saveregs ();
8146 :
8147 16 : case BUILT_IN_VA_ARG_PACK:
8148 : /* All valid uses of __builtin_va_arg_pack () are removed during
8149 : inlining. */
8150 16 : error ("invalid use of %<__builtin_va_arg_pack ()%>");
8151 16 : return const0_rtx;
8152 :
8153 0 : case BUILT_IN_VA_ARG_PACK_LEN:
8154 : /* All valid uses of __builtin_va_arg_pack_len () are removed during
8155 : inlining. */
8156 0 : error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
8157 0 : return const0_rtx;
8158 :
8159 : /* Return the address of the first anonymous stack arg. */
8160 158 : case BUILT_IN_NEXT_ARG:
8161 158 : if (fold_builtin_next_arg (exp, false))
8162 0 : return const0_rtx;
8163 158 : return expand_builtin_next_arg ();
8164 :
8165 28 : case BUILT_IN_CLEAR_CACHE:
8166 28 : expand_builtin___clear_cache (exp);
8167 28 : return const0_rtx;
8168 :
8169 0 : case BUILT_IN_CLASSIFY_TYPE:
8170 0 : return expand_builtin_classify_type (exp);
8171 :
8172 0 : case BUILT_IN_CONSTANT_P:
8173 0 : return const0_rtx;
8174 :
8175 15936 : case BUILT_IN_FRAME_ADDRESS:
8176 15936 : case BUILT_IN_RETURN_ADDRESS:
8177 15936 : return expand_builtin_frame_address (fndecl, exp);
8178 :
8179 545 : case BUILT_IN_STACK_ADDRESS:
8180 545 : return expand_builtin_stack_address ();
8181 :
8182 2175 : case BUILT_IN___STRUB_ENTER:
8183 2175 : target = expand_builtin_strub_enter (exp);
8184 2175 : if (target)
8185 : return target;
8186 : break;
8187 :
8188 1091 : case BUILT_IN___STRUB_UPDATE:
8189 1091 : target = expand_builtin_strub_update (exp);
8190 1091 : if (target)
8191 : return target;
8192 : break;
8193 :
8194 2745 : case BUILT_IN___STRUB_LEAVE:
8195 2745 : target = expand_builtin_strub_leave (exp);
8196 2745 : if (target)
8197 : return target;
8198 : break;
8199 :
8200 : /* Returns the address of the area where the structure is returned.
8201 : 0 otherwise. */
8202 0 : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
8203 0 : if (call_expr_nargs (exp) != 0
8204 0 : || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
8205 0 : || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
8206 0 : return const0_rtx;
8207 : else
8208 0 : return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
8209 :
8210 28032 : CASE_BUILT_IN_ALLOCA:
8211 28032 : target = expand_builtin_alloca (exp);
8212 28032 : if (target)
8213 : return target;
8214 : break;
8215 :
8216 204 : case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
8217 204 : return expand_asan_emit_allocas_unpoison (exp);
8218 :
8219 1878 : case BUILT_IN_STACK_SAVE:
8220 1878 : return expand_stack_save ();
8221 :
8222 1773 : case BUILT_IN_STACK_RESTORE:
8223 1773 : expand_stack_restore (CALL_EXPR_ARG (exp, 0));
8224 1773 : return const0_rtx;
8225 :
8226 159 : CASE_INTSZ_FN (BUILT_IN_BSWAP):
8227 159 : target = expand_builtin_bswap (target_mode, exp, target, subtarget);
8228 159 : if (target)
8229 : return target;
8230 : break;
8231 :
8232 109 : CASE_INTSZ_FN (BUILT_IN_BITREVERSE):
8233 : /* Not part of CASE_INTSZ_FN: there is no BUILT_IN_BSWAP8. */
8234 109 : case BUILT_IN_BITREVERSE8:
8235 109 : target = expand_builtin_unop (target_mode, exp, target, subtarget,
8236 : bitreverse_optab);
8237 109 : if (target)
8238 : return target;
8239 : break;
8240 :
8241 0 : CASE_INT_FN (BUILT_IN_FFS):
8242 0 : target = expand_builtin_unop (target_mode, exp, target,
8243 : subtarget, ffs_optab);
8244 0 : if (target)
8245 : return target;
8246 : break;
8247 :
8248 206 : CASE_INT_FN (BUILT_IN_CLZ):
8249 206 : target = expand_builtin_unop (target_mode, exp, target,
8250 : subtarget, clz_optab);
8251 206 : if (target)
8252 : return target;
8253 : break;
8254 :
8255 48 : CASE_INT_FN (BUILT_IN_CTZ):
8256 48 : target = expand_builtin_unop (target_mode, exp, target,
8257 : subtarget, ctz_optab);
8258 48 : if (target)
8259 : return target;
8260 : break;
8261 :
8262 91 : CASE_INT_FN (BUILT_IN_CLRSB):
8263 91 : target = expand_builtin_unop (target_mode, exp, target,
8264 : subtarget, clrsb_optab);
8265 91 : if (target)
8266 : return target;
8267 : break;
8268 :
8269 496 : CASE_INT_FN (BUILT_IN_POPCOUNT):
8270 496 : target = expand_builtin_unop (target_mode, exp, target,
8271 : subtarget, popcount_optab);
8272 496 : if (target)
8273 : return target;
8274 : break;
8275 :
8276 11 : CASE_INT_FN (BUILT_IN_PARITY):
8277 11 : target = expand_builtin_unop (target_mode, exp, target,
8278 : subtarget, parity_optab);
8279 11 : if (target)
8280 : return target;
8281 : break;
8282 :
8283 13769 : case BUILT_IN_STRLEN:
8284 13769 : target = expand_builtin_strlen (exp, target, target_mode);
8285 13769 : if (target)
8286 : return target;
8287 : break;
8288 :
8289 582 : case BUILT_IN_STRNLEN:
8290 582 : target = expand_builtin_strnlen (exp, target, target_mode);
8291 582 : if (target)
8292 : return target;
8293 : break;
8294 :
8295 1861 : case BUILT_IN_STRCPY:
8296 1861 : target = expand_builtin_strcpy (exp, target);
8297 1861 : if (target)
8298 : return target;
8299 : break;
8300 :
8301 2177 : case BUILT_IN_STRNCPY:
8302 2177 : target = expand_builtin_strncpy (exp, target);
8303 2177 : if (target)
8304 : return target;
8305 : break;
8306 :
8307 454 : case BUILT_IN_STPCPY:
8308 454 : target = expand_builtin_stpcpy (exp, target, mode);
8309 454 : if (target)
8310 : return target;
8311 : break;
8312 :
8313 107001 : case BUILT_IN_MEMCPY:
8314 107001 : target = expand_builtin_memcpy (exp, target);
8315 107001 : if (target)
8316 : return target;
8317 : break;
8318 :
8319 17420 : case BUILT_IN_MEMMOVE:
8320 17420 : target = expand_builtin_memmove (exp, target);
8321 17420 : if (target)
8322 : return target;
8323 : break;
8324 :
8325 1395 : case BUILT_IN_MEMPCPY:
8326 1395 : target = expand_builtin_mempcpy (exp, target);
8327 1395 : if (target)
8328 : return target;
8329 : break;
8330 :
8331 38823 : case BUILT_IN_MEMSET:
8332 38823 : target = expand_builtin_memset (exp, target, mode);
8333 38823 : if (target)
8334 : return target;
8335 : break;
8336 :
8337 0 : case BUILT_IN_BZERO:
8338 0 : target = expand_builtin_bzero (exp);
8339 0 : if (target)
8340 : return target;
8341 : break;
8342 :
8343 : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8344 : back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
8345 : when changing it to a strcmp call. */
8346 371 : case BUILT_IN_STRCMP_EQ:
8347 371 : target = expand_builtin_memcmp (exp, target, true);
8348 371 : if (target)
8349 : return target;
8350 :
8351 : /* Change this call back to a BUILT_IN_STRCMP. */
8352 25 : TREE_OPERAND (exp, 1)
8353 25 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
8354 :
8355 : /* Delete the last parameter. */
8356 25 : unsigned int i;
8357 25 : vec<tree, va_gc> *arg_vec;
8358 25 : vec_alloc (arg_vec, 2);
8359 100 : for (i = 0; i < 2; i++)
8360 50 : arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
8361 25 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
8362 : /* FALLTHROUGH */
8363 :
8364 127946 : case BUILT_IN_STRCMP:
8365 127946 : target = expand_builtin_strcmp (exp, target);
8366 127946 : if (target)
8367 : return target;
8368 : break;
8369 :
8370 : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8371 : back to a BUILT_IN_STRNCMP. */
8372 43 : case BUILT_IN_STRNCMP_EQ:
8373 43 : target = expand_builtin_memcmp (exp, target, true);
8374 43 : if (target)
8375 : return target;
8376 :
8377 : /* Change it back to a BUILT_IN_STRNCMP. */
8378 0 : TREE_OPERAND (exp, 1)
8379 0 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
8380 : /* FALLTHROUGH */
8381 :
8382 2027 : case BUILT_IN_STRNCMP:
8383 2027 : target = expand_builtin_strncmp (exp, target, mode);
8384 2027 : if (target)
8385 : return target;
8386 : break;
8387 :
8388 105588 : case BUILT_IN_BCMP:
8389 105588 : case BUILT_IN_MEMCMP:
8390 105588 : case BUILT_IN_MEMCMP_EQ:
8391 105588 : target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
8392 105588 : if (target)
8393 : return target;
8394 37868 : if (fcode == BUILT_IN_MEMCMP_EQ)
8395 : {
8396 22989 : tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
8397 22989 : TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
8398 : }
8399 : break;
8400 :
8401 0 : case BUILT_IN_SETJMP:
8402 : /* This should have been lowered to the builtins below. */
8403 0 : gcc_unreachable ();
8404 :
8405 842 : case BUILT_IN_SETJMP_SETUP:
8406 : /* __builtin_setjmp_setup is passed a pointer to an array of five words
8407 : and the receiver label. */
8408 842 : if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8409 : {
8410 842 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8411 : VOIDmode, EXPAND_NORMAL);
8412 842 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
8413 842 : rtx_insn *label_r = label_rtx (label);
8414 :
8415 842 : expand_builtin_setjmp_setup (buf_addr, label_r);
8416 842 : return const0_rtx;
8417 : }
8418 : break;
8419 :
8420 842 : case BUILT_IN_SETJMP_RECEIVER:
8421 : /* __builtin_setjmp_receiver is passed the receiver label. */
8422 842 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8423 : {
8424 842 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
8425 842 : rtx_insn *label_r = label_rtx (label);
8426 :
8427 842 : expand_builtin_setjmp_receiver (label_r);
8428 842 : nonlocal_goto_handler_labels
8429 1684 : = gen_rtx_INSN_LIST (VOIDmode, label_r,
8430 842 : nonlocal_goto_handler_labels);
8431 : /* ??? Do not let expand_label treat us as such since we would
8432 : not want to be both on the list of non-local labels and on
8433 : the list of forced labels. */
8434 842 : FORCED_LABEL (label) = 0;
8435 842 : return const0_rtx;
8436 : }
8437 : break;
8438 :
8439 : /* __builtin_longjmp is passed a pointer to an array of five words.
8440 : It's similar to the C library longjmp function but works with
8441 : __builtin_setjmp above. */
8442 392 : case BUILT_IN_LONGJMP:
8443 392 : if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8444 : {
8445 392 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8446 : VOIDmode, EXPAND_NORMAL);
8447 392 : rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
8448 :
8449 392 : if (value != const1_rtx)
8450 : {
8451 0 : error ("%<__builtin_longjmp%> second argument must be 1");
8452 0 : return const0_rtx;
8453 : }
8454 :
8455 392 : expand_builtin_longjmp (buf_addr, value);
8456 392 : return const0_rtx;
8457 : }
8458 : break;
8459 :
8460 525 : case BUILT_IN_NONLOCAL_GOTO:
8461 525 : target = expand_builtin_nonlocal_goto (exp);
8462 525 : if (target)
8463 : return target;
8464 : break;
8465 :
8466 : /* This updates the setjmp buffer that is its argument with the value
8467 : of the current stack pointer. */
8468 0 : case BUILT_IN_UPDATE_SETJMP_BUF:
8469 0 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8470 : {
8471 0 : rtx buf_addr
8472 0 : = expand_normal (CALL_EXPR_ARG (exp, 0));
8473 :
8474 0 : expand_builtin_update_setjmp_buf (buf_addr);
8475 0 : return const0_rtx;
8476 : }
8477 : break;
8478 :
8479 38956 : case BUILT_IN_TRAP:
8480 38956 : case BUILT_IN_UNREACHABLE_TRAP:
8481 38956 : expand_builtin_trap ();
8482 38956 : return const0_rtx;
8483 :
8484 2671 : case BUILT_IN_UNREACHABLE:
8485 2671 : expand_builtin_unreachable ();
8486 2671 : return const0_rtx;
8487 :
8488 423 : case BUILT_IN_OBSERVABLE_CHKPT:
8489 : /* Generate no code. */
8490 423 : return const0_rtx;
8491 :
8492 1375 : CASE_FLT_FN (BUILT_IN_SIGNBIT):
8493 1375 : case BUILT_IN_SIGNBITD32:
8494 1375 : case BUILT_IN_SIGNBITD64:
8495 1375 : case BUILT_IN_SIGNBITD128:
8496 1375 : target = expand_builtin_signbit (exp, target);
8497 1375 : if (target)
8498 : return target;
8499 : break;
8500 :
8501 : /* Various hooks for the DWARF 2 __throw routine. */
8502 34 : case BUILT_IN_UNWIND_INIT:
8503 34 : expand_builtin_unwind_init ();
8504 34 : return const0_rtx;
8505 1305 : case BUILT_IN_DWARF_CFA:
8506 1305 : return virtual_cfa_rtx;
8507 : #ifdef DWARF2_UNWIND_INFO
8508 48 : case BUILT_IN_DWARF_SP_COLUMN:
8509 48 : return expand_builtin_dwarf_sp_column ();
8510 8 : case BUILT_IN_INIT_DWARF_REG_SIZES:
8511 8 : expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
8512 8 : return const0_rtx;
8513 : #endif
8514 19 : case BUILT_IN_FROB_RETURN_ADDR:
8515 19 : return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
8516 2352 : case BUILT_IN_EXTRACT_RETURN_ADDR:
8517 2352 : return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
8518 31 : case BUILT_IN_EH_RETURN:
8519 62 : expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
8520 31 : CALL_EXPR_ARG (exp, 1));
8521 31 : return const0_rtx;
8522 21 : case BUILT_IN_EH_RETURN_DATA_REGNO:
8523 21 : return expand_builtin_eh_return_data_regno (exp);
8524 2 : case BUILT_IN_EXTEND_POINTER:
8525 2 : return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
8526 103206 : case BUILT_IN_EH_POINTER:
8527 103206 : return expand_builtin_eh_pointer (exp);
8528 7146 : case BUILT_IN_EH_FILTER:
8529 7146 : return expand_builtin_eh_filter (exp);
8530 103318 : case BUILT_IN_EH_COPY_VALUES:
8531 103318 : return expand_builtin_eh_copy_values (exp);
8532 :
8533 21118 : case BUILT_IN_VA_START:
8534 21118 : return expand_builtin_va_start (exp);
8535 12206 : case BUILT_IN_VA_END:
8536 12206 : return expand_builtin_va_end (exp);
8537 248 : case BUILT_IN_VA_COPY:
8538 248 : return expand_builtin_va_copy (exp);
8539 1407 : case BUILT_IN_EXPECT:
8540 1407 : return expand_builtin_expect (exp, target);
8541 5 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
8542 5 : return expand_builtin_expect_with_probability (exp, target);
8543 662 : case BUILT_IN_ASSUME_ALIGNED:
8544 662 : return expand_builtin_assume_aligned (exp, target);
8545 2061 : case BUILT_IN_PREFETCH:
8546 2061 : expand_builtin_prefetch (exp);
8547 2061 : return const0_rtx;
8548 :
8549 309 : case BUILT_IN_INIT_TRAMPOLINE:
8550 309 : return expand_builtin_init_trampoline (exp, true);
8551 0 : case BUILT_IN_INIT_HEAP_TRAMPOLINE:
8552 0 : return expand_builtin_init_trampoline (exp, false);
8553 357 : case BUILT_IN_ADJUST_TRAMPOLINE:
8554 357 : return expand_builtin_adjust_trampoline (exp);
8555 :
8556 0 : case BUILT_IN_INIT_DESCRIPTOR:
8557 0 : return expand_builtin_init_descriptor (exp);
8558 0 : case BUILT_IN_ADJUST_DESCRIPTOR:
8559 0 : return expand_builtin_adjust_descriptor (exp);
8560 :
8561 : case BUILT_IN_GCC_NESTED_PTR_CREATED:
8562 : case BUILT_IN_GCC_NESTED_PTR_DELETED:
8563 : break; /* At present, no expansion, just call the function. */
8564 :
8565 112 : case BUILT_IN_FORK:
8566 112 : case BUILT_IN_EXECL:
8567 112 : case BUILT_IN_EXECV:
8568 112 : case BUILT_IN_EXECLP:
8569 112 : case BUILT_IN_EXECLE:
8570 112 : case BUILT_IN_EXECVP:
8571 112 : case BUILT_IN_EXECVE:
8572 112 : target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
8573 112 : if (target)
8574 : return target;
8575 : break;
8576 :
8577 712 : case BUILT_IN_SYNC_FETCH_AND_ADD_1:
8578 712 : case BUILT_IN_SYNC_FETCH_AND_ADD_2:
8579 712 : case BUILT_IN_SYNC_FETCH_AND_ADD_4:
8580 712 : case BUILT_IN_SYNC_FETCH_AND_ADD_8:
8581 712 : case BUILT_IN_SYNC_FETCH_AND_ADD_16:
8582 712 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
8583 712 : target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
8584 712 : if (target)
8585 : return target;
8586 : break;
8587 :
8588 585 : case BUILT_IN_SYNC_FETCH_AND_SUB_1:
8589 585 : case BUILT_IN_SYNC_FETCH_AND_SUB_2:
8590 585 : case BUILT_IN_SYNC_FETCH_AND_SUB_4:
8591 585 : case BUILT_IN_SYNC_FETCH_AND_SUB_8:
8592 585 : case BUILT_IN_SYNC_FETCH_AND_SUB_16:
8593 585 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
8594 585 : target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
8595 585 : if (target)
8596 : return target;
8597 : break;
8598 :
8599 512 : case BUILT_IN_SYNC_FETCH_AND_OR_1:
8600 512 : case BUILT_IN_SYNC_FETCH_AND_OR_2:
8601 512 : case BUILT_IN_SYNC_FETCH_AND_OR_4:
8602 512 : case BUILT_IN_SYNC_FETCH_AND_OR_8:
8603 512 : case BUILT_IN_SYNC_FETCH_AND_OR_16:
8604 512 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8605 512 : target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8606 512 : if (target)
8607 : return target;
8608 : break;
8609 :
8610 488 : case BUILT_IN_SYNC_FETCH_AND_AND_1:
8611 488 : case BUILT_IN_SYNC_FETCH_AND_AND_2:
8612 488 : case BUILT_IN_SYNC_FETCH_AND_AND_4:
8613 488 : case BUILT_IN_SYNC_FETCH_AND_AND_8:
8614 488 : case BUILT_IN_SYNC_FETCH_AND_AND_16:
8615 488 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8616 488 : target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8617 488 : if (target)
8618 : return target;
8619 : break;
8620 :
8621 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8622 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8623 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8624 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8625 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8626 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8627 581 : target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8628 581 : if (target)
8629 : return target;
8630 : break;
8631 :
8632 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8633 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8634 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8635 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8636 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8637 434 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8638 434 : target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8639 434 : if (target)
8640 : return target;
8641 : break;
8642 :
8643 161 : case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8644 161 : case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8645 161 : case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8646 161 : case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8647 161 : case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8648 161 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8649 161 : target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8650 161 : if (target)
8651 : return target;
8652 : break;
8653 :
8654 121 : case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8655 121 : case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8656 121 : case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8657 121 : case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8658 121 : case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8659 121 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8660 121 : target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8661 121 : if (target)
8662 : return target;
8663 : break;
8664 :
8665 115 : case BUILT_IN_SYNC_OR_AND_FETCH_1:
8666 115 : case BUILT_IN_SYNC_OR_AND_FETCH_2:
8667 115 : case BUILT_IN_SYNC_OR_AND_FETCH_4:
8668 115 : case BUILT_IN_SYNC_OR_AND_FETCH_8:
8669 115 : case BUILT_IN_SYNC_OR_AND_FETCH_16:
8670 115 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8671 115 : target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8672 115 : if (target)
8673 : return target;
8674 : break;
8675 :
8676 116 : case BUILT_IN_SYNC_AND_AND_FETCH_1:
8677 116 : case BUILT_IN_SYNC_AND_AND_FETCH_2:
8678 116 : case BUILT_IN_SYNC_AND_AND_FETCH_4:
8679 116 : case BUILT_IN_SYNC_AND_AND_FETCH_8:
8680 116 : case BUILT_IN_SYNC_AND_AND_FETCH_16:
8681 116 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8682 116 : target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8683 116 : if (target)
8684 : return target;
8685 : break;
8686 :
8687 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8688 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8689 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8690 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8691 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8692 114 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8693 114 : target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8694 114 : if (target)
8695 : return target;
8696 : break;
8697 :
8698 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8699 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8700 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8701 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8702 89 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8703 89 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8704 89 : target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8705 89 : if (target)
8706 : return target;
8707 : break;
8708 :
8709 205 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8710 205 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8711 205 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8712 205 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8713 205 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8714 205 : if (mode == VOIDmode)
8715 41 : mode = TYPE_MODE (boolean_type_node);
8716 205 : if (!target || !register_operand (target, mode))
8717 41 : target = gen_reg_rtx (mode);
8718 :
8719 205 : mode = get_builtin_sync_mode
8720 205 : (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8721 205 : target = expand_builtin_compare_and_swap (mode, exp, true, target);
8722 205 : if (target)
8723 : return target;
8724 : break;
8725 :
8726 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8727 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8728 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8729 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8730 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8731 256 : mode = get_builtin_sync_mode
8732 256 : (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8733 256 : target = expand_builtin_compare_and_swap (mode, exp, false, target);
8734 256 : if (target)
8735 : return target;
8736 : break;
8737 :
8738 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8739 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8740 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8741 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8742 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8743 326 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8744 326 : target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8745 326 : if (target)
8746 : return target;
8747 : break;
8748 :
8749 146 : case BUILT_IN_SYNC_LOCK_RELEASE_1:
8750 146 : case BUILT_IN_SYNC_LOCK_RELEASE_2:
8751 146 : case BUILT_IN_SYNC_LOCK_RELEASE_4:
8752 146 : case BUILT_IN_SYNC_LOCK_RELEASE_8:
8753 146 : case BUILT_IN_SYNC_LOCK_RELEASE_16:
8754 146 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8755 146 : if (expand_builtin_sync_lock_release (mode, exp))
8756 144 : return const0_rtx;
8757 : break;
8758 :
8759 275 : case BUILT_IN_SYNC_SYNCHRONIZE:
8760 275 : expand_builtin_sync_synchronize ();
8761 275 : return const0_rtx;
8762 :
8763 2910 : case BUILT_IN_ATOMIC_EXCHANGE_1:
8764 2910 : case BUILT_IN_ATOMIC_EXCHANGE_2:
8765 2910 : case BUILT_IN_ATOMIC_EXCHANGE_4:
8766 2910 : case BUILT_IN_ATOMIC_EXCHANGE_8:
8767 2910 : case BUILT_IN_ATOMIC_EXCHANGE_16:
8768 2910 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8769 2910 : target = expand_builtin_atomic_exchange (mode, exp, target);
8770 2910 : if (target)
8771 : return target;
8772 : break;
8773 :
8774 9414 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8775 9414 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8776 9414 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8777 9414 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8778 9414 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8779 9414 : {
8780 9414 : unsigned int nargs, z;
8781 9414 : vec<tree, va_gc> *vec;
8782 :
8783 9414 : mode =
8784 9414 : get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8785 9414 : target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8786 9414 : if (target)
8787 7614 : return target;
8788 :
8789 : /* If this is turned into an external library call, the weak parameter
8790 : must be dropped to match the expected parameter list. */
8791 1800 : nargs = call_expr_nargs (exp);
8792 1800 : vec_alloc (vec, nargs - 1);
8793 9000 : for (z = 0; z < 3; z++)
8794 5400 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8795 : /* Skip the boolean weak parameter. */
8796 5400 : for (z = 4; z < 6; z++)
8797 3600 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8798 1800 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8799 1800 : break;
8800 : }
8801 :
8802 71650 : case BUILT_IN_ATOMIC_LOAD_1:
8803 71650 : case BUILT_IN_ATOMIC_LOAD_2:
8804 71650 : case BUILT_IN_ATOMIC_LOAD_4:
8805 71650 : case BUILT_IN_ATOMIC_LOAD_8:
8806 71650 : case BUILT_IN_ATOMIC_LOAD_16:
8807 71650 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8808 71650 : target = expand_builtin_atomic_load (mode, exp, target);
8809 71650 : if (target)
8810 : return target;
8811 : break;
8812 :
8813 17558 : case BUILT_IN_ATOMIC_STORE_1:
8814 17558 : case BUILT_IN_ATOMIC_STORE_2:
8815 17558 : case BUILT_IN_ATOMIC_STORE_4:
8816 17558 : case BUILT_IN_ATOMIC_STORE_8:
8817 17558 : case BUILT_IN_ATOMIC_STORE_16:
8818 17558 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8819 17558 : target = expand_builtin_atomic_store (mode, exp);
8820 17558 : if (target)
8821 15926 : return const0_rtx;
8822 : break;
8823 :
8824 4406 : case BUILT_IN_ATOMIC_ADD_FETCH_1:
8825 4406 : case BUILT_IN_ATOMIC_ADD_FETCH_2:
8826 4406 : case BUILT_IN_ATOMIC_ADD_FETCH_4:
8827 4406 : case BUILT_IN_ATOMIC_ADD_FETCH_8:
8828 4406 : case BUILT_IN_ATOMIC_ADD_FETCH_16:
8829 4406 : {
8830 4406 : enum built_in_function lib;
8831 4406 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8832 4406 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8833 : (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8834 4406 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8835 : ignore, lib);
8836 4406 : if (target)
8837 : return target;
8838 : break;
8839 : }
8840 2172 : case BUILT_IN_ATOMIC_SUB_FETCH_1:
8841 2172 : case BUILT_IN_ATOMIC_SUB_FETCH_2:
8842 2172 : case BUILT_IN_ATOMIC_SUB_FETCH_4:
8843 2172 : case BUILT_IN_ATOMIC_SUB_FETCH_8:
8844 2172 : case BUILT_IN_ATOMIC_SUB_FETCH_16:
8845 2172 : {
8846 2172 : enum built_in_function lib;
8847 2172 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8848 2172 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8849 : (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8850 2172 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8851 : ignore, lib);
8852 2172 : if (target)
8853 : return target;
8854 : break;
8855 : }
8856 902 : case BUILT_IN_ATOMIC_AND_FETCH_1:
8857 902 : case BUILT_IN_ATOMIC_AND_FETCH_2:
8858 902 : case BUILT_IN_ATOMIC_AND_FETCH_4:
8859 902 : case BUILT_IN_ATOMIC_AND_FETCH_8:
8860 902 : case BUILT_IN_ATOMIC_AND_FETCH_16:
8861 902 : {
8862 902 : enum built_in_function lib;
8863 902 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8864 902 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8865 : (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8866 902 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8867 : ignore, lib);
8868 902 : if (target)
8869 : return target;
8870 : break;
8871 : }
8872 162 : case BUILT_IN_ATOMIC_NAND_FETCH_1:
8873 162 : case BUILT_IN_ATOMIC_NAND_FETCH_2:
8874 162 : case BUILT_IN_ATOMIC_NAND_FETCH_4:
8875 162 : case BUILT_IN_ATOMIC_NAND_FETCH_8:
8876 162 : case BUILT_IN_ATOMIC_NAND_FETCH_16:
8877 162 : {
8878 162 : enum built_in_function lib;
8879 162 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8880 162 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8881 : (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8882 162 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8883 : ignore, lib);
8884 162 : if (target)
8885 : return target;
8886 : break;
8887 : }
8888 854 : case BUILT_IN_ATOMIC_XOR_FETCH_1:
8889 854 : case BUILT_IN_ATOMIC_XOR_FETCH_2:
8890 854 : case BUILT_IN_ATOMIC_XOR_FETCH_4:
8891 854 : case BUILT_IN_ATOMIC_XOR_FETCH_8:
8892 854 : case BUILT_IN_ATOMIC_XOR_FETCH_16:
8893 854 : {
8894 854 : enum built_in_function lib;
8895 854 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8896 854 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8897 : (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8898 854 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8899 : ignore, lib);
8900 854 : if (target)
8901 : return target;
8902 : break;
8903 : }
8904 965 : case BUILT_IN_ATOMIC_OR_FETCH_1:
8905 965 : case BUILT_IN_ATOMIC_OR_FETCH_2:
8906 965 : case BUILT_IN_ATOMIC_OR_FETCH_4:
8907 965 : case BUILT_IN_ATOMIC_OR_FETCH_8:
8908 965 : case BUILT_IN_ATOMIC_OR_FETCH_16:
8909 965 : {
8910 965 : enum built_in_function lib;
8911 965 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8912 965 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8913 : (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8914 965 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8915 : ignore, lib);
8916 965 : if (target)
8917 : return target;
8918 : break;
8919 : }
8920 14764 : case BUILT_IN_ATOMIC_FETCH_ADD_1:
8921 14764 : case BUILT_IN_ATOMIC_FETCH_ADD_2:
8922 14764 : case BUILT_IN_ATOMIC_FETCH_ADD_4:
8923 14764 : case BUILT_IN_ATOMIC_FETCH_ADD_8:
8924 14764 : case BUILT_IN_ATOMIC_FETCH_ADD_16:
8925 14764 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8926 14764 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8927 : ignore, BUILT_IN_NONE);
8928 14764 : if (target)
8929 : return target;
8930 : break;
8931 :
8932 2125 : case BUILT_IN_ATOMIC_FETCH_SUB_1:
8933 2125 : case BUILT_IN_ATOMIC_FETCH_SUB_2:
8934 2125 : case BUILT_IN_ATOMIC_FETCH_SUB_4:
8935 2125 : case BUILT_IN_ATOMIC_FETCH_SUB_8:
8936 2125 : case BUILT_IN_ATOMIC_FETCH_SUB_16:
8937 2125 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8938 2125 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8939 : ignore, BUILT_IN_NONE);
8940 2125 : if (target)
8941 : return target;
8942 : break;
8943 :
8944 853 : case BUILT_IN_ATOMIC_FETCH_AND_1:
8945 853 : case BUILT_IN_ATOMIC_FETCH_AND_2:
8946 853 : case BUILT_IN_ATOMIC_FETCH_AND_4:
8947 853 : case BUILT_IN_ATOMIC_FETCH_AND_8:
8948 853 : case BUILT_IN_ATOMIC_FETCH_AND_16:
8949 853 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8950 853 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8951 : ignore, BUILT_IN_NONE);
8952 853 : if (target)
8953 : return target;
8954 : break;
8955 :
8956 165 : case BUILT_IN_ATOMIC_FETCH_NAND_1:
8957 165 : case BUILT_IN_ATOMIC_FETCH_NAND_2:
8958 165 : case BUILT_IN_ATOMIC_FETCH_NAND_4:
8959 165 : case BUILT_IN_ATOMIC_FETCH_NAND_8:
8960 165 : case BUILT_IN_ATOMIC_FETCH_NAND_16:
8961 165 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8962 165 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8963 : ignore, BUILT_IN_NONE);
8964 165 : if (target)
8965 : return target;
8966 : break;
8967 :
8968 932 : case BUILT_IN_ATOMIC_FETCH_XOR_1:
8969 932 : case BUILT_IN_ATOMIC_FETCH_XOR_2:
8970 932 : case BUILT_IN_ATOMIC_FETCH_XOR_4:
8971 932 : case BUILT_IN_ATOMIC_FETCH_XOR_8:
8972 932 : case BUILT_IN_ATOMIC_FETCH_XOR_16:
8973 932 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8974 932 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8975 : ignore, BUILT_IN_NONE);
8976 932 : if (target)
8977 : return target;
8978 : break;
8979 :
8980 1238 : case BUILT_IN_ATOMIC_FETCH_OR_1:
8981 1238 : case BUILT_IN_ATOMIC_FETCH_OR_2:
8982 1238 : case BUILT_IN_ATOMIC_FETCH_OR_4:
8983 1238 : case BUILT_IN_ATOMIC_FETCH_OR_8:
8984 1238 : case BUILT_IN_ATOMIC_FETCH_OR_16:
8985 1238 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8986 1238 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8987 : ignore, BUILT_IN_NONE);
8988 1238 : if (target)
8989 : return target;
8990 : break;
8991 :
8992 266 : case BUILT_IN_ATOMIC_TEST_AND_SET:
8993 266 : target = expand_builtin_atomic_test_and_set (exp, target);
8994 266 : if (target)
8995 : return target;
8996 : break;
8997 :
8998 53 : case BUILT_IN_ATOMIC_CLEAR:
8999 53 : return expand_builtin_atomic_clear (exp);
9000 :
9001 1 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9002 1 : return expand_builtin_atomic_always_lock_free (exp);
9003 :
9004 10 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9005 10 : target = expand_builtin_atomic_is_lock_free (exp);
9006 10 : if (target)
9007 : return target;
9008 : break;
9009 :
9010 719 : case BUILT_IN_ATOMIC_THREAD_FENCE:
9011 719 : expand_builtin_atomic_thread_fence (exp);
9012 719 : return const0_rtx;
9013 :
9014 60 : case BUILT_IN_ATOMIC_SIGNAL_FENCE:
9015 60 : expand_builtin_atomic_signal_fence (exp);
9016 60 : return const0_rtx;
9017 :
9018 620 : case BUILT_IN_OBJECT_SIZE:
9019 620 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9020 620 : return expand_builtin_object_size (exp);
9021 :
9022 837 : case BUILT_IN_MEMCPY_CHK:
9023 837 : case BUILT_IN_MEMPCPY_CHK:
9024 837 : case BUILT_IN_MEMMOVE_CHK:
9025 837 : case BUILT_IN_MEMSET_CHK:
9026 837 : target = expand_builtin_memory_chk (exp, target, mode, fcode);
9027 837 : if (target)
9028 : return target;
9029 : break;
9030 :
9031 1086 : case BUILT_IN_STRCPY_CHK:
9032 1086 : case BUILT_IN_STPCPY_CHK:
9033 1086 : case BUILT_IN_STRNCPY_CHK:
9034 1086 : case BUILT_IN_STPNCPY_CHK:
9035 1086 : case BUILT_IN_STRCAT_CHK:
9036 1086 : case BUILT_IN_STRNCAT_CHK:
9037 1086 : case BUILT_IN_SNPRINTF_CHK:
9038 1086 : case BUILT_IN_VSNPRINTF_CHK:
9039 1086 : maybe_emit_chk_warning (exp, fcode);
9040 1086 : break;
9041 :
9042 1328 : case BUILT_IN_SPRINTF_CHK:
9043 1328 : case BUILT_IN_VSPRINTF_CHK:
9044 1328 : maybe_emit_sprintf_chk_warning (exp, fcode);
9045 1328 : break;
9046 :
9047 3 : case BUILT_IN_THREAD_POINTER:
9048 3 : return expand_builtin_thread_pointer (exp, target);
9049 :
9050 0 : case BUILT_IN_SET_THREAD_POINTER:
9051 0 : expand_builtin_set_thread_pointer (exp);
9052 0 : return const0_rtx;
9053 :
9054 : case BUILT_IN_ACC_ON_DEVICE:
9055 : /* Do library call, if we failed to expand the builtin when
9056 : folding. */
9057 : break;
9058 :
9059 356 : case BUILT_IN_GOACC_PARLEVEL_ID:
9060 356 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
9061 356 : return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
9062 :
9063 4 : case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
9064 4 : return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
9065 :
9066 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_1:
9067 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_2:
9068 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_4:
9069 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_8:
9070 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_16:
9071 31 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
9072 31 : return expand_speculation_safe_value (mode, exp, target, ignore);
9073 :
9074 2 : case BUILT_IN_CRC8_DATA8:
9075 2 : return expand_builtin_crc_table_based (IFN_CRC, QImode, QImode, mode,
9076 : exp, target);
9077 2 : case BUILT_IN_CRC16_DATA8:
9078 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, QImode, mode,
9079 : exp, target);
9080 3 : case BUILT_IN_CRC16_DATA16:
9081 3 : return expand_builtin_crc_table_based (IFN_CRC, HImode, HImode, mode,
9082 : exp, target);
9083 2 : case BUILT_IN_CRC32_DATA8:
9084 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, QImode, mode,
9085 : exp, target);
9086 2 : case BUILT_IN_CRC32_DATA16:
9087 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, HImode, mode,
9088 : exp, target);
9089 14 : case BUILT_IN_CRC32_DATA32:
9090 14 : return expand_builtin_crc_table_based (IFN_CRC, SImode, SImode, mode,
9091 : exp, target);
9092 1 : case BUILT_IN_CRC64_DATA8:
9093 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, QImode, mode,
9094 : exp, target);
9095 1 : case BUILT_IN_CRC64_DATA16:
9096 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, HImode, mode,
9097 : exp, target);
9098 1 : case BUILT_IN_CRC64_DATA32:
9099 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, SImode, mode,
9100 : exp, target);
9101 1 : case BUILT_IN_CRC64_DATA64:
9102 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, DImode, mode,
9103 : exp, target);
9104 2 : case BUILT_IN_REV_CRC8_DATA8:
9105 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, QImode, QImode,
9106 : mode, exp, target);
9107 2 : case BUILT_IN_REV_CRC16_DATA8:
9108 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, QImode,
9109 : mode, exp, target);
9110 2 : case BUILT_IN_REV_CRC16_DATA16:
9111 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, HImode,
9112 : mode, exp, target);
9113 3 : case BUILT_IN_REV_CRC32_DATA8:
9114 3 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, QImode,
9115 : mode, exp, target);
9116 2 : case BUILT_IN_REV_CRC32_DATA16:
9117 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, HImode,
9118 : mode, exp, target);
9119 2 : case BUILT_IN_REV_CRC32_DATA32:
9120 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, SImode,
9121 : mode, exp, target);
9122 1 : case BUILT_IN_REV_CRC64_DATA8:
9123 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, QImode,
9124 : mode, exp, target);
9125 1 : case BUILT_IN_REV_CRC64_DATA16:
9126 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, HImode,
9127 : mode, exp, target);
9128 1 : case BUILT_IN_REV_CRC64_DATA32:
9129 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, SImode,
9130 : mode, exp, target);
9131 1 : case BUILT_IN_REV_CRC64_DATA64:
9132 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, DImode,
9133 : mode, exp, target);
9134 : default: /* just do library call, if unknown builtin */
9135 : break;
9136 : }
9137 :
9138 : /* The switch statement above can drop through to cause the function
9139 : to be called normally. */
9140 879910 : return expand_call (exp, target, ignore);
9141 : }
9142 :
9143 : /* Determine whether a tree node represents a call to a built-in
9144 : function. If the tree T is a call to a built-in function with
9145 : the right number of arguments of the appropriate types, return
9146 : the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
9147 : Otherwise the return value is END_BUILTINS. */
9148 :
9149 : enum built_in_function
9150 108101798 : builtin_mathfn_code (const_tree t)
9151 : {
9152 108101798 : const_tree fndecl, arg, parmlist;
9153 108101798 : const_tree argtype, parmtype;
9154 108101798 : const_call_expr_arg_iterator iter;
9155 :
9156 108101798 : if (TREE_CODE (t) != CALL_EXPR)
9157 : return END_BUILTINS;
9158 :
9159 1454186 : fndecl = get_callee_fndecl (t);
9160 1454186 : if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9161 : return END_BUILTINS;
9162 :
9163 660675 : parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
9164 660675 : init_const_call_expr_arg_iterator (t, &iter);
9165 2133738 : for (; parmlist; parmlist = TREE_CHAIN (parmlist))
9166 : {
9167 : /* If a function doesn't take a variable number of arguments,
9168 : the last element in the list will have type `void'. */
9169 1453612 : parmtype = TREE_VALUE (parmlist);
9170 1453612 : if (VOID_TYPE_P (parmtype))
9171 : {
9172 641210 : if (more_const_call_expr_args_p (&iter))
9173 : return END_BUILTINS;
9174 641210 : return DECL_FUNCTION_CODE (fndecl);
9175 : }
9176 :
9177 812402 : if (! more_const_call_expr_args_p (&iter))
9178 : return END_BUILTINS;
9179 :
9180 812402 : arg = next_const_call_expr_arg (&iter);
9181 812402 : argtype = TREE_TYPE (arg);
9182 :
9183 812402 : if (SCALAR_FLOAT_TYPE_P (parmtype))
9184 : {
9185 551338 : if (! SCALAR_FLOAT_TYPE_P (argtype))
9186 : return END_BUILTINS;
9187 : }
9188 261064 : else if (COMPLEX_FLOAT_TYPE_P (parmtype))
9189 : {
9190 7865 : if (! COMPLEX_FLOAT_TYPE_P (argtype))
9191 : return END_BUILTINS;
9192 : }
9193 253199 : else if (POINTER_TYPE_P (parmtype))
9194 : {
9195 75553 : if (! POINTER_TYPE_P (argtype))
9196 : return END_BUILTINS;
9197 : }
9198 177646 : else if (INTEGRAL_TYPE_P (parmtype))
9199 : {
9200 177646 : if (! INTEGRAL_TYPE_P (argtype))
9201 : return END_BUILTINS;
9202 : }
9203 : else
9204 : return END_BUILTINS;
9205 : }
9206 :
9207 : /* Variable-length argument list. */
9208 19451 : return DECL_FUNCTION_CODE (fndecl);
9209 : }
9210 :
9211 : /* Fold a call to __builtin_constant_p, if we know its argument ARG will
9212 : evaluate to a constant. */
9213 :
9214 : tree
9215 3259338 : fold_builtin_constant_p (tree arg)
9216 : {
9217 : /* We return 1 for a numeric type that's known to be a constant
9218 : value at compile-time or for an aggregate type that's a
9219 : literal constant. */
9220 3259338 : STRIP_NOPS (arg);
9221 :
9222 : /* If we know this is a constant, emit the constant of one. */
9223 3259338 : if (CONSTANT_CLASS_P (arg)
9224 3259338 : || (TREE_CODE (arg) == CONSTRUCTOR
9225 6 : && TREE_CONSTANT (arg)))
9226 1835743 : return integer_one_node;
9227 1423595 : if (TREE_CODE (arg) == ADDR_EXPR)
9228 : {
9229 110 : tree op = TREE_OPERAND (arg, 0);
9230 110 : if (TREE_CODE (op) == STRING_CST
9231 110 : || (TREE_CODE (op) == ARRAY_REF
9232 57 : && integer_zerop (TREE_OPERAND (op, 1))
9233 57 : && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
9234 87 : return integer_one_node;
9235 : }
9236 :
9237 : /* If this expression has side effects, show we don't know it to be a
9238 : constant. Likewise if it's a pointer or aggregate type since in
9239 : those case we only want literals, since those are only optimized
9240 : when generating RTL, not later.
9241 : And finally, if we are compiling an initializer, not code, we
9242 : need to return a definite result now; there's not going to be any
9243 : more optimization done. */
9244 1423508 : if (TREE_SIDE_EFFECTS (arg)
9245 1423383 : || AGGREGATE_TYPE_P (TREE_TYPE (arg))
9246 1423377 : || POINTER_TYPE_P (TREE_TYPE (arg))
9247 1423206 : || cfun == 0
9248 1423113 : || folding_initializer
9249 2846614 : || force_folding_builtin_constant_p)
9250 497 : return integer_zero_node;
9251 :
9252 : return NULL_TREE;
9253 : }
9254 :
9255 : /* Create builtin_expect or builtin_expect_with_probability
9256 : with PRED and EXPECTED as its arguments and return it as a truthvalue.
9257 : Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
9258 : builtin_expect_with_probability instead uses third argument as PROBABILITY
9259 : value. */
9260 :
9261 : static tree
9262 229582 : build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
9263 : tree predictor, tree probability)
9264 : {
9265 229582 : tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
9266 :
9267 229582 : fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
9268 : : BUILT_IN_EXPECT_WITH_PROBABILITY);
9269 229582 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
9270 229582 : ret_type = TREE_TYPE (TREE_TYPE (fn));
9271 229582 : pred_type = TREE_VALUE (arg_types);
9272 229582 : expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
9273 :
9274 229582 : pred = fold_convert_loc (loc, pred_type, pred);
9275 229582 : expected = fold_convert_loc (loc, expected_type, expected);
9276 :
9277 229582 : if (probability)
9278 0 : call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
9279 : else
9280 459164 : call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
9281 : predictor);
9282 :
9283 229582 : return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
9284 229582 : build_int_cst (ret_type, 0));
9285 : }
9286 :
9287 : /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
9288 : NULL_TREE if no simplification is possible. */
9289 :
9290 : tree
9291 7371069 : fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
9292 : tree arg3)
9293 : {
9294 7371069 : tree inner, fndecl, inner_arg0;
9295 7371069 : enum tree_code code;
9296 :
9297 : /* Distribute the expected value over short-circuiting operators.
9298 : See through the cast from truthvalue_type_node to long. */
9299 7371069 : inner_arg0 = arg0;
9300 15057935 : while (CONVERT_EXPR_P (inner_arg0)
9301 1080606 : && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
9302 9532280 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
9303 1080605 : inner_arg0 = TREE_OPERAND (inner_arg0, 0);
9304 :
9305 : /* If this is a builtin_expect within a builtin_expect keep the
9306 : inner one. See through a comparison against a constant. It
9307 : might have been added to create a thruthvalue. */
9308 7371069 : inner = inner_arg0;
9309 :
9310 7371069 : if (COMPARISON_CLASS_P (inner)
9311 7371069 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
9312 1417528 : inner = TREE_OPERAND (inner, 0);
9313 :
9314 7371069 : if (TREE_CODE (inner) == CALL_EXPR
9315 175471 : && (fndecl = get_callee_fndecl (inner))
9316 7546540 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
9317 : BUILT_IN_EXPECT_WITH_PROBABILITY))
9318 : return arg0;
9319 :
9320 7371061 : inner = inner_arg0;
9321 7371061 : code = TREE_CODE (inner);
9322 7371061 : if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
9323 : {
9324 114791 : tree op0 = TREE_OPERAND (inner, 0);
9325 114791 : tree op1 = TREE_OPERAND (inner, 1);
9326 114791 : arg1 = save_expr (arg1);
9327 :
9328 114791 : op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
9329 114791 : op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
9330 114791 : inner = build2 (code, TREE_TYPE (inner), op0, op1);
9331 :
9332 114791 : return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
9333 : }
9334 :
9335 : /* If the argument isn't invariant then there's nothing else we can do. */
9336 7256270 : if (!TREE_CONSTANT (inner_arg0))
9337 : return NULL_TREE;
9338 :
9339 : /* If we expect that a comparison against the argument will fold to
9340 : a constant return the constant. In practice, this means a true
9341 : constant or the address of a non-weak symbol. */
9342 254650 : inner = inner_arg0;
9343 254650 : STRIP_NOPS (inner);
9344 254650 : if (TREE_CODE (inner) == ADDR_EXPR)
9345 : {
9346 1 : do
9347 : {
9348 1 : inner = TREE_OPERAND (inner, 0);
9349 : }
9350 1 : while (TREE_CODE (inner) == COMPONENT_REF
9351 1 : || TREE_CODE (inner) == ARRAY_REF);
9352 1 : if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
9353 0 : return NULL_TREE;
9354 : }
9355 :
9356 : /* Otherwise, ARG0 already has the proper type for the return value. */
9357 : return arg0;
9358 : }
9359 :
9360 : /* Fold a call to __builtin_classify_type with argument ARG. */
9361 :
9362 : static tree
9363 2096 : fold_builtin_classify_type (tree arg)
9364 : {
9365 2096 : if (arg == 0)
9366 0 : return build_int_cst (integer_type_node, no_type_class);
9367 :
9368 2096 : return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
9369 : }
9370 :
9371 : /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
9372 : ARG. */
9373 :
9374 : static tree
9375 461603 : fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
9376 : {
9377 461603 : if (!validate_arg (arg, POINTER_TYPE))
9378 : return NULL_TREE;
9379 : else
9380 : {
9381 461600 : c_strlen_data lendata = { };
9382 461600 : tree len = c_strlen (arg, 0, &lendata);
9383 :
9384 461600 : if (len)
9385 2223 : return fold_convert_loc (loc, type, len);
9386 :
9387 : /* TODO: Move this to gimple-ssa-warn-access once the pass runs
9388 : also early enough to detect invalid reads in multimensional
9389 : arrays and struct members. */
9390 459377 : if (!lendata.decl)
9391 452199 : c_strlen (arg, 1, &lendata);
9392 :
9393 459377 : if (lendata.decl)
9394 : {
9395 7192 : if (EXPR_HAS_LOCATION (arg))
9396 2800 : loc = EXPR_LOCATION (arg);
9397 4392 : else if (loc == UNKNOWN_LOCATION)
9398 0 : loc = input_location;
9399 7192 : warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
9400 : }
9401 :
9402 : return NULL_TREE;
9403 : }
9404 : }
9405 :
9406 : /* Fold a call to __builtin_inf or __builtin_huge_val. */
9407 :
9408 : static tree
9409 258264 : fold_builtin_inf (location_t loc, tree type, int warn)
9410 : {
9411 : /* __builtin_inff is intended to be usable to define INFINITY on all
9412 : targets. If an infinity is not available, INFINITY expands "to a
9413 : positive constant of type float that overflows at translation
9414 : time", footnote "In this case, using INFINITY will violate the
9415 : constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
9416 : Thus we pedwarn to ensure this constraint violation is
9417 : diagnosed. */
9418 1032247 : if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
9419 0 : pedwarn (loc, 0, "target format does not support infinity");
9420 :
9421 258264 : return build_real (type, dconstinf);
9422 : }
9423 :
9424 : /* Fold function call to builtin sincos, sincosf, or sincosl. Return
9425 : NULL_TREE if no simplification can be made. */
9426 :
9427 : static tree
9428 145 : fold_builtin_sincos (location_t loc,
9429 : tree arg0, tree arg1, tree arg2)
9430 : {
9431 145 : tree type;
9432 145 : tree fndecl, call = NULL_TREE;
9433 :
9434 145 : if (!validate_arg (arg0, REAL_TYPE)
9435 145 : || !validate_arg (arg1, POINTER_TYPE)
9436 290 : || !validate_arg (arg2, POINTER_TYPE))
9437 : return NULL_TREE;
9438 :
9439 145 : type = TREE_TYPE (arg0);
9440 :
9441 : /* Calculate the result when the argument is a constant. */
9442 145 : built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
9443 145 : if (fn == END_BUILTINS)
9444 : return NULL_TREE;
9445 :
9446 : /* Canonicalize sincos to cexpi. */
9447 145 : if (TREE_CODE (arg0) == REAL_CST)
9448 : {
9449 86 : tree complex_type = build_complex_type (type);
9450 86 : call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
9451 : }
9452 86 : if (!call)
9453 : {
9454 59 : if (!targetm.libc_has_function (function_c99_math_complex, type)
9455 59 : || !builtin_decl_implicit_p (fn))
9456 : return NULL_TREE;
9457 59 : fndecl = builtin_decl_explicit (fn);
9458 59 : call = build_call_expr_loc (loc, fndecl, 1, arg0);
9459 59 : call = builtin_save_expr (call);
9460 : }
9461 :
9462 145 : tree ptype = build_pointer_type (type);
9463 145 : arg1 = fold_convert (ptype, arg1);
9464 145 : arg2 = fold_convert (ptype, arg2);
9465 145 : return build2 (COMPOUND_EXPR, void_type_node,
9466 : build2 (MODIFY_EXPR, void_type_node,
9467 : build_fold_indirect_ref_loc (loc, arg1),
9468 : fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
9469 : build2 (MODIFY_EXPR, void_type_node,
9470 : build_fold_indirect_ref_loc (loc, arg2),
9471 145 : fold_build1_loc (loc, REALPART_EXPR, type, call)));
9472 : }
9473 :
9474 : /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9475 : Return NULL_TREE if no simplification can be made. */
9476 :
9477 : static tree
9478 2551307 : fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9479 : {
9480 2551307 : if (!validate_arg (arg1, POINTER_TYPE)
9481 2551307 : || !validate_arg (arg2, POINTER_TYPE)
9482 5102614 : || !validate_arg (len, INTEGER_TYPE))
9483 : return NULL_TREE;
9484 :
9485 : /* If the LEN parameter is zero, return zero. */
9486 2551307 : if (integer_zerop (len))
9487 0 : return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9488 0 : arg1, arg2);
9489 :
9490 : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9491 2551307 : if (operand_equal_p (arg1, arg2, 0))
9492 705 : return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9493 :
9494 : /* If len parameter is one, return an expression corresponding to
9495 : (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9496 2550602 : if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9497 : {
9498 54763 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9499 54763 : tree cst_uchar_ptr_node
9500 54763 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9501 :
9502 54763 : tree ind1
9503 54763 : = fold_convert_loc (loc, integer_type_node,
9504 : build1 (INDIRECT_REF, cst_uchar_node,
9505 : fold_convert_loc (loc,
9506 : cst_uchar_ptr_node,
9507 : arg1)));
9508 54763 : tree ind2
9509 54763 : = fold_convert_loc (loc, integer_type_node,
9510 : build1 (INDIRECT_REF, cst_uchar_node,
9511 : fold_convert_loc (loc,
9512 : cst_uchar_ptr_node,
9513 : arg2)));
9514 54763 : return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9515 : }
9516 :
9517 : return NULL_TREE;
9518 : }
9519 :
9520 : /* Fold a call to builtin isascii with argument ARG. */
9521 :
9522 : static tree
9523 211 : fold_builtin_isascii (location_t loc, tree arg)
9524 : {
9525 211 : if (!validate_arg (arg, INTEGER_TYPE))
9526 : return NULL_TREE;
9527 : else
9528 : {
9529 : /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9530 211 : arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9531 : build_int_cst (integer_type_node,
9532 : ~ HOST_WIDE_INT_UC (0x7f)));
9533 211 : return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9534 211 : arg, integer_zero_node);
9535 : }
9536 : }
9537 :
9538 : /* Fold a call to builtin toascii with argument ARG. */
9539 :
9540 : static tree
9541 168 : fold_builtin_toascii (location_t loc, tree arg)
9542 : {
9543 168 : if (!validate_arg (arg, INTEGER_TYPE))
9544 : return NULL_TREE;
9545 :
9546 : /* Transform toascii(c) -> (c & 0x7f). */
9547 168 : return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9548 : build_int_cst (integer_type_node, 0x7f));
9549 : }
9550 :
9551 : /* Fold a call to builtin isdigit with argument ARG. */
9552 :
9553 : static tree
9554 325 : fold_builtin_isdigit (location_t loc, tree arg)
9555 : {
9556 325 : if (!validate_arg (arg, INTEGER_TYPE))
9557 : return NULL_TREE;
9558 : else
9559 : {
9560 : /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9561 : /* According to the C standard, isdigit is unaffected by locale.
9562 : However, it definitely is affected by the target character set. */
9563 313 : unsigned HOST_WIDE_INT target_digit0
9564 313 : = lang_hooks.to_target_charset ('0');
9565 :
9566 313 : if (target_digit0 == 0)
9567 : return NULL_TREE;
9568 :
9569 313 : arg = fold_convert_loc (loc, unsigned_type_node, arg);
9570 313 : arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9571 : build_int_cst (unsigned_type_node, target_digit0));
9572 313 : return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9573 : build_int_cst (unsigned_type_node, 9));
9574 : }
9575 : }
9576 :
9577 : /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9578 :
9579 : static tree
9580 374110 : fold_builtin_fabs (location_t loc, tree arg, tree type)
9581 : {
9582 374110 : if (!validate_arg (arg, REAL_TYPE))
9583 : return NULL_TREE;
9584 :
9585 374025 : arg = fold_convert_loc (loc, type, arg);
9586 374025 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9587 : }
9588 :
9589 : /* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
9590 : with argument ARG. */
9591 :
9592 : static tree
9593 98988 : fold_builtin_abs (location_t loc, tree arg, tree type)
9594 : {
9595 98988 : if (!validate_arg (arg, INTEGER_TYPE) || !INTEGRAL_TYPE_P (type))
9596 : return NULL_TREE;
9597 :
9598 98940 : if (TYPE_UNSIGNED (type))
9599 : {
9600 1168 : if (TYPE_PRECISION (TREE_TYPE (arg))
9601 1168 : != TYPE_PRECISION (type)
9602 1168 : || TYPE_UNSIGNED (TREE_TYPE (arg)))
9603 : return NULL_TREE;
9604 1168 : return fold_build1_loc (loc, ABSU_EXPR, type, arg);
9605 : }
9606 97772 : arg = fold_convert_loc (loc, type, arg);
9607 97772 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9608 : }
9609 :
9610 : /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9611 :
9612 : static tree
9613 104166 : fold_builtin_carg (location_t loc, tree arg, tree type)
9614 : {
9615 104166 : if (validate_arg (arg, COMPLEX_TYPE)
9616 104166 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
9617 : {
9618 104166 : tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9619 :
9620 104166 : if (atan2_fn)
9621 : {
9622 100220 : tree new_arg = builtin_save_expr (arg);
9623 100220 : tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9624 100220 : tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9625 100220 : return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9626 : }
9627 : }
9628 :
9629 : return NULL_TREE;
9630 : }
9631 :
9632 : /* Fold a call to builtin frexp, we can assume the base is 2. */
9633 :
9634 : static tree
9635 116034 : fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9636 : {
9637 116034 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9638 : return NULL_TREE;
9639 :
9640 116034 : STRIP_NOPS (arg0);
9641 :
9642 2275 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))
9643 118309 : || tree_expr_signaling_nan_p (arg0))
9644 : return NULL_TREE;
9645 :
9646 2235 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9647 :
9648 : /* Proceed if a valid pointer type was passed in. */
9649 2235 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9650 : {
9651 2235 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9652 2235 : tree frac, exp, res;
9653 :
9654 2235 : switch (value->cl)
9655 : {
9656 279 : case rvc_zero:
9657 279 : case rvc_nan:
9658 279 : case rvc_inf:
9659 : /* For +-0, return (*exp = 0, +-0). */
9660 : /* For +-NaN or +-Inf, *exp is unspecified, but something should
9661 : be stored there so that it isn't read from uninitialized object.
9662 : As glibc and newlib store *exp = 0 for +-Inf/NaN, storing
9663 : 0 here as well is easiest. */
9664 279 : exp = integer_zero_node;
9665 279 : frac = arg0;
9666 279 : break;
9667 1956 : case rvc_normal:
9668 1956 : {
9669 : /* Since the frexp function always expects base 2, and in
9670 : GCC normalized significands are already in the range
9671 : [0.5, 1.0), we have exactly what frexp wants. */
9672 1956 : REAL_VALUE_TYPE frac_rvt = *value;
9673 1956 : SET_REAL_EXP (&frac_rvt, 0);
9674 1956 : frac = build_real (rettype, frac_rvt);
9675 1956 : exp = build_int_cst (integer_type_node, REAL_EXP (value));
9676 : }
9677 1956 : break;
9678 0 : default:
9679 0 : gcc_unreachable ();
9680 : }
9681 :
9682 : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9683 2235 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9684 2235 : TREE_SIDE_EFFECTS (arg1) = 1;
9685 2235 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9686 2235 : suppress_warning (res, OPT_Wunused_value);
9687 2235 : return res;
9688 : }
9689 :
9690 : return NULL_TREE;
9691 : }
9692 :
9693 : /* Fold a call to builtin modf. */
9694 :
9695 : static tree
9696 92567 : fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9697 : {
9698 92567 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9699 : return NULL_TREE;
9700 :
9701 92567 : STRIP_NOPS (arg0);
9702 :
9703 2515 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))
9704 95082 : || tree_expr_signaling_nan_p (arg0))
9705 : return NULL_TREE;
9706 :
9707 2475 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9708 :
9709 : /* Proceed if a valid pointer type was passed in. */
9710 2475 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9711 : {
9712 2475 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9713 2475 : REAL_VALUE_TYPE trunc, frac;
9714 2475 : tree res;
9715 :
9716 2475 : switch (value->cl)
9717 : {
9718 229 : case rvc_nan:
9719 229 : case rvc_zero:
9720 : /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9721 229 : trunc = frac = *value;
9722 229 : break;
9723 96 : case rvc_inf:
9724 : /* For +-Inf, return (*arg1 = arg0, +-0). */
9725 96 : frac = dconst0;
9726 96 : frac.sign = value->sign;
9727 96 : trunc = *value;
9728 96 : break;
9729 2150 : case rvc_normal:
9730 : /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9731 2150 : real_trunc (&trunc, VOIDmode, value);
9732 2150 : real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9733 : /* If the original number was negative and already
9734 : integral, then the fractional part is -0.0. */
9735 2150 : if (value->sign && frac.cl == rvc_zero)
9736 69 : frac.sign = value->sign;
9737 : break;
9738 : }
9739 :
9740 : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9741 2475 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9742 : build_real (rettype, trunc));
9743 2475 : TREE_SIDE_EFFECTS (arg1) = 1;
9744 2475 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9745 : build_real (rettype, frac));
9746 2475 : suppress_warning (res, OPT_Wunused_value);
9747 2475 : return res;
9748 : }
9749 :
9750 : return NULL_TREE;
9751 : }
9752 :
9753 : /* Given a location LOC, an interclass builtin function decl FNDECL
9754 : and its single argument ARG, return an folded expression computing
9755 : the same, or NULL_TREE if we either couldn't or didn't want to fold
9756 : (the latter happen if there's an RTL instruction available). */
9757 :
9758 : static tree
9759 1818138 : fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9760 : {
9761 1818138 : machine_mode mode;
9762 :
9763 1818138 : if (!validate_arg (arg, REAL_TYPE))
9764 : return NULL_TREE;
9765 :
9766 1818138 : if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9767 : return NULL_TREE;
9768 :
9769 1818138 : mode = TYPE_MODE (TREE_TYPE (arg));
9770 :
9771 12723124 : bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9772 :
9773 : /* If there is no optab, try generic code. */
9774 1818138 : switch (DECL_FUNCTION_CODE (fndecl))
9775 : {
9776 704982 : tree result;
9777 :
9778 704982 : CASE_FLT_FN (BUILT_IN_ISINF):
9779 704982 : {
9780 : /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9781 704982 : tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9782 704982 : tree type = TREE_TYPE (arg);
9783 704982 : REAL_VALUE_TYPE r;
9784 704982 : char buf[128];
9785 :
9786 704982 : if (is_ibm_extended)
9787 : {
9788 : /* NaN and Inf are encoded in the high-order double value
9789 : only. The low-order value is not significant. */
9790 0 : type = double_type_node;
9791 0 : mode = DFmode;
9792 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9793 : }
9794 704982 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9795 704982 : real_from_string3 (&r, buf, mode);
9796 704982 : result = build_call_expr (isgr_fn, 2,
9797 : fold_build1_loc (loc, ABS_EXPR, type, arg),
9798 : build_real (type, r));
9799 704982 : return result;
9800 : }
9801 630205 : CASE_FLT_FN (BUILT_IN_FINITE):
9802 630205 : case BUILT_IN_ISFINITE:
9803 630205 : {
9804 : /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9805 630205 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9806 630205 : tree type = TREE_TYPE (arg);
9807 630205 : REAL_VALUE_TYPE r;
9808 630205 : char buf[128];
9809 :
9810 630205 : if (is_ibm_extended)
9811 : {
9812 : /* NaN and Inf are encoded in the high-order double value
9813 : only. The low-order value is not significant. */
9814 0 : type = double_type_node;
9815 0 : mode = DFmode;
9816 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9817 : }
9818 630205 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9819 630205 : real_from_string3 (&r, buf, mode);
9820 630205 : result = build_call_expr (isle_fn, 2,
9821 : fold_build1_loc (loc, ABS_EXPR, type, arg),
9822 : build_real (type, r));
9823 : /*result = fold_build2_loc (loc, UNGT_EXPR,
9824 : TREE_TYPE (TREE_TYPE (fndecl)),
9825 : fold_build1_loc (loc, ABS_EXPR, type, arg),
9826 : build_real (type, r));
9827 : result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9828 : TREE_TYPE (TREE_TYPE (fndecl)),
9829 : result);*/
9830 630205 : return result;
9831 : }
9832 239210 : case BUILT_IN_ISNORMAL:
9833 239210 : {
9834 : /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9835 : islessequal(fabs(x),DBL_MAX). */
9836 239210 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9837 239210 : tree type = TREE_TYPE (arg);
9838 239210 : tree orig_arg, max_exp, min_exp;
9839 239210 : machine_mode orig_mode = mode;
9840 239210 : REAL_VALUE_TYPE rmax, rmin;
9841 239210 : char buf[128];
9842 :
9843 239210 : orig_arg = arg = builtin_save_expr (arg);
9844 239210 : if (is_ibm_extended)
9845 : {
9846 : /* Use double to test the normal range of IBM extended
9847 : precision. Emin for IBM extended precision is
9848 : different to emin for IEEE double, being 53 higher
9849 : since the low double exponent is at least 53 lower
9850 : than the high double exponent. */
9851 0 : type = double_type_node;
9852 0 : mode = DFmode;
9853 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9854 : }
9855 239210 : arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9856 :
9857 239210 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9858 239210 : real_from_string3 (&rmax, buf, mode);
9859 239210 : if (DECIMAL_FLOAT_MODE_P (mode))
9860 1 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9861 : else
9862 239209 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9863 239210 : real_from_string3 (&rmin, buf, orig_mode);
9864 239210 : max_exp = build_real (type, rmax);
9865 239210 : min_exp = build_real (type, rmin);
9866 :
9867 239210 : max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9868 239210 : if (is_ibm_extended)
9869 : {
9870 : /* Testing the high end of the range is done just using
9871 : the high double, using the same test as isfinite().
9872 : For the subnormal end of the range we first test the
9873 : high double, then if its magnitude is equal to the
9874 : limit of 0x1p-969, we test whether the low double is
9875 : non-zero and opposite sign to the high double. */
9876 0 : tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9877 0 : tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9878 0 : tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9879 0 : tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9880 : arg, min_exp);
9881 0 : tree as_complex = build1 (VIEW_CONVERT_EXPR,
9882 : complex_double_type_node, orig_arg);
9883 0 : tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9884 0 : tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9885 0 : tree zero = build_real (type, dconst0);
9886 0 : tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9887 0 : tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9888 0 : tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9889 0 : tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9890 : fold_build3 (COND_EXPR,
9891 : integer_type_node,
9892 : hilt, logt, lolt));
9893 0 : eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9894 : eq_min, ok_lo);
9895 0 : min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9896 : gt_min, eq_min);
9897 : }
9898 : else
9899 : {
9900 239210 : tree const isge_fn
9901 239210 : = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9902 239210 : min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9903 : }
9904 239210 : result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9905 : max_exp, min_exp);
9906 239210 : return result;
9907 : }
9908 242207 : CASE_FLT_FN (BUILT_IN_ISNAN):
9909 242207 : case BUILT_IN_ISNAND32:
9910 242207 : case BUILT_IN_ISNAND64:
9911 242207 : case BUILT_IN_ISNAND128:
9912 242207 : {
9913 : /* In IBM extended NaN and Inf are encoded in the high-order double
9914 : value only. The low-order value is not significant. */
9915 242207 : if (is_ibm_extended)
9916 0 : arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9917 242207 : arg = builtin_save_expr (arg);
9918 242207 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
9919 242207 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9920 : }
9921 : default:
9922 : break;
9923 : }
9924 :
9925 : return NULL_TREE;
9926 : }
9927 :
9928 : /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9929 : ARG is the argument for the call. */
9930 :
9931 : static tree
9932 1608222 : fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9933 : {
9934 1608222 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
9935 :
9936 1608222 : if (!validate_arg (arg, REAL_TYPE))
9937 : return NULL_TREE;
9938 :
9939 1608222 : switch (builtin_index)
9940 : {
9941 706767 : case BUILT_IN_ISINF:
9942 706767 : if (tree_expr_infinite_p (arg))
9943 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9944 706767 : if (!tree_expr_maybe_infinite_p (arg))
9945 251 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9946 : return NULL_TREE;
9947 :
9948 708 : case BUILT_IN_ISINF_SIGN:
9949 708 : {
9950 : /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9951 : /* In a boolean context, GCC will fold the inner COND_EXPR to
9952 : 1. So e.g. "if (isinf_sign(x))" would be folded to just
9953 : "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9954 708 : tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9955 708 : tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9956 708 : tree tmp = NULL_TREE;
9957 :
9958 708 : arg = builtin_save_expr (arg);
9959 :
9960 708 : if (signbit_fn && isinf_fn)
9961 : {
9962 708 : tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9963 708 : tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9964 :
9965 708 : signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9966 : signbit_call, integer_zero_node);
9967 708 : isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9968 : isinf_call, integer_zero_node);
9969 :
9970 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9971 : integer_minus_one_node, integer_one_node);
9972 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9973 : isinf_call, tmp,
9974 : integer_zero_node);
9975 : }
9976 :
9977 : return tmp;
9978 : }
9979 :
9980 630537 : case BUILT_IN_ISFINITE:
9981 630537 : if (tree_expr_finite_p (arg))
9982 332 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9983 630205 : if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9984 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9985 : return NULL_TREE;
9986 :
9987 242331 : case BUILT_IN_ISNAN:
9988 242331 : if (tree_expr_nan_p (arg))
9989 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9990 242331 : if (!tree_expr_maybe_nan_p (arg))
9991 124 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9992 : return NULL_TREE;
9993 :
9994 27879 : case BUILT_IN_ISSIGNALING:
9995 : /* Folding to true for REAL_CST is done in fold_const_call_ss.
9996 : Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9997 : and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9998 : here, so there is some possibility of __builtin_issignaling working
9999 : without -fsignaling-nans. Especially when -fno-signaling-nans is
10000 : the default. */
10001 27879 : if (!tree_expr_maybe_nan_p (arg))
10002 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10003 : return NULL_TREE;
10004 :
10005 0 : default:
10006 0 : gcc_unreachable ();
10007 : }
10008 : }
10009 :
10010 : /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10011 : This builtin will generate code to return the appropriate floating
10012 : point classification depending on the value of the floating point
10013 : number passed in. The possible return values must be supplied as
10014 : int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10015 : FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10016 : one floating point argument which is "type generic". */
10017 :
10018 : static tree
10019 104220 : fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
10020 : {
10021 104220 : tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10022 : arg, type, res, tmp;
10023 104220 : machine_mode mode;
10024 104220 : REAL_VALUE_TYPE r;
10025 104220 : char buf[128];
10026 :
10027 : /* Verify the required arguments in the original call. */
10028 104220 : if (nargs != 6
10029 104220 : || !validate_arg (args[0], INTEGER_TYPE)
10030 104220 : || !validate_arg (args[1], INTEGER_TYPE)
10031 104220 : || !validate_arg (args[2], INTEGER_TYPE)
10032 104220 : || !validate_arg (args[3], INTEGER_TYPE)
10033 104220 : || !validate_arg (args[4], INTEGER_TYPE)
10034 208440 : || !validate_arg (args[5], REAL_TYPE))
10035 : return NULL_TREE;
10036 :
10037 104220 : fp_nan = args[0];
10038 104220 : fp_infinite = args[1];
10039 104220 : fp_normal = args[2];
10040 104220 : fp_subnormal = args[3];
10041 104220 : fp_zero = args[4];
10042 104220 : arg = args[5];
10043 104220 : type = TREE_TYPE (arg);
10044 104220 : mode = TYPE_MODE (type);
10045 :
10046 : /* A target defines the classification optabs when FP comparisons are
10047 : unsuitable for classification, for instance because they raise
10048 : FE_INVALID for a signaling NaN (PR middle-end/66462). Build on the
10049 : built-ins in that case rather than on comparisons. */
10050 104220 : if (optab_handler (isnan_optab, mode) != CODE_FOR_nothing
10051 0 : && optab_handler (isinf_optab, mode) != CODE_FOR_nothing
10052 104220 : && optab_handler (isnormal_optab, mode) != CODE_FOR_nothing)
10053 : {
10054 0 : tree isnan_fn = builtin_decl_explicit (BUILT_IN_ISNAN);
10055 0 : tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10056 0 : tree isnormal_fn = builtin_decl_explicit (BUILT_IN_ISNORMAL);
10057 0 : const struct real_format *fmt = REAL_MODE_FORMAT (mode);
10058 0 : tree itype = NULL_TREE;
10059 0 : scalar_int_mode imode;
10060 :
10061 : /* The remaining zero versus subnormal test must not use a comparison
10062 : either. Testing the magnitude of the encoding for zero works
10063 : whenever the format is binary, has a matching integer mode and
10064 : stores its sign in the high bit of it; that excludes e.g. XFmode,
10065 : whose encoding is narrower than its integer mode, and the composite
10066 : formats, whose sign is not a single bit that can be shifted out. */
10067 0 : if (fmt->b == 2
10068 0 : && int_mode_for_mode (mode).exists (&imode)
10069 0 : && fmt->signbit_rw == fmt->signbit_ro
10070 0 : && fmt->signbit_rw == (int) GET_MODE_PRECISION (imode) - 1)
10071 : {
10072 0 : itype = lang_hooks.types.type_for_mode (imode, 1);
10073 0 : if (itype && TYPE_PRECISION (itype) != GET_MODE_PRECISION (imode))
10074 : itype = NULL_TREE;
10075 : }
10076 :
10077 0 : if (isnan_fn && isinf_fn && isnormal_fn && itype)
10078 : {
10079 0 : arg = builtin_save_expr (arg);
10080 :
10081 : /* Shifting the sign bit out leaves zero iff ARG is +-0. */
10082 0 : tmp = fold_build1_loc (loc, VIEW_CONVERT_EXPR, itype, arg);
10083 0 : tmp = fold_build2_loc (loc, LSHIFT_EXPR, itype, tmp,
10084 : build_int_cst (integer_type_node, 1));
10085 0 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, tmp,
10086 : build_int_cst (itype, 0));
10087 0 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10088 : tmp, fp_zero, fp_subnormal);
10089 :
10090 0 : tmp = build_call_expr_loc (loc, isnormal_fn, 1, arg);
10091 0 : tmp = fold_build2_loc (loc, NE_EXPR, integer_type_node, tmp,
10092 : integer_zero_node);
10093 0 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10094 : fp_normal, res);
10095 :
10096 0 : if (tree_expr_maybe_infinite_p (arg))
10097 : {
10098 0 : tmp = build_call_expr_loc (loc, isinf_fn, 1, arg);
10099 0 : tmp = fold_build2_loc (loc, NE_EXPR, integer_type_node, tmp,
10100 : integer_zero_node);
10101 0 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10102 : fp_infinite, res);
10103 : }
10104 :
10105 0 : if (tree_expr_maybe_nan_p (arg))
10106 : {
10107 0 : tmp = build_call_expr_loc (loc, isnan_fn, 1, arg);
10108 0 : tmp = fold_build2_loc (loc, NE_EXPR, integer_type_node, tmp,
10109 : integer_zero_node);
10110 0 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10111 : fp_nan, res);
10112 : }
10113 :
10114 104220 : return res;
10115 : }
10116 : }
10117 :
10118 104220 : arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10119 :
10120 : /* fpclassify(x) ->
10121 : isnan(x) ? FP_NAN :
10122 : (fabs(x) == Inf ? FP_INFINITE :
10123 : (fabs(x) >= DBL_MIN ? FP_NORMAL :
10124 : (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10125 :
10126 104220 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10127 : build_real (type, dconst0));
10128 104220 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10129 : tmp, fp_zero, fp_subnormal);
10130 :
10131 104220 : if (DECIMAL_FLOAT_MODE_P (mode))
10132 3 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (mode)->emin - 1);
10133 : else
10134 104217 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10135 104220 : real_from_string3 (&r, buf, mode);
10136 104220 : tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10137 : arg, build_real (type, r));
10138 104220 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10139 : fp_normal, res);
10140 :
10141 104220 : if (tree_expr_maybe_infinite_p (arg))
10142 : {
10143 104132 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10144 : build_real (type, dconstinf));
10145 104132 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10146 : fp_infinite, res);
10147 : }
10148 :
10149 104220 : if (tree_expr_maybe_nan_p (arg))
10150 : {
10151 104130 : tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10152 104130 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10153 : res, fp_nan);
10154 : }
10155 :
10156 : return res;
10157 : }
10158 :
10159 : /* Fold a call to an unordered comparison function such as
10160 : __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10161 : being called and ARG0 and ARG1 are the arguments for the call.
10162 : UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10163 : the opposite of the desired result. UNORDERED_CODE is used
10164 : for modes that can hold NaNs and ORDERED_CODE is used for
10165 : the rest. */
10166 :
10167 : static tree
10168 3253364 : fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10169 : enum tree_code unordered_code,
10170 : enum tree_code ordered_code)
10171 : {
10172 3253364 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10173 3253364 : enum tree_code code;
10174 3253364 : tree type0, type1;
10175 3253364 : enum tree_code code0, code1;
10176 3253364 : tree cmp_type = NULL_TREE;
10177 :
10178 3253364 : type0 = TREE_TYPE (arg0);
10179 3253364 : type1 = TREE_TYPE (arg1);
10180 :
10181 3253364 : code0 = TREE_CODE (type0);
10182 3253364 : code1 = TREE_CODE (type1);
10183 :
10184 3253364 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10185 : /* Choose the wider of two real types. */
10186 3253184 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10187 3253184 : ? type0 : type1;
10188 180 : else if (code0 == REAL_TYPE
10189 91 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10190 : cmp_type = type0;
10191 89 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10192 89 : && code1 == REAL_TYPE)
10193 135 : cmp_type = type1;
10194 :
10195 3253364 : arg0 = fold_convert_loc (loc, cmp_type, arg0);
10196 3253364 : arg1 = fold_convert_loc (loc, cmp_type, arg1);
10197 :
10198 3253364 : if (unordered_code == UNORDERED_EXPR)
10199 : {
10200 240258 : if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
10201 16 : return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
10202 240242 : if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
10203 141 : return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10204 240101 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10205 : }
10206 :
10207 3015957 : code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
10208 3013106 : ? unordered_code : ordered_code;
10209 3013106 : return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10210 3013106 : fold_build2_loc (loc, code, type, arg0, arg1));
10211 : }
10212 :
10213 : /* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments.
10214 : After choosing the wider floating-point type for the comparison,
10215 : the code is folded to:
10216 : SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1> */
10217 :
10218 : static tree
10219 709 : fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
10220 : {
10221 709 : tree type0, type1;
10222 709 : enum tree_code code0, code1;
10223 709 : tree cmp1, cmp2, cmp_type = NULL_TREE;
10224 :
10225 709 : type0 = TREE_TYPE (arg0);
10226 709 : type1 = TREE_TYPE (arg1);
10227 :
10228 709 : code0 = TREE_CODE (type0);
10229 709 : code1 = TREE_CODE (type1);
10230 :
10231 709 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10232 : /* Choose the wider of two real types. */
10233 690 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10234 690 : ? type0 : type1;
10235 19 : else if (code0 == REAL_TYPE
10236 6 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10237 : cmp_type = type0;
10238 13 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10239 13 : && code1 == REAL_TYPE)
10240 13 : cmp_type = type1;
10241 :
10242 709 : arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
10243 709 : arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
10244 :
10245 709 : cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
10246 709 : cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
10247 :
10248 709 : return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
10249 : }
10250 :
10251 : /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
10252 : arithmetics if it can never overflow, or into internal functions that
10253 : return both result of arithmetics and overflowed boolean flag in
10254 : a complex integer result, or some other check for overflow.
10255 : Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
10256 : checking part of that. */
10257 :
10258 : static tree
10259 502929 : fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
10260 : tree arg0, tree arg1, tree arg2)
10261 : {
10262 502929 : enum internal_fn ifn = IFN_LAST;
10263 : /* The code of the expression corresponding to the built-in. */
10264 502929 : enum tree_code opcode = ERROR_MARK;
10265 502929 : bool ovf_only = false;
10266 :
10267 502929 : switch (fcode)
10268 : {
10269 : case BUILT_IN_ADD_OVERFLOW_P:
10270 : ovf_only = true;
10271 : /* FALLTHRU */
10272 : case BUILT_IN_ADD_OVERFLOW:
10273 : case BUILT_IN_SADD_OVERFLOW:
10274 : case BUILT_IN_SADDL_OVERFLOW:
10275 : case BUILT_IN_SADDLL_OVERFLOW:
10276 : case BUILT_IN_UADD_OVERFLOW:
10277 : case BUILT_IN_UADDL_OVERFLOW:
10278 : case BUILT_IN_UADDLL_OVERFLOW:
10279 : opcode = PLUS_EXPR;
10280 : ifn = IFN_ADD_OVERFLOW;
10281 : break;
10282 15591 : case BUILT_IN_SUB_OVERFLOW_P:
10283 15591 : ovf_only = true;
10284 : /* FALLTHRU */
10285 37650 : case BUILT_IN_SUB_OVERFLOW:
10286 37650 : case BUILT_IN_SSUB_OVERFLOW:
10287 37650 : case BUILT_IN_SSUBL_OVERFLOW:
10288 37650 : case BUILT_IN_SSUBLL_OVERFLOW:
10289 37650 : case BUILT_IN_USUB_OVERFLOW:
10290 37650 : case BUILT_IN_USUBL_OVERFLOW:
10291 37650 : case BUILT_IN_USUBLL_OVERFLOW:
10292 37650 : opcode = MINUS_EXPR;
10293 37650 : ifn = IFN_SUB_OVERFLOW;
10294 37650 : break;
10295 15878 : case BUILT_IN_MUL_OVERFLOW_P:
10296 15878 : ovf_only = true;
10297 : /* FALLTHRU */
10298 425759 : case BUILT_IN_MUL_OVERFLOW:
10299 425759 : case BUILT_IN_SMUL_OVERFLOW:
10300 425759 : case BUILT_IN_SMULL_OVERFLOW:
10301 425759 : case BUILT_IN_SMULLL_OVERFLOW:
10302 425759 : case BUILT_IN_UMUL_OVERFLOW:
10303 425759 : case BUILT_IN_UMULL_OVERFLOW:
10304 425759 : case BUILT_IN_UMULLL_OVERFLOW:
10305 425759 : opcode = MULT_EXPR;
10306 425759 : ifn = IFN_MUL_OVERFLOW;
10307 425759 : break;
10308 0 : default:
10309 0 : gcc_unreachable ();
10310 : }
10311 :
10312 : /* For the "generic" overloads, the first two arguments can have different
10313 : types and the last argument determines the target type to use to check
10314 : for overflow. The arguments of the other overloads all have the same
10315 : type. */
10316 502929 : tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
10317 :
10318 : /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
10319 : arguments are constant, attempt to fold the built-in call into a constant
10320 : expression indicating whether or not it detected an overflow. */
10321 502929 : if (ovf_only
10322 43279 : && TREE_CODE (arg0) == INTEGER_CST
10323 13131 : && TREE_CODE (arg1) == INTEGER_CST)
10324 : /* Perform the computation in the target type and check for overflow. */
10325 7347 : return omit_one_operand_loc (loc, boolean_type_node,
10326 7347 : arith_overflowed_p (opcode, type, arg0, arg1)
10327 : ? boolean_true_node : boolean_false_node,
10328 7347 : arg2);
10329 :
10330 495582 : tree intres, ovfres;
10331 495582 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10332 : {
10333 89160 : intres = fold_binary_loc (loc, opcode, type,
10334 : fold_convert_loc (loc, type, arg0),
10335 : fold_convert_loc (loc, type, arg1));
10336 89160 : if (TREE_OVERFLOW (intres))
10337 1629 : intres = drop_tree_overflow (intres);
10338 178320 : ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
10339 89160 : ? boolean_true_node : boolean_false_node);
10340 : }
10341 : else
10342 : {
10343 406422 : tree ctype = build_complex_type (type);
10344 406422 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10345 : arg0, arg1);
10346 406422 : tree tgt;
10347 406422 : if (ovf_only)
10348 : {
10349 : tgt = call;
10350 406422 : intres = NULL_TREE;
10351 : }
10352 : else
10353 : {
10354 : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10355 : as while the call itself is const, the REALPART_EXPR store is
10356 : certainly not. And in any case, we want just one call,
10357 : not multiple and trying to CSE them later. */
10358 370490 : TREE_SIDE_EFFECTS (call) = 1;
10359 370490 : tgt = save_expr (call);
10360 : }
10361 406422 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10362 406422 : ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10363 406422 : ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
10364 : }
10365 :
10366 495582 : if (ovf_only)
10367 35932 : return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
10368 :
10369 459650 : tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
10370 459650 : tree store
10371 459650 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
10372 459650 : return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
10373 : }
10374 :
10375 : /* Fold __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g into corresponding
10376 : internal function. */
10377 :
10378 : static tree
10379 262914 : fold_builtin_bit_query (location_t loc, enum built_in_function fcode,
10380 : tree arg0, tree arg1)
10381 : {
10382 262914 : enum internal_fn ifn;
10383 262914 : enum built_in_function fcodei, fcodel, fcodell;
10384 262914 : tree arg0_type = TREE_TYPE (arg0);
10385 262914 : tree cast_type = NULL_TREE;
10386 262914 : int addend = 0;
10387 :
10388 262914 : switch (fcode)
10389 : {
10390 184362 : case BUILT_IN_CLZG:
10391 184362 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10392 : return NULL_TREE;
10393 : ifn = IFN_CLZ;
10394 : fcodei = BUILT_IN_CLZ;
10395 : fcodel = BUILT_IN_CLZL;
10396 : fcodell = BUILT_IN_CLZLL;
10397 : break;
10398 66903 : case BUILT_IN_CTZG:
10399 66903 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10400 : return NULL_TREE;
10401 : ifn = IFN_CTZ;
10402 : fcodei = BUILT_IN_CTZ;
10403 : fcodel = BUILT_IN_CTZL;
10404 : fcodell = BUILT_IN_CTZLL;
10405 : break;
10406 : case BUILT_IN_CLRSBG:
10407 : ifn = IFN_CLRSB;
10408 : fcodei = BUILT_IN_CLRSB;
10409 : fcodel = BUILT_IN_CLRSBL;
10410 : fcodell = BUILT_IN_CLRSBLL;
10411 : break;
10412 84 : case BUILT_IN_FFSG:
10413 84 : ifn = IFN_FFS;
10414 84 : fcodei = BUILT_IN_FFS;
10415 84 : fcodel = BUILT_IN_FFSL;
10416 84 : fcodell = BUILT_IN_FFSLL;
10417 84 : break;
10418 82 : case BUILT_IN_PARITYG:
10419 82 : ifn = IFN_PARITY;
10420 82 : fcodei = BUILT_IN_PARITY;
10421 82 : fcodel = BUILT_IN_PARITYL;
10422 82 : fcodell = BUILT_IN_PARITYLL;
10423 82 : break;
10424 11401 : case BUILT_IN_POPCOUNTG:
10425 11401 : ifn = IFN_POPCOUNT;
10426 11401 : fcodei = BUILT_IN_POPCOUNT;
10427 11401 : fcodel = BUILT_IN_POPCOUNTL;
10428 11401 : fcodell = BUILT_IN_POPCOUNTLL;
10429 11401 : break;
10430 0 : default:
10431 0 : gcc_unreachable ();
10432 : }
10433 :
10434 262651 : if (TYPE_PRECISION (arg0_type)
10435 262651 : <= TYPE_PRECISION (long_long_unsigned_type_node))
10436 : {
10437 224566 : if (TYPE_PRECISION (arg0_type) <= TYPE_PRECISION (unsigned_type_node))
10438 :
10439 79535 : cast_type = (TYPE_UNSIGNED (arg0_type)
10440 79481 : ? unsigned_type_node : integer_type_node);
10441 145085 : else if (TYPE_PRECISION (arg0_type)
10442 145085 : <= TYPE_PRECISION (long_unsigned_type_node))
10443 : {
10444 145034 : cast_type = (TYPE_UNSIGNED (arg0_type)
10445 144962 : ? long_unsigned_type_node : long_integer_type_node);
10446 : fcodei = fcodel;
10447 : }
10448 : else
10449 : {
10450 123 : cast_type = (TYPE_UNSIGNED (arg0_type)
10451 123 : ? long_long_unsigned_type_node
10452 : : long_long_integer_type_node);
10453 : fcodei = fcodell;
10454 : }
10455 : }
10456 76170 : else if (TYPE_PRECISION (arg0_type) <= MAX_FIXED_MODE_SIZE)
10457 : {
10458 37924 : cast_type
10459 37924 : = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
10460 37924 : TYPE_UNSIGNED (arg0_type));
10461 37924 : gcc_assert (TYPE_PRECISION (cast_type)
10462 : == 2 * TYPE_PRECISION (long_long_unsigned_type_node));
10463 : fcodei = END_BUILTINS;
10464 : }
10465 : else
10466 : fcodei = END_BUILTINS;
10467 262490 : if (cast_type)
10468 : {
10469 262490 : switch (fcode)
10470 : {
10471 184256 : case BUILT_IN_CLZG:
10472 184256 : case BUILT_IN_CLRSBG:
10473 184256 : addend = TYPE_PRECISION (arg0_type) - TYPE_PRECISION (cast_type);
10474 184256 : break;
10475 : default:
10476 : break;
10477 : }
10478 262490 : arg0 = fold_convert (cast_type, arg0);
10479 262490 : arg0_type = cast_type;
10480 : }
10481 :
10482 262651 : if (arg1)
10483 174999 : arg1 = fold_convert (integer_type_node, arg1);
10484 :
10485 262651 : tree arg2 = arg1;
10486 262651 : if (fcode == BUILT_IN_CLZG && addend)
10487 : {
10488 4437 : if (arg1)
10489 4404 : arg0 = save_expr (arg0);
10490 : arg2 = NULL_TREE;
10491 : }
10492 262651 : tree call = NULL_TREE, tem;
10493 262651 : if (TYPE_PRECISION (arg0_type) == MAX_FIXED_MODE_SIZE
10494 38047 : && (TYPE_PRECISION (arg0_type)
10495 38047 : == 2 * TYPE_PRECISION (long_long_unsigned_type_node))
10496 : /* If the target supports the optab, then don't do the expansion. */
10497 300575 : && !direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH))
10498 : {
10499 : /* __int128 expansions using up to 2 long long builtins. */
10500 37924 : arg0 = save_expr (arg0);
10501 37924 : tree type = (TYPE_UNSIGNED (arg0_type)
10502 37924 : ? long_long_unsigned_type_node
10503 37924 : : long_long_integer_type_node);
10504 75848 : tree hi = fold_build2 (RSHIFT_EXPR, arg0_type, arg0,
10505 : build_int_cst (integer_type_node,
10506 : MAX_FIXED_MODE_SIZE / 2));
10507 37924 : hi = fold_convert (type, hi);
10508 37924 : tree lo = fold_convert (type, arg0);
10509 37924 : switch (fcode)
10510 : {
10511 37807 : case BUILT_IN_CLZG:
10512 37807 : call = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10513 75614 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10514 : build_int_cst (integer_type_node,
10515 : MAX_FIXED_MODE_SIZE / 2));
10516 37807 : if (arg2)
10517 37773 : call = fold_build3 (COND_EXPR, integer_type_node,
10518 : fold_build2 (NE_EXPR, boolean_type_node,
10519 : lo, build_zero_cst (type)),
10520 : call, arg2);
10521 37807 : call = fold_build3 (COND_EXPR, integer_type_node,
10522 : fold_build2 (NE_EXPR, boolean_type_node,
10523 : hi, build_zero_cst (type)),
10524 : fold_builtin_bit_query (loc, fcode, hi,
10525 : NULL_TREE),
10526 : call);
10527 37807 : break;
10528 41 : case BUILT_IN_CTZG:
10529 41 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10530 82 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10531 : build_int_cst (integer_type_node,
10532 : MAX_FIXED_MODE_SIZE / 2));
10533 41 : if (arg2)
10534 18 : call = fold_build3 (COND_EXPR, integer_type_node,
10535 : fold_build2 (NE_EXPR, boolean_type_node,
10536 : hi, build_zero_cst (type)),
10537 : call, arg2);
10538 41 : call = fold_build3 (COND_EXPR, integer_type_node,
10539 : fold_build2 (NE_EXPR, boolean_type_node,
10540 : lo, build_zero_cst (type)),
10541 : fold_builtin_bit_query (loc, fcode, lo,
10542 : NULL_TREE),
10543 : call);
10544 41 : break;
10545 9 : case BUILT_IN_CLRSBG:
10546 9 : tem = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10547 18 : tem = fold_build2 (PLUS_EXPR, integer_type_node, tem,
10548 : build_int_cst (integer_type_node,
10549 : MAX_FIXED_MODE_SIZE / 2));
10550 18 : tem = fold_build3 (COND_EXPR, integer_type_node,
10551 : fold_build2 (LT_EXPR, boolean_type_node,
10552 : fold_build2 (BIT_XOR_EXPR, type,
10553 : lo, hi),
10554 : build_zero_cst (type)),
10555 : build_int_cst (integer_type_node,
10556 : MAX_FIXED_MODE_SIZE / 2 - 1),
10557 : tem);
10558 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10559 9 : call = save_expr (call);
10560 18 : call = fold_build3 (COND_EXPR, integer_type_node,
10561 : fold_build2 (NE_EXPR, boolean_type_node,
10562 : call,
10563 : build_int_cst (integer_type_node,
10564 : MAX_FIXED_MODE_SIZE
10565 : / 2 - 1)),
10566 : call, tem);
10567 9 : break;
10568 9 : case BUILT_IN_FFSG:
10569 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10570 18 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10571 : build_int_cst (integer_type_node,
10572 : MAX_FIXED_MODE_SIZE / 2));
10573 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10574 : fold_build2 (NE_EXPR, boolean_type_node,
10575 : hi, build_zero_cst (type)),
10576 : call, integer_zero_node);
10577 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10578 : fold_build2 (NE_EXPR, boolean_type_node,
10579 : lo, build_zero_cst (type)),
10580 : fold_builtin_bit_query (loc, fcode, lo,
10581 : NULL_TREE),
10582 : call);
10583 9 : break;
10584 9 : case BUILT_IN_PARITYG:
10585 9 : call = fold_builtin_bit_query (loc, fcode,
10586 : fold_build2 (BIT_XOR_EXPR, type,
10587 : lo, hi), NULL_TREE);
10588 9 : break;
10589 49 : case BUILT_IN_POPCOUNTG:
10590 49 : call = fold_build2 (PLUS_EXPR, integer_type_node,
10591 : fold_builtin_bit_query (loc, fcode, hi,
10592 : NULL_TREE),
10593 : fold_builtin_bit_query (loc, fcode, lo,
10594 : NULL_TREE));
10595 49 : break;
10596 : default:
10597 : gcc_unreachable ();
10598 : }
10599 : }
10600 : else
10601 : {
10602 : /* Only keep second argument to IFN_CLZ/IFN_CTZ if it is the
10603 : value defined at zero during GIMPLE, or for large/huge _BitInt
10604 : (which are then lowered during bitint lowering). */
10605 224727 : if (arg2 && !BITINT_TYPE_P (TREE_TYPE (arg0)))
10606 : {
10607 132784 : int val;
10608 132784 : if (fcode == BUILT_IN_CLZG)
10609 : {
10610 66271 : if (CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10611 : val) != 2
10612 66271 : || wi::to_widest (arg2) != val)
10613 : arg2 = NULL_TREE;
10614 : }
10615 66513 : else if (CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10616 : val) != 2
10617 66513 : || wi::to_widest (arg2) != val)
10618 : arg2 = NULL_TREE;
10619 132784 : if (!direct_internal_fn_supported_p (ifn, arg0_type,
10620 : OPTIMIZE_FOR_BOTH))
10621 : arg2 = NULL_TREE;
10622 132673 : if (arg2 == NULL_TREE)
10623 132736 : arg0 = save_expr (arg0);
10624 : }
10625 224727 : if (fcodei == END_BUILTINS || arg2)
10626 350 : call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
10627 : arg2 ? 2 : 1, arg0, arg2);
10628 : else
10629 224518 : call = build_call_expr_loc (loc, builtin_decl_explicit (fcodei), 1,
10630 : arg0);
10631 : }
10632 262651 : if (addend)
10633 4455 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10634 : build_int_cst (integer_type_node, addend));
10635 262651 : if (arg1 && arg2 == NULL_TREE)
10636 137140 : call = fold_build3 (COND_EXPR, integer_type_node,
10637 : fold_build2 (NE_EXPR, boolean_type_node,
10638 : arg0, build_zero_cst (arg0_type)),
10639 : call, arg1);
10640 :
10641 : return call;
10642 : }
10643 :
10644 : /* Fold or build a __builtin_bswapg (ARG) (if IFN is IFN_BSWAP) or
10645 : __builtin_bitreverseg (ARG (otherwise) call. The FE should have
10646 : verified earlier that the argument type is unsigned INTEGER_TYPE
10647 : or BITINT_TYPE, for __builtin_bswapg with precision divisible by 8. */
10648 :
10649 : tree
10650 218 : fold_build_builtin_bswapg_bitreverseg (location_t loc, enum internal_fn ifn,
10651 : tree arg)
10652 : {
10653 218 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg));
10654 218 : if (ifn == IFN_BSWAP && TYPE_PRECISION (type) == 8)
10655 11 : return fold_convert_loc (loc, type, arg);
10656 207 : struct { tree type; built_in_function bswap, bitreverse; } fns[]
10657 207 : = { { unsigned_char_type_node, END_BUILTINS, BUILT_IN_BITREVERSE8 },
10658 207 : { uint16_type_node, BUILT_IN_BSWAP16, BUILT_IN_BITREVERSE16 },
10659 207 : { uint32_type_node, BUILT_IN_BSWAP32, BUILT_IN_BITREVERSE32 },
10660 207 : { uint64_type_node, BUILT_IN_BSWAP64, BUILT_IN_BITREVERSE64 },
10661 207 : { uint128_type_node, BUILT_IN_BSWAP128, BUILT_IN_BITREVERSE128 } };
10662 207 : if (TREE_CODE (arg) == INTEGER_CST)
10663 : {
10664 127 : wide_int res;
10665 127 : if (ifn == IFN_BSWAP)
10666 37 : res = wi::bswap (wi::to_wide (arg));
10667 : else
10668 96 : res = wi::bitreverse (wi::to_wide (arg));
10669 127 : return wide_int_to_tree (type, res);
10670 127 : }
10671 346 : for (unsigned i = 0; i < ARRAY_SIZE (fns); ++i)
10672 314 : if (fns[i].type
10673 314 : && TYPE_PRECISION (fns[i].type) >= TYPE_PRECISION (type))
10674 : {
10675 48 : arg = fold_convert_loc (loc, fns[i].type, arg);
10676 48 : tree fndecl
10677 48 : = builtin_decl_explicit (ifn == IFN_BSWAP
10678 : ? fns[i].bswap : fns[i].bitreverse);
10679 48 : tree ret = build_call_expr_loc (loc, fndecl, 1, arg);
10680 48 : if (TYPE_PRECISION (type) != TYPE_PRECISION (fns[i].type))
10681 4 : ret = fold_build2_loc (loc, RSHIFT_EXPR, fns[i].type, ret,
10682 : build_int_cst (unsigned_type_node,
10683 4 : TYPE_PRECISION (fns[i].type)
10684 4 : - TYPE_PRECISION (type)));
10685 48 : return fold_convert_loc (loc, type, ret);
10686 : }
10687 32 : gcc_assert (TREE_CODE (type) == BITINT_TYPE);
10688 32 : return build_call_expr_internal_loc (loc, ifn, type, 1, arg);
10689 : }
10690 :
10691 : /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
10692 : that return both result of arithmetics and overflowed boolean
10693 : flag in a complex integer result. */
10694 :
10695 : static tree
10696 55 : fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
10697 : tree *args)
10698 : {
10699 55 : enum internal_fn ifn;
10700 :
10701 55 : switch (fcode)
10702 : {
10703 : case BUILT_IN_ADDC:
10704 : case BUILT_IN_ADDCL:
10705 : case BUILT_IN_ADDCLL:
10706 : ifn = IFN_ADD_OVERFLOW;
10707 : break;
10708 29 : case BUILT_IN_SUBC:
10709 29 : case BUILT_IN_SUBCL:
10710 29 : case BUILT_IN_SUBCLL:
10711 29 : ifn = IFN_SUB_OVERFLOW;
10712 29 : break;
10713 0 : default:
10714 0 : gcc_unreachable ();
10715 : }
10716 :
10717 55 : tree type = TREE_TYPE (args[0]);
10718 55 : tree ctype = build_complex_type (type);
10719 55 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10720 : args[0], args[1]);
10721 : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10722 : as while the call itself is const, the REALPART_EXPR store is
10723 : certainly not. And in any case, we want just one call,
10724 : not multiple and trying to CSE them later. */
10725 55 : TREE_SIDE_EFFECTS (call) = 1;
10726 55 : tree tgt = save_expr (call);
10727 55 : tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10728 55 : tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10729 55 : call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10730 : intres, args[2]);
10731 55 : TREE_SIDE_EFFECTS (call) = 1;
10732 55 : tgt = save_expr (call);
10733 55 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10734 55 : tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10735 55 : ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
10736 55 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
10737 55 : tree store
10738 55 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10739 55 : return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10740 : }
10741 :
10742 : /* Fold __builtin_clmulg (arg1, arg2) or __builtin_clmulg (arg1, arg2, arg3)
10743 : into IFN_CLMUL or IFN_CLMUL_WIDEN internal call. */
10744 :
10745 : tree
10746 552 : fold_build_builtin_clmulg (location_t loc, tree arg1, tree arg2, tree arg3)
10747 : {
10748 552 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg1));
10749 552 : if (TREE_CODE (arg1) == INTEGER_CST
10750 173 : && !TREE_OVERFLOW (arg1)
10751 173 : && TREE_CODE (arg2) == INTEGER_CST
10752 692 : && !TREE_OVERFLOW (arg2))
10753 : {
10754 140 : unsigned prec = TYPE_PRECISION (type);
10755 140 : wide_int res = wi::clmul (wi::to_wide (arg1), wi::to_wide (arg2),
10756 300 : prec * (arg3 == NULL_TREE ? 1 : 2));
10757 140 : if (arg3 == NULL_TREE)
10758 120 : return wide_int_to_tree (type, res);
10759 20 : wide_int lo = wide_int::from (res, prec, UNSIGNED);
10760 20 : wide_int hi = wide_int::from (wi::rshift (res, prec, UNSIGNED),
10761 40 : prec, UNSIGNED);
10762 20 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, arg3);
10763 20 : tree store
10764 20 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3,
10765 20 : wide_int_to_tree (type, hi));
10766 40 : return build2_loc (loc, COMPOUND_EXPR, type, store,
10767 40 : wide_int_to_tree (type, lo));
10768 160 : }
10769 412 : if (arg3 == NULL_TREE)
10770 246 : return build_call_expr_internal_loc (loc, IFN_CLMUL, type, 2, arg1, arg2);
10771 166 : tree ctype = build_complex_type (type);
10772 166 : tree call = build_call_expr_internal_loc (loc, IFN_CLMUL_WIDEN, ctype, 2,
10773 : arg1, arg2);
10774 : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10775 : as while the call itself is const, the REALPART_EXPR store is
10776 : certainly not. And in any case, we want just one call,
10777 : not multiple and trying to CSE them later. */
10778 166 : TREE_SIDE_EFFECTS (call) = 1;
10779 166 : tree tgt = save_expr (call);
10780 166 : tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10781 166 : tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10782 166 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, arg3);
10783 166 : tree store
10784 166 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10785 166 : return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10786 : }
10787 :
10788 : /* Fold __builtin_bitcompressg (arg1, arg2) or
10789 : __builtin_bitexpandg (arg1, arg2) into IFN_BITCOMPRESS or IFN_BITEXPAND
10790 : internal call. */
10791 :
10792 : tree
10793 2628 : fold_build_builtin_bitcompressg_bitexpandg (location_t loc, internal_fn ifn,
10794 : tree arg1, tree arg2)
10795 : {
10796 2628 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg1));
10797 2628 : if (TREE_CODE (arg1) == INTEGER_CST
10798 2470 : && !TREE_OVERFLOW (arg1)
10799 2470 : && TREE_CODE (arg2) == INTEGER_CST
10800 5098 : && !TREE_OVERFLOW (arg2))
10801 : {
10802 2470 : unsigned prec = TYPE_PRECISION (type);
10803 2470 : wide_int res;
10804 2470 : if (ifn == IFN_BITCOMPRESS)
10805 1223 : res = wi::bit_compress (wi::to_wide (arg1), wi::to_wide (arg2), prec);
10806 : else
10807 1263 : res = wi::bit_expand (wi::to_wide (arg1), wi::to_wide (arg2), prec);
10808 2470 : return wide_int_to_tree (type, res);
10809 2470 : }
10810 158 : return build_call_expr_internal_loc (loc, ifn, type, 2, arg1, arg2);
10811 : }
10812 :
10813 : /* Fold a call to __builtin_FILE to a constant string. */
10814 :
10815 : static inline tree
10816 5949 : fold_builtin_FILE (location_t loc)
10817 : {
10818 5949 : if (const char *fname = LOCATION_FILE (loc))
10819 : {
10820 : /* The documentation says this builtin is equivalent to the preprocessor
10821 : __FILE__ macro so it appears appropriate to use the same file prefix
10822 : mappings. */
10823 5949 : fname = remap_macro_filename (fname);
10824 5949 : return build_string_literal (fname);
10825 : }
10826 :
10827 0 : return build_string_literal ("");
10828 : }
10829 :
10830 : /* Fold a call to __builtin_FUNCTION to a constant string. */
10831 :
10832 : static inline tree
10833 68 : fold_builtin_FUNCTION ()
10834 : {
10835 68 : const char *name = "";
10836 :
10837 68 : if (current_function_decl)
10838 43 : name = lang_hooks.decl_printable_name (current_function_decl, 0);
10839 :
10840 68 : return build_string_literal (name);
10841 : }
10842 :
10843 : /* Fold a call to __builtin_LINE to an integer constant. */
10844 :
10845 : static inline tree
10846 11865 : fold_builtin_LINE (location_t loc, tree type)
10847 : {
10848 11865 : return build_int_cst (type, LOCATION_LINE (loc));
10849 : }
10850 :
10851 : /* Fold a call to built-in function FNDECL with 0 arguments.
10852 : This function returns NULL_TREE if no simplification was possible. */
10853 :
10854 : static tree
10855 23741625 : fold_builtin_0 (location_t loc, tree fndecl)
10856 : {
10857 23741625 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10858 23741625 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10859 23741625 : switch (fcode)
10860 : {
10861 5949 : case BUILT_IN_FILE:
10862 5949 : return fold_builtin_FILE (loc);
10863 :
10864 68 : case BUILT_IN_FUNCTION:
10865 68 : return fold_builtin_FUNCTION ();
10866 :
10867 11865 : case BUILT_IN_LINE:
10868 11865 : return fold_builtin_LINE (loc, type);
10869 :
10870 34170 : CASE_FLT_FN (BUILT_IN_INF):
10871 34170 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
10872 34170 : case BUILT_IN_INFD32:
10873 34170 : case BUILT_IN_INFD64:
10874 34170 : case BUILT_IN_INFD128:
10875 34170 : case BUILT_IN_INFD64X:
10876 34170 : return fold_builtin_inf (loc, type, true);
10877 :
10878 224094 : CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10879 224094 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
10880 224094 : return fold_builtin_inf (loc, type, false);
10881 :
10882 0 : case BUILT_IN_CLASSIFY_TYPE:
10883 0 : return fold_builtin_classify_type (NULL_TREE);
10884 :
10885 20224280 : case BUILT_IN_UNREACHABLE:
10886 : /* Rewrite any explicit calls to __builtin_unreachable. */
10887 20224280 : if (sanitize_flags_p (SANITIZE_UNREACHABLE))
10888 91 : return build_builtin_unreachable (loc);
10889 : break;
10890 :
10891 : default:
10892 : break;
10893 : }
10894 : return NULL_TREE;
10895 : }
10896 :
10897 : /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10898 : This function returns NULL_TREE if no simplification was possible. */
10899 :
10900 : static tree
10901 20380103 : fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
10902 : {
10903 20380103 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10904 20380103 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10905 :
10906 20380103 : if (error_operand_p (arg0))
10907 : return NULL_TREE;
10908 :
10909 20380103 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
10910 : return ret;
10911 :
10912 19767309 : switch (fcode)
10913 : {
10914 3088273 : case BUILT_IN_CONSTANT_P:
10915 3088273 : {
10916 3088273 : tree val = fold_builtin_constant_p (arg0);
10917 :
10918 : /* Gimplification will pull the CALL_EXPR for the builtin out of
10919 : an if condition. When not optimizing, we'll not CSE it back.
10920 : To avoid link error types of regressions, return false now. */
10921 3088273 : if (!val && !optimize)
10922 1558 : val = integer_zero_node;
10923 :
10924 : return val;
10925 : }
10926 :
10927 2096 : case BUILT_IN_CLASSIFY_TYPE:
10928 2096 : return fold_builtin_classify_type (arg0);
10929 :
10930 461603 : case BUILT_IN_STRLEN:
10931 461603 : return fold_builtin_strlen (loc, expr, type, arg0);
10932 :
10933 374110 : CASE_FLT_FN (BUILT_IN_FABS):
10934 374110 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10935 374110 : case BUILT_IN_FABSD32:
10936 374110 : case BUILT_IN_FABSD64:
10937 374110 : case BUILT_IN_FABSD128:
10938 374110 : case BUILT_IN_FABSD64X:
10939 374110 : return fold_builtin_fabs (loc, arg0, type);
10940 :
10941 98988 : case BUILT_IN_ABS:
10942 98988 : case BUILT_IN_LABS:
10943 98988 : case BUILT_IN_LLABS:
10944 98988 : case BUILT_IN_IMAXABS:
10945 98988 : case BUILT_IN_UABS:
10946 98988 : case BUILT_IN_ULABS:
10947 98988 : case BUILT_IN_ULLABS:
10948 98988 : case BUILT_IN_UMAXABS:
10949 98988 : return fold_builtin_abs (loc, arg0, type);
10950 :
10951 25461 : CASE_FLT_FN (BUILT_IN_CONJ):
10952 25461 : if (validate_arg (arg0, COMPLEX_TYPE)
10953 25461 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10954 25461 : return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10955 : break;
10956 :
10957 764 : CASE_FLT_FN (BUILT_IN_CREAL):
10958 764 : if (validate_arg (arg0, COMPLEX_TYPE)
10959 764 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10960 764 : return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
10961 : break;
10962 :
10963 1884 : CASE_FLT_FN (BUILT_IN_CIMAG):
10964 1884 : if (validate_arg (arg0, COMPLEX_TYPE)
10965 1884 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10966 1884 : return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10967 : break;
10968 :
10969 104166 : CASE_FLT_FN (BUILT_IN_CARG):
10970 104166 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
10971 104166 : return fold_builtin_carg (loc, arg0, type);
10972 :
10973 211 : case BUILT_IN_ISASCII:
10974 211 : return fold_builtin_isascii (loc, arg0);
10975 :
10976 168 : case BUILT_IN_TOASCII:
10977 168 : return fold_builtin_toascii (loc, arg0);
10978 :
10979 325 : case BUILT_IN_ISDIGIT:
10980 325 : return fold_builtin_isdigit (loc, arg0);
10981 :
10982 630537 : CASE_FLT_FN (BUILT_IN_FINITE):
10983 630537 : case BUILT_IN_FINITED32:
10984 630537 : case BUILT_IN_FINITED64:
10985 630537 : case BUILT_IN_FINITED128:
10986 630537 : case BUILT_IN_ISFINITE:
10987 630537 : {
10988 630537 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10989 630537 : if (ret)
10990 : return ret;
10991 630205 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10992 : }
10993 :
10994 706767 : CASE_FLT_FN (BUILT_IN_ISINF):
10995 706767 : case BUILT_IN_ISINFD32:
10996 706767 : case BUILT_IN_ISINFD64:
10997 706767 : case BUILT_IN_ISINFD128:
10998 706767 : {
10999 706767 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
11000 706767 : if (ret)
11001 : return ret;
11002 706516 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
11003 : }
11004 :
11005 239210 : case BUILT_IN_ISNORMAL:
11006 239210 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
11007 :
11008 708 : case BUILT_IN_ISINF_SIGN:
11009 708 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
11010 :
11011 242331 : CASE_FLT_FN (BUILT_IN_ISNAN):
11012 242331 : case BUILT_IN_ISNAND32:
11013 242331 : case BUILT_IN_ISNAND64:
11014 242331 : case BUILT_IN_ISNAND128:
11015 242331 : {
11016 242331 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
11017 242331 : if (ret)
11018 : return ret;
11019 242207 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
11020 : }
11021 :
11022 27879 : case BUILT_IN_ISSIGNALING:
11023 27879 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
11024 :
11025 681447 : case BUILT_IN_FREE:
11026 681447 : if (integer_zerop (arg0))
11027 1093 : return build_empty_stmt (loc);
11028 : break;
11029 :
11030 11813 : case BUILT_IN_CLZG:
11031 11813 : case BUILT_IN_CTZG:
11032 11813 : case BUILT_IN_CLRSBG:
11033 11813 : case BUILT_IN_FFSG:
11034 11813 : case BUILT_IN_PARITYG:
11035 11813 : case BUILT_IN_POPCOUNTG:
11036 11813 : return fold_builtin_bit_query (loc, fcode, arg0, NULL_TREE);
11037 :
11038 : default:
11039 : break;
11040 : }
11041 :
11042 : return NULL_TREE;
11043 :
11044 : }
11045 :
11046 : /* Folds a call EXPR (which may be null) to built-in function FNDECL
11047 : with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
11048 : if no simplification was possible. */
11049 :
11050 : static tree
11051 19402427 : fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
11052 : {
11053 19402427 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
11054 19402427 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11055 :
11056 19402427 : if (error_operand_p (arg0)
11057 19402427 : || error_operand_p (arg1))
11058 : return NULL_TREE;
11059 :
11060 19402423 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
11061 : return ret;
11062 :
11063 19186998 : switch (fcode)
11064 : {
11065 6523 : CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
11066 6523 : CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
11067 6523 : if (validate_arg (arg0, REAL_TYPE)
11068 6523 : && validate_arg (arg1, POINTER_TYPE))
11069 6523 : return do_mpfr_lgamma_r (arg0, arg1, type);
11070 : break;
11071 :
11072 116034 : CASE_FLT_FN (BUILT_IN_FREXP):
11073 116034 : return fold_builtin_frexp (loc, arg0, arg1, type);
11074 :
11075 92567 : CASE_FLT_FN (BUILT_IN_MODF):
11076 92567 : return fold_builtin_modf (loc, arg0, arg1, type);
11077 :
11078 2560 : case BUILT_IN_STRSPN:
11079 2560 : return fold_builtin_strspn (loc, expr, arg0, arg1, type);
11080 :
11081 2465 : case BUILT_IN_STRCSPN:
11082 2465 : return fold_builtin_strcspn (loc, expr, arg0, arg1, type);
11083 :
11084 99427 : case BUILT_IN_STRPBRK:
11085 99427 : return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
11086 :
11087 6659109 : case BUILT_IN_EXPECT:
11088 6659109 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
11089 :
11090 945887 : case BUILT_IN_ISGREATER:
11091 945887 : return fold_builtin_unordered_cmp (loc, fndecl,
11092 945887 : arg0, arg1, UNLE_EXPR, LE_EXPR);
11093 478556 : case BUILT_IN_ISGREATEREQUAL:
11094 478556 : return fold_builtin_unordered_cmp (loc, fndecl,
11095 478556 : arg0, arg1, UNLT_EXPR, LT_EXPR);
11096 240873 : case BUILT_IN_ISLESS:
11097 240873 : return fold_builtin_unordered_cmp (loc, fndecl,
11098 240873 : arg0, arg1, UNGE_EXPR, GE_EXPR);
11099 1108749 : case BUILT_IN_ISLESSEQUAL:
11100 1108749 : return fold_builtin_unordered_cmp (loc, fndecl,
11101 1108749 : arg0, arg1, UNGT_EXPR, GT_EXPR);
11102 239041 : case BUILT_IN_ISLESSGREATER:
11103 239041 : return fold_builtin_unordered_cmp (loc, fndecl,
11104 239041 : arg0, arg1, UNEQ_EXPR, EQ_EXPR);
11105 240258 : case BUILT_IN_ISUNORDERED:
11106 240258 : return fold_builtin_unordered_cmp (loc, fndecl,
11107 : arg0, arg1, UNORDERED_EXPR,
11108 240258 : NOP_EXPR);
11109 :
11110 709 : case BUILT_IN_ISEQSIG:
11111 709 : return fold_builtin_iseqsig (loc, arg0, arg1);
11112 :
11113 : /* We do the folding for va_start in the expander. */
11114 : case BUILT_IN_VA_START:
11115 : break;
11116 :
11117 201177 : case BUILT_IN_OBJECT_SIZE:
11118 201177 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
11119 201177 : return fold_builtin_object_size (arg0, arg1, fcode);
11120 :
11121 104805 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
11122 104805 : return fold_builtin_atomic_always_lock_free (arg0, arg1);
11123 :
11124 38549 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
11125 38549 : return fold_builtin_atomic_is_lock_free (arg0, arg1);
11126 :
11127 175262 : case BUILT_IN_CLZG:
11128 175262 : case BUILT_IN_CTZG:
11129 175262 : return fold_builtin_bit_query (loc, fcode, arg0, arg1);
11130 :
11131 : default:
11132 : break;
11133 : }
11134 : return NULL_TREE;
11135 : }
11136 :
11137 : /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
11138 : and ARG2.
11139 : This function returns NULL_TREE if no simplification was possible. */
11140 :
11141 : static tree
11142 7386576 : fold_builtin_3 (location_t loc, tree fndecl,
11143 : tree arg0, tree arg1, tree arg2)
11144 : {
11145 7386576 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
11146 7386576 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11147 :
11148 7386576 : if (error_operand_p (arg0)
11149 7386576 : || error_operand_p (arg1)
11150 14773152 : || error_operand_p (arg2))
11151 : return NULL_TREE;
11152 :
11153 7386574 : if (tree ret = fold_const_call (as_combined_fn (fcode), type,
11154 : arg0, arg1, arg2))
11155 : return ret;
11156 :
11157 7369504 : switch (fcode)
11158 : {
11159 :
11160 145 : CASE_FLT_FN (BUILT_IN_SINCOS):
11161 145 : return fold_builtin_sincos (loc, arg0, arg1, arg2);
11162 :
11163 99800 : CASE_FLT_FN (BUILT_IN_REMQUO):
11164 99800 : if (validate_arg (arg0, REAL_TYPE)
11165 99800 : && validate_arg (arg1, REAL_TYPE)
11166 199600 : && validate_arg (arg2, POINTER_TYPE))
11167 99800 : return do_mpfr_remquo (arg0, arg1, arg2);
11168 : break;
11169 :
11170 2551307 : case BUILT_IN_MEMCMP:
11171 2551307 : return fold_builtin_memcmp (loc, arg0, arg1, arg2);
11172 :
11173 537757 : case BUILT_IN_EXPECT:
11174 537757 : return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
11175 :
11176 395 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
11177 395 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
11178 :
11179 502929 : case BUILT_IN_ADD_OVERFLOW:
11180 502929 : case BUILT_IN_SUB_OVERFLOW:
11181 502929 : case BUILT_IN_MUL_OVERFLOW:
11182 502929 : case BUILT_IN_ADD_OVERFLOW_P:
11183 502929 : case BUILT_IN_SUB_OVERFLOW_P:
11184 502929 : case BUILT_IN_MUL_OVERFLOW_P:
11185 502929 : case BUILT_IN_SADD_OVERFLOW:
11186 502929 : case BUILT_IN_SADDL_OVERFLOW:
11187 502929 : case BUILT_IN_SADDLL_OVERFLOW:
11188 502929 : case BUILT_IN_SSUB_OVERFLOW:
11189 502929 : case BUILT_IN_SSUBL_OVERFLOW:
11190 502929 : case BUILT_IN_SSUBLL_OVERFLOW:
11191 502929 : case BUILT_IN_SMUL_OVERFLOW:
11192 502929 : case BUILT_IN_SMULL_OVERFLOW:
11193 502929 : case BUILT_IN_SMULLL_OVERFLOW:
11194 502929 : case BUILT_IN_UADD_OVERFLOW:
11195 502929 : case BUILT_IN_UADDL_OVERFLOW:
11196 502929 : case BUILT_IN_UADDLL_OVERFLOW:
11197 502929 : case BUILT_IN_USUB_OVERFLOW:
11198 502929 : case BUILT_IN_USUBL_OVERFLOW:
11199 502929 : case BUILT_IN_USUBLL_OVERFLOW:
11200 502929 : case BUILT_IN_UMUL_OVERFLOW:
11201 502929 : case BUILT_IN_UMULL_OVERFLOW:
11202 502929 : case BUILT_IN_UMULLL_OVERFLOW:
11203 502929 : return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
11204 :
11205 : default:
11206 : break;
11207 : }
11208 : return NULL_TREE;
11209 : }
11210 :
11211 : /* Folds a call EXPR (which may be null) to built-in function FNDECL.
11212 : ARGS is an array of NARGS arguments. IGNORE is true if the result
11213 : of the function call is ignored. This function returns NULL_TREE
11214 : if no simplification was possible. */
11215 :
11216 : static tree
11217 73592206 : fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
11218 : int nargs, bool)
11219 : {
11220 73592206 : tree ret = NULL_TREE;
11221 :
11222 73592206 : switch (nargs)
11223 : {
11224 23741625 : case 0:
11225 23741625 : ret = fold_builtin_0 (loc, fndecl);
11226 23741625 : break;
11227 20380103 : case 1:
11228 20380103 : ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
11229 20380103 : break;
11230 19402427 : case 2:
11231 19402427 : ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
11232 19402427 : break;
11233 7386576 : case 3:
11234 7386576 : ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
11235 7386576 : break;
11236 2681475 : default:
11237 2681475 : ret = fold_builtin_varargs (loc, fndecl, args, nargs);
11238 2681475 : break;
11239 : }
11240 73592206 : if (ret)
11241 : {
11242 10021398 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11243 10021398 : SET_EXPR_LOCATION (ret, loc);
11244 10021398 : return ret;
11245 : }
11246 : return NULL_TREE;
11247 : }
11248 :
11249 : /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11250 : list ARGS along with N new arguments in NEWARGS. SKIP is the number
11251 : of arguments in ARGS to be omitted. OLDNARGS is the number of
11252 : elements in ARGS. */
11253 :
11254 : static tree
11255 4 : rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11256 : int skip, tree fndecl, int n, va_list newargs)
11257 : {
11258 4 : int nargs = oldnargs - skip + n;
11259 4 : tree *buffer;
11260 :
11261 4 : if (n > 0)
11262 : {
11263 0 : int i, j;
11264 :
11265 0 : buffer = XALLOCAVEC (tree, nargs);
11266 0 : for (i = 0; i < n; i++)
11267 0 : buffer[i] = va_arg (newargs, tree);
11268 0 : for (j = skip; j < oldnargs; j++, i++)
11269 0 : buffer[i] = args[j];
11270 : }
11271 : else
11272 4 : buffer = args + skip;
11273 :
11274 4 : return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11275 : }
11276 :
11277 : /* Return true if FNDECL shouldn't be folded right now.
11278 : If a built-in function has an inline attribute always_inline
11279 : wrapper, defer folding it after always_inline functions have
11280 : been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11281 : might not be performed. */
11282 :
11283 : bool
11284 154880192 : avoid_folding_inline_builtin (tree fndecl)
11285 : {
11286 154880192 : return (DECL_DECLARED_INLINE_P (fndecl)
11287 13336 : && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11288 13294 : && cfun
11289 13294 : && !cfun->always_inline_functions_inlined
11290 154893486 : && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11291 : }
11292 :
11293 : /* A wrapper function for builtin folding that prevents warnings for
11294 : "statement without effect" and the like, caused by removing the
11295 : call node earlier than the warning is generated. */
11296 :
11297 : tree
11298 202672164 : fold_call_expr (location_t loc, tree exp, bool ignore)
11299 : {
11300 202672164 : tree ret = NULL_TREE;
11301 202672164 : tree fndecl = get_callee_fndecl (exp);
11302 200911146 : if (fndecl && fndecl_built_in_p (fndecl)
11303 : /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11304 : yet. Defer folding until we see all the arguments
11305 : (after inlining). */
11306 262836642 : && !CALL_EXPR_VA_ARG_PACK (exp))
11307 : {
11308 60164447 : int nargs = call_expr_nargs (exp);
11309 :
11310 : /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11311 : instead last argument is __builtin_va_arg_pack (). Defer folding
11312 : even in that case, until arguments are finalized. */
11313 60164447 : if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11314 : {
11315 221315 : tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11316 221315 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11317 : return NULL_TREE;
11318 : }
11319 :
11320 60164378 : if (avoid_folding_inline_builtin (fndecl))
11321 : return NULL_TREE;
11322 :
11323 60160840 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11324 72288100 : return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11325 72288100 : CALL_EXPR_ARGP (exp), ignore);
11326 : else
11327 : {
11328 24016790 : tree *args = CALL_EXPR_ARGP (exp);
11329 24016790 : ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
11330 24016790 : if (ret)
11331 : return ret;
11332 : }
11333 : }
11334 : return NULL_TREE;
11335 : }
11336 :
11337 : /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
11338 : N arguments are passed in the array ARGARRAY. Return a folded
11339 : expression or NULL_TREE if no simplification was possible. */
11340 :
11341 : tree
11342 79302934 : fold_builtin_call_array (location_t loc, tree,
11343 : tree fn,
11344 : int n,
11345 : tree *argarray)
11346 : {
11347 79302934 : if (TREE_CODE (fn) != ADDR_EXPR)
11348 : return NULL_TREE;
11349 :
11350 79302934 : tree fndecl = TREE_OPERAND (fn, 0);
11351 79302934 : if (TREE_CODE (fndecl) == FUNCTION_DECL
11352 79302934 : && fndecl_built_in_p (fndecl))
11353 : {
11354 : /* If last argument is __builtin_va_arg_pack (), arguments to this
11355 : function are not finalized yet. Defer folding until they are. */
11356 78680518 : if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11357 : {
11358 137307 : tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11359 137307 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11360 : return NULL_TREE;
11361 : }
11362 78680479 : if (avoid_folding_inline_builtin (fndecl))
11363 : return NULL_TREE;
11364 78680479 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11365 35379941 : return targetm.fold_builtin (fndecl, n, argarray, false);
11366 : else
11367 43300538 : return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
11368 : }
11369 :
11370 : return NULL_TREE;
11371 : }
11372 :
11373 : /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11374 : along with N new arguments specified as the "..." parameters. SKIP
11375 : is the number of arguments in EXP to be omitted. This function is used
11376 : to do varargs-to-varargs transformations. */
11377 :
11378 : static tree
11379 4 : rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11380 : {
11381 4 : va_list ap;
11382 4 : tree t;
11383 :
11384 4 : va_start (ap, n);
11385 8 : t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11386 4 : CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11387 4 : va_end (ap);
11388 :
11389 4 : return t;
11390 : }
11391 :
11392 : /* Validate a single argument ARG against a tree code CODE representing
11393 : a type. Return true when argument is valid. */
11394 :
11395 : static bool
11396 15376140 : validate_arg (const_tree arg, enum tree_code code)
11397 : {
11398 15376140 : if (!arg)
11399 : return false;
11400 15376117 : else if (code == POINTER_TYPE)
11401 7118025 : return POINTER_TYPE_P (TREE_TYPE (arg));
11402 8258092 : else if (code == INTEGER_TYPE)
11403 3778312 : return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11404 4479780 : return code == TREE_CODE (TREE_TYPE (arg));
11405 : }
11406 :
11407 : /* This function validates the types of a function call argument list
11408 : against a specified list of tree_codes. If the last specifier is a 0,
11409 : that represents an ellipses, otherwise the last specifier must be a
11410 : VOID_TYPE.
11411 :
11412 : This is the GIMPLE version of validate_arglist. Eventually we want to
11413 : completely convert builtins.cc to work from GIMPLEs and the tree based
11414 : validate_arglist will then be removed. */
11415 :
11416 : bool
11417 136 : validate_gimple_arglist (const gcall *call, ...)
11418 : {
11419 136 : enum tree_code code;
11420 136 : bool res = 0;
11421 136 : va_list ap;
11422 136 : const_tree arg;
11423 136 : size_t i;
11424 :
11425 136 : va_start (ap, call);
11426 136 : i = 0;
11427 :
11428 544 : do
11429 : {
11430 544 : code = (enum tree_code) va_arg (ap, int);
11431 544 : switch (code)
11432 : {
11433 0 : case 0:
11434 : /* This signifies an ellipses, any further arguments are all ok. */
11435 0 : res = true;
11436 0 : goto end;
11437 136 : case VOID_TYPE:
11438 : /* This signifies an endlink, if no arguments remain, return
11439 : true, otherwise return false. */
11440 136 : res = (i == gimple_call_num_args (call));
11441 136 : goto end;
11442 408 : default:
11443 : /* If no parameters remain or the parameter's code does not
11444 : match the specified code, return false. Otherwise continue
11445 : checking any remaining arguments. */
11446 408 : arg = gimple_call_arg (call, i++);
11447 408 : if (!validate_arg (arg, code))
11448 0 : goto end;
11449 : break;
11450 : }
11451 : }
11452 : while (1);
11453 :
11454 : /* We need gotos here since we can only have one VA_CLOSE in a
11455 : function. */
11456 136 : end: ;
11457 136 : va_end (ap);
11458 :
11459 136 : return res;
11460 : }
11461 :
11462 : /* Default target-specific builtin expander that does nothing. */
11463 :
11464 : rtx
11465 0 : default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11466 : rtx target ATTRIBUTE_UNUSED,
11467 : rtx subtarget ATTRIBUTE_UNUSED,
11468 : machine_mode mode ATTRIBUTE_UNUSED,
11469 : int ignore ATTRIBUTE_UNUSED)
11470 : {
11471 0 : return NULL_RTX;
11472 : }
11473 :
11474 : /* Returns true is EXP represents data that would potentially reside
11475 : in a readonly section. */
11476 :
11477 : bool
11478 209353 : readonly_data_expr (tree exp)
11479 : {
11480 209353 : STRIP_NOPS (exp);
11481 :
11482 209353 : if (TREE_CODE (exp) != ADDR_EXPR)
11483 : return false;
11484 :
11485 27170 : exp = get_base_address (TREE_OPERAND (exp, 0));
11486 27170 : if (!exp)
11487 : return false;
11488 :
11489 : /* Make sure we call decl_readonly_section only for trees it
11490 : can handle (since it returns true for everything it doesn't
11491 : understand). */
11492 27170 : if (TREE_CODE (exp) == STRING_CST
11493 5942 : || TREE_CODE (exp) == CONSTRUCTOR
11494 5942 : || (VAR_P (exp) && TREE_STATIC (exp)))
11495 23943 : return decl_readonly_section (exp, 0);
11496 : else
11497 : return false;
11498 : }
11499 :
11500 : /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11501 : to the call, and TYPE is its return type.
11502 :
11503 : Return NULL_TREE if no simplification was possible, otherwise return the
11504 : simplified form of the call as a tree.
11505 :
11506 : The simplified form may be a constant or other expression which
11507 : computes the same value, but in a more efficient manner (including
11508 : calls to other builtin functions).
11509 :
11510 : The call may contain arguments which need to be evaluated, but
11511 : which are not useful to determine the result of the call. In
11512 : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11513 : COMPOUND_EXPR will be an argument which must be evaluated.
11514 : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11515 : COMPOUND_EXPR in the chain will contain the tree for the simplified
11516 : form of the builtin function call. */
11517 :
11518 : static tree
11519 99427 : fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
11520 : {
11521 99427 : if (!validate_arg (s1, POINTER_TYPE)
11522 99427 : || !validate_arg (s2, POINTER_TYPE))
11523 : return NULL_TREE;
11524 :
11525 99427 : tree fn;
11526 99427 : const char *p1, *p2;
11527 :
11528 99427 : p2 = c_getstr (s2);
11529 99427 : if (p2 == NULL)
11530 : return NULL_TREE;
11531 :
11532 92 : p1 = c_getstr (s1);
11533 92 : if (p1 != NULL)
11534 : {
11535 22 : const char *r = strpbrk (p1, p2);
11536 22 : tree tem;
11537 :
11538 22 : if (r == NULL)
11539 0 : return build_int_cst (TREE_TYPE (s1), 0);
11540 :
11541 : /* Return an offset into the constant string argument. */
11542 22 : tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11543 22 : return fold_convert_loc (loc, type, tem);
11544 : }
11545 :
11546 70 : if (p2[0] == '\0')
11547 : /* strpbrk(x, "") == NULL.
11548 : Evaluate and ignore s1 in case it had side-effects. */
11549 26 : return omit_one_operand_loc (loc, type, integer_zero_node, s1);
11550 :
11551 44 : if (p2[1] != '\0')
11552 : return NULL_TREE; /* Really call strpbrk. */
11553 :
11554 41 : fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11555 41 : if (!fn)
11556 : return NULL_TREE;
11557 :
11558 : /* New argument list transforming strpbrk(s1, s2) to
11559 : strchr(s1, s2[0]). */
11560 41 : return build_call_expr_loc (loc, fn, 2, s1,
11561 41 : build_int_cst (integer_type_node, p2[0]));
11562 : }
11563 :
11564 : /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11565 : to the call.
11566 :
11567 : Return NULL_TREE if no simplification was possible, otherwise return the
11568 : simplified form of the call as a tree.
11569 :
11570 : The simplified form may be a constant or other expression which
11571 : computes the same value, but in a more efficient manner (including
11572 : calls to other builtin functions).
11573 :
11574 : The call may contain arguments which need to be evaluated, but
11575 : which are not useful to determine the result of the call. In
11576 : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11577 : COMPOUND_EXPR will be an argument which must be evaluated.
11578 : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11579 : COMPOUND_EXPR in the chain will contain the tree for the simplified
11580 : form of the builtin function call. */
11581 :
11582 : static tree
11583 2560 : fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11584 : {
11585 2560 : if (!validate_arg (s1, POINTER_TYPE)
11586 2560 : || !validate_arg (s2, POINTER_TYPE))
11587 : return NULL_TREE;
11588 :
11589 2560 : if (!check_nul_terminated_array (expr, s1)
11590 2560 : || !check_nul_terminated_array (expr, s2))
11591 : return NULL_TREE;
11592 :
11593 2502 : const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11594 :
11595 : /* If either argument is "", return NULL_TREE. */
11596 2502 : if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11597 : /* Evaluate and ignore both arguments in case either one has
11598 : side-effects. */
11599 147 : return omit_two_operands_loc (loc, type, size_zero_node, s1, s2);
11600 : return NULL_TREE;
11601 : }
11602 :
11603 : /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11604 : to the call.
11605 :
11606 : Return NULL_TREE if no simplification was possible, otherwise return the
11607 : simplified form of the call as a tree.
11608 :
11609 : The simplified form may be a constant or other expression which
11610 : computes the same value, but in a more efficient manner (including
11611 : calls to other builtin functions).
11612 :
11613 : The call may contain arguments which need to be evaluated, but
11614 : which are not useful to determine the result of the call. In
11615 : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11616 : COMPOUND_EXPR will be an argument which must be evaluated.
11617 : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11618 : COMPOUND_EXPR in the chain will contain the tree for the simplified
11619 : form of the builtin function call. */
11620 :
11621 : static tree
11622 2465 : fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11623 : {
11624 2465 : if (!validate_arg (s1, POINTER_TYPE)
11625 2465 : || !validate_arg (s2, POINTER_TYPE))
11626 : return NULL_TREE;
11627 :
11628 2465 : if (!check_nul_terminated_array (expr, s1)
11629 2465 : || !check_nul_terminated_array (expr, s2))
11630 : return NULL_TREE;
11631 :
11632 : /* If the first argument is "", return NULL_TREE. */
11633 2407 : const char *p1 = c_getstr (s1);
11634 2407 : if (p1 && *p1 == '\0')
11635 : {
11636 : /* Evaluate and ignore argument s2 in case it has
11637 : side-effects. */
11638 65 : return omit_one_operand_loc (loc, type, size_zero_node, s2);
11639 : }
11640 :
11641 : /* If the second argument is "", return __builtin_strlen(s1). */
11642 2342 : const char *p2 = c_getstr (s2);
11643 2342 : if (p2 && *p2 == '\0')
11644 : {
11645 81 : tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11646 :
11647 : /* If the replacement _DECL isn't initialized, don't do the
11648 : transformation. */
11649 81 : if (!fn)
11650 : return NULL_TREE;
11651 :
11652 81 : return fold_convert_loc (loc, type,
11653 81 : build_call_expr_loc (loc, fn, 1, s1));
11654 : }
11655 : return NULL_TREE;
11656 : }
11657 :
11658 : /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11659 : produced. False otherwise. This is done so that we don't output the error
11660 : or warning twice or three times. */
11661 :
11662 : bool
11663 42213 : fold_builtin_next_arg (tree exp, bool va_start_p)
11664 : {
11665 42213 : tree fntype = TREE_TYPE (current_function_decl);
11666 42213 : int nargs = call_expr_nargs (exp);
11667 42213 : tree arg;
11668 : /* There is good chance the current input_location points inside the
11669 : definition of the va_start macro (perhaps on the token for
11670 : builtin) in a system header, so warnings will not be emitted.
11671 : Use the location in real source code. */
11672 42213 : location_t current_location =
11673 42213 : linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11674 : NULL);
11675 :
11676 42213 : if (!stdarg_p (fntype))
11677 : {
11678 8 : error ("%<va_start%> used in function with fixed arguments");
11679 8 : return true;
11680 : }
11681 :
11682 42205 : if (va_start_p)
11683 : {
11684 42047 : if (va_start_p && (nargs != 2))
11685 : {
11686 0 : error ("wrong number of arguments to function %<va_start%>");
11687 0 : return true;
11688 : }
11689 42047 : arg = CALL_EXPR_ARG (exp, 1);
11690 : }
11691 : /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11692 : when we checked the arguments and if needed issued a warning. */
11693 : else
11694 : {
11695 158 : if (nargs == 0)
11696 : {
11697 : /* Evidently an out of date version of <stdarg.h>; can't validate
11698 : va_start's second argument, but can still work as intended. */
11699 0 : warning_at (current_location,
11700 0 : OPT_Wvarargs,
11701 : "%<__builtin_next_arg%> called without an argument");
11702 0 : return true;
11703 : }
11704 158 : else if (nargs > 1)
11705 : {
11706 0 : error ("wrong number of arguments to function %<__builtin_next_arg%>");
11707 0 : return true;
11708 : }
11709 158 : arg = CALL_EXPR_ARG (exp, 0);
11710 : }
11711 :
11712 42205 : if (TREE_CODE (arg) == SSA_NAME
11713 42205 : && SSA_NAME_VAR (arg))
11714 : arg = SSA_NAME_VAR (arg);
11715 :
11716 : /* We destructively modify the call to be __builtin_va_start (ap, 0)
11717 : or __builtin_next_arg (0) the first time we see it, after checking
11718 : the arguments and if needed issuing a warning. */
11719 42205 : if (!integer_zerop (arg))
11720 : {
11721 7031 : tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11722 :
11723 : /* Strip off all nops for the sake of the comparison. This
11724 : is not quite the same as STRIP_NOPS. It does more.
11725 : We must also strip off INDIRECT_EXPR for C++ reference
11726 : parameters. */
11727 14064 : while (CONVERT_EXPR_P (arg)
11728 14075 : || INDIRECT_REF_P (arg))
11729 11 : arg = TREE_OPERAND (arg, 0);
11730 7031 : if (arg != last_parm)
11731 : {
11732 : /* FIXME: Sometimes with the tree optimizers we can get the
11733 : not the last argument even though the user used the last
11734 : argument. We just warn and set the arg to be the last
11735 : argument so that we will get wrong-code because of
11736 : it. */
11737 12 : warning_at (current_location,
11738 12 : OPT_Wvarargs,
11739 : "second parameter of %<va_start%> not last named argument");
11740 : }
11741 :
11742 : /* Undefined by C99 7.15.1.4p4 (va_start):
11743 : "If the parameter parmN is declared with the register storage
11744 : class, with a function or array type, or with a type that is
11745 : not compatible with the type that results after application of
11746 : the default argument promotions, the behavior is undefined."
11747 : */
11748 7019 : else if (DECL_REGISTER (arg))
11749 : {
11750 12 : warning_at (current_location,
11751 12 : OPT_Wvarargs,
11752 : "undefined behavior when second parameter of "
11753 : "%<va_start%> is declared with %<register%> storage");
11754 : }
11755 :
11756 : /* We want to verify the second parameter just once before the tree
11757 : optimizers are run and then avoid keeping it in the tree,
11758 : as otherwise we could warn even for correct code like:
11759 : void foo (int i, ...)
11760 : { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11761 7031 : if (va_start_p)
11762 7029 : CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11763 : else
11764 2 : CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11765 : }
11766 : return false;
11767 : }
11768 :
11769 :
11770 : /* Expand a call EXP to __builtin_object_size. */
11771 :
11772 : static rtx
11773 620 : expand_builtin_object_size (tree exp)
11774 : {
11775 620 : tree ost;
11776 620 : int object_size_type;
11777 620 : tree fndecl = get_callee_fndecl (exp);
11778 :
11779 620 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11780 : {
11781 0 : error ("first argument of %qD must be a pointer, second integer constant",
11782 : fndecl);
11783 0 : expand_builtin_trap ();
11784 0 : return const0_rtx;
11785 : }
11786 :
11787 620 : ost = CALL_EXPR_ARG (exp, 1);
11788 620 : STRIP_NOPS (ost);
11789 :
11790 620 : if (TREE_CODE (ost) != INTEGER_CST
11791 620 : || tree_int_cst_sgn (ost) < 0
11792 1240 : || compare_tree_int (ost, 3) > 0)
11793 : {
11794 0 : error ("last argument of %qD is not integer constant between 0 and 3",
11795 : fndecl);
11796 0 : expand_builtin_trap ();
11797 0 : return const0_rtx;
11798 : }
11799 :
11800 620 : object_size_type = tree_to_shwi (ost);
11801 :
11802 620 : return object_size_type < 2 ? constm1_rtx : const0_rtx;
11803 : }
11804 :
11805 : /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11806 : FCODE is the BUILT_IN_* to use.
11807 : Return NULL_RTX if we failed; the caller should emit a normal call,
11808 : otherwise try to get the result in TARGET, if convenient (and in
11809 : mode MODE if that's convenient). */
11810 :
11811 : static rtx
11812 837 : expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11813 : enum built_in_function fcode)
11814 : {
11815 1410 : if (!validate_arglist (exp,
11816 : POINTER_TYPE,
11817 : fcode == BUILT_IN_MEMSET_CHK
11818 : ? INTEGER_TYPE : POINTER_TYPE,
11819 : INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11820 : return NULL_RTX;
11821 :
11822 837 : tree dest = CALL_EXPR_ARG (exp, 0);
11823 837 : tree src = CALL_EXPR_ARG (exp, 1);
11824 837 : tree len = CALL_EXPR_ARG (exp, 2);
11825 837 : tree size = CALL_EXPR_ARG (exp, 3);
11826 :
11827 : /* FIXME: Set access mode to write only for memset et al. */
11828 837 : bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
11829 : /*srcstr=*/NULL_TREE, size, access_read_write);
11830 :
11831 837 : if (!tree_fits_uhwi_p (size))
11832 : return NULL_RTX;
11833 :
11834 627 : if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11835 : {
11836 : /* Avoid transforming the checking call to an ordinary one when
11837 : an overflow has been detected or when the call couldn't be
11838 : validated because the size is not constant. */
11839 186 : if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
11840 : return NULL_RTX;
11841 :
11842 0 : tree fn = NULL_TREE;
11843 : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11844 : mem{cpy,pcpy,move,set} is available. */
11845 0 : switch (fcode)
11846 : {
11847 0 : case BUILT_IN_MEMCPY_CHK:
11848 0 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11849 0 : break;
11850 0 : case BUILT_IN_MEMPCPY_CHK:
11851 0 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11852 0 : break;
11853 0 : case BUILT_IN_MEMMOVE_CHK:
11854 0 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11855 0 : break;
11856 0 : case BUILT_IN_MEMSET_CHK:
11857 0 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11858 0 : break;
11859 : default:
11860 : break;
11861 : }
11862 :
11863 0 : if (! fn)
11864 : return NULL_RTX;
11865 :
11866 0 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11867 0 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11868 0 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11869 0 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11870 : }
11871 441 : else if (fcode == BUILT_IN_MEMSET_CHK)
11872 : return NULL_RTX;
11873 : else
11874 : {
11875 293 : unsigned int dest_align = get_pointer_alignment (dest);
11876 :
11877 : /* If DEST is not a pointer type, call the normal function. */
11878 293 : if (dest_align == 0)
11879 : return NULL_RTX;
11880 :
11881 : /* If SRC and DEST are the same (and not volatile), do nothing. */
11882 293 : if (operand_equal_p (src, dest, 0))
11883 : {
11884 0 : tree expr;
11885 :
11886 0 : if (fcode != BUILT_IN_MEMPCPY_CHK)
11887 : {
11888 : /* Evaluate and ignore LEN in case it has side-effects. */
11889 0 : expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11890 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
11891 : }
11892 :
11893 0 : expr = fold_build_pointer_plus (dest, len);
11894 0 : return expand_expr (expr, target, mode, EXPAND_NORMAL);
11895 : }
11896 :
11897 : /* __memmove_chk special case. */
11898 293 : if (fcode == BUILT_IN_MEMMOVE_CHK)
11899 : {
11900 79 : unsigned int src_align = get_pointer_alignment (src);
11901 :
11902 79 : if (src_align == 0)
11903 : return NULL_RTX;
11904 :
11905 : /* If src is categorized for a readonly section we can use
11906 : normal __memcpy_chk. */
11907 79 : if (readonly_data_expr (src))
11908 : {
11909 15 : tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11910 15 : if (!fn)
11911 : return NULL_RTX;
11912 15 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11913 : dest, src, len, size);
11914 15 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11915 15 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11916 15 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11917 : }
11918 : }
11919 : return NULL_RTX;
11920 : }
11921 : }
11922 :
11923 : /* Emit warning if a buffer overflow is detected at compile time. */
11924 :
11925 : static void
11926 1086 : maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11927 : {
11928 : /* The source string. */
11929 1086 : tree srcstr = NULL_TREE;
11930 : /* The size of the destination object returned by __builtin_object_size. */
11931 1086 : tree objsize = NULL_TREE;
11932 : /* The string that is being concatenated with (as in __strcat_chk)
11933 : or null if it isn't. */
11934 1086 : tree catstr = NULL_TREE;
11935 : /* The maximum length of the source sequence in a bounded operation
11936 : (such as __strncat_chk) or null if the operation isn't bounded
11937 : (such as __strcat_chk). */
11938 1086 : tree maxread = NULL_TREE;
11939 : /* The exact size of the access (such as in __strncpy_chk). */
11940 1086 : tree size = NULL_TREE;
11941 : /* The access by the function that's checked. Except for snprintf
11942 : both writing and reading is checked. */
11943 1086 : access_mode mode = access_read_write;
11944 :
11945 1086 : switch (fcode)
11946 : {
11947 275 : case BUILT_IN_STRCPY_CHK:
11948 275 : case BUILT_IN_STPCPY_CHK:
11949 275 : srcstr = CALL_EXPR_ARG (exp, 1);
11950 275 : objsize = CALL_EXPR_ARG (exp, 2);
11951 275 : break;
11952 :
11953 151 : case BUILT_IN_STRCAT_CHK:
11954 : /* For __strcat_chk the warning will be emitted only if overflowing
11955 : by at least strlen (dest) + 1 bytes. */
11956 151 : catstr = CALL_EXPR_ARG (exp, 0);
11957 151 : srcstr = CALL_EXPR_ARG (exp, 1);
11958 151 : objsize = CALL_EXPR_ARG (exp, 2);
11959 151 : break;
11960 :
11961 108 : case BUILT_IN_STRNCAT_CHK:
11962 108 : catstr = CALL_EXPR_ARG (exp, 0);
11963 108 : srcstr = CALL_EXPR_ARG (exp, 1);
11964 108 : maxread = CALL_EXPR_ARG (exp, 2);
11965 108 : objsize = CALL_EXPR_ARG (exp, 3);
11966 108 : break;
11967 :
11968 243 : case BUILT_IN_STRNCPY_CHK:
11969 243 : case BUILT_IN_STPNCPY_CHK:
11970 243 : srcstr = CALL_EXPR_ARG (exp, 1);
11971 243 : size = CALL_EXPR_ARG (exp, 2);
11972 243 : objsize = CALL_EXPR_ARG (exp, 3);
11973 243 : break;
11974 :
11975 309 : case BUILT_IN_SNPRINTF_CHK:
11976 309 : case BUILT_IN_VSNPRINTF_CHK:
11977 309 : maxread = CALL_EXPR_ARG (exp, 1);
11978 309 : objsize = CALL_EXPR_ARG (exp, 3);
11979 : /* The only checked access the write to the destination. */
11980 309 : mode = access_write_only;
11981 309 : break;
11982 0 : default:
11983 0 : gcc_unreachable ();
11984 : }
11985 :
11986 1086 : if (catstr && maxread)
11987 : {
11988 : /* Check __strncat_chk. There is no way to determine the length
11989 : of the string to which the source string is being appended so
11990 : just warn when the length of the source string is not known. */
11991 108 : check_strncat_sizes (exp, objsize);
11992 108 : return;
11993 : }
11994 :
11995 978 : check_access (exp, size, maxread, srcstr, objsize, mode);
11996 : }
11997 :
11998 : /* Emit warning if a buffer overflow is detected at compile time
11999 : in __sprintf_chk/__vsprintf_chk calls. */
12000 :
12001 : static void
12002 1328 : maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12003 : {
12004 1328 : tree size, len, fmt;
12005 1328 : const char *fmt_str;
12006 1328 : int nargs = call_expr_nargs (exp);
12007 :
12008 : /* Verify the required arguments in the original call. */
12009 :
12010 1328 : if (nargs < 4)
12011 : return;
12012 1328 : size = CALL_EXPR_ARG (exp, 2);
12013 1328 : fmt = CALL_EXPR_ARG (exp, 3);
12014 :
12015 1328 : if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12016 : return;
12017 :
12018 : /* Check whether the format is a literal string constant. */
12019 1307 : fmt_str = c_getstr (fmt);
12020 1307 : if (fmt_str == NULL)
12021 : return;
12022 :
12023 1271 : if (!init_target_chars ())
12024 : return;
12025 :
12026 : /* If the format doesn't contain % args or %%, we know its size. */
12027 1271 : if (strchr (fmt_str, target_percent) == 0)
12028 22 : len = build_int_cstu (size_type_node, strlen (fmt_str));
12029 : /* If the format is "%s" and first ... argument is a string literal,
12030 : we know it too. */
12031 1249 : else if (fcode == BUILT_IN_SPRINTF_CHK
12032 1095 : && strcmp (fmt_str, target_percent_s) == 0)
12033 : {
12034 49 : tree arg;
12035 :
12036 49 : if (nargs < 5)
12037 : return;
12038 49 : arg = CALL_EXPR_ARG (exp, 4);
12039 49 : if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12040 : return;
12041 :
12042 45 : len = c_strlen (arg, 1);
12043 45 : if (!len || ! tree_fits_uhwi_p (len))
12044 : return;
12045 : }
12046 : else
12047 : return;
12048 :
12049 : /* Add one for the terminating nul. */
12050 34 : len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
12051 :
12052 34 : check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
12053 : access_write_only);
12054 : }
12055 :
12056 : /* Fold a call to __builtin_object_size with arguments PTR and OST,
12057 : if possible. */
12058 :
12059 : static tree
12060 201177 : fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
12061 : {
12062 201177 : tree bytes;
12063 201177 : int object_size_type;
12064 :
12065 201177 : if (!validate_arg (ptr, POINTER_TYPE)
12066 201177 : || !validate_arg (ost, INTEGER_TYPE))
12067 : return NULL_TREE;
12068 :
12069 201177 : STRIP_NOPS (ost);
12070 :
12071 201177 : if (TREE_CODE (ost) != INTEGER_CST
12072 201177 : || tree_int_cst_sgn (ost) < 0
12073 402354 : || compare_tree_int (ost, 3) > 0)
12074 : return NULL_TREE;
12075 :
12076 201177 : object_size_type = tree_to_shwi (ost);
12077 :
12078 : /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12079 : if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12080 : and (size_t) 0 for types 2 and 3. */
12081 201177 : if (TREE_SIDE_EFFECTS (ptr))
12082 603 : return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12083 :
12084 200574 : if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
12085 67524 : object_size_type |= OST_DYNAMIC;
12086 :
12087 200574 : if (TREE_CODE (ptr) == ADDR_EXPR)
12088 : {
12089 12244 : compute_builtin_object_size (ptr, object_size_type, &bytes);
12090 12244 : if ((object_size_type & OST_DYNAMIC)
12091 10892 : || int_fits_type_p (bytes, size_type_node))
12092 12244 : return fold_convert (size_type_node, bytes);
12093 : }
12094 188330 : else if (TREE_CODE (ptr) == SSA_NAME)
12095 : {
12096 : /* If object size is not known yet, delay folding until
12097 : later. Maybe subsequent passes will help determining
12098 : it. */
12099 102272 : if (compute_builtin_object_size (ptr, object_size_type, &bytes)
12100 102272 : && ((object_size_type & OST_DYNAMIC)
12101 2538 : || int_fits_type_p (bytes, size_type_node)))
12102 4808 : return fold_convert (size_type_node, bytes);
12103 : }
12104 :
12105 : return NULL_TREE;
12106 : }
12107 :
12108 : /* Builtins with folding operations that operate on "..." arguments
12109 : need special handling; we need to store the arguments in a convenient
12110 : data structure before attempting any folding. Fortunately there are
12111 : only a few builtins that fall into this category. FNDECL is the
12112 : function, EXP is the CALL_EXPR for the call. */
12113 :
12114 : static tree
12115 2681475 : fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
12116 : {
12117 2681475 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
12118 2681475 : tree ret = NULL_TREE;
12119 :
12120 2681475 : switch (fcode)
12121 : {
12122 104220 : case BUILT_IN_FPCLASSIFY:
12123 104220 : ret = fold_builtin_fpclassify (loc, args, nargs);
12124 104220 : break;
12125 :
12126 55 : case BUILT_IN_ADDC:
12127 55 : case BUILT_IN_ADDCL:
12128 55 : case BUILT_IN_ADDCLL:
12129 55 : case BUILT_IN_SUBC:
12130 55 : case BUILT_IN_SUBCL:
12131 55 : case BUILT_IN_SUBCLL:
12132 55 : return fold_builtin_addc_subc (loc, fcode, args);
12133 :
12134 : default:
12135 : break;
12136 : }
12137 104220 : if (ret)
12138 : {
12139 104220 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
12140 104220 : SET_EXPR_LOCATION (ret, loc);
12141 104220 : suppress_warning (ret);
12142 104220 : return ret;
12143 : }
12144 : return NULL_TREE;
12145 : }
12146 :
12147 : /* Initialize format string characters in the target charset. */
12148 :
12149 : bool
12150 294864 : init_target_chars (void)
12151 : {
12152 294864 : static bool init;
12153 294864 : if (!init)
12154 : {
12155 117562 : target_newline = lang_hooks.to_target_charset ('\n');
12156 117562 : target_percent = lang_hooks.to_target_charset ('%');
12157 117562 : target_c = lang_hooks.to_target_charset ('c');
12158 117562 : target_s = lang_hooks.to_target_charset ('s');
12159 117562 : if (target_newline == 0 || target_percent == 0 || target_c == 0
12160 117562 : || target_s == 0)
12161 : return false;
12162 :
12163 117562 : target_percent_c[0] = target_percent;
12164 117562 : target_percent_c[1] = target_c;
12165 117562 : target_percent_c[2] = '\0';
12166 :
12167 117562 : target_percent_s[0] = target_percent;
12168 117562 : target_percent_s[1] = target_s;
12169 117562 : target_percent_s[2] = '\0';
12170 :
12171 117562 : target_percent_s_newline[0] = target_percent;
12172 117562 : target_percent_s_newline[1] = target_s;
12173 117562 : target_percent_s_newline[2] = target_newline;
12174 117562 : target_percent_s_newline[3] = '\0';
12175 :
12176 117562 : init = true;
12177 : }
12178 : return true;
12179 : }
12180 :
12181 : /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12182 : and no overflow/underflow occurred. INEXACT is true if M was not
12183 : exactly calculated. TYPE is the tree type for the result. This
12184 : function assumes that you cleared the MPFR flags and then
12185 : calculated M to see if anything subsequently set a flag prior to
12186 : entering this function. Return NULL_TREE if any checks fail. */
12187 :
12188 : static tree
12189 2979 : do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12190 : {
12191 : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12192 : overflow/underflow occurred. If -frounding-math, proceed iff the
12193 : result of calling FUNC was exact. */
12194 4832 : if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12195 4832 : && (!flag_rounding_math || !inexact))
12196 : {
12197 1853 : REAL_VALUE_TYPE rr;
12198 :
12199 1853 : real_from_mpfr (&rr, m, type, MPFR_RNDN);
12200 : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12201 : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12202 : but the mpfr_t is not, then we underflowed in the
12203 : conversion. */
12204 1853 : if (real_isfinite (&rr)
12205 1853 : && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12206 : {
12207 1853 : REAL_VALUE_TYPE rmode;
12208 :
12209 1853 : real_convert (&rmode, TYPE_MODE (type), &rr);
12210 : /* Proceed iff the specified mode can hold the value. */
12211 1853 : if (real_identical (&rmode, &rr))
12212 1853 : return build_real (type, rmode);
12213 : }
12214 : }
12215 : return NULL_TREE;
12216 : }
12217 :
12218 : /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12219 : number and no overflow/underflow occurred. INEXACT is true if M
12220 : was not exactly calculated. TYPE is the tree type for the result.
12221 : This function assumes that you cleared the MPFR flags and then
12222 : calculated M to see if anything subsequently set a flag prior to
12223 : entering this function. Return NULL_TREE if any checks fail, if
12224 : FORCE_CONVERT is true, then bypass the checks. */
12225 :
12226 : static tree
12227 4207 : do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
12228 : {
12229 : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12230 : overflow/underflow occurred. If -frounding-math, proceed iff the
12231 : result of calling FUNC was exact. */
12232 4207 : if (force_convert
12233 4207 : || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
12234 3888 : && !mpfr_overflow_p () && !mpfr_underflow_p ()
12235 3888 : && (!flag_rounding_math || !inexact)))
12236 : {
12237 4028 : REAL_VALUE_TYPE re, im;
12238 :
12239 4028 : real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
12240 4028 : real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
12241 : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12242 : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12243 : but the mpfr_t is not, then we underflowed in the
12244 : conversion. */
12245 4028 : if (force_convert
12246 4028 : || (real_isfinite (&re) && real_isfinite (&im)
12247 3888 : && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
12248 3888 : && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
12249 : {
12250 4028 : REAL_VALUE_TYPE re_mode, im_mode;
12251 :
12252 4028 : real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
12253 4028 : real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
12254 : /* Proceed iff the specified mode can hold the value. */
12255 4028 : if (force_convert
12256 4028 : || (real_identical (&re_mode, &re)
12257 3888 : && real_identical (&im_mode, &im)))
12258 4028 : return build_complex (type, build_real (TREE_TYPE (type), re_mode),
12259 8056 : build_real (TREE_TYPE (type), im_mode));
12260 : }
12261 : }
12262 : return NULL_TREE;
12263 : }
12264 :
12265 : /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12266 : the pointer *(ARG_QUO) and return the result. The type is taken
12267 : from the type of ARG0 and is used for setting the precision of the
12268 : calculation and results. */
12269 :
12270 : static tree
12271 99800 : do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12272 : {
12273 99800 : tree const type = TREE_TYPE (arg0);
12274 99800 : tree result = NULL_TREE;
12275 :
12276 99800 : STRIP_NOPS (arg0);
12277 99800 : STRIP_NOPS (arg1);
12278 :
12279 : /* To proceed, MPFR must exactly represent the target floating point
12280 : format, which only happens when the target base equals two. */
12281 99800 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12282 99800 : && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12283 102105 : && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12284 : {
12285 2305 : const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12286 2305 : const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12287 :
12288 2305 : if (real_isfinite (ra0) && real_isfinite (ra1))
12289 : {
12290 2305 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12291 2305 : const int prec = fmt->p;
12292 2305 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12293 2305 : tree result_rem;
12294 2305 : long integer_quo;
12295 2305 : mpfr_t m0, m1;
12296 :
12297 2305 : mpfr_inits2 (prec, m0, m1, NULL);
12298 2305 : mpfr_from_real (m0, ra0, MPFR_RNDN);
12299 2305 : mpfr_from_real (m1, ra1, MPFR_RNDN);
12300 2305 : mpfr_clear_flags ();
12301 2305 : mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12302 : /* Remquo is independent of the rounding mode, so pass
12303 : inexact=0 to do_mpfr_ckconv(). */
12304 2305 : result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12305 2305 : mpfr_clears (m0, m1, NULL);
12306 2305 : if (result_rem)
12307 : {
12308 : /* MPFR calculates quo in the host's long so it may
12309 : return more bits in quo than the target int can hold
12310 : if sizeof(host long) > sizeof(target int). This can
12311 : happen even for native compilers in LP64 mode. In
12312 : these cases, modulo the quo value with the largest
12313 : number that the target int can hold while leaving one
12314 : bit for the sign. */
12315 1179 : if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12316 1179 : integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12317 :
12318 : /* Dereference the quo pointer argument. */
12319 1179 : arg_quo = build_fold_indirect_ref (arg_quo);
12320 : /* Proceed iff a valid pointer type was passed in. */
12321 1179 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12322 : {
12323 : /* Set the value. */
12324 1179 : tree result_quo
12325 1179 : = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12326 : build_int_cst (TREE_TYPE (arg_quo),
12327 : integer_quo));
12328 1179 : TREE_SIDE_EFFECTS (result_quo) = 1;
12329 : /* Combine the quo assignment with the rem. */
12330 1179 : result = fold_build2 (COMPOUND_EXPR, type,
12331 : result_quo, result_rem);
12332 1179 : suppress_warning (result, OPT_Wunused_value);
12333 1179 : result = non_lvalue (result);
12334 : }
12335 : }
12336 : }
12337 : }
12338 99800 : return result;
12339 : }
12340 :
12341 : /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12342 : resulting value as a tree with type TYPE. The mpfr precision is
12343 : set to the precision of TYPE. We assume that this mpfr function
12344 : returns zero if the result could be calculated exactly within the
12345 : requested precision. In addition, the integer pointer represented
12346 : by ARG_SG will be dereferenced and set to the appropriate signgam
12347 : (-1,1) value. */
12348 :
12349 : static tree
12350 6523 : do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12351 : {
12352 6523 : tree result = NULL_TREE;
12353 :
12354 6523 : STRIP_NOPS (arg);
12355 :
12356 : /* To proceed, MPFR must exactly represent the target floating point
12357 : format, which only happens when the target base equals two. Also
12358 : verify ARG is a constant and that ARG_SG is an int pointer. */
12359 6523 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12360 6523 : && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12361 6470 : && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12362 12993 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12363 : {
12364 6470 : const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12365 :
12366 : /* In addition to NaN and Inf, the argument cannot be zero or a
12367 : negative integer. */
12368 6470 : if (real_isfinite (ra)
12369 6470 : && ra->cl != rvc_zero
12370 12940 : && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12371 : {
12372 674 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12373 674 : const int prec = fmt->p;
12374 674 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12375 674 : int inexact, sg;
12376 674 : tree result_lg;
12377 :
12378 674 : auto_mpfr m (prec);
12379 674 : mpfr_from_real (m, ra, MPFR_RNDN);
12380 674 : mpfr_clear_flags ();
12381 674 : inexact = mpfr_lgamma (m, &sg, m, rnd);
12382 674 : result_lg = do_mpfr_ckconv (m, type, inexact);
12383 674 : if (result_lg)
12384 : {
12385 674 : tree result_sg;
12386 :
12387 : /* Dereference the arg_sg pointer argument. */
12388 674 : arg_sg = build_fold_indirect_ref (arg_sg);
12389 : /* Assign the signgam value into *arg_sg. */
12390 674 : result_sg = fold_build2 (MODIFY_EXPR,
12391 : TREE_TYPE (arg_sg), arg_sg,
12392 : build_int_cst (TREE_TYPE (arg_sg), sg));
12393 674 : TREE_SIDE_EFFECTS (result_sg) = 1;
12394 : /* Combine the signgam assignment with the lgamma result. */
12395 674 : result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12396 : result_sg, result_lg));
12397 : }
12398 674 : }
12399 : }
12400 :
12401 6523 : return result;
12402 : }
12403 :
12404 : /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12405 : mpc function FUNC on it and return the resulting value as a tree
12406 : with type TYPE. The mpfr precision is set to the precision of
12407 : TYPE. We assume that function FUNC returns zero if the result
12408 : could be calculated exactly within the requested precision. If
12409 : DO_NONFINITE is true, then fold expressions containing Inf or NaN
12410 : in the arguments and/or results. */
12411 :
12412 : tree
12413 4559 : do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12414 : int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12415 : {
12416 4559 : tree result = NULL_TREE;
12417 :
12418 4559 : STRIP_NOPS (arg0);
12419 4559 : STRIP_NOPS (arg1);
12420 :
12421 : /* To proceed, MPFR must exactly represent the target floating point
12422 : format, which only happens when the target base equals two. */
12423 4559 : if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12424 4559 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
12425 4559 : && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12426 4559 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
12427 9118 : && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12428 : {
12429 4559 : const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12430 4559 : const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12431 4559 : const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12432 4559 : const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12433 :
12434 4559 : if (do_nonfinite
12435 4559 : || (real_isfinite (re0) && real_isfinite (im0)
12436 4095 : && real_isfinite (re1) && real_isfinite (im1)))
12437 : {
12438 4207 : const struct real_format *const fmt =
12439 4207 : REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12440 4207 : const int prec = fmt->p;
12441 4207 : const mpfr_rnd_t rnd = fmt->round_towards_zero
12442 4207 : ? MPFR_RNDZ : MPFR_RNDN;
12443 4207 : const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12444 4207 : int inexact;
12445 4207 : mpc_t m0, m1;
12446 :
12447 4207 : mpc_init2 (m0, prec);
12448 4207 : mpc_init2 (m1, prec);
12449 4207 : mpfr_from_real (mpc_realref (m0), re0, rnd);
12450 4207 : mpfr_from_real (mpc_imagref (m0), im0, rnd);
12451 4207 : mpfr_from_real (mpc_realref (m1), re1, rnd);
12452 4207 : mpfr_from_real (mpc_imagref (m1), im1, rnd);
12453 4207 : mpfr_clear_flags ();
12454 4207 : inexact = func (m0, m0, m1, crnd);
12455 4207 : result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12456 4207 : mpc_clear (m0);
12457 4207 : mpc_clear (m1);
12458 : }
12459 : }
12460 :
12461 4559 : return result;
12462 : }
12463 :
12464 : /* A wrapper function for builtin folding that prevents warnings for
12465 : "statement without effect" and the like, caused by removing the
12466 : call node earlier than the warning is generated. */
12467 :
12468 : tree
12469 6274944 : fold_call_stmt (gcall *stmt, bool ignore)
12470 : {
12471 6274944 : tree ret = NULL_TREE;
12472 6274944 : tree fndecl = gimple_call_fndecl (stmt);
12473 6274944 : location_t loc = gimple_location (stmt);
12474 6274944 : if (fndecl && fndecl_built_in_p (fndecl)
12475 12549888 : && !gimple_call_va_arg_pack_p (stmt))
12476 : {
12477 6274878 : int nargs = gimple_call_num_args (stmt);
12478 6274878 : tree *args = (nargs > 0
12479 6274878 : ? gimple_call_arg_ptr (stmt, 0)
12480 : : &error_mark_node);
12481 :
12482 6274878 : if (avoid_folding_inline_builtin (fndecl))
12483 : return NULL_TREE;
12484 6274878 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12485 : {
12486 0 : return targetm.fold_builtin (fndecl, nargs, args, ignore);
12487 : }
12488 : else
12489 : {
12490 6274878 : ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
12491 6274878 : if (ret)
12492 : {
12493 : /* Propagate location information from original call to
12494 : expansion of builtin. Otherwise things like
12495 : maybe_emit_chk_warning, that operate on the expansion
12496 : of a builtin, will use the wrong location information. */
12497 5158 : if (gimple_has_location (stmt))
12498 : {
12499 5157 : tree realret = ret;
12500 5157 : if (TREE_CODE (ret) == NOP_EXPR)
12501 5157 : realret = TREE_OPERAND (ret, 0);
12502 5157 : if (CAN_HAVE_LOCATION_P (realret)
12503 9444 : && !EXPR_HAS_LOCATION (realret))
12504 6 : SET_EXPR_LOCATION (realret, loc);
12505 : return realret;
12506 : }
12507 : return ret;
12508 : }
12509 : }
12510 : }
12511 : return NULL_TREE;
12512 : }
12513 :
12514 : /* Look up the function in builtin_decl that corresponds to DECL
12515 : and set ASMSPEC as its user assembler name. DECL must be a
12516 : function decl that declares a builtin. */
12517 :
12518 : void
12519 154394 : set_builtin_user_assembler_name (tree decl, const char *asmspec)
12520 : {
12521 154394 : gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
12522 : && asmspec != 0);
12523 :
12524 154394 : tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12525 154394 : set_user_assembler_name (builtin, asmspec);
12526 :
12527 154394 : if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
12528 154394 : && INT_TYPE_SIZE < BITS_PER_WORD)
12529 : {
12530 1 : scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
12531 1 : set_user_assembler_libfunc ("ffs", asmspec);
12532 1 : set_optab_libfunc (ffs_optab, mode, "ffs");
12533 : }
12534 154394 : }
12535 :
12536 : /* Return true if DECL is a builtin that expands to a constant or similarly
12537 : simple code. */
12538 : bool
12539 33011960 : is_simple_builtin (tree decl)
12540 : {
12541 33011960 : if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
12542 31688399 : switch (DECL_FUNCTION_CODE (decl))
12543 : {
12544 : /* Builtins that expand to constants. */
12545 : case BUILT_IN_CONSTANT_P:
12546 : case BUILT_IN_EXPECT:
12547 : case BUILT_IN_OBJECT_SIZE:
12548 : case BUILT_IN_UNREACHABLE:
12549 : /* Simple register moves or loads from stack. */
12550 : case BUILT_IN_ASSUME_ALIGNED:
12551 : case BUILT_IN_RETURN_ADDRESS:
12552 : case BUILT_IN_EXTRACT_RETURN_ADDR:
12553 : case BUILT_IN_FROB_RETURN_ADDR:
12554 : case BUILT_IN_RETURN:
12555 : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12556 : case BUILT_IN_FRAME_ADDRESS:
12557 : case BUILT_IN_VA_END:
12558 : case BUILT_IN_STACK_SAVE:
12559 : case BUILT_IN_STACK_RESTORE:
12560 : case BUILT_IN_DWARF_CFA:
12561 : case BUILT_IN_CALL_CODE_ADDRESS:
12562 : case BUILT_IN_CALL_STATIC_CHAIN:
12563 : /* Exception state returns or moves registers around. */
12564 : case BUILT_IN_EH_FILTER:
12565 : case BUILT_IN_EH_POINTER:
12566 : case BUILT_IN_EH_COPY_VALUES:
12567 : return true;
12568 :
12569 28534950 : default:
12570 28534950 : return false;
12571 : }
12572 :
12573 : return false;
12574 : }
12575 :
12576 : /* Return true if DECL is a builtin that is not expensive, i.e., they are
12577 : most probably expanded inline into reasonably simple code. This is a
12578 : superset of is_simple_builtin. */
12579 : bool
12580 19800986 : is_inexpensive_builtin (tree decl)
12581 : {
12582 19800986 : if (!decl)
12583 : return false;
12584 19786457 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12585 : return true;
12586 18436530 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12587 15440987 : switch (DECL_FUNCTION_CODE (decl))
12588 : {
12589 : case BUILT_IN_ABS:
12590 : CASE_BUILT_IN_ALLOCA:
12591 : CASE_INTSZ_FN (BUILT_IN_BSWAP):
12592 : CASE_INTSZ_FN (BUILT_IN_BITREVERSE):
12593 : /* Not part of CASE_INTSZ_FN: there is no BUILT_IN_BSWAP8. */
12594 : case BUILT_IN_BITREVERSE8:
12595 : case BUILT_IN_CLZ:
12596 : case BUILT_IN_CLZIMAX:
12597 : case BUILT_IN_CLZL:
12598 : case BUILT_IN_CLZLL:
12599 : case BUILT_IN_CTZ:
12600 : case BUILT_IN_CTZIMAX:
12601 : case BUILT_IN_CTZL:
12602 : case BUILT_IN_CTZLL:
12603 : case BUILT_IN_FFS:
12604 : case BUILT_IN_FFSIMAX:
12605 : case BUILT_IN_FFSL:
12606 : case BUILT_IN_FFSLL:
12607 : case BUILT_IN_IMAXABS:
12608 : case BUILT_IN_FINITE:
12609 : case BUILT_IN_FINITEF:
12610 : case BUILT_IN_FINITEL:
12611 : case BUILT_IN_FINITED32:
12612 : case BUILT_IN_FINITED64:
12613 : case BUILT_IN_FINITED128:
12614 : case BUILT_IN_FPCLASSIFY:
12615 : case BUILT_IN_ISFINITE:
12616 : case BUILT_IN_ISINF_SIGN:
12617 : case BUILT_IN_ISINF:
12618 : case BUILT_IN_ISINFF:
12619 : case BUILT_IN_ISINFL:
12620 : case BUILT_IN_ISINFD32:
12621 : case BUILT_IN_ISINFD64:
12622 : case BUILT_IN_ISINFD128:
12623 : case BUILT_IN_ISNAN:
12624 : case BUILT_IN_ISNANF:
12625 : case BUILT_IN_ISNANL:
12626 : case BUILT_IN_ISNAND32:
12627 : case BUILT_IN_ISNAND64:
12628 : case BUILT_IN_ISNAND128:
12629 : case BUILT_IN_ISNORMAL:
12630 : case BUILT_IN_ISGREATER:
12631 : case BUILT_IN_ISGREATEREQUAL:
12632 : case BUILT_IN_ISLESS:
12633 : case BUILT_IN_ISLESSEQUAL:
12634 : case BUILT_IN_ISLESSGREATER:
12635 : case BUILT_IN_ISUNORDERED:
12636 : case BUILT_IN_ISEQSIG:
12637 : case BUILT_IN_VA_ARG_PACK:
12638 : case BUILT_IN_VA_ARG_PACK_LEN:
12639 : case BUILT_IN_VA_COPY:
12640 : case BUILT_IN_TRAP:
12641 : case BUILT_IN_UNREACHABLE_TRAP:
12642 : case BUILT_IN_SAVEREGS:
12643 : case BUILT_IN_POPCOUNTL:
12644 : case BUILT_IN_POPCOUNTLL:
12645 : case BUILT_IN_POPCOUNTIMAX:
12646 : case BUILT_IN_POPCOUNT:
12647 : case BUILT_IN_PARITYL:
12648 : case BUILT_IN_PARITYLL:
12649 : case BUILT_IN_PARITYIMAX:
12650 : case BUILT_IN_PARITY:
12651 : case BUILT_IN_LABS:
12652 : case BUILT_IN_LLABS:
12653 : case BUILT_IN_PREFETCH:
12654 : case BUILT_IN_ACC_ON_DEVICE:
12655 : return true;
12656 :
12657 14774232 : default:
12658 14774232 : return is_simple_builtin (decl);
12659 : }
12660 :
12661 : return false;
12662 : }
12663 :
12664 : /* Return true if T is a constant and the value cast to a target char
12665 : can be represented by a host char.
12666 : Store the casted char constant in *P if so. */
12667 :
12668 : bool
12669 3729 : target_char_cst_p (tree t, char *p)
12670 : {
12671 3729 : if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
12672 : return false;
12673 :
12674 2065 : *p = (char)tree_to_uhwi (t);
12675 2065 : return true;
12676 : }
12677 :
12678 : /* Return true if the builtin DECL is implemented in a standard library.
12679 : Otherwise return false which doesn't guarantee it is not (thus the list
12680 : of handled builtins below may be incomplete). */
12681 :
12682 : bool
12683 43558 : builtin_with_linkage_p (tree decl)
12684 : {
12685 43558 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12686 43274 : switch (DECL_FUNCTION_CODE (decl))
12687 : {
12688 1346 : CASE_FLT_FN (BUILT_IN_ACOS):
12689 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
12690 1346 : CASE_FLT_FN (BUILT_IN_ACOSH):
12691 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
12692 1346 : CASE_FLT_FN (BUILT_IN_ACOSPI):
12693 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSPI):
12694 1346 : CASE_FLT_FN (BUILT_IN_ASIN):
12695 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
12696 1346 : CASE_FLT_FN (BUILT_IN_ASINH):
12697 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
12698 1346 : CASE_FLT_FN (BUILT_IN_ASINPI):
12699 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINPI):
12700 1346 : CASE_FLT_FN (BUILT_IN_ATAN):
12701 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
12702 1346 : CASE_FLT_FN (BUILT_IN_ATAN2):
12703 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
12704 1346 : CASE_FLT_FN (BUILT_IN_ATAN2PI):
12705 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2PI):
12706 1346 : CASE_FLT_FN (BUILT_IN_ATANH):
12707 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
12708 1346 : CASE_FLT_FN (BUILT_IN_ATANPI):
12709 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANPI):
12710 1346 : CASE_FLT_FN (BUILT_IN_CBRT):
12711 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
12712 1346 : CASE_FLT_FN (BUILT_IN_CEIL):
12713 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
12714 1346 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
12715 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
12716 1346 : CASE_FLT_FN (BUILT_IN_COS):
12717 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
12718 1346 : CASE_FLT_FN (BUILT_IN_COSH):
12719 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
12720 1346 : CASE_FLT_FN (BUILT_IN_COSPI):
12721 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSPI):
12722 1346 : CASE_FLT_FN (BUILT_IN_ERF):
12723 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
12724 1346 : CASE_FLT_FN (BUILT_IN_ERFC):
12725 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
12726 1346 : CASE_FLT_FN (BUILT_IN_EXP):
12727 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
12728 1346 : CASE_FLT_FN (BUILT_IN_EXP2):
12729 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
12730 1346 : CASE_FLT_FN (BUILT_IN_EXPM1):
12731 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
12732 1346 : CASE_FLT_FN (BUILT_IN_FABS):
12733 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
12734 1346 : CASE_FLT_FN (BUILT_IN_FDIM):
12735 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
12736 1346 : CASE_FLT_FN (BUILT_IN_FLOOR):
12737 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
12738 1346 : CASE_FLT_FN (BUILT_IN_FMA):
12739 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
12740 1346 : CASE_FLT_FN (BUILT_IN_FMAX):
12741 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
12742 1346 : CASE_FLT_FN (BUILT_IN_FMIN):
12743 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
12744 1346 : CASE_FLT_FN (BUILT_IN_FMOD):
12745 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
12746 1346 : CASE_FLT_FN (BUILT_IN_FREXP):
12747 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
12748 1346 : CASE_FLT_FN (BUILT_IN_HYPOT):
12749 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
12750 1346 : CASE_FLT_FN (BUILT_IN_ILOGB):
12751 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
12752 1346 : CASE_FLT_FN (BUILT_IN_LDEXP):
12753 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
12754 1346 : CASE_FLT_FN (BUILT_IN_LGAMMA):
12755 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
12756 1346 : CASE_FLT_FN (BUILT_IN_LLRINT):
12757 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
12758 1346 : CASE_FLT_FN (BUILT_IN_LLROUND):
12759 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
12760 1346 : CASE_FLT_FN (BUILT_IN_LOG):
12761 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
12762 1346 : CASE_FLT_FN (BUILT_IN_LOG10):
12763 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
12764 1346 : CASE_FLT_FN (BUILT_IN_LOG1P):
12765 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
12766 1346 : CASE_FLT_FN (BUILT_IN_LOG2):
12767 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
12768 1346 : CASE_FLT_FN (BUILT_IN_LOGB):
12769 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
12770 1346 : CASE_FLT_FN (BUILT_IN_LRINT):
12771 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
12772 1346 : CASE_FLT_FN (BUILT_IN_LROUND):
12773 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
12774 1346 : CASE_FLT_FN (BUILT_IN_MODF):
12775 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
12776 1346 : CASE_FLT_FN (BUILT_IN_NAN):
12777 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
12778 1346 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
12779 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
12780 1346 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
12781 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
12782 1346 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
12783 1346 : CASE_FLT_FN (BUILT_IN_POW):
12784 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
12785 1346 : CASE_FLT_FN (BUILT_IN_REMAINDER):
12786 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
12787 1346 : CASE_FLT_FN (BUILT_IN_REMQUO):
12788 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
12789 1346 : CASE_FLT_FN (BUILT_IN_RINT):
12790 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
12791 1346 : CASE_FLT_FN (BUILT_IN_ROUND):
12792 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
12793 1346 : CASE_FLT_FN (BUILT_IN_SCALBLN):
12794 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
12795 1346 : CASE_FLT_FN (BUILT_IN_SCALBN):
12796 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
12797 1346 : CASE_FLT_FN (BUILT_IN_SIN):
12798 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
12799 1346 : CASE_FLT_FN (BUILT_IN_SINCOS):
12800 1346 : CASE_FLT_FN (BUILT_IN_SINH):
12801 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
12802 1346 : CASE_FLT_FN (BUILT_IN_SINPI):
12803 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINPI):
12804 1346 : CASE_FLT_FN (BUILT_IN_SQRT):
12805 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
12806 1346 : CASE_FLT_FN (BUILT_IN_TAN):
12807 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
12808 1346 : CASE_FLT_FN (BUILT_IN_TANH):
12809 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
12810 1346 : CASE_FLT_FN (BUILT_IN_TANPI):
12811 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANPI):
12812 1346 : CASE_FLT_FN (BUILT_IN_TGAMMA):
12813 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
12814 1346 : CASE_FLT_FN (BUILT_IN_TRUNC):
12815 1346 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
12816 1346 : return true;
12817 :
12818 16 : case BUILT_IN_STPCPY:
12819 16 : case BUILT_IN_STPNCPY:
12820 : /* stpcpy is both referenced in libiberty's pex-win32.c and provided
12821 : by libiberty's stpcpy.c for MinGW targets so we need to return true
12822 : in order to be able to build libiberty in LTO mode for them. */
12823 16 : return true;
12824 :
12825 : default:
12826 : break;
12827 : }
12828 : return false;
12829 : }
12830 :
12831 : /* Return true if OFFRNG is bounded to a subrange of offset values
12832 : valid for the largest possible object. */
12833 :
12834 : bool
12835 501 : access_ref::offset_bounded () const
12836 : {
12837 501 : tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
12838 501 : tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
12839 975 : return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
12840 : }
12841 :
12842 : /* If CALLEE has known side effects, fill in INFO and return true.
12843 : See tree-ssa-structalias.cc:find_func_aliases
12844 : for the list of builtins we might need to handle here. */
12845 :
12846 : attr_fnspec
12847 119851628 : builtin_fnspec (tree callee)
12848 : {
12849 119851628 : built_in_function code = DECL_FUNCTION_CODE (callee);
12850 :
12851 119851628 : switch (code)
12852 : {
12853 : /* All the following functions read memory pointed to by
12854 : their second argument and write memory pointed to by first
12855 : argument.
12856 : strcat/strncat additionally reads memory pointed to by the first
12857 : argument. */
12858 133486 : case BUILT_IN_STRCAT:
12859 133486 : case BUILT_IN_STRCAT_CHK:
12860 133486 : return "1cW 1 ";
12861 42167 : case BUILT_IN_STRNCAT:
12862 42167 : case BUILT_IN_STRNCAT_CHK:
12863 42167 : return "1cW 13";
12864 263758 : case BUILT_IN_STRCPY:
12865 263758 : case BUILT_IN_STRCPY_CHK:
12866 263758 : return "1cO 1 ";
12867 32662 : case BUILT_IN_STPCPY:
12868 32662 : case BUILT_IN_STPCPY_CHK:
12869 32662 : return ".cO 1 ";
12870 19564672 : case BUILT_IN_STRNCPY:
12871 19564672 : case BUILT_IN_MEMCPY:
12872 19564672 : case BUILT_IN_MEMMOVE:
12873 19564672 : case BUILT_IN_TM_MEMCPY:
12874 19564672 : case BUILT_IN_TM_MEMMOVE:
12875 19564672 : case BUILT_IN_STRNCPY_CHK:
12876 19564672 : case BUILT_IN_MEMCPY_CHK:
12877 19564672 : case BUILT_IN_MEMMOVE_CHK:
12878 19564672 : return "1cO313";
12879 144545 : case BUILT_IN_MEMPCPY:
12880 144545 : case BUILT_IN_MEMPCPY_CHK:
12881 144545 : return ".cO313";
12882 58971 : case BUILT_IN_STPNCPY:
12883 58971 : case BUILT_IN_STPNCPY_CHK:
12884 58971 : return ".cO313";
12885 0 : case BUILT_IN_BCOPY:
12886 0 : return ".c23O3";
12887 0 : case BUILT_IN_BZERO:
12888 0 : return ".cO2";
12889 10210569 : case BUILT_IN_MEMCMP:
12890 10210569 : case BUILT_IN_MEMCMP_EQ:
12891 10210569 : case BUILT_IN_BCMP:
12892 10210569 : case BUILT_IN_STRNCMP:
12893 10210569 : case BUILT_IN_STRNCMP_EQ:
12894 10210569 : case BUILT_IN_STRNCASECMP:
12895 10210569 : return ".cR3R3";
12896 :
12897 : /* The following functions read memory pointed to by their
12898 : first argument. */
12899 773 : CASE_BUILT_IN_TM_LOAD (1):
12900 773 : CASE_BUILT_IN_TM_LOAD (2):
12901 773 : CASE_BUILT_IN_TM_LOAD (4):
12902 773 : CASE_BUILT_IN_TM_LOAD (8):
12903 773 : CASE_BUILT_IN_TM_LOAD (FLOAT):
12904 773 : CASE_BUILT_IN_TM_LOAD (DOUBLE):
12905 773 : CASE_BUILT_IN_TM_LOAD (LDOUBLE):
12906 773 : CASE_BUILT_IN_TM_LOAD (M64):
12907 773 : CASE_BUILT_IN_TM_LOAD (M128):
12908 773 : CASE_BUILT_IN_TM_LOAD (M256):
12909 773 : case BUILT_IN_TM_LOG:
12910 773 : case BUILT_IN_TM_LOG_1:
12911 773 : case BUILT_IN_TM_LOG_2:
12912 773 : case BUILT_IN_TM_LOG_4:
12913 773 : case BUILT_IN_TM_LOG_8:
12914 773 : case BUILT_IN_TM_LOG_FLOAT:
12915 773 : case BUILT_IN_TM_LOG_DOUBLE:
12916 773 : case BUILT_IN_TM_LOG_LDOUBLE:
12917 773 : case BUILT_IN_TM_LOG_M64:
12918 773 : case BUILT_IN_TM_LOG_M128:
12919 773 : case BUILT_IN_TM_LOG_M256:
12920 773 : return ".cR ";
12921 :
12922 516333 : case BUILT_IN_INDEX:
12923 516333 : case BUILT_IN_RINDEX:
12924 516333 : case BUILT_IN_STRCHR:
12925 516333 : case BUILT_IN_STRLEN:
12926 516333 : case BUILT_IN_STRRCHR:
12927 516333 : return ".cR ";
12928 60216 : case BUILT_IN_STRNLEN:
12929 60216 : return ".cR2";
12930 :
12931 : /* These read memory pointed to by the first argument.
12932 : Allocating memory does not have any side-effects apart from
12933 : being the definition point for the pointer.
12934 : Unix98 specifies that errno is set on allocation failure. */
12935 18278 : case BUILT_IN_STRDUP:
12936 18278 : return "mCR ";
12937 12930 : case BUILT_IN_STRNDUP:
12938 12930 : return "mCR2";
12939 : /* Allocating memory does not have any side-effects apart from
12940 : being the definition point for the pointer. */
12941 12008023 : case BUILT_IN_MALLOC:
12942 12008023 : case BUILT_IN_ALIGNED_ALLOC:
12943 12008023 : case BUILT_IN_CALLOC:
12944 12008023 : case BUILT_IN_GOMP_ALLOC:
12945 12008023 : return "mC";
12946 861526 : CASE_BUILT_IN_ALLOCA:
12947 861526 : return "mc";
12948 : /* These read memory pointed to by the first argument with size
12949 : in the third argument. */
12950 586345 : case BUILT_IN_MEMCHR:
12951 586345 : return ".cR3";
12952 : /* These read memory pointed to by the first and second arguments. */
12953 12746913 : case BUILT_IN_STRSTR:
12954 12746913 : case BUILT_IN_STRPBRK:
12955 12746913 : case BUILT_IN_STRCASECMP:
12956 12746913 : case BUILT_IN_STRCSPN:
12957 12746913 : case BUILT_IN_STRSPN:
12958 12746913 : case BUILT_IN_STRCMP:
12959 12746913 : case BUILT_IN_STRCMP_EQ:
12960 12746913 : return ".cR R ";
12961 : /* Freeing memory kills the pointed-to memory. More importantly
12962 : the call has to serve as a barrier for moving loads and stores
12963 : across it. */
12964 7309364 : case BUILT_IN_STACK_RESTORE:
12965 7309364 : case BUILT_IN_FREE:
12966 7309364 : case BUILT_IN_GOMP_FREE:
12967 7309364 : return ".co ";
12968 108784 : case BUILT_IN_VA_END:
12969 108784 : return ".cO ";
12970 : /* Realloc serves both as allocation point and deallocation point. */
12971 1133996 : case BUILT_IN_REALLOC:
12972 1133996 : case BUILT_IN_GOMP_REALLOC:
12973 1133996 : return ".Cw ";
12974 15740 : case BUILT_IN_GAMMA_R:
12975 15740 : case BUILT_IN_GAMMAF_R:
12976 15740 : case BUILT_IN_GAMMAL_R:
12977 15740 : case BUILT_IN_LGAMMA_R:
12978 15740 : case BUILT_IN_LGAMMAF_R:
12979 15740 : case BUILT_IN_LGAMMAL_R:
12980 15740 : return ".C. Ot";
12981 44985 : case BUILT_IN_FREXP:
12982 44985 : case BUILT_IN_FREXPF:
12983 44985 : case BUILT_IN_FREXPL:
12984 44985 : case BUILT_IN_MODF:
12985 44985 : case BUILT_IN_MODFF:
12986 44985 : case BUILT_IN_MODFL:
12987 44985 : return ".c. Ot";
12988 7842 : case BUILT_IN_REMQUO:
12989 7842 : case BUILT_IN_REMQUOF:
12990 7842 : case BUILT_IN_REMQUOL:
12991 7842 : return ".c. . Ot";
12992 167 : case BUILT_IN_SINCOS:
12993 167 : case BUILT_IN_SINCOSF:
12994 167 : case BUILT_IN_SINCOSL:
12995 167 : return ".c. OtOt";
12996 4479055 : case BUILT_IN_MEMSET:
12997 4479055 : case BUILT_IN_MEMSET_CHK:
12998 4479055 : case BUILT_IN_TM_MEMSET:
12999 4479055 : return "1cO3";
13000 366 : CASE_BUILT_IN_TM_STORE (1):
13001 366 : CASE_BUILT_IN_TM_STORE (2):
13002 366 : CASE_BUILT_IN_TM_STORE (4):
13003 366 : CASE_BUILT_IN_TM_STORE (8):
13004 366 : CASE_BUILT_IN_TM_STORE (FLOAT):
13005 366 : CASE_BUILT_IN_TM_STORE (DOUBLE):
13006 366 : CASE_BUILT_IN_TM_STORE (LDOUBLE):
13007 366 : CASE_BUILT_IN_TM_STORE (M64):
13008 366 : CASE_BUILT_IN_TM_STORE (M128):
13009 366 : CASE_BUILT_IN_TM_STORE (M256):
13010 366 : return ".cO ";
13011 2689450 : case BUILT_IN_STACK_SAVE:
13012 2689450 : case BUILT_IN_RETURN:
13013 2689450 : case BUILT_IN_EH_POINTER:
13014 2689450 : case BUILT_IN_EH_FILTER:
13015 2689450 : case BUILT_IN_UNWIND_RESUME:
13016 2689450 : case BUILT_IN_CXA_END_CLEANUP:
13017 2689450 : case BUILT_IN_EH_COPY_VALUES:
13018 2689450 : case BUILT_IN_FRAME_ADDRESS:
13019 2689450 : case BUILT_IN_APPLY_ARGS:
13020 2689450 : case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
13021 2689450 : case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
13022 2689450 : case BUILT_IN_PREFETCH:
13023 2689450 : case BUILT_IN_DWARF_CFA:
13024 2689450 : case BUILT_IN_RETURN_ADDRESS:
13025 2689450 : return ".c";
13026 2486724 : case BUILT_IN_ASSUME_ALIGNED:
13027 2486724 : case BUILT_IN_EXPECT:
13028 2486724 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
13029 2486724 : return "1cX ";
13030 : /* But posix_memalign stores a pointer into the memory pointed to
13031 : by its first argument. */
13032 6510 : case BUILT_IN_POSIX_MEMALIGN:
13033 6510 : return ".cOt";
13034 4450 : case BUILT_IN_OMP_GET_MAPPED_PTR:
13035 4450 : return ". R ";
13036 :
13037 44302028 : default:
13038 44302028 : return "";
13039 : }
13040 : }
|