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 : 526709 : is_builtin_name (const char *name)
204 : : {
205 : 526709 : return (startswith (name, "__builtin_")
206 : 189778 : || startswith (name, "__sync_")
207 : 715625 : || 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 : 526709 : 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 : 526709 : const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
221 : 526709 : 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 : 106077194 : get_object_alignment_2 (tree exp, unsigned int *alignp,
241 : : unsigned HOST_WIDE_INT *bitposp, bool addr_p)
242 : : {
243 : 106077194 : poly_int64 bitsize, bitpos;
244 : 106077194 : tree offset;
245 : 106077194 : machine_mode mode;
246 : 106077194 : int unsignedp, reversep, volatilep;
247 : 106077194 : unsigned int align = BITS_PER_UNIT;
248 : 106077194 : bool known_alignment = false;
249 : :
250 : : /* Get the innermost object and the constant (bitpos) and possibly
251 : : variable (offset) offset of the access. */
252 : 106077194 : 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 : 106077194 : 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 : 105765503 : else if (TREE_CODE (exp) == LABEL_DECL)
267 : : ;
268 : 105728531 : else if (TREE_CODE (exp) == CONST_DECL)
269 : : {
270 : : /* The alignment of a CONST_DECL is determined by its initializer. */
271 : 58541 : exp = DECL_INITIAL (exp);
272 : 58541 : align = TYPE_ALIGN (TREE_TYPE (exp));
273 : 58541 : if (CONSTANT_CLASS_P (exp))
274 : 58534 : align = targetm.constant_alignment (exp, align);
275 : :
276 : : known_alignment = true;
277 : : }
278 : 105669990 : else if (DECL_P (exp))
279 : : {
280 : 63926755 : align = DECL_ALIGN (exp);
281 : : known_alignment = true;
282 : : }
283 : 41743235 : else if (TREE_CODE (exp) == INDIRECT_REF
284 : 41731663 : || TREE_CODE (exp) == MEM_REF
285 : 5307124 : || TREE_CODE (exp) == TARGET_MEM_REF)
286 : : {
287 : 40031585 : tree addr = TREE_OPERAND (exp, 0);
288 : 40031585 : unsigned ptr_align;
289 : 40031585 : unsigned HOST_WIDE_INT ptr_bitpos;
290 : 40031585 : unsigned HOST_WIDE_INT ptr_bitmask = ~0;
291 : :
292 : : /* If the address is explicitely aligned, handle that. */
293 : 40031585 : if (TREE_CODE (addr) == BIT_AND_EXPR
294 : 40031585 : && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
295 : : {
296 : 76 : ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
297 : 76 : ptr_bitmask *= BITS_PER_UNIT;
298 : 76 : align = least_bit_hwi (ptr_bitmask);
299 : 76 : addr = TREE_OPERAND (addr, 0);
300 : : }
301 : :
302 : 40031585 : known_alignment
303 : 40031585 : = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
304 : 40031585 : align = MAX (ptr_align, align);
305 : :
306 : : /* Re-apply explicit alignment to the bitpos. */
307 : 40031585 : 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 : 40031585 : if (TREE_CODE (exp) == TARGET_MEM_REF)
312 : : {
313 : 3595474 : if (TMR_INDEX (exp))
314 : : {
315 : 1647906 : unsigned HOST_WIDE_INT step = 1;
316 : 1647906 : if (TMR_STEP (exp))
317 : 1504178 : step = TREE_INT_CST_LOW (TMR_STEP (exp));
318 : 1647906 : align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
319 : : }
320 : 3595474 : if (TMR_INDEX2 (exp))
321 : 42150 : 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 : 40031585 : unsigned int talign;
331 : 40031585 : if (!addr_p && !known_alignment
332 : 37644096 : && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
333 : 77675675 : && talign > align)
334 : : align = talign;
335 : : else
336 : : {
337 : : /* Else adjust bitpos accordingly. */
338 : 10706034 : bitpos += ptr_bitpos;
339 : 10706034 : if (TREE_CODE (exp) == MEM_REF
340 : 10706034 : || TREE_CODE (exp) == TARGET_MEM_REF)
341 : 10697267 : bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
342 : : }
343 : 40031585 : }
344 : 1711650 : 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 : 1711156 : align = TYPE_ALIGN (TREE_TYPE (exp));
349 : 1711156 : if (CONSTANT_CLASS_P (exp))
350 : 1711156 : 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 : 106077194 : if (offset)
358 : : {
359 : 9785127 : unsigned int trailing_zeros = tree_ctz (offset);
360 : 9785127 : if (trailing_zeros < HOST_BITS_PER_INT)
361 : : {
362 : 9785071 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
363 : 9785071 : if (inner)
364 : 9785071 : 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 : 106077194 : unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
371 : 106077194 : if (alt_align != 0 && alt_align < align)
372 : : {
373 : : align = alt_align;
374 : : known_alignment = false;
375 : : }
376 : :
377 : 106077194 : *alignp = align;
378 : 106077194 : *bitposp = bitpos.coeffs[0] & (align - 1);
379 : 106077194 : 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 : 83452731 : 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 : 83452731 : if (TREE_CODE (exp) == WITH_SIZE_EXPR)
394 : 0 : exp = TREE_OPERAND (exp, 0);
395 : 83452731 : 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 : 33051979 : get_object_alignment (tree exp)
402 : : {
403 : 33051979 : unsigned HOST_WIDE_INT bitpos = 0;
404 : 33051979 : unsigned int align;
405 : :
406 : 33051979 : 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 : 33051979 : if (bitpos != 0)
412 : 1842589 : align = least_bit_hwi (bitpos);
413 : 33051979 : 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 : 80409147 : get_pointer_alignment_1 (tree exp, unsigned int *alignp,
425 : : unsigned HOST_WIDE_INT *bitposp)
426 : : {
427 : 80409147 : STRIP_NOPS (exp);
428 : :
429 : 80409147 : if (TREE_CODE (exp) == ADDR_EXPR)
430 : 22622930 : return get_object_alignment_2 (TREE_OPERAND (exp, 0),
431 : 22622930 : alignp, bitposp, true);
432 : 57786217 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
433 : : {
434 : 942378 : unsigned int align;
435 : 942378 : unsigned HOST_WIDE_INT bitpos;
436 : 942378 : bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
437 : : &align, &bitpos);
438 : 942378 : if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
439 : 2736 : bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
440 : : else
441 : : {
442 : 939642 : unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
443 : 939642 : if (trailing_zeros < HOST_BITS_PER_INT)
444 : : {
445 : 939636 : unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
446 : 939636 : if (inner)
447 : 939636 : align = MIN (align, inner);
448 : : }
449 : : }
450 : 942378 : *alignp = align;
451 : 942378 : *bitposp = bitpos & (align - 1);
452 : 942378 : return res;
453 : : }
454 : 56843839 : else if (TREE_CODE (exp) == SSA_NAME
455 : 56843839 : && POINTER_TYPE_P (TREE_TYPE (exp)))
456 : : {
457 : 54845393 : unsigned int ptr_align, ptr_misalign;
458 : 54845393 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
459 : :
460 : 54845393 : if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
461 : : {
462 : 5717348 : *bitposp = ptr_misalign * BITS_PER_UNIT;
463 : 5717348 : *alignp = ptr_align * BITS_PER_UNIT;
464 : : /* Make sure to return a sensible alignment when the multiplication
465 : : by BITS_PER_UNIT overflowed. */
466 : 5717348 : if (*alignp == 0)
467 : 30 : *alignp = 1u << (HOST_BITS_PER_INT - 1);
468 : : /* We cannot really tell whether this result is an approximation. */
469 : 5717348 : return false;
470 : : }
471 : : else
472 : : {
473 : 49128045 : *bitposp = 0;
474 : 49128045 : *alignp = BITS_PER_UNIT;
475 : 49128045 : return false;
476 : : }
477 : : }
478 : 1998446 : else if (TREE_CODE (exp) == INTEGER_CST)
479 : : {
480 : 16617 : *alignp = BIGGEST_ALIGNMENT;
481 : 16617 : *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
482 : 16617 : & (BIGGEST_ALIGNMENT - 1));
483 : 16617 : return true;
484 : : }
485 : :
486 : 1981829 : *bitposp = 0;
487 : 1981829 : *alignp = BITS_PER_UNIT;
488 : 1981829 : 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 : 11157029 : get_pointer_alignment (tree exp)
500 : : {
501 : 11157029 : unsigned HOST_WIDE_INT bitpos = 0;
502 : 11157029 : unsigned int align;
503 : :
504 : 11157029 : 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 : 11157029 : if (bitpos != 0)
510 : 103742 : align = least_bit_hwi (bitpos);
511 : :
512 : 11157029 : 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 : 791546 : string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
521 : : {
522 : 791546 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
523 : :
524 : 791546 : unsigned n;
525 : :
526 : 791546 : if (eltsize == 1)
527 : : {
528 : : /* Optimize the common case of plain char. */
529 : 245093010 : for (n = 0; n < maxelts; n++)
530 : : {
531 : 245001292 : const char *elt = (const char*) ptr + n;
532 : 245001292 : 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 : 791546 : 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 : 2676285 : 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 : 2676285 : c_strlen_data local_strlen_data = { };
584 : 2676285 : if (!data)
585 : 604055 : data = &local_strlen_data;
586 : :
587 : 2676285 : gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
588 : :
589 : 2676285 : tree src = STRIP_NOPS (arg);
590 : 2676285 : if (TREE_CODE (src) == COND_EXPR
591 : 2676285 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 : : {
593 : 687 : tree len1, len2;
594 : :
595 : 687 : len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
596 : 687 : len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
597 : 687 : if (tree_int_cst_equal (len1, len2))
598 : : return len1;
599 : : }
600 : :
601 : 2676119 : if (TREE_CODE (src) == COMPOUND_EXPR
602 : 2676119 : && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
603 : 45 : return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
604 : :
605 : 2676074 : location_t loc = EXPR_LOC_OR_LOC (src, input_location);
606 : :
607 : : /* Offset from the beginning of the string in bytes. */
608 : 2676074 : tree byteoff;
609 : 2676074 : tree memsize;
610 : 2676074 : tree decl;
611 : 2676074 : src = string_constant (src, &byteoff, &memsize, &decl);
612 : 2676074 : if (src == 0)
613 : : return NULL_TREE;
614 : :
615 : : /* Determine the size of the string element. */
616 : 806485 : 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 : 795266 : HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
626 : 795266 : strelts = strelts / eltsize;
627 : :
628 : 795266 : if (!tree_fits_uhwi_p (memsize))
629 : : return NULL_TREE;
630 : :
631 : 795266 : 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 : 795266 : const char *ptr = TREE_STRING_POINTER (src);
636 : :
637 : 795266 : if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
638 : : {
639 : : /* The code below works only for single byte character types. */
640 : 5363 : 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 : 5363 : 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 : 5363 : if (len + 1 < strelts)
653 : : return NULL_TREE;
654 : 4793 : 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 : 3644 : 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 : 3605 : tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
672 : : : byteoff;
673 : 3605 : offsave = fold_convert_loc (loc, sizetype, offsave);
674 : 3605 : tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
675 : 3605 : size_int (len));
676 : 3605 : tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
677 : : offsave);
678 : 3605 : lenexp = fold_convert_loc (loc, ssizetype, lenexp);
679 : 3605 : return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
680 : 3605 : build_zero_cst (ssizetype));
681 : : }
682 : :
683 : : /* Offset from the beginning of the string in elements. */
684 : 789903 : 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 : 789903 : if (byteoff == 0)
689 : : eltoff = 0;
690 : 789903 : else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
691 : : eltoff = -1;
692 : : else
693 : 788715 : 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 : 789903 : if (eltoff < 0 || eltoff >= maxelts)
698 : : {
699 : : /* Suppress multiple warnings for propagated constant strings. */
700 : 3720 : if (only_value != 2
701 : 3720 : && !warning_suppressed_p (arg, OPT_Warray_bounds_)
702 : 7440 : && warning_at (loc, OPT_Warray_bounds_,
703 : : "offset %qwi outside bounds of constant string",
704 : : eltoff))
705 : : {
706 : 679 : if (decl)
707 : 678 : inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
708 : 679 : suppress_warning (arg, OPT_Warray_bounds_);
709 : : }
710 : 3720 : return NULL_TREE;
711 : : }
712 : :
713 : : /* If eltoff is larger than strelts but less than maxelts the
714 : : string length is zero, since the excess memory will be zero. */
715 : 786183 : 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 : 786183 : unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
725 : 786183 : 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 : 786183 : if (len >= maxelts - eltoff)
731 : : {
732 : 85548 : data->decl = decl;
733 : 85548 : data->off = byteoff;
734 : 85548 : data->minlen = ssize_int (len);
735 : 85548 : return NULL_TREE;
736 : : }
737 : :
738 : 700635 : 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 : 292044 : c_readstr (const char *str, fixed_size_mode mode,
749 : : bool null_terminated_p/*=true*/)
750 : : {
751 : 292044 : auto_vec<target_unit, MAX_BITSIZE_MODE_ANY_INT / BITS_PER_UNIT> bytes;
752 : :
753 : 584088 : bytes.reserve (GET_MODE_SIZE (mode));
754 : :
755 : 292044 : target_unit ch = 1;
756 : 5107330 : for (unsigned int i = 0; i < GET_MODE_SIZE (mode); ++i)
757 : : {
758 : 2261621 : if (ch || !null_terminated_p)
759 : 2177290 : ch = (unsigned char) str[i];
760 : 2261621 : bytes.quick_push (ch);
761 : : }
762 : :
763 : 292044 : return native_decode_rtx (mode, bytes, 0);
764 : 292044 : }
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 : 34138 : target_char_cast (tree cst, char *p)
772 : : {
773 : 34138 : unsigned HOST_WIDE_INT val, hostval;
774 : :
775 : 34138 : 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 : 34138 : val = TREE_INT_CST_LOW (cst);
781 : :
782 : 34138 : if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
783 : 34138 : val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
784 : :
785 : 34138 : hostval = val;
786 : 34138 : if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
787 : 34138 : hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
788 : :
789 : 34138 : if (val != hostval)
790 : : return 1;
791 : :
792 : 34138 : *p = hostval;
793 : 34138 : 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 : 1116791 : builtin_save_expr (tree exp)
802 : : {
803 : 1116791 : if (TREE_CODE (exp) == SSA_NAME
804 : 952486 : || (TREE_ADDRESSABLE (exp) == 0
805 : 952294 : && (TREE_CODE (exp) == PARM_DECL
806 : 371165 : || (VAR_P (exp) && !TREE_STATIC (exp)))))
807 : : return exp;
808 : :
809 : 369375 : 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 : 15583 : expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
818 : : {
819 : 15583 : int i;
820 : 15583 : rtx tem = INITIAL_FRAME_ADDRESS_RTX;
821 : 15583 : 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 : 15583 : if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
833 : 4246 : tem = frame_pointer_rtx;
834 : : else
835 : : {
836 : 11337 : tem = hard_frame_pointer_rtx;
837 : :
838 : : /* Tell reload not to eliminate the frame pointer. */
839 : 11337 : crtl->accesses_prior_frames = 1;
840 : : }
841 : : }
842 : :
843 : 15583 : 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 : 51459 : 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 : 15583 : 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 : 7297 : 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 : 5086 : return tem;
878 : : }
879 : :
880 : : /* Alias set used for setjmp buffer. */
881 : : static alias_set_type setjmp_alias_set = -1;
882 : :
883 : : /* Construct the leading half of a __builtin_setjmp call. Control will
884 : : return to RECEIVER_LABEL. This is also called directly by the SJLJ
885 : : exception handling code. */
886 : :
887 : : void
888 : 841 : expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
889 : : {
890 : 841 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
891 : 841 : rtx stack_save;
892 : 841 : rtx mem;
893 : :
894 : 841 : if (setjmp_alias_set == -1)
895 : 241 : setjmp_alias_set = new_alias_set ();
896 : :
897 : 841 : buf_addr = convert_memory_address (Pmode, buf_addr);
898 : :
899 : 842 : buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
900 : :
901 : : /* We store the frame pointer and the address of receiver_label in
902 : : the buffer and use the rest of it for the stack save area, which
903 : : is machine-dependent. */
904 : :
905 : 842 : mem = gen_rtx_MEM (Pmode, buf_addr);
906 : 841 : set_mem_alias_set (mem, setjmp_alias_set);
907 : 841 : emit_move_insn (mem, hard_frame_pointer_rtx);
908 : :
909 : 842 : mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
910 : 1682 : GET_MODE_SIZE (Pmode))),
911 : 841 : set_mem_alias_set (mem, setjmp_alias_set);
912 : :
913 : 1682 : emit_move_insn (validize_mem (mem),
914 : 1682 : force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
915 : :
916 : 841 : stack_save = gen_rtx_MEM (sa_mode,
917 : : plus_constant (Pmode, buf_addr,
918 : 1682 : 2 * GET_MODE_SIZE (Pmode)));
919 : 841 : set_mem_alias_set (stack_save, setjmp_alias_set);
920 : 841 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
921 : :
922 : : /* If there is further processing to do, do it. */
923 : 841 : if (targetm.have_builtin_setjmp_setup ())
924 : 0 : emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
925 : :
926 : : /* We have a nonlocal label. */
927 : 841 : cfun->has_nonlocal_label = 1;
928 : 841 : }
929 : :
930 : : /* Construct the trailing part of a __builtin_setjmp call. This is
931 : : also called directly by the SJLJ exception handling code.
932 : : If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
933 : :
934 : : void
935 : 1343 : expand_builtin_setjmp_receiver (rtx receiver_label)
936 : : {
937 : 1343 : rtx chain;
938 : :
939 : : /* Mark the FP as used when we get here, so we have to make sure it's
940 : : marked as used by this function. */
941 : 1343 : emit_use (hard_frame_pointer_rtx);
942 : :
943 : : /* Mark the static chain as clobbered here so life information
944 : : doesn't get messed up for it. */
945 : 1343 : chain = rtx_for_static_chain (current_function_decl, true);
946 : 1343 : if (chain && REG_P (chain))
947 : 1 : emit_clobber (chain);
948 : :
949 : 1343 : if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
950 : : {
951 : : /* If the argument pointer can be eliminated in favor of the
952 : : frame pointer, we don't need to restore it. We assume here
953 : : that if such an elimination is present, it can always be used.
954 : : This is the case on all known machines; if we don't make this
955 : : assumption, we do unnecessary saving on many machines. */
956 : : size_t i;
957 : : static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
958 : :
959 : 2686 : for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
960 : 2686 : if (elim_regs[i].from == ARG_POINTER_REGNUM
961 : 2686 : && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
962 : : break;
963 : :
964 : 1343 : if (i == ARRAY_SIZE (elim_regs))
965 : : {
966 : : /* Now restore our arg pointer from the address at which it
967 : : was saved in our stack frame. */
968 : 0 : emit_move_insn (crtl->args.internal_arg_pointer,
969 : : copy_to_reg (get_arg_pointer_save_area ()));
970 : : }
971 : : }
972 : :
973 : 1343 : if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
974 : 0 : emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
975 : 1343 : else if (targetm.have_nonlocal_goto_receiver ())
976 : 0 : emit_insn (targetm.gen_nonlocal_goto_receiver ());
977 : : else
978 : : { /* Nothing */ }
979 : :
980 : : /* We must not allow the code we just generated to be reordered by
981 : : scheduling. Specifically, the update of the frame pointer must
982 : : happen immediately, not later. */
983 : 1343 : emit_insn (gen_blockage ());
984 : 1343 : }
985 : :
986 : : /* __builtin_longjmp is passed a pointer to an array of five words (not
987 : : all will be used on all machines). It operates similarly to the C
988 : : library function of the same name, but is more efficient. Much of
989 : : the code below is copied from the handling of non-local gotos. */
990 : :
991 : : static void
992 : 391 : expand_builtin_longjmp (rtx buf_addr, rtx value)
993 : : {
994 : 391 : rtx fp, lab, stack;
995 : 391 : rtx_insn *insn, *last;
996 : 391 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
997 : :
998 : : /* DRAP is needed for stack realign if longjmp is expanded to current
999 : : function */
1000 : 391 : if (SUPPORTS_STACK_ALIGNMENT)
1001 : 391 : crtl->need_drap = true;
1002 : :
1003 : 391 : if (setjmp_alias_set == -1)
1004 : 330 : setjmp_alias_set = new_alias_set ();
1005 : :
1006 : 391 : buf_addr = convert_memory_address (Pmode, buf_addr);
1007 : :
1008 : 392 : buf_addr = force_reg (Pmode, buf_addr);
1009 : :
1010 : : /* We require that the user must pass a second argument of 1, because
1011 : : that is what builtin_setjmp will return. */
1012 : 391 : gcc_assert (value == const1_rtx);
1013 : :
1014 : 391 : last = get_last_insn ();
1015 : 391 : if (targetm.have_builtin_longjmp ())
1016 : 0 : emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1017 : : else
1018 : : {
1019 : 392 : fp = gen_rtx_MEM (Pmode, buf_addr);
1020 : 392 : lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1021 : 782 : GET_MODE_SIZE (Pmode)));
1022 : :
1023 : 391 : stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1024 : 782 : 2 * GET_MODE_SIZE (Pmode)));
1025 : 391 : set_mem_alias_set (fp, setjmp_alias_set);
1026 : 391 : set_mem_alias_set (lab, setjmp_alias_set);
1027 : 391 : set_mem_alias_set (stack, setjmp_alias_set);
1028 : :
1029 : : /* Pick up FP, label, and SP from the block and jump. This code is
1030 : : from expand_goto in stmt.cc; see there for detailed comments. */
1031 : 391 : if (targetm.have_nonlocal_goto ())
1032 : : /* We have to pass a value to the nonlocal_goto pattern that will
1033 : : get copied into the static_chain pointer, but it does not matter
1034 : : what that value is, because builtin_setjmp does not use it. */
1035 : 0 : emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1036 : : else
1037 : : {
1038 : 391 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1039 : 391 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1040 : :
1041 : 391 : lab = copy_to_reg (lab);
1042 : :
1043 : : /* Restore the frame pointer and stack pointer. We must use a
1044 : : temporary since the setjmp buffer may be a local. */
1045 : 391 : fp = copy_to_reg (fp);
1046 : 391 : emit_stack_restore (SAVE_NONLOCAL, stack);
1047 : :
1048 : : /* Ensure the frame pointer move is not optimized. */
1049 : 391 : emit_insn (gen_blockage ());
1050 : 391 : emit_clobber (hard_frame_pointer_rtx);
1051 : 391 : emit_clobber (frame_pointer_rtx);
1052 : 391 : emit_move_insn (hard_frame_pointer_rtx, fp);
1053 : :
1054 : 391 : emit_use (hard_frame_pointer_rtx);
1055 : 391 : emit_use (stack_pointer_rtx);
1056 : 391 : emit_indirect_jump (lab);
1057 : : }
1058 : : }
1059 : :
1060 : : /* Search backwards and mark the jump insn as a non-local goto.
1061 : : Note that this precludes the use of __builtin_longjmp to a
1062 : : __builtin_setjmp target in the same function. However, we've
1063 : : already cautioned the user that these functions are for
1064 : : internal exception handling use only. */
1065 : 782 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1066 : : {
1067 : 782 : gcc_assert (insn != last);
1068 : :
1069 : 782 : if (JUMP_P (insn))
1070 : : {
1071 : 391 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1072 : 391 : break;
1073 : : }
1074 : 391 : else if (CALL_P (insn))
1075 : : break;
1076 : : }
1077 : 391 : }
1078 : :
1079 : : static inline bool
1080 : 2182811 : more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1081 : : {
1082 : 2182811 : 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 : 453130 : validate_arglist (const_tree callexpr, ...)
1092 : : {
1093 : 453130 : enum tree_code code;
1094 : 453130 : bool res = 0;
1095 : 453130 : va_list ap;
1096 : 453130 : const_call_expr_arg_iterator iter;
1097 : 453130 : const_tree arg;
1098 : :
1099 : 453130 : va_start (ap, callexpr);
1100 : 453130 : init_const_call_expr_arg_iterator (callexpr, &iter);
1101 : :
1102 : : /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1103 : 453130 : tree fn = CALL_EXPR_FN (callexpr);
1104 : 453130 : bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1105 : :
1106 : 453130 : for (unsigned argno = 1; ; ++argno)
1107 : : {
1108 : 1560184 : code = (enum tree_code) va_arg (ap, int);
1109 : :
1110 : 1560184 : switch (code)
1111 : : {
1112 : 2037 : case 0:
1113 : : /* This signifies an ellipses, any further arguments are all ok. */
1114 : 2037 : res = true;
1115 : 2037 : goto end;
1116 : 450860 : case VOID_TYPE:
1117 : : /* This signifies an endlink, if no arguments remain, return
1118 : : true, otherwise return false. */
1119 : 450860 : res = !more_const_call_expr_args_p (&iter);
1120 : 450860 : goto end;
1121 : 756011 : 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 : 756011 : if (argmap
1126 : 756011 : && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1127 : : {
1128 : 284157 : arg = next_const_call_expr_arg (&iter);
1129 : 284157 : if (!validate_arg (arg, code) || integer_zerop (arg))
1130 : 59 : goto end;
1131 : : break;
1132 : : }
1133 : : /* FALLTHRU */
1134 : 823130 : 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 : 823130 : arg = next_const_call_expr_arg (&iter);
1139 : 823130 : 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 : 453130 : end: ;
1148 : 453130 : va_end (ap);
1149 : :
1150 : 453130 : BITMAP_FREE (argmap);
1151 : :
1152 : 453130 : if (res)
1153 : 452897 : for (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (TREE_TYPE (fn)));
1154 : 678699 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
1155 : 225802 : attrs = TREE_CHAIN (attrs))
1156 : : {
1157 : 225824 : tree args = TREE_VALUE (attrs);
1158 : 225824 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
1159 : 225824 : unsigned int idx2
1160 : 225824 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
1161 : 225824 : unsigned int idx3 = idx2;
1162 : 225824 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
1163 : 0 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
1164 : 225824 : if (idx < (unsigned) call_expr_nargs (callexpr)
1165 : 225824 : && idx2 < (unsigned) call_expr_nargs (callexpr)
1166 : 225824 : && idx3 < (unsigned) call_expr_nargs (callexpr)
1167 : 225824 : && POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx)))
1168 : 225824 : && integer_zerop (CALL_EXPR_ARG (callexpr, idx))
1169 : 82 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx2)))
1170 : 82 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx2))
1171 : 22 : && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx3)))
1172 : 225846 : && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx3)))
1173 : : return false;
1174 : : }
1175 : :
1176 : : return res;
1177 : : }
1178 : :
1179 : : /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1180 : : and the address of the save area. */
1181 : :
1182 : : static rtx
1183 : 510 : expand_builtin_nonlocal_goto (tree exp)
1184 : : {
1185 : 510 : tree t_label, t_save_area;
1186 : 510 : rtx r_label, r_save_area, r_fp, r_sp;
1187 : 510 : rtx_insn *insn;
1188 : :
1189 : 510 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1190 : : return NULL_RTX;
1191 : :
1192 : 510 : t_label = CALL_EXPR_ARG (exp, 0);
1193 : 510 : t_save_area = CALL_EXPR_ARG (exp, 1);
1194 : :
1195 : 510 : r_label = expand_normal (t_label);
1196 : 510 : r_label = convert_memory_address (Pmode, r_label);
1197 : 510 : r_save_area = expand_normal (t_save_area);
1198 : 510 : r_save_area = convert_memory_address (Pmode, r_save_area);
1199 : : /* Copy the address of the save location to a register just in case it was
1200 : : based on the frame pointer. */
1201 : 510 : r_save_area = copy_to_reg (r_save_area);
1202 : 510 : r_fp = gen_rtx_MEM (Pmode, r_save_area);
1203 : 510 : r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1204 : : plus_constant (Pmode, r_save_area,
1205 : 1020 : GET_MODE_SIZE (Pmode)));
1206 : :
1207 : 510 : crtl->has_nonlocal_goto = 1;
1208 : :
1209 : : /* ??? We no longer need to pass the static chain value, afaik. */
1210 : 510 : if (targetm.have_nonlocal_goto ())
1211 : 0 : emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1212 : : else
1213 : : {
1214 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1215 : 510 : emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1216 : :
1217 : 510 : r_label = copy_to_reg (r_label);
1218 : :
1219 : : /* Restore the frame pointer and stack pointer. We must use a
1220 : : temporary since the setjmp buffer may be a local. */
1221 : 510 : r_fp = copy_to_reg (r_fp);
1222 : 510 : emit_stack_restore (SAVE_NONLOCAL, r_sp);
1223 : :
1224 : : /* Ensure the frame pointer move is not optimized. */
1225 : 510 : emit_insn (gen_blockage ());
1226 : 510 : emit_clobber (hard_frame_pointer_rtx);
1227 : 510 : emit_clobber (frame_pointer_rtx);
1228 : 510 : emit_move_insn (hard_frame_pointer_rtx, r_fp);
1229 : :
1230 : : /* USE of hard_frame_pointer_rtx added for consistency;
1231 : : not clear if really needed. */
1232 : 510 : emit_use (hard_frame_pointer_rtx);
1233 : 510 : emit_use (stack_pointer_rtx);
1234 : :
1235 : : /* If the architecture is using a GP register, we must
1236 : : conservatively assume that the target function makes use of it.
1237 : : The prologue of functions with nonlocal gotos must therefore
1238 : : initialize the GP register to the appropriate value, and we
1239 : : must then make sure that this value is live at the point
1240 : : of the jump. (Note that this doesn't necessarily apply
1241 : : to targets with a nonlocal_goto pattern; they are free
1242 : : to implement it in their own way. Note also that this is
1243 : : a no-op if the GP register is a global invariant.) */
1244 : 510 : unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1245 : 0 : if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1246 : 0 : emit_use (pic_offset_table_rtx);
1247 : :
1248 : 510 : emit_indirect_jump (r_label);
1249 : : }
1250 : :
1251 : : /* Search backwards to the jump insn and mark it as a
1252 : : non-local goto. */
1253 : 1020 : for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1254 : : {
1255 : 1020 : if (JUMP_P (insn))
1256 : : {
1257 : 510 : add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1258 : 510 : break;
1259 : : }
1260 : 510 : else if (CALL_P (insn))
1261 : : break;
1262 : : }
1263 : :
1264 : 510 : return const0_rtx;
1265 : : }
1266 : :
1267 : : /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1268 : : (not all will be used on all machines) that was passed to __builtin_setjmp.
1269 : : It updates the stack pointer in that block to the current value. This is
1270 : : also called directly by the SJLJ exception handling code. */
1271 : :
1272 : : void
1273 : 0 : expand_builtin_update_setjmp_buf (rtx buf_addr)
1274 : : {
1275 : 0 : machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1276 : 0 : buf_addr = convert_memory_address (Pmode, buf_addr);
1277 : 0 : rtx stack_save
1278 : 0 : = gen_rtx_MEM (sa_mode,
1279 : 0 : memory_address
1280 : : (sa_mode,
1281 : : plus_constant (Pmode, buf_addr,
1282 : 0 : 2 * GET_MODE_SIZE (Pmode))));
1283 : :
1284 : 0 : emit_stack_save (SAVE_NONLOCAL, &stack_save);
1285 : 0 : }
1286 : :
1287 : : /* Expand a call to __builtin_prefetch. For a target that does not support
1288 : : data prefetch, evaluate the memory address argument in case it has side
1289 : : effects. */
1290 : :
1291 : : static void
1292 : 2037 : expand_builtin_prefetch (tree exp)
1293 : : {
1294 : 2037 : tree arg0, arg1, arg2;
1295 : 2037 : int nargs;
1296 : 2037 : rtx op0, op1, op2;
1297 : :
1298 : 2037 : if (!validate_arglist (exp, POINTER_TYPE, 0))
1299 : : return;
1300 : :
1301 : 2037 : arg0 = CALL_EXPR_ARG (exp, 0);
1302 : :
1303 : : /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1304 : : zero (read) and argument 2 (locality) defaults to 3 (high degree of
1305 : : locality). */
1306 : 2037 : nargs = call_expr_nargs (exp);
1307 : 2037 : arg1 = nargs > 1 ? CALL_EXPR_ARG (exp, 1) : NULL_TREE;
1308 : 2001 : arg2 = nargs > 2 ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
1309 : :
1310 : : /* Argument 0 is an address. */
1311 : 2133 : op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1312 : :
1313 : : /* Argument 1 (read/write flag) must be a compile-time constant int. */
1314 : 2037 : if (arg1 == NULL_TREE)
1315 : 36 : op1 = const0_rtx;
1316 : 2001 : else if (TREE_CODE (arg1) != INTEGER_CST)
1317 : : {
1318 : 0 : error ("second argument to %<__builtin_prefetch%> must be a constant");
1319 : 0 : op1 = const0_rtx;
1320 : : }
1321 : : else
1322 : 2001 : op1 = expand_normal (arg1);
1323 : : /* Argument 1 must be 0, 1 or 2. */
1324 : 2037 : if (!IN_RANGE (INTVAL (op1), 0, 2))
1325 : : {
1326 : 3 : warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1327 : : " using zero");
1328 : 3 : op1 = const0_rtx;
1329 : : }
1330 : :
1331 : : /* Argument 2 (locality) must be a compile-time constant int. */
1332 : 2037 : if (arg2 == NULL_TREE)
1333 : 61 : op2 = GEN_INT (3);
1334 : 1976 : else if (TREE_CODE (arg2) != INTEGER_CST)
1335 : : {
1336 : 0 : error ("third argument to %<__builtin_prefetch%> must be a constant");
1337 : 0 : op2 = const0_rtx;
1338 : : }
1339 : : else
1340 : 1976 : op2 = expand_normal (arg2);
1341 : : /* Argument 2 must be 0, 1, 2, or 3. */
1342 : 2037 : if (!IN_RANGE (INTVAL (op2), 0, 3))
1343 : : {
1344 : 3 : warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1345 : 3 : op2 = const0_rtx;
1346 : : }
1347 : :
1348 : 2037 : if (targetm.have_prefetch ())
1349 : : {
1350 : 2037 : class expand_operand ops[3];
1351 : :
1352 : 2037 : create_address_operand (&ops[0], op0);
1353 : 2037 : create_integer_operand (&ops[1], INTVAL (op1));
1354 : 2037 : create_integer_operand (&ops[2], INTVAL (op2));
1355 : 2037 : if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1356 : 2037 : return;
1357 : : }
1358 : :
1359 : : /* Don't do anything with direct references to volatile memory, but
1360 : : generate code to handle other side effects. */
1361 : 0 : if (!MEM_P (op0) && side_effects_p (op0))
1362 : 0 : emit_insn (op0);
1363 : : }
1364 : :
1365 : : /* Get a MEM rtx for expression EXP which is the address of an operand
1366 : : to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1367 : : the maximum length of the block of memory that might be accessed or
1368 : : NULL if unknown. */
1369 : :
1370 : : rtx
1371 : 688457 : get_memory_rtx (tree exp, tree len)
1372 : : {
1373 : 688457 : tree orig_exp = exp, base;
1374 : 688457 : rtx addr, mem;
1375 : :
1376 : 688457 : gcc_checking_assert
1377 : : (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))));
1378 : :
1379 : : /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1380 : : from its expression, for expr->a.b only <variable>.a.b is recorded. */
1381 : 688457 : if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1382 : 1339 : exp = TREE_OPERAND (exp, 0);
1383 : :
1384 : 688457 : addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1385 : 688457 : mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1386 : :
1387 : : /* Get an expression we can use to find the attributes to assign to MEM.
1388 : : First remove any nops. */
1389 : 1376914 : while (CONVERT_EXPR_P (exp)
1390 : 688457 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1391 : 0 : exp = TREE_OPERAND (exp, 0);
1392 : :
1393 : : /* Build a MEM_REF representing the whole accessed area as a byte blob,
1394 : : (as builtin stringops may alias with anything). */
1395 : 688457 : exp = fold_build2 (MEM_REF,
1396 : : build_array_type (char_type_node,
1397 : : build_range_type (sizetype,
1398 : : size_one_node, len)),
1399 : : exp, build_int_cst (ptr_type_node, 0));
1400 : :
1401 : : /* If the MEM_REF has no acceptable address, try to get the base object
1402 : : from the original address we got, and build an all-aliasing
1403 : : unknown-sized access to that one. */
1404 : 688457 : if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1405 : 688445 : set_mem_attributes (mem, exp, 0);
1406 : 12 : else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1407 : 12 : && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1408 : : 0))))
1409 : : {
1410 : 12 : unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1411 : 12 : exp = build_fold_addr_expr (base);
1412 : 12 : exp = fold_build2 (MEM_REF,
1413 : : build_array_type (char_type_node,
1414 : : build_range_type (sizetype,
1415 : : size_zero_node,
1416 : : NULL)),
1417 : : exp, build_int_cst (ptr_type_node, 0));
1418 : 12 : set_mem_attributes (mem, exp, 0);
1419 : : /* Since we stripped parts make sure the offset is unknown and the
1420 : : alignment is computed from the original address. */
1421 : 12 : clear_mem_offset (mem);
1422 : 12 : set_mem_align (mem, align);
1423 : : }
1424 : 688457 : set_mem_alias_set (mem, 0);
1425 : 688457 : return mem;
1426 : : }
1427 : :
1428 : : /* Built-in functions to perform an untyped call and return. */
1429 : :
1430 : : /* Wrapper that implicitly applies a delta when getting or setting the
1431 : : enclosed value. */
1432 : : template <typename T>
1433 : : class delta_type
1434 : : {
1435 : : T &value; T const delta;
1436 : : public:
1437 : 2524 : delta_type (T &val, T dlt) : value (val), delta (dlt) {}
1438 : 1809 : operator T () const { return value + delta; }
1439 : 715 : T operator = (T val) const { value = val - delta; return val; }
1440 : : };
1441 : :
1442 : : #define saved_apply_args_size \
1443 : : (delta_type<int> (this_target_builtins->x_apply_args_size_plus_one, -1))
1444 : : #define apply_args_mode \
1445 : : (this_target_builtins->x_apply_args_mode)
1446 : : #define saved_apply_result_size \
1447 : : (delta_type<int> (this_target_builtins->x_apply_result_size_plus_one, -1))
1448 : : #define apply_result_mode \
1449 : : (this_target_builtins->x_apply_result_mode)
1450 : :
1451 : : /* Return the size required for the block returned by __builtin_apply_args,
1452 : : and initialize apply_args_mode. */
1453 : :
1454 : : static int
1455 : 947 : apply_args_size (void)
1456 : : {
1457 : 947 : int size = saved_apply_args_size;
1458 : 947 : int align;
1459 : 947 : unsigned int regno;
1460 : :
1461 : : /* The values computed by this function never change. */
1462 : 947 : if (size < 0)
1463 : : {
1464 : : /* The first value is the incoming arg-pointer. */
1465 : 360 : size = GET_MODE_SIZE (Pmode);
1466 : :
1467 : : /* The second value is the structure value address unless this is
1468 : : passed as an "invisible" first argument. */
1469 : 360 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1470 : 0 : size += GET_MODE_SIZE (Pmode);
1471 : :
1472 : 33480 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 : 33120 : if (FUNCTION_ARG_REGNO_P (regno))
1474 : : {
1475 : 5400 : fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1476 : :
1477 : 5400 : if (mode != VOIDmode)
1478 : : {
1479 : 5400 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1480 : 5400 : if (size % align != 0)
1481 : 1 : size = CEIL (size, align) * align;
1482 : 5400 : size += GET_MODE_SIZE (mode);
1483 : 5400 : apply_args_mode[regno] = mode;
1484 : : }
1485 : : else
1486 : 0 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1487 : : }
1488 : : else
1489 : 27720 : apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1490 : :
1491 : 360 : saved_apply_args_size = size;
1492 : : }
1493 : 947 : return size;
1494 : : }
1495 : :
1496 : : /* Return the size required for the block returned by __builtin_apply,
1497 : : and initialize apply_result_mode. */
1498 : :
1499 : : static int
1500 : 862 : apply_result_size (void)
1501 : : {
1502 : 862 : int size = saved_apply_result_size;
1503 : 862 : int align, regno;
1504 : :
1505 : : /* The values computed by this function never change. */
1506 : 862 : if (size < 0)
1507 : : {
1508 : : size = 0;
1509 : :
1510 : 33015 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1511 : 32660 : if (targetm.calls.function_value_regno_p (regno))
1512 : : {
1513 : 2840 : fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1514 : :
1515 : 2840 : if (mode != VOIDmode)
1516 : : {
1517 : 2840 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1518 : 2840 : if (size % align != 0)
1519 : : size = CEIL (size, align) * align;
1520 : 2840 : size += GET_MODE_SIZE (mode);
1521 : 2840 : apply_result_mode[regno] = mode;
1522 : : }
1523 : : else
1524 : 0 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1525 : : }
1526 : : else
1527 : 29820 : apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1528 : :
1529 : : /* Allow targets that use untyped_call and untyped_return to override
1530 : : the size so that machine-specific information can be stored here. */
1531 : : #ifdef APPLY_RESULT_SIZE
1532 : 355 : size = APPLY_RESULT_SIZE;
1533 : : #endif
1534 : :
1535 : 355 : saved_apply_result_size = size;
1536 : : }
1537 : 862 : return size;
1538 : : }
1539 : :
1540 : : /* Create a vector describing the result block RESULT. If SAVEP is true,
1541 : : the result block is used to save the values; otherwise it is used to
1542 : : restore the values. */
1543 : :
1544 : : static rtx
1545 : 483 : result_vector (int savep, rtx result)
1546 : : {
1547 : 483 : int regno, size, align, nelts;
1548 : 483 : fixed_size_mode mode;
1549 : 483 : rtx reg, mem;
1550 : 483 : rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1551 : :
1552 : 483 : size = nelts = 0;
1553 : 44919 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1554 : 44436 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1555 : : {
1556 : 3864 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1557 : 3864 : if (size % align != 0)
1558 : 0 : size = CEIL (size, align) * align;
1559 : 3864 : reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1560 : 3864 : mem = adjust_address (result, mode, size);
1561 : 7728 : savevec[nelts++] = (savep
1562 : 7728 : ? gen_rtx_SET (mem, reg)
1563 : 0 : : gen_rtx_SET (reg, mem));
1564 : 7728 : size += GET_MODE_SIZE (mode);
1565 : : }
1566 : 483 : return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1567 : : }
1568 : :
1569 : : /* Save the state required to perform an untyped call with the same
1570 : : arguments as were passed to the current function. */
1571 : :
1572 : : static rtx
1573 : 464 : expand_builtin_apply_args_1 (void)
1574 : : {
1575 : 464 : rtx registers, tem;
1576 : 464 : int size, align, regno;
1577 : 464 : fixed_size_mode mode;
1578 : 464 : rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1579 : :
1580 : : /* Create a block where the arg-pointer, structure value address,
1581 : : and argument registers can be saved. */
1582 : 464 : registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1583 : :
1584 : : /* Walk past the arg-pointer and structure value address. */
1585 : 464 : size = GET_MODE_SIZE (Pmode);
1586 : 464 : if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1587 : 0 : size += GET_MODE_SIZE (Pmode);
1588 : :
1589 : : /* Save each register used in calling a function to the block. */
1590 : 43152 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1591 : 42688 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1592 : : {
1593 : 6960 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1594 : 6960 : if (size % align != 0)
1595 : 1 : size = CEIL (size, align) * align;
1596 : :
1597 : 6960 : tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1598 : :
1599 : 6960 : emit_move_insn (adjust_address (registers, mode, size), tem);
1600 : 13920 : size += GET_MODE_SIZE (mode);
1601 : : }
1602 : :
1603 : : /* Save the arg pointer to the block. */
1604 : 464 : tem = copy_to_reg (crtl->args.internal_arg_pointer);
1605 : : /* We need the pointer as the caller actually passed them to us, not
1606 : : as we might have pretended they were passed. Make sure it's a valid
1607 : : operand, as emit_move_insn isn't expected to handle a PLUS. */
1608 : 464 : if (STACK_GROWS_DOWNWARD)
1609 : 464 : tem
1610 : 464 : = force_operand (plus_constant (Pmode, tem,
1611 : 464 : crtl->args.pretend_args_size),
1612 : : NULL_RTX);
1613 : 464 : emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1614 : :
1615 : 464 : size = GET_MODE_SIZE (Pmode);
1616 : :
1617 : : /* Save the structure value address unless this is passed as an
1618 : : "invisible" first argument. */
1619 : 464 : if (struct_incoming_value)
1620 : 0 : emit_move_insn (adjust_address (registers, Pmode, size),
1621 : : copy_to_reg (struct_incoming_value));
1622 : :
1623 : : /* Return the address of the block. */
1624 : 464 : return copy_addr_to_reg (XEXP (registers, 0));
1625 : : }
1626 : :
1627 : : /* __builtin_apply_args returns block of memory allocated on
1628 : : the stack into which is stored the arg pointer, structure
1629 : : value address, static chain, and all the registers that might
1630 : : possibly be used in performing a function call. The code is
1631 : : moved to the start of the function so the incoming values are
1632 : : saved. */
1633 : :
1634 : : static rtx
1635 : 464 : expand_builtin_apply_args (void)
1636 : : {
1637 : : /* Don't do __builtin_apply_args more than once in a function.
1638 : : Save the result of the first call and reuse it. */
1639 : 464 : if (apply_args_value != 0)
1640 : : return apply_args_value;
1641 : 464 : {
1642 : : /* When this function is called, it means that registers must be
1643 : : saved on entry to this function. So we migrate the
1644 : : call to the first insn of this function. */
1645 : 464 : rtx temp;
1646 : :
1647 : 464 : start_sequence ();
1648 : 464 : temp = expand_builtin_apply_args_1 ();
1649 : 464 : rtx_insn *seq = end_sequence ();
1650 : :
1651 : 464 : apply_args_value = temp;
1652 : :
1653 : : /* Put the insns after the NOTE that starts the function.
1654 : : If this is inside a start_sequence, make the outer-level insn
1655 : : chain current, so the code is placed at the start of the
1656 : : function. If internal_arg_pointer is a non-virtual pseudo,
1657 : : it needs to be placed after the function that initializes
1658 : : that pseudo. */
1659 : 464 : push_topmost_sequence ();
1660 : 464 : if (REG_P (crtl->args.internal_arg_pointer)
1661 : 464 : && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1662 : 0 : emit_insn_before (seq, parm_birth_insn);
1663 : : else
1664 : 464 : emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1665 : 464 : pop_topmost_sequence ();
1666 : 464 : return temp;
1667 : : }
1668 : : }
1669 : :
1670 : : /* Perform an untyped call and save the state required to perform an
1671 : : untyped return of whatever value was returned by the given function. */
1672 : :
1673 : : static rtx
1674 : 483 : expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1675 : : {
1676 : 483 : int size, align, regno;
1677 : 483 : fixed_size_mode mode;
1678 : 483 : rtx incoming_args, result, reg, dest, src;
1679 : 483 : rtx_call_insn *call_insn;
1680 : 483 : rtx old_stack_level = 0;
1681 : 483 : rtx call_fusage = 0;
1682 : 483 : rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1683 : :
1684 : 483 : arguments = convert_memory_address (Pmode, arguments);
1685 : :
1686 : : /* Create a block where the return registers can be saved. */
1687 : 483 : result = assign_stack_local (BLKmode, apply_result_size (), -1);
1688 : :
1689 : : /* Fetch the arg pointer from the ARGUMENTS block. */
1690 : 483 : incoming_args = gen_reg_rtx (Pmode);
1691 : 483 : emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1692 : 483 : if (!STACK_GROWS_DOWNWARD)
1693 : : incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1694 : : incoming_args, 0, OPTAB_LIB_WIDEN);
1695 : :
1696 : : /* Push a new argument block and copy the arguments. Do not allow
1697 : : the (potential) memcpy call below to interfere with our stack
1698 : : manipulations. */
1699 : 483 : do_pending_stack_adjust ();
1700 : 483 : NO_DEFER_POP;
1701 : :
1702 : : /* Save the stack with nonlocal if available. */
1703 : 483 : if (targetm.have_save_stack_nonlocal ())
1704 : 483 : emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1705 : : else
1706 : 0 : emit_stack_save (SAVE_BLOCK, &old_stack_level);
1707 : :
1708 : : /* Allocate a block of memory onto the stack and copy the memory
1709 : : arguments to the outgoing arguments address. We can pass TRUE
1710 : : as the 4th argument because we just saved the stack pointer
1711 : : and will restore it right after the call. */
1712 : 966 : allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1713 : :
1714 : : /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1715 : : may have already set current_function_calls_alloca to true.
1716 : : current_function_calls_alloca won't be set if argsize is zero,
1717 : : so we have to guarantee need_drap is true here. */
1718 : 483 : if (SUPPORTS_STACK_ALIGNMENT)
1719 : 483 : crtl->need_drap = true;
1720 : :
1721 : 483 : dest = virtual_outgoing_args_rtx;
1722 : 483 : if (!STACK_GROWS_DOWNWARD)
1723 : : {
1724 : : if (CONST_INT_P (argsize))
1725 : : dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1726 : : else
1727 : : dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1728 : : }
1729 : 483 : dest = gen_rtx_MEM (BLKmode, dest);
1730 : 483 : set_mem_align (dest, PARM_BOUNDARY);
1731 : 483 : src = gen_rtx_MEM (BLKmode, incoming_args);
1732 : 483 : set_mem_align (src, PARM_BOUNDARY);
1733 : 483 : emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1734 : :
1735 : : /* Refer to the argument block. */
1736 : 483 : apply_args_size ();
1737 : 483 : arguments = gen_rtx_MEM (BLKmode, arguments);
1738 : 483 : set_mem_align (arguments, PARM_BOUNDARY);
1739 : :
1740 : : /* Walk past the arg-pointer and structure value address. */
1741 : 483 : size = GET_MODE_SIZE (Pmode);
1742 : 483 : if (struct_value)
1743 : 0 : size += GET_MODE_SIZE (Pmode);
1744 : :
1745 : : /* Restore each of the registers previously saved. Make USE insns
1746 : : for each of these registers for use in making the call. */
1747 : 44919 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1748 : 44436 : if ((mode = apply_args_mode[regno]) != VOIDmode)
1749 : : {
1750 : 7245 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1751 : 7245 : if (size % align != 0)
1752 : 0 : size = CEIL (size, align) * align;
1753 : 7245 : reg = gen_rtx_REG (mode, regno);
1754 : 7245 : emit_move_insn (reg, adjust_address (arguments, mode, size));
1755 : 7245 : use_reg (&call_fusage, reg);
1756 : 14490 : size += GET_MODE_SIZE (mode);
1757 : : }
1758 : :
1759 : : /* Restore the structure value address unless this is passed as an
1760 : : "invisible" first argument. */
1761 : 483 : size = GET_MODE_SIZE (Pmode);
1762 : 483 : if (struct_value)
1763 : : {
1764 : 0 : rtx value = gen_reg_rtx (Pmode);
1765 : 0 : emit_move_insn (value, adjust_address (arguments, Pmode, size));
1766 : 0 : emit_move_insn (struct_value, value);
1767 : 0 : if (REG_P (struct_value))
1768 : 0 : use_reg (&call_fusage, struct_value);
1769 : : }
1770 : :
1771 : : /* All arguments and registers used for the call are set up by now! */
1772 : 483 : function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1773 : :
1774 : : /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1775 : : and we don't want to load it into a register as an optimization,
1776 : : because prepare_call_address already did it if it should be done. */
1777 : 483 : if (GET_CODE (function) != SYMBOL_REF)
1778 : 29 : function = memory_address (FUNCTION_MODE, function);
1779 : :
1780 : : /* Generate the actual call instruction and save the return value. */
1781 : 483 : if (targetm.have_untyped_call ())
1782 : : {
1783 : 483 : rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1784 : 483 : rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1785 : : result_vector (1, result));
1786 : 5800 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1787 : 5317 : if (CALL_P (insn))
1788 : 483 : add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1789 : 483 : emit_insn (seq);
1790 : : }
1791 : 0 : else if (targetm.have_call_value ())
1792 : : {
1793 : : rtx valreg = 0;
1794 : :
1795 : : /* Locate the unique return register. It is not possible to
1796 : : express a call that sets more than one return register using
1797 : : call_value; use untyped_call for that. In fact, untyped_call
1798 : : only needs to save the return registers in the given block. */
1799 : 0 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1800 : 0 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1801 : : {
1802 : 0 : gcc_assert (!valreg); /* have_untyped_call required. */
1803 : :
1804 : 0 : valreg = gen_rtx_REG (mode, regno);
1805 : : }
1806 : :
1807 : 0 : emit_insn (targetm.gen_call_value (valreg,
1808 : : gen_rtx_MEM (FUNCTION_MODE, function),
1809 : : const0_rtx, NULL_RTX, const0_rtx));
1810 : :
1811 : 0 : emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1812 : : }
1813 : : else
1814 : 0 : gcc_unreachable ();
1815 : :
1816 : : /* Find the CALL insn we just emitted, and attach the register usage
1817 : : information. */
1818 : 483 : call_insn = last_call_insn ();
1819 : 483 : add_function_usage_to (call_insn, call_fusage);
1820 : :
1821 : : /* Restore the stack. */
1822 : 483 : if (targetm.have_save_stack_nonlocal ())
1823 : 483 : emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1824 : : else
1825 : 0 : emit_stack_restore (SAVE_BLOCK, old_stack_level);
1826 : 483 : fixup_args_size_notes (call_insn, get_last_insn (), 0);
1827 : :
1828 : 483 : OK_DEFER_POP;
1829 : :
1830 : : /* Return the address of the result block. */
1831 : 483 : result = copy_addr_to_reg (XEXP (result, 0));
1832 : 483 : return convert_memory_address (ptr_mode, result);
1833 : : }
1834 : :
1835 : : /* Perform an untyped return. */
1836 : :
1837 : : static void
1838 : 379 : expand_builtin_return (rtx result)
1839 : : {
1840 : 379 : int size, align, regno;
1841 : 379 : fixed_size_mode mode;
1842 : 379 : rtx reg;
1843 : 379 : rtx_insn *call_fusage = 0;
1844 : :
1845 : 379 : result = convert_memory_address (Pmode, result);
1846 : :
1847 : 379 : apply_result_size ();
1848 : 379 : result = gen_rtx_MEM (BLKmode, result);
1849 : :
1850 : 379 : if (targetm.have_untyped_return ())
1851 : : {
1852 : 0 : rtx vector = result_vector (0, result);
1853 : 0 : emit_jump_insn (targetm.gen_untyped_return (result, vector));
1854 : 0 : emit_barrier ();
1855 : 0 : return;
1856 : : }
1857 : :
1858 : : /* Restore the return value and note that each value is used. */
1859 : : size = 0;
1860 : 35247 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1861 : 34868 : if ((mode = apply_result_mode[regno]) != VOIDmode)
1862 : : {
1863 : 3032 : align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1864 : 3032 : if (size % align != 0)
1865 : 0 : size = CEIL (size, align) * align;
1866 : 3032 : reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1867 : 3032 : emit_move_insn (reg, adjust_address (result, mode, size));
1868 : :
1869 : 3032 : push_to_sequence (call_fusage);
1870 : 3032 : emit_use (reg);
1871 : 3032 : call_fusage = end_sequence ();
1872 : 6064 : size += GET_MODE_SIZE (mode);
1873 : : }
1874 : :
1875 : : /* Put the USE insns before the return. */
1876 : 379 : emit_insn (call_fusage);
1877 : :
1878 : : /* Return whatever values was restored by jumping directly to the end
1879 : : of the function. */
1880 : 379 : expand_naked_return ();
1881 : : }
1882 : :
1883 : : /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1884 : :
1885 : : int
1886 : 2433 : type_to_class (tree type)
1887 : : {
1888 : 2433 : switch (TREE_CODE (type))
1889 : : {
1890 : : case VOID_TYPE: return void_type_class;
1891 : 930 : case INTEGER_TYPE: return integer_type_class;
1892 : 39 : case ENUMERAL_TYPE: return enumeral_type_class;
1893 : 38 : case BOOLEAN_TYPE: return boolean_type_class;
1894 : 1016 : case POINTER_TYPE: return pointer_type_class;
1895 : 27 : case REFERENCE_TYPE: return reference_type_class;
1896 : 36 : case OFFSET_TYPE: return offset_type_class;
1897 : 132 : case REAL_TYPE: return real_type_class;
1898 : 40 : case COMPLEX_TYPE: return complex_type_class;
1899 : 20 : case FUNCTION_TYPE: return function_type_class;
1900 : 0 : case METHOD_TYPE: return method_type_class;
1901 : 44 : case RECORD_TYPE: return record_type_class;
1902 : 44 : case UNION_TYPE:
1903 : 44 : case QUAL_UNION_TYPE: return union_type_class;
1904 : 24 : case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1905 : 24 : ? string_type_class : array_type_class);
1906 : 0 : case LANG_TYPE: return lang_type_class;
1907 : 0 : case OPAQUE_TYPE: return opaque_type_class;
1908 : 0 : case BITINT_TYPE: return bitint_type_class;
1909 : 30 : case VECTOR_TYPE: return vector_type_class;
1910 : 3 : default: return no_type_class;
1911 : : }
1912 : : }
1913 : :
1914 : : /* Expand a call EXP to __builtin_classify_type. */
1915 : :
1916 : : static rtx
1917 : 0 : expand_builtin_classify_type (tree exp)
1918 : : {
1919 : 0 : if (call_expr_nargs (exp))
1920 : 0 : return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1921 : 0 : return GEN_INT (no_type_class);
1922 : : }
1923 : :
1924 : : /* This helper macro, meant to be used in mathfn_built_in below, determines
1925 : : which among a set of builtin math functions is appropriate for a given type
1926 : : mode. The `F' (float) and `L' (long double) are automatically generated
1927 : : from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1928 : : types, there are additional types that are considered with 'F32', 'F64',
1929 : : 'F128', etc. suffixes. */
1930 : : #define CASE_MATHFN(MATHFN) \
1931 : : CASE_CFN_##MATHFN: \
1932 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1933 : : fcodel = BUILT_IN_##MATHFN##L ; break;
1934 : : /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1935 : : types. */
1936 : : #define CASE_MATHFN_FLOATN(MATHFN) \
1937 : : CASE_CFN_##MATHFN: \
1938 : : fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1939 : : fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1940 : : fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1941 : : fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1942 : : fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1943 : : break;
1944 : : /* Similar to above, but appends _R after any F/L suffix. */
1945 : : #define CASE_MATHFN_REENT(MATHFN) \
1946 : : case CFN_BUILT_IN_##MATHFN##_R: \
1947 : : case CFN_BUILT_IN_##MATHFN##F_R: \
1948 : : case CFN_BUILT_IN_##MATHFN##L_R: \
1949 : : fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1950 : : fcodel = BUILT_IN_##MATHFN##L_R ; break;
1951 : :
1952 : : /* Return a function equivalent to FN but operating on floating-point
1953 : : values of type TYPE, or END_BUILTINS if no such function exists.
1954 : : This is purely an operation on function codes; it does not guarantee
1955 : : that the target actually has an implementation of the function. */
1956 : :
1957 : : static built_in_function
1958 : 594725 : mathfn_built_in_2 (tree type, combined_fn fn)
1959 : : {
1960 : 594725 : tree mtype;
1961 : 594725 : built_in_function fcode, fcodef, fcodel;
1962 : 594725 : built_in_function fcodef16 = END_BUILTINS;
1963 : 594725 : built_in_function fcodef32 = END_BUILTINS;
1964 : 594725 : built_in_function fcodef64 = END_BUILTINS;
1965 : 594725 : built_in_function fcodef128 = END_BUILTINS;
1966 : 594725 : built_in_function fcodef32x = END_BUILTINS;
1967 : 594725 : built_in_function fcodef64x = END_BUILTINS;
1968 : 594725 : built_in_function fcodef128x = END_BUILTINS;
1969 : :
1970 : : /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1971 : : break the uses below. */
1972 : : #undef HUGE_VAL
1973 : : #undef NAN
1974 : :
1975 : 594725 : switch (fn)
1976 : : {
1977 : : #define SEQ_OF_CASE_MATHFN \
1978 : : CASE_MATHFN_FLOATN (ACOS) \
1979 : : CASE_MATHFN_FLOATN (ACOSH) \
1980 : : CASE_MATHFN_FLOATN (ASIN) \
1981 : : CASE_MATHFN_FLOATN (ASINH) \
1982 : : CASE_MATHFN_FLOATN (ATAN) \
1983 : : CASE_MATHFN_FLOATN (ATAN2) \
1984 : : CASE_MATHFN_FLOATN (ATANH) \
1985 : : CASE_MATHFN_FLOATN (CBRT) \
1986 : : CASE_MATHFN_FLOATN (CEIL) \
1987 : : CASE_MATHFN (CEXPI) \
1988 : : CASE_MATHFN_FLOATN (COPYSIGN) \
1989 : : CASE_MATHFN_FLOATN (COS) \
1990 : : CASE_MATHFN_FLOATN (COSH) \
1991 : : CASE_MATHFN (DREM) \
1992 : : CASE_MATHFN_FLOATN (ERF) \
1993 : : CASE_MATHFN_FLOATN (ERFC) \
1994 : : CASE_MATHFN_FLOATN (EXP) \
1995 : : CASE_MATHFN (EXP10) \
1996 : : CASE_MATHFN_FLOATN (EXP2) \
1997 : : CASE_MATHFN_FLOATN (EXPM1) \
1998 : : CASE_MATHFN_FLOATN (FABS) \
1999 : : CASE_MATHFN_FLOATN (FDIM) \
2000 : : CASE_MATHFN_FLOATN (FLOOR) \
2001 : : CASE_MATHFN_FLOATN (FMA) \
2002 : : CASE_MATHFN_FLOATN (FMAX) \
2003 : : CASE_MATHFN_FLOATN (FMIN) \
2004 : : CASE_MATHFN_FLOATN (FMOD) \
2005 : : CASE_MATHFN_FLOATN (FREXP) \
2006 : : CASE_MATHFN (GAMMA) \
2007 : : CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2008 : : CASE_MATHFN_FLOATN (HUGE_VAL) \
2009 : : CASE_MATHFN_FLOATN (HYPOT) \
2010 : : CASE_MATHFN_FLOATN (ILOGB) \
2011 : : CASE_MATHFN (ICEIL) \
2012 : : CASE_MATHFN (IFLOOR) \
2013 : : CASE_MATHFN_FLOATN (INF) \
2014 : : CASE_MATHFN (IRINT) \
2015 : : CASE_MATHFN (IROUND) \
2016 : : CASE_MATHFN (ISINF) \
2017 : : CASE_MATHFN (J0) \
2018 : : CASE_MATHFN (J1) \
2019 : : CASE_MATHFN (JN) \
2020 : : CASE_MATHFN (LCEIL) \
2021 : : CASE_MATHFN_FLOATN (LDEXP) \
2022 : : CASE_MATHFN (LFLOOR) \
2023 : : CASE_MATHFN_FLOATN (LGAMMA) \
2024 : : CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2025 : : CASE_MATHFN (LLCEIL) \
2026 : : CASE_MATHFN (LLFLOOR) \
2027 : : CASE_MATHFN_FLOATN (LLRINT) \
2028 : : CASE_MATHFN_FLOATN (LLROUND) \
2029 : : CASE_MATHFN_FLOATN (LOG) \
2030 : : CASE_MATHFN_FLOATN (LOG10) \
2031 : : CASE_MATHFN_FLOATN (LOG1P) \
2032 : : CASE_MATHFN_FLOATN (LOG2) \
2033 : : CASE_MATHFN_FLOATN (LOGB) \
2034 : : CASE_MATHFN_FLOATN (LRINT) \
2035 : : CASE_MATHFN_FLOATN (LROUND) \
2036 : : CASE_MATHFN_FLOATN (MODF) \
2037 : : CASE_MATHFN_FLOATN (NAN) \
2038 : : CASE_MATHFN_FLOATN (NANS) \
2039 : : CASE_MATHFN_FLOATN (NEARBYINT) \
2040 : : CASE_MATHFN_FLOATN (NEXTAFTER) \
2041 : : CASE_MATHFN (NEXTTOWARD) \
2042 : : CASE_MATHFN_FLOATN (POW) \
2043 : : CASE_MATHFN (POWI) \
2044 : : CASE_MATHFN (POW10) \
2045 : : CASE_MATHFN_FLOATN (REMAINDER) \
2046 : : CASE_MATHFN_FLOATN (REMQUO) \
2047 : : CASE_MATHFN_FLOATN (RINT) \
2048 : : CASE_MATHFN_FLOATN (ROUND) \
2049 : : CASE_MATHFN_FLOATN (ROUNDEVEN) \
2050 : : CASE_MATHFN (SCALB) \
2051 : : CASE_MATHFN_FLOATN (SCALBLN) \
2052 : : CASE_MATHFN_FLOATN (SCALBN) \
2053 : : CASE_MATHFN (SIGNBIT) \
2054 : : CASE_MATHFN (SIGNIFICAND) \
2055 : : CASE_MATHFN_FLOATN (SIN) \
2056 : : CASE_MATHFN (SINCOS) \
2057 : : CASE_MATHFN_FLOATN (SINH) \
2058 : : CASE_MATHFN_FLOATN (SQRT) \
2059 : : CASE_MATHFN_FLOATN (TAN) \
2060 : : CASE_MATHFN_FLOATN (TANH) \
2061 : : CASE_MATHFN_FLOATN (TGAMMA) \
2062 : : CASE_MATHFN_FLOATN (TRUNC) \
2063 : : CASE_MATHFN (Y0) \
2064 : : CASE_MATHFN (Y1) \
2065 : : CASE_MATHFN (YN)
2066 : :
2067 : 594662 : SEQ_OF_CASE_MATHFN
2068 : :
2069 : : default:
2070 : : return END_BUILTINS;
2071 : : }
2072 : :
2073 : 594725 : mtype = TYPE_MAIN_VARIANT (type);
2074 : 594725 : if (mtype == double_type_node)
2075 : : return fcode;
2076 : 552025 : else if (mtype == float_type_node)
2077 : : return fcodef;
2078 : 500522 : else if (mtype == long_double_type_node)
2079 : : return fcodel;
2080 : 467044 : else if (mtype == float16_type_node)
2081 : : return fcodef16;
2082 : 466996 : else if (mtype == float32_type_node)
2083 : : return fcodef32;
2084 : 466996 : else if (mtype == float64_type_node)
2085 : : return fcodef64;
2086 : 466996 : else if (mtype == float128_type_node)
2087 : : return fcodef128;
2088 : 459064 : else if (mtype == float32x_type_node)
2089 : : return fcodef32x;
2090 : 459064 : else if (mtype == float64x_type_node)
2091 : : return fcodef64x;
2092 : 459064 : else if (mtype == float128x_type_node)
2093 : : return fcodef128x;
2094 : : else
2095 : 459064 : return END_BUILTINS;
2096 : : }
2097 : :
2098 : : #undef CASE_MATHFN
2099 : : #undef CASE_MATHFN_FLOATN
2100 : : #undef CASE_MATHFN_REENT
2101 : :
2102 : : /* Return mathematic function equivalent to FN but operating directly on TYPE,
2103 : : if available. If IMPLICIT_P is true use the implicit builtin declaration,
2104 : : otherwise use the explicit declaration. If we can't do the conversion,
2105 : : return null. */
2106 : :
2107 : : static tree
2108 : 594581 : mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2109 : : {
2110 : 594581 : built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2111 : 594581 : if (fcode2 == END_BUILTINS)
2112 : : return NULL_TREE;
2113 : :
2114 : 131869 : if (implicit_p && !builtin_decl_implicit_p (fcode2))
2115 : : return NULL_TREE;
2116 : :
2117 : 127488 : return builtin_decl_explicit (fcode2);
2118 : : }
2119 : :
2120 : : /* Like mathfn_built_in_1, but always use the implicit array. */
2121 : :
2122 : : tree
2123 : 210 : mathfn_built_in (tree type, combined_fn fn)
2124 : : {
2125 : 210 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2126 : : }
2127 : :
2128 : : /* Like mathfn_built_in_1, but always use the explicit array. */
2129 : :
2130 : : tree
2131 : 0 : mathfn_built_in_explicit (tree type, combined_fn fn)
2132 : : {
2133 : 0 : return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2134 : : }
2135 : :
2136 : : /* Like mathfn_built_in_1, but take a built_in_function and
2137 : : always use the implicit array. */
2138 : :
2139 : : tree
2140 : 594150 : mathfn_built_in (tree type, enum built_in_function fn)
2141 : : {
2142 : 594150 : return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2143 : : }
2144 : :
2145 : : /* Return the type associated with a built in function, i.e., the one
2146 : : to be passed to mathfn_built_in to get the type-specific
2147 : : function. */
2148 : :
2149 : : tree
2150 : 1237 : mathfn_built_in_type (combined_fn fn)
2151 : : {
2152 : : #define CASE_MATHFN(MATHFN) \
2153 : : case CFN_BUILT_IN_##MATHFN: \
2154 : : return double_type_node; \
2155 : : case CFN_BUILT_IN_##MATHFN##F: \
2156 : : return float_type_node; \
2157 : : case CFN_BUILT_IN_##MATHFN##L: \
2158 : : return long_double_type_node;
2159 : :
2160 : : #define CASE_MATHFN_FLOATN(MATHFN) \
2161 : : CASE_MATHFN(MATHFN) \
2162 : : case CFN_BUILT_IN_##MATHFN##F16: \
2163 : : return float16_type_node; \
2164 : : case CFN_BUILT_IN_##MATHFN##F32: \
2165 : : return float32_type_node; \
2166 : : case CFN_BUILT_IN_##MATHFN##F64: \
2167 : : return float64_type_node; \
2168 : : case CFN_BUILT_IN_##MATHFN##F128: \
2169 : : return float128_type_node; \
2170 : : case CFN_BUILT_IN_##MATHFN##F32X: \
2171 : : return float32x_type_node; \
2172 : : case CFN_BUILT_IN_##MATHFN##F64X: \
2173 : : return float64x_type_node; \
2174 : : case CFN_BUILT_IN_##MATHFN##F128X: \
2175 : : return float128x_type_node;
2176 : :
2177 : : /* Similar to above, but appends _R after any F/L suffix. */
2178 : : #define CASE_MATHFN_REENT(MATHFN) \
2179 : : case CFN_BUILT_IN_##MATHFN##_R: \
2180 : : return double_type_node; \
2181 : : case CFN_BUILT_IN_##MATHFN##F_R: \
2182 : : return float_type_node; \
2183 : : case CFN_BUILT_IN_##MATHFN##L_R: \
2184 : : return long_double_type_node;
2185 : :
2186 : 1237 : switch (fn)
2187 : : {
2188 : 1237 : SEQ_OF_CASE_MATHFN
2189 : :
2190 : : default:
2191 : : return NULL_TREE;
2192 : : }
2193 : :
2194 : : #undef CASE_MATHFN
2195 : : #undef CASE_MATHFN_FLOATN
2196 : : #undef CASE_MATHFN_REENT
2197 : : #undef SEQ_OF_CASE_MATHFN
2198 : : }
2199 : :
2200 : : /* Check whether there is an internal function associated with function FN
2201 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2202 : : IFN_LAST.
2203 : :
2204 : : Note that this function only tests whether the function is defined in
2205 : : internals.def, not whether it is actually available on the target. */
2206 : :
2207 : : static internal_fn
2208 : 14109954 : associated_internal_fn (built_in_function fn, tree return_type)
2209 : : {
2210 : 14109954 : switch (fn)
2211 : : {
2212 : : #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2213 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2214 : : #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2215 : : CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2216 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2217 : : #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2218 : : CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2219 : : #include "internal-fn.def"
2220 : :
2221 : : CASE_FLT_FN (BUILT_IN_POW10):
2222 : : return IFN_EXP10;
2223 : :
2224 : : CASE_FLT_FN (BUILT_IN_DREM):
2225 : : return IFN_REMAINDER;
2226 : :
2227 : 19564 : CASE_FLT_FN (BUILT_IN_SCALBN):
2228 : 19564 : CASE_FLT_FN (BUILT_IN_SCALBLN):
2229 : 19564 : if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2230 : : return IFN_LDEXP;
2231 : : return IFN_LAST;
2232 : 75 : case BUILT_IN_CRC8_DATA8:
2233 : 75 : case BUILT_IN_CRC16_DATA8:
2234 : 75 : case BUILT_IN_CRC16_DATA16:
2235 : 75 : case BUILT_IN_CRC32_DATA8:
2236 : 75 : case BUILT_IN_CRC32_DATA16:
2237 : 75 : case BUILT_IN_CRC32_DATA32:
2238 : 75 : case BUILT_IN_CRC64_DATA8:
2239 : 75 : case BUILT_IN_CRC64_DATA16:
2240 : 75 : case BUILT_IN_CRC64_DATA32:
2241 : 75 : case BUILT_IN_CRC64_DATA64:
2242 : 75 : return IFN_CRC;
2243 : 132 : case BUILT_IN_REV_CRC8_DATA8:
2244 : 132 : case BUILT_IN_REV_CRC16_DATA8:
2245 : 132 : case BUILT_IN_REV_CRC16_DATA16:
2246 : 132 : case BUILT_IN_REV_CRC32_DATA8:
2247 : 132 : case BUILT_IN_REV_CRC32_DATA16:
2248 : 132 : case BUILT_IN_REV_CRC32_DATA32:
2249 : 132 : case BUILT_IN_REV_CRC64_DATA8:
2250 : 132 : case BUILT_IN_REV_CRC64_DATA16:
2251 : 132 : case BUILT_IN_REV_CRC64_DATA32:
2252 : 132 : case BUILT_IN_REV_CRC64_DATA64:
2253 : 132 : return IFN_CRC_REV;
2254 : : default:
2255 : : return IFN_LAST;
2256 : : }
2257 : : }
2258 : :
2259 : : /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2260 : : return its code, otherwise return IFN_LAST. Note that this function
2261 : : only tests whether the function is defined in internals.def, not whether
2262 : : it is actually available on the target. */
2263 : :
2264 : : internal_fn
2265 : 635978 : associated_internal_fn (tree fndecl)
2266 : : {
2267 : 635978 : gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2268 : 635978 : return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2269 : 635978 : TREE_TYPE (TREE_TYPE (fndecl)));
2270 : : }
2271 : :
2272 : : /* Check whether there is an internal function associated with function CFN
2273 : : and return type RETURN_TYPE. Return the function if so, otherwise return
2274 : : IFN_LAST.
2275 : :
2276 : : Note that this function only tests whether the function is defined in
2277 : : internals.def, not whether it is actually available on the target. */
2278 : :
2279 : : internal_fn
2280 : 32243757 : associated_internal_fn (combined_fn cfn, tree return_type)
2281 : : {
2282 : 32243757 : if (internal_fn_p (cfn))
2283 : 18769781 : return as_internal_fn (cfn);
2284 : 13473976 : return associated_internal_fn (as_builtin_fn (cfn), return_type);
2285 : : }
2286 : :
2287 : : /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2288 : : on the current target by a call to an internal function, return the
2289 : : code of that internal function, otherwise return IFN_LAST. The caller
2290 : : is responsible for ensuring that any side-effects of the built-in
2291 : : call are dealt with correctly. E.g. if CALL sets errno, the caller
2292 : : must decide that the errno result isn't needed or make it available
2293 : : in some other way. */
2294 : :
2295 : : internal_fn
2296 : 856097 : replacement_internal_fn (gcall *call)
2297 : : {
2298 : 856097 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2299 : : {
2300 : 633638 : internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2301 : 633638 : if (ifn != IFN_LAST)
2302 : : {
2303 : 61944 : tree_pair types = direct_internal_fn_types (ifn, call);
2304 : 61944 : optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2305 : 61944 : if (direct_internal_fn_supported_p (ifn, types, opt_type))
2306 : 40531 : return ifn;
2307 : : }
2308 : : }
2309 : : return IFN_LAST;
2310 : : }
2311 : :
2312 : : /* Expand a call to the builtin trinary math functions (fma).
2313 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2314 : : function in-line. EXP is the expression that is a call to the builtin
2315 : : function; if convenient, the result should be placed in TARGET.
2316 : : SUBTARGET may be used as the target for computing one of EXP's
2317 : : operands. */
2318 : :
2319 : : static rtx
2320 : 364 : expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2321 : : {
2322 : 364 : optab builtin_optab;
2323 : 364 : rtx op0, op1, op2, result;
2324 : 364 : rtx_insn *insns;
2325 : 364 : tree fndecl = get_callee_fndecl (exp);
2326 : 364 : tree arg0, arg1, arg2;
2327 : 364 : machine_mode mode;
2328 : :
2329 : 364 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2330 : : return NULL_RTX;
2331 : :
2332 : 364 : arg0 = CALL_EXPR_ARG (exp, 0);
2333 : 364 : arg1 = CALL_EXPR_ARG (exp, 1);
2334 : 364 : arg2 = CALL_EXPR_ARG (exp, 2);
2335 : :
2336 : 364 : switch (DECL_FUNCTION_CODE (fndecl))
2337 : : {
2338 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
2339 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2340 : 364 : builtin_optab = fma_optab; break;
2341 : 0 : default:
2342 : 0 : gcc_unreachable ();
2343 : : }
2344 : :
2345 : : /* Make a suitable register to place result in. */
2346 : 364 : mode = TYPE_MODE (TREE_TYPE (exp));
2347 : :
2348 : : /* Before working hard, check whether the instruction is available. */
2349 : 364 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2350 : : return NULL_RTX;
2351 : :
2352 : 0 : result = gen_reg_rtx (mode);
2353 : :
2354 : : /* Always stabilize the argument list. */
2355 : 0 : CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2356 : 0 : CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2357 : 0 : CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2358 : :
2359 : 0 : op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2360 : 0 : op1 = expand_normal (arg1);
2361 : 0 : op2 = expand_normal (arg2);
2362 : :
2363 : 0 : start_sequence ();
2364 : :
2365 : : /* Compute into RESULT.
2366 : : Set RESULT to wherever the result comes back. */
2367 : 0 : result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2368 : : result, 0);
2369 : :
2370 : : /* If we were unable to expand via the builtin, stop the sequence
2371 : : (without outputting the insns) and call to the library function
2372 : : with the stabilized argument list. */
2373 : 0 : if (result == 0)
2374 : : {
2375 : 0 : end_sequence ();
2376 : 0 : return expand_call (exp, target, target == const0_rtx);
2377 : : }
2378 : :
2379 : : /* Output the entire sequence. */
2380 : 0 : insns = end_sequence ();
2381 : 0 : emit_insn (insns);
2382 : :
2383 : 0 : return result;
2384 : : }
2385 : :
2386 : : /* Expand a call to the builtin sin and cos math functions.
2387 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2388 : : function in-line. EXP is the expression that is a call to the builtin
2389 : : function; if convenient, the result should be placed in TARGET.
2390 : : SUBTARGET may be used as the target for computing one of EXP's
2391 : : operands. */
2392 : :
2393 : : static rtx
2394 : 47 : expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2395 : : {
2396 : 47 : optab builtin_optab;
2397 : 47 : rtx op0;
2398 : 47 : rtx_insn *insns;
2399 : 47 : tree fndecl = get_callee_fndecl (exp);
2400 : 47 : machine_mode mode;
2401 : 47 : tree arg;
2402 : :
2403 : 47 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2404 : : return NULL_RTX;
2405 : :
2406 : 39 : arg = CALL_EXPR_ARG (exp, 0);
2407 : :
2408 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2409 : : {
2410 : 39 : CASE_FLT_FN (BUILT_IN_SIN):
2411 : 39 : CASE_FLT_FN (BUILT_IN_COS):
2412 : 39 : builtin_optab = sincos_optab; break;
2413 : 0 : default:
2414 : 0 : gcc_unreachable ();
2415 : : }
2416 : :
2417 : : /* Make a suitable register to place result in. */
2418 : 39 : mode = TYPE_MODE (TREE_TYPE (exp));
2419 : :
2420 : : /* Check if sincos insn is available, otherwise fallback
2421 : : to sin or cos insn. */
2422 : 39 : if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2423 : 39 : switch (DECL_FUNCTION_CODE (fndecl))
2424 : : {
2425 : : CASE_FLT_FN (BUILT_IN_SIN):
2426 : : builtin_optab = sin_optab; break;
2427 : 14 : CASE_FLT_FN (BUILT_IN_COS):
2428 : 14 : builtin_optab = cos_optab; break;
2429 : 0 : default:
2430 : 0 : gcc_unreachable ();
2431 : : }
2432 : :
2433 : : /* Before working hard, check whether the instruction is available. */
2434 : 39 : if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2435 : : {
2436 : 0 : rtx result = gen_reg_rtx (mode);
2437 : :
2438 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2439 : : need to expand the argument again. This way, we will not perform
2440 : : side-effects more the once. */
2441 : 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2442 : :
2443 : 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2444 : :
2445 : 0 : start_sequence ();
2446 : :
2447 : : /* Compute into RESULT.
2448 : : Set RESULT to wherever the result comes back. */
2449 : 0 : if (builtin_optab == sincos_optab)
2450 : : {
2451 : 0 : int ok;
2452 : :
2453 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
2454 : : {
2455 : 0 : CASE_FLT_FN (BUILT_IN_SIN):
2456 : 0 : ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2457 : 0 : break;
2458 : 0 : CASE_FLT_FN (BUILT_IN_COS):
2459 : 0 : ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2460 : 0 : break;
2461 : 0 : default:
2462 : 0 : gcc_unreachable ();
2463 : : }
2464 : 0 : gcc_assert (ok);
2465 : : }
2466 : : else
2467 : 0 : result = expand_unop (mode, builtin_optab, op0, result, 0);
2468 : :
2469 : 0 : if (result != 0)
2470 : : {
2471 : : /* Output the entire sequence. */
2472 : 0 : insns = end_sequence ();
2473 : 0 : emit_insn (insns);
2474 : 0 : return result;
2475 : : }
2476 : :
2477 : : /* If we were unable to expand via the builtin, stop the sequence
2478 : : (without outputting the insns) and call to the library function
2479 : : with the stabilized argument list. */
2480 : 0 : end_sequence ();
2481 : : }
2482 : :
2483 : 39 : return expand_call (exp, target, target == const0_rtx);
2484 : : }
2485 : :
2486 : : /* Given an interclass math builtin decl FNDECL and it's argument ARG
2487 : : return an RTL instruction code that implements the functionality.
2488 : : If that isn't possible or available return CODE_FOR_nothing. */
2489 : :
2490 : : static enum insn_code
2491 : 1094106 : interclass_mathfn_icode (tree arg, tree fndecl)
2492 : : {
2493 : 1094106 : bool errno_set = false;
2494 : 1094106 : optab builtin_optab = unknown_optab;
2495 : 1094106 : machine_mode mode;
2496 : :
2497 : 1094106 : switch (DECL_FUNCTION_CODE (fndecl))
2498 : : {
2499 : 4 : CASE_FLT_FN (BUILT_IN_ILOGB):
2500 : 4 : errno_set = true; builtin_optab = ilogb_optab; break;
2501 : 263436 : CASE_FLT_FN (BUILT_IN_ISINF):
2502 : 263436 : builtin_optab = isinf_optab; break;
2503 : 567306 : case BUILT_IN_ISFINITE:
2504 : 567306 : builtin_optab = isfinite_optab;
2505 : 567306 : break;
2506 : 261652 : case BUILT_IN_ISNORMAL:
2507 : 261652 : builtin_optab = isnormal_optab;
2508 : 261652 : break;
2509 : : CASE_FLT_FN (BUILT_IN_FINITE):
2510 : : case BUILT_IN_FINITED32:
2511 : : case BUILT_IN_FINITED64:
2512 : : case BUILT_IN_FINITED128:
2513 : : case BUILT_IN_ISINFD32:
2514 : : case BUILT_IN_ISINFD64:
2515 : : case BUILT_IN_ISINFD128:
2516 : : /* These builtins have no optabs (yet). */
2517 : : break;
2518 : 0 : default:
2519 : 0 : gcc_unreachable ();
2520 : : }
2521 : :
2522 : : /* There's no easy way to detect the case we need to set EDOM. */
2523 : 1094106 : if (flag_errno_math && errno_set)
2524 : : return CODE_FOR_nothing;
2525 : :
2526 : : /* Optab mode depends on the mode of the input argument. */
2527 : 1094106 : mode = TYPE_MODE (TREE_TYPE (arg));
2528 : :
2529 : 1094106 : if (builtin_optab)
2530 : 1092398 : return optab_handler (builtin_optab, mode);
2531 : : return CODE_FOR_nothing;
2532 : : }
2533 : :
2534 : : /* Expand a call to one of the builtin math functions that operate on
2535 : : floating point argument and output an integer result (ilogb, isinf,
2536 : : isnan, etc).
2537 : : Return 0 if a normal call should be emitted rather than expanding the
2538 : : function in-line. EXP is the expression that is a call to the builtin
2539 : : function; if convenient, the result should be placed in TARGET. */
2540 : :
2541 : : static rtx
2542 : 4 : expand_builtin_interclass_mathfn (tree exp, rtx target)
2543 : : {
2544 : 4 : enum insn_code icode = CODE_FOR_nothing;
2545 : 4 : rtx op0;
2546 : 4 : tree fndecl = get_callee_fndecl (exp);
2547 : 4 : machine_mode mode;
2548 : 4 : tree arg;
2549 : :
2550 : 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2551 : : return NULL_RTX;
2552 : :
2553 : 4 : arg = CALL_EXPR_ARG (exp, 0);
2554 : 4 : icode = interclass_mathfn_icode (arg, fndecl);
2555 : 4 : mode = TYPE_MODE (TREE_TYPE (arg));
2556 : :
2557 : 4 : if (icode != CODE_FOR_nothing)
2558 : : {
2559 : 2 : class expand_operand ops[1];
2560 : 2 : rtx_insn *last = get_last_insn ();
2561 : 2 : tree orig_arg = arg;
2562 : :
2563 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2564 : : need to expand the argument again. This way, we will not perform
2565 : : side-effects more the once. */
2566 : 2 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2567 : :
2568 : 2 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2569 : :
2570 : 2 : if (mode != GET_MODE (op0))
2571 : 0 : op0 = convert_to_mode (mode, op0, 0);
2572 : :
2573 : 2 : create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2574 : 2 : if (maybe_legitimize_operands (icode, 0, 1, ops)
2575 : 2 : && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2576 : 1 : return ops[0].value;
2577 : :
2578 : 1 : delete_insns_since (last);
2579 : 1 : CALL_EXPR_ARG (exp, 0) = orig_arg;
2580 : : }
2581 : :
2582 : : return NULL_RTX;
2583 : : }
2584 : :
2585 : : /* Expand a call to the builtin sincos math function.
2586 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
2587 : : function in-line. EXP is the expression that is a call to the builtin
2588 : : function. */
2589 : :
2590 : : static rtx
2591 : 3 : expand_builtin_sincos (tree exp)
2592 : : {
2593 : 3 : rtx op0, op1, op2, target1, target2;
2594 : 3 : machine_mode mode;
2595 : 3 : tree arg, sinp, cosp;
2596 : 3 : int result;
2597 : 3 : location_t loc = EXPR_LOCATION (exp);
2598 : 3 : tree alias_type, alias_off;
2599 : :
2600 : 3 : if (!validate_arglist (exp, REAL_TYPE,
2601 : : POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2602 : : return NULL_RTX;
2603 : :
2604 : 3 : arg = CALL_EXPR_ARG (exp, 0);
2605 : 3 : sinp = CALL_EXPR_ARG (exp, 1);
2606 : 3 : cosp = CALL_EXPR_ARG (exp, 2);
2607 : :
2608 : : /* Make a suitable register to place result in. */
2609 : 3 : mode = TYPE_MODE (TREE_TYPE (arg));
2610 : :
2611 : : /* Check if sincos insn is available, otherwise emit the call. */
2612 : 3 : if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2613 : : return NULL_RTX;
2614 : :
2615 : 0 : target1 = gen_reg_rtx (mode);
2616 : 0 : target2 = gen_reg_rtx (mode);
2617 : :
2618 : 0 : op0 = expand_normal (arg);
2619 : 0 : alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2620 : 0 : alias_off = build_int_cst (alias_type, 0);
2621 : 0 : op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2622 : : sinp, alias_off));
2623 : 0 : op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2624 : : cosp, alias_off));
2625 : :
2626 : : /* Compute into target1 and target2.
2627 : : Set TARGET to wherever the result comes back. */
2628 : 0 : result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2629 : 0 : gcc_assert (result);
2630 : :
2631 : : /* Move target1 and target2 to the memory locations indicated
2632 : : by op1 and op2. */
2633 : 0 : emit_move_insn (op1, target1);
2634 : 0 : emit_move_insn (op2, target2);
2635 : :
2636 : 0 : return const0_rtx;
2637 : : }
2638 : :
2639 : : /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2640 : : result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2641 : : static rtx
2642 : 60 : expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2643 : : {
2644 : 60 : if (!validate_arglist (exp, VOID_TYPE))
2645 : : return NULL_RTX;
2646 : :
2647 : 60 : insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2648 : 60 : if (icode == CODE_FOR_nothing)
2649 : : return NULL_RTX;
2650 : :
2651 : 0 : if (target == 0
2652 : 0 : || GET_MODE (target) != target_mode
2653 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2654 : 0 : target = gen_reg_rtx (target_mode);
2655 : :
2656 : 0 : rtx pat = GEN_FCN (icode) (target);
2657 : 0 : if (!pat)
2658 : : return NULL_RTX;
2659 : 0 : emit_insn (pat);
2660 : :
2661 : 0 : return target;
2662 : : }
2663 : :
2664 : : /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2665 : : fenv.h), returning the result and setting it in TARGET. Otherwise return
2666 : : NULL_RTX on failure. */
2667 : : static rtx
2668 : 1894 : expand_builtin_feclear_feraise_except (tree exp, rtx target,
2669 : : machine_mode target_mode, optab op_optab)
2670 : : {
2671 : 1894 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2672 : : return NULL_RTX;
2673 : 1894 : rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2674 : :
2675 : 1894 : insn_code icode = direct_optab_handler (op_optab, SImode);
2676 : 1894 : if (icode == CODE_FOR_nothing)
2677 : : return NULL_RTX;
2678 : :
2679 : 0 : if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2680 : : return NULL_RTX;
2681 : :
2682 : 0 : if (target == 0
2683 : 0 : || GET_MODE (target) != target_mode
2684 : 0 : || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2685 : 0 : target = gen_reg_rtx (target_mode);
2686 : :
2687 : 0 : rtx pat = GEN_FCN (icode) (target, op0);
2688 : 0 : if (!pat)
2689 : : return NULL_RTX;
2690 : 0 : emit_insn (pat);
2691 : :
2692 : 0 : return target;
2693 : : }
2694 : :
2695 : : /* Expand a call to the internal cexpi builtin to the sincos math function.
2696 : : EXP is the expression that is a call to the builtin function; if convenient,
2697 : : the result should be placed in TARGET. */
2698 : :
2699 : : static rtx
2700 : 157 : expand_builtin_cexpi (tree exp, rtx target)
2701 : : {
2702 : 157 : tree fndecl = get_callee_fndecl (exp);
2703 : 157 : tree arg, type;
2704 : 157 : machine_mode mode;
2705 : 157 : rtx op0, op1, op2;
2706 : 157 : location_t loc = EXPR_LOCATION (exp);
2707 : :
2708 : 157 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2709 : : return NULL_RTX;
2710 : :
2711 : 157 : arg = CALL_EXPR_ARG (exp, 0);
2712 : 157 : type = TREE_TYPE (arg);
2713 : 157 : mode = TYPE_MODE (TREE_TYPE (arg));
2714 : :
2715 : : /* Try expanding via a sincos optab, fall back to emitting a libcall
2716 : : to sincos or cexp. We are sure we have sincos or cexp because cexpi
2717 : : is only generated from sincos, cexp or if we have either of them. */
2718 : 157 : if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2719 : : {
2720 : 6 : op1 = gen_reg_rtx (mode);
2721 : 6 : op2 = gen_reg_rtx (mode);
2722 : :
2723 : 6 : op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2724 : :
2725 : : /* Compute into op1 and op2. */
2726 : 6 : expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2727 : : }
2728 : 151 : else if (targetm.libc_has_function (function_sincos, type))
2729 : : {
2730 : 151 : tree call, fn = NULL_TREE;
2731 : 151 : tree top1, top2;
2732 : 151 : rtx op1a, op2a;
2733 : :
2734 : 151 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2735 : 51 : fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2736 : 100 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2737 : 81 : fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2738 : 19 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2739 : 19 : fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2740 : : else
2741 : 0 : gcc_unreachable ();
2742 : :
2743 : 151 : op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2744 : 151 : op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2745 : 151 : op1a = copy_addr_to_reg (XEXP (op1, 0));
2746 : 151 : op2a = copy_addr_to_reg (XEXP (op2, 0));
2747 : 151 : top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2748 : 151 : top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2749 : :
2750 : : /* Make sure not to fold the sincos call again. */
2751 : 151 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2752 : 151 : expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2753 : : call, 3, arg, top1, top2));
2754 : : }
2755 : : else
2756 : : {
2757 : 0 : tree call, fn = NULL_TREE, narg;
2758 : 0 : tree ctype = build_complex_type (type);
2759 : :
2760 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2761 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2762 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2763 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXP);
2764 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2765 : 0 : fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2766 : : else
2767 : 0 : gcc_unreachable ();
2768 : :
2769 : : /* If we don't have a decl for cexp create one. This is the
2770 : : friendliest fallback if the user calls __builtin_cexpi
2771 : : without full target C99 function support. */
2772 : 0 : if (fn == NULL_TREE)
2773 : : {
2774 : 0 : tree fntype;
2775 : 0 : const char *name = NULL;
2776 : :
2777 : 0 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2778 : : name = "cexpf";
2779 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2780 : : name = "cexp";
2781 : 0 : else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2782 : 0 : name = "cexpl";
2783 : :
2784 : 0 : fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2785 : 0 : fn = build_fn_decl (name, fntype);
2786 : : }
2787 : :
2788 : 0 : narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2789 : : build_real (type, dconst0), arg);
2790 : :
2791 : : /* Make sure not to fold the cexp call again. */
2792 : 0 : call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2793 : 0 : return expand_expr (build_call_nary (ctype, call, 1, narg),
2794 : 0 : target, VOIDmode, EXPAND_NORMAL);
2795 : : }
2796 : :
2797 : : /* Now build the proper return type. */
2798 : 314 : return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2799 : 157 : make_tree (TREE_TYPE (arg), op2),
2800 : 157 : make_tree (TREE_TYPE (arg), op1)),
2801 : 157 : target, VOIDmode, EXPAND_NORMAL);
2802 : : }
2803 : :
2804 : : /* Conveniently construct a function call expression. FNDECL names the
2805 : : function to be called, N is the number of arguments, and the "..."
2806 : : parameters are the argument expressions. Unlike build_call_exr
2807 : : this doesn't fold the call, hence it will always return a CALL_EXPR. */
2808 : :
2809 : : static tree
2810 : 136368 : build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2811 : : {
2812 : 136368 : va_list ap;
2813 : 136368 : tree fntype = TREE_TYPE (fndecl);
2814 : 136368 : tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2815 : :
2816 : 136368 : va_start (ap, n);
2817 : 136368 : fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2818 : 136368 : va_end (ap);
2819 : 136368 : SET_EXPR_LOCATION (fn, loc);
2820 : 136368 : return fn;
2821 : : }
2822 : :
2823 : : /* Expand the __builtin_issignaling builtin. This needs to handle
2824 : : all floating point formats that do support NaNs (for those that
2825 : : don't it just sets target to 0). */
2826 : :
2827 : : static rtx
2828 : 823 : expand_builtin_issignaling (tree exp, rtx target)
2829 : : {
2830 : 823 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2831 : : return NULL_RTX;
2832 : :
2833 : 823 : tree arg = CALL_EXPR_ARG (exp, 0);
2834 : 823 : scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2835 : 823 : const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2836 : :
2837 : : /* Expand the argument yielding a RTX expression. */
2838 : 823 : rtx temp = expand_normal (arg);
2839 : :
2840 : : /* If mode doesn't support NaN, always return 0.
2841 : : Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2842 : : __builtin_issignaling working without -fsignaling-nans. Especially
2843 : : when -fno-signaling-nans is the default.
2844 : : On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2845 : : -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2846 : : fold to 0 or non-NaN/Inf classification. */
2847 : 823 : if (!HONOR_NANS (fmode))
2848 : : {
2849 : 0 : emit_move_insn (target, const0_rtx);
2850 : 0 : return target;
2851 : : }
2852 : :
2853 : : /* Check if the back end provides an insn that handles issignaling for the
2854 : : argument's mode. */
2855 : 823 : enum insn_code icode = optab_handler (issignaling_optab, fmode);
2856 : 823 : if (icode != CODE_FOR_nothing)
2857 : : {
2858 : 155 : rtx_insn *last = get_last_insn ();
2859 : 155 : rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2860 : 155 : if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2861 : : return this_target;
2862 : 0 : delete_insns_since (last);
2863 : : }
2864 : :
2865 : 668 : if (DECIMAL_FLOAT_MODE_P (fmode))
2866 : : {
2867 : 63 : scalar_int_mode imode;
2868 : 63 : rtx hi;
2869 : 63 : switch (fmt->ieee_bits)
2870 : : {
2871 : 42 : case 32:
2872 : 42 : case 64:
2873 : 42 : imode = int_mode_for_mode (fmode).require ();
2874 : 42 : temp = gen_lowpart (imode, temp);
2875 : 42 : break;
2876 : 21 : case 128:
2877 : 21 : imode = int_mode_for_size (64, 1).require ();
2878 : 21 : hi = NULL_RTX;
2879 : : /* For decimal128, TImode support isn't always there and even when
2880 : : it is, working on the DImode high part is usually better. */
2881 : 21 : if (!MEM_P (temp))
2882 : : {
2883 : 6 : if (rtx t = force_highpart_subreg (imode, temp, fmode))
2884 : : hi = t;
2885 : : else
2886 : : {
2887 : 0 : scalar_int_mode imode2;
2888 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2889 : : {
2890 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2891 : 0 : if (rtx t = force_highpart_subreg (imode, temp2, imode2))
2892 : : hi = t;
2893 : : }
2894 : : }
2895 : 0 : if (!hi)
2896 : : {
2897 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2898 : 0 : emit_move_insn (mem, temp);
2899 : 0 : temp = mem;
2900 : : }
2901 : : }
2902 : 0 : if (!hi)
2903 : : {
2904 : 15 : poly_int64 offset
2905 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
2906 : 15 : hi = adjust_address (temp, imode, offset);
2907 : : }
2908 : : temp = hi;
2909 : : break;
2910 : 0 : default:
2911 : 0 : gcc_unreachable ();
2912 : : }
2913 : : /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2914 : : have 6 bits below it all set. */
2915 : 63 : rtx val
2916 : 63 : = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2917 : 63 : temp = expand_binop (imode, and_optab, temp, val,
2918 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2919 : 63 : temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2920 : 63 : return temp;
2921 : : }
2922 : :
2923 : : /* Only PDP11 has these defined differently but doesn't support NaNs. */
2924 : 605 : gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2925 : 605 : gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2926 : 4235 : gcc_assert (MODE_COMPOSITE_P (fmode)
2927 : : || (fmt->pnan == fmt->p
2928 : : && fmt->signbit_ro == fmt->signbit_rw));
2929 : :
2930 : 605 : switch (fmt->p)
2931 : : {
2932 : 0 : case 106: /* IBM double double */
2933 : : /* For IBM double double, recurse on the most significant double. */
2934 : 0 : gcc_assert (MODE_COMPOSITE_P (fmode));
2935 : 0 : temp = convert_modes (DFmode, fmode, temp, 0);
2936 : 0 : fmode = DFmode;
2937 : 0 : fmt = REAL_MODE_FORMAT (DFmode);
2938 : : /* FALLTHRU */
2939 : 505 : case 8: /* bfloat */
2940 : 505 : case 11: /* IEEE half */
2941 : 505 : case 24: /* IEEE single */
2942 : 505 : case 53: /* IEEE double or Intel extended with rounding to double */
2943 : 505 : if (fmt->p == 53 && fmt->signbit_ro == 79)
2944 : 0 : goto extended;
2945 : 505 : {
2946 : 505 : scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2947 : 505 : temp = gen_lowpart (imode, temp);
2948 : 505 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2949 : : & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2950 : 505 : if (fmt->qnan_msb_set)
2951 : : {
2952 : 505 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2953 : 505 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2954 : : /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2955 : : ((temp ^ bit) & mask) > val. */
2956 : 505 : temp = expand_binop (imode, xor_optab, temp, bit,
2957 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2958 : 505 : temp = expand_binop (imode, and_optab, temp, mask,
2959 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2960 : 505 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
2961 : : 1, 1);
2962 : : }
2963 : : else
2964 : : {
2965 : : /* For MIPS/PA IEEE single/double, expand to:
2966 : : (temp & val) == val. */
2967 : 0 : temp = expand_binop (imode, and_optab, temp, val,
2968 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
2969 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
2970 : : 1, 1);
2971 : : }
2972 : : }
2973 : : break;
2974 : 100 : case 113: /* IEEE quad */
2975 : 100 : {
2976 : 100 : rtx hi = NULL_RTX, lo = NULL_RTX;
2977 : 100 : scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2978 : : /* For IEEE quad, TImode support isn't always there and even when
2979 : : it is, working on DImode parts is usually better. */
2980 : 100 : if (!MEM_P (temp))
2981 : : {
2982 : 85 : hi = force_highpart_subreg (imode, temp, fmode);
2983 : 85 : lo = force_lowpart_subreg (imode, temp, fmode);
2984 : 85 : if (!hi || !lo)
2985 : : {
2986 : 0 : scalar_int_mode imode2;
2987 : 0 : if (int_mode_for_mode (fmode).exists (&imode2))
2988 : : {
2989 : 0 : rtx temp2 = gen_lowpart (imode2, temp);
2990 : 0 : hi = force_highpart_subreg (imode, temp2, imode2);
2991 : 0 : lo = force_lowpart_subreg (imode, temp2, imode2);
2992 : : }
2993 : : }
2994 : 85 : if (!hi || !lo)
2995 : : {
2996 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2997 : 0 : emit_move_insn (mem, temp);
2998 : 0 : temp = mem;
2999 : : }
3000 : : }
3001 : 100 : if (!hi || !lo)
3002 : : {
3003 : 15 : poly_int64 offset
3004 : 15 : = subreg_highpart_offset (imode, GET_MODE (temp));
3005 : 15 : hi = adjust_address (temp, imode, offset);
3006 : 15 : offset = subreg_lowpart_offset (imode, GET_MODE (temp));
3007 : 15 : lo = adjust_address (temp, imode, offset);
3008 : : }
3009 : 100 : rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
3010 : : & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
3011 : 100 : if (fmt->qnan_msb_set)
3012 : : {
3013 : 100 : rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
3014 : : - 64)));
3015 : 100 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
3016 : : /* For non-MIPS/PA IEEE quad, expand to:
3017 : : (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
3018 : 100 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3019 : 100 : lo = expand_binop (imode, ior_optab, lo, nlo,
3020 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3021 : 100 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
3022 : 100 : temp = expand_binop (imode, xor_optab, hi, bit,
3023 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3024 : 100 : temp = expand_binop (imode, ior_optab, temp, lo,
3025 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3026 : 100 : temp = expand_binop (imode, and_optab, temp, mask,
3027 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3028 : 100 : temp = emit_store_flag_force (target, GTU, temp, val, imode,
3029 : : 1, 1);
3030 : : }
3031 : : else
3032 : : {
3033 : : /* For MIPS/PA IEEE quad, expand to:
3034 : : (hi & val) == val. */
3035 : 0 : temp = expand_binop (imode, and_optab, hi, val,
3036 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3037 : 0 : temp = emit_store_flag_force (target, EQ, temp, val, imode,
3038 : : 1, 1);
3039 : : }
3040 : : }
3041 : : break;
3042 : 0 : case 64: /* Intel or Motorola extended */
3043 : 0 : extended:
3044 : 0 : {
3045 : 0 : rtx ex, hi, lo;
3046 : 0 : scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3047 : 0 : scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3048 : 0 : if (!MEM_P (temp))
3049 : : {
3050 : 0 : rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3051 : 0 : emit_move_insn (mem, temp);
3052 : 0 : temp = mem;
3053 : : }
3054 : 0 : if (fmt->signbit_ro == 95)
3055 : : {
3056 : : /* Motorola, always big endian, with 16-bit gap in between
3057 : : 16-bit sign+exponent and 64-bit mantissa. */
3058 : 0 : ex = adjust_address (temp, iemode, 0);
3059 : 0 : hi = adjust_address (temp, imode, 4);
3060 : 0 : lo = adjust_address (temp, imode, 8);
3061 : : }
3062 : 0 : else if (!WORDS_BIG_ENDIAN)
3063 : : {
3064 : : /* Intel little endian, 64-bit mantissa followed by 16-bit
3065 : : sign+exponent and then either 16 or 48 bits of gap. */
3066 : 0 : ex = adjust_address (temp, iemode, 8);
3067 : 0 : hi = adjust_address (temp, imode, 4);
3068 : 0 : lo = adjust_address (temp, imode, 0);
3069 : : }
3070 : : else
3071 : : {
3072 : : /* Big endian Itanium. */
3073 : : ex = adjust_address (temp, iemode, 0);
3074 : : hi = adjust_address (temp, imode, 2);
3075 : : lo = adjust_address (temp, imode, 6);
3076 : : }
3077 : 0 : rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3078 : 0 : gcc_assert (fmt->qnan_msb_set);
3079 : 0 : rtx mask = GEN_INT (0x7fff);
3080 : 0 : rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3081 : : /* For Intel/Motorola extended format, expand to:
3082 : : (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3083 : 0 : rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3084 : 0 : lo = expand_binop (imode, ior_optab, lo, nlo,
3085 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3086 : 0 : lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3087 : 0 : temp = expand_binop (imode, xor_optab, hi, bit,
3088 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3089 : 0 : temp = expand_binop (imode, ior_optab, temp, lo,
3090 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3091 : 0 : temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3092 : 0 : ex = expand_binop (iemode, and_optab, ex, mask,
3093 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3094 : 0 : ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3095 : : ex, mask, iemode, 1, 1);
3096 : 0 : temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3097 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
3098 : : }
3099 : 0 : break;
3100 : 0 : default:
3101 : 0 : gcc_unreachable ();
3102 : : }
3103 : :
3104 : : return temp;
3105 : : }
3106 : :
3107 : : /* Expand a call to one of the builtin rounding functions gcc defines
3108 : : as an extension (lfloor and lceil). As these are gcc extensions we
3109 : : do not need to worry about setting errno to EDOM.
3110 : : If expanding via optab fails, lower expression to (int)(floor(x)).
3111 : : EXP is the expression that is a call to the builtin function;
3112 : : if convenient, the result should be placed in TARGET. */
3113 : :
3114 : : static rtx
3115 : 231 : expand_builtin_int_roundingfn (tree exp, rtx target)
3116 : : {
3117 : 231 : convert_optab builtin_optab;
3118 : 231 : rtx op0, tmp;
3119 : 231 : rtx_insn *insns;
3120 : 231 : tree fndecl = get_callee_fndecl (exp);
3121 : 231 : enum built_in_function fallback_fn;
3122 : 231 : tree fallback_fndecl;
3123 : 231 : machine_mode mode;
3124 : 231 : tree arg;
3125 : :
3126 : 231 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3127 : : return NULL_RTX;
3128 : :
3129 : 231 : arg = CALL_EXPR_ARG (exp, 0);
3130 : :
3131 : 231 : switch (DECL_FUNCTION_CODE (fndecl))
3132 : : {
3133 : : CASE_FLT_FN (BUILT_IN_ICEIL):
3134 : : CASE_FLT_FN (BUILT_IN_LCEIL):
3135 : : CASE_FLT_FN (BUILT_IN_LLCEIL):
3136 : : builtin_optab = lceil_optab;
3137 : : fallback_fn = BUILT_IN_CEIL;
3138 : : break;
3139 : :
3140 : 116 : CASE_FLT_FN (BUILT_IN_IFLOOR):
3141 : 116 : CASE_FLT_FN (BUILT_IN_LFLOOR):
3142 : 116 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
3143 : 116 : builtin_optab = lfloor_optab;
3144 : 116 : fallback_fn = BUILT_IN_FLOOR;
3145 : 116 : break;
3146 : :
3147 : 0 : default:
3148 : 0 : gcc_unreachable ();
3149 : : }
3150 : :
3151 : : /* Make a suitable register to place result in. */
3152 : 231 : mode = TYPE_MODE (TREE_TYPE (exp));
3153 : :
3154 : 231 : target = gen_reg_rtx (mode);
3155 : :
3156 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3157 : : need to expand the argument again. This way, we will not perform
3158 : : side-effects more the once. */
3159 : 231 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3160 : :
3161 : 231 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3162 : :
3163 : 231 : start_sequence ();
3164 : :
3165 : : /* Compute into TARGET. */
3166 : 231 : if (expand_sfix_optab (target, op0, builtin_optab))
3167 : : {
3168 : : /* Output the entire sequence. */
3169 : 40 : insns = end_sequence ();
3170 : 40 : emit_insn (insns);
3171 : 40 : return target;
3172 : : }
3173 : :
3174 : : /* If we were unable to expand via the builtin, stop the sequence
3175 : : (without outputting the insns). */
3176 : 191 : end_sequence ();
3177 : :
3178 : : /* Fall back to floating point rounding optab. */
3179 : 191 : fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3180 : :
3181 : : /* For non-C99 targets we may end up without a fallback fndecl here
3182 : : if the user called __builtin_lfloor directly. In this case emit
3183 : : a call to the floor/ceil variants nevertheless. This should result
3184 : : in the best user experience for not full C99 targets. */
3185 : 191 : if (fallback_fndecl == NULL_TREE)
3186 : : {
3187 : 0 : tree fntype;
3188 : 0 : const char *name = NULL;
3189 : :
3190 : 0 : switch (DECL_FUNCTION_CODE (fndecl))
3191 : : {
3192 : : case BUILT_IN_ICEIL:
3193 : : case BUILT_IN_LCEIL:
3194 : : case BUILT_IN_LLCEIL:
3195 : : name = "ceil";
3196 : : break;
3197 : 0 : case BUILT_IN_ICEILF:
3198 : 0 : case BUILT_IN_LCEILF:
3199 : 0 : case BUILT_IN_LLCEILF:
3200 : 0 : name = "ceilf";
3201 : 0 : break;
3202 : 0 : case BUILT_IN_ICEILL:
3203 : 0 : case BUILT_IN_LCEILL:
3204 : 0 : case BUILT_IN_LLCEILL:
3205 : 0 : name = "ceill";
3206 : 0 : break;
3207 : 0 : case BUILT_IN_IFLOOR:
3208 : 0 : case BUILT_IN_LFLOOR:
3209 : 0 : case BUILT_IN_LLFLOOR:
3210 : 0 : name = "floor";
3211 : 0 : break;
3212 : 0 : case BUILT_IN_IFLOORF:
3213 : 0 : case BUILT_IN_LFLOORF:
3214 : 0 : case BUILT_IN_LLFLOORF:
3215 : 0 : name = "floorf";
3216 : 0 : break;
3217 : 0 : case BUILT_IN_IFLOORL:
3218 : 0 : case BUILT_IN_LFLOORL:
3219 : 0 : case BUILT_IN_LLFLOORL:
3220 : 0 : name = "floorl";
3221 : 0 : break;
3222 : 0 : default:
3223 : 0 : gcc_unreachable ();
3224 : : }
3225 : :
3226 : 0 : fntype = build_function_type_list (TREE_TYPE (arg),
3227 : 0 : TREE_TYPE (arg), NULL_TREE);
3228 : 0 : fallback_fndecl = build_fn_decl (name, fntype);
3229 : : }
3230 : :
3231 : 191 : exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3232 : :
3233 : 191 : tmp = expand_normal (exp);
3234 : 191 : tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3235 : :
3236 : : /* Truncate the result of floating point optab to integer
3237 : : via expand_fix (). */
3238 : 191 : target = gen_reg_rtx (mode);
3239 : 191 : expand_fix (target, tmp, 0);
3240 : :
3241 : 191 : return target;
3242 : : }
3243 : :
3244 : : /* Expand a call to one of the builtin math functions doing integer
3245 : : conversion (lrint).
3246 : : Return 0 if a normal call should be emitted rather than expanding the
3247 : : function in-line. EXP is the expression that is a call to the builtin
3248 : : function; if convenient, the result should be placed in TARGET. */
3249 : :
3250 : : static rtx
3251 : 590 : expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3252 : : {
3253 : 590 : convert_optab builtin_optab;
3254 : 590 : rtx op0;
3255 : 590 : rtx_insn *insns;
3256 : 590 : tree fndecl = get_callee_fndecl (exp);
3257 : 590 : tree arg;
3258 : 590 : machine_mode mode;
3259 : 590 : enum built_in_function fallback_fn = BUILT_IN_NONE;
3260 : :
3261 : 590 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3262 : : return NULL_RTX;
3263 : :
3264 : 490 : arg = CALL_EXPR_ARG (exp, 0);
3265 : :
3266 : 490 : switch (DECL_FUNCTION_CODE (fndecl))
3267 : : {
3268 : 8 : CASE_FLT_FN (BUILT_IN_IRINT):
3269 : 8 : fallback_fn = BUILT_IN_LRINT;
3270 : : gcc_fallthrough ();
3271 : : CASE_FLT_FN (BUILT_IN_LRINT):
3272 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3273 : : builtin_optab = lrint_optab;
3274 : : break;
3275 : :
3276 : 221 : CASE_FLT_FN (BUILT_IN_IROUND):
3277 : 221 : fallback_fn = BUILT_IN_LROUND;
3278 : : gcc_fallthrough ();
3279 : : CASE_FLT_FN (BUILT_IN_LROUND):
3280 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3281 : : builtin_optab = lround_optab;
3282 : : break;
3283 : :
3284 : 0 : default:
3285 : 0 : gcc_unreachable ();
3286 : : }
3287 : :
3288 : : /* There's no easy way to detect the case we need to set EDOM. */
3289 : 490 : if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3290 : : return NULL_RTX;
3291 : :
3292 : : /* Make a suitable register to place result in. */
3293 : 259 : mode = TYPE_MODE (TREE_TYPE (exp));
3294 : :
3295 : : /* There's no easy way to detect the case we need to set EDOM. */
3296 : 259 : if (!flag_errno_math)
3297 : : {
3298 : 259 : rtx result = gen_reg_rtx (mode);
3299 : :
3300 : : /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3301 : : need to expand the argument again. This way, we will not perform
3302 : : side-effects more the once. */
3303 : 259 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3304 : :
3305 : 259 : op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3306 : :
3307 : 259 : start_sequence ();
3308 : :
3309 : 259 : if (expand_sfix_optab (result, op0, builtin_optab))
3310 : : {
3311 : : /* Output the entire sequence. */
3312 : 24 : insns = end_sequence ();
3313 : 24 : emit_insn (insns);
3314 : 24 : return result;
3315 : : }
3316 : :
3317 : : /* If we were unable to expand via the builtin, stop the sequence
3318 : : (without outputting the insns) and call to the library function
3319 : : with the stabilized argument list. */
3320 : 235 : end_sequence ();
3321 : : }
3322 : :
3323 : 235 : if (fallback_fn != BUILT_IN_NONE)
3324 : : {
3325 : : /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3326 : : targets, (int) round (x) should never be transformed into
3327 : : BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3328 : : a call to lround in the hope that the target provides at least some
3329 : : C99 functions. This should result in the best user experience for
3330 : : not full C99 targets.
3331 : : As scalar float conversions with same mode are useless in GIMPLE,
3332 : : we can end up e.g. with _Float32 argument passed to float builtin,
3333 : : try to get the type from the builtin prototype first. */
3334 : 221 : tree fallback_fndecl = NULL_TREE;
3335 : 221 : if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3336 : 221 : fallback_fndecl
3337 : 221 : = mathfn_built_in_1 (TREE_VALUE (argtypes),
3338 : : as_combined_fn (fallback_fn), 0);
3339 : 221 : if (fallback_fndecl == NULL_TREE)
3340 : 0 : fallback_fndecl
3341 : 0 : = mathfn_built_in_1 (TREE_TYPE (arg),
3342 : : as_combined_fn (fallback_fn), 0);
3343 : 0 : if (fallback_fndecl)
3344 : : {
3345 : 221 : exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3346 : : fallback_fndecl, 1, arg);
3347 : :
3348 : 221 : target = expand_call (exp, NULL_RTX, target == const0_rtx);
3349 : 221 : target = maybe_emit_group_store (target, TREE_TYPE (exp));
3350 : 221 : return convert_to_mode (mode, target, 0);
3351 : : }
3352 : : }
3353 : :
3354 : 14 : return expand_call (exp, target, target == const0_rtx);
3355 : : }
3356 : :
3357 : : /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3358 : : a normal call should be emitted rather than expanding the function
3359 : : in-line. EXP is the expression that is a call to the builtin
3360 : : function; if convenient, the result should be placed in TARGET. */
3361 : :
3362 : : static rtx
3363 : 279 : expand_builtin_powi (tree exp, rtx target)
3364 : : {
3365 : 279 : tree arg0, arg1;
3366 : 279 : rtx op0, op1;
3367 : 279 : machine_mode mode;
3368 : 279 : machine_mode mode2;
3369 : :
3370 : 279 : if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3371 : : return NULL_RTX;
3372 : :
3373 : 279 : arg0 = CALL_EXPR_ARG (exp, 0);
3374 : 279 : arg1 = CALL_EXPR_ARG (exp, 1);
3375 : 279 : mode = TYPE_MODE (TREE_TYPE (exp));
3376 : :
3377 : : /* Emit a libcall to libgcc. */
3378 : :
3379 : : /* Mode of the 2nd argument must match that of an int. */
3380 : 279 : mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3381 : :
3382 : 279 : if (target == NULL_RTX)
3383 : 0 : target = gen_reg_rtx (mode);
3384 : :
3385 : 279 : op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3386 : 279 : if (GET_MODE (op0) != mode)
3387 : 0 : op0 = convert_to_mode (mode, op0, 0);
3388 : 279 : op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3389 : 279 : if (GET_MODE (op1) != mode2)
3390 : 39 : op1 = convert_to_mode (mode2, op1, 0);
3391 : :
3392 : 279 : target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3393 : : target, LCT_CONST, mode,
3394 : : op0, mode, op1, mode2);
3395 : :
3396 : 279 : return target;
3397 : : }
3398 : :
3399 : : /* Expand expression EXP which is a call to the strlen builtin. Return
3400 : : NULL_RTX if we failed and the caller should emit a normal call, otherwise
3401 : : try to get the result in TARGET, if convenient. */
3402 : :
3403 : : static rtx
3404 : 13727 : expand_builtin_strlen (tree exp, rtx target,
3405 : : machine_mode target_mode)
3406 : : {
3407 : 13727 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3408 : : return NULL_RTX;
3409 : :
3410 : 13707 : tree src = CALL_EXPR_ARG (exp, 0);
3411 : :
3412 : : /* If the length can be computed at compile-time, return it. */
3413 : 13707 : if (tree len = c_strlen (src, 0))
3414 : 177 : 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 : 13530 : tree len = c_strlen (src, 1);
3422 : 13530 : 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 : 13530 : 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 : 13530 : 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 : 13530 : FOR_EACH_MODE_FROM (insn_mode, target_mode)
3438 : : {
3439 : 13530 : icode = optab_handler (strlen_optab, insn_mode);
3440 : 13530 : if (icode != CODE_FOR_nothing)
3441 : : break;
3442 : : }
3443 : 13530 : 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 : 14104 : 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 : 13530 : rtx_insn *before_strlen = get_last_insn ();
3454 : :
3455 : 13530 : class expand_operand ops[4];
3456 : 13530 : create_output_operand (&ops[0], target, insn_mode);
3457 : 13530 : create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3458 : 13530 : create_integer_operand (&ops[2], 0);
3459 : 13530 : create_integer_operand (&ops[3], align);
3460 : 13530 : if (!maybe_expand_insn (icode, 4, ops))
3461 : : return NULL_RTX;
3462 : :
3463 : : /* Check to see if the argument was declared attribute nonstring
3464 : : and if so, issue a warning since at this point it's not known
3465 : : to be nul-terminated. */
3466 : 11 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3467 : :
3468 : : /* Now that we are assured of success, expand the source. */
3469 : 11 : start_sequence ();
3470 : 15 : rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3471 : 11 : if (pat != src_reg)
3472 : : {
3473 : : #ifdef POINTERS_EXTEND_UNSIGNED
3474 : 15 : if (GET_MODE (pat) != Pmode)
3475 : 0 : pat = convert_to_mode (Pmode, pat,
3476 : : POINTERS_EXTEND_UNSIGNED);
3477 : : #endif
3478 : 11 : emit_move_insn (src_reg, pat);
3479 : : }
3480 : 11 : pat = end_sequence ();
3481 : :
3482 : 11 : if (before_strlen)
3483 : 11 : emit_insn_after (pat, before_strlen);
3484 : : else
3485 : 0 : emit_insn_before (pat, get_insns ());
3486 : :
3487 : : /* Return the value in the proper mode for this function. */
3488 : 11 : if (GET_MODE (ops[0].value) == target_mode)
3489 : : target = ops[0].value;
3490 : 0 : else if (target != 0)
3491 : 0 : convert_move (target, ops[0].value, 0);
3492 : : else
3493 : 0 : target = convert_to_mode (target_mode, ops[0].value, 0);
3494 : :
3495 : : return target;
3496 : : }
3497 : :
3498 : : /* Expand call EXP to the strnlen built-in, returning the result
3499 : : and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3500 : :
3501 : : static rtx
3502 : 580 : expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3503 : : {
3504 : 580 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3505 : : return NULL_RTX;
3506 : :
3507 : 571 : tree src = CALL_EXPR_ARG (exp, 0);
3508 : 571 : tree bound = CALL_EXPR_ARG (exp, 1);
3509 : :
3510 : 571 : if (!bound)
3511 : : return NULL_RTX;
3512 : :
3513 : 571 : location_t loc = UNKNOWN_LOCATION;
3514 : 571 : if (EXPR_HAS_LOCATION (exp))
3515 : 571 : loc = EXPR_LOCATION (exp);
3516 : :
3517 : : /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3518 : : so these conversions aren't necessary. */
3519 : 571 : c_strlen_data lendata = { };
3520 : 571 : tree len = c_strlen (src, 0, &lendata, 1);
3521 : 571 : if (len)
3522 : 102 : len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3523 : :
3524 : 571 : if (TREE_CODE (bound) == INTEGER_CST)
3525 : : {
3526 : 331 : if (!len)
3527 : : return NULL_RTX;
3528 : :
3529 : 62 : len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3530 : 62 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3531 : : }
3532 : :
3533 : 240 : if (TREE_CODE (bound) != SSA_NAME)
3534 : : return NULL_RTX;
3535 : :
3536 : 240 : wide_int min, max;
3537 : 240 : int_range_max r;
3538 : 480 : get_range_query (cfun)->range_of_expr (r, bound,
3539 : : currently_expanding_gimple_stmt);
3540 : 240 : if (r.varying_p () || r.undefined_p ())
3541 : : return NULL_RTX;
3542 : 135 : min = r.lower_bound ();
3543 : 135 : max = r.upper_bound ();
3544 : :
3545 : 135 : if (!len || TREE_CODE (len) != INTEGER_CST)
3546 : : {
3547 : 95 : bool exact;
3548 : 95 : lendata.decl = unterminated_array (src, &len, &exact);
3549 : 95 : if (!lendata.decl)
3550 : 75 : return NULL_RTX;
3551 : : }
3552 : :
3553 : 60 : if (lendata.decl)
3554 : : return NULL_RTX;
3555 : :
3556 : 40 : if (wi::gtu_p (min, wi::to_wide (len)))
3557 : 7 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3558 : :
3559 : 33 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3560 : 33 : return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3561 : 240 : }
3562 : :
3563 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3564 : : bytes from bytes at DATA + OFFSET and return it reinterpreted as
3565 : : a target constant. */
3566 : :
3567 : : static rtx
3568 : 159492 : 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 : 159492 : const char *rep = (const char *) data;
3574 : :
3575 : 159492 : 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 : 144063 : 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 : 144063 : if (CONST_INT_P (len_rtx))
3590 : : {
3591 : 64347 : *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3592 : 64347 : return;
3593 : : }
3594 : : else
3595 : : {
3596 : 79716 : wide_int min, max;
3597 : 79716 : enum value_range_kind range_type = VR_UNDEFINED;
3598 : :
3599 : : /* Determine bounds from the type. */
3600 : 79716 : if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3601 : 79715 : *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3602 : : else
3603 : 1 : *min_size = 0;
3604 : 79716 : if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3605 : 159432 : *probable_max_size = *max_size
3606 : 79716 : = 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 : 79716 : if (TREE_CODE (len) == SSA_NAME)
3611 : : {
3612 : 79716 : int_range_max r;
3613 : 79716 : tree tmin, tmax;
3614 : 79716 : gimple *cg = currently_expanding_gimple_stmt;
3615 : 159432 : get_range_query (cfun)->range_of_expr (r, len, cg);
3616 : 79716 : range_type = get_legacy_range (r, tmin, tmax);
3617 : 79716 : if (range_type != VR_UNDEFINED)
3618 : : {
3619 : 79716 : min = wi::to_wide (tmin);
3620 : 79716 : max = wi::to_wide (tmax);
3621 : : }
3622 : 79716 : }
3623 : 79716 : if (range_type == VR_RANGE)
3624 : : {
3625 : 60490 : if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3626 : 49177 : *min_size = min.to_uhwi ();
3627 : 60490 : if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3628 : 50912 : *probable_max_size = *max_size = max.to_uhwi ();
3629 : : }
3630 : 19226 : else if (range_type == VR_ANTI_RANGE)
3631 : : {
3632 : : /* Code like
3633 : :
3634 : : int n;
3635 : : if (n < 100)
3636 : : memcpy (a, b, n)
3637 : :
3638 : : Produce anti range allowing negative values of N. We still
3639 : : can use the information and make a guess that N is not negative.
3640 : : */
3641 : 3643 : if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3642 : 3564 : *probable_max_size = min.to_uhwi () - 1;
3643 : : }
3644 : 79716 : }
3645 : 79716 : gcc_checking_assert (*max_size <=
3646 : : (unsigned HOST_WIDE_INT)
3647 : : GET_MODE_MASK (GET_MODE (len_rtx)));
3648 : : }
3649 : :
3650 : : /* Expand a call EXP to the memcpy builtin.
3651 : : Return NULL_RTX if we failed, the caller should emit a normal call,
3652 : : otherwise try to get the result in TARGET, if convenient (and in
3653 : : mode MODE if that's convenient). */
3654 : :
3655 : : static rtx
3656 : 90506 : expand_builtin_memcpy (tree exp, rtx target)
3657 : : {
3658 : 90506 : if (!validate_arglist (exp,
3659 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3660 : : return NULL_RTX;
3661 : :
3662 : 90488 : tree dest = CALL_EXPR_ARG (exp, 0);
3663 : 90488 : tree src = CALL_EXPR_ARG (exp, 1);
3664 : 90488 : tree len = CALL_EXPR_ARG (exp, 2);
3665 : :
3666 : 90488 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3667 : 90488 : /*retmode=*/ RETURN_BEGIN, false);
3668 : : }
3669 : :
3670 : : /* Check a call EXP to the memmove built-in for validity.
3671 : : Return NULL_RTX on both success and failure. */
3672 : :
3673 : : static rtx
3674 : 16077 : expand_builtin_memmove (tree exp, rtx target)
3675 : : {
3676 : 16077 : if (!validate_arglist (exp,
3677 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3678 : : return NULL_RTX;
3679 : :
3680 : 16062 : tree dest = CALL_EXPR_ARG (exp, 0);
3681 : 16062 : tree src = CALL_EXPR_ARG (exp, 1);
3682 : 16062 : tree len = CALL_EXPR_ARG (exp, 2);
3683 : :
3684 : 16062 : return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3685 : 16062 : /*retmode=*/ RETURN_BEGIN, true);
3686 : : }
3687 : :
3688 : : /* Expand a call EXP to the mempcpy builtin.
3689 : : Return NULL_RTX if we failed; the caller should emit a normal call,
3690 : : otherwise try to get the result in TARGET, if convenient (and in
3691 : : mode MODE if that's convenient). */
3692 : :
3693 : : static rtx
3694 : 1637 : expand_builtin_mempcpy (tree exp, rtx target)
3695 : : {
3696 : 1637 : if (!validate_arglist (exp,
3697 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3698 : : return NULL_RTX;
3699 : :
3700 : 1632 : tree dest = CALL_EXPR_ARG (exp, 0);
3701 : 1632 : tree src = CALL_EXPR_ARG (exp, 1);
3702 : 1632 : tree len = CALL_EXPR_ARG (exp, 2);
3703 : :
3704 : : /* Policy does not generally allow using compute_objsize (which
3705 : : is used internally by check_memop_size) to change code generation
3706 : : or drive optimization decisions.
3707 : :
3708 : : In this instance it is safe because the code we generate has
3709 : : the same semantics regardless of the return value of
3710 : : check_memop_sizes. Exactly the same amount of data is copied
3711 : : and the return value is exactly the same in both cases.
3712 : :
3713 : : Furthermore, check_memop_size always uses mode 0 for the call to
3714 : : compute_objsize, so the imprecise nature of compute_objsize is
3715 : : avoided. */
3716 : :
3717 : : /* Avoid expanding mempcpy into memcpy when the call is determined
3718 : : to overflow the buffer. This also prevents the same overflow
3719 : : from being diagnosed again when expanding memcpy. */
3720 : :
3721 : 1632 : return expand_builtin_mempcpy_args (dest, src, len,
3722 : 1632 : target, exp, /*retmode=*/ RETURN_END);
3723 : : }
3724 : :
3725 : : /* Helper function to do the actual work for expand of memory copy family
3726 : : functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3727 : : of memory from SRC to DEST and assign to TARGET if convenient. Return
3728 : : value is based on RETMODE argument. */
3729 : :
3730 : : static rtx
3731 : 108205 : expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3732 : : rtx target, tree exp, memop_ret retmode,
3733 : : bool might_overlap)
3734 : : {
3735 : 108205 : unsigned int src_align = get_pointer_alignment (src);
3736 : 108205 : unsigned int dest_align = get_pointer_alignment (dest);
3737 : 108205 : rtx dest_mem, src_mem, dest_addr, len_rtx;
3738 : 108205 : HOST_WIDE_INT expected_size = -1;
3739 : 108205 : unsigned int expected_align = 0;
3740 : 108205 : unsigned HOST_WIDE_INT min_size;
3741 : 108205 : unsigned HOST_WIDE_INT max_size;
3742 : 108205 : unsigned HOST_WIDE_INT probable_max_size;
3743 : :
3744 : 108205 : bool is_move_done;
3745 : :
3746 : : /* If DEST is not a pointer type, call the normal function. */
3747 : 108205 : if (dest_align == 0)
3748 : : return NULL_RTX;
3749 : :
3750 : : /* If either SRC is not a pointer type, don't do this
3751 : : operation in-line. */
3752 : 108205 : if (src_align == 0)
3753 : : return NULL_RTX;
3754 : :
3755 : 108205 : if (currently_expanding_gimple_stmt)
3756 : 108205 : stringop_block_profile (currently_expanding_gimple_stmt,
3757 : : &expected_align, &expected_size);
3758 : :
3759 : 108205 : if (expected_align < dest_align)
3760 : 108192 : expected_align = dest_align;
3761 : 108205 : dest_mem = get_memory_rtx (dest, len);
3762 : 108205 : set_mem_align (dest_mem, dest_align);
3763 : 108205 : len_rtx = expand_normal (len);
3764 : 108205 : determine_block_size (len, len_rtx, &min_size, &max_size,
3765 : : &probable_max_size);
3766 : :
3767 : : /* Try to get the byte representation of the constant SRC points to,
3768 : : with its byte size in NBYTES. */
3769 : 108205 : unsigned HOST_WIDE_INT nbytes;
3770 : 108205 : const char *rep = getbyterep (src, &nbytes);
3771 : :
3772 : : /* If the function's constant bound LEN_RTX is less than or equal
3773 : : to the byte size of the representation of the constant argument,
3774 : : and if block move would be done by pieces, we can avoid loading
3775 : : the bytes from memory and only store the computed constant.
3776 : : This works in the overlap (memmove) case as well because
3777 : : store_by_pieces just generates a series of stores of constants
3778 : : from the representation returned by getbyterep(). */
3779 : 108205 : if (rep
3780 : 29160 : && CONST_INT_P (len_rtx)
3781 : 27643 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3782 : 135848 : && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3783 : : CONST_CAST (char *, rep),
3784 : : dest_align, false))
3785 : : {
3786 : 27183 : dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3787 : : builtin_memcpy_read_str,
3788 : : CONST_CAST (char *, rep),
3789 : : dest_align, false, retmode);
3790 : 27183 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
3791 : 27183 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
3792 : 27183 : return dest_mem;
3793 : : }
3794 : :
3795 : 81022 : src_mem = get_memory_rtx (src, len);
3796 : 81022 : set_mem_align (src_mem, src_align);
3797 : :
3798 : : /* Copy word part most expediently. */
3799 : 81022 : enum block_op_methods method = BLOCK_OP_NORMAL;
3800 : 81022 : if (CALL_EXPR_TAILCALL (exp)
3801 : 81022 : && (retmode == RETURN_BEGIN || target == const0_rtx))
3802 : : method = BLOCK_OP_TAILCALL;
3803 : 81022 : bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3804 : 81022 : && retmode == RETURN_END
3805 : 81022 : && !might_overlap
3806 : 82324 : && target != const0_rtx);
3807 : : if (use_mempcpy_call)
3808 : 81022 : method = BLOCK_OP_NO_LIBCALL_RET;
3809 : 81022 : dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3810 : : expected_align, expected_size,
3811 : : min_size, max_size, probable_max_size,
3812 : : use_mempcpy_call, &is_move_done,
3813 : : might_overlap, tree_ctz (len));
3814 : :
3815 : : /* Bail out when a mempcpy call would be expanded as libcall and when
3816 : : we have a target that provides a fast implementation
3817 : : of mempcpy routine. */
3818 : 81022 : if (!is_move_done)
3819 : : return NULL_RTX;
3820 : :
3821 : 65168 : if (dest_addr == pc_rtx)
3822 : : return NULL_RTX;
3823 : :
3824 : 65168 : if (dest_addr == 0)
3825 : : {
3826 : 25853 : dest_addr = force_operand (XEXP (dest_mem, 0), target);
3827 : 25853 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
3828 : : }
3829 : :
3830 : 65168 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3831 : : {
3832 : 808 : dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3833 : : /* stpcpy pointer to last byte. */
3834 : 808 : if (retmode == RETURN_END_MINUS_ONE)
3835 : 0 : dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3836 : : }
3837 : :
3838 : : return dest_addr;
3839 : : }
3840 : :
3841 : : static rtx
3842 : 1655 : expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3843 : : rtx target, tree orig_exp, memop_ret retmode)
3844 : : {
3845 : 1632 : return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3846 : 0 : retmode, false);
3847 : : }
3848 : :
3849 : : /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3850 : : we failed, the caller should emit a normal call, otherwise try to
3851 : : get the result in TARGET, if convenient.
3852 : : Return value is based on RETMODE argument. */
3853 : :
3854 : : static rtx
3855 : 2269 : expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3856 : : {
3857 : 2269 : class expand_operand ops[3];
3858 : 2269 : rtx dest_mem;
3859 : 2269 : rtx src_mem;
3860 : :
3861 : 2269 : if (!targetm.have_movstr ())
3862 : : return NULL_RTX;
3863 : :
3864 : 0 : dest_mem = get_memory_rtx (dest, NULL);
3865 : 0 : src_mem = get_memory_rtx (src, NULL);
3866 : 0 : if (retmode == RETURN_BEGIN)
3867 : : {
3868 : 0 : target = force_reg (Pmode, XEXP (dest_mem, 0));
3869 : 0 : dest_mem = replace_equiv_address (dest_mem, target);
3870 : : }
3871 : :
3872 : 0 : create_output_operand (&ops[0],
3873 : 0 : retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3874 : 0 : create_fixed_operand (&ops[1], dest_mem);
3875 : 0 : create_fixed_operand (&ops[2], src_mem);
3876 : 0 : if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3877 : : return NULL_RTX;
3878 : :
3879 : 0 : if (retmode != RETURN_BEGIN && target != const0_rtx)
3880 : : {
3881 : 0 : target = ops[0].value;
3882 : : /* movstr is supposed to set end to the address of the NUL
3883 : : terminator. If the caller requested a mempcpy-like return value,
3884 : : adjust it. */
3885 : 0 : if (retmode == RETURN_END)
3886 : : {
3887 : 0 : rtx tem = plus_constant (GET_MODE (target),
3888 : 0 : gen_lowpart (GET_MODE (target), target), 1);
3889 : 0 : emit_move_insn (target, force_operand (tem, NULL_RTX));
3890 : : }
3891 : : }
3892 : : return target;
3893 : : }
3894 : :
3895 : : /* Expand expression EXP, which is a call to the strcpy builtin. Return
3896 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
3897 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
3898 : : convenient). */
3899 : :
3900 : : static rtx
3901 : 1862 : expand_builtin_strcpy (tree exp, rtx target)
3902 : : {
3903 : 1862 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3904 : : return NULL_RTX;
3905 : :
3906 : 1847 : tree dest = CALL_EXPR_ARG (exp, 0);
3907 : 1847 : tree src = CALL_EXPR_ARG (exp, 1);
3908 : :
3909 : 1847 : return expand_builtin_strcpy_args (exp, dest, src, target);
3910 : : }
3911 : :
3912 : : /* Helper function to do the actual work for expand_builtin_strcpy. The
3913 : : arguments to the builtin_strcpy call DEST and SRC are broken out
3914 : : so that this can also be called without constructing an actual CALL_EXPR.
3915 : : The other arguments and return value are the same as for
3916 : : expand_builtin_strcpy. */
3917 : :
3918 : : static rtx
3919 : 1847 : expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3920 : : {
3921 : 1847 : return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3922 : : }
3923 : :
3924 : : /* Expand a call EXP to the stpcpy builtin.
3925 : : Return NULL_RTX if we failed the caller should emit a normal call,
3926 : : otherwise try to get the result in TARGET, if convenient (and in
3927 : : mode MODE if that's convenient). */
3928 : :
3929 : : static rtx
3930 : 454 : expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3931 : : {
3932 : 454 : tree dst, src;
3933 : 454 : location_t loc = EXPR_LOCATION (exp);
3934 : :
3935 : 454 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3936 : : return NULL_RTX;
3937 : :
3938 : 445 : dst = CALL_EXPR_ARG (exp, 0);
3939 : 445 : src = CALL_EXPR_ARG (exp, 1);
3940 : :
3941 : : /* If return value is ignored, transform stpcpy into strcpy. */
3942 : 445 : if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3943 : : {
3944 : 0 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3945 : 0 : tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3946 : 0 : return expand_expr (result, target, mode, EXPAND_NORMAL);
3947 : : }
3948 : : else
3949 : : {
3950 : 445 : tree len, lenp1;
3951 : 445 : rtx ret;
3952 : :
3953 : : /* Ensure we get an actual string whose length can be evaluated at
3954 : : compile-time, not an expression containing a string. This is
3955 : : because the latter will potentially produce pessimized code
3956 : : when used to produce the return value. */
3957 : 445 : c_strlen_data lendata = { };
3958 : 445 : if (!c_getstr (src)
3959 : 445 : || !(len = c_strlen (src, 0, &lendata, 1)))
3960 : 422 : return expand_movstr (dst, src, target,
3961 : 422 : /*retmode=*/ RETURN_END_MINUS_ONE);
3962 : :
3963 : 23 : lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3964 : 23 : ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3965 : : target, exp,
3966 : : /*retmode=*/ RETURN_END_MINUS_ONE);
3967 : :
3968 : 23 : if (ret)
3969 : : return ret;
3970 : :
3971 : 0 : if (TREE_CODE (len) == INTEGER_CST)
3972 : : {
3973 : 0 : rtx len_rtx = expand_normal (len);
3974 : :
3975 : 0 : if (CONST_INT_P (len_rtx))
3976 : : {
3977 : 0 : ret = expand_builtin_strcpy_args (exp, dst, src, target);
3978 : :
3979 : 0 : if (ret)
3980 : : {
3981 : 0 : if (! target)
3982 : : {
3983 : 0 : if (mode != VOIDmode)
3984 : 0 : target = gen_reg_rtx (mode);
3985 : : else
3986 : 0 : target = gen_reg_rtx (GET_MODE (ret));
3987 : : }
3988 : 0 : if (GET_MODE (target) != GET_MODE (ret))
3989 : 0 : ret = gen_lowpart (GET_MODE (target), ret);
3990 : :
3991 : 0 : ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3992 : 0 : ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3993 : 0 : gcc_assert (ret);
3994 : :
3995 : : return target;
3996 : : }
3997 : : }
3998 : : }
3999 : :
4000 : 0 : return expand_movstr (dst, src, target,
4001 : 0 : /*retmode=*/ RETURN_END_MINUS_ONE);
4002 : : }
4003 : : }
4004 : :
4005 : : /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
4006 : : arguments while being careful to avoid duplicate warnings (which could
4007 : : be issued if the expander were to expand the call, resulting in it
4008 : : being emitted in expand_call(). */
4009 : :
4010 : : static rtx
4011 : 454 : expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
4012 : : {
4013 : 454 : if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
4014 : : {
4015 : : /* The call has been successfully expanded. Check for nonstring
4016 : : arguments and issue warnings as appropriate. */
4017 : 23 : maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
4018 : 23 : return ret;
4019 : : }
4020 : :
4021 : : return NULL_RTX;
4022 : : }
4023 : :
4024 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4025 : : bytes from constant string DATA + OFFSET and return it as target
4026 : : constant. */
4027 : :
4028 : : rtx
4029 : 9310 : builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
4030 : : fixed_size_mode mode)
4031 : : {
4032 : 9310 : const char *str = (const char *) data;
4033 : :
4034 : 9310 : if ((unsigned HOST_WIDE_INT) offset > strlen (str))
4035 : 260 : return const0_rtx;
4036 : :
4037 : 9050 : return c_readstr (str + offset, mode);
4038 : : }
4039 : :
4040 : : /* Helper to check the sizes of sequences and the destination of calls
4041 : : to __builtin_strncat and __builtin___strncat_chk. Returns true on
4042 : : success (no overflow or invalid sizes), false otherwise. */
4043 : :
4044 : : static bool
4045 : 109 : check_strncat_sizes (tree exp, tree objsize)
4046 : : {
4047 : 109 : tree dest = CALL_EXPR_ARG (exp, 0);
4048 : 109 : tree src = CALL_EXPR_ARG (exp, 1);
4049 : 109 : tree maxread = CALL_EXPR_ARG (exp, 2);
4050 : :
4051 : : /* Try to determine the range of lengths that the source expression
4052 : : refers to. */
4053 : 109 : c_strlen_data lendata = { };
4054 : 109 : get_range_strlen (src, &lendata, /* eltsize = */ 1);
4055 : :
4056 : : /* Try to verify that the destination is big enough for the shortest
4057 : : string. */
4058 : :
4059 : 109 : access_data data (nullptr, exp, access_read_write, maxread, true);
4060 : 109 : if (!objsize && warn_stringop_overflow)
4061 : : {
4062 : : /* If it hasn't been provided by __strncat_chk, try to determine
4063 : : the size of the destination object into which the source is
4064 : : being copied. */
4065 : 0 : objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4066 : : }
4067 : :
4068 : : /* Add one for the terminating nul. */
4069 : 109 : tree srclen = (lendata.minlen
4070 : 109 : ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4071 : : size_one_node)
4072 : : : NULL_TREE);
4073 : :
4074 : : /* The strncat function copies at most MAXREAD bytes and always appends
4075 : : the terminating nul so the specified upper bound should never be equal
4076 : : to (or greater than) the size of the destination. */
4077 : 56 : if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4078 : 157 : && tree_int_cst_equal (objsize, maxread))
4079 : : {
4080 : 23 : location_t loc = EXPR_LOCATION (exp);
4081 : 23 : warning_at (loc, OPT_Wstringop_overflow_,
4082 : : "%qD specified bound %E equals destination size",
4083 : : get_callee_fndecl (exp), maxread);
4084 : :
4085 : 23 : return false;
4086 : : }
4087 : :
4088 : 86 : if (!srclen
4089 : 86 : || (maxread && tree_fits_uhwi_p (maxread)
4090 : 33 : && tree_fits_uhwi_p (srclen)
4091 : 33 : && tree_int_cst_lt (maxread, srclen)))
4092 : : srclen = maxread;
4093 : :
4094 : : /* The number of bytes to write is LEN but check_access will alsoa
4095 : : check SRCLEN if LEN's value isn't known. */
4096 : 86 : return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4097 : 86 : objsize, data.mode, &data);
4098 : : }
4099 : :
4100 : : /* Expand expression EXP, which is a call to the strncpy builtin. Return
4101 : : NULL_RTX if we failed the caller should emit a normal call. */
4102 : :
4103 : : static rtx
4104 : 2168 : expand_builtin_strncpy (tree exp, rtx target)
4105 : : {
4106 : 2168 : location_t loc = EXPR_LOCATION (exp);
4107 : :
4108 : 2168 : if (!validate_arglist (exp,
4109 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4110 : : return NULL_RTX;
4111 : 2156 : tree dest = CALL_EXPR_ARG (exp, 0);
4112 : 2156 : tree src = CALL_EXPR_ARG (exp, 1);
4113 : : /* The number of bytes to write (not the maximum). */
4114 : 2156 : tree len = CALL_EXPR_ARG (exp, 2);
4115 : :
4116 : : /* The length of the source sequence. */
4117 : 2156 : tree slen = c_strlen (src, 1);
4118 : :
4119 : : /* We must be passed a constant len and src parameter. */
4120 : 2156 : if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4121 : : return NULL_RTX;
4122 : :
4123 : 237 : slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4124 : :
4125 : : /* We're required to pad with trailing zeros if the requested
4126 : : len is greater than strlen(s2)+1. In that case try to
4127 : : use store_by_pieces, if it fails, punt. */
4128 : 237 : if (tree_int_cst_lt (slen, len))
4129 : : {
4130 : 142 : unsigned int dest_align = get_pointer_alignment (dest);
4131 : 142 : const char *p = c_getstr (src);
4132 : 142 : rtx dest_mem;
4133 : :
4134 : 136 : if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4135 : 278 : || !can_store_by_pieces (tree_to_uhwi (len),
4136 : : builtin_strncpy_read_str,
4137 : : CONST_CAST (char *, p),
4138 : : dest_align, false))
4139 : 14 : return NULL_RTX;
4140 : :
4141 : 128 : dest_mem = get_memory_rtx (dest, len);
4142 : 128 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4143 : : builtin_strncpy_read_str,
4144 : : CONST_CAST (char *, p), dest_align, false,
4145 : : RETURN_BEGIN);
4146 : 128 : dest_mem = force_operand (XEXP (dest_mem, 0), target);
4147 : 128 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4148 : 128 : return dest_mem;
4149 : : }
4150 : :
4151 : : return NULL_RTX;
4152 : : }
4153 : :
4154 : : /* Return the RTL of a register in MODE generated from PREV in the
4155 : : previous iteration. */
4156 : :
4157 : : static rtx
4158 : 198951 : gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4159 : : {
4160 : 198951 : rtx target = nullptr;
4161 : 198951 : if (prev != nullptr && prev->data != nullptr)
4162 : : {
4163 : : /* Use the previous data in the same mode. */
4164 : 113105 : if (prev->mode == mode)
4165 : 198951 : return prev->data;
4166 : :
4167 : 29966 : fixed_size_mode prev_mode = prev->mode;
4168 : :
4169 : : /* Don't use the previous data to write QImode if it is in a
4170 : : vector mode. */
4171 : 29966 : if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4172 : : return target;
4173 : :
4174 : 29703 : rtx prev_rtx = prev->data;
4175 : :
4176 : 29703 : if (REG_P (prev_rtx)
4177 : 18833 : && HARD_REGISTER_P (prev_rtx)
4178 : 29703 : && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4179 : : {
4180 : : /* This case occurs when PREV_MODE is a vector and when
4181 : : MODE is too small to store using vector operations.
4182 : : After register allocation, the code will need to move the
4183 : : lowpart of the vector register into a non-vector register.
4184 : :
4185 : : Also, the target has chosen to use a hard register
4186 : : instead of going with the default choice of using a
4187 : : pseudo register. We should respect that choice and try to
4188 : : avoid creating a pseudo register with the same mode as the
4189 : : current hard register.
4190 : :
4191 : : In principle, we could just use a lowpart MODE subreg of
4192 : : the vector register. However, the vector register mode might
4193 : : be too wide for non-vector registers, and we already know
4194 : : that the non-vector mode is too small for vector registers.
4195 : : It's therefore likely that we'd need to spill to memory in
4196 : : the vector mode and reload the non-vector value from there.
4197 : :
4198 : : Try to avoid that by reducing the vector register to the
4199 : : smallest size that it can hold. This should increase the
4200 : : chances that non-vector registers can hold both the inner
4201 : : and outer modes of the subreg that we generate later. */
4202 : 0 : machine_mode m;
4203 : 0 : fixed_size_mode candidate;
4204 : 0 : FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4205 : 0 : if (is_a<fixed_size_mode> (m, &candidate))
4206 : : {
4207 : 0 : if (GET_MODE_SIZE (candidate)
4208 : 0 : >= GET_MODE_SIZE (prev_mode))
4209 : : break;
4210 : 0 : if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4211 : 0 : && lowpart_subreg_regno (REGNO (prev_rtx),
4212 : : prev_mode, candidate) >= 0)
4213 : : {
4214 : 0 : target = lowpart_subreg (candidate, prev_rtx,
4215 : : prev_mode);
4216 : 0 : prev_rtx = target;
4217 : 0 : prev_mode = candidate;
4218 : 0 : break;
4219 : : }
4220 : : }
4221 : 0 : if (target == nullptr)
4222 : 0 : prev_rtx = copy_to_reg (prev_rtx);
4223 : : }
4224 : :
4225 : 29703 : target = lowpart_subreg (mode, prev_rtx, prev_mode);
4226 : : }
4227 : : return target;
4228 : : }
4229 : :
4230 : : /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4231 : : bytes from constant string DATA + OFFSET and return it as target
4232 : : constant. If PREV isn't nullptr, it has the RTL info from the
4233 : : previous iteration. */
4234 : :
4235 : : rtx
4236 : 197641 : builtin_memset_read_str (void *data, void *prev,
4237 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4238 : : fixed_size_mode mode)
4239 : : {
4240 : 197641 : const char *c = (const char *) data;
4241 : 197641 : unsigned int size = GET_MODE_SIZE (mode);
4242 : :
4243 : 197641 : rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4244 : : mode);
4245 : 197641 : if (target != nullptr)
4246 : : return target;
4247 : 85379 : rtx src = gen_int_mode (*c, QImode);
4248 : :
4249 : 85379 : if (VECTOR_MODE_P (mode))
4250 : : {
4251 : 114908 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4252 : :
4253 : 57454 : rtx const_vec = gen_const_vec_duplicate (mode, src);
4254 : 57454 : if (prev == NULL)
4255 : : /* Return CONST_VECTOR when called by a query function. */
4256 : : return const_vec;
4257 : :
4258 : : /* Use the move expander with CONST_VECTOR. */
4259 : 41553 : target = gen_reg_rtx (mode);
4260 : 41553 : emit_move_insn (target, const_vec);
4261 : 41553 : return target;
4262 : : }
4263 : :
4264 : 27925 : char *p = XALLOCAVEC (char, size);
4265 : :
4266 : 27925 : memset (p, *c, size);
4267 : :
4268 : 27925 : return c_readstr (p, mode);
4269 : : }
4270 : :
4271 : : /* Callback routine for store_by_pieces. Return the RTL of a register
4272 : : containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4273 : : char value given in the RTL register data. For example, if mode is
4274 : : 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4275 : : nullptr, it has the RTL info from the previous iteration. */
4276 : :
4277 : : static rtx
4278 : 1479 : builtin_memset_gen_str (void *data, void *prev,
4279 : : HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4280 : : fixed_size_mode mode)
4281 : : {
4282 : 1479 : rtx target, coeff;
4283 : 1479 : size_t size;
4284 : 1479 : char *p;
4285 : :
4286 : 1479 : size = GET_MODE_SIZE (mode);
4287 : 1479 : if (size == 1)
4288 : : return (rtx) data;
4289 : :
4290 : 1310 : target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4291 : 1310 : if (target != nullptr)
4292 : : return target;
4293 : :
4294 : 730 : if (VECTOR_MODE_P (mode))
4295 : : {
4296 : 448 : gcc_assert (GET_MODE_INNER (mode) == QImode);
4297 : :
4298 : : /* vec_duplicate_optab is a precondition to pick a vector mode for
4299 : : the memset expander. */
4300 : 224 : insn_code icode = optab_handler (vec_duplicate_optab, mode);
4301 : :
4302 : 224 : target = gen_reg_rtx (mode);
4303 : 224 : class expand_operand ops[2];
4304 : 224 : create_output_operand (&ops[0], target, mode);
4305 : 224 : create_input_operand (&ops[1], (rtx) data, QImode);
4306 : 224 : expand_insn (icode, 2, ops);
4307 : 224 : if (!rtx_equal_p (target, ops[0].value))
4308 : 0 : emit_move_insn (target, ops[0].value);
4309 : :
4310 : 224 : return target;
4311 : : }
4312 : :
4313 : 506 : p = XALLOCAVEC (char, size);
4314 : 506 : memset (p, 1, size);
4315 : 506 : coeff = c_readstr (p, mode);
4316 : :
4317 : 506 : target = convert_to_mode (mode, (rtx) data, 1);
4318 : 506 : target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4319 : 506 : return force_reg (mode, target);
4320 : : }
4321 : :
4322 : : /* Expand expression EXP, which is a call to the memset builtin. Return
4323 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
4324 : : try to get the result in TARGET, if convenient (and in mode MODE if that's
4325 : : convenient). */
4326 : :
4327 : : rtx
4328 : 35867 : expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4329 : : {
4330 : 35867 : if (!validate_arglist (exp,
4331 : : POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4332 : : return NULL_RTX;
4333 : :
4334 : 35858 : tree dest = CALL_EXPR_ARG (exp, 0);
4335 : 35858 : tree val = CALL_EXPR_ARG (exp, 1);
4336 : 35858 : tree len = CALL_EXPR_ARG (exp, 2);
4337 : :
4338 : 35858 : return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4339 : : }
4340 : :
4341 : : /* Check that store_by_pieces allows BITS + LEN (so that we don't
4342 : : expand something too unreasonably long), and every power of 2 in
4343 : : BITS. It is assumed that LEN has already been tested by
4344 : : itself. */
4345 : : static bool
4346 : 22989 : can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
4347 : : by_pieces_constfn constfun,
4348 : : void *constfundata, unsigned int align,
4349 : : bool memsetp,
4350 : : unsigned HOST_WIDE_INT len)
4351 : : {
4352 : 22989 : if (bits
4353 : 22989 : && !can_store_by_pieces (bits + len, constfun, constfundata,
4354 : : align, memsetp))
4355 : : return false;
4356 : :
4357 : : /* BITS set are expected to be generally in the low range and
4358 : : contiguous. We do NOT want to repeat the test above in case BITS
4359 : : has a single bit set, so we terminate the loop when BITS == BIT.
4360 : : In the unlikely case that BITS has the MSB set, also terminate in
4361 : : case BIT gets shifted out. */
4362 : 1116 : for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
4363 : : {
4364 : 840 : if ((bits & bit) == 0)
4365 : 504 : continue;
4366 : :
4367 : 336 : if (!can_store_by_pieces (bit, constfun, constfundata,
4368 : : align, memsetp))
4369 : : return false;
4370 : : }
4371 : :
4372 : : return true;
4373 : : }
4374 : :
4375 : : /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4376 : : Return TRUE if successful, FALSE otherwise. TO is assumed to be
4377 : : aligned at an ALIGN-bits boundary. LEN must be a multiple of
4378 : : 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4379 : :
4380 : : The strategy is to issue one store_by_pieces for each power of two,
4381 : : from most to least significant, guarded by a test on whether there
4382 : : are at least that many bytes left to copy in LEN.
4383 : :
4384 : : ??? Should we skip some powers of two in favor of loops? Maybe start
4385 : : at the max of TO/LEN/word alignment, at least when optimizing for
4386 : : size, instead of ensuring O(log len) dynamic compares? */
4387 : :
4388 : : bool
4389 : 21289 : try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4390 : : unsigned HOST_WIDE_INT min_len,
4391 : : unsigned HOST_WIDE_INT max_len,
4392 : : rtx val, char valc, unsigned int align)
4393 : : {
4394 : 21289 : int max_bits = floor_log2 (max_len);
4395 : 21289 : int min_bits = floor_log2 (min_len);
4396 : 21289 : int sctz_len = ctz_len;
4397 : :
4398 : 21289 : gcc_checking_assert (sctz_len >= 0);
4399 : :
4400 : 21289 : if (val)
4401 : 516 : valc = 1;
4402 : :
4403 : : /* Bits more significant than TST_BITS are part of the shared prefix
4404 : : in the binary representation of both min_len and max_len. Since
4405 : : they're identical, we don't need to test them in the loop. */
4406 : 21289 : int tst_bits = (max_bits != min_bits ? max_bits
4407 : 8819 : : floor_log2 (max_len ^ min_len));
4408 : :
4409 : : /* Save the pre-blksize values. */
4410 : 21289 : int orig_max_bits = max_bits;
4411 : 21289 : int orig_tst_bits = tst_bits;
4412 : :
4413 : : /* Check whether it's profitable to start by storing a fixed BLKSIZE
4414 : : bytes, to lower max_bits. In the unlikely case of a constant LEN
4415 : : (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4416 : : single store_by_pieces, but otherwise, select the minimum multiple
4417 : : of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4418 : : brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4419 : 21289 : unsigned HOST_WIDE_INT blksize;
4420 : 21289 : if (max_len > min_len)
4421 : : {
4422 : 12483 : unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4423 : : align / BITS_PER_UNIT);
4424 : 12483 : blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4425 : 12483 : blksize &= ~(alrng - 1);
4426 : : }
4427 : 8806 : else if (max_len == min_len)
4428 : : blksize = max_len;
4429 : : else
4430 : : /* Huh, max_len < min_len? Punt. See pr100843.c. */
4431 : : return false;
4432 : 21288 : if (min_len >= blksize
4433 : : /* ??? Maybe try smaller fixed-prefix blksizes before
4434 : : punting? */
4435 : 21288 : && can_store_by_pieces (blksize, builtin_memset_read_str,
4436 : : &valc, align, true))
4437 : : {
4438 : 279 : min_len -= blksize;
4439 : 279 : min_bits = floor_log2 (min_len);
4440 : 279 : max_len -= blksize;
4441 : 279 : max_bits = floor_log2 (max_len);
4442 : :
4443 : 279 : tst_bits = (max_bits != min_bits ? max_bits
4444 : 169 : : floor_log2 (max_len ^ min_len));
4445 : : }
4446 : : else
4447 : : blksize = 0;
4448 : :
4449 : : /* Check that we can use store by pieces for the maximum store count
4450 : : we may issue (initial fixed-size block, plus conditional
4451 : : power-of-two-sized from max_bits to ctz_len. */
4452 : 21288 : unsigned HOST_WIDE_INT xlenest = blksize;
4453 : 21288 : if (max_bits >= 0)
4454 : 21119 : xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4455 : 21119 : - (HOST_WIDE_INT_1U << ctz_len));
4456 : 21288 : bool max_loop = false;
4457 : 21288 : bool use_store_by_pieces = true;
4458 : : /* Skip the test in case of overflow in xlenest. It shouldn't
4459 : : happen because of the way max_bits and blksize are related, but
4460 : : it doesn't hurt to test. */
4461 : 21288 : if (blksize > xlenest
4462 : 21288 : || !can_store_by_multiple_pieces (xlenest - blksize,
4463 : : builtin_memset_read_str,
4464 : : &valc, align, true, blksize))
4465 : : {
4466 : 21119 : if (!(flag_inline_stringops & ILSOP_MEMSET))
4467 : : return false;
4468 : :
4469 : 1601 : for (max_bits = orig_max_bits;
4470 : 1708 : max_bits >= sctz_len;
4471 : : --max_bits)
4472 : : {
4473 : 1708 : xlenest = ((HOST_WIDE_INT_1U << max_bits) * 2
4474 : 1708 : - (HOST_WIDE_INT_1U << ctz_len));
4475 : : /* Check that blksize plus the bits to be stored as blocks
4476 : : sized at powers of two can be stored by pieces. This is
4477 : : like the test above, but with smaller max_bits. Skip
4478 : : orig_max_bits (it would be redundant). Also skip in case
4479 : : of overflow. */
4480 : 1708 : if (max_bits < orig_max_bits
4481 : 1601 : && xlenest + blksize >= xlenest
4482 : 3309 : && can_store_by_multiple_pieces (xlenest,
4483 : : builtin_memset_read_str,
4484 : : &valc, align, true, blksize))
4485 : : {
4486 : : max_loop = true;
4487 : : break;
4488 : : }
4489 : 1601 : if (blksize
4490 : 1601 : && can_store_by_multiple_pieces (xlenest,
4491 : : builtin_memset_read_str,
4492 : : &valc, align, true, 0))
4493 : : {
4494 : 0 : max_len += blksize;
4495 : 0 : min_len += blksize;
4496 : 0 : tst_bits = orig_tst_bits;
4497 : 0 : blksize = 0;
4498 : 0 : max_loop = true;
4499 : 0 : break;
4500 : : }
4501 : 1601 : if (max_bits == sctz_len)
4502 : : {
4503 : : /* We'll get here if can_store_by_pieces refuses to
4504 : : store even a single QImode. We'll fall back to
4505 : : QImode stores then. */
4506 : 98 : if (!sctz_len)
4507 : : {
4508 : : blksize = 0;
4509 : : max_loop = true;
4510 : : use_store_by_pieces = false;
4511 : : break;
4512 : : }
4513 : 98 : --sctz_len;
4514 : 98 : --ctz_len;
4515 : : }
4516 : : }
4517 : 107 : if (!max_loop)
4518 : : return false;
4519 : : /* If the boundaries are such that min and max may run a
4520 : : different number of trips in the initial loop, the remainder
4521 : : needs not be between the moduli, so set tst_bits to cover all
4522 : : bits. Otherwise, if the trip counts are the same, max_len
4523 : : has the common prefix, and the previously-computed tst_bits
4524 : : is usable. */
4525 : 107 : if (max_len >> max_bits > min_len >> max_bits)
4526 : 64 : tst_bits = max_bits;
4527 : : }
4528 : :
4529 : 276 : by_pieces_constfn constfun;
4530 : 276 : void *constfundata;
4531 : 276 : if (val)
4532 : : {
4533 : 1 : constfun = builtin_memset_gen_str;
4534 : 1 : constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4535 : : val);
4536 : : }
4537 : : else
4538 : : {
4539 : : constfun = builtin_memset_read_str;
4540 : : constfundata = &valc;
4541 : : }
4542 : :
4543 : 276 : rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4544 : 276 : rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4545 : 276 : to = replace_equiv_address (to, ptr);
4546 : 276 : set_mem_align (to, align);
4547 : :
4548 : 276 : if (blksize)
4549 : : {
4550 : 356 : to = store_by_pieces (to, blksize,
4551 : : constfun, constfundata,
4552 : : align, true,
4553 : : max_len != 0 ? RETURN_END : RETURN_BEGIN);
4554 : 188 : if (max_len == 0)
4555 : : return true;
4556 : :
4557 : : /* Adjust PTR, TO and REM. Since TO's address is likely
4558 : : PTR+offset, we have to replace it. */
4559 : 20 : emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4560 : 20 : to = replace_equiv_address (to, ptr);
4561 : 20 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4562 : 20 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4563 : : }
4564 : :
4565 : : /* Iterate over power-of-two block sizes from the maximum length to
4566 : : the least significant bit possibly set in the length. */
4567 : 493 : for (int i = max_bits; i >= sctz_len; i--)
4568 : : {
4569 : 385 : rtx_code_label *loop_label = NULL;
4570 : 385 : rtx_code_label *label = NULL;
4571 : :
4572 : 385 : blksize = HOST_WIDE_INT_1U << i;
4573 : :
4574 : : /* If we're past the bits shared between min_ and max_len, expand
4575 : : a test on the dynamic length, comparing it with the
4576 : : BLKSIZE. */
4577 : 385 : if (i <= tst_bits)
4578 : : {
4579 : 275 : label = gen_label_rtx ();
4580 : 275 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4581 : : ptr_mode, 1, label,
4582 : : profile_probability::even ());
4583 : : }
4584 : : /* If we are at a bit that is in the prefix shared by min_ and
4585 : : max_len, skip the current BLKSIZE if the bit is clear, but do
4586 : : not skip the loop, even if it doesn't require
4587 : : prechecking. */
4588 : 110 : else if ((max_len & blksize) == 0
4589 : 69 : && !(max_loop && i == max_bits))
4590 : 39 : continue;
4591 : :
4592 : 346 : if (max_loop && i == max_bits)
4593 : : {
4594 : 107 : loop_label = gen_label_rtx ();
4595 : 107 : emit_label (loop_label);
4596 : : /* Since we may run this multiple times, don't assume we
4597 : : know anything about the offset. */
4598 : 107 : clear_mem_offset (to);
4599 : : }
4600 : :
4601 : 346 : bool update_needed = i != sctz_len || loop_label;
4602 : 346 : rtx next_ptr = NULL_RTX;
4603 : 346 : if (!use_store_by_pieces)
4604 : : {
4605 : 0 : gcc_checking_assert (blksize == 1);
4606 : 0 : if (!val)
4607 : 0 : val = gen_int_mode (valc, QImode);
4608 : 0 : to = change_address (to, QImode, 0);
4609 : 0 : emit_move_insn (to, val);
4610 : 0 : if (update_needed)
4611 : 0 : next_ptr = plus_constant (GET_MODE (ptr), ptr, blksize);
4612 : : }
4613 : : else
4614 : : {
4615 : : /* Issue a store of BLKSIZE bytes. */
4616 : 404 : to = store_by_pieces (to, blksize,
4617 : : constfun, constfundata,
4618 : : align, true,
4619 : : update_needed ? RETURN_END : RETURN_BEGIN);
4620 : 346 : next_ptr = XEXP (to, 0);
4621 : : }
4622 : : /* Adjust REM and PTR, unless this is the last iteration. */
4623 : 346 : if (update_needed)
4624 : : {
4625 : 288 : emit_move_insn (ptr, force_operand (next_ptr, NULL_RTX));
4626 : 288 : to = replace_equiv_address (to, ptr);
4627 : 288 : rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4628 : 288 : emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4629 : : }
4630 : :
4631 : 288 : if (loop_label)
4632 : 107 : emit_cmp_and_jump_insns (rem, GEN_INT (blksize), GE, NULL,
4633 : : ptr_mode, 1, loop_label,
4634 : : profile_probability::likely ());
4635 : :
4636 : 346 : if (label)
4637 : : {
4638 : 275 : emit_label (label);
4639 : :
4640 : : /* Given conditional stores, the offset can no longer be
4641 : : known, so clear it. */
4642 : 275 : clear_mem_offset (to);
4643 : : }
4644 : : }
4645 : :
4646 : : return true;
4647 : : }
4648 : :
4649 : : /* Helper function to do the actual work for expand_builtin_memset. The
4650 : : arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4651 : : so that this can also be called without constructing an actual CALL_EXPR.
4652 : : The other arguments and return value are the same as for
4653 : : expand_builtin_memset. */
4654 : :
4655 : : static rtx
4656 : 35858 : expand_builtin_memset_args (tree dest, tree val, tree len,
4657 : : rtx target, machine_mode mode, tree orig_exp)
4658 : : {
4659 : 35858 : tree fndecl, fn;
4660 : 35858 : enum built_in_function fcode;
4661 : 35858 : machine_mode val_mode;
4662 : 35858 : char c;
4663 : 35858 : unsigned int dest_align;
4664 : 35858 : rtx dest_mem, dest_addr, len_rtx;
4665 : 35858 : HOST_WIDE_INT expected_size = -1;
4666 : 35858 : unsigned int expected_align = 0;
4667 : 35858 : unsigned HOST_WIDE_INT min_size;
4668 : 35858 : unsigned HOST_WIDE_INT max_size;
4669 : 35858 : unsigned HOST_WIDE_INT probable_max_size;
4670 : :
4671 : 35858 : dest_align = get_pointer_alignment (dest);
4672 : :
4673 : : /* If DEST is not a pointer type, don't do this operation in-line. */
4674 : 35858 : if (dest_align == 0)
4675 : : return NULL_RTX;
4676 : :
4677 : 35858 : if (currently_expanding_gimple_stmt)
4678 : 35858 : stringop_block_profile (currently_expanding_gimple_stmt,
4679 : : &expected_align, &expected_size);
4680 : :
4681 : 35858 : if (expected_align < dest_align)
4682 : 35853 : expected_align = dest_align;
4683 : :
4684 : : /* If the LEN parameter is zero, return DEST. */
4685 : 35858 : if (integer_zerop (len))
4686 : : {
4687 : : /* Evaluate and ignore VAL in case it has side-effects. */
4688 : 0 : expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4689 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
4690 : : }
4691 : :
4692 : : /* Stabilize the arguments in case we fail. */
4693 : 35858 : dest = builtin_save_expr (dest);
4694 : 35858 : val = builtin_save_expr (val);
4695 : 35858 : len = builtin_save_expr (len);
4696 : :
4697 : 35858 : len_rtx = expand_normal (len);
4698 : 35858 : determine_block_size (len, len_rtx, &min_size, &max_size,
4699 : : &probable_max_size);
4700 : 35858 : dest_mem = get_memory_rtx (dest, len);
4701 : 35858 : val_mode = TYPE_MODE (unsigned_char_type_node);
4702 : :
4703 : 35858 : if (TREE_CODE (val) != INTEGER_CST
4704 : 35858 : || target_char_cast (val, &c))
4705 : : {
4706 : 1720 : rtx val_rtx;
4707 : :
4708 : 1720 : val_rtx = expand_normal (val);
4709 : 1720 : val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4710 : :
4711 : : /* Assume that we can memset by pieces if we can store
4712 : : * the coefficients by pieces (in the required modes).
4713 : : * We can't pass builtin_memset_gen_str as that emits RTL. */
4714 : 1720 : c = 1;
4715 : 1720 : if (tree_fits_uhwi_p (len)
4716 : 1720 : && can_store_by_pieces (tree_to_uhwi (len),
4717 : : builtin_memset_read_str, &c, dest_align,
4718 : : true))
4719 : : {
4720 : 772 : val_rtx = force_reg (val_mode, val_rtx);
4721 : 772 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4722 : : builtin_memset_gen_str, val_rtx, dest_align,
4723 : : true, RETURN_BEGIN);
4724 : : }
4725 : 948 : else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4726 : : dest_align, expected_align,
4727 : : expected_size, min_size, max_size,
4728 : : probable_max_size)
4729 : 948 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4730 : : tree_ctz (len),
4731 : : min_size, max_size,
4732 : : val_rtx, 0,
4733 : : dest_align))
4734 : 515 : goto do_libcall;
4735 : :
4736 : 1205 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4737 : 1205 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4738 : 1205 : return dest_mem;
4739 : : }
4740 : :
4741 : 34138 : if (c)
4742 : : {
4743 : 12797 : if (tree_fits_uhwi_p (len)
4744 : 12797 : && can_store_by_pieces (tree_to_uhwi (len),
4745 : : builtin_memset_read_str, &c, dest_align,
4746 : : true))
4747 : 4868 : store_by_pieces (dest_mem, tree_to_uhwi (len),
4748 : : builtin_memset_read_str, &c, dest_align, true,
4749 : : RETURN_BEGIN);
4750 : 9681 : else if (!set_storage_via_setmem (dest_mem, len_rtx,
4751 : 7929 : gen_int_mode (c, val_mode),
4752 : : dest_align, expected_align,
4753 : : expected_size, min_size, max_size,
4754 : : probable_max_size)
4755 : 7929 : && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4756 : : tree_ctz (len),
4757 : : min_size, max_size,
4758 : : NULL_RTX, c,
4759 : : dest_align))
4760 : 6177 : goto do_libcall;
4761 : :
4762 : 6620 : dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4763 : 6620 : dest_mem = convert_memory_address (ptr_mode, dest_mem);
4764 : 6620 : return dest_mem;
4765 : : }
4766 : :
4767 : 21341 : set_mem_align (dest_mem, dest_align);
4768 : 42682 : dest_addr = clear_storage_hints (dest_mem, len_rtx,
4769 : 21341 : CALL_EXPR_TAILCALL (orig_exp)
4770 : : ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4771 : : expected_align, expected_size,
4772 : : min_size, max_size,
4773 : : probable_max_size, tree_ctz (len));
4774 : :
4775 : 21341 : if (dest_addr == 0)
4776 : : {
4777 : 12722 : dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4778 : 12722 : dest_addr = convert_memory_address (ptr_mode, dest_addr);
4779 : : }
4780 : :
4781 : : return dest_addr;
4782 : :
4783 : 6692 : do_libcall:
4784 : 6692 : fndecl = get_callee_fndecl (orig_exp);
4785 : 6692 : fcode = DECL_FUNCTION_CODE (fndecl);
4786 : 6692 : if (fcode == BUILT_IN_MEMSET)
4787 : 6692 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4788 : : dest, val, len);
4789 : 0 : else if (fcode == BUILT_IN_BZERO)
4790 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4791 : : dest, len);
4792 : : else
4793 : 0 : gcc_unreachable ();
4794 : 6692 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4795 : 6692 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4796 : 6692 : return expand_call (fn, target, target == const0_rtx);
4797 : : }
4798 : :
4799 : : /* Expand expression EXP, which is a call to the bzero builtin. Return
4800 : : NULL_RTX if we failed the caller should emit a normal call. */
4801 : :
4802 : : static rtx
4803 : 0 : expand_builtin_bzero (tree exp)
4804 : : {
4805 : 0 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4806 : : return NULL_RTX;
4807 : :
4808 : 0 : tree dest = CALL_EXPR_ARG (exp, 0);
4809 : 0 : tree size = CALL_EXPR_ARG (exp, 1);
4810 : :
4811 : : /* New argument list transforming bzero(ptr x, int y) to
4812 : : memset(ptr x, int 0, size_t y). This is done this way
4813 : : so that if it isn't expanded inline, we fallback to
4814 : : calling bzero instead of memset. */
4815 : :
4816 : 0 : location_t loc = EXPR_LOCATION (exp);
4817 : :
4818 : 0 : return expand_builtin_memset_args (dest, integer_zero_node,
4819 : : fold_convert_loc (loc,
4820 : : size_type_node, size),
4821 : 0 : const0_rtx, VOIDmode, exp);
4822 : : }
4823 : :
4824 : : /* Try to expand cmpstr operation ICODE with the given operands.
4825 : : Return the result rtx on success, otherwise return null. */
4826 : :
4827 : : static rtx
4828 : 0 : expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4829 : : HOST_WIDE_INT align)
4830 : : {
4831 : 0 : machine_mode insn_mode = insn_data[icode].operand[0].mode;
4832 : :
4833 : 0 : if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4834 : : target = NULL_RTX;
4835 : :
4836 : 0 : class expand_operand ops[4];
4837 : 0 : create_output_operand (&ops[0], target, insn_mode);
4838 : 0 : create_fixed_operand (&ops[1], arg1_rtx);
4839 : 0 : create_fixed_operand (&ops[2], arg2_rtx);
4840 : 0 : create_integer_operand (&ops[3], align);
4841 : 0 : if (maybe_expand_insn (icode, 4, ops))
4842 : 0 : return ops[0].value;
4843 : : return NULL_RTX;
4844 : : }
4845 : :
4846 : : /* Expand expression EXP, which is a call to the memcmp built-in function.
4847 : : Return NULL_RTX if we failed and the caller should emit a normal call,
4848 : : otherwise try to get the result in TARGET, if convenient.
4849 : : RESULT_EQ is true if we can relax the returned value to be either zero
4850 : : or nonzero, without caring about the sign. */
4851 : :
4852 : : static rtx
4853 : 102000 : expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4854 : : {
4855 : 102000 : if (!validate_arglist (exp,
4856 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4857 : : return NULL_RTX;
4858 : :
4859 : 101996 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4860 : 101996 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4861 : 101996 : tree len = CALL_EXPR_ARG (exp, 2);
4862 : :
4863 : : /* Due to the performance benefit, always inline the calls first
4864 : : when result_eq is false. */
4865 : 101996 : rtx result = NULL_RTX;
4866 : 101996 : enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4867 : 101996 : if (!result_eq && fcode != BUILT_IN_BCMP)
4868 : : {
4869 : 43501 : result = inline_expand_builtin_bytecmp (exp, target);
4870 : 43501 : if (result)
4871 : : return result;
4872 : : }
4873 : :
4874 : 101987 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4875 : 101987 : location_t loc = EXPR_LOCATION (exp);
4876 : :
4877 : 101987 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4878 : 101987 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4879 : :
4880 : : /* If we don't have POINTER_TYPE, call the function. */
4881 : 101987 : if (arg1_align == 0 || arg2_align == 0)
4882 : : return NULL_RTX;
4883 : :
4884 : 101987 : rtx arg1_rtx = get_memory_rtx (arg1, len);
4885 : 101987 : rtx arg2_rtx = get_memory_rtx (arg2, len);
4886 : 101987 : rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4887 : :
4888 : : /* Set MEM_SIZE as appropriate. */
4889 : 101987 : if (CONST_INT_P (len_rtx))
4890 : : {
4891 : 78036 : set_mem_size (arg1_rtx, INTVAL (len_rtx));
4892 : 78036 : set_mem_size (arg2_rtx, INTVAL (len_rtx));
4893 : : }
4894 : :
4895 : 101987 : by_pieces_constfn constfn = NULL;
4896 : :
4897 : : /* Try to get the byte representation of the constant ARG2 (or, only
4898 : : when the function's result is used for equality to zero, ARG1)
4899 : : points to, with its byte size in NBYTES. */
4900 : 101987 : unsigned HOST_WIDE_INT nbytes;
4901 : 101987 : const char *rep = getbyterep (arg2, &nbytes);
4902 : 101987 : if (result_eq && rep == NULL)
4903 : : {
4904 : : /* For equality to zero the arguments are interchangeable. */
4905 : 35419 : rep = getbyterep (arg1, &nbytes);
4906 : 35419 : if (rep != NULL)
4907 : : std::swap (arg1_rtx, arg2_rtx);
4908 : : }
4909 : :
4910 : : /* If the function's constant bound LEN_RTX is less than or equal
4911 : : to the byte size of the representation of the constant argument,
4912 : : and if block move would be done by pieces, we can avoid loading
4913 : : the bytes from memory and only store the computed constant result. */
4914 : 66568 : if (rep
4915 : 27248 : && CONST_INT_P (len_rtx)
4916 : 27136 : && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4917 : 101987 : constfn = builtin_memcpy_read_str;
4918 : :
4919 : 203974 : result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4920 : 101987 : TREE_TYPE (len), target,
4921 : : result_eq, constfn,
4922 : : CONST_CAST (char *, rep),
4923 : : tree_ctz (len));
4924 : :
4925 : 101987 : if (result)
4926 : : {
4927 : : /* Return the value in the proper mode for this function. */
4928 : 40414 : if (GET_MODE (result) == mode)
4929 : : return result;
4930 : :
4931 : 6 : if (target != 0)
4932 : : {
4933 : 6 : convert_move (target, result, 0);
4934 : 6 : return target;
4935 : : }
4936 : :
4937 : 0 : return convert_to_mode (mode, result, 0);
4938 : : }
4939 : :
4940 : : return NULL_RTX;
4941 : : }
4942 : :
4943 : : /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4944 : : if we failed the caller should emit a normal call, otherwise try to get
4945 : : the result in TARGET, if convenient. */
4946 : :
4947 : : static rtx
4948 : 127931 : expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4949 : : {
4950 : 127931 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4951 : : return NULL_RTX;
4952 : :
4953 : 127916 : tree arg1 = CALL_EXPR_ARG (exp, 0);
4954 : 127916 : tree arg2 = CALL_EXPR_ARG (exp, 1);
4955 : :
4956 : : /* Due to the performance benefit, always inline the calls first. */
4957 : 127916 : rtx result = NULL_RTX;
4958 : 127916 : result = inline_expand_builtin_bytecmp (exp, target);
4959 : 127916 : if (result)
4960 : : return result;
4961 : :
4962 : 127517 : insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4963 : 127517 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4964 : 127517 : if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4965 : : return NULL_RTX;
4966 : :
4967 : 127517 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4968 : 127517 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4969 : :
4970 : : /* If we don't have POINTER_TYPE, call the function. */
4971 : 127517 : if (arg1_align == 0 || arg2_align == 0)
4972 : : return NULL_RTX;
4973 : :
4974 : : /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4975 : 127517 : arg1 = builtin_save_expr (arg1);
4976 : 127517 : arg2 = builtin_save_expr (arg2);
4977 : :
4978 : 127517 : rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4979 : 127517 : rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4980 : :
4981 : : /* Try to call cmpstrsi. */
4982 : 127517 : if (cmpstr_icode != CODE_FOR_nothing)
4983 : 0 : result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4984 : 0 : MIN (arg1_align, arg2_align));
4985 : :
4986 : : /* Try to determine at least one length and call cmpstrnsi. */
4987 : 127517 : if (!result && cmpstrn_icode != CODE_FOR_nothing)
4988 : : {
4989 : 127517 : tree len;
4990 : 127517 : rtx arg3_rtx;
4991 : :
4992 : 127517 : tree len1 = c_strlen (arg1, 1);
4993 : 127517 : tree len2 = c_strlen (arg2, 1);
4994 : :
4995 : 127517 : if (len1)
4996 : 224 : len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4997 : 127517 : if (len2)
4998 : 125889 : len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4999 : :
5000 : : /* If we don't have a constant length for the first, use the length
5001 : : of the second, if we know it. We don't require a constant for
5002 : : this case; some cost analysis could be done if both are available
5003 : : but neither is constant. For now, assume they're equally cheap,
5004 : : unless one has side effects. If both strings have constant lengths,
5005 : : use the smaller. */
5006 : :
5007 : 127517 : if (!len1)
5008 : : len = len2;
5009 : 224 : else if (!len2)
5010 : : len = len1;
5011 : 17 : else if (TREE_SIDE_EFFECTS (len1))
5012 : : len = len2;
5013 : 17 : else if (TREE_SIDE_EFFECTS (len2))
5014 : : len = len1;
5015 : 17 : else if (TREE_CODE (len1) != INTEGER_CST)
5016 : : len = len2;
5017 : 17 : else if (TREE_CODE (len2) != INTEGER_CST)
5018 : : len = len1;
5019 : 10 : else if (tree_int_cst_lt (len1, len2))
5020 : : len = len1;
5021 : : else
5022 : 127299 : len = len2;
5023 : :
5024 : : /* If both arguments have side effects, we cannot optimize. */
5025 : 127517 : if (len && !TREE_SIDE_EFFECTS (len))
5026 : : {
5027 : 126096 : arg3_rtx = expand_normal (len);
5028 : 126096 : result = expand_cmpstrn_or_cmpmem
5029 : 126096 : (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
5030 : 126096 : arg3_rtx, MIN (arg1_align, arg2_align));
5031 : : }
5032 : : }
5033 : :
5034 : 127517 : tree fndecl = get_callee_fndecl (exp);
5035 : 127517 : if (result)
5036 : : {
5037 : : /* Return the value in the proper mode for this function. */
5038 : 52 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
5039 : 52 : if (GET_MODE (result) == mode)
5040 : : return result;
5041 : 0 : if (target == 0)
5042 : 0 : return convert_to_mode (mode, result, 0);
5043 : 0 : convert_move (target, result, 0);
5044 : 0 : return target;
5045 : : }
5046 : :
5047 : : /* Expand the library call ourselves using a stabilized argument
5048 : : list to avoid re-evaluating the function's arguments twice. */
5049 : 127465 : tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
5050 : 127465 : copy_warning (fn, exp);
5051 : 127465 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5052 : 127465 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5053 : 127465 : return expand_call (fn, target, target == const0_rtx);
5054 : : }
5055 : :
5056 : : /* Expand expression EXP, which is a call to the strncmp builtin. Return
5057 : : NULL_RTX if we failed the caller should emit a normal call, otherwise
5058 : : try to get the result in TARGET, if convenient. */
5059 : :
5060 : : static rtx
5061 : 2029 : expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
5062 : : ATTRIBUTE_UNUSED machine_mode mode)
5063 : : {
5064 : 2029 : if (!validate_arglist (exp,
5065 : : POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5066 : : return NULL_RTX;
5067 : :
5068 : 2023 : tree arg1 = CALL_EXPR_ARG (exp, 0);
5069 : 2023 : tree arg2 = CALL_EXPR_ARG (exp, 1);
5070 : 2023 : tree arg3 = CALL_EXPR_ARG (exp, 2);
5071 : :
5072 : 2023 : location_t loc = EXPR_LOCATION (exp);
5073 : 2023 : tree len1 = c_strlen (arg1, 1);
5074 : 2023 : tree len2 = c_strlen (arg2, 1);
5075 : :
5076 : : /* Due to the performance benefit, always inline the calls first. */
5077 : 2023 : rtx result = NULL_RTX;
5078 : 2023 : result = inline_expand_builtin_bytecmp (exp, target);
5079 : 2023 : if (result)
5080 : : return result;
5081 : :
5082 : : /* If c_strlen can determine an expression for one of the string
5083 : : lengths, and it doesn't have side effects, then emit cmpstrnsi
5084 : : using length MIN(strlen(string)+1, arg3). */
5085 : 1805 : insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
5086 : 1805 : if (cmpstrn_icode == CODE_FOR_nothing)
5087 : : return NULL_RTX;
5088 : :
5089 : 1805 : tree len;
5090 : :
5091 : 1805 : unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
5092 : 1805 : unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
5093 : :
5094 : 1805 : if (len1)
5095 : 160 : len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
5096 : 1805 : if (len2)
5097 : 676 : len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
5098 : :
5099 : 1805 : tree len3 = fold_convert_loc (loc, sizetype, arg3);
5100 : :
5101 : : /* If we don't have a constant length for the first, use the length
5102 : : of the second, if we know it. If neither string is constant length,
5103 : : use the given length argument. We don't require a constant for
5104 : : this case; some cost analysis could be done if both are available
5105 : : but neither is constant. For now, assume they're equally cheap,
5106 : : unless one has side effects. If both strings have constant lengths,
5107 : : use the smaller. */
5108 : :
5109 : 1805 : if (!len1 && !len2)
5110 : : len = len3;
5111 : 719 : else if (!len1)
5112 : : len = len2;
5113 : 160 : else if (!len2)
5114 : : len = len1;
5115 : 117 : else if (TREE_SIDE_EFFECTS (len1))
5116 : : len = len2;
5117 : 117 : else if (TREE_SIDE_EFFECTS (len2))
5118 : : len = len1;
5119 : 117 : else if (TREE_CODE (len1) != INTEGER_CST)
5120 : : len = len2;
5121 : 117 : else if (TREE_CODE (len2) != INTEGER_CST)
5122 : : len = len1;
5123 : 117 : else if (tree_int_cst_lt (len1, len2))
5124 : : len = len1;
5125 : : else
5126 : 560 : len = len2;
5127 : :
5128 : : /* If we are not using the given length, we must incorporate it here.
5129 : : The actual new length parameter will be MIN(len,arg3) in this case. */
5130 : 1805 : if (len != len3)
5131 : : {
5132 : 719 : len = fold_convert_loc (loc, sizetype, len);
5133 : 719 : len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
5134 : : }
5135 : 1805 : rtx arg1_rtx = get_memory_rtx (arg1, len);
5136 : 1805 : rtx arg2_rtx = get_memory_rtx (arg2, len);
5137 : 1805 : rtx arg3_rtx = expand_normal (len);
5138 : 1805 : result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
5139 : 1805 : arg2_rtx, TREE_TYPE (len), arg3_rtx,
5140 : 1805 : MIN (arg1_align, arg2_align));
5141 : :
5142 : 1805 : tree fndecl = get_callee_fndecl (exp);
5143 : 1805 : if (result)
5144 : : {
5145 : : /* Return the value in the proper mode for this function. */
5146 : 21 : mode = TYPE_MODE (TREE_TYPE (exp));
5147 : 21 : if (GET_MODE (result) == mode)
5148 : : return result;
5149 : 0 : if (target == 0)
5150 : 0 : return convert_to_mode (mode, result, 0);
5151 : 0 : convert_move (target, result, 0);
5152 : 0 : return target;
5153 : : }
5154 : :
5155 : : /* Expand the library call ourselves using a stabilized argument
5156 : : list to avoid re-evaluating the function's arguments twice. */
5157 : 1784 : tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
5158 : 1784 : copy_warning (call, exp);
5159 : 1784 : gcc_assert (TREE_CODE (call) == CALL_EXPR);
5160 : 1784 : CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
5161 : 1784 : return expand_call (call, target, target == const0_rtx);
5162 : : }
5163 : :
5164 : : /* Expand a call to __builtin_saveregs, generating the result in TARGET,
5165 : : if that's convenient. */
5166 : :
5167 : : rtx
5168 : 0 : expand_builtin_saveregs (void)
5169 : : {
5170 : 0 : rtx val;
5171 : 0 : rtx_insn *seq;
5172 : :
5173 : : /* Don't do __builtin_saveregs more than once in a function.
5174 : : Save the result of the first call and reuse it. */
5175 : 0 : if (saveregs_value != 0)
5176 : : return saveregs_value;
5177 : :
5178 : : /* When this function is called, it means that registers must be
5179 : : saved on entry to this function. So we migrate the call to the
5180 : : first insn of this function. */
5181 : :
5182 : 0 : start_sequence ();
5183 : :
5184 : : /* Do whatever the machine needs done in this case. */
5185 : 0 : val = targetm.calls.expand_builtin_saveregs ();
5186 : :
5187 : 0 : seq = end_sequence ();
5188 : :
5189 : 0 : saveregs_value = val;
5190 : :
5191 : : /* Put the insns after the NOTE that starts the function. If this
5192 : : is inside a start_sequence, make the outer-level insn chain current, so
5193 : : the code is placed at the start of the function. */
5194 : 0 : push_topmost_sequence ();
5195 : 0 : emit_insn_after (seq, entry_of_function ());
5196 : 0 : pop_topmost_sequence ();
5197 : :
5198 : 0 : return val;
5199 : : }
5200 : :
5201 : : /* Expand a call to __builtin_next_arg. */
5202 : :
5203 : : static rtx
5204 : 21033 : 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 : 21033 : return expand_binop (ptr_mode, add_optab,
5209 : : crtl->args.internal_arg_pointer,
5210 : : crtl->args.arg_offset_rtx,
5211 : 21033 : 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 : 21367 : stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5219 : : {
5220 : 21367 : 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 : 21367 : if (!vatype)
5225 : 21243 : vatype = targetm.fn_abi_va_list (cfun->decl);
5226 : :
5227 : 21367 : if (TREE_CODE (vatype) == ARRAY_TYPE)
5228 : : {
5229 : 15705 : 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 : 15705 : if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5237 : : {
5238 : 0 : tree p1 = build_pointer_type (TREE_TYPE (vatype));
5239 : 0 : valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5240 : : }
5241 : : }
5242 : : else
5243 : : {
5244 : 5662 : tree pt = build_pointer_type (vatype);
5245 : :
5246 : 5662 : if (! needs_lvalue)
5247 : : {
5248 : 6 : if (! TREE_SIDE_EFFECTS (valist))
5249 : : return valist;
5250 : :
5251 : 0 : valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5252 : 0 : TREE_SIDE_EFFECTS (valist) = 1;
5253 : : }
5254 : :
5255 : 5656 : if (TREE_SIDE_EFFECTS (valist))
5256 : 0 : valist = save_expr (valist);
5257 : 5656 : valist = fold_build2_loc (loc, MEM_REF,
5258 : : vatype, valist, build_int_cst (pt, 0));
5259 : : }
5260 : :
5261 : : return valist;
5262 : : }
5263 : :
5264 : : /* The "standard" definition of va_list is void*. */
5265 : :
5266 : : tree
5267 : 0 : std_build_builtin_va_list (void)
5268 : : {
5269 : 0 : return ptr_type_node;
5270 : : }
5271 : :
5272 : : /* The "standard" abi va_list is va_list_type_node. */
5273 : :
5274 : : tree
5275 : 0 : std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5276 : : {
5277 : 0 : return va_list_type_node;
5278 : : }
5279 : :
5280 : : /* The "standard" type of va_list is va_list_type_node. */
5281 : :
5282 : : tree
5283 : 502 : std_canonical_va_list_type (tree type)
5284 : : {
5285 : 502 : tree wtype, htype;
5286 : :
5287 : 502 : wtype = va_list_type_node;
5288 : 502 : htype = type;
5289 : :
5290 : 502 : if (TREE_CODE (wtype) == ARRAY_TYPE)
5291 : : {
5292 : : /* If va_list is an array type, the argument may have decayed
5293 : : to a pointer type, e.g. by being passed to another function.
5294 : : In that case, unwrap both types so that we can compare the
5295 : : underlying records. */
5296 : 0 : if (TREE_CODE (htype) == ARRAY_TYPE
5297 : 0 : || POINTER_TYPE_P (htype))
5298 : : {
5299 : 0 : wtype = TREE_TYPE (wtype);
5300 : 0 : htype = TREE_TYPE (htype);
5301 : : }
5302 : : }
5303 : 502 : if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5304 : 332 : return va_list_type_node;
5305 : :
5306 : : return NULL_TREE;
5307 : : }
5308 : :
5309 : : /* The "standard" implementation of va_start: just assign `nextarg' to
5310 : : the variable. */
5311 : :
5312 : : void
5313 : 5652 : std_expand_builtin_va_start (tree valist, rtx nextarg)
5314 : : {
5315 : 5652 : rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5316 : 5652 : convert_move (va_r, nextarg, 0);
5317 : 5652 : }
5318 : :
5319 : : /* Expand EXP, a call to __builtin_va_start. */
5320 : :
5321 : : static rtx
5322 : 20883 : expand_builtin_va_start (tree exp)
5323 : : {
5324 : 20883 : rtx nextarg;
5325 : 20883 : tree valist;
5326 : 20883 : location_t loc = EXPR_LOCATION (exp);
5327 : :
5328 : 20883 : 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 : 20883 : if (fold_builtin_next_arg (exp, true))
5335 : 0 : return const0_rtx;
5336 : :
5337 : 20883 : nextarg = expand_builtin_next_arg ();
5338 : 20883 : valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5339 : :
5340 : 20883 : if (targetm.expand_builtin_va_start)
5341 : 20883 : targetm.expand_builtin_va_start (valist, nextarg);
5342 : : else
5343 : 0 : std_expand_builtin_va_start (valist, nextarg);
5344 : :
5345 : 20883 : return const0_rtx;
5346 : : }
5347 : :
5348 : : /* Expand EXP, a call to __builtin_va_end. */
5349 : :
5350 : : static rtx
5351 : 12077 : expand_builtin_va_end (tree exp)
5352 : : {
5353 : 12077 : 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 : 12077 : if (TREE_SIDE_EFFECTS (valist))
5358 : 0 : expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5359 : :
5360 : 12077 : return const0_rtx;
5361 : : }
5362 : :
5363 : : /* Expand EXP, a call to __builtin_va_copy. We do this as a
5364 : : builtin rather than just as an assignment in stdarg.h because of the
5365 : : nastiness of array-type va_list types. */
5366 : :
5367 : : static rtx
5368 : 242 : expand_builtin_va_copy (tree exp)
5369 : : {
5370 : 242 : tree dst, src, t;
5371 : 242 : location_t loc = EXPR_LOCATION (exp);
5372 : :
5373 : 242 : dst = CALL_EXPR_ARG (exp, 0);
5374 : 242 : src = CALL_EXPR_ARG (exp, 1);
5375 : :
5376 : 242 : dst = stabilize_va_list_loc (loc, dst, 1);
5377 : 242 : src = stabilize_va_list_loc (loc, src, 0);
5378 : :
5379 : 242 : gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5380 : :
5381 : 242 : if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5382 : : {
5383 : 0 : t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5384 : 0 : TREE_SIDE_EFFECTS (t) = 1;
5385 : 0 : expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5386 : : }
5387 : : else
5388 : : {
5389 : 242 : rtx dstb, srcb, size;
5390 : :
5391 : : /* Evaluate to pointers. */
5392 : 242 : dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5393 : 242 : srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5394 : 242 : size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5395 : : NULL_RTX, VOIDmode, EXPAND_NORMAL);
5396 : :
5397 : 242 : dstb = convert_memory_address (Pmode, dstb);
5398 : 242 : srcb = convert_memory_address (Pmode, srcb);
5399 : :
5400 : : /* "Dereference" to BLKmode memories. */
5401 : 242 : dstb = gen_rtx_MEM (BLKmode, dstb);
5402 : 242 : set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5403 : 242 : set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5404 : 242 : srcb = gen_rtx_MEM (BLKmode, srcb);
5405 : 242 : set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5406 : 242 : set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5407 : :
5408 : : /* Copy. */
5409 : 242 : emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5410 : : }
5411 : :
5412 : 242 : return const0_rtx;
5413 : : }
5414 : :
5415 : : /* Expand a call to one of the builtin functions __builtin_frame_address or
5416 : : __builtin_return_address. */
5417 : :
5418 : : static rtx
5419 : 15583 : 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 : 15583 : if (call_expr_nargs (exp) == 0)
5426 : : /* Warning about missing arg was already issued. */
5427 : 0 : return const0_rtx;
5428 : 15583 : 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 : 15583 : unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5437 : :
5438 : 15583 : rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5439 : :
5440 : : /* Some ports cannot access arbitrary stack frames. */
5441 : 15583 : if (tem == NULL)
5442 : : {
5443 : 0 : warning (0, "unsupported argument to %qD", fndecl);
5444 : 0 : return const0_rtx;
5445 : : }
5446 : :
5447 : 15583 : 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 : 15583 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5457 : : return tem;
5458 : :
5459 : 5086 : if (!REG_P (tem)
5460 : 5086 : && ! CONSTANT_P (tem))
5461 : 5086 : tem = copy_addr_to_reg (tem);
5462 : 5086 : return tem;
5463 : : }
5464 : : }
5465 : :
5466 : : #if ! STACK_GROWS_DOWNWARD
5467 : : # define STACK_TOPS GT
5468 : : #else
5469 : : # define STACK_TOPS LT
5470 : : #endif
5471 : :
5472 : : #ifdef POINTERS_EXTEND_UNSIGNED
5473 : : # define STACK_UNSIGNED POINTERS_EXTEND_UNSIGNED
5474 : : #else
5475 : : # define STACK_UNSIGNED true
5476 : : #endif
5477 : :
5478 : : /* Expand a call to builtin function __builtin_stack_address. */
5479 : :
5480 : : static rtx
5481 : 3427 : expand_builtin_stack_address ()
5482 : : {
5483 : 3427 : rtx ret = convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
5484 : : STACK_UNSIGNED);
5485 : :
5486 : : #ifdef STACK_ADDRESS_OFFSET
5487 : : /* Unbias the stack pointer, bringing it to the boundary between the
5488 : : stack area claimed by the active function calling this builtin,
5489 : : and stack ranges that could get clobbered if it called another
5490 : : function. It should NOT encompass any stack red zone, that is
5491 : : used in leaf functions.
5492 : :
5493 : : On SPARC, the register save area is *not* considered active or
5494 : : used by the active function, but rather as akin to the area in
5495 : : which call-preserved registers are saved by callees. This
5496 : : enables __strub_leave to clear what would otherwise overlap with
5497 : : its own register save area.
5498 : :
5499 : : If the address is computed too high or too low, parts of a stack
5500 : : range that should be scrubbed may be left unscrubbed, scrubbing
5501 : : may corrupt active portions of the stack frame, and stack ranges
5502 : : may be doubly-scrubbed by caller and callee.
5503 : :
5504 : : In order for it to be just right, the area delimited by
5505 : : @code{__builtin_stack_address} and @code{__builtin_frame_address
5506 : : (0)} should encompass caller's registers saved by the function,
5507 : : local on-stack variables and @code{alloca} stack areas.
5508 : : Accumulated outgoing on-stack arguments, preallocated as part of
5509 : : a function's own prologue, are to be regarded as part of the
5510 : : (caller) function's active area as well, whereas those pushed or
5511 : : allocated temporarily for a call are regarded as part of the
5512 : : callee's stack range, rather than the caller's. */
5513 : : ret = plus_constant (ptr_mode, ret, STACK_ADDRESS_OFFSET);
5514 : : #endif
5515 : :
5516 : 3427 : return force_reg (ptr_mode, ret);
5517 : : }
5518 : :
5519 : : /* Expand a call to builtin function __builtin_strub_enter. */
5520 : :
5521 : : static rtx
5522 : 2159 : expand_builtin_strub_enter (tree exp)
5523 : : {
5524 : 2159 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5525 : : return NULL_RTX;
5526 : :
5527 : 2159 : if (optimize < 1 || flag_no_inline)
5528 : : return NULL_RTX;
5529 : :
5530 : 1515 : rtx stktop = expand_builtin_stack_address ();
5531 : :
5532 : 1515 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5533 : 1515 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5534 : 1515 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5535 : : build_int_cst (TREE_TYPE (wmptr), 0));
5536 : 1515 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5537 : :
5538 : 1515 : emit_move_insn (wmark, stktop);
5539 : :
5540 : 1515 : return const0_rtx;
5541 : : }
5542 : :
5543 : : /* Expand a call to builtin function __builtin_strub_update. */
5544 : :
5545 : : static rtx
5546 : 1071 : expand_builtin_strub_update (tree exp)
5547 : : {
5548 : 1071 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5549 : : return NULL_RTX;
5550 : :
5551 : 1071 : if (optimize < 2 || flag_no_inline)
5552 : : return NULL_RTX;
5553 : :
5554 : 652 : rtx stktop = expand_builtin_stack_address ();
5555 : :
5556 : : #ifdef RED_ZONE_SIZE
5557 : : /* Here's how the strub enter, update and leave functions deal with red zones.
5558 : :
5559 : : If it weren't for red zones, update, called from within a strub context,
5560 : : would bump the watermark to the top of the stack. Enter and leave, running
5561 : : in the caller, would use the caller's top of stack address both to
5562 : : initialize the watermark passed to the callee, and to start strubbing the
5563 : : stack afterwards.
5564 : :
5565 : : Ideally, we'd update the watermark so as to cover the used amount of red
5566 : : zone, and strub starting at the caller's other end of the (presumably
5567 : : unused) red zone. Normally, only leaf functions use the red zone, but at
5568 : : this point we can't tell whether a function is a leaf, nor can we tell how
5569 : : much of the red zone it uses. Furthermore, some strub contexts may have
5570 : : been inlined so that update and leave are called from the same stack frame,
5571 : : and the strub builtins may all have been inlined, turning a strub function
5572 : : into a leaf.
5573 : :
5574 : : So cleaning the range from the caller's stack pointer (one end of the red
5575 : : zone) to the (potentially inlined) callee's (other end of the) red zone
5576 : : could scribble over the caller's own red zone.
5577 : :
5578 : : We avoid this possibility by arranging for callers that are strub contexts
5579 : : to use their own watermark as the strub starting point. So, if A calls B,
5580 : : and B calls C, B will tell A to strub up to the end of B's red zone, and
5581 : : will strub itself only the part of C's stack frame and red zone that
5582 : : doesn't overlap with B's. With that, we don't need to know who's leaf and
5583 : : who isn't: inlined calls will shrink their strub window to zero, each
5584 : : remaining call will strub some portion of the stack, and eventually the
5585 : : strub context will return to a caller that isn't a strub context itself,
5586 : : that will therefore use its own stack pointer as the strub starting point.
5587 : : It's not a leaf, because strub contexts can't be inlined into non-strub
5588 : : contexts, so it doesn't use the red zone, and it will therefore correctly
5589 : : strub up the callee's stack frame up to the end of the callee's red zone.
5590 : : Neat! */
5591 : 652 : if (true /* (flags_from_decl_or_type (current_function_decl) & ECF_LEAF) */)
5592 : : {
5593 : 652 : poly_int64 red_zone_size = RED_ZONE_SIZE;
5594 : : #if STACK_GROWS_DOWNWARD
5595 : 652 : red_zone_size = -red_zone_size;
5596 : : #endif
5597 : 652 : stktop = plus_constant (ptr_mode, stktop, red_zone_size);
5598 : 652 : stktop = force_reg (ptr_mode, stktop);
5599 : : }
5600 : : #endif
5601 : :
5602 : 652 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5603 : 652 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5604 : 652 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5605 : : build_int_cst (TREE_TYPE (wmptr), 0));
5606 : 652 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5607 : :
5608 : 652 : rtx wmarkr = force_reg (ptr_mode, wmark);
5609 : :
5610 : 652 : rtx_code_label *lab = gen_label_rtx ();
5611 : 652 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5612 : : ptr_mode, NULL_RTX, lab, NULL,
5613 : : profile_probability::very_likely ());
5614 : 652 : emit_move_insn (wmark, stktop);
5615 : :
5616 : : /* If this is an inlined strub function, also bump the watermark for the
5617 : : enclosing function. This avoids a problem with the following scenario: A
5618 : : calls B and B calls C, and both B and C get inlined into A. B allocates
5619 : : temporary stack space before calling C. If we don't update A's watermark,
5620 : : we may use an outdated baseline for the post-C strub_leave, erasing B's
5621 : : temporary stack allocation. We only need this if we're fully expanding
5622 : : strub_leave inline. */
5623 : 652 : tree xwmptr = (optimize > 2
5624 : 652 : ? strub_watermark_parm (current_function_decl)
5625 : : : wmptr);
5626 : 652 : if (wmptr != xwmptr)
5627 : : {
5628 : 156 : wmptr = xwmptr;
5629 : 156 : wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5630 : 156 : wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5631 : : build_int_cst (TREE_TYPE (wmptr), 0));
5632 : 156 : wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5633 : 156 : wmarkr = force_reg (ptr_mode, wmark);
5634 : :
5635 : 156 : do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5636 : : ptr_mode, NULL_RTX, lab, NULL,
5637 : : profile_probability::very_likely ());
5638 : 156 : emit_move_insn (wmark, stktop);
5639 : : }
5640 : :
5641 : 652 : emit_label (lab);
5642 : :
5643 : 652 : return const0_rtx;
5644 : : }
5645 : :
5646 : :
5647 : : /* Expand a call to builtin function __builtin_strub_leave. */
5648 : :
5649 : : static rtx
5650 : 2729 : expand_builtin_strub_leave (tree exp)
5651 : : {
5652 : 2729 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5653 : : return NULL_RTX;
5654 : :
5655 : 2729 : if (optimize < 2 || optimize_size || flag_no_inline)
5656 : : return NULL_RTX;
5657 : :
5658 : 1229 : rtx stktop = NULL_RTX;
5659 : :
5660 : 1229 : if (tree wmptr = (optimize
5661 : 1229 : ? strub_watermark_parm (current_function_decl)
5662 : : : NULL_TREE))
5663 : : {
5664 : 509 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5665 : 509 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5666 : : build_int_cst (TREE_TYPE (wmptr), 0));
5667 : 509 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5668 : 509 : stktop = force_reg (ptr_mode, wmark);
5669 : : }
5670 : :
5671 : 509 : if (!stktop)
5672 : 720 : stktop = expand_builtin_stack_address ();
5673 : :
5674 : 1229 : tree wmptr = CALL_EXPR_ARG (exp, 0);
5675 : 1229 : tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5676 : 1229 : tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5677 : : build_int_cst (TREE_TYPE (wmptr), 0));
5678 : 1229 : rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5679 : :
5680 : 1229 : rtx wmarkr = force_reg (ptr_mode, wmark);
5681 : :
5682 : : #if ! STACK_GROWS_DOWNWARD
5683 : : rtx base = stktop;
5684 : : rtx end = wmarkr;
5685 : : #else
5686 : 1229 : rtx base = wmarkr;
5687 : 1229 : rtx end = stktop;
5688 : : #endif
5689 : :
5690 : : /* We're going to modify it, so make sure it's not e.g. the stack pointer. */
5691 : 1229 : base = copy_to_reg (base);
5692 : :
5693 : 1229 : rtx_code_label *done = gen_label_rtx ();
5694 : 1229 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5695 : : ptr_mode, NULL_RTX, done, NULL,
5696 : : profile_probability::very_likely ());
5697 : :
5698 : 1229 : if (optimize < 3)
5699 : 909 : expand_call (exp, NULL_RTX, true);
5700 : : else
5701 : : {
5702 : : /* Ok, now we've determined we want to copy the block, so convert the
5703 : : addresses to Pmode, as needed to dereference them to access ptr_mode
5704 : : memory locations, so that we don't have to convert anything within the
5705 : : loop. */
5706 : 320 : base = memory_address (ptr_mode, base);
5707 : 320 : end = memory_address (ptr_mode, end);
5708 : :
5709 : 320 : rtx zero = force_operand (const0_rtx, NULL_RTX);
5710 : 320 : int ulen = GET_MODE_SIZE (ptr_mode);
5711 : :
5712 : : /* ??? It would be nice to use setmem or similar patterns here,
5713 : : but they do not necessarily obey the stack growth direction,
5714 : : which has security implications. We also have to avoid calls
5715 : : (memset, bzero or any machine-specific ones), which are
5716 : : likely unsafe here (see TARGET_STRUB_MAY_USE_MEMSET). */
5717 : : #if ! STACK_GROWS_DOWNWARD
5718 : : rtx incr = plus_constant (Pmode, base, ulen);
5719 : : rtx dstm = gen_rtx_MEM (ptr_mode, base);
5720 : :
5721 : : rtx_code_label *loop = gen_label_rtx ();
5722 : : emit_label (loop);
5723 : : emit_move_insn (dstm, zero);
5724 : : emit_move_insn (base, force_operand (incr, NULL_RTX));
5725 : : #else
5726 : 320 : rtx decr = plus_constant (Pmode, end, -ulen);
5727 : 320 : rtx dstm = gen_rtx_MEM (ptr_mode, end);
5728 : :
5729 : 320 : rtx_code_label *loop = gen_label_rtx ();
5730 : 320 : emit_label (loop);
5731 : 320 : emit_move_insn (end, force_operand (decr, NULL_RTX));
5732 : 320 : emit_move_insn (dstm, zero);
5733 : : #endif
5734 : 640 : do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5735 : 320 : Pmode, NULL_RTX, NULL, loop,
5736 : : profile_probability::very_likely ());
5737 : : }
5738 : :
5739 : 1229 : emit_label (done);
5740 : :
5741 : 1229 : return const0_rtx;
5742 : : }
5743 : :
5744 : : /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5745 : : failed and the caller should emit a normal call. */
5746 : :
5747 : : static rtx
5748 : 25566 : expand_builtin_alloca (tree exp)
5749 : : {
5750 : 25566 : rtx op0;
5751 : 25566 : rtx result;
5752 : 25566 : unsigned int align;
5753 : 25566 : tree fndecl = get_callee_fndecl (exp);
5754 : 25566 : HOST_WIDE_INT max_size;
5755 : 25566 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5756 : 25566 : bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5757 : 25566 : bool valid_arglist
5758 : : = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5759 : 25566 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5760 : : VOID_TYPE)
5761 : : : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5762 : 25564 : ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5763 : 25566 : : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5764 : :
5765 : 25566 : if (!valid_arglist)
5766 : : return NULL_RTX;
5767 : :
5768 : : /* Compute the argument. */
5769 : 25561 : op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5770 : :
5771 : : /* Compute the alignment. */
5772 : 47316 : align = (fcode == BUILT_IN_ALLOCA
5773 : 21756 : ? BIGGEST_ALIGNMENT
5774 : 3805 : : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5775 : :
5776 : : /* Compute the maximum size. */
5777 : 2 : max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5778 : 25563 : ? 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 : 25561 : result
5784 : 25561 : = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5785 : 25561 : result = convert_memory_address (ptr_mode, result);
5786 : :
5787 : : /* Dynamic allocations for variables are recorded during gimplification. */
5788 : 25561 : 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 : 1163 : expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5825 : : rtx subtarget)
5826 : : {
5827 : 1163 : tree arg;
5828 : 1163 : rtx op0;
5829 : :
5830 : 1163 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5831 : : return NULL_RTX;
5832 : :
5833 : 1163 : arg = CALL_EXPR_ARG (exp, 0);
5834 : 1163 : op0 = expand_expr (arg,
5835 : 50 : subtarget && GET_MODE (subtarget) == target_mode
5836 : : ? subtarget : NULL_RTX,
5837 : : target_mode, EXPAND_NORMAL);
5838 : 1163 : if (GET_MODE (op0) != target_mode)
5839 : 0 : op0 = convert_to_mode (target_mode, op0, 1);
5840 : :
5841 : 1163 : target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5842 : :
5843 : 1163 : gcc_assert (target);
5844 : :
5845 : 1163 : return convert_to_mode (target_mode, target, 1);
5846 : : }
5847 : :
5848 : : /* Expand a call to a unary builtin in EXP.
5849 : : Return NULL_RTX if a normal call should be emitted rather than expanding the
5850 : : function in-line. If convenient, the result should be placed in TARGET.
5851 : : SUBTARGET may be used as the target for computing one of EXP's operands. */
5852 : :
5853 : : static rtx
5854 : 751 : expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5855 : : rtx subtarget, optab op_optab)
5856 : : {
5857 : 751 : rtx op0;
5858 : :
5859 : 751 : if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5860 : : return NULL_RTX;
5861 : :
5862 : : /* Compute the argument. */
5863 : 1502 : op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5864 : : (subtarget
5865 : 94 : && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5866 : 94 : == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5867 : : VOIDmode, EXPAND_NORMAL);
5868 : : /* Compute op, into TARGET if possible.
5869 : : Set TARGET to wherever the result comes back. */
5870 : 751 : target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5871 : : op_optab, op0, target, op_optab != clrsb_optab);
5872 : 751 : gcc_assert (target);
5873 : :
5874 : 751 : return convert_to_mode (target_mode, target, 0);
5875 : : }
5876 : :
5877 : : /* Expand a call to __builtin_expect. We just return our argument
5878 : : as the builtin_expect semantic should've been already executed by
5879 : : tree branch prediction pass. */
5880 : :
5881 : : static rtx
5882 : 1025 : expand_builtin_expect (tree exp, rtx target)
5883 : : {
5884 : 1025 : tree arg;
5885 : :
5886 : 1025 : if (call_expr_nargs (exp) < 2)
5887 : 0 : return const0_rtx;
5888 : 1025 : arg = CALL_EXPR_ARG (exp, 0);
5889 : :
5890 : 1025 : target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5891 : : /* When guessing was done, the hints should be already stripped away. */
5892 : 1025 : 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 : 37986 : expand_builtin_trap (void)
5937 : : {
5938 : 37986 : if (targetm.have_trap ())
5939 : : {
5940 : 37986 : 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 : 37986 : if (!ACCUMULATE_OUTGOING_ARGS)
5945 : 37984 : 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 : 37986 : emit_barrier ();
5955 : 37986 : }
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 : 5311 : expand_builtin_unreachable (void)
5964 : : {
5965 : : /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5966 : : to avoid this. */
5967 : 5311 : gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5968 : 5311 : emit_barrier ();
5969 : 5311 : }
5970 : :
5971 : : /* Expand EXP, a call to fabs, fabsf or fabsl.
5972 : : Return NULL_RTX if a normal call should be emitted rather than expanding
5973 : : the function inline. If convenient, the result should be placed
5974 : : in TARGET. SUBTARGET may be used as the target for computing
5975 : : the operand. */
5976 : :
5977 : : static rtx
5978 : 4 : expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5979 : : {
5980 : 4 : machine_mode mode;
5981 : 4 : tree arg;
5982 : 4 : rtx op0;
5983 : :
5984 : 4 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5985 : : return NULL_RTX;
5986 : :
5987 : 0 : arg = CALL_EXPR_ARG (exp, 0);
5988 : 0 : CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5989 : 0 : mode = TYPE_MODE (TREE_TYPE (arg));
5990 : 0 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5991 : 0 : return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5992 : : }
5993 : :
5994 : : /* Expand EXP, a call to copysign, copysignf, or copysignl.
5995 : : Return NULL is a normal call should be emitted rather than expanding the
5996 : : function inline. If convenient, the result should be placed in TARGET.
5997 : : SUBTARGET may be used as the target for computing the operand. */
5998 : :
5999 : : static rtx
6000 : 11674 : expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
6001 : : {
6002 : 11674 : rtx op0, op1;
6003 : 11674 : tree arg;
6004 : :
6005 : 11674 : if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6006 : : return NULL_RTX;
6007 : :
6008 : 11673 : arg = CALL_EXPR_ARG (exp, 0);
6009 : 11673 : op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6010 : :
6011 : 11673 : arg = CALL_EXPR_ARG (exp, 1);
6012 : 11673 : op1 = expand_normal (arg);
6013 : :
6014 : 11673 : return expand_copysign (op0, op1, target);
6015 : : }
6016 : :
6017 : : /* Emit a call to __builtin___clear_cache. */
6018 : :
6019 : : void
6020 : 0 : default_emit_call_builtin___clear_cache (rtx begin, rtx end)
6021 : : {
6022 : 0 : rtx callee = gen_rtx_SYMBOL_REF (Pmode,
6023 : : BUILTIN_ASM_NAME_PTR
6024 : : (BUILT_IN_CLEAR_CACHE));
6025 : :
6026 : 0 : emit_library_call (callee,
6027 : : LCT_NORMAL, VOIDmode,
6028 : : convert_memory_address (ptr_mode, begin), ptr_mode,
6029 : : convert_memory_address (ptr_mode, end), ptr_mode);
6030 : 0 : }
6031 : :
6032 : : /* Emit a call to __builtin___clear_cache, unless the target specifies
6033 : : it as do-nothing. This function can be used by trampoline
6034 : : finalizers to duplicate the effects of expanding a call to the
6035 : : clear_cache builtin. */
6036 : :
6037 : : void
6038 : 28 : maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
6039 : : {
6040 : 28 : gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
6041 : : || CONST_INT_P (begin))
6042 : : && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
6043 : : || CONST_INT_P (end)));
6044 : :
6045 : 28 : if (targetm.have_clear_cache ())
6046 : : {
6047 : : /* We have a "clear_cache" insn, and it will handle everything. */
6048 : 0 : class expand_operand ops[2];
6049 : :
6050 : 0 : create_address_operand (&ops[0], begin);
6051 : 0 : create_address_operand (&ops[1], end);
6052 : :
6053 : 0 : if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
6054 : 0 : return;
6055 : : }
6056 : : else
6057 : : {
6058 : : #ifndef CLEAR_INSN_CACHE
6059 : : /* There is no "clear_cache" insn, and __clear_cache() in libgcc
6060 : : does nothing. There is no need to call it. Do nothing. */
6061 : : return;
6062 : : #endif /* CLEAR_INSN_CACHE */
6063 : : }
6064 : :
6065 : 0 : targetm.calls.emit_call_builtin___clear_cache (begin, end);
6066 : : }
6067 : :
6068 : : /* Expand a call to __builtin___clear_cache. */
6069 : :
6070 : : static void
6071 : 28 : expand_builtin___clear_cache (tree exp)
6072 : : {
6073 : 28 : tree begin, end;
6074 : 28 : rtx begin_rtx, end_rtx;
6075 : :
6076 : : /* We must not expand to a library call. If we did, any
6077 : : fallback library function in libgcc that might contain a call to
6078 : : __builtin___clear_cache() would recurse infinitely. */
6079 : 28 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6080 : : {
6081 : 0 : error ("both arguments to %<__builtin___clear_cache%> must be pointers");
6082 : 0 : return;
6083 : : }
6084 : :
6085 : 28 : begin = CALL_EXPR_ARG (exp, 0);
6086 : 30 : begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
6087 : :
6088 : 28 : end = CALL_EXPR_ARG (exp, 1);
6089 : 30 : end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
6090 : :
6091 : 28 : maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
6092 : : }
6093 : :
6094 : : /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
6095 : :
6096 : : static rtx
6097 : 623 : round_trampoline_addr (rtx tramp)
6098 : : {
6099 : 623 : rtx temp, addend, mask;
6100 : :
6101 : : /* If we don't need too much alignment, we'll have been guaranteed
6102 : : proper alignment by get_trampoline_type. */
6103 : 623 : if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
6104 : : return tramp;
6105 : :
6106 : : /* Round address up to desired boundary. */
6107 : 0 : temp = gen_reg_rtx (Pmode);
6108 : 0 : addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
6109 : 0 : mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
6110 : :
6111 : 0 : temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
6112 : : temp, 0, OPTAB_LIB_WIDEN);
6113 : 0 : tramp = expand_simple_binop (Pmode, AND, temp, mask,
6114 : : temp, 0, OPTAB_LIB_WIDEN);
6115 : :
6116 : 0 : return tramp;
6117 : : }
6118 : :
6119 : : static rtx
6120 : 290 : expand_builtin_init_trampoline (tree exp, bool onstack)
6121 : : {
6122 : 290 : tree t_tramp, t_func, t_chain;
6123 : 290 : rtx m_tramp, r_tramp, r_chain, tmp;
6124 : :
6125 : 290 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
6126 : : POINTER_TYPE, VOID_TYPE))
6127 : : return NULL_RTX;
6128 : :
6129 : 290 : t_tramp = CALL_EXPR_ARG (exp, 0);
6130 : 290 : t_func = CALL_EXPR_ARG (exp, 1);
6131 : 290 : t_chain = CALL_EXPR_ARG (exp, 2);
6132 : :
6133 : 290 : r_tramp = expand_normal (t_tramp);
6134 : 290 : m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
6135 : 290 : MEM_NOTRAP_P (m_tramp) = 1;
6136 : :
6137 : : /* If ONSTACK, the TRAMP argument should be the address of a field
6138 : : within the local function's FRAME decl. Either way, let's see if
6139 : : we can fill in the MEM_ATTRs for this memory. */
6140 : 290 : if (TREE_CODE (t_tramp) == ADDR_EXPR)
6141 : 290 : set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
6142 : :
6143 : : /* Creator of a heap trampoline is responsible for making sure the
6144 : : address is aligned to at least STACK_BOUNDARY. Normally malloc
6145 : : will ensure this anyhow. */
6146 : 290 : tmp = round_trampoline_addr (r_tramp);
6147 : 290 : if (tmp != r_tramp)
6148 : : {
6149 : 0 : m_tramp = change_address (m_tramp, BLKmode, tmp);
6150 : 0 : set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
6151 : 0 : set_mem_size (m_tramp, TRAMPOLINE_SIZE);
6152 : : }
6153 : :
6154 : : /* The FUNC argument should be the address of the nested function.
6155 : : Extract the actual function decl to pass to the hook. */
6156 : 290 : gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
6157 : 290 : t_func = TREE_OPERAND (t_func, 0);
6158 : 290 : gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
6159 : :
6160 : 290 : r_chain = expand_normal (t_chain);
6161 : :
6162 : : /* Generate insns to initialize the trampoline. */
6163 : 290 : targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
6164 : :
6165 : 290 : if (onstack)
6166 : : {
6167 : 290 : trampolines_created = 1;
6168 : :
6169 : 290 : if (targetm.calls.custom_function_descriptors != 0)
6170 : 290 : warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
6171 : : "trampoline generated for nested function %qD", t_func);
6172 : : }
6173 : :
6174 : 290 : return const0_rtx;
6175 : : }
6176 : :
6177 : : static rtx
6178 : 333 : expand_builtin_adjust_trampoline (tree exp)
6179 : : {
6180 : 333 : rtx tramp;
6181 : :
6182 : 333 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6183 : : return NULL_RTX;
6184 : :
6185 : 333 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6186 : 333 : tramp = round_trampoline_addr (tramp);
6187 : 333 : if (targetm.calls.trampoline_adjust_address)
6188 : 0 : tramp = targetm.calls.trampoline_adjust_address (tramp);
6189 : :
6190 : : return tramp;
6191 : : }
6192 : :
6193 : : /* Expand a call to the builtin descriptor initialization routine.
6194 : : A descriptor is made up of a couple of pointers to the static
6195 : : chain and the code entry in this order. */
6196 : :
6197 : : static rtx
6198 : 0 : expand_builtin_init_descriptor (tree exp)
6199 : : {
6200 : 0 : tree t_descr, t_func, t_chain;
6201 : 0 : rtx m_descr, r_descr, r_func, r_chain;
6202 : :
6203 : 0 : if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
6204 : : VOID_TYPE))
6205 : : return NULL_RTX;
6206 : :
6207 : 0 : t_descr = CALL_EXPR_ARG (exp, 0);
6208 : 0 : t_func = CALL_EXPR_ARG (exp, 1);
6209 : 0 : t_chain = CALL_EXPR_ARG (exp, 2);
6210 : :
6211 : 0 : r_descr = expand_normal (t_descr);
6212 : 0 : m_descr = gen_rtx_MEM (BLKmode, r_descr);
6213 : 0 : MEM_NOTRAP_P (m_descr) = 1;
6214 : 0 : set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
6215 : :
6216 : 0 : r_func = expand_normal (t_func);
6217 : 0 : r_chain = expand_normal (t_chain);
6218 : :
6219 : : /* Generate insns to initialize the descriptor. */
6220 : 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
6221 : 0 : emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
6222 : : POINTER_SIZE / BITS_PER_UNIT), r_func);
6223 : :
6224 : 0 : return const0_rtx;
6225 : : }
6226 : :
6227 : : /* Expand a call to the builtin descriptor adjustment routine. */
6228 : :
6229 : : static rtx
6230 : 0 : expand_builtin_adjust_descriptor (tree exp)
6231 : : {
6232 : 0 : rtx tramp;
6233 : :
6234 : 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6235 : : return NULL_RTX;
6236 : :
6237 : 0 : tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6238 : :
6239 : : /* Unalign the descriptor to allow runtime identification. */
6240 : 0 : tramp = plus_constant (ptr_mode, tramp,
6241 : 0 : targetm.calls.custom_function_descriptors);
6242 : :
6243 : 0 : return force_operand (tramp, NULL_RTX);
6244 : : }
6245 : :
6246 : : /* Expand the call EXP to the built-in signbit, signbitf or signbitl
6247 : : function. The function first checks whether the back end provides
6248 : : an insn to implement signbit for the respective mode. If not, it
6249 : : checks whether the floating point format of the value is such that
6250 : : the sign bit can be extracted. If that is not the case, error out.
6251 : : EXP is the expression that is a call to the builtin function; if
6252 : : convenient, the result should be placed in TARGET. */
6253 : : static rtx
6254 : 1133 : expand_builtin_signbit (tree exp, rtx target)
6255 : : {
6256 : 1133 : const struct real_format *fmt;
6257 : 1133 : scalar_float_mode fmode;
6258 : 1133 : scalar_int_mode rmode, imode;
6259 : 1133 : tree arg;
6260 : 1133 : int word, bitpos;
6261 : 1133 : enum insn_code icode;
6262 : 1133 : rtx temp;
6263 : 1133 : location_t loc = EXPR_LOCATION (exp);
6264 : :
6265 : 1133 : if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6266 : : return NULL_RTX;
6267 : :
6268 : 1133 : arg = CALL_EXPR_ARG (exp, 0);
6269 : 1133 : fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
6270 : 1133 : rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
6271 : 1133 : fmt = REAL_MODE_FORMAT (fmode);
6272 : :
6273 : 1133 : arg = builtin_save_expr (arg);
6274 : :
6275 : : /* Expand the argument yielding a RTX expression. */
6276 : 1133 : temp = expand_normal (arg);
6277 : :
6278 : : /* Check if the back end provides an insn that handles signbit for the
6279 : : argument's mode. */
6280 : 1133 : icode = optab_handler (signbit_optab, fmode);
6281 : 1133 : 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 : 1123 : bitpos = fmt->signbit_ro;
6293 : 1123 : 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 : 2246 : if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
6304 : : {
6305 : 1019 : imode = int_mode_for_mode (fmode).require ();
6306 : 1019 : 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 : 1123 : 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 : 2246 : if (bitpos < GET_MODE_BITSIZE (rmode))
6330 : : {
6331 : 915 : wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
6332 : :
6333 : 2745 : if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
6334 : 0 : temp = gen_lowpart (rmode, temp);
6335 : 1830 : temp = expand_binop (rmode, and_optab, temp,
6336 : 1830 : immed_wide_int_const (mask, rmode),
6337 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6338 : 915 : }
6339 : : else
6340 : : {
6341 : : /* Perform a logical right shift to place the signbit in the least
6342 : : significant bit, then truncate the result to the desired mode
6343 : : and mask just this bit. */
6344 : 208 : temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
6345 : 208 : temp = gen_lowpart (rmode, temp);
6346 : 208 : temp = expand_binop (rmode, and_optab, temp, const1_rtx,
6347 : : NULL_RTX, 1, OPTAB_LIB_WIDEN);
6348 : : }
6349 : :
6350 : : return temp;
6351 : : }
6352 : :
6353 : : /* Expand fork or exec calls. TARGET is the desired target of the
6354 : : call. EXP is the call. FN is the
6355 : : identificator of the actual function. IGNORE is nonzero if the
6356 : : value is to be ignored. */
6357 : :
6358 : : static rtx
6359 : 87 : expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
6360 : : {
6361 : 87 : tree id, decl;
6362 : 87 : tree call;
6363 : :
6364 : : /* If we are not profiling, just call the function. */
6365 : 87 : if (!coverage_instrumentation_p ())
6366 : : return NULL_RTX;
6367 : :
6368 : : /* Otherwise call the wrapper. This should be equivalent for the rest of
6369 : : compiler, so the code does not diverge, and the wrapper may run the
6370 : : code necessary for keeping the profiling sane. */
6371 : :
6372 : 4 : switch (DECL_FUNCTION_CODE (fn))
6373 : : {
6374 : 4 : case BUILT_IN_FORK:
6375 : 4 : id = get_identifier ("__gcov_fork");
6376 : 4 : break;
6377 : :
6378 : 0 : case BUILT_IN_EXECL:
6379 : 0 : id = get_identifier ("__gcov_execl");
6380 : 0 : break;
6381 : :
6382 : 0 : case BUILT_IN_EXECV:
6383 : 0 : id = get_identifier ("__gcov_execv");
6384 : 0 : break;
6385 : :
6386 : 0 : case BUILT_IN_EXECLP:
6387 : 0 : id = get_identifier ("__gcov_execlp");
6388 : 0 : break;
6389 : :
6390 : 0 : case BUILT_IN_EXECLE:
6391 : 0 : id = get_identifier ("__gcov_execle");
6392 : 0 : break;
6393 : :
6394 : 0 : case BUILT_IN_EXECVP:
6395 : 0 : id = get_identifier ("__gcov_execvp");
6396 : 0 : break;
6397 : :
6398 : 0 : case BUILT_IN_EXECVE:
6399 : 0 : id = get_identifier ("__gcov_execve");
6400 : 0 : break;
6401 : :
6402 : 0 : default:
6403 : 0 : gcc_unreachable ();
6404 : : }
6405 : :
6406 : 4 : decl = build_decl (DECL_SOURCE_LOCATION (fn),
6407 : 4 : FUNCTION_DECL, id, TREE_TYPE (fn));
6408 : 4 : DECL_EXTERNAL (decl) = 1;
6409 : 4 : TREE_PUBLIC (decl) = 1;
6410 : 4 : DECL_ARTIFICIAL (decl) = 1;
6411 : 4 : TREE_NOTHROW (decl) = 1;
6412 : 4 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6413 : 4 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
6414 : 4 : call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
6415 : 4 : return expand_call (call, target, ignore);
6416 : : }
6417 : :
6418 : :
6419 : :
6420 : : /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6421 : : the pointer in these functions is void*, the tree optimizers may remove
6422 : : casts. The mode computed in expand_builtin isn't reliable either, due
6423 : : to __sync_bool_compare_and_swap.
6424 : :
6425 : : FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6426 : : group of builtins. This gives us log2 of the mode size. */
6427 : :
6428 : : static inline machine_mode
6429 : 135260 : 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 : 135260 : 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 : 151516 : get_builtin_sync_mem (tree loc, machine_mode mode)
6441 : : {
6442 : 151516 : rtx addr, mem;
6443 : 151516 : int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
6444 : : ? TREE_TYPE (TREE_TYPE (loc))
6445 : : : TREE_TYPE (loc));
6446 : 151516 : scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
6447 : :
6448 : 151516 : addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
6449 : 151516 : 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 : 151516 : mem = gen_rtx_MEM (mode, addr);
6455 : :
6456 : 151516 : set_mem_addr_space (mem, addr_space);
6457 : :
6458 : 151516 : mem = validize_mem (mem);
6459 : :
6460 : : /* The alignment needs to be at least according to that of the mode. */
6461 : 151516 : set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6462 : : get_pointer_alignment (loc)));
6463 : 151516 : set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6464 : 151516 : MEM_VOLATILE_P (mem) = 1;
6465 : :
6466 : 151516 : 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 : 94655 : expand_expr_force_mode (tree exp, machine_mode mode)
6475 : : {
6476 : 94655 : rtx val;
6477 : 94655 : machine_mode old_mode;
6478 : :
6479 : 94655 : if (TREE_CODE (exp) == SSA_NAME
6480 : 94655 : && TYPE_MODE (TREE_TYPE (exp)) != mode)
6481 : : {
6482 : : /* Undo argument promotion if possible, as combine might not
6483 : : be able to do it later due to MEM_VOLATILE_P uses in the
6484 : : patterns. */
6485 : 25 : gimple *g = get_gimple_for_ssa_name (exp);
6486 : 25 : if (g && gimple_assign_cast_p (g))
6487 : : {
6488 : 6 : tree rhs = gimple_assign_rhs1 (g);
6489 : 6 : tree_code code = gimple_assign_rhs_code (g);
6490 : 6 : if (CONVERT_EXPR_CODE_P (code)
6491 : 6 : && TYPE_MODE (TREE_TYPE (rhs)) == mode
6492 : 6 : && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6493 : 6 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6494 : 12 : && (TYPE_PRECISION (TREE_TYPE (exp))
6495 : 6 : > TYPE_PRECISION (TREE_TYPE (rhs))))
6496 : : exp = rhs;
6497 : : }
6498 : : }
6499 : :
6500 : 94655 : 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 : 94655 : old_mode = GET_MODE (val);
6505 : 94655 : if (old_mode == VOIDmode)
6506 : 45180 : old_mode = TYPE_MODE (TREE_TYPE (exp));
6507 : 94655 : val = convert_modes (mode, old_mode, val, 1);
6508 : 94655 : return val;
6509 : : }
6510 : :
6511 : :
6512 : : /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6513 : : EXP is the CALL_EXPR. CODE is the rtx code
6514 : : that corresponds to the arithmetic or logical operation from the name;
6515 : : an exception here is that NOT actually means NAND. TARGET is an optional
6516 : : place for us to store the results; AFTER is true if this is the
6517 : : fetch_and_xxx form. */
6518 : :
6519 : : static rtx
6520 : 3997 : expand_builtin_sync_operation (machine_mode mode, tree exp,
6521 : : enum rtx_code code, bool after,
6522 : : rtx target)
6523 : : {
6524 : 3997 : rtx val, mem;
6525 : 3997 : location_t loc = EXPR_LOCATION (exp);
6526 : :
6527 : 3997 : if (code == NOT && warn_sync_nand)
6528 : : {
6529 : 517 : tree fndecl = get_callee_fndecl (exp);
6530 : 517 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6531 : :
6532 : 517 : static bool warned_f_a_n, warned_n_a_f;
6533 : :
6534 : 517 : switch (fcode)
6535 : : {
6536 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6537 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6538 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6539 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6540 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6541 : 434 : if (warned_f_a_n)
6542 : : break;
6543 : :
6544 : 30 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6545 : 30 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6546 : 30 : warned_f_a_n = true;
6547 : 30 : break;
6548 : :
6549 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6550 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6551 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6552 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6553 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6554 : 83 : if (warned_n_a_f)
6555 : : break;
6556 : :
6557 : 16 : fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6558 : 16 : inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6559 : 16 : warned_n_a_f = true;
6560 : 16 : break;
6561 : :
6562 : 0 : default:
6563 : 0 : gcc_unreachable ();
6564 : : }
6565 : : }
6566 : :
6567 : : /* Expand the operands. */
6568 : 3997 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6569 : 3997 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6570 : :
6571 : 3997 : return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6572 : 3997 : after);
6573 : : }
6574 : :
6575 : : /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6576 : : intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6577 : : true if this is the boolean form. TARGET is a place for us to store the
6578 : : results; this is NOT optional if IS_BOOL is true. */
6579 : :
6580 : : static rtx
6581 : 456 : expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6582 : : bool is_bool, rtx target)
6583 : : {
6584 : 456 : rtx old_val, new_val, mem;
6585 : 456 : rtx *pbool, *poval;
6586 : :
6587 : : /* Expand the operands. */
6588 : 456 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6589 : 456 : old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6590 : 456 : new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6591 : :
6592 : 456 : pbool = poval = NULL;
6593 : 456 : if (target != const0_rtx)
6594 : : {
6595 : 430 : if (is_bool)
6596 : : pbool = ⌖
6597 : : else
6598 : 230 : poval = ⌖
6599 : : }
6600 : 456 : if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6601 : : false, MEMMODEL_SYNC_SEQ_CST,
6602 : : MEMMODEL_SYNC_SEQ_CST))
6603 : : return NULL_RTX;
6604 : :
6605 : 454 : return target;
6606 : : }
6607 : :
6608 : : /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6609 : : general form is actually an atomic exchange, and some targets only
6610 : : support a reduced form with the second argument being a constant 1.
6611 : : EXP is the CALL_EXPR; TARGET is an optional place for us to store
6612 : : the results. */
6613 : :
6614 : : static rtx
6615 : 326 : expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6616 : : rtx target)
6617 : : {
6618 : 326 : rtx val, mem;
6619 : :
6620 : : /* Expand the operands. */
6621 : 326 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6622 : 326 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6623 : :
6624 : 326 : return expand_sync_lock_test_and_set (target, mem, val);
6625 : : }
6626 : :
6627 : : /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6628 : :
6629 : : static rtx
6630 : 158 : expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6631 : : {
6632 : 158 : rtx mem;
6633 : :
6634 : : /* Expand the operands. */
6635 : 158 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6636 : :
6637 : 158 : return expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6638 : : }
6639 : :
6640 : : /* Given an integer representing an ``enum memmodel'', verify its
6641 : : correctness and return the memory model enum. */
6642 : :
6643 : : static enum memmodel
6644 : 169902 : 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 : 169902 : if (TREE_CODE (exp) != INTEGER_CST)
6649 : : return MEMMODEL_SEQ_CST;
6650 : :
6651 : 169059 : rtx op = expand_normal (exp);
6652 : :
6653 : 169059 : unsigned HOST_WIDE_INT val = INTVAL (op);
6654 : 169059 : if (targetm.memmodel_check)
6655 : 169059 : 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 : 169059 : 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 : 169058 : if (val == MEMMODEL_CONSUME)
6666 : 750 : val = MEMMODEL_ACQUIRE;
6667 : :
6668 : 169058 : return (enum memmodel) val;
6669 : : }
6670 : :
6671 : : /* Expand the __atomic_exchange intrinsic:
6672 : : TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6673 : : EXP is the CALL_EXPR.
6674 : : TARGET is an optional place for us to store the results. */
6675 : :
6676 : : static rtx
6677 : 2811 : expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6678 : : {
6679 : 2811 : rtx val, mem;
6680 : 2811 : enum memmodel model;
6681 : :
6682 : 2811 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6683 : :
6684 : 2811 : if (!flag_inline_atomics)
6685 : : return NULL_RTX;
6686 : :
6687 : : /* Expand the operands. */
6688 : 2760 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6689 : 2760 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6690 : :
6691 : 2760 : return expand_atomic_exchange (target, mem, val, model);
6692 : : }
6693 : :
6694 : : /* Expand the __atomic_compare_exchange intrinsic:
6695 : : bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6696 : : TYPE desired, BOOL weak,
6697 : : enum memmodel success,
6698 : : enum memmodel failure)
6699 : : EXP is the CALL_EXPR.
6700 : : TARGET is an optional place for us to store the results. */
6701 : :
6702 : : static rtx
6703 : 9204 : expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6704 : : rtx target)
6705 : : {
6706 : 9204 : rtx expect, desired, mem, oldval;
6707 : 9204 : rtx_code_label *label;
6708 : 9204 : tree weak;
6709 : 9204 : bool is_weak;
6710 : :
6711 : 9204 : memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6712 : 9204 : memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6713 : :
6714 : 9204 : if (failure > success)
6715 : 22 : success = MEMMODEL_SEQ_CST;
6716 : :
6717 : 9204 : 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 : 9204 : if (!flag_inline_atomics)
6725 : : return NULL_RTX;
6726 : :
6727 : : /* Expand the operands. */
6728 : 9153 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6729 : :
6730 : 9153 : expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6731 : 9153 : expect = convert_memory_address (Pmode, expect);
6732 : 9153 : expect = gen_rtx_MEM (mode, expect);
6733 : 9153 : desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6734 : :
6735 : 9153 : weak = CALL_EXPR_ARG (exp, 3);
6736 : 9153 : is_weak = false;
6737 : 9153 : if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6738 : 9153 : is_weak = true;
6739 : :
6740 : 9153 : if (target == const0_rtx)
6741 : 282 : 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 : 9153 : oldval = NULL;
6746 : :
6747 : 9153 : 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 : 7410 : label = gen_label_rtx ();
6757 : 7410 : emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6758 : 7410 : GET_MODE (target), 1, label);
6759 : 7410 : emit_move_insn (expect, oldval);
6760 : 7410 : emit_label (label);
6761 : :
6762 : 7410 : 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 : 13731 : expand_ifn_atomic_compare_exchange (gcall *call)
6819 : : {
6820 : 13731 : int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6821 : 13731 : gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6822 : 13731 : machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6823 : :
6824 : 13731 : memmodel success = get_memmodel (gimple_call_arg (call, 4));
6825 : 13731 : memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6826 : :
6827 : 13731 : if (failure > success)
6828 : 0 : success = MEMMODEL_SEQ_CST;
6829 : :
6830 : 13731 : if (is_mm_release (failure) || is_mm_acq_rel (failure))
6831 : : {
6832 : : failure = MEMMODEL_SEQ_CST;
6833 : : success = MEMMODEL_SEQ_CST;
6834 : : }
6835 : :
6836 : 13731 : 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 : 13731 : rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6844 : :
6845 : 13731 : rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6846 : 13731 : rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6847 : :
6848 : 13731 : bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6849 : :
6850 : 13731 : rtx boolret = NULL;
6851 : 13731 : rtx oldval = NULL;
6852 : :
6853 : 13731 : 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 : 13731 : tree lhs = gimple_call_lhs (call);
6861 : 13731 : if (lhs)
6862 : : {
6863 : 13531 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6864 : 13531 : if (GET_MODE (boolret) != mode)
6865 : 12041 : boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6866 : 13531 : write_complex_part (target, boolret, true, true);
6867 : 13531 : 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 : 70615 : expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6878 : : {
6879 : 70615 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6880 : 70615 : if (is_mm_release (model) || is_mm_acq_rel (model))
6881 : : model = MEMMODEL_SEQ_CST;
6882 : :
6883 : 70615 : if (!flag_inline_atomics)
6884 : : return NULL_RTX;
6885 : :
6886 : : /* Expand the operand. */
6887 : 70574 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6888 : :
6889 : 70574 : 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 : 17481 : expand_builtin_atomic_store (machine_mode mode, tree exp)
6900 : : {
6901 : 17481 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6902 : 17481 : if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6903 : 1585 : || is_mm_release (model)))
6904 : : model = MEMMODEL_SEQ_CST;
6905 : :
6906 : 17481 : if (!flag_inline_atomics)
6907 : : return NULL_RTX;
6908 : :
6909 : : /* Expand the operands. */
6910 : 17450 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6911 : 17450 : rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6912 : :
6913 : 17450 : 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 : 30182 : 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 : 30182 : rtx val, mem, ret;
6933 : 30182 : enum memmodel model;
6934 : 30182 : tree fndecl;
6935 : 30182 : tree addr;
6936 : :
6937 : 30182 : model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6938 : :
6939 : : /* Expand the operands. */
6940 : 30182 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6941 : 30182 : val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6942 : :
6943 : : /* Only try generating instructions if inlining is turned on. */
6944 : 30182 : if (flag_inline_atomics)
6945 : : {
6946 : 29639 : ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6947 : 29639 : 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 : 1986 : expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
7099 : : {
7100 : 1986 : tree cmp = gimple_call_arg (call, 0);
7101 : 1986 : tree ptr = gimple_call_arg (call, 1);
7102 : 1986 : tree arg = gimple_call_arg (call, 2);
7103 : 1986 : tree lhs = gimple_call_lhs (call);
7104 : 1986 : enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7105 : 1986 : machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
7106 : 1986 : optab optab;
7107 : 1986 : rtx_code code;
7108 : 1986 : class expand_operand ops[5];
7109 : :
7110 : 1986 : gcc_assert (flag_inline_atomics);
7111 : :
7112 : 1986 : if (gimple_call_num_args (call) == 5)
7113 : 1578 : model = get_memmodel (gimple_call_arg (call, 3));
7114 : :
7115 : 1986 : rtx mem = get_builtin_sync_mem (ptr, mode);
7116 : 1986 : rtx op = expand_expr_force_mode (arg, mode);
7117 : :
7118 : 1986 : 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 : 1986 : enum rtx_code comp = UNKNOWN;
7145 : 1986 : 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 : 1986 : rtx target;
7157 : 1986 : if (lhs == NULL_TREE)
7158 : 0 : target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
7159 : : else
7160 : 1986 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7161 : 1986 : enum insn_code icode = direct_optab_handler (optab, mode);
7162 : 1986 : gcc_assert (icode != CODE_FOR_nothing);
7163 : 1986 : create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
7164 : 1986 : create_fixed_operand (&ops[1], mem);
7165 : 1986 : create_convert_operand_to (&ops[2], op, mode, true);
7166 : 1986 : create_integer_operand (&ops[3], model);
7167 : 1986 : create_integer_operand (&ops[4], comp);
7168 : 1986 : if (maybe_expand_insn (icode, 5, ops))
7169 : 1954 : return;
7170 : :
7171 : 32 : rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
7172 : : code, model, true);
7173 : 32 : if (!result)
7174 : : {
7175 : 0 : bool is_atomic = gimple_call_num_args (call) == 5;
7176 : 0 : tree tcall = gimple_call_arg (call, 3 + is_atomic);
7177 : 0 : tree fndecl = gimple_call_addr_fndecl (tcall);
7178 : 0 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
7179 : 0 : tree exp = build_call_nary (type, tcall,
7180 : : 2 + is_atomic, ptr, arg,
7181 : : is_atomic
7182 : 0 : ? gimple_call_arg (call, 3)
7183 : : : integer_zero_node);
7184 : 0 : result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7185 : : mode, !lhs);
7186 : : }
7187 : :
7188 : 32 : if (lhs)
7189 : : {
7190 : 32 : result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
7191 : : 0, 1);
7192 : 32 : if (result != target)
7193 : 32 : emit_move_insn (target, result);
7194 : : }
7195 : : }
7196 : :
7197 : : /* Expand an atomic clear operation.
7198 : : void _atomic_clear (BOOL *obj, enum memmodel)
7199 : : EXP is the call expression. */
7200 : :
7201 : : static rtx
7202 : 57 : expand_builtin_atomic_clear (tree exp)
7203 : : {
7204 : 57 : machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7205 : 57 : rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7206 : 57 : memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7207 : :
7208 : 57 : if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
7209 : : model = MEMMODEL_SEQ_CST;
7210 : :
7211 : : /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
7212 : : Failing that, a store is issued by __atomic_store. The only way this can
7213 : : fail is if the bool type is larger than a word size. Unlikely, but
7214 : : handle it anyway for completeness. Assume a single threaded model since
7215 : : there is no atomic support in this case, and no barriers are required. */
7216 : 57 : rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
7217 : 57 : if (!ret)
7218 : 0 : emit_move_insn (mem, const0_rtx);
7219 : 57 : return const0_rtx;
7220 : : }
7221 : :
7222 : : /* Expand an atomic test_and_set operation.
7223 : : bool _atomic_test_and_set (BOOL *obj, enum memmodel)
7224 : : EXP is the call expression. */
7225 : :
7226 : : static rtx
7227 : 259 : expand_builtin_atomic_test_and_set (tree exp, rtx target)
7228 : : {
7229 : 259 : rtx mem;
7230 : 259 : enum memmodel model;
7231 : 259 : machine_mode mode;
7232 : :
7233 : 259 : mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7234 : 259 : mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7235 : 259 : model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7236 : :
7237 : 259 : return expand_atomic_test_and_set (target, mem, model);
7238 : : }
7239 : :
7240 : :
7241 : : /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
7242 : : this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
7243 : :
7244 : : static tree
7245 : 89600 : fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
7246 : : {
7247 : 89600 : int size;
7248 : 89600 : machine_mode mode;
7249 : 89600 : unsigned int mode_align, type_align;
7250 : :
7251 : 89600 : 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 : 89506 : size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
7256 : 89506 : if (!int_mode_for_size (size, 0).exists (&mode))
7257 : 1 : return boolean_false_node;
7258 : :
7259 : 89505 : mode_align = GET_MODE_ALIGNMENT (mode);
7260 : :
7261 : 89505 : if (TREE_CODE (arg1) == INTEGER_CST)
7262 : : {
7263 : 69381 : 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 : 69381 : val = least_bit_hwi (val);
7268 : 69381 : val *= BITS_PER_UNIT;
7269 : :
7270 : 69381 : if (val == 0 || mode_align < val)
7271 : : type_align = mode_align;
7272 : : else
7273 : 19530 : type_align = val;
7274 : : }
7275 : : else
7276 : : {
7277 : 20124 : 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 : 20092 : && POINTER_TYPE_P (ttype)
7285 : 20092 : && VOID_TYPE_P (TREE_TYPE (ttype))
7286 : 40216 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
7287 : 20061 : arg1 = TREE_OPERAND (arg1, 0);
7288 : :
7289 : 20124 : ttype = TREE_TYPE (arg1);
7290 : 20124 : gcc_assert (POINTER_TYPE_P (ttype));
7291 : :
7292 : : /* Get the underlying type of the object. */
7293 : 20124 : ttype = TREE_TYPE (ttype);
7294 : 20124 : type_align = TYPE_ALIGN (ttype);
7295 : : }
7296 : :
7297 : : /* If the object has smaller alignment, the lock free routines cannot
7298 : : be used. */
7299 : 89505 : if (type_align < mode_align)
7300 : 91 : return boolean_false_node;
7301 : :
7302 : : /* Check if a compare_and_swap pattern exists for the mode which represents
7303 : : the required size. The pattern is not allowed to fail, so the existence
7304 : : of the pattern indicates support is present. Also require that an
7305 : : atomic load exists for the required size. */
7306 : 89414 : if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
7307 : 89372 : 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 : 39697 : fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
7343 : : {
7344 : 39697 : if (!flag_inline_atomics)
7345 : : return NULL_TREE;
7346 : :
7347 : : /* If it isn't always lock free, don't generate a result. */
7348 : 39688 : if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
7349 : : return boolean_true_node;
7350 : :
7351 : : return NULL_TREE;
7352 : : }
7353 : :
7354 : : /* Return true if the parameters to call EXP represent an object which will
7355 : : always generate lock free instructions. The first argument represents the
7356 : : size of the object, and the second parameter is a pointer to the object
7357 : : itself. If NULL is passed for the object, then the result is based on
7358 : : typical alignment for an object of the specified size. Otherwise return
7359 : : NULL*/
7360 : :
7361 : : static rtx
7362 : 3 : expand_builtin_atomic_is_lock_free (tree exp)
7363 : : {
7364 : 3 : tree size;
7365 : 3 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7366 : 3 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7367 : :
7368 : 3 : if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
7369 : : {
7370 : 0 : error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
7371 : 0 : return NULL_RTX;
7372 : : }
7373 : :
7374 : 3 : if (!flag_inline_atomics)
7375 : : return NULL_RTX;
7376 : :
7377 : : /* If the value is known at compile time, return the RTX for it. */
7378 : 2 : size = fold_builtin_atomic_is_lock_free (arg0, arg1);
7379 : 2 : if (size == boolean_true_node)
7380 : 0 : return const1_rtx;
7381 : :
7382 : : return NULL_RTX;
7383 : : }
7384 : :
7385 : : /* Expand the __atomic_thread_fence intrinsic:
7386 : : void __atomic_thread_fence (enum memmodel)
7387 : : EXP is the CALL_EXPR. */
7388 : :
7389 : : static void
7390 : 693 : expand_builtin_atomic_thread_fence (tree exp)
7391 : : {
7392 : 693 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7393 : 693 : expand_mem_thread_fence (model);
7394 : 693 : }
7395 : :
7396 : : /* Expand the __atomic_signal_fence intrinsic:
7397 : : void __atomic_signal_fence (enum memmodel)
7398 : : EXP is the CALL_EXPR. */
7399 : :
7400 : : static void
7401 : 60 : expand_builtin_atomic_signal_fence (tree exp)
7402 : : {
7403 : 60 : enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7404 : 60 : expand_mem_signal_fence (model);
7405 : 60 : }
7406 : :
7407 : : /* Expand the __sync_synchronize intrinsic. */
7408 : :
7409 : : static void
7410 : 281 : expand_builtin_sync_synchronize (void)
7411 : : {
7412 : 0 : expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
7413 : 0 : }
7414 : :
7415 : : static rtx
7416 : 3 : expand_builtin_thread_pointer (tree exp, rtx target)
7417 : : {
7418 : 3 : enum insn_code icode;
7419 : 3 : if (!validate_arglist (exp, VOID_TYPE))
7420 : 0 : return const0_rtx;
7421 : 3 : icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
7422 : 3 : if (icode != CODE_FOR_nothing)
7423 : : {
7424 : 3 : class expand_operand op;
7425 : : /* If the target is not sutitable then create a new target. */
7426 : 3 : if (target == NULL_RTX
7427 : 3 : || !REG_P (target)
7428 : 6 : || GET_MODE (target) != Pmode)
7429 : 0 : target = gen_reg_rtx (Pmode);
7430 : 3 : create_output_operand (&op, target, Pmode);
7431 : 3 : expand_insn (icode, 1, &op);
7432 : 3 : return target;
7433 : : }
7434 : 0 : error ("%<__builtin_thread_pointer%> is not supported on this target");
7435 : 0 : return const0_rtx;
7436 : : }
7437 : :
7438 : : static void
7439 : 0 : expand_builtin_set_thread_pointer (tree exp)
7440 : : {
7441 : 0 : enum insn_code icode;
7442 : 0 : if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7443 : : return;
7444 : 0 : icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
7445 : 0 : if (icode != CODE_FOR_nothing)
7446 : : {
7447 : 0 : class expand_operand op;
7448 : 0 : rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
7449 : 0 : Pmode, EXPAND_NORMAL);
7450 : 0 : create_input_operand (&op, val, Pmode);
7451 : 0 : expand_insn (icode, 1, &op);
7452 : 0 : return;
7453 : : }
7454 : 0 : error ("%<__builtin_set_thread_pointer%> is not supported on this target");
7455 : : }
7456 : :
7457 : :
7458 : : /* Emit code to restore the current value of stack. */
7459 : :
7460 : : static void
7461 : 1669 : expand_stack_restore (tree var)
7462 : : {
7463 : 1669 : rtx_insn *prev;
7464 : 1669 : rtx sa = expand_normal (var);
7465 : :
7466 : 1669 : sa = convert_memory_address (Pmode, sa);
7467 : :
7468 : 1669 : prev = get_last_insn ();
7469 : 1669 : emit_stack_restore (SAVE_BLOCK, sa);
7470 : :
7471 : 1669 : record_new_stack_level ();
7472 : :
7473 : 1669 : fixup_args_size_notes (prev, get_last_insn (), 0);
7474 : 1669 : }
7475 : :
7476 : : /* Emit code to save the current value of stack. */
7477 : :
7478 : : static rtx
7479 : 1810 : expand_stack_save (void)
7480 : : {
7481 : 1810 : rtx ret = NULL_RTX;
7482 : :
7483 : 0 : emit_stack_save (SAVE_BLOCK, &ret);
7484 : 1810 : return ret;
7485 : : }
7486 : :
7487 : : /* Emit code to get the openacc gang, worker or vector id or size. */
7488 : :
7489 : : static rtx
7490 : 356 : expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7491 : : {
7492 : 356 : const char *name;
7493 : 356 : rtx fallback_retval;
7494 : 356 : rtx_insn *(*gen_fn) (rtx, rtx);
7495 : 356 : switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7496 : : {
7497 : 244 : case BUILT_IN_GOACC_PARLEVEL_ID:
7498 : 244 : name = "__builtin_goacc_parlevel_id";
7499 : 244 : fallback_retval = const0_rtx;
7500 : 244 : gen_fn = targetm.gen_oacc_dim_pos;
7501 : 244 : break;
7502 : 112 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
7503 : 112 : name = "__builtin_goacc_parlevel_size";
7504 : 112 : fallback_retval = const1_rtx;
7505 : 112 : gen_fn = targetm.gen_oacc_dim_size;
7506 : 112 : break;
7507 : 0 : default:
7508 : 0 : gcc_unreachable ();
7509 : : }
7510 : :
7511 : 356 : if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7512 : : {
7513 : 8 : error ("%qs only supported in OpenACC code", name);
7514 : 8 : return const0_rtx;
7515 : : }
7516 : :
7517 : 348 : tree arg = CALL_EXPR_ARG (exp, 0);
7518 : 348 : if (TREE_CODE (arg) != INTEGER_CST)
7519 : : {
7520 : 8 : error ("non-constant argument 0 to %qs", name);
7521 : 8 : return const0_rtx;
7522 : : }
7523 : :
7524 : 340 : int dim = TREE_INT_CST_LOW (arg);
7525 : 340 : switch (dim)
7526 : : {
7527 : 324 : case GOMP_DIM_GANG:
7528 : 324 : case GOMP_DIM_WORKER:
7529 : 324 : case GOMP_DIM_VECTOR:
7530 : 324 : break;
7531 : 16 : default:
7532 : 16 : error ("illegal argument 0 to %qs", name);
7533 : 16 : return const0_rtx;
7534 : : }
7535 : :
7536 : 324 : if (ignore)
7537 : : return target;
7538 : :
7539 : 180 : if (target == NULL_RTX)
7540 : 0 : target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7541 : :
7542 : 180 : if (!targetm.have_oacc_dim_size ())
7543 : : {
7544 : 180 : emit_move_insn (target, fallback_retval);
7545 : 180 : return target;
7546 : : }
7547 : :
7548 : 0 : rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7549 : 0 : emit_insn (gen_fn (reg, GEN_INT (dim)));
7550 : 0 : if (reg != target)
7551 : 0 : emit_move_insn (target, reg);
7552 : :
7553 : : return target;
7554 : : }
7555 : :
7556 : : /* Expand a string compare operation using a sequence of char comparison
7557 : : to get rid of the calling overhead, with result going to TARGET if
7558 : : that's convenient.
7559 : :
7560 : : VAR_STR is the variable string source;
7561 : : CONST_STR is the constant string source;
7562 : : LENGTH is the number of chars to compare;
7563 : : CONST_STR_N indicates which source string is the constant string;
7564 : : IS_MEMCMP indicates whether it's a memcmp or strcmp.
7565 : :
7566 : : to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7567 : :
7568 : : target = (int) (unsigned char) var_str[0]
7569 : : - (int) (unsigned char) const_str[0];
7570 : : if (target != 0)
7571 : : goto ne_label;
7572 : : ...
7573 : : target = (int) (unsigned char) var_str[length - 2]
7574 : : - (int) (unsigned char) const_str[length - 2];
7575 : : if (target != 0)
7576 : : goto ne_label;
7577 : : target = (int) (unsigned char) var_str[length - 1]
7578 : : - (int) (unsigned char) const_str[length - 1];
7579 : : ne_label:
7580 : : */
7581 : :
7582 : : static rtx
7583 : 626 : 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 : 626 : HOST_WIDE_INT offset = 0;
7588 : 626 : rtx var_rtx_array
7589 : 626 : = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7590 : 626 : rtx var_rtx = NULL_RTX;
7591 : 626 : rtx const_rtx = NULL_RTX;
7592 : 626 : rtx result = target ? target : gen_reg_rtx (mode);
7593 : 626 : rtx_code_label *ne_label = gen_label_rtx ();
7594 : 626 : tree unit_type_node = unsigned_char_type_node;
7595 : 626 : scalar_int_mode unit_mode
7596 : 626 : = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7597 : :
7598 : 626 : start_sequence ();
7599 : :
7600 : 2292 : for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7601 : : {
7602 : 1666 : var_rtx
7603 : 1666 : = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7604 : 1666 : const_rtx = c_readstr (const_str + offset, unit_mode);
7605 : 1666 : rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7606 : 1516 : rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7607 : :
7608 : 1666 : op0 = convert_modes (mode, unit_mode, op0, 1);
7609 : 1666 : op1 = convert_modes (mode, unit_mode, op1, 1);
7610 : 1666 : 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 : 1666 : if (diff != result)
7618 : 0 : emit_move_insn (result, diff);
7619 : :
7620 : 1666 : if (i < length - 1)
7621 : 1040 : emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7622 : : mode, true, ne_label);
7623 : 3332 : offset += GET_MODE_SIZE (unit_mode);
7624 : : }
7625 : :
7626 : 626 : emit_label (ne_label);
7627 : 626 : rtx_insn *insns = end_sequence ();
7628 : 626 : emit_insn (insns);
7629 : :
7630 : 626 : return result;
7631 : : }
7632 : :
7633 : : /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7634 : : to TARGET if that's convenient.
7635 : : If the call is not been inlined, return NULL_RTX. */
7636 : :
7637 : : static rtx
7638 : 173440 : inline_expand_builtin_bytecmp (tree exp, rtx target)
7639 : : {
7640 : 173440 : tree fndecl = get_callee_fndecl (exp);
7641 : 173440 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7642 : 173440 : bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7643 : :
7644 : : /* Do NOT apply this inlining expansion when optimizing for size or
7645 : : optimization level below 2 or if unused *cmp hasn't been DCEd. */
7646 : 173440 : if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7647 : 151130 : return NULL_RTX;
7648 : :
7649 : 22310 : gcc_checking_assert (fcode == BUILT_IN_STRCMP
7650 : : || fcode == BUILT_IN_STRNCMP
7651 : : || fcode == BUILT_IN_MEMCMP);
7652 : :
7653 : : /* On a target where the type of the call (int) has same or narrower presicion
7654 : : than unsigned char, give up the inlining expansion. */
7655 : 22310 : if (TYPE_PRECISION (unsigned_char_type_node)
7656 : 22310 : >= TYPE_PRECISION (TREE_TYPE (exp)))
7657 : : return NULL_RTX;
7658 : :
7659 : 22310 : tree arg1 = CALL_EXPR_ARG (exp, 0);
7660 : 22310 : tree arg2 = CALL_EXPR_ARG (exp, 1);
7661 : 22310 : tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7662 : :
7663 : 22310 : unsigned HOST_WIDE_INT len1 = 0;
7664 : 22310 : unsigned HOST_WIDE_INT len2 = 0;
7665 : 22310 : unsigned HOST_WIDE_INT len3 = 0;
7666 : :
7667 : : /* Get the object representation of the initializers of ARG1 and ARG2
7668 : : as strings, provided they refer to constant objects, with their byte
7669 : : sizes in LEN1 and LEN2, respectively. */
7670 : 22310 : const char *bytes1 = getbyterep (arg1, &len1);
7671 : 22310 : const char *bytes2 = getbyterep (arg2, &len2);
7672 : :
7673 : : /* Fail if neither argument refers to an initialized constant. */
7674 : 22310 : if (!bytes1 && !bytes2)
7675 : : return NULL_RTX;
7676 : :
7677 : 18413 : if (is_ncmp)
7678 : : {
7679 : : /* Fail if the memcmp/strncmp bound is not a constant. */
7680 : 1334 : if (!tree_fits_uhwi_p (len3_tree))
7681 : : return NULL_RTX;
7682 : :
7683 : 989 : len3 = tree_to_uhwi (len3_tree);
7684 : :
7685 : 989 : if (fcode == BUILT_IN_MEMCMP)
7686 : : {
7687 : : /* Fail if the memcmp bound is greater than the size of either
7688 : : of the two constant objects. */
7689 : 486 : if ((bytes1 && len1 < len3)
7690 : 486 : || (bytes2 && len2 < len3))
7691 : : return NULL_RTX;
7692 : : }
7693 : : }
7694 : :
7695 : : if (fcode != BUILT_IN_MEMCMP)
7696 : : {
7697 : : /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7698 : : and LEN2 to the length of the nul-terminated string stored
7699 : : in each. */
7700 : 17582 : if (bytes1 != NULL)
7701 : 236 : len1 = strnlen (bytes1, len1) + 1;
7702 : 17582 : if (bytes2 != NULL)
7703 : 17349 : len2 = strnlen (bytes2, len2) + 1;
7704 : : }
7705 : :
7706 : : /* See inline_string_cmp. */
7707 : 18064 : int const_str_n;
7708 : 18064 : if (!len1)
7709 : : const_str_n = 2;
7710 : 241 : else if (!len2)
7711 : : const_str_n = 1;
7712 : 3 : else if (len2 > len1)
7713 : : const_str_n = 1;
7714 : : else
7715 : : const_str_n = 2;
7716 : :
7717 : : /* For strncmp only, compute the new bound as the smallest of
7718 : : the lengths of the two strings (plus 1) and the bound provided
7719 : : to the function. */
7720 : 17825 : unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7721 : 18064 : if (is_ncmp && len3 < bound)
7722 : 529 : bound = len3;
7723 : :
7724 : : /* If the bound of the comparison is larger than the threshold,
7725 : : do nothing. */
7726 : 18064 : if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7727 : : return NULL_RTX;
7728 : :
7729 : 626 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7730 : :
7731 : : /* Now, start inline expansion the call. */
7732 : 1195 : return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7733 : : (const_str_n == 1) ? bytes1 : bytes2, bound,
7734 : 626 : const_str_n, mode);
7735 : : }
7736 : :
7737 : : /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7738 : : represents the size of the first argument to that call, or VOIDmode
7739 : : if the argument is a pointer. IGNORE will be true if the result
7740 : : isn't used. */
7741 : : static rtx
7742 : 34 : expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7743 : : bool ignore)
7744 : : {
7745 : 34 : rtx val, failsafe;
7746 : 34 : unsigned nargs = call_expr_nargs (exp);
7747 : :
7748 : 34 : tree arg0 = CALL_EXPR_ARG (exp, 0);
7749 : :
7750 : 34 : if (mode == VOIDmode)
7751 : : {
7752 : 4 : mode = TYPE_MODE (TREE_TYPE (arg0));
7753 : 4 : gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7754 : : }
7755 : :
7756 : 34 : val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7757 : :
7758 : : /* An optional second argument can be used as a failsafe value on
7759 : : some machines. If it isn't present, then the failsafe value is
7760 : : assumed to be 0. */
7761 : 34 : if (nargs > 1)
7762 : : {
7763 : 4 : tree arg1 = CALL_EXPR_ARG (exp, 1);
7764 : 4 : failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7765 : : }
7766 : : else
7767 : 30 : failsafe = const0_rtx;
7768 : :
7769 : : /* If the result isn't used, the behavior is undefined. It would be
7770 : : nice to emit a warning here, but path splitting means this might
7771 : : happen with legitimate code. So simply drop the builtin
7772 : : expansion in that case; we've handled any side-effects above. */
7773 : 34 : if (ignore)
7774 : 0 : return const0_rtx;
7775 : :
7776 : : /* If we don't have a suitable target, create one to hold the result. */
7777 : 34 : if (target == NULL || GET_MODE (target) != mode)
7778 : 0 : target = gen_reg_rtx (mode);
7779 : :
7780 : 34 : if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7781 : 0 : val = convert_modes (mode, VOIDmode, val, false);
7782 : :
7783 : 34 : return targetm.speculation_safe_value (mode, target, val, failsafe);
7784 : : }
7785 : :
7786 : : /* Expand CRC* or REV_CRC* built-ins. */
7787 : :
7788 : : rtx
7789 : 34 : expand_builtin_crc_table_based (internal_fn fn, scalar_mode crc_mode,
7790 : : scalar_mode data_mode, machine_mode mode,
7791 : : tree exp, rtx target)
7792 : : {
7793 : 34 : tree rhs1 = CALL_EXPR_ARG (exp, 0); // crc
7794 : 34 : tree rhs2 = CALL_EXPR_ARG (exp, 1); // data
7795 : 34 : tree rhs3 = CALL_EXPR_ARG (exp, 2); // polynomial
7796 : :
7797 : 34 : if (!target || mode == VOIDmode)
7798 : 0 : target = gen_reg_rtx (crc_mode);
7799 : :
7800 : 34 : rtx op1 = expand_normal (rhs1);
7801 : 34 : rtx op2 = expand_normal (rhs2);
7802 : 34 : rtx op3;
7803 : 34 : if (TREE_CODE (rhs3) != INTEGER_CST)
7804 : : {
7805 : 1 : error ("third argument to %<crc%> builtins must be a constant");
7806 : 1 : op3 = const0_rtx;
7807 : : }
7808 : : else
7809 : 33 : op3 = convert_to_mode (crc_mode, expand_normal (rhs3), 0);
7810 : :
7811 : 34 : if (CONST_INT_P (op2))
7812 : 33 : op2 = convert_to_mode (crc_mode, op2, 0);
7813 : :
7814 : 34 : if (fn == IFN_CRC)
7815 : 17 : expand_crc_table_based (target, op1, op2, op3, data_mode);
7816 : : else
7817 : : /* If it's IFN_CRC_REV generate bit-reversed CRC. */
7818 : 17 : expand_reversed_crc_table_based (target, op1, op2, op3,
7819 : : data_mode,
7820 : : generate_reflecting_code_standard);
7821 : 34 : return target;
7822 : : }
7823 : :
7824 : : /* Expand an expression EXP that calls a built-in function,
7825 : : with result going to TARGET if that's convenient
7826 : : (and in mode MODE if that's convenient).
7827 : : SUBTARGET may be used as the target for computing one of EXP's operands.
7828 : : IGNORE is nonzero if the value is to be ignored. */
7829 : :
7830 : : rtx
7831 : 1958879 : expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7832 : : int ignore)
7833 : : {
7834 : 1958879 : tree fndecl = get_callee_fndecl (exp);
7835 : 1958879 : machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7836 : 1958879 : int flags;
7837 : :
7838 : 1958879 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7839 : 171256 : return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7840 : :
7841 : : /* When ASan is enabled, we don't want to expand some memory/string
7842 : : builtins and rely on libsanitizer's hooks. This allows us to avoid
7843 : : redundant checks and be sure, that possible overflow will be detected
7844 : : by ASan. */
7845 : :
7846 : 1787623 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7847 : 1787623 : if (param_asan_kernel_mem_intrinsic_prefix
7848 : 1787623 : && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7849 : : | SANITIZE_KERNEL_HWADDRESS))
7850 : 42 : switch (fcode)
7851 : : {
7852 : 42 : rtx save_decl_rtl, ret;
7853 : 42 : case BUILT_IN_MEMCPY:
7854 : 42 : case BUILT_IN_MEMMOVE:
7855 : 42 : case BUILT_IN_MEMSET:
7856 : 42 : save_decl_rtl = DECL_RTL (fndecl);
7857 : 42 : DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7858 : 42 : ret = expand_call (exp, target, ignore);
7859 : 42 : DECL_RTL (fndecl) = save_decl_rtl;
7860 : 42 : return ret;
7861 : : default:
7862 : : break;
7863 : : }
7864 : 1787581 : if (sanitize_flags_p (SANITIZE_ADDRESS | SANITIZE_HWADDRESS)
7865 : 1787581 : && asan_intercepted_p (fcode))
7866 : 759 : return expand_call (exp, target, ignore);
7867 : :
7868 : : /* When not optimizing, generate calls to library functions for a certain
7869 : : set of builtins. */
7870 : 1786822 : if (!optimize
7871 : 419493 : && !called_as_built_in (fndecl)
7872 : 141557 : && fcode != BUILT_IN_FORK
7873 : 141557 : && fcode != BUILT_IN_EXECL
7874 : 141546 : && fcode != BUILT_IN_EXECV
7875 : 141546 : && fcode != BUILT_IN_EXECLP
7876 : 141544 : && fcode != BUILT_IN_EXECLE
7877 : 141544 : && fcode != BUILT_IN_EXECVP
7878 : 141543 : && fcode != BUILT_IN_EXECVE
7879 : 141543 : && fcode != BUILT_IN_CLEAR_CACHE
7880 : 141540 : && !ALLOCA_FUNCTION_CODE_P (fcode)
7881 : 141486 : && fcode != BUILT_IN_FREE
7882 : 139406 : && (fcode != BUILT_IN_MEMSET
7883 : 55883 : || !(flag_inline_stringops & ILSOP_MEMSET))
7884 : 139403 : && (fcode != BUILT_IN_MEMCPY
7885 : 918 : || !(flag_inline_stringops & ILSOP_MEMCPY))
7886 : 139267 : && (fcode != BUILT_IN_MEMMOVE
7887 : 347 : || !(flag_inline_stringops & ILSOP_MEMMOVE))
7888 : 1926089 : && (fcode != BUILT_IN_MEMCMP
7889 : 2504 : || !(flag_inline_stringops & ILSOP_MEMCMP)))
7890 : 139219 : return expand_call (exp, target, ignore);
7891 : :
7892 : : /* The built-in function expanders test for target == const0_rtx
7893 : : to determine whether the function's result will be ignored. */
7894 : 1647603 : if (ignore)
7895 : 963417 : target = const0_rtx;
7896 : :
7897 : : /* If the result of a pure or const built-in function is ignored, and
7898 : : none of its arguments are volatile, we can avoid expanding the
7899 : : built-in call and just evaluate the arguments for side-effects. */
7900 : 1647603 : if (target == const0_rtx
7901 : 963417 : && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7902 : 1653991 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
7903 : : {
7904 : 6 : bool volatilep = false;
7905 : 6 : tree arg;
7906 : 6 : call_expr_arg_iterator iter;
7907 : :
7908 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7909 : 8 : if (TREE_THIS_VOLATILE (arg))
7910 : : {
7911 : : volatilep = true;
7912 : : break;
7913 : : }
7914 : :
7915 : 6 : if (! volatilep)
7916 : : {
7917 : 20 : FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7918 : 8 : expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7919 : 6 : return const0_rtx;
7920 : : }
7921 : : }
7922 : :
7923 : 1647597 : switch (fcode)
7924 : : {
7925 : 4 : CASE_FLT_FN (BUILT_IN_FABS):
7926 : 4 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7927 : 4 : case BUILT_IN_FABSD32:
7928 : 4 : case BUILT_IN_FABSD64:
7929 : 4 : case BUILT_IN_FABSD128:
7930 : 4 : case BUILT_IN_FABSD64X:
7931 : 4 : target = expand_builtin_fabs (exp, target, subtarget);
7932 : 4 : if (target)
7933 : 787656 : return target;
7934 : 859941 : break;
7935 : :
7936 : 11674 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
7937 : 11674 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7938 : 11674 : target = expand_builtin_copysign (exp, target, subtarget);
7939 : 11674 : if (target)
7940 : : return target;
7941 : : break;
7942 : :
7943 : : /* Just do a normal library call if we were unable to fold
7944 : : the values. */
7945 : : CASE_FLT_FN (BUILT_IN_CABS):
7946 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7947 : : break;
7948 : :
7949 : 364 : CASE_FLT_FN (BUILT_IN_FMA):
7950 : 364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7951 : 364 : target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7952 : 364 : if (target)
7953 : : return target;
7954 : : break;
7955 : :
7956 : 235 : CASE_FLT_FN (BUILT_IN_ILOGB):
7957 : 235 : if (! flag_unsafe_math_optimizations)
7958 : : break;
7959 : 4 : gcc_fallthrough ();
7960 : 4 : CASE_FLT_FN (BUILT_IN_ISINF):
7961 : 4 : CASE_FLT_FN (BUILT_IN_FINITE):
7962 : 4 : case BUILT_IN_ISFINITE:
7963 : 4 : case BUILT_IN_ISNORMAL:
7964 : 4 : target = expand_builtin_interclass_mathfn (exp, target);
7965 : 4 : if (target)
7966 : : return target;
7967 : : break;
7968 : :
7969 : 823 : case BUILT_IN_ISSIGNALING:
7970 : 823 : target = expand_builtin_issignaling (exp, target);
7971 : 823 : if (target)
7972 : : return target;
7973 : : break;
7974 : :
7975 : 231 : CASE_FLT_FN (BUILT_IN_ICEIL):
7976 : 231 : CASE_FLT_FN (BUILT_IN_LCEIL):
7977 : 231 : CASE_FLT_FN (BUILT_IN_LLCEIL):
7978 : 231 : CASE_FLT_FN (BUILT_IN_LFLOOR):
7979 : 231 : CASE_FLT_FN (BUILT_IN_IFLOOR):
7980 : 231 : CASE_FLT_FN (BUILT_IN_LLFLOOR):
7981 : 231 : target = expand_builtin_int_roundingfn (exp, target);
7982 : 231 : if (target)
7983 : : return target;
7984 : : break;
7985 : :
7986 : 590 : CASE_FLT_FN (BUILT_IN_IRINT):
7987 : 590 : CASE_FLT_FN (BUILT_IN_LRINT):
7988 : 590 : CASE_FLT_FN (BUILT_IN_LLRINT):
7989 : 590 : CASE_FLT_FN (BUILT_IN_IROUND):
7990 : 590 : CASE_FLT_FN (BUILT_IN_LROUND):
7991 : 590 : CASE_FLT_FN (BUILT_IN_LLROUND):
7992 : 590 : target = expand_builtin_int_roundingfn_2 (exp, target);
7993 : 590 : if (target)
7994 : : return target;
7995 : : break;
7996 : :
7997 : 279 : CASE_FLT_FN (BUILT_IN_POWI):
7998 : 279 : target = expand_builtin_powi (exp, target);
7999 : 279 : if (target)
8000 : : return target;
8001 : : break;
8002 : :
8003 : 157 : CASE_FLT_FN (BUILT_IN_CEXPI):
8004 : 157 : target = expand_builtin_cexpi (exp, target);
8005 : 157 : gcc_assert (target);
8006 : : return target;
8007 : :
8008 : 991 : CASE_FLT_FN (BUILT_IN_SIN):
8009 : 991 : CASE_FLT_FN (BUILT_IN_COS):
8010 : 991 : if (! flag_unsafe_math_optimizations)
8011 : : break;
8012 : 47 : target = expand_builtin_mathfn_3 (exp, target, subtarget);
8013 : 47 : if (target)
8014 : : return target;
8015 : : break;
8016 : :
8017 : 151 : CASE_FLT_FN (BUILT_IN_SINCOS):
8018 : 151 : if (! flag_unsafe_math_optimizations)
8019 : : break;
8020 : 3 : target = expand_builtin_sincos (exp);
8021 : 3 : if (target)
8022 : : return target;
8023 : : break;
8024 : :
8025 : 60 : case BUILT_IN_FEGETROUND:
8026 : 60 : target = expand_builtin_fegetround (exp, target, target_mode);
8027 : 60 : if (target)
8028 : : return target;
8029 : : break;
8030 : :
8031 : 1063 : case BUILT_IN_FECLEAREXCEPT:
8032 : 1063 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8033 : : feclearexcept_optab);
8034 : 1063 : if (target)
8035 : : return target;
8036 : : break;
8037 : :
8038 : 831 : case BUILT_IN_FERAISEEXCEPT:
8039 : 831 : target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8040 : : feraiseexcept_optab);
8041 : 831 : if (target)
8042 : : return target;
8043 : : break;
8044 : :
8045 : 464 : case BUILT_IN_APPLY_ARGS:
8046 : 464 : return expand_builtin_apply_args ();
8047 : :
8048 : : /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
8049 : : FUNCTION with a copy of the parameters described by
8050 : : ARGUMENTS, and ARGSIZE. It returns a block of memory
8051 : : allocated on the stack into which is stored all the registers
8052 : : that might possibly be used for returning the result of a
8053 : : function. ARGUMENTS is the value returned by
8054 : : __builtin_apply_args. ARGSIZE is the number of bytes of
8055 : : arguments that must be copied. ??? How should this value be
8056 : : computed? We'll also need a safe worst case value for varargs
8057 : : functions. */
8058 : 483 : case BUILT_IN_APPLY:
8059 : 483 : if (!validate_arglist (exp, POINTER_TYPE,
8060 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
8061 : 483 : && !validate_arglist (exp, REFERENCE_TYPE,
8062 : : POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8063 : 0 : return const0_rtx;
8064 : : else
8065 : : {
8066 : 483 : rtx ops[3];
8067 : :
8068 : 483 : ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
8069 : 483 : ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
8070 : 483 : ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
8071 : :
8072 : 483 : return expand_builtin_apply (ops[0], ops[1], ops[2]);
8073 : : }
8074 : :
8075 : : /* __builtin_return (RESULT) causes the function to return the
8076 : : value described by RESULT. RESULT is address of the block of
8077 : : memory returned by __builtin_apply. */
8078 : 379 : case BUILT_IN_RETURN:
8079 : 379 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8080 : 379 : expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
8081 : 379 : return const0_rtx;
8082 : :
8083 : 0 : case BUILT_IN_SAVEREGS:
8084 : 0 : return expand_builtin_saveregs ();
8085 : :
8086 : 16 : case BUILT_IN_VA_ARG_PACK:
8087 : : /* All valid uses of __builtin_va_arg_pack () are removed during
8088 : : inlining. */
8089 : 16 : error ("invalid use of %<__builtin_va_arg_pack ()%>");
8090 : 16 : return const0_rtx;
8091 : :
8092 : 0 : case BUILT_IN_VA_ARG_PACK_LEN:
8093 : : /* All valid uses of __builtin_va_arg_pack_len () are removed during
8094 : : inlining. */
8095 : 0 : error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
8096 : 0 : return const0_rtx;
8097 : :
8098 : : /* Return the address of the first anonymous stack arg. */
8099 : 150 : case BUILT_IN_NEXT_ARG:
8100 : 150 : if (fold_builtin_next_arg (exp, false))
8101 : 0 : return const0_rtx;
8102 : 150 : return expand_builtin_next_arg ();
8103 : :
8104 : 28 : case BUILT_IN_CLEAR_CACHE:
8105 : 28 : expand_builtin___clear_cache (exp);
8106 : 28 : return const0_rtx;
8107 : :
8108 : 0 : case BUILT_IN_CLASSIFY_TYPE:
8109 : 0 : return expand_builtin_classify_type (exp);
8110 : :
8111 : 0 : case BUILT_IN_CONSTANT_P:
8112 : 0 : return const0_rtx;
8113 : :
8114 : 15583 : case BUILT_IN_FRAME_ADDRESS:
8115 : 15583 : case BUILT_IN_RETURN_ADDRESS:
8116 : 15583 : return expand_builtin_frame_address (fndecl, exp);
8117 : :
8118 : 540 : case BUILT_IN_STACK_ADDRESS:
8119 : 540 : return expand_builtin_stack_address ();
8120 : :
8121 : 2159 : case BUILT_IN___STRUB_ENTER:
8122 : 2159 : target = expand_builtin_strub_enter (exp);
8123 : 2159 : if (target)
8124 : : return target;
8125 : : break;
8126 : :
8127 : 1071 : case BUILT_IN___STRUB_UPDATE:
8128 : 1071 : target = expand_builtin_strub_update (exp);
8129 : 1071 : if (target)
8130 : : return target;
8131 : : break;
8132 : :
8133 : 2729 : case BUILT_IN___STRUB_LEAVE:
8134 : 2729 : target = expand_builtin_strub_leave (exp);
8135 : 2729 : if (target)
8136 : : return target;
8137 : : break;
8138 : :
8139 : : /* Returns the address of the area where the structure is returned.
8140 : : 0 otherwise. */
8141 : 0 : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
8142 : 0 : if (call_expr_nargs (exp) != 0
8143 : 0 : || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
8144 : 0 : || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
8145 : 0 : return const0_rtx;
8146 : : else
8147 : 0 : return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
8148 : :
8149 : 25566 : CASE_BUILT_IN_ALLOCA:
8150 : 25566 : target = expand_builtin_alloca (exp);
8151 : 25566 : if (target)
8152 : : return target;
8153 : : break;
8154 : :
8155 : 203 : case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
8156 : 203 : return expand_asan_emit_allocas_unpoison (exp);
8157 : :
8158 : 1810 : case BUILT_IN_STACK_SAVE:
8159 : 1810 : return expand_stack_save ();
8160 : :
8161 : 1669 : case BUILT_IN_STACK_RESTORE:
8162 : 1669 : expand_stack_restore (CALL_EXPR_ARG (exp, 0));
8163 : 1669 : return const0_rtx;
8164 : :
8165 : 1163 : case BUILT_IN_BSWAP16:
8166 : 1163 : case BUILT_IN_BSWAP32:
8167 : 1163 : case BUILT_IN_BSWAP64:
8168 : 1163 : case BUILT_IN_BSWAP128:
8169 : 1163 : target = expand_builtin_bswap (target_mode, exp, target, subtarget);
8170 : 1163 : if (target)
8171 : : return target;
8172 : : break;
8173 : :
8174 : 0 : CASE_INT_FN (BUILT_IN_FFS):
8175 : 0 : target = expand_builtin_unop (target_mode, exp, target,
8176 : : subtarget, ffs_optab);
8177 : 0 : if (target)
8178 : : return target;
8179 : : break;
8180 : :
8181 : 183 : CASE_INT_FN (BUILT_IN_CLZ):
8182 : 183 : target = expand_builtin_unop (target_mode, exp, target,
8183 : : subtarget, clz_optab);
8184 : 183 : if (target)
8185 : : return target;
8186 : : break;
8187 : :
8188 : 50 : CASE_INT_FN (BUILT_IN_CTZ):
8189 : 50 : target = expand_builtin_unop (target_mode, exp, target,
8190 : : subtarget, ctz_optab);
8191 : 50 : if (target)
8192 : : return target;
8193 : : break;
8194 : :
8195 : 90 : CASE_INT_FN (BUILT_IN_CLRSB):
8196 : 90 : target = expand_builtin_unop (target_mode, exp, target,
8197 : : subtarget, clrsb_optab);
8198 : 90 : if (target)
8199 : : return target;
8200 : : break;
8201 : :
8202 : 417 : CASE_INT_FN (BUILT_IN_POPCOUNT):
8203 : 417 : target = expand_builtin_unop (target_mode, exp, target,
8204 : : subtarget, popcount_optab);
8205 : 417 : if (target)
8206 : : return target;
8207 : : break;
8208 : :
8209 : 11 : CASE_INT_FN (BUILT_IN_PARITY):
8210 : 11 : target = expand_builtin_unop (target_mode, exp, target,
8211 : : subtarget, parity_optab);
8212 : 11 : if (target)
8213 : : return target;
8214 : : break;
8215 : :
8216 : 13727 : case BUILT_IN_STRLEN:
8217 : 13727 : target = expand_builtin_strlen (exp, target, target_mode);
8218 : 13727 : if (target)
8219 : : return target;
8220 : : break;
8221 : :
8222 : 580 : case BUILT_IN_STRNLEN:
8223 : 580 : target = expand_builtin_strnlen (exp, target, target_mode);
8224 : 580 : if (target)
8225 : : return target;
8226 : : break;
8227 : :
8228 : 1862 : case BUILT_IN_STRCPY:
8229 : 1862 : target = expand_builtin_strcpy (exp, target);
8230 : 1862 : if (target)
8231 : : return target;
8232 : : break;
8233 : :
8234 : 2168 : case BUILT_IN_STRNCPY:
8235 : 2168 : target = expand_builtin_strncpy (exp, target);
8236 : 2168 : if (target)
8237 : : return target;
8238 : : break;
8239 : :
8240 : 454 : case BUILT_IN_STPCPY:
8241 : 454 : target = expand_builtin_stpcpy (exp, target, mode);
8242 : 454 : if (target)
8243 : : return target;
8244 : : break;
8245 : :
8246 : 90506 : case BUILT_IN_MEMCPY:
8247 : 90506 : target = expand_builtin_memcpy (exp, target);
8248 : 90506 : if (target)
8249 : : return target;
8250 : : break;
8251 : :
8252 : 16077 : case BUILT_IN_MEMMOVE:
8253 : 16077 : target = expand_builtin_memmove (exp, target);
8254 : 16077 : if (target)
8255 : : return target;
8256 : : break;
8257 : :
8258 : 1637 : case BUILT_IN_MEMPCPY:
8259 : 1637 : target = expand_builtin_mempcpy (exp, target);
8260 : 1637 : if (target)
8261 : : return target;
8262 : : break;
8263 : :
8264 : 35682 : case BUILT_IN_MEMSET:
8265 : 35682 : target = expand_builtin_memset (exp, target, mode);
8266 : 35682 : if (target)
8267 : : return target;
8268 : : break;
8269 : :
8270 : 0 : case BUILT_IN_BZERO:
8271 : 0 : target = expand_builtin_bzero (exp);
8272 : 0 : if (target)
8273 : : return target;
8274 : : break;
8275 : :
8276 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8277 : : back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
8278 : : when changing it to a strcmp call. */
8279 : 381 : case BUILT_IN_STRCMP_EQ:
8280 : 381 : target = expand_builtin_memcmp (exp, target, true);
8281 : 381 : if (target)
8282 : : return target;
8283 : :
8284 : : /* Change this call back to a BUILT_IN_STRCMP. */
8285 : 22 : TREE_OPERAND (exp, 1)
8286 : 22 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
8287 : :
8288 : : /* Delete the last parameter. */
8289 : 22 : unsigned int i;
8290 : 22 : vec<tree, va_gc> *arg_vec;
8291 : 22 : vec_alloc (arg_vec, 2);
8292 : 88 : for (i = 0; i < 2; i++)
8293 : 44 : arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
8294 : 22 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
8295 : : /* FALLTHROUGH */
8296 : :
8297 : 127931 : case BUILT_IN_STRCMP:
8298 : 127931 : target = expand_builtin_strcmp (exp, target);
8299 : 127931 : if (target)
8300 : : return target;
8301 : : break;
8302 : :
8303 : : /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8304 : : back to a BUILT_IN_STRNCMP. */
8305 : 36 : case BUILT_IN_STRNCMP_EQ:
8306 : 36 : target = expand_builtin_memcmp (exp, target, true);
8307 : 36 : if (target)
8308 : : return target;
8309 : :
8310 : : /* Change it back to a BUILT_IN_STRNCMP. */
8311 : 0 : TREE_OPERAND (exp, 1)
8312 : 0 : = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
8313 : : /* FALLTHROUGH */
8314 : :
8315 : 2029 : case BUILT_IN_STRNCMP:
8316 : 2029 : target = expand_builtin_strncmp (exp, target, mode);
8317 : 2029 : if (target)
8318 : : return target;
8319 : : break;
8320 : :
8321 : 101583 : case BUILT_IN_BCMP:
8322 : 101583 : case BUILT_IN_MEMCMP:
8323 : 101583 : case BUILT_IN_MEMCMP_EQ:
8324 : 101583 : target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
8325 : 101583 : if (target)
8326 : : return target;
8327 : 61555 : if (fcode == BUILT_IN_MEMCMP_EQ)
8328 : : {
8329 : 18453 : tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
8330 : 18453 : TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
8331 : : }
8332 : : break;
8333 : :
8334 : 0 : case BUILT_IN_SETJMP:
8335 : : /* This should have been lowered to the builtins below. */
8336 : 0 : gcc_unreachable ();
8337 : :
8338 : 841 : case BUILT_IN_SETJMP_SETUP:
8339 : : /* __builtin_setjmp_setup is passed a pointer to an array of five words
8340 : : and the receiver label. */
8341 : 841 : if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8342 : : {
8343 : 841 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8344 : : VOIDmode, EXPAND_NORMAL);
8345 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
8346 : 841 : rtx_insn *label_r = label_rtx (label);
8347 : :
8348 : 841 : expand_builtin_setjmp_setup (buf_addr, label_r);
8349 : 841 : return const0_rtx;
8350 : : }
8351 : : break;
8352 : :
8353 : 841 : case BUILT_IN_SETJMP_RECEIVER:
8354 : : /* __builtin_setjmp_receiver is passed the receiver label. */
8355 : 841 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8356 : : {
8357 : 841 : tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
8358 : 841 : rtx_insn *label_r = label_rtx (label);
8359 : :
8360 : 841 : expand_builtin_setjmp_receiver (label_r);
8361 : 841 : nonlocal_goto_handler_labels
8362 : 1682 : = gen_rtx_INSN_LIST (VOIDmode, label_r,
8363 : 841 : nonlocal_goto_handler_labels);
8364 : : /* ??? Do not let expand_label treat us as such since we would
8365 : : not want to be both on the list of non-local labels and on
8366 : : the list of forced labels. */
8367 : 841 : FORCED_LABEL (label) = 0;
8368 : 841 : return const0_rtx;
8369 : : }
8370 : : break;
8371 : :
8372 : : /* __builtin_longjmp is passed a pointer to an array of five words.
8373 : : It's similar to the C library longjmp function but works with
8374 : : __builtin_setjmp above. */
8375 : 391 : case BUILT_IN_LONGJMP:
8376 : 391 : if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8377 : : {
8378 : 391 : rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8379 : : VOIDmode, EXPAND_NORMAL);
8380 : 391 : rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
8381 : :
8382 : 391 : if (value != const1_rtx)
8383 : : {
8384 : 0 : error ("%<__builtin_longjmp%> second argument must be 1");
8385 : 0 : return const0_rtx;
8386 : : }
8387 : :
8388 : 391 : expand_builtin_longjmp (buf_addr, value);
8389 : 391 : return const0_rtx;
8390 : : }
8391 : : break;
8392 : :
8393 : 510 : case BUILT_IN_NONLOCAL_GOTO:
8394 : 510 : target = expand_builtin_nonlocal_goto (exp);
8395 : 510 : if (target)
8396 : : return target;
8397 : : break;
8398 : :
8399 : : /* This updates the setjmp buffer that is its argument with the value
8400 : : of the current stack pointer. */
8401 : 0 : case BUILT_IN_UPDATE_SETJMP_BUF:
8402 : 0 : if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8403 : : {
8404 : 0 : rtx buf_addr
8405 : 0 : = expand_normal (CALL_EXPR_ARG (exp, 0));
8406 : :
8407 : 0 : expand_builtin_update_setjmp_buf (buf_addr);
8408 : 0 : return const0_rtx;
8409 : : }
8410 : : break;
8411 : :
8412 : 37985 : case BUILT_IN_TRAP:
8413 : 37985 : case BUILT_IN_UNREACHABLE_TRAP:
8414 : 37985 : expand_builtin_trap ();
8415 : 37985 : return const0_rtx;
8416 : :
8417 : 5311 : case BUILT_IN_UNREACHABLE:
8418 : 5311 : expand_builtin_unreachable ();
8419 : 5311 : return const0_rtx;
8420 : :
8421 : 1133 : CASE_FLT_FN (BUILT_IN_SIGNBIT):
8422 : 1133 : case BUILT_IN_SIGNBITD32:
8423 : 1133 : case BUILT_IN_SIGNBITD64:
8424 : 1133 : case BUILT_IN_SIGNBITD128:
8425 : 1133 : target = expand_builtin_signbit (exp, target);
8426 : 1133 : if (target)
8427 : : return target;
8428 : : break;
8429 : :
8430 : : /* Various hooks for the DWARF 2 __throw routine. */
8431 : 34 : case BUILT_IN_UNWIND_INIT:
8432 : 34 : expand_builtin_unwind_init ();
8433 : 34 : return const0_rtx;
8434 : 1165 : case BUILT_IN_DWARF_CFA:
8435 : 1165 : return virtual_cfa_rtx;
8436 : : #ifdef DWARF2_UNWIND_INFO
8437 : 48 : case BUILT_IN_DWARF_SP_COLUMN:
8438 : 48 : return expand_builtin_dwarf_sp_column ();
8439 : 8 : case BUILT_IN_INIT_DWARF_REG_SIZES:
8440 : 8 : expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
8441 : 8 : return const0_rtx;
8442 : : #endif
8443 : 19 : case BUILT_IN_FROB_RETURN_ADDR:
8444 : 19 : return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
8445 : 2296 : case BUILT_IN_EXTRACT_RETURN_ADDR:
8446 : 2296 : return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
8447 : 29 : case BUILT_IN_EH_RETURN:
8448 : 58 : expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
8449 : 29 : CALL_EXPR_ARG (exp, 1));
8450 : 29 : return const0_rtx;
8451 : 21 : case BUILT_IN_EH_RETURN_DATA_REGNO:
8452 : 21 : return expand_builtin_eh_return_data_regno (exp);
8453 : 2 : case BUILT_IN_EXTEND_POINTER:
8454 : 2 : return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
8455 : 96062 : case BUILT_IN_EH_POINTER:
8456 : 96062 : return expand_builtin_eh_pointer (exp);
8457 : 4983 : case BUILT_IN_EH_FILTER:
8458 : 4983 : return expand_builtin_eh_filter (exp);
8459 : 105145 : case BUILT_IN_EH_COPY_VALUES:
8460 : 105145 : return expand_builtin_eh_copy_values (exp);
8461 : :
8462 : 20883 : case BUILT_IN_VA_START:
8463 : 20883 : return expand_builtin_va_start (exp);
8464 : 12077 : case BUILT_IN_VA_END:
8465 : 12077 : return expand_builtin_va_end (exp);
8466 : 242 : case BUILT_IN_VA_COPY:
8467 : 242 : return expand_builtin_va_copy (exp);
8468 : 1025 : case BUILT_IN_EXPECT:
8469 : 1025 : return expand_builtin_expect (exp, target);
8470 : 5 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
8471 : 5 : return expand_builtin_expect_with_probability (exp, target);
8472 : 75 : case BUILT_IN_ASSUME_ALIGNED:
8473 : 75 : return expand_builtin_assume_aligned (exp, target);
8474 : 2037 : case BUILT_IN_PREFETCH:
8475 : 2037 : expand_builtin_prefetch (exp);
8476 : 2037 : return const0_rtx;
8477 : :
8478 : 290 : case BUILT_IN_INIT_TRAMPOLINE:
8479 : 290 : return expand_builtin_init_trampoline (exp, true);
8480 : 0 : case BUILT_IN_INIT_HEAP_TRAMPOLINE:
8481 : 0 : return expand_builtin_init_trampoline (exp, false);
8482 : 333 : case BUILT_IN_ADJUST_TRAMPOLINE:
8483 : 333 : return expand_builtin_adjust_trampoline (exp);
8484 : :
8485 : 0 : case BUILT_IN_INIT_DESCRIPTOR:
8486 : 0 : return expand_builtin_init_descriptor (exp);
8487 : 0 : case BUILT_IN_ADJUST_DESCRIPTOR:
8488 : 0 : return expand_builtin_adjust_descriptor (exp);
8489 : :
8490 : : case BUILT_IN_GCC_NESTED_PTR_CREATED:
8491 : : case BUILT_IN_GCC_NESTED_PTR_DELETED:
8492 : : break; /* At present, no expansion, just call the function. */
8493 : :
8494 : 87 : case BUILT_IN_FORK:
8495 : 87 : case BUILT_IN_EXECL:
8496 : 87 : case BUILT_IN_EXECV:
8497 : 87 : case BUILT_IN_EXECLP:
8498 : 87 : case BUILT_IN_EXECLE:
8499 : 87 : case BUILT_IN_EXECVP:
8500 : 87 : case BUILT_IN_EXECVE:
8501 : 87 : target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
8502 : 87 : if (target)
8503 : : return target;
8504 : : break;
8505 : :
8506 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_1:
8507 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_2:
8508 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_4:
8509 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_8:
8510 : 707 : case BUILT_IN_SYNC_FETCH_AND_ADD_16:
8511 : 707 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
8512 : 707 : target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
8513 : 707 : if (target)
8514 : : return target;
8515 : : break;
8516 : :
8517 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_1:
8518 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_2:
8519 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_4:
8520 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_8:
8521 : 581 : case BUILT_IN_SYNC_FETCH_AND_SUB_16:
8522 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
8523 : 581 : target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
8524 : 581 : if (target)
8525 : : return target;
8526 : : break;
8527 : :
8528 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_1:
8529 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_2:
8530 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_4:
8531 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_8:
8532 : 512 : case BUILT_IN_SYNC_FETCH_AND_OR_16:
8533 : 512 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8534 : 512 : target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8535 : 512 : if (target)
8536 : : return target;
8537 : : break;
8538 : :
8539 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_1:
8540 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_2:
8541 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_4:
8542 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_8:
8543 : 488 : case BUILT_IN_SYNC_FETCH_AND_AND_16:
8544 : 488 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8545 : 488 : target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8546 : 488 : if (target)
8547 : : return target;
8548 : : break;
8549 : :
8550 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8551 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8552 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8553 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8554 : 581 : case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8555 : 581 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8556 : 581 : target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8557 : 581 : if (target)
8558 : : return target;
8559 : : break;
8560 : :
8561 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8562 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8563 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8564 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8565 : 434 : case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8566 : 434 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8567 : 434 : target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8568 : 434 : if (target)
8569 : : return target;
8570 : : break;
8571 : :
8572 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8573 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8574 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8575 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8576 : 149 : case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8577 : 149 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8578 : 149 : target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8579 : 149 : if (target)
8580 : : return target;
8581 : : break;
8582 : :
8583 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8584 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8585 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8586 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8587 : 117 : case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8588 : 117 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8589 : 117 : target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8590 : 117 : if (target)
8591 : : return target;
8592 : : break;
8593 : :
8594 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_1:
8595 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_2:
8596 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_4:
8597 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_8:
8598 : 115 : case BUILT_IN_SYNC_OR_AND_FETCH_16:
8599 : 115 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8600 : 115 : target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8601 : 115 : if (target)
8602 : : return target;
8603 : : break;
8604 : :
8605 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_1:
8606 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_2:
8607 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_4:
8608 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_8:
8609 : 116 : case BUILT_IN_SYNC_AND_AND_FETCH_16:
8610 : 116 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8611 : 116 : target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8612 : 116 : if (target)
8613 : : return target;
8614 : : break;
8615 : :
8616 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8617 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8618 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8619 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8620 : 114 : case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8621 : 114 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8622 : 114 : target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8623 : 114 : if (target)
8624 : : return target;
8625 : : break;
8626 : :
8627 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8628 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8629 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8630 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8631 : 83 : case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8632 : 83 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8633 : 83 : target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8634 : 83 : if (target)
8635 : : return target;
8636 : : break;
8637 : :
8638 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8639 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8640 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8641 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8642 : 200 : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8643 : 200 : if (mode == VOIDmode)
8644 : 40 : mode = TYPE_MODE (boolean_type_node);
8645 : 200 : if (!target || !register_operand (target, mode))
8646 : 40 : target = gen_reg_rtx (mode);
8647 : :
8648 : 200 : mode = get_builtin_sync_mode
8649 : 200 : (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8650 : 200 : target = expand_builtin_compare_and_swap (mode, exp, true, target);
8651 : 200 : if (target)
8652 : : return target;
8653 : : break;
8654 : :
8655 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8656 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8657 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8658 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8659 : 256 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8660 : 256 : mode = get_builtin_sync_mode
8661 : 256 : (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8662 : 256 : target = expand_builtin_compare_and_swap (mode, exp, false, target);
8663 : 256 : if (target)
8664 : : return target;
8665 : : break;
8666 : :
8667 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8668 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8669 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8670 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8671 : 326 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8672 : 326 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8673 : 326 : target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8674 : 326 : if (target)
8675 : : return target;
8676 : : break;
8677 : :
8678 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_1:
8679 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_2:
8680 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_4:
8681 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_8:
8682 : 158 : case BUILT_IN_SYNC_LOCK_RELEASE_16:
8683 : 158 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8684 : 158 : if (expand_builtin_sync_lock_release (mode, exp))
8685 : 156 : return const0_rtx;
8686 : : break;
8687 : :
8688 : 281 : case BUILT_IN_SYNC_SYNCHRONIZE:
8689 : 281 : expand_builtin_sync_synchronize ();
8690 : 281 : return const0_rtx;
8691 : :
8692 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_1:
8693 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_2:
8694 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_4:
8695 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_8:
8696 : 2811 : case BUILT_IN_ATOMIC_EXCHANGE_16:
8697 : 2811 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8698 : 2811 : target = expand_builtin_atomic_exchange (mode, exp, target);
8699 : 2811 : if (target)
8700 : : return target;
8701 : : break;
8702 : :
8703 : 9204 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8704 : 9204 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8705 : 9204 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8706 : 9204 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8707 : 9204 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8708 : 9204 : {
8709 : 9204 : unsigned int nargs, z;
8710 : 9204 : vec<tree, va_gc> *vec;
8711 : :
8712 : 9204 : mode =
8713 : 9204 : get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8714 : 9204 : target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8715 : 9204 : if (target)
8716 : 7410 : return target;
8717 : :
8718 : : /* If this is turned into an external library call, the weak parameter
8719 : : must be dropped to match the expected parameter list. */
8720 : 1794 : nargs = call_expr_nargs (exp);
8721 : 1794 : vec_alloc (vec, nargs - 1);
8722 : 8970 : for (z = 0; z < 3; z++)
8723 : 5382 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8724 : : /* Skip the boolean weak parameter. */
8725 : 5382 : for (z = 4; z < 6; z++)
8726 : 3588 : vec->quick_push (CALL_EXPR_ARG (exp, z));
8727 : 1794 : exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8728 : 1794 : break;
8729 : : }
8730 : :
8731 : 70615 : case BUILT_IN_ATOMIC_LOAD_1:
8732 : 70615 : case BUILT_IN_ATOMIC_LOAD_2:
8733 : 70615 : case BUILT_IN_ATOMIC_LOAD_4:
8734 : 70615 : case BUILT_IN_ATOMIC_LOAD_8:
8735 : 70615 : case BUILT_IN_ATOMIC_LOAD_16:
8736 : 70615 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8737 : 70615 : target = expand_builtin_atomic_load (mode, exp, target);
8738 : 70615 : if (target)
8739 : : return target;
8740 : : break;
8741 : :
8742 : 17481 : case BUILT_IN_ATOMIC_STORE_1:
8743 : 17481 : case BUILT_IN_ATOMIC_STORE_2:
8744 : 17481 : case BUILT_IN_ATOMIC_STORE_4:
8745 : 17481 : case BUILT_IN_ATOMIC_STORE_8:
8746 : 17481 : case BUILT_IN_ATOMIC_STORE_16:
8747 : 17481 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8748 : 17481 : target = expand_builtin_atomic_store (mode, exp);
8749 : 17481 : if (target)
8750 : 15854 : return const0_rtx;
8751 : : break;
8752 : :
8753 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_1:
8754 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_2:
8755 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_4:
8756 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_8:
8757 : 4377 : case BUILT_IN_ATOMIC_ADD_FETCH_16:
8758 : 4377 : {
8759 : 4377 : enum built_in_function lib;
8760 : 4377 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8761 : 4377 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8762 : : (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8763 : 4377 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8764 : : ignore, lib);
8765 : 4377 : if (target)
8766 : : return target;
8767 : : break;
8768 : : }
8769 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_1:
8770 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_2:
8771 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_4:
8772 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_8:
8773 : 2166 : case BUILT_IN_ATOMIC_SUB_FETCH_16:
8774 : 2166 : {
8775 : 2166 : enum built_in_function lib;
8776 : 2166 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8777 : 2166 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8778 : : (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8779 : 2166 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8780 : : ignore, lib);
8781 : 2166 : if (target)
8782 : : return target;
8783 : : break;
8784 : : }
8785 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_1:
8786 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_2:
8787 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_4:
8788 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_8:
8789 : 891 : case BUILT_IN_ATOMIC_AND_FETCH_16:
8790 : 891 : {
8791 : 891 : enum built_in_function lib;
8792 : 891 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8793 : 891 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8794 : : (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8795 : 891 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8796 : : ignore, lib);
8797 : 891 : if (target)
8798 : : return target;
8799 : : break;
8800 : : }
8801 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_1:
8802 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_2:
8803 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_4:
8804 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_8:
8805 : 155 : case BUILT_IN_ATOMIC_NAND_FETCH_16:
8806 : 155 : {
8807 : 155 : enum built_in_function lib;
8808 : 155 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8809 : 155 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8810 : : (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8811 : 155 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8812 : : ignore, lib);
8813 : 155 : if (target)
8814 : : return target;
8815 : : break;
8816 : : }
8817 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_1:
8818 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_2:
8819 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_4:
8820 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_8:
8821 : 836 : case BUILT_IN_ATOMIC_XOR_FETCH_16:
8822 : 836 : {
8823 : 836 : enum built_in_function lib;
8824 : 836 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8825 : 836 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8826 : : (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8827 : 836 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8828 : : ignore, lib);
8829 : 836 : if (target)
8830 : : return target;
8831 : : break;
8832 : : }
8833 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_1:
8834 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_2:
8835 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_4:
8836 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_8:
8837 : 953 : case BUILT_IN_ATOMIC_OR_FETCH_16:
8838 : 953 : {
8839 : 953 : enum built_in_function lib;
8840 : 953 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8841 : 953 : lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8842 : : (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8843 : 953 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8844 : : ignore, lib);
8845 : 953 : if (target)
8846 : : return target;
8847 : : break;
8848 : : }
8849 : 15491 : case BUILT_IN_ATOMIC_FETCH_ADD_1:
8850 : 15491 : case BUILT_IN_ATOMIC_FETCH_ADD_2:
8851 : 15491 : case BUILT_IN_ATOMIC_FETCH_ADD_4:
8852 : 15491 : case BUILT_IN_ATOMIC_FETCH_ADD_8:
8853 : 15491 : case BUILT_IN_ATOMIC_FETCH_ADD_16:
8854 : 15491 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8855 : 15491 : target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8856 : : ignore, BUILT_IN_NONE);
8857 : 15491 : if (target)
8858 : : return target;
8859 : : break;
8860 : :
8861 : 2416 : case BUILT_IN_ATOMIC_FETCH_SUB_1:
8862 : 2416 : case BUILT_IN_ATOMIC_FETCH_SUB_2:
8863 : 2416 : case BUILT_IN_ATOMIC_FETCH_SUB_4:
8864 : 2416 : case BUILT_IN_ATOMIC_FETCH_SUB_8:
8865 : 2416 : case BUILT_IN_ATOMIC_FETCH_SUB_16:
8866 : 2416 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8867 : 2416 : target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8868 : : ignore, BUILT_IN_NONE);
8869 : 2416 : if (target)
8870 : : return target;
8871 : : break;
8872 : :
8873 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_1:
8874 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_2:
8875 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_4:
8876 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_8:
8877 : 772 : case BUILT_IN_ATOMIC_FETCH_AND_16:
8878 : 772 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8879 : 772 : target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8880 : : ignore, BUILT_IN_NONE);
8881 : 772 : if (target)
8882 : : return target;
8883 : : break;
8884 : :
8885 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_1:
8886 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_2:
8887 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_4:
8888 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_8:
8889 : 118 : case BUILT_IN_ATOMIC_FETCH_NAND_16:
8890 : 118 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8891 : 118 : target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8892 : : ignore, BUILT_IN_NONE);
8893 : 118 : if (target)
8894 : : return target;
8895 : : break;
8896 : :
8897 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_1:
8898 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_2:
8899 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_4:
8900 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_8:
8901 : 862 : case BUILT_IN_ATOMIC_FETCH_XOR_16:
8902 : 862 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8903 : 862 : target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8904 : : ignore, BUILT_IN_NONE);
8905 : 862 : if (target)
8906 : : return target;
8907 : : break;
8908 : :
8909 : 1145 : case BUILT_IN_ATOMIC_FETCH_OR_1:
8910 : 1145 : case BUILT_IN_ATOMIC_FETCH_OR_2:
8911 : 1145 : case BUILT_IN_ATOMIC_FETCH_OR_4:
8912 : 1145 : case BUILT_IN_ATOMIC_FETCH_OR_8:
8913 : 1145 : case BUILT_IN_ATOMIC_FETCH_OR_16:
8914 : 1145 : mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8915 : 1145 : target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8916 : : ignore, BUILT_IN_NONE);
8917 : 1145 : if (target)
8918 : : return target;
8919 : : break;
8920 : :
8921 : 259 : case BUILT_IN_ATOMIC_TEST_AND_SET:
8922 : 259 : target = expand_builtin_atomic_test_and_set (exp, target);
8923 : 259 : if (target)
8924 : : return target;
8925 : : break;
8926 : :
8927 : 57 : case BUILT_IN_ATOMIC_CLEAR:
8928 : 57 : return expand_builtin_atomic_clear (exp);
8929 : :
8930 : 1 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8931 : 1 : return expand_builtin_atomic_always_lock_free (exp);
8932 : :
8933 : 3 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8934 : 3 : target = expand_builtin_atomic_is_lock_free (exp);
8935 : 3 : if (target)
8936 : : return target;
8937 : : break;
8938 : :
8939 : 693 : case BUILT_IN_ATOMIC_THREAD_FENCE:
8940 : 693 : expand_builtin_atomic_thread_fence (exp);
8941 : 693 : return const0_rtx;
8942 : :
8943 : 60 : case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8944 : 60 : expand_builtin_atomic_signal_fence (exp);
8945 : 60 : return const0_rtx;
8946 : :
8947 : 617 : case BUILT_IN_OBJECT_SIZE:
8948 : 617 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8949 : 617 : return expand_builtin_object_size (exp);
8950 : :
8951 : 837 : case BUILT_IN_MEMCPY_CHK:
8952 : 837 : case BUILT_IN_MEMPCPY_CHK:
8953 : 837 : case BUILT_IN_MEMMOVE_CHK:
8954 : 837 : case BUILT_IN_MEMSET_CHK:
8955 : 837 : target = expand_builtin_memory_chk (exp, target, mode, fcode);
8956 : 837 : if (target)
8957 : : return target;
8958 : : break;
8959 : :
8960 : 1135 : case BUILT_IN_STRCPY_CHK:
8961 : 1135 : case BUILT_IN_STPCPY_CHK:
8962 : 1135 : case BUILT_IN_STRNCPY_CHK:
8963 : 1135 : case BUILT_IN_STPNCPY_CHK:
8964 : 1135 : case BUILT_IN_STRCAT_CHK:
8965 : 1135 : case BUILT_IN_STRNCAT_CHK:
8966 : 1135 : case BUILT_IN_SNPRINTF_CHK:
8967 : 1135 : case BUILT_IN_VSNPRINTF_CHK:
8968 : 1135 : maybe_emit_chk_warning (exp, fcode);
8969 : 1135 : break;
8970 : :
8971 : 1329 : case BUILT_IN_SPRINTF_CHK:
8972 : 1329 : case BUILT_IN_VSPRINTF_CHK:
8973 : 1329 : maybe_emit_sprintf_chk_warning (exp, fcode);
8974 : 1329 : break;
8975 : :
8976 : 3 : case BUILT_IN_THREAD_POINTER:
8977 : 3 : return expand_builtin_thread_pointer (exp, target);
8978 : :
8979 : 0 : case BUILT_IN_SET_THREAD_POINTER:
8980 : 0 : expand_builtin_set_thread_pointer (exp);
8981 : 0 : return const0_rtx;
8982 : :
8983 : : case BUILT_IN_ACC_ON_DEVICE:
8984 : : /* Do library call, if we failed to expand the builtin when
8985 : : folding. */
8986 : : break;
8987 : :
8988 : 356 : case BUILT_IN_GOACC_PARLEVEL_ID:
8989 : 356 : case BUILT_IN_GOACC_PARLEVEL_SIZE:
8990 : 356 : return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8991 : :
8992 : 4 : case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8993 : 4 : return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8994 : :
8995 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8996 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8997 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8998 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8999 : 30 : case BUILT_IN_SPECULATION_SAFE_VALUE_16:
9000 : 30 : mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
9001 : 30 : return expand_speculation_safe_value (mode, exp, target, ignore);
9002 : :
9003 : 3 : case BUILT_IN_CRC8_DATA8:
9004 : 3 : return expand_builtin_crc_table_based (IFN_CRC, QImode, QImode, mode,
9005 : : exp, target);
9006 : 2 : case BUILT_IN_CRC16_DATA8:
9007 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, QImode, mode,
9008 : : exp, target);
9009 : 2 : case BUILT_IN_CRC16_DATA16:
9010 : 2 : return expand_builtin_crc_table_based (IFN_CRC, HImode, HImode, mode,
9011 : : exp, target);
9012 : 2 : case BUILT_IN_CRC32_DATA8:
9013 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, QImode, mode,
9014 : : exp, target);
9015 : 2 : case BUILT_IN_CRC32_DATA16:
9016 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, HImode, mode,
9017 : : exp, target);
9018 : 2 : case BUILT_IN_CRC32_DATA32:
9019 : 2 : return expand_builtin_crc_table_based (IFN_CRC, SImode, SImode, mode,
9020 : : exp, target);
9021 : 1 : case BUILT_IN_CRC64_DATA8:
9022 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, QImode, mode,
9023 : : exp, target);
9024 : 1 : case BUILT_IN_CRC64_DATA16:
9025 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, HImode, mode,
9026 : : exp, target);
9027 : 1 : case BUILT_IN_CRC64_DATA32:
9028 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, SImode, mode,
9029 : : exp, target);
9030 : 1 : case BUILT_IN_CRC64_DATA64:
9031 : 1 : return expand_builtin_crc_table_based (IFN_CRC, DImode, DImode, mode,
9032 : : exp, target);
9033 : 2 : case BUILT_IN_REV_CRC8_DATA8:
9034 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, QImode, QImode,
9035 : : mode, exp, target);
9036 : 2 : case BUILT_IN_REV_CRC16_DATA8:
9037 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, QImode,
9038 : : mode, exp, target);
9039 : 2 : case BUILT_IN_REV_CRC16_DATA16:
9040 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, HImode,
9041 : : mode, exp, target);
9042 : 3 : case BUILT_IN_REV_CRC32_DATA8:
9043 : 3 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, QImode,
9044 : : mode, exp, target);
9045 : 2 : case BUILT_IN_REV_CRC32_DATA16:
9046 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, HImode,
9047 : : mode, exp, target);
9048 : 2 : case BUILT_IN_REV_CRC32_DATA32:
9049 : 2 : return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, SImode,
9050 : : mode, exp, target);
9051 : 1 : case BUILT_IN_REV_CRC64_DATA8:
9052 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, QImode,
9053 : : mode, exp, target);
9054 : 1 : case BUILT_IN_REV_CRC64_DATA16:
9055 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, HImode,
9056 : : mode, exp, target);
9057 : 1 : case BUILT_IN_REV_CRC64_DATA32:
9058 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, SImode,
9059 : : mode, exp, target);
9060 : 1 : case BUILT_IN_REV_CRC64_DATA64:
9061 : 1 : return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, DImode,
9062 : : mode, exp, target);
9063 : : default: /* just do library call, if unknown builtin */
9064 : : break;
9065 : : }
9066 : :
9067 : : /* The switch statement above can drop through to cause the function
9068 : : to be called normally. */
9069 : 859941 : return expand_call (exp, target, ignore);
9070 : : }
9071 : :
9072 : : /* Determine whether a tree node represents a call to a built-in
9073 : : function. If the tree T is a call to a built-in function with
9074 : : the right number of arguments of the appropriate types, return
9075 : : the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
9076 : : Otherwise the return value is END_BUILTINS. */
9077 : :
9078 : : enum built_in_function
9079 : 92983275 : builtin_mathfn_code (const_tree t)
9080 : : {
9081 : 92983275 : const_tree fndecl, arg, parmlist;
9082 : 92983275 : const_tree argtype, parmtype;
9083 : 92983275 : const_call_expr_arg_iterator iter;
9084 : :
9085 : 92983275 : if (TREE_CODE (t) != CALL_EXPR)
9086 : : return END_BUILTINS;
9087 : :
9088 : 1331705 : fndecl = get_callee_fndecl (t);
9089 : 1331705 : if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9090 : : return END_BUILTINS;
9091 : :
9092 : 762649 : parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
9093 : 762649 : init_const_call_expr_arg_iterator (t, &iter);
9094 : 2494809 : for (; parmlist; parmlist = TREE_CHAIN (parmlist))
9095 : : {
9096 : : /* If a function doesn't take a variable number of arguments,
9097 : : the last element in the list will have type `void'. */
9098 : 1731951 : parmtype = TREE_VALUE (parmlist);
9099 : 1731951 : if (VOID_TYPE_P (parmtype))
9100 : : {
9101 : 762426 : if (more_const_call_expr_args_p (&iter))
9102 : : return END_BUILTINS;
9103 : 762426 : return DECL_FUNCTION_CODE (fndecl);
9104 : : }
9105 : :
9106 : 969525 : if (! more_const_call_expr_args_p (&iter))
9107 : : return END_BUILTINS;
9108 : :
9109 : 969525 : arg = next_const_call_expr_arg (&iter);
9110 : 969525 : argtype = TREE_TYPE (arg);
9111 : :
9112 : 969525 : if (SCALAR_FLOAT_TYPE_P (parmtype))
9113 : : {
9114 : 714792 : if (! SCALAR_FLOAT_TYPE_P (argtype))
9115 : : return END_BUILTINS;
9116 : : }
9117 : 254733 : else if (COMPLEX_FLOAT_TYPE_P (parmtype))
9118 : : {
9119 : 11409 : if (! COMPLEX_FLOAT_TYPE_P (argtype))
9120 : : return END_BUILTINS;
9121 : : }
9122 : 243324 : else if (POINTER_TYPE_P (parmtype))
9123 : : {
9124 : 71119 : if (! POINTER_TYPE_P (argtype))
9125 : : return END_BUILTINS;
9126 : : }
9127 : 172205 : else if (INTEGRAL_TYPE_P (parmtype))
9128 : : {
9129 : 172205 : if (! INTEGRAL_TYPE_P (argtype))
9130 : : return END_BUILTINS;
9131 : : }
9132 : : else
9133 : : return END_BUILTINS;
9134 : : }
9135 : :
9136 : : /* Variable-length argument list. */
9137 : 209 : return DECL_FUNCTION_CODE (fndecl);
9138 : : }
9139 : :
9140 : : /* Fold a call to __builtin_constant_p, if we know its argument ARG will
9141 : : evaluate to a constant. */
9142 : :
9143 : : static tree
9144 : 780733 : fold_builtin_constant_p (tree arg)
9145 : : {
9146 : : /* We return 1 for a numeric type that's known to be a constant
9147 : : value at compile-time or for an aggregate type that's a
9148 : : literal constant. */
9149 : 780733 : STRIP_NOPS (arg);
9150 : :
9151 : : /* If we know this is a constant, emit the constant of one. */
9152 : 780733 : if (CONSTANT_CLASS_P (arg)
9153 : 780733 : || (TREE_CODE (arg) == CONSTRUCTOR
9154 : 10 : && TREE_CONSTANT (arg)))
9155 : 26683 : return integer_one_node;
9156 : 754050 : if (TREE_CODE (arg) == ADDR_EXPR)
9157 : : {
9158 : 110 : tree op = TREE_OPERAND (arg, 0);
9159 : 110 : if (TREE_CODE (op) == STRING_CST
9160 : 110 : || (TREE_CODE (op) == ARRAY_REF
9161 : 57 : && integer_zerop (TREE_OPERAND (op, 1))
9162 : 57 : && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
9163 : 87 : return integer_one_node;
9164 : : }
9165 : :
9166 : : /* If this expression has side effects, show we don't know it to be a
9167 : : constant. Likewise if it's a pointer or aggregate type since in
9168 : : those case we only want literals, since those are only optimized
9169 : : when generating RTL, not later.
9170 : : And finally, if we are compiling an initializer, not code, we
9171 : : need to return a definite result now; there's not going to be any
9172 : : more optimization done. */
9173 : 753963 : if (TREE_SIDE_EFFECTS (arg)
9174 : 753838 : || AGGREGATE_TYPE_P (TREE_TYPE (arg))
9175 : 753823 : || POINTER_TYPE_P (TREE_TYPE (arg))
9176 : 753658 : || cfun == 0
9177 : 753600 : || folding_initializer
9178 : 1507556 : || force_folding_builtin_constant_p)
9179 : 388 : return integer_zero_node;
9180 : :
9181 : : return NULL_TREE;
9182 : : }
9183 : :
9184 : : /* Create builtin_expect or builtin_expect_with_probability
9185 : : with PRED and EXPECTED as its arguments and return it as a truthvalue.
9186 : : Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
9187 : : builtin_expect_with_probability instead uses third argument as PROBABILITY
9188 : : value. */
9189 : :
9190 : : static tree
9191 : 49770 : build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
9192 : : tree predictor, tree probability)
9193 : : {
9194 : 49770 : tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
9195 : :
9196 : 49770 : fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
9197 : : : BUILT_IN_EXPECT_WITH_PROBABILITY);
9198 : 49770 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
9199 : 49770 : ret_type = TREE_TYPE (TREE_TYPE (fn));
9200 : 49770 : pred_type = TREE_VALUE (arg_types);
9201 : 49770 : expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
9202 : :
9203 : 49770 : pred = fold_convert_loc (loc, pred_type, pred);
9204 : 49770 : expected = fold_convert_loc (loc, expected_type, expected);
9205 : :
9206 : 49770 : if (probability)
9207 : 0 : call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
9208 : : else
9209 : 99540 : call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
9210 : : predictor);
9211 : :
9212 : 49770 : return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
9213 : 49770 : build_int_cst (ret_type, 0));
9214 : : }
9215 : :
9216 : : /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
9217 : : NULL_TREE if no simplification is possible. */
9218 : :
9219 : : tree
9220 : 5551351 : fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
9221 : : tree arg3)
9222 : : {
9223 : 5551351 : tree inner, fndecl, inner_arg0;
9224 : 5551351 : enum tree_code code;
9225 : :
9226 : : /* Distribute the expected value over short-circuiting operators.
9227 : : See through the cast from truthvalue_type_node to long. */
9228 : 5551351 : inner_arg0 = arg0;
9229 : 11235580 : while (CONVERT_EXPR_P (inner_arg0)
9230 : 417349 : && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
9231 : 6386048 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
9232 : 417348 : inner_arg0 = TREE_OPERAND (inner_arg0, 0);
9233 : :
9234 : : /* If this is a builtin_expect within a builtin_expect keep the
9235 : : inner one. See through a comparison against a constant. It
9236 : : might have been added to create a thruthvalue. */
9237 : 5551351 : inner = inner_arg0;
9238 : :
9239 : 5551351 : if (COMPARISON_CLASS_P (inner)
9240 : 5551351 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
9241 : 704268 : inner = TREE_OPERAND (inner, 0);
9242 : :
9243 : 5551351 : if (TREE_CODE (inner) == CALL_EXPR
9244 : 54718 : && (fndecl = get_callee_fndecl (inner))
9245 : 5606069 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
9246 : : BUILT_IN_EXPECT_WITH_PROBABILITY))
9247 : : return arg0;
9248 : :
9249 : 5551343 : inner = inner_arg0;
9250 : 5551343 : code = TREE_CODE (inner);
9251 : 5551343 : if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
9252 : : {
9253 : 24885 : tree op0 = TREE_OPERAND (inner, 0);
9254 : 24885 : tree op1 = TREE_OPERAND (inner, 1);
9255 : 24885 : arg1 = save_expr (arg1);
9256 : :
9257 : 24885 : op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
9258 : 24885 : op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
9259 : 24885 : inner = build2 (code, TREE_TYPE (inner), op0, op1);
9260 : :
9261 : 24885 : return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
9262 : : }
9263 : :
9264 : : /* If the argument isn't invariant then there's nothing else we can do. */
9265 : 5526458 : if (!TREE_CONSTANT (inner_arg0))
9266 : : return NULL_TREE;
9267 : :
9268 : : /* If we expect that a comparison against the argument will fold to
9269 : : a constant return the constant. In practice, this means a true
9270 : : constant or the address of a non-weak symbol. */
9271 : 66722 : inner = inner_arg0;
9272 : 66722 : STRIP_NOPS (inner);
9273 : 66722 : if (TREE_CODE (inner) == ADDR_EXPR)
9274 : : {
9275 : 1 : do
9276 : : {
9277 : 1 : inner = TREE_OPERAND (inner, 0);
9278 : : }
9279 : 1 : while (TREE_CODE (inner) == COMPONENT_REF
9280 : 1 : || TREE_CODE (inner) == ARRAY_REF);
9281 : 1 : if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
9282 : : return NULL_TREE;
9283 : : }
9284 : :
9285 : : /* Otherwise, ARG0 already has the proper type for the return value. */
9286 : : return arg0;
9287 : : }
9288 : :
9289 : : /* Fold a call to __builtin_classify_type with argument ARG. */
9290 : :
9291 : : static tree
9292 : 2146 : fold_builtin_classify_type (tree arg)
9293 : : {
9294 : 2146 : if (arg == 0)
9295 : 0 : return build_int_cst (integer_type_node, no_type_class);
9296 : :
9297 : 2146 : return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
9298 : : }
9299 : :
9300 : : /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
9301 : : ARG. */
9302 : :
9303 : : static tree
9304 : 416512 : fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
9305 : : {
9306 : 416512 : if (!validate_arg (arg, POINTER_TYPE))
9307 : : return NULL_TREE;
9308 : : else
9309 : : {
9310 : 416509 : c_strlen_data lendata = { };
9311 : 416509 : tree len = c_strlen (arg, 0, &lendata);
9312 : :
9313 : 416509 : if (len)
9314 : 2359 : return fold_convert_loc (loc, type, len);
9315 : :
9316 : : /* TODO: Move this to gimple-ssa-warn-access once the pass runs
9317 : : also early enough to detect invalid reads in multimensional
9318 : : arrays and struct members. */
9319 : 414150 : if (!lendata.decl)
9320 : 407031 : c_strlen (arg, 1, &lendata);
9321 : :
9322 : 414150 : if (lendata.decl)
9323 : : {
9324 : 7133 : if (EXPR_HAS_LOCATION (arg))
9325 : 2812 : loc = EXPR_LOCATION (arg);
9326 : 4321 : else if (loc == UNKNOWN_LOCATION)
9327 : 0 : loc = input_location;
9328 : 7133 : warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
9329 : : }
9330 : :
9331 : 414150 : return NULL_TREE;
9332 : : }
9333 : : }
9334 : :
9335 : : /* Fold a call to __builtin_inf or __builtin_huge_val. */
9336 : :
9337 : : static tree
9338 : 228280 : fold_builtin_inf (location_t loc, tree type, int warn)
9339 : : {
9340 : : /* __builtin_inff is intended to be usable to define INFINITY on all
9341 : : targets. If an infinity is not available, INFINITY expands "to a
9342 : : positive constant of type float that overflows at translation
9343 : : time", footnote "In this case, using INFINITY will violate the
9344 : : constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
9345 : : Thus we pedwarn to ensure this constraint violation is
9346 : : diagnosed. */
9347 : 912311 : if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
9348 : 0 : pedwarn (loc, 0, "target format does not support infinity");
9349 : :
9350 : 228280 : return build_real (type, dconstinf);
9351 : : }
9352 : :
9353 : : /* Fold function call to builtin sincos, sincosf, or sincosl. Return
9354 : : NULL_TREE if no simplification can be made. */
9355 : :
9356 : : static tree
9357 : 144 : fold_builtin_sincos (location_t loc,
9358 : : tree arg0, tree arg1, tree arg2)
9359 : : {
9360 : 144 : tree type;
9361 : 144 : tree fndecl, call = NULL_TREE;
9362 : :
9363 : 144 : if (!validate_arg (arg0, REAL_TYPE)
9364 : 144 : || !validate_arg (arg1, POINTER_TYPE)
9365 : 288 : || !validate_arg (arg2, POINTER_TYPE))
9366 : : return NULL_TREE;
9367 : :
9368 : 144 : type = TREE_TYPE (arg0);
9369 : :
9370 : : /* Calculate the result when the argument is a constant. */
9371 : 144 : built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
9372 : 144 : if (fn == END_BUILTINS)
9373 : : return NULL_TREE;
9374 : :
9375 : : /* Canonicalize sincos to cexpi. */
9376 : 144 : if (TREE_CODE (arg0) == REAL_CST)
9377 : : {
9378 : 86 : tree complex_type = build_complex_type (type);
9379 : 86 : call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
9380 : : }
9381 : 86 : if (!call)
9382 : : {
9383 : 58 : if (!targetm.libc_has_function (function_c99_math_complex, type)
9384 : 58 : || !builtin_decl_implicit_p (fn))
9385 : : return NULL_TREE;
9386 : 58 : fndecl = builtin_decl_explicit (fn);
9387 : 58 : call = build_call_expr_loc (loc, fndecl, 1, arg0);
9388 : 58 : call = builtin_save_expr (call);
9389 : : }
9390 : :
9391 : 144 : tree ptype = build_pointer_type (type);
9392 : 144 : arg1 = fold_convert (ptype, arg1);
9393 : 144 : arg2 = fold_convert (ptype, arg2);
9394 : 144 : return build2 (COMPOUND_EXPR, void_type_node,
9395 : : build2 (MODIFY_EXPR, void_type_node,
9396 : : build_fold_indirect_ref_loc (loc, arg1),
9397 : : fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
9398 : : build2 (MODIFY_EXPR, void_type_node,
9399 : : build_fold_indirect_ref_loc (loc, arg2),
9400 : 144 : fold_build1_loc (loc, REALPART_EXPR, type, call)));
9401 : : }
9402 : :
9403 : : /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9404 : : Return NULL_TREE if no simplification can be made. */
9405 : :
9406 : : static tree
9407 : 2234461 : fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9408 : : {
9409 : 2234461 : if (!validate_arg (arg1, POINTER_TYPE)
9410 : 2234461 : || !validate_arg (arg2, POINTER_TYPE)
9411 : 4468922 : || !validate_arg (len, INTEGER_TYPE))
9412 : : return NULL_TREE;
9413 : :
9414 : : /* If the LEN parameter is zero, return zero. */
9415 : 2234461 : if (integer_zerop (len))
9416 : 0 : return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9417 : 0 : arg1, arg2);
9418 : :
9419 : : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9420 : 2234461 : if (operand_equal_p (arg1, arg2, 0))
9421 : 736 : return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9422 : :
9423 : : /* If len parameter is one, return an expression corresponding to
9424 : : (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9425 : 2233725 : if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9426 : : {
9427 : 19232 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9428 : 19232 : tree cst_uchar_ptr_node
9429 : 19232 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9430 : :
9431 : 19232 : tree ind1
9432 : 19232 : = fold_convert_loc (loc, integer_type_node,
9433 : : build1 (INDIRECT_REF, cst_uchar_node,
9434 : : fold_convert_loc (loc,
9435 : : cst_uchar_ptr_node,
9436 : : arg1)));
9437 : 19232 : tree ind2
9438 : 19232 : = fold_convert_loc (loc, integer_type_node,
9439 : : build1 (INDIRECT_REF, cst_uchar_node,
9440 : : fold_convert_loc (loc,
9441 : : cst_uchar_ptr_node,
9442 : : arg2)));
9443 : 19232 : return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9444 : : }
9445 : :
9446 : : return NULL_TREE;
9447 : : }
9448 : :
9449 : : /* Fold a call to builtin isascii with argument ARG. */
9450 : :
9451 : : static tree
9452 : 211 : fold_builtin_isascii (location_t loc, tree arg)
9453 : : {
9454 : 211 : if (!validate_arg (arg, INTEGER_TYPE))
9455 : : return NULL_TREE;
9456 : : else
9457 : : {
9458 : : /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9459 : 211 : arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9460 : : build_int_cst (integer_type_node,
9461 : : ~ HOST_WIDE_INT_UC (0x7f)));
9462 : 211 : return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9463 : 211 : arg, integer_zero_node);
9464 : : }
9465 : : }
9466 : :
9467 : : /* Fold a call to builtin toascii with argument ARG. */
9468 : :
9469 : : static tree
9470 : 168 : fold_builtin_toascii (location_t loc, tree arg)
9471 : : {
9472 : 168 : if (!validate_arg (arg, INTEGER_TYPE))
9473 : : return NULL_TREE;
9474 : :
9475 : : /* Transform toascii(c) -> (c & 0x7f). */
9476 : 168 : return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9477 : : build_int_cst (integer_type_node, 0x7f));
9478 : : }
9479 : :
9480 : : /* Fold a call to builtin isdigit with argument ARG. */
9481 : :
9482 : : static tree
9483 : 325 : fold_builtin_isdigit (location_t loc, tree arg)
9484 : : {
9485 : 325 : if (!validate_arg (arg, INTEGER_TYPE))
9486 : : return NULL_TREE;
9487 : : else
9488 : : {
9489 : : /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9490 : : /* According to the C standard, isdigit is unaffected by locale.
9491 : : However, it definitely is affected by the target character set. */
9492 : 313 : unsigned HOST_WIDE_INT target_digit0
9493 : 313 : = lang_hooks.to_target_charset ('0');
9494 : :
9495 : 313 : if (target_digit0 == 0)
9496 : : return NULL_TREE;
9497 : :
9498 : 313 : arg = fold_convert_loc (loc, unsigned_type_node, arg);
9499 : 313 : arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9500 : : build_int_cst (unsigned_type_node, target_digit0));
9501 : 313 : return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9502 : : build_int_cst (unsigned_type_node, 9));
9503 : : }
9504 : : }
9505 : :
9506 : : /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9507 : :
9508 : : static tree
9509 : 359371 : fold_builtin_fabs (location_t loc, tree arg, tree type)
9510 : : {
9511 : 359371 : if (!validate_arg (arg, REAL_TYPE))
9512 : : return NULL_TREE;
9513 : :
9514 : 359286 : arg = fold_convert_loc (loc, type, arg);
9515 : 359286 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9516 : : }
9517 : :
9518 : : /* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
9519 : : with argument ARG. */
9520 : :
9521 : : static tree
9522 : 93475 : fold_builtin_abs (location_t loc, tree arg, tree type)
9523 : : {
9524 : 93475 : if (!validate_arg (arg, INTEGER_TYPE))
9525 : : return NULL_TREE;
9526 : :
9527 : 93448 : if (TYPE_UNSIGNED (type))
9528 : : {
9529 : 1168 : if (TYPE_PRECISION (TREE_TYPE (arg))
9530 : 1168 : != TYPE_PRECISION (type)
9531 : 1168 : || TYPE_UNSIGNED (TREE_TYPE (arg)))
9532 : : return NULL_TREE;
9533 : 1168 : return fold_build1_loc (loc, ABSU_EXPR, type, arg);
9534 : : }
9535 : 92280 : arg = fold_convert_loc (loc, type, arg);
9536 : 92280 : return fold_build1_loc (loc, ABS_EXPR, type, arg);
9537 : : }
9538 : :
9539 : : /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9540 : :
9541 : : static tree
9542 : 114640 : fold_builtin_carg (location_t loc, tree arg, tree type)
9543 : : {
9544 : 114640 : if (validate_arg (arg, COMPLEX_TYPE)
9545 : 114640 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
9546 : : {
9547 : 114640 : tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9548 : :
9549 : 114640 : if (atan2_fn)
9550 : : {
9551 : 110308 : tree new_arg = builtin_save_expr (arg);
9552 : 110308 : tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9553 : 110308 : tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9554 : 110308 : return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9555 : : }
9556 : : }
9557 : :
9558 : : return NULL_TREE;
9559 : : }
9560 : :
9561 : : /* Fold a call to builtin frexp, we can assume the base is 2. */
9562 : :
9563 : : static tree
9564 : 114564 : fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9565 : : {
9566 : 114564 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9567 : : return NULL_TREE;
9568 : :
9569 : 114564 : STRIP_NOPS (arg0);
9570 : :
9571 : 114564 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9572 : : return NULL_TREE;
9573 : :
9574 : 2210 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9575 : :
9576 : : /* Proceed if a valid pointer type was passed in. */
9577 : 2210 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9578 : : {
9579 : 2210 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9580 : 2210 : tree frac, exp, res;
9581 : :
9582 : 2210 : switch (value->cl)
9583 : : {
9584 : 276 : case rvc_zero:
9585 : 276 : case rvc_nan:
9586 : 276 : case rvc_inf:
9587 : : /* For +-0, return (*exp = 0, +-0). */
9588 : : /* For +-NaN or +-Inf, *exp is unspecified, but something should
9589 : : be stored there so that it isn't read from uninitialized object.
9590 : : As glibc and newlib store *exp = 0 for +-Inf/NaN, storing
9591 : : 0 here as well is easiest. */
9592 : 276 : exp = integer_zero_node;
9593 : 276 : frac = arg0;
9594 : 276 : break;
9595 : 1934 : case rvc_normal:
9596 : 1934 : {
9597 : : /* Since the frexp function always expects base 2, and in
9598 : : GCC normalized significands are already in the range
9599 : : [0.5, 1.0), we have exactly what frexp wants. */
9600 : 1934 : REAL_VALUE_TYPE frac_rvt = *value;
9601 : 1934 : SET_REAL_EXP (&frac_rvt, 0);
9602 : 1934 : frac = build_real (rettype, frac_rvt);
9603 : 1934 : exp = build_int_cst (integer_type_node, REAL_EXP (value));
9604 : : }
9605 : 1934 : break;
9606 : 0 : default:
9607 : 0 : gcc_unreachable ();
9608 : : }
9609 : :
9610 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9611 : 2210 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9612 : 2210 : TREE_SIDE_EFFECTS (arg1) = 1;
9613 : 2210 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9614 : 2210 : suppress_warning (res, OPT_Wunused_value);
9615 : 2210 : return res;
9616 : : }
9617 : :
9618 : : return NULL_TREE;
9619 : : }
9620 : :
9621 : : /* Fold a call to builtin modf. */
9622 : :
9623 : : static tree
9624 : 75037 : fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9625 : : {
9626 : 75037 : if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9627 : : return NULL_TREE;
9628 : :
9629 : 75037 : STRIP_NOPS (arg0);
9630 : :
9631 : 75037 : if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9632 : : return NULL_TREE;
9633 : :
9634 : 2474 : arg1 = build_fold_indirect_ref_loc (loc, arg1);
9635 : :
9636 : : /* Proceed if a valid pointer type was passed in. */
9637 : 2474 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9638 : : {
9639 : 2474 : const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9640 : 2474 : REAL_VALUE_TYPE trunc, frac;
9641 : 2474 : tree res;
9642 : :
9643 : 2474 : switch (value->cl)
9644 : : {
9645 : 228 : case rvc_nan:
9646 : 228 : case rvc_zero:
9647 : : /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9648 : 228 : trunc = frac = *value;
9649 : 228 : break;
9650 : 96 : case rvc_inf:
9651 : : /* For +-Inf, return (*arg1 = arg0, +-0). */
9652 : 96 : frac = dconst0;
9653 : 96 : frac.sign = value->sign;
9654 : 96 : trunc = *value;
9655 : 96 : break;
9656 : 2150 : case rvc_normal:
9657 : : /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9658 : 2150 : real_trunc (&trunc, VOIDmode, value);
9659 : 2150 : real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9660 : : /* If the original number was negative and already
9661 : : integral, then the fractional part is -0.0. */
9662 : 2150 : if (value->sign && frac.cl == rvc_zero)
9663 : 69 : frac.sign = value->sign;
9664 : : break;
9665 : : }
9666 : :
9667 : : /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9668 : 2474 : arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9669 : : build_real (rettype, trunc));
9670 : 2474 : TREE_SIDE_EFFECTS (arg1) = 1;
9671 : 2474 : res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9672 : : build_real (rettype, frac));
9673 : 2474 : suppress_warning (res, OPT_Wunused_value);
9674 : 2474 : return res;
9675 : : }
9676 : :
9677 : : return NULL_TREE;
9678 : : }
9679 : :
9680 : : /* Given a location LOC, an interclass builtin function decl FNDECL
9681 : : and its single argument ARG, return an folded expression computing
9682 : : the same, or NULL_TREE if we either couldn't or didn't want to fold
9683 : : (the latter happen if there's an RTL instruction available). */
9684 : :
9685 : : static tree
9686 : 1094102 : fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9687 : : {
9688 : 1094102 : machine_mode mode;
9689 : :
9690 : 1094102 : if (!validate_arg (arg, REAL_TYPE))
9691 : : return NULL_TREE;
9692 : :
9693 : 1094102 : if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9694 : : return NULL_TREE;
9695 : :
9696 : 1094102 : mode = TYPE_MODE (TREE_TYPE (arg));
9697 : :
9698 : 7655628 : bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9699 : :
9700 : : /* If there is no optab, try generic code. */
9701 : 1094102 : switch (DECL_FUNCTION_CODE (fndecl))
9702 : : {
9703 : 263436 : tree result;
9704 : :
9705 : 263436 : CASE_FLT_FN (BUILT_IN_ISINF):
9706 : 263436 : {
9707 : : /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9708 : 263436 : tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9709 : 263436 : tree type = TREE_TYPE (arg);
9710 : 263436 : REAL_VALUE_TYPE r;
9711 : 263436 : char buf[128];
9712 : :
9713 : 263436 : if (is_ibm_extended)
9714 : : {
9715 : : /* NaN and Inf are encoded in the high-order double value
9716 : : only. The low-order value is not significant. */
9717 : 0 : type = double_type_node;
9718 : 0 : mode = DFmode;
9719 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9720 : : }
9721 : 263436 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9722 : 263436 : real_from_string3 (&r, buf, mode);
9723 : 263436 : result = build_call_expr (isgr_fn, 2,
9724 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9725 : : build_real (type, r));
9726 : 263436 : return result;
9727 : : }
9728 : 567474 : CASE_FLT_FN (BUILT_IN_FINITE):
9729 : 567474 : case BUILT_IN_ISFINITE:
9730 : 567474 : {
9731 : : /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9732 : 567474 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9733 : 567474 : tree type = TREE_TYPE (arg);
9734 : 567474 : REAL_VALUE_TYPE r;
9735 : 567474 : char buf[128];
9736 : :
9737 : 567474 : if (is_ibm_extended)
9738 : : {
9739 : : /* NaN and Inf are encoded in the high-order double value
9740 : : only. The low-order value is not significant. */
9741 : 0 : type = double_type_node;
9742 : 0 : mode = DFmode;
9743 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9744 : : }
9745 : 567474 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9746 : 567474 : real_from_string3 (&r, buf, mode);
9747 : 567474 : result = build_call_expr (isle_fn, 2,
9748 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9749 : : build_real (type, r));
9750 : : /*result = fold_build2_loc (loc, UNGT_EXPR,
9751 : : TREE_TYPE (TREE_TYPE (fndecl)),
9752 : : fold_build1_loc (loc, ABS_EXPR, type, arg),
9753 : : build_real (type, r));
9754 : : result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9755 : : TREE_TYPE (TREE_TYPE (fndecl)),
9756 : : result);*/
9757 : 567474 : return result;
9758 : : }
9759 : 261652 : case BUILT_IN_ISNORMAL:
9760 : 261652 : {
9761 : : /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9762 : : islessequal(fabs(x),DBL_MAX). */
9763 : 261652 : tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9764 : 261652 : tree type = TREE_TYPE (arg);
9765 : 261652 : tree orig_arg, max_exp, min_exp;
9766 : 261652 : machine_mode orig_mode = mode;
9767 : 261652 : REAL_VALUE_TYPE rmax, rmin;
9768 : 261652 : char buf[128];
9769 : :
9770 : 261652 : orig_arg = arg = builtin_save_expr (arg);
9771 : 261652 : if (is_ibm_extended)
9772 : : {
9773 : : /* Use double to test the normal range of IBM extended
9774 : : precision. Emin for IBM extended precision is
9775 : : different to emin for IEEE double, being 53 higher
9776 : : since the low double exponent is at least 53 lower
9777 : : than the high double exponent. */
9778 : 0 : type = double_type_node;
9779 : 0 : mode = DFmode;
9780 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9781 : : }
9782 : 261652 : arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9783 : :
9784 : 261652 : get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9785 : 261652 : real_from_string3 (&rmax, buf, mode);
9786 : 261652 : if (DECIMAL_FLOAT_MODE_P (mode))
9787 : 1 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9788 : : else
9789 : 261651 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9790 : 261652 : real_from_string3 (&rmin, buf, orig_mode);
9791 : 261652 : max_exp = build_real (type, rmax);
9792 : 261652 : min_exp = build_real (type, rmin);
9793 : :
9794 : 261652 : max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9795 : 261652 : if (is_ibm_extended)
9796 : : {
9797 : : /* Testing the high end of the range is done just using
9798 : : the high double, using the same test as isfinite().
9799 : : For the subnormal end of the range we first test the
9800 : : high double, then if its magnitude is equal to the
9801 : : limit of 0x1p-969, we test whether the low double is
9802 : : non-zero and opposite sign to the high double. */
9803 : 0 : tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9804 : 0 : tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9805 : 0 : tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9806 : 0 : tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9807 : : arg, min_exp);
9808 : 0 : tree as_complex = build1 (VIEW_CONVERT_EXPR,
9809 : : complex_double_type_node, orig_arg);
9810 : 0 : tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9811 : 0 : tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9812 : 0 : tree zero = build_real (type, dconst0);
9813 : 0 : tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9814 : 0 : tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9815 : 0 : tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9816 : 0 : tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9817 : : fold_build3 (COND_EXPR,
9818 : : integer_type_node,
9819 : : hilt, logt, lolt));
9820 : 0 : eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9821 : : eq_min, ok_lo);
9822 : 0 : min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9823 : : gt_min, eq_min);
9824 : : }
9825 : : else
9826 : : {
9827 : 261652 : tree const isge_fn
9828 : 261652 : = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9829 : 261652 : min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9830 : : }
9831 : 261652 : result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9832 : : max_exp, min_exp);
9833 : 261652 : return result;
9834 : : }
9835 : : default:
9836 : : break;
9837 : : }
9838 : :
9839 : : return NULL_TREE;
9840 : : }
9841 : :
9842 : : /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9843 : : ARG is the argument for the call. */
9844 : :
9845 : : static tree
9846 : 1127071 : fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9847 : : {
9848 : 1127071 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
9849 : :
9850 : 1127071 : if (!validate_arg (arg, REAL_TYPE))
9851 : : return NULL_TREE;
9852 : :
9853 : 1127071 : switch (builtin_index)
9854 : : {
9855 : 265097 : case BUILT_IN_ISINF:
9856 : 265097 : if (tree_expr_infinite_p (arg))
9857 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9858 : 265097 : if (!tree_expr_maybe_infinite_p (arg))
9859 : 121 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9860 : : return NULL_TREE;
9861 : :
9862 : 704 : case BUILT_IN_ISINF_SIGN:
9863 : 704 : {
9864 : : /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9865 : : /* In a boolean context, GCC will fold the inner COND_EXPR to
9866 : : 1. So e.g. "if (isinf_sign(x))" would be folded to just
9867 : : "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9868 : 704 : tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9869 : 704 : tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9870 : 704 : tree tmp = NULL_TREE;
9871 : :
9872 : 704 : arg = builtin_save_expr (arg);
9873 : :
9874 : 704 : if (signbit_fn && isinf_fn)
9875 : : {
9876 : 704 : tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9877 : 704 : tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9878 : :
9879 : 704 : signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9880 : : signbit_call, integer_zero_node);
9881 : 704 : isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9882 : : isinf_call, integer_zero_node);
9883 : :
9884 : 704 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9885 : : integer_minus_one_node, integer_one_node);
9886 : 704 : tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9887 : : isinf_call, tmp,
9888 : : integer_zero_node);
9889 : : }
9890 : :
9891 : : return tmp;
9892 : : }
9893 : :
9894 : 567698 : case BUILT_IN_ISFINITE:
9895 : 567698 : if (tree_expr_finite_p (arg))
9896 : 224 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9897 : 567474 : if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9898 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9899 : : return NULL_TREE;
9900 : :
9901 : 264718 : case BUILT_IN_ISNAN:
9902 : 264718 : if (tree_expr_nan_p (arg))
9903 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg);
9904 : 264718 : if (!tree_expr_maybe_nan_p (arg))
9905 : 124 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9906 : :
9907 : 264594 : {
9908 : 1851390 : bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9909 : 264594 : if (is_ibm_extended)
9910 : : {
9911 : : /* NaN and Inf are encoded in the high-order double value
9912 : : only. The low-order value is not significant. */
9913 : 0 : arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9914 : : }
9915 : : }
9916 : 264594 : arg = builtin_save_expr (arg);
9917 : 264594 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9918 : :
9919 : 28854 : case BUILT_IN_ISSIGNALING:
9920 : : /* Folding to true for REAL_CST is done in fold_const_call_ss.
9921 : : Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9922 : : and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9923 : : here, so there is some possibility of __builtin_issignaling working
9924 : : without -fsignaling-nans. Especially when -fno-signaling-nans is
9925 : : the default. */
9926 : 28854 : if (!tree_expr_maybe_nan_p (arg))
9927 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9928 : : return NULL_TREE;
9929 : :
9930 : 0 : default:
9931 : 0 : gcc_unreachable ();
9932 : : }
9933 : : }
9934 : :
9935 : : /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9936 : : This builtin will generate code to return the appropriate floating
9937 : : point classification depending on the value of the floating point
9938 : : number passed in. The possible return values must be supplied as
9939 : : int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9940 : : FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9941 : : one floating point argument which is "type generic". */
9942 : :
9943 : : static tree
9944 : 113838 : fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9945 : : {
9946 : 113838 : tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9947 : : arg, type, res, tmp;
9948 : 113838 : machine_mode mode;
9949 : 113838 : REAL_VALUE_TYPE r;
9950 : 113838 : char buf[128];
9951 : :
9952 : : /* Verify the required arguments in the original call. */
9953 : 113838 : if (nargs != 6
9954 : 113838 : || !validate_arg (args[0], INTEGER_TYPE)
9955 : 113838 : || !validate_arg (args[1], INTEGER_TYPE)
9956 : 113838 : || !validate_arg (args[2], INTEGER_TYPE)
9957 : 113838 : || !validate_arg (args[3], INTEGER_TYPE)
9958 : 113838 : || !validate_arg (args[4], INTEGER_TYPE)
9959 : 227676 : || !validate_arg (args[5], REAL_TYPE))
9960 : : return NULL_TREE;
9961 : :
9962 : 113838 : fp_nan = args[0];
9963 : 113838 : fp_infinite = args[1];
9964 : 113838 : fp_normal = args[2];
9965 : 113838 : fp_subnormal = args[3];
9966 : 113838 : fp_zero = args[4];
9967 : 113838 : arg = args[5];
9968 : 113838 : type = TREE_TYPE (arg);
9969 : 113838 : mode = TYPE_MODE (type);
9970 : 113838 : arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9971 : :
9972 : : /* fpclassify(x) ->
9973 : : isnan(x) ? FP_NAN :
9974 : : (fabs(x) == Inf ? FP_INFINITE :
9975 : : (fabs(x) >= DBL_MIN ? FP_NORMAL :
9976 : : (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9977 : :
9978 : 113838 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9979 : : build_real (type, dconst0));
9980 : 113838 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9981 : : tmp, fp_zero, fp_subnormal);
9982 : :
9983 : 113838 : if (DECIMAL_FLOAT_MODE_P (mode))
9984 : 3 : sprintf (buf, "1E%d", REAL_MODE_FORMAT (mode)->emin - 1);
9985 : : else
9986 : 113835 : sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9987 : 113838 : real_from_string3 (&r, buf, mode);
9988 : 113838 : tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9989 : : arg, build_real (type, r));
9990 : 113838 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9991 : : fp_normal, res);
9992 : :
9993 : 113838 : if (tree_expr_maybe_infinite_p (arg))
9994 : : {
9995 : 113750 : tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9996 : : build_real (type, dconstinf));
9997 : 113750 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9998 : : fp_infinite, res);
9999 : : }
10000 : :
10001 : 113838 : if (tree_expr_maybe_nan_p (arg))
10002 : : {
10003 : 113748 : tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10004 : 113748 : res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10005 : : res, fp_nan);
10006 : : }
10007 : :
10008 : : return res;
10009 : : }
10010 : :
10011 : : /* Fold a call to an unordered comparison function such as
10012 : : __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10013 : : being called and ARG0 and ARG1 are the arguments for the call.
10014 : : UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10015 : : the opposite of the desired result. UNORDERED_CODE is used
10016 : : for modes that can hold NaNs and ORDERED_CODE is used for
10017 : : the rest. */
10018 : :
10019 : : static tree
10020 : 2928600 : fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10021 : : enum tree_code unordered_code,
10022 : : enum tree_code ordered_code)
10023 : : {
10024 : 2928600 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10025 : 2928600 : enum tree_code code;
10026 : 2928600 : tree type0, type1;
10027 : 2928600 : enum tree_code code0, code1;
10028 : 2928600 : tree cmp_type = NULL_TREE;
10029 : :
10030 : 2928600 : type0 = TREE_TYPE (arg0);
10031 : 2928600 : type1 = TREE_TYPE (arg1);
10032 : :
10033 : 2928600 : code0 = TREE_CODE (type0);
10034 : 2928600 : code1 = TREE_CODE (type1);
10035 : :
10036 : 2928600 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10037 : : /* Choose the wider of two real types. */
10038 : 2928420 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10039 : 2928420 : ? type0 : type1;
10040 : 180 : else if (code0 == REAL_TYPE
10041 : 91 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10042 : : cmp_type = type0;
10043 : 89 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10044 : 89 : && code1 == REAL_TYPE)
10045 : 135 : cmp_type = type1;
10046 : :
10047 : 2928600 : arg0 = fold_convert_loc (loc, cmp_type, arg0);
10048 : 2928600 : arg1 = fold_convert_loc (loc, cmp_type, arg1);
10049 : :
10050 : 2928600 : if (unordered_code == UNORDERED_EXPR)
10051 : : {
10052 : 262699 : if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
10053 : 16 : return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
10054 : 262683 : if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
10055 : 141 : return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10056 : 262542 : return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10057 : : }
10058 : :
10059 : 2668752 : code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
10060 : 2665901 : ? unordered_code : ordered_code;
10061 : 2665901 : return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10062 : 2665901 : fold_build2_loc (loc, code, type, arg0, arg1));
10063 : : }
10064 : :
10065 : : /* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments.
10066 : : After choosing the wider floating-point type for the comparison,
10067 : : the code is folded to:
10068 : : SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1> */
10069 : :
10070 : : static tree
10071 : 702 : fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
10072 : : {
10073 : 702 : tree type0, type1;
10074 : 702 : enum tree_code code0, code1;
10075 : 702 : tree cmp1, cmp2, cmp_type = NULL_TREE;
10076 : :
10077 : 702 : type0 = TREE_TYPE (arg0);
10078 : 702 : type1 = TREE_TYPE (arg1);
10079 : :
10080 : 702 : code0 = TREE_CODE (type0);
10081 : 702 : code1 = TREE_CODE (type1);
10082 : :
10083 : 702 : if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10084 : : /* Choose the wider of two real types. */
10085 : 690 : cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10086 : 690 : ? type0 : type1;
10087 : 12 : else if (code0 == REAL_TYPE
10088 : 6 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10089 : : cmp_type = type0;
10090 : 6 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10091 : 6 : && code1 == REAL_TYPE)
10092 : 6 : cmp_type = type1;
10093 : :
10094 : 702 : arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
10095 : 702 : arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
10096 : :
10097 : 702 : cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
10098 : 702 : cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
10099 : :
10100 : 702 : return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
10101 : : }
10102 : :
10103 : : /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
10104 : : arithmetics if it can never overflow, or into internal functions that
10105 : : return both result of arithmetics and overflowed boolean flag in
10106 : : a complex integer result, or some other check for overflow.
10107 : : Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
10108 : : checking part of that. */
10109 : :
10110 : : static tree
10111 : 182584 : fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
10112 : : tree arg0, tree arg1, tree arg2)
10113 : : {
10114 : 182584 : enum internal_fn ifn = IFN_LAST;
10115 : : /* The code of the expression corresponding to the built-in. */
10116 : 182584 : enum tree_code opcode = ERROR_MARK;
10117 : 182584 : bool ovf_only = false;
10118 : :
10119 : 182584 : switch (fcode)
10120 : : {
10121 : : case BUILT_IN_ADD_OVERFLOW_P:
10122 : : ovf_only = true;
10123 : : /* FALLTHRU */
10124 : : case BUILT_IN_ADD_OVERFLOW:
10125 : : case BUILT_IN_SADD_OVERFLOW:
10126 : : case BUILT_IN_SADDL_OVERFLOW:
10127 : : case BUILT_IN_SADDLL_OVERFLOW:
10128 : : case BUILT_IN_UADD_OVERFLOW:
10129 : : case BUILT_IN_UADDL_OVERFLOW:
10130 : : case BUILT_IN_UADDLL_OVERFLOW:
10131 : : opcode = PLUS_EXPR;
10132 : : ifn = IFN_ADD_OVERFLOW;
10133 : : break;
10134 : 15577 : case BUILT_IN_SUB_OVERFLOW_P:
10135 : 15577 : ovf_only = true;
10136 : : /* FALLTHRU */
10137 : 37199 : case BUILT_IN_SUB_OVERFLOW:
10138 : 37199 : case BUILT_IN_SSUB_OVERFLOW:
10139 : 37199 : case BUILT_IN_SSUBL_OVERFLOW:
10140 : 37199 : case BUILT_IN_SSUBLL_OVERFLOW:
10141 : 37199 : case BUILT_IN_USUB_OVERFLOW:
10142 : 37199 : case BUILT_IN_USUBL_OVERFLOW:
10143 : 37199 : case BUILT_IN_USUBLL_OVERFLOW:
10144 : 37199 : opcode = MINUS_EXPR;
10145 : 37199 : ifn = IFN_SUB_OVERFLOW;
10146 : 37199 : break;
10147 : 15857 : case BUILT_IN_MUL_OVERFLOW_P:
10148 : 15857 : ovf_only = true;
10149 : : /* FALLTHRU */
10150 : 113442 : case BUILT_IN_MUL_OVERFLOW:
10151 : 113442 : case BUILT_IN_SMUL_OVERFLOW:
10152 : 113442 : case BUILT_IN_SMULL_OVERFLOW:
10153 : 113442 : case BUILT_IN_SMULLL_OVERFLOW:
10154 : 113442 : case BUILT_IN_UMUL_OVERFLOW:
10155 : 113442 : case BUILT_IN_UMULL_OVERFLOW:
10156 : 113442 : case BUILT_IN_UMULLL_OVERFLOW:
10157 : 113442 : opcode = MULT_EXPR;
10158 : 113442 : ifn = IFN_MUL_OVERFLOW;
10159 : 113442 : break;
10160 : 0 : default:
10161 : 0 : gcc_unreachable ();
10162 : : }
10163 : :
10164 : : /* For the "generic" overloads, the first two arguments can have different
10165 : : types and the last argument determines the target type to use to check
10166 : : for overflow. The arguments of the other overloads all have the same
10167 : : type. */
10168 : 182584 : tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
10169 : :
10170 : : /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
10171 : : arguments are constant, attempt to fold the built-in call into a constant
10172 : : expression indicating whether or not it detected an overflow. */
10173 : 182584 : if (ovf_only
10174 : 43244 : && TREE_CODE (arg0) == INTEGER_CST
10175 : 13131 : && TREE_CODE (arg1) == INTEGER_CST)
10176 : : /* Perform the computation in the target type and check for overflow. */
10177 : 7347 : return omit_one_operand_loc (loc, boolean_type_node,
10178 : 7347 : arith_overflowed_p (opcode, type, arg0, arg1)
10179 : : ? boolean_true_node : boolean_false_node,
10180 : 7347 : arg2);
10181 : :
10182 : 175237 : tree intres, ovfres;
10183 : 175237 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10184 : : {
10185 : 12268 : intres = fold_binary_loc (loc, opcode, type,
10186 : : fold_convert_loc (loc, type, arg0),
10187 : : fold_convert_loc (loc, type, arg1));
10188 : 12268 : if (TREE_OVERFLOW (intres))
10189 : 1586 : intres = drop_tree_overflow (intres);
10190 : 24536 : ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
10191 : 12268 : ? boolean_true_node : boolean_false_node);
10192 : : }
10193 : : else
10194 : : {
10195 : 162969 : tree ctype = build_complex_type (type);
10196 : 162969 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10197 : : arg0, arg1);
10198 : 162969 : tree tgt;
10199 : 162969 : if (ovf_only)
10200 : : {
10201 : : tgt = call;
10202 : 162969 : intres = NULL_TREE;
10203 : : }
10204 : : else
10205 : : {
10206 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10207 : : as while the call itself is const, the REALPART_EXPR store is
10208 : : certainly not. And in any case, we want just one call,
10209 : : not multiple and trying to CSE them later. */
10210 : 127072 : TREE_SIDE_EFFECTS (call) = 1;
10211 : 127072 : tgt = save_expr (call);
10212 : : }
10213 : 162969 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10214 : 162969 : ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10215 : 162969 : ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
10216 : : }
10217 : :
10218 : 175237 : if (ovf_only)
10219 : 35897 : return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
10220 : :
10221 : 139340 : tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
10222 : 139340 : tree store
10223 : 139340 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
10224 : 139340 : return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
10225 : : }
10226 : :
10227 : : /* Fold __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g into corresponding
10228 : : internal function. */
10229 : :
10230 : : static tree
10231 : 230427 : fold_builtin_bit_query (location_t loc, enum built_in_function fcode,
10232 : : tree arg0, tree arg1)
10233 : : {
10234 : 230427 : enum internal_fn ifn;
10235 : 230427 : enum built_in_function fcodei, fcodel, fcodell;
10236 : 230427 : tree arg0_type = TREE_TYPE (arg0);
10237 : 230427 : tree cast_type = NULL_TREE;
10238 : 230427 : int addend = 0;
10239 : :
10240 : 230427 : switch (fcode)
10241 : : {
10242 : 163822 : case BUILT_IN_CLZG:
10243 : 163822 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10244 : : return NULL_TREE;
10245 : : ifn = IFN_CLZ;
10246 : : fcodei = BUILT_IN_CLZ;
10247 : : fcodel = BUILT_IN_CLZL;
10248 : : fcodell = BUILT_IN_CLZLL;
10249 : : break;
10250 : 48077 : case BUILT_IN_CTZG:
10251 : 48077 : if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10252 : : return NULL_TREE;
10253 : : ifn = IFN_CTZ;
10254 : : fcodei = BUILT_IN_CTZ;
10255 : : fcodel = BUILT_IN_CTZL;
10256 : : fcodell = BUILT_IN_CTZLL;
10257 : : break;
10258 : : case BUILT_IN_CLRSBG:
10259 : : ifn = IFN_CLRSB;
10260 : : fcodei = BUILT_IN_CLRSB;
10261 : : fcodel = BUILT_IN_CLRSBL;
10262 : : fcodell = BUILT_IN_CLRSBLL;
10263 : : break;
10264 : 83 : case BUILT_IN_FFSG:
10265 : 83 : ifn = IFN_FFS;
10266 : 83 : fcodei = BUILT_IN_FFS;
10267 : 83 : fcodel = BUILT_IN_FFSL;
10268 : 83 : fcodell = BUILT_IN_FFSLL;
10269 : 83 : break;
10270 : 76 : case BUILT_IN_PARITYG:
10271 : 76 : ifn = IFN_PARITY;
10272 : 76 : fcodei = BUILT_IN_PARITY;
10273 : 76 : fcodel = BUILT_IN_PARITYL;
10274 : 76 : fcodell = BUILT_IN_PARITYLL;
10275 : 76 : break;
10276 : 18288 : case BUILT_IN_POPCOUNTG:
10277 : 18288 : ifn = IFN_POPCOUNT;
10278 : 18288 : fcodei = BUILT_IN_POPCOUNT;
10279 : 18288 : fcodel = BUILT_IN_POPCOUNTL;
10280 : 18288 : fcodell = BUILT_IN_POPCOUNTLL;
10281 : 18288 : break;
10282 : 0 : default:
10283 : 0 : gcc_unreachable ();
10284 : : }
10285 : :
10286 : 230305 : if (TYPE_PRECISION (arg0_type)
10287 : 230305 : <= TYPE_PRECISION (long_long_unsigned_type_node))
10288 : : {
10289 : 199164 : if (TYPE_PRECISION (arg0_type) <= TYPE_PRECISION (unsigned_type_node))
10290 : :
10291 : 85489 : cast_type = (TYPE_UNSIGNED (arg0_type)
10292 : 85435 : ? unsigned_type_node : integer_type_node);
10293 : 113729 : else if (TYPE_PRECISION (arg0_type)
10294 : 113729 : <= TYPE_PRECISION (long_unsigned_type_node))
10295 : : {
10296 : 113753 : cast_type = (TYPE_UNSIGNED (arg0_type)
10297 : 113681 : ? long_unsigned_type_node : long_integer_type_node);
10298 : : fcodei = fcodel;
10299 : : }
10300 : : else
10301 : : {
10302 : 48 : cast_type = (TYPE_UNSIGNED (arg0_type)
10303 : 48 : ? long_long_unsigned_type_node
10304 : : : long_long_integer_type_node);
10305 : : fcodei = fcodell;
10306 : : }
10307 : : }
10308 : 62282 : else if (TYPE_PRECISION (arg0_type) <= MAX_FIXED_MODE_SIZE)
10309 : : {
10310 : 31041 : cast_type
10311 : 31041 : = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
10312 : 31041 : TYPE_UNSIGNED (arg0_type));
10313 : 31041 : gcc_assert (TYPE_PRECISION (cast_type)
10314 : : == 2 * TYPE_PRECISION (long_long_unsigned_type_node));
10315 : : fcodei = END_BUILTINS;
10316 : : }
10317 : : else
10318 : : fcodei = END_BUILTINS;
10319 : 230205 : if (cast_type)
10320 : : {
10321 : 230205 : switch (fcode)
10322 : : {
10323 : 163811 : case BUILT_IN_CLZG:
10324 : 163811 : case BUILT_IN_CLRSBG:
10325 : 163811 : addend = TYPE_PRECISION (arg0_type) - TYPE_PRECISION (cast_type);
10326 : 163811 : break;
10327 : : default:
10328 : : break;
10329 : : }
10330 : 230205 : arg0 = fold_convert (cast_type, arg0);
10331 : 230205 : arg0_type = cast_type;
10332 : : }
10333 : :
10334 : 230305 : if (arg1)
10335 : 149619 : arg1 = fold_convert (integer_type_node, arg1);
10336 : :
10337 : 230305 : tree arg2 = arg1;
10338 : 230305 : if (fcode == BUILT_IN_CLZG && addend)
10339 : : {
10340 : 7344 : if (arg1)
10341 : 7311 : arg0 = save_expr (arg0);
10342 : : arg2 = NULL_TREE;
10343 : : }
10344 : 230305 : tree call = NULL_TREE, tem;
10345 : 230305 : if (TYPE_PRECISION (arg0_type) == MAX_FIXED_MODE_SIZE
10346 : 31089 : && (TYPE_PRECISION (arg0_type)
10347 : 31089 : == 2 * TYPE_PRECISION (long_long_unsigned_type_node))
10348 : : /* If the target supports the optab, then don't do the expansion. */
10349 : 261346 : && !direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH))
10350 : : {
10351 : : /* __int128 expansions using up to 2 long long builtins. */
10352 : 31041 : arg0 = save_expr (arg0);
10353 : 31041 : tree type = (TYPE_UNSIGNED (arg0_type)
10354 : 31041 : ? long_long_unsigned_type_node
10355 : 31041 : : long_long_integer_type_node);
10356 : 62082 : tree hi = fold_build2 (RSHIFT_EXPR, arg0_type, arg0,
10357 : : build_int_cst (integer_type_node,
10358 : : MAX_FIXED_MODE_SIZE / 2));
10359 : 31041 : hi = fold_convert (type, hi);
10360 : 31041 : tree lo = fold_convert (type, arg0);
10361 : 31041 : switch (fcode)
10362 : : {
10363 : 30946 : case BUILT_IN_CLZG:
10364 : 30946 : call = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10365 : 61892 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10366 : : build_int_cst (integer_type_node,
10367 : : MAX_FIXED_MODE_SIZE / 2));
10368 : 30946 : if (arg2)
10369 : 30933 : call = fold_build3 (COND_EXPR, integer_type_node,
10370 : : fold_build2 (NE_EXPR, boolean_type_node,
10371 : : lo, build_zero_cst (type)),
10372 : : call, arg2);
10373 : 30946 : call = fold_build3 (COND_EXPR, integer_type_node,
10374 : : fold_build2 (NE_EXPR, boolean_type_node,
10375 : : hi, build_zero_cst (type)),
10376 : : fold_builtin_bit_query (loc, fcode, hi,
10377 : : NULL_TREE),
10378 : : call);
10379 : 30946 : break;
10380 : 33 : case BUILT_IN_CTZG:
10381 : 33 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10382 : 66 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10383 : : build_int_cst (integer_type_node,
10384 : : MAX_FIXED_MODE_SIZE / 2));
10385 : 33 : if (arg2)
10386 : 24 : call = fold_build3 (COND_EXPR, integer_type_node,
10387 : : fold_build2 (NE_EXPR, boolean_type_node,
10388 : : hi, build_zero_cst (type)),
10389 : : call, arg2);
10390 : 33 : call = fold_build3 (COND_EXPR, integer_type_node,
10391 : : fold_build2 (NE_EXPR, boolean_type_node,
10392 : : lo, build_zero_cst (type)),
10393 : : fold_builtin_bit_query (loc, fcode, lo,
10394 : : NULL_TREE),
10395 : : call);
10396 : 33 : break;
10397 : 9 : case BUILT_IN_CLRSBG:
10398 : 9 : tem = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10399 : 18 : tem = fold_build2 (PLUS_EXPR, integer_type_node, tem,
10400 : : build_int_cst (integer_type_node,
10401 : : MAX_FIXED_MODE_SIZE / 2));
10402 : 18 : tem = fold_build3 (COND_EXPR, integer_type_node,
10403 : : fold_build2 (LT_EXPR, boolean_type_node,
10404 : : fold_build2 (BIT_XOR_EXPR, type,
10405 : : lo, hi),
10406 : : build_zero_cst (type)),
10407 : : build_int_cst (integer_type_node,
10408 : : MAX_FIXED_MODE_SIZE / 2 - 1),
10409 : : tem);
10410 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10411 : 9 : call = save_expr (call);
10412 : 18 : call = fold_build3 (COND_EXPR, integer_type_node,
10413 : : fold_build2 (NE_EXPR, boolean_type_node,
10414 : : call,
10415 : : build_int_cst (integer_type_node,
10416 : : MAX_FIXED_MODE_SIZE
10417 : : / 2 - 1)),
10418 : : call, tem);
10419 : 9 : break;
10420 : 9 : case BUILT_IN_FFSG:
10421 : 9 : call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10422 : 18 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10423 : : build_int_cst (integer_type_node,
10424 : : MAX_FIXED_MODE_SIZE / 2));
10425 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10426 : : fold_build2 (NE_EXPR, boolean_type_node,
10427 : : hi, build_zero_cst (type)),
10428 : : call, integer_zero_node);
10429 : 9 : call = fold_build3 (COND_EXPR, integer_type_node,
10430 : : fold_build2 (NE_EXPR, boolean_type_node,
10431 : : lo, build_zero_cst (type)),
10432 : : fold_builtin_bit_query (loc, fcode, lo,
10433 : : NULL_TREE),
10434 : : call);
10435 : 9 : break;
10436 : 9 : case BUILT_IN_PARITYG:
10437 : 9 : call = fold_builtin_bit_query (loc, fcode,
10438 : : fold_build2 (BIT_XOR_EXPR, type,
10439 : : lo, hi), NULL_TREE);
10440 : 9 : break;
10441 : 35 : case BUILT_IN_POPCOUNTG:
10442 : 35 : call = fold_build2 (PLUS_EXPR, integer_type_node,
10443 : : fold_builtin_bit_query (loc, fcode, hi,
10444 : : NULL_TREE),
10445 : : fold_builtin_bit_query (loc, fcode, lo,
10446 : : NULL_TREE));
10447 : 35 : break;
10448 : 0 : default:
10449 : 0 : gcc_unreachable ();
10450 : : }
10451 : : }
10452 : : else
10453 : : {
10454 : : /* Only keep second argument to IFN_CLZ/IFN_CTZ if it is the
10455 : : value defined at zero during GIMPLE, or for large/huge _BitInt
10456 : : (which are then lowered during bitint lowering). */
10457 : 199264 : if (arg2 && TREE_CODE (TREE_TYPE (arg0)) != BITINT_TYPE)
10458 : : {
10459 : 111333 : int val;
10460 : 111333 : if (fcode == BUILT_IN_CLZG)
10461 : : {
10462 : 63514 : if (CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10463 : : val) != 2
10464 : 63526 : || wi::to_widest (arg2) != val)
10465 : 63502 : arg2 = NULL_TREE;
10466 : : }
10467 : 47819 : else if (CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10468 : : val) != 2
10469 : 47819 : || wi::to_widest (arg2) != val)
10470 : 47819 : arg2 = NULL_TREE;
10471 : 111333 : if (!direct_internal_fn_supported_p (ifn, arg0_type,
10472 : : OPTIMIZE_FOR_BOTH))
10473 : : arg2 = NULL_TREE;
10474 : 111288 : if (arg2 == NULL_TREE)
10475 : 111321 : arg0 = save_expr (arg0);
10476 : : }
10477 : 199264 : if (fcodei == END_BUILTINS || arg2)
10478 : 194 : call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
10479 : : arg2 ? 2 : 1, arg0, arg2);
10480 : : else
10481 : 199152 : call = build_call_expr_loc (loc, builtin_decl_explicit (fcodei), 1,
10482 : : arg0);
10483 : : }
10484 : 230305 : if (addend)
10485 : 7362 : call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10486 : : build_int_cst (integer_type_node, addend));
10487 : 230305 : if (arg1 && arg2 == NULL_TREE)
10488 : 118632 : call = fold_build3 (COND_EXPR, integer_type_node,
10489 : : fold_build2 (NE_EXPR, boolean_type_node,
10490 : : arg0, build_zero_cst (arg0_type)),
10491 : : call, arg1);
10492 : :
10493 : : return call;
10494 : : }
10495 : :
10496 : : /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
10497 : : that return both result of arithmetics and overflowed boolean
10498 : : flag in a complex integer result. */
10499 : :
10500 : : static tree
10501 : 54 : fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
10502 : : tree *args)
10503 : : {
10504 : 54 : enum internal_fn ifn;
10505 : :
10506 : 54 : switch (fcode)
10507 : : {
10508 : : case BUILT_IN_ADDC:
10509 : : case BUILT_IN_ADDCL:
10510 : : case BUILT_IN_ADDCLL:
10511 : : ifn = IFN_ADD_OVERFLOW;
10512 : : break;
10513 : 28 : case BUILT_IN_SUBC:
10514 : 28 : case BUILT_IN_SUBCL:
10515 : 28 : case BUILT_IN_SUBCLL:
10516 : 28 : ifn = IFN_SUB_OVERFLOW;
10517 : 28 : break;
10518 : 0 : default:
10519 : 0 : gcc_unreachable ();
10520 : : }
10521 : :
10522 : 54 : tree type = TREE_TYPE (args[0]);
10523 : 54 : tree ctype = build_complex_type (type);
10524 : 54 : tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10525 : : args[0], args[1]);
10526 : : /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10527 : : as while the call itself is const, the REALPART_EXPR store is
10528 : : certainly not. And in any case, we want just one call,
10529 : : not multiple and trying to CSE them later. */
10530 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10531 : 54 : tree tgt = save_expr (call);
10532 : 54 : tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10533 : 54 : tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10534 : 54 : call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10535 : : intres, args[2]);
10536 : 54 : TREE_SIDE_EFFECTS (call) = 1;
10537 : 54 : tgt = save_expr (call);
10538 : 54 : intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10539 : 54 : tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10540 : 54 : ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
10541 : 54 : tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
10542 : 54 : tree store
10543 : 54 : = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10544 : 54 : return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10545 : : }
10546 : :
10547 : : /* Fold a call to __builtin_FILE to a constant string. */
10548 : :
10549 : : static inline tree
10550 : 5843 : fold_builtin_FILE (location_t loc)
10551 : : {
10552 : 5843 : if (const char *fname = LOCATION_FILE (loc))
10553 : : {
10554 : : /* The documentation says this builtin is equivalent to the preprocessor
10555 : : __FILE__ macro so it appears appropriate to use the same file prefix
10556 : : mappings. */
10557 : 5843 : fname = remap_macro_filename (fname);
10558 : 5843 : return build_string_literal (fname);
10559 : : }
10560 : :
10561 : 0 : return build_string_literal ("");
10562 : : }
10563 : :
10564 : : /* Fold a call to __builtin_FUNCTION to a constant string. */
10565 : :
10566 : : static inline tree
10567 : 68 : fold_builtin_FUNCTION ()
10568 : : {
10569 : 68 : const char *name = "";
10570 : :
10571 : 68 : if (current_function_decl)
10572 : 43 : name = lang_hooks.decl_printable_name (current_function_decl, 0);
10573 : :
10574 : 68 : return build_string_literal (name);
10575 : : }
10576 : :
10577 : : /* Fold a call to __builtin_LINE to an integer constant. */
10578 : :
10579 : : static inline tree
10580 : 11701 : fold_builtin_LINE (location_t loc, tree type)
10581 : : {
10582 : 11701 : return build_int_cst (type, LOCATION_LINE (loc));
10583 : : }
10584 : :
10585 : : /* Fold a call to built-in function FNDECL with 0 arguments.
10586 : : This function returns NULL_TREE if no simplification was possible. */
10587 : :
10588 : : static tree
10589 : 22458431 : fold_builtin_0 (location_t loc, tree fndecl)
10590 : : {
10591 : 22458431 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10592 : 22458431 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10593 : 22458431 : switch (fcode)
10594 : : {
10595 : 5843 : case BUILT_IN_FILE:
10596 : 5843 : return fold_builtin_FILE (loc);
10597 : :
10598 : 68 : case BUILT_IN_FUNCTION:
10599 : 68 : return fold_builtin_FUNCTION ();
10600 : :
10601 : 11701 : case BUILT_IN_LINE:
10602 : 11701 : return fold_builtin_LINE (loc, type);
10603 : :
10604 : 34926 : CASE_FLT_FN (BUILT_IN_INF):
10605 : 34926 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
10606 : 34926 : case BUILT_IN_INFD32:
10607 : 34926 : case BUILT_IN_INFD64:
10608 : 34926 : case BUILT_IN_INFD128:
10609 : 34926 : case BUILT_IN_INFD64X:
10610 : 34926 : return fold_builtin_inf (loc, type, true);
10611 : :
10612 : 193354 : CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10613 : 193354 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
10614 : 193354 : return fold_builtin_inf (loc, type, false);
10615 : :
10616 : 0 : case BUILT_IN_CLASSIFY_TYPE:
10617 : 0 : return fold_builtin_classify_type (NULL_TREE);
10618 : :
10619 : 17109784 : case BUILT_IN_UNREACHABLE:
10620 : : /* Rewrite any explicit calls to __builtin_unreachable. */
10621 : 17109784 : if (sanitize_flags_p (SANITIZE_UNREACHABLE))
10622 : 115 : return build_builtin_unreachable (loc);
10623 : : break;
10624 : :
10625 : : default:
10626 : : break;
10627 : : }
10628 : : return NULL_TREE;
10629 : : }
10630 : :
10631 : : /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10632 : : This function returns NULL_TREE if no simplification was possible. */
10633 : :
10634 : : static tree
10635 : 15916719 : fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
10636 : : {
10637 : 15916719 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10638 : 15916719 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10639 : :
10640 : 15916719 : if (error_operand_p (arg0))
10641 : : return NULL_TREE;
10642 : :
10643 : 15916719 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
10644 : : return ret;
10645 : :
10646 : 15428483 : switch (fcode)
10647 : : {
10648 : 780733 : case BUILT_IN_CONSTANT_P:
10649 : 780733 : {
10650 : 780733 : tree val = fold_builtin_constant_p (arg0);
10651 : :
10652 : : /* Gimplification will pull the CALL_EXPR for the builtin out of
10653 : : an if condition. When not optimizing, we'll not CSE it back.
10654 : : To avoid link error types of regressions, return false now. */
10655 : 780733 : if (!val && !optimize)
10656 : 1178 : val = integer_zero_node;
10657 : :
10658 : : return val;
10659 : : }
10660 : :
10661 : 2146 : case BUILT_IN_CLASSIFY_TYPE:
10662 : 2146 : return fold_builtin_classify_type (arg0);
10663 : :
10664 : 416512 : case BUILT_IN_STRLEN:
10665 : 416512 : return fold_builtin_strlen (loc, expr, type, arg0);
10666 : :
10667 : 359371 : CASE_FLT_FN (BUILT_IN_FABS):
10668 : 359371 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10669 : 359371 : case BUILT_IN_FABSD32:
10670 : 359371 : case BUILT_IN_FABSD64:
10671 : 359371 : case BUILT_IN_FABSD128:
10672 : 359371 : case BUILT_IN_FABSD64X:
10673 : 359371 : return fold_builtin_fabs (loc, arg0, type);
10674 : :
10675 : 93475 : case BUILT_IN_ABS:
10676 : 93475 : case BUILT_IN_LABS:
10677 : 93475 : case BUILT_IN_LLABS:
10678 : 93475 : case BUILT_IN_IMAXABS:
10679 : 93475 : case BUILT_IN_UABS:
10680 : 93475 : case BUILT_IN_ULABS:
10681 : 93475 : case BUILT_IN_ULLABS:
10682 : 93475 : case BUILT_IN_UIMAXABS:
10683 : 93475 : return fold_builtin_abs (loc, arg0, type);
10684 : :
10685 : 24501 : CASE_FLT_FN (BUILT_IN_CONJ):
10686 : 24501 : if (validate_arg (arg0, COMPLEX_TYPE)
10687 : 24501 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10688 : 24501 : return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10689 : : break;
10690 : :
10691 : 764 : CASE_FLT_FN (BUILT_IN_CREAL):
10692 : 764 : if (validate_arg (arg0, COMPLEX_TYPE)
10693 : 764 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10694 : 764 : return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
10695 : : break;
10696 : :
10697 : 1884 : CASE_FLT_FN (BUILT_IN_CIMAG):
10698 : 1884 : if (validate_arg (arg0, COMPLEX_TYPE)
10699 : 1884 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10700 : 1884 : return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10701 : : break;
10702 : :
10703 : 114640 : CASE_FLT_FN (BUILT_IN_CARG):
10704 : 114640 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
10705 : 114640 : return fold_builtin_carg (loc, arg0, type);
10706 : :
10707 : 211 : case BUILT_IN_ISASCII:
10708 : 211 : return fold_builtin_isascii (loc, arg0);
10709 : :
10710 : 168 : case BUILT_IN_TOASCII:
10711 : 168 : return fold_builtin_toascii (loc, arg0);
10712 : :
10713 : 325 : case BUILT_IN_ISDIGIT:
10714 : 325 : return fold_builtin_isdigit (loc, arg0);
10715 : :
10716 : 567698 : CASE_FLT_FN (BUILT_IN_FINITE):
10717 : 567698 : case BUILT_IN_FINITED32:
10718 : 567698 : case BUILT_IN_FINITED64:
10719 : 567698 : case BUILT_IN_FINITED128:
10720 : 567698 : case BUILT_IN_ISFINITE:
10721 : 567698 : {
10722 : 567698 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10723 : 567698 : if (ret)
10724 : : return ret;
10725 : 567474 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10726 : : }
10727 : :
10728 : 265097 : CASE_FLT_FN (BUILT_IN_ISINF):
10729 : 265097 : case BUILT_IN_ISINFD32:
10730 : 265097 : case BUILT_IN_ISINFD64:
10731 : 265097 : case BUILT_IN_ISINFD128:
10732 : 265097 : {
10733 : 265097 : tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10734 : 265097 : if (ret)
10735 : : return ret;
10736 : 264976 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10737 : : }
10738 : :
10739 : 261652 : case BUILT_IN_ISNORMAL:
10740 : 261652 : return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10741 : :
10742 : 704 : case BUILT_IN_ISINF_SIGN:
10743 : 704 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10744 : :
10745 : 264718 : CASE_FLT_FN (BUILT_IN_ISNAN):
10746 : 264718 : case BUILT_IN_ISNAND32:
10747 : 264718 : case BUILT_IN_ISNAND64:
10748 : 264718 : case BUILT_IN_ISNAND128:
10749 : 264718 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10750 : :
10751 : 28854 : case BUILT_IN_ISSIGNALING:
10752 : 28854 : return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
10753 : :
10754 : 596589 : case BUILT_IN_FREE:
10755 : 596589 : if (integer_zerop (arg0))
10756 : 1049 : return build_empty_stmt (loc);
10757 : : break;
10758 : :
10759 : 18613 : case BUILT_IN_CLZG:
10760 : 18613 : case BUILT_IN_CTZG:
10761 : 18613 : case BUILT_IN_CLRSBG:
10762 : 18613 : case BUILT_IN_FFSG:
10763 : 18613 : case BUILT_IN_PARITYG:
10764 : 18613 : case BUILT_IN_POPCOUNTG:
10765 : 18613 : return fold_builtin_bit_query (loc, fcode, arg0, NULL_TREE);
10766 : :
10767 : : default:
10768 : : break;
10769 : : }
10770 : :
10771 : : return NULL_TREE;
10772 : :
10773 : : }
10774 : :
10775 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL
10776 : : with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
10777 : : if no simplification was possible. */
10778 : :
10779 : : static tree
10780 : 16791085 : fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
10781 : : {
10782 : 16791085 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10783 : 16791085 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10784 : :
10785 : 16791085 : if (error_operand_p (arg0)
10786 : 16791085 : || error_operand_p (arg1))
10787 : : return NULL_TREE;
10788 : :
10789 : 16791081 : if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
10790 : : return ret;
10791 : :
10792 : 16586132 : switch (fcode)
10793 : : {
10794 : 6720 : CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10795 : 6720 : CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10796 : 6720 : if (validate_arg (arg0, REAL_TYPE)
10797 : 6720 : && validate_arg (arg1, POINTER_TYPE))
10798 : 6720 : return do_mpfr_lgamma_r (arg0, arg1, type);
10799 : : break;
10800 : :
10801 : 114564 : CASE_FLT_FN (BUILT_IN_FREXP):
10802 : 114564 : return fold_builtin_frexp (loc, arg0, arg1, type);
10803 : :
10804 : 75037 : CASE_FLT_FN (BUILT_IN_MODF):
10805 : 75037 : return fold_builtin_modf (loc, arg0, arg1, type);
10806 : :
10807 : 2655 : case BUILT_IN_STRSPN:
10808 : 2655 : return fold_builtin_strspn (loc, expr, arg0, arg1, type);
10809 : :
10810 : 2553 : case BUILT_IN_STRCSPN:
10811 : 2553 : return fold_builtin_strcspn (loc, expr, arg0, arg1, type);
10812 : :
10813 : 82381 : case BUILT_IN_STRPBRK:
10814 : 82381 : return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
10815 : :
10816 : 4905437 : case BUILT_IN_EXPECT:
10817 : 4905437 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
10818 : :
10819 : 526792 : case BUILT_IN_ISGREATER:
10820 : 526792 : return fold_builtin_unordered_cmp (loc, fndecl,
10821 : 526792 : arg0, arg1, UNLE_EXPR, LE_EXPR);
10822 : 523409 : case BUILT_IN_ISGREATEREQUAL:
10823 : 523409 : return fold_builtin_unordered_cmp (loc, fndecl,
10824 : 523409 : arg0, arg1, UNLT_EXPR, LT_EXPR);
10825 : 263326 : case BUILT_IN_ISLESS:
10826 : 263326 : return fold_builtin_unordered_cmp (loc, fndecl,
10827 : 263326 : arg0, arg1, UNGE_EXPR, GE_EXPR);
10828 : 1090912 : case BUILT_IN_ISLESSEQUAL:
10829 : 1090912 : return fold_builtin_unordered_cmp (loc, fndecl,
10830 : 1090912 : arg0, arg1, UNGT_EXPR, GT_EXPR);
10831 : 261462 : case BUILT_IN_ISLESSGREATER:
10832 : 261462 : return fold_builtin_unordered_cmp (loc, fndecl,
10833 : 261462 : arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10834 : 262699 : case BUILT_IN_ISUNORDERED:
10835 : 262699 : return fold_builtin_unordered_cmp (loc, fndecl,
10836 : : arg0, arg1, UNORDERED_EXPR,
10837 : 262699 : NOP_EXPR);
10838 : :
10839 : 702 : case BUILT_IN_ISEQSIG:
10840 : 702 : return fold_builtin_iseqsig (loc, arg0, arg1);
10841 : :
10842 : : /* We do the folding for va_start in the expander. */
10843 : : case BUILT_IN_VA_START:
10844 : : break;
10845 : :
10846 : 199834 : case BUILT_IN_OBJECT_SIZE:
10847 : 199834 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
10848 : 199834 : return fold_builtin_object_size (arg0, arg1, fcode);
10849 : :
10850 : 49912 : case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10851 : 49912 : return fold_builtin_atomic_always_lock_free (arg0, arg1);
10852 : :
10853 : 39695 : case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10854 : 39695 : return fold_builtin_atomic_is_lock_free (arg0, arg1);
10855 : :
10856 : 149741 : case BUILT_IN_CLZG:
10857 : 149741 : case BUILT_IN_CTZG:
10858 : 149741 : return fold_builtin_bit_query (loc, fcode, arg0, arg1);
10859 : :
10860 : : default:
10861 : : break;
10862 : : }
10863 : : return NULL_TREE;
10864 : : }
10865 : :
10866 : : /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10867 : : and ARG2.
10868 : : This function returns NULL_TREE if no simplification was possible. */
10869 : :
10870 : : static tree
10871 : 6153706 : fold_builtin_3 (location_t loc, tree fndecl,
10872 : : tree arg0, tree arg1, tree arg2)
10873 : : {
10874 : 6153706 : tree type = TREE_TYPE (TREE_TYPE (fndecl));
10875 : 6153706 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10876 : :
10877 : 6153706 : if (error_operand_p (arg0)
10878 : 6153706 : || error_operand_p (arg1)
10879 : 12307412 : || error_operand_p (arg2))
10880 : : return NULL_TREE;
10881 : :
10882 : 6153704 : if (tree ret = fold_const_call (as_combined_fn (fcode), type,
10883 : : arg0, arg1, arg2))
10884 : : return ret;
10885 : :
10886 : 6137025 : switch (fcode)
10887 : : {
10888 : :
10889 : 144 : CASE_FLT_FN (BUILT_IN_SINCOS):
10890 : 144 : return fold_builtin_sincos (loc, arg0, arg1, arg2);
10891 : :
10892 : 85934 : CASE_FLT_FN (BUILT_IN_REMQUO):
10893 : 85934 : if (validate_arg (arg0, REAL_TYPE)
10894 : 85934 : && validate_arg (arg1, REAL_TYPE)
10895 : 171868 : && validate_arg (arg2, POINTER_TYPE))
10896 : 85934 : return do_mpfr_remquo (arg0, arg1, arg2);
10897 : : break;
10898 : :
10899 : 2234461 : case BUILT_IN_MEMCMP:
10900 : 2234461 : return fold_builtin_memcmp (loc, arg0, arg1, arg2);
10901 : :
10902 : 488800 : case BUILT_IN_EXPECT:
10903 : 488800 : return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
10904 : :
10905 : 337 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
10906 : 337 : return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
10907 : :
10908 : 182584 : case BUILT_IN_ADD_OVERFLOW:
10909 : 182584 : case BUILT_IN_SUB_OVERFLOW:
10910 : 182584 : case BUILT_IN_MUL_OVERFLOW:
10911 : 182584 : case BUILT_IN_ADD_OVERFLOW_P:
10912 : 182584 : case BUILT_IN_SUB_OVERFLOW_P:
10913 : 182584 : case BUILT_IN_MUL_OVERFLOW_P:
10914 : 182584 : case BUILT_IN_SADD_OVERFLOW:
10915 : 182584 : case BUILT_IN_SADDL_OVERFLOW:
10916 : 182584 : case BUILT_IN_SADDLL_OVERFLOW:
10917 : 182584 : case BUILT_IN_SSUB_OVERFLOW:
10918 : 182584 : case BUILT_IN_SSUBL_OVERFLOW:
10919 : 182584 : case BUILT_IN_SSUBLL_OVERFLOW:
10920 : 182584 : case BUILT_IN_SMUL_OVERFLOW:
10921 : 182584 : case BUILT_IN_SMULL_OVERFLOW:
10922 : 182584 : case BUILT_IN_SMULLL_OVERFLOW:
10923 : 182584 : case BUILT_IN_UADD_OVERFLOW:
10924 : 182584 : case BUILT_IN_UADDL_OVERFLOW:
10925 : 182584 : case BUILT_IN_UADDLL_OVERFLOW:
10926 : 182584 : case BUILT_IN_USUB_OVERFLOW:
10927 : 182584 : case BUILT_IN_USUBL_OVERFLOW:
10928 : 182584 : case BUILT_IN_USUBLL_OVERFLOW:
10929 : 182584 : case BUILT_IN_UMUL_OVERFLOW:
10930 : 182584 : case BUILT_IN_UMULL_OVERFLOW:
10931 : 182584 : case BUILT_IN_UMULLL_OVERFLOW:
10932 : 182584 : return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10933 : :
10934 : : default:
10935 : : break;
10936 : : }
10937 : : return NULL_TREE;
10938 : : }
10939 : :
10940 : : /* Folds a call EXPR (which may be null) to built-in function FNDECL.
10941 : : ARGS is an array of NARGS arguments. IGNORE is true if the result
10942 : : of the function call is ignored. This function returns NULL_TREE
10943 : : if no simplification was possible. */
10944 : :
10945 : : static tree
10946 : 63957683 : fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
10947 : : int nargs, bool)
10948 : : {
10949 : 63957683 : tree ret = NULL_TREE;
10950 : :
10951 : 63957683 : switch (nargs)
10952 : : {
10953 : 22458431 : case 0:
10954 : 22458431 : ret = fold_builtin_0 (loc, fndecl);
10955 : 22458431 : break;
10956 : 15916719 : case 1:
10957 : 15916719 : ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
10958 : 15916719 : break;
10959 : 16791085 : case 2:
10960 : 16791085 : ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
10961 : 16791085 : break;
10962 : 6153706 : case 3:
10963 : 6153706 : ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10964 : 6153706 : break;
10965 : 2637742 : default:
10966 : 2637742 : ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10967 : 2637742 : break;
10968 : : }
10969 : 63957683 : if (ret)
10970 : : {
10971 : 6556818 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10972 : 6556818 : SET_EXPR_LOCATION (ret, loc);
10973 : 6556818 : return ret;
10974 : : }
10975 : : return NULL_TREE;
10976 : : }
10977 : :
10978 : : /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10979 : : list ARGS along with N new arguments in NEWARGS. SKIP is the number
10980 : : of arguments in ARGS to be omitted. OLDNARGS is the number of
10981 : : elements in ARGS. */
10982 : :
10983 : : static tree
10984 : 4 : rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10985 : : int skip, tree fndecl, int n, va_list newargs)
10986 : : {
10987 : 4 : int nargs = oldnargs - skip + n;
10988 : 4 : tree *buffer;
10989 : :
10990 : 4 : if (n > 0)
10991 : : {
10992 : 0 : int i, j;
10993 : :
10994 : 0 : buffer = XALLOCAVEC (tree, nargs);
10995 : 0 : for (i = 0; i < n; i++)
10996 : 0 : buffer[i] = va_arg (newargs, tree);
10997 : 0 : for (j = skip; j < oldnargs; j++, i++)
10998 : 0 : buffer[i] = args[j];
10999 : : }
11000 : : else
11001 : 4 : buffer = args + skip;
11002 : :
11003 : 4 : return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11004 : : }
11005 : :
11006 : : /* Return true if FNDECL shouldn't be folded right now.
11007 : : If a built-in function has an inline attribute always_inline
11008 : : wrapper, defer folding it after always_inline functions have
11009 : : been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11010 : : might not be performed. */
11011 : :
11012 : : bool
11013 : 143533520 : avoid_folding_inline_builtin (tree fndecl)
11014 : : {
11015 : 143533520 : return (DECL_DECLARED_INLINE_P (fndecl)
11016 : 9443 : && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11017 : 9401 : && cfun
11018 : 9401 : && !cfun->always_inline_functions_inlined
11019 : 143542921 : && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11020 : : }
11021 : :
11022 : : /* A wrapper function for builtin folding that prevents warnings for
11023 : : "statement without effect" and the like, caused by removing the
11024 : : call node earlier than the warning is generated. */
11025 : :
11026 : : tree
11027 : 181340512 : fold_call_expr (location_t loc, tree exp, bool ignore)
11028 : : {
11029 : 181340512 : tree ret = NULL_TREE;
11030 : 181340512 : tree fndecl = get_callee_fndecl (exp);
11031 : 179577901 : if (fndecl && fndecl_built_in_p (fndecl)
11032 : : /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11033 : : yet. Defer folding until we see all the arguments
11034 : : (after inlining). */
11035 : 237749342 : && !CALL_EXPR_VA_ARG_PACK (exp))
11036 : : {
11037 : 56408805 : int nargs = call_expr_nargs (exp);
11038 : :
11039 : : /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11040 : : instead last argument is __builtin_va_arg_pack (). Defer folding
11041 : : even in that case, until arguments are finalized. */
11042 : 56408805 : if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11043 : : {
11044 : 252490 : tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11045 : 252490 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11046 : : return NULL_TREE;
11047 : : }
11048 : :
11049 : 56408741 : if (avoid_folding_inline_builtin (fndecl))
11050 : : return NULL_TREE;
11051 : :
11052 : 56405206 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11053 : 67165944 : return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11054 : 67165944 : CALL_EXPR_ARGP (exp), ignore);
11055 : : else
11056 : : {
11057 : 22822234 : tree *args = CALL_EXPR_ARGP (exp);
11058 : 22822234 : ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
11059 : 22822234 : if (ret)
11060 : : return ret;
11061 : : }
11062 : : }
11063 : : return NULL_TREE;
11064 : : }
11065 : :
11066 : : /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
11067 : : N arguments are passed in the array ARGARRAY. Return a folded
11068 : : expression or NULL_TREE if no simplification was possible. */
11069 : :
11070 : : tree
11071 : 63873537 : fold_builtin_call_array (location_t loc, tree,
11072 : : tree fn,
11073 : : int n,
11074 : : tree *argarray)
11075 : : {
11076 : 63873537 : if (TREE_CODE (fn) != ADDR_EXPR)
11077 : : return NULL_TREE;
11078 : :
11079 : 63873537 : tree fndecl = TREE_OPERAND (fn, 0);
11080 : 63873537 : if (TREE_CODE (fndecl) == FUNCTION_DECL
11081 : 63873537 : && fndecl_built_in_p (fndecl))
11082 : : {
11083 : : /* If last argument is __builtin_va_arg_pack (), arguments to this
11084 : : function are not finalized yet. Defer folding until they are. */
11085 : 63296005 : if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11086 : : {
11087 : 102742 : tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11088 : 102742 : if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11089 : : return NULL_TREE;
11090 : : }
11091 : 63295978 : if (avoid_folding_inline_builtin (fndecl))
11092 : : return NULL_TREE;
11093 : 63295978 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11094 : 32438152 : return targetm.fold_builtin (fndecl, n, argarray, false);
11095 : : else
11096 : 30857826 : return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
11097 : : }
11098 : :
11099 : : return NULL_TREE;
11100 : : }
11101 : :
11102 : : /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11103 : : along with N new arguments specified as the "..." parameters. SKIP
11104 : : is the number of arguments in EXP to be omitted. This function is used
11105 : : to do varargs-to-varargs transformations. */
11106 : :
11107 : : static tree
11108 : 4 : rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11109 : : {
11110 : 4 : va_list ap;
11111 : 4 : tree t;
11112 : :
11113 : 4 : va_start (ap, n);
11114 : 8 : t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11115 : 4 : CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11116 : 4 : va_end (ap);
11117 : :
11118 : 4 : return t;
11119 : : }
11120 : :
11121 : : /* Validate a single argument ARG against a tree code CODE representing
11122 : : a type. Return true when argument is valid. */
11123 : :
11124 : : static bool
11125 : 12952723 : validate_arg (const_tree arg, enum tree_code code)
11126 : : {
11127 : 12952723 : if (!arg)
11128 : : return false;
11129 : 12952700 : else if (code == POINTER_TYPE)
11130 : 6299140 : return POINTER_TYPE_P (TREE_TYPE (arg));
11131 : 6653560 : else if (code == INTEGER_TYPE)
11132 : 3421347 : return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11133 : 3232213 : return code == TREE_CODE (TREE_TYPE (arg));
11134 : : }
11135 : :
11136 : : /* This function validates the types of a function call argument list
11137 : : against a specified list of tree_codes. If the last specifier is a 0,
11138 : : that represents an ellipses, otherwise the last specifier must be a
11139 : : VOID_TYPE.
11140 : :
11141 : : This is the GIMPLE version of validate_arglist. Eventually we want to
11142 : : completely convert builtins.cc to work from GIMPLEs and the tree based
11143 : : validate_arglist will then be removed. */
11144 : :
11145 : : bool
11146 : 93 : validate_gimple_arglist (const gcall *call, ...)
11147 : : {
11148 : 93 : enum tree_code code;
11149 : 93 : bool res = 0;
11150 : 93 : va_list ap;
11151 : 93 : const_tree arg;
11152 : 93 : size_t i;
11153 : :
11154 : 93 : va_start (ap, call);
11155 : 93 : i = 0;
11156 : :
11157 : 372 : do
11158 : : {
11159 : 372 : code = (enum tree_code) va_arg (ap, int);
11160 : 372 : switch (code)
11161 : : {
11162 : 0 : case 0:
11163 : : /* This signifies an ellipses, any further arguments are all ok. */
11164 : 0 : res = true;
11165 : 0 : goto end;
11166 : 93 : case VOID_TYPE:
11167 : : /* This signifies an endlink, if no arguments remain, return
11168 : : true, otherwise return false. */
11169 : 93 : res = (i == gimple_call_num_args (call));
11170 : 93 : goto end;
11171 : 279 : default:
11172 : : /* If no parameters remain or the parameter's code does not
11173 : : match the specified code, return false. Otherwise continue
11174 : : checking any remaining arguments. */
11175 : 279 : arg = gimple_call_arg (call, i++);
11176 : 279 : if (!validate_arg (arg, code))
11177 : 0 : goto end;
11178 : : break;
11179 : : }
11180 : : }
11181 : : while (1);
11182 : :
11183 : : /* We need gotos here since we can only have one VA_CLOSE in a
11184 : : function. */
11185 : 93 : end: ;
11186 : 93 : va_end (ap);
11187 : :
11188 : 93 : return res;
11189 : : }
11190 : :
11191 : : /* Default target-specific builtin expander that does nothing. */
11192 : :
11193 : : rtx
11194 : 0 : default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11195 : : rtx target ATTRIBUTE_UNUSED,
11196 : : rtx subtarget ATTRIBUTE_UNUSED,
11197 : : machine_mode mode ATTRIBUTE_UNUSED,
11198 : : int ignore ATTRIBUTE_UNUSED)
11199 : : {
11200 : 0 : return NULL_RTX;
11201 : : }
11202 : :
11203 : : /* Returns true is EXP represents data that would potentially reside
11204 : : in a readonly section. */
11205 : :
11206 : : bool
11207 : 196853 : readonly_data_expr (tree exp)
11208 : : {
11209 : 196853 : STRIP_NOPS (exp);
11210 : :
11211 : 196853 : if (TREE_CODE (exp) != ADDR_EXPR)
11212 : : return false;
11213 : :
11214 : 23868 : exp = get_base_address (TREE_OPERAND (exp, 0));
11215 : 23868 : if (!exp)
11216 : : return false;
11217 : :
11218 : : /* Make sure we call decl_readonly_section only for trees it
11219 : : can handle (since it returns true for everything it doesn't
11220 : : understand). */
11221 : 23868 : if (TREE_CODE (exp) == STRING_CST
11222 : 5792 : || TREE_CODE (exp) == CONSTRUCTOR
11223 : 5792 : || (VAR_P (exp) && TREE_STATIC (exp)))
11224 : 20820 : return decl_readonly_section (exp, 0);
11225 : : else
11226 : : return false;
11227 : : }
11228 : :
11229 : : /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11230 : : to the call, and TYPE is its return type.
11231 : :
11232 : : Return NULL_TREE if no simplification was possible, otherwise return the
11233 : : simplified form of the call as a tree.
11234 : :
11235 : : The simplified form may be a constant or other expression which
11236 : : computes the same value, but in a more efficient manner (including
11237 : : calls to other builtin functions).
11238 : :
11239 : : The call may contain arguments which need to be evaluated, but
11240 : : which are not useful to determine the result of the call. In
11241 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11242 : : COMPOUND_EXPR will be an argument which must be evaluated.
11243 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11244 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11245 : : form of the builtin function call. */
11246 : :
11247 : : static tree
11248 : 82381 : fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
11249 : : {
11250 : 82381 : if (!validate_arg (s1, POINTER_TYPE)
11251 : 82381 : || !validate_arg (s2, POINTER_TYPE))
11252 : : return NULL_TREE;
11253 : :
11254 : 82381 : tree fn;
11255 : 82381 : const char *p1, *p2;
11256 : :
11257 : 82381 : p2 = c_getstr (s2);
11258 : 82381 : if (p2 == NULL)
11259 : : return NULL_TREE;
11260 : :
11261 : 92 : p1 = c_getstr (s1);
11262 : 92 : if (p1 != NULL)
11263 : : {
11264 : 22 : const char *r = strpbrk (p1, p2);
11265 : 22 : tree tem;
11266 : :
11267 : 22 : if (r == NULL)
11268 : 0 : return build_int_cst (TREE_TYPE (s1), 0);
11269 : :
11270 : : /* Return an offset into the constant string argument. */
11271 : 22 : tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11272 : 22 : return fold_convert_loc (loc, type, tem);
11273 : : }
11274 : :
11275 : 70 : if (p2[0] == '\0')
11276 : : /* strpbrk(x, "") == NULL.
11277 : : Evaluate and ignore s1 in case it had side-effects. */
11278 : 26 : return omit_one_operand_loc (loc, type, integer_zero_node, s1);
11279 : :
11280 : 44 : if (p2[1] != '\0')
11281 : : return NULL_TREE; /* Really call strpbrk. */
11282 : :
11283 : 82333 : fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11284 : 41 : if (!fn)
11285 : : return NULL_TREE;
11286 : :
11287 : : /* New argument list transforming strpbrk(s1, s2) to
11288 : : strchr(s1, s2[0]). */
11289 : 41 : return build_call_expr_loc (loc, fn, 2, s1,
11290 : 41 : build_int_cst (integer_type_node, p2[0]));
11291 : : }
11292 : :
11293 : : /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11294 : : to the call.
11295 : :
11296 : : Return NULL_TREE if no simplification was possible, otherwise return the
11297 : : simplified form of the call as a tree.
11298 : :
11299 : : The simplified form may be a constant or other expression which
11300 : : computes the same value, but in a more efficient manner (including
11301 : : calls to other builtin functions).
11302 : :
11303 : : The call may contain arguments which need to be evaluated, but
11304 : : which are not useful to determine the result of the call. In
11305 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11306 : : COMPOUND_EXPR will be an argument which must be evaluated.
11307 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11308 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11309 : : form of the builtin function call. */
11310 : :
11311 : : static tree
11312 : 2655 : fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11313 : : {
11314 : 2655 : if (!validate_arg (s1, POINTER_TYPE)
11315 : 2655 : || !validate_arg (s2, POINTER_TYPE))
11316 : : return NULL_TREE;
11317 : :
11318 : 2655 : if (!check_nul_terminated_array (expr, s1)
11319 : 2655 : || !check_nul_terminated_array (expr, s2))
11320 : 60 : return NULL_TREE;
11321 : :
11322 : 2595 : const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11323 : :
11324 : : /* If either argument is "", return NULL_TREE. */
11325 : 2595 : if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11326 : : /* Evaluate and ignore both arguments in case either one has
11327 : : side-effects. */
11328 : 147 : return omit_two_operands_loc (loc, type, size_zero_node, s1, s2);
11329 : : return NULL_TREE;
11330 : : }
11331 : :
11332 : : /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11333 : : to the call.
11334 : :
11335 : : Return NULL_TREE if no simplification was possible, otherwise return the
11336 : : simplified form of the call as a tree.
11337 : :
11338 : : The simplified form may be a constant or other expression which
11339 : : computes the same value, but in a more efficient manner (including
11340 : : calls to other builtin functions).
11341 : :
11342 : : The call may contain arguments which need to be evaluated, but
11343 : : which are not useful to determine the result of the call. In
11344 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
11345 : : COMPOUND_EXPR will be an argument which must be evaluated.
11346 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11347 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
11348 : : form of the builtin function call. */
11349 : :
11350 : : static tree
11351 : 2553 : fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11352 : : {
11353 : 2553 : if (!validate_arg (s1, POINTER_TYPE)
11354 : 2553 : || !validate_arg (s2, POINTER_TYPE))
11355 : : return NULL_TREE;
11356 : :
11357 : 2553 : if (!check_nul_terminated_array (expr, s1)
11358 : 2553 : || !check_nul_terminated_array (expr, s2))
11359 : 60 : return NULL_TREE;
11360 : :
11361 : : /* If the first argument is "", return NULL_TREE. */
11362 : 2493 : const char *p1 = c_getstr (s1);
11363 : 2493 : if (p1 && *p1 == '\0')
11364 : : {
11365 : : /* Evaluate and ignore argument s2 in case it has
11366 : : side-effects. */
11367 : 65 : return omit_one_operand_loc (loc, type, size_zero_node, s2);
11368 : : }
11369 : :
11370 : : /* If the second argument is "", return __builtin_strlen(s1). */
11371 : 2428 : const char *p2 = c_getstr (s2);
11372 : 2428 : if (p2 && *p2 == '\0')
11373 : : {
11374 : 2488 : tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11375 : :
11376 : : /* If the replacement _DECL isn't initialized, don't do the
11377 : : transformation. */
11378 : 81 : if (!fn)
11379 : : return NULL_TREE;
11380 : :
11381 : 81 : return fold_convert_loc (loc, type,
11382 : 81 : build_call_expr_loc (loc, fn, 1, s1));
11383 : : }
11384 : : return NULL_TREE;
11385 : : }
11386 : :
11387 : : /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11388 : : produced. False otherwise. This is done so that we don't output the error
11389 : : or warning twice or three times. */
11390 : :
11391 : : bool
11392 : 41713 : fold_builtin_next_arg (tree exp, bool va_start_p)
11393 : : {
11394 : 41713 : tree fntype = TREE_TYPE (current_function_decl);
11395 : 41713 : int nargs = call_expr_nargs (exp);
11396 : 41713 : tree arg;
11397 : : /* There is good chance the current input_location points inside the
11398 : : definition of the va_start macro (perhaps on the token for
11399 : : builtin) in a system header, so warnings will not be emitted.
11400 : : Use the location in real source code. */
11401 : 41713 : location_t current_location =
11402 : 41713 : linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11403 : : NULL);
11404 : :
11405 : 41713 : if (!stdarg_p (fntype))
11406 : : {
11407 : 8 : error ("%<va_start%> used in function with fixed arguments");
11408 : 8 : return true;
11409 : : }
11410 : :
11411 : 41705 : if (va_start_p)
11412 : : {
11413 : 41555 : if (va_start_p && (nargs != 2))
11414 : : {
11415 : 0 : error ("wrong number of arguments to function %<va_start%>");
11416 : 0 : return true;
11417 : : }
11418 : 41555 : arg = CALL_EXPR_ARG (exp, 1);
11419 : : }
11420 : : /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11421 : : when we checked the arguments and if needed issued a warning. */
11422 : : else
11423 : : {
11424 : 150 : if (nargs == 0)
11425 : : {
11426 : : /* Evidently an out of date version of <stdarg.h>; can't validate
11427 : : va_start's second argument, but can still work as intended. */
11428 : 0 : warning_at (current_location,
11429 : 0 : OPT_Wvarargs,
11430 : : "%<__builtin_next_arg%> called without an argument");
11431 : 0 : return true;
11432 : : }
11433 : 150 : else if (nargs > 1)
11434 : : {
11435 : 0 : error ("wrong number of arguments to function %<__builtin_next_arg%>");
11436 : 0 : return true;
11437 : : }
11438 : 150 : arg = CALL_EXPR_ARG (exp, 0);
11439 : : }
11440 : :
11441 : 41705 : if (TREE_CODE (arg) == SSA_NAME
11442 : 41705 : && SSA_NAME_VAR (arg))
11443 : : arg = SSA_NAME_VAR (arg);
11444 : :
11445 : : /* We destructively modify the call to be __builtin_va_start (ap, 0)
11446 : : or __builtin_next_arg (0) the first time we see it, after checking
11447 : : the arguments and if needed issuing a warning. */
11448 : 41705 : if (!integer_zerop (arg))
11449 : : {
11450 : 7089 : tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11451 : :
11452 : : /* Strip off all nops for the sake of the comparison. This
11453 : : is not quite the same as STRIP_NOPS. It does more.
11454 : : We must also strip off INDIRECT_EXPR for C++ reference
11455 : : parameters. */
11456 : 14181 : while (CONVERT_EXPR_P (arg)
11457 : 14195 : || INDIRECT_REF_P (arg))
11458 : 14 : arg = TREE_OPERAND (arg, 0);
11459 : 7089 : if (arg != last_parm)
11460 : : {
11461 : : /* FIXME: Sometimes with the tree optimizers we can get the
11462 : : not the last argument even though the user used the last
11463 : : argument. We just warn and set the arg to be the last
11464 : : argument so that we will get wrong-code because of
11465 : : it. */
11466 : 12 : warning_at (current_location,
11467 : 12 : OPT_Wvarargs,
11468 : : "second parameter of %<va_start%> not last named argument");
11469 : : }
11470 : :
11471 : : /* Undefined by C99 7.15.1.4p4 (va_start):
11472 : : "If the parameter parmN is declared with the register storage
11473 : : class, with a function or array type, or with a type that is
11474 : : not compatible with the type that results after application of
11475 : : the default argument promotions, the behavior is undefined."
11476 : : */
11477 : 7077 : else if (DECL_REGISTER (arg))
11478 : : {
11479 : 12 : warning_at (current_location,
11480 : 12 : OPT_Wvarargs,
11481 : : "undefined behavior when second parameter of "
11482 : : "%<va_start%> is declared with %<register%> storage");
11483 : : }
11484 : :
11485 : : /* We want to verify the second parameter just once before the tree
11486 : : optimizers are run and then avoid keeping it in the tree,
11487 : : as otherwise we could warn even for correct code like:
11488 : : void foo (int i, ...)
11489 : : { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11490 : 7089 : if (va_start_p)
11491 : 7087 : CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11492 : : else
11493 : 2 : CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11494 : : }
11495 : : return false;
11496 : : }
11497 : :
11498 : :
11499 : : /* Expand a call EXP to __builtin_object_size. */
11500 : :
11501 : : static rtx
11502 : 617 : expand_builtin_object_size (tree exp)
11503 : : {
11504 : 617 : tree ost;
11505 : 617 : int object_size_type;
11506 : 617 : tree fndecl = get_callee_fndecl (exp);
11507 : :
11508 : 617 : if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11509 : : {
11510 : 0 : error ("first argument of %qD must be a pointer, second integer constant",
11511 : : fndecl);
11512 : 0 : expand_builtin_trap ();
11513 : 0 : return const0_rtx;
11514 : : }
11515 : :
11516 : 617 : ost = CALL_EXPR_ARG (exp, 1);
11517 : 617 : STRIP_NOPS (ost);
11518 : :
11519 : 617 : if (TREE_CODE (ost) != INTEGER_CST
11520 : 617 : || tree_int_cst_sgn (ost) < 0
11521 : 1234 : || compare_tree_int (ost, 3) > 0)
11522 : : {
11523 : 0 : error ("last argument of %qD is not integer constant between 0 and 3",
11524 : : fndecl);
11525 : 0 : expand_builtin_trap ();
11526 : 0 : return const0_rtx;
11527 : : }
11528 : :
11529 : 617 : object_size_type = tree_to_shwi (ost);
11530 : :
11531 : 617 : return object_size_type < 2 ? constm1_rtx : const0_rtx;
11532 : : }
11533 : :
11534 : : /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11535 : : FCODE is the BUILT_IN_* to use.
11536 : : Return NULL_RTX if we failed; the caller should emit a normal call,
11537 : : otherwise try to get the result in TARGET, if convenient (and in
11538 : : mode MODE if that's convenient). */
11539 : :
11540 : : static rtx
11541 : 837 : expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11542 : : enum built_in_function fcode)
11543 : : {
11544 : 1410 : if (!validate_arglist (exp,
11545 : : POINTER_TYPE,
11546 : : fcode == BUILT_IN_MEMSET_CHK
11547 : : ? INTEGER_TYPE : POINTER_TYPE,
11548 : : INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11549 : : return NULL_RTX;
11550 : :
11551 : 837 : tree dest = CALL_EXPR_ARG (exp, 0);
11552 : 837 : tree src = CALL_EXPR_ARG (exp, 1);
11553 : 837 : tree len = CALL_EXPR_ARG (exp, 2);
11554 : 837 : tree size = CALL_EXPR_ARG (exp, 3);
11555 : :
11556 : : /* FIXME: Set access mode to write only for memset et al. */
11557 : 837 : bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
11558 : : /*srcstr=*/NULL_TREE, size, access_read_write);
11559 : :
11560 : 837 : if (!tree_fits_uhwi_p (size))
11561 : : return NULL_RTX;
11562 : :
11563 : 627 : if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11564 : : {
11565 : : /* Avoid transforming the checking call to an ordinary one when
11566 : : an overflow has been detected or when the call couldn't be
11567 : : validated because the size is not constant. */
11568 : 186 : if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
11569 : : return NULL_RTX;
11570 : :
11571 : 0 : tree fn = NULL_TREE;
11572 : : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11573 : : mem{cpy,pcpy,move,set} is available. */
11574 : 0 : switch (fcode)
11575 : : {
11576 : 0 : case BUILT_IN_MEMCPY_CHK:
11577 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11578 : 0 : break;
11579 : 0 : case BUILT_IN_MEMPCPY_CHK:
11580 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11581 : 0 : break;
11582 : 0 : case BUILT_IN_MEMMOVE_CHK:
11583 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11584 : 0 : break;
11585 : 0 : case BUILT_IN_MEMSET_CHK:
11586 : 0 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11587 : 0 : break;
11588 : : default:
11589 : : break;
11590 : : }
11591 : :
11592 : 0 : if (! fn)
11593 : : return NULL_RTX;
11594 : :
11595 : 0 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11596 : 0 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11597 : 0 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11598 : 0 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11599 : : }
11600 : 441 : else if (fcode == BUILT_IN_MEMSET_CHK)
11601 : : return NULL_RTX;
11602 : : else
11603 : : {
11604 : 293 : unsigned int dest_align = get_pointer_alignment (dest);
11605 : :
11606 : : /* If DEST is not a pointer type, call the normal function. */
11607 : 293 : if (dest_align == 0)
11608 : : return NULL_RTX;
11609 : :
11610 : : /* If SRC and DEST are the same (and not volatile), do nothing. */
11611 : 293 : if (operand_equal_p (src, dest, 0))
11612 : : {
11613 : 0 : tree expr;
11614 : :
11615 : 0 : if (fcode != BUILT_IN_MEMPCPY_CHK)
11616 : : {
11617 : : /* Evaluate and ignore LEN in case it has side-effects. */
11618 : 0 : expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11619 : 0 : return expand_expr (dest, target, mode, EXPAND_NORMAL);
11620 : : }
11621 : :
11622 : 0 : expr = fold_build_pointer_plus (dest, len);
11623 : 0 : return expand_expr (expr, target, mode, EXPAND_NORMAL);
11624 : : }
11625 : :
11626 : : /* __memmove_chk special case. */
11627 : 293 : if (fcode == BUILT_IN_MEMMOVE_CHK)
11628 : : {
11629 : 79 : unsigned int src_align = get_pointer_alignment (src);
11630 : :
11631 : 79 : if (src_align == 0)
11632 : : return NULL_RTX;
11633 : :
11634 : : /* If src is categorized for a readonly section we can use
11635 : : normal __memcpy_chk. */
11636 : 79 : if (readonly_data_expr (src))
11637 : : {
11638 : 15 : tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11639 : 15 : if (!fn)
11640 : : return NULL_RTX;
11641 : 15 : fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11642 : : dest, src, len, size);
11643 : 15 : gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11644 : 15 : CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11645 : 15 : return expand_expr (fn, target, mode, EXPAND_NORMAL);
11646 : : }
11647 : : }
11648 : 278 : return NULL_RTX;
11649 : : }
11650 : : }
11651 : :
11652 : : /* Emit warning if a buffer overflow is detected at compile time. */
11653 : :
11654 : : static void
11655 : 1135 : maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11656 : : {
11657 : : /* The source string. */
11658 : 1135 : tree srcstr = NULL_TREE;
11659 : : /* The size of the destination object returned by __builtin_object_size. */
11660 : 1135 : tree objsize = NULL_TREE;
11661 : : /* The string that is being concatenated with (as in __strcat_chk)
11662 : : or null if it isn't. */
11663 : 1135 : tree catstr = NULL_TREE;
11664 : : /* The maximum length of the source sequence in a bounded operation
11665 : : (such as __strncat_chk) or null if the operation isn't bounded
11666 : : (such as __strcat_chk). */
11667 : 1135 : tree maxread = NULL_TREE;
11668 : : /* The exact size of the access (such as in __strncpy_chk). */
11669 : 1135 : tree size = NULL_TREE;
11670 : : /* The access by the function that's checked. Except for snprintf
11671 : : both writing and reading is checked. */
11672 : 1135 : access_mode mode = access_read_write;
11673 : :
11674 : 1135 : switch (fcode)
11675 : : {
11676 : 276 : case BUILT_IN_STRCPY_CHK:
11677 : 276 : case BUILT_IN_STPCPY_CHK:
11678 : 276 : srcstr = CALL_EXPR_ARG (exp, 1);
11679 : 276 : objsize = CALL_EXPR_ARG (exp, 2);
11680 : 276 : break;
11681 : :
11682 : 198 : case BUILT_IN_STRCAT_CHK:
11683 : : /* For __strcat_chk the warning will be emitted only if overflowing
11684 : : by at least strlen (dest) + 1 bytes. */
11685 : 198 : catstr = CALL_EXPR_ARG (exp, 0);
11686 : 198 : srcstr = CALL_EXPR_ARG (exp, 1);
11687 : 198 : objsize = CALL_EXPR_ARG (exp, 2);
11688 : 198 : break;
11689 : :
11690 : 109 : case BUILT_IN_STRNCAT_CHK:
11691 : 109 : catstr = CALL_EXPR_ARG (exp, 0);
11692 : 109 : srcstr = CALL_EXPR_ARG (exp, 1);
11693 : 109 : maxread = CALL_EXPR_ARG (exp, 2);
11694 : 109 : objsize = CALL_EXPR_ARG (exp, 3);
11695 : 109 : break;
11696 : :
11697 : 243 : case BUILT_IN_STRNCPY_CHK:
11698 : 243 : case BUILT_IN_STPNCPY_CHK:
11699 : 243 : srcstr = CALL_EXPR_ARG (exp, 1);
11700 : 243 : size = CALL_EXPR_ARG (exp, 2);
11701 : 243 : objsize = CALL_EXPR_ARG (exp, 3);
11702 : 243 : break;
11703 : :
11704 : 309 : case BUILT_IN_SNPRINTF_CHK:
11705 : 309 : case BUILT_IN_VSNPRINTF_CHK:
11706 : 309 : maxread = CALL_EXPR_ARG (exp, 1);
11707 : 309 : objsize = CALL_EXPR_ARG (exp, 3);
11708 : : /* The only checked access the write to the destination. */
11709 : 309 : mode = access_write_only;
11710 : 309 : break;
11711 : 0 : default:
11712 : 0 : gcc_unreachable ();
11713 : : }
11714 : :
11715 : 1135 : if (catstr && maxread)
11716 : : {
11717 : : /* Check __strncat_chk. There is no way to determine the length
11718 : : of the string to which the source string is being appended so
11719 : : just warn when the length of the source string is not known. */
11720 : 109 : check_strncat_sizes (exp, objsize);
11721 : 109 : return;
11722 : : }
11723 : :
11724 : 1026 : check_access (exp, size, maxread, srcstr, objsize, mode);
11725 : : }
11726 : :
11727 : : /* Emit warning if a buffer overflow is detected at compile time
11728 : : in __sprintf_chk/__vsprintf_chk calls. */
11729 : :
11730 : : static void
11731 : 1329 : maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11732 : : {
11733 : 1329 : tree size, len, fmt;
11734 : 1329 : const char *fmt_str;
11735 : 1329 : int nargs = call_expr_nargs (exp);
11736 : :
11737 : : /* Verify the required arguments in the original call. */
11738 : :
11739 : 1329 : if (nargs < 4)
11740 : : return;
11741 : 1329 : size = CALL_EXPR_ARG (exp, 2);
11742 : 1329 : fmt = CALL_EXPR_ARG (exp, 3);
11743 : :
11744 : 1329 : if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11745 : 21 : return;
11746 : :
11747 : : /* Check whether the format is a literal string constant. */
11748 : 1308 : fmt_str = c_getstr (fmt);
11749 : 1308 : if (fmt_str == NULL)
11750 : : return;
11751 : :
11752 : 1272 : if (!init_target_chars ())
11753 : : return;
11754 : :
11755 : : /* If the format doesn't contain % args or %%, we know its size. */
11756 : 1272 : if (strchr (fmt_str, target_percent) == 0)
11757 : 22 : len = build_int_cstu (size_type_node, strlen (fmt_str));
11758 : : /* If the format is "%s" and first ... argument is a string literal,
11759 : : we know it too. */
11760 : 1250 : else if (fcode == BUILT_IN_SPRINTF_CHK
11761 : 1094 : && strcmp (fmt_str, target_percent_s) == 0)
11762 : : {
11763 : 49 : tree arg;
11764 : :
11765 : 49 : if (nargs < 5)
11766 : : return;
11767 : 49 : arg = CALL_EXPR_ARG (exp, 4);
11768 : 49 : if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11769 : : return;
11770 : :
11771 : 45 : len = c_strlen (arg, 1);
11772 : 45 : if (!len || ! tree_fits_uhwi_p (len))
11773 : : return;
11774 : : }
11775 : : else
11776 : : return;
11777 : :
11778 : : /* Add one for the terminating nul. */
11779 : 34 : len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
11780 : :
11781 : 34 : check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
11782 : : access_write_only);
11783 : : }
11784 : :
11785 : : /* Fold a call to __builtin_object_size with arguments PTR and OST,
11786 : : if possible. */
11787 : :
11788 : : static tree
11789 : 199834 : fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
11790 : : {
11791 : 199834 : tree bytes;
11792 : 199834 : int object_size_type;
11793 : :
11794 : 199834 : if (!validate_arg (ptr, POINTER_TYPE)
11795 : 199834 : || !validate_arg (ost, INTEGER_TYPE))
11796 : : return NULL_TREE;
11797 : :
11798 : 199834 : STRIP_NOPS (ost);
11799 : :
11800 : 199834 : if (TREE_CODE (ost) != INTEGER_CST
11801 : 199834 : || tree_int_cst_sgn (ost) < 0
11802 : 399668 : || compare_tree_int (ost, 3) > 0)
11803 : 0 : return NULL_TREE;
11804 : :
11805 : 199834 : object_size_type = tree_to_shwi (ost);
11806 : :
11807 : : /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11808 : : if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11809 : : and (size_t) 0 for types 2 and 3. */
11810 : 199834 : if (TREE_SIDE_EFFECTS (ptr))
11811 : 603 : return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11812 : :
11813 : 199231 : if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
11814 : 66404 : object_size_type |= OST_DYNAMIC;
11815 : :
11816 : 199231 : if (TREE_CODE (ptr) == ADDR_EXPR)
11817 : : {
11818 : 12077 : compute_builtin_object_size (ptr, object_size_type, &bytes);
11819 : 12077 : if ((object_size_type & OST_DYNAMIC)
11820 : 10892 : || int_fits_type_p (bytes, size_type_node))
11821 : 12077 : return fold_convert (size_type_node, bytes);
11822 : : }
11823 : 187154 : else if (TREE_CODE (ptr) == SSA_NAME)
11824 : : {
11825 : : /* If object size is not known yet, delay folding until
11826 : : later. Maybe subsequent passes will help determining
11827 : : it. */
11828 : 101865 : if (compute_builtin_object_size (ptr, object_size_type, &bytes)
11829 : 101865 : && ((object_size_type & OST_DYNAMIC)
11830 : 2544 : || int_fits_type_p (bytes, size_type_node)))
11831 : 4859 : return fold_convert (size_type_node, bytes);
11832 : : }
11833 : :
11834 : : return NULL_TREE;
11835 : : }
11836 : :
11837 : : /* Builtins with folding operations that operate on "..." arguments
11838 : : need special handling; we need to store the arguments in a convenient
11839 : : data structure before attempting any folding. Fortunately there are
11840 : : only a few builtins that fall into this category. FNDECL is the
11841 : : function, EXP is the CALL_EXPR for the call. */
11842 : :
11843 : : static tree
11844 : 2637742 : fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11845 : : {
11846 : 2637742 : enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11847 : 2637742 : tree ret = NULL_TREE;
11848 : :
11849 : 2637742 : switch (fcode)
11850 : : {
11851 : 113838 : case BUILT_IN_FPCLASSIFY:
11852 : 113838 : ret = fold_builtin_fpclassify (loc, args, nargs);
11853 : 113838 : break;
11854 : :
11855 : 54 : case BUILT_IN_ADDC:
11856 : 54 : case BUILT_IN_ADDCL:
11857 : 54 : case BUILT_IN_ADDCLL:
11858 : 54 : case BUILT_IN_SUBC:
11859 : 54 : case BUILT_IN_SUBCL:
11860 : 54 : case BUILT_IN_SUBCLL:
11861 : 54 : return fold_builtin_addc_subc (loc, fcode, args);
11862 : :
11863 : : default:
11864 : : break;
11865 : : }
11866 : 113838 : if (ret)
11867 : : {
11868 : 113838 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11869 : 113838 : SET_EXPR_LOCATION (ret, loc);
11870 : 113838 : suppress_warning (ret);
11871 : 113838 : return ret;
11872 : : }
11873 : : return NULL_TREE;
11874 : : }
11875 : :
11876 : : /* Initialize format string characters in the target charset. */
11877 : :
11878 : : bool
11879 : 288678 : init_target_chars (void)
11880 : : {
11881 : 288678 : static bool init;
11882 : 288678 : if (!init)
11883 : : {
11884 : 111314 : target_newline = lang_hooks.to_target_charset ('\n');
11885 : 111314 : target_percent = lang_hooks.to_target_charset ('%');
11886 : 111314 : target_c = lang_hooks.to_target_charset ('c');
11887 : 111314 : target_s = lang_hooks.to_target_charset ('s');
11888 : 111314 : if (target_newline == 0 || target_percent == 0 || target_c == 0
11889 : 111314 : || target_s == 0)
11890 : : return false;
11891 : :
11892 : 111314 : target_percent_c[0] = target_percent;
11893 : 111314 : target_percent_c[1] = target_c;
11894 : 111314 : target_percent_c[2] = '\0';
11895 : :
11896 : 111314 : target_percent_s[0] = target_percent;
11897 : 111314 : target_percent_s[1] = target_s;
11898 : 111314 : target_percent_s[2] = '\0';
11899 : :
11900 : 111314 : target_percent_s_newline[0] = target_percent;
11901 : 111314 : target_percent_s_newline[1] = target_s;
11902 : 111314 : target_percent_s_newline[2] = target_newline;
11903 : 111314 : target_percent_s_newline[3] = '\0';
11904 : :
11905 : 111314 : init = true;
11906 : : }
11907 : : return true;
11908 : : }
11909 : :
11910 : : /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11911 : : and no overflow/underflow occurred. INEXACT is true if M was not
11912 : : exactly calculated. TYPE is the tree type for the result. This
11913 : : function assumes that you cleared the MPFR flags and then
11914 : : calculated M to see if anything subsequently set a flag prior to
11915 : : entering this function. Return NULL_TREE if any checks fail. */
11916 : :
11917 : : static tree
11918 : 3037 : do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11919 : : {
11920 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11921 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11922 : : result of calling FUNC was exact. */
11923 : 4890 : if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11924 : 4890 : && (!flag_rounding_math || !inexact))
11925 : : {
11926 : 1853 : REAL_VALUE_TYPE rr;
11927 : :
11928 : 1853 : real_from_mpfr (&rr, m, type, MPFR_RNDN);
11929 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11930 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11931 : : but the mpfr_t is not, then we underflowed in the
11932 : : conversion. */
11933 : 1853 : if (real_isfinite (&rr)
11934 : 1853 : && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11935 : : {
11936 : 1853 : REAL_VALUE_TYPE rmode;
11937 : :
11938 : 1853 : real_convert (&rmode, TYPE_MODE (type), &rr);
11939 : : /* Proceed iff the specified mode can hold the value. */
11940 : 1853 : if (real_identical (&rmode, &rr))
11941 : 1853 : return build_real (type, rmode);
11942 : : }
11943 : : }
11944 : : return NULL_TREE;
11945 : : }
11946 : :
11947 : : /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11948 : : number and no overflow/underflow occurred. INEXACT is true if M
11949 : : was not exactly calculated. TYPE is the tree type for the result.
11950 : : This function assumes that you cleared the MPFR flags and then
11951 : : calculated M to see if anything subsequently set a flag prior to
11952 : : entering this function. Return NULL_TREE if any checks fail, if
11953 : : FORCE_CONVERT is true, then bypass the checks. */
11954 : :
11955 : : static tree
11956 : 4130 : do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11957 : : {
11958 : : /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11959 : : overflow/underflow occurred. If -frounding-math, proceed iff the
11960 : : result of calling FUNC was exact. */
11961 : 4130 : if (force_convert
11962 : 4130 : || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11963 : 3784 : && !mpfr_overflow_p () && !mpfr_underflow_p ()
11964 : 3784 : && (!flag_rounding_math || !inexact)))
11965 : : {
11966 : 3951 : REAL_VALUE_TYPE re, im;
11967 : :
11968 : 3951 : real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
11969 : 3951 : real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
11970 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11971 : : check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11972 : : but the mpfr_t is not, then we underflowed in the
11973 : : conversion. */
11974 : 3951 : if (force_convert
11975 : 3951 : || (real_isfinite (&re) && real_isfinite (&im)
11976 : 3784 : && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11977 : 3784 : && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11978 : : {
11979 : 3951 : REAL_VALUE_TYPE re_mode, im_mode;
11980 : :
11981 : 3951 : real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11982 : 3951 : real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11983 : : /* Proceed iff the specified mode can hold the value. */
11984 : 3951 : if (force_convert
11985 : 3951 : || (real_identical (&re_mode, &re)
11986 : 3784 : && real_identical (&im_mode, &im)))
11987 : 3951 : return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11988 : 7902 : build_real (TREE_TYPE (type), im_mode));
11989 : : }
11990 : : }
11991 : : return NULL_TREE;
11992 : : }
11993 : :
11994 : : /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11995 : : the pointer *(ARG_QUO) and return the result. The type is taken
11996 : : from the type of ARG0 and is used for setting the precision of the
11997 : : calculation and results. */
11998 : :
11999 : : static tree
12000 : 85934 : do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12001 : : {
12002 : 85934 : tree const type = TREE_TYPE (arg0);
12003 : 85934 : tree result = NULL_TREE;
12004 : :
12005 : 85934 : STRIP_NOPS (arg0);
12006 : 85934 : STRIP_NOPS (arg1);
12007 : :
12008 : : /* To proceed, MPFR must exactly represent the target floating point
12009 : : format, which only happens when the target base equals two. */
12010 : 85934 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12011 : 85934 : && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12012 : 88297 : && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12013 : : {
12014 : 2363 : const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12015 : 2363 : const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12016 : :
12017 : 2363 : if (real_isfinite (ra0) && real_isfinite (ra1))
12018 : : {
12019 : 2363 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12020 : 2363 : const int prec = fmt->p;
12021 : 2363 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12022 : 2363 : tree result_rem;
12023 : 2363 : long integer_quo;
12024 : 2363 : mpfr_t m0, m1;
12025 : :
12026 : 2363 : mpfr_inits2 (prec, m0, m1, NULL);
12027 : 2363 : mpfr_from_real (m0, ra0, MPFR_RNDN);
12028 : 2363 : mpfr_from_real (m1, ra1, MPFR_RNDN);
12029 : 2363 : mpfr_clear_flags ();
12030 : 2363 : mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12031 : : /* Remquo is independent of the rounding mode, so pass
12032 : : inexact=0 to do_mpfr_ckconv(). */
12033 : 2363 : result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12034 : 2363 : mpfr_clears (m0, m1, NULL);
12035 : 2363 : if (result_rem)
12036 : : {
12037 : : /* MPFR calculates quo in the host's long so it may
12038 : : return more bits in quo than the target int can hold
12039 : : if sizeof(host long) > sizeof(target int). This can
12040 : : happen even for native compilers in LP64 mode. In
12041 : : these cases, modulo the quo value with the largest
12042 : : number that the target int can hold while leaving one
12043 : : bit for the sign. */
12044 : 1179 : if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12045 : 1179 : integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12046 : :
12047 : : /* Dereference the quo pointer argument. */
12048 : 1179 : arg_quo = build_fold_indirect_ref (arg_quo);
12049 : : /* Proceed iff a valid pointer type was passed in. */
12050 : 1179 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12051 : : {
12052 : : /* Set the value. */
12053 : 1179 : tree result_quo
12054 : 1179 : = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12055 : : build_int_cst (TREE_TYPE (arg_quo),
12056 : : integer_quo));
12057 : 1179 : TREE_SIDE_EFFECTS (result_quo) = 1;
12058 : : /* Combine the quo assignment with the rem. */
12059 : 1179 : result = fold_build2 (COMPOUND_EXPR, type,
12060 : : result_quo, result_rem);
12061 : 1179 : suppress_warning (result, OPT_Wunused_value);
12062 : 1179 : result = non_lvalue (result);
12063 : : }
12064 : : }
12065 : : }
12066 : : }
12067 : 85934 : return result;
12068 : : }
12069 : :
12070 : : /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12071 : : resulting value as a tree with type TYPE. The mpfr precision is
12072 : : set to the precision of TYPE. We assume that this mpfr function
12073 : : returns zero if the result could be calculated exactly within the
12074 : : requested precision. In addition, the integer pointer represented
12075 : : by ARG_SG will be dereferenced and set to the appropriate signgam
12076 : : (-1,1) value. */
12077 : :
12078 : : static tree
12079 : 6720 : do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12080 : : {
12081 : 6720 : tree result = NULL_TREE;
12082 : :
12083 : 6720 : STRIP_NOPS (arg);
12084 : :
12085 : : /* To proceed, MPFR must exactly represent the target floating point
12086 : : format, which only happens when the target base equals two. Also
12087 : : verify ARG is a constant and that ARG_SG is an int pointer. */
12088 : 6720 : if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12089 : 6720 : && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12090 : 6686 : && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12091 : 13406 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12092 : : {
12093 : 6686 : const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12094 : :
12095 : : /* In addition to NaN and Inf, the argument cannot be zero or a
12096 : : negative integer. */
12097 : 6686 : if (real_isfinite (ra)
12098 : 6686 : && ra->cl != rvc_zero
12099 : 13372 : && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12100 : : {
12101 : 674 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12102 : 674 : const int prec = fmt->p;
12103 : 674 : const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12104 : 674 : int inexact, sg;
12105 : 674 : tree result_lg;
12106 : :
12107 : 674 : auto_mpfr m (prec);
12108 : 674 : mpfr_from_real (m, ra, MPFR_RNDN);
12109 : 674 : mpfr_clear_flags ();
12110 : 674 : inexact = mpfr_lgamma (m, &sg, m, rnd);
12111 : 674 : result_lg = do_mpfr_ckconv (m, type, inexact);
12112 : 674 : if (result_lg)
12113 : : {
12114 : 674 : tree result_sg;
12115 : :
12116 : : /* Dereference the arg_sg pointer argument. */
12117 : 674 : arg_sg = build_fold_indirect_ref (arg_sg);
12118 : : /* Assign the signgam value into *arg_sg. */
12119 : 674 : result_sg = fold_build2 (MODIFY_EXPR,
12120 : : TREE_TYPE (arg_sg), arg_sg,
12121 : : build_int_cst (TREE_TYPE (arg_sg), sg));
12122 : 674 : TREE_SIDE_EFFECTS (result_sg) = 1;
12123 : : /* Combine the signgam assignment with the lgamma result. */
12124 : 674 : result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12125 : : result_sg, result_lg));
12126 : : }
12127 : 674 : }
12128 : : }
12129 : :
12130 : 6720 : return result;
12131 : : }
12132 : :
12133 : : /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12134 : : mpc function FUNC on it and return the resulting value as a tree
12135 : : with type TYPE. The mpfr precision is set to the precision of
12136 : : TYPE. We assume that function FUNC returns zero if the result
12137 : : could be calculated exactly within the requested precision. If
12138 : : DO_NONFINITE is true, then fold expressions containing Inf or NaN
12139 : : in the arguments and/or results. */
12140 : :
12141 : : tree
12142 : 4482 : do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12143 : : int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12144 : : {
12145 : 4482 : tree result = NULL_TREE;
12146 : :
12147 : 4482 : STRIP_NOPS (arg0);
12148 : 4482 : STRIP_NOPS (arg1);
12149 : :
12150 : : /* To proceed, MPFR must exactly represent the target floating point
12151 : : format, which only happens when the target base equals two. */
12152 : 4482 : if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12153 : 4482 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
12154 : 4482 : && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12155 : 4482 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
12156 : 8964 : && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12157 : : {
12158 : 4482 : const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12159 : 4482 : const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12160 : 4482 : const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12161 : 4482 : const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12162 : :
12163 : 4482 : if (do_nonfinite
12164 : 4482 : || (real_isfinite (re0) && real_isfinite (im0)
12165 : 3991 : && real_isfinite (re1) && real_isfinite (im1)))
12166 : : {
12167 : 4130 : const struct real_format *const fmt =
12168 : 4130 : REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12169 : 4130 : const int prec = fmt->p;
12170 : 4130 : const mpfr_rnd_t rnd = fmt->round_towards_zero
12171 : 4130 : ? MPFR_RNDZ : MPFR_RNDN;
12172 : 4130 : const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12173 : 4130 : int inexact;
12174 : 4130 : mpc_t m0, m1;
12175 : :
12176 : 4130 : mpc_init2 (m0, prec);
12177 : 4130 : mpc_init2 (m1, prec);
12178 : 4130 : mpfr_from_real (mpc_realref (m0), re0, rnd);
12179 : 4130 : mpfr_from_real (mpc_imagref (m0), im0, rnd);
12180 : 4130 : mpfr_from_real (mpc_realref (m1), re1, rnd);
12181 : 4130 : mpfr_from_real (mpc_imagref (m1), im1, rnd);
12182 : 4130 : mpfr_clear_flags ();
12183 : 4130 : inexact = func (m0, m0, m1, crnd);
12184 : 4130 : result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12185 : 4130 : mpc_clear (m0);
12186 : 4130 : mpc_clear (m1);
12187 : : }
12188 : : }
12189 : :
12190 : 4482 : return result;
12191 : : }
12192 : :
12193 : : /* A wrapper function for builtin folding that prevents warnings for
12194 : : "statement without effect" and the like, caused by removing the
12195 : : call node earlier than the warning is generated. */
12196 : :
12197 : : tree
12198 : 10277689 : fold_call_stmt (gcall *stmt, bool ignore)
12199 : : {
12200 : 10277689 : tree ret = NULL_TREE;
12201 : 10277689 : tree fndecl = gimple_call_fndecl (stmt);
12202 : 10277689 : location_t loc = gimple_location (stmt);
12203 : 10277689 : if (fndecl && fndecl_built_in_p (fndecl)
12204 : 20555378 : && !gimple_call_va_arg_pack_p (stmt))
12205 : : {
12206 : 10277623 : int nargs = gimple_call_num_args (stmt);
12207 : 10277623 : tree *args = (nargs > 0
12208 : 10277623 : ? gimple_call_arg_ptr (stmt, 0)
12209 : : : &error_mark_node);
12210 : :
12211 : 10277623 : if (avoid_folding_inline_builtin (fndecl))
12212 : : return NULL_TREE;
12213 : 10277623 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12214 : : {
12215 : 0 : return targetm.fold_builtin (fndecl, nargs, args, ignore);
12216 : : }
12217 : : else
12218 : : {
12219 : 10277623 : ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
12220 : 10277623 : if (ret)
12221 : : {
12222 : : /* Propagate location information from original call to
12223 : : expansion of builtin. Otherwise things like
12224 : : maybe_emit_chk_warning, that operate on the expansion
12225 : : of a builtin, will use the wrong location information. */
12226 : 5714 : if (gimple_has_location (stmt))
12227 : : {
12228 : 5713 : tree realret = ret;
12229 : 5713 : if (TREE_CODE (ret) == NOP_EXPR)
12230 : 5713 : realret = TREE_OPERAND (ret, 0);
12231 : 5713 : if (CAN_HAVE_LOCATION_P (realret)
12232 : 9999 : && !EXPR_HAS_LOCATION (realret))
12233 : 5 : SET_EXPR_LOCATION (realret, loc);
12234 : 5713 : return realret;
12235 : : }
12236 : : return ret;
12237 : : }
12238 : : }
12239 : : }
12240 : : return NULL_TREE;
12241 : : }
12242 : :
12243 : : /* Look up the function in builtin_decl that corresponds to DECL
12244 : : and set ASMSPEC as its user assembler name. DECL must be a
12245 : : function decl that declares a builtin. */
12246 : :
12247 : : void
12248 : 148504 : set_builtin_user_assembler_name (tree decl, const char *asmspec)
12249 : : {
12250 : 148504 : gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
12251 : : && asmspec != 0);
12252 : :
12253 : 148504 : tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12254 : 148504 : set_user_assembler_name (builtin, asmspec);
12255 : :
12256 : 148504 : if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
12257 : 148504 : && INT_TYPE_SIZE < BITS_PER_WORD)
12258 : : {
12259 : 1 : scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
12260 : 1 : set_user_assembler_libfunc ("ffs", asmspec);
12261 : 1 : set_optab_libfunc (ffs_optab, mode, "ffs");
12262 : : }
12263 : 148504 : }
12264 : :
12265 : : /* Return true if DECL is a builtin that expands to a constant or similarly
12266 : : simple code. */
12267 : : bool
12268 : 31699631 : is_simple_builtin (tree decl)
12269 : : {
12270 : 31699631 : if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
12271 : 30453527 : switch (DECL_FUNCTION_CODE (decl))
12272 : : {
12273 : : /* Builtins that expand to constants. */
12274 : : case BUILT_IN_CONSTANT_P:
12275 : : case BUILT_IN_EXPECT:
12276 : : case BUILT_IN_OBJECT_SIZE:
12277 : : case BUILT_IN_UNREACHABLE:
12278 : : /* Simple register moves or loads from stack. */
12279 : : case BUILT_IN_ASSUME_ALIGNED:
12280 : : case BUILT_IN_RETURN_ADDRESS:
12281 : : case BUILT_IN_EXTRACT_RETURN_ADDR:
12282 : : case BUILT_IN_FROB_RETURN_ADDR:
12283 : : case BUILT_IN_RETURN:
12284 : : case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12285 : : case BUILT_IN_FRAME_ADDRESS:
12286 : : case BUILT_IN_VA_END:
12287 : : case BUILT_IN_STACK_SAVE:
12288 : : case BUILT_IN_STACK_RESTORE:
12289 : : case BUILT_IN_DWARF_CFA:
12290 : : /* Exception state returns or moves registers around. */
12291 : : case BUILT_IN_EH_FILTER:
12292 : : case BUILT_IN_EH_POINTER:
12293 : : case BUILT_IN_EH_COPY_VALUES:
12294 : : return true;
12295 : :
12296 : 27825207 : default:
12297 : 27825207 : return false;
12298 : : }
12299 : :
12300 : : return false;
12301 : : }
12302 : :
12303 : : /* Return true if DECL is a builtin that is not expensive, i.e., they are
12304 : : most probably expanded inline into reasonably simple code. This is a
12305 : : superset of is_simple_builtin. */
12306 : : bool
12307 : 19166175 : is_inexpensive_builtin (tree decl)
12308 : : {
12309 : 19166175 : if (!decl)
12310 : : return false;
12311 : 19150165 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12312 : : return true;
12313 : 17881793 : else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12314 : 15046820 : switch (DECL_FUNCTION_CODE (decl))
12315 : : {
12316 : : case BUILT_IN_ABS:
12317 : : CASE_BUILT_IN_ALLOCA:
12318 : : case BUILT_IN_BSWAP16:
12319 : : case BUILT_IN_BSWAP32:
12320 : : case BUILT_IN_BSWAP64:
12321 : : case BUILT_IN_BSWAP128:
12322 : : case BUILT_IN_CLZ:
12323 : : case BUILT_IN_CLZIMAX:
12324 : : case BUILT_IN_CLZL:
12325 : : case BUILT_IN_CLZLL:
12326 : : case BUILT_IN_CTZ:
12327 : : case BUILT_IN_CTZIMAX:
12328 : : case BUILT_IN_CTZL:
12329 : : case BUILT_IN_CTZLL:
12330 : : case BUILT_IN_FFS:
12331 : : case BUILT_IN_FFSIMAX:
12332 : : case BUILT_IN_FFSL:
12333 : : case BUILT_IN_FFSLL:
12334 : : case BUILT_IN_IMAXABS:
12335 : : case BUILT_IN_FINITE:
12336 : : case BUILT_IN_FINITEF:
12337 : : case BUILT_IN_FINITEL:
12338 : : case BUILT_IN_FINITED32:
12339 : : case BUILT_IN_FINITED64:
12340 : : case BUILT_IN_FINITED128:
12341 : : case BUILT_IN_FPCLASSIFY:
12342 : : case BUILT_IN_ISFINITE:
12343 : : case BUILT_IN_ISINF_SIGN:
12344 : : case BUILT_IN_ISINF:
12345 : : case BUILT_IN_ISINFF:
12346 : : case BUILT_IN_ISINFL:
12347 : : case BUILT_IN_ISINFD32:
12348 : : case BUILT_IN_ISINFD64:
12349 : : case BUILT_IN_ISINFD128:
12350 : : case BUILT_IN_ISNAN:
12351 : : case BUILT_IN_ISNANF:
12352 : : case BUILT_IN_ISNANL:
12353 : : case BUILT_IN_ISNAND32:
12354 : : case BUILT_IN_ISNAND64:
12355 : : case BUILT_IN_ISNAND128:
12356 : : case BUILT_IN_ISNORMAL:
12357 : : case BUILT_IN_ISGREATER:
12358 : : case BUILT_IN_ISGREATEREQUAL:
12359 : : case BUILT_IN_ISLESS:
12360 : : case BUILT_IN_ISLESSEQUAL:
12361 : : case BUILT_IN_ISLESSGREATER:
12362 : : case BUILT_IN_ISUNORDERED:
12363 : : case BUILT_IN_ISEQSIG:
12364 : : case BUILT_IN_VA_ARG_PACK:
12365 : : case BUILT_IN_VA_ARG_PACK_LEN:
12366 : : case BUILT_IN_VA_COPY:
12367 : : case BUILT_IN_TRAP:
12368 : : case BUILT_IN_UNREACHABLE_TRAP:
12369 : : case BUILT_IN_SAVEREGS:
12370 : : case BUILT_IN_POPCOUNTL:
12371 : : case BUILT_IN_POPCOUNTLL:
12372 : : case BUILT_IN_POPCOUNTIMAX:
12373 : : case BUILT_IN_POPCOUNT:
12374 : : case BUILT_IN_PARITYL:
12375 : : case BUILT_IN_PARITYLL:
12376 : : case BUILT_IN_PARITYIMAX:
12377 : : case BUILT_IN_PARITY:
12378 : : case BUILT_IN_LABS:
12379 : : case BUILT_IN_LLABS:
12380 : : case BUILT_IN_PREFETCH:
12381 : : case BUILT_IN_ACC_ON_DEVICE:
12382 : : return true;
12383 : :
12384 : 14287692 : default:
12385 : 14287692 : return is_simple_builtin (decl);
12386 : : }
12387 : :
12388 : : return false;
12389 : : }
12390 : :
12391 : : /* Return true if T is a constant and the value cast to a target char
12392 : : can be represented by a host char.
12393 : : Store the casted char constant in *P if so. */
12394 : :
12395 : : bool
12396 : 2741 : target_char_cst_p (tree t, char *p)
12397 : : {
12398 : 2741 : if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
12399 : : return false;
12400 : :
12401 : 1607 : *p = (char)tree_to_uhwi (t);
12402 : 1607 : return true;
12403 : : }
12404 : :
12405 : : /* Return true if the builtin DECL is implemented in a standard library.
12406 : : Otherwise return false which doesn't guarantee it is not (thus the list
12407 : : of handled builtins below may be incomplete). */
12408 : :
12409 : : bool
12410 : 40464 : builtin_with_linkage_p (tree decl)
12411 : : {
12412 : 40464 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12413 : 40186 : switch (DECL_FUNCTION_CODE (decl))
12414 : : {
12415 : 1294 : CASE_FLT_FN (BUILT_IN_ACOS):
12416 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
12417 : 1294 : CASE_FLT_FN (BUILT_IN_ACOSH):
12418 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
12419 : 1294 : CASE_FLT_FN (BUILT_IN_ASIN):
12420 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
12421 : 1294 : CASE_FLT_FN (BUILT_IN_ASINH):
12422 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
12423 : 1294 : CASE_FLT_FN (BUILT_IN_ATAN):
12424 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
12425 : 1294 : CASE_FLT_FN (BUILT_IN_ATANH):
12426 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
12427 : 1294 : CASE_FLT_FN (BUILT_IN_ATAN2):
12428 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
12429 : 1294 : CASE_FLT_FN (BUILT_IN_CBRT):
12430 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
12431 : 1294 : CASE_FLT_FN (BUILT_IN_CEIL):
12432 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
12433 : 1294 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
12434 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
12435 : 1294 : CASE_FLT_FN (BUILT_IN_COS):
12436 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
12437 : 1294 : CASE_FLT_FN (BUILT_IN_COSH):
12438 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
12439 : 1294 : CASE_FLT_FN (BUILT_IN_ERF):
12440 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
12441 : 1294 : CASE_FLT_FN (BUILT_IN_ERFC):
12442 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
12443 : 1294 : CASE_FLT_FN (BUILT_IN_EXP):
12444 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
12445 : 1294 : CASE_FLT_FN (BUILT_IN_EXP2):
12446 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
12447 : 1294 : CASE_FLT_FN (BUILT_IN_EXPM1):
12448 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
12449 : 1294 : CASE_FLT_FN (BUILT_IN_FABS):
12450 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
12451 : 1294 : CASE_FLT_FN (BUILT_IN_FDIM):
12452 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
12453 : 1294 : CASE_FLT_FN (BUILT_IN_FLOOR):
12454 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
12455 : 1294 : CASE_FLT_FN (BUILT_IN_FMA):
12456 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
12457 : 1294 : CASE_FLT_FN (BUILT_IN_FMAX):
12458 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
12459 : 1294 : CASE_FLT_FN (BUILT_IN_FMIN):
12460 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
12461 : 1294 : CASE_FLT_FN (BUILT_IN_FMOD):
12462 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
12463 : 1294 : CASE_FLT_FN (BUILT_IN_FREXP):
12464 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
12465 : 1294 : CASE_FLT_FN (BUILT_IN_HYPOT):
12466 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
12467 : 1294 : CASE_FLT_FN (BUILT_IN_ILOGB):
12468 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
12469 : 1294 : CASE_FLT_FN (BUILT_IN_LDEXP):
12470 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
12471 : 1294 : CASE_FLT_FN (BUILT_IN_LGAMMA):
12472 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
12473 : 1294 : CASE_FLT_FN (BUILT_IN_LLRINT):
12474 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
12475 : 1294 : CASE_FLT_FN (BUILT_IN_LLROUND):
12476 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
12477 : 1294 : CASE_FLT_FN (BUILT_IN_LOG):
12478 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
12479 : 1294 : CASE_FLT_FN (BUILT_IN_LOG10):
12480 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
12481 : 1294 : CASE_FLT_FN (BUILT_IN_LOG1P):
12482 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
12483 : 1294 : CASE_FLT_FN (BUILT_IN_LOG2):
12484 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
12485 : 1294 : CASE_FLT_FN (BUILT_IN_LOGB):
12486 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
12487 : 1294 : CASE_FLT_FN (BUILT_IN_LRINT):
12488 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
12489 : 1294 : CASE_FLT_FN (BUILT_IN_LROUND):
12490 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
12491 : 1294 : CASE_FLT_FN (BUILT_IN_MODF):
12492 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
12493 : 1294 : CASE_FLT_FN (BUILT_IN_NAN):
12494 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
12495 : 1294 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
12496 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
12497 : 1294 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
12498 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
12499 : 1294 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
12500 : 1294 : CASE_FLT_FN (BUILT_IN_POW):
12501 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
12502 : 1294 : CASE_FLT_FN (BUILT_IN_REMAINDER):
12503 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
12504 : 1294 : CASE_FLT_FN (BUILT_IN_REMQUO):
12505 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
12506 : 1294 : CASE_FLT_FN (BUILT_IN_RINT):
12507 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
12508 : 1294 : CASE_FLT_FN (BUILT_IN_ROUND):
12509 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
12510 : 1294 : CASE_FLT_FN (BUILT_IN_SCALBLN):
12511 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
12512 : 1294 : CASE_FLT_FN (BUILT_IN_SCALBN):
12513 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
12514 : 1294 : CASE_FLT_FN (BUILT_IN_SIN):
12515 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
12516 : 1294 : CASE_FLT_FN (BUILT_IN_SINH):
12517 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
12518 : 1294 : CASE_FLT_FN (BUILT_IN_SINCOS):
12519 : 1294 : CASE_FLT_FN (BUILT_IN_SQRT):
12520 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
12521 : 1294 : CASE_FLT_FN (BUILT_IN_TAN):
12522 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
12523 : 1294 : CASE_FLT_FN (BUILT_IN_TANH):
12524 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
12525 : 1294 : CASE_FLT_FN (BUILT_IN_TGAMMA):
12526 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
12527 : 1294 : CASE_FLT_FN (BUILT_IN_TRUNC):
12528 : 1294 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
12529 : 1294 : return true;
12530 : :
12531 : 16 : case BUILT_IN_STPCPY:
12532 : 16 : case BUILT_IN_STPNCPY:
12533 : : /* stpcpy is both referenced in libiberty's pex-win32.c and provided
12534 : : by libiberty's stpcpy.c for MinGW targets so we need to return true
12535 : : in order to be able to build libiberty in LTO mode for them. */
12536 : 16 : return true;
12537 : :
12538 : : default:
12539 : : break;
12540 : : }
12541 : : return false;
12542 : : }
12543 : :
12544 : : /* Return true if OFFRNG is bounded to a subrange of offset values
12545 : : valid for the largest possible object. */
12546 : :
12547 : : bool
12548 : 501 : access_ref::offset_bounded () const
12549 : : {
12550 : 501 : tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
12551 : 501 : tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
12552 : 975 : return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
12553 : : }
12554 : :
12555 : : /* If CALLEE has known side effects, fill in INFO and return true.
12556 : : See tree-ssa-structalias.cc:find_func_aliases
12557 : : for the list of builtins we might need to handle here. */
12558 : :
12559 : : attr_fnspec
12560 : 108381794 : builtin_fnspec (tree callee)
12561 : : {
12562 : 108381794 : built_in_function code = DECL_FUNCTION_CODE (callee);
12563 : :
12564 : 108381794 : switch (code)
12565 : : {
12566 : : /* All the following functions read memory pointed to by
12567 : : their second argument and write memory pointed to by first
12568 : : argument.
12569 : : strcat/strncat additionally reads memory pointed to by the first
12570 : : argument. */
12571 : 133260 : case BUILT_IN_STRCAT:
12572 : 133260 : case BUILT_IN_STRCAT_CHK:
12573 : 133260 : return "1cW 1 ";
12574 : 42638 : case BUILT_IN_STRNCAT:
12575 : 42638 : case BUILT_IN_STRNCAT_CHK:
12576 : 42638 : return "1cW 13";
12577 : 261467 : case BUILT_IN_STRCPY:
12578 : 261467 : case BUILT_IN_STRCPY_CHK:
12579 : 261467 : return "1cO 1 ";
12580 : 32303 : case BUILT_IN_STPCPY:
12581 : 32303 : case BUILT_IN_STPCPY_CHK:
12582 : 32303 : return ".cO 1 ";
12583 : 18527392 : case BUILT_IN_STRNCPY:
12584 : 18527392 : case BUILT_IN_MEMCPY:
12585 : 18527392 : case BUILT_IN_MEMMOVE:
12586 : 18527392 : case BUILT_IN_TM_MEMCPY:
12587 : 18527392 : case BUILT_IN_TM_MEMMOVE:
12588 : 18527392 : case BUILT_IN_STRNCPY_CHK:
12589 : 18527392 : case BUILT_IN_MEMCPY_CHK:
12590 : 18527392 : case BUILT_IN_MEMMOVE_CHK:
12591 : 18527392 : return "1cO313";
12592 : 152489 : case BUILT_IN_MEMPCPY:
12593 : 152489 : case BUILT_IN_MEMPCPY_CHK:
12594 : 152489 : return ".cO313";
12595 : 57322 : case BUILT_IN_STPNCPY:
12596 : 57322 : case BUILT_IN_STPNCPY_CHK:
12597 : 57322 : return ".cO313";
12598 : 0 : case BUILT_IN_BCOPY:
12599 : 0 : return ".c23O3";
12600 : 0 : case BUILT_IN_BZERO:
12601 : 0 : return ".cO2";
12602 : 8566337 : case BUILT_IN_MEMCMP:
12603 : 8566337 : case BUILT_IN_MEMCMP_EQ:
12604 : 8566337 : case BUILT_IN_BCMP:
12605 : 8566337 : case BUILT_IN_STRNCMP:
12606 : 8566337 : case BUILT_IN_STRNCMP_EQ:
12607 : 8566337 : case BUILT_IN_STRNCASECMP:
12608 : 8566337 : return ".cR3R3";
12609 : :
12610 : : /* The following functions read memory pointed to by their
12611 : : first argument. */
12612 : 774 : CASE_BUILT_IN_TM_LOAD (1):
12613 : 774 : CASE_BUILT_IN_TM_LOAD (2):
12614 : 774 : CASE_BUILT_IN_TM_LOAD (4):
12615 : 774 : CASE_BUILT_IN_TM_LOAD (8):
12616 : 774 : CASE_BUILT_IN_TM_LOAD (FLOAT):
12617 : 774 : CASE_BUILT_IN_TM_LOAD (DOUBLE):
12618 : 774 : CASE_BUILT_IN_TM_LOAD (LDOUBLE):
12619 : 774 : CASE_BUILT_IN_TM_LOAD (M64):
12620 : 774 : CASE_BUILT_IN_TM_LOAD (M128):
12621 : 774 : CASE_BUILT_IN_TM_LOAD (M256):
12622 : 774 : case BUILT_IN_TM_LOG:
12623 : 774 : case BUILT_IN_TM_LOG_1:
12624 : 774 : case BUILT_IN_TM_LOG_2:
12625 : 774 : case BUILT_IN_TM_LOG_4:
12626 : 774 : case BUILT_IN_TM_LOG_8:
12627 : 774 : case BUILT_IN_TM_LOG_FLOAT:
12628 : 774 : case BUILT_IN_TM_LOG_DOUBLE:
12629 : 774 : case BUILT_IN_TM_LOG_LDOUBLE:
12630 : 774 : case BUILT_IN_TM_LOG_M64:
12631 : 774 : case BUILT_IN_TM_LOG_M128:
12632 : 774 : case BUILT_IN_TM_LOG_M256:
12633 : 774 : return ".cR ";
12634 : :
12635 : 478994 : case BUILT_IN_INDEX:
12636 : 478994 : case BUILT_IN_RINDEX:
12637 : 478994 : case BUILT_IN_STRCHR:
12638 : 478994 : case BUILT_IN_STRLEN:
12639 : 478994 : case BUILT_IN_STRRCHR:
12640 : 478994 : return ".cR ";
12641 : 62550 : case BUILT_IN_STRNLEN:
12642 : 62550 : return ".cR2";
12643 : :
12644 : : /* These read memory pointed to by the first argument.
12645 : : Allocating memory does not have any side-effects apart from
12646 : : being the definition point for the pointer.
12647 : : Unix98 specifies that errno is set on allocation failure. */
12648 : 17703 : case BUILT_IN_STRDUP:
12649 : 17703 : return "mCR ";
12650 : 13312 : case BUILT_IN_STRNDUP:
12651 : 13312 : return "mCR2";
12652 : : /* Allocating memory does not have any side-effects apart from
12653 : : being the definition point for the pointer. */
12654 : 9346238 : case BUILT_IN_MALLOC:
12655 : 9346238 : case BUILT_IN_ALIGNED_ALLOC:
12656 : 9346238 : case BUILT_IN_CALLOC:
12657 : 9346238 : case BUILT_IN_GOMP_ALLOC:
12658 : 9346238 : return "mC";
12659 : 851377 : CASE_BUILT_IN_ALLOCA:
12660 : 851377 : return "mc";
12661 : : /* These read memory pointed to by the first argument with size
12662 : : in the third argument. */
12663 : 480577 : case BUILT_IN_MEMCHR:
12664 : 480577 : return ".cR3";
12665 : : /* These read memory pointed to by the first and second arguments. */
12666 : 11119906 : case BUILT_IN_STRSTR:
12667 : 11119906 : case BUILT_IN_STRPBRK:
12668 : 11119906 : case BUILT_IN_STRCASECMP:
12669 : 11119906 : case BUILT_IN_STRCSPN:
12670 : 11119906 : case BUILT_IN_STRSPN:
12671 : 11119906 : case BUILT_IN_STRCMP:
12672 : 11119906 : case BUILT_IN_STRCMP_EQ:
12673 : 11119906 : return ".cR R ";
12674 : : /* Freeing memory kills the pointed-to memory. More importantly
12675 : : the call has to serve as a barrier for moving loads and stores
12676 : : across it. */
12677 : 5535500 : case BUILT_IN_STACK_RESTORE:
12678 : 5535500 : case BUILT_IN_FREE:
12679 : 5535500 : case BUILT_IN_GOMP_FREE:
12680 : 5535500 : return ".co ";
12681 : 92689 : case BUILT_IN_VA_END:
12682 : 92689 : return ".cO ";
12683 : : /* Realloc serves both as allocation point and deallocation point. */
12684 : 975141 : case BUILT_IN_REALLOC:
12685 : 975141 : case BUILT_IN_GOMP_REALLOC:
12686 : 975141 : return ".Cw ";
12687 : 15516 : case BUILT_IN_GAMMA_R:
12688 : 15516 : case BUILT_IN_GAMMAF_R:
12689 : 15516 : case BUILT_IN_GAMMAL_R:
12690 : 15516 : case BUILT_IN_LGAMMA_R:
12691 : 15516 : case BUILT_IN_LGAMMAF_R:
12692 : 15516 : case BUILT_IN_LGAMMAL_R:
12693 : 15516 : return ".C. Ot";
12694 : 79305 : case BUILT_IN_FREXP:
12695 : 79305 : case BUILT_IN_FREXPF:
12696 : 79305 : case BUILT_IN_FREXPL:
12697 : 79305 : case BUILT_IN_MODF:
12698 : 79305 : case BUILT_IN_MODFF:
12699 : 79305 : case BUILT_IN_MODFL:
12700 : 79305 : return ".c. Ot";
12701 : 7316 : case BUILT_IN_REMQUO:
12702 : 7316 : case BUILT_IN_REMQUOF:
12703 : 7316 : case BUILT_IN_REMQUOL:
12704 : 7316 : return ".c. . Ot";
12705 : 151 : case BUILT_IN_SINCOS:
12706 : 151 : case BUILT_IN_SINCOSF:
12707 : 151 : case BUILT_IN_SINCOSL:
12708 : 151 : return ".c. OtOt";
12709 : 4166520 : case BUILT_IN_MEMSET:
12710 : 4166520 : case BUILT_IN_MEMSET_CHK:
12711 : 4166520 : case BUILT_IN_TM_MEMSET:
12712 : 4166520 : return "1cO3";
12713 : 365 : CASE_BUILT_IN_TM_STORE (1):
12714 : 365 : CASE_BUILT_IN_TM_STORE (2):
12715 : 365 : CASE_BUILT_IN_TM_STORE (4):
12716 : 365 : CASE_BUILT_IN_TM_STORE (8):
12717 : 365 : CASE_BUILT_IN_TM_STORE (FLOAT):
12718 : 365 : CASE_BUILT_IN_TM_STORE (DOUBLE):
12719 : 365 : CASE_BUILT_IN_TM_STORE (LDOUBLE):
12720 : 365 : CASE_BUILT_IN_TM_STORE (M64):
12721 : 365 : CASE_BUILT_IN_TM_STORE (M128):
12722 : 365 : CASE_BUILT_IN_TM_STORE (M256):
12723 : 365 : return ".cO ";
12724 : 2319164 : case BUILT_IN_STACK_SAVE:
12725 : 2319164 : case BUILT_IN_RETURN:
12726 : 2319164 : case BUILT_IN_EH_POINTER:
12727 : 2319164 : case BUILT_IN_EH_FILTER:
12728 : 2319164 : case BUILT_IN_UNWIND_RESUME:
12729 : 2319164 : case BUILT_IN_CXA_END_CLEANUP:
12730 : 2319164 : case BUILT_IN_EH_COPY_VALUES:
12731 : 2319164 : case BUILT_IN_FRAME_ADDRESS:
12732 : 2319164 : case BUILT_IN_APPLY_ARGS:
12733 : 2319164 : case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
12734 : 2319164 : case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
12735 : 2319164 : case BUILT_IN_PREFETCH:
12736 : 2319164 : case BUILT_IN_DWARF_CFA:
12737 : 2319164 : case BUILT_IN_RETURN_ADDRESS:
12738 : 2319164 : return ".c";
12739 : 1682257 : case BUILT_IN_ASSUME_ALIGNED:
12740 : 1682257 : case BUILT_IN_EXPECT:
12741 : 1682257 : case BUILT_IN_EXPECT_WITH_PROBABILITY:
12742 : 1682257 : return "1cX ";
12743 : : /* But posix_memalign stores a pointer into the memory pointed to
12744 : : by its first argument. */
12745 : 5573 : case BUILT_IN_POSIX_MEMALIGN:
12746 : 5573 : return ".cOt";
12747 : 4438 : case BUILT_IN_OMP_GET_MAPPED_PTR:
12748 : 4438 : return ". R ";
12749 : :
12750 : 43353220 : default:
12751 : 43353220 : return "";
12752 : : }
12753 : : }
|