Branch data Line data Source code
1 : : /* Expand builtin functions.
2 : : Copyright (C) 1988-2025 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 "predict.h"
34 : : #include "tm_p.h"
35 : : #include "stringpool.h"
36 : : #include "tree-vrp.h"
37 : : #include "tree-ssanames.h"
38 : : #include "expmed.h"
39 : : #include "optabs.h"
40 : : #include "emit-rtl.h"
41 : : #include "recog.h"
42 : : #include "diagnostic-core.h"
43 : : #include "alias.h"
44 : : #include "fold-const.h"
45 : : #include "fold-const-call.h"
46 : : #include "gimple-ssa-warn-access.h"
47 : : #include "stor-layout.h"
48 : : #include "calls.h"
49 : : #include "varasm.h"
50 : : #include "tree-object-size.h"
51 : : #include "tree-ssa-strlen.h"
52 : : #include "realmpfr.h"
53 : : #include "cfgrtl.h"
54 : : #include "except.h"
55 : : #include "dojump.h"
56 : : #include "explow.h"
57 : : #include "stmt.h"
58 : : #include "expr.h"
59 : : #include "libfuncs.h"
60 : : #include "output.h"
61 : : #include "typeclass.h"
62 : : #include "langhooks.h"
63 : : #include "value-prof.h"
64 : : #include "builtins.h"
65 : : #include "stringpool.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_constant_p (tree);
160 : : static tree fold_builtin_classify_type (tree);
161 : : static tree fold_builtin_strlen (location_t, tree, tree, tree);
162 : : static tree fold_builtin_inf (location_t, tree, int);
163 : : static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
164 : : static bool validate_arg (const_tree, enum tree_code code);
165 : : static rtx expand_builtin_fabs (tree, rtx, rtx);
166 : : static rtx expand_builtin_signbit (tree, rtx);
167 : : static tree fold_builtin_memcmp (location_t, tree, tree, tree);
168 : : static tree fold_builtin_isascii (location_t, tree);
169 : : static tree fold_builtin_toascii (location_t, tree);
170 : : static tree fold_builtin_isdigit (location_t, tree);
171 : : static tree fold_builtin_fabs (location_t, tree, tree);
172 : : static tree fold_builtin_abs (location_t, tree, tree);
173 : : static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
174 : : enum tree_code);
175 : : static tree fold_builtin_iseqsig (location_t, tree, tree);
176 : : static tree fold_builtin_varargs (location_t, tree, tree*, int);
177 : :
178 : : static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
179 : : static tree fold_builtin_strspn (location_t, tree, tree, tree, tree);
180 : : static tree fold_builtin_strcspn (location_t, tree, tree, tree, tree);
181 : :
182 : : static rtx expand_builtin_object_size (tree);
183 : : static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
184 : : enum built_in_function);
185 : : static void maybe_emit_chk_warning (tree, enum built_in_function);
186 : : static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
187 : : static tree fold_builtin_object_size (tree, tree, enum built_in_function);
188 : :
189 : : unsigned HOST_WIDE_INT target_newline;
190 : : unsigned HOST_WIDE_INT target_percent;
191 : : static unsigned HOST_WIDE_INT target_c;
192 : : static unsigned HOST_WIDE_INT target_s;
193 : : char target_percent_c[3];
194 : : char target_percent_s[3];
195 : : char target_percent_s_newline[4];
196 : : static tree do_mpfr_remquo (tree, tree, tree);
197 : : static tree do_mpfr_lgamma_r (tree, tree, tree);
198 : : static void expand_builtin_sync_synchronize (void);
199 : :
200 : : /* Return true if NAME starts with __builtin_ or __sync_. */
201 : :
202 : : static bool
203 : 525385 : is_builtin_name (const char *name)
204 : : {
205 : 525385 : return (startswith (name, "__builtin_")
206 : 189990 : || startswith (name, "__sync_")
207 : 714513 : || startswith (name, "__atomic_"));
208 : : }
209 : :
210 : : /* Return true if NODE should be considered for inline expansion regardless
211 : : of the optimization level. This means whenever a function is invoked with
212 : : its "internal" name, which normally contains the prefix "__builtin". */
213 : :
214 : : bool
215 : 525385 : called_as_built_in (tree node)
216 : : {
217 : : /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
218 : : we want the name used to call the function, not the name it
219 : : will have. */
220 : 525385 : const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
221 : 525385 : return is_builtin_name (name);
222 : : }
223 : :
224 : : /* Compute values M and N such that M divides (address of EXP - N) and such
225 : : that N < M. If these numbers can be determined, store M in alignp and N in
226 : : *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
227 : : *alignp and any bit-offset to *bitposp.
228 : :
229 : : Note that the address (and thus the alignment) computed here is based
230 : : on the address to which a symbol resolves, whereas DECL_ALIGN is based
231 : : on the address at which an object is actually located. These two
232 : : addresses are not always the same. For example, on ARM targets,
233 : : the address &foo of a Thumb function foo() has the lowest bit set,
234 : : whereas foo() itself starts on an even address.
235 : :
236 : : If ADDR_P is true we are taking the address of the memory reference EXP
237 : : and thus cannot rely on the access taking place. */
238 : :
239 : : bool
240 : 105378039 : get_object_alignment_2 (tree exp, unsigned int *alignp,
241 : : unsigned HOST_WIDE_INT *bitposp, bool addr_p)
242 : : {
243 : 105378039 : poly_int64 bitsize, bitpos;
244 : 105378039 : tree offset;
245 : 105378039 : machine_mode mode;
246 : 105378039 : int unsignedp, reversep, volatilep;
247 : 105378039 : unsigned int align = BITS_PER_UNIT;
248 : 105378039 : bool known_alignment = false;
249 : :
250 : : /* Get the innermost object and the constant (bitpos) and possibly
251 : : variable (offset) offset of the access. */
252 : 105378039 : exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
253 : : &unsignedp, &reversep, &volatilep);
254 : :
255 : : /* Extract alignment information from the innermost object and
256 : : possibly adjust bitpos and offset. */
257 : 105378039 : if (TREE_CODE (exp) == FUNCTION_DECL)
258 : : {
259 : : /* Function addresses can encode extra information besides their
260 : : alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
261 : : allows the low bit to be used as a virtual bit, we know
262 : : that the address itself must be at least 2-byte aligned. */
263 : : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
264 : : align = 2 * BITS_PER_UNIT;
265 : : }
266 : 105070954 : else if (TREE_CODE (exp) == LABEL_DECL)
267 : : ;
268 : 105033982 : else if (TREE_CODE (exp) == CONST_DECL)
269 : : {
270 : : /* The alignment of a CONST_DECL is determined by its initializer. */
271 : 58517 : exp = DECL_INITIAL (exp);
272 : 58517 : align = TYPE_ALIGN (TREE_TYPE (exp));
273 : 58517 : if (CONSTANT_CLASS_P (exp))
274 : 58510 : align = targetm.constant_alignment (exp, align);
275 : :
276 : : known_alignment = true;
277 : : }
278 : 104975465 : else if (DECL_P (exp))
279 : : {
280 : 63350903 : align = DECL_ALIGN (exp);
281 : : known_alignment = true;
282 : : }
283 : 41624562 : else if (TREE_CODE (exp) == INDIRECT_REF
284 : 41613004 : || TREE_CODE (exp) == MEM_REF
285 : 5321050 : || TREE_CODE (exp) == TARGET_MEM_REF)
286 : : {
287 : 39944546 : tree addr = TREE_OPERAND (exp, 0);
288 : 39944546 : unsigned ptr_align;
289 : 39944546 : unsigned HOST_WIDE_INT ptr_bitpos;
290 : 39944546 : unsigned HOST_WIDE_INT ptr_bitmask = ~0;
291 : :
292 : : /* If the address is explicitely aligned, handle that. */
293 : 39944546 : if (TREE_CODE (addr) == BIT_AND_EXPR
294 : 39944546 : && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
295 : : {
296 : 76 : ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
297 : 76 : ptr_bitmask *= BITS_PER_UNIT;
298 : 76 : align = least_bit_hwi (ptr_bitmask);
299 : 76 : addr = TREE_OPERAND (addr, 0);
300 : : }
301 : :
302 : 39944546 : known_alignment
303 : 39944546 : = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
304 : 39944546 : align = MAX (ptr_align, align);
305 : :
306 : : /* Re-apply explicit alignment to the bitpos. */
307 : 39944546 : ptr_bitpos &= ptr_bitmask;
308 : :
309 : : /* The alignment of the pointer operand in a TARGET_MEM_REF
310 : : has to take the variable offset parts into account. */
311 : 39944546 : if (TREE_CODE (exp) == TARGET_MEM_REF)
312 : : {
313 : 3641034 : if (TMR_INDEX (exp))
314 : : {
315 : 1717070 : unsigned HOST_WIDE_INT step = 1;
316 : 1717070 : if (TMR_STEP (exp))
317 : 1561067 : step = TREE_INT_CST_LOW (TMR_STEP (exp));
318 : 1717070 : align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
319 : : }
320 : 3641034 : if (TMR_INDEX2 (exp))
321 : 42140 : align = BITS_PER_UNIT;
322 : : known_alignment = false;
323 : : }
324 : :
325 : : /* When EXP is an actual memory reference then we can use
326 : : TYPE_ALIGN of a pointer indirection to derive alignment.
327 : : Do so only if get_pointer_alignment_1 did not reveal absolute
328 : : alignment knowledge and if using that alignment would
329 : : improve the situation. */
330 : 39944546 : unsigned int talign;
331 : 39944546 : if (!addr_p && !known_alignment
332 : 37598744 : && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
333 : 77543284 : && talign > align)
334 : : align = talign;
335 : : else
336 : : {
337 : : /* Else adjust bitpos accordingly. */
338 : 10753969 : bitpos += ptr_bitpos;
339 : 10753969 : if (TREE_CODE (exp) == MEM_REF
340 : 10753969 : || TREE_CODE (exp) == TARGET_MEM_REF)
341 : 10745219 : bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
342 : : }
343 : 39944546 : }
344 : 1680016 : else if (TREE_CODE (exp) == STRING_CST)
345 : : {
346 : : /* STRING_CST are the only constant objects we allow to be not
347 : : wrapped inside a CONST_DECL. */
348 : 1679522 : align = TYPE_ALIGN (TREE_TYPE (exp));
349 : 1679522 : if (CONSTANT_CLASS_P (exp))
350 : 1679522 : align = targetm.constant_alignment (exp, align);
351 : :
352 : : known_alignment = true;
353 : : }
354 : :
355 : : /* If there is a non-constant offset part extract the maximum
356 : : alignment that can prevail. */
357 : 105378039 : if (offset)
358 : : {
359 : 9964177 : unsigned int trailing_zeros = tree_ctz (offset);
360 : 9964177 : if (trailing_zeros < HOST_BITS_PER_INT)
361 : : {
362 : 9964121 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
363 : 9964121 : if (inner)
364 : 9964121 : align = MIN (align, inner);
365 : : }
366 : : }
367 : :
368 : : /* Account for the alignment of runtime coefficients, so that the constant
369 : : bitpos is guaranteed to be accurate. */
370 : 105378039 : unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
371 : 105378039 : if (alt_align != 0 && alt_align < align)
372 : : {
373 : : align = alt_align;
374 : : known_alignment = false;
375 : : }
376 : :
377 : 105378039 : *alignp = align;
378 : 105378039 : *bitposp = bitpos.coeffs[0] & (align - 1);
379 : 105378039 : return known_alignment;
380 : : }
381 : :
382 : : /* For a memory reference expression EXP compute values M and N such that M
383 : : divides (&EXP - N) and such that N < M. If these numbers can be determined,
384 : : store M in alignp and N in *BITPOSP and return true. Otherwise return false
385 : : and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
386 : :
387 : : bool
388 : 82797401 : get_object_alignment_1 (tree exp, unsigned int *alignp,
389 : : unsigned HOST_WIDE_INT *bitposp)
390 : : {
391 : : /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
392 : : with it. */
393 : 82797401 : if (TREE_CODE (exp) == WITH_SIZE_EXPR)
394 : 0 : exp = TREE_OPERAND (exp, 0);
395 : 82797401 : return get_object_alignment_2 (exp, alignp, bitposp, false);
396 : : }
397 : :
398 : : /* Return the alignment in bits of EXP, an object. */
399 : :
400 : : unsigned int
401 : 32758151 : get_object_alignment (tree exp)
402 : : {
403 : 32758151 : unsigned HOST_WIDE_INT bitpos = 0;
404 : 32758151 : unsigned int align;
405 : :
406 : 32758151 : get_object_alignment_1 (exp, &align, &bitpos);
407 : :
408 : : /* align and bitpos now specify known low bits of the pointer.
409 : : ptr & (align - 1) == bitpos. */
410 : :
411 : 32758151 : if (bitpos != 0)
412 : 1736561 : align = least_bit_hwi (bitpos);
413 : 32758151 : return align;
414 : : }
415 : :
416 : : /* For a pointer valued expression EXP compute values M and N such that M
417 : : divides (EXP - N) and such that N < M. If these numbers can be determined,
418 : : store M in alignp and N in *BITPOSP and return true. Return false if
419 : : the results are just a conservative approximation.
420 : :
421 : : If EXP is not a pointer, false is returned too. */
422 : :
423 : : bool
424 : 80139084 : get_pointer_alignment_1 (tree exp, unsigned int *alignp,
425 : : unsigned HOST_WIDE_INT *bitposp)
426 : : {
427 : 80139084 : STRIP_NOPS (exp);
428 : :
429 : 80139084 : if (TREE_CODE (exp) == ADDR_EXPR)
430 : 22579103 : return get_object_alignment_2 (TREE_OPERAND (exp, 0),
431 : 22579103 : alignp, bitposp, true);
432 : 57559981 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
433 : : {
434 : 939390 : unsigned int align;
435 : 939390 : unsigned HOST_WIDE_INT bitpos;
436 : 939390 : bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
437 : : &align, &bitpos);
438 : 939390 : if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
439 : 1737 : bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
440 : : else
441 : : {
442 : 937653 : unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
443 : 937653 : if (trailing_zeros < HOST_BITS_PER_INT)
444 : : {
445 : 937647 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
446 : 937647 : if (inner)
447 : 937647 : align = MIN (align, inner);
448 : : }
449 : : }
450 : 939390 : *alignp = align;
451 : 939390 : *bitposp = bitpos & (align - 1);
452 : 939390 : return res;
453 : : }
454 : 56620591 : else if (TREE_CODE (exp) == SSA_NAME
455 : 56620591 : && POINTER_TYPE_P (TREE_TYPE (exp)))
456 : : {
457 : 54678251 : unsigned int ptr_align, ptr_misalign;
458 : 54678251 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
459 : :
460 : 54678251 : if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
461 : : {
462 : 5887624 : *bitposp = ptr_misalign * BITS_PER_UNIT;
463 : 5887624 : *alignp = ptr_align * BITS_PER_UNIT;
464 : : /* Make sure to return a sensible alignment when the multiplication
465 : : by BITS_PER_UNIT overflowed. */
466 : 5887624 : if (*alignp == 0)
467 : 30 : *alignp = 1u << (HOST_BITS_PER_INT - 1);
468 : : /* We cannot really tell whether this result is an approximation. */
469 : 5887624 : return false;
470 : : }
471 : : else
472 : : {
473 : 48790627 : *bitposp = 0;
474 : 48790627 : *alignp = BITS_PER_UNIT;
475 : 48790627 : return false;
476 : : }
477 : : }
478 : 1942340 : else if (TREE_CODE (exp) == INTEGER_CST)
479 : : {
480 : 16581 : *alignp = BIGGEST_ALIGNMENT;
481 : 16581 : *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
482 : 16581 : & (BIGGEST_ALIGNMENT - 1));
483 : 16581 : return true;
484 : : }
485 : :
486 : 1925759 : *bitposp = 0;
487 : 1925759 : *alignp = BITS_PER_UNIT;
488 : 1925759 : return false;
489 : : }
490 : :
491 : : /* Return the alignment in bits of EXP, a pointer valued expression.
492 : : The alignment returned is, by default, the alignment of the thing that
493 : : EXP points to. If it is not a POINTER_TYPE, 0 is returned.
494 : :
495 : : Otherwise, look at the expression to see if we can do better, i.e., if the
496 : : expression is actually pointing at an object whose alignment is tighter. */
497 : :
498 : : unsigned int
499 : 11019449 : get_pointer_alignment (tree exp)
500 : : {
501 : 11019449 : unsigned HOST_WIDE_INT bitpos = 0;
502 : 11019449 : unsigned int align;
503 : :
504 : 11019449 : get_pointer_alignment_1 (exp, &align, &bitpos);
505 : :
506 : : /* align and bitpos now specify known low bits of the pointer.
507 : : ptr & (align - 1) == bitpos. */
508 : :
509 : 11019449 : if (bitpos != 0)
510 : 103918 : align = least_bit_hwi (bitpos);
511 : :
512 : 11019449 : return align;
513 : : }
514 : :
515 : : /* Return the number of leading non-zero elements in the sequence
516 : : [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
517 : : ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
518 : :
519 : : unsigned
520 : 787087 : string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
521 : : {
522 : 787087 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
523 : :
524 : 787087 : unsigned n;
525 : :
526 : 787087 : if (eltsize == 1)
527 : : {
528 : : /* Optimize the common case of plain char. */
529 : 245002980 : for (n = 0; n < maxelts; n++)
530 : : {
531 : 244910370 : const char *elt = (const char*) ptr + n;
532 : 244910370 : if (!*elt)
533 : : break;
534 : : }
535 : : }
536 : : else
537 : : {
538 : 590 : for (n = 0; n < maxelts; n++)
539 : : {
540 : 566 : const char *elt = (const char*) ptr + n * eltsize;
541 : 566 : if (!memcmp (elt, "\0\0\0\0", eltsize))
542 : : break;
543 : : }
544 : : }
545 : 787087 : return n;
546 : : }
547 : :
548 : : /* Compute the length of a null-terminated character string or wide
549 : : character string handling character sizes of 1, 2, and 4 bytes.
550 : : TREE_STRING_LENGTH is not the right way because it evaluates to
551 : : the size of the character array in bytes (as opposed to characters)
552 : : and because it can contain a zero byte in the middle.
553 : :
554 : : ONLY_VALUE should be nonzero if the result is not going to be emitted
555 : : into the instruction stream and zero if it is going to be expanded.
556 : : E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
557 : : is returned, otherwise NULL, since
558 : : len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
559 : : evaluate the side-effects.
560 : :
561 : : If ONLY_VALUE is two then we do not emit warnings about out-of-bound
562 : : accesses. Note that this implies the result is not going to be emitted
563 : : into the instruction stream.
564 : :
565 : : Additional information about the string accessed may be recorded
566 : : in DATA. For example, if ARG references an unterminated string,
567 : : then the declaration will be stored in the DECL field. If the
568 : : length of the unterminated string can be determined, it'll be
569 : : stored in the LEN field. Note this length could well be different
570 : : than what a C strlen call would return.
571 : :
572 : : ELTSIZE is 1 for normal single byte character strings, and 2 or
573 : : 4 for wide characer strings. ELTSIZE is by default 1.
574 : :
575 : : The value returned is of type `ssizetype'. */
576 : :
577 : : tree
578 : 2714785 : c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
579 : : {
580 : : /* If we were not passed a DATA pointer, then get one to a local
581 : : structure. That avoids having to check DATA for NULL before
582 : : each time we want to use it. */
583 : 2714785 : c_strlen_data local_strlen_data = { };
584 : 2714785 : if (!data)
585 : 596440 : data = &local_strlen_data;
586 : :
587 : 2714785 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
588 : :
589 : 2714785 : tree src = STRIP_NOPS (arg);
590 : 2714785 : if (TREE_CODE (src) == COND_EXPR
591 : 2714785 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 : : {
593 : 719 : tree len1, len2;
594 : :
595 : 719 : len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
596 : 719 : len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
597 : 719 : if (tree_int_cst_equal (len1, len2))
598 : : return len1;
599 : : }
600 : :
601 : 2714619 : if (TREE_CODE (src) == COMPOUND_EXPR
602 : 2714619 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
603 : 45 : return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
604 : :
605 : 2714574 : location_t loc = EXPR_LOC_OR_LOC (src, input_location);
606 : :
607 : : /* Offset from the beginning of the string in bytes. */
608 : 2714574 : tree byteoff;
609 : 2714574 : tree memsize;
610 : 2714574 : tree decl;
611 : 2714574 : src = string_constant (src, &byteoff, &memsize, &decl);
612 : 2714574 : if (src == 0)
613 : : return NULL_TREE;
614 : :
615 : : /* Determine the size of the string element. */
616 : 802018 : if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src)))))
617 : : return NULL_TREE;
618 : :
619 : : /* Set MAXELTS to ARRAY_SIZE (SRC) - 1, the maximum possible
620 : : length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
621 : : in case the latter is less than the size of the array, such as when
622 : : SRC refers to a short string literal used to initialize a large array.
623 : : In that case, the elements of the array after the terminating NUL are
624 : : all NUL. */
625 : 790807 : HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
626 : 790807 : strelts = strelts / eltsize;
627 : :
628 : 790807 : if (!tree_fits_uhwi_p (memsize))
629 : : return NULL_TREE;
630 : :
631 : 790807 : HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize;
632 : :
633 : : /* PTR can point to the byte representation of any string type, including
634 : : char* and wchar_t*. */
635 : 790807 : const char *ptr = TREE_STRING_POINTER (src);
636 : :
637 : 790807 : if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
638 : : {
639 : : /* The code below works only for single byte character types. */
640 : 5159 : if (eltsize != 1)
641 : : return NULL_TREE;
642 : :
643 : : /* If the string has an internal NUL character followed by any
644 : : non-NUL characters (e.g., "foo\0bar"), we can't compute
645 : : the offset to the following NUL if we don't know where to
646 : : start searching for it. */
647 : 5159 : unsigned len = string_length (ptr, eltsize, strelts);
648 : :
649 : : /* Return when an embedded null character is found or none at all.
650 : : In the latter case, set the DECL/LEN field in the DATA structure
651 : : so that callers may examine them. */
652 : 5159 : if (len + 1 < strelts)
653 : : return NULL_TREE;
654 : 4589 : else if (len >= maxelts)
655 : : {
656 : 1149 : data->decl = decl;
657 : 1149 : data->off = byteoff;
658 : 1149 : data->minlen = ssize_int (len);
659 : 1149 : return NULL_TREE;
660 : : }
661 : :
662 : : /* For empty strings the result should be zero. */
663 : 3440 : if (len == 0)
664 : 39 : return ssize_int (0);
665 : :
666 : : /* We don't know the starting offset, but we do know that the string
667 : : has no internal zero bytes. If the offset falls within the bounds
668 : : of the string subtract the offset from the length of the string,
669 : : and return that. Otherwise the length is zero. Take care to
670 : : use SAVE_EXPR in case the OFFSET has side-effects. */
671 : 3401 : tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
672 : : : byteoff;
673 : 3401 : offsave = fold_convert_loc (loc, sizetype, offsave);
674 : 3401 : tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
675 : 3401 : size_int (len));
676 : 3401 : tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
677 : : offsave);
678 : 3401 : lenexp = fold_convert_loc (loc, ssizetype, lenexp);
679 : 3401 : return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
680 : 3401 : build_zero_cst (ssizetype));
681 : : }
682 : :
683 : : /* Offset from the beginning of the string in elements. */
684 : 785648 : HOST_WIDE_INT eltoff;
685 : :
686 : : /* We have a known offset into the string. Start searching there for
687 : : a null character if we can represent it as a single HOST_WIDE_INT. */
688 : 785648 : if (byteoff == 0)
689 : : eltoff = 0;
690 : 785648 : else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
691 : : eltoff = -1;
692 : : else
693 : 784460 : eltoff = tree_to_uhwi (byteoff) / eltsize;
694 : :
695 : : /* If the offset is known to be out of bounds, warn, and call strlen at
696 : : runtime. */
697 : 785648 : if (eltoff < 0 || eltoff >= maxelts)
698 : : {
699 : : /* Suppress multiple warnings for propagated constant strings. */
700 : 3720 : if (only_value != 2
701 : 3720 : && !warning_suppressed_p (arg, OPT_Warray_bounds_)
702 : 7440 : && warning_at (loc, OPT_Warray_bounds_,
703 : : "offset %qwi outside bounds of constant string",
704 : : eltoff))
705 : : {
706 : 679 : if (decl)
707 : 678 : inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
708 : 679 : suppress_warning (arg, OPT_Warray_bounds_);
709 : : }
710 : 3720 : return NULL_TREE;
711 : : }
712 : :
713 : : /* If eltoff is larger than strelts but less than maxelts the
714 : : string length is zero, since the excess memory will be zero. */
715 : 781928 : if (eltoff > strelts)
716 : 0 : return ssize_int (0);
717 : :
718 : : /* Use strlen to search for the first zero byte. Since any strings
719 : : constructed with build_string will have nulls appended, we win even
720 : : if we get handed something like (char[4])"abcd".
721 : :
722 : : Since ELTOFF is our starting index into the string, no further
723 : : calculation is needed. */
724 : 781928 : unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
725 : 781928 : strelts - eltoff);
726 : :
727 : : /* Don't know what to return if there was no zero termination.
728 : : Ideally this would turn into a gcc_checking_assert over time.
729 : : Set DECL/LEN so callers can examine them. */
730 : 781928 : if (len >= maxelts - eltoff)
731 : : {
732 : 86348 : data->decl = decl;
733 : 86348 : data->off = byteoff;
734 : 86348 : data->minlen = ssize_int (len);
735 : 86348 : return NULL_TREE;
736 : : }
737 : :
738 : 695580 : return ssize_int (len);
739 : : }
740 : :
741 : : /* Return a constant integer corresponding to target reading
742 : : GET_MODE_BITSIZE (MODE) bits from string constant STR. If
743 : : NULL_TERMINATED_P, reading stops after '\0' character, all further ones
744 : : are assumed to be zero, otherwise it reads as many characters
745 : : as needed. */
746 : :
747 : : rtx
748 : 284557 : c_readstr (const char *str, fixed_size_mode mode,
749 : : bool null_terminated_p/*=true*/)
750 : : {
751 : 284557 : auto_vec<target_unit, MAX_BITSIZE_MODE_ANY_INT / BITS_PER_UNIT> bytes;
752 : :
753 : 569114 : bytes.reserve (GET_MODE_SIZE (mode));
754 : :
755 : 284557 : target_unit ch = 1;
756 : 4961092 : for (unsigned int i = 0; i < GET_MODE_SIZE (mode); ++i)
757 : : {
758 : 2195989 : if (ch || !null_terminated_p)
759 : 2112827 : ch = (unsigned char) str[i];
760 : 2195989 : bytes.quick_push (ch);
761 : : }
762 : :
763 : 284557 : return native_decode_rtx (mode, bytes, 0);
764 : 284557 : }
765 : :
766 : : /* Cast a target constant CST to target CHAR and if that value fits into
767 : : host char type, return zero and put that value into variable pointed to by
768 : : P. */
769 : :
770 : : static int
771 : 33784 : target_char_cast (tree cst, char *p)
772 : : {
773 : 33784 : unsigned HOST_WIDE_INT val, hostval;
774 : :
775 : 33784 : if (TREE_CODE (cst) != INTEGER_CST
776 : : || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
777 : : return 1;
778 : :
779 : : /* Do not care if it fits or not right here. */
780 : 33784 : val = TREE_INT_CST_LOW (cst);
781 : :
782 : 33784 : if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
783 : 33784 : val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
784 : :
785 : 33784 : hostval = val;
786 : 33784 : if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
787 : 33784 : hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
788 : :
789 : 33784 : if (val != hostval)
790 : : return 1;
791 : :
792 : 33784 : *p = hostval;
793 : 33784 : return 0;
794 : : }
795 : :
796 : : /* Similar to save_expr, but assumes that arbitrary code is not executed
797 : : in between the multiple evaluations. In particular, we assume that a
798 : : non-addressable local variable will not be modified. */
799 : :
800 : : static tree
801 : 1105380 : builtin_save_expr (tree exp)
802 : : {
803 : 1105380 : if (TREE_CODE (exp) == SSA_NAME
804 : 941423 : || (TREE_ADDRESSABLE (exp) == 0
805 : 941231 : && (TREE_CODE (exp) == PARM_DECL
806 : 367860 : || (VAR_P (exp) && !TREE_STATIC (exp)))))
807 : : return exp;
808 : :
809 : 366070 : return save_expr (exp);
810 : : }
811 : :
812 : : /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
813 : : times to get the address of either a higher stack frame, or a return
814 : : address located within it (depending on FNDECL_CODE). */
815 : :
816 : : static rtx
817 : 15582 : expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
818 : : {
819 : 15582 : int i;
820 : 15582 : rtx tem = INITIAL_FRAME_ADDRESS_RTX;
821 : 15582 : if (tem == NULL_RTX)
822 : : {
823 : : /* For a zero count with __builtin_return_address, we don't care what
824 : : frame address we return, because target-specific definitions will
825 : : override us. Therefore frame pointer elimination is OK, and using
826 : : the soft frame pointer is OK.
827 : :
828 : : For a nonzero count, or a zero count with __builtin_frame_address,
829 : : we require a stable offset from the current frame pointer to the
830 : : previous one, so we must use the hard frame pointer, and
831 : : we must disable frame pointer elimination. */
832 : 15582 : if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
833 : 4245 : tem = frame_pointer_rtx;
834 : : else
835 : : {
836 : 11337 : tem = hard_frame_pointer_rtx;
837 : :
838 : : /* Tell reload not to eliminate the frame pointer. */
839 : 11337 : crtl->accesses_prior_frames = 1;
840 : : }
841 : : }
842 : :
843 : 15582 : if (count > 0)
844 : 932 : SETUP_FRAME_ADDRESSES ();
845 : :
846 : : /* On the SPARC, the return address is not in the frame, it is in a
847 : : register. There is no way to access it off of the current frame
848 : : pointer, but it can be accessed off the previous frame pointer by
849 : : reading the value from the register window save area. */
850 : : if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
851 : : count--;
852 : :
853 : : /* Scan back COUNT frames to the specified frame. */
854 : 51458 : for (i = 0; i < count; i++)
855 : : {
856 : : /* Assume the dynamic chain pointer is in the word that the
857 : : frame address points to, unless otherwise specified. */
858 : 35876 : tem = DYNAMIC_CHAIN_ADDRESS (tem);
859 : 35876 : tem = memory_address (Pmode, tem);
860 : 35876 : tem = gen_frame_mem (Pmode, tem);
861 : 35876 : tem = copy_to_reg (tem);
862 : : }
863 : :
864 : : /* For __builtin_frame_address, return what we've got. But, on
865 : : the SPARC for example, we may have to add a bias. */
866 : 15582 : if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
867 : : return FRAME_ADDR_RTX (tem);
868 : :
869 : : /* For __builtin_return_address, get the return address from that frame. */
870 : : #ifdef RETURN_ADDR_RTX
871 : 7296 : tem = RETURN_ADDR_RTX (count, tem);
872 : : #else
873 : : tem = memory_address (Pmode,
874 : : plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
875 : : tem = gen_frame_mem (Pmode, tem);
876 : : #endif
877 : 5085 : return tem;
878 : : }
879 : :
880 : : /* Alias set used for setjmp buffer. */
881 : : static alias_set_type setjmp_alias_set = -1;
882 : :
883 : : /* Construct the leading half of a __builtin_setjmp call. Control will
884 : : return to RECEIVER_LABEL. This is also called directly by the SJLJ
885 : : exception handling code. */
886 : :
887 : : void
888 : 841 : expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
889 : : {
890 : 841 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
891 : 841 : rtx stack_save;
892 : 841 : rtx mem;
893 : :
894 : 841 : if (setjmp_alias_set == -1)
895 : 241 : setjmp_alias_set = new_alias_set ();
896 : :
897 : 841 : buf_addr = convert_memory_address (Pmode, buf_addr);
898 : :
899 : 842 : buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
900 : :
901 : : /* We store the frame pointer and the address of receiver_label in
902 : : the buffer and use the rest of it for the stack save area, which
903 : : is machine-dependent. */
904 : :
905 : 842 : mem = gen_rtx_MEM (Pmode, buf_addr);
906 : 841 : set_mem_alias_set (mem, setjmp_alias_set);
907 : 841 : emit_move_insn (mem, hard_frame_pointer_rtx);
908 : :
909 : 842 : mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
910 : 1682 : GET_MODE_SIZE (Pmode))),
911 : 841 : set_mem_alias_set (mem, setjmp_alias_set);
912 : :
913 : 1682 : emit_move_insn (validize_mem (mem),
914 : 1682 : force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
915 : :
916 : 841 : stack_save = gen_rtx_MEM (sa_mode,
917 : : plus_constant (Pmode, buf_addr,
918 : 1682 : 2 * GET_MODE_SIZE (Pmode)));
919 : 841 : set_mem_alias_set (stack_save, setjmp_alias_set);
920 : 841 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
921 : :
922 : : /* If there is further processing to do, do it. */
923 : 841 : if (targetm.have_builtin_setjmp_setup ())
924 : 0 : emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
925 : :
926 : : /* We have a nonlocal label. */
927 : 841 : cfun->has_nonlocal_label = 1;
928 : 841 : }
929 : :
930 : : /* Construct the trailing part of a __builtin_setjmp call. This is
931 : : also called directly by the SJLJ exception handling code.
932 : : If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
933 : :
934 : : void
935 : 1343 : expand_builtin_setjmp_receiver (rtx receiver_label)
936 : : {
937 : 1343 : rtx chain;
938 : :
939 : : /* Mark the FP as used when we get here, so we have to make sure it's
940 : : marked as used by this function. */
941 : 1343 : emit_use (hard_frame_pointer_rtx);
942 : :
943 : : /* Mark the static chain as clobbered here so life information
944 : : doesn't get messed up for it. */
945 : 1343 : chain = rtx_for_static_chain (current_function_decl, true);
946 : 1343 : if (chain && REG_P (chain))
947 : 1 : emit_clobber (chain);
948 : :
949 : 1343 : if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
950 : : {
951 : : /* If the argument pointer can be eliminated in favor of the
952 : : frame pointer, we don't need to restore it. We assume here
953 : : that if such an elimination is present, it can always be used.
954 : : This is the case on all known machines; if we don't make this
955 : : assumption, we do unnecessary saving on many machines. */
956 : : size_t i;
957 : : static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
958 : :
959 : 2686 : for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
960 : 2686 : if (elim_regs[i].from == ARG_POINTER_REGNUM
961 : 2686 : && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
962 : : break;
963 : :
964 : 1343 : if (i == ARRAY_SIZE (elim_regs))
965 : : {
966 : : /* Now restore our arg pointer from the address at which it
967 : : was saved in our stack frame. */
968 : 0 : emit_move_insn (crtl->args.internal_arg_pointer,
969 : : copy_to_reg (get_arg_pointer_save_area ()));
970 : : }
971 : : }
972 : :
973 : 1343 : if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
974 : 0 : emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
975 : 1343 : else if (targetm.have_nonlocal_goto_receiver ())
976 : 0 : emit_insn (targetm.gen_nonlocal_goto_receiver ());
977 : : else
978 : : { /* Nothing */ }
979 : :
980 : : /* We must not allow the code we just generated to be reordered by
981 : : scheduling. Specifically, the update of the frame pointer must
982 : : happen immediately, not later. */
983 : 1343 : emit_insn (gen_blockage ());
984 : 1343 : }
985 : :
986 : : /* __builtin_longjmp is passed a pointer to an array of five words (not
987 : : all will be used on all machines). It operates similarly to the C
988 : : library function of the same name, but is more efficient. Much of
989 : : the code below is copied from the handling of non-local gotos. */
990 : :
991 : : static void
992 : 391 : expand_builtin_longjmp (rtx buf_addr, rtx value)
993 : : {
994 : 391 : rtx fp, lab, stack;
995 : 391 : rtx_insn *insn, *last;
996 : 391 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
997 : :
998 : : /* DRAP is needed for stack realign if longjmp is expanded to current
999 : : function */
1000 : 391 : if (SUPPORTS_STACK_ALIGNMENT)
1001 : 391 : crtl->need_drap = true;
1002 : :
1003 : 391 : if (setjmp_alias_set == -1)
1004 : 330 : setjmp_alias_set = new_alias_set ();
1005 : :
1006 : 391 : buf_addr = convert_memory_address (Pmode, buf_addr);
1007 : :
1008 : 392 : buf_addr = force_reg (Pmode, buf_addr);
1009 : :
1010 : : /* We require that the user must pass a second argument of 1, because
1011 : : that is what builtin_setjmp will return. */
1012 : 391 : gcc_assert (value == const1_rtx);
1013 : :
1014 : 391 : last = get_last_insn ();
1015 : 391 : if (targetm.have_builtin_longjmp ())
1016 : 0 : emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1017 : : else
1018 : : {
1019 : 392 : fp = gen_rtx_MEM (Pmode, buf_addr);
1020 : 392 : lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1021 : 782 : GET_MODE_SIZE (Pmode)));
1022 : :
1023 : 391 : stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1024 : 782 : 2 * GET_MODE_SIZE (Pmode)));
1025 : 391 : set_mem_alias_set (fp, setjmp_alias_set);
1026 : 391 : set_mem_alias_set (lab, setjmp_alias_set);
1027 : 391 : set_mem_alias_set (stack, setjmp_alias_set);
1028 : :
1029 : : /* Pick up FP, label, and SP from the block and jump. This code is
1030 : : from expand_goto in stmt.cc; see there for detailed comments. */
1031 : 391 : if (targetm.have_nonlocal_goto ())
1032 : : /* We have to pass a value to the nonlocal_goto pattern that will
1033 : : get copied into the static_chain pointer, but it does not matter
1034 : : what that value is, because builtin_setjmp does not use it. */
1035 : 0 : emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1036 : : else
1037 : : {
1038 : 391 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1039 : 391 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1040 : :
1041 : 391 : lab = copy_to_reg (lab);
1042 : :
1043 : : /* Restore the frame pointer and stack pointer. We must use a
1044 : : temporary since the setjmp buffer may be a local. */
1045 : 391 : fp = copy_to_reg (fp);
1046 : 391 : emit_stack_restore (SAVE_NONLOCAL, stack);
1047 : :
1048 : : /* Ensure the frame pointer move is not optimized. */
1049 : 391 : emit_insn (gen_blockage ());
1050 : 391 : emit_clobber (hard_frame_pointer_rtx);
1051 : 391 : emit_clobber (frame_pointer_rtx);
1052 : 391 : emit_move_insn (hard_frame_pointer_rtx, fp);
1053 : :
1054 : 391 : emit_use (hard_frame_pointer_rtx);
1055 : 391 : emit_use (stack_pointer_rtx);
1056 : 391 : emit_indirect_jump (lab);
1057 : : }
1058 : : }
1059 : :
1060 : : /* Search backwards and mark the jump insn as a non-local goto.
1061 : : Note that this precludes the use of __builtin_longjmp to a
1062 : : __builtin_setjmp target in the same function. However, we've
1063 : : already cautioned the user that these functions are for
1064 : : internal exception handling use only. */
1065 : 782 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1066 : : {
1067 : 782 : gcc_assert (insn != last);
1068 : :
1069 : 782 : if (JUMP_P (insn))
1070 : : {
1071 : 391 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1072 : 391 : break;
1073 : : }
1074 : 391 : else if (CALL_P (insn))
1075 : : break;
1076 : : }
1077 : 391 : }
1078 : :
1079 : : static inline bool
1080 : 2149124 : more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1081 : : {
1082 : 2149124 : return (iter->i < iter->n);
1083 : : }
1084 : :
1085 : : /* This function validates the types of a function call argument list
1086 : : against a specified list of tree_codes. If the last specifier is a 0,
1087 : : that represents an ellipsis, otherwise the last specifier must be a
1088 : : VOID_TYPE. */
1089 : :
1090 : : static bool
1091 : 459842 : validate_arglist (const_tree callexpr, ...)
1092 : : {
1093 : 459842 : enum tree_code code;
1094 : 459842 : bool res = 0;
1095 : 459842 : va_list ap;
1096 : 459842 : const_call_expr_arg_iterator iter;
1097 : 459842 : const_tree arg;
1098 : :
1099 : 459842 : va_start (ap, callexpr);
1100 : 459842 : init_const_call_expr_arg_iterator (callexpr, &iter);
1101 : :
1102 : : /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1103 : 459842 : tree fn = CALL_EXPR_FN (callexpr);
1104 : 459842 : bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1105 : :
1106 : 459842 : for (unsigned argno = 1; ; ++argno)
1107 : : {
1108 : 1587465 : code = (enum tree_code) va_arg (ap, int);
1109 : :
1110 : 1587465 : switch (code)
1111 : : {
1112 : 2037 : case 0:
1113 : : /* This signifies an ellipses, any further arguments are all ok. */
1114 : 2037 : res = true;
1115 : 2037 : goto end;
1116 : 457572 : case VOID_TYPE:
1117 : : /* This signifies an endlink, if no arguments remain, return
1118 : : true, otherwise return false. */
1119 : 457572 : res = !more_const_call_expr_args_p (&iter);
1120 : 457572 : goto end;
1121 : 769917 : case POINTER_TYPE:
1122 : : /* The actual argument must be nonnull when either the whole
1123 : : called function has been declared nonnull, or when the formal
1124 : : argument corresponding to the actual argument has been. */
1125 : 769917 : if (argmap
1126 : 769917 : && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1127 : : {
1128 : 284173 : arg = next_const_call_expr_arg (&iter);
1129 : 284173 : if (!validate_arg (arg, code) || integer_zerop (arg))
1130 : 59 : goto end;
1131 : : break;
1132 : : }
1133 : : /* FALLTHRU */
1134 : 843683 : default:
1135 : : /* If no parameters remain or the parameter's code does not
1136 : : match the specified code, return false. Otherwise continue
1137 : : checking any remaining arguments. */
1138 : 843683 : arg = next_const_call_expr_arg (&iter);
1139 : 843683 : if (!validate_arg (arg, code))
1140 : 174 : goto end;
1141 : : break;
1142 : : }
1143 : : }
1144 : :
1145 : : /* We need gotos here since we can only have one VA_CLOSE in a
1146 : : function. */
1147 : 459842 : end: ;
1148 : 459842 : va_end (ap);
1149 : :
1150 : 459842 : BITMAP_FREE (argmap);
1151 : :
1152 : 459842 : if (res)
1153 : 459609 : for (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (TREE_TYPE (fn)));
1154 : 699189 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
1155 : 239580 : attrs = TREE_CHAIN (attrs))
1156 : : {
1157 : 239602 : tree args = TREE_VALUE (attrs);
1158 : 239602 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
1159 : 239602 : unsigned int idx2
1160 : 239602 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
1161 : 239602 : unsigned int idx3 = idx2;
1162 : 239602 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
1163 : 0 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
1164 : 239602 : if (idx < (unsigned) call_expr_nargs (callexpr)
1165 : 239602 : && idx2 < (unsigned) call_expr_nargs (callexpr)
1166 : 239602 : && idx3 < (unsigned) call_expr_nargs (callexpr)
1167 : 239602 : && POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx)))
1168 : 239602 : && integer_zerop (CALL_EXPR_ARG (callexpr, idx))
1169 : 83 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx2)))
1170 : 83 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx2))
1171 : 22 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx3)))
1172 : 239624 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx3)))
1173 : : return false;
1174 : : }
1175 : :
1176 : : return res;
1177 : : }
1178 : :
1179 : : /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1180 : : and the address of the save area. */
1181 : :
1182 : : static rtx
1183 : 510 : expand_builtin_nonlocal_goto (tree exp)
1184 : : {
1185 : 510 : tree t_label, t_save_area;
1186 : 510 : rtx r_label, r_save_area, r_fp, r_sp;
1187 : 510 : rtx_insn *insn;
1188 : :
1189 : 510 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1190 : : return NULL_RTX;
1191 : :
1192 : 510 : t_label = CALL_EXPR_ARG (exp, 0);
1193 : 510 : t_save_area = CALL_EXPR_ARG (exp, 1);
1194 : :
1195 : 510 : r_label = expand_normal (t_label);
1196 : 510 : r_label = convert_memory_address (Pmode, r_label);
1197 : 510 : r_save_area = expand_normal (t_save_area);
1198 : 510 : r_save_area = convert_memory_address (Pmode, r_save_area);
1199 : : /* Copy the address of the save location to a register just in case it was
1200 : : based on the frame pointer. */
1201 : 510 : r_save_area = copy_to_reg (r_save_area);
1202 : 510 : r_fp = gen_rtx_MEM (Pmode, r_save_area);
1203 : 510 : r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1204 : : plus_constant (Pmode, r_save_area,
1205 : 1020 : GET_MODE_SIZE (Pmode)));
1206 : :
1207 : 510 : crtl->has_nonlocal_goto = 1;
1208 : :
1209 : : /* ??? We no longer need to pass the static chain value, afaik. */
1210 : 510 : if (targetm.have_nonlocal_goto ())
1211 : 0 : emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1212 : : else
1213 : : {
1214 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1215 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1216 : :
1217 : 510 : r_label = copy_to_reg (r_label);
1218 : :
1219 : : /* Restore the frame pointer and stack pointer. We must use a
1220 : : temporary since the setjmp buffer may be a local. */
1221 : 510 : r_fp = copy_to_reg (r_fp);
1222 : 510 : emit_stack_restore (SAVE_NONLOCAL, r_sp);
1223 : :
1224 : : /* Ensure the frame pointer move is not optimized. */
1225 : 510 : emit_insn (gen_blockage ());
1226 : 510 : emit_clobber (hard_frame_pointer_rtx);
1227 : 510 : emit_clobber (frame_pointer_rtx);
1228 : 510 : emit_move_insn (hard_frame_pointer_rtx, r_fp);
1229 : :
1230 : : /* USE of hard_frame_pointer_rtx added for consistency;
1231 : : not clear if really needed. */
1232 : 510 : emit_use (hard_frame_pointer_rtx);
1233 : 510 : emit_use (stack_pointer_rtx);
1234 : :
1235 : : /* If the architecture is using a GP register, we must
1236 : : conservatively assume that the target function makes use of it.
1237 : : The prologue of functions with nonlocal gotos must therefore
1238 : : initialize the GP register to the appropriate value, and we
1239 : : must then make sure that this value is live at the point
1240 : : of the jump. (Note that this doesn't necessarily apply
1241 : : to targets with a nonlocal_goto pattern; they are free
1242 : : to implement it in their own way. Note also that this is
1243 : : a no-op if the GP register is a global invariant.) */
1244 : 510 : unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1245 : 0 : if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1246 : 0 : emit_use (pic_offset_table_rtx);
1247 : :
1248 : 510 : emit_indirect_jump (r_label);
1249 : : }
1250 : :
1251 : : /* Search backwards to the jump insn and mark it as a
1252 : : non-local goto. */
1253 : 1020 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1254 : : {
1255 : 1020 : if (JUMP_P (insn))
1256 : : {
1257 : 510 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1258 : 510 : break;
1259 : : }
1260 : 510 : else if (CALL_P (insn))
1261 : : break;
1262 : : }
1263 : :
1264 : 510 : return const0_rtx;
1265 : : }
1266 : :
1267 : : /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1268 : : (not all will be used on all machines) that was passed to __builtin_setjmp.
1269 : : It updates the stack pointer in that block to the current value. This is
1270 : : also called directly by the SJLJ exception handling code. */
1271 : :
1272 : : void
1273 : 0 : expand_builtin_update_setjmp_buf (rtx buf_addr)
1274 : : {
1275 : 0 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1276 : 0 : buf_addr = convert_memory_address (Pmode, buf_addr);
1277 : 0 : rtx stack_save
1278 : 0 : = gen_rtx_MEM (sa_mode,
1279 : 0 : memory_address
1280 : : (sa_mode,
1281 : : plus_constant (Pmode, buf_addr,
1282 : 0 : 2 * GET_MODE_SIZE (Pmode))));
1283 : :
1284 : 0 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
1285 : 0 : }
1286 : :
1287 : : /* Expand a call to __builtin_prefetch. For a target that does not support
1288 : : data prefetch, evaluate the memory address argument in case it has side
1289 : : effects. */
1290 : :
1291 : : static void
1292 : 2037 : expand_builtin_prefetch (tree exp)
1293 : : {
1294 : 2037 : tree arg0, arg1, arg2;
1295 : 2037 : int nargs;
1296 : 2037 : rtx op0, op1, op2;
1297 : :
1298 : 2037 : if (!validate_arglist (exp, POINTER_TYPE, 0))
1299 : : return;
1300 : :
1301 : 2037 : arg0 = CALL_EXPR_ARG (exp, 0);
1302 : :
1303 : : /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1304 : : zero (read) and argument 2 (locality) defaults to 3 (high degree of
1305 : : locality). */
1306 : 2037 : nargs = call_expr_nargs (exp);
1307 : 2037 : arg1 = nargs > 1 ? CALL_EXPR_ARG (exp, 1) : NULL_TREE;
1308 : 2001 : arg2 = nargs > 2 ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
1309 : :
1310 : : /* Argument 0 is an address. */
1311 : 2133 : op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1312 : :
1313 : : /* Argument 1 (read/write flag) must be a compile-time constant int. */
1314 : 2037 : if (arg1 == NULL_TREE)
1315 : 36 : op1 = const0_rtx;
1316 : 2001 : else if (TREE_CODE (arg1) != INTEGER_CST)
1317 : : {
1318 : 0 : error ("second argument to %<__builtin_prefetch%> must be a constant");
1319 : 0 : op1 = const0_rtx;
1320 : : }
1321 : : else
1322 : 2001 : op1 = expand_normal (arg1);
1323 : : /* Argument 1 must be 0, 1 or 2. */
1324 : 2037 : if (!IN_RANGE (INTVAL (op1), 0, 2))
1325 : : {
1326 : 3 : warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1327 : : " using zero");
1328 : 3 : op1 = const0_rtx;
1329 : : }
1330 : :
1331 : : /* Argument 2 (locality) must be a compile-time constant int. */
1332 : 2037 : if (arg2 == NULL_TREE)
1333 : 61 : op2 = GEN_INT (3);
1334 : 1976 : else if (TREE_CODE (arg2) != INTEGER_CST)
1335 : : {
1336 : 0 : error ("third argument to %<__builtin_prefetch%> must be a constant");
1337 : 0 : op2 = const0_rtx;
1338 : : }
1339 : : else
1340 : 1976 : op2 = expand_normal (arg2);
1341 : : /* Argument 2 must be 0, 1, 2, or 3. */
1342 : 2037 : if (!IN_RANGE (INTVAL (op2), 0, 3))
1343 : : {
1344 : 3 : warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1345 : 3 : op2 = const0_rtx;
1346 : : }
1347 : :
1348 : 2037 : if (targetm.have_prefetch ())
1349 : : {
1350 : 2037 : class expand_operand ops[3];
1351 : :
1352 : 2037 : create_address_operand (&ops[0], op0);
1353 : 2037 : create_integer_operand (&ops[1], INTVAL (op1));
1354 : 2037 : create_integer_operand (&ops[2], INTVAL (op2));
1355 : 2037 : if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1356 : 2037 : return;
1357 : : }
1358 : :
1359 : : /* Don't do anything with direct references to volatile memory, but
1360 : : generate code to handle other side effects. */
1361 : 0 : if (!MEM_P (op0) && side_effects_p (op0))
1362 : 0 : emit_insn (op0);
1363 : : }
1364 : :
1365 : : /* Get a MEM rtx for expression EXP which is the address of an operand
1366 : : to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1367 : : the maximum length of the block of memory that might be accessed or
1368 : : NULL if unknown. */
1369 : :
1370 : : rtx
1371 : 703611 : get_memory_rtx (tree exp, tree len)
1372 : : {
1373 : 703611 : tree orig_exp = exp, base;
1374 : 703611 : rtx addr, mem;
1375 : :
1376 : 703611 : gcc_checking_assert
1377 : : (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))));
1378 : :
1379 : : /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1380 : : from its expression, for expr->a.b only <variable>.a.b is recorded. */
1381 : 703611 : if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1382 : 1342 : exp = TREE_OPERAND (exp, 0);
1383 : :
1384 : 703611 : addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1385 : 703611 : mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1386 : :
1387 : : /* Get an expression we can use to find the attributes to assign to MEM.
1388 : : First remove any nops. */
1389 : 1407222 : while (CONVERT_EXPR_P (exp)
1390 : 703611 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1391 : 0 : exp = TREE_OPERAND (exp, 0);
1392 : :
1393 : : /* Build a MEM_REF representing the whole accessed area as a byte blob,
1394 : : (as builtin stringops may alias with anything). */
1395 : 703611 : exp = fold_build2 (MEM_REF,
1396 : : build_array_type (char_type_node,
1397 : : build_range_type (sizetype,
1398 : : size_one_node, len)),
1399 : : exp, build_int_cst (ptr_type_node, 0));
1400 : :
1401 : : /* If the MEM_REF has no acceptable address, try to get the base object
1402 : : from the original address we got, and build an all-aliasing
1403 : : unknown-sized access to that one. */
1404 : 703611 : if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1405 : 703599 : set_mem_attributes (mem, exp, 0);
1406 : 12 : else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1407 : 12 : && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1408 : : 0))))
1409 : : {
1410 : 12 : unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1411 : 12 : exp = build_fold_addr_expr (base);
1412 : 12 : exp = fold_build2 (MEM_REF,
1413 : : build_array_type (char_type_node,
1414 : : build_range_type (sizetype,
1415 : : size_zero_node,
1416 : : NULL)),
1417 : : exp, build_int_cst (ptr_type_node, 0));
1418 : 12 : set_mem_attributes (mem, exp, 0);
1419 : : /* Since we stripped parts make sure the offset is unknown and the
1420 : : alignment is computed from the original address. */
1421 : 12 : clear_mem_offset (mem);
1422 : 12 : set_mem_align (mem, align);
1423 : : }
1424 : 703611 : set_mem_alias_set (mem, 0);
1425 : 703611 : return mem;
1426 : : }
1427 : :
1428 : : /* Built-in functions to perform an untyped call and return. */
1429 : :
1430 : : /* Wrapper that implicitly applies a delta when getting or setting the
1431 : : enclosed value. */
1432 : : template <typename T>
1433 : : class delta_type
1434 : : {
1435 : : T &value; T const delta;
1436 : : public:
1437 : 2524 : delta_type (T &val, T dlt) : value (val), delta (dlt) {}
1438 : 1809 : operator T () const { return value + delta; }
1439 : 715 : T operator = (T val) const { value = val - delta; return val; }
1440 : : };
1441 : :
1442 : : #define saved_apply_args_size \
1443 : : (delta_type<int> (this_target_builtins->x_apply_args_size_plus_one, -1))
1444 : : #define apply_args_mode \
1445 : : (this_target_builtins->x_apply_args_mode)
1446 : : #define saved_apply_result_size \
1447 : : (delta_type<int> (this_target_builtins->x_apply_result_size_plus_one, -1))
1448 : : #define apply_result_mode \
1449 : : (this_target_builtins->x_apply_result_mode)
1450 : :
1451 : : /* Return the size required for the block returned by __builtin_apply_args,
1452 : : and initialize apply_args_mode. */
1453 : :
1454 : : static int
1455 : 947 : apply_args_size (void)
1456 : : {
1457 : 947 : int size = saved_apply_args_size;
1458 : 947 : int align;
1459 : 947 : unsigned int regno;
1460 : :
1461 : : /* The values computed by this function never change. */
1462 : 947 : if (size < 0)
1463 : : {
1464 : : /* The first value is the incoming arg-pointer. */
1465 : 360 : size = GET_MODE_SIZE (Pmode);
1466 : :
1467 : : /* The second value is the structure value address unless this is
1468 : : passed as an "invisible" first argument. */
1469 : 360 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1470 : 0 : size += GET_MODE_SIZE (Pmode);
1471 : :
1472 : 33480 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 : 33120 : if (FUNCTION_ARG_REGNO_P (regno))
1474 : : {
1475 : 5400 : fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1476 : :
1477 : 5400 : if (mode != VOIDmode)
1478 : : {
1479 : 5400 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1480 : 5400 : if (size % align != 0)
1481 : 1 : size = CEIL (size, align) * align;
1482 : 5400 : size += GET_MODE_SIZE (mode);
1483 : 5400 : apply_args_mode[regno] = mode;
1484 : : }
1485 : : else
1486 : 0 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1487 : : }
1488 : : else
1489 : 27720 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1490 : :
1491 : 360 : saved_apply_args_size = size;
1492 : : }
1493 : 947 : return size;
1494 : : }
1495 : :
1496 : : /* Return the size required for the block returned by __builtin_apply,
1497 : : and initialize apply_result_mode. */
1498 : :
1499 : : static int
1500 : 862 : apply_result_size (void)
1501 : : {
1502 : 862 : int size = saved_apply_result_size;
1503 : 862 : int align, regno;
1504 : :
1505 : : /* The values computed by this function never change. */
1506 : 862 : if (size < 0)
1507 : : {
1508 : : size = 0;
1509 : :
1510 : 33015 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1511 : 32660 : if (targetm.calls.function_value_regno_p (regno))
1512 : : {
1513 : 2840 : fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1514 : :
1515 : 2840 : if (mode != VOIDmode)
1516 : : {
1517 : 2840 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1518 : 2840 : if (size % align != 0)
1519 : : size = CEIL (size, align) * align;
1520 : 2840 : size += GET_MODE_SIZE (mode);
1521 : 2840 : apply_result_mode[regno] = mode;
1522 : : }
1523 : : else
1524 : 0 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1525 : : }
1526 : : else
1527 : 29820 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1528 : :
1529 : : /* Allow targets that use untyped_call and untyped_return to override
1530 : : the size so that machine-specific information can be stored here. */
1531 : : #ifdef APPLY_RESULT_SIZE
1532 : 355 : size = APPLY_RESULT_SIZE;
1533 : : #endif
1534 : :
1535 : 355 : saved_apply_result_size = size;
1536 : : }
1537 : 862 : return size;
1538 : : }
1539 : :
1540 : : /* Create a vector describing the result block RESULT. If SAVEP is true,
1541 : : the result block is used to save the values; otherwise it is used to
1542 : : restore the values. */
1543 : :
1544 : : static rtx
1545 : 483 : result_vector (int savep, rtx result)
1546 : : {
1547 : 483 : int regno, size, align, nelts;
1548 : 483 : fixed_size_mode mode;
1549 : 483 : rtx reg, mem;
1550 : 483 : rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1551 : :
1552 : 483 : size = nelts = 0;
1553 : 44919 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1554 : 44436 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1555 : : {
1556 : 3864 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1557 : 3864 : if (size % align != 0)
1558 : 0 : size = CEIL (size, align) * align;
1559 : 3864 : reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1560 : 3864 : mem = adjust_address (result, mode, size);
1561 : 7728 : savevec[nelts++] = (savep
1562 : 7728 : ? gen_rtx_SET (mem, reg)
1563 : 0 : : gen_rtx_SET (reg, mem));
1564 : 7728 : size += GET_MODE_SIZE (mode);
1565 : : }
1566 : 483 : return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1567 : : }
1568 : :
1569 : : /* Save the state required to perform an untyped call with the same
1570 : : arguments as were passed to the current function. */
1571 : :
1572 : : static rtx
1573 : 464 : expand_builtin_apply_args_1 (void)
1574 : : {
1575 : 464 : rtx registers, tem;
1576 : 464 : int size, align, regno;
1577 : 464 : fixed_size_mode mode;
1578 : 464 : rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1579 : :
1580 : : /* Create a block where the arg-pointer, structure value address,
1581 : : and argument registers can be saved. */
1582 : 464 : registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1583 : :
1584 : : /* Walk past the arg-pointer and structure value address. */
1585 : 464 : size = GET_MODE_SIZE (Pmode);
1586 : 464 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1587 : 0 : size += GET_MODE_SIZE (Pmode);
1588 : :
1589 : : /* Save each register used in calling a function to the block. */
1590 : 43152 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1591 : 42688 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1592 : : {
1593 : 6960 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1594 : 6960 : if (size % align != 0)
1595 : 1 : size = CEIL (size, align) * align;
1596 : :
1597 : 6960 : tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1598 : :
1599 : 6960 : emit_move_insn (adjust_address (registers, mode, size), tem);
1600 : 13920 : size += GET_MODE_SIZE (mode);
1601 : : }
1602 : :
1603 : : /* Save the arg pointer to the block. */
1604 : 464 : tem = copy_to_reg (crtl->args.internal_arg_pointer);
1605 : : /* We need the pointer as the caller actually passed them to us, not
1606 : : as we might have pretended they were passed. Make sure it's a valid
1607 : : operand, as emit_move_insn isn't expected to handle a PLUS. */
1608 : 464 : if (STACK_GROWS_DOWNWARD)
1609 : 464 : tem
1610 : 464 : = force_operand (plus_constant (Pmode, tem,
1611 : 464 : crtl->args.pretend_args_size),
1612 : : NULL_RTX);
1613 : 464 : emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1614 : :
1615 : 464 : size = GET_MODE_SIZE (Pmode);
1616 : :
1617 : : /* Save the structure value address unless this is passed as an
1618 : : "invisible" first argument. */
1619 : 464 : if (struct_incoming_value)
1620 : 0 : emit_move_insn (adjust_address (registers, Pmode, size),
1621 : : copy_to_reg (struct_incoming_value));
1622 : :
1623 : : /* Return the address of the block. */
1624 : 464 : return copy_addr_to_reg (XEXP (registers, 0));
1625 : : }
1626 : :
1627 : : /* __builtin_apply_args returns block of memory allocated on
1628 : : the stack into which is stored the arg pointer, structure
1629 : : value address, static chain, and all the registers that might
1630 : : possibly be used in performing a function call. The code is
1631 : : moved to the start of the function so the incoming values are
1632 : : saved. */
1633 : :
1634 : : static rtx
1635 : 464 : expand_builtin_apply_args (void)
1636 : : {
1637 : : /* Don't do __builtin_apply_args more than once in a function.
1638 : : Save the result of the first call and reuse it. */
1639 : 464 : if (apply_args_value != 0)
1640 : : return apply_args_value;
1641 : 464 : {
1642 : : /* When this function is called, it means that registers must be
1643 : : saved on entry to this function. So we migrate the
1644 : : call to the first insn of this function. */
1645 : 464 : rtx temp;
1646 : :
1647 : 464 : start_sequence ();
1648 : 464 : temp = expand_builtin_apply_args_1 ();
1649 : 464 : rtx_insn *seq = end_sequence ();
1650 : :
1651 : 464 : apply_args_value = temp;
1652 : :
1653 : : /* Put the insns after the NOTE that starts the function.
1654 : : If this is inside a start_sequence, make the outer-level insn
1655 : : chain current, so the code is placed at the start of the
1656 : : function. If internal_arg_pointer is a non-virtual pseudo,
1657 : : it needs to be placed after the function that initializes
1658 : : that pseudo. */
1659 : 464 : push_topmost_sequence ();
1660 : 464 : if (REG_P (crtl->args.internal_arg_pointer)
1661 : 464 : && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1662 : 0 : emit_insn_before (seq, parm_birth_insn);
1663 : : else
1664 : 464 : emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1665 : 464 : pop_topmost_sequence ();
1666 : 464 : return temp;
1667 : : }
1668 : : }
1669 : :
1670 : : /* Perform an untyped call and save the state required to perform an
1671 : : untyped return of whatever value was returned by the given function. */
1672 : :
1673 : : static rtx
1674 : 483 : expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1675 : : {
1676 : 483 : int size, align, regno;
1677 : 483 : fixed_size_mode mode;
1678 : 483 : rtx incoming_args, result, reg, dest, src;
1679 : 483 : rtx_call_insn *call_insn;
1680 : 483 : rtx old_stack_level = 0;
1681 : 483 : rtx call_fusage = 0;
1682 : 483 : rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1683 : :
1684 : 483 : arguments = convert_memory_address (Pmode, arguments);
1685 : :
1686 : : /* Create a block where the return registers can be saved. */
1687 : 483 : result = assign_stack_local (BLKmode, apply_result_size (), -1);
1688 : :
1689 : : /* Fetch the arg pointer from the ARGUMENTS block. */
1690 : 483 : incoming_args = gen_reg_rtx (Pmode);
1691 : 483 : emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1692 : 483 : if (!STACK_GROWS_DOWNWARD)
1693 : : incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1694 : : incoming_args, 0, OPTAB_LIB_WIDEN);
1695 : :
1696 : : /* Push a new argument block and copy the arguments. Do not allow
1697 : : the (potential) memcpy call below to interfere with our stack
1698 : : manipulations. */
1699 : 483 : do_pending_stack_adjust ();
1700 : 483 : NO_DEFER_POP;
1701 : :
1702 : : /* Save the stack with nonlocal if available. */
1703 : 483 : if (targetm.have_save_stack_nonlocal ())
1704 : 483 : emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1705 : : else
1706 : 0 : emit_stack_save (SAVE_BLOCK, &old_stack_level);
1707 : :
1708 : : /* Allocate a block of memory onto the stack and copy the memory
1709 : : arguments to the outgoing arguments address. We can pass TRUE
1710 : : as the 4th argument because we just saved the stack pointer
1711 : : and will restore it right after the call. */
1712 : 966 : allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1713 : :
1714 : : /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1715 : : may have already set current_function_calls_alloca to true.
1716 : : current_function_calls_alloca won't be set if argsize is zero,
1717 : : so we have to guarantee need_drap is true here. */
1718 : 483 : if (SUPPORTS_STACK_ALIGNMENT)
1719 : 483 : crtl->need_drap = true;
1720 : :
1721 : 483 : dest = virtual_outgoing_args_rtx;
1722 : 483 : if (!STACK_GROWS_DOWNWARD)
1723 : : {
1724 : : if (CONST_INT_P (argsize))
1725 : : dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1726 : : else
1727 : : dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1728 : : }
1729 : 483 : dest = gen_rtx_MEM (BLKmode, dest);
1730 : 483 : set_mem_align (dest, PARM_BOUNDARY);
1731 : 483 : src = gen_rtx_MEM (BLKmode, incoming_args);
1732 : 483 : set_mem_align (src, PARM_BOUNDARY);
1733 : 483 : emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1734 : :
1735 : : /* Refer to the argument block. */
1736 : 483 : apply_args_size ();
1737 : 483 : arguments = gen_rtx_MEM (BLKmode, arguments);
1738 : 483 : set_mem_align (arguments, PARM_BOUNDARY);
1739 : :
1740 : : /* Walk past the arg-pointer and structure value address. */
1741 : 483 : size = GET_MODE_SIZE (Pmode);
1742 : 483 : if (struct_value)
1743 : 0 : size += GET_MODE_SIZE (Pmode);
1744 : :
1745 : : /* Restore each of the registers previously saved. Make USE insns
1746 : : for each of these registers for use in making the call. */
1747 : 44919 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1748 : 44436 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1749 : : {
1750 : 7245 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1751 : 7245 : if (size % align != 0)
1752 : 0 : size = CEIL (size, align) * align;
1753 : 7245 : reg = gen_rtx_REG (mode, regno);
1754 : 7245 : emit_move_insn (reg, adjust_address (arguments, mode, size));
1755 : 7245 : use_reg (&call_fusage, reg);
1756 : 14490 : size += GET_MODE_SIZE (mode);
1757 : : }
1758 : :
1759 : : /* Restore the structure value address unless this is passed as an
1760 : : "invisible" first argument. */
1761 : 483 : size = GET_MODE_SIZE (Pmode);
1762 : 483 : if (struct_value)
1763 : : {
1764 : 0 : rtx value = gen_reg_rtx (Pmode);
1765 : 0 : emit_move_insn (value, adjust_address (arguments, Pmode, size));
1766 : 0 : emit_move_insn (struct_value, value);
1767 : 0 : if (REG_P (struct_value))
1768 : 0 : use_reg (&call_fusage, struct_value);
1769 : : }
1770 : :
1771 : : /* All arguments and registers used for the call are set up by now! */
1772 : 483 : function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1773 : :
1774 : : /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1775 : : and we don't want to load it into a register as an optimization,
1776 : : because prepare_call_address already did it if it should be done. */
1777 : 483 : if (GET_CODE (function) != SYMBOL_REF)
1778 : 29 : function = memory_address (FUNCTION_MODE, function);
1779 : :
1780 : : /* Generate the actual call instruction and save the return value. */
1781 : 483 : if (targetm.have_untyped_call ())
1782 : : {
1783 : 483 : rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1784 : 483 : rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1785 : : result_vector (1, result));
1786 : 5800 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1787 : 5317 : if (CALL_P (insn))
1788 : 483 : add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1789 : 483 : emit_insn (seq);
1790 : : }
1791 : 0 : else if (targetm.have_call_value ())
1792 : : {
1793 : : rtx valreg = 0;
1794 : :
1795 : : /* Locate the unique return register. It is not possible to
1796 : : express a call that sets more than one return register using
1797 : : call_value; use untyped_call for that. In fact, untyped_call
1798 : : only needs to save the return registers in the given block. */
1799 : 0 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1800 : 0 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1801 : : {
1802 : 0 : gcc_assert (!valreg); /* have_untyped_call required. */
1803 : :
1804 : 0 : valreg = gen_rtx_REG (mode, regno);
1805 : : }
1806 : :
1807 : 0 : emit_insn (targetm.gen_call_value (valreg,
1808 : : gen_rtx_MEM (FUNCTION_MODE, function),
1809 : : const0_rtx, NULL_RTX, const0_rtx));
1810 : :
1811 : 0 : emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1812 : : }
1813 : : else
1814 : 0 : gcc_unreachable ();
1815 : :
1816 : : /* Find the CALL insn we just emitted, and attach the register usage
1817 : : information. */
1818 : 483 : call_insn = last_call_insn ();
1819 : 483 : add_function_usage_to (call_insn, call_fusage);
1820 : :
1821 : : /* Restore the stack. */
1822 : 483 : if (targetm.have_save_stack_nonlocal ())
1823 : 483 : emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1824 : : else
1825 : 0 : emit_stack_restore (SAVE_BLOCK, old_stack_level);
1826 : 483 : fixup_args_size_notes (call_insn, get_last_insn (), 0);
1827 : :
1828 : 483 : OK_DEFER_POP;
1829 : :
1830 : : /* Return the address of the result block. */
1831 : 483 : result = copy_addr_to_reg (XEXP (result, 0));
1832 : 483 : return convert_memory_address (ptr_mode, result);
1833 : : }
1834 : :
1835 : : /* Perform an untyped return. */
1836 : :
1837 : : static void
1838 : 379 : expand_builtin_return (rtx result)
1839 : : {
1840 : 379 : int size, align, regno;
1841 : 379 : fixed_size_mode mode;
1842 : 379 : rtx reg;
1843 : 379 : rtx_insn *call_fusage = 0;
1844 : :
1845 : 379 : result = convert_memory_address (Pmode, result);
1846 : :
1847 : 379 : apply_result_size ();
1848 : 379 : result = gen_rtx_MEM (BLKmode, result);
1849 : :
1850 : 379 : if (targetm.have_untyped_return ())
1851 : : {
1852 : 0 : rtx vector = result_vector (0, result);
1853 : 0 : emit_jump_insn (targetm.gen_untyped_return (result, vector));
1854 : 0 : emit_barrier ();
1855 : 0 : return;
1856 : : }
1857 : :
1858 : : /* Restore the return value and note that each value is used. */
1859 : : size = 0;
1860 : 35247 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1861 : 34868 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1862 : : {
1863 : 3032 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1864 : 3032 : if (size % align != 0)
1865 : 0 : size = CEIL (size, align) * align;
1866 : 3032 : reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1867 : 3032 : emit_move_insn (reg, adjust_address (result, mode, size));
1868 : :
1869 : 3032 : push_to_sequence (call_fusage);
1870 : 3032 : emit_use (reg);
1871 : 3032 : call_fusage = end_sequence ();
1872 : 6064 : size += GET_MODE_SIZE (mode);
1873 : : }
1874 : :
1875 : : /* Put the USE insns before the return. */
1876 : 379 : emit_insn (call_fusage);
1877 : :
1878 : : /* Return whatever values was restored by jumping directly to the end
1879 : : of the function. */
1880 : 379 : expand_naked_return ();
1881 : : }
1882 : :
1883 : : /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1884 : :
1885 : : int
1886 : 2429 : type_to_class (tree type)
1887 : : {
1888 : 2429 : switch (TREE_CODE (type))
1889 : : {
1890 : : case VOID_TYPE: return void_type_class;
1891 : 928 : case INTEGER_TYPE: return integer_type_class;
1892 : 39 : case ENUMERAL_TYPE: return enumeral_type_class;
1893 : 38 : case BOOLEAN_TYPE: return boolean_type_class;
1894 : 1014 : case POINTER_TYPE: return pointer_type_class;
1895 : 27 : case REFERENCE_TYPE: return reference_type_class;
1896 : 36 : case OFFSET_TYPE: return offset_type_class;
1897 : 132 : case REAL_TYPE: return real_type_class;
1898 : 40 : case COMPLEX_TYPE: return complex_type_class;
1899 : 20 : case FUNCTION_TYPE: return function_type_class;
1900 : 0 : case METHOD_TYPE: return method_type_class;
1901 : 44 : case RECORD_TYPE: return record_type_class;
1902 : 44 : case UNION_TYPE:
1903 : 44 : case QUAL_UNION_TYPE: return union_type_class;
1904 : 24 : case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1905 : 24 : ? string_type_class : array_type_class);
1906 : 0 : case LANG_TYPE: return lang_type_class;
1907 : 0 : case OPAQUE_TYPE: return opaque_type_class;
1908 : 0 : case BITINT_TYPE: return bitint_type_class;
1909 : 30 : case VECTOR_TYPE: return vector_type_class;
1910 : 3 : default: return no_type_class;
1911 : : }
1912 : : }
1913 : :
1914 : : /* Expand a call EXP to __builtin_classify_type. */
1915 : :
1916 : : static rtx
1917 : 0 : expand_builtin_classify_type (tree exp)
1918 : : {
1919 : 0 : if (call_expr_nargs (exp))
1920 : 0 : return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1921 : 0 : return GEN_INT (no_type_class);
1922 : : }
1923 : :
1924 : : /* This helper macro, meant to be used in mathfn_built_in below, determines
1925 : : which among a set of builtin math functions is appropriate for a given type
1926 : : mode. The `F' (float) and `L' (long double) are automatically generated
1927 : : from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1928 : : types, there are additional types that are considered with 'F32', 'F64',
1929 : : 'F128', etc. suffixes. */
1930 : : #define CASE_MATHFN(MATHFN) \
1931 : : CASE_CFN_##MATHFN: \
1932 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1933 : : fcodel = BUILT_IN_##MATHFN##L ; break;
1934 : : /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1935 : : types. */
1936 : : #define CASE_MATHFN_FLOATN(MATHFN) \
1937 : : CASE_CFN_##MATHFN: \
1938 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1939 : : fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1940 : : fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1941 : : fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1942 : : fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1943 : : break;
1944 : : /* Similar to above, but appends _R after any F/L suffix. */
1945 : : #define CASE_MATHFN_REENT(MATHFN) \
1946 : : case CFN_BUILT_IN_##MATHFN##_R: \
1947 : : case CFN_BUILT_IN_##MATHFN##F_R: \
1948 : : case CFN_BUILT_IN_##MATHFN##L_R: \
1949 : : fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1950 : : fcodel = BUILT_IN_##MATHFN##L_R ; break;
1951 : :
1952 : : /* Return a function equivalent to FN but operating on floating-point
1953 : : values of type TYPE, or END_BUILTINS if no such function exists.
1954 : : This is purely an operation on function codes; it does not guarantee
1955 : : that the target actually has an implementation of the function. */
1956 : :
1957 : : static built_in_function
1958 : 591600 : mathfn_built_in_2 (tree type, combined_fn fn)
1959 : : {
1960 : 591600 : tree mtype;
1961 : 591600 : built_in_function fcode, fcodef, fcodel;
1962 : 591600 : built_in_function fcodef16 = END_BUILTINS;
1963 : 591600 : built_in_function fcodef32 = END_BUILTINS;
1964 : 591600 : built_in_function fcodef64 = END_BUILTINS;
1965 : 591600 : built_in_function fcodef128 = END_BUILTINS;
1966 : 591600 : built_in_function fcodef32x = END_BUILTINS;
1967 : 591600 : built_in_function fcodef64x = END_BUILTINS;
1968 : 591600 : built_in_function fcodef128x = END_BUILTINS;
1969 : :
1970 : : /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1971 : : break the uses below. */
1972 : : #undef HUGE_VAL
1973 : : #undef NAN
1974 : :
1975 : 591600 : switch (fn)
1976 : : {
1977 : : #define SEQ_OF_CASE_MATHFN \
1978 : : CASE_MATHFN_FLOATN (ACOS) \
1979 : : CASE_MATHFN_FLOATN (ACOSH) \
1980 : : CASE_MATHFN_FLOATN (ASIN) \
1981 : : CASE_MATHFN_FLOATN (ASINH) \
1982 : : CASE_MATHFN_FLOATN (ATAN) \
1983 : : CASE_MATHFN_FLOATN (ATAN2) \
1984 : : CASE_MATHFN_FLOATN (ATANH) \
1985 : : CASE_MATHFN_FLOATN (CBRT) \
1986 : : CASE_MATHFN_FLOATN (CEIL) \
1987 : : CASE_MATHFN (CEXPI) \
1988 : : CASE_MATHFN_FLOATN (COPYSIGN) \
1989 : : CASE_MATHFN_FLOATN (COS) \
1990 : : CASE_MATHFN_FLOATN (COSH) \
1991 : : CASE_MATHFN (DREM) \
1992 : : CASE_MATHFN_FLOATN (ERF) \
1993 : : CASE_MATHFN_FLOATN (ERFC) \
1994 : : CASE_MATHFN_FLOATN (EXP) \
1995 : : CASE_MATHFN (EXP10) \
1996 : : CASE_MATHFN_FLOATN (EXP2) \
1997 : : CASE_MATHFN_FLOATN (EXPM1) \
1998 : : CASE_MATHFN_FLOATN (FABS) \
1999 : : CASE_MATHFN_FLOATN (FDIM) \
2000 : : CASE_MATHFN_FLOATN (FLOOR) \
2001 : : CASE_MATHFN_FLOATN (FMA) \
2002 : : CASE_MATHFN_FLOATN (FMAX) \
2003 : : CASE_MATHFN_FLOATN (FMIN) \
2004 : : CASE_MATHFN_FLOATN (FMOD) \
2005 : : CASE_MATHFN_FLOATN (FREXP) \
2006 : : CASE_MATHFN (GAMMA) \
2007 : : CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2008 : : CASE_MATHFN_FLOATN (HUGE_VAL) \
2009 : : CASE_MATHFN_FLOATN (HYPOT) \
2010 : : CASE_MATHFN_FLOATN (ILOGB) \
2011 : : CASE_MATHFN (ICEIL) \
2012 : : CASE_MATHFN (IFLOOR) \
2013 : : CASE_MATHFN_FLOATN (INF) \
2014 : : CASE_MATHFN (IRINT) \
2015 : : CASE_MATHFN (IROUND) \
2016 : : CASE_MATHFN (ISINF) \
2017 : : CASE_MATHFN (J0) \
2018 : : CASE_MATHFN (J1) \
2019 : : CASE_MATHFN (JN) \
2020 : : CASE_MATHFN (LCEIL) \
2021 : : CASE_MATHFN_FLOATN (LDEXP) \
2022 : : CASE_MATHFN (LFLOOR) \
2023 : : CASE_MATHFN_FLOATN (LGAMMA) \
2024 : : CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2025 : : CASE_MATHFN (LLCEIL) \
2026 : : CASE_MATHFN (LLFLOOR) \
2027 : : CASE_MATHFN_FLOATN (LLRINT) \
2028 : : CASE_MATHFN_FLOATN (LLROUND) \
2029 : : CASE_MATHFN_FLOATN (LOG) \
2030 : : CASE_MATHFN_FLOATN (LOG10) \
2031 : : CASE_MATHFN_FLOATN (LOG1P) \
2032 : : CASE_MATHFN_FLOATN (LOG2) \
2033 : : CASE_MATHFN_FLOATN (LOGB) \
2034 : : CASE_MATHFN_FLOATN (LRINT) \
2035 : : CASE_MATHFN_FLOATN (LROUND) \
2036 : : CASE_MATHFN_FLOATN (MODF) \
2037 : : CASE_MATHFN_FLOATN (NAN) \
2038 : : CASE_MATHFN_FLOATN (NANS) \
2039 : : CASE_MATHFN_FLOATN (NEARBYINT) \
2040 : : CASE_MATHFN_FLOATN (NEXTAFTER) \
2041 : : CASE_MATHFN (NEXTTOWARD) \
2042 : : CASE_MATHFN_FLOATN (POW) \
2043 : : CASE_MATHFN (POWI) \
2044 : : CASE_MATHFN (POW10) \
2045 : : CASE_MATHFN_FLOATN (REMAINDER) \
2046 : : CASE_MATHFN_FLOATN (REMQUO) \
2047 : : CASE_MATHFN_FLOATN (RINT) \
2048 : : CASE_MATHFN_FLOATN (ROUND) \
2049 : : CASE_MATHFN_FLOATN (ROUNDEVEN) \
2050 : : CASE_MATHFN (SCALB) \
2051 : : CASE_MATHFN_FLOATN (SCALBLN) \
2052 : : CASE_MATHFN_FLOATN (SCALBN) \
2053 : : CASE_MATHFN (SIGNBIT) \
2054 : : CASE_MATHFN (SIGNIFICAND) \
2055 : : CASE_MATHFN_FLOATN (SIN) \
2056 : : CASE_MATHFN (SINCOS) \
2057 : : CASE_MATHFN_FLOATN (SINH) \
2058 : : CASE_MATHFN_FLOATN (SQRT) \
2059 : : CASE_MATHFN_FLOATN (TAN) \
2060 : : CASE_MATHFN_FLOATN (TANH) \
2061 : : CASE_MATHFN_FLOATN (TGAMMA) \
2062 : : CASE_MATHFN_FLOATN (TRUNC) \
2063 : : CASE_MATHFN (Y0) \
2064 : : CASE_MATHFN (Y1) \
2065 : : CASE_MATHFN (YN)
2066 : :
2067 : 591537 : SEQ_OF_CASE_MATHFN
2068 : :
2069 : : default:
2070 : : return END_BUILTINS;
2071 : : }
2072 : :
2073 : 591600 : mtype = TYPE_MAIN_VARIANT (type);
2074 : 591600 : if (mtype == double_type_node)
2075 : : return fcode;
2076 : 549413 : else if (mtype == float_type_node)
2077 : : return fcodef;
2078 : 498706 : else if (mtype == long_double_type_node)
2079 : : return fcodel;
2080 : 465504 : else if (mtype == float16_type_node)
2081 : : return fcodef16;
2082 : 465456 : else if (mtype == float32_type_node)
2083 : : return fcodef32;
2084 : 465456 : else if (mtype == float64_type_node)
2085 : : return fcodef64;
2086 : 465456 : else if (mtype == float128_type_node)
2087 : : return fcodef128;
2088 : 457665 : else if (mtype == float32x_type_node)
2089 : : return fcodef32x;
2090 : 457665 : else if (mtype == float64x_type_node)
2091 : : return fcodef64x;
2092 : 457665 : else if (mtype == float128x_type_node)
2093 : : return fcodef128x;
2094 : : else
2095 : 457665 : return END_BUILTINS;
2096 : : }
2097 : :
2098 : : #undef CASE_MATHFN
2099 : : #undef CASE_MATHFN_FLOATN
2100 : : #undef CASE_MATHFN_REENT
2101 : :
2102 : : /* Return mathematic function equivalent to FN but operating directly on TYPE,
2103 : : if available. If IMPLICIT_P is true use the implicit builtin declaration,
2104 : : otherwise use the explicit declaration. If we can't do the conversion,
2105 : : return null. */
2106 : :
2107 : : static tree
2108 : 591456 : mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2109 : : {
2110 : 591456 : built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2111 : 591456 : if (fcode2 == END_BUILTINS)
2112 : : return NULL_TREE;
2113 : :
2114 : 130143 : if (implicit_p && !builtin_decl_implicit_p (fcode2))
2115 : : return NULL_TREE;
2116 : :
2117 : 125903 : return builtin_decl_explicit (fcode2);
2118 : : }
2119 : :
2120 : : /* Like mathfn_built_in_1, but always use the implicit array. */
2121 : :
2122 : : tree
2123 : 210 : mathfn_built_in (tree type, combined_fn fn)
2124 : : {
2125 : 210 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2126 : : }
2127 : :
2128 : : /* Like mathfn_built_in_1, but always use the explicit array. */
2129 : :
2130 : : tree
2131 : 0 : mathfn_built_in_explicit (tree type, combined_fn fn)
2132 : : {
2133 : 0 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2134 : : }
2135 : :
2136 : : /* Like mathfn_built_in_1, but take a built_in_function and
2137 : : always use the implicit array. */
2138 : :
2139 : : tree
2140 : 591025 : mathfn_built_in (tree type, enum built_in_function fn)
2141 : : {
2142 : 591025 : return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2143 : : }
2144 : :
2145 : : /* Return the type associated with a built in function, i.e., the one
2146 : : to be passed to mathfn_built_in to get the type-specific
2147 : : function. */
2148 : :
2149 : : tree
2150 : 1237 : mathfn_built_in_type (combined_fn fn)
2151 : : {
2152 : : #define CASE_MATHFN(MATHFN) \
2153 : : case CFN_BUILT_IN_##MATHFN: \
2154 : : return double_type_node; \
2155 : : case CFN_BUILT_IN_##MATHFN##F: \
2156 : : return float_type_node; \
2157 : : case CFN_BUILT_IN_##MATHFN##L: \
2158 : : return long_double_type_node;
2159 : :
2160 : : #define CASE_MATHFN_FLOATN(MATHFN) \
2161 : : CASE_MATHFN(MATHFN) \
2162 : : case CFN_BUILT_IN_##MATHFN##F16: \
2163 : : return float16_type_node; \
2164 : : case CFN_BUILT_IN_##MATHFN##F32: \
2165 : : return float32_type_node; \
2166 : : case CFN_BUILT_IN_##MATHFN##F64: \
2167 : : return float64_type_node; \
2168 : : case CFN_BUILT_IN_##MATHFN##F128: \
2169 : : return float128_type_node; \
2170 : : case CFN_BUILT_IN_##MATHFN##F32X: \
2171 : : return float32x_type_node; \
2172 : : case CFN_BUILT_IN_##MATHFN##F64X: \
2173 : : return float64x_type_node; \
2174 : : case CFN_BUILT_IN_##MATHFN##F128X: \
2175 : : return float128x_type_node;
2176 : :
2177 : : /* Similar to above, but appends _R after any F/L suffix. */
2178 : : #define CASE_MATHFN_REENT(MATHFN) \
2179 : : case CFN_BUILT_IN_##MATHFN##_R: \
2180 : : return double_type_node; \
2181 : : case CFN_BUILT_IN_##MATHFN##F_R: \
2182 : : return float_type_node; \
2183 : : case CFN_BUILT_IN_##MATHFN##L_R: \
2184 : : return long_double_type_node;
2185 : :
2186 : 1237 : switch (fn)
2187 : : {
2188 : 1237 : SEQ_OF_CASE_MATHFN
2189 : :
2190 : : default:
2191 : : return NULL_TREE;
2192 : : }
2193 : :
2194 : : #undef CASE_MATHFN
2195 : : #undef CASE_MATHFN_FLOATN
2196 : : #undef CASE_MATHFN_REENT
2197 : : #undef SEQ_OF_CASE_MATHFN
2198 : : }
2199 : :
2200 : : /* Check whether there is an internal function associated with function FN
2201 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2202 : : IFN_LAST.
2203 : :
2204 : : Note that this function only tests whether the function is defined in
2205 : : internals.def, not whether it is actually available on the target. */
2206 : :
2207 : : static internal_fn
2208 : 14297787 : associated_internal_fn (built_in_function fn, tree return_type)
2209 : : {
2210 : 14297787 : switch (fn)
2211 : : {
2212 : : #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2213 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2214 : : #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2215 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2216 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2217 : : #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2218 : : CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2219 : : #include "internal-fn.def"
2220 : :
2221 : : CASE_FLT_FN (BUILT_IN_POW10):
2222 : : return IFN_EXP10;
2223 : :
2224 : : CASE_FLT_FN (BUILT_IN_DREM):
2225 : : return IFN_REMAINDER;
2226 : :
2227 : 19564 : CASE_FLT_FN (BUILT_IN_SCALBN):
2228 : 19564 : CASE_FLT_FN (BUILT_IN_SCALBLN):
2229 : 19564 : if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2230 : : return IFN_LDEXP;
2231 : : return IFN_LAST;
2232 : 75 : case BUILT_IN_CRC8_DATA8:
2233 : 75 : case BUILT_IN_CRC16_DATA8:
2234 : 75 : case BUILT_IN_CRC16_DATA16:
2235 : 75 : case BUILT_IN_CRC32_DATA8:
2236 : 75 : case BUILT_IN_CRC32_DATA16:
2237 : 75 : case BUILT_IN_CRC32_DATA32:
2238 : 75 : case BUILT_IN_CRC64_DATA8:
2239 : 75 : case BUILT_IN_CRC64_DATA16:
2240 : 75 : case BUILT_IN_CRC64_DATA32:
2241 : 75 : case BUILT_IN_CRC64_DATA64:
2242 : 75 : return IFN_CRC;
2243 : 129 : case BUILT_IN_REV_CRC8_DATA8:
2244 : 129 : case BUILT_IN_REV_CRC16_DATA8:
2245 : 129 : case BUILT_IN_REV_CRC16_DATA16:
2246 : 129 : case BUILT_IN_REV_CRC32_DATA8:
2247 : 129 : case BUILT_IN_REV_CRC32_DATA16:
2248 : 129 : case BUILT_IN_REV_CRC32_DATA32:
2249 : 129 : case BUILT_IN_REV_CRC64_DATA8:
2250 : 129 : case BUILT_IN_REV_CRC64_DATA16:
2251 : 129 : case BUILT_IN_REV_CRC64_DATA32:
2252 : 129 : case BUILT_IN_REV_CRC64_DATA64:
2253 : 129 : return IFN_CRC_REV;
2254 : : default:
2255 : : return IFN_LAST;
2256 : : }
2257 : : }
2258 : :
2259 : : /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2260 : : return its code, otherwise return IFN_LAST. Note that this function
2261 : : only tests whether the function is defined in internals.def, not whether
2262 : : it is actually available on the target. */
2263 : :
2264 : : internal_fn
2265 : 642336 : associated_internal_fn (tree fndecl)
2266 : : {
2267 : 642336 : gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2268 : 642336 : return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2269 : 642336 : TREE_TYPE (TREE_TYPE (fndecl)));
2270 : : }
2271 : :
2272 : : /* Check whether there is an internal function associated with function CFN
2273 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2274 : : IFN_LAST.
2275 : :
2276 : : Note that this function only tests whether the function is defined in
2277 : : internals.def, not whether it is actually available on the target. */
2278 : :
2279 : : internal_fn
2280 : 32405415 : associated_internal_fn (combined_fn cfn, tree return_type)
2281 : : {
2282 : 32405415 : if (internal_fn_p (cfn))
2283 : 18749964 : return as_internal_fn (cfn);
2284 : 13655451 : return associated_internal_fn (as_builtin_fn (cfn), return_type);
2285 : : }
2286 : :
2287 : : /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2288 : : on the current target by a call to an internal function, return the
2289 : : code of that internal function, otherwise return IFN_LAST. The caller
2290 : : is responsible for ensuring that any side-effects of the built-in
2291 : : call are dealt with correctly. E.g. if CALL sets errno, the caller
2292 : : must decide that the errno result isn't needed or make it available
2293 : : in some other way. */
2294 : :
2295 : : internal_fn
2296 : 861550 : replacement_internal_fn (gcall *call)
2297 : : {
2298 : 861550 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2299 : : {
2300 : 639963 : internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2301 : 639963 : if (ifn != IFN_LAST)
2302 : : {
2303 : 62156 : tree_pair types = direct_internal_fn_types (ifn, call);
2304 : 62156 : optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2305 : 62156 : if (direct_internal_fn_supported_p (ifn, types, opt_type))
2306 : 40751 : return ifn;
2307 : : }
2308 : : }
2309 : : return IFN_LAST;
2310 : : }
2311 : :
2312 : : /* Expand a call to the builtin trinary math functions (fma).
2313 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2314 : : function in-line. EXP is the expression that is a call to the builtin
2315 : : function; if convenient, the result should be placed in TARGET.
2316 : : SUBTARGET may be used as the target for computing one of EXP's
2317 : : operands. */
2318 : :
2319 : : static rtx
2320 : 364 : expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2321 : : {
2322 : 364 : optab builtin_optab;
2323 : 364 : rtx op0, op1, op2, result;
2324 : 364 : rtx_insn *insns;
2325 : 364 : tree fndecl = get_callee_fndecl (exp);
2326 : 364 : tree arg0, arg1, arg2;
2327 : 364 : machine_mode mode;
2328 : :
2329 : 364 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2330 : : return NULL_RTX;
2331 : :
2332 : 364 : arg0 = CALL_EXPR_ARG (exp, 0);
2333 : 364 : arg1 = CALL_EXPR_ARG (exp, 1);
2334 : 364 : arg2 = CALL_EXPR_ARG (exp, 2);
2335 : :
2336 : 364 : switch (DECL_FUNCTION_CODE (fndecl))
2337 : : {
2338 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
2339 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2340 : 364 : builtin_optab = fma_optab; break;
2341 : 0 : default:
2342 : 0 : gcc_unreachable ();
2343 : : }
2344 : :
2345 : : /* Make a suitable register to place result in. */
2346 : 364 : mode = TYPE_MODE (TREE_TYPE (exp));
2347 : :
2348 : : /* Before working hard, check whether the instruction is available. */
2349 : 364 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2350 : : return NULL_RTX;
2351 : :
2352 : 0 : result = gen_reg_rtx (mode);
2353 : :
2354 : : /* Always stabilize the argument list. */
2355 : 0 : CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2356 : 0 : CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2357 : 0 : CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2358 : :
2359 : 0 : op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2360 : 0 : op1 = expand_normal (arg1);
2361 : 0 : op2 = expand_normal (arg2);
2362 : :
2363 : 0 : start_sequence ();
2364 : :
2365 : : /* Compute into RESULT.
2366 : : Set RESULT to wherever the result comes back. */
2367 : 0 : result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2368 : : result, 0);
2369 : :
2370 : : /* If we were unable to expand via the builtin, stop the sequence
2371 : : (without outputting the insns) and call to the library function
2372 : : with the stabilized argument list. */
2373 : 0 : if (result == 0)
2374 : : {
2375 : 0 : end_sequence ();
2376 : 0 : return expand_call (exp, target, target == const0_rtx);
2377 : : }
2378 : :
2379 : : /* Output the entire sequence. */
2380 : 0 : insns = end_sequence ();
2381 : 0 : emit_insn (insns);
2382 : :
2383 : 0 : return result;
2384 : : }
2385 : :
2386 : : /* Expand a call to the builtin sin and cos math functions.
2387 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2388 : : function in-line. EXP is the expression that is a call to the builtin
2389 : : function; if convenient, the result should be placed in TARGET.
2390 : : SUBTARGET may be used as the target for computing one of EXP's
2391 : : operands. */
2392 : :
2393 : : static rtx
2394 : 47 : expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2395 : : {
2396 : 47 : optab builtin_optab;
2397 : 47 : rtx op0;
2398 : 47 : rtx_insn *insns;
2399 : 47 : tree fndecl = get_callee_fndecl (exp);
2400 : 47 : machine_mode mode;
2401 : 47 : tree arg;
2402 : :
2403 : 47 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2404 : : return NULL_RTX;
2405 : :
2406 : 39 : arg = CALL_EXPR_ARG (exp, 0);
2407 : :
2408 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2409 : : {
2410 : 39 : CASE_FLT_FN (BUILT_IN_SIN):
2411 : 39 : CASE_FLT_FN (BUILT_IN_COS):
2412 : 39 : builtin_optab = sincos_optab; break;
2413 : 0 : default:
2414 : 0 : gcc_unreachable ();
2415 : : }
2416 : :
2417 : : /* Make a suitable register to place result in. */
2418 : 39 : mode = TYPE_MODE (TREE_TYPE (exp));
2419 : :
2420 : : /* Check if sincos insn is available, otherwise fallback
2421 : : to sin or cos insn. */
2422 : 39 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2423 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2424 : : {
2425 : : CASE_FLT_FN (BUILT_IN_SIN):
2426 : : builtin_optab = sin_optab; break;
2427 : 14 : CASE_FLT_FN (BUILT_IN_COS):
2428 : 14 : builtin_optab = cos_optab; break;
2429 : 0 : default:
2430 : 0 : gcc_unreachable ();
2431 : : }
2432 : :
2433 : : /* Before working hard, check whether the instruction is available. */
2434 : 39 : if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2435 : : {
2436 : 0 : rtx result = gen_reg_rtx (mode);
2437 : :
2438 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2439 : : need to expand the argument again. This way, we will not perform
2440 : : side-effects more the once. */
2441 : 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2442 : :
2443 : 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2444 : :
2445 : 0 : start_sequence ();
2446 : :
2447 : : /* Compute into RESULT.
2448 : : Set RESULT to wherever the result comes back. */
2449 : 0 : if (builtin_optab == sincos_optab)
2450 : : {
2451 : 0 : int ok;
2452 : :
2453 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
2454 : : {
2455 : 0 : CASE_FLT_FN (BUILT_IN_SIN):
2456 : 0 : ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2457 : 0 : break;
2458 : 0 : CASE_FLT_FN (BUILT_IN_COS):
2459 : 0 : ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2460 : 0 : break;
2461 : 0 : default:
2462 : 0 : gcc_unreachable ();
2463 : : }
2464 : 0 : gcc_assert (ok);
2465 : : }
2466 : : else
2467 : 0 : result = expand_unop (mode, builtin_optab, op0, result, 0);
2468 : :
2469 : 0 : if (result != 0)
2470 : : {
2471 : : /* Output the entire sequence. */
2472 : 0 : insns = end_sequence ();
2473 : 0 : emit_insn (insns);
2474 : 0 : return result;
2475 : : }
2476 : :
2477 : : /* If we were unable to expand via the builtin, stop the sequence
2478 : : (without outputting the insns) and call to the library function
2479 : : with the stabilized argument list. */
2480 : 0 : end_sequence ();
2481 : : }
2482 : :
2483 : 39 : return expand_call (exp, target, target == const0_rtx);
2484 : : }
2485 : :
2486 : : /* Given an interclass math builtin decl FNDECL and it's argument ARG
2487 : : return an RTL instruction code that implements the functionality.
2488 : : If that isn't possible or available return CODE_FOR_nothing. */
2489 : :
2490 : : static enum insn_code
2491 : 1076828 : interclass_mathfn_icode (tree arg, tree fndecl)
2492 : : {
2493 : 1076828 : bool errno_set = false;
2494 : 1076828 : optab builtin_optab = unknown_optab;
2495 : 1076828 : machine_mode mode;
2496 : :
2497 : 1076828 : switch (DECL_FUNCTION_CODE (fndecl))
2498 : : {
2499 : 4 : CASE_FLT_FN (BUILT_IN_ILOGB):
2500 : 4 : errno_set = true; builtin_optab = ilogb_optab; break;
2501 : 259821 : CASE_FLT_FN (BUILT_IN_ISINF):
2502 : 259821 : builtin_optab = isinf_optab; break;
2503 : 557252 : case BUILT_IN_ISFINITE:
2504 : 557252 : builtin_optab = isfinite_optab;
2505 : 557252 : break;
2506 : 258049 : case BUILT_IN_ISNORMAL:
2507 : 258049 : builtin_optab = isnormal_optab;
2508 : 258049 : break;
2509 : : CASE_FLT_FN (BUILT_IN_FINITE):
2510 : : case BUILT_IN_FINITED32:
2511 : : case BUILT_IN_FINITED64:
2512 : : case BUILT_IN_FINITED128:
2513 : : case BUILT_IN_ISINFD32:
2514 : : case BUILT_IN_ISINFD64:
2515 : : case BUILT_IN_ISINFD128:
2516 : : /* These builtins have no optabs (yet). */
2517 : : break;
2518 : 0 : default:
2519 : 0 : gcc_unreachable ();
2520 : : }
2521 : :
2522 : : /* There's no easy way to detect the case we need to set EDOM. */
2523 : 1076828 : if (flag_errno_math && errno_set)
2524 : : return CODE_FOR_nothing;
2525 : :
2526 : : /* Optab mode depends on the mode of the input argument. */
2527 : 1076828 : mode = TYPE_MODE (TREE_TYPE (arg));
2528 : :
2529 : 1076828 : if (builtin_optab)
2530 : 1075126 : return optab_handler (builtin_optab, mode);
2531 : : return CODE_FOR_nothing;
2532 : : }
2533 : :
2534 : : /* Expand a call to one of the builtin math functions that operate on
2535 : : floating point argument and output an integer result (ilogb, isinf,
2536 : : isnan, etc).
2537 : : Return 0 if a normal call should be emitted rather than expanding the
2538 : : function in-line. EXP is the expression that is a call to the builtin
2539 : : function; if convenient, the result should be placed in TARGET. */
2540 : :
2541 : : static rtx
2542 : 4 : expand_builtin_interclass_mathfn (tree exp, rtx target)
2543 : : {
2544 : 4 : enum insn_code icode = CODE_FOR_nothing;
2545 : 4 : rtx op0;
2546 : 4 : tree fndecl = get_callee_fndecl (exp);
2547 : 4 : machine_mode mode;
2548 : 4 : tree arg;
2549 : :
2550 : 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2551 : : return NULL_RTX;
2552 : :
2553 : 4 : arg = CALL_EXPR_ARG (exp, 0);
2554 : 4 : icode = interclass_mathfn_icode (arg, fndecl);
2555 : 4 : mode = TYPE_MODE (TREE_TYPE (arg));
2556 : :
2557 : 4 : if (icode != CODE_FOR_nothing)
2558 : : {
2559 : 2 : class expand_operand ops[1];
2560 : 2 : rtx_insn *last = get_last_insn ();
2561 : 2 : tree orig_arg = arg;
2562 : :
2563 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2564 : : need to expand the argument again. This way, we will not perform
2565 : : side-effects more the once. */
2566 : 2 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2567 : :
2568 : 2 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2569 : :
2570 : 2 : if (mode != GET_MODE (op0))
2571 : 0 : op0 = convert_to_mode (mode, op0, 0);
2572 : :
2573 : 2 : create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2574 : 2 : if (maybe_legitimize_operands (icode, 0, 1, ops)
2575 : 2 : && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2576 : 1 : return ops[0].value;
2577 : :
2578 : 1 : delete_insns_since (last);
2579 : 1 : CALL_EXPR_ARG (exp, 0) = orig_arg;
2580 : : }
2581 : :
2582 : : return NULL_RTX;
2583 : : }
2584 : :
2585 : : /* Expand a call to the builtin sincos math function.
2586 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2587 : : function in-line. EXP is the expression that is a call to the builtin
2588 : : function. */
2589 : :
2590 : : static rtx
2591 : 3 : expand_builtin_sincos (tree exp)
2592 : : {
2593 : 3 : rtx op0, op1, op2, target1, target2;
2594 : 3 : machine_mode mode;
2595 : 3 : tree arg, sinp, cosp;
2596 : 3 : int result;
2597 : 3 : location_t loc = EXPR_LOCATION (exp);
2598 : 3 : tree alias_type, alias_off;
2599 : :
2600 : 3 : if (!validate_arglist (exp, REAL_TYPE,
2601 : : POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2602 : : return NULL_RTX;
2603 : :
2604 : 3 : arg = CALL_EXPR_ARG (exp, 0);
2605 : 3 : sinp = CALL_EXPR_ARG (exp, 1);
2606 : 3 : cosp = CALL_EXPR_ARG (exp, 2);
2607 : :
2608 : : /* Make a suitable register to place result in. */
2609 : 3 : mode = TYPE_MODE (TREE_TYPE (arg));
2610 : :
2611 : : /* Check if sincos insn is available, otherwise emit the call. */
2612 : 3 : if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2613 : : return NULL_RTX;
2614 : :
2615 : 0 : target1 = gen_reg_rtx (mode);
2616 : 0 : target2 = gen_reg_rtx (mode);
2617 : :
2618 : 0 : op0 = expand_normal (arg);
2619 : 0 : alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2620 : 0 : alias_off = build_int_cst (alias_type, 0);
2621 : 0 : op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2622 : : sinp, alias_off));
2623 : 0 : op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2624 : : cosp, alias_off));
2625 : :
2626 : : /* Compute into target1 and target2.
2627 : : Set TARGET to wherever the result comes back. */
2628 : 0 : result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2629 : 0 : gcc_assert (result);
2630 : :
2631 : : /* Move target1 and target2 to the memory locations indicated
2632 : : by op1 and op2. */
2633 : 0 : emit_move_insn (op1, target1);
2634 : 0 : emit_move_insn (op2, target2);
2635 : :
2636 : 0 : return const0_rtx;
2637 : : }
2638 : :
2639 : : /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2640 : : result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2641 : : static rtx
2642 : 60 : expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2643 : : {
2644 : 60 : if (!validate_arglist (exp, VOID_TYPE))
2645 : : return NULL_RTX;
2646 : :
2647 : 60 : insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2648 : 60 : if (icode == CODE_FOR_nothing)
2649 : : return NULL_RTX;
2650 : :
2651 : 0 : if (target == 0
2652 : 0 : || GET_MODE (target) != target_mode
2653 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2654 : 0 : target = gen_reg_rtx (target_mode);
2655 : :
2656 : 0 : rtx pat = GEN_FCN (icode) (target);
2657 : 0 : if (!pat)
2658 : : return NULL_RTX;
2659 : 0 : emit_insn (pat);
2660 : :
2661 : 0 : return target;
2662 : : }
2663 : :
2664 : : /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2665 : : fenv.h), returning the result and setting it in TARGET. Otherwise return
2666 : : NULL_RTX on failure. */
2667 : : static rtx
2668 : 1894 : expand_builtin_feclear_feraise_except (tree exp, rtx target,
2669 : : machine_mode target_mode, optab op_optab)
2670 : : {
2671 : 1894 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2672 : : return NULL_RTX;
2673 : 1894 : rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2674 : :
2675 : 1894 : insn_code icode = direct_optab_handler (op_optab, SImode);
2676 : 1894 : if (icode == CODE_FOR_nothing)
2677 : : return NULL_RTX;
2678 : :
2679 : 0 : if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2680 : : return NULL_RTX;
2681 : :
2682 : 0 : if (target == 0
2683 : 0 : || GET_MODE (target) != target_mode
2684 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2685 : 0 : target = gen_reg_rtx (target_mode);
2686 : :
2687 : 0 : rtx pat = GEN_FCN (icode) (target, op0);
2688 : 0 : if (!pat)
2689 : : return NULL_RTX;
2690 : 0 : emit_insn (pat);
2691 : :
2692 : 0 : return target;
2693 : : }
2694 : :
2695 : : /* Expand a call to the internal cexpi builtin to the sincos math function.
2696 : : EXP is the expression that is a call to the builtin function; if convenient,
2697 : : the result should be placed in TARGET. */
2698 : :
2699 : : static rtx
2700 : 157 : expand_builtin_cexpi (tree exp, rtx target)
2701 : : {
2702 : 157 : tree fndecl = get_callee_fndecl (exp);
2703 : 157 : tree arg, type;
2704 : 157 : machine_mode mode;
2705 : 157 : rtx op0, op1, op2;
2706 : 157 : location_t loc = EXPR_LOCATION (exp);
2707 : :
2708 : 157 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2709 : : return NULL_RTX;
2710 : :
2711 : 157 : arg = CALL_EXPR_ARG (exp, 0);
2712 : 157 : type = TREE_TYPE (arg);
2713 : 157 : mode = TYPE_MODE (TREE_TYPE (arg));
2714 : :
2715 : : /* Try expanding via a sincos optab, fall back to emitting a libcall
2716 : : to sincos or cexp. We are sure we have sincos or cexp because cexpi
2717 : : is only generated from sincos, cexp or if we have either of them. */
2718 : 157 : if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2719 : : {
2720 : 6 : op1 = gen_reg_rtx (mode);
2721 : 6 : op2 = gen_reg_rtx (mode);
2722 : :
2723 : 6 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2724 : :
2725 : : /* Compute into op1 and op2. */
2726 : 6 : expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2727 : : }
2728 : 151 : else if (targetm.libc_has_function (function_sincos, type))
2729 : : {
2730 : 151 : tree call, fn = NULL_TREE;
2731 : 151 : tree top1, top2;
2732 : 151 : rtx op1a, op2a;
2733 : :
2734 : 151 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2735 : 51 : fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2736 : 100 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2737 : 81 : fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2738 : 19 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2739 : 19 : fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2740 : : else
2741 : 0 : gcc_unreachable ();
2742 : :
2743 : 151 : op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2744 : 151 : op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2745 : 151 : op1a = copy_addr_to_reg (XEXP (op1, 0));
2746 : 151 : op2a = copy_addr_to_reg (XEXP (op2, 0));
2747 : 151 : top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2748 : 151 : top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2749 : :
2750 : : /* Make sure not to fold the sincos call again. */
2751 : 151 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2752 : 151 : expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2753 : : call, 3, arg, top1, top2));
2754 : : }
2755 : : else
2756 : : {
2757 : 0 : tree call, fn = NULL_TREE, narg;
2758 : 0 : tree ctype = build_complex_type (type);
2759 : :
2760 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2761 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2762 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2763 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXP);
2764 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2765 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2766 : : else
2767 : 0 : gcc_unreachable ();
2768 : :
2769 : : /* If we don't have a decl for cexp create one. This is the
2770 : : friendliest fallback if the user calls __builtin_cexpi
2771 : : without full target C99 function support. */
2772 : 0 : if (fn == NULL_TREE)
2773 : : {
2774 : 0 : tree fntype;
2775 : 0 : const char *name = NULL;
2776 : :
2777 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2778 : : name = "cexpf";
2779 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2780 : : name = "cexp";
2781 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2782 : 0 : name = "cexpl";
2783 : :
2784 : 0 : fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2785 : 0 : fn = build_fn_decl (name, fntype);
2786 : : }
2787 : :
2788 : 0 : narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2789 : : build_real (type, dconst0), arg);
2790 : :
2791 : : /* Make sure not to fold the cexp call again. */
2792 : 0 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2793 : 0 : return expand_expr (build_call_nary (ctype, call, 1, narg),
2794 : 0 : target, VOIDmode, EXPAND_NORMAL);
2795 : : }
2796 : :
2797 : : /* Now build the proper return type. */
2798 : 314 : return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2799 : 157 : make_tree (TREE_TYPE (arg), op2),
2800 : 157 : make_tree (TREE_TYPE (arg), op1)),
2801 : 157 : target, VOIDmode, EXPAND_NORMAL);
2802 : : }
2803 : :
2804 : : /* Conveniently construct a function call expression. FNDECL names the
2805 : : function to be called, N is the number of arguments, and the "..."
2806 : : parameters are the argument expressions. Unlike build_call_exr
2807 : : this doesn't fold the call, hence it will always return a CALL_EXPR. */
2808 : :
2809 : : static tree
2810 : 136272 : build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2811 : : {
2812 : 136272 : va_list ap;
2813 : 136272 : tree fntype = TREE_TYPE (fndecl);
2814 : 136272 : tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2815 : :
2816 : 136272 : va_start (ap, n);
2817 : 136272 : fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2818 : 136272 : va_end (ap);
2819 : 136272 : SET_EXPR_LOCATION (fn, loc);
2820 : 136272 : return fn;
2821 : : }
2822 : :
2823 : : /* Expand the __builtin_issignaling builtin. This needs to handle
2824 : : all floating point formats that do support NaNs (for those that
2825 : : don't it just sets target to 0). */
2826 : :
2827 : : static rtx
2828 : 823 : expand_builtin_issignaling (tree exp, rtx target)
2829 : : {
2830 : 823 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2831 : : return NULL_RTX;
2832 : :
2833 : 823 : tree arg = CALL_EXPR_ARG (exp, 0);
2834 : 823 : scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2835 : 823 : const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2836 : :
2837 : : /* Expand the argument yielding a RTX expression. */
2838 : 823 : rtx temp = expand_normal (arg);
2839 : :
2840 : : /* If mode doesn't support NaN, always return 0.
2841 : : Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2842 : : __builtin_issignaling working without -fsignaling-nans. Especially
2843 : : when -fno-signaling-nans is the default.
2844 : : On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2845 : : -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2846 : : fold to 0 or non-NaN/Inf classification. */
2847 : 823 : if (!HONOR_NANS (fmode))
2848 : : {
2849 : 0 : emit_move_insn (target, const0_rtx);
2850 : 0 : return target;
2851 : : }
2852 : :
2853 : : /* Check if the back end provides an insn that handles issignaling for the
2854 : : argument's mode. */
2855 : 823 : enum insn_code icode = optab_handler (issignaling_optab, fmode);
2856 : 823 : if (icode != CODE_FOR_nothing)
2857 : : {
2858 : 155 : rtx_insn *last = get_last_insn ();
2859 : 155 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2860 : 155 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2861 : : return this_target;
2862 : 0 : delete_insns_since (last);
2863 : : }
2864 : :
2865 : 668 : if (DECIMAL_FLOAT_MODE_P (fmode))
2866 : : {
2867 : 63 : scalar_int_mode imode;
2868 : 63 : rtx hi;
2869 : 63 : switch (fmt->ieee_bits)
2870 : : {
2871 : 42 : case 32:
2872 : 42 : case 64:
2873 : 42 : imode = int_mode_for_mode (fmode).require ();
2874 : 42 : temp = gen_lowpart (imode, temp);
2875 : 42 : break;
2876 : 21 : case 128:
2877 : 21 : imode = int_mode_for_size (64, 1).require ();
2878 : 21 : hi = NULL_RTX;
2879 : : /* For decimal128, TImode support isn't always there and even when
2880 : : it is, working on the DImode high part is usually better. */
2881 : 21 : if (!MEM_P (temp))
2882 : : {
2883 : 6 : if (rtx t = force_highpart_subreg (imode, temp, fmode))
2884 : : hi = t;
2885 : : else
2886 : : {
2887 : 0 : scalar_int_mode imode2;
2888 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2889 : : {
2890 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2891 : 0 : if (rtx t = force_highpart_subreg (imode, temp2, imode2))
2892 : : hi = t;
2893 : : }
2894 : : }
2895 : 0 : if (!hi)
2896 : : {
2897 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2898 : 0 : emit_move_insn (mem, temp);
2899 : 0 : temp = mem;
2900 : : }
2901 : : }
2902 : 0 : if (!hi)
2903 : : {
2904 : 15 : poly_int64 offset
2905 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
2906 : 15 : hi = adjust_address (temp, imode, offset);
2907 : : }
2908 : : temp = hi;
2909 : : break;
2910 : 0 : default:
2911 : 0 : gcc_unreachable ();
2912 : : }
2913 : : /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2914 : : have 6 bits below it all set. */
2915 : 63 : rtx val
2916 : 63 : = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2917 : 63 : temp = expand_binop (imode, and_optab, temp, val,
2918 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2919 : 63 : temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2920 : 63 : return temp;
2921 : : }
2922 : :
2923 : : /* Only PDP11 has these defined differently but doesn't support NaNs. */
2924 : 605 : gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2925 : 605 : gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2926 : 4235 : gcc_assert (MODE_COMPOSITE_P (fmode)
2927 : : || (fmt->pnan == fmt->p
2928 : : && fmt->signbit_ro == fmt->signbit_rw));
2929 : :
2930 : 605 : switch (fmt->p)
2931 : : {
2932 : 0 : case 106: /* IBM double double */
2933 : : /* For IBM double double, recurse on the most significant double. */
2934 : 0 : gcc_assert (MODE_COMPOSITE_P (fmode));
2935 : 0 : temp = convert_modes (DFmode, fmode, temp, 0);
2936 : 0 : fmode = DFmode;
2937 : 0 : fmt = REAL_MODE_FORMAT (DFmode);
2938 : : /* FALLTHRU */
2939 : 505 : case 8: /* bfloat */
2940 : 505 : case 11: /* IEEE half */
2941 : 505 : case 24: /* IEEE single */
2942 : 505 : case 53: /* IEEE double or Intel extended with rounding to double */
2943 : 505 : if (fmt->p == 53 && fmt->signbit_ro == 79)
2944 : 0 : goto extended;
2945 : 505 : {
2946 : 505 : scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2947 : 505 : temp = gen_lowpart (imode, temp);
2948 : 505 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2949 : : & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2950 : 505 : if (fmt->qnan_msb_set)
2951 : : {
2952 : 505 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2953 : 505 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2954 : : /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2955 : : ((temp ^ bit) & mask) > val. */
2956 : 505 : temp = expand_binop (imode, xor_optab, temp, bit,
2957 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2958 : 505 : temp = expand_binop (imode, and_optab, temp, mask,
2959 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2960 : 505 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
2961 : : 1, 1);
2962 : : }
2963 : : else
2964 : : {
2965 : : /* For MIPS/PA IEEE single/double, expand to:
2966 : : (temp & val) == val. */
2967 : 0 : temp = expand_binop (imode, and_optab, temp, val,
2968 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2969 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
2970 : : 1, 1);
2971 : : }
2972 : : }
2973 : : break;
2974 : 100 : case 113: /* IEEE quad */
2975 : 100 : {
2976 : 100 : rtx hi = NULL_RTX, lo = NULL_RTX;
2977 : 100 : scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2978 : : /* For IEEE quad, TImode support isn't always there and even when
2979 : : it is, working on DImode parts is usually better. */
2980 : 100 : if (!MEM_P (temp))
2981 : : {
2982 : 85 : hi = force_highpart_subreg (imode, temp, fmode);
2983 : 85 : lo = force_lowpart_subreg (imode, temp, fmode);
2984 : 85 : if (!hi || !lo)
2985 : : {
2986 : 0 : scalar_int_mode imode2;
2987 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2988 : : {
2989 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2990 : 0 : hi = force_highpart_subreg (imode, temp2, imode2);
2991 : 0 : lo = force_lowpart_subreg (imode, temp2, imode2);
2992 : : }
2993 : : }
2994 : 85 : if (!hi || !lo)
2995 : : {
2996 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2997 : 0 : emit_move_insn (mem, temp);
2998 : 0 : temp = mem;
2999 : : }
3000 : : }
3001 : 100 : if (!hi || !lo)
3002 : : {
3003 : 15 : poly_int64 offset
3004 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
3005 : 15 : hi = adjust_address (temp, imode, offset);
3006 : 15 : offset = subreg_lowpart_offset (imode, GET_MODE (temp));
3007 : 15 : lo = adjust_address (temp, imode, offset);
3008 : : }
3009 : 100 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
3010 : : & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
3011 : 100 : if (fmt->qnan_msb_set)
3012 : : {
3013 : 100 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
3014 : : - 64)));
3015 : 100 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
3016 : : /* For non-MIPS/PA IEEE quad, expand to:
3017 : : (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
3018 : 100 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3019 : 100 : lo = expand_binop (imode, ior_optab, lo, nlo,
3020 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3021 : 100 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
3022 : 100 : temp = expand_binop (imode, xor_optab, hi, bit,
3023 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3024 : 100 : temp = expand_binop (imode, ior_optab, temp, lo,
3025 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3026 : 100 : temp = expand_binop (imode, and_optab, temp, mask,
3027 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3028 : 100 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
3029 : : 1, 1);
3030 : : }
3031 : : else
3032 : : {
3033 : : /* For MIPS/PA IEEE quad, expand to:
3034 : : (hi & val) == val. */
3035 : 0 : temp = expand_binop (imode, and_optab, hi, val,
3036 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3037 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
3038 : : 1, 1);
3039 : : }
3040 : : }
3041 : : break;
3042 : 0 : case 64: /* Intel or Motorola extended */
3043 : 0 : extended:
3044 : 0 : {
3045 : 0 : rtx ex, hi, lo;
3046 : 0 : scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3047 : 0 : scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3048 : 0 : if (!MEM_P (temp))
3049 : : {
3050 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3051 : 0 : emit_move_insn (mem, temp);
3052 : 0 : temp = mem;
3053 : : }
3054 : 0 : if (fmt->signbit_ro == 95)
3055 : : {
3056 : : /* Motorola, always big endian, with 16-bit gap in between
3057 : : 16-bit sign+exponent and 64-bit mantissa. */
3058 : 0 : ex = adjust_address (temp, iemode, 0);
3059 : 0 : hi = adjust_address (temp, imode, 4);
3060 : 0 : lo = adjust_address (temp, imode, 8);
3061 : : }
3062 : 0 : else if (!WORDS_BIG_ENDIAN)
3063 : : {
3064 : : /* Intel little endian, 64-bit mantissa followed by 16-bit
3065 : : sign+exponent and then either 16 or 48 bits of gap. */
3066 : 0 : ex = adjust_address (temp, iemode, 8);
3067 : 0 : hi = adjust_address (temp, imode, 4);
3068 : 0 : lo = adjust_address (temp, imode, 0);
3069 : : }
3070 : : else
3071 : : {
3072 : : /* Big endian Itanium. */
3073 : : ex = adjust_address (temp, iemode, 0);
3074 : : hi = adjust_address (temp, imode, 2);
3075 : : lo = adjust_address (temp, imode, 6);
3076 : : }
3077 : 0 : rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3078 : 0 : gcc_assert (fmt->qnan_msb_set);
3079 : 0 : rtx mask = GEN_INT (0x7fff);
3080 : 0 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3081 : : /* For Intel/Motorola extended format, expand to:
3082 : : (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3083 : 0 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3084 : 0 : lo = expand_binop (imode, ior_optab, lo, nlo,
3085 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3086 : 0 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3087 : 0 : temp = expand_binop (imode, xor_optab, hi, bit,
3088 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3089 : 0 : temp = expand_binop (imode, ior_optab, temp, lo,
3090 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3091 : 0 : temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3092 : 0 : ex = expand_binop (iemode, and_optab, ex, mask,
3093 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3094 : 0 : ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3095 : : ex, mask, iemode, 1, 1);
3096 : 0 : temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3097 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3098 : : }
3099 : 0 : break;
3100 : 0 : default:
3101 : 0 : gcc_unreachable ();
3102 : : }
3103 : :
3104 : : return temp;
3105 : : }
3106 : :
3107 : : /* Expand a call to one of the builtin rounding functions gcc defines
3108 : : as an extension (lfloor and lceil). As these are gcc extensions we
3109 : : do not need to worry about setting errno to EDOM.
3110 : : If expanding via optab fails, lower expression to (int)(floor(x)).
3111 : : EXP is the expression that is a call to the builtin function;
3112 : : if convenient, the result should be placed in TARGET. */
3113 : :
3114 : : static rtx
3115 : 231 : expand_builtin_int_roundingfn (tree exp, rtx target)
3116 : : {
3117 : 231 : convert_optab builtin_optab;
3118 : 231 : rtx op0, tmp;
3119 : 231 : rtx_insn *insns;
3120 : 231 : tree fndecl = get_callee_fndecl (exp);
3121 : 231 : enum built_in_function fallback_fn;
3122 : 231 : tree fallback_fndecl;
3123 : 231 : machine_mode mode;
3124 : 231 : tree arg;
3125 : :
3126 : 231 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3127 : : return NULL_RTX;
3128 : :
3129 : 231 : arg = CALL_EXPR_ARG (exp, 0);
3130 : :
3131 : 231 : switch (DECL_FUNCTION_CODE (fndecl))
3132 : : {
3133 : : CASE_FLT_FN (BUILT_IN_ICEIL):
3134 : : CASE_FLT_FN (BUILT_IN_LCEIL):
3135 : : CASE_FLT_FN (BUILT_IN_LLCEIL):
3136 : : builtin_optab = lceil_optab;
3137 : : fallback_fn = BUILT_IN_CEIL;
3138 : : break;
3139 : :
3140 : 116 : CASE_FLT_FN (BUILT_IN_IFLOOR):
3141 : 116 : CASE_FLT_FN (BUILT_IN_LFLOOR):
3142 : 116 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
3143 : 116 : builtin_optab = lfloor_optab;
3144 : 116 : fallback_fn = BUILT_IN_FLOOR;
3145 : 116 : break;
3146 : :
3147 : 0 : default:
3148 : 0 : gcc_unreachable ();
3149 : : }
3150 : :
3151 : : /* Make a suitable register to place result in. */
3152 : 231 : mode = TYPE_MODE (TREE_TYPE (exp));
3153 : :
3154 : 231 : target = gen_reg_rtx (mode);
3155 : :
3156 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3157 : : need to expand the argument again. This way, we will not perform
3158 : : side-effects more the once. */
3159 : 231 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3160 : :
3161 : 231 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3162 : :
3163 : 231 : start_sequence ();
3164 : :
3165 : : /* Compute into TARGET. */
3166 : 231 : if (expand_sfix_optab (target, op0, builtin_optab))
3167 : : {
3168 : : /* Output the entire sequence. */
3169 : 40 : insns = end_sequence ();
3170 : 40 : emit_insn (insns);
3171 : 40 : return target;
3172 : : }
3173 : :
3174 : : /* If we were unable to expand via the builtin, stop the sequence
3175 : : (without outputting the insns). */
3176 : 191 : end_sequence ();
3177 : :
3178 : : /* Fall back to floating point rounding optab. */
3179 : 191 : fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3180 : :
3181 : : /* For non-C99 targets we may end up without a fallback fndecl here
3182 : : if the user called __builtin_lfloor directly. In this case emit
3183 : : a call to the floor/ceil variants nevertheless. This should result
3184 : : in the best user experience for not full C99 targets. */
3185 : 191 : if (fallback_fndecl == NULL_TREE)
3186 : : {
3187 : 0 : tree fntype;
3188 : 0 : const char *name = NULL;
3189 : :
3190 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
3191 : : {
3192 : : case BUILT_IN_ICEIL:
3193 : : case BUILT_IN_LCEIL:
3194 : : case BUILT_IN_LLCEIL:
3195 : : name = "ceil";
3196 : : break;
3197 : 0 : case BUILT_IN_ICEILF:
3198 : 0 : case BUILT_IN_LCEILF:
3199 : 0 : case BUILT_IN_LLCEILF:
3200 : 0 : name = "ceilf";
3201 : 0 : break;
3202 : 0 : case BUILT_IN_ICEILL:
3203 : 0 : case BUILT_IN_LCEILL:
3204 : 0 : case BUILT_IN_LLCEILL:
3205 : 0 : name = "ceill";
3206 : 0 : break;
3207 : 0 : case BUILT_IN_IFLOOR:
3208 : 0 : case BUILT_IN_LFLOOR:
3209 : 0 : case BUILT_IN_LLFLOOR:
3210 : 0 : name = "floor";
3211 : 0 : break;
3212 : 0 : case BUILT_IN_IFLOORF:
3213 : 0 : case BUILT_IN_LFLOORF:
3214 : 0 : case BUILT_IN_LLFLOORF:
3215 : 0 : name = "floorf";
3216 : 0 : break;
3217 : 0 : case BUILT_IN_IFLOORL:
3218 : 0 : case BUILT_IN_LFLOORL:
3219 : 0 : case BUILT_IN_LLFLOORL:
3220 : 0 : name = "floorl";
3221 : 0 : break;
3222 : 0 : default:
3223 : 0 : gcc_unreachable ();
3224 : : }
3225 : :
3226 : 0 : fntype = build_function_type_list (TREE_TYPE (arg),
3227 : 0 : TREE_TYPE (arg), NULL_TREE);
3228 : 0 : fallback_fndecl = build_fn_decl (name, fntype);
3229 : : }
3230 : :
3231 : 191 : exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3232 : :
3233 : 191 : tmp = expand_normal (exp);
3234 : 191 : tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3235 : :
3236 : : /* Truncate the result of floating point optab to integer
3237 : : via expand_fix (). */
3238 : 191 : target = gen_reg_rtx (mode);
3239 : 191 : expand_fix (target, tmp, 0);
3240 : :
3241 : 191 : return target;
3242 : : }
3243 : :
3244 : : /* Expand a call to one of the builtin math functions doing integer
3245 : : conversion (lrint).
3246 : : Return 0 if a normal call should be emitted rather than expanding the
3247 : : function in-line. EXP is the expression that is a call to the builtin
3248 : : function; if convenient, the result should be placed in TARGET. */
3249 : :
3250 : : static rtx
3251 : 590 : expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3252 : : {
3253 : 590 : convert_optab builtin_optab;
3254 : 590 : rtx op0;
3255 : 590 : rtx_insn *insns;
3256 : 590 : tree fndecl = get_callee_fndecl (exp);
3257 : 590 : tree arg;
3258 : 590 : machine_mode mode;
3259 : 590 : enum built_in_function fallback_fn = BUILT_IN_NONE;
3260 : :
3261 : 590 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3262 : : return NULL_RTX;
3263 : :
3264 : 490 : arg = CALL_EXPR_ARG (exp, 0);
3265 : :
3266 : 490 : switch (DECL_FUNCTION_CODE (fndecl))
3267 : : {
3268 : 8 : CASE_FLT_FN (BUILT_IN_IRINT):
3269 : 8 : fallback_fn = BUILT_IN_LRINT;
3270 : : gcc_fallthrough ();
3271 : : CASE_FLT_FN (BUILT_IN_LRINT):
3272 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3273 : : builtin_optab = lrint_optab;
3274 : : break;
3275 : :
3276 : 221 : CASE_FLT_FN (BUILT_IN_IROUND):
3277 : 221 : fallback_fn = BUILT_IN_LROUND;
3278 : : gcc_fallthrough ();
3279 : : CASE_FLT_FN (BUILT_IN_LROUND):
3280 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3281 : : builtin_optab = lround_optab;
3282 : : break;
3283 : :
3284 : 0 : default:
3285 : 0 : gcc_unreachable ();
3286 : : }
3287 : :
3288 : : /* There's no easy way to detect the case we need to set EDOM. */
3289 : 490 : if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3290 : : return NULL_RTX;
3291 : :
3292 : : /* Make a suitable register to place result in. */
3293 : 259 : mode = TYPE_MODE (TREE_TYPE (exp));
3294 : :
3295 : : /* There's no easy way to detect the case we need to set EDOM. */
3296 : 259 : if (!flag_errno_math)
3297 : : {
3298 : 259 : rtx result = gen_reg_rtx (mode);
3299 : :
3300 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3301 : : need to expand the argument again. This way, we will not perform
3302 : : side-effects more the once. */
3303 : 259 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3304 : :
3305 : 259 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3306 : :
3307 : 259 : start_sequence ();
3308 : :
3309 : 259 : if (expand_sfix_optab (result, op0, builtin_optab))
3310 : : {
3311 : : /* Output the entire sequence. */
3312 : 24 : insns = end_sequence ();
3313 : 24 : emit_insn (insns);
3314 : 24 : return result;
3315 : : }
3316 : :
3317 : : /* If we were unable to expand via the builtin, stop the sequence
3318 : : (without outputting the insns) and call to the library function
3319 : : with the stabilized argument list. */
3320 : 235 : end_sequence ();
3321 : : }
3322 : :
3323 : 235 : if (fallback_fn != BUILT_IN_NONE)
3324 : : {
3325 : : /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3326 : : targets, (int) round (x) should never be transformed into
3327 : : BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3328 : : a call to lround in the hope that the target provides at least some
3329 : : C99 functions. This should result in the best user experience for
3330 : : not full C99 targets.
3331 : : As scalar float conversions with same mode are useless in GIMPLE,
3332 : : we can end up e.g. with _Float32 argument passed to float builtin,
3333 : : try to get the type from the builtin prototype first. */
3334 : 221 : tree fallback_fndecl = NULL_TREE;
3335 : 221 : if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3336 : 221 : fallback_fndecl
3337 : 221 : = mathfn_built_in_1 (TREE_VALUE (argtypes),
3338 : : as_combined_fn (fallback_fn), 0);
3339 : 221 : if (fallback_fndecl == NULL_TREE)
3340 : 0 : fallback_fndecl
3341 : 0 : = mathfn_built_in_1 (TREE_TYPE (arg),
3342 : : as_combined_fn (fallback_fn), 0);
3343 : 0 : if (fallback_fndecl)
3344 : : {
3345 : 221 : exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3346 : : fallback_fndecl, 1, arg);
3347 : :
3348 : 221 : target = expand_call (exp, NULL_RTX, target == const0_rtx);
3349 : 221 : target = maybe_emit_group_store (target, TREE_TYPE (exp));
3350 : 221 : return convert_to_mode (mode, target, 0);
3351 : : }
3352 : : }
3353 : :
3354 : 14 : return expand_call (exp, target, target == const0_rtx);
3355 : : }
3356 : :
3357 : : /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3358 : : a normal call should be emitted rather than expanding the function
3359 : : in-line. EXP is the expression that is a call to the builtin
3360 : : function; if convenient, the result should be placed in TARGET. */
3361 : :
3362 : : static rtx
3363 : 279 : expand_builtin_powi (tree exp, rtx target)
3364 : : {
3365 : 279 : tree arg0, arg1;
3366 : 279 : rtx op0, op1;
3367 : 279 : machine_mode mode;
3368 : 279 : machine_mode mode2;
3369 : :
3370 : 279 : if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3371 : : return NULL_RTX;
3372 : :
3373 : 279 : arg0 = CALL_EXPR_ARG (exp, 0);
3374 : 279 : arg1 = CALL_EXPR_ARG (exp, 1);
3375 : 279 : mode = TYPE_MODE (TREE_TYPE (exp));
3376 : :
3377 : : /* Emit a libcall to libgcc. */
3378 : :
3379 : : /* Mode of the 2nd argument must match that of an int. */
3380 : 279 : mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3381 : :
3382 : 279 : if (target == NULL_RTX)
3383 : 0 : target = gen_reg_rtx (mode);
3384 : :
3385 : 279 : op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3386 : 279 : if (GET_MODE (op0) != mode)
3387 : 0 : op0 = convert_to_mode (mode, op0, 0);
3388 : 279 : op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3389 : 279 : if (GET_MODE (op1) != mode2)
3390 : 39 : op1 = convert_to_mode (mode2, op1, 0);
3391 : :
3392 : 279 : target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3393 : : target, LCT_CONST, mode,
3394 : : op0, mode, op1, mode2);
3395 : :
3396 : 279 : return target;
3397 : : }
3398 : :
3399 : : /* Expand expression EXP which is a call to the strlen builtin. Return
3400 : : NULL_RTX if we failed and the caller should emit a normal call, otherwise
3401 : : try to get the result in TARGET, if convenient. */
3402 : :
3403 : : static rtx
3404 : 13426 : expand_builtin_strlen (tree exp, rtx target,
3405 : : machine_mode target_mode)
3406 : : {
3407 : 13426 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3408 : : return NULL_RTX;
3409 : :
3410 : 13406 : tree src = CALL_EXPR_ARG (exp, 0);
3411 : :
3412 : : /* If the length can be computed at compile-time, return it. */
3413 : 13406 : if (tree len = c_strlen (src, 0))
3414 : 173 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3415 : :
3416 : : /* If the length can be computed at compile-time and is constant
3417 : : integer, but there are side-effects in src, evaluate
3418 : : src for side-effects, then return len.
3419 : : E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3420 : : can be optimized into: i++; x = 3; */
3421 : 13233 : tree len = c_strlen (src, 1);
3422 : 13233 : if (len && TREE_CODE (len) == INTEGER_CST)
3423 : : {
3424 : 0 : expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3425 : 0 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3426 : : }
3427 : :
3428 : 13233 : unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3429 : :
3430 : : /* If SRC is not a pointer type, don't do this operation inline. */
3431 : 13233 : if (align == 0)
3432 : : return NULL_RTX;
3433 : :
3434 : : /* Bail out if we can't compute strlen in the right mode. */
3435 : : machine_mode insn_mode;
3436 : : enum insn_code icode = CODE_FOR_nothing;
3437 : 13233 : FOR_EACH_MODE_FROM (insn_mode, target_mode)
3438 : : {
3439 : 13233 : icode = optab_handler (strlen_optab, insn_mode);
3440 : 13233 : if (icode != CODE_FOR_nothing)
3441 : : break;
3442 : : }
3443 : 13233 : if (insn_mode == VOIDmode)
3444 : : return NULL_RTX;
3445 : :
3446 : : /* Make a place to hold the source address. We will not expand
3447 : : the actual source until we are sure that the expansion will
3448 : : not fail -- there are trees that cannot be expanded twice. */
3449 : 13804 : rtx src_reg = gen_reg_rtx (Pmode);
3450 : :
3451 : : /* Mark the beginning of the strlen sequence so we can emit the
3452 : : source operand later. */
3453 : 13233 : rtx_insn *before_strlen = get_last_insn ();
3454 : :
3455 : 13233 : class expand_operand ops[4];
3456 : 13233 : create_output_operand (&ops[0], target, insn_mode);
3457 : 13233 : create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3458 : 13233 : create_integer_operand (&ops[2], 0);
3459 : 13233 : create_integer_operand (&ops[3], align);
3460 : 13233 : if (!maybe_expand_insn (icode, 4, ops))
3461 : : return NULL_RTX;
3462 : :
3463 : : /* Check to see if the argument was declared attribute nonstring
3464 : : and if so, issue a warning since at this point it's not known
3465 : : to be nul-terminated. */
3466 : 11 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3467 : :
3468 : : /* Now that we are assured of success, expand the source. */
3469 : 11 : start_sequence ();
3470 : 15 : rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3471 : 11 : if (pat != src_reg)
3472 : : {
3473 : : #ifdef POINTERS_EXTEND_UNSIGNED
3474 : 15 : if (GET_MODE (pat) != Pmode)
3475 : 0 : pat = convert_to_mode (Pmode, pat,
3476 : : POINTERS_EXTEND_UNSIGNED);
3477 : : #endif
3478 : 11 : emit_move_insn (src_reg, pat);
3479 : : }
3480 : 11 : pat = end_sequence ();
3481 : :
3482 : 11 : if (before_strlen)
3483 : 11 : emit_insn_after (pat, before_strlen);
3484 : : else
3485 : 0 : emit_insn_before (pat, get_insns ());
3486 : :
3487 : : /* Return the value in the proper mode for this function. */
3488 : 11 : if (GET_MODE (ops[0].value) == target_mode)
3489 : : target = ops[0].value;
3490 : 0 : else if (target != 0)
3491 : 0 : convert_move (target, ops[0].value, 0);
3492 : : else
3493 : 0 : target = convert_to_mode (target_mode, ops[0].value, 0);
3494 : :
3495 : : return target;
3496 : : }
3497 : :
3498 : : /* Expand call EXP to the strnlen built-in, returning the result
3499 : : and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3500 : :
3501 : : static rtx
3502 : 580 : expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3503 : : {
3504 : 580 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3505 : : return NULL_RTX;
3506 : :
3507 : 571 : tree src = CALL_EXPR_ARG (exp, 0);
3508 : 571 : tree bound = CALL_EXPR_ARG (exp, 1);
3509 : :
3510 : 571 : if (!bound)
3511 : : return NULL_RTX;
3512 : :
3513 : 571 : location_t loc = UNKNOWN_LOCATION;
3514 : 571 : if (EXPR_HAS_LOCATION (exp))
3515 : 571 : loc = EXPR_LOCATION (exp);
3516 : :
3517 : : /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3518 : : so these conversions aren't necessary. */
3519 : 571 : c_strlen_data lendata = { };
3520 : 571 : tree len = c_strlen (src, 0, &lendata, 1);
3521 : 571 : if (len)
3522 : 102 : len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3523 : :
3524 : 571 : if (TREE_CODE (bound) == INTEGER_CST)
3525 : : {
3526 : 331 : if (!len)
3527 : : return NULL_RTX;
3528 : :
3529 : 62 : len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3530 : 62 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3531 : : }
3532 : :
3533 : 240 : if (TREE_CODE (bound) != SSA_NAME)
3534 : : return NULL_RTX;
3535 : :
3536 : 240 : wide_int min, max;
3537 : 240 : int_range_max r;
3538 : 480 : get_range_query (cfun)->range_of_expr (r, bound,
3539 : : currently_expanding_gimple_stmt);
3540 : 240 : if (r.varying_p () || r.undefined_p ())
3541 : : return NULL_RTX;
3542 : 135 : min = r.lower_bound ();
3543 : 135 : max = r.upper_bound ();
3544 : :
3545 : 135 : if (!len || TREE_CODE (len) != INTEGER_CST)
3546 : : {
3547 : 95 : bool exact;
3548 : 95 : lendata.decl = unterminated_array (src, &len, &exact);
3549 : 95 : if (!lendata.decl)
3550 : 75 : return NULL_RTX;
3551 : : }
3552 : :
3553 : 60 : if (lendata.decl)
3554 : : return NULL_RTX;
3555 : :
3556 : 40 : if (wi::gtu_p (min, wi::to_wide (len)))
3557 : 7 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3558 : :
3559 : 33 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3560 : 33 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3561 : 240 : }
3562 : :
3563 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3564 : : bytes from bytes at DATA + OFFSET and return it reinterpreted as
3565 : : a target constant. */
3566 : :
3567 : : static rtx
3568 : 154508 : builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3569 : : fixed_size_mode mode)
3570 : : {
3571 : : /* The REPresentation pointed to by DATA need not be a nul-terminated
3572 : : string but the caller guarantees it's large enough for MODE. */
3573 : 154508 : const char *rep = (const char *) data;
3574 : :
3575 : 154508 : return c_readstr (rep + offset, mode, /*nul_terminated=*/false);
3576 : : }
3577 : :
3578 : : /* LEN specify length of the block of memcpy/memset operation.
3579 : : Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3580 : : In some cases we can make very likely guess on max size, then we
3581 : : set it into PROBABLE_MAX_SIZE. */
3582 : :
3583 : : static void
3584 : 141658 : determine_block_size (tree len, rtx len_rtx,
3585 : : unsigned HOST_WIDE_INT *min_size,
3586 : : unsigned HOST_WIDE_INT *max_size,
3587 : : unsigned HOST_WIDE_INT *probable_max_size)
3588 : : {
3589 : 141658 : if (CONST_INT_P (len_rtx))
3590 : : {
3591 : 63196 : *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3592 : 63196 : return;
3593 : : }
3594 : : else
3595 : : {
3596 : 78462 : wide_int min, max;
3597 : 78462 : enum value_range_kind range_type = VR_UNDEFINED;
3598 : :
3599 : : /* Determine bounds from the type. */
3600 : 78462 : if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3601 : 78461 : *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3602 : : else
3603 : 1 : *min_size = 0;
3604 : 78462 : if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3605 : 156924 : *probable_max_size = *max_size
3606 : 78462 : = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3607 : : else
3608 : 0 : *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3609 : :
3610 : 78462 : if (TREE_CODE (len) == SSA_NAME)
3611 : : {
3612 : 78462 : int_range_max r;
3613 : 78462 : tree tmin, tmax;
3614 : 78462 : gimple *cg = currently_expanding_gimple_stmt;
3615 : 156924 : get_range_query (cfun)->range_of_expr (r, len, cg);
3616 : 78462 : range_type = get_legacy_range (r, tmin, tmax);
3617 : 78462 : if (range_type != VR_UNDEFINED)
3618 : : {
3619 : 78461 : min = wi::to_wide (tmin);
3620 : 78461 : max = wi::to_wide (tmax);
3621 : : }
3622 : 78462 : }
3623 : 78462 : if (range_type == VR_RANGE)
3624 : : {
3625 : 59083 : if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3626 : 47990 : *min_size = min.to_uhwi ();
3627 : 59083 : if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3628 : 49787 : *probable_max_size = *max_size = max.to_uhwi ();
3629 : : }
3630 : 19379 : else if (range_type == VR_ANTI_RANGE)
3631 : : {
3632 : : /* Code like
3633 : :
3634 : : int n;
3635 : : if (n < 100)
3636 : : memcpy (a, b, n)
3637 : :
3638 : : Produce anti range allowing negative values of N. We still
3639 : : can use the information and make a guess that N is not negative.
3640 : : */
3641 : 3768 : if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3642 : 3689 : *probable_max_size = min.to_uhwi () - 1;
3643 : : }
3644 : 78462 : }
3645 : 78462 : gcc_checking_assert (*max_size <=
3646 : : (unsigned HOST_WIDE_INT)
3647 : : GET_MODE_MASK (GET_MODE (len_rtx)));
3648 : : }
3649 : :
3650 : : /* Expand a call EXP to the memcpy builtin.
3651 : : Return NULL_RTX if we failed, the caller should emit a normal call,
3652 : : otherwise try to get the result in TARGET, if convenient (and in
3653 : : mode MODE if that's convenient). */
3654 : :
3655 : : static rtx
3656 : 88522 : expand_builtin_memcpy (tree exp, rtx target)
3657 : : {
3658 : 88522 : if (!validate_arglist (exp,
3659 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3660 : : return NULL_RTX;
3661 : :
3662 : 88504 : tree dest = CALL_EXPR_ARG (exp, 0);
3663 : 88504 : tree src = CALL_EXPR_ARG (exp, 1);
3664 : 88504 : tree len = CALL_EXPR_ARG (exp, 2);
3665 : :
3666 : 88504 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3667 : 88504 : /*retmode=*/ RETURN_BEGIN, false);
3668 : : }
3669 : :
3670 : : /* Check a call EXP to the memmove built-in for validity.
3671 : : Return NULL_RTX on both success and failure. */
3672 : :
3673 : : static rtx
3674 : 16008 : expand_builtin_memmove (tree exp, rtx target)
3675 : : {
3676 : 16008 : if (!validate_arglist (exp,
3677 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3678 : : return NULL_RTX;
3679 : :
3680 : 15993 : tree dest = CALL_EXPR_ARG (exp, 0);
3681 : 15993 : tree src = CALL_EXPR_ARG (exp, 1);
3682 : 15993 : tree len = CALL_EXPR_ARG (exp, 2);
3683 : :
3684 : 15993 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3685 : 15993 : /*retmode=*/ RETURN_BEGIN, true);
3686 : : }
3687 : :
3688 : : /* Expand a call EXP to the mempcpy builtin.
3689 : : Return NULL_RTX if we failed; the caller should emit a normal call,
3690 : : otherwise try to get the result in TARGET, if convenient (and in
3691 : : mode MODE if that's convenient). */
3692 : :
3693 : : static rtx
3694 : 1637 : expand_builtin_mempcpy (tree exp, rtx target)
3695 : : {
3696 : 1637 : if (!validate_arglist (exp,
3697 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3698 : : return NULL_RTX;
3699 : :
3700 : 1632 : tree dest = CALL_EXPR_ARG (exp, 0);
3701 : 1632 : tree src = CALL_EXPR_ARG (exp, 1);
3702 : 1632 : tree len = CALL_EXPR_ARG (exp, 2);
3703 : :
3704 : : /* Policy does not generally allow using compute_objsize (which
3705 : : is used internally by check_memop_size) to change code generation
3706 : : or drive optimization decisions.
3707 : :
3708 : : In this instance it is safe because the code we generate has
3709 : : the same semantics regardless of the return value of
3710 : : check_memop_sizes. Exactly the same amount of data is copied
3711 : : and the return value is exactly the same in both cases.
3712 : :
3713 : : Furthermore, check_memop_size always uses mode 0 for the call to
3714 : : compute_objsize, so the imprecise nature of compute_objsize is
3715 : : avoided. */
3716 : :
3717 : : /* Avoid expanding mempcpy into memcpy when the call is determined
3718 : : to overflow the buffer. This also prevents the same overflow
3719 : : from being diagnosed again when expanding memcpy. */
3720 : :
3721 : 1632 : return expand_builtin_mempcpy_args (dest, src, len,
3722 : 1632 : target, exp, /*retmode=*/ RETURN_END);
3723 : : }
3724 : :
3725 : : /* Helper function to do the actual work for expand of memory copy family
3726 : : functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3727 : : of memory from SRC to DEST and assign to TARGET if convenient. Return
3728 : : value is based on RETMODE argument. */
3729 : :
3730 : : static rtx
3731 : 106152 : expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3732 : : rtx target, tree exp, memop_ret retmode,
3733 : : bool might_overlap)
3734 : : {
3735 : 106152 : unsigned int src_align = get_pointer_alignment (src);
3736 : 106152 : unsigned int dest_align = get_pointer_alignment (dest);
3737 : 106152 : rtx dest_mem, src_mem, dest_addr, len_rtx;
3738 : 106152 : HOST_WIDE_INT expected_size = -1;
3739 : 106152 : unsigned int expected_align = 0;
3740 : 106152 : unsigned HOST_WIDE_INT min_size;
3741 : 106152 : unsigned HOST_WIDE_INT max_size;
3742 : 106152 : unsigned HOST_WIDE_INT probable_max_size;
3743 : :
3744 : 106152 : bool is_move_done;
3745 : :
3746 : : /* If DEST is not a pointer type, call the normal function. */
3747 : 106152 : if (dest_align == 0)
3748 : : return NULL_RTX;
3749 : :
3750 : : /* If either SRC is not a pointer type, don't do this
3751 : : operation in-line. */
3752 : 106152 : if (src_align == 0)
3753 : : return NULL_RTX;
3754 : :
3755 : 106152 : if (currently_expanding_gimple_stmt)
3756 : 106152 : stringop_block_profile (currently_expanding_gimple_stmt,
3757 : : &expected_align, &expected_size);
3758 : :
3759 : 106152 : if (expected_align < dest_align)
3760 : 106140 : expected_align = dest_align;
3761 : 106152 : dest_mem = get_memory_rtx (dest, len);
3762 : 106152 : set_mem_align (dest_mem, dest_align);
3763 : 106152 : len_rtx = expand_normal (len);
3764 : 106152 : determine_block_size (len, len_rtx, &min_size, &max_size,
3765 : : &probable_max_size);
3766 : :
3767 : : /* Try to get the byte representation of the constant SRC points to,
3768 : : with its byte size in NBYTES. */
3769 : 106152 : unsigned HOST_WIDE_INT nbytes;
3770 : 106152 : const char *rep = getbyterep (src, &nbytes);
3771 : :
3772 : : /* If the function's constant bound LEN_RTX is less than or equal
3773 : : to the byte size of the representation of the constant argument,
3774 : : and if block move would be done by pieces, we can avoid loading
3775 : : the bytes from memory and only store the computed constant.
3776 : : This works in the overlap (memmove) case as well because
3777 : : store_by_pieces just generates a series of stores of constants
3778 : : from the representation returned by getbyterep(). */
3779 : 106152 : if (rep
3780 : 28198 : && CONST_INT_P (len_rtx)
3781 : 26688 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3782 : 132840 : && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3783 : : CONST_CAST (char *, rep),
3784 : : dest_align, false))
3785 : : {
3786 : 26225 : dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3787 : : builtin_memcpy_read_str,
3788 : : CONST_CAST (char *, rep),
3789 : : dest_align, false, retmode);
3790 : 26225 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
3791 : 26225 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
3792 : 26225 : return dest_mem;
3793 : : }
3794 : :
3795 : 79927 : src_mem = get_memory_rtx (src, len);
3796 : 79927 : set_mem_align (src_mem, src_align);
3797 : :
3798 : : /* Copy word part most expediently. */
3799 : 79927 : enum block_op_methods method = BLOCK_OP_NORMAL;
3800 : 79927 : if (CALL_EXPR_TAILCALL (exp)
3801 : 79927 : && (retmode == RETURN_BEGIN || target == const0_rtx))
3802 : : method = BLOCK_OP_TAILCALL;
3803 : 79927 : bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3804 : 79927 : && retmode == RETURN_END
3805 : 79927 : && !might_overlap
3806 : 81229 : && target != const0_rtx);
3807 : : if (use_mempcpy_call)
3808 : 79927 : method = BLOCK_OP_NO_LIBCALL_RET;
3809 : 79927 : dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3810 : : expected_align, expected_size,
3811 : : min_size, max_size, probable_max_size,
3812 : : use_mempcpy_call, &is_move_done,
3813 : : might_overlap, tree_ctz (len));
3814 : :
3815 : : /* Bail out when a mempcpy call would be expanded as libcall and when
3816 : : we have a target that provides a fast implementation
3817 : : of mempcpy routine. */
3818 : 79927 : if (!is_move_done)
3819 : : return NULL_RTX;
3820 : :
3821 : 64139 : if (dest_addr == pc_rtx)
3822 : : return NULL_RTX;
3823 : :
3824 : 64139 : if (dest_addr == 0)
3825 : : {
3826 : 25610 : dest_addr = force_operand (XEXP (dest_mem, 0), target);
3827 : 25610 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
3828 : : }
3829 : :
3830 : 64139 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3831 : : {
3832 : 808 : dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3833 : : /* stpcpy pointer to last byte. */
3834 : 808 : if (retmode == RETURN_END_MINUS_ONE)
3835 : 0 : dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3836 : : }
3837 : :
3838 : : return dest_addr;
3839 : : }
3840 : :
3841 : : static rtx
3842 : 1655 : expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3843 : : rtx target, tree orig_exp, memop_ret retmode)
3844 : : {
3845 : 1632 : return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3846 : 0 : retmode, false);
3847 : : }
3848 : :
3849 : : /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3850 : : we failed, the caller should emit a normal call, otherwise try to
3851 : : get the result in TARGET, if convenient.
3852 : : Return value is based on RETMODE argument. */
3853 : :
3854 : : static rtx
3855 : 2269 : expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3856 : : {
3857 : 2269 : class expand_operand ops[3];
3858 : 2269 : rtx dest_mem;
3859 : 2269 : rtx src_mem;
3860 : :
3861 : 2269 : if (!targetm.have_movstr ())
3862 : : return NULL_RTX;
3863 : :
3864 : 0 : dest_mem = get_memory_rtx (dest, NULL);
3865 : 0 : src_mem = get_memory_rtx (src, NULL);
3866 : 0 : if (retmode == RETURN_BEGIN)
3867 : : {
3868 : 0 : target = force_reg (Pmode, XEXP (dest_mem, 0));
3869 : 0 : dest_mem = replace_equiv_address (dest_mem, target);
3870 : : }
3871 : :
3872 : 0 : create_output_operand (&ops[0],
3873 : 0 : retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3874 : 0 : create_fixed_operand (&ops[1], dest_mem);
3875 : 0 : create_fixed_operand (&ops[2], src_mem);
3876 : 0 : if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3877 : : return NULL_RTX;
3878 : :
3879 : 0 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3880 : : {
3881 : 0 : target = ops[0].value;
3882 : : /* movstr is supposed to set end to the address of the NUL
3883 : : terminator. If the caller requested a mempcpy-like return value,
3884 : : adjust it. */
3885 : 0 : if (retmode == RETURN_END)
3886 : : {
3887 : 0 : rtx tem = plus_constant (GET_MODE (target),
3888 : 0 : gen_lowpart (GET_MODE (target), target), 1);
3889 : 0 : emit_move_insn (target, force_operand (tem, NULL_RTX));
3890 : : }
3891 : : }
3892 : : return target;
3893 : : }
3894 : :
3895 : : /* Expand expression EXP, which is a call to the strcpy builtin. Return
3896 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
3897 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
3898 : : convenient). */
3899 : :
3900 : : static rtx
3901 : 1862 : expand_builtin_strcpy (tree exp, rtx target)
3902 : : {
3903 : 1862 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3904 : : return NULL_RTX;
3905 : :
3906 : 1847 : tree dest = CALL_EXPR_ARG (exp, 0);
3907 : 1847 : tree src = CALL_EXPR_ARG (exp, 1);
3908 : :
3909 : 1847 : return expand_builtin_strcpy_args (exp, dest, src, target);
3910 : : }
3911 : :
3912 : : /* Helper function to do the actual work for expand_builtin_strcpy. The
3913 : : arguments to the builtin_strcpy call DEST and SRC are broken out
3914 : : so that this can also be called without constructing an actual CALL_EXPR.
3915 : : The other arguments and return value are the same as for
3916 : : expand_builtin_strcpy. */
3917 : :
3918 : : static rtx
3919 : 1847 : expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3920 : : {
3921 : 1847 : return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3922 : : }
3923 : :
3924 : : /* Expand a call EXP to the stpcpy builtin.
3925 : : Return NULL_RTX if we failed the caller should emit a normal call,
3926 : : otherwise try to get the result in TARGET, if convenient (and in
3927 : : mode MODE if that's convenient). */
3928 : :
3929 : : static rtx
3930 : 454 : expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3931 : : {
3932 : 454 : tree dst, src;
3933 : 454 : location_t loc = EXPR_LOCATION (exp);
3934 : :
3935 : 454 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3936 : : return NULL_RTX;
3937 : :
3938 : 445 : dst = CALL_EXPR_ARG (exp, 0);
3939 : 445 : src = CALL_EXPR_ARG (exp, 1);
3940 : :
3941 : : /* If return value is ignored, transform stpcpy into strcpy. */
3942 : 445 : if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3943 : : {
3944 : 0 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3945 : 0 : tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3946 : 0 : return expand_expr (result, target, mode, EXPAND_NORMAL);
3947 : : }
3948 : : else
3949 : : {
3950 : 445 : tree len, lenp1;
3951 : 445 : rtx ret;
3952 : :
3953 : : /* Ensure we get an actual string whose length can be evaluated at
3954 : : compile-time, not an expression containing a string. This is
3955 : : because the latter will potentially produce pessimized code
3956 : : when used to produce the return value. */
3957 : 445 : c_strlen_data lendata = { };
3958 : 445 : if (!c_getstr (src)
3959 : 445 : || !(len = c_strlen (src, 0, &lendata, 1)))
3960 : 422 : return expand_movstr (dst, src, target,
3961 : 422 : /*retmode=*/ RETURN_END_MINUS_ONE);
3962 : :
3963 : 23 : lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3964 : 23 : ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3965 : : target, exp,
3966 : : /*retmode=*/ RETURN_END_MINUS_ONE);
3967 : :
3968 : 23 : if (ret)
3969 : : return ret;
3970 : :
3971 : 0 : if (TREE_CODE (len) == INTEGER_CST)
3972 : : {
3973 : 0 : rtx len_rtx = expand_normal (len);
3974 : :
3975 : 0 : if (CONST_INT_P (len_rtx))
3976 : : {
3977 : 0 : ret = expand_builtin_strcpy_args (exp, dst, src, target);
3978 : :
3979 : 0 : if (ret)
3980 : : {
3981 : 0 : if (! target)
3982 : : {
3983 : 0 : if (mode != VOIDmode)
3984 : 0 : target = gen_reg_rtx (mode);
3985 : : else
3986 : 0 : target = gen_reg_rtx (GET_MODE (ret));
3987 : : }
3988 : 0 : if (GET_MODE (target) != GET_MODE (ret))
3989 : 0 : ret = gen_lowpart (GET_MODE (target), ret);
3990 : :
3991 : 0 : ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3992 : 0 : ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3993 : 0 : gcc_assert (ret);
3994 : :
3995 : : return target;
3996 : : }
3997 : : }
3998 : : }
3999 : :
4000 : 0 : return expand_movstr (dst, src, target,
4001 : 0 : /*retmode=*/ RETURN_END_MINUS_ONE);
4002 : : }
4003 : : }
4004 : :
4005 : : /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
4006 : : arguments while being careful to avoid duplicate warnings (which could
4007 : : be issued if the expander were to expand the call, resulting in it
4008 : : being emitted in expand_call(). */
4009 : :
4010 : : static rtx
4011 : 454 : expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
4012 : : {
4013 : 454 : if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
4014 : : {
4015 : : /* The call has been successfully expanded. Check for nonstring
4016 : : arguments and issue warnings as appropriate. */
4017 : 23 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
4018 : 23 : return ret;
4019 : : }
4020 : :
4021 : : return NULL_RTX;
4022 : : }
4023 : :
4024 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4025 : : bytes from constant string DATA + OFFSET and return it as target
4026 : : constant. */
4027 : :
4028 : : rtx
4029 : 9216 : builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
4030 : : fixed_size_mode mode)
4031 : : {
4032 : 9216 : const char *str = (const char *) data;
4033 : :
4034 : 9216 : if ((unsigned HOST_WIDE_INT) offset > strlen (str))
4035 : 260 : return const0_rtx;
4036 : :
4037 : 8956 : return c_readstr (str + offset, mode);
4038 : : }
4039 : :
4040 : : /* Helper to check the sizes of sequences and the destination of calls
4041 : : to __builtin_strncat and __builtin___strncat_chk. Returns true on
4042 : : success (no overflow or invalid sizes), false otherwise. */
4043 : :
4044 : : static bool
4045 : 109 : check_strncat_sizes (tree exp, tree objsize)
4046 : : {
4047 : 109 : tree dest = CALL_EXPR_ARG (exp, 0);
4048 : 109 : tree src = CALL_EXPR_ARG (exp, 1);
4049 : 109 : tree maxread = CALL_EXPR_ARG (exp, 2);
4050 : :
4051 : : /* Try to determine the range of lengths that the source expression
4052 : : refers to. */
4053 : 109 : c_strlen_data lendata = { };
4054 : 109 : get_range_strlen (src, &lendata, /* eltsize = */ 1);
4055 : :
4056 : : /* Try to verify that the destination is big enough for the shortest
4057 : : string. */
4058 : :
4059 : 109 : access_data data (nullptr, exp, access_read_write, maxread, true);
4060 : 109 : if (!objsize && warn_stringop_overflow)
4061 : : {
4062 : : /* If it hasn't been provided by __strncat_chk, try to determine
4063 : : the size of the destination object into which the source is
4064 : : being copied. */
4065 : 0 : objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4066 : : }
4067 : :
4068 : : /* Add one for the terminating nul. */
4069 : 109 : tree srclen = (lendata.minlen
4070 : 109 : ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4071 : : size_one_node)
4072 : : : NULL_TREE);
4073 : :
4074 : : /* The strncat function copies at most MAXREAD bytes and always appends
4075 : : the terminating nul so the specified upper bound should never be equal
4076 : : to (or greater than) the size of the destination. */
4077 : 56 : if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4078 : 157 : && tree_int_cst_equal (objsize, maxread))
4079 : : {
4080 : 23 : location_t loc = EXPR_LOCATION (exp);
4081 : 23 : warning_at (loc, OPT_Wstringop_overflow_,
4082 : : "%qD specified bound %E equals destination size",
4083 : : get_callee_fndecl (exp), maxread);
4084 : :
4085 : 23 : return false;
4086 : : }
4087 : :
4088 : 86 : if (!srclen
4089 : 86 : || (maxread && tree_fits_uhwi_p (maxread)
4090 : 33 : && tree_fits_uhwi_p (srclen)
4091 : 33 : && tree_int_cst_lt (maxread, srclen)))
4092 : : srclen = maxread;
4093 : :
4094 : : /* The number of bytes to write is LEN but check_access will alsoa
4095 : : check SRCLEN if LEN's value isn't known. */
4096 : 86 : return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4097 : 86 : objsize, data.mode, &data);
4098 : : }
4099 : :
4100 : : /* Expand expression EXP, which is a call to the strncpy builtin. Return
4101 : : NULL_RTX if we failed the caller should emit a normal call. */
4102 : :
4103 : : static rtx
4104 : 2166 : expand_builtin_strncpy (tree exp, rtx target)
4105 : : {
4106 : 2166 : location_t loc = EXPR_LOCATION (exp);
4107 : :
4108 : 2166 : if (!validate_arglist (exp,
4109 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4110 : : return NULL_RTX;
4111 : 2154 : tree dest = CALL_EXPR_ARG (exp, 0);
4112 : 2154 : tree src = CALL_EXPR_ARG (exp, 1);
4113 : : /* The number of bytes to write (not the maximum). */
4114 : 2154 : tree len = CALL_EXPR_ARG (exp, 2);
4115 : :
4116 : : /* The length of the source sequence. */
4117 : 2154 : tree slen = c_strlen (src, 1);
4118 : :
4119 : : /* We must be passed a constant len and src parameter. */
4120 : 2154 : if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4121 : : return NULL_RTX;
4122 : :
4123 : 237 : slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4124 : :
4125 : : /* We're required to pad with trailing zeros if the requested
4126 : : len is greater than strlen(s2)+1. In that case try to
4127 : : use store_by_pieces, if it fails, punt. */
4128 : 237 : if (tree_int_cst_lt (slen, len))
4129 : : {
4130 : 142 : unsigned int dest_align = get_pointer_alignment (dest);
4131 : 142 : const char *p = c_getstr (src);
4132 : 142 : rtx dest_mem;
4133 : :
4134 : 136 : if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4135 : 278 : || !can_store_by_pieces (tree_to_uhwi (len),
4136 : : builtin_strncpy_read_str,
4137 : : CONST_CAST (char *, p),
4138 : : dest_align, false))
4139 : 14 : return NULL_RTX;
4140 : :
4141 : 128 : dest_mem = get_memory_rtx (dest, len);
4142 : 128 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4143 : : builtin_strncpy_read_str,
4144 : : CONST_CAST (char *, p), dest_align, false,
4145 : : RETURN_BEGIN);
4146 : 128 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
4147 : 128 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4148 : 128 : return dest_mem;
4149 : : }
4150 : :
4151 : : return NULL_RTX;
4152 : : }
4153 : :
4154 : : /* Return the RTL of a register in MODE generated from PREV in the
4155 : : previous iteration. */
4156 : :
4157 : : static rtx
4158 : 196900 : gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4159 : : {
4160 : 196900 : rtx target = nullptr;
4161 : 196900 : if (prev != nullptr && prev->data != nullptr)
4162 : : {
4163 : : /* Use the previous data in the same mode. */
4164 : 111935 : if (prev->mode == mode)
4165 : 196900 : return prev->data;
4166 : :
4167 : 29602 : fixed_size_mode prev_mode = prev->mode;
4168 : :
4169 : : /* Don't use the previous data to write QImode if it is in a
4170 : : vector mode. */
4171 : 29602 : if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4172 : : return target;
4173 : :
4174 : 29338 : rtx prev_rtx = prev->data;
4175 : :
4176 : 29338 : if (REG_P (prev_rtx)
4177 : 18609 : && HARD_REGISTER_P (prev_rtx)
4178 : 29338 : && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4179 : : {
4180 : : /* This case occurs when PREV_MODE is a vector and when
4181 : : MODE is too small to store using vector operations.
4182 : : After register allocation, the code will need to move the
4183 : : lowpart of the vector register into a non-vector register.
4184 : :
4185 : : Also, the target has chosen to use a hard register
4186 : : instead of going with the default choice of using a
4187 : : pseudo register. We should respect that choice and try to
4188 : : avoid creating a pseudo register with the same mode as the
4189 : : current hard register.
4190 : :
4191 : : In principle, we could just use a lowpart MODE subreg of
4192 : : the vector register. However, the vector register mode might
4193 : : be too wide for non-vector registers, and we already know
4194 : : that the non-vector mode is too small for vector registers.
4195 : : It's therefore likely that we'd need to spill to memory in
4196 : : the vector mode and reload the non-vector value from there.
4197 : :
4198 : : Try to avoid that by reducing the vector register to the
4199 : : smallest size that it can hold. This should increase the
4200 : : chances that non-vector registers can hold both the inner
4201 : : and outer modes of the subreg that we generate later. */
4202 : 0 : machine_mode m;
4203 : 0 : fixed_size_mode candidate;
4204 : 0 : FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4205 : 0 : if (is_a<fixed_size_mode> (m, &candidate))
4206 : : {
4207 : 0 : if (GET_MODE_SIZE (candidate)
4208 : 0 : >= GET_MODE_SIZE (prev_mode))
4209 : : break;
4210 : 0 : if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4211 : 0 : && lowpart_subreg_regno (REGNO (prev_rtx),
4212 : : prev_mode, candidate) >= 0)
4213 : : {
4214 : 0 : target = lowpart_subreg (candidate, prev_rtx,
4215 : : prev_mode);
4216 : 0 : prev_rtx = target;
4217 : 0 : prev_mode = candidate;
4218 : 0 : break;
4219 : : }
4220 : : }
4221 : 0 : if (target == nullptr)
4222 : 0 : prev_rtx = copy_to_reg (prev_rtx);
4223 : : }
4224 : :
4225 : 29338 : target = lowpart_subreg (mode, prev_rtx, prev_mode);
4226 : : }
4227 : : return target;
4228 : : }
4229 : :
4230 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4231 : : bytes from constant string DATA + OFFSET and return it as target
4232 : : constant. If PREV isn't nullptr, it has the RTL info from the
4233 : : previous iteration. */
4234 : :
4235 : : rtx
4236 : 195625 : builtin_memset_read_str (void *data, void *prev,
4237 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4238 : : fixed_size_mode mode)
4239 : : {
4240 : 195625 : const char *c = (const char *) data;
4241 : 195625 : unsigned int size = GET_MODE_SIZE (mode);
4242 : :
4243 : 195625 : rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4244 : : mode);
4245 : 195625 : if (target != nullptr)
4246 : : return target;
4247 : 84504 : rtx src = gen_int_mode (*c, QImode);
4248 : :
4249 : 84504 : if (VECTOR_MODE_P (mode))
4250 : : {
4251 : 113886 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4252 : :
4253 : 56943 : rtx const_vec = gen_const_vec_duplicate (mode, src);
4254 : 56943 : if (prev == NULL)
4255 : : /* Return CONST_VECTOR when called by a query function. */
4256 : : return const_vec;
4257 : :
4258 : : /* Use the move expander with CONST_VECTOR. */
4259 : 41080 : target = gen_reg_rtx (mode);
4260 : 41080 : emit_move_insn (target, const_vec);
4261 : 41080 : return target;
4262 : : }
4263 : :
4264 : 27561 : char *p = XALLOCAVEC (char, size);
4265 : :
4266 : 27561 : memset (p, *c, size);
4267 : :
4268 : 27561 : return c_readstr (p, mode);
4269 : : }
4270 : :
4271 : : /* Callback routine for store_by_pieces. Return the RTL of a register
4272 : : containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4273 : : char value given in the RTL register data. For example, if mode is
4274 : : 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4275 : : nullptr, it has the RTL info from the previous iteration. */
4276 : :
4277 : : static rtx
4278 : 1442 : builtin_memset_gen_str (void *data, void *prev,
4279 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4280 : : fixed_size_mode mode)
4281 : : {
4282 : 1442 : rtx target, coeff;
4283 : 1442 : size_t size;
4284 : 1442 : char *p;
4285 : :
4286 : 1442 : size = GET_MODE_SIZE (mode);
4287 : 1442 : if (size == 1)
4288 : : return (rtx) data;
4289 : :
4290 : 1275 : target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4291 : 1275 : if (target != nullptr)
4292 : : return target;
4293 : :
4294 : 725 : if (VECTOR_MODE_P (mode))
4295 : : {
4296 : 440 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4297 : :
4298 : : /* vec_duplicate_optab is a precondition to pick a vector mode for
4299 : : the memset expander. */
4300 : 220 : insn_code icode = optab_handler (vec_duplicate_optab, mode);
4301 : :
4302 : 220 : target = gen_reg_rtx (mode);
4303 : 220 : class expand_operand ops[2];
4304 : 220 : create_output_operand (&ops[0], target, mode);
4305 : 220 : create_input_operand (&ops[1], (rtx) data, QImode);
4306 : 220 : expand_insn (icode, 2, ops);
4307 : 220 : if (!rtx_equal_p (target, ops[0].value))
4308 : 0 : emit_move_insn (target, ops[0].value);
4309 : :
4310 : 220 : return target;
4311 : : }
4312 : :
4313 : 505 : p = XALLOCAVEC (char, size);
4314 : 505 : memset (p, 1, size);
4315 : 505 : coeff = c_readstr (p, mode);
4316 : :
4317 : 505 : target = convert_to_mode (mode, (rtx) data, 1);
4318 : 505 : target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4319 : 505 : return force_reg (mode, target);
4320 : : }
4321 : :
4322 : : /* Expand expression EXP, which is a call to the memset builtin. Return
4323 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
4324 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
4325 : : convenient). */
4326 : :
4327 : : rtx
4328 : 35515 : expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4329 : : {
4330 : 35515 : if (!validate_arglist (exp,
4331 : : POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4332 : : return NULL_RTX;
4333 : :
4334 : 35506 : tree dest = CALL_EXPR_ARG (exp, 0);
4335 : 35506 : tree val = CALL_EXPR_ARG (exp, 1);
4336 : 35506 : tree len = CALL_EXPR_ARG (exp, 2);
4337 : :
4338 : 35506 : return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4339 : : }
4340 : :
4341 : : /* Check that store_by_pieces allows BITS + LEN (so that we don't
4342 : : expand something too unreasonably long), and every power of 2 in
4343 : : BITS. It is assumed that LEN has already been tested by
4344 : : itself. */
4345 : : static bool
4346 : 22841 : can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
4347 : : by_pieces_constfn constfun,
4348 : : void *constfundata, unsigned int align,
4349 : : bool memsetp,
4350 : : unsigned HOST_WIDE_INT len)
4351 : : {
4352 : 22841 : if (bits
4353 : 22841 : && !can_store_by_pieces (bits + len, constfun, constfundata,
4354 : : align, memsetp))
4355 : : return false;
4356 : :
4357 : : /* BITS set are expected to be generally in the low range and
4358 : : contiguous. We do NOT want to repeat the test above in case BITS
4359 : : has a single bit set, so we terminate the loop when BITS == BIT.
4360 : : In the unlikely case that BITS has the MSB set, also terminate in
4361 : : case BIT gets shifted out. */
4362 : 1113 : for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
4363 : : {
4364 : 840 : if ((bits & bit) == 0)
4365 : 504 : continue;
4366 : :
4367 : 336 : if (!can_store_by_pieces (bit, constfun, constfundata,
4368 : : align, memsetp))
4369 : : return false;
4370 : : }
4371 : :
4372 : : return true;
4373 : : }
4374 : :
4375 : : /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4376 : : Return TRUE if successful, FALSE otherwise. TO is assumed to be
4377 : : aligned at an ALIGN-bits boundary. LEN must be a multiple of
4378 : : 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4379 : :
4380 : : The strategy is to issue one store_by_pieces for each power of two,
4381 : : from most to least significant, guarded by a test on whether there
4382 : : are at least that many bytes left to copy in LEN.
4383 : :
4384 : : ??? Should we skip some powers of two in favor of loops? Maybe start
4385 : : at the max of TO/LEN/word alignment, at least when optimizing for
4386 : : size, instead of ensuring O(log len) dynamic compares? */
4387 : :
4388 : : bool
4389 : 21141 : try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4390 : : unsigned HOST_WIDE_INT min_len,
4391 : : unsigned HOST_WIDE_INT max_len,
4392 : : rtx val, char valc, unsigned int align)
4393 : : {
4394 : 21141 : int max_bits = floor_log2 (max_len);
4395 : 21141 : int min_bits = floor_log2 (min_len);
4396 : 21141 : int sctz_len = ctz_len;
4397 : :
4398 : 21141 : gcc_checking_assert (sctz_len >= 0);
4399 : :
4400 : 21141 : if (val)
4401 : 532 : valc = 1;
4402 : :
4403 : : /* Bits more significant than TST_BITS are part of the shared prefix
4404 : : in the binary representation of both min_len and max_len. Since
4405 : : they're identical, we don't need to test them in the loop. */
4406 : 21141 : int tst_bits = (max_bits != min_bits ? max_bits
4407 : 8816 : : floor_log2 (max_len ^ min_len));
4408 : :
4409 : : /* Save the pre-blksize values. */
4410 : 21141 : int orig_max_bits = max_bits;
4411 : 21141 : int orig_tst_bits = tst_bits;
4412 : :
4413 : : /* Check whether it's profitable to start by storing a fixed BLKSIZE
4414 : : bytes, to lower max_bits. In the unlikely case of a constant LEN
4415 : : (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4416 : : single store_by_pieces, but otherwise, select the minimum multiple
4417 : : of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4418 : : brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4419 : 21141 : unsigned HOST_WIDE_INT blksize;
4420 : 21141 : if (max_len > min_len)
4421 : : {
4422 : 12338 : unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4423 : : align / BITS_PER_UNIT);
4424 : 12338 : blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4425 : 12338 : blksize &= ~(alrng - 1);
4426 : : }
4427 : 8803 : else if (max_len == min_len)
4428 : : blksize = max_len;
4429 : : else
4430 : : /* Huh, max_len < min_len? Punt. See pr100843.c. */
4431 : : return false;
4432 : 21140 : if (min_len >= blksize
4433 : : /* ??? Maybe try smaller fixed-prefix blksizes before
4434 : : punting? */
4435 : 21140 : && can_store_by_pieces (blksize, builtin_memset_read_str,
4436 : : &valc, align, true))
4437 : : {
4438 : 274 : min_len -= blksize;
4439 : 274 : min_bits = floor_log2 (min_len);
4440 : 274 : max_len -= blksize;
4441 : 274 : max_bits = floor_log2 (max_len);
4442 : :
4443 : 274 : tst_bits = (max_bits != min_bits ? max_bits
4444 : 166 : : floor_log2 (max_len ^ min_len));
4445 : : }
4446 : : else
4447 : : blksize = 0;
4448 : :
4449 : : /* Check that we can use store by pieces for the maximum store count
4450 : : we may issue (initial fixed-size block, plus conditional
4451 : : power-of-two-sized from max_bits to ctz_len. */
4452 : 21140 : unsigned HOST_WIDE_INT xlenest = blksize;
4453 : 21140 : if (max_bits >= 0)
4454 : 20974 : xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4455 : 20974 : - (HOST_WIDE_INT_1U << ctz_len));
4456 : 21140 : bool max_loop = false;
4457 : 21140 : bool use_store_by_pieces = true;
4458 : : /* Skip the test in case of overflow in xlenest. It shouldn't
4459 : : happen because of the way max_bits and blksize are related, but
4460 : : it doesn't hurt to test. */
4461 : 21140 : if (blksize > xlenest
4462 : 21140 : || !can_store_by_multiple_pieces (xlenest - blksize,
4463 : : builtin_memset_read_str,
4464 : : &valc, align, true, blksize))
4465 : : {
4466 : 20974 : if (!(flag_inline_stringops & ILSOP_MEMSET))
4467 : : return false;
4468 : :
4469 : 1601 : for (max_bits = orig_max_bits;
4470 : 1708 : max_bits >= sctz_len;
4471 : : --max_bits)
4472 : : {
4473 : 1708 : xlenest = ((HOST_WIDE_INT_1U << max_bits) * 2
4474 : 1708 : - (HOST_WIDE_INT_1U << ctz_len));
4475 : : /* Check that blksize plus the bits to be stored as blocks
4476 : : sized at powers of two can be stored by pieces. This is
4477 : : like the test above, but with smaller max_bits. Skip
4478 : : orig_max_bits (it would be redundant). Also skip in case
4479 : : of overflow. */
4480 : 1708 : if (max_bits < orig_max_bits
4481 : 1601 : && xlenest + blksize >= xlenest
4482 : 3309 : && can_store_by_multiple_pieces (xlenest,
4483 : : builtin_memset_read_str,
4484 : : &valc, align, true, blksize))
4485 : : {
4486 : : max_loop = true;
4487 : : break;
4488 : : }
4489 : 1601 : if (blksize
4490 : 1601 : && can_store_by_multiple_pieces (xlenest,
4491 : : builtin_memset_read_str,
4492 : : &valc, align, true, 0))
4493 : : {
4494 : 0 : max_len += blksize;
4495 : 0 : min_len += blksize;
4496 : 0 : tst_bits = orig_tst_bits;
4497 : 0 : blksize = 0;
4498 : 0 : max_loop = true;
4499 : 0 : break;
4500 : : }
4501 : 1601 : if (max_bits == sctz_len)
4502 : : {
4503 : : /* We'll get here if can_store_by_pieces refuses to
4504 : : store even a single QImode. We'll fall back to
4505 : : QImode stores then. */
4506 : 98 : if (!sctz_len)
4507 : : {
4508 : : blksize = 0;
4509 : : max_loop = true;
4510 : : use_store_by_pieces = false;
4511 : : break;
4512 : : }
4513 : 98 : --sctz_len;
4514 : 98 : --ctz_len;
4515 : : }
4516 : : }
4517 : 107 : if (!max_loop)
4518 : : return false;
4519 : : /* If the boundaries are such that min and max may run a
4520 : : different number of trips in the initial loop, the remainder
4521 : : needs not be between the moduli, so set tst_bits to cover all
4522 : : bits. Otherwise, if the trip counts are the same, max_len
4523 : : has the common prefix, and the previously-computed tst_bits
4524 : : is usable. */
4525 : 107 : if (max_len >> max_bits > min_len >> max_bits)
4526 : 64 : tst_bits = max_bits;
4527 : : }
4528 : :
4529 : 273 : by_pieces_constfn constfun;
4530 : 273 : void *constfundata;
4531 : 273 : if (val)
4532 : : {
4533 : 1 : constfun = builtin_memset_gen_str;
4534 : 1 : constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4535 : : val);
4536 : : }
4537 : : else
4538 : : {
4539 : : constfun = builtin_memset_read_str;
4540 : : constfundata = &valc;
4541 : : }
4542 : :
4543 : 273 : rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4544 : 273 : rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4545 : 273 : to = replace_equiv_address (to, ptr);
4546 : 273 : set_mem_align (to, align);
4547 : :
4548 : 273 : if (blksize)
4549 : : {
4550 : 350 : to = store_by_pieces (to, blksize,
4551 : : constfun, constfundata,
4552 : : align, true,
4553 : : max_len != 0 ? RETURN_END : RETURN_BEGIN);
4554 : 185 : if (max_len == 0)
4555 : : return true;
4556 : :
4557 : : /* Adjust PTR, TO and REM. Since TO's address is likely
4558 : : PTR+offset, we have to replace it. */
4559 : 20 : emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4560 : 20 : to = replace_equiv_address (to, ptr);
4561 : 20 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4562 : 20 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4563 : : }
4564 : :
4565 : : /* Iterate over power-of-two block sizes from the maximum length to
4566 : : the least significant bit possibly set in the length. */
4567 : 493 : for (int i = max_bits; i >= sctz_len; i--)
4568 : : {
4569 : 385 : rtx_code_label *loop_label = NULL;
4570 : 385 : rtx_code_label *label = NULL;
4571 : :
4572 : 385 : blksize = HOST_WIDE_INT_1U << i;
4573 : :
4574 : : /* If we're past the bits shared between min_ and max_len, expand
4575 : : a test on the dynamic length, comparing it with the
4576 : : BLKSIZE. */
4577 : 385 : if (i <= tst_bits)
4578 : : {
4579 : 275 : label = gen_label_rtx ();
4580 : 275 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4581 : : ptr_mode, 1, label,
4582 : : profile_probability::even ());
4583 : : }
4584 : : /* If we are at a bit that is in the prefix shared by min_ and
4585 : : max_len, skip the current BLKSIZE if the bit is clear, but do
4586 : : not skip the loop, even if it doesn't require
4587 : : prechecking. */
4588 : 110 : else if ((max_len & blksize) == 0
4589 : 69 : && !(max_loop && i == max_bits))
4590 : 39 : continue;
4591 : :
4592 : 346 : if (max_loop && i == max_bits)
4593 : : {
4594 : 107 : loop_label = gen_label_rtx ();
4595 : 107 : emit_label (loop_label);
4596 : : /* Since we may run this multiple times, don't assume we
4597 : : know anything about the offset. */
4598 : 107 : clear_mem_offset (to);
4599 : : }
4600 : :
4601 : 346 : bool update_needed = i != sctz_len || loop_label;
4602 : 346 : rtx next_ptr = NULL_RTX;
4603 : 346 : if (!use_store_by_pieces)
4604 : : {
4605 : 0 : gcc_checking_assert (blksize == 1);
4606 : 0 : if (!val)
4607 : 0 : val = gen_int_mode (valc, QImode);
4608 : 0 : to = change_address (to, QImode, 0);
4609 : 0 : emit_move_insn (to, val);
4610 : 0 : if (update_needed)
4611 : 0 : next_ptr = plus_constant (GET_MODE (ptr), ptr, blksize);
4612 : : }
4613 : : else
4614 : : {
4615 : : /* Issue a store of BLKSIZE bytes. */
4616 : 404 : to = store_by_pieces (to, blksize,
4617 : : constfun, constfundata,
4618 : : align, true,
4619 : : update_needed ? RETURN_END : RETURN_BEGIN);
4620 : 346 : next_ptr = XEXP (to, 0);
4621 : : }
4622 : : /* Adjust REM and PTR, unless this is the last iteration. */
4623 : 346 : if (update_needed)
4624 : : {
4625 : 288 : emit_move_insn (ptr, force_operand (next_ptr, NULL_RTX));
4626 : 288 : to = replace_equiv_address (to, ptr);
4627 : 288 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4628 : 288 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4629 : : }
4630 : :
4631 : 288 : if (loop_label)
4632 : 107 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), GE, NULL,
4633 : : ptr_mode, 1, loop_label,
4634 : : profile_probability::likely ());
4635 : :
4636 : 346 : if (label)
4637 : : {
4638 : 275 : emit_label (label);
4639 : :
4640 : : /* Given conditional stores, the offset can no longer be
4641 : : known, so clear it. */
4642 : 275 : clear_mem_offset (to);
4643 : : }
4644 : : }
4645 : :
4646 : : return true;
4647 : : }
4648 : :
4649 : : /* Helper function to do the actual work for expand_builtin_memset. The
4650 : : arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4651 : : so that this can also be called without constructing an actual CALL_EXPR.
4652 : : The other arguments and return value are the same as for
4653 : : expand_builtin_memset. */
4654 : :
4655 : : static rtx
4656 : 35506 : expand_builtin_memset_args (tree dest, tree val, tree len,
4657 : : rtx target, machine_mode mode, tree orig_exp)
4658 : : {
4659 : 35506 : tree fndecl, fn;
4660 : 35506 : enum built_in_function fcode;
4661 : 35506 : machine_mode val_mode;
4662 : 35506 : char c;
4663 : 35506 : unsigned int dest_align;
4664 : 35506 : rtx dest_mem, dest_addr, len_rtx;
4665 : 35506 : HOST_WIDE_INT expected_size = -1;
4666 : 35506 : unsigned int expected_align = 0;
4667 : 35506 : unsigned HOST_WIDE_INT min_size;
4668 : 35506 : unsigned HOST_WIDE_INT max_size;
4669 : 35506 : unsigned HOST_WIDE_INT probable_max_size;
4670 : :
4671 : 35506 : dest_align = get_pointer_alignment (dest);
4672 : :
4673 : : /* If DEST is not a pointer type, don't do this operation in-line. */
4674 : 35506 : if (dest_align == 0)
4675 : : return NULL_RTX;
4676 : :
4677 : 35506 : if (currently_expanding_gimple_stmt)
4678 : 35506 : stringop_block_profile (currently_expanding_gimple_stmt,
4679 : : &expected_align, &expected_size);
4680 : :
4681 : 35506 : if (expected_align < dest_align)
4682 : 35501 : expected_align = dest_align;
4683 : :
4684 : : /* If the LEN parameter is zero, return DEST. */
4685 : 35506 : if (integer_zerop (len))
4686 : : {
4687 : : /* Evaluate and ignore VAL in case it has side-effects. */
4688 : 0 : expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4689 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
4690 : : }
4691 : :
4692 : : /* Stabilize the arguments in case we fail. */
4693 : 35506 : dest = builtin_save_expr (dest);
4694 : 35506 : val = builtin_save_expr (val);
4695 : 35506 : len = builtin_save_expr (len);
4696 : :
4697 : 35506 : len_rtx = expand_normal (len);
4698 : 35506 : determine_block_size (len, len_rtx, &min_size, &max_size,
4699 : : &probable_max_size);
4700 : 35506 : dest_mem = get_memory_rtx (dest, len);
4701 : 35506 : val_mode = TYPE_MODE (unsigned_char_type_node);
4702 : :
4703 : 35506 : if (TREE_CODE (val) != INTEGER_CST
4704 : 35506 : || target_char_cast (val, &c))
4705 : : {
4706 : 1722 : rtx val_rtx;
4707 : :
4708 : 1722 : val_rtx = expand_normal (val);
4709 : 1722 : val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4710 : :
4711 : : /* Assume that we can memset by pieces if we can store
4712 : : * the coefficients by pieces (in the required modes).
4713 : : * We can't pass builtin_memset_gen_str as that emits RTL. */
4714 : 1722 : c = 1;
4715 : 1722 : if (tree_fits_uhwi_p (len)
4716 : 1722 : && can_store_by_pieces (tree_to_uhwi (len),
4717 : : builtin_memset_read_str, &c, dest_align,
4718 : : true))
4719 : : {
4720 : 766 : val_rtx = force_reg (val_mode, val_rtx);
4721 : 766 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4722 : : builtin_memset_gen_str, val_rtx, dest_align,
4723 : : true, RETURN_BEGIN);
4724 : : }
4725 : 956 : else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4726 : : dest_align, expected_align,
4727 : : expected_size, min_size, max_size,
4728 : : probable_max_size)
4729 : 956 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4730 : : tree_ctz (len),
4731 : : min_size, max_size,
4732 : : val_rtx, 0,
4733 : : dest_align))
4734 : 531 : goto do_libcall;
4735 : :
4736 : 1191 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4737 : 1191 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4738 : 1191 : return dest_mem;
4739 : : }
4740 : :
4741 : 33784 : if (c)
4742 : : {
4743 : 12571 : if (tree_fits_uhwi_p (len)
4744 : 12571 : && can_store_by_pieces (tree_to_uhwi (len),
4745 : : builtin_memset_read_str, &c, dest_align,
4746 : : true))
4747 : 4869 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4748 : : builtin_memset_read_str, &c, dest_align, true,
4749 : : RETURN_BEGIN);
4750 : 9337 : else if (!set_storage_via_setmem (dest_mem, len_rtx,
4751 : 7702 : gen_int_mode (c, val_mode),
4752 : : dest_align, expected_align,
4753 : : expected_size, min_size, max_size,
4754 : : probable_max_size)
4755 : 7702 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4756 : : tree_ctz (len),
4757 : : min_size, max_size,
4758 : : NULL_RTX, c,
4759 : : dest_align))
4760 : 6067 : goto do_libcall;
4761 : :
4762 : 6504 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4763 : 6504 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4764 : 6504 : return dest_mem;
4765 : : }
4766 : :
4767 : 21213 : set_mem_align (dest_mem, dest_align);
4768 : 42426 : dest_addr = clear_storage_hints (dest_mem, len_rtx,
4769 : 21213 : CALL_EXPR_TAILCALL (orig_exp)
4770 : : ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4771 : : expected_align, expected_size,
4772 : : min_size, max_size,
4773 : : probable_max_size, tree_ctz (len));
4774 : :
4775 : 21213 : if (dest_addr == 0)
4776 : : {
4777 : 12645 : dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4778 : 12645 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
4779 : : }
4780 : :
4781 : : return dest_addr;
4782 : :
4783 : 6598 : do_libcall:
4784 : 6598 : fndecl = get_callee_fndecl (orig_exp);
4785 : 6598 : fcode = DECL_FUNCTION_CODE (fndecl);
4786 : 6598 : if (fcode == BUILT_IN_MEMSET)
4787 : 6598 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4788 : : dest, val, len);
4789 : 0 : else if (fcode == BUILT_IN_BZERO)
4790 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4791 : : dest, len);
4792 : : else
4793 : 0 : gcc_unreachable ();
4794 : 6598 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4795 : 6598 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4796 : 6598 : return expand_call (fn, target, target == const0_rtx);
4797 : : }
4798 : :
4799 : : /* Expand expression EXP, which is a call to the bzero builtin. Return
4800 : : NULL_RTX if we failed the caller should emit a normal call. */
4801 : :
4802 : : static rtx
4803 : 0 : expand_builtin_bzero (tree exp)
4804 : : {
4805 : 0 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4806 : : return NULL_RTX;
4807 : :
4808 : 0 : tree dest = CALL_EXPR_ARG (exp, 0);
4809 : 0 : tree size = CALL_EXPR_ARG (exp, 1);
4810 : :
4811 : : /* New argument list transforming bzero(ptr x, int y) to
4812 : : memset(ptr x, int 0, size_t y). This is done this way
4813 : : so that if it isn't expanded inline, we fallback to
4814 : : calling bzero instead of memset. */
4815 : :
4816 : 0 : location_t loc = EXPR_LOCATION (exp);
4817 : :
4818 : 0 : return expand_builtin_memset_args (dest, integer_zero_node,
4819 : : fold_convert_loc (loc,
4820 : : size_type_node, size),
4821 : 0 : const0_rtx, VOIDmode, exp);
4822 : : }
4823 : :
4824 : : /* Try to expand cmpstr operation ICODE with the given operands.
4825 : : Return the result rtx on success, otherwise return null. */
4826 : :
4827 : : static rtx
4828 : 0 : expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4829 : : HOST_WIDE_INT align)
4830 : : {
4831 : 0 : machine_mode insn_mode = insn_data[icode].operand[0].mode;
4832 : :
4833 : 0 : if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4834 : : target = NULL_RTX;
4835 : :
4836 : 0 : class expand_operand ops[4];
4837 : 0 : create_output_operand (&ops[0], target, insn_mode);
4838 : 0 : create_fixed_operand (&ops[1], arg1_rtx);
4839 : 0 : create_fixed_operand (&ops[2], arg2_rtx);
4840 : 0 : create_integer_operand (&ops[3], align);
4841 : 0 : if (maybe_expand_insn (icode, 4, ops))
4842 : 0 : return ops[0].value;
4843 : : return NULL_RTX;
4844 : : }
4845 : :
4846 : : /* Expand expression EXP, which is a call to the memcmp built-in function.
4847 : : Return NULL_RTX if we failed and the caller should emit a normal call,
4848 : : otherwise try to get the result in TARGET, if convenient.
4849 : : RESULT_EQ is true if we can relax the returned value to be either zero
4850 : : or nonzero, without caring about the sign. */
4851 : :
4852 : : static rtx
4853 : 111337 : expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4854 : : {
4855 : 111337 : if (!validate_arglist (exp,
4856 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4857 : : return NULL_RTX;
4858 : :
4859 : 111333 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4860 : 111333 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4861 : 111333 : tree len = CALL_EXPR_ARG (exp, 2);
4862 : :
4863 : : /* Due to the performance benefit, always inline the calls first
4864 : : when result_eq is false. */
4865 : 111333 : rtx result = NULL_RTX;
4866 : 111333 : enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4867 : 111333 : if (!result_eq && fcode != BUILT_IN_BCMP)
4868 : : {
4869 : 53283 : result = inline_expand_builtin_bytecmp (exp, target);
4870 : 53283 : if (result)
4871 : : return result;
4872 : : }
4873 : :
4874 : 111308 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4875 : 111308 : location_t loc = EXPR_LOCATION (exp);
4876 : :
4877 : 111308 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4878 : 111308 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4879 : :
4880 : : /* If we don't have POINTER_TYPE, call the function. */
4881 : 111308 : if (arg1_align == 0 || arg2_align == 0)
4882 : : return NULL_RTX;
4883 : :
4884 : 111308 : rtx arg1_rtx = get_memory_rtx (arg1, len);
4885 : 111308 : rtx arg2_rtx = get_memory_rtx (arg2, len);
4886 : 111308 : rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4887 : :
4888 : : /* Set MEM_SIZE as appropriate. */
4889 : 111308 : if (CONST_INT_P (len_rtx))
4890 : : {
4891 : 87563 : set_mem_size (arg1_rtx, INTVAL (len_rtx));
4892 : 87563 : set_mem_size (arg2_rtx, INTVAL (len_rtx));
4893 : : }
4894 : :
4895 : 111308 : by_pieces_constfn constfn = NULL;
4896 : :
4897 : : /* Try to get the byte representation of the constant ARG2 (or, only
4898 : : when the function's result is used for equality to zero, ARG1)
4899 : : points to, with its byte size in NBYTES. */
4900 : 111308 : unsigned HOST_WIDE_INT nbytes;
4901 : 111308 : const char *rep = getbyterep (arg2, &nbytes);
4902 : 111308 : if (result_eq && rep == NULL)
4903 : : {
4904 : : /* For equality to zero the arguments are interchangeable. */
4905 : 35154 : rep = getbyterep (arg1, &nbytes);
4906 : 35154 : if (rep != NULL)
4907 : : std::swap (arg1_rtx, arg2_rtx);
4908 : : }
4909 : :
4910 : : /* If the function's constant bound LEN_RTX is less than or equal
4911 : : to the byte size of the representation of the constant argument,
4912 : : and if block move would be done by pieces, we can avoid loading
4913 : : the bytes from memory and only store the computed constant result. */
4914 : 76154 : if (rep
4915 : 27747 : && CONST_INT_P (len_rtx)
4916 : 27651 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4917 : 111308 : constfn = builtin_memcpy_read_str;
4918 : :
4919 : 222616 : result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4920 : 111308 : TREE_TYPE (len), target,
4921 : : result_eq, constfn,
4922 : : CONST_CAST (char *, rep),
4923 : : tree_ctz (len));
4924 : :
4925 : 111308 : if (result)
4926 : : {
4927 : : /* Return the value in the proper mode for this function. */
4928 : 40292 : if (GET_MODE (result) == mode)
4929 : : return result;
4930 : :
4931 : 6 : if (target != 0)
4932 : : {
4933 : 6 : convert_move (target, result, 0);
4934 : 6 : return target;
4935 : : }
4936 : :
4937 : 0 : return convert_to_mode (mode, result, 0);
4938 : : }
4939 : :
4940 : : return NULL_RTX;
4941 : : }
4942 : :
4943 : : /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4944 : : if we failed the caller should emit a normal call, otherwise try to get
4945 : : the result in TARGET, if convenient. */
4946 : :
4947 : : static rtx
4948 : 127929 : expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4949 : : {
4950 : 127929 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4951 : : return NULL_RTX;
4952 : :
4953 : 127914 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4954 : 127914 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4955 : :
4956 : : /* Due to the performance benefit, always inline the calls first. */
4957 : 127914 : rtx result = NULL_RTX;
4958 : 127914 : result = inline_expand_builtin_bytecmp (exp, target);
4959 : 127914 : if (result)
4960 : : return result;
4961 : :
4962 : 127515 : insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4963 : 127515 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4964 : 127515 : if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4965 : : return NULL_RTX;
4966 : :
4967 : 127515 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4968 : 127515 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4969 : :
4970 : : /* If we don't have POINTER_TYPE, call the function. */
4971 : 127515 : if (arg1_align == 0 || arg2_align == 0)
4972 : : return NULL_RTX;
4973 : :
4974 : : /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4975 : 127515 : arg1 = builtin_save_expr (arg1);
4976 : 127515 : arg2 = builtin_save_expr (arg2);
4977 : :
4978 : 127515 : rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4979 : 127515 : rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4980 : :
4981 : : /* Try to call cmpstrsi. */
4982 : 127515 : if (cmpstr_icode != CODE_FOR_nothing)
4983 : 0 : result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4984 : 0 : MIN (arg1_align, arg2_align));
4985 : :
4986 : : /* Try to determine at least one length and call cmpstrnsi. */
4987 : 127515 : if (!result && cmpstrn_icode != CODE_FOR_nothing)
4988 : : {
4989 : 127515 : tree len;
4990 : 127515 : rtx arg3_rtx;
4991 : :
4992 : 127515 : tree len1 = c_strlen (arg1, 1);
4993 : 127515 : tree len2 = c_strlen (arg2, 1);
4994 : :
4995 : 127515 : if (len1)
4996 : 224 : len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4997 : 127515 : if (len2)
4998 : 125889 : len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4999 : :
5000 : : /* If we don't have a constant length for the first, use the length
5001 : : of the second, if we know it. We don't require a constant for
5002 : : this case; some cost analysis could be done if both are available
5003 : : but neither is constant. For now, assume they're equally cheap,
5004 : : unless one has side effects. If both strings have constant lengths,
5005 : : use the smaller. */
5006 : :
5007 : 127515 : if (!len1)
5008 : : len = len2;
5009 : 224 : else if (!len2)
5010 : : len = len1;
5011 : 17 : else if (TREE_SIDE_EFFECTS (len1))
5012 : : len = len2;
5013 : 17 : else if (TREE_SIDE_EFFECTS (len2))
5014 : : len = len1;
5015 : 17 : else if (TREE_CODE (len1) != INTEGER_CST)
5016 : : len = len2;
5017 : 17 : else if (TREE_CODE (len2) != INTEGER_CST)
5018 : : len = len1;
5019 : 10 : else if (tree_int_cst_lt (len1, len2))
5020 : : len = len1;
5021 : : else
5022 : 127297 : len = len2;
5023 : :
5024 : : /* If both arguments have side effects, we cannot optimize. */
5025 : 127515 : if (len && !TREE_SIDE_EFFECTS (len))
5026 : : {
5027 : 126096 : arg3_rtx = expand_normal (len);
5028 : 126096 : result = expand_cmpstrn_or_cmpmem
5029 : 126096 : (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
5030 : 126096 : arg3_rtx, MIN (arg1_align, arg2_align));
5031 : : }
5032 : : }
5033 : :
5034 : 127515 : tree fndecl = get_callee_fndecl (exp);
5035 : 127515 : if (result)
5036 : : {
5037 : : /* Return the value in the proper mode for this function. */
5038 : 52 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
5039 : 52 : if (GET_MODE (result) == mode)
5040 : : return result;
5041 : 0 : if (target == 0)
5042 : 0 : return convert_to_mode (mode, result, 0);
5043 : 0 : convert_move (target, result, 0);
5044 : 0 : return target;
5045 : : }
5046 : :
5047 : : /* Expand the library call ourselves using a stabilized argument
5048 : : list to avoid re-evaluating the function's arguments twice. */
5049 : 127463 : tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
5050 : 127463 : copy_warning (fn, exp);
5051 : 127463 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5052 : 127463 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5053 : 127463 : return expand_call (fn, target, target == const0_rtx);
5054 : : }
5055 : :
5056 : : /* Expand expression EXP, which is a call to the strncmp builtin. Return
5057 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
5058 : : try to get the result in TARGET, if convenient. */
5059 : :
5060 : : static rtx
5061 : 2029 : expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
5062 : : ATTRIBUTE_UNUSED machine_mode mode)
5063 : : {
5064 : 2029 : if (!validate_arglist (exp,
5065 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5066 : : return NULL_RTX;
5067 : :
5068 : 2023 : tree arg1 = CALL_EXPR_ARG (exp, 0);
5069 : 2023 : tree arg2 = CALL_EXPR_ARG (exp, 1);
5070 : 2023 : tree arg3 = CALL_EXPR_ARG (exp, 2);
5071 : :
5072 : 2023 : location_t loc = EXPR_LOCATION (exp);
5073 : 2023 : tree len1 = c_strlen (arg1, 1);
5074 : 2023 : tree len2 = c_strlen (arg2, 1);
5075 : :
5076 : : /* Due to the performance benefit, always inline the calls first. */
5077 : 2023 : rtx result = NULL_RTX;
5078 : 2023 : result = inline_expand_builtin_bytecmp (exp, target);
5079 : 2023 : if (result)
5080 : : return result;
5081 : :
5082 : : /* If c_strlen can determine an expression for one of the string
5083 : : lengths, and it doesn't have side effects, then emit cmpstrnsi
5084 : : using length MIN(strlen(string)+1, arg3). */
5085 : 1805 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
5086 : 1805 : if (cmpstrn_icode == CODE_FOR_nothing)
5087 : : return NULL_RTX;
5088 : :
5089 : 1805 : tree len;
5090 : :
5091 : 1805 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
5092 : 1805 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
5093 : :
5094 : 1805 : if (len1)
5095 : 160 : len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
5096 : 1805 : if (len2)
5097 : 676 : len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
5098 : :
5099 : 1805 : tree len3 = fold_convert_loc (loc, sizetype, arg3);
5100 : :
5101 : : /* If we don't have a constant length for the first, use the length
5102 : : of the second, if we know it. If neither string is constant length,
5103 : : use the given length argument. We don't require a constant for
5104 : : this case; some cost analysis could be done if both are available
5105 : : but neither is constant. For now, assume they're equally cheap,
5106 : : unless one has side effects. If both strings have constant lengths,
5107 : : use the smaller. */
5108 : :
5109 : 1805 : if (!len1 && !len2)
5110 : : len = len3;
5111 : 719 : else if (!len1)
5112 : : len = len2;
5113 : 160 : else if (!len2)
5114 : : len = len1;
5115 : 117 : else if (TREE_SIDE_EFFECTS (len1))
5116 : : len = len2;
5117 : 117 : else if (TREE_SIDE_EFFECTS (len2))
5118 : : len = len1;
5119 : 117 : else if (TREE_CODE (len1) != INTEGER_CST)
5120 : : len = len2;
5121 : 117 : else if (TREE_CODE (len2) != INTEGER_CST)
5122 : : len = len1;
5123 : 117 : else if (tree_int_cst_lt (len1, len2))
5124 : : len = len1;
5125 : : else
5126 : 560 : len = len2;
5127 : :
5128 : : /* If we are not using the given length, we must incorporate it here.
5129 : : The actual new length parameter will be MIN(len,arg3) in this case. */
5130 : 1805 : if (len != len3)
5131 : : {
5132 : 719 : len = fold_convert_loc (loc, sizetype, len);
5133 : 719 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
5134 : : }
5135 : 1805 : rtx arg1_rtx = get_memory_rtx (arg1, len);
5136 : 1805 : rtx arg2_rtx = get_memory_rtx (arg2, len);
5137 : 1805 : rtx arg3_rtx = expand_normal (len);
5138 : 1805 : result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
5139 : 1805 : arg2_rtx, TREE_TYPE (len), arg3_rtx,
5140 : 1805 : MIN (arg1_align, arg2_align));
5141 : :
5142 : 1805 : tree fndecl = get_callee_fndecl (exp);
5143 : 1805 : if (result)
5144 : : {
5145 : : /* Return the value in the proper mode for this function. */
5146 : 21 : mode = TYPE_MODE (TREE_TYPE (exp));
5147 : 21 : if (GET_MODE (result) == mode)
5148 : : return result;
5149 : 0 : if (target == 0)
5150 : 0 : return convert_to_mode (mode, result, 0);
5151 : 0 : convert_move (target, result, 0);
5152 : 0 : return target;
5153 : : }
5154 : :
5155 : : /* Expand the library call ourselves using a stabilized argument
5156 : : list to avoid re-evaluating the function's arguments twice. */
5157 : 1784 : tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
5158 : 1784 : copy_warning (call, exp);
5159 : 1784 : gcc_assert (TREE_CODE (call) == CALL_EXPR);
5160 : 1784 : CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
5161 : 1784 : return expand_call (call, target, target == const0_rtx);
5162 : : }
5163 : :
5164 : : /* Expand a call to __builtin_saveregs, generating the result in TARGET,
5165 : : if that's convenient. */
5166 : :
5167 : : rtx
5168 : 0 : expand_builtin_saveregs (void)
5169 : : {
5170 : 0 : rtx val;
5171 : 0 : rtx_insn *seq;
5172 : :
5173 : : /* Don't do __builtin_saveregs more than once in a function.
5174 : : Save the result of the first call and reuse it. */
5175 : 0 : if (saveregs_value != 0)
5176 : : return saveregs_value;
5177 : :
5178 : : /* When this function is called, it means that registers must be
5179 : : saved on entry to this function. So we migrate the call to the
5180 : : first insn of this function. */
5181 : :
5182 : 0 : start_sequence ();
5183 : :
5184 : : /* Do whatever the machine needs done in this case. */
5185 : 0 : val = targetm.calls.expand_builtin_saveregs ();
5186 : :
5187 : 0 : seq = end_sequence ();
5188 : :
5189 : 0 : saveregs_value = val;
5190 : :
5191 : : /* Put the insns after the NOTE that starts the function. If this
5192 : : is inside a start_sequence, make the outer-level insn chain current, so
5193 : : the code is placed at the start of the function. */
5194 : 0 : push_topmost_sequence ();
5195 : 0 : emit_insn_after (seq, entry_of_function ());
5196 : 0 : pop_topmost_sequence ();
5197 : :
5198 : 0 : return val;
5199 : : }
5200 : :
5201 : : /* Expand a call to __builtin_next_arg. */
5202 : :
5203 : : static rtx
5204 : 21025 : expand_builtin_next_arg (void)
5205 : : {
5206 : : /* Checking arguments is already done in fold_builtin_next_arg
5207 : : that must be called before this function. */
5208 : 21025 : return expand_binop (ptr_mode, add_optab,
5209 : : crtl->args.internal_arg_pointer,
5210 : : crtl->args.arg_offset_rtx,
5211 : 21025 : NULL_RTX, 0, OPTAB_LIB_WIDEN);
5212 : : }
5213 : :
5214 : : /* Make it easier for the backends by protecting the valist argument
5215 : : from multiple evaluations. */
5216 : :
5217 : : static tree
5218 : 21359 : stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5219 : : {
5220 : 21359 : tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5221 : :
5222 : : /* The current way of determining the type of valist is completely
5223 : : bogus. We should have the information on the va builtin instead. */
5224 : 21359 : if (!vatype)
5225 : 21235 : vatype = targetm.fn_abi_va_list (cfun->decl);
5226 : :
5227 : 21359 : if (TREE_CODE (vatype) == ARRAY_TYPE)
5228 : : {
5229 : 15697 : if (TREE_SIDE_EFFECTS (valist))
5230 : 0 : valist = save_expr (valist);
5231 : :
5232 : : /* For this case, the backends will be expecting a pointer to
5233 : : vatype, but it's possible we've actually been given an array
5234 : : (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5235 : : So fix it. */
5236 : 15697 : if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5237 : : {
5238 : 0 : tree p1 = build_pointer_type (TREE_TYPE (vatype));
5239 : 0 : valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5240 : : }
5241 : : }
5242 : : else
5243 : : {
5244 : 5662 : tree pt = build_pointer_type (vatype);
5245 : :
5246 : 5662 : if (! needs_lvalue)
5247 : : {
5248 : 6 : if (! TREE_SIDE_EFFECTS (valist))
5249 : : return valist;
5250 : :
5251 : 0 : valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5252 : 0 : TREE_SIDE_EFFECTS (valist) = 1;
5253 : : }
5254 : :
5255 : 5656 : if (TREE_SIDE_EFFECTS (valist))
5256 : 0 : valist = save_expr (valist);
5257 : 5656 : valist = fold_build2_loc (loc, MEM_REF,
5258 : : vatype, valist, build_int_cst (pt, 0));
5259 : : }
5260 : :
5261 : : return valist;
5262 : : }
5263 : :
5264 : : /* The "standard" definition of va_list is void*. */
5265 : :
5266 : : tree
5267 : 0 : std_build_builtin_va_list (void)
5268 : : {
5269 : 0 : return ptr_type_node;
5270 : : }
5271 : :
5272 : : /* The "standard" abi va_list is va_list_type_node. */
5273 : :
5274 : : tree
5275 : 0 : std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5276 : : {
5277 : 0 : return va_list_type_node;
5278 : : }
5279 : :
5280 : : /* The "standard" type of va_list is va_list_type_node. */
5281 : :
5282 : : tree
5283 : 502 : std_canonical_va_list_type (tree type)
5284 : : {
5285 : 502 : tree wtype, htype;
5286 : :
5287 : 502 : wtype = va_list_type_node;
5288 : 502 : htype = type;
5289 : :
5290 : 502 : if (TREE_CODE (wtype) == ARRAY_TYPE)
5291 : : {
5292 : : /* If va_list is an array type, the argument may have decayed
5293 : : to a pointer type, e.g. by being passed to another function.
5294 : : In that case, unwrap both types so that we can compare the
5295 : : underlying records. */
5296 : 0 : if (TREE_CODE (htype) == ARRAY_TYPE
5297 : 0 : || POINTER_TYPE_P (htype))
5298 : : {
5299 : 0 : wtype = TREE_TYPE (wtype);
5300 : 0 : htype = TREE_TYPE (htype);
5301 : : }
5302 : : }
5303 : 502 : if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5304 : 332 : return va_list_type_node;
5305 : :
5306 : : return NULL_TREE;
5307 : : }
5308 : :
5309 : : /* The "standard" implementation of va_start: just assign `nextarg' to
5310 : : the variable. */
5311 : :
5312 : : void
5313 : 5652 : std_expand_builtin_va_start (tree valist, rtx nextarg)
5314 : : {
5315 : 5652 : rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5316 : 5652 : convert_move (va_r, nextarg, 0);
5317 : 5652 : }
5318 : :
5319 : : /* Expand EXP, a call to __builtin_va_start. */
5320 : :
5321 : : static rtx
5322 : 20875 : expand_builtin_va_start (tree exp)
5323 : : {
5324 : 20875 : rtx nextarg;
5325 : 20875 : tree valist;
5326 : 20875 : location_t loc = EXPR_LOCATION (exp);
5327 : :
5328 : 20875 : if (call_expr_nargs (exp) < 2)
5329 : : {
5330 : 0 : error_at (loc, "too few arguments to function %<va_start%>");
5331 : 0 : return const0_rtx;
5332 : : }
5333 : :
5334 : 20875 : if (fold_builtin_next_arg (exp, true))
5335 : 0 : return const0_rtx;
5336 : :
5337 : 20875 : nextarg = expand_builtin_next_arg ();
5338 : 20875 : valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5339 : :
5340 : 20875 : if (targetm.expand_builtin_va_start)
5341 : 20875 : targetm.expand_builtin_va_start (valist, nextarg);
5342 : : else
5343 : 0 : std_expand_builtin_va_start (valist, nextarg);
5344 : :
5345 : 20875 : return const0_rtx;
5346 : : }
5347 : :
5348 : : /* Expand EXP, a call to __builtin_va_end. */
5349 : :
5350 : : static rtx
5351 : 12071 : expand_builtin_va_end (tree exp)
5352 : : {
5353 : 12071 : tree valist = CALL_EXPR_ARG (exp, 0);
5354 : :
5355 : : /* Evaluate for side effects, if needed. I hate macros that don't
5356 : : do that. */
5357 : 12071 : if (TREE_SIDE_EFFECTS (valist))
5358 : 0 : expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5359 : :
5360 : 12071 : return const0_rtx;
5361 : : }
5362 : :
5363 : : /* Expand EXP, a call to __builtin_va_copy. We do this as a
5364 : : builtin rather than just as an assignment in stdarg.h because of the
5365 : : nastiness of array-type va_list types. */
5366 : :
5367 : : static rtx
5368 : 242 : expand_builtin_va_copy (tree exp)
5369 : : {
5370 : 242 : tree dst, src, t;
5371 : 242 : location_t loc = EXPR_LOCATION (exp);
5372 : :
5373 : 242 : dst = CALL_EXPR_ARG (exp, 0);
5374 : 242 : src = CALL_EXPR_ARG (exp, 1);
5375 : :
5376 : 242 : dst = stabilize_va_list_loc (loc, dst, 1);
5377 : 242 : src = stabilize_va_list_loc (loc, src, 0);
5378 : :
5379 : 242 : gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5380 : :
5381 : 242 : if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5382 : : {
5383 : 0 : t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5384 : 0 : TREE_SIDE_EFFECTS (t) = 1;
5385 : 0 : expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5386 : : }
5387 : : else
5388 : : {
5389 : 242 : rtx dstb, srcb, size;
5390 : :
5391 : : /* Evaluate to pointers. */
5392 : 242 : dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5393 : 242 : srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5394 : 242 : size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5395 : : NULL_RTX, VOIDmode, EXPAND_NORMAL);
5396 : :
5397 : 242 : dstb = convert_memory_address (Pmode, dstb);
5398 : 242 : srcb = convert_memory_address (Pmode, srcb);
5399 : :
5400 : : /* "Dereference" to BLKmode memories. */
5401 : 242 : dstb = gen_rtx_MEM (BLKmode, dstb);
5402 : 242 : set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5403 : 242 : set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5404 : 242 : srcb = gen_rtx_MEM (BLKmode, srcb);
5405 : 242 : set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5406 : 242 : set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5407 : :
5408 : : /* Copy. */
5409 : 242 : emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5410 : : }
5411 : :
5412 : 242 : return const0_rtx;
5413 : : }
5414 : :
5415 : : /* Expand a call to one of the builtin functions __builtin_frame_address or
5416 : : __builtin_return_address. */
5417 : :
5418 : : static rtx
5419 : 15582 : expand_builtin_frame_address (tree fndecl, tree exp)
5420 : : {
5421 : : /* The argument must be a nonnegative integer constant.
5422 : : It counts the number of frames to scan up the stack.
5423 : : The value is either the frame pointer value or the return
5424 : : address saved in that frame. */
5425 : 15582 : if (call_expr_nargs (exp) == 0)
5426 : : /* Warning about missing arg was already issued. */
5427 : 0 : return const0_rtx;
5428 : 15582 : else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5429 : : {
5430 : 0 : error ("invalid argument to %qD", fndecl);
5431 : 0 : return const0_rtx;
5432 : : }
5433 : : else
5434 : : {
5435 : : /* Number of frames to scan up the stack. */
5436 : 15582 : unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5437 : :
5438 : 15582 : rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5439 : :
5440 : : /* Some ports cannot access arbitrary stack frames. */
5441 : 15582 : if (tem == NULL)
5442 : : {
5443 : 0 : warning (0, "unsupported argument to %qD", fndecl);
5444 : 0 : return const0_rtx;
5445 : : }
5446 : :
5447 : 15582 : if (count)
5448 : : {
5449 : : /* Warn since no effort is made to ensure that any frame
5450 : : beyond the current one exists or can be safely reached. */
5451 : 932 : warning (OPT_Wframe_address, "calling %qD with "
5452 : : "a nonzero argument is unsafe", fndecl);
5453 : : }
5454 : :
5455 : : /* For __builtin_frame_address, return what we've got. */
5456 : 15582 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5457 : : return tem;
5458 : :
5459 : 5085 : if (!REG_P (tem)
5460 : 5085 : && ! CONSTANT_P (tem))
5461 : 5085 : tem = copy_addr_to_reg (tem);
5462 : 5085 : return tem;
5463 : : }
5464 : : }
5465 : :
5466 : : #if ! STACK_GROWS_DOWNWARD
5467 : : # define STACK_TOPS GT
5468 : : #else
5469 : : # define STACK_TOPS LT
5470 : : #endif
5471 : :
5472 : : #ifdef POINTERS_EXTEND_UNSIGNED
5473 : : # define STACK_UNSIGNED POINTERS_EXTEND_UNSIGNED
5474 : : #else
5475 : : # define STACK_UNSIGNED true
5476 : : #endif
5477 : :
5478 : : /* Expand a call to builtin function __builtin_stack_address. */
5479 : :
5480 : : static rtx
5481 : 3427 : expand_builtin_stack_address ()
5482 : : {
5483 : 3427 : rtx ret = convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
5484 : : STACK_UNSIGNED);
5485 : :
5486 : : #ifdef STACK_ADDRESS_OFFSET
5487 : : /* Unbias the stack pointer, bringing it to the boundary between the
5488 : : stack area claimed by the active function calling this builtin,
5489 : : and stack ranges that could get clobbered if it called another
5490 : : function. It should NOT encompass any stack red zone, that is
5491 : : used in leaf functions.
5492 : :
5493 : : On SPARC, the register save area is *not* considered active or
5494 : : used by the active function, but rather as akin to the area in
5495 : : which call-preserved registers are saved by callees. This
5496 : : enables __strub_leave to clear what would otherwise overlap with
5497 : : its own register save area.
5498 : :
5499 : : If the address is computed too high or too low, parts of a stack
5500 : : range that should be scrubbed may be left unscrubbed, scrubbing
5501 : : may corrupt active portions of the stack frame, and stack ranges
5502 : : may be doubly-scrubbed by caller and callee.
5503 : :
5504 : : In order for it to be just right, the area delimited by
5505 : : @code{__builtin_stack_address} and @code{__builtin_frame_address
5506 : : (0)} should encompass caller's registers saved by the function,
5507 : : local on-stack variables and @code{alloca} stack areas.
5508 : : Accumulated outgoing on-stack arguments, preallocated as part of
5509 : : a function's own prologue, are to be regarded as part of the
5510 : : (caller) function's active area as well, whereas those pushed or
5511 : : allocated temporarily for a call are regarded as part of the
5512 : : callee's stack range, rather than the caller's. */
5513 : : ret = plus_constant (ptr_mode, ret, STACK_ADDRESS_OFFSET);
5514 : : #endif
5515 : :
5516 : 3427 : return force_reg (ptr_mode, ret);
5517 : : }
5518 : :
5519 : : /* Expand a call to builtin function __builtin_strub_enter. */
5520 : :
5521 : : static rtx
5522 : 2159 : expand_builtin_strub_enter (tree exp)
5523 : : {
5524 : 2159 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5525 : : return NULL_RTX;
5526 : :
5527 : 2159 : if (optimize < 1 || flag_no_inline)
5528 : : return NULL_RTX;
5529 : :
5530 : 1515 : rtx stktop = expand_builtin_stack_address ();
5531 : :
5532 : 1515 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5533 : 1515 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5534 : 1515 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5535 : : build_int_cst (TREE_TYPE (wmptr), 0));
5536 : 1515 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5537 : :
5538 : 1515 : emit_move_insn (wmark, stktop);
5539 : :
5540 : 1515 : return const0_rtx;
5541 : : }
5542 : :
5543 : : /* Expand a call to builtin function __builtin_strub_update. */
5544 : :
5545 : : static rtx
5546 : 1072 : expand_builtin_strub_update (tree exp)
5547 : : {
5548 : 1072 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5549 : : return NULL_RTX;
5550 : :
5551 : 1072 : if (optimize < 2 || flag_no_inline)
5552 : : return NULL_RTX;
5553 : :
5554 : 652 : rtx stktop = expand_builtin_stack_address ();
5555 : :
5556 : : #ifdef RED_ZONE_SIZE
5557 : : /* Here's how the strub enter, update and leave functions deal with red zones.
5558 : :
5559 : : If it weren't for red zones, update, called from within a strub context,
5560 : : would bump the watermark to the top of the stack. Enter and leave, running
5561 : : in the caller, would use the caller's top of stack address both to
5562 : : initialize the watermark passed to the callee, and to start strubbing the
5563 : : stack afterwards.
5564 : :
5565 : : Ideally, we'd update the watermark so as to cover the used amount of red
5566 : : zone, and strub starting at the caller's other end of the (presumably
5567 : : unused) red zone. Normally, only leaf functions use the red zone, but at
5568 : : this point we can't tell whether a function is a leaf, nor can we tell how
5569 : : much of the red zone it uses. Furthermore, some strub contexts may have
5570 : : been inlined so that update and leave are called from the same stack frame,
5571 : : and the strub builtins may all have been inlined, turning a strub function
5572 : : into a leaf.
5573 : :
5574 : : So cleaning the range from the caller's stack pointer (one end of the red
5575 : : zone) to the (potentially inlined) callee's (other end of the) red zone
5576 : : could scribble over the caller's own red zone.
5577 : :
5578 : : We avoid this possibility by arranging for callers that are strub contexts
5579 : : to use their own watermark as the strub starting point. So, if A calls B,
5580 : : and B calls C, B will tell A to strub up to the end of B's red zone, and
5581 : : will strub itself only the part of C's stack frame and red zone that
5582 : : doesn't overlap with B's. With that, we don't need to know who's leaf and
5583 : : who isn't: inlined calls will shrink their strub window to zero, each
5584 : : remaining call will strub some portion of the stack, and eventually the
5585 : : strub context will return to a caller that isn't a strub context itself,
5586 : : that will therefore use its own stack pointer as the strub starting point.
5587 : : It's not a leaf, because strub contexts can't be inlined into non-strub
5588 : : contexts, so it doesn't use the red zone, and it will therefore correctly
5589 : : strub up the callee's stack frame up to the end of the callee's red zone.
5590 : : Neat! */
5591 : 652 : if (true /* (flags_from_decl_or_type (current_function_decl) & ECF_LEAF) */)
5592 : : {
5593 : 652 : poly_int64 red_zone_size = RED_ZONE_SIZE;
5594 : : #if STACK_GROWS_DOWNWARD
5595 : 652 : red_zone_size = -red_zone_size;
5596 : : #endif
5597 : 652 : stktop = plus_constant (ptr_mode, stktop, red_zone_size);
5598 : 652 : stktop = force_reg (ptr_mode, stktop);
5599 : : }
5600 : : #endif
5601 : :
5602 : 652 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5603 : 652 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5604 : 652 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5605 : : build_int_cst (TREE_TYPE (wmptr), 0));
5606 : 652 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5607 : :
5608 : 652 : rtx wmarkr = force_reg (ptr_mode, wmark);
5609 : :
5610 : 652 : rtx_code_label *lab = gen_label_rtx ();
5611 : 652 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5612 : : ptr_mode, NULL_RTX, lab, NULL,
5613 : : profile_probability::very_likely ());
5614 : 652 : emit_move_insn (wmark, stktop);
5615 : :
5616 : : /* If this is an inlined strub function, also bump the watermark for the
5617 : : enclosing function. This avoids a problem with the following scenario: A
5618 : : calls B and B calls C, and both B and C get inlined into A. B allocates
5619 : : temporary stack space before calling C. If we don't update A's watermark,
5620 : : we may use an outdated baseline for the post-C strub_leave, erasing B's
5621 : : temporary stack allocation. We only need this if we're fully expanding
5622 : : strub_leave inline. */
5623 : 652 : tree xwmptr = (optimize > 2
5624 : 652 : ? strub_watermark_parm (current_function_decl)
5625 : : : wmptr);
5626 : 652 : if (wmptr != xwmptr)
5627 : : {
5628 : 156 : wmptr = xwmptr;
5629 : 156 : wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5630 : 156 : wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5631 : : build_int_cst (TREE_TYPE (wmptr), 0));
5632 : 156 : wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5633 : 156 : wmarkr = force_reg (ptr_mode, wmark);
5634 : :
5635 : 156 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5636 : : ptr_mode, NULL_RTX, lab, NULL,
5637 : : profile_probability::very_likely ());
5638 : 156 : emit_move_insn (wmark, stktop);
5639 : : }
5640 : :
5641 : 652 : emit_label (lab);
5642 : :
5643 : 652 : return const0_rtx;
5644 : : }
5645 : :
5646 : :
5647 : : /* Expand a call to builtin function __builtin_strub_leave. */
5648 : :
5649 : : static rtx
5650 : 2729 : expand_builtin_strub_leave (tree exp)
5651 : : {
5652 : 2729 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5653 : : return NULL_RTX;
5654 : :
5655 : 2729 : if (optimize < 2 || optimize_size || flag_no_inline)
5656 : : return NULL_RTX;
5657 : :
5658 : 1229 : rtx stktop = NULL_RTX;
5659 : :
5660 : 1229 : if (tree wmptr = (optimize
5661 : 1229 : ? strub_watermark_parm (current_function_decl)
5662 : : : NULL_TREE))
5663 : : {
5664 : 509 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5665 : 509 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5666 : : build_int_cst (TREE_TYPE (wmptr), 0));
5667 : 509 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5668 : 509 : stktop = force_reg (ptr_mode, wmark);
5669 : : }
5670 : :
5671 : 509 : if (!stktop)
5672 : 720 : stktop = expand_builtin_stack_address ();
5673 : :
5674 : 1229 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5675 : 1229 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5676 : 1229 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5677 : : build_int_cst (TREE_TYPE (wmptr), 0));
5678 : 1229 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5679 : :
5680 : 1229 : rtx wmarkr = force_reg (ptr_mode, wmark);
5681 : :
5682 : : #if ! STACK_GROWS_DOWNWARD
5683 : : rtx base = stktop;
5684 : : rtx end = wmarkr;
5685 : : #else
5686 : 1229 : rtx base = wmarkr;
5687 : 1229 : rtx end = stktop;
5688 : : #endif
5689 : :
5690 : : /* We're going to modify it, so make sure it's not e.g. the stack pointer. */
5691 : 1229 : base = copy_to_reg (base);
5692 : :
5693 : 1229 : rtx_code_label *done = gen_label_rtx ();
5694 : 1229 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5695 : : ptr_mode, NULL_RTX, done, NULL,
5696 : : profile_probability::very_likely ());
5697 : :
5698 : 1229 : if (optimize < 3)
5699 : 909 : expand_call (exp, NULL_RTX, true);
5700 : : else
5701 : : {
5702 : : /* Ok, now we've determined we want to copy the block, so convert the
5703 : : addresses to Pmode, as needed to dereference them to access ptr_mode
5704 : : memory locations, so that we don't have to convert anything within the
5705 : : loop. */
5706 : 320 : base = memory_address (ptr_mode, base);
5707 : 320 : end = memory_address (ptr_mode, end);
5708 : :
5709 : 320 : rtx zero = force_operand (const0_rtx, NULL_RTX);
5710 : 320 : int ulen = GET_MODE_SIZE (ptr_mode);
5711 : :
5712 : : /* ??? It would be nice to use setmem or similar patterns here,
5713 : : but they do not necessarily obey the stack growth direction,
5714 : : which has security implications. We also have to avoid calls
5715 : : (memset, bzero or any machine-specific ones), which are
5716 : : likely unsafe here (see TARGET_STRUB_MAY_USE_MEMSET). */
5717 : : #if ! STACK_GROWS_DOWNWARD
5718 : : rtx incr = plus_constant (Pmode, base, ulen);
5719 : : rtx dstm = gen_rtx_MEM (ptr_mode, base);
5720 : :
5721 : : rtx_code_label *loop = gen_label_rtx ();
5722 : : emit_label (loop);
5723 : : emit_move_insn (dstm, zero);
5724 : : emit_move_insn (base, force_operand (incr, NULL_RTX));
5725 : : #else
5726 : 320 : rtx decr = plus_constant (Pmode, end, -ulen);
5727 : 320 : rtx dstm = gen_rtx_MEM (ptr_mode, end);
5728 : :
5729 : 320 : rtx_code_label *loop = gen_label_rtx ();
5730 : 320 : emit_label (loop);
5731 : 320 : emit_move_insn (end, force_operand (decr, NULL_RTX));
5732 : 320 : emit_move_insn (dstm, zero);
5733 : : #endif
5734 : 640 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5735 : 320 : Pmode, NULL_RTX, NULL, loop,
5736 : : profile_probability::very_likely ());
5737 : : }
5738 : :
5739 : 1229 : emit_label (done);
5740 : :
5741 : 1229 : return const0_rtx;
5742 : : }
5743 : :
5744 : : /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5745 : : failed and the caller should emit a normal call. */
5746 : :
5747 : : static rtx
5748 : 25653 : expand_builtin_alloca (tree exp)
5749 : : {
5750 : 25653 : rtx op0;
5751 : 25653 : rtx result;
5752 : 25653 : unsigned int align;
5753 : 25653 : tree fndecl = get_callee_fndecl (exp);
5754 : 25653 : HOST_WIDE_INT max_size;
5755 : 25653 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5756 : 25653 : bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5757 : 25653 : bool valid_arglist
5758 : : = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5759 : 25653 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5760 : : VOID_TYPE)
5761 : : : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5762 : 25651 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5763 : 25653 : : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5764 : :
5765 : 25653 : if (!valid_arglist)
5766 : : return NULL_RTX;
5767 : :
5768 : : /* Compute the argument. */
5769 : 25648 : op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5770 : :
5771 : : /* Compute the alignment. */
5772 : 47485 : align = (fcode == BUILT_IN_ALLOCA
5773 : 21838 : ? BIGGEST_ALIGNMENT
5774 : 3810 : : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5775 : :
5776 : : /* Compute the maximum size. */
5777 : 2 : max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5778 : 25650 : ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5779 : : : -1);
5780 : :
5781 : : /* Allocate the desired space. If the allocation stems from the declaration
5782 : : of a variable-sized object, it cannot accumulate. */
5783 : 25648 : result
5784 : 25648 : = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5785 : 25648 : result = convert_memory_address (ptr_mode, result);
5786 : :
5787 : : /* Dynamic allocations for variables are recorded during gimplification. */
5788 : 25648 : if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5789 : 0 : record_dynamic_alloc (exp);
5790 : :
5791 : : return result;
5792 : : }
5793 : :
5794 : : /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5795 : : of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5796 : : STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5797 : : handle_builtin_stack_restore function. */
5798 : :
5799 : : static rtx
5800 : 203 : expand_asan_emit_allocas_unpoison (tree exp)
5801 : : {
5802 : 203 : tree arg0 = CALL_EXPR_ARG (exp, 0);
5803 : 203 : tree arg1 = CALL_EXPR_ARG (exp, 1);
5804 : 203 : rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5805 : 203 : rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5806 : 203 : rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5807 : : stack_pointer_rtx, NULL_RTX, 0,
5808 : : OPTAB_LIB_WIDEN);
5809 : 203 : off = convert_modes (ptr_mode, Pmode, off, 0);
5810 : 203 : bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5811 : : OPTAB_LIB_WIDEN);
5812 : 203 : rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5813 : 203 : ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5814 : : top, ptr_mode, bot, ptr_mode);
5815 : 203 : return ret;
5816 : : }
5817 : :
5818 : : /* Expand a call to bswap builtin in EXP.
5819 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
5820 : : function in-line. If convenient, the result should be placed in TARGET.
5821 : : SUBTARGET may be used as the target for computing one of EXP's operands. */
5822 : :
5823 : : static rtx
5824 : 1164 : expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5825 : : rtx subtarget)
5826 : : {
5827 : 1164 : tree arg;
5828 : 1164 : rtx op0;
5829 : :
5830 : 1164 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5831 : : return NULL_RTX;
5832 : :
5833 : 1164 : arg = CALL_EXPR_ARG (exp, 0);
5834 : 1164 : op0 = expand_expr (arg,
5835 : 50 : subtarget && GET_MODE (subtarget) == target_mode
5836 : : ? subtarget : NULL_RTX,
5837 : : target_mode, EXPAND_NORMAL);
5838 : 1164 : if (GET_MODE (op0) != target_mode)
5839 : 0 : op0 = convert_to_mode (target_mode, op0, 1);
5840 : :
5841 : 1164 : target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5842 : :
5843 : 1164 : gcc_assert (target);
5844 : :
5845 : 1164 : return convert_to_mode (target_mode, target, 1);
5846 : : }
5847 : :
5848 : : /* Expand a call to a unary builtin in EXP.
5849 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
5850 : : function in-line. If convenient, the result should be placed in TARGET.
5851 : : SUBTARGET may be used as the target for computing one of EXP's operands. */
5852 : :
5853 : : static rtx
5854 : 751 : expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5855 : : rtx subtarget, optab op_optab)
5856 : : {
5857 : 751 : rtx op0;
5858 : :
5859 : 751 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5860 : : return NULL_RTX;
5861 : :
5862 : : /* Compute the argument. */
5863 : 1502 : op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5864 : : (subtarget
5865 : 94 : && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5866 : 94 : == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5867 : : VOIDmode, EXPAND_NORMAL);
5868 : : /* Compute op, into TARGET if possible.
5869 : : Set TARGET to wherever the result comes back. */
5870 : 751 : target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5871 : : op_optab, op0, target, op_optab != clrsb_optab);
5872 : 751 : gcc_assert (target);
5873 : :
5874 : 751 : return convert_to_mode (target_mode, target, 0);
5875 : : }
5876 : :
5877 : : /* Expand a call to __builtin_expect. We just return our argument
5878 : : as the builtin_expect semantic should've been already executed by
5879 : : tree branch prediction pass. */
5880 : :
5881 : : static rtx
5882 : 1035 : expand_builtin_expect (tree exp, rtx target)
5883 : : {
5884 : 1035 : tree arg;
5885 : :
5886 : 1035 : if (call_expr_nargs (exp) < 2)
5887 : 0 : return const0_rtx;
5888 : 1035 : arg = CALL_EXPR_ARG (exp, 0);
5889 : :
5890 : 1035 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5891 : : /* When guessing was done, the hints should be already stripped away. */
5892 : 1035 : gcc_assert (!flag_guess_branch_prob
5893 : : || optimize == 0 || seen_error ());
5894 : : return target;
5895 : : }
5896 : :
5897 : : /* Expand a call to __builtin_expect_with_probability. We just return our
5898 : : argument as the builtin_expect semantic should've been already executed by
5899 : : tree branch prediction pass. */
5900 : :
5901 : : static rtx
5902 : 5 : expand_builtin_expect_with_probability (tree exp, rtx target)
5903 : : {
5904 : 5 : tree arg;
5905 : :
5906 : 5 : if (call_expr_nargs (exp) < 3)
5907 : 0 : return const0_rtx;
5908 : 5 : arg = CALL_EXPR_ARG (exp, 0);
5909 : :
5910 : 5 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5911 : : /* When guessing was done, the hints should be already stripped away. */
5912 : 5 : gcc_assert (!flag_guess_branch_prob
5913 : : || optimize == 0 || seen_error ());
5914 : : return target;
5915 : : }
5916 : :
5917 : :
5918 : : /* Expand a call to __builtin_assume_aligned. We just return our first
5919 : : argument as the builtin_assume_aligned semantic should've been already
5920 : : executed by CCP. */
5921 : :
5922 : : static rtx
5923 : 75 : expand_builtin_assume_aligned (tree exp, rtx target)
5924 : : {
5925 : 75 : if (call_expr_nargs (exp) < 2)
5926 : 0 : return const0_rtx;
5927 : 75 : target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5928 : : EXPAND_NORMAL);
5929 : 75 : gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5930 : : && (call_expr_nargs (exp) < 3
5931 : : || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5932 : : return target;
5933 : : }
5934 : :
5935 : : void
5936 : 36177 : expand_builtin_trap (void)
5937 : : {
5938 : 36177 : if (targetm.have_trap ())
5939 : : {
5940 : 36177 : rtx_insn *insn = emit_insn (targetm.gen_trap ());
5941 : : /* For trap insns when not accumulating outgoing args force
5942 : : REG_ARGS_SIZE note to prevent crossjumping of calls with
5943 : : different args sizes. */
5944 : 36177 : if (!ACCUMULATE_OUTGOING_ARGS)
5945 : 36176 : add_args_size_note (insn, stack_pointer_delta);
5946 : : }
5947 : : else
5948 : : {
5949 : 0 : tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5950 : 0 : tree call_expr = build_call_expr (fn, 0);
5951 : 0 : expand_call (call_expr, NULL_RTX, false);
5952 : : }
5953 : :
5954 : 36177 : emit_barrier ();
5955 : 36177 : }
5956 : :
5957 : : /* Expand a call to __builtin_unreachable. We do nothing except emit
5958 : : a barrier saying that control flow will not pass here.
5959 : :
5960 : : It is the responsibility of the program being compiled to ensure
5961 : : that control flow does never reach __builtin_unreachable. */
5962 : : static void
5963 : 5602 : expand_builtin_unreachable (void)
5964 : : {
5965 : : /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5966 : : to avoid this. */
5967 : 5602 : gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5968 : 5602 : emit_barrier ();
5969 : 5602 : }
5970 : :
5971 : : /* Expand EXP, a call to fabs, fabsf or fabsl.
5972 : : Return NULL_RTX if a normal call should be emitted rather than expanding
5973 : : the function inline. If convenient, the result should be placed
5974 : : in TARGET. SUBTARGET may be used as the target for computing
5975 : : the operand. */
5976 : :
5977 : : static rtx
5978 : 4 : expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5979 : : {
5980 : 4 : machine_mode mode;
5981 : 4 : tree arg;
5982 : 4 : rtx op0;
5983 : :
5984 : 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5985 : : return NULL_RTX;
5986 : :
5987 : 0 : arg = CALL_EXPR_ARG (exp, 0);
5988 : 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5989 : 0 : mode = TYPE_MODE (TREE_TYPE (arg));
5990 : 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5991 : 0 : return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5992 : : }
5993 : :
5994 : : /* Expand EXP, a call to copysign, copysignf, or copysignl.
5995 : : Return NULL is a normal call should be emitted rather than expanding the
5996 : : function inline. If convenient, the result should be placed in TARGET.
5997 : : SUBTARGET may be used as the target for computing the operand. */
5998 : :
5999 : : static rtx
6000 : 11674 : expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
6001 : : {
6002 : 11674 : rtx op0, op1;
6003 : 11674 : tree arg;
6004 : :
6005 : 11674 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6006 : : return NULL_RTX;
6007 : :
6008 : 11673 : arg = CALL_EXPR_ARG (exp, 0);
6009 : 11673 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6010 : :
6011 : 11673 : arg = CALL_EXPR_ARG (exp, 1);
6012 : 11673 : op1 = expand_normal (arg);
6013 : :
6014 : 11673 : return expand_copysign (op0, op1, target);
6015 : : }
6016 : :
6017 : : /* Emit a call to __builtin___clear_cache. */
6018 : :
6019 : : void
6020 : 0 : default_emit_call_builtin___clear_cache (rtx begin, rtx end)
6021 : : {
6022 : 0 : rtx callee = gen_rtx_SYMBOL_REF (Pmode,
6023 : : BUILTIN_ASM_NAME_PTR
6024 : : (BUILT_IN_CLEAR_CACHE));
6025 : :
6026 : 0 : emit_library_call (callee,
6027 : : LCT_NORMAL, VOIDmode,
6028 : : convert_memory_address (ptr_mode, begin), ptr_mode,
6029 : : convert_memory_address (ptr_mode, end), ptr_mode);
6030 : 0 : }
6031 : :
6032 : : /* Emit a call to __builtin___clear_cache, unless the target specifies
6033 : : it as do-nothing. This function can be used by trampoline
6034 : : finalizers to duplicate the effects of expanding a call to the
6035 : : clear_cache builtin. */
6036 : :
6037 : : void
6038 : 28 : maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
6039 : : {
6040 : 28 : gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
6041 : : || CONST_INT_P (begin))
6042 : : && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
6043 : : || CONST_INT_P (end)));
6044 : :
6045 : 28 : if (targetm.have_clear_cache ())
6046 : : {
6047 : : /* We have a "clear_cache" insn, and it will handle everything. */
6048 : 0 : class expand_operand ops[2];
6049 : :
6050 : 0 : create_address_operand (&ops[0], begin);
6051 : 0 : create_address_operand (&ops[1], end);
6052 : :
6053 : 0 : if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
6054 : 0 : return;
6055 : : }
6056 : : else
6057 : : {
6058 : : #ifndef CLEAR_INSN_CACHE
6059 : : /* There is no "clear_cache" insn, and __clear_cache() in libgcc
6060 : : does nothing. There is no need to call it. Do nothing. */
6061 : : return;
6062 : : #endif /* CLEAR_INSN_CACHE */
6063 : : }
6064 : :
6065 : 0 : targetm.calls.emit_call_builtin___clear_cache (begin, end);
6066 : : }
6067 : :
6068 : : /* Expand a call to __builtin___clear_cache. */
6069 : :
6070 : : static void
6071 : 28 : expand_builtin___clear_cache (tree exp)
6072 : : {
6073 : 28 : tree begin, end;
6074 : 28 : rtx begin_rtx, end_rtx;
6075 : :
6076 : : /* We must not expand to a library call. If we did, any
6077 : : fallback library function in libgcc that might contain a call to
6078 : : __builtin___clear_cache() would recurse infinitely. */
6079 : 28 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6080 : : {
6081 : 0 : error ("both arguments to %<__builtin___clear_cache%> must be pointers");
6082 : 0 : return;
6083 : : }
6084 : :
6085 : 28 : begin = CALL_EXPR_ARG (exp, 0);
6086 : 30 : begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
6087 : :
6088 : 28 : end = CALL_EXPR_ARG (exp, 1);
6089 : 30 : end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
6090 : :
6091 : 28 : maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
6092 : : }
6093 : :
6094 : : /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
6095 : :
6096 : : static rtx
6097 : 623 : round_trampoline_addr (rtx tramp)
6098 : : {
6099 : 623 : rtx temp, addend, mask;
6100 : :
6101 : : /* If we don't need too much alignment, we'll have been guaranteed
6102 : : proper alignment by get_trampoline_type. */
6103 : 623 : if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
6104 : : return tramp;
6105 : :
6106 : : /* Round address up to desired boundary. */
6107 : 0 : temp = gen_reg_rtx (Pmode);
6108 : 0 : addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
6109 : 0 : mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
6110 : :
6111 : 0 : temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
6112 : : temp, 0, OPTAB_LIB_WIDEN);
6113 : 0 : tramp = expand_simple_binop (Pmode, AND, temp, mask,
6114 : : temp, 0, OPTAB_LIB_WIDEN);
6115 : :
6116 : 0 : return tramp;
6117 : : }
6118 : :
6119 : : static rtx
6120 : 290 : expand_builtin_init_trampoline (tree exp, bool onstack)
6121 : : {
6122 : 290 : tree t_tramp, t_func, t_chain;
6123 : 290 : rtx m_tramp, r_tramp, r_chain, tmp;
6124 : :
6125 : 290 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
6126 : : POINTER_TYPE, VOID_TYPE))
6127 : : return NULL_RTX;
6128 : :
6129 : 290 : t_tramp = CALL_EXPR_ARG (exp, 0);
6130 : 290 : t_func = CALL_EXPR_ARG (exp, 1);
6131 : 290 : t_chain = CALL_EXPR_ARG (exp, 2);
6132 : :
6133 : 290 : r_tramp = expand_normal (t_tramp);
6134 : 290 : m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
6135 : 290 : MEM_NOTRAP_P (m_tramp) = 1;
6136 : :
6137 : : /* If ONSTACK, the TRAMP argument should be the address of a field
6138 : : within the local function's FRAME decl. Either way, let's see if
6139 : : we can fill in the MEM_ATTRs for this memory. */
6140 : 290 : if (TREE_CODE (t_tramp) == ADDR_EXPR)
6141 : 290 : set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
6142 : :
6143 : : /* Creator of a heap trampoline is responsible for making sure the
6144 : : address is aligned to at least STACK_BOUNDARY. Normally malloc
6145 : : will ensure this anyhow. */
6146 : 290 : tmp = round_trampoline_addr (r_tramp);
6147 : 290 : if (tmp != r_tramp)
6148 : : {
6149 : 0 : m_tramp = change_address (m_tramp, BLKmode, tmp);
6150 : 0 : set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
6151 : 0 : set_mem_size (m_tramp, TRAMPOLINE_SIZE);
6152 : : }
6153 : :
6154 : : /* The FUNC argument should be the address of the nested function.
6155 : : Extract the actual function decl to pass to the hook. */
6156 : 290 : gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
6157 : 290 : t_func = TREE_OPERAND (t_func, 0);
6158 : 290 : gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
6159 : :
6160 : 290 : r_chain = expand_normal (t_chain);
6161 : :
6162 : : /* Generate insns to initialize the trampoline. */
6163 : 290 : targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
6164 : :
6165 : 290 : if (onstack)
6166 : : {
6167 : 290 : trampolines_created = 1;
6168 : :
6169 : 290 : if (targetm.calls.custom_function_descriptors != 0)
6170 : 290 : warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
6171 : : "trampoline generated for nested function %qD", t_func);
6172 : : }
6173 : :
6174 : 290 : return const0_rtx;
6175 : : }
6176 : :
6177 : : static rtx
6178 : 333 : expand_builtin_adjust_trampoline (tree exp)
6179 : : {
6180 : 333 : rtx tramp;
6181 : :
6182 : 333 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6183 : : return NULL_RTX;
6184 : :
6185 : 333 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6186 : 333 : tramp = round_trampoline_addr (tramp);
6187 : 333 : if (targetm.calls.trampoline_adjust_address)
6188 : 0 : tramp = targetm.calls.trampoline_adjust_address (tramp);
6189 : :
6190 : : return tramp;
6191 : : }
6192 : :
6193 : : /* Expand a call to the builtin descriptor initialization routine.
6194 : : A descriptor is made up of a couple of pointers to the static
6195 : : chain and the code entry in this order. */
6196 : :
6197 : : static rtx
6198 : 0 : expand_builtin_init_descriptor (tree exp)
6199 : : {
6200 : 0 : tree t_descr, t_func, t_chain;
6201 : 0 : rtx m_descr, r_descr, r_func, r_chain;
6202 : :
6203 : 0 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
6204 : : VOID_TYPE))
6205 : : return NULL_RTX;
6206 : :
6207 : 0 : t_descr = CALL_EXPR_ARG (exp, 0);
6208 : 0 : t_func = CALL_EXPR_ARG (exp, 1);
6209 : 0 : t_chain = CALL_EXPR_ARG (exp, 2);
6210 : :
6211 : 0 : r_descr = expand_normal (t_descr);
6212 : 0 : m_descr = gen_rtx_MEM (BLKmode, r_descr);
6213 : 0 : MEM_NOTRAP_P (m_descr) = 1;
6214 : 0 : set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
6215 : :
6216 : 0 : r_func = expand_normal (t_func);
6217 : 0 : r_chain = expand_normal (t_chain);
6218 : :
6219 : : /* Generate insns to initialize the descriptor. */
6220 : 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
6221 : 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
6222 : : POINTER_SIZE / BITS_PER_UNIT), r_func);
6223 : :
6224 : 0 : return const0_rtx;
6225 : : }
6226 : :
6227 : : /* Expand a call to the builtin descriptor adjustment routine. */
6228 : :
6229 : : static rtx
6230 : 0 : expand_builtin_adjust_descriptor (tree exp)
6231 : : {
6232 : 0 : rtx tramp;
6233 : :
6234 : 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6235 : : return NULL_RTX;
6236 : :
6237 : 0 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6238 : :
6239 : : /* Unalign the descriptor to allow runtime identification. */
6240 : 0 : tramp = plus_constant (ptr_mode, tramp,
6241 : 0 : targetm.calls.custom_function_descriptors);
6242 : :
6243 : 0 : return force_operand (tramp, NULL_RTX);
6244 : : }
6245 : :
6246 : : /* Expand the call EXP to the built-in signbit, signbitf or signbitl
6247 : : function. The function first checks whether the back end provides
6248 : : an insn to implement signbit for the respective mode. If not, it
6249 : : checks whether the floating point format of the value is such that
6250 : : the sign bit can be extracted. If that is not the case, error out.
6251 : : EXP is the expression that is a call to the builtin function; if
6252 : : convenient, the result should be placed in TARGET. */
6253 : : static rtx
6254 : 1131 : expand_builtin_signbit (tree exp, rtx target)
6255 : : {
6256 : 1131 : const struct real_format *fmt;
6257 : 1131 : scalar_float_mode fmode;
6258 : 1131 : scalar_int_mode rmode, imode;
6259 : 1131 : tree arg;
6260 : 1131 : int word, bitpos;
6261 : 1131 : enum insn_code icode;
6262 : 1131 : rtx temp;
6263 : 1131 : location_t loc = EXPR_LOCATION (exp);
6264 : :
6265 : 1131 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6266 : : return NULL_RTX;
6267 : :
6268 : 1131 : arg = CALL_EXPR_ARG (exp, 0);
6269 : 1131 : fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
6270 : 1131 : rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
6271 : 1131 : fmt = REAL_MODE_FORMAT (fmode);
6272 : :
6273 : 1131 : arg = builtin_save_expr (arg);
6274 : :
6275 : : /* Expand the argument yielding a RTX expression. */
6276 : 1131 : temp = expand_normal (arg);
6277 : :
6278 : : /* Check if the back end provides an insn that handles signbit for the
6279 : : argument's mode. */
6280 : 1131 : icode = optab_handler (signbit_optab, fmode);
6281 : 1131 : if (icode != CODE_FOR_nothing)
6282 : : {
6283 : 10 : rtx_insn *last = get_last_insn ();
6284 : 10 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6285 : 10 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
6286 : : return this_target;
6287 : 0 : delete_insns_since (last);
6288 : : }
6289 : :
6290 : : /* For floating point formats without a sign bit, implement signbit
6291 : : as "ARG < 0.0". */
6292 : 1121 : bitpos = fmt->signbit_ro;
6293 : 1121 : if (bitpos < 0)
6294 : : {
6295 : : /* But we can't do this if the format supports signed zero. */
6296 : 0 : gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
6297 : :
6298 : 0 : arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
6299 : 0 : build_real (TREE_TYPE (arg), dconst0));
6300 : 0 : return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
6301 : : }
6302 : :
6303 : 2242 : if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
6304 : : {
6305 : 1017 : imode = int_mode_for_mode (fmode).require ();
6306 : 1017 : temp = gen_lowpart (imode, temp);
6307 : : }
6308 : : else
6309 : : {
6310 : 104 : imode = word_mode;
6311 : : /* Handle targets with different FP word orders. */
6312 : 104 : if (FLOAT_WORDS_BIG_ENDIAN)
6313 : : word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
6314 : : else
6315 : 104 : word = bitpos / BITS_PER_WORD;
6316 : 104 : temp = operand_subword_force (temp, word, fmode);
6317 : 104 : bitpos = bitpos % BITS_PER_WORD;
6318 : : }
6319 : :
6320 : : /* Force the intermediate word_mode (or narrower) result into a
6321 : : register. This avoids attempting to create paradoxical SUBREGs
6322 : : of floating point modes below. */
6323 : 1121 : temp = force_reg (imode, temp);
6324 : :
6325 : : /* If the bitpos is within the "result mode" lowpart, the operation
6326 : : can be implement with a single bitwise AND. Otherwise, we need
6327 : : a right shift and an AND. */
6328 : :
6329 : 2242 : if (bitpos < GET_MODE_BITSIZE (rmode))
6330 : : {
6331 : 913 : wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
6332 : :
6333 : 2739 : if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
6334 : 0 : temp = gen_lowpart (rmode, temp);
6335 : 1826 : temp = expand_binop (rmode, and_optab, temp,
6336 : 1826 : immed_wide_int_const (mask, rmode),
6337 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6338 : 913 : }
6339 : : else
6340 : : {
6341 : : /* Perform a logical right shift to place the signbit in the least
6342 : : significant bit, then truncate the result to the desired mode
6343 : : and mask just this bit. */
6344 : 208 : temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
6345 : 208 : temp = gen_lowpart (rmode, temp);
6346 : 208 : temp = expand_binop (rmode, and_optab, temp, const1_rtx,
6347 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6348 : : }
6349 : :
6350 : : return temp;
6351 : : }
6352 : :
6353 : : /* Expand fork or exec calls. TARGET is the desired target of the
6354 : : call. EXP is the call. FN is the
6355 : : identificator of the actual function. IGNORE is nonzero if the
6356 : : value is to be ignored. */
6357 : :
6358 : : static rtx
6359 : 87 : expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
6360 : : {
6361 : 87 : tree id, decl;
6362 : 87 : tree call;
6363 : :
6364 : : /* If we are not profiling, just call the function. */
6365 : 87 : if (!coverage_instrumentation_p ())
6366 : : return NULL_RTX;
6367 : :
6368 : : /* Otherwise call the wrapper. This should be equivalent for the rest of
6369 : : compiler, so the code does not diverge, and the wrapper may run the
6370 : : code necessary for keeping the profiling sane. */
6371 : :
6372 : 4 : switch (DECL_FUNCTION_CODE (fn))
6373 : : {
6374 : 4 : case BUILT_IN_FORK:
6375 : 4 : id = get_identifier ("__gcov_fork");
6376 : 4 : break;
6377 : :
6378 : 0 : case BUILT_IN_EXECL:
6379 : 0 : id = get_identifier ("__gcov_execl");
6380 : 0 : break;
6381 : :
6382 : 0 : case BUILT_IN_EXECV:
6383 : 0 : id = get_identifier ("__gcov_execv");
6384 : 0 : break;
6385 : :
6386 : 0 : case BUILT_IN_EXECLP:
6387 : 0 : id = get_identifier ("__gcov_execlp");
6388 : 0 : break;
6389 : :
6390 : 0 : case BUILT_IN_EXECLE:
6391 : 0 : id = get_identifier ("__gcov_execle");
6392 : 0 : break;
6393 : :
6394 : 0 : case BUILT_IN_EXECVP:
6395 : 0 : id = get_identifier ("__gcov_execvp");
6396 : 0 : break;
6397 : :
6398 : 0 : case BUILT_IN_EXECVE:
6399 : 0 : id = get_identifier ("__gcov_execve");
6400 : 0 : break;
6401 : :
6402 : 0 : default:
6403 : 0 : gcc_unreachable ();
6404 : : }
6405 : :
6406 : 4 : decl = build_decl (DECL_SOURCE_LOCATION (fn),
6407 : 4 : FUNCTION_DECL, id, TREE_TYPE (fn));
6408 : 4 : DECL_EXTERNAL (decl) = 1;
6409 : 4 : TREE_PUBLIC (decl) = 1;
6410 : 4 : DECL_ARTIFICIAL (decl) = 1;
6411 : 4 : TREE_NOTHROW (decl) = 1;
6412 : 4 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6413 : 4 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
6414 : 4 : call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
6415 : 4 : return expand_call (call, target, ignore);
6416 : : }
6417 : :
6418 : :
6419 : :
6420 : : /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6421 : : the pointer in these functions is void*, the tree optimizers may remove
6422 : : casts. The mode computed in expand_builtin isn't reliable either, due
6423 : : to __sync_bool_compare_and_swap.
6424 : :
6425 : : FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6426 : : group of builtins. This gives us log2 of the mode size. */
6427 : :
6428 : : static inline machine_mode
6429 : 134405 : get_builtin_sync_mode (int fcode_diff)
6430 : : {
6431 : : /* The size is not negotiable, so ask not to get BLKmode in return
6432 : : if the target indicates that a smaller size would be better. */
6433 : 134405 : return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
6434 : : }
6435 : :
6436 : : /* Expand the memory expression LOC and return the appropriate memory operand
6437 : : for the builtin_sync operations. */
6438 : :
6439 : : static rtx
6440 : 150552 : get_builtin_sync_mem (tree loc, machine_mode mode)
6441 : : {
6442 : 150552 : rtx addr, mem;
6443 : 150552 : int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
6444 : : ? TREE_TYPE (TREE_TYPE (loc))
6445 : : : TREE_TYPE (loc));
6446 : 150552 : scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
6447 : :
6448 : 150552 : addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
6449 : 150552 : addr = convert_memory_address (addr_mode, addr);
6450 : :
6451 : : /* Note that we explicitly do not want any alias information for this
6452 : : memory, so that we kill all other live memories. Otherwise we don't
6453 : : satisfy the full barrier semantics of the intrinsic. */
6454 : 150552 : mem = gen_rtx_MEM (mode, addr);
6455 : :
6456 : 150552 : set_mem_addr_space (mem, addr_space);
6457 : :
6458 : 150552 : mem = validize_mem (mem);
6459 : :
6460 : : /* The alignment needs to be at least according to that of the mode. */
6461 : 150552 : set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6462 : : get_pointer_alignment (loc)));
6463 : 150552 : set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6464 : 150552 : MEM_VOLATILE_P (mem) = 1;
6465 : :
6466 : 150552 : return mem;
6467 : : }
6468 : :
6469 : : /* Make sure an argument is in the right mode.
6470 : : EXP is the tree argument.
6471 : : MODE is the mode it should be in. */
6472 : :
6473 : : static rtx
6474 : 94198 : expand_expr_force_mode (tree exp, machine_mode mode)
6475 : : {
6476 : 94198 : rtx val;
6477 : 94198 : machine_mode old_mode;
6478 : :
6479 : 94198 : if (TREE_CODE (exp) == SSA_NAME
6480 : 94198 : && TYPE_MODE (TREE_TYPE (exp)) != mode)
6481 : : {
6482 : : /* Undo argument promotion if possible, as combine might not
6483 : : be able to do it later due to MEM_VOLATILE_P uses in the
6484 : : patterns. */
6485 : 25 : gimple *g = get_gimple_for_ssa_name (exp);
6486 : 25 : if (g && gimple_assign_cast_p (g))
6487 : : {
6488 : 6 : tree rhs = gimple_assign_rhs1 (g);
6489 : 6 : tree_code code = gimple_assign_rhs_code (g);
6490 : 6 : if (CONVERT_EXPR_CODE_P (code)
6491 : 6 : && TYPE_MODE (TREE_TYPE (rhs)) == mode
6492 : 6 : && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6493 : 6 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6494 : 12 : && (TYPE_PRECISION (TREE_TYPE (exp))
6495 : 6 : > TYPE_PRECISION (TREE_TYPE (rhs))))
6496 : : exp = rhs;
6497 : : }
6498 : : }
6499 : :
6500 : 94198 : val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6501 : : /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6502 : : of CONST_INTs, where we know the old_mode only from the call argument. */
6503 : :
6504 : 94198 : old_mode = GET_MODE (val);
6505 : 94198 : if (old_mode == VOIDmode)
6506 : 44883 : old_mode = TYPE_MODE (TREE_TYPE (exp));
6507 : 94198 : val = convert_modes (mode, old_mode, val, 1);
6508 : 94198 : return val;
6509 : : }
6510 : :
6511 : :
6512 : : /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6513 : : EXP is the CALL_EXPR. CODE is the rtx code
6514 : : that corresponds to the arithmetic or logical operation from the name;
6515 : : an exception here is that NOT actually means NAND. TARGET is an optional
6516 : : place for us to store the results; AFTER is true if this is the
6517 : : fetch_and_xxx form. */
6518 : :
6519 : : static rtx
6520 : 3997 : expand_builtin_sync_operation (machine_mode mode, tree exp,
6521 : : enum rtx_code code, bool after,
6522 : : rtx target)
6523 : : {
6524 : 3997 : rtx val, mem;
6525 : 3997 : location_t loc = EXPR_LOCATION (exp);
6526 : :
6527 : 3997 : if (code == NOT && warn_sync_nand)
6528 : : {
6529 : 517 : tree fndecl = get_callee_fndecl (exp);
6530 : 517 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6531 : :
6532 : 517 : static bool warned_f_a_n, warned_n_a_f;
6533 : :
6534 : 517 : switch (fcode)
6535 : : {
6536 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6537 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6538 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6539 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6540 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6541 : 434 : if (warned_f_a_n)
6542 : : break;
6543 : :
6544 : 30 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6545 : 30 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6546 : 30 : warned_f_a_n = true;
6547 : 30 : break;
6548 : :
6549 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6550 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6551 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6552 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6553 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6554 : 83 : if (warned_n_a_f)
6555 : : break;
6556 : :
6557 : 16 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6558 : 16 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6559 : 16 : warned_n_a_f = true;
6560 : 16 : break;
6561 : :
6562 : 0 : default:
6563 : 0 : gcc_unreachable ();
6564 : : }
6565 : : }
6566 : :
6567 : : /* Expand the operands. */
6568 : 3997 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6569 : 3997 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6570 : :
6571 : 3997 : return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6572 : 3997 : after);
6573 : : }
6574 : :
6575 : : /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6576 : : intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6577 : : true if this is the boolean form. TARGET is a place for us to store the
6578 : : results; this is NOT optional if IS_BOOL is true. */
6579 : :
6580 : : static rtx
6581 : 456 : expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6582 : : bool is_bool, rtx target)
6583 : : {
6584 : 456 : rtx old_val, new_val, mem;
6585 : 456 : rtx *pbool, *poval;
6586 : :
6587 : : /* Expand the operands. */
6588 : 456 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6589 : 456 : old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6590 : 456 : new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6591 : :
6592 : 456 : pbool = poval = NULL;
6593 : 456 : if (target != const0_rtx)
6594 : : {
6595 : 430 : if (is_bool)
6596 : : pbool = ⌖
6597 : : else
6598 : 230 : poval = ⌖
6599 : : }
6600 : 456 : if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6601 : : false, MEMMODEL_SYNC_SEQ_CST,
6602 : : MEMMODEL_SYNC_SEQ_CST))
6603 : : return NULL_RTX;
6604 : :
6605 : 454 : return target;
6606 : : }
6607 : :
6608 : : /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6609 : : general form is actually an atomic exchange, and some targets only
6610 : : support a reduced form with the second argument being a constant 1.
6611 : : EXP is the CALL_EXPR; TARGET is an optional place for us to store
6612 : : the results. */
6613 : :
6614 : : static rtx
6615 : 326 : expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6616 : : rtx target)
6617 : : {
6618 : 326 : rtx val, mem;
6619 : :
6620 : : /* Expand the operands. */
6621 : 326 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6622 : 326 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6623 : :
6624 : 326 : return expand_sync_lock_test_and_set (target, mem, val);
6625 : : }
6626 : :
6627 : : /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6628 : :
6629 : : static rtx
6630 : 158 : expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6631 : : {
6632 : 158 : rtx mem;
6633 : :
6634 : : /* Expand the operands. */
6635 : 158 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6636 : :
6637 : 158 : return expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6638 : : }
6639 : :
6640 : : /* Given an integer representing an ``enum memmodel'', verify its
6641 : : correctness and return the memory model enum. */
6642 : :
6643 : : static enum memmodel
6644 : 168844 : get_memmodel (tree exp)
6645 : : {
6646 : : /* If the parameter is not a constant, it's a run time value so we'll just
6647 : : convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6648 : 168844 : if (TREE_CODE (exp) != INTEGER_CST)
6649 : : return MEMMODEL_SEQ_CST;
6650 : :
6651 : 168001 : rtx op = expand_normal (exp);
6652 : :
6653 : 168001 : unsigned HOST_WIDE_INT val = INTVAL (op);
6654 : 168001 : if (targetm.memmodel_check)
6655 : 168001 : val = targetm.memmodel_check (val);
6656 : 0 : else if (val & ~MEMMODEL_MASK)
6657 : : return MEMMODEL_SEQ_CST;
6658 : :
6659 : : /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6660 : 168001 : if (memmodel_base (val) >= MEMMODEL_LAST)
6661 : : return MEMMODEL_SEQ_CST;
6662 : :
6663 : : /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6664 : : be conservative and promote consume to acquire. */
6665 : 168000 : if (val == MEMMODEL_CONSUME)
6666 : 750 : val = MEMMODEL_ACQUIRE;
6667 : :
6668 : 168000 : return (enum memmodel) val;
6669 : : }
6670 : :
6671 : : /* Expand the __atomic_exchange intrinsic:
6672 : : TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6673 : : EXP is the CALL_EXPR.
6674 : : TARGET is an optional place for us to store the results. */
6675 : :
6676 : : static rtx
6677 : 2811 : expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6678 : : {
6679 : 2811 : rtx val, mem;
6680 : 2811 : enum memmodel model;
6681 : :
6682 : 2811 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6683 : :
6684 : 2811 : if (!flag_inline_atomics)
6685 : : return NULL_RTX;
6686 : :
6687 : : /* Expand the operands. */
6688 : 2760 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6689 : 2760 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6690 : :
6691 : 2760 : return expand_atomic_exchange (target, mem, val, model);
6692 : : }
6693 : :
6694 : : /* Expand the __atomic_compare_exchange intrinsic:
6695 : : bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6696 : : TYPE desired, BOOL weak,
6697 : : enum memmodel success,
6698 : : enum memmodel failure)
6699 : : EXP is the CALL_EXPR.
6700 : : TARGET is an optional place for us to store the results. */
6701 : :
6702 : : static rtx
6703 : 9194 : expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6704 : : rtx target)
6705 : : {
6706 : 9194 : rtx expect, desired, mem, oldval;
6707 : 9194 : rtx_code_label *label;
6708 : 9194 : tree weak;
6709 : 9194 : bool is_weak;
6710 : :
6711 : 9194 : memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6712 : 9194 : memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6713 : :
6714 : 9194 : if (failure > success)
6715 : 22 : success = MEMMODEL_SEQ_CST;
6716 : :
6717 : 9194 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6718 : : {
6719 : : failure = MEMMODEL_SEQ_CST;
6720 : : success = MEMMODEL_SEQ_CST;
6721 : : }
6722 : :
6723 : :
6724 : 9194 : if (!flag_inline_atomics)
6725 : : return NULL_RTX;
6726 : :
6727 : : /* Expand the operands. */
6728 : 9143 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6729 : :
6730 : 9143 : expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6731 : 9143 : expect = convert_memory_address (Pmode, expect);
6732 : 9143 : expect = gen_rtx_MEM (mode, expect);
6733 : 9143 : desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6734 : :
6735 : 9143 : weak = CALL_EXPR_ARG (exp, 3);
6736 : 9143 : is_weak = false;
6737 : 9143 : if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6738 : 9143 : is_weak = true;
6739 : :
6740 : 9143 : if (target == const0_rtx)
6741 : 280 : target = NULL;
6742 : :
6743 : : /* Lest the rtl backend create a race condition with an imporoper store
6744 : : to memory, always create a new pseudo for OLDVAL. */
6745 : 9143 : oldval = NULL;
6746 : :
6747 : 9143 : if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6748 : : is_weak, success, failure))
6749 : : return NULL_RTX;
6750 : :
6751 : : /* Conditionally store back to EXPECT, lest we create a race condition
6752 : : with an improper store to memory. */
6753 : : /* ??? With a rearrangement of atomics at the gimple level, we can handle
6754 : : the normal case where EXPECT is totally private, i.e. a register. At
6755 : : which point the store can be unconditional. */
6756 : 7400 : label = gen_label_rtx ();
6757 : 7400 : emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6758 : 7400 : GET_MODE (target), 1, label);
6759 : 7400 : emit_move_insn (expect, oldval);
6760 : 7400 : emit_label (label);
6761 : :
6762 : 7400 : return target;
6763 : : }
6764 : :
6765 : : /* Helper function for expand_ifn_atomic_compare_exchange - expand
6766 : : internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6767 : : call. The weak parameter must be dropped to match the expected parameter
6768 : : list and the expected argument changed from value to pointer to memory
6769 : : slot. */
6770 : :
6771 : : static void
6772 : 0 : expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6773 : : {
6774 : 0 : unsigned int z;
6775 : 0 : vec<tree, va_gc> *vec;
6776 : :
6777 : 0 : vec_alloc (vec, 5);
6778 : 0 : vec->quick_push (gimple_call_arg (call, 0));
6779 : 0 : tree expected = gimple_call_arg (call, 1);
6780 : 0 : rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6781 : 0 : TREE_TYPE (expected));
6782 : 0 : rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6783 : 0 : if (expd != x)
6784 : 0 : emit_move_insn (x, expd);
6785 : 0 : tree v = make_tree (TREE_TYPE (expected), x);
6786 : 0 : vec->quick_push (build1 (ADDR_EXPR,
6787 : 0 : build_pointer_type (TREE_TYPE (expected)), v));
6788 : 0 : vec->quick_push (gimple_call_arg (call, 2));
6789 : : /* Skip the boolean weak parameter. */
6790 : 0 : for (z = 4; z < 6; z++)
6791 : 0 : vec->quick_push (gimple_call_arg (call, z));
6792 : : /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6793 : 0 : unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6794 : 0 : gcc_assert (bytes_log2 < 5);
6795 : 0 : built_in_function fncode
6796 : : = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6797 : : + bytes_log2);
6798 : 0 : tree fndecl = builtin_decl_explicit (fncode);
6799 : 0 : tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6800 : : fndecl);
6801 : 0 : tree exp = build_call_vec (boolean_type_node, fn, vec);
6802 : 0 : tree lhs = gimple_call_lhs (call);
6803 : 0 : rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6804 : 0 : if (lhs)
6805 : : {
6806 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6807 : 0 : if (GET_MODE (boolret) != mode)
6808 : 0 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6809 : 0 : x = force_reg (mode, x);
6810 : 0 : write_complex_part (target, boolret, true, true);
6811 : 0 : write_complex_part (target, x, false, false);
6812 : : }
6813 : 0 : }
6814 : :
6815 : : /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6816 : :
6817 : : void
6818 : 13647 : expand_ifn_atomic_compare_exchange (gcall *call)
6819 : : {
6820 : 13647 : int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6821 : 13647 : gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6822 : 13647 : machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6823 : :
6824 : 13647 : memmodel success = get_memmodel (gimple_call_arg (call, 4));
6825 : 13647 : memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6826 : :
6827 : 13647 : if (failure > success)
6828 : 0 : success = MEMMODEL_SEQ_CST;
6829 : :
6830 : 13647 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6831 : : {
6832 : : failure = MEMMODEL_SEQ_CST;
6833 : : success = MEMMODEL_SEQ_CST;
6834 : : }
6835 : :
6836 : 13647 : if (!flag_inline_atomics)
6837 : : {
6838 : 0 : expand_ifn_atomic_compare_exchange_into_call (call, mode);
6839 : 0 : return;
6840 : : }
6841 : :
6842 : : /* Expand the operands. */
6843 : 13647 : rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6844 : :
6845 : 13647 : rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6846 : 13647 : rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6847 : :
6848 : 13647 : bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6849 : :
6850 : 13647 : rtx boolret = NULL;
6851 : 13647 : rtx oldval = NULL;
6852 : :
6853 : 13647 : if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6854 : : is_weak, success, failure))
6855 : : {
6856 : 0 : expand_ifn_atomic_compare_exchange_into_call (call, mode);
6857 : 0 : return;
6858 : : }
6859 : :
6860 : 13647 : tree lhs = gimple_call_lhs (call);
6861 : 13647 : if (lhs)
6862 : : {
6863 : 13447 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6864 : 13447 : if (GET_MODE (boolret) != mode)
6865 : 11957 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6866 : 13447 : write_complex_part (target, boolret, true, true);
6867 : 13447 : write_complex_part (target, oldval, false, false);
6868 : : }
6869 : : }
6870 : :
6871 : : /* Expand the __atomic_load intrinsic:
6872 : : TYPE __atomic_load (TYPE *object, enum memmodel)
6873 : : EXP is the CALL_EXPR.
6874 : : TARGET is an optional place for us to store the results. */
6875 : :
6876 : : static rtx
6877 : 70024 : expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6878 : : {
6879 : 70024 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6880 : 70024 : if (is_mm_release (model) || is_mm_acq_rel (model))
6881 : : model = MEMMODEL_SEQ_CST;
6882 : :
6883 : 70024 : if (!flag_inline_atomics)
6884 : : return NULL_RTX;
6885 : :
6886 : : /* Expand the operand. */
6887 : 69983 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6888 : :
6889 : 69983 : return expand_atomic_load (target, mem, model);
6890 : : }
6891 : :
6892 : :
6893 : : /* Expand the __atomic_store intrinsic:
6894 : : void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6895 : : EXP is the CALL_EXPR.
6896 : : TARGET is an optional place for us to store the results. */
6897 : :
6898 : : static rtx
6899 : 17373 : expand_builtin_atomic_store (machine_mode mode, tree exp)
6900 : : {
6901 : 17373 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6902 : 17373 : if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6903 : 1586 : || is_mm_release (model)))
6904 : : model = MEMMODEL_SEQ_CST;
6905 : :
6906 : 17373 : if (!flag_inline_atomics)
6907 : : return NULL_RTX;
6908 : :
6909 : : /* Expand the operands. */
6910 : 17342 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6911 : 17342 : rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6912 : :
6913 : 17342 : return expand_atomic_store (mem, val, model, false);
6914 : : }
6915 : :
6916 : : /* Expand the __atomic_fetch_XXX intrinsic:
6917 : : TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6918 : : EXP is the CALL_EXPR.
6919 : : TARGET is an optional place for us to store the results.
6920 : : CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6921 : : FETCH_AFTER is true if returning the result of the operation.
6922 : : FETCH_AFTER is false if returning the value before the operation.
6923 : : IGNORE is true if the result is not used.
6924 : : EXT_CALL is the correct builtin for an external call if this cannot be
6925 : : resolved to an instruction sequence. */
6926 : :
6927 : : static rtx
6928 : 30036 : expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6929 : : enum rtx_code code, bool fetch_after,
6930 : : bool ignore, enum built_in_function ext_call)
6931 : : {
6932 : 30036 : rtx val, mem, ret;
6933 : 30036 : enum memmodel model;
6934 : 30036 : tree fndecl;
6935 : 30036 : tree addr;
6936 : :
6937 : 30036 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6938 : :
6939 : : /* Expand the operands. */
6940 : 30036 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6941 : 30036 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6942 : :
6943 : : /* Only try generating instructions if inlining is turned on. */
6944 : 30036 : if (flag_inline_atomics)
6945 : : {
6946 : 29493 : ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6947 : 29493 : if (ret)
6948 : : return ret;
6949 : : }
6950 : :
6951 : : /* Return if a different routine isn't needed for the library call. */
6952 : 1142 : if (ext_call == BUILT_IN_NONE)
6953 : : return NULL_RTX;
6954 : :
6955 : : /* Change the call to the specified function. */
6956 : 279 : fndecl = get_callee_fndecl (exp);
6957 : 279 : addr = CALL_EXPR_FN (exp);
6958 : 279 : STRIP_NOPS (addr);
6959 : :
6960 : 279 : gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6961 : 279 : TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6962 : :
6963 : : /* If we will emit code after the call, the call cannot be a tail call.
6964 : : If it is emitted as a tail call, a barrier is emitted after it, and
6965 : : then all trailing code is removed. */
6966 : 279 : if (!ignore)
6967 : 182 : CALL_EXPR_TAILCALL (exp) = 0;
6968 : :
6969 : : /* Expand the call here so we can emit trailing code. */
6970 : 279 : ret = expand_call (exp, target, ignore);
6971 : :
6972 : : /* Replace the original function just in case it matters. */
6973 : 279 : TREE_OPERAND (addr, 0) = fndecl;
6974 : :
6975 : : /* Then issue the arithmetic correction to return the right result. */
6976 : 279 : if (!ignore)
6977 : : {
6978 : 182 : if (code == NOT)
6979 : : {
6980 : 31 : ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6981 : : OPTAB_LIB_WIDEN);
6982 : 31 : ret = expand_simple_unop (mode, NOT, ret, target, true);
6983 : : }
6984 : : else
6985 : 151 : ret = expand_simple_binop (mode, code, ret, val, target, true,
6986 : : OPTAB_LIB_WIDEN);
6987 : : }
6988 : : return ret;
6989 : : }
6990 : :
6991 : : /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6992 : :
6993 : : void
6994 : 427 : expand_ifn_atomic_bit_test_and (gcall *call)
6995 : : {
6996 : 427 : tree ptr = gimple_call_arg (call, 0);
6997 : 427 : tree bit = gimple_call_arg (call, 1);
6998 : 427 : tree flag = gimple_call_arg (call, 2);
6999 : 427 : tree lhs = gimple_call_lhs (call);
7000 : 427 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7001 : 427 : machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
7002 : 427 : enum rtx_code code;
7003 : 427 : optab optab;
7004 : 427 : class expand_operand ops[5];
7005 : :
7006 : 427 : gcc_assert (flag_inline_atomics);
7007 : :
7008 : 427 : if (gimple_call_num_args (call) == 5)
7009 : 296 : model = get_memmodel (gimple_call_arg (call, 3));
7010 : :
7011 : 427 : rtx mem = get_builtin_sync_mem (ptr, mode);
7012 : 427 : rtx val = expand_expr_force_mode (bit, mode);
7013 : :
7014 : 427 : switch (gimple_call_internal_fn (call))
7015 : : {
7016 : : case IFN_ATOMIC_BIT_TEST_AND_SET:
7017 : : code = IOR;
7018 : : optab = atomic_bit_test_and_set_optab;
7019 : : break;
7020 : : case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
7021 : : code = XOR;
7022 : : optab = atomic_bit_test_and_complement_optab;
7023 : : break;
7024 : : case IFN_ATOMIC_BIT_TEST_AND_RESET:
7025 : : code = AND;
7026 : : optab = atomic_bit_test_and_reset_optab;
7027 : : break;
7028 : 0 : default:
7029 : 0 : gcc_unreachable ();
7030 : : }
7031 : :
7032 : 427 : if (lhs == NULL_TREE)
7033 : : {
7034 : 0 : rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
7035 : : val, NULL_RTX, true, OPTAB_DIRECT);
7036 : 0 : if (code == AND)
7037 : 0 : val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
7038 : 0 : if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
7039 : 427 : return;
7040 : : }
7041 : :
7042 : 427 : rtx target;
7043 : 427 : if (lhs)
7044 : 427 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7045 : : else
7046 : 0 : target = gen_reg_rtx (mode);
7047 : 427 : enum insn_code icode = direct_optab_handler (optab, mode);
7048 : 427 : gcc_assert (icode != CODE_FOR_nothing);
7049 : 427 : create_output_operand (&ops[0], target, mode);
7050 : 427 : create_fixed_operand (&ops[1], mem);
7051 : 427 : create_convert_operand_to (&ops[2], val, mode, true);
7052 : 427 : create_integer_operand (&ops[3], model);
7053 : 427 : create_integer_operand (&ops[4], integer_onep (flag));
7054 : 427 : if (maybe_expand_insn (icode, 5, ops))
7055 : : return;
7056 : :
7057 : 0 : rtx bitval = val;
7058 : 0 : val = expand_simple_binop (mode, ASHIFT, const1_rtx,
7059 : : val, NULL_RTX, true, OPTAB_DIRECT);
7060 : 0 : rtx maskval = val;
7061 : 0 : if (code == AND)
7062 : 0 : val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
7063 : 0 : rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
7064 : : code, model, false);
7065 : 0 : if (!result)
7066 : : {
7067 : 0 : bool is_atomic = gimple_call_num_args (call) == 5;
7068 : 0 : tree tcall = gimple_call_arg (call, 3 + is_atomic);
7069 : 0 : tree fndecl = gimple_call_addr_fndecl (tcall);
7070 : 0 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
7071 : 0 : tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
7072 : : make_tree (type, val),
7073 : : is_atomic
7074 : 0 : ? gimple_call_arg (call, 3)
7075 : : : integer_zero_node);
7076 : 0 : result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7077 : : mode, !lhs);
7078 : : }
7079 : 0 : if (!lhs)
7080 : : return;
7081 : 0 : if (integer_onep (flag))
7082 : : {
7083 : 0 : result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
7084 : : NULL_RTX, true, OPTAB_DIRECT);
7085 : 0 : result = expand_simple_binop (mode, AND, result, const1_rtx, target,
7086 : : true, OPTAB_DIRECT);
7087 : : }
7088 : : else
7089 : 0 : result = expand_simple_binop (mode, AND, result, maskval, target, true,
7090 : : OPTAB_DIRECT);
7091 : 0 : if (result != target)
7092 : 0 : emit_move_insn (target, result);
7093 : : }
7094 : :
7095 : : /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
7096 : :
7097 : : void
7098 : 1961 : expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
7099 : : {
7100 : 1961 : tree cmp = gimple_call_arg (call, 0);
7101 : 1961 : tree ptr = gimple_call_arg (call, 1);
7102 : 1961 : tree arg = gimple_call_arg (call, 2);
7103 : 1961 : tree lhs = gimple_call_lhs (call);
7104 : 1961 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7105 : 1961 : machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
7106 : 1961 : optab optab;
7107 : 1961 : rtx_code code;
7108 : 1961 : class expand_operand ops[5];
7109 : :
7110 : 1961 : gcc_assert (flag_inline_atomics);
7111 : :
7112 : 1961 : if (gimple_call_num_args (call) == 5)
7113 : 1553 : model = get_memmodel (gimple_call_arg (call, 3));
7114 : :
7115 : 1961 : rtx mem = get_builtin_sync_mem (ptr, mode);
7116 : 1961 : rtx op = expand_expr_force_mode (arg, mode);
7117 : :
7118 : 1961 : switch (gimple_call_internal_fn (call))
7119 : : {
7120 : : case IFN_ATOMIC_ADD_FETCH_CMP_0:
7121 : : code = PLUS;
7122 : : optab = atomic_add_fetch_cmp_0_optab;
7123 : : break;
7124 : : case IFN_ATOMIC_SUB_FETCH_CMP_0:
7125 : : code = MINUS;
7126 : : optab = atomic_sub_fetch_cmp_0_optab;
7127 : : break;
7128 : : case IFN_ATOMIC_AND_FETCH_CMP_0:
7129 : : code = AND;
7130 : : optab = atomic_and_fetch_cmp_0_optab;
7131 : : break;
7132 : : case IFN_ATOMIC_OR_FETCH_CMP_0:
7133 : : code = IOR;
7134 : : optab = atomic_or_fetch_cmp_0_optab;
7135 : : break;
7136 : : case IFN_ATOMIC_XOR_FETCH_CMP_0:
7137 : : code = XOR;
7138 : : optab = atomic_xor_fetch_cmp_0_optab;
7139 : : break;
7140 : 0 : default:
7141 : 0 : gcc_unreachable ();
7142 : : }
7143 : :
7144 : 1961 : enum rtx_code comp = UNKNOWN;
7145 : 1961 : switch (tree_to_uhwi (cmp))
7146 : : {
7147 : : case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
7148 : : case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
7149 : : case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
7150 : : case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
7151 : : case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
7152 : : case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
7153 : 0 : default: gcc_unreachable ();
7154 : : }
7155 : :
7156 : 1961 : rtx target;
7157 : 1961 : if (lhs == NULL_TREE)
7158 : 0 : target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
7159 : : else
7160 : 1961 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7161 : 1961 : enum insn_code icode = direct_optab_handler (optab, mode);
7162 : 1961 : gcc_assert (icode != CODE_FOR_nothing);
7163 : 1961 : create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
7164 : 1961 : create_fixed_operand (&ops[1], mem);
7165 : 1961 : create_convert_operand_to (&ops[2], op, mode, true);
7166 : 1961 : create_integer_operand (&ops[3], model);
7167 : 1961 : create_integer_operand (&ops[4], comp);
7168 : 1961 : if (maybe_expand_insn (icode, 5, ops))
7169 : 1929 : return;
7170 : :
7171 : 32 : rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
7172 : : code, model, true);
7173 : 32 : if (!result)
7174 : : {
7175 : 0 : bool is_atomic = gimple_call_num_args (call) == 5;
7176 : 0 : tree tcall = gimple_call_arg (call, 3 + is_atomic);
7177 : 0 : tree fndecl = gimple_call_addr_fndecl (tcall);
7178 : 0 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
7179 : 0 : tree exp = build_call_nary (type, tcall,
7180 : : 2 + is_atomic, ptr, arg,
7181 : : is_atomic
7182 : 0 : ? gimple_call_arg (call, 3)
7183 : : : integer_zero_node);
7184 : 0 : result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7185 : : mode, !lhs);
7186 : : }
7187 : :
7188 : 32 : if (lhs)
7189 : : {
7190 : 32 : result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
7191 : : 0, 1);
7192 : 32 : if (result != target)
7193 : 32 : emit_move_insn (target, result);
7194 : : }
7195 : : }
7196 : :
7197 : : /* Expand an atomic clear operation.
7198 : : void _atomic_clear (BOOL *obj, enum memmodel)
7199 : : EXP is the call expression. */
7200 : :
7201 : : static rtx
7202 : 57 : expand_builtin_atomic_clear (tree exp)
7203 : : {
7204 : 57 : machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7205 : 57 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7206 : 57 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7207 : :
7208 : 57 : if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
7209 : : model = MEMMODEL_SEQ_CST;
7210 : :
7211 : : /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
7212 : : Failing that, a store is issued by __atomic_store. The only way this can
7213 : : fail is if the bool type is larger than a word size. Unlikely, but
7214 : : handle it anyway for completeness. Assume a single threaded model since
7215 : : there is no atomic support in this case, and no barriers are required. */
7216 : 57 : rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
7217 : 57 : if (!ret)
7218 : 0 : emit_move_insn (mem, const0_rtx);
7219 : 57 : return const0_rtx;
7220 : : }
7221 : :
7222 : : /* Expand an atomic test_and_set operation.
7223 : : bool _atomic_test_and_set (BOOL *obj, enum memmodel)
7224 : : EXP is the call expression. */
7225 : :
7226 : : static rtx
7227 : 259 : expand_builtin_atomic_test_and_set (tree exp, rtx target)
7228 : : {
7229 : 259 : rtx mem;
7230 : 259 : enum memmodel model;
7231 : 259 : machine_mode mode;
7232 : :
7233 : 259 : mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7234 : 259 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7235 : 259 : model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7236 : :
7237 : 259 : return expand_atomic_test_and_set (target, mem, model);
7238 : : }
7239 : :
7240 : :
7241 : : /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
7242 : : this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
7243 : :
7244 : : static tree
7245 : 88762 : fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
7246 : : {
7247 : 88762 : int size;
7248 : 88762 : machine_mode mode;
7249 : 88762 : unsigned int mode_align, type_align;
7250 : :
7251 : 88762 : if (TREE_CODE (arg0) != INTEGER_CST)
7252 : : return NULL_TREE;
7253 : :
7254 : : /* We need a corresponding integer mode for the access to be lock-free. */
7255 : 88668 : size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
7256 : 88668 : if (!int_mode_for_size (size, 0).exists (&mode))
7257 : 1 : return boolean_false_node;
7258 : :
7259 : 88667 : mode_align = GET_MODE_ALIGNMENT (mode);
7260 : :
7261 : 88667 : if (TREE_CODE (arg1) == INTEGER_CST)
7262 : : {
7263 : 68727 : unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
7264 : :
7265 : : /* Either this argument is null, or it's a fake pointer encoding
7266 : : the alignment of the object. */
7267 : 68727 : val = least_bit_hwi (val);
7268 : 68727 : val *= BITS_PER_UNIT;
7269 : :
7270 : 68727 : if (val == 0 || mode_align < val)
7271 : : type_align = mode_align;
7272 : : else
7273 : 19346 : type_align = val;
7274 : : }
7275 : : else
7276 : : {
7277 : 19940 : tree ttype = TREE_TYPE (arg1);
7278 : :
7279 : : /* This function is usually invoked and folded immediately by the front
7280 : : end before anything else has a chance to look at it. The pointer
7281 : : parameter at this point is usually cast to a void *, so check for that
7282 : : and look past the cast. */
7283 : 63 : if (CONVERT_EXPR_P (arg1)
7284 : 19908 : && POINTER_TYPE_P (ttype)
7285 : 19908 : && VOID_TYPE_P (TREE_TYPE (ttype))
7286 : 39848 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
7287 : 19877 : arg1 = TREE_OPERAND (arg1, 0);
7288 : :
7289 : 19940 : ttype = TREE_TYPE (arg1);
7290 : 19940 : gcc_assert (POINTER_TYPE_P (ttype));
7291 : :
7292 : : /* Get the underlying type of the object. */
7293 : 19940 : ttype = TREE_TYPE (ttype);
7294 : 19940 : type_align = TYPE_ALIGN (ttype);
7295 : : }
7296 : :
7297 : : /* If the object has smaller alignment, the lock free routines cannot
7298 : : be used. */
7299 : 88667 : if (type_align < mode_align)
7300 : 91 : return boolean_false_node;
7301 : :
7302 : : /* Check if a compare_and_swap pattern exists for the mode which represents
7303 : : the required size. The pattern is not allowed to fail, so the existence
7304 : : of the pattern indicates support is present. Also require that an
7305 : : atomic load exists for the required size. */
7306 : 88576 : if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
7307 : 88534 : return boolean_true_node;
7308 : : else
7309 : 42 : return boolean_false_node;
7310 : : }
7311 : :
7312 : : /* Return true if the parameters to call EXP represent an object which will
7313 : : always generate lock free instructions. The first argument represents the
7314 : : size of the object, and the second parameter is a pointer to the object
7315 : : itself. If NULL is passed for the object, then the result is based on
7316 : : typical alignment for an object of the specified size. Otherwise return
7317 : : false. */
7318 : :
7319 : : static rtx
7320 : 1 : expand_builtin_atomic_always_lock_free (tree exp)
7321 : : {
7322 : 1 : tree size;
7323 : 1 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7324 : 1 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7325 : :
7326 : 1 : if (TREE_CODE (arg0) != INTEGER_CST)
7327 : : {
7328 : 1 : error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
7329 : 1 : return const0_rtx;
7330 : : }
7331 : :
7332 : 0 : size = fold_builtin_atomic_always_lock_free (arg0, arg1);
7333 : 0 : if (size == boolean_true_node)
7334 : 0 : return const1_rtx;
7335 : 0 : return const0_rtx;
7336 : : }
7337 : :
7338 : : /* Return a one or zero if it can be determined that object ARG1 of size ARG
7339 : : is lock free on this architecture. */
7340 : :
7341 : : static tree
7342 : 39329 : fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
7343 : : {
7344 : 39329 : if (!flag_inline_atomics)
7345 : : return NULL_TREE;
7346 : :
7347 : : /* If it isn't always lock free, don't generate a result. */
7348 : 39320 : if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
7349 : : return boolean_true_node;
7350 : :
7351 : : return NULL_TREE;
7352 : : }
7353 : :
7354 : : /* Return true if the parameters to call EXP represent an object which will
7355 : : always generate lock free instructions. The first argument represents the
7356 : : size of the object, and the second parameter is a pointer to the object
7357 : : itself. If NULL is passed for the object, then the result is based on
7358 : : typical alignment for an object of the specified size. Otherwise return
7359 : : NULL*/
7360 : :
7361 : : static rtx
7362 : 3 : expand_builtin_atomic_is_lock_free (tree exp)
7363 : : {
7364 : 3 : tree size;
7365 : 3 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7366 : 3 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7367 : :
7368 : 3 : if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
7369 : : {
7370 : 0 : error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
7371 : 0 : return NULL_RTX;
7372 : : }
7373 : :
7374 : 3 : if (!flag_inline_atomics)
7375 : : return NULL_RTX;
7376 : :
7377 : : /* If the value is known at compile time, return the RTX for it. */
7378 : 2 : size = fold_builtin_atomic_is_lock_free (arg0, arg1);
7379 : 2 : if (size == boolean_true_node)
7380 : 0 : return const1_rtx;
7381 : :
7382 : : return NULL_RTX;
7383 : : }
7384 : :
7385 : : /* Expand the __atomic_thread_fence intrinsic:
7386 : : void __atomic_thread_fence (enum memmodel)
7387 : : EXP is the CALL_EXPR. */
7388 : :
7389 : : static void
7390 : 693 : expand_builtin_atomic_thread_fence (tree exp)
7391 : : {
7392 : 693 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7393 : 693 : expand_mem_thread_fence (model);
7394 : 693 : }
7395 : :
7396 : : /* Expand the __atomic_signal_fence intrinsic:
7397 : : void __atomic_signal_fence (enum memmodel)
7398 : : EXP is the CALL_EXPR. */
7399 : :
7400 : : static void
7401 : 60 : expand_builtin_atomic_signal_fence (tree exp)
7402 : : {
7403 : 60 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7404 : 60 : expand_mem_signal_fence (model);
7405 : 60 : }
7406 : :
7407 : : /* Expand the __sync_synchronize intrinsic. */
7408 : :
7409 : : static void
7410 : 281 : expand_builtin_sync_synchronize (void)
7411 : : {
7412 : 0 : expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
7413 : 0 : }
7414 : :
7415 : : static rtx
7416 : 3 : expand_builtin_thread_pointer (tree exp, rtx target)
7417 : : {
7418 : 3 : enum insn_code icode;
7419 : 3 : if (!validate_arglist (exp, VOID_TYPE))
7420 : 0 : return const0_rtx;
7421 : 3 : icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
7422 : 3 : if (icode != CODE_FOR_nothing)
7423 : : {
7424 : 3 : class expand_operand op;
7425 : : /* If the target is not sutitable then create a new target. */
7426 : 3 : if (target == NULL_RTX
7427 : 3 : || !REG_P (target)
7428 : 6 : || GET_MODE (target) != Pmode)
7429 : 0 : target = gen_reg_rtx (Pmode);
7430 : 3 : create_output_operand (&op, target, Pmode);
7431 : 3 : expand_insn (icode, 1, &op);
7432 : 3 : return target;
7433 : : }
7434 : 0 : error ("%<__builtin_thread_pointer%> is not supported on this target");
7435 : 0 : return const0_rtx;
7436 : : }
7437 : :
7438 : : static void
7439 : 0 : expand_builtin_set_thread_pointer (tree exp)
7440 : : {
7441 : 0 : enum insn_code icode;
7442 : 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7443 : : return;
7444 : 0 : icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
7445 : 0 : if (icode != CODE_FOR_nothing)
7446 : : {
7447 : 0 : class expand_operand op;
7448 : 0 : rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
7449 : 0 : Pmode, EXPAND_NORMAL);
7450 : 0 : create_input_operand (&op, val, Pmode);
7451 : 0 : expand_insn (icode, 1, &op);
7452 : 0 : return;
7453 : : }
7454 : 0 : error ("%<__builtin_set_thread_pointer%> is not supported on this target");
7455 : : }
7456 : :
7457 : :
7458 : : /* Emit code to restore the current value of stack. */
7459 : :
7460 : : static void
7461 : 1669 : expand_stack_restore (tree var)
7462 : : {
7463 : 1669 : rtx_insn *prev;
7464 : 1669 : rtx sa = expand_normal (var);
7465 : :
7466 : 1669 : sa = convert_memory_address (Pmode, sa);
7467 : :
7468 : 1669 : prev = get_last_insn ();
7469 : 1669 : emit_stack_restore (SAVE_BLOCK, sa);
7470 : :
7471 : 1669 : record_new_stack_level ();
7472 : :
7473 : 1669 : fixup_args_size_notes (prev, get_last_insn (), 0);
7474 : 1669 : }
7475 : :
7476 : : /* Emit code to save the current value of stack. */
7477 : :
7478 : : static rtx
7479 : 1810 : expand_stack_save (void)
7480 : : {
7481 : 1810 : rtx ret = NULL_RTX;
7482 : :
7483 : 0 : emit_stack_save (SAVE_BLOCK, &ret);
7484 : 1810 : return ret;
7485 : : }
7486 : :
7487 : : /* Emit code to get the openacc gang, worker or vector id or size. */
7488 : :
7489 : : static rtx
7490 : 356 : expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7491 : : {
7492 : 356 : const char *name;
7493 : 356 : rtx fallback_retval;
7494 : 356 : rtx_insn *(*gen_fn) (rtx, rtx);
7495 : 356 : switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7496 : : {
7497 : 244 : case BUILT_IN_GOACC_PARLEVEL_ID:
7498 : 244 : name = "__builtin_goacc_parlevel_id";
7499 : 244 : fallback_retval = const0_rtx;
7500 : 244 : gen_fn = targetm.gen_oacc_dim_pos;
7501 : 244 : break;
7502 : 112 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
7503 : 112 : name = "__builtin_goacc_parlevel_size";
7504 : 112 : fallback_retval = const1_rtx;
7505 : 112 : gen_fn = targetm.gen_oacc_dim_size;
7506 : 112 : break;
7507 : 0 : default:
7508 : 0 : gcc_unreachable ();
7509 : : }
7510 : :
7511 : 356 : if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7512 : : {
7513 : 8 : error ("%qs only supported in OpenACC code", name);
7514 : 8 : return const0_rtx;
7515 : : }
7516 : :
7517 : 348 : tree arg = CALL_EXPR_ARG (exp, 0);
7518 : 348 : if (TREE_CODE (arg) != INTEGER_CST)
7519 : : {
7520 : 8 : error ("non-constant argument 0 to %qs", name);
7521 : 8 : return const0_rtx;
7522 : : }
7523 : :
7524 : 340 : int dim = TREE_INT_CST_LOW (arg);
7525 : 340 : switch (dim)
7526 : : {
7527 : 324 : case GOMP_DIM_GANG:
7528 : 324 : case GOMP_DIM_WORKER:
7529 : 324 : case GOMP_DIM_VECTOR:
7530 : 324 : break;
7531 : 16 : default:
7532 : 16 : error ("illegal argument 0 to %qs", name);
7533 : 16 : return const0_rtx;
7534 : : }
7535 : :
7536 : 324 : if (ignore)
7537 : : return target;
7538 : :
7539 : 180 : if (target == NULL_RTX)
7540 : 0 : target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7541 : :
7542 : 180 : if (!targetm.have_oacc_dim_size ())
7543 : : {
7544 : 180 : emit_move_insn (target, fallback_retval);
7545 : 180 : return target;
7546 : : }
7547 : :
7548 : 0 : rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7549 : 0 : emit_insn (gen_fn (reg, GEN_INT (dim)));
7550 : 0 : if (reg != target)
7551 : 0 : emit_move_insn (target, reg);
7552 : :
7553 : : return target;
7554 : : }
7555 : :
7556 : : /* Expand a string compare operation using a sequence of char comparison
7557 : : to get rid of the calling overhead, with result going to TARGET if
7558 : : that's convenient.
7559 : :
7560 : : VAR_STR is the variable string source;
7561 : : CONST_STR is the constant string source;
7562 : : LENGTH is the number of chars to compare;
7563 : : CONST_STR_N indicates which source string is the constant string;
7564 : : IS_MEMCMP indicates whether it's a memcmp or strcmp.
7565 : :
7566 : : to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7567 : :
7568 : : target = (int) (unsigned char) var_str[0]
7569 : : - (int) (unsigned char) const_str[0];
7570 : : if (target != 0)
7571 : : goto ne_label;
7572 : : ...
7573 : : target = (int) (unsigned char) var_str[length - 2]
7574 : : - (int) (unsigned char) const_str[length - 2];
7575 : : if (target != 0)
7576 : : goto ne_label;
7577 : : target = (int) (unsigned char) var_str[length - 1]
7578 : : - (int) (unsigned char) const_str[length - 1];
7579 : : ne_label:
7580 : : */
7581 : :
7582 : : static rtx
7583 : 642 : inline_string_cmp (rtx target, tree var_str, const char *const_str,
7584 : : unsigned HOST_WIDE_INT length,
7585 : : int const_str_n, machine_mode mode)
7586 : : {
7587 : 642 : HOST_WIDE_INT offset = 0;
7588 : 642 : rtx var_rtx_array
7589 : 642 : = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7590 : 642 : rtx var_rtx = NULL_RTX;
7591 : 642 : rtx const_rtx = NULL_RTX;
7592 : 642 : rtx result = target ? target : gen_reg_rtx (mode);
7593 : 642 : rtx_code_label *ne_label = gen_label_rtx ();
7594 : 642 : tree unit_type_node = unsigned_char_type_node;
7595 : 642 : scalar_int_mode unit_mode
7596 : 642 : = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7597 : :
7598 : 642 : start_sequence ();
7599 : :
7600 : 2337 : for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7601 : : {
7602 : 1695 : var_rtx
7603 : 1695 : = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7604 : 1695 : const_rtx = c_readstr (const_str + offset, unit_mode);
7605 : 1695 : rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7606 : 1545 : rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7607 : :
7608 : 1695 : op0 = convert_modes (mode, unit_mode, op0, 1);
7609 : 1695 : op1 = convert_modes (mode, unit_mode, op1, 1);
7610 : 1695 : rtx diff = expand_simple_binop (mode, MINUS, op0, op1,
7611 : : result, 1, OPTAB_WIDEN);
7612 : :
7613 : : /* Force the difference into result register. We cannot reassign
7614 : : result here ("result = diff") or we may end up returning
7615 : : uninitialized result when expand_simple_binop allocates a new
7616 : : pseudo-register for returning. */
7617 : 1695 : if (diff != result)
7618 : 0 : emit_move_insn (result, diff);
7619 : :
7620 : 1695 : if (i < length - 1)
7621 : 1053 : emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7622 : : mode, true, ne_label);
7623 : 3390 : offset += GET_MODE_SIZE (unit_mode);
7624 : : }
7625 : :
7626 : 642 : emit_label (ne_label);
7627 : 642 : rtx_insn *insns = end_sequence ();
7628 : 642 : emit_insn (insns);
7629 : :
7630 : 642 : return result;
7631 : : }
7632 : :
7633 : : /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7634 : : to TARGET if that's convenient.
7635 : : If the call is not been inlined, return NULL_RTX. */
7636 : :
7637 : : static rtx
7638 : 183220 : inline_expand_builtin_bytecmp (tree exp, rtx target)
7639 : : {
7640 : 183220 : tree fndecl = get_callee_fndecl (exp);
7641 : 183220 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7642 : 183220 : bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7643 : :
7644 : : /* Do NOT apply this inlining expansion when optimizing for size or
7645 : : optimization level below 2 or if unused *cmp hasn't been DCEd. */
7646 : 183220 : if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7647 : 160883 : return NULL_RTX;
7648 : :
7649 : 22337 : gcc_checking_assert (fcode == BUILT_IN_STRCMP
7650 : : || fcode == BUILT_IN_STRNCMP
7651 : : || fcode == BUILT_IN_MEMCMP);
7652 : :
7653 : : /* On a target where the type of the call (int) has same or narrower presicion
7654 : : than unsigned char, give up the inlining expansion. */
7655 : 22337 : if (TYPE_PRECISION (unsigned_char_type_node)
7656 : 22337 : >= TYPE_PRECISION (TREE_TYPE (exp)))
7657 : : return NULL_RTX;
7658 : :
7659 : 22337 : tree arg1 = CALL_EXPR_ARG (exp, 0);
7660 : 22337 : tree arg2 = CALL_EXPR_ARG (exp, 1);
7661 : 22337 : tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7662 : :
7663 : 22337 : unsigned HOST_WIDE_INT len1 = 0;
7664 : 22337 : unsigned HOST_WIDE_INT len2 = 0;
7665 : 22337 : unsigned HOST_WIDE_INT len3 = 0;
7666 : :
7667 : : /* Get the object representation of the initializers of ARG1 and ARG2
7668 : : as strings, provided they refer to constant objects, with their byte
7669 : : sizes in LEN1 and LEN2, respectively. */
7670 : 22337 : const char *bytes1 = getbyterep (arg1, &len1);
7671 : 22337 : const char *bytes2 = getbyterep (arg2, &len2);
7672 : :
7673 : : /* Fail if neither argument refers to an initialized constant. */
7674 : 22337 : if (!bytes1 && !bytes2)
7675 : : return NULL_RTX;
7676 : :
7677 : 18431 : if (is_ncmp)
7678 : : {
7679 : : /* Fail if the memcmp/strncmp bound is not a constant. */
7680 : 1352 : if (!tree_fits_uhwi_p (len3_tree))
7681 : : return NULL_RTX;
7682 : :
7683 : 1023 : len3 = tree_to_uhwi (len3_tree);
7684 : :
7685 : 1023 : if (fcode == BUILT_IN_MEMCMP)
7686 : : {
7687 : : /* Fail if the memcmp bound is greater than the size of either
7688 : : of the two constant objects. */
7689 : 520 : if ((bytes1 && len1 < len3)
7690 : 520 : || (bytes2 && len2 < len3))
7691 : : return NULL_RTX;
7692 : : }
7693 : : }
7694 : :
7695 : : if (fcode != BUILT_IN_MEMCMP)
7696 : : {
7697 : : /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7698 : : and LEN2 to the length of the nul-terminated string stored
7699 : : in each. */
7700 : 17582 : if (bytes1 != NULL)
7701 : 236 : len1 = strnlen (bytes1, len1) + 1;
7702 : 17582 : if (bytes2 != NULL)
7703 : 17349 : len2 = strnlen (bytes2, len2) + 1;
7704 : : }
7705 : :
7706 : : /* See inline_string_cmp. */
7707 : 18098 : int const_str_n;
7708 : 18098 : if (!len1)
7709 : : const_str_n = 2;
7710 : 241 : else if (!len2)
7711 : : const_str_n = 1;
7712 : 3 : else if (len2 > len1)
7713 : : const_str_n = 1;
7714 : : else
7715 : : const_str_n = 2;
7716 : :
7717 : : /* For strncmp only, compute the new bound as the smallest of
7718 : : the lengths of the two strings (plus 1) and the bound provided
7719 : : to the function. */
7720 : 17859 : unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7721 : 18098 : if (is_ncmp && len3 < bound)
7722 : 529 : bound = len3;
7723 : :
7724 : : /* If the bound of the comparison is larger than the threshold,
7725 : : do nothing. */
7726 : 18098 : if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7727 : : return NULL_RTX;
7728 : :
7729 : 642 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7730 : :
7731 : : /* Now, start inline expansion the call. */
7732 : 1227 : return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7733 : : (const_str_n == 1) ? bytes1 : bytes2, bound,
7734 : 642 : const_str_n, mode);
7735 : : }
7736 : :
7737 : : /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7738 : : represents the size of the first argument to that call, or VOIDmode
7739 : : if the argument is a pointer. IGNORE will be true if the result
7740 : : isn't used. */
7741 : : static rtx
7742 : 34 : expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7743 : : bool ignore)
7744 : : {
7745 : 34 : rtx val, failsafe;
7746 : 34 : unsigned nargs = call_expr_nargs (exp);
7747 : :
7748 : 34 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7749 : :
7750 : 34 : if (mode == VOIDmode)
7751 : : {
7752 : 4 : mode = TYPE_MODE (TREE_TYPE (arg0));
7753 : 4 : gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7754 : : }
7755 : :
7756 : 34 : val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7757 : :
7758 : : /* An optional second argument can be used as a failsafe value on
7759 : : some machines. If it isn't present, then the failsafe value is
7760 : : assumed to be 0. */
7761 : 34 : if (nargs > 1)
7762 : : {
7763 : 4 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7764 : 4 : failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7765 : : }
7766 : : else
7767 : 30 : failsafe = const0_rtx;
7768 : :
7769 : : /* If the result isn't used, the behavior is undefined. It would be
7770 : : nice to emit a warning here, but path splitting means this might
7771 : : happen with legitimate code. So simply drop the builtin
7772 : : expansion in that case; we've handled any side-effects above. */
7773 : 34 : if (ignore)
7774 : 0 : return const0_rtx;
7775 : :
7776 : : /* If we don't have a suitable target, create one to hold the result. */
7777 : 34 : if (target == NULL || GET_MODE (target) != mode)
7778 : 0 : target = gen_reg_rtx (mode);
7779 : :
7780 : 34 : if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7781 : 0 : val = convert_modes (mode, VOIDmode, val, false);
7782 : :
7783 : 34 : return targetm.speculation_safe_value (mode, target, val, failsafe);
7784 : : }
7785 : :
7786 : : /* Expand CRC* or REV_CRC* built-ins. */
7787 : :
7788 : : rtx
7789 : 33 : expand_builtin_crc_table_based (internal_fn fn, scalar_mode crc_mode,
7790 : : scalar_mode data_mode, machine_mode mode,
7791 : : tree exp, rtx target)
7792 : : {
7793 : 33 : tree rhs1 = CALL_EXPR_ARG (exp, 0); // crc
7794 : 33 : tree rhs2 = CALL_EXPR_ARG (exp, 1); // data
7795 : 33 : tree rhs3 = CALL_EXPR_ARG (exp, 2); // polynomial
7796 : :
7797 : 33 : if (!target || mode == VOIDmode)
7798 : 0 : target = gen_reg_rtx (crc_mode);
7799 : :
7800 : 33 : rtx op1 = expand_normal (rhs1);
7801 : 33 : rtx op2 = expand_normal (rhs2);
7802 : 33 : gcc_assert (TREE_CODE (rhs3) == INTEGER_CST);
7803 : 33 : rtx op3 = gen_int_mode (TREE_INT_CST_LOW (rhs3), crc_mode);
7804 : :
7805 : 33 : if (CONST_INT_P (op2))
7806 : 33 : op2 = gen_int_mode (INTVAL (op2), crc_mode);
7807 : :
7808 : 33 : if (fn == IFN_CRC)
7809 : 17 : expand_crc_table_based (target, op1, op2, op3, data_mode);
7810 : : else
7811 : : /* If it's IFN_CRC_REV generate bit-reversed CRC. */
7812 : 16 : expand_reversed_crc_table_based (target, op1, op2, op3,
7813 : : data_mode,
7814 : : generate_reflecting_code_standard);
7815 : 33 : return target;
7816 : : }
7817 : :
7818 : : /* Expand an expression EXP that calls a built-in function,
7819 : : with result going to TARGET if that's convenient
7820 : : (and in mode MODE if that's convenient).
7821 : : SUBTARGET may be used as the target for computing one of EXP's operands.
7822 : : IGNORE is nonzero if the value is to be ignored. */
7823 : :
7824 : : rtx
7825 : 1949152 : expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7826 : : int ignore)
7827 : : {
7828 : 1949152 : tree fndecl = get_callee_fndecl (exp);
7829 : 1949152 : machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7830 : 1949152 : int flags;
7831 : :
7832 : 1949152 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7833 : 171097 : return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7834 : :
7835 : : /* When ASan is enabled, we don't want to expand some memory/string
7836 : : builtins and rely on libsanitizer's hooks. This allows us to avoid
7837 : : redundant checks and be sure, that possible overflow will be detected
7838 : : by ASan. */
7839 : :
7840 : 1778055 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7841 : 1778055 : if (param_asan_kernel_mem_intrinsic_prefix
7842 : 1778055 : && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7843 : : | SANITIZE_KERNEL_HWADDRESS))
7844 : 42 : switch (fcode)
7845 : : {
7846 : 42 : rtx save_decl_rtl, ret;
7847 : 42 : case BUILT_IN_MEMCPY:
7848 : 42 : case BUILT_IN_MEMMOVE:
7849 : 42 : case BUILT_IN_MEMSET:
7850 : 42 : save_decl_rtl = DECL_RTL (fndecl);
7851 : 42 : DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7852 : 42 : ret = expand_call (exp, target, ignore);
7853 : 42 : DECL_RTL (fndecl) = save_decl_rtl;
7854 : 42 : return ret;
7855 : : default:
7856 : : break;
7857 : : }
7858 : 1778013 : if (sanitize_flags_p (SANITIZE_ADDRESS | SANITIZE_HWADDRESS)
7859 : 1778013 : && asan_intercepted_p (fcode))
7860 : 713 : return expand_call (exp, target, ignore);
7861 : :
7862 : : /* When not optimizing, generate calls to library functions for a certain
7863 : : set of builtins. */
7864 : 1777300 : if (!optimize
7865 : 418049 : && !called_as_built_in (fndecl)
7866 : 141757 : && fcode != BUILT_IN_FORK
7867 : 141757 : && fcode != BUILT_IN_EXECL
7868 : 141746 : && fcode != BUILT_IN_EXECV
7869 : 141746 : && fcode != BUILT_IN_EXECLP
7870 : 141744 : && fcode != BUILT_IN_EXECLE
7871 : 141744 : && fcode != BUILT_IN_EXECVP
7872 : 141743 : && fcode != BUILT_IN_EXECVE
7873 : 141743 : && fcode != BUILT_IN_CLEAR_CACHE
7874 : 141740 : && !ALLOCA_FUNCTION_CODE_P (fcode)
7875 : 141686 : && fcode != BUILT_IN_FREE
7876 : 139604 : && (fcode != BUILT_IN_MEMSET
7877 : 55885 : || !(flag_inline_stringops & ILSOP_MEMSET))
7878 : 139601 : && (fcode != BUILT_IN_MEMCPY
7879 : 918 : || !(flag_inline_stringops & ILSOP_MEMCPY))
7880 : 139465 : && (fcode != BUILT_IN_MEMMOVE
7881 : 347 : || !(flag_inline_stringops & ILSOP_MEMMOVE))
7882 : 1916765 : && (fcode != BUILT_IN_MEMCMP
7883 : 2504 : || !(flag_inline_stringops & ILSOP_MEMCMP)))
7884 : 139417 : return expand_call (exp, target, ignore);
7885 : :
7886 : : /* The built-in function expanders test for target == const0_rtx
7887 : : to determine whether the function's result will be ignored. */
7888 : 1637883 : if (ignore)
7889 : 948009 : target = const0_rtx;
7890 : :
7891 : : /* If the result of a pure or const built-in function is ignored, and
7892 : : none of its arguments are volatile, we can avoid expanding the
7893 : : built-in call and just evaluate the arguments for side-effects. */
7894 : 1637883 : if (target == const0_rtx
7895 : 948009 : && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7896 : 1644228 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
7897 : : {
7898 : 6 : bool volatilep = false;
7899 : 6 : tree arg;
7900 : 6 : call_expr_arg_iterator iter;
7901 : :
7902 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7903 : 8 : if (TREE_THIS_VOLATILE (arg))
7904 : : {
7905 : : volatilep = true;
7906 : : break;
7907 : : }
7908 : :
7909 : 6 : if (! volatilep)
7910 : : {
7911 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7912 : 8 : expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7913 : 6 : return const0_rtx;
7914 : : }
7915 : : }
7916 : :
7917 : 1637877 : switch (fcode)
7918 : : {
7919 : 4 : CASE_FLT_FN (BUILT_IN_FABS):
7920 : 4 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7921 : 4 : case BUILT_IN_FABSD32:
7922 : 4 : case BUILT_IN_FABSD64:
7923 : 4 : case BUILT_IN_FABSD128:
7924 : 4 : case BUILT_IN_FABSD64X:
7925 : 4 : target = expand_builtin_fabs (exp, target, subtarget);
7926 : 4 : if (target)
7927 : 775125 : return target;
7928 : 862752 : break;
7929 : :
7930 : 11674 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
7931 : 11674 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7932 : 11674 : target = expand_builtin_copysign (exp, target, subtarget);
7933 : 11674 : if (target)
7934 : : return target;
7935 : : break;
7936 : :
7937 : : /* Just do a normal library call if we were unable to fold
7938 : : the values. */
7939 : : CASE_FLT_FN (BUILT_IN_CABS):
7940 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7941 : : break;
7942 : :
7943 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
7944 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7945 : 364 : target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7946 : 364 : if (target)
7947 : : return target;
7948 : : break;
7949 : :
7950 : 235 : CASE_FLT_FN (BUILT_IN_ILOGB):
7951 : 235 : if (! flag_unsafe_math_optimizations)
7952 : : break;
7953 : 4 : gcc_fallthrough ();
7954 : 4 : CASE_FLT_FN (BUILT_IN_ISINF):
7955 : 4 : CASE_FLT_FN (BUILT_IN_FINITE):
7956 : 4 : case BUILT_IN_ISFINITE:
7957 : 4 : case BUILT_IN_ISNORMAL:
7958 : 4 : target = expand_builtin_interclass_mathfn (exp, target);
7959 : 4 : if (target)
7960 : : return target;
7961 : : break;
7962 : :
7963 : 823 : case BUILT_IN_ISSIGNALING:
7964 : 823 : target = expand_builtin_issignaling (exp, target);
7965 : 823 : if (target)
7966 : : return target;
7967 : : break;
7968 : :
7969 : 231 : CASE_FLT_FN (BUILT_IN_ICEIL):
7970 : 231 : CASE_FLT_FN (BUILT_IN_LCEIL):
7971 : 231 : CASE_FLT_FN (BUILT_IN_LLCEIL):
7972 : 231 : CASE_FLT_FN (BUILT_IN_LFLOOR):
7973 : 231 : CASE_FLT_FN (BUILT_IN_IFLOOR):
7974 : 231 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
7975 : 231 : target = expand_builtin_int_roundingfn (exp, target);
7976 : 231 : if (target)
7977 : : return target;
7978 : : break;
7979 : :
7980 : 590 : CASE_FLT_FN (BUILT_IN_IRINT):
7981 : 590 : CASE_FLT_FN (BUILT_IN_LRINT):
7982 : 590 : CASE_FLT_FN (BUILT_IN_LLRINT):
7983 : 590 : CASE_FLT_FN (BUILT_IN_IROUND):
7984 : 590 : CASE_FLT_FN (BUILT_IN_LROUND):
7985 : 590 : CASE_FLT_FN (BUILT_IN_LLROUND):
7986 : 590 : target = expand_builtin_int_roundingfn_2 (exp, target);
7987 : 590 : if (target)
7988 : : return target;
7989 : : break;
7990 : :
7991 : 279 : CASE_FLT_FN (BUILT_IN_POWI):
7992 : 279 : target = expand_builtin_powi (exp, target);
7993 : 279 : if (target)
7994 : : return target;
7995 : : break;
7996 : :
7997 : 157 : CASE_FLT_FN (BUILT_IN_CEXPI):
7998 : 157 : target = expand_builtin_cexpi (exp, target);
7999 : 157 : gcc_assert (target);
8000 : : return target;
8001 : :
8002 : 991 : CASE_FLT_FN (BUILT_IN_SIN):
8003 : 991 : CASE_FLT_FN (BUILT_IN_COS):
8004 : 991 : if (! flag_unsafe_math_optimizations)
8005 : : break;
8006 : 47 : target = expand_builtin_mathfn_3 (exp, target, subtarget);
8007 : 47 : if (target)
8008 : : return target;
8009 : : break;
8010 : :
8011 : 151 : CASE_FLT_FN (BUILT_IN_SINCOS):
8012 : 151 : if (! flag_unsafe_math_optimizations)
8013 : : break;
8014 : 3 : target = expand_builtin_sincos (exp);
8015 : 3 : if (target)
8016 : : return target;
8017 : : break;
8018 : :
8019 : 60 : case BUILT_IN_FEGETROUND:
8020 : 60 : target = expand_builtin_fegetround (exp, target, target_mode);
8021 : 60 : if (target)
8022 : : return target;
8023 : : break;
8024 : :
8025 : 1063 : case BUILT_IN_FECLEAREXCEPT:
8026 : 1063 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8027 : : feclearexcept_optab);
8028 : 1063 : if (target)
8029 : : return target;
8030 : : break;
8031 : :
8032 : 831 : case BUILT_IN_FERAISEEXCEPT:
8033 : 831 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8034 : : feraiseexcept_optab);
8035 : 831 : if (target)
8036 : : return target;
8037 : : break;
8038 : :
8039 : 464 : case BUILT_IN_APPLY_ARGS:
8040 : 464 : return expand_builtin_apply_args ();
8041 : :
8042 : : /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
8043 : : FUNCTION with a copy of the parameters described by
8044 : : ARGUMENTS, and ARGSIZE. It returns a block of memory
8045 : : allocated on the stack into which is stored all the registers
8046 : : that might possibly be used for returning the result of a
8047 : : function. ARGUMENTS is the value returned by
8048 : : __builtin_apply_args. ARGSIZE is the number of bytes of
8049 : : arguments that must be copied. ??? How should this value be
8050 : : computed? We'll also need a safe worst case value for varargs
8051 : : functions. */
8052 : 483 : case BUILT_IN_APPLY:
8053 : 483 : if (!validate_arglist (exp, POINTER_TYPE,
8054 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
8055 : 483 : && !validate_arglist (exp, REFERENCE_TYPE,
8056 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8057 : 0 : return const0_rtx;
8058 : : else
8059 : : {
8060 : 483 : rtx ops[3];
8061 : :
8062 : 483 : ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
8063 : 483 : ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
8064 : 483 : ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
8065 : :
8066 : 483 : return expand_builtin_apply (ops[0], ops[1], ops[2]);
8067 : : }
8068 : :
8069 : : /* __builtin_return (RESULT) causes the function to return the
8070 : : value described by RESULT. RESULT is address of the block of
8071 : : memory returned by __builtin_apply. */
8072 : 379 : case BUILT_IN_RETURN:
8073 : 379 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8074 : 379 : expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
8075 : 379 : return const0_rtx;
8076 : :
8077 : 0 : case BUILT_IN_SAVEREGS:
8078 : 0 : return expand_builtin_saveregs ();
8079 : :
8080 : 16 : case BUILT_IN_VA_ARG_PACK:
8081 : : /* All valid uses of __builtin_va_arg_pack () are removed during
8082 : : inlining. */
8083 : 16 : error ("invalid use of %<__builtin_va_arg_pack ()%>");
8084 : 16 : return const0_rtx;
8085 : :
8086 : 0 : case BUILT_IN_VA_ARG_PACK_LEN:
8087 : : /* All valid uses of __builtin_va_arg_pack_len () are removed during
8088 : : inlining. */
8089 : 0 : error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
8090 : 0 : return const0_rtx;
8091 : :
8092 : : /* Return the address of the first anonymous stack arg. */
8093 : 150 : case BUILT_IN_NEXT_ARG:
8094 : 150 : if (fold_builtin_next_arg (exp, false))
8095 : 0 : return const0_rtx;
8096 : 150 : return expand_builtin_next_arg ();
8097 : :
8098 : 28 : case BUILT_IN_CLEAR_CACHE:
8099 : 28 : expand_builtin___clear_cache (exp);
8100 : 28 : return const0_rtx;
8101 : :
8102 : 0 : case BUILT_IN_CLASSIFY_TYPE:
8103 : 0 : return expand_builtin_classify_type (exp);
8104 : :
8105 : 0 : case BUILT_IN_CONSTANT_P:
8106 : 0 : return const0_rtx;
8107 : :
8108 : 15582 : case BUILT_IN_FRAME_ADDRESS:
8109 : 15582 : case BUILT_IN_RETURN_ADDRESS:
8110 : 15582 : return expand_builtin_frame_address (fndecl, exp);
8111 : :
8112 : 540 : case BUILT_IN_STACK_ADDRESS:
8113 : 540 : return expand_builtin_stack_address ();
8114 : :
8115 : 2159 : case BUILT_IN___STRUB_ENTER:
8116 : 2159 : target = expand_builtin_strub_enter (exp);
8117 : 2159 : if (target)
8118 : : return target;
8119 : : break;
8120 : :
8121 : 1072 : case BUILT_IN___STRUB_UPDATE:
8122 : 1072 : target = expand_builtin_strub_update (exp);
8123 : 1072 : if (target)
8124 : : return target;
8125 : : break;
8126 : :
8127 : 2729 : case BUILT_IN___STRUB_LEAVE:
8128 : 2729 : target = expand_builtin_strub_leave (exp);
8129 : 2729 : if (target)
8130 : : return target;
8131 : : break;
8132 : :
8133 : : /* Returns the address of the area where the structure is returned.
8134 : : 0 otherwise. */
8135 : 0 : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
8136 : 0 : if (call_expr_nargs (exp) != 0
8137 : 0 : || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
8138 : 0 : || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
8139 : 0 : return const0_rtx;
8140 : : else
8141 : 0 : return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
8142 : :
8143 : 25653 : CASE_BUILT_IN_ALLOCA:
8144 : 25653 : target = expand_builtin_alloca (exp);
8145 : 25653 : if (target)
8146 : : return target;
8147 : : break;
8148 : :
8149 : 203 : case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
8150 : 203 : return expand_asan_emit_allocas_unpoison (exp);
8151 : :
8152 : 1810 : case BUILT_IN_STACK_SAVE:
8153 : 1810 : return expand_stack_save ();
8154 : :
8155 : 1669 : case BUILT_IN_STACK_RESTORE:
8156 : 1669 : expand_stack_restore (CALL_EXPR_ARG (exp, 0));
8157 : 1669 : return const0_rtx;
8158 : :
8159 : 1164 : case BUILT_IN_BSWAP16:
8160 : 1164 : case BUILT_IN_BSWAP32:
8161 : 1164 : case BUILT_IN_BSWAP64:
8162 : 1164 : case BUILT_IN_BSWAP128:
8163 : 1164 : target = expand_builtin_bswap (target_mode, exp, target, subtarget);
8164 : 1164 : if (target)
8165 : : return target;
8166 : : break;
8167 : :
8168 : 0 : CASE_INT_FN (BUILT_IN_FFS):
8169 : 0 : target = expand_builtin_unop (target_mode, exp, target,
8170 : : subtarget, ffs_optab);
8171 : 0 : if (target)
8172 : : return target;
8173 : : break;
8174 : :
8175 : 183 : CASE_INT_FN (BUILT_IN_CLZ):
8176 : 183 : target = expand_builtin_unop (target_mode, exp, target,
8177 : : subtarget, clz_optab);
8178 : 183 : if (target)
8179 : : return target;
8180 : : break;
8181 : :
8182 : 50 : CASE_INT_FN (BUILT_IN_CTZ):
8183 : 50 : target = expand_builtin_unop (target_mode, exp, target,
8184 : : subtarget, ctz_optab);
8185 : 50 : if (target)
8186 : : return target;
8187 : : break;
8188 : :
8189 : 90 : CASE_INT_FN (BUILT_IN_CLRSB):
8190 : 90 : target = expand_builtin_unop (target_mode, exp, target,
8191 : : subtarget, clrsb_optab);
8192 : 90 : if (target)
8193 : : return target;
8194 : : break;
8195 : :
8196 : 417 : CASE_INT_FN (BUILT_IN_POPCOUNT):
8197 : 417 : target = expand_builtin_unop (target_mode, exp, target,
8198 : : subtarget, popcount_optab);
8199 : 417 : if (target)
8200 : : return target;
8201 : : break;
8202 : :
8203 : 11 : CASE_INT_FN (BUILT_IN_PARITY):
8204 : 11 : target = expand_builtin_unop (target_mode, exp, target,
8205 : : subtarget, parity_optab);
8206 : 11 : if (target)
8207 : : return target;
8208 : : break;
8209 : :
8210 : 13426 : case BUILT_IN_STRLEN:
8211 : 13426 : target = expand_builtin_strlen (exp, target, target_mode);
8212 : 13426 : if (target)
8213 : : return target;
8214 : : break;
8215 : :
8216 : 580 : case BUILT_IN_STRNLEN:
8217 : 580 : target = expand_builtin_strnlen (exp, target, target_mode);
8218 : 580 : if (target)
8219 : : return target;
8220 : : break;
8221 : :
8222 : 1862 : case BUILT_IN_STRCPY:
8223 : 1862 : target = expand_builtin_strcpy (exp, target);
8224 : 1862 : if (target)
8225 : : return target;
8226 : : break;
8227 : :
8228 : 2166 : case BUILT_IN_STRNCPY:
8229 : 2166 : target = expand_builtin_strncpy (exp, target);
8230 : 2166 : if (target)
8231 : : return target;
8232 : : break;
8233 : :
8234 : 454 : case BUILT_IN_STPCPY:
8235 : 454 : target = expand_builtin_stpcpy (exp, target, mode);
8236 : 454 : if (target)
8237 : : return target;
8238 : : break;
8239 : :
8240 : 88522 : case BUILT_IN_MEMCPY:
8241 : 88522 : target = expand_builtin_memcpy (exp, target);
8242 : 88522 : if (target)
8243 : : return target;
8244 : : break;
8245 : :
8246 : 16008 : case BUILT_IN_MEMMOVE:
8247 : 16008 : target = expand_builtin_memmove (exp, target);
8248 : 16008 : if (target)
8249 : : return target;
8250 : : break;
8251 : :
8252 : 1637 : case BUILT_IN_MEMPCPY:
8253 : 1637 : target = expand_builtin_mempcpy (exp, target);
8254 : 1637 : if (target)
8255 : : return target;
8256 : : break;
8257 : :
8258 : 35330 : case BUILT_IN_MEMSET:
8259 : 35330 : target = expand_builtin_memset (exp, target, mode);
8260 : 35330 : if (target)
8261 : : return target;
8262 : : break;
8263 : :
8264 : 0 : case BUILT_IN_BZERO:
8265 : 0 : target = expand_builtin_bzero (exp);
8266 : 0 : if (target)
8267 : : return target;
8268 : : break;
8269 : :
8270 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8271 : : back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
8272 : : when changing it to a strcmp call. */
8273 : 381 : case BUILT_IN_STRCMP_EQ:
8274 : 381 : target = expand_builtin_memcmp (exp, target, true);
8275 : 381 : if (target)
8276 : : return target;
8277 : :
8278 : : /* Change this call back to a BUILT_IN_STRCMP. */
8279 : 22 : TREE_OPERAND (exp, 1)
8280 : 22 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
8281 : :
8282 : : /* Delete the last parameter. */
8283 : 22 : unsigned int i;
8284 : 22 : vec<tree, va_gc> *arg_vec;
8285 : 22 : vec_alloc (arg_vec, 2);
8286 : 88 : for (i = 0; i < 2; i++)
8287 : 44 : arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
8288 : 22 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
8289 : : /* FALLTHROUGH */
8290 : :
8291 : 127929 : case BUILT_IN_STRCMP:
8292 : 127929 : target = expand_builtin_strcmp (exp, target);
8293 : 127929 : if (target)
8294 : : return target;
8295 : : break;
8296 : :
8297 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8298 : : back to a BUILT_IN_STRNCMP. */
8299 : 36 : case BUILT_IN_STRNCMP_EQ:
8300 : 36 : target = expand_builtin_memcmp (exp, target, true);
8301 : 36 : if (target)
8302 : : return target;
8303 : :
8304 : : /* Change it back to a BUILT_IN_STRNCMP. */
8305 : 0 : TREE_OPERAND (exp, 1)
8306 : 0 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
8307 : : /* FALLTHROUGH */
8308 : :
8309 : 2029 : case BUILT_IN_STRNCMP:
8310 : 2029 : target = expand_builtin_strncmp (exp, target, mode);
8311 : 2029 : if (target)
8312 : : return target;
8313 : : break;
8314 : :
8315 : 110920 : case BUILT_IN_BCMP:
8316 : 110920 : case BUILT_IN_MEMCMP:
8317 : 110920 : case BUILT_IN_MEMCMP_EQ:
8318 : 110920 : target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
8319 : 110920 : if (target)
8320 : : return target;
8321 : 70998 : if (fcode == BUILT_IN_MEMCMP_EQ)
8322 : : {
8323 : 18139 : tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
8324 : 18139 : TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
8325 : : }
8326 : : break;
8327 : :
8328 : 0 : case BUILT_IN_SETJMP:
8329 : : /* This should have been lowered to the builtins below. */
8330 : 0 : gcc_unreachable ();
8331 : :
8332 : 841 : case BUILT_IN_SETJMP_SETUP:
8333 : : /* __builtin_setjmp_setup is passed a pointer to an array of five words
8334 : : and the receiver label. */
8335 : 841 : if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8336 : : {
8337 : 841 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8338 : : VOIDmode, EXPAND_NORMAL);
8339 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
8340 : 841 : rtx_insn *label_r = label_rtx (label);
8341 : :
8342 : 841 : expand_builtin_setjmp_setup (buf_addr, label_r);
8343 : 841 : return const0_rtx;
8344 : : }
8345 : : break;
8346 : :
8347 : 841 : case BUILT_IN_SETJMP_RECEIVER:
8348 : : /* __builtin_setjmp_receiver is passed the receiver label. */
8349 : 841 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8350 : : {
8351 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
8352 : 841 : rtx_insn *label_r = label_rtx (label);
8353 : :
8354 : 841 : expand_builtin_setjmp_receiver (label_r);
8355 : 841 : nonlocal_goto_handler_labels
8356 : 1682 : = gen_rtx_INSN_LIST (VOIDmode, label_r,
8357 : 841 : nonlocal_goto_handler_labels);
8358 : : /* ??? Do not let expand_label treat us as such since we would
8359 : : not want to be both on the list of non-local labels and on
8360 : : the list of forced labels. */
8361 : 841 : FORCED_LABEL (label) = 0;
8362 : 841 : return const0_rtx;
8363 : : }
8364 : : break;
8365 : :
8366 : : /* __builtin_longjmp is passed a pointer to an array of five words.
8367 : : It's similar to the C library longjmp function but works with
8368 : : __builtin_setjmp above. */
8369 : 391 : case BUILT_IN_LONGJMP:
8370 : 391 : if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8371 : : {
8372 : 391 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8373 : : VOIDmode, EXPAND_NORMAL);
8374 : 391 : rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
8375 : :
8376 : 391 : if (value != const1_rtx)
8377 : : {
8378 : 0 : error ("%<__builtin_longjmp%> second argument must be 1");
8379 : 0 : return const0_rtx;
8380 : : }
8381 : :
8382 : 391 : expand_builtin_longjmp (buf_addr, value);
8383 : 391 : return const0_rtx;
8384 : : }
8385 : : break;
8386 : :
8387 : 510 : case BUILT_IN_NONLOCAL_GOTO:
8388 : 510 : target = expand_builtin_nonlocal_goto (exp);
8389 : 510 : if (target)
8390 : : return target;
8391 : : break;
8392 : :
8393 : : /* This updates the setjmp buffer that is its argument with the value
8394 : : of the current stack pointer. */
8395 : 0 : case BUILT_IN_UPDATE_SETJMP_BUF:
8396 : 0 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8397 : : {
8398 : 0 : rtx buf_addr
8399 : 0 : = expand_normal (CALL_EXPR_ARG (exp, 0));
8400 : :
8401 : 0 : expand_builtin_update_setjmp_buf (buf_addr);
8402 : 0 : return const0_rtx;
8403 : : }
8404 : : break;
8405 : :
8406 : 36176 : case BUILT_IN_TRAP:
8407 : 36176 : case BUILT_IN_UNREACHABLE_TRAP:
8408 : 36176 : expand_builtin_trap ();
8409 : 36176 : return const0_rtx;
8410 : :
8411 : 5602 : case BUILT_IN_UNREACHABLE:
8412 : 5602 : expand_builtin_unreachable ();
8413 : 5602 : return const0_rtx;
8414 : :
8415 : 1131 : CASE_FLT_FN (BUILT_IN_SIGNBIT):
8416 : 1131 : case BUILT_IN_SIGNBITD32:
8417 : 1131 : case BUILT_IN_SIGNBITD64:
8418 : 1131 : case BUILT_IN_SIGNBITD128:
8419 : 1131 : target = expand_builtin_signbit (exp, target);
8420 : 1131 : if (target)
8421 : : return target;
8422 : : break;
8423 : :
8424 : : /* Various hooks for the DWARF 2 __throw routine. */
8425 : 34 : case BUILT_IN_UNWIND_INIT:
8426 : 34 : expand_builtin_unwind_init ();
8427 : 34 : return const0_rtx;
8428 : 1165 : case BUILT_IN_DWARF_CFA:
8429 : 1165 : return virtual_cfa_rtx;
8430 : : #ifdef DWARF2_UNWIND_INFO
8431 : 48 : case BUILT_IN_DWARF_SP_COLUMN:
8432 : 48 : return expand_builtin_dwarf_sp_column ();
8433 : 8 : case BUILT_IN_INIT_DWARF_REG_SIZES:
8434 : 8 : expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
8435 : 8 : return const0_rtx;
8436 : : #endif
8437 : 19 : case BUILT_IN_FROB_RETURN_ADDR:
8438 : 19 : return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
8439 : 2296 : case BUILT_IN_EXTRACT_RETURN_ADDR:
8440 : 2296 : return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
8441 : 29 : case BUILT_IN_EH_RETURN:
8442 : 58 : expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
8443 : 29 : CALL_EXPR_ARG (exp, 1));
8444 : 29 : return const0_rtx;
8445 : 21 : case BUILT_IN_EH_RETURN_DATA_REGNO:
8446 : 21 : return expand_builtin_eh_return_data_regno (exp);
8447 : 2 : case BUILT_IN_EXTEND_POINTER:
8448 : 2 : return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
8449 : 93985 : case BUILT_IN_EH_POINTER:
8450 : 93985 : return expand_builtin_eh_pointer (exp);
8451 : 4708 : case BUILT_IN_EH_FILTER:
8452 : 4708 : return expand_builtin_eh_filter (exp);
8453 : 99710 : case BUILT_IN_EH_COPY_VALUES:
8454 : 99710 : return expand_builtin_eh_copy_values (exp);
8455 : :
8456 : 20875 : case BUILT_IN_VA_START:
8457 : 20875 : return expand_builtin_va_start (exp);
8458 : 12071 : case BUILT_IN_VA_END:
8459 : 12071 : return expand_builtin_va_end (exp);
8460 : 242 : case BUILT_IN_VA_COPY:
8461 : 242 : return expand_builtin_va_copy (exp);
8462 : 1035 : case BUILT_IN_EXPECT:
8463 : 1035 : return expand_builtin_expect (exp, target);
8464 : 5 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
8465 : 5 : return expand_builtin_expect_with_probability (exp, target);
8466 : 75 : case BUILT_IN_ASSUME_ALIGNED:
8467 : 75 : return expand_builtin_assume_aligned (exp, target);
8468 : 2037 : case BUILT_IN_PREFETCH:
8469 : 2037 : expand_builtin_prefetch (exp);
8470 : 2037 : return const0_rtx;
8471 : :
8472 : 290 : case BUILT_IN_INIT_TRAMPOLINE:
8473 : 290 : return expand_builtin_init_trampoline (exp, true);
8474 : 0 : case BUILT_IN_INIT_HEAP_TRAMPOLINE:
8475 : 0 : return expand_builtin_init_trampoline (exp, false);
8476 : 333 : case BUILT_IN_ADJUST_TRAMPOLINE:
8477 : 333 : return expand_builtin_adjust_trampoline (exp);
8478 : :
8479 : 0 : case BUILT_IN_INIT_DESCRIPTOR:
8480 : 0 : return expand_builtin_init_descriptor (exp);
8481 : 0 : case BUILT_IN_ADJUST_DESCRIPTOR:
8482 : 0 : return expand_builtin_adjust_descriptor (exp);
8483 : :
8484 : : case BUILT_IN_GCC_NESTED_PTR_CREATED:
8485 : : case BUILT_IN_GCC_NESTED_PTR_DELETED:
8486 : : break; /* At present, no expansion, just call the function. */
8487 : :
8488 : 87 : case BUILT_IN_FORK:
8489 : 87 : case BUILT_IN_EXECL:
8490 : 87 : case BUILT_IN_EXECV:
8491 : 87 : case BUILT_IN_EXECLP:
8492 : 87 : case BUILT_IN_EXECLE:
8493 : 87 : case BUILT_IN_EXECVP:
8494 : 87 : case BUILT_IN_EXECVE:
8495 : 87 : target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
8496 : 87 : if (target)
8497 : : return target;
8498 : : break;
8499 : :
8500 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_1:
8501 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_2:
8502 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_4:
8503 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_8:
8504 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_16:
8505 : 707 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
8506 : 707 : target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
8507 : 707 : if (target)
8508 : : return target;
8509 : : break;
8510 : :
8511 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_1:
8512 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_2:
8513 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_4:
8514 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_8:
8515 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_16:
8516 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
8517 : 581 : target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
8518 : 581 : if (target)
8519 : : return target;
8520 : : break;
8521 : :
8522 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_1:
8523 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_2:
8524 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_4:
8525 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_8:
8526 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_16:
8527 : 512 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8528 : 512 : target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8529 : 512 : if (target)
8530 : : return target;
8531 : : break;
8532 : :
8533 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_1:
8534 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_2:
8535 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_4:
8536 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_8:
8537 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_16:
8538 : 488 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8539 : 488 : target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8540 : 488 : if (target)
8541 : : return target;
8542 : : break;
8543 : :
8544 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8545 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8546 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8547 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8548 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8549 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8550 : 581 : target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8551 : 581 : if (target)
8552 : : return target;
8553 : : break;
8554 : :
8555 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8556 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8557 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8558 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8559 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8560 : 434 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8561 : 434 : target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8562 : 434 : if (target)
8563 : : return target;
8564 : : break;
8565 : :
8566 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8567 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8568 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8569 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8570 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8571 : 149 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8572 : 149 : target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8573 : 149 : if (target)
8574 : : return target;
8575 : : break;
8576 : :
8577 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8578 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8579 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8580 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8581 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8582 : 117 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8583 : 117 : target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8584 : 117 : if (target)
8585 : : return target;
8586 : : break;
8587 : :
8588 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_1:
8589 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_2:
8590 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_4:
8591 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_8:
8592 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_16:
8593 : 115 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8594 : 115 : target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8595 : 115 : if (target)
8596 : : return target;
8597 : : break;
8598 : :
8599 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_1:
8600 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_2:
8601 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_4:
8602 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_8:
8603 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_16:
8604 : 116 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8605 : 116 : target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8606 : 116 : if (target)
8607 : : return target;
8608 : : break;
8609 : :
8610 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8611 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8612 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8613 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8614 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8615 : 114 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8616 : 114 : target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8617 : 114 : if (target)
8618 : : return target;
8619 : : break;
8620 : :
8621 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8622 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8623 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8624 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8625 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8626 : 83 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8627 : 83 : target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8628 : 83 : if (target)
8629 : : return target;
8630 : : break;
8631 : :
8632 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8633 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8634 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8635 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8636 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8637 : 200 : if (mode == VOIDmode)
8638 : 40 : mode = TYPE_MODE (boolean_type_node);
8639 : 200 : if (!target || !register_operand (target, mode))
8640 : 40 : target = gen_reg_rtx (mode);
8641 : :
8642 : 200 : mode = get_builtin_sync_mode
8643 : 200 : (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8644 : 200 : target = expand_builtin_compare_and_swap (mode, exp, true, target);
8645 : 200 : if (target)
8646 : : return target;
8647 : : break;
8648 : :
8649 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8650 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8651 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8652 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8653 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8654 : 256 : mode = get_builtin_sync_mode
8655 : 256 : (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8656 : 256 : target = expand_builtin_compare_and_swap (mode, exp, false, target);
8657 : 256 : if (target)
8658 : : return target;
8659 : : break;
8660 : :
8661 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8662 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8663 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8664 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8665 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8666 : 326 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8667 : 326 : target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8668 : 326 : if (target)
8669 : : return target;
8670 : : break;
8671 : :
8672 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_1:
8673 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_2:
8674 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_4:
8675 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_8:
8676 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_16:
8677 : 158 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8678 : 158 : if (expand_builtin_sync_lock_release (mode, exp))
8679 : 156 : return const0_rtx;
8680 : : break;
8681 : :
8682 : 281 : case BUILT_IN_SYNC_SYNCHRONIZE:
8683 : 281 : expand_builtin_sync_synchronize ();
8684 : 281 : return const0_rtx;
8685 : :
8686 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_1:
8687 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_2:
8688 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_4:
8689 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_8:
8690 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_16:
8691 : 2811 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8692 : 2811 : target = expand_builtin_atomic_exchange (mode, exp, target);
8693 : 2811 : if (target)
8694 : : return target;
8695 : : break;
8696 : :
8697 : 9194 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8698 : 9194 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8699 : 9194 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8700 : 9194 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8701 : 9194 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8702 : 9194 : {
8703 : 9194 : unsigned int nargs, z;
8704 : 9194 : vec<tree, va_gc> *vec;
8705 : :
8706 : 9194 : mode =
8707 : 9194 : get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8708 : 9194 : target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8709 : 9194 : if (target)
8710 : 7400 : return target;
8711 : :
8712 : : /* If this is turned into an external library call, the weak parameter
8713 : : must be dropped to match the expected parameter list. */
8714 : 1794 : nargs = call_expr_nargs (exp);
8715 : 1794 : vec_alloc (vec, nargs - 1);
8716 : 8970 : for (z = 0; z < 3; z++)
8717 : 5382 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8718 : : /* Skip the boolean weak parameter. */
8719 : 5382 : for (z = 4; z < 6; z++)
8720 : 3588 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8721 : 1794 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8722 : 1794 : break;
8723 : : }
8724 : :
8725 : 70024 : case BUILT_IN_ATOMIC_LOAD_1:
8726 : 70024 : case BUILT_IN_ATOMIC_LOAD_2:
8727 : 70024 : case BUILT_IN_ATOMIC_LOAD_4:
8728 : 70024 : case BUILT_IN_ATOMIC_LOAD_8:
8729 : 70024 : case BUILT_IN_ATOMIC_LOAD_16:
8730 : 70024 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8731 : 70024 : target = expand_builtin_atomic_load (mode, exp, target);
8732 : 70024 : if (target)
8733 : : return target;
8734 : : break;
8735 : :
8736 : 17373 : case BUILT_IN_ATOMIC_STORE_1:
8737 : 17373 : case BUILT_IN_ATOMIC_STORE_2:
8738 : 17373 : case BUILT_IN_ATOMIC_STORE_4:
8739 : 17373 : case BUILT_IN_ATOMIC_STORE_8:
8740 : 17373 : case BUILT_IN_ATOMIC_STORE_16:
8741 : 17373 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8742 : 17373 : target = expand_builtin_atomic_store (mode, exp);
8743 : 17373 : if (target)
8744 : 15746 : return const0_rtx;
8745 : : break;
8746 : :
8747 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_1:
8748 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_2:
8749 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_4:
8750 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_8:
8751 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_16:
8752 : 4377 : {
8753 : 4377 : enum built_in_function lib;
8754 : 4377 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8755 : 4377 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8756 : : (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8757 : 4377 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8758 : : ignore, lib);
8759 : 4377 : if (target)
8760 : : return target;
8761 : : break;
8762 : : }
8763 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_1:
8764 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_2:
8765 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_4:
8766 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_8:
8767 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_16:
8768 : 2166 : {
8769 : 2166 : enum built_in_function lib;
8770 : 2166 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8771 : 2166 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8772 : : (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8773 : 2166 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8774 : : ignore, lib);
8775 : 2166 : if (target)
8776 : : return target;
8777 : : break;
8778 : : }
8779 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_1:
8780 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_2:
8781 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_4:
8782 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_8:
8783 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_16:
8784 : 891 : {
8785 : 891 : enum built_in_function lib;
8786 : 891 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8787 : 891 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8788 : : (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8789 : 891 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8790 : : ignore, lib);
8791 : 891 : if (target)
8792 : : return target;
8793 : : break;
8794 : : }
8795 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_1:
8796 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_2:
8797 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_4:
8798 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_8:
8799 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_16:
8800 : 155 : {
8801 : 155 : enum built_in_function lib;
8802 : 155 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8803 : 155 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8804 : : (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8805 : 155 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8806 : : ignore, lib);
8807 : 155 : if (target)
8808 : : return target;
8809 : : break;
8810 : : }
8811 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_1:
8812 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_2:
8813 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_4:
8814 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_8:
8815 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_16:
8816 : 836 : {
8817 : 836 : enum built_in_function lib;
8818 : 836 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8819 : 836 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8820 : : (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8821 : 836 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8822 : : ignore, lib);
8823 : 836 : if (target)
8824 : : return target;
8825 : : break;
8826 : : }
8827 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_1:
8828 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_2:
8829 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_4:
8830 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_8:
8831 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_16:
8832 : 953 : {
8833 : 953 : enum built_in_function lib;
8834 : 953 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8835 : 953 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8836 : : (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8837 : 953 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8838 : : ignore, lib);
8839 : 953 : if (target)
8840 : : return target;
8841 : : break;
8842 : : }
8843 : 15344 : case BUILT_IN_ATOMIC_FETCH_ADD_1:
8844 : 15344 : case BUILT_IN_ATOMIC_FETCH_ADD_2:
8845 : 15344 : case BUILT_IN_ATOMIC_FETCH_ADD_4:
8846 : 15344 : case BUILT_IN_ATOMIC_FETCH_ADD_8:
8847 : 15344 : case BUILT_IN_ATOMIC_FETCH_ADD_16:
8848 : 15344 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8849 : 15344 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8850 : : ignore, BUILT_IN_NONE);
8851 : 15344 : if (target)
8852 : : return target;
8853 : : break;
8854 : :
8855 : 2418 : case BUILT_IN_ATOMIC_FETCH_SUB_1:
8856 : 2418 : case BUILT_IN_ATOMIC_FETCH_SUB_2:
8857 : 2418 : case BUILT_IN_ATOMIC_FETCH_SUB_4:
8858 : 2418 : case BUILT_IN_ATOMIC_FETCH_SUB_8:
8859 : 2418 : case BUILT_IN_ATOMIC_FETCH_SUB_16:
8860 : 2418 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8861 : 2418 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8862 : : ignore, BUILT_IN_NONE);
8863 : 2418 : if (target)
8864 : : return target;
8865 : : break;
8866 : :
8867 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_1:
8868 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_2:
8869 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_4:
8870 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_8:
8871 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_16:
8872 : 772 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8873 : 772 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8874 : : ignore, BUILT_IN_NONE);
8875 : 772 : if (target)
8876 : : return target;
8877 : : break;
8878 : :
8879 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_1:
8880 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_2:
8881 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_4:
8882 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_8:
8883 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_16:
8884 : 118 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8885 : 118 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8886 : : ignore, BUILT_IN_NONE);
8887 : 118 : if (target)
8888 : : return target;
8889 : : break;
8890 : :
8891 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_1:
8892 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_2:
8893 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_4:
8894 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_8:
8895 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_16:
8896 : 862 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8897 : 862 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8898 : : ignore, BUILT_IN_NONE);
8899 : 862 : if (target)
8900 : : return target;
8901 : : break;
8902 : :
8903 : 1144 : case BUILT_IN_ATOMIC_FETCH_OR_1:
8904 : 1144 : case BUILT_IN_ATOMIC_FETCH_OR_2:
8905 : 1144 : case BUILT_IN_ATOMIC_FETCH_OR_4:
8906 : 1144 : case BUILT_IN_ATOMIC_FETCH_OR_8:
8907 : 1144 : case BUILT_IN_ATOMIC_FETCH_OR_16:
8908 : 1144 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8909 : 1144 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8910 : : ignore, BUILT_IN_NONE);
8911 : 1144 : if (target)
8912 : : return target;
8913 : : break;
8914 : :
8915 : 259 : case BUILT_IN_ATOMIC_TEST_AND_SET:
8916 : 259 : target = expand_builtin_atomic_test_and_set (exp, target);
8917 : 259 : if (target)
8918 : : return target;
8919 : : break;
8920 : :
8921 : 57 : case BUILT_IN_ATOMIC_CLEAR:
8922 : 57 : return expand_builtin_atomic_clear (exp);
8923 : :
8924 : 1 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8925 : 1 : return expand_builtin_atomic_always_lock_free (exp);
8926 : :
8927 : 3 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8928 : 3 : target = expand_builtin_atomic_is_lock_free (exp);
8929 : 3 : if (target)
8930 : : return target;
8931 : : break;
8932 : :
8933 : 693 : case BUILT_IN_ATOMIC_THREAD_FENCE:
8934 : 693 : expand_builtin_atomic_thread_fence (exp);
8935 : 693 : return const0_rtx;
8936 : :
8937 : 60 : case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8938 : 60 : expand_builtin_atomic_signal_fence (exp);
8939 : 60 : return const0_rtx;
8940 : :
8941 : 617 : case BUILT_IN_OBJECT_SIZE:
8942 : 617 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8943 : 617 : return expand_builtin_object_size (exp);
8944 : :
8945 : 835 : case BUILT_IN_MEMCPY_CHK:
8946 : 835 : case BUILT_IN_MEMPCPY_CHK:
8947 : 835 : case BUILT_IN_MEMMOVE_CHK:
8948 : 835 : case BUILT_IN_MEMSET_CHK:
8949 : 835 : target = expand_builtin_memory_chk (exp, target, mode, fcode);
8950 : 835 : if (target)
8951 : : return target;
8952 : : break;
8953 : :
8954 : 1135 : case BUILT_IN_STRCPY_CHK:
8955 : 1135 : case BUILT_IN_STPCPY_CHK:
8956 : 1135 : case BUILT_IN_STRNCPY_CHK:
8957 : 1135 : case BUILT_IN_STPNCPY_CHK:
8958 : 1135 : case BUILT_IN_STRCAT_CHK:
8959 : 1135 : case BUILT_IN_STRNCAT_CHK:
8960 : 1135 : case BUILT_IN_SNPRINTF_CHK:
8961 : 1135 : case BUILT_IN_VSNPRINTF_CHK:
8962 : 1135 : maybe_emit_chk_warning (exp, fcode);
8963 : 1135 : break;
8964 : :
8965 : 1329 : case BUILT_IN_SPRINTF_CHK:
8966 : 1329 : case BUILT_IN_VSPRINTF_CHK:
8967 : 1329 : maybe_emit_sprintf_chk_warning (exp, fcode);
8968 : 1329 : break;
8969 : :
8970 : 3 : case BUILT_IN_THREAD_POINTER:
8971 : 3 : return expand_builtin_thread_pointer (exp, target);
8972 : :
8973 : 0 : case BUILT_IN_SET_THREAD_POINTER:
8974 : 0 : expand_builtin_set_thread_pointer (exp);
8975 : 0 : return const0_rtx;
8976 : :
8977 : : case BUILT_IN_ACC_ON_DEVICE:
8978 : : /* Do library call, if we failed to expand the builtin when
8979 : : folding. */
8980 : : break;
8981 : :
8982 : 356 : case BUILT_IN_GOACC_PARLEVEL_ID:
8983 : 356 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
8984 : 356 : return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8985 : :
8986 : 4 : case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8987 : 4 : return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8988 : :
8989 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8990 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8991 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8992 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8993 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8994 : 30 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8995 : 30 : return expand_speculation_safe_value (mode, exp, target, ignore);
8996 : :
8997 : 3 : case BUILT_IN_CRC8_DATA8:
8998 : 3 : return expand_builtin_crc_table_based (IFN_CRC, QImode, QImode, mode,
8999 : : exp, target);
9000 : 2 : case BUILT_IN_CRC16_DATA8:
9001 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, QImode, mode,
9002 : : exp, target);
9003 : 2 : case BUILT_IN_CRC16_DATA16:
9004 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, HImode, mode,
9005 : : exp, target);
9006 : 2 : case BUILT_IN_CRC32_DATA8:
9007 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, QImode, mode,
9008 : : exp, target);
9009 : 2 : case BUILT_IN_CRC32_DATA16:
9010 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, HImode, mode,
9011 : : exp, target);
9012 : 2 : case BUILT_IN_CRC32_DATA32:
9013 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, SImode, mode,
9014 : : exp, target);
9015 : 1 : case BUILT_IN_CRC64_DATA8:
9016 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, QImode, mode,
9017 : : exp, target);
9018 : 1 : case BUILT_IN_CRC64_DATA16:
9019 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, HImode, mode,
9020 : : exp, target);
9021 : 1 : case BUILT_IN_CRC64_DATA32:
9022 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, SImode, mode,
9023 : : exp, target);
9024 : 1 : case BUILT_IN_CRC64_DATA64:
9025 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, DImode, mode,
9026 : : exp, target);
9027 : 2 : case BUILT_IN_REV_CRC8_DATA8:
9028 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, QImode, QImode,
9029 : : mode, exp, target);
9030 : 2 : case BUILT_IN_REV_CRC16_DATA8:
9031 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, QImode,
9032 : : mode, exp, target);
9033 : 2 : case BUILT_IN_REV_CRC16_DATA16:
9034 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, HImode,
9035 : : mode, exp, target);
9036 : 2 : case BUILT_IN_REV_CRC32_DATA8:
9037 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, QImode,
9038 : : mode, exp, target);
9039 : 2 : case BUILT_IN_REV_CRC32_DATA16:
9040 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, HImode,
9041 : : mode, exp, target);
9042 : 2 : case BUILT_IN_REV_CRC32_DATA32:
9043 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, SImode,
9044 : : mode, exp, target);
9045 : 1 : case BUILT_IN_REV_CRC64_DATA8:
9046 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, QImode,
9047 : : mode, exp, target);
9048 : 1 : case BUILT_IN_REV_CRC64_DATA16:
9049 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, HImode,
9050 : : mode, exp, target);
9051 : 1 : case BUILT_IN_REV_CRC64_DATA32:
9052 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, SImode,
9053 : : mode, exp, target);
9054 : 1 : case BUILT_IN_REV_CRC64_DATA64:
9055 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, DImode,
9056 : : mode, exp, target);
9057 : : default: /* just do library call, if unknown builtin */
9058 : : break;
9059 : : }
9060 : :
9061 : : /* The switch statement above can drop through to cause the function
9062 : : to be called normally. */
9063 : 862752 : return expand_call (exp, target, ignore);
9064 : : }
9065 : :
9066 : : /* Determine whether a tree node represents a call to a built-in
9067 : : function. If the tree T is a call to a built-in function with
9068 : : the right number of arguments of the appropriate types, return
9069 : : the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
9070 : : Otherwise the return value is END_BUILTINS. */
9071 : :
9072 : : enum built_in_function
9073 : 91599993 : builtin_mathfn_code (const_tree t)
9074 : : {
9075 : 91599993 : const_tree fndecl, arg, parmlist;
9076 : 91599993 : const_tree argtype, parmtype;
9077 : 91599993 : const_call_expr_arg_iterator iter;
9078 : :
9079 : 91599993 : if (TREE_CODE (t) != CALL_EXPR)
9080 : : return END_BUILTINS;
9081 : :
9082 : 1303673 : fndecl = get_callee_fndecl (t);
9083 : 1303673 : if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9084 : : return END_BUILTINS;
9085 : :
9086 : 744591 : parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
9087 : 744591 : init_const_call_expr_arg_iterator (t, &iter);
9088 : 2436352 : for (; parmlist; parmlist = TREE_CHAIN (parmlist))
9089 : : {
9090 : : /* If a function doesn't take a variable number of arguments,
9091 : : the last element in the list will have type `void'. */
9092 : 1691552 : parmtype = TREE_VALUE (parmlist);
9093 : 1691552 : if (VOID_TYPE_P (parmtype))
9094 : : {
9095 : 744368 : if (more_const_call_expr_args_p (&iter))
9096 : : return END_BUILTINS;
9097 : 744368 : return DECL_FUNCTION_CODE (fndecl);
9098 : : }
9099 : :
9100 : 947184 : if (! more_const_call_expr_args_p (&iter))
9101 : : return END_BUILTINS;
9102 : :
9103 : 947184 : arg = next_const_call_expr_arg (&iter);
9104 : 947184 : argtype = TREE_TYPE (arg);
9105 : :
9106 : 947184 : if (SCALAR_FLOAT_TYPE_P (parmtype))
9107 : : {
9108 : 696095 : if (! SCALAR_FLOAT_TYPE_P (argtype))
9109 : : return END_BUILTINS;
9110 : : }
9111 : 251089 : else if (COMPLEX_FLOAT_TYPE_P (parmtype))
9112 : : {
9113 : 11033 : if (! COMPLEX_FLOAT_TYPE_P (argtype))
9114 : : return END_BUILTINS;
9115 : : }
9116 : 240056 : else if (POINTER_TYPE_P (parmtype))
9117 : : {
9118 : 69453 : if (! POINTER_TYPE_P (argtype))
9119 : : return END_BUILTINS;
9120 : : }
9121 : 170603 : else if (INTEGRAL_TYPE_P (parmtype))
9122 : : {
9123 : 170603 : if (! INTEGRAL_TYPE_P (argtype))
9124 : : return END_BUILTINS;
9125 : : }
9126 : : else
9127 : : return END_BUILTINS;
9128 : : }
9129 : :
9130 : : /* Variable-length argument list. */
9131 : 209 : return DECL_FUNCTION_CODE (fndecl);
9132 : : }
9133 : :
9134 : : /* Fold a call to __builtin_constant_p, if we know its argument ARG will
9135 : : evaluate to a constant. */
9136 : :
9137 : : static tree
9138 : 766788 : fold_builtin_constant_p (tree arg)
9139 : : {
9140 : : /* We return 1 for a numeric type that's known to be a constant
9141 : : value at compile-time or for an aggregate type that's a
9142 : : literal constant. */
9143 : 766788 : STRIP_NOPS (arg);
9144 : :
9145 : : /* If we know this is a constant, emit the constant of one. */
9146 : 766788 : if (CONSTANT_CLASS_P (arg)
9147 : 766788 : || (TREE_CODE (arg) == CONSTRUCTOR
9148 : 6 : && TREE_CONSTANT (arg)))
9149 : 25251 : return integer_one_node;
9150 : 741537 : if (TREE_CODE (arg) == ADDR_EXPR)
9151 : : {
9152 : 110 : tree op = TREE_OPERAND (arg, 0);
9153 : 110 : if (TREE_CODE (op) == STRING_CST
9154 : 110 : || (TREE_CODE (op) == ARRAY_REF
9155 : 57 : && integer_zerop (TREE_OPERAND (op, 1))
9156 : 57 : && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
9157 : 87 : return integer_one_node;
9158 : : }
9159 : :
9160 : : /* If this expression has side effects, show we don't know it to be a
9161 : : constant. Likewise if it's a pointer or aggregate type since in
9162 : : those case we only want literals, since those are only optimized
9163 : : when generating RTL, not later.
9164 : : And finally, if we are compiling an initializer, not code, we
9165 : : need to return a definite result now; there's not going to be any
9166 : : more optimization done. */
9167 : 741450 : if (TREE_SIDE_EFFECTS (arg)
9168 : 741325 : || AGGREGATE_TYPE_P (TREE_TYPE (arg))
9169 : 741325 : || POINTER_TYPE_P (TREE_TYPE (arg))
9170 : 741160 : || cfun == 0
9171 : 741110 : || folding_initializer
9172 : 1482553 : || force_folding_builtin_constant_p)
9173 : 365 : return integer_zero_node;
9174 : :
9175 : : return NULL_TREE;
9176 : : }
9177 : :
9178 : : /* Create builtin_expect or builtin_expect_with_probability
9179 : : with PRED and EXPECTED as its arguments and return it as a truthvalue.
9180 : : Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
9181 : : builtin_expect_with_probability instead uses third argument as PROBABILITY
9182 : : value. */
9183 : :
9184 : : static tree
9185 : 46906 : build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
9186 : : tree predictor, tree probability)
9187 : : {
9188 : 46906 : tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
9189 : :
9190 : 46906 : fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
9191 : : : BUILT_IN_EXPECT_WITH_PROBABILITY);
9192 : 46906 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
9193 : 46906 : ret_type = TREE_TYPE (TREE_TYPE (fn));
9194 : 46906 : pred_type = TREE_VALUE (arg_types);
9195 : 46906 : expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
9196 : :
9197 : 46906 : pred = fold_convert_loc (loc, pred_type, pred);
9198 : 46906 : expected = fold_convert_loc (loc, expected_type, expected);
9199 : :
9200 : 46906 : if (probability)
9201 : 0 : call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
9202 : : else
9203 : 93812 : call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
9204 : : predictor);
9205 : :
9206 : 46906 : return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
9207 : 46906 : build_int_cst (ret_type, 0));
9208 : : }
9209 : :
9210 : : /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
9211 : : NULL_TREE if no simplification is possible. */
9212 : :
9213 : : tree
9214 : 5486216 : fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
9215 : : tree arg3)
9216 : : {
9217 : 5486216 : tree inner, fndecl, inner_arg0;
9218 : 5486216 : enum tree_code code;
9219 : :
9220 : : /* Distribute the expected value over short-circuiting operators.
9221 : : See through the cast from truthvalue_type_node to long. */
9222 : 5486216 : inner_arg0 = arg0;
9223 : 11103386 : while (CONVERT_EXPR_P (inner_arg0)
9224 : 409694 : && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
9225 : 6305603 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
9226 : 409693 : inner_arg0 = TREE_OPERAND (inner_arg0, 0);
9227 : :
9228 : : /* If this is a builtin_expect within a builtin_expect keep the
9229 : : inner one. See through a comparison against a constant. It
9230 : : might have been added to create a thruthvalue. */
9231 : 5486216 : inner = inner_arg0;
9232 : :
9233 : 5486216 : if (COMPARISON_CLASS_P (inner)
9234 : 5486216 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
9235 : 692448 : inner = TREE_OPERAND (inner, 0);
9236 : :
9237 : 5486216 : if (TREE_CODE (inner) == CALL_EXPR
9238 : 54242 : && (fndecl = get_callee_fndecl (inner))
9239 : 5540458 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
9240 : : BUILT_IN_EXPECT_WITH_PROBABILITY))
9241 : : return arg0;
9242 : :
9243 : 5486208 : inner = inner_arg0;
9244 : 5486208 : code = TREE_CODE (inner);
9245 : 5486208 : if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
9246 : : {
9247 : 23453 : tree op0 = TREE_OPERAND (inner, 0);
9248 : 23453 : tree op1 = TREE_OPERAND (inner, 1);
9249 : 23453 : arg1 = save_expr (arg1);
9250 : :
9251 : 23453 : op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
9252 : 23453 : op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
9253 : 23453 : inner = build2 (code, TREE_TYPE (inner), op0, op1);
9254 : :
9255 : 23453 : return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
9256 : : }
9257 : :
9258 : : /* If the argument isn't invariant then there's nothing else we can do. */
9259 : 5462755 : if (!TREE_CONSTANT (inner_arg0))
9260 : : return NULL_TREE;
9261 : :
9262 : : /* If we expect that a comparison against the argument will fold to
9263 : : a constant return the constant. In practice, this means a true
9264 : : constant or the address of a non-weak symbol. */
9265 : 64944 : inner = inner_arg0;
9266 : 64944 : STRIP_NOPS (inner);
9267 : 64944 : if (TREE_CODE (inner) == ADDR_EXPR)
9268 : : {
9269 : 1 : do
9270 : : {
9271 : 1 : inner = TREE_OPERAND (inner, 0);
9272 : : }
9273 : 1 : while (TREE_CODE (inner) == COMPONENT_REF
9274 : 1 : || TREE_CODE (inner) == ARRAY_REF);
9275 : 1 : if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
9276 : : return NULL_TREE;
9277 : : }
9278 : :
9279 : : /* Otherwise, ARG0 already has the proper type for the return value. */
9280 : : return arg0;
9281 : : }
9282 : :
9283 : : /* Fold a call to __builtin_classify_type with argument ARG. */
9284 : :
9285 : : static tree
9286 : 2142 : fold_builtin_classify_type (tree arg)
9287 : : {
9288 : 2142 : if (arg == 0)
9289 : 0 : return build_int_cst (integer_type_node, no_type_class);
9290 : :
9291 : 2142 : return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
9292 : : }
9293 : :
9294 : : /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
9295 : : ARG. */
9296 : :
9297 : : static tree
9298 : 410909 : fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
9299 : : {
9300 : 410909 : if (!validate_arg (arg, POINTER_TYPE))
9301 : : return NULL_TREE;
9302 : : else
9303 : : {
9304 : 410906 : c_strlen_data lendata = { };
9305 : 410906 : tree len = c_strlen (arg, 0, &lendata);
9306 : :
9307 : 410906 : if (len)
9308 : 2163 : return fold_convert_loc (loc, type, len);
9309 : :
9310 : : /* TODO: Move this to gimple-ssa-warn-access once the pass runs
9311 : : also early enough to detect invalid reads in multimensional
9312 : : arrays and struct members. */
9313 : 408743 : if (!lendata.decl)
9314 : 401616 : c_strlen (arg, 1, &lendata);
9315 : :
9316 : 408743 : if (lendata.decl)
9317 : : {
9318 : 7133 : if (EXPR_HAS_LOCATION (arg))
9319 : 2812 : loc = EXPR_LOCATION (arg);
9320 : 4321 : else if (loc == UNKNOWN_LOCATION)
9321 : 0 : loc = input_location;
9322 : 7133 : warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
9323 : : }
9324 : :
9325 : 408743 : return NULL_TREE;
9326 : : }
9327 : : }
9328 : :
9329 : : /* Fold a call to __builtin_inf or __builtin_huge_val. */
9330 : :
9331 : : static tree
9332 : 217698 : fold_builtin_inf (location_t loc, tree type, int warn)
9333 : : {
9334 : : /* __builtin_inff is intended to be usable to define INFINITY on all
9335 : : targets. If an infinity is not available, INFINITY expands "to a
9336 : : positive constant of type float that overflows at translation
9337 : : time", footnote "In this case, using INFINITY will violate the
9338 : : constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
9339 : : Thus we pedwarn to ensure this constraint violation is
9340 : : diagnosed. */
9341 : 869983 : if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
9342 : 0 : pedwarn (loc, 0, "target format does not support infinity");
9343 : :
9344 : 217698 : return build_real (type, dconstinf);
9345 : : }
9346 : :
9347 : : /* Fold function call to builtin sincos, sincosf, or sincosl. Return
9348 : : NULL_TREE if no simplification can be made. */
9349 : :
9350 : : static tree
9351 : 144 : fold_builtin_sincos (location_t loc,
9352 : : tree arg0, tree arg1, tree arg2)
9353 : : {
9354 : 144 : tree type;
9355 : 144 : tree fndecl, call = NULL_TREE;
9356 : :
9357 : 144 : if (!validate_arg (arg0, REAL_TYPE)
9358 : 144 : || !validate_arg (arg1, POINTER_TYPE)
9359 : 288 : || !validate_arg (arg2, POINTER_TYPE))
9360 : : return NULL_TREE;
9361 : :
9362 : 144 : type = TREE_TYPE (arg0);
9363 : :
9364 : : /* Calculate the result when the argument is a constant. */
9365 : 144 : built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
9366 : 144 : if (fn == END_BUILTINS)
9367 : : return NULL_TREE;
9368 : :
9369 : : /* Canonicalize sincos to cexpi. */
9370 : 144 : if (TREE_CODE (arg0) == REAL_CST)
9371 : : {
9372 : 86 : tree complex_type = build_complex_type (type);
9373 : 86 : call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
9374 : : }
9375 : 86 : if (!call)
9376 : : {
9377 : 58 : if (!targetm.libc_has_function (function_c99_math_complex, type)
9378 : 58 : || !builtin_decl_implicit_p (fn))
9379 : : return NULL_TREE;
9380 : 58 : fndecl = builtin_decl_explicit (fn);
9381 : 58 : call = build_call_expr_loc (loc, fndecl, 1, arg0);
9382 : 58 : call = builtin_save_expr (call);
9383 : : }
9384 : :
9385 : 144 : tree ptype = build_pointer_type (type);
9386 : 144 : arg1 = fold_convert (ptype, arg1);
9387 : 144 : arg2 = fold_convert (ptype, arg2);
9388 : 144 : return build2 (COMPOUND_EXPR, void_type_node,
9389 : : build2 (MODIFY_EXPR, void_type_node,
9390 : : build_fold_indirect_ref_loc (loc, arg1),
9391 : : fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
9392 : : build2 (MODIFY_EXPR, void_type_node,
9393 : : build_fold_indirect_ref_loc (loc, arg2),
9394 : 144 : fold_build1_loc (loc, REALPART_EXPR, type, call)));
9395 : : }
9396 : :
9397 : : /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9398 : : Return NULL_TREE if no simplification can be made. */
9399 : :
9400 : : static tree
9401 : 2395801 : fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9402 : : {
9403 : 2395801 : if (!validate_arg (arg1, POINTER_TYPE)
9404 : 2395801 : || !validate_arg (arg2, POINTER_TYPE)
9405 : 4791602 : || !validate_arg (len, INTEGER_TYPE))
9406 : : return NULL_TREE;
9407 : :
9408 : : /* If the LEN parameter is zero, return zero. */
9409 : 2395801 : if (integer_zerop (len))
9410 : 0 : return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9411 : 0 : arg1, arg2);
9412 : :
9413 : : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9414 : 2395801 : if (operand_equal_p (arg1, arg2, 0))
9415 : 744 : return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9416 : :
9417 : : /* If len parameter is one, return an expression corresponding to
9418 : : (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9419 : 2395057 : if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9420 : : {
9421 : 19049 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9422 : 19049 : tree cst_uchar_ptr_node
9423 : 19049 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9424 : :
9425 : 19049 : tree ind1
9426 : 19049 : = fold_convert_loc (loc, integer_type_node,
9427 : : build1 (INDIRECT_REF, cst_uchar_node,
9428 : : fold_convert_loc (loc,
9429 : : cst_uchar_ptr_node,
9430 : : arg1)));
9431 : 19049 : tree ind2
9432 : 19049 : = fold_convert_loc (loc, integer_type_node,
9433 : : build1 (INDIRECT_REF, cst_uchar_node,
9434 : : fold_convert_loc (loc,
9435 : : cst_uchar_ptr_node,
9436 : : arg2)));
9437 : 19049 : return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9438 : : }
9439 : :
9440 : : return NULL_TREE;
9441 : : }
9442 : :
9443 : : /* Fold a call to builtin isascii with argument ARG. */
9444 : :
9445 : : static tree
9446 : 211 : fold_builtin_isascii (location_t loc, tree arg)
9447 : : {
9448 : 211 : if (!validate_arg (arg, INTEGER_TYPE))
9449 : : return NULL_TREE;
9450 : : else
9451 : : {
9452 : : /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9453 : 211 : arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9454 : : build_int_cst (integer_type_node,
9455 : : ~ HOST_WIDE_INT_UC (0x7f)));
9456 : 211 : return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9457 : 211 : arg, integer_zero_node);
9458 : : }
9459 : : }
9460 : :
9461 : : /* Fold a call to builtin toascii with argument ARG. */
9462 : :
9463 : : static tree
9464 : 168 : fold_builtin_toascii (location_t loc, tree arg)
9465 : : {
9466 : 168 : if (!validate_arg (arg, INTEGER_TYPE))
9467 : : return NULL_TREE;
9468 : :
9469 : : /* Transform toascii(c) -> (c & 0x7f). */
9470 : 168 : return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9471 : : build_int_cst (integer_type_node, 0x7f));
9472 : : }
9473 : :
9474 : : /* Fold a call to builtin isdigit with argument ARG. */
9475 : :
9476 : : static tree
9477 : 325 : fold_builtin_isdigit (location_t loc, tree arg)
9478 : : {
9479 : 325 : if (!validate_arg (arg, INTEGER_TYPE))
9480 : : return NULL_TREE;
9481 : : else
9482 : : {
9483 : : /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9484 : : /* According to the C standard, isdigit is unaffected by locale.
9485 : : However, it definitely is affected by the target character set. */
9486 : 313 : unsigned HOST_WIDE_INT target_digit0
9487 : 313 : = lang_hooks.to_target_charset ('0');
9488 : :
9489 : 313 : if (target_digit0 == 0)
9490 : : return NULL_TREE;
9491 : :
9492 : 313 : arg = fold_convert_loc (loc, unsigned_type_node, arg);
9493 : 313 : arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9494 : : build_int_cst (unsigned_type_node, target_digit0));
9495 : 313 : return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9496 : : build_int_cst (unsigned_type_node, 9));
9497 : : }
9498 : : }
9499 : :
9500 : : /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9501 : :
9502 : : static tree
9503 : 354111 : fold_builtin_fabs (location_t loc, tree arg, tree type)
9504 : : {
9505 : 354111 : if (!validate_arg (arg, REAL_TYPE))
9506 : : return NULL_TREE;
9507 : :
9508 : 354026 : arg = fold_convert_loc (loc, type, arg);
9509 : 354026 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9510 : : }
9511 : :
9512 : : /* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
9513 : : with argument ARG. */
9514 : :
9515 : : static tree
9516 : 91955 : fold_builtin_abs (location_t loc, tree arg, tree type)
9517 : : {
9518 : 91955 : if (!validate_arg (arg, INTEGER_TYPE))
9519 : : return NULL_TREE;
9520 : :
9521 : 91928 : if (TYPE_UNSIGNED (type))
9522 : : {
9523 : 1168 : if (TYPE_PRECISION (TREE_TYPE (arg))
9524 : 1168 : != TYPE_PRECISION (type)
9525 : 1168 : || TYPE_UNSIGNED (TREE_TYPE (arg)))
9526 : : return NULL_TREE;
9527 : 1168 : return fold_build1_loc (loc, ABSU_EXPR, type, arg);
9528 : : }
9529 : 90760 : arg = fold_convert_loc (loc, type, arg);
9530 : 90760 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9531 : : }
9532 : :
9533 : : /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9534 : :
9535 : : static tree
9536 : 112919 : fold_builtin_carg (location_t loc, tree arg, tree type)
9537 : : {
9538 : 112919 : if (validate_arg (arg, COMPLEX_TYPE)
9539 : 112919 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
9540 : : {
9541 : 112919 : tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9542 : :
9543 : 112919 : if (atan2_fn)
9544 : : {
9545 : 108728 : tree new_arg = builtin_save_expr (arg);
9546 : 108728 : tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9547 : 108728 : tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9548 : 108728 : return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9549 : : }
9550 : : }
9551 : :
9552 : : return NULL_TREE;
9553 : : }
9554 : :
9555 : : /* Fold a call to builtin frexp, we can assume the base is 2. */
9556 : :
9557 : : static tree
9558 : 112852 : fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9559 : : {
9560 : 112852 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9561 : : return NULL_TREE;
9562 : :
9563 : 112852 : STRIP_NOPS (arg0);
9564 : :
9565 : 112852 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9566 : : return NULL_TREE;
9567 : :
9568 : 2210 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9569 : :
9570 : : /* Proceed if a valid pointer type was passed in. */
9571 : 2210 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9572 : : {
9573 : 2210 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9574 : 2210 : tree frac, exp, res;
9575 : :
9576 : 2210 : switch (value->cl)
9577 : : {
9578 : 276 : case rvc_zero:
9579 : 276 : case rvc_nan:
9580 : 276 : case rvc_inf:
9581 : : /* For +-0, return (*exp = 0, +-0). */
9582 : : /* For +-NaN or +-Inf, *exp is unspecified, but something should
9583 : : be stored there so that it isn't read from uninitialized object.
9584 : : As glibc and newlib store *exp = 0 for +-Inf/NaN, storing
9585 : : 0 here as well is easiest. */
9586 : 276 : exp = integer_zero_node;
9587 : 276 : frac = arg0;
9588 : 276 : break;
9589 : 1934 : case rvc_normal:
9590 : 1934 : {
9591 : : /* Since the frexp function always expects base 2, and in
9592 : : GCC normalized significands are already in the range
9593 : : [0.5, 1.0), we have exactly what frexp wants. */
9594 : 1934 : REAL_VALUE_TYPE frac_rvt = *value;
9595 : 1934 : SET_REAL_EXP (&frac_rvt, 0);
9596 : 1934 : frac = build_real (rettype, frac_rvt);
9597 : 1934 : exp = build_int_cst (integer_type_node, REAL_EXP (value));
9598 : : }
9599 : 1934 : break;
9600 : 0 : default:
9601 : 0 : gcc_unreachable ();
9602 : : }
9603 : :
9604 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9605 : 2210 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9606 : 2210 : TREE_SIDE_EFFECTS (arg1) = 1;
9607 : 2210 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9608 : 2210 : suppress_warning (res, OPT_Wunused_value);
9609 : 2210 : return res;
9610 : : }
9611 : :
9612 : : return NULL_TREE;
9613 : : }
9614 : :
9615 : : /* Fold a call to builtin modf. */
9616 : :
9617 : : static tree
9618 : 74091 : fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9619 : : {
9620 : 74091 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9621 : : return NULL_TREE;
9622 : :
9623 : 74091 : STRIP_NOPS (arg0);
9624 : :
9625 : 74091 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9626 : : return NULL_TREE;
9627 : :
9628 : 2474 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9629 : :
9630 : : /* Proceed if a valid pointer type was passed in. */
9631 : 2474 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9632 : : {
9633 : 2474 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9634 : 2474 : REAL_VALUE_TYPE trunc, frac;
9635 : 2474 : tree res;
9636 : :
9637 : 2474 : switch (value->cl)
9638 : : {
9639 : 228 : case rvc_nan:
9640 : 228 : case rvc_zero:
9641 : : /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9642 : 228 : trunc = frac = *value;
9643 : 228 : break;
9644 : 96 : case rvc_inf:
9645 : : /* For +-Inf, return (*arg1 = arg0, +-0). */
9646 : 96 : frac = dconst0;
9647 : 96 : frac.sign = value->sign;
9648 : 96 : trunc = *value;
9649 : 96 : break;
9650 : 2150 : case rvc_normal:
9651 : : /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9652 : 2150 : real_trunc (&trunc, VOIDmode, value);
9653 : 2150 : real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9654 : : /* If the original number was negative and already
9655 : : integral, then the fractional part is -0.0. */
9656 : 2150 : if (value->sign && frac.cl == rvc_zero)
9657 : 69 : frac.sign = value->sign;
9658 : : break;
9659 : : }
9660 : :
9661 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9662 : 2474 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9663 : : build_real (rettype, trunc));
9664 : 2474 : TREE_SIDE_EFFECTS (arg1) = 1;
9665 : 2474 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9666 : : build_real (rettype, frac));
9667 : 2474 : suppress_warning (res, OPT_Wunused_value);
9668 : 2474 : return res;
9669 : : }
9670 : :
9671 : : return NULL_TREE;
9672 : : }
9673 : :
9674 : : /* Given a location LOC, an interclass builtin function decl FNDECL
9675 : : and its single argument ARG, return an folded expression computing
9676 : : the same, or NULL_TREE if we either couldn't or didn't want to fold
9677 : : (the latter happen if there's an RTL instruction available). */
9678 : :
9679 : : static tree
9680 : 1076824 : fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9681 : : {
9682 : 1076824 : machine_mode mode;
9683 : :
9684 : 1076824 : if (!validate_arg (arg, REAL_TYPE))
9685 : : return NULL_TREE;
9686 : :
9687 : 1076824 : if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9688 : : return NULL_TREE;
9689 : :
9690 : 1076824 : mode = TYPE_MODE (TREE_TYPE (arg));
9691 : :
9692 : 7534694 : bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9693 : :
9694 : : /* If there is no optab, try generic code. */
9695 : 1076824 : switch (DECL_FUNCTION_CODE (fndecl))
9696 : : {
9697 : 259821 : tree result;
9698 : :
9699 : 259821 : CASE_FLT_FN (BUILT_IN_ISINF):
9700 : 259821 : {
9701 : : /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9702 : 259821 : tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9703 : 259821 : tree type = TREE_TYPE (arg);
9704 : 259821 : REAL_VALUE_TYPE r;
9705 : 259821 : char buf[128];
9706 : :
9707 : 259821 : if (is_ibm_extended)
9708 : : {
9709 : : /* NaN and Inf are encoded in the high-order double value
9710 : : only. The low-order value is not significant. */
9711 : 0 : type = double_type_node;
9712 : 0 : mode = DFmode;
9713 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9714 : : }
9715 : 259821 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9716 : 259821 : real_from_string3 (&r, buf, mode);
9717 : 259821 : result = build_call_expr (isgr_fn, 2,
9718 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9719 : : build_real (type, r));
9720 : 259821 : return result;
9721 : : }
9722 : 557420 : CASE_FLT_FN (BUILT_IN_FINITE):
9723 : 557420 : case BUILT_IN_ISFINITE:
9724 : 557420 : {
9725 : : /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9726 : 557420 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9727 : 557420 : tree type = TREE_TYPE (arg);
9728 : 557420 : REAL_VALUE_TYPE r;
9729 : 557420 : char buf[128];
9730 : :
9731 : 557420 : if (is_ibm_extended)
9732 : : {
9733 : : /* NaN and Inf are encoded in the high-order double value
9734 : : only. The low-order value is not significant. */
9735 : 0 : type = double_type_node;
9736 : 0 : mode = DFmode;
9737 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9738 : : }
9739 : 557420 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9740 : 557420 : real_from_string3 (&r, buf, mode);
9741 : 557420 : result = build_call_expr (isle_fn, 2,
9742 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9743 : : build_real (type, r));
9744 : : /*result = fold_build2_loc (loc, UNGT_EXPR,
9745 : : TREE_TYPE (TREE_TYPE (fndecl)),
9746 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9747 : : build_real (type, r));
9748 : : result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9749 : : TREE_TYPE (TREE_TYPE (fndecl)),
9750 : : result);*/
9751 : 557420 : return result;
9752 : : }
9753 : 258049 : case BUILT_IN_ISNORMAL:
9754 : 258049 : {
9755 : : /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9756 : : islessequal(fabs(x),DBL_MAX). */
9757 : 258049 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9758 : 258049 : tree type = TREE_TYPE (arg);
9759 : 258049 : tree orig_arg, max_exp, min_exp;
9760 : 258049 : machine_mode orig_mode = mode;
9761 : 258049 : REAL_VALUE_TYPE rmax, rmin;
9762 : 258049 : char buf[128];
9763 : :
9764 : 258049 : orig_arg = arg = builtin_save_expr (arg);
9765 : 258049 : if (is_ibm_extended)
9766 : : {
9767 : : /* Use double to test the normal range of IBM extended
9768 : : precision. Emin for IBM extended precision is
9769 : : different to emin for IEEE double, being 53 higher
9770 : : since the low double exponent is at least 53 lower
9771 : : than the high double exponent. */
9772 : 0 : type = double_type_node;
9773 : 0 : mode = DFmode;
9774 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9775 : : }
9776 : 258049 : arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9777 : :
9778 : 258049 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9779 : 258049 : real_from_string3 (&rmax, buf, mode);
9780 : 258049 : if (DECIMAL_FLOAT_MODE_P (mode))
9781 : 1 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9782 : : else
9783 : 258048 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9784 : 258049 : real_from_string3 (&rmin, buf, orig_mode);
9785 : 258049 : max_exp = build_real (type, rmax);
9786 : 258049 : min_exp = build_real (type, rmin);
9787 : :
9788 : 258049 : max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9789 : 258049 : if (is_ibm_extended)
9790 : : {
9791 : : /* Testing the high end of the range is done just using
9792 : : the high double, using the same test as isfinite().
9793 : : For the subnormal end of the range we first test the
9794 : : high double, then if its magnitude is equal to the
9795 : : limit of 0x1p-969, we test whether the low double is
9796 : : non-zero and opposite sign to the high double. */
9797 : 0 : tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9798 : 0 : tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9799 : 0 : tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9800 : 0 : tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9801 : : arg, min_exp);
9802 : 0 : tree as_complex = build1 (VIEW_CONVERT_EXPR,
9803 : : complex_double_type_node, orig_arg);
9804 : 0 : tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9805 : 0 : tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9806 : 0 : tree zero = build_real (type, dconst0);
9807 : 0 : tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9808 : 0 : tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9809 : 0 : tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9810 : 0 : tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9811 : : fold_build3 (COND_EXPR,
9812 : : integer_type_node,
9813 : : hilt, logt, lolt));
9814 : 0 : eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9815 : : eq_min, ok_lo);
9816 : 0 : min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9817 : : gt_min, eq_min);
9818 : : }
9819 : : else
9820 : : {
9821 : 258049 : tree const isge_fn
9822 : 258049 : = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9823 : 258049 : min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9824 : : }
9825 : 258049 : result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9826 : : max_exp, min_exp);
9827 : 258049 : return result;
9828 : : }
9829 : : default:
9830 : : break;
9831 : : }
9832 : :
9833 : : return NULL_TREE;
9834 : : }
9835 : :
9836 : : /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9837 : : ARG is the argument for the call. */
9838 : :
9839 : : static tree
9840 : 1109781 : fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9841 : : {
9842 : 1109781 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
9843 : :
9844 : 1109781 : if (!validate_arg (arg, REAL_TYPE))
9845 : : return NULL_TREE;
9846 : :
9847 : 1109781 : switch (builtin_index)
9848 : : {
9849 : 261476 : case BUILT_IN_ISINF:
9850 : 261476 : if (tree_expr_infinite_p (arg))
9851 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9852 : 261476 : if (!tree_expr_maybe_infinite_p (arg))
9853 : 121 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9854 : : return NULL_TREE;
9855 : :
9856 : 708 : case BUILT_IN_ISINF_SIGN:
9857 : 708 : {
9858 : : /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9859 : : /* In a boolean context, GCC will fold the inner COND_EXPR to
9860 : : 1. So e.g. "if (isinf_sign(x))" would be folded to just
9861 : : "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9862 : 708 : tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9863 : 708 : tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9864 : 708 : tree tmp = NULL_TREE;
9865 : :
9866 : 708 : arg = builtin_save_expr (arg);
9867 : :
9868 : 708 : if (signbit_fn && isinf_fn)
9869 : : {
9870 : 708 : tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9871 : 708 : tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9872 : :
9873 : 708 : signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9874 : : signbit_call, integer_zero_node);
9875 : 708 : isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9876 : : isinf_call, integer_zero_node);
9877 : :
9878 : 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9879 : : integer_minus_one_node, integer_one_node);
9880 : 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9881 : : isinf_call, tmp,
9882 : : integer_zero_node);
9883 : : }
9884 : :
9885 : : return tmp;
9886 : : }
9887 : :
9888 : 557644 : case BUILT_IN_ISFINITE:
9889 : 557644 : if (tree_expr_finite_p (arg))
9890 : 224 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9891 : 557420 : if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9892 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9893 : : return NULL_TREE;
9894 : :
9895 : 261099 : case BUILT_IN_ISNAN:
9896 : 261099 : if (tree_expr_nan_p (arg))
9897 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9898 : 261099 : if (!tree_expr_maybe_nan_p (arg))
9899 : 124 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9900 : :
9901 : 260975 : {
9902 : 1826069 : bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9903 : 260975 : if (is_ibm_extended)
9904 : : {
9905 : : /* NaN and Inf are encoded in the high-order double value
9906 : : only. The low-order value is not significant. */
9907 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9908 : : }
9909 : : }
9910 : 260975 : arg = builtin_save_expr (arg);
9911 : 260975 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9912 : :
9913 : 28854 : case BUILT_IN_ISSIGNALING:
9914 : : /* Folding to true for REAL_CST is done in fold_const_call_ss.
9915 : : Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9916 : : and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9917 : : here, so there is some possibility of __builtin_issignaling working
9918 : : without -fsignaling-nans. Especially when -fno-signaling-nans is
9919 : : the default. */
9920 : 28854 : if (!tree_expr_maybe_nan_p (arg))
9921 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9922 : : return NULL_TREE;
9923 : :
9924 : 0 : default:
9925 : 0 : gcc_unreachable ();
9926 : : }
9927 : : }
9928 : :
9929 : : /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9930 : : This builtin will generate code to return the appropriate floating
9931 : : point classification depending on the value of the floating point
9932 : : number passed in. The possible return values must be supplied as
9933 : : int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9934 : : FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9935 : : one floating point argument which is "type generic". */
9936 : :
9937 : : static tree
9938 : 112287 : fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9939 : : {
9940 : 112287 : tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9941 : : arg, type, res, tmp;
9942 : 112287 : machine_mode mode;
9943 : 112287 : REAL_VALUE_TYPE r;
9944 : 112287 : char buf[128];
9945 : :
9946 : : /* Verify the required arguments in the original call. */
9947 : 112287 : if (nargs != 6
9948 : 112287 : || !validate_arg (args[0], INTEGER_TYPE)
9949 : 112287 : || !validate_arg (args[1], INTEGER_TYPE)
9950 : 112287 : || !validate_arg (args[2], INTEGER_TYPE)
9951 : 112287 : || !validate_arg (args[3], INTEGER_TYPE)
9952 : 112287 : || !validate_arg (args[4], INTEGER_TYPE)
9953 : 224574 : || !validate_arg (args[5], REAL_TYPE))
9954 : : return NULL_TREE;
9955 : :
9956 : 112287 : fp_nan = args[0];
9957 : 112287 : fp_infinite = args[1];
9958 : 112287 : fp_normal = args[2];
9959 : 112287 : fp_subnormal = args[3];
9960 : 112287 : fp_zero = args[4];
9961 : 112287 : arg = args[5];
9962 : 112287 : type = TREE_TYPE (arg);
9963 : 112287 : mode = TYPE_MODE (type);
9964 : 112287 : arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9965 : :
9966 : : /* fpclassify(x) ->
9967 : : isnan(x) ? FP_NAN :
9968 : : (fabs(x) == Inf ? FP_INFINITE :
9969 : : (fabs(x) >= DBL_MIN ? FP_NORMAL :
9970 : : (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9971 : :
9972 : 112287 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9973 : : build_real (type, dconst0));
9974 : 112287 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9975 : : tmp, fp_zero, fp_subnormal);
9976 : :
9977 : 112287 : if (DECIMAL_FLOAT_MODE_P (mode))
9978 : 3 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (mode)->emin - 1);
9979 : : else
9980 : 112284 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9981 : 112287 : real_from_string3 (&r, buf, mode);
9982 : 112287 : tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9983 : : arg, build_real (type, r));
9984 : 112287 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9985 : : fp_normal, res);
9986 : :
9987 : 112287 : if (tree_expr_maybe_infinite_p (arg))
9988 : : {
9989 : 112199 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9990 : : build_real (type, dconstinf));
9991 : 112199 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9992 : : fp_infinite, res);
9993 : : }
9994 : :
9995 : 112287 : if (tree_expr_maybe_nan_p (arg))
9996 : : {
9997 : 112197 : tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9998 : 112197 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9999 : : res, fp_nan);
10000 : : }
10001 : :
10002 : : return res;
10003 : : }
10004 : :
10005 : : /* Fold a call to an unordered comparison function such as
10006 : : __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10007 : : being called and ARG0 and ARG1 are the arguments for the call.
10008 : : UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10009 : : the opposite of the desired result. UNORDERED_CODE is used
10010 : : for modes that can hold NaNs and ORDERED_CODE is used for
10011 : : the rest. */
10012 : :
10013 : : static tree
10014 : 2886011 : fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10015 : : enum tree_code unordered_code,
10016 : : enum tree_code ordered_code)
10017 : : {
10018 : 2886011 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10019 : 2886011 : enum tree_code code;
10020 : 2886011 : tree type0, type1;
10021 : 2886011 : enum tree_code code0, code1;
10022 : 2886011 : tree cmp_type = NULL_TREE;
10023 : :
10024 : 2886011 : type0 = TREE_TYPE (arg0);
10025 : 2886011 : type1 = TREE_TYPE (arg1);
10026 : :
10027 : 2886011 : code0 = TREE_CODE (type0);
10028 : 2886011 : code1 = TREE_CODE (type1);
10029 : :
10030 : 2886011 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10031 : : /* Choose the wider of two real types. */
10032 : 2885831 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10033 : 2885831 : ? type0 : type1;
10034 : 180 : else if (code0 == REAL_TYPE
10035 : 91 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10036 : : cmp_type = type0;
10037 : 89 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10038 : 89 : && code1 == REAL_TYPE)
10039 : 135 : cmp_type = type1;
10040 : :
10041 : 2886011 : arg0 = fold_convert_loc (loc, cmp_type, arg0);
10042 : 2886011 : arg1 = fold_convert_loc (loc, cmp_type, arg1);
10043 : :
10044 : 2886011 : if (unordered_code == UNORDERED_EXPR)
10045 : : {
10046 : 259080 : if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
10047 : 16 : return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
10048 : 259064 : if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
10049 : 141 : return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10050 : 258923 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10051 : : }
10052 : :
10053 : 2629782 : code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
10054 : 2626931 : ? unordered_code : ordered_code;
10055 : 2626931 : return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10056 : 2626931 : fold_build2_loc (loc, code, type, arg0, arg1));
10057 : : }
10058 : :
10059 : : /* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments.
10060 : : After choosing the wider floating-point type for the comparison,
10061 : : the code is folded to:
10062 : : SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1> */
10063 : :
10064 : : static tree
10065 : 702 : fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
10066 : : {
10067 : 702 : tree type0, type1;
10068 : 702 : enum tree_code code0, code1;
10069 : 702 : tree cmp1, cmp2, cmp_type = NULL_TREE;
10070 : :
10071 : 702 : type0 = TREE_TYPE (arg0);
10072 : 702 : type1 = TREE_TYPE (arg1);
10073 : :
10074 : 702 : code0 = TREE_CODE (type0);
10075 : 702 : code1 = TREE_CODE (type1);
10076 : :
10077 : 702 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10078 : : /* Choose the wider of two real types. */
10079 : 690 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10080 : 690 : ? type0 : type1;
10081 : 12 : else if (code0 == REAL_TYPE
10082 : 6 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10083 : : cmp_type = type0;
10084 : 6 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10085 : 6 : && code1 == REAL_TYPE)
10086 : 6 : cmp_type = type1;
10087 : :
10088 : 702 : arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
10089 : 702 : arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
10090 : :
10091 : 702 : cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
10092 : 702 : cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
10093 : :
10094 : 702 : return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
10095 : : }
10096 : :
10097 : : /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
10098 : : arithmetics if it can never overflow, or into internal functions that
10099 : : return both result of arithmetics and overflowed boolean flag in
10100 : : a complex integer result, or some other check for overflow.
10101 : : Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
10102 : : checking part of that. */
10103 : :
10104 : : static tree
10105 : 177855 : fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
10106 : : tree arg0, tree arg1, tree arg2)
10107 : : {
10108 : 177855 : enum internal_fn ifn = IFN_LAST;
10109 : : /* The code of the expression corresponding to the built-in. */
10110 : 177855 : enum tree_code opcode = ERROR_MARK;
10111 : 177855 : bool ovf_only = false;
10112 : :
10113 : 177855 : switch (fcode)
10114 : : {
10115 : : case BUILT_IN_ADD_OVERFLOW_P:
10116 : : ovf_only = true;
10117 : : /* FALLTHRU */
10118 : : case BUILT_IN_ADD_OVERFLOW:
10119 : : case BUILT_IN_SADD_OVERFLOW:
10120 : : case BUILT_IN_SADDL_OVERFLOW:
10121 : : case BUILT_IN_SADDLL_OVERFLOW:
10122 : : case BUILT_IN_UADD_OVERFLOW:
10123 : : case BUILT_IN_UADDL_OVERFLOW:
10124 : : case BUILT_IN_UADDLL_OVERFLOW:
10125 : : opcode = PLUS_EXPR;
10126 : : ifn = IFN_ADD_OVERFLOW;
10127 : : break;
10128 : 15577 : case BUILT_IN_SUB_OVERFLOW_P:
10129 : 15577 : ovf_only = true;
10130 : : /* FALLTHRU */
10131 : 37061 : case BUILT_IN_SUB_OVERFLOW:
10132 : 37061 : case BUILT_IN_SSUB_OVERFLOW:
10133 : 37061 : case BUILT_IN_SSUBL_OVERFLOW:
10134 : 37061 : case BUILT_IN_SSUBLL_OVERFLOW:
10135 : 37061 : case BUILT_IN_USUB_OVERFLOW:
10136 : 37061 : case BUILT_IN_USUBL_OVERFLOW:
10137 : 37061 : case BUILT_IN_USUBLL_OVERFLOW:
10138 : 37061 : opcode = MINUS_EXPR;
10139 : 37061 : ifn = IFN_SUB_OVERFLOW;
10140 : 37061 : break;
10141 : 15857 : case BUILT_IN_MUL_OVERFLOW_P:
10142 : 15857 : ovf_only = true;
10143 : : /* FALLTHRU */
10144 : 108955 : case BUILT_IN_MUL_OVERFLOW:
10145 : 108955 : case BUILT_IN_SMUL_OVERFLOW:
10146 : 108955 : case BUILT_IN_SMULL_OVERFLOW:
10147 : 108955 : case BUILT_IN_SMULLL_OVERFLOW:
10148 : 108955 : case BUILT_IN_UMUL_OVERFLOW:
10149 : 108955 : case BUILT_IN_UMULL_OVERFLOW:
10150 : 108955 : case BUILT_IN_UMULLL_OVERFLOW:
10151 : 108955 : opcode = MULT_EXPR;
10152 : 108955 : ifn = IFN_MUL_OVERFLOW;
10153 : 108955 : break;
10154 : 0 : default:
10155 : 0 : gcc_unreachable ();
10156 : : }
10157 : :
10158 : : /* For the "generic" overloads, the first two arguments can have different
10159 : : types and the last argument determines the target type to use to check
10160 : : for overflow. The arguments of the other overloads all have the same
10161 : : type. */
10162 : 177855 : tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
10163 : :
10164 : : /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
10165 : : arguments are constant, attempt to fold the built-in call into a constant
10166 : : expression indicating whether or not it detected an overflow. */
10167 : 177855 : if (ovf_only
10168 : 43244 : && TREE_CODE (arg0) == INTEGER_CST
10169 : 13131 : && TREE_CODE (arg1) == INTEGER_CST)
10170 : : /* Perform the computation in the target type and check for overflow. */
10171 : 7347 : return omit_one_operand_loc (loc, boolean_type_node,
10172 : 7347 : arith_overflowed_p (opcode, type, arg0, arg1)
10173 : : ? boolean_true_node : boolean_false_node,
10174 : 7347 : arg2);
10175 : :
10176 : 170508 : tree intres, ovfres;
10177 : 170508 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10178 : : {
10179 : 9206 : intres = fold_binary_loc (loc, opcode, type,
10180 : : fold_convert_loc (loc, type, arg0),
10181 : : fold_convert_loc (loc, type, arg1));
10182 : 9206 : if (TREE_OVERFLOW (intres))
10183 : 1586 : intres = drop_tree_overflow (intres);
10184 : 18412 : ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
10185 : 9206 : ? boolean_true_node : boolean_false_node);
10186 : : }
10187 : : else
10188 : : {
10189 : 161302 : tree ctype = build_complex_type (type);
10190 : 161302 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10191 : : arg0, arg1);
10192 : 161302 : tree tgt;
10193 : 161302 : if (ovf_only)
10194 : : {
10195 : : tgt = call;
10196 : 161302 : intres = NULL_TREE;
10197 : : }
10198 : : else
10199 : : {
10200 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10201 : : as while the call itself is const, the REALPART_EXPR store is
10202 : : certainly not. And in any case, we want just one call,
10203 : : not multiple and trying to CSE them later. */
10204 : 125405 : TREE_SIDE_EFFECTS (call) = 1;
10205 : 125405 : tgt = save_expr (call);
10206 : : }
10207 : 161302 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10208 : 161302 : ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10209 : 161302 : ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
10210 : : }
10211 : :
10212 : 170508 : if (ovf_only)
10213 : 35897 : return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
10214 : :
10215 : 134611 : tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
10216 : 134611 : tree store
10217 : 134611 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
10218 : 134611 : return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
10219 : : }
10220 : :
10221 : : /* Fold __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g into corresponding
10222 : : internal function. */
10223 : :
10224 : : static tree
10225 : 227088 : fold_builtin_bit_query (location_t loc, enum built_in_function fcode,
10226 : : tree arg0, tree arg1)
10227 : : {
10228 : 227088 : enum internal_fn ifn;
10229 : 227088 : enum built_in_function fcodei, fcodel, fcodell;
10230 : 227088 : tree arg0_type = TREE_TYPE (arg0);
10231 : 227088 : tree cast_type = NULL_TREE;
10232 : 227088 : int addend = 0;
10233 : :
10234 : 227088 : switch (fcode)
10235 : : {
10236 : 161884 : case BUILT_IN_CLZG:
10237 : 161884 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10238 : : return NULL_TREE;
10239 : : ifn = IFN_CLZ;
10240 : : fcodei = BUILT_IN_CLZ;
10241 : : fcodel = BUILT_IN_CLZL;
10242 : : fcodell = BUILT_IN_CLZLL;
10243 : : break;
10244 : 47276 : case BUILT_IN_CTZG:
10245 : 47276 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10246 : : return NULL_TREE;
10247 : : ifn = IFN_CTZ;
10248 : : fcodei = BUILT_IN_CTZ;
10249 : : fcodel = BUILT_IN_CTZL;
10250 : : fcodell = BUILT_IN_CTZLL;
10251 : : break;
10252 : : case BUILT_IN_CLRSBG:
10253 : : ifn = IFN_CLRSB;
10254 : : fcodei = BUILT_IN_CLRSB;
10255 : : fcodel = BUILT_IN_CLRSBL;
10256 : : fcodell = BUILT_IN_CLRSBLL;
10257 : : break;
10258 : 83 : case BUILT_IN_FFSG:
10259 : 83 : ifn = IFN_FFS;
10260 : 83 : fcodei = BUILT_IN_FFS;
10261 : 83 : fcodel = BUILT_IN_FFSL;
10262 : 83 : fcodell = BUILT_IN_FFSLL;
10263 : 83 : break;
10264 : 76 : case BUILT_IN_PARITYG:
10265 : 76 : ifn = IFN_PARITY;
10266 : 76 : fcodei = BUILT_IN_PARITY;
10267 : 76 : fcodel = BUILT_IN_PARITYL;
10268 : 76 : fcodell = BUILT_IN_PARITYLL;
10269 : 76 : break;
10270 : 17688 : case BUILT_IN_POPCOUNTG:
10271 : 17688 : ifn = IFN_POPCOUNT;
10272 : 17688 : fcodei = BUILT_IN_POPCOUNT;
10273 : 17688 : fcodel = BUILT_IN_POPCOUNTL;
10274 : 17688 : fcodell = BUILT_IN_POPCOUNTLL;
10275 : 17688 : break;
10276 : 0 : default:
10277 : 0 : gcc_unreachable ();
10278 : : }
10279 : :
10280 : 226966 : if (TYPE_PRECISION (arg0_type)
10281 : 226966 : <= TYPE_PRECISION (long_long_unsigned_type_node))
10282 : : {
10283 : 196158 : if (TYPE_PRECISION (arg0_type) <= TYPE_PRECISION (unsigned_type_node))
10284 : :
10285 : 84070 : cast_type = (TYPE_UNSIGNED (arg0_type)
10286 : 84016 : ? unsigned_type_node : integer_type_node);
10287 : 112142 : else if (TYPE_PRECISION (arg0_type)
10288 : 112142 : <= TYPE_PRECISION (long_unsigned_type_node))
10289 : : {
10290 : 112175 : cast_type = (TYPE_UNSIGNED (arg0_type)
10291 : 112103 : ? long_unsigned_type_node : long_integer_type_node);
10292 : : fcodei = fcodel;
10293 : : }
10294 : : else
10295 : : {
10296 : 39 : cast_type = (TYPE_UNSIGNED (arg0_type)
10297 : 39 : ? long_long_unsigned_type_node
10298 : : : long_long_integer_type_node);
10299 : : fcodei = fcodell;
10300 : : }
10301 : : }
10302 : 61616 : else if (TYPE_PRECISION (arg0_type) <= MAX_FIXED_MODE_SIZE)
10303 : : {
10304 : 30708 : cast_type
10305 : 30708 : = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
10306 : 30708 : TYPE_UNSIGNED (arg0_type));
10307 : 30708 : gcc_assert (TYPE_PRECISION (cast_type)
10308 : : == 2 * TYPE_PRECISION (long_long_unsigned_type_node));
10309 : : fcodei = END_BUILTINS;
10310 : : }
10311 : : else
10312 : : fcodei = END_BUILTINS;
10313 : 226866 : if (cast_type)
10314 : : {
10315 : 226866 : switch (fcode)
10316 : : {
10317 : 161873 : case BUILT_IN_CLZG:
10318 : 161873 : case BUILT_IN_CLRSBG:
10319 : 161873 : addend = TYPE_PRECISION (arg0_type) - TYPE_PRECISION (cast_type);
10320 : 161873 : break;
10321 : : default:
10322 : : break;
10323 : : }
10324 : 226866 : arg0 = fold_convert (cast_type, arg0);
10325 : 226866 : arg0_type = cast_type;
10326 : : }
10327 : :
10328 : 226966 : if (arg1)
10329 : 147546 : arg1 = fold_convert (integer_type_node, arg1);
10330 : :
10331 : 226966 : tree arg2 = arg1;
10332 : 226966 : if (fcode == BUILT_IN_CLZG && addend)
10333 : : {
10334 : 7101 : if (arg1)
10335 : 7068 : arg0 = save_expr (arg0);
10336 : : arg2 = NULL_TREE;
10337 : : }
10338 : 226966 : tree call = NULL_TREE, tem;
10339 : 226966 : if (TYPE_PRECISION (arg0_type) == MAX_FIXED_MODE_SIZE
10340 : 30747 : && (TYPE_PRECISION (arg0_type)
10341 : 30747 : == 2 * TYPE_PRECISION (long_long_unsigned_type_node))
10342 : : /* If the target supports the optab, then don't do the expansion. */
10343 : 257674 : && !direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH))
10344 : : {
10345 : : /* __int128 expansions using up to 2 long long builtins. */
10346 : 30708 : arg0 = save_expr (arg0);
10347 : 30708 : tree type = (TYPE_UNSIGNED (arg0_type)
10348 : 30708 : ? long_long_unsigned_type_node
10349 : 30708 : : long_long_integer_type_node);
10350 : 61416 : tree hi = fold_build2 (RSHIFT_EXPR, arg0_type, arg0,
10351 : : build_int_cst (integer_type_node,
10352 : : MAX_FIXED_MODE_SIZE / 2));
10353 : 30708 : hi = fold_convert (type, hi);
10354 : 30708 : tree lo = fold_convert (type, arg0);
10355 : 30708 : switch (fcode)
10356 : : {
10357 : 30613 : case BUILT_IN_CLZG:
10358 : 30613 : call = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10359 : 61226 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10360 : : build_int_cst (integer_type_node,
10361 : : MAX_FIXED_MODE_SIZE / 2));
10362 : 30613 : if (arg2)
10363 : 30600 : call = fold_build3 (COND_EXPR, integer_type_node,
10364 : : fold_build2 (NE_EXPR, boolean_type_node,
10365 : : lo, build_zero_cst (type)),
10366 : : call, arg2);
10367 : 30613 : call = fold_build3 (COND_EXPR, integer_type_node,
10368 : : fold_build2 (NE_EXPR, boolean_type_node,
10369 : : hi, build_zero_cst (type)),
10370 : : fold_builtin_bit_query (loc, fcode, hi,
10371 : : NULL_TREE),
10372 : : call);
10373 : 30613 : break;
10374 : 33 : case BUILT_IN_CTZG:
10375 : 33 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10376 : 66 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10377 : : build_int_cst (integer_type_node,
10378 : : MAX_FIXED_MODE_SIZE / 2));
10379 : 33 : if (arg2)
10380 : 24 : call = fold_build3 (COND_EXPR, integer_type_node,
10381 : : fold_build2 (NE_EXPR, boolean_type_node,
10382 : : hi, build_zero_cst (type)),
10383 : : call, arg2);
10384 : 33 : call = fold_build3 (COND_EXPR, integer_type_node,
10385 : : fold_build2 (NE_EXPR, boolean_type_node,
10386 : : lo, build_zero_cst (type)),
10387 : : fold_builtin_bit_query (loc, fcode, lo,
10388 : : NULL_TREE),
10389 : : call);
10390 : 33 : break;
10391 : 9 : case BUILT_IN_CLRSBG:
10392 : 9 : tem = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10393 : 18 : tem = fold_build2 (PLUS_EXPR, integer_type_node, tem,
10394 : : build_int_cst (integer_type_node,
10395 : : MAX_FIXED_MODE_SIZE / 2));
10396 : 18 : tem = fold_build3 (COND_EXPR, integer_type_node,
10397 : : fold_build2 (LT_EXPR, boolean_type_node,
10398 : : fold_build2 (BIT_XOR_EXPR, type,
10399 : : lo, hi),
10400 : : build_zero_cst (type)),
10401 : : build_int_cst (integer_type_node,
10402 : : MAX_FIXED_MODE_SIZE / 2 - 1),
10403 : : tem);
10404 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10405 : 9 : call = save_expr (call);
10406 : 18 : call = fold_build3 (COND_EXPR, integer_type_node,
10407 : : fold_build2 (NE_EXPR, boolean_type_node,
10408 : : call,
10409 : : build_int_cst (integer_type_node,
10410 : : MAX_FIXED_MODE_SIZE
10411 : : / 2 - 1)),
10412 : : call, tem);
10413 : 9 : break;
10414 : 9 : case BUILT_IN_FFSG:
10415 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10416 : 18 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10417 : : build_int_cst (integer_type_node,
10418 : : MAX_FIXED_MODE_SIZE / 2));
10419 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10420 : : fold_build2 (NE_EXPR, boolean_type_node,
10421 : : hi, build_zero_cst (type)),
10422 : : call, integer_zero_node);
10423 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10424 : : fold_build2 (NE_EXPR, boolean_type_node,
10425 : : lo, build_zero_cst (type)),
10426 : : fold_builtin_bit_query (loc, fcode, lo,
10427 : : NULL_TREE),
10428 : : call);
10429 : 9 : break;
10430 : 9 : case BUILT_IN_PARITYG:
10431 : 9 : call = fold_builtin_bit_query (loc, fcode,
10432 : : fold_build2 (BIT_XOR_EXPR, type,
10433 : : lo, hi), NULL_TREE);
10434 : 9 : break;
10435 : 35 : case BUILT_IN_POPCOUNTG:
10436 : 35 : call = fold_build2 (PLUS_EXPR, integer_type_node,
10437 : : fold_builtin_bit_query (loc, fcode, hi,
10438 : : NULL_TREE),
10439 : : fold_builtin_bit_query (loc, fcode, lo,
10440 : : NULL_TREE));
10441 : 35 : break;
10442 : 0 : default:
10443 : 0 : gcc_unreachable ();
10444 : : }
10445 : : }
10446 : : else
10447 : : {
10448 : : /* Only keep second argument to IFN_CLZ/IFN_CTZ if it is the
10449 : : value defined at zero during GIMPLE, or for large/huge _BitInt
10450 : : (which are then lowered during bitint lowering). */
10451 : 196258 : if (arg2 && TREE_CODE (TREE_TYPE (arg0)) != BITINT_TYPE)
10452 : : {
10453 : 109836 : int val;
10454 : 109836 : if (fcode == BUILT_IN_CLZG)
10455 : : {
10456 : 62818 : if (CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10457 : : val) != 2
10458 : 62818 : || wi::to_widest (arg2) != val)
10459 : 62806 : arg2 = NULL_TREE;
10460 : : }
10461 : 47018 : else if (CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10462 : : val) != 2
10463 : 47018 : || wi::to_widest (arg2) != val)
10464 : 47018 : arg2 = NULL_TREE;
10465 : 109836 : if (!direct_internal_fn_supported_p (ifn, arg0_type,
10466 : : OPTIMIZE_FOR_BOTH))
10467 : : arg2 = NULL_TREE;
10468 : 109800 : if (arg2 == NULL_TREE)
10469 : 109824 : arg0 = save_expr (arg0);
10470 : : }
10471 : 196258 : if (fcodei == END_BUILTINS || arg2)
10472 : 194 : call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
10473 : : arg2 ? 2 : 1, arg0, arg2);
10474 : : else
10475 : 196146 : call = build_call_expr_loc (loc, builtin_decl_explicit (fcodei), 1,
10476 : : arg0);
10477 : : }
10478 : 226966 : if (addend)
10479 : 7119 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10480 : : build_int_cst (integer_type_node, addend));
10481 : 226966 : if (arg1 && arg2 == NULL_TREE)
10482 : 116892 : call = fold_build3 (COND_EXPR, integer_type_node,
10483 : : fold_build2 (NE_EXPR, boolean_type_node,
10484 : : arg0, build_zero_cst (arg0_type)),
10485 : : call, arg1);
10486 : :
10487 : : return call;
10488 : : }
10489 : :
10490 : : /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
10491 : : that return both result of arithmetics and overflowed boolean
10492 : : flag in a complex integer result. */
10493 : :
10494 : : static tree
10495 : 54 : fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
10496 : : tree *args)
10497 : : {
10498 : 54 : enum internal_fn ifn;
10499 : :
10500 : 54 : switch (fcode)
10501 : : {
10502 : : case BUILT_IN_ADDC:
10503 : : case BUILT_IN_ADDCL:
10504 : : case BUILT_IN_ADDCLL:
10505 : : ifn = IFN_ADD_OVERFLOW;
10506 : : break;
10507 : 28 : case BUILT_IN_SUBC:
10508 : 28 : case BUILT_IN_SUBCL:
10509 : 28 : case BUILT_IN_SUBCLL:
10510 : 28 : ifn = IFN_SUB_OVERFLOW;
10511 : 28 : break;
10512 : 0 : default:
10513 : 0 : gcc_unreachable ();
10514 : : }
10515 : :
10516 : 54 : tree type = TREE_TYPE (args[0]);
10517 : 54 : tree ctype = build_complex_type (type);
10518 : 54 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10519 : : args[0], args[1]);
10520 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10521 : : as while the call itself is const, the REALPART_EXPR store is
10522 : : certainly not. And in any case, we want just one call,
10523 : : not multiple and trying to CSE them later. */
10524 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10525 : 54 : tree tgt = save_expr (call);
10526 : 54 : tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10527 : 54 : tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10528 : 54 : call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10529 : : intres, args[2]);
10530 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10531 : 54 : tgt = save_expr (call);
10532 : 54 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10533 : 54 : tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10534 : 54 : ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
10535 : 54 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
10536 : 54 : tree store
10537 : 54 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10538 : 54 : return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10539 : : }
10540 : :
10541 : : /* Fold a call to __builtin_FILE to a constant string. */
10542 : :
10543 : : static inline tree
10544 : 5812 : fold_builtin_FILE (location_t loc)
10545 : : {
10546 : 5812 : if (const char *fname = LOCATION_FILE (loc))
10547 : : {
10548 : : /* The documentation says this builtin is equivalent to the preprocessor
10549 : : __FILE__ macro so it appears appropriate to use the same file prefix
10550 : : mappings. */
10551 : 5812 : fname = remap_macro_filename (fname);
10552 : 5812 : return build_string_literal (fname);
10553 : : }
10554 : :
10555 : 0 : return build_string_literal ("");
10556 : : }
10557 : :
10558 : : /* Fold a call to __builtin_FUNCTION to a constant string. */
10559 : :
10560 : : static inline tree
10561 : 68 : fold_builtin_FUNCTION ()
10562 : : {
10563 : 68 : const char *name = "";
10564 : :
10565 : 68 : if (current_function_decl)
10566 : 43 : name = lang_hooks.decl_printable_name (current_function_decl, 0);
10567 : :
10568 : 68 : return build_string_literal (name);
10569 : : }
10570 : :
10571 : : /* Fold a call to __builtin_LINE to an integer constant. */
10572 : :
10573 : : static inline tree
10574 : 11650 : fold_builtin_LINE (location_t loc, tree type)
10575 : : {
10576 : 11650 : return build_int_cst (type, LOCATION_LINE (loc));
10577 : : }
10578 : :
10579 : : /* Fold a call to built-in function FNDECL with 0 arguments.
10580 : : This function returns NULL_TREE if no simplification was possible. */
10581 : :
10582 : : static tree
10583 : 21956293 : fold_builtin_0 (location_t loc, tree fndecl)
10584 : : {
10585 : 21956293 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10586 : 21956293 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10587 : 21956293 : switch (fcode)
10588 : : {
10589 : 5812 : case BUILT_IN_FILE:
10590 : 5812 : return fold_builtin_FILE (loc);
10591 : :
10592 : 68 : case BUILT_IN_FUNCTION:
10593 : 68 : return fold_builtin_FUNCTION ();
10594 : :
10595 : 11650 : case BUILT_IN_LINE:
10596 : 11650 : return fold_builtin_LINE (loc, type);
10597 : :
10598 : 34738 : CASE_FLT_FN (BUILT_IN_INF):
10599 : 34738 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
10600 : 34738 : case BUILT_IN_INFD32:
10601 : 34738 : case BUILT_IN_INFD64:
10602 : 34738 : case BUILT_IN_INFD128:
10603 : 34738 : case BUILT_IN_INFD64X:
10604 : 34738 : return fold_builtin_inf (loc, type, true);
10605 : :
10606 : 182960 : CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10607 : 182960 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
10608 : 182960 : return fold_builtin_inf (loc, type, false);
10609 : :
10610 : 0 : case BUILT_IN_CLASSIFY_TYPE:
10611 : 0 : return fold_builtin_classify_type (NULL_TREE);
10612 : :
10613 : 16728066 : case BUILT_IN_UNREACHABLE:
10614 : : /* Rewrite any explicit calls to __builtin_unreachable. */
10615 : 16728066 : if (sanitize_flags_p (SANITIZE_UNREACHABLE))
10616 : 115 : return build_builtin_unreachable (loc);
10617 : : break;
10618 : :
10619 : : default:
10620 : : break;
10621 : : }
10622 : : return NULL_TREE;
10623 : : }
10624 : :
10625 : : /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10626 : : This function returns NULL_TREE if no simplification was possible. */
10627 : :
10628 : : static tree
10629 : 15673940 : fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
10630 : : {
10631 : 15673940 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10632 : 15673940 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10633 : :
10634 : 15673940 : if (error_operand_p (arg0))
10635 : : return NULL_TREE;
10636 : :
10637 : 15673940 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
10638 : : return ret;
10639 : :
10640 : 15205720 : switch (fcode)
10641 : : {
10642 : 766788 : case BUILT_IN_CONSTANT_P:
10643 : 766788 : {
10644 : 766788 : tree val = fold_builtin_constant_p (arg0);
10645 : :
10646 : : /* Gimplification will pull the CALL_EXPR for the builtin out of
10647 : : an if condition. When not optimizing, we'll not CSE it back.
10648 : : To avoid link error types of regressions, return false now. */
10649 : 766788 : if (!val && !optimize)
10650 : 1178 : val = integer_zero_node;
10651 : :
10652 : : return val;
10653 : : }
10654 : :
10655 : 2142 : case BUILT_IN_CLASSIFY_TYPE:
10656 : 2142 : return fold_builtin_classify_type (arg0);
10657 : :
10658 : 410909 : case BUILT_IN_STRLEN:
10659 : 410909 : return fold_builtin_strlen (loc, expr, type, arg0);
10660 : :
10661 : 354111 : CASE_FLT_FN (BUILT_IN_FABS):
10662 : 354111 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10663 : 354111 : case BUILT_IN_FABSD32:
10664 : 354111 : case BUILT_IN_FABSD64:
10665 : 354111 : case BUILT_IN_FABSD128:
10666 : 354111 : case BUILT_IN_FABSD64X:
10667 : 354111 : return fold_builtin_fabs (loc, arg0, type);
10668 : :
10669 : 91955 : case BUILT_IN_ABS:
10670 : 91955 : case BUILT_IN_LABS:
10671 : 91955 : case BUILT_IN_LLABS:
10672 : 91955 : case BUILT_IN_IMAXABS:
10673 : 91955 : case BUILT_IN_UABS:
10674 : 91955 : case BUILT_IN_ULABS:
10675 : 91955 : case BUILT_IN_ULLABS:
10676 : 91955 : case BUILT_IN_UIMAXABS:
10677 : 91955 : return fold_builtin_abs (loc, arg0, type);
10678 : :
10679 : 24501 : CASE_FLT_FN (BUILT_IN_CONJ):
10680 : 24501 : if (validate_arg (arg0, COMPLEX_TYPE)
10681 : 24501 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10682 : 24501 : return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10683 : : break;
10684 : :
10685 : 764 : CASE_FLT_FN (BUILT_IN_CREAL):
10686 : 764 : if (validate_arg (arg0, COMPLEX_TYPE)
10687 : 764 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10688 : 764 : return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
10689 : : break;
10690 : :
10691 : 1884 : CASE_FLT_FN (BUILT_IN_CIMAG):
10692 : 1884 : if (validate_arg (arg0, COMPLEX_TYPE)
10693 : 1884 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10694 : 1884 : return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10695 : : break;
10696 : :
10697 : 112919 : CASE_FLT_FN (BUILT_IN_CARG):
10698 : 112919 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
10699 : 112919 : return fold_builtin_carg (loc, arg0, type);
10700 : :
10701 : 211 : case BUILT_IN_ISASCII:
10702 : 211 : return fold_builtin_isascii (loc, arg0);
10703 : :
10704 : 168 : case BUILT_IN_TOASCII:
10705 : 168 : return fold_builtin_toascii (loc, arg0);
10706 : :
10707 : 325 : case BUILT_IN_ISDIGIT:
10708 : 325 : return fold_builtin_isdigit (loc, arg0);
10709 : :
10710 : 557644 : CASE_FLT_FN (BUILT_IN_FINITE):
10711 : 557644 : case BUILT_IN_FINITED32:
10712 : 557644 : case BUILT_IN_FINITED64:
10713 : 557644 : case BUILT_IN_FINITED128:
10714 : 557644 : case BUILT_IN_ISFINITE:
10715 : 557644 : {
10716 : 557644 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10717 : 557644 : if (ret)
10718 : : return ret;
10719 : 557420 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10720 : : }
10721 : :
10722 : 261476 : CASE_FLT_FN (BUILT_IN_ISINF):
10723 : 261476 : case BUILT_IN_ISINFD32:
10724 : 261476 : case BUILT_IN_ISINFD64:
10725 : 261476 : case BUILT_IN_ISINFD128:
10726 : 261476 : {
10727 : 261476 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10728 : 261476 : if (ret)
10729 : : return ret;
10730 : 261355 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10731 : : }
10732 : :
10733 : 258049 : case BUILT_IN_ISNORMAL:
10734 : 258049 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10735 : :
10736 : 708 : case BUILT_IN_ISINF_SIGN:
10737 : 708 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10738 : :
10739 : 261099 : CASE_FLT_FN (BUILT_IN_ISNAN):
10740 : 261099 : case BUILT_IN_ISNAND32:
10741 : 261099 : case BUILT_IN_ISNAND64:
10742 : 261099 : case BUILT_IN_ISNAND128:
10743 : 261099 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10744 : :
10745 : 28854 : case BUILT_IN_ISSIGNALING:
10746 : 28854 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
10747 : :
10748 : 593748 : case BUILT_IN_FREE:
10749 : 593748 : if (integer_zerop (arg0))
10750 : 1049 : return build_empty_stmt (loc);
10751 : : break;
10752 : :
10753 : 18013 : case BUILT_IN_CLZG:
10754 : 18013 : case BUILT_IN_CTZG:
10755 : 18013 : case BUILT_IN_CLRSBG:
10756 : 18013 : case BUILT_IN_FFSG:
10757 : 18013 : case BUILT_IN_PARITYG:
10758 : 18013 : case BUILT_IN_POPCOUNTG:
10759 : 18013 : return fold_builtin_bit_query (loc, fcode, arg0, NULL_TREE);
10760 : :
10761 : : default:
10762 : : break;
10763 : : }
10764 : :
10765 : : return NULL_TREE;
10766 : :
10767 : : }
10768 : :
10769 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL
10770 : : with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
10771 : : if no simplification was possible. */
10772 : :
10773 : : static tree
10774 : 16624865 : fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
10775 : : {
10776 : 16624865 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10777 : 16624865 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10778 : :
10779 : 16624865 : if (error_operand_p (arg0)
10780 : 16624865 : || error_operand_p (arg1))
10781 : : return NULL_TREE;
10782 : :
10783 : 16624861 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
10784 : : return ret;
10785 : :
10786 : 16424391 : switch (fcode)
10787 : : {
10788 : 6720 : CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10789 : 6720 : CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10790 : 6720 : if (validate_arg (arg0, REAL_TYPE)
10791 : 6720 : && validate_arg (arg1, POINTER_TYPE))
10792 : 6720 : return do_mpfr_lgamma_r (arg0, arg1, type);
10793 : : break;
10794 : :
10795 : 112852 : CASE_FLT_FN (BUILT_IN_FREXP):
10796 : 112852 : return fold_builtin_frexp (loc, arg0, arg1, type);
10797 : :
10798 : 74091 : CASE_FLT_FN (BUILT_IN_MODF):
10799 : 74091 : return fold_builtin_modf (loc, arg0, arg1, type);
10800 : :
10801 : 2655 : case BUILT_IN_STRSPN:
10802 : 2655 : return fold_builtin_strspn (loc, expr, arg0, arg1, type);
10803 : :
10804 : 2553 : case BUILT_IN_STRCSPN:
10805 : 2553 : return fold_builtin_strcspn (loc, expr, arg0, arg1, type);
10806 : :
10807 : 81573 : case BUILT_IN_STRPBRK:
10808 : 81573 : return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
10809 : :
10810 : 4841962 : case BUILT_IN_EXPECT:
10811 : 4841962 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
10812 : :
10813 : 519558 : case BUILT_IN_ISGREATER:
10814 : 519558 : return fold_builtin_unordered_cmp (loc, fndecl,
10815 : 519558 : arg0, arg1, UNLE_EXPR, LE_EXPR);
10816 : 516187 : case BUILT_IN_ISGREATEREQUAL:
10817 : 516187 : return fold_builtin_unordered_cmp (loc, fndecl,
10818 : 516187 : arg0, arg1, UNLT_EXPR, LT_EXPR);
10819 : 259707 : case BUILT_IN_ISLESS:
10820 : 259707 : return fold_builtin_unordered_cmp (loc, fndecl,
10821 : 259707 : arg0, arg1, UNGE_EXPR, GE_EXPR);
10822 : 1073636 : case BUILT_IN_ISLESSEQUAL:
10823 : 1073636 : return fold_builtin_unordered_cmp (loc, fndecl,
10824 : 1073636 : arg0, arg1, UNGT_EXPR, GT_EXPR);
10825 : 257843 : case BUILT_IN_ISLESSGREATER:
10826 : 257843 : return fold_builtin_unordered_cmp (loc, fndecl,
10827 : 257843 : arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10828 : 259080 : case BUILT_IN_ISUNORDERED:
10829 : 259080 : return fold_builtin_unordered_cmp (loc, fndecl,
10830 : : arg0, arg1, UNORDERED_EXPR,
10831 : 259080 : NOP_EXPR);
10832 : :
10833 : 702 : case BUILT_IN_ISEQSIG:
10834 : 702 : return fold_builtin_iseqsig (loc, arg0, arg1);
10835 : :
10836 : : /* We do the folding for va_start in the expander. */
10837 : : case BUILT_IN_VA_START:
10838 : : break;
10839 : :
10840 : 199058 : case BUILT_IN_OBJECT_SIZE:
10841 : 199058 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
10842 : 199058 : return fold_builtin_object_size (arg0, arg1, fcode);
10843 : :
10844 : 49442 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10845 : 49442 : return fold_builtin_atomic_always_lock_free (arg0, arg1);
10846 : :
10847 : 39327 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10848 : 39327 : return fold_builtin_atomic_is_lock_free (arg0, arg1);
10849 : :
10850 : 147668 : case BUILT_IN_CLZG:
10851 : 147668 : case BUILT_IN_CTZG:
10852 : 147668 : return fold_builtin_bit_query (loc, fcode, arg0, arg1);
10853 : :
10854 : : default:
10855 : : break;
10856 : : }
10857 : : return NULL_TREE;
10858 : : }
10859 : :
10860 : : /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10861 : : and ARG2.
10862 : : This function returns NULL_TREE if no simplification was possible. */
10863 : :
10864 : : static tree
10865 : 6284929 : fold_builtin_3 (location_t loc, tree fndecl,
10866 : : tree arg0, tree arg1, tree arg2)
10867 : : {
10868 : 6284929 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10869 : 6284929 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10870 : :
10871 : 6284929 : if (error_operand_p (arg0)
10872 : 6284929 : || error_operand_p (arg1)
10873 : 12569858 : || error_operand_p (arg2))
10874 : : return NULL_TREE;
10875 : :
10876 : 6284927 : if (tree ret = fold_const_call (as_combined_fn (fcode), type,
10877 : : arg0, arg1, arg2))
10878 : : return ret;
10879 : :
10880 : 6268085 : switch (fcode)
10881 : : {
10882 : :
10883 : 144 : CASE_FLT_FN (BUILT_IN_SINCOS):
10884 : 144 : return fold_builtin_sincos (loc, arg0, arg1, arg2);
10885 : :
10886 : 84618 : CASE_FLT_FN (BUILT_IN_REMQUO):
10887 : 84618 : if (validate_arg (arg0, REAL_TYPE)
10888 : 84618 : && validate_arg (arg1, REAL_TYPE)
10889 : 169236 : && validate_arg (arg2, POINTER_TYPE))
10890 : 84618 : return do_mpfr_remquo (arg0, arg1, arg2);
10891 : : break;
10892 : :
10893 : 2395801 : case BUILT_IN_MEMCMP:
10894 : 2395801 : return fold_builtin_memcmp (loc, arg0, arg1, arg2);
10895 : :
10896 : 487515 : case BUILT_IN_EXPECT:
10897 : 487515 : return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
10898 : :
10899 : 337 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
10900 : 337 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
10901 : :
10902 : 177855 : case BUILT_IN_ADD_OVERFLOW:
10903 : 177855 : case BUILT_IN_SUB_OVERFLOW:
10904 : 177855 : case BUILT_IN_MUL_OVERFLOW:
10905 : 177855 : case BUILT_IN_ADD_OVERFLOW_P:
10906 : 177855 : case BUILT_IN_SUB_OVERFLOW_P:
10907 : 177855 : case BUILT_IN_MUL_OVERFLOW_P:
10908 : 177855 : case BUILT_IN_SADD_OVERFLOW:
10909 : 177855 : case BUILT_IN_SADDL_OVERFLOW:
10910 : 177855 : case BUILT_IN_SADDLL_OVERFLOW:
10911 : 177855 : case BUILT_IN_SSUB_OVERFLOW:
10912 : 177855 : case BUILT_IN_SSUBL_OVERFLOW:
10913 : 177855 : case BUILT_IN_SSUBLL_OVERFLOW:
10914 : 177855 : case BUILT_IN_SMUL_OVERFLOW:
10915 : 177855 : case BUILT_IN_SMULL_OVERFLOW:
10916 : 177855 : case BUILT_IN_SMULLL_OVERFLOW:
10917 : 177855 : case BUILT_IN_UADD_OVERFLOW:
10918 : 177855 : case BUILT_IN_UADDL_OVERFLOW:
10919 : 177855 : case BUILT_IN_UADDLL_OVERFLOW:
10920 : 177855 : case BUILT_IN_USUB_OVERFLOW:
10921 : 177855 : case BUILT_IN_USUBL_OVERFLOW:
10922 : 177855 : case BUILT_IN_USUBLL_OVERFLOW:
10923 : 177855 : case BUILT_IN_UMUL_OVERFLOW:
10924 : 177855 : case BUILT_IN_UMULL_OVERFLOW:
10925 : 177855 : case BUILT_IN_UMULLL_OVERFLOW:
10926 : 177855 : return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10927 : :
10928 : : default:
10929 : : break;
10930 : : }
10931 : : return NULL_TREE;
10932 : : }
10933 : :
10934 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL.
10935 : : ARGS is an array of NARGS arguments. IGNORE is true if the result
10936 : : of the function call is ignored. This function returns NULL_TREE
10937 : : if no simplification was possible. */
10938 : :
10939 : : static tree
10940 : 63160030 : fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
10941 : : int nargs, bool)
10942 : : {
10943 : 63160030 : tree ret = NULL_TREE;
10944 : :
10945 : 63160030 : switch (nargs)
10946 : : {
10947 : 21956293 : case 0:
10948 : 21956293 : ret = fold_builtin_0 (loc, fndecl);
10949 : 21956293 : break;
10950 : 15673940 : case 1:
10951 : 15673940 : ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
10952 : 15673940 : break;
10953 : 16624865 : case 2:
10954 : 16624865 : ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
10955 : 16624865 : break;
10956 : 6284929 : case 3:
10957 : 6284929 : ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10958 : 6284929 : break;
10959 : 2620003 : default:
10960 : 2620003 : ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10961 : 2620003 : break;
10962 : : }
10963 : 63160030 : if (ret)
10964 : : {
10965 : 6435131 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10966 : 6435131 : SET_EXPR_LOCATION (ret, loc);
10967 : 6435131 : return ret;
10968 : : }
10969 : : return NULL_TREE;
10970 : : }
10971 : :
10972 : : /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10973 : : list ARGS along with N new arguments in NEWARGS. SKIP is the number
10974 : : of arguments in ARGS to be omitted. OLDNARGS is the number of
10975 : : elements in ARGS. */
10976 : :
10977 : : static tree
10978 : 4 : rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10979 : : int skip, tree fndecl, int n, va_list newargs)
10980 : : {
10981 : 4 : int nargs = oldnargs - skip + n;
10982 : 4 : tree *buffer;
10983 : :
10984 : 4 : if (n > 0)
10985 : : {
10986 : 0 : int i, j;
10987 : :
10988 : 0 : buffer = XALLOCAVEC (tree, nargs);
10989 : 0 : for (i = 0; i < n; i++)
10990 : 0 : buffer[i] = va_arg (newargs, tree);
10991 : 0 : for (j = skip; j < oldnargs; j++, i++)
10992 : 0 : buffer[i] = args[j];
10993 : : }
10994 : : else
10995 : 4 : buffer = args + skip;
10996 : :
10997 : 4 : return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10998 : : }
10999 : :
11000 : : /* Return true if FNDECL shouldn't be folded right now.
11001 : : If a built-in function has an inline attribute always_inline
11002 : : wrapper, defer folding it after always_inline functions have
11003 : : been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11004 : : might not be performed. */
11005 : :
11006 : : bool
11007 : 142660100 : avoid_folding_inline_builtin (tree fndecl)
11008 : : {
11009 : 142660100 : return (DECL_DECLARED_INLINE_P (fndecl)
11010 : 9443 : && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11011 : 9401 : && cfun
11012 : 9401 : && !cfun->always_inline_functions_inlined
11013 : 142669501 : && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11014 : : }
11015 : :
11016 : : /* A wrapper function for builtin folding that prevents warnings for
11017 : : "statement without effect" and the like, caused by removing the
11018 : : call node earlier than the warning is generated. */
11019 : :
11020 : : tree
11021 : 179492822 : fold_call_expr (location_t loc, tree exp, bool ignore)
11022 : : {
11023 : 179492822 : tree ret = NULL_TREE;
11024 : 179492822 : tree fndecl = get_callee_fndecl (exp);
11025 : 177750765 : if (fndecl && fndecl_built_in_p (fndecl)
11026 : : /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11027 : : yet. Defer folding until we see all the arguments
11028 : : (after inlining). */
11029 : 235626665 : && !CALL_EXPR_VA_ARG_PACK (exp))
11030 : : {
11031 : 56133818 : int nargs = call_expr_nargs (exp);
11032 : :
11033 : : /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11034 : : instead last argument is __builtin_va_arg_pack (). Defer folding
11035 : : even in that case, until arguments are finalized. */
11036 : 56133818 : if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11037 : : {
11038 : 250138 : tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11039 : 250138 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11040 : : return NULL_TREE;
11041 : : }
11042 : :
11043 : 56133754 : if (avoid_folding_inline_builtin (fndecl))
11044 : : return NULL_TREE;
11045 : :
11046 : 56130219 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11047 : 67163572 : return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11048 : 67163572 : CALL_EXPR_ARGP (exp), ignore);
11049 : : else
11050 : : {
11051 : 22548433 : tree *args = CALL_EXPR_ARGP (exp);
11052 : 22548433 : ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
11053 : 22548433 : if (ret)
11054 : : return ret;
11055 : : }
11056 : : }
11057 : : return NULL_TREE;
11058 : : }
11059 : :
11060 : : /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
11061 : : N arguments are passed in the array ARGARRAY. Return a folded
11062 : : expression or NULL_TREE if no simplification was possible. */
11063 : :
11064 : : tree
11065 : 63386200 : fold_builtin_call_array (location_t loc, tree,
11066 : : tree fn,
11067 : : int n,
11068 : : tree *argarray)
11069 : : {
11070 : 63386200 : if (TREE_CODE (fn) != ADDR_EXPR)
11071 : : return NULL_TREE;
11072 : :
11073 : 63386200 : tree fndecl = TREE_OPERAND (fn, 0);
11074 : 63386200 : if (TREE_CODE (fndecl) == FUNCTION_DECL
11075 : 63386200 : && fndecl_built_in_p (fndecl))
11076 : : {
11077 : : /* If last argument is __builtin_va_arg_pack (), arguments to this
11078 : : function are not finalized yet. Defer folding until they are. */
11079 : 62811129 : if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11080 : : {
11081 : 102344 : tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11082 : 102344 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11083 : : return NULL_TREE;
11084 : : }
11085 : 62811102 : if (avoid_folding_inline_builtin (fndecl))
11086 : : return NULL_TREE;
11087 : 62811102 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11088 : 32436593 : return targetm.fold_builtin (fndecl, n, argarray, false);
11089 : : else
11090 : 30374509 : return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
11091 : : }
11092 : :
11093 : : return NULL_TREE;
11094 : : }
11095 : :
11096 : : /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11097 : : along with N new arguments specified as the "..." parameters. SKIP
11098 : : is the number of arguments in EXP to be omitted. This function is used
11099 : : to do varargs-to-varargs transformations. */
11100 : :
11101 : : static tree
11102 : 4 : rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11103 : : {
11104 : 4 : va_list ap;
11105 : 4 : tree t;
11106 : :
11107 : 4 : va_start (ap, n);
11108 : 8 : t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11109 : 4 : CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11110 : 4 : va_end (ap);
11111 : :
11112 : 4 : return t;
11113 : : }
11114 : :
11115 : : /* Validate a single argument ARG against a tree code CODE representing
11116 : : a type. Return true when argument is valid. */
11117 : :
11118 : : static bool
11119 : 13386893 : validate_arg (const_tree arg, enum tree_code code)
11120 : : {
11121 : 13386893 : if (!arg)
11122 : : return false;
11123 : 13386870 : else if (code == POINTER_TYPE)
11124 : 6623751 : return POINTER_TYPE_P (TREE_TYPE (arg));
11125 : 6763119 : else if (code == INTEGER_TYPE)
11126 : 3579298 : return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11127 : 3183821 : return code == TREE_CODE (TREE_TYPE (arg));
11128 : : }
11129 : :
11130 : : /* This function validates the types of a function call argument list
11131 : : against a specified list of tree_codes. If the last specifier is a 0,
11132 : : that represents an ellipses, otherwise the last specifier must be a
11133 : : VOID_TYPE.
11134 : :
11135 : : This is the GIMPLE version of validate_arglist. Eventually we want to
11136 : : completely convert builtins.cc to work from GIMPLEs and the tree based
11137 : : validate_arglist will then be removed. */
11138 : :
11139 : : bool
11140 : 90 : validate_gimple_arglist (const gcall *call, ...)
11141 : : {
11142 : 90 : enum tree_code code;
11143 : 90 : bool res = 0;
11144 : 90 : va_list ap;
11145 : 90 : const_tree arg;
11146 : 90 : size_t i;
11147 : :
11148 : 90 : va_start (ap, call);
11149 : 90 : i = 0;
11150 : :
11151 : 360 : do
11152 : : {
11153 : 360 : code = (enum tree_code) va_arg (ap, int);
11154 : 360 : switch (code)
11155 : : {
11156 : 0 : case 0:
11157 : : /* This signifies an ellipses, any further arguments are all ok. */
11158 : 0 : res = true;
11159 : 0 : goto end;
11160 : 90 : case VOID_TYPE:
11161 : : /* This signifies an endlink, if no arguments remain, return
11162 : : true, otherwise return false. */
11163 : 90 : res = (i == gimple_call_num_args (call));
11164 : 90 : goto end;
11165 : 270 : default:
11166 : : /* If no parameters remain or the parameter's code does not
11167 : : match the specified code, return false. Otherwise continue
11168 : : checking any remaining arguments. */
11169 : 270 : arg = gimple_call_arg (call, i++);
11170 : 270 : if (!validate_arg (arg, code))
11171 : 0 : goto end;
11172 : : break;
11173 : : }
11174 : : }
11175 : : while (1);
11176 : :
11177 : : /* We need gotos here since we can only have one VA_CLOSE in a
11178 : : function. */
11179 : 90 : end: ;
11180 : 90 : va_end (ap);
11181 : :
11182 : 90 : return res;
11183 : : }
11184 : :
11185 : : /* Default target-specific builtin expander that does nothing. */
11186 : :
11187 : : rtx
11188 : 0 : default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11189 : : rtx target ATTRIBUTE_UNUSED,
11190 : : rtx subtarget ATTRIBUTE_UNUSED,
11191 : : machine_mode mode ATTRIBUTE_UNUSED,
11192 : : int ignore ATTRIBUTE_UNUSED)
11193 : : {
11194 : 0 : return NULL_RTX;
11195 : : }
11196 : :
11197 : : /* Returns true is EXP represents data that would potentially reside
11198 : : in a readonly section. */
11199 : :
11200 : : bool
11201 : 195824 : readonly_data_expr (tree exp)
11202 : : {
11203 : 195824 : STRIP_NOPS (exp);
11204 : :
11205 : 195824 : if (TREE_CODE (exp) != ADDR_EXPR)
11206 : : return false;
11207 : :
11208 : 23753 : exp = get_base_address (TREE_OPERAND (exp, 0));
11209 : 23753 : if (!exp)
11210 : : return false;
11211 : :
11212 : : /* Make sure we call decl_readonly_section only for trees it
11213 : : can handle (since it returns true for everything it doesn't
11214 : : understand). */
11215 : 23753 : if (TREE_CODE (exp) == STRING_CST
11216 : 5781 : || TREE_CODE (exp) == CONSTRUCTOR
11217 : 5781 : || (VAR_P (exp) && TREE_STATIC (exp)))
11218 : 20711 : return decl_readonly_section (exp, 0);
11219 : : else
11220 : : return false;
11221 : : }
11222 : :
11223 : : /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11224 : : to the call, and TYPE is its return type.
11225 : :
11226 : : Return NULL_TREE if no simplification was possible, otherwise return the
11227 : : simplified form of the call as a tree.
11228 : :
11229 : : The simplified form may be a constant or other expression which
11230 : : computes the same value, but in a more efficient manner (including
11231 : : calls to other builtin functions).
11232 : :
11233 : : The call may contain arguments which need to be evaluated, but
11234 : : which are not useful to determine the result of the call. In
11235 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11236 : : COMPOUND_EXPR will be an argument which must be evaluated.
11237 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11238 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11239 : : form of the builtin function call. */
11240 : :
11241 : : static tree
11242 : 81573 : fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
11243 : : {
11244 : 81573 : if (!validate_arg (s1, POINTER_TYPE)
11245 : 81573 : || !validate_arg (s2, POINTER_TYPE))
11246 : : return NULL_TREE;
11247 : :
11248 : 81573 : tree fn;
11249 : 81573 : const char *p1, *p2;
11250 : :
11251 : 81573 : p2 = c_getstr (s2);
11252 : 81573 : if (p2 == NULL)
11253 : : return NULL_TREE;
11254 : :
11255 : 92 : p1 = c_getstr (s1);
11256 : 92 : if (p1 != NULL)
11257 : : {
11258 : 22 : const char *r = strpbrk (p1, p2);
11259 : 22 : tree tem;
11260 : :
11261 : 22 : if (r == NULL)
11262 : 0 : return build_int_cst (TREE_TYPE (s1), 0);
11263 : :
11264 : : /* Return an offset into the constant string argument. */
11265 : 22 : tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11266 : 22 : return fold_convert_loc (loc, type, tem);
11267 : : }
11268 : :
11269 : 70 : if (p2[0] == '\0')
11270 : : /* strpbrk(x, "") == NULL.
11271 : : Evaluate and ignore s1 in case it had side-effects. */
11272 : 26 : return omit_one_operand_loc (loc, type, integer_zero_node, s1);
11273 : :
11274 : 44 : if (p2[1] != '\0')
11275 : : return NULL_TREE; /* Really call strpbrk. */
11276 : :
11277 : 81525 : fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11278 : 41 : if (!fn)
11279 : : return NULL_TREE;
11280 : :
11281 : : /* New argument list transforming strpbrk(s1, s2) to
11282 : : strchr(s1, s2[0]). */
11283 : 41 : return build_call_expr_loc (loc, fn, 2, s1,
11284 : 41 : build_int_cst (integer_type_node, p2[0]));
11285 : : }
11286 : :
11287 : : /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11288 : : to the call.
11289 : :
11290 : : Return NULL_TREE if no simplification was possible, otherwise return the
11291 : : simplified form of the call as a tree.
11292 : :
11293 : : The simplified form may be a constant or other expression which
11294 : : computes the same value, but in a more efficient manner (including
11295 : : calls to other builtin functions).
11296 : :
11297 : : The call may contain arguments which need to be evaluated, but
11298 : : which are not useful to determine the result of the call. In
11299 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11300 : : COMPOUND_EXPR will be an argument which must be evaluated.
11301 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11302 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11303 : : form of the builtin function call. */
11304 : :
11305 : : static tree
11306 : 2655 : fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11307 : : {
11308 : 2655 : if (!validate_arg (s1, POINTER_TYPE)
11309 : 2655 : || !validate_arg (s2, POINTER_TYPE))
11310 : : return NULL_TREE;
11311 : :
11312 : 2655 : if (!check_nul_terminated_array (expr, s1)
11313 : 2655 : || !check_nul_terminated_array (expr, s2))
11314 : 60 : return NULL_TREE;
11315 : :
11316 : 2595 : const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11317 : :
11318 : : /* If either argument is "", return NULL_TREE. */
11319 : 2595 : if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11320 : : /* Evaluate and ignore both arguments in case either one has
11321 : : side-effects. */
11322 : 147 : return omit_two_operands_loc (loc, type, size_zero_node, s1, s2);
11323 : : return NULL_TREE;
11324 : : }
11325 : :
11326 : : /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11327 : : to the call.
11328 : :
11329 : : Return NULL_TREE if no simplification was possible, otherwise return the
11330 : : simplified form of the call as a tree.
11331 : :
11332 : : The simplified form may be a constant or other expression which
11333 : : computes the same value, but in a more efficient manner (including
11334 : : calls to other builtin functions).
11335 : :
11336 : : The call may contain arguments which need to be evaluated, but
11337 : : which are not useful to determine the result of the call. In
11338 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11339 : : COMPOUND_EXPR will be an argument which must be evaluated.
11340 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11341 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11342 : : form of the builtin function call. */
11343 : :
11344 : : static tree
11345 : 2553 : fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11346 : : {
11347 : 2553 : if (!validate_arg (s1, POINTER_TYPE)
11348 : 2553 : || !validate_arg (s2, POINTER_TYPE))
11349 : : return NULL_TREE;
11350 : :
11351 : 2553 : if (!check_nul_terminated_array (expr, s1)
11352 : 2553 : || !check_nul_terminated_array (expr, s2))
11353 : 60 : return NULL_TREE;
11354 : :
11355 : : /* If the first argument is "", return NULL_TREE. */
11356 : 2493 : const char *p1 = c_getstr (s1);
11357 : 2493 : if (p1 && *p1 == '\0')
11358 : : {
11359 : : /* Evaluate and ignore argument s2 in case it has
11360 : : side-effects. */
11361 : 65 : return omit_one_operand_loc (loc, type, size_zero_node, s2);
11362 : : }
11363 : :
11364 : : /* If the second argument is "", return __builtin_strlen(s1). */
11365 : 2428 : const char *p2 = c_getstr (s2);
11366 : 2428 : if (p2 && *p2 == '\0')
11367 : : {
11368 : 2488 : tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11369 : :
11370 : : /* If the replacement _DECL isn't initialized, don't do the
11371 : : transformation. */
11372 : 81 : if (!fn)
11373 : : return NULL_TREE;
11374 : :
11375 : 81 : return fold_convert_loc (loc, type,
11376 : 81 : build_call_expr_loc (loc, fn, 1, s1));
11377 : : }
11378 : : return NULL_TREE;
11379 : : }
11380 : :
11381 : : /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11382 : : produced. False otherwise. This is done so that we don't output the error
11383 : : or warning twice or three times. */
11384 : :
11385 : : bool
11386 : 41697 : fold_builtin_next_arg (tree exp, bool va_start_p)
11387 : : {
11388 : 41697 : tree fntype = TREE_TYPE (current_function_decl);
11389 : 41697 : int nargs = call_expr_nargs (exp);
11390 : 41697 : tree arg;
11391 : : /* There is good chance the current input_location points inside the
11392 : : definition of the va_start macro (perhaps on the token for
11393 : : builtin) in a system header, so warnings will not be emitted.
11394 : : Use the location in real source code. */
11395 : 41697 : location_t current_location =
11396 : 41697 : linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11397 : : NULL);
11398 : :
11399 : 41697 : if (!stdarg_p (fntype))
11400 : : {
11401 : 8 : error ("%<va_start%> used in function with fixed arguments");
11402 : 8 : return true;
11403 : : }
11404 : :
11405 : 41689 : if (va_start_p)
11406 : : {
11407 : 41539 : if (va_start_p && (nargs != 2))
11408 : : {
11409 : 0 : error ("wrong number of arguments to function %<va_start%>");
11410 : 0 : return true;
11411 : : }
11412 : 41539 : arg = CALL_EXPR_ARG (exp, 1);
11413 : : }
11414 : : /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11415 : : when we checked the arguments and if needed issued a warning. */
11416 : : else
11417 : : {
11418 : 150 : if (nargs == 0)
11419 : : {
11420 : : /* Evidently an out of date version of <stdarg.h>; can't validate
11421 : : va_start's second argument, but can still work as intended. */
11422 : 0 : warning_at (current_location,
11423 : 0 : OPT_Wvarargs,
11424 : : "%<__builtin_next_arg%> called without an argument");
11425 : 0 : return true;
11426 : : }
11427 : 150 : else if (nargs > 1)
11428 : : {
11429 : 0 : error ("wrong number of arguments to function %<__builtin_next_arg%>");
11430 : 0 : return true;
11431 : : }
11432 : 150 : arg = CALL_EXPR_ARG (exp, 0);
11433 : : }
11434 : :
11435 : 41689 : if (TREE_CODE (arg) == SSA_NAME
11436 : 41689 : && SSA_NAME_VAR (arg))
11437 : : arg = SSA_NAME_VAR (arg);
11438 : :
11439 : : /* We destructively modify the call to be __builtin_va_start (ap, 0)
11440 : : or __builtin_next_arg (0) the first time we see it, after checking
11441 : : the arguments and if needed issuing a warning. */
11442 : 41689 : if (!integer_zerop (arg))
11443 : : {
11444 : 7080 : tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11445 : :
11446 : : /* Strip off all nops for the sake of the comparison. This
11447 : : is not quite the same as STRIP_NOPS. It does more.
11448 : : We must also strip off INDIRECT_EXPR for C++ reference
11449 : : parameters. */
11450 : 14163 : while (CONVERT_EXPR_P (arg)
11451 : 14177 : || INDIRECT_REF_P (arg))
11452 : 14 : arg = TREE_OPERAND (arg, 0);
11453 : 7080 : if (arg != last_parm)
11454 : : {
11455 : : /* FIXME: Sometimes with the tree optimizers we can get the
11456 : : not the last argument even though the user used the last
11457 : : argument. We just warn and set the arg to be the last
11458 : : argument so that we will get wrong-code because of
11459 : : it. */
11460 : 12 : warning_at (current_location,
11461 : 12 : OPT_Wvarargs,
11462 : : "second parameter of %<va_start%> not last named argument");
11463 : : }
11464 : :
11465 : : /* Undefined by C99 7.15.1.4p4 (va_start):
11466 : : "If the parameter parmN is declared with the register storage
11467 : : class, with a function or array type, or with a type that is
11468 : : not compatible with the type that results after application of
11469 : : the default argument promotions, the behavior is undefined."
11470 : : */
11471 : 7068 : else if (DECL_REGISTER (arg))
11472 : : {
11473 : 12 : warning_at (current_location,
11474 : 12 : OPT_Wvarargs,
11475 : : "undefined behavior when second parameter of "
11476 : : "%<va_start%> is declared with %<register%> storage");
11477 : : }
11478 : :
11479 : : /* We want to verify the second parameter just once before the tree
11480 : : optimizers are run and then avoid keeping it in the tree,
11481 : : as otherwise we could warn even for correct code like:
11482 : : void foo (int i, ...)
11483 : : { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11484 : 7080 : if (va_start_p)
11485 : 7078 : CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11486 : : else
11487 : 2 : CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11488 : : }
11489 : : return false;
11490 : : }
11491 : :
11492 : :
11493 : : /* Expand a call EXP to __builtin_object_size. */
11494 : :
11495 : : static rtx
11496 : 617 : expand_builtin_object_size (tree exp)
11497 : : {
11498 : 617 : tree ost;
11499 : 617 : int object_size_type;
11500 : 617 : tree fndecl = get_callee_fndecl (exp);
11501 : :
11502 : 617 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11503 : : {
11504 : 0 : error ("first argument of %qD must be a pointer, second integer constant",
11505 : : fndecl);
11506 : 0 : expand_builtin_trap ();
11507 : 0 : return const0_rtx;
11508 : : }
11509 : :
11510 : 617 : ost = CALL_EXPR_ARG (exp, 1);
11511 : 617 : STRIP_NOPS (ost);
11512 : :
11513 : 617 : if (TREE_CODE (ost) != INTEGER_CST
11514 : 617 : || tree_int_cst_sgn (ost) < 0
11515 : 1234 : || compare_tree_int (ost, 3) > 0)
11516 : : {
11517 : 0 : error ("last argument of %qD is not integer constant between 0 and 3",
11518 : : fndecl);
11519 : 0 : expand_builtin_trap ();
11520 : 0 : return const0_rtx;
11521 : : }
11522 : :
11523 : 617 : object_size_type = tree_to_shwi (ost);
11524 : :
11525 : 617 : return object_size_type < 2 ? constm1_rtx : const0_rtx;
11526 : : }
11527 : :
11528 : : /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11529 : : FCODE is the BUILT_IN_* to use.
11530 : : Return NULL_RTX if we failed; the caller should emit a normal call,
11531 : : otherwise try to get the result in TARGET, if convenient (and in
11532 : : mode MODE if that's convenient). */
11533 : :
11534 : : static rtx
11535 : 835 : expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11536 : : enum built_in_function fcode)
11537 : : {
11538 : 1408 : if (!validate_arglist (exp,
11539 : : POINTER_TYPE,
11540 : : fcode == BUILT_IN_MEMSET_CHK
11541 : : ? INTEGER_TYPE : POINTER_TYPE,
11542 : : INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11543 : : return NULL_RTX;
11544 : :
11545 : 835 : tree dest = CALL_EXPR_ARG (exp, 0);
11546 : 835 : tree src = CALL_EXPR_ARG (exp, 1);
11547 : 835 : tree len = CALL_EXPR_ARG (exp, 2);
11548 : 835 : tree size = CALL_EXPR_ARG (exp, 3);
11549 : :
11550 : : /* FIXME: Set access mode to write only for memset et al. */
11551 : 835 : bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
11552 : : /*srcstr=*/NULL_TREE, size, access_read_write);
11553 : :
11554 : 835 : if (!tree_fits_uhwi_p (size))
11555 : : return NULL_RTX;
11556 : :
11557 : 625 : if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11558 : : {
11559 : : /* Avoid transforming the checking call to an ordinary one when
11560 : : an overflow has been detected or when the call couldn't be
11561 : : validated because the size is not constant. */
11562 : 185 : if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
11563 : : return NULL_RTX;
11564 : :
11565 : 0 : tree fn = NULL_TREE;
11566 : : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11567 : : mem{cpy,pcpy,move,set} is available. */
11568 : 0 : switch (fcode)
11569 : : {
11570 : 0 : case BUILT_IN_MEMCPY_CHK:
11571 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11572 : 0 : break;
11573 : 0 : case BUILT_IN_MEMPCPY_CHK:
11574 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11575 : 0 : break;
11576 : 0 : case BUILT_IN_MEMMOVE_CHK:
11577 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11578 : 0 : break;
11579 : 0 : case BUILT_IN_MEMSET_CHK:
11580 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11581 : 0 : break;
11582 : : default:
11583 : : break;
11584 : : }
11585 : :
11586 : 0 : if (! fn)
11587 : : return NULL_RTX;
11588 : :
11589 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11590 : 0 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11591 : 0 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11592 : 0 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11593 : : }
11594 : 440 : else if (fcode == BUILT_IN_MEMSET_CHK)
11595 : : return NULL_RTX;
11596 : : else
11597 : : {
11598 : 293 : unsigned int dest_align = get_pointer_alignment (dest);
11599 : :
11600 : : /* If DEST is not a pointer type, call the normal function. */
11601 : 293 : if (dest_align == 0)
11602 : : return NULL_RTX;
11603 : :
11604 : : /* If SRC and DEST are the same (and not volatile), do nothing. */
11605 : 293 : if (operand_equal_p (src, dest, 0))
11606 : : {
11607 : 0 : tree expr;
11608 : :
11609 : 0 : if (fcode != BUILT_IN_MEMPCPY_CHK)
11610 : : {
11611 : : /* Evaluate and ignore LEN in case it has side-effects. */
11612 : 0 : expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11613 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
11614 : : }
11615 : :
11616 : 0 : expr = fold_build_pointer_plus (dest, len);
11617 : 0 : return expand_expr (expr, target, mode, EXPAND_NORMAL);
11618 : : }
11619 : :
11620 : : /* __memmove_chk special case. */
11621 : 293 : if (fcode == BUILT_IN_MEMMOVE_CHK)
11622 : : {
11623 : 79 : unsigned int src_align = get_pointer_alignment (src);
11624 : :
11625 : 79 : if (src_align == 0)
11626 : : return NULL_RTX;
11627 : :
11628 : : /* If src is categorized for a readonly section we can use
11629 : : normal __memcpy_chk. */
11630 : 79 : if (readonly_data_expr (src))
11631 : : {
11632 : 15 : tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11633 : 15 : if (!fn)
11634 : : return NULL_RTX;
11635 : 15 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11636 : : dest, src, len, size);
11637 : 15 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11638 : 15 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11639 : 15 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11640 : : }
11641 : : }
11642 : 278 : return NULL_RTX;
11643 : : }
11644 : : }
11645 : :
11646 : : /* Emit warning if a buffer overflow is detected at compile time. */
11647 : :
11648 : : static void
11649 : 1135 : maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11650 : : {
11651 : : /* The source string. */
11652 : 1135 : tree srcstr = NULL_TREE;
11653 : : /* The size of the destination object returned by __builtin_object_size. */
11654 : 1135 : tree objsize = NULL_TREE;
11655 : : /* The string that is being concatenated with (as in __strcat_chk)
11656 : : or null if it isn't. */
11657 : 1135 : tree catstr = NULL_TREE;
11658 : : /* The maximum length of the source sequence in a bounded operation
11659 : : (such as __strncat_chk) or null if the operation isn't bounded
11660 : : (such as __strcat_chk). */
11661 : 1135 : tree maxread = NULL_TREE;
11662 : : /* The exact size of the access (such as in __strncpy_chk). */
11663 : 1135 : tree size = NULL_TREE;
11664 : : /* The access by the function that's checked. Except for snprintf
11665 : : both writing and reading is checked. */
11666 : 1135 : access_mode mode = access_read_write;
11667 : :
11668 : 1135 : switch (fcode)
11669 : : {
11670 : 276 : case BUILT_IN_STRCPY_CHK:
11671 : 276 : case BUILT_IN_STPCPY_CHK:
11672 : 276 : srcstr = CALL_EXPR_ARG (exp, 1);
11673 : 276 : objsize = CALL_EXPR_ARG (exp, 2);
11674 : 276 : break;
11675 : :
11676 : 198 : case BUILT_IN_STRCAT_CHK:
11677 : : /* For __strcat_chk the warning will be emitted only if overflowing
11678 : : by at least strlen (dest) + 1 bytes. */
11679 : 198 : catstr = CALL_EXPR_ARG (exp, 0);
11680 : 198 : srcstr = CALL_EXPR_ARG (exp, 1);
11681 : 198 : objsize = CALL_EXPR_ARG (exp, 2);
11682 : 198 : break;
11683 : :
11684 : 109 : case BUILT_IN_STRNCAT_CHK:
11685 : 109 : catstr = CALL_EXPR_ARG (exp, 0);
11686 : 109 : srcstr = CALL_EXPR_ARG (exp, 1);
11687 : 109 : maxread = CALL_EXPR_ARG (exp, 2);
11688 : 109 : objsize = CALL_EXPR_ARG (exp, 3);
11689 : 109 : break;
11690 : :
11691 : 243 : case BUILT_IN_STRNCPY_CHK:
11692 : 243 : case BUILT_IN_STPNCPY_CHK:
11693 : 243 : srcstr = CALL_EXPR_ARG (exp, 1);
11694 : 243 : size = CALL_EXPR_ARG (exp, 2);
11695 : 243 : objsize = CALL_EXPR_ARG (exp, 3);
11696 : 243 : break;
11697 : :
11698 : 309 : case BUILT_IN_SNPRINTF_CHK:
11699 : 309 : case BUILT_IN_VSNPRINTF_CHK:
11700 : 309 : maxread = CALL_EXPR_ARG (exp, 1);
11701 : 309 : objsize = CALL_EXPR_ARG (exp, 3);
11702 : : /* The only checked access the write to the destination. */
11703 : 309 : mode = access_write_only;
11704 : 309 : break;
11705 : 0 : default:
11706 : 0 : gcc_unreachable ();
11707 : : }
11708 : :
11709 : 1135 : if (catstr && maxread)
11710 : : {
11711 : : /* Check __strncat_chk. There is no way to determine the length
11712 : : of the string to which the source string is being appended so
11713 : : just warn when the length of the source string is not known. */
11714 : 109 : check_strncat_sizes (exp, objsize);
11715 : 109 : return;
11716 : : }
11717 : :
11718 : 1026 : check_access (exp, size, maxread, srcstr, objsize, mode);
11719 : : }
11720 : :
11721 : : /* Emit warning if a buffer overflow is detected at compile time
11722 : : in __sprintf_chk/__vsprintf_chk calls. */
11723 : :
11724 : : static void
11725 : 1329 : maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11726 : : {
11727 : 1329 : tree size, len, fmt;
11728 : 1329 : const char *fmt_str;
11729 : 1329 : int nargs = call_expr_nargs (exp);
11730 : :
11731 : : /* Verify the required arguments in the original call. */
11732 : :
11733 : 1329 : if (nargs < 4)
11734 : : return;
11735 : 1329 : size = CALL_EXPR_ARG (exp, 2);
11736 : 1329 : fmt = CALL_EXPR_ARG (exp, 3);
11737 : :
11738 : 1329 : if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11739 : 21 : return;
11740 : :
11741 : : /* Check whether the format is a literal string constant. */
11742 : 1308 : fmt_str = c_getstr (fmt);
11743 : 1308 : if (fmt_str == NULL)
11744 : : return;
11745 : :
11746 : 1272 : if (!init_target_chars ())
11747 : : return;
11748 : :
11749 : : /* If the format doesn't contain % args or %%, we know its size. */
11750 : 1272 : if (strchr (fmt_str, target_percent) == 0)
11751 : 22 : len = build_int_cstu (size_type_node, strlen (fmt_str));
11752 : : /* If the format is "%s" and first ... argument is a string literal,
11753 : : we know it too. */
11754 : 1250 : else if (fcode == BUILT_IN_SPRINTF_CHK
11755 : 1094 : && strcmp (fmt_str, target_percent_s) == 0)
11756 : : {
11757 : 49 : tree arg;
11758 : :
11759 : 49 : if (nargs < 5)
11760 : : return;
11761 : 49 : arg = CALL_EXPR_ARG (exp, 4);
11762 : 49 : if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11763 : : return;
11764 : :
11765 : 45 : len = c_strlen (arg, 1);
11766 : 45 : if (!len || ! tree_fits_uhwi_p (len))
11767 : : return;
11768 : : }
11769 : : else
11770 : : return;
11771 : :
11772 : : /* Add one for the terminating nul. */
11773 : 34 : len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
11774 : :
11775 : 34 : check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
11776 : : access_write_only);
11777 : : }
11778 : :
11779 : : /* Fold a call to __builtin_object_size with arguments PTR and OST,
11780 : : if possible. */
11781 : :
11782 : : static tree
11783 : 199058 : fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
11784 : : {
11785 : 199058 : tree bytes;
11786 : 199058 : int object_size_type;
11787 : :
11788 : 199058 : if (!validate_arg (ptr, POINTER_TYPE)
11789 : 199058 : || !validate_arg (ost, INTEGER_TYPE))
11790 : : return NULL_TREE;
11791 : :
11792 : 199058 : STRIP_NOPS (ost);
11793 : :
11794 : 199058 : if (TREE_CODE (ost) != INTEGER_CST
11795 : 199058 : || tree_int_cst_sgn (ost) < 0
11796 : 398116 : || compare_tree_int (ost, 3) > 0)
11797 : 0 : return NULL_TREE;
11798 : :
11799 : 199058 : object_size_type = tree_to_shwi (ost);
11800 : :
11801 : : /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11802 : : if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11803 : : and (size_t) 0 for types 2 and 3. */
11804 : 199058 : if (TREE_SIDE_EFFECTS (ptr))
11805 : 603 : return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11806 : :
11807 : 198455 : if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
11808 : 65652 : object_size_type |= OST_DYNAMIC;
11809 : :
11810 : 198455 : if (TREE_CODE (ptr) == ADDR_EXPR)
11811 : : {
11812 : 12076 : compute_builtin_object_size (ptr, object_size_type, &bytes);
11813 : 12076 : if ((object_size_type & OST_DYNAMIC)
11814 : 10891 : || int_fits_type_p (bytes, size_type_node))
11815 : 12076 : return fold_convert (size_type_node, bytes);
11816 : : }
11817 : 186379 : else if (TREE_CODE (ptr) == SSA_NAME)
11818 : : {
11819 : : /* If object size is not known yet, delay folding until
11820 : : later. Maybe subsequent passes will help determining
11821 : : it. */
11822 : 101138 : if (compute_builtin_object_size (ptr, object_size_type, &bytes)
11823 : 101138 : && ((object_size_type & OST_DYNAMIC)
11824 : 2544 : || int_fits_type_p (bytes, size_type_node)))
11825 : 4836 : return fold_convert (size_type_node, bytes);
11826 : : }
11827 : :
11828 : : return NULL_TREE;
11829 : : }
11830 : :
11831 : : /* Builtins with folding operations that operate on "..." arguments
11832 : : need special handling; we need to store the arguments in a convenient
11833 : : data structure before attempting any folding. Fortunately there are
11834 : : only a few builtins that fall into this category. FNDECL is the
11835 : : function, EXP is the CALL_EXPR for the call. */
11836 : :
11837 : : static tree
11838 : 2620003 : fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11839 : : {
11840 : 2620003 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11841 : 2620003 : tree ret = NULL_TREE;
11842 : :
11843 : 2620003 : switch (fcode)
11844 : : {
11845 : 112287 : case BUILT_IN_FPCLASSIFY:
11846 : 112287 : ret = fold_builtin_fpclassify (loc, args, nargs);
11847 : 112287 : break;
11848 : :
11849 : 54 : case BUILT_IN_ADDC:
11850 : 54 : case BUILT_IN_ADDCL:
11851 : 54 : case BUILT_IN_ADDCLL:
11852 : 54 : case BUILT_IN_SUBC:
11853 : 54 : case BUILT_IN_SUBCL:
11854 : 54 : case BUILT_IN_SUBCLL:
11855 : 54 : return fold_builtin_addc_subc (loc, fcode, args);
11856 : :
11857 : : default:
11858 : : break;
11859 : : }
11860 : 112287 : if (ret)
11861 : : {
11862 : 112287 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11863 : 112287 : SET_EXPR_LOCATION (ret, loc);
11864 : 112287 : suppress_warning (ret);
11865 : 112287 : return ret;
11866 : : }
11867 : : return NULL_TREE;
11868 : : }
11869 : :
11870 : : /* Initialize format string characters in the target charset. */
11871 : :
11872 : : bool
11873 : 287694 : init_target_chars (void)
11874 : : {
11875 : 287694 : static bool init;
11876 : 287694 : if (!init)
11877 : : {
11878 : 110494 : target_newline = lang_hooks.to_target_charset ('\n');
11879 : 110494 : target_percent = lang_hooks.to_target_charset ('%');
11880 : 110494 : target_c = lang_hooks.to_target_charset ('c');
11881 : 110494 : target_s = lang_hooks.to_target_charset ('s');
11882 : 110494 : if (target_newline == 0 || target_percent == 0 || target_c == 0
11883 : 110494 : || target_s == 0)
11884 : : return false;
11885 : :
11886 : 110494 : target_percent_c[0] = target_percent;
11887 : 110494 : target_percent_c[1] = target_c;
11888 : 110494 : target_percent_c[2] = '\0';
11889 : :
11890 : 110494 : target_percent_s[0] = target_percent;
11891 : 110494 : target_percent_s[1] = target_s;
11892 : 110494 : target_percent_s[2] = '\0';
11893 : :
11894 : 110494 : target_percent_s_newline[0] = target_percent;
11895 : 110494 : target_percent_s_newline[1] = target_s;
11896 : 110494 : target_percent_s_newline[2] = target_newline;
11897 : 110494 : target_percent_s_newline[3] = '\0';
11898 : :
11899 : 110494 : init = true;
11900 : : }
11901 : : return true;
11902 : : }
11903 : :
11904 : : /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11905 : : and no overflow/underflow occurred. INEXACT is true if M was not
11906 : : exactly calculated. TYPE is the tree type for the result. This
11907 : : function assumes that you cleared the MPFR flags and then
11908 : : calculated M to see if anything subsequently set a flag prior to
11909 : : entering this function. Return NULL_TREE if any checks fail. */
11910 : :
11911 : : static tree
11912 : 3037 : do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11913 : : {
11914 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11915 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11916 : : result of calling FUNC was exact. */
11917 : 4890 : if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11918 : 4890 : && (!flag_rounding_math || !inexact))
11919 : : {
11920 : 1853 : REAL_VALUE_TYPE rr;
11921 : :
11922 : 1853 : real_from_mpfr (&rr, m, type, MPFR_RNDN);
11923 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11924 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11925 : : but the mpfr_t is not, then we underflowed in the
11926 : : conversion. */
11927 : 1853 : if (real_isfinite (&rr)
11928 : 1853 : && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11929 : : {
11930 : 1853 : REAL_VALUE_TYPE rmode;
11931 : :
11932 : 1853 : real_convert (&rmode, TYPE_MODE (type), &rr);
11933 : : /* Proceed iff the specified mode can hold the value. */
11934 : 1853 : if (real_identical (&rmode, &rr))
11935 : 1853 : return build_real (type, rmode);
11936 : : }
11937 : : }
11938 : : return NULL_TREE;
11939 : : }
11940 : :
11941 : : /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11942 : : number and no overflow/underflow occurred. INEXACT is true if M
11943 : : was not exactly calculated. TYPE is the tree type for the result.
11944 : : This function assumes that you cleared the MPFR flags and then
11945 : : calculated M to see if anything subsequently set a flag prior to
11946 : : entering this function. Return NULL_TREE if any checks fail, if
11947 : : FORCE_CONVERT is true, then bypass the checks. */
11948 : :
11949 : : static tree
11950 : 4126 : do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11951 : : {
11952 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11953 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11954 : : result of calling FUNC was exact. */
11955 : 4126 : if (force_convert
11956 : 4126 : || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11957 : 3780 : && !mpfr_overflow_p () && !mpfr_underflow_p ()
11958 : 3780 : && (!flag_rounding_math || !inexact)))
11959 : : {
11960 : 3947 : REAL_VALUE_TYPE re, im;
11961 : :
11962 : 3947 : real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
11963 : 3947 : real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
11964 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11965 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11966 : : but the mpfr_t is not, then we underflowed in the
11967 : : conversion. */
11968 : 3947 : if (force_convert
11969 : 3947 : || (real_isfinite (&re) && real_isfinite (&im)
11970 : 3780 : && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11971 : 3780 : && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11972 : : {
11973 : 3947 : REAL_VALUE_TYPE re_mode, im_mode;
11974 : :
11975 : 3947 : real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11976 : 3947 : real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11977 : : /* Proceed iff the specified mode can hold the value. */
11978 : 3947 : if (force_convert
11979 : 3947 : || (real_identical (&re_mode, &re)
11980 : 3780 : && real_identical (&im_mode, &im)))
11981 : 3947 : return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11982 : 7894 : build_real (TREE_TYPE (type), im_mode));
11983 : : }
11984 : : }
11985 : : return NULL_TREE;
11986 : : }
11987 : :
11988 : : /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11989 : : the pointer *(ARG_QUO) and return the result. The type is taken
11990 : : from the type of ARG0 and is used for setting the precision of the
11991 : : calculation and results. */
11992 : :
11993 : : static tree
11994 : 84618 : do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
11995 : : {
11996 : 84618 : tree const type = TREE_TYPE (arg0);
11997 : 84618 : tree result = NULL_TREE;
11998 : :
11999 : 84618 : STRIP_NOPS (arg0);
12000 : 84618 : STRIP_NOPS (arg1);
12001 : :
12002 : : /* To proceed, MPFR must exactly represent the target floating point
12003 : : format, which only happens when the target base equals two. */
12004 : 84618 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12005 : 84618 : && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12006 : 86981 : && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12007 : : {
12008 : 2363 : const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12009 : 2363 : const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12010 : :
12011 : 2363 : if (real_isfinite (ra0) && real_isfinite (ra1))
12012 : : {
12013 : 2363 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12014 : 2363 : const int prec = fmt->p;
12015 : 2363 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12016 : 2363 : tree result_rem;
12017 : 2363 : long integer_quo;
12018 : 2363 : mpfr_t m0, m1;
12019 : :
12020 : 2363 : mpfr_inits2 (prec, m0, m1, NULL);
12021 : 2363 : mpfr_from_real (m0, ra0, MPFR_RNDN);
12022 : 2363 : mpfr_from_real (m1, ra1, MPFR_RNDN);
12023 : 2363 : mpfr_clear_flags ();
12024 : 2363 : mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12025 : : /* Remquo is independent of the rounding mode, so pass
12026 : : inexact=0 to do_mpfr_ckconv(). */
12027 : 2363 : result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12028 : 2363 : mpfr_clears (m0, m1, NULL);
12029 : 2363 : if (result_rem)
12030 : : {
12031 : : /* MPFR calculates quo in the host's long so it may
12032 : : return more bits in quo than the target int can hold
12033 : : if sizeof(host long) > sizeof(target int). This can
12034 : : happen even for native compilers in LP64 mode. In
12035 : : these cases, modulo the quo value with the largest
12036 : : number that the target int can hold while leaving one
12037 : : bit for the sign. */
12038 : 1179 : if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12039 : 1179 : integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12040 : :
12041 : : /* Dereference the quo pointer argument. */
12042 : 1179 : arg_quo = build_fold_indirect_ref (arg_quo);
12043 : : /* Proceed iff a valid pointer type was passed in. */
12044 : 1179 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12045 : : {
12046 : : /* Set the value. */
12047 : 1179 : tree result_quo
12048 : 1179 : = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12049 : : build_int_cst (TREE_TYPE (arg_quo),
12050 : : integer_quo));
12051 : 1179 : TREE_SIDE_EFFECTS (result_quo) = 1;
12052 : : /* Combine the quo assignment with the rem. */
12053 : 1179 : result = fold_build2 (COMPOUND_EXPR, type,
12054 : : result_quo, result_rem);
12055 : 1179 : suppress_warning (result, OPT_Wunused_value);
12056 : 1179 : result = non_lvalue (result);
12057 : : }
12058 : : }
12059 : : }
12060 : : }
12061 : 84618 : return result;
12062 : : }
12063 : :
12064 : : /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12065 : : resulting value as a tree with type TYPE. The mpfr precision is
12066 : : set to the precision of TYPE. We assume that this mpfr function
12067 : : returns zero if the result could be calculated exactly within the
12068 : : requested precision. In addition, the integer pointer represented
12069 : : by ARG_SG will be dereferenced and set to the appropriate signgam
12070 : : (-1,1) value. */
12071 : :
12072 : : static tree
12073 : 6720 : do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12074 : : {
12075 : 6720 : tree result = NULL_TREE;
12076 : :
12077 : 6720 : STRIP_NOPS (arg);
12078 : :
12079 : : /* To proceed, MPFR must exactly represent the target floating point
12080 : : format, which only happens when the target base equals two. Also
12081 : : verify ARG is a constant and that ARG_SG is an int pointer. */
12082 : 6720 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12083 : 6720 : && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12084 : 6686 : && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12085 : 13406 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12086 : : {
12087 : 6686 : const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12088 : :
12089 : : /* In addition to NaN and Inf, the argument cannot be zero or a
12090 : : negative integer. */
12091 : 6686 : if (real_isfinite (ra)
12092 : 6686 : && ra->cl != rvc_zero
12093 : 13372 : && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12094 : : {
12095 : 674 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12096 : 674 : const int prec = fmt->p;
12097 : 674 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12098 : 674 : int inexact, sg;
12099 : 674 : tree result_lg;
12100 : :
12101 : 674 : auto_mpfr m (prec);
12102 : 674 : mpfr_from_real (m, ra, MPFR_RNDN);
12103 : 674 : mpfr_clear_flags ();
12104 : 674 : inexact = mpfr_lgamma (m, &sg, m, rnd);
12105 : 674 : result_lg = do_mpfr_ckconv (m, type, inexact);
12106 : 674 : if (result_lg)
12107 : : {
12108 : 674 : tree result_sg;
12109 : :
12110 : : /* Dereference the arg_sg pointer argument. */
12111 : 674 : arg_sg = build_fold_indirect_ref (arg_sg);
12112 : : /* Assign the signgam value into *arg_sg. */
12113 : 674 : result_sg = fold_build2 (MODIFY_EXPR,
12114 : : TREE_TYPE (arg_sg), arg_sg,
12115 : : build_int_cst (TREE_TYPE (arg_sg), sg));
12116 : 674 : TREE_SIDE_EFFECTS (result_sg) = 1;
12117 : : /* Combine the signgam assignment with the lgamma result. */
12118 : 674 : result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12119 : : result_sg, result_lg));
12120 : : }
12121 : 674 : }
12122 : : }
12123 : :
12124 : 6720 : return result;
12125 : : }
12126 : :
12127 : : /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12128 : : mpc function FUNC on it and return the resulting value as a tree
12129 : : with type TYPE. The mpfr precision is set to the precision of
12130 : : TYPE. We assume that function FUNC returns zero if the result
12131 : : could be calculated exactly within the requested precision. If
12132 : : DO_NONFINITE is true, then fold expressions containing Inf or NaN
12133 : : in the arguments and/or results. */
12134 : :
12135 : : tree
12136 : 4478 : do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12137 : : int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12138 : : {
12139 : 4478 : tree result = NULL_TREE;
12140 : :
12141 : 4478 : STRIP_NOPS (arg0);
12142 : 4478 : STRIP_NOPS (arg1);
12143 : :
12144 : : /* To proceed, MPFR must exactly represent the target floating point
12145 : : format, which only happens when the target base equals two. */
12146 : 4478 : if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12147 : 4478 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
12148 : 4478 : && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12149 : 4478 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
12150 : 8956 : && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12151 : : {
12152 : 4478 : const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12153 : 4478 : const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12154 : 4478 : const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12155 : 4478 : const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12156 : :
12157 : 4478 : if (do_nonfinite
12158 : 4478 : || (real_isfinite (re0) && real_isfinite (im0)
12159 : 3987 : && real_isfinite (re1) && real_isfinite (im1)))
12160 : : {
12161 : 4126 : const struct real_format *const fmt =
12162 : 4126 : REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12163 : 4126 : const int prec = fmt->p;
12164 : 4126 : const mpfr_rnd_t rnd = fmt->round_towards_zero
12165 : 4126 : ? MPFR_RNDZ : MPFR_RNDN;
12166 : 4126 : const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12167 : 4126 : int inexact;
12168 : 4126 : mpc_t m0, m1;
12169 : :
12170 : 4126 : mpc_init2 (m0, prec);
12171 : 4126 : mpc_init2 (m1, prec);
12172 : 4126 : mpfr_from_real (mpc_realref (m0), re0, rnd);
12173 : 4126 : mpfr_from_real (mpc_imagref (m0), im0, rnd);
12174 : 4126 : mpfr_from_real (mpc_realref (m1), re1, rnd);
12175 : 4126 : mpfr_from_real (mpc_imagref (m1), im1, rnd);
12176 : 4126 : mpfr_clear_flags ();
12177 : 4126 : inexact = func (m0, m0, m1, crnd);
12178 : 4126 : result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12179 : 4126 : mpc_clear (m0);
12180 : 4126 : mpc_clear (m1);
12181 : : }
12182 : : }
12183 : :
12184 : 4478 : return result;
12185 : : }
12186 : :
12187 : : /* A wrapper function for builtin folding that prevents warnings for
12188 : : "statement without effect" and the like, caused by removing the
12189 : : call node earlier than the warning is generated. */
12190 : :
12191 : : tree
12192 : 10237154 : fold_call_stmt (gcall *stmt, bool ignore)
12193 : : {
12194 : 10237154 : tree ret = NULL_TREE;
12195 : 10237154 : tree fndecl = gimple_call_fndecl (stmt);
12196 : 10237154 : location_t loc = gimple_location (stmt);
12197 : 10237154 : if (fndecl && fndecl_built_in_p (fndecl)
12198 : 20474308 : && !gimple_call_va_arg_pack_p (stmt))
12199 : : {
12200 : 10237088 : int nargs = gimple_call_num_args (stmt);
12201 : 10237088 : tree *args = (nargs > 0
12202 : 10237088 : ? gimple_call_arg_ptr (stmt, 0)
12203 : : : &error_mark_node);
12204 : :
12205 : 10237088 : if (avoid_folding_inline_builtin (fndecl))
12206 : : return NULL_TREE;
12207 : 10237088 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12208 : : {
12209 : 0 : return targetm.fold_builtin (fndecl, nargs, args, ignore);
12210 : : }
12211 : : else
12212 : : {
12213 : 10237088 : ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
12214 : 10237088 : if (ret)
12215 : : {
12216 : : /* Propagate location information from original call to
12217 : : expansion of builtin. Otherwise things like
12218 : : maybe_emit_chk_warning, that operate on the expansion
12219 : : of a builtin, will use the wrong location information. */
12220 : 5723 : if (gimple_has_location (stmt))
12221 : : {
12222 : 5722 : tree realret = ret;
12223 : 5722 : if (TREE_CODE (ret) == NOP_EXPR)
12224 : 5722 : realret = TREE_OPERAND (ret, 0);
12225 : 5722 : if (CAN_HAVE_LOCATION_P (realret)
12226 : 9997 : && !EXPR_HAS_LOCATION (realret))
12227 : 5 : SET_EXPR_LOCATION (realret, loc);
12228 : 5722 : return realret;
12229 : : }
12230 : : return ret;
12231 : : }
12232 : : }
12233 : : }
12234 : : return NULL_TREE;
12235 : : }
12236 : :
12237 : : /* Look up the function in builtin_decl that corresponds to DECL
12238 : : and set ASMSPEC as its user assembler name. DECL must be a
12239 : : function decl that declares a builtin. */
12240 : :
12241 : : void
12242 : 147334 : set_builtin_user_assembler_name (tree decl, const char *asmspec)
12243 : : {
12244 : 147334 : gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
12245 : : && asmspec != 0);
12246 : :
12247 : 147334 : tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12248 : 147334 : set_user_assembler_name (builtin, asmspec);
12249 : :
12250 : 147334 : if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
12251 : 147334 : && INT_TYPE_SIZE < BITS_PER_WORD)
12252 : : {
12253 : 1 : scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
12254 : 1 : set_user_assembler_libfunc ("ffs", asmspec);
12255 : 1 : set_optab_libfunc (ffs_optab, mode, "ffs");
12256 : : }
12257 : 147334 : }
12258 : :
12259 : : /* Return true if DECL is a builtin that expands to a constant or similarly
12260 : : simple code. */
12261 : : bool
12262 : 31624473 : is_simple_builtin (tree decl)
12263 : : {
12264 : 31624473 : if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
12265 : 30379358 : switch (DECL_FUNCTION_CODE (decl))
12266 : : {
12267 : : /* Builtins that expand to constants. */
12268 : : case BUILT_IN_CONSTANT_P:
12269 : : case BUILT_IN_EXPECT:
12270 : : case BUILT_IN_OBJECT_SIZE:
12271 : : case BUILT_IN_UNREACHABLE:
12272 : : /* Simple register moves or loads from stack. */
12273 : : case BUILT_IN_ASSUME_ALIGNED:
12274 : : case BUILT_IN_RETURN_ADDRESS:
12275 : : case BUILT_IN_EXTRACT_RETURN_ADDR:
12276 : : case BUILT_IN_FROB_RETURN_ADDR:
12277 : : case BUILT_IN_RETURN:
12278 : : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12279 : : case BUILT_IN_FRAME_ADDRESS:
12280 : : case BUILT_IN_VA_END:
12281 : : case BUILT_IN_STACK_SAVE:
12282 : : case BUILT_IN_STACK_RESTORE:
12283 : : case BUILT_IN_DWARF_CFA:
12284 : : /* Exception state returns or moves registers around. */
12285 : : case BUILT_IN_EH_FILTER:
12286 : : case BUILT_IN_EH_POINTER:
12287 : : case BUILT_IN_EH_COPY_VALUES:
12288 : : return true;
12289 : :
12290 : 27790605 : default:
12291 : 27790605 : return false;
12292 : : }
12293 : :
12294 : : return false;
12295 : : }
12296 : :
12297 : : /* Return true if DECL is a builtin that is not expensive, i.e., they are
12298 : : most probably expanded inline into reasonably simple code. This is a
12299 : : superset of is_simple_builtin. */
12300 : : bool
12301 : 19080970 : is_inexpensive_builtin (tree decl)
12302 : : {
12303 : 19080970 : if (!decl)
12304 : : return false;
12305 : 19065387 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12306 : : return true;
12307 : 17798005 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12308 : 15011376 : switch (DECL_FUNCTION_CODE (decl))
12309 : : {
12310 : : case BUILT_IN_ABS:
12311 : : CASE_BUILT_IN_ALLOCA:
12312 : : case BUILT_IN_BSWAP16:
12313 : : case BUILT_IN_BSWAP32:
12314 : : case BUILT_IN_BSWAP64:
12315 : : case BUILT_IN_BSWAP128:
12316 : : case BUILT_IN_CLZ:
12317 : : case BUILT_IN_CLZIMAX:
12318 : : case BUILT_IN_CLZL:
12319 : : case BUILT_IN_CLZLL:
12320 : : case BUILT_IN_CTZ:
12321 : : case BUILT_IN_CTZIMAX:
12322 : : case BUILT_IN_CTZL:
12323 : : case BUILT_IN_CTZLL:
12324 : : case BUILT_IN_FFS:
12325 : : case BUILT_IN_FFSIMAX:
12326 : : case BUILT_IN_FFSL:
12327 : : case BUILT_IN_FFSLL:
12328 : : case BUILT_IN_IMAXABS:
12329 : : case BUILT_IN_FINITE:
12330 : : case BUILT_IN_FINITEF:
12331 : : case BUILT_IN_FINITEL:
12332 : : case BUILT_IN_FINITED32:
12333 : : case BUILT_IN_FINITED64:
12334 : : case BUILT_IN_FINITED128:
12335 : : case BUILT_IN_FPCLASSIFY:
12336 : : case BUILT_IN_ISFINITE:
12337 : : case BUILT_IN_ISINF_SIGN:
12338 : : case BUILT_IN_ISINF:
12339 : : case BUILT_IN_ISINFF:
12340 : : case BUILT_IN_ISINFL:
12341 : : case BUILT_IN_ISINFD32:
12342 : : case BUILT_IN_ISINFD64:
12343 : : case BUILT_IN_ISINFD128:
12344 : : case BUILT_IN_ISNAN:
12345 : : case BUILT_IN_ISNANF:
12346 : : case BUILT_IN_ISNANL:
12347 : : case BUILT_IN_ISNAND32:
12348 : : case BUILT_IN_ISNAND64:
12349 : : case BUILT_IN_ISNAND128:
12350 : : case BUILT_IN_ISNORMAL:
12351 : : case BUILT_IN_ISGREATER:
12352 : : case BUILT_IN_ISGREATEREQUAL:
12353 : : case BUILT_IN_ISLESS:
12354 : : case BUILT_IN_ISLESSEQUAL:
12355 : : case BUILT_IN_ISLESSGREATER:
12356 : : case BUILT_IN_ISUNORDERED:
12357 : : case BUILT_IN_ISEQSIG:
12358 : : case BUILT_IN_VA_ARG_PACK:
12359 : : case BUILT_IN_VA_ARG_PACK_LEN:
12360 : : case BUILT_IN_VA_COPY:
12361 : : case BUILT_IN_TRAP:
12362 : : case BUILT_IN_UNREACHABLE_TRAP:
12363 : : case BUILT_IN_SAVEREGS:
12364 : : case BUILT_IN_POPCOUNTL:
12365 : : case BUILT_IN_POPCOUNTLL:
12366 : : case BUILT_IN_POPCOUNTIMAX:
12367 : : case BUILT_IN_POPCOUNT:
12368 : : case BUILT_IN_PARITYL:
12369 : : case BUILT_IN_PARITYLL:
12370 : : case BUILT_IN_PARITYIMAX:
12371 : : case BUILT_IN_PARITY:
12372 : : case BUILT_IN_LABS:
12373 : : case BUILT_IN_LLABS:
12374 : : case BUILT_IN_PREFETCH:
12375 : : case BUILT_IN_ACC_ON_DEVICE:
12376 : : return true;
12377 : :
12378 : 14285975 : default:
12379 : 14285975 : return is_simple_builtin (decl);
12380 : : }
12381 : :
12382 : : return false;
12383 : : }
12384 : :
12385 : : /* Return true if T is a constant and the value cast to a target char
12386 : : can be represented by a host char.
12387 : : Store the casted char constant in *P if so. */
12388 : :
12389 : : bool
12390 : 2750 : target_char_cst_p (tree t, char *p)
12391 : : {
12392 : 2750 : if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
12393 : : return false;
12394 : :
12395 : 1607 : *p = (char)tree_to_uhwi (t);
12396 : 1607 : return true;
12397 : : }
12398 : :
12399 : : /* Return true if the builtin DECL is implemented in a standard library.
12400 : : Otherwise return false which doesn't guarantee it is not (thus the list
12401 : : of handled builtins below may be incomplete). */
12402 : :
12403 : : bool
12404 : 40422 : builtin_with_linkage_p (tree decl)
12405 : : {
12406 : 40422 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12407 : 40144 : switch (DECL_FUNCTION_CODE (decl))
12408 : : {
12409 : 1290 : CASE_FLT_FN (BUILT_IN_ACOS):
12410 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
12411 : 1290 : CASE_FLT_FN (BUILT_IN_ACOSH):
12412 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
12413 : 1290 : CASE_FLT_FN (BUILT_IN_ASIN):
12414 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
12415 : 1290 : CASE_FLT_FN (BUILT_IN_ASINH):
12416 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
12417 : 1290 : CASE_FLT_FN (BUILT_IN_ATAN):
12418 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
12419 : 1290 : CASE_FLT_FN (BUILT_IN_ATANH):
12420 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
12421 : 1290 : CASE_FLT_FN (BUILT_IN_ATAN2):
12422 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
12423 : 1290 : CASE_FLT_FN (BUILT_IN_CBRT):
12424 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
12425 : 1290 : CASE_FLT_FN (BUILT_IN_CEIL):
12426 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
12427 : 1290 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
12428 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
12429 : 1290 : CASE_FLT_FN (BUILT_IN_COS):
12430 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
12431 : 1290 : CASE_FLT_FN (BUILT_IN_COSH):
12432 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
12433 : 1290 : CASE_FLT_FN (BUILT_IN_ERF):
12434 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
12435 : 1290 : CASE_FLT_FN (BUILT_IN_ERFC):
12436 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
12437 : 1290 : CASE_FLT_FN (BUILT_IN_EXP):
12438 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
12439 : 1290 : CASE_FLT_FN (BUILT_IN_EXP2):
12440 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
12441 : 1290 : CASE_FLT_FN (BUILT_IN_EXPM1):
12442 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
12443 : 1290 : CASE_FLT_FN (BUILT_IN_FABS):
12444 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
12445 : 1290 : CASE_FLT_FN (BUILT_IN_FDIM):
12446 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
12447 : 1290 : CASE_FLT_FN (BUILT_IN_FLOOR):
12448 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
12449 : 1290 : CASE_FLT_FN (BUILT_IN_FMA):
12450 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
12451 : 1290 : CASE_FLT_FN (BUILT_IN_FMAX):
12452 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
12453 : 1290 : CASE_FLT_FN (BUILT_IN_FMIN):
12454 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
12455 : 1290 : CASE_FLT_FN (BUILT_IN_FMOD):
12456 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
12457 : 1290 : CASE_FLT_FN (BUILT_IN_FREXP):
12458 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
12459 : 1290 : CASE_FLT_FN (BUILT_IN_HYPOT):
12460 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
12461 : 1290 : CASE_FLT_FN (BUILT_IN_ILOGB):
12462 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
12463 : 1290 : CASE_FLT_FN (BUILT_IN_LDEXP):
12464 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
12465 : 1290 : CASE_FLT_FN (BUILT_IN_LGAMMA):
12466 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
12467 : 1290 : CASE_FLT_FN (BUILT_IN_LLRINT):
12468 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
12469 : 1290 : CASE_FLT_FN (BUILT_IN_LLROUND):
12470 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
12471 : 1290 : CASE_FLT_FN (BUILT_IN_LOG):
12472 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
12473 : 1290 : CASE_FLT_FN (BUILT_IN_LOG10):
12474 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
12475 : 1290 : CASE_FLT_FN (BUILT_IN_LOG1P):
12476 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
12477 : 1290 : CASE_FLT_FN (BUILT_IN_LOG2):
12478 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
12479 : 1290 : CASE_FLT_FN (BUILT_IN_LOGB):
12480 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
12481 : 1290 : CASE_FLT_FN (BUILT_IN_LRINT):
12482 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
12483 : 1290 : CASE_FLT_FN (BUILT_IN_LROUND):
12484 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
12485 : 1290 : CASE_FLT_FN (BUILT_IN_MODF):
12486 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
12487 : 1290 : CASE_FLT_FN (BUILT_IN_NAN):
12488 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
12489 : 1290 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
12490 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
12491 : 1290 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
12492 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
12493 : 1290 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
12494 : 1290 : CASE_FLT_FN (BUILT_IN_POW):
12495 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
12496 : 1290 : CASE_FLT_FN (BUILT_IN_REMAINDER):
12497 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
12498 : 1290 : CASE_FLT_FN (BUILT_IN_REMQUO):
12499 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
12500 : 1290 : CASE_FLT_FN (BUILT_IN_RINT):
12501 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
12502 : 1290 : CASE_FLT_FN (BUILT_IN_ROUND):
12503 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
12504 : 1290 : CASE_FLT_FN (BUILT_IN_SCALBLN):
12505 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
12506 : 1290 : CASE_FLT_FN (BUILT_IN_SCALBN):
12507 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
12508 : 1290 : CASE_FLT_FN (BUILT_IN_SIN):
12509 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
12510 : 1290 : CASE_FLT_FN (BUILT_IN_SINH):
12511 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
12512 : 1290 : CASE_FLT_FN (BUILT_IN_SINCOS):
12513 : 1290 : CASE_FLT_FN (BUILT_IN_SQRT):
12514 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
12515 : 1290 : CASE_FLT_FN (BUILT_IN_TAN):
12516 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
12517 : 1290 : CASE_FLT_FN (BUILT_IN_TANH):
12518 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
12519 : 1290 : CASE_FLT_FN (BUILT_IN_TGAMMA):
12520 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
12521 : 1290 : CASE_FLT_FN (BUILT_IN_TRUNC):
12522 : 1290 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
12523 : 1290 : return true;
12524 : :
12525 : 16 : case BUILT_IN_STPCPY:
12526 : 16 : case BUILT_IN_STPNCPY:
12527 : : /* stpcpy is both referenced in libiberty's pex-win32.c and provided
12528 : : by libiberty's stpcpy.c for MinGW targets so we need to return true
12529 : : in order to be able to build libiberty in LTO mode for them. */
12530 : 16 : return true;
12531 : :
12532 : : default:
12533 : : break;
12534 : : }
12535 : : return false;
12536 : : }
12537 : :
12538 : : /* Return true if OFFRNG is bounded to a subrange of offset values
12539 : : valid for the largest possible object. */
12540 : :
12541 : : bool
12542 : 501 : access_ref::offset_bounded () const
12543 : : {
12544 : 501 : tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
12545 : 501 : tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
12546 : 975 : return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
12547 : : }
12548 : :
12549 : : /* If CALLEE has known side effects, fill in INFO and return true.
12550 : : See tree-ssa-structalias.cc:find_func_aliases
12551 : : for the list of builtins we might need to handle here. */
12552 : :
12553 : : attr_fnspec
12554 : 107969139 : builtin_fnspec (tree callee)
12555 : : {
12556 : 107969139 : built_in_function code = DECL_FUNCTION_CODE (callee);
12557 : :
12558 : 107969139 : switch (code)
12559 : : {
12560 : : /* All the following functions read memory pointed to by
12561 : : their second argument and write memory pointed to by first
12562 : : argument.
12563 : : strcat/strncat additionally reads memory pointed to by the first
12564 : : argument. */
12565 : 133349 : case BUILT_IN_STRCAT:
12566 : 133349 : case BUILT_IN_STRCAT_CHK:
12567 : 133349 : return "1cW 1 ";
12568 : 42625 : case BUILT_IN_STRNCAT:
12569 : 42625 : case BUILT_IN_STRNCAT_CHK:
12570 : 42625 : return "1cW 13";
12571 : 260367 : case BUILT_IN_STRCPY:
12572 : 260367 : case BUILT_IN_STRCPY_CHK:
12573 : 260367 : return "1cO 1 ";
12574 : 32157 : case BUILT_IN_STPCPY:
12575 : 32157 : case BUILT_IN_STPCPY_CHK:
12576 : 32157 : return ".cO 1 ";
12577 : 18384912 : case BUILT_IN_STRNCPY:
12578 : 18384912 : case BUILT_IN_MEMCPY:
12579 : 18384912 : case BUILT_IN_MEMMOVE:
12580 : 18384912 : case BUILT_IN_TM_MEMCPY:
12581 : 18384912 : case BUILT_IN_TM_MEMMOVE:
12582 : 18384912 : case BUILT_IN_STRNCPY_CHK:
12583 : 18384912 : case BUILT_IN_MEMCPY_CHK:
12584 : 18384912 : case BUILT_IN_MEMMOVE_CHK:
12585 : 18384912 : return "1cO313";
12586 : 151413 : case BUILT_IN_MEMPCPY:
12587 : 151413 : case BUILT_IN_MEMPCPY_CHK:
12588 : 151413 : return ".cO313";
12589 : 57095 : case BUILT_IN_STPNCPY:
12590 : 57095 : case BUILT_IN_STPNCPY_CHK:
12591 : 57095 : return ".cO313";
12592 : 0 : case BUILT_IN_BCOPY:
12593 : 0 : return ".c23O3";
12594 : 0 : case BUILT_IN_BZERO:
12595 : 0 : return ".cO2";
12596 : 9532062 : case BUILT_IN_MEMCMP:
12597 : 9532062 : case BUILT_IN_MEMCMP_EQ:
12598 : 9532062 : case BUILT_IN_BCMP:
12599 : 9532062 : case BUILT_IN_STRNCMP:
12600 : 9532062 : case BUILT_IN_STRNCMP_EQ:
12601 : 9532062 : case BUILT_IN_STRNCASECMP:
12602 : 9532062 : return ".cR3R3";
12603 : :
12604 : : /* The following functions read memory pointed to by their
12605 : : first argument. */
12606 : 774 : CASE_BUILT_IN_TM_LOAD (1):
12607 : 774 : CASE_BUILT_IN_TM_LOAD (2):
12608 : 774 : CASE_BUILT_IN_TM_LOAD (4):
12609 : 774 : CASE_BUILT_IN_TM_LOAD (8):
12610 : 774 : CASE_BUILT_IN_TM_LOAD (FLOAT):
12611 : 774 : CASE_BUILT_IN_TM_LOAD (DOUBLE):
12612 : 774 : CASE_BUILT_IN_TM_LOAD (LDOUBLE):
12613 : 774 : CASE_BUILT_IN_TM_LOAD (M64):
12614 : 774 : CASE_BUILT_IN_TM_LOAD (M128):
12615 : 774 : CASE_BUILT_IN_TM_LOAD (M256):
12616 : 774 : case BUILT_IN_TM_LOG:
12617 : 774 : case BUILT_IN_TM_LOG_1:
12618 : 774 : case BUILT_IN_TM_LOG_2:
12619 : 774 : case BUILT_IN_TM_LOG_4:
12620 : 774 : case BUILT_IN_TM_LOG_8:
12621 : 774 : case BUILT_IN_TM_LOG_FLOAT:
12622 : 774 : case BUILT_IN_TM_LOG_DOUBLE:
12623 : 774 : case BUILT_IN_TM_LOG_LDOUBLE:
12624 : 774 : case BUILT_IN_TM_LOG_M64:
12625 : 774 : case BUILT_IN_TM_LOG_M128:
12626 : 774 : case BUILT_IN_TM_LOG_M256:
12627 : 774 : return ".cR ";
12628 : :
12629 : 458885 : case BUILT_IN_INDEX:
12630 : 458885 : case BUILT_IN_RINDEX:
12631 : 458885 : case BUILT_IN_STRCHR:
12632 : 458885 : case BUILT_IN_STRLEN:
12633 : 458885 : case BUILT_IN_STRRCHR:
12634 : 458885 : return ".cR ";
12635 : 62543 : case BUILT_IN_STRNLEN:
12636 : 62543 : return ".cR2";
12637 : :
12638 : : /* These read memory pointed to by the first argument.
12639 : : Allocating memory does not have any side-effects apart from
12640 : : being the definition point for the pointer.
12641 : : Unix98 specifies that errno is set on allocation failure. */
12642 : 17385 : case BUILT_IN_STRDUP:
12643 : 17385 : return "mCR ";
12644 : 13312 : case BUILT_IN_STRNDUP:
12645 : 13312 : return "mCR2";
12646 : : /* Allocating memory does not have any side-effects apart from
12647 : : being the definition point for the pointer. */
12648 : 9269448 : case BUILT_IN_MALLOC:
12649 : 9269448 : case BUILT_IN_ALIGNED_ALLOC:
12650 : 9269448 : case BUILT_IN_CALLOC:
12651 : 9269448 : case BUILT_IN_GOMP_ALLOC:
12652 : 9269448 : return "mC";
12653 : 844720 : CASE_BUILT_IN_ALLOCA:
12654 : 844720 : return "mc";
12655 : : /* These read memory pointed to by the first argument with size
12656 : : in the third argument. */
12657 : 453137 : case BUILT_IN_MEMCHR:
12658 : 453137 : return ".cR3";
12659 : : /* These read memory pointed to by the first and second arguments. */
12660 : 11119089 : case BUILT_IN_STRSTR:
12661 : 11119089 : case BUILT_IN_STRPBRK:
12662 : 11119089 : case BUILT_IN_STRCASECMP:
12663 : 11119089 : case BUILT_IN_STRCSPN:
12664 : 11119089 : case BUILT_IN_STRSPN:
12665 : 11119089 : case BUILT_IN_STRCMP:
12666 : 11119089 : case BUILT_IN_STRCMP_EQ:
12667 : 11119089 : return ".cR R ";
12668 : : /* Freeing memory kills the pointed-to memory. More importantly
12669 : : the call has to serve as a barrier for moving loads and stores
12670 : : across it. */
12671 : 5530769 : case BUILT_IN_STACK_RESTORE:
12672 : 5530769 : case BUILT_IN_FREE:
12673 : 5530769 : case BUILT_IN_GOMP_FREE:
12674 : 5530769 : return ".co ";
12675 : 92327 : case BUILT_IN_VA_END:
12676 : 92327 : return ".cO ";
12677 : : /* Realloc serves both as allocation point and deallocation point. */
12678 : 969167 : case BUILT_IN_REALLOC:
12679 : 969167 : case BUILT_IN_GOMP_REALLOC:
12680 : 969167 : return ".Cw ";
12681 : 15516 : case BUILT_IN_GAMMA_R:
12682 : 15516 : case BUILT_IN_GAMMAF_R:
12683 : 15516 : case BUILT_IN_GAMMAL_R:
12684 : 15516 : case BUILT_IN_LGAMMA_R:
12685 : 15516 : case BUILT_IN_LGAMMAF_R:
12686 : 15516 : case BUILT_IN_LGAMMAL_R:
12687 : 15516 : return ".C. Ot";
12688 : 71947 : case BUILT_IN_FREXP:
12689 : 71947 : case BUILT_IN_FREXPF:
12690 : 71947 : case BUILT_IN_FREXPL:
12691 : 71947 : case BUILT_IN_MODF:
12692 : 71947 : case BUILT_IN_MODFF:
12693 : 71947 : case BUILT_IN_MODFL:
12694 : 71947 : return ".c. Ot";
12695 : 7236 : case BUILT_IN_REMQUO:
12696 : 7236 : case BUILT_IN_REMQUOF:
12697 : 7236 : case BUILT_IN_REMQUOL:
12698 : 7236 : return ".c. . Ot";
12699 : 151 : case BUILT_IN_SINCOS:
12700 : 151 : case BUILT_IN_SINCOSF:
12701 : 151 : case BUILT_IN_SINCOSL:
12702 : 151 : return ".c. OtOt";
12703 : 4250088 : case BUILT_IN_MEMSET:
12704 : 4250088 : case BUILT_IN_MEMSET_CHK:
12705 : 4250088 : case BUILT_IN_TM_MEMSET:
12706 : 4250088 : return "1cO3";
12707 : 365 : CASE_BUILT_IN_TM_STORE (1):
12708 : 365 : CASE_BUILT_IN_TM_STORE (2):
12709 : 365 : CASE_BUILT_IN_TM_STORE (4):
12710 : 365 : CASE_BUILT_IN_TM_STORE (8):
12711 : 365 : CASE_BUILT_IN_TM_STORE (FLOAT):
12712 : 365 : CASE_BUILT_IN_TM_STORE (DOUBLE):
12713 : 365 : CASE_BUILT_IN_TM_STORE (LDOUBLE):
12714 : 365 : CASE_BUILT_IN_TM_STORE (M64):
12715 : 365 : CASE_BUILT_IN_TM_STORE (M128):
12716 : 365 : CASE_BUILT_IN_TM_STORE (M256):
12717 : 365 : return ".cO ";
12718 : 2217125 : case BUILT_IN_STACK_SAVE:
12719 : 2217125 : case BUILT_IN_RETURN:
12720 : 2217125 : case BUILT_IN_EH_POINTER:
12721 : 2217125 : case BUILT_IN_EH_FILTER:
12722 : 2217125 : case BUILT_IN_UNWIND_RESUME:
12723 : 2217125 : case BUILT_IN_CXA_END_CLEANUP:
12724 : 2217125 : case BUILT_IN_EH_COPY_VALUES:
12725 : 2217125 : case BUILT_IN_FRAME_ADDRESS:
12726 : 2217125 : case BUILT_IN_APPLY_ARGS:
12727 : 2217125 : case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
12728 : 2217125 : case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
12729 : 2217125 : case BUILT_IN_PREFETCH:
12730 : 2217125 : case BUILT_IN_DWARF_CFA:
12731 : 2217125 : case BUILT_IN_RETURN_ADDRESS:
12732 : 2217125 : return ".c";
12733 : 1671290 : case BUILT_IN_ASSUME_ALIGNED:
12734 : 1671290 : case BUILT_IN_EXPECT:
12735 : 1671290 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
12736 : 1671290 : return "1cX ";
12737 : : /* But posix_memalign stores a pointer into the memory pointed to
12738 : : by its first argument. */
12739 : 5573 : case BUILT_IN_POSIX_MEMALIGN:
12740 : 5573 : return ".cOt";
12741 : 4350 : case BUILT_IN_OMP_GET_MAPPED_PTR:
12742 : 4350 : return ". R ";
12743 : :
12744 : 42299962 : default:
12745 : 42299962 : return "";
12746 : : }
12747 : : }
|