1 /* Expand builtin functions.
2 Copyright (C) 1988-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.c instead. */
26 #include "coretypes.h"
35 #include "stringpool.h"
37 #include "tree-ssanames.h"
42 #include "diagnostic-core.h"
44 #include "fold-const.h"
45 #include "fold-const-call.h"
46 #include "gimple-ssa-warn-restrict.h"
47 #include "stor-layout.h"
50 #include "tree-object-size.h"
51 #include "tree-ssa-strlen.h"
61 #include "typeclass.h"
62 #include "langhooks.h"
63 #include "value-prof.h"
65 #include "stringpool.h"
68 #include "internal-fn.h"
69 #include "case-cfn-macros.h"
70 #include "gimple-fold.h"
72 #include "file-prefix-map.h" /* remap_macro_filename() */
73 #include "gomp-constants.h"
74 #include "omp-general.h"
76 #include "gimple-iterator.h"
77 #include "gimple-ssa.h"
78 #include "tree-ssa-live.h"
79 #include "tree-outof-ssa.h"
80 #include "attr-fnspec.h"
82 #include "gimple-range.h"
84 struct target_builtins default_target_builtins
;
86 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
89 /* Define the names of the builtin function types and codes. */
90 const char *const built_in_class_names
[BUILT_IN_LAST
]
91 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
93 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
94 const char * built_in_names
[(int) END_BUILTINS
] =
96 #include "builtins.def"
99 /* Setup an array of builtin_info_type, make sure each element decl is
100 initialized to NULL_TREE. */
101 builtin_info_type builtin_info
[(int)END_BUILTINS
];
103 /* Non-zero if __builtin_constant_p should be folded right away. */
104 bool force_folding_builtin_constant_p
;
106 static int target_char_cast (tree
, char *);
107 static rtx
get_memory_rtx (tree
, tree
);
108 static int apply_args_size (void);
109 static int apply_result_size (void);
110 static rtx
result_vector (int, rtx
);
111 static void expand_builtin_prefetch (tree
);
112 static rtx
expand_builtin_apply_args (void);
113 static rtx
expand_builtin_apply_args_1 (void);
114 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
115 static void expand_builtin_return (rtx
);
116 static enum type_class
type_to_class (tree
);
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_cexpi (tree
, rtx
);
123 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
124 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
125 static rtx
expand_builtin_next_arg (void);
126 static rtx
expand_builtin_va_start (tree
);
127 static rtx
expand_builtin_va_end (tree
);
128 static rtx
expand_builtin_va_copy (tree
);
129 static rtx
inline_expand_builtin_bytecmp (tree
, rtx
);
130 static rtx
expand_builtin_strcmp (tree
, rtx
);
131 static rtx
expand_builtin_strncmp (tree
, rtx
, machine_mode
);
132 static rtx
expand_builtin_memchr (tree
, rtx
);
133 static rtx
expand_builtin_memcpy (tree
, rtx
);
134 static rtx
expand_builtin_memory_copy_args (tree dest
, tree src
, tree len
,
135 rtx target
, tree exp
,
138 static rtx
expand_builtin_memmove (tree
, rtx
);
139 static rtx
expand_builtin_mempcpy (tree
, rtx
);
140 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
, tree
, memop_ret
);
141 static rtx
expand_builtin_strcat (tree
);
142 static rtx
expand_builtin_strcpy (tree
, rtx
);
143 static rtx
expand_builtin_strcpy_args (tree
, tree
, tree
, rtx
);
144 static rtx
expand_builtin_stpcpy (tree
, rtx
, machine_mode
);
145 static rtx
expand_builtin_stpncpy (tree
, rtx
);
146 static rtx
expand_builtin_strncat (tree
, rtx
);
147 static rtx
expand_builtin_strncpy (tree
, rtx
);
148 static rtx
expand_builtin_memset (tree
, rtx
, machine_mode
);
149 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, machine_mode
, tree
);
150 static rtx
expand_builtin_bzero (tree
);
151 static rtx
expand_builtin_strlen (tree
, rtx
, machine_mode
);
152 static rtx
expand_builtin_strnlen (tree
, rtx
, machine_mode
);
153 static rtx
expand_builtin_alloca (tree
);
154 static rtx
expand_builtin_unop (machine_mode
, tree
, rtx
, rtx
, optab
);
155 static rtx
expand_builtin_frame_address (tree
, tree
);
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
,
175 static tree
fold_builtin_varargs (location_t
, tree
, tree
*, int);
177 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
, tree
);
178 static tree
fold_builtin_strspn (location_t
, tree
, tree
, tree
);
179 static tree
fold_builtin_strcspn (location_t
, tree
, tree
, tree
);
181 static rtx
expand_builtin_object_size (tree
);
182 static rtx
expand_builtin_memory_chk (tree
, rtx
, machine_mode
,
183 enum built_in_function
);
184 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
185 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
186 static tree
fold_builtin_object_size (tree
, tree
);
187 static bool check_read_access (tree
, tree
, tree
= NULL_TREE
, int = 1);
188 static bool compute_objsize_r (tree
, int, access_ref
*, ssa_name_limit_t
&,
191 unsigned HOST_WIDE_INT target_newline
;
192 unsigned HOST_WIDE_INT target_percent
;
193 static unsigned HOST_WIDE_INT target_c
;
194 static unsigned HOST_WIDE_INT target_s
;
195 char target_percent_c
[3];
196 char target_percent_s
[3];
197 char target_percent_s_newline
[4];
198 static tree
do_mpfr_remquo (tree
, tree
, tree
);
199 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
200 static void expand_builtin_sync_synchronize (void);
202 access_ref::access_ref (tree bound
/* = NULL_TREE */,
203 bool minaccess
/* = false */)
204 : ref (), eval ([](tree x
){ return x
; }), deref (), trail1special (true),
205 base0 (true), parmarray ()
208 offrng
[0] = offrng
[1] = 0;
209 offmax
[0] = offmax
[1] = 0;
211 sizrng
[0] = sizrng
[1] = -1;
213 /* Set the default bounds of the access and adjust below. */
214 bndrng
[0] = minaccess
? 1 : 0;
215 bndrng
[1] = HOST_WIDE_INT_M1U
;
217 /* When BOUND is nonnull and a range can be extracted from it,
218 set the bounds of the access to reflect both it and MINACCESS.
219 BNDRNG[0] is the size of the minimum access. */
221 if (bound
&& get_size_range (bound
, rng
, SR_ALLOW_ZERO
))
223 bndrng
[0] = wi::to_offset (rng
[0]);
224 bndrng
[1] = wi::to_offset (rng
[1]);
225 bndrng
[0] = bndrng
[0] > 0 && minaccess
? 1 : 0;
229 /* Return the PHI node REF refers to or null if it doesn't. */
232 access_ref::phi () const
234 if (!ref
|| TREE_CODE (ref
) != SSA_NAME
)
237 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
);
238 if (gimple_code (def_stmt
) != GIMPLE_PHI
)
241 return as_a
<gphi
*> (def_stmt
);
244 /* Determine and return the largest object to which *THIS. If *THIS
245 refers to a PHI and PREF is nonnull, fill *PREF with the details
246 of the object determined by compute_objsize(ARG, OSTYPE) for each
250 access_ref::get_ref (vec
<access_ref
> *all_refs
,
251 access_ref
*pref
/* = NULL */,
252 int ostype
/* = 1 */,
253 ssa_name_limit_t
*psnlim
/* = NULL */,
254 pointer_query
*qry
/* = NULL */) const
256 gphi
*phi_stmt
= this->phi ();
260 /* FIXME: Calling get_ref() with a null PSNLIM is dangerous and might
261 cause unbounded recursion. */
262 ssa_name_limit_t snlim_buf
;
266 if (!psnlim
->visit_phi (ref
))
269 /* Reflects the range of offsets of all PHI arguments refer to the same
270 object (i.e., have the same REF). */
272 /* The conservative result of the PHI reflecting the offset and size
273 of the largest PHI argument, regardless of whether or not they all
274 refer to the same object. */
275 pointer_query empty_qry
;
286 /* Set if any argument is a function array (or VLA) parameter not
287 declared [static]. */
288 bool parmarray
= false;
289 /* The size of the smallest object referenced by the PHI arguments. */
290 offset_int minsize
= 0;
291 const offset_int maxobjsize
= wi::to_offset (max_object_size ());
292 /* The offset of the PHI, not reflecting those of its arguments. */
293 const offset_int orng
[2] = { phi_ref
.offrng
[0], phi_ref
.offrng
[1] };
295 const unsigned nargs
= gimple_phi_num_args (phi_stmt
);
296 for (unsigned i
= 0; i
< nargs
; ++i
)
298 access_ref phi_arg_ref
;
299 tree arg
= gimple_phi_arg_def (phi_stmt
, i
);
300 if (!compute_objsize_r (arg
, ostype
, &phi_arg_ref
, *psnlim
, qry
)
301 || phi_arg_ref
.sizrng
[0] < 0)
302 /* A PHI with all null pointer arguments. */
305 /* Add PREF's offset to that of the argument. */
306 phi_arg_ref
.add_offset (orng
[0], orng
[1]);
307 if (TREE_CODE (arg
) == SSA_NAME
)
308 qry
->put_ref (arg
, phi_arg_ref
);
311 all_refs
->safe_push (phi_arg_ref
);
313 const bool arg_known_size
= (phi_arg_ref
.sizrng
[0] != 0
314 || phi_arg_ref
.sizrng
[1] != maxobjsize
);
316 parmarray
|= phi_arg_ref
.parmarray
;
318 const bool nullp
= integer_zerop (arg
) && (i
|| i
+ 1 < nargs
);
320 if (phi_ref
.sizrng
[0] < 0)
323 same_ref
= phi_arg_ref
;
324 phi_ref
= phi_arg_ref
;
326 minsize
= phi_arg_ref
.sizrng
[0];
330 const bool phi_known_size
= (phi_ref
.sizrng
[0] != 0
331 || phi_ref
.sizrng
[1] != maxobjsize
);
333 if (phi_known_size
&& phi_arg_ref
.sizrng
[0] < minsize
)
334 minsize
= phi_arg_ref
.sizrng
[0];
336 /* Disregard null pointers in PHIs with two or more arguments.
337 TODO: Handle this better! */
341 /* Determine the amount of remaining space in the argument. */
342 offset_int argrem
[2];
343 argrem
[1] = phi_arg_ref
.size_remaining (argrem
);
345 /* Determine the amount of remaining space computed so far and
346 if the remaining space in the argument is more use it instead. */
347 offset_int phirem
[2];
348 phirem
[1] = phi_ref
.size_remaining (phirem
);
350 if (phi_arg_ref
.ref
!= same_ref
.ref
)
351 same_ref
.ref
= NULL_TREE
;
353 if (phirem
[1] < argrem
[1]
354 || (phirem
[1] == argrem
[1]
355 && phi_ref
.sizrng
[1] < phi_arg_ref
.sizrng
[1]))
356 /* Use the argument with the most space remaining as the result,
357 or the larger one if the space is equal. */
358 phi_ref
= phi_arg_ref
;
360 /* Set SAME_REF.OFFRNG to the maximum range of all arguments. */
361 if (phi_arg_ref
.offrng
[0] < same_ref
.offrng
[0])
362 same_ref
.offrng
[0] = phi_arg_ref
.offrng
[0];
363 if (same_ref
.offrng
[1] < phi_arg_ref
.offrng
[1])
364 same_ref
.offrng
[1] = phi_arg_ref
.offrng
[1];
367 if (!same_ref
.ref
&& same_ref
.offrng
[0] != 0)
368 /* Clear BASE0 if not all the arguments refer to the same object and
369 if not all their offsets are zero-based. This allows the final
370 PHI offset to out of bounds for some arguments but not for others
371 (or negative even of all the arguments are BASE0), which is overly
373 phi_ref
.base0
= false;
379 /* Replace the lower bound of the largest argument with the size
380 of the smallest argument, and set PARMARRAY if any argument
382 phi_ref
.sizrng
[0] = minsize
;
383 phi_ref
.parmarray
= parmarray
;
386 if (phi_ref
.sizrng
[0] < 0)
388 /* Fail if none of the PHI's arguments resulted in updating PHI_REF
389 (perhaps because they have all been already visited by prior
391 psnlim
->leave_phi (ref
);
395 /* Avoid changing *THIS. */
396 if (pref
&& pref
!= this)
399 psnlim
->leave_phi (ref
);
404 /* Return the maximum amount of space remaining and if non-null, set
405 argument to the minimum. */
408 access_ref::size_remaining (offset_int
*pmin
/* = NULL */) const
414 /* add_offset() ensures the offset range isn't inverted. */
415 gcc_checking_assert (offrng
[0] <= offrng
[1]);
419 /* The offset into referenced object is zero-based (i.e., it's
420 not referenced by a pointer into middle of some unknown object). */
421 if (offrng
[0] < 0 && offrng
[1] < 0)
423 /* If the offset is negative the remaining size is zero. */
428 if (sizrng
[1] <= offrng
[0])
430 /* If the starting offset is greater than or equal to the upper
431 bound on the size of the object, the space remaining is zero.
432 As a special case, if it's equal, set *PMIN to -1 to let
433 the caller know the offset is valid and just past the end. */
434 *pmin
= sizrng
[1] == offrng
[0] ? -1 : 0;
438 /* Otherwise return the size minus the lower bound of the offset. */
439 offset_int or0
= offrng
[0] < 0 ? 0 : offrng
[0];
441 *pmin
= sizrng
[0] - or0
;
442 return sizrng
[1] - or0
;
445 /* The offset to the referenced object isn't zero-based (i.e., it may
446 refer to a byte other than the first. The size of such an object
447 is constrained only by the size of the address space (the result
448 of max_object_size()). */
449 if (sizrng
[1] <= offrng
[0])
455 offset_int or0
= offrng
[0] < 0 ? 0 : offrng
[0];
457 *pmin
= sizrng
[0] - or0
;
458 return sizrng
[1] - or0
;
461 /* Return true if the offset and object size are in range for SIZE. */
464 access_ref::offset_in_range (const offset_int
&size
) const
466 if (size_remaining () < size
)
470 return offmax
[0] >= 0 && offmax
[1] <= sizrng
[1];
472 offset_int maxoff
= wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node
));
473 return offmax
[0] > -maxoff
&& offmax
[1] < maxoff
;
476 /* Add the range [MIN, MAX] to the offset range. For known objects (with
477 zero-based offsets) at least one of whose offset's bounds is in range,
478 constrain the other (or both) to the bounds of the object (i.e., zero
479 and the upper bound of its size). This improves the quality of
482 void access_ref::add_offset (const offset_int
&min
, const offset_int
&max
)
486 /* To add an ordinary range just add it to the bounds. */
492 /* To add an inverted range to an offset to an unknown object
493 expand it to the maximum. */
499 /* To add an inverted range to an offset to an known object set
500 the upper bound to the maximum representable offset value
501 (which may be greater than MAX_OBJECT_SIZE).
502 The lower bound is either the sum of the current offset and
503 MIN when abs(MAX) is greater than the former, or zero otherwise.
504 Zero because then then inverted range includes the negative of
506 offset_int maxoff
= wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node
));
517 offset_int absmax
= wi::abs (max
);
518 if (offrng
[0] < absmax
)
521 /* Cap the lower bound at the upper (set to MAXOFF above)
522 to avoid inadvertently recreating an inverted range. */
523 if (offrng
[1] < offrng
[0])
524 offrng
[0] = offrng
[1];
530 /* Set the minimum and maximmum computed so far. */
531 if (offrng
[1] < 0 && offrng
[1] < offmax
[0])
532 offmax
[0] = offrng
[1];
533 if (offrng
[0] > 0 && offrng
[0] > offmax
[1])
534 offmax
[1] = offrng
[0];
539 /* When referencing a known object check to see if the offset computed
540 so far is in bounds... */
541 offset_int remrng
[2];
542 remrng
[1] = size_remaining (remrng
);
543 if (remrng
[1] > 0 || remrng
[0] < 0)
545 /* ...if so, constrain it so that neither bound exceeds the size of
546 the object. Out of bounds offsets are left unchanged, and, for
547 better or worse, become in bounds later. They should be detected
548 and diagnosed at the point they first become invalid by
552 if (offrng
[1] > sizrng
[1])
553 offrng
[1] = sizrng
[1];
557 /* Set a bit for the PHI in VISITED and return true if it wasn't
561 ssa_name_limit_t::visit_phi (tree ssa_name
)
564 visited
= BITMAP_ALLOC (NULL
);
566 /* Return false if SSA_NAME has already been visited. */
567 return bitmap_set_bit (visited
, SSA_NAME_VERSION (ssa_name
));
570 /* Clear a bit for the PHI in VISITED. */
573 ssa_name_limit_t::leave_phi (tree ssa_name
)
575 /* Return false if SSA_NAME has already been visited. */
576 bitmap_clear_bit (visited
, SSA_NAME_VERSION (ssa_name
));
579 /* Return false if the SSA_NAME chain length counter has reached
580 the limit, otherwise increment the counter and return true. */
583 ssa_name_limit_t::next ()
585 /* Return a negative value to let caller avoid recursing beyond
586 the specified limit. */
587 if (ssa_def_max
== 0)
594 /* If the SSA_NAME has already been "seen" return a positive value.
595 Otherwise add it to VISITED. If the SSA_NAME limit has been
596 reached, return a negative value. Otherwise return zero. */
599 ssa_name_limit_t::next_phi (tree ssa_name
)
602 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ssa_name
);
603 /* Return a positive value if the PHI has already been visited. */
604 if (gimple_code (def_stmt
) == GIMPLE_PHI
605 && !visit_phi (ssa_name
))
609 /* Return a negative value to let caller avoid recursing beyond
610 the specified limit. */
611 if (ssa_def_max
== 0)
619 ssa_name_limit_t::~ssa_name_limit_t ()
622 BITMAP_FREE (visited
);
625 /* Default ctor. Initialize object with pointers to the range_query
626 and cache_type instances to use or null. */
628 pointer_query::pointer_query (range_query
*qry
/* = NULL */,
629 cache_type
*cache
/* = NULL */)
630 : rvals (qry
), var_cache (cache
), hits (), misses (),
631 failures (), depth (), max_depth ()
636 /* Return a pointer to the cached access_ref instance for the SSA_NAME
637 PTR if it's there or null otherwise. */
640 pointer_query::get_ref (tree ptr
, int ostype
/* = 1 */) const
648 unsigned version
= SSA_NAME_VERSION (ptr
);
649 unsigned idx
= version
<< 1 | (ostype
& 1);
650 if (var_cache
->indices
.length () <= idx
)
656 unsigned cache_idx
= var_cache
->indices
[idx
];
657 if (var_cache
->access_refs
.length () <= cache_idx
)
663 access_ref
&cache_ref
= var_cache
->access_refs
[cache_idx
];
674 /* Retrieve the access_ref instance for a variable from the cache if it's
675 there or compute it and insert it into the cache if it's nonnonull. */
678 pointer_query::get_ref (tree ptr
, access_ref
*pref
, int ostype
/* = 1 */)
680 const unsigned version
681 = TREE_CODE (ptr
) == SSA_NAME
? SSA_NAME_VERSION (ptr
) : 0;
683 if (var_cache
&& version
)
685 unsigned idx
= version
<< 1 | (ostype
& 1);
686 if (idx
< var_cache
->indices
.length ())
688 unsigned cache_idx
= var_cache
->indices
[idx
] - 1;
689 if (cache_idx
< var_cache
->access_refs
.length ()
690 && var_cache
->access_refs
[cache_idx
].ref
)
693 *pref
= var_cache
->access_refs
[cache_idx
];
701 if (!compute_objsize (ptr
, ostype
, pref
, this))
710 /* Add a copy of the access_ref REF for the SSA_NAME to the cache if it's
714 pointer_query::put_ref (tree ptr
, const access_ref
&ref
, int ostype
/* = 1 */)
716 /* Only add populated/valid entries. */
717 if (!var_cache
|| !ref
.ref
|| ref
.sizrng
[0] < 0)
720 /* Add REF to the two-level cache. */
721 unsigned version
= SSA_NAME_VERSION (ptr
);
722 unsigned idx
= version
<< 1 | (ostype
& 1);
724 /* Grow INDICES if necessary. An index is valid if it's nonzero.
725 Its value minus one is the index into ACCESS_REFS. Not all
726 entries are valid. */
727 if (var_cache
->indices
.length () <= idx
)
728 var_cache
->indices
.safe_grow_cleared (idx
+ 1);
730 if (!var_cache
->indices
[idx
])
731 var_cache
->indices
[idx
] = var_cache
->access_refs
.length () + 1;
733 /* Grow ACCESS_REF cache if necessary. An entry is valid if its
734 REF member is nonnull. All entries except for the last two
735 are valid. Once nonnull, the REF value must stay unchanged. */
736 unsigned cache_idx
= var_cache
->indices
[idx
];
737 if (var_cache
->access_refs
.length () <= cache_idx
)
738 var_cache
->access_refs
.safe_grow_cleared (cache_idx
+ 1);
740 access_ref cache_ref
= var_cache
->access_refs
[cache_idx
- 1];
743 gcc_checking_assert (cache_ref
.ref
== ref
.ref
);
750 /* Flush the cache if it's nonnull. */
753 pointer_query::flush_cache ()
757 var_cache
->indices
.release ();
758 var_cache
->access_refs
.release ();
761 /* Return true if NAME starts with __builtin_ or __sync_. */
764 is_builtin_name (const char *name
)
766 return (startswith (name
, "__builtin_")
767 || startswith (name
, "__sync_")
768 || startswith (name
, "__atomic_"));
771 /* Return true if NODE should be considered for inline expansion regardless
772 of the optimization level. This means whenever a function is invoked with
773 its "internal" name, which normally contains the prefix "__builtin". */
776 called_as_built_in (tree node
)
778 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
779 we want the name used to call the function, not the name it
781 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
782 return is_builtin_name (name
);
785 /* Compute values M and N such that M divides (address of EXP - N) and such
786 that N < M. If these numbers can be determined, store M in alignp and N in
787 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
788 *alignp and any bit-offset to *bitposp.
790 Note that the address (and thus the alignment) computed here is based
791 on the address to which a symbol resolves, whereas DECL_ALIGN is based
792 on the address at which an object is actually located. These two
793 addresses are not always the same. For example, on ARM targets,
794 the address &foo of a Thumb function foo() has the lowest bit set,
795 whereas foo() itself starts on an even address.
797 If ADDR_P is true we are taking the address of the memory reference EXP
798 and thus cannot rely on the access taking place. */
801 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
802 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
804 poly_int64 bitsize
, bitpos
;
807 int unsignedp
, reversep
, volatilep
;
808 unsigned int align
= BITS_PER_UNIT
;
809 bool known_alignment
= false;
811 /* Get the innermost object and the constant (bitpos) and possibly
812 variable (offset) offset of the access. */
813 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
, &mode
,
814 &unsignedp
, &reversep
, &volatilep
);
816 /* Extract alignment information from the innermost object and
817 possibly adjust bitpos and offset. */
818 if (TREE_CODE (exp
) == FUNCTION_DECL
)
820 /* Function addresses can encode extra information besides their
821 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
822 allows the low bit to be used as a virtual bit, we know
823 that the address itself must be at least 2-byte aligned. */
824 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
825 align
= 2 * BITS_PER_UNIT
;
827 else if (TREE_CODE (exp
) == LABEL_DECL
)
829 else if (TREE_CODE (exp
) == CONST_DECL
)
831 /* The alignment of a CONST_DECL is determined by its initializer. */
832 exp
= DECL_INITIAL (exp
);
833 align
= TYPE_ALIGN (TREE_TYPE (exp
));
834 if (CONSTANT_CLASS_P (exp
))
835 align
= targetm
.constant_alignment (exp
, align
);
837 known_alignment
= true;
839 else if (DECL_P (exp
))
841 align
= DECL_ALIGN (exp
);
842 known_alignment
= true;
844 else if (TREE_CODE (exp
) == INDIRECT_REF
845 || TREE_CODE (exp
) == MEM_REF
846 || TREE_CODE (exp
) == TARGET_MEM_REF
)
848 tree addr
= TREE_OPERAND (exp
, 0);
850 unsigned HOST_WIDE_INT ptr_bitpos
;
851 unsigned HOST_WIDE_INT ptr_bitmask
= ~0;
853 /* If the address is explicitely aligned, handle that. */
854 if (TREE_CODE (addr
) == BIT_AND_EXPR
855 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
857 ptr_bitmask
= TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1));
858 ptr_bitmask
*= BITS_PER_UNIT
;
859 align
= least_bit_hwi (ptr_bitmask
);
860 addr
= TREE_OPERAND (addr
, 0);
864 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
865 align
= MAX (ptr_align
, align
);
867 /* Re-apply explicit alignment to the bitpos. */
868 ptr_bitpos
&= ptr_bitmask
;
870 /* The alignment of the pointer operand in a TARGET_MEM_REF
871 has to take the variable offset parts into account. */
872 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
876 unsigned HOST_WIDE_INT step
= 1;
878 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
879 align
= MIN (align
, least_bit_hwi (step
) * BITS_PER_UNIT
);
881 if (TMR_INDEX2 (exp
))
882 align
= BITS_PER_UNIT
;
883 known_alignment
= false;
886 /* When EXP is an actual memory reference then we can use
887 TYPE_ALIGN of a pointer indirection to derive alignment.
888 Do so only if get_pointer_alignment_1 did not reveal absolute
889 alignment knowledge and if using that alignment would
890 improve the situation. */
892 if (!addr_p
&& !known_alignment
893 && (talign
= min_align_of_type (TREE_TYPE (exp
)) * BITS_PER_UNIT
)
898 /* Else adjust bitpos accordingly. */
899 bitpos
+= ptr_bitpos
;
900 if (TREE_CODE (exp
) == MEM_REF
901 || TREE_CODE (exp
) == TARGET_MEM_REF
)
902 bitpos
+= mem_ref_offset (exp
).force_shwi () * BITS_PER_UNIT
;
905 else if (TREE_CODE (exp
) == STRING_CST
)
907 /* STRING_CST are the only constant objects we allow to be not
908 wrapped inside a CONST_DECL. */
909 align
= TYPE_ALIGN (TREE_TYPE (exp
));
910 if (CONSTANT_CLASS_P (exp
))
911 align
= targetm
.constant_alignment (exp
, align
);
913 known_alignment
= true;
916 /* If there is a non-constant offset part extract the maximum
917 alignment that can prevail. */
920 unsigned int trailing_zeros
= tree_ctz (offset
);
921 if (trailing_zeros
< HOST_BITS_PER_INT
)
923 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
925 align
= MIN (align
, inner
);
929 /* Account for the alignment of runtime coefficients, so that the constant
930 bitpos is guaranteed to be accurate. */
931 unsigned int alt_align
= ::known_alignment (bitpos
- bitpos
.coeffs
[0]);
932 if (alt_align
!= 0 && alt_align
< align
)
935 known_alignment
= false;
939 *bitposp
= bitpos
.coeffs
[0] & (align
- 1);
940 return known_alignment
;
943 /* For a memory reference expression EXP compute values M and N such that M
944 divides (&EXP - N) and such that N < M. If these numbers can be determined,
945 store M in alignp and N in *BITPOSP and return true. Otherwise return false
946 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
949 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
950 unsigned HOST_WIDE_INT
*bitposp
)
952 /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
954 if (TREE_CODE (exp
) == WITH_SIZE_EXPR
)
955 exp
= TREE_OPERAND (exp
, 0);
956 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
959 /* Return the alignment in bits of EXP, an object. */
962 get_object_alignment (tree exp
)
964 unsigned HOST_WIDE_INT bitpos
= 0;
967 get_object_alignment_1 (exp
, &align
, &bitpos
);
969 /* align and bitpos now specify known low bits of the pointer.
970 ptr & (align - 1) == bitpos. */
973 align
= least_bit_hwi (bitpos
);
977 /* For a pointer valued expression EXP compute values M and N such that M
978 divides (EXP - N) and such that N < M. If these numbers can be determined,
979 store M in alignp and N in *BITPOSP and return true. Return false if
980 the results are just a conservative approximation.
982 If EXP is not a pointer, false is returned too. */
985 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
986 unsigned HOST_WIDE_INT
*bitposp
)
990 if (TREE_CODE (exp
) == ADDR_EXPR
)
991 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
992 alignp
, bitposp
, true);
993 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
996 unsigned HOST_WIDE_INT bitpos
;
997 bool res
= get_pointer_alignment_1 (TREE_OPERAND (exp
, 0),
999 if (TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
1000 bitpos
+= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 1)) * BITS_PER_UNIT
;
1003 unsigned int trailing_zeros
= tree_ctz (TREE_OPERAND (exp
, 1));
1004 if (trailing_zeros
< HOST_BITS_PER_INT
)
1006 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
1008 align
= MIN (align
, inner
);
1012 *bitposp
= bitpos
& (align
- 1);
1015 else if (TREE_CODE (exp
) == SSA_NAME
1016 && POINTER_TYPE_P (TREE_TYPE (exp
)))
1018 unsigned int ptr_align
, ptr_misalign
;
1019 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
1021 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
1023 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
1024 *alignp
= ptr_align
* BITS_PER_UNIT
;
1025 /* Make sure to return a sensible alignment when the multiplication
1026 by BITS_PER_UNIT overflowed. */
1028 *alignp
= 1u << (HOST_BITS_PER_INT
- 1);
1029 /* We cannot really tell whether this result is an approximation. */
1035 *alignp
= BITS_PER_UNIT
;
1039 else if (TREE_CODE (exp
) == INTEGER_CST
)
1041 *alignp
= BIGGEST_ALIGNMENT
;
1042 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
1043 & (BIGGEST_ALIGNMENT
- 1));
1048 *alignp
= BITS_PER_UNIT
;
1052 /* Return the alignment in bits of EXP, a pointer valued expression.
1053 The alignment returned is, by default, the alignment of the thing that
1054 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
1056 Otherwise, look at the expression to see if we can do better, i.e., if the
1057 expression is actually pointing at an object whose alignment is tighter. */
1060 get_pointer_alignment (tree exp
)
1062 unsigned HOST_WIDE_INT bitpos
= 0;
1065 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
1067 /* align and bitpos now specify known low bits of the pointer.
1068 ptr & (align - 1) == bitpos. */
1071 align
= least_bit_hwi (bitpos
);
1076 /* Return the number of leading non-zero elements in the sequence
1077 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
1078 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
1081 string_length (const void *ptr
, unsigned eltsize
, unsigned maxelts
)
1083 gcc_checking_assert (eltsize
== 1 || eltsize
== 2 || eltsize
== 4);
1089 /* Optimize the common case of plain char. */
1090 for (n
= 0; n
< maxelts
; n
++)
1092 const char *elt
= (const char*) ptr
+ n
;
1099 for (n
= 0; n
< maxelts
; n
++)
1101 const char *elt
= (const char*) ptr
+ n
* eltsize
;
1102 if (!memcmp (elt
, "\0\0\0\0", eltsize
))
1109 /* For a call EXPR at LOC to a function FNAME that expects a string
1110 in the argument ARG, issue a diagnostic due to it being a called
1111 with an argument that is a character array with no terminating
1112 NUL. SIZE is the EXACT size of the array, and BNDRNG the number
1113 of characters in which the NUL is expected. Either EXPR or FNAME
1114 may be null but noth both. SIZE may be null when BNDRNG is null. */
1117 warn_string_no_nul (location_t loc
, tree expr
, const char *fname
,
1118 tree arg
, tree decl
, tree size
/* = NULL_TREE */,
1119 bool exact
/* = false */,
1120 const wide_int bndrng
[2] /* = NULL */)
1122 const opt_code opt
= OPT_Wstringop_overread
;
1123 if ((expr
&& warning_suppressed_p (expr
, opt
))
1124 || warning_suppressed_p (arg
, opt
))
1127 loc
= expansion_point_location_if_in_system_header (loc
);
1130 /* Format the bound range as a string to keep the nuber of messages
1136 if (bndrng
[0] == bndrng
[1])
1137 sprintf (bndstr
, "%llu", (unsigned long long) bndrng
[0].to_uhwi ());
1139 sprintf (bndstr
, "[%llu, %llu]",
1140 (unsigned long long) bndrng
[0].to_uhwi (),
1141 (unsigned long long) bndrng
[1].to_uhwi ());
1144 const tree maxobjsize
= max_object_size ();
1145 const wide_int maxsiz
= wi::to_wide (maxobjsize
);
1148 tree func
= get_callee_fndecl (expr
);
1151 if (wi::ltu_p (maxsiz
, bndrng
[0]))
1152 warned
= warning_at (loc
, opt
,
1153 "%qD specified bound %s exceeds "
1154 "maximum object size %E",
1155 func
, bndstr
, maxobjsize
);
1158 bool maybe
= wi::to_wide (size
) == bndrng
[0];
1159 warned
= warning_at (loc
, opt
,
1161 ? G_("%qD specified bound %s exceeds "
1162 "the size %E of unterminated array")
1164 ? G_("%qD specified bound %s may "
1165 "exceed the size of at most %E "
1166 "of unterminated array")
1167 : G_("%qD specified bound %s exceeds "
1168 "the size of at most %E "
1169 "of unterminated array")),
1170 func
, bndstr
, size
);
1174 warned
= warning_at (loc
, opt
,
1175 "%qD argument missing terminating nul",
1182 if (wi::ltu_p (maxsiz
, bndrng
[0]))
1183 warned
= warning_at (loc
, opt
,
1184 "%qs specified bound %s exceeds "
1185 "maximum object size %E",
1186 fname
, bndstr
, maxobjsize
);
1189 bool maybe
= wi::to_wide (size
) == bndrng
[0];
1190 warned
= warning_at (loc
, opt
,
1192 ? G_("%qs specified bound %s exceeds "
1193 "the size %E of unterminated array")
1195 ? G_("%qs specified bound %s may "
1196 "exceed the size of at most %E "
1197 "of unterminated array")
1198 : G_("%qs specified bound %s exceeds "
1199 "the size of at most %E "
1200 "of unterminated array")),
1201 fname
, bndstr
, size
);
1205 warned
= warning_at (loc
, opt
,
1206 "%qs argument missing terminating nul",
1212 inform (DECL_SOURCE_LOCATION (decl
),
1213 "referenced argument declared here");
1214 suppress_warning (arg
, opt
);
1216 suppress_warning (expr
, opt
);
1220 /* For a call EXPR (which may be null) that expects a string argument
1221 SRC as an argument, returns false if SRC is a character array with
1222 no terminating NUL. When nonnull, BOUND is the number of characters
1223 in which to expect the terminating NUL. RDONLY is true for read-only
1224 accesses such as strcmp, false for read-write such as strcpy. When
1225 EXPR is also issues a warning. */
1228 check_nul_terminated_array (tree expr
, tree src
,
1229 tree bound
/* = NULL_TREE */)
1231 /* The constant size of the array SRC points to. The actual size
1232 may be less of EXACT is true, but not more. */
1234 /* True if SRC involves a non-constant offset into the array. */
1236 /* The unterminated constant array SRC points to. */
1237 tree nonstr
= unterminated_array (src
, &size
, &exact
);
1241 /* NONSTR refers to the non-nul terminated constant array and SIZE
1242 is the constant size of the array in bytes. EXACT is true when
1250 get_global_range_query ()->range_of_expr (r
, bound
);
1252 if (r
.kind () != VR_RANGE
)
1255 bndrng
[0] = r
.lower_bound ();
1256 bndrng
[1] = r
.upper_bound ();
1260 if (wi::leu_p (bndrng
[0], wi::to_wide (size
)))
1263 else if (wi::lt_p (bndrng
[0], wi::to_wide (size
), UNSIGNED
))
1268 warn_string_no_nul (EXPR_LOCATION (expr
), expr
, NULL
, src
, nonstr
,
1269 size
, exact
, bound
? bndrng
: NULL
);
1274 /* If EXP refers to an unterminated constant character array return
1275 the declaration of the object of which the array is a member or
1276 element and if SIZE is not null, set *SIZE to the size of
1277 the unterminated array and set *EXACT if the size is exact or
1278 clear it otherwise. Otherwise return null. */
1281 unterminated_array (tree exp
, tree
*size
/* = NULL */, bool *exact
/* = NULL */)
1283 /* C_STRLEN will return NULL and set DECL in the info
1284 structure if EXP references a unterminated array. */
1285 c_strlen_data lendata
= { };
1286 tree len
= c_strlen (exp
, 1, &lendata
);
1287 if (len
== NULL_TREE
&& lendata
.minlen
&& lendata
.decl
)
1291 len
= lendata
.minlen
;
1294 /* Constant offsets are already accounted for in LENDATA.MINLEN,
1295 but not in a SSA_NAME + CST expression. */
1296 if (TREE_CODE (lendata
.off
) == INTEGER_CST
)
1298 else if (TREE_CODE (lendata
.off
) == PLUS_EXPR
1299 && TREE_CODE (TREE_OPERAND (lendata
.off
, 1)) == INTEGER_CST
)
1301 /* Subtract the offset from the size of the array. */
1303 tree temp
= TREE_OPERAND (lendata
.off
, 1);
1304 temp
= fold_convert (ssizetype
, temp
);
1305 len
= fold_build2 (MINUS_EXPR
, ssizetype
, len
, temp
);
1315 return lendata
.decl
;
1321 /* Compute the length of a null-terminated character string or wide
1322 character string handling character sizes of 1, 2, and 4 bytes.
1323 TREE_STRING_LENGTH is not the right way because it evaluates to
1324 the size of the character array in bytes (as opposed to characters)
1325 and because it can contain a zero byte in the middle.
1327 ONLY_VALUE should be nonzero if the result is not going to be emitted
1328 into the instruction stream and zero if it is going to be expanded.
1329 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
1330 is returned, otherwise NULL, since
1331 len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
1332 evaluate the side-effects.
1334 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
1335 accesses. Note that this implies the result is not going to be emitted
1336 into the instruction stream.
1338 Additional information about the string accessed may be recorded
1339 in DATA. For example, if ARG references an unterminated string,
1340 then the declaration will be stored in the DECL field. If the
1341 length of the unterminated string can be determined, it'll be
1342 stored in the LEN field. Note this length could well be different
1343 than what a C strlen call would return.
1345 ELTSIZE is 1 for normal single byte character strings, and 2 or
1346 4 for wide characer strings. ELTSIZE is by default 1.
1348 The value returned is of type `ssizetype'. */
1351 c_strlen (tree arg
, int only_value
, c_strlen_data
*data
, unsigned eltsize
)
1353 /* If we were not passed a DATA pointer, then get one to a local
1354 structure. That avoids having to check DATA for NULL before
1355 each time we want to use it. */
1356 c_strlen_data local_strlen_data
= { };
1358 data
= &local_strlen_data
;
1360 gcc_checking_assert (eltsize
== 1 || eltsize
== 2 || eltsize
== 4);
1362 tree src
= STRIP_NOPS (arg
);
1363 if (TREE_CODE (src
) == COND_EXPR
1364 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
1368 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
, data
, eltsize
);
1369 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
, data
, eltsize
);
1370 if (tree_int_cst_equal (len1
, len2
))
1374 if (TREE_CODE (src
) == COMPOUND_EXPR
1375 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
1376 return c_strlen (TREE_OPERAND (src
, 1), only_value
, data
, eltsize
);
1378 location_t loc
= EXPR_LOC_OR_LOC (src
, input_location
);
1380 /* Offset from the beginning of the string in bytes. */
1384 src
= string_constant (src
, &byteoff
, &memsize
, &decl
);
1388 /* Determine the size of the string element. */
1389 if (eltsize
!= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src
)))))
1392 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
1393 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
1394 in case the latter is less than the size of the array, such as when
1395 SRC refers to a short string literal used to initialize a large array.
1396 In that case, the elements of the array after the terminating NUL are
1398 HOST_WIDE_INT strelts
= TREE_STRING_LENGTH (src
);
1399 strelts
= strelts
/ eltsize
;
1401 if (!tree_fits_uhwi_p (memsize
))
1404 HOST_WIDE_INT maxelts
= tree_to_uhwi (memsize
) / eltsize
;
1406 /* PTR can point to the byte representation of any string type, including
1407 char* and wchar_t*. */
1408 const char *ptr
= TREE_STRING_POINTER (src
);
1410 if (byteoff
&& TREE_CODE (byteoff
) != INTEGER_CST
)
1412 /* The code below works only for single byte character types. */
1416 /* If the string has an internal NUL character followed by any
1417 non-NUL characters (e.g., "foo\0bar"), we can't compute
1418 the offset to the following NUL if we don't know where to
1419 start searching for it. */
1420 unsigned len
= string_length (ptr
, eltsize
, strelts
);
1422 /* Return when an embedded null character is found or none at all.
1423 In the latter case, set the DECL/LEN field in the DATA structure
1424 so that callers may examine them. */
1425 if (len
+ 1 < strelts
)
1427 else if (len
>= maxelts
)
1430 data
->off
= byteoff
;
1431 data
->minlen
= ssize_int (len
);
1435 /* For empty strings the result should be zero. */
1437 return ssize_int (0);
1439 /* We don't know the starting offset, but we do know that the string
1440 has no internal zero bytes. If the offset falls within the bounds
1441 of the string subtract the offset from the length of the string,
1442 and return that. Otherwise the length is zero. Take care to
1443 use SAVE_EXPR in case the OFFSET has side-effects. */
1444 tree offsave
= TREE_SIDE_EFFECTS (byteoff
) ? save_expr (byteoff
)
1446 offsave
= fold_convert_loc (loc
, sizetype
, offsave
);
1447 tree condexp
= fold_build2_loc (loc
, LE_EXPR
, boolean_type_node
, offsave
,
1449 tree lenexp
= fold_build2_loc (loc
, MINUS_EXPR
, sizetype
, size_int (len
),
1451 lenexp
= fold_convert_loc (loc
, ssizetype
, lenexp
);
1452 return fold_build3_loc (loc
, COND_EXPR
, ssizetype
, condexp
, lenexp
,
1453 build_zero_cst (ssizetype
));
1456 /* Offset from the beginning of the string in elements. */
1457 HOST_WIDE_INT eltoff
;
1459 /* We have a known offset into the string. Start searching there for
1460 a null character if we can represent it as a single HOST_WIDE_INT. */
1463 else if (! tree_fits_uhwi_p (byteoff
) || tree_to_uhwi (byteoff
) % eltsize
)
1466 eltoff
= tree_to_uhwi (byteoff
) / eltsize
;
1468 /* If the offset is known to be out of bounds, warn, and call strlen at
1470 if (eltoff
< 0 || eltoff
>= maxelts
)
1472 /* Suppress multiple warnings for propagated constant strings. */
1474 && !warning_suppressed_p (arg
, OPT_Warray_bounds
)
1475 && warning_at (loc
, OPT_Warray_bounds
,
1476 "offset %qwi outside bounds of constant string",
1480 inform (DECL_SOURCE_LOCATION (decl
), "%qE declared here", decl
);
1481 suppress_warning (arg
, OPT_Warray_bounds
);
1486 /* If eltoff is larger than strelts but less than maxelts the
1487 string length is zero, since the excess memory will be zero. */
1488 if (eltoff
> strelts
)
1489 return ssize_int (0);
1491 /* Use strlen to search for the first zero byte. Since any strings
1492 constructed with build_string will have nulls appended, we win even
1493 if we get handed something like (char[4])"abcd".
1495 Since ELTOFF is our starting index into the string, no further
1496 calculation is needed. */
1497 unsigned len
= string_length (ptr
+ eltoff
* eltsize
, eltsize
,
1500 /* Don't know what to return if there was no zero termination.
1501 Ideally this would turn into a gcc_checking_assert over time.
1502 Set DECL/LEN so callers can examine them. */
1503 if (len
>= maxelts
- eltoff
)
1506 data
->off
= byteoff
;
1507 data
->minlen
= ssize_int (len
);
1511 return ssize_int (len
);
1514 /* Return a constant integer corresponding to target reading
1515 GET_MODE_BITSIZE (MODE) bits from string constant STR. If
1516 NULL_TERMINATED_P, reading stops after '\0' character, all further ones
1517 are assumed to be zero, otherwise it reads as many characters
1521 c_readstr (const char *str
, scalar_int_mode mode
,
1522 bool null_terminated_p
/*=true*/)
1526 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
1528 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
1529 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
1530 / HOST_BITS_PER_WIDE_INT
;
1532 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
1533 for (i
= 0; i
< len
; i
++)
1537 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
1540 if (WORDS_BIG_ENDIAN
)
1541 j
= GET_MODE_SIZE (mode
) - i
- 1;
1542 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
1543 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
1544 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
1547 if (ch
|| !null_terminated_p
)
1548 ch
= (unsigned char) str
[i
];
1549 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
1552 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
1553 return immed_wide_int_const (c
, mode
);
1556 /* Cast a target constant CST to target CHAR and if that value fits into
1557 host char type, return zero and put that value into variable pointed to by
1561 target_char_cast (tree cst
, char *p
)
1563 unsigned HOST_WIDE_INT val
, hostval
;
1565 if (TREE_CODE (cst
) != INTEGER_CST
1566 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
1569 /* Do not care if it fits or not right here. */
1570 val
= TREE_INT_CST_LOW (cst
);
1572 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
1573 val
&= (HOST_WIDE_INT_1U
<< CHAR_TYPE_SIZE
) - 1;
1576 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
1577 hostval
&= (HOST_WIDE_INT_1U
<< HOST_BITS_PER_CHAR
) - 1;
1586 /* Similar to save_expr, but assumes that arbitrary code is not executed
1587 in between the multiple evaluations. In particular, we assume that a
1588 non-addressable local variable will not be modified. */
1591 builtin_save_expr (tree exp
)
1593 if (TREE_CODE (exp
) == SSA_NAME
1594 || (TREE_ADDRESSABLE (exp
) == 0
1595 && (TREE_CODE (exp
) == PARM_DECL
1596 || (VAR_P (exp
) && !TREE_STATIC (exp
)))))
1599 return save_expr (exp
);
1602 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
1603 times to get the address of either a higher stack frame, or a return
1604 address located within it (depending on FNDECL_CODE). */
1607 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
1610 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
1611 if (tem
== NULL_RTX
)
1613 /* For a zero count with __builtin_return_address, we don't care what
1614 frame address we return, because target-specific definitions will
1615 override us. Therefore frame pointer elimination is OK, and using
1616 the soft frame pointer is OK.
1618 For a nonzero count, or a zero count with __builtin_frame_address,
1619 we require a stable offset from the current frame pointer to the
1620 previous one, so we must use the hard frame pointer, and
1621 we must disable frame pointer elimination. */
1622 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
1623 tem
= frame_pointer_rtx
;
1626 tem
= hard_frame_pointer_rtx
;
1628 /* Tell reload not to eliminate the frame pointer. */
1629 crtl
->accesses_prior_frames
= 1;
1634 SETUP_FRAME_ADDRESSES ();
1636 /* On the SPARC, the return address is not in the frame, it is in a
1637 register. There is no way to access it off of the current frame
1638 pointer, but it can be accessed off the previous frame pointer by
1639 reading the value from the register window save area. */
1640 if (RETURN_ADDR_IN_PREVIOUS_FRAME
&& fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
1643 /* Scan back COUNT frames to the specified frame. */
1644 for (i
= 0; i
< count
; i
++)
1646 /* Assume the dynamic chain pointer is in the word that the
1647 frame address points to, unless otherwise specified. */
1648 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
1649 tem
= memory_address (Pmode
, tem
);
1650 tem
= gen_frame_mem (Pmode
, tem
);
1651 tem
= copy_to_reg (tem
);
1654 /* For __builtin_frame_address, return what we've got. But, on
1655 the SPARC for example, we may have to add a bias. */
1656 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
1657 return FRAME_ADDR_RTX (tem
);
1659 /* For __builtin_return_address, get the return address from that frame. */
1660 #ifdef RETURN_ADDR_RTX
1661 tem
= RETURN_ADDR_RTX (count
, tem
);
1663 tem
= memory_address (Pmode
,
1664 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
1665 tem
= gen_frame_mem (Pmode
, tem
);
1670 /* Alias set used for setjmp buffer. */
1671 static alias_set_type setjmp_alias_set
= -1;
1673 /* Construct the leading half of a __builtin_setjmp call. Control will
1674 return to RECEIVER_LABEL. This is also called directly by the SJLJ
1675 exception handling code. */
1678 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
1680 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1684 if (setjmp_alias_set
== -1)
1685 setjmp_alias_set
= new_alias_set ();
1687 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
1689 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
1691 /* We store the frame pointer and the address of receiver_label in
1692 the buffer and use the rest of it for the stack save area, which
1693 is machine-dependent. */
1695 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
1696 set_mem_alias_set (mem
, setjmp_alias_set
);
1697 emit_move_insn (mem
, hard_frame_pointer_rtx
);
1699 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1700 GET_MODE_SIZE (Pmode
))),
1701 set_mem_alias_set (mem
, setjmp_alias_set
);
1703 emit_move_insn (validize_mem (mem
),
1704 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
1706 stack_save
= gen_rtx_MEM (sa_mode
,
1707 plus_constant (Pmode
, buf_addr
,
1708 2 * GET_MODE_SIZE (Pmode
)));
1709 set_mem_alias_set (stack_save
, setjmp_alias_set
);
1710 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1712 /* If there is further processing to do, do it. */
1713 if (targetm
.have_builtin_setjmp_setup ())
1714 emit_insn (targetm
.gen_builtin_setjmp_setup (buf_addr
));
1716 /* We have a nonlocal label. */
1717 cfun
->has_nonlocal_label
= 1;
1720 /* Construct the trailing part of a __builtin_setjmp call. This is
1721 also called directly by the SJLJ exception handling code.
1722 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
1725 expand_builtin_setjmp_receiver (rtx receiver_label
)
1729 /* Mark the FP as used when we get here, so we have to make sure it's
1730 marked as used by this function. */
1731 emit_use (hard_frame_pointer_rtx
);
1733 /* Mark the static chain as clobbered here so life information
1734 doesn't get messed up for it. */
1735 chain
= rtx_for_static_chain (current_function_decl
, true);
1736 if (chain
&& REG_P (chain
))
1737 emit_clobber (chain
);
1739 if (!HARD_FRAME_POINTER_IS_ARG_POINTER
&& fixed_regs
[ARG_POINTER_REGNUM
])
1741 /* If the argument pointer can be eliminated in favor of the
1742 frame pointer, we don't need to restore it. We assume here
1743 that if such an elimination is present, it can always be used.
1744 This is the case on all known machines; if we don't make this
1745 assumption, we do unnecessary saving on many machines. */
1747 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
1749 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
1750 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
1751 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
1754 if (i
== ARRAY_SIZE (elim_regs
))
1756 /* Now restore our arg pointer from the address at which it
1757 was saved in our stack frame. */
1758 emit_move_insn (crtl
->args
.internal_arg_pointer
,
1759 copy_to_reg (get_arg_pointer_save_area ()));
1763 if (receiver_label
!= NULL
&& targetm
.have_builtin_setjmp_receiver ())
1764 emit_insn (targetm
.gen_builtin_setjmp_receiver (receiver_label
));
1765 else if (targetm
.have_nonlocal_goto_receiver ())
1766 emit_insn (targetm
.gen_nonlocal_goto_receiver ());
1770 /* We must not allow the code we just generated to be reordered by
1771 scheduling. Specifically, the update of the frame pointer must
1772 happen immediately, not later. */
1773 emit_insn (gen_blockage ());
1776 /* __builtin_longjmp is passed a pointer to an array of five words (not
1777 all will be used on all machines). It operates similarly to the C
1778 library function of the same name, but is more efficient. Much of
1779 the code below is copied from the handling of non-local gotos. */
1782 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
1785 rtx_insn
*insn
, *last
;
1786 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1788 /* DRAP is needed for stack realign if longjmp is expanded to current
1790 if (SUPPORTS_STACK_ALIGNMENT
)
1791 crtl
->need_drap
= true;
1793 if (setjmp_alias_set
== -1)
1794 setjmp_alias_set
= new_alias_set ();
1796 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
1798 buf_addr
= force_reg (Pmode
, buf_addr
);
1800 /* We require that the user must pass a second argument of 1, because
1801 that is what builtin_setjmp will return. */
1802 gcc_assert (value
== const1_rtx
);
1804 last
= get_last_insn ();
1805 if (targetm
.have_builtin_longjmp ())
1806 emit_insn (targetm
.gen_builtin_longjmp (buf_addr
));
1809 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
1810 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1811 GET_MODE_SIZE (Pmode
)));
1813 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
1814 2 * GET_MODE_SIZE (Pmode
)));
1815 set_mem_alias_set (fp
, setjmp_alias_set
);
1816 set_mem_alias_set (lab
, setjmp_alias_set
);
1817 set_mem_alias_set (stack
, setjmp_alias_set
);
1819 /* Pick up FP, label, and SP from the block and jump. This code is
1820 from expand_goto in stmt.c; see there for detailed comments. */
1821 if (targetm
.have_nonlocal_goto ())
1822 /* We have to pass a value to the nonlocal_goto pattern that will
1823 get copied into the static_chain pointer, but it does not matter
1824 what that value is, because builtin_setjmp does not use it. */
1825 emit_insn (targetm
.gen_nonlocal_goto (value
, lab
, stack
, fp
));
1828 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1829 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1831 lab
= copy_to_reg (lab
);
1833 /* Restore the frame pointer and stack pointer. We must use a
1834 temporary since the setjmp buffer may be a local. */
1835 fp
= copy_to_reg (fp
);
1836 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1838 /* Ensure the frame pointer move is not optimized. */
1839 emit_insn (gen_blockage ());
1840 emit_clobber (hard_frame_pointer_rtx
);
1841 emit_clobber (frame_pointer_rtx
);
1842 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1844 emit_use (hard_frame_pointer_rtx
);
1845 emit_use (stack_pointer_rtx
);
1846 emit_indirect_jump (lab
);
1850 /* Search backwards and mark the jump insn as a non-local goto.
1851 Note that this precludes the use of __builtin_longjmp to a
1852 __builtin_setjmp target in the same function. However, we've
1853 already cautioned the user that these functions are for
1854 internal exception handling use only. */
1855 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1857 gcc_assert (insn
!= last
);
1861 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1864 else if (CALL_P (insn
))
1870 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1872 return (iter
->i
< iter
->n
);
1875 /* This function validates the types of a function call argument list
1876 against a specified list of tree_codes. If the last specifier is a 0,
1877 that represents an ellipsis, otherwise the last specifier must be a
1881 validate_arglist (const_tree callexpr
, ...)
1883 enum tree_code code
;
1886 const_call_expr_arg_iterator iter
;
1889 va_start (ap
, callexpr
);
1890 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1892 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1893 tree fn
= CALL_EXPR_FN (callexpr
);
1894 bitmap argmap
= get_nonnull_args (TREE_TYPE (TREE_TYPE (fn
)));
1896 for (unsigned argno
= 1; ; ++argno
)
1898 code
= (enum tree_code
) va_arg (ap
, int);
1903 /* This signifies an ellipses, any further arguments are all ok. */
1907 /* This signifies an endlink, if no arguments remain, return
1908 true, otherwise return false. */
1909 res
= !more_const_call_expr_args_p (&iter
);
1912 /* The actual argument must be nonnull when either the whole
1913 called function has been declared nonnull, or when the formal
1914 argument corresponding to the actual argument has been. */
1916 && (bitmap_empty_p (argmap
) || bitmap_bit_p (argmap
, argno
)))
1918 arg
= next_const_call_expr_arg (&iter
);
1919 if (!validate_arg (arg
, code
) || integer_zerop (arg
))
1925 /* If no parameters remain or the parameter's code does not
1926 match the specified code, return false. Otherwise continue
1927 checking any remaining arguments. */
1928 arg
= next_const_call_expr_arg (&iter
);
1929 if (!validate_arg (arg
, code
))
1935 /* We need gotos here since we can only have one VA_CLOSE in a
1940 BITMAP_FREE (argmap
);
1945 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1946 and the address of the save area. */
1949 expand_builtin_nonlocal_goto (tree exp
)
1951 tree t_label
, t_save_area
;
1952 rtx r_label
, r_save_area
, r_fp
, r_sp
;
1955 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1958 t_label
= CALL_EXPR_ARG (exp
, 0);
1959 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1961 r_label
= expand_normal (t_label
);
1962 r_label
= convert_memory_address (Pmode
, r_label
);
1963 r_save_area
= expand_normal (t_save_area
);
1964 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1965 /* Copy the address of the save location to a register just in case it was
1966 based on the frame pointer. */
1967 r_save_area
= copy_to_reg (r_save_area
);
1968 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1969 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1970 plus_constant (Pmode
, r_save_area
,
1971 GET_MODE_SIZE (Pmode
)));
1973 crtl
->has_nonlocal_goto
= 1;
1975 /* ??? We no longer need to pass the static chain value, afaik. */
1976 if (targetm
.have_nonlocal_goto ())
1977 emit_insn (targetm
.gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1980 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1981 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1983 r_label
= copy_to_reg (r_label
);
1985 /* Restore the frame pointer and stack pointer. We must use a
1986 temporary since the setjmp buffer may be a local. */
1987 r_fp
= copy_to_reg (r_fp
);
1988 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1990 /* Ensure the frame pointer move is not optimized. */
1991 emit_insn (gen_blockage ());
1992 emit_clobber (hard_frame_pointer_rtx
);
1993 emit_clobber (frame_pointer_rtx
);
1994 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1996 /* USE of hard_frame_pointer_rtx added for consistency;
1997 not clear if really needed. */
1998 emit_use (hard_frame_pointer_rtx
);
1999 emit_use (stack_pointer_rtx
);
2001 /* If the architecture is using a GP register, we must
2002 conservatively assume that the target function makes use of it.
2003 The prologue of functions with nonlocal gotos must therefore
2004 initialize the GP register to the appropriate value, and we
2005 must then make sure that this value is live at the point
2006 of the jump. (Note that this doesn't necessarily apply
2007 to targets with a nonlocal_goto pattern; they are free
2008 to implement it in their own way. Note also that this is
2009 a no-op if the GP register is a global invariant.) */
2010 unsigned regnum
= PIC_OFFSET_TABLE_REGNUM
;
2011 if (regnum
!= INVALID_REGNUM
&& fixed_regs
[regnum
])
2012 emit_use (pic_offset_table_rtx
);
2014 emit_indirect_jump (r_label
);
2017 /* Search backwards to the jump insn and mark it as a
2019 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
2023 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
2026 else if (CALL_P (insn
))
2033 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
2034 (not all will be used on all machines) that was passed to __builtin_setjmp.
2035 It updates the stack pointer in that block to the current value. This is
2036 also called directly by the SJLJ exception handling code. */
2039 expand_builtin_update_setjmp_buf (rtx buf_addr
)
2041 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
2042 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
2044 = gen_rtx_MEM (sa_mode
,
2047 plus_constant (Pmode
, buf_addr
,
2048 2 * GET_MODE_SIZE (Pmode
))));
2050 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
2053 /* Expand a call to __builtin_prefetch. For a target that does not support
2054 data prefetch, evaluate the memory address argument in case it has side
2058 expand_builtin_prefetch (tree exp
)
2060 tree arg0
, arg1
, arg2
;
2064 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
2067 arg0
= CALL_EXPR_ARG (exp
, 0);
2069 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
2070 zero (read) and argument 2 (locality) defaults to 3 (high degree of
2072 nargs
= call_expr_nargs (exp
);
2074 arg1
= CALL_EXPR_ARG (exp
, 1);
2076 arg1
= integer_zero_node
;
2078 arg2
= CALL_EXPR_ARG (exp
, 2);
2080 arg2
= integer_three_node
;
2082 /* Argument 0 is an address. */
2083 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
2085 /* Argument 1 (read/write flag) must be a compile-time constant int. */
2086 if (TREE_CODE (arg1
) != INTEGER_CST
)
2088 error ("second argument to %<__builtin_prefetch%> must be a constant");
2089 arg1
= integer_zero_node
;
2091 op1
= expand_normal (arg1
);
2092 /* Argument 1 must be either zero or one. */
2093 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
2095 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
2100 /* Argument 2 (locality) must be a compile-time constant int. */
2101 if (TREE_CODE (arg2
) != INTEGER_CST
)
2103 error ("third argument to %<__builtin_prefetch%> must be a constant");
2104 arg2
= integer_zero_node
;
2106 op2
= expand_normal (arg2
);
2107 /* Argument 2 must be 0, 1, 2, or 3. */
2108 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
2110 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
2114 if (targetm
.have_prefetch ())
2116 class expand_operand ops
[3];
2118 create_address_operand (&ops
[0], op0
);
2119 create_integer_operand (&ops
[1], INTVAL (op1
));
2120 create_integer_operand (&ops
[2], INTVAL (op2
));
2121 if (maybe_expand_insn (targetm
.code_for_prefetch
, 3, ops
))
2125 /* Don't do anything with direct references to volatile memory, but
2126 generate code to handle other side effects. */
2127 if (!MEM_P (op0
) && side_effects_p (op0
))
2131 /* Get a MEM rtx for expression EXP which is the address of an operand
2132 to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
2133 the maximum length of the block of memory that might be accessed or
2137 get_memory_rtx (tree exp
, tree len
)
2139 tree orig_exp
= exp
;
2142 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
2143 from its expression, for expr->a.b only <variable>.a.b is recorded. */
2144 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
2145 exp
= TREE_OPERAND (exp
, 0);
2147 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
2148 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
2150 /* Get an expression we can use to find the attributes to assign to MEM.
2151 First remove any nops. */
2152 while (CONVERT_EXPR_P (exp
)
2153 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
2154 exp
= TREE_OPERAND (exp
, 0);
2156 /* Build a MEM_REF representing the whole accessed area as a byte blob,
2157 (as builtin stringops may alias with anything). */
2158 exp
= fold_build2 (MEM_REF
,
2159 build_array_type (char_type_node
,
2160 build_range_type (sizetype
,
2161 size_one_node
, len
)),
2162 exp
, build_int_cst (ptr_type_node
, 0));
2164 /* If the MEM_REF has no acceptable address, try to get the base object
2165 from the original address we got, and build an all-aliasing
2166 unknown-sized access to that one. */
2167 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
2168 set_mem_attributes (mem
, exp
, 0);
2169 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
2170 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
2173 exp
= build_fold_addr_expr (exp
);
2174 exp
= fold_build2 (MEM_REF
,
2175 build_array_type (char_type_node
,
2176 build_range_type (sizetype
,
2179 exp
, build_int_cst (ptr_type_node
, 0));
2180 set_mem_attributes (mem
, exp
, 0);
2182 set_mem_alias_set (mem
, 0);
2186 /* Built-in functions to perform an untyped call and return. */
2188 #define apply_args_mode \
2189 (this_target_builtins->x_apply_args_mode)
2190 #define apply_result_mode \
2191 (this_target_builtins->x_apply_result_mode)
2193 /* Return the size required for the block returned by __builtin_apply_args,
2194 and initialize apply_args_mode. */
2197 apply_args_size (void)
2199 static int size
= -1;
2203 /* The values computed by this function never change. */
2206 /* The first value is the incoming arg-pointer. */
2207 size
= GET_MODE_SIZE (Pmode
);
2209 /* The second value is the structure value address unless this is
2210 passed as an "invisible" first argument. */
2211 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
2212 size
+= GET_MODE_SIZE (Pmode
);
2214 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2215 if (FUNCTION_ARG_REGNO_P (regno
))
2217 fixed_size_mode mode
= targetm
.calls
.get_raw_arg_mode (regno
);
2219 gcc_assert (mode
!= VOIDmode
);
2221 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2222 if (size
% align
!= 0)
2223 size
= CEIL (size
, align
) * align
;
2224 size
+= GET_MODE_SIZE (mode
);
2225 apply_args_mode
[regno
] = mode
;
2229 apply_args_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
2235 /* Return the size required for the block returned by __builtin_apply,
2236 and initialize apply_result_mode. */
2239 apply_result_size (void)
2241 static int size
= -1;
2244 /* The values computed by this function never change. */
2249 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2250 if (targetm
.calls
.function_value_regno_p (regno
))
2252 fixed_size_mode mode
= targetm
.calls
.get_raw_result_mode (regno
);
2254 gcc_assert (mode
!= VOIDmode
);
2256 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2257 if (size
% align
!= 0)
2258 size
= CEIL (size
, align
) * align
;
2259 size
+= GET_MODE_SIZE (mode
);
2260 apply_result_mode
[regno
] = mode
;
2263 apply_result_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
2265 /* Allow targets that use untyped_call and untyped_return to override
2266 the size so that machine-specific information can be stored here. */
2267 #ifdef APPLY_RESULT_SIZE
2268 size
= APPLY_RESULT_SIZE
;
2274 /* Create a vector describing the result block RESULT. If SAVEP is true,
2275 the result block is used to save the values; otherwise it is used to
2276 restore the values. */
2279 result_vector (int savep
, rtx result
)
2281 int regno
, size
, align
, nelts
;
2282 fixed_size_mode mode
;
2284 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
2287 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2288 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
2290 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2291 if (size
% align
!= 0)
2292 size
= CEIL (size
, align
) * align
;
2293 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
2294 mem
= adjust_address (result
, mode
, size
);
2295 savevec
[nelts
++] = (savep
2296 ? gen_rtx_SET (mem
, reg
)
2297 : gen_rtx_SET (reg
, mem
));
2298 size
+= GET_MODE_SIZE (mode
);
2300 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
2303 /* Save the state required to perform an untyped call with the same
2304 arguments as were passed to the current function. */
2307 expand_builtin_apply_args_1 (void)
2310 int size
, align
, regno
;
2311 fixed_size_mode mode
;
2312 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
2314 /* Create a block where the arg-pointer, structure value address,
2315 and argument registers can be saved. */
2316 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
2318 /* Walk past the arg-pointer and structure value address. */
2319 size
= GET_MODE_SIZE (Pmode
);
2320 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
2321 size
+= GET_MODE_SIZE (Pmode
);
2323 /* Save each register used in calling a function to the block. */
2324 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2325 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
2327 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2328 if (size
% align
!= 0)
2329 size
= CEIL (size
, align
) * align
;
2331 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
2333 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
2334 size
+= GET_MODE_SIZE (mode
);
2337 /* Save the arg pointer to the block. */
2338 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
2339 /* We need the pointer as the caller actually passed them to us, not
2340 as we might have pretended they were passed. Make sure it's a valid
2341 operand, as emit_move_insn isn't expected to handle a PLUS. */
2342 if (STACK_GROWS_DOWNWARD
)
2344 = force_operand (plus_constant (Pmode
, tem
,
2345 crtl
->args
.pretend_args_size
),
2347 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
2349 size
= GET_MODE_SIZE (Pmode
);
2351 /* Save the structure value address unless this is passed as an
2352 "invisible" first argument. */
2353 if (struct_incoming_value
)
2354 emit_move_insn (adjust_address (registers
, Pmode
, size
),
2355 copy_to_reg (struct_incoming_value
));
2357 /* Return the address of the block. */
2358 return copy_addr_to_reg (XEXP (registers
, 0));
2361 /* __builtin_apply_args returns block of memory allocated on
2362 the stack into which is stored the arg pointer, structure
2363 value address, static chain, and all the registers that might
2364 possibly be used in performing a function call. The code is
2365 moved to the start of the function so the incoming values are
2369 expand_builtin_apply_args (void)
2371 /* Don't do __builtin_apply_args more than once in a function.
2372 Save the result of the first call and reuse it. */
2373 if (apply_args_value
!= 0)
2374 return apply_args_value
;
2376 /* When this function is called, it means that registers must be
2377 saved on entry to this function. So we migrate the
2378 call to the first insn of this function. */
2382 temp
= expand_builtin_apply_args_1 ();
2383 rtx_insn
*seq
= get_insns ();
2386 apply_args_value
= temp
;
2388 /* Put the insns after the NOTE that starts the function.
2389 If this is inside a start_sequence, make the outer-level insn
2390 chain current, so the code is placed at the start of the
2391 function. If internal_arg_pointer is a non-virtual pseudo,
2392 it needs to be placed after the function that initializes
2394 push_topmost_sequence ();
2395 if (REG_P (crtl
->args
.internal_arg_pointer
)
2396 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
2397 emit_insn_before (seq
, parm_birth_insn
);
2399 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
2400 pop_topmost_sequence ();
2405 /* Perform an untyped call and save the state required to perform an
2406 untyped return of whatever value was returned by the given function. */
2409 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
2411 int size
, align
, regno
;
2412 fixed_size_mode mode
;
2413 rtx incoming_args
, result
, reg
, dest
, src
;
2414 rtx_call_insn
*call_insn
;
2415 rtx old_stack_level
= 0;
2416 rtx call_fusage
= 0;
2417 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
2419 arguments
= convert_memory_address (Pmode
, arguments
);
2421 /* Create a block where the return registers can be saved. */
2422 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
2424 /* Fetch the arg pointer from the ARGUMENTS block. */
2425 incoming_args
= gen_reg_rtx (Pmode
);
2426 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
2427 if (!STACK_GROWS_DOWNWARD
)
2428 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
2429 incoming_args
, 0, OPTAB_LIB_WIDEN
);
2431 /* Push a new argument block and copy the arguments. Do not allow
2432 the (potential) memcpy call below to interfere with our stack
2434 do_pending_stack_adjust ();
2437 /* Save the stack with nonlocal if available. */
2438 if (targetm
.have_save_stack_nonlocal ())
2439 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
2441 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
2443 /* Allocate a block of memory onto the stack and copy the memory
2444 arguments to the outgoing arguments address. We can pass TRUE
2445 as the 4th argument because we just saved the stack pointer
2446 and will restore it right after the call. */
2447 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, -1, true);
2449 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
2450 may have already set current_function_calls_alloca to true.
2451 current_function_calls_alloca won't be set if argsize is zero,
2452 so we have to guarantee need_drap is true here. */
2453 if (SUPPORTS_STACK_ALIGNMENT
)
2454 crtl
->need_drap
= true;
2456 dest
= virtual_outgoing_args_rtx
;
2457 if (!STACK_GROWS_DOWNWARD
)
2459 if (CONST_INT_P (argsize
))
2460 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
2462 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
2464 dest
= gen_rtx_MEM (BLKmode
, dest
);
2465 set_mem_align (dest
, PARM_BOUNDARY
);
2466 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
2467 set_mem_align (src
, PARM_BOUNDARY
);
2468 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
2470 /* Refer to the argument block. */
2472 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
2473 set_mem_align (arguments
, PARM_BOUNDARY
);
2475 /* Walk past the arg-pointer and structure value address. */
2476 size
= GET_MODE_SIZE (Pmode
);
2478 size
+= GET_MODE_SIZE (Pmode
);
2480 /* Restore each of the registers previously saved. Make USE insns
2481 for each of these registers for use in making the call. */
2482 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2483 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
2485 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2486 if (size
% align
!= 0)
2487 size
= CEIL (size
, align
) * align
;
2488 reg
= gen_rtx_REG (mode
, regno
);
2489 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
2490 use_reg (&call_fusage
, reg
);
2491 size
+= GET_MODE_SIZE (mode
);
2494 /* Restore the structure value address unless this is passed as an
2495 "invisible" first argument. */
2496 size
= GET_MODE_SIZE (Pmode
);
2499 rtx value
= gen_reg_rtx (Pmode
);
2500 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
2501 emit_move_insn (struct_value
, value
);
2502 if (REG_P (struct_value
))
2503 use_reg (&call_fusage
, struct_value
);
2506 /* All arguments and registers used for the call are set up by now! */
2507 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
2509 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
2510 and we don't want to load it into a register as an optimization,
2511 because prepare_call_address already did it if it should be done. */
2512 if (GET_CODE (function
) != SYMBOL_REF
)
2513 function
= memory_address (FUNCTION_MODE
, function
);
2515 /* Generate the actual call instruction and save the return value. */
2516 if (targetm
.have_untyped_call ())
2518 rtx mem
= gen_rtx_MEM (FUNCTION_MODE
, function
);
2519 rtx_insn
*seq
= targetm
.gen_untyped_call (mem
, result
,
2520 result_vector (1, result
));
2521 for (rtx_insn
*insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
2523 add_reg_note (insn
, REG_UNTYPED_CALL
, NULL_RTX
);
2526 else if (targetm
.have_call_value ())
2530 /* Locate the unique return register. It is not possible to
2531 express a call that sets more than one return register using
2532 call_value; use untyped_call for that. In fact, untyped_call
2533 only needs to save the return registers in the given block. */
2534 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2535 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
2537 gcc_assert (!valreg
); /* have_untyped_call required. */
2539 valreg
= gen_rtx_REG (mode
, regno
);
2542 emit_insn (targetm
.gen_call_value (valreg
,
2543 gen_rtx_MEM (FUNCTION_MODE
, function
),
2544 const0_rtx
, NULL_RTX
, const0_rtx
));
2546 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
2551 /* Find the CALL insn we just emitted, and attach the register usage
2553 call_insn
= last_call_insn ();
2554 add_function_usage_to (call_insn
, call_fusage
);
2556 /* Restore the stack. */
2557 if (targetm
.have_save_stack_nonlocal ())
2558 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
2560 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
2561 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
2565 /* Return the address of the result block. */
2566 result
= copy_addr_to_reg (XEXP (result
, 0));
2567 return convert_memory_address (ptr_mode
, result
);
2570 /* Perform an untyped return. */
2573 expand_builtin_return (rtx result
)
2575 int size
, align
, regno
;
2576 fixed_size_mode mode
;
2578 rtx_insn
*call_fusage
= 0;
2580 result
= convert_memory_address (Pmode
, result
);
2582 apply_result_size ();
2583 result
= gen_rtx_MEM (BLKmode
, result
);
2585 if (targetm
.have_untyped_return ())
2587 rtx vector
= result_vector (0, result
);
2588 emit_jump_insn (targetm
.gen_untyped_return (result
, vector
));
2593 /* Restore the return value and note that each value is used. */
2595 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
2596 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
2598 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
2599 if (size
% align
!= 0)
2600 size
= CEIL (size
, align
) * align
;
2601 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
2602 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
2604 push_to_sequence (call_fusage
);
2606 call_fusage
= get_insns ();
2608 size
+= GET_MODE_SIZE (mode
);
2611 /* Put the USE insns before the return. */
2612 emit_insn (call_fusage
);
2614 /* Return whatever values was restored by jumping directly to the end
2616 expand_naked_return ();
2619 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
2621 static enum type_class
2622 type_to_class (tree type
)
2624 switch (TREE_CODE (type
))
2626 case VOID_TYPE
: return void_type_class
;
2627 case INTEGER_TYPE
: return integer_type_class
;
2628 case ENUMERAL_TYPE
: return enumeral_type_class
;
2629 case BOOLEAN_TYPE
: return boolean_type_class
;
2630 case POINTER_TYPE
: return pointer_type_class
;
2631 case REFERENCE_TYPE
: return reference_type_class
;
2632 case OFFSET_TYPE
: return offset_type_class
;
2633 case REAL_TYPE
: return real_type_class
;
2634 case COMPLEX_TYPE
: return complex_type_class
;
2635 case FUNCTION_TYPE
: return function_type_class
;
2636 case METHOD_TYPE
: return method_type_class
;
2637 case RECORD_TYPE
: return record_type_class
;
2639 case QUAL_UNION_TYPE
: return union_type_class
;
2640 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
2641 ? string_type_class
: array_type_class
);
2642 case LANG_TYPE
: return lang_type_class
;
2643 case OPAQUE_TYPE
: return opaque_type_class
;
2644 default: return no_type_class
;
2648 /* Expand a call EXP to __builtin_classify_type. */
2651 expand_builtin_classify_type (tree exp
)
2653 if (call_expr_nargs (exp
))
2654 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
2655 return GEN_INT (no_type_class
);
2658 /* This helper macro, meant to be used in mathfn_built_in below, determines
2659 which among a set of builtin math functions is appropriate for a given type
2660 mode. The `F' (float) and `L' (long double) are automatically generated
2661 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
2662 types, there are additional types that are considered with 'F32', 'F64',
2663 'F128', etc. suffixes. */
2664 #define CASE_MATHFN(MATHFN) \
2665 CASE_CFN_##MATHFN: \
2666 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
2667 fcodel = BUILT_IN_##MATHFN##L ; break;
2668 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
2670 #define CASE_MATHFN_FLOATN(MATHFN) \
2671 CASE_CFN_##MATHFN: \
2672 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
2673 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
2674 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
2675 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
2676 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
2678 /* Similar to above, but appends _R after any F/L suffix. */
2679 #define CASE_MATHFN_REENT(MATHFN) \
2680 case CFN_BUILT_IN_##MATHFN##_R: \
2681 case CFN_BUILT_IN_##MATHFN##F_R: \
2682 case CFN_BUILT_IN_##MATHFN##L_R: \
2683 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
2684 fcodel = BUILT_IN_##MATHFN##L_R ; break;
2686 /* Return a function equivalent to FN but operating on floating-point
2687 values of type TYPE, or END_BUILTINS if no such function exists.
2688 This is purely an operation on function codes; it does not guarantee
2689 that the target actually has an implementation of the function. */
2691 static built_in_function
2692 mathfn_built_in_2 (tree type
, combined_fn fn
)
2695 built_in_function fcode
, fcodef
, fcodel
;
2696 built_in_function fcodef16
= END_BUILTINS
;
2697 built_in_function fcodef32
= END_BUILTINS
;
2698 built_in_function fcodef64
= END_BUILTINS
;
2699 built_in_function fcodef128
= END_BUILTINS
;
2700 built_in_function fcodef32x
= END_BUILTINS
;
2701 built_in_function fcodef64x
= END_BUILTINS
;
2702 built_in_function fcodef128x
= END_BUILTINS
;
2706 #define SEQ_OF_CASE_MATHFN \
2707 CASE_MATHFN (ACOS) \
2708 CASE_MATHFN (ACOSH) \
2709 CASE_MATHFN (ASIN) \
2710 CASE_MATHFN (ASINH) \
2711 CASE_MATHFN (ATAN) \
2712 CASE_MATHFN (ATAN2) \
2713 CASE_MATHFN (ATANH) \
2714 CASE_MATHFN (CBRT) \
2715 CASE_MATHFN_FLOATN (CEIL) \
2716 CASE_MATHFN (CEXPI) \
2717 CASE_MATHFN_FLOATN (COPYSIGN) \
2719 CASE_MATHFN (COSH) \
2720 CASE_MATHFN (DREM) \
2722 CASE_MATHFN (ERFC) \
2724 CASE_MATHFN (EXP10) \
2725 CASE_MATHFN (EXP2) \
2726 CASE_MATHFN (EXPM1) \
2727 CASE_MATHFN (FABS) \
2728 CASE_MATHFN (FDIM) \
2729 CASE_MATHFN_FLOATN (FLOOR) \
2730 CASE_MATHFN_FLOATN (FMA) \
2731 CASE_MATHFN_FLOATN (FMAX) \
2732 CASE_MATHFN_FLOATN (FMIN) \
2733 CASE_MATHFN (FMOD) \
2734 CASE_MATHFN (FREXP) \
2735 CASE_MATHFN (GAMMA) \
2736 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2737 CASE_MATHFN (HUGE_VAL) \
2738 CASE_MATHFN (HYPOT) \
2739 CASE_MATHFN (ILOGB) \
2740 CASE_MATHFN (ICEIL) \
2741 CASE_MATHFN (IFLOOR) \
2743 CASE_MATHFN (IRINT) \
2744 CASE_MATHFN (IROUND) \
2745 CASE_MATHFN (ISINF) \
2749 CASE_MATHFN (LCEIL) \
2750 CASE_MATHFN (LDEXP) \
2751 CASE_MATHFN (LFLOOR) \
2752 CASE_MATHFN (LGAMMA) \
2753 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2754 CASE_MATHFN (LLCEIL) \
2755 CASE_MATHFN (LLFLOOR) \
2756 CASE_MATHFN (LLRINT) \
2757 CASE_MATHFN (LLROUND) \
2759 CASE_MATHFN (LOG10) \
2760 CASE_MATHFN (LOG1P) \
2761 CASE_MATHFN (LOG2) \
2762 CASE_MATHFN (LOGB) \
2763 CASE_MATHFN (LRINT) \
2764 CASE_MATHFN (LROUND) \
2765 CASE_MATHFN (MODF) \
2767 CASE_MATHFN (NANS) \
2768 CASE_MATHFN_FLOATN (NEARBYINT) \
2769 CASE_MATHFN (NEXTAFTER) \
2770 CASE_MATHFN (NEXTTOWARD) \
2772 CASE_MATHFN (POWI) \
2773 CASE_MATHFN (POW10) \
2774 CASE_MATHFN (REMAINDER) \
2775 CASE_MATHFN (REMQUO) \
2776 CASE_MATHFN_FLOATN (RINT) \
2777 CASE_MATHFN_FLOATN (ROUND) \
2778 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2779 CASE_MATHFN (SCALB) \
2780 CASE_MATHFN (SCALBLN) \
2781 CASE_MATHFN (SCALBN) \
2782 CASE_MATHFN (SIGNBIT) \
2783 CASE_MATHFN (SIGNIFICAND) \
2785 CASE_MATHFN (SINCOS) \
2786 CASE_MATHFN (SINH) \
2787 CASE_MATHFN_FLOATN (SQRT) \
2789 CASE_MATHFN (TANH) \
2790 CASE_MATHFN (TGAMMA) \
2791 CASE_MATHFN_FLOATN (TRUNC) \
2799 return END_BUILTINS
;
2802 mtype
= TYPE_MAIN_VARIANT (type
);
2803 if (mtype
== double_type_node
)
2805 else if (mtype
== float_type_node
)
2807 else if (mtype
== long_double_type_node
)
2809 else if (mtype
== float16_type_node
)
2811 else if (mtype
== float32_type_node
)
2813 else if (mtype
== float64_type_node
)
2815 else if (mtype
== float128_type_node
)
2817 else if (mtype
== float32x_type_node
)
2819 else if (mtype
== float64x_type_node
)
2821 else if (mtype
== float128x_type_node
)
2824 return END_BUILTINS
;
2828 #undef CASE_MATHFN_FLOATN
2829 #undef CASE_MATHFN_REENT
2831 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2832 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2833 otherwise use the explicit declaration. If we can't do the conversion,
2837 mathfn_built_in_1 (tree type
, combined_fn fn
, bool implicit_p
)
2839 built_in_function fcode2
= mathfn_built_in_2 (type
, fn
);
2840 if (fcode2
== END_BUILTINS
)
2843 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
2846 return builtin_decl_explicit (fcode2
);
2849 /* Like mathfn_built_in_1, but always use the implicit array. */
2852 mathfn_built_in (tree type
, combined_fn fn
)
2854 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
2857 /* Like mathfn_built_in_1, but take a built_in_function and
2858 always use the implicit array. */
2861 mathfn_built_in (tree type
, enum built_in_function fn
)
2863 return mathfn_built_in_1 (type
, as_combined_fn (fn
), /*implicit=*/ 1);
2866 /* Return the type associated with a built in function, i.e., the one
2867 to be passed to mathfn_built_in to get the type-specific
2871 mathfn_built_in_type (combined_fn fn
)
2873 #define CASE_MATHFN(MATHFN) \
2874 case CFN_BUILT_IN_##MATHFN: \
2875 return double_type_node; \
2876 case CFN_BUILT_IN_##MATHFN##F: \
2877 return float_type_node; \
2878 case CFN_BUILT_IN_##MATHFN##L: \
2879 return long_double_type_node;
2881 #define CASE_MATHFN_FLOATN(MATHFN) \
2882 CASE_MATHFN(MATHFN) \
2883 case CFN_BUILT_IN_##MATHFN##F16: \
2884 return float16_type_node; \
2885 case CFN_BUILT_IN_##MATHFN##F32: \
2886 return float32_type_node; \
2887 case CFN_BUILT_IN_##MATHFN##F64: \
2888 return float64_type_node; \
2889 case CFN_BUILT_IN_##MATHFN##F128: \
2890 return float128_type_node; \
2891 case CFN_BUILT_IN_##MATHFN##F32X: \
2892 return float32x_type_node; \
2893 case CFN_BUILT_IN_##MATHFN##F64X: \
2894 return float64x_type_node; \
2895 case CFN_BUILT_IN_##MATHFN##F128X: \
2896 return float128x_type_node;
2898 /* Similar to above, but appends _R after any F/L suffix. */
2899 #define CASE_MATHFN_REENT(MATHFN) \
2900 case CFN_BUILT_IN_##MATHFN##_R: \
2901 return double_type_node; \
2902 case CFN_BUILT_IN_##MATHFN##F_R: \
2903 return float_type_node; \
2904 case CFN_BUILT_IN_##MATHFN##L_R: \
2905 return long_double_type_node;
2916 #undef CASE_MATHFN_FLOATN
2917 #undef CASE_MATHFN_REENT
2918 #undef SEQ_OF_CASE_MATHFN
2921 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2922 return its code, otherwise return IFN_LAST. Note that this function
2923 only tests whether the function is defined in internals.def, not whether
2924 it is actually available on the target. */
2927 associated_internal_fn (tree fndecl
)
2929 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
);
2930 tree return_type
= TREE_TYPE (TREE_TYPE (fndecl
));
2931 switch (DECL_FUNCTION_CODE (fndecl
))
2933 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2934 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2935 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2936 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2937 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2938 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2939 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2940 #include "internal-fn.def"
2942 CASE_FLT_FN (BUILT_IN_POW10
):
2945 CASE_FLT_FN (BUILT_IN_DREM
):
2946 return IFN_REMAINDER
;
2948 CASE_FLT_FN (BUILT_IN_SCALBN
):
2949 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2950 if (REAL_MODE_FORMAT (TYPE_MODE (return_type
))->b
== 2)
2959 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2960 on the current target by a call to an internal function, return the
2961 code of that internal function, otherwise return IFN_LAST. The caller
2962 is responsible for ensuring that any side-effects of the built-in
2963 call are dealt with correctly. E.g. if CALL sets errno, the caller
2964 must decide that the errno result isn't needed or make it available
2965 in some other way. */
2968 replacement_internal_fn (gcall
*call
)
2970 if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
))
2972 internal_fn ifn
= associated_internal_fn (gimple_call_fndecl (call
));
2973 if (ifn
!= IFN_LAST
)
2975 tree_pair types
= direct_internal_fn_types (ifn
, call
);
2976 optimization_type opt_type
= bb_optimization_type (gimple_bb (call
));
2977 if (direct_internal_fn_supported_p (ifn
, types
, opt_type
))
2984 /* Expand a call to the builtin trinary math functions (fma).
2985 Return NULL_RTX if a normal call should be emitted rather than expanding the
2986 function in-line. EXP is the expression that is a call to the builtin
2987 function; if convenient, the result should be placed in TARGET.
2988 SUBTARGET may be used as the target for computing one of EXP's
2992 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2994 optab builtin_optab
;
2995 rtx op0
, op1
, op2
, result
;
2997 tree fndecl
= get_callee_fndecl (exp
);
2998 tree arg0
, arg1
, arg2
;
3001 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
3004 arg0
= CALL_EXPR_ARG (exp
, 0);
3005 arg1
= CALL_EXPR_ARG (exp
, 1);
3006 arg2
= CALL_EXPR_ARG (exp
, 2);
3008 switch (DECL_FUNCTION_CODE (fndecl
))
3010 CASE_FLT_FN (BUILT_IN_FMA
):
3011 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
3012 builtin_optab
= fma_optab
; break;
3017 /* Make a suitable register to place result in. */
3018 mode
= TYPE_MODE (TREE_TYPE (exp
));
3020 /* Before working hard, check whether the instruction is available. */
3021 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
3024 result
= gen_reg_rtx (mode
);
3026 /* Always stabilize the argument list. */
3027 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
3028 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
3029 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
3031 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
3032 op1
= expand_normal (arg1
);
3033 op2
= expand_normal (arg2
);
3037 /* Compute into RESULT.
3038 Set RESULT to wherever the result comes back. */
3039 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
3042 /* If we were unable to expand via the builtin, stop the sequence
3043 (without outputting the insns) and call to the library function
3044 with the stabilized argument list. */
3048 return expand_call (exp
, target
, target
== const0_rtx
);
3051 /* Output the entire sequence. */
3052 insns
= get_insns ();
3059 /* Expand a call to the builtin sin and cos math functions.
3060 Return NULL_RTX if a normal call should be emitted rather than expanding the
3061 function in-line. EXP is the expression that is a call to the builtin
3062 function; if convenient, the result should be placed in TARGET.
3063 SUBTARGET may be used as the target for computing one of EXP's
3067 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
3069 optab builtin_optab
;
3072 tree fndecl
= get_callee_fndecl (exp
);
3076 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3079 arg
= CALL_EXPR_ARG (exp
, 0);
3081 switch (DECL_FUNCTION_CODE (fndecl
))
3083 CASE_FLT_FN (BUILT_IN_SIN
):
3084 CASE_FLT_FN (BUILT_IN_COS
):
3085 builtin_optab
= sincos_optab
; break;
3090 /* Make a suitable register to place result in. */
3091 mode
= TYPE_MODE (TREE_TYPE (exp
));
3093 /* Check if sincos insn is available, otherwise fallback
3094 to sin or cos insn. */
3095 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
3096 switch (DECL_FUNCTION_CODE (fndecl
))
3098 CASE_FLT_FN (BUILT_IN_SIN
):
3099 builtin_optab
= sin_optab
; break;
3100 CASE_FLT_FN (BUILT_IN_COS
):
3101 builtin_optab
= cos_optab
; break;
3106 /* Before working hard, check whether the instruction is available. */
3107 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
3109 rtx result
= gen_reg_rtx (mode
);
3111 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3112 need to expand the argument again. This way, we will not perform
3113 side-effects more the once. */
3114 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3116 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
3120 /* Compute into RESULT.
3121 Set RESULT to wherever the result comes back. */
3122 if (builtin_optab
== sincos_optab
)
3126 switch (DECL_FUNCTION_CODE (fndecl
))
3128 CASE_FLT_FN (BUILT_IN_SIN
):
3129 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
3131 CASE_FLT_FN (BUILT_IN_COS
):
3132 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
3140 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
3144 /* Output the entire sequence. */
3145 insns
= get_insns ();
3151 /* If we were unable to expand via the builtin, stop the sequence
3152 (without outputting the insns) and call to the library function
3153 with the stabilized argument list. */
3157 return expand_call (exp
, target
, target
== const0_rtx
);
3160 /* Given an interclass math builtin decl FNDECL and it's argument ARG
3161 return an RTL instruction code that implements the functionality.
3162 If that isn't possible or available return CODE_FOR_nothing. */
3164 static enum insn_code
3165 interclass_mathfn_icode (tree arg
, tree fndecl
)
3167 bool errno_set
= false;
3168 optab builtin_optab
= unknown_optab
;
3171 switch (DECL_FUNCTION_CODE (fndecl
))
3173 CASE_FLT_FN (BUILT_IN_ILOGB
):
3174 errno_set
= true; builtin_optab
= ilogb_optab
; break;
3175 CASE_FLT_FN (BUILT_IN_ISINF
):
3176 builtin_optab
= isinf_optab
; break;
3177 case BUILT_IN_ISNORMAL
:
3178 case BUILT_IN_ISFINITE
:
3179 CASE_FLT_FN (BUILT_IN_FINITE
):
3180 case BUILT_IN_FINITED32
:
3181 case BUILT_IN_FINITED64
:
3182 case BUILT_IN_FINITED128
:
3183 case BUILT_IN_ISINFD32
:
3184 case BUILT_IN_ISINFD64
:
3185 case BUILT_IN_ISINFD128
:
3186 /* These builtins have no optabs (yet). */
3192 /* There's no easy way to detect the case we need to set EDOM. */
3193 if (flag_errno_math
&& errno_set
)
3194 return CODE_FOR_nothing
;
3196 /* Optab mode depends on the mode of the input argument. */
3197 mode
= TYPE_MODE (TREE_TYPE (arg
));
3200 return optab_handler (builtin_optab
, mode
);
3201 return CODE_FOR_nothing
;
3204 /* Expand a call to one of the builtin math functions that operate on
3205 floating point argument and output an integer result (ilogb, isinf,
3207 Return 0 if a normal call should be emitted rather than expanding the
3208 function in-line. EXP is the expression that is a call to the builtin
3209 function; if convenient, the result should be placed in TARGET. */
3212 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
3214 enum insn_code icode
= CODE_FOR_nothing
;
3216 tree fndecl
= get_callee_fndecl (exp
);
3220 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3223 arg
= CALL_EXPR_ARG (exp
, 0);
3224 icode
= interclass_mathfn_icode (arg
, fndecl
);
3225 mode
= TYPE_MODE (TREE_TYPE (arg
));
3227 if (icode
!= CODE_FOR_nothing
)
3229 class expand_operand ops
[1];
3230 rtx_insn
*last
= get_last_insn ();
3231 tree orig_arg
= arg
;
3233 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3234 need to expand the argument again. This way, we will not perform
3235 side-effects more the once. */
3236 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3238 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
3240 if (mode
!= GET_MODE (op0
))
3241 op0
= convert_to_mode (mode
, op0
, 0);
3243 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
3244 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
3245 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
3246 return ops
[0].value
;
3248 delete_insns_since (last
);
3249 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
3255 /* Expand a call to the builtin sincos math function.
3256 Return NULL_RTX if a normal call should be emitted rather than expanding the
3257 function in-line. EXP is the expression that is a call to the builtin
3261 expand_builtin_sincos (tree exp
)
3263 rtx op0
, op1
, op2
, target1
, target2
;
3265 tree arg
, sinp
, cosp
;
3267 location_t loc
= EXPR_LOCATION (exp
);
3268 tree alias_type
, alias_off
;
3270 if (!validate_arglist (exp
, REAL_TYPE
,
3271 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3274 arg
= CALL_EXPR_ARG (exp
, 0);
3275 sinp
= CALL_EXPR_ARG (exp
, 1);
3276 cosp
= CALL_EXPR_ARG (exp
, 2);
3278 /* Make a suitable register to place result in. */
3279 mode
= TYPE_MODE (TREE_TYPE (arg
));
3281 /* Check if sincos insn is available, otherwise emit the call. */
3282 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
3285 target1
= gen_reg_rtx (mode
);
3286 target2
= gen_reg_rtx (mode
);
3288 op0
= expand_normal (arg
);
3289 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
3290 alias_off
= build_int_cst (alias_type
, 0);
3291 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
3293 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
3296 /* Compute into target1 and target2.
3297 Set TARGET to wherever the result comes back. */
3298 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
3299 gcc_assert (result
);
3301 /* Move target1 and target2 to the memory locations indicated
3303 emit_move_insn (op1
, target1
);
3304 emit_move_insn (op2
, target2
);
3309 /* Expand a call to the internal cexpi builtin to the sincos math function.
3310 EXP is the expression that is a call to the builtin function; if convenient,
3311 the result should be placed in TARGET. */
3314 expand_builtin_cexpi (tree exp
, rtx target
)
3316 tree fndecl
= get_callee_fndecl (exp
);
3320 location_t loc
= EXPR_LOCATION (exp
);
3322 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3325 arg
= CALL_EXPR_ARG (exp
, 0);
3326 type
= TREE_TYPE (arg
);
3327 mode
= TYPE_MODE (TREE_TYPE (arg
));
3329 /* Try expanding via a sincos optab, fall back to emitting a libcall
3330 to sincos or cexp. We are sure we have sincos or cexp because cexpi
3331 is only generated from sincos, cexp or if we have either of them. */
3332 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
3334 op1
= gen_reg_rtx (mode
);
3335 op2
= gen_reg_rtx (mode
);
3337 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
3339 /* Compute into op1 and op2. */
3340 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
3342 else if (targetm
.libc_has_function (function_sincos
, type
))
3344 tree call
, fn
= NULL_TREE
;
3348 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
3349 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
3350 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
3351 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
3352 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
3353 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
3357 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
3358 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
3359 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
3360 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
3361 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
3362 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
3364 /* Make sure not to fold the sincos call again. */
3365 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
3366 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
3367 call
, 3, arg
, top1
, top2
));
3371 tree call
, fn
= NULL_TREE
, narg
;
3372 tree ctype
= build_complex_type (type
);
3374 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
3375 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
3376 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
3377 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
3378 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
3379 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
3383 /* If we don't have a decl for cexp create one. This is the
3384 friendliest fallback if the user calls __builtin_cexpi
3385 without full target C99 function support. */
3386 if (fn
== NULL_TREE
)
3389 const char *name
= NULL
;
3391 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
3393 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
3395 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
3398 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
3399 fn
= build_fn_decl (name
, fntype
);
3402 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
3403 build_real (type
, dconst0
), arg
);
3405 /* Make sure not to fold the cexp call again. */
3406 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
3407 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
3408 target
, VOIDmode
, EXPAND_NORMAL
);
3411 /* Now build the proper return type. */
3412 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
3413 make_tree (TREE_TYPE (arg
), op2
),
3414 make_tree (TREE_TYPE (arg
), op1
)),
3415 target
, VOIDmode
, EXPAND_NORMAL
);
3418 /* Conveniently construct a function call expression. FNDECL names the
3419 function to be called, N is the number of arguments, and the "..."
3420 parameters are the argument expressions. Unlike build_call_exr
3421 this doesn't fold the call, hence it will always return a CALL_EXPR. */
3424 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
3427 tree fntype
= TREE_TYPE (fndecl
);
3428 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
3431 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
3433 SET_EXPR_LOCATION (fn
, loc
);
3437 /* Expand a call to one of the builtin rounding functions gcc defines
3438 as an extension (lfloor and lceil). As these are gcc extensions we
3439 do not need to worry about setting errno to EDOM.
3440 If expanding via optab fails, lower expression to (int)(floor(x)).
3441 EXP is the expression that is a call to the builtin function;
3442 if convenient, the result should be placed in TARGET. */
3445 expand_builtin_int_roundingfn (tree exp
, rtx target
)
3447 convert_optab builtin_optab
;
3450 tree fndecl
= get_callee_fndecl (exp
);
3451 enum built_in_function fallback_fn
;
3452 tree fallback_fndecl
;
3456 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3459 arg
= CALL_EXPR_ARG (exp
, 0);
3461 switch (DECL_FUNCTION_CODE (fndecl
))
3463 CASE_FLT_FN (BUILT_IN_ICEIL
):
3464 CASE_FLT_FN (BUILT_IN_LCEIL
):
3465 CASE_FLT_FN (BUILT_IN_LLCEIL
):
3466 builtin_optab
= lceil_optab
;
3467 fallback_fn
= BUILT_IN_CEIL
;
3470 CASE_FLT_FN (BUILT_IN_IFLOOR
):
3471 CASE_FLT_FN (BUILT_IN_LFLOOR
):
3472 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
3473 builtin_optab
= lfloor_optab
;
3474 fallback_fn
= BUILT_IN_FLOOR
;
3481 /* Make a suitable register to place result in. */
3482 mode
= TYPE_MODE (TREE_TYPE (exp
));
3484 target
= gen_reg_rtx (mode
);
3486 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3487 need to expand the argument again. This way, we will not perform
3488 side-effects more the once. */
3489 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3491 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
3495 /* Compute into TARGET. */
3496 if (expand_sfix_optab (target
, op0
, builtin_optab
))
3498 /* Output the entire sequence. */
3499 insns
= get_insns ();
3505 /* If we were unable to expand via the builtin, stop the sequence
3506 (without outputting the insns). */
3509 /* Fall back to floating point rounding optab. */
3510 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
3512 /* For non-C99 targets we may end up without a fallback fndecl here
3513 if the user called __builtin_lfloor directly. In this case emit
3514 a call to the floor/ceil variants nevertheless. This should result
3515 in the best user experience for not full C99 targets. */
3516 if (fallback_fndecl
== NULL_TREE
)
3519 const char *name
= NULL
;
3521 switch (DECL_FUNCTION_CODE (fndecl
))
3523 case BUILT_IN_ICEIL
:
3524 case BUILT_IN_LCEIL
:
3525 case BUILT_IN_LLCEIL
:
3528 case BUILT_IN_ICEILF
:
3529 case BUILT_IN_LCEILF
:
3530 case BUILT_IN_LLCEILF
:
3533 case BUILT_IN_ICEILL
:
3534 case BUILT_IN_LCEILL
:
3535 case BUILT_IN_LLCEILL
:
3538 case BUILT_IN_IFLOOR
:
3539 case BUILT_IN_LFLOOR
:
3540 case BUILT_IN_LLFLOOR
:
3543 case BUILT_IN_IFLOORF
:
3544 case BUILT_IN_LFLOORF
:
3545 case BUILT_IN_LLFLOORF
:
3548 case BUILT_IN_IFLOORL
:
3549 case BUILT_IN_LFLOORL
:
3550 case BUILT_IN_LLFLOORL
:
3557 fntype
= build_function_type_list (TREE_TYPE (arg
),
3558 TREE_TYPE (arg
), NULL_TREE
);
3559 fallback_fndecl
= build_fn_decl (name
, fntype
);
3562 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
3564 tmp
= expand_normal (exp
);
3565 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
3567 /* Truncate the result of floating point optab to integer
3568 via expand_fix (). */
3569 target
= gen_reg_rtx (mode
);
3570 expand_fix (target
, tmp
, 0);
3575 /* Expand a call to one of the builtin math functions doing integer
3577 Return 0 if a normal call should be emitted rather than expanding the
3578 function in-line. EXP is the expression that is a call to the builtin
3579 function; if convenient, the result should be placed in TARGET. */
3582 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
3584 convert_optab builtin_optab
;
3587 tree fndecl
= get_callee_fndecl (exp
);
3590 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
3592 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3595 arg
= CALL_EXPR_ARG (exp
, 0);
3597 switch (DECL_FUNCTION_CODE (fndecl
))
3599 CASE_FLT_FN (BUILT_IN_IRINT
):
3600 fallback_fn
= BUILT_IN_LRINT
;
3602 CASE_FLT_FN (BUILT_IN_LRINT
):
3603 CASE_FLT_FN (BUILT_IN_LLRINT
):
3604 builtin_optab
= lrint_optab
;
3607 CASE_FLT_FN (BUILT_IN_IROUND
):
3608 fallback_fn
= BUILT_IN_LROUND
;
3610 CASE_FLT_FN (BUILT_IN_LROUND
):
3611 CASE_FLT_FN (BUILT_IN_LLROUND
):
3612 builtin_optab
= lround_optab
;
3619 /* There's no easy way to detect the case we need to set EDOM. */
3620 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
3623 /* Make a suitable register to place result in. */
3624 mode
= TYPE_MODE (TREE_TYPE (exp
));
3626 /* There's no easy way to detect the case we need to set EDOM. */
3627 if (!flag_errno_math
)
3629 rtx result
= gen_reg_rtx (mode
);
3631 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3632 need to expand the argument again. This way, we will not perform
3633 side-effects more the once. */
3634 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3636 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
3640 if (expand_sfix_optab (result
, op0
, builtin_optab
))
3642 /* Output the entire sequence. */
3643 insns
= get_insns ();
3649 /* If we were unable to expand via the builtin, stop the sequence
3650 (without outputting the insns) and call to the library function
3651 with the stabilized argument list. */
3655 if (fallback_fn
!= BUILT_IN_NONE
)
3657 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3658 targets, (int) round (x) should never be transformed into
3659 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3660 a call to lround in the hope that the target provides at least some
3661 C99 functions. This should result in the best user experience for
3662 not full C99 targets. */
3663 tree fallback_fndecl
= mathfn_built_in_1
3664 (TREE_TYPE (arg
), as_combined_fn (fallback_fn
), 0);
3666 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
3667 fallback_fndecl
, 1, arg
);
3669 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
3670 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
3671 return convert_to_mode (mode
, target
, 0);
3674 return expand_call (exp
, target
, target
== const0_rtx
);
3677 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3678 a normal call should be emitted rather than expanding the function
3679 in-line. EXP is the expression that is a call to the builtin
3680 function; if convenient, the result should be placed in TARGET. */
3683 expand_builtin_powi (tree exp
, rtx target
)
3690 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3693 arg0
= CALL_EXPR_ARG (exp
, 0);
3694 arg1
= CALL_EXPR_ARG (exp
, 1);
3695 mode
= TYPE_MODE (TREE_TYPE (exp
));
3697 /* Emit a libcall to libgcc. */
3699 /* Mode of the 2nd argument must match that of an int. */
3700 mode2
= int_mode_for_size (INT_TYPE_SIZE
, 0).require ();
3702 if (target
== NULL_RTX
)
3703 target
= gen_reg_rtx (mode
);
3705 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
3706 if (GET_MODE (op0
) != mode
)
3707 op0
= convert_to_mode (mode
, op0
, 0);
3708 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
3709 if (GET_MODE (op1
) != mode2
)
3710 op1
= convert_to_mode (mode2
, op1
, 0);
3712 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
3713 target
, LCT_CONST
, mode
,
3714 op0
, mode
, op1
, mode2
);
3719 /* Expand expression EXP which is a call to the strlen builtin. Return
3720 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3721 try to get the result in TARGET, if convenient. */
3724 expand_builtin_strlen (tree exp
, rtx target
,
3725 machine_mode target_mode
)
3727 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
3730 tree src
= CALL_EXPR_ARG (exp
, 0);
3731 if (!check_read_access (exp
, src
))
3734 /* If the length can be computed at compile-time, return it. */
3735 if (tree len
= c_strlen (src
, 0))
3736 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3738 /* If the length can be computed at compile-time and is constant
3739 integer, but there are side-effects in src, evaluate
3740 src for side-effects, then return len.
3741 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3742 can be optimized into: i++; x = 3; */
3743 tree len
= c_strlen (src
, 1);
3744 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3746 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3747 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3750 unsigned int align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3752 /* If SRC is not a pointer type, don't do this operation inline. */
3756 /* Bail out if we can't compute strlen in the right mode. */
3757 machine_mode insn_mode
;
3758 enum insn_code icode
= CODE_FOR_nothing
;
3759 FOR_EACH_MODE_FROM (insn_mode
, target_mode
)
3761 icode
= optab_handler (strlen_optab
, insn_mode
);
3762 if (icode
!= CODE_FOR_nothing
)
3765 if (insn_mode
== VOIDmode
)
3768 /* Make a place to hold the source address. We will not expand
3769 the actual source until we are sure that the expansion will
3770 not fail -- there are trees that cannot be expanded twice. */
3771 rtx src_reg
= gen_reg_rtx (Pmode
);
3773 /* Mark the beginning of the strlen sequence so we can emit the
3774 source operand later. */
3775 rtx_insn
*before_strlen
= get_last_insn ();
3777 class expand_operand ops
[4];
3778 create_output_operand (&ops
[0], target
, insn_mode
);
3779 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3780 create_integer_operand (&ops
[2], 0);
3781 create_integer_operand (&ops
[3], align
);
3782 if (!maybe_expand_insn (icode
, 4, ops
))
3785 /* Check to see if the argument was declared attribute nonstring
3786 and if so, issue a warning since at this point it's not known
3787 to be nul-terminated. */
3788 maybe_warn_nonstring_arg (get_callee_fndecl (exp
), exp
);
3790 /* Now that we are assured of success, expand the source. */
3792 rtx pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3795 #ifdef POINTERS_EXTEND_UNSIGNED
3796 if (GET_MODE (pat
) != Pmode
)
3797 pat
= convert_to_mode (Pmode
, pat
,
3798 POINTERS_EXTEND_UNSIGNED
);
3800 emit_move_insn (src_reg
, pat
);
3806 emit_insn_after (pat
, before_strlen
);
3808 emit_insn_before (pat
, get_insns ());
3810 /* Return the value in the proper mode for this function. */
3811 if (GET_MODE (ops
[0].value
) == target_mode
)
3812 target
= ops
[0].value
;
3813 else if (target
!= 0)
3814 convert_move (target
, ops
[0].value
, 0);
3816 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3821 /* Expand call EXP to the strnlen built-in, returning the result
3822 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3825 expand_builtin_strnlen (tree exp
, rtx target
, machine_mode target_mode
)
3827 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3830 tree src
= CALL_EXPR_ARG (exp
, 0);
3831 tree bound
= CALL_EXPR_ARG (exp
, 1);
3836 check_read_access (exp
, src
, bound
);
3838 location_t loc
= UNKNOWN_LOCATION
;
3839 if (EXPR_HAS_LOCATION (exp
))
3840 loc
= EXPR_LOCATION (exp
);
3842 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3843 so these conversions aren't necessary. */
3844 c_strlen_data lendata
= { };
3845 tree len
= c_strlen (src
, 0, &lendata
, 1);
3847 len
= fold_convert_loc (loc
, TREE_TYPE (bound
), len
);
3849 if (TREE_CODE (bound
) == INTEGER_CST
)
3854 len
= fold_build2_loc (loc
, MIN_EXPR
, size_type_node
, len
, bound
);
3855 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3858 if (TREE_CODE (bound
) != SSA_NAME
)
3863 get_global_range_query ()->range_of_expr (r
, bound
);
3864 if (r
.kind () != VR_RANGE
)
3866 min
= r
.lower_bound ();
3867 max
= r
.upper_bound ();
3869 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
3872 lendata
.decl
= unterminated_array (src
, &len
, &exact
);
3880 if (wi::gtu_p (min
, wi::to_wide (len
)))
3881 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3883 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
, bound
);
3884 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3887 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3888 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3889 a target constant. */
3892 builtin_memcpy_read_str (void *data
, void *, HOST_WIDE_INT offset
,
3893 scalar_int_mode mode
)
3895 /* The REPresentation pointed to by DATA need not be a nul-terminated
3896 string but the caller guarantees it's large enough for MODE. */
3897 const char *rep
= (const char *) data
;
3899 return c_readstr (rep
+ offset
, mode
, /*nul_terminated=*/false);
3902 /* LEN specify length of the block of memcpy/memset operation.
3903 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3904 In some cases we can make very likely guess on max size, then we
3905 set it into PROBABLE_MAX_SIZE. */
3908 determine_block_size (tree len
, rtx len_rtx
,
3909 unsigned HOST_WIDE_INT
*min_size
,
3910 unsigned HOST_WIDE_INT
*max_size
,
3911 unsigned HOST_WIDE_INT
*probable_max_size
)
3913 if (CONST_INT_P (len_rtx
))
3915 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3921 enum value_range_kind range_type
= VR_UNDEFINED
;
3923 /* Determine bounds from the type. */
3924 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3925 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3928 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3929 *probable_max_size
= *max_size
3930 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3932 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3934 if (TREE_CODE (len
) == SSA_NAME
)
3937 get_global_range_query ()->range_of_expr (r
, len
);
3938 range_type
= r
.kind ();
3939 if (range_type
!= VR_UNDEFINED
)
3941 min
= wi::to_wide (r
.min ());
3942 max
= wi::to_wide (r
.max ());
3945 if (range_type
== VR_RANGE
)
3947 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3948 *min_size
= min
.to_uhwi ();
3949 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3950 *probable_max_size
= *max_size
= max
.to_uhwi ();
3952 else if (range_type
== VR_ANTI_RANGE
)
3960 Produce anti range allowing negative values of N. We still
3961 can use the information and make a guess that N is not negative.
3963 if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3964 *probable_max_size
= min
.to_uhwi () - 1;
3967 gcc_checking_assert (*max_size
<=
3968 (unsigned HOST_WIDE_INT
)
3969 GET_MODE_MASK (GET_MODE (len_rtx
)));
3972 /* Issue a warning OPT for a bounded call EXP with a bound in RANGE
3973 accessing an object with SIZE. */
3976 maybe_warn_for_bound (opt_code opt
, location_t loc
, tree exp
, tree func
,
3977 tree bndrng
[2], tree size
, const access_data
*pad
= NULL
)
3979 if (!bndrng
[0] || warning_suppressed_p (exp
, opt
))
3982 tree maxobjsize
= max_object_size ();
3984 bool warned
= false;
3986 if (opt
== OPT_Wstringop_overread
)
3988 bool maybe
= pad
&& pad
->src
.phi ();
3990 if (tree_int_cst_lt (maxobjsize
, bndrng
[0]))
3992 if (bndrng
[0] == bndrng
[1])
3994 ? warning_at (loc
, opt
,
3996 ? G_("%qD specified bound %E may "
3997 "exceed maximum object size %E")
3998 : G_("%qD specified bound %E "
3999 "exceeds maximum object size %E")),
4000 func
, bndrng
[0], maxobjsize
)
4001 : warning_at (loc
, opt
,
4003 ? G_("specified bound %E may "
4004 "exceed maximum object size %E")
4005 : G_("specified bound %E "
4006 "exceeds maximum object size %E")),
4007 bndrng
[0], maxobjsize
));
4010 ? warning_at (loc
, opt
,
4012 ? G_("%qD specified bound [%E, %E] may "
4013 "exceed maximum object size %E")
4014 : G_("%qD specified bound [%E, %E] "
4015 "exceeds maximum object size %E")),
4016 func
, bndrng
[0], bndrng
[1], maxobjsize
)
4017 : warning_at (loc
, opt
,
4019 ? G_("specified bound [%E, %E] may "
4020 "exceed maximum object size %E")
4021 : G_("specified bound [%E, %E] "
4022 "exceeds maximum object size %E")),
4023 bndrng
[0], bndrng
[1], maxobjsize
));
4025 else if (!size
|| tree_int_cst_le (bndrng
[0], size
))
4027 else if (tree_int_cst_equal (bndrng
[0], bndrng
[1]))
4029 ? warning_at (loc
, opt
,
4031 ? G_("%qD specified bound %E may exceed "
4033 : G_("%qD specified bound %E exceeds "
4035 func
, bndrng
[0], size
)
4036 : warning_at (loc
, opt
,
4038 ? G_("specified bound %E may exceed "
4040 : G_("specified bound %E exceeds "
4045 ? warning_at (loc
, opt
,
4047 ? G_("%qD specified bound [%E, %E] may "
4048 "exceed source size %E")
4049 : G_("%qD specified bound [%E, %E] exceeds "
4051 func
, bndrng
[0], bndrng
[1], size
)
4052 : warning_at (loc
, opt
,
4054 ? G_("specified bound [%E, %E] may exceed "
4056 : G_("specified bound [%E, %E] exceeds "
4058 bndrng
[0], bndrng
[1], size
));
4061 if (pad
&& pad
->src
.ref
)
4063 if (DECL_P (pad
->src
.ref
))
4064 inform (DECL_SOURCE_LOCATION (pad
->src
.ref
),
4065 "source object declared here");
4066 else if (EXPR_HAS_LOCATION (pad
->src
.ref
))
4067 inform (EXPR_LOCATION (pad
->src
.ref
),
4068 "source object allocated here");
4070 suppress_warning (exp
, opt
);
4076 bool maybe
= pad
&& pad
->dst
.phi ();
4077 if (tree_int_cst_lt (maxobjsize
, bndrng
[0]))
4079 if (bndrng
[0] == bndrng
[1])
4081 ? warning_at (loc
, opt
,
4083 ? G_("%qD specified size %E may "
4084 "exceed maximum object size %E")
4085 : G_("%qD specified size %E "
4086 "exceeds maximum object size %E")),
4087 func
, bndrng
[0], maxobjsize
)
4088 : warning_at (loc
, opt
,
4090 ? G_("specified size %E may exceed "
4091 "maximum object size %E")
4092 : G_("specified size %E exceeds "
4093 "maximum object size %E")),
4094 bndrng
[0], maxobjsize
));
4097 ? warning_at (loc
, opt
,
4099 ? G_("%qD specified size between %E and %E "
4100 "may exceed maximum object size %E")
4101 : G_("%qD specified size between %E and %E "
4102 "exceeds maximum object size %E")),
4103 func
, bndrng
[0], bndrng
[1], maxobjsize
)
4104 : warning_at (loc
, opt
,
4106 ? G_("specified size between %E and %E "
4107 "may exceed maximum object size %E")
4108 : G_("specified size between %E and %E "
4109 "exceeds maximum object size %E")),
4110 bndrng
[0], bndrng
[1], maxobjsize
));
4112 else if (!size
|| tree_int_cst_le (bndrng
[0], size
))
4114 else if (tree_int_cst_equal (bndrng
[0], bndrng
[1]))
4116 ? warning_at (loc
, opt
,
4118 ? G_("%qD specified bound %E may exceed "
4119 "destination size %E")
4120 : G_("%qD specified bound %E exceeds "
4121 "destination size %E")),
4122 func
, bndrng
[0], size
)
4123 : warning_at (loc
, opt
,
4125 ? G_("specified bound %E may exceed "
4126 "destination size %E")
4127 : G_("specified bound %E exceeds "
4128 "destination size %E")),
4132 ? warning_at (loc
, opt
,
4134 ? G_("%qD specified bound [%E, %E] may exceed "
4135 "destination size %E")
4136 : G_("%qD specified bound [%E, %E] exceeds "
4137 "destination size %E")),
4138 func
, bndrng
[0], bndrng
[1], size
)
4139 : warning_at (loc
, opt
,
4141 ? G_("specified bound [%E, %E] exceeds "
4142 "destination size %E")
4143 : G_("specified bound [%E, %E] exceeds "
4144 "destination size %E")),
4145 bndrng
[0], bndrng
[1], size
));
4149 if (pad
&& pad
->dst
.ref
)
4151 if (DECL_P (pad
->dst
.ref
))
4152 inform (DECL_SOURCE_LOCATION (pad
->dst
.ref
),
4153 "destination object declared here");
4154 else if (EXPR_HAS_LOCATION (pad
->dst
.ref
))
4155 inform (EXPR_LOCATION (pad
->dst
.ref
),
4156 "destination object allocated here");
4158 suppress_warning (exp
, opt
);
4164 /* For an expression EXP issue an access warning controlled by option OPT
4165 with access to a region SIZE bytes in size in the RANGE of sizes.
4166 WRITE is true for a write access, READ for a read access, neither for
4167 call that may or may not perform an access but for which the range
4168 is expected to valid.
4169 Returns true when a warning has been issued. */
4172 warn_for_access (location_t loc
, tree func
, tree exp
, int opt
, tree range
[2],
4173 tree size
, bool write
, bool read
, bool maybe
)
4175 bool warned
= false;
4179 if (tree_int_cst_equal (range
[0], range
[1]))
4181 ? warning_n (loc
, opt
, tree_to_uhwi (range
[0]),
4183 ? G_("%qD may access %E byte in a region "
4185 : G_("%qD accessing %E byte in a region "
4188 ? G_ ("%qD may access %E bytes in a region "
4190 : G_ ("%qD accessing %E bytes in a region "
4192 func
, range
[0], size
)
4193 : warning_n (loc
, opt
, tree_to_uhwi (range
[0]),
4195 ? G_("may access %E byte in a region "
4197 : G_("accessing %E byte in a region "
4200 ? G_("may access %E bytes in a region "
4202 : G_("accessing %E bytes in a region "
4205 else if (tree_int_cst_sign_bit (range
[1]))
4207 /* Avoid printing the upper bound if it's invalid. */
4209 ? warning_at (loc
, opt
,
4211 ? G_("%qD may access %E or more bytes "
4212 "in a region of size %E")
4213 : G_("%qD accessing %E or more bytes "
4214 "in a region of size %E")),
4215 func
, range
[0], size
)
4216 : warning_at (loc
, opt
,
4218 ? G_("may access %E or more bytes "
4219 "in a region of size %E")
4220 : G_("accessing %E or more bytes "
4221 "in a region of size %E")),
4226 ? warning_at (loc
, opt
,
4228 ? G_("%qD may access between %E and %E "
4229 "bytes in a region of size %E")
4230 : G_("%qD accessing between %E and %E "
4231 "bytes in a region of size %E")),
4232 func
, range
[0], range
[1], size
)
4233 : warning_at (loc
, opt
,
4235 ? G_("may access between %E and %E bytes "
4236 "in a region of size %E")
4237 : G_("accessing between %E and %E bytes "
4238 "in a region of size %E")),
4239 range
[0], range
[1], size
));
4245 if (tree_int_cst_equal (range
[0], range
[1]))
4247 ? warning_n (loc
, opt
, tree_to_uhwi (range
[0]),
4249 ? G_("%qD may write %E byte into a region "
4251 : G_("%qD writing %E byte into a region "
4252 "of size %E overflows the destination")),
4254 ? G_("%qD may write %E bytes into a region "
4256 : G_("%qD writing %E bytes into a region "
4257 "of size %E overflows the destination")),
4258 func
, range
[0], size
)
4259 : warning_n (loc
, opt
, tree_to_uhwi (range
[0]),
4261 ? G_("may write %E byte into a region "
4263 : G_("writing %E byte into a region "
4264 "of size %E overflows the destination")),
4266 ? G_("may write %E bytes into a region "
4268 : G_("writing %E bytes into a region "
4269 "of size %E overflows the destination")),
4271 else if (tree_int_cst_sign_bit (range
[1]))
4273 /* Avoid printing the upper bound if it's invalid. */
4275 ? warning_at (loc
, opt
,
4277 ? G_("%qD may write %E or more bytes "
4278 "into a region of size %E")
4279 : G_("%qD writing %E or more bytes "
4280 "into a region of size %E overflows "
4281 "the destination")),
4282 func
, range
[0], size
)
4283 : warning_at (loc
, opt
,
4285 ? G_("may write %E or more bytes into "
4286 "a region of size %E")
4287 : G_("writing %E or more bytes into "
4288 "a region of size %E overflows "
4289 "the destination")),
4294 ? warning_at (loc
, opt
,
4296 ? G_("%qD may write between %E and %E bytes "
4297 "into a region of size %E")
4298 : G_("%qD writing between %E and %E bytes "
4299 "into a region of size %E overflows "
4300 "the destination")),
4301 func
, range
[0], range
[1], size
)
4302 : warning_at (loc
, opt
,
4304 ? G_("may write between %E and %E bytes "
4305 "into a region of size %E")
4306 : G_("writing between %E and %E bytes "
4307 "into a region of size %E overflows "
4308 "the destination")),
4309 range
[0], range
[1], size
));
4315 if (tree_int_cst_equal (range
[0], range
[1]))
4317 ? warning_n (loc
, OPT_Wstringop_overread
,
4318 tree_to_uhwi (range
[0]),
4320 ? G_("%qD may read %E byte from a region "
4322 : G_("%qD reading %E byte from a region "
4325 ? G_("%qD may read %E bytes from a region "
4327 : G_("%qD reading %E bytes from a region "
4329 func
, range
[0], size
)
4330 : warning_n (loc
, OPT_Wstringop_overread
,
4331 tree_to_uhwi (range
[0]),
4333 ? G_("may read %E byte from a region "
4335 : G_("reading %E byte from a region "
4338 ? G_("may read %E bytes from a region "
4340 : G_("reading %E bytes from a region "
4343 else if (tree_int_cst_sign_bit (range
[1]))
4345 /* Avoid printing the upper bound if it's invalid. */
4347 ? warning_at (loc
, OPT_Wstringop_overread
,
4349 ? G_("%qD may read %E or more bytes "
4350 "from a region of size %E")
4351 : G_("%qD reading %E or more bytes "
4352 "from a region of size %E")),
4353 func
, range
[0], size
)
4354 : warning_at (loc
, OPT_Wstringop_overread
,
4356 ? G_("may read %E or more bytes "
4357 "from a region of size %E")
4358 : G_("reading %E or more bytes "
4359 "from a region of size %E")),
4364 ? warning_at (loc
, OPT_Wstringop_overread
,
4366 ? G_("%qD may read between %E and %E bytes "
4367 "from a region of size %E")
4368 : G_("%qD reading between %E and %E bytes "
4369 "from a region of size %E")),
4370 func
, range
[0], range
[1], size
)
4371 : warning_at (loc
, opt
,
4373 ? G_("may read between %E and %E bytes "
4374 "from a region of size %E")
4375 : G_("reading between %E and %E bytes "
4376 "from a region of size %E")),
4377 range
[0], range
[1], size
));
4380 suppress_warning (exp
, OPT_Wstringop_overread
);
4385 if (tree_int_cst_equal (range
[0], range
[1])
4386 || tree_int_cst_sign_bit (range
[1]))
4388 ? warning_n (loc
, OPT_Wstringop_overread
,
4389 tree_to_uhwi (range
[0]),
4390 "%qD expecting %E byte in a region of size %E",
4391 "%qD expecting %E bytes in a region of size %E",
4392 func
, range
[0], size
)
4393 : warning_n (loc
, OPT_Wstringop_overread
,
4394 tree_to_uhwi (range
[0]),
4395 "expecting %E byte in a region of size %E",
4396 "expecting %E bytes in a region of size %E",
4398 else if (tree_int_cst_sign_bit (range
[1]))
4400 /* Avoid printing the upper bound if it's invalid. */
4402 ? warning_at (loc
, OPT_Wstringop_overread
,
4403 "%qD expecting %E or more bytes in a region "
4405 func
, range
[0], size
)
4406 : warning_at (loc
, OPT_Wstringop_overread
,
4407 "expecting %E or more bytes in a region "
4413 ? warning_at (loc
, OPT_Wstringop_overread
,
4414 "%qD expecting between %E and %E bytes in "
4415 "a region of size %E",
4416 func
, range
[0], range
[1], size
)
4417 : warning_at (loc
, OPT_Wstringop_overread
,
4418 "expecting between %E and %E bytes in "
4419 "a region of size %E",
4420 range
[0], range
[1], size
));
4423 suppress_warning (exp
, OPT_Wstringop_overread
);
4428 /* Issue one inform message describing each target of an access REF.
4429 WRITE is set for a write access and clear for a read access. */
4432 access_ref::inform_access (access_mode mode
) const
4434 const access_ref
&aref
= *this;
4440 /* Set MAXREF to refer to the largest object and fill ALL_REFS
4441 with data for all objects referenced by the PHI arguments. */
4443 auto_vec
<access_ref
> all_refs
;
4444 if (!get_ref (&all_refs
, &maxref
))
4447 /* Except for MAXREF, the rest of the arguments' offsets need not
4448 reflect one added to the PHI itself. Determine the latter from
4449 MAXREF on which the result is based. */
4450 const offset_int orng
[] =
4452 offrng
[0] - maxref
.offrng
[0],
4453 wi::smax (offrng
[1] - maxref
.offrng
[1], offrng
[0]),
4456 /* Add the final PHI's offset to that of each of the arguments
4457 and recurse to issue an inform message for it. */
4458 for (unsigned i
= 0; i
!= all_refs
.length (); ++i
)
4460 /* Skip any PHIs; those could lead to infinite recursion. */
4461 if (all_refs
[i
].phi ())
4464 all_refs
[i
].add_offset (orng
[0], orng
[1]);
4465 all_refs
[i
].inform_access (mode
);
4470 /* Convert offset range and avoid including a zero range since it
4471 isn't necessarily meaningful. */
4472 HOST_WIDE_INT diff_min
= tree_to_shwi (TYPE_MIN_VALUE (ptrdiff_type_node
));
4473 HOST_WIDE_INT diff_max
= tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node
));
4474 HOST_WIDE_INT minoff
;
4475 HOST_WIDE_INT maxoff
= diff_max
;
4476 if (wi::fits_shwi_p (aref
.offrng
[0]))
4477 minoff
= aref
.offrng
[0].to_shwi ();
4479 minoff
= aref
.offrng
[0] < 0 ? diff_min
: diff_max
;
4481 if (wi::fits_shwi_p (aref
.offrng
[1]))
4482 maxoff
= aref
.offrng
[1].to_shwi ();
4484 if (maxoff
<= diff_min
|| maxoff
>= diff_max
)
4485 /* Avoid mentioning an upper bound that's equal to or in excess
4486 of the maximum of ptrdiff_t. */
4489 /* Convert size range and always include it since all sizes are
4491 unsigned long long minsize
= 0, maxsize
= 0;
4492 if (wi::fits_shwi_p (aref
.sizrng
[0])
4493 && wi::fits_shwi_p (aref
.sizrng
[1]))
4495 minsize
= aref
.sizrng
[0].to_shwi ();
4496 maxsize
= aref
.sizrng
[1].to_shwi ();
4499 /* SIZRNG doesn't necessarily have the same range as the allocation
4500 size determined by gimple_call_alloc_size (). */
4502 if (minsize
== maxsize
)
4503 sprintf (sizestr
, "%llu", minsize
);
4505 sprintf (sizestr
, "[%llu, %llu]", minsize
, maxsize
);
4509 && (maxoff
== 0 || aref
.sizrng
[1] <= maxoff
))
4511 else if (minoff
== maxoff
)
4512 sprintf (offstr
, "%lli", (long long) minoff
);
4514 sprintf (offstr
, "[%lli, %lli]", (long long) minoff
, (long long) maxoff
);
4516 location_t loc
= UNKNOWN_LOCATION
;
4518 tree ref
= this->ref
;
4519 tree allocfn
= NULL_TREE
;
4520 if (TREE_CODE (ref
) == SSA_NAME
)
4522 gimple
*stmt
= SSA_NAME_DEF_STMT (ref
);
4523 if (is_gimple_call (stmt
))
4525 loc
= gimple_location (stmt
);
4526 if (gimple_call_builtin_p (stmt
, BUILT_IN_ALLOCA_WITH_ALIGN
))
4528 /* Strip the SSA_NAME suffix from the variable name and
4529 recreate an identifier with the VLA's original name. */
4530 ref
= gimple_call_lhs (stmt
);
4531 if (SSA_NAME_IDENTIFIER (ref
))
4533 ref
= SSA_NAME_IDENTIFIER (ref
);
4534 const char *id
= IDENTIFIER_POINTER (ref
);
4535 size_t len
= strcspn (id
, ".$");
4538 ref
= get_identifier_with_length (id
, len
);
4543 /* Except for VLAs, retrieve the allocation function. */
4544 allocfn
= gimple_call_fndecl (stmt
);
4546 allocfn
= gimple_call_fn (stmt
);
4547 if (TREE_CODE (allocfn
) == SSA_NAME
)
4549 /* For an ALLOC_CALL via a function pointer make a small
4550 effort to determine the destination of the pointer. */
4551 gimple
*def
= SSA_NAME_DEF_STMT (allocfn
);
4552 if (gimple_assign_single_p (def
))
4554 tree rhs
= gimple_assign_rhs1 (def
);
4557 else if (TREE_CODE (rhs
) == COMPONENT_REF
)
4558 allocfn
= TREE_OPERAND (rhs
, 1);
4563 else if (gimple_nop_p (stmt
))
4564 /* Handle DECL_PARM below. */
4565 ref
= SSA_NAME_VAR (ref
);
4569 loc
= DECL_SOURCE_LOCATION (ref
);
4570 else if (EXPR_P (ref
) && EXPR_HAS_LOCATION (ref
))
4571 loc
= EXPR_LOCATION (ref
);
4572 else if (TREE_CODE (ref
) != IDENTIFIER_NODE
4573 && TREE_CODE (ref
) != SSA_NAME
)
4576 if (mode
== access_read_write
|| mode
== access_write_only
)
4578 if (allocfn
== NULL_TREE
)
4581 inform (loc
, "at offset %s into destination object %qE of size %s",
4582 offstr
, ref
, sizestr
);
4584 inform (loc
, "destination object %qE of size %s", ref
, sizestr
);
4590 "at offset %s into destination object of size %s "
4591 "allocated by %qE", offstr
, sizestr
, allocfn
);
4593 inform (loc
, "destination object of size %s allocated by %qE",
4598 if (mode
== access_read_only
)
4600 if (allocfn
== NULL_TREE
)
4603 inform (loc
, "at offset %s into source object %qE of size %s",
4604 offstr
, ref
, sizestr
);
4606 inform (loc
, "source object %qE of size %s", ref
, sizestr
);
4613 "at offset %s into source object of size %s allocated by %qE",
4614 offstr
, sizestr
, allocfn
);
4616 inform (loc
, "source object of size %s allocated by %qE",
4621 if (allocfn
== NULL_TREE
)
4624 inform (loc
, "at offset %s into object %qE of size %s",
4625 offstr
, ref
, sizestr
);
4627 inform (loc
, "object %qE of size %s", ref
, sizestr
);
4634 "at offset %s into object of size %s allocated by %qE",
4635 offstr
, sizestr
, allocfn
);
4637 inform (loc
, "object of size %s allocated by %qE",
4641 /* Helper to set RANGE to the range of BOUND if it's nonnull, bounded
4642 by BNDRNG if nonnull and valid. */
4645 get_size_range (tree bound
, tree range
[2], const offset_int bndrng
[2])
4648 get_size_range (bound
, range
);
4650 if (!bndrng
|| (bndrng
[0] == 0 && bndrng
[1] == HOST_WIDE_INT_M1U
))
4653 if (range
[0] && TREE_CODE (range
[0]) == INTEGER_CST
)
4656 { wi::to_offset (range
[0]), wi::to_offset (range
[1]) };
4657 if (r
[0] < bndrng
[0])
4658 range
[0] = wide_int_to_tree (sizetype
, bndrng
[0]);
4659 if (bndrng
[1] < r
[1])
4660 range
[1] = wide_int_to_tree (sizetype
, bndrng
[1]);
4664 range
[0] = wide_int_to_tree (sizetype
, bndrng
[0]);
4665 range
[1] = wide_int_to_tree (sizetype
, bndrng
[1]);
4669 /* Try to verify that the sizes and lengths of the arguments to a string
4670 manipulation function given by EXP are within valid bounds and that
4671 the operation does not lead to buffer overflow or read past the end.
4672 Arguments other than EXP may be null. When non-null, the arguments
4673 have the following meaning:
4674 DST is the destination of a copy call or NULL otherwise.
4675 SRC is the source of a copy call or NULL otherwise.
4676 DSTWRITE is the number of bytes written into the destination obtained
4677 from the user-supplied size argument to the function (such as in
4678 memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
4679 MAXREAD is the user-supplied bound on the length of the source sequence
4680 (such as in strncat(d, s, N). It specifies the upper limit on the number
4681 of bytes to write. If NULL, it's taken to be the same as DSTWRITE.
4682 SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
4683 expression EXP is a string function call (as opposed to a memory call
4684 like memcpy). As an exception, SRCSTR can also be an integer denoting
4685 the precomputed size of the source string or object (for functions like
4687 DSTSIZE is the size of the destination object.
4689 When DSTWRITE is null LEN is checked to verify that it doesn't exceed
4692 WRITE is true for write accesses, READ is true for reads. Both are
4693 false for simple size checks in calls to functions that neither read
4694 from nor write to the region.
4696 When nonnull, PAD points to a more detailed description of the access.
4698 If the call is successfully verified as safe return true, otherwise
4702 check_access (tree exp
, tree dstwrite
,
4703 tree maxread
, tree srcstr
, tree dstsize
,
4704 access_mode mode
, const access_data
*pad
/* = NULL */)
4706 /* The size of the largest object is half the address space, or
4707 PTRDIFF_MAX. (This is way too permissive.) */
4708 tree maxobjsize
= max_object_size ();
4710 /* Either an approximate/minimum the length of the source string for
4711 string functions or the size of the source object for raw memory
4713 tree slen
= NULL_TREE
;
4715 /* The range of the access in bytes; first set to the write access
4716 for functions that write and then read for those that also (or
4718 tree range
[2] = { NULL_TREE
, NULL_TREE
};
4720 /* Set to true when the exact number of bytes written by a string
4721 function like strcpy is not known and the only thing that is
4722 known is that it must be at least one (for the terminating nul). */
4723 bool at_least_one
= false;
4726 /* SRCSTR is normally a pointer to string but as a special case
4727 it can be an integer denoting the length of a string. */
4728 if (POINTER_TYPE_P (TREE_TYPE (srcstr
)))
4730 if (!check_nul_terminated_array (exp
, srcstr
, maxread
))
4732 /* Try to determine the range of lengths the source string
4733 refers to. If it can be determined and is less than
4734 the upper bound given by MAXREAD add one to it for
4735 the terminating nul. Otherwise, set it to one for
4736 the same reason, or to MAXREAD as appropriate. */
4737 c_strlen_data lendata
= { };
4738 get_range_strlen (srcstr
, &lendata
, /* eltsize = */ 1);
4739 range
[0] = lendata
.minlen
;
4740 range
[1] = lendata
.maxbound
? lendata
.maxbound
: lendata
.maxlen
;
4742 && TREE_CODE (range
[0]) == INTEGER_CST
4743 && TREE_CODE (range
[1]) == INTEGER_CST
4744 && (!maxread
|| TREE_CODE (maxread
) == INTEGER_CST
))
4746 if (maxread
&& tree_int_cst_le (maxread
, range
[0]))
4747 range
[0] = range
[1] = maxread
;
4749 range
[0] = fold_build2 (PLUS_EXPR
, size_type_node
,
4750 range
[0], size_one_node
);
4752 if (maxread
&& tree_int_cst_le (maxread
, range
[1]))
4754 else if (!integer_all_onesp (range
[1]))
4755 range
[1] = fold_build2 (PLUS_EXPR
, size_type_node
,
4756 range
[1], size_one_node
);
4762 at_least_one
= true;
4763 slen
= size_one_node
;
4770 if (!dstwrite
&& !maxread
)
4772 /* When the only available piece of data is the object size
4773 there is nothing to do. */
4777 /* Otherwise, when the length of the source sequence is known
4778 (as with strlen), set DSTWRITE to it. */
4784 dstsize
= maxobjsize
;
4786 /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST.BNDRNG
4788 get_size_range (dstwrite
, range
, pad
? pad
->dst
.bndrng
: NULL
);
4790 tree func
= get_callee_fndecl (exp
);
4791 /* Read vs write access by built-ins can be determined from the const
4792 qualifiers on the pointer argument. In the absence of attribute
4793 access, non-const qualified pointer arguments to user-defined
4794 functions are assumed to both read and write the objects. */
4795 const bool builtin
= func
? fndecl_built_in_p (func
) : false;
4797 /* First check the number of bytes to be written against the maximum
4800 && TREE_CODE (range
[0]) == INTEGER_CST
4801 && tree_int_cst_lt (maxobjsize
, range
[0]))
4803 location_t loc
= EXPR_LOCATION (exp
);
4804 maybe_warn_for_bound (OPT_Wstringop_overflow_
, loc
, exp
, func
, range
,
4809 /* The number of bytes to write is "exact" if DSTWRITE is non-null,
4810 constant, and in range of unsigned HOST_WIDE_INT. */
4811 bool exactwrite
= dstwrite
&& tree_fits_uhwi_p (dstwrite
);
4813 /* Next check the number of bytes to be written against the destination
4815 if (range
[0] || !exactwrite
|| integer_all_onesp (dstwrite
))
4818 && TREE_CODE (range
[0]) == INTEGER_CST
4819 && ((tree_fits_uhwi_p (dstsize
)
4820 && tree_int_cst_lt (dstsize
, range
[0]))
4822 && tree_fits_uhwi_p (dstwrite
)
4823 && tree_int_cst_lt (dstwrite
, range
[0]))))
4825 const opt_code opt
= OPT_Wstringop_overflow_
;
4826 if (warning_suppressed_p (exp
, opt
)
4827 || (pad
&& pad
->dst
.ref
4828 && warning_suppressed_p (pad
->dst
.ref
, opt
)))
4831 location_t loc
= EXPR_LOCATION (exp
);
4832 bool warned
= false;
4833 if (dstwrite
== slen
&& at_least_one
)
4835 /* This is a call to strcpy with a destination of 0 size
4836 and a source of unknown length. The call will write
4837 at least one byte past the end of the destination. */
4839 ? warning_at (loc
, opt
,
4840 "%qD writing %E or more bytes into "
4841 "a region of size %E overflows "
4843 func
, range
[0], dstsize
)
4844 : warning_at (loc
, opt
,
4845 "writing %E or more bytes into "
4846 "a region of size %E overflows "
4848 range
[0], dstsize
));
4853 = mode
== access_read_only
|| mode
== access_read_write
;
4855 = mode
== access_write_only
|| mode
== access_read_write
;
4856 const bool maybe
= pad
&& pad
->dst
.parmarray
;
4857 warned
= warn_for_access (loc
, func
, exp
,
4858 OPT_Wstringop_overflow_
,
4860 write
, read
&& !builtin
, maybe
);
4865 suppress_warning (exp
, OPT_Wstringop_overflow_
);
4867 pad
->dst
.inform_access (pad
->mode
);
4870 /* Return error when an overflow has been detected. */
4875 /* Check the maximum length of the source sequence against the size
4876 of the destination object if known, or against the maximum size
4880 /* Set RANGE to that of MAXREAD, bounded by PAD->SRC.BNDRNG if
4881 PAD is nonnull and BNDRNG is valid. */
4882 get_size_range (maxread
, range
, pad
? pad
->src
.bndrng
: NULL
);
4884 location_t loc
= EXPR_LOCATION (exp
);
4885 tree size
= dstsize
;
4886 if (pad
&& pad
->mode
== access_read_only
)
4887 size
= wide_int_to_tree (sizetype
, pad
->src
.sizrng
[1]);
4889 if (range
[0] && maxread
&& tree_fits_uhwi_p (size
))
4891 if (tree_int_cst_lt (maxobjsize
, range
[0]))
4893 maybe_warn_for_bound (OPT_Wstringop_overread
, loc
, exp
, func
,
4898 if (size
!= maxobjsize
&& tree_int_cst_lt (size
, range
[0]))
4900 opt_code opt
= (dstwrite
|| mode
!= access_read_only
4901 ? OPT_Wstringop_overflow_
4902 : OPT_Wstringop_overread
);
4903 maybe_warn_for_bound (opt
, loc
, exp
, func
, range
, size
, pad
);
4908 maybe_warn_nonstring_arg (func
, exp
);
4911 /* Check for reading past the end of SRC. */
4912 bool overread
= (slen
4916 && TREE_CODE (slen
) == INTEGER_CST
4917 && tree_int_cst_lt (slen
, range
[0]));
4918 /* If none is determined try to get a better answer based on the details
4922 && pad
->src
.sizrng
[1] >= 0
4923 && pad
->src
.offrng
[0] >= 0
4924 && (pad
->src
.offrng
[1] < 0
4925 || pad
->src
.offrng
[0] <= pad
->src
.offrng
[1]))
4927 /* Set RANGE to that of MAXREAD, bounded by PAD->SRC.BNDRNG if
4928 PAD is nonnull and BNDRNG is valid. */
4929 get_size_range (maxread
, range
, pad
? pad
->src
.bndrng
: NULL
);
4930 /* Set OVERREAD for reads starting just past the end of an object. */
4931 overread
= pad
->src
.sizrng
[1] - pad
->src
.offrng
[0] < pad
->src
.bndrng
[0];
4932 range
[0] = wide_int_to_tree (sizetype
, pad
->src
.bndrng
[0]);
4933 slen
= size_zero_node
;
4938 const opt_code opt
= OPT_Wstringop_overread
;
4939 if (warning_suppressed_p (exp
, opt
)
4940 || (srcstr
&& warning_suppressed_p (srcstr
, opt
))
4941 || (pad
&& pad
->src
.ref
4942 && warning_suppressed_p (pad
->src
.ref
, opt
)))
4945 location_t loc
= EXPR_LOCATION (exp
);
4947 = mode
== access_read_only
|| mode
== access_read_write
;
4948 const bool maybe
= pad
&& pad
->dst
.parmarray
;
4949 if (warn_for_access (loc
, func
, exp
, opt
, range
, slen
, false, read
,
4952 suppress_warning (exp
, opt
);
4954 pad
->src
.inform_access (access_read_only
);
4962 /* A convenience wrapper for check_access above to check access
4963 by a read-only function like puts. */
4966 check_read_access (tree exp
, tree src
, tree bound
/* = NULL_TREE */,
4969 if (!warn_stringop_overread
)
4972 if (bound
&& !useless_type_conversion_p (size_type_node
, TREE_TYPE (bound
)))
4973 bound
= fold_convert (size_type_node
, bound
);
4974 access_data
data (exp
, access_read_only
, NULL_TREE
, false, bound
, true);
4975 compute_objsize (src
, ost
, &data
.src
);
4976 return check_access (exp
, /*dstwrite=*/ NULL_TREE
, /*maxread=*/ bound
,
4977 /*srcstr=*/ src
, /*dstsize=*/ NULL_TREE
, data
.mode
,
4981 /* If STMT is a call to an allocation function, returns the constant
4982 maximum size of the object allocated by the call represented as
4983 sizetype. If nonnull, sets RNG1[] to the range of the size.
4984 When nonnull, uses RVALS for range information, otherwise gets global
4986 Returns null when STMT is not a call to a valid allocation function. */
4989 gimple_call_alloc_size (gimple
*stmt
, wide_int rng1
[2] /* = NULL */,
4990 range_query
* /* = NULL */)
4992 if (!stmt
|| !is_gimple_call (stmt
))
4996 if (tree fndecl
= gimple_call_fndecl (stmt
))
4997 allocfntype
= TREE_TYPE (fndecl
);
4999 allocfntype
= gimple_call_fntype (stmt
);
5004 unsigned argidx1
= UINT_MAX
, argidx2
= UINT_MAX
;
5005 tree at
= lookup_attribute ("alloc_size", TYPE_ATTRIBUTES (allocfntype
));
5008 if (!gimple_call_builtin_p (stmt
, BUILT_IN_ALLOCA_WITH_ALIGN
))
5014 unsigned nargs
= gimple_call_num_args (stmt
);
5016 if (argidx1
== UINT_MAX
)
5018 tree atval
= TREE_VALUE (at
);
5022 argidx1
= TREE_INT_CST_LOW (TREE_VALUE (atval
)) - 1;
5023 if (nargs
<= argidx1
)
5026 atval
= TREE_CHAIN (atval
);
5029 argidx2
= TREE_INT_CST_LOW (TREE_VALUE (atval
)) - 1;
5030 if (nargs
<= argidx2
)
5035 tree size
= gimple_call_arg (stmt
, argidx1
);
5037 wide_int rng1_buf
[2];
5038 /* If RNG1 is not set, use the buffer. */
5042 /* Use maximum precision to avoid overflow below. */
5043 const int prec
= ADDR_MAX_PRECISION
;
5047 /* Determine the largest valid range size, including zero. */
5048 if (!get_size_range (size
, r
, SR_ALLOW_ZERO
| SR_USE_LARGEST
))
5050 rng1
[0] = wi::to_wide (r
[0], prec
);
5051 rng1
[1] = wi::to_wide (r
[1], prec
);
5054 if (argidx2
> nargs
&& TREE_CODE (size
) == INTEGER_CST
)
5055 return fold_convert (sizetype
, size
);
5057 /* To handle ranges do the math in wide_int and return the product
5058 of the upper bounds as a constant. Ignore anti-ranges. */
5059 tree n
= argidx2
< nargs
? gimple_call_arg (stmt
, argidx2
) : integer_one_node
;
5063 /* As above, use the full non-negative range on failure. */
5064 if (!get_size_range (n
, r
, SR_ALLOW_ZERO
| SR_USE_LARGEST
))
5066 rng2
[0] = wi::to_wide (r
[0], prec
);
5067 rng2
[1] = wi::to_wide (r
[1], prec
);
5070 /* Compute products of both bounds for the caller but return the lesser
5071 of SIZE_MAX and the product of the upper bounds as a constant. */
5072 rng1
[0] = rng1
[0] * rng2
[0];
5073 rng1
[1] = rng1
[1] * rng2
[1];
5075 const tree size_max
= TYPE_MAX_VALUE (sizetype
);
5076 if (wi::gtu_p (rng1
[1], wi::to_wide (size_max
, prec
)))
5078 rng1
[1] = wi::to_wide (size_max
, prec
);
5082 return wide_int_to_tree (sizetype
, rng1
[1]);
5085 /* For an access to an object referenced to by the function parameter PTR
5086 of pointer type, and set RNG[] to the range of sizes of the object
5087 obtainedfrom the attribute access specification for the current function.
5088 Set STATIC_ARRAY if the array parameter has been declared [static].
5089 Return the function parameter on success and null otherwise. */
5092 gimple_parm_array_size (tree ptr
, wide_int rng
[2],
5093 bool *static_array
/* = NULL */)
5095 /* For a function argument try to determine the byte size of the array
5096 from the current function declaratation (e.g., attribute access or
5098 tree var
= SSA_NAME_VAR (ptr
);
5099 if (TREE_CODE (var
) != PARM_DECL
)
5102 const unsigned prec
= TYPE_PRECISION (sizetype
);
5105 attr_access
*access
= get_parm_access (rdwr_idx
, var
);
5109 if (access
->sizarg
!= UINT_MAX
)
5111 /* TODO: Try to extract the range from the argument based on
5112 those of subsequent assertions or based on known calls to
5113 the current function. */
5117 if (!access
->minsize
)
5120 /* Only consider ordinary array bound at level 2 (or above if it's
5122 if (warn_array_parameter
< 2 && !access
->static_p
)
5126 *static_array
= access
->static_p
;
5128 rng
[0] = wi::zero (prec
);
5129 rng
[1] = wi::uhwi (access
->minsize
, prec
);
5130 /* Multiply the array bound encoded in the attribute by the size
5131 of what the pointer argument to which it decays points to. */
5132 tree eltype
= TREE_TYPE (TREE_TYPE (ptr
));
5133 tree size
= TYPE_SIZE_UNIT (eltype
);
5134 if (!size
|| TREE_CODE (size
) != INTEGER_CST
)
5137 rng
[1] *= wi::to_wide (size
, prec
);
5141 /* Wrapper around the wide_int overload of get_range that accepts
5142 offset_int instead. For middle end expressions returns the same
5143 result. For a subset of nonconstamt expressions emitted by the front
5144 end determines a more precise range than would be possible otherwise. */
5147 get_offset_range (tree x
, gimple
*stmt
, offset_int r
[2], range_query
*rvals
)
5150 if (TREE_CODE (x
) == PLUS_EXPR
)
5152 /* Handle constant offsets in pointer addition expressions seen
5153 n the front end IL. */
5154 tree op
= TREE_OPERAND (x
, 1);
5155 if (TREE_CODE (op
) == INTEGER_CST
)
5157 op
= fold_convert (signed_type_for (TREE_TYPE (op
)), op
);
5158 add
= wi::to_offset (op
);
5159 x
= TREE_OPERAND (x
, 0);
5163 if (TREE_CODE (x
) == NOP_EXPR
)
5164 /* Also handle conversions to sizetype seen in the front end IL. */
5165 x
= TREE_OPERAND (x
, 0);
5167 tree type
= TREE_TYPE (x
);
5168 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
5171 if (TREE_CODE (x
) != INTEGER_CST
5172 && TREE_CODE (x
) != SSA_NAME
)
5174 if (TYPE_UNSIGNED (type
)
5175 && TYPE_PRECISION (type
) == TYPE_PRECISION (sizetype
))
5176 type
= signed_type_for (type
);
5178 r
[0] = wi::to_offset (TYPE_MIN_VALUE (type
)) + add
;
5179 r
[1] = wi::to_offset (TYPE_MAX_VALUE (type
)) + add
;
5184 if (!get_range (x
, stmt
, wr
, rvals
))
5187 signop sgn
= SIGNED
;
5188 /* Only convert signed integers or unsigned sizetype to a signed
5189 offset and avoid converting large positive values in narrower
5190 types to negative offsets. */
5191 if (TYPE_UNSIGNED (type
)
5192 && wr
[0].get_precision () < TYPE_PRECISION (sizetype
))
5195 r
[0] = offset_int::from (wr
[0], sgn
);
5196 r
[1] = offset_int::from (wr
[1], sgn
);
5200 /* Return the argument that the call STMT to a built-in function returns
5201 or null if it doesn't. On success, set OFFRNG[] to the range of offsets
5202 from the argument reflected in the value returned by the built-in if it
5203 can be determined, otherwise to 0 and HWI_M1U respectively. */
5206 gimple_call_return_array (gimple
*stmt
, offset_int offrng
[2],
5210 /* Check for attribute fn spec to see if the function returns one
5211 of its arguments. */
5212 attr_fnspec fnspec
= gimple_call_fnspec (as_a
<gcall
*>(stmt
));
5214 if (fnspec
.returns_arg (&argno
))
5216 offrng
[0] = offrng
[1] = 0;
5217 return gimple_call_arg (stmt
, argno
);
5221 if (gimple_call_num_args (stmt
) < 1)
5224 tree fn
= gimple_call_fndecl (stmt
);
5225 if (!gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
5227 /* See if this is a call to placement new. */
5229 || !DECL_IS_OPERATOR_NEW_P (fn
)
5230 || DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fn
))
5233 /* Check the mangling, keeping in mind that operator new takes
5234 a size_t which could be unsigned int or unsigned long. */
5235 tree fname
= DECL_ASSEMBLER_NAME (fn
);
5236 if (!id_equal (fname
, "_ZnwjPv") // ordinary form
5237 && !id_equal (fname
, "_ZnwmPv") // ordinary form
5238 && !id_equal (fname
, "_ZnajPv") // array form
5239 && !id_equal (fname
, "_ZnamPv")) // array form
5242 if (gimple_call_num_args (stmt
) != 2)
5245 offrng
[0] = offrng
[1] = 0;
5246 return gimple_call_arg (stmt
, 1);
5249 switch (DECL_FUNCTION_CODE (fn
))
5251 case BUILT_IN_MEMCPY
:
5252 case BUILT_IN_MEMCPY_CHK
:
5253 case BUILT_IN_MEMMOVE
:
5254 case BUILT_IN_MEMMOVE_CHK
:
5255 case BUILT_IN_MEMSET
:
5256 case BUILT_IN_STPCPY
:
5257 case BUILT_IN_STPCPY_CHK
:
5258 case BUILT_IN_STPNCPY
:
5259 case BUILT_IN_STPNCPY_CHK
:
5260 case BUILT_IN_STRCAT
:
5261 case BUILT_IN_STRCAT_CHK
:
5262 case BUILT_IN_STRCPY
:
5263 case BUILT_IN_STRCPY_CHK
:
5264 case BUILT_IN_STRNCAT
:
5265 case BUILT_IN_STRNCAT_CHK
:
5266 case BUILT_IN_STRNCPY
:
5267 case BUILT_IN_STRNCPY_CHK
:
5268 offrng
[0] = offrng
[1] = 0;
5269 return gimple_call_arg (stmt
, 0);
5271 case BUILT_IN_MEMPCPY
:
5272 case BUILT_IN_MEMPCPY_CHK
:
5274 tree off
= gimple_call_arg (stmt
, 2);
5275 if (!get_offset_range (off
, stmt
, offrng
, rvals
))
5278 offrng
[1] = HOST_WIDE_INT_M1U
;
5280 return gimple_call_arg (stmt
, 0);
5283 case BUILT_IN_MEMCHR
:
5285 tree off
= gimple_call_arg (stmt
, 2);
5286 if (get_offset_range (off
, stmt
, offrng
, rvals
))
5291 offrng
[1] = HOST_WIDE_INT_M1U
;
5293 return gimple_call_arg (stmt
, 0);
5296 case BUILT_IN_STRCHR
:
5297 case BUILT_IN_STRRCHR
:
5298 case BUILT_IN_STRSTR
:
5301 offrng
[1] = HOST_WIDE_INT_M1U
;
5303 return gimple_call_arg (stmt
, 0);
5312 /* A helper of compute_objsize_r() to determine the size from an assignment
5313 statement STMT with the RHS of either MIN_EXPR or MAX_EXPR. */
5316 handle_min_max_size (gimple
*stmt
, int ostype
, access_ref
*pref
,
5317 ssa_name_limit_t
&snlim
, pointer_query
*qry
)
5319 tree_code code
= gimple_assign_rhs_code (stmt
);
5321 tree ptr
= gimple_assign_rhs1 (stmt
);
5323 /* In a valid MAX_/MIN_EXPR both operands must refer to the same array.
5324 Determine the size/offset of each and use the one with more or less
5325 space remaining, respectively. If either fails, use the information
5326 determined from the other instead, adjusted up or down as appropriate
5327 for the expression. */
5328 access_ref aref
[2] = { *pref
, *pref
};
5329 if (!compute_objsize_r (ptr
, ostype
, &aref
[0], snlim
, qry
))
5331 aref
[0].base0
= false;
5332 aref
[0].offrng
[0] = aref
[0].offrng
[1] = 0;
5333 aref
[0].add_max_offset ();
5334 aref
[0].set_max_size_range ();
5337 ptr
= gimple_assign_rhs2 (stmt
);
5338 if (!compute_objsize_r (ptr
, ostype
, &aref
[1], snlim
, qry
))
5340 aref
[1].base0
= false;
5341 aref
[1].offrng
[0] = aref
[1].offrng
[1] = 0;
5342 aref
[1].add_max_offset ();
5343 aref
[1].set_max_size_range ();
5346 if (!aref
[0].ref
&& !aref
[1].ref
)
5347 /* Fail if the identity of neither argument could be determined. */
5351 if (aref
[0].ref
&& aref
[0].base0
)
5353 if (aref
[1].ref
&& aref
[1].base0
)
5355 /* If the object referenced by both arguments has been determined
5356 set *PREF to the one with more or less space remainng, whichever
5357 is appopriate for CODE.
5358 TODO: Indicate when the objects are distinct so it can be
5360 i0
= code
== MAX_EXPR
;
5361 const bool i1
= !i0
;
5363 if (aref
[i0
].size_remaining () < aref
[i1
].size_remaining ())
5370 /* If only the object referenced by one of the arguments could be
5371 determined, use it and... */
5378 const bool i1
= !i0
;
5379 /* ...see if the offset obtained from the other pointer can be used
5380 to tighten up the bound on the offset obtained from the first. */
5381 if ((code
== MAX_EXPR
&& aref
[i1
].offrng
[1] < aref
[i0
].offrng
[0])
5382 || (code
== MIN_EXPR
&& aref
[i0
].offrng
[0] < aref
[i1
].offrng
[1]))
5384 pref
->offrng
[0] = aref
[i0
].offrng
[0];
5385 pref
->offrng
[1] = aref
[i0
].offrng
[1];
5390 /* A helper of compute_objsize_r() to determine the size from ARRAY_REF
5391 AREF. ADDR is true if PTR is the operand of ADDR_EXPR. Return true
5392 on success and false on failure. */
5395 handle_array_ref (tree aref
, bool addr
, int ostype
, access_ref
*pref
,
5396 ssa_name_limit_t
&snlim
, pointer_query
*qry
)
5398 gcc_assert (TREE_CODE (aref
) == ARRAY_REF
);
5402 tree arefop
= TREE_OPERAND (aref
, 0);
5403 tree reftype
= TREE_TYPE (arefop
);
5404 if (!addr
&& TREE_CODE (TREE_TYPE (reftype
)) == POINTER_TYPE
)
5405 /* Avoid arrays of pointers. FIXME: Hande pointers to arrays
5409 if (!compute_objsize_r (arefop
, ostype
, pref
, snlim
, qry
))
5413 tree off
= pref
->eval (TREE_OPERAND (aref
, 1));
5414 range_query
*const rvals
= qry
? qry
->rvals
: NULL
;
5415 if (!get_offset_range (off
, NULL
, orng
, rvals
))
5417 /* Set ORNG to the maximum offset representable in ptrdiff_t. */
5418 orng
[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node
));
5419 orng
[0] = -orng
[1] - 1;
5422 /* Convert the array index range determined above to a byte
5424 tree lowbnd
= array_ref_low_bound (aref
);
5425 if (!integer_zerop (lowbnd
) && tree_fits_uhwi_p (lowbnd
))
5427 /* Adjust the index by the low bound of the array domain
5428 (normally zero but 1 in Fortran). */
5429 unsigned HOST_WIDE_INT lb
= tree_to_uhwi (lowbnd
);
5434 tree eltype
= TREE_TYPE (aref
);
5435 tree tpsize
= TYPE_SIZE_UNIT (eltype
);
5436 if (!tpsize
|| TREE_CODE (tpsize
) != INTEGER_CST
)
5438 pref
->add_max_offset ();
5442 offset_int sz
= wi::to_offset (tpsize
);
5446 if (ostype
&& TREE_CODE (eltype
) == ARRAY_TYPE
)
5448 /* Except for the permissive raw memory functions which use
5449 the size of the whole object determined above, use the size
5450 of the referenced array. Because the overall offset is from
5451 the beginning of the complete array object add this overall
5452 offset to the size of array. */
5453 offset_int sizrng
[2] =
5455 pref
->offrng
[0] + orng
[0] + sz
,
5456 pref
->offrng
[1] + orng
[1] + sz
5458 if (sizrng
[1] < sizrng
[0])
5459 std::swap (sizrng
[0], sizrng
[1]);
5460 if (sizrng
[0] >= 0 && sizrng
[0] <= pref
->sizrng
[0])
5461 pref
->sizrng
[0] = sizrng
[0];
5462 if (sizrng
[1] >= 0 && sizrng
[1] <= pref
->sizrng
[1])
5463 pref
->sizrng
[1] = sizrng
[1];
5466 pref
->add_offset (orng
[0], orng
[1]);
5470 /* A helper of compute_objsize_r() to determine the size from MEM_REF
5471 MREF. Return true on success and false on failure. */
5474 handle_mem_ref (tree mref
, int ostype
, access_ref
*pref
,
5475 ssa_name_limit_t
&snlim
, pointer_query
*qry
)
5477 gcc_assert (TREE_CODE (mref
) == MEM_REF
);
5481 if (VECTOR_TYPE_P (TREE_TYPE (mref
)))
5483 /* Hack: Handle MEM_REFs of vector types as those to complete
5484 objects; those may be synthesized from multiple assignments
5485 to consecutive data members (see PR 93200 and 96963).
5486 FIXME: Vectorized assignments should only be present after
5487 vectorization so this hack is only necessary after it has
5488 run and could be avoided in calls from prior passes (e.g.,
5490 FIXME: Deal with this more generally, e.g., by marking up
5491 such MEM_REFs at the time they're created. */
5495 tree mrefop
= TREE_OPERAND (mref
, 0);
5496 if (!compute_objsize_r (mrefop
, ostype
, pref
, snlim
, qry
))
5500 tree off
= pref
->eval (TREE_OPERAND (mref
, 1));
5501 range_query
*const rvals
= qry
? qry
->rvals
: NULL
;
5502 if (!get_offset_range (off
, NULL
, orng
, rvals
))
5504 /* Set ORNG to the maximum offset representable in ptrdiff_t. */
5505 orng
[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node
));
5506 orng
[0] = -orng
[1] - 1;
5509 pref
->add_offset (orng
[0], orng
[1]);
5513 /* Helper to compute the size of the object referenced by the PTR
5514 expression which must have pointer type, using Object Size type
5515 OSTYPE (only the least significant 2 bits are used).
5516 On success, sets PREF->REF to the DECL of the referenced object
5517 if it's unique, otherwise to null, PREF->OFFRNG to the range of
5518 offsets into it, and PREF->SIZRNG to the range of sizes of
5520 SNLIM is used to avoid visiting the same PHI operand multiple
5521 times, and, when nonnull, RVALS to determine range information.
5522 Returns true on success, false when a meaningful size (or range)
5523 cannot be determined.
5525 The function is intended for diagnostics and should not be used
5526 to influence code generation or optimization. */
5529 compute_objsize_r (tree ptr
, int ostype
, access_ref
*pref
,
5530 ssa_name_limit_t
&snlim
, pointer_query
*qry
)
5534 const bool addr
= TREE_CODE (ptr
) == ADDR_EXPR
;
5538 ptr
= TREE_OPERAND (ptr
, 0);
5545 if (!addr
&& POINTER_TYPE_P (TREE_TYPE (ptr
)))
5547 /* Set the maximum size if the reference is to the pointer
5548 itself (as opposed to what it points to), and clear
5549 BASE0 since the offset isn't necessarily zero-based. */
5550 pref
->set_max_size_range ();
5551 pref
->base0
= false;
5555 if (tree size
= decl_init_size (ptr
, false))
5556 if (TREE_CODE (size
) == INTEGER_CST
)
5558 pref
->sizrng
[0] = pref
->sizrng
[1] = wi::to_offset (size
);
5562 pref
->set_max_size_range ();
5566 const tree_code code
= TREE_CODE (ptr
);
5567 range_query
*const rvals
= qry
? qry
->rvals
: NULL
;
5569 if (code
== BIT_FIELD_REF
)
5571 tree ref
= TREE_OPERAND (ptr
, 0);
5572 if (!compute_objsize_r (ref
, ostype
, pref
, snlim
, qry
))
5575 offset_int off
= wi::to_offset (pref
->eval (TREE_OPERAND (ptr
, 2)));
5576 pref
->add_offset (off
/ BITS_PER_UNIT
);
5580 if (code
== COMPONENT_REF
)
5582 tree ref
= TREE_OPERAND (ptr
, 0);
5583 if (TREE_CODE (TREE_TYPE (ref
)) == UNION_TYPE
)
5584 /* In accesses through union types consider the entire unions
5585 rather than just their members. */
5587 tree field
= TREE_OPERAND (ptr
, 1);
5591 /* In OSTYPE zero (for raw memory functions like memcpy), use
5592 the maximum size instead if the identity of the enclosing
5593 object cannot be determined. */
5594 if (!compute_objsize_r (ref
, ostype
, pref
, snlim
, qry
))
5597 /* Otherwise, use the size of the enclosing object and add
5598 the offset of the member to the offset computed so far. */
5599 tree offset
= byte_position (field
);
5600 if (TREE_CODE (offset
) == INTEGER_CST
)
5601 pref
->add_offset (wi::to_offset (offset
));
5603 pref
->add_max_offset ();
5606 /* REF may have been already set to an SSA_NAME earlier
5607 to provide better context for diagnostics. In that case,
5608 leave it unchanged. */
5615 if (!addr
&& POINTER_TYPE_P (TREE_TYPE (field
)))
5617 /* Set maximum size if the reference is to the pointer member
5618 itself (as opposed to what it points to). */
5619 pref
->set_max_size_range ();
5623 /* SAM is set for array members that might need special treatment. */
5624 special_array_member sam
;
5625 tree size
= component_ref_size (ptr
, &sam
);
5626 if (sam
== special_array_member::int_0
)
5627 pref
->sizrng
[0] = pref
->sizrng
[1] = 0;
5628 else if (!pref
->trail1special
&& sam
== special_array_member::trail_1
)
5629 pref
->sizrng
[0] = pref
->sizrng
[1] = 1;
5630 else if (size
&& TREE_CODE (size
) == INTEGER_CST
)
5631 pref
->sizrng
[0] = pref
->sizrng
[1] = wi::to_offset (size
);
5634 /* When the size of the member is unknown it's either a flexible
5635 array member or a trailing special array member (either zero
5636 length or one-element). Set the size to the maximum minus
5637 the constant size of the type. */
5638 pref
->sizrng
[0] = 0;
5639 pref
->sizrng
[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node
));
5640 if (tree recsize
= TYPE_SIZE_UNIT (TREE_TYPE (ref
)))
5641 if (TREE_CODE (recsize
) == INTEGER_CST
)
5642 pref
->sizrng
[1] -= wi::to_offset (recsize
);
5647 if (code
== ARRAY_REF
)
5648 return handle_array_ref (ptr
, addr
, ostype
, pref
, snlim
, qry
);
5650 if (code
== MEM_REF
)
5651 return handle_mem_ref (ptr
, ostype
, pref
, snlim
, qry
);
5653 if (code
== TARGET_MEM_REF
)
5655 tree ref
= TREE_OPERAND (ptr
, 0);
5656 if (!compute_objsize_r (ref
, ostype
, pref
, snlim
, qry
))
5659 /* TODO: Handle remaining operands. Until then, add maximum offset. */
5661 pref
->add_max_offset ();
5665 if (code
== INTEGER_CST
)
5667 /* Pointer constants other than null are most likely the result
5668 of erroneous null pointer addition/subtraction. Set size to
5669 zero. For null pointers, set size to the maximum for now
5670 since those may be the result of jump threading. */
5671 if (integer_zerop (ptr
))
5672 pref
->set_max_size_range ();
5674 pref
->sizrng
[0] = pref
->sizrng
[1] = 0;
5680 if (code
== STRING_CST
)
5682 pref
->sizrng
[0] = pref
->sizrng
[1] = TREE_STRING_LENGTH (ptr
);
5687 if (code
== POINTER_PLUS_EXPR
)
5689 tree ref
= TREE_OPERAND (ptr
, 0);
5690 if (!compute_objsize_r (ref
, ostype
, pref
, snlim
, qry
))
5693 /* Clear DEREF since the offset is being applied to the target
5694 of the dereference. */
5698 tree off
= pref
->eval (TREE_OPERAND (ptr
, 1));
5699 if (get_offset_range (off
, NULL
, orng
, rvals
))
5700 pref
->add_offset (orng
[0], orng
[1]);
5702 pref
->add_max_offset ();
5706 if (code
== VIEW_CONVERT_EXPR
)
5708 ptr
= TREE_OPERAND (ptr
, 0);
5709 return compute_objsize_r (ptr
, ostype
, pref
, snlim
, qry
);
5712 if (code
== SSA_NAME
)
5717 /* Only process an SSA_NAME if the recursion limit has not yet
5722 qry
->max_depth
= qry
->depth
;
5723 if (const access_ref
*cache_ref
= qry
->get_ref (ptr
))
5725 /* If the pointer is in the cache set *PREF to what it refers
5726 to and return success. */
5732 gimple
*stmt
= SSA_NAME_DEF_STMT (ptr
);
5733 if (is_gimple_call (stmt
))
5735 /* If STMT is a call to an allocation function get the size
5736 from its argument(s). If successful, also set *PREF->REF
5737 to PTR for the caller to include in diagnostics. */
5739 if (gimple_call_alloc_size (stmt
, wr
, rvals
))
5742 pref
->sizrng
[0] = offset_int::from (wr
[0], UNSIGNED
);
5743 pref
->sizrng
[1] = offset_int::from (wr
[1], UNSIGNED
);
5744 /* Constrain both bounds to a valid size. */
5745 offset_int maxsize
= wi::to_offset (max_object_size ());
5746 if (pref
->sizrng
[0] > maxsize
)
5747 pref
->sizrng
[0] = maxsize
;
5748 if (pref
->sizrng
[1] > maxsize
)
5749 pref
->sizrng
[1] = maxsize
;
5753 /* For functions known to return one of their pointer arguments
5754 try to determine what the returned pointer points to, and on
5755 success add OFFRNG which was set to the offset added by
5756 the function (e.g., memchr) to the overall offset. */
5757 offset_int offrng
[2];
5758 if (tree ret
= gimple_call_return_array (stmt
, offrng
, rvals
))
5760 if (!compute_objsize_r (ret
, ostype
, pref
, snlim
, qry
))
5763 /* Cap OFFRNG[1] to at most the remaining size of
5765 offset_int remrng
[2];
5766 remrng
[1] = pref
->size_remaining (remrng
);
5767 if (remrng
[1] < offrng
[1])
5768 offrng
[1] = remrng
[1];
5769 pref
->add_offset (offrng
[0], offrng
[1]);
5773 /* For other calls that might return arbitrary pointers
5774 including into the middle of objects set the size
5775 range to maximum, clear PREF->BASE0, and also set
5776 PREF->REF to include in diagnostics. */
5777 pref
->set_max_size_range ();
5778 pref
->base0
= false;
5782 qry
->put_ref (ptr
, *pref
);
5786 if (gimple_nop_p (stmt
))
5788 /* For a function argument try to determine the byte size
5789 of the array from the current function declaratation
5790 (e.g., attribute access or related). */
5792 bool static_array
= false;
5793 if (tree ref
= gimple_parm_array_size (ptr
, wr
, &static_array
))
5795 pref
->parmarray
= !static_array
;
5796 pref
->sizrng
[0] = offset_int::from (wr
[0], UNSIGNED
);
5797 pref
->sizrng
[1] = offset_int::from (wr
[1], UNSIGNED
);
5799 qry
->put_ref (ptr
, *pref
);
5803 pref
->set_max_size_range ();
5804 pref
->base0
= false;
5806 qry
->put_ref (ptr
, *pref
);
5810 if (gimple_code (stmt
) == GIMPLE_PHI
)
5813 access_ref phi_ref
= *pref
;
5814 if (!pref
->get_ref (NULL
, &phi_ref
, ostype
, &snlim
, qry
))
5818 qry
->put_ref (ptr
, *pref
);
5822 if (!is_gimple_assign (stmt
))
5824 /* Clear BASE0 since the assigned pointer might point into
5825 the middle of the object, set the maximum size range and,
5826 if the SSA_NAME refers to a function argumnent, set
5828 pref
->base0
= false;
5829 pref
->set_max_size_range ();
5834 tree_code code
= gimple_assign_rhs_code (stmt
);
5836 if (code
== MAX_EXPR
|| code
== MIN_EXPR
)
5838 if (!handle_min_max_size (stmt
, ostype
, pref
, snlim
, qry
))
5840 qry
->put_ref (ptr
, *pref
);
5844 tree rhs
= gimple_assign_rhs1 (stmt
);
5846 if (code
== ASSERT_EXPR
)
5848 rhs
= TREE_OPERAND (rhs
, 0);
5849 return compute_objsize_r (rhs
, ostype
, pref
, snlim
, qry
);
5852 if (code
== POINTER_PLUS_EXPR
5853 && TREE_CODE (TREE_TYPE (rhs
)) == POINTER_TYPE
)
5855 /* Compute the size of the object first. */
5856 if (!compute_objsize_r (rhs
, ostype
, pref
, snlim
, qry
))
5860 tree off
= gimple_assign_rhs2 (stmt
);
5861 if (get_offset_range (off
, stmt
, orng
, rvals
))
5862 pref
->add_offset (orng
[0], orng
[1]);
5864 pref
->add_max_offset ();
5865 qry
->put_ref (ptr
, *pref
);
5869 if (code
== ADDR_EXPR
5870 || code
== SSA_NAME
)
5871 return compute_objsize_r (rhs
, ostype
, pref
, snlim
, qry
);
5873 /* (This could also be an assignment from a nonlocal pointer.) Save
5874 PTR to mention in diagnostics but otherwise treat it as a pointer
5875 to an unknown object. */
5877 pref
->base0
= false;
5878 pref
->set_max_size_range ();
5882 /* Assume all other expressions point into an unknown object
5883 of the maximum valid size. */
5885 pref
->base0
= false;
5886 pref
->set_max_size_range ();
5887 if (TREE_CODE (ptr
) == SSA_NAME
)
5888 qry
->put_ref (ptr
, *pref
);
5892 /* A "public" wrapper around the above. Clients should use this overload
5896 compute_objsize (tree ptr
, int ostype
, access_ref
*pref
,
5897 range_query
*rvals
/* = NULL */)
5901 ssa_name_limit_t snlim
;
5902 if (!compute_objsize_r (ptr
, ostype
, pref
, snlim
, &qry
))
5905 offset_int maxsize
= pref
->size_remaining ();
5906 if (pref
->base0
&& pref
->offrng
[0] < 0 && pref
->offrng
[1] >= 0)
5907 pref
->offrng
[0] = 0;
5908 return wide_int_to_tree (sizetype
, maxsize
);
5911 /* Transitional wrapper. The function should be removed once callers
5912 transition to the pointer_query API. */
5915 compute_objsize (tree ptr
, int ostype
, access_ref
*pref
, pointer_query
*ptr_qry
)
5923 ssa_name_limit_t snlim
;
5924 if (!compute_objsize_r (ptr
, ostype
, pref
, snlim
, ptr_qry
))
5927 offset_int maxsize
= pref
->size_remaining ();
5928 if (pref
->base0
&& pref
->offrng
[0] < 0 && pref
->offrng
[1] >= 0)
5929 pref
->offrng
[0] = 0;
5930 return wide_int_to_tree (sizetype
, maxsize
);
5933 /* Legacy wrapper around the above. The function should be removed
5934 once callers transition to one of the two above. */
5937 compute_objsize (tree ptr
, int ostype
, tree
*pdecl
/* = NULL */,
5938 tree
*poff
/* = NULL */, range_query
*rvals
/* = NULL */)
5940 /* Set the initial offsets to zero and size to negative to indicate
5941 none has been computed yet. */
5943 tree size
= compute_objsize (ptr
, ostype
, &ref
, rvals
);
5944 if (!size
|| !ref
.base0
)
5951 *poff
= wide_int_to_tree (ptrdiff_type_node
, ref
.offrng
[ref
.offrng
[0] < 0]);
5956 /* Helper to determine and check the sizes of the source and the destination
5957 of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. EXP is the
5958 call expression, DEST is the destination argument, SRC is the source
5959 argument or null, and LEN is the number of bytes. Use Object Size type-0
5960 regardless of the OPT_Wstringop_overflow_ setting. Return true on success
5961 (no overflow or invalid sizes), false otherwise. */
5964 check_memop_access (tree exp
, tree dest
, tree src
, tree size
)
5966 /* For functions like memset and memcpy that operate on raw memory
5967 try to determine the size of the largest source and destination
5968 object using type-0 Object Size regardless of the object size
5969 type specified by the option. */
5970 access_data
data (exp
, access_read_write
);
5971 tree srcsize
= src
? compute_objsize (src
, 0, &data
.src
) : NULL_TREE
;
5972 tree dstsize
= compute_objsize (dest
, 0, &data
.dst
);
5974 return check_access (exp
, size
, /*maxread=*/NULL_TREE
,
5975 srcsize
, dstsize
, data
.mode
, &data
);
5978 /* Validate memchr arguments without performing any expansion.
5982 expand_builtin_memchr (tree exp
, rtx
)
5984 if (!validate_arglist (exp
,
5985 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5988 tree arg1
= CALL_EXPR_ARG (exp
, 0);
5989 tree len
= CALL_EXPR_ARG (exp
, 2);
5991 check_read_access (exp
, arg1
, len
, 0);
5996 /* Expand a call EXP to the memcpy builtin.
5997 Return NULL_RTX if we failed, the caller should emit a normal call,
5998 otherwise try to get the result in TARGET, if convenient (and in
5999 mode MODE if that's convenient). */
6002 expand_builtin_memcpy (tree exp
, rtx target
)
6004 if (!validate_arglist (exp
,
6005 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6008 tree dest
= CALL_EXPR_ARG (exp
, 0);
6009 tree src
= CALL_EXPR_ARG (exp
, 1);
6010 tree len
= CALL_EXPR_ARG (exp
, 2);
6012 check_memop_access (exp
, dest
, src
, len
);
6014 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, exp
,
6015 /*retmode=*/ RETURN_BEGIN
, false);
6018 /* Check a call EXP to the memmove built-in for validity.
6019 Return NULL_RTX on both success and failure. */
6022 expand_builtin_memmove (tree exp
, rtx target
)
6024 if (!validate_arglist (exp
,
6025 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6028 tree dest
= CALL_EXPR_ARG (exp
, 0);
6029 tree src
= CALL_EXPR_ARG (exp
, 1);
6030 tree len
= CALL_EXPR_ARG (exp
, 2);
6032 check_memop_access (exp
, dest
, src
, len
);
6034 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, exp
,
6035 /*retmode=*/ RETURN_BEGIN
, true);
6038 /* Expand a call EXP to the mempcpy builtin.
6039 Return NULL_RTX if we failed; the caller should emit a normal call,
6040 otherwise try to get the result in TARGET, if convenient (and in
6041 mode MODE if that's convenient). */
6044 expand_builtin_mempcpy (tree exp
, rtx target
)
6046 if (!validate_arglist (exp
,
6047 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6050 tree dest
= CALL_EXPR_ARG (exp
, 0);
6051 tree src
= CALL_EXPR_ARG (exp
, 1);
6052 tree len
= CALL_EXPR_ARG (exp
, 2);
6054 /* Policy does not generally allow using compute_objsize (which
6055 is used internally by check_memop_size) to change code generation
6056 or drive optimization decisions.
6058 In this instance it is safe because the code we generate has
6059 the same semantics regardless of the return value of
6060 check_memop_sizes. Exactly the same amount of data is copied
6061 and the return value is exactly the same in both cases.
6063 Furthermore, check_memop_size always uses mode 0 for the call to
6064 compute_objsize, so the imprecise nature of compute_objsize is
6067 /* Avoid expanding mempcpy into memcpy when the call is determined
6068 to overflow the buffer. This also prevents the same overflow
6069 from being diagnosed again when expanding memcpy. */
6070 if (!check_memop_access (exp
, dest
, src
, len
))
6073 return expand_builtin_mempcpy_args (dest
, src
, len
,
6074 target
, exp
, /*retmode=*/ RETURN_END
);
6077 /* Helper function to do the actual work for expand of memory copy family
6078 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
6079 of memory from SRC to DEST and assign to TARGET if convenient. Return
6080 value is based on RETMODE argument. */
6083 expand_builtin_memory_copy_args (tree dest
, tree src
, tree len
,
6084 rtx target
, tree exp
, memop_ret retmode
,
6087 unsigned int src_align
= get_pointer_alignment (src
);
6088 unsigned int dest_align
= get_pointer_alignment (dest
);
6089 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
6090 HOST_WIDE_INT expected_size
= -1;
6091 unsigned int expected_align
= 0;
6092 unsigned HOST_WIDE_INT min_size
;
6093 unsigned HOST_WIDE_INT max_size
;
6094 unsigned HOST_WIDE_INT probable_max_size
;
6098 /* If DEST is not a pointer type, call the normal function. */
6099 if (dest_align
== 0)
6102 /* If either SRC is not a pointer type, don't do this
6103 operation in-line. */
6107 if (currently_expanding_gimple_stmt
)
6108 stringop_block_profile (currently_expanding_gimple_stmt
,
6109 &expected_align
, &expected_size
);
6111 if (expected_align
< dest_align
)
6112 expected_align
= dest_align
;
6113 dest_mem
= get_memory_rtx (dest
, len
);
6114 set_mem_align (dest_mem
, dest_align
);
6115 len_rtx
= expand_normal (len
);
6116 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
6117 &probable_max_size
);
6119 /* Try to get the byte representation of the constant SRC points to,
6120 with its byte size in NBYTES. */
6121 unsigned HOST_WIDE_INT nbytes
;
6122 const char *rep
= getbyterep (src
, &nbytes
);
6124 /* If the function's constant bound LEN_RTX is less than or equal
6125 to the byte size of the representation of the constant argument,
6126 and if block move would be done by pieces, we can avoid loading
6127 the bytes from memory and only store the computed constant.
6128 This works in the overlap (memmove) case as well because
6129 store_by_pieces just generates a series of stores of constants
6130 from the representation returned by getbyterep(). */
6132 && CONST_INT_P (len_rtx
)
6133 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= nbytes
6134 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
6135 CONST_CAST (char *, rep
),
6138 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
6139 builtin_memcpy_read_str
,
6140 CONST_CAST (char *, rep
),
6141 dest_align
, false, retmode
);
6142 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
6143 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
6147 src_mem
= get_memory_rtx (src
, len
);
6148 set_mem_align (src_mem
, src_align
);
6150 /* Copy word part most expediently. */
6151 enum block_op_methods method
= BLOCK_OP_NORMAL
;
6152 if (CALL_EXPR_TAILCALL (exp
)
6153 && (retmode
== RETURN_BEGIN
|| target
== const0_rtx
))
6154 method
= BLOCK_OP_TAILCALL
;
6155 bool use_mempcpy_call
= (targetm
.libc_has_fast_function (BUILT_IN_MEMPCPY
)
6156 && retmode
== RETURN_END
6158 && target
!= const0_rtx
);
6159 if (use_mempcpy_call
)
6160 method
= BLOCK_OP_NO_LIBCALL_RET
;
6161 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
, method
,
6162 expected_align
, expected_size
,
6163 min_size
, max_size
, probable_max_size
,
6164 use_mempcpy_call
, &is_move_done
,
6167 /* Bail out when a mempcpy call would be expanded as libcall and when
6168 we have a target that provides a fast implementation
6169 of mempcpy routine. */
6173 if (dest_addr
== pc_rtx
)
6178 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
6179 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
6182 if (retmode
!= RETURN_BEGIN
&& target
!= const0_rtx
)
6184 dest_addr
= gen_rtx_PLUS (ptr_mode
, dest_addr
, len_rtx
);
6185 /* stpcpy pointer to last byte. */
6186 if (retmode
== RETURN_END_MINUS_ONE
)
6187 dest_addr
= gen_rtx_MINUS (ptr_mode
, dest_addr
, const1_rtx
);
6194 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
6195 rtx target
, tree orig_exp
, memop_ret retmode
)
6197 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, orig_exp
,
6201 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
6202 we failed, the caller should emit a normal call, otherwise try to
6203 get the result in TARGET, if convenient.
6204 Return value is based on RETMODE argument. */
6207 expand_movstr (tree dest
, tree src
, rtx target
, memop_ret retmode
)
6209 class expand_operand ops
[3];
6213 if (!targetm
.have_movstr ())
6216 dest_mem
= get_memory_rtx (dest
, NULL
);
6217 src_mem
= get_memory_rtx (src
, NULL
);
6218 if (retmode
== RETURN_BEGIN
)
6220 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
6221 dest_mem
= replace_equiv_address (dest_mem
, target
);
6224 create_output_operand (&ops
[0],
6225 retmode
!= RETURN_BEGIN
? target
: NULL_RTX
, Pmode
);
6226 create_fixed_operand (&ops
[1], dest_mem
);
6227 create_fixed_operand (&ops
[2], src_mem
);
6228 if (!maybe_expand_insn (targetm
.code_for_movstr
, 3, ops
))
6231 if (retmode
!= RETURN_BEGIN
&& target
!= const0_rtx
)
6233 target
= ops
[0].value
;
6234 /* movstr is supposed to set end to the address of the NUL
6235 terminator. If the caller requested a mempcpy-like return value,
6237 if (retmode
== RETURN_END
)
6239 rtx tem
= plus_constant (GET_MODE (target
),
6240 gen_lowpart (GET_MODE (target
), target
), 1);
6241 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
6247 /* Do some very basic size validation of a call to the strcpy builtin
6248 given by EXP. Return NULL_RTX to have the built-in expand to a call
6249 to the library function. */
6252 expand_builtin_strcat (tree exp
)
6254 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
)
6255 || !warn_stringop_overflow
)
6258 tree dest
= CALL_EXPR_ARG (exp
, 0);
6259 tree src
= CALL_EXPR_ARG (exp
, 1);
6261 /* There is no way here to determine the length of the string in
6262 the destination to which the SRC string is being appended so
6263 just diagnose cases when the souce string is longer than
6264 the destination object. */
6265 access_data
data (exp
, access_read_write
, NULL_TREE
, true,
6267 const int ost
= warn_stringop_overflow
? warn_stringop_overflow
- 1 : 1;
6268 compute_objsize (src
, ost
, &data
.src
);
6269 tree destsize
= compute_objsize (dest
, ost
, &data
.dst
);
6271 check_access (exp
, /*dstwrite=*/NULL_TREE
, /*maxread=*/NULL_TREE
,
6272 src
, destsize
, data
.mode
, &data
);
6277 /* Expand expression EXP, which is a call to the strcpy builtin. Return
6278 NULL_RTX if we failed the caller should emit a normal call, otherwise
6279 try to get the result in TARGET, if convenient (and in mode MODE if that's
6283 expand_builtin_strcpy (tree exp
, rtx target
)
6285 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6288 tree dest
= CALL_EXPR_ARG (exp
, 0);
6289 tree src
= CALL_EXPR_ARG (exp
, 1);
6291 if (warn_stringop_overflow
)
6293 access_data
data (exp
, access_read_write
, NULL_TREE
, true,
6295 const int ost
= warn_stringop_overflow
? warn_stringop_overflow
- 1 : 1;
6296 compute_objsize (src
, ost
, &data
.src
);
6297 tree dstsize
= compute_objsize (dest
, ost
, &data
.dst
);
6298 check_access (exp
, /*dstwrite=*/ NULL_TREE
,
6299 /*maxread=*/ NULL_TREE
, /*srcstr=*/ src
,
6300 dstsize
, data
.mode
, &data
);
6303 if (rtx ret
= expand_builtin_strcpy_args (exp
, dest
, src
, target
))
6305 /* Check to see if the argument was declared attribute nonstring
6306 and if so, issue a warning since at this point it's not known
6307 to be nul-terminated. */
6308 tree fndecl
= get_callee_fndecl (exp
);
6309 maybe_warn_nonstring_arg (fndecl
, exp
);
6316 /* Helper function to do the actual work for expand_builtin_strcpy. The
6317 arguments to the builtin_strcpy call DEST and SRC are broken out
6318 so that this can also be called without constructing an actual CALL_EXPR.
6319 The other arguments and return value are the same as for
6320 expand_builtin_strcpy. */
6323 expand_builtin_strcpy_args (tree exp
, tree dest
, tree src
, rtx target
)
6325 /* Detect strcpy calls with unterminated arrays.. */
6328 if (tree nonstr
= unterminated_array (src
, &size
, &exact
))
6330 /* NONSTR refers to the non-nul terminated constant array. */
6331 warn_string_no_nul (EXPR_LOCATION (exp
), exp
, NULL
, src
, nonstr
,
6336 return expand_movstr (dest
, src
, target
, /*retmode=*/ RETURN_BEGIN
);
6339 /* Expand a call EXP to the stpcpy builtin.
6340 Return NULL_RTX if we failed the caller should emit a normal call,
6341 otherwise try to get the result in TARGET, if convenient (and in
6342 mode MODE if that's convenient). */
6345 expand_builtin_stpcpy_1 (tree exp
, rtx target
, machine_mode mode
)
6348 location_t loc
= EXPR_LOCATION (exp
);
6350 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6353 dst
= CALL_EXPR_ARG (exp
, 0);
6354 src
= CALL_EXPR_ARG (exp
, 1);
6356 if (warn_stringop_overflow
)
6358 access_data
data (exp
, access_read_write
);
6359 tree destsize
= compute_objsize (dst
, warn_stringop_overflow
- 1,
6361 check_access (exp
, /*dstwrite=*/NULL_TREE
, /*maxread=*/NULL_TREE
,
6362 src
, destsize
, data
.mode
, &data
);
6365 /* If return value is ignored, transform stpcpy into strcpy. */
6366 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
6368 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
6369 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
6370 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
6377 /* Ensure we get an actual string whose length can be evaluated at
6378 compile-time, not an expression containing a string. This is
6379 because the latter will potentially produce pessimized code
6380 when used to produce the return value. */
6381 c_strlen_data lendata
= { };
6383 || !(len
= c_strlen (src
, 0, &lendata
, 1)))
6384 return expand_movstr (dst
, src
, target
,
6385 /*retmode=*/ RETURN_END_MINUS_ONE
);
6388 warn_string_no_nul (EXPR_LOCATION (exp
), exp
, NULL
, src
, lendata
.decl
);
6390 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
6391 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
6393 /*retmode=*/ RETURN_END_MINUS_ONE
);
6398 if (TREE_CODE (len
) == INTEGER_CST
)
6400 rtx len_rtx
= expand_normal (len
);
6402 if (CONST_INT_P (len_rtx
))
6404 ret
= expand_builtin_strcpy_args (exp
, dst
, src
, target
);
6410 if (mode
!= VOIDmode
)
6411 target
= gen_reg_rtx (mode
);
6413 target
= gen_reg_rtx (GET_MODE (ret
));
6415 if (GET_MODE (target
) != GET_MODE (ret
))
6416 ret
= gen_lowpart (GET_MODE (target
), ret
);
6418 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
6419 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
6427 return expand_movstr (dst
, src
, target
,
6428 /*retmode=*/ RETURN_END_MINUS_ONE
);
6432 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
6433 arguments while being careful to avoid duplicate warnings (which could
6434 be issued if the expander were to expand the call, resulting in it
6435 being emitted in expand_call(). */
6438 expand_builtin_stpcpy (tree exp
, rtx target
, machine_mode mode
)
6440 if (rtx ret
= expand_builtin_stpcpy_1 (exp
, target
, mode
))
6442 /* The call has been successfully expanded. Check for nonstring
6443 arguments and issue warnings as appropriate. */
6444 maybe_warn_nonstring_arg (get_callee_fndecl (exp
), exp
);
6451 /* Check a call EXP to the stpncpy built-in for validity.
6452 Return NULL_RTX on both success and failure. */
6455 expand_builtin_stpncpy (tree exp
, rtx
)
6457 if (!validate_arglist (exp
,
6458 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
6459 || !warn_stringop_overflow
)
6462 /* The source and destination of the call. */
6463 tree dest
= CALL_EXPR_ARG (exp
, 0);
6464 tree src
= CALL_EXPR_ARG (exp
, 1);
6466 /* The exact number of bytes to write (not the maximum). */
6467 tree len
= CALL_EXPR_ARG (exp
, 2);
6468 access_data
data (exp
, access_read_write
);
6469 /* The size of the destination object. */
6470 tree destsize
= compute_objsize (dest
, warn_stringop_overflow
- 1, &data
.dst
);
6471 check_access (exp
, len
, /*maxread=*/len
, src
, destsize
, data
.mode
, &data
);
6475 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
6476 bytes from constant string DATA + OFFSET and return it as target
6480 builtin_strncpy_read_str (void *data
, void *, HOST_WIDE_INT offset
,
6481 scalar_int_mode mode
)
6483 const char *str
= (const char *) data
;
6485 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
6488 return c_readstr (str
+ offset
, mode
);
6491 /* Helper to check the sizes of sequences and the destination of calls
6492 to __builtin_strncat and __builtin___strncat_chk. Returns true on
6493 success (no overflow or invalid sizes), false otherwise. */
6496 check_strncat_sizes (tree exp
, tree objsize
)
6498 tree dest
= CALL_EXPR_ARG (exp
, 0);
6499 tree src
= CALL_EXPR_ARG (exp
, 1);
6500 tree maxread
= CALL_EXPR_ARG (exp
, 2);
6502 /* Try to determine the range of lengths that the source expression
6504 c_strlen_data lendata
= { };
6505 get_range_strlen (src
, &lendata
, /* eltsize = */ 1);
6507 /* Try to verify that the destination is big enough for the shortest
6510 access_data
data (exp
, access_read_write
, maxread
, true);
6511 if (!objsize
&& warn_stringop_overflow
)
6513 /* If it hasn't been provided by __strncat_chk, try to determine
6514 the size of the destination object into which the source is
6516 objsize
= compute_objsize (dest
, warn_stringop_overflow
- 1, &data
.dst
);
6519 /* Add one for the terminating nul. */
6520 tree srclen
= (lendata
.minlen
6521 ? fold_build2 (PLUS_EXPR
, size_type_node
, lendata
.minlen
,
6525 /* The strncat function copies at most MAXREAD bytes and always appends
6526 the terminating nul so the specified upper bound should never be equal
6527 to (or greater than) the size of the destination. */
6528 if (tree_fits_uhwi_p (maxread
) && tree_fits_uhwi_p (objsize
)
6529 && tree_int_cst_equal (objsize
, maxread
))
6531 location_t loc
= EXPR_LOCATION (exp
);
6532 warning_at (loc
, OPT_Wstringop_overflow_
,
6533 "%qD specified bound %E equals destination size",
6534 get_callee_fndecl (exp
), maxread
);
6540 || (maxread
&& tree_fits_uhwi_p (maxread
)
6541 && tree_fits_uhwi_p (srclen
)
6542 && tree_int_cst_lt (maxread
, srclen
)))
6545 /* The number of bytes to write is LEN but check_access will alsoa
6546 check SRCLEN if LEN's value isn't known. */
6547 return check_access (exp
, /*dstwrite=*/NULL_TREE
, maxread
, srclen
,
6548 objsize
, data
.mode
, &data
);
6551 /* Similar to expand_builtin_strcat, do some very basic size validation
6552 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
6553 the built-in expand to a call to the library function. */
6556 expand_builtin_strncat (tree exp
, rtx
)
6558 if (!validate_arglist (exp
,
6559 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
6560 || !warn_stringop_overflow
)
6563 tree dest
= CALL_EXPR_ARG (exp
, 0);
6564 tree src
= CALL_EXPR_ARG (exp
, 1);
6565 /* The upper bound on the number of bytes to write. */
6566 tree maxread
= CALL_EXPR_ARG (exp
, 2);
6568 /* Detect unterminated source (only). */
6569 if (!check_nul_terminated_array (exp
, src
, maxread
))
6572 /* The length of the source sequence. */
6573 tree slen
= c_strlen (src
, 1);
6575 /* Try to determine the range of lengths that the source expression
6576 refers to. Since the lengths are only used for warning and not
6577 for code generation disable strict mode below. */
6581 c_strlen_data lendata
= { };
6582 get_range_strlen (src
, &lendata
, /* eltsize = */ 1);
6583 maxlen
= lendata
.maxbound
;
6586 access_data
data (exp
, access_read_write
);
6587 /* Try to verify that the destination is big enough for the shortest
6588 string. First try to determine the size of the destination object
6589 into which the source is being copied. */
6590 tree destsize
= compute_objsize (dest
, warn_stringop_overflow
- 1, &data
.dst
);
6592 /* Add one for the terminating nul. */
6593 tree srclen
= (maxlen
6594 ? fold_build2 (PLUS_EXPR
, size_type_node
, maxlen
,
6598 /* The strncat function copies at most MAXREAD bytes and always appends
6599 the terminating nul so the specified upper bound should never be equal
6600 to (or greater than) the size of the destination. */
6601 if (tree_fits_uhwi_p (maxread
) && tree_fits_uhwi_p (destsize
)
6602 && tree_int_cst_equal (destsize
, maxread
))
6604 location_t loc
= EXPR_LOCATION (exp
);
6605 warning_at (loc
, OPT_Wstringop_overflow_
,
6606 "%qD specified bound %E equals destination size",
6607 get_callee_fndecl (exp
), maxread
);
6613 || (maxread
&& tree_fits_uhwi_p (maxread
)
6614 && tree_fits_uhwi_p (srclen
)
6615 && tree_int_cst_lt (maxread
, srclen
)))
6618 check_access (exp
, /*dstwrite=*/NULL_TREE
, maxread
, srclen
,
6619 destsize
, data
.mode
, &data
);
6623 /* Expand expression EXP, which is a call to the strncpy builtin. Return
6624 NULL_RTX if we failed the caller should emit a normal call. */
6627 expand_builtin_strncpy (tree exp
, rtx target
)
6629 location_t loc
= EXPR_LOCATION (exp
);
6631 if (!validate_arglist (exp
,
6632 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6634 tree dest
= CALL_EXPR_ARG (exp
, 0);
6635 tree src
= CALL_EXPR_ARG (exp
, 1);
6636 /* The number of bytes to write (not the maximum). */
6637 tree len
= CALL_EXPR_ARG (exp
, 2);
6639 /* The length of the source sequence. */
6640 tree slen
= c_strlen (src
, 1);
6642 if (warn_stringop_overflow
)
6644 access_data
data (exp
, access_read_write
, len
, true, len
, true);
6645 const int ost
= warn_stringop_overflow
? warn_stringop_overflow
- 1 : 1;
6646 compute_objsize (src
, ost
, &data
.src
);
6647 tree dstsize
= compute_objsize (dest
, ost
, &data
.dst
);
6648 /* The number of bytes to write is LEN but check_access will also
6649 check SLEN if LEN's value isn't known. */
6650 check_access (exp
, /*dstwrite=*/len
,
6651 /*maxread=*/len
, src
, dstsize
, data
.mode
, &data
);
6654 /* We must be passed a constant len and src parameter. */
6655 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
6658 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
6660 /* We're required to pad with trailing zeros if the requested
6661 len is greater than strlen(s2)+1. In that case try to
6662 use store_by_pieces, if it fails, punt. */
6663 if (tree_int_cst_lt (slen
, len
))
6665 unsigned int dest_align
= get_pointer_alignment (dest
);
6666 const char *p
= c_getstr (src
);
6669 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
6670 || !can_store_by_pieces (tree_to_uhwi (len
),
6671 builtin_strncpy_read_str
,
6672 CONST_CAST (char *, p
),
6676 dest_mem
= get_memory_rtx (dest
, len
);
6677 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
6678 builtin_strncpy_read_str
,
6679 CONST_CAST (char *, p
), dest_align
, false,
6681 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
6682 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
6689 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
6690 bytes from constant string DATA + OFFSET and return it as target
6691 constant. If PREV isn't nullptr, it has the RTL info from the
6692 previous iteration. */
6695 builtin_memset_read_str (void *data
, void *prevp
,
6696 HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
6697 scalar_int_mode mode
)
6699 by_pieces_prev
*prev
= (by_pieces_prev
*) prevp
;
6700 if (prev
!= nullptr && prev
->data
!= nullptr)
6702 /* Use the previous data in the same mode. */
6703 if (prev
->mode
== mode
)
6707 const char *c
= (const char *) data
;
6708 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
6710 memset (p
, *c
, GET_MODE_SIZE (mode
));
6712 return c_readstr (p
, mode
);
6715 /* Callback routine for store_by_pieces. Return the RTL of a register
6716 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
6717 char value given in the RTL register data. For example, if mode is
6718 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
6719 nullptr, it has the RTL info from the previous iteration. */
6722 builtin_memset_gen_str (void *data
, void *prevp
,
6723 HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
6724 scalar_int_mode mode
)
6730 by_pieces_prev
*prev
= (by_pieces_prev
*) prevp
;
6731 if (prev
!= nullptr && prev
->data
!= nullptr)
6733 /* Use the previous data in the same mode. */
6734 if (prev
->mode
== mode
)
6737 target
= simplify_gen_subreg (mode
, prev
->data
, prev
->mode
, 0);
6738 if (target
!= nullptr)
6742 size
= GET_MODE_SIZE (mode
);
6746 p
= XALLOCAVEC (char, size
);
6747 memset (p
, 1, size
);
6748 coeff
= c_readstr (p
, mode
);
6750 target
= convert_to_mode (mode
, (rtx
) data
, 1);
6751 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
6752 return force_reg (mode
, target
);
6755 /* Expand expression EXP, which is a call to the memset builtin. Return
6756 NULL_RTX if we failed the caller should emit a normal call, otherwise
6757 try to get the result in TARGET, if convenient (and in mode MODE if that's
6761 expand_builtin_memset (tree exp
, rtx target
, machine_mode mode
)
6763 if (!validate_arglist (exp
,
6764 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6767 tree dest
= CALL_EXPR_ARG (exp
, 0);
6768 tree val
= CALL_EXPR_ARG (exp
, 1);
6769 tree len
= CALL_EXPR_ARG (exp
, 2);
6771 check_memop_access (exp
, dest
, NULL_TREE
, len
);
6773 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
6776 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
6777 Return TRUE if successful, FALSE otherwise. TO is assumed to be
6778 aligned at an ALIGN-bits boundary. LEN must be a multiple of
6779 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
6781 The strategy is to issue one store_by_pieces for each power of two,
6782 from most to least significant, guarded by a test on whether there
6783 are at least that many bytes left to copy in LEN.
6785 ??? Should we skip some powers of two in favor of loops? Maybe start
6786 at the max of TO/LEN/word alignment, at least when optimizing for
6787 size, instead of ensuring O(log len) dynamic compares? */
6790 try_store_by_multiple_pieces (rtx to
, rtx len
, unsigned int ctz_len
,
6791 unsigned HOST_WIDE_INT min_len
,
6792 unsigned HOST_WIDE_INT max_len
,
6793 rtx val
, char valc
, unsigned int align
)
6795 int max_bits
= floor_log2 (max_len
);
6796 int min_bits
= floor_log2 (min_len
);
6797 int sctz_len
= ctz_len
;
6799 gcc_checking_assert (sctz_len
>= 0);
6804 /* Bits more significant than TST_BITS are part of the shared prefix
6805 in the binary representation of both min_len and max_len. Since
6806 they're identical, we don't need to test them in the loop. */
6807 int tst_bits
= (max_bits
!= min_bits
? max_bits
6808 : floor_log2 (max_len
^ min_len
));
6810 /* Check whether it's profitable to start by storing a fixed BLKSIZE
6811 bytes, to lower max_bits. In the unlikely case of a constant LEN
6812 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
6813 single store_by_pieces, but otherwise, select the minimum multiple
6814 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
6815 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
6816 unsigned HOST_WIDE_INT blksize
;
6817 if (max_len
> min_len
)
6819 unsigned HOST_WIDE_INT alrng
= MAX (HOST_WIDE_INT_1U
<< ctz_len
,
6820 align
/ BITS_PER_UNIT
);
6821 blksize
= max_len
- (HOST_WIDE_INT_1U
<< tst_bits
) + alrng
;
6822 blksize
&= ~(alrng
- 1);
6824 else if (max_len
== min_len
)
6828 if (min_len
>= blksize
)
6831 min_bits
= floor_log2 (min_len
);
6833 max_bits
= floor_log2 (max_len
);
6835 tst_bits
= (max_bits
!= min_bits
? max_bits
6836 : floor_log2 (max_len
^ min_len
));
6841 /* Check that we can use store by pieces for the maximum store count
6842 we may issue (initial fixed-size block, plus conditional
6843 power-of-two-sized from max_bits to ctz_len. */
6844 unsigned HOST_WIDE_INT xlenest
= blksize
;
6846 xlenest
+= ((HOST_WIDE_INT_1U
<< max_bits
) * 2
6847 - (HOST_WIDE_INT_1U
<< ctz_len
));
6848 if (!can_store_by_pieces (xlenest
, builtin_memset_read_str
,
6849 &valc
, align
, true))
6852 rtx (*constfun
) (void *, void *, HOST_WIDE_INT
, scalar_int_mode
);
6856 constfun
= builtin_memset_gen_str
;
6857 constfundata
= val
= force_reg (TYPE_MODE (unsigned_char_type_node
),
6862 constfun
= builtin_memset_read_str
;
6863 constfundata
= &valc
;
6866 rtx ptr
= copy_addr_to_reg (convert_to_mode (ptr_mode
, XEXP (to
, 0), 0));
6867 rtx rem
= copy_to_mode_reg (ptr_mode
, convert_to_mode (ptr_mode
, len
, 0));
6868 to
= replace_equiv_address (to
, ptr
);
6869 set_mem_align (to
, align
);
6873 to
= store_by_pieces (to
, blksize
,
6874 constfun
, constfundata
,
6876 max_len
!= 0 ? RETURN_END
: RETURN_BEGIN
);
6880 /* Adjust PTR, TO and REM. Since TO's address is likely
6881 PTR+offset, we have to replace it. */
6882 emit_move_insn (ptr
, force_operand (XEXP (to
, 0), NULL_RTX
));
6883 to
= replace_equiv_address (to
, ptr
);
6884 rtx rem_minus_blksize
= plus_constant (ptr_mode
, rem
, -blksize
);
6885 emit_move_insn (rem
, force_operand (rem_minus_blksize
, NULL_RTX
));
6888 /* Iterate over power-of-two block sizes from the maximum length to
6889 the least significant bit possibly set in the length. */
6890 for (int i
= max_bits
; i
>= sctz_len
; i
--)
6892 rtx_code_label
*label
= NULL
;
6893 blksize
= HOST_WIDE_INT_1U
<< i
;
6895 /* If we're past the bits shared between min_ and max_len, expand
6896 a test on the dynamic length, comparing it with the
6900 label
= gen_label_rtx ();
6901 emit_cmp_and_jump_insns (rem
, GEN_INT (blksize
), LT
, NULL
,
6903 profile_probability::even ());
6905 /* If we are at a bit that is in the prefix shared by min_ and
6906 max_len, skip this BLKSIZE if the bit is clear. */
6907 else if ((max_len
& blksize
) == 0)
6910 /* Issue a store of BLKSIZE bytes. */
6911 to
= store_by_pieces (to
, blksize
,
6912 constfun
, constfundata
,
6914 i
!= sctz_len
? RETURN_END
: RETURN_BEGIN
);
6916 /* Adjust REM and PTR, unless this is the last iteration. */
6919 emit_move_insn (ptr
, force_operand (XEXP (to
, 0), NULL_RTX
));
6920 to
= replace_equiv_address (to
, ptr
);
6921 rtx rem_minus_blksize
= plus_constant (ptr_mode
, rem
, -blksize
);
6922 emit_move_insn (rem
, force_operand (rem_minus_blksize
, NULL_RTX
));
6929 /* Given conditional stores, the offset can no longer be
6930 known, so clear it. */
6931 clear_mem_offset (to
);
6938 /* Helper function to do the actual work for expand_builtin_memset. The
6939 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
6940 so that this can also be called without constructing an actual CALL_EXPR.
6941 The other arguments and return value are the same as for
6942 expand_builtin_memset. */
6945 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
6946 rtx target
, machine_mode mode
, tree orig_exp
)
6949 enum built_in_function fcode
;
6950 machine_mode val_mode
;
6952 unsigned int dest_align
;
6953 rtx dest_mem
, dest_addr
, len_rtx
;
6954 HOST_WIDE_INT expected_size
= -1;
6955 unsigned int expected_align
= 0;
6956 unsigned HOST_WIDE_INT min_size
;
6957 unsigned HOST_WIDE_INT max_size
;
6958 unsigned HOST_WIDE_INT probable_max_size
;
6960 dest_align
= get_pointer_alignment (dest
);
6962 /* If DEST is not a pointer type, don't do this operation in-line. */
6963 if (dest_align
== 0)
6966 if (currently_expanding_gimple_stmt
)
6967 stringop_block_profile (currently_expanding_gimple_stmt
,
6968 &expected_align
, &expected_size
);
6970 if (expected_align
< dest_align
)
6971 expected_align
= dest_align
;
6973 /* If the LEN parameter is zero, return DEST. */
6974 if (integer_zerop (len
))
6976 /* Evaluate and ignore VAL in case it has side-effects. */
6977 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6978 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
6981 /* Stabilize the arguments in case we fail. */
6982 dest
= builtin_save_expr (dest
);
6983 val
= builtin_save_expr (val
);
6984 len
= builtin_save_expr (len
);
6986 len_rtx
= expand_normal (len
);
6987 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
6988 &probable_max_size
);
6989 dest_mem
= get_memory_rtx (dest
, len
);
6990 val_mode
= TYPE_MODE (unsigned_char_type_node
);
6992 if (TREE_CODE (val
) != INTEGER_CST
6993 || target_char_cast (val
, &c
))
6997 val_rtx
= expand_normal (val
);
6998 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
7000 /* Assume that we can memset by pieces if we can store
7001 * the coefficients by pieces (in the required modes).
7002 * We can't pass builtin_memset_gen_str as that emits RTL. */
7004 if (tree_fits_uhwi_p (len
)
7005 && can_store_by_pieces (tree_to_uhwi (len
),
7006 builtin_memset_read_str
, &c
, dest_align
,
7009 val_rtx
= force_reg (val_mode
, val_rtx
);
7010 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
7011 builtin_memset_gen_str
, val_rtx
, dest_align
,
7012 true, RETURN_BEGIN
);
7014 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
7015 dest_align
, expected_align
,
7016 expected_size
, min_size
, max_size
,
7018 && !try_store_by_multiple_pieces (dest_mem
, len_rtx
,
7025 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
7026 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
7032 if (tree_fits_uhwi_p (len
)
7033 && can_store_by_pieces (tree_to_uhwi (len
),
7034 builtin_memset_read_str
, &c
, dest_align
,
7036 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
7037 builtin_memset_read_str
, &c
, dest_align
, true,
7039 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
7040 gen_int_mode (c
, val_mode
),
7041 dest_align
, expected_align
,
7042 expected_size
, min_size
, max_size
,
7044 && !try_store_by_multiple_pieces (dest_mem
, len_rtx
,
7051 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
7052 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
7056 set_mem_align (dest_mem
, dest_align
);
7057 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
7058 CALL_EXPR_TAILCALL (orig_exp
)
7059 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
7060 expected_align
, expected_size
,
7062 probable_max_size
, tree_ctz (len
));
7066 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
7067 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
7073 fndecl
= get_callee_fndecl (orig_exp
);
7074 fcode
= DECL_FUNCTION_CODE (fndecl
);
7075 if (fcode
== BUILT_IN_MEMSET
)
7076 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
7078 else if (fcode
== BUILT_IN_BZERO
)
7079 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
7083 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
7084 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
7085 return expand_call (fn
, target
, target
== const0_rtx
);
7088 /* Expand expression EXP, which is a call to the bzero builtin. Return
7089 NULL_RTX if we failed the caller should emit a normal call. */
7092 expand_builtin_bzero (tree exp
)
7094 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7097 tree dest
= CALL_EXPR_ARG (exp
, 0);
7098 tree size
= CALL_EXPR_ARG (exp
, 1);
7100 check_memop_access (exp
, dest
, NULL_TREE
, size
);
7102 /* New argument list transforming bzero(ptr x, int y) to
7103 memset(ptr x, int 0, size_t y). This is done this way
7104 so that if it isn't expanded inline, we fallback to
7105 calling bzero instead of memset. */
7107 location_t loc
= EXPR_LOCATION (exp
);
7109 return expand_builtin_memset_args (dest
, integer_zero_node
,
7110 fold_convert_loc (loc
,
7111 size_type_node
, size
),
7112 const0_rtx
, VOIDmode
, exp
);
7115 /* Try to expand cmpstr operation ICODE with the given operands.
7116 Return the result rtx on success, otherwise return null. */
7119 expand_cmpstr (insn_code icode
, rtx target
, rtx arg1_rtx
, rtx arg2_rtx
,
7120 HOST_WIDE_INT align
)
7122 machine_mode insn_mode
= insn_data
[icode
].operand
[0].mode
;
7124 if (target
&& (!REG_P (target
) || HARD_REGISTER_P (target
)))
7127 class expand_operand ops
[4];
7128 create_output_operand (&ops
[0], target
, insn_mode
);
7129 create_fixed_operand (&ops
[1], arg1_rtx
);
7130 create_fixed_operand (&ops
[2], arg2_rtx
);
7131 create_integer_operand (&ops
[3], align
);
7132 if (maybe_expand_insn (icode
, 4, ops
))
7133 return ops
[0].value
;
7137 /* Expand expression EXP, which is a call to the memcmp built-in function.
7138 Return NULL_RTX if we failed and the caller should emit a normal call,
7139 otherwise try to get the result in TARGET, if convenient.
7140 RESULT_EQ is true if we can relax the returned value to be either zero
7141 or nonzero, without caring about the sign. */
7144 expand_builtin_memcmp (tree exp
, rtx target
, bool result_eq
)
7146 if (!validate_arglist (exp
,
7147 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7150 tree arg1
= CALL_EXPR_ARG (exp
, 0);
7151 tree arg2
= CALL_EXPR_ARG (exp
, 1);
7152 tree len
= CALL_EXPR_ARG (exp
, 2);
7154 /* Diagnose calls where the specified length exceeds the size of either
7156 if (!check_read_access (exp
, arg1
, len
, 0)
7157 || !check_read_access (exp
, arg2
, len
, 0))
7160 /* Due to the performance benefit, always inline the calls first
7161 when result_eq is false. */
7162 rtx result
= NULL_RTX
;
7163 enum built_in_function fcode
= DECL_FUNCTION_CODE (get_callee_fndecl (exp
));
7164 if (!result_eq
&& fcode
!= BUILT_IN_BCMP
)
7166 result
= inline_expand_builtin_bytecmp (exp
, target
);
7171 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
7172 location_t loc
= EXPR_LOCATION (exp
);
7174 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
7175 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
7177 /* If we don't have POINTER_TYPE, call the function. */
7178 if (arg1_align
== 0 || arg2_align
== 0)
7181 rtx arg1_rtx
= get_memory_rtx (arg1
, len
);
7182 rtx arg2_rtx
= get_memory_rtx (arg2
, len
);
7183 rtx len_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
7185 /* Set MEM_SIZE as appropriate. */
7186 if (CONST_INT_P (len_rtx
))
7188 set_mem_size (arg1_rtx
, INTVAL (len_rtx
));
7189 set_mem_size (arg2_rtx
, INTVAL (len_rtx
));
7192 by_pieces_constfn constfn
= NULL
;
7194 /* Try to get the byte representation of the constant ARG2 (or, only
7195 when the function's result is used for equality to zero, ARG1)
7196 points to, with its byte size in NBYTES. */
7197 unsigned HOST_WIDE_INT nbytes
;
7198 const char *rep
= getbyterep (arg2
, &nbytes
);
7199 if (result_eq
&& rep
== NULL
)
7201 /* For equality to zero the arguments are interchangeable. */
7202 rep
= getbyterep (arg1
, &nbytes
);
7204 std::swap (arg1_rtx
, arg2_rtx
);
7207 /* If the function's constant bound LEN_RTX is less than or equal
7208 to the byte size of the representation of the constant argument,
7209 and if block move would be done by pieces, we can avoid loading
7210 the bytes from memory and only store the computed constant result. */
7212 && CONST_INT_P (len_rtx
)
7213 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= nbytes
)
7214 constfn
= builtin_memcpy_read_str
;
7216 result
= emit_block_cmp_hints (arg1_rtx
, arg2_rtx
, len_rtx
,
7217 TREE_TYPE (len
), target
,
7219 CONST_CAST (char *, rep
));
7223 /* Return the value in the proper mode for this function. */
7224 if (GET_MODE (result
) == mode
)
7229 convert_move (target
, result
, 0);
7233 return convert_to_mode (mode
, result
, 0);
7239 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
7240 if we failed the caller should emit a normal call, otherwise try to get
7241 the result in TARGET, if convenient. */
7244 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
7246 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
7249 tree arg1
= CALL_EXPR_ARG (exp
, 0);
7250 tree arg2
= CALL_EXPR_ARG (exp
, 1);
7252 if (!check_read_access (exp
, arg1
)
7253 || !check_read_access (exp
, arg2
))
7256 /* Due to the performance benefit, always inline the calls first. */
7257 rtx result
= NULL_RTX
;
7258 result
= inline_expand_builtin_bytecmp (exp
, target
);
7262 insn_code cmpstr_icode
= direct_optab_handler (cmpstr_optab
, SImode
);
7263 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
7264 if (cmpstr_icode
== CODE_FOR_nothing
&& cmpstrn_icode
== CODE_FOR_nothing
)
7267 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
7268 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
7270 /* If we don't have POINTER_TYPE, call the function. */
7271 if (arg1_align
== 0 || arg2_align
== 0)
7274 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
7275 arg1
= builtin_save_expr (arg1
);
7276 arg2
= builtin_save_expr (arg2
);
7278 rtx arg1_rtx
= get_memory_rtx (arg1
, NULL
);
7279 rtx arg2_rtx
= get_memory_rtx (arg2
, NULL
);
7281 /* Try to call cmpstrsi. */
7282 if (cmpstr_icode
!= CODE_FOR_nothing
)
7283 result
= expand_cmpstr (cmpstr_icode
, target
, arg1_rtx
, arg2_rtx
,
7284 MIN (arg1_align
, arg2_align
));
7286 /* Try to determine at least one length and call cmpstrnsi. */
7287 if (!result
&& cmpstrn_icode
!= CODE_FOR_nothing
)
7292 tree len1
= c_strlen (arg1
, 1);
7293 tree len2
= c_strlen (arg2
, 1);
7296 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
7298 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
7300 /* If we don't have a constant length for the first, use the length
7301 of the second, if we know it. We don't require a constant for
7302 this case; some cost analysis could be done if both are available
7303 but neither is constant. For now, assume they're equally cheap,
7304 unless one has side effects. If both strings have constant lengths,
7311 else if (TREE_SIDE_EFFECTS (len1
))
7313 else if (TREE_SIDE_EFFECTS (len2
))
7315 else if (TREE_CODE (len1
) != INTEGER_CST
)
7317 else if (TREE_CODE (len2
) != INTEGER_CST
)
7319 else if (tree_int_cst_lt (len1
, len2
))
7324 /* If both arguments have side effects, we cannot optimize. */
7325 if (len
&& !TREE_SIDE_EFFECTS (len
))
7327 arg3_rtx
= expand_normal (len
);
7328 result
= expand_cmpstrn_or_cmpmem
7329 (cmpstrn_icode
, target
, arg1_rtx
, arg2_rtx
, TREE_TYPE (len
),
7330 arg3_rtx
, MIN (arg1_align
, arg2_align
));
7334 tree fndecl
= get_callee_fndecl (exp
);
7337 /* Check to see if the argument was declared attribute nonstring
7338 and if so, issue a warning since at this point it's not known
7339 to be nul-terminated. */
7340 maybe_warn_nonstring_arg (fndecl
, exp
);
7342 /* Return the value in the proper mode for this function. */
7343 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
7344 if (GET_MODE (result
) == mode
)
7347 return convert_to_mode (mode
, result
, 0);
7348 convert_move (target
, result
, 0);
7352 /* Expand the library call ourselves using a stabilized argument
7353 list to avoid re-evaluating the function's arguments twice. */
7354 tree fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
7355 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
7356 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
7357 return expand_call (fn
, target
, target
== const0_rtx
);
7360 /* Expand expression EXP, which is a call to the strncmp builtin. Return
7361 NULL_RTX if we failed the caller should emit a normal call, otherwise
7362 try to get the result in TARGET, if convenient. */
7365 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
7366 ATTRIBUTE_UNUSED machine_mode mode
)
7368 if (!validate_arglist (exp
,
7369 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7372 tree arg1
= CALL_EXPR_ARG (exp
, 0);
7373 tree arg2
= CALL_EXPR_ARG (exp
, 1);
7374 tree arg3
= CALL_EXPR_ARG (exp
, 2);
7376 if (!check_nul_terminated_array (exp
, arg1
, arg3
)
7377 || !check_nul_terminated_array (exp
, arg2
, arg3
))
7380 location_t loc
= EXPR_LOCATION (exp
);
7381 tree len1
= c_strlen (arg1
, 1);
7382 tree len2
= c_strlen (arg2
, 1);
7386 /* Check to see if the argument was declared attribute nonstring
7387 and if so, issue a warning since at this point it's not known
7388 to be nul-terminated. */
7389 if (!maybe_warn_nonstring_arg (get_callee_fndecl (exp
), exp
)
7392 /* A strncmp read is constrained not just by the bound but
7393 also by the length of the shorter string. Specifying
7394 a bound that's larger than the size of either array makes
7395 no sense and is likely a bug. When the length of neither
7396 of the two strings is known but the sizes of both of
7397 the arrays they are stored in is, issue a warning if
7398 the bound is larger than than the size of the larger
7399 of the two arrays. */
7401 access_ref
ref1 (arg3
, true);
7402 access_ref
ref2 (arg3
, true);
7404 tree bndrng
[2] = { NULL_TREE
, NULL_TREE
};
7405 get_size_range (arg3
, bndrng
, ref1
.bndrng
);
7407 tree size1
= compute_objsize (arg1
, 1, &ref1
);
7408 tree size2
= compute_objsize (arg2
, 1, &ref2
);
7409 tree func
= get_callee_fndecl (exp
);
7411 if (size1
&& size2
&& bndrng
[0] && !integer_zerop (bndrng
[0]))
7413 offset_int rem1
= ref1
.size_remaining ();
7414 offset_int rem2
= ref2
.size_remaining ();
7415 if (rem1
== 0 || rem2
== 0)
7416 maybe_warn_for_bound (OPT_Wstringop_overread
, loc
, exp
, func
,
7417 bndrng
, integer_zero_node
);
7420 offset_int maxrem
= wi::max (rem1
, rem2
, UNSIGNED
);
7421 if (maxrem
< wi::to_offset (bndrng
[0]))
7422 maybe_warn_for_bound (OPT_Wstringop_overread
, loc
, exp
,
7424 wide_int_to_tree (sizetype
, maxrem
));
7428 && !integer_zerop (bndrng
[0])
7429 && ((size1
&& integer_zerop (size1
))
7430 || (size2
&& integer_zerop (size2
))))
7431 maybe_warn_for_bound (OPT_Wstringop_overread
, loc
, exp
, func
,
7432 bndrng
, integer_zero_node
);
7436 /* Due to the performance benefit, always inline the calls first. */
7437 rtx result
= NULL_RTX
;
7438 result
= inline_expand_builtin_bytecmp (exp
, target
);
7442 /* If c_strlen can determine an expression for one of the string
7443 lengths, and it doesn't have side effects, then emit cmpstrnsi
7444 using length MIN(strlen(string)+1, arg3). */
7445 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
7446 if (cmpstrn_icode
== CODE_FOR_nothing
)
7451 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
7452 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
7455 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
7457 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
7459 tree len3
= fold_convert_loc (loc
, sizetype
, arg3
);
7461 /* If we don't have a constant length for the first, use the length
7462 of the second, if we know it. If neither string is constant length,
7463 use the given length argument. We don't require a constant for
7464 this case; some cost analysis could be done if both are available
7465 but neither is constant. For now, assume they're equally cheap,
7466 unless one has side effects. If both strings have constant lengths,
7475 else if (TREE_SIDE_EFFECTS (len1
))
7477 else if (TREE_SIDE_EFFECTS (len2
))
7479 else if (TREE_CODE (len1
) != INTEGER_CST
)
7481 else if (TREE_CODE (len2
) != INTEGER_CST
)
7483 else if (tree_int_cst_lt (len1
, len2
))
7488 /* If we are not using the given length, we must incorporate it here.
7489 The actual new length parameter will be MIN(len,arg3) in this case. */
7492 len
= fold_convert_loc (loc
, sizetype
, len
);
7493 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
, len3
);
7495 rtx arg1_rtx
= get_memory_rtx (arg1
, len
);
7496 rtx arg2_rtx
= get_memory_rtx (arg2
, len
);
7497 rtx arg3_rtx
= expand_normal (len
);
7498 result
= expand_cmpstrn_or_cmpmem (cmpstrn_icode
, target
, arg1_rtx
,
7499 arg2_rtx
, TREE_TYPE (len
), arg3_rtx
,
7500 MIN (arg1_align
, arg2_align
));
7502 tree fndecl
= get_callee_fndecl (exp
);
7505 /* Return the value in the proper mode for this function. */
7506 mode
= TYPE_MODE (TREE_TYPE (exp
));
7507 if (GET_MODE (result
) == mode
)
7510 return convert_to_mode (mode
, result
, 0);
7511 convert_move (target
, result
, 0);
7515 /* Expand the library call ourselves using a stabilized argument
7516 list to avoid re-evaluating the function's arguments twice. */
7517 tree call
= build_call_nofold_loc (loc
, fndecl
, 3, arg1
, arg2
, len
);
7518 copy_warning (call
, exp
);
7519 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
7520 CALL_EXPR_TAILCALL (call
) = CALL_EXPR_TAILCALL (exp
);
7521 return expand_call (call
, target
, target
== const0_rtx
);
7524 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
7525 if that's convenient. */
7528 expand_builtin_saveregs (void)
7533 /* Don't do __builtin_saveregs more than once in a function.
7534 Save the result of the first call and reuse it. */
7535 if (saveregs_value
!= 0)
7536 return saveregs_value
;
7538 /* When this function is called, it means that registers must be
7539 saved on entry to this function. So we migrate the call to the
7540 first insn of this function. */
7544 /* Do whatever the machine needs done in this case. */
7545 val
= targetm
.calls
.expand_builtin_saveregs ();
7550 saveregs_value
= val
;
7552 /* Put the insns after the NOTE that starts the function. If this
7553 is inside a start_sequence, make the outer-level insn chain current, so
7554 the code is placed at the start of the function. */
7555 push_topmost_sequence ();
7556 emit_insn_after (seq
, entry_of_function ());
7557 pop_topmost_sequence ();
7562 /* Expand a call to __builtin_next_arg. */
7565 expand_builtin_next_arg (void)
7567 /* Checking arguments is already done in fold_builtin_next_arg
7568 that must be called before this function. */
7569 return expand_binop (ptr_mode
, add_optab
,
7570 crtl
->args
.internal_arg_pointer
,
7571 crtl
->args
.arg_offset_rtx
,
7572 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
7575 /* Make it easier for the backends by protecting the valist argument
7576 from multiple evaluations. */
7579 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
7581 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
7583 /* The current way of determining the type of valist is completely
7584 bogus. We should have the information on the va builtin instead. */
7586 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
7588 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
7590 if (TREE_SIDE_EFFECTS (valist
))
7591 valist
= save_expr (valist
);
7593 /* For this case, the backends will be expecting a pointer to
7594 vatype, but it's possible we've actually been given an array
7595 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
7597 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
7599 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
7600 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
7605 tree pt
= build_pointer_type (vatype
);
7609 if (! TREE_SIDE_EFFECTS (valist
))
7612 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
7613 TREE_SIDE_EFFECTS (valist
) = 1;
7616 if (TREE_SIDE_EFFECTS (valist
))
7617 valist
= save_expr (valist
);
7618 valist
= fold_build2_loc (loc
, MEM_REF
,
7619 vatype
, valist
, build_int_cst (pt
, 0));
7625 /* The "standard" definition of va_list is void*. */
7628 std_build_builtin_va_list (void)
7630 return ptr_type_node
;
7633 /* The "standard" abi va_list is va_list_type_node. */
7636 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
7638 return va_list_type_node
;
7641 /* The "standard" type of va_list is va_list_type_node. */
7644 std_canonical_va_list_type (tree type
)
7648 wtype
= va_list_type_node
;
7651 if (TREE_CODE (wtype
) == ARRAY_TYPE
)
7653 /* If va_list is an array type, the argument may have decayed
7654 to a pointer type, e.g. by being passed to another function.
7655 In that case, unwrap both types so that we can compare the
7656 underlying records. */
7657 if (TREE_CODE (htype
) == ARRAY_TYPE
7658 || POINTER_TYPE_P (htype
))
7660 wtype
= TREE_TYPE (wtype
);
7661 htype
= TREE_TYPE (htype
);
7664 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
7665 return va_list_type_node
;
7670 /* The "standard" implementation of va_start: just assign `nextarg' to
7674 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
7676 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
7677 convert_move (va_r
, nextarg
, 0);
7680 /* Expand EXP, a call to __builtin_va_start. */
7683 expand_builtin_va_start (tree exp
)
7687 location_t loc
= EXPR_LOCATION (exp
);
7689 if (call_expr_nargs (exp
) < 2)
7691 error_at (loc
, "too few arguments to function %<va_start%>");
7695 if (fold_builtin_next_arg (exp
, true))
7698 nextarg
= expand_builtin_next_arg ();
7699 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
7701 if (targetm
.expand_builtin_va_start
)
7702 targetm
.expand_builtin_va_start (valist
, nextarg
);
7704 std_expand_builtin_va_start (valist
, nextarg
);
7709 /* Expand EXP, a call to __builtin_va_end. */
7712 expand_builtin_va_end (tree exp
)
7714 tree valist
= CALL_EXPR_ARG (exp
, 0);
7716 /* Evaluate for side effects, if needed. I hate macros that don't
7718 if (TREE_SIDE_EFFECTS (valist
))
7719 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7724 /* Expand EXP, a call to __builtin_va_copy. We do this as a
7725 builtin rather than just as an assignment in stdarg.h because of the
7726 nastiness of array-type va_list types. */
7729 expand_builtin_va_copy (tree exp
)
7732 location_t loc
= EXPR_LOCATION (exp
);
7734 dst
= CALL_EXPR_ARG (exp
, 0);
7735 src
= CALL_EXPR_ARG (exp
, 1);
7737 dst
= stabilize_va_list_loc (loc
, dst
, 1);
7738 src
= stabilize_va_list_loc (loc
, src
, 0);
7740 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
7742 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
7744 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
7745 TREE_SIDE_EFFECTS (t
) = 1;
7746 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7750 rtx dstb
, srcb
, size
;
7752 /* Evaluate to pointers. */
7753 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
7754 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
7755 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
7756 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
7758 dstb
= convert_memory_address (Pmode
, dstb
);
7759 srcb
= convert_memory_address (Pmode
, srcb
);
7761 /* "Dereference" to BLKmode memories. */
7762 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
7763 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
7764 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
7765 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
7766 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
7767 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
7770 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
7776 /* Expand a call to one of the builtin functions __builtin_frame_address or
7777 __builtin_return_address. */
7780 expand_builtin_frame_address (tree fndecl
, tree exp
)
7782 /* The argument must be a nonnegative integer constant.
7783 It counts the number of frames to scan up the stack.
7784 The value is either the frame pointer value or the return
7785 address saved in that frame. */
7786 if (call_expr_nargs (exp
) == 0)
7787 /* Warning about missing arg was already issued. */
7789 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
7791 error ("invalid argument to %qD", fndecl
);
7796 /* Number of frames to scan up the stack. */
7797 unsigned HOST_WIDE_INT count
= tree_to_uhwi (CALL_EXPR_ARG (exp
, 0));
7799 rtx tem
= expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
), count
);
7801 /* Some ports cannot access arbitrary stack frames. */
7804 warning (0, "unsupported argument to %qD", fndecl
);
7810 /* Warn since no effort is made to ensure that any frame
7811 beyond the current one exists or can be safely reached. */
7812 warning (OPT_Wframe_address
, "calling %qD with "
7813 "a nonzero argument is unsafe", fndecl
);
7816 /* For __builtin_frame_address, return what we've got. */
7817 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
7821 && ! CONSTANT_P (tem
))
7822 tem
= copy_addr_to_reg (tem
);
7827 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
7828 failed and the caller should emit a normal call. */
7831 expand_builtin_alloca (tree exp
)
7836 tree fndecl
= get_callee_fndecl (exp
);
7837 HOST_WIDE_INT max_size
;
7838 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7839 bool alloca_for_var
= CALL_ALLOCA_FOR_VAR_P (exp
);
7841 = (fcode
== BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
7842 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
,
7844 : fcode
== BUILT_IN_ALLOCA_WITH_ALIGN
7845 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
7846 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
7852 && warn_vla_limit
>= HOST_WIDE_INT_MAX
7853 && warn_alloc_size_limit
< warn_vla_limit
)
7855 && warn_alloca_limit
>= HOST_WIDE_INT_MAX
7856 && warn_alloc_size_limit
< warn_alloca_limit
7859 /* -Walloca-larger-than and -Wvla-larger-than settings of
7860 less than HOST_WIDE_INT_MAX override the more general
7861 -Walloc-size-larger-than so unless either of the former
7862 options is smaller than the last one (wchich would imply
7863 that the call was already checked), check the alloca
7864 arguments for overflow. */
7865 tree args
[] = { CALL_EXPR_ARG (exp
, 0), NULL_TREE
};
7866 int idx
[] = { 0, -1 };
7867 maybe_warn_alloc_args_overflow (fndecl
, exp
, args
, idx
);
7870 /* Compute the argument. */
7871 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
7873 /* Compute the alignment. */
7874 align
= (fcode
== BUILT_IN_ALLOCA
7876 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1)));
7878 /* Compute the maximum size. */
7879 max_size
= (fcode
== BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
7880 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 2))
7883 /* Allocate the desired space. If the allocation stems from the declaration
7884 of a variable-sized object, it cannot accumulate. */
7886 = allocate_dynamic_stack_space (op0
, 0, align
, max_size
, alloca_for_var
);
7887 result
= convert_memory_address (ptr_mode
, result
);
7889 /* Dynamic allocations for variables are recorded during gimplification. */
7890 if (!alloca_for_var
&& (flag_callgraph_info
& CALLGRAPH_INFO_DYNAMIC_ALLOC
))
7891 record_dynamic_alloc (exp
);
7896 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
7897 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
7898 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
7899 handle_builtin_stack_restore function. */
7902 expand_asan_emit_allocas_unpoison (tree exp
)
7904 tree arg0
= CALL_EXPR_ARG (exp
, 0);
7905 tree arg1
= CALL_EXPR_ARG (exp
, 1);
7906 rtx top
= expand_expr (arg0
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
7907 rtx bot
= expand_expr (arg1
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
7908 rtx off
= expand_simple_binop (Pmode
, MINUS
, virtual_stack_dynamic_rtx
,
7909 stack_pointer_rtx
, NULL_RTX
, 0,
7911 off
= convert_modes (ptr_mode
, Pmode
, off
, 0);
7912 bot
= expand_simple_binop (ptr_mode
, PLUS
, bot
, off
, NULL_RTX
, 0,
7914 rtx ret
= init_one_libfunc ("__asan_allocas_unpoison");
7915 ret
= emit_library_call_value (ret
, NULL_RTX
, LCT_NORMAL
, ptr_mode
,
7916 top
, ptr_mode
, bot
, ptr_mode
);
7920 /* Expand a call to bswap builtin in EXP.
7921 Return NULL_RTX if a normal call should be emitted rather than expanding the
7922 function in-line. If convenient, the result should be placed in TARGET.
7923 SUBTARGET may be used as the target for computing one of EXP's operands. */
7926 expand_builtin_bswap (machine_mode target_mode
, tree exp
, rtx target
,
7932 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
7935 arg
= CALL_EXPR_ARG (exp
, 0);
7936 op0
= expand_expr (arg
,
7937 subtarget
&& GET_MODE (subtarget
) == target_mode
7938 ? subtarget
: NULL_RTX
,
7939 target_mode
, EXPAND_NORMAL
);
7940 if (GET_MODE (op0
) != target_mode
)
7941 op0
= convert_to_mode (target_mode
, op0
, 1);
7943 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
7945 gcc_assert (target
);
7947 return convert_to_mode (target_mode
, target
, 1);
7950 /* Expand a call to a unary builtin in EXP.
7951 Return NULL_RTX if a normal call should be emitted rather than expanding the
7952 function in-line. If convenient, the result should be placed in TARGET.
7953 SUBTARGET may be used as the target for computing one of EXP's operands. */
7956 expand_builtin_unop (machine_mode target_mode
, tree exp
, rtx target
,
7957 rtx subtarget
, optab op_optab
)
7961 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
7964 /* Compute the argument. */
7965 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
7967 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
7968 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
7969 VOIDmode
, EXPAND_NORMAL
);
7970 /* Compute op, into TARGET if possible.
7971 Set TARGET to wherever the result comes back. */
7972 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
7973 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
7974 gcc_assert (target
);
7976 return convert_to_mode (target_mode
, target
, 0);
7979 /* Expand a call to __builtin_expect. We just return our argument
7980 as the builtin_expect semantic should've been already executed by
7981 tree branch prediction pass. */
7984 expand_builtin_expect (tree exp
, rtx target
)
7988 if (call_expr_nargs (exp
) < 2)
7990 arg
= CALL_EXPR_ARG (exp
, 0);
7992 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
7993 /* When guessing was done, the hints should be already stripped away. */
7994 gcc_assert (!flag_guess_branch_prob
7995 || optimize
== 0 || seen_error ());
7999 /* Expand a call to __builtin_expect_with_probability. We just return our
8000 argument as the builtin_expect semantic should've been already executed by
8001 tree branch prediction pass. */
8004 expand_builtin_expect_with_probability (tree exp
, rtx target
)
8008 if (call_expr_nargs (exp
) < 3)
8010 arg
= CALL_EXPR_ARG (exp
, 0);
8012 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
8013 /* When guessing was done, the hints should be already stripped away. */
8014 gcc_assert (!flag_guess_branch_prob
8015 || optimize
== 0 || seen_error ());
8020 /* Expand a call to __builtin_assume_aligned. We just return our first
8021 argument as the builtin_assume_aligned semantic should've been already
8025 expand_builtin_assume_aligned (tree exp
, rtx target
)
8027 if (call_expr_nargs (exp
) < 2)
8029 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
8031 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
8032 && (call_expr_nargs (exp
) < 3
8033 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
8038 expand_builtin_trap (void)
8040 if (targetm
.have_trap ())
8042 rtx_insn
*insn
= emit_insn (targetm
.gen_trap ());
8043 /* For trap insns when not accumulating outgoing args force
8044 REG_ARGS_SIZE note to prevent crossjumping of calls with
8045 different args sizes. */
8046 if (!ACCUMULATE_OUTGOING_ARGS
)
8047 add_args_size_note (insn
, stack_pointer_delta
);
8051 tree fn
= builtin_decl_implicit (BUILT_IN_ABORT
);
8052 tree call_expr
= build_call_expr (fn
, 0);
8053 expand_call (call_expr
, NULL_RTX
, false);
8059 /* Expand a call to __builtin_unreachable. We do nothing except emit
8060 a barrier saying that control flow will not pass here.
8062 It is the responsibility of the program being compiled to ensure
8063 that control flow does never reach __builtin_unreachable. */
8065 expand_builtin_unreachable (void)
8070 /* Expand EXP, a call to fabs, fabsf or fabsl.
8071 Return NULL_RTX if a normal call should be emitted rather than expanding
8072 the function inline. If convenient, the result should be placed
8073 in TARGET. SUBTARGET may be used as the target for computing
8077 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
8083 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
8086 arg
= CALL_EXPR_ARG (exp
, 0);
8087 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
8088 mode
= TYPE_MODE (TREE_TYPE (arg
));
8089 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
8090 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
8093 /* Expand EXP, a call to copysign, copysignf, or copysignl.
8094 Return NULL is a normal call should be emitted rather than expanding the
8095 function inline. If convenient, the result should be placed in TARGET.
8096 SUBTARGET may be used as the target for computing the operand. */
8099 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
8104 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8107 arg
= CALL_EXPR_ARG (exp
, 0);
8108 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
8110 arg
= CALL_EXPR_ARG (exp
, 1);
8111 op1
= expand_normal (arg
);
8113 return expand_copysign (op0
, op1
, target
);
8116 /* Emit a call to __builtin___clear_cache. */
8119 default_emit_call_builtin___clear_cache (rtx begin
, rtx end
)
8121 rtx callee
= gen_rtx_SYMBOL_REF (Pmode
,
8122 BUILTIN_ASM_NAME_PTR
8123 (BUILT_IN_CLEAR_CACHE
));
8125 emit_library_call (callee
,
8126 LCT_NORMAL
, VOIDmode
,
8127 convert_memory_address (ptr_mode
, begin
), ptr_mode
,
8128 convert_memory_address (ptr_mode
, end
), ptr_mode
);
8131 /* Emit a call to __builtin___clear_cache, unless the target specifies
8132 it as do-nothing. This function can be used by trampoline
8133 finalizers to duplicate the effects of expanding a call to the
8134 clear_cache builtin. */
8137 maybe_emit_call_builtin___clear_cache (rtx begin
, rtx end
)
8139 if ((GET_MODE (begin
) != ptr_mode
&& GET_MODE (begin
) != Pmode
)
8140 || (GET_MODE (end
) != ptr_mode
&& GET_MODE (end
) != Pmode
))
8142 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
8146 if (targetm
.have_clear_cache ())
8148 /* We have a "clear_cache" insn, and it will handle everything. */
8149 class expand_operand ops
[2];
8151 create_address_operand (&ops
[0], begin
);
8152 create_address_operand (&ops
[1], end
);
8154 if (maybe_expand_insn (targetm
.code_for_clear_cache
, 2, ops
))
8159 #ifndef CLEAR_INSN_CACHE
8160 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
8161 does nothing. There is no need to call it. Do nothing. */
8163 #endif /* CLEAR_INSN_CACHE */
8166 targetm
.calls
.emit_call_builtin___clear_cache (begin
, end
);
8169 /* Expand a call to __builtin___clear_cache. */
8172 expand_builtin___clear_cache (tree exp
)
8175 rtx begin_rtx
, end_rtx
;
8177 /* We must not expand to a library call. If we did, any
8178 fallback library function in libgcc that might contain a call to
8179 __builtin___clear_cache() would recurse infinitely. */
8180 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8182 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
8186 begin
= CALL_EXPR_ARG (exp
, 0);
8187 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
8189 end
= CALL_EXPR_ARG (exp
, 1);
8190 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
8192 maybe_emit_call_builtin___clear_cache (begin_rtx
, end_rtx
);
8195 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
8198 round_trampoline_addr (rtx tramp
)
8200 rtx temp
, addend
, mask
;
8202 /* If we don't need too much alignment, we'll have been guaranteed
8203 proper alignment by get_trampoline_type. */
8204 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
8207 /* Round address up to desired boundary. */
8208 temp
= gen_reg_rtx (Pmode
);
8209 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
8210 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
8212 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
8213 temp
, 0, OPTAB_LIB_WIDEN
);
8214 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
8215 temp
, 0, OPTAB_LIB_WIDEN
);
8221 expand_builtin_init_trampoline (tree exp
, bool onstack
)
8223 tree t_tramp
, t_func
, t_chain
;
8224 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
8226 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
8227 POINTER_TYPE
, VOID_TYPE
))
8230 t_tramp
= CALL_EXPR_ARG (exp
, 0);
8231 t_func
= CALL_EXPR_ARG (exp
, 1);
8232 t_chain
= CALL_EXPR_ARG (exp
, 2);
8234 r_tramp
= expand_normal (t_tramp
);
8235 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
8236 MEM_NOTRAP_P (m_tramp
) = 1;
8238 /* If ONSTACK, the TRAMP argument should be the address of a field
8239 within the local function's FRAME decl. Either way, let's see if
8240 we can fill in the MEM_ATTRs for this memory. */
8241 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
8242 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
8244 /* Creator of a heap trampoline is responsible for making sure the
8245 address is aligned to at least STACK_BOUNDARY. Normally malloc
8246 will ensure this anyhow. */
8247 tmp
= round_trampoline_addr (r_tramp
);
8250 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
8251 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
8252 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
8255 /* The FUNC argument should be the address of the nested function.
8256 Extract the actual function decl to pass to the hook. */
8257 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
8258 t_func
= TREE_OPERAND (t_func
, 0);
8259 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
8261 r_chain
= expand_normal (t_chain
);
8263 /* Generate insns to initialize the trampoline. */
8264 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
8268 trampolines_created
= 1;
8270 if (targetm
.calls
.custom_function_descriptors
!= 0)
8271 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
8272 "trampoline generated for nested function %qD", t_func
);
8279 expand_builtin_adjust_trampoline (tree exp
)
8283 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
8286 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
8287 tramp
= round_trampoline_addr (tramp
);
8288 if (targetm
.calls
.trampoline_adjust_address
)
8289 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
8294 /* Expand a call to the builtin descriptor initialization routine.
8295 A descriptor is made up of a couple of pointers to the static
8296 chain and the code entry in this order. */
8299 expand_builtin_init_descriptor (tree exp
)
8301 tree t_descr
, t_func
, t_chain
;
8302 rtx m_descr
, r_descr
, r_func
, r_chain
;
8304 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, POINTER_TYPE
,
8308 t_descr
= CALL_EXPR_ARG (exp
, 0);
8309 t_func
= CALL_EXPR_ARG (exp
, 1);
8310 t_chain
= CALL_EXPR_ARG (exp
, 2);
8312 r_descr
= expand_normal (t_descr
);
8313 m_descr
= gen_rtx_MEM (BLKmode
, r_descr
);
8314 MEM_NOTRAP_P (m_descr
) = 1;
8315 set_mem_align (m_descr
, GET_MODE_ALIGNMENT (ptr_mode
));
8317 r_func
= expand_normal (t_func
);
8318 r_chain
= expand_normal (t_chain
);
8320 /* Generate insns to initialize the descriptor. */
8321 emit_move_insn (adjust_address_nv (m_descr
, ptr_mode
, 0), r_chain
);
8322 emit_move_insn (adjust_address_nv (m_descr
, ptr_mode
,
8323 POINTER_SIZE
/ BITS_PER_UNIT
), r_func
);
8328 /* Expand a call to the builtin descriptor adjustment routine. */
8331 expand_builtin_adjust_descriptor (tree exp
)
8335 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
8338 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
8340 /* Unalign the descriptor to allow runtime identification. */
8341 tramp
= plus_constant (ptr_mode
, tramp
,
8342 targetm
.calls
.custom_function_descriptors
);
8344 return force_operand (tramp
, NULL_RTX
);
8347 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
8348 function. The function first checks whether the back end provides
8349 an insn to implement signbit for the respective mode. If not, it
8350 checks whether the floating point format of the value is such that
8351 the sign bit can be extracted. If that is not the case, error out.
8352 EXP is the expression that is a call to the builtin function; if
8353 convenient, the result should be placed in TARGET. */
8355 expand_builtin_signbit (tree exp
, rtx target
)
8357 const struct real_format
*fmt
;
8358 scalar_float_mode fmode
;
8359 scalar_int_mode rmode
, imode
;
8362 enum insn_code icode
;
8364 location_t loc
= EXPR_LOCATION (exp
);
8366 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
8369 arg
= CALL_EXPR_ARG (exp
, 0);
8370 fmode
= SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg
));
8371 rmode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (exp
));
8372 fmt
= REAL_MODE_FORMAT (fmode
);
8374 arg
= builtin_save_expr (arg
);
8376 /* Expand the argument yielding a RTX expression. */
8377 temp
= expand_normal (arg
);
8379 /* Check if the back end provides an insn that handles signbit for the
8381 icode
= optab_handler (signbit_optab
, fmode
);
8382 if (icode
!= CODE_FOR_nothing
)
8384 rtx_insn
*last
= get_last_insn ();
8385 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
8386 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
8388 delete_insns_since (last
);
8391 /* For floating point formats without a sign bit, implement signbit
8393 bitpos
= fmt
->signbit_ro
;
8396 /* But we can't do this if the format supports signed zero. */
8397 gcc_assert (!fmt
->has_signed_zero
|| !HONOR_SIGNED_ZEROS (fmode
));
8399 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
8400 build_real (TREE_TYPE (arg
), dconst0
));
8401 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
8404 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
8406 imode
= int_mode_for_mode (fmode
).require ();
8407 temp
= gen_lowpart (imode
, temp
);
8412 /* Handle targets with different FP word orders. */
8413 if (FLOAT_WORDS_BIG_ENDIAN
)
8414 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
8416 word
= bitpos
/ BITS_PER_WORD
;
8417 temp
= operand_subword_force (temp
, word
, fmode
);
8418 bitpos
= bitpos
% BITS_PER_WORD
;
8421 /* Force the intermediate word_mode (or narrower) result into a
8422 register. This avoids attempting to create paradoxical SUBREGs
8423 of floating point modes below. */
8424 temp
= force_reg (imode
, temp
);
8426 /* If the bitpos is within the "result mode" lowpart, the operation
8427 can be implement with a single bitwise AND. Otherwise, we need
8428 a right shift and an AND. */
8430 if (bitpos
< GET_MODE_BITSIZE (rmode
))
8432 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
8434 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
8435 temp
= gen_lowpart (rmode
, temp
);
8436 temp
= expand_binop (rmode
, and_optab
, temp
,
8437 immed_wide_int_const (mask
, rmode
),
8438 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
8442 /* Perform a logical right shift to place the signbit in the least
8443 significant bit, then truncate the result to the desired mode
8444 and mask just this bit. */
8445 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
8446 temp
= gen_lowpart (rmode
, temp
);
8447 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
8448 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
8454 /* Expand fork or exec calls. TARGET is the desired target of the
8455 call. EXP is the call. FN is the
8456 identificator of the actual function. IGNORE is nonzero if the
8457 value is to be ignored. */
8460 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
8465 if (DECL_FUNCTION_CODE (fn
) != BUILT_IN_FORK
)
8467 tree path
= CALL_EXPR_ARG (exp
, 0);
8468 /* Detect unterminated path. */
8469 if (!check_read_access (exp
, path
))
8472 /* Also detect unterminated first argument. */
8473 switch (DECL_FUNCTION_CODE (fn
))
8475 case BUILT_IN_EXECL
:
8476 case BUILT_IN_EXECLE
:
8477 case BUILT_IN_EXECLP
:
8478 if (!check_read_access (exp
, path
))
8486 /* If we are not profiling, just call the function. */
8487 if (!profile_arc_flag
)
8490 /* Otherwise call the wrapper. This should be equivalent for the rest of
8491 compiler, so the code does not diverge, and the wrapper may run the
8492 code necessary for keeping the profiling sane. */
8494 switch (DECL_FUNCTION_CODE (fn
))
8497 id
= get_identifier ("__gcov_fork");
8500 case BUILT_IN_EXECL
:
8501 id
= get_identifier ("__gcov_execl");
8504 case BUILT_IN_EXECV
:
8505 id
= get_identifier ("__gcov_execv");
8508 case BUILT_IN_EXECLP
:
8509 id
= get_identifier ("__gcov_execlp");
8512 case BUILT_IN_EXECLE
:
8513 id
= get_identifier ("__gcov_execle");
8516 case BUILT_IN_EXECVP
:
8517 id
= get_identifier ("__gcov_execvp");
8520 case BUILT_IN_EXECVE
:
8521 id
= get_identifier ("__gcov_execve");
8528 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
8529 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
8530 DECL_EXTERNAL (decl
) = 1;
8531 TREE_PUBLIC (decl
) = 1;
8532 DECL_ARTIFICIAL (decl
) = 1;
8533 TREE_NOTHROW (decl
) = 1;
8534 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
8535 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
8536 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
8537 return expand_call (call
, target
, ignore
);
8542 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
8543 the pointer in these functions is void*, the tree optimizers may remove
8544 casts. The mode computed in expand_builtin isn't reliable either, due
8545 to __sync_bool_compare_and_swap.
8547 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
8548 group of builtins. This gives us log2 of the mode size. */
8550 static inline machine_mode
8551 get_builtin_sync_mode (int fcode_diff
)
8553 /* The size is not negotiable, so ask not to get BLKmode in return
8554 if the target indicates that a smaller size would be better. */
8555 return int_mode_for_size (BITS_PER_UNIT
<< fcode_diff
, 0).require ();
8558 /* Expand the memory expression LOC and return the appropriate memory operand
8559 for the builtin_sync operations. */
8562 get_builtin_sync_mem (tree loc
, machine_mode mode
)
8565 int addr_space
= TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc
))
8566 ? TREE_TYPE (TREE_TYPE (loc
))
8568 scalar_int_mode addr_mode
= targetm
.addr_space
.address_mode (addr_space
);
8570 addr
= expand_expr (loc
, NULL_RTX
, addr_mode
, EXPAND_SUM
);
8571 addr
= convert_memory_address (addr_mode
, addr
);
8573 /* Note that we explicitly do not want any alias information for this
8574 memory, so that we kill all other live memories. Otherwise we don't
8575 satisfy the full barrier semantics of the intrinsic. */
8576 mem
= gen_rtx_MEM (mode
, addr
);
8578 set_mem_addr_space (mem
, addr_space
);
8580 mem
= validize_mem (mem
);
8582 /* The alignment needs to be at least according to that of the mode. */
8583 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
8584 get_pointer_alignment (loc
)));
8585 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
8586 MEM_VOLATILE_P (mem
) = 1;
8591 /* Make sure an argument is in the right mode.
8592 EXP is the tree argument.
8593 MODE is the mode it should be in. */
8596 expand_expr_force_mode (tree exp
, machine_mode mode
)
8599 machine_mode old_mode
;
8601 if (TREE_CODE (exp
) == SSA_NAME
8602 && TYPE_MODE (TREE_TYPE (exp
)) != mode
)
8604 /* Undo argument promotion if possible, as combine might not
8605 be able to do it later due to MEM_VOLATILE_P uses in the
8607 gimple
*g
= get_gimple_for_ssa_name (exp
);
8608 if (g
&& gimple_assign_cast_p (g
))
8610 tree rhs
= gimple_assign_rhs1 (g
);
8611 tree_code code
= gimple_assign_rhs_code (g
);
8612 if (CONVERT_EXPR_CODE_P (code
)
8613 && TYPE_MODE (TREE_TYPE (rhs
)) == mode
8614 && INTEGRAL_TYPE_P (TREE_TYPE (exp
))
8615 && INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
8616 && (TYPE_PRECISION (TREE_TYPE (exp
))
8617 > TYPE_PRECISION (TREE_TYPE (rhs
))))
8622 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
8623 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
8624 of CONST_INTs, where we know the old_mode only from the call argument. */
8626 old_mode
= GET_MODE (val
);
8627 if (old_mode
== VOIDmode
)
8628 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
8629 val
= convert_modes (mode
, old_mode
, val
, 1);
8634 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
8635 EXP is the CALL_EXPR. CODE is the rtx code
8636 that corresponds to the arithmetic or logical operation from the name;
8637 an exception here is that NOT actually means NAND. TARGET is an optional
8638 place for us to store the results; AFTER is true if this is the
8639 fetch_and_xxx form. */
8642 expand_builtin_sync_operation (machine_mode mode
, tree exp
,
8643 enum rtx_code code
, bool after
,
8647 location_t loc
= EXPR_LOCATION (exp
);
8649 if (code
== NOT
&& warn_sync_nand
)
8651 tree fndecl
= get_callee_fndecl (exp
);
8652 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
8654 static bool warned_f_a_n
, warned_n_a_f
;
8658 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
8659 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
8660 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
8661 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
8662 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
8666 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
8667 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
8668 warned_f_a_n
= true;
8671 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
8672 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
8673 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
8674 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
8675 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
8679 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
8680 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
8681 warned_n_a_f
= true;
8689 /* Expand the operands. */
8690 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8691 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
8693 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SYNC_SEQ_CST
,
8697 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
8698 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
8699 true if this is the boolean form. TARGET is a place for us to store the
8700 results; this is NOT optional if IS_BOOL is true. */
8703 expand_builtin_compare_and_swap (machine_mode mode
, tree exp
,
8704 bool is_bool
, rtx target
)
8706 rtx old_val
, new_val
, mem
;
8709 /* Expand the operands. */
8710 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8711 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
8712 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
8714 pbool
= poval
= NULL
;
8715 if (target
!= const0_rtx
)
8722 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
8723 false, MEMMODEL_SYNC_SEQ_CST
,
8724 MEMMODEL_SYNC_SEQ_CST
))
8730 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
8731 general form is actually an atomic exchange, and some targets only
8732 support a reduced form with the second argument being a constant 1.
8733 EXP is the CALL_EXPR; TARGET is an optional place for us to store
8737 expand_builtin_sync_lock_test_and_set (machine_mode mode
, tree exp
,
8742 /* Expand the operands. */
8743 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8744 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
8746 return expand_sync_lock_test_and_set (target
, mem
, val
);
8749 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
8752 expand_builtin_sync_lock_release (machine_mode mode
, tree exp
)
8756 /* Expand the operands. */
8757 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8759 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_SYNC_RELEASE
, true);
8762 /* Given an integer representing an ``enum memmodel'', verify its
8763 correctness and return the memory model enum. */
8765 static enum memmodel
8766 get_memmodel (tree exp
)
8769 unsigned HOST_WIDE_INT val
;
8771 = expansion_point_location_if_in_system_header (input_location
);
8773 /* If the parameter is not a constant, it's a run time value so we'll just
8774 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
8775 if (TREE_CODE (exp
) != INTEGER_CST
)
8776 return MEMMODEL_SEQ_CST
;
8778 op
= expand_normal (exp
);
8781 if (targetm
.memmodel_check
)
8782 val
= targetm
.memmodel_check (val
);
8783 else if (val
& ~MEMMODEL_MASK
)
8785 warning_at (loc
, OPT_Winvalid_memory_model
,
8786 "unknown architecture specifier in memory model to builtin");
8787 return MEMMODEL_SEQ_CST
;
8790 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
8791 if (memmodel_base (val
) >= MEMMODEL_LAST
)
8793 warning_at (loc
, OPT_Winvalid_memory_model
,
8794 "invalid memory model argument to builtin");
8795 return MEMMODEL_SEQ_CST
;
8798 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
8799 be conservative and promote consume to acquire. */
8800 if (val
== MEMMODEL_CONSUME
)
8801 val
= MEMMODEL_ACQUIRE
;
8803 return (enum memmodel
) val
;
8806 /* Expand the __atomic_exchange intrinsic:
8807 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
8808 EXP is the CALL_EXPR.
8809 TARGET is an optional place for us to store the results. */
8812 expand_builtin_atomic_exchange (machine_mode mode
, tree exp
, rtx target
)
8815 enum memmodel model
;
8817 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
8819 if (!flag_inline_atomics
)
8822 /* Expand the operands. */
8823 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8824 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
8826 return expand_atomic_exchange (target
, mem
, val
, model
);
8829 /* Expand the __atomic_compare_exchange intrinsic:
8830 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
8831 TYPE desired, BOOL weak,
8832 enum memmodel success,
8833 enum memmodel failure)
8834 EXP is the CALL_EXPR.
8835 TARGET is an optional place for us to store the results. */
8838 expand_builtin_atomic_compare_exchange (machine_mode mode
, tree exp
,
8841 rtx expect
, desired
, mem
, oldval
;
8842 rtx_code_label
*label
;
8843 enum memmodel success
, failure
;
8847 = expansion_point_location_if_in_system_header (input_location
);
8849 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
8850 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
8852 if (failure
> success
)
8854 warning_at (loc
, OPT_Winvalid_memory_model
,
8855 "failure memory model cannot be stronger than success "
8856 "memory model for %<__atomic_compare_exchange%>");
8857 success
= MEMMODEL_SEQ_CST
;
8860 if (is_mm_release (failure
) || is_mm_acq_rel (failure
))
8862 warning_at (loc
, OPT_Winvalid_memory_model
,
8863 "invalid failure memory model for "
8864 "%<__atomic_compare_exchange%>");
8865 failure
= MEMMODEL_SEQ_CST
;
8866 success
= MEMMODEL_SEQ_CST
;
8870 if (!flag_inline_atomics
)
8873 /* Expand the operands. */
8874 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
8876 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
8877 expect
= convert_memory_address (Pmode
, expect
);
8878 expect
= gen_rtx_MEM (mode
, expect
);
8879 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
8881 weak
= CALL_EXPR_ARG (exp
, 3);
8883 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
8886 if (target
== const0_rtx
)
8889 /* Lest the rtl backend create a race condition with an imporoper store
8890 to memory, always create a new pseudo for OLDVAL. */
8893 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
8894 is_weak
, success
, failure
))
8897 /* Conditionally store back to EXPECT, lest we create a race condition
8898 with an improper store to memory. */
8899 /* ??? With a rearrangement of atomics at the gimple level, we can handle
8900 the normal case where EXPECT is totally private, i.e. a register. At
8901 which point the store can be unconditional. */
8902 label
= gen_label_rtx ();
8903 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
,
8904 GET_MODE (target
), 1, label
);
8905 emit_move_insn (expect
, oldval
);
8911 /* Helper function for expand_ifn_atomic_compare_exchange - expand
8912 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
8913 call. The weak parameter must be dropped to match the expected parameter
8914 list and the expected argument changed from value to pointer to memory
8918 expand_ifn_atomic_compare_exchange_into_call (gcall
*call
, machine_mode mode
)
8921 vec
<tree
, va_gc
> *vec
;
8924 vec
->quick_push (gimple_call_arg (call
, 0));
8925 tree expected
= gimple_call_arg (call
, 1);
8926 rtx x
= assign_stack_temp_for_type (mode
, GET_MODE_SIZE (mode
),
8927 TREE_TYPE (expected
));
8928 rtx expd
= expand_expr (expected
, x
, mode
, EXPAND_NORMAL
);
8930 emit_move_insn (x
, expd
);
8931 tree v
= make_tree (TREE_TYPE (expected
), x
);
8932 vec
->quick_push (build1 (ADDR_EXPR
,
8933 build_pointer_type (TREE_TYPE (expected
)), v
));
8934 vec
->quick_push (gimple_call_arg (call
, 2));
8935 /* Skip the boolean weak parameter. */
8936 for (z
= 4; z
< 6; z
++)
8937 vec
->quick_push (gimple_call_arg (call
, z
));
8938 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
8939 unsigned int bytes_log2
= exact_log2 (GET_MODE_SIZE (mode
).to_constant ());
8940 gcc_assert (bytes_log2
< 5);
8941 built_in_function fncode
8942 = (built_in_function
) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
8944 tree fndecl
= builtin_decl_explicit (fncode
);
8945 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fndecl
)),
8947 tree exp
= build_call_vec (boolean_type_node
, fn
, vec
);
8948 tree lhs
= gimple_call_lhs (call
);
8949 rtx boolret
= expand_call (exp
, NULL_RTX
, lhs
== NULL_TREE
);
8952 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
8953 if (GET_MODE (boolret
) != mode
)
8954 boolret
= convert_modes (mode
, GET_MODE (boolret
), boolret
, 1);
8955 x
= force_reg (mode
, x
);
8956 write_complex_part (target
, boolret
, true);
8957 write_complex_part (target
, x
, false);
8961 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
8964 expand_ifn_atomic_compare_exchange (gcall
*call
)
8966 int size
= tree_to_shwi (gimple_call_arg (call
, 3)) & 255;
8967 gcc_assert (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16);
8968 machine_mode mode
= int_mode_for_size (BITS_PER_UNIT
* size
, 0).require ();
8969 rtx expect
, desired
, mem
, oldval
, boolret
;
8970 enum memmodel success
, failure
;
8974 = expansion_point_location_if_in_system_header (gimple_location (call
));
8976 success
= get_memmodel (gimple_call_arg (call
, 4));
8977 failure
= get_memmodel (gimple_call_arg (call
, 5));
8979 if (failure
> success
)
8981 warning_at (loc
, OPT_Winvalid_memory_model
,
8982 "failure memory model cannot be stronger than success "
8983 "memory model for %<__atomic_compare_exchange%>");
8984 success
= MEMMODEL_SEQ_CST
;
8987 if (is_mm_release (failure
) || is_mm_acq_rel (failure
))
8989 warning_at (loc
, OPT_Winvalid_memory_model
,
8990 "invalid failure memory model for "
8991 "%<__atomic_compare_exchange%>");
8992 failure
= MEMMODEL_SEQ_CST
;
8993 success
= MEMMODEL_SEQ_CST
;
8996 if (!flag_inline_atomics
)
8998 expand_ifn_atomic_compare_exchange_into_call (call
, mode
);
9002 /* Expand the operands. */
9003 mem
= get_builtin_sync_mem (gimple_call_arg (call
, 0), mode
);
9005 expect
= expand_expr_force_mode (gimple_call_arg (call
, 1), mode
);
9006 desired
= expand_expr_force_mode (gimple_call_arg (call
, 2), mode
);
9008 is_weak
= (tree_to_shwi (gimple_call_arg (call
, 3)) & 256) != 0;
9013 if (!expand_atomic_compare_and_swap (&boolret
, &oldval
, mem
, expect
, desired
,
9014 is_weak
, success
, failure
))
9016 expand_ifn_atomic_compare_exchange_into_call (call
, mode
);
9020 lhs
= gimple_call_lhs (call
);
9023 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
9024 if (GET_MODE (boolret
) != mode
)
9025 boolret
= convert_modes (mode
, GET_MODE (boolret
), boolret
, 1);
9026 write_complex_part (target
, boolret
, true);
9027 write_complex_part (target
, oldval
, false);
9031 /* Expand the __atomic_load intrinsic:
9032 TYPE __atomic_load (TYPE *object, enum memmodel)
9033 EXP is the CALL_EXPR.
9034 TARGET is an optional place for us to store the results. */
9037 expand_builtin_atomic_load (machine_mode mode
, tree exp
, rtx target
)
9040 enum memmodel model
;
9042 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
9043 if (is_mm_release (model
) || is_mm_acq_rel (model
))
9046 = expansion_point_location_if_in_system_header (input_location
);
9047 warning_at (loc
, OPT_Winvalid_memory_model
,
9048 "invalid memory model for %<__atomic_load%>");
9049 model
= MEMMODEL_SEQ_CST
;
9052 if (!flag_inline_atomics
)
9055 /* Expand the operand. */
9056 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
9058 return expand_atomic_load (target
, mem
, model
);
9062 /* Expand the __atomic_store intrinsic:
9063 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
9064 EXP is the CALL_EXPR.
9065 TARGET is an optional place for us to store the results. */
9068 expand_builtin_atomic_store (machine_mode mode
, tree exp
)
9071 enum memmodel model
;
9073 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
9074 if (!(is_mm_relaxed (model
) || is_mm_seq_cst (model
)
9075 || is_mm_release (model
)))
9078 = expansion_point_location_if_in_system_header (input_location
);
9079 warning_at (loc
, OPT_Winvalid_memory_model
,
9080 "invalid memory model for %<__atomic_store%>");
9081 model
= MEMMODEL_SEQ_CST
;
9084 if (!flag_inline_atomics
)
9087 /* Expand the operands. */
9088 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
9089 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
9091 return expand_atomic_store (mem
, val
, model
, false);
9094 /* Expand the __atomic_fetch_XXX intrinsic:
9095 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
9096 EXP is the CALL_EXPR.
9097 TARGET is an optional place for us to store the results.
9098 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
9099 FETCH_AFTER is true if returning the result of the operation.
9100 FETCH_AFTER is false if returning the value before the operation.
9101 IGNORE is true if the result is not used.
9102 EXT_CALL is the correct builtin for an external call if this cannot be
9103 resolved to an instruction sequence. */
9106 expand_builtin_atomic_fetch_op (machine_mode mode
, tree exp
, rtx target
,
9107 enum rtx_code code
, bool fetch_after
,
9108 bool ignore
, enum built_in_function ext_call
)
9111 enum memmodel model
;
9115 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
9117 /* Expand the operands. */
9118 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
9119 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
9121 /* Only try generating instructions if inlining is turned on. */
9122 if (flag_inline_atomics
)
9124 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
9129 /* Return if a different routine isn't needed for the library call. */
9130 if (ext_call
== BUILT_IN_NONE
)
9133 /* Change the call to the specified function. */
9134 fndecl
= get_callee_fndecl (exp
);
9135 addr
= CALL_EXPR_FN (exp
);
9138 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
9139 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
9141 /* If we will emit code after the call, the call cannot be a tail call.
9142 If it is emitted as a tail call, a barrier is emitted after it, and
9143 then all trailing code is removed. */
9145 CALL_EXPR_TAILCALL (exp
) = 0;
9147 /* Expand the call here so we can emit trailing code. */
9148 ret
= expand_call (exp
, target
, ignore
);
9150 /* Replace the original function just in case it matters. */
9151 TREE_OPERAND (addr
, 0) = fndecl
;
9153 /* Then issue the arithmetic correction to return the right result. */
9158 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
9160 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
9163 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
9169 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
9172 expand_ifn_atomic_bit_test_and (gcall
*call
)
9174 tree ptr
= gimple_call_arg (call
, 0);
9175 tree bit
= gimple_call_arg (call
, 1);
9176 tree flag
= gimple_call_arg (call
, 2);
9177 tree lhs
= gimple_call_lhs (call
);
9178 enum memmodel model
= MEMMODEL_SYNC_SEQ_CST
;
9179 machine_mode mode
= TYPE_MODE (TREE_TYPE (flag
));
9182 class expand_operand ops
[5];
9184 gcc_assert (flag_inline_atomics
);
9186 if (gimple_call_num_args (call
) == 4)
9187 model
= get_memmodel (gimple_call_arg (call
, 3));
9189 rtx mem
= get_builtin_sync_mem (ptr
, mode
);
9190 rtx val
= expand_expr_force_mode (bit
, mode
);
9192 switch (gimple_call_internal_fn (call
))
9194 case IFN_ATOMIC_BIT_TEST_AND_SET
:
9196 optab
= atomic_bit_test_and_set_optab
;
9198 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
:
9200 optab
= atomic_bit_test_and_complement_optab
;
9202 case IFN_ATOMIC_BIT_TEST_AND_RESET
:
9204 optab
= atomic_bit_test_and_reset_optab
;
9210 if (lhs
== NULL_TREE
)
9212 val
= expand_simple_binop (mode
, ASHIFT
, const1_rtx
,
9213 val
, NULL_RTX
, true, OPTAB_DIRECT
);
9215 val
= expand_simple_unop (mode
, NOT
, val
, NULL_RTX
, true);
9216 expand_atomic_fetch_op (const0_rtx
, mem
, val
, code
, model
, false);
9220 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
9221 enum insn_code icode
= direct_optab_handler (optab
, mode
);
9222 gcc_assert (icode
!= CODE_FOR_nothing
);
9223 create_output_operand (&ops
[0], target
, mode
);
9224 create_fixed_operand (&ops
[1], mem
);
9225 create_convert_operand_to (&ops
[2], val
, mode
, true);
9226 create_integer_operand (&ops
[3], model
);
9227 create_integer_operand (&ops
[4], integer_onep (flag
));
9228 if (maybe_expand_insn (icode
, 5, ops
))
9232 val
= expand_simple_binop (mode
, ASHIFT
, const1_rtx
,
9233 val
, NULL_RTX
, true, OPTAB_DIRECT
);
9236 val
= expand_simple_unop (mode
, NOT
, val
, NULL_RTX
, true);
9237 rtx result
= expand_atomic_fetch_op (gen_reg_rtx (mode
), mem
, val
,
9238 code
, model
, false);
9239 if (integer_onep (flag
))
9241 result
= expand_simple_binop (mode
, ASHIFTRT
, result
, bitval
,
9242 NULL_RTX
, true, OPTAB_DIRECT
);
9243 result
= expand_simple_binop (mode
, AND
, result
, const1_rtx
, target
,
9244 true, OPTAB_DIRECT
);
9247 result
= expand_simple_binop (mode
, AND
, result
, maskval
, target
, true,
9249 if (result
!= target
)
9250 emit_move_insn (target
, result
);
9253 /* Expand an atomic clear operation.
9254 void _atomic_clear (BOOL *obj, enum memmodel)
9255 EXP is the call expression. */
9258 expand_builtin_atomic_clear (tree exp
)
9262 enum memmodel model
;
9264 mode
= int_mode_for_size (BOOL_TYPE_SIZE
, 0).require ();
9265 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
9266 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
9268 if (is_mm_consume (model
) || is_mm_acquire (model
) || is_mm_acq_rel (model
))
9271 = expansion_point_location_if_in_system_header (input_location
);
9272 warning_at (loc
, OPT_Winvalid_memory_model
,
9273 "invalid memory model for %<__atomic_store%>");
9274 model
= MEMMODEL_SEQ_CST
;
9277 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
9278 Failing that, a store is issued by __atomic_store. The only way this can
9279 fail is if the bool type is larger than a word size. Unlikely, but
9280 handle it anyway for completeness. Assume a single threaded model since
9281 there is no atomic support in this case, and no barriers are required. */
9282 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
9284 emit_move_insn (mem
, const0_rtx
);
9288 /* Expand an atomic test_and_set operation.
9289 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
9290 EXP is the call expression. */
9293 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
9296 enum memmodel model
;
9299 mode
= int_mode_for_size (BOOL_TYPE_SIZE
, 0).require ();
9300 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
9301 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
9303 return expand_atomic_test_and_set (target
, mem
, model
);
9307 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
9308 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
9311 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
9315 unsigned int mode_align
, type_align
;
9317 if (TREE_CODE (arg0
) != INTEGER_CST
)
9320 /* We need a corresponding integer mode for the access to be lock-free. */
9321 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
9322 if (!int_mode_for_size (size
, 0).exists (&mode
))
9323 return boolean_false_node
;
9325 mode_align
= GET_MODE_ALIGNMENT (mode
);
9327 if (TREE_CODE (arg1
) == INTEGER_CST
)
9329 unsigned HOST_WIDE_INT val
= UINTVAL (expand_normal (arg1
));
9331 /* Either this argument is null, or it's a fake pointer encoding
9332 the alignment of the object. */
9333 val
= least_bit_hwi (val
);
9334 val
*= BITS_PER_UNIT
;
9336 if (val
== 0 || mode_align
< val
)
9337 type_align
= mode_align
;
9343 tree ttype
= TREE_TYPE (arg1
);
9345 /* This function is usually invoked and folded immediately by the front
9346 end before anything else has a chance to look at it. The pointer
9347 parameter at this point is usually cast to a void *, so check for that
9348 and look past the cast. */
9349 if (CONVERT_EXPR_P (arg1
)
9350 && POINTER_TYPE_P (ttype
)
9351 && VOID_TYPE_P (TREE_TYPE (ttype
))
9352 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
9353 arg1
= TREE_OPERAND (arg1
, 0);
9355 ttype
= TREE_TYPE (arg1
);
9356 gcc_assert (POINTER_TYPE_P (ttype
));
9358 /* Get the underlying type of the object. */
9359 ttype
= TREE_TYPE (ttype
);
9360 type_align
= TYPE_ALIGN (ttype
);
9363 /* If the object has smaller alignment, the lock free routines cannot
9365 if (type_align
< mode_align
)
9366 return boolean_false_node
;
9368 /* Check if a compare_and_swap pattern exists for the mode which represents
9369 the required size. The pattern is not allowed to fail, so the existence
9370 of the pattern indicates support is present. Also require that an
9371 atomic load exists for the required size. */
9372 if (can_compare_and_swap_p (mode
, true) && can_atomic_load_p (mode
))
9373 return boolean_true_node
;
9375 return boolean_false_node
;
9378 /* Return true if the parameters to call EXP represent an object which will
9379 always generate lock free instructions. The first argument represents the
9380 size of the object, and the second parameter is a pointer to the object
9381 itself. If NULL is passed for the object, then the result is based on
9382 typical alignment for an object of the specified size. Otherwise return
9386 expand_builtin_atomic_always_lock_free (tree exp
)
9389 tree arg0
= CALL_EXPR_ARG (exp
, 0);
9390 tree arg1
= CALL_EXPR_ARG (exp
, 1);
9392 if (TREE_CODE (arg0
) != INTEGER_CST
)
9394 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
9398 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
9399 if (size
== boolean_true_node
)
9404 /* Return a one or zero if it can be determined that object ARG1 of size ARG
9405 is lock free on this architecture. */
9408 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
9410 if (!flag_inline_atomics
)
9413 /* If it isn't always lock free, don't generate a result. */
9414 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
9415 return boolean_true_node
;
9420 /* Return true if the parameters to call EXP represent an object which will
9421 always generate lock free instructions. The first argument represents the
9422 size of the object, and the second parameter is a pointer to the object
9423 itself. If NULL is passed for the object, then the result is based on
9424 typical alignment for an object of the specified size. Otherwise return
9428 expand_builtin_atomic_is_lock_free (tree exp
)
9431 tree arg0
= CALL_EXPR_ARG (exp
, 0);
9432 tree arg1
= CALL_EXPR_ARG (exp
, 1);
9434 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9436 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
9440 if (!flag_inline_atomics
)
9443 /* If the value is known at compile time, return the RTX for it. */
9444 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
9445 if (size
== boolean_true_node
)
9451 /* Expand the __atomic_thread_fence intrinsic:
9452 void __atomic_thread_fence (enum memmodel)
9453 EXP is the CALL_EXPR. */
9456 expand_builtin_atomic_thread_fence (tree exp
)
9458 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
9459 expand_mem_thread_fence (model
);
9462 /* Expand the __atomic_signal_fence intrinsic:
9463 void __atomic_signal_fence (enum memmodel)
9464 EXP is the CALL_EXPR. */
9467 expand_builtin_atomic_signal_fence (tree exp
)
9469 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
9470 expand_mem_signal_fence (model
);
9473 /* Expand the __sync_synchronize intrinsic. */
9476 expand_builtin_sync_synchronize (void)
9478 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST
);
9482 expand_builtin_thread_pointer (tree exp
, rtx target
)
9484 enum insn_code icode
;
9485 if (!validate_arglist (exp
, VOID_TYPE
))
9487 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
9488 if (icode
!= CODE_FOR_nothing
)
9490 class expand_operand op
;
9491 /* If the target is not sutitable then create a new target. */
9492 if (target
== NULL_RTX
9494 || GET_MODE (target
) != Pmode
)
9495 target
= gen_reg_rtx (Pmode
);
9496 create_output_operand (&op
, target
, Pmode
);
9497 expand_insn (icode
, 1, &op
);
9500 error ("%<__builtin_thread_pointer%> is not supported on this target");
9505 expand_builtin_set_thread_pointer (tree exp
)
9507 enum insn_code icode
;
9508 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
9510 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
9511 if (icode
!= CODE_FOR_nothing
)
9513 class expand_operand op
;
9514 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
9515 Pmode
, EXPAND_NORMAL
);
9516 create_input_operand (&op
, val
, Pmode
);
9517 expand_insn (icode
, 1, &op
);
9520 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
9524 /* Emit code to restore the current value of stack. */
9527 expand_stack_restore (tree var
)
9530 rtx sa
= expand_normal (var
);
9532 sa
= convert_memory_address (Pmode
, sa
);
9534 prev
= get_last_insn ();
9535 emit_stack_restore (SAVE_BLOCK
, sa
);
9537 record_new_stack_level ();
9539 fixup_args_size_notes (prev
, get_last_insn (), 0);
9542 /* Emit code to save the current value of stack. */
9545 expand_stack_save (void)
9549 emit_stack_save (SAVE_BLOCK
, &ret
);
9553 /* Emit code to get the openacc gang, worker or vector id or size. */
9556 expand_builtin_goacc_parlevel_id_size (tree exp
, rtx target
, int ignore
)
9559 rtx fallback_retval
;
9560 rtx_insn
*(*gen_fn
) (rtx
, rtx
);
9561 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp
)))
9563 case BUILT_IN_GOACC_PARLEVEL_ID
:
9564 name
= "__builtin_goacc_parlevel_id";
9565 fallback_retval
= const0_rtx
;
9566 gen_fn
= targetm
.gen_oacc_dim_pos
;
9568 case BUILT_IN_GOACC_PARLEVEL_SIZE
:
9569 name
= "__builtin_goacc_parlevel_size";
9570 fallback_retval
= const1_rtx
;
9571 gen_fn
= targetm
.gen_oacc_dim_size
;
9577 if (oacc_get_fn_attrib (current_function_decl
) == NULL_TREE
)
9579 error ("%qs only supported in OpenACC code", name
);
9583 tree arg
= CALL_EXPR_ARG (exp
, 0);
9584 if (TREE_CODE (arg
) != INTEGER_CST
)
9586 error ("non-constant argument 0 to %qs", name
);
9590 int dim
= TREE_INT_CST_LOW (arg
);
9594 case GOMP_DIM_WORKER
:
9595 case GOMP_DIM_VECTOR
:
9598 error ("illegal argument 0 to %qs", name
);
9605 if (target
== NULL_RTX
)
9606 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
9608 if (!targetm
.have_oacc_dim_size ())
9610 emit_move_insn (target
, fallback_retval
);
9614 rtx reg
= MEM_P (target
) ? gen_reg_rtx (GET_MODE (target
)) : target
;
9615 emit_insn (gen_fn (reg
, GEN_INT (dim
)));
9617 emit_move_insn (target
, reg
);
9622 /* Expand a string compare operation using a sequence of char comparison
9623 to get rid of the calling overhead, with result going to TARGET if
9626 VAR_STR is the variable string source;
9627 CONST_STR is the constant string source;
9628 LENGTH is the number of chars to compare;
9629 CONST_STR_N indicates which source string is the constant string;
9630 IS_MEMCMP indicates whether it's a memcmp or strcmp.
9632 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
9634 target = (int) (unsigned char) var_str[0]
9635 - (int) (unsigned char) const_str[0];
9639 target = (int) (unsigned char) var_str[length - 2]
9640 - (int) (unsigned char) const_str[length - 2];
9643 target = (int) (unsigned char) var_str[length - 1]
9644 - (int) (unsigned char) const_str[length - 1];
9649 inline_string_cmp (rtx target
, tree var_str
, const char *const_str
,
9650 unsigned HOST_WIDE_INT length
,
9651 int const_str_n
, machine_mode mode
)
9653 HOST_WIDE_INT offset
= 0;
9655 = get_memory_rtx (var_str
, build_int_cst (unsigned_type_node
,length
));
9656 rtx var_rtx
= NULL_RTX
;
9657 rtx const_rtx
= NULL_RTX
;
9658 rtx result
= target
? target
: gen_reg_rtx (mode
);
9659 rtx_code_label
*ne_label
= gen_label_rtx ();
9660 tree unit_type_node
= unsigned_char_type_node
;
9661 scalar_int_mode unit_mode
9662 = as_a
<scalar_int_mode
> TYPE_MODE (unit_type_node
);
9666 for (unsigned HOST_WIDE_INT i
= 0; i
< length
; i
++)
9669 = adjust_address (var_rtx_array
, TYPE_MODE (unit_type_node
), offset
);
9670 const_rtx
= c_readstr (const_str
+ offset
, unit_mode
);
9671 rtx op0
= (const_str_n
== 1) ? const_rtx
: var_rtx
;
9672 rtx op1
= (const_str_n
== 1) ? var_rtx
: const_rtx
;
9674 op0
= convert_modes (mode
, unit_mode
, op0
, 1);
9675 op1
= convert_modes (mode
, unit_mode
, op1
, 1);
9676 result
= expand_simple_binop (mode
, MINUS
, op0
, op1
,
9677 result
, 1, OPTAB_WIDEN
);
9679 emit_cmp_and_jump_insns (result
, CONST0_RTX (mode
), NE
, NULL_RTX
,
9680 mode
, true, ne_label
);
9681 offset
+= GET_MODE_SIZE (unit_mode
);
9684 emit_label (ne_label
);
9685 rtx_insn
*insns
= get_insns ();
9692 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
9693 to TARGET if that's convenient.
9694 If the call is not been inlined, return NULL_RTX. */
9697 inline_expand_builtin_bytecmp (tree exp
, rtx target
)
9699 tree fndecl
= get_callee_fndecl (exp
);
9700 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9701 bool is_ncmp
= (fcode
== BUILT_IN_STRNCMP
|| fcode
== BUILT_IN_MEMCMP
);
9703 /* Do NOT apply this inlining expansion when optimizing for size or
9704 optimization level below 2. */
9705 if (optimize
< 2 || optimize_insn_for_size_p ())
9708 gcc_checking_assert (fcode
== BUILT_IN_STRCMP
9709 || fcode
== BUILT_IN_STRNCMP
9710 || fcode
== BUILT_IN_MEMCMP
);
9712 /* On a target where the type of the call (int) has same or narrower presicion
9713 than unsigned char, give up the inlining expansion. */
9714 if (TYPE_PRECISION (unsigned_char_type_node
)
9715 >= TYPE_PRECISION (TREE_TYPE (exp
)))
9718 tree arg1
= CALL_EXPR_ARG (exp
, 0);
9719 tree arg2
= CALL_EXPR_ARG (exp
, 1);
9720 tree len3_tree
= is_ncmp
? CALL_EXPR_ARG (exp
, 2) : NULL_TREE
;
9722 unsigned HOST_WIDE_INT len1
= 0;
9723 unsigned HOST_WIDE_INT len2
= 0;
9724 unsigned HOST_WIDE_INT len3
= 0;
9726 /* Get the object representation of the initializers of ARG1 and ARG2
9727 as strings, provided they refer to constant objects, with their byte
9728 sizes in LEN1 and LEN2, respectively. */
9729 const char *bytes1
= getbyterep (arg1
, &len1
);
9730 const char *bytes2
= getbyterep (arg2
, &len2
);
9732 /* Fail if neither argument refers to an initialized constant. */
9733 if (!bytes1
&& !bytes2
)
9738 /* Fail if the memcmp/strncmp bound is not a constant. */
9739 if (!tree_fits_uhwi_p (len3_tree
))
9742 len3
= tree_to_uhwi (len3_tree
);
9744 if (fcode
== BUILT_IN_MEMCMP
)
9746 /* Fail if the memcmp bound is greater than the size of either
9747 of the two constant objects. */
9748 if ((bytes1
&& len1
< len3
)
9749 || (bytes2
&& len2
< len3
))
9754 if (fcode
!= BUILT_IN_MEMCMP
)
9756 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
9757 and LEN2 to the length of the nul-terminated string stored
9760 len1
= strnlen (bytes1
, len1
) + 1;
9762 len2
= strnlen (bytes2
, len2
) + 1;
9765 /* See inline_string_cmp. */
9771 else if (len2
> len1
)
9776 /* For strncmp only, compute the new bound as the smallest of
9777 the lengths of the two strings (plus 1) and the bound provided
9779 unsigned HOST_WIDE_INT bound
= (const_str_n
== 1) ? len1
: len2
;
9780 if (is_ncmp
&& len3
< bound
)
9783 /* If the bound of the comparison is larger than the threshold,
9785 if (bound
> (unsigned HOST_WIDE_INT
) param_builtin_string_cmp_inline_length
)
9788 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
9790 /* Now, start inline expansion the call. */
9791 return inline_string_cmp (target
, (const_str_n
== 1) ? arg2
: arg1
,
9792 (const_str_n
== 1) ? bytes1
: bytes2
, bound
,
9796 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
9797 represents the size of the first argument to that call, or VOIDmode
9798 if the argument is a pointer. IGNORE will be true if the result
9801 expand_speculation_safe_value (machine_mode mode
, tree exp
, rtx target
,
9805 unsigned nargs
= call_expr_nargs (exp
);
9807 tree arg0
= CALL_EXPR_ARG (exp
, 0);
9809 if (mode
== VOIDmode
)
9811 mode
= TYPE_MODE (TREE_TYPE (arg0
));
9812 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
9815 val
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
9817 /* An optional second argument can be used as a failsafe value on
9818 some machines. If it isn't present, then the failsafe value is
9822 tree arg1
= CALL_EXPR_ARG (exp
, 1);
9823 failsafe
= expand_expr (arg1
, NULL_RTX
, mode
, EXPAND_NORMAL
);
9826 failsafe
= const0_rtx
;
9828 /* If the result isn't used, the behavior is undefined. It would be
9829 nice to emit a warning here, but path splitting means this might
9830 happen with legitimate code. So simply drop the builtin
9831 expansion in that case; we've handled any side-effects above. */
9835 /* If we don't have a suitable target, create one to hold the result. */
9836 if (target
== NULL
|| GET_MODE (target
) != mode
)
9837 target
= gen_reg_rtx (mode
);
9839 if (GET_MODE (val
) != mode
&& GET_MODE (val
) != VOIDmode
)
9840 val
= convert_modes (mode
, VOIDmode
, val
, false);
9842 return targetm
.speculation_safe_value (mode
, target
, val
, failsafe
);
9845 /* Expand an expression EXP that calls a built-in function,
9846 with result going to TARGET if that's convenient
9847 (and in mode MODE if that's convenient).
9848 SUBTARGET may be used as the target for computing one of EXP's operands.
9849 IGNORE is nonzero if the value is to be ignored. */
9852 expand_builtin (tree exp
, rtx target
, rtx subtarget
, machine_mode mode
,
9855 tree fndecl
= get_callee_fndecl (exp
);
9856 machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
9859 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
9860 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
9862 /* When ASan is enabled, we don't want to expand some memory/string
9863 builtins and rely on libsanitizer's hooks. This allows us to avoid
9864 redundant checks and be sure, that possible overflow will be detected
9867 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9868 if ((flag_sanitize
& SANITIZE_ADDRESS
) && asan_intercepted_p (fcode
))
9869 return expand_call (exp
, target
, ignore
);
9871 /* When not optimizing, generate calls to library functions for a certain
9874 && !called_as_built_in (fndecl
)
9875 && fcode
!= BUILT_IN_FORK
9876 && fcode
!= BUILT_IN_EXECL
9877 && fcode
!= BUILT_IN_EXECV
9878 && fcode
!= BUILT_IN_EXECLP
9879 && fcode
!= BUILT_IN_EXECLE
9880 && fcode
!= BUILT_IN_EXECVP
9881 && fcode
!= BUILT_IN_EXECVE
9882 && fcode
!= BUILT_IN_CLEAR_CACHE
9883 && !ALLOCA_FUNCTION_CODE_P (fcode
)
9884 && fcode
!= BUILT_IN_FREE
)
9885 return expand_call (exp
, target
, ignore
);
9887 /* The built-in function expanders test for target == const0_rtx
9888 to determine whether the function's result will be ignored. */
9890 target
= const0_rtx
;
9892 /* If the result of a pure or const built-in function is ignored, and
9893 none of its arguments are volatile, we can avoid expanding the
9894 built-in call and just evaluate the arguments for side-effects. */
9895 if (target
== const0_rtx
9896 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
9897 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
9899 bool volatilep
= false;
9901 call_expr_arg_iterator iter
;
9903 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
9904 if (TREE_THIS_VOLATILE (arg
))
9912 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
9913 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
9920 CASE_FLT_FN (BUILT_IN_FABS
):
9921 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
9922 case BUILT_IN_FABSD32
:
9923 case BUILT_IN_FABSD64
:
9924 case BUILT_IN_FABSD128
:
9925 target
= expand_builtin_fabs (exp
, target
, subtarget
);
9930 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
9931 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
9932 target
= expand_builtin_copysign (exp
, target
, subtarget
);
9937 /* Just do a normal library call if we were unable to fold
9939 CASE_FLT_FN (BUILT_IN_CABS
):
9942 CASE_FLT_FN (BUILT_IN_FMA
):
9943 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
9944 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
9949 CASE_FLT_FN (BUILT_IN_ILOGB
):
9950 if (! flag_unsafe_math_optimizations
)
9953 CASE_FLT_FN (BUILT_IN_ISINF
):
9954 CASE_FLT_FN (BUILT_IN_FINITE
):
9955 case BUILT_IN_ISFINITE
:
9956 case BUILT_IN_ISNORMAL
:
9957 target
= expand_builtin_interclass_mathfn (exp
, target
);
9962 CASE_FLT_FN (BUILT_IN_ICEIL
):
9963 CASE_FLT_FN (BUILT_IN_LCEIL
):
9964 CASE_FLT_FN (BUILT_IN_LLCEIL
):
9965 CASE_FLT_FN (BUILT_IN_LFLOOR
):
9966 CASE_FLT_FN (BUILT_IN_IFLOOR
):
9967 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
9968 target
= expand_builtin_int_roundingfn (exp
, target
);
9973 CASE_FLT_FN (BUILT_IN_IRINT
):
9974 CASE_FLT_FN (BUILT_IN_LRINT
):
9975 CASE_FLT_FN (BUILT_IN_LLRINT
):
9976 CASE_FLT_FN (BUILT_IN_IROUND
):
9977 CASE_FLT_FN (BUILT_IN_LROUND
):
9978 CASE_FLT_FN (BUILT_IN_LLROUND
):
9979 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
9984 CASE_FLT_FN (BUILT_IN_POWI
):
9985 target
= expand_builtin_powi (exp
, target
);
9990 CASE_FLT_FN (BUILT_IN_CEXPI
):
9991 target
= expand_builtin_cexpi (exp
, target
);
9992 gcc_assert (target
);
9995 CASE_FLT_FN (BUILT_IN_SIN
):
9996 CASE_FLT_FN (BUILT_IN_COS
):
9997 if (! flag_unsafe_math_optimizations
)
9999 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
10004 CASE_FLT_FN (BUILT_IN_SINCOS
):
10005 if (! flag_unsafe_math_optimizations
)
10007 target
= expand_builtin_sincos (exp
);
10012 case BUILT_IN_APPLY_ARGS
:
10013 return expand_builtin_apply_args ();
10015 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
10016 FUNCTION with a copy of the parameters described by
10017 ARGUMENTS, and ARGSIZE. It returns a block of memory
10018 allocated on the stack into which is stored all the registers
10019 that might possibly be used for returning the result of a
10020 function. ARGUMENTS is the value returned by
10021 __builtin_apply_args. ARGSIZE is the number of bytes of
10022 arguments that must be copied. ??? How should this value be
10023 computed? We'll also need a safe worst case value for varargs
10025 case BUILT_IN_APPLY
:
10026 if (!validate_arglist (exp
, POINTER_TYPE
,
10027 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
10028 && !validate_arglist (exp
, REFERENCE_TYPE
,
10029 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10035 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
10036 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
10037 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
10039 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
10042 /* __builtin_return (RESULT) causes the function to return the
10043 value described by RESULT. RESULT is address of the block of
10044 memory returned by __builtin_apply. */
10045 case BUILT_IN_RETURN
:
10046 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
10047 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
10050 case BUILT_IN_SAVEREGS
:
10051 return expand_builtin_saveregs ();
10053 case BUILT_IN_VA_ARG_PACK
:
10054 /* All valid uses of __builtin_va_arg_pack () are removed during
10056 error ("invalid use of %<__builtin_va_arg_pack ()%>");
10059 case BUILT_IN_VA_ARG_PACK_LEN
:
10060 /* All valid uses of __builtin_va_arg_pack_len () are removed during
10062 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
10065 /* Return the address of the first anonymous stack arg. */
10066 case BUILT_IN_NEXT_ARG
:
10067 if (fold_builtin_next_arg (exp
, false))
10069 return expand_builtin_next_arg ();
10071 case BUILT_IN_CLEAR_CACHE
:
10072 expand_builtin___clear_cache (exp
);
10075 case BUILT_IN_CLASSIFY_TYPE
:
10076 return expand_builtin_classify_type (exp
);
10078 case BUILT_IN_CONSTANT_P
:
10081 case BUILT_IN_FRAME_ADDRESS
:
10082 case BUILT_IN_RETURN_ADDRESS
:
10083 return expand_builtin_frame_address (fndecl
, exp
);
10085 /* Returns the address of the area where the structure is returned.
10087 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
10088 if (call_expr_nargs (exp
) != 0
10089 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
10090 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
10093 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
10095 CASE_BUILT_IN_ALLOCA
:
10096 target
= expand_builtin_alloca (exp
);
10101 case BUILT_IN_ASAN_ALLOCAS_UNPOISON
:
10102 return expand_asan_emit_allocas_unpoison (exp
);
10104 case BUILT_IN_STACK_SAVE
:
10105 return expand_stack_save ();
10107 case BUILT_IN_STACK_RESTORE
:
10108 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
10111 case BUILT_IN_BSWAP16
:
10112 case BUILT_IN_BSWAP32
:
10113 case BUILT_IN_BSWAP64
:
10114 case BUILT_IN_BSWAP128
:
10115 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
10120 CASE_INT_FN (BUILT_IN_FFS
):
10121 target
= expand_builtin_unop (target_mode
, exp
, target
,
10122 subtarget
, ffs_optab
);
10127 CASE_INT_FN (BUILT_IN_CLZ
):
10128 target
= expand_builtin_unop (target_mode
, exp
, target
,
10129 subtarget
, clz_optab
);
10134 CASE_INT_FN (BUILT_IN_CTZ
):
10135 target
= expand_builtin_unop (target_mode
, exp
, target
,
10136 subtarget
, ctz_optab
);
10141 CASE_INT_FN (BUILT_IN_CLRSB
):
10142 target
= expand_builtin_unop (target_mode
, exp
, target
,
10143 subtarget
, clrsb_optab
);
10148 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10149 target
= expand_builtin_unop (target_mode
, exp
, target
,
10150 subtarget
, popcount_optab
);
10155 CASE_INT_FN (BUILT_IN_PARITY
):
10156 target
= expand_builtin_unop (target_mode
, exp
, target
,
10157 subtarget
, parity_optab
);
10162 case BUILT_IN_STRLEN
:
10163 target
= expand_builtin_strlen (exp
, target
, target_mode
);
10168 case BUILT_IN_STRNLEN
:
10169 target
= expand_builtin_strnlen (exp
, target
, target_mode
);
10174 case BUILT_IN_STRCAT
:
10175 target
= expand_builtin_strcat (exp
);
10180 case BUILT_IN_GETTEXT
:
10181 case BUILT_IN_PUTS
:
10182 case BUILT_IN_PUTS_UNLOCKED
:
10183 case BUILT_IN_STRDUP
:
10184 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
10185 check_read_access (exp
, CALL_EXPR_ARG (exp
, 0));
10188 case BUILT_IN_INDEX
:
10189 case BUILT_IN_RINDEX
:
10190 case BUILT_IN_STRCHR
:
10191 case BUILT_IN_STRRCHR
:
10192 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10193 check_read_access (exp
, CALL_EXPR_ARG (exp
, 0));
10196 case BUILT_IN_FPUTS
:
10197 case BUILT_IN_FPUTS_UNLOCKED
:
10198 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
10199 check_read_access (exp
, CALL_EXPR_ARG (exp
, 0));
10202 case BUILT_IN_STRNDUP
:
10203 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10204 check_read_access (exp
, CALL_EXPR_ARG (exp
, 0), CALL_EXPR_ARG (exp
, 1));
10207 case BUILT_IN_STRCASECMP
:
10208 case BUILT_IN_STRPBRK
:
10209 case BUILT_IN_STRSPN
:
10210 case BUILT_IN_STRCSPN
:
10211 case BUILT_IN_STRSTR
:
10212 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
10214 check_read_access (exp
, CALL_EXPR_ARG (exp
, 0));
10215 check_read_access (exp
, CALL_EXPR_ARG (exp
, 1));
10219 case BUILT_IN_STRCPY
:
10220 target
= expand_builtin_strcpy (exp
, target
);
10225 case BUILT_IN_STRNCAT
:
10226 target
= expand_builtin_strncat (exp
, target
);
10231 case BUILT_IN_STRNCPY
:
10232 target
= expand_builtin_strncpy (exp
, target
);
10237 case BUILT_IN_STPCPY
:
10238 target
= expand_builtin_stpcpy (exp
, target
, mode
);
10243 case BUILT_IN_STPNCPY
:
10244 target
= expand_builtin_stpncpy (exp
, target
);
10249 case BUILT_IN_MEMCHR
:
10250 target
= expand_builtin_memchr (exp
, target
);
10255 case BUILT_IN_MEMCPY
:
10256 target
= expand_builtin_memcpy (exp
, target
);
10261 case BUILT_IN_MEMMOVE
:
10262 target
= expand_builtin_memmove (exp
, target
);
10267 case BUILT_IN_MEMPCPY
:
10268 target
= expand_builtin_mempcpy (exp
, target
);
10273 case BUILT_IN_MEMSET
:
10274 target
= expand_builtin_memset (exp
, target
, mode
);
10279 case BUILT_IN_BZERO
:
10280 target
= expand_builtin_bzero (exp
);
10285 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
10286 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
10287 when changing it to a strcmp call. */
10288 case BUILT_IN_STRCMP_EQ
:
10289 target
= expand_builtin_memcmp (exp
, target
, true);
10293 /* Change this call back to a BUILT_IN_STRCMP. */
10294 TREE_OPERAND (exp
, 1)
10295 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP
));
10297 /* Delete the last parameter. */
10299 vec
<tree
, va_gc
> *arg_vec
;
10300 vec_alloc (arg_vec
, 2);
10301 for (i
= 0; i
< 2; i
++)
10302 arg_vec
->quick_push (CALL_EXPR_ARG (exp
, i
));
10303 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), arg_vec
);
10306 case BUILT_IN_STRCMP
:
10307 target
= expand_builtin_strcmp (exp
, target
);
10312 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
10313 back to a BUILT_IN_STRNCMP. */
10314 case BUILT_IN_STRNCMP_EQ
:
10315 target
= expand_builtin_memcmp (exp
, target
, true);
10319 /* Change it back to a BUILT_IN_STRNCMP. */
10320 TREE_OPERAND (exp
, 1)
10321 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP
));
10324 case BUILT_IN_STRNCMP
:
10325 target
= expand_builtin_strncmp (exp
, target
, mode
);
10330 case BUILT_IN_BCMP
:
10331 case BUILT_IN_MEMCMP
:
10332 case BUILT_IN_MEMCMP_EQ
:
10333 target
= expand_builtin_memcmp (exp
, target
, fcode
== BUILT_IN_MEMCMP_EQ
);
10336 if (fcode
== BUILT_IN_MEMCMP_EQ
)
10338 tree newdecl
= builtin_decl_explicit (BUILT_IN_MEMCMP
);
10339 TREE_OPERAND (exp
, 1) = build_fold_addr_expr (newdecl
);
10343 case BUILT_IN_SETJMP
:
10344 /* This should have been lowered to the builtins below. */
10345 gcc_unreachable ();
10347 case BUILT_IN_SETJMP_SETUP
:
10348 /* __builtin_setjmp_setup is passed a pointer to an array of five words
10349 and the receiver label. */
10350 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
10352 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
10353 VOIDmode
, EXPAND_NORMAL
);
10354 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
10355 rtx_insn
*label_r
= label_rtx (label
);
10357 /* This is copied from the handling of non-local gotos. */
10358 expand_builtin_setjmp_setup (buf_addr
, label_r
);
10359 nonlocal_goto_handler_labels
10360 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
10361 nonlocal_goto_handler_labels
);
10362 /* ??? Do not let expand_label treat us as such since we would
10363 not want to be both on the list of non-local labels and on
10364 the list of forced labels. */
10365 FORCED_LABEL (label
) = 0;
10370 case BUILT_IN_SETJMP_RECEIVER
:
10371 /* __builtin_setjmp_receiver is passed the receiver label. */
10372 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
10374 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
10375 rtx_insn
*label_r
= label_rtx (label
);
10377 expand_builtin_setjmp_receiver (label_r
);
10382 /* __builtin_longjmp is passed a pointer to an array of five words.
10383 It's similar to the C library longjmp function but works with
10384 __builtin_setjmp above. */
10385 case BUILT_IN_LONGJMP
:
10386 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10388 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
10389 VOIDmode
, EXPAND_NORMAL
);
10390 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
10392 if (value
!= const1_rtx
)
10394 error ("%<__builtin_longjmp%> second argument must be 1");
10398 expand_builtin_longjmp (buf_addr
, value
);
10403 case BUILT_IN_NONLOCAL_GOTO
:
10404 target
= expand_builtin_nonlocal_goto (exp
);
10409 /* This updates the setjmp buffer that is its argument with the value
10410 of the current stack pointer. */
10411 case BUILT_IN_UPDATE_SETJMP_BUF
:
10412 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
10415 = expand_normal (CALL_EXPR_ARG (exp
, 0));
10417 expand_builtin_update_setjmp_buf (buf_addr
);
10422 case BUILT_IN_TRAP
:
10423 expand_builtin_trap ();
10426 case BUILT_IN_UNREACHABLE
:
10427 expand_builtin_unreachable ();
10430 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10431 case BUILT_IN_SIGNBITD32
:
10432 case BUILT_IN_SIGNBITD64
:
10433 case BUILT_IN_SIGNBITD128
:
10434 target
= expand_builtin_signbit (exp
, target
);
10439 /* Various hooks for the DWARF 2 __throw routine. */
10440 case BUILT_IN_UNWIND_INIT
:
10441 expand_builtin_unwind_init ();
10443 case BUILT_IN_DWARF_CFA
:
10444 return virtual_cfa_rtx
;
10445 #ifdef DWARF2_UNWIND_INFO
10446 case BUILT_IN_DWARF_SP_COLUMN
:
10447 return expand_builtin_dwarf_sp_column ();
10448 case BUILT_IN_INIT_DWARF_REG_SIZES
:
10449 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
10452 case BUILT_IN_FROB_RETURN_ADDR
:
10453 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
10454 case BUILT_IN_EXTRACT_RETURN_ADDR
:
10455 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
10456 case BUILT_IN_EH_RETURN
:
10457 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
10458 CALL_EXPR_ARG (exp
, 1));
10460 case BUILT_IN_EH_RETURN_DATA_REGNO
:
10461 return expand_builtin_eh_return_data_regno (exp
);
10462 case BUILT_IN_EXTEND_POINTER
:
10463 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
10464 case BUILT_IN_EH_POINTER
:
10465 return expand_builtin_eh_pointer (exp
);
10466 case BUILT_IN_EH_FILTER
:
10467 return expand_builtin_eh_filter (exp
);
10468 case BUILT_IN_EH_COPY_VALUES
:
10469 return expand_builtin_eh_copy_values (exp
);
10471 case BUILT_IN_VA_START
:
10472 return expand_builtin_va_start (exp
);
10473 case BUILT_IN_VA_END
:
10474 return expand_builtin_va_end (exp
);
10475 case BUILT_IN_VA_COPY
:
10476 return expand_builtin_va_copy (exp
);
10477 case BUILT_IN_EXPECT
:
10478 return expand_builtin_expect (exp
, target
);
10479 case BUILT_IN_EXPECT_WITH_PROBABILITY
:
10480 return expand_builtin_expect_with_probability (exp
, target
);
10481 case BUILT_IN_ASSUME_ALIGNED
:
10482 return expand_builtin_assume_aligned (exp
, target
);
10483 case BUILT_IN_PREFETCH
:
10484 expand_builtin_prefetch (exp
);
10487 case BUILT_IN_INIT_TRAMPOLINE
:
10488 return expand_builtin_init_trampoline (exp
, true);
10489 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
10490 return expand_builtin_init_trampoline (exp
, false);
10491 case BUILT_IN_ADJUST_TRAMPOLINE
:
10492 return expand_builtin_adjust_trampoline (exp
);
10494 case BUILT_IN_INIT_DESCRIPTOR
:
10495 return expand_builtin_init_descriptor (exp
);
10496 case BUILT_IN_ADJUST_DESCRIPTOR
:
10497 return expand_builtin_adjust_descriptor (exp
);
10499 case BUILT_IN_FORK
:
10500 case BUILT_IN_EXECL
:
10501 case BUILT_IN_EXECV
:
10502 case BUILT_IN_EXECLP
:
10503 case BUILT_IN_EXECLE
:
10504 case BUILT_IN_EXECVP
:
10505 case BUILT_IN_EXECVE
:
10506 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
10511 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
10512 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
10513 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
10514 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
10515 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
10516 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
10517 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
10522 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
10523 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
10524 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
10525 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
10526 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
10527 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
10528 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
10533 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
10534 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
10535 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
10536 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
10537 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
10538 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
10539 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
10544 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
10545 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
10546 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
10547 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
10548 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
10549 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
10550 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
10555 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
10556 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
10557 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
10558 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
10559 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
10560 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
10561 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
10566 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
10567 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
10568 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
10569 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
10570 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
10571 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
10572 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
10577 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
10578 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
10579 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
10580 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
10581 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
10582 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
10583 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
10588 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
10589 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
10590 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
10591 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
10592 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
10593 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
10594 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
10599 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
10600 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
10601 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
10602 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
10603 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
10604 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
10605 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
10610 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
10611 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
10612 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
10613 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
10614 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
10615 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
10616 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
10621 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
10622 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
10623 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
10624 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
10625 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
10626 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
10627 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
10632 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
10633 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
10634 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
10635 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
10636 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
10637 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
10638 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
10643 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
10644 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
10645 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
10646 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
10647 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
10648 if (mode
== VOIDmode
)
10649 mode
= TYPE_MODE (boolean_type_node
);
10650 if (!target
|| !register_operand (target
, mode
))
10651 target
= gen_reg_rtx (mode
);
10653 mode
= get_builtin_sync_mode
10654 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
10655 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
10660 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
10661 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
10662 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
10663 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
10664 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
10665 mode
= get_builtin_sync_mode
10666 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
10667 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
10672 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
10673 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
10674 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
10675 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
10676 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
10677 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
10678 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
10683 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
10684 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
10685 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
10686 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
10687 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
10688 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
10689 expand_builtin_sync_lock_release (mode
, exp
);
10692 case BUILT_IN_SYNC_SYNCHRONIZE
:
10693 expand_builtin_sync_synchronize ();
10696 case BUILT_IN_ATOMIC_EXCHANGE_1
:
10697 case BUILT_IN_ATOMIC_EXCHANGE_2
:
10698 case BUILT_IN_ATOMIC_EXCHANGE_4
:
10699 case BUILT_IN_ATOMIC_EXCHANGE_8
:
10700 case BUILT_IN_ATOMIC_EXCHANGE_16
:
10701 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
10702 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
10707 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
10708 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
10709 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
10710 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
10711 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
10713 unsigned int nargs
, z
;
10714 vec
<tree
, va_gc
> *vec
;
10717 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
10718 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
10722 /* If this is turned into an external library call, the weak parameter
10723 must be dropped to match the expected parameter list. */
10724 nargs
= call_expr_nargs (exp
);
10725 vec_alloc (vec
, nargs
- 1);
10726 for (z
= 0; z
< 3; z
++)
10727 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
10728 /* Skip the boolean weak parameter. */
10729 for (z
= 4; z
< 6; z
++)
10730 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
10731 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
10735 case BUILT_IN_ATOMIC_LOAD_1
:
10736 case BUILT_IN_ATOMIC_LOAD_2
:
10737 case BUILT_IN_ATOMIC_LOAD_4
:
10738 case BUILT_IN_ATOMIC_LOAD_8
:
10739 case BUILT_IN_ATOMIC_LOAD_16
:
10740 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
10741 target
= expand_builtin_atomic_load (mode
, exp
, target
);
10746 case BUILT_IN_ATOMIC_STORE_1
:
10747 case BUILT_IN_ATOMIC_STORE_2
:
10748 case BUILT_IN_ATOMIC_STORE_4
:
10749 case BUILT_IN_ATOMIC_STORE_8
:
10750 case BUILT_IN_ATOMIC_STORE_16
:
10751 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
10752 target
= expand_builtin_atomic_store (mode
, exp
);
10757 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
10758 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
10759 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
10760 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
10761 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
10763 enum built_in_function lib
;
10764 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
10765 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
10766 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
10767 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
10773 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
10774 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
10775 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
10776 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
10777 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
10779 enum built_in_function lib
;
10780 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
10781 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
10782 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
10783 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
10789 case BUILT_IN_ATOMIC_AND_FETCH_1
:
10790 case BUILT_IN_ATOMIC_AND_FETCH_2
:
10791 case BUILT_IN_ATOMIC_AND_FETCH_4
:
10792 case BUILT_IN_ATOMIC_AND_FETCH_8
:
10793 case BUILT_IN_ATOMIC_AND_FETCH_16
:
10795 enum built_in_function lib
;
10796 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
10797 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
10798 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
10799 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
10805 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
10806 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
10807 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
10808 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
10809 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
10811 enum built_in_function lib
;
10812 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
10813 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
10814 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
10815 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
10821 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
10822 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
10823 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
10824 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
10825 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
10827 enum built_in_function lib
;
10828 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
10829 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
10830 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
10831 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
10837 case BUILT_IN_ATOMIC_OR_FETCH_1
:
10838 case BUILT_IN_ATOMIC_OR_FETCH_2
:
10839 case BUILT_IN_ATOMIC_OR_FETCH_4
:
10840 case BUILT_IN_ATOMIC_OR_FETCH_8
:
10841 case BUILT_IN_ATOMIC_OR_FETCH_16
:
10843 enum built_in_function lib
;
10844 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
10845 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
10846 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
10847 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
10853 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
10854 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
10855 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
10856 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
10857 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
10858 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
10859 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
10860 ignore
, BUILT_IN_NONE
);
10865 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
10866 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
10867 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
10868 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
10869 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
10870 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
10871 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
10872 ignore
, BUILT_IN_NONE
);
10877 case BUILT_IN_ATOMIC_FETCH_AND_1
:
10878 case BUILT_IN_ATOMIC_FETCH_AND_2
:
10879 case BUILT_IN_ATOMIC_FETCH_AND_4
:
10880 case BUILT_IN_ATOMIC_FETCH_AND_8
:
10881 case BUILT_IN_ATOMIC_FETCH_AND_16
:
10882 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
10883 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
10884 ignore
, BUILT_IN_NONE
);
10889 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
10890 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
10891 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
10892 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
10893 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
10894 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
10895 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
10896 ignore
, BUILT_IN_NONE
);
10901 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
10902 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
10903 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
10904 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
10905 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
10906 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
10907 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
10908 ignore
, BUILT_IN_NONE
);
10913 case BUILT_IN_ATOMIC_FETCH_OR_1
:
10914 case BUILT_IN_ATOMIC_FETCH_OR_2
:
10915 case BUILT_IN_ATOMIC_FETCH_OR_4
:
10916 case BUILT_IN_ATOMIC_FETCH_OR_8
:
10917 case BUILT_IN_ATOMIC_FETCH_OR_16
:
10918 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
10919 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
10920 ignore
, BUILT_IN_NONE
);
10925 case BUILT_IN_ATOMIC_TEST_AND_SET
:
10926 return expand_builtin_atomic_test_and_set (exp
, target
);
10928 case BUILT_IN_ATOMIC_CLEAR
:
10929 return expand_builtin_atomic_clear (exp
);
10931 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10932 return expand_builtin_atomic_always_lock_free (exp
);
10934 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10935 target
= expand_builtin_atomic_is_lock_free (exp
);
10940 case BUILT_IN_ATOMIC_THREAD_FENCE
:
10941 expand_builtin_atomic_thread_fence (exp
);
10944 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
10945 expand_builtin_atomic_signal_fence (exp
);
10948 case BUILT_IN_OBJECT_SIZE
:
10949 return expand_builtin_object_size (exp
);
10951 case BUILT_IN_MEMCPY_CHK
:
10952 case BUILT_IN_MEMPCPY_CHK
:
10953 case BUILT_IN_MEMMOVE_CHK
:
10954 case BUILT_IN_MEMSET_CHK
:
10955 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
10960 case BUILT_IN_STRCPY_CHK
:
10961 case BUILT_IN_STPCPY_CHK
:
10962 case BUILT_IN_STRNCPY_CHK
:
10963 case BUILT_IN_STPNCPY_CHK
:
10964 case BUILT_IN_STRCAT_CHK
:
10965 case BUILT_IN_STRNCAT_CHK
:
10966 case BUILT_IN_SNPRINTF_CHK
:
10967 case BUILT_IN_VSNPRINTF_CHK
:
10968 maybe_emit_chk_warning (exp
, fcode
);
10971 case BUILT_IN_SPRINTF_CHK
:
10972 case BUILT_IN_VSPRINTF_CHK
:
10973 maybe_emit_sprintf_chk_warning (exp
, fcode
);
10976 case BUILT_IN_THREAD_POINTER
:
10977 return expand_builtin_thread_pointer (exp
, target
);
10979 case BUILT_IN_SET_THREAD_POINTER
:
10980 expand_builtin_set_thread_pointer (exp
);
10983 case BUILT_IN_ACC_ON_DEVICE
:
10984 /* Do library call, if we failed to expand the builtin when
10988 case BUILT_IN_GOACC_PARLEVEL_ID
:
10989 case BUILT_IN_GOACC_PARLEVEL_SIZE
:
10990 return expand_builtin_goacc_parlevel_id_size (exp
, target
, ignore
);
10992 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR
:
10993 return expand_speculation_safe_value (VOIDmode
, exp
, target
, ignore
);
10995 case BUILT_IN_SPECULATION_SAFE_VALUE_1
:
10996 case BUILT_IN_SPECULATION_SAFE_VALUE_2
:
10997 case BUILT_IN_SPECULATION_SAFE_VALUE_4
:
10998 case BUILT_IN_SPECULATION_SAFE_VALUE_8
:
10999 case BUILT_IN_SPECULATION_SAFE_VALUE_16
:
11000 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SPECULATION_SAFE_VALUE_1
);
11001 return expand_speculation_safe_value (mode
, exp
, target
, ignore
);
11003 default: /* just do library call, if unknown builtin */
11007 /* The switch statement above can drop through to cause the function
11008 to be called normally. */
11009 return expand_call (exp
, target
, ignore
);
11012 /* Determine whether a tree node represents a call to a built-in
11013 function. If the tree T is a call to a built-in function with
11014 the right number of arguments of the appropriate types, return
11015 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
11016 Otherwise the return value is END_BUILTINS. */
11018 enum built_in_function
11019 builtin_mathfn_code (const_tree t
)
11021 const_tree fndecl
, arg
, parmlist
;
11022 const_tree argtype
, parmtype
;
11023 const_call_expr_arg_iterator iter
;
11025 if (TREE_CODE (t
) != CALL_EXPR
)
11026 return END_BUILTINS
;
11028 fndecl
= get_callee_fndecl (t
);
11029 if (fndecl
== NULL_TREE
|| !fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
11030 return END_BUILTINS
;
11032 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
11033 init_const_call_expr_arg_iterator (t
, &iter
);
11034 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
11036 /* If a function doesn't take a variable number of arguments,
11037 the last element in the list will have type `void'. */
11038 parmtype
= TREE_VALUE (parmlist
);
11039 if (VOID_TYPE_P (parmtype
))
11041 if (more_const_call_expr_args_p (&iter
))
11042 return END_BUILTINS
;
11043 return DECL_FUNCTION_CODE (fndecl
);
11046 if (! more_const_call_expr_args_p (&iter
))
11047 return END_BUILTINS
;
11049 arg
= next_const_call_expr_arg (&iter
);
11050 argtype
= TREE_TYPE (arg
);
11052 if (SCALAR_FLOAT_TYPE_P (parmtype
))
11054 if (! SCALAR_FLOAT_TYPE_P (argtype
))
11055 return END_BUILTINS
;
11057 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
11059 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
11060 return END_BUILTINS
;
11062 else if (POINTER_TYPE_P (parmtype
))
11064 if (! POINTER_TYPE_P (argtype
))
11065 return END_BUILTINS
;
11067 else if (INTEGRAL_TYPE_P (parmtype
))
11069 if (! INTEGRAL_TYPE_P (argtype
))
11070 return END_BUILTINS
;
11073 return END_BUILTINS
;
11076 /* Variable-length argument list. */
11077 return DECL_FUNCTION_CODE (fndecl
);
11080 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
11081 evaluate to a constant. */
11084 fold_builtin_constant_p (tree arg
)
11086 /* We return 1 for a numeric type that's known to be a constant
11087 value at compile-time or for an aggregate type that's a
11088 literal constant. */
11091 /* If we know this is a constant, emit the constant of one. */
11092 if (CONSTANT_CLASS_P (arg
)
11093 || (TREE_CODE (arg
) == CONSTRUCTOR
11094 && TREE_CONSTANT (arg
)))
11095 return integer_one_node
;
11096 if (TREE_CODE (arg
) == ADDR_EXPR
)
11098 tree op
= TREE_OPERAND (arg
, 0);
11099 if (TREE_CODE (op
) == STRING_CST
11100 || (TREE_CODE (op
) == ARRAY_REF
11101 && integer_zerop (TREE_OPERAND (op
, 1))
11102 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
11103 return integer_one_node
;
11106 /* If this expression has side effects, show we don't know it to be a
11107 constant. Likewise if it's a pointer or aggregate type since in
11108 those case we only want literals, since those are only optimized
11109 when generating RTL, not later.
11110 And finally, if we are compiling an initializer, not code, we
11111 need to return a definite result now; there's not going to be any
11112 more optimization done. */
11113 if (TREE_SIDE_EFFECTS (arg
)
11114 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
11115 || POINTER_TYPE_P (TREE_TYPE (arg
))
11117 || folding_initializer
11118 || force_folding_builtin_constant_p
)
11119 return integer_zero_node
;
11124 /* Create builtin_expect or builtin_expect_with_probability
11125 with PRED and EXPECTED as its arguments and return it as a truthvalue.
11126 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
11127 builtin_expect_with_probability instead uses third argument as PROBABILITY
11131 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
11132 tree predictor
, tree probability
)
11134 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
11136 fn
= builtin_decl_explicit (probability
== NULL_TREE
? BUILT_IN_EXPECT
11137 : BUILT_IN_EXPECT_WITH_PROBABILITY
);
11138 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
11139 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
11140 pred_type
= TREE_VALUE (arg_types
);
11141 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
11143 pred
= fold_convert_loc (loc
, pred_type
, pred
);
11144 expected
= fold_convert_loc (loc
, expected_type
, expected
);
11147 call_expr
= build_call_expr_loc (loc
, fn
, 3, pred
, expected
, probability
);
11149 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
11152 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
11153 build_int_cst (ret_type
, 0));
11156 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
11157 NULL_TREE if no simplification is possible. */
11160 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
,
11163 tree inner
, fndecl
, inner_arg0
;
11164 enum tree_code code
;
11166 /* Distribute the expected value over short-circuiting operators.
11167 See through the cast from truthvalue_type_node to long. */
11169 while (CONVERT_EXPR_P (inner_arg0
)
11170 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
11171 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
11172 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
11174 /* If this is a builtin_expect within a builtin_expect keep the
11175 inner one. See through a comparison against a constant. It
11176 might have been added to create a thruthvalue. */
11177 inner
= inner_arg0
;
11179 if (COMPARISON_CLASS_P (inner
)
11180 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
11181 inner
= TREE_OPERAND (inner
, 0);
11183 if (TREE_CODE (inner
) == CALL_EXPR
11184 && (fndecl
= get_callee_fndecl (inner
))
11185 && (fndecl_built_in_p (fndecl
, BUILT_IN_EXPECT
)
11186 || fndecl_built_in_p (fndecl
, BUILT_IN_EXPECT_WITH_PROBABILITY
)))
11189 inner
= inner_arg0
;
11190 code
= TREE_CODE (inner
);
11191 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
11193 tree op0
= TREE_OPERAND (inner
, 0);
11194 tree op1
= TREE_OPERAND (inner
, 1);
11195 arg1
= save_expr (arg1
);
11197 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
, arg3
);
11198 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
, arg3
);
11199 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
11201 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
11204 /* If the argument isn't invariant then there's nothing else we can do. */
11205 if (!TREE_CONSTANT (inner_arg0
))
11208 /* If we expect that a comparison against the argument will fold to
11209 a constant return the constant. In practice, this means a true
11210 constant or the address of a non-weak symbol. */
11211 inner
= inner_arg0
;
11212 STRIP_NOPS (inner
);
11213 if (TREE_CODE (inner
) == ADDR_EXPR
)
11217 inner
= TREE_OPERAND (inner
, 0);
11219 while (TREE_CODE (inner
) == COMPONENT_REF
11220 || TREE_CODE (inner
) == ARRAY_REF
);
11221 if (VAR_OR_FUNCTION_DECL_P (inner
) && DECL_WEAK (inner
))
11225 /* Otherwise, ARG0 already has the proper type for the return value. */
11229 /* Fold a call to __builtin_classify_type with argument ARG. */
11232 fold_builtin_classify_type (tree arg
)
11235 return build_int_cst (integer_type_node
, no_type_class
);
11237 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
11240 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
11244 fold_builtin_strlen (location_t loc
, tree expr
, tree type
, tree arg
)
11246 if (!validate_arg (arg
, POINTER_TYPE
))
11250 c_strlen_data lendata
= { };
11251 tree len
= c_strlen (arg
, 0, &lendata
);
11254 return fold_convert_loc (loc
, type
, len
);
11257 c_strlen (arg
, 1, &lendata
);
11261 if (EXPR_HAS_LOCATION (arg
))
11262 loc
= EXPR_LOCATION (arg
);
11263 else if (loc
== UNKNOWN_LOCATION
)
11264 loc
= input_location
;
11265 warn_string_no_nul (loc
, expr
, "strlen", arg
, lendata
.decl
);
11272 /* Fold a call to __builtin_inf or __builtin_huge_val. */
11275 fold_builtin_inf (location_t loc
, tree type
, int warn
)
11277 REAL_VALUE_TYPE real
;
11279 /* __builtin_inff is intended to be usable to define INFINITY on all
11280 targets. If an infinity is not available, INFINITY expands "to a
11281 positive constant of type float that overflows at translation
11282 time", footnote "In this case, using INFINITY will violate the
11283 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
11284 Thus we pedwarn to ensure this constraint violation is
11286 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
11287 pedwarn (loc
, 0, "target format does not support infinity");
11290 return build_real (type
, real
);
11293 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
11294 NULL_TREE if no simplification can be made. */
11297 fold_builtin_sincos (location_t loc
,
11298 tree arg0
, tree arg1
, tree arg2
)
11301 tree fndecl
, call
= NULL_TREE
;
11303 if (!validate_arg (arg0
, REAL_TYPE
)
11304 || !validate_arg (arg1
, POINTER_TYPE
)
11305 || !validate_arg (arg2
, POINTER_TYPE
))
11308 type
= TREE_TYPE (arg0
);
11310 /* Calculate the result when the argument is a constant. */
11311 built_in_function fn
= mathfn_built_in_2 (type
, CFN_BUILT_IN_CEXPI
);
11312 if (fn
== END_BUILTINS
)
11315 /* Canonicalize sincos to cexpi. */
11316 if (TREE_CODE (arg0
) == REAL_CST
)
11318 tree complex_type
= build_complex_type (type
);
11319 call
= fold_const_call (as_combined_fn (fn
), complex_type
, arg0
);
11323 if (!targetm
.libc_has_function (function_c99_math_complex
, type
)
11324 || !builtin_decl_implicit_p (fn
))
11326 fndecl
= builtin_decl_explicit (fn
);
11327 call
= build_call_expr_loc (loc
, fndecl
, 1, arg0
);
11328 call
= builtin_save_expr (call
);
11331 tree ptype
= build_pointer_type (type
);
11332 arg1
= fold_convert (ptype
, arg1
);
11333 arg2
= fold_convert (ptype
, arg2
);
11334 return build2 (COMPOUND_EXPR
, void_type_node
,
11335 build2 (MODIFY_EXPR
, void_type_node
,
11336 build_fold_indirect_ref_loc (loc
, arg1
),
11337 fold_build1_loc (loc
, IMAGPART_EXPR
, type
, call
)),
11338 build2 (MODIFY_EXPR
, void_type_node
,
11339 build_fold_indirect_ref_loc (loc
, arg2
),
11340 fold_build1_loc (loc
, REALPART_EXPR
, type
, call
)));
11343 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
11344 Return NULL_TREE if no simplification can be made. */
11347 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
11349 if (!validate_arg (arg1
, POINTER_TYPE
)
11350 || !validate_arg (arg2
, POINTER_TYPE
)
11351 || !validate_arg (len
, INTEGER_TYPE
))
11354 /* If the LEN parameter is zero, return zero. */
11355 if (integer_zerop (len
))
11356 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
11359 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
11360 if (operand_equal_p (arg1
, arg2
, 0))
11361 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
11363 /* If len parameter is one, return an expression corresponding to
11364 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
11365 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
11367 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
11368 tree cst_uchar_ptr_node
11369 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
11372 = fold_convert_loc (loc
, integer_type_node
,
11373 build1 (INDIRECT_REF
, cst_uchar_node
,
11374 fold_convert_loc (loc
,
11375 cst_uchar_ptr_node
,
11378 = fold_convert_loc (loc
, integer_type_node
,
11379 build1 (INDIRECT_REF
, cst_uchar_node
,
11380 fold_convert_loc (loc
,
11381 cst_uchar_ptr_node
,
11383 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
11389 /* Fold a call to builtin isascii with argument ARG. */
11392 fold_builtin_isascii (location_t loc
, tree arg
)
11394 if (!validate_arg (arg
, INTEGER_TYPE
))
11398 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
11399 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
11400 build_int_cst (integer_type_node
,
11401 ~ (unsigned HOST_WIDE_INT
) 0x7f));
11402 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
11403 arg
, integer_zero_node
);
11407 /* Fold a call to builtin toascii with argument ARG. */
11410 fold_builtin_toascii (location_t loc
, tree arg
)
11412 if (!validate_arg (arg
, INTEGER_TYPE
))
11415 /* Transform toascii(c) -> (c & 0x7f). */
11416 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
11417 build_int_cst (integer_type_node
, 0x7f));
11420 /* Fold a call to builtin isdigit with argument ARG. */
11423 fold_builtin_isdigit (location_t loc
, tree arg
)
11425 if (!validate_arg (arg
, INTEGER_TYPE
))
11429 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
11430 /* According to the C standard, isdigit is unaffected by locale.
11431 However, it definitely is affected by the target character set. */
11432 unsigned HOST_WIDE_INT target_digit0
11433 = lang_hooks
.to_target_charset ('0');
11435 if (target_digit0
== 0)
11438 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
11439 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
11440 build_int_cst (unsigned_type_node
, target_digit0
));
11441 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
11442 build_int_cst (unsigned_type_node
, 9));
11446 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
11449 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
11451 if (!validate_arg (arg
, REAL_TYPE
))
11454 arg
= fold_convert_loc (loc
, type
, arg
);
11455 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
11458 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
11461 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
11463 if (!validate_arg (arg
, INTEGER_TYPE
))
11466 arg
= fold_convert_loc (loc
, type
, arg
);
11467 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
11470 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
11473 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
11475 if (validate_arg (arg
, COMPLEX_TYPE
)
11476 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
11478 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
11482 tree new_arg
= builtin_save_expr (arg
);
11483 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
11484 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
11485 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
11492 /* Fold a call to builtin frexp, we can assume the base is 2. */
11495 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
11497 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
11502 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
11505 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
11507 /* Proceed if a valid pointer type was passed in. */
11508 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
11510 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
11516 /* For +-0, return (*exp = 0, +-0). */
11517 exp
= integer_zero_node
;
11522 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
11523 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
11526 /* Since the frexp function always expects base 2, and in
11527 GCC normalized significands are already in the range
11528 [0.5, 1.0), we have exactly what frexp wants. */
11529 REAL_VALUE_TYPE frac_rvt
= *value
;
11530 SET_REAL_EXP (&frac_rvt
, 0);
11531 frac
= build_real (rettype
, frac_rvt
);
11532 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
11536 gcc_unreachable ();
11539 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
11540 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
11541 TREE_SIDE_EFFECTS (arg1
) = 1;
11542 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
11548 /* Fold a call to builtin modf. */
11551 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
11553 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
11558 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
11561 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
11563 /* Proceed if a valid pointer type was passed in. */
11564 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
11566 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
11567 REAL_VALUE_TYPE trunc
, frac
;
11573 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
11574 trunc
= frac
= *value
;
11577 /* For +-Inf, return (*arg1 = arg0, +-0). */
11579 frac
.sign
= value
->sign
;
11583 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
11584 real_trunc (&trunc
, VOIDmode
, value
);
11585 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
11586 /* If the original number was negative and already
11587 integral, then the fractional part is -0.0. */
11588 if (value
->sign
&& frac
.cl
== rvc_zero
)
11589 frac
.sign
= value
->sign
;
11593 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
11594 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
11595 build_real (rettype
, trunc
));
11596 TREE_SIDE_EFFECTS (arg1
) = 1;
11597 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
11598 build_real (rettype
, frac
));
11604 /* Given a location LOC, an interclass builtin function decl FNDECL
11605 and its single argument ARG, return an folded expression computing
11606 the same, or NULL_TREE if we either couldn't or didn't want to fold
11607 (the latter happen if there's an RTL instruction available). */
11610 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
11614 if (!validate_arg (arg
, REAL_TYPE
))
11617 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
11620 mode
= TYPE_MODE (TREE_TYPE (arg
));
11622 bool is_ibm_extended
= MODE_COMPOSITE_P (mode
);
11624 /* If there is no optab, try generic code. */
11625 switch (DECL_FUNCTION_CODE (fndecl
))
11629 CASE_FLT_FN (BUILT_IN_ISINF
):
11631 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
11632 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
11633 tree type
= TREE_TYPE (arg
);
11637 if (is_ibm_extended
)
11639 /* NaN and Inf are encoded in the high-order double value
11640 only. The low-order value is not significant. */
11641 type
= double_type_node
;
11643 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
11645 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
11646 real_from_string (&r
, buf
);
11647 result
= build_call_expr (isgr_fn
, 2,
11648 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
11649 build_real (type
, r
));
11652 CASE_FLT_FN (BUILT_IN_FINITE
):
11653 case BUILT_IN_ISFINITE
:
11655 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
11656 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
11657 tree type
= TREE_TYPE (arg
);
11661 if (is_ibm_extended
)
11663 /* NaN and Inf are encoded in the high-order double value
11664 only. The low-order value is not significant. */
11665 type
= double_type_node
;
11667 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
11669 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
11670 real_from_string (&r
, buf
);
11671 result
= build_call_expr (isle_fn
, 2,
11672 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
11673 build_real (type
, r
));
11674 /*result = fold_build2_loc (loc, UNGT_EXPR,
11675 TREE_TYPE (TREE_TYPE (fndecl)),
11676 fold_build1_loc (loc, ABS_EXPR, type, arg),
11677 build_real (type, r));
11678 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
11679 TREE_TYPE (TREE_TYPE (fndecl)),
11683 case BUILT_IN_ISNORMAL
:
11685 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
11686 islessequal(fabs(x),DBL_MAX). */
11687 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
11688 tree type
= TREE_TYPE (arg
);
11689 tree orig_arg
, max_exp
, min_exp
;
11690 machine_mode orig_mode
= mode
;
11691 REAL_VALUE_TYPE rmax
, rmin
;
11694 orig_arg
= arg
= builtin_save_expr (arg
);
11695 if (is_ibm_extended
)
11697 /* Use double to test the normal range of IBM extended
11698 precision. Emin for IBM extended precision is
11699 different to emin for IEEE double, being 53 higher
11700 since the low double exponent is at least 53 lower
11701 than the high double exponent. */
11702 type
= double_type_node
;
11704 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
11706 arg
= fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
11708 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
11709 real_from_string (&rmax
, buf
);
11710 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (orig_mode
)->emin
- 1);
11711 real_from_string (&rmin
, buf
);
11712 max_exp
= build_real (type
, rmax
);
11713 min_exp
= build_real (type
, rmin
);
11715 max_exp
= build_call_expr (isle_fn
, 2, arg
, max_exp
);
11716 if (is_ibm_extended
)
11718 /* Testing the high end of the range is done just using
11719 the high double, using the same test as isfinite().
11720 For the subnormal end of the range we first test the
11721 high double, then if its magnitude is equal to the
11722 limit of 0x1p-969, we test whether the low double is
11723 non-zero and opposite sign to the high double. */
11724 tree
const islt_fn
= builtin_decl_explicit (BUILT_IN_ISLESS
);
11725 tree
const isgt_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
11726 tree gt_min
= build_call_expr (isgt_fn
, 2, arg
, min_exp
);
11727 tree eq_min
= fold_build2 (EQ_EXPR
, integer_type_node
,
11729 tree as_complex
= build1 (VIEW_CONVERT_EXPR
,
11730 complex_double_type_node
, orig_arg
);
11731 tree hi_dbl
= build1 (REALPART_EXPR
, type
, as_complex
);
11732 tree lo_dbl
= build1 (IMAGPART_EXPR
, type
, as_complex
);
11733 tree zero
= build_real (type
, dconst0
);
11734 tree hilt
= build_call_expr (islt_fn
, 2, hi_dbl
, zero
);
11735 tree lolt
= build_call_expr (islt_fn
, 2, lo_dbl
, zero
);
11736 tree logt
= build_call_expr (isgt_fn
, 2, lo_dbl
, zero
);
11737 tree ok_lo
= fold_build1 (TRUTH_NOT_EXPR
, integer_type_node
,
11738 fold_build3 (COND_EXPR
,
11740 hilt
, logt
, lolt
));
11741 eq_min
= fold_build2 (TRUTH_ANDIF_EXPR
, integer_type_node
,
11743 min_exp
= fold_build2 (TRUTH_ORIF_EXPR
, integer_type_node
,
11749 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
11750 min_exp
= build_call_expr (isge_fn
, 2, arg
, min_exp
);
11752 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
,
11763 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
11764 ARG is the argument for the call. */
11767 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
11769 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
11771 if (!validate_arg (arg
, REAL_TYPE
))
11774 switch (builtin_index
)
11776 case BUILT_IN_ISINF
:
11777 if (tree_expr_infinite_p (arg
))
11778 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
11779 if (!tree_expr_maybe_infinite_p (arg
))
11780 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
11783 case BUILT_IN_ISINF_SIGN
:
11785 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
11786 /* In a boolean context, GCC will fold the inner COND_EXPR to
11787 1. So e.g. "if (isinf_sign(x))" would be folded to just
11788 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
11789 tree signbit_fn
= builtin_decl_explicit (BUILT_IN_SIGNBIT
);
11790 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
11791 tree tmp
= NULL_TREE
;
11793 arg
= builtin_save_expr (arg
);
11795 if (signbit_fn
&& isinf_fn
)
11797 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
11798 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
11800 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
11801 signbit_call
, integer_zero_node
);
11802 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
11803 isinf_call
, integer_zero_node
);
11805 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
11806 integer_minus_one_node
, integer_one_node
);
11807 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
11809 integer_zero_node
);
11815 case BUILT_IN_ISFINITE
:
11816 if (tree_expr_finite_p (arg
))
11817 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
11818 if (tree_expr_nan_p (arg
) || tree_expr_infinite_p (arg
))
11819 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
11822 case BUILT_IN_ISNAN
:
11823 if (tree_expr_nan_p (arg
))
11824 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
11825 if (!tree_expr_maybe_nan_p (arg
))
11826 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
11829 bool is_ibm_extended
= MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg
)));
11830 if (is_ibm_extended
)
11832 /* NaN and Inf are encoded in the high-order double value
11833 only. The low-order value is not significant. */
11834 arg
= fold_build1_loc (loc
, NOP_EXPR
, double_type_node
, arg
);
11837 arg
= builtin_save_expr (arg
);
11838 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
11841 gcc_unreachable ();
11845 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
11846 This builtin will generate code to return the appropriate floating
11847 point classification depending on the value of the floating point
11848 number passed in. The possible return values must be supplied as
11849 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
11850 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
11851 one floating point argument which is "type generic". */
11854 fold_builtin_fpclassify (location_t loc
, tree
*args
, int nargs
)
11856 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
11857 arg
, type
, res
, tmp
;
11862 /* Verify the required arguments in the original call. */
11864 || !validate_arg (args
[0], INTEGER_TYPE
)
11865 || !validate_arg (args
[1], INTEGER_TYPE
)
11866 || !validate_arg (args
[2], INTEGER_TYPE
)
11867 || !validate_arg (args
[3], INTEGER_TYPE
)
11868 || !validate_arg (args
[4], INTEGER_TYPE
)
11869 || !validate_arg (args
[5], REAL_TYPE
))
11873 fp_infinite
= args
[1];
11874 fp_normal
= args
[2];
11875 fp_subnormal
= args
[3];
11878 type
= TREE_TYPE (arg
);
11879 mode
= TYPE_MODE (type
);
11880 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
11882 /* fpclassify(x) ->
11883 isnan(x) ? FP_NAN :
11884 (fabs(x) == Inf ? FP_INFINITE :
11885 (fabs(x) >= DBL_MIN ? FP_NORMAL :
11886 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
11888 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
11889 build_real (type
, dconst0
));
11890 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
11891 tmp
, fp_zero
, fp_subnormal
);
11893 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
11894 real_from_string (&r
, buf
);
11895 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
11896 arg
, build_real (type
, r
));
11897 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
11899 if (tree_expr_maybe_infinite_p (arg
))
11902 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
11903 build_real (type
, r
));
11904 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
11908 if (tree_expr_maybe_nan_p (arg
))
11910 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
11911 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
11917 /* Fold a call to an unordered comparison function such as
11918 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
11919 being called and ARG0 and ARG1 are the arguments for the call.
11920 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
11921 the opposite of the desired result. UNORDERED_CODE is used
11922 for modes that can hold NaNs and ORDERED_CODE is used for
11926 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
11927 enum tree_code unordered_code
,
11928 enum tree_code ordered_code
)
11930 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
11931 enum tree_code code
;
11933 enum tree_code code0
, code1
;
11934 tree cmp_type
= NULL_TREE
;
11936 type0
= TREE_TYPE (arg0
);
11937 type1
= TREE_TYPE (arg1
);
11939 code0
= TREE_CODE (type0
);
11940 code1
= TREE_CODE (type1
);
11942 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
11943 /* Choose the wider of two real types. */
11944 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
11946 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
11948 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
11951 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
11952 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
11954 if (unordered_code
== UNORDERED_EXPR
)
11956 if (tree_expr_nan_p (arg0
) || tree_expr_nan_p (arg1
))
11957 return omit_two_operands_loc (loc
, type
, integer_one_node
, arg0
, arg1
);
11958 if (!tree_expr_maybe_nan_p (arg0
) && !tree_expr_maybe_nan_p (arg1
))
11959 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
11960 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
11963 code
= (tree_expr_maybe_nan_p (arg0
) || tree_expr_maybe_nan_p (arg1
))
11964 ? unordered_code
: ordered_code
;
11965 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
11966 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
11969 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
11970 arithmetics if it can never overflow, or into internal functions that
11971 return both result of arithmetics and overflowed boolean flag in
11972 a complex integer result, or some other check for overflow.
11973 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
11974 checking part of that. */
11977 fold_builtin_arith_overflow (location_t loc
, enum built_in_function fcode
,
11978 tree arg0
, tree arg1
, tree arg2
)
11980 enum internal_fn ifn
= IFN_LAST
;
11981 /* The code of the expression corresponding to the built-in. */
11982 enum tree_code opcode
= ERROR_MARK
;
11983 bool ovf_only
= false;
11987 case BUILT_IN_ADD_OVERFLOW_P
:
11990 case BUILT_IN_ADD_OVERFLOW
:
11991 case BUILT_IN_SADD_OVERFLOW
:
11992 case BUILT_IN_SADDL_OVERFLOW
:
11993 case BUILT_IN_SADDLL_OVERFLOW
:
11994 case BUILT_IN_UADD_OVERFLOW
:
11995 case BUILT_IN_UADDL_OVERFLOW
:
11996 case BUILT_IN_UADDLL_OVERFLOW
:
11997 opcode
= PLUS_EXPR
;
11998 ifn
= IFN_ADD_OVERFLOW
;
12000 case BUILT_IN_SUB_OVERFLOW_P
:
12003 case BUILT_IN_SUB_OVERFLOW
:
12004 case BUILT_IN_SSUB_OVERFLOW
:
12005 case BUILT_IN_SSUBL_OVERFLOW
:
12006 case BUILT_IN_SSUBLL_OVERFLOW
:
12007 case BUILT_IN_USUB_OVERFLOW
:
12008 case BUILT_IN_USUBL_OVERFLOW
:
12009 case BUILT_IN_USUBLL_OVERFLOW
:
12010 opcode
= MINUS_EXPR
;
12011 ifn
= IFN_SUB_OVERFLOW
;
12013 case BUILT_IN_MUL_OVERFLOW_P
:
12016 case BUILT_IN_MUL_OVERFLOW
:
12017 case BUILT_IN_SMUL_OVERFLOW
:
12018 case BUILT_IN_SMULL_OVERFLOW
:
12019 case BUILT_IN_SMULLL_OVERFLOW
:
12020 case BUILT_IN_UMUL_OVERFLOW
:
12021 case BUILT_IN_UMULL_OVERFLOW
:
12022 case BUILT_IN_UMULLL_OVERFLOW
:
12023 opcode
= MULT_EXPR
;
12024 ifn
= IFN_MUL_OVERFLOW
;
12027 gcc_unreachable ();
12030 /* For the "generic" overloads, the first two arguments can have different
12031 types and the last argument determines the target type to use to check
12032 for overflow. The arguments of the other overloads all have the same
12034 tree type
= ovf_only
? TREE_TYPE (arg2
) : TREE_TYPE (TREE_TYPE (arg2
));
12036 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
12037 arguments are constant, attempt to fold the built-in call into a constant
12038 expression indicating whether or not it detected an overflow. */
12040 && TREE_CODE (arg0
) == INTEGER_CST
12041 && TREE_CODE (arg1
) == INTEGER_CST
)
12042 /* Perform the computation in the target type and check for overflow. */
12043 return omit_one_operand_loc (loc
, boolean_type_node
,
12044 arith_overflowed_p (opcode
, type
, arg0
, arg1
)
12045 ? boolean_true_node
: boolean_false_node
,
12048 tree intres
, ovfres
;
12049 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
12051 intres
= fold_binary_loc (loc
, opcode
, type
,
12052 fold_convert_loc (loc
, type
, arg0
),
12053 fold_convert_loc (loc
, type
, arg1
));
12054 if (TREE_OVERFLOW (intres
))
12055 intres
= drop_tree_overflow (intres
);
12056 ovfres
= (arith_overflowed_p (opcode
, type
, arg0
, arg1
)
12057 ? boolean_true_node
: boolean_false_node
);
12061 tree ctype
= build_complex_type (type
);
12062 tree call
= build_call_expr_internal_loc (loc
, ifn
, ctype
, 2,
12064 tree tgt
= save_expr (call
);
12065 intres
= build1_loc (loc
, REALPART_EXPR
, type
, tgt
);
12066 ovfres
= build1_loc (loc
, IMAGPART_EXPR
, type
, tgt
);
12067 ovfres
= fold_convert_loc (loc
, boolean_type_node
, ovfres
);
12071 return omit_one_operand_loc (loc
, boolean_type_node
, ovfres
, arg2
);
12073 tree mem_arg2
= build_fold_indirect_ref_loc (loc
, arg2
);
12075 = fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, mem_arg2
, intres
);
12076 return build2_loc (loc
, COMPOUND_EXPR
, boolean_type_node
, store
, ovfres
);
12079 /* Fold a call to __builtin_FILE to a constant string. */
12082 fold_builtin_FILE (location_t loc
)
12084 if (const char *fname
= LOCATION_FILE (loc
))
12086 /* The documentation says this builtin is equivalent to the preprocessor
12087 __FILE__ macro so it appears appropriate to use the same file prefix
12089 fname
= remap_macro_filename (fname
);
12090 return build_string_literal (strlen (fname
) + 1, fname
);
12093 return build_string_literal (1, "");
12096 /* Fold a call to __builtin_FUNCTION to a constant string. */
12099 fold_builtin_FUNCTION ()
12101 const char *name
= "";
12103 if (current_function_decl
)
12104 name
= lang_hooks
.decl_printable_name (current_function_decl
, 0);
12106 return build_string_literal (strlen (name
) + 1, name
);
12109 /* Fold a call to __builtin_LINE to an integer constant. */
12112 fold_builtin_LINE (location_t loc
, tree type
)
12114 return build_int_cst (type
, LOCATION_LINE (loc
));
12117 /* Fold a call to built-in function FNDECL with 0 arguments.
12118 This function returns NULL_TREE if no simplification was possible. */
12121 fold_builtin_0 (location_t loc
, tree fndecl
)
12123 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
12124 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
12127 case BUILT_IN_FILE
:
12128 return fold_builtin_FILE (loc
);
12130 case BUILT_IN_FUNCTION
:
12131 return fold_builtin_FUNCTION ();
12133 case BUILT_IN_LINE
:
12134 return fold_builtin_LINE (loc
, type
);
12136 CASE_FLT_FN (BUILT_IN_INF
):
12137 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF
):
12138 case BUILT_IN_INFD32
:
12139 case BUILT_IN_INFD64
:
12140 case BUILT_IN_INFD128
:
12141 return fold_builtin_inf (loc
, type
, true);
12143 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
12144 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL
):
12145 return fold_builtin_inf (loc
, type
, false);
12147 case BUILT_IN_CLASSIFY_TYPE
:
12148 return fold_builtin_classify_type (NULL_TREE
);
12156 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
12157 This function returns NULL_TREE if no simplification was possible. */
12160 fold_builtin_1 (location_t loc
, tree expr
, tree fndecl
, tree arg0
)
12162 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
12163 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
12165 if (TREE_CODE (arg0
) == ERROR_MARK
)
12168 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
, arg0
))
12173 case BUILT_IN_CONSTANT_P
:
12175 tree val
= fold_builtin_constant_p (arg0
);
12177 /* Gimplification will pull the CALL_EXPR for the builtin out of
12178 an if condition. When not optimizing, we'll not CSE it back.
12179 To avoid link error types of regressions, return false now. */
12180 if (!val
&& !optimize
)
12181 val
= integer_zero_node
;
12186 case BUILT_IN_CLASSIFY_TYPE
:
12187 return fold_builtin_classify_type (arg0
);
12189 case BUILT_IN_STRLEN
:
12190 return fold_builtin_strlen (loc
, expr
, type
, arg0
);
12192 CASE_FLT_FN (BUILT_IN_FABS
):
12193 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
12194 case BUILT_IN_FABSD32
:
12195 case BUILT_IN_FABSD64
:
12196 case BUILT_IN_FABSD128
:
12197 return fold_builtin_fabs (loc
, arg0
, type
);
12200 case BUILT_IN_LABS
:
12201 case BUILT_IN_LLABS
:
12202 case BUILT_IN_IMAXABS
:
12203 return fold_builtin_abs (loc
, arg0
, type
);
12205 CASE_FLT_FN (BUILT_IN_CONJ
):
12206 if (validate_arg (arg0
, COMPLEX_TYPE
)
12207 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
12208 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
12211 CASE_FLT_FN (BUILT_IN_CREAL
):
12212 if (validate_arg (arg0
, COMPLEX_TYPE
)
12213 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
12214 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));
12217 CASE_FLT_FN (BUILT_IN_CIMAG
):
12218 if (validate_arg (arg0
, COMPLEX_TYPE
)
12219 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
12220 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
12223 CASE_FLT_FN (BUILT_IN_CARG
):
12224 return fold_builtin_carg (loc
, arg0
, type
);
12226 case BUILT_IN_ISASCII
:
12227 return fold_builtin_isascii (loc
, arg0
);
12229 case BUILT_IN_TOASCII
:
12230 return fold_builtin_toascii (loc
, arg0
);
12232 case BUILT_IN_ISDIGIT
:
12233 return fold_builtin_isdigit (loc
, arg0
);
12235 CASE_FLT_FN (BUILT_IN_FINITE
):
12236 case BUILT_IN_FINITED32
:
12237 case BUILT_IN_FINITED64
:
12238 case BUILT_IN_FINITED128
:
12239 case BUILT_IN_ISFINITE
:
12241 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
12244 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
12247 CASE_FLT_FN (BUILT_IN_ISINF
):
12248 case BUILT_IN_ISINFD32
:
12249 case BUILT_IN_ISINFD64
:
12250 case BUILT_IN_ISINFD128
:
12252 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
12255 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
12258 case BUILT_IN_ISNORMAL
:
12259 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
12261 case BUILT_IN_ISINF_SIGN
:
12262 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
12264 CASE_FLT_FN (BUILT_IN_ISNAN
):
12265 case BUILT_IN_ISNAND32
:
12266 case BUILT_IN_ISNAND64
:
12267 case BUILT_IN_ISNAND128
:
12268 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
12270 case BUILT_IN_FREE
:
12271 if (integer_zerop (arg0
))
12272 return build_empty_stmt (loc
);
12283 /* Folds a call EXPR (which may be null) to built-in function FNDECL
12284 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
12285 if no simplification was possible. */
12288 fold_builtin_2 (location_t loc
, tree expr
, tree fndecl
, tree arg0
, tree arg1
)
12290 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
12291 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
12293 if (TREE_CODE (arg0
) == ERROR_MARK
12294 || TREE_CODE (arg1
) == ERROR_MARK
)
12297 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
, arg0
, arg1
))
12302 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
12303 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
12304 if (validate_arg (arg0
, REAL_TYPE
)
12305 && validate_arg (arg1
, POINTER_TYPE
))
12306 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
12309 CASE_FLT_FN (BUILT_IN_FREXP
):
12310 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
12312 CASE_FLT_FN (BUILT_IN_MODF
):
12313 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
12315 case BUILT_IN_STRSPN
:
12316 return fold_builtin_strspn (loc
, expr
, arg0
, arg1
);
12318 case BUILT_IN_STRCSPN
:
12319 return fold_builtin_strcspn (loc
, expr
, arg0
, arg1
);
12321 case BUILT_IN_STRPBRK
:
12322 return fold_builtin_strpbrk (loc
, expr
, arg0
, arg1
, type
);
12324 case BUILT_IN_EXPECT
:
12325 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
, NULL_TREE
);
12327 case BUILT_IN_ISGREATER
:
12328 return fold_builtin_unordered_cmp (loc
, fndecl
,
12329 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
12330 case BUILT_IN_ISGREATEREQUAL
:
12331 return fold_builtin_unordered_cmp (loc
, fndecl
,
12332 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
12333 case BUILT_IN_ISLESS
:
12334 return fold_builtin_unordered_cmp (loc
, fndecl
,
12335 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
12336 case BUILT_IN_ISLESSEQUAL
:
12337 return fold_builtin_unordered_cmp (loc
, fndecl
,
12338 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
12339 case BUILT_IN_ISLESSGREATER
:
12340 return fold_builtin_unordered_cmp (loc
, fndecl
,
12341 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
12342 case BUILT_IN_ISUNORDERED
:
12343 return fold_builtin_unordered_cmp (loc
, fndecl
,
12344 arg0
, arg1
, UNORDERED_EXPR
,
12347 /* We do the folding for va_start in the expander. */
12348 case BUILT_IN_VA_START
:
12351 case BUILT_IN_OBJECT_SIZE
:
12352 return fold_builtin_object_size (arg0
, arg1
);
12354 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
12355 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
12357 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
12358 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
12366 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
12368 This function returns NULL_TREE if no simplification was possible. */
12371 fold_builtin_3 (location_t loc
, tree fndecl
,
12372 tree arg0
, tree arg1
, tree arg2
)
12374 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
12375 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
12377 if (TREE_CODE (arg0
) == ERROR_MARK
12378 || TREE_CODE (arg1
) == ERROR_MARK
12379 || TREE_CODE (arg2
) == ERROR_MARK
)
12382 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
,
12389 CASE_FLT_FN (BUILT_IN_SINCOS
):
12390 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
12392 CASE_FLT_FN (BUILT_IN_REMQUO
):
12393 if (validate_arg (arg0
, REAL_TYPE
)
12394 && validate_arg (arg1
, REAL_TYPE
)
12395 && validate_arg (arg2
, POINTER_TYPE
))
12396 return do_mpfr_remquo (arg0
, arg1
, arg2
);
12399 case BUILT_IN_MEMCMP
:
12400 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);
12402 case BUILT_IN_EXPECT
:
12403 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
, NULL_TREE
);
12405 case BUILT_IN_EXPECT_WITH_PROBABILITY
:
12406 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
, arg2
);
12408 case BUILT_IN_ADD_OVERFLOW
:
12409 case BUILT_IN_SUB_OVERFLOW
:
12410 case BUILT_IN_MUL_OVERFLOW
:
12411 case BUILT_IN_ADD_OVERFLOW_P
:
12412 case BUILT_IN_SUB_OVERFLOW_P
:
12413 case BUILT_IN_MUL_OVERFLOW_P
:
12414 case BUILT_IN_SADD_OVERFLOW
:
12415 case BUILT_IN_SADDL_OVERFLOW
:
12416 case BUILT_IN_SADDLL_OVERFLOW
:
12417 case BUILT_IN_SSUB_OVERFLOW
:
12418 case BUILT_IN_SSUBL_OVERFLOW
:
12419 case BUILT_IN_SSUBLL_OVERFLOW
:
12420 case BUILT_IN_SMUL_OVERFLOW
:
12421 case BUILT_IN_SMULL_OVERFLOW
:
12422 case BUILT_IN_SMULLL_OVERFLOW
:
12423 case BUILT_IN_UADD_OVERFLOW
:
12424 case BUILT_IN_UADDL_OVERFLOW
:
12425 case BUILT_IN_UADDLL_OVERFLOW
:
12426 case BUILT_IN_USUB_OVERFLOW
:
12427 case BUILT_IN_USUBL_OVERFLOW
:
12428 case BUILT_IN_USUBLL_OVERFLOW
:
12429 case BUILT_IN_UMUL_OVERFLOW
:
12430 case BUILT_IN_UMULL_OVERFLOW
:
12431 case BUILT_IN_UMULLL_OVERFLOW
:
12432 return fold_builtin_arith_overflow (loc
, fcode
, arg0
, arg1
, arg2
);
12440 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
12441 ARGS is an array of NARGS arguments. IGNORE is true if the result
12442 of the function call is ignored. This function returns NULL_TREE
12443 if no simplification was possible. */
12446 fold_builtin_n (location_t loc
, tree expr
, tree fndecl
, tree
*args
,
12449 tree ret
= NULL_TREE
;
12454 ret
= fold_builtin_0 (loc
, fndecl
);
12457 ret
= fold_builtin_1 (loc
, expr
, fndecl
, args
[0]);
12460 ret
= fold_builtin_2 (loc
, expr
, fndecl
, args
[0], args
[1]);
12463 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2]);
12466 ret
= fold_builtin_varargs (loc
, fndecl
, args
, nargs
);
12471 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
12472 SET_EXPR_LOCATION (ret
, loc
);
12478 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
12479 list ARGS along with N new arguments in NEWARGS. SKIP is the number
12480 of arguments in ARGS to be omitted. OLDNARGS is the number of
12481 elements in ARGS. */
12484 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
12485 int skip
, tree fndecl
, int n
, va_list newargs
)
12487 int nargs
= oldnargs
- skip
+ n
;
12494 buffer
= XALLOCAVEC (tree
, nargs
);
12495 for (i
= 0; i
< n
; i
++)
12496 buffer
[i
] = va_arg (newargs
, tree
);
12497 for (j
= skip
; j
< oldnargs
; j
++, i
++)
12498 buffer
[i
] = args
[j
];
12501 buffer
= args
+ skip
;
12503 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
12506 /* Return true if FNDECL shouldn't be folded right now.
12507 If a built-in function has an inline attribute always_inline
12508 wrapper, defer folding it after always_inline functions have
12509 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
12510 might not be performed. */
12513 avoid_folding_inline_builtin (tree fndecl
)
12515 return (DECL_DECLARED_INLINE_P (fndecl
)
12516 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
12518 && !cfun
->always_inline_functions_inlined
12519 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
12522 /* A wrapper function for builtin folding that prevents warnings for
12523 "statement without effect" and the like, caused by removing the
12524 call node earlier than the warning is generated. */
12527 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
12529 tree ret
= NULL_TREE
;
12530 tree fndecl
= get_callee_fndecl (exp
);
12531 if (fndecl
&& fndecl_built_in_p (fndecl
)
12532 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
12533 yet. Defer folding until we see all the arguments
12534 (after inlining). */
12535 && !CALL_EXPR_VA_ARG_PACK (exp
))
12537 int nargs
= call_expr_nargs (exp
);
12539 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
12540 instead last argument is __builtin_va_arg_pack (). Defer folding
12541 even in that case, until arguments are finalized. */
12542 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
12544 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
12545 if (fndecl2
&& fndecl_built_in_p (fndecl2
, BUILT_IN_VA_ARG_PACK
))
12549 if (avoid_folding_inline_builtin (fndecl
))
12552 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
12553 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
12554 CALL_EXPR_ARGP (exp
), ignore
);
12557 tree
*args
= CALL_EXPR_ARGP (exp
);
12558 ret
= fold_builtin_n (loc
, exp
, fndecl
, args
, nargs
, ignore
);
12566 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
12567 N arguments are passed in the array ARGARRAY. Return a folded
12568 expression or NULL_TREE if no simplification was possible. */
12571 fold_builtin_call_array (location_t loc
, tree
,
12576 if (TREE_CODE (fn
) != ADDR_EXPR
)
12579 tree fndecl
= TREE_OPERAND (fn
, 0);
12580 if (TREE_CODE (fndecl
) == FUNCTION_DECL
12581 && fndecl_built_in_p (fndecl
))
12583 /* If last argument is __builtin_va_arg_pack (), arguments to this
12584 function are not finalized yet. Defer folding until they are. */
12585 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
12587 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
12588 if (fndecl2
&& fndecl_built_in_p (fndecl2
, BUILT_IN_VA_ARG_PACK
))
12591 if (avoid_folding_inline_builtin (fndecl
))
12593 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
12594 return targetm
.fold_builtin (fndecl
, n
, argarray
, false);
12596 return fold_builtin_n (loc
, NULL_TREE
, fndecl
, argarray
, n
, false);
12602 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
12603 along with N new arguments specified as the "..." parameters. SKIP
12604 is the number of arguments in EXP to be omitted. This function is used
12605 to do varargs-to-varargs transformations. */
12608 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
12614 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
12615 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
12621 /* Validate a single argument ARG against a tree code CODE representing
12622 a type. Return true when argument is valid. */
12625 validate_arg (const_tree arg
, enum tree_code code
)
12629 else if (code
== POINTER_TYPE
)
12630 return POINTER_TYPE_P (TREE_TYPE (arg
));
12631 else if (code
== INTEGER_TYPE
)
12632 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
12633 return code
== TREE_CODE (TREE_TYPE (arg
));
12636 /* This function validates the types of a function call argument list
12637 against a specified list of tree_codes. If the last specifier is a 0,
12638 that represents an ellipses, otherwise the last specifier must be a
12641 This is the GIMPLE version of validate_arglist. Eventually we want to
12642 completely convert builtins.c to work from GIMPLEs and the tree based
12643 validate_arglist will then be removed. */
12646 validate_gimple_arglist (const gcall
*call
, ...)
12648 enum tree_code code
;
12654 va_start (ap
, call
);
12659 code
= (enum tree_code
) va_arg (ap
, int);
12663 /* This signifies an ellipses, any further arguments are all ok. */
12667 /* This signifies an endlink, if no arguments remain, return
12668 true, otherwise return false. */
12669 res
= (i
== gimple_call_num_args (call
));
12672 /* If no parameters remain or the parameter's code does not
12673 match the specified code, return false. Otherwise continue
12674 checking any remaining arguments. */
12675 arg
= gimple_call_arg (call
, i
++);
12676 if (!validate_arg (arg
, code
))
12683 /* We need gotos here since we can only have one VA_CLOSE in a
12691 /* Default target-specific builtin expander that does nothing. */
12694 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
12695 rtx target ATTRIBUTE_UNUSED
,
12696 rtx subtarget ATTRIBUTE_UNUSED
,
12697 machine_mode mode ATTRIBUTE_UNUSED
,
12698 int ignore ATTRIBUTE_UNUSED
)
12703 /* Returns true is EXP represents data that would potentially reside
12704 in a readonly section. */
12707 readonly_data_expr (tree exp
)
12711 if (TREE_CODE (exp
) != ADDR_EXPR
)
12714 exp
= get_base_address (TREE_OPERAND (exp
, 0));
12718 /* Make sure we call decl_readonly_section only for trees it
12719 can handle (since it returns true for everything it doesn't
12721 if (TREE_CODE (exp
) == STRING_CST
12722 || TREE_CODE (exp
) == CONSTRUCTOR
12723 || (VAR_P (exp
) && TREE_STATIC (exp
)))
12724 return decl_readonly_section (exp
, 0);
12729 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
12730 to the call, and TYPE is its return type.
12732 Return NULL_TREE if no simplification was possible, otherwise return the
12733 simplified form of the call as a tree.
12735 The simplified form may be a constant or other expression which
12736 computes the same value, but in a more efficient manner (including
12737 calls to other builtin functions).
12739 The call may contain arguments which need to be evaluated, but
12740 which are not useful to determine the result of the call. In
12741 this case we return a chain of COMPOUND_EXPRs. The LHS of each
12742 COMPOUND_EXPR will be an argument which must be evaluated.
12743 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
12744 COMPOUND_EXPR in the chain will contain the tree for the simplified
12745 form of the builtin function call. */
12748 fold_builtin_strpbrk (location_t loc
, tree
, tree s1
, tree s2
, tree type
)
12750 if (!validate_arg (s1
, POINTER_TYPE
)
12751 || !validate_arg (s2
, POINTER_TYPE
))
12755 const char *p1
, *p2
;
12757 p2
= c_getstr (s2
);
12761 p1
= c_getstr (s1
);
12764 const char *r
= strpbrk (p1
, p2
);
12768 return build_int_cst (TREE_TYPE (s1
), 0);
12770 /* Return an offset into the constant string argument. */
12771 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
12772 return fold_convert_loc (loc
, type
, tem
);
12776 /* strpbrk(x, "") == NULL.
12777 Evaluate and ignore s1 in case it had side-effects. */
12778 return omit_one_operand_loc (loc
, type
, integer_zero_node
, s1
);
12781 return NULL_TREE
; /* Really call strpbrk. */
12783 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
12787 /* New argument list transforming strpbrk(s1, s2) to
12788 strchr(s1, s2[0]). */
12789 return build_call_expr_loc (loc
, fn
, 2, s1
,
12790 build_int_cst (integer_type_node
, p2
[0]));
12793 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
12796 Return NULL_TREE if no simplification was possible, otherwise return the
12797 simplified form of the call as a tree.
12799 The simplified form may be a constant or other expression which
12800 computes the same value, but in a more efficient manner (including
12801 calls to other builtin functions).
12803 The call may contain arguments which need to be evaluated, but
12804 which are not useful to determine the result of the call. In
12805 this case we return a chain of COMPOUND_EXPRs. The LHS of each
12806 COMPOUND_EXPR will be an argument which must be evaluated.
12807 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
12808 COMPOUND_EXPR in the chain will contain the tree for the simplified
12809 form of the builtin function call. */
12812 fold_builtin_strspn (location_t loc
, tree expr
, tree s1
, tree s2
)
12814 if (!validate_arg (s1
, POINTER_TYPE
)
12815 || !validate_arg (s2
, POINTER_TYPE
))
12818 if (!check_nul_terminated_array (expr
, s1
)
12819 || !check_nul_terminated_array (expr
, s2
))
12822 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
12824 /* If either argument is "", return NULL_TREE. */
12825 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
12826 /* Evaluate and ignore both arguments in case either one has
12828 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
12833 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
12836 Return NULL_TREE if no simplification was possible, otherwise return the
12837 simplified form of the call as a tree.
12839 The simplified form may be a constant or other expression which
12840 computes the same value, but in a more efficient manner (including
12841 calls to other builtin functions).
12843 The call may contain arguments which need to be evaluated, but
12844 which are not useful to determine the result of the call. In
12845 this case we return a chain of COMPOUND_EXPRs. The LHS of each
12846 COMPOUND_EXPR will be an argument which must be evaluated.
12847 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
12848 COMPOUND_EXPR in the chain will contain the tree for the simplified
12849 form of the builtin function call. */
12852 fold_builtin_strcspn (location_t loc
, tree expr
, tree s1
, tree s2
)
12854 if (!validate_arg (s1
, POINTER_TYPE
)
12855 || !validate_arg (s2
, POINTER_TYPE
))
12858 if (!check_nul_terminated_array (expr
, s1
)
12859 || !check_nul_terminated_array (expr
, s2
))
12862 /* If the first argument is "", return NULL_TREE. */
12863 const char *p1
= c_getstr (s1
);
12864 if (p1
&& *p1
== '\0')
12866 /* Evaluate and ignore argument s2 in case it has
12868 return omit_one_operand_loc (loc
, size_type_node
,
12869 size_zero_node
, s2
);
12872 /* If the second argument is "", return __builtin_strlen(s1). */
12873 const char *p2
= c_getstr (s2
);
12874 if (p2
&& *p2
== '\0')
12876 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
12878 /* If the replacement _DECL isn't initialized, don't do the
12883 return build_call_expr_loc (loc
, fn
, 1, s1
);
12888 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12889 produced. False otherwise. This is done so that we don't output the error
12890 or warning twice or three times. */
12893 fold_builtin_next_arg (tree exp
, bool va_start_p
)
12895 tree fntype
= TREE_TYPE (current_function_decl
);
12896 int nargs
= call_expr_nargs (exp
);
12898 /* There is good chance the current input_location points inside the
12899 definition of the va_start macro (perhaps on the token for
12900 builtin) in a system header, so warnings will not be emitted.
12901 Use the location in real source code. */
12902 location_t current_location
=
12903 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
12906 if (!stdarg_p (fntype
))
12908 error ("%<va_start%> used in function with fixed arguments");
12914 if (va_start_p
&& (nargs
!= 2))
12916 error ("wrong number of arguments to function %<va_start%>");
12919 arg
= CALL_EXPR_ARG (exp
, 1);
12921 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12922 when we checked the arguments and if needed issued a warning. */
12927 /* Evidently an out of date version of <stdarg.h>; can't validate
12928 va_start's second argument, but can still work as intended. */
12929 warning_at (current_location
,
12931 "%<__builtin_next_arg%> called without an argument");
12934 else if (nargs
> 1)
12936 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12939 arg
= CALL_EXPR_ARG (exp
, 0);
12942 if (TREE_CODE (arg
) == SSA_NAME
12943 && SSA_NAME_VAR (arg
))
12944 arg
= SSA_NAME_VAR (arg
);
12946 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12947 or __builtin_next_arg (0) the first time we see it, after checking
12948 the arguments and if needed issuing a warning. */
12949 if (!integer_zerop (arg
))
12951 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
12953 /* Strip off all nops for the sake of the comparison. This
12954 is not quite the same as STRIP_NOPS. It does more.
12955 We must also strip off INDIRECT_EXPR for C++ reference
12957 while (CONVERT_EXPR_P (arg
)
12958 || TREE_CODE (arg
) == INDIRECT_REF
)
12959 arg
= TREE_OPERAND (arg
, 0);
12960 if (arg
!= last_parm
)
12962 /* FIXME: Sometimes with the tree optimizers we can get the
12963 not the last argument even though the user used the last
12964 argument. We just warn and set the arg to be the last
12965 argument so that we will get wrong-code because of
12967 warning_at (current_location
,
12969 "second parameter of %<va_start%> not last named argument");
12972 /* Undefined by C99 7.15.1.4p4 (va_start):
12973 "If the parameter parmN is declared with the register storage
12974 class, with a function or array type, or with a type that is
12975 not compatible with the type that results after application of
12976 the default argument promotions, the behavior is undefined."
12978 else if (DECL_REGISTER (arg
))
12980 warning_at (current_location
,
12982 "undefined behavior when second parameter of "
12983 "%<va_start%> is declared with %<register%> storage");
12986 /* We want to verify the second parameter just once before the tree
12987 optimizers are run and then avoid keeping it in the tree,
12988 as otherwise we could warn even for correct code like:
12989 void foo (int i, ...)
12990 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12992 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
12994 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
13000 /* Expand a call EXP to __builtin_object_size. */
13003 expand_builtin_object_size (tree exp
)
13006 int object_size_type
;
13007 tree fndecl
= get_callee_fndecl (exp
);
13009 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
13011 error ("first argument of %qD must be a pointer, second integer constant",
13013 expand_builtin_trap ();
13017 ost
= CALL_EXPR_ARG (exp
, 1);
13020 if (TREE_CODE (ost
) != INTEGER_CST
13021 || tree_int_cst_sgn (ost
) < 0
13022 || compare_tree_int (ost
, 3) > 0)
13024 error ("last argument of %qD is not integer constant between 0 and 3",
13026 expand_builtin_trap ();
13030 object_size_type
= tree_to_shwi (ost
);
13032 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
13035 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
13036 FCODE is the BUILT_IN_* to use.
13037 Return NULL_RTX if we failed; the caller should emit a normal call,
13038 otherwise try to get the result in TARGET, if convenient (and in
13039 mode MODE if that's convenient). */
13042 expand_builtin_memory_chk (tree exp
, rtx target
, machine_mode mode
,
13043 enum built_in_function fcode
)
13045 if (!validate_arglist (exp
,
13047 fcode
== BUILT_IN_MEMSET_CHK
13048 ? INTEGER_TYPE
: POINTER_TYPE
,
13049 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
13052 tree dest
= CALL_EXPR_ARG (exp
, 0);
13053 tree src
= CALL_EXPR_ARG (exp
, 1);
13054 tree len
= CALL_EXPR_ARG (exp
, 2);
13055 tree size
= CALL_EXPR_ARG (exp
, 3);
13057 /* FIXME: Set access mode to write only for memset et al. */
13058 bool sizes_ok
= check_access (exp
, len
, /*maxread=*/NULL_TREE
,
13059 /*srcstr=*/NULL_TREE
, size
, access_read_write
);
13061 if (!tree_fits_uhwi_p (size
))
13064 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
13066 /* Avoid transforming the checking call to an ordinary one when
13067 an overflow has been detected or when the call couldn't be
13068 validated because the size is not constant. */
13069 if (!sizes_ok
&& !integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
13072 tree fn
= NULL_TREE
;
13073 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
13074 mem{cpy,pcpy,move,set} is available. */
13077 case BUILT_IN_MEMCPY_CHK
:
13078 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
13080 case BUILT_IN_MEMPCPY_CHK
:
13081 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
13083 case BUILT_IN_MEMMOVE_CHK
:
13084 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
13086 case BUILT_IN_MEMSET_CHK
:
13087 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
13096 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
13097 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
13098 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
13099 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
13101 else if (fcode
== BUILT_IN_MEMSET_CHK
)
13105 unsigned int dest_align
= get_pointer_alignment (dest
);
13107 /* If DEST is not a pointer type, call the normal function. */
13108 if (dest_align
== 0)
13111 /* If SRC and DEST are the same (and not volatile), do nothing. */
13112 if (operand_equal_p (src
, dest
, 0))
13116 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
13118 /* Evaluate and ignore LEN in case it has side-effects. */
13119 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
13120 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
13123 expr
= fold_build_pointer_plus (dest
, len
);
13124 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
13127 /* __memmove_chk special case. */
13128 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
13130 unsigned int src_align
= get_pointer_alignment (src
);
13132 if (src_align
== 0)
13135 /* If src is categorized for a readonly section we can use
13136 normal __memcpy_chk. */
13137 if (readonly_data_expr (src
))
13139 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
13142 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
13143 dest
, src
, len
, size
);
13144 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
13145 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
13146 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
13153 /* Emit warning if a buffer overflow is detected at compile time. */
13156 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
13158 /* The source string. */
13159 tree srcstr
= NULL_TREE
;
13160 /* The size of the destination object returned by __builtin_object_size. */
13161 tree objsize
= NULL_TREE
;
13162 /* The string that is being concatenated with (as in __strcat_chk)
13163 or null if it isn't. */
13164 tree catstr
= NULL_TREE
;
13165 /* The maximum length of the source sequence in a bounded operation
13166 (such as __strncat_chk) or null if the operation isn't bounded
13167 (such as __strcat_chk). */
13168 tree maxread
= NULL_TREE
;
13169 /* The exact size of the access (such as in __strncpy_chk). */
13170 tree size
= NULL_TREE
;
13171 /* The access by the function that's checked. Except for snprintf
13172 both writing and reading is checked. */
13173 access_mode mode
= access_read_write
;
13177 case BUILT_IN_STRCPY_CHK
:
13178 case BUILT_IN_STPCPY_CHK
:
13179 srcstr
= CALL_EXPR_ARG (exp
, 1);
13180 objsize
= CALL_EXPR_ARG (exp
, 2);
13183 case BUILT_IN_STRCAT_CHK
:
13184 /* For __strcat_chk the warning will be emitted only if overflowing
13185 by at least strlen (dest) + 1 bytes. */
13186 catstr
= CALL_EXPR_ARG (exp
, 0);
13187 srcstr
= CALL_EXPR_ARG (exp
, 1);
13188 objsize
= CALL_EXPR_ARG (exp
, 2);
13191 case BUILT_IN_STRNCAT_CHK
:
13192 catstr
= CALL_EXPR_ARG (exp
, 0);
13193 srcstr
= CALL_EXPR_ARG (exp
, 1);
13194 maxread
= CALL_EXPR_ARG (exp
, 2);
13195 objsize
= CALL_EXPR_ARG (exp
, 3);
13198 case BUILT_IN_STRNCPY_CHK
:
13199 case BUILT_IN_STPNCPY_CHK
:
13200 srcstr
= CALL_EXPR_ARG (exp
, 1);
13201 size
= CALL_EXPR_ARG (exp
, 2);
13202 objsize
= CALL_EXPR_ARG (exp
, 3);
13205 case BUILT_IN_SNPRINTF_CHK
:
13206 case BUILT_IN_VSNPRINTF_CHK
:
13207 maxread
= CALL_EXPR_ARG (exp
, 1);
13208 objsize
= CALL_EXPR_ARG (exp
, 3);
13209 /* The only checked access the write to the destination. */
13210 mode
= access_write_only
;
13213 gcc_unreachable ();
13216 if (catstr
&& maxread
)
13218 /* Check __strncat_chk. There is no way to determine the length
13219 of the string to which the source string is being appended so
13220 just warn when the length of the source string is not known. */
13221 check_strncat_sizes (exp
, objsize
);
13225 check_access (exp
, size
, maxread
, srcstr
, objsize
, mode
);
13228 /* Emit warning if a buffer overflow is detected at compile time
13229 in __sprintf_chk/__vsprintf_chk calls. */
13232 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
13234 tree size
, len
, fmt
;
13235 const char *fmt_str
;
13236 int nargs
= call_expr_nargs (exp
);
13238 /* Verify the required arguments in the original call. */
13242 size
= CALL_EXPR_ARG (exp
, 2);
13243 fmt
= CALL_EXPR_ARG (exp
, 3);
13245 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
13248 /* Check whether the format is a literal string constant. */
13249 fmt_str
= c_getstr (fmt
);
13250 if (fmt_str
== NULL
)
13253 if (!init_target_chars ())
13256 /* If the format doesn't contain % args or %%, we know its size. */
13257 if (strchr (fmt_str
, target_percent
) == 0)
13258 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
13259 /* If the format is "%s" and first ... argument is a string literal,
13261 else if (fcode
== BUILT_IN_SPRINTF_CHK
13262 && strcmp (fmt_str
, target_percent_s
) == 0)
13268 arg
= CALL_EXPR_ARG (exp
, 4);
13269 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
13272 len
= c_strlen (arg
, 1);
13273 if (!len
|| ! tree_fits_uhwi_p (len
))
13279 /* Add one for the terminating nul. */
13280 len
= fold_build2 (PLUS_EXPR
, TREE_TYPE (len
), len
, size_one_node
);
13282 check_access (exp
, /*size=*/NULL_TREE
, /*maxread=*/NULL_TREE
, len
, size
,
13283 access_write_only
);
13286 /* Return true if STMT is a call to an allocation function. Unless
13287 ALL_ALLOC is set, consider only functions that return dynmamically
13288 allocated objects. Otherwise return true even for all forms of
13289 alloca (including VLA). */
13292 fndecl_alloc_p (tree fndecl
, bool all_alloc
)
13297 /* A call to operator new isn't recognized as one to a built-in. */
13298 if (DECL_IS_OPERATOR_NEW_P (fndecl
))
13301 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
13303 switch (DECL_FUNCTION_CODE (fndecl
))
13305 case BUILT_IN_ALLOCA
:
13306 case BUILT_IN_ALLOCA_WITH_ALIGN
:
13308 case BUILT_IN_ALIGNED_ALLOC
:
13309 case BUILT_IN_CALLOC
:
13310 case BUILT_IN_GOMP_ALLOC
:
13311 case BUILT_IN_MALLOC
:
13312 case BUILT_IN_REALLOC
:
13313 case BUILT_IN_STRDUP
:
13314 case BUILT_IN_STRNDUP
:
13321 /* A function is considered an allocation function if it's declared
13322 with attribute malloc with an argument naming its associated
13323 deallocation function. */
13324 tree attrs
= DECL_ATTRIBUTES (fndecl
);
13328 for (tree allocs
= attrs
;
13329 (allocs
= lookup_attribute ("malloc", allocs
));
13330 allocs
= TREE_CHAIN (allocs
))
13332 tree args
= TREE_VALUE (allocs
);
13336 if (TREE_VALUE (args
))
13343 /* Return true if STMT is a call to an allocation function. A wrapper
13344 around fndecl_alloc_p. */
13347 gimple_call_alloc_p (gimple
*stmt
, bool all_alloc
= false)
13349 return fndecl_alloc_p (gimple_call_fndecl (stmt
), all_alloc
);
13352 /* Return the zero-based number corresponding to the argument being
13353 deallocated if STMT is a call to a deallocation function or UINT_MAX
13357 call_dealloc_argno (tree exp
)
13359 tree fndecl
= get_callee_fndecl (exp
);
13363 return fndecl_dealloc_argno (fndecl
);
13366 /* Return the zero-based number corresponding to the argument being
13367 deallocated if FNDECL is a deallocation function or UINT_MAX
13371 fndecl_dealloc_argno (tree fndecl
)
13373 /* A call to operator delete isn't recognized as one to a built-in. */
13374 if (DECL_IS_OPERATOR_DELETE_P (fndecl
))
13376 if (DECL_IS_REPLACEABLE_OPERATOR (fndecl
))
13379 /* Avoid placement delete that's not been inlined. */
13380 tree fname
= DECL_ASSEMBLER_NAME (fndecl
);
13381 if (id_equal (fname
, "_ZdlPvS_") // ordinary form
13382 || id_equal (fname
, "_ZdaPvS_")) // array form
13387 /* TODO: Handle user-defined functions with attribute malloc? Handle
13388 known non-built-ins like fopen? */
13389 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
13391 switch (DECL_FUNCTION_CODE (fndecl
))
13393 case BUILT_IN_FREE
:
13394 case BUILT_IN_REALLOC
:
13402 tree attrs
= DECL_ATTRIBUTES (fndecl
);
13406 for (tree atfree
= attrs
;
13407 (atfree
= lookup_attribute ("*dealloc", atfree
));
13408 atfree
= TREE_CHAIN (atfree
))
13410 tree alloc
= TREE_VALUE (atfree
);
13414 tree pos
= TREE_CHAIN (alloc
);
13418 pos
= TREE_VALUE (pos
);
13419 return TREE_INT_CST_LOW (pos
) - 1;
13425 /* Return true if DELC doesn't refer to an operator delete that's
13426 suitable to call with a pointer returned from the operator new
13427 described by NEWC. */
13430 new_delete_mismatch_p (const demangle_component
&newc
,
13431 const demangle_component
&delc
)
13433 if (newc
.type
!= delc
.type
)
13438 case DEMANGLE_COMPONENT_NAME
:
13440 int len
= newc
.u
.s_name
.len
;
13441 const char *news
= newc
.u
.s_name
.s
;
13442 const char *dels
= delc
.u
.s_name
.s
;
13443 if (len
!= delc
.u
.s_name
.len
|| memcmp (news
, dels
, len
))
13446 if (news
[len
] == 'n')
13448 if (news
[len
+ 1] == 'a')
13449 return dels
[len
] != 'd' || dels
[len
+ 1] != 'a';
13450 if (news
[len
+ 1] == 'w')
13451 return dels
[len
] != 'd' || dels
[len
+ 1] != 'l';
13456 case DEMANGLE_COMPONENT_OPERATOR
:
13457 /* Operator mismatches are handled above. */
13460 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
13461 if (newc
.u
.s_extended_operator
.args
!= delc
.u
.s_extended_operator
.args
)
13463 return new_delete_mismatch_p (*newc
.u
.s_extended_operator
.name
,
13464 *delc
.u
.s_extended_operator
.name
);
13466 case DEMANGLE_COMPONENT_FIXED_TYPE
:
13467 if (newc
.u
.s_fixed
.accum
!= delc
.u
.s_fixed
.accum
13468 || newc
.u
.s_fixed
.sat
!= delc
.u
.s_fixed
.sat
)
13470 return new_delete_mismatch_p (*newc
.u
.s_fixed
.length
,
13471 *delc
.u
.s_fixed
.length
);
13473 case DEMANGLE_COMPONENT_CTOR
:
13474 if (newc
.u
.s_ctor
.kind
!= delc
.u
.s_ctor
.kind
)
13476 return new_delete_mismatch_p (*newc
.u
.s_ctor
.name
,
13477 *delc
.u
.s_ctor
.name
);
13479 case DEMANGLE_COMPONENT_DTOR
:
13480 if (newc
.u
.s_dtor
.kind
!= delc
.u
.s_dtor
.kind
)
13482 return new_delete_mismatch_p (*newc
.u
.s_dtor
.name
,
13483 *delc
.u
.s_dtor
.name
);
13485 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
13487 /* The demangler API provides no better way to compare built-in
13488 types except to by comparing their demangled names. */
13490 demangle_component
*pnc
= const_cast<demangle_component
*>(&newc
);
13491 demangle_component
*pdc
= const_cast<demangle_component
*>(&delc
);
13492 char *nts
= cplus_demangle_print (0, pnc
, 16, &nsz
);
13493 char *dts
= cplus_demangle_print (0, pdc
, 16, &dsz
);
13496 bool mismatch
= strcmp (nts
, dts
);
13502 case DEMANGLE_COMPONENT_SUB_STD
:
13503 if (newc
.u
.s_string
.len
!= delc
.u
.s_string
.len
)
13505 return memcmp (newc
.u
.s_string
.string
, delc
.u
.s_string
.string
,
13506 newc
.u
.s_string
.len
);
13508 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
13509 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
13510 return newc
.u
.s_number
.number
!= delc
.u
.s_number
.number
;
13512 case DEMANGLE_COMPONENT_CHARACTER
:
13513 return newc
.u
.s_character
.character
!= delc
.u
.s_character
.character
;
13515 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
13516 case DEMANGLE_COMPONENT_LAMBDA
:
13517 if (newc
.u
.s_unary_num
.num
!= delc
.u
.s_unary_num
.num
)
13519 return new_delete_mismatch_p (*newc
.u
.s_unary_num
.sub
,
13520 *delc
.u
.s_unary_num
.sub
);
13525 if (!newc
.u
.s_binary
.left
!= !delc
.u
.s_binary
.left
)
13528 if (!newc
.u
.s_binary
.left
)
13531 if (new_delete_mismatch_p (*newc
.u
.s_binary
.left
, *delc
.u
.s_binary
.left
)
13532 || !newc
.u
.s_binary
.right
!= !delc
.u
.s_binary
.right
)
13535 if (newc
.u
.s_binary
.right
)
13536 return new_delete_mismatch_p (*newc
.u
.s_binary
.right
,
13537 *delc
.u
.s_binary
.right
);
13541 /* Return true if DELETE_DECL is an operator delete that's not suitable
13542 to call with a pointer returned fron NEW_DECL. */
13545 new_delete_mismatch_p (tree new_decl
, tree delete_decl
)
13547 tree new_name
= DECL_ASSEMBLER_NAME (new_decl
);
13548 tree delete_name
= DECL_ASSEMBLER_NAME (delete_decl
);
13550 /* valid_new_delete_pair_p() returns a conservative result (currently
13551 it only handles global operators). A true result is reliable but
13552 a false result doesn't necessarily mean the operators don't match. */
13553 if (valid_new_delete_pair_p (new_name
, delete_name
))
13556 /* For anything not handled by valid_new_delete_pair_p() such as member
13557 operators compare the individual demangled components of the mangled
13559 const char *new_str
= IDENTIFIER_POINTER (new_name
);
13560 const char *del_str
= IDENTIFIER_POINTER (delete_name
);
13562 void *np
= NULL
, *dp
= NULL
;
13563 demangle_component
*ndc
= cplus_demangle_v3_components (new_str
, 0, &np
);
13564 demangle_component
*ddc
= cplus_demangle_v3_components (del_str
, 0, &dp
);
13565 bool mismatch
= new_delete_mismatch_p (*ndc
, *ddc
);
13571 /* ALLOC_DECL and DEALLOC_DECL are pair of allocation and deallocation
13572 functions. Return true if the latter is suitable to deallocate objects
13573 allocated by calls to the former. */
13576 matching_alloc_calls_p (tree alloc_decl
, tree dealloc_decl
)
13578 /* Set to alloc_kind_t::builtin if ALLOC_DECL is associated with
13579 a built-in deallocator. */
13580 enum class alloc_kind_t
{ none
, builtin
, user
}
13581 alloc_dealloc_kind
= alloc_kind_t::none
;
13583 if (DECL_IS_OPERATOR_NEW_P (alloc_decl
))
13585 if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl
))
13586 /* Return true iff both functions are of the same array or
13587 singleton form and false otherwise. */
13588 return !new_delete_mismatch_p (alloc_decl
, dealloc_decl
);
13590 /* Return false for deallocation functions that are known not
13592 if (fndecl_built_in_p (dealloc_decl
, BUILT_IN_FREE
)
13593 || fndecl_built_in_p (dealloc_decl
, BUILT_IN_REALLOC
))
13595 /* Otherwise proceed below to check the deallocation function's
13596 "*dealloc" attributes to look for one that mentions this operator
13599 else if (fndecl_built_in_p (alloc_decl
, BUILT_IN_NORMAL
))
13601 switch (DECL_FUNCTION_CODE (alloc_decl
))
13603 case BUILT_IN_ALLOCA
:
13604 case BUILT_IN_ALLOCA_WITH_ALIGN
:
13607 case BUILT_IN_ALIGNED_ALLOC
:
13608 case BUILT_IN_CALLOC
:
13609 case BUILT_IN_GOMP_ALLOC
:
13610 case BUILT_IN_MALLOC
:
13611 case BUILT_IN_REALLOC
:
13612 case BUILT_IN_STRDUP
:
13613 case BUILT_IN_STRNDUP
:
13614 if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl
))
13617 if (fndecl_built_in_p (dealloc_decl
, BUILT_IN_FREE
)
13618 || fndecl_built_in_p (dealloc_decl
, BUILT_IN_REALLOC
))
13621 alloc_dealloc_kind
= alloc_kind_t::builtin
;
13629 /* Set if DEALLOC_DECL both allocates and deallocates. */
13630 alloc_kind_t realloc_kind
= alloc_kind_t::none
;
13632 if (fndecl_built_in_p (dealloc_decl
, BUILT_IN_NORMAL
))
13634 built_in_function dealloc_code
= DECL_FUNCTION_CODE (dealloc_decl
);
13635 if (dealloc_code
== BUILT_IN_REALLOC
)
13636 realloc_kind
= alloc_kind_t::builtin
;
13638 for (tree amats
= DECL_ATTRIBUTES (alloc_decl
);
13639 (amats
= lookup_attribute ("malloc", amats
));
13640 amats
= TREE_CHAIN (amats
))
13642 tree args
= TREE_VALUE (amats
);
13646 tree fndecl
= TREE_VALUE (args
);
13647 if (!fndecl
|| !DECL_P (fndecl
))
13650 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
13651 && dealloc_code
== DECL_FUNCTION_CODE (fndecl
))
13656 const bool alloc_builtin
= fndecl_built_in_p (alloc_decl
, BUILT_IN_NORMAL
);
13657 alloc_kind_t realloc_dealloc_kind
= alloc_kind_t::none
;
13659 /* If DEALLOC_DECL has an internal "*dealloc" attribute scan the list
13660 of its associated allocation functions for ALLOC_DECL.
13661 If the corresponding ALLOC_DECL is found they're a matching pair,
13662 otherwise they're not.
13663 With DDATS set to the Deallocator's *Dealloc ATtributes... */
13664 for (tree ddats
= DECL_ATTRIBUTES (dealloc_decl
);
13665 (ddats
= lookup_attribute ("*dealloc", ddats
));
13666 ddats
= TREE_CHAIN (ddats
))
13668 tree args
= TREE_VALUE (ddats
);
13672 tree alloc
= TREE_VALUE (args
);
13676 if (alloc
== DECL_NAME (dealloc_decl
))
13677 realloc_kind
= alloc_kind_t::user
;
13679 if (DECL_P (alloc
))
13681 gcc_checking_assert (fndecl_built_in_p (alloc
, BUILT_IN_NORMAL
));
13683 switch (DECL_FUNCTION_CODE (alloc
))
13685 case BUILT_IN_ALIGNED_ALLOC
:
13686 case BUILT_IN_CALLOC
:
13687 case BUILT_IN_GOMP_ALLOC
:
13688 case BUILT_IN_MALLOC
:
13689 case BUILT_IN_REALLOC
:
13690 case BUILT_IN_STRDUP
:
13691 case BUILT_IN_STRNDUP
:
13692 realloc_dealloc_kind
= alloc_kind_t::builtin
;
13698 if (!alloc_builtin
)
13701 if (DECL_FUNCTION_CODE (alloc
) != DECL_FUNCTION_CODE (alloc_decl
))
13707 if (alloc
== DECL_NAME (alloc_decl
))
13711 if (realloc_kind
== alloc_kind_t::none
)
13714 hash_set
<tree
> common_deallocs
;
13715 /* Special handling for deallocators. Iterate over both the allocator's
13716 and the reallocator's associated deallocator functions looking for
13717 the first one in common. If one is found, the de/reallocator is
13718 a match for the allocator even though the latter isn't directly
13719 associated with the former. This simplifies declarations in system
13721 With AMATS set to the Allocator's Malloc ATtributes,
13722 and RMATS set to Reallocator's Malloc ATtributes... */
13723 for (tree amats
= DECL_ATTRIBUTES (alloc_decl
),
13724 rmats
= DECL_ATTRIBUTES (dealloc_decl
);
13725 (amats
= lookup_attribute ("malloc", amats
))
13726 || (rmats
= lookup_attribute ("malloc", rmats
));
13727 amats
= amats
? TREE_CHAIN (amats
) : NULL_TREE
,
13728 rmats
= rmats
? TREE_CHAIN (rmats
) : NULL_TREE
)
13730 if (tree args
= amats
? TREE_VALUE (amats
) : NULL_TREE
)
13731 if (tree adealloc
= TREE_VALUE (args
))
13733 if (DECL_P (adealloc
)
13734 && fndecl_built_in_p (adealloc
, BUILT_IN_NORMAL
))
13736 built_in_function fncode
= DECL_FUNCTION_CODE (adealloc
);
13737 if (fncode
== BUILT_IN_FREE
|| fncode
== BUILT_IN_REALLOC
)
13739 if (realloc_kind
== alloc_kind_t::builtin
)
13741 alloc_dealloc_kind
= alloc_kind_t::builtin
;
13746 common_deallocs
.add (adealloc
);
13749 if (tree args
= rmats
? TREE_VALUE (rmats
) : NULL_TREE
)
13750 if (tree ddealloc
= TREE_VALUE (args
))
13752 if (DECL_P (ddealloc
)
13753 && fndecl_built_in_p (ddealloc
, BUILT_IN_NORMAL
))
13755 built_in_function fncode
= DECL_FUNCTION_CODE (ddealloc
);
13756 if (fncode
== BUILT_IN_FREE
|| fncode
== BUILT_IN_REALLOC
)
13758 if (alloc_dealloc_kind
== alloc_kind_t::builtin
)
13760 realloc_dealloc_kind
= alloc_kind_t::builtin
;
13765 if (common_deallocs
.add (ddealloc
))
13770 /* Succeed only if ALLOC_DECL and the reallocator DEALLOC_DECL share
13771 a built-in deallocator. */
13772 return (alloc_dealloc_kind
== alloc_kind_t::builtin
13773 && realloc_dealloc_kind
== alloc_kind_t::builtin
);
13776 /* Return true if DEALLOC_DECL is a function suitable to deallocate
13777 objectes allocated by the ALLOC call. */
13780 matching_alloc_calls_p (gimple
*alloc
, tree dealloc_decl
)
13782 tree alloc_decl
= gimple_call_fndecl (alloc
);
13786 return matching_alloc_calls_p (alloc_decl
, dealloc_decl
);
13789 /* Diagnose a call EXP to deallocate a pointer referenced by AREF if it
13790 includes a nonzero offset. Such a pointer cannot refer to the beginning
13791 of an allocated object. A negative offset may refer to it only if
13792 the target pointer is unknown. */
13795 warn_dealloc_offset (location_t loc
, tree exp
, const access_ref
&aref
)
13797 if (aref
.deref
|| aref
.offrng
[0] <= 0 || aref
.offrng
[1] <= 0)
13800 tree dealloc_decl
= get_callee_fndecl (exp
);
13804 if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl
)
13805 && !DECL_IS_REPLACEABLE_OPERATOR (dealloc_decl
))
13807 /* A call to a user-defined operator delete with a pointer plus offset
13808 may be valid if it's returned from an unknown function (i.e., one
13809 that's not operator new). */
13810 if (TREE_CODE (aref
.ref
) == SSA_NAME
)
13812 gimple
*def_stmt
= SSA_NAME_DEF_STMT (aref
.ref
);
13813 if (is_gimple_call (def_stmt
))
13815 tree alloc_decl
= gimple_call_fndecl (def_stmt
);
13816 if (!alloc_decl
|| !DECL_IS_OPERATOR_NEW_P (alloc_decl
))
13824 if (wi::fits_shwi_p (aref
.offrng
[0]))
13826 if (aref
.offrng
[0] == aref
.offrng
[1]
13827 || !wi::fits_shwi_p (aref
.offrng
[1]))
13828 sprintf (offstr
, " %lli",
13829 (long long)aref
.offrng
[0].to_shwi ());
13831 sprintf (offstr
, " [%lli, %lli]",
13832 (long long)aref
.offrng
[0].to_shwi (),
13833 (long long)aref
.offrng
[1].to_shwi ());
13836 if (!warning_at (loc
, OPT_Wfree_nonheap_object
,
13837 "%qD called on pointer %qE with nonzero offset%s",
13838 dealloc_decl
, aref
.ref
, offstr
))
13841 if (DECL_P (aref
.ref
))
13842 inform (DECL_SOURCE_LOCATION (aref
.ref
), "declared here");
13843 else if (TREE_CODE (aref
.ref
) == SSA_NAME
)
13845 gimple
*def_stmt
= SSA_NAME_DEF_STMT (aref
.ref
);
13846 if (is_gimple_call (def_stmt
))
13848 location_t def_loc
= gimple_location (def_stmt
);
13849 tree alloc_decl
= gimple_call_fndecl (def_stmt
);
13852 "returned from %qD", alloc_decl
);
13853 else if (tree alloc_fntype
= gimple_call_fntype (def_stmt
))
13855 "returned from %qT", alloc_fntype
);
13857 inform (def_loc
, "obtained here");
13864 /* Issue a warning if a deallocation function such as free, realloc,
13865 or C++ operator delete is called with an argument not returned by
13866 a matching allocation function such as malloc or the corresponding
13867 form of C++ operatorn new. */
13870 maybe_emit_free_warning (tree exp
)
13872 tree fndecl
= get_callee_fndecl (exp
);
13876 unsigned argno
= call_dealloc_argno (exp
);
13877 if ((unsigned) call_expr_nargs (exp
) <= argno
)
13880 tree ptr
= CALL_EXPR_ARG (exp
, argno
);
13881 if (integer_zerop (ptr
))
13885 if (!compute_objsize (ptr
, 0, &aref
))
13888 tree ref
= aref
.ref
;
13889 if (integer_zerop (ref
))
13892 tree dealloc_decl
= get_callee_fndecl (exp
);
13893 location_t loc
= EXPR_LOCATION (exp
);
13895 if (DECL_P (ref
) || EXPR_P (ref
))
13897 /* Diagnose freeing a declared object. */
13898 if (aref
.ref_declared ()
13899 && warning_at (loc
, OPT_Wfree_nonheap_object
,
13900 "%qD called on unallocated object %qD",
13901 dealloc_decl
, ref
))
13903 loc
= (DECL_P (ref
)
13904 ? DECL_SOURCE_LOCATION (ref
)
13905 : EXPR_LOCATION (ref
));
13906 inform (loc
, "declared here");
13910 /* Diagnose freeing a pointer that includes a positive offset.
13911 Such a pointer cannot refer to the beginning of an allocated
13912 object. A negative offset may refer to it. */
13913 if (aref
.sizrng
[0] != aref
.sizrng
[1]
13914 && warn_dealloc_offset (loc
, exp
, aref
))
13917 else if (CONSTANT_CLASS_P (ref
))
13919 if (warning_at (loc
, OPT_Wfree_nonheap_object
,
13920 "%qD called on a pointer to an unallocated "
13921 "object %qE", dealloc_decl
, ref
))
13923 if (TREE_CODE (ptr
) == SSA_NAME
)
13925 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ptr
);
13926 if (is_gimple_assign (def_stmt
))
13928 location_t loc
= gimple_location (def_stmt
);
13929 inform (loc
, "assigned here");
13935 else if (TREE_CODE (ref
) == SSA_NAME
)
13937 /* Also warn if the pointer argument refers to the result
13938 of an allocation call like alloca or VLA. */
13939 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
);
13940 if (is_gimple_call (def_stmt
))
13942 bool warned
= false;
13943 if (gimple_call_alloc_p (def_stmt
))
13945 if (matching_alloc_calls_p (def_stmt
, dealloc_decl
))
13947 if (warn_dealloc_offset (loc
, exp
, aref
))
13952 tree alloc_decl
= gimple_call_fndecl (def_stmt
);
13953 const opt_code opt
=
13954 (DECL_IS_OPERATOR_NEW_P (alloc_decl
)
13955 || DECL_IS_OPERATOR_DELETE_P (dealloc_decl
)
13956 ? OPT_Wmismatched_new_delete
13957 : OPT_Wmismatched_dealloc
);
13958 warned
= warning_at (loc
, opt
,
13959 "%qD called on pointer returned "
13960 "from a mismatched allocation "
13961 "function", dealloc_decl
);
13964 else if (gimple_call_builtin_p (def_stmt
, BUILT_IN_ALLOCA
)
13965 || gimple_call_builtin_p (def_stmt
,
13966 BUILT_IN_ALLOCA_WITH_ALIGN
))
13967 warned
= warning_at (loc
, OPT_Wfree_nonheap_object
,
13968 "%qD called on pointer to "
13969 "an unallocated object",
13971 else if (warn_dealloc_offset (loc
, exp
, aref
))
13976 tree fndecl
= gimple_call_fndecl (def_stmt
);
13977 inform (gimple_location (def_stmt
),
13978 "returned from %qD", fndecl
);
13982 else if (gimple_nop_p (def_stmt
))
13984 ref
= SSA_NAME_VAR (ref
);
13985 /* Diagnose freeing a pointer that includes a positive offset. */
13986 if (TREE_CODE (ref
) == PARM_DECL
13988 && aref
.sizrng
[0] != aref
.sizrng
[1]
13989 && aref
.offrng
[0] > 0 && aref
.offrng
[1] > 0
13990 && warn_dealloc_offset (loc
, exp
, aref
))
13996 /* Fold a call to __builtin_object_size with arguments PTR and OST,
14000 fold_builtin_object_size (tree ptr
, tree ost
)
14002 unsigned HOST_WIDE_INT bytes
;
14003 int object_size_type
;
14005 if (!validate_arg (ptr
, POINTER_TYPE
)
14006 || !validate_arg (ost
, INTEGER_TYPE
))
14011 if (TREE_CODE (ost
) != INTEGER_CST
14012 || tree_int_cst_sgn (ost
) < 0
14013 || compare_tree_int (ost
, 3) > 0)
14016 object_size_type
= tree_to_shwi (ost
);
14018 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
14019 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
14020 and (size_t) 0 for types 2 and 3. */
14021 if (TREE_SIDE_EFFECTS (ptr
))
14022 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
14024 if (TREE_CODE (ptr
) == ADDR_EXPR
)
14026 compute_builtin_object_size (ptr
, object_size_type
, &bytes
);
14027 if (wi::fits_to_tree_p (bytes
, size_type_node
))
14028 return build_int_cstu (size_type_node
, bytes
);
14030 else if (TREE_CODE (ptr
) == SSA_NAME
)
14032 /* If object size is not known yet, delay folding until
14033 later. Maybe subsequent passes will help determining
14035 if (compute_builtin_object_size (ptr
, object_size_type
, &bytes
)
14036 && wi::fits_to_tree_p (bytes
, size_type_node
))
14037 return build_int_cstu (size_type_node
, bytes
);
14043 /* Builtins with folding operations that operate on "..." arguments
14044 need special handling; we need to store the arguments in a convenient
14045 data structure before attempting any folding. Fortunately there are
14046 only a few builtins that fall into this category. FNDECL is the
14047 function, EXP is the CALL_EXPR for the call. */
14050 fold_builtin_varargs (location_t loc
, tree fndecl
, tree
*args
, int nargs
)
14052 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
14053 tree ret
= NULL_TREE
;
14057 case BUILT_IN_FPCLASSIFY
:
14058 ret
= fold_builtin_fpclassify (loc
, args
, nargs
);
14066 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
14067 SET_EXPR_LOCATION (ret
, loc
);
14068 suppress_warning (ret
);
14074 /* Initialize format string characters in the target charset. */
14077 init_target_chars (void)
14082 target_newline
= lang_hooks
.to_target_charset ('\n');
14083 target_percent
= lang_hooks
.to_target_charset ('%');
14084 target_c
= lang_hooks
.to_target_charset ('c');
14085 target_s
= lang_hooks
.to_target_charset ('s');
14086 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
14090 target_percent_c
[0] = target_percent
;
14091 target_percent_c
[1] = target_c
;
14092 target_percent_c
[2] = '\0';
14094 target_percent_s
[0] = target_percent
;
14095 target_percent_s
[1] = target_s
;
14096 target_percent_s
[2] = '\0';
14098 target_percent_s_newline
[0] = target_percent
;
14099 target_percent_s_newline
[1] = target_s
;
14100 target_percent_s_newline
[2] = target_newline
;
14101 target_percent_s_newline
[3] = '\0';
14108 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
14109 and no overflow/underflow occurred. INEXACT is true if M was not
14110 exactly calculated. TYPE is the tree type for the result. This
14111 function assumes that you cleared the MPFR flags and then
14112 calculated M to see if anything subsequently set a flag prior to
14113 entering this function. Return NULL_TREE if any checks fail. */
14116 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
14118 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
14119 overflow/underflow occurred. If -frounding-math, proceed iff the
14120 result of calling FUNC was exact. */
14121 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
14122 && (!flag_rounding_math
|| !inexact
))
14124 REAL_VALUE_TYPE rr
;
14126 real_from_mpfr (&rr
, m
, type
, MPFR_RNDN
);
14127 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
14128 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
14129 but the mpft_t is not, then we underflowed in the
14131 if (real_isfinite (&rr
)
14132 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
14134 REAL_VALUE_TYPE rmode
;
14136 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
14137 /* Proceed iff the specified mode can hold the value. */
14138 if (real_identical (&rmode
, &rr
))
14139 return build_real (type
, rmode
);
14145 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
14146 number and no overflow/underflow occurred. INEXACT is true if M
14147 was not exactly calculated. TYPE is the tree type for the result.
14148 This function assumes that you cleared the MPFR flags and then
14149 calculated M to see if anything subsequently set a flag prior to
14150 entering this function. Return NULL_TREE if any checks fail, if
14151 FORCE_CONVERT is true, then bypass the checks. */
14154 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
14156 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
14157 overflow/underflow occurred. If -frounding-math, proceed iff the
14158 result of calling FUNC was exact. */
14160 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
14161 && !mpfr_overflow_p () && !mpfr_underflow_p ()
14162 && (!flag_rounding_math
|| !inexact
)))
14164 REAL_VALUE_TYPE re
, im
;
14166 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), MPFR_RNDN
);
14167 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), MPFR_RNDN
);
14168 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
14169 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
14170 but the mpft_t is not, then we underflowed in the
14173 || (real_isfinite (&re
) && real_isfinite (&im
)
14174 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
14175 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
14177 REAL_VALUE_TYPE re_mode
, im_mode
;
14179 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
14180 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
14181 /* Proceed iff the specified mode can hold the value. */
14183 || (real_identical (&re_mode
, &re
)
14184 && real_identical (&im_mode
, &im
)))
14185 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
14186 build_real (TREE_TYPE (type
), im_mode
));
14192 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
14193 the pointer *(ARG_QUO) and return the result. The type is taken
14194 from the type of ARG0 and is used for setting the precision of the
14195 calculation and results. */
14198 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
14200 tree
const type
= TREE_TYPE (arg0
);
14201 tree result
= NULL_TREE
;
14206 /* To proceed, MPFR must exactly represent the target floating point
14207 format, which only happens when the target base equals two. */
14208 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
14209 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
14210 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
14212 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
14213 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
14215 if (real_isfinite (ra0
) && real_isfinite (ra1
))
14217 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
14218 const int prec
= fmt
->p
;
14219 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
? MPFR_RNDZ
: MPFR_RNDN
;
14224 mpfr_inits2 (prec
, m0
, m1
, NULL
);
14225 mpfr_from_real (m0
, ra0
, MPFR_RNDN
);
14226 mpfr_from_real (m1
, ra1
, MPFR_RNDN
);
14227 mpfr_clear_flags ();
14228 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
14229 /* Remquo is independent of the rounding mode, so pass
14230 inexact=0 to do_mpfr_ckconv(). */
14231 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
14232 mpfr_clears (m0
, m1
, NULL
);
14235 /* MPFR calculates quo in the host's long so it may
14236 return more bits in quo than the target int can hold
14237 if sizeof(host long) > sizeof(target int). This can
14238 happen even for native compilers in LP64 mode. In
14239 these cases, modulo the quo value with the largest
14240 number that the target int can hold while leaving one
14241 bit for the sign. */
14242 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
14243 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
14245 /* Dereference the quo pointer argument. */
14246 arg_quo
= build_fold_indirect_ref (arg_quo
);
14247 /* Proceed iff a valid pointer type was passed in. */
14248 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
14250 /* Set the value. */
14252 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
14253 build_int_cst (TREE_TYPE (arg_quo
),
14255 TREE_SIDE_EFFECTS (result_quo
) = 1;
14256 /* Combine the quo assignment with the rem. */
14257 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
14258 result_quo
, result_rem
));
14266 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
14267 resulting value as a tree with type TYPE. The mpfr precision is
14268 set to the precision of TYPE. We assume that this mpfr function
14269 returns zero if the result could be calculated exactly within the
14270 requested precision. In addition, the integer pointer represented
14271 by ARG_SG will be dereferenced and set to the appropriate signgam
14275 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
14277 tree result
= NULL_TREE
;
14281 /* To proceed, MPFR must exactly represent the target floating point
14282 format, which only happens when the target base equals two. Also
14283 verify ARG is a constant and that ARG_SG is an int pointer. */
14284 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
14285 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
14286 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
14287 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
14289 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
14291 /* In addition to NaN and Inf, the argument cannot be zero or a
14292 negative integer. */
14293 if (real_isfinite (ra
)
14294 && ra
->cl
!= rvc_zero
14295 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
14297 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
14298 const int prec
= fmt
->p
;
14299 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
? MPFR_RNDZ
: MPFR_RNDN
;
14304 mpfr_init2 (m
, prec
);
14305 mpfr_from_real (m
, ra
, MPFR_RNDN
);
14306 mpfr_clear_flags ();
14307 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
14308 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
14314 /* Dereference the arg_sg pointer argument. */
14315 arg_sg
= build_fold_indirect_ref (arg_sg
);
14316 /* Assign the signgam value into *arg_sg. */
14317 result_sg
= fold_build2 (MODIFY_EXPR
,
14318 TREE_TYPE (arg_sg
), arg_sg
,
14319 build_int_cst (TREE_TYPE (arg_sg
), sg
));
14320 TREE_SIDE_EFFECTS (result_sg
) = 1;
14321 /* Combine the signgam assignment with the lgamma result. */
14322 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
14323 result_sg
, result_lg
));
14331 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14332 mpc function FUNC on it and return the resulting value as a tree
14333 with type TYPE. The mpfr precision is set to the precision of
14334 TYPE. We assume that function FUNC returns zero if the result
14335 could be calculated exactly within the requested precision. If
14336 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14337 in the arguments and/or results. */
14340 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
14341 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
14343 tree result
= NULL_TREE
;
14348 /* To proceed, MPFR must exactly represent the target floating point
14349 format, which only happens when the target base equals two. */
14350 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
14351 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
14352 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
14353 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
14354 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
14356 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
14357 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
14358 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
14359 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
14362 || (real_isfinite (re0
) && real_isfinite (im0
)
14363 && real_isfinite (re1
) && real_isfinite (im1
)))
14365 const struct real_format
*const fmt
=
14366 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
14367 const int prec
= fmt
->p
;
14368 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
14369 ? MPFR_RNDZ
: MPFR_RNDN
;
14370 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
14374 mpc_init2 (m0
, prec
);
14375 mpc_init2 (m1
, prec
);
14376 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
14377 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
14378 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
14379 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
14380 mpfr_clear_flags ();
14381 inexact
= func (m0
, m0
, m1
, crnd
);
14382 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
14391 /* A wrapper function for builtin folding that prevents warnings for
14392 "statement without effect" and the like, caused by removing the
14393 call node earlier than the warning is generated. */
14396 fold_call_stmt (gcall
*stmt
, bool ignore
)
14398 tree ret
= NULL_TREE
;
14399 tree fndecl
= gimple_call_fndecl (stmt
);
14400 location_t loc
= gimple_location (stmt
);
14401 if (fndecl
&& fndecl_built_in_p (fndecl
)
14402 && !gimple_call_va_arg_pack_p (stmt
))
14404 int nargs
= gimple_call_num_args (stmt
);
14405 tree
*args
= (nargs
> 0
14406 ? gimple_call_arg_ptr (stmt
, 0)
14407 : &error_mark_node
);
14409 if (avoid_folding_inline_builtin (fndecl
))
14411 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
14413 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
14417 ret
= fold_builtin_n (loc
, NULL_TREE
, fndecl
, args
, nargs
, ignore
);
14420 /* Propagate location information from original call to
14421 expansion of builtin. Otherwise things like
14422 maybe_emit_chk_warning, that operate on the expansion
14423 of a builtin, will use the wrong location information. */
14424 if (gimple_has_location (stmt
))
14426 tree realret
= ret
;
14427 if (TREE_CODE (ret
) == NOP_EXPR
)
14428 realret
= TREE_OPERAND (ret
, 0);
14429 if (CAN_HAVE_LOCATION_P (realret
)
14430 && !EXPR_HAS_LOCATION (realret
))
14431 SET_EXPR_LOCATION (realret
, loc
);
14441 /* Look up the function in builtin_decl that corresponds to DECL
14442 and set ASMSPEC as its user assembler name. DECL must be a
14443 function decl that declares a builtin. */
14446 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
14448 gcc_assert (fndecl_built_in_p (decl
, BUILT_IN_NORMAL
)
14451 tree builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
14452 set_user_assembler_name (builtin
, asmspec
);
14454 if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_FFS
14455 && INT_TYPE_SIZE
< BITS_PER_WORD
)
14457 scalar_int_mode mode
= int_mode_for_size (INT_TYPE_SIZE
, 0).require ();
14458 set_user_assembler_libfunc ("ffs", asmspec
);
14459 set_optab_libfunc (ffs_optab
, mode
, "ffs");
14463 /* Return true if DECL is a builtin that expands to a constant or similarly
14466 is_simple_builtin (tree decl
)
14468 if (decl
&& fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
14469 switch (DECL_FUNCTION_CODE (decl
))
14471 /* Builtins that expand to constants. */
14472 case BUILT_IN_CONSTANT_P
:
14473 case BUILT_IN_EXPECT
:
14474 case BUILT_IN_OBJECT_SIZE
:
14475 case BUILT_IN_UNREACHABLE
:
14476 /* Simple register moves or loads from stack. */
14477 case BUILT_IN_ASSUME_ALIGNED
:
14478 case BUILT_IN_RETURN_ADDRESS
:
14479 case BUILT_IN_EXTRACT_RETURN_ADDR
:
14480 case BUILT_IN_FROB_RETURN_ADDR
:
14481 case BUILT_IN_RETURN
:
14482 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
14483 case BUILT_IN_FRAME_ADDRESS
:
14484 case BUILT_IN_VA_END
:
14485 case BUILT_IN_STACK_SAVE
:
14486 case BUILT_IN_STACK_RESTORE
:
14487 /* Exception state returns or moves registers around. */
14488 case BUILT_IN_EH_FILTER
:
14489 case BUILT_IN_EH_POINTER
:
14490 case BUILT_IN_EH_COPY_VALUES
:
14500 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14501 most probably expanded inline into reasonably simple code. This is a
14502 superset of is_simple_builtin. */
14504 is_inexpensive_builtin (tree decl
)
14508 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
14510 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14511 switch (DECL_FUNCTION_CODE (decl
))
14514 CASE_BUILT_IN_ALLOCA
:
14515 case BUILT_IN_BSWAP16
:
14516 case BUILT_IN_BSWAP32
:
14517 case BUILT_IN_BSWAP64
:
14518 case BUILT_IN_BSWAP128
:
14520 case BUILT_IN_CLZIMAX
:
14521 case BUILT_IN_CLZL
:
14522 case BUILT_IN_CLZLL
:
14524 case BUILT_IN_CTZIMAX
:
14525 case BUILT_IN_CTZL
:
14526 case BUILT_IN_CTZLL
:
14528 case BUILT_IN_FFSIMAX
:
14529 case BUILT_IN_FFSL
:
14530 case BUILT_IN_FFSLL
:
14531 case BUILT_IN_IMAXABS
:
14532 case BUILT_IN_FINITE
:
14533 case BUILT_IN_FINITEF
:
14534 case BUILT_IN_FINITEL
:
14535 case BUILT_IN_FINITED32
:
14536 case BUILT_IN_FINITED64
:
14537 case BUILT_IN_FINITED128
:
14538 case BUILT_IN_FPCLASSIFY
:
14539 case BUILT_IN_ISFINITE
:
14540 case BUILT_IN_ISINF_SIGN
:
14541 case BUILT_IN_ISINF
:
14542 case BUILT_IN_ISINFF
:
14543 case BUILT_IN_ISINFL
:
14544 case BUILT_IN_ISINFD32
:
14545 case BUILT_IN_ISINFD64
:
14546 case BUILT_IN_ISINFD128
:
14547 case BUILT_IN_ISNAN
:
14548 case BUILT_IN_ISNANF
:
14549 case BUILT_IN_ISNANL
:
14550 case BUILT_IN_ISNAND32
:
14551 case BUILT_IN_ISNAND64
:
14552 case BUILT_IN_ISNAND128
:
14553 case BUILT_IN_ISNORMAL
:
14554 case BUILT_IN_ISGREATER
:
14555 case BUILT_IN_ISGREATEREQUAL
:
14556 case BUILT_IN_ISLESS
:
14557 case BUILT_IN_ISLESSEQUAL
:
14558 case BUILT_IN_ISLESSGREATER
:
14559 case BUILT_IN_ISUNORDERED
:
14560 case BUILT_IN_VA_ARG_PACK
:
14561 case BUILT_IN_VA_ARG_PACK_LEN
:
14562 case BUILT_IN_VA_COPY
:
14563 case BUILT_IN_TRAP
:
14564 case BUILT_IN_SAVEREGS
:
14565 case BUILT_IN_POPCOUNTL
:
14566 case BUILT_IN_POPCOUNTLL
:
14567 case BUILT_IN_POPCOUNTIMAX
:
14568 case BUILT_IN_POPCOUNT
:
14569 case BUILT_IN_PARITYL
:
14570 case BUILT_IN_PARITYLL
:
14571 case BUILT_IN_PARITYIMAX
:
14572 case BUILT_IN_PARITY
:
14573 case BUILT_IN_LABS
:
14574 case BUILT_IN_LLABS
:
14575 case BUILT_IN_PREFETCH
:
14576 case BUILT_IN_ACC_ON_DEVICE
:
14580 return is_simple_builtin (decl
);
14586 /* Return true if T is a constant and the value cast to a target char
14587 can be represented by a host char.
14588 Store the casted char constant in *P if so. */
14591 target_char_cst_p (tree t
, char *p
)
14593 if (!tree_fits_uhwi_p (t
) || CHAR_TYPE_SIZE
!= HOST_BITS_PER_CHAR
)
14596 *p
= (char)tree_to_uhwi (t
);
14600 /* Return true if the builtin DECL is implemented in a standard library.
14601 Otherwise return false which doesn't guarantee it is not (thus the list
14602 of handled builtins below may be incomplete). */
14605 builtin_with_linkage_p (tree decl
)
14607 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14608 switch (DECL_FUNCTION_CODE (decl
))
14610 CASE_FLT_FN (BUILT_IN_ACOS
):
14611 CASE_FLT_FN (BUILT_IN_ACOSH
):
14612 CASE_FLT_FN (BUILT_IN_ASIN
):
14613 CASE_FLT_FN (BUILT_IN_ASINH
):
14614 CASE_FLT_FN (BUILT_IN_ATAN
):
14615 CASE_FLT_FN (BUILT_IN_ATANH
):
14616 CASE_FLT_FN (BUILT_IN_ATAN2
):
14617 CASE_FLT_FN (BUILT_IN_CBRT
):
14618 CASE_FLT_FN (BUILT_IN_CEIL
):
14619 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
14620 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
14621 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
14622 CASE_FLT_FN (BUILT_IN_COS
):
14623 CASE_FLT_FN (BUILT_IN_COSH
):
14624 CASE_FLT_FN (BUILT_IN_ERF
):
14625 CASE_FLT_FN (BUILT_IN_ERFC
):
14626 CASE_FLT_FN (BUILT_IN_EXP
):
14627 CASE_FLT_FN (BUILT_IN_EXP2
):
14628 CASE_FLT_FN (BUILT_IN_EXPM1
):
14629 CASE_FLT_FN (BUILT_IN_FABS
):
14630 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
14631 CASE_FLT_FN (BUILT_IN_FDIM
):
14632 CASE_FLT_FN (BUILT_IN_FLOOR
):
14633 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
14634 CASE_FLT_FN (BUILT_IN_FMA
):
14635 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
14636 CASE_FLT_FN (BUILT_IN_FMAX
):
14637 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX
):
14638 CASE_FLT_FN (BUILT_IN_FMIN
):
14639 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN
):
14640 CASE_FLT_FN (BUILT_IN_FMOD
):
14641 CASE_FLT_FN (BUILT_IN_FREXP
):
14642 CASE_FLT_FN (BUILT_IN_HYPOT
):
14643 CASE_FLT_FN (BUILT_IN_ILOGB
):
14644 CASE_FLT_FN (BUILT_IN_LDEXP
):
14645 CASE_FLT_FN (BUILT_IN_LGAMMA
):
14646 CASE_FLT_FN (BUILT_IN_LLRINT
):
14647 CASE_FLT_FN (BUILT_IN_LLROUND
):
14648 CASE_FLT_FN (BUILT_IN_LOG
):
14649 CASE_FLT_FN (BUILT_IN_LOG10
):
14650 CASE_FLT_FN (BUILT_IN_LOG1P
):
14651 CASE_FLT_FN (BUILT_IN_LOG2
):
14652 CASE_FLT_FN (BUILT_IN_LOGB
):
14653 CASE_FLT_FN (BUILT_IN_LRINT
):
14654 CASE_FLT_FN (BUILT_IN_LROUND
):
14655 CASE_FLT_FN (BUILT_IN_MODF
):
14656 CASE_FLT_FN (BUILT_IN_NAN
):
14657 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
14658 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
14659 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
14660 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
14661 CASE_FLT_FN (BUILT_IN_POW
):
14662 CASE_FLT_FN (BUILT_IN_REMAINDER
):
14663 CASE_FLT_FN (BUILT_IN_REMQUO
):
14664 CASE_FLT_FN (BUILT_IN_RINT
):
14665 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
14666 CASE_FLT_FN (BUILT_IN_ROUND
):
14667 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
14668 CASE_FLT_FN (BUILT_IN_SCALBLN
):
14669 CASE_FLT_FN (BUILT_IN_SCALBN
):
14670 CASE_FLT_FN (BUILT_IN_SIN
):
14671 CASE_FLT_FN (BUILT_IN_SINH
):
14672 CASE_FLT_FN (BUILT_IN_SINCOS
):
14673 CASE_FLT_FN (BUILT_IN_SQRT
):
14674 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT
):
14675 CASE_FLT_FN (BUILT_IN_TAN
):
14676 CASE_FLT_FN (BUILT_IN_TANH
):
14677 CASE_FLT_FN (BUILT_IN_TGAMMA
):
14678 CASE_FLT_FN (BUILT_IN_TRUNC
):
14679 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
14682 case BUILT_IN_STPCPY
:
14683 case BUILT_IN_STPNCPY
:
14684 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
14685 by libiberty's stpcpy.c for MinGW targets so we need to return true
14686 in order to be able to build libiberty in LTO mode for them. */
14695 /* Return true if OFFRNG is bounded to a subrange of offset values
14696 valid for the largest possible object. */
14699 access_ref::offset_bounded () const
14701 tree min
= TYPE_MIN_VALUE (ptrdiff_type_node
);
14702 tree max
= TYPE_MAX_VALUE (ptrdiff_type_node
);
14703 return wi::to_offset (min
) <= offrng
[0] && offrng
[1] <= wi::to_offset (max
);
14706 /* If CALLEE has known side effects, fill in INFO and return true.
14707 See tree-ssa-structalias.c:find_func_aliases
14708 for the list of builtins we might need to handle here. */
14711 builtin_fnspec (tree callee
)
14713 built_in_function code
= DECL_FUNCTION_CODE (callee
);
14717 /* All the following functions read memory pointed to by
14718 their second argument and write memory pointed to by first
14720 strcat/strncat additionally reads memory pointed to by the first
14722 case BUILT_IN_STRCAT
:
14723 case BUILT_IN_STRCAT_CHK
:
14725 case BUILT_IN_STRNCAT
:
14726 case BUILT_IN_STRNCAT_CHK
:
14728 case BUILT_IN_STRCPY
:
14729 case BUILT_IN_STRCPY_CHK
:
14731 case BUILT_IN_STPCPY
:
14732 case BUILT_IN_STPCPY_CHK
:
14734 case BUILT_IN_STRNCPY
:
14735 case BUILT_IN_MEMCPY
:
14736 case BUILT_IN_MEMMOVE
:
14737 case BUILT_IN_TM_MEMCPY
:
14738 case BUILT_IN_TM_MEMMOVE
:
14739 case BUILT_IN_STRNCPY_CHK
:
14740 case BUILT_IN_MEMCPY_CHK
:
14741 case BUILT_IN_MEMMOVE_CHK
:
14743 case BUILT_IN_MEMPCPY
:
14744 case BUILT_IN_MEMPCPY_CHK
:
14746 case BUILT_IN_STPNCPY
:
14747 case BUILT_IN_STPNCPY_CHK
:
14749 case BUILT_IN_BCOPY
:
14751 case BUILT_IN_BZERO
:
14753 case BUILT_IN_MEMCMP
:
14754 case BUILT_IN_MEMCMP_EQ
:
14755 case BUILT_IN_BCMP
:
14756 case BUILT_IN_STRNCMP
:
14757 case BUILT_IN_STRNCMP_EQ
:
14758 case BUILT_IN_STRNCASECMP
:
14761 /* The following functions read memory pointed to by their
14763 CASE_BUILT_IN_TM_LOAD (1):
14764 CASE_BUILT_IN_TM_LOAD (2):
14765 CASE_BUILT_IN_TM_LOAD (4):
14766 CASE_BUILT_IN_TM_LOAD (8):
14767 CASE_BUILT_IN_TM_LOAD (FLOAT
):
14768 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
14769 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
14770 CASE_BUILT_IN_TM_LOAD (M64
):
14771 CASE_BUILT_IN_TM_LOAD (M128
):
14772 CASE_BUILT_IN_TM_LOAD (M256
):
14773 case BUILT_IN_TM_LOG
:
14774 case BUILT_IN_TM_LOG_1
:
14775 case BUILT_IN_TM_LOG_2
:
14776 case BUILT_IN_TM_LOG_4
:
14777 case BUILT_IN_TM_LOG_8
:
14778 case BUILT_IN_TM_LOG_FLOAT
:
14779 case BUILT_IN_TM_LOG_DOUBLE
:
14780 case BUILT_IN_TM_LOG_LDOUBLE
:
14781 case BUILT_IN_TM_LOG_M64
:
14782 case BUILT_IN_TM_LOG_M128
:
14783 case BUILT_IN_TM_LOG_M256
:
14786 case BUILT_IN_INDEX
:
14787 case BUILT_IN_RINDEX
:
14788 case BUILT_IN_STRCHR
:
14789 case BUILT_IN_STRLEN
:
14790 case BUILT_IN_STRRCHR
:
14792 case BUILT_IN_STRNLEN
:
14795 /* These read memory pointed to by the first argument.
14796 Allocating memory does not have any side-effects apart from
14797 being the definition point for the pointer.
14798 Unix98 specifies that errno is set on allocation failure. */
14799 case BUILT_IN_STRDUP
:
14801 case BUILT_IN_STRNDUP
:
14803 /* Allocating memory does not have any side-effects apart from
14804 being the definition point for the pointer. */
14805 case BUILT_IN_MALLOC
:
14806 case BUILT_IN_ALIGNED_ALLOC
:
14807 case BUILT_IN_CALLOC
:
14808 case BUILT_IN_GOMP_ALLOC
:
14810 CASE_BUILT_IN_ALLOCA
:
14812 /* These read memory pointed to by the first argument with size
14813 in the third argument. */
14814 case BUILT_IN_MEMCHR
:
14816 /* These read memory pointed to by the first and second arguments. */
14817 case BUILT_IN_STRSTR
:
14818 case BUILT_IN_STRPBRK
:
14819 case BUILT_IN_STRCASECMP
:
14820 case BUILT_IN_STRCSPN
:
14821 case BUILT_IN_STRSPN
:
14822 case BUILT_IN_STRCMP
:
14823 case BUILT_IN_STRCMP_EQ
:
14825 /* Freeing memory kills the pointed-to memory. More importantly
14826 the call has to serve as a barrier for moving loads and stores
14828 case BUILT_IN_STACK_RESTORE
:
14829 case BUILT_IN_FREE
:
14830 case BUILT_IN_GOMP_FREE
:
14832 case BUILT_IN_VA_END
:
14834 /* Realloc serves both as allocation point and deallocation point. */
14835 case BUILT_IN_REALLOC
:
14837 case BUILT_IN_GAMMA_R
:
14838 case BUILT_IN_GAMMAF_R
:
14839 case BUILT_IN_GAMMAL_R
:
14840 case BUILT_IN_LGAMMA_R
:
14841 case BUILT_IN_LGAMMAF_R
:
14842 case BUILT_IN_LGAMMAL_R
:
14844 case BUILT_IN_FREXP
:
14845 case BUILT_IN_FREXPF
:
14846 case BUILT_IN_FREXPL
:
14847 case BUILT_IN_MODF
:
14848 case BUILT_IN_MODFF
:
14849 case BUILT_IN_MODFL
:
14851 case BUILT_IN_REMQUO
:
14852 case BUILT_IN_REMQUOF
:
14853 case BUILT_IN_REMQUOL
:
14855 case BUILT_IN_SINCOS
:
14856 case BUILT_IN_SINCOSF
:
14857 case BUILT_IN_SINCOSL
:
14859 case BUILT_IN_MEMSET
:
14860 case BUILT_IN_MEMSET_CHK
:
14861 case BUILT_IN_TM_MEMSET
:
14863 CASE_BUILT_IN_TM_STORE (1):
14864 CASE_BUILT_IN_TM_STORE (2):
14865 CASE_BUILT_IN_TM_STORE (4):
14866 CASE_BUILT_IN_TM_STORE (8):
14867 CASE_BUILT_IN_TM_STORE (FLOAT
):
14868 CASE_BUILT_IN_TM_STORE (DOUBLE
):
14869 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
14870 CASE_BUILT_IN_TM_STORE (M64
):
14871 CASE_BUILT_IN_TM_STORE (M128
):
14872 CASE_BUILT_IN_TM_STORE (M256
):
14874 case BUILT_IN_STACK_SAVE
:
14876 case BUILT_IN_ASSUME_ALIGNED
:
14878 /* But posix_memalign stores a pointer into the memory pointed to
14879 by its first argument. */
14880 case BUILT_IN_POSIX_MEMALIGN
: