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 : 521137 : is_builtin_name (const char *name)
204 : : {
205 : 521137 : return (startswith (name, "__builtin_")
206 : 189725 : || startswith (name, "__sync_")
207 : 710012 : || 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 : 521137 : 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 : 521137 : const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
221 : 521137 : 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 : 95012355 : get_object_alignment_2 (tree exp, unsigned int *alignp,
241 : : unsigned HOST_WIDE_INT *bitposp, bool addr_p)
242 : : {
243 : 95012355 : poly_int64 bitsize, bitpos;
244 : 95012355 : tree offset;
245 : 95012355 : machine_mode mode;
246 : 95012355 : int unsignedp, reversep, volatilep;
247 : 95012355 : unsigned int align = BITS_PER_UNIT;
248 : 95012355 : bool known_alignment = false;
249 : :
250 : : /* Get the innermost object and the constant (bitpos) and possibly
251 : : variable (offset) offset of the access. */
252 : 95012355 : 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 : 95012355 : 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 : 94707708 : else if (TREE_CODE (exp) == LABEL_DECL)
267 : : ;
268 : 94670736 : else if (TREE_CODE (exp) == CONST_DECL)
269 : : {
270 : : /* The alignment of a CONST_DECL is determined by its initializer. */
271 : 57464 : exp = DECL_INITIAL (exp);
272 : 57464 : align = TYPE_ALIGN (TREE_TYPE (exp));
273 : 57464 : if (CONSTANT_CLASS_P (exp))
274 : 57457 : align = targetm.constant_alignment (exp, align);
275 : :
276 : : known_alignment = true;
277 : : }
278 : 94613272 : else if (DECL_P (exp))
279 : : {
280 : 56456531 : align = DECL_ALIGN (exp);
281 : : known_alignment = true;
282 : : }
283 : 38156741 : else if (TREE_CODE (exp) == INDIRECT_REF
284 : 38145455 : || TREE_CODE (exp) == MEM_REF
285 : 4849270 : || TREE_CODE (exp) == TARGET_MEM_REF)
286 : : {
287 : 36576934 : tree addr = TREE_OPERAND (exp, 0);
288 : 36576934 : unsigned ptr_align;
289 : 36576934 : unsigned HOST_WIDE_INT ptr_bitpos;
290 : 36576934 : unsigned HOST_WIDE_INT ptr_bitmask = ~0;
291 : :
292 : : /* If the address is explicitely aligned, handle that. */
293 : 36576934 : if (TREE_CODE (addr) == BIT_AND_EXPR
294 : 36576934 : && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
295 : : {
296 : 78 : ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
297 : 78 : ptr_bitmask *= BITS_PER_UNIT;
298 : 78 : align = least_bit_hwi (ptr_bitmask);
299 : 78 : addr = TREE_OPERAND (addr, 0);
300 : : }
301 : :
302 : 36576934 : known_alignment
303 : 36576934 : = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
304 : 36576934 : align = MAX (ptr_align, align);
305 : :
306 : : /* Re-apply explicit alignment to the bitpos. */
307 : 36576934 : 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 : 36576934 : if (TREE_CODE (exp) == TARGET_MEM_REF)
312 : : {
313 : 3269463 : if (TMR_INDEX (exp))
314 : : {
315 : 1570660 : unsigned HOST_WIDE_INT step = 1;
316 : 1570660 : if (TMR_STEP (exp))
317 : 1434815 : step = TREE_INT_CST_LOW (TMR_STEP (exp));
318 : 1570660 : align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
319 : : }
320 : 3269463 : if (TMR_INDEX2 (exp))
321 : 32230 : 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 : 36576934 : unsigned int talign;
331 : 36576934 : if (!addr_p && !known_alignment
332 : 34387921 : && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
333 : 70964849 : && talign > align)
334 : : align = talign;
335 : : else
336 : : {
337 : : /* Else adjust bitpos accordingly. */
338 : 10096576 : bitpos += ptr_bitpos;
339 : 10096576 : if (TREE_CODE (exp) == MEM_REF
340 : 10096576 : || TREE_CODE (exp) == TARGET_MEM_REF)
341 : 10088047 : bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
342 : : }
343 : 36576934 : }
344 : 1579807 : 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 : 1579367 : align = TYPE_ALIGN (TREE_TYPE (exp));
349 : 1579367 : if (CONSTANT_CLASS_P (exp))
350 : 1579367 : 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 : 95012355 : if (offset)
358 : : {
359 : 6956655 : unsigned int trailing_zeros = tree_ctz (offset);
360 : 6956655 : if (trailing_zeros < HOST_BITS_PER_INT)
361 : : {
362 : 6956599 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
363 : 6956599 : if (inner)
364 : 6956599 : 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 : 95012355 : unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
371 : 95012355 : if (alt_align != 0 && alt_align < align)
372 : : {
373 : : align = alt_align;
374 : : known_alignment = false;
375 : : }
376 : :
377 : 95012355 : *alignp = align;
378 : 95012355 : *bitposp = bitpos.coeffs[0] & (align - 1);
379 : 95012355 : 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 : 73777115 : 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 : 73777115 : if (TREE_CODE (exp) == WITH_SIZE_EXPR)
394 : 0 : exp = TREE_OPERAND (exp, 0);
395 : 73777115 : 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 : 27744449 : get_object_alignment (tree exp)
402 : : {
403 : 27744449 : unsigned HOST_WIDE_INT bitpos = 0;
404 : 27744449 : unsigned int align;
405 : :
406 : 27744449 : 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 : 27744449 : if (bitpos != 0)
412 : 1551446 : align = least_bit_hwi (bitpos);
413 : 27744449 : 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 : 74341727 : get_pointer_alignment_1 (tree exp, unsigned int *alignp,
425 : : unsigned HOST_WIDE_INT *bitposp)
426 : : {
427 : 74341727 : STRIP_NOPS (exp);
428 : :
429 : 74341727 : if (TREE_CODE (exp) == ADDR_EXPR)
430 : 21233712 : return get_object_alignment_2 (TREE_OPERAND (exp, 0),
431 : 21233712 : alignp, bitposp, true);
432 : 53108015 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
433 : : {
434 : 865749 : unsigned int align;
435 : 865749 : unsigned HOST_WIDE_INT bitpos;
436 : 865749 : bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
437 : : &align, &bitpos);
438 : 865749 : if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
439 : 1735 : bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
440 : : else
441 : : {
442 : 864014 : unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
443 : 864014 : if (trailing_zeros < HOST_BITS_PER_INT)
444 : : {
445 : 864008 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
446 : 864008 : if (inner)
447 : 864008 : align = MIN (align, inner);
448 : : }
449 : : }
450 : 865749 : *alignp = align;
451 : 865749 : *bitposp = bitpos & (align - 1);
452 : 865749 : return res;
453 : : }
454 : 52242266 : else if (TREE_CODE (exp) == SSA_NAME
455 : 52242266 : && POINTER_TYPE_P (TREE_TYPE (exp)))
456 : : {
457 : 50579829 : unsigned int ptr_align, ptr_misalign;
458 : 50579829 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
459 : :
460 : 50579829 : if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
461 : : {
462 : 5636816 : *bitposp = ptr_misalign * BITS_PER_UNIT;
463 : 5636816 : *alignp = ptr_align * BITS_PER_UNIT;
464 : : /* Make sure to return a sensible alignment when the multiplication
465 : : by BITS_PER_UNIT overflowed. */
466 : 5636816 : if (*alignp == 0)
467 : 30 : *alignp = 1u << (HOST_BITS_PER_INT - 1);
468 : : /* We cannot really tell whether this result is an approximation. */
469 : 5636816 : return false;
470 : : }
471 : : else
472 : : {
473 : 44943013 : *bitposp = 0;
474 : 44943013 : *alignp = BITS_PER_UNIT;
475 : 44943013 : return false;
476 : : }
477 : : }
478 : 1662437 : else if (TREE_CODE (exp) == INTEGER_CST)
479 : : {
480 : 14324 : *alignp = BIGGEST_ALIGNMENT;
481 : 14324 : *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
482 : 14324 : & (BIGGEST_ALIGNMENT - 1));
483 : 14324 : return true;
484 : : }
485 : :
486 : 1648113 : *bitposp = 0;
487 : 1648113 : *alignp = BITS_PER_UNIT;
488 : 1648113 : 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 : 10495836 : get_pointer_alignment (tree exp)
500 : : {
501 : 10495836 : unsigned HOST_WIDE_INT bitpos = 0;
502 : 10495836 : unsigned int align;
503 : :
504 : 10495836 : 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 : 10495836 : if (bitpos != 0)
510 : 72412 : align = least_bit_hwi (bitpos);
511 : :
512 : 10495836 : 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 : 768870 : string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
521 : : {
522 : 768870 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
523 : :
524 : 768870 : unsigned n;
525 : :
526 : 768870 : if (eltsize == 1)
527 : : {
528 : : /* Optimize the common case of plain char. */
529 : 245516105 : for (n = 0; n < maxelts; n++)
530 : : {
531 : 245424389 : const char *elt = (const char*) ptr + n;
532 : 245424389 : 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 : 768870 : 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 : 2690693 : 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 : 2690693 : c_strlen_data local_strlen_data = { };
584 : 2690693 : if (!data)
585 : 579827 : data = &local_strlen_data;
586 : :
587 : 2690693 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
588 : :
589 : 2690693 : tree src = STRIP_NOPS (arg);
590 : 2690693 : if (TREE_CODE (src) == COND_EXPR
591 : 2690693 : && (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 : 2690527 : if (TREE_CODE (src) == COMPOUND_EXPR
602 : 2690527 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
603 : 45 : return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
604 : :
605 : 2690482 : location_t loc = EXPR_LOC_OR_LOC (src, input_location);
606 : :
607 : : /* Offset from the beginning of the string in bytes. */
608 : 2690482 : tree byteoff;
609 : 2690482 : tree memsize;
610 : 2690482 : tree decl;
611 : 2690482 : src = string_constant (src, &byteoff, &memsize, &decl);
612 : 2690482 : if (src == 0)
613 : : return NULL_TREE;
614 : :
615 : : /* Determine the size of the string element. */
616 : 783784 : 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 : 772590 : HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
626 : 772590 : strelts = strelts / eltsize;
627 : :
628 : 772590 : if (!tree_fits_uhwi_p (memsize))
629 : : return NULL_TREE;
630 : :
631 : 772590 : 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 : 772590 : const char *ptr = TREE_STRING_POINTER (src);
636 : :
637 : 772590 : if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
638 : : {
639 : : /* The code below works only for single byte character types. */
640 : 5811 : 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 : 5811 : 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 : 5811 : if (len + 1 < strelts)
653 : : return NULL_TREE;
654 : 5241 : 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 : 4092 : 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 : 4053 : tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
672 : 4053 : : byteoff;
673 : 4053 : offsave = fold_convert_loc (loc, sizetype, offsave);
674 : 4053 : tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
675 : 4053 : size_int (len));
676 : 4053 : tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
677 : : offsave);
678 : 4053 : lenexp = fold_convert_loc (loc, ssizetype, lenexp);
679 : 4053 : return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
680 : 4053 : build_zero_cst (ssizetype));
681 : : }
682 : :
683 : : /* Offset from the beginning of the string in elements. */
684 : 766779 : 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 : 766779 : if (byteoff == 0)
689 : : eltoff = 0;
690 : 766779 : else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
691 : : eltoff = -1;
692 : : else
693 : 765591 : 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 : 766779 : 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 : 787 : if (decl)
707 : 786 : inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
708 : 787 : 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 : 763059 : 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 : 763059 : unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
725 : 763059 : 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 : 763059 : if (len >= maxelts - eltoff)
731 : : {
732 : 85454 : data->decl = decl;
733 : 85454 : data->off = byteoff;
734 : 85454 : data->minlen = ssize_int (len);
735 : 85454 : return NULL_TREE;
736 : : }
737 : :
738 : 677605 : 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 : 257831 : c_readstr (const char *str, fixed_size_mode mode,
749 : : bool null_terminated_p/*=true*/)
750 : : {
751 : 257831 : auto_vec<target_unit, MAX_BITSIZE_MODE_ANY_INT / BITS_PER_UNIT> bytes;
752 : :
753 : 515662 : bytes.reserve (GET_MODE_SIZE (mode));
754 : :
755 : 257831 : target_unit ch = 1;
756 : 4435808 : for (unsigned int i = 0; i < GET_MODE_SIZE (mode); ++i)
757 : : {
758 : 1960073 : if (ch || !null_terminated_p)
759 : 1913100 : ch = (unsigned char) str[i];
760 : 1960073 : bytes.quick_push (ch);
761 : : }
762 : :
763 : 257831 : return native_decode_rtx (mode, bytes, 0);
764 : 257831 : }
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 : 32121 : target_char_cast (tree cst, char *p)
772 : : {
773 : 32121 : unsigned HOST_WIDE_INT val, hostval;
774 : :
775 : 32121 : 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 : 32121 : val = TREE_INT_CST_LOW (cst);
781 : :
782 : 32121 : if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
783 : 32121 : val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
784 : :
785 : 32121 : hostval = val;
786 : 32121 : if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
787 : 32121 : hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
788 : :
789 : 32121 : if (val != hostval)
790 : : return 1;
791 : :
792 : 32121 : *p = hostval;
793 : 32121 : 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 : 1070774 : builtin_save_expr (tree exp)
802 : : {
803 : 1070774 : if (TREE_CODE (exp) == SSA_NAME
804 : 909829 : || (TREE_ADDRESSABLE (exp) == 0
805 : 909637 : && (TREE_CODE (exp) == PARM_DECL
806 : 357723 : || (VAR_P (exp) && !TREE_STATIC (exp)))))
807 : : return exp;
808 : :
809 : 355936 : 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 : 15556 : expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
818 : : {
819 : 15556 : int i;
820 : 15556 : rtx tem = INITIAL_FRAME_ADDRESS_RTX;
821 : 15556 : 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 : 15556 : if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
833 : 4221 : tem = frame_pointer_rtx;
834 : : else
835 : : {
836 : 11335 : tem = hard_frame_pointer_rtx;
837 : :
838 : : /* Tell reload not to eliminate the frame pointer. */
839 : 11335 : crtl->accesses_prior_frames = 1;
840 : : }
841 : : }
842 : :
843 : 15556 : 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 : 51432 : 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 : 15556 : 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 : 5798 : 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 : 5061 : 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 : 841 : 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 : 841 : 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 : 841 : 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 : 841 : 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 : 391 : 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 : 391 : fp = gen_rtx_MEM (Pmode, buf_addr);
1020 : 391 : 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 : 1993647 : more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1081 : : {
1082 : 1993647 : 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 : 448200 : validate_arglist (const_tree callexpr, ...)
1092 : : {
1093 : 448200 : enum tree_code code;
1094 : 448200 : bool res = 0;
1095 : 448200 : va_list ap;
1096 : 448200 : const_call_expr_arg_iterator iter;
1097 : 448200 : const_tree arg;
1098 : :
1099 : 448200 : va_start (ap, callexpr);
1100 : 448200 : init_const_call_expr_arg_iterator (callexpr, &iter);
1101 : :
1102 : : /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1103 : 448200 : tree fn = CALL_EXPR_FN (callexpr);
1104 : 448200 : bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1105 : :
1106 : 448200 : for (unsigned argno = 1; ; ++argno)
1107 : : {
1108 : 1541537 : code = (enum tree_code) va_arg (ap, int);
1109 : :
1110 : 1541537 : switch (code)
1111 : : {
1112 : 1971 : case 0:
1113 : : /* This signifies an ellipses, any further arguments are all ok. */
1114 : 1971 : res = true;
1115 : 1971 : goto end;
1116 : 445996 : case VOID_TYPE:
1117 : : /* This signifies an endlink, if no arguments remain, return
1118 : : true, otherwise return false. */
1119 : 445996 : res = !more_const_call_expr_args_p (&iter);
1120 : 445996 : goto end;
1121 : 749190 : 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 : 749190 : if (argmap
1126 : 749190 : && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1127 : : {
1128 : 284647 : arg = next_const_call_expr_arg (&iter);
1129 : 284647 : if (!validate_arg (arg, code) || integer_zerop (arg))
1130 : 59 : goto end;
1131 : : break;
1132 : : }
1133 : : /* FALLTHRU */
1134 : 808923 : 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 : 808923 : arg = next_const_call_expr_arg (&iter);
1139 : 808923 : 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 : 448200 : end: ;
1148 : 448200 : va_end (ap);
1149 : :
1150 : 448200 : BITMAP_FREE (argmap);
1151 : :
1152 : 448200 : if (res)
1153 : 447967 : for (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (TREE_TYPE (fn)));
1154 : 670428 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
1155 : 222461 : attrs = TREE_CHAIN (attrs))
1156 : : {
1157 : 222483 : tree args = TREE_VALUE (attrs);
1158 : 222483 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
1159 : 222483 : unsigned int idx2
1160 : 222483 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
1161 : 222483 : if (idx < (unsigned) call_expr_nargs (callexpr)
1162 : 222483 : && idx2 < (unsigned) call_expr_nargs (callexpr)
1163 : 222483 : && POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx)))
1164 : 222483 : && integer_zerop (CALL_EXPR_ARG (callexpr, idx))
1165 : 82 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx2)))
1166 : 222565 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx2)))
1167 : : return false;
1168 : : }
1169 : :
1170 : : return res;
1171 : : }
1172 : :
1173 : : /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1174 : : and the address of the save area. */
1175 : :
1176 : : static rtx
1177 : 510 : expand_builtin_nonlocal_goto (tree exp)
1178 : : {
1179 : 510 : tree t_label, t_save_area;
1180 : 510 : rtx r_label, r_save_area, r_fp, r_sp;
1181 : 510 : rtx_insn *insn;
1182 : :
1183 : 510 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1184 : : return NULL_RTX;
1185 : :
1186 : 510 : t_label = CALL_EXPR_ARG (exp, 0);
1187 : 510 : t_save_area = CALL_EXPR_ARG (exp, 1);
1188 : :
1189 : 510 : r_label = expand_normal (t_label);
1190 : 510 : r_label = convert_memory_address (Pmode, r_label);
1191 : 510 : r_save_area = expand_normal (t_save_area);
1192 : 510 : r_save_area = convert_memory_address (Pmode, r_save_area);
1193 : : /* Copy the address of the save location to a register just in case it was
1194 : : based on the frame pointer. */
1195 : 510 : r_save_area = copy_to_reg (r_save_area);
1196 : 510 : r_fp = gen_rtx_MEM (Pmode, r_save_area);
1197 : 510 : r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1198 : 510 : plus_constant (Pmode, r_save_area,
1199 : 1020 : GET_MODE_SIZE (Pmode)));
1200 : :
1201 : 510 : crtl->has_nonlocal_goto = 1;
1202 : :
1203 : : /* ??? We no longer need to pass the static chain value, afaik. */
1204 : 510 : if (targetm.have_nonlocal_goto ())
1205 : 0 : emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1206 : : else
1207 : : {
1208 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1209 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1210 : :
1211 : 510 : r_label = copy_to_reg (r_label);
1212 : :
1213 : : /* Restore the frame pointer and stack pointer. We must use a
1214 : : temporary since the setjmp buffer may be a local. */
1215 : 510 : r_fp = copy_to_reg (r_fp);
1216 : 510 : emit_stack_restore (SAVE_NONLOCAL, r_sp);
1217 : :
1218 : : /* Ensure the frame pointer move is not optimized. */
1219 : 510 : emit_insn (gen_blockage ());
1220 : 510 : emit_clobber (hard_frame_pointer_rtx);
1221 : 510 : emit_clobber (frame_pointer_rtx);
1222 : 510 : emit_move_insn (hard_frame_pointer_rtx, r_fp);
1223 : :
1224 : : /* USE of hard_frame_pointer_rtx added for consistency;
1225 : : not clear if really needed. */
1226 : 510 : emit_use (hard_frame_pointer_rtx);
1227 : 510 : emit_use (stack_pointer_rtx);
1228 : :
1229 : : /* If the architecture is using a GP register, we must
1230 : : conservatively assume that the target function makes use of it.
1231 : : The prologue of functions with nonlocal gotos must therefore
1232 : : initialize the GP register to the appropriate value, and we
1233 : : must then make sure that this value is live at the point
1234 : : of the jump. (Note that this doesn't necessarily apply
1235 : : to targets with a nonlocal_goto pattern; they are free
1236 : : to implement it in their own way. Note also that this is
1237 : : a no-op if the GP register is a global invariant.) */
1238 : 510 : unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1239 : 0 : if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1240 : 0 : emit_use (pic_offset_table_rtx);
1241 : :
1242 : 510 : emit_indirect_jump (r_label);
1243 : : }
1244 : :
1245 : : /* Search backwards to the jump insn and mark it as a
1246 : : non-local goto. */
1247 : 1020 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1248 : : {
1249 : 1020 : if (JUMP_P (insn))
1250 : : {
1251 : 510 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1252 : 510 : break;
1253 : : }
1254 : 510 : else if (CALL_P (insn))
1255 : : break;
1256 : : }
1257 : :
1258 : 510 : return const0_rtx;
1259 : : }
1260 : :
1261 : : /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1262 : : (not all will be used on all machines) that was passed to __builtin_setjmp.
1263 : : It updates the stack pointer in that block to the current value. This is
1264 : : also called directly by the SJLJ exception handling code. */
1265 : :
1266 : : void
1267 : 0 : expand_builtin_update_setjmp_buf (rtx buf_addr)
1268 : : {
1269 : 0 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1270 : 0 : buf_addr = convert_memory_address (Pmode, buf_addr);
1271 : 0 : rtx stack_save
1272 : 0 : = gen_rtx_MEM (sa_mode,
1273 : 0 : memory_address
1274 : : (sa_mode,
1275 : : plus_constant (Pmode, buf_addr,
1276 : 0 : 2 * GET_MODE_SIZE (Pmode))));
1277 : :
1278 : 0 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
1279 : 0 : }
1280 : :
1281 : : /* Expand a call to __builtin_prefetch. For a target that does not support
1282 : : data prefetch, evaluate the memory address argument in case it has side
1283 : : effects. */
1284 : :
1285 : : static void
1286 : 1971 : expand_builtin_prefetch (tree exp)
1287 : : {
1288 : 1971 : tree arg0, arg1, arg2;
1289 : 1971 : int nargs;
1290 : 1971 : rtx op0, op1, op2;
1291 : :
1292 : 1971 : if (!validate_arglist (exp, POINTER_TYPE, 0))
1293 : : return;
1294 : :
1295 : 1971 : arg0 = CALL_EXPR_ARG (exp, 0);
1296 : :
1297 : : /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1298 : : zero (read) and argument 2 (locality) defaults to 3 (high degree of
1299 : : locality). */
1300 : 1971 : nargs = call_expr_nargs (exp);
1301 : 1971 : arg1 = nargs > 1 ? CALL_EXPR_ARG (exp, 1) : NULL_TREE;
1302 : 1935 : arg2 = nargs > 2 ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
1303 : :
1304 : : /* Argument 0 is an address. */
1305 : 1971 : op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1306 : :
1307 : : /* Argument 1 (read/write flag) must be a compile-time constant int. */
1308 : 1971 : if (arg1 == NULL_TREE)
1309 : 36 : op1 = const0_rtx;
1310 : 1935 : else if (TREE_CODE (arg1) != INTEGER_CST)
1311 : : {
1312 : 0 : error ("second argument to %<__builtin_prefetch%> must be a constant");
1313 : 0 : op1 = const0_rtx;
1314 : : }
1315 : : else
1316 : 1935 : op1 = expand_normal (arg1);
1317 : : /* Argument 1 must be 0, 1 or 2. */
1318 : 1971 : if (!IN_RANGE (INTVAL (op1), 0, 2))
1319 : : {
1320 : 3 : warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1321 : : " using zero");
1322 : 3 : op1 = const0_rtx;
1323 : : }
1324 : :
1325 : : /* Argument 2 (locality) must be a compile-time constant int. */
1326 : 1971 : if (arg2 == NULL_TREE)
1327 : 61 : op2 = GEN_INT (3);
1328 : 1910 : else if (TREE_CODE (arg2) != INTEGER_CST)
1329 : : {
1330 : 0 : error ("third argument to %<__builtin_prefetch%> must be a constant");
1331 : 0 : op2 = const0_rtx;
1332 : : }
1333 : : else
1334 : 1910 : op2 = expand_normal (arg2);
1335 : : /* Argument 2 must be 0, 1, 2, or 3. */
1336 : 1971 : if (!IN_RANGE (INTVAL (op2), 0, 3))
1337 : : {
1338 : 3 : warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1339 : 3 : op2 = const0_rtx;
1340 : : }
1341 : :
1342 : 1971 : if (targetm.have_prefetch ())
1343 : : {
1344 : 1971 : class expand_operand ops[3];
1345 : :
1346 : 1971 : create_address_operand (&ops[0], op0);
1347 : 1971 : create_integer_operand (&ops[1], INTVAL (op1));
1348 : 1971 : create_integer_operand (&ops[2], INTVAL (op2));
1349 : 1971 : if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1350 : 1971 : return;
1351 : : }
1352 : :
1353 : : /* Don't do anything with direct references to volatile memory, but
1354 : : generate code to handle other side effects. */
1355 : 0 : if (!MEM_P (op0) && side_effects_p (op0))
1356 : 0 : emit_insn (op0);
1357 : : }
1358 : :
1359 : : /* Get a MEM rtx for expression EXP which is the address of an operand
1360 : : to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1361 : : the maximum length of the block of memory that might be accessed or
1362 : : NULL if unknown. */
1363 : :
1364 : : rtx
1365 : 685183 : get_memory_rtx (tree exp, tree len)
1366 : : {
1367 : 685183 : tree orig_exp = exp, base;
1368 : 685183 : rtx addr, mem;
1369 : :
1370 : 685183 : gcc_checking_assert
1371 : : (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))));
1372 : :
1373 : : /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1374 : : from its expression, for expr->a.b only <variable>.a.b is recorded. */
1375 : 685183 : if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1376 : 1334 : exp = TREE_OPERAND (exp, 0);
1377 : :
1378 : 685183 : addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1379 : 685183 : mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1380 : :
1381 : : /* Get an expression we can use to find the attributes to assign to MEM.
1382 : : First remove any nops. */
1383 : 1370366 : while (CONVERT_EXPR_P (exp)
1384 : 685183 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1385 : 0 : exp = TREE_OPERAND (exp, 0);
1386 : :
1387 : : /* Build a MEM_REF representing the whole accessed area as a byte blob,
1388 : : (as builtin stringops may alias with anything). */
1389 : 685183 : exp = fold_build2 (MEM_REF,
1390 : : build_array_type (char_type_node,
1391 : : build_range_type (sizetype,
1392 : : size_one_node, len)),
1393 : : exp, build_int_cst (ptr_type_node, 0));
1394 : :
1395 : : /* If the MEM_REF has no acceptable address, try to get the base object
1396 : : from the original address we got, and build an all-aliasing
1397 : : unknown-sized access to that one. */
1398 : 685183 : if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1399 : 685166 : set_mem_attributes (mem, exp, 0);
1400 : 17 : else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1401 : 17 : && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1402 : : 0))))
1403 : : {
1404 : 17 : unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1405 : 17 : exp = build_fold_addr_expr (base);
1406 : 17 : exp = fold_build2 (MEM_REF,
1407 : : build_array_type (char_type_node,
1408 : : build_range_type (sizetype,
1409 : : size_zero_node,
1410 : : NULL)),
1411 : : exp, build_int_cst (ptr_type_node, 0));
1412 : 17 : set_mem_attributes (mem, exp, 0);
1413 : : /* Since we stripped parts make sure the offset is unknown and the
1414 : : alignment is computed from the original address. */
1415 : 17 : clear_mem_offset (mem);
1416 : 17 : set_mem_align (mem, align);
1417 : : }
1418 : 685183 : set_mem_alias_set (mem, 0);
1419 : 685183 : return mem;
1420 : : }
1421 : :
1422 : : /* Built-in functions to perform an untyped call and return. */
1423 : :
1424 : : /* Wrapper that implicitly applies a delta when getting or setting the
1425 : : enclosed value. */
1426 : : template <typename T>
1427 : : class delta_type
1428 : : {
1429 : : T &value; T const delta;
1430 : : public:
1431 : 2518 : delta_type (T &val, T dlt) : value (val), delta (dlt) {}
1432 : 1805 : operator T () const { return value + delta; }
1433 : 713 : T operator = (T val) const { value = val - delta; return val; }
1434 : : };
1435 : :
1436 : : #define saved_apply_args_size \
1437 : : (delta_type<int> (this_target_builtins->x_apply_args_size_plus_one, -1))
1438 : : #define apply_args_mode \
1439 : : (this_target_builtins->x_apply_args_mode)
1440 : : #define saved_apply_result_size \
1441 : : (delta_type<int> (this_target_builtins->x_apply_result_size_plus_one, -1))
1442 : : #define apply_result_mode \
1443 : : (this_target_builtins->x_apply_result_mode)
1444 : :
1445 : : /* Return the size required for the block returned by __builtin_apply_args,
1446 : : and initialize apply_args_mode. */
1447 : :
1448 : : static int
1449 : 945 : apply_args_size (void)
1450 : : {
1451 : 945 : int size = saved_apply_args_size;
1452 : 945 : int align;
1453 : 945 : unsigned int regno;
1454 : :
1455 : : /* The values computed by this function never change. */
1456 : 945 : if (size < 0)
1457 : : {
1458 : : /* The first value is the incoming arg-pointer. */
1459 : 359 : size = GET_MODE_SIZE (Pmode);
1460 : :
1461 : : /* The second value is the structure value address unless this is
1462 : : passed as an "invisible" first argument. */
1463 : 359 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1464 : 0 : size += GET_MODE_SIZE (Pmode);
1465 : :
1466 : 33387 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1467 : 33028 : if (FUNCTION_ARG_REGNO_P (regno))
1468 : : {
1469 : 5385 : fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1470 : :
1471 : 5385 : if (mode != VOIDmode)
1472 : : {
1473 : 5385 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1474 : 5385 : if (size % align != 0)
1475 : 1 : size = CEIL (size, align) * align;
1476 : 5385 : size += GET_MODE_SIZE (mode);
1477 : 5385 : apply_args_mode[regno] = mode;
1478 : : }
1479 : : else
1480 : 0 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1481 : : }
1482 : : else
1483 : 27643 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1484 : :
1485 : 359 : saved_apply_args_size = size;
1486 : : }
1487 : 945 : return size;
1488 : : }
1489 : :
1490 : : /* Return the size required for the block returned by __builtin_apply,
1491 : : and initialize apply_result_mode. */
1492 : :
1493 : : static int
1494 : 860 : apply_result_size (void)
1495 : : {
1496 : 860 : int size = saved_apply_result_size;
1497 : 860 : int align, regno;
1498 : :
1499 : : /* The values computed by this function never change. */
1500 : 860 : if (size < 0)
1501 : : {
1502 : : size = 0;
1503 : :
1504 : 32922 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1505 : 32568 : if (targetm.calls.function_value_regno_p (regno))
1506 : : {
1507 : 2832 : fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1508 : :
1509 : 2832 : if (mode != VOIDmode)
1510 : : {
1511 : 2832 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1512 : 2832 : if (size % align != 0)
1513 : : size = CEIL (size, align) * align;
1514 : 2832 : size += GET_MODE_SIZE (mode);
1515 : 2832 : apply_result_mode[regno] = mode;
1516 : : }
1517 : : else
1518 : 0 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1519 : : }
1520 : : else
1521 : 29736 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1522 : :
1523 : : /* Allow targets that use untyped_call and untyped_return to override
1524 : : the size so that machine-specific information can be stored here. */
1525 : : #ifdef APPLY_RESULT_SIZE
1526 : 354 : size = APPLY_RESULT_SIZE;
1527 : : #endif
1528 : :
1529 : 354 : saved_apply_result_size = size;
1530 : : }
1531 : 860 : return size;
1532 : : }
1533 : :
1534 : : /* Create a vector describing the result block RESULT. If SAVEP is true,
1535 : : the result block is used to save the values; otherwise it is used to
1536 : : restore the values. */
1537 : :
1538 : : static rtx
1539 : 482 : result_vector (int savep, rtx result)
1540 : : {
1541 : 482 : int regno, size, align, nelts;
1542 : 482 : fixed_size_mode mode;
1543 : 482 : rtx reg, mem;
1544 : 482 : rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1545 : :
1546 : 482 : size = nelts = 0;
1547 : 44826 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1548 : 44344 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1549 : : {
1550 : 3856 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1551 : 3856 : if (size % align != 0)
1552 : 0 : size = CEIL (size, align) * align;
1553 : 3856 : reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1554 : 3856 : mem = adjust_address (result, mode, size);
1555 : 7712 : savevec[nelts++] = (savep
1556 : 7712 : ? gen_rtx_SET (mem, reg)
1557 : 0 : : gen_rtx_SET (reg, mem));
1558 : 7712 : size += GET_MODE_SIZE (mode);
1559 : : }
1560 : 482 : return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1561 : : }
1562 : :
1563 : : /* Save the state required to perform an untyped call with the same
1564 : : arguments as were passed to the current function. */
1565 : :
1566 : : static rtx
1567 : 463 : expand_builtin_apply_args_1 (void)
1568 : : {
1569 : 463 : rtx registers, tem;
1570 : 463 : int size, align, regno;
1571 : 463 : fixed_size_mode mode;
1572 : 463 : rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1573 : :
1574 : : /* Create a block where the arg-pointer, structure value address,
1575 : : and argument registers can be saved. */
1576 : 463 : registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1577 : :
1578 : : /* Walk past the arg-pointer and structure value address. */
1579 : 463 : size = GET_MODE_SIZE (Pmode);
1580 : 463 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1581 : 0 : size += GET_MODE_SIZE (Pmode);
1582 : :
1583 : : /* Save each register used in calling a function to the block. */
1584 : 43059 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1585 : 42596 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1586 : : {
1587 : 6945 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1588 : 6945 : if (size % align != 0)
1589 : 1 : size = CEIL (size, align) * align;
1590 : :
1591 : 6945 : tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1592 : :
1593 : 6945 : emit_move_insn (adjust_address (registers, mode, size), tem);
1594 : 13890 : size += GET_MODE_SIZE (mode);
1595 : : }
1596 : :
1597 : : /* Save the arg pointer to the block. */
1598 : 463 : tem = copy_to_reg (crtl->args.internal_arg_pointer);
1599 : : /* We need the pointer as the caller actually passed them to us, not
1600 : : as we might have pretended they were passed. Make sure it's a valid
1601 : : operand, as emit_move_insn isn't expected to handle a PLUS. */
1602 : 463 : if (STACK_GROWS_DOWNWARD)
1603 : 463 : tem
1604 : 463 : = force_operand (plus_constant (Pmode, tem,
1605 : : crtl->args.pretend_args_size),
1606 : : NULL_RTX);
1607 : 463 : emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1608 : :
1609 : 463 : size = GET_MODE_SIZE (Pmode);
1610 : :
1611 : : /* Save the structure value address unless this is passed as an
1612 : : "invisible" first argument. */
1613 : 463 : if (struct_incoming_value)
1614 : 0 : emit_move_insn (adjust_address (registers, Pmode, size),
1615 : : copy_to_reg (struct_incoming_value));
1616 : :
1617 : : /* Return the address of the block. */
1618 : 463 : return copy_addr_to_reg (XEXP (registers, 0));
1619 : : }
1620 : :
1621 : : /* __builtin_apply_args returns block of memory allocated on
1622 : : the stack into which is stored the arg pointer, structure
1623 : : value address, static chain, and all the registers that might
1624 : : possibly be used in performing a function call. The code is
1625 : : moved to the start of the function so the incoming values are
1626 : : saved. */
1627 : :
1628 : : static rtx
1629 : 463 : expand_builtin_apply_args (void)
1630 : : {
1631 : : /* Don't do __builtin_apply_args more than once in a function.
1632 : : Save the result of the first call and reuse it. */
1633 : 463 : if (apply_args_value != 0)
1634 : : return apply_args_value;
1635 : 463 : {
1636 : : /* When this function is called, it means that registers must be
1637 : : saved on entry to this function. So we migrate the
1638 : : call to the first insn of this function. */
1639 : 463 : rtx temp;
1640 : :
1641 : 463 : start_sequence ();
1642 : 463 : temp = expand_builtin_apply_args_1 ();
1643 : 463 : rtx_insn *seq = get_insns ();
1644 : 463 : end_sequence ();
1645 : :
1646 : 463 : apply_args_value = temp;
1647 : :
1648 : : /* Put the insns after the NOTE that starts the function.
1649 : : If this is inside a start_sequence, make the outer-level insn
1650 : : chain current, so the code is placed at the start of the
1651 : : function. If internal_arg_pointer is a non-virtual pseudo,
1652 : : it needs to be placed after the function that initializes
1653 : : that pseudo. */
1654 : 463 : push_topmost_sequence ();
1655 : 463 : if (REG_P (crtl->args.internal_arg_pointer)
1656 : 463 : && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1657 : 0 : emit_insn_before (seq, parm_birth_insn);
1658 : : else
1659 : 463 : emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1660 : 463 : pop_topmost_sequence ();
1661 : 463 : return temp;
1662 : : }
1663 : : }
1664 : :
1665 : : /* Perform an untyped call and save the state required to perform an
1666 : : untyped return of whatever value was returned by the given function. */
1667 : :
1668 : : static rtx
1669 : 482 : expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1670 : : {
1671 : 482 : int size, align, regno;
1672 : 482 : fixed_size_mode mode;
1673 : 482 : rtx incoming_args, result, reg, dest, src;
1674 : 482 : rtx_call_insn *call_insn;
1675 : 482 : rtx old_stack_level = 0;
1676 : 482 : rtx call_fusage = 0;
1677 : 482 : rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1678 : :
1679 : 482 : arguments = convert_memory_address (Pmode, arguments);
1680 : :
1681 : : /* Create a block where the return registers can be saved. */
1682 : 482 : result = assign_stack_local (BLKmode, apply_result_size (), -1);
1683 : :
1684 : : /* Fetch the arg pointer from the ARGUMENTS block. */
1685 : 482 : incoming_args = gen_reg_rtx (Pmode);
1686 : 482 : emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1687 : 482 : if (!STACK_GROWS_DOWNWARD)
1688 : : incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1689 : : incoming_args, 0, OPTAB_LIB_WIDEN);
1690 : :
1691 : : /* Push a new argument block and copy the arguments. Do not allow
1692 : : the (potential) memcpy call below to interfere with our stack
1693 : : manipulations. */
1694 : 482 : do_pending_stack_adjust ();
1695 : 482 : NO_DEFER_POP;
1696 : :
1697 : : /* Save the stack with nonlocal if available. */
1698 : 482 : if (targetm.have_save_stack_nonlocal ())
1699 : 482 : emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1700 : : else
1701 : 0 : emit_stack_save (SAVE_BLOCK, &old_stack_level);
1702 : :
1703 : : /* Allocate a block of memory onto the stack and copy the memory
1704 : : arguments to the outgoing arguments address. We can pass TRUE
1705 : : as the 4th argument because we just saved the stack pointer
1706 : : and will restore it right after the call. */
1707 : 964 : allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1708 : :
1709 : : /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1710 : : may have already set current_function_calls_alloca to true.
1711 : : current_function_calls_alloca won't be set if argsize is zero,
1712 : : so we have to guarantee need_drap is true here. */
1713 : 482 : if (SUPPORTS_STACK_ALIGNMENT)
1714 : 482 : crtl->need_drap = true;
1715 : :
1716 : 482 : dest = virtual_outgoing_args_rtx;
1717 : 482 : if (!STACK_GROWS_DOWNWARD)
1718 : : {
1719 : : if (CONST_INT_P (argsize))
1720 : : dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1721 : : else
1722 : : dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1723 : : }
1724 : 482 : dest = gen_rtx_MEM (BLKmode, dest);
1725 : 482 : set_mem_align (dest, PARM_BOUNDARY);
1726 : 482 : src = gen_rtx_MEM (BLKmode, incoming_args);
1727 : 482 : set_mem_align (src, PARM_BOUNDARY);
1728 : 482 : emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1729 : :
1730 : : /* Refer to the argument block. */
1731 : 482 : apply_args_size ();
1732 : 482 : arguments = gen_rtx_MEM (BLKmode, arguments);
1733 : 482 : set_mem_align (arguments, PARM_BOUNDARY);
1734 : :
1735 : : /* Walk past the arg-pointer and structure value address. */
1736 : 482 : size = GET_MODE_SIZE (Pmode);
1737 : 482 : if (struct_value)
1738 : 0 : size += GET_MODE_SIZE (Pmode);
1739 : :
1740 : : /* Restore each of the registers previously saved. Make USE insns
1741 : : for each of these registers for use in making the call. */
1742 : 44826 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1743 : 44344 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1744 : : {
1745 : 7230 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1746 : 7230 : if (size % align != 0)
1747 : 0 : size = CEIL (size, align) * align;
1748 : 7230 : reg = gen_rtx_REG (mode, regno);
1749 : 7230 : emit_move_insn (reg, adjust_address (arguments, mode, size));
1750 : 7230 : use_reg (&call_fusage, reg);
1751 : 14460 : size += GET_MODE_SIZE (mode);
1752 : : }
1753 : :
1754 : : /* Restore the structure value address unless this is passed as an
1755 : : "invisible" first argument. */
1756 : 482 : size = GET_MODE_SIZE (Pmode);
1757 : 482 : if (struct_value)
1758 : : {
1759 : 0 : rtx value = gen_reg_rtx (Pmode);
1760 : 0 : emit_move_insn (value, adjust_address (arguments, Pmode, size));
1761 : 0 : emit_move_insn (struct_value, value);
1762 : 0 : if (REG_P (struct_value))
1763 : 0 : use_reg (&call_fusage, struct_value);
1764 : : }
1765 : :
1766 : : /* All arguments and registers used for the call are set up by now! */
1767 : 482 : function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1768 : :
1769 : : /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1770 : : and we don't want to load it into a register as an optimization,
1771 : : because prepare_call_address already did it if it should be done. */
1772 : 482 : if (GET_CODE (function) != SYMBOL_REF)
1773 : 29 : function = memory_address (FUNCTION_MODE, function);
1774 : :
1775 : : /* Generate the actual call instruction and save the return value. */
1776 : 482 : if (targetm.have_untyped_call ())
1777 : : {
1778 : 482 : rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1779 : 482 : rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1780 : : result_vector (1, result));
1781 : 5788 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1782 : 5306 : if (CALL_P (insn))
1783 : 482 : add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1784 : 482 : emit_insn (seq);
1785 : : }
1786 : 0 : else if (targetm.have_call_value ())
1787 : : {
1788 : : rtx valreg = 0;
1789 : :
1790 : : /* Locate the unique return register. It is not possible to
1791 : : express a call that sets more than one return register using
1792 : : call_value; use untyped_call for that. In fact, untyped_call
1793 : : only needs to save the return registers in the given block. */
1794 : 0 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1795 : 0 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1796 : : {
1797 : 0 : gcc_assert (!valreg); /* have_untyped_call required. */
1798 : :
1799 : 0 : valreg = gen_rtx_REG (mode, regno);
1800 : : }
1801 : :
1802 : 0 : emit_insn (targetm.gen_call_value (valreg,
1803 : : gen_rtx_MEM (FUNCTION_MODE, function),
1804 : : const0_rtx, NULL_RTX, const0_rtx));
1805 : :
1806 : 0 : emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1807 : : }
1808 : : else
1809 : 0 : gcc_unreachable ();
1810 : :
1811 : : /* Find the CALL insn we just emitted, and attach the register usage
1812 : : information. */
1813 : 482 : call_insn = last_call_insn ();
1814 : 482 : add_function_usage_to (call_insn, call_fusage);
1815 : :
1816 : : /* Restore the stack. */
1817 : 482 : if (targetm.have_save_stack_nonlocal ())
1818 : 482 : emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1819 : : else
1820 : 0 : emit_stack_restore (SAVE_BLOCK, old_stack_level);
1821 : 482 : fixup_args_size_notes (call_insn, get_last_insn (), 0);
1822 : :
1823 : 482 : OK_DEFER_POP;
1824 : :
1825 : : /* Return the address of the result block. */
1826 : 482 : result = copy_addr_to_reg (XEXP (result, 0));
1827 : 482 : return convert_memory_address (ptr_mode, result);
1828 : : }
1829 : :
1830 : : /* Perform an untyped return. */
1831 : :
1832 : : static void
1833 : 378 : expand_builtin_return (rtx result)
1834 : : {
1835 : 378 : int size, align, regno;
1836 : 378 : fixed_size_mode mode;
1837 : 378 : rtx reg;
1838 : 378 : rtx_insn *call_fusage = 0;
1839 : :
1840 : 378 : result = convert_memory_address (Pmode, result);
1841 : :
1842 : 378 : apply_result_size ();
1843 : 378 : result = gen_rtx_MEM (BLKmode, result);
1844 : :
1845 : 378 : if (targetm.have_untyped_return ())
1846 : : {
1847 : 0 : rtx vector = result_vector (0, result);
1848 : 0 : emit_jump_insn (targetm.gen_untyped_return (result, vector));
1849 : 0 : emit_barrier ();
1850 : 0 : return;
1851 : : }
1852 : :
1853 : : /* Restore the return value and note that each value is used. */
1854 : : size = 0;
1855 : 35154 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1856 : 34776 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1857 : : {
1858 : 3024 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1859 : 3024 : if (size % align != 0)
1860 : 0 : size = CEIL (size, align) * align;
1861 : 3024 : reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1862 : 3024 : emit_move_insn (reg, adjust_address (result, mode, size));
1863 : :
1864 : 3024 : push_to_sequence (call_fusage);
1865 : 3024 : emit_use (reg);
1866 : 3024 : call_fusage = get_insns ();
1867 : 3024 : end_sequence ();
1868 : 6048 : size += GET_MODE_SIZE (mode);
1869 : : }
1870 : :
1871 : : /* Put the USE insns before the return. */
1872 : 378 : emit_insn (call_fusage);
1873 : :
1874 : : /* Return whatever values was restored by jumping directly to the end
1875 : : of the function. */
1876 : 378 : expand_naked_return ();
1877 : : }
1878 : :
1879 : : /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1880 : :
1881 : : int
1882 : 2591 : type_to_class (tree type)
1883 : : {
1884 : 2591 : switch (TREE_CODE (type))
1885 : : {
1886 : : case VOID_TYPE: return void_type_class;
1887 : 931 : case INTEGER_TYPE: return integer_type_class;
1888 : 39 : case ENUMERAL_TYPE: return enumeral_type_class;
1889 : 38 : case BOOLEAN_TYPE: return boolean_type_class;
1890 : 1173 : case POINTER_TYPE: return pointer_type_class;
1891 : 27 : case REFERENCE_TYPE: return reference_type_class;
1892 : 36 : case OFFSET_TYPE: return offset_type_class;
1893 : 132 : case REAL_TYPE: return real_type_class;
1894 : 40 : case COMPLEX_TYPE: return complex_type_class;
1895 : 20 : case FUNCTION_TYPE: return function_type_class;
1896 : 0 : case METHOD_TYPE: return method_type_class;
1897 : 44 : case RECORD_TYPE: return record_type_class;
1898 : 44 : case UNION_TYPE:
1899 : 44 : case QUAL_UNION_TYPE: return union_type_class;
1900 : 24 : case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1901 : 24 : ? string_type_class : array_type_class);
1902 : 0 : case LANG_TYPE: return lang_type_class;
1903 : 0 : case OPAQUE_TYPE: return opaque_type_class;
1904 : 0 : case BITINT_TYPE: return bitint_type_class;
1905 : 30 : case VECTOR_TYPE: return vector_type_class;
1906 : 3 : default: return no_type_class;
1907 : : }
1908 : : }
1909 : :
1910 : : /* Expand a call EXP to __builtin_classify_type. */
1911 : :
1912 : : static rtx
1913 : 0 : expand_builtin_classify_type (tree exp)
1914 : : {
1915 : 0 : if (call_expr_nargs (exp))
1916 : 0 : return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1917 : 0 : return GEN_INT (no_type_class);
1918 : : }
1919 : :
1920 : : /* This helper macro, meant to be used in mathfn_built_in below, determines
1921 : : which among a set of builtin math functions is appropriate for a given type
1922 : : mode. The `F' (float) and `L' (long double) are automatically generated
1923 : : from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1924 : : types, there are additional types that are considered with 'F32', 'F64',
1925 : : 'F128', etc. suffixes. */
1926 : : #define CASE_MATHFN(MATHFN) \
1927 : : CASE_CFN_##MATHFN: \
1928 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1929 : : fcodel = BUILT_IN_##MATHFN##L ; break;
1930 : : /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1931 : : types. */
1932 : : #define CASE_MATHFN_FLOATN(MATHFN) \
1933 : : CASE_CFN_##MATHFN: \
1934 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1935 : : fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1936 : : fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1937 : : fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1938 : : fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1939 : : break;
1940 : : /* Similar to above, but appends _R after any F/L suffix. */
1941 : : #define CASE_MATHFN_REENT(MATHFN) \
1942 : : case CFN_BUILT_IN_##MATHFN##_R: \
1943 : : case CFN_BUILT_IN_##MATHFN##F_R: \
1944 : : case CFN_BUILT_IN_##MATHFN##L_R: \
1945 : : fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1946 : : fcodel = BUILT_IN_##MATHFN##L_R ; break;
1947 : :
1948 : : /* Return a function equivalent to FN but operating on floating-point
1949 : : values of type TYPE, or END_BUILTINS if no such function exists.
1950 : : This is purely an operation on function codes; it does not guarantee
1951 : : that the target actually has an implementation of the function. */
1952 : :
1953 : : static built_in_function
1954 : 556748 : mathfn_built_in_2 (tree type, combined_fn fn)
1955 : : {
1956 : 556748 : tree mtype;
1957 : 556748 : built_in_function fcode, fcodef, fcodel;
1958 : 556748 : built_in_function fcodef16 = END_BUILTINS;
1959 : 556748 : built_in_function fcodef32 = END_BUILTINS;
1960 : 556748 : built_in_function fcodef64 = END_BUILTINS;
1961 : 556748 : built_in_function fcodef128 = END_BUILTINS;
1962 : 556748 : built_in_function fcodef32x = END_BUILTINS;
1963 : 556748 : built_in_function fcodef64x = END_BUILTINS;
1964 : 556748 : built_in_function fcodef128x = END_BUILTINS;
1965 : :
1966 : : /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1967 : : break the uses below. */
1968 : : #undef HUGE_VAL
1969 : : #undef NAN
1970 : :
1971 : 556748 : switch (fn)
1972 : : {
1973 : : #define SEQ_OF_CASE_MATHFN \
1974 : : CASE_MATHFN_FLOATN (ACOS) \
1975 : : CASE_MATHFN_FLOATN (ACOSH) \
1976 : : CASE_MATHFN_FLOATN (ASIN) \
1977 : : CASE_MATHFN_FLOATN (ASINH) \
1978 : : CASE_MATHFN_FLOATN (ATAN) \
1979 : : CASE_MATHFN_FLOATN (ATAN2) \
1980 : : CASE_MATHFN_FLOATN (ATANH) \
1981 : : CASE_MATHFN_FLOATN (CBRT) \
1982 : : CASE_MATHFN_FLOATN (CEIL) \
1983 : : CASE_MATHFN (CEXPI) \
1984 : : CASE_MATHFN_FLOATN (COPYSIGN) \
1985 : : CASE_MATHFN_FLOATN (COS) \
1986 : : CASE_MATHFN_FLOATN (COSH) \
1987 : : CASE_MATHFN (DREM) \
1988 : : CASE_MATHFN_FLOATN (ERF) \
1989 : : CASE_MATHFN_FLOATN (ERFC) \
1990 : : CASE_MATHFN_FLOATN (EXP) \
1991 : : CASE_MATHFN (EXP10) \
1992 : : CASE_MATHFN_FLOATN (EXP2) \
1993 : : CASE_MATHFN_FLOATN (EXPM1) \
1994 : : CASE_MATHFN_FLOATN (FABS) \
1995 : : CASE_MATHFN_FLOATN (FDIM) \
1996 : : CASE_MATHFN_FLOATN (FLOOR) \
1997 : : CASE_MATHFN_FLOATN (FMA) \
1998 : : CASE_MATHFN_FLOATN (FMAX) \
1999 : : CASE_MATHFN_FLOATN (FMIN) \
2000 : : CASE_MATHFN_FLOATN (FMOD) \
2001 : : CASE_MATHFN_FLOATN (FREXP) \
2002 : : CASE_MATHFN (GAMMA) \
2003 : : CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2004 : : CASE_MATHFN_FLOATN (HUGE_VAL) \
2005 : : CASE_MATHFN_FLOATN (HYPOT) \
2006 : : CASE_MATHFN_FLOATN (ILOGB) \
2007 : : CASE_MATHFN (ICEIL) \
2008 : : CASE_MATHFN (IFLOOR) \
2009 : : CASE_MATHFN_FLOATN (INF) \
2010 : : CASE_MATHFN (IRINT) \
2011 : : CASE_MATHFN (IROUND) \
2012 : : CASE_MATHFN (ISINF) \
2013 : : CASE_MATHFN (J0) \
2014 : : CASE_MATHFN (J1) \
2015 : : CASE_MATHFN (JN) \
2016 : : CASE_MATHFN (LCEIL) \
2017 : : CASE_MATHFN_FLOATN (LDEXP) \
2018 : : CASE_MATHFN (LFLOOR) \
2019 : : CASE_MATHFN_FLOATN (LGAMMA) \
2020 : : CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2021 : : CASE_MATHFN (LLCEIL) \
2022 : : CASE_MATHFN (LLFLOOR) \
2023 : : CASE_MATHFN_FLOATN (LLRINT) \
2024 : : CASE_MATHFN_FLOATN (LLROUND) \
2025 : : CASE_MATHFN_FLOATN (LOG) \
2026 : : CASE_MATHFN_FLOATN (LOG10) \
2027 : : CASE_MATHFN_FLOATN (LOG1P) \
2028 : : CASE_MATHFN_FLOATN (LOG2) \
2029 : : CASE_MATHFN_FLOATN (LOGB) \
2030 : : CASE_MATHFN_FLOATN (LRINT) \
2031 : : CASE_MATHFN_FLOATN (LROUND) \
2032 : : CASE_MATHFN_FLOATN (MODF) \
2033 : : CASE_MATHFN_FLOATN (NAN) \
2034 : : CASE_MATHFN_FLOATN (NANS) \
2035 : : CASE_MATHFN_FLOATN (NEARBYINT) \
2036 : : CASE_MATHFN_FLOATN (NEXTAFTER) \
2037 : : CASE_MATHFN (NEXTTOWARD) \
2038 : : CASE_MATHFN_FLOATN (POW) \
2039 : : CASE_MATHFN (POWI) \
2040 : : CASE_MATHFN (POW10) \
2041 : : CASE_MATHFN_FLOATN (REMAINDER) \
2042 : : CASE_MATHFN_FLOATN (REMQUO) \
2043 : : CASE_MATHFN_FLOATN (RINT) \
2044 : : CASE_MATHFN_FLOATN (ROUND) \
2045 : : CASE_MATHFN_FLOATN (ROUNDEVEN) \
2046 : : CASE_MATHFN (SCALB) \
2047 : : CASE_MATHFN_FLOATN (SCALBLN) \
2048 : : CASE_MATHFN_FLOATN (SCALBN) \
2049 : : CASE_MATHFN (SIGNBIT) \
2050 : : CASE_MATHFN (SIGNIFICAND) \
2051 : : CASE_MATHFN_FLOATN (SIN) \
2052 : : CASE_MATHFN (SINCOS) \
2053 : : CASE_MATHFN_FLOATN (SINH) \
2054 : : CASE_MATHFN_FLOATN (SQRT) \
2055 : : CASE_MATHFN_FLOATN (TAN) \
2056 : : CASE_MATHFN_FLOATN (TANH) \
2057 : : CASE_MATHFN_FLOATN (TGAMMA) \
2058 : : CASE_MATHFN_FLOATN (TRUNC) \
2059 : : CASE_MATHFN (Y0) \
2060 : : CASE_MATHFN (Y1) \
2061 : : CASE_MATHFN (YN)
2062 : :
2063 : 556685 : SEQ_OF_CASE_MATHFN
2064 : :
2065 : : default:
2066 : : return END_BUILTINS;
2067 : : }
2068 : :
2069 : 556748 : mtype = TYPE_MAIN_VARIANT (type);
2070 : 556748 : if (mtype == double_type_node)
2071 : : return fcode;
2072 : 515863 : else if (mtype == float_type_node)
2073 : : return fcodef;
2074 : 467613 : else if (mtype == long_double_type_node)
2075 : : return fcodel;
2076 : 434995 : else if (mtype == float16_type_node)
2077 : : return fcodef16;
2078 : 434947 : else if (mtype == float32_type_node)
2079 : : return fcodef32;
2080 : 434947 : else if (mtype == float64_type_node)
2081 : : return fcodef64;
2082 : 434947 : else if (mtype == float128_type_node)
2083 : : return fcodef128;
2084 : 427663 : else if (mtype == float32x_type_node)
2085 : : return fcodef32x;
2086 : 427663 : else if (mtype == float64x_type_node)
2087 : : return fcodef64x;
2088 : 427663 : else if (mtype == float128x_type_node)
2089 : : return fcodef128x;
2090 : : else
2091 : 427663 : return END_BUILTINS;
2092 : : }
2093 : :
2094 : : #undef CASE_MATHFN
2095 : : #undef CASE_MATHFN_FLOATN
2096 : : #undef CASE_MATHFN_REENT
2097 : :
2098 : : /* Return mathematic function equivalent to FN but operating directly on TYPE,
2099 : : if available. If IMPLICIT_P is true use the implicit builtin declaration,
2100 : : otherwise use the explicit declaration. If we can't do the conversion,
2101 : : return null. */
2102 : :
2103 : : static tree
2104 : 556602 : mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2105 : : {
2106 : 556602 : built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2107 : 556602 : if (fcode2 == END_BUILTINS)
2108 : : return NULL_TREE;
2109 : :
2110 : 125291 : if (implicit_p && !builtin_decl_implicit_p (fcode2))
2111 : : return NULL_TREE;
2112 : :
2113 : 121558 : return builtin_decl_explicit (fcode2);
2114 : : }
2115 : :
2116 : : /* Like mathfn_built_in_1, but always use the implicit array. */
2117 : :
2118 : : tree
2119 : 210 : mathfn_built_in (tree type, combined_fn fn)
2120 : : {
2121 : 210 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2122 : : }
2123 : :
2124 : : /* Like mathfn_built_in_1, but always use the explicit array. */
2125 : :
2126 : : tree
2127 : 0 : mathfn_built_in_explicit (tree type, combined_fn fn)
2128 : : {
2129 : 0 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2130 : : }
2131 : :
2132 : : /* Like mathfn_built_in_1, but take a built_in_function and
2133 : : always use the implicit array. */
2134 : :
2135 : : tree
2136 : 556171 : mathfn_built_in (tree type, enum built_in_function fn)
2137 : : {
2138 : 556171 : return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2139 : : }
2140 : :
2141 : : /* Return the type associated with a built in function, i.e., the one
2142 : : to be passed to mathfn_built_in to get the type-specific
2143 : : function. */
2144 : :
2145 : : tree
2146 : 1244 : mathfn_built_in_type (combined_fn fn)
2147 : : {
2148 : : #define CASE_MATHFN(MATHFN) \
2149 : : case CFN_BUILT_IN_##MATHFN: \
2150 : : return double_type_node; \
2151 : : case CFN_BUILT_IN_##MATHFN##F: \
2152 : : return float_type_node; \
2153 : : case CFN_BUILT_IN_##MATHFN##L: \
2154 : : return long_double_type_node;
2155 : :
2156 : : #define CASE_MATHFN_FLOATN(MATHFN) \
2157 : : CASE_MATHFN(MATHFN) \
2158 : : case CFN_BUILT_IN_##MATHFN##F16: \
2159 : : return float16_type_node; \
2160 : : case CFN_BUILT_IN_##MATHFN##F32: \
2161 : : return float32_type_node; \
2162 : : case CFN_BUILT_IN_##MATHFN##F64: \
2163 : : return float64_type_node; \
2164 : : case CFN_BUILT_IN_##MATHFN##F128: \
2165 : : return float128_type_node; \
2166 : : case CFN_BUILT_IN_##MATHFN##F32X: \
2167 : : return float32x_type_node; \
2168 : : case CFN_BUILT_IN_##MATHFN##F64X: \
2169 : : return float64x_type_node; \
2170 : : case CFN_BUILT_IN_##MATHFN##F128X: \
2171 : : return float128x_type_node;
2172 : :
2173 : : /* Similar to above, but appends _R after any F/L suffix. */
2174 : : #define CASE_MATHFN_REENT(MATHFN) \
2175 : : case CFN_BUILT_IN_##MATHFN##_R: \
2176 : : return double_type_node; \
2177 : : case CFN_BUILT_IN_##MATHFN##F_R: \
2178 : : return float_type_node; \
2179 : : case CFN_BUILT_IN_##MATHFN##L_R: \
2180 : : return long_double_type_node;
2181 : :
2182 : 1244 : switch (fn)
2183 : : {
2184 : 1244 : SEQ_OF_CASE_MATHFN
2185 : :
2186 : : default:
2187 : : return NULL_TREE;
2188 : : }
2189 : :
2190 : : #undef CASE_MATHFN
2191 : : #undef CASE_MATHFN_FLOATN
2192 : : #undef CASE_MATHFN_REENT
2193 : : #undef SEQ_OF_CASE_MATHFN
2194 : : }
2195 : :
2196 : : /* Check whether there is an internal function associated with function FN
2197 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2198 : : IFN_LAST.
2199 : :
2200 : : Note that this function only tests whether the function is defined in
2201 : : internals.def, not whether it is actually available on the target. */
2202 : :
2203 : : static internal_fn
2204 : 14067872 : associated_internal_fn (built_in_function fn, tree return_type)
2205 : : {
2206 : 14067872 : switch (fn)
2207 : : {
2208 : : #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2209 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2210 : : #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2211 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2212 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2213 : : #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2214 : : CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2215 : : #include "internal-fn.def"
2216 : :
2217 : : CASE_FLT_FN (BUILT_IN_POW10):
2218 : : return IFN_EXP10;
2219 : :
2220 : : CASE_FLT_FN (BUILT_IN_DREM):
2221 : : return IFN_REMAINDER;
2222 : :
2223 : 19516 : CASE_FLT_FN (BUILT_IN_SCALBN):
2224 : 19516 : CASE_FLT_FN (BUILT_IN_SCALBLN):
2225 : 19516 : if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2226 : : return IFN_LDEXP;
2227 : : return IFN_LAST;
2228 : 75 : case BUILT_IN_CRC8_DATA8:
2229 : 75 : case BUILT_IN_CRC16_DATA8:
2230 : 75 : case BUILT_IN_CRC16_DATA16:
2231 : 75 : case BUILT_IN_CRC32_DATA8:
2232 : 75 : case BUILT_IN_CRC32_DATA16:
2233 : 75 : case BUILT_IN_CRC32_DATA32:
2234 : 75 : case BUILT_IN_CRC64_DATA8:
2235 : 75 : case BUILT_IN_CRC64_DATA16:
2236 : 75 : case BUILT_IN_CRC64_DATA32:
2237 : 75 : case BUILT_IN_CRC64_DATA64:
2238 : 75 : return IFN_CRC;
2239 : 48 : case BUILT_IN_REV_CRC8_DATA8:
2240 : 48 : case BUILT_IN_REV_CRC16_DATA8:
2241 : 48 : case BUILT_IN_REV_CRC16_DATA16:
2242 : 48 : case BUILT_IN_REV_CRC32_DATA8:
2243 : 48 : case BUILT_IN_REV_CRC32_DATA16:
2244 : 48 : case BUILT_IN_REV_CRC32_DATA32:
2245 : 48 : case BUILT_IN_REV_CRC64_DATA8:
2246 : 48 : case BUILT_IN_REV_CRC64_DATA16:
2247 : 48 : case BUILT_IN_REV_CRC64_DATA32:
2248 : 48 : case BUILT_IN_REV_CRC64_DATA64:
2249 : 48 : return IFN_CRC_REV;
2250 : : default:
2251 : : return IFN_LAST;
2252 : : }
2253 : : }
2254 : :
2255 : : /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2256 : : return its code, otherwise return IFN_LAST. Note that this function
2257 : : only tests whether the function is defined in internals.def, not whether
2258 : : it is actually available on the target. */
2259 : :
2260 : : internal_fn
2261 : 625927 : associated_internal_fn (tree fndecl)
2262 : : {
2263 : 625927 : gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2264 : 625927 : return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2265 : 625927 : TREE_TYPE (TREE_TYPE (fndecl)));
2266 : : }
2267 : :
2268 : : /* Check whether there is an internal function associated with function CFN
2269 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2270 : : IFN_LAST.
2271 : :
2272 : : Note that this function only tests whether the function is defined in
2273 : : internals.def, not whether it is actually available on the target. */
2274 : :
2275 : : internal_fn
2276 : 31377934 : associated_internal_fn (combined_fn cfn, tree return_type)
2277 : : {
2278 : 31377934 : if (internal_fn_p (cfn))
2279 : 17935989 : return as_internal_fn (cfn);
2280 : 13441945 : return associated_internal_fn (as_builtin_fn (cfn), return_type);
2281 : : }
2282 : :
2283 : : /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2284 : : on the current target by a call to an internal function, return the
2285 : : code of that internal function, otherwise return IFN_LAST. The caller
2286 : : is responsible for ensuring that any side-effects of the built-in
2287 : : call are dealt with correctly. E.g. if CALL sets errno, the caller
2288 : : must decide that the errno result isn't needed or make it available
2289 : : in some other way. */
2290 : :
2291 : : internal_fn
2292 : 838295 : replacement_internal_fn (gcall *call)
2293 : : {
2294 : 838295 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2295 : : {
2296 : 623591 : internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2297 : 623591 : if (ifn != IFN_LAST)
2298 : : {
2299 : 59614 : tree_pair types = direct_internal_fn_types (ifn, call);
2300 : 59614 : optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2301 : 59614 : if (direct_internal_fn_supported_p (ifn, types, opt_type))
2302 : 38350 : return ifn;
2303 : : }
2304 : : }
2305 : : return IFN_LAST;
2306 : : }
2307 : :
2308 : : /* Expand a call to the builtin trinary math functions (fma).
2309 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2310 : : function in-line. EXP is the expression that is a call to the builtin
2311 : : function; if convenient, the result should be placed in TARGET.
2312 : : SUBTARGET may be used as the target for computing one of EXP's
2313 : : operands. */
2314 : :
2315 : : static rtx
2316 : 364 : expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2317 : : {
2318 : 364 : optab builtin_optab;
2319 : 364 : rtx op0, op1, op2, result;
2320 : 364 : rtx_insn *insns;
2321 : 364 : tree fndecl = get_callee_fndecl (exp);
2322 : 364 : tree arg0, arg1, arg2;
2323 : 364 : machine_mode mode;
2324 : :
2325 : 364 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2326 : : return NULL_RTX;
2327 : :
2328 : 364 : arg0 = CALL_EXPR_ARG (exp, 0);
2329 : 364 : arg1 = CALL_EXPR_ARG (exp, 1);
2330 : 364 : arg2 = CALL_EXPR_ARG (exp, 2);
2331 : :
2332 : 364 : switch (DECL_FUNCTION_CODE (fndecl))
2333 : : {
2334 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
2335 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2336 : 364 : builtin_optab = fma_optab; break;
2337 : 0 : default:
2338 : 0 : gcc_unreachable ();
2339 : : }
2340 : :
2341 : : /* Make a suitable register to place result in. */
2342 : 364 : mode = TYPE_MODE (TREE_TYPE (exp));
2343 : :
2344 : : /* Before working hard, check whether the instruction is available. */
2345 : 364 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2346 : : return NULL_RTX;
2347 : :
2348 : 0 : result = gen_reg_rtx (mode);
2349 : :
2350 : : /* Always stabilize the argument list. */
2351 : 0 : CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2352 : 0 : CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2353 : 0 : CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2354 : :
2355 : 0 : op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2356 : 0 : op1 = expand_normal (arg1);
2357 : 0 : op2 = expand_normal (arg2);
2358 : :
2359 : 0 : start_sequence ();
2360 : :
2361 : : /* Compute into RESULT.
2362 : : Set RESULT to wherever the result comes back. */
2363 : 0 : result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2364 : : result, 0);
2365 : :
2366 : : /* If we were unable to expand via the builtin, stop the sequence
2367 : : (without outputting the insns) and call to the library function
2368 : : with the stabilized argument list. */
2369 : 0 : if (result == 0)
2370 : : {
2371 : 0 : end_sequence ();
2372 : 0 : return expand_call (exp, target, target == const0_rtx);
2373 : : }
2374 : :
2375 : : /* Output the entire sequence. */
2376 : 0 : insns = get_insns ();
2377 : 0 : end_sequence ();
2378 : 0 : emit_insn (insns);
2379 : :
2380 : 0 : return result;
2381 : : }
2382 : :
2383 : : /* Expand a call to the builtin sin and cos math functions.
2384 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2385 : : function in-line. EXP is the expression that is a call to the builtin
2386 : : function; if convenient, the result should be placed in TARGET.
2387 : : SUBTARGET may be used as the target for computing one of EXP's
2388 : : operands. */
2389 : :
2390 : : static rtx
2391 : 47 : expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2392 : : {
2393 : 47 : optab builtin_optab;
2394 : 47 : rtx op0;
2395 : 47 : rtx_insn *insns;
2396 : 47 : tree fndecl = get_callee_fndecl (exp);
2397 : 47 : machine_mode mode;
2398 : 47 : tree arg;
2399 : :
2400 : 47 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2401 : : return NULL_RTX;
2402 : :
2403 : 39 : arg = CALL_EXPR_ARG (exp, 0);
2404 : :
2405 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2406 : : {
2407 : 39 : CASE_FLT_FN (BUILT_IN_SIN):
2408 : 39 : CASE_FLT_FN (BUILT_IN_COS):
2409 : 39 : builtin_optab = sincos_optab; break;
2410 : 0 : default:
2411 : 0 : gcc_unreachable ();
2412 : : }
2413 : :
2414 : : /* Make a suitable register to place result in. */
2415 : 39 : mode = TYPE_MODE (TREE_TYPE (exp));
2416 : :
2417 : : /* Check if sincos insn is available, otherwise fallback
2418 : : to sin or cos insn. */
2419 : 39 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2420 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2421 : : {
2422 : : CASE_FLT_FN (BUILT_IN_SIN):
2423 : : builtin_optab = sin_optab; break;
2424 : 14 : CASE_FLT_FN (BUILT_IN_COS):
2425 : 14 : builtin_optab = cos_optab; break;
2426 : 0 : default:
2427 : 0 : gcc_unreachable ();
2428 : : }
2429 : :
2430 : : /* Before working hard, check whether the instruction is available. */
2431 : 39 : if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2432 : : {
2433 : 0 : rtx result = gen_reg_rtx (mode);
2434 : :
2435 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2436 : : need to expand the argument again. This way, we will not perform
2437 : : side-effects more the once. */
2438 : 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2439 : :
2440 : 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2441 : :
2442 : 0 : start_sequence ();
2443 : :
2444 : : /* Compute into RESULT.
2445 : : Set RESULT to wherever the result comes back. */
2446 : 0 : if (builtin_optab == sincos_optab)
2447 : : {
2448 : 0 : int ok;
2449 : :
2450 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
2451 : : {
2452 : 0 : CASE_FLT_FN (BUILT_IN_SIN):
2453 : 0 : ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2454 : 0 : break;
2455 : 0 : CASE_FLT_FN (BUILT_IN_COS):
2456 : 0 : ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2457 : 0 : break;
2458 : 0 : default:
2459 : 0 : gcc_unreachable ();
2460 : : }
2461 : 0 : gcc_assert (ok);
2462 : : }
2463 : : else
2464 : 0 : result = expand_unop (mode, builtin_optab, op0, result, 0);
2465 : :
2466 : 0 : if (result != 0)
2467 : : {
2468 : : /* Output the entire sequence. */
2469 : 0 : insns = get_insns ();
2470 : 0 : end_sequence ();
2471 : 0 : emit_insn (insns);
2472 : 0 : return result;
2473 : : }
2474 : :
2475 : : /* If we were unable to expand via the builtin, stop the sequence
2476 : : (without outputting the insns) and call to the library function
2477 : : with the stabilized argument list. */
2478 : 0 : end_sequence ();
2479 : : }
2480 : :
2481 : 39 : return expand_call (exp, target, target == const0_rtx);
2482 : : }
2483 : :
2484 : : /* Given an interclass math builtin decl FNDECL and it's argument ARG
2485 : : return an RTL instruction code that implements the functionality.
2486 : : If that isn't possible or available return CODE_FOR_nothing. */
2487 : :
2488 : : static enum insn_code
2489 : 1020117 : interclass_mathfn_icode (tree arg, tree fndecl)
2490 : : {
2491 : 1020117 : bool errno_set = false;
2492 : 1020117 : optab builtin_optab = unknown_optab;
2493 : 1020117 : machine_mode mode;
2494 : :
2495 : 1020117 : switch (DECL_FUNCTION_CODE (fndecl))
2496 : : {
2497 : 4 : CASE_FLT_FN (BUILT_IN_ILOGB):
2498 : 4 : errno_set = true; builtin_optab = ilogb_optab; break;
2499 : 249681 : CASE_FLT_FN (BUILT_IN_ISINF):
2500 : 249681 : builtin_optab = isinf_optab; break;
2501 : 520835 : case BUILT_IN_ISFINITE:
2502 : 520835 : builtin_optab = isfinite_optab;
2503 : 520835 : break;
2504 : 247907 : case BUILT_IN_ISNORMAL:
2505 : 247907 : builtin_optab = isnormal_optab;
2506 : 247907 : break;
2507 : : CASE_FLT_FN (BUILT_IN_FINITE):
2508 : : case BUILT_IN_FINITED32:
2509 : : case BUILT_IN_FINITED64:
2510 : : case BUILT_IN_FINITED128:
2511 : : case BUILT_IN_ISINFD32:
2512 : : case BUILT_IN_ISINFD64:
2513 : : case BUILT_IN_ISINFD128:
2514 : : /* These builtins have no optabs (yet). */
2515 : : break;
2516 : 0 : default:
2517 : 0 : gcc_unreachable ();
2518 : : }
2519 : :
2520 : : /* There's no easy way to detect the case we need to set EDOM. */
2521 : 1020117 : if (flag_errno_math && errno_set)
2522 : : return CODE_FOR_nothing;
2523 : :
2524 : : /* Optab mode depends on the mode of the input argument. */
2525 : 1020117 : mode = TYPE_MODE (TREE_TYPE (arg));
2526 : :
2527 : 1020117 : if (builtin_optab)
2528 : 1018427 : return optab_handler (builtin_optab, mode);
2529 : : return CODE_FOR_nothing;
2530 : : }
2531 : :
2532 : : /* Expand a call to one of the builtin math functions that operate on
2533 : : floating point argument and output an integer result (ilogb, isinf,
2534 : : isnan, etc).
2535 : : Return 0 if a normal call should be emitted rather than expanding the
2536 : : function in-line. EXP is the expression that is a call to the builtin
2537 : : function; if convenient, the result should be placed in TARGET. */
2538 : :
2539 : : static rtx
2540 : 4 : expand_builtin_interclass_mathfn (tree exp, rtx target)
2541 : : {
2542 : 4 : enum insn_code icode = CODE_FOR_nothing;
2543 : 4 : rtx op0;
2544 : 4 : tree fndecl = get_callee_fndecl (exp);
2545 : 4 : machine_mode mode;
2546 : 4 : tree arg;
2547 : :
2548 : 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2549 : : return NULL_RTX;
2550 : :
2551 : 4 : arg = CALL_EXPR_ARG (exp, 0);
2552 : 4 : icode = interclass_mathfn_icode (arg, fndecl);
2553 : 4 : mode = TYPE_MODE (TREE_TYPE (arg));
2554 : :
2555 : 4 : if (icode != CODE_FOR_nothing)
2556 : : {
2557 : 2 : class expand_operand ops[1];
2558 : 2 : rtx_insn *last = get_last_insn ();
2559 : 2 : tree orig_arg = arg;
2560 : :
2561 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2562 : : need to expand the argument again. This way, we will not perform
2563 : : side-effects more the once. */
2564 : 2 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2565 : :
2566 : 2 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2567 : :
2568 : 2 : if (mode != GET_MODE (op0))
2569 : 0 : op0 = convert_to_mode (mode, op0, 0);
2570 : :
2571 : 2 : create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2572 : 2 : if (maybe_legitimize_operands (icode, 0, 1, ops)
2573 : 2 : && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2574 : 1 : return ops[0].value;
2575 : :
2576 : 1 : delete_insns_since (last);
2577 : 1 : CALL_EXPR_ARG (exp, 0) = orig_arg;
2578 : : }
2579 : :
2580 : : return NULL_RTX;
2581 : : }
2582 : :
2583 : : /* Expand a call to the builtin sincos math function.
2584 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2585 : : function in-line. EXP is the expression that is a call to the builtin
2586 : : function. */
2587 : :
2588 : : static rtx
2589 : 3 : expand_builtin_sincos (tree exp)
2590 : : {
2591 : 3 : rtx op0, op1, op2, target1, target2;
2592 : 3 : machine_mode mode;
2593 : 3 : tree arg, sinp, cosp;
2594 : 3 : int result;
2595 : 3 : location_t loc = EXPR_LOCATION (exp);
2596 : 3 : tree alias_type, alias_off;
2597 : :
2598 : 3 : if (!validate_arglist (exp, REAL_TYPE,
2599 : : POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2600 : : return NULL_RTX;
2601 : :
2602 : 3 : arg = CALL_EXPR_ARG (exp, 0);
2603 : 3 : sinp = CALL_EXPR_ARG (exp, 1);
2604 : 3 : cosp = CALL_EXPR_ARG (exp, 2);
2605 : :
2606 : : /* Make a suitable register to place result in. */
2607 : 3 : mode = TYPE_MODE (TREE_TYPE (arg));
2608 : :
2609 : : /* Check if sincos insn is available, otherwise emit the call. */
2610 : 3 : if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2611 : : return NULL_RTX;
2612 : :
2613 : 0 : target1 = gen_reg_rtx (mode);
2614 : 0 : target2 = gen_reg_rtx (mode);
2615 : :
2616 : 0 : op0 = expand_normal (arg);
2617 : 0 : alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2618 : 0 : alias_off = build_int_cst (alias_type, 0);
2619 : 0 : op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2620 : : sinp, alias_off));
2621 : 0 : op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2622 : : cosp, alias_off));
2623 : :
2624 : : /* Compute into target1 and target2.
2625 : : Set TARGET to wherever the result comes back. */
2626 : 0 : result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2627 : 0 : gcc_assert (result);
2628 : :
2629 : : /* Move target1 and target2 to the memory locations indicated
2630 : : by op1 and op2. */
2631 : 0 : emit_move_insn (op1, target1);
2632 : 0 : emit_move_insn (op2, target2);
2633 : :
2634 : 0 : return const0_rtx;
2635 : : }
2636 : :
2637 : : /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2638 : : result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2639 : : static rtx
2640 : 60 : expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2641 : : {
2642 : 60 : if (!validate_arglist (exp, VOID_TYPE))
2643 : : return NULL_RTX;
2644 : :
2645 : 60 : insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2646 : 60 : if (icode == CODE_FOR_nothing)
2647 : : return NULL_RTX;
2648 : :
2649 : 0 : if (target == 0
2650 : 0 : || GET_MODE (target) != target_mode
2651 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2652 : 0 : target = gen_reg_rtx (target_mode);
2653 : :
2654 : 0 : rtx pat = GEN_FCN (icode) (target);
2655 : 0 : if (!pat)
2656 : : return NULL_RTX;
2657 : 0 : emit_insn (pat);
2658 : :
2659 : 0 : return target;
2660 : : }
2661 : :
2662 : : /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2663 : : fenv.h), returning the result and setting it in TARGET. Otherwise return
2664 : : NULL_RTX on failure. */
2665 : : static rtx
2666 : 1894 : expand_builtin_feclear_feraise_except (tree exp, rtx target,
2667 : : machine_mode target_mode, optab op_optab)
2668 : : {
2669 : 1894 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2670 : : return NULL_RTX;
2671 : 1894 : rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2672 : :
2673 : 1894 : insn_code icode = direct_optab_handler (op_optab, SImode);
2674 : 1894 : if (icode == CODE_FOR_nothing)
2675 : : return NULL_RTX;
2676 : :
2677 : 0 : if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2678 : : return NULL_RTX;
2679 : :
2680 : 0 : if (target == 0
2681 : 0 : || GET_MODE (target) != target_mode
2682 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2683 : 0 : target = gen_reg_rtx (target_mode);
2684 : :
2685 : 0 : rtx pat = GEN_FCN (icode) (target, op0);
2686 : 0 : if (!pat)
2687 : : return NULL_RTX;
2688 : 0 : emit_insn (pat);
2689 : :
2690 : 0 : return target;
2691 : : }
2692 : :
2693 : : /* Expand a call to the internal cexpi builtin to the sincos math function.
2694 : : EXP is the expression that is a call to the builtin function; if convenient,
2695 : : the result should be placed in TARGET. */
2696 : :
2697 : : static rtx
2698 : 163 : expand_builtin_cexpi (tree exp, rtx target)
2699 : : {
2700 : 163 : tree fndecl = get_callee_fndecl (exp);
2701 : 163 : tree arg, type;
2702 : 163 : machine_mode mode;
2703 : 163 : rtx op0, op1, op2;
2704 : 163 : location_t loc = EXPR_LOCATION (exp);
2705 : :
2706 : 163 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2707 : : return NULL_RTX;
2708 : :
2709 : 163 : arg = CALL_EXPR_ARG (exp, 0);
2710 : 163 : type = TREE_TYPE (arg);
2711 : 163 : mode = TYPE_MODE (TREE_TYPE (arg));
2712 : :
2713 : : /* Try expanding via a sincos optab, fall back to emitting a libcall
2714 : : to sincos or cexp. We are sure we have sincos or cexp because cexpi
2715 : : is only generated from sincos, cexp or if we have either of them. */
2716 : 163 : if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2717 : : {
2718 : 6 : op1 = gen_reg_rtx (mode);
2719 : 6 : op2 = gen_reg_rtx (mode);
2720 : :
2721 : 6 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2722 : :
2723 : : /* Compute into op1 and op2. */
2724 : 6 : expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2725 : : }
2726 : 157 : else if (targetm.libc_has_function (function_sincos, type))
2727 : : {
2728 : 157 : tree call, fn = NULL_TREE;
2729 : 157 : tree top1, top2;
2730 : 157 : rtx op1a, op2a;
2731 : :
2732 : 157 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2733 : 56 : fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2734 : 101 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2735 : 82 : fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2736 : 19 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2737 : 19 : fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2738 : : else
2739 : 0 : gcc_unreachable ();
2740 : :
2741 : 157 : op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2742 : 157 : op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2743 : 157 : op1a = copy_addr_to_reg (XEXP (op1, 0));
2744 : 157 : op2a = copy_addr_to_reg (XEXP (op2, 0));
2745 : 157 : top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2746 : 157 : top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2747 : :
2748 : : /* Make sure not to fold the sincos call again. */
2749 : 157 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2750 : 157 : expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2751 : : call, 3, arg, top1, top2));
2752 : : }
2753 : : else
2754 : : {
2755 : 0 : tree call, fn = NULL_TREE, narg;
2756 : 0 : tree ctype = build_complex_type (type);
2757 : :
2758 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2759 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2760 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2761 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXP);
2762 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2763 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2764 : : else
2765 : 0 : gcc_unreachable ();
2766 : :
2767 : : /* If we don't have a decl for cexp create one. This is the
2768 : : friendliest fallback if the user calls __builtin_cexpi
2769 : : without full target C99 function support. */
2770 : 0 : if (fn == NULL_TREE)
2771 : : {
2772 : 0 : tree fntype;
2773 : 0 : const char *name = NULL;
2774 : :
2775 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2776 : : name = "cexpf";
2777 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2778 : : name = "cexp";
2779 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2780 : 0 : name = "cexpl";
2781 : :
2782 : 0 : fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2783 : 0 : fn = build_fn_decl (name, fntype);
2784 : : }
2785 : :
2786 : 0 : narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2787 : : build_real (type, dconst0), arg);
2788 : :
2789 : : /* Make sure not to fold the cexp call again. */
2790 : 0 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2791 : 0 : return expand_expr (build_call_nary (ctype, call, 1, narg),
2792 : 0 : target, VOIDmode, EXPAND_NORMAL);
2793 : : }
2794 : :
2795 : : /* Now build the proper return type. */
2796 : 326 : return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2797 : 163 : make_tree (TREE_TYPE (arg), op2),
2798 : 163 : make_tree (TREE_TYPE (arg), op1)),
2799 : 163 : target, VOIDmode, EXPAND_NORMAL);
2800 : : }
2801 : :
2802 : : /* Conveniently construct a function call expression. FNDECL names the
2803 : : function to be called, N is the number of arguments, and the "..."
2804 : : parameters are the argument expressions. Unlike build_call_exr
2805 : : this doesn't fold the call, hence it will always return a CALL_EXPR. */
2806 : :
2807 : : static tree
2808 : 135496 : build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2809 : : {
2810 : 135496 : va_list ap;
2811 : 135496 : tree fntype = TREE_TYPE (fndecl);
2812 : 135496 : tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2813 : :
2814 : 135496 : va_start (ap, n);
2815 : 135496 : fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2816 : 135496 : va_end (ap);
2817 : 135496 : SET_EXPR_LOCATION (fn, loc);
2818 : 135496 : return fn;
2819 : : }
2820 : :
2821 : : /* Expand the __builtin_issignaling builtin. This needs to handle
2822 : : all floating point formats that do support NaNs (for those that
2823 : : don't it just sets target to 0). */
2824 : :
2825 : : static rtx
2826 : 823 : expand_builtin_issignaling (tree exp, rtx target)
2827 : : {
2828 : 823 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2829 : : return NULL_RTX;
2830 : :
2831 : 823 : tree arg = CALL_EXPR_ARG (exp, 0);
2832 : 823 : scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2833 : 823 : const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2834 : :
2835 : : /* Expand the argument yielding a RTX expression. */
2836 : 823 : rtx temp = expand_normal (arg);
2837 : :
2838 : : /* If mode doesn't support NaN, always return 0.
2839 : : Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2840 : : __builtin_issignaling working without -fsignaling-nans. Especially
2841 : : when -fno-signaling-nans is the default.
2842 : : On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2843 : : -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2844 : : fold to 0 or non-NaN/Inf classification. */
2845 : 823 : if (!HONOR_NANS (fmode))
2846 : : {
2847 : 0 : emit_move_insn (target, const0_rtx);
2848 : 0 : return target;
2849 : : }
2850 : :
2851 : : /* Check if the back end provides an insn that handles issignaling for the
2852 : : argument's mode. */
2853 : 823 : enum insn_code icode = optab_handler (issignaling_optab, fmode);
2854 : 823 : if (icode != CODE_FOR_nothing)
2855 : : {
2856 : 155 : rtx_insn *last = get_last_insn ();
2857 : 155 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2858 : 155 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2859 : : return this_target;
2860 : 0 : delete_insns_since (last);
2861 : : }
2862 : :
2863 : 668 : if (DECIMAL_FLOAT_MODE_P (fmode))
2864 : : {
2865 : 63 : scalar_int_mode imode;
2866 : 63 : rtx hi;
2867 : 63 : switch (fmt->ieee_bits)
2868 : : {
2869 : 42 : case 32:
2870 : 42 : case 64:
2871 : 42 : imode = int_mode_for_mode (fmode).require ();
2872 : 42 : temp = gen_lowpart (imode, temp);
2873 : 42 : break;
2874 : 21 : case 128:
2875 : 21 : imode = int_mode_for_size (64, 1).require ();
2876 : 21 : hi = NULL_RTX;
2877 : : /* For decimal128, TImode support isn't always there and even when
2878 : : it is, working on the DImode high part is usually better. */
2879 : 21 : if (!MEM_P (temp))
2880 : : {
2881 : 6 : if (rtx t = force_highpart_subreg (imode, temp, fmode))
2882 : : hi = t;
2883 : : else
2884 : : {
2885 : 0 : scalar_int_mode imode2;
2886 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2887 : : {
2888 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2889 : 0 : if (rtx t = force_highpart_subreg (imode, temp2, imode2))
2890 : : hi = t;
2891 : : }
2892 : : }
2893 : 0 : if (!hi)
2894 : : {
2895 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2896 : 0 : emit_move_insn (mem, temp);
2897 : 0 : temp = mem;
2898 : : }
2899 : : }
2900 : 0 : if (!hi)
2901 : : {
2902 : 15 : poly_int64 offset
2903 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
2904 : 15 : hi = adjust_address (temp, imode, offset);
2905 : : }
2906 : : temp = hi;
2907 : : break;
2908 : 0 : default:
2909 : 0 : gcc_unreachable ();
2910 : : }
2911 : : /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2912 : : have 6 bits below it all set. */
2913 : 63 : rtx val
2914 : 63 : = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2915 : 63 : temp = expand_binop (imode, and_optab, temp, val,
2916 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2917 : 63 : temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2918 : 63 : return temp;
2919 : : }
2920 : :
2921 : : /* Only PDP11 has these defined differently but doesn't support NaNs. */
2922 : 605 : gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2923 : 605 : gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2924 : 4235 : gcc_assert (MODE_COMPOSITE_P (fmode)
2925 : : || (fmt->pnan == fmt->p
2926 : : && fmt->signbit_ro == fmt->signbit_rw));
2927 : :
2928 : 605 : switch (fmt->p)
2929 : : {
2930 : 0 : case 106: /* IBM double double */
2931 : : /* For IBM double double, recurse on the most significant double. */
2932 : 0 : gcc_assert (MODE_COMPOSITE_P (fmode));
2933 : 0 : temp = convert_modes (DFmode, fmode, temp, 0);
2934 : 0 : fmode = DFmode;
2935 : 0 : fmt = REAL_MODE_FORMAT (DFmode);
2936 : : /* FALLTHRU */
2937 : 505 : case 8: /* bfloat */
2938 : 505 : case 11: /* IEEE half */
2939 : 505 : case 24: /* IEEE single */
2940 : 505 : case 53: /* IEEE double or Intel extended with rounding to double */
2941 : 505 : if (fmt->p == 53 && fmt->signbit_ro == 79)
2942 : 0 : goto extended;
2943 : 505 : {
2944 : 505 : scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2945 : 505 : temp = gen_lowpart (imode, temp);
2946 : 505 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2947 : : & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2948 : 505 : if (fmt->qnan_msb_set)
2949 : : {
2950 : 505 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2951 : 505 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2952 : : /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2953 : : ((temp ^ bit) & mask) > val. */
2954 : 505 : temp = expand_binop (imode, xor_optab, temp, bit,
2955 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2956 : 505 : temp = expand_binop (imode, and_optab, temp, mask,
2957 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2958 : 505 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
2959 : : 1, 1);
2960 : : }
2961 : : else
2962 : : {
2963 : : /* For MIPS/PA IEEE single/double, expand to:
2964 : : (temp & val) == val. */
2965 : 0 : temp = expand_binop (imode, and_optab, temp, val,
2966 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2967 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
2968 : : 1, 1);
2969 : : }
2970 : : }
2971 : : break;
2972 : 100 : case 113: /* IEEE quad */
2973 : 100 : {
2974 : 100 : rtx hi = NULL_RTX, lo = NULL_RTX;
2975 : 100 : scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2976 : : /* For IEEE quad, TImode support isn't always there and even when
2977 : : it is, working on DImode parts is usually better. */
2978 : 100 : if (!MEM_P (temp))
2979 : : {
2980 : 85 : hi = force_highpart_subreg (imode, temp, fmode);
2981 : 85 : lo = force_lowpart_subreg (imode, temp, fmode);
2982 : 85 : if (!hi || !lo)
2983 : : {
2984 : 0 : scalar_int_mode imode2;
2985 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2986 : : {
2987 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2988 : 0 : hi = force_highpart_subreg (imode, temp2, imode2);
2989 : 0 : lo = force_lowpart_subreg (imode, temp2, imode2);
2990 : : }
2991 : : }
2992 : 85 : if (!hi || !lo)
2993 : : {
2994 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2995 : 0 : emit_move_insn (mem, temp);
2996 : 0 : temp = mem;
2997 : : }
2998 : : }
2999 : 100 : if (!hi || !lo)
3000 : : {
3001 : 15 : poly_int64 offset
3002 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
3003 : 15 : hi = adjust_address (temp, imode, offset);
3004 : 15 : offset = subreg_lowpart_offset (imode, GET_MODE (temp));
3005 : 15 : lo = adjust_address (temp, imode, offset);
3006 : : }
3007 : 100 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
3008 : : & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
3009 : 100 : if (fmt->qnan_msb_set)
3010 : : {
3011 : 100 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
3012 : : - 64)));
3013 : 100 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
3014 : : /* For non-MIPS/PA IEEE quad, expand to:
3015 : : (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
3016 : 100 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3017 : 100 : lo = expand_binop (imode, ior_optab, lo, nlo,
3018 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3019 : 100 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
3020 : 100 : temp = expand_binop (imode, xor_optab, hi, bit,
3021 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3022 : 100 : temp = expand_binop (imode, ior_optab, temp, lo,
3023 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3024 : 100 : temp = expand_binop (imode, and_optab, temp, mask,
3025 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3026 : 100 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
3027 : : 1, 1);
3028 : : }
3029 : : else
3030 : : {
3031 : : /* For MIPS/PA IEEE quad, expand to:
3032 : : (hi & val) == val. */
3033 : 0 : temp = expand_binop (imode, and_optab, hi, val,
3034 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3035 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
3036 : : 1, 1);
3037 : : }
3038 : : }
3039 : : break;
3040 : 0 : case 64: /* Intel or Motorola extended */
3041 : 0 : extended:
3042 : 0 : {
3043 : 0 : rtx ex, hi, lo;
3044 : 0 : scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3045 : 0 : scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3046 : 0 : if (!MEM_P (temp))
3047 : : {
3048 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3049 : 0 : emit_move_insn (mem, temp);
3050 : 0 : temp = mem;
3051 : : }
3052 : 0 : if (fmt->signbit_ro == 95)
3053 : : {
3054 : : /* Motorola, always big endian, with 16-bit gap in between
3055 : : 16-bit sign+exponent and 64-bit mantissa. */
3056 : 0 : ex = adjust_address (temp, iemode, 0);
3057 : 0 : hi = adjust_address (temp, imode, 4);
3058 : 0 : lo = adjust_address (temp, imode, 8);
3059 : : }
3060 : 0 : else if (!WORDS_BIG_ENDIAN)
3061 : : {
3062 : : /* Intel little endian, 64-bit mantissa followed by 16-bit
3063 : : sign+exponent and then either 16 or 48 bits of gap. */
3064 : 0 : ex = adjust_address (temp, iemode, 8);
3065 : 0 : hi = adjust_address (temp, imode, 4);
3066 : 0 : lo = adjust_address (temp, imode, 0);
3067 : : }
3068 : : else
3069 : : {
3070 : : /* Big endian Itanium. */
3071 : : ex = adjust_address (temp, iemode, 0);
3072 : : hi = adjust_address (temp, imode, 2);
3073 : : lo = adjust_address (temp, imode, 6);
3074 : : }
3075 : 0 : rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3076 : 0 : gcc_assert (fmt->qnan_msb_set);
3077 : 0 : rtx mask = GEN_INT (0x7fff);
3078 : 0 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3079 : : /* For Intel/Motorola extended format, expand to:
3080 : : (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3081 : 0 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3082 : 0 : lo = expand_binop (imode, ior_optab, lo, nlo,
3083 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3084 : 0 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3085 : 0 : temp = expand_binop (imode, xor_optab, hi, bit,
3086 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3087 : 0 : temp = expand_binop (imode, ior_optab, temp, lo,
3088 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3089 : 0 : temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3090 : 0 : ex = expand_binop (iemode, and_optab, ex, mask,
3091 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3092 : 0 : ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3093 : : ex, mask, iemode, 1, 1);
3094 : 0 : temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3095 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3096 : : }
3097 : 0 : break;
3098 : 0 : default:
3099 : 0 : gcc_unreachable ();
3100 : : }
3101 : :
3102 : : return temp;
3103 : : }
3104 : :
3105 : : /* Expand a call to one of the builtin rounding functions gcc defines
3106 : : as an extension (lfloor and lceil). As these are gcc extensions we
3107 : : do not need to worry about setting errno to EDOM.
3108 : : If expanding via optab fails, lower expression to (int)(floor(x)).
3109 : : EXP is the expression that is a call to the builtin function;
3110 : : if convenient, the result should be placed in TARGET. */
3111 : :
3112 : : static rtx
3113 : 231 : expand_builtin_int_roundingfn (tree exp, rtx target)
3114 : : {
3115 : 231 : convert_optab builtin_optab;
3116 : 231 : rtx op0, tmp;
3117 : 231 : rtx_insn *insns;
3118 : 231 : tree fndecl = get_callee_fndecl (exp);
3119 : 231 : enum built_in_function fallback_fn;
3120 : 231 : tree fallback_fndecl;
3121 : 231 : machine_mode mode;
3122 : 231 : tree arg;
3123 : :
3124 : 231 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3125 : : return NULL_RTX;
3126 : :
3127 : 231 : arg = CALL_EXPR_ARG (exp, 0);
3128 : :
3129 : 231 : switch (DECL_FUNCTION_CODE (fndecl))
3130 : : {
3131 : : CASE_FLT_FN (BUILT_IN_ICEIL):
3132 : : CASE_FLT_FN (BUILT_IN_LCEIL):
3133 : : CASE_FLT_FN (BUILT_IN_LLCEIL):
3134 : : builtin_optab = lceil_optab;
3135 : : fallback_fn = BUILT_IN_CEIL;
3136 : : break;
3137 : :
3138 : 116 : CASE_FLT_FN (BUILT_IN_IFLOOR):
3139 : 116 : CASE_FLT_FN (BUILT_IN_LFLOOR):
3140 : 116 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
3141 : 116 : builtin_optab = lfloor_optab;
3142 : 116 : fallback_fn = BUILT_IN_FLOOR;
3143 : 116 : break;
3144 : :
3145 : 0 : default:
3146 : 0 : gcc_unreachable ();
3147 : : }
3148 : :
3149 : : /* Make a suitable register to place result in. */
3150 : 231 : mode = TYPE_MODE (TREE_TYPE (exp));
3151 : :
3152 : 231 : target = gen_reg_rtx (mode);
3153 : :
3154 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3155 : : need to expand the argument again. This way, we will not perform
3156 : : side-effects more the once. */
3157 : 231 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3158 : :
3159 : 231 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3160 : :
3161 : 231 : start_sequence ();
3162 : :
3163 : : /* Compute into TARGET. */
3164 : 231 : if (expand_sfix_optab (target, op0, builtin_optab))
3165 : : {
3166 : : /* Output the entire sequence. */
3167 : 40 : insns = get_insns ();
3168 : 40 : end_sequence ();
3169 : 40 : emit_insn (insns);
3170 : 40 : return target;
3171 : : }
3172 : :
3173 : : /* If we were unable to expand via the builtin, stop the sequence
3174 : : (without outputting the insns). */
3175 : 191 : end_sequence ();
3176 : :
3177 : : /* Fall back to floating point rounding optab. */
3178 : 191 : fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3179 : :
3180 : : /* For non-C99 targets we may end up without a fallback fndecl here
3181 : : if the user called __builtin_lfloor directly. In this case emit
3182 : : a call to the floor/ceil variants nevertheless. This should result
3183 : : in the best user experience for not full C99 targets. */
3184 : 191 : if (fallback_fndecl == NULL_TREE)
3185 : : {
3186 : 0 : tree fntype;
3187 : 0 : const char *name = NULL;
3188 : :
3189 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
3190 : : {
3191 : : case BUILT_IN_ICEIL:
3192 : : case BUILT_IN_LCEIL:
3193 : : case BUILT_IN_LLCEIL:
3194 : : name = "ceil";
3195 : : break;
3196 : 0 : case BUILT_IN_ICEILF:
3197 : 0 : case BUILT_IN_LCEILF:
3198 : 0 : case BUILT_IN_LLCEILF:
3199 : 0 : name = "ceilf";
3200 : 0 : break;
3201 : 0 : case BUILT_IN_ICEILL:
3202 : 0 : case BUILT_IN_LCEILL:
3203 : 0 : case BUILT_IN_LLCEILL:
3204 : 0 : name = "ceill";
3205 : 0 : break;
3206 : 0 : case BUILT_IN_IFLOOR:
3207 : 0 : case BUILT_IN_LFLOOR:
3208 : 0 : case BUILT_IN_LLFLOOR:
3209 : 0 : name = "floor";
3210 : 0 : break;
3211 : 0 : case BUILT_IN_IFLOORF:
3212 : 0 : case BUILT_IN_LFLOORF:
3213 : 0 : case BUILT_IN_LLFLOORF:
3214 : 0 : name = "floorf";
3215 : 0 : break;
3216 : 0 : case BUILT_IN_IFLOORL:
3217 : 0 : case BUILT_IN_LFLOORL:
3218 : 0 : case BUILT_IN_LLFLOORL:
3219 : 0 : name = "floorl";
3220 : 0 : break;
3221 : 0 : default:
3222 : 0 : gcc_unreachable ();
3223 : : }
3224 : :
3225 : 0 : fntype = build_function_type_list (TREE_TYPE (arg),
3226 : 0 : TREE_TYPE (arg), NULL_TREE);
3227 : 0 : fallback_fndecl = build_fn_decl (name, fntype);
3228 : : }
3229 : :
3230 : 191 : exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3231 : :
3232 : 191 : tmp = expand_normal (exp);
3233 : 191 : tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3234 : :
3235 : : /* Truncate the result of floating point optab to integer
3236 : : via expand_fix (). */
3237 : 191 : target = gen_reg_rtx (mode);
3238 : 191 : expand_fix (target, tmp, 0);
3239 : :
3240 : 191 : return target;
3241 : : }
3242 : :
3243 : : /* Expand a call to one of the builtin math functions doing integer
3244 : : conversion (lrint).
3245 : : Return 0 if a normal call should be emitted rather than expanding the
3246 : : function in-line. EXP is the expression that is a call to the builtin
3247 : : function; if convenient, the result should be placed in TARGET. */
3248 : :
3249 : : static rtx
3250 : 590 : expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3251 : : {
3252 : 590 : convert_optab builtin_optab;
3253 : 590 : rtx op0;
3254 : 590 : rtx_insn *insns;
3255 : 590 : tree fndecl = get_callee_fndecl (exp);
3256 : 590 : tree arg;
3257 : 590 : machine_mode mode;
3258 : 590 : enum built_in_function fallback_fn = BUILT_IN_NONE;
3259 : :
3260 : 590 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3261 : : return NULL_RTX;
3262 : :
3263 : 490 : arg = CALL_EXPR_ARG (exp, 0);
3264 : :
3265 : 490 : switch (DECL_FUNCTION_CODE (fndecl))
3266 : : {
3267 : 8 : CASE_FLT_FN (BUILT_IN_IRINT):
3268 : 8 : fallback_fn = BUILT_IN_LRINT;
3269 : : gcc_fallthrough ();
3270 : : CASE_FLT_FN (BUILT_IN_LRINT):
3271 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3272 : : builtin_optab = lrint_optab;
3273 : : break;
3274 : :
3275 : 221 : CASE_FLT_FN (BUILT_IN_IROUND):
3276 : 221 : fallback_fn = BUILT_IN_LROUND;
3277 : : gcc_fallthrough ();
3278 : : CASE_FLT_FN (BUILT_IN_LROUND):
3279 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3280 : : builtin_optab = lround_optab;
3281 : : break;
3282 : :
3283 : 0 : default:
3284 : 0 : gcc_unreachable ();
3285 : : }
3286 : :
3287 : : /* There's no easy way to detect the case we need to set EDOM. */
3288 : 490 : if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3289 : : return NULL_RTX;
3290 : :
3291 : : /* Make a suitable register to place result in. */
3292 : 259 : mode = TYPE_MODE (TREE_TYPE (exp));
3293 : :
3294 : : /* There's no easy way to detect the case we need to set EDOM. */
3295 : 259 : if (!flag_errno_math)
3296 : : {
3297 : 259 : rtx result = gen_reg_rtx (mode);
3298 : :
3299 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3300 : : need to expand the argument again. This way, we will not perform
3301 : : side-effects more the once. */
3302 : 259 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3303 : :
3304 : 259 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3305 : :
3306 : 259 : start_sequence ();
3307 : :
3308 : 259 : if (expand_sfix_optab (result, op0, builtin_optab))
3309 : : {
3310 : : /* Output the entire sequence. */
3311 : 24 : insns = get_insns ();
3312 : 24 : 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 : 312 : expand_builtin_powi (tree exp, rtx target)
3364 : : {
3365 : 312 : tree arg0, arg1;
3366 : 312 : rtx op0, op1;
3367 : 312 : machine_mode mode;
3368 : 312 : machine_mode mode2;
3369 : :
3370 : 312 : if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3371 : : return NULL_RTX;
3372 : :
3373 : 312 : arg0 = CALL_EXPR_ARG (exp, 0);
3374 : 312 : arg1 = CALL_EXPR_ARG (exp, 1);
3375 : 312 : 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 : 312 : mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3381 : :
3382 : 312 : if (target == NULL_RTX)
3383 : 0 : target = gen_reg_rtx (mode);
3384 : :
3385 : 312 : op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3386 : 312 : if (GET_MODE (op0) != mode)
3387 : 0 : op0 = convert_to_mode (mode, op0, 0);
3388 : 312 : op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3389 : 312 : if (GET_MODE (op1) != mode2)
3390 : 39 : op1 = convert_to_mode (mode2, op1, 0);
3391 : :
3392 : 312 : target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3393 : : target, LCT_CONST, mode,
3394 : : op0, mode, op1, mode2);
3395 : :
3396 : 312 : 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 : 13507 : expand_builtin_strlen (tree exp, rtx target,
3405 : : machine_mode target_mode)
3406 : : {
3407 : 13507 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3408 : : return NULL_RTX;
3409 : :
3410 : 13487 : tree src = CALL_EXPR_ARG (exp, 0);
3411 : :
3412 : : /* If the length can be computed at compile-time, return it. */
3413 : 13487 : if (tree len = c_strlen (src, 0))
3414 : 21 : 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 : 13466 : tree len = c_strlen (src, 1);
3422 : 13466 : 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 : 13466 : 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 : 13466 : 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 : 13466 : FOR_EACH_MODE_FROM (insn_mode, target_mode)
3438 : : {
3439 : 13466 : icode = optab_handler (strlen_optab, insn_mode);
3440 : 13466 : if (icode != CODE_FOR_nothing)
3441 : : break;
3442 : : }
3443 : 13466 : 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 : 13466 : 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 : 13466 : rtx_insn *before_strlen = get_last_insn ();
3454 : :
3455 : 13466 : class expand_operand ops[4];
3456 : 13466 : create_output_operand (&ops[0], target, insn_mode);
3457 : 13466 : create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3458 : 13466 : create_integer_operand (&ops[2], 0);
3459 : 13466 : create_integer_operand (&ops[3], align);
3460 : 13466 : 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 : 11 : rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3471 : 11 : if (pat != src_reg)
3472 : : {
3473 : : #ifdef POINTERS_EXTEND_UNSIGNED
3474 : 11 : 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 = get_insns ();
3481 : 11 : end_sequence ();
3482 : :
3483 : 11 : if (before_strlen)
3484 : 11 : emit_insn_after (pat, before_strlen);
3485 : : else
3486 : 0 : emit_insn_before (pat, get_insns ());
3487 : :
3488 : : /* Return the value in the proper mode for this function. */
3489 : 11 : if (GET_MODE (ops[0].value) == target_mode)
3490 : : target = ops[0].value;
3491 : 0 : else if (target != 0)
3492 : 0 : convert_move (target, ops[0].value, 0);
3493 : : else
3494 : 0 : target = convert_to_mode (target_mode, ops[0].value, 0);
3495 : :
3496 : : return target;
3497 : : }
3498 : :
3499 : : /* Expand call EXP to the strnlen built-in, returning the result
3500 : : and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3501 : :
3502 : : static rtx
3503 : 580 : expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3504 : : {
3505 : 580 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3506 : : return NULL_RTX;
3507 : :
3508 : 571 : tree src = CALL_EXPR_ARG (exp, 0);
3509 : 571 : tree bound = CALL_EXPR_ARG (exp, 1);
3510 : :
3511 : 571 : if (!bound)
3512 : : return NULL_RTX;
3513 : :
3514 : 571 : location_t loc = UNKNOWN_LOCATION;
3515 : 571 : if (EXPR_HAS_LOCATION (exp))
3516 : 571 : loc = EXPR_LOCATION (exp);
3517 : :
3518 : : /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3519 : : so these conversions aren't necessary. */
3520 : 571 : c_strlen_data lendata = { };
3521 : 571 : tree len = c_strlen (src, 0, &lendata, 1);
3522 : 571 : if (len)
3523 : 102 : len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3524 : :
3525 : 571 : if (TREE_CODE (bound) == INTEGER_CST)
3526 : : {
3527 : 331 : if (!len)
3528 : : return NULL_RTX;
3529 : :
3530 : 62 : len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3531 : 62 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3532 : : }
3533 : :
3534 : 240 : if (TREE_CODE (bound) != SSA_NAME)
3535 : : return NULL_RTX;
3536 : :
3537 : 240 : wide_int min, max;
3538 : 240 : int_range_max r;
3539 : 240 : get_global_range_query ()->range_of_expr (r, bound);
3540 : 240 : if (r.varying_p () || r.undefined_p ())
3541 : : return NULL_RTX;
3542 : 132 : min = r.lower_bound ();
3543 : 132 : max = r.upper_bound ();
3544 : :
3545 : 132 : 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 : 57 : if (lendata.decl)
3554 : : return NULL_RTX;
3555 : :
3556 : 37 : if (wi::gtu_p (min, wi::to_wide (len)))
3557 : 7 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3558 : :
3559 : 30 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3560 : 30 : 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 : 144454 : 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 : 144454 : const char *rep = (const char *) data;
3574 : :
3575 : 144454 : 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 : 131511 : 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 : 131511 : if (CONST_INT_P (len_rtx))
3590 : : {
3591 : 60129 : *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3592 : 60129 : return;
3593 : : }
3594 : : else
3595 : : {
3596 : 71382 : wide_int min, max;
3597 : 71382 : enum value_range_kind range_type = VR_UNDEFINED;
3598 : :
3599 : : /* Determine bounds from the type. */
3600 : 71382 : if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3601 : 71381 : *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3602 : : else
3603 : 1 : *min_size = 0;
3604 : 71382 : if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3605 : 142764 : *probable_max_size = *max_size
3606 : 71382 : = 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 : 71382 : if (TREE_CODE (len) == SSA_NAME)
3611 : : {
3612 : 71382 : int_range_max r;
3613 : 71382 : tree tmin, tmax;
3614 : 71382 : get_global_range_query ()->range_of_expr (r, len);
3615 : 71382 : range_type = get_legacy_range (r, tmin, tmax);
3616 : 71382 : if (range_type != VR_UNDEFINED)
3617 : : {
3618 : 71382 : min = wi::to_wide (tmin);
3619 : 71382 : max = wi::to_wide (tmax);
3620 : : }
3621 : 71382 : }
3622 : 71382 : if (range_type == VR_RANGE)
3623 : : {
3624 : 36426 : if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3625 : 21750 : *min_size = min.to_uhwi ();
3626 : 36426 : if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3627 : 32511 : *probable_max_size = *max_size = max.to_uhwi ();
3628 : : }
3629 : 34956 : else if (range_type == VR_ANTI_RANGE)
3630 : : {
3631 : : /* Code like
3632 : :
3633 : : int n;
3634 : : if (n < 100)
3635 : : memcpy (a, b, n)
3636 : :
3637 : : Produce anti range allowing negative values of N. We still
3638 : : can use the information and make a guess that N is not negative.
3639 : : */
3640 : 6344 : if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3641 : 3344 : *probable_max_size = min.to_uhwi () - 1;
3642 : : }
3643 : 71382 : }
3644 : 71382 : gcc_checking_assert (*max_size <=
3645 : : (unsigned HOST_WIDE_INT)
3646 : : GET_MODE_MASK (GET_MODE (len_rtx)));
3647 : : }
3648 : :
3649 : : /* Expand a call EXP to the memcpy builtin.
3650 : : Return NULL_RTX if we failed, the caller should emit a normal call,
3651 : : otherwise try to get the result in TARGET, if convenient (and in
3652 : : mode MODE if that's convenient). */
3653 : :
3654 : : static rtx
3655 : 81107 : expand_builtin_memcpy (tree exp, rtx target)
3656 : : {
3657 : 81107 : if (!validate_arglist (exp,
3658 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3659 : : return NULL_RTX;
3660 : :
3661 : 81089 : tree dest = CALL_EXPR_ARG (exp, 0);
3662 : 81089 : tree src = CALL_EXPR_ARG (exp, 1);
3663 : 81089 : tree len = CALL_EXPR_ARG (exp, 2);
3664 : :
3665 : 81089 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3666 : 81089 : /*retmode=*/ RETURN_BEGIN, false);
3667 : : }
3668 : :
3669 : : /* Check a call EXP to the memmove built-in for validity.
3670 : : Return NULL_RTX on both success and failure. */
3671 : :
3672 : : static rtx
3673 : 15038 : expand_builtin_memmove (tree exp, rtx target)
3674 : : {
3675 : 15038 : if (!validate_arglist (exp,
3676 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3677 : : return NULL_RTX;
3678 : :
3679 : 15023 : tree dest = CALL_EXPR_ARG (exp, 0);
3680 : 15023 : tree src = CALL_EXPR_ARG (exp, 1);
3681 : 15023 : tree len = CALL_EXPR_ARG (exp, 2);
3682 : :
3683 : 15023 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3684 : 15023 : /*retmode=*/ RETURN_BEGIN, true);
3685 : : }
3686 : :
3687 : : /* Expand a call EXP to the mempcpy builtin.
3688 : : Return NULL_RTX if we failed; the caller should emit a normal call,
3689 : : otherwise try to get the result in TARGET, if convenient (and in
3690 : : mode MODE if that's convenient). */
3691 : :
3692 : : static rtx
3693 : 1637 : expand_builtin_mempcpy (tree exp, rtx target)
3694 : : {
3695 : 1637 : if (!validate_arglist (exp,
3696 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3697 : : return NULL_RTX;
3698 : :
3699 : 1632 : tree dest = CALL_EXPR_ARG (exp, 0);
3700 : 1632 : tree src = CALL_EXPR_ARG (exp, 1);
3701 : 1632 : tree len = CALL_EXPR_ARG (exp, 2);
3702 : :
3703 : : /* Policy does not generally allow using compute_objsize (which
3704 : : is used internally by check_memop_size) to change code generation
3705 : : or drive optimization decisions.
3706 : :
3707 : : In this instance it is safe because the code we generate has
3708 : : the same semantics regardless of the return value of
3709 : : check_memop_sizes. Exactly the same amount of data is copied
3710 : : and the return value is exactly the same in both cases.
3711 : :
3712 : : Furthermore, check_memop_size always uses mode 0 for the call to
3713 : : compute_objsize, so the imprecise nature of compute_objsize is
3714 : : avoided. */
3715 : :
3716 : : /* Avoid expanding mempcpy into memcpy when the call is determined
3717 : : to overflow the buffer. This also prevents the same overflow
3718 : : from being diagnosed again when expanding memcpy. */
3719 : :
3720 : 1632 : return expand_builtin_mempcpy_args (dest, src, len,
3721 : 1632 : target, exp, /*retmode=*/ RETURN_END);
3722 : : }
3723 : :
3724 : : /* Helper function to do the actual work for expand of memory copy family
3725 : : functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3726 : : of memory from SRC to DEST and assign to TARGET if convenient. Return
3727 : : value is based on RETMODE argument. */
3728 : :
3729 : : static rtx
3730 : 97767 : expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3731 : : rtx target, tree exp, memop_ret retmode,
3732 : : bool might_overlap)
3733 : : {
3734 : 97767 : unsigned int src_align = get_pointer_alignment (src);
3735 : 97767 : unsigned int dest_align = get_pointer_alignment (dest);
3736 : 97767 : rtx dest_mem, src_mem, dest_addr, len_rtx;
3737 : 97767 : HOST_WIDE_INT expected_size = -1;
3738 : 97767 : unsigned int expected_align = 0;
3739 : 97767 : unsigned HOST_WIDE_INT min_size;
3740 : 97767 : unsigned HOST_WIDE_INT max_size;
3741 : 97767 : unsigned HOST_WIDE_INT probable_max_size;
3742 : :
3743 : 97767 : bool is_move_done;
3744 : :
3745 : : /* If DEST is not a pointer type, call the normal function. */
3746 : 97767 : if (dest_align == 0)
3747 : : return NULL_RTX;
3748 : :
3749 : : /* If either SRC is not a pointer type, don't do this
3750 : : operation in-line. */
3751 : 97767 : if (src_align == 0)
3752 : : return NULL_RTX;
3753 : :
3754 : 97767 : if (currently_expanding_gimple_stmt)
3755 : 97767 : stringop_block_profile (currently_expanding_gimple_stmt,
3756 : : &expected_align, &expected_size);
3757 : :
3758 : 97767 : if (expected_align < dest_align)
3759 : 97755 : expected_align = dest_align;
3760 : 97767 : dest_mem = get_memory_rtx (dest, len);
3761 : 97767 : set_mem_align (dest_mem, dest_align);
3762 : 97767 : len_rtx = expand_normal (len);
3763 : 97767 : determine_block_size (len, len_rtx, &min_size, &max_size,
3764 : : &probable_max_size);
3765 : :
3766 : : /* Try to get the byte representation of the constant SRC points to,
3767 : : with its byte size in NBYTES. */
3768 : 97767 : unsigned HOST_WIDE_INT nbytes;
3769 : 97767 : const char *rep = getbyterep (src, &nbytes);
3770 : :
3771 : : /* If the function's constant bound LEN_RTX is less than or equal
3772 : : to the byte size of the representation of the constant argument,
3773 : : and if block move would be done by pieces, we can avoid loading
3774 : : the bytes from memory and only store the computed constant.
3775 : : This works in the overlap (memmove) case as well because
3776 : : store_by_pieces just generates a series of stores of constants
3777 : : from the representation returned by getbyterep(). */
3778 : 97767 : if (rep
3779 : 25739 : && CONST_INT_P (len_rtx)
3780 : 24228 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3781 : 121995 : && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3782 : : CONST_CAST (char *, rep),
3783 : : dest_align, false))
3784 : : {
3785 : 23799 : dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3786 : : builtin_memcpy_read_str,
3787 : : CONST_CAST (char *, rep),
3788 : : dest_align, false, retmode);
3789 : 23799 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
3790 : 23799 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
3791 : 23799 : return dest_mem;
3792 : : }
3793 : :
3794 : 73968 : src_mem = get_memory_rtx (src, len);
3795 : 73968 : set_mem_align (src_mem, src_align);
3796 : :
3797 : : /* Copy word part most expediently. */
3798 : 73968 : enum block_op_methods method = BLOCK_OP_NORMAL;
3799 : 73968 : if (CALL_EXPR_TAILCALL (exp)
3800 : 73968 : && (retmode == RETURN_BEGIN || target == const0_rtx))
3801 : : method = BLOCK_OP_TAILCALL;
3802 : 73968 : bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3803 : 73968 : && retmode == RETURN_END
3804 : 73968 : && !might_overlap
3805 : 75270 : && target != const0_rtx);
3806 : : if (use_mempcpy_call)
3807 : 73968 : method = BLOCK_OP_NO_LIBCALL_RET;
3808 : 73968 : dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3809 : : expected_align, expected_size,
3810 : : min_size, max_size, probable_max_size,
3811 : : use_mempcpy_call, &is_move_done,
3812 : : might_overlap, tree_ctz (len));
3813 : :
3814 : : /* Bail out when a mempcpy call would be expanded as libcall and when
3815 : : we have a target that provides a fast implementation
3816 : : of mempcpy routine. */
3817 : 73968 : if (!is_move_done)
3818 : : return NULL_RTX;
3819 : :
3820 : 59148 : if (dest_addr == pc_rtx)
3821 : : return NULL_RTX;
3822 : :
3823 : 59148 : if (dest_addr == 0)
3824 : : {
3825 : 24183 : dest_addr = force_operand (XEXP (dest_mem, 0), target);
3826 : 24183 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
3827 : : }
3828 : :
3829 : 59148 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3830 : : {
3831 : 807 : dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3832 : : /* stpcpy pointer to last byte. */
3833 : 807 : if (retmode == RETURN_END_MINUS_ONE)
3834 : 0 : dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3835 : : }
3836 : :
3837 : : return dest_addr;
3838 : : }
3839 : :
3840 : : static rtx
3841 : 1655 : expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3842 : : rtx target, tree orig_exp, memop_ret retmode)
3843 : : {
3844 : 1632 : return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3845 : 0 : retmode, false);
3846 : : }
3847 : :
3848 : : /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3849 : : we failed, the caller should emit a normal call, otherwise try to
3850 : : get the result in TARGET, if convenient.
3851 : : Return value is based on RETMODE argument. */
3852 : :
3853 : : static rtx
3854 : 2445 : expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3855 : : {
3856 : 2445 : class expand_operand ops[3];
3857 : 2445 : rtx dest_mem;
3858 : 2445 : rtx src_mem;
3859 : :
3860 : 2445 : if (!targetm.have_movstr ())
3861 : : return NULL_RTX;
3862 : :
3863 : 0 : dest_mem = get_memory_rtx (dest, NULL);
3864 : 0 : src_mem = get_memory_rtx (src, NULL);
3865 : 0 : if (retmode == RETURN_BEGIN)
3866 : : {
3867 : 0 : target = force_reg (Pmode, XEXP (dest_mem, 0));
3868 : 0 : dest_mem = replace_equiv_address (dest_mem, target);
3869 : : }
3870 : :
3871 : 0 : create_output_operand (&ops[0],
3872 : 0 : retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3873 : 0 : create_fixed_operand (&ops[1], dest_mem);
3874 : 0 : create_fixed_operand (&ops[2], src_mem);
3875 : 0 : if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3876 : : return NULL_RTX;
3877 : :
3878 : 0 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3879 : : {
3880 : 0 : target = ops[0].value;
3881 : : /* movstr is supposed to set end to the address of the NUL
3882 : : terminator. If the caller requested a mempcpy-like return value,
3883 : : adjust it. */
3884 : 0 : if (retmode == RETURN_END)
3885 : : {
3886 : 0 : rtx tem = plus_constant (GET_MODE (target),
3887 : 0 : gen_lowpart (GET_MODE (target), target), 1);
3888 : 0 : emit_move_insn (target, force_operand (tem, NULL_RTX));
3889 : : }
3890 : : }
3891 : : return target;
3892 : : }
3893 : :
3894 : : /* Expand expression EXP, which is a call to the strcpy builtin. Return
3895 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
3896 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
3897 : : convenient). */
3898 : :
3899 : : static rtx
3900 : 1863 : expand_builtin_strcpy (tree exp, rtx target)
3901 : : {
3902 : 1863 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3903 : : return NULL_RTX;
3904 : :
3905 : 1848 : tree dest = CALL_EXPR_ARG (exp, 0);
3906 : 1848 : tree src = CALL_EXPR_ARG (exp, 1);
3907 : :
3908 : 1848 : return expand_builtin_strcpy_args (exp, dest, src, target);
3909 : : }
3910 : :
3911 : : /* Helper function to do the actual work for expand_builtin_strcpy. The
3912 : : arguments to the builtin_strcpy call DEST and SRC are broken out
3913 : : so that this can also be called without constructing an actual CALL_EXPR.
3914 : : The other arguments and return value are the same as for
3915 : : expand_builtin_strcpy. */
3916 : :
3917 : : static rtx
3918 : 1848 : expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3919 : : {
3920 : 1848 : return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3921 : : }
3922 : :
3923 : : /* Expand a call EXP to the stpcpy builtin.
3924 : : Return NULL_RTX if we failed the caller should emit a normal call,
3925 : : otherwise try to get the result in TARGET, if convenient (and in
3926 : : mode MODE if that's convenient). */
3927 : :
3928 : : static rtx
3929 : 629 : expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3930 : : {
3931 : 629 : tree dst, src;
3932 : 629 : location_t loc = EXPR_LOCATION (exp);
3933 : :
3934 : 629 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3935 : : return NULL_RTX;
3936 : :
3937 : 620 : dst = CALL_EXPR_ARG (exp, 0);
3938 : 620 : src = CALL_EXPR_ARG (exp, 1);
3939 : :
3940 : : /* If return value is ignored, transform stpcpy into strcpy. */
3941 : 620 : if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3942 : : {
3943 : 0 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3944 : 0 : tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3945 : 0 : return expand_expr (result, target, mode, EXPAND_NORMAL);
3946 : : }
3947 : : else
3948 : : {
3949 : 620 : tree len, lenp1;
3950 : 620 : rtx ret;
3951 : :
3952 : : /* Ensure we get an actual string whose length can be evaluated at
3953 : : compile-time, not an expression containing a string. This is
3954 : : because the latter will potentially produce pessimized code
3955 : : when used to produce the return value. */
3956 : 620 : c_strlen_data lendata = { };
3957 : 620 : if (!c_getstr (src)
3958 : 620 : || !(len = c_strlen (src, 0, &lendata, 1)))
3959 : 597 : return expand_movstr (dst, src, target,
3960 : 597 : /*retmode=*/ RETURN_END_MINUS_ONE);
3961 : :
3962 : 23 : lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3963 : 23 : ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3964 : : target, exp,
3965 : : /*retmode=*/ RETURN_END_MINUS_ONE);
3966 : :
3967 : 23 : if (ret)
3968 : : return ret;
3969 : :
3970 : 0 : if (TREE_CODE (len) == INTEGER_CST)
3971 : : {
3972 : 0 : rtx len_rtx = expand_normal (len);
3973 : :
3974 : 0 : if (CONST_INT_P (len_rtx))
3975 : : {
3976 : 0 : ret = expand_builtin_strcpy_args (exp, dst, src, target);
3977 : :
3978 : 0 : if (ret)
3979 : : {
3980 : 0 : if (! target)
3981 : : {
3982 : 0 : if (mode != VOIDmode)
3983 : 0 : target = gen_reg_rtx (mode);
3984 : : else
3985 : 0 : target = gen_reg_rtx (GET_MODE (ret));
3986 : : }
3987 : 0 : if (GET_MODE (target) != GET_MODE (ret))
3988 : 0 : ret = gen_lowpart (GET_MODE (target), ret);
3989 : :
3990 : 0 : ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3991 : 0 : ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3992 : 0 : gcc_assert (ret);
3993 : :
3994 : : return target;
3995 : : }
3996 : : }
3997 : : }
3998 : :
3999 : 0 : return expand_movstr (dst, src, target,
4000 : 0 : /*retmode=*/ RETURN_END_MINUS_ONE);
4001 : : }
4002 : : }
4003 : :
4004 : : /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
4005 : : arguments while being careful to avoid duplicate warnings (which could
4006 : : be issued if the expander were to expand the call, resulting in it
4007 : : being emitted in expand_call(). */
4008 : :
4009 : : static rtx
4010 : 629 : expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
4011 : : {
4012 : 629 : if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
4013 : : {
4014 : : /* The call has been successfully expanded. Check for nonstring
4015 : : arguments and issue warnings as appropriate. */
4016 : 23 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
4017 : 23 : return ret;
4018 : : }
4019 : :
4020 : : return NULL_RTX;
4021 : : }
4022 : :
4023 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4024 : : bytes from constant string DATA + OFFSET and return it as target
4025 : : constant. */
4026 : :
4027 : : rtx
4028 : 9212 : builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
4029 : : fixed_size_mode mode)
4030 : : {
4031 : 9212 : const char *str = (const char *) data;
4032 : :
4033 : 9212 : if ((unsigned HOST_WIDE_INT) offset > strlen (str))
4034 : 218 : return const0_rtx;
4035 : :
4036 : 8994 : return c_readstr (str + offset, mode);
4037 : : }
4038 : :
4039 : : /* Helper to check the sizes of sequences and the destination of calls
4040 : : to __builtin_strncat and __builtin___strncat_chk. Returns true on
4041 : : success (no overflow or invalid sizes), false otherwise. */
4042 : :
4043 : : static bool
4044 : 109 : check_strncat_sizes (tree exp, tree objsize)
4045 : : {
4046 : 109 : tree dest = CALL_EXPR_ARG (exp, 0);
4047 : 109 : tree src = CALL_EXPR_ARG (exp, 1);
4048 : 109 : tree maxread = CALL_EXPR_ARG (exp, 2);
4049 : :
4050 : : /* Try to determine the range of lengths that the source expression
4051 : : refers to. */
4052 : 109 : c_strlen_data lendata = { };
4053 : 109 : get_range_strlen (src, &lendata, /* eltsize = */ 1);
4054 : :
4055 : : /* Try to verify that the destination is big enough for the shortest
4056 : : string. */
4057 : :
4058 : 109 : access_data data (nullptr, exp, access_read_write, maxread, true);
4059 : 109 : if (!objsize && warn_stringop_overflow)
4060 : : {
4061 : : /* If it hasn't been provided by __strncat_chk, try to determine
4062 : : the size of the destination object into which the source is
4063 : : being copied. */
4064 : 0 : objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4065 : : }
4066 : :
4067 : : /* Add one for the terminating nul. */
4068 : 109 : tree srclen = (lendata.minlen
4069 : 109 : ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4070 : : size_one_node)
4071 : 109 : : NULL_TREE);
4072 : :
4073 : : /* The strncat function copies at most MAXREAD bytes and always appends
4074 : : the terminating nul so the specified upper bound should never be equal
4075 : : to (or greater than) the size of the destination. */
4076 : 56 : if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4077 : 157 : && tree_int_cst_equal (objsize, maxread))
4078 : : {
4079 : 23 : location_t loc = EXPR_LOCATION (exp);
4080 : 23 : warning_at (loc, OPT_Wstringop_overflow_,
4081 : : "%qD specified bound %E equals destination size",
4082 : : get_callee_fndecl (exp), maxread);
4083 : :
4084 : 23 : return false;
4085 : : }
4086 : :
4087 : 86 : if (!srclen
4088 : 86 : || (maxread && tree_fits_uhwi_p (maxread)
4089 : 33 : && tree_fits_uhwi_p (srclen)
4090 : 33 : && tree_int_cst_lt (maxread, srclen)))
4091 : : srclen = maxread;
4092 : :
4093 : : /* The number of bytes to write is LEN but check_access will alsoa
4094 : : check SRCLEN if LEN's value isn't known. */
4095 : 86 : return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4096 : 86 : objsize, data.mode, &data);
4097 : : }
4098 : :
4099 : : /* Expand expression EXP, which is a call to the strncpy builtin. Return
4100 : : NULL_RTX if we failed the caller should emit a normal call. */
4101 : :
4102 : : static rtx
4103 : 2055 : expand_builtin_strncpy (tree exp, rtx target)
4104 : : {
4105 : 2055 : location_t loc = EXPR_LOCATION (exp);
4106 : :
4107 : 2055 : if (!validate_arglist (exp,
4108 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4109 : : return NULL_RTX;
4110 : 2043 : tree dest = CALL_EXPR_ARG (exp, 0);
4111 : 2043 : tree src = CALL_EXPR_ARG (exp, 1);
4112 : : /* The number of bytes to write (not the maximum). */
4113 : 2043 : tree len = CALL_EXPR_ARG (exp, 2);
4114 : :
4115 : : /* The length of the source sequence. */
4116 : 2043 : tree slen = c_strlen (src, 1);
4117 : :
4118 : : /* We must be passed a constant len and src parameter. */
4119 : 2043 : if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4120 : : return NULL_RTX;
4121 : :
4122 : 131 : slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4123 : :
4124 : : /* We're required to pad with trailing zeros if the requested
4125 : : len is greater than strlen(s2)+1. In that case try to
4126 : : use store_by_pieces, if it fails, punt. */
4127 : 131 : if (tree_int_cst_lt (slen, len))
4128 : : {
4129 : 120 : unsigned int dest_align = get_pointer_alignment (dest);
4130 : 120 : const char *p = c_getstr (src);
4131 : 120 : rtx dest_mem;
4132 : :
4133 : 114 : if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4134 : 234 : || !can_store_by_pieces (tree_to_uhwi (len),
4135 : : builtin_strncpy_read_str,
4136 : : CONST_CAST (char *, p),
4137 : : dest_align, false))
4138 : 14 : return NULL_RTX;
4139 : :
4140 : 106 : dest_mem = get_memory_rtx (dest, len);
4141 : 106 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4142 : : builtin_strncpy_read_str,
4143 : : CONST_CAST (char *, p), dest_align, false,
4144 : : RETURN_BEGIN);
4145 : 106 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
4146 : 106 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4147 : 106 : return dest_mem;
4148 : : }
4149 : :
4150 : : return NULL_RTX;
4151 : : }
4152 : :
4153 : : /* Return the RTL of a register in MODE generated from PREV in the
4154 : : previous iteration. */
4155 : :
4156 : : static rtx
4157 : 152331 : gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4158 : : {
4159 : 152331 : rtx target = nullptr;
4160 : 152331 : if (prev != nullptr && prev->data != nullptr)
4161 : : {
4162 : : /* Use the previous data in the same mode. */
4163 : 78745 : if (prev->mode == mode)
4164 : 152331 : return prev->data;
4165 : :
4166 : 25146 : fixed_size_mode prev_mode = prev->mode;
4167 : :
4168 : : /* Don't use the previous data to write QImode if it is in a
4169 : : vector mode. */
4170 : 25146 : if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4171 : : return target;
4172 : :
4173 : 24905 : rtx prev_rtx = prev->data;
4174 : :
4175 : 24905 : if (REG_P (prev_rtx)
4176 : 19191 : && HARD_REGISTER_P (prev_rtx)
4177 : 24905 : && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4178 : : {
4179 : : /* This case occurs when PREV_MODE is a vector and when
4180 : : MODE is too small to store using vector operations.
4181 : : After register allocation, the code will need to move the
4182 : : lowpart of the vector register into a non-vector register.
4183 : :
4184 : : Also, the target has chosen to use a hard register
4185 : : instead of going with the default choice of using a
4186 : : pseudo register. We should respect that choice and try to
4187 : : avoid creating a pseudo register with the same mode as the
4188 : : current hard register.
4189 : :
4190 : : In principle, we could just use a lowpart MODE subreg of
4191 : : the vector register. However, the vector register mode might
4192 : : be too wide for non-vector registers, and we already know
4193 : : that the non-vector mode is too small for vector registers.
4194 : : It's therefore likely that we'd need to spill to memory in
4195 : : the vector mode and reload the non-vector value from there.
4196 : :
4197 : : Try to avoid that by reducing the vector register to the
4198 : : smallest size that it can hold. This should increase the
4199 : : chances that non-vector registers can hold both the inner
4200 : : and outer modes of the subreg that we generate later. */
4201 : 0 : machine_mode m;
4202 : 0 : fixed_size_mode candidate;
4203 : 0 : FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4204 : 0 : if (is_a<fixed_size_mode> (m, &candidate))
4205 : : {
4206 : 0 : if (GET_MODE_SIZE (candidate)
4207 : 0 : >= GET_MODE_SIZE (prev_mode))
4208 : : break;
4209 : 0 : if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4210 : 0 : && lowpart_subreg_regno (REGNO (prev_rtx),
4211 : : prev_mode, candidate) >= 0)
4212 : : {
4213 : 0 : target = lowpart_subreg (candidate, prev_rtx,
4214 : : prev_mode);
4215 : 0 : prev_rtx = target;
4216 : 0 : prev_mode = candidate;
4217 : 0 : break;
4218 : : }
4219 : : }
4220 : 0 : if (target == nullptr)
4221 : 0 : prev_rtx = copy_to_reg (prev_rtx);
4222 : : }
4223 : :
4224 : 24905 : target = lowpart_subreg (mode, prev_rtx, prev_mode);
4225 : : }
4226 : : return target;
4227 : : }
4228 : :
4229 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4230 : : bytes from constant string DATA + OFFSET and return it as target
4231 : : constant. If PREV isn't nullptr, it has the RTL info from the
4232 : : previous iteration. */
4233 : :
4234 : : rtx
4235 : 151123 : builtin_memset_read_str (void *data, void *prev,
4236 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4237 : : fixed_size_mode mode)
4238 : : {
4239 : 151123 : const char *c = (const char *) data;
4240 : 151123 : unsigned int size = GET_MODE_SIZE (mode);
4241 : :
4242 : 151123 : rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4243 : : mode);
4244 : 151123 : if (target != nullptr)
4245 : : return target;
4246 : 73135 : rtx src = gen_int_mode (*c, QImode);
4247 : :
4248 : 73135 : if (VECTOR_MODE_P (mode))
4249 : : {
4250 : 102858 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4251 : :
4252 : 51429 : rtx const_vec = gen_const_vec_duplicate (mode, src);
4253 : 51429 : if (prev == NULL)
4254 : : /* Return CONST_VECTOR when called by a query function. */
4255 : : return const_vec;
4256 : :
4257 : : /* Use the move expander with CONST_VECTOR. */
4258 : 38963 : target = gen_reg_rtx (mode);
4259 : 38963 : emit_move_insn (target, const_vec);
4260 : 38963 : return target;
4261 : : }
4262 : :
4263 : 21706 : char *p = XALLOCAVEC (char, size);
4264 : :
4265 : 21706 : memset (p, *c, size);
4266 : :
4267 : 21706 : return c_readstr (p, mode);
4268 : : }
4269 : :
4270 : : /* Callback routine for store_by_pieces. Return the RTL of a register
4271 : : containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4272 : : char value given in the RTL register data. For example, if mode is
4273 : : 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4274 : : nullptr, it has the RTL info from the previous iteration. */
4275 : :
4276 : : static rtx
4277 : 1375 : builtin_memset_gen_str (void *data, void *prev,
4278 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4279 : : fixed_size_mode mode)
4280 : : {
4281 : 1375 : rtx target, coeff;
4282 : 1375 : size_t size;
4283 : 1375 : char *p;
4284 : :
4285 : 1375 : size = GET_MODE_SIZE (mode);
4286 : 1375 : if (size == 1)
4287 : : return (rtx) data;
4288 : :
4289 : 1208 : target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4290 : 1208 : if (target != nullptr)
4291 : : return target;
4292 : :
4293 : 692 : if (VECTOR_MODE_P (mode))
4294 : : {
4295 : 376 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4296 : :
4297 : : /* vec_duplicate_optab is a precondition to pick a vector mode for
4298 : : the memset expander. */
4299 : 188 : insn_code icode = optab_handler (vec_duplicate_optab, mode);
4300 : :
4301 : 188 : target = gen_reg_rtx (mode);
4302 : 188 : class expand_operand ops[2];
4303 : 188 : create_output_operand (&ops[0], target, mode);
4304 : 188 : create_input_operand (&ops[1], (rtx) data, QImode);
4305 : 188 : expand_insn (icode, 2, ops);
4306 : 188 : if (!rtx_equal_p (target, ops[0].value))
4307 : 0 : emit_move_insn (target, ops[0].value);
4308 : :
4309 : 188 : return target;
4310 : : }
4311 : :
4312 : 504 : p = XALLOCAVEC (char, size);
4313 : 504 : memset (p, 1, size);
4314 : 504 : coeff = c_readstr (p, mode);
4315 : :
4316 : 504 : target = convert_to_mode (mode, (rtx) data, 1);
4317 : 504 : target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4318 : 504 : return force_reg (mode, target);
4319 : : }
4320 : :
4321 : : /* Expand expression EXP, which is a call to the memset builtin. Return
4322 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
4323 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
4324 : : convenient). */
4325 : :
4326 : : rtx
4327 : 33753 : expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4328 : : {
4329 : 33753 : if (!validate_arglist (exp,
4330 : : POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4331 : : return NULL_RTX;
4332 : :
4333 : 33744 : tree dest = CALL_EXPR_ARG (exp, 0);
4334 : 33744 : tree val = CALL_EXPR_ARG (exp, 1);
4335 : 33744 : tree len = CALL_EXPR_ARG (exp, 2);
4336 : :
4337 : 33744 : return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4338 : : }
4339 : :
4340 : : /* Check that store_by_pieces allows BITS + LEN (so that we don't
4341 : : expand something too unreasonably long), and every power of 2 in
4342 : : BITS. It is assumed that LEN has already been tested by
4343 : : itself. */
4344 : : static bool
4345 : 13128 : can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
4346 : : by_pieces_constfn constfun,
4347 : : void *constfundata, unsigned int align,
4348 : : bool memsetp,
4349 : : unsigned HOST_WIDE_INT len)
4350 : : {
4351 : 13128 : if (bits
4352 : 13128 : && !can_store_by_pieces (bits + len, constfun, constfundata,
4353 : : align, memsetp))
4354 : : return false;
4355 : :
4356 : : /* BITS set are expected to be generally in the low range and
4357 : : contiguous. We do NOT want to repeat the test above in case BITS
4358 : : has a single bit set, so we terminate the loop when BITS == BIT.
4359 : : In the unlikely case that BITS has the MSB set, also terminate in
4360 : : case BIT gets shifted out. */
4361 : 611 : for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
4362 : : {
4363 : 541 : if ((bits & bit) == 0)
4364 : 327 : continue;
4365 : :
4366 : 214 : if (!can_store_by_pieces (bit, constfun, constfundata,
4367 : : align, memsetp))
4368 : : return false;
4369 : : }
4370 : :
4371 : : return true;
4372 : : }
4373 : :
4374 : : /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4375 : : Return TRUE if successful, FALSE otherwise. TO is assumed to be
4376 : : aligned at an ALIGN-bits boundary. LEN must be a multiple of
4377 : : 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4378 : :
4379 : : The strategy is to issue one store_by_pieces for each power of two,
4380 : : from most to least significant, guarded by a test on whether there
4381 : : are at least that many bytes left to copy in LEN.
4382 : :
4383 : : ??? Should we skip some powers of two in favor of loops? Maybe start
4384 : : at the max of TO/LEN/word alignment, at least when optimizing for
4385 : : size, instead of ensuring O(log len) dynamic compares? */
4386 : :
4387 : : bool
4388 : 11220 : try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4389 : : unsigned HOST_WIDE_INT min_len,
4390 : : unsigned HOST_WIDE_INT max_len,
4391 : : rtx val, char valc, unsigned int align)
4392 : : {
4393 : 11220 : int max_bits = floor_log2 (max_len);
4394 : 11220 : int min_bits = floor_log2 (min_len);
4395 : 11220 : int sctz_len = ctz_len;
4396 : :
4397 : 11220 : gcc_checking_assert (sctz_len >= 0);
4398 : :
4399 : 11220 : if (val)
4400 : 613 : valc = 1;
4401 : :
4402 : : /* Bits more significant than TST_BITS are part of the shared prefix
4403 : : in the binary representation of both min_len and max_len. Since
4404 : : they're identical, we don't need to test them in the loop. */
4405 : 11220 : int tst_bits = (max_bits != min_bits ? max_bits
4406 : 592 : : floor_log2 (max_len ^ min_len));
4407 : :
4408 : : /* Save the pre-blksize values. */
4409 : 11220 : int orig_max_bits = max_bits;
4410 : 11220 : int orig_tst_bits = tst_bits;
4411 : :
4412 : : /* Check whether it's profitable to start by storing a fixed BLKSIZE
4413 : : bytes, to lower max_bits. In the unlikely case of a constant LEN
4414 : : (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4415 : : single store_by_pieces, but otherwise, select the minimum multiple
4416 : : of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4417 : : brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4418 : 11220 : unsigned HOST_WIDE_INT blksize;
4419 : 11220 : if (max_len > min_len)
4420 : : {
4421 : 10641 : unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4422 : : align / BITS_PER_UNIT);
4423 : 10641 : blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4424 : 10641 : blksize &= ~(alrng - 1);
4425 : : }
4426 : 579 : else if (max_len == min_len)
4427 : : blksize = max_len;
4428 : : else
4429 : : /* Huh, max_len < min_len? Punt. See pr100843.c. */
4430 : : return false;
4431 : 11219 : if (min_len >= blksize
4432 : : /* ??? Maybe try smaller fixed-prefix blksizes before
4433 : : punting? */
4434 : 11219 : && can_store_by_pieces (blksize, builtin_memset_read_str,
4435 : : &valc, align, true))
4436 : : {
4437 : 79 : min_len -= blksize;
4438 : 79 : min_bits = floor_log2 (min_len);
4439 : 79 : max_len -= blksize;
4440 : 79 : max_bits = floor_log2 (max_len);
4441 : :
4442 : 79 : tst_bits = (max_bits != min_bits ? max_bits
4443 : 1 : : floor_log2 (max_len ^ min_len));
4444 : : }
4445 : : else
4446 : : blksize = 0;
4447 : :
4448 : : /* Check that we can use store by pieces for the maximum store count
4449 : : we may issue (initial fixed-size block, plus conditional
4450 : : power-of-two-sized from max_bits to ctz_len. */
4451 : 11219 : unsigned HOST_WIDE_INT xlenest = blksize;
4452 : 11219 : if (max_bits >= 0)
4453 : 11218 : xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4454 : 11218 : - (HOST_WIDE_INT_1U << ctz_len));
4455 : 11219 : bool max_loop = false;
4456 : 11219 : bool use_store_by_pieces = true;
4457 : : /* Skip the test in case of overflow in xlenest. It shouldn't
4458 : : happen because of the way max_bits and blksize are related, but
4459 : : it doesn't hurt to test. */
4460 : 11219 : if (blksize > xlenest
4461 : 11219 : || !can_store_by_multiple_pieces (xlenest - blksize,
4462 : : builtin_memset_read_str,
4463 : : &valc, align, true, blksize))
4464 : : {
4465 : 11218 : if (!(flag_inline_stringops & ILSOP_MEMSET))
4466 : : return false;
4467 : :
4468 : 1909 : for (max_bits = orig_max_bits;
4469 : 1978 : max_bits >= sctz_len;
4470 : : --max_bits)
4471 : : {
4472 : 1978 : xlenest = ((HOST_WIDE_INT_1U << max_bits) * 2
4473 : 1978 : - (HOST_WIDE_INT_1U << ctz_len));
4474 : : /* Check that blksize plus the bits to be stored as blocks
4475 : : sized at powers of two can be stored by pieces. This is
4476 : : like the test above, but with smaller max_bits. Skip
4477 : : orig_max_bits (it would be redundant). Also skip in case
4478 : : of overflow. */
4479 : 1978 : if (max_bits < orig_max_bits
4480 : 1909 : && xlenest + blksize >= xlenest
4481 : 3887 : && can_store_by_multiple_pieces (xlenest,
4482 : : builtin_memset_read_str,
4483 : : &valc, align, true, blksize))
4484 : : {
4485 : : max_loop = true;
4486 : : break;
4487 : : }
4488 : 1909 : if (blksize
4489 : 1909 : && can_store_by_multiple_pieces (xlenest,
4490 : : builtin_memset_read_str,
4491 : : &valc, align, true, 0))
4492 : : {
4493 : 0 : max_len += blksize;
4494 : 0 : min_len += blksize;
4495 : 0 : tst_bits = orig_tst_bits;
4496 : 0 : blksize = 0;
4497 : 0 : max_loop = true;
4498 : 0 : break;
4499 : : }
4500 : 1909 : if (max_bits == sctz_len)
4501 : : {
4502 : : /* We'll get here if can_store_by_pieces refuses to
4503 : : store even a single QImode. We'll fall back to
4504 : : QImode stores then. */
4505 : 78 : if (!sctz_len)
4506 : : {
4507 : : blksize = 0;
4508 : : max_loop = true;
4509 : : use_store_by_pieces = false;
4510 : : break;
4511 : : }
4512 : 78 : --sctz_len;
4513 : 78 : --ctz_len;
4514 : : }
4515 : : }
4516 : 69 : if (!max_loop)
4517 : : return false;
4518 : : /* If the boundaries are such that min and max may run a
4519 : : different number of trips in the initial loop, the remainder
4520 : : needs not be between the moduli, so set tst_bits to cover all
4521 : : bits. Otherwise, if the trip counts are the same, max_len
4522 : : has the common prefix, and the previously-computed tst_bits
4523 : : is usable. */
4524 : 69 : if (max_len >> max_bits > min_len >> max_bits)
4525 : 27 : tst_bits = max_bits;
4526 : : }
4527 : :
4528 : 70 : by_pieces_constfn constfun;
4529 : 70 : void *constfundata;
4530 : 70 : if (val)
4531 : : {
4532 : 1 : constfun = builtin_memset_gen_str;
4533 : 1 : constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4534 : : val);
4535 : : }
4536 : : else
4537 : : {
4538 : : constfun = builtin_memset_read_str;
4539 : : constfundata = &valc;
4540 : : }
4541 : :
4542 : 70 : rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4543 : 70 : rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4544 : 70 : to = replace_equiv_address (to, ptr);
4545 : 70 : set_mem_align (to, align);
4546 : :
4547 : 70 : if (blksize)
4548 : : {
4549 : 0 : to = store_by_pieces (to, blksize,
4550 : : constfun, constfundata,
4551 : : align, true,
4552 : : max_len != 0 ? RETURN_END : RETURN_BEGIN);
4553 : 0 : if (max_len == 0)
4554 : : return true;
4555 : :
4556 : : /* Adjust PTR, TO and REM. Since TO's address is likely
4557 : : PTR+offset, we have to replace it. */
4558 : 0 : emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4559 : 0 : to = replace_equiv_address (to, ptr);
4560 : 0 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4561 : 0 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4562 : : }
4563 : :
4564 : : /* Iterate over power-of-two block sizes from the maximum length to
4565 : : the least significant bit possibly set in the length. */
4566 : 318 : for (int i = max_bits; i >= sctz_len; i--)
4567 : : {
4568 : 248 : rtx_code_label *loop_label = NULL;
4569 : 248 : rtx_code_label *label = NULL;
4570 : :
4571 : 248 : blksize = HOST_WIDE_INT_1U << i;
4572 : :
4573 : : /* If we're past the bits shared between min_ and max_len, expand
4574 : : a test on the dynamic length, comparing it with the
4575 : : BLKSIZE. */
4576 : 248 : if (i <= tst_bits)
4577 : : {
4578 : 146 : label = gen_label_rtx ();
4579 : 146 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4580 : : ptr_mode, 1, label,
4581 : : profile_probability::even ());
4582 : : }
4583 : : /* If we are at a bit that is in the prefix shared by min_ and
4584 : : max_len, skip the current BLKSIZE if the bit is clear, but do
4585 : : not skip the loop, even if it doesn't require
4586 : : prechecking. */
4587 : 102 : else if ((max_len & blksize) == 0
4588 : 66 : && !(max_loop && i == max_bits))
4589 : 36 : continue;
4590 : :
4591 : 212 : if (max_loop && i == max_bits)
4592 : : {
4593 : 69 : loop_label = gen_label_rtx ();
4594 : 69 : emit_label (loop_label);
4595 : : /* Since we may run this multiple times, don't assume we
4596 : : know anything about the offset. */
4597 : 69 : clear_mem_offset (to);
4598 : : }
4599 : :
4600 : 212 : bool update_needed = i != sctz_len || loop_label;
4601 : 212 : rtx next_ptr = NULL_RTX;
4602 : 212 : if (!use_store_by_pieces)
4603 : : {
4604 : 0 : gcc_checking_assert (blksize == 1);
4605 : 0 : if (!val)
4606 : 0 : val = gen_int_mode (valc, QImode);
4607 : 0 : to = change_address (to, QImode, 0);
4608 : 0 : emit_move_insn (to, val);
4609 : 0 : if (update_needed)
4610 : 0 : next_ptr = plus_constant (GET_MODE (ptr), ptr, blksize);
4611 : : }
4612 : : else
4613 : : {
4614 : : /* Issue a store of BLKSIZE bytes. */
4615 : 247 : to = store_by_pieces (to, blksize,
4616 : : constfun, constfundata,
4617 : : align, true,
4618 : : update_needed ? RETURN_END : RETURN_BEGIN);
4619 : 212 : next_ptr = XEXP (to, 0);
4620 : : }
4621 : : /* Adjust REM and PTR, unless this is the last iteration. */
4622 : 212 : if (update_needed)
4623 : : {
4624 : 177 : emit_move_insn (ptr, force_operand (next_ptr, NULL_RTX));
4625 : 177 : to = replace_equiv_address (to, ptr);
4626 : 177 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4627 : 177 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4628 : : }
4629 : :
4630 : 177 : if (loop_label)
4631 : 69 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), GE, NULL,
4632 : : ptr_mode, 1, loop_label,
4633 : : profile_probability::likely ());
4634 : :
4635 : 212 : if (label)
4636 : : {
4637 : 146 : emit_label (label);
4638 : :
4639 : : /* Given conditional stores, the offset can no longer be
4640 : : known, so clear it. */
4641 : 146 : clear_mem_offset (to);
4642 : : }
4643 : : }
4644 : :
4645 : : return true;
4646 : : }
4647 : :
4648 : : /* Helper function to do the actual work for expand_builtin_memset. The
4649 : : arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4650 : : so that this can also be called without constructing an actual CALL_EXPR.
4651 : : The other arguments and return value are the same as for
4652 : : expand_builtin_memset. */
4653 : :
4654 : : static rtx
4655 : 33744 : expand_builtin_memset_args (tree dest, tree val, tree len,
4656 : : rtx target, machine_mode mode, tree orig_exp)
4657 : : {
4658 : 33744 : tree fndecl, fn;
4659 : 33744 : enum built_in_function fcode;
4660 : 33744 : machine_mode val_mode;
4661 : 33744 : char c;
4662 : 33744 : unsigned int dest_align;
4663 : 33744 : rtx dest_mem, dest_addr, len_rtx;
4664 : 33744 : HOST_WIDE_INT expected_size = -1;
4665 : 33744 : unsigned int expected_align = 0;
4666 : 33744 : unsigned HOST_WIDE_INT min_size;
4667 : 33744 : unsigned HOST_WIDE_INT max_size;
4668 : 33744 : unsigned HOST_WIDE_INT probable_max_size;
4669 : :
4670 : 33744 : dest_align = get_pointer_alignment (dest);
4671 : :
4672 : : /* If DEST is not a pointer type, don't do this operation in-line. */
4673 : 33744 : if (dest_align == 0)
4674 : : return NULL_RTX;
4675 : :
4676 : 33744 : if (currently_expanding_gimple_stmt)
4677 : 33744 : stringop_block_profile (currently_expanding_gimple_stmt,
4678 : : &expected_align, &expected_size);
4679 : :
4680 : 33744 : if (expected_align < dest_align)
4681 : 33739 : expected_align = dest_align;
4682 : :
4683 : : /* If the LEN parameter is zero, return DEST. */
4684 : 33744 : if (integer_zerop (len))
4685 : : {
4686 : : /* Evaluate and ignore VAL in case it has side-effects. */
4687 : 0 : expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4688 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
4689 : : }
4690 : :
4691 : : /* Stabilize the arguments in case we fail. */
4692 : 33744 : dest = builtin_save_expr (dest);
4693 : 33744 : val = builtin_save_expr (val);
4694 : 33744 : len = builtin_save_expr (len);
4695 : :
4696 : 33744 : len_rtx = expand_normal (len);
4697 : 33744 : determine_block_size (len, len_rtx, &min_size, &max_size,
4698 : : &probable_max_size);
4699 : 33744 : dest_mem = get_memory_rtx (dest, len);
4700 : 33744 : val_mode = TYPE_MODE (unsigned_char_type_node);
4701 : :
4702 : 33744 : if (TREE_CODE (val) != INTEGER_CST
4703 : 33744 : || target_char_cast (val, &c))
4704 : : {
4705 : 1623 : rtx val_rtx;
4706 : :
4707 : 1623 : val_rtx = expand_normal (val);
4708 : 1623 : val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4709 : :
4710 : : /* Assume that we can memset by pieces if we can store
4711 : : * the coefficients by pieces (in the required modes).
4712 : : * We can't pass builtin_memset_gen_str as that emits RTL. */
4713 : 1623 : c = 1;
4714 : 1623 : if (tree_fits_uhwi_p (len)
4715 : 1623 : && can_store_by_pieces (tree_to_uhwi (len),
4716 : : builtin_memset_read_str, &c, dest_align,
4717 : : true))
4718 : : {
4719 : 733 : val_rtx = force_reg (val_mode, val_rtx);
4720 : 733 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4721 : : builtin_memset_gen_str, val_rtx, dest_align,
4722 : : true, RETURN_BEGIN);
4723 : : }
4724 : 890 : else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4725 : : dest_align, expected_align,
4726 : : expected_size, min_size, max_size,
4727 : : probable_max_size)
4728 : 890 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4729 : : tree_ctz (len),
4730 : : min_size, max_size,
4731 : : val_rtx, 0,
4732 : : dest_align))
4733 : 612 : goto do_libcall;
4734 : :
4735 : 1011 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4736 : 1011 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4737 : 1011 : return dest_mem;
4738 : : }
4739 : :
4740 : 32121 : if (c)
4741 : : {
4742 : 11710 : if (tree_fits_uhwi_p (len)
4743 : 11710 : && can_store_by_pieces (tree_to_uhwi (len),
4744 : : builtin_memset_read_str, &c, dest_align,
4745 : : true))
4746 : 4811 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4747 : : builtin_memset_read_str, &c, dest_align, true,
4748 : : RETURN_BEGIN);
4749 : 8521 : else if (!set_storage_via_setmem (dest_mem, len_rtx,
4750 : 6899 : gen_int_mode (c, val_mode),
4751 : : dest_align, expected_align,
4752 : : expected_size, min_size, max_size,
4753 : : probable_max_size)
4754 : 6899 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4755 : : tree_ctz (len),
4756 : : min_size, max_size,
4757 : : NULL_RTX, c,
4758 : : dest_align))
4759 : 5277 : goto do_libcall;
4760 : :
4761 : 6433 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4762 : 6433 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4763 : 6433 : return dest_mem;
4764 : : }
4765 : :
4766 : 20411 : set_mem_align (dest_mem, dest_align);
4767 : 40822 : dest_addr = clear_storage_hints (dest_mem, len_rtx,
4768 : 20411 : CALL_EXPR_TAILCALL (orig_exp)
4769 : : ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4770 : : expected_align, expected_size,
4771 : : min_size, max_size,
4772 : : probable_max_size, tree_ctz (len));
4773 : :
4774 : 20411 : if (dest_addr == 0)
4775 : : {
4776 : 15437 : dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4777 : 15437 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
4778 : : }
4779 : :
4780 : : return dest_addr;
4781 : :
4782 : 5889 : do_libcall:
4783 : 5889 : fndecl = get_callee_fndecl (orig_exp);
4784 : 5889 : fcode = DECL_FUNCTION_CODE (fndecl);
4785 : 5889 : if (fcode == BUILT_IN_MEMSET)
4786 : 5889 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4787 : : dest, val, len);
4788 : 0 : else if (fcode == BUILT_IN_BZERO)
4789 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4790 : : dest, len);
4791 : : else
4792 : 0 : gcc_unreachable ();
4793 : 5889 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4794 : 5889 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4795 : 5889 : return expand_call (fn, target, target == const0_rtx);
4796 : : }
4797 : :
4798 : : /* Expand expression EXP, which is a call to the bzero builtin. Return
4799 : : NULL_RTX if we failed the caller should emit a normal call. */
4800 : :
4801 : : static rtx
4802 : 0 : expand_builtin_bzero (tree exp)
4803 : : {
4804 : 0 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4805 : : return NULL_RTX;
4806 : :
4807 : 0 : tree dest = CALL_EXPR_ARG (exp, 0);
4808 : 0 : tree size = CALL_EXPR_ARG (exp, 1);
4809 : :
4810 : : /* New argument list transforming bzero(ptr x, int y) to
4811 : : memset(ptr x, int 0, size_t y). This is done this way
4812 : : so that if it isn't expanded inline, we fallback to
4813 : : calling bzero instead of memset. */
4814 : :
4815 : 0 : location_t loc = EXPR_LOCATION (exp);
4816 : :
4817 : 0 : return expand_builtin_memset_args (dest, integer_zero_node,
4818 : : fold_convert_loc (loc,
4819 : : size_type_node, size),
4820 : 0 : const0_rtx, VOIDmode, exp);
4821 : : }
4822 : :
4823 : : /* Try to expand cmpstr operation ICODE with the given operands.
4824 : : Return the result rtx on success, otherwise return null. */
4825 : :
4826 : : static rtx
4827 : 0 : expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4828 : : HOST_WIDE_INT align)
4829 : : {
4830 : 0 : machine_mode insn_mode = insn_data[icode].operand[0].mode;
4831 : :
4832 : 0 : if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4833 : : target = NULL_RTX;
4834 : :
4835 : 0 : class expand_operand ops[4];
4836 : 0 : create_output_operand (&ops[0], target, insn_mode);
4837 : 0 : create_fixed_operand (&ops[1], arg1_rtx);
4838 : 0 : create_fixed_operand (&ops[2], arg2_rtx);
4839 : 0 : create_integer_operand (&ops[3], align);
4840 : 0 : if (maybe_expand_insn (icode, 4, ops))
4841 : 0 : return ops[0].value;
4842 : : return NULL_RTX;
4843 : : }
4844 : :
4845 : : /* Expand expression EXP, which is a call to the memcmp built-in function.
4846 : : Return NULL_RTX if we failed and the caller should emit a normal call,
4847 : : otherwise try to get the result in TARGET, if convenient.
4848 : : RESULT_EQ is true if we can relax the returned value to be either zero
4849 : : or nonzero, without caring about the sign. */
4850 : :
4851 : : static rtx
4852 : 110217 : expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4853 : : {
4854 : 110217 : if (!validate_arglist (exp,
4855 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4856 : : return NULL_RTX;
4857 : :
4858 : 110213 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4859 : 110213 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4860 : 110213 : tree len = CALL_EXPR_ARG (exp, 2);
4861 : :
4862 : : /* Due to the performance benefit, always inline the calls first
4863 : : when result_eq is false. */
4864 : 110213 : rtx result = NULL_RTX;
4865 : 110213 : enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4866 : 110213 : if (!result_eq && fcode != BUILT_IN_BCMP)
4867 : : {
4868 : 53346 : result = inline_expand_builtin_bytecmp (exp, target);
4869 : 53346 : if (result)
4870 : : return result;
4871 : : }
4872 : :
4873 : 110188 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4874 : 110188 : location_t loc = EXPR_LOCATION (exp);
4875 : :
4876 : 110188 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4877 : 110188 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4878 : :
4879 : : /* If we don't have POINTER_TYPE, call the function. */
4880 : 110188 : if (arg1_align == 0 || arg2_align == 0)
4881 : : return NULL_RTX;
4882 : :
4883 : 110188 : rtx arg1_rtx = get_memory_rtx (arg1, len);
4884 : 110188 : rtx arg2_rtx = get_memory_rtx (arg2, len);
4885 : 110188 : rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4886 : :
4887 : : /* Set MEM_SIZE as appropriate. */
4888 : 110188 : if (CONST_INT_P (len_rtx))
4889 : : {
4890 : 86807 : set_mem_size (arg1_rtx, INTVAL (len_rtx));
4891 : 86807 : set_mem_size (arg2_rtx, INTVAL (len_rtx));
4892 : : }
4893 : :
4894 : 110188 : by_pieces_constfn constfn = NULL;
4895 : :
4896 : : /* Try to get the byte representation of the constant ARG2 (or, only
4897 : : when the function's result is used for equality to zero, ARG1)
4898 : : points to, with its byte size in NBYTES. */
4899 : 110188 : unsigned HOST_WIDE_INT nbytes;
4900 : 110188 : const char *rep = getbyterep (arg2, &nbytes);
4901 : 110188 : if (result_eq && rep == NULL)
4902 : : {
4903 : : /* For equality to zero the arguments are interchangeable. */
4904 : 34501 : rep = getbyterep (arg1, &nbytes);
4905 : 34501 : if (rep != NULL)
4906 : : std::swap (arg1_rtx, arg2_rtx);
4907 : : }
4908 : :
4909 : : /* If the function's constant bound LEN_RTX is less than or equal
4910 : : to the byte size of the representation of the constant argument,
4911 : : and if block move would be done by pieces, we can avoid loading
4912 : : the bytes from memory and only store the computed constant result. */
4913 : 75687 : if (rep
4914 : 27250 : && CONST_INT_P (len_rtx)
4915 : 27157 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4916 : 110188 : constfn = builtin_memcpy_read_str;
4917 : :
4918 : 220376 : result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4919 : 110188 : TREE_TYPE (len), target,
4920 : : result_eq, constfn,
4921 : : CONST_CAST (char *, rep),
4922 : : tree_ctz (len));
4923 : :
4924 : 110188 : if (result)
4925 : : {
4926 : : /* Return the value in the proper mode for this function. */
4927 : 39551 : if (GET_MODE (result) == mode)
4928 : : return result;
4929 : :
4930 : 6 : if (target != 0)
4931 : : {
4932 : 6 : convert_move (target, result, 0);
4933 : 6 : return target;
4934 : : }
4935 : :
4936 : 0 : return convert_to_mode (mode, result, 0);
4937 : : }
4938 : :
4939 : : return NULL_RTX;
4940 : : }
4941 : :
4942 : : /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4943 : : if we failed the caller should emit a normal call, otherwise try to get
4944 : : the result in TARGET, if convenient. */
4945 : :
4946 : : static rtx
4947 : 127953 : expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4948 : : {
4949 : 127953 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4950 : : return NULL_RTX;
4951 : :
4952 : 127938 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4953 : 127938 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4954 : :
4955 : : /* Due to the performance benefit, always inline the calls first. */
4956 : 127938 : rtx result = NULL_RTX;
4957 : 127938 : result = inline_expand_builtin_bytecmp (exp, target);
4958 : 127938 : if (result)
4959 : : return result;
4960 : :
4961 : 127465 : insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4962 : 127465 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4963 : 127465 : if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4964 : : return NULL_RTX;
4965 : :
4966 : 127465 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4967 : 127465 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4968 : :
4969 : : /* If we don't have POINTER_TYPE, call the function. */
4970 : 127465 : if (arg1_align == 0 || arg2_align == 0)
4971 : : return NULL_RTX;
4972 : :
4973 : : /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4974 : 127465 : arg1 = builtin_save_expr (arg1);
4975 : 127465 : arg2 = builtin_save_expr (arg2);
4976 : :
4977 : 127465 : rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4978 : 127465 : rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4979 : :
4980 : : /* Try to call cmpstrsi. */
4981 : 127465 : if (cmpstr_icode != CODE_FOR_nothing)
4982 : 0 : result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4983 : 0 : MIN (arg1_align, arg2_align));
4984 : :
4985 : : /* Try to determine at least one length and call cmpstrnsi. */
4986 : 127465 : if (!result && cmpstrn_icode != CODE_FOR_nothing)
4987 : : {
4988 : 127465 : tree len;
4989 : 127465 : rtx arg3_rtx;
4990 : :
4991 : 127465 : tree len1 = c_strlen (arg1, 1);
4992 : 127465 : tree len2 = c_strlen (arg2, 1);
4993 : :
4994 : 127465 : if (len1)
4995 : 218 : len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4996 : 127465 : if (len2)
4997 : 125849 : len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4998 : :
4999 : : /* If we don't have a constant length for the first, use the length
5000 : : of the second, if we know it. We don't require a constant for
5001 : : this case; some cost analysis could be done if both are available
5002 : : but neither is constant. For now, assume they're equally cheap,
5003 : : unless one has side effects. If both strings have constant lengths,
5004 : : use the smaller. */
5005 : :
5006 : 127465 : if (!len1)
5007 : : len = len2;
5008 : 218 : else if (!len2)
5009 : : len = len1;
5010 : 16 : else if (TREE_SIDE_EFFECTS (len1))
5011 : : len = len2;
5012 : 16 : else if (TREE_SIDE_EFFECTS (len2))
5013 : : len = len1;
5014 : 16 : else if (TREE_CODE (len1) != INTEGER_CST)
5015 : : len = len2;
5016 : 16 : else if (TREE_CODE (len2) != INTEGER_CST)
5017 : : len = len1;
5018 : 9 : else if (tree_int_cst_lt (len1, len2))
5019 : : len = len1;
5020 : : else
5021 : 127252 : len = len2;
5022 : :
5023 : : /* If both arguments have side effects, we cannot optimize. */
5024 : 127465 : if (len && !TREE_SIDE_EFFECTS (len))
5025 : : {
5026 : 126051 : arg3_rtx = expand_normal (len);
5027 : 126051 : result = expand_cmpstrn_or_cmpmem
5028 : 126051 : (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
5029 : 126051 : arg3_rtx, MIN (arg1_align, arg2_align));
5030 : : }
5031 : : }
5032 : :
5033 : 127465 : tree fndecl = get_callee_fndecl (exp);
5034 : 127465 : if (result)
5035 : : {
5036 : : /* Return the value in the proper mode for this function. */
5037 : 52 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
5038 : 52 : if (GET_MODE (result) == mode)
5039 : : return result;
5040 : 0 : if (target == 0)
5041 : 0 : return convert_to_mode (mode, result, 0);
5042 : 0 : convert_move (target, result, 0);
5043 : 0 : return target;
5044 : : }
5045 : :
5046 : : /* Expand the library call ourselves using a stabilized argument
5047 : : list to avoid re-evaluating the function's arguments twice. */
5048 : 127413 : tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
5049 : 127413 : copy_warning (fn, exp);
5050 : 127413 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5051 : 127413 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5052 : 127413 : return expand_call (fn, target, target == const0_rtx);
5053 : : }
5054 : :
5055 : : /* Expand expression EXP, which is a call to the strncmp builtin. Return
5056 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
5057 : : try to get the result in TARGET, if convenient. */
5058 : :
5059 : : static rtx
5060 : 2012 : expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
5061 : : ATTRIBUTE_UNUSED machine_mode mode)
5062 : : {
5063 : 2012 : if (!validate_arglist (exp,
5064 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5065 : : return NULL_RTX;
5066 : :
5067 : 2006 : tree arg1 = CALL_EXPR_ARG (exp, 0);
5068 : 2006 : tree arg2 = CALL_EXPR_ARG (exp, 1);
5069 : 2006 : tree arg3 = CALL_EXPR_ARG (exp, 2);
5070 : :
5071 : 2006 : location_t loc = EXPR_LOCATION (exp);
5072 : 2006 : tree len1 = c_strlen (arg1, 1);
5073 : 2006 : tree len2 = c_strlen (arg2, 1);
5074 : :
5075 : : /* Due to the performance benefit, always inline the calls first. */
5076 : 2006 : rtx result = NULL_RTX;
5077 : 2006 : result = inline_expand_builtin_bytecmp (exp, target);
5078 : 2006 : if (result)
5079 : : return result;
5080 : :
5081 : : /* If c_strlen can determine an expression for one of the string
5082 : : lengths, and it doesn't have side effects, then emit cmpstrnsi
5083 : : using length MIN(strlen(string)+1, arg3). */
5084 : 1788 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
5085 : 1788 : if (cmpstrn_icode == CODE_FOR_nothing)
5086 : : return NULL_RTX;
5087 : :
5088 : 1788 : tree len;
5089 : :
5090 : 1788 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
5091 : 1788 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
5092 : :
5093 : 1788 : if (len1)
5094 : 151 : len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
5095 : 1788 : if (len2)
5096 : 664 : len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
5097 : :
5098 : 1788 : tree len3 = fold_convert_loc (loc, sizetype, arg3);
5099 : :
5100 : : /* If we don't have a constant length for the first, use the length
5101 : : of the second, if we know it. If neither string is constant length,
5102 : : use the given length argument. We don't require a constant for
5103 : : this case; some cost analysis could be done if both are available
5104 : : but neither is constant. For now, assume they're equally cheap,
5105 : : unless one has side effects. If both strings have constant lengths,
5106 : : use the smaller. */
5107 : :
5108 : 1788 : if (!len1 && !len2)
5109 : : len = len3;
5110 : 698 : else if (!len1)
5111 : : len = len2;
5112 : 151 : else if (!len2)
5113 : : len = len1;
5114 : 117 : else if (TREE_SIDE_EFFECTS (len1))
5115 : : len = len2;
5116 : 117 : else if (TREE_SIDE_EFFECTS (len2))
5117 : : len = len1;
5118 : 117 : else if (TREE_CODE (len1) != INTEGER_CST)
5119 : : len = len2;
5120 : 117 : else if (TREE_CODE (len2) != INTEGER_CST)
5121 : : len = len1;
5122 : 117 : else if (tree_int_cst_lt (len1, len2))
5123 : : len = len1;
5124 : : else
5125 : 548 : len = len2;
5126 : :
5127 : : /* If we are not using the given length, we must incorporate it here.
5128 : : The actual new length parameter will be MIN(len,arg3) in this case. */
5129 : 1788 : if (len != len3)
5130 : : {
5131 : 698 : len = fold_convert_loc (loc, sizetype, len);
5132 : 698 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
5133 : : }
5134 : 1788 : rtx arg1_rtx = get_memory_rtx (arg1, len);
5135 : 1788 : rtx arg2_rtx = get_memory_rtx (arg2, len);
5136 : 1788 : rtx arg3_rtx = expand_normal (len);
5137 : 1788 : result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
5138 : 1788 : arg2_rtx, TREE_TYPE (len), arg3_rtx,
5139 : 1788 : MIN (arg1_align, arg2_align));
5140 : :
5141 : 1788 : tree fndecl = get_callee_fndecl (exp);
5142 : 1788 : if (result)
5143 : : {
5144 : : /* Return the value in the proper mode for this function. */
5145 : 21 : mode = TYPE_MODE (TREE_TYPE (exp));
5146 : 21 : if (GET_MODE (result) == mode)
5147 : : return result;
5148 : 0 : if (target == 0)
5149 : 0 : return convert_to_mode (mode, result, 0);
5150 : 0 : convert_move (target, result, 0);
5151 : 0 : return target;
5152 : : }
5153 : :
5154 : : /* Expand the library call ourselves using a stabilized argument
5155 : : list to avoid re-evaluating the function's arguments twice. */
5156 : 1767 : tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
5157 : 1767 : copy_warning (call, exp);
5158 : 1767 : gcc_assert (TREE_CODE (call) == CALL_EXPR);
5159 : 1767 : CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
5160 : 1767 : return expand_call (call, target, target == const0_rtx);
5161 : : }
5162 : :
5163 : : /* Expand a call to __builtin_saveregs, generating the result in TARGET,
5164 : : if that's convenient. */
5165 : :
5166 : : rtx
5167 : 0 : expand_builtin_saveregs (void)
5168 : : {
5169 : 0 : rtx val;
5170 : 0 : rtx_insn *seq;
5171 : :
5172 : : /* Don't do __builtin_saveregs more than once in a function.
5173 : : Save the result of the first call and reuse it. */
5174 : 0 : if (saveregs_value != 0)
5175 : : return saveregs_value;
5176 : :
5177 : : /* When this function is called, it means that registers must be
5178 : : saved on entry to this function. So we migrate the call to the
5179 : : first insn of this function. */
5180 : :
5181 : 0 : start_sequence ();
5182 : :
5183 : : /* Do whatever the machine needs done in this case. */
5184 : 0 : val = targetm.calls.expand_builtin_saveregs ();
5185 : :
5186 : 0 : seq = get_insns ();
5187 : 0 : 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 : 20913 : 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 : 20913 : return expand_binop (ptr_mode, add_optab,
5209 : : crtl->args.internal_arg_pointer,
5210 : : crtl->args.arg_offset_rtx,
5211 : 20913 : 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 : 21199 : stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5219 : : {
5220 : 21199 : 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 : 21199 : if (!vatype)
5225 : 21075 : vatype = targetm.fn_abi_va_list (cfun->decl);
5226 : :
5227 : 21199 : if (TREE_CODE (vatype) == ARRAY_TYPE)
5228 : : {
5229 : 15537 : 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 : 15537 : 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 : 5656 : 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 : 20763 : expand_builtin_va_start (tree exp)
5323 : : {
5324 : 20763 : rtx nextarg;
5325 : 20763 : tree valist;
5326 : 20763 : location_t loc = EXPR_LOCATION (exp);
5327 : :
5328 : 20763 : 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 : 20763 : if (fold_builtin_next_arg (exp, true))
5335 : 0 : return const0_rtx;
5336 : :
5337 : 20763 : nextarg = expand_builtin_next_arg ();
5338 : 20763 : valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5339 : :
5340 : 20763 : if (targetm.expand_builtin_va_start)
5341 : 20763 : targetm.expand_builtin_va_start (valist, nextarg);
5342 : : else
5343 : 0 : std_expand_builtin_va_start (valist, nextarg);
5344 : :
5345 : 20763 : 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 : 218 : expand_builtin_va_copy (tree exp)
5369 : : {
5370 : 218 : tree dst, src, t;
5371 : 218 : location_t loc = EXPR_LOCATION (exp);
5372 : :
5373 : 218 : dst = CALL_EXPR_ARG (exp, 0);
5374 : 218 : src = CALL_EXPR_ARG (exp, 1);
5375 : :
5376 : 218 : dst = stabilize_va_list_loc (loc, dst, 1);
5377 : 218 : src = stabilize_va_list_loc (loc, src, 0);
5378 : :
5379 : 218 : gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5380 : :
5381 : 218 : 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 : 218 : rtx dstb, srcb, size;
5390 : :
5391 : : /* Evaluate to pointers. */
5392 : 218 : dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5393 : 218 : srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5394 : 218 : size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5395 : : NULL_RTX, VOIDmode, EXPAND_NORMAL);
5396 : :
5397 : 218 : dstb = convert_memory_address (Pmode, dstb);
5398 : 218 : srcb = convert_memory_address (Pmode, srcb);
5399 : :
5400 : : /* "Dereference" to BLKmode memories. */
5401 : 218 : dstb = gen_rtx_MEM (BLKmode, dstb);
5402 : 218 : set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5403 : 218 : set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5404 : 218 : srcb = gen_rtx_MEM (BLKmode, srcb);
5405 : 218 : set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5406 : 218 : set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5407 : :
5408 : : /* Copy. */
5409 : 218 : emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5410 : : }
5411 : :
5412 : 218 : 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 : 15556 : 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 : 15556 : if (call_expr_nargs (exp) == 0)
5426 : : /* Warning about missing arg was already issued. */
5427 : 0 : return const0_rtx;
5428 : 15556 : 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 : 15556 : unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5437 : :
5438 : 15556 : rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5439 : :
5440 : : /* Some ports cannot access arbitrary stack frames. */
5441 : 15556 : if (tem == NULL)
5442 : : {
5443 : 0 : warning (0, "unsupported argument to %qD", fndecl);
5444 : 0 : return const0_rtx;
5445 : : }
5446 : :
5447 : 15556 : 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 : 15556 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5457 : : return tem;
5458 : :
5459 : 5061 : if (!REG_P (tem)
5460 : 5061 : && ! CONSTANT_P (tem))
5461 : 5061 : tem = copy_addr_to_reg (tem);
5462 : 5061 : 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 : 3418 : expand_builtin_stack_address ()
5482 : : {
5483 : 3418 : 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 : 3418 : return force_reg (ptr_mode, ret);
5517 : : }
5518 : :
5519 : : /* Expand a call to builtin function __builtin_strub_enter. */
5520 : :
5521 : : static rtx
5522 : 2156 : expand_builtin_strub_enter (tree exp)
5523 : : {
5524 : 2156 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5525 : : return NULL_RTX;
5526 : :
5527 : 2156 : if (optimize < 1 || flag_no_inline)
5528 : : return NULL_RTX;
5529 : :
5530 : 1512 : rtx stktop = expand_builtin_stack_address ();
5531 : :
5532 : 1512 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5533 : 1512 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5534 : 1512 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5535 : : build_int_cst (TREE_TYPE (wmptr), 0));
5536 : 1512 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5537 : :
5538 : 1512 : emit_move_insn (wmark, stktop);
5539 : :
5540 : 1512 : return const0_rtx;
5541 : : }
5542 : :
5543 : : /* Expand a call to builtin function __builtin_strub_update. */
5544 : :
5545 : : static rtx
5546 : 1069 : expand_builtin_strub_update (tree exp)
5547 : : {
5548 : 1069 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5549 : : return NULL_RTX;
5550 : :
5551 : 1069 : if (optimize < 2 || flag_no_inline)
5552 : : return NULL_RTX;
5553 : :
5554 : 649 : 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 : 649 : if (true /* (flags_from_decl_or_type (current_function_decl) & ECF_LEAF) */)
5592 : : {
5593 : 649 : poly_int64 red_zone_size = RED_ZONE_SIZE;
5594 : : #if STACK_GROWS_DOWNWARD
5595 : 649 : red_zone_size = -red_zone_size;
5596 : : #endif
5597 : 649 : stktop = plus_constant (ptr_mode, stktop, red_zone_size);
5598 : 649 : stktop = force_reg (ptr_mode, stktop);
5599 : : }
5600 : : #endif
5601 : :
5602 : 649 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5603 : 649 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5604 : 649 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5605 : : build_int_cst (TREE_TYPE (wmptr), 0));
5606 : 649 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5607 : :
5608 : 649 : rtx wmarkr = force_reg (ptr_mode, wmark);
5609 : :
5610 : 649 : rtx_code_label *lab = gen_label_rtx ();
5611 : 649 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5612 : : ptr_mode, NULL_RTX, lab, NULL,
5613 : : profile_probability::very_likely ());
5614 : 649 : 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 : 649 : tree xwmptr = (optimize > 2
5624 : 649 : ? strub_watermark_parm (current_function_decl)
5625 : 649 : : wmptr);
5626 : 649 : 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 : 649 : emit_label (lab);
5642 : :
5643 : 649 : return const0_rtx;
5644 : : }
5645 : :
5646 : :
5647 : : /* Expand a call to builtin function __builtin_strub_leave. */
5648 : :
5649 : : static rtx
5650 : 2726 : expand_builtin_strub_leave (tree exp)
5651 : : {
5652 : 2726 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5653 : : return NULL_RTX;
5654 : :
5655 : 2726 : if (optimize < 2 || optimize_size || flag_no_inline)
5656 : : return NULL_RTX;
5657 : :
5658 : 1226 : rtx stktop = NULL_RTX;
5659 : :
5660 : 2452 : if (tree wmptr = (optimize
5661 : 1226 : ? strub_watermark_parm (current_function_decl)
5662 : 1226 : : 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 : 717 : stktop = expand_builtin_stack_address ();
5673 : :
5674 : 1226 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5675 : 1226 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5676 : 1226 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5677 : : build_int_cst (TREE_TYPE (wmptr), 0));
5678 : 1226 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5679 : :
5680 : 1226 : rtx wmarkr = force_reg (ptr_mode, wmark);
5681 : :
5682 : : #if ! STACK_GROWS_DOWNWARD
5683 : : rtx base = stktop;
5684 : : rtx end = wmarkr;
5685 : : #else
5686 : 1226 : rtx base = wmarkr;
5687 : 1226 : 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 : 1226 : base = copy_to_reg (base);
5692 : :
5693 : 1226 : rtx_code_label *done = gen_label_rtx ();
5694 : 1226 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5695 : : ptr_mode, NULL_RTX, done, NULL,
5696 : : profile_probability::very_likely ());
5697 : :
5698 : 1226 : if (optimize < 3)
5699 : 906 : 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 : 1226 : emit_label (done);
5740 : :
5741 : 1226 : 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 : 25274 : expand_builtin_alloca (tree exp)
5749 : : {
5750 : 25274 : rtx op0;
5751 : 25274 : rtx result;
5752 : 25274 : unsigned int align;
5753 : 25274 : tree fndecl = get_callee_fndecl (exp);
5754 : 25274 : HOST_WIDE_INT max_size;
5755 : 25274 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5756 : 25274 : bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5757 : 25274 : bool valid_arglist
5758 : : = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5759 : 50546 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5760 : : VOID_TYPE)
5761 : : : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5762 : 46760 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5763 : 21488 : : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5764 : :
5765 : 25274 : if (!valid_arglist)
5766 : : return NULL_RTX;
5767 : :
5768 : : /* Compute the argument. */
5769 : 25269 : op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5770 : :
5771 : : /* Compute the alignment. */
5772 : 46751 : align = (fcode == BUILT_IN_ALLOCA
5773 : 21483 : ? BIGGEST_ALIGNMENT
5774 : 3786 : : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5775 : :
5776 : : /* Compute the maximum size. */
5777 : 50538 : max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5778 : 25271 : ? 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 : 25269 : result
5784 : 25269 : = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5785 : 25269 : result = convert_memory_address (ptr_mode, result);
5786 : :
5787 : : /* Dynamic allocations for variables are recorded during gimplification. */
5788 : 25269 : 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 : 1210 : expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5825 : : rtx subtarget)
5826 : : {
5827 : 1210 : tree arg;
5828 : 1210 : rtx op0;
5829 : :
5830 : 1210 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5831 : : return NULL_RTX;
5832 : :
5833 : 1210 : arg = CALL_EXPR_ARG (exp, 0);
5834 : 1210 : op0 = expand_expr (arg,
5835 : 50 : subtarget && GET_MODE (subtarget) == target_mode
5836 : : ? subtarget : NULL_RTX,
5837 : : target_mode, EXPAND_NORMAL);
5838 : 1210 : if (GET_MODE (op0) != target_mode)
5839 : 123 : op0 = convert_to_mode (target_mode, op0, 1);
5840 : :
5841 : 1210 : target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5842 : :
5843 : 1210 : gcc_assert (target);
5844 : :
5845 : 1210 : 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 : 746 : expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5855 : : rtx subtarget, optab op_optab)
5856 : : {
5857 : 746 : rtx op0;
5858 : :
5859 : 746 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5860 : : return NULL_RTX;
5861 : :
5862 : : /* Compute the argument. */
5863 : 1492 : op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5864 : : (subtarget
5865 : 96 : && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5866 : 96 : == 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 : 746 : target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5871 : : op_optab, op0, target, op_optab != clrsb_optab);
5872 : 746 : gcc_assert (target);
5873 : :
5874 : 746 : 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 : 1029 : expand_builtin_expect (tree exp, rtx target)
5883 : : {
5884 : 1029 : tree arg;
5885 : :
5886 : 1029 : if (call_expr_nargs (exp) < 2)
5887 : 0 : return const0_rtx;
5888 : 1029 : arg = CALL_EXPR_ARG (exp, 0);
5889 : :
5890 : 1029 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5891 : : /* When guessing was done, the hints should be already stripped away. */
5892 : 1029 : 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 : 36016 : expand_builtin_trap (void)
5937 : : {
5938 : 36016 : if (targetm.have_trap ())
5939 : : {
5940 : 36016 : 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 : 36016 : if (!ACCUMULATE_OUTGOING_ARGS)
5945 : 36015 : 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 : 36016 : emit_barrier ();
5955 : 36016 : }
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 : 3617 : expand_builtin_unreachable (void)
5964 : : {
5965 : : /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5966 : : to avoid this. */
5967 : 3617 : gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5968 : 3617 : emit_barrier ();
5969 : 3617 : }
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 : 11650 : expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
6001 : : {
6002 : 11650 : rtx op0, op1;
6003 : 11650 : tree arg;
6004 : :
6005 : 11650 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6006 : : return NULL_RTX;
6007 : :
6008 : 11649 : arg = CALL_EXPR_ARG (exp, 0);
6009 : 11649 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6010 : :
6011 : 11649 : arg = CALL_EXPR_ARG (exp, 1);
6012 : 11649 : op1 = expand_normal (arg);
6013 : :
6014 : 11649 : 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 : 28 : begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
6087 : :
6088 : 28 : end = CALL_EXPR_ARG (exp, 1);
6089 : 28 : 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 : 558 : round_trampoline_addr (rtx tramp)
6098 : : {
6099 : 558 : 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 : 558 : 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 : 272 : expand_builtin_init_trampoline (tree exp, bool onstack)
6121 : : {
6122 : 272 : tree t_tramp, t_func, t_chain;
6123 : 272 : rtx m_tramp, r_tramp, r_chain, tmp;
6124 : :
6125 : 272 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
6126 : : POINTER_TYPE, VOID_TYPE))
6127 : : return NULL_RTX;
6128 : :
6129 : 272 : t_tramp = CALL_EXPR_ARG (exp, 0);
6130 : 272 : t_func = CALL_EXPR_ARG (exp, 1);
6131 : 272 : t_chain = CALL_EXPR_ARG (exp, 2);
6132 : :
6133 : 272 : r_tramp = expand_normal (t_tramp);
6134 : 272 : m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
6135 : 272 : 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 : 272 : if (TREE_CODE (t_tramp) == ADDR_EXPR)
6141 : 272 : 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 : 272 : tmp = round_trampoline_addr (r_tramp);
6147 : 272 : 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 : 272 : gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
6157 : 272 : t_func = TREE_OPERAND (t_func, 0);
6158 : 272 : gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
6159 : :
6160 : 272 : r_chain = expand_normal (t_chain);
6161 : :
6162 : : /* Generate insns to initialize the trampoline. */
6163 : 272 : targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
6164 : :
6165 : 272 : if (onstack)
6166 : : {
6167 : 272 : trampolines_created = 1;
6168 : :
6169 : 272 : if (targetm.calls.custom_function_descriptors != 0)
6170 : 272 : warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
6171 : : "trampoline generated for nested function %qD", t_func);
6172 : : }
6173 : :
6174 : 272 : return const0_rtx;
6175 : : }
6176 : :
6177 : : static rtx
6178 : 286 : expand_builtin_adjust_trampoline (tree exp)
6179 : : {
6180 : 286 : rtx tramp;
6181 : :
6182 : 286 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6183 : : return NULL_RTX;
6184 : :
6185 : 286 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6186 : 286 : tramp = round_trampoline_addr (tramp);
6187 : 286 : 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 : : 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 : 1069 : expand_builtin_signbit (tree exp, rtx target)
6255 : : {
6256 : 1069 : const struct real_format *fmt;
6257 : 1069 : scalar_float_mode fmode;
6258 : 1069 : scalar_int_mode rmode, imode;
6259 : 1069 : tree arg;
6260 : 1069 : int word, bitpos;
6261 : 1069 : enum insn_code icode;
6262 : 1069 : rtx temp;
6263 : 1069 : location_t loc = EXPR_LOCATION (exp);
6264 : :
6265 : 1069 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6266 : : return NULL_RTX;
6267 : :
6268 : 1069 : arg = CALL_EXPR_ARG (exp, 0);
6269 : 1069 : fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
6270 : 1069 : rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
6271 : 1069 : fmt = REAL_MODE_FORMAT (fmode);
6272 : :
6273 : 1069 : arg = builtin_save_expr (arg);
6274 : :
6275 : : /* Expand the argument yielding a RTX expression. */
6276 : 1069 : temp = expand_normal (arg);
6277 : :
6278 : : /* Check if the back end provides an insn that handles signbit for the
6279 : : argument's mode. */
6280 : 1069 : icode = optab_handler (signbit_optab, fmode);
6281 : 1069 : 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 : 1059 : bitpos = fmt->signbit_ro;
6293 : 1059 : 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 : 2118 : if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
6304 : : {
6305 : 955 : imode = int_mode_for_mode (fmode).require ();
6306 : 955 : 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 : 1059 : 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 : 2118 : if (bitpos < GET_MODE_BITSIZE (rmode))
6330 : : {
6331 : 851 : wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
6332 : :
6333 : 2553 : if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
6334 : 0 : temp = gen_lowpart (rmode, temp);
6335 : 1702 : temp = expand_binop (rmode, and_optab, temp,
6336 : 1702 : immed_wide_int_const (mask, rmode),
6337 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6338 : 851 : }
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 (!profile_arc_flag && !condition_coverage_flag)
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 : 132558 : 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 : 132558 : 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 : 148540 : get_builtin_sync_mem (tree loc, machine_mode mode)
6441 : : {
6442 : 148540 : rtx addr, mem;
6443 : 148540 : int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
6444 : : ? TREE_TYPE (TREE_TYPE (loc))
6445 : : : TREE_TYPE (loc));
6446 : 148540 : scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
6447 : :
6448 : 148540 : addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
6449 : 148540 : 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 : 148540 : mem = gen_rtx_MEM (mode, addr);
6455 : :
6456 : 148540 : set_mem_addr_space (mem, addr_space);
6457 : :
6458 : 148540 : mem = validize_mem (mem);
6459 : :
6460 : : /* The alignment needs to be at least according to that of the mode. */
6461 : 148540 : set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6462 : : get_pointer_alignment (loc)));
6463 : 148540 : set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6464 : 148540 : MEM_VOLATILE_P (mem) = 1;
6465 : :
6466 : 148540 : 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 : 93180 : expand_expr_force_mode (tree exp, machine_mode mode)
6475 : : {
6476 : 93180 : rtx val;
6477 : 93180 : machine_mode old_mode;
6478 : :
6479 : 93180 : if (TREE_CODE (exp) == SSA_NAME
6480 : 93180 : && 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 : 8455 : gimple *g = get_gimple_for_ssa_name (exp);
6486 : 8455 : if (g && gimple_assign_cast_p (g))
6487 : : {
6488 : 3893 : tree rhs = gimple_assign_rhs1 (g);
6489 : 3893 : tree_code code = gimple_assign_rhs_code (g);
6490 : 3893 : if (CONVERT_EXPR_CODE_P (code)
6491 : 3893 : && TYPE_MODE (TREE_TYPE (rhs)) == mode
6492 : 3797 : && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6493 : 3797 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6494 : 7690 : && (TYPE_PRECISION (TREE_TYPE (exp))
6495 : 3797 : > TYPE_PRECISION (TREE_TYPE (rhs))))
6496 : : exp = rhs;
6497 : : }
6498 : : }
6499 : :
6500 : 93180 : 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 : 93180 : old_mode = GET_MODE (val);
6505 : 93180 : if (old_mode == VOIDmode)
6506 : 44248 : old_mode = TYPE_MODE (TREE_TYPE (exp));
6507 : 93180 : val = convert_modes (mode, old_mode, val, 1);
6508 : 93180 : 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 : 324 : expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6616 : : rtx target)
6617 : : {
6618 : 324 : rtx val, mem;
6619 : :
6620 : : /* Expand the operands. */
6621 : 324 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6622 : 324 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6623 : :
6624 : 324 : 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 : 166675 : 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 : 166675 : if (TREE_CODE (exp) != INTEGER_CST)
6649 : : return MEMMODEL_SEQ_CST;
6650 : :
6651 : 165819 : rtx op = expand_normal (exp);
6652 : :
6653 : 165819 : unsigned HOST_WIDE_INT val = INTVAL (op);
6654 : 165819 : if (targetm.memmodel_check)
6655 : 165819 : 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 : 165819 : 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 : 165818 : if (val == MEMMODEL_CONSUME)
6666 : 750 : val = MEMMODEL_ACQUIRE;
6667 : :
6668 : 165818 : 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 : 2749 : expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6678 : : {
6679 : 2749 : rtx val, mem;
6680 : 2749 : enum memmodel model;
6681 : :
6682 : 2749 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6683 : :
6684 : 2749 : if (!flag_inline_atomics)
6685 : : return NULL_RTX;
6686 : :
6687 : : /* Expand the operands. */
6688 : 2698 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6689 : 2698 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6690 : :
6691 : 2698 : 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 : 9184 : expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6704 : : rtx target)
6705 : : {
6706 : 9184 : rtx expect, desired, mem, oldval;
6707 : 9184 : rtx_code_label *label;
6708 : 9184 : tree weak;
6709 : 9184 : bool is_weak;
6710 : :
6711 : 9184 : memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6712 : 9184 : memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6713 : :
6714 : 9184 : if (failure > success)
6715 : 22 : success = MEMMODEL_SEQ_CST;
6716 : :
6717 : 9184 : 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 : 9184 : if (!flag_inline_atomics)
6725 : : return NULL_RTX;
6726 : :
6727 : : /* Expand the operands. */
6728 : 9133 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6729 : :
6730 : 9133 : expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6731 : 9133 : expect = convert_memory_address (Pmode, expect);
6732 : 9133 : expect = gen_rtx_MEM (mode, expect);
6733 : 9133 : desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6734 : :
6735 : 9133 : weak = CALL_EXPR_ARG (exp, 3);
6736 : 9133 : is_weak = false;
6737 : 9133 : if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6738 : 9133 : is_weak = true;
6739 : :
6740 : 9133 : if (target == const0_rtx)
6741 : 284 : 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 : 9133 : oldval = NULL;
6746 : :
6747 : 9133 : 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 : 7390 : label = gen_label_rtx ();
6757 : 7390 : emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6758 : 7390 : GET_MODE (target), 1, label);
6759 : 7390 : emit_move_insn (expect, oldval);
6760 : 7390 : emit_label (label);
6761 : :
6762 : 7390 : 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 : 13498 : expand_ifn_atomic_compare_exchange (gcall *call)
6819 : : {
6820 : 13498 : int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6821 : 13498 : gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6822 : 13498 : machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6823 : :
6824 : 13498 : memmodel success = get_memmodel (gimple_call_arg (call, 4));
6825 : 13498 : memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6826 : :
6827 : 13498 : if (failure > success)
6828 : 0 : success = MEMMODEL_SEQ_CST;
6829 : :
6830 : 13498 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6831 : : {
6832 : : failure = MEMMODEL_SEQ_CST;
6833 : : success = MEMMODEL_SEQ_CST;
6834 : : }
6835 : :
6836 : 13498 : 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 : 13498 : rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6844 : :
6845 : 13498 : rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6846 : 13498 : rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6847 : :
6848 : 13498 : bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6849 : :
6850 : 13498 : rtx boolret = NULL;
6851 : 13498 : rtx oldval = NULL;
6852 : :
6853 : 13498 : 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 : 13498 : tree lhs = gimple_call_lhs (call);
6861 : 13498 : if (lhs)
6862 : : {
6863 : 13292 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6864 : 13292 : if (GET_MODE (boolret) != mode)
6865 : 11828 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6866 : 13292 : write_complex_part (target, boolret, true, true);
6867 : 13292 : 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 : 68880 : expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6878 : : {
6879 : 68880 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6880 : 68880 : if (is_mm_release (model) || is_mm_acq_rel (model))
6881 : : model = MEMMODEL_SEQ_CST;
6882 : :
6883 : 68880 : if (!flag_inline_atomics)
6884 : : return NULL_RTX;
6885 : :
6886 : : /* Expand the operand. */
6887 : 68839 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6888 : :
6889 : 68839 : 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 : 17255 : expand_builtin_atomic_store (machine_mode mode, tree exp)
6900 : : {
6901 : 17255 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6902 : 17255 : if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6903 : 1517 : || is_mm_release (model)))
6904 : : model = MEMMODEL_SEQ_CST;
6905 : :
6906 : 17255 : if (!flag_inline_atomics)
6907 : : return NULL_RTX;
6908 : :
6909 : : /* Expand the operands. */
6910 : 17224 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6911 : 17224 : rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6912 : :
6913 : 17224 : 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 : 29524 : 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 : 29524 : rtx val, mem, ret;
6933 : 29524 : enum memmodel model;
6934 : 29524 : tree fndecl;
6935 : 29524 : tree addr;
6936 : :
6937 : 29524 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6938 : :
6939 : : /* Expand the operands. */
6940 : 29524 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6941 : 29524 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6942 : :
6943 : : /* Only try generating instructions if inlining is turned on. */
6944 : 29524 : if (flag_inline_atomics)
6945 : : {
6946 : 28981 : ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6947 : 28981 : 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 : 1945 : expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
7099 : : {
7100 : 1945 : tree cmp = gimple_call_arg (call, 0);
7101 : 1945 : tree ptr = gimple_call_arg (call, 1);
7102 : 1945 : tree arg = gimple_call_arg (call, 2);
7103 : 1945 : tree lhs = gimple_call_lhs (call);
7104 : 1945 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7105 : 1945 : machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
7106 : 1945 : optab optab;
7107 : 1945 : rtx_code code;
7108 : 1945 : class expand_operand ops[5];
7109 : :
7110 : 1945 : gcc_assert (flag_inline_atomics);
7111 : :
7112 : 1945 : if (gimple_call_num_args (call) == 5)
7113 : 1537 : model = get_memmodel (gimple_call_arg (call, 3));
7114 : :
7115 : 1945 : rtx mem = get_builtin_sync_mem (ptr, mode);
7116 : 1945 : rtx op = expand_expr_force_mode (arg, mode);
7117 : :
7118 : 1945 : 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 : 1945 : enum rtx_code comp = UNKNOWN;
7145 : 1945 : 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 : 1945 : rtx target;
7157 : 1945 : if (lhs == NULL_TREE)
7158 : 0 : target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
7159 : : else
7160 : 1945 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7161 : 1945 : enum insn_code icode = direct_optab_handler (optab, mode);
7162 : 1945 : gcc_assert (icode != CODE_FOR_nothing);
7163 : 1945 : create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
7164 : 1945 : create_fixed_operand (&ops[1], mem);
7165 : 1945 : create_convert_operand_to (&ops[2], op, mode, true);
7166 : 1945 : create_integer_operand (&ops[3], model);
7167 : 1945 : create_integer_operand (&ops[4], comp);
7168 : 1945 : if (maybe_expand_insn (icode, 5, ops))
7169 : 1913 : 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 : 260 : expand_builtin_atomic_test_and_set (tree exp, rtx target)
7228 : : {
7229 : 260 : rtx mem;
7230 : 260 : enum memmodel model;
7231 : 260 : machine_mode mode;
7232 : :
7233 : 260 : mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7234 : 260 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7235 : 260 : model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7236 : :
7237 : 260 : 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 : 77253 : fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
7246 : : {
7247 : 77253 : int size;
7248 : 77253 : machine_mode mode;
7249 : 77253 : unsigned int mode_align, type_align;
7250 : :
7251 : 77253 : 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 : 77159 : size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
7256 : 77159 : if (!int_mode_for_size (size, 0).exists (&mode))
7257 : 1 : return boolean_false_node;
7258 : :
7259 : 77158 : mode_align = GET_MODE_ALIGNMENT (mode);
7260 : :
7261 : 77158 : if (TREE_CODE (arg1) == INTEGER_CST)
7262 : : {
7263 : 57612 : 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 : 57612 : val = least_bit_hwi (val);
7268 : 57612 : val *= BITS_PER_UNIT;
7269 : :
7270 : 57612 : if (val == 0 || mode_align < val)
7271 : : type_align = mode_align;
7272 : : else
7273 : 18950 : type_align = val;
7274 : : }
7275 : : else
7276 : : {
7277 : 19546 : 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 : 19514 : && POINTER_TYPE_P (ttype)
7285 : 19514 : && VOID_TYPE_P (TREE_TYPE (ttype))
7286 : 39060 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
7287 : 19483 : arg1 = TREE_OPERAND (arg1, 0);
7288 : :
7289 : 19546 : ttype = TREE_TYPE (arg1);
7290 : 19546 : gcc_assert (POINTER_TYPE_P (ttype));
7291 : :
7292 : : /* Get the underlying type of the object. */
7293 : 19546 : ttype = TREE_TYPE (ttype);
7294 : 19546 : type_align = TYPE_ALIGN (ttype);
7295 : : }
7296 : :
7297 : : /* If the object has smaller alignment, the lock free routines cannot
7298 : : be used. */
7299 : 77158 : if (type_align < mode_align)
7300 : 93 : 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 : 77065 : if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
7307 : 77023 : 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 : 38537 : fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
7343 : : {
7344 : 38537 : if (!flag_inline_atomics)
7345 : : return NULL_TREE;
7346 : :
7347 : : /* If it isn't always lock free, don't generate a result. */
7348 : 38528 : 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 : 269 : 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 : 1641 : expand_stack_restore (tree var)
7462 : : {
7463 : 1641 : rtx_insn *prev;
7464 : 1641 : rtx sa = expand_normal (var);
7465 : :
7466 : 1641 : sa = convert_memory_address (Pmode, sa);
7467 : :
7468 : 1641 : prev = get_last_insn ();
7469 : 1641 : emit_stack_restore (SAVE_BLOCK, sa);
7470 : :
7471 : 1641 : record_new_stack_level ();
7472 : :
7473 : 1641 : fixup_args_size_notes (prev, get_last_insn (), 0);
7474 : 1641 : }
7475 : :
7476 : : /* Emit code to save the current value of stack. */
7477 : :
7478 : : static rtx
7479 : 1781 : expand_stack_save (void)
7480 : : {
7481 : 1781 : rtx ret = NULL_RTX;
7482 : :
7483 : 0 : emit_stack_save (SAVE_BLOCK, &ret);
7484 : 1781 : 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 : 716 : 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 : 716 : HOST_WIDE_INT offset = 0;
7588 : 716 : rtx var_rtx_array
7589 : 716 : = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7590 : 716 : rtx var_rtx = NULL_RTX;
7591 : 716 : rtx const_rtx = NULL_RTX;
7592 : 716 : rtx result = target ? target : gen_reg_rtx (mode);
7593 : 716 : rtx_code_label *ne_label = gen_label_rtx ();
7594 : 716 : tree unit_type_node = unsigned_char_type_node;
7595 : 716 : scalar_int_mode unit_mode
7596 : 716 : = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7597 : :
7598 : 716 : start_sequence ();
7599 : :
7600 : 2593 : for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7601 : : {
7602 : 1877 : var_rtx
7603 : 1877 : = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7604 : 1877 : const_rtx = c_readstr (const_str + offset, unit_mode);
7605 : 1877 : rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7606 : 1727 : rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7607 : :
7608 : 1877 : op0 = convert_modes (mode, unit_mode, op0, 1);
7609 : 1877 : op1 = convert_modes (mode, unit_mode, op1, 1);
7610 : 1877 : 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 : 1877 : if (diff != result)
7618 : 0 : emit_move_insn (result, diff);
7619 : :
7620 : 1877 : if (i < length - 1)
7621 : 1161 : emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7622 : : mode, true, ne_label);
7623 : 3754 : offset += GET_MODE_SIZE (unit_mode);
7624 : : }
7625 : :
7626 : 716 : emit_label (ne_label);
7627 : 716 : rtx_insn *insns = get_insns ();
7628 : 716 : end_sequence ();
7629 : 716 : emit_insn (insns);
7630 : :
7631 : 716 : return result;
7632 : : }
7633 : :
7634 : : /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7635 : : to TARGET if that's convenient.
7636 : : If the call is not been inlined, return NULL_RTX. */
7637 : :
7638 : : static rtx
7639 : 183290 : inline_expand_builtin_bytecmp (tree exp, rtx target)
7640 : : {
7641 : 183290 : tree fndecl = get_callee_fndecl (exp);
7642 : 183290 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7643 : 183290 : bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7644 : :
7645 : : /* Do NOT apply this inlining expansion when optimizing for size or
7646 : : optimization level below 2 or if unused *cmp hasn't been DCEd. */
7647 : 183290 : if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7648 : 160921 : return NULL_RTX;
7649 : :
7650 : 22369 : gcc_checking_assert (fcode == BUILT_IN_STRCMP
7651 : : || fcode == BUILT_IN_STRNCMP
7652 : : || fcode == BUILT_IN_MEMCMP);
7653 : :
7654 : : /* On a target where the type of the call (int) has same or narrower presicion
7655 : : than unsigned char, give up the inlining expansion. */
7656 : 22369 : if (TYPE_PRECISION (unsigned_char_type_node)
7657 : 22369 : >= TYPE_PRECISION (TREE_TYPE (exp)))
7658 : : return NULL_RTX;
7659 : :
7660 : 22369 : tree arg1 = CALL_EXPR_ARG (exp, 0);
7661 : 22369 : tree arg2 = CALL_EXPR_ARG (exp, 1);
7662 : 22369 : tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7663 : :
7664 : 22369 : unsigned HOST_WIDE_INT len1 = 0;
7665 : 22369 : unsigned HOST_WIDE_INT len2 = 0;
7666 : 22369 : unsigned HOST_WIDE_INT len3 = 0;
7667 : :
7668 : : /* Get the object representation of the initializers of ARG1 and ARG2
7669 : : as strings, provided they refer to constant objects, with their byte
7670 : : sizes in LEN1 and LEN2, respectively. */
7671 : 22369 : const char *bytes1 = getbyterep (arg1, &len1);
7672 : 22369 : const char *bytes2 = getbyterep (arg2, &len2);
7673 : :
7674 : : /* Fail if neither argument refers to an initialized constant. */
7675 : 22369 : if (!bytes1 && !bytes2)
7676 : : return NULL_RTX;
7677 : :
7678 : 18468 : if (is_ncmp)
7679 : : {
7680 : : /* Fail if the memcmp/strncmp bound is not a constant. */
7681 : 1342 : if (!tree_fits_uhwi_p (len3_tree))
7682 : : return NULL_RTX;
7683 : :
7684 : 1015 : len3 = tree_to_uhwi (len3_tree);
7685 : :
7686 : 1015 : if (fcode == BUILT_IN_MEMCMP)
7687 : : {
7688 : : /* Fail if the memcmp bound is greater than the size of either
7689 : : of the two constant objects. */
7690 : 520 : if ((bytes1 && len1 < len3)
7691 : 520 : || (bytes2 && len2 < len3))
7692 : : return NULL_RTX;
7693 : : }
7694 : : }
7695 : :
7696 : : if (fcode != BUILT_IN_MEMCMP)
7697 : : {
7698 : : /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7699 : : and LEN2 to the length of the nul-terminated string stored
7700 : : in each. */
7701 : 17621 : if (bytes1 != NULL)
7702 : 239 : len1 = strnlen (bytes1, len1) + 1;
7703 : 17621 : if (bytes2 != NULL)
7704 : 17385 : len2 = strnlen (bytes2, len2) + 1;
7705 : : }
7706 : :
7707 : : /* See inline_string_cmp. */
7708 : 18137 : int const_str_n;
7709 : 18137 : if (!len1)
7710 : : const_str_n = 2;
7711 : 244 : else if (!len2)
7712 : : const_str_n = 1;
7713 : 3 : else if (len2 > len1)
7714 : : const_str_n = 1;
7715 : : else
7716 : : const_str_n = 2;
7717 : :
7718 : : /* For strncmp only, compute the new bound as the smallest of
7719 : : the lengths of the two strings (plus 1) and the bound provided
7720 : : to the function. */
7721 : 17895 : unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7722 : 18137 : if (is_ncmp && len3 < bound)
7723 : 521 : bound = len3;
7724 : :
7725 : : /* If the bound of the comparison is larger than the threshold,
7726 : : do nothing. */
7727 : 18137 : if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7728 : : return NULL_RTX;
7729 : :
7730 : 716 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7731 : :
7732 : : /* Now, start inline expansion the call. */
7733 : 1375 : return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7734 : : (const_str_n == 1) ? bytes1 : bytes2, bound,
7735 : 716 : const_str_n, mode);
7736 : : }
7737 : :
7738 : : /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7739 : : represents the size of the first argument to that call, or VOIDmode
7740 : : if the argument is a pointer. IGNORE will be true if the result
7741 : : isn't used. */
7742 : : static rtx
7743 : 35 : expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7744 : : bool ignore)
7745 : : {
7746 : 35 : rtx val, failsafe;
7747 : 35 : unsigned nargs = call_expr_nargs (exp);
7748 : :
7749 : 35 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7750 : :
7751 : 35 : if (mode == VOIDmode)
7752 : : {
7753 : 4 : mode = TYPE_MODE (TREE_TYPE (arg0));
7754 : 4 : gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7755 : : }
7756 : :
7757 : 35 : val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7758 : :
7759 : : /* An optional second argument can be used as a failsafe value on
7760 : : some machines. If it isn't present, then the failsafe value is
7761 : : assumed to be 0. */
7762 : 35 : if (nargs > 1)
7763 : : {
7764 : 4 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7765 : 4 : failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7766 : : }
7767 : : else
7768 : 31 : failsafe = const0_rtx;
7769 : :
7770 : : /* If the result isn't used, the behavior is undefined. It would be
7771 : : nice to emit a warning here, but path splitting means this might
7772 : : happen with legitimate code. So simply drop the builtin
7773 : : expansion in that case; we've handled any side-effects above. */
7774 : 35 : if (ignore)
7775 : 0 : return const0_rtx;
7776 : :
7777 : : /* If we don't have a suitable target, create one to hold the result. */
7778 : 35 : if (target == NULL || GET_MODE (target) != mode)
7779 : 0 : target = gen_reg_rtx (mode);
7780 : :
7781 : 35 : if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7782 : 8 : val = convert_modes (mode, VOIDmode, val, false);
7783 : :
7784 : 35 : return targetm.speculation_safe_value (mode, target, val, failsafe);
7785 : : }
7786 : :
7787 : : /* Expand CRC* or REV_CRC* built-ins. */
7788 : :
7789 : : rtx
7790 : 33 : expand_builtin_crc_table_based (internal_fn fn, scalar_mode crc_mode,
7791 : : scalar_mode data_mode, machine_mode mode,
7792 : : tree exp, rtx target)
7793 : : {
7794 : 33 : tree rhs1 = CALL_EXPR_ARG (exp, 0); // crc
7795 : 33 : tree rhs2 = CALL_EXPR_ARG (exp, 1); // data
7796 : 33 : tree rhs3 = CALL_EXPR_ARG (exp, 2); // polynomial
7797 : :
7798 : 33 : if (!target || mode == VOIDmode)
7799 : 0 : target = gen_reg_rtx (crc_mode);
7800 : :
7801 : 33 : rtx op1 = expand_normal (rhs1);
7802 : 33 : rtx op2 = expand_normal (rhs2);
7803 : 33 : gcc_assert (TREE_CODE (rhs3) == INTEGER_CST);
7804 : 33 : rtx op3 = gen_int_mode (TREE_INT_CST_LOW (rhs3), crc_mode);
7805 : :
7806 : 33 : if (CONST_INT_P (op2))
7807 : 33 : op2 = gen_int_mode (INTVAL (op2), crc_mode);
7808 : :
7809 : 33 : if (fn == IFN_CRC)
7810 : 17 : expand_crc_table_based (target, op1, op2, op3, data_mode);
7811 : : else
7812 : : /* If it's IFN_CRC_REV generate bit-reversed CRC. */
7813 : 16 : expand_reversed_crc_table_based (target, op1, op2, op3,
7814 : : data_mode,
7815 : : generate_reflecting_code_standard);
7816 : 33 : return target;
7817 : : }
7818 : :
7819 : : /* Expand an expression EXP that calls a built-in function,
7820 : : with result going to TARGET if that's convenient
7821 : : (and in mode MODE if that's convenient).
7822 : : SUBTARGET may be used as the target for computing one of EXP's operands.
7823 : : IGNORE is nonzero if the value is to be ignored. */
7824 : :
7825 : : rtx
7826 : 1884134 : expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7827 : : int ignore)
7828 : : {
7829 : 1884134 : tree fndecl = get_callee_fndecl (exp);
7830 : 1884134 : machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7831 : 1884134 : int flags;
7832 : :
7833 : 1884134 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7834 : 174042 : return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7835 : :
7836 : : /* When ASan is enabled, we don't want to expand some memory/string
7837 : : builtins and rely on libsanitizer's hooks. This allows us to avoid
7838 : : redundant checks and be sure, that possible overflow will be detected
7839 : : by ASan. */
7840 : :
7841 : 1710092 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7842 : 1710092 : if (param_asan_kernel_mem_intrinsic_prefix
7843 : 1710092 : && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7844 : : | SANITIZE_KERNEL_HWADDRESS))
7845 : 42 : switch (fcode)
7846 : : {
7847 : 42 : rtx save_decl_rtl, ret;
7848 : 42 : case BUILT_IN_MEMCPY:
7849 : 42 : case BUILT_IN_MEMMOVE:
7850 : 42 : case BUILT_IN_MEMSET:
7851 : 42 : save_decl_rtl = DECL_RTL (fndecl);
7852 : 42 : DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7853 : 42 : ret = expand_call (exp, target, ignore);
7854 : 42 : DECL_RTL (fndecl) = save_decl_rtl;
7855 : 42 : return ret;
7856 : : default:
7857 : : break;
7858 : : }
7859 : 1710050 : if (sanitize_flags_p (SANITIZE_ADDRESS | SANITIZE_HWADDRESS)
7860 : 1710050 : && asan_intercepted_p (fcode))
7861 : 703 : return expand_call (exp, target, ignore);
7862 : :
7863 : : /* When not optimizing, generate calls to library functions for a certain
7864 : : set of builtins. */
7865 : 1709347 : if (!optimize
7866 : 414391 : && !called_as_built_in (fndecl)
7867 : 142437 : && fcode != BUILT_IN_FORK
7868 : 142437 : && fcode != BUILT_IN_EXECL
7869 : 142426 : && fcode != BUILT_IN_EXECV
7870 : 142426 : && fcode != BUILT_IN_EXECLP
7871 : 142424 : && fcode != BUILT_IN_EXECLE
7872 : 142424 : && fcode != BUILT_IN_EXECVP
7873 : 142423 : && fcode != BUILT_IN_EXECVE
7874 : 142423 : && fcode != BUILT_IN_CLEAR_CACHE
7875 : 142420 : && !ALLOCA_FUNCTION_CODE_P (fcode)
7876 : 142366 : && fcode != BUILT_IN_FREE
7877 : 140299 : && (fcode != BUILT_IN_MEMSET
7878 : 55885 : || !(flag_inline_stringops & ILSOP_MEMSET))
7879 : 140296 : && (fcode != BUILT_IN_MEMCPY
7880 : 918 : || !(flag_inline_stringops & ILSOP_MEMCPY))
7881 : 140160 : && (fcode != BUILT_IN_MEMMOVE
7882 : 347 : || !(flag_inline_stringops & ILSOP_MEMMOVE))
7883 : 1849507 : && (fcode != BUILT_IN_MEMCMP
7884 : 2503 : || !(flag_inline_stringops & ILSOP_MEMCMP)))
7885 : 140112 : return expand_call (exp, target, ignore);
7886 : :
7887 : : /* The built-in function expanders test for target == const0_rtx
7888 : : to determine whether the function's result will be ignored. */
7889 : 1569235 : if (ignore)
7890 : 894426 : target = const0_rtx;
7891 : :
7892 : : /* If the result of a pure or const built-in function is ignored, and
7893 : : none of its arguments are volatile, we can avoid expanding the
7894 : : built-in call and just evaluate the arguments for side-effects. */
7895 : 1569235 : if (target == const0_rtx
7896 : 894426 : && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7897 : 1573558 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
7898 : : {
7899 : 6 : bool volatilep = false;
7900 : 6 : tree arg;
7901 : 6 : call_expr_arg_iterator iter;
7902 : :
7903 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7904 : 8 : if (TREE_THIS_VOLATILE (arg))
7905 : : {
7906 : : volatilep = true;
7907 : : break;
7908 : : }
7909 : :
7910 : 6 : if (! volatilep)
7911 : : {
7912 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7913 : 8 : expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7914 : 6 : return const0_rtx;
7915 : : }
7916 : : }
7917 : :
7918 : 1569229 : switch (fcode)
7919 : : {
7920 : 4 : CASE_FLT_FN (BUILT_IN_FABS):
7921 : 4 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7922 : 4 : case BUILT_IN_FABSD32:
7923 : 4 : case BUILT_IN_FABSD64:
7924 : 4 : case BUILT_IN_FABSD128:
7925 : 4 : case BUILT_IN_FABSD64X:
7926 : 4 : target = expand_builtin_fabs (exp, target, subtarget);
7927 : 4 : if (target)
7928 : 747831 : return target;
7929 : 821398 : break;
7930 : :
7931 : 11650 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
7932 : 11650 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7933 : 11650 : target = expand_builtin_copysign (exp, target, subtarget);
7934 : 11650 : if (target)
7935 : : return target;
7936 : : break;
7937 : :
7938 : : /* Just do a normal library call if we were unable to fold
7939 : : the values. */
7940 : : CASE_FLT_FN (BUILT_IN_CABS):
7941 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7942 : : break;
7943 : :
7944 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
7945 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7946 : 364 : target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7947 : 364 : if (target)
7948 : : return target;
7949 : : break;
7950 : :
7951 : 235 : CASE_FLT_FN (BUILT_IN_ILOGB):
7952 : 235 : if (! flag_unsafe_math_optimizations)
7953 : : break;
7954 : 4 : gcc_fallthrough ();
7955 : 4 : CASE_FLT_FN (BUILT_IN_ISINF):
7956 : 4 : CASE_FLT_FN (BUILT_IN_FINITE):
7957 : 4 : case BUILT_IN_ISFINITE:
7958 : 4 : case BUILT_IN_ISNORMAL:
7959 : 4 : target = expand_builtin_interclass_mathfn (exp, target);
7960 : 4 : if (target)
7961 : : return target;
7962 : : break;
7963 : :
7964 : 823 : case BUILT_IN_ISSIGNALING:
7965 : 823 : target = expand_builtin_issignaling (exp, target);
7966 : 823 : if (target)
7967 : : return target;
7968 : : break;
7969 : :
7970 : 231 : CASE_FLT_FN (BUILT_IN_ICEIL):
7971 : 231 : CASE_FLT_FN (BUILT_IN_LCEIL):
7972 : 231 : CASE_FLT_FN (BUILT_IN_LLCEIL):
7973 : 231 : CASE_FLT_FN (BUILT_IN_LFLOOR):
7974 : 231 : CASE_FLT_FN (BUILT_IN_IFLOOR):
7975 : 231 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
7976 : 231 : target = expand_builtin_int_roundingfn (exp, target);
7977 : 231 : if (target)
7978 : : return target;
7979 : : break;
7980 : :
7981 : 590 : CASE_FLT_FN (BUILT_IN_IRINT):
7982 : 590 : CASE_FLT_FN (BUILT_IN_LRINT):
7983 : 590 : CASE_FLT_FN (BUILT_IN_LLRINT):
7984 : 590 : CASE_FLT_FN (BUILT_IN_IROUND):
7985 : 590 : CASE_FLT_FN (BUILT_IN_LROUND):
7986 : 590 : CASE_FLT_FN (BUILT_IN_LLROUND):
7987 : 590 : target = expand_builtin_int_roundingfn_2 (exp, target);
7988 : 590 : if (target)
7989 : : return target;
7990 : : break;
7991 : :
7992 : 312 : CASE_FLT_FN (BUILT_IN_POWI):
7993 : 312 : target = expand_builtin_powi (exp, target);
7994 : 312 : if (target)
7995 : : return target;
7996 : : break;
7997 : :
7998 : 163 : CASE_FLT_FN (BUILT_IN_CEXPI):
7999 : 163 : target = expand_builtin_cexpi (exp, target);
8000 : 163 : gcc_assert (target);
8001 : : return target;
8002 : :
8003 : 988 : CASE_FLT_FN (BUILT_IN_SIN):
8004 : 988 : CASE_FLT_FN (BUILT_IN_COS):
8005 : 988 : if (! flag_unsafe_math_optimizations)
8006 : : break;
8007 : 47 : target = expand_builtin_mathfn_3 (exp, target, subtarget);
8008 : 47 : if (target)
8009 : : return target;
8010 : : break;
8011 : :
8012 : 157 : CASE_FLT_FN (BUILT_IN_SINCOS):
8013 : 157 : if (! flag_unsafe_math_optimizations)
8014 : : break;
8015 : 3 : target = expand_builtin_sincos (exp);
8016 : 3 : if (target)
8017 : : return target;
8018 : : break;
8019 : :
8020 : 60 : case BUILT_IN_FEGETROUND:
8021 : 60 : target = expand_builtin_fegetround (exp, target, target_mode);
8022 : 60 : if (target)
8023 : : return target;
8024 : : break;
8025 : :
8026 : 1063 : case BUILT_IN_FECLEAREXCEPT:
8027 : 1063 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8028 : : feclearexcept_optab);
8029 : 1063 : if (target)
8030 : : return target;
8031 : : break;
8032 : :
8033 : 831 : case BUILT_IN_FERAISEEXCEPT:
8034 : 831 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8035 : : feraiseexcept_optab);
8036 : 831 : if (target)
8037 : : return target;
8038 : : break;
8039 : :
8040 : 463 : case BUILT_IN_APPLY_ARGS:
8041 : 463 : return expand_builtin_apply_args ();
8042 : :
8043 : : /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
8044 : : FUNCTION with a copy of the parameters described by
8045 : : ARGUMENTS, and ARGSIZE. It returns a block of memory
8046 : : allocated on the stack into which is stored all the registers
8047 : : that might possibly be used for returning the result of a
8048 : : function. ARGUMENTS is the value returned by
8049 : : __builtin_apply_args. ARGSIZE is the number of bytes of
8050 : : arguments that must be copied. ??? How should this value be
8051 : : computed? We'll also need a safe worst case value for varargs
8052 : : functions. */
8053 : 482 : case BUILT_IN_APPLY:
8054 : 482 : if (!validate_arglist (exp, POINTER_TYPE,
8055 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
8056 : 482 : && !validate_arglist (exp, REFERENCE_TYPE,
8057 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8058 : 0 : return const0_rtx;
8059 : : else
8060 : : {
8061 : 482 : rtx ops[3];
8062 : :
8063 : 482 : ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
8064 : 482 : ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
8065 : 482 : ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
8066 : :
8067 : 482 : return expand_builtin_apply (ops[0], ops[1], ops[2]);
8068 : : }
8069 : :
8070 : : /* __builtin_return (RESULT) causes the function to return the
8071 : : value described by RESULT. RESULT is address of the block of
8072 : : memory returned by __builtin_apply. */
8073 : 378 : case BUILT_IN_RETURN:
8074 : 378 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8075 : 378 : expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
8076 : 378 : return const0_rtx;
8077 : :
8078 : 0 : case BUILT_IN_SAVEREGS:
8079 : 0 : return expand_builtin_saveregs ();
8080 : :
8081 : 16 : case BUILT_IN_VA_ARG_PACK:
8082 : : /* All valid uses of __builtin_va_arg_pack () are removed during
8083 : : inlining. */
8084 : 16 : error ("invalid use of %<__builtin_va_arg_pack ()%>");
8085 : 16 : return const0_rtx;
8086 : :
8087 : 0 : case BUILT_IN_VA_ARG_PACK_LEN:
8088 : : /* All valid uses of __builtin_va_arg_pack_len () are removed during
8089 : : inlining. */
8090 : 0 : error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
8091 : 0 : return const0_rtx;
8092 : :
8093 : : /* Return the address of the first anonymous stack arg. */
8094 : 150 : case BUILT_IN_NEXT_ARG:
8095 : 150 : if (fold_builtin_next_arg (exp, false))
8096 : 0 : return const0_rtx;
8097 : 150 : return expand_builtin_next_arg ();
8098 : :
8099 : 28 : case BUILT_IN_CLEAR_CACHE:
8100 : 28 : expand_builtin___clear_cache (exp);
8101 : 28 : return const0_rtx;
8102 : :
8103 : 0 : case BUILT_IN_CLASSIFY_TYPE:
8104 : 0 : return expand_builtin_classify_type (exp);
8105 : :
8106 : 0 : case BUILT_IN_CONSTANT_P:
8107 : 0 : return const0_rtx;
8108 : :
8109 : 15556 : case BUILT_IN_FRAME_ADDRESS:
8110 : 15556 : case BUILT_IN_RETURN_ADDRESS:
8111 : 15556 : return expand_builtin_frame_address (fndecl, exp);
8112 : :
8113 : 540 : case BUILT_IN_STACK_ADDRESS:
8114 : 540 : return expand_builtin_stack_address ();
8115 : :
8116 : 2156 : case BUILT_IN___STRUB_ENTER:
8117 : 2156 : target = expand_builtin_strub_enter (exp);
8118 : 2156 : if (target)
8119 : : return target;
8120 : : break;
8121 : :
8122 : 1069 : case BUILT_IN___STRUB_UPDATE:
8123 : 1069 : target = expand_builtin_strub_update (exp);
8124 : 1069 : if (target)
8125 : : return target;
8126 : : break;
8127 : :
8128 : 2726 : case BUILT_IN___STRUB_LEAVE:
8129 : 2726 : target = expand_builtin_strub_leave (exp);
8130 : 2726 : if (target)
8131 : : return target;
8132 : : break;
8133 : :
8134 : : /* Returns the address of the area where the structure is returned.
8135 : : 0 otherwise. */
8136 : 0 : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
8137 : 0 : if (call_expr_nargs (exp) != 0
8138 : 0 : || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
8139 : 0 : || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
8140 : 0 : return const0_rtx;
8141 : : else
8142 : 0 : return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
8143 : :
8144 : 25274 : CASE_BUILT_IN_ALLOCA:
8145 : 25274 : target = expand_builtin_alloca (exp);
8146 : 25274 : if (target)
8147 : : return target;
8148 : : break;
8149 : :
8150 : 203 : case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
8151 : 203 : return expand_asan_emit_allocas_unpoison (exp);
8152 : :
8153 : 1781 : case BUILT_IN_STACK_SAVE:
8154 : 1781 : return expand_stack_save ();
8155 : :
8156 : 1641 : case BUILT_IN_STACK_RESTORE:
8157 : 1641 : expand_stack_restore (CALL_EXPR_ARG (exp, 0));
8158 : 1641 : return const0_rtx;
8159 : :
8160 : 1210 : case BUILT_IN_BSWAP16:
8161 : 1210 : case BUILT_IN_BSWAP32:
8162 : 1210 : case BUILT_IN_BSWAP64:
8163 : 1210 : case BUILT_IN_BSWAP128:
8164 : 1210 : target = expand_builtin_bswap (target_mode, exp, target, subtarget);
8165 : 1210 : if (target)
8166 : : return target;
8167 : : break;
8168 : :
8169 : 0 : CASE_INT_FN (BUILT_IN_FFS):
8170 : 0 : target = expand_builtin_unop (target_mode, exp, target,
8171 : : subtarget, ffs_optab);
8172 : 0 : if (target)
8173 : : return target;
8174 : : break;
8175 : :
8176 : 182 : CASE_INT_FN (BUILT_IN_CLZ):
8177 : 182 : target = expand_builtin_unop (target_mode, exp, target,
8178 : : subtarget, clz_optab);
8179 : 182 : if (target)
8180 : : return target;
8181 : : break;
8182 : :
8183 : 48 : CASE_INT_FN (BUILT_IN_CTZ):
8184 : 48 : target = expand_builtin_unop (target_mode, exp, target,
8185 : : subtarget, ctz_optab);
8186 : 48 : if (target)
8187 : : return target;
8188 : : break;
8189 : :
8190 : 90 : CASE_INT_FN (BUILT_IN_CLRSB):
8191 : 90 : target = expand_builtin_unop (target_mode, exp, target,
8192 : : subtarget, clrsb_optab);
8193 : 90 : if (target)
8194 : : return target;
8195 : : break;
8196 : :
8197 : 415 : CASE_INT_FN (BUILT_IN_POPCOUNT):
8198 : 415 : target = expand_builtin_unop (target_mode, exp, target,
8199 : : subtarget, popcount_optab);
8200 : 415 : if (target)
8201 : : return target;
8202 : : break;
8203 : :
8204 : 11 : CASE_INT_FN (BUILT_IN_PARITY):
8205 : 11 : target = expand_builtin_unop (target_mode, exp, target,
8206 : : subtarget, parity_optab);
8207 : 11 : if (target)
8208 : : return target;
8209 : : break;
8210 : :
8211 : 13507 : case BUILT_IN_STRLEN:
8212 : 13507 : target = expand_builtin_strlen (exp, target, target_mode);
8213 : 13507 : if (target)
8214 : : return target;
8215 : : break;
8216 : :
8217 : 580 : case BUILT_IN_STRNLEN:
8218 : 580 : target = expand_builtin_strnlen (exp, target, target_mode);
8219 : 580 : if (target)
8220 : : return target;
8221 : : break;
8222 : :
8223 : 1863 : case BUILT_IN_STRCPY:
8224 : 1863 : target = expand_builtin_strcpy (exp, target);
8225 : 1863 : if (target)
8226 : : return target;
8227 : : break;
8228 : :
8229 : 2055 : case BUILT_IN_STRNCPY:
8230 : 2055 : target = expand_builtin_strncpy (exp, target);
8231 : 2055 : if (target)
8232 : : return target;
8233 : : break;
8234 : :
8235 : 629 : case BUILT_IN_STPCPY:
8236 : 629 : target = expand_builtin_stpcpy (exp, target, mode);
8237 : 629 : if (target)
8238 : : return target;
8239 : : break;
8240 : :
8241 : 81107 : case BUILT_IN_MEMCPY:
8242 : 81107 : target = expand_builtin_memcpy (exp, target);
8243 : 81107 : if (target)
8244 : : return target;
8245 : : break;
8246 : :
8247 : 15038 : case BUILT_IN_MEMMOVE:
8248 : 15038 : target = expand_builtin_memmove (exp, target);
8249 : 15038 : if (target)
8250 : : return target;
8251 : : break;
8252 : :
8253 : 1637 : case BUILT_IN_MEMPCPY:
8254 : 1637 : target = expand_builtin_mempcpy (exp, target);
8255 : 1637 : if (target)
8256 : : return target;
8257 : : break;
8258 : :
8259 : 33569 : case BUILT_IN_MEMSET:
8260 : 33569 : target = expand_builtin_memset (exp, target, mode);
8261 : 33569 : if (target)
8262 : : return target;
8263 : : break;
8264 : :
8265 : 0 : case BUILT_IN_BZERO:
8266 : 0 : target = expand_builtin_bzero (exp);
8267 : 0 : if (target)
8268 : : return target;
8269 : : break;
8270 : :
8271 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8272 : : back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
8273 : : when changing it to a strcmp call. */
8274 : 380 : case BUILT_IN_STRCMP_EQ:
8275 : 380 : target = expand_builtin_memcmp (exp, target, true);
8276 : 380 : if (target)
8277 : : return target;
8278 : :
8279 : : /* Change this call back to a BUILT_IN_STRCMP. */
8280 : 22 : TREE_OPERAND (exp, 1)
8281 : 22 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
8282 : :
8283 : : /* Delete the last parameter. */
8284 : 22 : unsigned int i;
8285 : 22 : vec<tree, va_gc> *arg_vec;
8286 : 22 : vec_alloc (arg_vec, 2);
8287 : 88 : for (i = 0; i < 2; i++)
8288 : 44 : arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
8289 : 22 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
8290 : : /* FALLTHROUGH */
8291 : :
8292 : 127953 : case BUILT_IN_STRCMP:
8293 : 127953 : target = expand_builtin_strcmp (exp, target);
8294 : 127953 : if (target)
8295 : : return target;
8296 : : break;
8297 : :
8298 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8299 : : back to a BUILT_IN_STRNCMP. */
8300 : 36 : case BUILT_IN_STRNCMP_EQ:
8301 : 36 : target = expand_builtin_memcmp (exp, target, true);
8302 : 36 : if (target)
8303 : : return target;
8304 : :
8305 : : /* Change it back to a BUILT_IN_STRNCMP. */
8306 : 0 : TREE_OPERAND (exp, 1)
8307 : 0 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
8308 : : /* FALLTHROUGH */
8309 : :
8310 : 2012 : case BUILT_IN_STRNCMP:
8311 : 2012 : target = expand_builtin_strncmp (exp, target, mode);
8312 : 2012 : if (target)
8313 : : return target;
8314 : : break;
8315 : :
8316 : 109801 : case BUILT_IN_BCMP:
8317 : 109801 : case BUILT_IN_MEMCMP:
8318 : 109801 : case BUILT_IN_MEMCMP_EQ:
8319 : 109801 : target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
8320 : 109801 : if (target)
8321 : : return target;
8322 : 70619 : if (fcode == BUILT_IN_MEMCMP_EQ)
8323 : : {
8324 : 17697 : tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
8325 : 17697 : TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
8326 : : }
8327 : : break;
8328 : :
8329 : 0 : case BUILT_IN_SETJMP:
8330 : : /* This should have been lowered to the builtins below. */
8331 : 0 : gcc_unreachable ();
8332 : :
8333 : 841 : case BUILT_IN_SETJMP_SETUP:
8334 : : /* __builtin_setjmp_setup is passed a pointer to an array of five words
8335 : : and the receiver label. */
8336 : 841 : if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8337 : : {
8338 : 841 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8339 : : VOIDmode, EXPAND_NORMAL);
8340 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
8341 : 841 : rtx_insn *label_r = label_rtx (label);
8342 : :
8343 : 841 : expand_builtin_setjmp_setup (buf_addr, label_r);
8344 : 841 : return const0_rtx;
8345 : : }
8346 : : break;
8347 : :
8348 : 841 : case BUILT_IN_SETJMP_RECEIVER:
8349 : : /* __builtin_setjmp_receiver is passed the receiver label. */
8350 : 841 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8351 : : {
8352 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
8353 : 841 : rtx_insn *label_r = label_rtx (label);
8354 : :
8355 : 841 : expand_builtin_setjmp_receiver (label_r);
8356 : 841 : nonlocal_goto_handler_labels
8357 : 1682 : = gen_rtx_INSN_LIST (VOIDmode, label_r,
8358 : 841 : nonlocal_goto_handler_labels);
8359 : : /* ??? Do not let expand_label treat us as such since we would
8360 : : not want to be both on the list of non-local labels and on
8361 : : the list of forced labels. */
8362 : 841 : FORCED_LABEL (label) = 0;
8363 : 841 : return const0_rtx;
8364 : : }
8365 : : break;
8366 : :
8367 : : /* __builtin_longjmp is passed a pointer to an array of five words.
8368 : : It's similar to the C library longjmp function but works with
8369 : : __builtin_setjmp above. */
8370 : 391 : case BUILT_IN_LONGJMP:
8371 : 391 : if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8372 : : {
8373 : 391 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8374 : : VOIDmode, EXPAND_NORMAL);
8375 : 391 : rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
8376 : :
8377 : 391 : if (value != const1_rtx)
8378 : : {
8379 : 0 : error ("%<__builtin_longjmp%> second argument must be 1");
8380 : 0 : return const0_rtx;
8381 : : }
8382 : :
8383 : 391 : expand_builtin_longjmp (buf_addr, value);
8384 : 391 : return const0_rtx;
8385 : : }
8386 : : break;
8387 : :
8388 : 510 : case BUILT_IN_NONLOCAL_GOTO:
8389 : 510 : target = expand_builtin_nonlocal_goto (exp);
8390 : 510 : if (target)
8391 : : return target;
8392 : : break;
8393 : :
8394 : : /* This updates the setjmp buffer that is its argument with the value
8395 : : of the current stack pointer. */
8396 : 0 : case BUILT_IN_UPDATE_SETJMP_BUF:
8397 : 0 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8398 : : {
8399 : 0 : rtx buf_addr
8400 : 0 : = expand_normal (CALL_EXPR_ARG (exp, 0));
8401 : :
8402 : 0 : expand_builtin_update_setjmp_buf (buf_addr);
8403 : 0 : return const0_rtx;
8404 : : }
8405 : : break;
8406 : :
8407 : 36015 : case BUILT_IN_TRAP:
8408 : 36015 : case BUILT_IN_UNREACHABLE_TRAP:
8409 : 36015 : expand_builtin_trap ();
8410 : 36015 : return const0_rtx;
8411 : :
8412 : 3617 : case BUILT_IN_UNREACHABLE:
8413 : 3617 : expand_builtin_unreachable ();
8414 : 3617 : return const0_rtx;
8415 : :
8416 : 1069 : CASE_FLT_FN (BUILT_IN_SIGNBIT):
8417 : 1069 : case BUILT_IN_SIGNBITD32:
8418 : 1069 : case BUILT_IN_SIGNBITD64:
8419 : 1069 : case BUILT_IN_SIGNBITD128:
8420 : 1069 : target = expand_builtin_signbit (exp, target);
8421 : 1069 : if (target)
8422 : : return target;
8423 : : break;
8424 : :
8425 : : /* Various hooks for the DWARF 2 __throw routine. */
8426 : 34 : case BUILT_IN_UNWIND_INIT:
8427 : 34 : expand_builtin_unwind_init ();
8428 : 34 : return const0_rtx;
8429 : 1128 : case BUILT_IN_DWARF_CFA:
8430 : 1128 : return virtual_cfa_rtx;
8431 : : #ifdef DWARF2_UNWIND_INFO
8432 : 48 : case BUILT_IN_DWARF_SP_COLUMN:
8433 : 48 : return expand_builtin_dwarf_sp_column ();
8434 : 8 : case BUILT_IN_INIT_DWARF_REG_SIZES:
8435 : 8 : expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
8436 : 8 : return const0_rtx;
8437 : : #endif
8438 : 19 : case BUILT_IN_FROB_RETURN_ADDR:
8439 : 19 : return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
8440 : 2296 : case BUILT_IN_EXTRACT_RETURN_ADDR:
8441 : 2296 : return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
8442 : 29 : case BUILT_IN_EH_RETURN:
8443 : 58 : expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
8444 : 29 : CALL_EXPR_ARG (exp, 1));
8445 : 29 : return const0_rtx;
8446 : 21 : case BUILT_IN_EH_RETURN_DATA_REGNO:
8447 : 21 : return expand_builtin_eh_return_data_regno (exp);
8448 : 2 : case BUILT_IN_EXTEND_POINTER:
8449 : 2 : return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
8450 : 85856 : case BUILT_IN_EH_POINTER:
8451 : 85856 : return expand_builtin_eh_pointer (exp);
8452 : 4501 : case BUILT_IN_EH_FILTER:
8453 : 4501 : return expand_builtin_eh_filter (exp);
8454 : 95636 : case BUILT_IN_EH_COPY_VALUES:
8455 : 95636 : return expand_builtin_eh_copy_values (exp);
8456 : :
8457 : 20763 : case BUILT_IN_VA_START:
8458 : 20763 : return expand_builtin_va_start (exp);
8459 : 12071 : case BUILT_IN_VA_END:
8460 : 12071 : return expand_builtin_va_end (exp);
8461 : 218 : case BUILT_IN_VA_COPY:
8462 : 218 : return expand_builtin_va_copy (exp);
8463 : 1029 : case BUILT_IN_EXPECT:
8464 : 1029 : return expand_builtin_expect (exp, target);
8465 : 5 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
8466 : 5 : return expand_builtin_expect_with_probability (exp, target);
8467 : 75 : case BUILT_IN_ASSUME_ALIGNED:
8468 : 75 : return expand_builtin_assume_aligned (exp, target);
8469 : 1971 : case BUILT_IN_PREFETCH:
8470 : 1971 : expand_builtin_prefetch (exp);
8471 : 1971 : return const0_rtx;
8472 : :
8473 : 272 : case BUILT_IN_INIT_TRAMPOLINE:
8474 : 272 : return expand_builtin_init_trampoline (exp, true);
8475 : 0 : case BUILT_IN_INIT_HEAP_TRAMPOLINE:
8476 : 0 : return expand_builtin_init_trampoline (exp, false);
8477 : 286 : case BUILT_IN_ADJUST_TRAMPOLINE:
8478 : 286 : return expand_builtin_adjust_trampoline (exp);
8479 : :
8480 : 0 : case BUILT_IN_INIT_DESCRIPTOR:
8481 : 0 : return expand_builtin_init_descriptor (exp);
8482 : 0 : case BUILT_IN_ADJUST_DESCRIPTOR:
8483 : 0 : return expand_builtin_adjust_descriptor (exp);
8484 : :
8485 : : case BUILT_IN_GCC_NESTED_PTR_CREATED:
8486 : : case BUILT_IN_GCC_NESTED_PTR_DELETED:
8487 : : break; /* At present, no expansion, just call the function. */
8488 : :
8489 : 87 : case BUILT_IN_FORK:
8490 : 87 : case BUILT_IN_EXECL:
8491 : 87 : case BUILT_IN_EXECV:
8492 : 87 : case BUILT_IN_EXECLP:
8493 : 87 : case BUILT_IN_EXECLE:
8494 : 87 : case BUILT_IN_EXECVP:
8495 : 87 : case BUILT_IN_EXECVE:
8496 : 87 : target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
8497 : 87 : if (target)
8498 : : return target;
8499 : : break;
8500 : :
8501 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_1:
8502 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_2:
8503 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_4:
8504 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_8:
8505 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_16:
8506 : 707 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
8507 : 707 : target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
8508 : 707 : if (target)
8509 : : return target;
8510 : : break;
8511 : :
8512 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_1:
8513 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_2:
8514 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_4:
8515 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_8:
8516 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_16:
8517 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
8518 : 581 : target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
8519 : 581 : if (target)
8520 : : return target;
8521 : : break;
8522 : :
8523 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_1:
8524 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_2:
8525 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_4:
8526 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_8:
8527 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_16:
8528 : 512 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8529 : 512 : target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8530 : 512 : if (target)
8531 : : return target;
8532 : : break;
8533 : :
8534 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_1:
8535 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_2:
8536 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_4:
8537 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_8:
8538 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_16:
8539 : 488 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8540 : 488 : target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8541 : 488 : if (target)
8542 : : return target;
8543 : : break;
8544 : :
8545 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8546 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8547 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8548 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8549 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8550 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8551 : 581 : target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8552 : 581 : if (target)
8553 : : return target;
8554 : : break;
8555 : :
8556 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8557 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8558 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8559 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8560 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8561 : 434 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8562 : 434 : target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8563 : 434 : if (target)
8564 : : return target;
8565 : : break;
8566 : :
8567 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8568 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8569 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8570 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8571 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8572 : 149 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8573 : 149 : target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8574 : 149 : if (target)
8575 : : return target;
8576 : : break;
8577 : :
8578 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8579 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8580 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8581 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8582 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8583 : 117 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8584 : 117 : target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8585 : 117 : if (target)
8586 : : return target;
8587 : : break;
8588 : :
8589 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_1:
8590 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_2:
8591 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_4:
8592 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_8:
8593 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_16:
8594 : 115 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8595 : 115 : target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8596 : 115 : if (target)
8597 : : return target;
8598 : : break;
8599 : :
8600 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_1:
8601 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_2:
8602 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_4:
8603 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_8:
8604 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_16:
8605 : 116 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8606 : 116 : target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8607 : 116 : if (target)
8608 : : return target;
8609 : : break;
8610 : :
8611 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8612 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8613 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8614 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8615 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8616 : 114 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8617 : 114 : target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8618 : 114 : if (target)
8619 : : return target;
8620 : : break;
8621 : :
8622 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8623 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8624 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8625 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8626 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8627 : 83 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8628 : 83 : target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8629 : 83 : if (target)
8630 : : return target;
8631 : : break;
8632 : :
8633 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8634 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8635 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8636 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8637 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8638 : 200 : if (mode == VOIDmode)
8639 : 40 : mode = TYPE_MODE (boolean_type_node);
8640 : 200 : if (!target || !register_operand (target, mode))
8641 : 40 : target = gen_reg_rtx (mode);
8642 : :
8643 : 200 : mode = get_builtin_sync_mode
8644 : 200 : (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8645 : 200 : target = expand_builtin_compare_and_swap (mode, exp, true, target);
8646 : 200 : if (target)
8647 : : return target;
8648 : : break;
8649 : :
8650 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8651 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8652 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8653 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8654 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8655 : 256 : mode = get_builtin_sync_mode
8656 : 256 : (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8657 : 256 : target = expand_builtin_compare_and_swap (mode, exp, false, target);
8658 : 256 : if (target)
8659 : : return target;
8660 : : break;
8661 : :
8662 : 324 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8663 : 324 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8664 : 324 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8665 : 324 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8666 : 324 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8667 : 324 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8668 : 324 : target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8669 : 324 : if (target)
8670 : : return target;
8671 : : break;
8672 : :
8673 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_1:
8674 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_2:
8675 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_4:
8676 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_8:
8677 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_16:
8678 : 158 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8679 : 158 : if (expand_builtin_sync_lock_release (mode, exp))
8680 : 156 : return const0_rtx;
8681 : : break;
8682 : :
8683 : 269 : case BUILT_IN_SYNC_SYNCHRONIZE:
8684 : 269 : expand_builtin_sync_synchronize ();
8685 : 269 : return const0_rtx;
8686 : :
8687 : 2749 : case BUILT_IN_ATOMIC_EXCHANGE_1:
8688 : 2749 : case BUILT_IN_ATOMIC_EXCHANGE_2:
8689 : 2749 : case BUILT_IN_ATOMIC_EXCHANGE_4:
8690 : 2749 : case BUILT_IN_ATOMIC_EXCHANGE_8:
8691 : 2749 : case BUILT_IN_ATOMIC_EXCHANGE_16:
8692 : 2749 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8693 : 2749 : target = expand_builtin_atomic_exchange (mode, exp, target);
8694 : 2749 : if (target)
8695 : : return target;
8696 : : break;
8697 : :
8698 : 9184 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8699 : 9184 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8700 : 9184 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8701 : 9184 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8702 : 9184 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8703 : 9184 : {
8704 : 9184 : unsigned int nargs, z;
8705 : 9184 : vec<tree, va_gc> *vec;
8706 : :
8707 : 9184 : mode =
8708 : 9184 : get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8709 : 9184 : target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8710 : 9184 : if (target)
8711 : 7390 : return target;
8712 : :
8713 : : /* If this is turned into an external library call, the weak parameter
8714 : : must be dropped to match the expected parameter list. */
8715 : 1794 : nargs = call_expr_nargs (exp);
8716 : 1794 : vec_alloc (vec, nargs - 1);
8717 : 8970 : for (z = 0; z < 3; z++)
8718 : 5382 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8719 : : /* Skip the boolean weak parameter. */
8720 : 5382 : for (z = 4; z < 6; z++)
8721 : 3588 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8722 : 1794 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8723 : 1794 : break;
8724 : : }
8725 : :
8726 : 68880 : case BUILT_IN_ATOMIC_LOAD_1:
8727 : 68880 : case BUILT_IN_ATOMIC_LOAD_2:
8728 : 68880 : case BUILT_IN_ATOMIC_LOAD_4:
8729 : 68880 : case BUILT_IN_ATOMIC_LOAD_8:
8730 : 68880 : case BUILT_IN_ATOMIC_LOAD_16:
8731 : 68880 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8732 : 68880 : target = expand_builtin_atomic_load (mode, exp, target);
8733 : 68880 : if (target)
8734 : : return target;
8735 : : break;
8736 : :
8737 : 17255 : case BUILT_IN_ATOMIC_STORE_1:
8738 : 17255 : case BUILT_IN_ATOMIC_STORE_2:
8739 : 17255 : case BUILT_IN_ATOMIC_STORE_4:
8740 : 17255 : case BUILT_IN_ATOMIC_STORE_8:
8741 : 17255 : case BUILT_IN_ATOMIC_STORE_16:
8742 : 17255 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8743 : 17255 : target = expand_builtin_atomic_store (mode, exp);
8744 : 17255 : if (target)
8745 : 15628 : return const0_rtx;
8746 : : break;
8747 : :
8748 : 3905 : case BUILT_IN_ATOMIC_ADD_FETCH_1:
8749 : 3905 : case BUILT_IN_ATOMIC_ADD_FETCH_2:
8750 : 3905 : case BUILT_IN_ATOMIC_ADD_FETCH_4:
8751 : 3905 : case BUILT_IN_ATOMIC_ADD_FETCH_8:
8752 : 3905 : case BUILT_IN_ATOMIC_ADD_FETCH_16:
8753 : 3905 : {
8754 : 3905 : enum built_in_function lib;
8755 : 3905 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8756 : 3905 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8757 : : (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8758 : 3905 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8759 : : ignore, lib);
8760 : 3905 : if (target)
8761 : : return target;
8762 : : break;
8763 : : }
8764 : 2154 : case BUILT_IN_ATOMIC_SUB_FETCH_1:
8765 : 2154 : case BUILT_IN_ATOMIC_SUB_FETCH_2:
8766 : 2154 : case BUILT_IN_ATOMIC_SUB_FETCH_4:
8767 : 2154 : case BUILT_IN_ATOMIC_SUB_FETCH_8:
8768 : 2154 : case BUILT_IN_ATOMIC_SUB_FETCH_16:
8769 : 2154 : {
8770 : 2154 : enum built_in_function lib;
8771 : 2154 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8772 : 2154 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8773 : : (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8774 : 2154 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8775 : : ignore, lib);
8776 : 2154 : if (target)
8777 : : return target;
8778 : : break;
8779 : : }
8780 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_1:
8781 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_2:
8782 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_4:
8783 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_8:
8784 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_16:
8785 : 891 : {
8786 : 891 : enum built_in_function lib;
8787 : 891 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8788 : 891 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8789 : : (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8790 : 891 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8791 : : ignore, lib);
8792 : 891 : if (target)
8793 : : return target;
8794 : : break;
8795 : : }
8796 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_1:
8797 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_2:
8798 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_4:
8799 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_8:
8800 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_16:
8801 : 155 : {
8802 : 155 : enum built_in_function lib;
8803 : 155 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8804 : 155 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8805 : : (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8806 : 155 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8807 : : ignore, lib);
8808 : 155 : if (target)
8809 : : return target;
8810 : : break;
8811 : : }
8812 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_1:
8813 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_2:
8814 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_4:
8815 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_8:
8816 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_16:
8817 : 836 : {
8818 : 836 : enum built_in_function lib;
8819 : 836 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8820 : 836 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8821 : : (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8822 : 836 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8823 : : ignore, lib);
8824 : 836 : if (target)
8825 : : return target;
8826 : : break;
8827 : : }
8828 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_1:
8829 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_2:
8830 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_4:
8831 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_8:
8832 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_16:
8833 : 953 : {
8834 : 953 : enum built_in_function lib;
8835 : 953 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8836 : 953 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8837 : : (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8838 : 953 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8839 : : ignore, lib);
8840 : 953 : if (target)
8841 : : return target;
8842 : : break;
8843 : : }
8844 : 15336 : case BUILT_IN_ATOMIC_FETCH_ADD_1:
8845 : 15336 : case BUILT_IN_ATOMIC_FETCH_ADD_2:
8846 : 15336 : case BUILT_IN_ATOMIC_FETCH_ADD_4:
8847 : 15336 : case BUILT_IN_ATOMIC_FETCH_ADD_8:
8848 : 15336 : case BUILT_IN_ATOMIC_FETCH_ADD_16:
8849 : 15336 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8850 : 15336 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8851 : : ignore, BUILT_IN_NONE);
8852 : 15336 : if (target)
8853 : : return target;
8854 : : break;
8855 : :
8856 : 2484 : case BUILT_IN_ATOMIC_FETCH_SUB_1:
8857 : 2484 : case BUILT_IN_ATOMIC_FETCH_SUB_2:
8858 : 2484 : case BUILT_IN_ATOMIC_FETCH_SUB_4:
8859 : 2484 : case BUILT_IN_ATOMIC_FETCH_SUB_8:
8860 : 2484 : case BUILT_IN_ATOMIC_FETCH_SUB_16:
8861 : 2484 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8862 : 2484 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8863 : : ignore, BUILT_IN_NONE);
8864 : 2484 : if (target)
8865 : : return target;
8866 : : break;
8867 : :
8868 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_1:
8869 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_2:
8870 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_4:
8871 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_8:
8872 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_16:
8873 : 772 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8874 : 772 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8875 : : ignore, BUILT_IN_NONE);
8876 : 772 : if (target)
8877 : : return target;
8878 : : break;
8879 : :
8880 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_1:
8881 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_2:
8882 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_4:
8883 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_8:
8884 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_16:
8885 : 118 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8886 : 118 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8887 : : ignore, BUILT_IN_NONE);
8888 : 118 : if (target)
8889 : : return target;
8890 : : break;
8891 : :
8892 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_1:
8893 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_2:
8894 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_4:
8895 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_8:
8896 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_16:
8897 : 862 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8898 : 862 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8899 : : ignore, BUILT_IN_NONE);
8900 : 862 : if (target)
8901 : : return target;
8902 : : break;
8903 : :
8904 : 1058 : case BUILT_IN_ATOMIC_FETCH_OR_1:
8905 : 1058 : case BUILT_IN_ATOMIC_FETCH_OR_2:
8906 : 1058 : case BUILT_IN_ATOMIC_FETCH_OR_4:
8907 : 1058 : case BUILT_IN_ATOMIC_FETCH_OR_8:
8908 : 1058 : case BUILT_IN_ATOMIC_FETCH_OR_16:
8909 : 1058 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8910 : 1058 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8911 : : ignore, BUILT_IN_NONE);
8912 : 1058 : if (target)
8913 : : return target;
8914 : : break;
8915 : :
8916 : 260 : case BUILT_IN_ATOMIC_TEST_AND_SET:
8917 : 260 : target = expand_builtin_atomic_test_and_set (exp, target);
8918 : 260 : if (target)
8919 : : return target;
8920 : : break;
8921 : :
8922 : 57 : case BUILT_IN_ATOMIC_CLEAR:
8923 : 57 : return expand_builtin_atomic_clear (exp);
8924 : :
8925 : 1 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8926 : 1 : return expand_builtin_atomic_always_lock_free (exp);
8927 : :
8928 : 3 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8929 : 3 : target = expand_builtin_atomic_is_lock_free (exp);
8930 : 3 : if (target)
8931 : : return target;
8932 : : break;
8933 : :
8934 : 693 : case BUILT_IN_ATOMIC_THREAD_FENCE:
8935 : 693 : expand_builtin_atomic_thread_fence (exp);
8936 : 693 : return const0_rtx;
8937 : :
8938 : 60 : case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8939 : 60 : expand_builtin_atomic_signal_fence (exp);
8940 : 60 : return const0_rtx;
8941 : :
8942 : 616 : case BUILT_IN_OBJECT_SIZE:
8943 : 616 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8944 : 616 : return expand_builtin_object_size (exp);
8945 : :
8946 : 835 : case BUILT_IN_MEMCPY_CHK:
8947 : 835 : case BUILT_IN_MEMPCPY_CHK:
8948 : 835 : case BUILT_IN_MEMMOVE_CHK:
8949 : 835 : case BUILT_IN_MEMSET_CHK:
8950 : 835 : target = expand_builtin_memory_chk (exp, target, mode, fcode);
8951 : 835 : if (target)
8952 : : return target;
8953 : : break;
8954 : :
8955 : 1135 : case BUILT_IN_STRCPY_CHK:
8956 : 1135 : case BUILT_IN_STPCPY_CHK:
8957 : 1135 : case BUILT_IN_STRNCPY_CHK:
8958 : 1135 : case BUILT_IN_STPNCPY_CHK:
8959 : 1135 : case BUILT_IN_STRCAT_CHK:
8960 : 1135 : case BUILT_IN_STRNCAT_CHK:
8961 : 1135 : case BUILT_IN_SNPRINTF_CHK:
8962 : 1135 : case BUILT_IN_VSNPRINTF_CHK:
8963 : 1135 : maybe_emit_chk_warning (exp, fcode);
8964 : 1135 : break;
8965 : :
8966 : 1329 : case BUILT_IN_SPRINTF_CHK:
8967 : 1329 : case BUILT_IN_VSPRINTF_CHK:
8968 : 1329 : maybe_emit_sprintf_chk_warning (exp, fcode);
8969 : 1329 : break;
8970 : :
8971 : 3 : case BUILT_IN_THREAD_POINTER:
8972 : 3 : return expand_builtin_thread_pointer (exp, target);
8973 : :
8974 : 0 : case BUILT_IN_SET_THREAD_POINTER:
8975 : 0 : expand_builtin_set_thread_pointer (exp);
8976 : 0 : return const0_rtx;
8977 : :
8978 : : case BUILT_IN_ACC_ON_DEVICE:
8979 : : /* Do library call, if we failed to expand the builtin when
8980 : : folding. */
8981 : : break;
8982 : :
8983 : 356 : case BUILT_IN_GOACC_PARLEVEL_ID:
8984 : 356 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
8985 : 356 : return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8986 : :
8987 : 4 : case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8988 : 4 : return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8989 : :
8990 : 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8991 : 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8992 : 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8993 : 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8994 : 31 : case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8995 : 31 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8996 : 31 : return expand_speculation_safe_value (mode, exp, target, ignore);
8997 : :
8998 : 3 : case BUILT_IN_CRC8_DATA8:
8999 : 3 : return expand_builtin_crc_table_based (IFN_CRC, QImode, QImode, mode,
9000 : : exp, target);
9001 : 2 : case BUILT_IN_CRC16_DATA8:
9002 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, QImode, mode,
9003 : : exp, target);
9004 : 2 : case BUILT_IN_CRC16_DATA16:
9005 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, HImode, mode,
9006 : : exp, target);
9007 : 2 : case BUILT_IN_CRC32_DATA8:
9008 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, QImode, mode,
9009 : : exp, target);
9010 : 2 : case BUILT_IN_CRC32_DATA16:
9011 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, HImode, mode,
9012 : : exp, target);
9013 : 2 : case BUILT_IN_CRC32_DATA32:
9014 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, SImode, mode,
9015 : : exp, target);
9016 : 1 : case BUILT_IN_CRC64_DATA8:
9017 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, QImode, mode,
9018 : : exp, target);
9019 : 1 : case BUILT_IN_CRC64_DATA16:
9020 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, HImode, mode,
9021 : : exp, target);
9022 : 1 : case BUILT_IN_CRC64_DATA32:
9023 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, SImode, mode,
9024 : : exp, target);
9025 : 1 : case BUILT_IN_CRC64_DATA64:
9026 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, DImode, mode,
9027 : : exp, target);
9028 : 2 : case BUILT_IN_REV_CRC8_DATA8:
9029 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, QImode, QImode,
9030 : : mode, exp, target);
9031 : 2 : case BUILT_IN_REV_CRC16_DATA8:
9032 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, QImode,
9033 : : mode, exp, target);
9034 : 2 : case BUILT_IN_REV_CRC16_DATA16:
9035 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, HImode,
9036 : : mode, exp, target);
9037 : 2 : case BUILT_IN_REV_CRC32_DATA8:
9038 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, QImode,
9039 : : mode, exp, target);
9040 : 2 : case BUILT_IN_REV_CRC32_DATA16:
9041 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, HImode,
9042 : : mode, exp, target);
9043 : 2 : case BUILT_IN_REV_CRC32_DATA32:
9044 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, SImode,
9045 : : mode, exp, target);
9046 : 1 : case BUILT_IN_REV_CRC64_DATA8:
9047 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, QImode,
9048 : : mode, exp, target);
9049 : 1 : case BUILT_IN_REV_CRC64_DATA16:
9050 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, HImode,
9051 : : mode, exp, target);
9052 : 1 : case BUILT_IN_REV_CRC64_DATA32:
9053 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, SImode,
9054 : : mode, exp, target);
9055 : 1 : case BUILT_IN_REV_CRC64_DATA64:
9056 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, DImode,
9057 : : mode, exp, target);
9058 : : default: /* just do library call, if unknown builtin */
9059 : : break;
9060 : : }
9061 : :
9062 : : /* The switch statement above can drop through to cause the function
9063 : : to be called normally. */
9064 : 821398 : return expand_call (exp, target, ignore);
9065 : : }
9066 : :
9067 : : /* Determine whether a tree node represents a call to a built-in
9068 : : function. If the tree T is a call to a built-in function with
9069 : : the right number of arguments of the appropriate types, return
9070 : : the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
9071 : : Otherwise the return value is END_BUILTINS. */
9072 : :
9073 : : enum built_in_function
9074 : 88888361 : builtin_mathfn_code (const_tree t)
9075 : : {
9076 : 88888361 : const_tree fndecl, arg, parmlist;
9077 : 88888361 : const_tree argtype, parmtype;
9078 : 88888361 : const_call_expr_arg_iterator iter;
9079 : :
9080 : 88888361 : if (TREE_CODE (t) != CALL_EXPR)
9081 : : return END_BUILTINS;
9082 : :
9083 : 1199205 : fndecl = get_callee_fndecl (t);
9084 : 1199205 : if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9085 : : return END_BUILTINS;
9086 : :
9087 : 683841 : parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
9088 : 683841 : init_const_call_expr_arg_iterator (t, &iter);
9089 : 2231701 : for (; parmlist; parmlist = TREE_CHAIN (parmlist))
9090 : : {
9091 : : /* If a function doesn't take a variable number of arguments,
9092 : : the last element in the list will have type `void'. */
9093 : 1547651 : parmtype = TREE_VALUE (parmlist);
9094 : 1547651 : if (VOID_TYPE_P (parmtype))
9095 : : {
9096 : 683618 : if (more_const_call_expr_args_p (&iter))
9097 : : return END_BUILTINS;
9098 : 683618 : return DECL_FUNCTION_CODE (fndecl);
9099 : : }
9100 : :
9101 : 864033 : if (! more_const_call_expr_args_p (&iter))
9102 : : return END_BUILTINS;
9103 : :
9104 : 864033 : arg = next_const_call_expr_arg (&iter);
9105 : 864033 : argtype = TREE_TYPE (arg);
9106 : :
9107 : 864033 : if (SCALAR_FLOAT_TYPE_P (parmtype))
9108 : : {
9109 : 628262 : if (! SCALAR_FLOAT_TYPE_P (argtype))
9110 : : return END_BUILTINS;
9111 : : }
9112 : 235771 : else if (COMPLEX_FLOAT_TYPE_P (parmtype))
9113 : : {
9114 : 9681 : if (! COMPLEX_FLOAT_TYPE_P (argtype))
9115 : : return END_BUILTINS;
9116 : : }
9117 : 226090 : else if (POINTER_TYPE_P (parmtype))
9118 : : {
9119 : 64459 : if (! POINTER_TYPE_P (argtype))
9120 : : return END_BUILTINS;
9121 : : }
9122 : 161631 : else if (INTEGRAL_TYPE_P (parmtype))
9123 : : {
9124 : 161631 : if (! INTEGRAL_TYPE_P (argtype))
9125 : : return END_BUILTINS;
9126 : : }
9127 : : else
9128 : : return END_BUILTINS;
9129 : : }
9130 : :
9131 : : /* Variable-length argument list. */
9132 : 209 : return DECL_FUNCTION_CODE (fndecl);
9133 : : }
9134 : :
9135 : : /* Fold a call to __builtin_constant_p, if we know its argument ARG will
9136 : : evaluate to a constant. */
9137 : :
9138 : : static tree
9139 : 699603 : fold_builtin_constant_p (tree arg)
9140 : : {
9141 : : /* We return 1 for a numeric type that's known to be a constant
9142 : : value at compile-time or for an aggregate type that's a
9143 : : literal constant. */
9144 : 699603 : STRIP_NOPS (arg);
9145 : :
9146 : : /* If we know this is a constant, emit the constant of one. */
9147 : 699603 : if (CONSTANT_CLASS_P (arg)
9148 : 699603 : || (TREE_CODE (arg) == CONSTRUCTOR
9149 : 6 : && TREE_CONSTANT (arg)))
9150 : 19157 : return integer_one_node;
9151 : 680446 : if (TREE_CODE (arg) == ADDR_EXPR)
9152 : : {
9153 : 110 : tree op = TREE_OPERAND (arg, 0);
9154 : 110 : if (TREE_CODE (op) == STRING_CST
9155 : 110 : || (TREE_CODE (op) == ARRAY_REF
9156 : 55 : && integer_zerop (TREE_OPERAND (op, 1))
9157 : 55 : && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
9158 : 85 : return integer_one_node;
9159 : : }
9160 : :
9161 : : /* If this expression has side effects, show we don't know it to be a
9162 : : constant. Likewise if it's a pointer or aggregate type since in
9163 : : those case we only want literals, since those are only optimized
9164 : : when generating RTL, not later.
9165 : : And finally, if we are compiling an initializer, not code, we
9166 : : need to return a definite result now; there's not going to be any
9167 : : more optimization done. */
9168 : 680361 : if (TREE_SIDE_EFFECTS (arg)
9169 : 680246 : || AGGREGATE_TYPE_P (TREE_TYPE (arg))
9170 : 680246 : || POINTER_TYPE_P (TREE_TYPE (arg))
9171 : 680077 : || cfun == 0
9172 : 680018 : || folding_initializer
9173 : 1360372 : || force_folding_builtin_constant_p)
9174 : 368 : return integer_zero_node;
9175 : :
9176 : : return NULL_TREE;
9177 : : }
9178 : :
9179 : : /* Create builtin_expect or builtin_expect_with_probability
9180 : : with PRED and EXPECTED as its arguments and return it as a truthvalue.
9181 : : Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
9182 : : builtin_expect_with_probability instead uses third argument as PROBABILITY
9183 : : value. */
9184 : :
9185 : : static tree
9186 : 39986 : build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
9187 : : tree predictor, tree probability)
9188 : : {
9189 : 39986 : tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
9190 : :
9191 : 39986 : fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
9192 : : : BUILT_IN_EXPECT_WITH_PROBABILITY);
9193 : 39986 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
9194 : 39986 : ret_type = TREE_TYPE (TREE_TYPE (fn));
9195 : 39986 : pred_type = TREE_VALUE (arg_types);
9196 : 39986 : expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
9197 : :
9198 : 39986 : pred = fold_convert_loc (loc, pred_type, pred);
9199 : 39986 : expected = fold_convert_loc (loc, expected_type, expected);
9200 : :
9201 : 39986 : if (probability)
9202 : 0 : call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
9203 : : else
9204 : 79972 : call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
9205 : : predictor);
9206 : :
9207 : 39986 : return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
9208 : 39986 : build_int_cst (ret_type, 0));
9209 : : }
9210 : :
9211 : : /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
9212 : : NULL_TREE if no simplification is possible. */
9213 : :
9214 : : tree
9215 : 5102623 : fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
9216 : : tree arg3)
9217 : : {
9218 : 5102623 : tree inner, fndecl, inner_arg0;
9219 : 5102623 : enum tree_code code;
9220 : :
9221 : : /* Distribute the expected value over short-circuiting operators.
9222 : : See through the cast from truthvalue_type_node to long. */
9223 : 5102623 : inner_arg0 = arg0;
9224 : 10321385 : while (CONVERT_EXPR_P (inner_arg0)
9225 : 372885 : && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
9226 : 5848392 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
9227 : 372884 : inner_arg0 = TREE_OPERAND (inner_arg0, 0);
9228 : :
9229 : : /* If this is a builtin_expect within a builtin_expect keep the
9230 : : inner one. See through a comparison against a constant. It
9231 : : might have been added to create a thruthvalue. */
9232 : 5102623 : inner = inner_arg0;
9233 : :
9234 : 5102623 : if (COMPARISON_CLASS_P (inner)
9235 : 5102623 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
9236 : 892166 : inner = TREE_OPERAND (inner, 0);
9237 : :
9238 : 5102623 : if (TREE_CODE (inner) == CALL_EXPR
9239 : 51421 : && (fndecl = get_callee_fndecl (inner))
9240 : 5154044 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
9241 : : BUILT_IN_EXPECT_WITH_PROBABILITY))
9242 : : return arg0;
9243 : :
9244 : 5102615 : inner = inner_arg0;
9245 : 5102615 : code = TREE_CODE (inner);
9246 : 5102615 : if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
9247 : : {
9248 : 19993 : tree op0 = TREE_OPERAND (inner, 0);
9249 : 19993 : tree op1 = TREE_OPERAND (inner, 1);
9250 : 19993 : arg1 = save_expr (arg1);
9251 : :
9252 : 19993 : op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
9253 : 19993 : op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
9254 : 19993 : inner = build2 (code, TREE_TYPE (inner), op0, op1);
9255 : :
9256 : 19993 : return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
9257 : : }
9258 : :
9259 : : /* If the argument isn't invariant then there's nothing else we can do. */
9260 : 5082622 : if (!TREE_CONSTANT (inner_arg0))
9261 : : return NULL_TREE;
9262 : :
9263 : : /* If we expect that a comparison against the argument will fold to
9264 : : a constant return the constant. In practice, this means a true
9265 : : constant or the address of a non-weak symbol. */
9266 : 52505 : inner = inner_arg0;
9267 : 52505 : STRIP_NOPS (inner);
9268 : 52505 : if (TREE_CODE (inner) == ADDR_EXPR)
9269 : : {
9270 : 1 : do
9271 : : {
9272 : 1 : inner = TREE_OPERAND (inner, 0);
9273 : : }
9274 : 1 : while (TREE_CODE (inner) == COMPONENT_REF
9275 : 1 : || TREE_CODE (inner) == ARRAY_REF);
9276 : 1 : if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
9277 : : return NULL_TREE;
9278 : : }
9279 : :
9280 : : /* Otherwise, ARG0 already has the proper type for the return value. */
9281 : : return arg0;
9282 : : }
9283 : :
9284 : : /* Fold a call to __builtin_classify_type with argument ARG. */
9285 : :
9286 : : static tree
9287 : 2304 : fold_builtin_classify_type (tree arg)
9288 : : {
9289 : 2304 : if (arg == 0)
9290 : 0 : return build_int_cst (integer_type_node, no_type_class);
9291 : :
9292 : 2304 : return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
9293 : : }
9294 : :
9295 : : /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
9296 : : ARG. */
9297 : :
9298 : : static tree
9299 : 404579 : fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
9300 : : {
9301 : 404579 : if (!validate_arg (arg, POINTER_TYPE))
9302 : : return NULL_TREE;
9303 : : else
9304 : : {
9305 : 404576 : c_strlen_data lendata = { };
9306 : 404576 : tree len = c_strlen (arg, 0, &lendata);
9307 : :
9308 : 404576 : if (len)
9309 : 643 : return fold_convert_loc (loc, type, len);
9310 : :
9311 : : /* TODO: Move this to gimple-ssa-warn-access once the pass runs
9312 : : also early enough to detect invalid reads in multimensional
9313 : : arrays and struct members. */
9314 : 403933 : if (!lendata.decl)
9315 : 396806 : c_strlen (arg, 1, &lendata);
9316 : :
9317 : 403933 : if (lendata.decl)
9318 : : {
9319 : 7133 : if (EXPR_HAS_LOCATION (arg))
9320 : 2812 : loc = EXPR_LOCATION (arg);
9321 : 4321 : else if (loc == UNKNOWN_LOCATION)
9322 : 0 : loc = input_location;
9323 : 7133 : warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
9324 : : }
9325 : :
9326 : 403933 : return NULL_TREE;
9327 : : }
9328 : : }
9329 : :
9330 : : /* Fold a call to __builtin_inf or __builtin_huge_val. */
9331 : :
9332 : : static tree
9333 : 232315 : fold_builtin_inf (location_t loc, tree type, int warn)
9334 : : {
9335 : : /* __builtin_inff is intended to be usable to define INFINITY on all
9336 : : targets. If an infinity is not available, INFINITY expands "to a
9337 : : positive constant of type float that overflows at translation
9338 : : time", footnote "In this case, using INFINITY will violate the
9339 : : constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
9340 : : Thus we pedwarn to ensure this constraint violation is
9341 : : diagnosed. */
9342 : 928243 : if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
9343 : 0 : pedwarn (loc, 0, "target format does not support infinity");
9344 : :
9345 : 232315 : return build_real (type, dconstinf);
9346 : : }
9347 : :
9348 : : /* Fold function call to builtin sincos, sincosf, or sincosl. Return
9349 : : NULL_TREE if no simplification can be made. */
9350 : :
9351 : : static tree
9352 : 146 : fold_builtin_sincos (location_t loc,
9353 : : tree arg0, tree arg1, tree arg2)
9354 : : {
9355 : 146 : tree type;
9356 : 146 : tree fndecl, call = NULL_TREE;
9357 : :
9358 : 146 : if (!validate_arg (arg0, REAL_TYPE)
9359 : 146 : || !validate_arg (arg1, POINTER_TYPE)
9360 : 292 : || !validate_arg (arg2, POINTER_TYPE))
9361 : : return NULL_TREE;
9362 : :
9363 : 146 : type = TREE_TYPE (arg0);
9364 : :
9365 : : /* Calculate the result when the argument is a constant. */
9366 : 146 : built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
9367 : 146 : if (fn == END_BUILTINS)
9368 : : return NULL_TREE;
9369 : :
9370 : : /* Canonicalize sincos to cexpi. */
9371 : 146 : if (TREE_CODE (arg0) == REAL_CST)
9372 : : {
9373 : 88 : tree complex_type = build_complex_type (type);
9374 : 88 : call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
9375 : : }
9376 : 88 : if (!call)
9377 : : {
9378 : 58 : if (!targetm.libc_has_function (function_c99_math_complex, type)
9379 : 58 : || !builtin_decl_implicit_p (fn))
9380 : : return NULL_TREE;
9381 : 58 : fndecl = builtin_decl_explicit (fn);
9382 : 58 : call = build_call_expr_loc (loc, fndecl, 1, arg0);
9383 : 58 : call = builtin_save_expr (call);
9384 : : }
9385 : :
9386 : 146 : tree ptype = build_pointer_type (type);
9387 : 146 : arg1 = fold_convert (ptype, arg1);
9388 : 146 : arg2 = fold_convert (ptype, arg2);
9389 : 146 : return build2 (COMPOUND_EXPR, void_type_node,
9390 : : build2 (MODIFY_EXPR, void_type_node,
9391 : : build_fold_indirect_ref_loc (loc, arg1),
9392 : : fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
9393 : : build2 (MODIFY_EXPR, void_type_node,
9394 : : build_fold_indirect_ref_loc (loc, arg2),
9395 : 146 : fold_build1_loc (loc, REALPART_EXPR, type, call)));
9396 : : }
9397 : :
9398 : : /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9399 : : Return NULL_TREE if no simplification can be made. */
9400 : :
9401 : : static tree
9402 : 2379144 : fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9403 : : {
9404 : 2379144 : if (!validate_arg (arg1, POINTER_TYPE)
9405 : 2379144 : || !validate_arg (arg2, POINTER_TYPE)
9406 : 4758288 : || !validate_arg (len, INTEGER_TYPE))
9407 : : return NULL_TREE;
9408 : :
9409 : : /* If the LEN parameter is zero, return zero. */
9410 : 2379144 : if (integer_zerop (len))
9411 : 0 : return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9412 : 0 : arg1, arg2);
9413 : :
9414 : : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9415 : 2379144 : if (operand_equal_p (arg1, arg2, 0))
9416 : 686 : return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9417 : :
9418 : : /* If len parameter is one, return an expression corresponding to
9419 : : (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9420 : 2378458 : if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9421 : : {
9422 : 17782 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9423 : 17782 : tree cst_uchar_ptr_node
9424 : 17782 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9425 : :
9426 : 17782 : tree ind1
9427 : 17782 : = fold_convert_loc (loc, integer_type_node,
9428 : : build1 (INDIRECT_REF, cst_uchar_node,
9429 : : fold_convert_loc (loc,
9430 : : cst_uchar_ptr_node,
9431 : : arg1)));
9432 : 17782 : tree ind2
9433 : 17782 : = fold_convert_loc (loc, integer_type_node,
9434 : : build1 (INDIRECT_REF, cst_uchar_node,
9435 : : fold_convert_loc (loc,
9436 : : cst_uchar_ptr_node,
9437 : : arg2)));
9438 : 17782 : return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9439 : : }
9440 : :
9441 : : return NULL_TREE;
9442 : : }
9443 : :
9444 : : /* Fold a call to builtin isascii with argument ARG. */
9445 : :
9446 : : static tree
9447 : 211 : fold_builtin_isascii (location_t loc, tree arg)
9448 : : {
9449 : 211 : if (!validate_arg (arg, INTEGER_TYPE))
9450 : : return NULL_TREE;
9451 : : else
9452 : : {
9453 : : /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9454 : 211 : arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9455 : : build_int_cst (integer_type_node,
9456 : : ~ HOST_WIDE_INT_UC (0x7f)));
9457 : 211 : return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9458 : 211 : arg, integer_zero_node);
9459 : : }
9460 : : }
9461 : :
9462 : : /* Fold a call to builtin toascii with argument ARG. */
9463 : :
9464 : : static tree
9465 : 168 : fold_builtin_toascii (location_t loc, tree arg)
9466 : : {
9467 : 168 : if (!validate_arg (arg, INTEGER_TYPE))
9468 : : return NULL_TREE;
9469 : :
9470 : : /* Transform toascii(c) -> (c & 0x7f). */
9471 : 168 : return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9472 : 168 : build_int_cst (integer_type_node, 0x7f));
9473 : : }
9474 : :
9475 : : /* Fold a call to builtin isdigit with argument ARG. */
9476 : :
9477 : : static tree
9478 : 325 : fold_builtin_isdigit (location_t loc, tree arg)
9479 : : {
9480 : 325 : if (!validate_arg (arg, INTEGER_TYPE))
9481 : : return NULL_TREE;
9482 : : else
9483 : : {
9484 : : /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9485 : : /* According to the C standard, isdigit is unaffected by locale.
9486 : : However, it definitely is affected by the target character set. */
9487 : 313 : unsigned HOST_WIDE_INT target_digit0
9488 : 313 : = lang_hooks.to_target_charset ('0');
9489 : :
9490 : 313 : if (target_digit0 == 0)
9491 : : return NULL_TREE;
9492 : :
9493 : 313 : arg = fold_convert_loc (loc, unsigned_type_node, arg);
9494 : 313 : arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9495 : : build_int_cst (unsigned_type_node, target_digit0));
9496 : 313 : return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9497 : 313 : build_int_cst (unsigned_type_node, 9));
9498 : : }
9499 : : }
9500 : :
9501 : : /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9502 : :
9503 : : static tree
9504 : 327342 : fold_builtin_fabs (location_t loc, tree arg, tree type)
9505 : : {
9506 : 327342 : if (!validate_arg (arg, REAL_TYPE))
9507 : : return NULL_TREE;
9508 : :
9509 : 327257 : arg = fold_convert_loc (loc, type, arg);
9510 : 327257 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9511 : : }
9512 : :
9513 : : /* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
9514 : : with argument ARG. */
9515 : :
9516 : : static tree
9517 : 90295 : fold_builtin_abs (location_t loc, tree arg, tree type)
9518 : : {
9519 : 90295 : if (!validate_arg (arg, INTEGER_TYPE))
9520 : : return NULL_TREE;
9521 : :
9522 : 90268 : if (TYPE_UNSIGNED (type))
9523 : : {
9524 : 1168 : if (TYPE_PRECISION (TREE_TYPE (arg))
9525 : 1168 : != TYPE_PRECISION (type)
9526 : 1168 : || TYPE_UNSIGNED (TREE_TYPE (arg)))
9527 : : return NULL_TREE;
9528 : 1168 : return fold_build1_loc (loc, ABSU_EXPR, type, arg);
9529 : : }
9530 : 89100 : arg = fold_convert_loc (loc, type, arg);
9531 : 89100 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9532 : : }
9533 : :
9534 : : /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9535 : :
9536 : : static tree
9537 : 107935 : fold_builtin_carg (location_t loc, tree arg, tree type)
9538 : : {
9539 : 107935 : if (validate_arg (arg, COMPLEX_TYPE)
9540 : 107935 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
9541 : : {
9542 : 107935 : tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9543 : :
9544 : 107935 : if (atan2_fn)
9545 : : {
9546 : 104251 : tree new_arg = builtin_save_expr (arg);
9547 : 104251 : tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9548 : 104251 : tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9549 : 104251 : return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9550 : : }
9551 : : }
9552 : :
9553 : : return NULL_TREE;
9554 : : }
9555 : :
9556 : : /* Fold a call to builtin frexp, we can assume the base is 2. */
9557 : :
9558 : : static tree
9559 : 105381 : fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9560 : : {
9561 : 105381 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9562 : : return NULL_TREE;
9563 : :
9564 : 105381 : STRIP_NOPS (arg0);
9565 : :
9566 : 105381 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9567 : : return NULL_TREE;
9568 : :
9569 : 2206 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9570 : :
9571 : : /* Proceed if a valid pointer type was passed in. */
9572 : 2206 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9573 : : {
9574 : 2206 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9575 : 2206 : tree frac, exp, res;
9576 : :
9577 : 2206 : switch (value->cl)
9578 : : {
9579 : 276 : case rvc_zero:
9580 : 276 : case rvc_nan:
9581 : 276 : case rvc_inf:
9582 : : /* For +-0, return (*exp = 0, +-0). */
9583 : : /* For +-NaN or +-Inf, *exp is unspecified, but something should
9584 : : be stored there so that it isn't read from uninitialized object.
9585 : : As glibc and newlib store *exp = 0 for +-Inf/NaN, storing
9586 : : 0 here as well is easiest. */
9587 : 276 : exp = integer_zero_node;
9588 : 276 : frac = arg0;
9589 : 276 : break;
9590 : 1930 : case rvc_normal:
9591 : 1930 : {
9592 : : /* Since the frexp function always expects base 2, and in
9593 : : GCC normalized significands are already in the range
9594 : : [0.5, 1.0), we have exactly what frexp wants. */
9595 : 1930 : REAL_VALUE_TYPE frac_rvt = *value;
9596 : 1930 : SET_REAL_EXP (&frac_rvt, 0);
9597 : 1930 : frac = build_real (rettype, frac_rvt);
9598 : 1930 : exp = build_int_cst (integer_type_node, REAL_EXP (value));
9599 : : }
9600 : 1930 : break;
9601 : 0 : default:
9602 : 0 : gcc_unreachable ();
9603 : : }
9604 : :
9605 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9606 : 2206 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9607 : 2206 : TREE_SIDE_EFFECTS (arg1) = 1;
9608 : 2206 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9609 : 2206 : suppress_warning (res, OPT_Wunused_value);
9610 : 2206 : return res;
9611 : : }
9612 : :
9613 : : return NULL_TREE;
9614 : : }
9615 : :
9616 : : /* Fold a call to builtin modf. */
9617 : :
9618 : : static tree
9619 : 71535 : fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9620 : : {
9621 : 71535 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9622 : : return NULL_TREE;
9623 : :
9624 : 71535 : STRIP_NOPS (arg0);
9625 : :
9626 : 71535 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9627 : : return NULL_TREE;
9628 : :
9629 : 2476 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9630 : :
9631 : : /* Proceed if a valid pointer type was passed in. */
9632 : 2476 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9633 : : {
9634 : 2476 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9635 : 2476 : REAL_VALUE_TYPE trunc, frac;
9636 : 2476 : tree res;
9637 : :
9638 : 2476 : switch (value->cl)
9639 : : {
9640 : 228 : case rvc_nan:
9641 : 228 : case rvc_zero:
9642 : : /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9643 : 228 : trunc = frac = *value;
9644 : 228 : break;
9645 : 96 : case rvc_inf:
9646 : : /* For +-Inf, return (*arg1 = arg0, +-0). */
9647 : 96 : frac = dconst0;
9648 : 96 : frac.sign = value->sign;
9649 : 96 : trunc = *value;
9650 : 96 : break;
9651 : 2152 : case rvc_normal:
9652 : : /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9653 : 2152 : real_trunc (&trunc, VOIDmode, value);
9654 : 2152 : real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9655 : : /* If the original number was negative and already
9656 : : integral, then the fractional part is -0.0. */
9657 : 2152 : if (value->sign && frac.cl == rvc_zero)
9658 : 69 : frac.sign = value->sign;
9659 : : break;
9660 : : }
9661 : :
9662 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9663 : 2476 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9664 : : build_real (rettype, trunc));
9665 : 2476 : TREE_SIDE_EFFECTS (arg1) = 1;
9666 : 2476 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9667 : : build_real (rettype, frac));
9668 : 2476 : suppress_warning (res, OPT_Wunused_value);
9669 : 2476 : return res;
9670 : : }
9671 : :
9672 : : return NULL_TREE;
9673 : : }
9674 : :
9675 : : /* Given a location LOC, an interclass builtin function decl FNDECL
9676 : : and its single argument ARG, return an folded expression computing
9677 : : the same, or NULL_TREE if we either couldn't or didn't want to fold
9678 : : (the latter happen if there's an RTL instruction available). */
9679 : :
9680 : : static tree
9681 : 1020113 : fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9682 : : {
9683 : 1020113 : machine_mode mode;
9684 : :
9685 : 1020113 : if (!validate_arg (arg, REAL_TYPE))
9686 : : return NULL_TREE;
9687 : :
9688 : 1020113 : if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9689 : : return NULL_TREE;
9690 : :
9691 : 1020113 : mode = TYPE_MODE (TREE_TYPE (arg));
9692 : :
9693 : 7137741 : bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9694 : :
9695 : : /* If there is no optab, try generic code. */
9696 : 1020113 : switch (DECL_FUNCTION_CODE (fndecl))
9697 : : {
9698 : 249681 : tree result;
9699 : :
9700 : 249681 : CASE_FLT_FN (BUILT_IN_ISINF):
9701 : 249681 : {
9702 : : /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9703 : 249681 : tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9704 : 249681 : tree type = TREE_TYPE (arg);
9705 : 249681 : REAL_VALUE_TYPE r;
9706 : 249681 : char buf[128];
9707 : :
9708 : 249681 : if (is_ibm_extended)
9709 : : {
9710 : : /* NaN and Inf are encoded in the high-order double value
9711 : : only. The low-order value is not significant. */
9712 : 0 : type = double_type_node;
9713 : 0 : mode = DFmode;
9714 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9715 : : }
9716 : 249681 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9717 : 249681 : real_from_string3 (&r, buf, mode);
9718 : 249681 : result = build_call_expr (isgr_fn, 2,
9719 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9720 : : build_real (type, r));
9721 : 249681 : return result;
9722 : : }
9723 : 521003 : CASE_FLT_FN (BUILT_IN_FINITE):
9724 : 521003 : case BUILT_IN_ISFINITE:
9725 : 521003 : {
9726 : : /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9727 : 521003 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9728 : 521003 : tree type = TREE_TYPE (arg);
9729 : 521003 : REAL_VALUE_TYPE r;
9730 : 521003 : char buf[128];
9731 : :
9732 : 521003 : if (is_ibm_extended)
9733 : : {
9734 : : /* NaN and Inf are encoded in the high-order double value
9735 : : only. The low-order value is not significant. */
9736 : 0 : type = double_type_node;
9737 : 0 : mode = DFmode;
9738 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9739 : : }
9740 : 521003 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9741 : 521003 : real_from_string3 (&r, buf, mode);
9742 : 521003 : result = build_call_expr (isle_fn, 2,
9743 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9744 : : build_real (type, r));
9745 : : /*result = fold_build2_loc (loc, UNGT_EXPR,
9746 : : TREE_TYPE (TREE_TYPE (fndecl)),
9747 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9748 : : build_real (type, r));
9749 : : result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9750 : : TREE_TYPE (TREE_TYPE (fndecl)),
9751 : : result);*/
9752 : 521003 : return result;
9753 : : }
9754 : 247907 : case BUILT_IN_ISNORMAL:
9755 : 247907 : {
9756 : : /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9757 : : islessequal(fabs(x),DBL_MAX). */
9758 : 247907 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9759 : 247907 : tree type = TREE_TYPE (arg);
9760 : 247907 : tree orig_arg, max_exp, min_exp;
9761 : 247907 : machine_mode orig_mode = mode;
9762 : 247907 : REAL_VALUE_TYPE rmax, rmin;
9763 : 247907 : char buf[128];
9764 : :
9765 : 247907 : orig_arg = arg = builtin_save_expr (arg);
9766 : 247907 : if (is_ibm_extended)
9767 : : {
9768 : : /* Use double to test the normal range of IBM extended
9769 : : precision. Emin for IBM extended precision is
9770 : : different to emin for IEEE double, being 53 higher
9771 : : since the low double exponent is at least 53 lower
9772 : : than the high double exponent. */
9773 : 0 : type = double_type_node;
9774 : 0 : mode = DFmode;
9775 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9776 : : }
9777 : 247907 : arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9778 : :
9779 : 247907 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9780 : 247907 : real_from_string3 (&rmax, buf, mode);
9781 : 247907 : if (DECIMAL_FLOAT_MODE_P (mode))
9782 : 1 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9783 : : else
9784 : 247906 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9785 : 247907 : real_from_string3 (&rmin, buf, orig_mode);
9786 : 247907 : max_exp = build_real (type, rmax);
9787 : 247907 : min_exp = build_real (type, rmin);
9788 : :
9789 : 247907 : max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9790 : 247907 : if (is_ibm_extended)
9791 : : {
9792 : : /* Testing the high end of the range is done just using
9793 : : the high double, using the same test as isfinite().
9794 : : For the subnormal end of the range we first test the
9795 : : high double, then if its magnitude is equal to the
9796 : : limit of 0x1p-969, we test whether the low double is
9797 : : non-zero and opposite sign to the high double. */
9798 : 0 : tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9799 : 0 : tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9800 : 0 : tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9801 : 0 : tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9802 : : arg, min_exp);
9803 : 0 : tree as_complex = build1 (VIEW_CONVERT_EXPR,
9804 : : complex_double_type_node, orig_arg);
9805 : 0 : tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9806 : 0 : tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9807 : 0 : tree zero = build_real (type, dconst0);
9808 : 0 : tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9809 : 0 : tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9810 : 0 : tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9811 : 0 : tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9812 : : fold_build3 (COND_EXPR,
9813 : : integer_type_node,
9814 : : hilt, logt, lolt));
9815 : 0 : eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9816 : : eq_min, ok_lo);
9817 : 0 : min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9818 : : gt_min, eq_min);
9819 : : }
9820 : : else
9821 : : {
9822 : 247907 : tree const isge_fn
9823 : 247907 : = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9824 : 247907 : min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9825 : : }
9826 : 247907 : result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9827 : : max_exp, min_exp);
9828 : 247907 : return result;
9829 : : }
9830 : : default:
9831 : : break;
9832 : : }
9833 : :
9834 : : return NULL_TREE;
9835 : : }
9836 : :
9837 : : /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9838 : : ARG is the argument for the call. */
9839 : :
9840 : : static tree
9841 : 1053025 : fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9842 : : {
9843 : 1053025 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
9844 : :
9845 : 1053025 : if (!validate_arg (arg, REAL_TYPE))
9846 : : return NULL_TREE;
9847 : :
9848 : 1053025 : switch (builtin_index)
9849 : : {
9850 : 251324 : case BUILT_IN_ISINF:
9851 : 251324 : if (tree_expr_infinite_p (arg))
9852 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9853 : 251324 : if (!tree_expr_maybe_infinite_p (arg))
9854 : 121 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9855 : : return NULL_TREE;
9856 : :
9857 : 708 : case BUILT_IN_ISINF_SIGN:
9858 : 708 : {
9859 : : /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9860 : : /* In a boolean context, GCC will fold the inner COND_EXPR to
9861 : : 1. So e.g. "if (isinf_sign(x))" would be folded to just
9862 : : "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9863 : 708 : tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9864 : 708 : tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9865 : 708 : tree tmp = NULL_TREE;
9866 : :
9867 : 708 : arg = builtin_save_expr (arg);
9868 : :
9869 : 708 : if (signbit_fn && isinf_fn)
9870 : : {
9871 : 708 : tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9872 : 708 : tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9873 : :
9874 : 708 : signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9875 : : signbit_call, integer_zero_node);
9876 : 708 : isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9877 : : isinf_call, integer_zero_node);
9878 : :
9879 : 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9880 : : integer_minus_one_node, integer_one_node);
9881 : 708 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9882 : : isinf_call, tmp,
9883 : : integer_zero_node);
9884 : : }
9885 : :
9886 : : return tmp;
9887 : : }
9888 : :
9889 : 521227 : case BUILT_IN_ISFINITE:
9890 : 521227 : if (tree_expr_finite_p (arg))
9891 : 224 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9892 : 521003 : if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9893 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9894 : : return NULL_TREE;
9895 : :
9896 : 250912 : case BUILT_IN_ISNAN:
9897 : 250912 : if (tree_expr_nan_p (arg))
9898 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9899 : 250912 : if (!tree_expr_maybe_nan_p (arg))
9900 : 124 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9901 : :
9902 : 250788 : {
9903 : 1754856 : bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9904 : 250788 : if (is_ibm_extended)
9905 : : {
9906 : : /* NaN and Inf are encoded in the high-order double value
9907 : : only. The low-order value is not significant. */
9908 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9909 : : }
9910 : : }
9911 : 250788 : arg = builtin_save_expr (arg);
9912 : 250788 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9913 : :
9914 : 28854 : case BUILT_IN_ISSIGNALING:
9915 : : /* Folding to true for REAL_CST is done in fold_const_call_ss.
9916 : : Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9917 : : and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9918 : : here, so there is some possibility of __builtin_issignaling working
9919 : : without -fsignaling-nans. Especially when -fno-signaling-nans is
9920 : : the default. */
9921 : 28854 : if (!tree_expr_maybe_nan_p (arg))
9922 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9923 : : return NULL_TREE;
9924 : :
9925 : 0 : default:
9926 : 0 : gcc_unreachable ();
9927 : : }
9928 : : }
9929 : :
9930 : : /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9931 : : This builtin will generate code to return the appropriate floating
9932 : : point classification depending on the value of the floating point
9933 : : number passed in. The possible return values must be supplied as
9934 : : int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9935 : : FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9936 : : one floating point argument which is "type generic". */
9937 : :
9938 : : static tree
9939 : 107935 : fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9940 : : {
9941 : 107935 : tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9942 : : arg, type, res, tmp;
9943 : 107935 : machine_mode mode;
9944 : 107935 : REAL_VALUE_TYPE r;
9945 : 107935 : char buf[128];
9946 : :
9947 : : /* Verify the required arguments in the original call. */
9948 : 107935 : if (nargs != 6
9949 : 107935 : || !validate_arg (args[0], INTEGER_TYPE)
9950 : 107935 : || !validate_arg (args[1], INTEGER_TYPE)
9951 : 107935 : || !validate_arg (args[2], INTEGER_TYPE)
9952 : 107935 : || !validate_arg (args[3], INTEGER_TYPE)
9953 : 107935 : || !validate_arg (args[4], INTEGER_TYPE)
9954 : 215870 : || !validate_arg (args[5], REAL_TYPE))
9955 : : return NULL_TREE;
9956 : :
9957 : 107935 : fp_nan = args[0];
9958 : 107935 : fp_infinite = args[1];
9959 : 107935 : fp_normal = args[2];
9960 : 107935 : fp_subnormal = args[3];
9961 : 107935 : fp_zero = args[4];
9962 : 107935 : arg = args[5];
9963 : 107935 : type = TREE_TYPE (arg);
9964 : 107935 : mode = TYPE_MODE (type);
9965 : 107935 : arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9966 : :
9967 : : /* fpclassify(x) ->
9968 : : isnan(x) ? FP_NAN :
9969 : : (fabs(x) == Inf ? FP_INFINITE :
9970 : : (fabs(x) >= DBL_MIN ? FP_NORMAL :
9971 : : (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9972 : :
9973 : 107935 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9974 : : build_real (type, dconst0));
9975 : 107935 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9976 : : tmp, fp_zero, fp_subnormal);
9977 : :
9978 : 107935 : if (DECIMAL_FLOAT_MODE_P (mode))
9979 : 3 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (mode)->emin - 1);
9980 : : else
9981 : 107932 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9982 : 107935 : real_from_string3 (&r, buf, mode);
9983 : 107935 : tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9984 : : arg, build_real (type, r));
9985 : 107935 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9986 : : fp_normal, res);
9987 : :
9988 : 107935 : if (tree_expr_maybe_infinite_p (arg))
9989 : : {
9990 : 107851 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9991 : : build_real (type, dconstinf));
9992 : 107851 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9993 : : fp_infinite, res);
9994 : : }
9995 : :
9996 : 107935 : if (tree_expr_maybe_nan_p (arg))
9997 : : {
9998 : 107851 : tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9999 : 107851 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10000 : : res, fp_nan);
10001 : : }
10002 : :
10003 : : return res;
10004 : : }
10005 : :
10006 : : /* Fold a call to an unordered comparison function such as
10007 : : __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10008 : : being called and ARG0 and ARG1 are the arguments for the call.
10009 : : UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10010 : : the opposite of the desired result. UNORDERED_CODE is used
10011 : : for modes that can hold NaNs and ORDERED_CODE is used for
10012 : : the rest. */
10013 : :
10014 : : static tree
10015 : 2758347 : fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10016 : : enum tree_code unordered_code,
10017 : : enum tree_code ordered_code)
10018 : : {
10019 : 2758347 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10020 : 2758347 : enum tree_code code;
10021 : 2758347 : tree type0, type1;
10022 : 2758347 : enum tree_code code0, code1;
10023 : 2758347 : tree cmp_type = NULL_TREE;
10024 : :
10025 : 2758347 : type0 = TREE_TYPE (arg0);
10026 : 2758347 : type1 = TREE_TYPE (arg1);
10027 : :
10028 : 2758347 : code0 = TREE_CODE (type0);
10029 : 2758347 : code1 = TREE_CODE (type1);
10030 : :
10031 : 2758347 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10032 : : /* Choose the wider of two real types. */
10033 : 2758167 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10034 : 2758167 : ? type0 : type1;
10035 : 180 : else if (code0 == REAL_TYPE
10036 : 91 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10037 : : cmp_type = type0;
10038 : 89 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10039 : 89 : && code1 == REAL_TYPE)
10040 : 135 : cmp_type = type1;
10041 : :
10042 : 2758347 : arg0 = fold_convert_loc (loc, cmp_type, arg0);
10043 : 2758347 : arg1 = fold_convert_loc (loc, cmp_type, arg1);
10044 : :
10045 : 2758347 : if (unordered_code == UNORDERED_EXPR)
10046 : : {
10047 : 248937 : if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
10048 : 16 : return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
10049 : 248921 : if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
10050 : 141 : return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10051 : 248780 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10052 : : }
10053 : :
10054 : 2512253 : code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
10055 : 2509410 : ? unordered_code : ordered_code;
10056 : 2509410 : return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10057 : 2509410 : fold_build2_loc (loc, code, type, arg0, arg1));
10058 : : }
10059 : :
10060 : : /* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments.
10061 : : After choosing the wider floating-point type for the comparison,
10062 : : the code is folded to:
10063 : : SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1> */
10064 : :
10065 : : static tree
10066 : 702 : fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
10067 : : {
10068 : 702 : tree type0, type1;
10069 : 702 : enum tree_code code0, code1;
10070 : 702 : tree cmp1, cmp2, cmp_type = NULL_TREE;
10071 : :
10072 : 702 : type0 = TREE_TYPE (arg0);
10073 : 702 : type1 = TREE_TYPE (arg1);
10074 : :
10075 : 702 : code0 = TREE_CODE (type0);
10076 : 702 : code1 = TREE_CODE (type1);
10077 : :
10078 : 702 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10079 : : /* Choose the wider of two real types. */
10080 : 690 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10081 : 690 : ? type0 : type1;
10082 : 12 : else if (code0 == REAL_TYPE
10083 : 6 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10084 : : cmp_type = type0;
10085 : 6 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10086 : 6 : && code1 == REAL_TYPE)
10087 : 6 : cmp_type = type1;
10088 : :
10089 : 702 : arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
10090 : 702 : arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
10091 : :
10092 : 702 : cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
10093 : 702 : cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
10094 : :
10095 : 702 : return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
10096 : : }
10097 : :
10098 : : /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
10099 : : arithmetics if it can never overflow, or into internal functions that
10100 : : return both result of arithmetics and overflowed boolean flag in
10101 : : a complex integer result, or some other check for overflow.
10102 : : Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
10103 : : checking part of that. */
10104 : :
10105 : : static tree
10106 : 170201 : fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
10107 : : tree arg0, tree arg1, tree arg2)
10108 : : {
10109 : 170201 : enum internal_fn ifn = IFN_LAST;
10110 : : /* The code of the expression corresponding to the built-in. */
10111 : 170201 : enum tree_code opcode = ERROR_MARK;
10112 : 170201 : bool ovf_only = false;
10113 : :
10114 : 170201 : switch (fcode)
10115 : : {
10116 : : case BUILT_IN_ADD_OVERFLOW_P:
10117 : : ovf_only = true;
10118 : : /* FALLTHRU */
10119 : : case BUILT_IN_ADD_OVERFLOW:
10120 : : case BUILT_IN_SADD_OVERFLOW:
10121 : : case BUILT_IN_SADDL_OVERFLOW:
10122 : : case BUILT_IN_SADDLL_OVERFLOW:
10123 : : case BUILT_IN_UADD_OVERFLOW:
10124 : : case BUILT_IN_UADDL_OVERFLOW:
10125 : : case BUILT_IN_UADDLL_OVERFLOW:
10126 : : opcode = PLUS_EXPR;
10127 : : ifn = IFN_ADD_OVERFLOW;
10128 : : break;
10129 : 15577 : case BUILT_IN_SUB_OVERFLOW_P:
10130 : 15577 : ovf_only = true;
10131 : : /* FALLTHRU */
10132 : 36920 : case BUILT_IN_SUB_OVERFLOW:
10133 : 36920 : case BUILT_IN_SSUB_OVERFLOW:
10134 : 36920 : case BUILT_IN_SSUBL_OVERFLOW:
10135 : 36920 : case BUILT_IN_SSUBLL_OVERFLOW:
10136 : 36920 : case BUILT_IN_USUB_OVERFLOW:
10137 : 36920 : case BUILT_IN_USUBL_OVERFLOW:
10138 : 36920 : case BUILT_IN_USUBLL_OVERFLOW:
10139 : 36920 : opcode = MINUS_EXPR;
10140 : 36920 : ifn = IFN_SUB_OVERFLOW;
10141 : 36920 : break;
10142 : 15857 : case BUILT_IN_MUL_OVERFLOW_P:
10143 : 15857 : ovf_only = true;
10144 : : /* FALLTHRU */
10145 : 101914 : case BUILT_IN_MUL_OVERFLOW:
10146 : 101914 : case BUILT_IN_SMUL_OVERFLOW:
10147 : 101914 : case BUILT_IN_SMULL_OVERFLOW:
10148 : 101914 : case BUILT_IN_SMULLL_OVERFLOW:
10149 : 101914 : case BUILT_IN_UMUL_OVERFLOW:
10150 : 101914 : case BUILT_IN_UMULL_OVERFLOW:
10151 : 101914 : case BUILT_IN_UMULLL_OVERFLOW:
10152 : 101914 : opcode = MULT_EXPR;
10153 : 101914 : ifn = IFN_MUL_OVERFLOW;
10154 : 101914 : break;
10155 : 0 : default:
10156 : 0 : gcc_unreachable ();
10157 : : }
10158 : :
10159 : : /* For the "generic" overloads, the first two arguments can have different
10160 : : types and the last argument determines the target type to use to check
10161 : : for overflow. The arguments of the other overloads all have the same
10162 : : type. */
10163 : 170201 : tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
10164 : :
10165 : : /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
10166 : : arguments are constant, attempt to fold the built-in call into a constant
10167 : : expression indicating whether or not it detected an overflow. */
10168 : 170201 : if (ovf_only
10169 : 43246 : && TREE_CODE (arg0) == INTEGER_CST
10170 : 13133 : && TREE_CODE (arg1) == INTEGER_CST)
10171 : : /* Perform the computation in the target type and check for overflow. */
10172 : 7349 : return omit_one_operand_loc (loc, boolean_type_node,
10173 : 7349 : arith_overflowed_p (opcode, type, arg0, arg1)
10174 : : ? boolean_true_node : boolean_false_node,
10175 : 7349 : arg2);
10176 : :
10177 : 162852 : tree intres, ovfres;
10178 : 162852 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10179 : : {
10180 : 9335 : intres = fold_binary_loc (loc, opcode, type,
10181 : : fold_convert_loc (loc, type, arg0),
10182 : : fold_convert_loc (loc, type, arg1));
10183 : 9335 : if (TREE_OVERFLOW (intres))
10184 : 1655 : intres = drop_tree_overflow (intres);
10185 : 9335 : ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
10186 : 9335 : ? boolean_true_node : boolean_false_node);
10187 : : }
10188 : : else
10189 : : {
10190 : 153517 : tree ctype = build_complex_type (type);
10191 : 153517 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10192 : : arg0, arg1);
10193 : 153517 : tree tgt;
10194 : 153517 : if (ovf_only)
10195 : : {
10196 : : tgt = call;
10197 : 153517 : intres = NULL_TREE;
10198 : : }
10199 : : else
10200 : : {
10201 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10202 : : as while the call itself is const, the REALPART_EXPR store is
10203 : : certainly not. And in any case, we want just one call,
10204 : : not multiple and trying to CSE them later. */
10205 : 117620 : TREE_SIDE_EFFECTS (call) = 1;
10206 : 117620 : tgt = save_expr (call);
10207 : : }
10208 : 153517 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10209 : 153517 : ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10210 : 153517 : ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
10211 : : }
10212 : :
10213 : 162852 : if (ovf_only)
10214 : 35897 : return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
10215 : :
10216 : 126955 : tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
10217 : 126955 : tree store
10218 : 126955 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
10219 : 126955 : return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
10220 : : }
10221 : :
10222 : : /* Fold __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g into corresponding
10223 : : internal function. */
10224 : :
10225 : : static tree
10226 : 174357 : fold_builtin_bit_query (location_t loc, enum built_in_function fcode,
10227 : : tree arg0, tree arg1)
10228 : : {
10229 : 174357 : enum internal_fn ifn;
10230 : 174357 : enum built_in_function fcodei, fcodel, fcodell;
10231 : 174357 : tree arg0_type = TREE_TYPE (arg0);
10232 : 174357 : tree cast_type = NULL_TREE;
10233 : 174357 : int addend = 0;
10234 : :
10235 : 174357 : switch (fcode)
10236 : : {
10237 : 144271 : case BUILT_IN_CLZG:
10238 : 144271 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10239 : : return NULL_TREE;
10240 : : ifn = IFN_CLZ;
10241 : : fcodei = BUILT_IN_CLZ;
10242 : : fcodel = BUILT_IN_CLZL;
10243 : : fcodell = BUILT_IN_CLZLL;
10244 : : break;
10245 : 29525 : case BUILT_IN_CTZG:
10246 : 29525 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10247 : : return NULL_TREE;
10248 : : ifn = IFN_CTZ;
10249 : : fcodei = BUILT_IN_CTZ;
10250 : : fcodel = BUILT_IN_CTZL;
10251 : : fcodell = BUILT_IN_CTZLL;
10252 : : break;
10253 : : case BUILT_IN_CLRSBG:
10254 : : ifn = IFN_CLRSB;
10255 : : fcodei = BUILT_IN_CLRSB;
10256 : : fcodel = BUILT_IN_CLRSBL;
10257 : : fcodell = BUILT_IN_CLRSBLL;
10258 : : break;
10259 : 83 : case BUILT_IN_FFSG:
10260 : 83 : ifn = IFN_FFS;
10261 : 83 : fcodei = BUILT_IN_FFS;
10262 : 83 : fcodel = BUILT_IN_FFSL;
10263 : 83 : fcodell = BUILT_IN_FFSLL;
10264 : 83 : break;
10265 : 76 : case BUILT_IN_PARITYG:
10266 : 76 : ifn = IFN_PARITY;
10267 : 76 : fcodei = BUILT_IN_PARITY;
10268 : 76 : fcodel = BUILT_IN_PARITYL;
10269 : 76 : fcodell = BUILT_IN_PARITYLL;
10270 : 76 : break;
10271 : 321 : case BUILT_IN_POPCOUNTG:
10272 : 321 : ifn = IFN_POPCOUNT;
10273 : 321 : fcodei = BUILT_IN_POPCOUNT;
10274 : 321 : fcodel = BUILT_IN_POPCOUNTL;
10275 : 321 : fcodell = BUILT_IN_POPCOUNTLL;
10276 : 321 : break;
10277 : 0 : default:
10278 : 0 : gcc_unreachable ();
10279 : : }
10280 : :
10281 : 174235 : if (TYPE_PRECISION (arg0_type)
10282 : 174235 : <= TYPE_PRECISION (long_long_unsigned_type_node))
10283 : : {
10284 : 145338 : if (TYPE_PRECISION (arg0_type) <= TYPE_PRECISION (unsigned_type_node))
10285 : :
10286 : 58237 : cast_type = (TYPE_UNSIGNED (arg0_type)
10287 : 58237 : ? unsigned_type_node : integer_type_node);
10288 : 87101 : else if (TYPE_PRECISION (arg0_type)
10289 : 87101 : <= TYPE_PRECISION (long_unsigned_type_node))
10290 : : {
10291 : 87068 : cast_type = (TYPE_UNSIGNED (arg0_type)
10292 : 87068 : ? long_unsigned_type_node : long_integer_type_node);
10293 : : fcodei = fcodel;
10294 : : }
10295 : : else
10296 : : {
10297 : 33 : cast_type = (TYPE_UNSIGNED (arg0_type)
10298 : 33 : ? long_long_unsigned_type_node
10299 : : : long_long_integer_type_node);
10300 : : fcodei = fcodell;
10301 : : }
10302 : : }
10303 : 57794 : else if (TYPE_PRECISION (arg0_type) <= MAX_FIXED_MODE_SIZE)
10304 : : {
10305 : 28797 : cast_type
10306 : 28797 : = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
10307 : 28797 : TYPE_UNSIGNED (arg0_type));
10308 : 28797 : gcc_assert (TYPE_PRECISION (cast_type)
10309 : : == 2 * TYPE_PRECISION (long_long_unsigned_type_node));
10310 : : fcodei = END_BUILTINS;
10311 : : }
10312 : : else
10313 : : fcodei = END_BUILTINS;
10314 : 174135 : if (cast_type)
10315 : : {
10316 : 174135 : switch (fcode)
10317 : : {
10318 : 144260 : case BUILT_IN_CLZG:
10319 : 144260 : case BUILT_IN_CLRSBG:
10320 : 144260 : addend = TYPE_PRECISION (arg0_type) - TYPE_PRECISION (cast_type);
10321 : 144260 : break;
10322 : : default:
10323 : : break;
10324 : : }
10325 : 174135 : arg0 = fold_convert (cast_type, arg0);
10326 : 174135 : arg0_type = cast_type;
10327 : : }
10328 : :
10329 : 174235 : if (arg1)
10330 : 116004 : arg1 = fold_convert (integer_type_node, arg1);
10331 : :
10332 : 174235 : tree arg2 = arg1;
10333 : 174235 : if (fcode == BUILT_IN_CLZG && addend)
10334 : : {
10335 : 174 : if (arg1)
10336 : 141 : arg0 = save_expr (arg0);
10337 : : arg2 = NULL_TREE;
10338 : : }
10339 : 174235 : tree call = NULL_TREE, tem;
10340 : 174235 : if (TYPE_PRECISION (arg0_type) == MAX_FIXED_MODE_SIZE
10341 : 28830 : && (TYPE_PRECISION (arg0_type)
10342 : 28830 : == 2 * TYPE_PRECISION (long_long_unsigned_type_node))
10343 : : /* If the target supports the optab, then don't do the expansion. */
10344 : 203032 : && !direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH))
10345 : : {
10346 : : /* __int128 expansions using up to 2 long long builtins. */
10347 : 28797 : arg0 = save_expr (arg0);
10348 : 28797 : tree type = (TYPE_UNSIGNED (arg0_type)
10349 : 28797 : ? long_long_unsigned_type_node
10350 : 28797 : : long_long_integer_type_node);
10351 : 57594 : tree hi = fold_build2 (RSHIFT_EXPR, arg0_type, arg0,
10352 : : build_int_cst (integer_type_node,
10353 : : MAX_FIXED_MODE_SIZE / 2));
10354 : 28797 : hi = fold_convert (type, hi);
10355 : 28797 : tree lo = fold_convert (type, arg0);
10356 : 28797 : switch (fcode)
10357 : : {
10358 : 28702 : case BUILT_IN_CLZG:
10359 : 28702 : call = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10360 : 57404 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10361 : : build_int_cst (integer_type_node,
10362 : : MAX_FIXED_MODE_SIZE / 2));
10363 : 28702 : if (arg2)
10364 : 28689 : call = fold_build3 (COND_EXPR, integer_type_node,
10365 : : fold_build2 (NE_EXPR, boolean_type_node,
10366 : : lo, build_zero_cst (type)),
10367 : : call, arg2);
10368 : 28702 : call = fold_build3 (COND_EXPR, integer_type_node,
10369 : : fold_build2 (NE_EXPR, boolean_type_node,
10370 : : hi, build_zero_cst (type)),
10371 : : fold_builtin_bit_query (loc, fcode, hi,
10372 : : NULL_TREE),
10373 : : call);
10374 : 28702 : break;
10375 : 33 : case BUILT_IN_CTZG:
10376 : 33 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10377 : 66 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10378 : : build_int_cst (integer_type_node,
10379 : : MAX_FIXED_MODE_SIZE / 2));
10380 : 33 : if (arg2)
10381 : 24 : call = fold_build3 (COND_EXPR, integer_type_node,
10382 : : fold_build2 (NE_EXPR, boolean_type_node,
10383 : : hi, build_zero_cst (type)),
10384 : : call, arg2);
10385 : 33 : call = fold_build3 (COND_EXPR, integer_type_node,
10386 : : fold_build2 (NE_EXPR, boolean_type_node,
10387 : : lo, build_zero_cst (type)),
10388 : : fold_builtin_bit_query (loc, fcode, lo,
10389 : : NULL_TREE),
10390 : : call);
10391 : 33 : break;
10392 : 9 : case BUILT_IN_CLRSBG:
10393 : 9 : tem = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10394 : 18 : tem = fold_build2 (PLUS_EXPR, integer_type_node, tem,
10395 : : build_int_cst (integer_type_node,
10396 : : MAX_FIXED_MODE_SIZE / 2));
10397 : 18 : tem = fold_build3 (COND_EXPR, integer_type_node,
10398 : : fold_build2 (LT_EXPR, boolean_type_node,
10399 : : fold_build2 (BIT_XOR_EXPR, type,
10400 : : lo, hi),
10401 : : build_zero_cst (type)),
10402 : : build_int_cst (integer_type_node,
10403 : : MAX_FIXED_MODE_SIZE / 2 - 1),
10404 : : tem);
10405 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10406 : 9 : call = save_expr (call);
10407 : 18 : call = fold_build3 (COND_EXPR, integer_type_node,
10408 : : fold_build2 (NE_EXPR, boolean_type_node,
10409 : : call,
10410 : : build_int_cst (integer_type_node,
10411 : : MAX_FIXED_MODE_SIZE
10412 : : / 2 - 1)),
10413 : : call, tem);
10414 : 9 : break;
10415 : 9 : case BUILT_IN_FFSG:
10416 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10417 : 18 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10418 : : build_int_cst (integer_type_node,
10419 : : MAX_FIXED_MODE_SIZE / 2));
10420 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10421 : : fold_build2 (NE_EXPR, boolean_type_node,
10422 : : hi, build_zero_cst (type)),
10423 : : call, integer_zero_node);
10424 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10425 : : fold_build2 (NE_EXPR, boolean_type_node,
10426 : : lo, build_zero_cst (type)),
10427 : : fold_builtin_bit_query (loc, fcode, lo,
10428 : : NULL_TREE),
10429 : : call);
10430 : 9 : break;
10431 : 9 : case BUILT_IN_PARITYG:
10432 : 9 : call = fold_builtin_bit_query (loc, fcode,
10433 : : fold_build2 (BIT_XOR_EXPR, type,
10434 : : lo, hi), NULL_TREE);
10435 : 9 : break;
10436 : 35 : case BUILT_IN_POPCOUNTG:
10437 : 35 : call = fold_build2 (PLUS_EXPR, integer_type_node,
10438 : : fold_builtin_bit_query (loc, fcode, hi,
10439 : : NULL_TREE),
10440 : : fold_builtin_bit_query (loc, fcode, lo,
10441 : : NULL_TREE));
10442 : 35 : break;
10443 : 0 : default:
10444 : 0 : gcc_unreachable ();
10445 : : }
10446 : : }
10447 : : else
10448 : : {
10449 : : /* Only keep second argument to IFN_CLZ/IFN_CTZ if it is the
10450 : : value defined at zero during GIMPLE, or for large/huge _BitInt
10451 : : (which are then lowered during bitint lowering). */
10452 : 145438 : if (arg2 && TREE_CODE (TREE_TYPE (arg0)) != BITINT_TYPE)
10453 : : {
10454 : 87132 : int val;
10455 : 87132 : if (fcode == BUILT_IN_CLZG)
10456 : : {
10457 : 57865 : if (CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10458 : : val) != 2
10459 : 57865 : || wi::to_widest (arg2) != val)
10460 : 57856 : arg2 = NULL_TREE;
10461 : : }
10462 : 29267 : else if (CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10463 : : val) != 2
10464 : 29267 : || wi::to_widest (arg2) != val)
10465 : 29267 : arg2 = NULL_TREE;
10466 : 87132 : if (!direct_internal_fn_supported_p (ifn, arg0_type,
10467 : : OPTIMIZE_FOR_BOTH))
10468 : : arg2 = NULL_TREE;
10469 : 87102 : if (arg2 == NULL_TREE)
10470 : 87123 : arg0 = save_expr (arg0);
10471 : : }
10472 : 145438 : if (fcodei == END_BUILTINS || arg2)
10473 : 191 : call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
10474 : : arg2 ? 2 : 1, arg0, arg2);
10475 : : else
10476 : 145329 : call = build_call_expr_loc (loc, builtin_decl_explicit (fcodei), 1,
10477 : : arg0);
10478 : : }
10479 : 174235 : if (addend)
10480 : 192 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10481 : : build_int_cst (integer_type_node, addend));
10482 : 174235 : if (arg1 && arg2 == NULL_TREE)
10483 : 87264 : call = fold_build3 (COND_EXPR, integer_type_node,
10484 : : fold_build2 (NE_EXPR, boolean_type_node,
10485 : : arg0, build_zero_cst (arg0_type)),
10486 : : call, arg1);
10487 : :
10488 : : return call;
10489 : : }
10490 : :
10491 : : /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
10492 : : that return both result of arithmetics and overflowed boolean
10493 : : flag in a complex integer result. */
10494 : :
10495 : : static tree
10496 : 54 : fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
10497 : : tree *args)
10498 : : {
10499 : 54 : enum internal_fn ifn;
10500 : :
10501 : 54 : switch (fcode)
10502 : : {
10503 : : case BUILT_IN_ADDC:
10504 : : case BUILT_IN_ADDCL:
10505 : : case BUILT_IN_ADDCLL:
10506 : : ifn = IFN_ADD_OVERFLOW;
10507 : : break;
10508 : 28 : case BUILT_IN_SUBC:
10509 : 28 : case BUILT_IN_SUBCL:
10510 : 28 : case BUILT_IN_SUBCLL:
10511 : 28 : ifn = IFN_SUB_OVERFLOW;
10512 : 28 : break;
10513 : 0 : default:
10514 : 0 : gcc_unreachable ();
10515 : : }
10516 : :
10517 : 54 : tree type = TREE_TYPE (args[0]);
10518 : 54 : tree ctype = build_complex_type (type);
10519 : 54 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10520 : : args[0], args[1]);
10521 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10522 : : as while the call itself is const, the REALPART_EXPR store is
10523 : : certainly not. And in any case, we want just one call,
10524 : : not multiple and trying to CSE them later. */
10525 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10526 : 54 : tree tgt = save_expr (call);
10527 : 54 : tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10528 : 54 : tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10529 : 54 : call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10530 : : intres, args[2]);
10531 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10532 : 54 : tgt = save_expr (call);
10533 : 54 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10534 : 54 : tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10535 : 54 : ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
10536 : 54 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
10537 : 54 : tree store
10538 : 54 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10539 : 54 : return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10540 : : }
10541 : :
10542 : : /* Fold a call to __builtin_FILE to a constant string. */
10543 : :
10544 : : static inline tree
10545 : 5757 : fold_builtin_FILE (location_t loc)
10546 : : {
10547 : 5757 : if (const char *fname = LOCATION_FILE (loc))
10548 : : {
10549 : : /* The documentation says this builtin is equivalent to the preprocessor
10550 : : __FILE__ macro so it appears appropriate to use the same file prefix
10551 : : mappings. */
10552 : 5757 : fname = remap_macro_filename (fname);
10553 : 5757 : return build_string_literal (fname);
10554 : : }
10555 : :
10556 : 0 : return build_string_literal ("");
10557 : : }
10558 : :
10559 : : /* Fold a call to __builtin_FUNCTION to a constant string. */
10560 : :
10561 : : static inline tree
10562 : 66 : fold_builtin_FUNCTION ()
10563 : : {
10564 : 66 : const char *name = "";
10565 : :
10566 : 66 : if (current_function_decl)
10567 : 39 : name = lang_hooks.decl_printable_name (current_function_decl, 0);
10568 : :
10569 : 66 : return build_string_literal (name);
10570 : : }
10571 : :
10572 : : /* Fold a call to __builtin_LINE to an integer constant. */
10573 : :
10574 : : static inline tree
10575 : 10844 : fold_builtin_LINE (location_t loc, tree type)
10576 : : {
10577 : 10844 : return build_int_cst (type, LOCATION_LINE (loc));
10578 : : }
10579 : :
10580 : : /* Fold a call to built-in function FNDECL with 0 arguments.
10581 : : This function returns NULL_TREE if no simplification was possible. */
10582 : :
10583 : : static tree
10584 : 20616279 : fold_builtin_0 (location_t loc, tree fndecl)
10585 : : {
10586 : 20616279 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10587 : 20616279 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10588 : 20616279 : switch (fcode)
10589 : : {
10590 : 5757 : case BUILT_IN_FILE:
10591 : 5757 : return fold_builtin_FILE (loc);
10592 : :
10593 : 66 : case BUILT_IN_FUNCTION:
10594 : 66 : return fold_builtin_FUNCTION ();
10595 : :
10596 : 10844 : case BUILT_IN_LINE:
10597 : 10844 : return fold_builtin_LINE (loc, type);
10598 : :
10599 : 34861 : CASE_FLT_FN (BUILT_IN_INF):
10600 : 34861 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
10601 : 34861 : case BUILT_IN_INFD32:
10602 : 34861 : case BUILT_IN_INFD64:
10603 : 34861 : case BUILT_IN_INFD128:
10604 : 34861 : case BUILT_IN_INFD64X:
10605 : 34861 : return fold_builtin_inf (loc, type, true);
10606 : :
10607 : 197454 : CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10608 : 197454 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
10609 : 197454 : return fold_builtin_inf (loc, type, false);
10610 : :
10611 : 0 : case BUILT_IN_CLASSIFY_TYPE:
10612 : 0 : return fold_builtin_classify_type (NULL_TREE);
10613 : :
10614 : 15606819 : case BUILT_IN_UNREACHABLE:
10615 : : /* Rewrite any explicit calls to __builtin_unreachable. */
10616 : 15606819 : if (sanitize_flags_p (SANITIZE_UNREACHABLE))
10617 : 113 : return build_builtin_unreachable (loc);
10618 : : break;
10619 : :
10620 : : default:
10621 : : break;
10622 : : }
10623 : : return NULL_TREE;
10624 : : }
10625 : :
10626 : : /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10627 : : This function returns NULL_TREE if no simplification was possible. */
10628 : :
10629 : : static tree
10630 : 14892519 : fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
10631 : : {
10632 : 14892519 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10633 : 14892519 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10634 : :
10635 : 14892519 : if (error_operand_p (arg0))
10636 : : return NULL_TREE;
10637 : :
10638 : 14892519 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
10639 : : return ret;
10640 : :
10641 : 14408051 : switch (fcode)
10642 : : {
10643 : 699603 : case BUILT_IN_CONSTANT_P:
10644 : 699603 : {
10645 : 699603 : tree val = fold_builtin_constant_p (arg0);
10646 : :
10647 : : /* Gimplification will pull the CALL_EXPR for the builtin out of
10648 : : an if condition. When not optimizing, we'll not CSE it back.
10649 : : To avoid link error types of regressions, return false now. */
10650 : 699603 : if (!val && !optimize)
10651 : 1236 : val = integer_zero_node;
10652 : :
10653 : : return val;
10654 : : }
10655 : :
10656 : 2304 : case BUILT_IN_CLASSIFY_TYPE:
10657 : 2304 : return fold_builtin_classify_type (arg0);
10658 : :
10659 : 404579 : case BUILT_IN_STRLEN:
10660 : 404579 : return fold_builtin_strlen (loc, expr, type, arg0);
10661 : :
10662 : 327342 : CASE_FLT_FN (BUILT_IN_FABS):
10663 : 327342 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10664 : 327342 : case BUILT_IN_FABSD32:
10665 : 327342 : case BUILT_IN_FABSD64:
10666 : 327342 : case BUILT_IN_FABSD128:
10667 : 327342 : case BUILT_IN_FABSD64X:
10668 : 327342 : return fold_builtin_fabs (loc, arg0, type);
10669 : :
10670 : 90295 : case BUILT_IN_ABS:
10671 : 90295 : case BUILT_IN_LABS:
10672 : 90295 : case BUILT_IN_LLABS:
10673 : 90295 : case BUILT_IN_IMAXABS:
10674 : 90295 : case BUILT_IN_UABS:
10675 : 90295 : case BUILT_IN_ULABS:
10676 : 90295 : case BUILT_IN_ULLABS:
10677 : 90295 : case BUILT_IN_UIMAXABS:
10678 : 90295 : return fold_builtin_abs (loc, arg0, type);
10679 : :
10680 : 24501 : CASE_FLT_FN (BUILT_IN_CONJ):
10681 : 24501 : if (validate_arg (arg0, COMPLEX_TYPE)
10682 : 24501 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10683 : 24501 : return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10684 : : break;
10685 : :
10686 : 764 : CASE_FLT_FN (BUILT_IN_CREAL):
10687 : 764 : if (validate_arg (arg0, COMPLEX_TYPE)
10688 : 764 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10689 : 764 : return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
10690 : : break;
10691 : :
10692 : 1884 : CASE_FLT_FN (BUILT_IN_CIMAG):
10693 : 1884 : if (validate_arg (arg0, COMPLEX_TYPE)
10694 : 1884 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10695 : 1884 : return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10696 : : break;
10697 : :
10698 : 107935 : CASE_FLT_FN (BUILT_IN_CARG):
10699 : 107935 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
10700 : 107935 : return fold_builtin_carg (loc, arg0, type);
10701 : :
10702 : 211 : case BUILT_IN_ISASCII:
10703 : 211 : return fold_builtin_isascii (loc, arg0);
10704 : :
10705 : 168 : case BUILT_IN_TOASCII:
10706 : 168 : return fold_builtin_toascii (loc, arg0);
10707 : :
10708 : 325 : case BUILT_IN_ISDIGIT:
10709 : 325 : return fold_builtin_isdigit (loc, arg0);
10710 : :
10711 : 521227 : CASE_FLT_FN (BUILT_IN_FINITE):
10712 : 521227 : case BUILT_IN_FINITED32:
10713 : 521227 : case BUILT_IN_FINITED64:
10714 : 521227 : case BUILT_IN_FINITED128:
10715 : 521227 : case BUILT_IN_ISFINITE:
10716 : 521227 : {
10717 : 521227 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10718 : 521227 : if (ret)
10719 : : return ret;
10720 : 521003 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10721 : : }
10722 : :
10723 : 251324 : CASE_FLT_FN (BUILT_IN_ISINF):
10724 : 251324 : case BUILT_IN_ISINFD32:
10725 : 251324 : case BUILT_IN_ISINFD64:
10726 : 251324 : case BUILT_IN_ISINFD128:
10727 : 251324 : {
10728 : 251324 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10729 : 251324 : if (ret)
10730 : : return ret;
10731 : 251203 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10732 : : }
10733 : :
10734 : 247907 : case BUILT_IN_ISNORMAL:
10735 : 247907 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10736 : :
10737 : 708 : case BUILT_IN_ISINF_SIGN:
10738 : 708 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10739 : :
10740 : 250912 : CASE_FLT_FN (BUILT_IN_ISNAN):
10741 : 250912 : case BUILT_IN_ISNAND32:
10742 : 250912 : case BUILT_IN_ISNAND64:
10743 : 250912 : case BUILT_IN_ISNAND128:
10744 : 250912 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10745 : :
10746 : 28854 : case BUILT_IN_ISSIGNALING:
10747 : 28854 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
10748 : :
10749 : 575350 : case BUILT_IN_FREE:
10750 : 575350 : if (integer_zerop (arg0))
10751 : 1011 : return build_empty_stmt (loc);
10752 : : break;
10753 : :
10754 : 646 : case BUILT_IN_CLZG:
10755 : 646 : case BUILT_IN_CTZG:
10756 : 646 : case BUILT_IN_CLRSBG:
10757 : 646 : case BUILT_IN_FFSG:
10758 : 646 : case BUILT_IN_PARITYG:
10759 : 646 : case BUILT_IN_POPCOUNTG:
10760 : 646 : return fold_builtin_bit_query (loc, fcode, arg0, NULL_TREE);
10761 : :
10762 : : default:
10763 : : break;
10764 : : }
10765 : :
10766 : : return NULL_TREE;
10767 : :
10768 : : }
10769 : :
10770 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL
10771 : : with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
10772 : : if no simplification was possible. */
10773 : :
10774 : : static tree
10775 : 15855320 : fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
10776 : : {
10777 : 15855320 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10778 : 15855320 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10779 : :
10780 : 15855320 : if (error_operand_p (arg0)
10781 : 15855320 : || error_operand_p (arg1))
10782 : : return NULL_TREE;
10783 : :
10784 : 15855316 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
10785 : : return ret;
10786 : :
10787 : 15639788 : switch (fcode)
10788 : : {
10789 : 6722 : CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10790 : 6722 : CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10791 : 6722 : if (validate_arg (arg0, REAL_TYPE)
10792 : 6722 : && validate_arg (arg1, POINTER_TYPE))
10793 : 6722 : return do_mpfr_lgamma_r (arg0, arg1, type);
10794 : : break;
10795 : :
10796 : 105381 : CASE_FLT_FN (BUILT_IN_FREXP):
10797 : 105381 : return fold_builtin_frexp (loc, arg0, arg1, type);
10798 : :
10799 : 71535 : CASE_FLT_FN (BUILT_IN_MODF):
10800 : 71535 : return fold_builtin_modf (loc, arg0, arg1, type);
10801 : :
10802 : 2655 : case BUILT_IN_STRSPN:
10803 : 2655 : return fold_builtin_strspn (loc, expr, arg0, arg1, type);
10804 : :
10805 : 2553 : case BUILT_IN_STRCSPN:
10806 : 2553 : return fold_builtin_strcspn (loc, expr, arg0, arg1, type);
10807 : :
10808 : 79925 : case BUILT_IN_STRPBRK:
10809 : 79925 : return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
10810 : :
10811 : 4493854 : case BUILT_IN_EXPECT:
10812 : 4493854 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
10813 : :
10814 : 499282 : case BUILT_IN_ISGREATER:
10815 : 499282 : return fold_builtin_unordered_cmp (loc, fndecl,
10816 : 499282 : arg0, arg1, UNLE_EXPR, LE_EXPR);
10817 : 495909 : case BUILT_IN_ISGREATEREQUAL:
10818 : 495909 : return fold_builtin_unordered_cmp (loc, fndecl,
10819 : 495909 : arg0, arg1, UNLT_EXPR, LT_EXPR);
10820 : 249571 : case BUILT_IN_ISLESS:
10821 : 249571 : return fold_builtin_unordered_cmp (loc, fndecl,
10822 : 249571 : arg0, arg1, UNGE_EXPR, GE_EXPR);
10823 : 1016941 : case BUILT_IN_ISLESSEQUAL:
10824 : 1016941 : return fold_builtin_unordered_cmp (loc, fndecl,
10825 : 1016941 : arg0, arg1, UNGT_EXPR, GT_EXPR);
10826 : 247707 : case BUILT_IN_ISLESSGREATER:
10827 : 247707 : return fold_builtin_unordered_cmp (loc, fndecl,
10828 : 247707 : arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10829 : 248937 : case BUILT_IN_ISUNORDERED:
10830 : 248937 : return fold_builtin_unordered_cmp (loc, fndecl,
10831 : : arg0, arg1, UNORDERED_EXPR,
10832 : 248937 : NOP_EXPR);
10833 : :
10834 : 702 : case BUILT_IN_ISEQSIG:
10835 : 702 : return fold_builtin_iseqsig (loc, arg0, arg1);
10836 : :
10837 : : /* We do the folding for va_start in the expander. */
10838 : : case BUILT_IN_VA_START:
10839 : : break;
10840 : :
10841 : 196243 : case BUILT_IN_OBJECT_SIZE:
10842 : 196243 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
10843 : 196243 : return fold_builtin_object_size (arg0, arg1, fcode);
10844 : :
10845 : 38725 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10846 : 38725 : return fold_builtin_atomic_always_lock_free (arg0, arg1);
10847 : :
10848 : 38535 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10849 : 38535 : return fold_builtin_atomic_is_lock_free (arg0, arg1);
10850 : :
10851 : 116126 : case BUILT_IN_CLZG:
10852 : 116126 : case BUILT_IN_CTZG:
10853 : 116126 : return fold_builtin_bit_query (loc, fcode, arg0, arg1);
10854 : :
10855 : : default:
10856 : : break;
10857 : : }
10858 : : return NULL_TREE;
10859 : : }
10860 : :
10861 : : /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10862 : : and ARG2.
10863 : : This function returns NULL_TREE if no simplification was possible. */
10864 : :
10865 : : static tree
10866 : 6077986 : fold_builtin_3 (location_t loc, tree fndecl,
10867 : : tree arg0, tree arg1, tree arg2)
10868 : : {
10869 : 6077986 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10870 : 6077986 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10871 : :
10872 : 6077986 : if (error_operand_p (arg0)
10873 : 6077986 : || error_operand_p (arg1)
10874 : 12155972 : || error_operand_p (arg2))
10875 : : return NULL_TREE;
10876 : :
10877 : 6077984 : if (tree ret = fold_const_call (as_combined_fn (fcode), type,
10878 : : arg0, arg1, arg2))
10879 : : return ret;
10880 : :
10881 : 6060938 : switch (fcode)
10882 : : {
10883 : :
10884 : 146 : CASE_FLT_FN (BUILT_IN_SINCOS):
10885 : 146 : return fold_builtin_sincos (loc, arg0, arg1, arg2);
10886 : :
10887 : 80710 : CASE_FLT_FN (BUILT_IN_REMQUO):
10888 : 80710 : if (validate_arg (arg0, REAL_TYPE)
10889 : 80710 : && validate_arg (arg1, REAL_TYPE)
10890 : 161420 : && validate_arg (arg2, POINTER_TYPE))
10891 : 80710 : return do_mpfr_remquo (arg0, arg1, arg2);
10892 : : break;
10893 : :
10894 : 2379144 : case BUILT_IN_MEMCMP:
10895 : 2379144 : return fold_builtin_memcmp (loc, arg0, arg1, arg2);
10896 : :
10897 : 462891 : case BUILT_IN_EXPECT:
10898 : 462891 : return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
10899 : :
10900 : 337 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
10901 : 337 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
10902 : :
10903 : 170201 : case BUILT_IN_ADD_OVERFLOW:
10904 : 170201 : case BUILT_IN_SUB_OVERFLOW:
10905 : 170201 : case BUILT_IN_MUL_OVERFLOW:
10906 : 170201 : case BUILT_IN_ADD_OVERFLOW_P:
10907 : 170201 : case BUILT_IN_SUB_OVERFLOW_P:
10908 : 170201 : case BUILT_IN_MUL_OVERFLOW_P:
10909 : 170201 : case BUILT_IN_SADD_OVERFLOW:
10910 : 170201 : case BUILT_IN_SADDL_OVERFLOW:
10911 : 170201 : case BUILT_IN_SADDLL_OVERFLOW:
10912 : 170201 : case BUILT_IN_SSUB_OVERFLOW:
10913 : 170201 : case BUILT_IN_SSUBL_OVERFLOW:
10914 : 170201 : case BUILT_IN_SSUBLL_OVERFLOW:
10915 : 170201 : case BUILT_IN_SMUL_OVERFLOW:
10916 : 170201 : case BUILT_IN_SMULL_OVERFLOW:
10917 : 170201 : case BUILT_IN_SMULLL_OVERFLOW:
10918 : 170201 : case BUILT_IN_UADD_OVERFLOW:
10919 : 170201 : case BUILT_IN_UADDL_OVERFLOW:
10920 : 170201 : case BUILT_IN_UADDLL_OVERFLOW:
10921 : 170201 : case BUILT_IN_USUB_OVERFLOW:
10922 : 170201 : case BUILT_IN_USUBL_OVERFLOW:
10923 : 170201 : case BUILT_IN_USUBLL_OVERFLOW:
10924 : 170201 : case BUILT_IN_UMUL_OVERFLOW:
10925 : 170201 : case BUILT_IN_UMULL_OVERFLOW:
10926 : 170201 : case BUILT_IN_UMULLL_OVERFLOW:
10927 : 170201 : return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10928 : :
10929 : : default:
10930 : : break;
10931 : : }
10932 : : return NULL_TREE;
10933 : : }
10934 : :
10935 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL.
10936 : : ARGS is an array of NARGS arguments. IGNORE is true if the result
10937 : : of the function call is ignored. This function returns NULL_TREE
10938 : : if no simplification was possible. */
10939 : :
10940 : : static tree
10941 : 59751292 : fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
10942 : : int nargs, bool)
10943 : : {
10944 : 59751292 : tree ret = NULL_TREE;
10945 : :
10946 : 59751292 : switch (nargs)
10947 : : {
10948 : 20616279 : case 0:
10949 : 20616279 : ret = fold_builtin_0 (loc, fndecl);
10950 : 20616279 : break;
10951 : 14892519 : case 1:
10952 : 14892519 : ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
10953 : 14892519 : break;
10954 : 15855320 : case 2:
10955 : 15855320 : ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
10956 : 15855320 : break;
10957 : 6077986 : case 3:
10958 : 6077986 : ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10959 : 6077986 : break;
10960 : 2309188 : default:
10961 : 2309188 : ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10962 : 2309188 : break;
10963 : : }
10964 : 59751292 : if (ret)
10965 : : {
10966 : 6155766 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10967 : 6155766 : SET_EXPR_LOCATION (ret, loc);
10968 : 6155766 : return ret;
10969 : : }
10970 : : return NULL_TREE;
10971 : : }
10972 : :
10973 : : /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10974 : : list ARGS along with N new arguments in NEWARGS. SKIP is the number
10975 : : of arguments in ARGS to be omitted. OLDNARGS is the number of
10976 : : elements in ARGS. */
10977 : :
10978 : : static tree
10979 : 4 : rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10980 : : int skip, tree fndecl, int n, va_list newargs)
10981 : : {
10982 : 4 : int nargs = oldnargs - skip + n;
10983 : 4 : tree *buffer;
10984 : :
10985 : 4 : if (n > 0)
10986 : : {
10987 : 0 : int i, j;
10988 : :
10989 : 0 : buffer = XALLOCAVEC (tree, nargs);
10990 : 0 : for (i = 0; i < n; i++)
10991 : 0 : buffer[i] = va_arg (newargs, tree);
10992 : 0 : for (j = skip; j < oldnargs; j++, i++)
10993 : 0 : buffer[i] = args[j];
10994 : : }
10995 : : else
10996 : 4 : buffer = args + skip;
10997 : :
10998 : 4 : return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10999 : : }
11000 : :
11001 : : /* Return true if FNDECL shouldn't be folded right now.
11002 : : If a built-in function has an inline attribute always_inline
11003 : : wrapper, defer folding it after always_inline functions have
11004 : : been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11005 : : might not be performed. */
11006 : :
11007 : : bool
11008 : 141490913 : avoid_folding_inline_builtin (tree fndecl)
11009 : : {
11010 : 141490913 : return (DECL_DECLARED_INLINE_P (fndecl)
11011 : 9443 : && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11012 : 9401 : && cfun
11013 : 9401 : && !cfun->always_inline_functions_inlined
11014 : 141500314 : && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11015 : : }
11016 : :
11017 : : /* A wrapper function for builtin folding that prevents warnings for
11018 : : "statement without effect" and the like, caused by removing the
11019 : : call node earlier than the warning is generated. */
11020 : :
11021 : : tree
11022 : 173918662 : fold_call_expr (location_t loc, tree exp, bool ignore)
11023 : : {
11024 : 173918662 : tree ret = NULL_TREE;
11025 : 173918662 : tree fndecl = get_callee_fndecl (exp);
11026 : 172232970 : if (fndecl && fndecl_built_in_p (fndecl)
11027 : : /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11028 : : yet. Defer folding until we see all the arguments
11029 : : (after inlining). */
11030 : 230472556 : && !CALL_EXPR_VA_ARG_PACK (exp))
11031 : : {
11032 : 56553869 : int nargs = call_expr_nargs (exp);
11033 : :
11034 : : /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11035 : : instead last argument is __builtin_va_arg_pack (). Defer folding
11036 : : even in that case, until arguments are finalized. */
11037 : 56553869 : if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11038 : : {
11039 : 243644 : tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11040 : 243644 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11041 : : return NULL_TREE;
11042 : : }
11043 : :
11044 : 56553805 : if (avoid_folding_inline_builtin (fndecl))
11045 : : return NULL_TREE;
11046 : :
11047 : 56550270 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11048 : 70188676 : return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11049 : 70188676 : CALL_EXPR_ARGP (exp), ignore);
11050 : : else
11051 : : {
11052 : 21455932 : tree *args = CALL_EXPR_ARGP (exp);
11053 : 21455932 : ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
11054 : 21455932 : if (ret)
11055 : : return ret;
11056 : : }
11057 : : }
11058 : : return NULL_TREE;
11059 : : }
11060 : :
11061 : : /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
11062 : : N arguments are passed in the array ARGARRAY. Return a folded
11063 : : expression or NULL_TREE if no simplification was possible. */
11064 : :
11065 : : tree
11066 : 63192634 : fold_builtin_call_array (location_t loc, tree,
11067 : : tree fn,
11068 : : int n,
11069 : : tree *argarray)
11070 : : {
11071 : 63192634 : if (TREE_CODE (fn) != ADDR_EXPR)
11072 : : return NULL_TREE;
11073 : :
11074 : 63192634 : tree fndecl = TREE_OPERAND (fn, 0);
11075 : 63192634 : if (TREE_CODE (fndecl) == FUNCTION_DECL
11076 : 63192634 : && fndecl_built_in_p (fndecl))
11077 : : {
11078 : : /* If last argument is __builtin_va_arg_pack (), arguments to this
11079 : : function are not finalized yet. Defer folding until they are. */
11080 : 62623364 : if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11081 : : {
11082 : 101478 : tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11083 : 101478 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11084 : : return NULL_TREE;
11085 : : }
11086 : 62623337 : if (avoid_folding_inline_builtin (fndecl))
11087 : : return NULL_TREE;
11088 : 62623337 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11089 : 33917997 : return targetm.fold_builtin (fndecl, n, argarray, false);
11090 : : else
11091 : 28705340 : return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
11092 : : }
11093 : :
11094 : : return NULL_TREE;
11095 : : }
11096 : :
11097 : : /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11098 : : along with N new arguments specified as the "..." parameters. SKIP
11099 : : is the number of arguments in EXP to be omitted. This function is used
11100 : : to do varargs-to-varargs transformations. */
11101 : :
11102 : : static tree
11103 : 4 : rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11104 : : {
11105 : 4 : va_list ap;
11106 : 4 : tree t;
11107 : :
11108 : 4 : va_start (ap, n);
11109 : 8 : t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11110 : 4 : CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11111 : 4 : va_end (ap);
11112 : :
11113 : 4 : return t;
11114 : : }
11115 : :
11116 : : /* Validate a single argument ARG against a tree code CODE representing
11117 : : a type. Return true when argument is valid. */
11118 : :
11119 : : static bool
11120 : 13082620 : validate_arg (const_tree arg, enum tree_code code)
11121 : : {
11122 : 13082620 : if (!arg)
11123 : : return false;
11124 : 13082597 : else if (code == POINTER_TYPE)
11125 : 6543340 : return POINTER_TYPE_P (TREE_TYPE (arg));
11126 : 6539257 : else if (code == INTEGER_TYPE)
11127 : 3522918 : return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11128 : 3016339 : return code == TREE_CODE (TREE_TYPE (arg));
11129 : : }
11130 : :
11131 : : /* This function validates the types of a function call argument list
11132 : : against a specified list of tree_codes. If the last specifier is a 0,
11133 : : that represents an ellipses, otherwise the last specifier must be a
11134 : : VOID_TYPE.
11135 : :
11136 : : This is the GIMPLE version of validate_arglist. Eventually we want to
11137 : : completely convert builtins.cc to work from GIMPLEs and the tree based
11138 : : validate_arglist will then be removed. */
11139 : :
11140 : : bool
11141 : 90 : validate_gimple_arglist (const gcall *call, ...)
11142 : : {
11143 : 90 : enum tree_code code;
11144 : 90 : bool res = 0;
11145 : 90 : va_list ap;
11146 : 90 : const_tree arg;
11147 : 90 : size_t i;
11148 : :
11149 : 90 : va_start (ap, call);
11150 : 90 : i = 0;
11151 : :
11152 : 360 : do
11153 : : {
11154 : 360 : code = (enum tree_code) va_arg (ap, int);
11155 : 360 : switch (code)
11156 : : {
11157 : 0 : case 0:
11158 : : /* This signifies an ellipses, any further arguments are all ok. */
11159 : 0 : res = true;
11160 : 0 : goto end;
11161 : 90 : case VOID_TYPE:
11162 : : /* This signifies an endlink, if no arguments remain, return
11163 : : true, otherwise return false. */
11164 : 90 : res = (i == gimple_call_num_args (call));
11165 : 90 : goto end;
11166 : 270 : default:
11167 : : /* If no parameters remain or the parameter's code does not
11168 : : match the specified code, return false. Otherwise continue
11169 : : checking any remaining arguments. */
11170 : 270 : arg = gimple_call_arg (call, i++);
11171 : 270 : if (!validate_arg (arg, code))
11172 : 0 : goto end;
11173 : : break;
11174 : : }
11175 : : }
11176 : : while (1);
11177 : :
11178 : : /* We need gotos here since we can only have one VA_CLOSE in a
11179 : : function. */
11180 : 90 : end: ;
11181 : 90 : va_end (ap);
11182 : :
11183 : 90 : return res;
11184 : : }
11185 : :
11186 : : /* Default target-specific builtin expander that does nothing. */
11187 : :
11188 : : rtx
11189 : 0 : default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11190 : : rtx target ATTRIBUTE_UNUSED,
11191 : : rtx subtarget ATTRIBUTE_UNUSED,
11192 : : machine_mode mode ATTRIBUTE_UNUSED,
11193 : : int ignore ATTRIBUTE_UNUSED)
11194 : : {
11195 : 0 : return NULL_RTX;
11196 : : }
11197 : :
11198 : : /* Returns true is EXP represents data that would potentially reside
11199 : : in a readonly section. */
11200 : :
11201 : : bool
11202 : 185385 : readonly_data_expr (tree exp)
11203 : : {
11204 : 185385 : STRIP_NOPS (exp);
11205 : :
11206 : 185385 : if (TREE_CODE (exp) != ADDR_EXPR)
11207 : : return false;
11208 : :
11209 : 23507 : exp = get_base_address (TREE_OPERAND (exp, 0));
11210 : 23507 : if (!exp)
11211 : : return false;
11212 : :
11213 : : /* Make sure we call decl_readonly_section only for trees it
11214 : : can handle (since it returns true for everything it doesn't
11215 : : understand). */
11216 : 23507 : if (TREE_CODE (exp) == STRING_CST
11217 : 5495 : || TREE_CODE (exp) == CONSTRUCTOR
11218 : 5495 : || (VAR_P (exp) && TREE_STATIC (exp)))
11219 : 20441 : return decl_readonly_section (exp, 0);
11220 : : else
11221 : : return false;
11222 : : }
11223 : :
11224 : : /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11225 : : to the call, and TYPE is its return type.
11226 : :
11227 : : Return NULL_TREE if no simplification was possible, otherwise return the
11228 : : simplified form of the call as a tree.
11229 : :
11230 : : The simplified form may be a constant or other expression which
11231 : : computes the same value, but in a more efficient manner (including
11232 : : calls to other builtin functions).
11233 : :
11234 : : The call may contain arguments which need to be evaluated, but
11235 : : which are not useful to determine the result of the call. In
11236 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11237 : : COMPOUND_EXPR will be an argument which must be evaluated.
11238 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11239 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11240 : : form of the builtin function call. */
11241 : :
11242 : : static tree
11243 : 79925 : fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
11244 : : {
11245 : 79925 : if (!validate_arg (s1, POINTER_TYPE)
11246 : 79925 : || !validate_arg (s2, POINTER_TYPE))
11247 : : return NULL_TREE;
11248 : :
11249 : 79925 : tree fn;
11250 : 79925 : const char *p1, *p2;
11251 : :
11252 : 79925 : p2 = c_getstr (s2);
11253 : 79925 : if (p2 == NULL)
11254 : : return NULL_TREE;
11255 : :
11256 : 92 : p1 = c_getstr (s1);
11257 : 92 : if (p1 != NULL)
11258 : : {
11259 : 22 : const char *r = strpbrk (p1, p2);
11260 : 22 : tree tem;
11261 : :
11262 : 22 : if (r == NULL)
11263 : 0 : return build_int_cst (TREE_TYPE (s1), 0);
11264 : :
11265 : : /* Return an offset into the constant string argument. */
11266 : 22 : tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11267 : 22 : return fold_convert_loc (loc, type, tem);
11268 : : }
11269 : :
11270 : 70 : if (p2[0] == '\0')
11271 : : /* strpbrk(x, "") == NULL.
11272 : : Evaluate and ignore s1 in case it had side-effects. */
11273 : 26 : return omit_one_operand_loc (loc, type, integer_zero_node, s1);
11274 : :
11275 : 44 : if (p2[1] != '\0')
11276 : : return NULL_TREE; /* Really call strpbrk. */
11277 : :
11278 : 79877 : fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11279 : 41 : if (!fn)
11280 : : return NULL_TREE;
11281 : :
11282 : : /* New argument list transforming strpbrk(s1, s2) to
11283 : : strchr(s1, s2[0]). */
11284 : 41 : return build_call_expr_loc (loc, fn, 2, s1,
11285 : 41 : build_int_cst (integer_type_node, p2[0]));
11286 : : }
11287 : :
11288 : : /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11289 : : to the call.
11290 : :
11291 : : Return NULL_TREE if no simplification was possible, otherwise return the
11292 : : simplified form of the call as a tree.
11293 : :
11294 : : The simplified form may be a constant or other expression which
11295 : : computes the same value, but in a more efficient manner (including
11296 : : calls to other builtin functions).
11297 : :
11298 : : The call may contain arguments which need to be evaluated, but
11299 : : which are not useful to determine the result of the call. In
11300 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11301 : : COMPOUND_EXPR will be an argument which must be evaluated.
11302 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11303 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11304 : : form of the builtin function call. */
11305 : :
11306 : : static tree
11307 : 2655 : fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11308 : : {
11309 : 2655 : if (!validate_arg (s1, POINTER_TYPE)
11310 : 2655 : || !validate_arg (s2, POINTER_TYPE))
11311 : : return NULL_TREE;
11312 : :
11313 : 2655 : if (!check_nul_terminated_array (expr, s1)
11314 : 2655 : || !check_nul_terminated_array (expr, s2))
11315 : 60 : return NULL_TREE;
11316 : :
11317 : 2595 : const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11318 : :
11319 : : /* If either argument is "", return NULL_TREE. */
11320 : 2595 : if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11321 : : /* Evaluate and ignore both arguments in case either one has
11322 : : side-effects. */
11323 : 147 : return omit_two_operands_loc (loc, type, size_zero_node, s1, s2);
11324 : : return NULL_TREE;
11325 : : }
11326 : :
11327 : : /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11328 : : to the call.
11329 : :
11330 : : Return NULL_TREE if no simplification was possible, otherwise return the
11331 : : simplified form of the call as a tree.
11332 : :
11333 : : The simplified form may be a constant or other expression which
11334 : : computes the same value, but in a more efficient manner (including
11335 : : calls to other builtin functions).
11336 : :
11337 : : The call may contain arguments which need to be evaluated, but
11338 : : which are not useful to determine the result of the call. In
11339 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11340 : : COMPOUND_EXPR will be an argument which must be evaluated.
11341 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11342 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11343 : : form of the builtin function call. */
11344 : :
11345 : : static tree
11346 : 2553 : fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11347 : : {
11348 : 2553 : if (!validate_arg (s1, POINTER_TYPE)
11349 : 2553 : || !validate_arg (s2, POINTER_TYPE))
11350 : : return NULL_TREE;
11351 : :
11352 : 2553 : if (!check_nul_terminated_array (expr, s1)
11353 : 2553 : || !check_nul_terminated_array (expr, s2))
11354 : 60 : return NULL_TREE;
11355 : :
11356 : : /* If the first argument is "", return NULL_TREE. */
11357 : 2493 : const char *p1 = c_getstr (s1);
11358 : 2493 : if (p1 && *p1 == '\0')
11359 : : {
11360 : : /* Evaluate and ignore argument s2 in case it has
11361 : : side-effects. */
11362 : 65 : return omit_one_operand_loc (loc, type, size_zero_node, s2);
11363 : : }
11364 : :
11365 : : /* If the second argument is "", return __builtin_strlen(s1). */
11366 : 2428 : const char *p2 = c_getstr (s2);
11367 : 2428 : if (p2 && *p2 == '\0')
11368 : : {
11369 : 2488 : tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11370 : :
11371 : : /* If the replacement _DECL isn't initialized, don't do the
11372 : : transformation. */
11373 : 81 : if (!fn)
11374 : : return NULL_TREE;
11375 : :
11376 : 81 : return fold_convert_loc (loc, type,
11377 : 81 : build_call_expr_loc (loc, fn, 1, s1));
11378 : : }
11379 : : return NULL_TREE;
11380 : : }
11381 : :
11382 : : /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11383 : : produced. False otherwise. This is done so that we don't output the error
11384 : : or warning twice or three times. */
11385 : :
11386 : : bool
11387 : 41582 : fold_builtin_next_arg (tree exp, bool va_start_p)
11388 : : {
11389 : 41582 : tree fntype = TREE_TYPE (current_function_decl);
11390 : 41582 : int nargs = call_expr_nargs (exp);
11391 : 41582 : tree arg;
11392 : : /* There is good chance the current input_location points inside the
11393 : : definition of the va_start macro (perhaps on the token for
11394 : : builtin) in a system header, so warnings will not be emitted.
11395 : : Use the location in real source code. */
11396 : 41582 : location_t current_location =
11397 : 41582 : linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11398 : : NULL);
11399 : :
11400 : 41582 : if (!stdarg_p (fntype))
11401 : : {
11402 : 8 : error ("%<va_start%> used in function with fixed arguments");
11403 : 8 : return true;
11404 : : }
11405 : :
11406 : 41574 : if (va_start_p)
11407 : : {
11408 : 41424 : if (va_start_p && (nargs != 2))
11409 : : {
11410 : 0 : error ("wrong number of arguments to function %<va_start%>");
11411 : 0 : return true;
11412 : : }
11413 : 41424 : arg = CALL_EXPR_ARG (exp, 1);
11414 : : }
11415 : : /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11416 : : when we checked the arguments and if needed issued a warning. */
11417 : : else
11418 : : {
11419 : 150 : if (nargs == 0)
11420 : : {
11421 : : /* Evidently an out of date version of <stdarg.h>; can't validate
11422 : : va_start's second argument, but can still work as intended. */
11423 : 0 : warning_at (current_location,
11424 : : OPT_Wvarargs,
11425 : : "%<__builtin_next_arg%> called without an argument");
11426 : 0 : return true;
11427 : : }
11428 : 150 : else if (nargs > 1)
11429 : : {
11430 : 0 : error ("wrong number of arguments to function %<__builtin_next_arg%>");
11431 : 0 : return true;
11432 : : }
11433 : 150 : arg = CALL_EXPR_ARG (exp, 0);
11434 : : }
11435 : :
11436 : 41574 : if (TREE_CODE (arg) == SSA_NAME
11437 : 41574 : && SSA_NAME_VAR (arg))
11438 : : arg = SSA_NAME_VAR (arg);
11439 : :
11440 : : /* We destructively modify the call to be __builtin_va_start (ap, 0)
11441 : : or __builtin_next_arg (0) the first time we see it, after checking
11442 : : the arguments and if needed issuing a warning. */
11443 : 41574 : if (!integer_zerop (arg))
11444 : : {
11445 : 7080 : tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11446 : :
11447 : : /* Strip off all nops for the sake of the comparison. This
11448 : : is not quite the same as STRIP_NOPS. It does more.
11449 : : We must also strip off INDIRECT_EXPR for C++ reference
11450 : : parameters. */
11451 : 14163 : while (CONVERT_EXPR_P (arg)
11452 : 14177 : || INDIRECT_REF_P (arg))
11453 : 14 : arg = TREE_OPERAND (arg, 0);
11454 : 7080 : if (arg != last_parm)
11455 : : {
11456 : : /* FIXME: Sometimes with the tree optimizers we can get the
11457 : : not the last argument even though the user used the last
11458 : : argument. We just warn and set the arg to be the last
11459 : : argument so that we will get wrong-code because of
11460 : : it. */
11461 : 12 : warning_at (current_location,
11462 : : OPT_Wvarargs,
11463 : : "second parameter of %<va_start%> not last named argument");
11464 : : }
11465 : :
11466 : : /* Undefined by C99 7.15.1.4p4 (va_start):
11467 : : "If the parameter parmN is declared with the register storage
11468 : : class, with a function or array type, or with a type that is
11469 : : not compatible with the type that results after application of
11470 : : the default argument promotions, the behavior is undefined."
11471 : : */
11472 : 7068 : else if (DECL_REGISTER (arg))
11473 : : {
11474 : 12 : warning_at (current_location,
11475 : : OPT_Wvarargs,
11476 : : "undefined behavior when second parameter of "
11477 : : "%<va_start%> is declared with %<register%> storage");
11478 : : }
11479 : :
11480 : : /* We want to verify the second parameter just once before the tree
11481 : : optimizers are run and then avoid keeping it in the tree,
11482 : : as otherwise we could warn even for correct code like:
11483 : : void foo (int i, ...)
11484 : : { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11485 : 7080 : if (va_start_p)
11486 : 7078 : CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11487 : : else
11488 : 2 : CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11489 : : }
11490 : : return false;
11491 : : }
11492 : :
11493 : :
11494 : : /* Expand a call EXP to __builtin_object_size. */
11495 : :
11496 : : static rtx
11497 : 616 : expand_builtin_object_size (tree exp)
11498 : : {
11499 : 616 : tree ost;
11500 : 616 : int object_size_type;
11501 : 616 : tree fndecl = get_callee_fndecl (exp);
11502 : :
11503 : 616 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11504 : : {
11505 : 0 : error ("first argument of %qD must be a pointer, second integer constant",
11506 : : fndecl);
11507 : 0 : expand_builtin_trap ();
11508 : 0 : return const0_rtx;
11509 : : }
11510 : :
11511 : 616 : ost = CALL_EXPR_ARG (exp, 1);
11512 : 616 : STRIP_NOPS (ost);
11513 : :
11514 : 616 : if (TREE_CODE (ost) != INTEGER_CST
11515 : 616 : || tree_int_cst_sgn (ost) < 0
11516 : 1232 : || compare_tree_int (ost, 3) > 0)
11517 : : {
11518 : 0 : error ("last argument of %qD is not integer constant between 0 and 3",
11519 : : fndecl);
11520 : 0 : expand_builtin_trap ();
11521 : 0 : return const0_rtx;
11522 : : }
11523 : :
11524 : 616 : object_size_type = tree_to_shwi (ost);
11525 : :
11526 : 616 : return object_size_type < 2 ? constm1_rtx : const0_rtx;
11527 : : }
11528 : :
11529 : : /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11530 : : FCODE is the BUILT_IN_* to use.
11531 : : Return NULL_RTX if we failed; the caller should emit a normal call,
11532 : : otherwise try to get the result in TARGET, if convenient (and in
11533 : : mode MODE if that's convenient). */
11534 : :
11535 : : static rtx
11536 : 835 : expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11537 : : enum built_in_function fcode)
11538 : : {
11539 : 1408 : if (!validate_arglist (exp,
11540 : : POINTER_TYPE,
11541 : : fcode == BUILT_IN_MEMSET_CHK
11542 : : ? INTEGER_TYPE : POINTER_TYPE,
11543 : : INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11544 : : return NULL_RTX;
11545 : :
11546 : 835 : tree dest = CALL_EXPR_ARG (exp, 0);
11547 : 835 : tree src = CALL_EXPR_ARG (exp, 1);
11548 : 835 : tree len = CALL_EXPR_ARG (exp, 2);
11549 : 835 : tree size = CALL_EXPR_ARG (exp, 3);
11550 : :
11551 : : /* FIXME: Set access mode to write only for memset et al. */
11552 : 835 : bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
11553 : : /*srcstr=*/NULL_TREE, size, access_read_write);
11554 : :
11555 : 835 : if (!tree_fits_uhwi_p (size))
11556 : : return NULL_RTX;
11557 : :
11558 : 625 : if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11559 : : {
11560 : : /* Avoid transforming the checking call to an ordinary one when
11561 : : an overflow has been detected or when the call couldn't be
11562 : : validated because the size is not constant. */
11563 : 185 : if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
11564 : : return NULL_RTX;
11565 : :
11566 : 0 : tree fn = NULL_TREE;
11567 : : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11568 : : mem{cpy,pcpy,move,set} is available. */
11569 : 0 : switch (fcode)
11570 : : {
11571 : 0 : case BUILT_IN_MEMCPY_CHK:
11572 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11573 : 0 : break;
11574 : 0 : case BUILT_IN_MEMPCPY_CHK:
11575 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11576 : 0 : break;
11577 : 0 : case BUILT_IN_MEMMOVE_CHK:
11578 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11579 : 0 : break;
11580 : 0 : case BUILT_IN_MEMSET_CHK:
11581 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11582 : 0 : break;
11583 : : default:
11584 : : break;
11585 : : }
11586 : :
11587 : 0 : if (! fn)
11588 : : return NULL_RTX;
11589 : :
11590 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11591 : 0 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11592 : 0 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11593 : 0 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11594 : : }
11595 : 440 : else if (fcode == BUILT_IN_MEMSET_CHK)
11596 : : return NULL_RTX;
11597 : : else
11598 : : {
11599 : 293 : unsigned int dest_align = get_pointer_alignment (dest);
11600 : :
11601 : : /* If DEST is not a pointer type, call the normal function. */
11602 : 293 : if (dest_align == 0)
11603 : : return NULL_RTX;
11604 : :
11605 : : /* If SRC and DEST are the same (and not volatile), do nothing. */
11606 : 293 : if (operand_equal_p (src, dest, 0))
11607 : : {
11608 : 0 : tree expr;
11609 : :
11610 : 0 : if (fcode != BUILT_IN_MEMPCPY_CHK)
11611 : : {
11612 : : /* Evaluate and ignore LEN in case it has side-effects. */
11613 : 0 : expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11614 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
11615 : : }
11616 : :
11617 : 0 : expr = fold_build_pointer_plus (dest, len);
11618 : 0 : return expand_expr (expr, target, mode, EXPAND_NORMAL);
11619 : : }
11620 : :
11621 : : /* __memmove_chk special case. */
11622 : 293 : if (fcode == BUILT_IN_MEMMOVE_CHK)
11623 : : {
11624 : 79 : unsigned int src_align = get_pointer_alignment (src);
11625 : :
11626 : 79 : if (src_align == 0)
11627 : : return NULL_RTX;
11628 : :
11629 : : /* If src is categorized for a readonly section we can use
11630 : : normal __memcpy_chk. */
11631 : 79 : if (readonly_data_expr (src))
11632 : : {
11633 : 15 : tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11634 : 15 : if (!fn)
11635 : : return NULL_RTX;
11636 : 15 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11637 : : dest, src, len, size);
11638 : 15 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11639 : 15 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11640 : 15 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11641 : : }
11642 : : }
11643 : 278 : return NULL_RTX;
11644 : : }
11645 : : }
11646 : :
11647 : : /* Emit warning if a buffer overflow is detected at compile time. */
11648 : :
11649 : : static void
11650 : 1135 : maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11651 : : {
11652 : : /* The source string. */
11653 : 1135 : tree srcstr = NULL_TREE;
11654 : : /* The size of the destination object returned by __builtin_object_size. */
11655 : 1135 : tree objsize = NULL_TREE;
11656 : : /* The string that is being concatenated with (as in __strcat_chk)
11657 : : or null if it isn't. */
11658 : 1135 : tree catstr = NULL_TREE;
11659 : : /* The maximum length of the source sequence in a bounded operation
11660 : : (such as __strncat_chk) or null if the operation isn't bounded
11661 : : (such as __strcat_chk). */
11662 : 1135 : tree maxread = NULL_TREE;
11663 : : /* The exact size of the access (such as in __strncpy_chk). */
11664 : 1135 : tree size = NULL_TREE;
11665 : : /* The access by the function that's checked. Except for snprintf
11666 : : both writing and reading is checked. */
11667 : 1135 : access_mode mode = access_read_write;
11668 : :
11669 : 1135 : switch (fcode)
11670 : : {
11671 : 276 : case BUILT_IN_STRCPY_CHK:
11672 : 276 : case BUILT_IN_STPCPY_CHK:
11673 : 276 : srcstr = CALL_EXPR_ARG (exp, 1);
11674 : 276 : objsize = CALL_EXPR_ARG (exp, 2);
11675 : 276 : break;
11676 : :
11677 : 198 : case BUILT_IN_STRCAT_CHK:
11678 : : /* For __strcat_chk the warning will be emitted only if overflowing
11679 : : by at least strlen (dest) + 1 bytes. */
11680 : 198 : catstr = CALL_EXPR_ARG (exp, 0);
11681 : 198 : srcstr = CALL_EXPR_ARG (exp, 1);
11682 : 198 : objsize = CALL_EXPR_ARG (exp, 2);
11683 : 198 : break;
11684 : :
11685 : 109 : case BUILT_IN_STRNCAT_CHK:
11686 : 109 : catstr = CALL_EXPR_ARG (exp, 0);
11687 : 109 : srcstr = CALL_EXPR_ARG (exp, 1);
11688 : 109 : maxread = CALL_EXPR_ARG (exp, 2);
11689 : 109 : objsize = CALL_EXPR_ARG (exp, 3);
11690 : 109 : break;
11691 : :
11692 : 243 : case BUILT_IN_STRNCPY_CHK:
11693 : 243 : case BUILT_IN_STPNCPY_CHK:
11694 : 243 : srcstr = CALL_EXPR_ARG (exp, 1);
11695 : 243 : size = CALL_EXPR_ARG (exp, 2);
11696 : 243 : objsize = CALL_EXPR_ARG (exp, 3);
11697 : 243 : break;
11698 : :
11699 : 309 : case BUILT_IN_SNPRINTF_CHK:
11700 : 309 : case BUILT_IN_VSNPRINTF_CHK:
11701 : 309 : maxread = CALL_EXPR_ARG (exp, 1);
11702 : 309 : objsize = CALL_EXPR_ARG (exp, 3);
11703 : : /* The only checked access the write to the destination. */
11704 : 309 : mode = access_write_only;
11705 : 309 : break;
11706 : 0 : default:
11707 : 0 : gcc_unreachable ();
11708 : : }
11709 : :
11710 : 1135 : if (catstr && maxread)
11711 : : {
11712 : : /* Check __strncat_chk. There is no way to determine the length
11713 : : of the string to which the source string is being appended so
11714 : : just warn when the length of the source string is not known. */
11715 : 109 : check_strncat_sizes (exp, objsize);
11716 : 109 : return;
11717 : : }
11718 : :
11719 : 1026 : check_access (exp, size, maxread, srcstr, objsize, mode);
11720 : : }
11721 : :
11722 : : /* Emit warning if a buffer overflow is detected at compile time
11723 : : in __sprintf_chk/__vsprintf_chk calls. */
11724 : :
11725 : : static void
11726 : 1329 : maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11727 : : {
11728 : 1329 : tree size, len, fmt;
11729 : 1329 : const char *fmt_str;
11730 : 1329 : int nargs = call_expr_nargs (exp);
11731 : :
11732 : : /* Verify the required arguments in the original call. */
11733 : :
11734 : 1329 : if (nargs < 4)
11735 : : return;
11736 : 1329 : size = CALL_EXPR_ARG (exp, 2);
11737 : 1329 : fmt = CALL_EXPR_ARG (exp, 3);
11738 : :
11739 : 1329 : if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11740 : 21 : return;
11741 : :
11742 : : /* Check whether the format is a literal string constant. */
11743 : 1308 : fmt_str = c_getstr (fmt);
11744 : 1308 : if (fmt_str == NULL)
11745 : : return;
11746 : :
11747 : 1272 : if (!init_target_chars ())
11748 : : return;
11749 : :
11750 : : /* If the format doesn't contain % args or %%, we know its size. */
11751 : 1272 : if (strchr (fmt_str, target_percent) == 0)
11752 : 22 : len = build_int_cstu (size_type_node, strlen (fmt_str));
11753 : : /* If the format is "%s" and first ... argument is a string literal,
11754 : : we know it too. */
11755 : 1250 : else if (fcode == BUILT_IN_SPRINTF_CHK
11756 : 1094 : && strcmp (fmt_str, target_percent_s) == 0)
11757 : : {
11758 : 49 : tree arg;
11759 : :
11760 : 49 : if (nargs < 5)
11761 : : return;
11762 : 49 : arg = CALL_EXPR_ARG (exp, 4);
11763 : 49 : if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11764 : : return;
11765 : :
11766 : 45 : len = c_strlen (arg, 1);
11767 : 45 : if (!len || ! tree_fits_uhwi_p (len))
11768 : : return;
11769 : : }
11770 : : else
11771 : : return;
11772 : :
11773 : : /* Add one for the terminating nul. */
11774 : 34 : len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
11775 : :
11776 : 34 : check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
11777 : : access_write_only);
11778 : : }
11779 : :
11780 : : /* Fold a call to __builtin_object_size with arguments PTR and OST,
11781 : : if possible. */
11782 : :
11783 : : static tree
11784 : 196243 : fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
11785 : : {
11786 : 196243 : tree bytes;
11787 : 196243 : int object_size_type;
11788 : :
11789 : 196243 : if (!validate_arg (ptr, POINTER_TYPE)
11790 : 196243 : || !validate_arg (ost, INTEGER_TYPE))
11791 : : return NULL_TREE;
11792 : :
11793 : 196243 : STRIP_NOPS (ost);
11794 : :
11795 : 196243 : if (TREE_CODE (ost) != INTEGER_CST
11796 : 196243 : || tree_int_cst_sgn (ost) < 0
11797 : 392486 : || compare_tree_int (ost, 3) > 0)
11798 : 0 : return NULL_TREE;
11799 : :
11800 : 196243 : object_size_type = tree_to_shwi (ost);
11801 : :
11802 : : /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11803 : : if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11804 : : and (size_t) 0 for types 2 and 3. */
11805 : 196243 : if (TREE_SIDE_EFFECTS (ptr))
11806 : 603 : return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11807 : :
11808 : 195640 : if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
11809 : 63657 : object_size_type |= OST_DYNAMIC;
11810 : :
11811 : 195640 : if (TREE_CODE (ptr) == ADDR_EXPR)
11812 : : {
11813 : 12084 : compute_builtin_object_size (ptr, object_size_type, &bytes);
11814 : 12084 : if ((object_size_type & OST_DYNAMIC)
11815 : 10889 : || int_fits_type_p (bytes, size_type_node))
11816 : 12084 : return fold_convert (size_type_node, bytes);
11817 : : }
11818 : 183556 : else if (TREE_CODE (ptr) == SSA_NAME)
11819 : : {
11820 : : /* If object size is not known yet, delay folding until
11821 : : later. Maybe subsequent passes will help determining
11822 : : it. */
11823 : 99570 : if (compute_builtin_object_size (ptr, object_size_type, &bytes)
11824 : 99570 : && ((object_size_type & OST_DYNAMIC)
11825 : 2544 : || int_fits_type_p (bytes, size_type_node)))
11826 : 4795 : return fold_convert (size_type_node, bytes);
11827 : : }
11828 : :
11829 : : return NULL_TREE;
11830 : : }
11831 : :
11832 : : /* Builtins with folding operations that operate on "..." arguments
11833 : : need special handling; we need to store the arguments in a convenient
11834 : : data structure before attempting any folding. Fortunately there are
11835 : : only a few builtins that fall into this category. FNDECL is the
11836 : : function, EXP is the CALL_EXPR for the call. */
11837 : :
11838 : : static tree
11839 : 2309188 : fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11840 : : {
11841 : 2309188 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11842 : 2309188 : tree ret = NULL_TREE;
11843 : :
11844 : 2309188 : switch (fcode)
11845 : : {
11846 : 107935 : case BUILT_IN_FPCLASSIFY:
11847 : 107935 : ret = fold_builtin_fpclassify (loc, args, nargs);
11848 : 107935 : break;
11849 : :
11850 : 54 : case BUILT_IN_ADDC:
11851 : 54 : case BUILT_IN_ADDCL:
11852 : 54 : case BUILT_IN_ADDCLL:
11853 : 54 : case BUILT_IN_SUBC:
11854 : 54 : case BUILT_IN_SUBCL:
11855 : 54 : case BUILT_IN_SUBCLL:
11856 : 54 : return fold_builtin_addc_subc (loc, fcode, args);
11857 : :
11858 : : default:
11859 : : break;
11860 : : }
11861 : 107935 : if (ret)
11862 : : {
11863 : 107935 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11864 : 107935 : SET_EXPR_LOCATION (ret, loc);
11865 : 107935 : suppress_warning (ret);
11866 : 107935 : return ret;
11867 : : }
11868 : : return NULL_TREE;
11869 : : }
11870 : :
11871 : : /* Initialize format string characters in the target charset. */
11872 : :
11873 : : bool
11874 : 280156 : init_target_chars (void)
11875 : : {
11876 : 280156 : static bool init;
11877 : 280156 : if (!init)
11878 : : {
11879 : 109642 : target_newline = lang_hooks.to_target_charset ('\n');
11880 : 109642 : target_percent = lang_hooks.to_target_charset ('%');
11881 : 109642 : target_c = lang_hooks.to_target_charset ('c');
11882 : 109642 : target_s = lang_hooks.to_target_charset ('s');
11883 : 109642 : if (target_newline == 0 || target_percent == 0 || target_c == 0
11884 : 109642 : || target_s == 0)
11885 : : return false;
11886 : :
11887 : 109642 : target_percent_c[0] = target_percent;
11888 : 109642 : target_percent_c[1] = target_c;
11889 : 109642 : target_percent_c[2] = '\0';
11890 : :
11891 : 109642 : target_percent_s[0] = target_percent;
11892 : 109642 : target_percent_s[1] = target_s;
11893 : 109642 : target_percent_s[2] = '\0';
11894 : :
11895 : 109642 : target_percent_s_newline[0] = target_percent;
11896 : 109642 : target_percent_s_newline[1] = target_s;
11897 : 109642 : target_percent_s_newline[2] = target_newline;
11898 : 109642 : target_percent_s_newline[3] = '\0';
11899 : :
11900 : 109642 : init = true;
11901 : : }
11902 : : return true;
11903 : : }
11904 : :
11905 : : /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11906 : : and no overflow/underflow occurred. INEXACT is true if M was not
11907 : : exactly calculated. TYPE is the tree type for the result. This
11908 : : function assumes that you cleared the MPFR flags and then
11909 : : calculated M to see if anything subsequently set a flag prior to
11910 : : entering this function. Return NULL_TREE if any checks fail. */
11911 : :
11912 : : static tree
11913 : 3041 : do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11914 : : {
11915 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11916 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11917 : : result of calling FUNC was exact. */
11918 : 4898 : if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11919 : 4898 : && (!flag_rounding_math || !inexact))
11920 : : {
11921 : 1857 : REAL_VALUE_TYPE rr;
11922 : :
11923 : 1857 : real_from_mpfr (&rr, m, type, MPFR_RNDN);
11924 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11925 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11926 : : but the mpfr_t is not, then we underflowed in the
11927 : : conversion. */
11928 : 1857 : if (real_isfinite (&rr)
11929 : 1857 : && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11930 : : {
11931 : 1857 : REAL_VALUE_TYPE rmode;
11932 : :
11933 : 1857 : real_convert (&rmode, TYPE_MODE (type), &rr);
11934 : : /* Proceed iff the specified mode can hold the value. */
11935 : 1857 : if (real_identical (&rmode, &rr))
11936 : 1857 : return build_real (type, rmode);
11937 : : }
11938 : : }
11939 : : return NULL_TREE;
11940 : : }
11941 : :
11942 : : /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11943 : : number and no overflow/underflow occurred. INEXACT is true if M
11944 : : was not exactly calculated. TYPE is the tree type for the result.
11945 : : This function assumes that you cleared the MPFR flags and then
11946 : : calculated M to see if anything subsequently set a flag prior to
11947 : : entering this function. Return NULL_TREE if any checks fail, if
11948 : : FORCE_CONVERT is true, then bypass the checks. */
11949 : :
11950 : : static tree
11951 : 4087 : do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11952 : : {
11953 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11954 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11955 : : result of calling FUNC was exact. */
11956 : 4087 : if (force_convert
11957 : 4087 : || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11958 : 3741 : && !mpfr_overflow_p () && !mpfr_underflow_p ()
11959 : 3741 : && (!flag_rounding_math || !inexact)))
11960 : : {
11961 : 3908 : REAL_VALUE_TYPE re, im;
11962 : :
11963 : 3908 : real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
11964 : 3908 : real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
11965 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11966 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11967 : : but the mpfr_t is not, then we underflowed in the
11968 : : conversion. */
11969 : 3908 : if (force_convert
11970 : 3908 : || (real_isfinite (&re) && real_isfinite (&im)
11971 : 3741 : && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11972 : 3741 : && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11973 : : {
11974 : 3908 : REAL_VALUE_TYPE re_mode, im_mode;
11975 : :
11976 : 3908 : real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11977 : 3908 : real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11978 : : /* Proceed iff the specified mode can hold the value. */
11979 : 3908 : if (force_convert
11980 : 3908 : || (real_identical (&re_mode, &re)
11981 : 3741 : && real_identical (&im_mode, &im)))
11982 : 3908 : return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11983 : 7816 : build_real (TREE_TYPE (type), im_mode));
11984 : : }
11985 : : }
11986 : : return NULL_TREE;
11987 : : }
11988 : :
11989 : : /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11990 : : the pointer *(ARG_QUO) and return the result. The type is taken
11991 : : from the type of ARG0 and is used for setting the precision of the
11992 : : calculation and results. */
11993 : :
11994 : : static tree
11995 : 80710 : do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
11996 : : {
11997 : 80710 : tree const type = TREE_TYPE (arg0);
11998 : 80710 : tree result = NULL_TREE;
11999 : :
12000 : 80710 : STRIP_NOPS (arg0);
12001 : 80710 : STRIP_NOPS (arg1);
12002 : :
12003 : : /* To proceed, MPFR must exactly represent the target floating point
12004 : : format, which only happens when the target base equals two. */
12005 : 80710 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12006 : 80710 : && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12007 : 83075 : && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12008 : : {
12009 : 2365 : const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12010 : 2365 : const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12011 : :
12012 : 2365 : if (real_isfinite (ra0) && real_isfinite (ra1))
12013 : : {
12014 : 2365 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12015 : 2365 : const int prec = fmt->p;
12016 : 2365 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12017 : 2365 : tree result_rem;
12018 : 2365 : long integer_quo;
12019 : 2365 : mpfr_t m0, m1;
12020 : :
12021 : 2365 : mpfr_inits2 (prec, m0, m1, NULL);
12022 : 2365 : mpfr_from_real (m0, ra0, MPFR_RNDN);
12023 : 2365 : mpfr_from_real (m1, ra1, MPFR_RNDN);
12024 : 2365 : mpfr_clear_flags ();
12025 : 2365 : mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12026 : : /* Remquo is independent of the rounding mode, so pass
12027 : : inexact=0 to do_mpfr_ckconv(). */
12028 : 2365 : result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12029 : 2365 : mpfr_clears (m0, m1, NULL);
12030 : 2365 : if (result_rem)
12031 : : {
12032 : : /* MPFR calculates quo in the host's long so it may
12033 : : return more bits in quo than the target int can hold
12034 : : if sizeof(host long) > sizeof(target int). This can
12035 : : happen even for native compilers in LP64 mode. In
12036 : : these cases, modulo the quo value with the largest
12037 : : number that the target int can hold while leaving one
12038 : : bit for the sign. */
12039 : 1181 : if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12040 : 1181 : integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12041 : :
12042 : : /* Dereference the quo pointer argument. */
12043 : 1181 : arg_quo = build_fold_indirect_ref (arg_quo);
12044 : : /* Proceed iff a valid pointer type was passed in. */
12045 : 1181 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12046 : : {
12047 : : /* Set the value. */
12048 : 1181 : tree result_quo
12049 : 1181 : = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12050 : : build_int_cst (TREE_TYPE (arg_quo),
12051 : : integer_quo));
12052 : 1181 : TREE_SIDE_EFFECTS (result_quo) = 1;
12053 : : /* Combine the quo assignment with the rem. */
12054 : 1181 : result = fold_build2 (COMPOUND_EXPR, type,
12055 : : result_quo, result_rem);
12056 : 1181 : suppress_warning (result, OPT_Wunused_value);
12057 : 1181 : result = non_lvalue (result);
12058 : : }
12059 : : }
12060 : : }
12061 : : }
12062 : 80710 : return result;
12063 : : }
12064 : :
12065 : : /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12066 : : resulting value as a tree with type TYPE. The mpfr precision is
12067 : : set to the precision of TYPE. We assume that this mpfr function
12068 : : returns zero if the result could be calculated exactly within the
12069 : : requested precision. In addition, the integer pointer represented
12070 : : by ARG_SG will be dereferenced and set to the appropriate signgam
12071 : : (-1,1) value. */
12072 : :
12073 : : static tree
12074 : 6722 : do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12075 : : {
12076 : 6722 : tree result = NULL_TREE;
12077 : :
12078 : 6722 : STRIP_NOPS (arg);
12079 : :
12080 : : /* To proceed, MPFR must exactly represent the target floating point
12081 : : format, which only happens when the target base equals two. Also
12082 : : verify ARG is a constant and that ARG_SG is an int pointer. */
12083 : 6722 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12084 : 6722 : && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12085 : 6688 : && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12086 : 13410 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12087 : : {
12088 : 6688 : const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12089 : :
12090 : : /* In addition to NaN and Inf, the argument cannot be zero or a
12091 : : negative integer. */
12092 : 6688 : if (real_isfinite (ra)
12093 : 6688 : && ra->cl != rvc_zero
12094 : 13376 : && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12095 : : {
12096 : 676 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12097 : 676 : const int prec = fmt->p;
12098 : 676 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12099 : 676 : int inexact, sg;
12100 : 676 : tree result_lg;
12101 : :
12102 : 676 : auto_mpfr m (prec);
12103 : 676 : mpfr_from_real (m, ra, MPFR_RNDN);
12104 : 676 : mpfr_clear_flags ();
12105 : 676 : inexact = mpfr_lgamma (m, &sg, m, rnd);
12106 : 676 : result_lg = do_mpfr_ckconv (m, type, inexact);
12107 : 676 : if (result_lg)
12108 : : {
12109 : 676 : tree result_sg;
12110 : :
12111 : : /* Dereference the arg_sg pointer argument. */
12112 : 676 : arg_sg = build_fold_indirect_ref (arg_sg);
12113 : : /* Assign the signgam value into *arg_sg. */
12114 : 676 : result_sg = fold_build2 (MODIFY_EXPR,
12115 : : TREE_TYPE (arg_sg), arg_sg,
12116 : : build_int_cst (TREE_TYPE (arg_sg), sg));
12117 : 676 : TREE_SIDE_EFFECTS (result_sg) = 1;
12118 : : /* Combine the signgam assignment with the lgamma result. */
12119 : 676 : result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12120 : : result_sg, result_lg));
12121 : : }
12122 : 676 : }
12123 : : }
12124 : :
12125 : 6722 : return result;
12126 : : }
12127 : :
12128 : : /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12129 : : mpc function FUNC on it and return the resulting value as a tree
12130 : : with type TYPE. The mpfr precision is set to the precision of
12131 : : TYPE. We assume that function FUNC returns zero if the result
12132 : : could be calculated exactly within the requested precision. If
12133 : : DO_NONFINITE is true, then fold expressions containing Inf or NaN
12134 : : in the arguments and/or results. */
12135 : :
12136 : : tree
12137 : 4439 : do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12138 : : int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12139 : : {
12140 : 4439 : tree result = NULL_TREE;
12141 : :
12142 : 4439 : STRIP_NOPS (arg0);
12143 : 4439 : STRIP_NOPS (arg1);
12144 : :
12145 : : /* To proceed, MPFR must exactly represent the target floating point
12146 : : format, which only happens when the target base equals two. */
12147 : 4439 : if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12148 : 4439 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
12149 : 4439 : && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12150 : 4439 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
12151 : 8878 : && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12152 : : {
12153 : 4439 : const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12154 : 4439 : const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12155 : 4439 : const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12156 : 4439 : const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12157 : :
12158 : 4439 : if (do_nonfinite
12159 : 4439 : || (real_isfinite (re0) && real_isfinite (im0)
12160 : 3948 : && real_isfinite (re1) && real_isfinite (im1)))
12161 : : {
12162 : 4087 : const struct real_format *const fmt =
12163 : 4087 : REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12164 : 4087 : const int prec = fmt->p;
12165 : 8174 : const mpfr_rnd_t rnd = fmt->round_towards_zero
12166 : 4087 : ? MPFR_RNDZ : MPFR_RNDN;
12167 : 4087 : const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12168 : 4087 : int inexact;
12169 : 4087 : mpc_t m0, m1;
12170 : :
12171 : 4087 : mpc_init2 (m0, prec);
12172 : 4087 : mpc_init2 (m1, prec);
12173 : 4087 : mpfr_from_real (mpc_realref (m0), re0, rnd);
12174 : 4087 : mpfr_from_real (mpc_imagref (m0), im0, rnd);
12175 : 4087 : mpfr_from_real (mpc_realref (m1), re1, rnd);
12176 : 4087 : mpfr_from_real (mpc_imagref (m1), im1, rnd);
12177 : 4087 : mpfr_clear_flags ();
12178 : 4087 : inexact = func (m0, m0, m1, crnd);
12179 : 4087 : result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12180 : 4087 : mpc_clear (m0);
12181 : 4087 : mpc_clear (m1);
12182 : : }
12183 : : }
12184 : :
12185 : 4439 : return result;
12186 : : }
12187 : :
12188 : : /* A wrapper function for builtin folding that prevents warnings for
12189 : : "statement without effect" and the like, caused by removing the
12190 : : call node earlier than the warning is generated. */
12191 : :
12192 : : tree
12193 : 9590086 : fold_call_stmt (gcall *stmt, bool ignore)
12194 : : {
12195 : 9590086 : tree ret = NULL_TREE;
12196 : 9590086 : tree fndecl = gimple_call_fndecl (stmt);
12197 : 9590086 : location_t loc = gimple_location (stmt);
12198 : 9590086 : if (fndecl && fndecl_built_in_p (fndecl)
12199 : 19180172 : && !gimple_call_va_arg_pack_p (stmt))
12200 : : {
12201 : 9590020 : int nargs = gimple_call_num_args (stmt);
12202 : 9590020 : tree *args = (nargs > 0
12203 : 9590020 : ? gimple_call_arg_ptr (stmt, 0)
12204 : 9590020 : : &error_mark_node);
12205 : :
12206 : 9590020 : if (avoid_folding_inline_builtin (fndecl))
12207 : : return NULL_TREE;
12208 : 9590020 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12209 : : {
12210 : 0 : return targetm.fold_builtin (fndecl, nargs, args, ignore);
12211 : : }
12212 : : else
12213 : : {
12214 : 9590020 : ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
12215 : 9590020 : if (ret)
12216 : : {
12217 : : /* Propagate location information from original call to
12218 : : expansion of builtin. Otherwise things like
12219 : : maybe_emit_chk_warning, that operate on the expansion
12220 : : of a builtin, will use the wrong location information. */
12221 : 5572 : if (gimple_has_location (stmt))
12222 : : {
12223 : 5571 : tree realret = ret;
12224 : 5571 : if (TREE_CODE (ret) == NOP_EXPR)
12225 : 5571 : realret = TREE_OPERAND (ret, 0);
12226 : 5571 : if (CAN_HAVE_LOCATION_P (realret)
12227 : 9733 : && !EXPR_HAS_LOCATION (realret))
12228 : 5 : SET_EXPR_LOCATION (realret, loc);
12229 : 5571 : return realret;
12230 : : }
12231 : : return ret;
12232 : : }
12233 : : }
12234 : : }
12235 : : return NULL_TREE;
12236 : : }
12237 : :
12238 : : /* Look up the function in builtin_decl that corresponds to DECL
12239 : : and set ASMSPEC as its user assembler name. DECL must be a
12240 : : function decl that declares a builtin. */
12241 : :
12242 : : void
12243 : 145132 : set_builtin_user_assembler_name (tree decl, const char *asmspec)
12244 : : {
12245 : 145132 : gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
12246 : : && asmspec != 0);
12247 : :
12248 : 145132 : tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12249 : 145132 : set_user_assembler_name (builtin, asmspec);
12250 : :
12251 : 145132 : if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
12252 : 145132 : && INT_TYPE_SIZE < BITS_PER_WORD)
12253 : : {
12254 : 1 : scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
12255 : 1 : set_user_assembler_libfunc ("ffs", asmspec);
12256 : 1 : set_optab_libfunc (ffs_optab, mode, "ffs");
12257 : : }
12258 : 145132 : }
12259 : :
12260 : : /* Return true if DECL is a builtin that expands to a constant or similarly
12261 : : simple code. */
12262 : : bool
12263 : 29827427 : is_simple_builtin (tree decl)
12264 : : {
12265 : 29827427 : if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
12266 : 28560976 : switch (DECL_FUNCTION_CODE (decl))
12267 : : {
12268 : : /* Builtins that expand to constants. */
12269 : : case BUILT_IN_CONSTANT_P:
12270 : : case BUILT_IN_EXPECT:
12271 : : case BUILT_IN_OBJECT_SIZE:
12272 : : case BUILT_IN_UNREACHABLE:
12273 : : /* Simple register moves or loads from stack. */
12274 : : case BUILT_IN_ASSUME_ALIGNED:
12275 : : case BUILT_IN_RETURN_ADDRESS:
12276 : : case BUILT_IN_EXTRACT_RETURN_ADDR:
12277 : : case BUILT_IN_FROB_RETURN_ADDR:
12278 : : case BUILT_IN_RETURN:
12279 : : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12280 : : case BUILT_IN_FRAME_ADDRESS:
12281 : : case BUILT_IN_VA_END:
12282 : : case BUILT_IN_STACK_SAVE:
12283 : : case BUILT_IN_STACK_RESTORE:
12284 : : case BUILT_IN_DWARF_CFA:
12285 : : /* Exception state returns or moves registers around. */
12286 : : case BUILT_IN_EH_FILTER:
12287 : : case BUILT_IN_EH_POINTER:
12288 : : case BUILT_IN_EH_COPY_VALUES:
12289 : : return true;
12290 : :
12291 : 26258258 : default:
12292 : 26258258 : return false;
12293 : : }
12294 : :
12295 : : return false;
12296 : : }
12297 : :
12298 : : /* Return true if DECL is a builtin that is not expensive, i.e., they are
12299 : : most probably expanded inline into reasonably simple code. This is a
12300 : : superset of is_simple_builtin. */
12301 : : bool
12302 : 18107658 : is_inexpensive_builtin (tree decl)
12303 : : {
12304 : 18107658 : if (!decl)
12305 : : return false;
12306 : 18093090 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12307 : : return true;
12308 : 16803999 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12309 : 14160477 : switch (DECL_FUNCTION_CODE (decl))
12310 : : {
12311 : : case BUILT_IN_ABS:
12312 : : CASE_BUILT_IN_ALLOCA:
12313 : : case BUILT_IN_BSWAP16:
12314 : : case BUILT_IN_BSWAP32:
12315 : : case BUILT_IN_BSWAP64:
12316 : : case BUILT_IN_BSWAP128:
12317 : : case BUILT_IN_CLZ:
12318 : : case BUILT_IN_CLZIMAX:
12319 : : case BUILT_IN_CLZL:
12320 : : case BUILT_IN_CLZLL:
12321 : : case BUILT_IN_CTZ:
12322 : : case BUILT_IN_CTZIMAX:
12323 : : case BUILT_IN_CTZL:
12324 : : case BUILT_IN_CTZLL:
12325 : : case BUILT_IN_FFS:
12326 : : case BUILT_IN_FFSIMAX:
12327 : : case BUILT_IN_FFSL:
12328 : : case BUILT_IN_FFSLL:
12329 : : case BUILT_IN_IMAXABS:
12330 : : case BUILT_IN_FINITE:
12331 : : case BUILT_IN_FINITEF:
12332 : : case BUILT_IN_FINITEL:
12333 : : case BUILT_IN_FINITED32:
12334 : : case BUILT_IN_FINITED64:
12335 : : case BUILT_IN_FINITED128:
12336 : : case BUILT_IN_FPCLASSIFY:
12337 : : case BUILT_IN_ISFINITE:
12338 : : case BUILT_IN_ISINF_SIGN:
12339 : : case BUILT_IN_ISINF:
12340 : : case BUILT_IN_ISINFF:
12341 : : case BUILT_IN_ISINFL:
12342 : : case BUILT_IN_ISINFD32:
12343 : : case BUILT_IN_ISINFD64:
12344 : : case BUILT_IN_ISINFD128:
12345 : : case BUILT_IN_ISNAN:
12346 : : case BUILT_IN_ISNANF:
12347 : : case BUILT_IN_ISNANL:
12348 : : case BUILT_IN_ISNAND32:
12349 : : case BUILT_IN_ISNAND64:
12350 : : case BUILT_IN_ISNAND128:
12351 : : case BUILT_IN_ISNORMAL:
12352 : : case BUILT_IN_ISGREATER:
12353 : : case BUILT_IN_ISGREATEREQUAL:
12354 : : case BUILT_IN_ISLESS:
12355 : : case BUILT_IN_ISLESSEQUAL:
12356 : : case BUILT_IN_ISLESSGREATER:
12357 : : case BUILT_IN_ISUNORDERED:
12358 : : case BUILT_IN_ISEQSIG:
12359 : : case BUILT_IN_VA_ARG_PACK:
12360 : : case BUILT_IN_VA_ARG_PACK_LEN:
12361 : : case BUILT_IN_VA_COPY:
12362 : : case BUILT_IN_TRAP:
12363 : : case BUILT_IN_UNREACHABLE_TRAP:
12364 : : case BUILT_IN_SAVEREGS:
12365 : : case BUILT_IN_POPCOUNTL:
12366 : : case BUILT_IN_POPCOUNTLL:
12367 : : case BUILT_IN_POPCOUNTIMAX:
12368 : : case BUILT_IN_POPCOUNT:
12369 : : case BUILT_IN_PARITYL:
12370 : : case BUILT_IN_PARITYLL:
12371 : : case BUILT_IN_PARITYIMAX:
12372 : : case BUILT_IN_PARITY:
12373 : : case BUILT_IN_LABS:
12374 : : case BUILT_IN_LLABS:
12375 : : case BUILT_IN_PREFETCH:
12376 : : case BUILT_IN_ACC_ON_DEVICE:
12377 : : return true;
12378 : :
12379 : 13490697 : default:
12380 : 13490697 : return is_simple_builtin (decl);
12381 : : }
12382 : :
12383 : : return false;
12384 : : }
12385 : :
12386 : : /* Return true if T is a constant and the value cast to a target char
12387 : : can be represented by a host char.
12388 : : Store the casted char constant in *P if so. */
12389 : :
12390 : : bool
12391 : 2765 : target_char_cst_p (tree t, char *p)
12392 : : {
12393 : 2765 : if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
12394 : : return false;
12395 : :
12396 : 1712 : *p = (char)tree_to_uhwi (t);
12397 : 1712 : return true;
12398 : : }
12399 : :
12400 : : /* Return true if the builtin DECL is implemented in a standard library.
12401 : : Otherwise return false which doesn't guarantee it is not (thus the list
12402 : : of handled builtins below may be incomplete). */
12403 : :
12404 : : bool
12405 : 40016 : builtin_with_linkage_p (tree decl)
12406 : : {
12407 : 40016 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12408 : 39738 : switch (DECL_FUNCTION_CODE (decl))
12409 : : {
12410 : 1286 : CASE_FLT_FN (BUILT_IN_ACOS):
12411 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
12412 : 1286 : CASE_FLT_FN (BUILT_IN_ACOSH):
12413 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
12414 : 1286 : CASE_FLT_FN (BUILT_IN_ASIN):
12415 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
12416 : 1286 : CASE_FLT_FN (BUILT_IN_ASINH):
12417 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
12418 : 1286 : CASE_FLT_FN (BUILT_IN_ATAN):
12419 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
12420 : 1286 : CASE_FLT_FN (BUILT_IN_ATANH):
12421 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
12422 : 1286 : CASE_FLT_FN (BUILT_IN_ATAN2):
12423 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
12424 : 1286 : CASE_FLT_FN (BUILT_IN_CBRT):
12425 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
12426 : 1286 : CASE_FLT_FN (BUILT_IN_CEIL):
12427 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
12428 : 1286 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
12429 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
12430 : 1286 : CASE_FLT_FN (BUILT_IN_COS):
12431 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
12432 : 1286 : CASE_FLT_FN (BUILT_IN_COSH):
12433 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
12434 : 1286 : CASE_FLT_FN (BUILT_IN_ERF):
12435 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
12436 : 1286 : CASE_FLT_FN (BUILT_IN_ERFC):
12437 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
12438 : 1286 : CASE_FLT_FN (BUILT_IN_EXP):
12439 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
12440 : 1286 : CASE_FLT_FN (BUILT_IN_EXP2):
12441 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
12442 : 1286 : CASE_FLT_FN (BUILT_IN_EXPM1):
12443 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
12444 : 1286 : CASE_FLT_FN (BUILT_IN_FABS):
12445 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
12446 : 1286 : CASE_FLT_FN (BUILT_IN_FDIM):
12447 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
12448 : 1286 : CASE_FLT_FN (BUILT_IN_FLOOR):
12449 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
12450 : 1286 : CASE_FLT_FN (BUILT_IN_FMA):
12451 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
12452 : 1286 : CASE_FLT_FN (BUILT_IN_FMAX):
12453 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
12454 : 1286 : CASE_FLT_FN (BUILT_IN_FMIN):
12455 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
12456 : 1286 : CASE_FLT_FN (BUILT_IN_FMOD):
12457 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
12458 : 1286 : CASE_FLT_FN (BUILT_IN_FREXP):
12459 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
12460 : 1286 : CASE_FLT_FN (BUILT_IN_HYPOT):
12461 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
12462 : 1286 : CASE_FLT_FN (BUILT_IN_ILOGB):
12463 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
12464 : 1286 : CASE_FLT_FN (BUILT_IN_LDEXP):
12465 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
12466 : 1286 : CASE_FLT_FN (BUILT_IN_LGAMMA):
12467 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
12468 : 1286 : CASE_FLT_FN (BUILT_IN_LLRINT):
12469 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
12470 : 1286 : CASE_FLT_FN (BUILT_IN_LLROUND):
12471 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
12472 : 1286 : CASE_FLT_FN (BUILT_IN_LOG):
12473 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
12474 : 1286 : CASE_FLT_FN (BUILT_IN_LOG10):
12475 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
12476 : 1286 : CASE_FLT_FN (BUILT_IN_LOG1P):
12477 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
12478 : 1286 : CASE_FLT_FN (BUILT_IN_LOG2):
12479 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
12480 : 1286 : CASE_FLT_FN (BUILT_IN_LOGB):
12481 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
12482 : 1286 : CASE_FLT_FN (BUILT_IN_LRINT):
12483 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
12484 : 1286 : CASE_FLT_FN (BUILT_IN_LROUND):
12485 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
12486 : 1286 : CASE_FLT_FN (BUILT_IN_MODF):
12487 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
12488 : 1286 : CASE_FLT_FN (BUILT_IN_NAN):
12489 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
12490 : 1286 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
12491 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
12492 : 1286 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
12493 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
12494 : 1286 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
12495 : 1286 : CASE_FLT_FN (BUILT_IN_POW):
12496 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
12497 : 1286 : CASE_FLT_FN (BUILT_IN_REMAINDER):
12498 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
12499 : 1286 : CASE_FLT_FN (BUILT_IN_REMQUO):
12500 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
12501 : 1286 : CASE_FLT_FN (BUILT_IN_RINT):
12502 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
12503 : 1286 : CASE_FLT_FN (BUILT_IN_ROUND):
12504 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
12505 : 1286 : CASE_FLT_FN (BUILT_IN_SCALBLN):
12506 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
12507 : 1286 : CASE_FLT_FN (BUILT_IN_SCALBN):
12508 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
12509 : 1286 : CASE_FLT_FN (BUILT_IN_SIN):
12510 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
12511 : 1286 : CASE_FLT_FN (BUILT_IN_SINH):
12512 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
12513 : 1286 : CASE_FLT_FN (BUILT_IN_SINCOS):
12514 : 1286 : CASE_FLT_FN (BUILT_IN_SQRT):
12515 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
12516 : 1286 : CASE_FLT_FN (BUILT_IN_TAN):
12517 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
12518 : 1286 : CASE_FLT_FN (BUILT_IN_TANH):
12519 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
12520 : 1286 : CASE_FLT_FN (BUILT_IN_TGAMMA):
12521 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
12522 : 1286 : CASE_FLT_FN (BUILT_IN_TRUNC):
12523 : 1286 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
12524 : 1286 : return true;
12525 : :
12526 : 16 : case BUILT_IN_STPCPY:
12527 : 16 : case BUILT_IN_STPNCPY:
12528 : : /* stpcpy is both referenced in libiberty's pex-win32.c and provided
12529 : : by libiberty's stpcpy.c for MinGW targets so we need to return true
12530 : : in order to be able to build libiberty in LTO mode for them. */
12531 : 16 : return true;
12532 : :
12533 : : default:
12534 : : break;
12535 : : }
12536 : : return false;
12537 : : }
12538 : :
12539 : : /* Return true if OFFRNG is bounded to a subrange of offset values
12540 : : valid for the largest possible object. */
12541 : :
12542 : : bool
12543 : 501 : access_ref::offset_bounded () const
12544 : : {
12545 : 501 : tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
12546 : 501 : tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
12547 : 975 : return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
12548 : : }
12549 : :
12550 : : /* If CALLEE has known side effects, fill in INFO and return true.
12551 : : See tree-ssa-structalias.cc:find_func_aliases
12552 : : for the list of builtins we might need to handle here. */
12553 : :
12554 : : attr_fnspec
12555 : 101096643 : builtin_fnspec (tree callee)
12556 : : {
12557 : 101096643 : built_in_function code = DECL_FUNCTION_CODE (callee);
12558 : :
12559 : 101096643 : switch (code)
12560 : : {
12561 : : /* All the following functions read memory pointed to by
12562 : : their second argument and write memory pointed to by first
12563 : : argument.
12564 : : strcat/strncat additionally reads memory pointed to by the first
12565 : : argument. */
12566 : 133215 : case BUILT_IN_STRCAT:
12567 : 133215 : case BUILT_IN_STRCAT_CHK:
12568 : 133215 : return "1cW 1 ";
12569 : 42603 : case BUILT_IN_STRNCAT:
12570 : 42603 : case BUILT_IN_STRNCAT_CHK:
12571 : 42603 : return "1cW 13";
12572 : 260090 : case BUILT_IN_STRCPY:
12573 : 260090 : case BUILT_IN_STRCPY_CHK:
12574 : 260090 : return "1cO 1 ";
12575 : 34670 : case BUILT_IN_STPCPY:
12576 : 34670 : case BUILT_IN_STPCPY_CHK:
12577 : 34670 : return ".cO 1 ";
12578 : 16338639 : case BUILT_IN_STRNCPY:
12579 : 16338639 : case BUILT_IN_MEMCPY:
12580 : 16338639 : case BUILT_IN_MEMMOVE:
12581 : 16338639 : case BUILT_IN_TM_MEMCPY:
12582 : 16338639 : case BUILT_IN_TM_MEMMOVE:
12583 : 16338639 : case BUILT_IN_STRNCPY_CHK:
12584 : 16338639 : case BUILT_IN_MEMCPY_CHK:
12585 : 16338639 : case BUILT_IN_MEMMOVE_CHK:
12586 : 16338639 : return "1cO313";
12587 : 150279 : case BUILT_IN_MEMPCPY:
12588 : 150279 : case BUILT_IN_MEMPCPY_CHK:
12589 : 150279 : return ".cO313";
12590 : 56495 : case BUILT_IN_STPNCPY:
12591 : 56495 : case BUILT_IN_STPNCPY_CHK:
12592 : 56495 : return ".cO313";
12593 : 0 : case BUILT_IN_BCOPY:
12594 : 0 : return ".c23O3";
12595 : 0 : case BUILT_IN_BZERO:
12596 : 0 : return ".cO2";
12597 : 8813039 : case BUILT_IN_MEMCMP:
12598 : 8813039 : case BUILT_IN_MEMCMP_EQ:
12599 : 8813039 : case BUILT_IN_BCMP:
12600 : 8813039 : case BUILT_IN_STRNCMP:
12601 : 8813039 : case BUILT_IN_STRNCMP_EQ:
12602 : 8813039 : case BUILT_IN_STRNCASECMP:
12603 : 8813039 : return ".cR3R3";
12604 : :
12605 : : /* The following functions read memory pointed to by their
12606 : : first argument. */
12607 : 614 : CASE_BUILT_IN_TM_LOAD (1):
12608 : 614 : CASE_BUILT_IN_TM_LOAD (2):
12609 : 614 : CASE_BUILT_IN_TM_LOAD (4):
12610 : 614 : CASE_BUILT_IN_TM_LOAD (8):
12611 : 614 : CASE_BUILT_IN_TM_LOAD (FLOAT):
12612 : 614 : CASE_BUILT_IN_TM_LOAD (DOUBLE):
12613 : 614 : CASE_BUILT_IN_TM_LOAD (LDOUBLE):
12614 : 614 : CASE_BUILT_IN_TM_LOAD (M64):
12615 : 614 : CASE_BUILT_IN_TM_LOAD (M128):
12616 : 614 : CASE_BUILT_IN_TM_LOAD (M256):
12617 : 614 : case BUILT_IN_TM_LOG:
12618 : 614 : case BUILT_IN_TM_LOG_1:
12619 : 614 : case BUILT_IN_TM_LOG_2:
12620 : 614 : case BUILT_IN_TM_LOG_4:
12621 : 614 : case BUILT_IN_TM_LOG_8:
12622 : 614 : case BUILT_IN_TM_LOG_FLOAT:
12623 : 614 : case BUILT_IN_TM_LOG_DOUBLE:
12624 : 614 : case BUILT_IN_TM_LOG_LDOUBLE:
12625 : 614 : case BUILT_IN_TM_LOG_M64:
12626 : 614 : case BUILT_IN_TM_LOG_M128:
12627 : 614 : case BUILT_IN_TM_LOG_M256:
12628 : 614 : return ".cR ";
12629 : :
12630 : 448757 : case BUILT_IN_INDEX:
12631 : 448757 : case BUILT_IN_RINDEX:
12632 : 448757 : case BUILT_IN_STRCHR:
12633 : 448757 : case BUILT_IN_STRLEN:
12634 : 448757 : case BUILT_IN_STRRCHR:
12635 : 448757 : return ".cR ";
12636 : 61870 : case BUILT_IN_STRNLEN:
12637 : 61870 : return ".cR2";
12638 : :
12639 : : /* These read memory pointed to by the first argument.
12640 : : Allocating memory does not have any side-effects apart from
12641 : : being the definition point for the pointer.
12642 : : Unix98 specifies that errno is set on allocation failure. */
12643 : 16095 : case BUILT_IN_STRDUP:
12644 : 16095 : return "mCR ";
12645 : 13284 : case BUILT_IN_STRNDUP:
12646 : 13284 : return "mCR2";
12647 : : /* Allocating memory does not have any side-effects apart from
12648 : : being the definition point for the pointer. */
12649 : 8901415 : case BUILT_IN_MALLOC:
12650 : 8901415 : case BUILT_IN_ALIGNED_ALLOC:
12651 : 8901415 : case BUILT_IN_CALLOC:
12652 : 8901415 : case BUILT_IN_GOMP_ALLOC:
12653 : 8901415 : return "mC";
12654 : 825888 : CASE_BUILT_IN_ALLOCA:
12655 : 825888 : return "mc";
12656 : : /* These read memory pointed to by the first argument with size
12657 : : in the third argument. */
12658 : 361733 : case BUILT_IN_MEMCHR:
12659 : 361733 : return ".cR3";
12660 : : /* These read memory pointed to by the first and second arguments. */
12661 : 10860146 : case BUILT_IN_STRSTR:
12662 : 10860146 : case BUILT_IN_STRPBRK:
12663 : 10860146 : case BUILT_IN_STRCASECMP:
12664 : 10860146 : case BUILT_IN_STRCSPN:
12665 : 10860146 : case BUILT_IN_STRSPN:
12666 : 10860146 : case BUILT_IN_STRCMP:
12667 : 10860146 : case BUILT_IN_STRCMP_EQ:
12668 : 10860146 : return ".cR R ";
12669 : : /* Freeing memory kills the pointed-to memory. More importantly
12670 : : the call has to serve as a barrier for moving loads and stores
12671 : : across it. */
12672 : 5260137 : case BUILT_IN_STACK_RESTORE:
12673 : 5260137 : case BUILT_IN_FREE:
12674 : 5260137 : case BUILT_IN_GOMP_FREE:
12675 : 5260137 : return ".co ";
12676 : 89612 : case BUILT_IN_VA_END:
12677 : 89612 : return ".cO ";
12678 : : /* Realloc serves both as allocation point and deallocation point. */
12679 : 944297 : case BUILT_IN_REALLOC:
12680 : 944297 : case BUILT_IN_GOMP_REALLOC:
12681 : 944297 : return ".Cw ";
12682 : 15516 : case BUILT_IN_GAMMA_R:
12683 : 15516 : case BUILT_IN_GAMMAF_R:
12684 : 15516 : case BUILT_IN_GAMMAL_R:
12685 : 15516 : case BUILT_IN_LGAMMA_R:
12686 : 15516 : case BUILT_IN_LGAMMAF_R:
12687 : 15516 : case BUILT_IN_LGAMMAL_R:
12688 : 15516 : return ".C. Ot";
12689 : 58028 : case BUILT_IN_FREXP:
12690 : 58028 : case BUILT_IN_FREXPF:
12691 : 58028 : case BUILT_IN_FREXPL:
12692 : 58028 : case BUILT_IN_MODF:
12693 : 58028 : case BUILT_IN_MODFF:
12694 : 58028 : case BUILT_IN_MODFL:
12695 : 58028 : return ".c. Ot";
12696 : 7262 : case BUILT_IN_REMQUO:
12697 : 7262 : case BUILT_IN_REMQUOF:
12698 : 7262 : case BUILT_IN_REMQUOL:
12699 : 7262 : return ".c. . Ot";
12700 : 157 : case BUILT_IN_SINCOS:
12701 : 157 : case BUILT_IN_SINCOSF:
12702 : 157 : case BUILT_IN_SINCOSL:
12703 : 157 : return ".c. OtOt";
12704 : 3930656 : case BUILT_IN_MEMSET:
12705 : 3930656 : case BUILT_IN_MEMSET_CHK:
12706 : 3930656 : case BUILT_IN_TM_MEMSET:
12707 : 3930656 : return "1cO3";
12708 : 365 : CASE_BUILT_IN_TM_STORE (1):
12709 : 365 : CASE_BUILT_IN_TM_STORE (2):
12710 : 365 : CASE_BUILT_IN_TM_STORE (4):
12711 : 365 : CASE_BUILT_IN_TM_STORE (8):
12712 : 365 : CASE_BUILT_IN_TM_STORE (FLOAT):
12713 : 365 : CASE_BUILT_IN_TM_STORE (DOUBLE):
12714 : 365 : CASE_BUILT_IN_TM_STORE (LDOUBLE):
12715 : 365 : CASE_BUILT_IN_TM_STORE (M64):
12716 : 365 : CASE_BUILT_IN_TM_STORE (M128):
12717 : 365 : CASE_BUILT_IN_TM_STORE (M256):
12718 : 365 : return ".cO ";
12719 : 2127365 : case BUILT_IN_STACK_SAVE:
12720 : 2127365 : case BUILT_IN_RETURN:
12721 : 2127365 : case BUILT_IN_EH_POINTER:
12722 : 2127365 : case BUILT_IN_EH_FILTER:
12723 : 2127365 : case BUILT_IN_UNWIND_RESUME:
12724 : 2127365 : case BUILT_IN_CXA_END_CLEANUP:
12725 : 2127365 : case BUILT_IN_EH_COPY_VALUES:
12726 : 2127365 : case BUILT_IN_FRAME_ADDRESS:
12727 : 2127365 : case BUILT_IN_APPLY_ARGS:
12728 : 2127365 : case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
12729 : 2127365 : case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
12730 : 2127365 : case BUILT_IN_PREFETCH:
12731 : 2127365 : case BUILT_IN_DWARF_CFA:
12732 : 2127365 : case BUILT_IN_RETURN_ADDRESS:
12733 : 2127365 : return ".c";
12734 : 1589664 : case BUILT_IN_ASSUME_ALIGNED:
12735 : 1589664 : case BUILT_IN_EXPECT:
12736 : 1589664 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
12737 : 1589664 : return "1cX ";
12738 : : /* But posix_memalign stores a pointer into the memory pointed to
12739 : : by its first argument. */
12740 : 5469 : case BUILT_IN_POSIX_MEMALIGN:
12741 : 5469 : return ".cOt";
12742 : 4312 : case BUILT_IN_OMP_GET_MAPPED_PTR:
12743 : 4312 : return ". R ";
12744 : :
12745 : 39744971 : default:
12746 : 39744971 : return "";
12747 : : }
12748 : : }
|