]>
Commit | Line | Data |
---|---|---|
53800dbe | 1 | /* Expand builtin functions. |
fbd26352 | 2 | Copyright (C) 1988-2019 Free Software Foundation, Inc. |
53800dbe | 3 | |
f12b58b3 | 4 | This file is part of GCC. |
53800dbe | 5 | |
f12b58b3 | 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 | |
8c4c00c1 | 8 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 9 | version. |
53800dbe | 10 | |
f12b58b3 | 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
53800dbe | 15 | |
16 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
53800dbe | 19 | |
7c2ecb89 | 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. */ | |
23 | ||
53800dbe | 24 | #include "config.h" |
25 | #include "system.h" | |
805e22b2 | 26 | #include "coretypes.h" |
9ef16211 | 27 | #include "backend.h" |
7c29e30e | 28 | #include "target.h" |
29 | #include "rtl.h" | |
9ef16211 | 30 | #include "tree.h" |
ea36272b | 31 | #include "memmodel.h" |
9ef16211 | 32 | #include "gimple.h" |
7c29e30e | 33 | #include "predict.h" |
a950155e | 34 | #include "params.h" |
7c29e30e | 35 | #include "tm_p.h" |
36 | #include "stringpool.h" | |
c296f633 | 37 | #include "tree-vrp.h" |
7c29e30e | 38 | #include "tree-ssanames.h" |
39 | #include "expmed.h" | |
40 | #include "optabs.h" | |
7c29e30e | 41 | #include "emit-rtl.h" |
42 | #include "recog.h" | |
7c29e30e | 43 | #include "diagnostic-core.h" |
b20a8bb4 | 44 | #include "alias.h" |
b20a8bb4 | 45 | #include "fold-const.h" |
6c21be92 | 46 | #include "fold-const-call.h" |
e6a18b5a | 47 | #include "gimple-ssa-warn-restrict.h" |
9ed99284 | 48 | #include "stor-layout.h" |
49 | #include "calls.h" | |
50 | #include "varasm.h" | |
51 | #include "tree-object-size.h" | |
dae0b5cb | 52 | #include "realmpfr.h" |
94ea8568 | 53 | #include "cfgrtl.h" |
53800dbe | 54 | #include "except.h" |
d53441c8 | 55 | #include "dojump.h" |
56 | #include "explow.h" | |
d53441c8 | 57 | #include "stmt.h" |
53800dbe | 58 | #include "expr.h" |
d8fc4d0b | 59 | #include "libfuncs.h" |
53800dbe | 60 | #include "output.h" |
61 | #include "typeclass.h" | |
63c62881 | 62 | #include "langhooks.h" |
162719b3 | 63 | #include "value-prof.h" |
3b9c3a16 | 64 | #include "builtins.h" |
30a86690 | 65 | #include "stringpool.h" |
66 | #include "attribs.h" | |
f9acf11a | 67 | #include "asan.h" |
1f24b8e9 | 68 | #include "internal-fn.h" |
e3240774 | 69 | #include "case-cfn-macros.h" |
732905bb | 70 | #include "gimple-fold.h" |
5aef8938 | 71 | #include "intl.h" |
859b51f8 | 72 | #include "file-prefix-map.h" /* remap_macro_filename() */ |
a7babc1e | 73 | #include "gomp-constants.h" |
74 | #include "omp-general.h" | |
5383fb56 | 75 | |
3b9c3a16 | 76 | struct target_builtins default_target_builtins; |
77 | #if SWITCHABLE_TARGET | |
78 | struct target_builtins *this_target_builtins = &default_target_builtins; | |
79 | #endif | |
80 | ||
ab7943b9 | 81 | /* Define the names of the builtin function types and codes. */ |
96423453 | 82 | const char *const built_in_class_names[BUILT_IN_LAST] |
ab7943b9 | 83 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
84 | ||
9cfddb70 | 85 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X, |
0dfc45b5 | 86 | const char * built_in_names[(int) END_BUILTINS] = |
4e9d90c7 | 87 | { |
88 | #include "builtins.def" | |
89 | }; | |
ab7943b9 | 90 | |
cffdfb3d | 91 | /* Setup an array of builtin_info_type, make sure each element decl is |
df94cd3b | 92 | initialized to NULL_TREE. */ |
cffdfb3d | 93 | builtin_info_type builtin_info[(int)END_BUILTINS]; |
df94cd3b | 94 | |
0b049e15 | 95 | /* Non-zero if __builtin_constant_p should be folded right away. */ |
96 | bool force_folding_builtin_constant_p; | |
97 | ||
aecda0d6 | 98 | static int target_char_cast (tree, char *); |
d8ae1baa | 99 | static rtx get_memory_rtx (tree, tree); |
aecda0d6 | 100 | static int apply_args_size (void); |
101 | static int apply_result_size (void); | |
aecda0d6 | 102 | static rtx result_vector (int, rtx); |
aecda0d6 | 103 | static void expand_builtin_prefetch (tree); |
104 | static rtx expand_builtin_apply_args (void); | |
105 | static rtx expand_builtin_apply_args_1 (void); | |
106 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
107 | static void expand_builtin_return (rtx); | |
108 | static enum type_class type_to_class (tree); | |
109 | static rtx expand_builtin_classify_type (tree); | |
6b43bae4 | 110 | static rtx expand_builtin_mathfn_3 (tree, rtx, rtx); |
7e0713b1 | 111 | static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx); |
f97eea22 | 112 | static rtx expand_builtin_interclass_mathfn (tree, rtx); |
c3147c1a | 113 | static rtx expand_builtin_sincos (tree); |
f97eea22 | 114 | static rtx expand_builtin_cexpi (tree, rtx); |
ff1b14e4 | 115 | static rtx expand_builtin_int_roundingfn (tree, rtx); |
116 | static rtx expand_builtin_int_roundingfn_2 (tree, rtx); | |
79012a9d | 117 | static rtx expand_builtin_next_arg (void); |
aecda0d6 | 118 | static rtx expand_builtin_va_start (tree); |
119 | static rtx expand_builtin_va_end (tree); | |
120 | static rtx expand_builtin_va_copy (tree); | |
0dbefa15 | 121 | static rtx inline_expand_builtin_string_cmp (tree, rtx); |
a65c4d64 | 122 | static rtx expand_builtin_strcmp (tree, rtx); |
3754d046 | 123 | static rtx expand_builtin_strncmp (tree, rtx, machine_mode); |
f77c4496 | 124 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, scalar_int_mode); |
8d6c6ef5 | 125 | static rtx expand_builtin_memchr (tree, rtx); |
a65c4d64 | 126 | static rtx expand_builtin_memcpy (tree, rtx); |
d0fbba1a | 127 | static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len, |
02aa6d73 | 128 | rtx target, tree exp, |
129 | memop_ret retmode); | |
4d317237 | 130 | static rtx expand_builtin_memmove (tree, rtx); |
d0fbba1a | 131 | static rtx expand_builtin_mempcpy (tree, rtx); |
02aa6d73 | 132 | static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, memop_ret); |
5aef8938 | 133 | static rtx expand_builtin_strcat (tree, rtx); |
a65c4d64 | 134 | static rtx expand_builtin_strcpy (tree, rtx); |
a788aa5f | 135 | static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx); |
3754d046 | 136 | static rtx expand_builtin_stpcpy (tree, rtx, machine_mode); |
4d317237 | 137 | static rtx expand_builtin_stpncpy (tree, rtx); |
5aef8938 | 138 | static rtx expand_builtin_strncat (tree, rtx); |
a65c4d64 | 139 | static rtx expand_builtin_strncpy (tree, rtx); |
f77c4496 | 140 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, scalar_int_mode); |
3754d046 | 141 | static rtx expand_builtin_memset (tree, rtx, machine_mode); |
142 | static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree); | |
aecda0d6 | 143 | static rtx expand_builtin_bzero (tree); |
3754d046 | 144 | static rtx expand_builtin_strlen (tree, rtx, machine_mode); |
864bd5de | 145 | static rtx expand_builtin_strnlen (tree, rtx, machine_mode); |
2b29cc6a | 146 | static rtx expand_builtin_alloca (tree); |
3754d046 | 147 | static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab); |
aecda0d6 | 148 | static rtx expand_builtin_frame_address (tree, tree); |
389dd41b | 149 | static tree stabilize_va_list_loc (location_t, tree, int); |
aecda0d6 | 150 | static rtx expand_builtin_expect (tree, rtx); |
01107f42 | 151 | static rtx expand_builtin_expect_with_probability (tree, rtx); |
aecda0d6 | 152 | static tree fold_builtin_constant_p (tree); |
153 | static tree fold_builtin_classify_type (tree); | |
c7cbde74 | 154 | static tree fold_builtin_strlen (location_t, tree, tree); |
389dd41b | 155 | static tree fold_builtin_inf (location_t, tree, int); |
389dd41b | 156 | static tree rewrite_call_expr (location_t, tree, int, tree, int, ...); |
184fac50 | 157 | static bool validate_arg (const_tree, enum tree_code code); |
aecda0d6 | 158 | static rtx expand_builtin_fabs (tree, rtx, rtx); |
27f261ef | 159 | static rtx expand_builtin_signbit (tree, rtx); |
389dd41b | 160 | static tree fold_builtin_memcmp (location_t, tree, tree, tree); |
389dd41b | 161 | static tree fold_builtin_isascii (location_t, tree); |
162 | static tree fold_builtin_toascii (location_t, tree); | |
163 | static tree fold_builtin_isdigit (location_t, tree); | |
164 | static tree fold_builtin_fabs (location_t, tree, tree); | |
165 | static tree fold_builtin_abs (location_t, tree, tree); | |
166 | static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code, | |
d5019fe8 | 167 | enum tree_code); |
e80cc485 | 168 | static tree fold_builtin_0 (location_t, tree); |
169 | static tree fold_builtin_1 (location_t, tree, tree); | |
170 | static tree fold_builtin_2 (location_t, tree, tree, tree); | |
171 | static tree fold_builtin_3 (location_t, tree, tree, tree, tree); | |
12f08300 | 172 | static tree fold_builtin_varargs (location_t, tree, tree*, int); |
389dd41b | 173 | |
174 | static tree fold_builtin_strpbrk (location_t, tree, tree, tree); | |
389dd41b | 175 | static tree fold_builtin_strspn (location_t, tree, tree); |
176 | static tree fold_builtin_strcspn (location_t, tree, tree); | |
4ee9c684 | 177 | |
0a39fd54 | 178 | static rtx expand_builtin_object_size (tree); |
3754d046 | 179 | static rtx expand_builtin_memory_chk (tree, rtx, machine_mode, |
0a39fd54 | 180 | enum built_in_function); |
181 | static void maybe_emit_chk_warning (tree, enum built_in_function); | |
182 | static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); | |
2c281b15 | 183 | static void maybe_emit_free_warning (tree); |
c2f47e15 | 184 | static tree fold_builtin_object_size (tree, tree); |
99eabcc1 | 185 | |
e788f202 | 186 | unsigned HOST_WIDE_INT target_newline; |
b9ea678c | 187 | unsigned HOST_WIDE_INT target_percent; |
99eabcc1 | 188 | static unsigned HOST_WIDE_INT target_c; |
189 | static unsigned HOST_WIDE_INT target_s; | |
aea88c77 | 190 | char target_percent_c[3]; |
b9ea678c | 191 | char target_percent_s[3]; |
e788f202 | 192 | char target_percent_s_newline[4]; |
e5407ca6 | 193 | static tree do_mpfr_remquo (tree, tree, tree); |
e84da7c1 | 194 | static tree do_mpfr_lgamma_r (tree, tree, tree); |
1cd6e20d | 195 | static void expand_builtin_sync_synchronize (void); |
0a39fd54 | 196 | |
7bfefa9d | 197 | /* Return true if NAME starts with __builtin_ or __sync_. */ |
198 | ||
b29139ad | 199 | static bool |
1c47b3e8 | 200 | is_builtin_name (const char *name) |
b6a5fc45 | 201 | { |
b6a5fc45 | 202 | if (strncmp (name, "__builtin_", 10) == 0) |
203 | return true; | |
204 | if (strncmp (name, "__sync_", 7) == 0) | |
205 | return true; | |
1cd6e20d | 206 | if (strncmp (name, "__atomic_", 9) == 0) |
207 | return true; | |
b6a5fc45 | 208 | return false; |
209 | } | |
4ee9c684 | 210 | |
1c47b3e8 | 211 | /* Return true if NODE should be considered for inline expansion regardless |
212 | of the optimization level. This means whenever a function is invoked with | |
213 | its "internal" name, which normally contains the prefix "__builtin". */ | |
214 | ||
ae62deea | 215 | bool |
1c47b3e8 | 216 | called_as_built_in (tree node) |
217 | { | |
218 | /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since | |
219 | we want the name used to call the function, not the name it | |
220 | will have. */ | |
221 | const char *name = IDENTIFIER_POINTER (DECL_NAME (node)); | |
222 | return is_builtin_name (name); | |
223 | } | |
224 | ||
ceea063b | 225 | /* Compute values M and N such that M divides (address of EXP - N) and such |
226 | that N < M. If these numbers can be determined, store M in alignp and N in | |
227 | *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to | |
228 | *alignp and any bit-offset to *bitposp. | |
0d8f7716 | 229 | |
230 | Note that the address (and thus the alignment) computed here is based | |
231 | on the address to which a symbol resolves, whereas DECL_ALIGN is based | |
232 | on the address at which an object is actually located. These two | |
233 | addresses are not always the same. For example, on ARM targets, | |
234 | the address &foo of a Thumb function foo() has the lowest bit set, | |
3482bf13 | 235 | whereas foo() itself starts on an even address. |
698537d1 | 236 | |
3482bf13 | 237 | If ADDR_P is true we are taking the address of the memory reference EXP |
238 | and thus cannot rely on the access taking place. */ | |
239 | ||
240 | static bool | |
241 | get_object_alignment_2 (tree exp, unsigned int *alignp, | |
242 | unsigned HOST_WIDE_INT *bitposp, bool addr_p) | |
698537d1 | 243 | { |
eaa09bfd | 244 | poly_int64 bitsize, bitpos; |
98ab9e8f | 245 | tree offset; |
3754d046 | 246 | machine_mode mode; |
292237f3 | 247 | int unsignedp, reversep, volatilep; |
c8a2b4ff | 248 | unsigned int align = BITS_PER_UNIT; |
ceea063b | 249 | bool known_alignment = false; |
698537d1 | 250 | |
98ab9e8f | 251 | /* Get the innermost object and the constant (bitpos) and possibly |
252 | variable (offset) offset of the access. */ | |
292237f3 | 253 | exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode, |
b3b6e4b5 | 254 | &unsignedp, &reversep, &volatilep); |
98ab9e8f | 255 | |
256 | /* Extract alignment information from the innermost object and | |
257 | possibly adjust bitpos and offset. */ | |
3482bf13 | 258 | if (TREE_CODE (exp) == FUNCTION_DECL) |
0d8f7716 | 259 | { |
3482bf13 | 260 | /* Function addresses can encode extra information besides their |
261 | alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION | |
262 | allows the low bit to be used as a virtual bit, we know | |
263 | that the address itself must be at least 2-byte aligned. */ | |
264 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn) | |
265 | align = 2 * BITS_PER_UNIT; | |
0d8f7716 | 266 | } |
3482bf13 | 267 | else if (TREE_CODE (exp) == LABEL_DECL) |
268 | ; | |
269 | else if (TREE_CODE (exp) == CONST_DECL) | |
98ab9e8f | 270 | { |
3482bf13 | 271 | /* The alignment of a CONST_DECL is determined by its initializer. */ |
272 | exp = DECL_INITIAL (exp); | |
98ab9e8f | 273 | align = TYPE_ALIGN (TREE_TYPE (exp)); |
3482bf13 | 274 | if (CONSTANT_CLASS_P (exp)) |
579d67ba | 275 | align = targetm.constant_alignment (exp, align); |
e532afed | 276 | |
3482bf13 | 277 | known_alignment = true; |
98ab9e8f | 278 | } |
3482bf13 | 279 | else if (DECL_P (exp)) |
ceea063b | 280 | { |
3482bf13 | 281 | align = DECL_ALIGN (exp); |
ceea063b | 282 | known_alignment = true; |
ceea063b | 283 | } |
3482bf13 | 284 | else if (TREE_CODE (exp) == INDIRECT_REF |
285 | || TREE_CODE (exp) == MEM_REF | |
286 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
98ab9e8f | 287 | { |
288 | tree addr = TREE_OPERAND (exp, 0); | |
ceea063b | 289 | unsigned ptr_align; |
290 | unsigned HOST_WIDE_INT ptr_bitpos; | |
ab1e78e5 | 291 | unsigned HOST_WIDE_INT ptr_bitmask = ~0; |
ceea063b | 292 | |
ab1e78e5 | 293 | /* If the address is explicitely aligned, handle that. */ |
98ab9e8f | 294 | if (TREE_CODE (addr) == BIT_AND_EXPR |
295 | && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST) | |
296 | { | |
ab1e78e5 | 297 | ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)); |
298 | ptr_bitmask *= BITS_PER_UNIT; | |
ac29ece2 | 299 | align = least_bit_hwi (ptr_bitmask); |
98ab9e8f | 300 | addr = TREE_OPERAND (addr, 0); |
301 | } | |
ceea063b | 302 | |
3482bf13 | 303 | known_alignment |
304 | = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos); | |
3482bf13 | 305 | align = MAX (ptr_align, align); |
306 | ||
ab1e78e5 | 307 | /* Re-apply explicit alignment to the bitpos. */ |
308 | ptr_bitpos &= ptr_bitmask; | |
309 | ||
4083990a | 310 | /* The alignment of the pointer operand in a TARGET_MEM_REF |
311 | has to take the variable offset parts into account. */ | |
3482bf13 | 312 | if (TREE_CODE (exp) == TARGET_MEM_REF) |
153c3b50 | 313 | { |
3482bf13 | 314 | if (TMR_INDEX (exp)) |
315 | { | |
316 | unsigned HOST_WIDE_INT step = 1; | |
317 | if (TMR_STEP (exp)) | |
f9ae6f95 | 318 | step = TREE_INT_CST_LOW (TMR_STEP (exp)); |
ac29ece2 | 319 | align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT); |
3482bf13 | 320 | } |
321 | if (TMR_INDEX2 (exp)) | |
322 | align = BITS_PER_UNIT; | |
323 | known_alignment = false; | |
153c3b50 | 324 | } |
ceea063b | 325 | |
3482bf13 | 326 | /* When EXP is an actual memory reference then we can use |
327 | TYPE_ALIGN of a pointer indirection to derive alignment. | |
328 | Do so only if get_pointer_alignment_1 did not reveal absolute | |
4083990a | 329 | alignment knowledge and if using that alignment would |
330 | improve the situation. */ | |
700a9760 | 331 | unsigned int talign; |
4083990a | 332 | if (!addr_p && !known_alignment |
700a9760 | 333 | && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT) |
334 | && talign > align) | |
335 | align = talign; | |
4083990a | 336 | else |
337 | { | |
338 | /* Else adjust bitpos accordingly. */ | |
339 | bitpos += ptr_bitpos; | |
340 | if (TREE_CODE (exp) == MEM_REF | |
341 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
90ca1268 | 342 | bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT; |
4083990a | 343 | } |
98ab9e8f | 344 | } |
3482bf13 | 345 | else if (TREE_CODE (exp) == STRING_CST) |
153c3b50 | 346 | { |
3482bf13 | 347 | /* STRING_CST are the only constant objects we allow to be not |
348 | wrapped inside a CONST_DECL. */ | |
349 | align = TYPE_ALIGN (TREE_TYPE (exp)); | |
3482bf13 | 350 | if (CONSTANT_CLASS_P (exp)) |
579d67ba | 351 | align = targetm.constant_alignment (exp, align); |
e532afed | 352 | |
3482bf13 | 353 | known_alignment = true; |
98ab9e8f | 354 | } |
98ab9e8f | 355 | |
356 | /* If there is a non-constant offset part extract the maximum | |
357 | alignment that can prevail. */ | |
c8a2b4ff | 358 | if (offset) |
98ab9e8f | 359 | { |
ad464c56 | 360 | unsigned int trailing_zeros = tree_ctz (offset); |
c8a2b4ff | 361 | if (trailing_zeros < HOST_BITS_PER_INT) |
98ab9e8f | 362 | { |
c8a2b4ff | 363 | unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT; |
364 | if (inner) | |
365 | align = MIN (align, inner); | |
98ab9e8f | 366 | } |
98ab9e8f | 367 | } |
368 | ||
eaa09bfd | 369 | /* Account for the alignment of runtime coefficients, so that the constant |
370 | bitpos is guaranteed to be accurate. */ | |
371 | unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]); | |
372 | if (alt_align != 0 && alt_align < align) | |
373 | { | |
374 | align = alt_align; | |
375 | known_alignment = false; | |
376 | } | |
377 | ||
3482bf13 | 378 | *alignp = align; |
eaa09bfd | 379 | *bitposp = bitpos.coeffs[0] & (align - 1); |
ceea063b | 380 | return known_alignment; |
0c883ef3 | 381 | } |
382 | ||
3482bf13 | 383 | /* For a memory reference expression EXP compute values M and N such that M |
384 | divides (&EXP - N) and such that N < M. If these numbers can be determined, | |
385 | store M in alignp and N in *BITPOSP and return true. Otherwise return false | |
386 | and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */ | |
387 | ||
388 | bool | |
389 | get_object_alignment_1 (tree exp, unsigned int *alignp, | |
390 | unsigned HOST_WIDE_INT *bitposp) | |
391 | { | |
392 | return get_object_alignment_2 (exp, alignp, bitposp, false); | |
393 | } | |
394 | ||
957d0361 | 395 | /* Return the alignment in bits of EXP, an object. */ |
0c883ef3 | 396 | |
397 | unsigned int | |
957d0361 | 398 | get_object_alignment (tree exp) |
0c883ef3 | 399 | { |
400 | unsigned HOST_WIDE_INT bitpos = 0; | |
401 | unsigned int align; | |
402 | ||
ceea063b | 403 | get_object_alignment_1 (exp, &align, &bitpos); |
0c883ef3 | 404 | |
98ab9e8f | 405 | /* align and bitpos now specify known low bits of the pointer. |
406 | ptr & (align - 1) == bitpos. */ | |
407 | ||
408 | if (bitpos != 0) | |
ac29ece2 | 409 | align = least_bit_hwi (bitpos); |
957d0361 | 410 | return align; |
698537d1 | 411 | } |
412 | ||
ceea063b | 413 | /* For a pointer valued expression EXP compute values M and N such that M |
414 | divides (EXP - N) and such that N < M. If these numbers can be determined, | |
3482bf13 | 415 | store M in alignp and N in *BITPOSP and return true. Return false if |
416 | the results are just a conservative approximation. | |
53800dbe | 417 | |
ceea063b | 418 | If EXP is not a pointer, false is returned too. */ |
53800dbe | 419 | |
ceea063b | 420 | bool |
421 | get_pointer_alignment_1 (tree exp, unsigned int *alignp, | |
422 | unsigned HOST_WIDE_INT *bitposp) | |
53800dbe | 423 | { |
153c3b50 | 424 | STRIP_NOPS (exp); |
535e2026 | 425 | |
153c3b50 | 426 | if (TREE_CODE (exp) == ADDR_EXPR) |
3482bf13 | 427 | return get_object_alignment_2 (TREE_OPERAND (exp, 0), |
428 | alignp, bitposp, true); | |
906a9403 | 429 | else if (TREE_CODE (exp) == POINTER_PLUS_EXPR) |
430 | { | |
431 | unsigned int align; | |
432 | unsigned HOST_WIDE_INT bitpos; | |
433 | bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0), | |
434 | &align, &bitpos); | |
435 | if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST) | |
436 | bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT; | |
437 | else | |
438 | { | |
439 | unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1)); | |
440 | if (trailing_zeros < HOST_BITS_PER_INT) | |
441 | { | |
442 | unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT; | |
443 | if (inner) | |
444 | align = MIN (align, inner); | |
445 | } | |
446 | } | |
447 | *alignp = align; | |
448 | *bitposp = bitpos & (align - 1); | |
449 | return res; | |
450 | } | |
153c3b50 | 451 | else if (TREE_CODE (exp) == SSA_NAME |
452 | && POINTER_TYPE_P (TREE_TYPE (exp))) | |
53800dbe | 453 | { |
ceea063b | 454 | unsigned int ptr_align, ptr_misalign; |
153c3b50 | 455 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp); |
ceea063b | 456 | |
457 | if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign)) | |
458 | { | |
459 | *bitposp = ptr_misalign * BITS_PER_UNIT; | |
460 | *alignp = ptr_align * BITS_PER_UNIT; | |
d10da77a | 461 | /* Make sure to return a sensible alignment when the multiplication |
462 | by BITS_PER_UNIT overflowed. */ | |
463 | if (*alignp == 0) | |
464 | *alignp = 1u << (HOST_BITS_PER_INT - 1); | |
3482bf13 | 465 | /* We cannot really tell whether this result is an approximation. */ |
b428654a | 466 | return false; |
ceea063b | 467 | } |
468 | else | |
69fbc3aa | 469 | { |
470 | *bitposp = 0; | |
ceea063b | 471 | *alignp = BITS_PER_UNIT; |
472 | return false; | |
69fbc3aa | 473 | } |
53800dbe | 474 | } |
0bb8b39a | 475 | else if (TREE_CODE (exp) == INTEGER_CST) |
476 | { | |
477 | *alignp = BIGGEST_ALIGNMENT; | |
f9ae6f95 | 478 | *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT) |
0bb8b39a | 479 | & (BIGGEST_ALIGNMENT - 1)); |
480 | return true; | |
481 | } | |
153c3b50 | 482 | |
69fbc3aa | 483 | *bitposp = 0; |
ceea063b | 484 | *alignp = BITS_PER_UNIT; |
485 | return false; | |
53800dbe | 486 | } |
487 | ||
69fbc3aa | 488 | /* Return the alignment in bits of EXP, a pointer valued expression. |
489 | The alignment returned is, by default, the alignment of the thing that | |
490 | EXP points to. If it is not a POINTER_TYPE, 0 is returned. | |
491 | ||
492 | Otherwise, look at the expression to see if we can do better, i.e., if the | |
493 | expression is actually pointing at an object whose alignment is tighter. */ | |
494 | ||
495 | unsigned int | |
496 | get_pointer_alignment (tree exp) | |
497 | { | |
498 | unsigned HOST_WIDE_INT bitpos = 0; | |
499 | unsigned int align; | |
ceea063b | 500 | |
501 | get_pointer_alignment_1 (exp, &align, &bitpos); | |
69fbc3aa | 502 | |
503 | /* align and bitpos now specify known low bits of the pointer. | |
504 | ptr & (align - 1) == bitpos. */ | |
505 | ||
506 | if (bitpos != 0) | |
ac29ece2 | 507 | align = least_bit_hwi (bitpos); |
69fbc3aa | 508 | |
509 | return align; | |
510 | } | |
511 | ||
c4183f31 | 512 | /* Return the number of leading non-zero elements in the sequence |
c62d63d4 | 513 | [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes. |
514 | ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */ | |
515 | ||
c4183f31 | 516 | unsigned |
c62d63d4 | 517 | string_length (const void *ptr, unsigned eltsize, unsigned maxelts) |
518 | { | |
519 | gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4); | |
520 | ||
521 | unsigned n; | |
522 | ||
523 | if (eltsize == 1) | |
524 | { | |
525 | /* Optimize the common case of plain char. */ | |
526 | for (n = 0; n < maxelts; n++) | |
527 | { | |
528 | const char *elt = (const char*) ptr + n; | |
529 | if (!*elt) | |
530 | break; | |
531 | } | |
532 | } | |
533 | else | |
534 | { | |
535 | for (n = 0; n < maxelts; n++) | |
536 | { | |
537 | const char *elt = (const char*) ptr + n * eltsize; | |
538 | if (!memcmp (elt, "\0\0\0\0", eltsize)) | |
539 | break; | |
540 | } | |
541 | } | |
542 | return n; | |
543 | } | |
544 | ||
7af57b1c | 545 | /* For a call at LOC to a function FN that expects a string in the argument |
546 | ARG, issue a diagnostic due to it being a called with an argument | |
547 | declared at NONSTR that is a character array with no terminating NUL. */ | |
548 | ||
549 | void | |
550 | warn_string_no_nul (location_t loc, const char *fn, tree arg, tree decl) | |
551 | { | |
552 | if (TREE_NO_WARNING (arg)) | |
553 | return; | |
554 | ||
555 | loc = expansion_point_location_if_in_system_header (loc); | |
556 | ||
557 | if (warning_at (loc, OPT_Wstringop_overflow_, | |
558 | "%qs argument missing terminating nul", fn)) | |
559 | { | |
560 | inform (DECL_SOURCE_LOCATION (decl), | |
561 | "referenced argument declared here"); | |
562 | TREE_NO_WARNING (arg) = 1; | |
563 | } | |
564 | } | |
565 | ||
a788aa5f | 566 | /* If EXP refers to an unterminated constant character array return |
567 | the declaration of the object of which the array is a member or | |
fec27bf2 | 568 | element and if SIZE is not null, set *SIZE to the size of |
569 | the unterminated array and set *EXACT if the size is exact or | |
570 | clear it otherwise. Otherwise return null. */ | |
a788aa5f | 571 | |
50e57712 | 572 | tree |
fec27bf2 | 573 | unterminated_array (tree exp, tree *size /* = NULL */, bool *exact /* = NULL */) |
a788aa5f | 574 | { |
fec27bf2 | 575 | /* C_STRLEN will return NULL and set DECL in the info |
576 | structure if EXP references a unterminated array. */ | |
98d5ba5d | 577 | c_strlen_data lendata = { }; |
578 | tree len = c_strlen (exp, 1, &lendata); | |
6f7fa01f | 579 | if (len == NULL_TREE && lendata.minlen && lendata.decl) |
fec27bf2 | 580 | { |
581 | if (size) | |
582 | { | |
6f7fa01f | 583 | len = lendata.minlen; |
98d5ba5d | 584 | if (lendata.off) |
fec27bf2 | 585 | { |
98d5ba5d | 586 | /* Constant offsets are already accounted for in LENDATA.MINLEN, |
587 | but not in a SSA_NAME + CST expression. */ | |
588 | if (TREE_CODE (lendata.off) == INTEGER_CST) | |
fec27bf2 | 589 | *exact = true; |
98d5ba5d | 590 | else if (TREE_CODE (lendata.off) == PLUS_EXPR |
591 | && TREE_CODE (TREE_OPERAND (lendata.off, 1)) == INTEGER_CST) | |
fec27bf2 | 592 | { |
593 | /* Subtract the offset from the size of the array. */ | |
594 | *exact = false; | |
98d5ba5d | 595 | tree temp = TREE_OPERAND (lendata.off, 1); |
fec27bf2 | 596 | temp = fold_convert (ssizetype, temp); |
597 | len = fold_build2 (MINUS_EXPR, ssizetype, len, temp); | |
598 | } | |
599 | else | |
600 | *exact = false; | |
601 | } | |
602 | else | |
603 | *exact = true; | |
604 | ||
605 | *size = len; | |
606 | } | |
98d5ba5d | 607 | return lendata.decl; |
fec27bf2 | 608 | } |
609 | ||
610 | return NULL_TREE; | |
a788aa5f | 611 | } |
612 | ||
c62d63d4 | 613 | /* Compute the length of a null-terminated character string or wide |
614 | character string handling character sizes of 1, 2, and 4 bytes. | |
615 | TREE_STRING_LENGTH is not the right way because it evaluates to | |
616 | the size of the character array in bytes (as opposed to characters) | |
617 | and because it can contain a zero byte in the middle. | |
53800dbe | 618 | |
4172d65e | 619 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
c09841f6 | 620 | into the instruction stream and zero if it is going to be expanded. |
4172d65e | 621 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
681fab1e | 622 | is returned, otherwise NULL, since |
623 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
624 | evaluate the side-effects. | |
625 | ||
6bda159e | 626 | If ONLY_VALUE is two then we do not emit warnings about out-of-bound |
627 | accesses. Note that this implies the result is not going to be emitted | |
628 | into the instruction stream. | |
629 | ||
2b84b289 | 630 | Additional information about the string accessed may be recorded |
631 | in DATA. For example, if SRC references an unterminated string, | |
632 | then the declaration will be stored in the DECL field. If the | |
633 | length of the unterminated string can be determined, it'll be | |
634 | stored in the LEN field. Note this length could well be different | |
635 | than what a C strlen call would return. | |
7af57b1c | 636 | |
893c4605 | 637 | ELTSIZE is 1 for normal single byte character strings, and 2 or |
638 | 4 for wide characer strings. ELTSIZE is by default 1. | |
902de8ed | 639 | |
893c4605 | 640 | The value returned is of type `ssizetype'. */ |
53800dbe | 641 | |
4ee9c684 | 642 | tree |
2b84b289 | 643 | c_strlen (tree src, int only_value, c_strlen_data *data, unsigned eltsize) |
53800dbe | 644 | { |
2b84b289 | 645 | /* If we were not passed a DATA pointer, then get one to a local |
646 | structure. That avoids having to check DATA for NULL before | |
647 | each time we want to use it. */ | |
7d2853d1 | 648 | c_strlen_data local_strlen_data = { }; |
2b84b289 | 649 | if (!data) |
650 | data = &local_strlen_data; | |
651 | ||
2a0aa722 | 652 | gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4); |
681fab1e | 653 | STRIP_NOPS (src); |
654 | if (TREE_CODE (src) == COND_EXPR | |
655 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
656 | { | |
657 | tree len1, len2; | |
658 | ||
2b84b289 | 659 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize); |
660 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize); | |
0862b7e9 | 661 | if (tree_int_cst_equal (len1, len2)) |
681fab1e | 662 | return len1; |
663 | } | |
664 | ||
665 | if (TREE_CODE (src) == COMPOUND_EXPR | |
666 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
2b84b289 | 667 | return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize); |
681fab1e | 668 | |
c62d63d4 | 669 | location_t loc = EXPR_LOC_OR_LOC (src, input_location); |
da136652 | 670 | |
c62d63d4 | 671 | /* Offset from the beginning of the string in bytes. */ |
672 | tree byteoff; | |
893c4605 | 673 | tree memsize; |
7af57b1c | 674 | tree decl; |
675 | src = string_constant (src, &byteoff, &memsize, &decl); | |
53800dbe | 676 | if (src == 0) |
c2f47e15 | 677 | return NULL_TREE; |
902de8ed | 678 | |
c62d63d4 | 679 | /* Determine the size of the string element. */ |
893c4605 | 680 | if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))))) |
681 | return NULL_TREE; | |
c62d63d4 | 682 | |
683 | /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible | |
47d2cd73 | 684 | length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible |
c4183f31 | 685 | in case the latter is less than the size of the array, such as when |
686 | SRC refers to a short string literal used to initialize a large array. | |
687 | In that case, the elements of the array after the terminating NUL are | |
688 | all NUL. */ | |
689 | HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src); | |
9b7116a1 | 690 | strelts = strelts / eltsize; |
c4183f31 | 691 | |
893c4605 | 692 | if (!tree_fits_uhwi_p (memsize)) |
693 | return NULL_TREE; | |
694 | ||
9b7116a1 | 695 | HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize; |
c62d63d4 | 696 | |
697 | /* PTR can point to the byte representation of any string type, including | |
698 | char* and wchar_t*. */ | |
699 | const char *ptr = TREE_STRING_POINTER (src); | |
902de8ed | 700 | |
c62d63d4 | 701 | if (byteoff && TREE_CODE (byteoff) != INTEGER_CST) |
53800dbe | 702 | { |
893c4605 | 703 | /* The code below works only for single byte character types. */ |
704 | if (eltsize != 1) | |
705 | return NULL_TREE; | |
706 | ||
c4183f31 | 707 | /* If the string has an internal NUL character followed by any |
708 | non-NUL characters (e.g., "foo\0bar"), we can't compute | |
709 | the offset to the following NUL if we don't know where to | |
53800dbe | 710 | start searching for it. */ |
c4183f31 | 711 | unsigned len = string_length (ptr, eltsize, strelts); |
902de8ed | 712 | |
2b84b289 | 713 | /* Return when an embedded null character is found or none at all. |
714 | In the latter case, set the DECL/LEN field in the DATA structure | |
715 | so that callers may examine them. */ | |
7af57b1c | 716 | if (len + 1 < strelts) |
893c4605 | 717 | return NULL_TREE; |
7af57b1c | 718 | else if (len >= maxelts) |
719 | { | |
2b84b289 | 720 | data->decl = decl; |
fec27bf2 | 721 | data->off = byteoff; |
6f7fa01f | 722 | data->minlen = ssize_int (len); |
7af57b1c | 723 | return NULL_TREE; |
724 | } | |
d5d661d5 | 725 | |
9b7116a1 | 726 | /* For empty strings the result should be zero. */ |
727 | if (len == 0) | |
728 | return ssize_int (0); | |
729 | ||
53800dbe | 730 | /* We don't know the starting offset, but we do know that the string |
c4183f31 | 731 | has no internal zero bytes. If the offset falls within the bounds |
732 | of the string subtract the offset from the length of the string, | |
733 | and return that. Otherwise the length is zero. Take care to | |
734 | use SAVE_EXPR in case the OFFSET has side-effects. */ | |
f90ef0b2 | 735 | tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff) |
736 | : byteoff; | |
737 | offsave = fold_convert_loc (loc, sizetype, offsave); | |
c4183f31 | 738 | tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave, |
f90ef0b2 | 739 | size_int (len)); |
740 | tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len), | |
741 | offsave); | |
742 | lenexp = fold_convert_loc (loc, ssizetype, lenexp); | |
c4183f31 | 743 | return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp, |
744 | build_zero_cst (ssizetype)); | |
53800dbe | 745 | } |
746 | ||
c62d63d4 | 747 | /* Offset from the beginning of the string in elements. */ |
748 | HOST_WIDE_INT eltoff; | |
749 | ||
53800dbe | 750 | /* We have a known offset into the string. Start searching there for |
27d0c333 | 751 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
c62d63d4 | 752 | if (byteoff == 0) |
753 | eltoff = 0; | |
2a0aa722 | 754 | else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize) |
c62d63d4 | 755 | eltoff = -1; |
53800dbe | 756 | else |
2a0aa722 | 757 | eltoff = tree_to_uhwi (byteoff) / eltsize; |
902de8ed | 758 | |
1f63a7d6 | 759 | /* If the offset is known to be out of bounds, warn, and call strlen at |
760 | runtime. */ | |
9b7116a1 | 761 | if (eltoff < 0 || eltoff >= maxelts) |
53800dbe | 762 | { |
94a62c5a | 763 | /* Suppress multiple warnings for propagated constant strings. */ |
2f1c4f17 | 764 | if (only_value != 2 |
94a62c5a | 765 | && !TREE_NO_WARNING (src) |
766 | && warning_at (loc, OPT_Warray_bounds, | |
767 | "offset %qwi outside bounds of constant string", | |
768 | eltoff)) | |
769 | TREE_NO_WARNING (src) = 1; | |
c2f47e15 | 770 | return NULL_TREE; |
53800dbe | 771 | } |
902de8ed | 772 | |
893c4605 | 773 | /* If eltoff is larger than strelts but less than maxelts the |
774 | string length is zero, since the excess memory will be zero. */ | |
775 | if (eltoff > strelts) | |
776 | return ssize_int (0); | |
777 | ||
53800dbe | 778 | /* Use strlen to search for the first zero byte. Since any strings |
779 | constructed with build_string will have nulls appended, we win even | |
780 | if we get handed something like (char[4])"abcd". | |
781 | ||
c62d63d4 | 782 | Since ELTOFF is our starting index into the string, no further |
53800dbe | 783 | calculation is needed. */ |
c62d63d4 | 784 | unsigned len = string_length (ptr + eltoff * eltsize, eltsize, |
893c4605 | 785 | strelts - eltoff); |
c62d63d4 | 786 | |
9b7116a1 | 787 | /* Don't know what to return if there was no zero termination. |
2b84b289 | 788 | Ideally this would turn into a gcc_checking_assert over time. |
789 | Set DECL/LEN so callers can examine them. */ | |
9b7116a1 | 790 | if (len >= maxelts - eltoff) |
7af57b1c | 791 | { |
2b84b289 | 792 | data->decl = decl; |
fec27bf2 | 793 | data->off = byteoff; |
6f7fa01f | 794 | data->minlen = ssize_int (len); |
7af57b1c | 795 | return NULL_TREE; |
796 | } | |
2a0aa722 | 797 | |
c62d63d4 | 798 | return ssize_int (len); |
53800dbe | 799 | } |
800 | ||
e913b5cd | 801 | /* Return a constant integer corresponding to target reading |
78a0882d | 802 | GET_MODE_BITSIZE (MODE) bits from string constant STR. If |
803 | NULL_TERMINATED_P, reading stops after '\0' character, all further ones | |
804 | are assumed to be zero, otherwise it reads as many characters | |
805 | as needed. */ | |
806 | ||
807 | rtx | |
808 | c_readstr (const char *str, scalar_int_mode mode, | |
809 | bool null_terminated_p/*=true*/) | |
6840589f | 810 | { |
6840589f | 811 | HOST_WIDE_INT ch; |
812 | unsigned int i, j; | |
e913b5cd | 813 | HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT]; |
0407eaee | 814 | |
815 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); | |
e913b5cd | 816 | unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1) |
817 | / HOST_BITS_PER_WIDE_INT; | |
818 | ||
a12aa4cc | 819 | gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT); |
e913b5cd | 820 | for (i = 0; i < len; i++) |
821 | tmp[i] = 0; | |
6840589f | 822 | |
6840589f | 823 | ch = 1; |
824 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
825 | { | |
826 | j = i; | |
827 | if (WORDS_BIG_ENDIAN) | |
828 | j = GET_MODE_SIZE (mode) - i - 1; | |
829 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
ad8f8e52 | 830 | && GET_MODE_SIZE (mode) >= UNITS_PER_WORD) |
6840589f | 831 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; |
832 | j *= BITS_PER_UNIT; | |
7d3f6cc7 | 833 | |
78a0882d | 834 | if (ch || !null_terminated_p) |
6840589f | 835 | ch = (unsigned char) str[i]; |
e913b5cd | 836 | tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); |
6840589f | 837 | } |
ddb1be65 | 838 | |
ab2c1de8 | 839 | wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode)); |
e913b5cd | 840 | return immed_wide_int_const (c, mode); |
6840589f | 841 | } |
842 | ||
ecc318ff | 843 | /* Cast a target constant CST to target CHAR and if that value fits into |
5206b159 | 844 | host char type, return zero and put that value into variable pointed to by |
ecc318ff | 845 | P. */ |
846 | ||
847 | static int | |
aecda0d6 | 848 | target_char_cast (tree cst, char *p) |
ecc318ff | 849 | { |
850 | unsigned HOST_WIDE_INT val, hostval; | |
851 | ||
c19686c5 | 852 | if (TREE_CODE (cst) != INTEGER_CST |
ecc318ff | 853 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
854 | return 1; | |
855 | ||
e913b5cd | 856 | /* Do not care if it fits or not right here. */ |
f9ae6f95 | 857 | val = TREE_INT_CST_LOW (cst); |
e913b5cd | 858 | |
ecc318ff | 859 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
edc19fd0 | 860 | val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1; |
ecc318ff | 861 | |
862 | hostval = val; | |
863 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
edc19fd0 | 864 | hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1; |
ecc318ff | 865 | |
866 | if (val != hostval) | |
867 | return 1; | |
868 | ||
869 | *p = hostval; | |
870 | return 0; | |
871 | } | |
872 | ||
4ee9c684 | 873 | /* Similar to save_expr, but assumes that arbitrary code is not executed |
874 | in between the multiple evaluations. In particular, we assume that a | |
875 | non-addressable local variable will not be modified. */ | |
876 | ||
877 | static tree | |
878 | builtin_save_expr (tree exp) | |
879 | { | |
f6c35aa4 | 880 | if (TREE_CODE (exp) == SSA_NAME |
881 | || (TREE_ADDRESSABLE (exp) == 0 | |
882 | && (TREE_CODE (exp) == PARM_DECL | |
53e9c5c4 | 883 | || (VAR_P (exp) && !TREE_STATIC (exp))))) |
4ee9c684 | 884 | return exp; |
885 | ||
886 | return save_expr (exp); | |
887 | } | |
888 | ||
53800dbe | 889 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
890 | times to get the address of either a higher stack frame, or a return | |
891 | address located within it (depending on FNDECL_CODE). */ | |
902de8ed | 892 | |
c626df3d | 893 | static rtx |
869d0ef0 | 894 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count) |
53800dbe | 895 | { |
896 | int i; | |
869d0ef0 | 897 | rtx tem = INITIAL_FRAME_ADDRESS_RTX; |
3f840859 | 898 | if (tem == NULL_RTX) |
e3e15c50 | 899 | { |
3f840859 | 900 | /* For a zero count with __builtin_return_address, we don't care what |
901 | frame address we return, because target-specific definitions will | |
902 | override us. Therefore frame pointer elimination is OK, and using | |
903 | the soft frame pointer is OK. | |
904 | ||
905 | For a nonzero count, or a zero count with __builtin_frame_address, | |
906 | we require a stable offset from the current frame pointer to the | |
907 | previous one, so we must use the hard frame pointer, and | |
908 | we must disable frame pointer elimination. */ | |
909 | if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
910 | tem = frame_pointer_rtx; | |
911 | else | |
912 | { | |
913 | tem = hard_frame_pointer_rtx; | |
e3e15c50 | 914 | |
3f840859 | 915 | /* Tell reload not to eliminate the frame pointer. */ |
916 | crtl->accesses_prior_frames = 1; | |
917 | } | |
e3e15c50 | 918 | } |
869d0ef0 | 919 | |
53800dbe | 920 | if (count > 0) |
921 | SETUP_FRAME_ADDRESSES (); | |
53800dbe | 922 | |
3a69c60c | 923 | /* On the SPARC, the return address is not in the frame, it is in a |
53800dbe | 924 | register. There is no way to access it off of the current frame |
925 | pointer, but it can be accessed off the previous frame pointer by | |
926 | reading the value from the register window save area. */ | |
a26d6c60 | 927 | if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS) |
53800dbe | 928 | count--; |
53800dbe | 929 | |
930 | /* Scan back COUNT frames to the specified frame. */ | |
931 | for (i = 0; i < count; i++) | |
932 | { | |
933 | /* Assume the dynamic chain pointer is in the word that the | |
934 | frame address points to, unless otherwise specified. */ | |
53800dbe | 935 | tem = DYNAMIC_CHAIN_ADDRESS (tem); |
53800dbe | 936 | tem = memory_address (Pmode, tem); |
00060fc2 | 937 | tem = gen_frame_mem (Pmode, tem); |
83fc1478 | 938 | tem = copy_to_reg (tem); |
53800dbe | 939 | } |
940 | ||
3a69c60c | 941 | /* For __builtin_frame_address, return what we've got. But, on |
942 | the SPARC for example, we may have to add a bias. */ | |
53800dbe | 943 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) |
3a69c60c | 944 | return FRAME_ADDR_RTX (tem); |
53800dbe | 945 | |
3a69c60c | 946 | /* For __builtin_return_address, get the return address from that frame. */ |
53800dbe | 947 | #ifdef RETURN_ADDR_RTX |
948 | tem = RETURN_ADDR_RTX (count, tem); | |
949 | #else | |
950 | tem = memory_address (Pmode, | |
29c05e22 | 951 | plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode))); |
00060fc2 | 952 | tem = gen_frame_mem (Pmode, tem); |
53800dbe | 953 | #endif |
954 | return tem; | |
955 | } | |
956 | ||
f7c44134 | 957 | /* Alias set used for setjmp buffer. */ |
32c2fdea | 958 | static alias_set_type setjmp_alias_set = -1; |
f7c44134 | 959 | |
6b7f6858 | 960 | /* Construct the leading half of a __builtin_setjmp call. Control will |
2c8a1497 | 961 | return to RECEIVER_LABEL. This is also called directly by the SJLJ |
962 | exception handling code. */ | |
53800dbe | 963 | |
6b7f6858 | 964 | void |
aecda0d6 | 965 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
53800dbe | 966 | { |
3754d046 | 967 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 968 | rtx stack_save; |
f7c44134 | 969 | rtx mem; |
53800dbe | 970 | |
f7c44134 | 971 | if (setjmp_alias_set == -1) |
972 | setjmp_alias_set = new_alias_set (); | |
973 | ||
85d654dd | 974 | buf_addr = convert_memory_address (Pmode, buf_addr); |
53800dbe | 975 | |
37ae8504 | 976 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
53800dbe | 977 | |
6b7f6858 | 978 | /* We store the frame pointer and the address of receiver_label in |
979 | the buffer and use the rest of it for the stack save area, which | |
980 | is machine-dependent. */ | |
53800dbe | 981 | |
f7c44134 | 982 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ab6ab77e | 983 | set_mem_alias_set (mem, setjmp_alias_set); |
e3e026e8 | 984 | emit_move_insn (mem, targetm.builtin_setjmp_frame_value ()); |
f7c44134 | 985 | |
29c05e22 | 986 | mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
987 | GET_MODE_SIZE (Pmode))), | |
ab6ab77e | 988 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 989 | |
990 | emit_move_insn (validize_mem (mem), | |
6b7f6858 | 991 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
53800dbe | 992 | |
993 | stack_save = gen_rtx_MEM (sa_mode, | |
29c05e22 | 994 | plus_constant (Pmode, buf_addr, |
53800dbe | 995 | 2 * GET_MODE_SIZE (Pmode))); |
ab6ab77e | 996 | set_mem_alias_set (stack_save, setjmp_alias_set); |
e9c97615 | 997 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
53800dbe | 998 | |
999 | /* If there is further processing to do, do it. */ | |
a3c81e61 | 1000 | if (targetm.have_builtin_setjmp_setup ()) |
1001 | emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr)); | |
53800dbe | 1002 | |
29f09705 | 1003 | /* We have a nonlocal label. */ |
18d50ae6 | 1004 | cfun->has_nonlocal_label = 1; |
6b7f6858 | 1005 | } |
53800dbe | 1006 | |
2c8a1497 | 1007 | /* Construct the trailing part of a __builtin_setjmp call. This is |
4598ade9 | 1008 | also called directly by the SJLJ exception handling code. |
1009 | If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */ | |
6b7f6858 | 1010 | |
1011 | void | |
a3c81e61 | 1012 | expand_builtin_setjmp_receiver (rtx receiver_label) |
6b7f6858 | 1013 | { |
82c7907c | 1014 | rtx chain; |
1015 | ||
4598ade9 | 1016 | /* Mark the FP as used when we get here, so we have to make sure it's |
53800dbe | 1017 | marked as used by this function. */ |
18b42941 | 1018 | emit_use (hard_frame_pointer_rtx); |
53800dbe | 1019 | |
1020 | /* Mark the static chain as clobbered here so life information | |
1021 | doesn't get messed up for it. */ | |
3c56e0c1 | 1022 | chain = rtx_for_static_chain (current_function_decl, true); |
82c7907c | 1023 | if (chain && REG_P (chain)) |
1024 | emit_clobber (chain); | |
53800dbe | 1025 | |
1026 | /* Now put in the code to restore the frame pointer, and argument | |
491e04ef | 1027 | pointer, if needed. */ |
a3c81e61 | 1028 | if (! targetm.have_nonlocal_goto ()) |
62dcb5c8 | 1029 | { |
1030 | /* First adjust our frame pointer to its actual value. It was | |
1031 | previously set to the start of the virtual area corresponding to | |
1032 | the stacked variables when we branched here and now needs to be | |
1033 | adjusted to the actual hardware fp value. | |
1034 | ||
1035 | Assignments to virtual registers are converted by | |
1036 | instantiate_virtual_regs into the corresponding assignment | |
1037 | to the underlying register (fp in this case) that makes | |
1038 | the original assignment true. | |
1039 | So the following insn will actually be decrementing fp by | |
8374586c | 1040 | TARGET_STARTING_FRAME_OFFSET. */ |
62dcb5c8 | 1041 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); |
1042 | ||
1043 | /* Restoring the frame pointer also modifies the hard frame pointer. | |
1044 | Mark it used (so that the previous assignment remains live once | |
1045 | the frame pointer is eliminated) and clobbered (to represent the | |
1046 | implicit update from the assignment). */ | |
1047 | emit_use (hard_frame_pointer_rtx); | |
1048 | emit_clobber (hard_frame_pointer_rtx); | |
1049 | } | |
53800dbe | 1050 | |
a494b6d7 | 1051 | if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM]) |
53800dbe | 1052 | { |
4598ade9 | 1053 | /* If the argument pointer can be eliminated in favor of the |
1054 | frame pointer, we don't need to restore it. We assume here | |
1055 | that if such an elimination is present, it can always be used. | |
1056 | This is the case on all known machines; if we don't make this | |
1057 | assumption, we do unnecessary saving on many machines. */ | |
53800dbe | 1058 | size_t i; |
e99c3a1d | 1059 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
53800dbe | 1060 | |
3098b2d3 | 1061 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
53800dbe | 1062 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
1063 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
1064 | break; | |
1065 | ||
3098b2d3 | 1066 | if (i == ARRAY_SIZE (elim_regs)) |
53800dbe | 1067 | { |
1068 | /* Now restore our arg pointer from the address at which it | |
05927e40 | 1069 | was saved in our stack frame. */ |
27a7a23a | 1070 | emit_move_insn (crtl->args.internal_arg_pointer, |
b079a207 | 1071 | copy_to_reg (get_arg_pointer_save_area ())); |
53800dbe | 1072 | } |
1073 | } | |
53800dbe | 1074 | |
a3c81e61 | 1075 | if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ()) |
1076 | emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label)); | |
1077 | else if (targetm.have_nonlocal_goto_receiver ()) | |
1078 | emit_insn (targetm.gen_nonlocal_goto_receiver ()); | |
53800dbe | 1079 | else |
a3c81e61 | 1080 | { /* Nothing */ } |
57f6bb94 | 1081 | |
3072d30e | 1082 | /* We must not allow the code we just generated to be reordered by |
1083 | scheduling. Specifically, the update of the frame pointer must | |
62dcb5c8 | 1084 | happen immediately, not later. */ |
3072d30e | 1085 | emit_insn (gen_blockage ()); |
6b7f6858 | 1086 | } |
53800dbe | 1087 | |
53800dbe | 1088 | /* __builtin_longjmp is passed a pointer to an array of five words (not |
1089 | all will be used on all machines). It operates similarly to the C | |
1090 | library function of the same name, but is more efficient. Much of | |
2c8a1497 | 1091 | the code below is copied from the handling of non-local gotos. */ |
53800dbe | 1092 | |
c626df3d | 1093 | static void |
aecda0d6 | 1094 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
53800dbe | 1095 | { |
1e0c0b35 | 1096 | rtx fp, lab, stack; |
1097 | rtx_insn *insn, *last; | |
3754d046 | 1098 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 1099 | |
48e1416a | 1100 | /* DRAP is needed for stack realign if longjmp is expanded to current |
27a7a23a | 1101 | function */ |
1102 | if (SUPPORTS_STACK_ALIGNMENT) | |
1103 | crtl->need_drap = true; | |
1104 | ||
f7c44134 | 1105 | if (setjmp_alias_set == -1) |
1106 | setjmp_alias_set = new_alias_set (); | |
1107 | ||
85d654dd | 1108 | buf_addr = convert_memory_address (Pmode, buf_addr); |
479e4d5e | 1109 | |
53800dbe | 1110 | buf_addr = force_reg (Pmode, buf_addr); |
1111 | ||
82c7907c | 1112 | /* We require that the user must pass a second argument of 1, because |
1113 | that is what builtin_setjmp will return. */ | |
64db345d | 1114 | gcc_assert (value == const1_rtx); |
53800dbe | 1115 | |
4712c7d6 | 1116 | last = get_last_insn (); |
a3c81e61 | 1117 | if (targetm.have_builtin_longjmp ()) |
1118 | emit_insn (targetm.gen_builtin_longjmp (buf_addr)); | |
53800dbe | 1119 | else |
53800dbe | 1120 | { |
1121 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
29c05e22 | 1122 | lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
53800dbe | 1123 | GET_MODE_SIZE (Pmode))); |
1124 | ||
29c05e22 | 1125 | stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr, |
53800dbe | 1126 | 2 * GET_MODE_SIZE (Pmode))); |
ab6ab77e | 1127 | set_mem_alias_set (fp, setjmp_alias_set); |
1128 | set_mem_alias_set (lab, setjmp_alias_set); | |
1129 | set_mem_alias_set (stack, setjmp_alias_set); | |
53800dbe | 1130 | |
1131 | /* Pick up FP, label, and SP from the block and jump. This code is | |
1132 | from expand_goto in stmt.c; see there for detailed comments. */ | |
a3c81e61 | 1133 | if (targetm.have_nonlocal_goto ()) |
53800dbe | 1134 | /* We have to pass a value to the nonlocal_goto pattern that will |
1135 | get copied into the static_chain pointer, but it does not matter | |
1136 | what that value is, because builtin_setjmp does not use it. */ | |
a3c81e61 | 1137 | emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp)); |
53800dbe | 1138 | else |
53800dbe | 1139 | { |
1140 | lab = copy_to_reg (lab); | |
1141 | ||
18b42941 | 1142 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
1143 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
2a871ad1 | 1144 | |
5f337044 | 1145 | /* Restore the frame pointer and stack pointer. We must use a |
1146 | temporary since the setjmp buffer may be a local. */ | |
1147 | fp = copy_to_reg (fp); | |
e9c97615 | 1148 | emit_stack_restore (SAVE_NONLOCAL, stack); |
5f337044 | 1149 | emit_move_insn (hard_frame_pointer_rtx, fp); |
53800dbe | 1150 | |
18b42941 | 1151 | emit_use (hard_frame_pointer_rtx); |
1152 | emit_use (stack_pointer_rtx); | |
53800dbe | 1153 | emit_indirect_jump (lab); |
1154 | } | |
1155 | } | |
615166bb | 1156 | |
1157 | /* Search backwards and mark the jump insn as a non-local goto. | |
1158 | Note that this precludes the use of __builtin_longjmp to a | |
1159 | __builtin_setjmp target in the same function. However, we've | |
1160 | already cautioned the user that these functions are for | |
1161 | internal exception handling use only. */ | |
449c0509 | 1162 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
1163 | { | |
64db345d | 1164 | gcc_assert (insn != last); |
7d3f6cc7 | 1165 | |
6d7dc5b9 | 1166 | if (JUMP_P (insn)) |
449c0509 | 1167 | { |
a1ddb869 | 1168 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
449c0509 | 1169 | break; |
1170 | } | |
6d7dc5b9 | 1171 | else if (CALL_P (insn)) |
9342ee68 | 1172 | break; |
449c0509 | 1173 | } |
53800dbe | 1174 | } |
1175 | ||
0e80b01d | 1176 | static inline bool |
1177 | more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter) | |
1178 | { | |
1179 | return (iter->i < iter->n); | |
1180 | } | |
1181 | ||
1182 | /* This function validates the types of a function call argument list | |
1183 | against a specified list of tree_codes. If the last specifier is a 0, | |
5cfa3fc8 | 1184 | that represents an ellipsis, otherwise the last specifier must be a |
0e80b01d | 1185 | VOID_TYPE. */ |
1186 | ||
1187 | static bool | |
1188 | validate_arglist (const_tree callexpr, ...) | |
1189 | { | |
1190 | enum tree_code code; | |
1191 | bool res = 0; | |
1192 | va_list ap; | |
1193 | const_call_expr_arg_iterator iter; | |
1194 | const_tree arg; | |
1195 | ||
1196 | va_start (ap, callexpr); | |
1197 | init_const_call_expr_arg_iterator (callexpr, &iter); | |
1198 | ||
5cfa3fc8 | 1199 | /* Get a bitmap of pointer argument numbers declared attribute nonnull. */ |
184fac50 | 1200 | tree fn = CALL_EXPR_FN (callexpr); |
1201 | bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn))); | |
5cfa3fc8 | 1202 | |
1203 | for (unsigned argno = 1; ; ++argno) | |
0e80b01d | 1204 | { |
1205 | code = (enum tree_code) va_arg (ap, int); | |
5cfa3fc8 | 1206 | |
0e80b01d | 1207 | switch (code) |
1208 | { | |
1209 | case 0: | |
1210 | /* This signifies an ellipses, any further arguments are all ok. */ | |
1211 | res = true; | |
1212 | goto end; | |
1213 | case VOID_TYPE: | |
1214 | /* This signifies an endlink, if no arguments remain, return | |
1215 | true, otherwise return false. */ | |
1216 | res = !more_const_call_expr_args_p (&iter); | |
1217 | goto end; | |
5cfa3fc8 | 1218 | case POINTER_TYPE: |
1219 | /* The actual argument must be nonnull when either the whole | |
1220 | called function has been declared nonnull, or when the formal | |
1221 | argument corresponding to the actual argument has been. */ | |
184fac50 | 1222 | if (argmap |
1223 | && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno))) | |
1224 | { | |
1225 | arg = next_const_call_expr_arg (&iter); | |
1226 | if (!validate_arg (arg, code) || integer_zerop (arg)) | |
1227 | goto end; | |
1228 | break; | |
1229 | } | |
5cfa3fc8 | 1230 | /* FALLTHRU */ |
0e80b01d | 1231 | default: |
1232 | /* If no parameters remain or the parameter's code does not | |
1233 | match the specified code, return false. Otherwise continue | |
1234 | checking any remaining arguments. */ | |
1235 | arg = next_const_call_expr_arg (&iter); | |
184fac50 | 1236 | if (!validate_arg (arg, code)) |
0e80b01d | 1237 | goto end; |
1238 | break; | |
1239 | } | |
1240 | } | |
0e80b01d | 1241 | |
1242 | /* We need gotos here since we can only have one VA_CLOSE in a | |
1243 | function. */ | |
1244 | end: ; | |
1245 | va_end (ap); | |
1246 | ||
5cfa3fc8 | 1247 | BITMAP_FREE (argmap); |
1248 | ||
0e80b01d | 1249 | return res; |
1250 | } | |
1251 | ||
4ee9c684 | 1252 | /* Expand a call to __builtin_nonlocal_goto. We're passed the target label |
1253 | and the address of the save area. */ | |
1254 | ||
1255 | static rtx | |
c2f47e15 | 1256 | expand_builtin_nonlocal_goto (tree exp) |
4ee9c684 | 1257 | { |
1258 | tree t_label, t_save_area; | |
1e0c0b35 | 1259 | rtx r_label, r_save_area, r_fp, r_sp; |
1260 | rtx_insn *insn; | |
4ee9c684 | 1261 | |
c2f47e15 | 1262 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 1263 | return NULL_RTX; |
1264 | ||
c2f47e15 | 1265 | t_label = CALL_EXPR_ARG (exp, 0); |
1266 | t_save_area = CALL_EXPR_ARG (exp, 1); | |
4ee9c684 | 1267 | |
8ec3c5c2 | 1268 | r_label = expand_normal (t_label); |
3dce56cc | 1269 | r_label = convert_memory_address (Pmode, r_label); |
8ec3c5c2 | 1270 | r_save_area = expand_normal (t_save_area); |
3dce56cc | 1271 | r_save_area = convert_memory_address (Pmode, r_save_area); |
d1ff492e | 1272 | /* Copy the address of the save location to a register just in case it was |
1273 | based on the frame pointer. */ | |
51adbc8a | 1274 | r_save_area = copy_to_reg (r_save_area); |
4ee9c684 | 1275 | r_fp = gen_rtx_MEM (Pmode, r_save_area); |
1276 | r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL), | |
29c05e22 | 1277 | plus_constant (Pmode, r_save_area, |
1278 | GET_MODE_SIZE (Pmode))); | |
4ee9c684 | 1279 | |
18d50ae6 | 1280 | crtl->has_nonlocal_goto = 1; |
4ee9c684 | 1281 | |
4ee9c684 | 1282 | /* ??? We no longer need to pass the static chain value, afaik. */ |
a3c81e61 | 1283 | if (targetm.have_nonlocal_goto ()) |
1284 | emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp)); | |
4ee9c684 | 1285 | else |
4ee9c684 | 1286 | { |
1287 | r_label = copy_to_reg (r_label); | |
1288 | ||
18b42941 | 1289 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
1290 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
491e04ef | 1291 | |
5f337044 | 1292 | /* Restore the frame pointer and stack pointer. We must use a |
1293 | temporary since the setjmp buffer may be a local. */ | |
1294 | r_fp = copy_to_reg (r_fp); | |
e9c97615 | 1295 | emit_stack_restore (SAVE_NONLOCAL, r_sp); |
5f337044 | 1296 | emit_move_insn (hard_frame_pointer_rtx, r_fp); |
491e04ef | 1297 | |
4ee9c684 | 1298 | /* USE of hard_frame_pointer_rtx added for consistency; |
1299 | not clear if really needed. */ | |
18b42941 | 1300 | emit_use (hard_frame_pointer_rtx); |
1301 | emit_use (stack_pointer_rtx); | |
ad0d0af8 | 1302 | |
1303 | /* If the architecture is using a GP register, we must | |
1304 | conservatively assume that the target function makes use of it. | |
1305 | The prologue of functions with nonlocal gotos must therefore | |
1306 | initialize the GP register to the appropriate value, and we | |
1307 | must then make sure that this value is live at the point | |
1308 | of the jump. (Note that this doesn't necessarily apply | |
1309 | to targets with a nonlocal_goto pattern; they are free | |
1310 | to implement it in their own way. Note also that this is | |
1311 | a no-op if the GP register is a global invariant.) */ | |
1e826931 | 1312 | unsigned regnum = PIC_OFFSET_TABLE_REGNUM; |
1313 | if (regnum != INVALID_REGNUM && fixed_regs[regnum]) | |
18b42941 | 1314 | emit_use (pic_offset_table_rtx); |
ad0d0af8 | 1315 | |
4ee9c684 | 1316 | emit_indirect_jump (r_label); |
1317 | } | |
491e04ef | 1318 | |
4ee9c684 | 1319 | /* Search backwards to the jump insn and mark it as a |
1320 | non-local goto. */ | |
1321 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) | |
1322 | { | |
6d7dc5b9 | 1323 | if (JUMP_P (insn)) |
4ee9c684 | 1324 | { |
a1ddb869 | 1325 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
4ee9c684 | 1326 | break; |
1327 | } | |
6d7dc5b9 | 1328 | else if (CALL_P (insn)) |
4ee9c684 | 1329 | break; |
1330 | } | |
1331 | ||
1332 | return const0_rtx; | |
1333 | } | |
1334 | ||
843d08a9 | 1335 | /* __builtin_update_setjmp_buf is passed a pointer to an array of five words |
1336 | (not all will be used on all machines) that was passed to __builtin_setjmp. | |
97354ae4 | 1337 | It updates the stack pointer in that block to the current value. This is |
1338 | also called directly by the SJLJ exception handling code. */ | |
843d08a9 | 1339 | |
97354ae4 | 1340 | void |
843d08a9 | 1341 | expand_builtin_update_setjmp_buf (rtx buf_addr) |
1342 | { | |
3754d046 | 1343 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
77e843a0 | 1344 | buf_addr = convert_memory_address (Pmode, buf_addr); |
d1ff492e | 1345 | rtx stack_save |
843d08a9 | 1346 | = gen_rtx_MEM (sa_mode, |
1347 | memory_address | |
1348 | (sa_mode, | |
29c05e22 | 1349 | plus_constant (Pmode, buf_addr, |
1350 | 2 * GET_MODE_SIZE (Pmode)))); | |
843d08a9 | 1351 | |
e9c97615 | 1352 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
843d08a9 | 1353 | } |
1354 | ||
5e3608d8 | 1355 | /* Expand a call to __builtin_prefetch. For a target that does not support |
1356 | data prefetch, evaluate the memory address argument in case it has side | |
1357 | effects. */ | |
1358 | ||
1359 | static void | |
c2f47e15 | 1360 | expand_builtin_prefetch (tree exp) |
5e3608d8 | 1361 | { |
1362 | tree arg0, arg1, arg2; | |
c2f47e15 | 1363 | int nargs; |
5e3608d8 | 1364 | rtx op0, op1, op2; |
1365 | ||
c2f47e15 | 1366 | if (!validate_arglist (exp, POINTER_TYPE, 0)) |
26a5cadb | 1367 | return; |
1368 | ||
c2f47e15 | 1369 | arg0 = CALL_EXPR_ARG (exp, 0); |
1370 | ||
26a5cadb | 1371 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
1372 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
1373 | locality). */ | |
c2f47e15 | 1374 | nargs = call_expr_nargs (exp); |
1375 | if (nargs > 1) | |
1376 | arg1 = CALL_EXPR_ARG (exp, 1); | |
26a5cadb | 1377 | else |
c2f47e15 | 1378 | arg1 = integer_zero_node; |
1379 | if (nargs > 2) | |
1380 | arg2 = CALL_EXPR_ARG (exp, 2); | |
1381 | else | |
2512209b | 1382 | arg2 = integer_three_node; |
5e3608d8 | 1383 | |
1384 | /* Argument 0 is an address. */ | |
1385 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
1386 | ||
1387 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
1388 | if (TREE_CODE (arg1) != INTEGER_CST) | |
1389 | { | |
07e3a3d2 | 1390 | error ("second argument to %<__builtin_prefetch%> must be a constant"); |
9342ee68 | 1391 | arg1 = integer_zero_node; |
5e3608d8 | 1392 | } |
8ec3c5c2 | 1393 | op1 = expand_normal (arg1); |
5e3608d8 | 1394 | /* Argument 1 must be either zero or one. */ |
1395 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
1396 | { | |
c3ceba8e | 1397 | warning (0, "invalid second argument to %<__builtin_prefetch%>;" |
07e3a3d2 | 1398 | " using zero"); |
5e3608d8 | 1399 | op1 = const0_rtx; |
1400 | } | |
1401 | ||
1402 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
1403 | if (TREE_CODE (arg2) != INTEGER_CST) | |
1404 | { | |
07e3a3d2 | 1405 | error ("third argument to %<__builtin_prefetch%> must be a constant"); |
5e3608d8 | 1406 | arg2 = integer_zero_node; |
1407 | } | |
8ec3c5c2 | 1408 | op2 = expand_normal (arg2); |
5e3608d8 | 1409 | /* Argument 2 must be 0, 1, 2, or 3. */ |
1410 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
1411 | { | |
c3ceba8e | 1412 | warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero"); |
5e3608d8 | 1413 | op2 = const0_rtx; |
1414 | } | |
1415 | ||
1d375a79 | 1416 | if (targetm.have_prefetch ()) |
5e3608d8 | 1417 | { |
8786db1e | 1418 | struct expand_operand ops[3]; |
1419 | ||
1420 | create_address_operand (&ops[0], op0); | |
1421 | create_integer_operand (&ops[1], INTVAL (op1)); | |
1422 | create_integer_operand (&ops[2], INTVAL (op2)); | |
1d375a79 | 1423 | if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops)) |
8786db1e | 1424 | return; |
5e3608d8 | 1425 | } |
0a534ba7 | 1426 | |
f0ce3b1f | 1427 | /* Don't do anything with direct references to volatile memory, but |
1428 | generate code to handle other side effects. */ | |
e16ceb8e | 1429 | if (!MEM_P (op0) && side_effects_p (op0)) |
f0ce3b1f | 1430 | emit_insn (op0); |
5e3608d8 | 1431 | } |
1432 | ||
f7c44134 | 1433 | /* Get a MEM rtx for expression EXP which is the address of an operand |
d8ae1baa | 1434 | to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is |
1435 | the maximum length of the block of memory that might be accessed or | |
1436 | NULL if unknown. */ | |
f7c44134 | 1437 | |
53800dbe | 1438 | static rtx |
d8ae1baa | 1439 | get_memory_rtx (tree exp, tree len) |
53800dbe | 1440 | { |
ad0a178f | 1441 | tree orig_exp = exp; |
1442 | rtx addr, mem; | |
ad0a178f | 1443 | |
1444 | /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived | |
1445 | from its expression, for expr->a.b only <variable>.a.b is recorded. */ | |
1446 | if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp)) | |
1447 | exp = TREE_OPERAND (exp, 0); | |
1448 | ||
1449 | addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL); | |
1450 | mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
2a631e19 | 1451 | |
f7c44134 | 1452 | /* Get an expression we can use to find the attributes to assign to MEM. |
5dd3f78f | 1453 | First remove any nops. */ |
72dd6141 | 1454 | while (CONVERT_EXPR_P (exp) |
f7c44134 | 1455 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
1456 | exp = TREE_OPERAND (exp, 0); | |
1457 | ||
5dd3f78f | 1458 | /* Build a MEM_REF representing the whole accessed area as a byte blob, |
1459 | (as builtin stringops may alias with anything). */ | |
1460 | exp = fold_build2 (MEM_REF, | |
1461 | build_array_type (char_type_node, | |
1462 | build_range_type (sizetype, | |
1463 | size_one_node, len)), | |
1464 | exp, build_int_cst (ptr_type_node, 0)); | |
1465 | ||
1466 | /* If the MEM_REF has no acceptable address, try to get the base object | |
1467 | from the original address we got, and build an all-aliasing | |
1468 | unknown-sized access to that one. */ | |
1469 | if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0))) | |
1470 | set_mem_attributes (mem, exp, 0); | |
1471 | else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR | |
1472 | && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0), | |
1473 | 0)))) | |
eec8e941 | 1474 | { |
5dd3f78f | 1475 | exp = build_fold_addr_expr (exp); |
1476 | exp = fold_build2 (MEM_REF, | |
1477 | build_array_type (char_type_node, | |
1478 | build_range_type (sizetype, | |
1479 | size_zero_node, | |
1480 | NULL)), | |
1481 | exp, build_int_cst (ptr_type_node, 0)); | |
a1a25d19 | 1482 | set_mem_attributes (mem, exp, 0); |
eec8e941 | 1483 | } |
5dd3f78f | 1484 | set_mem_alias_set (mem, 0); |
53800dbe | 1485 | return mem; |
1486 | } | |
1487 | \f | |
1488 | /* Built-in functions to perform an untyped call and return. */ | |
1489 | ||
3b9c3a16 | 1490 | #define apply_args_mode \ |
1491 | (this_target_builtins->x_apply_args_mode) | |
1492 | #define apply_result_mode \ | |
1493 | (this_target_builtins->x_apply_result_mode) | |
53800dbe | 1494 | |
53800dbe | 1495 | /* Return the size required for the block returned by __builtin_apply_args, |
1496 | and initialize apply_args_mode. */ | |
1497 | ||
1498 | static int | |
aecda0d6 | 1499 | apply_args_size (void) |
53800dbe | 1500 | { |
1501 | static int size = -1; | |
58e9ce8f | 1502 | int align; |
1503 | unsigned int regno; | |
53800dbe | 1504 | |
1505 | /* The values computed by this function never change. */ | |
1506 | if (size < 0) | |
1507 | { | |
1508 | /* The first value is the incoming arg-pointer. */ | |
1509 | size = GET_MODE_SIZE (Pmode); | |
1510 | ||
1511 | /* The second value is the structure value address unless this is | |
1512 | passed as an "invisible" first argument. */ | |
6812c89e | 1513 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1514 | size += GET_MODE_SIZE (Pmode); |
1515 | ||
1516 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1517 | if (FUNCTION_ARG_REGNO_P (regno)) | |
1518 | { | |
d8ba6ec1 | 1519 | fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno); |
0862b7e9 | 1520 | |
64db345d | 1521 | gcc_assert (mode != VOIDmode); |
53800dbe | 1522 | |
1523 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1524 | if (size % align != 0) | |
1525 | size = CEIL (size, align) * align; | |
53800dbe | 1526 | size += GET_MODE_SIZE (mode); |
1527 | apply_args_mode[regno] = mode; | |
1528 | } | |
1529 | else | |
1530 | { | |
d8ba6ec1 | 1531 | apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode); |
53800dbe | 1532 | } |
1533 | } | |
1534 | return size; | |
1535 | } | |
1536 | ||
1537 | /* Return the size required for the block returned by __builtin_apply, | |
1538 | and initialize apply_result_mode. */ | |
1539 | ||
1540 | static int | |
aecda0d6 | 1541 | apply_result_size (void) |
53800dbe | 1542 | { |
1543 | static int size = -1; | |
1544 | int align, regno; | |
53800dbe | 1545 | |
1546 | /* The values computed by this function never change. */ | |
1547 | if (size < 0) | |
1548 | { | |
1549 | size = 0; | |
1550 | ||
1551 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
e1ce1485 | 1552 | if (targetm.calls.function_value_regno_p (regno)) |
53800dbe | 1553 | { |
d8ba6ec1 | 1554 | fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno); |
0862b7e9 | 1555 | |
64db345d | 1556 | gcc_assert (mode != VOIDmode); |
53800dbe | 1557 | |
1558 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1559 | if (size % align != 0) | |
1560 | size = CEIL (size, align) * align; | |
1561 | size += GET_MODE_SIZE (mode); | |
1562 | apply_result_mode[regno] = mode; | |
1563 | } | |
1564 | else | |
d8ba6ec1 | 1565 | apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode); |
53800dbe | 1566 | |
1567 | /* Allow targets that use untyped_call and untyped_return to override | |
1568 | the size so that machine-specific information can be stored here. */ | |
1569 | #ifdef APPLY_RESULT_SIZE | |
1570 | size = APPLY_RESULT_SIZE; | |
1571 | #endif | |
1572 | } | |
1573 | return size; | |
1574 | } | |
1575 | ||
53800dbe | 1576 | /* Create a vector describing the result block RESULT. If SAVEP is true, |
1577 | the result block is used to save the values; otherwise it is used to | |
1578 | restore the values. */ | |
1579 | ||
1580 | static rtx | |
aecda0d6 | 1581 | result_vector (int savep, rtx result) |
53800dbe | 1582 | { |
1583 | int regno, size, align, nelts; | |
d8ba6ec1 | 1584 | fixed_size_mode mode; |
53800dbe | 1585 | rtx reg, mem; |
364c0c59 | 1586 | rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER); |
bf8e3599 | 1587 | |
53800dbe | 1588 | size = nelts = 0; |
1589 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1590 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1591 | { | |
1592 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1593 | if (size % align != 0) | |
1594 | size = CEIL (size, align) * align; | |
1595 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
e513d163 | 1596 | mem = adjust_address (result, mode, size); |
53800dbe | 1597 | savevec[nelts++] = (savep |
d1f9b275 | 1598 | ? gen_rtx_SET (mem, reg) |
1599 | : gen_rtx_SET (reg, mem)); | |
53800dbe | 1600 | size += GET_MODE_SIZE (mode); |
1601 | } | |
1602 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1603 | } | |
53800dbe | 1604 | |
1605 | /* Save the state required to perform an untyped call with the same | |
1606 | arguments as were passed to the current function. */ | |
1607 | ||
1608 | static rtx | |
aecda0d6 | 1609 | expand_builtin_apply_args_1 (void) |
53800dbe | 1610 | { |
1c7e61a7 | 1611 | rtx registers, tem; |
53800dbe | 1612 | int size, align, regno; |
d8ba6ec1 | 1613 | fixed_size_mode mode; |
6812c89e | 1614 | rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1); |
53800dbe | 1615 | |
1616 | /* Create a block where the arg-pointer, structure value address, | |
1617 | and argument registers can be saved. */ | |
1618 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1619 | ||
1620 | /* Walk past the arg-pointer and structure value address. */ | |
1621 | size = GET_MODE_SIZE (Pmode); | |
6812c89e | 1622 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1623 | size += GET_MODE_SIZE (Pmode); |
1624 | ||
1625 | /* Save each register used in calling a function to the block. */ | |
1626 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1627 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1628 | { | |
53800dbe | 1629 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; |
1630 | if (size % align != 0) | |
1631 | size = CEIL (size, align) * align; | |
1632 | ||
1633 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1634 | ||
e513d163 | 1635 | emit_move_insn (adjust_address (registers, mode, size), tem); |
53800dbe | 1636 | size += GET_MODE_SIZE (mode); |
1637 | } | |
1638 | ||
1639 | /* Save the arg pointer to the block. */ | |
27a7a23a | 1640 | tem = copy_to_reg (crtl->args.internal_arg_pointer); |
1c7e61a7 | 1641 | /* We need the pointer as the caller actually passed them to us, not |
9d4b544c | 1642 | as we might have pretended they were passed. Make sure it's a valid |
1643 | operand, as emit_move_insn isn't expected to handle a PLUS. */ | |
3764c94e | 1644 | if (STACK_GROWS_DOWNWARD) |
1645 | tem | |
1646 | = force_operand (plus_constant (Pmode, tem, | |
1647 | crtl->args.pretend_args_size), | |
1648 | NULL_RTX); | |
1c7e61a7 | 1649 | emit_move_insn (adjust_address (registers, Pmode, 0), tem); |
0862b7e9 | 1650 | |
53800dbe | 1651 | size = GET_MODE_SIZE (Pmode); |
1652 | ||
1653 | /* Save the structure value address unless this is passed as an | |
1654 | "invisible" first argument. */ | |
45550790 | 1655 | if (struct_incoming_value) |
53800dbe | 1656 | { |
e513d163 | 1657 | emit_move_insn (adjust_address (registers, Pmode, size), |
45550790 | 1658 | copy_to_reg (struct_incoming_value)); |
53800dbe | 1659 | size += GET_MODE_SIZE (Pmode); |
1660 | } | |
1661 | ||
1662 | /* Return the address of the block. */ | |
1663 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1664 | } | |
1665 | ||
1666 | /* __builtin_apply_args returns block of memory allocated on | |
1667 | the stack into which is stored the arg pointer, structure | |
1668 | value address, static chain, and all the registers that might | |
1669 | possibly be used in performing a function call. The code is | |
1670 | moved to the start of the function so the incoming values are | |
1671 | saved. */ | |
27d0c333 | 1672 | |
53800dbe | 1673 | static rtx |
aecda0d6 | 1674 | expand_builtin_apply_args (void) |
53800dbe | 1675 | { |
1676 | /* Don't do __builtin_apply_args more than once in a function. | |
1677 | Save the result of the first call and reuse it. */ | |
1678 | if (apply_args_value != 0) | |
1679 | return apply_args_value; | |
1680 | { | |
1681 | /* When this function is called, it means that registers must be | |
1682 | saved on entry to this function. So we migrate the | |
1683 | call to the first insn of this function. */ | |
1684 | rtx temp; | |
53800dbe | 1685 | |
1686 | start_sequence (); | |
1687 | temp = expand_builtin_apply_args_1 (); | |
9ed997be | 1688 | rtx_insn *seq = get_insns (); |
53800dbe | 1689 | end_sequence (); |
1690 | ||
1691 | apply_args_value = temp; | |
1692 | ||
31d3e01c | 1693 | /* Put the insns after the NOTE that starts the function. |
1694 | If this is inside a start_sequence, make the outer-level insn | |
53800dbe | 1695 | chain current, so the code is placed at the start of the |
0ef1a651 | 1696 | function. If internal_arg_pointer is a non-virtual pseudo, |
1697 | it needs to be placed after the function that initializes | |
1698 | that pseudo. */ | |
53800dbe | 1699 | push_topmost_sequence (); |
0ef1a651 | 1700 | if (REG_P (crtl->args.internal_arg_pointer) |
1701 | && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER) | |
1702 | emit_insn_before (seq, parm_birth_insn); | |
1703 | else | |
1704 | emit_insn_before (seq, NEXT_INSN (entry_of_function ())); | |
53800dbe | 1705 | pop_topmost_sequence (); |
1706 | return temp; | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | /* Perform an untyped call and save the state required to perform an | |
1711 | untyped return of whatever value was returned by the given function. */ | |
1712 | ||
1713 | static rtx | |
aecda0d6 | 1714 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
53800dbe | 1715 | { |
1716 | int size, align, regno; | |
d8ba6ec1 | 1717 | fixed_size_mode mode; |
1e0c0b35 | 1718 | rtx incoming_args, result, reg, dest, src; |
1719 | rtx_call_insn *call_insn; | |
53800dbe | 1720 | rtx old_stack_level = 0; |
1721 | rtx call_fusage = 0; | |
6812c89e | 1722 | rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0); |
53800dbe | 1723 | |
85d654dd | 1724 | arguments = convert_memory_address (Pmode, arguments); |
726ec87c | 1725 | |
53800dbe | 1726 | /* Create a block where the return registers can be saved. */ |
1727 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1728 | ||
53800dbe | 1729 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1730 | incoming_args = gen_reg_rtx (Pmode); | |
726ec87c | 1731 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
3764c94e | 1732 | if (!STACK_GROWS_DOWNWARD) |
1733 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, | |
1734 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
53800dbe | 1735 | |
04a46d40 | 1736 | /* Push a new argument block and copy the arguments. Do not allow |
1737 | the (potential) memcpy call below to interfere with our stack | |
1738 | manipulations. */ | |
53800dbe | 1739 | do_pending_stack_adjust (); |
04a46d40 | 1740 | NO_DEFER_POP; |
53800dbe | 1741 | |
2358393e | 1742 | /* Save the stack with nonlocal if available. */ |
71512c05 | 1743 | if (targetm.have_save_stack_nonlocal ()) |
e9c97615 | 1744 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level); |
53800dbe | 1745 | else |
e9c97615 | 1746 | emit_stack_save (SAVE_BLOCK, &old_stack_level); |
53800dbe | 1747 | |
59647703 | 1748 | /* Allocate a block of memory onto the stack and copy the memory |
990495a7 | 1749 | arguments to the outgoing arguments address. We can pass TRUE |
1750 | as the 4th argument because we just saved the stack pointer | |
1751 | and will restore it right after the call. */ | |
2b34677f | 1752 | allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true); |
27a7a23a | 1753 | |
1754 | /* Set DRAP flag to true, even though allocate_dynamic_stack_space | |
1755 | may have already set current_function_calls_alloca to true. | |
1756 | current_function_calls_alloca won't be set if argsize is zero, | |
1757 | so we have to guarantee need_drap is true here. */ | |
1758 | if (SUPPORTS_STACK_ALIGNMENT) | |
1759 | crtl->need_drap = true; | |
1760 | ||
59647703 | 1761 | dest = virtual_outgoing_args_rtx; |
3764c94e | 1762 | if (!STACK_GROWS_DOWNWARD) |
1763 | { | |
1764 | if (CONST_INT_P (argsize)) | |
1765 | dest = plus_constant (Pmode, dest, -INTVAL (argsize)); | |
1766 | else | |
1767 | dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize)); | |
1768 | } | |
2a631e19 | 1769 | dest = gen_rtx_MEM (BLKmode, dest); |
1770 | set_mem_align (dest, PARM_BOUNDARY); | |
1771 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1772 | set_mem_align (src, PARM_BOUNDARY); | |
0378dbdc | 1773 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
53800dbe | 1774 | |
1775 | /* Refer to the argument block. */ | |
1776 | apply_args_size (); | |
1777 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
2a631e19 | 1778 | set_mem_align (arguments, PARM_BOUNDARY); |
53800dbe | 1779 | |
1780 | /* Walk past the arg-pointer and structure value address. */ | |
1781 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1782 | if (struct_value) |
53800dbe | 1783 | size += GET_MODE_SIZE (Pmode); |
1784 | ||
1785 | /* Restore each of the registers previously saved. Make USE insns | |
1786 | for each of these registers for use in making the call. */ | |
1787 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1788 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1789 | { | |
1790 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1791 | if (size % align != 0) | |
1792 | size = CEIL (size, align) * align; | |
1793 | reg = gen_rtx_REG (mode, regno); | |
e513d163 | 1794 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
53800dbe | 1795 | use_reg (&call_fusage, reg); |
1796 | size += GET_MODE_SIZE (mode); | |
1797 | } | |
1798 | ||
1799 | /* Restore the structure value address unless this is passed as an | |
1800 | "invisible" first argument. */ | |
1801 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1802 | if (struct_value) |
53800dbe | 1803 | { |
1804 | rtx value = gen_reg_rtx (Pmode); | |
e513d163 | 1805 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
45550790 | 1806 | emit_move_insn (struct_value, value); |
8ad4c111 | 1807 | if (REG_P (struct_value)) |
45550790 | 1808 | use_reg (&call_fusage, struct_value); |
53800dbe | 1809 | size += GET_MODE_SIZE (Pmode); |
1810 | } | |
1811 | ||
1812 | /* All arguments and registers used for the call are set up by now! */ | |
82c7907c | 1813 | function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0); |
53800dbe | 1814 | |
1815 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1816 | and we don't want to load it into a register as an optimization, | |
1817 | because prepare_call_address already did it if it should be done. */ | |
1818 | if (GET_CODE (function) != SYMBOL_REF) | |
1819 | function = memory_address (FUNCTION_MODE, function); | |
1820 | ||
1821 | /* Generate the actual call instruction and save the return value. */ | |
1d99ab0a | 1822 | if (targetm.have_untyped_call ()) |
1823 | { | |
1824 | rtx mem = gen_rtx_MEM (FUNCTION_MODE, function); | |
1825 | emit_call_insn (targetm.gen_untyped_call (mem, result, | |
1826 | result_vector (1, result))); | |
1827 | } | |
7f265a08 | 1828 | else if (targetm.have_call_value ()) |
53800dbe | 1829 | { |
1830 | rtx valreg = 0; | |
1831 | ||
1832 | /* Locate the unique return register. It is not possible to | |
1833 | express a call that sets more than one return register using | |
1834 | call_value; use untyped_call for that. In fact, untyped_call | |
1835 | only needs to save the return registers in the given block. */ | |
1836 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1837 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1838 | { | |
7f265a08 | 1839 | gcc_assert (!valreg); /* have_untyped_call required. */ |
7d3f6cc7 | 1840 | |
53800dbe | 1841 | valreg = gen_rtx_REG (mode, regno); |
1842 | } | |
1843 | ||
7f265a08 | 1844 | emit_insn (targetm.gen_call_value (valreg, |
1845 | gen_rtx_MEM (FUNCTION_MODE, function), | |
1846 | const0_rtx, NULL_RTX, const0_rtx)); | |
53800dbe | 1847 | |
e513d163 | 1848 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
53800dbe | 1849 | } |
1850 | else | |
64db345d | 1851 | gcc_unreachable (); |
53800dbe | 1852 | |
d5f9786f | 1853 | /* Find the CALL insn we just emitted, and attach the register usage |
1854 | information. */ | |
1855 | call_insn = last_call_insn (); | |
1856 | add_function_usage_to (call_insn, call_fusage); | |
53800dbe | 1857 | |
1858 | /* Restore the stack. */ | |
71512c05 | 1859 | if (targetm.have_save_stack_nonlocal ()) |
e9c97615 | 1860 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level); |
53800dbe | 1861 | else |
e9c97615 | 1862 | emit_stack_restore (SAVE_BLOCK, old_stack_level); |
9af5ce0c | 1863 | fixup_args_size_notes (call_insn, get_last_insn (), 0); |
53800dbe | 1864 | |
04a46d40 | 1865 | OK_DEFER_POP; |
1866 | ||
53800dbe | 1867 | /* Return the address of the result block. */ |
85d654dd | 1868 | result = copy_addr_to_reg (XEXP (result, 0)); |
1869 | return convert_memory_address (ptr_mode, result); | |
53800dbe | 1870 | } |
1871 | ||
1872 | /* Perform an untyped return. */ | |
1873 | ||
1874 | static void | |
aecda0d6 | 1875 | expand_builtin_return (rtx result) |
53800dbe | 1876 | { |
1877 | int size, align, regno; | |
d8ba6ec1 | 1878 | fixed_size_mode mode; |
53800dbe | 1879 | rtx reg; |
57c26b3a | 1880 | rtx_insn *call_fusage = 0; |
53800dbe | 1881 | |
85d654dd | 1882 | result = convert_memory_address (Pmode, result); |
726ec87c | 1883 | |
53800dbe | 1884 | apply_result_size (); |
1885 | result = gen_rtx_MEM (BLKmode, result); | |
1886 | ||
1d99ab0a | 1887 | if (targetm.have_untyped_return ()) |
53800dbe | 1888 | { |
1d99ab0a | 1889 | rtx vector = result_vector (0, result); |
1890 | emit_jump_insn (targetm.gen_untyped_return (result, vector)); | |
53800dbe | 1891 | emit_barrier (); |
1892 | return; | |
1893 | } | |
53800dbe | 1894 | |
1895 | /* Restore the return value and note that each value is used. */ | |
1896 | size = 0; | |
1897 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1898 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1899 | { | |
1900 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1901 | if (size % align != 0) | |
1902 | size = CEIL (size, align) * align; | |
1903 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
e513d163 | 1904 | emit_move_insn (reg, adjust_address (result, mode, size)); |
53800dbe | 1905 | |
1906 | push_to_sequence (call_fusage); | |
18b42941 | 1907 | emit_use (reg); |
53800dbe | 1908 | call_fusage = get_insns (); |
1909 | end_sequence (); | |
1910 | size += GET_MODE_SIZE (mode); | |
1911 | } | |
1912 | ||
1913 | /* Put the USE insns before the return. */ | |
31d3e01c | 1914 | emit_insn (call_fusage); |
53800dbe | 1915 | |
1916 | /* Return whatever values was restored by jumping directly to the end | |
1917 | of the function. */ | |
62380d2d | 1918 | expand_naked_return (); |
53800dbe | 1919 | } |
1920 | ||
539a3a92 | 1921 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
27d0c333 | 1922 | |
539a3a92 | 1923 | static enum type_class |
aecda0d6 | 1924 | type_to_class (tree type) |
539a3a92 | 1925 | { |
1926 | switch (TREE_CODE (type)) | |
1927 | { | |
1928 | case VOID_TYPE: return void_type_class; | |
1929 | case INTEGER_TYPE: return integer_type_class; | |
539a3a92 | 1930 | case ENUMERAL_TYPE: return enumeral_type_class; |
1931 | case BOOLEAN_TYPE: return boolean_type_class; | |
1932 | case POINTER_TYPE: return pointer_type_class; | |
1933 | case REFERENCE_TYPE: return reference_type_class; | |
1934 | case OFFSET_TYPE: return offset_type_class; | |
1935 | case REAL_TYPE: return real_type_class; | |
1936 | case COMPLEX_TYPE: return complex_type_class; | |
1937 | case FUNCTION_TYPE: return function_type_class; | |
1938 | case METHOD_TYPE: return method_type_class; | |
1939 | case RECORD_TYPE: return record_type_class; | |
1940 | case UNION_TYPE: | |
1941 | case QUAL_UNION_TYPE: return union_type_class; | |
1942 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1943 | ? string_type_class : array_type_class); | |
539a3a92 | 1944 | case LANG_TYPE: return lang_type_class; |
1945 | default: return no_type_class; | |
1946 | } | |
1947 | } | |
bf8e3599 | 1948 | |
c2f47e15 | 1949 | /* Expand a call EXP to __builtin_classify_type. */ |
27d0c333 | 1950 | |
53800dbe | 1951 | static rtx |
c2f47e15 | 1952 | expand_builtin_classify_type (tree exp) |
53800dbe | 1953 | { |
c2f47e15 | 1954 | if (call_expr_nargs (exp)) |
1955 | return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))); | |
53800dbe | 1956 | return GEN_INT (no_type_class); |
1957 | } | |
1958 | ||
8c32188e | 1959 | /* This helper macro, meant to be used in mathfn_built_in below, determines |
1960 | which among a set of builtin math functions is appropriate for a given type | |
1961 | mode. The `F' (float) and `L' (long double) are automatically generated | |
1962 | from the 'double' case. If a function supports the _Float<N> and _Float<N>X | |
1963 | types, there are additional types that are considered with 'F32', 'F64', | |
1964 | 'F128', etc. suffixes. */ | |
e3240774 | 1965 | #define CASE_MATHFN(MATHFN) \ |
1966 | CASE_CFN_##MATHFN: \ | |
1967 | fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \ | |
1968 | fcodel = BUILT_IN_##MATHFN##L ; break; | |
8c32188e | 1969 | /* Similar to the above, but also add support for the _Float<N> and _Float<N>X |
1970 | types. */ | |
1971 | #define CASE_MATHFN_FLOATN(MATHFN) \ | |
1972 | CASE_CFN_##MATHFN: \ | |
1973 | fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \ | |
1974 | fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \ | |
1975 | fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \ | |
1976 | fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \ | |
1977 | fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\ | |
1978 | break; | |
cd2656b0 | 1979 | /* Similar to above, but appends _R after any F/L suffix. */ |
e3240774 | 1980 | #define CASE_MATHFN_REENT(MATHFN) \ |
1981 | case CFN_BUILT_IN_##MATHFN##_R: \ | |
1982 | case CFN_BUILT_IN_##MATHFN##F_R: \ | |
1983 | case CFN_BUILT_IN_##MATHFN##L_R: \ | |
1984 | fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \ | |
1985 | fcodel = BUILT_IN_##MATHFN##L_R ; break; | |
07976da7 | 1986 | |
6c21be92 | 1987 | /* Return a function equivalent to FN but operating on floating-point |
1988 | values of type TYPE, or END_BUILTINS if no such function exists. | |
e3240774 | 1989 | This is purely an operation on function codes; it does not guarantee |
1990 | that the target actually has an implementation of the function. */ | |
c319d56a | 1991 | |
6c21be92 | 1992 | static built_in_function |
e3240774 | 1993 | mathfn_built_in_2 (tree type, combined_fn fn) |
0a68165a | 1994 | { |
8c32188e | 1995 | tree mtype; |
6c21be92 | 1996 | built_in_function fcode, fcodef, fcodel; |
8c32188e | 1997 | built_in_function fcodef16 = END_BUILTINS; |
1998 | built_in_function fcodef32 = END_BUILTINS; | |
1999 | built_in_function fcodef64 = END_BUILTINS; | |
2000 | built_in_function fcodef128 = END_BUILTINS; | |
2001 | built_in_function fcodef32x = END_BUILTINS; | |
2002 | built_in_function fcodef64x = END_BUILTINS; | |
2003 | built_in_function fcodef128x = END_BUILTINS; | |
07976da7 | 2004 | |
2005 | switch (fn) | |
2006 | { | |
e3240774 | 2007 | CASE_MATHFN (ACOS) |
2008 | CASE_MATHFN (ACOSH) | |
2009 | CASE_MATHFN (ASIN) | |
2010 | CASE_MATHFN (ASINH) | |
2011 | CASE_MATHFN (ATAN) | |
2012 | CASE_MATHFN (ATAN2) | |
2013 | CASE_MATHFN (ATANH) | |
2014 | CASE_MATHFN (CBRT) | |
054e9558 | 2015 | CASE_MATHFN_FLOATN (CEIL) |
e3240774 | 2016 | CASE_MATHFN (CEXPI) |
8c32188e | 2017 | CASE_MATHFN_FLOATN (COPYSIGN) |
e3240774 | 2018 | CASE_MATHFN (COS) |
2019 | CASE_MATHFN (COSH) | |
2020 | CASE_MATHFN (DREM) | |
2021 | CASE_MATHFN (ERF) | |
2022 | CASE_MATHFN (ERFC) | |
2023 | CASE_MATHFN (EXP) | |
2024 | CASE_MATHFN (EXP10) | |
2025 | CASE_MATHFN (EXP2) | |
2026 | CASE_MATHFN (EXPM1) | |
2027 | CASE_MATHFN (FABS) | |
2028 | CASE_MATHFN (FDIM) | |
054e9558 | 2029 | CASE_MATHFN_FLOATN (FLOOR) |
8c32188e | 2030 | CASE_MATHFN_FLOATN (FMA) |
2031 | CASE_MATHFN_FLOATN (FMAX) | |
2032 | CASE_MATHFN_FLOATN (FMIN) | |
e3240774 | 2033 | CASE_MATHFN (FMOD) |
2034 | CASE_MATHFN (FREXP) | |
2035 | CASE_MATHFN (GAMMA) | |
2036 | CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ | |
2037 | CASE_MATHFN (HUGE_VAL) | |
2038 | CASE_MATHFN (HYPOT) | |
2039 | CASE_MATHFN (ILOGB) | |
2040 | CASE_MATHFN (ICEIL) | |
2041 | CASE_MATHFN (IFLOOR) | |
2042 | CASE_MATHFN (INF) | |
2043 | CASE_MATHFN (IRINT) | |
2044 | CASE_MATHFN (IROUND) | |
2045 | CASE_MATHFN (ISINF) | |
2046 | CASE_MATHFN (J0) | |
2047 | CASE_MATHFN (J1) | |
2048 | CASE_MATHFN (JN) | |
2049 | CASE_MATHFN (LCEIL) | |
2050 | CASE_MATHFN (LDEXP) | |
2051 | CASE_MATHFN (LFLOOR) | |
2052 | CASE_MATHFN (LGAMMA) | |
2053 | CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ | |
2054 | CASE_MATHFN (LLCEIL) | |
2055 | CASE_MATHFN (LLFLOOR) | |
2056 | CASE_MATHFN (LLRINT) | |
2057 | CASE_MATHFN (LLROUND) | |
2058 | CASE_MATHFN (LOG) | |
2059 | CASE_MATHFN (LOG10) | |
2060 | CASE_MATHFN (LOG1P) | |
2061 | CASE_MATHFN (LOG2) | |
2062 | CASE_MATHFN (LOGB) | |
2063 | CASE_MATHFN (LRINT) | |
2064 | CASE_MATHFN (LROUND) | |
2065 | CASE_MATHFN (MODF) | |
2066 | CASE_MATHFN (NAN) | |
2067 | CASE_MATHFN (NANS) | |
054e9558 | 2068 | CASE_MATHFN_FLOATN (NEARBYINT) |
e3240774 | 2069 | CASE_MATHFN (NEXTAFTER) |
2070 | CASE_MATHFN (NEXTTOWARD) | |
2071 | CASE_MATHFN (POW) | |
2072 | CASE_MATHFN (POWI) | |
2073 | CASE_MATHFN (POW10) | |
2074 | CASE_MATHFN (REMAINDER) | |
2075 | CASE_MATHFN (REMQUO) | |
054e9558 | 2076 | CASE_MATHFN_FLOATN (RINT) |
2077 | CASE_MATHFN_FLOATN (ROUND) | |
e3240774 | 2078 | CASE_MATHFN (SCALB) |
2079 | CASE_MATHFN (SCALBLN) | |
2080 | CASE_MATHFN (SCALBN) | |
2081 | CASE_MATHFN (SIGNBIT) | |
2082 | CASE_MATHFN (SIGNIFICAND) | |
2083 | CASE_MATHFN (SIN) | |
2084 | CASE_MATHFN (SINCOS) | |
2085 | CASE_MATHFN (SINH) | |
8c32188e | 2086 | CASE_MATHFN_FLOATN (SQRT) |
e3240774 | 2087 | CASE_MATHFN (TAN) |
2088 | CASE_MATHFN (TANH) | |
2089 | CASE_MATHFN (TGAMMA) | |
054e9558 | 2090 | CASE_MATHFN_FLOATN (TRUNC) |
e3240774 | 2091 | CASE_MATHFN (Y0) |
2092 | CASE_MATHFN (Y1) | |
2093 | CASE_MATHFN (YN) | |
07976da7 | 2094 | |
e3240774 | 2095 | default: |
2096 | return END_BUILTINS; | |
2097 | } | |
07976da7 | 2098 | |
8c32188e | 2099 | mtype = TYPE_MAIN_VARIANT (type); |
2100 | if (mtype == double_type_node) | |
6c21be92 | 2101 | return fcode; |
8c32188e | 2102 | else if (mtype == float_type_node) |
6c21be92 | 2103 | return fcodef; |
8c32188e | 2104 | else if (mtype == long_double_type_node) |
6c21be92 | 2105 | return fcodel; |
8c32188e | 2106 | else if (mtype == float16_type_node) |
2107 | return fcodef16; | |
2108 | else if (mtype == float32_type_node) | |
2109 | return fcodef32; | |
2110 | else if (mtype == float64_type_node) | |
2111 | return fcodef64; | |
2112 | else if (mtype == float128_type_node) | |
2113 | return fcodef128; | |
2114 | else if (mtype == float32x_type_node) | |
2115 | return fcodef32x; | |
2116 | else if (mtype == float64x_type_node) | |
2117 | return fcodef64x; | |
2118 | else if (mtype == float128x_type_node) | |
2119 | return fcodef128x; | |
07976da7 | 2120 | else |
6c21be92 | 2121 | return END_BUILTINS; |
2122 | } | |
2123 | ||
2124 | /* Return mathematic function equivalent to FN but operating directly on TYPE, | |
2125 | if available. If IMPLICIT_P is true use the implicit builtin declaration, | |
2126 | otherwise use the explicit declaration. If we can't do the conversion, | |
2127 | return null. */ | |
2128 | ||
2129 | static tree | |
e3240774 | 2130 | mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p) |
6c21be92 | 2131 | { |
2132 | built_in_function fcode2 = mathfn_built_in_2 (type, fn); | |
2133 | if (fcode2 == END_BUILTINS) | |
c2f47e15 | 2134 | return NULL_TREE; |
b9a16870 | 2135 | |
2136 | if (implicit_p && !builtin_decl_implicit_p (fcode2)) | |
2137 | return NULL_TREE; | |
2138 | ||
2139 | return builtin_decl_explicit (fcode2); | |
0a68165a | 2140 | } |
2141 | ||
e3240774 | 2142 | /* Like mathfn_built_in_1, but always use the implicit array. */ |
c319d56a | 2143 | |
2144 | tree | |
e3240774 | 2145 | mathfn_built_in (tree type, combined_fn fn) |
c319d56a | 2146 | { |
2147 | return mathfn_built_in_1 (type, fn, /*implicit=*/ 1); | |
2148 | } | |
2149 | ||
e3240774 | 2150 | /* Like mathfn_built_in_1, but take a built_in_function and |
2151 | always use the implicit array. */ | |
2152 | ||
2153 | tree | |
2154 | mathfn_built_in (tree type, enum built_in_function fn) | |
2155 | { | |
2156 | return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1); | |
2157 | } | |
2158 | ||
1f24b8e9 | 2159 | /* If BUILT_IN_NORMAL function FNDECL has an associated internal function, |
2160 | return its code, otherwise return IFN_LAST. Note that this function | |
2161 | only tests whether the function is defined in internals.def, not whether | |
2162 | it is actually available on the target. */ | |
2163 | ||
2164 | internal_fn | |
2165 | associated_internal_fn (tree fndecl) | |
2166 | { | |
2167 | gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL); | |
2168 | tree return_type = TREE_TYPE (TREE_TYPE (fndecl)); | |
2169 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2170 | { | |
2171 | #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \ | |
2172 | CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
8c32188e | 2173 | #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \ |
2174 | CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \ | |
2175 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME; | |
c9452b7c | 2176 | #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \ |
2177 | CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
1f24b8e9 | 2178 | #include "internal-fn.def" |
2179 | ||
2180 | CASE_FLT_FN (BUILT_IN_POW10): | |
2181 | return IFN_EXP10; | |
2182 | ||
2183 | CASE_FLT_FN (BUILT_IN_DREM): | |
2184 | return IFN_REMAINDER; | |
2185 | ||
2186 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
2187 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
2188 | if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2) | |
2189 | return IFN_LDEXP; | |
2190 | return IFN_LAST; | |
2191 | ||
2192 | default: | |
2193 | return IFN_LAST; | |
2194 | } | |
2195 | } | |
2196 | ||
2197 | /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced | |
2198 | on the current target by a call to an internal function, return the | |
2199 | code of that internal function, otherwise return IFN_LAST. The caller | |
2200 | is responsible for ensuring that any side-effects of the built-in | |
2201 | call are dealt with correctly. E.g. if CALL sets errno, the caller | |
2202 | must decide that the errno result isn't needed or make it available | |
2203 | in some other way. */ | |
2204 | ||
2205 | internal_fn | |
2206 | replacement_internal_fn (gcall *call) | |
2207 | { | |
2208 | if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)) | |
2209 | { | |
2210 | internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call)); | |
2211 | if (ifn != IFN_LAST) | |
2212 | { | |
2213 | tree_pair types = direct_internal_fn_types (ifn, call); | |
acdfe9e0 | 2214 | optimization_type opt_type = bb_optimization_type (gimple_bb (call)); |
2215 | if (direct_internal_fn_supported_p (ifn, types, opt_type)) | |
1f24b8e9 | 2216 | return ifn; |
2217 | } | |
2218 | } | |
2219 | return IFN_LAST; | |
2220 | } | |
2221 | ||
7e0713b1 | 2222 | /* Expand a call to the builtin trinary math functions (fma). |
2223 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
2224 | function in-line. EXP is the expression that is a call to the builtin | |
2225 | function; if convenient, the result should be placed in TARGET. | |
2226 | SUBTARGET may be used as the target for computing one of EXP's | |
2227 | operands. */ | |
2228 | ||
2229 | static rtx | |
2230 | expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget) | |
2231 | { | |
2232 | optab builtin_optab; | |
1e0c0b35 | 2233 | rtx op0, op1, op2, result; |
2234 | rtx_insn *insns; | |
7e0713b1 | 2235 | tree fndecl = get_callee_fndecl (exp); |
2236 | tree arg0, arg1, arg2; | |
3754d046 | 2237 | machine_mode mode; |
7e0713b1 | 2238 | |
2239 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
2240 | return NULL_RTX; | |
2241 | ||
2242 | arg0 = CALL_EXPR_ARG (exp, 0); | |
2243 | arg1 = CALL_EXPR_ARG (exp, 1); | |
2244 | arg2 = CALL_EXPR_ARG (exp, 2); | |
2245 | ||
2246 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2247 | { | |
2248 | CASE_FLT_FN (BUILT_IN_FMA): | |
8c32188e | 2249 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA): |
7e0713b1 | 2250 | builtin_optab = fma_optab; break; |
2251 | default: | |
2252 | gcc_unreachable (); | |
2253 | } | |
2254 | ||
2255 | /* Make a suitable register to place result in. */ | |
2256 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2257 | ||
2258 | /* Before working hard, check whether the instruction is available. */ | |
2259 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) | |
2260 | return NULL_RTX; | |
2261 | ||
de2e453e | 2262 | result = gen_reg_rtx (mode); |
7e0713b1 | 2263 | |
2264 | /* Always stabilize the argument list. */ | |
2265 | CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0); | |
2266 | CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1); | |
2267 | CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2); | |
2268 | ||
2269 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); | |
2270 | op1 = expand_normal (arg1); | |
2271 | op2 = expand_normal (arg2); | |
2272 | ||
2273 | start_sequence (); | |
2274 | ||
de2e453e | 2275 | /* Compute into RESULT. |
2276 | Set RESULT to wherever the result comes back. */ | |
2277 | result = expand_ternary_op (mode, builtin_optab, op0, op1, op2, | |
2278 | result, 0); | |
7e0713b1 | 2279 | |
2280 | /* If we were unable to expand via the builtin, stop the sequence | |
2281 | (without outputting the insns) and call to the library function | |
2282 | with the stabilized argument list. */ | |
de2e453e | 2283 | if (result == 0) |
7e0713b1 | 2284 | { |
2285 | end_sequence (); | |
2286 | return expand_call (exp, target, target == const0_rtx); | |
2287 | } | |
2288 | ||
2289 | /* Output the entire sequence. */ | |
2290 | insns = get_insns (); | |
2291 | end_sequence (); | |
2292 | emit_insn (insns); | |
2293 | ||
de2e453e | 2294 | return result; |
7e0713b1 | 2295 | } |
2296 | ||
6b43bae4 | 2297 | /* Expand a call to the builtin sin and cos math functions. |
c2f47e15 | 2298 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
6b43bae4 | 2299 | function in-line. EXP is the expression that is a call to the builtin |
2300 | function; if convenient, the result should be placed in TARGET. | |
2301 | SUBTARGET may be used as the target for computing one of EXP's | |
2302 | operands. */ | |
2303 | ||
2304 | static rtx | |
2305 | expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget) | |
2306 | { | |
2307 | optab builtin_optab; | |
1e0c0b35 | 2308 | rtx op0; |
2309 | rtx_insn *insns; | |
6b43bae4 | 2310 | tree fndecl = get_callee_fndecl (exp); |
3754d046 | 2311 | machine_mode mode; |
abfea505 | 2312 | tree arg; |
6b43bae4 | 2313 | |
c2f47e15 | 2314 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2315 | return NULL_RTX; | |
6b43bae4 | 2316 | |
c2f47e15 | 2317 | arg = CALL_EXPR_ARG (exp, 0); |
6b43bae4 | 2318 | |
2319 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2320 | { | |
4f35b1fc | 2321 | CASE_FLT_FN (BUILT_IN_SIN): |
2322 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 2323 | builtin_optab = sincos_optab; break; |
2324 | default: | |
64db345d | 2325 | gcc_unreachable (); |
6b43bae4 | 2326 | } |
2327 | ||
2328 | /* Make a suitable register to place result in. */ | |
2329 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2330 | ||
6b43bae4 | 2331 | /* Check if sincos insn is available, otherwise fallback |
0bed3869 | 2332 | to sin or cos insn. */ |
d6bf3b14 | 2333 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) |
6b43bae4 | 2334 | switch (DECL_FUNCTION_CODE (fndecl)) |
2335 | { | |
4f35b1fc | 2336 | CASE_FLT_FN (BUILT_IN_SIN): |
6b43bae4 | 2337 | builtin_optab = sin_optab; break; |
4f35b1fc | 2338 | CASE_FLT_FN (BUILT_IN_COS): |
6b43bae4 | 2339 | builtin_optab = cos_optab; break; |
2340 | default: | |
64db345d | 2341 | gcc_unreachable (); |
6b43bae4 | 2342 | } |
6b43bae4 | 2343 | |
2344 | /* Before working hard, check whether the instruction is available. */ | |
d6bf3b14 | 2345 | if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing) |
6b43bae4 | 2346 | { |
de2e453e | 2347 | rtx result = gen_reg_rtx (mode); |
6b43bae4 | 2348 | |
2349 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2350 | need to expand the argument again. This way, we will not perform | |
2351 | side-effects more the once. */ | |
abfea505 | 2352 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
6b43bae4 | 2353 | |
1db6d067 | 2354 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
6b43bae4 | 2355 | |
6b43bae4 | 2356 | start_sequence (); |
2357 | ||
de2e453e | 2358 | /* Compute into RESULT. |
2359 | Set RESULT to wherever the result comes back. */ | |
6b43bae4 | 2360 | if (builtin_optab == sincos_optab) |
2361 | { | |
de2e453e | 2362 | int ok; |
7d3f6cc7 | 2363 | |
6b43bae4 | 2364 | switch (DECL_FUNCTION_CODE (fndecl)) |
2365 | { | |
4f35b1fc | 2366 | CASE_FLT_FN (BUILT_IN_SIN): |
de2e453e | 2367 | ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0); |
6b43bae4 | 2368 | break; |
4f35b1fc | 2369 | CASE_FLT_FN (BUILT_IN_COS): |
de2e453e | 2370 | ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0); |
6b43bae4 | 2371 | break; |
2372 | default: | |
64db345d | 2373 | gcc_unreachable (); |
6b43bae4 | 2374 | } |
de2e453e | 2375 | gcc_assert (ok); |
6b43bae4 | 2376 | } |
2377 | else | |
de2e453e | 2378 | result = expand_unop (mode, builtin_optab, op0, result, 0); |
6b43bae4 | 2379 | |
de2e453e | 2380 | if (result != 0) |
6b43bae4 | 2381 | { |
6b43bae4 | 2382 | /* Output the entire sequence. */ |
2383 | insns = get_insns (); | |
2384 | end_sequence (); | |
2385 | emit_insn (insns); | |
de2e453e | 2386 | return result; |
6b43bae4 | 2387 | } |
2388 | ||
2389 | /* If we were unable to expand via the builtin, stop the sequence | |
2390 | (without outputting the insns) and call to the library function | |
2391 | with the stabilized argument list. */ | |
2392 | end_sequence (); | |
2393 | } | |
2394 | ||
de2e453e | 2395 | return expand_call (exp, target, target == const0_rtx); |
6b43bae4 | 2396 | } |
2397 | ||
a65c4d64 | 2398 | /* Given an interclass math builtin decl FNDECL and it's argument ARG |
2399 | return an RTL instruction code that implements the functionality. | |
2400 | If that isn't possible or available return CODE_FOR_nothing. */ | |
a67a90e5 | 2401 | |
a65c4d64 | 2402 | static enum insn_code |
2403 | interclass_mathfn_icode (tree arg, tree fndecl) | |
a67a90e5 | 2404 | { |
a65c4d64 | 2405 | bool errno_set = false; |
6cdd383a | 2406 | optab builtin_optab = unknown_optab; |
3754d046 | 2407 | machine_mode mode; |
a67a90e5 | 2408 | |
2409 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2410 | { | |
2411 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
12f08300 | 2412 | errno_set = true; builtin_optab = ilogb_optab; break; |
2413 | CASE_FLT_FN (BUILT_IN_ISINF): | |
2414 | builtin_optab = isinf_optab; break; | |
2415 | case BUILT_IN_ISNORMAL: | |
2416 | case BUILT_IN_ISFINITE: | |
2417 | CASE_FLT_FN (BUILT_IN_FINITE): | |
2418 | case BUILT_IN_FINITED32: | |
2419 | case BUILT_IN_FINITED64: | |
2420 | case BUILT_IN_FINITED128: | |
2421 | case BUILT_IN_ISINFD32: | |
2422 | case BUILT_IN_ISINFD64: | |
2423 | case BUILT_IN_ISINFD128: | |
2424 | /* These builtins have no optabs (yet). */ | |
cde061c1 | 2425 | break; |
a67a90e5 | 2426 | default: |
2427 | gcc_unreachable (); | |
2428 | } | |
2429 | ||
2430 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2431 | if (flag_errno_math && errno_set) | |
a65c4d64 | 2432 | return CODE_FOR_nothing; |
a67a90e5 | 2433 | |
2434 | /* Optab mode depends on the mode of the input argument. */ | |
2435 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2436 | ||
cde061c1 | 2437 | if (builtin_optab) |
d6bf3b14 | 2438 | return optab_handler (builtin_optab, mode); |
a65c4d64 | 2439 | return CODE_FOR_nothing; |
2440 | } | |
2441 | ||
2442 | /* Expand a call to one of the builtin math functions that operate on | |
12f08300 | 2443 | floating point argument and output an integer result (ilogb, isinf, |
2444 | isnan, etc). | |
a65c4d64 | 2445 | Return 0 if a normal call should be emitted rather than expanding the |
2446 | function in-line. EXP is the expression that is a call to the builtin | |
f97eea22 | 2447 | function; if convenient, the result should be placed in TARGET. */ |
a65c4d64 | 2448 | |
2449 | static rtx | |
f97eea22 | 2450 | expand_builtin_interclass_mathfn (tree exp, rtx target) |
a65c4d64 | 2451 | { |
2452 | enum insn_code icode = CODE_FOR_nothing; | |
2453 | rtx op0; | |
2454 | tree fndecl = get_callee_fndecl (exp); | |
3754d046 | 2455 | machine_mode mode; |
a65c4d64 | 2456 | tree arg; |
2457 | ||
2458 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) | |
2459 | return NULL_RTX; | |
2460 | ||
2461 | arg = CALL_EXPR_ARG (exp, 0); | |
2462 | icode = interclass_mathfn_icode (arg, fndecl); | |
2463 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2464 | ||
a67a90e5 | 2465 | if (icode != CODE_FOR_nothing) |
2466 | { | |
8786db1e | 2467 | struct expand_operand ops[1]; |
1e0c0b35 | 2468 | rtx_insn *last = get_last_insn (); |
4e2a2fb4 | 2469 | tree orig_arg = arg; |
a67a90e5 | 2470 | |
2471 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2472 | need to expand the argument again. This way, we will not perform | |
2473 | side-effects more the once. */ | |
abfea505 | 2474 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
a67a90e5 | 2475 | |
f97eea22 | 2476 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
a67a90e5 | 2477 | |
2478 | if (mode != GET_MODE (op0)) | |
2479 | op0 = convert_to_mode (mode, op0, 0); | |
2480 | ||
8786db1e | 2481 | create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp))); |
2482 | if (maybe_legitimize_operands (icode, 0, 1, ops) | |
2483 | && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN)) | |
2484 | return ops[0].value; | |
2485 | ||
4e2a2fb4 | 2486 | delete_insns_since (last); |
2487 | CALL_EXPR_ARG (exp, 0) = orig_arg; | |
a67a90e5 | 2488 | } |
2489 | ||
a65c4d64 | 2490 | return NULL_RTX; |
a67a90e5 | 2491 | } |
2492 | ||
c3147c1a | 2493 | /* Expand a call to the builtin sincos math function. |
c2f47e15 | 2494 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
c3147c1a | 2495 | function in-line. EXP is the expression that is a call to the builtin |
2496 | function. */ | |
2497 | ||
2498 | static rtx | |
2499 | expand_builtin_sincos (tree exp) | |
2500 | { | |
2501 | rtx op0, op1, op2, target1, target2; | |
3754d046 | 2502 | machine_mode mode; |
c3147c1a | 2503 | tree arg, sinp, cosp; |
2504 | int result; | |
389dd41b | 2505 | location_t loc = EXPR_LOCATION (exp); |
be5575b2 | 2506 | tree alias_type, alias_off; |
c3147c1a | 2507 | |
c2f47e15 | 2508 | if (!validate_arglist (exp, REAL_TYPE, |
2509 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2510 | return NULL_RTX; | |
c3147c1a | 2511 | |
c2f47e15 | 2512 | arg = CALL_EXPR_ARG (exp, 0); |
2513 | sinp = CALL_EXPR_ARG (exp, 1); | |
2514 | cosp = CALL_EXPR_ARG (exp, 2); | |
c3147c1a | 2515 | |
2516 | /* Make a suitable register to place result in. */ | |
2517 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2518 | ||
2519 | /* Check if sincos insn is available, otherwise emit the call. */ | |
d6bf3b14 | 2520 | if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing) |
c3147c1a | 2521 | return NULL_RTX; |
2522 | ||
2523 | target1 = gen_reg_rtx (mode); | |
2524 | target2 = gen_reg_rtx (mode); | |
2525 | ||
8ec3c5c2 | 2526 | op0 = expand_normal (arg); |
be5575b2 | 2527 | alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true); |
2528 | alias_off = build_int_cst (alias_type, 0); | |
2529 | op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2530 | sinp, alias_off)); | |
2531 | op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2532 | cosp, alias_off)); | |
c3147c1a | 2533 | |
2534 | /* Compute into target1 and target2. | |
2535 | Set TARGET to wherever the result comes back. */ | |
2536 | result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0); | |
2537 | gcc_assert (result); | |
2538 | ||
2539 | /* Move target1 and target2 to the memory locations indicated | |
2540 | by op1 and op2. */ | |
2541 | emit_move_insn (op1, target1); | |
2542 | emit_move_insn (op2, target2); | |
2543 | ||
2544 | return const0_rtx; | |
2545 | } | |
2546 | ||
d735c391 | 2547 | /* Expand a call to the internal cexpi builtin to the sincos math function. |
2548 | EXP is the expression that is a call to the builtin function; if convenient, | |
f97eea22 | 2549 | the result should be placed in TARGET. */ |
d735c391 | 2550 | |
2551 | static rtx | |
f97eea22 | 2552 | expand_builtin_cexpi (tree exp, rtx target) |
d735c391 | 2553 | { |
2554 | tree fndecl = get_callee_fndecl (exp); | |
d735c391 | 2555 | tree arg, type; |
3754d046 | 2556 | machine_mode mode; |
d735c391 | 2557 | rtx op0, op1, op2; |
389dd41b | 2558 | location_t loc = EXPR_LOCATION (exp); |
d735c391 | 2559 | |
c2f47e15 | 2560 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2561 | return NULL_RTX; | |
d735c391 | 2562 | |
c2f47e15 | 2563 | arg = CALL_EXPR_ARG (exp, 0); |
d735c391 | 2564 | type = TREE_TYPE (arg); |
2565 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2566 | ||
2567 | /* Try expanding via a sincos optab, fall back to emitting a libcall | |
18b8d8ae | 2568 | to sincos or cexp. We are sure we have sincos or cexp because cexpi |
2569 | is only generated from sincos, cexp or if we have either of them. */ | |
d6bf3b14 | 2570 | if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing) |
d735c391 | 2571 | { |
2572 | op1 = gen_reg_rtx (mode); | |
2573 | op2 = gen_reg_rtx (mode); | |
2574 | ||
f97eea22 | 2575 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2576 | |
2577 | /* Compute into op1 and op2. */ | |
2578 | expand_twoval_unop (sincos_optab, op0, op2, op1, 0); | |
2579 | } | |
30f690e0 | 2580 | else if (targetm.libc_has_function (function_sincos)) |
d735c391 | 2581 | { |
c2f47e15 | 2582 | tree call, fn = NULL_TREE; |
d735c391 | 2583 | tree top1, top2; |
2584 | rtx op1a, op2a; | |
2585 | ||
2586 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
b9a16870 | 2587 | fn = builtin_decl_explicit (BUILT_IN_SINCOSF); |
d735c391 | 2588 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
b9a16870 | 2589 | fn = builtin_decl_explicit (BUILT_IN_SINCOS); |
d735c391 | 2590 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
b9a16870 | 2591 | fn = builtin_decl_explicit (BUILT_IN_SINCOSL); |
c2f47e15 | 2592 | else |
2593 | gcc_unreachable (); | |
48e1416a | 2594 | |
0ab48139 | 2595 | op1 = assign_temp (TREE_TYPE (arg), 1, 1); |
2596 | op2 = assign_temp (TREE_TYPE (arg), 1, 1); | |
99182918 | 2597 | op1a = copy_addr_to_reg (XEXP (op1, 0)); |
2598 | op2a = copy_addr_to_reg (XEXP (op2, 0)); | |
d735c391 | 2599 | top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a); |
2600 | top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a); | |
2601 | ||
d735c391 | 2602 | /* Make sure not to fold the sincos call again. */ |
2603 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
c2f47e15 | 2604 | expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)), |
2605 | call, 3, arg, top1, top2)); | |
d735c391 | 2606 | } |
18b8d8ae | 2607 | else |
2608 | { | |
0ecbc158 | 2609 | tree call, fn = NULL_TREE, narg; |
18b8d8ae | 2610 | tree ctype = build_complex_type (type); |
2611 | ||
0ecbc158 | 2612 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) |
b9a16870 | 2613 | fn = builtin_decl_explicit (BUILT_IN_CEXPF); |
0ecbc158 | 2614 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
b9a16870 | 2615 | fn = builtin_decl_explicit (BUILT_IN_CEXP); |
0ecbc158 | 2616 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
b9a16870 | 2617 | fn = builtin_decl_explicit (BUILT_IN_CEXPL); |
c2f47e15 | 2618 | else |
2619 | gcc_unreachable (); | |
fc0dfa6e | 2620 | |
2621 | /* If we don't have a decl for cexp create one. This is the | |
2622 | friendliest fallback if the user calls __builtin_cexpi | |
2623 | without full target C99 function support. */ | |
2624 | if (fn == NULL_TREE) | |
2625 | { | |
2626 | tree fntype; | |
2627 | const char *name = NULL; | |
2628 | ||
2629 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
2630 | name = "cexpf"; | |
2631 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2632 | name = "cexp"; | |
2633 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2634 | name = "cexpl"; | |
2635 | ||
2636 | fntype = build_function_type_list (ctype, ctype, NULL_TREE); | |
2637 | fn = build_fn_decl (name, fntype); | |
2638 | } | |
2639 | ||
389dd41b | 2640 | narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype, |
18b8d8ae | 2641 | build_real (type, dconst0), arg); |
2642 | ||
2643 | /* Make sure not to fold the cexp call again. */ | |
2644 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
48e1416a | 2645 | return expand_expr (build_call_nary (ctype, call, 1, narg), |
1db6d067 | 2646 | target, VOIDmode, EXPAND_NORMAL); |
18b8d8ae | 2647 | } |
d735c391 | 2648 | |
2649 | /* Now build the proper return type. */ | |
2650 | return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type), | |
2651 | make_tree (TREE_TYPE (arg), op2), | |
2652 | make_tree (TREE_TYPE (arg), op1)), | |
1db6d067 | 2653 | target, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2654 | } |
2655 | ||
a65c4d64 | 2656 | /* Conveniently construct a function call expression. FNDECL names the |
2657 | function to be called, N is the number of arguments, and the "..." | |
2658 | parameters are the argument expressions. Unlike build_call_exr | |
2659 | this doesn't fold the call, hence it will always return a CALL_EXPR. */ | |
2660 | ||
2661 | static tree | |
2662 | build_call_nofold_loc (location_t loc, tree fndecl, int n, ...) | |
2663 | { | |
2664 | va_list ap; | |
2665 | tree fntype = TREE_TYPE (fndecl); | |
2666 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
2667 | ||
2668 | va_start (ap, n); | |
2669 | fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap); | |
2670 | va_end (ap); | |
2671 | SET_EXPR_LOCATION (fn, loc); | |
2672 | return fn; | |
2673 | } | |
a65c4d64 | 2674 | |
7d3afc77 | 2675 | /* Expand a call to one of the builtin rounding functions gcc defines |
2676 | as an extension (lfloor and lceil). As these are gcc extensions we | |
2677 | do not need to worry about setting errno to EDOM. | |
ad52b9b7 | 2678 | If expanding via optab fails, lower expression to (int)(floor(x)). |
2679 | EXP is the expression that is a call to the builtin function; | |
ff1b14e4 | 2680 | if convenient, the result should be placed in TARGET. */ |
ad52b9b7 | 2681 | |
2682 | static rtx | |
ff1b14e4 | 2683 | expand_builtin_int_roundingfn (tree exp, rtx target) |
ad52b9b7 | 2684 | { |
9c42dd28 | 2685 | convert_optab builtin_optab; |
1e0c0b35 | 2686 | rtx op0, tmp; |
2687 | rtx_insn *insns; | |
ad52b9b7 | 2688 | tree fndecl = get_callee_fndecl (exp); |
ad52b9b7 | 2689 | enum built_in_function fallback_fn; |
2690 | tree fallback_fndecl; | |
3754d046 | 2691 | machine_mode mode; |
4de0924f | 2692 | tree arg; |
ad52b9b7 | 2693 | |
c2f47e15 | 2694 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
ad52b9b7 | 2695 | gcc_unreachable (); |
2696 | ||
c2f47e15 | 2697 | arg = CALL_EXPR_ARG (exp, 0); |
ad52b9b7 | 2698 | |
2699 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2700 | { | |
80ff6494 | 2701 | CASE_FLT_FN (BUILT_IN_ICEIL): |
4f35b1fc | 2702 | CASE_FLT_FN (BUILT_IN_LCEIL): |
2703 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 2704 | builtin_optab = lceil_optab; |
2705 | fallback_fn = BUILT_IN_CEIL; | |
2706 | break; | |
2707 | ||
80ff6494 | 2708 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
4f35b1fc | 2709 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
2710 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 2711 | builtin_optab = lfloor_optab; |
2712 | fallback_fn = BUILT_IN_FLOOR; | |
2713 | break; | |
2714 | ||
2715 | default: | |
2716 | gcc_unreachable (); | |
2717 | } | |
2718 | ||
2719 | /* Make a suitable register to place result in. */ | |
2720 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2721 | ||
9c42dd28 | 2722 | target = gen_reg_rtx (mode); |
ad52b9b7 | 2723 | |
9c42dd28 | 2724 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2725 | need to expand the argument again. This way, we will not perform | |
2726 | side-effects more the once. */ | |
abfea505 | 2727 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
ad52b9b7 | 2728 | |
ff1b14e4 | 2729 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
ad52b9b7 | 2730 | |
9c42dd28 | 2731 | start_sequence (); |
ad52b9b7 | 2732 | |
9c42dd28 | 2733 | /* Compute into TARGET. */ |
2734 | if (expand_sfix_optab (target, op0, builtin_optab)) | |
2735 | { | |
2736 | /* Output the entire sequence. */ | |
2737 | insns = get_insns (); | |
ad52b9b7 | 2738 | end_sequence (); |
9c42dd28 | 2739 | emit_insn (insns); |
2740 | return target; | |
ad52b9b7 | 2741 | } |
2742 | ||
9c42dd28 | 2743 | /* If we were unable to expand via the builtin, stop the sequence |
2744 | (without outputting the insns). */ | |
2745 | end_sequence (); | |
2746 | ||
ad52b9b7 | 2747 | /* Fall back to floating point rounding optab. */ |
2748 | fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn); | |
fc0dfa6e | 2749 | |
2750 | /* For non-C99 targets we may end up without a fallback fndecl here | |
2751 | if the user called __builtin_lfloor directly. In this case emit | |
2752 | a call to the floor/ceil variants nevertheless. This should result | |
2753 | in the best user experience for not full C99 targets. */ | |
2754 | if (fallback_fndecl == NULL_TREE) | |
2755 | { | |
2756 | tree fntype; | |
2757 | const char *name = NULL; | |
2758 | ||
2759 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2760 | { | |
80ff6494 | 2761 | case BUILT_IN_ICEIL: |
fc0dfa6e | 2762 | case BUILT_IN_LCEIL: |
2763 | case BUILT_IN_LLCEIL: | |
2764 | name = "ceil"; | |
2765 | break; | |
80ff6494 | 2766 | case BUILT_IN_ICEILF: |
fc0dfa6e | 2767 | case BUILT_IN_LCEILF: |
2768 | case BUILT_IN_LLCEILF: | |
2769 | name = "ceilf"; | |
2770 | break; | |
80ff6494 | 2771 | case BUILT_IN_ICEILL: |
fc0dfa6e | 2772 | case BUILT_IN_LCEILL: |
2773 | case BUILT_IN_LLCEILL: | |
2774 | name = "ceill"; | |
2775 | break; | |
80ff6494 | 2776 | case BUILT_IN_IFLOOR: |
fc0dfa6e | 2777 | case BUILT_IN_LFLOOR: |
2778 | case BUILT_IN_LLFLOOR: | |
2779 | name = "floor"; | |
2780 | break; | |
80ff6494 | 2781 | case BUILT_IN_IFLOORF: |
fc0dfa6e | 2782 | case BUILT_IN_LFLOORF: |
2783 | case BUILT_IN_LLFLOORF: | |
2784 | name = "floorf"; | |
2785 | break; | |
80ff6494 | 2786 | case BUILT_IN_IFLOORL: |
fc0dfa6e | 2787 | case BUILT_IN_LFLOORL: |
2788 | case BUILT_IN_LLFLOORL: | |
2789 | name = "floorl"; | |
2790 | break; | |
2791 | default: | |
2792 | gcc_unreachable (); | |
2793 | } | |
2794 | ||
2795 | fntype = build_function_type_list (TREE_TYPE (arg), | |
2796 | TREE_TYPE (arg), NULL_TREE); | |
2797 | fallback_fndecl = build_fn_decl (name, fntype); | |
2798 | } | |
2799 | ||
0568e9c1 | 2800 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg); |
ad52b9b7 | 2801 | |
d4c690af | 2802 | tmp = expand_normal (exp); |
933eb13a | 2803 | tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp)); |
ad52b9b7 | 2804 | |
2805 | /* Truncate the result of floating point optab to integer | |
2806 | via expand_fix (). */ | |
2807 | target = gen_reg_rtx (mode); | |
2808 | expand_fix (target, tmp, 0); | |
2809 | ||
2810 | return target; | |
2811 | } | |
2812 | ||
7d3afc77 | 2813 | /* Expand a call to one of the builtin math functions doing integer |
2814 | conversion (lrint). | |
2815 | Return 0 if a normal call should be emitted rather than expanding the | |
2816 | function in-line. EXP is the expression that is a call to the builtin | |
ff1b14e4 | 2817 | function; if convenient, the result should be placed in TARGET. */ |
7d3afc77 | 2818 | |
2819 | static rtx | |
ff1b14e4 | 2820 | expand_builtin_int_roundingfn_2 (tree exp, rtx target) |
7d3afc77 | 2821 | { |
5f51ee59 | 2822 | convert_optab builtin_optab; |
1e0c0b35 | 2823 | rtx op0; |
2824 | rtx_insn *insns; | |
7d3afc77 | 2825 | tree fndecl = get_callee_fndecl (exp); |
4de0924f | 2826 | tree arg; |
3754d046 | 2827 | machine_mode mode; |
e951f9a4 | 2828 | enum built_in_function fallback_fn = BUILT_IN_NONE; |
7d3afc77 | 2829 | |
c2f47e15 | 2830 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2831 | gcc_unreachable (); | |
48e1416a | 2832 | |
c2f47e15 | 2833 | arg = CALL_EXPR_ARG (exp, 0); |
7d3afc77 | 2834 | |
2835 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2836 | { | |
80ff6494 | 2837 | CASE_FLT_FN (BUILT_IN_IRINT): |
e951f9a4 | 2838 | fallback_fn = BUILT_IN_LRINT; |
3c77f69c | 2839 | gcc_fallthrough (); |
7d3afc77 | 2840 | CASE_FLT_FN (BUILT_IN_LRINT): |
2841 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
e951f9a4 | 2842 | builtin_optab = lrint_optab; |
2843 | break; | |
80ff6494 | 2844 | |
2845 | CASE_FLT_FN (BUILT_IN_IROUND): | |
e951f9a4 | 2846 | fallback_fn = BUILT_IN_LROUND; |
3c77f69c | 2847 | gcc_fallthrough (); |
ef2f1a10 | 2848 | CASE_FLT_FN (BUILT_IN_LROUND): |
2849 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
e951f9a4 | 2850 | builtin_optab = lround_optab; |
2851 | break; | |
80ff6494 | 2852 | |
7d3afc77 | 2853 | default: |
2854 | gcc_unreachable (); | |
2855 | } | |
2856 | ||
e951f9a4 | 2857 | /* There's no easy way to detect the case we need to set EDOM. */ |
2858 | if (flag_errno_math && fallback_fn == BUILT_IN_NONE) | |
2859 | return NULL_RTX; | |
2860 | ||
7d3afc77 | 2861 | /* Make a suitable register to place result in. */ |
2862 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2863 | ||
e951f9a4 | 2864 | /* There's no easy way to detect the case we need to set EDOM. */ |
2865 | if (!flag_errno_math) | |
2866 | { | |
de2e453e | 2867 | rtx result = gen_reg_rtx (mode); |
7d3afc77 | 2868 | |
e951f9a4 | 2869 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2870 | need to expand the argument again. This way, we will not perform | |
2871 | side-effects more the once. */ | |
2872 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); | |
7d3afc77 | 2873 | |
e951f9a4 | 2874 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
7d3afc77 | 2875 | |
e951f9a4 | 2876 | start_sequence (); |
7d3afc77 | 2877 | |
de2e453e | 2878 | if (expand_sfix_optab (result, op0, builtin_optab)) |
e951f9a4 | 2879 | { |
2880 | /* Output the entire sequence. */ | |
2881 | insns = get_insns (); | |
2882 | end_sequence (); | |
2883 | emit_insn (insns); | |
de2e453e | 2884 | return result; |
e951f9a4 | 2885 | } |
2886 | ||
2887 | /* If we were unable to expand via the builtin, stop the sequence | |
2888 | (without outputting the insns) and call to the library function | |
2889 | with the stabilized argument list. */ | |
7d3afc77 | 2890 | end_sequence (); |
2891 | } | |
2892 | ||
e951f9a4 | 2893 | if (fallback_fn != BUILT_IN_NONE) |
2894 | { | |
2895 | /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99 | |
2896 | targets, (int) round (x) should never be transformed into | |
2897 | BUILT_IN_IROUND and if __builtin_iround is called directly, emit | |
2898 | a call to lround in the hope that the target provides at least some | |
2899 | C99 functions. This should result in the best user experience for | |
2900 | not full C99 targets. */ | |
e3240774 | 2901 | tree fallback_fndecl = mathfn_built_in_1 |
2902 | (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); | |
e951f9a4 | 2903 | |
2904 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), | |
2905 | fallback_fndecl, 1, arg); | |
2906 | ||
2907 | target = expand_call (exp, NULL_RTX, target == const0_rtx); | |
933eb13a | 2908 | target = maybe_emit_group_store (target, TREE_TYPE (exp)); |
e951f9a4 | 2909 | return convert_to_mode (mode, target, 0); |
2910 | } | |
5f51ee59 | 2911 | |
de2e453e | 2912 | return expand_call (exp, target, target == const0_rtx); |
7d3afc77 | 2913 | } |
2914 | ||
c2f47e15 | 2915 | /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if |
757c219d | 2916 | a normal call should be emitted rather than expanding the function |
2917 | in-line. EXP is the expression that is a call to the builtin | |
2918 | function; if convenient, the result should be placed in TARGET. */ | |
2919 | ||
2920 | static rtx | |
f97eea22 | 2921 | expand_builtin_powi (tree exp, rtx target) |
757c219d | 2922 | { |
757c219d | 2923 | tree arg0, arg1; |
2924 | rtx op0, op1; | |
3754d046 | 2925 | machine_mode mode; |
2926 | machine_mode mode2; | |
757c219d | 2927 | |
c2f47e15 | 2928 | if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) |
2929 | return NULL_RTX; | |
757c219d | 2930 | |
c2f47e15 | 2931 | arg0 = CALL_EXPR_ARG (exp, 0); |
2932 | arg1 = CALL_EXPR_ARG (exp, 1); | |
757c219d | 2933 | mode = TYPE_MODE (TREE_TYPE (exp)); |
2934 | ||
757c219d | 2935 | /* Emit a libcall to libgcc. */ |
2936 | ||
c2f47e15 | 2937 | /* Mode of the 2nd argument must match that of an int. */ |
517be012 | 2938 | mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require (); |
d0405f40 | 2939 | |
757c219d | 2940 | if (target == NULL_RTX) |
2941 | target = gen_reg_rtx (mode); | |
2942 | ||
f97eea22 | 2943 | op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL); |
757c219d | 2944 | if (GET_MODE (op0) != mode) |
2945 | op0 = convert_to_mode (mode, op0, 0); | |
1db6d067 | 2946 | op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL); |
d0405f40 | 2947 | if (GET_MODE (op1) != mode2) |
2948 | op1 = convert_to_mode (mode2, op1, 0); | |
757c219d | 2949 | |
f36b9f69 | 2950 | target = emit_library_call_value (optab_libfunc (powi_optab, mode), |
9e9e5c15 | 2951 | target, LCT_CONST, mode, |
d0405f40 | 2952 | op0, mode, op1, mode2); |
757c219d | 2953 | |
2954 | return target; | |
2955 | } | |
2956 | ||
48e1416a | 2957 | /* Expand expression EXP which is a call to the strlen builtin. Return |
864bd5de | 2958 | NULL_RTX if we failed and the caller should emit a normal call, otherwise |
aed0bd19 | 2959 | try to get the result in TARGET, if convenient. */ |
f7c44134 | 2960 | |
53800dbe | 2961 | static rtx |
c2f47e15 | 2962 | expand_builtin_strlen (tree exp, rtx target, |
3754d046 | 2963 | machine_mode target_mode) |
53800dbe | 2964 | { |
c2f47e15 | 2965 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2966 | return NULL_RTX; | |
6248e345 | 2967 | |
5c5d012b | 2968 | struct expand_operand ops[4]; |
2969 | rtx pat; | |
2970 | tree len; | |
2971 | tree src = CALL_EXPR_ARG (exp, 0); | |
2972 | rtx src_reg; | |
2973 | rtx_insn *before_strlen; | |
2974 | machine_mode insn_mode; | |
2975 | enum insn_code icode = CODE_FOR_nothing; | |
2976 | unsigned int align; | |
681fab1e | 2977 | |
5c5d012b | 2978 | /* If the length can be computed at compile-time, return it. */ |
2979 | len = c_strlen (src, 0); | |
2980 | if (len) | |
2981 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2982 | ||
2983 | /* If the length can be computed at compile-time and is constant | |
2984 | integer, but there are side-effects in src, evaluate | |
2985 | src for side-effects, then return len. | |
2986 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
2987 | can be optimized into: i++; x = 3; */ | |
2988 | len = c_strlen (src, 1); | |
2989 | if (len && TREE_CODE (len) == INTEGER_CST) | |
2990 | { | |
2991 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2992 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2993 | } | |
53800dbe | 2994 | |
5c5d012b | 2995 | align = get_pointer_alignment (src) / BITS_PER_UNIT; |
53800dbe | 2996 | |
5c5d012b | 2997 | /* If SRC is not a pointer type, don't do this operation inline. */ |
2998 | if (align == 0) | |
2999 | return NULL_RTX; | |
3000 | ||
3001 | /* Bail out if we can't compute strlen in the right mode. */ | |
3002 | FOR_EACH_MODE_FROM (insn_mode, target_mode) | |
3003 | { | |
3004 | icode = optab_handler (strlen_optab, insn_mode); | |
3005 | if (icode != CODE_FOR_nothing) | |
3006 | break; | |
3007 | } | |
3008 | if (insn_mode == VOIDmode) | |
3009 | return NULL_RTX; | |
53800dbe | 3010 | |
5c5d012b | 3011 | /* Make a place to hold the source address. We will not expand |
3012 | the actual source until we are sure that the expansion will | |
3013 | not fail -- there are trees that cannot be expanded twice. */ | |
3014 | src_reg = gen_reg_rtx (Pmode); | |
53800dbe | 3015 | |
5c5d012b | 3016 | /* Mark the beginning of the strlen sequence so we can emit the |
3017 | source operand later. */ | |
3018 | before_strlen = get_last_insn (); | |
53800dbe | 3019 | |
5c5d012b | 3020 | create_output_operand (&ops[0], target, insn_mode); |
3021 | create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg)); | |
3022 | create_integer_operand (&ops[2], 0); | |
3023 | create_integer_operand (&ops[3], align); | |
3024 | if (!maybe_expand_insn (icode, 4, ops)) | |
3025 | return NULL_RTX; | |
911c0150 | 3026 | |
5c5d012b | 3027 | /* Check to see if the argument was declared attribute nonstring |
3028 | and if so, issue a warning since at this point it's not known | |
3029 | to be nul-terminated. */ | |
3030 | maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp); | |
0c45740b | 3031 | |
5c5d012b | 3032 | /* Now that we are assured of success, expand the source. */ |
3033 | start_sequence (); | |
3034 | pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL); | |
3035 | if (pat != src_reg) | |
3036 | { | |
499eee58 | 3037 | #ifdef POINTERS_EXTEND_UNSIGNED |
5c5d012b | 3038 | if (GET_MODE (pat) != Pmode) |
3039 | pat = convert_to_mode (Pmode, pat, | |
3040 | POINTERS_EXTEND_UNSIGNED); | |
499eee58 | 3041 | #endif |
5c5d012b | 3042 | emit_move_insn (src_reg, pat); |
3043 | } | |
3044 | pat = get_insns (); | |
3045 | end_sequence (); | |
bceb0d1f | 3046 | |
5c5d012b | 3047 | if (before_strlen) |
3048 | emit_insn_after (pat, before_strlen); | |
3049 | else | |
3050 | emit_insn_before (pat, get_insns ()); | |
53800dbe | 3051 | |
5c5d012b | 3052 | /* Return the value in the proper mode for this function. */ |
3053 | if (GET_MODE (ops[0].value) == target_mode) | |
3054 | target = ops[0].value; | |
3055 | else if (target != 0) | |
3056 | convert_move (target, ops[0].value, 0); | |
3057 | else | |
3058 | target = convert_to_mode (target_mode, ops[0].value, 0); | |
911c0150 | 3059 | |
5c5d012b | 3060 | return target; |
53800dbe | 3061 | } |
3062 | ||
864bd5de | 3063 | /* Expand call EXP to the strnlen built-in, returning the result |
3064 | and setting it in TARGET. Otherwise return NULL_RTX on failure. */ | |
3065 | ||
3066 | static rtx | |
3067 | expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode) | |
3068 | { | |
3069 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3070 | return NULL_RTX; | |
3071 | ||
3072 | tree src = CALL_EXPR_ARG (exp, 0); | |
3073 | tree bound = CALL_EXPR_ARG (exp, 1); | |
3074 | ||
3075 | if (!bound) | |
3076 | return NULL_RTX; | |
3077 | ||
3078 | location_t loc = UNKNOWN_LOCATION; | |
3079 | if (EXPR_HAS_LOCATION (exp)) | |
3080 | loc = EXPR_LOCATION (exp); | |
3081 | ||
3082 | tree maxobjsize = max_object_size (); | |
3083 | tree func = get_callee_fndecl (exp); | |
3084 | ||
55769ed6 | 3085 | /* FIXME: Change c_strlen() to return sizetype instead of ssizetype |
3086 | so these conversions aren't necessary. */ | |
98d5ba5d | 3087 | c_strlen_data lendata = { }; |
3088 | tree len = c_strlen (src, 0, &lendata, 1); | |
55769ed6 | 3089 | if (len) |
3090 | len = fold_convert_loc (loc, TREE_TYPE (bound), len); | |
864bd5de | 3091 | |
3092 | if (TREE_CODE (bound) == INTEGER_CST) | |
3093 | { | |
3094 | if (!TREE_NO_WARNING (exp) | |
3095 | && tree_int_cst_lt (maxobjsize, bound) | |
3096 | && warning_at (loc, OPT_Wstringop_overflow_, | |
3097 | "%K%qD specified bound %E " | |
3098 | "exceeds maximum object size %E", | |
3099 | exp, func, bound, maxobjsize)) | |
94a62c5a | 3100 | TREE_NO_WARNING (exp) = true; |
864bd5de | 3101 | |
fec27bf2 | 3102 | bool exact = true; |
864bd5de | 3103 | if (!len || TREE_CODE (len) != INTEGER_CST) |
fec27bf2 | 3104 | { |
3105 | /* Clear EXACT if LEN may be less than SRC suggests, | |
3106 | such as in | |
3107 | strnlen (&a[i], sizeof a) | |
3108 | where the value of i is unknown. Unless i's value is | |
3109 | zero, the call is unsafe because the bound is greater. */ | |
98d5ba5d | 3110 | lendata.decl = unterminated_array (src, &len, &exact); |
3111 | if (!lendata.decl) | |
fec27bf2 | 3112 | return NULL_RTX; |
3113 | } | |
3114 | ||
98d5ba5d | 3115 | if (lendata.decl |
fec27bf2 | 3116 | && !TREE_NO_WARNING (exp) |
3117 | && ((tree_int_cst_lt (len, bound)) | |
3118 | || !exact)) | |
3119 | { | |
3120 | location_t warnloc | |
3121 | = expansion_point_location_if_in_system_header (loc); | |
3122 | ||
3123 | if (warning_at (warnloc, OPT_Wstringop_overflow_, | |
3124 | exact | |
3125 | ? G_("%K%qD specified bound %E exceeds the size %E " | |
3126 | "of unterminated array") | |
3127 | : G_("%K%qD specified bound %E may exceed the size " | |
3128 | "of at most %E of unterminated array"), | |
3129 | exp, func, bound, len)) | |
3130 | { | |
98d5ba5d | 3131 | inform (DECL_SOURCE_LOCATION (lendata.decl), |
fec27bf2 | 3132 | "referenced argument declared here"); |
3133 | TREE_NO_WARNING (exp) = true; | |
3134 | return NULL_RTX; | |
3135 | } | |
3136 | } | |
3137 | ||
3138 | if (!len) | |
864bd5de | 3139 | return NULL_RTX; |
3140 | ||
864bd5de | 3141 | len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound); |
3142 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
3143 | } | |
3144 | ||
3145 | if (TREE_CODE (bound) != SSA_NAME) | |
3146 | return NULL_RTX; | |
3147 | ||
3148 | wide_int min, max; | |
be44111e | 3149 | enum value_range_kind rng = get_range_info (bound, &min, &max); |
864bd5de | 3150 | if (rng != VR_RANGE) |
3151 | return NULL_RTX; | |
3152 | ||
3153 | if (!TREE_NO_WARNING (exp) | |
3154 | && wi::ltu_p (wi::to_wide (maxobjsize), min) | |
3155 | && warning_at (loc, OPT_Wstringop_overflow_, | |
3156 | "%K%qD specified bound [%wu, %wu] " | |
3157 | "exceeds maximum object size %E", | |
3158 | exp, func, min.to_uhwi (), max.to_uhwi (), maxobjsize)) | |
94a62c5a | 3159 | TREE_NO_WARNING (exp) = true; |
864bd5de | 3160 | |
179f1960 | 3161 | bool exact = true; |
864bd5de | 3162 | if (!len || TREE_CODE (len) != INTEGER_CST) |
179f1960 | 3163 | { |
98d5ba5d | 3164 | lendata.decl = unterminated_array (src, &len, &exact); |
3165 | if (!lendata.decl) | |
179f1960 | 3166 | return NULL_RTX; |
3167 | } | |
864bd5de | 3168 | |
98d5ba5d | 3169 | if (lendata.decl |
179f1960 | 3170 | && !TREE_NO_WARNING (exp) |
3171 | && (wi::ltu_p (wi::to_wide (len), min) | |
3172 | || !exact)) | |
fec27bf2 | 3173 | { |
179f1960 | 3174 | location_t warnloc |
3175 | = expansion_point_location_if_in_system_header (loc); | |
3176 | ||
3177 | if (warning_at (warnloc, OPT_Wstringop_overflow_, | |
3178 | exact | |
3179 | ? G_("%K%qD specified bound [%wu, %wu] exceeds " | |
3180 | "the size %E of unterminated array") | |
3181 | : G_("%K%qD specified bound [%wu, %wu] may exceed " | |
3182 | "the size of at most %E of unterminated array"), | |
3183 | exp, func, min.to_uhwi (), max.to_uhwi (), len)) | |
3184 | { | |
98d5ba5d | 3185 | inform (DECL_SOURCE_LOCATION (lendata.decl), |
179f1960 | 3186 | "referenced argument declared here"); |
3187 | TREE_NO_WARNING (exp) = true; | |
3188 | } | |
fec27bf2 | 3189 | } |
3190 | ||
98d5ba5d | 3191 | if (lendata.decl) |
179f1960 | 3192 | return NULL_RTX; |
3193 | ||
864bd5de | 3194 | if (wi::gtu_p (min, wi::to_wide (len))) |
3195 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
3196 | ||
3197 | len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound); | |
3198 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
3199 | } | |
3200 | ||
6840589f | 3201 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3202 | bytes from constant string DATA + OFFSET and return it as target | |
3203 | constant. */ | |
3204 | ||
3205 | static rtx | |
aecda0d6 | 3206 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
f77c4496 | 3207 | scalar_int_mode mode) |
6840589f | 3208 | { |
3209 | const char *str = (const char *) data; | |
3210 | ||
64db345d | 3211 | gcc_assert (offset >= 0 |
3212 | && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
3213 | <= strlen (str) + 1)); | |
6840589f | 3214 | |
3215 | return c_readstr (str + offset, mode); | |
3216 | } | |
3217 | ||
36d63243 | 3218 | /* LEN specify length of the block of memcpy/memset operation. |
9db0f34d | 3219 | Figure out its range and put it into MIN_SIZE/MAX_SIZE. |
3220 | In some cases we can make very likely guess on max size, then we | |
3221 | set it into PROBABLE_MAX_SIZE. */ | |
36d63243 | 3222 | |
3223 | static void | |
3224 | determine_block_size (tree len, rtx len_rtx, | |
3225 | unsigned HOST_WIDE_INT *min_size, | |
9db0f34d | 3226 | unsigned HOST_WIDE_INT *max_size, |
3227 | unsigned HOST_WIDE_INT *probable_max_size) | |
36d63243 | 3228 | { |
3229 | if (CONST_INT_P (len_rtx)) | |
3230 | { | |
4e140a5c | 3231 | *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx); |
36d63243 | 3232 | return; |
3233 | } | |
3234 | else | |
3235 | { | |
9c1be15e | 3236 | wide_int min, max; |
be44111e | 3237 | enum value_range_kind range_type = VR_UNDEFINED; |
9db0f34d | 3238 | |
3239 | /* Determine bounds from the type. */ | |
3240 | if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len)))) | |
3241 | *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len))); | |
3242 | else | |
3243 | *min_size = 0; | |
3244 | if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len)))) | |
4e140a5c | 3245 | *probable_max_size = *max_size |
3246 | = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len))); | |
9db0f34d | 3247 | else |
3248 | *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx)); | |
3249 | ||
3250 | if (TREE_CODE (len) == SSA_NAME) | |
3251 | range_type = get_range_info (len, &min, &max); | |
3252 | if (range_type == VR_RANGE) | |
36d63243 | 3253 | { |
fe5ad926 | 3254 | if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ()) |
36d63243 | 3255 | *min_size = min.to_uhwi (); |
fe5ad926 | 3256 | if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ()) |
9db0f34d | 3257 | *probable_max_size = *max_size = max.to_uhwi (); |
36d63243 | 3258 | } |
9db0f34d | 3259 | else if (range_type == VR_ANTI_RANGE) |
36d63243 | 3260 | { |
4a474a5a | 3261 | /* Anti range 0...N lets us to determine minimal size to N+1. */ |
fe5ad926 | 3262 | if (min == 0) |
9db0f34d | 3263 | { |
9c1be15e | 3264 | if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0) |
3265 | *min_size = max.to_uhwi () + 1; | |
9db0f34d | 3266 | } |
3267 | /* Code like | |
3268 | ||
3269 | int n; | |
3270 | if (n < 100) | |
4a474a5a | 3271 | memcpy (a, b, n) |
9db0f34d | 3272 | |
3273 | Produce anti range allowing negative values of N. We still | |
3274 | can use the information and make a guess that N is not negative. | |
3275 | */ | |
fe5ad926 | 3276 | else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min)) |
3277 | *probable_max_size = min.to_uhwi () - 1; | |
36d63243 | 3278 | } |
3279 | } | |
3280 | gcc_checking_assert (*max_size <= | |
3281 | (unsigned HOST_WIDE_INT) | |
3282 | GET_MODE_MASK (GET_MODE (len_rtx))); | |
3283 | } | |
3284 | ||
5aef8938 | 3285 | /* Try to verify that the sizes and lengths of the arguments to a string |
3286 | manipulation function given by EXP are within valid bounds and that | |
e6a18b5a | 3287 | the operation does not lead to buffer overflow or read past the end. |
3288 | Arguments other than EXP may be null. When non-null, the arguments | |
3289 | have the following meaning: | |
3290 | DST is the destination of a copy call or NULL otherwise. | |
3291 | SRC is the source of a copy call or NULL otherwise. | |
3292 | DSTWRITE is the number of bytes written into the destination obtained | |
3293 | from the user-supplied size argument to the function (such as in | |
3294 | memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE). | |
3295 | MAXREAD is the user-supplied bound on the length of the source sequence | |
5aef8938 | 3296 | (such as in strncat(d, s, N). It specifies the upper limit on the number |
e6a18b5a | 3297 | of bytes to write. If NULL, it's taken to be the same as DSTWRITE. |
3298 | SRCSTR is the source string (such as in strcpy(DST, SRC)) when the | |
3299 | expression EXP is a string function call (as opposed to a memory call | |
3300 | like memcpy). As an exception, SRCSTR can also be an integer denoting | |
3301 | the precomputed size of the source string or object (for functions like | |
3302 | memcpy). | |
3303 | DSTSIZE is the size of the destination object specified by the last | |
5aef8938 | 3304 | argument to the _chk builtins, typically resulting from the expansion |
e6a18b5a | 3305 | of __builtin_object_size (such as in __builtin___strcpy_chk(DST, SRC, |
3306 | DSTSIZE). | |
5aef8938 | 3307 | |
e6a18b5a | 3308 | When DSTWRITE is null LEN is checked to verify that it doesn't exceed |
5aef8938 | 3309 | SIZE_MAX. |
3310 | ||
e6a18b5a | 3311 | If the call is successfully verified as safe return true, otherwise |
3312 | return false. */ | |
5aef8938 | 3313 | |
3314 | static bool | |
e6a18b5a | 3315 | check_access (tree exp, tree, tree, tree dstwrite, |
3316 | tree maxread, tree srcstr, tree dstsize) | |
5aef8938 | 3317 | { |
e6a18b5a | 3318 | int opt = OPT_Wstringop_overflow_; |
3319 | ||
5aef8938 | 3320 | /* The size of the largest object is half the address space, or |
e6a18b5a | 3321 | PTRDIFF_MAX. (This is way too permissive.) */ |
3322 | tree maxobjsize = max_object_size (); | |
5aef8938 | 3323 | |
e6a18b5a | 3324 | /* Either the length of the source string for string functions or |
3325 | the size of the source object for raw memory functions. */ | |
5aef8938 | 3326 | tree slen = NULL_TREE; |
3327 | ||
8d6c6ef5 | 3328 | tree range[2] = { NULL_TREE, NULL_TREE }; |
3329 | ||
5aef8938 | 3330 | /* Set to true when the exact number of bytes written by a string |
3331 | function like strcpy is not known and the only thing that is | |
3332 | known is that it must be at least one (for the terminating nul). */ | |
3333 | bool at_least_one = false; | |
e6a18b5a | 3334 | if (srcstr) |
5aef8938 | 3335 | { |
e6a18b5a | 3336 | /* SRCSTR is normally a pointer to string but as a special case |
5aef8938 | 3337 | it can be an integer denoting the length of a string. */ |
e6a18b5a | 3338 | if (POINTER_TYPE_P (TREE_TYPE (srcstr))) |
5aef8938 | 3339 | { |
3340 | /* Try to determine the range of lengths the source string | |
8d6c6ef5 | 3341 | refers to. If it can be determined and is less than |
e6a18b5a | 3342 | the upper bound given by MAXREAD add one to it for |
5aef8938 | 3343 | the terminating nul. Otherwise, set it to one for |
e6a18b5a | 3344 | the same reason, or to MAXREAD as appropriate. */ |
14c286b1 | 3345 | c_strlen_data lendata = { }; |
3346 | get_range_strlen (srcstr, &lendata, /* eltsize = */ 1); | |
3347 | range[0] = lendata.minlen; | |
3348 | range[1] = lendata.maxbound; | |
e6a18b5a | 3349 | if (range[0] && (!maxread || TREE_CODE (maxread) == INTEGER_CST)) |
8d6c6ef5 | 3350 | { |
e6a18b5a | 3351 | if (maxread && tree_int_cst_le (maxread, range[0])) |
3352 | range[0] = range[1] = maxread; | |
8d6c6ef5 | 3353 | else |
3354 | range[0] = fold_build2 (PLUS_EXPR, size_type_node, | |
3355 | range[0], size_one_node); | |
3356 | ||
e6a18b5a | 3357 | if (maxread && tree_int_cst_le (maxread, range[1])) |
3358 | range[1] = maxread; | |
8d6c6ef5 | 3359 | else if (!integer_all_onesp (range[1])) |
3360 | range[1] = fold_build2 (PLUS_EXPR, size_type_node, | |
3361 | range[1], size_one_node); | |
3362 | ||
3363 | slen = range[0]; | |
3364 | } | |
5aef8938 | 3365 | else |
3366 | { | |
3367 | at_least_one = true; | |
3368 | slen = size_one_node; | |
3369 | } | |
3370 | } | |
3371 | else | |
e6a18b5a | 3372 | slen = srcstr; |
5aef8938 | 3373 | } |
3374 | ||
e6a18b5a | 3375 | if (!dstwrite && !maxread) |
5aef8938 | 3376 | { |
3377 | /* When the only available piece of data is the object size | |
3378 | there is nothing to do. */ | |
3379 | if (!slen) | |
3380 | return true; | |
3381 | ||
3382 | /* Otherwise, when the length of the source sequence is known | |
e6a18b5a | 3383 | (as with strlen), set DSTWRITE to it. */ |
8d6c6ef5 | 3384 | if (!range[0]) |
e6a18b5a | 3385 | dstwrite = slen; |
5aef8938 | 3386 | } |
3387 | ||
e6a18b5a | 3388 | if (!dstsize) |
3389 | dstsize = maxobjsize; | |
5aef8938 | 3390 | |
e6a18b5a | 3391 | if (dstwrite) |
3392 | get_size_range (dstwrite, range); | |
5aef8938 | 3393 | |
e6a18b5a | 3394 | tree func = get_callee_fndecl (exp); |
5aef8938 | 3395 | |
3396 | /* First check the number of bytes to be written against the maximum | |
3397 | object size. */ | |
c4183f31 | 3398 | if (range[0] |
3399 | && TREE_CODE (range[0]) == INTEGER_CST | |
3400 | && tree_int_cst_lt (maxobjsize, range[0])) | |
5aef8938 | 3401 | { |
864bd5de | 3402 | if (TREE_NO_WARNING (exp)) |
3403 | return false; | |
3404 | ||
5aef8938 | 3405 | location_t loc = tree_nonartificial_location (exp); |
4d317237 | 3406 | loc = expansion_point_location_if_in_system_header (loc); |
5aef8938 | 3407 | |
864bd5de | 3408 | bool warned; |
5aef8938 | 3409 | if (range[0] == range[1]) |
864bd5de | 3410 | warned = warning_at (loc, opt, |
3411 | "%K%qD specified size %E " | |
3412 | "exceeds maximum object size %E", | |
3413 | exp, func, range[0], maxobjsize); | |
3414 | else | |
3415 | warned = warning_at (loc, opt, | |
3416 | "%K%qD specified size between %E and %E " | |
3417 | "exceeds maximum object size %E", | |
3418 | exp, func, | |
3419 | range[0], range[1], maxobjsize); | |
3420 | if (warned) | |
3421 | TREE_NO_WARNING (exp) = true; | |
3422 | ||
5aef8938 | 3423 | return false; |
3424 | } | |
3425 | ||
e6a18b5a | 3426 | /* The number of bytes to write is "exact" if DSTWRITE is non-null, |
3427 | constant, and in range of unsigned HOST_WIDE_INT. */ | |
3428 | bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite); | |
3429 | ||
5aef8938 | 3430 | /* Next check the number of bytes to be written against the destination |
3431 | object size. */ | |
e6a18b5a | 3432 | if (range[0] || !exactwrite || integer_all_onesp (dstwrite)) |
5aef8938 | 3433 | { |
3434 | if (range[0] | |
c4183f31 | 3435 | && TREE_CODE (range[0]) == INTEGER_CST |
e6a18b5a | 3436 | && ((tree_fits_uhwi_p (dstsize) |
3437 | && tree_int_cst_lt (dstsize, range[0])) | |
c4183f31 | 3438 | || (dstwrite |
3439 | && tree_fits_uhwi_p (dstwrite) | |
e6a18b5a | 3440 | && tree_int_cst_lt (dstwrite, range[0])))) |
5aef8938 | 3441 | { |
080a1363 | 3442 | if (TREE_NO_WARNING (exp)) |
3443 | return false; | |
3444 | ||
5aef8938 | 3445 | location_t loc = tree_nonartificial_location (exp); |
4d317237 | 3446 | loc = expansion_point_location_if_in_system_header (loc); |
5aef8938 | 3447 | |
e6a18b5a | 3448 | if (dstwrite == slen && at_least_one) |
8d6c6ef5 | 3449 | { |
3450 | /* This is a call to strcpy with a destination of 0 size | |
3451 | and a source of unknown length. The call will write | |
3452 | at least one byte past the end of the destination. */ | |
3453 | warning_at (loc, opt, | |
9098b938 | 3454 | "%K%qD writing %E or more bytes into a region " |
8d6c6ef5 | 3455 | "of size %E overflows the destination", |
e6a18b5a | 3456 | exp, func, range[0], dstsize); |
8d6c6ef5 | 3457 | } |
3458 | else if (tree_int_cst_equal (range[0], range[1])) | |
625a4dfc | 3459 | warning_n (loc, opt, tree_to_uhwi (range[0]), |
3460 | "%K%qD writing %E byte into a region " | |
3461 | "of size %E overflows the destination", | |
3462 | "%K%qD writing %E bytes into a region " | |
3463 | "of size %E overflows the destination", | |
3464 | exp, func, range[0], dstsize); | |
8d6c6ef5 | 3465 | else if (tree_int_cst_sign_bit (range[1])) |
3466 | { | |
3467 | /* Avoid printing the upper bound if it's invalid. */ | |
3468 | warning_at (loc, opt, | |
9098b938 | 3469 | "%K%qD writing %E or more bytes into a region " |
8d6c6ef5 | 3470 | "of size %E overflows the destination", |
e6a18b5a | 3471 | exp, func, range[0], dstsize); |
8d6c6ef5 | 3472 | } |
5aef8938 | 3473 | else |
3474 | warning_at (loc, opt, | |
9098b938 | 3475 | "%K%qD writing between %E and %E bytes into " |
8d6c6ef5 | 3476 | "a region of size %E overflows the destination", |
e6a18b5a | 3477 | exp, func, range[0], range[1], |
3478 | dstsize); | |
5aef8938 | 3479 | |
3480 | /* Return error when an overflow has been detected. */ | |
3481 | return false; | |
3482 | } | |
3483 | } | |
3484 | ||
3485 | /* Check the maximum length of the source sequence against the size | |
3486 | of the destination object if known, or against the maximum size | |
3487 | of an object. */ | |
e6a18b5a | 3488 | if (maxread) |
5aef8938 | 3489 | { |
e6a18b5a | 3490 | get_size_range (maxread, range); |
3491 | ||
3492 | /* Use the lower end for MAXREAD from now on. */ | |
3493 | if (range[0]) | |
3494 | maxread = range[0]; | |
5aef8938 | 3495 | |
e6a18b5a | 3496 | if (range[0] && dstsize && tree_fits_uhwi_p (dstsize)) |
5aef8938 | 3497 | { |
3498 | location_t loc = tree_nonartificial_location (exp); | |
4d317237 | 3499 | loc = expansion_point_location_if_in_system_header (loc); |
5aef8938 | 3500 | |
3501 | if (tree_int_cst_lt (maxobjsize, range[0])) | |
3502 | { | |
080a1363 | 3503 | if (TREE_NO_WARNING (exp)) |
3504 | return false; | |
3505 | ||
5aef8938 | 3506 | /* Warn about crazy big sizes first since that's more |
3507 | likely to be meaningful than saying that the bound | |
3508 | is greater than the object size if both are big. */ | |
3509 | if (range[0] == range[1]) | |
3510 | warning_at (loc, opt, | |
9098b938 | 3511 | "%K%qD specified bound %E " |
8d6c6ef5 | 3512 | "exceeds maximum object size %E", |
e6a18b5a | 3513 | exp, func, |
8d6c6ef5 | 3514 | range[0], maxobjsize); |
5aef8938 | 3515 | else |
3516 | warning_at (loc, opt, | |
9098b938 | 3517 | "%K%qD specified bound between %E and %E " |
8d6c6ef5 | 3518 | "exceeds maximum object size %E", |
e6a18b5a | 3519 | exp, func, |
8d6c6ef5 | 3520 | range[0], range[1], maxobjsize); |
5aef8938 | 3521 | |
3522 | return false; | |
3523 | } | |
3524 | ||
e6a18b5a | 3525 | if (dstsize != maxobjsize && tree_int_cst_lt (dstsize, range[0])) |
5aef8938 | 3526 | { |
080a1363 | 3527 | if (TREE_NO_WARNING (exp)) |
3528 | return false; | |
3529 | ||
8d6c6ef5 | 3530 | if (tree_int_cst_equal (range[0], range[1])) |
5aef8938 | 3531 | warning_at (loc, opt, |
9098b938 | 3532 | "%K%qD specified bound %E " |
8d6c6ef5 | 3533 | "exceeds destination size %E", |
e6a18b5a | 3534 | exp, func, |
3535 | range[0], dstsize); | |
5aef8938 | 3536 | else |
3537 | warning_at (loc, opt, | |
9098b938 | 3538 | "%K%qD specified bound between %E and %E " |
8d6c6ef5 | 3539 | "exceeds destination size %E", |
e6a18b5a | 3540 | exp, func, |
3541 | range[0], range[1], dstsize); | |
5aef8938 | 3542 | return false; |
3543 | } | |
3544 | } | |
3545 | } | |
3546 | ||
e6a18b5a | 3547 | /* Check for reading past the end of SRC. */ |
8d6c6ef5 | 3548 | if (slen |
e6a18b5a | 3549 | && slen == srcstr |
3550 | && dstwrite && range[0] | |
8d6c6ef5 | 3551 | && tree_int_cst_lt (slen, range[0])) |
3552 | { | |
080a1363 | 3553 | if (TREE_NO_WARNING (exp)) |
3554 | return false; | |
3555 | ||
8d6c6ef5 | 3556 | location_t loc = tree_nonartificial_location (exp); |
3557 | ||
3558 | if (tree_int_cst_equal (range[0], range[1])) | |
625a4dfc | 3559 | warning_n (loc, opt, tree_to_uhwi (range[0]), |
3560 | "%K%qD reading %E byte from a region of size %E", | |
3561 | "%K%qD reading %E bytes from a region of size %E", | |
e6a18b5a | 3562 | exp, func, range[0], slen); |
8d6c6ef5 | 3563 | else if (tree_int_cst_sign_bit (range[1])) |
3564 | { | |
3565 | /* Avoid printing the upper bound if it's invalid. */ | |
3566 | warning_at (loc, opt, | |
9098b938 | 3567 | "%K%qD reading %E or more bytes from a region " |
8d6c6ef5 | 3568 | "of size %E", |
e6a18b5a | 3569 | exp, func, range[0], slen); |
8d6c6ef5 | 3570 | } |
3571 | else | |
3572 | warning_at (loc, opt, | |
9098b938 | 3573 | "%K%qD reading between %E and %E bytes from a region " |
8d6c6ef5 | 3574 | "of size %E", |
e6a18b5a | 3575 | exp, func, range[0], range[1], slen); |
8d6c6ef5 | 3576 | return false; |
3577 | } | |
3578 | ||
5aef8938 | 3579 | return true; |
3580 | } | |
3581 | ||
3582 | /* Helper to compute the size of the object referenced by the DEST | |
d8aad786 | 3583 | expression which must have pointer type, using Object Size type |
5aef8938 | 3584 | OSTYPE (only the least significant 2 bits are used). Return |
24e3b821 | 3585 | an estimate of the size of the object if successful or NULL when |
3586 | the size cannot be determined. When the referenced object involves | |
3587 | a non-constant offset in some range the returned value represents | |
3588 | the largest size given the smallest non-negative offset in the | |
3589 | range. The function is intended for diagnostics and should not | |
3590 | be used to influence code generation or optimization. */ | |
5aef8938 | 3591 | |
d8aad786 | 3592 | tree |
8d6c6ef5 | 3593 | compute_objsize (tree dest, int ostype) |
5aef8938 | 3594 | { |
3595 | unsigned HOST_WIDE_INT size; | |
d8aad786 | 3596 | |
3597 | /* Only the two least significant bits are meaningful. */ | |
3598 | ostype &= 3; | |
3599 | ||
3600 | if (compute_builtin_object_size (dest, ostype, &size)) | |
5aef8938 | 3601 | return build_int_cst (sizetype, size); |
3602 | ||
d8aad786 | 3603 | if (TREE_CODE (dest) == SSA_NAME) |
3604 | { | |
3605 | gimple *stmt = SSA_NAME_DEF_STMT (dest); | |
3606 | if (!is_gimple_assign (stmt)) | |
3607 | return NULL_TREE; | |
3608 | ||
24e3b821 | 3609 | dest = gimple_assign_rhs1 (stmt); |
3610 | ||
d8aad786 | 3611 | tree_code code = gimple_assign_rhs_code (stmt); |
24e3b821 | 3612 | if (code == POINTER_PLUS_EXPR) |
3613 | { | |
3614 | /* compute_builtin_object_size fails for addresses with | |
3615 | non-constant offsets. Try to determine the range of | |
c1a0c86c | 3616 | such an offset here and use it to adjust the constant |
24e3b821 | 3617 | size. */ |
3618 | tree off = gimple_assign_rhs2 (stmt); | |
c1a0c86c | 3619 | if (TREE_CODE (off) == INTEGER_CST) |
3620 | { | |
3621 | if (tree size = compute_objsize (dest, ostype)) | |
3622 | { | |
3623 | wide_int wioff = wi::to_wide (off); | |
3624 | wide_int wisiz = wi::to_wide (size); | |
3625 | ||
3626 | /* Ignore negative offsets for now. For others, | |
3627 | use the lower bound as the most optimistic | |
3628 | estimate of the (remaining) size. */ | |
3629 | if (wi::sign_mask (wioff)) | |
3630 | ; | |
3631 | else if (wi::ltu_p (wioff, wisiz)) | |
3632 | return wide_int_to_tree (TREE_TYPE (size), | |
3633 | wi::sub (wisiz, wioff)); | |
3634 | else | |
3635 | return size_zero_node; | |
3636 | } | |
3637 | } | |
3638 | else if (TREE_CODE (off) == SSA_NAME | |
24e3b821 | 3639 | && INTEGRAL_TYPE_P (TREE_TYPE (off))) |
3640 | { | |
3641 | wide_int min, max; | |
be44111e | 3642 | enum value_range_kind rng = get_range_info (off, &min, &max); |
24e3b821 | 3643 | |
3644 | if (rng == VR_RANGE) | |
3645 | { | |
3646 | if (tree size = compute_objsize (dest, ostype)) | |
3647 | { | |
3648 | wide_int wisiz = wi::to_wide (size); | |
3649 | ||
3650 | /* Ignore negative offsets for now. For others, | |
3651 | use the lower bound as the most optimistic | |
3652 | estimate of the (remaining)size. */ | |
3653 | if (wi::sign_mask (min)) | |
3654 | ; | |
3655 | else if (wi::ltu_p (min, wisiz)) | |
3656 | return wide_int_to_tree (TREE_TYPE (size), | |
3657 | wi::sub (wisiz, min)); | |
3658 | else | |
3659 | return size_zero_node; | |
3660 | } | |
3661 | } | |
3662 | } | |
3663 | } | |
3664 | else if (code != ADDR_EXPR) | |
d8aad786 | 3665 | return NULL_TREE; |
d8aad786 | 3666 | } |
3667 | ||
24e3b821 | 3668 | /* Unless computing the largest size (for memcpy and other raw memory |
3669 | functions), try to determine the size of the object from its type. */ | |
3670 | if (!ostype) | |
3671 | return NULL_TREE; | |
3672 | ||
d8aad786 | 3673 | if (TREE_CODE (dest) != ADDR_EXPR) |
3674 | return NULL_TREE; | |
3675 | ||
3676 | tree type = TREE_TYPE (dest); | |
3677 | if (TREE_CODE (type) == POINTER_TYPE) | |
3678 | type = TREE_TYPE (type); | |
3679 | ||
3680 | type = TYPE_MAIN_VARIANT (type); | |
3681 | ||
3682 | if (TREE_CODE (type) == ARRAY_TYPE | |
d4ad98ea | 3683 | && !array_at_struct_end_p (TREE_OPERAND (dest, 0))) |
d8aad786 | 3684 | { |
3685 | /* Return the constant size unless it's zero (that's a zero-length | |
3686 | array likely at the end of a struct). */ | |
3687 | tree size = TYPE_SIZE_UNIT (type); | |
3688 | if (size && TREE_CODE (size) == INTEGER_CST | |
3689 | && !integer_zerop (size)) | |
3690 | return size; | |
3691 | } | |
3692 | ||
5aef8938 | 3693 | return NULL_TREE; |
3694 | } | |
3695 | ||
3696 | /* Helper to determine and check the sizes of the source and the destination | |
8d6c6ef5 | 3697 | of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. EXP is the |
3698 | call expression, DEST is the destination argument, SRC is the source | |
3699 | argument or null, and LEN is the number of bytes. Use Object Size type-0 | |
3700 | regardless of the OPT_Wstringop_overflow_ setting. Return true on success | |
5aef8938 | 3701 | (no overflow or invalid sizes), false otherwise. */ |
3702 | ||
3703 | static bool | |
e6a18b5a | 3704 | check_memop_access (tree exp, tree dest, tree src, tree size) |
5aef8938 | 3705 | { |
5aef8938 | 3706 | /* For functions like memset and memcpy that operate on raw memory |
8d6c6ef5 | 3707 | try to determine the size of the largest source and destination |
3708 | object using type-0 Object Size regardless of the object size | |
3709 | type specified by the option. */ | |
3710 | tree srcsize = src ? compute_objsize (src, 0) : NULL_TREE; | |
3711 | tree dstsize = compute_objsize (dest, 0); | |
5aef8938 | 3712 | |
e6a18b5a | 3713 | return check_access (exp, dest, src, size, /*maxread=*/NULL_TREE, |
3714 | srcsize, dstsize); | |
8d6c6ef5 | 3715 | } |
3716 | ||
3717 | /* Validate memchr arguments without performing any expansion. | |
3718 | Return NULL_RTX. */ | |
3719 | ||
3720 | static rtx | |
3721 | expand_builtin_memchr (tree exp, rtx) | |
3722 | { | |
3723 | if (!validate_arglist (exp, | |
3724 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3725 | return NULL_RTX; | |
3726 | ||
3727 | tree arg1 = CALL_EXPR_ARG (exp, 0); | |
3728 | tree len = CALL_EXPR_ARG (exp, 2); | |
3729 | ||
3730 | /* Diagnose calls where the specified length exceeds the size | |
3731 | of the object. */ | |
3732 | if (warn_stringop_overflow) | |
3733 | { | |
3734 | tree size = compute_objsize (arg1, 0); | |
e6a18b5a | 3735 | check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len, |
3736 | /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE); | |
8d6c6ef5 | 3737 | } |
3738 | ||
3739 | return NULL_RTX; | |
5aef8938 | 3740 | } |
3741 | ||
c2f47e15 | 3742 | /* Expand a call EXP to the memcpy builtin. |
3743 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
3b824fa6 | 3744 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 3745 | mode MODE if that's convenient). */ |
c2f47e15 | 3746 | |
53800dbe | 3747 | static rtx |
a65c4d64 | 3748 | expand_builtin_memcpy (tree exp, rtx target) |
53800dbe | 3749 | { |
c2f47e15 | 3750 | if (!validate_arglist (exp, |
3751 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3752 | return NULL_RTX; | |
5aef8938 | 3753 | |
3754 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3755 | tree src = CALL_EXPR_ARG (exp, 1); | |
3756 | tree len = CALL_EXPR_ARG (exp, 2); | |
3757 | ||
e6a18b5a | 3758 | check_memop_access (exp, dest, src, len); |
5aef8938 | 3759 | |
d0fbba1a | 3760 | return expand_builtin_memory_copy_args (dest, src, len, target, exp, |
02aa6d73 | 3761 | /*retmode=*/ RETURN_BEGIN); |
f21337ef | 3762 | } |
6840589f | 3763 | |
4d317237 | 3764 | /* Check a call EXP to the memmove built-in for validity. |
3765 | Return NULL_RTX on both success and failure. */ | |
3766 | ||
3767 | static rtx | |
3768 | expand_builtin_memmove (tree exp, rtx) | |
3769 | { | |
3770 | if (!validate_arglist (exp, | |
3771 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3772 | return NULL_RTX; | |
3773 | ||
3774 | tree dest = CALL_EXPR_ARG (exp, 0); | |
8d6c6ef5 | 3775 | tree src = CALL_EXPR_ARG (exp, 1); |
4d317237 | 3776 | tree len = CALL_EXPR_ARG (exp, 2); |
3777 | ||
e6a18b5a | 3778 | check_memop_access (exp, dest, src, len); |
4d317237 | 3779 | |
3780 | return NULL_RTX; | |
3781 | } | |
3782 | ||
c2f47e15 | 3783 | /* Expand a call EXP to the mempcpy builtin. |
3784 | Return NULL_RTX if we failed; the caller should emit a normal call, | |
647661c6 | 3785 | otherwise try to get the result in TARGET, if convenient (and in |
02aa6d73 | 3786 | mode MODE if that's convenient). */ |
647661c6 | 3787 | |
3788 | static rtx | |
d0fbba1a | 3789 | expand_builtin_mempcpy (tree exp, rtx target) |
647661c6 | 3790 | { |
c2f47e15 | 3791 | if (!validate_arglist (exp, |
3792 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3793 | return NULL_RTX; | |
5aef8938 | 3794 | |
3795 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3796 | tree src = CALL_EXPR_ARG (exp, 1); | |
3797 | tree len = CALL_EXPR_ARG (exp, 2); | |
3798 | ||
24e3b821 | 3799 | /* Policy does not generally allow using compute_objsize (which |
3800 | is used internally by check_memop_size) to change code generation | |
3801 | or drive optimization decisions. | |
3802 | ||
3803 | In this instance it is safe because the code we generate has | |
3804 | the same semantics regardless of the return value of | |
3805 | check_memop_sizes. Exactly the same amount of data is copied | |
3806 | and the return value is exactly the same in both cases. | |
3807 | ||
3808 | Furthermore, check_memop_size always uses mode 0 for the call to | |
3809 | compute_objsize, so the imprecise nature of compute_objsize is | |
3810 | avoided. */ | |
3811 | ||
5aef8938 | 3812 | /* Avoid expanding mempcpy into memcpy when the call is determined |
3813 | to overflow the buffer. This also prevents the same overflow | |
3814 | from being diagnosed again when expanding memcpy. */ | |
e6a18b5a | 3815 | if (!check_memop_access (exp, dest, src, len)) |
5aef8938 | 3816 | return NULL_RTX; |
3817 | ||
3818 | return expand_builtin_mempcpy_args (dest, src, len, | |
02aa6d73 | 3819 | target, exp, /*retmode=*/ RETURN_END); |
f21337ef | 3820 | } |
3821 | ||
d0fbba1a | 3822 | /* Helper function to do the actual work for expand of memory copy family |
3823 | functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes | |
02aa6d73 | 3824 | of memory from SRC to DEST and assign to TARGET if convenient. Return |
3825 | value is based on RETMODE argument. */ | |
c2f47e15 | 3826 | |
3827 | static rtx | |
d0fbba1a | 3828 | expand_builtin_memory_copy_args (tree dest, tree src, tree len, |
02aa6d73 | 3829 | rtx target, tree exp, memop_ret retmode) |
c2f47e15 | 3830 | { |
d0fbba1a | 3831 | const char *src_str; |
3832 | unsigned int src_align = get_pointer_alignment (src); | |
3833 | unsigned int dest_align = get_pointer_alignment (dest); | |
3834 | rtx dest_mem, src_mem, dest_addr, len_rtx; | |
3835 | HOST_WIDE_INT expected_size = -1; | |
3836 | unsigned int expected_align = 0; | |
3837 | unsigned HOST_WIDE_INT min_size; | |
3838 | unsigned HOST_WIDE_INT max_size; | |
3839 | unsigned HOST_WIDE_INT probable_max_size; | |
f21337ef | 3840 | |
d0fbba1a | 3841 | /* If DEST is not a pointer type, call the normal function. */ |
3842 | if (dest_align == 0) | |
3843 | return NULL_RTX; | |
a0c938f0 | 3844 | |
d0fbba1a | 3845 | /* If either SRC is not a pointer type, don't do this |
3846 | operation in-line. */ | |
3847 | if (src_align == 0) | |
3848 | return NULL_RTX; | |
9fe0e1b8 | 3849 | |
d0fbba1a | 3850 | if (currently_expanding_gimple_stmt) |
3851 | stringop_block_profile (currently_expanding_gimple_stmt, | |
3852 | &expected_align, &expected_size); | |
0862b7e9 | 3853 | |
d0fbba1a | 3854 | if (expected_align < dest_align) |
3855 | expected_align = dest_align; | |
3856 | dest_mem = get_memory_rtx (dest, len); | |
3857 | set_mem_align (dest_mem, dest_align); | |
3858 | len_rtx = expand_normal (len); | |
3859 | determine_block_size (len, len_rtx, &min_size, &max_size, | |
3860 | &probable_max_size); | |
3861 | src_str = c_getstr (src); | |
647661c6 | 3862 | |
d0fbba1a | 3863 | /* If SRC is a string constant and block move would be done |
3864 | by pieces, we can avoid loading the string from memory | |
3865 | and only stored the computed constants. */ | |
3866 | if (src_str | |
3867 | && CONST_INT_P (len_rtx) | |
3868 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
3869 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
3870 | CONST_CAST (char *, src_str), | |
3871 | dest_align, false)) | |
3872 | { | |
3873 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
3874 | builtin_memcpy_read_str, | |
d72123ce | 3875 | CONST_CAST (char *, src_str), |
02aa6d73 | 3876 | dest_align, false, retmode); |
d0fbba1a | 3877 | dest_mem = force_operand (XEXP (dest_mem, 0), target); |
3878 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3879 | return dest_mem; | |
3880 | } | |
647661c6 | 3881 | |
d0fbba1a | 3882 | src_mem = get_memory_rtx (src, len); |
3883 | set_mem_align (src_mem, src_align); | |
9fe0e1b8 | 3884 | |
d0fbba1a | 3885 | /* Copy word part most expediently. */ |
21781799 | 3886 | enum block_op_methods method = BLOCK_OP_NORMAL; |
02aa6d73 | 3887 | if (CALL_EXPR_TAILCALL (exp) |
3888 | && (retmode == RETURN_BEGIN || target == const0_rtx)) | |
21781799 | 3889 | method = BLOCK_OP_TAILCALL; |
02aa6d73 | 3890 | if (retmode == RETURN_END && target != const0_rtx) |
21781799 | 3891 | method = BLOCK_OP_NO_LIBCALL_RET; |
3892 | dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method, | |
d0fbba1a | 3893 | expected_align, expected_size, |
3894 | min_size, max_size, probable_max_size); | |
21781799 | 3895 | if (dest_addr == pc_rtx) |
3896 | return NULL_RTX; | |
d0fbba1a | 3897 | |
3898 | if (dest_addr == 0) | |
3899 | { | |
3900 | dest_addr = force_operand (XEXP (dest_mem, 0), target); | |
3901 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
3902 | } | |
3903 | ||
02aa6d73 | 3904 | if (retmode != RETURN_BEGIN && target != const0_rtx) |
d0fbba1a | 3905 | { |
3906 | dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx); | |
3907 | /* stpcpy pointer to last byte. */ | |
02aa6d73 | 3908 | if (retmode == RETURN_END_MINUS_ONE) |
d0fbba1a | 3909 | dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx); |
d72123ce | 3910 | } |
d0fbba1a | 3911 | |
3912 | return dest_addr; | |
3913 | } | |
3914 | ||
3915 | static rtx | |
3916 | expand_builtin_mempcpy_args (tree dest, tree src, tree len, | |
02aa6d73 | 3917 | rtx target, tree orig_exp, memop_ret retmode) |
d0fbba1a | 3918 | { |
3919 | return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp, | |
02aa6d73 | 3920 | retmode); |
647661c6 | 3921 | } |
3922 | ||
c2f47e15 | 3923 | /* Expand into a movstr instruction, if one is available. Return NULL_RTX if |
727c62dd | 3924 | we failed, the caller should emit a normal call, otherwise try to |
02aa6d73 | 3925 | get the result in TARGET, if convenient. |
3926 | Return value is based on RETMODE argument. */ | |
727c62dd | 3927 | |
3928 | static rtx | |
02aa6d73 | 3929 | expand_movstr (tree dest, tree src, rtx target, memop_ret retmode) |
727c62dd | 3930 | { |
8786db1e | 3931 | struct expand_operand ops[3]; |
727c62dd | 3932 | rtx dest_mem; |
3933 | rtx src_mem; | |
727c62dd | 3934 | |
8d74dc42 | 3935 | if (!targetm.have_movstr ()) |
c2f47e15 | 3936 | return NULL_RTX; |
727c62dd | 3937 | |
d8ae1baa | 3938 | dest_mem = get_memory_rtx (dest, NULL); |
3939 | src_mem = get_memory_rtx (src, NULL); | |
74a1cc74 | 3940 | if (retmode == RETURN_BEGIN) |
727c62dd | 3941 | { |
3942 | target = force_reg (Pmode, XEXP (dest_mem, 0)); | |
3943 | dest_mem = replace_equiv_address (dest_mem, target); | |
727c62dd | 3944 | } |
3945 | ||
24c9d723 | 3946 | create_output_operand (&ops[0], |
3947 | retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode); | |
8786db1e | 3948 | create_fixed_operand (&ops[1], dest_mem); |
3949 | create_fixed_operand (&ops[2], src_mem); | |
8d74dc42 | 3950 | if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops)) |
1e1d5623 | 3951 | return NULL_RTX; |
727c62dd | 3952 | |
02aa6d73 | 3953 | if (retmode != RETURN_BEGIN && target != const0_rtx) |
c5aba89c | 3954 | { |
8786db1e | 3955 | target = ops[0].value; |
3956 | /* movstr is supposed to set end to the address of the NUL | |
3957 | terminator. If the caller requested a mempcpy-like return value, | |
3958 | adjust it. */ | |
02aa6d73 | 3959 | if (retmode == RETURN_END) |
8786db1e | 3960 | { |
29c05e22 | 3961 | rtx tem = plus_constant (GET_MODE (target), |
3962 | gen_lowpart (GET_MODE (target), target), 1); | |
8786db1e | 3963 | emit_move_insn (target, force_operand (tem, NULL_RTX)); |
3964 | } | |
c5aba89c | 3965 | } |
727c62dd | 3966 | return target; |
3967 | } | |
3968 | ||
5aef8938 | 3969 | /* Do some very basic size validation of a call to the strcpy builtin |
3970 | given by EXP. Return NULL_RTX to have the built-in expand to a call | |
3971 | to the library function. */ | |
3972 | ||
3973 | static rtx | |
3974 | expand_builtin_strcat (tree exp, rtx) | |
3975 | { | |
3976 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE) | |
3977 | || !warn_stringop_overflow) | |
3978 | return NULL_RTX; | |
3979 | ||
3980 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3981 | tree src = CALL_EXPR_ARG (exp, 1); | |
3982 | ||
3983 | /* There is no way here to determine the length of the string in | |
3984 | the destination to which the SRC string is being appended so | |
3985 | just diagnose cases when the souce string is longer than | |
3986 | the destination object. */ | |
3987 | ||
8d6c6ef5 | 3988 | tree destsize = compute_objsize (dest, warn_stringop_overflow - 1); |
5aef8938 | 3989 | |
e6a18b5a | 3990 | check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, src, |
3991 | destsize); | |
5aef8938 | 3992 | |
3993 | return NULL_RTX; | |
3994 | } | |
3995 | ||
48e1416a | 3996 | /* Expand expression EXP, which is a call to the strcpy builtin. Return |
3997 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
c2f47e15 | 3998 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
6f428e8b | 3999 | convenient). */ |
902de8ed | 4000 | |
53800dbe | 4001 | static rtx |
a65c4d64 | 4002 | expand_builtin_strcpy (tree exp, rtx target) |
53800dbe | 4003 | { |
5aef8938 | 4004 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4005 | return NULL_RTX; | |
4006 | ||
4007 | tree dest = CALL_EXPR_ARG (exp, 0); | |
4008 | tree src = CALL_EXPR_ARG (exp, 1); | |
4009 | ||
4010 | if (warn_stringop_overflow) | |
4011 | { | |
8d6c6ef5 | 4012 | tree destsize = compute_objsize (dest, warn_stringop_overflow - 1); |
e6a18b5a | 4013 | check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, |
4014 | src, destsize); | |
5aef8938 | 4015 | } |
4016 | ||
a788aa5f | 4017 | if (rtx ret = expand_builtin_strcpy_args (exp, dest, src, target)) |
0b39ade8 | 4018 | { |
4019 | /* Check to see if the argument was declared attribute nonstring | |
4020 | and if so, issue a warning since at this point it's not known | |
4021 | to be nul-terminated. */ | |
4022 | tree fndecl = get_callee_fndecl (exp); | |
4023 | maybe_warn_nonstring_arg (fndecl, exp); | |
4024 | return ret; | |
4025 | } | |
4026 | ||
4027 | return NULL_RTX; | |
c2f47e15 | 4028 | } |
4029 | ||
4030 | /* Helper function to do the actual work for expand_builtin_strcpy. The | |
4031 | arguments to the builtin_strcpy call DEST and SRC are broken out | |
4032 | so that this can also be called without constructing an actual CALL_EXPR. | |
4033 | The other arguments and return value are the same as for | |
4034 | expand_builtin_strcpy. */ | |
4035 | ||
4036 | static rtx | |
a788aa5f | 4037 | expand_builtin_strcpy_args (tree exp, tree dest, tree src, rtx target) |
c2f47e15 | 4038 | { |
a788aa5f | 4039 | /* Detect strcpy calls with unterminated arrays.. */ |
4040 | if (tree nonstr = unterminated_array (src)) | |
4041 | { | |
4042 | /* NONSTR refers to the non-nul terminated constant array. */ | |
4043 | if (!TREE_NO_WARNING (exp)) | |
4044 | warn_string_no_nul (EXPR_LOCATION (exp), "strcpy", src, nonstr); | |
4045 | return NULL_RTX; | |
4046 | } | |
4047 | ||
02aa6d73 | 4048 | return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN); |
53800dbe | 4049 | } |
4050 | ||
c2f47e15 | 4051 | /* Expand a call EXP to the stpcpy builtin. |
4052 | Return NULL_RTX if we failed the caller should emit a normal call, | |
3b824fa6 | 4053 | otherwise try to get the result in TARGET, if convenient (and in |
4054 | mode MODE if that's convenient). */ | |
4055 | ||
4056 | static rtx | |
df6e8b42 | 4057 | expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode) |
3b824fa6 | 4058 | { |
c2f47e15 | 4059 | tree dst, src; |
389dd41b | 4060 | location_t loc = EXPR_LOCATION (exp); |
c2f47e15 | 4061 | |
4062 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
4063 | return NULL_RTX; | |
4064 | ||
4065 | dst = CALL_EXPR_ARG (exp, 0); | |
4066 | src = CALL_EXPR_ARG (exp, 1); | |
4067 | ||
4d317237 | 4068 | if (warn_stringop_overflow) |
4069 | { | |
8d6c6ef5 | 4070 | tree destsize = compute_objsize (dst, warn_stringop_overflow - 1); |
e6a18b5a | 4071 | check_access (exp, dst, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, |
4072 | src, destsize); | |
4d317237 | 4073 | } |
4074 | ||
727c62dd | 4075 | /* If return value is ignored, transform stpcpy into strcpy. */ |
b9a16870 | 4076 | if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY)) |
978836e5 | 4077 | { |
b9a16870 | 4078 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); |
0568e9c1 | 4079 | tree result = build_call_nofold_loc (loc, fn, 2, dst, src); |
c8b17b2e | 4080 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
978836e5 | 4081 | } |
3b824fa6 | 4082 | else |
4083 | { | |
c2f47e15 | 4084 | tree len, lenp1; |
727c62dd | 4085 | rtx ret; |
647661c6 | 4086 | |
9fe0e1b8 | 4087 | /* Ensure we get an actual string whose length can be evaluated at |
a0c938f0 | 4088 | compile-time, not an expression containing a string. This is |
4089 | because the latter will potentially produce pessimized code | |
4090 | when used to produce the return value. */ | |
98d5ba5d | 4091 | c_strlen_data lendata = { }; |
50e57712 | 4092 | if (!c_getstr (src, NULL) |
98d5ba5d | 4093 | || !(len = c_strlen (src, 0, &lendata, 1))) |
02aa6d73 | 4094 | return expand_movstr (dst, src, target, |
4095 | /*retmode=*/ RETURN_END_MINUS_ONE); | |
3b824fa6 | 4096 | |
98d5ba5d | 4097 | if (lendata.decl && !TREE_NO_WARNING (exp)) |
4098 | warn_string_no_nul (EXPR_LOCATION (exp), "stpcpy", src, lendata.decl); | |
50e57712 | 4099 | |
389dd41b | 4100 | lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1)); |
a65c4d64 | 4101 | ret = expand_builtin_mempcpy_args (dst, src, lenp1, |
02aa6d73 | 4102 | target, exp, |
4103 | /*retmode=*/ RETURN_END_MINUS_ONE); | |
727c62dd | 4104 | |
4105 | if (ret) | |
4106 | return ret; | |
4107 | ||
4108 | if (TREE_CODE (len) == INTEGER_CST) | |
4109 | { | |
8ec3c5c2 | 4110 | rtx len_rtx = expand_normal (len); |
727c62dd | 4111 | |
971ba038 | 4112 | if (CONST_INT_P (len_rtx)) |
727c62dd | 4113 | { |
a788aa5f | 4114 | ret = expand_builtin_strcpy_args (exp, dst, src, target); |
727c62dd | 4115 | |
4116 | if (ret) | |
4117 | { | |
4118 | if (! target) | |
7ac87324 | 4119 | { |
4120 | if (mode != VOIDmode) | |
4121 | target = gen_reg_rtx (mode); | |
4122 | else | |
4123 | target = gen_reg_rtx (GET_MODE (ret)); | |
4124 | } | |
727c62dd | 4125 | if (GET_MODE (target) != GET_MODE (ret)) |
4126 | ret = gen_lowpart (GET_MODE (target), ret); | |
4127 | ||
29c05e22 | 4128 | ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx)); |
c5aba89c | 4129 | ret = emit_move_insn (target, force_operand (ret, NULL_RTX)); |
64db345d | 4130 | gcc_assert (ret); |
727c62dd | 4131 | |
4132 | return target; | |
4133 | } | |
4134 | } | |
4135 | } | |
4136 | ||
02aa6d73 | 4137 | return expand_movstr (dst, src, target, |
4138 | /*retmode=*/ RETURN_END_MINUS_ONE); | |
3b824fa6 | 4139 | } |
4140 | } | |
4141 | ||
df6e8b42 | 4142 | /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring |
4143 | arguments while being careful to avoid duplicate warnings (which could | |
4144 | be issued if the expander were to expand the call, resulting in it | |
4145 | being emitted in expand_call(). */ | |
4146 | ||
4147 | static rtx | |
4148 | expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode) | |
4149 | { | |
4150 | if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode)) | |
4151 | { | |
4152 | /* The call has been successfully expanded. Check for nonstring | |
4153 | arguments and issue warnings as appropriate. */ | |
4154 | maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp); | |
4155 | return ret; | |
4156 | } | |
4157 | ||
4158 | return NULL_RTX; | |
4159 | } | |
4160 | ||
4d317237 | 4161 | /* Check a call EXP to the stpncpy built-in for validity. |
4162 | Return NULL_RTX on both success and failure. */ | |
4163 | ||
4164 | static rtx | |
4165 | expand_builtin_stpncpy (tree exp, rtx) | |
4166 | { | |
4167 | if (!validate_arglist (exp, | |
4168 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
4169 | || !warn_stringop_overflow) | |
4170 | return NULL_RTX; | |
4171 | ||
aca1a787 | 4172 | /* The source and destination of the call. */ |
4d317237 | 4173 | tree dest = CALL_EXPR_ARG (exp, 0); |
4174 | tree src = CALL_EXPR_ARG (exp, 1); | |
4175 | ||
aca1a787 | 4176 | /* The exact number of bytes to write (not the maximum). */ |
4d317237 | 4177 | tree len = CALL_EXPR_ARG (exp, 2); |
4d317237 | 4178 | |
aca1a787 | 4179 | /* The size of the destination object. */ |
8d6c6ef5 | 4180 | tree destsize = compute_objsize (dest, warn_stringop_overflow - 1); |
4d317237 | 4181 | |
e6a18b5a | 4182 | check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src, destsize); |
4d317237 | 4183 | |
4184 | return NULL_RTX; | |
4185 | } | |
4186 | ||
6840589f | 4187 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
4188 | bytes from constant string DATA + OFFSET and return it as target | |
4189 | constant. */ | |
4190 | ||
09879952 | 4191 | rtx |
aecda0d6 | 4192 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
f77c4496 | 4193 | scalar_int_mode mode) |
6840589f | 4194 | { |
4195 | const char *str = (const char *) data; | |
4196 | ||
4197 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
4198 | return const0_rtx; | |
4199 | ||
4200 | return c_readstr (str + offset, mode); | |
4201 | } | |
4202 | ||
5aef8938 | 4203 | /* Helper to check the sizes of sequences and the destination of calls |
4204 | to __builtin_strncat and __builtin___strncat_chk. Returns true on | |
4205 | success (no overflow or invalid sizes), false otherwise. */ | |
4206 | ||
4207 | static bool | |
4208 | check_strncat_sizes (tree exp, tree objsize) | |
4209 | { | |
4210 | tree dest = CALL_EXPR_ARG (exp, 0); | |
4211 | tree src = CALL_EXPR_ARG (exp, 1); | |
e6a18b5a | 4212 | tree maxread = CALL_EXPR_ARG (exp, 2); |
5aef8938 | 4213 | |
4214 | /* Try to determine the range of lengths that the source expression | |
4215 | refers to. */ | |
14c286b1 | 4216 | c_strlen_data lendata = { }; |
4217 | get_range_strlen (src, &lendata, /* eltsize = */ 1); | |
5aef8938 | 4218 | |
4219 | /* Try to verify that the destination is big enough for the shortest | |
4220 | string. */ | |
4221 | ||
4222 | if (!objsize && warn_stringop_overflow) | |
4223 | { | |
4224 | /* If it hasn't been provided by __strncat_chk, try to determine | |
4225 | the size of the destination object into which the source is | |
4226 | being copied. */ | |
8d6c6ef5 | 4227 | objsize = compute_objsize (dest, warn_stringop_overflow - 1); |
5aef8938 | 4228 | } |
4229 | ||
4230 | /* Add one for the terminating nul. */ | |
14c286b1 | 4231 | tree srclen = (lendata.minlen |
4232 | ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen, | |
5aef8938 | 4233 | size_one_node) |
4234 | : NULL_TREE); | |
4235 | ||
e6a18b5a | 4236 | /* The strncat function copies at most MAXREAD bytes and always appends |
4237 | the terminating nul so the specified upper bound should never be equal | |
4238 | to (or greater than) the size of the destination. */ | |
4239 | if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize) | |
4240 | && tree_int_cst_equal (objsize, maxread)) | |
5aef8938 | 4241 | { |
4d317237 | 4242 | location_t loc = tree_nonartificial_location (exp); |
4243 | loc = expansion_point_location_if_in_system_header (loc); | |
4244 | ||
4245 | warning_at (loc, OPT_Wstringop_overflow_, | |
9098b938 | 4246 | "%K%qD specified bound %E equals destination size", |
e6a18b5a | 4247 | exp, get_callee_fndecl (exp), maxread); |
5aef8938 | 4248 | |
4249 | return false; | |
4250 | } | |
4251 | ||
4252 | if (!srclen | |
e6a18b5a | 4253 | || (maxread && tree_fits_uhwi_p (maxread) |
5aef8938 | 4254 | && tree_fits_uhwi_p (srclen) |
e6a18b5a | 4255 | && tree_int_cst_lt (maxread, srclen))) |
4256 | srclen = maxread; | |
5aef8938 | 4257 | |
e6a18b5a | 4258 | /* The number of bytes to write is LEN but check_access will also |
5aef8938 | 4259 | check SRCLEN if LEN's value isn't known. */ |
e6a18b5a | 4260 | return check_access (exp, dest, src, /*size=*/NULL_TREE, maxread, srclen, |
4261 | objsize); | |
5aef8938 | 4262 | } |
4263 | ||
4264 | /* Similar to expand_builtin_strcat, do some very basic size validation | |
4265 | of a call to the strcpy builtin given by EXP. Return NULL_RTX to have | |
4266 | the built-in expand to a call to the library function. */ | |
4267 | ||
4268 | static rtx | |
4269 | expand_builtin_strncat (tree exp, rtx) | |
4270 | { | |
4271 | if (!validate_arglist (exp, | |
4272 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
4273 | || !warn_stringop_overflow) | |
4274 | return NULL_RTX; | |
4275 | ||
4276 | tree dest = CALL_EXPR_ARG (exp, 0); | |
4277 | tree src = CALL_EXPR_ARG (exp, 1); | |
4278 | /* The upper bound on the number of bytes to write. */ | |
e6a18b5a | 4279 | tree maxread = CALL_EXPR_ARG (exp, 2); |
5aef8938 | 4280 | /* The length of the source sequence. */ |
4281 | tree slen = c_strlen (src, 1); | |
4282 | ||
4283 | /* Try to determine the range of lengths that the source expression | |
14c286b1 | 4284 | refers to. Since the lengths are only used for warning and not |
4285 | for code generation disable strict mode below. */ | |
4286 | tree maxlen = slen; | |
4287 | if (!maxlen) | |
4288 | { | |
4289 | c_strlen_data lendata = { }; | |
4290 | get_range_strlen (src, &lendata, /* eltsize = */ 1); | |
4291 | maxlen = lendata.maxbound; | |
4292 | } | |
5aef8938 | 4293 | |
4294 | /* Try to verify that the destination is big enough for the shortest | |
4295 | string. First try to determine the size of the destination object | |
4296 | into which the source is being copied. */ | |
8d6c6ef5 | 4297 | tree destsize = compute_objsize (dest, warn_stringop_overflow - 1); |
5aef8938 | 4298 | |
4299 | /* Add one for the terminating nul. */ | |
14c286b1 | 4300 | tree srclen = (maxlen |
4301 | ? fold_build2 (PLUS_EXPR, size_type_node, maxlen, | |
5aef8938 | 4302 | size_one_node) |
4303 | : NULL_TREE); | |
4304 | ||
e6a18b5a | 4305 | /* The strncat function copies at most MAXREAD bytes and always appends |
4306 | the terminating nul so the specified upper bound should never be equal | |
4307 | to (or greater than) the size of the destination. */ | |
4308 | if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize) | |
4309 | && tree_int_cst_equal (destsize, maxread)) | |
5aef8938 | 4310 | { |
4d317237 | 4311 | location_t loc = tree_nonartificial_location (exp); |
4312 | loc = expansion_point_location_if_in_system_header (loc); | |
4313 | ||
4314 | warning_at (loc, OPT_Wstringop_overflow_, | |
9098b938 | 4315 | "%K%qD specified bound %E equals destination size", |
e6a18b5a | 4316 | exp, get_callee_fndecl (exp), maxread); |
5aef8938 | 4317 | |
4318 | return NULL_RTX; | |
4319 | } | |
4320 | ||
4321 | if (!srclen | |
e6a18b5a | 4322 | || (maxread && tree_fits_uhwi_p (maxread) |
5aef8938 | 4323 | && tree_fits_uhwi_p (srclen) |
e6a18b5a | 4324 | && tree_int_cst_lt (maxread, srclen))) |
4325 | srclen = maxread; | |
5aef8938 | 4326 | |
e6a18b5a | 4327 | /* The number of bytes to write is SRCLEN. */ |
4328 | check_access (exp, dest, src, NULL_TREE, maxread, srclen, destsize); | |
5aef8938 | 4329 | |
4330 | return NULL_RTX; | |
4331 | } | |
4332 | ||
48e1416a | 4333 | /* Expand expression EXP, which is a call to the strncpy builtin. Return |
c2f47e15 | 4334 | NULL_RTX if we failed the caller should emit a normal call. */ |
ed09096d | 4335 | |
4336 | static rtx | |
a65c4d64 | 4337 | expand_builtin_strncpy (tree exp, rtx target) |
ed09096d | 4338 | { |
389dd41b | 4339 | location_t loc = EXPR_LOCATION (exp); |
c2f47e15 | 4340 | |
4341 | if (validate_arglist (exp, | |
4342 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 4343 | { |
c2f47e15 | 4344 | tree dest = CALL_EXPR_ARG (exp, 0); |
4345 | tree src = CALL_EXPR_ARG (exp, 1); | |
5aef8938 | 4346 | /* The number of bytes to write (not the maximum). */ |
c2f47e15 | 4347 | tree len = CALL_EXPR_ARG (exp, 2); |
5aef8938 | 4348 | /* The length of the source sequence. */ |
c2f47e15 | 4349 | tree slen = c_strlen (src, 1); |
6840589f | 4350 | |
e6a18b5a | 4351 | if (warn_stringop_overflow) |
4352 | { | |
4353 | tree destsize = compute_objsize (dest, | |
4354 | warn_stringop_overflow - 1); | |
4355 | ||
4356 | /* The number of bytes to write is LEN but check_access will also | |
4357 | check SLEN if LEN's value isn't known. */ | |
4358 | check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src, | |
4359 | destsize); | |
4360 | } | |
5aef8938 | 4361 | |
8ff6a5cd | 4362 | /* We must be passed a constant len and src parameter. */ |
e913b5cd | 4363 | if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen)) |
c2f47e15 | 4364 | return NULL_RTX; |
ed09096d | 4365 | |
389dd41b | 4366 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); |
ed09096d | 4367 | |
4368 | /* We're required to pad with trailing zeros if the requested | |
a0c938f0 | 4369 | len is greater than strlen(s2)+1. In that case try to |
6840589f | 4370 | use store_by_pieces, if it fails, punt. */ |
ed09096d | 4371 | if (tree_int_cst_lt (slen, len)) |
6840589f | 4372 | { |
957d0361 | 4373 | unsigned int dest_align = get_pointer_alignment (dest); |
c2f47e15 | 4374 | const char *p = c_getstr (src); |
6840589f | 4375 | rtx dest_mem; |
4376 | ||
e913b5cd | 4377 | if (!p || dest_align == 0 || !tree_fits_uhwi_p (len) |
4378 | || !can_store_by_pieces (tree_to_uhwi (len), | |
6840589f | 4379 | builtin_strncpy_read_str, |
364c0c59 | 4380 | CONST_CAST (char *, p), |
4381 | dest_align, false)) | |
c2f47e15 | 4382 | return NULL_RTX; |
6840589f | 4383 | |
d8ae1baa | 4384 | dest_mem = get_memory_rtx (dest, len); |
e913b5cd | 4385 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
6840589f | 4386 | builtin_strncpy_read_str, |
02aa6d73 | 4387 | CONST_CAST (char *, p), dest_align, false, |
4388 | RETURN_BEGIN); | |
a65c4d64 | 4389 | dest_mem = force_operand (XEXP (dest_mem, 0), target); |
85d654dd | 4390 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 4391 | return dest_mem; |
6840589f | 4392 | } |
ed09096d | 4393 | } |
c2f47e15 | 4394 | return NULL_RTX; |
ed09096d | 4395 | } |
4396 | ||
ecc318ff | 4397 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
4398 | bytes from constant string DATA + OFFSET and return it as target | |
4399 | constant. */ | |
4400 | ||
f656b751 | 4401 | rtx |
aecda0d6 | 4402 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
f77c4496 | 4403 | scalar_int_mode mode) |
ecc318ff | 4404 | { |
4405 | const char *c = (const char *) data; | |
364c0c59 | 4406 | char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode)); |
ecc318ff | 4407 | |
4408 | memset (p, *c, GET_MODE_SIZE (mode)); | |
4409 | ||
4410 | return c_readstr (p, mode); | |
4411 | } | |
4412 | ||
a7ec6974 | 4413 | /* Callback routine for store_by_pieces. Return the RTL of a register |
4414 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
4415 | char value given in the RTL register data. For example, if mode is | |
4416 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
4417 | ||
4418 | static rtx | |
aecda0d6 | 4419 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
f77c4496 | 4420 | scalar_int_mode mode) |
a7ec6974 | 4421 | { |
4422 | rtx target, coeff; | |
4423 | size_t size; | |
4424 | char *p; | |
4425 | ||
4426 | size = GET_MODE_SIZE (mode); | |
f0ce3b1f | 4427 | if (size == 1) |
4428 | return (rtx) data; | |
a7ec6974 | 4429 | |
364c0c59 | 4430 | p = XALLOCAVEC (char, size); |
a7ec6974 | 4431 | memset (p, 1, size); |
4432 | coeff = c_readstr (p, mode); | |
4433 | ||
f0ce3b1f | 4434 | target = convert_to_mode (mode, (rtx) data, 1); |
a7ec6974 | 4435 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
4436 | return force_reg (mode, target); | |
4437 | } | |
4438 | ||
48e1416a | 4439 | /* Expand expression EXP, which is a call to the memset builtin. Return |
4440 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
c2f47e15 | 4441 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
6f428e8b | 4442 | convenient). */ |
902de8ed | 4443 | |
53800dbe | 4444 | static rtx |
3754d046 | 4445 | expand_builtin_memset (tree exp, rtx target, machine_mode mode) |
53800dbe | 4446 | { |
c2f47e15 | 4447 | if (!validate_arglist (exp, |
4448 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
4449 | return NULL_RTX; | |
5aef8938 | 4450 | |
4451 | tree dest = CALL_EXPR_ARG (exp, 0); | |
4452 | tree val = CALL_EXPR_ARG (exp, 1); | |
4453 | tree len = CALL_EXPR_ARG (exp, 2); | |
4454 | ||
e6a18b5a | 4455 | check_memop_access (exp, dest, NULL_TREE, len); |
5aef8938 | 4456 | |
4457 | return expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
c2f47e15 | 4458 | } |
53800dbe | 4459 | |
c2f47e15 | 4460 | /* Helper function to do the actual work for expand_builtin_memset. The |
4461 | arguments to the builtin_memset call DEST, VAL, and LEN are broken out | |
4462 | so that this can also be called without constructing an actual CALL_EXPR. | |
4463 | The other arguments and return value are the same as for | |
4464 | expand_builtin_memset. */ | |
6b961939 | 4465 | |
c2f47e15 | 4466 | static rtx |
4467 | expand_builtin_memset_args (tree dest, tree val, tree len, | |
3754d046 | 4468 | rtx target, machine_mode mode, tree orig_exp) |
c2f47e15 | 4469 | { |
4470 | tree fndecl, fn; | |
4471 | enum built_in_function fcode; | |
3754d046 | 4472 | machine_mode val_mode; |
c2f47e15 | 4473 | char c; |
4474 | unsigned int dest_align; | |
4475 | rtx dest_mem, dest_addr, len_rtx; | |
4476 | HOST_WIDE_INT expected_size = -1; | |
4477 | unsigned int expected_align = 0; | |
36d63243 | 4478 | unsigned HOST_WIDE_INT min_size; |
4479 | unsigned HOST_WIDE_INT max_size; | |
9db0f34d | 4480 | unsigned HOST_WIDE_INT probable_max_size; |
53800dbe | 4481 | |
957d0361 | 4482 | dest_align = get_pointer_alignment (dest); |
162719b3 | 4483 | |
c2f47e15 | 4484 | /* If DEST is not a pointer type, don't do this operation in-line. */ |
4485 | if (dest_align == 0) | |
4486 | return NULL_RTX; | |
6f428e8b | 4487 | |
8cee8dc0 | 4488 | if (currently_expanding_gimple_stmt) |
4489 | stringop_block_profile (currently_expanding_gimple_stmt, | |
4490 | &expected_align, &expected_size); | |
75a70cf9 | 4491 | |
c2f47e15 | 4492 | if (expected_align < dest_align) |
4493 | expected_align = dest_align; | |
6b961939 | 4494 | |
c2f47e15 | 4495 | /* If the LEN parameter is zero, return DEST. */ |
4496 | if (integer_zerop (len)) | |
4497 | { | |
4498 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
4499 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4500 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
4501 | } | |
7a3e5564 | 4502 | |
c2f47e15 | 4503 | /* Stabilize the arguments in case we fail. */ |
4504 | dest = builtin_save_expr (dest); | |
4505 | val = builtin_save_expr (val); | |
4506 | len = builtin_save_expr (len); | |
a7ec6974 | 4507 | |
c2f47e15 | 4508 | len_rtx = expand_normal (len); |
9db0f34d | 4509 | determine_block_size (len, len_rtx, &min_size, &max_size, |
4510 | &probable_max_size); | |
c2f47e15 | 4511 | dest_mem = get_memory_rtx (dest, len); |
03a5dda9 | 4512 | val_mode = TYPE_MODE (unsigned_char_type_node); |
a7ec6974 | 4513 | |
c2f47e15 | 4514 | if (TREE_CODE (val) != INTEGER_CST) |
4515 | { | |
4516 | rtx val_rtx; | |
a7ec6974 | 4517 | |
c2f47e15 | 4518 | val_rtx = expand_normal (val); |
03a5dda9 | 4519 | val_rtx = convert_to_mode (val_mode, val_rtx, 0); |
53800dbe | 4520 | |
c2f47e15 | 4521 | /* Assume that we can memset by pieces if we can store |
4522 | * the coefficients by pieces (in the required modes). | |
4523 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
4524 | c = 1; | |
e913b5cd | 4525 | if (tree_fits_uhwi_p (len) |
4526 | && can_store_by_pieces (tree_to_uhwi (len), | |
4b297e2e | 4527 | builtin_memset_read_str, &c, dest_align, |
4528 | true)) | |
c2f47e15 | 4529 | { |
03a5dda9 | 4530 | val_rtx = force_reg (val_mode, val_rtx); |
e913b5cd | 4531 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
4b297e2e | 4532 | builtin_memset_gen_str, val_rtx, dest_align, |
02aa6d73 | 4533 | true, RETURN_BEGIN); |
c2f47e15 | 4534 | } |
4535 | else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx, | |
4536 | dest_align, expected_align, | |
9db0f34d | 4537 | expected_size, min_size, max_size, |
4538 | probable_max_size)) | |
6b961939 | 4539 | goto do_libcall; |
48e1416a | 4540 | |
c2f47e15 | 4541 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
4542 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
4543 | return dest_mem; | |
4544 | } | |
53800dbe | 4545 | |
c2f47e15 | 4546 | if (target_char_cast (val, &c)) |
4547 | goto do_libcall; | |
ecc318ff | 4548 | |
c2f47e15 | 4549 | if (c) |
4550 | { | |
e913b5cd | 4551 | if (tree_fits_uhwi_p (len) |
4552 | && can_store_by_pieces (tree_to_uhwi (len), | |
4b297e2e | 4553 | builtin_memset_read_str, &c, dest_align, |
4554 | true)) | |
e913b5cd | 4555 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
02aa6d73 | 4556 | builtin_memset_read_str, &c, dest_align, true, |
4557 | RETURN_BEGIN); | |
03a5dda9 | 4558 | else if (!set_storage_via_setmem (dest_mem, len_rtx, |
4559 | gen_int_mode (c, val_mode), | |
c2f47e15 | 4560 | dest_align, expected_align, |
9db0f34d | 4561 | expected_size, min_size, max_size, |
4562 | probable_max_size)) | |
c2f47e15 | 4563 | goto do_libcall; |
48e1416a | 4564 | |
c2f47e15 | 4565 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
4566 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
4567 | return dest_mem; | |
4568 | } | |
ecc318ff | 4569 | |
c2f47e15 | 4570 | set_mem_align (dest_mem, dest_align); |
4571 | dest_addr = clear_storage_hints (dest_mem, len_rtx, | |
4572 | CALL_EXPR_TAILCALL (orig_exp) | |
4573 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
36d63243 | 4574 | expected_align, expected_size, |
9db0f34d | 4575 | min_size, max_size, |
4576 | probable_max_size); | |
53800dbe | 4577 | |
c2f47e15 | 4578 | if (dest_addr == 0) |
4579 | { | |
4580 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
4581 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
4582 | } | |
53800dbe | 4583 | |
c2f47e15 | 4584 | return dest_addr; |
6b961939 | 4585 | |
c2f47e15 | 4586 | do_libcall: |
4587 | fndecl = get_callee_fndecl (orig_exp); | |
4588 | fcode = DECL_FUNCTION_CODE (fndecl); | |
1e42d5c6 | 4589 | if (fcode == BUILT_IN_MEMSET) |
0568e9c1 | 4590 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3, |
4591 | dest, val, len); | |
c2f47e15 | 4592 | else if (fcode == BUILT_IN_BZERO) |
0568e9c1 | 4593 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2, |
4594 | dest, len); | |
c2f47e15 | 4595 | else |
4596 | gcc_unreachable (); | |
a65c4d64 | 4597 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
4598 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp); | |
c2f47e15 | 4599 | return expand_call (fn, target, target == const0_rtx); |
53800dbe | 4600 | } |
4601 | ||
48e1416a | 4602 | /* Expand expression EXP, which is a call to the bzero builtin. Return |
c2f47e15 | 4603 | NULL_RTX if we failed the caller should emit a normal call. */ |
27d0c333 | 4604 | |
ffc83088 | 4605 | static rtx |
0b25db21 | 4606 | expand_builtin_bzero (tree exp) |
ffc83088 | 4607 | { |
c2f47e15 | 4608 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
7369e7ba | 4609 | return NULL_RTX; |
ffc83088 | 4610 | |
5aef8938 | 4611 | tree dest = CALL_EXPR_ARG (exp, 0); |
4612 | tree size = CALL_EXPR_ARG (exp, 1); | |
4613 | ||
e6a18b5a | 4614 | check_memop_access (exp, dest, NULL_TREE, size); |
bf8e3599 | 4615 | |
7369e7ba | 4616 | /* New argument list transforming bzero(ptr x, int y) to |
6f428e8b | 4617 | memset(ptr x, int 0, size_t y). This is done this way |
4618 | so that if it isn't expanded inline, we fallback to | |
4619 | calling bzero instead of memset. */ | |
bf8e3599 | 4620 | |
5aef8938 | 4621 | location_t loc = EXPR_LOCATION (exp); |
4622 | ||
c2f47e15 | 4623 | return expand_builtin_memset_args (dest, integer_zero_node, |
a0553bff | 4624 | fold_convert_loc (loc, |
4625 | size_type_node, size), | |
c2f47e15 | 4626 | const0_rtx, VOIDmode, exp); |
ffc83088 | 4627 | } |
4628 | ||
d6f01a40 | 4629 | /* Try to expand cmpstr operation ICODE with the given operands. |
4630 | Return the result rtx on success, otherwise return null. */ | |
4631 | ||
4632 | static rtx | |
4633 | expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx, | |
4634 | HOST_WIDE_INT align) | |
4635 | { | |
4636 | machine_mode insn_mode = insn_data[icode].operand[0].mode; | |
4637 | ||
4638 | if (target && (!REG_P (target) || HARD_REGISTER_P (target))) | |
4639 | target = NULL_RTX; | |
4640 | ||
4641 | struct expand_operand ops[4]; | |
4642 | create_output_operand (&ops[0], target, insn_mode); | |
4643 | create_fixed_operand (&ops[1], arg1_rtx); | |
4644 | create_fixed_operand (&ops[2], arg2_rtx); | |
4645 | create_integer_operand (&ops[3], align); | |
4646 | if (maybe_expand_insn (icode, 4, ops)) | |
4647 | return ops[0].value; | |
4648 | return NULL_RTX; | |
4649 | } | |
4650 | ||
7a3f89b5 | 4651 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
bd021c1c | 4652 | Return NULL_RTX if we failed and the caller should emit a normal call, |
3e346f54 | 4653 | otherwise try to get the result in TARGET, if convenient. |
4654 | RESULT_EQ is true if we can relax the returned value to be either zero | |
4655 | or nonzero, without caring about the sign. */ | |
27d0c333 | 4656 | |
53800dbe | 4657 | static rtx |
3e346f54 | 4658 | expand_builtin_memcmp (tree exp, rtx target, bool result_eq) |
53800dbe | 4659 | { |
c2f47e15 | 4660 | if (!validate_arglist (exp, |
4661 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
4662 | return NULL_RTX; | |
6f428e8b | 4663 | |
ea368aac | 4664 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
4665 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
4666 | tree len = CALL_EXPR_ARG (exp, 2); | |
a950155e | 4667 | enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp)); |
4668 | bool no_overflow = true; | |
8d6c6ef5 | 4669 | |
4670 | /* Diagnose calls where the specified length exceeds the size of either | |
4671 | object. */ | |
a950155e | 4672 | tree size = compute_objsize (arg1, 0); |
4673 | no_overflow = check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, | |
4674 | len, /*maxread=*/NULL_TREE, size, | |
4675 | /*objsize=*/NULL_TREE); | |
b3e6ae76 | 4676 | if (no_overflow) |
a950155e | 4677 | { |
4678 | size = compute_objsize (arg2, 0); | |
4679 | no_overflow = check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, | |
4680 | len, /*maxread=*/NULL_TREE, size, | |
4681 | /*objsize=*/NULL_TREE); | |
b3e6ae76 | 4682 | } |
a950155e | 4683 | |
ee1b788e | 4684 | /* If the specified length exceeds the size of either object, |
4685 | call the function. */ | |
4686 | if (!no_overflow) | |
4687 | return NULL_RTX; | |
4688 | ||
b3e6ae76 | 4689 | /* Due to the performance benefit, always inline the calls first |
a950155e | 4690 | when result_eq is false. */ |
4691 | rtx result = NULL_RTX; | |
b3e6ae76 | 4692 | |
ee1b788e | 4693 | if (!result_eq && fcode != BUILT_IN_BCMP) |
8d6c6ef5 | 4694 | { |
0dbefa15 | 4695 | result = inline_expand_builtin_string_cmp (exp, target); |
a950155e | 4696 | if (result) |
4697 | return result; | |
8d6c6ef5 | 4698 | } |
4699 | ||
3e346f54 | 4700 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
4701 | location_t loc = EXPR_LOCATION (exp); | |
b428c0a5 | 4702 | |
ea368aac | 4703 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
4704 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
53800dbe | 4705 | |
ea368aac | 4706 | /* If we don't have POINTER_TYPE, call the function. */ |
4707 | if (arg1_align == 0 || arg2_align == 0) | |
4708 | return NULL_RTX; | |
53800dbe | 4709 | |
ea368aac | 4710 | rtx arg1_rtx = get_memory_rtx (arg1, len); |
4711 | rtx arg2_rtx = get_memory_rtx (arg2, len); | |
3e346f54 | 4712 | rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len)); |
53800dbe | 4713 | |
ea368aac | 4714 | /* Set MEM_SIZE as appropriate. */ |
3e346f54 | 4715 | if (CONST_INT_P (len_rtx)) |
ea368aac | 4716 | { |
3e346f54 | 4717 | set_mem_size (arg1_rtx, INTVAL (len_rtx)); |
4718 | set_mem_size (arg2_rtx, INTVAL (len_rtx)); | |
ea368aac | 4719 | } |
83f88f8e | 4720 | |
3e346f54 | 4721 | by_pieces_constfn constfn = NULL; |
4722 | ||
719f3058 | 4723 | const char *src_str = c_getstr (arg2); |
4724 | if (result_eq && src_str == NULL) | |
4725 | { | |
4726 | src_str = c_getstr (arg1); | |
4727 | if (src_str != NULL) | |
092db747 | 4728 | std::swap (arg1_rtx, arg2_rtx); |
719f3058 | 4729 | } |
3e346f54 | 4730 | |
4731 | /* If SRC is a string constant and block move would be done | |
4732 | by pieces, we can avoid loading the string from memory | |
4733 | and only stored the computed constants. */ | |
4734 | if (src_str | |
4735 | && CONST_INT_P (len_rtx) | |
4736 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1) | |
4737 | constfn = builtin_memcpy_read_str; | |
4738 | ||
a950155e | 4739 | result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx, |
4740 | TREE_TYPE (len), target, | |
4741 | result_eq, constfn, | |
4742 | CONST_CAST (char *, src_str)); | |
3e346f54 | 4743 | |
ea368aac | 4744 | if (result) |
4745 | { | |
4746 | /* Return the value in the proper mode for this function. */ | |
4747 | if (GET_MODE (result) == mode) | |
4748 | return result; | |
83f88f8e | 4749 | |
ea368aac | 4750 | if (target != 0) |
4751 | { | |
4752 | convert_move (target, result, 0); | |
4753 | return target; | |
4754 | } | |
0cd832f0 | 4755 | |
53800dbe | 4756 | return convert_to_mode (mode, result, 0); |
ea368aac | 4757 | } |
53800dbe | 4758 | |
61ffc71a | 4759 | return NULL_RTX; |
6f428e8b | 4760 | } |
4761 | ||
c2f47e15 | 4762 | /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX |
53800dbe | 4763 | if we failed the caller should emit a normal call, otherwise try to get |
4764 | the result in TARGET, if convenient. */ | |
902de8ed | 4765 | |
53800dbe | 4766 | static rtx |
a65c4d64 | 4767 | expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target) |
53800dbe | 4768 | { |
c2f47e15 | 4769 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4770 | return NULL_RTX; | |
bf8e3599 | 4771 | |
a950155e | 4772 | /* Due to the performance benefit, always inline the calls first. */ |
4773 | rtx result = NULL_RTX; | |
0dbefa15 | 4774 | result = inline_expand_builtin_string_cmp (exp, target); |
a950155e | 4775 | if (result) |
4776 | return result; | |
4777 | ||
d6f01a40 | 4778 | insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode); |
4779 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); | |
5c5d012b | 4780 | if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing) |
4781 | return NULL_RTX; | |
a0c938f0 | 4782 | |
5c5d012b | 4783 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
4784 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
6ac5504b | 4785 | |
5c5d012b | 4786 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
4787 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
7a3f89b5 | 4788 | |
5c5d012b | 4789 | /* If we don't have POINTER_TYPE, call the function. */ |
4790 | if (arg1_align == 0 || arg2_align == 0) | |
4791 | return NULL_RTX; | |
7a3f89b5 | 4792 | |
5c5d012b | 4793 | /* Stabilize the arguments in case gen_cmpstr(n)si fail. */ |
4794 | arg1 = builtin_save_expr (arg1); | |
4795 | arg2 = builtin_save_expr (arg2); | |
53800dbe | 4796 | |
5c5d012b | 4797 | rtx arg1_rtx = get_memory_rtx (arg1, NULL); |
4798 | rtx arg2_rtx = get_memory_rtx (arg2, NULL); | |
d6f01a40 | 4799 | |
5c5d012b | 4800 | /* Try to call cmpstrsi. */ |
4801 | if (cmpstr_icode != CODE_FOR_nothing) | |
4802 | result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx, | |
4803 | MIN (arg1_align, arg2_align)); | |
6ac5504b | 4804 | |
5c5d012b | 4805 | /* Try to determine at least one length and call cmpstrnsi. */ |
4806 | if (!result && cmpstrn_icode != CODE_FOR_nothing) | |
4807 | { | |
4808 | tree len; | |
4809 | rtx arg3_rtx; | |
4810 | ||
4811 | tree len1 = c_strlen (arg1, 1); | |
4812 | tree len2 = c_strlen (arg2, 1); | |
4813 | ||
4814 | if (len1) | |
4815 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
4816 | if (len2) | |
4817 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
4818 | ||
4819 | /* If we don't have a constant length for the first, use the length | |
4820 | of the second, if we know it. We don't require a constant for | |
4821 | this case; some cost analysis could be done if both are available | |
4822 | but neither is constant. For now, assume they're equally cheap, | |
4823 | unless one has side effects. If both strings have constant lengths, | |
4824 | use the smaller. */ | |
4825 | ||
4826 | if (!len1) | |
4827 | len = len2; | |
4828 | else if (!len2) | |
4829 | len = len1; | |
4830 | else if (TREE_SIDE_EFFECTS (len1)) | |
4831 | len = len2; | |
4832 | else if (TREE_SIDE_EFFECTS (len2)) | |
4833 | len = len1; | |
4834 | else if (TREE_CODE (len1) != INTEGER_CST) | |
4835 | len = len2; | |
4836 | else if (TREE_CODE (len2) != INTEGER_CST) | |
4837 | len = len1; | |
4838 | else if (tree_int_cst_lt (len1, len2)) | |
4839 | len = len1; | |
4840 | else | |
4841 | len = len2; | |
3f8aefe2 | 4842 | |
5c5d012b | 4843 | /* If both arguments have side effects, we cannot optimize. */ |
4844 | if (len && !TREE_SIDE_EFFECTS (len)) | |
6ac5504b | 4845 | { |
5c5d012b | 4846 | arg3_rtx = expand_normal (len); |
4847 | result = expand_cmpstrn_or_cmpmem | |
4848 | (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len), | |
4849 | arg3_rtx, MIN (arg1_align, arg2_align)); | |
6ac5504b | 4850 | } |
5c5d012b | 4851 | } |
4852 | ||
5c5d012b | 4853 | tree fndecl = get_callee_fndecl (exp); |
5c5d012b | 4854 | if (result) |
4855 | { | |
0b39ade8 | 4856 | /* Check to see if the argument was declared attribute nonstring |
4857 | and if so, issue a warning since at this point it's not known | |
4858 | to be nul-terminated. */ | |
4859 | maybe_warn_nonstring_arg (fndecl, exp); | |
4860 | ||
5c5d012b | 4861 | /* Return the value in the proper mode for this function. */ |
4862 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); | |
4863 | if (GET_MODE (result) == mode) | |
4864 | return result; | |
4865 | if (target == 0) | |
4866 | return convert_to_mode (mode, result, 0); | |
4867 | convert_move (target, result, 0); | |
4868 | return target; | |
6ac5504b | 4869 | } |
5c5d012b | 4870 | |
4871 | /* Expand the library call ourselves using a stabilized argument | |
4872 | list to avoid re-evaluating the function's arguments twice. */ | |
4873 | tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2); | |
4874 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); | |
4875 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
4876 | return expand_call (fn, target, target == const0_rtx); | |
83d79705 | 4877 | } |
53800dbe | 4878 | |
48e1416a | 4879 | /* Expand expression EXP, which is a call to the strncmp builtin. Return |
c2f47e15 | 4880 | NULL_RTX if we failed the caller should emit a normal call, otherwise try to get |
ed09096d | 4881 | the result in TARGET, if convenient. */ |
27d0c333 | 4882 | |
ed09096d | 4883 | static rtx |
a65c4d64 | 4884 | expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target, |
3754d046 | 4885 | ATTRIBUTE_UNUSED machine_mode mode) |
ed09096d | 4886 | { |
c2f47e15 | 4887 | if (!validate_arglist (exp, |
4888 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
4889 | return NULL_RTX; | |
ed09096d | 4890 | |
a950155e | 4891 | /* Due to the performance benefit, always inline the calls first. */ |
4892 | rtx result = NULL_RTX; | |
0dbefa15 | 4893 | result = inline_expand_builtin_string_cmp (exp, target); |
a950155e | 4894 | if (result) |
4895 | return result; | |
4896 | ||
6e34e617 | 4897 | /* If c_strlen can determine an expression for one of the string |
6ac5504b | 4898 | lengths, and it doesn't have side effects, then emit cmpstrnsi |
7a3f89b5 | 4899 | using length MIN(strlen(string)+1, arg3). */ |
d6f01a40 | 4900 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); |
5c5d012b | 4901 | if (cmpstrn_icode == CODE_FOR_nothing) |
4902 | return NULL_RTX; | |
27d0c333 | 4903 | |
5c5d012b | 4904 | tree len; |
4905 | ||
4906 | tree arg1 = CALL_EXPR_ARG (exp, 0); | |
4907 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
4908 | tree arg3 = CALL_EXPR_ARG (exp, 2); | |
4909 | ||
4910 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; | |
4911 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
4912 | ||
4913 | tree len1 = c_strlen (arg1, 1); | |
4914 | tree len2 = c_strlen (arg2, 1); | |
4915 | ||
4916 | location_t loc = EXPR_LOCATION (exp); | |
4917 | ||
4918 | if (len1) | |
4919 | len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1); | |
4920 | if (len2) | |
4921 | len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2); | |
4922 | ||
4923 | tree len3 = fold_convert_loc (loc, sizetype, arg3); | |
4924 | ||
4925 | /* If we don't have a constant length for the first, use the length | |
4926 | of the second, if we know it. If neither string is constant length, | |
4927 | use the given length argument. We don't require a constant for | |
4928 | this case; some cost analysis could be done if both are available | |
4929 | but neither is constant. For now, assume they're equally cheap, | |
4930 | unless one has side effects. If both strings have constant lengths, | |
4931 | use the smaller. */ | |
4932 | ||
4933 | if (!len1 && !len2) | |
4934 | len = len3; | |
4935 | else if (!len1) | |
4936 | len = len2; | |
4937 | else if (!len2) | |
4938 | len = len1; | |
4939 | else if (TREE_SIDE_EFFECTS (len1)) | |
4940 | len = len2; | |
4941 | else if (TREE_SIDE_EFFECTS (len2)) | |
4942 | len = len1; | |
4943 | else if (TREE_CODE (len1) != INTEGER_CST) | |
4944 | len = len2; | |
4945 | else if (TREE_CODE (len2) != INTEGER_CST) | |
4946 | len = len1; | |
4947 | else if (tree_int_cst_lt (len1, len2)) | |
4948 | len = len1; | |
4949 | else | |
4950 | len = len2; | |
4951 | ||
4952 | /* If we are not using the given length, we must incorporate it here. | |
4953 | The actual new length parameter will be MIN(len,arg3) in this case. */ | |
4954 | if (len != len3) | |
a55f0871 | 4955 | { |
4956 | len = fold_convert_loc (loc, sizetype, len); | |
4957 | len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3); | |
4958 | } | |
5c5d012b | 4959 | rtx arg1_rtx = get_memory_rtx (arg1, len); |
4960 | rtx arg2_rtx = get_memory_rtx (arg2, len); | |
4961 | rtx arg3_rtx = expand_normal (len); | |
a950155e | 4962 | result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx, |
4963 | arg2_rtx, TREE_TYPE (len), arg3_rtx, | |
4964 | MIN (arg1_align, arg2_align)); | |
5c5d012b | 4965 | |
5c5d012b | 4966 | tree fndecl = get_callee_fndecl (exp); |
5c5d012b | 4967 | if (result) |
4968 | { | |
0b39ade8 | 4969 | /* Check to see if the argument was declared attribute nonstring |
4970 | and if so, issue a warning since at this point it's not known | |
4971 | to be nul-terminated. */ | |
4972 | maybe_warn_nonstring_arg (fndecl, exp); | |
4973 | ||
5c5d012b | 4974 | /* Return the value in the proper mode for this function. */ |
4975 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
4976 | if (GET_MODE (result) == mode) | |
4977 | return result; | |
4978 | if (target == 0) | |
4979 | return convert_to_mode (mode, result, 0); | |
4980 | convert_move (target, result, 0); | |
4981 | return target; | |
4982 | } | |
4983 | ||
4984 | /* Expand the library call ourselves using a stabilized argument | |
4985 | list to avoid re-evaluating the function's arguments twice. */ | |
4986 | tree fn = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len); | |
4987 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); | |
4988 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
4989 | return expand_call (fn, target, target == const0_rtx); | |
49f0327b | 4990 | } |
4991 | ||
a66c9326 | 4992 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
4993 | if that's convenient. */ | |
902de8ed | 4994 | |
a66c9326 | 4995 | rtx |
aecda0d6 | 4996 | expand_builtin_saveregs (void) |
53800dbe | 4997 | { |
1e0c0b35 | 4998 | rtx val; |
4999 | rtx_insn *seq; | |
53800dbe | 5000 | |
5001 | /* Don't do __builtin_saveregs more than once in a function. | |
5002 | Save the result of the first call and reuse it. */ | |
5003 | if (saveregs_value != 0) | |
5004 | return saveregs_value; | |
53800dbe | 5005 | |
a66c9326 | 5006 | /* When this function is called, it means that registers must be |
5007 | saved on entry to this function. So we migrate the call to the | |
5008 | first insn of this function. */ | |
5009 | ||
5010 | start_sequence (); | |
53800dbe | 5011 | |
a66c9326 | 5012 | /* Do whatever the machine needs done in this case. */ |
45550790 | 5013 | val = targetm.calls.expand_builtin_saveregs (); |
53800dbe | 5014 | |
a66c9326 | 5015 | seq = get_insns (); |
5016 | end_sequence (); | |
53800dbe | 5017 | |
a66c9326 | 5018 | saveregs_value = val; |
53800dbe | 5019 | |
31d3e01c | 5020 | /* Put the insns after the NOTE that starts the function. If this |
5021 | is inside a start_sequence, make the outer-level insn chain current, so | |
a66c9326 | 5022 | the code is placed at the start of the function. */ |
5023 | push_topmost_sequence (); | |
0ec80471 | 5024 | emit_insn_after (seq, entry_of_function ()); |
a66c9326 | 5025 | pop_topmost_sequence (); |
5026 | ||
5027 | return val; | |
53800dbe | 5028 | } |
5029 | ||
79012a9d | 5030 | /* Expand a call to __builtin_next_arg. */ |
27d0c333 | 5031 | |
53800dbe | 5032 | static rtx |
79012a9d | 5033 | expand_builtin_next_arg (void) |
53800dbe | 5034 | { |
79012a9d | 5035 | /* Checking arguments is already done in fold_builtin_next_arg |
5036 | that must be called before this function. */ | |
940ddc5c | 5037 | return expand_binop (ptr_mode, add_optab, |
abe32cce | 5038 | crtl->args.internal_arg_pointer, |
5039 | crtl->args.arg_offset_rtx, | |
53800dbe | 5040 | NULL_RTX, 0, OPTAB_LIB_WIDEN); |
5041 | } | |
5042 | ||
a66c9326 | 5043 | /* Make it easier for the backends by protecting the valist argument |
5044 | from multiple evaluations. */ | |
5045 | ||
5046 | static tree | |
389dd41b | 5047 | stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue) |
a66c9326 | 5048 | { |
5f57a8b1 | 5049 | tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist)); |
5050 | ||
182cf5a9 | 5051 | /* The current way of determining the type of valist is completely |
5052 | bogus. We should have the information on the va builtin instead. */ | |
5053 | if (!vatype) | |
5054 | vatype = targetm.fn_abi_va_list (cfun->decl); | |
5f57a8b1 | 5055 | |
5056 | if (TREE_CODE (vatype) == ARRAY_TYPE) | |
a66c9326 | 5057 | { |
2d47cc32 | 5058 | if (TREE_SIDE_EFFECTS (valist)) |
5059 | valist = save_expr (valist); | |
11a61dea | 5060 | |
2d47cc32 | 5061 | /* For this case, the backends will be expecting a pointer to |
5f57a8b1 | 5062 | vatype, but it's possible we've actually been given an array |
5063 | (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)). | |
2d47cc32 | 5064 | So fix it. */ |
5065 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
8a15c04a | 5066 | { |
5f57a8b1 | 5067 | tree p1 = build_pointer_type (TREE_TYPE (vatype)); |
389dd41b | 5068 | valist = build_fold_addr_expr_with_type_loc (loc, valist, p1); |
8a15c04a | 5069 | } |
a66c9326 | 5070 | } |
11a61dea | 5071 | else |
a66c9326 | 5072 | { |
182cf5a9 | 5073 | tree pt = build_pointer_type (vatype); |
11a61dea | 5074 | |
2d47cc32 | 5075 | if (! needs_lvalue) |
5076 | { | |
11a61dea | 5077 | if (! TREE_SIDE_EFFECTS (valist)) |
5078 | return valist; | |
bf8e3599 | 5079 | |
389dd41b | 5080 | valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist); |
a66c9326 | 5081 | TREE_SIDE_EFFECTS (valist) = 1; |
a66c9326 | 5082 | } |
2d47cc32 | 5083 | |
11a61dea | 5084 | if (TREE_SIDE_EFFECTS (valist)) |
2d47cc32 | 5085 | valist = save_expr (valist); |
182cf5a9 | 5086 | valist = fold_build2_loc (loc, MEM_REF, |
5087 | vatype, valist, build_int_cst (pt, 0)); | |
a66c9326 | 5088 | } |
5089 | ||
5090 | return valist; | |
5091 | } | |
5092 | ||
2e15d750 | 5093 | /* The "standard" definition of va_list is void*. */ |
5094 | ||
5095 | tree | |
5096 | std_build_builtin_va_list (void) | |
5097 | { | |
5098 | return ptr_type_node; | |
5099 | } | |
5100 | ||
5f57a8b1 | 5101 | /* The "standard" abi va_list is va_list_type_node. */ |
5102 | ||
5103 | tree | |
5104 | std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED) | |
5105 | { | |
5106 | return va_list_type_node; | |
5107 | } | |
5108 | ||
5109 | /* The "standard" type of va_list is va_list_type_node. */ | |
5110 | ||
5111 | tree | |
5112 | std_canonical_va_list_type (tree type) | |
5113 | { | |
5114 | tree wtype, htype; | |
5115 | ||
5f57a8b1 | 5116 | wtype = va_list_type_node; |
5117 | htype = type; | |
b6da2e41 | 5118 | |
5119 | if (TREE_CODE (wtype) == ARRAY_TYPE) | |
5f57a8b1 | 5120 | { |
5121 | /* If va_list is an array type, the argument may have decayed | |
5122 | to a pointer type, e.g. by being passed to another function. | |
5123 | In that case, unwrap both types so that we can compare the | |
5124 | underlying records. */ | |
5125 | if (TREE_CODE (htype) == ARRAY_TYPE | |
5126 | || POINTER_TYPE_P (htype)) | |
5127 | { | |
5128 | wtype = TREE_TYPE (wtype); | |
5129 | htype = TREE_TYPE (htype); | |
5130 | } | |
5131 | } | |
5132 | if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype)) | |
5133 | return va_list_type_node; | |
5134 | ||
5135 | return NULL_TREE; | |
5136 | } | |
5137 | ||
a66c9326 | 5138 | /* The "standard" implementation of va_start: just assign `nextarg' to |
5139 | the variable. */ | |
27d0c333 | 5140 | |
a66c9326 | 5141 | void |
aecda0d6 | 5142 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
a66c9326 | 5143 | { |
f03c17bc | 5144 | rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE); |
5145 | convert_move (va_r, nextarg, 0); | |
a66c9326 | 5146 | } |
5147 | ||
c2f47e15 | 5148 | /* Expand EXP, a call to __builtin_va_start. */ |
27d0c333 | 5149 | |
a66c9326 | 5150 | static rtx |
c2f47e15 | 5151 | expand_builtin_va_start (tree exp) |
a66c9326 | 5152 | { |
5153 | rtx nextarg; | |
c2f47e15 | 5154 | tree valist; |
389dd41b | 5155 | location_t loc = EXPR_LOCATION (exp); |
a66c9326 | 5156 | |
c2f47e15 | 5157 | if (call_expr_nargs (exp) < 2) |
cb166087 | 5158 | { |
389dd41b | 5159 | error_at (loc, "too few arguments to function %<va_start%>"); |
cb166087 | 5160 | return const0_rtx; |
5161 | } | |
a66c9326 | 5162 | |
c2f47e15 | 5163 | if (fold_builtin_next_arg (exp, true)) |
79012a9d | 5164 | return const0_rtx; |
7c2f0500 | 5165 | |
79012a9d | 5166 | nextarg = expand_builtin_next_arg (); |
389dd41b | 5167 | valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1); |
a66c9326 | 5168 | |
8a58ed0a | 5169 | if (targetm.expand_builtin_va_start) |
5170 | targetm.expand_builtin_va_start (valist, nextarg); | |
5171 | else | |
5172 | std_expand_builtin_va_start (valist, nextarg); | |
a66c9326 | 5173 | |
5174 | return const0_rtx; | |
5175 | } | |
5176 | ||
c2f47e15 | 5177 | /* Expand EXP, a call to __builtin_va_end. */ |
f7c44134 | 5178 | |
a66c9326 | 5179 | static rtx |
c2f47e15 | 5180 | expand_builtin_va_end (tree exp) |
a66c9326 | 5181 | { |
c2f47e15 | 5182 | tree valist = CALL_EXPR_ARG (exp, 0); |
8a15c04a | 5183 | |
8a15c04a | 5184 | /* Evaluate for side effects, if needed. I hate macros that don't |
5185 | do that. */ | |
5186 | if (TREE_SIDE_EFFECTS (valist)) | |
5187 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
a66c9326 | 5188 | |
5189 | return const0_rtx; | |
5190 | } | |
5191 | ||
c2f47e15 | 5192 | /* Expand EXP, a call to __builtin_va_copy. We do this as a |
a66c9326 | 5193 | builtin rather than just as an assignment in stdarg.h because of the |
5194 | nastiness of array-type va_list types. */ | |
f7c44134 | 5195 | |
a66c9326 | 5196 | static rtx |
c2f47e15 | 5197 | expand_builtin_va_copy (tree exp) |
a66c9326 | 5198 | { |
5199 | tree dst, src, t; | |
389dd41b | 5200 | location_t loc = EXPR_LOCATION (exp); |
a66c9326 | 5201 | |
c2f47e15 | 5202 | dst = CALL_EXPR_ARG (exp, 0); |
5203 | src = CALL_EXPR_ARG (exp, 1); | |
a66c9326 | 5204 | |
389dd41b | 5205 | dst = stabilize_va_list_loc (loc, dst, 1); |
5206 | src = stabilize_va_list_loc (loc, src, 0); | |
a66c9326 | 5207 | |
5f57a8b1 | 5208 | gcc_assert (cfun != NULL && cfun->decl != NULL_TREE); |
5209 | ||
5210 | if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE) | |
a66c9326 | 5211 | { |
5f57a8b1 | 5212 | t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src); |
a66c9326 | 5213 | TREE_SIDE_EFFECTS (t) = 1; |
5214 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
5215 | } | |
5216 | else | |
5217 | { | |
11a61dea | 5218 | rtx dstb, srcb, size; |
5219 | ||
5220 | /* Evaluate to pointers. */ | |
5221 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
5222 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
5f57a8b1 | 5223 | size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)), |
5224 | NULL_RTX, VOIDmode, EXPAND_NORMAL); | |
11a61dea | 5225 | |
85d654dd | 5226 | dstb = convert_memory_address (Pmode, dstb); |
5227 | srcb = convert_memory_address (Pmode, srcb); | |
726ec87c | 5228 | |
11a61dea | 5229 | /* "Dereference" to BLKmode memories. */ |
5230 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ab6ab77e | 5231 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
5f57a8b1 | 5232 | set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
11a61dea | 5233 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ab6ab77e | 5234 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
5f57a8b1 | 5235 | set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
11a61dea | 5236 | |
5237 | /* Copy. */ | |
0378dbdc | 5238 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
a66c9326 | 5239 | } |
5240 | ||
5241 | return const0_rtx; | |
5242 | } | |
5243 | ||
53800dbe | 5244 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
5245 | __builtin_return_address. */ | |
27d0c333 | 5246 | |
53800dbe | 5247 | static rtx |
c2f47e15 | 5248 | expand_builtin_frame_address (tree fndecl, tree exp) |
53800dbe | 5249 | { |
53800dbe | 5250 | /* The argument must be a nonnegative integer constant. |
5251 | It counts the number of frames to scan up the stack. | |
5b252e95 | 5252 | The value is either the frame pointer value or the return |
5253 | address saved in that frame. */ | |
c2f47e15 | 5254 | if (call_expr_nargs (exp) == 0) |
53800dbe | 5255 | /* Warning about missing arg was already issued. */ |
5256 | return const0_rtx; | |
e913b5cd | 5257 | else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0))) |
53800dbe | 5258 | { |
5b252e95 | 5259 | error ("invalid argument to %qD", fndecl); |
53800dbe | 5260 | return const0_rtx; |
5261 | } | |
5262 | else | |
5263 | { | |
5b252e95 | 5264 | /* Number of frames to scan up the stack. */ |
5265 | unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0)); | |
5266 | ||
5267 | rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count); | |
53800dbe | 5268 | |
5269 | /* Some ports cannot access arbitrary stack frames. */ | |
5270 | if (tem == NULL) | |
5271 | { | |
5b252e95 | 5272 | warning (0, "unsupported argument to %qD", fndecl); |
53800dbe | 5273 | return const0_rtx; |
5274 | } | |
5275 | ||
5b252e95 | 5276 | if (count) |
5277 | { | |
5278 | /* Warn since no effort is made to ensure that any frame | |
5279 | beyond the current one exists or can be safely reached. */ | |
5280 | warning (OPT_Wframe_address, "calling %qD with " | |
5281 | "a nonzero argument is unsafe", fndecl); | |
5282 | } | |
5283 | ||
53800dbe | 5284 | /* For __builtin_frame_address, return what we've got. */ |
5285 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
5286 | return tem; | |
5287 | ||
8ad4c111 | 5288 | if (!REG_P (tem) |
53800dbe | 5289 | && ! CONSTANT_P (tem)) |
99182918 | 5290 | tem = copy_addr_to_reg (tem); |
53800dbe | 5291 | return tem; |
5292 | } | |
5293 | } | |
5294 | ||
990495a7 | 5295 | /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we |
2b29cc6a | 5296 | failed and the caller should emit a normal call. */ |
15c6cf6b | 5297 | |
53800dbe | 5298 | static rtx |
2b29cc6a | 5299 | expand_builtin_alloca (tree exp) |
53800dbe | 5300 | { |
5301 | rtx op0; | |
15c6cf6b | 5302 | rtx result; |
581bf1c2 | 5303 | unsigned int align; |
370e45b9 | 5304 | tree fndecl = get_callee_fndecl (exp); |
2b34677f | 5305 | HOST_WIDE_INT max_size; |
5306 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
2b29cc6a | 5307 | bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp); |
370e45b9 | 5308 | bool valid_arglist |
2b34677f | 5309 | = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX |
5310 | ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE, | |
5311 | VOID_TYPE) | |
5312 | : fcode == BUILT_IN_ALLOCA_WITH_ALIGN | |
5313 | ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
5314 | : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)); | |
581bf1c2 | 5315 | |
5316 | if (!valid_arglist) | |
c2f47e15 | 5317 | return NULL_RTX; |
53800dbe | 5318 | |
8e18705e | 5319 | if ((alloca_for_var |
5320 | && warn_vla_limit >= HOST_WIDE_INT_MAX | |
5321 | && warn_alloc_size_limit < warn_vla_limit) | |
5322 | || (!alloca_for_var | |
5323 | && warn_alloca_limit >= HOST_WIDE_INT_MAX | |
5324 | && warn_alloc_size_limit < warn_alloca_limit | |
5325 | )) | |
370e45b9 | 5326 | { |
8e18705e | 5327 | /* -Walloca-larger-than and -Wvla-larger-than settings of |
5328 | less than HOST_WIDE_INT_MAX override the more general | |
5329 | -Walloc-size-larger-than so unless either of the former | |
5330 | options is smaller than the last one (wchich would imply | |
5331 | that the call was already checked), check the alloca | |
5332 | arguments for overflow. */ | |
370e45b9 | 5333 | tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE }; |
5334 | int idx[] = { 0, -1 }; | |
5335 | maybe_warn_alloc_args_overflow (fndecl, exp, args, idx); | |
5336 | } | |
5337 | ||
53800dbe | 5338 | /* Compute the argument. */ |
c2f47e15 | 5339 | op0 = expand_normal (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 5340 | |
581bf1c2 | 5341 | /* Compute the alignment. */ |
2b34677f | 5342 | align = (fcode == BUILT_IN_ALLOCA |
5343 | ? BIGGEST_ALIGNMENT | |
5344 | : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))); | |
5345 | ||
5346 | /* Compute the maximum size. */ | |
5347 | max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX | |
5348 | ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2)) | |
5349 | : -1); | |
581bf1c2 | 5350 | |
2b29cc6a | 5351 | /* Allocate the desired space. If the allocation stems from the declaration |
5352 | of a variable-sized object, it cannot accumulate. */ | |
2b34677f | 5353 | result |
5354 | = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var); | |
85d654dd | 5355 | result = convert_memory_address (ptr_mode, result); |
15c6cf6b | 5356 | |
5357 | return result; | |
53800dbe | 5358 | } |
5359 | ||
829e6a9b | 5360 | /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument |
5361 | of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the | |
5362 | STACK_DYNAMIC_OFFSET value. See motivation for this in comment to | |
5363 | handle_builtin_stack_restore function. */ | |
d08919a7 | 5364 | |
5365 | static rtx | |
5366 | expand_asan_emit_allocas_unpoison (tree exp) | |
5367 | { | |
5368 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
829e6a9b | 5369 | tree arg1 = CALL_EXPR_ARG (exp, 1); |
cd2ee6ee | 5370 | rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL); |
829e6a9b | 5371 | rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL); |
5372 | rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx, | |
5373 | stack_pointer_rtx, NULL_RTX, 0, | |
5374 | OPTAB_LIB_WIDEN); | |
5375 | off = convert_modes (ptr_mode, Pmode, off, 0); | |
5376 | bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0, | |
5377 | OPTAB_LIB_WIDEN); | |
d08919a7 | 5378 | rtx ret = init_one_libfunc ("__asan_allocas_unpoison"); |
9e9e5c15 | 5379 | ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode, |
5380 | top, ptr_mode, bot, ptr_mode); | |
d08919a7 | 5381 | return ret; |
5382 | } | |
5383 | ||
74bdbe96 | 5384 | /* Expand a call to bswap builtin in EXP. |
5385 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
5386 | function in-line. If convenient, the result should be placed in TARGET. | |
5387 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
42791117 | 5388 | |
5389 | static rtx | |
3754d046 | 5390 | expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target, |
74bdbe96 | 5391 | rtx subtarget) |
42791117 | 5392 | { |
42791117 | 5393 | tree arg; |
5394 | rtx op0; | |
5395 | ||
c2f47e15 | 5396 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) |
5397 | return NULL_RTX; | |
42791117 | 5398 | |
c2f47e15 | 5399 | arg = CALL_EXPR_ARG (exp, 0); |
74bdbe96 | 5400 | op0 = expand_expr (arg, |
5401 | subtarget && GET_MODE (subtarget) == target_mode | |
5402 | ? subtarget : NULL_RTX, | |
5403 | target_mode, EXPAND_NORMAL); | |
5404 | if (GET_MODE (op0) != target_mode) | |
5405 | op0 = convert_to_mode (target_mode, op0, 1); | |
42791117 | 5406 | |
74bdbe96 | 5407 | target = expand_unop (target_mode, bswap_optab, op0, target, 1); |
42791117 | 5408 | |
5409 | gcc_assert (target); | |
5410 | ||
74bdbe96 | 5411 | return convert_to_mode (target_mode, target, 1); |
42791117 | 5412 | } |
5413 | ||
c2f47e15 | 5414 | /* Expand a call to a unary builtin in EXP. |
5415 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
53800dbe | 5416 | function in-line. If convenient, the result should be placed in TARGET. |
5417 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
15c6cf6b | 5418 | |
53800dbe | 5419 | static rtx |
3754d046 | 5420 | expand_builtin_unop (machine_mode target_mode, tree exp, rtx target, |
aecda0d6 | 5421 | rtx subtarget, optab op_optab) |
53800dbe | 5422 | { |
5423 | rtx op0; | |
c2f47e15 | 5424 | |
5425 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) | |
5426 | return NULL_RTX; | |
53800dbe | 5427 | |
5428 | /* Compute the argument. */ | |
f97eea22 | 5429 | op0 = expand_expr (CALL_EXPR_ARG (exp, 0), |
5430 | (subtarget | |
5431 | && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))) | |
5432 | == GET_MODE (subtarget))) ? subtarget : NULL_RTX, | |
1db6d067 | 5433 | VOIDmode, EXPAND_NORMAL); |
6a08d0ab | 5434 | /* Compute op, into TARGET if possible. |
53800dbe | 5435 | Set TARGET to wherever the result comes back. */ |
c2f47e15 | 5436 | target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))), |
6aaa1f9e | 5437 | op_optab, op0, target, op_optab != clrsb_optab); |
64db345d | 5438 | gcc_assert (target); |
7d3f6cc7 | 5439 | |
efb070c8 | 5440 | return convert_to_mode (target_mode, target, 0); |
53800dbe | 5441 | } |
89cfe6e5 | 5442 | |
48e1416a | 5443 | /* Expand a call to __builtin_expect. We just return our argument |
5a74f77e | 5444 | as the builtin_expect semantic should've been already executed by |
5445 | tree branch prediction pass. */ | |
89cfe6e5 | 5446 | |
5447 | static rtx | |
c2f47e15 | 5448 | expand_builtin_expect (tree exp, rtx target) |
89cfe6e5 | 5449 | { |
1e4adcfc | 5450 | tree arg; |
89cfe6e5 | 5451 | |
c2f47e15 | 5452 | if (call_expr_nargs (exp) < 2) |
89cfe6e5 | 5453 | return const0_rtx; |
c2f47e15 | 5454 | arg = CALL_EXPR_ARG (exp, 0); |
89cfe6e5 | 5455 | |
c2f47e15 | 5456 | target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5a74f77e | 5457 | /* When guessing was done, the hints should be already stripped away. */ |
07311427 | 5458 | gcc_assert (!flag_guess_branch_prob |
852f689e | 5459 | || optimize == 0 || seen_error ()); |
89cfe6e5 | 5460 | return target; |
5461 | } | |
689df48e | 5462 | |
01107f42 | 5463 | /* Expand a call to __builtin_expect_with_probability. We just return our |
5464 | argument as the builtin_expect semantic should've been already executed by | |
5465 | tree branch prediction pass. */ | |
5466 | ||
5467 | static rtx | |
5468 | expand_builtin_expect_with_probability (tree exp, rtx target) | |
5469 | { | |
5470 | tree arg; | |
5471 | ||
5472 | if (call_expr_nargs (exp) < 3) | |
5473 | return const0_rtx; | |
5474 | arg = CALL_EXPR_ARG (exp, 0); | |
5475 | ||
5476 | target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); | |
5477 | /* When guessing was done, the hints should be already stripped away. */ | |
5478 | gcc_assert (!flag_guess_branch_prob | |
5479 | || optimize == 0 || seen_error ()); | |
5480 | return target; | |
5481 | } | |
5482 | ||
5483 | ||
fca0886c | 5484 | /* Expand a call to __builtin_assume_aligned. We just return our first |
5485 | argument as the builtin_assume_aligned semantic should've been already | |
5486 | executed by CCP. */ | |
5487 | ||
5488 | static rtx | |
5489 | expand_builtin_assume_aligned (tree exp, rtx target) | |
5490 | { | |
5491 | if (call_expr_nargs (exp) < 2) | |
5492 | return const0_rtx; | |
5493 | target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode, | |
5494 | EXPAND_NORMAL); | |
5495 | gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1)) | |
5496 | && (call_expr_nargs (exp) < 3 | |
5497 | || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2)))); | |
5498 | return target; | |
5499 | } | |
5500 | ||
c22de3f0 | 5501 | void |
aecda0d6 | 5502 | expand_builtin_trap (void) |
a0ef1725 | 5503 | { |
4db8dd0c | 5504 | if (targetm.have_trap ()) |
f73960eb | 5505 | { |
4db8dd0c | 5506 | rtx_insn *insn = emit_insn (targetm.gen_trap ()); |
f73960eb | 5507 | /* For trap insns when not accumulating outgoing args force |
5508 | REG_ARGS_SIZE note to prevent crossjumping of calls with | |
5509 | different args sizes. */ | |
5510 | if (!ACCUMULATE_OUTGOING_ARGS) | |
f6a1fc98 | 5511 | add_args_size_note (insn, stack_pointer_delta); |
f73960eb | 5512 | } |
a0ef1725 | 5513 | else |
61ffc71a | 5514 | { |
5515 | tree fn = builtin_decl_implicit (BUILT_IN_ABORT); | |
5516 | tree call_expr = build_call_expr (fn, 0); | |
5517 | expand_call (call_expr, NULL_RTX, false); | |
5518 | } | |
5519 | ||
a0ef1725 | 5520 | emit_barrier (); |
5521 | } | |
78a74442 | 5522 | |
d2b48f0c | 5523 | /* Expand a call to __builtin_unreachable. We do nothing except emit |
5524 | a barrier saying that control flow will not pass here. | |
5525 | ||
5526 | It is the responsibility of the program being compiled to ensure | |
5527 | that control flow does never reach __builtin_unreachable. */ | |
5528 | static void | |
5529 | expand_builtin_unreachable (void) | |
5530 | { | |
5531 | emit_barrier (); | |
5532 | } | |
5533 | ||
c2f47e15 | 5534 | /* Expand EXP, a call to fabs, fabsf or fabsl. |
5535 | Return NULL_RTX if a normal call should be emitted rather than expanding | |
78a74442 | 5536 | the function inline. If convenient, the result should be placed |
5537 | in TARGET. SUBTARGET may be used as the target for computing | |
5538 | the operand. */ | |
5539 | ||
5540 | static rtx | |
c2f47e15 | 5541 | expand_builtin_fabs (tree exp, rtx target, rtx subtarget) |
78a74442 | 5542 | { |
3754d046 | 5543 | machine_mode mode; |
78a74442 | 5544 | tree arg; |
5545 | rtx op0; | |
5546 | ||
c2f47e15 | 5547 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
5548 | return NULL_RTX; | |
78a74442 | 5549 | |
c2f47e15 | 5550 | arg = CALL_EXPR_ARG (exp, 0); |
c7f617c2 | 5551 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
78a74442 | 5552 | mode = TYPE_MODE (TREE_TYPE (arg)); |
1db6d067 | 5553 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
78a74442 | 5554 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); |
5555 | } | |
5556 | ||
c2f47e15 | 5557 | /* Expand EXP, a call to copysign, copysignf, or copysignl. |
270436f3 | 5558 | Return NULL is a normal call should be emitted rather than expanding the |
5559 | function inline. If convenient, the result should be placed in TARGET. | |
5560 | SUBTARGET may be used as the target for computing the operand. */ | |
5561 | ||
5562 | static rtx | |
c2f47e15 | 5563 | expand_builtin_copysign (tree exp, rtx target, rtx subtarget) |
270436f3 | 5564 | { |
5565 | rtx op0, op1; | |
5566 | tree arg; | |
5567 | ||
c2f47e15 | 5568 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE)) |
5569 | return NULL_RTX; | |
270436f3 | 5570 | |
c2f47e15 | 5571 | arg = CALL_EXPR_ARG (exp, 0); |
8ec3c5c2 | 5572 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
270436f3 | 5573 | |
c2f47e15 | 5574 | arg = CALL_EXPR_ARG (exp, 1); |
8ec3c5c2 | 5575 | op1 = expand_normal (arg); |
270436f3 | 5576 | |
5577 | return expand_copysign (op0, op1, target); | |
5578 | } | |
5579 | ||
ac8fb6db | 5580 | /* Expand a call to __builtin___clear_cache. */ |
5581 | ||
5582 | static rtx | |
32e17df0 | 5583 | expand_builtin___clear_cache (tree exp) |
ac8fb6db | 5584 | { |
32e17df0 | 5585 | if (!targetm.code_for_clear_cache) |
5586 | { | |
ac8fb6db | 5587 | #ifdef CLEAR_INSN_CACHE |
32e17df0 | 5588 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
5589 | does something. Just do the default expansion to a call to | |
5590 | __clear_cache(). */ | |
5591 | return NULL_RTX; | |
ac8fb6db | 5592 | #else |
32e17df0 | 5593 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
5594 | does nothing. There is no need to call it. Do nothing. */ | |
5595 | return const0_rtx; | |
ac8fb6db | 5596 | #endif /* CLEAR_INSN_CACHE */ |
32e17df0 | 5597 | } |
5598 | ||
ac8fb6db | 5599 | /* We have a "clear_cache" insn, and it will handle everything. */ |
5600 | tree begin, end; | |
5601 | rtx begin_rtx, end_rtx; | |
ac8fb6db | 5602 | |
5603 | /* We must not expand to a library call. If we did, any | |
5604 | fallback library function in libgcc that might contain a call to | |
5605 | __builtin___clear_cache() would recurse infinitely. */ | |
5606 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
5607 | { | |
5608 | error ("both arguments to %<__builtin___clear_cache%> must be pointers"); | |
5609 | return const0_rtx; | |
5610 | } | |
5611 | ||
32e17df0 | 5612 | if (targetm.have_clear_cache ()) |
ac8fb6db | 5613 | { |
8786db1e | 5614 | struct expand_operand ops[2]; |
ac8fb6db | 5615 | |
5616 | begin = CALL_EXPR_ARG (exp, 0); | |
5617 | begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL); | |
ac8fb6db | 5618 | |
5619 | end = CALL_EXPR_ARG (exp, 1); | |
5620 | end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL); | |
ac8fb6db | 5621 | |
8786db1e | 5622 | create_address_operand (&ops[0], begin_rtx); |
5623 | create_address_operand (&ops[1], end_rtx); | |
32e17df0 | 5624 | if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops)) |
8786db1e | 5625 | return const0_rtx; |
ac8fb6db | 5626 | } |
5627 | return const0_rtx; | |
ac8fb6db | 5628 | } |
5629 | ||
4ee9c684 | 5630 | /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */ |
5631 | ||
5632 | static rtx | |
5633 | round_trampoline_addr (rtx tramp) | |
5634 | { | |
5635 | rtx temp, addend, mask; | |
5636 | ||
5637 | /* If we don't need too much alignment, we'll have been guaranteed | |
5638 | proper alignment by get_trampoline_type. */ | |
5639 | if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY) | |
5640 | return tramp; | |
5641 | ||
5642 | /* Round address up to desired boundary. */ | |
5643 | temp = gen_reg_rtx (Pmode); | |
0359f9f5 | 5644 | addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode); |
5645 | mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode); | |
4ee9c684 | 5646 | |
5647 | temp = expand_simple_binop (Pmode, PLUS, tramp, addend, | |
5648 | temp, 0, OPTAB_LIB_WIDEN); | |
5649 | tramp = expand_simple_binop (Pmode, AND, temp, mask, | |
5650 | temp, 0, OPTAB_LIB_WIDEN); | |
5651 | ||
5652 | return tramp; | |
5653 | } | |
5654 | ||
5655 | static rtx | |
c307f106 | 5656 | expand_builtin_init_trampoline (tree exp, bool onstack) |
4ee9c684 | 5657 | { |
5658 | tree t_tramp, t_func, t_chain; | |
82c7907c | 5659 | rtx m_tramp, r_tramp, r_chain, tmp; |
4ee9c684 | 5660 | |
c2f47e15 | 5661 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, |
4ee9c684 | 5662 | POINTER_TYPE, VOID_TYPE)) |
5663 | return NULL_RTX; | |
5664 | ||
c2f47e15 | 5665 | t_tramp = CALL_EXPR_ARG (exp, 0); |
5666 | t_func = CALL_EXPR_ARG (exp, 1); | |
5667 | t_chain = CALL_EXPR_ARG (exp, 2); | |
4ee9c684 | 5668 | |
8ec3c5c2 | 5669 | r_tramp = expand_normal (t_tramp); |
82c7907c | 5670 | m_tramp = gen_rtx_MEM (BLKmode, r_tramp); |
5671 | MEM_NOTRAP_P (m_tramp) = 1; | |
5672 | ||
c307f106 | 5673 | /* If ONSTACK, the TRAMP argument should be the address of a field |
5674 | within the local function's FRAME decl. Either way, let's see if | |
5675 | we can fill in the MEM_ATTRs for this memory. */ | |
82c7907c | 5676 | if (TREE_CODE (t_tramp) == ADDR_EXPR) |
f4146cb8 | 5677 | set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true); |
82c7907c | 5678 | |
c307f106 | 5679 | /* Creator of a heap trampoline is responsible for making sure the |
5680 | address is aligned to at least STACK_BOUNDARY. Normally malloc | |
5681 | will ensure this anyhow. */ | |
82c7907c | 5682 | tmp = round_trampoline_addr (r_tramp); |
5683 | if (tmp != r_tramp) | |
5684 | { | |
5685 | m_tramp = change_address (m_tramp, BLKmode, tmp); | |
5686 | set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT); | |
5b2a69fa | 5687 | set_mem_size (m_tramp, TRAMPOLINE_SIZE); |
82c7907c | 5688 | } |
5689 | ||
5690 | /* The FUNC argument should be the address of the nested function. | |
5691 | Extract the actual function decl to pass to the hook. */ | |
5692 | gcc_assert (TREE_CODE (t_func) == ADDR_EXPR); | |
5693 | t_func = TREE_OPERAND (t_func, 0); | |
5694 | gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL); | |
5695 | ||
8ec3c5c2 | 5696 | r_chain = expand_normal (t_chain); |
4ee9c684 | 5697 | |
5698 | /* Generate insns to initialize the trampoline. */ | |
82c7907c | 5699 | targetm.calls.trampoline_init (m_tramp, t_func, r_chain); |
4ee9c684 | 5700 | |
c307f106 | 5701 | if (onstack) |
5702 | { | |
5703 | trampolines_created = 1; | |
8bc8a8f4 | 5704 | |
a27e3913 | 5705 | if (targetm.calls.custom_function_descriptors != 0) |
5706 | warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines, | |
5707 | "trampoline generated for nested function %qD", t_func); | |
c307f106 | 5708 | } |
8bc8a8f4 | 5709 | |
4ee9c684 | 5710 | return const0_rtx; |
5711 | } | |
5712 | ||
5713 | static rtx | |
c2f47e15 | 5714 | expand_builtin_adjust_trampoline (tree exp) |
4ee9c684 | 5715 | { |
5716 | rtx tramp; | |
5717 | ||
c2f47e15 | 5718 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 5719 | return NULL_RTX; |
5720 | ||
c2f47e15 | 5721 | tramp = expand_normal (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 5722 | tramp = round_trampoline_addr (tramp); |
82c7907c | 5723 | if (targetm.calls.trampoline_adjust_address) |
5724 | tramp = targetm.calls.trampoline_adjust_address (tramp); | |
4ee9c684 | 5725 | |
5726 | return tramp; | |
5727 | } | |
5728 | ||
a27e3913 | 5729 | /* Expand a call to the builtin descriptor initialization routine. |
5730 | A descriptor is made up of a couple of pointers to the static | |
5731 | chain and the code entry in this order. */ | |
5732 | ||
5733 | static rtx | |
5734 | expand_builtin_init_descriptor (tree exp) | |
5735 | { | |
5736 | tree t_descr, t_func, t_chain; | |
5737 | rtx m_descr, r_descr, r_func, r_chain; | |
5738 | ||
5739 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE, | |
5740 | VOID_TYPE)) | |
5741 | return NULL_RTX; | |
5742 | ||
5743 | t_descr = CALL_EXPR_ARG (exp, 0); | |
5744 | t_func = CALL_EXPR_ARG (exp, 1); | |
5745 | t_chain = CALL_EXPR_ARG (exp, 2); | |
5746 | ||
5747 | r_descr = expand_normal (t_descr); | |
5748 | m_descr = gen_rtx_MEM (BLKmode, r_descr); | |
5749 | MEM_NOTRAP_P (m_descr) = 1; | |
5750 | ||
5751 | r_func = expand_normal (t_func); | |
5752 | r_chain = expand_normal (t_chain); | |
5753 | ||
5754 | /* Generate insns to initialize the descriptor. */ | |
5755 | emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain); | |
5756 | emit_move_insn (adjust_address_nv (m_descr, ptr_mode, | |
5757 | POINTER_SIZE / BITS_PER_UNIT), r_func); | |
5758 | ||
5759 | return const0_rtx; | |
5760 | } | |
5761 | ||
5762 | /* Expand a call to the builtin descriptor adjustment routine. */ | |
5763 | ||
5764 | static rtx | |
5765 | expand_builtin_adjust_descriptor (tree exp) | |
5766 | { | |
5767 | rtx tramp; | |
5768 | ||
5769 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) | |
5770 | return NULL_RTX; | |
5771 | ||
5772 | tramp = expand_normal (CALL_EXPR_ARG (exp, 0)); | |
5773 | ||
5774 | /* Unalign the descriptor to allow runtime identification. */ | |
5775 | tramp = plus_constant (ptr_mode, tramp, | |
5776 | targetm.calls.custom_function_descriptors); | |
5777 | ||
5778 | return force_operand (tramp, NULL_RTX); | |
5779 | } | |
5780 | ||
93f564d6 | 5781 | /* Expand the call EXP to the built-in signbit, signbitf or signbitl |
5782 | function. The function first checks whether the back end provides | |
5783 | an insn to implement signbit for the respective mode. If not, it | |
5784 | checks whether the floating point format of the value is such that | |
10902624 | 5785 | the sign bit can be extracted. If that is not the case, error out. |
5786 | EXP is the expression that is a call to the builtin function; if | |
5787 | convenient, the result should be placed in TARGET. */ | |
27f261ef | 5788 | static rtx |
5789 | expand_builtin_signbit (tree exp, rtx target) | |
5790 | { | |
5791 | const struct real_format *fmt; | |
299dd9fa | 5792 | scalar_float_mode fmode; |
f77c4496 | 5793 | scalar_int_mode rmode, imode; |
c2f47e15 | 5794 | tree arg; |
ca4f1f5b | 5795 | int word, bitpos; |
27eda240 | 5796 | enum insn_code icode; |
27f261ef | 5797 | rtx temp; |
389dd41b | 5798 | location_t loc = EXPR_LOCATION (exp); |
27f261ef | 5799 | |
c2f47e15 | 5800 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
5801 | return NULL_RTX; | |
27f261ef | 5802 | |
c2f47e15 | 5803 | arg = CALL_EXPR_ARG (exp, 0); |
299dd9fa | 5804 | fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg)); |
03b7a719 | 5805 | rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)); |
27f261ef | 5806 | fmt = REAL_MODE_FORMAT (fmode); |
5807 | ||
93f564d6 | 5808 | arg = builtin_save_expr (arg); |
5809 | ||
5810 | /* Expand the argument yielding a RTX expression. */ | |
5811 | temp = expand_normal (arg); | |
5812 | ||
5813 | /* Check if the back end provides an insn that handles signbit for the | |
5814 | argument's mode. */ | |
d6bf3b14 | 5815 | icode = optab_handler (signbit_optab, fmode); |
27eda240 | 5816 | if (icode != CODE_FOR_nothing) |
93f564d6 | 5817 | { |
1e0c0b35 | 5818 | rtx_insn *last = get_last_insn (); |
93f564d6 | 5819 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); |
4e2a2fb4 | 5820 | if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN)) |
5821 | return target; | |
5822 | delete_insns_since (last); | |
93f564d6 | 5823 | } |
5824 | ||
27f261ef | 5825 | /* For floating point formats without a sign bit, implement signbit |
5826 | as "ARG < 0.0". */ | |
8d564692 | 5827 | bitpos = fmt->signbit_ro; |
ca4f1f5b | 5828 | if (bitpos < 0) |
27f261ef | 5829 | { |
5830 | /* But we can't do this if the format supports signed zero. */ | |
10902624 | 5831 | gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode)); |
27f261ef | 5832 | |
389dd41b | 5833 | arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg, |
49d00087 | 5834 | build_real (TREE_TYPE (arg), dconst0)); |
27f261ef | 5835 | return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5836 | } | |
5837 | ||
ca4f1f5b | 5838 | if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD) |
27f261ef | 5839 | { |
2cf1bb25 | 5840 | imode = int_mode_for_mode (fmode).require (); |
ca4f1f5b | 5841 | temp = gen_lowpart (imode, temp); |
24fd4260 | 5842 | } |
5843 | else | |
5844 | { | |
ca4f1f5b | 5845 | imode = word_mode; |
5846 | /* Handle targets with different FP word orders. */ | |
5847 | if (FLOAT_WORDS_BIG_ENDIAN) | |
a0c938f0 | 5848 | word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD; |
ca4f1f5b | 5849 | else |
a0c938f0 | 5850 | word = bitpos / BITS_PER_WORD; |
ca4f1f5b | 5851 | temp = operand_subword_force (temp, word, fmode); |
5852 | bitpos = bitpos % BITS_PER_WORD; | |
5853 | } | |
5854 | ||
44b0f1d0 | 5855 | /* Force the intermediate word_mode (or narrower) result into a |
5856 | register. This avoids attempting to create paradoxical SUBREGs | |
5857 | of floating point modes below. */ | |
5858 | temp = force_reg (imode, temp); | |
5859 | ||
ca4f1f5b | 5860 | /* If the bitpos is within the "result mode" lowpart, the operation |
5861 | can be implement with a single bitwise AND. Otherwise, we need | |
5862 | a right shift and an AND. */ | |
5863 | ||
5864 | if (bitpos < GET_MODE_BITSIZE (rmode)) | |
5865 | { | |
796b6678 | 5866 | wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode)); |
27f261ef | 5867 | |
4a46f016 | 5868 | if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode)) |
ca4f1f5b | 5869 | temp = gen_lowpart (rmode, temp); |
24fd4260 | 5870 | temp = expand_binop (rmode, and_optab, temp, |
e913b5cd | 5871 | immed_wide_int_const (mask, rmode), |
ca4f1f5b | 5872 | NULL_RTX, 1, OPTAB_LIB_WIDEN); |
27f261ef | 5873 | } |
ca4f1f5b | 5874 | else |
5875 | { | |
5876 | /* Perform a logical right shift to place the signbit in the least | |
a0c938f0 | 5877 | significant bit, then truncate the result to the desired mode |
ca4f1f5b | 5878 | and mask just this bit. */ |
f5ff0b21 | 5879 | temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1); |
ca4f1f5b | 5880 | temp = gen_lowpart (rmode, temp); |
5881 | temp = expand_binop (rmode, and_optab, temp, const1_rtx, | |
5882 | NULL_RTX, 1, OPTAB_LIB_WIDEN); | |
5883 | } | |
5884 | ||
27f261ef | 5885 | return temp; |
5886 | } | |
73673831 | 5887 | |
5888 | /* Expand fork or exec calls. TARGET is the desired target of the | |
c2f47e15 | 5889 | call. EXP is the call. FN is the |
73673831 | 5890 | identificator of the actual function. IGNORE is nonzero if the |
5891 | value is to be ignored. */ | |
5892 | ||
5893 | static rtx | |
c2f47e15 | 5894 | expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore) |
73673831 | 5895 | { |
5896 | tree id, decl; | |
5897 | tree call; | |
5898 | ||
5899 | /* If we are not profiling, just call the function. */ | |
5900 | if (!profile_arc_flag) | |
5901 | return NULL_RTX; | |
5902 | ||
5903 | /* Otherwise call the wrapper. This should be equivalent for the rest of | |
5904 | compiler, so the code does not diverge, and the wrapper may run the | |
9c9bad97 | 5905 | code necessary for keeping the profiling sane. */ |
73673831 | 5906 | |
5907 | switch (DECL_FUNCTION_CODE (fn)) | |
5908 | { | |
5909 | case BUILT_IN_FORK: | |
5910 | id = get_identifier ("__gcov_fork"); | |
5911 | break; | |
5912 | ||
5913 | case BUILT_IN_EXECL: | |
5914 | id = get_identifier ("__gcov_execl"); | |
5915 | break; | |
5916 | ||
5917 | case BUILT_IN_EXECV: | |
5918 | id = get_identifier ("__gcov_execv"); | |
5919 | break; | |
5920 | ||
5921 | case BUILT_IN_EXECLP: | |
5922 | id = get_identifier ("__gcov_execlp"); | |
5923 | break; | |
5924 | ||
5925 | case BUILT_IN_EXECLE: | |
5926 | id = get_identifier ("__gcov_execle"); | |
5927 | break; | |
5928 | ||
5929 | case BUILT_IN_EXECVP: | |
5930 | id = get_identifier ("__gcov_execvp"); | |
5931 | break; | |
5932 | ||
5933 | case BUILT_IN_EXECVE: | |
5934 | id = get_identifier ("__gcov_execve"); | |
5935 | break; | |
5936 | ||
5937 | default: | |
64db345d | 5938 | gcc_unreachable (); |
73673831 | 5939 | } |
5940 | ||
e60a6f7b | 5941 | decl = build_decl (DECL_SOURCE_LOCATION (fn), |
5942 | FUNCTION_DECL, id, TREE_TYPE (fn)); | |
73673831 | 5943 | DECL_EXTERNAL (decl) = 1; |
5944 | TREE_PUBLIC (decl) = 1; | |
5945 | DECL_ARTIFICIAL (decl) = 1; | |
5946 | TREE_NOTHROW (decl) = 1; | |
e82d310b | 5947 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
5948 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
389dd41b | 5949 | call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0); |
73673831 | 5950 | return expand_call (call, target, ignore); |
c2f47e15 | 5951 | } |
48e1416a | 5952 | |
b6a5fc45 | 5953 | |
5954 | \f | |
3e272de8 | 5955 | /* Reconstitute a mode for a __sync intrinsic operation. Since the type of |
5956 | the pointer in these functions is void*, the tree optimizers may remove | |
5957 | casts. The mode computed in expand_builtin isn't reliable either, due | |
5958 | to __sync_bool_compare_and_swap. | |
5959 | ||
5960 | FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the | |
5961 | group of builtins. This gives us log2 of the mode size. */ | |
5962 | ||
3754d046 | 5963 | static inline machine_mode |
3e272de8 | 5964 | get_builtin_sync_mode (int fcode_diff) |
5965 | { | |
ad3a13b5 | 5966 | /* The size is not negotiable, so ask not to get BLKmode in return |
5967 | if the target indicates that a smaller size would be better. */ | |
517be012 | 5968 | return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require (); |
3e272de8 | 5969 | } |
5970 | ||
041e0215 | 5971 | /* Expand the memory expression LOC and return the appropriate memory operand |
5972 | for the builtin_sync operations. */ | |
5973 | ||
5974 | static rtx | |
3754d046 | 5975 | get_builtin_sync_mem (tree loc, machine_mode mode) |
041e0215 | 5976 | { |
5977 | rtx addr, mem; | |
fcbc2234 | 5978 | int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc)) |
5979 | ? TREE_TYPE (TREE_TYPE (loc)) | |
5980 | : TREE_TYPE (loc)); | |
5981 | scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space); | |
041e0215 | 5982 | |
fcbc2234 | 5983 | addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM); |
ed825d83 | 5984 | addr = convert_memory_address (addr_mode, addr); |
041e0215 | 5985 | |
5986 | /* Note that we explicitly do not want any alias information for this | |
5987 | memory, so that we kill all other live memories. Otherwise we don't | |
5988 | satisfy the full barrier semantics of the intrinsic. */ | |
fcbc2234 | 5989 | mem = gen_rtx_MEM (mode, addr); |
5990 | ||
5991 | set_mem_addr_space (mem, addr_space); | |
5992 | ||
5993 | mem = validize_mem (mem); | |
041e0215 | 5994 | |
153c3b50 | 5995 | /* The alignment needs to be at least according to that of the mode. */ |
5996 | set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode), | |
957d0361 | 5997 | get_pointer_alignment (loc))); |
c94cfd1c | 5998 | set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER); |
041e0215 | 5999 | MEM_VOLATILE_P (mem) = 1; |
6000 | ||
6001 | return mem; | |
6002 | } | |
6003 | ||
1cd6e20d | 6004 | /* Make sure an argument is in the right mode. |
6005 | EXP is the tree argument. | |
6006 | MODE is the mode it should be in. */ | |
6007 | ||
6008 | static rtx | |
3754d046 | 6009 | expand_expr_force_mode (tree exp, machine_mode mode) |
1cd6e20d | 6010 | { |
6011 | rtx val; | |
3754d046 | 6012 | machine_mode old_mode; |
1cd6e20d | 6013 | |
6014 | val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL); | |
6015 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care | |
6016 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
6017 | ||
6018 | old_mode = GET_MODE (val); | |
6019 | if (old_mode == VOIDmode) | |
6020 | old_mode = TYPE_MODE (TREE_TYPE (exp)); | |
6021 | val = convert_modes (mode, old_mode, val, 1); | |
6022 | return val; | |
6023 | } | |
6024 | ||
6025 | ||
b6a5fc45 | 6026 | /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics. |
c2f47e15 | 6027 | EXP is the CALL_EXPR. CODE is the rtx code |
b6a5fc45 | 6028 | that corresponds to the arithmetic or logical operation from the name; |
6029 | an exception here is that NOT actually means NAND. TARGET is an optional | |
6030 | place for us to store the results; AFTER is true if this is the | |
1cd6e20d | 6031 | fetch_and_xxx form. */ |
b6a5fc45 | 6032 | |
6033 | static rtx | |
3754d046 | 6034 | expand_builtin_sync_operation (machine_mode mode, tree exp, |
3e272de8 | 6035 | enum rtx_code code, bool after, |
1cd6e20d | 6036 | rtx target) |
b6a5fc45 | 6037 | { |
041e0215 | 6038 | rtx val, mem; |
e60a6f7b | 6039 | location_t loc = EXPR_LOCATION (exp); |
b6a5fc45 | 6040 | |
cf73e559 | 6041 | if (code == NOT && warn_sync_nand) |
6042 | { | |
6043 | tree fndecl = get_callee_fndecl (exp); | |
6044 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6045 | ||
6046 | static bool warned_f_a_n, warned_n_a_f; | |
6047 | ||
6048 | switch (fcode) | |
6049 | { | |
2797f13a | 6050 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
6051 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
6052 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
6053 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
6054 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
cf73e559 | 6055 | if (warned_f_a_n) |
6056 | break; | |
6057 | ||
b9a16870 | 6058 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N); |
e60a6f7b | 6059 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
cf73e559 | 6060 | warned_f_a_n = true; |
6061 | break; | |
6062 | ||
2797f13a | 6063 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
6064 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
6065 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
6066 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
6067 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
cf73e559 | 6068 | if (warned_n_a_f) |
6069 | break; | |
6070 | ||
b9a16870 | 6071 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N); |
e60a6f7b | 6072 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
cf73e559 | 6073 | warned_n_a_f = true; |
6074 | break; | |
6075 | ||
6076 | default: | |
6077 | gcc_unreachable (); | |
6078 | } | |
6079 | } | |
6080 | ||
b6a5fc45 | 6081 | /* Expand the operands. */ |
c2f47e15 | 6082 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 6083 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
b6a5fc45 | 6084 | |
a372f7ca | 6085 | return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST, |
1cd6e20d | 6086 | after); |
b6a5fc45 | 6087 | } |
6088 | ||
6089 | /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap | |
c2f47e15 | 6090 | intrinsics. EXP is the CALL_EXPR. IS_BOOL is |
b6a5fc45 | 6091 | true if this is the boolean form. TARGET is a place for us to store the |
6092 | results; this is NOT optional if IS_BOOL is true. */ | |
6093 | ||
6094 | static rtx | |
3754d046 | 6095 | expand_builtin_compare_and_swap (machine_mode mode, tree exp, |
3e272de8 | 6096 | bool is_bool, rtx target) |
b6a5fc45 | 6097 | { |
041e0215 | 6098 | rtx old_val, new_val, mem; |
ba885f6a | 6099 | rtx *pbool, *poval; |
b6a5fc45 | 6100 | |
6101 | /* Expand the operands. */ | |
c2f47e15 | 6102 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 6103 | old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
6104 | new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); | |
b6a5fc45 | 6105 | |
ba885f6a | 6106 | pbool = poval = NULL; |
6107 | if (target != const0_rtx) | |
6108 | { | |
6109 | if (is_bool) | |
6110 | pbool = ⌖ | |
6111 | else | |
6112 | poval = ⌖ | |
6113 | } | |
6114 | if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val, | |
a372f7ca | 6115 | false, MEMMODEL_SYNC_SEQ_CST, |
6116 | MEMMODEL_SYNC_SEQ_CST)) | |
1cd6e20d | 6117 | return NULL_RTX; |
c2f47e15 | 6118 | |
1cd6e20d | 6119 | return target; |
b6a5fc45 | 6120 | } |
6121 | ||
6122 | /* Expand the __sync_lock_test_and_set intrinsic. Note that the most | |
6123 | general form is actually an atomic exchange, and some targets only | |
6124 | support a reduced form with the second argument being a constant 1. | |
48e1416a | 6125 | EXP is the CALL_EXPR; TARGET is an optional place for us to store |
c2f47e15 | 6126 | the results. */ |
b6a5fc45 | 6127 | |
6128 | static rtx | |
3754d046 | 6129 | expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp, |
1cd6e20d | 6130 | rtx target) |
b6a5fc45 | 6131 | { |
041e0215 | 6132 | rtx val, mem; |
b6a5fc45 | 6133 | |
6134 | /* Expand the operands. */ | |
c2f47e15 | 6135 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 6136 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
6137 | ||
7821cde1 | 6138 | return expand_sync_lock_test_and_set (target, mem, val); |
1cd6e20d | 6139 | } |
6140 | ||
6141 | /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */ | |
6142 | ||
6143 | static void | |
3754d046 | 6144 | expand_builtin_sync_lock_release (machine_mode mode, tree exp) |
1cd6e20d | 6145 | { |
6146 | rtx mem; | |
6147 | ||
6148 | /* Expand the operands. */ | |
6149 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6150 | ||
a372f7ca | 6151 | expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true); |
1cd6e20d | 6152 | } |
6153 | ||
6154 | /* Given an integer representing an ``enum memmodel'', verify its | |
6155 | correctness and return the memory model enum. */ | |
6156 | ||
6157 | static enum memmodel | |
6158 | get_memmodel (tree exp) | |
6159 | { | |
6160 | rtx op; | |
7f738025 | 6161 | unsigned HOST_WIDE_INT val; |
be1e7283 | 6162 | location_t loc |
2cb724f9 | 6163 | = expansion_point_location_if_in_system_header (input_location); |
1cd6e20d | 6164 | |
6165 | /* If the parameter is not a constant, it's a run time value so we'll just | |
6166 | convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */ | |
6167 | if (TREE_CODE (exp) != INTEGER_CST) | |
6168 | return MEMMODEL_SEQ_CST; | |
6169 | ||
6170 | op = expand_normal (exp); | |
7f738025 | 6171 | |
6172 | val = INTVAL (op); | |
6173 | if (targetm.memmodel_check) | |
6174 | val = targetm.memmodel_check (val); | |
6175 | else if (val & ~MEMMODEL_MASK) | |
6176 | { | |
2cb724f9 | 6177 | warning_at (loc, OPT_Winvalid_memory_model, |
6178 | "unknown architecture specifier in memory model to builtin"); | |
7f738025 | 6179 | return MEMMODEL_SEQ_CST; |
6180 | } | |
6181 | ||
a372f7ca | 6182 | /* Should never see a user explicit SYNC memodel model, so >= LAST works. */ |
6183 | if (memmodel_base (val) >= MEMMODEL_LAST) | |
1cd6e20d | 6184 | { |
2cb724f9 | 6185 | warning_at (loc, OPT_Winvalid_memory_model, |
6186 | "invalid memory model argument to builtin"); | |
1cd6e20d | 6187 | return MEMMODEL_SEQ_CST; |
6188 | } | |
7f738025 | 6189 | |
3070f133 | 6190 | /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so |
6191 | be conservative and promote consume to acquire. */ | |
6192 | if (val == MEMMODEL_CONSUME) | |
6193 | val = MEMMODEL_ACQUIRE; | |
6194 | ||
7f738025 | 6195 | return (enum memmodel) val; |
1cd6e20d | 6196 | } |
6197 | ||
6198 | /* Expand the __atomic_exchange intrinsic: | |
6199 | TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel) | |
6200 | EXP is the CALL_EXPR. | |
6201 | TARGET is an optional place for us to store the results. */ | |
6202 | ||
6203 | static rtx | |
3754d046 | 6204 | expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target) |
1cd6e20d | 6205 | { |
6206 | rtx val, mem; | |
6207 | enum memmodel model; | |
6208 | ||
6209 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
1cd6e20d | 6210 | |
6211 | if (!flag_inline_atomics) | |
6212 | return NULL_RTX; | |
6213 | ||
6214 | /* Expand the operands. */ | |
6215 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6216 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
6217 | ||
7821cde1 | 6218 | return expand_atomic_exchange (target, mem, val, model); |
1cd6e20d | 6219 | } |
6220 | ||
6221 | /* Expand the __atomic_compare_exchange intrinsic: | |
6222 | bool __atomic_compare_exchange (TYPE *object, TYPE *expect, | |
6223 | TYPE desired, BOOL weak, | |
6224 | enum memmodel success, | |
6225 | enum memmodel failure) | |
6226 | EXP is the CALL_EXPR. | |
6227 | TARGET is an optional place for us to store the results. */ | |
6228 | ||
6229 | static rtx | |
3754d046 | 6230 | expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp, |
1cd6e20d | 6231 | rtx target) |
6232 | { | |
1e0c0b35 | 6233 | rtx expect, desired, mem, oldval; |
6234 | rtx_code_label *label; | |
1cd6e20d | 6235 | enum memmodel success, failure; |
6236 | tree weak; | |
6237 | bool is_weak; | |
be1e7283 | 6238 | location_t loc |
2cb724f9 | 6239 | = expansion_point_location_if_in_system_header (input_location); |
1cd6e20d | 6240 | |
6241 | success = get_memmodel (CALL_EXPR_ARG (exp, 4)); | |
6242 | failure = get_memmodel (CALL_EXPR_ARG (exp, 5)); | |
6243 | ||
086f4e33 | 6244 | if (failure > success) |
6245 | { | |
2cb724f9 | 6246 | warning_at (loc, OPT_Winvalid_memory_model, |
6247 | "failure memory model cannot be stronger than success " | |
6248 | "memory model for %<__atomic_compare_exchange%>"); | |
086f4e33 | 6249 | success = MEMMODEL_SEQ_CST; |
6250 | } | |
6251 | ||
a372f7ca | 6252 | if (is_mm_release (failure) || is_mm_acq_rel (failure)) |
1cd6e20d | 6253 | { |
2cb724f9 | 6254 | warning_at (loc, OPT_Winvalid_memory_model, |
6255 | "invalid failure memory model for " | |
6256 | "%<__atomic_compare_exchange%>"); | |
086f4e33 | 6257 | failure = MEMMODEL_SEQ_CST; |
6258 | success = MEMMODEL_SEQ_CST; | |
1cd6e20d | 6259 | } |
6260 | ||
086f4e33 | 6261 | |
1cd6e20d | 6262 | if (!flag_inline_atomics) |
6263 | return NULL_RTX; | |
6264 | ||
6265 | /* Expand the operands. */ | |
6266 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6267 | ||
6268 | expect = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
6269 | expect = convert_memory_address (Pmode, expect); | |
c401b131 | 6270 | expect = gen_rtx_MEM (mode, expect); |
1cd6e20d | 6271 | desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); |
6272 | ||
6273 | weak = CALL_EXPR_ARG (exp, 3); | |
6274 | is_weak = false; | |
e913b5cd | 6275 | if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0) |
1cd6e20d | 6276 | is_weak = true; |
6277 | ||
d86e3752 | 6278 | if (target == const0_rtx) |
6279 | target = NULL; | |
d86e3752 | 6280 | |
3c29a9ea | 6281 | /* Lest the rtl backend create a race condition with an imporoper store |
6282 | to memory, always create a new pseudo for OLDVAL. */ | |
6283 | oldval = NULL; | |
6284 | ||
6285 | if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired, | |
ba885f6a | 6286 | is_weak, success, failure)) |
1cd6e20d | 6287 | return NULL_RTX; |
6288 | ||
d86e3752 | 6289 | /* Conditionally store back to EXPECT, lest we create a race condition |
6290 | with an improper store to memory. */ | |
6291 | /* ??? With a rearrangement of atomics at the gimple level, we can handle | |
6292 | the normal case where EXPECT is totally private, i.e. a register. At | |
6293 | which point the store can be unconditional. */ | |
6294 | label = gen_label_rtx (); | |
62589f76 | 6295 | emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, |
6296 | GET_MODE (target), 1, label); | |
d86e3752 | 6297 | emit_move_insn (expect, oldval); |
6298 | emit_label (label); | |
c401b131 | 6299 | |
1cd6e20d | 6300 | return target; |
6301 | } | |
6302 | ||
5a5ef659 | 6303 | /* Helper function for expand_ifn_atomic_compare_exchange - expand |
6304 | internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N | |
6305 | call. The weak parameter must be dropped to match the expected parameter | |
6306 | list and the expected argument changed from value to pointer to memory | |
6307 | slot. */ | |
6308 | ||
6309 | static void | |
6310 | expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode) | |
6311 | { | |
6312 | unsigned int z; | |
6313 | vec<tree, va_gc> *vec; | |
6314 | ||
6315 | vec_alloc (vec, 5); | |
6316 | vec->quick_push (gimple_call_arg (call, 0)); | |
6317 | tree expected = gimple_call_arg (call, 1); | |
6318 | rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode), | |
6319 | TREE_TYPE (expected)); | |
6320 | rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL); | |
6321 | if (expd != x) | |
6322 | emit_move_insn (x, expd); | |
6323 | tree v = make_tree (TREE_TYPE (expected), x); | |
6324 | vec->quick_push (build1 (ADDR_EXPR, | |
6325 | build_pointer_type (TREE_TYPE (expected)), v)); | |
6326 | vec->quick_push (gimple_call_arg (call, 2)); | |
6327 | /* Skip the boolean weak parameter. */ | |
6328 | for (z = 4; z < 6; z++) | |
6329 | vec->quick_push (gimple_call_arg (call, z)); | |
5eaf31bb | 6330 | /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */ |
52acb7ae | 6331 | unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ()); |
5eaf31bb | 6332 | gcc_assert (bytes_log2 < 5); |
5a5ef659 | 6333 | built_in_function fncode |
6334 | = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1 | |
5eaf31bb | 6335 | + bytes_log2); |
5a5ef659 | 6336 | tree fndecl = builtin_decl_explicit (fncode); |
6337 | tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)), | |
6338 | fndecl); | |
6339 | tree exp = build_call_vec (boolean_type_node, fn, vec); | |
6340 | tree lhs = gimple_call_lhs (call); | |
6341 | rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE); | |
6342 | if (lhs) | |
6343 | { | |
6344 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
6345 | if (GET_MODE (boolret) != mode) | |
6346 | boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1); | |
6347 | x = force_reg (mode, x); | |
6348 | write_complex_part (target, boolret, true); | |
6349 | write_complex_part (target, x, false); | |
6350 | } | |
6351 | } | |
6352 | ||
6353 | /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */ | |
6354 | ||
6355 | void | |
6356 | expand_ifn_atomic_compare_exchange (gcall *call) | |
6357 | { | |
6358 | int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255; | |
6359 | gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16); | |
517be012 | 6360 | machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require (); |
5a5ef659 | 6361 | rtx expect, desired, mem, oldval, boolret; |
6362 | enum memmodel success, failure; | |
6363 | tree lhs; | |
6364 | bool is_weak; | |
be1e7283 | 6365 | location_t loc |
5a5ef659 | 6366 | = expansion_point_location_if_in_system_header (gimple_location (call)); |
6367 | ||
6368 | success = get_memmodel (gimple_call_arg (call, 4)); | |
6369 | failure = get_memmodel (gimple_call_arg (call, 5)); | |
6370 | ||
6371 | if (failure > success) | |
6372 | { | |
6373 | warning_at (loc, OPT_Winvalid_memory_model, | |
6374 | "failure memory model cannot be stronger than success " | |
6375 | "memory model for %<__atomic_compare_exchange%>"); | |
6376 | success = MEMMODEL_SEQ_CST; | |
6377 | } | |
6378 | ||
6379 | if (is_mm_release (failure) || is_mm_acq_rel (failure)) | |
6380 | { | |
6381 | warning_at (loc, OPT_Winvalid_memory_model, | |
6382 | "invalid failure memory model for " | |
6383 | "%<__atomic_compare_exchange%>"); | |
6384 | failure = MEMMODEL_SEQ_CST; | |
6385 | success = MEMMODEL_SEQ_CST; | |
6386 | } | |
6387 | ||
6388 | if (!flag_inline_atomics) | |
6389 | { | |
6390 | expand_ifn_atomic_compare_exchange_into_call (call, mode); | |
6391 | return; | |
6392 | } | |
6393 | ||
6394 | /* Expand the operands. */ | |
6395 | mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode); | |
6396 | ||
6397 | expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode); | |
6398 | desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode); | |
6399 | ||
6400 | is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0; | |
6401 | ||
6402 | boolret = NULL; | |
6403 | oldval = NULL; | |
6404 | ||
6405 | if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired, | |
6406 | is_weak, success, failure)) | |
6407 | { | |
6408 | expand_ifn_atomic_compare_exchange_into_call (call, mode); | |
6409 | return; | |
6410 | } | |
6411 | ||
6412 | lhs = gimple_call_lhs (call); | |
6413 | if (lhs) | |
6414 | { | |
6415 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
6416 | if (GET_MODE (boolret) != mode) | |
6417 | boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1); | |
6418 | write_complex_part (target, boolret, true); | |
6419 | write_complex_part (target, oldval, false); | |
6420 | } | |
6421 | } | |
6422 | ||
1cd6e20d | 6423 | /* Expand the __atomic_load intrinsic: |
6424 | TYPE __atomic_load (TYPE *object, enum memmodel) | |
6425 | EXP is the CALL_EXPR. | |
6426 | TARGET is an optional place for us to store the results. */ | |
6427 | ||
6428 | static rtx | |
3754d046 | 6429 | expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target) |
1cd6e20d | 6430 | { |
6431 | rtx mem; | |
6432 | enum memmodel model; | |
6433 | ||
6434 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
a372f7ca | 6435 | if (is_mm_release (model) || is_mm_acq_rel (model)) |
1cd6e20d | 6436 | { |
be1e7283 | 6437 | location_t loc |
2cb724f9 | 6438 | = expansion_point_location_if_in_system_header (input_location); |
6439 | warning_at (loc, OPT_Winvalid_memory_model, | |
6440 | "invalid memory model for %<__atomic_load%>"); | |
086f4e33 | 6441 | model = MEMMODEL_SEQ_CST; |
1cd6e20d | 6442 | } |
6443 | ||
6444 | if (!flag_inline_atomics) | |
6445 | return NULL_RTX; | |
6446 | ||
6447 | /* Expand the operand. */ | |
6448 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6449 | ||
6450 | return expand_atomic_load (target, mem, model); | |
6451 | } | |
6452 | ||
6453 | ||
6454 | /* Expand the __atomic_store intrinsic: | |
6455 | void __atomic_store (TYPE *object, TYPE desired, enum memmodel) | |
6456 | EXP is the CALL_EXPR. | |
6457 | TARGET is an optional place for us to store the results. */ | |
6458 | ||
6459 | static rtx | |
3754d046 | 6460 | expand_builtin_atomic_store (machine_mode mode, tree exp) |
1cd6e20d | 6461 | { |
6462 | rtx mem, val; | |
6463 | enum memmodel model; | |
6464 | ||
6465 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
a372f7ca | 6466 | if (!(is_mm_relaxed (model) || is_mm_seq_cst (model) |
6467 | || is_mm_release (model))) | |
1cd6e20d | 6468 | { |
be1e7283 | 6469 | location_t loc |
2cb724f9 | 6470 | = expansion_point_location_if_in_system_header (input_location); |
6471 | warning_at (loc, OPT_Winvalid_memory_model, | |
6472 | "invalid memory model for %<__atomic_store%>"); | |
086f4e33 | 6473 | model = MEMMODEL_SEQ_CST; |
1cd6e20d | 6474 | } |
6475 | ||
6476 | if (!flag_inline_atomics) | |
6477 | return NULL_RTX; | |
6478 | ||
6479 | /* Expand the operands. */ | |
6480 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6481 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
6482 | ||
8808bf16 | 6483 | return expand_atomic_store (mem, val, model, false); |
1cd6e20d | 6484 | } |
6485 | ||
6486 | /* Expand the __atomic_fetch_XXX intrinsic: | |
6487 | TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel) | |
6488 | EXP is the CALL_EXPR. | |
6489 | TARGET is an optional place for us to store the results. | |
6490 | CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR. | |
6491 | FETCH_AFTER is true if returning the result of the operation. | |
6492 | FETCH_AFTER is false if returning the value before the operation. | |
6493 | IGNORE is true if the result is not used. | |
6494 | EXT_CALL is the correct builtin for an external call if this cannot be | |
6495 | resolved to an instruction sequence. */ | |
6496 | ||
6497 | static rtx | |
3754d046 | 6498 | expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target, |
1cd6e20d | 6499 | enum rtx_code code, bool fetch_after, |
6500 | bool ignore, enum built_in_function ext_call) | |
6501 | { | |
6502 | rtx val, mem, ret; | |
6503 | enum memmodel model; | |
6504 | tree fndecl; | |
6505 | tree addr; | |
6506 | ||
6507 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
6508 | ||
6509 | /* Expand the operands. */ | |
6510 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
6511 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
6512 | ||
6513 | /* Only try generating instructions if inlining is turned on. */ | |
6514 | if (flag_inline_atomics) | |
6515 | { | |
6516 | ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after); | |
6517 | if (ret) | |
6518 | return ret; | |
6519 | } | |
6520 | ||
6521 | /* Return if a different routine isn't needed for the library call. */ | |
6522 | if (ext_call == BUILT_IN_NONE) | |
6523 | return NULL_RTX; | |
6524 | ||
6525 | /* Change the call to the specified function. */ | |
6526 | fndecl = get_callee_fndecl (exp); | |
6527 | addr = CALL_EXPR_FN (exp); | |
6528 | STRIP_NOPS (addr); | |
6529 | ||
6530 | gcc_assert (TREE_OPERAND (addr, 0) == fndecl); | |
9af5ce0c | 6531 | TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call); |
1cd6e20d | 6532 | |
f4d3c071 | 6533 | /* If we will emit code after the call, the call cannot be a tail call. |
a2f95d97 | 6534 | If it is emitted as a tail call, a barrier is emitted after it, and |
6535 | then all trailing code is removed. */ | |
6536 | if (!ignore) | |
6537 | CALL_EXPR_TAILCALL (exp) = 0; | |
6538 | ||
1cd6e20d | 6539 | /* Expand the call here so we can emit trailing code. */ |
6540 | ret = expand_call (exp, target, ignore); | |
6541 | ||
6542 | /* Replace the original function just in case it matters. */ | |
6543 | TREE_OPERAND (addr, 0) = fndecl; | |
6544 | ||
6545 | /* Then issue the arithmetic correction to return the right result. */ | |
6546 | if (!ignore) | |
c449f851 | 6547 | { |
6548 | if (code == NOT) | |
6549 | { | |
6550 | ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true, | |
6551 | OPTAB_LIB_WIDEN); | |
6552 | ret = expand_simple_unop (mode, NOT, ret, target, true); | |
6553 | } | |
6554 | else | |
6555 | ret = expand_simple_binop (mode, code, ret, val, target, true, | |
6556 | OPTAB_LIB_WIDEN); | |
6557 | } | |
1cd6e20d | 6558 | return ret; |
6559 | } | |
6560 | ||
9c1a31e4 | 6561 | /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */ |
6562 | ||
6563 | void | |
6564 | expand_ifn_atomic_bit_test_and (gcall *call) | |
6565 | { | |
6566 | tree ptr = gimple_call_arg (call, 0); | |
6567 | tree bit = gimple_call_arg (call, 1); | |
6568 | tree flag = gimple_call_arg (call, 2); | |
6569 | tree lhs = gimple_call_lhs (call); | |
6570 | enum memmodel model = MEMMODEL_SYNC_SEQ_CST; | |
6571 | machine_mode mode = TYPE_MODE (TREE_TYPE (flag)); | |
6572 | enum rtx_code code; | |
6573 | optab optab; | |
6574 | struct expand_operand ops[5]; | |
6575 | ||
6576 | gcc_assert (flag_inline_atomics); | |
6577 | ||
6578 | if (gimple_call_num_args (call) == 4) | |
6579 | model = get_memmodel (gimple_call_arg (call, 3)); | |
6580 | ||
6581 | rtx mem = get_builtin_sync_mem (ptr, mode); | |
6582 | rtx val = expand_expr_force_mode (bit, mode); | |
6583 | ||
6584 | switch (gimple_call_internal_fn (call)) | |
6585 | { | |
6586 | case IFN_ATOMIC_BIT_TEST_AND_SET: | |
6587 | code = IOR; | |
6588 | optab = atomic_bit_test_and_set_optab; | |
6589 | break; | |
6590 | case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT: | |
6591 | code = XOR; | |
6592 | optab = atomic_bit_test_and_complement_optab; | |
6593 | break; | |
6594 | case IFN_ATOMIC_BIT_TEST_AND_RESET: | |
6595 | code = AND; | |
6596 | optab = atomic_bit_test_and_reset_optab; | |
6597 | break; | |
6598 | default: | |
6599 | gcc_unreachable (); | |
6600 | } | |
6601 | ||
6602 | if (lhs == NULL_TREE) | |
6603 | { | |
6604 | val = expand_simple_binop (mode, ASHIFT, const1_rtx, | |
6605 | val, NULL_RTX, true, OPTAB_DIRECT); | |
6606 | if (code == AND) | |
6607 | val = expand_simple_unop (mode, NOT, val, NULL_RTX, true); | |
6608 | expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false); | |
6609 | return; | |
6610 | } | |
6611 | ||
6612 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
6613 | enum insn_code icode = direct_optab_handler (optab, mode); | |
6614 | gcc_assert (icode != CODE_FOR_nothing); | |
6615 | create_output_operand (&ops[0], target, mode); | |
6616 | create_fixed_operand (&ops[1], mem); | |
6617 | create_convert_operand_to (&ops[2], val, mode, true); | |
6618 | create_integer_operand (&ops[3], model); | |
6619 | create_integer_operand (&ops[4], integer_onep (flag)); | |
6620 | if (maybe_expand_insn (icode, 5, ops)) | |
6621 | return; | |
6622 | ||
6623 | rtx bitval = val; | |
6624 | val = expand_simple_binop (mode, ASHIFT, const1_rtx, | |
6625 | val, NULL_RTX, true, OPTAB_DIRECT); | |
6626 | rtx maskval = val; | |
6627 | if (code == AND) | |
6628 | val = expand_simple_unop (mode, NOT, val, NULL_RTX, true); | |
6629 | rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val, | |
6630 | code, model, false); | |
6631 | if (integer_onep (flag)) | |
6632 | { | |
6633 | result = expand_simple_binop (mode, ASHIFTRT, result, bitval, | |
6634 | NULL_RTX, true, OPTAB_DIRECT); | |
6635 | result = expand_simple_binop (mode, AND, result, const1_rtx, target, | |
6636 | true, OPTAB_DIRECT); | |
6637 | } | |
6638 | else | |
6639 | result = expand_simple_binop (mode, AND, result, maskval, target, true, | |
6640 | OPTAB_DIRECT); | |
6641 | if (result != target) | |
6642 | emit_move_insn (target, result); | |
6643 | } | |
6644 | ||
10b744a3 | 6645 | /* Expand an atomic clear operation. |
6646 | void _atomic_clear (BOOL *obj, enum memmodel) | |
6647 | EXP is the call expression. */ | |
6648 | ||
6649 | static rtx | |
6650 | expand_builtin_atomic_clear (tree exp) | |
6651 | { | |
3754d046 | 6652 | machine_mode mode; |
10b744a3 | 6653 | rtx mem, ret; |
6654 | enum memmodel model; | |
6655 | ||
517be012 | 6656 | mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require (); |
10b744a3 | 6657 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
6658 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
6659 | ||
a372f7ca | 6660 | if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model)) |
10b744a3 | 6661 | { |
be1e7283 | 6662 | location_t loc |
2cb724f9 | 6663 | = expansion_point_location_if_in_system_header (input_location); |
6664 | warning_at (loc, OPT_Winvalid_memory_model, | |
6665 | "invalid memory model for %<__atomic_store%>"); | |
086f4e33 | 6666 | model = MEMMODEL_SEQ_CST; |
10b744a3 | 6667 | } |
6668 | ||
6669 | /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release. | |
6670 | Failing that, a store is issued by __atomic_store. The only way this can | |
6671 | fail is if the bool type is larger than a word size. Unlikely, but | |
6672 | handle it anyway for completeness. Assume a single threaded model since | |
6673 | there is no atomic support in this case, and no barriers are required. */ | |
6674 | ret = expand_atomic_store (mem, const0_rtx, model, true); | |
6675 | if (!ret) | |
6676 | emit_move_insn (mem, const0_rtx); | |
6677 | return const0_rtx; | |
6678 | } | |
6679 | ||
6680 | /* Expand an atomic test_and_set operation. | |
6681 | bool _atomic_test_and_set (BOOL *obj, enum memmodel) | |
6682 | EXP is the call expression. */ | |
6683 | ||
6684 | static rtx | |
7821cde1 | 6685 | expand_builtin_atomic_test_and_set (tree exp, rtx target) |
10b744a3 | 6686 | { |
7821cde1 | 6687 | rtx mem; |
10b744a3 | 6688 | enum memmodel model; |
3754d046 | 6689 | machine_mode mode; |
10b744a3 | 6690 | |
517be012 | 6691 | mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require (); |
10b744a3 | 6692 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
6693 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
6694 | ||
7821cde1 | 6695 | return expand_atomic_test_and_set (target, mem, model); |
10b744a3 | 6696 | } |
6697 | ||
6698 | ||
1cd6e20d | 6699 | /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on |
6700 | this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */ | |
6701 | ||
6702 | static tree | |
6703 | fold_builtin_atomic_always_lock_free (tree arg0, tree arg1) | |
6704 | { | |
6705 | int size; | |
3754d046 | 6706 | machine_mode mode; |
1cd6e20d | 6707 | unsigned int mode_align, type_align; |
6708 | ||
6709 | if (TREE_CODE (arg0) != INTEGER_CST) | |
6710 | return NULL_TREE; | |
b6a5fc45 | 6711 | |
517be012 | 6712 | /* We need a corresponding integer mode for the access to be lock-free. */ |
1cd6e20d | 6713 | size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT; |
517be012 | 6714 | if (!int_mode_for_size (size, 0).exists (&mode)) |
6715 | return boolean_false_node; | |
6716 | ||
1cd6e20d | 6717 | mode_align = GET_MODE_ALIGNMENT (mode); |
6718 | ||
4ca99588 | 6719 | if (TREE_CODE (arg1) == INTEGER_CST) |
6720 | { | |
6721 | unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1)); | |
6722 | ||
6723 | /* Either this argument is null, or it's a fake pointer encoding | |
6724 | the alignment of the object. */ | |
ac29ece2 | 6725 | val = least_bit_hwi (val); |
4ca99588 | 6726 | val *= BITS_PER_UNIT; |
6727 | ||
6728 | if (val == 0 || mode_align < val) | |
6729 | type_align = mode_align; | |
6730 | else | |
6731 | type_align = val; | |
6732 | } | |
1cd6e20d | 6733 | else |
6734 | { | |
6735 | tree ttype = TREE_TYPE (arg1); | |
6736 | ||
6737 | /* This function is usually invoked and folded immediately by the front | |
6738 | end before anything else has a chance to look at it. The pointer | |
6739 | parameter at this point is usually cast to a void *, so check for that | |
6740 | and look past the cast. */ | |
2f8a2ead | 6741 | if (CONVERT_EXPR_P (arg1) |
6742 | && POINTER_TYPE_P (ttype) | |
6743 | && VOID_TYPE_P (TREE_TYPE (ttype)) | |
6744 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))) | |
1cd6e20d | 6745 | arg1 = TREE_OPERAND (arg1, 0); |
6746 | ||
6747 | ttype = TREE_TYPE (arg1); | |
6748 | gcc_assert (POINTER_TYPE_P (ttype)); | |
6749 | ||
6750 | /* Get the underlying type of the object. */ | |
6751 | ttype = TREE_TYPE (ttype); | |
6752 | type_align = TYPE_ALIGN (ttype); | |
6753 | } | |
6754 | ||
47ae02b7 | 6755 | /* If the object has smaller alignment, the lock free routines cannot |
1cd6e20d | 6756 | be used. */ |
6757 | if (type_align < mode_align) | |
06308d2a | 6758 | return boolean_false_node; |
1cd6e20d | 6759 | |
6760 | /* Check if a compare_and_swap pattern exists for the mode which represents | |
6761 | the required size. The pattern is not allowed to fail, so the existence | |
d5f5fa27 | 6762 | of the pattern indicates support is present. Also require that an |
6763 | atomic load exists for the required size. */ | |
6764 | if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode)) | |
06308d2a | 6765 | return boolean_true_node; |
1cd6e20d | 6766 | else |
06308d2a | 6767 | return boolean_false_node; |
1cd6e20d | 6768 | } |
6769 | ||
6770 | /* Return true if the parameters to call EXP represent an object which will | |
6771 | always generate lock free instructions. The first argument represents the | |
6772 | size of the object, and the second parameter is a pointer to the object | |
6773 | itself. If NULL is passed for the object, then the result is based on | |
6774 | typical alignment for an object of the specified size. Otherwise return | |
6775 | false. */ | |
6776 | ||
6777 | static rtx | |
6778 | expand_builtin_atomic_always_lock_free (tree exp) | |
6779 | { | |
6780 | tree size; | |
6781 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
6782 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
6783 | ||
6784 | if (TREE_CODE (arg0) != INTEGER_CST) | |
6785 | { | |
6786 | error ("non-constant argument 1 to __atomic_always_lock_free"); | |
6787 | return const0_rtx; | |
6788 | } | |
6789 | ||
6790 | size = fold_builtin_atomic_always_lock_free (arg0, arg1); | |
06308d2a | 6791 | if (size == boolean_true_node) |
1cd6e20d | 6792 | return const1_rtx; |
6793 | return const0_rtx; | |
6794 | } | |
6795 | ||
6796 | /* Return a one or zero if it can be determined that object ARG1 of size ARG | |
6797 | is lock free on this architecture. */ | |
6798 | ||
6799 | static tree | |
6800 | fold_builtin_atomic_is_lock_free (tree arg0, tree arg1) | |
6801 | { | |
6802 | if (!flag_inline_atomics) | |
6803 | return NULL_TREE; | |
6804 | ||
6805 | /* If it isn't always lock free, don't generate a result. */ | |
06308d2a | 6806 | if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node) |
6807 | return boolean_true_node; | |
1cd6e20d | 6808 | |
6809 | return NULL_TREE; | |
6810 | } | |
6811 | ||
6812 | /* Return true if the parameters to call EXP represent an object which will | |
6813 | always generate lock free instructions. The first argument represents the | |
6814 | size of the object, and the second parameter is a pointer to the object | |
6815 | itself. If NULL is passed for the object, then the result is based on | |
6816 | typical alignment for an object of the specified size. Otherwise return | |
6817 | NULL*/ | |
6818 | ||
6819 | static rtx | |
6820 | expand_builtin_atomic_is_lock_free (tree exp) | |
6821 | { | |
6822 | tree size; | |
6823 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
6824 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
6825 | ||
6826 | if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
6827 | { | |
6828 | error ("non-integer argument 1 to __atomic_is_lock_free"); | |
6829 | return NULL_RTX; | |
6830 | } | |
6831 | ||
6832 | if (!flag_inline_atomics) | |
6833 | return NULL_RTX; | |
6834 | ||
6835 | /* If the value is known at compile time, return the RTX for it. */ | |
6836 | size = fold_builtin_atomic_is_lock_free (arg0, arg1); | |
06308d2a | 6837 | if (size == boolean_true_node) |
1cd6e20d | 6838 | return const1_rtx; |
6839 | ||
6840 | return NULL_RTX; | |
6841 | } | |
6842 | ||
1cd6e20d | 6843 | /* Expand the __atomic_thread_fence intrinsic: |
6844 | void __atomic_thread_fence (enum memmodel) | |
6845 | EXP is the CALL_EXPR. */ | |
6846 | ||
6847 | static void | |
6848 | expand_builtin_atomic_thread_fence (tree exp) | |
6849 | { | |
fe54c06b | 6850 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
6851 | expand_mem_thread_fence (model); | |
1cd6e20d | 6852 | } |
6853 | ||
6854 | /* Expand the __atomic_signal_fence intrinsic: | |
6855 | void __atomic_signal_fence (enum memmodel) | |
6856 | EXP is the CALL_EXPR. */ | |
6857 | ||
6858 | static void | |
6859 | expand_builtin_atomic_signal_fence (tree exp) | |
6860 | { | |
fe54c06b | 6861 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
6862 | expand_mem_signal_fence (model); | |
b6a5fc45 | 6863 | } |
6864 | ||
6865 | /* Expand the __sync_synchronize intrinsic. */ | |
6866 | ||
6867 | static void | |
2797f13a | 6868 | expand_builtin_sync_synchronize (void) |
b6a5fc45 | 6869 | { |
a372f7ca | 6870 | expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST); |
b6a5fc45 | 6871 | } |
6872 | ||
badaa04c | 6873 | static rtx |
6874 | expand_builtin_thread_pointer (tree exp, rtx target) | |
6875 | { | |
6876 | enum insn_code icode; | |
6877 | if (!validate_arglist (exp, VOID_TYPE)) | |
6878 | return const0_rtx; | |
6879 | icode = direct_optab_handler (get_thread_pointer_optab, Pmode); | |
6880 | if (icode != CODE_FOR_nothing) | |
6881 | { | |
6882 | struct expand_operand op; | |
3ed779c3 | 6883 | /* If the target is not sutitable then create a new target. */ |
6884 | if (target == NULL_RTX | |
6885 | || !REG_P (target) | |
6886 | || GET_MODE (target) != Pmode) | |
badaa04c | 6887 | target = gen_reg_rtx (Pmode); |
6888 | create_output_operand (&op, target, Pmode); | |
6889 | expand_insn (icode, 1, &op); | |
6890 | return target; | |
6891 | } | |
6892 | error ("__builtin_thread_pointer is not supported on this target"); | |
6893 | return const0_rtx; | |
6894 | } | |
6895 | ||
6896 | static void | |
6897 | expand_builtin_set_thread_pointer (tree exp) | |
6898 | { | |
6899 | enum insn_code icode; | |
6900 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) | |
6901 | return; | |
6902 | icode = direct_optab_handler (set_thread_pointer_optab, Pmode); | |
6903 | if (icode != CODE_FOR_nothing) | |
6904 | { | |
6905 | struct expand_operand op; | |
6906 | rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, | |
6907 | Pmode, EXPAND_NORMAL); | |
6f343c10 | 6908 | create_input_operand (&op, val, Pmode); |
badaa04c | 6909 | expand_insn (icode, 1, &op); |
6910 | return; | |
6911 | } | |
6912 | error ("__builtin_set_thread_pointer is not supported on this target"); | |
6913 | } | |
6914 | ||
53800dbe | 6915 | \f |
0e80b01d | 6916 | /* Emit code to restore the current value of stack. */ |
6917 | ||
6918 | static void | |
6919 | expand_stack_restore (tree var) | |
6920 | { | |
1e0c0b35 | 6921 | rtx_insn *prev; |
6922 | rtx sa = expand_normal (var); | |
0e80b01d | 6923 | |
6924 | sa = convert_memory_address (Pmode, sa); | |
6925 | ||
6926 | prev = get_last_insn (); | |
6927 | emit_stack_restore (SAVE_BLOCK, sa); | |
97354ae4 | 6928 | |
6929 | record_new_stack_level (); | |
6930 | ||
0e80b01d | 6931 | fixup_args_size_notes (prev, get_last_insn (), 0); |
6932 | } | |
6933 | ||
0e80b01d | 6934 | /* Emit code to save the current value of stack. */ |
6935 | ||
6936 | static rtx | |
6937 | expand_stack_save (void) | |
6938 | { | |
6939 | rtx ret = NULL_RTX; | |
6940 | ||
0e80b01d | 6941 | emit_stack_save (SAVE_BLOCK, &ret); |
6942 | return ret; | |
6943 | } | |
6944 | ||
a7babc1e | 6945 | /* Emit code to get the openacc gang, worker or vector id or size. */ |
6946 | ||
6947 | static rtx | |
6948 | expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore) | |
6949 | { | |
6950 | const char *name; | |
6951 | rtx fallback_retval; | |
6952 | rtx_insn *(*gen_fn) (rtx, rtx); | |
6953 | switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp))) | |
6954 | { | |
6955 | case BUILT_IN_GOACC_PARLEVEL_ID: | |
6956 | name = "__builtin_goacc_parlevel_id"; | |
6957 | fallback_retval = const0_rtx; | |
6958 | gen_fn = targetm.gen_oacc_dim_pos; | |
6959 | break; | |
6960 | case BUILT_IN_GOACC_PARLEVEL_SIZE: | |
6961 | name = "__builtin_goacc_parlevel_size"; | |
6962 | fallback_retval = const1_rtx; | |
6963 | gen_fn = targetm.gen_oacc_dim_size; | |
6964 | break; | |
6965 | default: | |
6966 | gcc_unreachable (); | |
6967 | } | |
6968 | ||
6969 | if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE) | |
6970 | { | |
6971 | error ("%qs only supported in OpenACC code", name); | |
6972 | return const0_rtx; | |
6973 | } | |
6974 | ||
6975 | tree arg = CALL_EXPR_ARG (exp, 0); | |
6976 | if (TREE_CODE (arg) != INTEGER_CST) | |
6977 | { | |
6978 | error ("non-constant argument 0 to %qs", name); | |
6979 | return const0_rtx; | |
6980 | } | |
6981 | ||
6982 | int dim = TREE_INT_CST_LOW (arg); | |
6983 | switch (dim) | |
6984 | { | |
6985 | case GOMP_DIM_GANG: | |
6986 | case GOMP_DIM_WORKER: | |
6987 | case GOMP_DIM_VECTOR: | |
6988 | break; | |
6989 | default: | |
6990 | error ("illegal argument 0 to %qs", name); | |
6991 | return const0_rtx; | |
6992 | } | |
6993 | ||
6994 | if (ignore) | |
6995 | return target; | |
6996 | ||
2b895374 | 6997 | if (target == NULL_RTX) |
6998 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); | |
6999 | ||
a7babc1e | 7000 | if (!targetm.have_oacc_dim_size ()) |
7001 | { | |
7002 | emit_move_insn (target, fallback_retval); | |
7003 | return target; | |
7004 | } | |
7005 | ||
7006 | rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target; | |
7007 | emit_insn (gen_fn (reg, GEN_INT (dim))); | |
7008 | if (reg != target) | |
7009 | emit_move_insn (target, reg); | |
7010 | ||
7011 | return target; | |
7012 | } | |
ca4c3545 | 7013 | |
b3e6ae76 | 7014 | /* Expand a string compare operation using a sequence of char comparison |
a950155e | 7015 | to get rid of the calling overhead, with result going to TARGET if |
7016 | that's convenient. | |
7017 | ||
7018 | VAR_STR is the variable string source; | |
7019 | CONST_STR is the constant string source; | |
7020 | LENGTH is the number of chars to compare; | |
7021 | CONST_STR_N indicates which source string is the constant string; | |
7022 | IS_MEMCMP indicates whether it's a memcmp or strcmp. | |
b3e6ae76 | 7023 | |
a950155e | 7024 | to: (assume const_str_n is 2, i.e., arg2 is a constant string) |
7025 | ||
0dbefa15 | 7026 | target = (int) (unsigned char) var_str[0] |
7027 | - (int) (unsigned char) const_str[0]; | |
a950155e | 7028 | if (target != 0) |
7029 | goto ne_label; | |
7030 | ... | |
0dbefa15 | 7031 | target = (int) (unsigned char) var_str[length - 2] |
7032 | - (int) (unsigned char) const_str[length - 2]; | |
a950155e | 7033 | if (target != 0) |
7034 | goto ne_label; | |
0dbefa15 | 7035 | target = (int) (unsigned char) var_str[length - 1] |
7036 | - (int) (unsigned char) const_str[length - 1]; | |
a950155e | 7037 | ne_label: |
7038 | */ | |
7039 | ||
7040 | static rtx | |
b3e6ae76 | 7041 | inline_string_cmp (rtx target, tree var_str, const char *const_str, |
a950155e | 7042 | unsigned HOST_WIDE_INT length, |
0dbefa15 | 7043 | int const_str_n, machine_mode mode) |
a950155e | 7044 | { |
7045 | HOST_WIDE_INT offset = 0; | |
b3e6ae76 | 7046 | rtx var_rtx_array |
a950155e | 7047 | = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length)); |
7048 | rtx var_rtx = NULL_RTX; | |
b3e6ae76 | 7049 | rtx const_rtx = NULL_RTX; |
7050 | rtx result = target ? target : gen_reg_rtx (mode); | |
7051 | rtx_code_label *ne_label = gen_label_rtx (); | |
0dbefa15 | 7052 | tree unit_type_node = unsigned_char_type_node; |
b3e6ae76 | 7053 | scalar_int_mode unit_mode |
7054 | = as_a <scalar_int_mode> TYPE_MODE (unit_type_node); | |
a950155e | 7055 | |
7056 | start_sequence (); | |
7057 | ||
7058 | for (unsigned HOST_WIDE_INT i = 0; i < length; i++) | |
7059 | { | |
b3e6ae76 | 7060 | var_rtx |
a950155e | 7061 | = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset); |
b3e6ae76 | 7062 | const_rtx = c_readstr (const_str + offset, unit_mode); |
a950155e | 7063 | rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx; |
7064 | rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx; | |
b3e6ae76 | 7065 | |
0dbefa15 | 7066 | op0 = convert_modes (mode, unit_mode, op0, 1); |
7067 | op1 = convert_modes (mode, unit_mode, op1, 1); | |
b3e6ae76 | 7068 | result = expand_simple_binop (mode, MINUS, op0, op1, |
0dbefa15 | 7069 | result, 1, OPTAB_WIDEN); |
b3e6ae76 | 7070 | if (i < length - 1) |
7071 | emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX, | |
7072 | mode, true, ne_label); | |
7073 | offset += GET_MODE_SIZE (unit_mode); | |
a950155e | 7074 | } |
7075 | ||
7076 | emit_label (ne_label); | |
7077 | rtx_insn *insns = get_insns (); | |
7078 | end_sequence (); | |
7079 | emit_insn (insns); | |
7080 | ||
7081 | return result; | |
7082 | } | |
7083 | ||
b3e6ae76 | 7084 | /* Inline expansion a call to str(n)cmp, with result going to |
a950155e | 7085 | TARGET if that's convenient. |
7086 | If the call is not been inlined, return NULL_RTX. */ | |
7087 | static rtx | |
0dbefa15 | 7088 | inline_expand_builtin_string_cmp (tree exp, rtx target) |
a950155e | 7089 | { |
7090 | tree fndecl = get_callee_fndecl (exp); | |
7091 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
7092 | unsigned HOST_WIDE_INT length = 0; | |
7093 | bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP); | |
7094 | ||
9c7661c8 | 7095 | /* Do NOT apply this inlining expansion when optimizing for size or |
7096 | optimization level below 2. */ | |
7097 | if (optimize < 2 || optimize_insn_for_size_p ()) | |
7098 | return NULL_RTX; | |
7099 | ||
a950155e | 7100 | gcc_checking_assert (fcode == BUILT_IN_STRCMP |
b3e6ae76 | 7101 | || fcode == BUILT_IN_STRNCMP |
a950155e | 7102 | || fcode == BUILT_IN_MEMCMP); |
7103 | ||
0dbefa15 | 7104 | /* On a target where the type of the call (int) has same or narrower presicion |
7105 | than unsigned char, give up the inlining expansion. */ | |
7106 | if (TYPE_PRECISION (unsigned_char_type_node) | |
7107 | >= TYPE_PRECISION (TREE_TYPE (exp))) | |
7108 | return NULL_RTX; | |
7109 | ||
a950155e | 7110 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
7111 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
7112 | tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE; | |
7113 | ||
7114 | unsigned HOST_WIDE_INT len1 = 0; | |
7115 | unsigned HOST_WIDE_INT len2 = 0; | |
7116 | unsigned HOST_WIDE_INT len3 = 0; | |
7117 | ||
7118 | const char *src_str1 = c_getstr (arg1, &len1); | |
7119 | const char *src_str2 = c_getstr (arg2, &len2); | |
b3e6ae76 | 7120 | |
a950155e | 7121 | /* If neither strings is constant string, the call is not qualify. */ |
7122 | if (!src_str1 && !src_str2) | |
7123 | return NULL_RTX; | |
7124 | ||
7125 | /* For strncmp, if the length is not a const, not qualify. */ | |
7126 | if (is_ncmp && !tree_fits_uhwi_p (len3_tree)) | |
7127 | return NULL_RTX; | |
7128 | ||
7129 | int const_str_n = 0; | |
7130 | if (!len1) | |
7131 | const_str_n = 2; | |
7132 | else if (!len2) | |
7133 | const_str_n = 1; | |
7134 | else if (len2 > len1) | |
7135 | const_str_n = 1; | |
7136 | else | |
7137 | const_str_n = 2; | |
7138 | ||
7139 | gcc_checking_assert (const_str_n > 0); | |
7140 | length = (const_str_n == 1) ? len1 : len2; | |
7141 | ||
7142 | if (is_ncmp && (len3 = tree_to_uhwi (len3_tree)) < length) | |
7143 | length = len3; | |
7144 | ||
b3e6ae76 | 7145 | /* If the length of the comparision is larger than the threshold, |
a950155e | 7146 | do nothing. */ |
b3e6ae76 | 7147 | if (length > (unsigned HOST_WIDE_INT) |
a950155e | 7148 | PARAM_VALUE (BUILTIN_STRING_CMP_INLINE_LENGTH)) |
7149 | return NULL_RTX; | |
7150 | ||
7151 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); | |
7152 | ||
7153 | /* Now, start inline expansion the call. */ | |
b3e6ae76 | 7154 | return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1, |
a950155e | 7155 | (const_str_n == 1) ? src_str1 : src_str2, length, |
0dbefa15 | 7156 | const_str_n, mode); |
a950155e | 7157 | } |
7158 | ||
123081ef | 7159 | /* Expand a call to __builtin_speculation_safe_value_<N>. MODE |
7160 | represents the size of the first argument to that call, or VOIDmode | |
7161 | if the argument is a pointer. IGNORE will be true if the result | |
7162 | isn't used. */ | |
7163 | static rtx | |
7164 | expand_speculation_safe_value (machine_mode mode, tree exp, rtx target, | |
7165 | bool ignore) | |
7166 | { | |
7167 | rtx val, failsafe; | |
7168 | unsigned nargs = call_expr_nargs (exp); | |
7169 | ||
7170 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
7171 | ||
7172 | if (mode == VOIDmode) | |
7173 | { | |
7174 | mode = TYPE_MODE (TREE_TYPE (arg0)); | |
7175 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); | |
7176 | } | |
7177 | ||
7178 | val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL); | |
7179 | ||
7180 | /* An optional second argument can be used as a failsafe value on | |
7181 | some machines. If it isn't present, then the failsafe value is | |
7182 | assumed to be 0. */ | |
7183 | if (nargs > 1) | |
7184 | { | |
7185 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
7186 | failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL); | |
7187 | } | |
7188 | else | |
7189 | failsafe = const0_rtx; | |
7190 | ||
7191 | /* If the result isn't used, the behavior is undefined. It would be | |
7192 | nice to emit a warning here, but path splitting means this might | |
7193 | happen with legitimate code. So simply drop the builtin | |
7194 | expansion in that case; we've handled any side-effects above. */ | |
7195 | if (ignore) | |
7196 | return const0_rtx; | |
7197 | ||
7198 | /* If we don't have a suitable target, create one to hold the result. */ | |
7199 | if (target == NULL || GET_MODE (target) != mode) | |
7200 | target = gen_reg_rtx (mode); | |
7201 | ||
7202 | if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode) | |
7203 | val = convert_modes (mode, VOIDmode, val, false); | |
7204 | ||
7205 | return targetm.speculation_safe_value (mode, target, val, failsafe); | |
7206 | } | |
7207 | ||
53800dbe | 7208 | /* Expand an expression EXP that calls a built-in function, |
7209 | with result going to TARGET if that's convenient | |
7210 | (and in mode MODE if that's convenient). | |
7211 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
7212 | IGNORE is nonzero if the value is to be ignored. */ | |
7213 | ||
7214 | rtx | |
3754d046 | 7215 | expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode, |
aecda0d6 | 7216 | int ignore) |
53800dbe | 7217 | { |
c6e6ecb1 | 7218 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 7219 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
3754d046 | 7220 | machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
67fa4078 | 7221 | int flags; |
53800dbe | 7222 | |
4e2f4ed5 | 7223 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
7224 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); | |
7225 | ||
f9acf11a | 7226 | /* When ASan is enabled, we don't want to expand some memory/string |
7227 | builtins and rely on libsanitizer's hooks. This allows us to avoid | |
7228 | redundant checks and be sure, that possible overflow will be detected | |
7229 | by ASan. */ | |
7230 | ||
7231 | if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode)) | |
7232 | return expand_call (exp, target, ignore); | |
7233 | ||
53800dbe | 7234 | /* When not optimizing, generate calls to library functions for a certain |
7235 | set of builtins. */ | |
cd9ff771 | 7236 | if (!optimize |
b6a5fc45 | 7237 | && !called_as_built_in (fndecl) |
73037a1e | 7238 | && fcode != BUILT_IN_FORK |
7239 | && fcode != BUILT_IN_EXECL | |
7240 | && fcode != BUILT_IN_EXECV | |
7241 | && fcode != BUILT_IN_EXECLP | |
7242 | && fcode != BUILT_IN_EXECLE | |
7243 | && fcode != BUILT_IN_EXECVP | |
7244 | && fcode != BUILT_IN_EXECVE | |
2b34677f | 7245 | && !ALLOCA_FUNCTION_CODE_P (fcode) |
1e42d5c6 | 7246 | && fcode != BUILT_IN_FREE) |
cd9ff771 | 7247 | return expand_call (exp, target, ignore); |
53800dbe | 7248 | |
8d6d7930 | 7249 | /* The built-in function expanders test for target == const0_rtx |
7250 | to determine whether the function's result will be ignored. */ | |
7251 | if (ignore) | |
7252 | target = const0_rtx; | |
7253 | ||
7254 | /* If the result of a pure or const built-in function is ignored, and | |
7255 | none of its arguments are volatile, we can avoid expanding the | |
7256 | built-in call and just evaluate the arguments for side-effects. */ | |
7257 | if (target == const0_rtx | |
67fa4078 | 7258 | && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE)) |
7259 | && !(flags & ECF_LOOPING_CONST_OR_PURE)) | |
8d6d7930 | 7260 | { |
7261 | bool volatilep = false; | |
7262 | tree arg; | |
c2f47e15 | 7263 | call_expr_arg_iterator iter; |
8d6d7930 | 7264 | |
c2f47e15 | 7265 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
7266 | if (TREE_THIS_VOLATILE (arg)) | |
8d6d7930 | 7267 | { |
7268 | volatilep = true; | |
7269 | break; | |
7270 | } | |
7271 | ||
7272 | if (! volatilep) | |
7273 | { | |
c2f47e15 | 7274 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
7275 | expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
8d6d7930 | 7276 | return const0_rtx; |
7277 | } | |
7278 | } | |
7279 | ||
53800dbe | 7280 | switch (fcode) |
7281 | { | |
4f35b1fc | 7282 | CASE_FLT_FN (BUILT_IN_FABS): |
012f068a | 7283 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS): |
8aa32773 | 7284 | case BUILT_IN_FABSD32: |
7285 | case BUILT_IN_FABSD64: | |
7286 | case BUILT_IN_FABSD128: | |
c2f47e15 | 7287 | target = expand_builtin_fabs (exp, target, subtarget); |
78a74442 | 7288 | if (target) |
a0c938f0 | 7289 | return target; |
78a74442 | 7290 | break; |
7291 | ||
4f35b1fc | 7292 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
012f068a | 7293 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN): |
c2f47e15 | 7294 | target = expand_builtin_copysign (exp, target, subtarget); |
270436f3 | 7295 | if (target) |
7296 | return target; | |
7297 | break; | |
7298 | ||
7d3f6cc7 | 7299 | /* Just do a normal library call if we were unable to fold |
7300 | the values. */ | |
4f35b1fc | 7301 | CASE_FLT_FN (BUILT_IN_CABS): |
78a74442 | 7302 | break; |
53800dbe | 7303 | |
7e0713b1 | 7304 | CASE_FLT_FN (BUILT_IN_FMA): |
8c32188e | 7305 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA): |
7e0713b1 | 7306 | target = expand_builtin_mathfn_ternary (exp, target, subtarget); |
7307 | if (target) | |
7308 | return target; | |
7309 | break; | |
7310 | ||
a67a90e5 | 7311 | CASE_FLT_FN (BUILT_IN_ILOGB): |
7312 | if (! flag_unsafe_math_optimizations) | |
7313 | break; | |
12f08300 | 7314 | gcc_fallthrough (); |
7315 | CASE_FLT_FN (BUILT_IN_ISINF): | |
7316 | CASE_FLT_FN (BUILT_IN_FINITE): | |
7317 | case BUILT_IN_ISFINITE: | |
7318 | case BUILT_IN_ISNORMAL: | |
f97eea22 | 7319 | target = expand_builtin_interclass_mathfn (exp, target); |
a67a90e5 | 7320 | if (target) |
7321 | return target; | |
7322 | break; | |
7323 | ||
80ff6494 | 7324 | CASE_FLT_FN (BUILT_IN_ICEIL): |
4f35b1fc | 7325 | CASE_FLT_FN (BUILT_IN_LCEIL): |
7326 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
7327 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
80ff6494 | 7328 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
4f35b1fc | 7329 | CASE_FLT_FN (BUILT_IN_LLFLOOR): |
ff1b14e4 | 7330 | target = expand_builtin_int_roundingfn (exp, target); |
ad52b9b7 | 7331 | if (target) |
7332 | return target; | |
7333 | break; | |
7334 | ||
80ff6494 | 7335 | CASE_FLT_FN (BUILT_IN_IRINT): |
7d3afc77 | 7336 | CASE_FLT_FN (BUILT_IN_LRINT): |
7337 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
80ff6494 | 7338 | CASE_FLT_FN (BUILT_IN_IROUND): |
ef2f1a10 | 7339 | CASE_FLT_FN (BUILT_IN_LROUND): |
7340 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
ff1b14e4 | 7341 | target = expand_builtin_int_roundingfn_2 (exp, target); |
7d3afc77 | 7342 | if (target) |
7343 | return target; | |
7344 | break; | |
7345 | ||
4f35b1fc | 7346 | CASE_FLT_FN (BUILT_IN_POWI): |
f97eea22 | 7347 | target = expand_builtin_powi (exp, target); |
757c219d | 7348 | if (target) |
7349 | return target; | |
7350 | break; | |
7351 | ||
d735c391 | 7352 | CASE_FLT_FN (BUILT_IN_CEXPI): |
f97eea22 | 7353 | target = expand_builtin_cexpi (exp, target); |
d735c391 | 7354 | gcc_assert (target); |
7355 | return target; | |
7356 | ||
4f35b1fc | 7357 | CASE_FLT_FN (BUILT_IN_SIN): |
7358 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 7359 | if (! flag_unsafe_math_optimizations) |
7360 | break; | |
7361 | target = expand_builtin_mathfn_3 (exp, target, subtarget); | |
7362 | if (target) | |
7363 | return target; | |
7364 | break; | |
7365 | ||
c3147c1a | 7366 | CASE_FLT_FN (BUILT_IN_SINCOS): |
7367 | if (! flag_unsafe_math_optimizations) | |
7368 | break; | |
7369 | target = expand_builtin_sincos (exp); | |
7370 | if (target) | |
7371 | return target; | |
7372 | break; | |
7373 | ||
53800dbe | 7374 | case BUILT_IN_APPLY_ARGS: |
7375 | return expand_builtin_apply_args (); | |
7376 | ||
7377 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
7378 | FUNCTION with a copy of the parameters described by | |
7379 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
7380 | allocated on the stack into which is stored all the registers | |
7381 | that might possibly be used for returning the result of a | |
7382 | function. ARGUMENTS is the value returned by | |
7383 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
7384 | arguments that must be copied. ??? How should this value be | |
7385 | computed? We'll also need a safe worst case value for varargs | |
7386 | functions. */ | |
7387 | case BUILT_IN_APPLY: | |
c2f47e15 | 7388 | if (!validate_arglist (exp, POINTER_TYPE, |
0eb671f7 | 7389 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) |
c2f47e15 | 7390 | && !validate_arglist (exp, REFERENCE_TYPE, |
0eb671f7 | 7391 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 7392 | return const0_rtx; |
7393 | else | |
7394 | { | |
53800dbe | 7395 | rtx ops[3]; |
7396 | ||
c2f47e15 | 7397 | ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0)); |
7398 | ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
7399 | ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2)); | |
53800dbe | 7400 | |
7401 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
7402 | } | |
7403 | ||
7404 | /* __builtin_return (RESULT) causes the function to return the | |
7405 | value described by RESULT. RESULT is address of the block of | |
7406 | memory returned by __builtin_apply. */ | |
7407 | case BUILT_IN_RETURN: | |
c2f47e15 | 7408 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
7409 | expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0))); | |
53800dbe | 7410 | return const0_rtx; |
7411 | ||
7412 | case BUILT_IN_SAVEREGS: | |
a66c9326 | 7413 | return expand_builtin_saveregs (); |
53800dbe | 7414 | |
48dc2227 | 7415 | case BUILT_IN_VA_ARG_PACK: |
7416 | /* All valid uses of __builtin_va_arg_pack () are removed during | |
7417 | inlining. */ | |
b8c23db3 | 7418 | error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp); |
48dc2227 | 7419 | return const0_rtx; |
7420 | ||
4e1d7ea4 | 7421 | case BUILT_IN_VA_ARG_PACK_LEN: |
7422 | /* All valid uses of __builtin_va_arg_pack_len () are removed during | |
7423 | inlining. */ | |
b8c23db3 | 7424 | error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp); |
4e1d7ea4 | 7425 | return const0_rtx; |
7426 | ||
53800dbe | 7427 | /* Return the address of the first anonymous stack arg. */ |
7428 | case BUILT_IN_NEXT_ARG: | |
c2f47e15 | 7429 | if (fold_builtin_next_arg (exp, false)) |
a0c938f0 | 7430 | return const0_rtx; |
79012a9d | 7431 | return expand_builtin_next_arg (); |
53800dbe | 7432 | |
ac8fb6db | 7433 | case BUILT_IN_CLEAR_CACHE: |
7434 | target = expand_builtin___clear_cache (exp); | |
7435 | if (target) | |
7436 | return target; | |
7437 | break; | |
7438 | ||
53800dbe | 7439 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 7440 | return expand_builtin_classify_type (exp); |
53800dbe | 7441 | |
7442 | case BUILT_IN_CONSTANT_P: | |
4ee9c684 | 7443 | return const0_rtx; |
53800dbe | 7444 | |
7445 | case BUILT_IN_FRAME_ADDRESS: | |
7446 | case BUILT_IN_RETURN_ADDRESS: | |
c2f47e15 | 7447 | return expand_builtin_frame_address (fndecl, exp); |
53800dbe | 7448 | |
7449 | /* Returns the address of the area where the structure is returned. | |
7450 | 0 otherwise. */ | |
7451 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
c2f47e15 | 7452 | if (call_expr_nargs (exp) != 0 |
9342ee68 | 7453 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
e16ceb8e | 7454 | || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl)))) |
9342ee68 | 7455 | return const0_rtx; |
53800dbe | 7456 | else |
9342ee68 | 7457 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
53800dbe | 7458 | |
2b34677f | 7459 | CASE_BUILT_IN_ALLOCA: |
2b29cc6a | 7460 | target = expand_builtin_alloca (exp); |
53800dbe | 7461 | if (target) |
7462 | return target; | |
7463 | break; | |
7464 | ||
d08919a7 | 7465 | case BUILT_IN_ASAN_ALLOCAS_UNPOISON: |
7466 | return expand_asan_emit_allocas_unpoison (exp); | |
7467 | ||
4ee9c684 | 7468 | case BUILT_IN_STACK_SAVE: |
7469 | return expand_stack_save (); | |
7470 | ||
7471 | case BUILT_IN_STACK_RESTORE: | |
c2f47e15 | 7472 | expand_stack_restore (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 7473 | return const0_rtx; |
7474 | ||
74bdbe96 | 7475 | case BUILT_IN_BSWAP16: |
42791117 | 7476 | case BUILT_IN_BSWAP32: |
7477 | case BUILT_IN_BSWAP64: | |
74bdbe96 | 7478 | target = expand_builtin_bswap (target_mode, exp, target, subtarget); |
42791117 | 7479 | if (target) |
7480 | return target; | |
7481 | break; | |
7482 | ||
4f35b1fc | 7483 | CASE_INT_FN (BUILT_IN_FFS): |
c2f47e15 | 7484 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 7485 | subtarget, ffs_optab); |
6a08d0ab | 7486 | if (target) |
7487 | return target; | |
7488 | break; | |
7489 | ||
4f35b1fc | 7490 | CASE_INT_FN (BUILT_IN_CLZ): |
c2f47e15 | 7491 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 7492 | subtarget, clz_optab); |
6a08d0ab | 7493 | if (target) |
7494 | return target; | |
7495 | break; | |
7496 | ||
4f35b1fc | 7497 | CASE_INT_FN (BUILT_IN_CTZ): |
c2f47e15 | 7498 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 7499 | subtarget, ctz_optab); |
6a08d0ab | 7500 | if (target) |
7501 | return target; | |
7502 | break; | |
7503 | ||
d8492bd3 | 7504 | CASE_INT_FN (BUILT_IN_CLRSB): |
d8492bd3 | 7505 | target = expand_builtin_unop (target_mode, exp, target, |
7506 | subtarget, clrsb_optab); | |
7507 | if (target) | |
7508 | return target; | |
7509 | break; | |
7510 | ||
4f35b1fc | 7511 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
c2f47e15 | 7512 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 7513 | subtarget, popcount_optab); |
6a08d0ab | 7514 | if (target) |
7515 | return target; | |
7516 | break; | |
7517 | ||
4f35b1fc | 7518 | CASE_INT_FN (BUILT_IN_PARITY): |
c2f47e15 | 7519 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 7520 | subtarget, parity_optab); |
53800dbe | 7521 | if (target) |
7522 | return target; | |
7523 | break; | |
7524 | ||
7525 | case BUILT_IN_STRLEN: | |
c2f47e15 | 7526 | target = expand_builtin_strlen (exp, target, target_mode); |
53800dbe | 7527 | if (target) |
7528 | return target; | |
7529 | break; | |
7530 | ||
864bd5de | 7531 | case BUILT_IN_STRNLEN: |
7532 | target = expand_builtin_strnlen (exp, target, target_mode); | |
7533 | if (target) | |
7534 | return target; | |
7535 | break; | |
7536 | ||
5aef8938 | 7537 | case BUILT_IN_STRCAT: |
7538 | target = expand_builtin_strcat (exp, target); | |
7539 | if (target) | |
7540 | return target; | |
7541 | break; | |
7542 | ||
53800dbe | 7543 | case BUILT_IN_STRCPY: |
a65c4d64 | 7544 | target = expand_builtin_strcpy (exp, target); |
53800dbe | 7545 | if (target) |
7546 | return target; | |
7547 | break; | |
bf8e3599 | 7548 | |
5aef8938 | 7549 | case BUILT_IN_STRNCAT: |
7550 | target = expand_builtin_strncat (exp, target); | |
7551 | if (target) | |
7552 | return target; | |
7553 | break; | |
7554 | ||
ed09096d | 7555 | case BUILT_IN_STRNCPY: |
a65c4d64 | 7556 | target = expand_builtin_strncpy (exp, target); |
ed09096d | 7557 | if (target) |
7558 | return target; | |
7559 | break; | |
bf8e3599 | 7560 | |
3b824fa6 | 7561 | case BUILT_IN_STPCPY: |
dc369150 | 7562 | target = expand_builtin_stpcpy (exp, target, mode); |
3b824fa6 | 7563 | if (target) |
7564 | return target; | |
7565 | break; | |
7566 | ||
4d317237 | 7567 | case BUILT_IN_STPNCPY: |
7568 | target = expand_builtin_stpncpy (exp, target); | |
7569 | if (target) | |
7570 | return target; | |
7571 | break; | |
7572 | ||
8d6c6ef5 | 7573 | case BUILT_IN_MEMCHR: |
7574 | target = expand_builtin_memchr (exp, target); | |
7575 | if (target) | |
7576 | return target; | |
7577 | break; | |
7578 | ||
53800dbe | 7579 | case BUILT_IN_MEMCPY: |
a65c4d64 | 7580 | target = expand_builtin_memcpy (exp, target); |
3b824fa6 | 7581 | if (target) |
7582 | return target; | |
7583 | break; | |
7584 | ||
4d317237 | 7585 | case BUILT_IN_MEMMOVE: |
7586 | target = expand_builtin_memmove (exp, target); | |
7587 | if (target) | |
7588 | return target; | |
7589 | break; | |
7590 | ||
3b824fa6 | 7591 | case BUILT_IN_MEMPCPY: |
d0fbba1a | 7592 | target = expand_builtin_mempcpy (exp, target); |
53800dbe | 7593 | if (target) |
7594 | return target; | |
7595 | break; | |
7596 | ||
7597 | case BUILT_IN_MEMSET: | |
c2f47e15 | 7598 | target = expand_builtin_memset (exp, target, mode); |
53800dbe | 7599 | if (target) |
7600 | return target; | |
7601 | break; | |
7602 | ||
ffc83088 | 7603 | case BUILT_IN_BZERO: |
0b25db21 | 7604 | target = expand_builtin_bzero (exp); |
ffc83088 | 7605 | if (target) |
7606 | return target; | |
7607 | break; | |
7608 | ||
b3e6ae76 | 7609 | /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it |
72dbc21d | 7610 | back to a BUILT_IN_STRCMP. Remember to delete the 3rd paramater |
7611 | when changing it to a strcmp call. */ | |
7612 | case BUILT_IN_STRCMP_EQ: | |
7613 | target = expand_builtin_memcmp (exp, target, true); | |
7614 | if (target) | |
7615 | return target; | |
7616 | ||
7617 | /* Change this call back to a BUILT_IN_STRCMP. */ | |
b3e6ae76 | 7618 | TREE_OPERAND (exp, 1) |
72dbc21d | 7619 | = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP)); |
7620 | ||
7621 | /* Delete the last parameter. */ | |
7622 | unsigned int i; | |
7623 | vec<tree, va_gc> *arg_vec; | |
7624 | vec_alloc (arg_vec, 2); | |
7625 | for (i = 0; i < 2; i++) | |
7626 | arg_vec->quick_push (CALL_EXPR_ARG (exp, i)); | |
7627 | exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec); | |
7628 | /* FALLTHROUGH */ | |
7629 | ||
53800dbe | 7630 | case BUILT_IN_STRCMP: |
a65c4d64 | 7631 | target = expand_builtin_strcmp (exp, target); |
53800dbe | 7632 | if (target) |
7633 | return target; | |
7634 | break; | |
7635 | ||
72dbc21d | 7636 | /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it |
7637 | back to a BUILT_IN_STRNCMP. */ | |
7638 | case BUILT_IN_STRNCMP_EQ: | |
7639 | target = expand_builtin_memcmp (exp, target, true); | |
7640 | if (target) | |
7641 | return target; | |
7642 | ||
7643 | /* Change it back to a BUILT_IN_STRNCMP. */ | |
b3e6ae76 | 7644 | TREE_OPERAND (exp, 1) |
72dbc21d | 7645 | = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP)); |
7646 | /* FALLTHROUGH */ | |
7647 | ||
ed09096d | 7648 | case BUILT_IN_STRNCMP: |
7649 | target = expand_builtin_strncmp (exp, target, mode); | |
7650 | if (target) | |
7651 | return target; | |
7652 | break; | |
7653 | ||
071f1696 | 7654 | case BUILT_IN_BCMP: |
53800dbe | 7655 | case BUILT_IN_MEMCMP: |
3e346f54 | 7656 | case BUILT_IN_MEMCMP_EQ: |
7657 | target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ); | |
53800dbe | 7658 | if (target) |
7659 | return target; | |
3e346f54 | 7660 | if (fcode == BUILT_IN_MEMCMP_EQ) |
7661 | { | |
7662 | tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP); | |
7663 | TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl); | |
7664 | } | |
53800dbe | 7665 | break; |
53800dbe | 7666 | |
7667 | case BUILT_IN_SETJMP: | |
12f08300 | 7668 | /* This should have been lowered to the builtins below. */ |
2c8a1497 | 7669 | gcc_unreachable (); |
7670 | ||
7671 | case BUILT_IN_SETJMP_SETUP: | |
7672 | /* __builtin_setjmp_setup is passed a pointer to an array of five words | |
7673 | and the receiver label. */ | |
c2f47e15 | 7674 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 7675 | { |
c2f47e15 | 7676 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
2c8a1497 | 7677 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 7678 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0); |
9ed997be | 7679 | rtx_insn *label_r = label_rtx (label); |
2c8a1497 | 7680 | |
7681 | /* This is copied from the handling of non-local gotos. */ | |
7682 | expand_builtin_setjmp_setup (buf_addr, label_r); | |
7683 | nonlocal_goto_handler_labels | |
a4de1c23 | 7684 | = gen_rtx_INSN_LIST (VOIDmode, label_r, |
2c8a1497 | 7685 | nonlocal_goto_handler_labels); |
7686 | /* ??? Do not let expand_label treat us as such since we would | |
7687 | not want to be both on the list of non-local labels and on | |
7688 | the list of forced labels. */ | |
7689 | FORCED_LABEL (label) = 0; | |
7690 | return const0_rtx; | |
7691 | } | |
7692 | break; | |
7693 | ||
2c8a1497 | 7694 | case BUILT_IN_SETJMP_RECEIVER: |
7695 | /* __builtin_setjmp_receiver is passed the receiver label. */ | |
c2f47e15 | 7696 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 7697 | { |
c2f47e15 | 7698 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0); |
9ed997be | 7699 | rtx_insn *label_r = label_rtx (label); |
2c8a1497 | 7700 | |
7701 | expand_builtin_setjmp_receiver (label_r); | |
7702 | return const0_rtx; | |
7703 | } | |
6b7f6858 | 7704 | break; |
53800dbe | 7705 | |
7706 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
7707 | It's similar to the C library longjmp function but works with | |
7708 | __builtin_setjmp above. */ | |
7709 | case BUILT_IN_LONGJMP: | |
c2f47e15 | 7710 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 7711 | { |
c2f47e15 | 7712 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
8ec3c5c2 | 7713 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 7714 | rtx value = expand_normal (CALL_EXPR_ARG (exp, 1)); |
53800dbe | 7715 | |
7716 | if (value != const1_rtx) | |
7717 | { | |
1e5fcbe2 | 7718 | error ("%<__builtin_longjmp%> second argument must be 1"); |
53800dbe | 7719 | return const0_rtx; |
7720 | } | |
7721 | ||
7722 | expand_builtin_longjmp (buf_addr, value); | |
7723 | return const0_rtx; | |
7724 | } | |
2c8a1497 | 7725 | break; |
53800dbe | 7726 | |
4ee9c684 | 7727 | case BUILT_IN_NONLOCAL_GOTO: |
c2f47e15 | 7728 | target = expand_builtin_nonlocal_goto (exp); |
4ee9c684 | 7729 | if (target) |
7730 | return target; | |
7731 | break; | |
7732 | ||
843d08a9 | 7733 | /* This updates the setjmp buffer that is its argument with the value |
7734 | of the current stack pointer. */ | |
7735 | case BUILT_IN_UPDATE_SETJMP_BUF: | |
c2f47e15 | 7736 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
843d08a9 | 7737 | { |
7738 | rtx buf_addr | |
c2f47e15 | 7739 | = expand_normal (CALL_EXPR_ARG (exp, 0)); |
843d08a9 | 7740 | |
7741 | expand_builtin_update_setjmp_buf (buf_addr); | |
7742 | return const0_rtx; | |
7743 | } | |
7744 | break; | |
7745 | ||
53800dbe | 7746 | case BUILT_IN_TRAP: |
a0ef1725 | 7747 | expand_builtin_trap (); |
53800dbe | 7748 | return const0_rtx; |
7749 | ||
d2b48f0c | 7750 | case BUILT_IN_UNREACHABLE: |
7751 | expand_builtin_unreachable (); | |
7752 | return const0_rtx; | |
7753 | ||
4f35b1fc | 7754 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
004e23c4 | 7755 | case BUILT_IN_SIGNBITD32: |
7756 | case BUILT_IN_SIGNBITD64: | |
7757 | case BUILT_IN_SIGNBITD128: | |
27f261ef | 7758 | target = expand_builtin_signbit (exp, target); |
7759 | if (target) | |
7760 | return target; | |
7761 | break; | |
7762 | ||
53800dbe | 7763 | /* Various hooks for the DWARF 2 __throw routine. */ |
7764 | case BUILT_IN_UNWIND_INIT: | |
7765 | expand_builtin_unwind_init (); | |
7766 | return const0_rtx; | |
7767 | case BUILT_IN_DWARF_CFA: | |
7768 | return virtual_cfa_rtx; | |
7769 | #ifdef DWARF2_UNWIND_INFO | |
f8f023a5 | 7770 | case BUILT_IN_DWARF_SP_COLUMN: |
7771 | return expand_builtin_dwarf_sp_column (); | |
695e919b | 7772 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
c2f47e15 | 7773 | expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0)); |
695e919b | 7774 | return const0_rtx; |
53800dbe | 7775 | #endif |
7776 | case BUILT_IN_FROB_RETURN_ADDR: | |
c2f47e15 | 7777 | return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 7778 | case BUILT_IN_EXTRACT_RETURN_ADDR: |
c2f47e15 | 7779 | return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 7780 | case BUILT_IN_EH_RETURN: |
c2f47e15 | 7781 | expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0), |
7782 | CALL_EXPR_ARG (exp, 1)); | |
53800dbe | 7783 | return const0_rtx; |
df4b504c | 7784 | case BUILT_IN_EH_RETURN_DATA_REGNO: |
c2f47e15 | 7785 | return expand_builtin_eh_return_data_regno (exp); |
26093bf4 | 7786 | case BUILT_IN_EXTEND_POINTER: |
c2f47e15 | 7787 | return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0)); |
e38def9c | 7788 | case BUILT_IN_EH_POINTER: |
7789 | return expand_builtin_eh_pointer (exp); | |
7790 | case BUILT_IN_EH_FILTER: | |
7791 | return expand_builtin_eh_filter (exp); | |
7792 | case BUILT_IN_EH_COPY_VALUES: | |
7793 | return expand_builtin_eh_copy_values (exp); | |
26093bf4 | 7794 | |
7ccc713a | 7795 | case BUILT_IN_VA_START: |
c2f47e15 | 7796 | return expand_builtin_va_start (exp); |
a66c9326 | 7797 | case BUILT_IN_VA_END: |
c2f47e15 | 7798 | return expand_builtin_va_end (exp); |
a66c9326 | 7799 | case BUILT_IN_VA_COPY: |
c2f47e15 | 7800 | return expand_builtin_va_copy (exp); |
89cfe6e5 | 7801 | case BUILT_IN_EXPECT: |
c2f47e15 | 7802 | return expand_builtin_expect (exp, target); |
01107f42 | 7803 | case BUILT_IN_EXPECT_WITH_PROBABILITY: |
7804 | return expand_builtin_expect_with_probability (exp, target); | |
fca0886c | 7805 | case BUILT_IN_ASSUME_ALIGNED: |
7806 | return expand_builtin_assume_aligned (exp, target); | |
5e3608d8 | 7807 | case BUILT_IN_PREFETCH: |
c2f47e15 | 7808 | expand_builtin_prefetch (exp); |
5e3608d8 | 7809 | return const0_rtx; |
7810 | ||
4ee9c684 | 7811 | case BUILT_IN_INIT_TRAMPOLINE: |
c307f106 | 7812 | return expand_builtin_init_trampoline (exp, true); |
7813 | case BUILT_IN_INIT_HEAP_TRAMPOLINE: | |
7814 | return expand_builtin_init_trampoline (exp, false); | |
4ee9c684 | 7815 | case BUILT_IN_ADJUST_TRAMPOLINE: |
c2f47e15 | 7816 | return expand_builtin_adjust_trampoline (exp); |
4ee9c684 | 7817 | |
a27e3913 | 7818 | case BUILT_IN_INIT_DESCRIPTOR: |
7819 | return expand_builtin_init_descriptor (exp); | |
7820 | case BUILT_IN_ADJUST_DESCRIPTOR: | |
7821 | return expand_builtin_adjust_descriptor (exp); | |
7822 | ||
73673831 | 7823 | case BUILT_IN_FORK: |
7824 | case BUILT_IN_EXECL: | |
7825 | case BUILT_IN_EXECV: | |
7826 | case BUILT_IN_EXECLP: | |
7827 | case BUILT_IN_EXECLE: | |
7828 | case BUILT_IN_EXECVP: | |
7829 | case BUILT_IN_EXECVE: | |
c2f47e15 | 7830 | target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore); |
73673831 | 7831 | if (target) |
7832 | return target; | |
7833 | break; | |
53800dbe | 7834 | |
2797f13a | 7835 | case BUILT_IN_SYNC_FETCH_AND_ADD_1: |
7836 | case BUILT_IN_SYNC_FETCH_AND_ADD_2: | |
7837 | case BUILT_IN_SYNC_FETCH_AND_ADD_4: | |
7838 | case BUILT_IN_SYNC_FETCH_AND_ADD_8: | |
7839 | case BUILT_IN_SYNC_FETCH_AND_ADD_16: | |
7840 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1); | |
1cd6e20d | 7841 | target = expand_builtin_sync_operation (mode, exp, PLUS, false, target); |
b6a5fc45 | 7842 | if (target) |
7843 | return target; | |
7844 | break; | |
7845 | ||
2797f13a | 7846 | case BUILT_IN_SYNC_FETCH_AND_SUB_1: |
7847 | case BUILT_IN_SYNC_FETCH_AND_SUB_2: | |
7848 | case BUILT_IN_SYNC_FETCH_AND_SUB_4: | |
7849 | case BUILT_IN_SYNC_FETCH_AND_SUB_8: | |
7850 | case BUILT_IN_SYNC_FETCH_AND_SUB_16: | |
7851 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1); | |
1cd6e20d | 7852 | target = expand_builtin_sync_operation (mode, exp, MINUS, false, target); |
b6a5fc45 | 7853 | if (target) |
7854 | return target; | |
7855 | break; | |
7856 | ||
2797f13a | 7857 | case BUILT_IN_SYNC_FETCH_AND_OR_1: |
7858 | case BUILT_IN_SYNC_FETCH_AND_OR_2: | |
7859 | case BUILT_IN_SYNC_FETCH_AND_OR_4: | |
7860 | case BUILT_IN_SYNC_FETCH_AND_OR_8: | |
7861 | case BUILT_IN_SYNC_FETCH_AND_OR_16: | |
7862 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1); | |
1cd6e20d | 7863 | target = expand_builtin_sync_operation (mode, exp, IOR, false, target); |
b6a5fc45 | 7864 | if (target) |
7865 | return target; | |
7866 | break; | |
7867 | ||
2797f13a | 7868 | case BUILT_IN_SYNC_FETCH_AND_AND_1: |
7869 | case BUILT_IN_SYNC_FETCH_AND_AND_2: | |
7870 | case BUILT_IN_SYNC_FETCH_AND_AND_4: | |
7871 | case BUILT_IN_SYNC_FETCH_AND_AND_8: | |
7872 | case BUILT_IN_SYNC_FETCH_AND_AND_16: | |
7873 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1); | |
1cd6e20d | 7874 | target = expand_builtin_sync_operation (mode, exp, AND, false, target); |
b6a5fc45 | 7875 | if (target) |
7876 | return target; | |
7877 | break; | |
7878 | ||
2797f13a | 7879 | case BUILT_IN_SYNC_FETCH_AND_XOR_1: |
7880 | case BUILT_IN_SYNC_FETCH_AND_XOR_2: | |
7881 | case BUILT_IN_SYNC_FETCH_AND_XOR_4: | |
7882 | case BUILT_IN_SYNC_FETCH_AND_XOR_8: | |
7883 | case BUILT_IN_SYNC_FETCH_AND_XOR_16: | |
7884 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1); | |
1cd6e20d | 7885 | target = expand_builtin_sync_operation (mode, exp, XOR, false, target); |
b6a5fc45 | 7886 | if (target) |
7887 | return target; | |
7888 | break; | |
7889 | ||
2797f13a | 7890 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
7891 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
7892 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
7893 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
7894 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
7895 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1); | |
1cd6e20d | 7896 | target = expand_builtin_sync_operation (mode, exp, NOT, false, target); |
b6a5fc45 | 7897 | if (target) |
7898 | return target; | |
7899 | break; | |
7900 | ||
2797f13a | 7901 | case BUILT_IN_SYNC_ADD_AND_FETCH_1: |
7902 | case BUILT_IN_SYNC_ADD_AND_FETCH_2: | |
7903 | case BUILT_IN_SYNC_ADD_AND_FETCH_4: | |
7904 | case BUILT_IN_SYNC_ADD_AND_FETCH_8: | |
7905 | case BUILT_IN_SYNC_ADD_AND_FETCH_16: | |
7906 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1); | |
1cd6e20d | 7907 | target = expand_builtin_sync_operation (mode, exp, PLUS, true, target); |
b6a5fc45 | 7908 | if (target) |
7909 | return target; | |
7910 | break; | |
7911 | ||
2797f13a | 7912 | case BUILT_IN_SYNC_SUB_AND_FETCH_1: |
7913 | case BUILT_IN_SYNC_SUB_AND_FETCH_2: | |
7914 | case BUILT_IN_SYNC_SUB_AND_FETCH_4: | |
7915 | case BUILT_IN_SYNC_SUB_AND_FETCH_8: | |
7916 | case BUILT_IN_SYNC_SUB_AND_FETCH_16: | |
7917 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1); | |
1cd6e20d | 7918 | target = expand_builtin_sync_operation (mode, exp, MINUS, true, target); |
b6a5fc45 | 7919 | if (target) |
7920 | return target; | |
7921 | break; | |
7922 | ||
2797f13a | 7923 | case BUILT_IN_SYNC_OR_AND_FETCH_1: |
7924 | case BUILT_IN_SYNC_OR_AND_FETCH_2: | |
7925 | case BUILT_IN_SYNC_OR_AND_FETCH_4: | |
7926 | case BUILT_IN_SYNC_OR_AND_FETCH_8: | |
7927 | case BUILT_IN_SYNC_OR_AND_FETCH_16: | |
7928 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1); | |
1cd6e20d | 7929 | target = expand_builtin_sync_operation (mode, exp, IOR, true, target); |
b6a5fc45 | 7930 | if (target) |
7931 | return target; | |
7932 | break; | |
7933 | ||
2797f13a | 7934 | case BUILT_IN_SYNC_AND_AND_FETCH_1: |
7935 | case BUILT_IN_SYNC_AND_AND_FETCH_2: | |
7936 | case BUILT_IN_SYNC_AND_AND_FETCH_4: | |
7937 | case BUILT_IN_SYNC_AND_AND_FETCH_8: | |
7938 | case BUILT_IN_SYNC_AND_AND_FETCH_16: | |
7939 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1); | |
1cd6e20d | 7940 | target = expand_builtin_sync_operation (mode, exp, AND, true, target); |
b6a5fc45 | 7941 | if (target) |
7942 | return target; | |
7943 | break; | |
7944 | ||
2797f13a | 7945 | case BUILT_IN_SYNC_XOR_AND_FETCH_1: |
7946 | case BUILT_IN_SYNC_XOR_AND_FETCH_2: | |
7947 | case BUILT_IN_SYNC_XOR_AND_FETCH_4: | |
7948 | case BUILT_IN_SYNC_XOR_AND_FETCH_8: | |
7949 | case BUILT_IN_SYNC_XOR_AND_FETCH_16: | |
7950 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1); | |
1cd6e20d | 7951 | target = expand_builtin_sync_operation (mode, exp, XOR, true, target); |
b6a5fc45 | 7952 | if (target) |
7953 | return target; | |
7954 | break; | |
7955 | ||
2797f13a | 7956 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
7957 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
7958 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
7959 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
7960 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
7961 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1); | |
1cd6e20d | 7962 | target = expand_builtin_sync_operation (mode, exp, NOT, true, target); |
b6a5fc45 | 7963 | if (target) |
7964 | return target; | |
7965 | break; | |
7966 | ||
2797f13a | 7967 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: |
7968 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
7969 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
7970 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
7971 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
a601d32a | 7972 | if (mode == VOIDmode) |
7973 | mode = TYPE_MODE (boolean_type_node); | |
b6a5fc45 | 7974 | if (!target || !register_operand (target, mode)) |
7975 | target = gen_reg_rtx (mode); | |
3e272de8 | 7976 | |
2797f13a | 7977 | mode = get_builtin_sync_mode |
7978 | (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1); | |
c2f47e15 | 7979 | target = expand_builtin_compare_and_swap (mode, exp, true, target); |
b6a5fc45 | 7980 | if (target) |
7981 | return target; | |
7982 | break; | |
7983 | ||
2797f13a | 7984 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: |
7985 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: | |
7986 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: | |
7987 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: | |
7988 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: | |
7989 | mode = get_builtin_sync_mode | |
7990 | (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1); | |
c2f47e15 | 7991 | target = expand_builtin_compare_and_swap (mode, exp, false, target); |
b6a5fc45 | 7992 | if (target) |
7993 | return target; | |
7994 | break; | |
7995 | ||
2797f13a | 7996 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: |
7997 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: | |
7998 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: | |
7999 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: | |
8000 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: | |
8001 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1); | |
8002 | target = expand_builtin_sync_lock_test_and_set (mode, exp, target); | |
b6a5fc45 | 8003 | if (target) |
8004 | return target; | |
8005 | break; | |
8006 | ||
2797f13a | 8007 | case BUILT_IN_SYNC_LOCK_RELEASE_1: |
8008 | case BUILT_IN_SYNC_LOCK_RELEASE_2: | |
8009 | case BUILT_IN_SYNC_LOCK_RELEASE_4: | |
8010 | case BUILT_IN_SYNC_LOCK_RELEASE_8: | |
8011 | case BUILT_IN_SYNC_LOCK_RELEASE_16: | |
8012 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1); | |
8013 | expand_builtin_sync_lock_release (mode, exp); | |
b6a5fc45 | 8014 | return const0_rtx; |
8015 | ||
2797f13a | 8016 | case BUILT_IN_SYNC_SYNCHRONIZE: |
8017 | expand_builtin_sync_synchronize (); | |
b6a5fc45 | 8018 | return const0_rtx; |
8019 | ||
1cd6e20d | 8020 | case BUILT_IN_ATOMIC_EXCHANGE_1: |
8021 | case BUILT_IN_ATOMIC_EXCHANGE_2: | |
8022 | case BUILT_IN_ATOMIC_EXCHANGE_4: | |
8023 | case BUILT_IN_ATOMIC_EXCHANGE_8: | |
8024 | case BUILT_IN_ATOMIC_EXCHANGE_16: | |
8025 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1); | |
8026 | target = expand_builtin_atomic_exchange (mode, exp, target); | |
8027 | if (target) | |
8028 | return target; | |
8029 | break; | |
8030 | ||
8031 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
8032 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
8033 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
8034 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
8035 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
2c201ad1 | 8036 | { |
8037 | unsigned int nargs, z; | |
f1f41a6c | 8038 | vec<tree, va_gc> *vec; |
2c201ad1 | 8039 | |
8040 | mode = | |
8041 | get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1); | |
8042 | target = expand_builtin_atomic_compare_exchange (mode, exp, target); | |
8043 | if (target) | |
8044 | return target; | |
8045 | ||
8046 | /* If this is turned into an external library call, the weak parameter | |
8047 | must be dropped to match the expected parameter list. */ | |
8048 | nargs = call_expr_nargs (exp); | |
f1f41a6c | 8049 | vec_alloc (vec, nargs - 1); |
2c201ad1 | 8050 | for (z = 0; z < 3; z++) |
f1f41a6c | 8051 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
2c201ad1 | 8052 | /* Skip the boolean weak parameter. */ |
8053 | for (z = 4; z < 6; z++) | |
f1f41a6c | 8054 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
2c201ad1 | 8055 | exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec); |
8056 | break; | |
8057 | } | |
1cd6e20d | 8058 | |
8059 | case BUILT_IN_ATOMIC_LOAD_1: | |
8060 | case BUILT_IN_ATOMIC_LOAD_2: | |
8061 | case BUILT_IN_ATOMIC_LOAD_4: | |
8062 | case BUILT_IN_ATOMIC_LOAD_8: | |
8063 | case BUILT_IN_ATOMIC_LOAD_16: | |
8064 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1); | |
8065 | target = expand_builtin_atomic_load (mode, exp, target); | |
8066 | if (target) | |
8067 | return target; | |
8068 | break; | |
8069 | ||
8070 | case BUILT_IN_ATOMIC_STORE_1: | |
8071 | case BUILT_IN_ATOMIC_STORE_2: | |
8072 | case BUILT_IN_ATOMIC_STORE_4: | |
8073 | case BUILT_IN_ATOMIC_STORE_8: | |
8074 | case BUILT_IN_ATOMIC_STORE_16: | |
8075 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1); | |
8076 | target = expand_builtin_atomic_store (mode, exp); | |
8077 | if (target) | |
8078 | return const0_rtx; | |
8079 | break; | |
8080 | ||
8081 | case BUILT_IN_ATOMIC_ADD_FETCH_1: | |
8082 | case BUILT_IN_ATOMIC_ADD_FETCH_2: | |
8083 | case BUILT_IN_ATOMIC_ADD_FETCH_4: | |
8084 | case BUILT_IN_ATOMIC_ADD_FETCH_8: | |
8085 | case BUILT_IN_ATOMIC_ADD_FETCH_16: | |
8086 | { | |
8087 | enum built_in_function lib; | |
8088 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1); | |
8089 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 + | |
8090 | (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1)); | |
8091 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true, | |
8092 | ignore, lib); | |
8093 | if (target) | |
8094 | return target; | |
8095 | break; | |
8096 | } | |
8097 | case BUILT_IN_ATOMIC_SUB_FETCH_1: | |
8098 | case BUILT_IN_ATOMIC_SUB_FETCH_2: | |
8099 | case BUILT_IN_ATOMIC_SUB_FETCH_4: | |
8100 | case BUILT_IN_ATOMIC_SUB_FETCH_8: | |
8101 | case BUILT_IN_ATOMIC_SUB_FETCH_16: | |
8102 | { | |
8103 | enum built_in_function lib; | |
8104 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1); | |
8105 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 + | |
8106 | (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1)); | |
8107 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true, | |
8108 | ignore, lib); | |
8109 | if (target) | |
8110 | return target; | |
8111 | break; | |
8112 | } | |
8113 | case BUILT_IN_ATOMIC_AND_FETCH_1: | |
8114 | case BUILT_IN_ATOMIC_AND_FETCH_2: | |
8115 | case BUILT_IN_ATOMIC_AND_FETCH_4: | |
8116 | case BUILT_IN_ATOMIC_AND_FETCH_8: | |
8117 | case BUILT_IN_ATOMIC_AND_FETCH_16: | |
8118 | { | |
8119 | enum built_in_function lib; | |
8120 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1); | |
8121 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 + | |
8122 | (fcode - BUILT_IN_ATOMIC_AND_FETCH_1)); | |
8123 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true, | |
8124 | ignore, lib); | |
8125 | if (target) | |
8126 | return target; | |
8127 | break; | |
8128 | } | |
8129 | case BUILT_IN_ATOMIC_NAND_FETCH_1: | |
8130 | case BUILT_IN_ATOMIC_NAND_FETCH_2: | |
8131 | case BUILT_IN_ATOMIC_NAND_FETCH_4: | |
8132 | case BUILT_IN_ATOMIC_NAND_FETCH_8: | |
8133 | case BUILT_IN_ATOMIC_NAND_FETCH_16: | |
8134 | { | |
8135 | enum built_in_function lib; | |
8136 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1); | |
8137 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 + | |
8138 | (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1)); | |
8139 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true, | |
8140 | ignore, lib); | |
8141 | if (target) | |
8142 | return target; | |
8143 | break; | |
8144 | } | |
8145 | case BUILT_IN_ATOMIC_XOR_FETCH_1: | |
8146 | case BUILT_IN_ATOMIC_XOR_FETCH_2: | |
8147 | case BUILT_IN_ATOMIC_XOR_FETCH_4: | |
8148 | case BUILT_IN_ATOMIC_XOR_FETCH_8: | |
8149 | case BUILT_IN_ATOMIC_XOR_FETCH_16: | |
8150 | { | |
8151 | enum built_in_function lib; | |
8152 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1); | |
8153 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 + | |
8154 | (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1)); | |
8155 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true, | |
8156 | ignore, lib); | |
8157 | if (target) | |
8158 | return target; | |
8159 | break; | |
8160 | } | |
8161 | case BUILT_IN_ATOMIC_OR_FETCH_1: | |
8162 | case BUILT_IN_ATOMIC_OR_FETCH_2: | |
8163 | case BUILT_IN_ATOMIC_OR_FETCH_4: | |
8164 | case BUILT_IN_ATOMIC_OR_FETCH_8: | |
8165 | case BUILT_IN_ATOMIC_OR_FETCH_16: | |
8166 | { | |
8167 | enum built_in_function lib; | |
8168 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1); | |
8169 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 + | |
8170 | (fcode - BUILT_IN_ATOMIC_OR_FETCH_1)); | |
8171 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true, | |
8172 | ignore, lib); | |
8173 | if (target) | |
8174 | return target; | |
8175 | break; | |
8176 | } | |
8177 | case BUILT_IN_ATOMIC_FETCH_ADD_1: | |
8178 | case BUILT_IN_ATOMIC_FETCH_ADD_2: | |
8179 | case BUILT_IN_ATOMIC_FETCH_ADD_4: | |
8180 | case BUILT_IN_ATOMIC_FETCH_ADD_8: | |
8181 | case BUILT_IN_ATOMIC_FETCH_ADD_16: | |
8182 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1); | |
8183 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false, | |
8184 | ignore, BUILT_IN_NONE); | |
8185 | if (target) | |
8186 | return target; | |
8187 | break; | |
8188 | ||
8189 | case BUILT_IN_ATOMIC_FETCH_SUB_1: | |
8190 | case BUILT_IN_ATOMIC_FETCH_SUB_2: | |
8191 | case BUILT_IN_ATOMIC_FETCH_SUB_4: | |
8192 | case BUILT_IN_ATOMIC_FETCH_SUB_8: | |
8193 | case BUILT_IN_ATOMIC_FETCH_SUB_16: | |
8194 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1); | |
8195 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false, | |
8196 | ignore, BUILT_IN_NONE); | |
8197 | if (target) | |
8198 | return target; | |
8199 | break; | |
8200 | ||
8201 | case BUILT_IN_ATOMIC_FETCH_AND_1: | |
8202 | case BUILT_IN_ATOMIC_FETCH_AND_2: | |
8203 | case BUILT_IN_ATOMIC_FETCH_AND_4: | |
8204 | case BUILT_IN_ATOMIC_FETCH_AND_8: | |
8205 | case BUILT_IN_ATOMIC_FETCH_AND_16: | |
8206 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1); | |
8207 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false, | |
8208 | ignore, BUILT_IN_NONE); | |
8209 | if (target) | |
8210 | return target; | |
8211 | break; | |
8212 | ||
8213 | case BUILT_IN_ATOMIC_FETCH_NAND_1: | |
8214 | case BUILT_IN_ATOMIC_FETCH_NAND_2: | |
8215 | case BUILT_IN_ATOMIC_FETCH_NAND_4: | |
8216 | case BUILT_IN_ATOMIC_FETCH_NAND_8: | |
8217 | case BUILT_IN_ATOMIC_FETCH_NAND_16: | |
8218 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1); | |
8219 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false, | |
8220 | ignore, BUILT_IN_NONE); | |
8221 | if (target) | |
8222 | return target; | |
8223 | break; | |
8224 | ||
8225 | case BUILT_IN_ATOMIC_FETCH_XOR_1: | |
8226 | case BUILT_IN_ATOMIC_FETCH_XOR_2: | |
8227 | case BUILT_IN_ATOMIC_FETCH_XOR_4: | |
8228 | case BUILT_IN_ATOMIC_FETCH_XOR_8: | |
8229 | case BUILT_IN_ATOMIC_FETCH_XOR_16: | |
8230 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1); | |
8231 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false, | |
8232 | ignore, BUILT_IN_NONE); | |
8233 | if (target) | |
8234 | return target; | |
8235 | break; | |
8236 | ||
8237 | case BUILT_IN_ATOMIC_FETCH_OR_1: | |
8238 | case BUILT_IN_ATOMIC_FETCH_OR_2: | |
8239 | case BUILT_IN_ATOMIC_FETCH_OR_4: | |
8240 | case BUILT_IN_ATOMIC_FETCH_OR_8: | |
8241 | case BUILT_IN_ATOMIC_FETCH_OR_16: | |
8242 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1); | |
8243 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false, | |
8244 | ignore, BUILT_IN_NONE); | |
8245 | if (target) | |
8246 | return target; | |
8247 | break; | |
10b744a3 | 8248 | |
8249 | case BUILT_IN_ATOMIC_TEST_AND_SET: | |
7821cde1 | 8250 | return expand_builtin_atomic_test_and_set (exp, target); |
10b744a3 | 8251 | |
8252 | case BUILT_IN_ATOMIC_CLEAR: | |
8253 | return expand_builtin_atomic_clear (exp); | |
1cd6e20d | 8254 | |
8255 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: | |
8256 | return expand_builtin_atomic_always_lock_free (exp); | |
8257 | ||
8258 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
8259 | target = expand_builtin_atomic_is_lock_free (exp); | |
8260 | if (target) | |
8261 | return target; | |
8262 | break; | |
8263 | ||
8264 | case BUILT_IN_ATOMIC_THREAD_FENCE: | |
8265 | expand_builtin_atomic_thread_fence (exp); | |
8266 | return const0_rtx; | |
8267 | ||
8268 | case BUILT_IN_ATOMIC_SIGNAL_FENCE: | |
8269 | expand_builtin_atomic_signal_fence (exp); | |
8270 | return const0_rtx; | |
8271 | ||
0a39fd54 | 8272 | case BUILT_IN_OBJECT_SIZE: |
8273 | return expand_builtin_object_size (exp); | |
8274 | ||
8275 | case BUILT_IN_MEMCPY_CHK: | |
8276 | case BUILT_IN_MEMPCPY_CHK: | |
8277 | case BUILT_IN_MEMMOVE_CHK: | |
8278 | case BUILT_IN_MEMSET_CHK: | |
8279 | target = expand_builtin_memory_chk (exp, target, mode, fcode); | |
8280 | if (target) | |
8281 | return target; | |
8282 | break; | |
8283 | ||
8284 | case BUILT_IN_STRCPY_CHK: | |
8285 | case BUILT_IN_STPCPY_CHK: | |
8286 | case BUILT_IN_STRNCPY_CHK: | |
1063acde | 8287 | case BUILT_IN_STPNCPY_CHK: |
0a39fd54 | 8288 | case BUILT_IN_STRCAT_CHK: |
b356dfef | 8289 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 8290 | case BUILT_IN_SNPRINTF_CHK: |
8291 | case BUILT_IN_VSNPRINTF_CHK: | |
8292 | maybe_emit_chk_warning (exp, fcode); | |
8293 | break; | |
8294 | ||
8295 | case BUILT_IN_SPRINTF_CHK: | |
8296 | case BUILT_IN_VSPRINTF_CHK: | |
8297 | maybe_emit_sprintf_chk_warning (exp, fcode); | |
8298 | break; | |
8299 | ||
2c281b15 | 8300 | case BUILT_IN_FREE: |
f74ea1c2 | 8301 | if (warn_free_nonheap_object) |
8302 | maybe_emit_free_warning (exp); | |
2c281b15 | 8303 | break; |
8304 | ||
badaa04c | 8305 | case BUILT_IN_THREAD_POINTER: |
8306 | return expand_builtin_thread_pointer (exp, target); | |
8307 | ||
8308 | case BUILT_IN_SET_THREAD_POINTER: | |
8309 | expand_builtin_set_thread_pointer (exp); | |
8310 | return const0_rtx; | |
8311 | ||
ca4c3545 | 8312 | case BUILT_IN_ACC_ON_DEVICE: |
1ae4e7aa | 8313 | /* Do library call, if we failed to expand the builtin when |
8314 | folding. */ | |
ca4c3545 | 8315 | break; |
8316 | ||
a7babc1e | 8317 | case BUILT_IN_GOACC_PARLEVEL_ID: |
8318 | case BUILT_IN_GOACC_PARLEVEL_SIZE: | |
8319 | return expand_builtin_goacc_parlevel_id_size (exp, target, ignore); | |
8320 | ||
123081ef | 8321 | case BUILT_IN_SPECULATION_SAFE_VALUE_PTR: |
8322 | return expand_speculation_safe_value (VOIDmode, exp, target, ignore); | |
8323 | ||
8324 | case BUILT_IN_SPECULATION_SAFE_VALUE_1: | |
8325 | case BUILT_IN_SPECULATION_SAFE_VALUE_2: | |
8326 | case BUILT_IN_SPECULATION_SAFE_VALUE_4: | |
8327 | case BUILT_IN_SPECULATION_SAFE_VALUE_8: | |
8328 | case BUILT_IN_SPECULATION_SAFE_VALUE_16: | |
8329 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1); | |
8330 | return expand_speculation_safe_value (mode, exp, target, ignore); | |
8331 | ||
92482ee0 | 8332 | default: /* just do library call, if unknown builtin */ |
146c1b4f | 8333 | break; |
53800dbe | 8334 | } |
8335 | ||
8336 | /* The switch statement above can drop through to cause the function | |
8337 | to be called normally. */ | |
8338 | return expand_call (exp, target, ignore); | |
8339 | } | |
650e4c94 | 8340 | |
805e22b2 | 8341 | /* Determine whether a tree node represents a call to a built-in |
52203a9d | 8342 | function. If the tree T is a call to a built-in function with |
8343 | the right number of arguments of the appropriate types, return | |
8344 | the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. | |
8345 | Otherwise the return value is END_BUILTINS. */ | |
aecda0d6 | 8346 | |
805e22b2 | 8347 | enum built_in_function |
b7bf20db | 8348 | builtin_mathfn_code (const_tree t) |
805e22b2 | 8349 | { |
b7bf20db | 8350 | const_tree fndecl, arg, parmlist; |
8351 | const_tree argtype, parmtype; | |
8352 | const_call_expr_arg_iterator iter; | |
805e22b2 | 8353 | |
d44e3710 | 8354 | if (TREE_CODE (t) != CALL_EXPR) |
805e22b2 | 8355 | return END_BUILTINS; |
8356 | ||
c6e6ecb1 | 8357 | fndecl = get_callee_fndecl (t); |
a0e9bfbb | 8358 | if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) |
8359 | return END_BUILTINS; | |
805e22b2 | 8360 | |
52203a9d | 8361 | parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
b7bf20db | 8362 | init_const_call_expr_arg_iterator (t, &iter); |
52203a9d | 8363 | for (; parmlist; parmlist = TREE_CHAIN (parmlist)) |
e9f80ff5 | 8364 | { |
52203a9d | 8365 | /* If a function doesn't take a variable number of arguments, |
8366 | the last element in the list will have type `void'. */ | |
8367 | parmtype = TREE_VALUE (parmlist); | |
8368 | if (VOID_TYPE_P (parmtype)) | |
8369 | { | |
b7bf20db | 8370 | if (more_const_call_expr_args_p (&iter)) |
52203a9d | 8371 | return END_BUILTINS; |
8372 | return DECL_FUNCTION_CODE (fndecl); | |
8373 | } | |
8374 | ||
b7bf20db | 8375 | if (! more_const_call_expr_args_p (&iter)) |
e9f80ff5 | 8376 | return END_BUILTINS; |
48e1416a | 8377 | |
b7bf20db | 8378 | arg = next_const_call_expr_arg (&iter); |
c2f47e15 | 8379 | argtype = TREE_TYPE (arg); |
52203a9d | 8380 | |
8381 | if (SCALAR_FLOAT_TYPE_P (parmtype)) | |
8382 | { | |
8383 | if (! SCALAR_FLOAT_TYPE_P (argtype)) | |
8384 | return END_BUILTINS; | |
8385 | } | |
8386 | else if (COMPLEX_FLOAT_TYPE_P (parmtype)) | |
8387 | { | |
8388 | if (! COMPLEX_FLOAT_TYPE_P (argtype)) | |
8389 | return END_BUILTINS; | |
8390 | } | |
8391 | else if (POINTER_TYPE_P (parmtype)) | |
8392 | { | |
8393 | if (! POINTER_TYPE_P (argtype)) | |
8394 | return END_BUILTINS; | |
8395 | } | |
8396 | else if (INTEGRAL_TYPE_P (parmtype)) | |
8397 | { | |
8398 | if (! INTEGRAL_TYPE_P (argtype)) | |
8399 | return END_BUILTINS; | |
8400 | } | |
8401 | else | |
e9f80ff5 | 8402 | return END_BUILTINS; |
e9f80ff5 | 8403 | } |
8404 | ||
52203a9d | 8405 | /* Variable-length argument list. */ |
805e22b2 | 8406 | return DECL_FUNCTION_CODE (fndecl); |
8407 | } | |
8408 | ||
c2f47e15 | 8409 | /* Fold a call to __builtin_constant_p, if we know its argument ARG will |
8410 | evaluate to a constant. */ | |
650e4c94 | 8411 | |
8412 | static tree | |
c2f47e15 | 8413 | fold_builtin_constant_p (tree arg) |
650e4c94 | 8414 | { |
650e4c94 | 8415 | /* We return 1 for a numeric type that's known to be a constant |
8416 | value at compile-time or for an aggregate type that's a | |
8417 | literal constant. */ | |
c2f47e15 | 8418 | STRIP_NOPS (arg); |
650e4c94 | 8419 | |
8420 | /* If we know this is a constant, emit the constant of one. */ | |
c2f47e15 | 8421 | if (CONSTANT_CLASS_P (arg) |
8422 | || (TREE_CODE (arg) == CONSTRUCTOR | |
8423 | && TREE_CONSTANT (arg))) | |
650e4c94 | 8424 | return integer_one_node; |
c2f47e15 | 8425 | if (TREE_CODE (arg) == ADDR_EXPR) |
adcfa3a3 | 8426 | { |
c2f47e15 | 8427 | tree op = TREE_OPERAND (arg, 0); |
adcfa3a3 | 8428 | if (TREE_CODE (op) == STRING_CST |
8429 | || (TREE_CODE (op) == ARRAY_REF | |
8430 | && integer_zerop (TREE_OPERAND (op, 1)) | |
8431 | && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST)) | |
8432 | return integer_one_node; | |
8433 | } | |
650e4c94 | 8434 | |
1fb4300c | 8435 | /* If this expression has side effects, show we don't know it to be a |
8436 | constant. Likewise if it's a pointer or aggregate type since in | |
8437 | those case we only want literals, since those are only optimized | |
f97c71a1 | 8438 | when generating RTL, not later. |
8439 | And finally, if we are compiling an initializer, not code, we | |
8440 | need to return a definite result now; there's not going to be any | |
8441 | more optimization done. */ | |
c2f47e15 | 8442 | if (TREE_SIDE_EFFECTS (arg) |
8443 | || AGGREGATE_TYPE_P (TREE_TYPE (arg)) | |
8444 | || POINTER_TYPE_P (TREE_TYPE (arg)) | |
47be647d | 8445 | || cfun == 0 |
0b049e15 | 8446 | || folding_initializer |
8447 | || force_folding_builtin_constant_p) | |
650e4c94 | 8448 | return integer_zero_node; |
8449 | ||
c2f47e15 | 8450 | return NULL_TREE; |
650e4c94 | 8451 | } |
8452 | ||
01107f42 | 8453 | /* Create builtin_expect or builtin_expect_with_probability |
8454 | with PRED and EXPECTED as its arguments and return it as a truthvalue. | |
8455 | Fortran FE can also produce builtin_expect with PREDICTOR as third argument. | |
8456 | builtin_expect_with_probability instead uses third argument as PROBABILITY | |
8457 | value. */ | |
4ee9c684 | 8458 | |
8459 | static tree | |
c83059be | 8460 | build_builtin_expect_predicate (location_t loc, tree pred, tree expected, |
01107f42 | 8461 | tree predictor, tree probability) |
4ee9c684 | 8462 | { |
76f5a783 | 8463 | tree fn, arg_types, pred_type, expected_type, call_expr, ret_type; |
4ee9c684 | 8464 | |
01107f42 | 8465 | fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT |
8466 | : BUILT_IN_EXPECT_WITH_PROBABILITY); | |
76f5a783 | 8467 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
8468 | ret_type = TREE_TYPE (TREE_TYPE (fn)); | |
8469 | pred_type = TREE_VALUE (arg_types); | |
8470 | expected_type = TREE_VALUE (TREE_CHAIN (arg_types)); | |
8471 | ||
389dd41b | 8472 | pred = fold_convert_loc (loc, pred_type, pred); |
8473 | expected = fold_convert_loc (loc, expected_type, expected); | |
01107f42 | 8474 | |
8475 | if (probability) | |
8476 | call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability); | |
8477 | else | |
8478 | call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected, | |
8479 | predictor); | |
76f5a783 | 8480 | |
8481 | return build2 (NE_EXPR, TREE_TYPE (pred), call_expr, | |
8482 | build_int_cst (ret_type, 0)); | |
8483 | } | |
8484 | ||
01107f42 | 8485 | /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return |
76f5a783 | 8486 | NULL_TREE if no simplification is possible. */ |
8487 | ||
c83059be | 8488 | tree |
01107f42 | 8489 | fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2, |
8490 | tree arg3) | |
76f5a783 | 8491 | { |
083bada9 | 8492 | tree inner, fndecl, inner_arg0; |
76f5a783 | 8493 | enum tree_code code; |
8494 | ||
083bada9 | 8495 | /* Distribute the expected value over short-circuiting operators. |
8496 | See through the cast from truthvalue_type_node to long. */ | |
8497 | inner_arg0 = arg0; | |
d09ef31a | 8498 | while (CONVERT_EXPR_P (inner_arg0) |
083bada9 | 8499 | && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0)) |
8500 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0)))) | |
8501 | inner_arg0 = TREE_OPERAND (inner_arg0, 0); | |
8502 | ||
76f5a783 | 8503 | /* If this is a builtin_expect within a builtin_expect keep the |
8504 | inner one. See through a comparison against a constant. It | |
8505 | might have been added to create a thruthvalue. */ | |
083bada9 | 8506 | inner = inner_arg0; |
8507 | ||
76f5a783 | 8508 | if (COMPARISON_CLASS_P (inner) |
8509 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST) | |
8510 | inner = TREE_OPERAND (inner, 0); | |
8511 | ||
8512 | if (TREE_CODE (inner) == CALL_EXPR | |
8513 | && (fndecl = get_callee_fndecl (inner)) | |
a0e9bfbb | 8514 | && (fndecl_built_in_p (fndecl, BUILT_IN_EXPECT) |
8515 | || fndecl_built_in_p (fndecl, BUILT_IN_EXPECT_WITH_PROBABILITY))) | |
76f5a783 | 8516 | return arg0; |
8517 | ||
083bada9 | 8518 | inner = inner_arg0; |
76f5a783 | 8519 | code = TREE_CODE (inner); |
8520 | if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) | |
8521 | { | |
8522 | tree op0 = TREE_OPERAND (inner, 0); | |
8523 | tree op1 = TREE_OPERAND (inner, 1); | |
2f2a7720 | 8524 | arg1 = save_expr (arg1); |
76f5a783 | 8525 | |
01107f42 | 8526 | op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3); |
8527 | op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3); | |
76f5a783 | 8528 | inner = build2 (code, TREE_TYPE (inner), op0, op1); |
8529 | ||
389dd41b | 8530 | return fold_convert_loc (loc, TREE_TYPE (arg0), inner); |
76f5a783 | 8531 | } |
8532 | ||
8533 | /* If the argument isn't invariant then there's nothing else we can do. */ | |
083bada9 | 8534 | if (!TREE_CONSTANT (inner_arg0)) |
c2f47e15 | 8535 | return NULL_TREE; |
4ee9c684 | 8536 | |
76f5a783 | 8537 | /* If we expect that a comparison against the argument will fold to |
8538 | a constant return the constant. In practice, this means a true | |
8539 | constant or the address of a non-weak symbol. */ | |
083bada9 | 8540 | inner = inner_arg0; |
4ee9c684 | 8541 | STRIP_NOPS (inner); |
8542 | if (TREE_CODE (inner) == ADDR_EXPR) | |
8543 | { | |
8544 | do | |
8545 | { | |
8546 | inner = TREE_OPERAND (inner, 0); | |
8547 | } | |
8548 | while (TREE_CODE (inner) == COMPONENT_REF | |
8549 | || TREE_CODE (inner) == ARRAY_REF); | |
53e9c5c4 | 8550 | if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner)) |
c2f47e15 | 8551 | return NULL_TREE; |
4ee9c684 | 8552 | } |
8553 | ||
76f5a783 | 8554 | /* Otherwise, ARG0 already has the proper type for the return value. */ |
8555 | return arg0; | |
4ee9c684 | 8556 | } |
8557 | ||
c2f47e15 | 8558 | /* Fold a call to __builtin_classify_type with argument ARG. */ |
27d0c333 | 8559 | |
539a3a92 | 8560 | static tree |
c2f47e15 | 8561 | fold_builtin_classify_type (tree arg) |
539a3a92 | 8562 | { |
c2f47e15 | 8563 | if (arg == 0) |
7002a1c8 | 8564 | return build_int_cst (integer_type_node, no_type_class); |
539a3a92 | 8565 | |
7002a1c8 | 8566 | return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg))); |
539a3a92 | 8567 | } |
8568 | ||
c2f47e15 | 8569 | /* Fold a call to __builtin_strlen with argument ARG. */ |
e6e27594 | 8570 | |
8571 | static tree | |
c7cbde74 | 8572 | fold_builtin_strlen (location_t loc, tree type, tree arg) |
e6e27594 | 8573 | { |
c2f47e15 | 8574 | if (!validate_arg (arg, POINTER_TYPE)) |
e6e27594 | 8575 | return NULL_TREE; |
8576 | else | |
8577 | { | |
98d5ba5d | 8578 | c_strlen_data lendata = { }; |
8579 | tree len = c_strlen (arg, 0, &lendata); | |
e6e27594 | 8580 | |
8581 | if (len) | |
c7cbde74 | 8582 | return fold_convert_loc (loc, type, len); |
e6e27594 | 8583 | |
98d5ba5d | 8584 | if (!lendata.decl) |
8585 | c_strlen (arg, 1, &lendata); | |
7af57b1c | 8586 | |
98d5ba5d | 8587 | if (lendata.decl) |
7af57b1c | 8588 | { |
8589 | if (EXPR_HAS_LOCATION (arg)) | |
8590 | loc = EXPR_LOCATION (arg); | |
8591 | else if (loc == UNKNOWN_LOCATION) | |
8592 | loc = input_location; | |
98d5ba5d | 8593 | warn_string_no_nul (loc, "strlen", arg, lendata.decl); |
7af57b1c | 8594 | } |
8595 | ||
e6e27594 | 8596 | return NULL_TREE; |
8597 | } | |
8598 | } | |
8599 | ||
92c43e3c | 8600 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
8601 | ||
8602 | static tree | |
389dd41b | 8603 | fold_builtin_inf (location_t loc, tree type, int warn) |
92c43e3c | 8604 | { |
aa870c1b | 8605 | REAL_VALUE_TYPE real; |
8606 | ||
40f4dbd5 | 8607 | /* __builtin_inff is intended to be usable to define INFINITY on all |
8608 | targets. If an infinity is not available, INFINITY expands "to a | |
8609 | positive constant of type float that overflows at translation | |
8610 | time", footnote "In this case, using INFINITY will violate the | |
8611 | constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4). | |
8612 | Thus we pedwarn to ensure this constraint violation is | |
8613 | diagnosed. */ | |
92c43e3c | 8614 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
389dd41b | 8615 | pedwarn (loc, 0, "target format does not support infinity"); |
92c43e3c | 8616 | |
aa870c1b | 8617 | real_inf (&real); |
8618 | return build_real (type, real); | |
92c43e3c | 8619 | } |
8620 | ||
d735c391 | 8621 | /* Fold function call to builtin sincos, sincosf, or sincosl. Return |
8622 | NULL_TREE if no simplification can be made. */ | |
8623 | ||
8624 | static tree | |
389dd41b | 8625 | fold_builtin_sincos (location_t loc, |
8626 | tree arg0, tree arg1, tree arg2) | |
d735c391 | 8627 | { |
c2f47e15 | 8628 | tree type; |
6c21be92 | 8629 | tree fndecl, call = NULL_TREE; |
d735c391 | 8630 | |
c2f47e15 | 8631 | if (!validate_arg (arg0, REAL_TYPE) |
8632 | || !validate_arg (arg1, POINTER_TYPE) | |
8633 | || !validate_arg (arg2, POINTER_TYPE)) | |
d735c391 | 8634 | return NULL_TREE; |
8635 | ||
d735c391 | 8636 | type = TREE_TYPE (arg0); |
d735c391 | 8637 | |
8638 | /* Calculate the result when the argument is a constant. */ | |
e3240774 | 8639 | built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI); |
6c21be92 | 8640 | if (fn == END_BUILTINS) |
d735c391 | 8641 | return NULL_TREE; |
8642 | ||
6c21be92 | 8643 | /* Canonicalize sincos to cexpi. */ |
8644 | if (TREE_CODE (arg0) == REAL_CST) | |
8645 | { | |
8646 | tree complex_type = build_complex_type (type); | |
744fe358 | 8647 | call = fold_const_call (as_combined_fn (fn), complex_type, arg0); |
6c21be92 | 8648 | } |
8649 | if (!call) | |
8650 | { | |
8651 | if (!targetm.libc_has_function (function_c99_math_complex) | |
8652 | || !builtin_decl_implicit_p (fn)) | |
8653 | return NULL_TREE; | |
8654 | fndecl = builtin_decl_explicit (fn); | |
8655 | call = build_call_expr_loc (loc, fndecl, 1, arg0); | |
8656 | call = builtin_save_expr (call); | |
8657 | } | |
d735c391 | 8658 | |
8234e9d3 | 8659 | tree ptype = build_pointer_type (type); |
8660 | arg1 = fold_convert (ptype, arg1); | |
8661 | arg2 = fold_convert (ptype, arg2); | |
a75b1c71 | 8662 | return build2 (COMPOUND_EXPR, void_type_node, |
d735c391 | 8663 | build2 (MODIFY_EXPR, void_type_node, |
389dd41b | 8664 | build_fold_indirect_ref_loc (loc, arg1), |
6c21be92 | 8665 | fold_build1_loc (loc, IMAGPART_EXPR, type, call)), |
d735c391 | 8666 | build2 (MODIFY_EXPR, void_type_node, |
389dd41b | 8667 | build_fold_indirect_ref_loc (loc, arg2), |
6c21be92 | 8668 | fold_build1_loc (loc, REALPART_EXPR, type, call))); |
d735c391 | 8669 | } |
8670 | ||
c2f47e15 | 8671 | /* Fold function call to builtin memcmp with arguments ARG1 and ARG2. |
8672 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 8673 | |
8674 | static tree | |
389dd41b | 8675 | fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len) |
9c8a1629 | 8676 | { |
c2f47e15 | 8677 | if (!validate_arg (arg1, POINTER_TYPE) |
8678 | || !validate_arg (arg2, POINTER_TYPE) | |
8679 | || !validate_arg (len, INTEGER_TYPE)) | |
8680 | return NULL_TREE; | |
9c8a1629 | 8681 | |
8682 | /* If the LEN parameter is zero, return zero. */ | |
8683 | if (integer_zerop (len)) | |
389dd41b | 8684 | return omit_two_operands_loc (loc, integer_type_node, integer_zero_node, |
c4fef134 | 8685 | arg1, arg2); |
9c8a1629 | 8686 | |
8687 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8688 | if (operand_equal_p (arg1, arg2, 0)) | |
389dd41b | 8689 | return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len); |
c4fef134 | 8690 | |
c4fef134 | 8691 | /* If len parameter is one, return an expression corresponding to |
8692 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
e913b5cd | 8693 | if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1) |
c4fef134 | 8694 | { |
8695 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8696 | tree cst_uchar_ptr_node |
8697 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8698 | ||
389dd41b | 8699 | tree ind1 |
8700 | = fold_convert_loc (loc, integer_type_node, | |
8701 | build1 (INDIRECT_REF, cst_uchar_node, | |
8702 | fold_convert_loc (loc, | |
8703 | cst_uchar_ptr_node, | |
c4fef134 | 8704 | arg1))); |
389dd41b | 8705 | tree ind2 |
8706 | = fold_convert_loc (loc, integer_type_node, | |
8707 | build1 (INDIRECT_REF, cst_uchar_node, | |
8708 | fold_convert_loc (loc, | |
8709 | cst_uchar_ptr_node, | |
c4fef134 | 8710 | arg2))); |
389dd41b | 8711 | return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2); |
c4fef134 | 8712 | } |
9c8a1629 | 8713 | |
c2f47e15 | 8714 | return NULL_TREE; |
9c8a1629 | 8715 | } |
8716 | ||
c2f47e15 | 8717 | /* Fold a call to builtin isascii with argument ARG. */ |
d49367d4 | 8718 | |
8719 | static tree | |
389dd41b | 8720 | fold_builtin_isascii (location_t loc, tree arg) |
d49367d4 | 8721 | { |
c2f47e15 | 8722 | if (!validate_arg (arg, INTEGER_TYPE)) |
8723 | return NULL_TREE; | |
d49367d4 | 8724 | else |
8725 | { | |
8726 | /* Transform isascii(c) -> ((c & ~0x7f) == 0). */ | |
c90b5d40 | 8727 | arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg, |
7002a1c8 | 8728 | build_int_cst (integer_type_node, |
c90b5d40 | 8729 | ~ (unsigned HOST_WIDE_INT) 0x7f)); |
389dd41b | 8730 | return fold_build2_loc (loc, EQ_EXPR, integer_type_node, |
7002a1c8 | 8731 | arg, integer_zero_node); |
d49367d4 | 8732 | } |
8733 | } | |
8734 | ||
c2f47e15 | 8735 | /* Fold a call to builtin toascii with argument ARG. */ |
d49367d4 | 8736 | |
8737 | static tree | |
389dd41b | 8738 | fold_builtin_toascii (location_t loc, tree arg) |
d49367d4 | 8739 | { |
c2f47e15 | 8740 | if (!validate_arg (arg, INTEGER_TYPE)) |
8741 | return NULL_TREE; | |
48e1416a | 8742 | |
c2f47e15 | 8743 | /* Transform toascii(c) -> (c & 0x7f). */ |
389dd41b | 8744 | return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg, |
7002a1c8 | 8745 | build_int_cst (integer_type_node, 0x7f)); |
d49367d4 | 8746 | } |
8747 | ||
c2f47e15 | 8748 | /* Fold a call to builtin isdigit with argument ARG. */ |
df1cf42e | 8749 | |
8750 | static tree | |
389dd41b | 8751 | fold_builtin_isdigit (location_t loc, tree arg) |
df1cf42e | 8752 | { |
c2f47e15 | 8753 | if (!validate_arg (arg, INTEGER_TYPE)) |
8754 | return NULL_TREE; | |
df1cf42e | 8755 | else |
8756 | { | |
8757 | /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ | |
624d37a6 | 8758 | /* According to the C standard, isdigit is unaffected by locale. |
8759 | However, it definitely is affected by the target character set. */ | |
624d37a6 | 8760 | unsigned HOST_WIDE_INT target_digit0 |
8761 | = lang_hooks.to_target_charset ('0'); | |
8762 | ||
8763 | if (target_digit0 == 0) | |
8764 | return NULL_TREE; | |
8765 | ||
389dd41b | 8766 | arg = fold_convert_loc (loc, unsigned_type_node, arg); |
c90b5d40 | 8767 | arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg, |
8768 | build_int_cst (unsigned_type_node, target_digit0)); | |
389dd41b | 8769 | return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg, |
f2532264 | 8770 | build_int_cst (unsigned_type_node, 9)); |
df1cf42e | 8771 | } |
8772 | } | |
27f261ef | 8773 | |
c2f47e15 | 8774 | /* Fold a call to fabs, fabsf or fabsl with argument ARG. */ |
d1aade50 | 8775 | |
8776 | static tree | |
389dd41b | 8777 | fold_builtin_fabs (location_t loc, tree arg, tree type) |
d1aade50 | 8778 | { |
c2f47e15 | 8779 | if (!validate_arg (arg, REAL_TYPE)) |
8780 | return NULL_TREE; | |
d1aade50 | 8781 | |
389dd41b | 8782 | arg = fold_convert_loc (loc, type, arg); |
389dd41b | 8783 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
d1aade50 | 8784 | } |
8785 | ||
c2f47e15 | 8786 | /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */ |
d1aade50 | 8787 | |
8788 | static tree | |
389dd41b | 8789 | fold_builtin_abs (location_t loc, tree arg, tree type) |
d1aade50 | 8790 | { |
c2f47e15 | 8791 | if (!validate_arg (arg, INTEGER_TYPE)) |
8792 | return NULL_TREE; | |
d1aade50 | 8793 | |
389dd41b | 8794 | arg = fold_convert_loc (loc, type, arg); |
389dd41b | 8795 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
d1aade50 | 8796 | } |
8797 | ||
abe4dcf6 | 8798 | /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */ |
8799 | ||
8800 | static tree | |
389dd41b | 8801 | fold_builtin_carg (location_t loc, tree arg, tree type) |
abe4dcf6 | 8802 | { |
239d491a | 8803 | if (validate_arg (arg, COMPLEX_TYPE) |
8804 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE) | |
abe4dcf6 | 8805 | { |
8806 | tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2); | |
48e1416a | 8807 | |
abe4dcf6 | 8808 | if (atan2_fn) |
8809 | { | |
c2f47e15 | 8810 | tree new_arg = builtin_save_expr (arg); |
389dd41b | 8811 | tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg); |
8812 | tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg); | |
8813 | return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg); | |
abe4dcf6 | 8814 | } |
8815 | } | |
48e1416a | 8816 | |
abe4dcf6 | 8817 | return NULL_TREE; |
8818 | } | |
8819 | ||
3838b9ae | 8820 | /* Fold a call to builtin frexp, we can assume the base is 2. */ |
8821 | ||
8822 | static tree | |
389dd41b | 8823 | fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype) |
3838b9ae | 8824 | { |
8825 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
8826 | return NULL_TREE; | |
48e1416a | 8827 | |
3838b9ae | 8828 | STRIP_NOPS (arg0); |
48e1416a | 8829 | |
3838b9ae | 8830 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
8831 | return NULL_TREE; | |
48e1416a | 8832 | |
389dd41b | 8833 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
3838b9ae | 8834 | |
8835 | /* Proceed if a valid pointer type was passed in. */ | |
8836 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node) | |
8837 | { | |
8838 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
8839 | tree frac, exp; | |
48e1416a | 8840 | |
3838b9ae | 8841 | switch (value->cl) |
8842 | { | |
8843 | case rvc_zero: | |
8844 | /* For +-0, return (*exp = 0, +-0). */ | |
8845 | exp = integer_zero_node; | |
8846 | frac = arg0; | |
8847 | break; | |
8848 | case rvc_nan: | |
8849 | case rvc_inf: | |
8850 | /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */ | |
389dd41b | 8851 | return omit_one_operand_loc (loc, rettype, arg0, arg1); |
3838b9ae | 8852 | case rvc_normal: |
8853 | { | |
8854 | /* Since the frexp function always expects base 2, and in | |
8855 | GCC normalized significands are already in the range | |
8856 | [0.5, 1.0), we have exactly what frexp wants. */ | |
8857 | REAL_VALUE_TYPE frac_rvt = *value; | |
8858 | SET_REAL_EXP (&frac_rvt, 0); | |
8859 | frac = build_real (rettype, frac_rvt); | |
7002a1c8 | 8860 | exp = build_int_cst (integer_type_node, REAL_EXP (value)); |
3838b9ae | 8861 | } |
8862 | break; | |
8863 | default: | |
8864 | gcc_unreachable (); | |
8865 | } | |
48e1416a | 8866 | |
3838b9ae | 8867 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
389dd41b | 8868 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp); |
3838b9ae | 8869 | TREE_SIDE_EFFECTS (arg1) = 1; |
389dd41b | 8870 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac); |
3838b9ae | 8871 | } |
8872 | ||
8873 | return NULL_TREE; | |
8874 | } | |
8875 | ||
ebf8b4f5 | 8876 | /* Fold a call to builtin modf. */ |
8877 | ||
8878 | static tree | |
389dd41b | 8879 | fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype) |
ebf8b4f5 | 8880 | { |
8881 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
8882 | return NULL_TREE; | |
48e1416a | 8883 | |
ebf8b4f5 | 8884 | STRIP_NOPS (arg0); |
48e1416a | 8885 | |
ebf8b4f5 | 8886 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
8887 | return NULL_TREE; | |
48e1416a | 8888 | |
389dd41b | 8889 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
ebf8b4f5 | 8890 | |
8891 | /* Proceed if a valid pointer type was passed in. */ | |
8892 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype)) | |
8893 | { | |
8894 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
8895 | REAL_VALUE_TYPE trunc, frac; | |
8896 | ||
8897 | switch (value->cl) | |
8898 | { | |
8899 | case rvc_nan: | |
8900 | case rvc_zero: | |
8901 | /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */ | |
8902 | trunc = frac = *value; | |
8903 | break; | |
8904 | case rvc_inf: | |
8905 | /* For +-Inf, return (*arg1 = arg0, +-0). */ | |
8906 | frac = dconst0; | |
8907 | frac.sign = value->sign; | |
8908 | trunc = *value; | |
8909 | break; | |
8910 | case rvc_normal: | |
8911 | /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */ | |
8912 | real_trunc (&trunc, VOIDmode, value); | |
8913 | real_arithmetic (&frac, MINUS_EXPR, value, &trunc); | |
8914 | /* If the original number was negative and already | |
8915 | integral, then the fractional part is -0.0. */ | |
8916 | if (value->sign && frac.cl == rvc_zero) | |
8917 | frac.sign = value->sign; | |
8918 | break; | |
8919 | } | |
48e1416a | 8920 | |
ebf8b4f5 | 8921 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
389dd41b | 8922 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, |
ebf8b4f5 | 8923 | build_real (rettype, trunc)); |
8924 | TREE_SIDE_EFFECTS (arg1) = 1; | |
389dd41b | 8925 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, |
ebf8b4f5 | 8926 | build_real (rettype, frac)); |
8927 | } | |
48e1416a | 8928 | |
ebf8b4f5 | 8929 | return NULL_TREE; |
8930 | } | |
8931 | ||
12f08300 | 8932 | /* Given a location LOC, an interclass builtin function decl FNDECL |
8933 | and its single argument ARG, return an folded expression computing | |
8934 | the same, or NULL_TREE if we either couldn't or didn't want to fold | |
8935 | (the latter happen if there's an RTL instruction available). */ | |
8936 | ||
8937 | static tree | |
8938 | fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg) | |
8939 | { | |
8940 | machine_mode mode; | |
8941 | ||
8942 | if (!validate_arg (arg, REAL_TYPE)) | |
8943 | return NULL_TREE; | |
8944 | ||
8945 | if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing) | |
8946 | return NULL_TREE; | |
8947 | ||
8948 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
8949 | ||
8950 | bool is_ibm_extended = MODE_COMPOSITE_P (mode); | |
7f38718f | 8951 | |
12f08300 | 8952 | /* If there is no optab, try generic code. */ |
8953 | switch (DECL_FUNCTION_CODE (fndecl)) | |
8954 | { | |
8955 | tree result; | |
a65c4d64 | 8956 | |
12f08300 | 8957 | CASE_FLT_FN (BUILT_IN_ISINF): |
8958 | { | |
8959 | /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */ | |
8960 | tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); | |
8961 | tree type = TREE_TYPE (arg); | |
8962 | REAL_VALUE_TYPE r; | |
8963 | char buf[128]; | |
8964 | ||
8965 | if (is_ibm_extended) | |
8966 | { | |
8967 | /* NaN and Inf are encoded in the high-order double value | |
8968 | only. The low-order value is not significant. */ | |
8969 | type = double_type_node; | |
8970 | mode = DFmode; | |
8971 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
8972 | } | |
8973 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
8974 | real_from_string (&r, buf); | |
8975 | result = build_call_expr (isgr_fn, 2, | |
8976 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
8977 | build_real (type, r)); | |
8978 | return result; | |
8979 | } | |
8980 | CASE_FLT_FN (BUILT_IN_FINITE): | |
8981 | case BUILT_IN_ISFINITE: | |
8982 | { | |
8983 | /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */ | |
8984 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); | |
8985 | tree type = TREE_TYPE (arg); | |
8986 | REAL_VALUE_TYPE r; | |
8987 | char buf[128]; | |
8988 | ||
8989 | if (is_ibm_extended) | |
8990 | { | |
8991 | /* NaN and Inf are encoded in the high-order double value | |
8992 | only. The low-order value is not significant. */ | |
8993 | type = double_type_node; | |
8994 | mode = DFmode; | |
8995 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
8996 | } | |
8997 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
8998 | real_from_string (&r, buf); | |
8999 | result = build_call_expr (isle_fn, 2, | |
9000 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
9001 | build_real (type, r)); | |
9002 | /*result = fold_build2_loc (loc, UNGT_EXPR, | |
9003 | TREE_TYPE (TREE_TYPE (fndecl)), | |
9004 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
9005 | build_real (type, r)); | |
9006 | result = fold_build1_loc (loc, TRUTH_NOT_EXPR, | |
9007 | TREE_TYPE (TREE_TYPE (fndecl)), | |
9008 | result);*/ | |
9009 | return result; | |
9010 | } | |
9011 | case BUILT_IN_ISNORMAL: | |
9012 | { | |
9013 | /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) & | |
9014 | islessequal(fabs(x),DBL_MAX). */ | |
9015 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); | |
9016 | tree type = TREE_TYPE (arg); | |
9017 | tree orig_arg, max_exp, min_exp; | |
9018 | machine_mode orig_mode = mode; | |
9019 | REAL_VALUE_TYPE rmax, rmin; | |
9020 | char buf[128]; | |
9021 | ||
9022 | orig_arg = arg = builtin_save_expr (arg); | |
9023 | if (is_ibm_extended) | |
9024 | { | |
9025 | /* Use double to test the normal range of IBM extended | |
9026 | precision. Emin for IBM extended precision is | |
9027 | different to emin for IEEE double, being 53 higher | |
9028 | since the low double exponent is at least 53 lower | |
9029 | than the high double exponent. */ | |
9030 | type = double_type_node; | |
9031 | mode = DFmode; | |
9032 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
9033 | } | |
9034 | arg = fold_build1_loc (loc, ABS_EXPR, type, arg); | |
9035 | ||
9036 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
9037 | real_from_string (&rmax, buf); | |
9038 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1); | |
9039 | real_from_string (&rmin, buf); | |
9040 | max_exp = build_real (type, rmax); | |
9041 | min_exp = build_real (type, rmin); | |
9042 | ||
9043 | max_exp = build_call_expr (isle_fn, 2, arg, max_exp); | |
9044 | if (is_ibm_extended) | |
9045 | { | |
9046 | /* Testing the high end of the range is done just using | |
9047 | the high double, using the same test as isfinite(). | |
9048 | For the subnormal end of the range we first test the | |
9049 | high double, then if its magnitude is equal to the | |
9050 | limit of 0x1p-969, we test whether the low double is | |
9051 | non-zero and opposite sign to the high double. */ | |
9052 | tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS); | |
9053 | tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); | |
9054 | tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp); | |
9055 | tree eq_min = fold_build2 (EQ_EXPR, integer_type_node, | |
9056 | arg, min_exp); | |
9057 | tree as_complex = build1 (VIEW_CONVERT_EXPR, | |
9058 | complex_double_type_node, orig_arg); | |
9059 | tree hi_dbl = build1 (REALPART_EXPR, type, as_complex); | |
9060 | tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex); | |
9061 | tree zero = build_real (type, dconst0); | |
9062 | tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero); | |
9063 | tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero); | |
9064 | tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero); | |
9065 | tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node, | |
9066 | fold_build3 (COND_EXPR, | |
9067 | integer_type_node, | |
9068 | hilt, logt, lolt)); | |
9069 | eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node, | |
9070 | eq_min, ok_lo); | |
9071 | min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node, | |
9072 | gt_min, eq_min); | |
9073 | } | |
9074 | else | |
9075 | { | |
9076 | tree const isge_fn | |
9077 | = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL); | |
9078 | min_exp = build_call_expr (isge_fn, 2, arg, min_exp); | |
9079 | } | |
9080 | result = fold_build2 (BIT_AND_EXPR, integer_type_node, | |
9081 | max_exp, min_exp); | |
9082 | return result; | |
9083 | } | |
9084 | default: | |
9085 | break; | |
9086 | } | |
9087 | ||
9088 | return NULL_TREE; | |
9089 | } | |
9090 | ||
9091 | /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite. | |
c2f47e15 | 9092 | ARG is the argument for the call. */ |
726069ba | 9093 | |
9094 | static tree | |
12f08300 | 9095 | fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index) |
726069ba | 9096 | { |
12f08300 | 9097 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
9098 | ||
c2f47e15 | 9099 | if (!validate_arg (arg, REAL_TYPE)) |
d43cee80 | 9100 | return NULL_TREE; |
726069ba | 9101 | |
726069ba | 9102 | switch (builtin_index) |
9103 | { | |
12f08300 | 9104 | case BUILT_IN_ISINF: |
9105 | if (!HONOR_INFINITIES (arg)) | |
9106 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); | |
9107 | ||
9108 | return NULL_TREE; | |
9109 | ||
c319d56a | 9110 | case BUILT_IN_ISINF_SIGN: |
9111 | { | |
9112 | /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */ | |
9113 | /* In a boolean context, GCC will fold the inner COND_EXPR to | |
9114 | 1. So e.g. "if (isinf_sign(x))" would be folded to just | |
9115 | "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */ | |
6cfc7001 | 9116 | tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT); |
b9a16870 | 9117 | tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF); |
c319d56a | 9118 | tree tmp = NULL_TREE; |
9119 | ||
9120 | arg = builtin_save_expr (arg); | |
9121 | ||
9122 | if (signbit_fn && isinf_fn) | |
9123 | { | |
389dd41b | 9124 | tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg); |
9125 | tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg); | |
c319d56a | 9126 | |
389dd41b | 9127 | signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
c319d56a | 9128 | signbit_call, integer_zero_node); |
389dd41b | 9129 | isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
c319d56a | 9130 | isinf_call, integer_zero_node); |
48e1416a | 9131 | |
389dd41b | 9132 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call, |
c319d56a | 9133 | integer_minus_one_node, integer_one_node); |
389dd41b | 9134 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, |
9135 | isinf_call, tmp, | |
c319d56a | 9136 | integer_zero_node); |
9137 | } | |
9138 | ||
9139 | return tmp; | |
9140 | } | |
9141 | ||
12f08300 | 9142 | case BUILT_IN_ISFINITE: |
9143 | if (!HONOR_NANS (arg) | |
9144 | && !HONOR_INFINITIES (arg)) | |
9145 | return omit_one_operand_loc (loc, type, integer_one_node, arg); | |
9146 | ||
9147 | return NULL_TREE; | |
9148 | ||
9149 | case BUILT_IN_ISNAN: | |
9150 | if (!HONOR_NANS (arg)) | |
9151 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); | |
9152 | ||
9153 | { | |
9154 | bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg))); | |
9155 | if (is_ibm_extended) | |
9156 | { | |
9157 | /* NaN and Inf are encoded in the high-order double value | |
9158 | only. The low-order value is not significant. */ | |
9159 | arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg); | |
9160 | } | |
9161 | } | |
9162 | arg = builtin_save_expr (arg); | |
9163 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg); | |
9164 | ||
726069ba | 9165 | default: |
64db345d | 9166 | gcc_unreachable (); |
726069ba | 9167 | } |
9168 | } | |
9169 | ||
12f08300 | 9170 | /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...). |
9171 | This builtin will generate code to return the appropriate floating | |
9172 | point classification depending on the value of the floating point | |
9173 | number passed in. The possible return values must be supplied as | |
9174 | int arguments to the call in the following order: FP_NAN, FP_INFINITE, | |
9175 | FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly | |
9176 | one floating point argument which is "type generic". */ | |
9177 | ||
9178 | static tree | |
9179 | fold_builtin_fpclassify (location_t loc, tree *args, int nargs) | |
9180 | { | |
9181 | tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero, | |
9182 | arg, type, res, tmp; | |
9183 | machine_mode mode; | |
9184 | REAL_VALUE_TYPE r; | |
9185 | char buf[128]; | |
9186 | ||
9187 | /* Verify the required arguments in the original call. */ | |
9188 | if (nargs != 6 | |
9189 | || !validate_arg (args[0], INTEGER_TYPE) | |
9190 | || !validate_arg (args[1], INTEGER_TYPE) | |
9191 | || !validate_arg (args[2], INTEGER_TYPE) | |
9192 | || !validate_arg (args[3], INTEGER_TYPE) | |
9193 | || !validate_arg (args[4], INTEGER_TYPE) | |
9194 | || !validate_arg (args[5], REAL_TYPE)) | |
9195 | return NULL_TREE; | |
9196 | ||
9197 | fp_nan = args[0]; | |
9198 | fp_infinite = args[1]; | |
9199 | fp_normal = args[2]; | |
9200 | fp_subnormal = args[3]; | |
9201 | fp_zero = args[4]; | |
9202 | arg = args[5]; | |
9203 | type = TREE_TYPE (arg); | |
9204 | mode = TYPE_MODE (type); | |
9205 | arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg)); | |
9206 | ||
9207 | /* fpclassify(x) -> | |
9208 | isnan(x) ? FP_NAN : | |
9209 | (fabs(x) == Inf ? FP_INFINITE : | |
9210 | (fabs(x) >= DBL_MIN ? FP_NORMAL : | |
9211 | (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */ | |
9212 | ||
9213 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, | |
9214 | build_real (type, dconst0)); | |
9215 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, | |
9216 | tmp, fp_zero, fp_subnormal); | |
9217 | ||
9218 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1); | |
9219 | real_from_string (&r, buf); | |
9220 | tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node, | |
9221 | arg, build_real (type, r)); | |
9222 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res); | |
9223 | ||
9224 | if (HONOR_INFINITIES (mode)) | |
9225 | { | |
9226 | real_inf (&r); | |
9227 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, | |
9228 | build_real (type, r)); | |
9229 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, | |
9230 | fp_infinite, res); | |
9231 | } | |
9232 | ||
9233 | if (HONOR_NANS (mode)) | |
9234 | { | |
9235 | tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg); | |
9236 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan); | |
9237 | } | |
9238 | ||
9239 | return res; | |
9240 | } | |
9241 | ||
9bc9f15f | 9242 | /* Fold a call to an unordered comparison function such as |
d5019fe8 | 9243 | __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function |
c2f47e15 | 9244 | being called and ARG0 and ARG1 are the arguments for the call. |
726069ba | 9245 | UNORDERED_CODE and ORDERED_CODE are comparison codes that give |
9246 | the opposite of the desired result. UNORDERED_CODE is used | |
9247 | for modes that can hold NaNs and ORDERED_CODE is used for | |
9248 | the rest. */ | |
9bc9f15f | 9249 | |
9250 | static tree | |
389dd41b | 9251 | fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1, |
9bc9f15f | 9252 | enum tree_code unordered_code, |
9253 | enum tree_code ordered_code) | |
9254 | { | |
859f903a | 9255 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
9bc9f15f | 9256 | enum tree_code code; |
6978db0d | 9257 | tree type0, type1; |
9258 | enum tree_code code0, code1; | |
9259 | tree cmp_type = NULL_TREE; | |
9bc9f15f | 9260 | |
6978db0d | 9261 | type0 = TREE_TYPE (arg0); |
9262 | type1 = TREE_TYPE (arg1); | |
a0c938f0 | 9263 | |
6978db0d | 9264 | code0 = TREE_CODE (type0); |
9265 | code1 = TREE_CODE (type1); | |
a0c938f0 | 9266 | |
6978db0d | 9267 | if (code0 == REAL_TYPE && code1 == REAL_TYPE) |
9268 | /* Choose the wider of two real types. */ | |
9269 | cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) | |
9270 | ? type0 : type1; | |
9271 | else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9272 | cmp_type = type0; | |
9273 | else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) | |
9274 | cmp_type = type1; | |
a0c938f0 | 9275 | |
389dd41b | 9276 | arg0 = fold_convert_loc (loc, cmp_type, arg0); |
9277 | arg1 = fold_convert_loc (loc, cmp_type, arg1); | |
859f903a | 9278 | |
9279 | if (unordered_code == UNORDERED_EXPR) | |
9280 | { | |
93633022 | 9281 | if (!HONOR_NANS (arg0)) |
389dd41b | 9282 | return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1); |
9283 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1); | |
859f903a | 9284 | } |
9bc9f15f | 9285 | |
93633022 | 9286 | code = HONOR_NANS (arg0) ? unordered_code : ordered_code; |
389dd41b | 9287 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, |
9288 | fold_build2_loc (loc, code, type, arg0, arg1)); | |
9bc9f15f | 9289 | } |
9290 | ||
0c93c8a9 | 9291 | /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal |
9292 | arithmetics if it can never overflow, or into internal functions that | |
9293 | return both result of arithmetics and overflowed boolean flag in | |
732905bb | 9294 | a complex integer result, or some other check for overflow. |
9295 | Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow | |
9296 | checking part of that. */ | |
0c93c8a9 | 9297 | |
9298 | static tree | |
9299 | fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode, | |
9300 | tree arg0, tree arg1, tree arg2) | |
9301 | { | |
9302 | enum internal_fn ifn = IFN_LAST; | |
02d7a132 | 9303 | /* The code of the expression corresponding to the built-in. */ |
732905bb | 9304 | enum tree_code opcode = ERROR_MARK; |
9305 | bool ovf_only = false; | |
9306 | ||
0c93c8a9 | 9307 | switch (fcode) |
9308 | { | |
732905bb | 9309 | case BUILT_IN_ADD_OVERFLOW_P: |
9310 | ovf_only = true; | |
9311 | /* FALLTHRU */ | |
0c93c8a9 | 9312 | case BUILT_IN_ADD_OVERFLOW: |
9313 | case BUILT_IN_SADD_OVERFLOW: | |
9314 | case BUILT_IN_SADDL_OVERFLOW: | |
9315 | case BUILT_IN_SADDLL_OVERFLOW: | |
9316 | case BUILT_IN_UADD_OVERFLOW: | |
9317 | case BUILT_IN_UADDL_OVERFLOW: | |
9318 | case BUILT_IN_UADDLL_OVERFLOW: | |
02d7a132 | 9319 | opcode = PLUS_EXPR; |
0c93c8a9 | 9320 | ifn = IFN_ADD_OVERFLOW; |
9321 | break; | |
732905bb | 9322 | case BUILT_IN_SUB_OVERFLOW_P: |
9323 | ovf_only = true; | |
9324 | /* FALLTHRU */ | |
0c93c8a9 | 9325 | case BUILT_IN_SUB_OVERFLOW: |
9326 | case BUILT_IN_SSUB_OVERFLOW: | |
9327 | case BUILT_IN_SSUBL_OVERFLOW: | |
9328 | case BUILT_IN_SSUBLL_OVERFLOW: | |
9329 | case BUILT_IN_USUB_OVERFLOW: | |
9330 | case BUILT_IN_USUBL_OVERFLOW: | |
9331 | case BUILT_IN_USUBLL_OVERFLOW: | |
02d7a132 | 9332 | opcode = MINUS_EXPR; |
0c93c8a9 | 9333 | ifn = IFN_SUB_OVERFLOW; |
9334 | break; | |
732905bb | 9335 | case BUILT_IN_MUL_OVERFLOW_P: |
9336 | ovf_only = true; | |
9337 | /* FALLTHRU */ | |
0c93c8a9 | 9338 | case BUILT_IN_MUL_OVERFLOW: |
9339 | case BUILT_IN_SMUL_OVERFLOW: | |
9340 | case BUILT_IN_SMULL_OVERFLOW: | |
9341 | case BUILT_IN_SMULLL_OVERFLOW: | |
9342 | case BUILT_IN_UMUL_OVERFLOW: | |
9343 | case BUILT_IN_UMULL_OVERFLOW: | |
9344 | case BUILT_IN_UMULLL_OVERFLOW: | |
02d7a132 | 9345 | opcode = MULT_EXPR; |
0c93c8a9 | 9346 | ifn = IFN_MUL_OVERFLOW; |
9347 | break; | |
9348 | default: | |
9349 | gcc_unreachable (); | |
9350 | } | |
732905bb | 9351 | |
9352 | /* For the "generic" overloads, the first two arguments can have different | |
9353 | types and the last argument determines the target type to use to check | |
9354 | for overflow. The arguments of the other overloads all have the same | |
9355 | type. */ | |
9356 | tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2)); | |
9357 | ||
9358 | /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two | |
9359 | arguments are constant, attempt to fold the built-in call into a constant | |
9360 | expression indicating whether or not it detected an overflow. */ | |
9361 | if (ovf_only | |
9362 | && TREE_CODE (arg0) == INTEGER_CST | |
9363 | && TREE_CODE (arg1) == INTEGER_CST) | |
9364 | /* Perform the computation in the target type and check for overflow. */ | |
9365 | return omit_one_operand_loc (loc, boolean_type_node, | |
9366 | arith_overflowed_p (opcode, type, arg0, arg1) | |
9367 | ? boolean_true_node : boolean_false_node, | |
9368 | arg2); | |
9369 | ||
02d7a132 | 9370 | tree intres, ovfres; |
9371 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
9372 | { | |
9373 | intres = fold_binary_loc (loc, opcode, type, | |
9374 | fold_convert_loc (loc, type, arg0), | |
9375 | fold_convert_loc (loc, type, arg1)); | |
9376 | if (TREE_OVERFLOW (intres)) | |
9377 | intres = drop_tree_overflow (intres); | |
9378 | ovfres = (arith_overflowed_p (opcode, type, arg0, arg1) | |
9379 | ? boolean_true_node : boolean_false_node); | |
9380 | } | |
9381 | else | |
9382 | { | |
9383 | tree ctype = build_complex_type (type); | |
9384 | tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2, | |
9385 | arg0, arg1); | |
9386 | tree tgt = save_expr (call); | |
9387 | intres = build1_loc (loc, REALPART_EXPR, type, tgt); | |
9388 | ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt); | |
9389 | ovfres = fold_convert_loc (loc, boolean_type_node, ovfres); | |
9390 | } | |
732905bb | 9391 | |
9392 | if (ovf_only) | |
9393 | return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2); | |
9394 | ||
9395 | tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2); | |
0c93c8a9 | 9396 | tree store |
9397 | = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres); | |
9398 | return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres); | |
9399 | } | |
9400 | ||
c388a0cf | 9401 | /* Fold a call to __builtin_FILE to a constant string. */ |
9402 | ||
9403 | static inline tree | |
9404 | fold_builtin_FILE (location_t loc) | |
9405 | { | |
9406 | if (const char *fname = LOCATION_FILE (loc)) | |
859b51f8 | 9407 | { |
9408 | /* The documentation says this builtin is equivalent to the preprocessor | |
9409 | __FILE__ macro so it appears appropriate to use the same file prefix | |
9410 | mappings. */ | |
9411 | fname = remap_macro_filename (fname); | |
c388a0cf | 9412 | return build_string_literal (strlen (fname) + 1, fname); |
859b51f8 | 9413 | } |
c388a0cf | 9414 | |
9415 | return build_string_literal (1, ""); | |
9416 | } | |
9417 | ||
9418 | /* Fold a call to __builtin_FUNCTION to a constant string. */ | |
9419 | ||
9420 | static inline tree | |
9421 | fold_builtin_FUNCTION () | |
9422 | { | |
c2d38635 | 9423 | const char *name = ""; |
9424 | ||
c388a0cf | 9425 | if (current_function_decl) |
c2d38635 | 9426 | name = lang_hooks.decl_printable_name (current_function_decl, 0); |
c388a0cf | 9427 | |
c2d38635 | 9428 | return build_string_literal (strlen (name) + 1, name); |
c388a0cf | 9429 | } |
9430 | ||
9431 | /* Fold a call to __builtin_LINE to an integer constant. */ | |
9432 | ||
9433 | static inline tree | |
9434 | fold_builtin_LINE (location_t loc, tree type) | |
9435 | { | |
9436 | return build_int_cst (type, LOCATION_LINE (loc)); | |
9437 | } | |
9438 | ||
c2f47e15 | 9439 | /* Fold a call to built-in function FNDECL with 0 arguments. |
e80cc485 | 9440 | This function returns NULL_TREE if no simplification was possible. */ |
650e4c94 | 9441 | |
4ee9c684 | 9442 | static tree |
e80cc485 | 9443 | fold_builtin_0 (location_t loc, tree fndecl) |
650e4c94 | 9444 | { |
e9f80ff5 | 9445 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
c2f47e15 | 9446 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
189b3398 | 9447 | switch (fcode) |
650e4c94 | 9448 | { |
c388a0cf | 9449 | case BUILT_IN_FILE: |
9450 | return fold_builtin_FILE (loc); | |
9451 | ||
9452 | case BUILT_IN_FUNCTION: | |
9453 | return fold_builtin_FUNCTION (); | |
9454 | ||
9455 | case BUILT_IN_LINE: | |
9456 | return fold_builtin_LINE (loc, type); | |
9457 | ||
c2f47e15 | 9458 | CASE_FLT_FN (BUILT_IN_INF): |
012f068a | 9459 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF): |
c2f47e15 | 9460 | case BUILT_IN_INFD32: |
9461 | case BUILT_IN_INFD64: | |
9462 | case BUILT_IN_INFD128: | |
389dd41b | 9463 | return fold_builtin_inf (loc, type, true); |
7c2f0500 | 9464 | |
c2f47e15 | 9465 | CASE_FLT_FN (BUILT_IN_HUGE_VAL): |
012f068a | 9466 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL): |
389dd41b | 9467 | return fold_builtin_inf (loc, type, false); |
7c2f0500 | 9468 | |
c2f47e15 | 9469 | case BUILT_IN_CLASSIFY_TYPE: |
9470 | return fold_builtin_classify_type (NULL_TREE); | |
7c2f0500 | 9471 | |
c2f47e15 | 9472 | default: |
9473 | break; | |
9474 | } | |
9475 | return NULL_TREE; | |
9476 | } | |
7c2f0500 | 9477 | |
c2f47e15 | 9478 | /* Fold a call to built-in function FNDECL with 1 argument, ARG0. |
e80cc485 | 9479 | This function returns NULL_TREE if no simplification was possible. */ |
7c2f0500 | 9480 | |
c2f47e15 | 9481 | static tree |
e80cc485 | 9482 | fold_builtin_1 (location_t loc, tree fndecl, tree arg0) |
c2f47e15 | 9483 | { |
9484 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
9485 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6c21be92 | 9486 | |
9487 | if (TREE_CODE (arg0) == ERROR_MARK) | |
9488 | return NULL_TREE; | |
9489 | ||
744fe358 | 9490 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0)) |
6c21be92 | 9491 | return ret; |
9492 | ||
c2f47e15 | 9493 | switch (fcode) |
9494 | { | |
650e4c94 | 9495 | case BUILT_IN_CONSTANT_P: |
7c2f0500 | 9496 | { |
c2f47e15 | 9497 | tree val = fold_builtin_constant_p (arg0); |
7c2f0500 | 9498 | |
7c2f0500 | 9499 | /* Gimplification will pull the CALL_EXPR for the builtin out of |
9500 | an if condition. When not optimizing, we'll not CSE it back. | |
9501 | To avoid link error types of regressions, return false now. */ | |
9502 | if (!val && !optimize) | |
9503 | val = integer_zero_node; | |
9504 | ||
9505 | return val; | |
9506 | } | |
650e4c94 | 9507 | |
539a3a92 | 9508 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 9509 | return fold_builtin_classify_type (arg0); |
539a3a92 | 9510 | |
650e4c94 | 9511 | case BUILT_IN_STRLEN: |
c7cbde74 | 9512 | return fold_builtin_strlen (loc, type, arg0); |
650e4c94 | 9513 | |
4f35b1fc | 9514 | CASE_FLT_FN (BUILT_IN_FABS): |
012f068a | 9515 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS): |
8aa32773 | 9516 | case BUILT_IN_FABSD32: |
9517 | case BUILT_IN_FABSD64: | |
9518 | case BUILT_IN_FABSD128: | |
389dd41b | 9519 | return fold_builtin_fabs (loc, arg0, type); |
d1aade50 | 9520 | |
9521 | case BUILT_IN_ABS: | |
9522 | case BUILT_IN_LABS: | |
9523 | case BUILT_IN_LLABS: | |
9524 | case BUILT_IN_IMAXABS: | |
389dd41b | 9525 | return fold_builtin_abs (loc, arg0, type); |
c63f4ad3 | 9526 | |
4f35b1fc | 9527 | CASE_FLT_FN (BUILT_IN_CONJ): |
239d491a | 9528 | if (validate_arg (arg0, COMPLEX_TYPE) |
48e1416a | 9529 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
389dd41b | 9530 | return fold_build1_loc (loc, CONJ_EXPR, type, arg0); |
c2f47e15 | 9531 | break; |
36d3581d | 9532 | |
4f35b1fc | 9533 | CASE_FLT_FN (BUILT_IN_CREAL): |
239d491a | 9534 | if (validate_arg (arg0, COMPLEX_TYPE) |
48e1416a | 9535 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
7082509e | 9536 | return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0)); |
c2f47e15 | 9537 | break; |
36d3581d | 9538 | |
4f35b1fc | 9539 | CASE_FLT_FN (BUILT_IN_CIMAG): |
b0ce8887 | 9540 | if (validate_arg (arg0, COMPLEX_TYPE) |
9541 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) | |
389dd41b | 9542 | return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0)); |
c2f47e15 | 9543 | break; |
36d3581d | 9544 | |
6c21be92 | 9545 | CASE_FLT_FN (BUILT_IN_CARG): |
9546 | return fold_builtin_carg (loc, arg0, type); | |
c2373fdb | 9547 | |
6c21be92 | 9548 | case BUILT_IN_ISASCII: |
9549 | return fold_builtin_isascii (loc, arg0); | |
48e1416a | 9550 | |
6c21be92 | 9551 | case BUILT_IN_TOASCII: |
9552 | return fold_builtin_toascii (loc, arg0); | |
48e1416a | 9553 | |
6c21be92 | 9554 | case BUILT_IN_ISDIGIT: |
9555 | return fold_builtin_isdigit (loc, arg0); | |
48e1416a | 9556 | |
12f08300 | 9557 | CASE_FLT_FN (BUILT_IN_FINITE): |
9558 | case BUILT_IN_FINITED32: | |
9559 | case BUILT_IN_FINITED64: | |
9560 | case BUILT_IN_FINITED128: | |
9561 | case BUILT_IN_ISFINITE: | |
9562 | { | |
9563 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE); | |
9564 | if (ret) | |
9565 | return ret; | |
9566 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
9567 | } | |
9568 | ||
9569 | CASE_FLT_FN (BUILT_IN_ISINF): | |
9570 | case BUILT_IN_ISINFD32: | |
9571 | case BUILT_IN_ISINFD64: | |
9572 | case BUILT_IN_ISINFD128: | |
9573 | { | |
9574 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF); | |
9575 | if (ret) | |
9576 | return ret; | |
9577 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
9578 | } | |
9579 | ||
9580 | case BUILT_IN_ISNORMAL: | |
9581 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
9582 | ||
6c21be92 | 9583 | case BUILT_IN_ISINF_SIGN: |
12f08300 | 9584 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN); |
9585 | ||
9586 | CASE_FLT_FN (BUILT_IN_ISNAN): | |
9587 | case BUILT_IN_ISNAND32: | |
9588 | case BUILT_IN_ISNAND64: | |
9589 | case BUILT_IN_ISNAND128: | |
9590 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN); | |
48e1416a | 9591 | |
6c21be92 | 9592 | case BUILT_IN_FREE: |
9593 | if (integer_zerop (arg0)) | |
9594 | return build_empty_stmt (loc); | |
d064d976 | 9595 | break; |
c63f4ad3 | 9596 | |
6c21be92 | 9597 | default: |
8b4af95f | 9598 | break; |
6c21be92 | 9599 | } |
805e22b2 | 9600 | |
6c21be92 | 9601 | return NULL_TREE; |
3bc5c41b | 9602 | |
6c21be92 | 9603 | } |
728bac60 | 9604 | |
6c21be92 | 9605 | /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1. |
9606 | This function returns NULL_TREE if no simplification was possible. */ | |
c2f47e15 | 9607 | |
9608 | static tree | |
e80cc485 | 9609 | fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1) |
c2f47e15 | 9610 | { |
9611 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
9612 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
9613 | ||
6c21be92 | 9614 | if (TREE_CODE (arg0) == ERROR_MARK |
9615 | || TREE_CODE (arg1) == ERROR_MARK) | |
9616 | return NULL_TREE; | |
e5407ca6 | 9617 | |
744fe358 | 9618 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1)) |
6c21be92 | 9619 | return ret; |
e84da7c1 | 9620 | |
6c21be92 | 9621 | switch (fcode) |
9622 | { | |
e84da7c1 | 9623 | CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */ |
9624 | CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */ | |
9625 | if (validate_arg (arg0, REAL_TYPE) | |
9af5ce0c | 9626 | && validate_arg (arg1, POINTER_TYPE)) |
e84da7c1 | 9627 | return do_mpfr_lgamma_r (arg0, arg1, type); |
9628 | break; | |
c2f47e15 | 9629 | |
3838b9ae | 9630 | CASE_FLT_FN (BUILT_IN_FREXP): |
389dd41b | 9631 | return fold_builtin_frexp (loc, arg0, arg1, type); |
3838b9ae | 9632 | |
ebf8b4f5 | 9633 | CASE_FLT_FN (BUILT_IN_MODF): |
389dd41b | 9634 | return fold_builtin_modf (loc, arg0, arg1, type); |
ebf8b4f5 | 9635 | |
c2f47e15 | 9636 | case BUILT_IN_STRSPN: |
389dd41b | 9637 | return fold_builtin_strspn (loc, arg0, arg1); |
c2f47e15 | 9638 | |
9639 | case BUILT_IN_STRCSPN: | |
389dd41b | 9640 | return fold_builtin_strcspn (loc, arg0, arg1); |
c2f47e15 | 9641 | |
c2f47e15 | 9642 | case BUILT_IN_STRPBRK: |
389dd41b | 9643 | return fold_builtin_strpbrk (loc, arg0, arg1, type); |
c2f47e15 | 9644 | |
9645 | case BUILT_IN_EXPECT: | |
01107f42 | 9646 | return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE); |
c2f47e15 | 9647 | |
9bc9f15f | 9648 | case BUILT_IN_ISGREATER: |
389dd41b | 9649 | return fold_builtin_unordered_cmp (loc, fndecl, |
9650 | arg0, arg1, UNLE_EXPR, LE_EXPR); | |
9bc9f15f | 9651 | case BUILT_IN_ISGREATEREQUAL: |
389dd41b | 9652 | return fold_builtin_unordered_cmp (loc, fndecl, |
9653 | arg0, arg1, UNLT_EXPR, LT_EXPR); | |
9bc9f15f | 9654 | case BUILT_IN_ISLESS: |
389dd41b | 9655 | return fold_builtin_unordered_cmp (loc, fndecl, |
9656 | arg0, arg1, UNGE_EXPR, GE_EXPR); | |
9bc9f15f | 9657 | case BUILT_IN_ISLESSEQUAL: |
389dd41b | 9658 | return fold_builtin_unordered_cmp (loc, fndecl, |
9659 | arg0, arg1, UNGT_EXPR, GT_EXPR); | |
9bc9f15f | 9660 | case BUILT_IN_ISLESSGREATER: |
389dd41b | 9661 | return fold_builtin_unordered_cmp (loc, fndecl, |
9662 | arg0, arg1, UNEQ_EXPR, EQ_EXPR); | |
9bc9f15f | 9663 | case BUILT_IN_ISUNORDERED: |
389dd41b | 9664 | return fold_builtin_unordered_cmp (loc, fndecl, |
9665 | arg0, arg1, UNORDERED_EXPR, | |
d5019fe8 | 9666 | NOP_EXPR); |
9bc9f15f | 9667 | |
7c2f0500 | 9668 | /* We do the folding for va_start in the expander. */ |
9669 | case BUILT_IN_VA_START: | |
9670 | break; | |
f0613857 | 9671 | |
0a39fd54 | 9672 | case BUILT_IN_OBJECT_SIZE: |
c2f47e15 | 9673 | return fold_builtin_object_size (arg0, arg1); |
0a39fd54 | 9674 | |
1cd6e20d | 9675 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: |
9676 | return fold_builtin_atomic_always_lock_free (arg0, arg1); | |
9677 | ||
9678 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
9679 | return fold_builtin_atomic_is_lock_free (arg0, arg1); | |
9680 | ||
c2f47e15 | 9681 | default: |
9682 | break; | |
9683 | } | |
9684 | return NULL_TREE; | |
9685 | } | |
9686 | ||
9687 | /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1, | |
e80cc485 | 9688 | and ARG2. |
c2f47e15 | 9689 | This function returns NULL_TREE if no simplification was possible. */ |
9690 | ||
9691 | static tree | |
389dd41b | 9692 | fold_builtin_3 (location_t loc, tree fndecl, |
e80cc485 | 9693 | tree arg0, tree arg1, tree arg2) |
c2f47e15 | 9694 | { |
9695 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
9696 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6c21be92 | 9697 | |
9698 | if (TREE_CODE (arg0) == ERROR_MARK | |
9699 | || TREE_CODE (arg1) == ERROR_MARK | |
9700 | || TREE_CODE (arg2) == ERROR_MARK) | |
9701 | return NULL_TREE; | |
9702 | ||
744fe358 | 9703 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, |
9704 | arg0, arg1, arg2)) | |
6c21be92 | 9705 | return ret; |
9706 | ||
c2f47e15 | 9707 | switch (fcode) |
9708 | { | |
9709 | ||
9710 | CASE_FLT_FN (BUILT_IN_SINCOS): | |
389dd41b | 9711 | return fold_builtin_sincos (loc, arg0, arg1, arg2); |
c2f47e15 | 9712 | |
e5407ca6 | 9713 | CASE_FLT_FN (BUILT_IN_REMQUO): |
9714 | if (validate_arg (arg0, REAL_TYPE) | |
9af5ce0c | 9715 | && validate_arg (arg1, REAL_TYPE) |
9716 | && validate_arg (arg2, POINTER_TYPE)) | |
e5407ca6 | 9717 | return do_mpfr_remquo (arg0, arg1, arg2); |
9718 | break; | |
e5407ca6 | 9719 | |
c2f47e15 | 9720 | case BUILT_IN_MEMCMP: |
7f38a6aa | 9721 | return fold_builtin_memcmp (loc, arg0, arg1, arg2); |
c2f47e15 | 9722 | |
c83059be | 9723 | case BUILT_IN_EXPECT: |
01107f42 | 9724 | return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE); |
9725 | ||
9726 | case BUILT_IN_EXPECT_WITH_PROBABILITY: | |
9727 | return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2); | |
c83059be | 9728 | |
0c93c8a9 | 9729 | case BUILT_IN_ADD_OVERFLOW: |
9730 | case BUILT_IN_SUB_OVERFLOW: | |
9731 | case BUILT_IN_MUL_OVERFLOW: | |
732905bb | 9732 | case BUILT_IN_ADD_OVERFLOW_P: |
9733 | case BUILT_IN_SUB_OVERFLOW_P: | |
9734 | case BUILT_IN_MUL_OVERFLOW_P: | |
0c93c8a9 | 9735 | case BUILT_IN_SADD_OVERFLOW: |
9736 | case BUILT_IN_SADDL_OVERFLOW: | |
9737 | case BUILT_IN_SADDLL_OVERFLOW: | |
9738 | case BUILT_IN_SSUB_OVERFLOW: | |
9739 | case BUILT_IN_SSUBL_OVERFLOW: | |
9740 | case BUILT_IN_SSUBLL_OVERFLOW: | |
9741 | case BUILT_IN_SMUL_OVERFLOW: | |
9742 | case BUILT_IN_SMULL_OVERFLOW: | |
9743 | case BUILT_IN_SMULLL_OVERFLOW: | |
9744 | case BUILT_IN_UADD_OVERFLOW: | |
9745 | case BUILT_IN_UADDL_OVERFLOW: | |
9746 | case BUILT_IN_UADDLL_OVERFLOW: | |
9747 | case BUILT_IN_USUB_OVERFLOW: | |
9748 | case BUILT_IN_USUBL_OVERFLOW: | |
9749 | case BUILT_IN_USUBLL_OVERFLOW: | |
9750 | case BUILT_IN_UMUL_OVERFLOW: | |
9751 | case BUILT_IN_UMULL_OVERFLOW: | |
9752 | case BUILT_IN_UMULLL_OVERFLOW: | |
9753 | return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2); | |
9754 | ||
650e4c94 | 9755 | default: |
9756 | break; | |
9757 | } | |
c2f47e15 | 9758 | return NULL_TREE; |
9759 | } | |
650e4c94 | 9760 | |
c2f47e15 | 9761 | /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS |
9d884767 | 9762 | arguments. IGNORE is true if the result of the |
9763 | function call is ignored. This function returns NULL_TREE if no | |
9764 | simplification was possible. */ | |
48e1416a | 9765 | |
2165588a | 9766 | tree |
e80cc485 | 9767 | fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool) |
c2f47e15 | 9768 | { |
9769 | tree ret = NULL_TREE; | |
a7f5bb2d | 9770 | |
c2f47e15 | 9771 | switch (nargs) |
9772 | { | |
9773 | case 0: | |
e80cc485 | 9774 | ret = fold_builtin_0 (loc, fndecl); |
c2f47e15 | 9775 | break; |
9776 | case 1: | |
e80cc485 | 9777 | ret = fold_builtin_1 (loc, fndecl, args[0]); |
c2f47e15 | 9778 | break; |
9779 | case 2: | |
e80cc485 | 9780 | ret = fold_builtin_2 (loc, fndecl, args[0], args[1]); |
c2f47e15 | 9781 | break; |
9782 | case 3: | |
e80cc485 | 9783 | ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]); |
c2f47e15 | 9784 | break; |
c2f47e15 | 9785 | default: |
12f08300 | 9786 | ret = fold_builtin_varargs (loc, fndecl, args, nargs); |
c2f47e15 | 9787 | break; |
9788 | } | |
9789 | if (ret) | |
9790 | { | |
75a70cf9 | 9791 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); |
389dd41b | 9792 | SET_EXPR_LOCATION (ret, loc); |
c2f47e15 | 9793 | return ret; |
9794 | } | |
9795 | return NULL_TREE; | |
9796 | } | |
9797 | ||
0e80b01d | 9798 | /* Construct a new CALL_EXPR to FNDECL using the tail of the argument |
9799 | list ARGS along with N new arguments in NEWARGS. SKIP is the number | |
9800 | of arguments in ARGS to be omitted. OLDNARGS is the number of | |
9801 | elements in ARGS. */ | |
c2f47e15 | 9802 | |
9803 | static tree | |
0e80b01d | 9804 | rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args, |
9805 | int skip, tree fndecl, int n, va_list newargs) | |
c2f47e15 | 9806 | { |
0e80b01d | 9807 | int nargs = oldnargs - skip + n; |
9808 | tree *buffer; | |
c2f47e15 | 9809 | |
0e80b01d | 9810 | if (n > 0) |
c2f47e15 | 9811 | { |
0e80b01d | 9812 | int i, j; |
c2f47e15 | 9813 | |
0e80b01d | 9814 | buffer = XALLOCAVEC (tree, nargs); |
9815 | for (i = 0; i < n; i++) | |
9816 | buffer[i] = va_arg (newargs, tree); | |
9817 | for (j = skip; j < oldnargs; j++, i++) | |
9818 | buffer[i] = args[j]; | |
9819 | } | |
9820 | else | |
9821 | buffer = args + skip; | |
19fbe3a4 | 9822 | |
0e80b01d | 9823 | return build_call_expr_loc_array (loc, fndecl, nargs, buffer); |
9824 | } | |
c2f47e15 | 9825 | |
198622c0 | 9826 | /* Return true if FNDECL shouldn't be folded right now. |
9827 | If a built-in function has an inline attribute always_inline | |
9828 | wrapper, defer folding it after always_inline functions have | |
9829 | been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking | |
9830 | might not be performed. */ | |
9831 | ||
51d2c51e | 9832 | bool |
198622c0 | 9833 | avoid_folding_inline_builtin (tree fndecl) |
9834 | { | |
9835 | return (DECL_DECLARED_INLINE_P (fndecl) | |
9836 | && DECL_DISREGARD_INLINE_LIMITS (fndecl) | |
9837 | && cfun | |
9838 | && !cfun->always_inline_functions_inlined | |
9839 | && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl))); | |
9840 | } | |
9841 | ||
4ee9c684 | 9842 | /* A wrapper function for builtin folding that prevents warnings for |
491e04ef | 9843 | "statement without effect" and the like, caused by removing the |
4ee9c684 | 9844 | call node earlier than the warning is generated. */ |
9845 | ||
9846 | tree | |
389dd41b | 9847 | fold_call_expr (location_t loc, tree exp, bool ignore) |
4ee9c684 | 9848 | { |
c2f47e15 | 9849 | tree ret = NULL_TREE; |
9850 | tree fndecl = get_callee_fndecl (exp); | |
a0e9bfbb | 9851 | if (fndecl && fndecl_built_in_p (fndecl) |
48dc2227 | 9852 | /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized |
9853 | yet. Defer folding until we see all the arguments | |
9854 | (after inlining). */ | |
9855 | && !CALL_EXPR_VA_ARG_PACK (exp)) | |
9856 | { | |
9857 | int nargs = call_expr_nargs (exp); | |
9858 | ||
9859 | /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but | |
9860 | instead last argument is __builtin_va_arg_pack (). Defer folding | |
9861 | even in that case, until arguments are finalized. */ | |
9862 | if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR) | |
9863 | { | |
9864 | tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1)); | |
a0e9bfbb | 9865 | if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK)) |
48dc2227 | 9866 | return NULL_TREE; |
9867 | } | |
9868 | ||
198622c0 | 9869 | if (avoid_folding_inline_builtin (fndecl)) |
9870 | return NULL_TREE; | |
9871 | ||
c2f47e15 | 9872 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
97d67146 | 9873 | return targetm.fold_builtin (fndecl, call_expr_nargs (exp), |
9874 | CALL_EXPR_ARGP (exp), ignore); | |
c2f47e15 | 9875 | else |
9876 | { | |
9d884767 | 9877 | tree *args = CALL_EXPR_ARGP (exp); |
9878 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); | |
c2f47e15 | 9879 | if (ret) |
389dd41b | 9880 | return ret; |
c2f47e15 | 9881 | } |
4ee9c684 | 9882 | } |
c2f47e15 | 9883 | return NULL_TREE; |
9884 | } | |
48e1416a | 9885 | |
9d884767 | 9886 | /* Fold a CALL_EXPR with type TYPE with FN as the function expression. |
9887 | N arguments are passed in the array ARGARRAY. Return a folded | |
9888 | expression or NULL_TREE if no simplification was possible. */ | |
805e22b2 | 9889 | |
9890 | tree | |
9d884767 | 9891 | fold_builtin_call_array (location_t loc, tree, |
d01f58f9 | 9892 | tree fn, |
9893 | int n, | |
9894 | tree *argarray) | |
7e15618b | 9895 | { |
9d884767 | 9896 | if (TREE_CODE (fn) != ADDR_EXPR) |
9897 | return NULL_TREE; | |
c2f47e15 | 9898 | |
9d884767 | 9899 | tree fndecl = TREE_OPERAND (fn, 0); |
9900 | if (TREE_CODE (fndecl) == FUNCTION_DECL | |
a0e9bfbb | 9901 | && fndecl_built_in_p (fndecl)) |
9d884767 | 9902 | { |
9903 | /* If last argument is __builtin_va_arg_pack (), arguments to this | |
9904 | function are not finalized yet. Defer folding until they are. */ | |
9905 | if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR) | |
9906 | { | |
9907 | tree fndecl2 = get_callee_fndecl (argarray[n - 1]); | |
a0e9bfbb | 9908 | if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK)) |
9d884767 | 9909 | return NULL_TREE; |
9910 | } | |
9911 | if (avoid_folding_inline_builtin (fndecl)) | |
9912 | return NULL_TREE; | |
9913 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
9914 | return targetm.fold_builtin (fndecl, n, argarray, false); | |
9915 | else | |
9916 | return fold_builtin_n (loc, fndecl, argarray, n, false); | |
9917 | } | |
c2f47e15 | 9918 | |
9d884767 | 9919 | return NULL_TREE; |
c2f47e15 | 9920 | } |
9921 | ||
af1409ad | 9922 | /* Construct a new CALL_EXPR using the tail of the argument list of EXP |
9923 | along with N new arguments specified as the "..." parameters. SKIP | |
9924 | is the number of arguments in EXP to be omitted. This function is used | |
9925 | to do varargs-to-varargs transformations. */ | |
9926 | ||
9927 | static tree | |
9928 | rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...) | |
9929 | { | |
9930 | va_list ap; | |
9931 | tree t; | |
9932 | ||
9933 | va_start (ap, n); | |
9934 | t = rewrite_call_expr_valist (loc, call_expr_nargs (exp), | |
9935 | CALL_EXPR_ARGP (exp), skip, fndecl, n, ap); | |
9936 | va_end (ap); | |
c2f47e15 | 9937 | |
af1409ad | 9938 | return t; |
c2f47e15 | 9939 | } |
9940 | ||
9941 | /* Validate a single argument ARG against a tree code CODE representing | |
184fac50 | 9942 | a type. Return true when argument is valid. */ |
48e1416a | 9943 | |
c2f47e15 | 9944 | static bool |
184fac50 | 9945 | validate_arg (const_tree arg, enum tree_code code) |
c2f47e15 | 9946 | { |
9947 | if (!arg) | |
9948 | return false; | |
9949 | else if (code == POINTER_TYPE) | |
184fac50 | 9950 | return POINTER_TYPE_P (TREE_TYPE (arg)); |
c7f617c2 | 9951 | else if (code == INTEGER_TYPE) |
9952 | return INTEGRAL_TYPE_P (TREE_TYPE (arg)); | |
c2f47e15 | 9953 | return code == TREE_CODE (TREE_TYPE (arg)); |
7e15618b | 9954 | } |
0eb671f7 | 9955 | |
75a70cf9 | 9956 | /* This function validates the types of a function call argument list |
9957 | against a specified list of tree_codes. If the last specifier is a 0, | |
9958 | that represents an ellipses, otherwise the last specifier must be a | |
9959 | VOID_TYPE. | |
9960 | ||
9961 | This is the GIMPLE version of validate_arglist. Eventually we want to | |
9962 | completely convert builtins.c to work from GIMPLEs and the tree based | |
9963 | validate_arglist will then be removed. */ | |
9964 | ||
9965 | bool | |
1a91d914 | 9966 | validate_gimple_arglist (const gcall *call, ...) |
75a70cf9 | 9967 | { |
9968 | enum tree_code code; | |
9969 | bool res = 0; | |
9970 | va_list ap; | |
9971 | const_tree arg; | |
9972 | size_t i; | |
9973 | ||
9974 | va_start (ap, call); | |
9975 | i = 0; | |
9976 | ||
9977 | do | |
9978 | { | |
d62e827b | 9979 | code = (enum tree_code) va_arg (ap, int); |
75a70cf9 | 9980 | switch (code) |
9981 | { | |
9982 | case 0: | |
9983 | /* This signifies an ellipses, any further arguments are all ok. */ | |
9984 | res = true; | |
9985 | goto end; | |
9986 | case VOID_TYPE: | |
9987 | /* This signifies an endlink, if no arguments remain, return | |
9988 | true, otherwise return false. */ | |
9989 | res = (i == gimple_call_num_args (call)); | |
9990 | goto end; | |
9991 | default: | |
9992 | /* If no parameters remain or the parameter's code does not | |
9993 | match the specified code, return false. Otherwise continue | |
9994 | checking any remaining arguments. */ | |
9995 | arg = gimple_call_arg (call, i++); | |
9996 | if (!validate_arg (arg, code)) | |
9997 | goto end; | |
9998 | break; | |
9999 | } | |
10000 | } | |
10001 | while (1); | |
10002 | ||
10003 | /* We need gotos here since we can only have one VA_CLOSE in a | |
10004 | function. */ | |
10005 | end: ; | |
10006 | va_end (ap); | |
10007 | ||
10008 | return res; | |
10009 | } | |
10010 | ||
fc2a2dcb | 10011 | /* Default target-specific builtin expander that does nothing. */ |
10012 | ||
10013 | rtx | |
aecda0d6 | 10014 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
10015 | rtx target ATTRIBUTE_UNUSED, | |
10016 | rtx subtarget ATTRIBUTE_UNUSED, | |
3754d046 | 10017 | machine_mode mode ATTRIBUTE_UNUSED, |
aecda0d6 | 10018 | int ignore ATTRIBUTE_UNUSED) |
fc2a2dcb | 10019 | { |
10020 | return NULL_RTX; | |
10021 | } | |
c7926a82 | 10022 | |
01537105 | 10023 | /* Returns true is EXP represents data that would potentially reside |
10024 | in a readonly section. */ | |
10025 | ||
b9ea678c | 10026 | bool |
01537105 | 10027 | readonly_data_expr (tree exp) |
10028 | { | |
10029 | STRIP_NOPS (exp); | |
10030 | ||
9ff0637e | 10031 | if (TREE_CODE (exp) != ADDR_EXPR) |
10032 | return false; | |
10033 | ||
10034 | exp = get_base_address (TREE_OPERAND (exp, 0)); | |
10035 | if (!exp) | |
10036 | return false; | |
10037 | ||
10038 | /* Make sure we call decl_readonly_section only for trees it | |
10039 | can handle (since it returns true for everything it doesn't | |
10040 | understand). */ | |
491e04ef | 10041 | if (TREE_CODE (exp) == STRING_CST |
9ff0637e | 10042 | || TREE_CODE (exp) == CONSTRUCTOR |
53e9c5c4 | 10043 | || (VAR_P (exp) && TREE_STATIC (exp))) |
9ff0637e | 10044 | return decl_readonly_section (exp, 0); |
01537105 | 10045 | else |
10046 | return false; | |
10047 | } | |
4ee9c684 | 10048 | |
c2f47e15 | 10049 | /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments |
10050 | to the call, and TYPE is its return type. | |
4ee9c684 | 10051 | |
c2f47e15 | 10052 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10053 | simplified form of the call as a tree. |
10054 | ||
10055 | The simplified form may be a constant or other expression which | |
10056 | computes the same value, but in a more efficient manner (including | |
10057 | calls to other builtin functions). | |
10058 | ||
10059 | The call may contain arguments which need to be evaluated, but | |
10060 | which are not useful to determine the result of the call. In | |
10061 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
10062 | COMPOUND_EXPR will be an argument which must be evaluated. | |
10063 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
10064 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
10065 | form of the builtin function call. */ | |
10066 | ||
10067 | static tree | |
389dd41b | 10068 | fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type) |
4ee9c684 | 10069 | { |
c2f47e15 | 10070 | if (!validate_arg (s1, POINTER_TYPE) |
10071 | || !validate_arg (s2, POINTER_TYPE)) | |
10072 | return NULL_TREE; | |
4ee9c684 | 10073 | else |
10074 | { | |
4ee9c684 | 10075 | tree fn; |
10076 | const char *p1, *p2; | |
10077 | ||
10078 | p2 = c_getstr (s2); | |
10079 | if (p2 == NULL) | |
c2f47e15 | 10080 | return NULL_TREE; |
4ee9c684 | 10081 | |
10082 | p1 = c_getstr (s1); | |
10083 | if (p1 != NULL) | |
10084 | { | |
10085 | const char *r = strpbrk (p1, p2); | |
daa1d5f5 | 10086 | tree tem; |
4ee9c684 | 10087 | |
10088 | if (r == NULL) | |
779b4c41 | 10089 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 10090 | |
10091 | /* Return an offset into the constant string argument. */ | |
2cc66f2a | 10092 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
389dd41b | 10093 | return fold_convert_loc (loc, type, tem); |
4ee9c684 | 10094 | } |
10095 | ||
10096 | if (p2[0] == '\0') | |
05abc81b | 10097 | /* strpbrk(x, "") == NULL. |
10098 | Evaluate and ignore s1 in case it had side-effects. */ | |
44bfe16d | 10099 | return omit_one_operand_loc (loc, type, integer_zero_node, s1); |
4ee9c684 | 10100 | |
10101 | if (p2[1] != '\0') | |
c2f47e15 | 10102 | return NULL_TREE; /* Really call strpbrk. */ |
4ee9c684 | 10103 | |
b9a16870 | 10104 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
4ee9c684 | 10105 | if (!fn) |
c2f47e15 | 10106 | return NULL_TREE; |
4ee9c684 | 10107 | |
10108 | /* New argument list transforming strpbrk(s1, s2) to | |
10109 | strchr(s1, s2[0]). */ | |
7002a1c8 | 10110 | return build_call_expr_loc (loc, fn, 2, s1, |
10111 | build_int_cst (integer_type_node, p2[0])); | |
4ee9c684 | 10112 | } |
10113 | } | |
10114 | ||
c2f47e15 | 10115 | /* Simplify a call to the strspn builtin. S1 and S2 are the arguments |
10116 | to the call. | |
4ee9c684 | 10117 | |
c2f47e15 | 10118 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10119 | simplified form of the call as a tree. |
10120 | ||
10121 | The simplified form may be a constant or other expression which | |
10122 | computes the same value, but in a more efficient manner (including | |
10123 | calls to other builtin functions). | |
10124 | ||
10125 | The call may contain arguments which need to be evaluated, but | |
10126 | which are not useful to determine the result of the call. In | |
10127 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
10128 | COMPOUND_EXPR will be an argument which must be evaluated. | |
10129 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
10130 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
10131 | form of the builtin function call. */ | |
10132 | ||
10133 | static tree | |
389dd41b | 10134 | fold_builtin_strspn (location_t loc, tree s1, tree s2) |
4ee9c684 | 10135 | { |
c2f47e15 | 10136 | if (!validate_arg (s1, POINTER_TYPE) |
10137 | || !validate_arg (s2, POINTER_TYPE)) | |
10138 | return NULL_TREE; | |
4ee9c684 | 10139 | else |
10140 | { | |
4ee9c684 | 10141 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); |
10142 | ||
c2f47e15 | 10143 | /* If either argument is "", return NULL_TREE. */ |
4ee9c684 | 10144 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) |
9bc9f15f | 10145 | /* Evaluate and ignore both arguments in case either one has |
10146 | side-effects. */ | |
389dd41b | 10147 | return omit_two_operands_loc (loc, size_type_node, size_zero_node, |
9bc9f15f | 10148 | s1, s2); |
c2f47e15 | 10149 | return NULL_TREE; |
4ee9c684 | 10150 | } |
10151 | } | |
10152 | ||
c2f47e15 | 10153 | /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments |
10154 | to the call. | |
4ee9c684 | 10155 | |
c2f47e15 | 10156 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10157 | simplified form of the call as a tree. |
10158 | ||
10159 | The simplified form may be a constant or other expression which | |
10160 | computes the same value, but in a more efficient manner (including | |
10161 | calls to other builtin functions). | |
10162 | ||
10163 | The call may contain arguments which need to be evaluated, but | |
10164 | which are not useful to determine the result of the call. In | |
10165 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
10166 | COMPOUND_EXPR will be an argument which must be evaluated. | |
10167 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
10168 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
10169 | form of the builtin function call. */ | |
10170 | ||
10171 | static tree | |
389dd41b | 10172 | fold_builtin_strcspn (location_t loc, tree s1, tree s2) |
4ee9c684 | 10173 | { |
c2f47e15 | 10174 | if (!validate_arg (s1, POINTER_TYPE) |
10175 | || !validate_arg (s2, POINTER_TYPE)) | |
10176 | return NULL_TREE; | |
4ee9c684 | 10177 | else |
10178 | { | |
c2f47e15 | 10179 | /* If the first argument is "", return NULL_TREE. */ |
b5e46e2c | 10180 | const char *p1 = c_getstr (s1); |
4ee9c684 | 10181 | if (p1 && *p1 == '\0') |
10182 | { | |
10183 | /* Evaluate and ignore argument s2 in case it has | |
10184 | side-effects. */ | |
389dd41b | 10185 | return omit_one_operand_loc (loc, size_type_node, |
39761420 | 10186 | size_zero_node, s2); |
4ee9c684 | 10187 | } |
10188 | ||
10189 | /* If the second argument is "", return __builtin_strlen(s1). */ | |
b5e46e2c | 10190 | const char *p2 = c_getstr (s2); |
4ee9c684 | 10191 | if (p2 && *p2 == '\0') |
10192 | { | |
b9a16870 | 10193 | tree fn = builtin_decl_implicit (BUILT_IN_STRLEN); |
4ee9c684 | 10194 | |
10195 | /* If the replacement _DECL isn't initialized, don't do the | |
10196 | transformation. */ | |
10197 | if (!fn) | |
c2f47e15 | 10198 | return NULL_TREE; |
4ee9c684 | 10199 | |
389dd41b | 10200 | return build_call_expr_loc (loc, fn, 1, s1); |
4ee9c684 | 10201 | } |
c2f47e15 | 10202 | return NULL_TREE; |
4ee9c684 | 10203 | } |
10204 | } | |
10205 | ||
c2f47e15 | 10206 | /* Fold the next_arg or va_start call EXP. Returns true if there was an error |
743b0c6a | 10207 | produced. False otherwise. This is done so that we don't output the error |
10208 | or warning twice or three times. */ | |
75a70cf9 | 10209 | |
743b0c6a | 10210 | bool |
c2f47e15 | 10211 | fold_builtin_next_arg (tree exp, bool va_start_p) |
4ee9c684 | 10212 | { |
10213 | tree fntype = TREE_TYPE (current_function_decl); | |
c2f47e15 | 10214 | int nargs = call_expr_nargs (exp); |
10215 | tree arg; | |
d98fd4a4 | 10216 | /* There is good chance the current input_location points inside the |
10217 | definition of the va_start macro (perhaps on the token for | |
10218 | builtin) in a system header, so warnings will not be emitted. | |
10219 | Use the location in real source code. */ | |
be1e7283 | 10220 | location_t current_location = |
d98fd4a4 | 10221 | linemap_unwind_to_first_non_reserved_loc (line_table, input_location, |
10222 | NULL); | |
4ee9c684 | 10223 | |
257d99c3 | 10224 | if (!stdarg_p (fntype)) |
743b0c6a | 10225 | { |
10226 | error ("%<va_start%> used in function with fixed args"); | |
10227 | return true; | |
10228 | } | |
c2f47e15 | 10229 | |
10230 | if (va_start_p) | |
79012a9d | 10231 | { |
c2f47e15 | 10232 | if (va_start_p && (nargs != 2)) |
10233 | { | |
10234 | error ("wrong number of arguments to function %<va_start%>"); | |
10235 | return true; | |
10236 | } | |
10237 | arg = CALL_EXPR_ARG (exp, 1); | |
79012a9d | 10238 | } |
10239 | /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0) | |
10240 | when we checked the arguments and if needed issued a warning. */ | |
c2f47e15 | 10241 | else |
4ee9c684 | 10242 | { |
c2f47e15 | 10243 | if (nargs == 0) |
10244 | { | |
10245 | /* Evidently an out of date version of <stdarg.h>; can't validate | |
10246 | va_start's second argument, but can still work as intended. */ | |
d98fd4a4 | 10247 | warning_at (current_location, |
7edb1062 | 10248 | OPT_Wvarargs, |
10249 | "%<__builtin_next_arg%> called without an argument"); | |
c2f47e15 | 10250 | return true; |
10251 | } | |
10252 | else if (nargs > 1) | |
a0c938f0 | 10253 | { |
c2f47e15 | 10254 | error ("wrong number of arguments to function %<__builtin_next_arg%>"); |
a0c938f0 | 10255 | return true; |
10256 | } | |
c2f47e15 | 10257 | arg = CALL_EXPR_ARG (exp, 0); |
10258 | } | |
10259 | ||
a8dd994c | 10260 | if (TREE_CODE (arg) == SSA_NAME) |
10261 | arg = SSA_NAME_VAR (arg); | |
10262 | ||
c2f47e15 | 10263 | /* We destructively modify the call to be __builtin_va_start (ap, 0) |
48e1416a | 10264 | or __builtin_next_arg (0) the first time we see it, after checking |
c2f47e15 | 10265 | the arguments and if needed issuing a warning. */ |
10266 | if (!integer_zerop (arg)) | |
10267 | { | |
10268 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
79012a9d | 10269 | |
4ee9c684 | 10270 | /* Strip off all nops for the sake of the comparison. This |
10271 | is not quite the same as STRIP_NOPS. It does more. | |
10272 | We must also strip off INDIRECT_EXPR for C++ reference | |
10273 | parameters. */ | |
72dd6141 | 10274 | while (CONVERT_EXPR_P (arg) |
4ee9c684 | 10275 | || TREE_CODE (arg) == INDIRECT_REF) |
10276 | arg = TREE_OPERAND (arg, 0); | |
10277 | if (arg != last_parm) | |
a0c938f0 | 10278 | { |
b08cf617 | 10279 | /* FIXME: Sometimes with the tree optimizers we can get the |
10280 | not the last argument even though the user used the last | |
10281 | argument. We just warn and set the arg to be the last | |
10282 | argument so that we will get wrong-code because of | |
10283 | it. */ | |
d98fd4a4 | 10284 | warning_at (current_location, |
7edb1062 | 10285 | OPT_Wvarargs, |
d98fd4a4 | 10286 | "second parameter of %<va_start%> not last named argument"); |
743b0c6a | 10287 | } |
24158ad7 | 10288 | |
10289 | /* Undefined by C99 7.15.1.4p4 (va_start): | |
10290 | "If the parameter parmN is declared with the register storage | |
10291 | class, with a function or array type, or with a type that is | |
10292 | not compatible with the type that results after application of | |
10293 | the default argument promotions, the behavior is undefined." | |
10294 | */ | |
10295 | else if (DECL_REGISTER (arg)) | |
d98fd4a4 | 10296 | { |
10297 | warning_at (current_location, | |
7edb1062 | 10298 | OPT_Wvarargs, |
67cf9b55 | 10299 | "undefined behavior when second parameter of " |
d98fd4a4 | 10300 | "%<va_start%> is declared with %<register%> storage"); |
10301 | } | |
24158ad7 | 10302 | |
79012a9d | 10303 | /* We want to verify the second parameter just once before the tree |
a0c938f0 | 10304 | optimizers are run and then avoid keeping it in the tree, |
10305 | as otherwise we could warn even for correct code like: | |
10306 | void foo (int i, ...) | |
10307 | { va_list ap; i++; va_start (ap, i); va_end (ap); } */ | |
c2f47e15 | 10308 | if (va_start_p) |
10309 | CALL_EXPR_ARG (exp, 1) = integer_zero_node; | |
10310 | else | |
10311 | CALL_EXPR_ARG (exp, 0) = integer_zero_node; | |
743b0c6a | 10312 | } |
10313 | return false; | |
4ee9c684 | 10314 | } |
10315 | ||
10316 | ||
c2f47e15 | 10317 | /* Expand a call EXP to __builtin_object_size. */ |
0a39fd54 | 10318 | |
f7715905 | 10319 | static rtx |
0a39fd54 | 10320 | expand_builtin_object_size (tree exp) |
10321 | { | |
10322 | tree ost; | |
10323 | int object_size_type; | |
10324 | tree fndecl = get_callee_fndecl (exp); | |
0a39fd54 | 10325 | |
c2f47e15 | 10326 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
0a39fd54 | 10327 | { |
8c41abe8 | 10328 | error ("%Kfirst argument of %qD must be a pointer, second integer constant", |
b8c23db3 | 10329 | exp, fndecl); |
0a39fd54 | 10330 | expand_builtin_trap (); |
10331 | return const0_rtx; | |
10332 | } | |
10333 | ||
c2f47e15 | 10334 | ost = CALL_EXPR_ARG (exp, 1); |
0a39fd54 | 10335 | STRIP_NOPS (ost); |
10336 | ||
10337 | if (TREE_CODE (ost) != INTEGER_CST | |
10338 | || tree_int_cst_sgn (ost) < 0 | |
10339 | || compare_tree_int (ost, 3) > 0) | |
10340 | { | |
8c41abe8 | 10341 | error ("%Klast argument of %qD is not integer constant between 0 and 3", |
b8c23db3 | 10342 | exp, fndecl); |
0a39fd54 | 10343 | expand_builtin_trap (); |
10344 | return const0_rtx; | |
10345 | } | |
10346 | ||
e913b5cd | 10347 | object_size_type = tree_to_shwi (ost); |
0a39fd54 | 10348 | |
10349 | return object_size_type < 2 ? constm1_rtx : const0_rtx; | |
10350 | } | |
10351 | ||
10352 | /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
10353 | FCODE is the BUILT_IN_* to use. | |
c2f47e15 | 10354 | Return NULL_RTX if we failed; the caller should emit a normal call, |
0a39fd54 | 10355 | otherwise try to get the result in TARGET, if convenient (and in |
10356 | mode MODE if that's convenient). */ | |
10357 | ||
10358 | static rtx | |
3754d046 | 10359 | expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode, |
0a39fd54 | 10360 | enum built_in_function fcode) |
10361 | { | |
c2f47e15 | 10362 | if (!validate_arglist (exp, |
0a39fd54 | 10363 | POINTER_TYPE, |
10364 | fcode == BUILT_IN_MEMSET_CHK | |
10365 | ? INTEGER_TYPE : POINTER_TYPE, | |
10366 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
c2f47e15 | 10367 | return NULL_RTX; |
0a39fd54 | 10368 | |
e6a18b5a | 10369 | tree dest = CALL_EXPR_ARG (exp, 0); |
10370 | tree src = CALL_EXPR_ARG (exp, 1); | |
10371 | tree len = CALL_EXPR_ARG (exp, 2); | |
10372 | tree size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 10373 | |
e6a18b5a | 10374 | bool sizes_ok = check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, |
10375 | /*str=*/NULL_TREE, size); | |
5aef8938 | 10376 | |
10377 | if (!tree_fits_uhwi_p (size)) | |
c2f47e15 | 10378 | return NULL_RTX; |
0a39fd54 | 10379 | |
e913b5cd | 10380 | if (tree_fits_uhwi_p (len) || integer_all_onesp (size)) |
0a39fd54 | 10381 | { |
5aef8938 | 10382 | /* Avoid transforming the checking call to an ordinary one when |
10383 | an overflow has been detected or when the call couldn't be | |
10384 | validated because the size is not constant. */ | |
10385 | if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len)) | |
10386 | return NULL_RTX; | |
0a39fd54 | 10387 | |
5aef8938 | 10388 | tree fn = NULL_TREE; |
0a39fd54 | 10389 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume |
10390 | mem{cpy,pcpy,move,set} is available. */ | |
10391 | switch (fcode) | |
10392 | { | |
10393 | case BUILT_IN_MEMCPY_CHK: | |
b9a16870 | 10394 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); |
0a39fd54 | 10395 | break; |
10396 | case BUILT_IN_MEMPCPY_CHK: | |
b9a16870 | 10397 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); |
0a39fd54 | 10398 | break; |
10399 | case BUILT_IN_MEMMOVE_CHK: | |
b9a16870 | 10400 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); |
0a39fd54 | 10401 | break; |
10402 | case BUILT_IN_MEMSET_CHK: | |
b9a16870 | 10403 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); |
0a39fd54 | 10404 | break; |
10405 | default: | |
10406 | break; | |
10407 | } | |
10408 | ||
10409 | if (! fn) | |
c2f47e15 | 10410 | return NULL_RTX; |
0a39fd54 | 10411 | |
0568e9c1 | 10412 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len); |
a65c4d64 | 10413 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
10414 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
0a39fd54 | 10415 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
10416 | } | |
10417 | else if (fcode == BUILT_IN_MEMSET_CHK) | |
c2f47e15 | 10418 | return NULL_RTX; |
0a39fd54 | 10419 | else |
10420 | { | |
957d0361 | 10421 | unsigned int dest_align = get_pointer_alignment (dest); |
0a39fd54 | 10422 | |
10423 | /* If DEST is not a pointer type, call the normal function. */ | |
10424 | if (dest_align == 0) | |
c2f47e15 | 10425 | return NULL_RTX; |
0a39fd54 | 10426 | |
10427 | /* If SRC and DEST are the same (and not volatile), do nothing. */ | |
10428 | if (operand_equal_p (src, dest, 0)) | |
10429 | { | |
10430 | tree expr; | |
10431 | ||
10432 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
10433 | { | |
10434 | /* Evaluate and ignore LEN in case it has side-effects. */ | |
10435 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
10436 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
10437 | } | |
10438 | ||
2cc66f2a | 10439 | expr = fold_build_pointer_plus (dest, len); |
0a39fd54 | 10440 | return expand_expr (expr, target, mode, EXPAND_NORMAL); |
10441 | } | |
10442 | ||
10443 | /* __memmove_chk special case. */ | |
10444 | if (fcode == BUILT_IN_MEMMOVE_CHK) | |
10445 | { | |
957d0361 | 10446 | unsigned int src_align = get_pointer_alignment (src); |
0a39fd54 | 10447 | |
10448 | if (src_align == 0) | |
c2f47e15 | 10449 | return NULL_RTX; |
0a39fd54 | 10450 | |
10451 | /* If src is categorized for a readonly section we can use | |
10452 | normal __memcpy_chk. */ | |
10453 | if (readonly_data_expr (src)) | |
10454 | { | |
b9a16870 | 10455 | tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); |
0a39fd54 | 10456 | if (!fn) |
c2f47e15 | 10457 | return NULL_RTX; |
0568e9c1 | 10458 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4, |
10459 | dest, src, len, size); | |
a65c4d64 | 10460 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
10461 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
0a39fd54 | 10462 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
10463 | } | |
10464 | } | |
c2f47e15 | 10465 | return NULL_RTX; |
0a39fd54 | 10466 | } |
10467 | } | |
10468 | ||
10469 | /* Emit warning if a buffer overflow is detected at compile time. */ | |
10470 | ||
10471 | static void | |
10472 | maybe_emit_chk_warning (tree exp, enum built_in_function fcode) | |
10473 | { | |
5aef8938 | 10474 | /* The source string. */ |
10475 | tree srcstr = NULL_TREE; | |
10476 | /* The size of the destination object. */ | |
10477 | tree objsize = NULL_TREE; | |
10478 | /* The string that is being concatenated with (as in __strcat_chk) | |
10479 | or null if it isn't. */ | |
10480 | tree catstr = NULL_TREE; | |
10481 | /* The maximum length of the source sequence in a bounded operation | |
10482 | (such as __strncat_chk) or null if the operation isn't bounded | |
10483 | (such as __strcat_chk). */ | |
e6a18b5a | 10484 | tree maxread = NULL_TREE; |
f3969b49 | 10485 | /* The exact size of the access (such as in __strncpy_chk). */ |
10486 | tree size = NULL_TREE; | |
0a39fd54 | 10487 | |
10488 | switch (fcode) | |
10489 | { | |
10490 | case BUILT_IN_STRCPY_CHK: | |
10491 | case BUILT_IN_STPCPY_CHK: | |
5aef8938 | 10492 | srcstr = CALL_EXPR_ARG (exp, 1); |
10493 | objsize = CALL_EXPR_ARG (exp, 2); | |
10494 | break; | |
10495 | ||
0a39fd54 | 10496 | case BUILT_IN_STRCAT_CHK: |
5aef8938 | 10497 | /* For __strcat_chk the warning will be emitted only if overflowing |
10498 | by at least strlen (dest) + 1 bytes. */ | |
10499 | catstr = CALL_EXPR_ARG (exp, 0); | |
10500 | srcstr = CALL_EXPR_ARG (exp, 1); | |
10501 | objsize = CALL_EXPR_ARG (exp, 2); | |
0a39fd54 | 10502 | break; |
5aef8938 | 10503 | |
b356dfef | 10504 | case BUILT_IN_STRNCAT_CHK: |
5aef8938 | 10505 | catstr = CALL_EXPR_ARG (exp, 0); |
10506 | srcstr = CALL_EXPR_ARG (exp, 1); | |
e6a18b5a | 10507 | maxread = CALL_EXPR_ARG (exp, 2); |
5aef8938 | 10508 | objsize = CALL_EXPR_ARG (exp, 3); |
10509 | break; | |
10510 | ||
0a39fd54 | 10511 | case BUILT_IN_STRNCPY_CHK: |
1063acde | 10512 | case BUILT_IN_STPNCPY_CHK: |
5aef8938 | 10513 | srcstr = CALL_EXPR_ARG (exp, 1); |
f3969b49 | 10514 | size = CALL_EXPR_ARG (exp, 2); |
5aef8938 | 10515 | objsize = CALL_EXPR_ARG (exp, 3); |
0a39fd54 | 10516 | break; |
5aef8938 | 10517 | |
0a39fd54 | 10518 | case BUILT_IN_SNPRINTF_CHK: |
10519 | case BUILT_IN_VSNPRINTF_CHK: | |
e6a18b5a | 10520 | maxread = CALL_EXPR_ARG (exp, 1); |
5aef8938 | 10521 | objsize = CALL_EXPR_ARG (exp, 3); |
0a39fd54 | 10522 | break; |
10523 | default: | |
10524 | gcc_unreachable (); | |
10525 | } | |
10526 | ||
e6a18b5a | 10527 | if (catstr && maxread) |
0a39fd54 | 10528 | { |
5aef8938 | 10529 | /* Check __strncat_chk. There is no way to determine the length |
10530 | of the string to which the source string is being appended so | |
10531 | just warn when the length of the source string is not known. */ | |
8d6c6ef5 | 10532 | check_strncat_sizes (exp, objsize); |
10533 | return; | |
0a39fd54 | 10534 | } |
0a39fd54 | 10535 | |
e6a18b5a | 10536 | /* The destination argument is the first one for all built-ins above. */ |
10537 | tree dst = CALL_EXPR_ARG (exp, 0); | |
10538 | ||
10539 | check_access (exp, dst, srcstr, size, maxread, srcstr, objsize); | |
0a39fd54 | 10540 | } |
10541 | ||
10542 | /* Emit warning if a buffer overflow is detected at compile time | |
10543 | in __sprintf_chk/__vsprintf_chk calls. */ | |
10544 | ||
10545 | static void | |
10546 | maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) | |
10547 | { | |
1e4adcfc | 10548 | tree size, len, fmt; |
0a39fd54 | 10549 | const char *fmt_str; |
c2f47e15 | 10550 | int nargs = call_expr_nargs (exp); |
0a39fd54 | 10551 | |
10552 | /* Verify the required arguments in the original call. */ | |
48e1416a | 10553 | |
c2f47e15 | 10554 | if (nargs < 4) |
0a39fd54 | 10555 | return; |
c2f47e15 | 10556 | size = CALL_EXPR_ARG (exp, 2); |
10557 | fmt = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 10558 | |
e913b5cd | 10559 | if (! tree_fits_uhwi_p (size) || integer_all_onesp (size)) |
0a39fd54 | 10560 | return; |
10561 | ||
10562 | /* Check whether the format is a literal string constant. */ | |
10563 | fmt_str = c_getstr (fmt); | |
10564 | if (fmt_str == NULL) | |
10565 | return; | |
10566 | ||
d4473c84 | 10567 | if (!init_target_chars ()) |
99eabcc1 | 10568 | return; |
10569 | ||
0a39fd54 | 10570 | /* If the format doesn't contain % args or %%, we know its size. */ |
99eabcc1 | 10571 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 10572 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
10573 | /* If the format is "%s" and first ... argument is a string literal, | |
10574 | we know it too. */ | |
c2f47e15 | 10575 | else if (fcode == BUILT_IN_SPRINTF_CHK |
10576 | && strcmp (fmt_str, target_percent_s) == 0) | |
0a39fd54 | 10577 | { |
10578 | tree arg; | |
10579 | ||
c2f47e15 | 10580 | if (nargs < 5) |
0a39fd54 | 10581 | return; |
c2f47e15 | 10582 | arg = CALL_EXPR_ARG (exp, 4); |
0a39fd54 | 10583 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
10584 | return; | |
10585 | ||
10586 | len = c_strlen (arg, 1); | |
e913b5cd | 10587 | if (!len || ! tree_fits_uhwi_p (len)) |
0a39fd54 | 10588 | return; |
10589 | } | |
10590 | else | |
10591 | return; | |
10592 | ||
5aef8938 | 10593 | /* Add one for the terminating nul. */ |
10594 | len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node); | |
e6a18b5a | 10595 | |
10596 | check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, /*size=*/NULL_TREE, | |
10597 | /*maxread=*/NULL_TREE, len, size); | |
0a39fd54 | 10598 | } |
10599 | ||
2c281b15 | 10600 | /* Emit warning if a free is called with address of a variable. */ |
10601 | ||
10602 | static void | |
10603 | maybe_emit_free_warning (tree exp) | |
10604 | { | |
06229fe5 | 10605 | if (call_expr_nargs (exp) != 1) |
10606 | return; | |
10607 | ||
2c281b15 | 10608 | tree arg = CALL_EXPR_ARG (exp, 0); |
10609 | ||
10610 | STRIP_NOPS (arg); | |
10611 | if (TREE_CODE (arg) != ADDR_EXPR) | |
10612 | return; | |
10613 | ||
10614 | arg = get_base_address (TREE_OPERAND (arg, 0)); | |
182cf5a9 | 10615 | if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF) |
2c281b15 | 10616 | return; |
10617 | ||
10618 | if (SSA_VAR_P (arg)) | |
f74ea1c2 | 10619 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
10620 | "%Kattempt to free a non-heap object %qD", exp, arg); | |
2c281b15 | 10621 | else |
f74ea1c2 | 10622 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
10623 | "%Kattempt to free a non-heap object", exp); | |
2c281b15 | 10624 | } |
10625 | ||
c2f47e15 | 10626 | /* Fold a call to __builtin_object_size with arguments PTR and OST, |
10627 | if possible. */ | |
0a39fd54 | 10628 | |
f7715905 | 10629 | static tree |
c2f47e15 | 10630 | fold_builtin_object_size (tree ptr, tree ost) |
0a39fd54 | 10631 | { |
a6caa15f | 10632 | unsigned HOST_WIDE_INT bytes; |
0a39fd54 | 10633 | int object_size_type; |
10634 | ||
c2f47e15 | 10635 | if (!validate_arg (ptr, POINTER_TYPE) |
10636 | || !validate_arg (ost, INTEGER_TYPE)) | |
10637 | return NULL_TREE; | |
0a39fd54 | 10638 | |
0a39fd54 | 10639 | STRIP_NOPS (ost); |
10640 | ||
10641 | if (TREE_CODE (ost) != INTEGER_CST | |
10642 | || tree_int_cst_sgn (ost) < 0 | |
10643 | || compare_tree_int (ost, 3) > 0) | |
c2f47e15 | 10644 | return NULL_TREE; |
0a39fd54 | 10645 | |
e913b5cd | 10646 | object_size_type = tree_to_shwi (ost); |
0a39fd54 | 10647 | |
10648 | /* __builtin_object_size doesn't evaluate side-effects in its arguments; | |
10649 | if there are any side-effects, it returns (size_t) -1 for types 0 and 1 | |
10650 | and (size_t) 0 for types 2 and 3. */ | |
10651 | if (TREE_SIDE_EFFECTS (ptr)) | |
697bbc3f | 10652 | return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0); |
0a39fd54 | 10653 | |
10654 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
a6caa15f | 10655 | { |
4e91a07b | 10656 | compute_builtin_object_size (ptr, object_size_type, &bytes); |
6da74b21 | 10657 | if (wi::fits_to_tree_p (bytes, size_type_node)) |
10658 | return build_int_cstu (size_type_node, bytes); | |
a6caa15f | 10659 | } |
0a39fd54 | 10660 | else if (TREE_CODE (ptr) == SSA_NAME) |
10661 | { | |
0a39fd54 | 10662 | /* If object size is not known yet, delay folding until |
10663 | later. Maybe subsequent passes will help determining | |
10664 | it. */ | |
4e91a07b | 10665 | if (compute_builtin_object_size (ptr, object_size_type, &bytes) |
10666 | && wi::fits_to_tree_p (bytes, size_type_node)) | |
6da74b21 | 10667 | return build_int_cstu (size_type_node, bytes); |
0a39fd54 | 10668 | } |
10669 | ||
a6caa15f | 10670 | return NULL_TREE; |
0a39fd54 | 10671 | } |
10672 | ||
12f08300 | 10673 | /* Builtins with folding operations that operate on "..." arguments |
10674 | need special handling; we need to store the arguments in a convenient | |
10675 | data structure before attempting any folding. Fortunately there are | |
10676 | only a few builtins that fall into this category. FNDECL is the | |
10677 | function, EXP is the CALL_EXPR for the call. */ | |
10678 | ||
10679 | static tree | |
10680 | fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs) | |
10681 | { | |
10682 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
10683 | tree ret = NULL_TREE; | |
10684 | ||
10685 | switch (fcode) | |
10686 | { | |
10687 | case BUILT_IN_FPCLASSIFY: | |
10688 | ret = fold_builtin_fpclassify (loc, args, nargs); | |
10689 | break; | |
10690 | ||
10691 | default: | |
10692 | break; | |
10693 | } | |
10694 | if (ret) | |
10695 | { | |
10696 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
10697 | SET_EXPR_LOCATION (ret, loc); | |
10698 | TREE_NO_WARNING (ret) = 1; | |
10699 | return ret; | |
10700 | } | |
10701 | return NULL_TREE; | |
10702 | } | |
10703 | ||
99eabcc1 | 10704 | /* Initialize format string characters in the target charset. */ |
10705 | ||
b9ea678c | 10706 | bool |
99eabcc1 | 10707 | init_target_chars (void) |
10708 | { | |
10709 | static bool init; | |
10710 | if (!init) | |
10711 | { | |
10712 | target_newline = lang_hooks.to_target_charset ('\n'); | |
10713 | target_percent = lang_hooks.to_target_charset ('%'); | |
10714 | target_c = lang_hooks.to_target_charset ('c'); | |
10715 | target_s = lang_hooks.to_target_charset ('s'); | |
10716 | if (target_newline == 0 || target_percent == 0 || target_c == 0 | |
10717 | || target_s == 0) | |
10718 | return false; | |
10719 | ||
10720 | target_percent_c[0] = target_percent; | |
10721 | target_percent_c[1] = target_c; | |
10722 | target_percent_c[2] = '\0'; | |
10723 | ||
10724 | target_percent_s[0] = target_percent; | |
10725 | target_percent_s[1] = target_s; | |
10726 | target_percent_s[2] = '\0'; | |
10727 | ||
10728 | target_percent_s_newline[0] = target_percent; | |
10729 | target_percent_s_newline[1] = target_s; | |
10730 | target_percent_s_newline[2] = target_newline; | |
10731 | target_percent_s_newline[3] = '\0'; | |
a0c938f0 | 10732 | |
99eabcc1 | 10733 | init = true; |
10734 | } | |
10735 | return true; | |
10736 | } | |
bffb7645 | 10737 | |
f0c477f2 | 10738 | /* Helper function for do_mpfr_arg*(). Ensure M is a normal number |
10739 | and no overflow/underflow occurred. INEXACT is true if M was not | |
fa7637bd | 10740 | exactly calculated. TYPE is the tree type for the result. This |
f0c477f2 | 10741 | function assumes that you cleared the MPFR flags and then |
10742 | calculated M to see if anything subsequently set a flag prior to | |
10743 | entering this function. Return NULL_TREE if any checks fail. */ | |
10744 | ||
10745 | static tree | |
d4473c84 | 10746 | do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact) |
f0c477f2 | 10747 | { |
10748 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
10749 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
10750 | result of calling FUNC was exact. */ | |
d4473c84 | 10751 | if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p () |
f0c477f2 | 10752 | && (!flag_rounding_math || !inexact)) |
10753 | { | |
10754 | REAL_VALUE_TYPE rr; | |
10755 | ||
66fa16e6 | 10756 | real_from_mpfr (&rr, m, type, GMP_RNDN); |
f0c477f2 | 10757 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value, |
10758 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
10759 | but the mpft_t is not, then we underflowed in the | |
10760 | conversion. */ | |
776a7bab | 10761 | if (real_isfinite (&rr) |
f0c477f2 | 10762 | && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0)) |
10763 | { | |
10764 | REAL_VALUE_TYPE rmode; | |
10765 | ||
10766 | real_convert (&rmode, TYPE_MODE (type), &rr); | |
10767 | /* Proceed iff the specified mode can hold the value. */ | |
10768 | if (real_identical (&rmode, &rr)) | |
10769 | return build_real (type, rmode); | |
10770 | } | |
10771 | } | |
10772 | return NULL_TREE; | |
10773 | } | |
10774 | ||
239d491a | 10775 | /* Helper function for do_mpc_arg*(). Ensure M is a normal complex |
10776 | number and no overflow/underflow occurred. INEXACT is true if M | |
10777 | was not exactly calculated. TYPE is the tree type for the result. | |
10778 | This function assumes that you cleared the MPFR flags and then | |
10779 | calculated M to see if anything subsequently set a flag prior to | |
652d9409 | 10780 | entering this function. Return NULL_TREE if any checks fail, if |
10781 | FORCE_CONVERT is true, then bypass the checks. */ | |
239d491a | 10782 | |
10783 | static tree | |
652d9409 | 10784 | do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert) |
239d491a | 10785 | { |
10786 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
10787 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
10788 | result of calling FUNC was exact. */ | |
652d9409 | 10789 | if (force_convert |
10790 | || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m)) | |
10791 | && !mpfr_overflow_p () && !mpfr_underflow_p () | |
10792 | && (!flag_rounding_math || !inexact))) | |
239d491a | 10793 | { |
10794 | REAL_VALUE_TYPE re, im; | |
10795 | ||
b0e7c4d4 | 10796 | real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN); |
10797 | real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN); | |
239d491a | 10798 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values, |
10799 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
10800 | but the mpft_t is not, then we underflowed in the | |
10801 | conversion. */ | |
652d9409 | 10802 | if (force_convert |
10803 | || (real_isfinite (&re) && real_isfinite (&im) | |
10804 | && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0) | |
10805 | && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0))) | |
239d491a | 10806 | { |
10807 | REAL_VALUE_TYPE re_mode, im_mode; | |
10808 | ||
10809 | real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re); | |
10810 | real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im); | |
10811 | /* Proceed iff the specified mode can hold the value. */ | |
652d9409 | 10812 | if (force_convert |
10813 | || (real_identical (&re_mode, &re) | |
10814 | && real_identical (&im_mode, &im))) | |
239d491a | 10815 | return build_complex (type, build_real (TREE_TYPE (type), re_mode), |
10816 | build_real (TREE_TYPE (type), im_mode)); | |
10817 | } | |
10818 | } | |
10819 | return NULL_TREE; | |
10820 | } | |
239d491a | 10821 | |
e5407ca6 | 10822 | /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set |
10823 | the pointer *(ARG_QUO) and return the result. The type is taken | |
10824 | from the type of ARG0 and is used for setting the precision of the | |
10825 | calculation and results. */ | |
10826 | ||
10827 | static tree | |
10828 | do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo) | |
10829 | { | |
10830 | tree const type = TREE_TYPE (arg0); | |
10831 | tree result = NULL_TREE; | |
48e1416a | 10832 | |
e5407ca6 | 10833 | STRIP_NOPS (arg0); |
10834 | STRIP_NOPS (arg1); | |
48e1416a | 10835 | |
e5407ca6 | 10836 | /* To proceed, MPFR must exactly represent the target floating point |
10837 | format, which only happens when the target base equals two. */ | |
10838 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
10839 | && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0) | |
10840 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)) | |
10841 | { | |
10842 | const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0); | |
10843 | const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1); | |
10844 | ||
776a7bab | 10845 | if (real_isfinite (ra0) && real_isfinite (ra1)) |
e5407ca6 | 10846 | { |
e2eb2b7f | 10847 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
10848 | const int prec = fmt->p; | |
10849 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
e5407ca6 | 10850 | tree result_rem; |
10851 | long integer_quo; | |
10852 | mpfr_t m0, m1; | |
10853 | ||
10854 | mpfr_inits2 (prec, m0, m1, NULL); | |
10855 | mpfr_from_real (m0, ra0, GMP_RNDN); | |
10856 | mpfr_from_real (m1, ra1, GMP_RNDN); | |
10857 | mpfr_clear_flags (); | |
e2eb2b7f | 10858 | mpfr_remquo (m0, &integer_quo, m0, m1, rnd); |
e5407ca6 | 10859 | /* Remquo is independent of the rounding mode, so pass |
10860 | inexact=0 to do_mpfr_ckconv(). */ | |
10861 | result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0); | |
10862 | mpfr_clears (m0, m1, NULL); | |
10863 | if (result_rem) | |
10864 | { | |
10865 | /* MPFR calculates quo in the host's long so it may | |
10866 | return more bits in quo than the target int can hold | |
10867 | if sizeof(host long) > sizeof(target int). This can | |
10868 | happen even for native compilers in LP64 mode. In | |
10869 | these cases, modulo the quo value with the largest | |
10870 | number that the target int can hold while leaving one | |
10871 | bit for the sign. */ | |
10872 | if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE) | |
10873 | integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1)); | |
10874 | ||
10875 | /* Dereference the quo pointer argument. */ | |
10876 | arg_quo = build_fold_indirect_ref (arg_quo); | |
10877 | /* Proceed iff a valid pointer type was passed in. */ | |
10878 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node) | |
10879 | { | |
10880 | /* Set the value. */ | |
7002a1c8 | 10881 | tree result_quo |
10882 | = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo, | |
10883 | build_int_cst (TREE_TYPE (arg_quo), | |
10884 | integer_quo)); | |
e5407ca6 | 10885 | TREE_SIDE_EFFECTS (result_quo) = 1; |
10886 | /* Combine the quo assignment with the rem. */ | |
10887 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
10888 | result_quo, result_rem)); | |
10889 | } | |
10890 | } | |
10891 | } | |
10892 | } | |
10893 | return result; | |
10894 | } | |
e84da7c1 | 10895 | |
10896 | /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the | |
10897 | resulting value as a tree with type TYPE. The mpfr precision is | |
10898 | set to the precision of TYPE. We assume that this mpfr function | |
10899 | returns zero if the result could be calculated exactly within the | |
10900 | requested precision. In addition, the integer pointer represented | |
10901 | by ARG_SG will be dereferenced and set to the appropriate signgam | |
10902 | (-1,1) value. */ | |
10903 | ||
10904 | static tree | |
10905 | do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type) | |
10906 | { | |
10907 | tree result = NULL_TREE; | |
10908 | ||
10909 | STRIP_NOPS (arg); | |
48e1416a | 10910 | |
e84da7c1 | 10911 | /* To proceed, MPFR must exactly represent the target floating point |
10912 | format, which only happens when the target base equals two. Also | |
10913 | verify ARG is a constant and that ARG_SG is an int pointer. */ | |
10914 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
10915 | && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg) | |
10916 | && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE | |
10917 | && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node) | |
10918 | { | |
10919 | const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg); | |
10920 | ||
10921 | /* In addition to NaN and Inf, the argument cannot be zero or a | |
10922 | negative integer. */ | |
776a7bab | 10923 | if (real_isfinite (ra) |
e84da7c1 | 10924 | && ra->cl != rvc_zero |
9af5ce0c | 10925 | && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type)))) |
e84da7c1 | 10926 | { |
e2eb2b7f | 10927 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
10928 | const int prec = fmt->p; | |
10929 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
e84da7c1 | 10930 | int inexact, sg; |
10931 | mpfr_t m; | |
10932 | tree result_lg; | |
10933 | ||
10934 | mpfr_init2 (m, prec); | |
10935 | mpfr_from_real (m, ra, GMP_RNDN); | |
10936 | mpfr_clear_flags (); | |
e2eb2b7f | 10937 | inexact = mpfr_lgamma (m, &sg, m, rnd); |
e84da7c1 | 10938 | result_lg = do_mpfr_ckconv (m, type, inexact); |
10939 | mpfr_clear (m); | |
10940 | if (result_lg) | |
10941 | { | |
10942 | tree result_sg; | |
10943 | ||
10944 | /* Dereference the arg_sg pointer argument. */ | |
10945 | arg_sg = build_fold_indirect_ref (arg_sg); | |
10946 | /* Assign the signgam value into *arg_sg. */ | |
10947 | result_sg = fold_build2 (MODIFY_EXPR, | |
10948 | TREE_TYPE (arg_sg), arg_sg, | |
7002a1c8 | 10949 | build_int_cst (TREE_TYPE (arg_sg), sg)); |
e84da7c1 | 10950 | TREE_SIDE_EFFECTS (result_sg) = 1; |
10951 | /* Combine the signgam assignment with the lgamma result. */ | |
10952 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
10953 | result_sg, result_lg)); | |
10954 | } | |
10955 | } | |
10956 | } | |
10957 | ||
10958 | return result; | |
10959 | } | |
75a70cf9 | 10960 | |
c699fab8 | 10961 | /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument |
10962 | mpc function FUNC on it and return the resulting value as a tree | |
10963 | with type TYPE. The mpfr precision is set to the precision of | |
10964 | TYPE. We assume that function FUNC returns zero if the result | |
652d9409 | 10965 | could be calculated exactly within the requested precision. If |
10966 | DO_NONFINITE is true, then fold expressions containing Inf or NaN | |
10967 | in the arguments and/or results. */ | |
c699fab8 | 10968 | |
63e89698 | 10969 | tree |
652d9409 | 10970 | do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite, |
c699fab8 | 10971 | int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t)) |
10972 | { | |
10973 | tree result = NULL_TREE; | |
48e1416a | 10974 | |
c699fab8 | 10975 | STRIP_NOPS (arg0); |
10976 | STRIP_NOPS (arg1); | |
10977 | ||
10978 | /* To proceed, MPFR must exactly represent the target floating point | |
10979 | format, which only happens when the target base equals two. */ | |
10980 | if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0) | |
10981 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE | |
10982 | && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1) | |
10983 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE | |
10984 | && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2) | |
10985 | { | |
10986 | const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0)); | |
10987 | const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0)); | |
10988 | const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1)); | |
10989 | const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1)); | |
10990 | ||
652d9409 | 10991 | if (do_nonfinite |
10992 | || (real_isfinite (re0) && real_isfinite (im0) | |
10993 | && real_isfinite (re1) && real_isfinite (im1))) | |
c699fab8 | 10994 | { |
10995 | const struct real_format *const fmt = | |
10996 | REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type))); | |
10997 | const int prec = fmt->p; | |
10998 | const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN; | |
10999 | const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN; | |
11000 | int inexact; | |
11001 | mpc_t m0, m1; | |
48e1416a | 11002 | |
c699fab8 | 11003 | mpc_init2 (m0, prec); |
11004 | mpc_init2 (m1, prec); | |
9af5ce0c | 11005 | mpfr_from_real (mpc_realref (m0), re0, rnd); |
11006 | mpfr_from_real (mpc_imagref (m0), im0, rnd); | |
11007 | mpfr_from_real (mpc_realref (m1), re1, rnd); | |
11008 | mpfr_from_real (mpc_imagref (m1), im1, rnd); | |
c699fab8 | 11009 | mpfr_clear_flags (); |
11010 | inexact = func (m0, m0, m1, crnd); | |
652d9409 | 11011 | result = do_mpc_ckconv (m0, type, inexact, do_nonfinite); |
c699fab8 | 11012 | mpc_clear (m0); |
11013 | mpc_clear (m1); | |
11014 | } | |
11015 | } | |
11016 | ||
11017 | return result; | |
11018 | } | |
239d491a | 11019 | |
75a70cf9 | 11020 | /* A wrapper function for builtin folding that prevents warnings for |
11021 | "statement without effect" and the like, caused by removing the | |
11022 | call node earlier than the warning is generated. */ | |
11023 | ||
11024 | tree | |
1a91d914 | 11025 | fold_call_stmt (gcall *stmt, bool ignore) |
75a70cf9 | 11026 | { |
11027 | tree ret = NULL_TREE; | |
11028 | tree fndecl = gimple_call_fndecl (stmt); | |
389dd41b | 11029 | location_t loc = gimple_location (stmt); |
a0e9bfbb | 11030 | if (fndecl && fndecl_built_in_p (fndecl) |
75a70cf9 | 11031 | && !gimple_call_va_arg_pack_p (stmt)) |
11032 | { | |
11033 | int nargs = gimple_call_num_args (stmt); | |
9845fb99 | 11034 | tree *args = (nargs > 0 |
11035 | ? gimple_call_arg_ptr (stmt, 0) | |
11036 | : &error_mark_node); | |
75a70cf9 | 11037 | |
198622c0 | 11038 | if (avoid_folding_inline_builtin (fndecl)) |
11039 | return NULL_TREE; | |
75a70cf9 | 11040 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
11041 | { | |
9845fb99 | 11042 | return targetm.fold_builtin (fndecl, nargs, args, ignore); |
75a70cf9 | 11043 | } |
11044 | else | |
11045 | { | |
9d884767 | 11046 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); |
75a70cf9 | 11047 | if (ret) |
11048 | { | |
11049 | /* Propagate location information from original call to | |
11050 | expansion of builtin. Otherwise things like | |
11051 | maybe_emit_chk_warning, that operate on the expansion | |
11052 | of a builtin, will use the wrong location information. */ | |
11053 | if (gimple_has_location (stmt)) | |
11054 | { | |
11055 | tree realret = ret; | |
11056 | if (TREE_CODE (ret) == NOP_EXPR) | |
11057 | realret = TREE_OPERAND (ret, 0); | |
11058 | if (CAN_HAVE_LOCATION_P (realret) | |
11059 | && !EXPR_HAS_LOCATION (realret)) | |
389dd41b | 11060 | SET_EXPR_LOCATION (realret, loc); |
75a70cf9 | 11061 | return realret; |
11062 | } | |
11063 | return ret; | |
11064 | } | |
11065 | } | |
11066 | } | |
11067 | return NULL_TREE; | |
11068 | } | |
7bfefa9d | 11069 | |
b9a16870 | 11070 | /* Look up the function in builtin_decl that corresponds to DECL |
7bfefa9d | 11071 | and set ASMSPEC as its user assembler name. DECL must be a |
11072 | function decl that declares a builtin. */ | |
11073 | ||
11074 | void | |
11075 | set_builtin_user_assembler_name (tree decl, const char *asmspec) | |
11076 | { | |
a0e9bfbb | 11077 | gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL) |
7bfefa9d | 11078 | && asmspec != 0); |
11079 | ||
61ffc71a | 11080 | tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl)); |
4d8e0d6d | 11081 | set_user_assembler_name (builtin, asmspec); |
61ffc71a | 11082 | |
11083 | if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS | |
11084 | && INT_TYPE_SIZE < BITS_PER_WORD) | |
7bfefa9d | 11085 | { |
44504d18 | 11086 | scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require (); |
61ffc71a | 11087 | set_user_assembler_libfunc ("ffs", asmspec); |
44504d18 | 11088 | set_optab_libfunc (ffs_optab, mode, "ffs"); |
7bfefa9d | 11089 | } |
11090 | } | |
a6b74a67 | 11091 | |
11092 | /* Return true if DECL is a builtin that expands to a constant or similarly | |
11093 | simple code. */ | |
11094 | bool | |
11095 | is_simple_builtin (tree decl) | |
11096 | { | |
a0e9bfbb | 11097 | if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL)) |
a6b74a67 | 11098 | switch (DECL_FUNCTION_CODE (decl)) |
11099 | { | |
11100 | /* Builtins that expand to constants. */ | |
11101 | case BUILT_IN_CONSTANT_P: | |
11102 | case BUILT_IN_EXPECT: | |
11103 | case BUILT_IN_OBJECT_SIZE: | |
11104 | case BUILT_IN_UNREACHABLE: | |
11105 | /* Simple register moves or loads from stack. */ | |
fca0886c | 11106 | case BUILT_IN_ASSUME_ALIGNED: |
a6b74a67 | 11107 | case BUILT_IN_RETURN_ADDRESS: |
11108 | case BUILT_IN_EXTRACT_RETURN_ADDR: | |
11109 | case BUILT_IN_FROB_RETURN_ADDR: | |
11110 | case BUILT_IN_RETURN: | |
11111 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
11112 | case BUILT_IN_FRAME_ADDRESS: | |
11113 | case BUILT_IN_VA_END: | |
11114 | case BUILT_IN_STACK_SAVE: | |
11115 | case BUILT_IN_STACK_RESTORE: | |
11116 | /* Exception state returns or moves registers around. */ | |
11117 | case BUILT_IN_EH_FILTER: | |
11118 | case BUILT_IN_EH_POINTER: | |
11119 | case BUILT_IN_EH_COPY_VALUES: | |
11120 | return true; | |
11121 | ||
11122 | default: | |
11123 | return false; | |
11124 | } | |
11125 | ||
11126 | return false; | |
11127 | } | |
11128 | ||
11129 | /* Return true if DECL is a builtin that is not expensive, i.e., they are | |
11130 | most probably expanded inline into reasonably simple code. This is a | |
11131 | superset of is_simple_builtin. */ | |
11132 | bool | |
11133 | is_inexpensive_builtin (tree decl) | |
11134 | { | |
11135 | if (!decl) | |
11136 | return false; | |
11137 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD) | |
11138 | return true; | |
11139 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
11140 | switch (DECL_FUNCTION_CODE (decl)) | |
11141 | { | |
11142 | case BUILT_IN_ABS: | |
2b34677f | 11143 | CASE_BUILT_IN_ALLOCA: |
74bdbe96 | 11144 | case BUILT_IN_BSWAP16: |
a6b74a67 | 11145 | case BUILT_IN_BSWAP32: |
11146 | case BUILT_IN_BSWAP64: | |
11147 | case BUILT_IN_CLZ: | |
11148 | case BUILT_IN_CLZIMAX: | |
11149 | case BUILT_IN_CLZL: | |
11150 | case BUILT_IN_CLZLL: | |
11151 | case BUILT_IN_CTZ: | |
11152 | case BUILT_IN_CTZIMAX: | |
11153 | case BUILT_IN_CTZL: | |
11154 | case BUILT_IN_CTZLL: | |
11155 | case BUILT_IN_FFS: | |
11156 | case BUILT_IN_FFSIMAX: | |
11157 | case BUILT_IN_FFSL: | |
11158 | case BUILT_IN_FFSLL: | |
11159 | case BUILT_IN_IMAXABS: | |
11160 | case BUILT_IN_FINITE: | |
11161 | case BUILT_IN_FINITEF: | |
11162 | case BUILT_IN_FINITEL: | |
11163 | case BUILT_IN_FINITED32: | |
11164 | case BUILT_IN_FINITED64: | |
11165 | case BUILT_IN_FINITED128: | |
11166 | case BUILT_IN_FPCLASSIFY: | |
11167 | case BUILT_IN_ISFINITE: | |
11168 | case BUILT_IN_ISINF_SIGN: | |
11169 | case BUILT_IN_ISINF: | |
11170 | case BUILT_IN_ISINFF: | |
11171 | case BUILT_IN_ISINFL: | |
11172 | case BUILT_IN_ISINFD32: | |
11173 | case BUILT_IN_ISINFD64: | |
11174 | case BUILT_IN_ISINFD128: | |
11175 | case BUILT_IN_ISNAN: | |
11176 | case BUILT_IN_ISNANF: | |
11177 | case BUILT_IN_ISNANL: | |
11178 | case BUILT_IN_ISNAND32: | |
11179 | case BUILT_IN_ISNAND64: | |
11180 | case BUILT_IN_ISNAND128: | |
11181 | case BUILT_IN_ISNORMAL: | |
11182 | case BUILT_IN_ISGREATER: | |
11183 | case BUILT_IN_ISGREATEREQUAL: | |
11184 | case BUILT_IN_ISLESS: | |
11185 | case BUILT_IN_ISLESSEQUAL: | |
11186 | case BUILT_IN_ISLESSGREATER: | |
11187 | case BUILT_IN_ISUNORDERED: | |
11188 | case BUILT_IN_VA_ARG_PACK: | |
11189 | case BUILT_IN_VA_ARG_PACK_LEN: | |
11190 | case BUILT_IN_VA_COPY: | |
11191 | case BUILT_IN_TRAP: | |
11192 | case BUILT_IN_SAVEREGS: | |
11193 | case BUILT_IN_POPCOUNTL: | |
11194 | case BUILT_IN_POPCOUNTLL: | |
11195 | case BUILT_IN_POPCOUNTIMAX: | |
11196 | case BUILT_IN_POPCOUNT: | |
11197 | case BUILT_IN_PARITYL: | |
11198 | case BUILT_IN_PARITYLL: | |
11199 | case BUILT_IN_PARITYIMAX: | |
11200 | case BUILT_IN_PARITY: | |
11201 | case BUILT_IN_LABS: | |
11202 | case BUILT_IN_LLABS: | |
11203 | case BUILT_IN_PREFETCH: | |
ca4c3545 | 11204 | case BUILT_IN_ACC_ON_DEVICE: |
a6b74a67 | 11205 | return true; |
11206 | ||
11207 | default: | |
11208 | return is_simple_builtin (decl); | |
11209 | } | |
11210 | ||
11211 | return false; | |
11212 | } | |
507a998e | 11213 | |
11214 | /* Return true if T is a constant and the value cast to a target char | |
11215 | can be represented by a host char. | |
11216 | Store the casted char constant in *P if so. */ | |
11217 | ||
11218 | bool | |
11219 | target_char_cst_p (tree t, char *p) | |
11220 | { | |
11221 | if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR) | |
11222 | return false; | |
11223 | ||
11224 | *p = (char)tree_to_uhwi (t); | |
11225 | return true; | |
11226 | } |