]>
Commit | Line | Data |
---|---|---|
53800dbe | 1 | /* Expand builtin functions. |
f1717362 | 2 | Copyright (C) 1988-2016 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" |
31 | #include "gimple.h" | |
7c29e30e | 32 | #include "predict.h" |
33 | #include "tm_p.h" | |
34 | #include "stringpool.h" | |
35 | #include "tree-ssanames.h" | |
36 | #include "expmed.h" | |
37 | #include "optabs.h" | |
7c29e30e | 38 | #include "emit-rtl.h" |
39 | #include "recog.h" | |
7c29e30e | 40 | #include "diagnostic-core.h" |
b20a8bb4 | 41 | #include "alias.h" |
b20a8bb4 | 42 | #include "fold-const.h" |
6c21be92 | 43 | #include "fold-const-call.h" |
9ed99284 | 44 | #include "stor-layout.h" |
45 | #include "calls.h" | |
46 | #include "varasm.h" | |
47 | #include "tree-object-size.h" | |
dae0b5cb | 48 | #include "realmpfr.h" |
94ea8568 | 49 | #include "cfgrtl.h" |
53800dbe | 50 | #include "except.h" |
d53441c8 | 51 | #include "dojump.h" |
52 | #include "explow.h" | |
d53441c8 | 53 | #include "stmt.h" |
53800dbe | 54 | #include "expr.h" |
d8fc4d0b | 55 | #include "libfuncs.h" |
53800dbe | 56 | #include "output.h" |
57 | #include "typeclass.h" | |
63c62881 | 58 | #include "langhooks.h" |
162719b3 | 59 | #include "value-prof.h" |
3b9c3a16 | 60 | #include "builtins.h" |
f9acf11a | 61 | #include "asan.h" |
d037099f | 62 | #include "cilk.h" |
058a1b7a | 63 | #include "tree-chkp.h" |
64 | #include "rtl-chkp.h" | |
1f24b8e9 | 65 | #include "internal-fn.h" |
e3240774 | 66 | #include "case-cfn-macros.h" |
53800dbe | 67 | |
5383fb56 | 68 | |
3b9c3a16 | 69 | struct target_builtins default_target_builtins; |
70 | #if SWITCHABLE_TARGET | |
71 | struct target_builtins *this_target_builtins = &default_target_builtins; | |
72 | #endif | |
73 | ||
ab7943b9 | 74 | /* Define the names of the builtin function types and codes. */ |
96423453 | 75 | const char *const built_in_class_names[BUILT_IN_LAST] |
ab7943b9 | 76 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
77 | ||
9cfddb70 | 78 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X, |
0dfc45b5 | 79 | const char * built_in_names[(int) END_BUILTINS] = |
4e9d90c7 | 80 | { |
81 | #include "builtins.def" | |
82 | }; | |
ab7943b9 | 83 | |
cffdfb3d | 84 | /* Setup an array of builtin_info_type, make sure each element decl is |
df94cd3b | 85 | initialized to NULL_TREE. */ |
cffdfb3d | 86 | builtin_info_type builtin_info[(int)END_BUILTINS]; |
df94cd3b | 87 | |
0b049e15 | 88 | /* Non-zero if __builtin_constant_p should be folded right away. */ |
89 | bool force_folding_builtin_constant_p; | |
90 | ||
3754d046 | 91 | static rtx c_readstr (const char *, machine_mode); |
aecda0d6 | 92 | static int target_char_cast (tree, char *); |
d8ae1baa | 93 | static rtx get_memory_rtx (tree, tree); |
aecda0d6 | 94 | static int apply_args_size (void); |
95 | static int apply_result_size (void); | |
aecda0d6 | 96 | static rtx result_vector (int, rtx); |
aecda0d6 | 97 | static void expand_builtin_prefetch (tree); |
98 | static rtx expand_builtin_apply_args (void); | |
99 | static rtx expand_builtin_apply_args_1 (void); | |
100 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
101 | static void expand_builtin_return (rtx); | |
102 | static enum type_class type_to_class (tree); | |
103 | static rtx expand_builtin_classify_type (tree); | |
6b43bae4 | 104 | static rtx expand_builtin_mathfn_3 (tree, rtx, rtx); |
7e0713b1 | 105 | static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx); |
f97eea22 | 106 | static rtx expand_builtin_interclass_mathfn (tree, rtx); |
c3147c1a | 107 | static rtx expand_builtin_sincos (tree); |
f97eea22 | 108 | static rtx expand_builtin_cexpi (tree, rtx); |
ff1b14e4 | 109 | static rtx expand_builtin_int_roundingfn (tree, rtx); |
110 | static rtx expand_builtin_int_roundingfn_2 (tree, rtx); | |
79012a9d | 111 | static rtx expand_builtin_next_arg (void); |
aecda0d6 | 112 | static rtx expand_builtin_va_start (tree); |
113 | static rtx expand_builtin_va_end (tree); | |
114 | static rtx expand_builtin_va_copy (tree); | |
a65c4d64 | 115 | static rtx expand_builtin_strcmp (tree, rtx); |
3754d046 | 116 | static rtx expand_builtin_strncmp (tree, rtx, machine_mode); |
117 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode); | |
a65c4d64 | 118 | static rtx expand_builtin_memcpy (tree, rtx); |
f21337ef | 119 | static rtx expand_builtin_memcpy_with_bounds (tree, rtx); |
120 | static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree); | |
3754d046 | 121 | static rtx expand_builtin_mempcpy (tree, rtx, machine_mode); |
f21337ef | 122 | static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode); |
48e1416a | 123 | static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, |
f21337ef | 124 | machine_mode, int, tree); |
a65c4d64 | 125 | static rtx expand_builtin_strcpy (tree, rtx); |
126 | static rtx expand_builtin_strcpy_args (tree, tree, rtx); | |
3754d046 | 127 | static rtx expand_builtin_stpcpy (tree, rtx, machine_mode); |
a65c4d64 | 128 | static rtx expand_builtin_strncpy (tree, rtx); |
3754d046 | 129 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode); |
130 | static rtx expand_builtin_memset (tree, rtx, machine_mode); | |
f21337ef | 131 | static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode); |
3754d046 | 132 | static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree); |
aecda0d6 | 133 | static rtx expand_builtin_bzero (tree); |
3754d046 | 134 | static rtx expand_builtin_strlen (tree, rtx, machine_mode); |
5be42b39 | 135 | static rtx expand_builtin_alloca (tree, bool); |
3754d046 | 136 | static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab); |
aecda0d6 | 137 | static rtx expand_builtin_frame_address (tree, tree); |
389dd41b | 138 | static tree stabilize_va_list_loc (location_t, tree, int); |
aecda0d6 | 139 | static rtx expand_builtin_expect (tree, rtx); |
140 | static tree fold_builtin_constant_p (tree); | |
141 | static tree fold_builtin_classify_type (tree); | |
c7cbde74 | 142 | static tree fold_builtin_strlen (location_t, tree, tree); |
389dd41b | 143 | static tree fold_builtin_inf (location_t, tree, int); |
389dd41b | 144 | static tree rewrite_call_expr (location_t, tree, int, tree, int, ...); |
b7bf20db | 145 | static bool validate_arg (const_tree, enum tree_code code); |
aecda0d6 | 146 | static rtx expand_builtin_fabs (tree, rtx, rtx); |
27f261ef | 147 | static rtx expand_builtin_signbit (tree, rtx); |
389dd41b | 148 | static tree fold_builtin_strchr (location_t, tree, tree, tree); |
149 | static tree fold_builtin_memchr (location_t, tree, tree, tree, tree); | |
150 | static tree fold_builtin_memcmp (location_t, tree, tree, tree); | |
151 | static tree fold_builtin_strcmp (location_t, tree, tree); | |
152 | static tree fold_builtin_strncmp (location_t, tree, tree, tree); | |
389dd41b | 153 | static tree fold_builtin_isascii (location_t, tree); |
154 | static tree fold_builtin_toascii (location_t, tree); | |
155 | static tree fold_builtin_isdigit (location_t, tree); | |
156 | static tree fold_builtin_fabs (location_t, tree, tree); | |
157 | static tree fold_builtin_abs (location_t, tree, tree); | |
158 | static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code, | |
d5019fe8 | 159 | enum tree_code); |
e80cc485 | 160 | static tree fold_builtin_0 (location_t, tree); |
161 | static tree fold_builtin_1 (location_t, tree, tree); | |
162 | static tree fold_builtin_2 (location_t, tree, tree, tree); | |
163 | static tree fold_builtin_3 (location_t, tree, tree, tree, tree); | |
164 | static tree fold_builtin_varargs (location_t, tree, tree*, int); | |
389dd41b | 165 | |
166 | static tree fold_builtin_strpbrk (location_t, tree, tree, tree); | |
167 | static tree fold_builtin_strstr (location_t, tree, tree, tree); | |
168 | static tree fold_builtin_strrchr (location_t, tree, tree, tree); | |
389dd41b | 169 | static tree fold_builtin_strspn (location_t, tree, tree); |
170 | static tree fold_builtin_strcspn (location_t, tree, tree); | |
4ee9c684 | 171 | |
0a39fd54 | 172 | static rtx expand_builtin_object_size (tree); |
3754d046 | 173 | static rtx expand_builtin_memory_chk (tree, rtx, machine_mode, |
0a39fd54 | 174 | enum built_in_function); |
175 | static void maybe_emit_chk_warning (tree, enum built_in_function); | |
176 | static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); | |
2c281b15 | 177 | static void maybe_emit_free_warning (tree); |
c2f47e15 | 178 | static tree fold_builtin_object_size (tree, tree); |
99eabcc1 | 179 | |
e788f202 | 180 | unsigned HOST_WIDE_INT target_newline; |
b9ea678c | 181 | unsigned HOST_WIDE_INT target_percent; |
99eabcc1 | 182 | static unsigned HOST_WIDE_INT target_c; |
183 | static unsigned HOST_WIDE_INT target_s; | |
aea88c77 | 184 | char target_percent_c[3]; |
b9ea678c | 185 | char target_percent_s[3]; |
e788f202 | 186 | char target_percent_s_newline[4]; |
e5407ca6 | 187 | static tree do_mpfr_remquo (tree, tree, tree); |
e84da7c1 | 188 | static tree do_mpfr_lgamma_r (tree, tree, tree); |
1cd6e20d | 189 | static void expand_builtin_sync_synchronize (void); |
0a39fd54 | 190 | |
7bfefa9d | 191 | /* Return true if NAME starts with __builtin_ or __sync_. */ |
192 | ||
b29139ad | 193 | static bool |
1c47b3e8 | 194 | is_builtin_name (const char *name) |
b6a5fc45 | 195 | { |
b6a5fc45 | 196 | if (strncmp (name, "__builtin_", 10) == 0) |
197 | return true; | |
198 | if (strncmp (name, "__sync_", 7) == 0) | |
199 | return true; | |
1cd6e20d | 200 | if (strncmp (name, "__atomic_", 9) == 0) |
201 | return true; | |
a89e6c15 | 202 | if (flag_cilkplus |
d037099f | 203 | && (!strcmp (name, "__cilkrts_detach") |
204 | || !strcmp (name, "__cilkrts_pop_frame"))) | |
205 | return true; | |
b6a5fc45 | 206 | return false; |
207 | } | |
4ee9c684 | 208 | |
7bfefa9d | 209 | |
210 | /* Return true if DECL is a function symbol representing a built-in. */ | |
211 | ||
212 | bool | |
213 | is_builtin_fn (tree decl) | |
214 | { | |
215 | return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl); | |
216 | } | |
217 | ||
1c47b3e8 | 218 | /* Return true if NODE should be considered for inline expansion regardless |
219 | of the optimization level. This means whenever a function is invoked with | |
220 | its "internal" name, which normally contains the prefix "__builtin". */ | |
221 | ||
ae62deea | 222 | bool |
1c47b3e8 | 223 | called_as_built_in (tree node) |
224 | { | |
225 | /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since | |
226 | we want the name used to call the function, not the name it | |
227 | will have. */ | |
228 | const char *name = IDENTIFIER_POINTER (DECL_NAME (node)); | |
229 | return is_builtin_name (name); | |
230 | } | |
231 | ||
ceea063b | 232 | /* Compute values M and N such that M divides (address of EXP - N) and such |
233 | that N < M. If these numbers can be determined, store M in alignp and N in | |
234 | *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to | |
235 | *alignp and any bit-offset to *bitposp. | |
0d8f7716 | 236 | |
237 | Note that the address (and thus the alignment) computed here is based | |
238 | on the address to which a symbol resolves, whereas DECL_ALIGN is based | |
239 | on the address at which an object is actually located. These two | |
240 | addresses are not always the same. For example, on ARM targets, | |
241 | the address &foo of a Thumb function foo() has the lowest bit set, | |
3482bf13 | 242 | whereas foo() itself starts on an even address. |
698537d1 | 243 | |
3482bf13 | 244 | If ADDR_P is true we are taking the address of the memory reference EXP |
245 | and thus cannot rely on the access taking place. */ | |
246 | ||
247 | static bool | |
248 | get_object_alignment_2 (tree exp, unsigned int *alignp, | |
249 | unsigned HOST_WIDE_INT *bitposp, bool addr_p) | |
698537d1 | 250 | { |
98ab9e8f | 251 | HOST_WIDE_INT bitsize, bitpos; |
252 | tree offset; | |
3754d046 | 253 | machine_mode mode; |
292237f3 | 254 | int unsignedp, reversep, volatilep; |
c8a2b4ff | 255 | unsigned int align = BITS_PER_UNIT; |
ceea063b | 256 | bool known_alignment = false; |
698537d1 | 257 | |
98ab9e8f | 258 | /* Get the innermost object and the constant (bitpos) and possibly |
259 | variable (offset) offset of the access. */ | |
292237f3 | 260 | exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode, |
261 | &unsignedp, &reversep, &volatilep, true); | |
98ab9e8f | 262 | |
263 | /* Extract alignment information from the innermost object and | |
264 | possibly adjust bitpos and offset. */ | |
3482bf13 | 265 | if (TREE_CODE (exp) == FUNCTION_DECL) |
0d8f7716 | 266 | { |
3482bf13 | 267 | /* Function addresses can encode extra information besides their |
268 | alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION | |
269 | allows the low bit to be used as a virtual bit, we know | |
270 | that the address itself must be at least 2-byte aligned. */ | |
271 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn) | |
272 | align = 2 * BITS_PER_UNIT; | |
0d8f7716 | 273 | } |
3482bf13 | 274 | else if (TREE_CODE (exp) == LABEL_DECL) |
275 | ; | |
276 | else if (TREE_CODE (exp) == CONST_DECL) | |
98ab9e8f | 277 | { |
3482bf13 | 278 | /* The alignment of a CONST_DECL is determined by its initializer. */ |
279 | exp = DECL_INITIAL (exp); | |
98ab9e8f | 280 | align = TYPE_ALIGN (TREE_TYPE (exp)); |
3482bf13 | 281 | if (CONSTANT_CLASS_P (exp)) |
282 | align = (unsigned) CONSTANT_ALIGNMENT (exp, align); | |
e532afed | 283 | |
3482bf13 | 284 | known_alignment = true; |
98ab9e8f | 285 | } |
3482bf13 | 286 | else if (DECL_P (exp)) |
ceea063b | 287 | { |
3482bf13 | 288 | align = DECL_ALIGN (exp); |
ceea063b | 289 | known_alignment = true; |
ceea063b | 290 | } |
3482bf13 | 291 | else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR) |
ceea063b | 292 | { |
ceea063b | 293 | align = TYPE_ALIGN (TREE_TYPE (exp)); |
294 | } | |
3482bf13 | 295 | else if (TREE_CODE (exp) == INDIRECT_REF |
296 | || TREE_CODE (exp) == MEM_REF | |
297 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
98ab9e8f | 298 | { |
299 | tree addr = TREE_OPERAND (exp, 0); | |
ceea063b | 300 | unsigned ptr_align; |
301 | unsigned HOST_WIDE_INT ptr_bitpos; | |
ab1e78e5 | 302 | unsigned HOST_WIDE_INT ptr_bitmask = ~0; |
ceea063b | 303 | |
ab1e78e5 | 304 | /* If the address is explicitely aligned, handle that. */ |
98ab9e8f | 305 | if (TREE_CODE (addr) == BIT_AND_EXPR |
306 | && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST) | |
307 | { | |
ab1e78e5 | 308 | ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)); |
309 | ptr_bitmask *= BITS_PER_UNIT; | |
310 | align = ptr_bitmask & -ptr_bitmask; | |
98ab9e8f | 311 | addr = TREE_OPERAND (addr, 0); |
312 | } | |
ceea063b | 313 | |
3482bf13 | 314 | known_alignment |
315 | = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos); | |
3482bf13 | 316 | align = MAX (ptr_align, align); |
317 | ||
ab1e78e5 | 318 | /* Re-apply explicit alignment to the bitpos. */ |
319 | ptr_bitpos &= ptr_bitmask; | |
320 | ||
4083990a | 321 | /* The alignment of the pointer operand in a TARGET_MEM_REF |
322 | has to take the variable offset parts into account. */ | |
3482bf13 | 323 | if (TREE_CODE (exp) == TARGET_MEM_REF) |
153c3b50 | 324 | { |
3482bf13 | 325 | if (TMR_INDEX (exp)) |
326 | { | |
327 | unsigned HOST_WIDE_INT step = 1; | |
328 | if (TMR_STEP (exp)) | |
f9ae6f95 | 329 | step = TREE_INT_CST_LOW (TMR_STEP (exp)); |
3482bf13 | 330 | align = MIN (align, (step & -step) * BITS_PER_UNIT); |
331 | } | |
332 | if (TMR_INDEX2 (exp)) | |
333 | align = BITS_PER_UNIT; | |
334 | known_alignment = false; | |
153c3b50 | 335 | } |
ceea063b | 336 | |
3482bf13 | 337 | /* When EXP is an actual memory reference then we can use |
338 | TYPE_ALIGN of a pointer indirection to derive alignment. | |
339 | Do so only if get_pointer_alignment_1 did not reveal absolute | |
4083990a | 340 | alignment knowledge and if using that alignment would |
341 | improve the situation. */ | |
342 | if (!addr_p && !known_alignment | |
343 | && TYPE_ALIGN (TREE_TYPE (exp)) > align) | |
344 | align = TYPE_ALIGN (TREE_TYPE (exp)); | |
345 | else | |
346 | { | |
347 | /* Else adjust bitpos accordingly. */ | |
348 | bitpos += ptr_bitpos; | |
349 | if (TREE_CODE (exp) == MEM_REF | |
350 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
e913b5cd | 351 | bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT; |
4083990a | 352 | } |
98ab9e8f | 353 | } |
3482bf13 | 354 | else if (TREE_CODE (exp) == STRING_CST) |
153c3b50 | 355 | { |
3482bf13 | 356 | /* STRING_CST are the only constant objects we allow to be not |
357 | wrapped inside a CONST_DECL. */ | |
358 | align = TYPE_ALIGN (TREE_TYPE (exp)); | |
3482bf13 | 359 | if (CONSTANT_CLASS_P (exp)) |
360 | align = (unsigned) CONSTANT_ALIGNMENT (exp, align); | |
e532afed | 361 | |
3482bf13 | 362 | known_alignment = true; |
98ab9e8f | 363 | } |
98ab9e8f | 364 | |
365 | /* If there is a non-constant offset part extract the maximum | |
366 | alignment that can prevail. */ | |
c8a2b4ff | 367 | if (offset) |
98ab9e8f | 368 | { |
ad464c56 | 369 | unsigned int trailing_zeros = tree_ctz (offset); |
c8a2b4ff | 370 | if (trailing_zeros < HOST_BITS_PER_INT) |
98ab9e8f | 371 | { |
c8a2b4ff | 372 | unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT; |
373 | if (inner) | |
374 | align = MIN (align, inner); | |
98ab9e8f | 375 | } |
98ab9e8f | 376 | } |
377 | ||
3482bf13 | 378 | *alignp = align; |
379 | *bitposp = bitpos & (*alignp - 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) | |
409 | align = (bitpos & -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) | |
507 | align = (bitpos & -bitpos); | |
508 | ||
509 | return align; | |
510 | } | |
511 | ||
53800dbe | 512 | /* Compute the length of a C string. TREE_STRING_LENGTH is not the right |
513 | way, because it could contain a zero byte in the middle. | |
514 | TREE_STRING_LENGTH is the size of the character array, not the string. | |
515 | ||
4172d65e | 516 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
c09841f6 | 517 | into the instruction stream and zero if it is going to be expanded. |
4172d65e | 518 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
681fab1e | 519 | is returned, otherwise NULL, since |
520 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
521 | evaluate the side-effects. | |
522 | ||
6bda159e | 523 | If ONLY_VALUE is two then we do not emit warnings about out-of-bound |
524 | accesses. Note that this implies the result is not going to be emitted | |
525 | into the instruction stream. | |
526 | ||
902de8ed | 527 | The value returned is of type `ssizetype'. |
528 | ||
53800dbe | 529 | Unfortunately, string_constant can't access the values of const char |
530 | arrays with initializers, so neither can we do so here. */ | |
531 | ||
4ee9c684 | 532 | tree |
681fab1e | 533 | c_strlen (tree src, int only_value) |
53800dbe | 534 | { |
535 | tree offset_node; | |
27d0c333 | 536 | HOST_WIDE_INT offset; |
537 | int max; | |
44acf429 | 538 | const char *ptr; |
da136652 | 539 | location_t loc; |
53800dbe | 540 | |
681fab1e | 541 | STRIP_NOPS (src); |
542 | if (TREE_CODE (src) == COND_EXPR | |
543 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
544 | { | |
545 | tree len1, len2; | |
546 | ||
547 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value); | |
548 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value); | |
0862b7e9 | 549 | if (tree_int_cst_equal (len1, len2)) |
681fab1e | 550 | return len1; |
551 | } | |
552 | ||
553 | if (TREE_CODE (src) == COMPOUND_EXPR | |
554 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
555 | return c_strlen (TREE_OPERAND (src, 1), only_value); | |
556 | ||
3df42822 | 557 | loc = EXPR_LOC_OR_LOC (src, input_location); |
da136652 | 558 | |
53800dbe | 559 | src = string_constant (src, &offset_node); |
560 | if (src == 0) | |
c2f47e15 | 561 | return NULL_TREE; |
902de8ed | 562 | |
83d79705 | 563 | max = TREE_STRING_LENGTH (src) - 1; |
53800dbe | 564 | ptr = TREE_STRING_POINTER (src); |
902de8ed | 565 | |
53800dbe | 566 | if (offset_node && TREE_CODE (offset_node) != INTEGER_CST) |
567 | { | |
568 | /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't | |
569 | compute the offset to the following null if we don't know where to | |
570 | start searching for it. */ | |
571 | int i; | |
902de8ed | 572 | |
53800dbe | 573 | for (i = 0; i < max; i++) |
574 | if (ptr[i] == 0) | |
c2f47e15 | 575 | return NULL_TREE; |
902de8ed | 576 | |
53800dbe | 577 | /* We don't know the starting offset, but we do know that the string |
578 | has no internal zero bytes. We can assume that the offset falls | |
579 | within the bounds of the string; otherwise, the programmer deserves | |
580 | what he gets. Subtract the offset from the length of the string, | |
902de8ed | 581 | and return that. This would perhaps not be valid if we were dealing |
582 | with named arrays in addition to literal string constants. */ | |
583 | ||
da136652 | 584 | return size_diffop_loc (loc, size_int (max), offset_node); |
53800dbe | 585 | } |
586 | ||
587 | /* We have a known offset into the string. Start searching there for | |
27d0c333 | 588 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
dabc4084 | 589 | if (offset_node == 0) |
53800dbe | 590 | offset = 0; |
35ec552a | 591 | else if (! tree_fits_shwi_p (offset_node)) |
dabc4084 | 592 | offset = -1; |
53800dbe | 593 | else |
e913b5cd | 594 | offset = tree_to_shwi (offset_node); |
902de8ed | 595 | |
1f63a7d6 | 596 | /* If the offset is known to be out of bounds, warn, and call strlen at |
597 | runtime. */ | |
2f1c4f17 | 598 | if (offset < 0 || offset > max) |
53800dbe | 599 | { |
1f63a7d6 | 600 | /* Suppress multiple warnings for propagated constant strings. */ |
2f1c4f17 | 601 | if (only_value != 2 |
602 | && !TREE_NO_WARNING (src)) | |
1f63a7d6 | 603 | { |
da136652 | 604 | warning_at (loc, 0, "offset outside bounds of constant string"); |
1f63a7d6 | 605 | TREE_NO_WARNING (src) = 1; |
606 | } | |
c2f47e15 | 607 | return NULL_TREE; |
53800dbe | 608 | } |
902de8ed | 609 | |
53800dbe | 610 | /* Use strlen to search for the first zero byte. Since any strings |
611 | constructed with build_string will have nulls appended, we win even | |
612 | if we get handed something like (char[4])"abcd". | |
613 | ||
614 | Since OFFSET is our starting index into the string, no further | |
615 | calculation is needed. */ | |
902de8ed | 616 | return ssize_int (strlen (ptr + offset)); |
53800dbe | 617 | } |
618 | ||
e913b5cd | 619 | /* Return a constant integer corresponding to target reading |
8c85fcb7 | 620 | GET_MODE_BITSIZE (MODE) bits from string constant STR. */ |
ecc318ff | 621 | |
6840589f | 622 | static rtx |
3754d046 | 623 | c_readstr (const char *str, machine_mode mode) |
6840589f | 624 | { |
6840589f | 625 | HOST_WIDE_INT ch; |
626 | unsigned int i, j; | |
e913b5cd | 627 | HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT]; |
0407eaee | 628 | |
629 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); | |
e913b5cd | 630 | unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1) |
631 | / HOST_BITS_PER_WIDE_INT; | |
632 | ||
a12aa4cc | 633 | gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT); |
e913b5cd | 634 | for (i = 0; i < len; i++) |
635 | tmp[i] = 0; | |
6840589f | 636 | |
6840589f | 637 | ch = 1; |
638 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
639 | { | |
640 | j = i; | |
641 | if (WORDS_BIG_ENDIAN) | |
642 | j = GET_MODE_SIZE (mode) - i - 1; | |
643 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
ad8f8e52 | 644 | && GET_MODE_SIZE (mode) >= UNITS_PER_WORD) |
6840589f | 645 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; |
646 | j *= BITS_PER_UNIT; | |
7d3f6cc7 | 647 | |
6840589f | 648 | if (ch) |
649 | ch = (unsigned char) str[i]; | |
e913b5cd | 650 | tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); |
6840589f | 651 | } |
ddb1be65 | 652 | |
ab2c1de8 | 653 | wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode)); |
e913b5cd | 654 | return immed_wide_int_const (c, mode); |
6840589f | 655 | } |
656 | ||
ecc318ff | 657 | /* Cast a target constant CST to target CHAR and if that value fits into |
5206b159 | 658 | host char type, return zero and put that value into variable pointed to by |
ecc318ff | 659 | P. */ |
660 | ||
661 | static int | |
aecda0d6 | 662 | target_char_cast (tree cst, char *p) |
ecc318ff | 663 | { |
664 | unsigned HOST_WIDE_INT val, hostval; | |
665 | ||
c19686c5 | 666 | if (TREE_CODE (cst) != INTEGER_CST |
ecc318ff | 667 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
668 | return 1; | |
669 | ||
e913b5cd | 670 | /* Do not care if it fits or not right here. */ |
f9ae6f95 | 671 | val = TREE_INT_CST_LOW (cst); |
e913b5cd | 672 | |
ecc318ff | 673 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
674 | val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1; | |
675 | ||
676 | hostval = val; | |
677 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
678 | hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1; | |
679 | ||
680 | if (val != hostval) | |
681 | return 1; | |
682 | ||
683 | *p = hostval; | |
684 | return 0; | |
685 | } | |
686 | ||
4ee9c684 | 687 | /* Similar to save_expr, but assumes that arbitrary code is not executed |
688 | in between the multiple evaluations. In particular, we assume that a | |
689 | non-addressable local variable will not be modified. */ | |
690 | ||
691 | static tree | |
692 | builtin_save_expr (tree exp) | |
693 | { | |
f6c35aa4 | 694 | if (TREE_CODE (exp) == SSA_NAME |
695 | || (TREE_ADDRESSABLE (exp) == 0 | |
696 | && (TREE_CODE (exp) == PARM_DECL | |
697 | || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))) | |
4ee9c684 | 698 | return exp; |
699 | ||
700 | return save_expr (exp); | |
701 | } | |
702 | ||
53800dbe | 703 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
704 | times to get the address of either a higher stack frame, or a return | |
705 | address located within it (depending on FNDECL_CODE). */ | |
902de8ed | 706 | |
c626df3d | 707 | static rtx |
869d0ef0 | 708 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count) |
53800dbe | 709 | { |
710 | int i; | |
869d0ef0 | 711 | rtx tem = INITIAL_FRAME_ADDRESS_RTX; |
3f840859 | 712 | if (tem == NULL_RTX) |
e3e15c50 | 713 | { |
3f840859 | 714 | /* For a zero count with __builtin_return_address, we don't care what |
715 | frame address we return, because target-specific definitions will | |
716 | override us. Therefore frame pointer elimination is OK, and using | |
717 | the soft frame pointer is OK. | |
718 | ||
719 | For a nonzero count, or a zero count with __builtin_frame_address, | |
720 | we require a stable offset from the current frame pointer to the | |
721 | previous one, so we must use the hard frame pointer, and | |
722 | we must disable frame pointer elimination. */ | |
723 | if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
724 | tem = frame_pointer_rtx; | |
725 | else | |
726 | { | |
727 | tem = hard_frame_pointer_rtx; | |
e3e15c50 | 728 | |
3f840859 | 729 | /* Tell reload not to eliminate the frame pointer. */ |
730 | crtl->accesses_prior_frames = 1; | |
731 | } | |
e3e15c50 | 732 | } |
869d0ef0 | 733 | |
53800dbe | 734 | if (count > 0) |
735 | SETUP_FRAME_ADDRESSES (); | |
53800dbe | 736 | |
3a69c60c | 737 | /* On the SPARC, the return address is not in the frame, it is in a |
53800dbe | 738 | register. There is no way to access it off of the current frame |
739 | pointer, but it can be accessed off the previous frame pointer by | |
740 | reading the value from the register window save area. */ | |
a26d6c60 | 741 | if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS) |
53800dbe | 742 | count--; |
53800dbe | 743 | |
744 | /* Scan back COUNT frames to the specified frame. */ | |
745 | for (i = 0; i < count; i++) | |
746 | { | |
747 | /* Assume the dynamic chain pointer is in the word that the | |
748 | frame address points to, unless otherwise specified. */ | |
53800dbe | 749 | tem = DYNAMIC_CHAIN_ADDRESS (tem); |
53800dbe | 750 | tem = memory_address (Pmode, tem); |
00060fc2 | 751 | tem = gen_frame_mem (Pmode, tem); |
83fc1478 | 752 | tem = copy_to_reg (tem); |
53800dbe | 753 | } |
754 | ||
3a69c60c | 755 | /* For __builtin_frame_address, return what we've got. But, on |
756 | the SPARC for example, we may have to add a bias. */ | |
53800dbe | 757 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) |
3a69c60c | 758 | return FRAME_ADDR_RTX (tem); |
53800dbe | 759 | |
3a69c60c | 760 | /* For __builtin_return_address, get the return address from that frame. */ |
53800dbe | 761 | #ifdef RETURN_ADDR_RTX |
762 | tem = RETURN_ADDR_RTX (count, tem); | |
763 | #else | |
764 | tem = memory_address (Pmode, | |
29c05e22 | 765 | plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode))); |
00060fc2 | 766 | tem = gen_frame_mem (Pmode, tem); |
53800dbe | 767 | #endif |
768 | return tem; | |
769 | } | |
770 | ||
f7c44134 | 771 | /* Alias set used for setjmp buffer. */ |
32c2fdea | 772 | static alias_set_type setjmp_alias_set = -1; |
f7c44134 | 773 | |
6b7f6858 | 774 | /* Construct the leading half of a __builtin_setjmp call. Control will |
2c8a1497 | 775 | return to RECEIVER_LABEL. This is also called directly by the SJLJ |
776 | exception handling code. */ | |
53800dbe | 777 | |
6b7f6858 | 778 | void |
aecda0d6 | 779 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
53800dbe | 780 | { |
3754d046 | 781 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 782 | rtx stack_save; |
f7c44134 | 783 | rtx mem; |
53800dbe | 784 | |
f7c44134 | 785 | if (setjmp_alias_set == -1) |
786 | setjmp_alias_set = new_alias_set (); | |
787 | ||
85d654dd | 788 | buf_addr = convert_memory_address (Pmode, buf_addr); |
53800dbe | 789 | |
37ae8504 | 790 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
53800dbe | 791 | |
6b7f6858 | 792 | /* We store the frame pointer and the address of receiver_label in |
793 | the buffer and use the rest of it for the stack save area, which | |
794 | is machine-dependent. */ | |
53800dbe | 795 | |
f7c44134 | 796 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ab6ab77e | 797 | set_mem_alias_set (mem, setjmp_alias_set); |
e3e026e8 | 798 | emit_move_insn (mem, targetm.builtin_setjmp_frame_value ()); |
f7c44134 | 799 | |
29c05e22 | 800 | mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
801 | GET_MODE_SIZE (Pmode))), | |
ab6ab77e | 802 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 803 | |
804 | emit_move_insn (validize_mem (mem), | |
6b7f6858 | 805 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
53800dbe | 806 | |
807 | stack_save = gen_rtx_MEM (sa_mode, | |
29c05e22 | 808 | plus_constant (Pmode, buf_addr, |
53800dbe | 809 | 2 * GET_MODE_SIZE (Pmode))); |
ab6ab77e | 810 | set_mem_alias_set (stack_save, setjmp_alias_set); |
e9c97615 | 811 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
53800dbe | 812 | |
813 | /* If there is further processing to do, do it. */ | |
a3c81e61 | 814 | if (targetm.have_builtin_setjmp_setup ()) |
815 | emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr)); | |
53800dbe | 816 | |
29f09705 | 817 | /* We have a nonlocal label. */ |
18d50ae6 | 818 | cfun->has_nonlocal_label = 1; |
6b7f6858 | 819 | } |
53800dbe | 820 | |
2c8a1497 | 821 | /* Construct the trailing part of a __builtin_setjmp call. This is |
4598ade9 | 822 | also called directly by the SJLJ exception handling code. |
823 | If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */ | |
6b7f6858 | 824 | |
825 | void | |
a3c81e61 | 826 | expand_builtin_setjmp_receiver (rtx receiver_label) |
6b7f6858 | 827 | { |
82c7907c | 828 | rtx chain; |
829 | ||
4598ade9 | 830 | /* Mark the FP as used when we get here, so we have to make sure it's |
53800dbe | 831 | marked as used by this function. */ |
18b42941 | 832 | emit_use (hard_frame_pointer_rtx); |
53800dbe | 833 | |
834 | /* Mark the static chain as clobbered here so life information | |
835 | doesn't get messed up for it. */ | |
82c7907c | 836 | chain = targetm.calls.static_chain (current_function_decl, true); |
837 | if (chain && REG_P (chain)) | |
838 | emit_clobber (chain); | |
53800dbe | 839 | |
840 | /* Now put in the code to restore the frame pointer, and argument | |
491e04ef | 841 | pointer, if needed. */ |
a3c81e61 | 842 | if (! targetm.have_nonlocal_goto ()) |
62dcb5c8 | 843 | { |
844 | /* First adjust our frame pointer to its actual value. It was | |
845 | previously set to the start of the virtual area corresponding to | |
846 | the stacked variables when we branched here and now needs to be | |
847 | adjusted to the actual hardware fp value. | |
848 | ||
849 | Assignments to virtual registers are converted by | |
850 | instantiate_virtual_regs into the corresponding assignment | |
851 | to the underlying register (fp in this case) that makes | |
852 | the original assignment true. | |
853 | So the following insn will actually be decrementing fp by | |
854 | STARTING_FRAME_OFFSET. */ | |
855 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); | |
856 | ||
857 | /* Restoring the frame pointer also modifies the hard frame pointer. | |
858 | Mark it used (so that the previous assignment remains live once | |
859 | the frame pointer is eliminated) and clobbered (to represent the | |
860 | implicit update from the assignment). */ | |
861 | emit_use (hard_frame_pointer_rtx); | |
862 | emit_clobber (hard_frame_pointer_rtx); | |
863 | } | |
53800dbe | 864 | |
a494b6d7 | 865 | if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM]) |
53800dbe | 866 | { |
867 | #ifdef ELIMINABLE_REGS | |
4598ade9 | 868 | /* If the argument pointer can be eliminated in favor of the |
869 | frame pointer, we don't need to restore it. We assume here | |
870 | that if such an elimination is present, it can always be used. | |
871 | This is the case on all known machines; if we don't make this | |
872 | assumption, we do unnecessary saving on many machines. */ | |
53800dbe | 873 | size_t i; |
e99c3a1d | 874 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
53800dbe | 875 | |
3098b2d3 | 876 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
53800dbe | 877 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
878 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
879 | break; | |
880 | ||
3098b2d3 | 881 | if (i == ARRAY_SIZE (elim_regs)) |
53800dbe | 882 | #endif |
883 | { | |
884 | /* Now restore our arg pointer from the address at which it | |
05927e40 | 885 | was saved in our stack frame. */ |
27a7a23a | 886 | emit_move_insn (crtl->args.internal_arg_pointer, |
b079a207 | 887 | copy_to_reg (get_arg_pointer_save_area ())); |
53800dbe | 888 | } |
889 | } | |
53800dbe | 890 | |
a3c81e61 | 891 | if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ()) |
892 | emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label)); | |
893 | else if (targetm.have_nonlocal_goto_receiver ()) | |
894 | emit_insn (targetm.gen_nonlocal_goto_receiver ()); | |
53800dbe | 895 | else |
a3c81e61 | 896 | { /* Nothing */ } |
57f6bb94 | 897 | |
3072d30e | 898 | /* We must not allow the code we just generated to be reordered by |
899 | scheduling. Specifically, the update of the frame pointer must | |
62dcb5c8 | 900 | happen immediately, not later. */ |
3072d30e | 901 | emit_insn (gen_blockage ()); |
6b7f6858 | 902 | } |
53800dbe | 903 | |
53800dbe | 904 | /* __builtin_longjmp is passed a pointer to an array of five words (not |
905 | all will be used on all machines). It operates similarly to the C | |
906 | library function of the same name, but is more efficient. Much of | |
2c8a1497 | 907 | the code below is copied from the handling of non-local gotos. */ |
53800dbe | 908 | |
c626df3d | 909 | static void |
aecda0d6 | 910 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
53800dbe | 911 | { |
1e0c0b35 | 912 | rtx fp, lab, stack; |
913 | rtx_insn *insn, *last; | |
3754d046 | 914 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 915 | |
48e1416a | 916 | /* DRAP is needed for stack realign if longjmp is expanded to current |
27a7a23a | 917 | function */ |
918 | if (SUPPORTS_STACK_ALIGNMENT) | |
919 | crtl->need_drap = true; | |
920 | ||
f7c44134 | 921 | if (setjmp_alias_set == -1) |
922 | setjmp_alias_set = new_alias_set (); | |
923 | ||
85d654dd | 924 | buf_addr = convert_memory_address (Pmode, buf_addr); |
479e4d5e | 925 | |
53800dbe | 926 | buf_addr = force_reg (Pmode, buf_addr); |
927 | ||
82c7907c | 928 | /* We require that the user must pass a second argument of 1, because |
929 | that is what builtin_setjmp will return. */ | |
64db345d | 930 | gcc_assert (value == const1_rtx); |
53800dbe | 931 | |
4712c7d6 | 932 | last = get_last_insn (); |
a3c81e61 | 933 | if (targetm.have_builtin_longjmp ()) |
934 | emit_insn (targetm.gen_builtin_longjmp (buf_addr)); | |
53800dbe | 935 | else |
53800dbe | 936 | { |
937 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
29c05e22 | 938 | lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
53800dbe | 939 | GET_MODE_SIZE (Pmode))); |
940 | ||
29c05e22 | 941 | stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr, |
53800dbe | 942 | 2 * GET_MODE_SIZE (Pmode))); |
ab6ab77e | 943 | set_mem_alias_set (fp, setjmp_alias_set); |
944 | set_mem_alias_set (lab, setjmp_alias_set); | |
945 | set_mem_alias_set (stack, setjmp_alias_set); | |
53800dbe | 946 | |
947 | /* Pick up FP, label, and SP from the block and jump. This code is | |
948 | from expand_goto in stmt.c; see there for detailed comments. */ | |
a3c81e61 | 949 | if (targetm.have_nonlocal_goto ()) |
53800dbe | 950 | /* We have to pass a value to the nonlocal_goto pattern that will |
951 | get copied into the static_chain pointer, but it does not matter | |
952 | what that value is, because builtin_setjmp does not use it. */ | |
a3c81e61 | 953 | emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp)); |
53800dbe | 954 | else |
53800dbe | 955 | { |
956 | lab = copy_to_reg (lab); | |
957 | ||
18b42941 | 958 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
959 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
2a871ad1 | 960 | |
53800dbe | 961 | emit_move_insn (hard_frame_pointer_rtx, fp); |
e9c97615 | 962 | emit_stack_restore (SAVE_NONLOCAL, stack); |
53800dbe | 963 | |
18b42941 | 964 | emit_use (hard_frame_pointer_rtx); |
965 | emit_use (stack_pointer_rtx); | |
53800dbe | 966 | emit_indirect_jump (lab); |
967 | } | |
968 | } | |
615166bb | 969 | |
970 | /* Search backwards and mark the jump insn as a non-local goto. | |
971 | Note that this precludes the use of __builtin_longjmp to a | |
972 | __builtin_setjmp target in the same function. However, we've | |
973 | already cautioned the user that these functions are for | |
974 | internal exception handling use only. */ | |
449c0509 | 975 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
976 | { | |
64db345d | 977 | gcc_assert (insn != last); |
7d3f6cc7 | 978 | |
6d7dc5b9 | 979 | if (JUMP_P (insn)) |
449c0509 | 980 | { |
a1ddb869 | 981 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
449c0509 | 982 | break; |
983 | } | |
6d7dc5b9 | 984 | else if (CALL_P (insn)) |
9342ee68 | 985 | break; |
449c0509 | 986 | } |
53800dbe | 987 | } |
988 | ||
0e80b01d | 989 | static inline bool |
990 | more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter) | |
991 | { | |
992 | return (iter->i < iter->n); | |
993 | } | |
994 | ||
995 | /* This function validates the types of a function call argument list | |
996 | against a specified list of tree_codes. If the last specifier is a 0, | |
997 | that represents an ellipses, otherwise the last specifier must be a | |
998 | VOID_TYPE. */ | |
999 | ||
1000 | static bool | |
1001 | validate_arglist (const_tree callexpr, ...) | |
1002 | { | |
1003 | enum tree_code code; | |
1004 | bool res = 0; | |
1005 | va_list ap; | |
1006 | const_call_expr_arg_iterator iter; | |
1007 | const_tree arg; | |
1008 | ||
1009 | va_start (ap, callexpr); | |
1010 | init_const_call_expr_arg_iterator (callexpr, &iter); | |
1011 | ||
1012 | do | |
1013 | { | |
1014 | code = (enum tree_code) va_arg (ap, int); | |
1015 | switch (code) | |
1016 | { | |
1017 | case 0: | |
1018 | /* This signifies an ellipses, any further arguments are all ok. */ | |
1019 | res = true; | |
1020 | goto end; | |
1021 | case VOID_TYPE: | |
1022 | /* This signifies an endlink, if no arguments remain, return | |
1023 | true, otherwise return false. */ | |
1024 | res = !more_const_call_expr_args_p (&iter); | |
1025 | goto end; | |
1026 | default: | |
1027 | /* If no parameters remain or the parameter's code does not | |
1028 | match the specified code, return false. Otherwise continue | |
1029 | checking any remaining arguments. */ | |
1030 | arg = next_const_call_expr_arg (&iter); | |
1031 | if (!validate_arg (arg, code)) | |
1032 | goto end; | |
1033 | break; | |
1034 | } | |
1035 | } | |
1036 | while (1); | |
1037 | ||
1038 | /* We need gotos here since we can only have one VA_CLOSE in a | |
1039 | function. */ | |
1040 | end: ; | |
1041 | va_end (ap); | |
1042 | ||
1043 | return res; | |
1044 | } | |
1045 | ||
4ee9c684 | 1046 | /* Expand a call to __builtin_nonlocal_goto. We're passed the target label |
1047 | and the address of the save area. */ | |
1048 | ||
1049 | static rtx | |
c2f47e15 | 1050 | expand_builtin_nonlocal_goto (tree exp) |
4ee9c684 | 1051 | { |
1052 | tree t_label, t_save_area; | |
1e0c0b35 | 1053 | rtx r_label, r_save_area, r_fp, r_sp; |
1054 | rtx_insn *insn; | |
4ee9c684 | 1055 | |
c2f47e15 | 1056 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 1057 | return NULL_RTX; |
1058 | ||
c2f47e15 | 1059 | t_label = CALL_EXPR_ARG (exp, 0); |
1060 | t_save_area = CALL_EXPR_ARG (exp, 1); | |
4ee9c684 | 1061 | |
8ec3c5c2 | 1062 | r_label = expand_normal (t_label); |
3dce56cc | 1063 | r_label = convert_memory_address (Pmode, r_label); |
8ec3c5c2 | 1064 | r_save_area = expand_normal (t_save_area); |
3dce56cc | 1065 | r_save_area = convert_memory_address (Pmode, r_save_area); |
d1ff492e | 1066 | /* Copy the address of the save location to a register just in case it was |
1067 | based on the frame pointer. */ | |
51adbc8a | 1068 | r_save_area = copy_to_reg (r_save_area); |
4ee9c684 | 1069 | r_fp = gen_rtx_MEM (Pmode, r_save_area); |
1070 | r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL), | |
29c05e22 | 1071 | plus_constant (Pmode, r_save_area, |
1072 | GET_MODE_SIZE (Pmode))); | |
4ee9c684 | 1073 | |
18d50ae6 | 1074 | crtl->has_nonlocal_goto = 1; |
4ee9c684 | 1075 | |
4ee9c684 | 1076 | /* ??? We no longer need to pass the static chain value, afaik. */ |
a3c81e61 | 1077 | if (targetm.have_nonlocal_goto ()) |
1078 | emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp)); | |
4ee9c684 | 1079 | else |
4ee9c684 | 1080 | { |
1081 | r_label = copy_to_reg (r_label); | |
1082 | ||
18b42941 | 1083 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
1084 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
491e04ef | 1085 | |
d1ff492e | 1086 | /* Restore frame pointer for containing function. */ |
4ee9c684 | 1087 | emit_move_insn (hard_frame_pointer_rtx, r_fp); |
e9c97615 | 1088 | emit_stack_restore (SAVE_NONLOCAL, r_sp); |
491e04ef | 1089 | |
4ee9c684 | 1090 | /* USE of hard_frame_pointer_rtx added for consistency; |
1091 | not clear if really needed. */ | |
18b42941 | 1092 | emit_use (hard_frame_pointer_rtx); |
1093 | emit_use (stack_pointer_rtx); | |
ad0d0af8 | 1094 | |
1095 | /* If the architecture is using a GP register, we must | |
1096 | conservatively assume that the target function makes use of it. | |
1097 | The prologue of functions with nonlocal gotos must therefore | |
1098 | initialize the GP register to the appropriate value, and we | |
1099 | must then make sure that this value is live at the point | |
1100 | of the jump. (Note that this doesn't necessarily apply | |
1101 | to targets with a nonlocal_goto pattern; they are free | |
1102 | to implement it in their own way. Note also that this is | |
1103 | a no-op if the GP register is a global invariant.) */ | |
1104 | if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM | |
1105 | && fixed_regs[PIC_OFFSET_TABLE_REGNUM]) | |
18b42941 | 1106 | emit_use (pic_offset_table_rtx); |
ad0d0af8 | 1107 | |
4ee9c684 | 1108 | emit_indirect_jump (r_label); |
1109 | } | |
491e04ef | 1110 | |
4ee9c684 | 1111 | /* Search backwards to the jump insn and mark it as a |
1112 | non-local goto. */ | |
1113 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) | |
1114 | { | |
6d7dc5b9 | 1115 | if (JUMP_P (insn)) |
4ee9c684 | 1116 | { |
a1ddb869 | 1117 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
4ee9c684 | 1118 | break; |
1119 | } | |
6d7dc5b9 | 1120 | else if (CALL_P (insn)) |
4ee9c684 | 1121 | break; |
1122 | } | |
1123 | ||
1124 | return const0_rtx; | |
1125 | } | |
1126 | ||
843d08a9 | 1127 | /* __builtin_update_setjmp_buf is passed a pointer to an array of five words |
1128 | (not all will be used on all machines) that was passed to __builtin_setjmp. | |
97354ae4 | 1129 | It updates the stack pointer in that block to the current value. This is |
1130 | also called directly by the SJLJ exception handling code. */ | |
843d08a9 | 1131 | |
97354ae4 | 1132 | void |
843d08a9 | 1133 | expand_builtin_update_setjmp_buf (rtx buf_addr) |
1134 | { | |
3754d046 | 1135 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
d1ff492e | 1136 | rtx stack_save |
843d08a9 | 1137 | = gen_rtx_MEM (sa_mode, |
1138 | memory_address | |
1139 | (sa_mode, | |
29c05e22 | 1140 | plus_constant (Pmode, buf_addr, |
1141 | 2 * GET_MODE_SIZE (Pmode)))); | |
843d08a9 | 1142 | |
e9c97615 | 1143 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
843d08a9 | 1144 | } |
1145 | ||
5e3608d8 | 1146 | /* Expand a call to __builtin_prefetch. For a target that does not support |
1147 | data prefetch, evaluate the memory address argument in case it has side | |
1148 | effects. */ | |
1149 | ||
1150 | static void | |
c2f47e15 | 1151 | expand_builtin_prefetch (tree exp) |
5e3608d8 | 1152 | { |
1153 | tree arg0, arg1, arg2; | |
c2f47e15 | 1154 | int nargs; |
5e3608d8 | 1155 | rtx op0, op1, op2; |
1156 | ||
c2f47e15 | 1157 | if (!validate_arglist (exp, POINTER_TYPE, 0)) |
26a5cadb | 1158 | return; |
1159 | ||
c2f47e15 | 1160 | arg0 = CALL_EXPR_ARG (exp, 0); |
1161 | ||
26a5cadb | 1162 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
1163 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
1164 | locality). */ | |
c2f47e15 | 1165 | nargs = call_expr_nargs (exp); |
1166 | if (nargs > 1) | |
1167 | arg1 = CALL_EXPR_ARG (exp, 1); | |
26a5cadb | 1168 | else |
c2f47e15 | 1169 | arg1 = integer_zero_node; |
1170 | if (nargs > 2) | |
1171 | arg2 = CALL_EXPR_ARG (exp, 2); | |
1172 | else | |
2512209b | 1173 | arg2 = integer_three_node; |
5e3608d8 | 1174 | |
1175 | /* Argument 0 is an address. */ | |
1176 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
1177 | ||
1178 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
1179 | if (TREE_CODE (arg1) != INTEGER_CST) | |
1180 | { | |
07e3a3d2 | 1181 | error ("second argument to %<__builtin_prefetch%> must be a constant"); |
9342ee68 | 1182 | arg1 = integer_zero_node; |
5e3608d8 | 1183 | } |
8ec3c5c2 | 1184 | op1 = expand_normal (arg1); |
5e3608d8 | 1185 | /* Argument 1 must be either zero or one. */ |
1186 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
1187 | { | |
c3ceba8e | 1188 | warning (0, "invalid second argument to %<__builtin_prefetch%>;" |
07e3a3d2 | 1189 | " using zero"); |
5e3608d8 | 1190 | op1 = const0_rtx; |
1191 | } | |
1192 | ||
1193 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
1194 | if (TREE_CODE (arg2) != INTEGER_CST) | |
1195 | { | |
07e3a3d2 | 1196 | error ("third argument to %<__builtin_prefetch%> must be a constant"); |
5e3608d8 | 1197 | arg2 = integer_zero_node; |
1198 | } | |
8ec3c5c2 | 1199 | op2 = expand_normal (arg2); |
5e3608d8 | 1200 | /* Argument 2 must be 0, 1, 2, or 3. */ |
1201 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
1202 | { | |
c3ceba8e | 1203 | warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero"); |
5e3608d8 | 1204 | op2 = const0_rtx; |
1205 | } | |
1206 | ||
1d375a79 | 1207 | if (targetm.have_prefetch ()) |
5e3608d8 | 1208 | { |
8786db1e | 1209 | struct expand_operand ops[3]; |
1210 | ||
1211 | create_address_operand (&ops[0], op0); | |
1212 | create_integer_operand (&ops[1], INTVAL (op1)); | |
1213 | create_integer_operand (&ops[2], INTVAL (op2)); | |
1d375a79 | 1214 | if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops)) |
8786db1e | 1215 | return; |
5e3608d8 | 1216 | } |
0a534ba7 | 1217 | |
f0ce3b1f | 1218 | /* Don't do anything with direct references to volatile memory, but |
1219 | generate code to handle other side effects. */ | |
e16ceb8e | 1220 | if (!MEM_P (op0) && side_effects_p (op0)) |
f0ce3b1f | 1221 | emit_insn (op0); |
5e3608d8 | 1222 | } |
1223 | ||
f7c44134 | 1224 | /* Get a MEM rtx for expression EXP which is the address of an operand |
d8ae1baa | 1225 | to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is |
1226 | the maximum length of the block of memory that might be accessed or | |
1227 | NULL if unknown. */ | |
f7c44134 | 1228 | |
53800dbe | 1229 | static rtx |
d8ae1baa | 1230 | get_memory_rtx (tree exp, tree len) |
53800dbe | 1231 | { |
ad0a178f | 1232 | tree orig_exp = exp; |
1233 | rtx addr, mem; | |
ad0a178f | 1234 | |
1235 | /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived | |
1236 | from its expression, for expr->a.b only <variable>.a.b is recorded. */ | |
1237 | if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp)) | |
1238 | exp = TREE_OPERAND (exp, 0); | |
1239 | ||
1240 | addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL); | |
1241 | mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
2a631e19 | 1242 | |
f7c44134 | 1243 | /* Get an expression we can use to find the attributes to assign to MEM. |
5dd3f78f | 1244 | First remove any nops. */ |
72dd6141 | 1245 | while (CONVERT_EXPR_P (exp) |
f7c44134 | 1246 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
1247 | exp = TREE_OPERAND (exp, 0); | |
1248 | ||
5dd3f78f | 1249 | /* Build a MEM_REF representing the whole accessed area as a byte blob, |
1250 | (as builtin stringops may alias with anything). */ | |
1251 | exp = fold_build2 (MEM_REF, | |
1252 | build_array_type (char_type_node, | |
1253 | build_range_type (sizetype, | |
1254 | size_one_node, len)), | |
1255 | exp, build_int_cst (ptr_type_node, 0)); | |
1256 | ||
1257 | /* If the MEM_REF has no acceptable address, try to get the base object | |
1258 | from the original address we got, and build an all-aliasing | |
1259 | unknown-sized access to that one. */ | |
1260 | if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0))) | |
1261 | set_mem_attributes (mem, exp, 0); | |
1262 | else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR | |
1263 | && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0), | |
1264 | 0)))) | |
eec8e941 | 1265 | { |
5dd3f78f | 1266 | exp = build_fold_addr_expr (exp); |
1267 | exp = fold_build2 (MEM_REF, | |
1268 | build_array_type (char_type_node, | |
1269 | build_range_type (sizetype, | |
1270 | size_zero_node, | |
1271 | NULL)), | |
1272 | exp, build_int_cst (ptr_type_node, 0)); | |
a1a25d19 | 1273 | set_mem_attributes (mem, exp, 0); |
eec8e941 | 1274 | } |
5dd3f78f | 1275 | set_mem_alias_set (mem, 0); |
53800dbe | 1276 | return mem; |
1277 | } | |
1278 | \f | |
1279 | /* Built-in functions to perform an untyped call and return. */ | |
1280 | ||
3b9c3a16 | 1281 | #define apply_args_mode \ |
1282 | (this_target_builtins->x_apply_args_mode) | |
1283 | #define apply_result_mode \ | |
1284 | (this_target_builtins->x_apply_result_mode) | |
53800dbe | 1285 | |
53800dbe | 1286 | /* Return the size required for the block returned by __builtin_apply_args, |
1287 | and initialize apply_args_mode. */ | |
1288 | ||
1289 | static int | |
aecda0d6 | 1290 | apply_args_size (void) |
53800dbe | 1291 | { |
1292 | static int size = -1; | |
58e9ce8f | 1293 | int align; |
1294 | unsigned int regno; | |
3754d046 | 1295 | machine_mode mode; |
53800dbe | 1296 | |
1297 | /* The values computed by this function never change. */ | |
1298 | if (size < 0) | |
1299 | { | |
1300 | /* The first value is the incoming arg-pointer. */ | |
1301 | size = GET_MODE_SIZE (Pmode); | |
1302 | ||
1303 | /* The second value is the structure value address unless this is | |
1304 | passed as an "invisible" first argument. */ | |
6812c89e | 1305 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1306 | size += GET_MODE_SIZE (Pmode); |
1307 | ||
1308 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1309 | if (FUNCTION_ARG_REGNO_P (regno)) | |
1310 | { | |
4bac51c9 | 1311 | mode = targetm.calls.get_raw_arg_mode (regno); |
0862b7e9 | 1312 | |
64db345d | 1313 | gcc_assert (mode != VOIDmode); |
53800dbe | 1314 | |
1315 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1316 | if (size % align != 0) | |
1317 | size = CEIL (size, align) * align; | |
53800dbe | 1318 | size += GET_MODE_SIZE (mode); |
1319 | apply_args_mode[regno] = mode; | |
1320 | } | |
1321 | else | |
1322 | { | |
1323 | apply_args_mode[regno] = VOIDmode; | |
53800dbe | 1324 | } |
1325 | } | |
1326 | return size; | |
1327 | } | |
1328 | ||
1329 | /* Return the size required for the block returned by __builtin_apply, | |
1330 | and initialize apply_result_mode. */ | |
1331 | ||
1332 | static int | |
aecda0d6 | 1333 | apply_result_size (void) |
53800dbe | 1334 | { |
1335 | static int size = -1; | |
1336 | int align, regno; | |
3754d046 | 1337 | machine_mode mode; |
53800dbe | 1338 | |
1339 | /* The values computed by this function never change. */ | |
1340 | if (size < 0) | |
1341 | { | |
1342 | size = 0; | |
1343 | ||
1344 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
e1ce1485 | 1345 | if (targetm.calls.function_value_regno_p (regno)) |
53800dbe | 1346 | { |
4bac51c9 | 1347 | mode = targetm.calls.get_raw_result_mode (regno); |
0862b7e9 | 1348 | |
64db345d | 1349 | gcc_assert (mode != VOIDmode); |
53800dbe | 1350 | |
1351 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1352 | if (size % align != 0) | |
1353 | size = CEIL (size, align) * align; | |
1354 | size += GET_MODE_SIZE (mode); | |
1355 | apply_result_mode[regno] = mode; | |
1356 | } | |
1357 | else | |
1358 | apply_result_mode[regno] = VOIDmode; | |
1359 | ||
1360 | /* Allow targets that use untyped_call and untyped_return to override | |
1361 | the size so that machine-specific information can be stored here. */ | |
1362 | #ifdef APPLY_RESULT_SIZE | |
1363 | size = APPLY_RESULT_SIZE; | |
1364 | #endif | |
1365 | } | |
1366 | return size; | |
1367 | } | |
1368 | ||
53800dbe | 1369 | /* Create a vector describing the result block RESULT. If SAVEP is true, |
1370 | the result block is used to save the values; otherwise it is used to | |
1371 | restore the values. */ | |
1372 | ||
1373 | static rtx | |
aecda0d6 | 1374 | result_vector (int savep, rtx result) |
53800dbe | 1375 | { |
1376 | int regno, size, align, nelts; | |
3754d046 | 1377 | machine_mode mode; |
53800dbe | 1378 | rtx reg, mem; |
364c0c59 | 1379 | rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER); |
bf8e3599 | 1380 | |
53800dbe | 1381 | size = nelts = 0; |
1382 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1383 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1384 | { | |
1385 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1386 | if (size % align != 0) | |
1387 | size = CEIL (size, align) * align; | |
1388 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
e513d163 | 1389 | mem = adjust_address (result, mode, size); |
53800dbe | 1390 | savevec[nelts++] = (savep |
d1f9b275 | 1391 | ? gen_rtx_SET (mem, reg) |
1392 | : gen_rtx_SET (reg, mem)); | |
53800dbe | 1393 | size += GET_MODE_SIZE (mode); |
1394 | } | |
1395 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1396 | } | |
53800dbe | 1397 | |
1398 | /* Save the state required to perform an untyped call with the same | |
1399 | arguments as were passed to the current function. */ | |
1400 | ||
1401 | static rtx | |
aecda0d6 | 1402 | expand_builtin_apply_args_1 (void) |
53800dbe | 1403 | { |
1c7e61a7 | 1404 | rtx registers, tem; |
53800dbe | 1405 | int size, align, regno; |
3754d046 | 1406 | machine_mode mode; |
6812c89e | 1407 | rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1); |
53800dbe | 1408 | |
1409 | /* Create a block where the arg-pointer, structure value address, | |
1410 | and argument registers can be saved. */ | |
1411 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1412 | ||
1413 | /* Walk past the arg-pointer and structure value address. */ | |
1414 | size = GET_MODE_SIZE (Pmode); | |
6812c89e | 1415 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1416 | size += GET_MODE_SIZE (Pmode); |
1417 | ||
1418 | /* Save each register used in calling a function to the block. */ | |
1419 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1420 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1421 | { | |
53800dbe | 1422 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; |
1423 | if (size % align != 0) | |
1424 | size = CEIL (size, align) * align; | |
1425 | ||
1426 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1427 | ||
e513d163 | 1428 | emit_move_insn (adjust_address (registers, mode, size), tem); |
53800dbe | 1429 | size += GET_MODE_SIZE (mode); |
1430 | } | |
1431 | ||
1432 | /* Save the arg pointer to the block. */ | |
27a7a23a | 1433 | tem = copy_to_reg (crtl->args.internal_arg_pointer); |
1c7e61a7 | 1434 | /* We need the pointer as the caller actually passed them to us, not |
9d4b544c | 1435 | as we might have pretended they were passed. Make sure it's a valid |
1436 | operand, as emit_move_insn isn't expected to handle a PLUS. */ | |
3764c94e | 1437 | if (STACK_GROWS_DOWNWARD) |
1438 | tem | |
1439 | = force_operand (plus_constant (Pmode, tem, | |
1440 | crtl->args.pretend_args_size), | |
1441 | NULL_RTX); | |
1c7e61a7 | 1442 | emit_move_insn (adjust_address (registers, Pmode, 0), tem); |
0862b7e9 | 1443 | |
53800dbe | 1444 | size = GET_MODE_SIZE (Pmode); |
1445 | ||
1446 | /* Save the structure value address unless this is passed as an | |
1447 | "invisible" first argument. */ | |
45550790 | 1448 | if (struct_incoming_value) |
53800dbe | 1449 | { |
e513d163 | 1450 | emit_move_insn (adjust_address (registers, Pmode, size), |
45550790 | 1451 | copy_to_reg (struct_incoming_value)); |
53800dbe | 1452 | size += GET_MODE_SIZE (Pmode); |
1453 | } | |
1454 | ||
1455 | /* Return the address of the block. */ | |
1456 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1457 | } | |
1458 | ||
1459 | /* __builtin_apply_args returns block of memory allocated on | |
1460 | the stack into which is stored the arg pointer, structure | |
1461 | value address, static chain, and all the registers that might | |
1462 | possibly be used in performing a function call. The code is | |
1463 | moved to the start of the function so the incoming values are | |
1464 | saved. */ | |
27d0c333 | 1465 | |
53800dbe | 1466 | static rtx |
aecda0d6 | 1467 | expand_builtin_apply_args (void) |
53800dbe | 1468 | { |
1469 | /* Don't do __builtin_apply_args more than once in a function. | |
1470 | Save the result of the first call and reuse it. */ | |
1471 | if (apply_args_value != 0) | |
1472 | return apply_args_value; | |
1473 | { | |
1474 | /* When this function is called, it means that registers must be | |
1475 | saved on entry to this function. So we migrate the | |
1476 | call to the first insn of this function. */ | |
1477 | rtx temp; | |
53800dbe | 1478 | |
1479 | start_sequence (); | |
1480 | temp = expand_builtin_apply_args_1 (); | |
9ed997be | 1481 | rtx_insn *seq = get_insns (); |
53800dbe | 1482 | end_sequence (); |
1483 | ||
1484 | apply_args_value = temp; | |
1485 | ||
31d3e01c | 1486 | /* Put the insns after the NOTE that starts the function. |
1487 | If this is inside a start_sequence, make the outer-level insn | |
53800dbe | 1488 | chain current, so the code is placed at the start of the |
0ef1a651 | 1489 | function. If internal_arg_pointer is a non-virtual pseudo, |
1490 | it needs to be placed after the function that initializes | |
1491 | that pseudo. */ | |
53800dbe | 1492 | push_topmost_sequence (); |
0ef1a651 | 1493 | if (REG_P (crtl->args.internal_arg_pointer) |
1494 | && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER) | |
1495 | emit_insn_before (seq, parm_birth_insn); | |
1496 | else | |
1497 | emit_insn_before (seq, NEXT_INSN (entry_of_function ())); | |
53800dbe | 1498 | pop_topmost_sequence (); |
1499 | return temp; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | /* Perform an untyped call and save the state required to perform an | |
1504 | untyped return of whatever value was returned by the given function. */ | |
1505 | ||
1506 | static rtx | |
aecda0d6 | 1507 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
53800dbe | 1508 | { |
1509 | int size, align, regno; | |
3754d046 | 1510 | machine_mode mode; |
1e0c0b35 | 1511 | rtx incoming_args, result, reg, dest, src; |
1512 | rtx_call_insn *call_insn; | |
53800dbe | 1513 | rtx old_stack_level = 0; |
1514 | rtx call_fusage = 0; | |
6812c89e | 1515 | rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0); |
53800dbe | 1516 | |
85d654dd | 1517 | arguments = convert_memory_address (Pmode, arguments); |
726ec87c | 1518 | |
53800dbe | 1519 | /* Create a block where the return registers can be saved. */ |
1520 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1521 | ||
53800dbe | 1522 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1523 | incoming_args = gen_reg_rtx (Pmode); | |
726ec87c | 1524 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
3764c94e | 1525 | if (!STACK_GROWS_DOWNWARD) |
1526 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, | |
1527 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
53800dbe | 1528 | |
04a46d40 | 1529 | /* Push a new argument block and copy the arguments. Do not allow |
1530 | the (potential) memcpy call below to interfere with our stack | |
1531 | manipulations. */ | |
53800dbe | 1532 | do_pending_stack_adjust (); |
04a46d40 | 1533 | NO_DEFER_POP; |
53800dbe | 1534 | |
2358393e | 1535 | /* Save the stack with nonlocal if available. */ |
71512c05 | 1536 | if (targetm.have_save_stack_nonlocal ()) |
e9c97615 | 1537 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level); |
53800dbe | 1538 | else |
e9c97615 | 1539 | emit_stack_save (SAVE_BLOCK, &old_stack_level); |
53800dbe | 1540 | |
59647703 | 1541 | /* Allocate a block of memory onto the stack and copy the memory |
990495a7 | 1542 | arguments to the outgoing arguments address. We can pass TRUE |
1543 | as the 4th argument because we just saved the stack pointer | |
1544 | and will restore it right after the call. */ | |
5be42b39 | 1545 | allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true); |
27a7a23a | 1546 | |
1547 | /* Set DRAP flag to true, even though allocate_dynamic_stack_space | |
1548 | may have already set current_function_calls_alloca to true. | |
1549 | current_function_calls_alloca won't be set if argsize is zero, | |
1550 | so we have to guarantee need_drap is true here. */ | |
1551 | if (SUPPORTS_STACK_ALIGNMENT) | |
1552 | crtl->need_drap = true; | |
1553 | ||
59647703 | 1554 | dest = virtual_outgoing_args_rtx; |
3764c94e | 1555 | if (!STACK_GROWS_DOWNWARD) |
1556 | { | |
1557 | if (CONST_INT_P (argsize)) | |
1558 | dest = plus_constant (Pmode, dest, -INTVAL (argsize)); | |
1559 | else | |
1560 | dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize)); | |
1561 | } | |
2a631e19 | 1562 | dest = gen_rtx_MEM (BLKmode, dest); |
1563 | set_mem_align (dest, PARM_BOUNDARY); | |
1564 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1565 | set_mem_align (src, PARM_BOUNDARY); | |
0378dbdc | 1566 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
53800dbe | 1567 | |
1568 | /* Refer to the argument block. */ | |
1569 | apply_args_size (); | |
1570 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
2a631e19 | 1571 | set_mem_align (arguments, PARM_BOUNDARY); |
53800dbe | 1572 | |
1573 | /* Walk past the arg-pointer and structure value address. */ | |
1574 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1575 | if (struct_value) |
53800dbe | 1576 | size += GET_MODE_SIZE (Pmode); |
1577 | ||
1578 | /* Restore each of the registers previously saved. Make USE insns | |
1579 | for each of these registers for use in making the call. */ | |
1580 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1581 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1582 | { | |
1583 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1584 | if (size % align != 0) | |
1585 | size = CEIL (size, align) * align; | |
1586 | reg = gen_rtx_REG (mode, regno); | |
e513d163 | 1587 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
53800dbe | 1588 | use_reg (&call_fusage, reg); |
1589 | size += GET_MODE_SIZE (mode); | |
1590 | } | |
1591 | ||
1592 | /* Restore the structure value address unless this is passed as an | |
1593 | "invisible" first argument. */ | |
1594 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1595 | if (struct_value) |
53800dbe | 1596 | { |
1597 | rtx value = gen_reg_rtx (Pmode); | |
e513d163 | 1598 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
45550790 | 1599 | emit_move_insn (struct_value, value); |
8ad4c111 | 1600 | if (REG_P (struct_value)) |
45550790 | 1601 | use_reg (&call_fusage, struct_value); |
53800dbe | 1602 | size += GET_MODE_SIZE (Pmode); |
1603 | } | |
1604 | ||
1605 | /* All arguments and registers used for the call are set up by now! */ | |
82c7907c | 1606 | function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0); |
53800dbe | 1607 | |
1608 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1609 | and we don't want to load it into a register as an optimization, | |
1610 | because prepare_call_address already did it if it should be done. */ | |
1611 | if (GET_CODE (function) != SYMBOL_REF) | |
1612 | function = memory_address (FUNCTION_MODE, function); | |
1613 | ||
1614 | /* Generate the actual call instruction and save the return value. */ | |
1d99ab0a | 1615 | if (targetm.have_untyped_call ()) |
1616 | { | |
1617 | rtx mem = gen_rtx_MEM (FUNCTION_MODE, function); | |
1618 | emit_call_insn (targetm.gen_untyped_call (mem, result, | |
1619 | result_vector (1, result))); | |
1620 | } | |
7f265a08 | 1621 | else if (targetm.have_call_value ()) |
53800dbe | 1622 | { |
1623 | rtx valreg = 0; | |
1624 | ||
1625 | /* Locate the unique return register. It is not possible to | |
1626 | express a call that sets more than one return register using | |
1627 | call_value; use untyped_call for that. In fact, untyped_call | |
1628 | only needs to save the return registers in the given block. */ | |
1629 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1630 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1631 | { | |
7f265a08 | 1632 | gcc_assert (!valreg); /* have_untyped_call required. */ |
7d3f6cc7 | 1633 | |
53800dbe | 1634 | valreg = gen_rtx_REG (mode, regno); |
1635 | } | |
1636 | ||
7f265a08 | 1637 | emit_insn (targetm.gen_call_value (valreg, |
1638 | gen_rtx_MEM (FUNCTION_MODE, function), | |
1639 | const0_rtx, NULL_RTX, const0_rtx)); | |
53800dbe | 1640 | |
e513d163 | 1641 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
53800dbe | 1642 | } |
1643 | else | |
64db345d | 1644 | gcc_unreachable (); |
53800dbe | 1645 | |
d5f9786f | 1646 | /* Find the CALL insn we just emitted, and attach the register usage |
1647 | information. */ | |
1648 | call_insn = last_call_insn (); | |
1649 | add_function_usage_to (call_insn, call_fusage); | |
53800dbe | 1650 | |
1651 | /* Restore the stack. */ | |
71512c05 | 1652 | if (targetm.have_save_stack_nonlocal ()) |
e9c97615 | 1653 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level); |
53800dbe | 1654 | else |
e9c97615 | 1655 | emit_stack_restore (SAVE_BLOCK, old_stack_level); |
9af5ce0c | 1656 | fixup_args_size_notes (call_insn, get_last_insn (), 0); |
53800dbe | 1657 | |
04a46d40 | 1658 | OK_DEFER_POP; |
1659 | ||
53800dbe | 1660 | /* Return the address of the result block. */ |
85d654dd | 1661 | result = copy_addr_to_reg (XEXP (result, 0)); |
1662 | return convert_memory_address (ptr_mode, result); | |
53800dbe | 1663 | } |
1664 | ||
1665 | /* Perform an untyped return. */ | |
1666 | ||
1667 | static void | |
aecda0d6 | 1668 | expand_builtin_return (rtx result) |
53800dbe | 1669 | { |
1670 | int size, align, regno; | |
3754d046 | 1671 | machine_mode mode; |
53800dbe | 1672 | rtx reg; |
57c26b3a | 1673 | rtx_insn *call_fusage = 0; |
53800dbe | 1674 | |
85d654dd | 1675 | result = convert_memory_address (Pmode, result); |
726ec87c | 1676 | |
53800dbe | 1677 | apply_result_size (); |
1678 | result = gen_rtx_MEM (BLKmode, result); | |
1679 | ||
1d99ab0a | 1680 | if (targetm.have_untyped_return ()) |
53800dbe | 1681 | { |
1d99ab0a | 1682 | rtx vector = result_vector (0, result); |
1683 | emit_jump_insn (targetm.gen_untyped_return (result, vector)); | |
53800dbe | 1684 | emit_barrier (); |
1685 | return; | |
1686 | } | |
53800dbe | 1687 | |
1688 | /* Restore the return value and note that each value is used. */ | |
1689 | size = 0; | |
1690 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1691 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1692 | { | |
1693 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1694 | if (size % align != 0) | |
1695 | size = CEIL (size, align) * align; | |
1696 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
e513d163 | 1697 | emit_move_insn (reg, adjust_address (result, mode, size)); |
53800dbe | 1698 | |
1699 | push_to_sequence (call_fusage); | |
18b42941 | 1700 | emit_use (reg); |
53800dbe | 1701 | call_fusage = get_insns (); |
1702 | end_sequence (); | |
1703 | size += GET_MODE_SIZE (mode); | |
1704 | } | |
1705 | ||
1706 | /* Put the USE insns before the return. */ | |
31d3e01c | 1707 | emit_insn (call_fusage); |
53800dbe | 1708 | |
1709 | /* Return whatever values was restored by jumping directly to the end | |
1710 | of the function. */ | |
62380d2d | 1711 | expand_naked_return (); |
53800dbe | 1712 | } |
1713 | ||
539a3a92 | 1714 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
27d0c333 | 1715 | |
539a3a92 | 1716 | static enum type_class |
aecda0d6 | 1717 | type_to_class (tree type) |
539a3a92 | 1718 | { |
1719 | switch (TREE_CODE (type)) | |
1720 | { | |
1721 | case VOID_TYPE: return void_type_class; | |
1722 | case INTEGER_TYPE: return integer_type_class; | |
539a3a92 | 1723 | case ENUMERAL_TYPE: return enumeral_type_class; |
1724 | case BOOLEAN_TYPE: return boolean_type_class; | |
1725 | case POINTER_TYPE: return pointer_type_class; | |
1726 | case REFERENCE_TYPE: return reference_type_class; | |
1727 | case OFFSET_TYPE: return offset_type_class; | |
1728 | case REAL_TYPE: return real_type_class; | |
1729 | case COMPLEX_TYPE: return complex_type_class; | |
1730 | case FUNCTION_TYPE: return function_type_class; | |
1731 | case METHOD_TYPE: return method_type_class; | |
1732 | case RECORD_TYPE: return record_type_class; | |
1733 | case UNION_TYPE: | |
1734 | case QUAL_UNION_TYPE: return union_type_class; | |
1735 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1736 | ? string_type_class : array_type_class); | |
539a3a92 | 1737 | case LANG_TYPE: return lang_type_class; |
1738 | default: return no_type_class; | |
1739 | } | |
1740 | } | |
bf8e3599 | 1741 | |
c2f47e15 | 1742 | /* Expand a call EXP to __builtin_classify_type. */ |
27d0c333 | 1743 | |
53800dbe | 1744 | static rtx |
c2f47e15 | 1745 | expand_builtin_classify_type (tree exp) |
53800dbe | 1746 | { |
c2f47e15 | 1747 | if (call_expr_nargs (exp)) |
1748 | return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))); | |
53800dbe | 1749 | return GEN_INT (no_type_class); |
1750 | } | |
1751 | ||
07976da7 | 1752 | /* This helper macro, meant to be used in mathfn_built_in below, |
1753 | determines which among a set of three builtin math functions is | |
1754 | appropriate for a given type mode. The `F' and `L' cases are | |
1755 | automatically generated from the `double' case. */ | |
e3240774 | 1756 | #define CASE_MATHFN(MATHFN) \ |
1757 | CASE_CFN_##MATHFN: \ | |
1758 | fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \ | |
1759 | fcodel = BUILT_IN_##MATHFN##L ; break; | |
cd2656b0 | 1760 | /* Similar to above, but appends _R after any F/L suffix. */ |
e3240774 | 1761 | #define CASE_MATHFN_REENT(MATHFN) \ |
1762 | case CFN_BUILT_IN_##MATHFN##_R: \ | |
1763 | case CFN_BUILT_IN_##MATHFN##F_R: \ | |
1764 | case CFN_BUILT_IN_##MATHFN##L_R: \ | |
1765 | fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \ | |
1766 | fcodel = BUILT_IN_##MATHFN##L_R ; break; | |
07976da7 | 1767 | |
6c21be92 | 1768 | /* Return a function equivalent to FN but operating on floating-point |
1769 | values of type TYPE, or END_BUILTINS if no such function exists. | |
e3240774 | 1770 | This is purely an operation on function codes; it does not guarantee |
1771 | that the target actually has an implementation of the function. */ | |
c319d56a | 1772 | |
6c21be92 | 1773 | static built_in_function |
e3240774 | 1774 | mathfn_built_in_2 (tree type, combined_fn fn) |
0a68165a | 1775 | { |
6c21be92 | 1776 | built_in_function fcode, fcodef, fcodel; |
07976da7 | 1777 | |
1778 | switch (fn) | |
1779 | { | |
e3240774 | 1780 | CASE_MATHFN (ACOS) |
1781 | CASE_MATHFN (ACOSH) | |
1782 | CASE_MATHFN (ASIN) | |
1783 | CASE_MATHFN (ASINH) | |
1784 | CASE_MATHFN (ATAN) | |
1785 | CASE_MATHFN (ATAN2) | |
1786 | CASE_MATHFN (ATANH) | |
1787 | CASE_MATHFN (CBRT) | |
1788 | CASE_MATHFN (CEIL) | |
1789 | CASE_MATHFN (CEXPI) | |
1790 | CASE_MATHFN (COPYSIGN) | |
1791 | CASE_MATHFN (COS) | |
1792 | CASE_MATHFN (COSH) | |
1793 | CASE_MATHFN (DREM) | |
1794 | CASE_MATHFN (ERF) | |
1795 | CASE_MATHFN (ERFC) | |
1796 | CASE_MATHFN (EXP) | |
1797 | CASE_MATHFN (EXP10) | |
1798 | CASE_MATHFN (EXP2) | |
1799 | CASE_MATHFN (EXPM1) | |
1800 | CASE_MATHFN (FABS) | |
1801 | CASE_MATHFN (FDIM) | |
1802 | CASE_MATHFN (FLOOR) | |
1803 | CASE_MATHFN (FMA) | |
1804 | CASE_MATHFN (FMAX) | |
1805 | CASE_MATHFN (FMIN) | |
1806 | CASE_MATHFN (FMOD) | |
1807 | CASE_MATHFN (FREXP) | |
1808 | CASE_MATHFN (GAMMA) | |
1809 | CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ | |
1810 | CASE_MATHFN (HUGE_VAL) | |
1811 | CASE_MATHFN (HYPOT) | |
1812 | CASE_MATHFN (ILOGB) | |
1813 | CASE_MATHFN (ICEIL) | |
1814 | CASE_MATHFN (IFLOOR) | |
1815 | CASE_MATHFN (INF) | |
1816 | CASE_MATHFN (IRINT) | |
1817 | CASE_MATHFN (IROUND) | |
1818 | CASE_MATHFN (ISINF) | |
1819 | CASE_MATHFN (J0) | |
1820 | CASE_MATHFN (J1) | |
1821 | CASE_MATHFN (JN) | |
1822 | CASE_MATHFN (LCEIL) | |
1823 | CASE_MATHFN (LDEXP) | |
1824 | CASE_MATHFN (LFLOOR) | |
1825 | CASE_MATHFN (LGAMMA) | |
1826 | CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ | |
1827 | CASE_MATHFN (LLCEIL) | |
1828 | CASE_MATHFN (LLFLOOR) | |
1829 | CASE_MATHFN (LLRINT) | |
1830 | CASE_MATHFN (LLROUND) | |
1831 | CASE_MATHFN (LOG) | |
1832 | CASE_MATHFN (LOG10) | |
1833 | CASE_MATHFN (LOG1P) | |
1834 | CASE_MATHFN (LOG2) | |
1835 | CASE_MATHFN (LOGB) | |
1836 | CASE_MATHFN (LRINT) | |
1837 | CASE_MATHFN (LROUND) | |
1838 | CASE_MATHFN (MODF) | |
1839 | CASE_MATHFN (NAN) | |
1840 | CASE_MATHFN (NANS) | |
1841 | CASE_MATHFN (NEARBYINT) | |
1842 | CASE_MATHFN (NEXTAFTER) | |
1843 | CASE_MATHFN (NEXTTOWARD) | |
1844 | CASE_MATHFN (POW) | |
1845 | CASE_MATHFN (POWI) | |
1846 | CASE_MATHFN (POW10) | |
1847 | CASE_MATHFN (REMAINDER) | |
1848 | CASE_MATHFN (REMQUO) | |
1849 | CASE_MATHFN (RINT) | |
1850 | CASE_MATHFN (ROUND) | |
1851 | CASE_MATHFN (SCALB) | |
1852 | CASE_MATHFN (SCALBLN) | |
1853 | CASE_MATHFN (SCALBN) | |
1854 | CASE_MATHFN (SIGNBIT) | |
1855 | CASE_MATHFN (SIGNIFICAND) | |
1856 | CASE_MATHFN (SIN) | |
1857 | CASE_MATHFN (SINCOS) | |
1858 | CASE_MATHFN (SINH) | |
1859 | CASE_MATHFN (SQRT) | |
1860 | CASE_MATHFN (TAN) | |
1861 | CASE_MATHFN (TANH) | |
1862 | CASE_MATHFN (TGAMMA) | |
1863 | CASE_MATHFN (TRUNC) | |
1864 | CASE_MATHFN (Y0) | |
1865 | CASE_MATHFN (Y1) | |
1866 | CASE_MATHFN (YN) | |
07976da7 | 1867 | |
e3240774 | 1868 | default: |
1869 | return END_BUILTINS; | |
1870 | } | |
07976da7 | 1871 | |
96b9f485 | 1872 | if (TYPE_MAIN_VARIANT (type) == double_type_node) |
6c21be92 | 1873 | return fcode; |
96b9f485 | 1874 | else if (TYPE_MAIN_VARIANT (type) == float_type_node) |
6c21be92 | 1875 | return fcodef; |
96b9f485 | 1876 | else if (TYPE_MAIN_VARIANT (type) == long_double_type_node) |
6c21be92 | 1877 | return fcodel; |
07976da7 | 1878 | else |
6c21be92 | 1879 | return END_BUILTINS; |
1880 | } | |
1881 | ||
1882 | /* Return mathematic function equivalent to FN but operating directly on TYPE, | |
1883 | if available. If IMPLICIT_P is true use the implicit builtin declaration, | |
1884 | otherwise use the explicit declaration. If we can't do the conversion, | |
1885 | return null. */ | |
1886 | ||
1887 | static tree | |
e3240774 | 1888 | mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p) |
6c21be92 | 1889 | { |
1890 | built_in_function fcode2 = mathfn_built_in_2 (type, fn); | |
1891 | if (fcode2 == END_BUILTINS) | |
c2f47e15 | 1892 | return NULL_TREE; |
b9a16870 | 1893 | |
1894 | if (implicit_p && !builtin_decl_implicit_p (fcode2)) | |
1895 | return NULL_TREE; | |
1896 | ||
1897 | return builtin_decl_explicit (fcode2); | |
0a68165a | 1898 | } |
1899 | ||
e3240774 | 1900 | /* Like mathfn_built_in_1, but always use the implicit array. */ |
c319d56a | 1901 | |
1902 | tree | |
e3240774 | 1903 | mathfn_built_in (tree type, combined_fn fn) |
c319d56a | 1904 | { |
1905 | return mathfn_built_in_1 (type, fn, /*implicit=*/ 1); | |
1906 | } | |
1907 | ||
e3240774 | 1908 | /* Like mathfn_built_in_1, but take a built_in_function and |
1909 | always use the implicit array. */ | |
1910 | ||
1911 | tree | |
1912 | mathfn_built_in (tree type, enum built_in_function fn) | |
1913 | { | |
1914 | return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1); | |
1915 | } | |
1916 | ||
1f24b8e9 | 1917 | /* If BUILT_IN_NORMAL function FNDECL has an associated internal function, |
1918 | return its code, otherwise return IFN_LAST. Note that this function | |
1919 | only tests whether the function is defined in internals.def, not whether | |
1920 | it is actually available on the target. */ | |
1921 | ||
1922 | internal_fn | |
1923 | associated_internal_fn (tree fndecl) | |
1924 | { | |
1925 | gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL); | |
1926 | tree return_type = TREE_TYPE (TREE_TYPE (fndecl)); | |
1927 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1928 | { | |
1929 | #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \ | |
1930 | CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
c9452b7c | 1931 | #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \ |
1932 | CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
1f24b8e9 | 1933 | #include "internal-fn.def" |
1934 | ||
1935 | CASE_FLT_FN (BUILT_IN_POW10): | |
1936 | return IFN_EXP10; | |
1937 | ||
1938 | CASE_FLT_FN (BUILT_IN_DREM): | |
1939 | return IFN_REMAINDER; | |
1940 | ||
1941 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
1942 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
1943 | if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2) | |
1944 | return IFN_LDEXP; | |
1945 | return IFN_LAST; | |
1946 | ||
1947 | default: | |
1948 | return IFN_LAST; | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced | |
1953 | on the current target by a call to an internal function, return the | |
1954 | code of that internal function, otherwise return IFN_LAST. The caller | |
1955 | is responsible for ensuring that any side-effects of the built-in | |
1956 | call are dealt with correctly. E.g. if CALL sets errno, the caller | |
1957 | must decide that the errno result isn't needed or make it available | |
1958 | in some other way. */ | |
1959 | ||
1960 | internal_fn | |
1961 | replacement_internal_fn (gcall *call) | |
1962 | { | |
1963 | if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)) | |
1964 | { | |
1965 | internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call)); | |
1966 | if (ifn != IFN_LAST) | |
1967 | { | |
1968 | tree_pair types = direct_internal_fn_types (ifn, call); | |
acdfe9e0 | 1969 | optimization_type opt_type = bb_optimization_type (gimple_bb (call)); |
1970 | if (direct_internal_fn_supported_p (ifn, types, opt_type)) | |
1f24b8e9 | 1971 | return ifn; |
1972 | } | |
1973 | } | |
1974 | return IFN_LAST; | |
1975 | } | |
1976 | ||
7e0713b1 | 1977 | /* Expand a call to the builtin trinary math functions (fma). |
1978 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
1979 | function in-line. EXP is the expression that is a call to the builtin | |
1980 | function; if convenient, the result should be placed in TARGET. | |
1981 | SUBTARGET may be used as the target for computing one of EXP's | |
1982 | operands. */ | |
1983 | ||
1984 | static rtx | |
1985 | expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget) | |
1986 | { | |
1987 | optab builtin_optab; | |
1e0c0b35 | 1988 | rtx op0, op1, op2, result; |
1989 | rtx_insn *insns; | |
7e0713b1 | 1990 | tree fndecl = get_callee_fndecl (exp); |
1991 | tree arg0, arg1, arg2; | |
3754d046 | 1992 | machine_mode mode; |
7e0713b1 | 1993 | |
1994 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
1995 | return NULL_RTX; | |
1996 | ||
1997 | arg0 = CALL_EXPR_ARG (exp, 0); | |
1998 | arg1 = CALL_EXPR_ARG (exp, 1); | |
1999 | arg2 = CALL_EXPR_ARG (exp, 2); | |
2000 | ||
2001 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2002 | { | |
2003 | CASE_FLT_FN (BUILT_IN_FMA): | |
2004 | builtin_optab = fma_optab; break; | |
2005 | default: | |
2006 | gcc_unreachable (); | |
2007 | } | |
2008 | ||
2009 | /* Make a suitable register to place result in. */ | |
2010 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2011 | ||
2012 | /* Before working hard, check whether the instruction is available. */ | |
2013 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) | |
2014 | return NULL_RTX; | |
2015 | ||
de2e453e | 2016 | result = gen_reg_rtx (mode); |
7e0713b1 | 2017 | |
2018 | /* Always stabilize the argument list. */ | |
2019 | CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0); | |
2020 | CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1); | |
2021 | CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2); | |
2022 | ||
2023 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); | |
2024 | op1 = expand_normal (arg1); | |
2025 | op2 = expand_normal (arg2); | |
2026 | ||
2027 | start_sequence (); | |
2028 | ||
de2e453e | 2029 | /* Compute into RESULT. |
2030 | Set RESULT to wherever the result comes back. */ | |
2031 | result = expand_ternary_op (mode, builtin_optab, op0, op1, op2, | |
2032 | result, 0); | |
7e0713b1 | 2033 | |
2034 | /* If we were unable to expand via the builtin, stop the sequence | |
2035 | (without outputting the insns) and call to the library function | |
2036 | with the stabilized argument list. */ | |
de2e453e | 2037 | if (result == 0) |
7e0713b1 | 2038 | { |
2039 | end_sequence (); | |
2040 | return expand_call (exp, target, target == const0_rtx); | |
2041 | } | |
2042 | ||
2043 | /* Output the entire sequence. */ | |
2044 | insns = get_insns (); | |
2045 | end_sequence (); | |
2046 | emit_insn (insns); | |
2047 | ||
de2e453e | 2048 | return result; |
7e0713b1 | 2049 | } |
2050 | ||
6b43bae4 | 2051 | /* Expand a call to the builtin sin and cos math functions. |
c2f47e15 | 2052 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
6b43bae4 | 2053 | function in-line. EXP is the expression that is a call to the builtin |
2054 | function; if convenient, the result should be placed in TARGET. | |
2055 | SUBTARGET may be used as the target for computing one of EXP's | |
2056 | operands. */ | |
2057 | ||
2058 | static rtx | |
2059 | expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget) | |
2060 | { | |
2061 | optab builtin_optab; | |
1e0c0b35 | 2062 | rtx op0; |
2063 | rtx_insn *insns; | |
6b43bae4 | 2064 | tree fndecl = get_callee_fndecl (exp); |
3754d046 | 2065 | machine_mode mode; |
abfea505 | 2066 | tree arg; |
6b43bae4 | 2067 | |
c2f47e15 | 2068 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2069 | return NULL_RTX; | |
6b43bae4 | 2070 | |
c2f47e15 | 2071 | arg = CALL_EXPR_ARG (exp, 0); |
6b43bae4 | 2072 | |
2073 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2074 | { | |
4f35b1fc | 2075 | CASE_FLT_FN (BUILT_IN_SIN): |
2076 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 2077 | builtin_optab = sincos_optab; break; |
2078 | default: | |
64db345d | 2079 | gcc_unreachable (); |
6b43bae4 | 2080 | } |
2081 | ||
2082 | /* Make a suitable register to place result in. */ | |
2083 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2084 | ||
6b43bae4 | 2085 | /* Check if sincos insn is available, otherwise fallback |
0bed3869 | 2086 | to sin or cos insn. */ |
d6bf3b14 | 2087 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) |
6b43bae4 | 2088 | switch (DECL_FUNCTION_CODE (fndecl)) |
2089 | { | |
4f35b1fc | 2090 | CASE_FLT_FN (BUILT_IN_SIN): |
6b43bae4 | 2091 | builtin_optab = sin_optab; break; |
4f35b1fc | 2092 | CASE_FLT_FN (BUILT_IN_COS): |
6b43bae4 | 2093 | builtin_optab = cos_optab; break; |
2094 | default: | |
64db345d | 2095 | gcc_unreachable (); |
6b43bae4 | 2096 | } |
6b43bae4 | 2097 | |
2098 | /* Before working hard, check whether the instruction is available. */ | |
d6bf3b14 | 2099 | if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing) |
6b43bae4 | 2100 | { |
de2e453e | 2101 | rtx result = gen_reg_rtx (mode); |
6b43bae4 | 2102 | |
2103 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2104 | need to expand the argument again. This way, we will not perform | |
2105 | side-effects more the once. */ | |
abfea505 | 2106 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
6b43bae4 | 2107 | |
1db6d067 | 2108 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
6b43bae4 | 2109 | |
6b43bae4 | 2110 | start_sequence (); |
2111 | ||
de2e453e | 2112 | /* Compute into RESULT. |
2113 | Set RESULT to wherever the result comes back. */ | |
6b43bae4 | 2114 | if (builtin_optab == sincos_optab) |
2115 | { | |
de2e453e | 2116 | int ok; |
7d3f6cc7 | 2117 | |
6b43bae4 | 2118 | switch (DECL_FUNCTION_CODE (fndecl)) |
2119 | { | |
4f35b1fc | 2120 | CASE_FLT_FN (BUILT_IN_SIN): |
de2e453e | 2121 | ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0); |
6b43bae4 | 2122 | break; |
4f35b1fc | 2123 | CASE_FLT_FN (BUILT_IN_COS): |
de2e453e | 2124 | ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0); |
6b43bae4 | 2125 | break; |
2126 | default: | |
64db345d | 2127 | gcc_unreachable (); |
6b43bae4 | 2128 | } |
de2e453e | 2129 | gcc_assert (ok); |
6b43bae4 | 2130 | } |
2131 | else | |
de2e453e | 2132 | result = expand_unop (mode, builtin_optab, op0, result, 0); |
6b43bae4 | 2133 | |
de2e453e | 2134 | if (result != 0) |
6b43bae4 | 2135 | { |
6b43bae4 | 2136 | /* Output the entire sequence. */ |
2137 | insns = get_insns (); | |
2138 | end_sequence (); | |
2139 | emit_insn (insns); | |
de2e453e | 2140 | return result; |
6b43bae4 | 2141 | } |
2142 | ||
2143 | /* If we were unable to expand via the builtin, stop the sequence | |
2144 | (without outputting the insns) and call to the library function | |
2145 | with the stabilized argument list. */ | |
2146 | end_sequence (); | |
2147 | } | |
2148 | ||
de2e453e | 2149 | return expand_call (exp, target, target == const0_rtx); |
6b43bae4 | 2150 | } |
2151 | ||
a65c4d64 | 2152 | /* Given an interclass math builtin decl FNDECL and it's argument ARG |
2153 | return an RTL instruction code that implements the functionality. | |
2154 | If that isn't possible or available return CODE_FOR_nothing. */ | |
a67a90e5 | 2155 | |
a65c4d64 | 2156 | static enum insn_code |
2157 | interclass_mathfn_icode (tree arg, tree fndecl) | |
a67a90e5 | 2158 | { |
a65c4d64 | 2159 | bool errno_set = false; |
6cdd383a | 2160 | optab builtin_optab = unknown_optab; |
3754d046 | 2161 | machine_mode mode; |
a67a90e5 | 2162 | |
2163 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2164 | { | |
2165 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
2166 | errno_set = true; builtin_optab = ilogb_optab; break; | |
69b779ea | 2167 | CASE_FLT_FN (BUILT_IN_ISINF): |
2168 | builtin_optab = isinf_optab; break; | |
8a1a9cb7 | 2169 | case BUILT_IN_ISNORMAL: |
cde061c1 | 2170 | case BUILT_IN_ISFINITE: |
2171 | CASE_FLT_FN (BUILT_IN_FINITE): | |
a65c4d64 | 2172 | case BUILT_IN_FINITED32: |
2173 | case BUILT_IN_FINITED64: | |
2174 | case BUILT_IN_FINITED128: | |
2175 | case BUILT_IN_ISINFD32: | |
2176 | case BUILT_IN_ISINFD64: | |
2177 | case BUILT_IN_ISINFD128: | |
cde061c1 | 2178 | /* These builtins have no optabs (yet). */ |
2179 | break; | |
a67a90e5 | 2180 | default: |
2181 | gcc_unreachable (); | |
2182 | } | |
2183 | ||
2184 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2185 | if (flag_errno_math && errno_set) | |
a65c4d64 | 2186 | return CODE_FOR_nothing; |
a67a90e5 | 2187 | |
2188 | /* Optab mode depends on the mode of the input argument. */ | |
2189 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2190 | ||
cde061c1 | 2191 | if (builtin_optab) |
d6bf3b14 | 2192 | return optab_handler (builtin_optab, mode); |
a65c4d64 | 2193 | return CODE_FOR_nothing; |
2194 | } | |
2195 | ||
2196 | /* Expand a call to one of the builtin math functions that operate on | |
2197 | floating point argument and output an integer result (ilogb, isinf, | |
2198 | isnan, etc). | |
2199 | Return 0 if a normal call should be emitted rather than expanding the | |
2200 | function in-line. EXP is the expression that is a call to the builtin | |
f97eea22 | 2201 | function; if convenient, the result should be placed in TARGET. */ |
a65c4d64 | 2202 | |
2203 | static rtx | |
f97eea22 | 2204 | expand_builtin_interclass_mathfn (tree exp, rtx target) |
a65c4d64 | 2205 | { |
2206 | enum insn_code icode = CODE_FOR_nothing; | |
2207 | rtx op0; | |
2208 | tree fndecl = get_callee_fndecl (exp); | |
3754d046 | 2209 | machine_mode mode; |
a65c4d64 | 2210 | tree arg; |
2211 | ||
2212 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) | |
2213 | return NULL_RTX; | |
2214 | ||
2215 | arg = CALL_EXPR_ARG (exp, 0); | |
2216 | icode = interclass_mathfn_icode (arg, fndecl); | |
2217 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2218 | ||
a67a90e5 | 2219 | if (icode != CODE_FOR_nothing) |
2220 | { | |
8786db1e | 2221 | struct expand_operand ops[1]; |
1e0c0b35 | 2222 | rtx_insn *last = get_last_insn (); |
4e2a2fb4 | 2223 | tree orig_arg = arg; |
a67a90e5 | 2224 | |
2225 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2226 | need to expand the argument again. This way, we will not perform | |
2227 | side-effects more the once. */ | |
abfea505 | 2228 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
a67a90e5 | 2229 | |
f97eea22 | 2230 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
a67a90e5 | 2231 | |
2232 | if (mode != GET_MODE (op0)) | |
2233 | op0 = convert_to_mode (mode, op0, 0); | |
2234 | ||
8786db1e | 2235 | create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp))); |
2236 | if (maybe_legitimize_operands (icode, 0, 1, ops) | |
2237 | && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN)) | |
2238 | return ops[0].value; | |
2239 | ||
4e2a2fb4 | 2240 | delete_insns_since (last); |
2241 | CALL_EXPR_ARG (exp, 0) = orig_arg; | |
a67a90e5 | 2242 | } |
2243 | ||
a65c4d64 | 2244 | return NULL_RTX; |
a67a90e5 | 2245 | } |
2246 | ||
c3147c1a | 2247 | /* Expand a call to the builtin sincos math function. |
c2f47e15 | 2248 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
c3147c1a | 2249 | function in-line. EXP is the expression that is a call to the builtin |
2250 | function. */ | |
2251 | ||
2252 | static rtx | |
2253 | expand_builtin_sincos (tree exp) | |
2254 | { | |
2255 | rtx op0, op1, op2, target1, target2; | |
3754d046 | 2256 | machine_mode mode; |
c3147c1a | 2257 | tree arg, sinp, cosp; |
2258 | int result; | |
389dd41b | 2259 | location_t loc = EXPR_LOCATION (exp); |
be5575b2 | 2260 | tree alias_type, alias_off; |
c3147c1a | 2261 | |
c2f47e15 | 2262 | if (!validate_arglist (exp, REAL_TYPE, |
2263 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2264 | return NULL_RTX; | |
c3147c1a | 2265 | |
c2f47e15 | 2266 | arg = CALL_EXPR_ARG (exp, 0); |
2267 | sinp = CALL_EXPR_ARG (exp, 1); | |
2268 | cosp = CALL_EXPR_ARG (exp, 2); | |
c3147c1a | 2269 | |
2270 | /* Make a suitable register to place result in. */ | |
2271 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2272 | ||
2273 | /* Check if sincos insn is available, otherwise emit the call. */ | |
d6bf3b14 | 2274 | if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing) |
c3147c1a | 2275 | return NULL_RTX; |
2276 | ||
2277 | target1 = gen_reg_rtx (mode); | |
2278 | target2 = gen_reg_rtx (mode); | |
2279 | ||
8ec3c5c2 | 2280 | op0 = expand_normal (arg); |
be5575b2 | 2281 | alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true); |
2282 | alias_off = build_int_cst (alias_type, 0); | |
2283 | op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2284 | sinp, alias_off)); | |
2285 | op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2286 | cosp, alias_off)); | |
c3147c1a | 2287 | |
2288 | /* Compute into target1 and target2. | |
2289 | Set TARGET to wherever the result comes back. */ | |
2290 | result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0); | |
2291 | gcc_assert (result); | |
2292 | ||
2293 | /* Move target1 and target2 to the memory locations indicated | |
2294 | by op1 and op2. */ | |
2295 | emit_move_insn (op1, target1); | |
2296 | emit_move_insn (op2, target2); | |
2297 | ||
2298 | return const0_rtx; | |
2299 | } | |
2300 | ||
d735c391 | 2301 | /* Expand a call to the internal cexpi builtin to the sincos math function. |
2302 | EXP is the expression that is a call to the builtin function; if convenient, | |
f97eea22 | 2303 | the result should be placed in TARGET. */ |
d735c391 | 2304 | |
2305 | static rtx | |
f97eea22 | 2306 | expand_builtin_cexpi (tree exp, rtx target) |
d735c391 | 2307 | { |
2308 | tree fndecl = get_callee_fndecl (exp); | |
d735c391 | 2309 | tree arg, type; |
3754d046 | 2310 | machine_mode mode; |
d735c391 | 2311 | rtx op0, op1, op2; |
389dd41b | 2312 | location_t loc = EXPR_LOCATION (exp); |
d735c391 | 2313 | |
c2f47e15 | 2314 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2315 | return NULL_RTX; | |
d735c391 | 2316 | |
c2f47e15 | 2317 | arg = CALL_EXPR_ARG (exp, 0); |
d735c391 | 2318 | type = TREE_TYPE (arg); |
2319 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2320 | ||
2321 | /* Try expanding via a sincos optab, fall back to emitting a libcall | |
18b8d8ae | 2322 | to sincos or cexp. We are sure we have sincos or cexp because cexpi |
2323 | is only generated from sincos, cexp or if we have either of them. */ | |
d6bf3b14 | 2324 | if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing) |
d735c391 | 2325 | { |
2326 | op1 = gen_reg_rtx (mode); | |
2327 | op2 = gen_reg_rtx (mode); | |
2328 | ||
f97eea22 | 2329 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2330 | |
2331 | /* Compute into op1 and op2. */ | |
2332 | expand_twoval_unop (sincos_optab, op0, op2, op1, 0); | |
2333 | } | |
30f690e0 | 2334 | else if (targetm.libc_has_function (function_sincos)) |
d735c391 | 2335 | { |
c2f47e15 | 2336 | tree call, fn = NULL_TREE; |
d735c391 | 2337 | tree top1, top2; |
2338 | rtx op1a, op2a; | |
2339 | ||
2340 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
b9a16870 | 2341 | fn = builtin_decl_explicit (BUILT_IN_SINCOSF); |
d735c391 | 2342 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
b9a16870 | 2343 | fn = builtin_decl_explicit (BUILT_IN_SINCOS); |
d735c391 | 2344 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
b9a16870 | 2345 | fn = builtin_decl_explicit (BUILT_IN_SINCOSL); |
c2f47e15 | 2346 | else |
2347 | gcc_unreachable (); | |
48e1416a | 2348 | |
0ab48139 | 2349 | op1 = assign_temp (TREE_TYPE (arg), 1, 1); |
2350 | op2 = assign_temp (TREE_TYPE (arg), 1, 1); | |
99182918 | 2351 | op1a = copy_addr_to_reg (XEXP (op1, 0)); |
2352 | op2a = copy_addr_to_reg (XEXP (op2, 0)); | |
d735c391 | 2353 | top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a); |
2354 | top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a); | |
2355 | ||
d735c391 | 2356 | /* Make sure not to fold the sincos call again. */ |
2357 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
c2f47e15 | 2358 | expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)), |
2359 | call, 3, arg, top1, top2)); | |
d735c391 | 2360 | } |
18b8d8ae | 2361 | else |
2362 | { | |
0ecbc158 | 2363 | tree call, fn = NULL_TREE, narg; |
18b8d8ae | 2364 | tree ctype = build_complex_type (type); |
2365 | ||
0ecbc158 | 2366 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) |
b9a16870 | 2367 | fn = builtin_decl_explicit (BUILT_IN_CEXPF); |
0ecbc158 | 2368 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
b9a16870 | 2369 | fn = builtin_decl_explicit (BUILT_IN_CEXP); |
0ecbc158 | 2370 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
b9a16870 | 2371 | fn = builtin_decl_explicit (BUILT_IN_CEXPL); |
c2f47e15 | 2372 | else |
2373 | gcc_unreachable (); | |
fc0dfa6e | 2374 | |
2375 | /* If we don't have a decl for cexp create one. This is the | |
2376 | friendliest fallback if the user calls __builtin_cexpi | |
2377 | without full target C99 function support. */ | |
2378 | if (fn == NULL_TREE) | |
2379 | { | |
2380 | tree fntype; | |
2381 | const char *name = NULL; | |
2382 | ||
2383 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
2384 | name = "cexpf"; | |
2385 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2386 | name = "cexp"; | |
2387 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2388 | name = "cexpl"; | |
2389 | ||
2390 | fntype = build_function_type_list (ctype, ctype, NULL_TREE); | |
2391 | fn = build_fn_decl (name, fntype); | |
2392 | } | |
2393 | ||
389dd41b | 2394 | narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype, |
18b8d8ae | 2395 | build_real (type, dconst0), arg); |
2396 | ||
2397 | /* Make sure not to fold the cexp call again. */ | |
2398 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
48e1416a | 2399 | return expand_expr (build_call_nary (ctype, call, 1, narg), |
1db6d067 | 2400 | target, VOIDmode, EXPAND_NORMAL); |
18b8d8ae | 2401 | } |
d735c391 | 2402 | |
2403 | /* Now build the proper return type. */ | |
2404 | return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type), | |
2405 | make_tree (TREE_TYPE (arg), op2), | |
2406 | make_tree (TREE_TYPE (arg), op1)), | |
1db6d067 | 2407 | target, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2408 | } |
2409 | ||
a65c4d64 | 2410 | /* Conveniently construct a function call expression. FNDECL names the |
2411 | function to be called, N is the number of arguments, and the "..." | |
2412 | parameters are the argument expressions. Unlike build_call_exr | |
2413 | this doesn't fold the call, hence it will always return a CALL_EXPR. */ | |
2414 | ||
2415 | static tree | |
2416 | build_call_nofold_loc (location_t loc, tree fndecl, int n, ...) | |
2417 | { | |
2418 | va_list ap; | |
2419 | tree fntype = TREE_TYPE (fndecl); | |
2420 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
2421 | ||
2422 | va_start (ap, n); | |
2423 | fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap); | |
2424 | va_end (ap); | |
2425 | SET_EXPR_LOCATION (fn, loc); | |
2426 | return fn; | |
2427 | } | |
a65c4d64 | 2428 | |
7d3afc77 | 2429 | /* Expand a call to one of the builtin rounding functions gcc defines |
2430 | as an extension (lfloor and lceil). As these are gcc extensions we | |
2431 | do not need to worry about setting errno to EDOM. | |
ad52b9b7 | 2432 | If expanding via optab fails, lower expression to (int)(floor(x)). |
2433 | EXP is the expression that is a call to the builtin function; | |
ff1b14e4 | 2434 | if convenient, the result should be placed in TARGET. */ |
ad52b9b7 | 2435 | |
2436 | static rtx | |
ff1b14e4 | 2437 | expand_builtin_int_roundingfn (tree exp, rtx target) |
ad52b9b7 | 2438 | { |
9c42dd28 | 2439 | convert_optab builtin_optab; |
1e0c0b35 | 2440 | rtx op0, tmp; |
2441 | rtx_insn *insns; | |
ad52b9b7 | 2442 | tree fndecl = get_callee_fndecl (exp); |
ad52b9b7 | 2443 | enum built_in_function fallback_fn; |
2444 | tree fallback_fndecl; | |
3754d046 | 2445 | machine_mode mode; |
4de0924f | 2446 | tree arg; |
ad52b9b7 | 2447 | |
c2f47e15 | 2448 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
ad52b9b7 | 2449 | gcc_unreachable (); |
2450 | ||
c2f47e15 | 2451 | arg = CALL_EXPR_ARG (exp, 0); |
ad52b9b7 | 2452 | |
2453 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2454 | { | |
80ff6494 | 2455 | CASE_FLT_FN (BUILT_IN_ICEIL): |
4f35b1fc | 2456 | CASE_FLT_FN (BUILT_IN_LCEIL): |
2457 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 2458 | builtin_optab = lceil_optab; |
2459 | fallback_fn = BUILT_IN_CEIL; | |
2460 | break; | |
2461 | ||
80ff6494 | 2462 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
4f35b1fc | 2463 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
2464 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 2465 | builtin_optab = lfloor_optab; |
2466 | fallback_fn = BUILT_IN_FLOOR; | |
2467 | break; | |
2468 | ||
2469 | default: | |
2470 | gcc_unreachable (); | |
2471 | } | |
2472 | ||
2473 | /* Make a suitable register to place result in. */ | |
2474 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2475 | ||
9c42dd28 | 2476 | target = gen_reg_rtx (mode); |
ad52b9b7 | 2477 | |
9c42dd28 | 2478 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2479 | need to expand the argument again. This way, we will not perform | |
2480 | side-effects more the once. */ | |
abfea505 | 2481 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
ad52b9b7 | 2482 | |
ff1b14e4 | 2483 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
ad52b9b7 | 2484 | |
9c42dd28 | 2485 | start_sequence (); |
ad52b9b7 | 2486 | |
9c42dd28 | 2487 | /* Compute into TARGET. */ |
2488 | if (expand_sfix_optab (target, op0, builtin_optab)) | |
2489 | { | |
2490 | /* Output the entire sequence. */ | |
2491 | insns = get_insns (); | |
ad52b9b7 | 2492 | end_sequence (); |
9c42dd28 | 2493 | emit_insn (insns); |
2494 | return target; | |
ad52b9b7 | 2495 | } |
2496 | ||
9c42dd28 | 2497 | /* If we were unable to expand via the builtin, stop the sequence |
2498 | (without outputting the insns). */ | |
2499 | end_sequence (); | |
2500 | ||
ad52b9b7 | 2501 | /* Fall back to floating point rounding optab. */ |
2502 | fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn); | |
fc0dfa6e | 2503 | |
2504 | /* For non-C99 targets we may end up without a fallback fndecl here | |
2505 | if the user called __builtin_lfloor directly. In this case emit | |
2506 | a call to the floor/ceil variants nevertheless. This should result | |
2507 | in the best user experience for not full C99 targets. */ | |
2508 | if (fallback_fndecl == NULL_TREE) | |
2509 | { | |
2510 | tree fntype; | |
2511 | const char *name = NULL; | |
2512 | ||
2513 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2514 | { | |
80ff6494 | 2515 | case BUILT_IN_ICEIL: |
fc0dfa6e | 2516 | case BUILT_IN_LCEIL: |
2517 | case BUILT_IN_LLCEIL: | |
2518 | name = "ceil"; | |
2519 | break; | |
80ff6494 | 2520 | case BUILT_IN_ICEILF: |
fc0dfa6e | 2521 | case BUILT_IN_LCEILF: |
2522 | case BUILT_IN_LLCEILF: | |
2523 | name = "ceilf"; | |
2524 | break; | |
80ff6494 | 2525 | case BUILT_IN_ICEILL: |
fc0dfa6e | 2526 | case BUILT_IN_LCEILL: |
2527 | case BUILT_IN_LLCEILL: | |
2528 | name = "ceill"; | |
2529 | break; | |
80ff6494 | 2530 | case BUILT_IN_IFLOOR: |
fc0dfa6e | 2531 | case BUILT_IN_LFLOOR: |
2532 | case BUILT_IN_LLFLOOR: | |
2533 | name = "floor"; | |
2534 | break; | |
80ff6494 | 2535 | case BUILT_IN_IFLOORF: |
fc0dfa6e | 2536 | case BUILT_IN_LFLOORF: |
2537 | case BUILT_IN_LLFLOORF: | |
2538 | name = "floorf"; | |
2539 | break; | |
80ff6494 | 2540 | case BUILT_IN_IFLOORL: |
fc0dfa6e | 2541 | case BUILT_IN_LFLOORL: |
2542 | case BUILT_IN_LLFLOORL: | |
2543 | name = "floorl"; | |
2544 | break; | |
2545 | default: | |
2546 | gcc_unreachable (); | |
2547 | } | |
2548 | ||
2549 | fntype = build_function_type_list (TREE_TYPE (arg), | |
2550 | TREE_TYPE (arg), NULL_TREE); | |
2551 | fallback_fndecl = build_fn_decl (name, fntype); | |
2552 | } | |
2553 | ||
0568e9c1 | 2554 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg); |
ad52b9b7 | 2555 | |
d4c690af | 2556 | tmp = expand_normal (exp); |
933eb13a | 2557 | tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp)); |
ad52b9b7 | 2558 | |
2559 | /* Truncate the result of floating point optab to integer | |
2560 | via expand_fix (). */ | |
2561 | target = gen_reg_rtx (mode); | |
2562 | expand_fix (target, tmp, 0); | |
2563 | ||
2564 | return target; | |
2565 | } | |
2566 | ||
7d3afc77 | 2567 | /* Expand a call to one of the builtin math functions doing integer |
2568 | conversion (lrint). | |
2569 | Return 0 if a normal call should be emitted rather than expanding the | |
2570 | function in-line. EXP is the expression that is a call to the builtin | |
ff1b14e4 | 2571 | function; if convenient, the result should be placed in TARGET. */ |
7d3afc77 | 2572 | |
2573 | static rtx | |
ff1b14e4 | 2574 | expand_builtin_int_roundingfn_2 (tree exp, rtx target) |
7d3afc77 | 2575 | { |
5f51ee59 | 2576 | convert_optab builtin_optab; |
1e0c0b35 | 2577 | rtx op0; |
2578 | rtx_insn *insns; | |
7d3afc77 | 2579 | tree fndecl = get_callee_fndecl (exp); |
4de0924f | 2580 | tree arg; |
3754d046 | 2581 | machine_mode mode; |
e951f9a4 | 2582 | enum built_in_function fallback_fn = BUILT_IN_NONE; |
7d3afc77 | 2583 | |
c2f47e15 | 2584 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2585 | gcc_unreachable (); | |
48e1416a | 2586 | |
c2f47e15 | 2587 | arg = CALL_EXPR_ARG (exp, 0); |
7d3afc77 | 2588 | |
2589 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2590 | { | |
80ff6494 | 2591 | CASE_FLT_FN (BUILT_IN_IRINT): |
e951f9a4 | 2592 | fallback_fn = BUILT_IN_LRINT; |
2593 | /* FALLTHRU */ | |
7d3afc77 | 2594 | CASE_FLT_FN (BUILT_IN_LRINT): |
2595 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
e951f9a4 | 2596 | builtin_optab = lrint_optab; |
2597 | break; | |
80ff6494 | 2598 | |
2599 | CASE_FLT_FN (BUILT_IN_IROUND): | |
e951f9a4 | 2600 | fallback_fn = BUILT_IN_LROUND; |
2601 | /* FALLTHRU */ | |
ef2f1a10 | 2602 | CASE_FLT_FN (BUILT_IN_LROUND): |
2603 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
e951f9a4 | 2604 | builtin_optab = lround_optab; |
2605 | break; | |
80ff6494 | 2606 | |
7d3afc77 | 2607 | default: |
2608 | gcc_unreachable (); | |
2609 | } | |
2610 | ||
e951f9a4 | 2611 | /* There's no easy way to detect the case we need to set EDOM. */ |
2612 | if (flag_errno_math && fallback_fn == BUILT_IN_NONE) | |
2613 | return NULL_RTX; | |
2614 | ||
7d3afc77 | 2615 | /* Make a suitable register to place result in. */ |
2616 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2617 | ||
e951f9a4 | 2618 | /* There's no easy way to detect the case we need to set EDOM. */ |
2619 | if (!flag_errno_math) | |
2620 | { | |
de2e453e | 2621 | rtx result = gen_reg_rtx (mode); |
7d3afc77 | 2622 | |
e951f9a4 | 2623 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2624 | need to expand the argument again. This way, we will not perform | |
2625 | side-effects more the once. */ | |
2626 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); | |
7d3afc77 | 2627 | |
e951f9a4 | 2628 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
7d3afc77 | 2629 | |
e951f9a4 | 2630 | start_sequence (); |
7d3afc77 | 2631 | |
de2e453e | 2632 | if (expand_sfix_optab (result, op0, builtin_optab)) |
e951f9a4 | 2633 | { |
2634 | /* Output the entire sequence. */ | |
2635 | insns = get_insns (); | |
2636 | end_sequence (); | |
2637 | emit_insn (insns); | |
de2e453e | 2638 | return result; |
e951f9a4 | 2639 | } |
2640 | ||
2641 | /* If we were unable to expand via the builtin, stop the sequence | |
2642 | (without outputting the insns) and call to the library function | |
2643 | with the stabilized argument list. */ | |
7d3afc77 | 2644 | end_sequence (); |
2645 | } | |
2646 | ||
e951f9a4 | 2647 | if (fallback_fn != BUILT_IN_NONE) |
2648 | { | |
2649 | /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99 | |
2650 | targets, (int) round (x) should never be transformed into | |
2651 | BUILT_IN_IROUND and if __builtin_iround is called directly, emit | |
2652 | a call to lround in the hope that the target provides at least some | |
2653 | C99 functions. This should result in the best user experience for | |
2654 | not full C99 targets. */ | |
e3240774 | 2655 | tree fallback_fndecl = mathfn_built_in_1 |
2656 | (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); | |
e951f9a4 | 2657 | |
2658 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), | |
2659 | fallback_fndecl, 1, arg); | |
2660 | ||
2661 | target = expand_call (exp, NULL_RTX, target == const0_rtx); | |
933eb13a | 2662 | target = maybe_emit_group_store (target, TREE_TYPE (exp)); |
e951f9a4 | 2663 | return convert_to_mode (mode, target, 0); |
2664 | } | |
5f51ee59 | 2665 | |
de2e453e | 2666 | return expand_call (exp, target, target == const0_rtx); |
7d3afc77 | 2667 | } |
2668 | ||
c2f47e15 | 2669 | /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if |
757c219d | 2670 | a normal call should be emitted rather than expanding the function |
2671 | in-line. EXP is the expression that is a call to the builtin | |
2672 | function; if convenient, the result should be placed in TARGET. */ | |
2673 | ||
2674 | static rtx | |
f97eea22 | 2675 | expand_builtin_powi (tree exp, rtx target) |
757c219d | 2676 | { |
757c219d | 2677 | tree arg0, arg1; |
2678 | rtx op0, op1; | |
3754d046 | 2679 | machine_mode mode; |
2680 | machine_mode mode2; | |
757c219d | 2681 | |
c2f47e15 | 2682 | if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) |
2683 | return NULL_RTX; | |
757c219d | 2684 | |
c2f47e15 | 2685 | arg0 = CALL_EXPR_ARG (exp, 0); |
2686 | arg1 = CALL_EXPR_ARG (exp, 1); | |
757c219d | 2687 | mode = TYPE_MODE (TREE_TYPE (exp)); |
2688 | ||
757c219d | 2689 | /* Emit a libcall to libgcc. */ |
2690 | ||
c2f47e15 | 2691 | /* Mode of the 2nd argument must match that of an int. */ |
d0405f40 | 2692 | mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0); |
2693 | ||
757c219d | 2694 | if (target == NULL_RTX) |
2695 | target = gen_reg_rtx (mode); | |
2696 | ||
f97eea22 | 2697 | op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL); |
757c219d | 2698 | if (GET_MODE (op0) != mode) |
2699 | op0 = convert_to_mode (mode, op0, 0); | |
1db6d067 | 2700 | op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL); |
d0405f40 | 2701 | if (GET_MODE (op1) != mode2) |
2702 | op1 = convert_to_mode (mode2, op1, 0); | |
757c219d | 2703 | |
f36b9f69 | 2704 | target = emit_library_call_value (optab_libfunc (powi_optab, mode), |
2dd6f9ed | 2705 | target, LCT_CONST, mode, 2, |
d0405f40 | 2706 | op0, mode, op1, mode2); |
757c219d | 2707 | |
2708 | return target; | |
2709 | } | |
2710 | ||
48e1416a | 2711 | /* Expand expression EXP which is a call to the strlen builtin. Return |
c2f47e15 | 2712 | NULL_RTX if we failed the caller should emit a normal call, otherwise |
aed0bd19 | 2713 | try to get the result in TARGET, if convenient. */ |
f7c44134 | 2714 | |
53800dbe | 2715 | static rtx |
c2f47e15 | 2716 | expand_builtin_strlen (tree exp, rtx target, |
3754d046 | 2717 | machine_mode target_mode) |
53800dbe | 2718 | { |
c2f47e15 | 2719 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2720 | return NULL_RTX; | |
53800dbe | 2721 | else |
2722 | { | |
8786db1e | 2723 | struct expand_operand ops[4]; |
911c0150 | 2724 | rtx pat; |
c2f47e15 | 2725 | tree len; |
2726 | tree src = CALL_EXPR_ARG (exp, 0); | |
1e0c0b35 | 2727 | rtx src_reg; |
2728 | rtx_insn *before_strlen; | |
3754d046 | 2729 | machine_mode insn_mode = target_mode; |
ef2c4a29 | 2730 | enum insn_code icode = CODE_FOR_nothing; |
153c3b50 | 2731 | unsigned int align; |
6248e345 | 2732 | |
2733 | /* If the length can be computed at compile-time, return it. */ | |
681fab1e | 2734 | len = c_strlen (src, 0); |
6248e345 | 2735 | if (len) |
80cd7a5e | 2736 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); |
6248e345 | 2737 | |
681fab1e | 2738 | /* If the length can be computed at compile-time and is constant |
2739 | integer, but there are side-effects in src, evaluate | |
2740 | src for side-effects, then return len. | |
2741 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
2742 | can be optimized into: i++; x = 3; */ | |
2743 | len = c_strlen (src, 1); | |
2744 | if (len && TREE_CODE (len) == INTEGER_CST) | |
2745 | { | |
2746 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2747 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2748 | } | |
2749 | ||
957d0361 | 2750 | align = get_pointer_alignment (src) / BITS_PER_UNIT; |
53800dbe | 2751 | |
53800dbe | 2752 | /* If SRC is not a pointer type, don't do this operation inline. */ |
2753 | if (align == 0) | |
c2f47e15 | 2754 | return NULL_RTX; |
53800dbe | 2755 | |
911c0150 | 2756 | /* Bail out if we can't compute strlen in the right mode. */ |
53800dbe | 2757 | while (insn_mode != VOIDmode) |
2758 | { | |
d6bf3b14 | 2759 | icode = optab_handler (strlen_optab, insn_mode); |
53800dbe | 2760 | if (icode != CODE_FOR_nothing) |
c28ae87f | 2761 | break; |
53800dbe | 2762 | |
2763 | insn_mode = GET_MODE_WIDER_MODE (insn_mode); | |
2764 | } | |
2765 | if (insn_mode == VOIDmode) | |
c2f47e15 | 2766 | return NULL_RTX; |
53800dbe | 2767 | |
911c0150 | 2768 | /* Make a place to hold the source address. We will not expand |
2769 | the actual source until we are sure that the expansion will | |
2770 | not fail -- there are trees that cannot be expanded twice. */ | |
2771 | src_reg = gen_reg_rtx (Pmode); | |
53800dbe | 2772 | |
911c0150 | 2773 | /* Mark the beginning of the strlen sequence so we can emit the |
2774 | source operand later. */ | |
f0ce3b1f | 2775 | before_strlen = get_last_insn (); |
53800dbe | 2776 | |
8786db1e | 2777 | create_output_operand (&ops[0], target, insn_mode); |
2778 | create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg)); | |
2779 | create_integer_operand (&ops[2], 0); | |
2780 | create_integer_operand (&ops[3], align); | |
2781 | if (!maybe_expand_insn (icode, 4, ops)) | |
c2f47e15 | 2782 | return NULL_RTX; |
911c0150 | 2783 | |
2784 | /* Now that we are assured of success, expand the source. */ | |
2785 | start_sequence (); | |
499eee58 | 2786 | pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL); |
911c0150 | 2787 | if (pat != src_reg) |
499eee58 | 2788 | { |
2789 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2790 | if (GET_MODE (pat) != Pmode) | |
2791 | pat = convert_to_mode (Pmode, pat, | |
2792 | POINTERS_EXTEND_UNSIGNED); | |
2793 | #endif | |
2794 | emit_move_insn (src_reg, pat); | |
2795 | } | |
31d3e01c | 2796 | pat = get_insns (); |
911c0150 | 2797 | end_sequence (); |
bceb0d1f | 2798 | |
2799 | if (before_strlen) | |
2800 | emit_insn_after (pat, before_strlen); | |
2801 | else | |
2802 | emit_insn_before (pat, get_insns ()); | |
53800dbe | 2803 | |
2804 | /* Return the value in the proper mode for this function. */ | |
8786db1e | 2805 | if (GET_MODE (ops[0].value) == target_mode) |
2806 | target = ops[0].value; | |
53800dbe | 2807 | else if (target != 0) |
8786db1e | 2808 | convert_move (target, ops[0].value, 0); |
53800dbe | 2809 | else |
8786db1e | 2810 | target = convert_to_mode (target_mode, ops[0].value, 0); |
911c0150 | 2811 | |
2812 | return target; | |
53800dbe | 2813 | } |
2814 | } | |
2815 | ||
6840589f | 2816 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2817 | bytes from constant string DATA + OFFSET and return it as target | |
2818 | constant. */ | |
2819 | ||
2820 | static rtx | |
aecda0d6 | 2821 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
3754d046 | 2822 | machine_mode mode) |
6840589f | 2823 | { |
2824 | const char *str = (const char *) data; | |
2825 | ||
64db345d | 2826 | gcc_assert (offset >= 0 |
2827 | && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
2828 | <= strlen (str) + 1)); | |
6840589f | 2829 | |
2830 | return c_readstr (str + offset, mode); | |
2831 | } | |
2832 | ||
36d63243 | 2833 | /* LEN specify length of the block of memcpy/memset operation. |
9db0f34d | 2834 | Figure out its range and put it into MIN_SIZE/MAX_SIZE. |
2835 | In some cases we can make very likely guess on max size, then we | |
2836 | set it into PROBABLE_MAX_SIZE. */ | |
36d63243 | 2837 | |
2838 | static void | |
2839 | determine_block_size (tree len, rtx len_rtx, | |
2840 | unsigned HOST_WIDE_INT *min_size, | |
9db0f34d | 2841 | unsigned HOST_WIDE_INT *max_size, |
2842 | unsigned HOST_WIDE_INT *probable_max_size) | |
36d63243 | 2843 | { |
2844 | if (CONST_INT_P (len_rtx)) | |
2845 | { | |
4e140a5c | 2846 | *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx); |
36d63243 | 2847 | return; |
2848 | } | |
2849 | else | |
2850 | { | |
9c1be15e | 2851 | wide_int min, max; |
9db0f34d | 2852 | enum value_range_type range_type = VR_UNDEFINED; |
2853 | ||
2854 | /* Determine bounds from the type. */ | |
2855 | if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len)))) | |
2856 | *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len))); | |
2857 | else | |
2858 | *min_size = 0; | |
2859 | if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len)))) | |
4e140a5c | 2860 | *probable_max_size = *max_size |
2861 | = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len))); | |
9db0f34d | 2862 | else |
2863 | *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx)); | |
2864 | ||
2865 | if (TREE_CODE (len) == SSA_NAME) | |
2866 | range_type = get_range_info (len, &min, &max); | |
2867 | if (range_type == VR_RANGE) | |
36d63243 | 2868 | { |
fe5ad926 | 2869 | if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ()) |
36d63243 | 2870 | *min_size = min.to_uhwi (); |
fe5ad926 | 2871 | if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ()) |
9db0f34d | 2872 | *probable_max_size = *max_size = max.to_uhwi (); |
36d63243 | 2873 | } |
9db0f34d | 2874 | else if (range_type == VR_ANTI_RANGE) |
36d63243 | 2875 | { |
4a474a5a | 2876 | /* Anti range 0...N lets us to determine minimal size to N+1. */ |
fe5ad926 | 2877 | if (min == 0) |
9db0f34d | 2878 | { |
9c1be15e | 2879 | if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0) |
2880 | *min_size = max.to_uhwi () + 1; | |
9db0f34d | 2881 | } |
2882 | /* Code like | |
2883 | ||
2884 | int n; | |
2885 | if (n < 100) | |
4a474a5a | 2886 | memcpy (a, b, n) |
9db0f34d | 2887 | |
2888 | Produce anti range allowing negative values of N. We still | |
2889 | can use the information and make a guess that N is not negative. | |
2890 | */ | |
fe5ad926 | 2891 | else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min)) |
2892 | *probable_max_size = min.to_uhwi () - 1; | |
36d63243 | 2893 | } |
2894 | } | |
2895 | gcc_checking_assert (*max_size <= | |
2896 | (unsigned HOST_WIDE_INT) | |
2897 | GET_MODE_MASK (GET_MODE (len_rtx))); | |
2898 | } | |
2899 | ||
f21337ef | 2900 | /* Helper function to do the actual work for expand_builtin_memcpy. */ |
2901 | ||
2902 | static rtx | |
2903 | expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp) | |
2904 | { | |
2905 | const char *src_str; | |
2906 | unsigned int src_align = get_pointer_alignment (src); | |
2907 | unsigned int dest_align = get_pointer_alignment (dest); | |
2908 | rtx dest_mem, src_mem, dest_addr, len_rtx; | |
2909 | HOST_WIDE_INT expected_size = -1; | |
2910 | unsigned int expected_align = 0; | |
2911 | unsigned HOST_WIDE_INT min_size; | |
2912 | unsigned HOST_WIDE_INT max_size; | |
2913 | unsigned HOST_WIDE_INT probable_max_size; | |
2914 | ||
2915 | /* If DEST is not a pointer type, call the normal function. */ | |
2916 | if (dest_align == 0) | |
2917 | return NULL_RTX; | |
2918 | ||
2919 | /* If either SRC is not a pointer type, don't do this | |
2920 | operation in-line. */ | |
2921 | if (src_align == 0) | |
2922 | return NULL_RTX; | |
2923 | ||
2924 | if (currently_expanding_gimple_stmt) | |
2925 | stringop_block_profile (currently_expanding_gimple_stmt, | |
2926 | &expected_align, &expected_size); | |
2927 | ||
2928 | if (expected_align < dest_align) | |
2929 | expected_align = dest_align; | |
2930 | dest_mem = get_memory_rtx (dest, len); | |
2931 | set_mem_align (dest_mem, dest_align); | |
2932 | len_rtx = expand_normal (len); | |
2933 | determine_block_size (len, len_rtx, &min_size, &max_size, | |
2934 | &probable_max_size); | |
2935 | src_str = c_getstr (src); | |
2936 | ||
2937 | /* If SRC is a string constant and block move would be done | |
2938 | by pieces, we can avoid loading the string from memory | |
2939 | and only stored the computed constants. */ | |
2940 | if (src_str | |
2941 | && CONST_INT_P (len_rtx) | |
2942 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
2943 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
2944 | CONST_CAST (char *, src_str), | |
2945 | dest_align, false)) | |
2946 | { | |
2947 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
2948 | builtin_memcpy_read_str, | |
2949 | CONST_CAST (char *, src_str), | |
2950 | dest_align, false, 0); | |
2951 | dest_mem = force_operand (XEXP (dest_mem, 0), target); | |
2952 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2953 | return dest_mem; | |
2954 | } | |
2955 | ||
2956 | src_mem = get_memory_rtx (src, len); | |
2957 | set_mem_align (src_mem, src_align); | |
2958 | ||
2959 | /* Copy word part most expediently. */ | |
2960 | dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, | |
2961 | CALL_EXPR_TAILCALL (exp) | |
2962 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
2963 | expected_align, expected_size, | |
2964 | min_size, max_size, probable_max_size); | |
2965 | ||
2966 | if (dest_addr == 0) | |
2967 | { | |
2968 | dest_addr = force_operand (XEXP (dest_mem, 0), target); | |
2969 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
2970 | } | |
2971 | ||
2972 | return dest_addr; | |
2973 | } | |
2974 | ||
c2f47e15 | 2975 | /* Expand a call EXP to the memcpy builtin. |
2976 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
3b824fa6 | 2977 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 2978 | mode MODE if that's convenient). */ |
c2f47e15 | 2979 | |
53800dbe | 2980 | static rtx |
a65c4d64 | 2981 | expand_builtin_memcpy (tree exp, rtx target) |
53800dbe | 2982 | { |
c2f47e15 | 2983 | if (!validate_arglist (exp, |
2984 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2985 | return NULL_RTX; | |
53800dbe | 2986 | else |
2987 | { | |
c2f47e15 | 2988 | tree dest = CALL_EXPR_ARG (exp, 0); |
2989 | tree src = CALL_EXPR_ARG (exp, 1); | |
2990 | tree len = CALL_EXPR_ARG (exp, 2); | |
f21337ef | 2991 | return expand_builtin_memcpy_args (dest, src, len, target, exp); |
2992 | } | |
2993 | } | |
6840589f | 2994 | |
f21337ef | 2995 | /* Expand an instrumented call EXP to the memcpy builtin. |
2996 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
2997 | otherwise try to get the result in TARGET, if convenient (and in | |
2998 | mode MODE if that's convenient). */ | |
53800dbe | 2999 | |
f21337ef | 3000 | static rtx |
3001 | expand_builtin_memcpy_with_bounds (tree exp, rtx target) | |
3002 | { | |
3003 | if (!validate_arglist (exp, | |
3004 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3005 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3006 | INTEGER_TYPE, VOID_TYPE)) | |
3007 | return NULL_RTX; | |
3008 | else | |
3009 | { | |
3010 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3011 | tree src = CALL_EXPR_ARG (exp, 2); | |
3012 | tree len = CALL_EXPR_ARG (exp, 4); | |
3013 | rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp); | |
53800dbe | 3014 | |
f21337ef | 3015 | /* Return src bounds with the result. */ |
3016 | if (res) | |
e5716f7e | 3017 | { |
17d388d8 | 3018 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
f21337ef | 3019 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3020 | res = chkp_join_splitted_slot (res, bnd); | |
e5716f7e | 3021 | } |
f21337ef | 3022 | return res; |
53800dbe | 3023 | } |
3024 | } | |
3025 | ||
c2f47e15 | 3026 | /* Expand a call EXP to the mempcpy builtin. |
3027 | Return NULL_RTX if we failed; the caller should emit a normal call, | |
647661c6 | 3028 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 3029 | mode MODE if that's convenient). If ENDP is 0 return the |
3030 | destination pointer, if ENDP is 1 return the end pointer ala | |
3031 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3032 | stpcpy. */ | |
647661c6 | 3033 | |
3034 | static rtx | |
3754d046 | 3035 | expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode) |
647661c6 | 3036 | { |
c2f47e15 | 3037 | if (!validate_arglist (exp, |
3038 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3039 | return NULL_RTX; | |
3040 | else | |
3041 | { | |
3042 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3043 | tree src = CALL_EXPR_ARG (exp, 1); | |
3044 | tree len = CALL_EXPR_ARG (exp, 2); | |
3045 | return expand_builtin_mempcpy_args (dest, src, len, | |
f21337ef | 3046 | target, mode, /*endp=*/ 1, |
3047 | exp); | |
3048 | } | |
3049 | } | |
3050 | ||
3051 | /* Expand an instrumented call EXP to the mempcpy builtin. | |
3052 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
3053 | otherwise try to get the result in TARGET, if convenient (and in | |
3054 | mode MODE if that's convenient). */ | |
3055 | ||
3056 | static rtx | |
3057 | expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode) | |
3058 | { | |
3059 | if (!validate_arglist (exp, | |
3060 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3061 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3062 | INTEGER_TYPE, VOID_TYPE)) | |
3063 | return NULL_RTX; | |
3064 | else | |
3065 | { | |
3066 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3067 | tree src = CALL_EXPR_ARG (exp, 2); | |
3068 | tree len = CALL_EXPR_ARG (exp, 4); | |
3069 | rtx res = expand_builtin_mempcpy_args (dest, src, len, target, | |
3070 | mode, 1, exp); | |
3071 | ||
3072 | /* Return src bounds with the result. */ | |
3073 | if (res) | |
3074 | { | |
17d388d8 | 3075 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
f21337ef | 3076 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3077 | res = chkp_join_splitted_slot (res, bnd); | |
3078 | } | |
3079 | return res; | |
c2f47e15 | 3080 | } |
3081 | } | |
3082 | ||
3083 | /* Helper function to do the actual work for expand_builtin_mempcpy. The | |
3084 | arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out | |
3085 | so that this can also be called without constructing an actual CALL_EXPR. | |
a65c4d64 | 3086 | The other arguments and return value are the same as for |
3087 | expand_builtin_mempcpy. */ | |
c2f47e15 | 3088 | |
3089 | static rtx | |
a65c4d64 | 3090 | expand_builtin_mempcpy_args (tree dest, tree src, tree len, |
f21337ef | 3091 | rtx target, machine_mode mode, int endp, |
3092 | tree orig_exp) | |
c2f47e15 | 3093 | { |
f21337ef | 3094 | tree fndecl = get_callee_fndecl (orig_exp); |
3095 | ||
c2f47e15 | 3096 | /* If return value is ignored, transform mempcpy into memcpy. */ |
f21337ef | 3097 | if (target == const0_rtx |
3098 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP | |
3099 | && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP)) | |
3100 | { | |
3101 | tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP); | |
3102 | tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3, | |
3103 | dest, src, len); | |
3104 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3105 | } | |
3106 | else if (target == const0_rtx | |
3107 | && builtin_decl_implicit_p (BUILT_IN_MEMCPY)) | |
9fe0e1b8 | 3108 | { |
b9a16870 | 3109 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY); |
0568e9c1 | 3110 | tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3, |
3111 | dest, src, len); | |
c8b17b2e | 3112 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
9fe0e1b8 | 3113 | } |
647661c6 | 3114 | else |
3115 | { | |
9fe0e1b8 | 3116 | const char *src_str; |
957d0361 | 3117 | unsigned int src_align = get_pointer_alignment (src); |
3118 | unsigned int dest_align = get_pointer_alignment (dest); | |
9fe0e1b8 | 3119 | rtx dest_mem, src_mem, len_rtx; |
a0c938f0 | 3120 | |
7da1412b | 3121 | /* If either SRC or DEST is not a pointer type, don't do this |
a0c938f0 | 3122 | operation in-line. */ |
7da1412b | 3123 | if (dest_align == 0 || src_align == 0) |
c2f47e15 | 3124 | return NULL_RTX; |
9fe0e1b8 | 3125 | |
6217c238 | 3126 | /* If LEN is not constant, call the normal function. */ |
e913b5cd | 3127 | if (! tree_fits_uhwi_p (len)) |
c2f47e15 | 3128 | return NULL_RTX; |
0862b7e9 | 3129 | |
8ec3c5c2 | 3130 | len_rtx = expand_normal (len); |
9fe0e1b8 | 3131 | src_str = c_getstr (src); |
647661c6 | 3132 | |
9fe0e1b8 | 3133 | /* If SRC is a string constant and block move would be done |
3134 | by pieces, we can avoid loading the string from memory | |
3135 | and only stored the computed constants. */ | |
3136 | if (src_str | |
971ba038 | 3137 | && CONST_INT_P (len_rtx) |
9fe0e1b8 | 3138 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 |
3139 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
364c0c59 | 3140 | CONST_CAST (char *, src_str), |
3141 | dest_align, false)) | |
9fe0e1b8 | 3142 | { |
d8ae1baa | 3143 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3144 | set_mem_align (dest_mem, dest_align); |
3145 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
3146 | builtin_memcpy_read_str, | |
364c0c59 | 3147 | CONST_CAST (char *, src_str), |
3148 | dest_align, false, endp); | |
9fe0e1b8 | 3149 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3150 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3151 | return dest_mem; |
647661c6 | 3152 | } |
3153 | ||
971ba038 | 3154 | if (CONST_INT_P (len_rtx) |
9fe0e1b8 | 3155 | && can_move_by_pieces (INTVAL (len_rtx), |
3156 | MIN (dest_align, src_align))) | |
3157 | { | |
d8ae1baa | 3158 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3159 | set_mem_align (dest_mem, dest_align); |
d8ae1baa | 3160 | src_mem = get_memory_rtx (src, len); |
9fe0e1b8 | 3161 | set_mem_align (src_mem, src_align); |
3162 | dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx), | |
3163 | MIN (dest_align, src_align), endp); | |
3164 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 3165 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3166 | return dest_mem; |
3167 | } | |
3168 | ||
c2f47e15 | 3169 | return NULL_RTX; |
647661c6 | 3170 | } |
3171 | } | |
3172 | ||
c2f47e15 | 3173 | /* Expand into a movstr instruction, if one is available. Return NULL_RTX if |
727c62dd | 3174 | we failed, the caller should emit a normal call, otherwise try to |
3175 | get the result in TARGET, if convenient. If ENDP is 0 return the | |
3176 | destination pointer, if ENDP is 1 return the end pointer ala | |
3177 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3178 | stpcpy. */ | |
3179 | ||
3180 | static rtx | |
3181 | expand_movstr (tree dest, tree src, rtx target, int endp) | |
3182 | { | |
8786db1e | 3183 | struct expand_operand ops[3]; |
727c62dd | 3184 | rtx dest_mem; |
3185 | rtx src_mem; | |
727c62dd | 3186 | |
8d74dc42 | 3187 | if (!targetm.have_movstr ()) |
c2f47e15 | 3188 | return NULL_RTX; |
727c62dd | 3189 | |
d8ae1baa | 3190 | dest_mem = get_memory_rtx (dest, NULL); |
3191 | src_mem = get_memory_rtx (src, NULL); | |
727c62dd | 3192 | if (!endp) |
3193 | { | |
3194 | target = force_reg (Pmode, XEXP (dest_mem, 0)); | |
3195 | dest_mem = replace_equiv_address (dest_mem, target); | |
727c62dd | 3196 | } |
3197 | ||
8786db1e | 3198 | create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode); |
3199 | create_fixed_operand (&ops[1], dest_mem); | |
3200 | create_fixed_operand (&ops[2], src_mem); | |
8d74dc42 | 3201 | if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops)) |
1e1d5623 | 3202 | return NULL_RTX; |
727c62dd | 3203 | |
8786db1e | 3204 | if (endp && target != const0_rtx) |
c5aba89c | 3205 | { |
8786db1e | 3206 | target = ops[0].value; |
3207 | /* movstr is supposed to set end to the address of the NUL | |
3208 | terminator. If the caller requested a mempcpy-like return value, | |
3209 | adjust it. */ | |
3210 | if (endp == 1) | |
3211 | { | |
29c05e22 | 3212 | rtx tem = plus_constant (GET_MODE (target), |
3213 | gen_lowpart (GET_MODE (target), target), 1); | |
8786db1e | 3214 | emit_move_insn (target, force_operand (tem, NULL_RTX)); |
3215 | } | |
c5aba89c | 3216 | } |
727c62dd | 3217 | return target; |
3218 | } | |
3219 | ||
48e1416a | 3220 | /* Expand expression EXP, which is a call to the strcpy builtin. Return |
3221 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
c2f47e15 | 3222 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
6f428e8b | 3223 | convenient). */ |
902de8ed | 3224 | |
53800dbe | 3225 | static rtx |
a65c4d64 | 3226 | expand_builtin_strcpy (tree exp, rtx target) |
53800dbe | 3227 | { |
c2f47e15 | 3228 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3229 | { | |
3230 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3231 | tree src = CALL_EXPR_ARG (exp, 1); | |
a65c4d64 | 3232 | return expand_builtin_strcpy_args (dest, src, target); |
c2f47e15 | 3233 | } |
3234 | return NULL_RTX; | |
3235 | } | |
3236 | ||
3237 | /* Helper function to do the actual work for expand_builtin_strcpy. The | |
3238 | arguments to the builtin_strcpy call DEST and SRC are broken out | |
3239 | so that this can also be called without constructing an actual CALL_EXPR. | |
3240 | The other arguments and return value are the same as for | |
3241 | expand_builtin_strcpy. */ | |
3242 | ||
3243 | static rtx | |
a65c4d64 | 3244 | expand_builtin_strcpy_args (tree dest, tree src, rtx target) |
c2f47e15 | 3245 | { |
c2f47e15 | 3246 | return expand_movstr (dest, src, target, /*endp=*/0); |
53800dbe | 3247 | } |
3248 | ||
c2f47e15 | 3249 | /* Expand a call EXP to the stpcpy builtin. |
3250 | Return NULL_RTX if we failed the caller should emit a normal call, | |
3b824fa6 | 3251 | otherwise try to get the result in TARGET, if convenient (and in |
3252 | mode MODE if that's convenient). */ | |
3253 | ||
3254 | static rtx | |
3754d046 | 3255 | expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode) |
3b824fa6 | 3256 | { |
c2f47e15 | 3257 | tree dst, src; |
389dd41b | 3258 | location_t loc = EXPR_LOCATION (exp); |
c2f47e15 | 3259 | |
3260 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
3261 | return NULL_RTX; | |
3262 | ||
3263 | dst = CALL_EXPR_ARG (exp, 0); | |
3264 | src = CALL_EXPR_ARG (exp, 1); | |
3265 | ||
727c62dd | 3266 | /* If return value is ignored, transform stpcpy into strcpy. */ |
b9a16870 | 3267 | if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY)) |
978836e5 | 3268 | { |
b9a16870 | 3269 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); |
0568e9c1 | 3270 | tree result = build_call_nofold_loc (loc, fn, 2, dst, src); |
c8b17b2e | 3271 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
978836e5 | 3272 | } |
3b824fa6 | 3273 | else |
3274 | { | |
c2f47e15 | 3275 | tree len, lenp1; |
727c62dd | 3276 | rtx ret; |
647661c6 | 3277 | |
9fe0e1b8 | 3278 | /* Ensure we get an actual string whose length can be evaluated at |
a0c938f0 | 3279 | compile-time, not an expression containing a string. This is |
3280 | because the latter will potentially produce pessimized code | |
3281 | when used to produce the return value. */ | |
681fab1e | 3282 | if (! c_getstr (src) || ! (len = c_strlen (src, 0))) |
c2f47e15 | 3283 | return expand_movstr (dst, src, target, /*endp=*/2); |
3b824fa6 | 3284 | |
389dd41b | 3285 | lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1)); |
a65c4d64 | 3286 | ret = expand_builtin_mempcpy_args (dst, src, lenp1, |
f21337ef | 3287 | target, mode, /*endp=*/2, |
3288 | exp); | |
727c62dd | 3289 | |
3290 | if (ret) | |
3291 | return ret; | |
3292 | ||
3293 | if (TREE_CODE (len) == INTEGER_CST) | |
3294 | { | |
8ec3c5c2 | 3295 | rtx len_rtx = expand_normal (len); |
727c62dd | 3296 | |
971ba038 | 3297 | if (CONST_INT_P (len_rtx)) |
727c62dd | 3298 | { |
a65c4d64 | 3299 | ret = expand_builtin_strcpy_args (dst, src, target); |
727c62dd | 3300 | |
3301 | if (ret) | |
3302 | { | |
3303 | if (! target) | |
7ac87324 | 3304 | { |
3305 | if (mode != VOIDmode) | |
3306 | target = gen_reg_rtx (mode); | |
3307 | else | |
3308 | target = gen_reg_rtx (GET_MODE (ret)); | |
3309 | } | |
727c62dd | 3310 | if (GET_MODE (target) != GET_MODE (ret)) |
3311 | ret = gen_lowpart (GET_MODE (target), ret); | |
3312 | ||
29c05e22 | 3313 | ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx)); |
c5aba89c | 3314 | ret = emit_move_insn (target, force_operand (ret, NULL_RTX)); |
64db345d | 3315 | gcc_assert (ret); |
727c62dd | 3316 | |
3317 | return target; | |
3318 | } | |
3319 | } | |
3320 | } | |
3321 | ||
c2f47e15 | 3322 | return expand_movstr (dst, src, target, /*endp=*/2); |
3b824fa6 | 3323 | } |
3324 | } | |
3325 | ||
6840589f | 3326 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3327 | bytes from constant string DATA + OFFSET and return it as target | |
3328 | constant. */ | |
3329 | ||
09879952 | 3330 | rtx |
aecda0d6 | 3331 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
3754d046 | 3332 | machine_mode mode) |
6840589f | 3333 | { |
3334 | const char *str = (const char *) data; | |
3335 | ||
3336 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
3337 | return const0_rtx; | |
3338 | ||
3339 | return c_readstr (str + offset, mode); | |
3340 | } | |
3341 | ||
48e1416a | 3342 | /* Expand expression EXP, which is a call to the strncpy builtin. Return |
c2f47e15 | 3343 | NULL_RTX if we failed the caller should emit a normal call. */ |
ed09096d | 3344 | |
3345 | static rtx | |
a65c4d64 | 3346 | expand_builtin_strncpy (tree exp, rtx target) |
ed09096d | 3347 | { |
389dd41b | 3348 | location_t loc = EXPR_LOCATION (exp); |
c2f47e15 | 3349 | |
3350 | if (validate_arglist (exp, | |
3351 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 3352 | { |
c2f47e15 | 3353 | tree dest = CALL_EXPR_ARG (exp, 0); |
3354 | tree src = CALL_EXPR_ARG (exp, 1); | |
3355 | tree len = CALL_EXPR_ARG (exp, 2); | |
3356 | tree slen = c_strlen (src, 1); | |
6840589f | 3357 | |
8ff6a5cd | 3358 | /* We must be passed a constant len and src parameter. */ |
e913b5cd | 3359 | if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen)) |
c2f47e15 | 3360 | return NULL_RTX; |
ed09096d | 3361 | |
389dd41b | 3362 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); |
ed09096d | 3363 | |
3364 | /* We're required to pad with trailing zeros if the requested | |
a0c938f0 | 3365 | len is greater than strlen(s2)+1. In that case try to |
6840589f | 3366 | use store_by_pieces, if it fails, punt. */ |
ed09096d | 3367 | if (tree_int_cst_lt (slen, len)) |
6840589f | 3368 | { |
957d0361 | 3369 | unsigned int dest_align = get_pointer_alignment (dest); |
c2f47e15 | 3370 | const char *p = c_getstr (src); |
6840589f | 3371 | rtx dest_mem; |
3372 | ||
e913b5cd | 3373 | if (!p || dest_align == 0 || !tree_fits_uhwi_p (len) |
3374 | || !can_store_by_pieces (tree_to_uhwi (len), | |
6840589f | 3375 | builtin_strncpy_read_str, |
364c0c59 | 3376 | CONST_CAST (char *, p), |
3377 | dest_align, false)) | |
c2f47e15 | 3378 | return NULL_RTX; |
6840589f | 3379 | |
d8ae1baa | 3380 | dest_mem = get_memory_rtx (dest, len); |
e913b5cd | 3381 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
6840589f | 3382 | builtin_strncpy_read_str, |
364c0c59 | 3383 | CONST_CAST (char *, p), dest_align, false, 0); |
a65c4d64 | 3384 | dest_mem = force_operand (XEXP (dest_mem, 0), target); |
85d654dd | 3385 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 3386 | return dest_mem; |
6840589f | 3387 | } |
ed09096d | 3388 | } |
c2f47e15 | 3389 | return NULL_RTX; |
ed09096d | 3390 | } |
3391 | ||
ecc318ff | 3392 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3393 | bytes from constant string DATA + OFFSET and return it as target | |
3394 | constant. */ | |
3395 | ||
f656b751 | 3396 | rtx |
aecda0d6 | 3397 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3754d046 | 3398 | machine_mode mode) |
ecc318ff | 3399 | { |
3400 | const char *c = (const char *) data; | |
364c0c59 | 3401 | char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode)); |
ecc318ff | 3402 | |
3403 | memset (p, *c, GET_MODE_SIZE (mode)); | |
3404 | ||
3405 | return c_readstr (p, mode); | |
3406 | } | |
3407 | ||
a7ec6974 | 3408 | /* Callback routine for store_by_pieces. Return the RTL of a register |
3409 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
3410 | char value given in the RTL register data. For example, if mode is | |
3411 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
3412 | ||
3413 | static rtx | |
aecda0d6 | 3414 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3754d046 | 3415 | machine_mode mode) |
a7ec6974 | 3416 | { |
3417 | rtx target, coeff; | |
3418 | size_t size; | |
3419 | char *p; | |
3420 | ||
3421 | size = GET_MODE_SIZE (mode); | |
f0ce3b1f | 3422 | if (size == 1) |
3423 | return (rtx) data; | |
a7ec6974 | 3424 | |
364c0c59 | 3425 | p = XALLOCAVEC (char, size); |
a7ec6974 | 3426 | memset (p, 1, size); |
3427 | coeff = c_readstr (p, mode); | |
3428 | ||
f0ce3b1f | 3429 | target = convert_to_mode (mode, (rtx) data, 1); |
a7ec6974 | 3430 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
3431 | return force_reg (mode, target); | |
3432 | } | |
3433 | ||
48e1416a | 3434 | /* Expand expression EXP, which is a call to the memset builtin. Return |
3435 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
c2f47e15 | 3436 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
6f428e8b | 3437 | convenient). */ |
902de8ed | 3438 | |
53800dbe | 3439 | static rtx |
3754d046 | 3440 | expand_builtin_memset (tree exp, rtx target, machine_mode mode) |
53800dbe | 3441 | { |
c2f47e15 | 3442 | if (!validate_arglist (exp, |
3443 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3444 | return NULL_RTX; | |
53800dbe | 3445 | else |
3446 | { | |
c2f47e15 | 3447 | tree dest = CALL_EXPR_ARG (exp, 0); |
3448 | tree val = CALL_EXPR_ARG (exp, 1); | |
3449 | tree len = CALL_EXPR_ARG (exp, 2); | |
3450 | return expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
3451 | } | |
3452 | } | |
53800dbe | 3453 | |
f21337ef | 3454 | /* Expand expression EXP, which is an instrumented call to the memset builtin. |
3455 | Return NULL_RTX if we failed the caller should emit a normal call, otherwise | |
3456 | try to get the result in TARGET, if convenient (and in mode MODE if that's | |
3457 | convenient). */ | |
3458 | ||
3459 | static rtx | |
3460 | expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode) | |
3461 | { | |
3462 | if (!validate_arglist (exp, | |
3463 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3464 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3465 | return NULL_RTX; | |
3466 | else | |
3467 | { | |
3468 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3469 | tree val = CALL_EXPR_ARG (exp, 2); | |
3470 | tree len = CALL_EXPR_ARG (exp, 3); | |
3471 | rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
3472 | ||
3473 | /* Return src bounds with the result. */ | |
3474 | if (res) | |
3475 | { | |
17d388d8 | 3476 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
f21337ef | 3477 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3478 | res = chkp_join_splitted_slot (res, bnd); | |
3479 | } | |
3480 | return res; | |
3481 | } | |
3482 | } | |
3483 | ||
c2f47e15 | 3484 | /* Helper function to do the actual work for expand_builtin_memset. The |
3485 | arguments to the builtin_memset call DEST, VAL, and LEN are broken out | |
3486 | so that this can also be called without constructing an actual CALL_EXPR. | |
3487 | The other arguments and return value are the same as for | |
3488 | expand_builtin_memset. */ | |
6b961939 | 3489 | |
c2f47e15 | 3490 | static rtx |
3491 | expand_builtin_memset_args (tree dest, tree val, tree len, | |
3754d046 | 3492 | rtx target, machine_mode mode, tree orig_exp) |
c2f47e15 | 3493 | { |
3494 | tree fndecl, fn; | |
3495 | enum built_in_function fcode; | |
3754d046 | 3496 | machine_mode val_mode; |
c2f47e15 | 3497 | char c; |
3498 | unsigned int dest_align; | |
3499 | rtx dest_mem, dest_addr, len_rtx; | |
3500 | HOST_WIDE_INT expected_size = -1; | |
3501 | unsigned int expected_align = 0; | |
36d63243 | 3502 | unsigned HOST_WIDE_INT min_size; |
3503 | unsigned HOST_WIDE_INT max_size; | |
9db0f34d | 3504 | unsigned HOST_WIDE_INT probable_max_size; |
53800dbe | 3505 | |
957d0361 | 3506 | dest_align = get_pointer_alignment (dest); |
162719b3 | 3507 | |
c2f47e15 | 3508 | /* If DEST is not a pointer type, don't do this operation in-line. */ |
3509 | if (dest_align == 0) | |
3510 | return NULL_RTX; | |
6f428e8b | 3511 | |
8cee8dc0 | 3512 | if (currently_expanding_gimple_stmt) |
3513 | stringop_block_profile (currently_expanding_gimple_stmt, | |
3514 | &expected_align, &expected_size); | |
75a70cf9 | 3515 | |
c2f47e15 | 3516 | if (expected_align < dest_align) |
3517 | expected_align = dest_align; | |
6b961939 | 3518 | |
c2f47e15 | 3519 | /* If the LEN parameter is zero, return DEST. */ |
3520 | if (integer_zerop (len)) | |
3521 | { | |
3522 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
3523 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3524 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
3525 | } | |
7a3e5564 | 3526 | |
c2f47e15 | 3527 | /* Stabilize the arguments in case we fail. */ |
3528 | dest = builtin_save_expr (dest); | |
3529 | val = builtin_save_expr (val); | |
3530 | len = builtin_save_expr (len); | |
a7ec6974 | 3531 | |
c2f47e15 | 3532 | len_rtx = expand_normal (len); |
9db0f34d | 3533 | determine_block_size (len, len_rtx, &min_size, &max_size, |
3534 | &probable_max_size); | |
c2f47e15 | 3535 | dest_mem = get_memory_rtx (dest, len); |
03a5dda9 | 3536 | val_mode = TYPE_MODE (unsigned_char_type_node); |
a7ec6974 | 3537 | |
c2f47e15 | 3538 | if (TREE_CODE (val) != INTEGER_CST) |
3539 | { | |
3540 | rtx val_rtx; | |
a7ec6974 | 3541 | |
c2f47e15 | 3542 | val_rtx = expand_normal (val); |
03a5dda9 | 3543 | val_rtx = convert_to_mode (val_mode, val_rtx, 0); |
53800dbe | 3544 | |
c2f47e15 | 3545 | /* Assume that we can memset by pieces if we can store |
3546 | * the coefficients by pieces (in the required modes). | |
3547 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
3548 | c = 1; | |
e913b5cd | 3549 | if (tree_fits_uhwi_p (len) |
3550 | && can_store_by_pieces (tree_to_uhwi (len), | |
4b297e2e | 3551 | builtin_memset_read_str, &c, dest_align, |
3552 | true)) | |
c2f47e15 | 3553 | { |
03a5dda9 | 3554 | val_rtx = force_reg (val_mode, val_rtx); |
e913b5cd | 3555 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
4b297e2e | 3556 | builtin_memset_gen_str, val_rtx, dest_align, |
3557 | true, 0); | |
c2f47e15 | 3558 | } |
3559 | else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx, | |
3560 | dest_align, expected_align, | |
9db0f34d | 3561 | expected_size, min_size, max_size, |
3562 | probable_max_size)) | |
6b961939 | 3563 | goto do_libcall; |
48e1416a | 3564 | |
c2f47e15 | 3565 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3566 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3567 | return dest_mem; | |
3568 | } | |
53800dbe | 3569 | |
c2f47e15 | 3570 | if (target_char_cast (val, &c)) |
3571 | goto do_libcall; | |
ecc318ff | 3572 | |
c2f47e15 | 3573 | if (c) |
3574 | { | |
e913b5cd | 3575 | if (tree_fits_uhwi_p (len) |
3576 | && can_store_by_pieces (tree_to_uhwi (len), | |
4b297e2e | 3577 | builtin_memset_read_str, &c, dest_align, |
3578 | true)) | |
e913b5cd | 3579 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
4b297e2e | 3580 | builtin_memset_read_str, &c, dest_align, true, 0); |
03a5dda9 | 3581 | else if (!set_storage_via_setmem (dest_mem, len_rtx, |
3582 | gen_int_mode (c, val_mode), | |
c2f47e15 | 3583 | dest_align, expected_align, |
9db0f34d | 3584 | expected_size, min_size, max_size, |
3585 | probable_max_size)) | |
c2f47e15 | 3586 | goto do_libcall; |
48e1416a | 3587 | |
c2f47e15 | 3588 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3589 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3590 | return dest_mem; | |
3591 | } | |
ecc318ff | 3592 | |
c2f47e15 | 3593 | set_mem_align (dest_mem, dest_align); |
3594 | dest_addr = clear_storage_hints (dest_mem, len_rtx, | |
3595 | CALL_EXPR_TAILCALL (orig_exp) | |
3596 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
36d63243 | 3597 | expected_align, expected_size, |
9db0f34d | 3598 | min_size, max_size, |
3599 | probable_max_size); | |
53800dbe | 3600 | |
c2f47e15 | 3601 | if (dest_addr == 0) |
3602 | { | |
3603 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3604 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
3605 | } | |
53800dbe | 3606 | |
c2f47e15 | 3607 | return dest_addr; |
6b961939 | 3608 | |
c2f47e15 | 3609 | do_libcall: |
3610 | fndecl = get_callee_fndecl (orig_exp); | |
3611 | fcode = DECL_FUNCTION_CODE (fndecl); | |
f21337ef | 3612 | if (fcode == BUILT_IN_MEMSET |
3613 | || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP) | |
0568e9c1 | 3614 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3, |
3615 | dest, val, len); | |
c2f47e15 | 3616 | else if (fcode == BUILT_IN_BZERO) |
0568e9c1 | 3617 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2, |
3618 | dest, len); | |
c2f47e15 | 3619 | else |
3620 | gcc_unreachable (); | |
a65c4d64 | 3621 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3622 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp); | |
c2f47e15 | 3623 | return expand_call (fn, target, target == const0_rtx); |
53800dbe | 3624 | } |
3625 | ||
48e1416a | 3626 | /* Expand expression EXP, which is a call to the bzero builtin. Return |
c2f47e15 | 3627 | NULL_RTX if we failed the caller should emit a normal call. */ |
27d0c333 | 3628 | |
ffc83088 | 3629 | static rtx |
0b25db21 | 3630 | expand_builtin_bzero (tree exp) |
ffc83088 | 3631 | { |
c2f47e15 | 3632 | tree dest, size; |
389dd41b | 3633 | location_t loc = EXPR_LOCATION (exp); |
ffc83088 | 3634 | |
c2f47e15 | 3635 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
7369e7ba | 3636 | return NULL_RTX; |
ffc83088 | 3637 | |
c2f47e15 | 3638 | dest = CALL_EXPR_ARG (exp, 0); |
3639 | size = CALL_EXPR_ARG (exp, 1); | |
bf8e3599 | 3640 | |
7369e7ba | 3641 | /* New argument list transforming bzero(ptr x, int y) to |
6f428e8b | 3642 | memset(ptr x, int 0, size_t y). This is done this way |
3643 | so that if it isn't expanded inline, we fallback to | |
3644 | calling bzero instead of memset. */ | |
bf8e3599 | 3645 | |
c2f47e15 | 3646 | return expand_builtin_memset_args (dest, integer_zero_node, |
a0553bff | 3647 | fold_convert_loc (loc, |
3648 | size_type_node, size), | |
c2f47e15 | 3649 | const0_rtx, VOIDmode, exp); |
ffc83088 | 3650 | } |
3651 | ||
d6f01a40 | 3652 | /* Try to expand cmpstr operation ICODE with the given operands. |
3653 | Return the result rtx on success, otherwise return null. */ | |
3654 | ||
3655 | static rtx | |
3656 | expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx, | |
3657 | HOST_WIDE_INT align) | |
3658 | { | |
3659 | machine_mode insn_mode = insn_data[icode].operand[0].mode; | |
3660 | ||
3661 | if (target && (!REG_P (target) || HARD_REGISTER_P (target))) | |
3662 | target = NULL_RTX; | |
3663 | ||
3664 | struct expand_operand ops[4]; | |
3665 | create_output_operand (&ops[0], target, insn_mode); | |
3666 | create_fixed_operand (&ops[1], arg1_rtx); | |
3667 | create_fixed_operand (&ops[2], arg2_rtx); | |
3668 | create_integer_operand (&ops[3], align); | |
3669 | if (maybe_expand_insn (icode, 4, ops)) | |
3670 | return ops[0].value; | |
3671 | return NULL_RTX; | |
3672 | } | |
3673 | ||
ea368aac | 3674 | /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands. |
d6f01a40 | 3675 | ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success, |
3676 | otherwise return null. */ | |
3677 | ||
3678 | static rtx | |
ea368aac | 3679 | expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, |
3680 | rtx arg2_rtx, tree arg3_type, rtx arg3_rtx, | |
3681 | HOST_WIDE_INT align) | |
d6f01a40 | 3682 | { |
3683 | machine_mode insn_mode = insn_data[icode].operand[0].mode; | |
3684 | ||
3685 | if (target && (!REG_P (target) || HARD_REGISTER_P (target))) | |
3686 | target = NULL_RTX; | |
3687 | ||
3688 | struct expand_operand ops[5]; | |
3689 | create_output_operand (&ops[0], target, insn_mode); | |
3690 | create_fixed_operand (&ops[1], arg1_rtx); | |
3691 | create_fixed_operand (&ops[2], arg2_rtx); | |
3692 | create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type), | |
3693 | TYPE_UNSIGNED (arg3_type)); | |
3694 | create_integer_operand (&ops[4], align); | |
3695 | if (maybe_expand_insn (icode, 5, ops)) | |
3696 | return ops[0].value; | |
3697 | return NULL_RTX; | |
3698 | } | |
3699 | ||
7a3f89b5 | 3700 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
bd021c1c | 3701 | Return NULL_RTX if we failed and the caller should emit a normal call, |
ea368aac | 3702 | otherwise try to get the result in TARGET, if convenient. */ |
27d0c333 | 3703 | |
53800dbe | 3704 | static rtx |
ea368aac | 3705 | expand_builtin_memcmp (tree exp, rtx target) |
53800dbe | 3706 | { |
c2f47e15 | 3707 | if (!validate_arglist (exp, |
3708 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3709 | return NULL_RTX; | |
6f428e8b | 3710 | |
bd021c1c | 3711 | /* Note: The cmpstrnsi pattern, if it exists, is not suitable for |
3712 | implementing memcmp because it will stop if it encounters two | |
3713 | zero bytes. */ | |
ea368aac | 3714 | insn_code icode = direct_optab_handler (cmpmem_optab, SImode); |
3715 | if (icode == CODE_FOR_nothing) | |
3716 | return NULL_RTX; | |
53800dbe | 3717 | |
ea368aac | 3718 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3719 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
3720 | tree len = CALL_EXPR_ARG (exp, 2); | |
b428c0a5 | 3721 | |
ea368aac | 3722 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3723 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
53800dbe | 3724 | |
ea368aac | 3725 | /* If we don't have POINTER_TYPE, call the function. */ |
3726 | if (arg1_align == 0 || arg2_align == 0) | |
3727 | return NULL_RTX; | |
53800dbe | 3728 | |
ea368aac | 3729 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
3730 | location_t loc = EXPR_LOCATION (exp); | |
3731 | rtx arg1_rtx = get_memory_rtx (arg1, len); | |
3732 | rtx arg2_rtx = get_memory_rtx (arg2, len); | |
3733 | rtx arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len)); | |
53800dbe | 3734 | |
ea368aac | 3735 | /* Set MEM_SIZE as appropriate. */ |
3736 | if (CONST_INT_P (arg3_rtx)) | |
3737 | { | |
3738 | set_mem_size (arg1_rtx, INTVAL (arg3_rtx)); | |
3739 | set_mem_size (arg2_rtx, INTVAL (arg3_rtx)); | |
3740 | } | |
83f88f8e | 3741 | |
ea368aac | 3742 | rtx result = expand_cmpstrn_or_cmpmem (icode, target, arg1_rtx, arg2_rtx, |
3743 | TREE_TYPE (len), arg3_rtx, | |
3744 | MIN (arg1_align, arg2_align)); | |
3745 | if (result) | |
3746 | { | |
3747 | /* Return the value in the proper mode for this function. */ | |
3748 | if (GET_MODE (result) == mode) | |
3749 | return result; | |
83f88f8e | 3750 | |
ea368aac | 3751 | if (target != 0) |
3752 | { | |
3753 | convert_move (target, result, 0); | |
3754 | return target; | |
3755 | } | |
0cd832f0 | 3756 | |
53800dbe | 3757 | return convert_to_mode (mode, result, 0); |
ea368aac | 3758 | } |
53800dbe | 3759 | |
ea368aac | 3760 | result = target; |
3761 | if (! (result != 0 | |
3762 | && REG_P (result) && GET_MODE (result) == mode | |
3763 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3764 | result = gen_reg_rtx (mode); | |
3765 | ||
3766 | emit_library_call_value (memcmp_libfunc, result, LCT_PURE, | |
3767 | TYPE_MODE (integer_type_node), 3, | |
3768 | XEXP (arg1_rtx, 0), Pmode, | |
3769 | XEXP (arg2_rtx, 0), Pmode, | |
3770 | convert_to_mode (TYPE_MODE (sizetype), arg3_rtx, | |
3771 | TYPE_UNSIGNED (sizetype)), | |
3772 | TYPE_MODE (sizetype)); | |
3773 | return result; | |
6f428e8b | 3774 | } |
3775 | ||
c2f47e15 | 3776 | /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX |
53800dbe | 3777 | if we failed the caller should emit a normal call, otherwise try to get |
3778 | the result in TARGET, if convenient. */ | |
902de8ed | 3779 | |
53800dbe | 3780 | static rtx |
a65c4d64 | 3781 | expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target) |
53800dbe | 3782 | { |
c2f47e15 | 3783 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3784 | return NULL_RTX; | |
bf8e3599 | 3785 | |
d6f01a40 | 3786 | insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode); |
3787 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); | |
3788 | if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing) | |
6ac5504b | 3789 | { |
3790 | rtx arg1_rtx, arg2_rtx; | |
6ac5504b | 3791 | tree fndecl, fn; |
c2f47e15 | 3792 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3793 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
d6f01a40 | 3794 | rtx result = NULL_RTX; |
a0c938f0 | 3795 | |
957d0361 | 3796 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3797 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
6ac5504b | 3798 | |
3799 | /* If we don't have POINTER_TYPE, call the function. */ | |
3800 | if (arg1_align == 0 || arg2_align == 0) | |
c2f47e15 | 3801 | return NULL_RTX; |
7a3f89b5 | 3802 | |
6ac5504b | 3803 | /* Stabilize the arguments in case gen_cmpstr(n)si fail. */ |
3804 | arg1 = builtin_save_expr (arg1); | |
3805 | arg2 = builtin_save_expr (arg2); | |
7a3f89b5 | 3806 | |
d8ae1baa | 3807 | arg1_rtx = get_memory_rtx (arg1, NULL); |
3808 | arg2_rtx = get_memory_rtx (arg2, NULL); | |
53800dbe | 3809 | |
6ac5504b | 3810 | /* Try to call cmpstrsi. */ |
d6f01a40 | 3811 | if (cmpstr_icode != CODE_FOR_nothing) |
3812 | result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx, | |
3813 | MIN (arg1_align, arg2_align)); | |
3814 | ||
6ac5504b | 3815 | /* Try to determine at least one length and call cmpstrnsi. */ |
d6f01a40 | 3816 | if (!result && cmpstrn_icode != CODE_FOR_nothing) |
6ac5504b | 3817 | { |
3818 | tree len; | |
3819 | rtx arg3_rtx; | |
3820 | ||
6ac5504b | 3821 | tree len1 = c_strlen (arg1, 1); |
3822 | tree len2 = c_strlen (arg2, 1); | |
3823 | ||
3824 | if (len1) | |
3825 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3826 | if (len2) | |
3827 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3828 | ||
3829 | /* If we don't have a constant length for the first, use the length | |
3830 | of the second, if we know it. We don't require a constant for | |
3831 | this case; some cost analysis could be done if both are available | |
3832 | but neither is constant. For now, assume they're equally cheap, | |
3833 | unless one has side effects. If both strings have constant lengths, | |
3834 | use the smaller. */ | |
3835 | ||
3836 | if (!len1) | |
3837 | len = len2; | |
3838 | else if (!len2) | |
3839 | len = len1; | |
3840 | else if (TREE_SIDE_EFFECTS (len1)) | |
3841 | len = len2; | |
3842 | else if (TREE_SIDE_EFFECTS (len2)) | |
3843 | len = len1; | |
3844 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3845 | len = len2; | |
3846 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3847 | len = len1; | |
3848 | else if (tree_int_cst_lt (len1, len2)) | |
3849 | len = len1; | |
3850 | else | |
3851 | len = len2; | |
3852 | ||
3853 | /* If both arguments have side effects, we cannot optimize. */ | |
d6f01a40 | 3854 | if (len && !TREE_SIDE_EFFECTS (len)) |
3855 | { | |
3856 | arg3_rtx = expand_normal (len); | |
ea368aac | 3857 | result = expand_cmpstrn_or_cmpmem |
3858 | (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len), | |
3859 | arg3_rtx, MIN (arg1_align, arg2_align)); | |
d6f01a40 | 3860 | } |
6ac5504b | 3861 | } |
3f8aefe2 | 3862 | |
d6f01a40 | 3863 | if (result) |
6ac5504b | 3864 | { |
6ac5504b | 3865 | /* Return the value in the proper mode for this function. */ |
d6f01a40 | 3866 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
6ac5504b | 3867 | if (GET_MODE (result) == mode) |
3868 | return result; | |
3869 | if (target == 0) | |
3870 | return convert_to_mode (mode, result, 0); | |
3871 | convert_move (target, result, 0); | |
3872 | return target; | |
3873 | } | |
902de8ed | 3874 | |
6ac5504b | 3875 | /* Expand the library call ourselves using a stabilized argument |
3876 | list to avoid re-evaluating the function's arguments twice. */ | |
6ac5504b | 3877 | fndecl = get_callee_fndecl (exp); |
0568e9c1 | 3878 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2); |
a65c4d64 | 3879 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3880 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
6ac5504b | 3881 | return expand_call (fn, target, target == const0_rtx); |
3882 | } | |
c2f47e15 | 3883 | return NULL_RTX; |
83d79705 | 3884 | } |
53800dbe | 3885 | |
48e1416a | 3886 | /* Expand expression EXP, which is a call to the strncmp builtin. Return |
c2f47e15 | 3887 | NULL_RTX if we failed the caller should emit a normal call, otherwise try to get |
ed09096d | 3888 | the result in TARGET, if convenient. */ |
27d0c333 | 3889 | |
ed09096d | 3890 | static rtx |
a65c4d64 | 3891 | expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target, |
3754d046 | 3892 | ATTRIBUTE_UNUSED machine_mode mode) |
ed09096d | 3893 | { |
a65c4d64 | 3894 | location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp); |
389dd41b | 3895 | |
c2f47e15 | 3896 | if (!validate_arglist (exp, |
3897 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3898 | return NULL_RTX; | |
ed09096d | 3899 | |
6e34e617 | 3900 | /* If c_strlen can determine an expression for one of the string |
6ac5504b | 3901 | lengths, and it doesn't have side effects, then emit cmpstrnsi |
7a3f89b5 | 3902 | using length MIN(strlen(string)+1, arg3). */ |
d6f01a40 | 3903 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); |
3904 | if (cmpstrn_icode != CODE_FOR_nothing) | |
7a3f89b5 | 3905 | { |
3906 | tree len, len1, len2; | |
3907 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
d6f01a40 | 3908 | rtx result; |
0b25db21 | 3909 | tree fndecl, fn; |
c2f47e15 | 3910 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3911 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
3912 | tree arg3 = CALL_EXPR_ARG (exp, 2); | |
6f428e8b | 3913 | |
957d0361 | 3914 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3915 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
bf8e3599 | 3916 | |
681fab1e | 3917 | len1 = c_strlen (arg1, 1); |
3918 | len2 = c_strlen (arg2, 1); | |
7a3f89b5 | 3919 | |
3920 | if (len1) | |
389dd41b | 3921 | len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1); |
7a3f89b5 | 3922 | if (len2) |
389dd41b | 3923 | len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2); |
7a3f89b5 | 3924 | |
3925 | /* If we don't have a constant length for the first, use the length | |
3926 | of the second, if we know it. We don't require a constant for | |
3927 | this case; some cost analysis could be done if both are available | |
3928 | but neither is constant. For now, assume they're equally cheap, | |
3929 | unless one has side effects. If both strings have constant lengths, | |
3930 | use the smaller. */ | |
3931 | ||
3932 | if (!len1) | |
3933 | len = len2; | |
3934 | else if (!len2) | |
3935 | len = len1; | |
3936 | else if (TREE_SIDE_EFFECTS (len1)) | |
3937 | len = len2; | |
3938 | else if (TREE_SIDE_EFFECTS (len2)) | |
3939 | len = len1; | |
3940 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3941 | len = len2; | |
3942 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3943 | len = len1; | |
3944 | else if (tree_int_cst_lt (len1, len2)) | |
3945 | len = len1; | |
3946 | else | |
3947 | len = len2; | |
6e34e617 | 3948 | |
7a3f89b5 | 3949 | /* If both arguments have side effects, we cannot optimize. */ |
3950 | if (!len || TREE_SIDE_EFFECTS (len)) | |
c2f47e15 | 3951 | return NULL_RTX; |
bf8e3599 | 3952 | |
7a3f89b5 | 3953 | /* The actual new length parameter is MIN(len,arg3). */ |
389dd41b | 3954 | len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, |
3955 | fold_convert_loc (loc, TREE_TYPE (len), arg3)); | |
7a3f89b5 | 3956 | |
3957 | /* If we don't have POINTER_TYPE, call the function. */ | |
3958 | if (arg1_align == 0 || arg2_align == 0) | |
c2f47e15 | 3959 | return NULL_RTX; |
7a3f89b5 | 3960 | |
a65c4d64 | 3961 | /* Stabilize the arguments in case gen_cmpstrnsi fails. */ |
3962 | arg1 = builtin_save_expr (arg1); | |
3963 | arg2 = builtin_save_expr (arg2); | |
3964 | len = builtin_save_expr (len); | |
27d0c333 | 3965 | |
a65c4d64 | 3966 | arg1_rtx = get_memory_rtx (arg1, len); |
3967 | arg2_rtx = get_memory_rtx (arg2, len); | |
3968 | arg3_rtx = expand_normal (len); | |
ea368aac | 3969 | result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx, |
3970 | arg2_rtx, TREE_TYPE (len), arg3_rtx, | |
3971 | MIN (arg1_align, arg2_align)); | |
d6f01a40 | 3972 | if (result) |
a65c4d64 | 3973 | { |
a65c4d64 | 3974 | /* Return the value in the proper mode for this function. */ |
3975 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3976 | if (GET_MODE (result) == mode) | |
3977 | return result; | |
3978 | if (target == 0) | |
3979 | return convert_to_mode (mode, result, 0); | |
3980 | convert_move (target, result, 0); | |
3981 | return target; | |
3982 | } | |
27d0c333 | 3983 | |
a65c4d64 | 3984 | /* Expand the library call ourselves using a stabilized argument |
3985 | list to avoid re-evaluating the function's arguments twice. */ | |
3986 | fndecl = get_callee_fndecl (exp); | |
0568e9c1 | 3987 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3, |
3988 | arg1, arg2, len); | |
a65c4d64 | 3989 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3990 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
3991 | return expand_call (fn, target, target == const0_rtx); | |
3992 | } | |
c2f47e15 | 3993 | return NULL_RTX; |
49f0327b | 3994 | } |
3995 | ||
a66c9326 | 3996 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
3997 | if that's convenient. */ | |
902de8ed | 3998 | |
a66c9326 | 3999 | rtx |
aecda0d6 | 4000 | expand_builtin_saveregs (void) |
53800dbe | 4001 | { |
1e0c0b35 | 4002 | rtx val; |
4003 | rtx_insn *seq; | |
53800dbe | 4004 | |
4005 | /* Don't do __builtin_saveregs more than once in a function. | |
4006 | Save the result of the first call and reuse it. */ | |
4007 | if (saveregs_value != 0) | |
4008 | return saveregs_value; | |
53800dbe | 4009 | |
a66c9326 | 4010 | /* When this function is called, it means that registers must be |
4011 | saved on entry to this function. So we migrate the call to the | |
4012 | first insn of this function. */ | |
4013 | ||
4014 | start_sequence (); | |
53800dbe | 4015 | |
a66c9326 | 4016 | /* Do whatever the machine needs done in this case. */ |
45550790 | 4017 | val = targetm.calls.expand_builtin_saveregs (); |
53800dbe | 4018 | |
a66c9326 | 4019 | seq = get_insns (); |
4020 | end_sequence (); | |
53800dbe | 4021 | |
a66c9326 | 4022 | saveregs_value = val; |
53800dbe | 4023 | |
31d3e01c | 4024 | /* Put the insns after the NOTE that starts the function. If this |
4025 | is inside a start_sequence, make the outer-level insn chain current, so | |
a66c9326 | 4026 | the code is placed at the start of the function. */ |
4027 | push_topmost_sequence (); | |
0ec80471 | 4028 | emit_insn_after (seq, entry_of_function ()); |
a66c9326 | 4029 | pop_topmost_sequence (); |
4030 | ||
4031 | return val; | |
53800dbe | 4032 | } |
4033 | ||
79012a9d | 4034 | /* Expand a call to __builtin_next_arg. */ |
27d0c333 | 4035 | |
53800dbe | 4036 | static rtx |
79012a9d | 4037 | expand_builtin_next_arg (void) |
53800dbe | 4038 | { |
79012a9d | 4039 | /* Checking arguments is already done in fold_builtin_next_arg |
4040 | that must be called before this function. */ | |
940ddc5c | 4041 | return expand_binop (ptr_mode, add_optab, |
abe32cce | 4042 | crtl->args.internal_arg_pointer, |
4043 | crtl->args.arg_offset_rtx, | |
53800dbe | 4044 | NULL_RTX, 0, OPTAB_LIB_WIDEN); |
4045 | } | |
4046 | ||
a66c9326 | 4047 | /* Make it easier for the backends by protecting the valist argument |
4048 | from multiple evaluations. */ | |
4049 | ||
4050 | static tree | |
389dd41b | 4051 | stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue) |
a66c9326 | 4052 | { |
5f57a8b1 | 4053 | tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist)); |
4054 | ||
182cf5a9 | 4055 | /* The current way of determining the type of valist is completely |
4056 | bogus. We should have the information on the va builtin instead. */ | |
4057 | if (!vatype) | |
4058 | vatype = targetm.fn_abi_va_list (cfun->decl); | |
5f57a8b1 | 4059 | |
4060 | if (TREE_CODE (vatype) == ARRAY_TYPE) | |
a66c9326 | 4061 | { |
2d47cc32 | 4062 | if (TREE_SIDE_EFFECTS (valist)) |
4063 | valist = save_expr (valist); | |
11a61dea | 4064 | |
2d47cc32 | 4065 | /* For this case, the backends will be expecting a pointer to |
5f57a8b1 | 4066 | vatype, but it's possible we've actually been given an array |
4067 | (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)). | |
2d47cc32 | 4068 | So fix it. */ |
4069 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
8a15c04a | 4070 | { |
5f57a8b1 | 4071 | tree p1 = build_pointer_type (TREE_TYPE (vatype)); |
389dd41b | 4072 | valist = build_fold_addr_expr_with_type_loc (loc, valist, p1); |
8a15c04a | 4073 | } |
a66c9326 | 4074 | } |
11a61dea | 4075 | else |
a66c9326 | 4076 | { |
182cf5a9 | 4077 | tree pt = build_pointer_type (vatype); |
11a61dea | 4078 | |
2d47cc32 | 4079 | if (! needs_lvalue) |
4080 | { | |
11a61dea | 4081 | if (! TREE_SIDE_EFFECTS (valist)) |
4082 | return valist; | |
bf8e3599 | 4083 | |
389dd41b | 4084 | valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist); |
a66c9326 | 4085 | TREE_SIDE_EFFECTS (valist) = 1; |
a66c9326 | 4086 | } |
2d47cc32 | 4087 | |
11a61dea | 4088 | if (TREE_SIDE_EFFECTS (valist)) |
2d47cc32 | 4089 | valist = save_expr (valist); |
182cf5a9 | 4090 | valist = fold_build2_loc (loc, MEM_REF, |
4091 | vatype, valist, build_int_cst (pt, 0)); | |
a66c9326 | 4092 | } |
4093 | ||
4094 | return valist; | |
4095 | } | |
4096 | ||
2e15d750 | 4097 | /* The "standard" definition of va_list is void*. */ |
4098 | ||
4099 | tree | |
4100 | std_build_builtin_va_list (void) | |
4101 | { | |
4102 | return ptr_type_node; | |
4103 | } | |
4104 | ||
5f57a8b1 | 4105 | /* The "standard" abi va_list is va_list_type_node. */ |
4106 | ||
4107 | tree | |
4108 | std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED) | |
4109 | { | |
4110 | return va_list_type_node; | |
4111 | } | |
4112 | ||
4113 | /* The "standard" type of va_list is va_list_type_node. */ | |
4114 | ||
4115 | tree | |
4116 | std_canonical_va_list_type (tree type) | |
4117 | { | |
4118 | tree wtype, htype; | |
4119 | ||
4120 | if (INDIRECT_REF_P (type)) | |
4121 | type = TREE_TYPE (type); | |
9af5ce0c | 4122 | else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type))) |
5f57a8b1 | 4123 | type = TREE_TYPE (type); |
5f57a8b1 | 4124 | wtype = va_list_type_node; |
4125 | htype = type; | |
7b36f9ab | 4126 | /* Treat structure va_list types. */ |
4127 | if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype)) | |
4128 | htype = TREE_TYPE (htype); | |
4129 | else if (TREE_CODE (wtype) == ARRAY_TYPE) | |
5f57a8b1 | 4130 | { |
4131 | /* If va_list is an array type, the argument may have decayed | |
4132 | to a pointer type, e.g. by being passed to another function. | |
4133 | In that case, unwrap both types so that we can compare the | |
4134 | underlying records. */ | |
4135 | if (TREE_CODE (htype) == ARRAY_TYPE | |
4136 | || POINTER_TYPE_P (htype)) | |
4137 | { | |
4138 | wtype = TREE_TYPE (wtype); | |
4139 | htype = TREE_TYPE (htype); | |
4140 | } | |
4141 | } | |
4142 | if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype)) | |
4143 | return va_list_type_node; | |
4144 | ||
4145 | return NULL_TREE; | |
4146 | } | |
4147 | ||
a66c9326 | 4148 | /* The "standard" implementation of va_start: just assign `nextarg' to |
4149 | the variable. */ | |
27d0c333 | 4150 | |
a66c9326 | 4151 | void |
aecda0d6 | 4152 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
a66c9326 | 4153 | { |
f03c17bc | 4154 | rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE); |
4155 | convert_move (va_r, nextarg, 0); | |
058a1b7a | 4156 | |
4157 | /* We do not have any valid bounds for the pointer, so | |
4158 | just store zero bounds for it. */ | |
4159 | if (chkp_function_instrumented_p (current_function_decl)) | |
4160 | chkp_expand_bounds_reset_for_mem (valist, | |
4161 | make_tree (TREE_TYPE (valist), | |
4162 | nextarg)); | |
a66c9326 | 4163 | } |
4164 | ||
c2f47e15 | 4165 | /* Expand EXP, a call to __builtin_va_start. */ |
27d0c333 | 4166 | |
a66c9326 | 4167 | static rtx |
c2f47e15 | 4168 | expand_builtin_va_start (tree exp) |
a66c9326 | 4169 | { |
4170 | rtx nextarg; | |
c2f47e15 | 4171 | tree valist; |
389dd41b | 4172 | location_t loc = EXPR_LOCATION (exp); |
a66c9326 | 4173 | |
c2f47e15 | 4174 | if (call_expr_nargs (exp) < 2) |
cb166087 | 4175 | { |
389dd41b | 4176 | error_at (loc, "too few arguments to function %<va_start%>"); |
cb166087 | 4177 | return const0_rtx; |
4178 | } | |
a66c9326 | 4179 | |
c2f47e15 | 4180 | if (fold_builtin_next_arg (exp, true)) |
79012a9d | 4181 | return const0_rtx; |
7c2f0500 | 4182 | |
79012a9d | 4183 | nextarg = expand_builtin_next_arg (); |
389dd41b | 4184 | valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1); |
a66c9326 | 4185 | |
8a58ed0a | 4186 | if (targetm.expand_builtin_va_start) |
4187 | targetm.expand_builtin_va_start (valist, nextarg); | |
4188 | else | |
4189 | std_expand_builtin_va_start (valist, nextarg); | |
a66c9326 | 4190 | |
4191 | return const0_rtx; | |
4192 | } | |
4193 | ||
c2f47e15 | 4194 | /* Expand EXP, a call to __builtin_va_end. */ |
f7c44134 | 4195 | |
a66c9326 | 4196 | static rtx |
c2f47e15 | 4197 | expand_builtin_va_end (tree exp) |
a66c9326 | 4198 | { |
c2f47e15 | 4199 | tree valist = CALL_EXPR_ARG (exp, 0); |
8a15c04a | 4200 | |
8a15c04a | 4201 | /* Evaluate for side effects, if needed. I hate macros that don't |
4202 | do that. */ | |
4203 | if (TREE_SIDE_EFFECTS (valist)) | |
4204 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
a66c9326 | 4205 | |
4206 | return const0_rtx; | |
4207 | } | |
4208 | ||
c2f47e15 | 4209 | /* Expand EXP, a call to __builtin_va_copy. We do this as a |
a66c9326 | 4210 | builtin rather than just as an assignment in stdarg.h because of the |
4211 | nastiness of array-type va_list types. */ | |
f7c44134 | 4212 | |
a66c9326 | 4213 | static rtx |
c2f47e15 | 4214 | expand_builtin_va_copy (tree exp) |
a66c9326 | 4215 | { |
4216 | tree dst, src, t; | |
389dd41b | 4217 | location_t loc = EXPR_LOCATION (exp); |
a66c9326 | 4218 | |
c2f47e15 | 4219 | dst = CALL_EXPR_ARG (exp, 0); |
4220 | src = CALL_EXPR_ARG (exp, 1); | |
a66c9326 | 4221 | |
389dd41b | 4222 | dst = stabilize_va_list_loc (loc, dst, 1); |
4223 | src = stabilize_va_list_loc (loc, src, 0); | |
a66c9326 | 4224 | |
5f57a8b1 | 4225 | gcc_assert (cfun != NULL && cfun->decl != NULL_TREE); |
4226 | ||
4227 | if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE) | |
a66c9326 | 4228 | { |
5f57a8b1 | 4229 | t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src); |
a66c9326 | 4230 | TREE_SIDE_EFFECTS (t) = 1; |
4231 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4232 | } | |
4233 | else | |
4234 | { | |
11a61dea | 4235 | rtx dstb, srcb, size; |
4236 | ||
4237 | /* Evaluate to pointers. */ | |
4238 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4239 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
5f57a8b1 | 4240 | size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)), |
4241 | NULL_RTX, VOIDmode, EXPAND_NORMAL); | |
11a61dea | 4242 | |
85d654dd | 4243 | dstb = convert_memory_address (Pmode, dstb); |
4244 | srcb = convert_memory_address (Pmode, srcb); | |
726ec87c | 4245 | |
11a61dea | 4246 | /* "Dereference" to BLKmode memories. */ |
4247 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ab6ab77e | 4248 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
5f57a8b1 | 4249 | set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
11a61dea | 4250 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ab6ab77e | 4251 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
5f57a8b1 | 4252 | set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
11a61dea | 4253 | |
4254 | /* Copy. */ | |
0378dbdc | 4255 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
a66c9326 | 4256 | } |
4257 | ||
4258 | return const0_rtx; | |
4259 | } | |
4260 | ||
53800dbe | 4261 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
4262 | __builtin_return_address. */ | |
27d0c333 | 4263 | |
53800dbe | 4264 | static rtx |
c2f47e15 | 4265 | expand_builtin_frame_address (tree fndecl, tree exp) |
53800dbe | 4266 | { |
53800dbe | 4267 | /* The argument must be a nonnegative integer constant. |
4268 | It counts the number of frames to scan up the stack. | |
5b252e95 | 4269 | The value is either the frame pointer value or the return |
4270 | address saved in that frame. */ | |
c2f47e15 | 4271 | if (call_expr_nargs (exp) == 0) |
53800dbe | 4272 | /* Warning about missing arg was already issued. */ |
4273 | return const0_rtx; | |
e913b5cd | 4274 | else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0))) |
53800dbe | 4275 | { |
5b252e95 | 4276 | error ("invalid argument to %qD", fndecl); |
53800dbe | 4277 | return const0_rtx; |
4278 | } | |
4279 | else | |
4280 | { | |
5b252e95 | 4281 | /* Number of frames to scan up the stack. */ |
4282 | unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0)); | |
4283 | ||
4284 | rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count); | |
53800dbe | 4285 | |
4286 | /* Some ports cannot access arbitrary stack frames. */ | |
4287 | if (tem == NULL) | |
4288 | { | |
5b252e95 | 4289 | warning (0, "unsupported argument to %qD", fndecl); |
53800dbe | 4290 | return const0_rtx; |
4291 | } | |
4292 | ||
5b252e95 | 4293 | if (count) |
4294 | { | |
4295 | /* Warn since no effort is made to ensure that any frame | |
4296 | beyond the current one exists or can be safely reached. */ | |
4297 | warning (OPT_Wframe_address, "calling %qD with " | |
4298 | "a nonzero argument is unsafe", fndecl); | |
4299 | } | |
4300 | ||
53800dbe | 4301 | /* For __builtin_frame_address, return what we've got. */ |
4302 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4303 | return tem; | |
4304 | ||
8ad4c111 | 4305 | if (!REG_P (tem) |
53800dbe | 4306 | && ! CONSTANT_P (tem)) |
99182918 | 4307 | tem = copy_addr_to_reg (tem); |
53800dbe | 4308 | return tem; |
4309 | } | |
4310 | } | |
4311 | ||
990495a7 | 4312 | /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we |
5be42b39 | 4313 | failed and the caller should emit a normal call. CANNOT_ACCUMULATE |
4314 | is the same as for allocate_dynamic_stack_space. */ | |
15c6cf6b | 4315 | |
53800dbe | 4316 | static rtx |
5be42b39 | 4317 | expand_builtin_alloca (tree exp, bool cannot_accumulate) |
53800dbe | 4318 | { |
4319 | rtx op0; | |
15c6cf6b | 4320 | rtx result; |
581bf1c2 | 4321 | bool valid_arglist; |
4322 | unsigned int align; | |
4323 | bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp)) | |
4324 | == BUILT_IN_ALLOCA_WITH_ALIGN); | |
53800dbe | 4325 | |
581bf1c2 | 4326 | valid_arglist |
4327 | = (alloca_with_align | |
4328 | ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
4329 | : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)); | |
4330 | ||
4331 | if (!valid_arglist) | |
c2f47e15 | 4332 | return NULL_RTX; |
53800dbe | 4333 | |
4334 | /* Compute the argument. */ | |
c2f47e15 | 4335 | op0 = expand_normal (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 4336 | |
581bf1c2 | 4337 | /* Compute the alignment. */ |
4338 | align = (alloca_with_align | |
f9ae6f95 | 4339 | ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)) |
581bf1c2 | 4340 | : BIGGEST_ALIGNMENT); |
4341 | ||
53800dbe | 4342 | /* Allocate the desired space. */ |
581bf1c2 | 4343 | result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate); |
85d654dd | 4344 | result = convert_memory_address (ptr_mode, result); |
15c6cf6b | 4345 | |
4346 | return result; | |
53800dbe | 4347 | } |
4348 | ||
74bdbe96 | 4349 | /* Expand a call to bswap builtin in EXP. |
4350 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
4351 | function in-line. If convenient, the result should be placed in TARGET. | |
4352 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
42791117 | 4353 | |
4354 | static rtx | |
3754d046 | 4355 | expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target, |
74bdbe96 | 4356 | rtx subtarget) |
42791117 | 4357 | { |
42791117 | 4358 | tree arg; |
4359 | rtx op0; | |
4360 | ||
c2f47e15 | 4361 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) |
4362 | return NULL_RTX; | |
42791117 | 4363 | |
c2f47e15 | 4364 | arg = CALL_EXPR_ARG (exp, 0); |
74bdbe96 | 4365 | op0 = expand_expr (arg, |
4366 | subtarget && GET_MODE (subtarget) == target_mode | |
4367 | ? subtarget : NULL_RTX, | |
4368 | target_mode, EXPAND_NORMAL); | |
4369 | if (GET_MODE (op0) != target_mode) | |
4370 | op0 = convert_to_mode (target_mode, op0, 1); | |
42791117 | 4371 | |
74bdbe96 | 4372 | target = expand_unop (target_mode, bswap_optab, op0, target, 1); |
42791117 | 4373 | |
4374 | gcc_assert (target); | |
4375 | ||
74bdbe96 | 4376 | return convert_to_mode (target_mode, target, 1); |
42791117 | 4377 | } |
4378 | ||
c2f47e15 | 4379 | /* Expand a call to a unary builtin in EXP. |
4380 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
53800dbe | 4381 | function in-line. If convenient, the result should be placed in TARGET. |
4382 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
15c6cf6b | 4383 | |
53800dbe | 4384 | static rtx |
3754d046 | 4385 | expand_builtin_unop (machine_mode target_mode, tree exp, rtx target, |
aecda0d6 | 4386 | rtx subtarget, optab op_optab) |
53800dbe | 4387 | { |
4388 | rtx op0; | |
c2f47e15 | 4389 | |
4390 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) | |
4391 | return NULL_RTX; | |
53800dbe | 4392 | |
4393 | /* Compute the argument. */ | |
f97eea22 | 4394 | op0 = expand_expr (CALL_EXPR_ARG (exp, 0), |
4395 | (subtarget | |
4396 | && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))) | |
4397 | == GET_MODE (subtarget))) ? subtarget : NULL_RTX, | |
1db6d067 | 4398 | VOIDmode, EXPAND_NORMAL); |
6a08d0ab | 4399 | /* Compute op, into TARGET if possible. |
53800dbe | 4400 | Set TARGET to wherever the result comes back. */ |
c2f47e15 | 4401 | target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))), |
6aaa1f9e | 4402 | op_optab, op0, target, op_optab != clrsb_optab); |
64db345d | 4403 | gcc_assert (target); |
7d3f6cc7 | 4404 | |
efb070c8 | 4405 | return convert_to_mode (target_mode, target, 0); |
53800dbe | 4406 | } |
89cfe6e5 | 4407 | |
48e1416a | 4408 | /* Expand a call to __builtin_expect. We just return our argument |
5a74f77e | 4409 | as the builtin_expect semantic should've been already executed by |
4410 | tree branch prediction pass. */ | |
89cfe6e5 | 4411 | |
4412 | static rtx | |
c2f47e15 | 4413 | expand_builtin_expect (tree exp, rtx target) |
89cfe6e5 | 4414 | { |
1e4adcfc | 4415 | tree arg; |
89cfe6e5 | 4416 | |
c2f47e15 | 4417 | if (call_expr_nargs (exp) < 2) |
89cfe6e5 | 4418 | return const0_rtx; |
c2f47e15 | 4419 | arg = CALL_EXPR_ARG (exp, 0); |
89cfe6e5 | 4420 | |
c2f47e15 | 4421 | target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5a74f77e | 4422 | /* When guessing was done, the hints should be already stripped away. */ |
07311427 | 4423 | gcc_assert (!flag_guess_branch_prob |
852f689e | 4424 | || optimize == 0 || seen_error ()); |
89cfe6e5 | 4425 | return target; |
4426 | } | |
689df48e | 4427 | |
fca0886c | 4428 | /* Expand a call to __builtin_assume_aligned. We just return our first |
4429 | argument as the builtin_assume_aligned semantic should've been already | |
4430 | executed by CCP. */ | |
4431 | ||
4432 | static rtx | |
4433 | expand_builtin_assume_aligned (tree exp, rtx target) | |
4434 | { | |
4435 | if (call_expr_nargs (exp) < 2) | |
4436 | return const0_rtx; | |
4437 | target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode, | |
4438 | EXPAND_NORMAL); | |
4439 | gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1)) | |
4440 | && (call_expr_nargs (exp) < 3 | |
4441 | || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2)))); | |
4442 | return target; | |
4443 | } | |
4444 | ||
c22de3f0 | 4445 | void |
aecda0d6 | 4446 | expand_builtin_trap (void) |
a0ef1725 | 4447 | { |
4db8dd0c | 4448 | if (targetm.have_trap ()) |
f73960eb | 4449 | { |
4db8dd0c | 4450 | rtx_insn *insn = emit_insn (targetm.gen_trap ()); |
f73960eb | 4451 | /* For trap insns when not accumulating outgoing args force |
4452 | REG_ARGS_SIZE note to prevent crossjumping of calls with | |
4453 | different args sizes. */ | |
4454 | if (!ACCUMULATE_OUTGOING_ARGS) | |
4455 | add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta)); | |
4456 | } | |
a0ef1725 | 4457 | else |
a0ef1725 | 4458 | emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0); |
4459 | emit_barrier (); | |
4460 | } | |
78a74442 | 4461 | |
d2b48f0c | 4462 | /* Expand a call to __builtin_unreachable. We do nothing except emit |
4463 | a barrier saying that control flow will not pass here. | |
4464 | ||
4465 | It is the responsibility of the program being compiled to ensure | |
4466 | that control flow does never reach __builtin_unreachable. */ | |
4467 | static void | |
4468 | expand_builtin_unreachable (void) | |
4469 | { | |
4470 | emit_barrier (); | |
4471 | } | |
4472 | ||
c2f47e15 | 4473 | /* Expand EXP, a call to fabs, fabsf or fabsl. |
4474 | Return NULL_RTX if a normal call should be emitted rather than expanding | |
78a74442 | 4475 | the function inline. If convenient, the result should be placed |
4476 | in TARGET. SUBTARGET may be used as the target for computing | |
4477 | the operand. */ | |
4478 | ||
4479 | static rtx | |
c2f47e15 | 4480 | expand_builtin_fabs (tree exp, rtx target, rtx subtarget) |
78a74442 | 4481 | { |
3754d046 | 4482 | machine_mode mode; |
78a74442 | 4483 | tree arg; |
4484 | rtx op0; | |
4485 | ||
c2f47e15 | 4486 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
4487 | return NULL_RTX; | |
78a74442 | 4488 | |
c2f47e15 | 4489 | arg = CALL_EXPR_ARG (exp, 0); |
c7f617c2 | 4490 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
78a74442 | 4491 | mode = TYPE_MODE (TREE_TYPE (arg)); |
1db6d067 | 4492 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
78a74442 | 4493 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); |
4494 | } | |
4495 | ||
c2f47e15 | 4496 | /* Expand EXP, a call to copysign, copysignf, or copysignl. |
270436f3 | 4497 | Return NULL is a normal call should be emitted rather than expanding the |
4498 | function inline. If convenient, the result should be placed in TARGET. | |
4499 | SUBTARGET may be used as the target for computing the operand. */ | |
4500 | ||
4501 | static rtx | |
c2f47e15 | 4502 | expand_builtin_copysign (tree exp, rtx target, rtx subtarget) |
270436f3 | 4503 | { |
4504 | rtx op0, op1; | |
4505 | tree arg; | |
4506 | ||
c2f47e15 | 4507 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE)) |
4508 | return NULL_RTX; | |
270436f3 | 4509 | |
c2f47e15 | 4510 | arg = CALL_EXPR_ARG (exp, 0); |
8ec3c5c2 | 4511 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
270436f3 | 4512 | |
c2f47e15 | 4513 | arg = CALL_EXPR_ARG (exp, 1); |
8ec3c5c2 | 4514 | op1 = expand_normal (arg); |
270436f3 | 4515 | |
4516 | return expand_copysign (op0, op1, target); | |
4517 | } | |
4518 | ||
ac8fb6db | 4519 | /* Expand a call to __builtin___clear_cache. */ |
4520 | ||
4521 | static rtx | |
32e17df0 | 4522 | expand_builtin___clear_cache (tree exp) |
ac8fb6db | 4523 | { |
32e17df0 | 4524 | if (!targetm.code_for_clear_cache) |
4525 | { | |
ac8fb6db | 4526 | #ifdef CLEAR_INSN_CACHE |
32e17df0 | 4527 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
4528 | does something. Just do the default expansion to a call to | |
4529 | __clear_cache(). */ | |
4530 | return NULL_RTX; | |
ac8fb6db | 4531 | #else |
32e17df0 | 4532 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
4533 | does nothing. There is no need to call it. Do nothing. */ | |
4534 | return const0_rtx; | |
ac8fb6db | 4535 | #endif /* CLEAR_INSN_CACHE */ |
32e17df0 | 4536 | } |
4537 | ||
ac8fb6db | 4538 | /* We have a "clear_cache" insn, and it will handle everything. */ |
4539 | tree begin, end; | |
4540 | rtx begin_rtx, end_rtx; | |
ac8fb6db | 4541 | |
4542 | /* We must not expand to a library call. If we did, any | |
4543 | fallback library function in libgcc that might contain a call to | |
4544 | __builtin___clear_cache() would recurse infinitely. */ | |
4545 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
4546 | { | |
4547 | error ("both arguments to %<__builtin___clear_cache%> must be pointers"); | |
4548 | return const0_rtx; | |
4549 | } | |
4550 | ||
32e17df0 | 4551 | if (targetm.have_clear_cache ()) |
ac8fb6db | 4552 | { |
8786db1e | 4553 | struct expand_operand ops[2]; |
ac8fb6db | 4554 | |
4555 | begin = CALL_EXPR_ARG (exp, 0); | |
4556 | begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL); | |
ac8fb6db | 4557 | |
4558 | end = CALL_EXPR_ARG (exp, 1); | |
4559 | end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL); | |
ac8fb6db | 4560 | |
8786db1e | 4561 | create_address_operand (&ops[0], begin_rtx); |
4562 | create_address_operand (&ops[1], end_rtx); | |
32e17df0 | 4563 | if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops)) |
8786db1e | 4564 | return const0_rtx; |
ac8fb6db | 4565 | } |
4566 | return const0_rtx; | |
ac8fb6db | 4567 | } |
4568 | ||
4ee9c684 | 4569 | /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */ |
4570 | ||
4571 | static rtx | |
4572 | round_trampoline_addr (rtx tramp) | |
4573 | { | |
4574 | rtx temp, addend, mask; | |
4575 | ||
4576 | /* If we don't need too much alignment, we'll have been guaranteed | |
4577 | proper alignment by get_trampoline_type. */ | |
4578 | if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY) | |
4579 | return tramp; | |
4580 | ||
4581 | /* Round address up to desired boundary. */ | |
4582 | temp = gen_reg_rtx (Pmode); | |
0359f9f5 | 4583 | addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode); |
4584 | mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode); | |
4ee9c684 | 4585 | |
4586 | temp = expand_simple_binop (Pmode, PLUS, tramp, addend, | |
4587 | temp, 0, OPTAB_LIB_WIDEN); | |
4588 | tramp = expand_simple_binop (Pmode, AND, temp, mask, | |
4589 | temp, 0, OPTAB_LIB_WIDEN); | |
4590 | ||
4591 | return tramp; | |
4592 | } | |
4593 | ||
4594 | static rtx | |
c307f106 | 4595 | expand_builtin_init_trampoline (tree exp, bool onstack) |
4ee9c684 | 4596 | { |
4597 | tree t_tramp, t_func, t_chain; | |
82c7907c | 4598 | rtx m_tramp, r_tramp, r_chain, tmp; |
4ee9c684 | 4599 | |
c2f47e15 | 4600 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, |
4ee9c684 | 4601 | POINTER_TYPE, VOID_TYPE)) |
4602 | return NULL_RTX; | |
4603 | ||
c2f47e15 | 4604 | t_tramp = CALL_EXPR_ARG (exp, 0); |
4605 | t_func = CALL_EXPR_ARG (exp, 1); | |
4606 | t_chain = CALL_EXPR_ARG (exp, 2); | |
4ee9c684 | 4607 | |
8ec3c5c2 | 4608 | r_tramp = expand_normal (t_tramp); |
82c7907c | 4609 | m_tramp = gen_rtx_MEM (BLKmode, r_tramp); |
4610 | MEM_NOTRAP_P (m_tramp) = 1; | |
4611 | ||
c307f106 | 4612 | /* If ONSTACK, the TRAMP argument should be the address of a field |
4613 | within the local function's FRAME decl. Either way, let's see if | |
4614 | we can fill in the MEM_ATTRs for this memory. */ | |
82c7907c | 4615 | if (TREE_CODE (t_tramp) == ADDR_EXPR) |
f4146cb8 | 4616 | set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true); |
82c7907c | 4617 | |
c307f106 | 4618 | /* Creator of a heap trampoline is responsible for making sure the |
4619 | address is aligned to at least STACK_BOUNDARY. Normally malloc | |
4620 | will ensure this anyhow. */ | |
82c7907c | 4621 | tmp = round_trampoline_addr (r_tramp); |
4622 | if (tmp != r_tramp) | |
4623 | { | |
4624 | m_tramp = change_address (m_tramp, BLKmode, tmp); | |
4625 | set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT); | |
5b2a69fa | 4626 | set_mem_size (m_tramp, TRAMPOLINE_SIZE); |
82c7907c | 4627 | } |
4628 | ||
4629 | /* The FUNC argument should be the address of the nested function. | |
4630 | Extract the actual function decl to pass to the hook. */ | |
4631 | gcc_assert (TREE_CODE (t_func) == ADDR_EXPR); | |
4632 | t_func = TREE_OPERAND (t_func, 0); | |
4633 | gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL); | |
4634 | ||
8ec3c5c2 | 4635 | r_chain = expand_normal (t_chain); |
4ee9c684 | 4636 | |
4637 | /* Generate insns to initialize the trampoline. */ | |
82c7907c | 4638 | targetm.calls.trampoline_init (m_tramp, t_func, r_chain); |
4ee9c684 | 4639 | |
c307f106 | 4640 | if (onstack) |
4641 | { | |
4642 | trampolines_created = 1; | |
8bc8a8f4 | 4643 | |
c307f106 | 4644 | warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines, |
4645 | "trampoline generated for nested function %qD", t_func); | |
4646 | } | |
8bc8a8f4 | 4647 | |
4ee9c684 | 4648 | return const0_rtx; |
4649 | } | |
4650 | ||
4651 | static rtx | |
c2f47e15 | 4652 | expand_builtin_adjust_trampoline (tree exp) |
4ee9c684 | 4653 | { |
4654 | rtx tramp; | |
4655 | ||
c2f47e15 | 4656 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 4657 | return NULL_RTX; |
4658 | ||
c2f47e15 | 4659 | tramp = expand_normal (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 4660 | tramp = round_trampoline_addr (tramp); |
82c7907c | 4661 | if (targetm.calls.trampoline_adjust_address) |
4662 | tramp = targetm.calls.trampoline_adjust_address (tramp); | |
4ee9c684 | 4663 | |
4664 | return tramp; | |
4665 | } | |
4666 | ||
93f564d6 | 4667 | /* Expand the call EXP to the built-in signbit, signbitf or signbitl |
4668 | function. The function first checks whether the back end provides | |
4669 | an insn to implement signbit for the respective mode. If not, it | |
4670 | checks whether the floating point format of the value is such that | |
10902624 | 4671 | the sign bit can be extracted. If that is not the case, error out. |
4672 | EXP is the expression that is a call to the builtin function; if | |
4673 | convenient, the result should be placed in TARGET. */ | |
27f261ef | 4674 | static rtx |
4675 | expand_builtin_signbit (tree exp, rtx target) | |
4676 | { | |
4677 | const struct real_format *fmt; | |
3754d046 | 4678 | machine_mode fmode, imode, rmode; |
c2f47e15 | 4679 | tree arg; |
ca4f1f5b | 4680 | int word, bitpos; |
27eda240 | 4681 | enum insn_code icode; |
27f261ef | 4682 | rtx temp; |
389dd41b | 4683 | location_t loc = EXPR_LOCATION (exp); |
27f261ef | 4684 | |
c2f47e15 | 4685 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
4686 | return NULL_RTX; | |
27f261ef | 4687 | |
c2f47e15 | 4688 | arg = CALL_EXPR_ARG (exp, 0); |
27f261ef | 4689 | fmode = TYPE_MODE (TREE_TYPE (arg)); |
4690 | rmode = TYPE_MODE (TREE_TYPE (exp)); | |
4691 | fmt = REAL_MODE_FORMAT (fmode); | |
4692 | ||
93f564d6 | 4693 | arg = builtin_save_expr (arg); |
4694 | ||
4695 | /* Expand the argument yielding a RTX expression. */ | |
4696 | temp = expand_normal (arg); | |
4697 | ||
4698 | /* Check if the back end provides an insn that handles signbit for the | |
4699 | argument's mode. */ | |
d6bf3b14 | 4700 | icode = optab_handler (signbit_optab, fmode); |
27eda240 | 4701 | if (icode != CODE_FOR_nothing) |
93f564d6 | 4702 | { |
1e0c0b35 | 4703 | rtx_insn *last = get_last_insn (); |
93f564d6 | 4704 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); |
4e2a2fb4 | 4705 | if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN)) |
4706 | return target; | |
4707 | delete_insns_since (last); | |
93f564d6 | 4708 | } |
4709 | ||
27f261ef | 4710 | /* For floating point formats without a sign bit, implement signbit |
4711 | as "ARG < 0.0". */ | |
8d564692 | 4712 | bitpos = fmt->signbit_ro; |
ca4f1f5b | 4713 | if (bitpos < 0) |
27f261ef | 4714 | { |
4715 | /* But we can't do this if the format supports signed zero. */ | |
10902624 | 4716 | gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode)); |
27f261ef | 4717 | |
389dd41b | 4718 | arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg, |
49d00087 | 4719 | build_real (TREE_TYPE (arg), dconst0)); |
27f261ef | 4720 | return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
4721 | } | |
4722 | ||
ca4f1f5b | 4723 | if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD) |
27f261ef | 4724 | { |
ca4f1f5b | 4725 | imode = int_mode_for_mode (fmode); |
10902624 | 4726 | gcc_assert (imode != BLKmode); |
ca4f1f5b | 4727 | temp = gen_lowpart (imode, temp); |
24fd4260 | 4728 | } |
4729 | else | |
4730 | { | |
ca4f1f5b | 4731 | imode = word_mode; |
4732 | /* Handle targets with different FP word orders. */ | |
4733 | if (FLOAT_WORDS_BIG_ENDIAN) | |
a0c938f0 | 4734 | word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD; |
ca4f1f5b | 4735 | else |
a0c938f0 | 4736 | word = bitpos / BITS_PER_WORD; |
ca4f1f5b | 4737 | temp = operand_subword_force (temp, word, fmode); |
4738 | bitpos = bitpos % BITS_PER_WORD; | |
4739 | } | |
4740 | ||
44b0f1d0 | 4741 | /* Force the intermediate word_mode (or narrower) result into a |
4742 | register. This avoids attempting to create paradoxical SUBREGs | |
4743 | of floating point modes below. */ | |
4744 | temp = force_reg (imode, temp); | |
4745 | ||
ca4f1f5b | 4746 | /* If the bitpos is within the "result mode" lowpart, the operation |
4747 | can be implement with a single bitwise AND. Otherwise, we need | |
4748 | a right shift and an AND. */ | |
4749 | ||
4750 | if (bitpos < GET_MODE_BITSIZE (rmode)) | |
4751 | { | |
796b6678 | 4752 | wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode)); |
27f261ef | 4753 | |
4a46f016 | 4754 | if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode)) |
ca4f1f5b | 4755 | temp = gen_lowpart (rmode, temp); |
24fd4260 | 4756 | temp = expand_binop (rmode, and_optab, temp, |
e913b5cd | 4757 | immed_wide_int_const (mask, rmode), |
ca4f1f5b | 4758 | NULL_RTX, 1, OPTAB_LIB_WIDEN); |
27f261ef | 4759 | } |
ca4f1f5b | 4760 | else |
4761 | { | |
4762 | /* Perform a logical right shift to place the signbit in the least | |
a0c938f0 | 4763 | significant bit, then truncate the result to the desired mode |
ca4f1f5b | 4764 | and mask just this bit. */ |
f5ff0b21 | 4765 | temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1); |
ca4f1f5b | 4766 | temp = gen_lowpart (rmode, temp); |
4767 | temp = expand_binop (rmode, and_optab, temp, const1_rtx, | |
4768 | NULL_RTX, 1, OPTAB_LIB_WIDEN); | |
4769 | } | |
4770 | ||
27f261ef | 4771 | return temp; |
4772 | } | |
73673831 | 4773 | |
4774 | /* Expand fork or exec calls. TARGET is the desired target of the | |
c2f47e15 | 4775 | call. EXP is the call. FN is the |
73673831 | 4776 | identificator of the actual function. IGNORE is nonzero if the |
4777 | value is to be ignored. */ | |
4778 | ||
4779 | static rtx | |
c2f47e15 | 4780 | expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore) |
73673831 | 4781 | { |
4782 | tree id, decl; | |
4783 | tree call; | |
4784 | ||
4785 | /* If we are not profiling, just call the function. */ | |
4786 | if (!profile_arc_flag) | |
4787 | return NULL_RTX; | |
4788 | ||
4789 | /* Otherwise call the wrapper. This should be equivalent for the rest of | |
4790 | compiler, so the code does not diverge, and the wrapper may run the | |
9c9bad97 | 4791 | code necessary for keeping the profiling sane. */ |
73673831 | 4792 | |
4793 | switch (DECL_FUNCTION_CODE (fn)) | |
4794 | { | |
4795 | case BUILT_IN_FORK: | |
4796 | id = get_identifier ("__gcov_fork"); | |
4797 | break; | |
4798 | ||
4799 | case BUILT_IN_EXECL: | |
4800 | id = get_identifier ("__gcov_execl"); | |
4801 | break; | |
4802 | ||
4803 | case BUILT_IN_EXECV: | |
4804 | id = get_identifier ("__gcov_execv"); | |
4805 | break; | |
4806 | ||
4807 | case BUILT_IN_EXECLP: | |
4808 | id = get_identifier ("__gcov_execlp"); | |
4809 | break; | |
4810 | ||
4811 | case BUILT_IN_EXECLE: | |
4812 | id = get_identifier ("__gcov_execle"); | |
4813 | break; | |
4814 | ||
4815 | case BUILT_IN_EXECVP: | |
4816 | id = get_identifier ("__gcov_execvp"); | |
4817 | break; | |
4818 | ||
4819 | case BUILT_IN_EXECVE: | |
4820 | id = get_identifier ("__gcov_execve"); | |
4821 | break; | |
4822 | ||
4823 | default: | |
64db345d | 4824 | gcc_unreachable (); |
73673831 | 4825 | } |
4826 | ||
e60a6f7b | 4827 | decl = build_decl (DECL_SOURCE_LOCATION (fn), |
4828 | FUNCTION_DECL, id, TREE_TYPE (fn)); | |
73673831 | 4829 | DECL_EXTERNAL (decl) = 1; |
4830 | TREE_PUBLIC (decl) = 1; | |
4831 | DECL_ARTIFICIAL (decl) = 1; | |
4832 | TREE_NOTHROW (decl) = 1; | |
e82d310b | 4833 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
4834 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
389dd41b | 4835 | call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0); |
73673831 | 4836 | return expand_call (call, target, ignore); |
c2f47e15 | 4837 | } |
48e1416a | 4838 | |
b6a5fc45 | 4839 | |
4840 | \f | |
3e272de8 | 4841 | /* Reconstitute a mode for a __sync intrinsic operation. Since the type of |
4842 | the pointer in these functions is void*, the tree optimizers may remove | |
4843 | casts. The mode computed in expand_builtin isn't reliable either, due | |
4844 | to __sync_bool_compare_and_swap. | |
4845 | ||
4846 | FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the | |
4847 | group of builtins. This gives us log2 of the mode size. */ | |
4848 | ||
3754d046 | 4849 | static inline machine_mode |
3e272de8 | 4850 | get_builtin_sync_mode (int fcode_diff) |
4851 | { | |
ad3a13b5 | 4852 | /* The size is not negotiable, so ask not to get BLKmode in return |
4853 | if the target indicates that a smaller size would be better. */ | |
4854 | return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0); | |
3e272de8 | 4855 | } |
4856 | ||
041e0215 | 4857 | /* Expand the memory expression LOC and return the appropriate memory operand |
4858 | for the builtin_sync operations. */ | |
4859 | ||
4860 | static rtx | |
3754d046 | 4861 | get_builtin_sync_mem (tree loc, machine_mode mode) |
041e0215 | 4862 | { |
4863 | rtx addr, mem; | |
4864 | ||
7f4d56ad | 4865 | addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM); |
4866 | addr = convert_memory_address (Pmode, addr); | |
041e0215 | 4867 | |
4868 | /* Note that we explicitly do not want any alias information for this | |
4869 | memory, so that we kill all other live memories. Otherwise we don't | |
4870 | satisfy the full barrier semantics of the intrinsic. */ | |
4871 | mem = validize_mem (gen_rtx_MEM (mode, addr)); | |
4872 | ||
153c3b50 | 4873 | /* The alignment needs to be at least according to that of the mode. */ |
4874 | set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode), | |
957d0361 | 4875 | get_pointer_alignment (loc))); |
c94cfd1c | 4876 | set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER); |
041e0215 | 4877 | MEM_VOLATILE_P (mem) = 1; |
4878 | ||
4879 | return mem; | |
4880 | } | |
4881 | ||
1cd6e20d | 4882 | /* Make sure an argument is in the right mode. |
4883 | EXP is the tree argument. | |
4884 | MODE is the mode it should be in. */ | |
4885 | ||
4886 | static rtx | |
3754d046 | 4887 | expand_expr_force_mode (tree exp, machine_mode mode) |
1cd6e20d | 4888 | { |
4889 | rtx val; | |
3754d046 | 4890 | machine_mode old_mode; |
1cd6e20d | 4891 | |
4892 | val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL); | |
4893 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care | |
4894 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
4895 | ||
4896 | old_mode = GET_MODE (val); | |
4897 | if (old_mode == VOIDmode) | |
4898 | old_mode = TYPE_MODE (TREE_TYPE (exp)); | |
4899 | val = convert_modes (mode, old_mode, val, 1); | |
4900 | return val; | |
4901 | } | |
4902 | ||
4903 | ||
b6a5fc45 | 4904 | /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics. |
c2f47e15 | 4905 | EXP is the CALL_EXPR. CODE is the rtx code |
b6a5fc45 | 4906 | that corresponds to the arithmetic or logical operation from the name; |
4907 | an exception here is that NOT actually means NAND. TARGET is an optional | |
4908 | place for us to store the results; AFTER is true if this is the | |
1cd6e20d | 4909 | fetch_and_xxx form. */ |
b6a5fc45 | 4910 | |
4911 | static rtx | |
3754d046 | 4912 | expand_builtin_sync_operation (machine_mode mode, tree exp, |
3e272de8 | 4913 | enum rtx_code code, bool after, |
1cd6e20d | 4914 | rtx target) |
b6a5fc45 | 4915 | { |
041e0215 | 4916 | rtx val, mem; |
e60a6f7b | 4917 | location_t loc = EXPR_LOCATION (exp); |
b6a5fc45 | 4918 | |
cf73e559 | 4919 | if (code == NOT && warn_sync_nand) |
4920 | { | |
4921 | tree fndecl = get_callee_fndecl (exp); | |
4922 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
4923 | ||
4924 | static bool warned_f_a_n, warned_n_a_f; | |
4925 | ||
4926 | switch (fcode) | |
4927 | { | |
2797f13a | 4928 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
4929 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
4930 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
4931 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
4932 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
cf73e559 | 4933 | if (warned_f_a_n) |
4934 | break; | |
4935 | ||
b9a16870 | 4936 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N); |
e60a6f7b | 4937 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
cf73e559 | 4938 | warned_f_a_n = true; |
4939 | break; | |
4940 | ||
2797f13a | 4941 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
4942 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
4943 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
4944 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
4945 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
cf73e559 | 4946 | if (warned_n_a_f) |
4947 | break; | |
4948 | ||
b9a16870 | 4949 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N); |
e60a6f7b | 4950 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
cf73e559 | 4951 | warned_n_a_f = true; |
4952 | break; | |
4953 | ||
4954 | default: | |
4955 | gcc_unreachable (); | |
4956 | } | |
4957 | } | |
4958 | ||
b6a5fc45 | 4959 | /* Expand the operands. */ |
c2f47e15 | 4960 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 4961 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
b6a5fc45 | 4962 | |
a372f7ca | 4963 | return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST, |
1cd6e20d | 4964 | after); |
b6a5fc45 | 4965 | } |
4966 | ||
4967 | /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap | |
c2f47e15 | 4968 | intrinsics. EXP is the CALL_EXPR. IS_BOOL is |
b6a5fc45 | 4969 | true if this is the boolean form. TARGET is a place for us to store the |
4970 | results; this is NOT optional if IS_BOOL is true. */ | |
4971 | ||
4972 | static rtx | |
3754d046 | 4973 | expand_builtin_compare_and_swap (machine_mode mode, tree exp, |
3e272de8 | 4974 | bool is_bool, rtx target) |
b6a5fc45 | 4975 | { |
041e0215 | 4976 | rtx old_val, new_val, mem; |
ba885f6a | 4977 | rtx *pbool, *poval; |
b6a5fc45 | 4978 | |
4979 | /* Expand the operands. */ | |
c2f47e15 | 4980 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 4981 | old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
4982 | new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); | |
b6a5fc45 | 4983 | |
ba885f6a | 4984 | pbool = poval = NULL; |
4985 | if (target != const0_rtx) | |
4986 | { | |
4987 | if (is_bool) | |
4988 | pbool = ⌖ | |
4989 | else | |
4990 | poval = ⌖ | |
4991 | } | |
4992 | if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val, | |
a372f7ca | 4993 | false, MEMMODEL_SYNC_SEQ_CST, |
4994 | MEMMODEL_SYNC_SEQ_CST)) | |
1cd6e20d | 4995 | return NULL_RTX; |
c2f47e15 | 4996 | |
1cd6e20d | 4997 | return target; |
b6a5fc45 | 4998 | } |
4999 | ||
5000 | /* Expand the __sync_lock_test_and_set intrinsic. Note that the most | |
5001 | general form is actually an atomic exchange, and some targets only | |
5002 | support a reduced form with the second argument being a constant 1. | |
48e1416a | 5003 | EXP is the CALL_EXPR; TARGET is an optional place for us to store |
c2f47e15 | 5004 | the results. */ |
b6a5fc45 | 5005 | |
5006 | static rtx | |
3754d046 | 5007 | expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp, |
1cd6e20d | 5008 | rtx target) |
b6a5fc45 | 5009 | { |
041e0215 | 5010 | rtx val, mem; |
b6a5fc45 | 5011 | |
5012 | /* Expand the operands. */ | |
c2f47e15 | 5013 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1cd6e20d | 5014 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
5015 | ||
7821cde1 | 5016 | return expand_sync_lock_test_and_set (target, mem, val); |
1cd6e20d | 5017 | } |
5018 | ||
5019 | /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */ | |
5020 | ||
5021 | static void | |
3754d046 | 5022 | expand_builtin_sync_lock_release (machine_mode mode, tree exp) |
1cd6e20d | 5023 | { |
5024 | rtx mem; | |
5025 | ||
5026 | /* Expand the operands. */ | |
5027 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5028 | ||
a372f7ca | 5029 | expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true); |
1cd6e20d | 5030 | } |
5031 | ||
5032 | /* Given an integer representing an ``enum memmodel'', verify its | |
5033 | correctness and return the memory model enum. */ | |
5034 | ||
5035 | static enum memmodel | |
5036 | get_memmodel (tree exp) | |
5037 | { | |
5038 | rtx op; | |
7f738025 | 5039 | unsigned HOST_WIDE_INT val; |
2cb724f9 | 5040 | source_location loc |
5041 | = expansion_point_location_if_in_system_header (input_location); | |
1cd6e20d | 5042 | |
5043 | /* If the parameter is not a constant, it's a run time value so we'll just | |
5044 | convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */ | |
5045 | if (TREE_CODE (exp) != INTEGER_CST) | |
5046 | return MEMMODEL_SEQ_CST; | |
5047 | ||
5048 | op = expand_normal (exp); | |
7f738025 | 5049 | |
5050 | val = INTVAL (op); | |
5051 | if (targetm.memmodel_check) | |
5052 | val = targetm.memmodel_check (val); | |
5053 | else if (val & ~MEMMODEL_MASK) | |
5054 | { | |
2cb724f9 | 5055 | warning_at (loc, OPT_Winvalid_memory_model, |
5056 | "unknown architecture specifier in memory model to builtin"); | |
7f738025 | 5057 | return MEMMODEL_SEQ_CST; |
5058 | } | |
5059 | ||
a372f7ca | 5060 | /* Should never see a user explicit SYNC memodel model, so >= LAST works. */ |
5061 | if (memmodel_base (val) >= MEMMODEL_LAST) | |
1cd6e20d | 5062 | { |
2cb724f9 | 5063 | warning_at (loc, OPT_Winvalid_memory_model, |
5064 | "invalid memory model argument to builtin"); | |
1cd6e20d | 5065 | return MEMMODEL_SEQ_CST; |
5066 | } | |
7f738025 | 5067 | |
3070f133 | 5068 | /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so |
5069 | be conservative and promote consume to acquire. */ | |
5070 | if (val == MEMMODEL_CONSUME) | |
5071 | val = MEMMODEL_ACQUIRE; | |
5072 | ||
7f738025 | 5073 | return (enum memmodel) val; |
1cd6e20d | 5074 | } |
5075 | ||
5076 | /* Expand the __atomic_exchange intrinsic: | |
5077 | TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel) | |
5078 | EXP is the CALL_EXPR. | |
5079 | TARGET is an optional place for us to store the results. */ | |
5080 | ||
5081 | static rtx | |
3754d046 | 5082 | expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target) |
1cd6e20d | 5083 | { |
5084 | rtx val, mem; | |
5085 | enum memmodel model; | |
5086 | ||
5087 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
1cd6e20d | 5088 | |
5089 | if (!flag_inline_atomics) | |
5090 | return NULL_RTX; | |
5091 | ||
5092 | /* Expand the operands. */ | |
5093 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5094 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5095 | ||
7821cde1 | 5096 | return expand_atomic_exchange (target, mem, val, model); |
1cd6e20d | 5097 | } |
5098 | ||
5099 | /* Expand the __atomic_compare_exchange intrinsic: | |
5100 | bool __atomic_compare_exchange (TYPE *object, TYPE *expect, | |
5101 | TYPE desired, BOOL weak, | |
5102 | enum memmodel success, | |
5103 | enum memmodel failure) | |
5104 | EXP is the CALL_EXPR. | |
5105 | TARGET is an optional place for us to store the results. */ | |
5106 | ||
5107 | static rtx | |
3754d046 | 5108 | expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp, |
1cd6e20d | 5109 | rtx target) |
5110 | { | |
1e0c0b35 | 5111 | rtx expect, desired, mem, oldval; |
5112 | rtx_code_label *label; | |
1cd6e20d | 5113 | enum memmodel success, failure; |
5114 | tree weak; | |
5115 | bool is_weak; | |
2cb724f9 | 5116 | source_location loc |
5117 | = expansion_point_location_if_in_system_header (input_location); | |
1cd6e20d | 5118 | |
5119 | success = get_memmodel (CALL_EXPR_ARG (exp, 4)); | |
5120 | failure = get_memmodel (CALL_EXPR_ARG (exp, 5)); | |
5121 | ||
086f4e33 | 5122 | if (failure > success) |
5123 | { | |
2cb724f9 | 5124 | warning_at (loc, OPT_Winvalid_memory_model, |
5125 | "failure memory model cannot be stronger than success " | |
5126 | "memory model for %<__atomic_compare_exchange%>"); | |
086f4e33 | 5127 | success = MEMMODEL_SEQ_CST; |
5128 | } | |
5129 | ||
a372f7ca | 5130 | if (is_mm_release (failure) || is_mm_acq_rel (failure)) |
1cd6e20d | 5131 | { |
2cb724f9 | 5132 | warning_at (loc, OPT_Winvalid_memory_model, |
5133 | "invalid failure memory model for " | |
5134 | "%<__atomic_compare_exchange%>"); | |
086f4e33 | 5135 | failure = MEMMODEL_SEQ_CST; |
5136 | success = MEMMODEL_SEQ_CST; | |
1cd6e20d | 5137 | } |
5138 | ||
086f4e33 | 5139 | |
1cd6e20d | 5140 | if (!flag_inline_atomics) |
5141 | return NULL_RTX; | |
5142 | ||
5143 | /* Expand the operands. */ | |
5144 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5145 | ||
5146 | expect = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
5147 | expect = convert_memory_address (Pmode, expect); | |
c401b131 | 5148 | expect = gen_rtx_MEM (mode, expect); |
1cd6e20d | 5149 | desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); |
5150 | ||
5151 | weak = CALL_EXPR_ARG (exp, 3); | |
5152 | is_weak = false; | |
e913b5cd | 5153 | if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0) |
1cd6e20d | 5154 | is_weak = true; |
5155 | ||
d86e3752 | 5156 | if (target == const0_rtx) |
5157 | target = NULL; | |
d86e3752 | 5158 | |
3c29a9ea | 5159 | /* Lest the rtl backend create a race condition with an imporoper store |
5160 | to memory, always create a new pseudo for OLDVAL. */ | |
5161 | oldval = NULL; | |
5162 | ||
5163 | if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired, | |
ba885f6a | 5164 | is_weak, success, failure)) |
1cd6e20d | 5165 | return NULL_RTX; |
5166 | ||
d86e3752 | 5167 | /* Conditionally store back to EXPECT, lest we create a race condition |
5168 | with an improper store to memory. */ | |
5169 | /* ??? With a rearrangement of atomics at the gimple level, we can handle | |
5170 | the normal case where EXPECT is totally private, i.e. a register. At | |
5171 | which point the store can be unconditional. */ | |
5172 | label = gen_label_rtx (); | |
62589f76 | 5173 | emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, |
5174 | GET_MODE (target), 1, label); | |
d86e3752 | 5175 | emit_move_insn (expect, oldval); |
5176 | emit_label (label); | |
c401b131 | 5177 | |
1cd6e20d | 5178 | return target; |
5179 | } | |
5180 | ||
5181 | /* Expand the __atomic_load intrinsic: | |
5182 | TYPE __atomic_load (TYPE *object, enum memmodel) | |
5183 | EXP is the CALL_EXPR. | |
5184 | TARGET is an optional place for us to store the results. */ | |
5185 | ||
5186 | static rtx | |
3754d046 | 5187 | expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target) |
1cd6e20d | 5188 | { |
5189 | rtx mem; | |
5190 | enum memmodel model; | |
5191 | ||
5192 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
a372f7ca | 5193 | if (is_mm_release (model) || is_mm_acq_rel (model)) |
1cd6e20d | 5194 | { |
2cb724f9 | 5195 | source_location loc |
5196 | = expansion_point_location_if_in_system_header (input_location); | |
5197 | warning_at (loc, OPT_Winvalid_memory_model, | |
5198 | "invalid memory model for %<__atomic_load%>"); | |
086f4e33 | 5199 | model = MEMMODEL_SEQ_CST; |
1cd6e20d | 5200 | } |
5201 | ||
5202 | if (!flag_inline_atomics) | |
5203 | return NULL_RTX; | |
5204 | ||
5205 | /* Expand the operand. */ | |
5206 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5207 | ||
5208 | return expand_atomic_load (target, mem, model); | |
5209 | } | |
5210 | ||
5211 | ||
5212 | /* Expand the __atomic_store intrinsic: | |
5213 | void __atomic_store (TYPE *object, TYPE desired, enum memmodel) | |
5214 | EXP is the CALL_EXPR. | |
5215 | TARGET is an optional place for us to store the results. */ | |
5216 | ||
5217 | static rtx | |
3754d046 | 5218 | expand_builtin_atomic_store (machine_mode mode, tree exp) |
1cd6e20d | 5219 | { |
5220 | rtx mem, val; | |
5221 | enum memmodel model; | |
5222 | ||
5223 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
a372f7ca | 5224 | if (!(is_mm_relaxed (model) || is_mm_seq_cst (model) |
5225 | || is_mm_release (model))) | |
1cd6e20d | 5226 | { |
2cb724f9 | 5227 | source_location loc |
5228 | = expansion_point_location_if_in_system_header (input_location); | |
5229 | warning_at (loc, OPT_Winvalid_memory_model, | |
5230 | "invalid memory model for %<__atomic_store%>"); | |
086f4e33 | 5231 | model = MEMMODEL_SEQ_CST; |
1cd6e20d | 5232 | } |
5233 | ||
5234 | if (!flag_inline_atomics) | |
5235 | return NULL_RTX; | |
5236 | ||
5237 | /* Expand the operands. */ | |
5238 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5239 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5240 | ||
8808bf16 | 5241 | return expand_atomic_store (mem, val, model, false); |
1cd6e20d | 5242 | } |
5243 | ||
5244 | /* Expand the __atomic_fetch_XXX intrinsic: | |
5245 | TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel) | |
5246 | EXP is the CALL_EXPR. | |
5247 | TARGET is an optional place for us to store the results. | |
5248 | CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR. | |
5249 | FETCH_AFTER is true if returning the result of the operation. | |
5250 | FETCH_AFTER is false if returning the value before the operation. | |
5251 | IGNORE is true if the result is not used. | |
5252 | EXT_CALL is the correct builtin for an external call if this cannot be | |
5253 | resolved to an instruction sequence. */ | |
5254 | ||
5255 | static rtx | |
3754d046 | 5256 | expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target, |
1cd6e20d | 5257 | enum rtx_code code, bool fetch_after, |
5258 | bool ignore, enum built_in_function ext_call) | |
5259 | { | |
5260 | rtx val, mem, ret; | |
5261 | enum memmodel model; | |
5262 | tree fndecl; | |
5263 | tree addr; | |
5264 | ||
5265 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
5266 | ||
5267 | /* Expand the operands. */ | |
5268 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5269 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5270 | ||
5271 | /* Only try generating instructions if inlining is turned on. */ | |
5272 | if (flag_inline_atomics) | |
5273 | { | |
5274 | ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after); | |
5275 | if (ret) | |
5276 | return ret; | |
5277 | } | |
5278 | ||
5279 | /* Return if a different routine isn't needed for the library call. */ | |
5280 | if (ext_call == BUILT_IN_NONE) | |
5281 | return NULL_RTX; | |
5282 | ||
5283 | /* Change the call to the specified function. */ | |
5284 | fndecl = get_callee_fndecl (exp); | |
5285 | addr = CALL_EXPR_FN (exp); | |
5286 | STRIP_NOPS (addr); | |
5287 | ||
5288 | gcc_assert (TREE_OPERAND (addr, 0) == fndecl); | |
9af5ce0c | 5289 | TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call); |
1cd6e20d | 5290 | |
5291 | /* Expand the call here so we can emit trailing code. */ | |
5292 | ret = expand_call (exp, target, ignore); | |
5293 | ||
5294 | /* Replace the original function just in case it matters. */ | |
5295 | TREE_OPERAND (addr, 0) = fndecl; | |
5296 | ||
5297 | /* Then issue the arithmetic correction to return the right result. */ | |
5298 | if (!ignore) | |
c449f851 | 5299 | { |
5300 | if (code == NOT) | |
5301 | { | |
5302 | ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true, | |
5303 | OPTAB_LIB_WIDEN); | |
5304 | ret = expand_simple_unop (mode, NOT, ret, target, true); | |
5305 | } | |
5306 | else | |
5307 | ret = expand_simple_binop (mode, code, ret, val, target, true, | |
5308 | OPTAB_LIB_WIDEN); | |
5309 | } | |
1cd6e20d | 5310 | return ret; |
5311 | } | |
5312 | ||
10b744a3 | 5313 | /* Expand an atomic clear operation. |
5314 | void _atomic_clear (BOOL *obj, enum memmodel) | |
5315 | EXP is the call expression. */ | |
5316 | ||
5317 | static rtx | |
5318 | expand_builtin_atomic_clear (tree exp) | |
5319 | { | |
3754d046 | 5320 | machine_mode mode; |
10b744a3 | 5321 | rtx mem, ret; |
5322 | enum memmodel model; | |
5323 | ||
5324 | mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0); | |
5325 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5326 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
5327 | ||
a372f7ca | 5328 | if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model)) |
10b744a3 | 5329 | { |
2cb724f9 | 5330 | source_location loc |
5331 | = expansion_point_location_if_in_system_header (input_location); | |
5332 | warning_at (loc, OPT_Winvalid_memory_model, | |
5333 | "invalid memory model for %<__atomic_store%>"); | |
086f4e33 | 5334 | model = MEMMODEL_SEQ_CST; |
10b744a3 | 5335 | } |
5336 | ||
5337 | /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release. | |
5338 | Failing that, a store is issued by __atomic_store. The only way this can | |
5339 | fail is if the bool type is larger than a word size. Unlikely, but | |
5340 | handle it anyway for completeness. Assume a single threaded model since | |
5341 | there is no atomic support in this case, and no barriers are required. */ | |
5342 | ret = expand_atomic_store (mem, const0_rtx, model, true); | |
5343 | if (!ret) | |
5344 | emit_move_insn (mem, const0_rtx); | |
5345 | return const0_rtx; | |
5346 | } | |
5347 | ||
5348 | /* Expand an atomic test_and_set operation. | |
5349 | bool _atomic_test_and_set (BOOL *obj, enum memmodel) | |
5350 | EXP is the call expression. */ | |
5351 | ||
5352 | static rtx | |
7821cde1 | 5353 | expand_builtin_atomic_test_and_set (tree exp, rtx target) |
10b744a3 | 5354 | { |
7821cde1 | 5355 | rtx mem; |
10b744a3 | 5356 | enum memmodel model; |
3754d046 | 5357 | machine_mode mode; |
10b744a3 | 5358 | |
5359 | mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0); | |
5360 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5361 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
5362 | ||
7821cde1 | 5363 | return expand_atomic_test_and_set (target, mem, model); |
10b744a3 | 5364 | } |
5365 | ||
5366 | ||
1cd6e20d | 5367 | /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on |
5368 | this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */ | |
5369 | ||
5370 | static tree | |
5371 | fold_builtin_atomic_always_lock_free (tree arg0, tree arg1) | |
5372 | { | |
5373 | int size; | |
3754d046 | 5374 | machine_mode mode; |
1cd6e20d | 5375 | unsigned int mode_align, type_align; |
5376 | ||
5377 | if (TREE_CODE (arg0) != INTEGER_CST) | |
5378 | return NULL_TREE; | |
b6a5fc45 | 5379 | |
1cd6e20d | 5380 | size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT; |
5381 | mode = mode_for_size (size, MODE_INT, 0); | |
5382 | mode_align = GET_MODE_ALIGNMENT (mode); | |
5383 | ||
4ca99588 | 5384 | if (TREE_CODE (arg1) == INTEGER_CST) |
5385 | { | |
5386 | unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1)); | |
5387 | ||
5388 | /* Either this argument is null, or it's a fake pointer encoding | |
5389 | the alignment of the object. */ | |
5390 | val = val & -val; | |
5391 | val *= BITS_PER_UNIT; | |
5392 | ||
5393 | if (val == 0 || mode_align < val) | |
5394 | type_align = mode_align; | |
5395 | else | |
5396 | type_align = val; | |
5397 | } | |
1cd6e20d | 5398 | else |
5399 | { | |
5400 | tree ttype = TREE_TYPE (arg1); | |
5401 | ||
5402 | /* This function is usually invoked and folded immediately by the front | |
5403 | end before anything else has a chance to look at it. The pointer | |
5404 | parameter at this point is usually cast to a void *, so check for that | |
5405 | and look past the cast. */ | |
d09ef31a | 5406 | if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype) |
1cd6e20d | 5407 | && VOID_TYPE_P (TREE_TYPE (ttype))) |
5408 | arg1 = TREE_OPERAND (arg1, 0); | |
5409 | ||
5410 | ttype = TREE_TYPE (arg1); | |
5411 | gcc_assert (POINTER_TYPE_P (ttype)); | |
5412 | ||
5413 | /* Get the underlying type of the object. */ | |
5414 | ttype = TREE_TYPE (ttype); | |
5415 | type_align = TYPE_ALIGN (ttype); | |
5416 | } | |
5417 | ||
47ae02b7 | 5418 | /* If the object has smaller alignment, the lock free routines cannot |
1cd6e20d | 5419 | be used. */ |
5420 | if (type_align < mode_align) | |
06308d2a | 5421 | return boolean_false_node; |
1cd6e20d | 5422 | |
5423 | /* Check if a compare_and_swap pattern exists for the mode which represents | |
5424 | the required size. The pattern is not allowed to fail, so the existence | |
5425 | of the pattern indicates support is present. */ | |
29139cdc | 5426 | if (can_compare_and_swap_p (mode, true)) |
06308d2a | 5427 | return boolean_true_node; |
1cd6e20d | 5428 | else |
06308d2a | 5429 | return boolean_false_node; |
1cd6e20d | 5430 | } |
5431 | ||
5432 | /* Return true if the parameters to call EXP represent an object which will | |
5433 | always generate lock free instructions. The first argument represents the | |
5434 | size of the object, and the second parameter is a pointer to the object | |
5435 | itself. If NULL is passed for the object, then the result is based on | |
5436 | typical alignment for an object of the specified size. Otherwise return | |
5437 | false. */ | |
5438 | ||
5439 | static rtx | |
5440 | expand_builtin_atomic_always_lock_free (tree exp) | |
5441 | { | |
5442 | tree size; | |
5443 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
5444 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
5445 | ||
5446 | if (TREE_CODE (arg0) != INTEGER_CST) | |
5447 | { | |
5448 | error ("non-constant argument 1 to __atomic_always_lock_free"); | |
5449 | return const0_rtx; | |
5450 | } | |
5451 | ||
5452 | size = fold_builtin_atomic_always_lock_free (arg0, arg1); | |
06308d2a | 5453 | if (size == boolean_true_node) |
1cd6e20d | 5454 | return const1_rtx; |
5455 | return const0_rtx; | |
5456 | } | |
5457 | ||
5458 | /* Return a one or zero if it can be determined that object ARG1 of size ARG | |
5459 | is lock free on this architecture. */ | |
5460 | ||
5461 | static tree | |
5462 | fold_builtin_atomic_is_lock_free (tree arg0, tree arg1) | |
5463 | { | |
5464 | if (!flag_inline_atomics) | |
5465 | return NULL_TREE; | |
5466 | ||
5467 | /* If it isn't always lock free, don't generate a result. */ | |
06308d2a | 5468 | if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node) |
5469 | return boolean_true_node; | |
1cd6e20d | 5470 | |
5471 | return NULL_TREE; | |
5472 | } | |
5473 | ||
5474 | /* Return true if the parameters to call EXP represent an object which will | |
5475 | always generate lock free instructions. The first argument represents the | |
5476 | size of the object, and the second parameter is a pointer to the object | |
5477 | itself. If NULL is passed for the object, then the result is based on | |
5478 | typical alignment for an object of the specified size. Otherwise return | |
5479 | NULL*/ | |
5480 | ||
5481 | static rtx | |
5482 | expand_builtin_atomic_is_lock_free (tree exp) | |
5483 | { | |
5484 | tree size; | |
5485 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
5486 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
5487 | ||
5488 | if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
5489 | { | |
5490 | error ("non-integer argument 1 to __atomic_is_lock_free"); | |
5491 | return NULL_RTX; | |
5492 | } | |
5493 | ||
5494 | if (!flag_inline_atomics) | |
5495 | return NULL_RTX; | |
5496 | ||
5497 | /* If the value is known at compile time, return the RTX for it. */ | |
5498 | size = fold_builtin_atomic_is_lock_free (arg0, arg1); | |
06308d2a | 5499 | if (size == boolean_true_node) |
1cd6e20d | 5500 | return const1_rtx; |
5501 | ||
5502 | return NULL_RTX; | |
5503 | } | |
5504 | ||
1cd6e20d | 5505 | /* Expand the __atomic_thread_fence intrinsic: |
5506 | void __atomic_thread_fence (enum memmodel) | |
5507 | EXP is the CALL_EXPR. */ | |
5508 | ||
5509 | static void | |
5510 | expand_builtin_atomic_thread_fence (tree exp) | |
5511 | { | |
fe54c06b | 5512 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
5513 | expand_mem_thread_fence (model); | |
1cd6e20d | 5514 | } |
5515 | ||
5516 | /* Expand the __atomic_signal_fence intrinsic: | |
5517 | void __atomic_signal_fence (enum memmodel) | |
5518 | EXP is the CALL_EXPR. */ | |
5519 | ||
5520 | static void | |
5521 | expand_builtin_atomic_signal_fence (tree exp) | |
5522 | { | |
fe54c06b | 5523 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
5524 | expand_mem_signal_fence (model); | |
b6a5fc45 | 5525 | } |
5526 | ||
5527 | /* Expand the __sync_synchronize intrinsic. */ | |
5528 | ||
5529 | static void | |
2797f13a | 5530 | expand_builtin_sync_synchronize (void) |
b6a5fc45 | 5531 | { |
a372f7ca | 5532 | expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST); |
b6a5fc45 | 5533 | } |
5534 | ||
badaa04c | 5535 | static rtx |
5536 | expand_builtin_thread_pointer (tree exp, rtx target) | |
5537 | { | |
5538 | enum insn_code icode; | |
5539 | if (!validate_arglist (exp, VOID_TYPE)) | |
5540 | return const0_rtx; | |
5541 | icode = direct_optab_handler (get_thread_pointer_optab, Pmode); | |
5542 | if (icode != CODE_FOR_nothing) | |
5543 | { | |
5544 | struct expand_operand op; | |
3ed779c3 | 5545 | /* If the target is not sutitable then create a new target. */ |
5546 | if (target == NULL_RTX | |
5547 | || !REG_P (target) | |
5548 | || GET_MODE (target) != Pmode) | |
badaa04c | 5549 | target = gen_reg_rtx (Pmode); |
5550 | create_output_operand (&op, target, Pmode); | |
5551 | expand_insn (icode, 1, &op); | |
5552 | return target; | |
5553 | } | |
5554 | error ("__builtin_thread_pointer is not supported on this target"); | |
5555 | return const0_rtx; | |
5556 | } | |
5557 | ||
5558 | static void | |
5559 | expand_builtin_set_thread_pointer (tree exp) | |
5560 | { | |
5561 | enum insn_code icode; | |
5562 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) | |
5563 | return; | |
5564 | icode = direct_optab_handler (set_thread_pointer_optab, Pmode); | |
5565 | if (icode != CODE_FOR_nothing) | |
5566 | { | |
5567 | struct expand_operand op; | |
5568 | rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, | |
5569 | Pmode, EXPAND_NORMAL); | |
6f343c10 | 5570 | create_input_operand (&op, val, Pmode); |
badaa04c | 5571 | expand_insn (icode, 1, &op); |
5572 | return; | |
5573 | } | |
5574 | error ("__builtin_set_thread_pointer is not supported on this target"); | |
5575 | } | |
5576 | ||
53800dbe | 5577 | \f |
0e80b01d | 5578 | /* Emit code to restore the current value of stack. */ |
5579 | ||
5580 | static void | |
5581 | expand_stack_restore (tree var) | |
5582 | { | |
1e0c0b35 | 5583 | rtx_insn *prev; |
5584 | rtx sa = expand_normal (var); | |
0e80b01d | 5585 | |
5586 | sa = convert_memory_address (Pmode, sa); | |
5587 | ||
5588 | prev = get_last_insn (); | |
5589 | emit_stack_restore (SAVE_BLOCK, sa); | |
97354ae4 | 5590 | |
5591 | record_new_stack_level (); | |
5592 | ||
0e80b01d | 5593 | fixup_args_size_notes (prev, get_last_insn (), 0); |
5594 | } | |
5595 | ||
0e80b01d | 5596 | /* Emit code to save the current value of stack. */ |
5597 | ||
5598 | static rtx | |
5599 | expand_stack_save (void) | |
5600 | { | |
5601 | rtx ret = NULL_RTX; | |
5602 | ||
0e80b01d | 5603 | emit_stack_save (SAVE_BLOCK, &ret); |
5604 | return ret; | |
5605 | } | |
5606 | ||
ca4c3545 | 5607 | |
53800dbe | 5608 | /* Expand an expression EXP that calls a built-in function, |
5609 | with result going to TARGET if that's convenient | |
5610 | (and in mode MODE if that's convenient). | |
5611 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
5612 | IGNORE is nonzero if the value is to be ignored. */ | |
5613 | ||
5614 | rtx | |
3754d046 | 5615 | expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode, |
aecda0d6 | 5616 | int ignore) |
53800dbe | 5617 | { |
c6e6ecb1 | 5618 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 5619 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
3754d046 | 5620 | machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
67fa4078 | 5621 | int flags; |
53800dbe | 5622 | |
4e2f4ed5 | 5623 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
5624 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); | |
5625 | ||
f9acf11a | 5626 | /* When ASan is enabled, we don't want to expand some memory/string |
5627 | builtins and rely on libsanitizer's hooks. This allows us to avoid | |
5628 | redundant checks and be sure, that possible overflow will be detected | |
5629 | by ASan. */ | |
5630 | ||
5631 | if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode)) | |
5632 | return expand_call (exp, target, ignore); | |
5633 | ||
53800dbe | 5634 | /* When not optimizing, generate calls to library functions for a certain |
5635 | set of builtins. */ | |
cd9ff771 | 5636 | if (!optimize |
b6a5fc45 | 5637 | && !called_as_built_in (fndecl) |
73037a1e | 5638 | && fcode != BUILT_IN_FORK |
5639 | && fcode != BUILT_IN_EXECL | |
5640 | && fcode != BUILT_IN_EXECV | |
5641 | && fcode != BUILT_IN_EXECLP | |
5642 | && fcode != BUILT_IN_EXECLE | |
5643 | && fcode != BUILT_IN_EXECVP | |
5644 | && fcode != BUILT_IN_EXECVE | |
2c281b15 | 5645 | && fcode != BUILT_IN_ALLOCA |
581bf1c2 | 5646 | && fcode != BUILT_IN_ALLOCA_WITH_ALIGN |
058a1b7a | 5647 | && fcode != BUILT_IN_FREE |
5648 | && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS | |
5649 | && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS | |
5650 | && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS | |
5651 | && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS | |
5652 | && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS | |
5653 | && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS | |
5654 | && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS | |
5655 | && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS | |
5656 | && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS | |
5657 | && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND | |
5658 | && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND | |
5659 | && fcode != BUILT_IN_CHKP_BNDRET) | |
cd9ff771 | 5660 | return expand_call (exp, target, ignore); |
53800dbe | 5661 | |
8d6d7930 | 5662 | /* The built-in function expanders test for target == const0_rtx |
5663 | to determine whether the function's result will be ignored. */ | |
5664 | if (ignore) | |
5665 | target = const0_rtx; | |
5666 | ||
5667 | /* If the result of a pure or const built-in function is ignored, and | |
5668 | none of its arguments are volatile, we can avoid expanding the | |
5669 | built-in call and just evaluate the arguments for side-effects. */ | |
5670 | if (target == const0_rtx | |
67fa4078 | 5671 | && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE)) |
5672 | && !(flags & ECF_LOOPING_CONST_OR_PURE)) | |
8d6d7930 | 5673 | { |
5674 | bool volatilep = false; | |
5675 | tree arg; | |
c2f47e15 | 5676 | call_expr_arg_iterator iter; |
8d6d7930 | 5677 | |
c2f47e15 | 5678 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
5679 | if (TREE_THIS_VOLATILE (arg)) | |
8d6d7930 | 5680 | { |
5681 | volatilep = true; | |
5682 | break; | |
5683 | } | |
5684 | ||
5685 | if (! volatilep) | |
5686 | { | |
c2f47e15 | 5687 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
5688 | expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
8d6d7930 | 5689 | return const0_rtx; |
5690 | } | |
5691 | } | |
5692 | ||
f21337ef | 5693 | /* expand_builtin_with_bounds is supposed to be used for |
5694 | instrumented builtin calls. */ | |
058a1b7a | 5695 | gcc_assert (!CALL_WITH_BOUNDS_P (exp)); |
5696 | ||
53800dbe | 5697 | switch (fcode) |
5698 | { | |
4f35b1fc | 5699 | CASE_FLT_FN (BUILT_IN_FABS): |
8aa32773 | 5700 | case BUILT_IN_FABSD32: |
5701 | case BUILT_IN_FABSD64: | |
5702 | case BUILT_IN_FABSD128: | |
c2f47e15 | 5703 | target = expand_builtin_fabs (exp, target, subtarget); |
78a74442 | 5704 | if (target) |
a0c938f0 | 5705 | return target; |
78a74442 | 5706 | break; |
5707 | ||
4f35b1fc | 5708 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
c2f47e15 | 5709 | target = expand_builtin_copysign (exp, target, subtarget); |
270436f3 | 5710 | if (target) |
5711 | return target; | |
5712 | break; | |
5713 | ||
7d3f6cc7 | 5714 | /* Just do a normal library call if we were unable to fold |
5715 | the values. */ | |
4f35b1fc | 5716 | CASE_FLT_FN (BUILT_IN_CABS): |
78a74442 | 5717 | break; |
53800dbe | 5718 | |
7e0713b1 | 5719 | CASE_FLT_FN (BUILT_IN_FMA): |
5720 | target = expand_builtin_mathfn_ternary (exp, target, subtarget); | |
5721 | if (target) | |
5722 | return target; | |
5723 | break; | |
5724 | ||
a67a90e5 | 5725 | CASE_FLT_FN (BUILT_IN_ILOGB): |
5726 | if (! flag_unsafe_math_optimizations) | |
5727 | break; | |
69b779ea | 5728 | CASE_FLT_FN (BUILT_IN_ISINF): |
cde061c1 | 5729 | CASE_FLT_FN (BUILT_IN_FINITE): |
5730 | case BUILT_IN_ISFINITE: | |
8a1a9cb7 | 5731 | case BUILT_IN_ISNORMAL: |
f97eea22 | 5732 | target = expand_builtin_interclass_mathfn (exp, target); |
a67a90e5 | 5733 | if (target) |
5734 | return target; | |
5735 | break; | |
5736 | ||
80ff6494 | 5737 | CASE_FLT_FN (BUILT_IN_ICEIL): |
4f35b1fc | 5738 | CASE_FLT_FN (BUILT_IN_LCEIL): |
5739 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
5740 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
80ff6494 | 5741 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
4f35b1fc | 5742 | CASE_FLT_FN (BUILT_IN_LLFLOOR): |
ff1b14e4 | 5743 | target = expand_builtin_int_roundingfn (exp, target); |
ad52b9b7 | 5744 | if (target) |
5745 | return target; | |
5746 | break; | |
5747 | ||
80ff6494 | 5748 | CASE_FLT_FN (BUILT_IN_IRINT): |
7d3afc77 | 5749 | CASE_FLT_FN (BUILT_IN_LRINT): |
5750 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
80ff6494 | 5751 | CASE_FLT_FN (BUILT_IN_IROUND): |
ef2f1a10 | 5752 | CASE_FLT_FN (BUILT_IN_LROUND): |
5753 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
ff1b14e4 | 5754 | target = expand_builtin_int_roundingfn_2 (exp, target); |
7d3afc77 | 5755 | if (target) |
5756 | return target; | |
5757 | break; | |
5758 | ||
4f35b1fc | 5759 | CASE_FLT_FN (BUILT_IN_POWI): |
f97eea22 | 5760 | target = expand_builtin_powi (exp, target); |
757c219d | 5761 | if (target) |
5762 | return target; | |
5763 | break; | |
5764 | ||
d735c391 | 5765 | CASE_FLT_FN (BUILT_IN_CEXPI): |
f97eea22 | 5766 | target = expand_builtin_cexpi (exp, target); |
d735c391 | 5767 | gcc_assert (target); |
5768 | return target; | |
5769 | ||
4f35b1fc | 5770 | CASE_FLT_FN (BUILT_IN_SIN): |
5771 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 5772 | if (! flag_unsafe_math_optimizations) |
5773 | break; | |
5774 | target = expand_builtin_mathfn_3 (exp, target, subtarget); | |
5775 | if (target) | |
5776 | return target; | |
5777 | break; | |
5778 | ||
c3147c1a | 5779 | CASE_FLT_FN (BUILT_IN_SINCOS): |
5780 | if (! flag_unsafe_math_optimizations) | |
5781 | break; | |
5782 | target = expand_builtin_sincos (exp); | |
5783 | if (target) | |
5784 | return target; | |
5785 | break; | |
5786 | ||
53800dbe | 5787 | case BUILT_IN_APPLY_ARGS: |
5788 | return expand_builtin_apply_args (); | |
5789 | ||
5790 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
5791 | FUNCTION with a copy of the parameters described by | |
5792 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
5793 | allocated on the stack into which is stored all the registers | |
5794 | that might possibly be used for returning the result of a | |
5795 | function. ARGUMENTS is the value returned by | |
5796 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
5797 | arguments that must be copied. ??? How should this value be | |
5798 | computed? We'll also need a safe worst case value for varargs | |
5799 | functions. */ | |
5800 | case BUILT_IN_APPLY: | |
c2f47e15 | 5801 | if (!validate_arglist (exp, POINTER_TYPE, |
0eb671f7 | 5802 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) |
c2f47e15 | 5803 | && !validate_arglist (exp, REFERENCE_TYPE, |
0eb671f7 | 5804 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 5805 | return const0_rtx; |
5806 | else | |
5807 | { | |
53800dbe | 5808 | rtx ops[3]; |
5809 | ||
c2f47e15 | 5810 | ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0)); |
5811 | ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
5812 | ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2)); | |
53800dbe | 5813 | |
5814 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
5815 | } | |
5816 | ||
5817 | /* __builtin_return (RESULT) causes the function to return the | |
5818 | value described by RESULT. RESULT is address of the block of | |
5819 | memory returned by __builtin_apply. */ | |
5820 | case BUILT_IN_RETURN: | |
c2f47e15 | 5821 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
5822 | expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0))); | |
53800dbe | 5823 | return const0_rtx; |
5824 | ||
5825 | case BUILT_IN_SAVEREGS: | |
a66c9326 | 5826 | return expand_builtin_saveregs (); |
53800dbe | 5827 | |
48dc2227 | 5828 | case BUILT_IN_VA_ARG_PACK: |
5829 | /* All valid uses of __builtin_va_arg_pack () are removed during | |
5830 | inlining. */ | |
b8c23db3 | 5831 | error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp); |
48dc2227 | 5832 | return const0_rtx; |
5833 | ||
4e1d7ea4 | 5834 | case BUILT_IN_VA_ARG_PACK_LEN: |
5835 | /* All valid uses of __builtin_va_arg_pack_len () are removed during | |
5836 | inlining. */ | |
b8c23db3 | 5837 | error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp); |
4e1d7ea4 | 5838 | return const0_rtx; |
5839 | ||
53800dbe | 5840 | /* Return the address of the first anonymous stack arg. */ |
5841 | case BUILT_IN_NEXT_ARG: | |
c2f47e15 | 5842 | if (fold_builtin_next_arg (exp, false)) |
a0c938f0 | 5843 | return const0_rtx; |
79012a9d | 5844 | return expand_builtin_next_arg (); |
53800dbe | 5845 | |
ac8fb6db | 5846 | case BUILT_IN_CLEAR_CACHE: |
5847 | target = expand_builtin___clear_cache (exp); | |
5848 | if (target) | |
5849 | return target; | |
5850 | break; | |
5851 | ||
53800dbe | 5852 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 5853 | return expand_builtin_classify_type (exp); |
53800dbe | 5854 | |
5855 | case BUILT_IN_CONSTANT_P: | |
4ee9c684 | 5856 | return const0_rtx; |
53800dbe | 5857 | |
5858 | case BUILT_IN_FRAME_ADDRESS: | |
5859 | case BUILT_IN_RETURN_ADDRESS: | |
c2f47e15 | 5860 | return expand_builtin_frame_address (fndecl, exp); |
53800dbe | 5861 | |
5862 | /* Returns the address of the area where the structure is returned. | |
5863 | 0 otherwise. */ | |
5864 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
c2f47e15 | 5865 | if (call_expr_nargs (exp) != 0 |
9342ee68 | 5866 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
e16ceb8e | 5867 | || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl)))) |
9342ee68 | 5868 | return const0_rtx; |
53800dbe | 5869 | else |
9342ee68 | 5870 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
53800dbe | 5871 | |
5872 | case BUILT_IN_ALLOCA: | |
581bf1c2 | 5873 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
990495a7 | 5874 | /* If the allocation stems from the declaration of a variable-sized |
5875 | object, it cannot accumulate. */ | |
a882d754 | 5876 | target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp)); |
53800dbe | 5877 | if (target) |
5878 | return target; | |
5879 | break; | |
5880 | ||
4ee9c684 | 5881 | case BUILT_IN_STACK_SAVE: |
5882 | return expand_stack_save (); | |
5883 | ||
5884 | case BUILT_IN_STACK_RESTORE: | |
c2f47e15 | 5885 | expand_stack_restore (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 5886 | return const0_rtx; |
5887 | ||
74bdbe96 | 5888 | case BUILT_IN_BSWAP16: |
42791117 | 5889 | case BUILT_IN_BSWAP32: |
5890 | case BUILT_IN_BSWAP64: | |
74bdbe96 | 5891 | target = expand_builtin_bswap (target_mode, exp, target, subtarget); |
42791117 | 5892 | if (target) |
5893 | return target; | |
5894 | break; | |
5895 | ||
4f35b1fc | 5896 | CASE_INT_FN (BUILT_IN_FFS): |
c2f47e15 | 5897 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 5898 | subtarget, ffs_optab); |
6a08d0ab | 5899 | if (target) |
5900 | return target; | |
5901 | break; | |
5902 | ||
4f35b1fc | 5903 | CASE_INT_FN (BUILT_IN_CLZ): |
c2f47e15 | 5904 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 5905 | subtarget, clz_optab); |
6a08d0ab | 5906 | if (target) |
5907 | return target; | |
5908 | break; | |
5909 | ||
4f35b1fc | 5910 | CASE_INT_FN (BUILT_IN_CTZ): |
c2f47e15 | 5911 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 5912 | subtarget, ctz_optab); |
6a08d0ab | 5913 | if (target) |
5914 | return target; | |
5915 | break; | |
5916 | ||
d8492bd3 | 5917 | CASE_INT_FN (BUILT_IN_CLRSB): |
d8492bd3 | 5918 | target = expand_builtin_unop (target_mode, exp, target, |
5919 | subtarget, clrsb_optab); | |
5920 | if (target) | |
5921 | return target; | |
5922 | break; | |
5923 | ||
4f35b1fc | 5924 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
c2f47e15 | 5925 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 5926 | subtarget, popcount_optab); |
6a08d0ab | 5927 | if (target) |
5928 | return target; | |
5929 | break; | |
5930 | ||
4f35b1fc | 5931 | CASE_INT_FN (BUILT_IN_PARITY): |
c2f47e15 | 5932 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 5933 | subtarget, parity_optab); |
53800dbe | 5934 | if (target) |
5935 | return target; | |
5936 | break; | |
5937 | ||
5938 | case BUILT_IN_STRLEN: | |
c2f47e15 | 5939 | target = expand_builtin_strlen (exp, target, target_mode); |
53800dbe | 5940 | if (target) |
5941 | return target; | |
5942 | break; | |
5943 | ||
5944 | case BUILT_IN_STRCPY: | |
a65c4d64 | 5945 | target = expand_builtin_strcpy (exp, target); |
53800dbe | 5946 | if (target) |
5947 | return target; | |
5948 | break; | |
bf8e3599 | 5949 | |
ed09096d | 5950 | case BUILT_IN_STRNCPY: |
a65c4d64 | 5951 | target = expand_builtin_strncpy (exp, target); |
ed09096d | 5952 | if (target) |
5953 | return target; | |
5954 | break; | |
bf8e3599 | 5955 | |
3b824fa6 | 5956 | case BUILT_IN_STPCPY: |
dc369150 | 5957 | target = expand_builtin_stpcpy (exp, target, mode); |
3b824fa6 | 5958 | if (target) |
5959 | return target; | |
5960 | break; | |
5961 | ||
53800dbe | 5962 | case BUILT_IN_MEMCPY: |
a65c4d64 | 5963 | target = expand_builtin_memcpy (exp, target); |
3b824fa6 | 5964 | if (target) |
5965 | return target; | |
5966 | break; | |
5967 | ||
5968 | case BUILT_IN_MEMPCPY: | |
c2f47e15 | 5969 | target = expand_builtin_mempcpy (exp, target, mode); |
53800dbe | 5970 | if (target) |
5971 | return target; | |
5972 | break; | |
5973 | ||
5974 | case BUILT_IN_MEMSET: | |
c2f47e15 | 5975 | target = expand_builtin_memset (exp, target, mode); |
53800dbe | 5976 | if (target) |
5977 | return target; | |
5978 | break; | |
5979 | ||
ffc83088 | 5980 | case BUILT_IN_BZERO: |
0b25db21 | 5981 | target = expand_builtin_bzero (exp); |
ffc83088 | 5982 | if (target) |
5983 | return target; | |
5984 | break; | |
5985 | ||
53800dbe | 5986 | case BUILT_IN_STRCMP: |
a65c4d64 | 5987 | target = expand_builtin_strcmp (exp, target); |
53800dbe | 5988 | if (target) |
5989 | return target; | |
5990 | break; | |
5991 | ||
ed09096d | 5992 | case BUILT_IN_STRNCMP: |
5993 | target = expand_builtin_strncmp (exp, target, mode); | |
5994 | if (target) | |
5995 | return target; | |
5996 | break; | |
5997 | ||
071f1696 | 5998 | case BUILT_IN_BCMP: |
53800dbe | 5999 | case BUILT_IN_MEMCMP: |
ea368aac | 6000 | target = expand_builtin_memcmp (exp, target); |
53800dbe | 6001 | if (target) |
6002 | return target; | |
6003 | break; | |
53800dbe | 6004 | |
6005 | case BUILT_IN_SETJMP: | |
2c8a1497 | 6006 | /* This should have been lowered to the builtins below. */ |
6007 | gcc_unreachable (); | |
6008 | ||
6009 | case BUILT_IN_SETJMP_SETUP: | |
6010 | /* __builtin_setjmp_setup is passed a pointer to an array of five words | |
6011 | and the receiver label. */ | |
c2f47e15 | 6012 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 6013 | { |
c2f47e15 | 6014 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
2c8a1497 | 6015 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 6016 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0); |
9ed997be | 6017 | rtx_insn *label_r = label_rtx (label); |
2c8a1497 | 6018 | |
6019 | /* This is copied from the handling of non-local gotos. */ | |
6020 | expand_builtin_setjmp_setup (buf_addr, label_r); | |
6021 | nonlocal_goto_handler_labels | |
a4de1c23 | 6022 | = gen_rtx_INSN_LIST (VOIDmode, label_r, |
2c8a1497 | 6023 | nonlocal_goto_handler_labels); |
6024 | /* ??? Do not let expand_label treat us as such since we would | |
6025 | not want to be both on the list of non-local labels and on | |
6026 | the list of forced labels. */ | |
6027 | FORCED_LABEL (label) = 0; | |
6028 | return const0_rtx; | |
6029 | } | |
6030 | break; | |
6031 | ||
2c8a1497 | 6032 | case BUILT_IN_SETJMP_RECEIVER: |
6033 | /* __builtin_setjmp_receiver is passed the receiver label. */ | |
c2f47e15 | 6034 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 6035 | { |
c2f47e15 | 6036 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0); |
9ed997be | 6037 | rtx_insn *label_r = label_rtx (label); |
2c8a1497 | 6038 | |
6039 | expand_builtin_setjmp_receiver (label_r); | |
6040 | return const0_rtx; | |
6041 | } | |
6b7f6858 | 6042 | break; |
53800dbe | 6043 | |
6044 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
6045 | It's similar to the C library longjmp function but works with | |
6046 | __builtin_setjmp above. */ | |
6047 | case BUILT_IN_LONGJMP: | |
c2f47e15 | 6048 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 6049 | { |
c2f47e15 | 6050 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
8ec3c5c2 | 6051 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 6052 | rtx value = expand_normal (CALL_EXPR_ARG (exp, 1)); |
53800dbe | 6053 | |
6054 | if (value != const1_rtx) | |
6055 | { | |
1e5fcbe2 | 6056 | error ("%<__builtin_longjmp%> second argument must be 1"); |
53800dbe | 6057 | return const0_rtx; |
6058 | } | |
6059 | ||
6060 | expand_builtin_longjmp (buf_addr, value); | |
6061 | return const0_rtx; | |
6062 | } | |
2c8a1497 | 6063 | break; |
53800dbe | 6064 | |
4ee9c684 | 6065 | case BUILT_IN_NONLOCAL_GOTO: |
c2f47e15 | 6066 | target = expand_builtin_nonlocal_goto (exp); |
4ee9c684 | 6067 | if (target) |
6068 | return target; | |
6069 | break; | |
6070 | ||
843d08a9 | 6071 | /* This updates the setjmp buffer that is its argument with the value |
6072 | of the current stack pointer. */ | |
6073 | case BUILT_IN_UPDATE_SETJMP_BUF: | |
c2f47e15 | 6074 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
843d08a9 | 6075 | { |
6076 | rtx buf_addr | |
c2f47e15 | 6077 | = expand_normal (CALL_EXPR_ARG (exp, 0)); |
843d08a9 | 6078 | |
6079 | expand_builtin_update_setjmp_buf (buf_addr); | |
6080 | return const0_rtx; | |
6081 | } | |
6082 | break; | |
6083 | ||
53800dbe | 6084 | case BUILT_IN_TRAP: |
a0ef1725 | 6085 | expand_builtin_trap (); |
53800dbe | 6086 | return const0_rtx; |
6087 | ||
d2b48f0c | 6088 | case BUILT_IN_UNREACHABLE: |
6089 | expand_builtin_unreachable (); | |
6090 | return const0_rtx; | |
6091 | ||
4f35b1fc | 6092 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
004e23c4 | 6093 | case BUILT_IN_SIGNBITD32: |
6094 | case BUILT_IN_SIGNBITD64: | |
6095 | case BUILT_IN_SIGNBITD128: | |
27f261ef | 6096 | target = expand_builtin_signbit (exp, target); |
6097 | if (target) | |
6098 | return target; | |
6099 | break; | |
6100 | ||
53800dbe | 6101 | /* Various hooks for the DWARF 2 __throw routine. */ |
6102 | case BUILT_IN_UNWIND_INIT: | |
6103 | expand_builtin_unwind_init (); | |
6104 | return const0_rtx; | |
6105 | case BUILT_IN_DWARF_CFA: | |
6106 | return virtual_cfa_rtx; | |
6107 | #ifdef DWARF2_UNWIND_INFO | |
f8f023a5 | 6108 | case BUILT_IN_DWARF_SP_COLUMN: |
6109 | return expand_builtin_dwarf_sp_column (); | |
695e919b | 6110 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
c2f47e15 | 6111 | expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0)); |
695e919b | 6112 | return const0_rtx; |
53800dbe | 6113 | #endif |
6114 | case BUILT_IN_FROB_RETURN_ADDR: | |
c2f47e15 | 6115 | return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 6116 | case BUILT_IN_EXTRACT_RETURN_ADDR: |
c2f47e15 | 6117 | return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 6118 | case BUILT_IN_EH_RETURN: |
c2f47e15 | 6119 | expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0), |
6120 | CALL_EXPR_ARG (exp, 1)); | |
53800dbe | 6121 | return const0_rtx; |
df4b504c | 6122 | case BUILT_IN_EH_RETURN_DATA_REGNO: |
c2f47e15 | 6123 | return expand_builtin_eh_return_data_regno (exp); |
26093bf4 | 6124 | case BUILT_IN_EXTEND_POINTER: |
c2f47e15 | 6125 | return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0)); |
e38def9c | 6126 | case BUILT_IN_EH_POINTER: |
6127 | return expand_builtin_eh_pointer (exp); | |
6128 | case BUILT_IN_EH_FILTER: | |
6129 | return expand_builtin_eh_filter (exp); | |
6130 | case BUILT_IN_EH_COPY_VALUES: | |
6131 | return expand_builtin_eh_copy_values (exp); | |
26093bf4 | 6132 | |
7ccc713a | 6133 | case BUILT_IN_VA_START: |
c2f47e15 | 6134 | return expand_builtin_va_start (exp); |
a66c9326 | 6135 | case BUILT_IN_VA_END: |
c2f47e15 | 6136 | return expand_builtin_va_end (exp); |
a66c9326 | 6137 | case BUILT_IN_VA_COPY: |
c2f47e15 | 6138 | return expand_builtin_va_copy (exp); |
89cfe6e5 | 6139 | case BUILT_IN_EXPECT: |
c2f47e15 | 6140 | return expand_builtin_expect (exp, target); |
fca0886c | 6141 | case BUILT_IN_ASSUME_ALIGNED: |
6142 | return expand_builtin_assume_aligned (exp, target); | |
5e3608d8 | 6143 | case BUILT_IN_PREFETCH: |
c2f47e15 | 6144 | expand_builtin_prefetch (exp); |
5e3608d8 | 6145 | return const0_rtx; |
6146 | ||
4ee9c684 | 6147 | case BUILT_IN_INIT_TRAMPOLINE: |
c307f106 | 6148 | return expand_builtin_init_trampoline (exp, true); |
6149 | case BUILT_IN_INIT_HEAP_TRAMPOLINE: | |
6150 | return expand_builtin_init_trampoline (exp, false); | |
4ee9c684 | 6151 | case BUILT_IN_ADJUST_TRAMPOLINE: |
c2f47e15 | 6152 | return expand_builtin_adjust_trampoline (exp); |
4ee9c684 | 6153 | |
73673831 | 6154 | case BUILT_IN_FORK: |
6155 | case BUILT_IN_EXECL: | |
6156 | case BUILT_IN_EXECV: | |
6157 | case BUILT_IN_EXECLP: | |
6158 | case BUILT_IN_EXECLE: | |
6159 | case BUILT_IN_EXECVP: | |
6160 | case BUILT_IN_EXECVE: | |
c2f47e15 | 6161 | target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore); |
73673831 | 6162 | if (target) |
6163 | return target; | |
6164 | break; | |
53800dbe | 6165 | |
2797f13a | 6166 | case BUILT_IN_SYNC_FETCH_AND_ADD_1: |
6167 | case BUILT_IN_SYNC_FETCH_AND_ADD_2: | |
6168 | case BUILT_IN_SYNC_FETCH_AND_ADD_4: | |
6169 | case BUILT_IN_SYNC_FETCH_AND_ADD_8: | |
6170 | case BUILT_IN_SYNC_FETCH_AND_ADD_16: | |
6171 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1); | |
1cd6e20d | 6172 | target = expand_builtin_sync_operation (mode, exp, PLUS, false, target); |
b6a5fc45 | 6173 | if (target) |
6174 | return target; | |
6175 | break; | |
6176 | ||
2797f13a | 6177 | case BUILT_IN_SYNC_FETCH_AND_SUB_1: |
6178 | case BUILT_IN_SYNC_FETCH_AND_SUB_2: | |
6179 | case BUILT_IN_SYNC_FETCH_AND_SUB_4: | |
6180 | case BUILT_IN_SYNC_FETCH_AND_SUB_8: | |
6181 | case BUILT_IN_SYNC_FETCH_AND_SUB_16: | |
6182 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1); | |
1cd6e20d | 6183 | target = expand_builtin_sync_operation (mode, exp, MINUS, false, target); |
b6a5fc45 | 6184 | if (target) |
6185 | return target; | |
6186 | break; | |
6187 | ||
2797f13a | 6188 | case BUILT_IN_SYNC_FETCH_AND_OR_1: |
6189 | case BUILT_IN_SYNC_FETCH_AND_OR_2: | |
6190 | case BUILT_IN_SYNC_FETCH_AND_OR_4: | |
6191 | case BUILT_IN_SYNC_FETCH_AND_OR_8: | |
6192 | case BUILT_IN_SYNC_FETCH_AND_OR_16: | |
6193 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1); | |
1cd6e20d | 6194 | target = expand_builtin_sync_operation (mode, exp, IOR, false, target); |
b6a5fc45 | 6195 | if (target) |
6196 | return target; | |
6197 | break; | |
6198 | ||
2797f13a | 6199 | case BUILT_IN_SYNC_FETCH_AND_AND_1: |
6200 | case BUILT_IN_SYNC_FETCH_AND_AND_2: | |
6201 | case BUILT_IN_SYNC_FETCH_AND_AND_4: | |
6202 | case BUILT_IN_SYNC_FETCH_AND_AND_8: | |
6203 | case BUILT_IN_SYNC_FETCH_AND_AND_16: | |
6204 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1); | |
1cd6e20d | 6205 | target = expand_builtin_sync_operation (mode, exp, AND, false, target); |
b6a5fc45 | 6206 | if (target) |
6207 | return target; | |
6208 | break; | |
6209 | ||
2797f13a | 6210 | case BUILT_IN_SYNC_FETCH_AND_XOR_1: |
6211 | case BUILT_IN_SYNC_FETCH_AND_XOR_2: | |
6212 | case BUILT_IN_SYNC_FETCH_AND_XOR_4: | |
6213 | case BUILT_IN_SYNC_FETCH_AND_XOR_8: | |
6214 | case BUILT_IN_SYNC_FETCH_AND_XOR_16: | |
6215 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1); | |
1cd6e20d | 6216 | target = expand_builtin_sync_operation (mode, exp, XOR, false, target); |
b6a5fc45 | 6217 | if (target) |
6218 | return target; | |
6219 | break; | |
6220 | ||
2797f13a | 6221 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
6222 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
6223 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
6224 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
6225 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
6226 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1); | |
1cd6e20d | 6227 | target = expand_builtin_sync_operation (mode, exp, NOT, false, target); |
b6a5fc45 | 6228 | if (target) |
6229 | return target; | |
6230 | break; | |
6231 | ||
2797f13a | 6232 | case BUILT_IN_SYNC_ADD_AND_FETCH_1: |
6233 | case BUILT_IN_SYNC_ADD_AND_FETCH_2: | |
6234 | case BUILT_IN_SYNC_ADD_AND_FETCH_4: | |
6235 | case BUILT_IN_SYNC_ADD_AND_FETCH_8: | |
6236 | case BUILT_IN_SYNC_ADD_AND_FETCH_16: | |
6237 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1); | |
1cd6e20d | 6238 | target = expand_builtin_sync_operation (mode, exp, PLUS, true, target); |
b6a5fc45 | 6239 | if (target) |
6240 | return target; | |
6241 | break; | |
6242 | ||
2797f13a | 6243 | case BUILT_IN_SYNC_SUB_AND_FETCH_1: |
6244 | case BUILT_IN_SYNC_SUB_AND_FETCH_2: | |
6245 | case BUILT_IN_SYNC_SUB_AND_FETCH_4: | |
6246 | case BUILT_IN_SYNC_SUB_AND_FETCH_8: | |
6247 | case BUILT_IN_SYNC_SUB_AND_FETCH_16: | |
6248 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1); | |
1cd6e20d | 6249 | target = expand_builtin_sync_operation (mode, exp, MINUS, true, target); |
b6a5fc45 | 6250 | if (target) |
6251 | return target; | |
6252 | break; | |
6253 | ||
2797f13a | 6254 | case BUILT_IN_SYNC_OR_AND_FETCH_1: |
6255 | case BUILT_IN_SYNC_OR_AND_FETCH_2: | |
6256 | case BUILT_IN_SYNC_OR_AND_FETCH_4: | |
6257 | case BUILT_IN_SYNC_OR_AND_FETCH_8: | |
6258 | case BUILT_IN_SYNC_OR_AND_FETCH_16: | |
6259 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1); | |
1cd6e20d | 6260 | target = expand_builtin_sync_operation (mode, exp, IOR, true, target); |
b6a5fc45 | 6261 | if (target) |
6262 | return target; | |
6263 | break; | |
6264 | ||
2797f13a | 6265 | case BUILT_IN_SYNC_AND_AND_FETCH_1: |
6266 | case BUILT_IN_SYNC_AND_AND_FETCH_2: | |
6267 | case BUILT_IN_SYNC_AND_AND_FETCH_4: | |
6268 | case BUILT_IN_SYNC_AND_AND_FETCH_8: | |
6269 | case BUILT_IN_SYNC_AND_AND_FETCH_16: | |
6270 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1); | |
1cd6e20d | 6271 | target = expand_builtin_sync_operation (mode, exp, AND, true, target); |
b6a5fc45 | 6272 | if (target) |
6273 | return target; | |
6274 | break; | |
6275 | ||
2797f13a | 6276 | case BUILT_IN_SYNC_XOR_AND_FETCH_1: |
6277 | case BUILT_IN_SYNC_XOR_AND_FETCH_2: | |
6278 | case BUILT_IN_SYNC_XOR_AND_FETCH_4: | |
6279 | case BUILT_IN_SYNC_XOR_AND_FETCH_8: | |
6280 | case BUILT_IN_SYNC_XOR_AND_FETCH_16: | |
6281 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1); | |
1cd6e20d | 6282 | target = expand_builtin_sync_operation (mode, exp, XOR, true, target); |
b6a5fc45 | 6283 | if (target) |
6284 | return target; | |
6285 | break; | |
6286 | ||
2797f13a | 6287 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
6288 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
6289 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
6290 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
6291 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
6292 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1); | |
1cd6e20d | 6293 | target = expand_builtin_sync_operation (mode, exp, NOT, true, target); |
b6a5fc45 | 6294 | if (target) |
6295 | return target; | |
6296 | break; | |
6297 | ||
2797f13a | 6298 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: |
6299 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
6300 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
6301 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
6302 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
a601d32a | 6303 | if (mode == VOIDmode) |
6304 | mode = TYPE_MODE (boolean_type_node); | |
b6a5fc45 | 6305 | if (!target || !register_operand (target, mode)) |
6306 | target = gen_reg_rtx (mode); | |
3e272de8 | 6307 | |
2797f13a | 6308 | mode = get_builtin_sync_mode |
6309 | (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1); | |
c2f47e15 | 6310 | target = expand_builtin_compare_and_swap (mode, exp, true, target); |
b6a5fc45 | 6311 | if (target) |
6312 | return target; | |
6313 | break; | |
6314 | ||
2797f13a | 6315 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: |
6316 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: | |
6317 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: | |
6318 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: | |
6319 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: | |
6320 | mode = get_builtin_sync_mode | |
6321 | (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1); | |
c2f47e15 | 6322 | target = expand_builtin_compare_and_swap (mode, exp, false, target); |
b6a5fc45 | 6323 | if (target) |
6324 | return target; | |
6325 | break; | |
6326 | ||
2797f13a | 6327 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: |
6328 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: | |
6329 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: | |
6330 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: | |
6331 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: | |
6332 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1); | |
6333 | target = expand_builtin_sync_lock_test_and_set (mode, exp, target); | |
b6a5fc45 | 6334 | if (target) |
6335 | return target; | |
6336 | break; | |
6337 | ||
2797f13a | 6338 | case BUILT_IN_SYNC_LOCK_RELEASE_1: |
6339 | case BUILT_IN_SYNC_LOCK_RELEASE_2: | |
6340 | case BUILT_IN_SYNC_LOCK_RELEASE_4: | |
6341 | case BUILT_IN_SYNC_LOCK_RELEASE_8: | |
6342 | case BUILT_IN_SYNC_LOCK_RELEASE_16: | |
6343 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1); | |
6344 | expand_builtin_sync_lock_release (mode, exp); | |
b6a5fc45 | 6345 | return const0_rtx; |
6346 | ||
2797f13a | 6347 | case BUILT_IN_SYNC_SYNCHRONIZE: |
6348 | expand_builtin_sync_synchronize (); | |
b6a5fc45 | 6349 | return const0_rtx; |
6350 | ||
1cd6e20d | 6351 | case BUILT_IN_ATOMIC_EXCHANGE_1: |
6352 | case BUILT_IN_ATOMIC_EXCHANGE_2: | |
6353 | case BUILT_IN_ATOMIC_EXCHANGE_4: | |
6354 | case BUILT_IN_ATOMIC_EXCHANGE_8: | |
6355 | case BUILT_IN_ATOMIC_EXCHANGE_16: | |
6356 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1); | |
6357 | target = expand_builtin_atomic_exchange (mode, exp, target); | |
6358 | if (target) | |
6359 | return target; | |
6360 | break; | |
6361 | ||
6362 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
6363 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
6364 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
6365 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
6366 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
2c201ad1 | 6367 | { |
6368 | unsigned int nargs, z; | |
f1f41a6c | 6369 | vec<tree, va_gc> *vec; |
2c201ad1 | 6370 | |
6371 | mode = | |
6372 | get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1); | |
6373 | target = expand_builtin_atomic_compare_exchange (mode, exp, target); | |
6374 | if (target) | |
6375 | return target; | |
6376 | ||
6377 | /* If this is turned into an external library call, the weak parameter | |
6378 | must be dropped to match the expected parameter list. */ | |
6379 | nargs = call_expr_nargs (exp); | |
f1f41a6c | 6380 | vec_alloc (vec, nargs - 1); |
2c201ad1 | 6381 | for (z = 0; z < 3; z++) |
f1f41a6c | 6382 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
2c201ad1 | 6383 | /* Skip the boolean weak parameter. */ |
6384 | for (z = 4; z < 6; z++) | |
f1f41a6c | 6385 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
2c201ad1 | 6386 | exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec); |
6387 | break; | |
6388 | } | |
1cd6e20d | 6389 | |
6390 | case BUILT_IN_ATOMIC_LOAD_1: | |
6391 | case BUILT_IN_ATOMIC_LOAD_2: | |
6392 | case BUILT_IN_ATOMIC_LOAD_4: | |
6393 | case BUILT_IN_ATOMIC_LOAD_8: | |
6394 | case BUILT_IN_ATOMIC_LOAD_16: | |
6395 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1); | |
6396 | target = expand_builtin_atomic_load (mode, exp, target); | |
6397 | if (target) | |
6398 | return target; | |
6399 | break; | |
6400 | ||
6401 | case BUILT_IN_ATOMIC_STORE_1: | |
6402 | case BUILT_IN_ATOMIC_STORE_2: | |
6403 | case BUILT_IN_ATOMIC_STORE_4: | |
6404 | case BUILT_IN_ATOMIC_STORE_8: | |
6405 | case BUILT_IN_ATOMIC_STORE_16: | |
6406 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1); | |
6407 | target = expand_builtin_atomic_store (mode, exp); | |
6408 | if (target) | |
6409 | return const0_rtx; | |
6410 | break; | |
6411 | ||
6412 | case BUILT_IN_ATOMIC_ADD_FETCH_1: | |
6413 | case BUILT_IN_ATOMIC_ADD_FETCH_2: | |
6414 | case BUILT_IN_ATOMIC_ADD_FETCH_4: | |
6415 | case BUILT_IN_ATOMIC_ADD_FETCH_8: | |
6416 | case BUILT_IN_ATOMIC_ADD_FETCH_16: | |
6417 | { | |
6418 | enum built_in_function lib; | |
6419 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1); | |
6420 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 + | |
6421 | (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1)); | |
6422 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true, | |
6423 | ignore, lib); | |
6424 | if (target) | |
6425 | return target; | |
6426 | break; | |
6427 | } | |
6428 | case BUILT_IN_ATOMIC_SUB_FETCH_1: | |
6429 | case BUILT_IN_ATOMIC_SUB_FETCH_2: | |
6430 | case BUILT_IN_ATOMIC_SUB_FETCH_4: | |
6431 | case BUILT_IN_ATOMIC_SUB_FETCH_8: | |
6432 | case BUILT_IN_ATOMIC_SUB_FETCH_16: | |
6433 | { | |
6434 | enum built_in_function lib; | |
6435 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1); | |
6436 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 + | |
6437 | (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1)); | |
6438 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true, | |
6439 | ignore, lib); | |
6440 | if (target) | |
6441 | return target; | |
6442 | break; | |
6443 | } | |
6444 | case BUILT_IN_ATOMIC_AND_FETCH_1: | |
6445 | case BUILT_IN_ATOMIC_AND_FETCH_2: | |
6446 | case BUILT_IN_ATOMIC_AND_FETCH_4: | |
6447 | case BUILT_IN_ATOMIC_AND_FETCH_8: | |
6448 | case BUILT_IN_ATOMIC_AND_FETCH_16: | |
6449 | { | |
6450 | enum built_in_function lib; | |
6451 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1); | |
6452 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 + | |
6453 | (fcode - BUILT_IN_ATOMIC_AND_FETCH_1)); | |
6454 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true, | |
6455 | ignore, lib); | |
6456 | if (target) | |
6457 | return target; | |
6458 | break; | |
6459 | } | |
6460 | case BUILT_IN_ATOMIC_NAND_FETCH_1: | |
6461 | case BUILT_IN_ATOMIC_NAND_FETCH_2: | |
6462 | case BUILT_IN_ATOMIC_NAND_FETCH_4: | |
6463 | case BUILT_IN_ATOMIC_NAND_FETCH_8: | |
6464 | case BUILT_IN_ATOMIC_NAND_FETCH_16: | |
6465 | { | |
6466 | enum built_in_function lib; | |
6467 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1); | |
6468 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 + | |
6469 | (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1)); | |
6470 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true, | |
6471 | ignore, lib); | |
6472 | if (target) | |
6473 | return target; | |
6474 | break; | |
6475 | } | |
6476 | case BUILT_IN_ATOMIC_XOR_FETCH_1: | |
6477 | case BUILT_IN_ATOMIC_XOR_FETCH_2: | |
6478 | case BUILT_IN_ATOMIC_XOR_FETCH_4: | |
6479 | case BUILT_IN_ATOMIC_XOR_FETCH_8: | |
6480 | case BUILT_IN_ATOMIC_XOR_FETCH_16: | |
6481 | { | |
6482 | enum built_in_function lib; | |
6483 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1); | |
6484 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 + | |
6485 | (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1)); | |
6486 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true, | |
6487 | ignore, lib); | |
6488 | if (target) | |
6489 | return target; | |
6490 | break; | |
6491 | } | |
6492 | case BUILT_IN_ATOMIC_OR_FETCH_1: | |
6493 | case BUILT_IN_ATOMIC_OR_FETCH_2: | |
6494 | case BUILT_IN_ATOMIC_OR_FETCH_4: | |
6495 | case BUILT_IN_ATOMIC_OR_FETCH_8: | |
6496 | case BUILT_IN_ATOMIC_OR_FETCH_16: | |
6497 | { | |
6498 | enum built_in_function lib; | |
6499 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1); | |
6500 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 + | |
6501 | (fcode - BUILT_IN_ATOMIC_OR_FETCH_1)); | |
6502 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true, | |
6503 | ignore, lib); | |
6504 | if (target) | |
6505 | return target; | |
6506 | break; | |
6507 | } | |
6508 | case BUILT_IN_ATOMIC_FETCH_ADD_1: | |
6509 | case BUILT_IN_ATOMIC_FETCH_ADD_2: | |
6510 | case BUILT_IN_ATOMIC_FETCH_ADD_4: | |
6511 | case BUILT_IN_ATOMIC_FETCH_ADD_8: | |
6512 | case BUILT_IN_ATOMIC_FETCH_ADD_16: | |
6513 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1); | |
6514 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false, | |
6515 | ignore, BUILT_IN_NONE); | |
6516 | if (target) | |
6517 | return target; | |
6518 | break; | |
6519 | ||
6520 | case BUILT_IN_ATOMIC_FETCH_SUB_1: | |
6521 | case BUILT_IN_ATOMIC_FETCH_SUB_2: | |
6522 | case BUILT_IN_ATOMIC_FETCH_SUB_4: | |
6523 | case BUILT_IN_ATOMIC_FETCH_SUB_8: | |
6524 | case BUILT_IN_ATOMIC_FETCH_SUB_16: | |
6525 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1); | |
6526 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false, | |
6527 | ignore, BUILT_IN_NONE); | |
6528 | if (target) | |
6529 | return target; | |
6530 | break; | |
6531 | ||
6532 | case BUILT_IN_ATOMIC_FETCH_AND_1: | |
6533 | case BUILT_IN_ATOMIC_FETCH_AND_2: | |
6534 | case BUILT_IN_ATOMIC_FETCH_AND_4: | |
6535 | case BUILT_IN_ATOMIC_FETCH_AND_8: | |
6536 | case BUILT_IN_ATOMIC_FETCH_AND_16: | |
6537 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1); | |
6538 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false, | |
6539 | ignore, BUILT_IN_NONE); | |
6540 | if (target) | |
6541 | return target; | |
6542 | break; | |
6543 | ||
6544 | case BUILT_IN_ATOMIC_FETCH_NAND_1: | |
6545 | case BUILT_IN_ATOMIC_FETCH_NAND_2: | |
6546 | case BUILT_IN_ATOMIC_FETCH_NAND_4: | |
6547 | case BUILT_IN_ATOMIC_FETCH_NAND_8: | |
6548 | case BUILT_IN_ATOMIC_FETCH_NAND_16: | |
6549 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1); | |
6550 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false, | |
6551 | ignore, BUILT_IN_NONE); | |
6552 | if (target) | |
6553 | return target; | |
6554 | break; | |
6555 | ||
6556 | case BUILT_IN_ATOMIC_FETCH_XOR_1: | |
6557 | case BUILT_IN_ATOMIC_FETCH_XOR_2: | |
6558 | case BUILT_IN_ATOMIC_FETCH_XOR_4: | |
6559 | case BUILT_IN_ATOMIC_FETCH_XOR_8: | |
6560 | case BUILT_IN_ATOMIC_FETCH_XOR_16: | |
6561 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1); | |
6562 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false, | |
6563 | ignore, BUILT_IN_NONE); | |
6564 | if (target) | |
6565 | return target; | |
6566 | break; | |
6567 | ||
6568 | case BUILT_IN_ATOMIC_FETCH_OR_1: | |
6569 | case BUILT_IN_ATOMIC_FETCH_OR_2: | |
6570 | case BUILT_IN_ATOMIC_FETCH_OR_4: | |
6571 | case BUILT_IN_ATOMIC_FETCH_OR_8: | |
6572 | case BUILT_IN_ATOMIC_FETCH_OR_16: | |
6573 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1); | |
6574 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false, | |
6575 | ignore, BUILT_IN_NONE); | |
6576 | if (target) | |
6577 | return target; | |
6578 | break; | |
10b744a3 | 6579 | |
6580 | case BUILT_IN_ATOMIC_TEST_AND_SET: | |
7821cde1 | 6581 | return expand_builtin_atomic_test_and_set (exp, target); |
10b744a3 | 6582 | |
6583 | case BUILT_IN_ATOMIC_CLEAR: | |
6584 | return expand_builtin_atomic_clear (exp); | |
1cd6e20d | 6585 | |
6586 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: | |
6587 | return expand_builtin_atomic_always_lock_free (exp); | |
6588 | ||
6589 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
6590 | target = expand_builtin_atomic_is_lock_free (exp); | |
6591 | if (target) | |
6592 | return target; | |
6593 | break; | |
6594 | ||
6595 | case BUILT_IN_ATOMIC_THREAD_FENCE: | |
6596 | expand_builtin_atomic_thread_fence (exp); | |
6597 | return const0_rtx; | |
6598 | ||
6599 | case BUILT_IN_ATOMIC_SIGNAL_FENCE: | |
6600 | expand_builtin_atomic_signal_fence (exp); | |
6601 | return const0_rtx; | |
6602 | ||
0a39fd54 | 6603 | case BUILT_IN_OBJECT_SIZE: |
6604 | return expand_builtin_object_size (exp); | |
6605 | ||
6606 | case BUILT_IN_MEMCPY_CHK: | |
6607 | case BUILT_IN_MEMPCPY_CHK: | |
6608 | case BUILT_IN_MEMMOVE_CHK: | |
6609 | case BUILT_IN_MEMSET_CHK: | |
6610 | target = expand_builtin_memory_chk (exp, target, mode, fcode); | |
6611 | if (target) | |
6612 | return target; | |
6613 | break; | |
6614 | ||
6615 | case BUILT_IN_STRCPY_CHK: | |
6616 | case BUILT_IN_STPCPY_CHK: | |
6617 | case BUILT_IN_STRNCPY_CHK: | |
1063acde | 6618 | case BUILT_IN_STPNCPY_CHK: |
0a39fd54 | 6619 | case BUILT_IN_STRCAT_CHK: |
b356dfef | 6620 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 6621 | case BUILT_IN_SNPRINTF_CHK: |
6622 | case BUILT_IN_VSNPRINTF_CHK: | |
6623 | maybe_emit_chk_warning (exp, fcode); | |
6624 | break; | |
6625 | ||
6626 | case BUILT_IN_SPRINTF_CHK: | |
6627 | case BUILT_IN_VSPRINTF_CHK: | |
6628 | maybe_emit_sprintf_chk_warning (exp, fcode); | |
6629 | break; | |
6630 | ||
2c281b15 | 6631 | case BUILT_IN_FREE: |
f74ea1c2 | 6632 | if (warn_free_nonheap_object) |
6633 | maybe_emit_free_warning (exp); | |
2c281b15 | 6634 | break; |
6635 | ||
badaa04c | 6636 | case BUILT_IN_THREAD_POINTER: |
6637 | return expand_builtin_thread_pointer (exp, target); | |
6638 | ||
6639 | case BUILT_IN_SET_THREAD_POINTER: | |
6640 | expand_builtin_set_thread_pointer (exp); | |
6641 | return const0_rtx; | |
6642 | ||
d037099f | 6643 | case BUILT_IN_CILK_DETACH: |
6644 | expand_builtin_cilk_detach (exp); | |
6645 | return const0_rtx; | |
6646 | ||
6647 | case BUILT_IN_CILK_POP_FRAME: | |
6648 | expand_builtin_cilk_pop_frame (exp); | |
6649 | return const0_rtx; | |
6650 | ||
058a1b7a | 6651 | case BUILT_IN_CHKP_INIT_PTR_BOUNDS: |
6652 | case BUILT_IN_CHKP_NULL_PTR_BOUNDS: | |
6653 | case BUILT_IN_CHKP_COPY_PTR_BOUNDS: | |
6654 | case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS: | |
6655 | case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS: | |
6656 | case BUILT_IN_CHKP_CHECK_PTR_BOUNDS: | |
6657 | case BUILT_IN_CHKP_SET_PTR_BOUNDS: | |
6658 | case BUILT_IN_CHKP_NARROW_PTR_BOUNDS: | |
6659 | case BUILT_IN_CHKP_STORE_PTR_BOUNDS: | |
6660 | case BUILT_IN_CHKP_GET_PTR_LBOUND: | |
6661 | case BUILT_IN_CHKP_GET_PTR_UBOUND: | |
6662 | /* We allow user CHKP builtins if Pointer Bounds | |
6663 | Checker is off. */ | |
6664 | if (!chkp_function_instrumented_p (current_function_decl)) | |
6665 | { | |
6666 | if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS | |
6667 | || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS | |
6668 | || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS | |
6669 | || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS | |
6670 | || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS) | |
6671 | return expand_normal (CALL_EXPR_ARG (exp, 0)); | |
6672 | else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND) | |
6673 | return expand_normal (size_zero_node); | |
6674 | else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND) | |
6675 | return expand_normal (size_int (-1)); | |
6676 | else | |
6677 | return const0_rtx; | |
6678 | } | |
6679 | /* FALLTHROUGH */ | |
6680 | ||
6681 | case BUILT_IN_CHKP_BNDMK: | |
6682 | case BUILT_IN_CHKP_BNDSTX: | |
6683 | case BUILT_IN_CHKP_BNDCL: | |
6684 | case BUILT_IN_CHKP_BNDCU: | |
6685 | case BUILT_IN_CHKP_BNDLDX: | |
6686 | case BUILT_IN_CHKP_BNDRET: | |
6687 | case BUILT_IN_CHKP_INTERSECT: | |
6688 | case BUILT_IN_CHKP_NARROW: | |
6689 | case BUILT_IN_CHKP_EXTRACT_LOWER: | |
6690 | case BUILT_IN_CHKP_EXTRACT_UPPER: | |
6691 | /* Software implementation of Pointer Bounds Checker is NYI. | |
6692 | Target support is required. */ | |
6693 | error ("Your target platform does not support -fcheck-pointer-bounds"); | |
6694 | break; | |
6695 | ||
ca4c3545 | 6696 | case BUILT_IN_ACC_ON_DEVICE: |
1ae4e7aa | 6697 | /* Do library call, if we failed to expand the builtin when |
6698 | folding. */ | |
ca4c3545 | 6699 | break; |
6700 | ||
92482ee0 | 6701 | default: /* just do library call, if unknown builtin */ |
146c1b4f | 6702 | break; |
53800dbe | 6703 | } |
6704 | ||
6705 | /* The switch statement above can drop through to cause the function | |
6706 | to be called normally. */ | |
6707 | return expand_call (exp, target, ignore); | |
6708 | } | |
650e4c94 | 6709 | |
f21337ef | 6710 | /* Similar to expand_builtin but is used for instrumented calls. */ |
6711 | ||
6712 | rtx | |
6713 | expand_builtin_with_bounds (tree exp, rtx target, | |
6714 | rtx subtarget ATTRIBUTE_UNUSED, | |
6715 | machine_mode mode, int ignore) | |
6716 | { | |
6717 | tree fndecl = get_callee_fndecl (exp); | |
6718 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6719 | ||
6720 | gcc_assert (CALL_WITH_BOUNDS_P (exp)); | |
6721 | ||
6722 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6723 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); | |
6724 | ||
6725 | gcc_assert (fcode > BEGIN_CHKP_BUILTINS | |
6726 | && fcode < END_CHKP_BUILTINS); | |
6727 | ||
6728 | switch (fcode) | |
6729 | { | |
6730 | case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP: | |
6731 | target = expand_builtin_memcpy_with_bounds (exp, target); | |
6732 | if (target) | |
6733 | return target; | |
6734 | break; | |
6735 | ||
6736 | case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP: | |
6737 | target = expand_builtin_mempcpy_with_bounds (exp, target, mode); | |
6738 | if (target) | |
6739 | return target; | |
6740 | break; | |
6741 | ||
6742 | case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP: | |
6743 | target = expand_builtin_memset_with_bounds (exp, target, mode); | |
6744 | if (target) | |
6745 | return target; | |
6746 | break; | |
6747 | ||
6748 | default: | |
6749 | break; | |
6750 | } | |
6751 | ||
6752 | /* The switch statement above can drop through to cause the function | |
6753 | to be called normally. */ | |
6754 | return expand_call (exp, target, ignore); | |
6755 | } | |
6756 | ||
805e22b2 | 6757 | /* Determine whether a tree node represents a call to a built-in |
52203a9d | 6758 | function. If the tree T is a call to a built-in function with |
6759 | the right number of arguments of the appropriate types, return | |
6760 | the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. | |
6761 | Otherwise the return value is END_BUILTINS. */ | |
aecda0d6 | 6762 | |
805e22b2 | 6763 | enum built_in_function |
b7bf20db | 6764 | builtin_mathfn_code (const_tree t) |
805e22b2 | 6765 | { |
b7bf20db | 6766 | const_tree fndecl, arg, parmlist; |
6767 | const_tree argtype, parmtype; | |
6768 | const_call_expr_arg_iterator iter; | |
805e22b2 | 6769 | |
6770 | if (TREE_CODE (t) != CALL_EXPR | |
c2f47e15 | 6771 | || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR) |
805e22b2 | 6772 | return END_BUILTINS; |
6773 | ||
c6e6ecb1 | 6774 | fndecl = get_callee_fndecl (t); |
6775 | if (fndecl == NULL_TREE | |
52203a9d | 6776 | || TREE_CODE (fndecl) != FUNCTION_DECL |
805e22b2 | 6777 | || ! DECL_BUILT_IN (fndecl) |
6778 | || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6779 | return END_BUILTINS; | |
6780 | ||
52203a9d | 6781 | parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
b7bf20db | 6782 | init_const_call_expr_arg_iterator (t, &iter); |
52203a9d | 6783 | for (; parmlist; parmlist = TREE_CHAIN (parmlist)) |
e9f80ff5 | 6784 | { |
52203a9d | 6785 | /* If a function doesn't take a variable number of arguments, |
6786 | the last element in the list will have type `void'. */ | |
6787 | parmtype = TREE_VALUE (parmlist); | |
6788 | if (VOID_TYPE_P (parmtype)) | |
6789 | { | |
b7bf20db | 6790 | if (more_const_call_expr_args_p (&iter)) |
52203a9d | 6791 | return END_BUILTINS; |
6792 | return DECL_FUNCTION_CODE (fndecl); | |
6793 | } | |
6794 | ||
b7bf20db | 6795 | if (! more_const_call_expr_args_p (&iter)) |
e9f80ff5 | 6796 | return END_BUILTINS; |
48e1416a | 6797 | |
b7bf20db | 6798 | arg = next_const_call_expr_arg (&iter); |
c2f47e15 | 6799 | argtype = TREE_TYPE (arg); |
52203a9d | 6800 | |
6801 | if (SCALAR_FLOAT_TYPE_P (parmtype)) | |
6802 | { | |
6803 | if (! SCALAR_FLOAT_TYPE_P (argtype)) | |
6804 | return END_BUILTINS; | |
6805 | } | |
6806 | else if (COMPLEX_FLOAT_TYPE_P (parmtype)) | |
6807 | { | |
6808 | if (! COMPLEX_FLOAT_TYPE_P (argtype)) | |
6809 | return END_BUILTINS; | |
6810 | } | |
6811 | else if (POINTER_TYPE_P (parmtype)) | |
6812 | { | |
6813 | if (! POINTER_TYPE_P (argtype)) | |
6814 | return END_BUILTINS; | |
6815 | } | |
6816 | else if (INTEGRAL_TYPE_P (parmtype)) | |
6817 | { | |
6818 | if (! INTEGRAL_TYPE_P (argtype)) | |
6819 | return END_BUILTINS; | |
6820 | } | |
6821 | else | |
e9f80ff5 | 6822 | return END_BUILTINS; |
e9f80ff5 | 6823 | } |
6824 | ||
52203a9d | 6825 | /* Variable-length argument list. */ |
805e22b2 | 6826 | return DECL_FUNCTION_CODE (fndecl); |
6827 | } | |
6828 | ||
c2f47e15 | 6829 | /* Fold a call to __builtin_constant_p, if we know its argument ARG will |
6830 | evaluate to a constant. */ | |
650e4c94 | 6831 | |
6832 | static tree | |
c2f47e15 | 6833 | fold_builtin_constant_p (tree arg) |
650e4c94 | 6834 | { |
650e4c94 | 6835 | /* We return 1 for a numeric type that's known to be a constant |
6836 | value at compile-time or for an aggregate type that's a | |
6837 | literal constant. */ | |
c2f47e15 | 6838 | STRIP_NOPS (arg); |
650e4c94 | 6839 | |
6840 | /* If we know this is a constant, emit the constant of one. */ | |
c2f47e15 | 6841 | if (CONSTANT_CLASS_P (arg) |
6842 | || (TREE_CODE (arg) == CONSTRUCTOR | |
6843 | && TREE_CONSTANT (arg))) | |
650e4c94 | 6844 | return integer_one_node; |
c2f47e15 | 6845 | if (TREE_CODE (arg) == ADDR_EXPR) |
adcfa3a3 | 6846 | { |
c2f47e15 | 6847 | tree op = TREE_OPERAND (arg, 0); |
adcfa3a3 | 6848 | if (TREE_CODE (op) == STRING_CST |
6849 | || (TREE_CODE (op) == ARRAY_REF | |
6850 | && integer_zerop (TREE_OPERAND (op, 1)) | |
6851 | && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST)) | |
6852 | return integer_one_node; | |
6853 | } | |
650e4c94 | 6854 | |
1fb4300c | 6855 | /* If this expression has side effects, show we don't know it to be a |
6856 | constant. Likewise if it's a pointer or aggregate type since in | |
6857 | those case we only want literals, since those are only optimized | |
f97c71a1 | 6858 | when generating RTL, not later. |
6859 | And finally, if we are compiling an initializer, not code, we | |
6860 | need to return a definite result now; there's not going to be any | |
6861 | more optimization done. */ | |
c2f47e15 | 6862 | if (TREE_SIDE_EFFECTS (arg) |
6863 | || AGGREGATE_TYPE_P (TREE_TYPE (arg)) | |
6864 | || POINTER_TYPE_P (TREE_TYPE (arg)) | |
47be647d | 6865 | || cfun == 0 |
0b049e15 | 6866 | || folding_initializer |
6867 | || force_folding_builtin_constant_p) | |
650e4c94 | 6868 | return integer_zero_node; |
6869 | ||
c2f47e15 | 6870 | return NULL_TREE; |
650e4c94 | 6871 | } |
6872 | ||
76f5a783 | 6873 | /* Create builtin_expect with PRED and EXPECTED as its arguments and |
6874 | return it as a truthvalue. */ | |
4ee9c684 | 6875 | |
6876 | static tree | |
c83059be | 6877 | build_builtin_expect_predicate (location_t loc, tree pred, tree expected, |
6878 | tree predictor) | |
4ee9c684 | 6879 | { |
76f5a783 | 6880 | tree fn, arg_types, pred_type, expected_type, call_expr, ret_type; |
4ee9c684 | 6881 | |
b9a16870 | 6882 | fn = builtin_decl_explicit (BUILT_IN_EXPECT); |
76f5a783 | 6883 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
6884 | ret_type = TREE_TYPE (TREE_TYPE (fn)); | |
6885 | pred_type = TREE_VALUE (arg_types); | |
6886 | expected_type = TREE_VALUE (TREE_CHAIN (arg_types)); | |
6887 | ||
389dd41b | 6888 | pred = fold_convert_loc (loc, pred_type, pred); |
6889 | expected = fold_convert_loc (loc, expected_type, expected); | |
c83059be | 6890 | call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected, |
6891 | predictor); | |
76f5a783 | 6892 | |
6893 | return build2 (NE_EXPR, TREE_TYPE (pred), call_expr, | |
6894 | build_int_cst (ret_type, 0)); | |
6895 | } | |
6896 | ||
6897 | /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return | |
6898 | NULL_TREE if no simplification is possible. */ | |
6899 | ||
c83059be | 6900 | tree |
6901 | fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2) | |
76f5a783 | 6902 | { |
083bada9 | 6903 | tree inner, fndecl, inner_arg0; |
76f5a783 | 6904 | enum tree_code code; |
6905 | ||
083bada9 | 6906 | /* Distribute the expected value over short-circuiting operators. |
6907 | See through the cast from truthvalue_type_node to long. */ | |
6908 | inner_arg0 = arg0; | |
d09ef31a | 6909 | while (CONVERT_EXPR_P (inner_arg0) |
083bada9 | 6910 | && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0)) |
6911 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0)))) | |
6912 | inner_arg0 = TREE_OPERAND (inner_arg0, 0); | |
6913 | ||
76f5a783 | 6914 | /* If this is a builtin_expect within a builtin_expect keep the |
6915 | inner one. See through a comparison against a constant. It | |
6916 | might have been added to create a thruthvalue. */ | |
083bada9 | 6917 | inner = inner_arg0; |
6918 | ||
76f5a783 | 6919 | if (COMPARISON_CLASS_P (inner) |
6920 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST) | |
6921 | inner = TREE_OPERAND (inner, 0); | |
6922 | ||
6923 | if (TREE_CODE (inner) == CALL_EXPR | |
6924 | && (fndecl = get_callee_fndecl (inner)) | |
6925 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
6926 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT) | |
6927 | return arg0; | |
6928 | ||
083bada9 | 6929 | inner = inner_arg0; |
76f5a783 | 6930 | code = TREE_CODE (inner); |
6931 | if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) | |
6932 | { | |
6933 | tree op0 = TREE_OPERAND (inner, 0); | |
6934 | tree op1 = TREE_OPERAND (inner, 1); | |
6935 | ||
c83059be | 6936 | op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2); |
6937 | op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2); | |
76f5a783 | 6938 | inner = build2 (code, TREE_TYPE (inner), op0, op1); |
6939 | ||
389dd41b | 6940 | return fold_convert_loc (loc, TREE_TYPE (arg0), inner); |
76f5a783 | 6941 | } |
6942 | ||
6943 | /* If the argument isn't invariant then there's nothing else we can do. */ | |
083bada9 | 6944 | if (!TREE_CONSTANT (inner_arg0)) |
c2f47e15 | 6945 | return NULL_TREE; |
4ee9c684 | 6946 | |
76f5a783 | 6947 | /* If we expect that a comparison against the argument will fold to |
6948 | a constant return the constant. In practice, this means a true | |
6949 | constant or the address of a non-weak symbol. */ | |
083bada9 | 6950 | inner = inner_arg0; |
4ee9c684 | 6951 | STRIP_NOPS (inner); |
6952 | if (TREE_CODE (inner) == ADDR_EXPR) | |
6953 | { | |
6954 | do | |
6955 | { | |
6956 | inner = TREE_OPERAND (inner, 0); | |
6957 | } | |
6958 | while (TREE_CODE (inner) == COMPONENT_REF | |
6959 | || TREE_CODE (inner) == ARRAY_REF); | |
062b4460 | 6960 | if ((TREE_CODE (inner) == VAR_DECL |
6961 | || TREE_CODE (inner) == FUNCTION_DECL) | |
6962 | && DECL_WEAK (inner)) | |
c2f47e15 | 6963 | return NULL_TREE; |
4ee9c684 | 6964 | } |
6965 | ||
76f5a783 | 6966 | /* Otherwise, ARG0 already has the proper type for the return value. */ |
6967 | return arg0; | |
4ee9c684 | 6968 | } |
6969 | ||
c2f47e15 | 6970 | /* Fold a call to __builtin_classify_type with argument ARG. */ |
27d0c333 | 6971 | |
539a3a92 | 6972 | static tree |
c2f47e15 | 6973 | fold_builtin_classify_type (tree arg) |
539a3a92 | 6974 | { |
c2f47e15 | 6975 | if (arg == 0) |
7002a1c8 | 6976 | return build_int_cst (integer_type_node, no_type_class); |
539a3a92 | 6977 | |
7002a1c8 | 6978 | return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg))); |
539a3a92 | 6979 | } |
6980 | ||
c2f47e15 | 6981 | /* Fold a call to __builtin_strlen with argument ARG. */ |
e6e27594 | 6982 | |
6983 | static tree | |
c7cbde74 | 6984 | fold_builtin_strlen (location_t loc, tree type, tree arg) |
e6e27594 | 6985 | { |
c2f47e15 | 6986 | if (!validate_arg (arg, POINTER_TYPE)) |
e6e27594 | 6987 | return NULL_TREE; |
6988 | else | |
6989 | { | |
c2f47e15 | 6990 | tree len = c_strlen (arg, 0); |
e6e27594 | 6991 | |
6992 | if (len) | |
c7cbde74 | 6993 | return fold_convert_loc (loc, type, len); |
e6e27594 | 6994 | |
6995 | return NULL_TREE; | |
6996 | } | |
6997 | } | |
6998 | ||
92c43e3c | 6999 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
7000 | ||
7001 | static tree | |
389dd41b | 7002 | fold_builtin_inf (location_t loc, tree type, int warn) |
92c43e3c | 7003 | { |
aa870c1b | 7004 | REAL_VALUE_TYPE real; |
7005 | ||
40f4dbd5 | 7006 | /* __builtin_inff is intended to be usable to define INFINITY on all |
7007 | targets. If an infinity is not available, INFINITY expands "to a | |
7008 | positive constant of type float that overflows at translation | |
7009 | time", footnote "In this case, using INFINITY will violate the | |
7010 | constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4). | |
7011 | Thus we pedwarn to ensure this constraint violation is | |
7012 | diagnosed. */ | |
92c43e3c | 7013 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
389dd41b | 7014 | pedwarn (loc, 0, "target format does not support infinity"); |
92c43e3c | 7015 | |
aa870c1b | 7016 | real_inf (&real); |
7017 | return build_real (type, real); | |
92c43e3c | 7018 | } |
7019 | ||
d735c391 | 7020 | /* Fold function call to builtin sincos, sincosf, or sincosl. Return |
7021 | NULL_TREE if no simplification can be made. */ | |
7022 | ||
7023 | static tree | |
389dd41b | 7024 | fold_builtin_sincos (location_t loc, |
7025 | tree arg0, tree arg1, tree arg2) | |
d735c391 | 7026 | { |
c2f47e15 | 7027 | tree type; |
6c21be92 | 7028 | tree fndecl, call = NULL_TREE; |
d735c391 | 7029 | |
c2f47e15 | 7030 | if (!validate_arg (arg0, REAL_TYPE) |
7031 | || !validate_arg (arg1, POINTER_TYPE) | |
7032 | || !validate_arg (arg2, POINTER_TYPE)) | |
d735c391 | 7033 | return NULL_TREE; |
7034 | ||
d735c391 | 7035 | type = TREE_TYPE (arg0); |
d735c391 | 7036 | |
7037 | /* Calculate the result when the argument is a constant. */ | |
e3240774 | 7038 | built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI); |
6c21be92 | 7039 | if (fn == END_BUILTINS) |
d735c391 | 7040 | return NULL_TREE; |
7041 | ||
6c21be92 | 7042 | /* Canonicalize sincos to cexpi. */ |
7043 | if (TREE_CODE (arg0) == REAL_CST) | |
7044 | { | |
7045 | tree complex_type = build_complex_type (type); | |
744fe358 | 7046 | call = fold_const_call (as_combined_fn (fn), complex_type, arg0); |
6c21be92 | 7047 | } |
7048 | if (!call) | |
7049 | { | |
7050 | if (!targetm.libc_has_function (function_c99_math_complex) | |
7051 | || !builtin_decl_implicit_p (fn)) | |
7052 | return NULL_TREE; | |
7053 | fndecl = builtin_decl_explicit (fn); | |
7054 | call = build_call_expr_loc (loc, fndecl, 1, arg0); | |
7055 | call = builtin_save_expr (call); | |
7056 | } | |
d735c391 | 7057 | |
a75b1c71 | 7058 | return build2 (COMPOUND_EXPR, void_type_node, |
d735c391 | 7059 | build2 (MODIFY_EXPR, void_type_node, |
389dd41b | 7060 | build_fold_indirect_ref_loc (loc, arg1), |
6c21be92 | 7061 | fold_build1_loc (loc, IMAGPART_EXPR, type, call)), |
d735c391 | 7062 | build2 (MODIFY_EXPR, void_type_node, |
389dd41b | 7063 | build_fold_indirect_ref_loc (loc, arg2), |
6c21be92 | 7064 | fold_build1_loc (loc, REALPART_EXPR, type, call))); |
d735c391 | 7065 | } |
7066 | ||
7959b13b | 7067 | /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the |
7068 | arguments to the call, and TYPE is its return type. | |
7069 | Return NULL_TREE if no simplification can be made. */ | |
7070 | ||
7071 | static tree | |
389dd41b | 7072 | fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type) |
7959b13b | 7073 | { |
7074 | if (!validate_arg (arg1, POINTER_TYPE) | |
7075 | || !validate_arg (arg2, INTEGER_TYPE) | |
7076 | || !validate_arg (len, INTEGER_TYPE)) | |
7077 | return NULL_TREE; | |
7078 | else | |
7079 | { | |
7080 | const char *p1; | |
7081 | ||
7082 | if (TREE_CODE (arg2) != INTEGER_CST | |
e913b5cd | 7083 | || !tree_fits_uhwi_p (len)) |
7959b13b | 7084 | return NULL_TREE; |
7085 | ||
7086 | p1 = c_getstr (arg1); | |
7087 | if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0) | |
7088 | { | |
7089 | char c; | |
7090 | const char *r; | |
7091 | tree tem; | |
7092 | ||
7093 | if (target_char_cast (arg2, &c)) | |
7094 | return NULL_TREE; | |
7095 | ||
e913b5cd | 7096 | r = (const char *) memchr (p1, c, tree_to_uhwi (len)); |
7959b13b | 7097 | |
7098 | if (r == NULL) | |
7099 | return build_int_cst (TREE_TYPE (arg1), 0); | |
7100 | ||
2cc66f2a | 7101 | tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1); |
389dd41b | 7102 | return fold_convert_loc (loc, type, tem); |
7959b13b | 7103 | } |
7104 | return NULL_TREE; | |
7105 | } | |
7106 | } | |
7107 | ||
c2f47e15 | 7108 | /* Fold function call to builtin memcmp with arguments ARG1 and ARG2. |
7109 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 7110 | |
7111 | static tree | |
389dd41b | 7112 | fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len) |
9c8a1629 | 7113 | { |
c2f47e15 | 7114 | if (!validate_arg (arg1, POINTER_TYPE) |
7115 | || !validate_arg (arg2, POINTER_TYPE) | |
7116 | || !validate_arg (len, INTEGER_TYPE)) | |
7117 | return NULL_TREE; | |
9c8a1629 | 7118 | |
7119 | /* If the LEN parameter is zero, return zero. */ | |
7120 | if (integer_zerop (len)) | |
389dd41b | 7121 | return omit_two_operands_loc (loc, integer_type_node, integer_zero_node, |
c4fef134 | 7122 | arg1, arg2); |
9c8a1629 | 7123 | |
7124 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7125 | if (operand_equal_p (arg1, arg2, 0)) | |
389dd41b | 7126 | return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len); |
c4fef134 | 7127 | |
c4fef134 | 7128 | /* If len parameter is one, return an expression corresponding to |
7129 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
e913b5cd | 7130 | if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1) |
c4fef134 | 7131 | { |
7132 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7133 | tree cst_uchar_ptr_node |
7134 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7135 | ||
389dd41b | 7136 | tree ind1 |
7137 | = fold_convert_loc (loc, integer_type_node, | |
7138 | build1 (INDIRECT_REF, cst_uchar_node, | |
7139 | fold_convert_loc (loc, | |
7140 | cst_uchar_ptr_node, | |
c4fef134 | 7141 | arg1))); |
389dd41b | 7142 | tree ind2 |
7143 | = fold_convert_loc (loc, integer_type_node, | |
7144 | build1 (INDIRECT_REF, cst_uchar_node, | |
7145 | fold_convert_loc (loc, | |
7146 | cst_uchar_ptr_node, | |
c4fef134 | 7147 | arg2))); |
389dd41b | 7148 | return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2); |
c4fef134 | 7149 | } |
9c8a1629 | 7150 | |
c2f47e15 | 7151 | return NULL_TREE; |
9c8a1629 | 7152 | } |
7153 | ||
c2f47e15 | 7154 | /* Fold function call to builtin strcmp with arguments ARG1 and ARG2. |
7155 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 7156 | |
7157 | static tree | |
389dd41b | 7158 | fold_builtin_strcmp (location_t loc, tree arg1, tree arg2) |
9c8a1629 | 7159 | { |
c2f47e15 | 7160 | if (!validate_arg (arg1, POINTER_TYPE) |
7161 | || !validate_arg (arg2, POINTER_TYPE)) | |
7162 | return NULL_TREE; | |
9c8a1629 | 7163 | |
7164 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7165 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 7166 | return integer_zero_node; |
9c8a1629 | 7167 | |
c4fef134 | 7168 | /* If the second arg is "", return *(const unsigned char*)arg1. */ |
b5e46e2c | 7169 | const char *p2 = c_getstr (arg2); |
c4fef134 | 7170 | if (p2 && *p2 == '\0') |
7171 | { | |
7172 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7173 | tree cst_uchar_ptr_node |
7174 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7175 | ||
389dd41b | 7176 | return fold_convert_loc (loc, integer_type_node, |
7177 | build1 (INDIRECT_REF, cst_uchar_node, | |
7178 | fold_convert_loc (loc, | |
7179 | cst_uchar_ptr_node, | |
7180 | arg1))); | |
c4fef134 | 7181 | } |
7182 | ||
7183 | /* If the first arg is "", return -*(const unsigned char*)arg2. */ | |
b5e46e2c | 7184 | const char *p1 = c_getstr (arg1); |
c4fef134 | 7185 | if (p1 && *p1 == '\0') |
7186 | { | |
7187 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7188 | tree cst_uchar_ptr_node |
7189 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7190 | ||
389dd41b | 7191 | tree temp |
7192 | = fold_convert_loc (loc, integer_type_node, | |
7193 | build1 (INDIRECT_REF, cst_uchar_node, | |
7194 | fold_convert_loc (loc, | |
7195 | cst_uchar_ptr_node, | |
c4fef134 | 7196 | arg2))); |
389dd41b | 7197 | return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp); |
9c8a1629 | 7198 | } |
7199 | ||
c2f47e15 | 7200 | return NULL_TREE; |
9c8a1629 | 7201 | } |
7202 | ||
c2f47e15 | 7203 | /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN. |
7204 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 7205 | |
7206 | static tree | |
389dd41b | 7207 | fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len) |
9c8a1629 | 7208 | { |
c2f47e15 | 7209 | if (!validate_arg (arg1, POINTER_TYPE) |
7210 | || !validate_arg (arg2, POINTER_TYPE) | |
7211 | || !validate_arg (len, INTEGER_TYPE)) | |
7212 | return NULL_TREE; | |
9c8a1629 | 7213 | |
7214 | /* If the LEN parameter is zero, return zero. */ | |
7215 | if (integer_zerop (len)) | |
389dd41b | 7216 | return omit_two_operands_loc (loc, integer_type_node, integer_zero_node, |
c4fef134 | 7217 | arg1, arg2); |
9c8a1629 | 7218 | |
7219 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7220 | if (operand_equal_p (arg1, arg2, 0)) | |
389dd41b | 7221 | return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len); |
9c8a1629 | 7222 | |
c4fef134 | 7223 | /* If the second arg is "", and the length is greater than zero, |
7224 | return *(const unsigned char*)arg1. */ | |
b5e46e2c | 7225 | const char *p2 = c_getstr (arg2); |
c4fef134 | 7226 | if (p2 && *p2 == '\0' |
7227 | && TREE_CODE (len) == INTEGER_CST | |
7228 | && tree_int_cst_sgn (len) == 1) | |
7229 | { | |
7230 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7231 | tree cst_uchar_ptr_node |
7232 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7233 | ||
389dd41b | 7234 | return fold_convert_loc (loc, integer_type_node, |
7235 | build1 (INDIRECT_REF, cst_uchar_node, | |
7236 | fold_convert_loc (loc, | |
7237 | cst_uchar_ptr_node, | |
7238 | arg1))); | |
c4fef134 | 7239 | } |
7240 | ||
7241 | /* If the first arg is "", and the length is greater than zero, | |
7242 | return -*(const unsigned char*)arg2. */ | |
b5e46e2c | 7243 | const char *p1 = c_getstr (arg1); |
c4fef134 | 7244 | if (p1 && *p1 == '\0' |
7245 | && TREE_CODE (len) == INTEGER_CST | |
7246 | && tree_int_cst_sgn (len) == 1) | |
7247 | { | |
7248 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7249 | tree cst_uchar_ptr_node |
7250 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7251 | ||
389dd41b | 7252 | tree temp = fold_convert_loc (loc, integer_type_node, |
7253 | build1 (INDIRECT_REF, cst_uchar_node, | |
7254 | fold_convert_loc (loc, | |
7255 | cst_uchar_ptr_node, | |
7256 | arg2))); | |
7257 | return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp); | |
c4fef134 | 7258 | } |
7259 | ||
7260 | /* If len parameter is one, return an expression corresponding to | |
7261 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
e913b5cd | 7262 | if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1) |
c4fef134 | 7263 | { |
7264 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 7265 | tree cst_uchar_ptr_node |
7266 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7267 | ||
389dd41b | 7268 | tree ind1 = fold_convert_loc (loc, integer_type_node, |
7269 | build1 (INDIRECT_REF, cst_uchar_node, | |
7270 | fold_convert_loc (loc, | |
7271 | cst_uchar_ptr_node, | |
7272 | arg1))); | |
7273 | tree ind2 = fold_convert_loc (loc, integer_type_node, | |
7274 | build1 (INDIRECT_REF, cst_uchar_node, | |
7275 | fold_convert_loc (loc, | |
7276 | cst_uchar_ptr_node, | |
7277 | arg2))); | |
7278 | return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2); | |
9c8a1629 | 7279 | } |
7280 | ||
c2f47e15 | 7281 | return NULL_TREE; |
9c8a1629 | 7282 | } |
7283 | ||
c2f47e15 | 7284 | /* Fold a call to builtin isascii with argument ARG. */ |
d49367d4 | 7285 | |
7286 | static tree | |
389dd41b | 7287 | fold_builtin_isascii (location_t loc, tree arg) |
d49367d4 | 7288 | { |
c2f47e15 | 7289 | if (!validate_arg (arg, INTEGER_TYPE)) |
7290 | return NULL_TREE; | |
d49367d4 | 7291 | else |
7292 | { | |
7293 | /* Transform isascii(c) -> ((c & ~0x7f) == 0). */ | |
c90b5d40 | 7294 | arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg, |
7002a1c8 | 7295 | build_int_cst (integer_type_node, |
c90b5d40 | 7296 | ~ (unsigned HOST_WIDE_INT) 0x7f)); |
389dd41b | 7297 | return fold_build2_loc (loc, EQ_EXPR, integer_type_node, |
7002a1c8 | 7298 | arg, integer_zero_node); |
d49367d4 | 7299 | } |
7300 | } | |
7301 | ||
c2f47e15 | 7302 | /* Fold a call to builtin toascii with argument ARG. */ |
d49367d4 | 7303 | |
7304 | static tree | |
389dd41b | 7305 | fold_builtin_toascii (location_t loc, tree arg) |
d49367d4 | 7306 | { |
c2f47e15 | 7307 | if (!validate_arg (arg, INTEGER_TYPE)) |
7308 | return NULL_TREE; | |
48e1416a | 7309 | |
c2f47e15 | 7310 | /* Transform toascii(c) -> (c & 0x7f). */ |
389dd41b | 7311 | return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg, |
7002a1c8 | 7312 | build_int_cst (integer_type_node, 0x7f)); |
d49367d4 | 7313 | } |
7314 | ||
c2f47e15 | 7315 | /* Fold a call to builtin isdigit with argument ARG. */ |
df1cf42e | 7316 | |
7317 | static tree | |
389dd41b | 7318 | fold_builtin_isdigit (location_t loc, tree arg) |
df1cf42e | 7319 | { |
c2f47e15 | 7320 | if (!validate_arg (arg, INTEGER_TYPE)) |
7321 | return NULL_TREE; | |
df1cf42e | 7322 | else |
7323 | { | |
7324 | /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ | |
624d37a6 | 7325 | /* According to the C standard, isdigit is unaffected by locale. |
7326 | However, it definitely is affected by the target character set. */ | |
624d37a6 | 7327 | unsigned HOST_WIDE_INT target_digit0 |
7328 | = lang_hooks.to_target_charset ('0'); | |
7329 | ||
7330 | if (target_digit0 == 0) | |
7331 | return NULL_TREE; | |
7332 | ||
389dd41b | 7333 | arg = fold_convert_loc (loc, unsigned_type_node, arg); |
c90b5d40 | 7334 | arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg, |
7335 | build_int_cst (unsigned_type_node, target_digit0)); | |
389dd41b | 7336 | return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg, |
f2532264 | 7337 | build_int_cst (unsigned_type_node, 9)); |
df1cf42e | 7338 | } |
7339 | } | |
27f261ef | 7340 | |
c2f47e15 | 7341 | /* Fold a call to fabs, fabsf or fabsl with argument ARG. */ |
d1aade50 | 7342 | |
7343 | static tree | |
389dd41b | 7344 | fold_builtin_fabs (location_t loc, tree arg, tree type) |
d1aade50 | 7345 | { |
c2f47e15 | 7346 | if (!validate_arg (arg, REAL_TYPE)) |
7347 | return NULL_TREE; | |
d1aade50 | 7348 | |
389dd41b | 7349 | arg = fold_convert_loc (loc, type, arg); |
389dd41b | 7350 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
d1aade50 | 7351 | } |
7352 | ||
c2f47e15 | 7353 | /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */ |
d1aade50 | 7354 | |
7355 | static tree | |
389dd41b | 7356 | fold_builtin_abs (location_t loc, tree arg, tree type) |
d1aade50 | 7357 | { |
c2f47e15 | 7358 | if (!validate_arg (arg, INTEGER_TYPE)) |
7359 | return NULL_TREE; | |
d1aade50 | 7360 | |
389dd41b | 7361 | arg = fold_convert_loc (loc, type, arg); |
389dd41b | 7362 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
d1aade50 | 7363 | } |
7364 | ||
b9be572e | 7365 | /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */ |
7366 | ||
7367 | static tree | |
7368 | fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type) | |
7369 | { | |
866b3d58 | 7370 | /* ??? Only expand to FMA_EXPR if it's directly supported. */ |
b9be572e | 7371 | if (validate_arg (arg0, REAL_TYPE) |
9af5ce0c | 7372 | && validate_arg (arg1, REAL_TYPE) |
866b3d58 | 7373 | && validate_arg (arg2, REAL_TYPE) |
7374 | && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing) | |
7375 | return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2); | |
b9be572e | 7376 | |
b9be572e | 7377 | return NULL_TREE; |
7378 | } | |
7379 | ||
abe4dcf6 | 7380 | /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */ |
7381 | ||
7382 | static tree | |
389dd41b | 7383 | fold_builtin_carg (location_t loc, tree arg, tree type) |
abe4dcf6 | 7384 | { |
239d491a | 7385 | if (validate_arg (arg, COMPLEX_TYPE) |
7386 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE) | |
abe4dcf6 | 7387 | { |
7388 | tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2); | |
48e1416a | 7389 | |
abe4dcf6 | 7390 | if (atan2_fn) |
7391 | { | |
c2f47e15 | 7392 | tree new_arg = builtin_save_expr (arg); |
389dd41b | 7393 | tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg); |
7394 | tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg); | |
7395 | return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg); | |
abe4dcf6 | 7396 | } |
7397 | } | |
48e1416a | 7398 | |
abe4dcf6 | 7399 | return NULL_TREE; |
7400 | } | |
7401 | ||
3838b9ae | 7402 | /* Fold a call to builtin frexp, we can assume the base is 2. */ |
7403 | ||
7404 | static tree | |
389dd41b | 7405 | fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype) |
3838b9ae | 7406 | { |
7407 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
7408 | return NULL_TREE; | |
48e1416a | 7409 | |
3838b9ae | 7410 | STRIP_NOPS (arg0); |
48e1416a | 7411 | |
3838b9ae | 7412 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
7413 | return NULL_TREE; | |
48e1416a | 7414 | |
389dd41b | 7415 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
3838b9ae | 7416 | |
7417 | /* Proceed if a valid pointer type was passed in. */ | |
7418 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node) | |
7419 | { | |
7420 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
7421 | tree frac, exp; | |
48e1416a | 7422 | |
3838b9ae | 7423 | switch (value->cl) |
7424 | { | |
7425 | case rvc_zero: | |
7426 | /* For +-0, return (*exp = 0, +-0). */ | |
7427 | exp = integer_zero_node; | |
7428 | frac = arg0; | |
7429 | break; | |
7430 | case rvc_nan: | |
7431 | case rvc_inf: | |
7432 | /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */ | |
389dd41b | 7433 | return omit_one_operand_loc (loc, rettype, arg0, arg1); |
3838b9ae | 7434 | case rvc_normal: |
7435 | { | |
7436 | /* Since the frexp function always expects base 2, and in | |
7437 | GCC normalized significands are already in the range | |
7438 | [0.5, 1.0), we have exactly what frexp wants. */ | |
7439 | REAL_VALUE_TYPE frac_rvt = *value; | |
7440 | SET_REAL_EXP (&frac_rvt, 0); | |
7441 | frac = build_real (rettype, frac_rvt); | |
7002a1c8 | 7442 | exp = build_int_cst (integer_type_node, REAL_EXP (value)); |
3838b9ae | 7443 | } |
7444 | break; | |
7445 | default: | |
7446 | gcc_unreachable (); | |
7447 | } | |
48e1416a | 7448 | |
3838b9ae | 7449 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
389dd41b | 7450 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp); |
3838b9ae | 7451 | TREE_SIDE_EFFECTS (arg1) = 1; |
389dd41b | 7452 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac); |
3838b9ae | 7453 | } |
7454 | ||
7455 | return NULL_TREE; | |
7456 | } | |
7457 | ||
ebf8b4f5 | 7458 | /* Fold a call to builtin modf. */ |
7459 | ||
7460 | static tree | |
389dd41b | 7461 | fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype) |
ebf8b4f5 | 7462 | { |
7463 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
7464 | return NULL_TREE; | |
48e1416a | 7465 | |
ebf8b4f5 | 7466 | STRIP_NOPS (arg0); |
48e1416a | 7467 | |
ebf8b4f5 | 7468 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
7469 | return NULL_TREE; | |
48e1416a | 7470 | |
389dd41b | 7471 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
ebf8b4f5 | 7472 | |
7473 | /* Proceed if a valid pointer type was passed in. */ | |
7474 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype)) | |
7475 | { | |
7476 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
7477 | REAL_VALUE_TYPE trunc, frac; | |
7478 | ||
7479 | switch (value->cl) | |
7480 | { | |
7481 | case rvc_nan: | |
7482 | case rvc_zero: | |
7483 | /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */ | |
7484 | trunc = frac = *value; | |
7485 | break; | |
7486 | case rvc_inf: | |
7487 | /* For +-Inf, return (*arg1 = arg0, +-0). */ | |
7488 | frac = dconst0; | |
7489 | frac.sign = value->sign; | |
7490 | trunc = *value; | |
7491 | break; | |
7492 | case rvc_normal: | |
7493 | /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */ | |
7494 | real_trunc (&trunc, VOIDmode, value); | |
7495 | real_arithmetic (&frac, MINUS_EXPR, value, &trunc); | |
7496 | /* If the original number was negative and already | |
7497 | integral, then the fractional part is -0.0. */ | |
7498 | if (value->sign && frac.cl == rvc_zero) | |
7499 | frac.sign = value->sign; | |
7500 | break; | |
7501 | } | |
48e1416a | 7502 | |
ebf8b4f5 | 7503 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
389dd41b | 7504 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, |
ebf8b4f5 | 7505 | build_real (rettype, trunc)); |
7506 | TREE_SIDE_EFFECTS (arg1) = 1; | |
389dd41b | 7507 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, |
ebf8b4f5 | 7508 | build_real (rettype, frac)); |
7509 | } | |
48e1416a | 7510 | |
ebf8b4f5 | 7511 | return NULL_TREE; |
7512 | } | |
7513 | ||
a65c4d64 | 7514 | /* Given a location LOC, an interclass builtin function decl FNDECL |
7515 | and its single argument ARG, return an folded expression computing | |
7516 | the same, or NULL_TREE if we either couldn't or didn't want to fold | |
7517 | (the latter happen if there's an RTL instruction available). */ | |
7518 | ||
7519 | static tree | |
7520 | fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg) | |
7521 | { | |
3754d046 | 7522 | machine_mode mode; |
a65c4d64 | 7523 | |
7524 | if (!validate_arg (arg, REAL_TYPE)) | |
7525 | return NULL_TREE; | |
7526 | ||
7527 | if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing) | |
7528 | return NULL_TREE; | |
7529 | ||
7530 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
7531 | ||
7f38718f | 7532 | bool is_ibm_extended = MODE_COMPOSITE_P (mode); |
7533 | ||
a65c4d64 | 7534 | /* If there is no optab, try generic code. */ |
7535 | switch (DECL_FUNCTION_CODE (fndecl)) | |
7536 | { | |
7537 | tree result; | |
7538 | ||
7539 | CASE_FLT_FN (BUILT_IN_ISINF): | |
7540 | { | |
7541 | /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */ | |
b9a16870 | 7542 | tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); |
7f38718f | 7543 | tree type = TREE_TYPE (arg); |
a65c4d64 | 7544 | REAL_VALUE_TYPE r; |
7545 | char buf[128]; | |
7546 | ||
7f38718f | 7547 | if (is_ibm_extended) |
7548 | { | |
7549 | /* NaN and Inf are encoded in the high-order double value | |
7550 | only. The low-order value is not significant. */ | |
7551 | type = double_type_node; | |
7552 | mode = DFmode; | |
7553 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7554 | } | |
a65c4d64 | 7555 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7556 | real_from_string (&r, buf); | |
7557 | result = build_call_expr (isgr_fn, 2, | |
7558 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7559 | build_real (type, r)); | |
7560 | return result; | |
7561 | } | |
7562 | CASE_FLT_FN (BUILT_IN_FINITE): | |
7563 | case BUILT_IN_ISFINITE: | |
7564 | { | |
7565 | /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */ | |
b9a16870 | 7566 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); |
7f38718f | 7567 | tree type = TREE_TYPE (arg); |
a65c4d64 | 7568 | REAL_VALUE_TYPE r; |
7569 | char buf[128]; | |
7570 | ||
7f38718f | 7571 | if (is_ibm_extended) |
7572 | { | |
7573 | /* NaN and Inf are encoded in the high-order double value | |
7574 | only. The low-order value is not significant. */ | |
7575 | type = double_type_node; | |
7576 | mode = DFmode; | |
7577 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7578 | } | |
a65c4d64 | 7579 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7580 | real_from_string (&r, buf); | |
7581 | result = build_call_expr (isle_fn, 2, | |
7582 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7583 | build_real (type, r)); | |
7584 | /*result = fold_build2_loc (loc, UNGT_EXPR, | |
7585 | TREE_TYPE (TREE_TYPE (fndecl)), | |
7586 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7587 | build_real (type, r)); | |
7588 | result = fold_build1_loc (loc, TRUTH_NOT_EXPR, | |
7589 | TREE_TYPE (TREE_TYPE (fndecl)), | |
7590 | result);*/ | |
7591 | return result; | |
7592 | } | |
7593 | case BUILT_IN_ISNORMAL: | |
7594 | { | |
7595 | /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) & | |
7596 | islessequal(fabs(x),DBL_MAX). */ | |
b9a16870 | 7597 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); |
7f38718f | 7598 | tree type = TREE_TYPE (arg); |
7599 | tree orig_arg, max_exp, min_exp; | |
7600 | machine_mode orig_mode = mode; | |
a65c4d64 | 7601 | REAL_VALUE_TYPE rmax, rmin; |
7602 | char buf[128]; | |
7603 | ||
7f38718f | 7604 | orig_arg = arg = builtin_save_expr (arg); |
7605 | if (is_ibm_extended) | |
7606 | { | |
7607 | /* Use double to test the normal range of IBM extended | |
7608 | precision. Emin for IBM extended precision is | |
7609 | different to emin for IEEE double, being 53 higher | |
7610 | since the low double exponent is at least 53 lower | |
7611 | than the high double exponent. */ | |
7612 | type = double_type_node; | |
7613 | mode = DFmode; | |
7614 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7615 | } | |
7616 | arg = fold_build1_loc (loc, ABS_EXPR, type, arg); | |
7617 | ||
a65c4d64 | 7618 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7619 | real_from_string (&rmax, buf); | |
7f38718f | 7620 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1); |
a65c4d64 | 7621 | real_from_string (&rmin, buf); |
7f38718f | 7622 | max_exp = build_real (type, rmax); |
7623 | min_exp = build_real (type, rmin); | |
7624 | ||
7625 | max_exp = build_call_expr (isle_fn, 2, arg, max_exp); | |
7626 | if (is_ibm_extended) | |
7627 | { | |
7628 | /* Testing the high end of the range is done just using | |
7629 | the high double, using the same test as isfinite(). | |
7630 | For the subnormal end of the range we first test the | |
7631 | high double, then if its magnitude is equal to the | |
7632 | limit of 0x1p-969, we test whether the low double is | |
7633 | non-zero and opposite sign to the high double. */ | |
7634 | tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS); | |
7635 | tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); | |
7636 | tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp); | |
7637 | tree eq_min = fold_build2 (EQ_EXPR, integer_type_node, | |
7638 | arg, min_exp); | |
7639 | tree as_complex = build1 (VIEW_CONVERT_EXPR, | |
7640 | complex_double_type_node, orig_arg); | |
7641 | tree hi_dbl = build1 (REALPART_EXPR, type, as_complex); | |
7642 | tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex); | |
7643 | tree zero = build_real (type, dconst0); | |
7644 | tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero); | |
7645 | tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero); | |
7646 | tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero); | |
7647 | tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node, | |
7648 | fold_build3 (COND_EXPR, | |
7649 | integer_type_node, | |
7650 | hilt, logt, lolt)); | |
7651 | eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node, | |
7652 | eq_min, ok_lo); | |
7653 | min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node, | |
7654 | gt_min, eq_min); | |
7655 | } | |
7656 | else | |
7657 | { | |
7658 | tree const isge_fn | |
7659 | = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL); | |
7660 | min_exp = build_call_expr (isge_fn, 2, arg, min_exp); | |
7661 | } | |
7662 | result = fold_build2 (BIT_AND_EXPR, integer_type_node, | |
7663 | max_exp, min_exp); | |
a65c4d64 | 7664 | return result; |
7665 | } | |
7666 | default: | |
7667 | break; | |
7668 | } | |
7669 | ||
7670 | return NULL_TREE; | |
7671 | } | |
7672 | ||
726069ba | 7673 | /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite. |
c2f47e15 | 7674 | ARG is the argument for the call. */ |
726069ba | 7675 | |
7676 | static tree | |
389dd41b | 7677 | fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index) |
726069ba | 7678 | { |
726069ba | 7679 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
726069ba | 7680 | |
c2f47e15 | 7681 | if (!validate_arg (arg, REAL_TYPE)) |
d43cee80 | 7682 | return NULL_TREE; |
726069ba | 7683 | |
726069ba | 7684 | switch (builtin_index) |
7685 | { | |
7686 | case BUILT_IN_ISINF: | |
fe994837 | 7687 | if (!HONOR_INFINITIES (arg)) |
389dd41b | 7688 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
726069ba | 7689 | |
726069ba | 7690 | return NULL_TREE; |
7691 | ||
c319d56a | 7692 | case BUILT_IN_ISINF_SIGN: |
7693 | { | |
7694 | /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */ | |
7695 | /* In a boolean context, GCC will fold the inner COND_EXPR to | |
7696 | 1. So e.g. "if (isinf_sign(x))" would be folded to just | |
7697 | "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */ | |
e3240774 | 7698 | tree signbit_fn = mathfn_built_in_1 |
7699 | (TREE_TYPE (arg), CFN_BUILT_IN_SIGNBIT, 0); | |
b9a16870 | 7700 | tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF); |
c319d56a | 7701 | tree tmp = NULL_TREE; |
7702 | ||
7703 | arg = builtin_save_expr (arg); | |
7704 | ||
7705 | if (signbit_fn && isinf_fn) | |
7706 | { | |
389dd41b | 7707 | tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg); |
7708 | tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg); | |
c319d56a | 7709 | |
389dd41b | 7710 | signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
c319d56a | 7711 | signbit_call, integer_zero_node); |
389dd41b | 7712 | isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
c319d56a | 7713 | isinf_call, integer_zero_node); |
48e1416a | 7714 | |
389dd41b | 7715 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call, |
c319d56a | 7716 | integer_minus_one_node, integer_one_node); |
389dd41b | 7717 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, |
7718 | isinf_call, tmp, | |
c319d56a | 7719 | integer_zero_node); |
7720 | } | |
7721 | ||
7722 | return tmp; | |
7723 | } | |
7724 | ||
cde061c1 | 7725 | case BUILT_IN_ISFINITE: |
93633022 | 7726 | if (!HONOR_NANS (arg) |
fe994837 | 7727 | && !HONOR_INFINITIES (arg)) |
389dd41b | 7728 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
726069ba | 7729 | |
726069ba | 7730 | return NULL_TREE; |
7731 | ||
7732 | case BUILT_IN_ISNAN: | |
93633022 | 7733 | if (!HONOR_NANS (arg)) |
389dd41b | 7734 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
726069ba | 7735 | |
7f38718f | 7736 | { |
7737 | bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg))); | |
7738 | if (is_ibm_extended) | |
7739 | { | |
7740 | /* NaN and Inf are encoded in the high-order double value | |
7741 | only. The low-order value is not significant. */ | |
7742 | arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg); | |
7743 | } | |
7744 | } | |
726069ba | 7745 | arg = builtin_save_expr (arg); |
389dd41b | 7746 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg); |
726069ba | 7747 | |
7748 | default: | |
64db345d | 7749 | gcc_unreachable (); |
726069ba | 7750 | } |
7751 | } | |
7752 | ||
19fbe3a4 | 7753 | /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...). |
7754 | This builtin will generate code to return the appropriate floating | |
7755 | point classification depending on the value of the floating point | |
7756 | number passed in. The possible return values must be supplied as | |
921b27c0 | 7757 | int arguments to the call in the following order: FP_NAN, FP_INFINITE, |
19fbe3a4 | 7758 | FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly |
7759 | one floating point argument which is "type generic". */ | |
7760 | ||
7761 | static tree | |
9d884767 | 7762 | fold_builtin_fpclassify (location_t loc, tree *args, int nargs) |
19fbe3a4 | 7763 | { |
921b27c0 | 7764 | tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero, |
7765 | arg, type, res, tmp; | |
3754d046 | 7766 | machine_mode mode; |
19fbe3a4 | 7767 | REAL_VALUE_TYPE r; |
7768 | char buf[128]; | |
48e1416a | 7769 | |
19fbe3a4 | 7770 | /* Verify the required arguments in the original call. */ |
9d884767 | 7771 | if (nargs != 6 |
7772 | || !validate_arg (args[0], INTEGER_TYPE) | |
7773 | || !validate_arg (args[1], INTEGER_TYPE) | |
7774 | || !validate_arg (args[2], INTEGER_TYPE) | |
7775 | || !validate_arg (args[3], INTEGER_TYPE) | |
7776 | || !validate_arg (args[4], INTEGER_TYPE) | |
7777 | || !validate_arg (args[5], REAL_TYPE)) | |
19fbe3a4 | 7778 | return NULL_TREE; |
48e1416a | 7779 | |
9d884767 | 7780 | fp_nan = args[0]; |
7781 | fp_infinite = args[1]; | |
7782 | fp_normal = args[2]; | |
7783 | fp_subnormal = args[3]; | |
7784 | fp_zero = args[4]; | |
7785 | arg = args[5]; | |
19fbe3a4 | 7786 | type = TREE_TYPE (arg); |
7787 | mode = TYPE_MODE (type); | |
389dd41b | 7788 | arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg)); |
19fbe3a4 | 7789 | |
48e1416a | 7790 | /* fpclassify(x) -> |
19fbe3a4 | 7791 | isnan(x) ? FP_NAN : |
921b27c0 | 7792 | (fabs(x) == Inf ? FP_INFINITE : |
19fbe3a4 | 7793 | (fabs(x) >= DBL_MIN ? FP_NORMAL : |
7794 | (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */ | |
48e1416a | 7795 | |
389dd41b | 7796 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, |
19fbe3a4 | 7797 | build_real (type, dconst0)); |
389dd41b | 7798 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, |
7799 | tmp, fp_zero, fp_subnormal); | |
19fbe3a4 | 7800 | |
7801 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1); | |
7802 | real_from_string (&r, buf); | |
389dd41b | 7803 | tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node, |
7804 | arg, build_real (type, r)); | |
7805 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res); | |
48e1416a | 7806 | |
19fbe3a4 | 7807 | if (HONOR_INFINITIES (mode)) |
7808 | { | |
7809 | real_inf (&r); | |
389dd41b | 7810 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, |
19fbe3a4 | 7811 | build_real (type, r)); |
389dd41b | 7812 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, |
7813 | fp_infinite, res); | |
19fbe3a4 | 7814 | } |
7815 | ||
7816 | if (HONOR_NANS (mode)) | |
7817 | { | |
389dd41b | 7818 | tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg); |
7819 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan); | |
19fbe3a4 | 7820 | } |
48e1416a | 7821 | |
19fbe3a4 | 7822 | return res; |
7823 | } | |
7824 | ||
9bc9f15f | 7825 | /* Fold a call to an unordered comparison function such as |
d5019fe8 | 7826 | __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function |
c2f47e15 | 7827 | being called and ARG0 and ARG1 are the arguments for the call. |
726069ba | 7828 | UNORDERED_CODE and ORDERED_CODE are comparison codes that give |
7829 | the opposite of the desired result. UNORDERED_CODE is used | |
7830 | for modes that can hold NaNs and ORDERED_CODE is used for | |
7831 | the rest. */ | |
9bc9f15f | 7832 | |
7833 | static tree | |
389dd41b | 7834 | fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1, |
9bc9f15f | 7835 | enum tree_code unordered_code, |
7836 | enum tree_code ordered_code) | |
7837 | { | |
859f903a | 7838 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
9bc9f15f | 7839 | enum tree_code code; |
6978db0d | 7840 | tree type0, type1; |
7841 | enum tree_code code0, code1; | |
7842 | tree cmp_type = NULL_TREE; | |
9bc9f15f | 7843 | |
6978db0d | 7844 | type0 = TREE_TYPE (arg0); |
7845 | type1 = TREE_TYPE (arg1); | |
a0c938f0 | 7846 | |
6978db0d | 7847 | code0 = TREE_CODE (type0); |
7848 | code1 = TREE_CODE (type1); | |
a0c938f0 | 7849 | |
6978db0d | 7850 | if (code0 == REAL_TYPE && code1 == REAL_TYPE) |
7851 | /* Choose the wider of two real types. */ | |
7852 | cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) | |
7853 | ? type0 : type1; | |
7854 | else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
7855 | cmp_type = type0; | |
7856 | else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) | |
7857 | cmp_type = type1; | |
a0c938f0 | 7858 | |
389dd41b | 7859 | arg0 = fold_convert_loc (loc, cmp_type, arg0); |
7860 | arg1 = fold_convert_loc (loc, cmp_type, arg1); | |
859f903a | 7861 | |
7862 | if (unordered_code == UNORDERED_EXPR) | |
7863 | { | |
93633022 | 7864 | if (!HONOR_NANS (arg0)) |
389dd41b | 7865 | return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1); |
7866 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1); | |
859f903a | 7867 | } |
9bc9f15f | 7868 | |
93633022 | 7869 | code = HONOR_NANS (arg0) ? unordered_code : ordered_code; |
389dd41b | 7870 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, |
7871 | fold_build2_loc (loc, code, type, arg0, arg1)); | |
9bc9f15f | 7872 | } |
7873 | ||
0c93c8a9 | 7874 | /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal |
7875 | arithmetics if it can never overflow, or into internal functions that | |
7876 | return both result of arithmetics and overflowed boolean flag in | |
7877 | a complex integer result, or some other check for overflow. */ | |
7878 | ||
7879 | static tree | |
7880 | fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode, | |
7881 | tree arg0, tree arg1, tree arg2) | |
7882 | { | |
7883 | enum internal_fn ifn = IFN_LAST; | |
7884 | tree type = TREE_TYPE (TREE_TYPE (arg2)); | |
7885 | tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2); | |
7886 | switch (fcode) | |
7887 | { | |
7888 | case BUILT_IN_ADD_OVERFLOW: | |
7889 | case BUILT_IN_SADD_OVERFLOW: | |
7890 | case BUILT_IN_SADDL_OVERFLOW: | |
7891 | case BUILT_IN_SADDLL_OVERFLOW: | |
7892 | case BUILT_IN_UADD_OVERFLOW: | |
7893 | case BUILT_IN_UADDL_OVERFLOW: | |
7894 | case BUILT_IN_UADDLL_OVERFLOW: | |
7895 | ifn = IFN_ADD_OVERFLOW; | |
7896 | break; | |
7897 | case BUILT_IN_SUB_OVERFLOW: | |
7898 | case BUILT_IN_SSUB_OVERFLOW: | |
7899 | case BUILT_IN_SSUBL_OVERFLOW: | |
7900 | case BUILT_IN_SSUBLL_OVERFLOW: | |
7901 | case BUILT_IN_USUB_OVERFLOW: | |
7902 | case BUILT_IN_USUBL_OVERFLOW: | |
7903 | case BUILT_IN_USUBLL_OVERFLOW: | |
7904 | ifn = IFN_SUB_OVERFLOW; | |
7905 | break; | |
7906 | case BUILT_IN_MUL_OVERFLOW: | |
7907 | case BUILT_IN_SMUL_OVERFLOW: | |
7908 | case BUILT_IN_SMULL_OVERFLOW: | |
7909 | case BUILT_IN_SMULLL_OVERFLOW: | |
7910 | case BUILT_IN_UMUL_OVERFLOW: | |
7911 | case BUILT_IN_UMULL_OVERFLOW: | |
7912 | case BUILT_IN_UMULLL_OVERFLOW: | |
7913 | ifn = IFN_MUL_OVERFLOW; | |
7914 | break; | |
7915 | default: | |
7916 | gcc_unreachable (); | |
7917 | } | |
7918 | tree ctype = build_complex_type (type); | |
7919 | tree call = build_call_expr_internal_loc (loc, ifn, ctype, | |
7920 | 2, arg0, arg1); | |
7921 | tree tgt = save_expr (call); | |
7922 | tree intres = build1_loc (loc, REALPART_EXPR, type, tgt); | |
7923 | tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt); | |
7924 | ovfres = fold_convert_loc (loc, boolean_type_node, ovfres); | |
7925 | tree store | |
7926 | = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres); | |
7927 | return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres); | |
7928 | } | |
7929 | ||
c2f47e15 | 7930 | /* Fold a call to built-in function FNDECL with 0 arguments. |
e80cc485 | 7931 | This function returns NULL_TREE if no simplification was possible. */ |
650e4c94 | 7932 | |
4ee9c684 | 7933 | static tree |
e80cc485 | 7934 | fold_builtin_0 (location_t loc, tree fndecl) |
650e4c94 | 7935 | { |
e9f80ff5 | 7936 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
c2f47e15 | 7937 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
189b3398 | 7938 | switch (fcode) |
650e4c94 | 7939 | { |
c2f47e15 | 7940 | CASE_FLT_FN (BUILT_IN_INF): |
7941 | case BUILT_IN_INFD32: | |
7942 | case BUILT_IN_INFD64: | |
7943 | case BUILT_IN_INFD128: | |
389dd41b | 7944 | return fold_builtin_inf (loc, type, true); |
7c2f0500 | 7945 | |
c2f47e15 | 7946 | CASE_FLT_FN (BUILT_IN_HUGE_VAL): |
389dd41b | 7947 | return fold_builtin_inf (loc, type, false); |
7c2f0500 | 7948 | |
c2f47e15 | 7949 | case BUILT_IN_CLASSIFY_TYPE: |
7950 | return fold_builtin_classify_type (NULL_TREE); | |
7c2f0500 | 7951 | |
c2f47e15 | 7952 | default: |
7953 | break; | |
7954 | } | |
7955 | return NULL_TREE; | |
7956 | } | |
7c2f0500 | 7957 | |
c2f47e15 | 7958 | /* Fold a call to built-in function FNDECL with 1 argument, ARG0. |
e80cc485 | 7959 | This function returns NULL_TREE if no simplification was possible. */ |
7c2f0500 | 7960 | |
c2f47e15 | 7961 | static tree |
e80cc485 | 7962 | fold_builtin_1 (location_t loc, tree fndecl, tree arg0) |
c2f47e15 | 7963 | { |
7964 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
7965 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6c21be92 | 7966 | |
7967 | if (TREE_CODE (arg0) == ERROR_MARK) | |
7968 | return NULL_TREE; | |
7969 | ||
744fe358 | 7970 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0)) |
6c21be92 | 7971 | return ret; |
7972 | ||
c2f47e15 | 7973 | switch (fcode) |
7974 | { | |
650e4c94 | 7975 | case BUILT_IN_CONSTANT_P: |
7c2f0500 | 7976 | { |
c2f47e15 | 7977 | tree val = fold_builtin_constant_p (arg0); |
7c2f0500 | 7978 | |
7c2f0500 | 7979 | /* Gimplification will pull the CALL_EXPR for the builtin out of |
7980 | an if condition. When not optimizing, we'll not CSE it back. | |
7981 | To avoid link error types of regressions, return false now. */ | |
7982 | if (!val && !optimize) | |
7983 | val = integer_zero_node; | |
7984 | ||
7985 | return val; | |
7986 | } | |
650e4c94 | 7987 | |
539a3a92 | 7988 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 7989 | return fold_builtin_classify_type (arg0); |
539a3a92 | 7990 | |
650e4c94 | 7991 | case BUILT_IN_STRLEN: |
c7cbde74 | 7992 | return fold_builtin_strlen (loc, type, arg0); |
650e4c94 | 7993 | |
4f35b1fc | 7994 | CASE_FLT_FN (BUILT_IN_FABS): |
8aa32773 | 7995 | case BUILT_IN_FABSD32: |
7996 | case BUILT_IN_FABSD64: | |
7997 | case BUILT_IN_FABSD128: | |
389dd41b | 7998 | return fold_builtin_fabs (loc, arg0, type); |
d1aade50 | 7999 | |
8000 | case BUILT_IN_ABS: | |
8001 | case BUILT_IN_LABS: | |
8002 | case BUILT_IN_LLABS: | |
8003 | case BUILT_IN_IMAXABS: | |
389dd41b | 8004 | return fold_builtin_abs (loc, arg0, type); |
c63f4ad3 | 8005 | |
4f35b1fc | 8006 | CASE_FLT_FN (BUILT_IN_CONJ): |
239d491a | 8007 | if (validate_arg (arg0, COMPLEX_TYPE) |
48e1416a | 8008 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
389dd41b | 8009 | return fold_build1_loc (loc, CONJ_EXPR, type, arg0); |
c2f47e15 | 8010 | break; |
36d3581d | 8011 | |
4f35b1fc | 8012 | CASE_FLT_FN (BUILT_IN_CREAL): |
239d491a | 8013 | if (validate_arg (arg0, COMPLEX_TYPE) |
48e1416a | 8014 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
7082509e | 8015 | return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0)); |
c2f47e15 | 8016 | break; |
36d3581d | 8017 | |
4f35b1fc | 8018 | CASE_FLT_FN (BUILT_IN_CIMAG): |
b0ce8887 | 8019 | if (validate_arg (arg0, COMPLEX_TYPE) |
8020 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) | |
389dd41b | 8021 | return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0)); |
c2f47e15 | 8022 | break; |
36d3581d | 8023 | |
6c21be92 | 8024 | CASE_FLT_FN (BUILT_IN_CARG): |
8025 | return fold_builtin_carg (loc, arg0, type); | |
c2373fdb | 8026 | |
6c21be92 | 8027 | case BUILT_IN_ISASCII: |
8028 | return fold_builtin_isascii (loc, arg0); | |
48e1416a | 8029 | |
6c21be92 | 8030 | case BUILT_IN_TOASCII: |
8031 | return fold_builtin_toascii (loc, arg0); | |
48e1416a | 8032 | |
6c21be92 | 8033 | case BUILT_IN_ISDIGIT: |
8034 | return fold_builtin_isdigit (loc, arg0); | |
48e1416a | 8035 | |
6c21be92 | 8036 | CASE_FLT_FN (BUILT_IN_FINITE): |
8037 | case BUILT_IN_FINITED32: | |
8038 | case BUILT_IN_FINITED64: | |
8039 | case BUILT_IN_FINITED128: | |
8040 | case BUILT_IN_ISFINITE: | |
8041 | { | |
8042 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE); | |
8043 | if (ret) | |
8044 | return ret; | |
8045 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
8046 | } | |
48e1416a | 8047 | |
6c21be92 | 8048 | CASE_FLT_FN (BUILT_IN_ISINF): |
8049 | case BUILT_IN_ISINFD32: | |
8050 | case BUILT_IN_ISINFD64: | |
8051 | case BUILT_IN_ISINFD128: | |
8052 | { | |
8053 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF); | |
8054 | if (ret) | |
8055 | return ret; | |
8056 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
8057 | } | |
48e1416a | 8058 | |
6c21be92 | 8059 | case BUILT_IN_ISNORMAL: |
8060 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
48e1416a | 8061 | |
6c21be92 | 8062 | case BUILT_IN_ISINF_SIGN: |
8063 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN); | |
48e1416a | 8064 | |
6c21be92 | 8065 | CASE_FLT_FN (BUILT_IN_ISNAN): |
8066 | case BUILT_IN_ISNAND32: | |
8067 | case BUILT_IN_ISNAND64: | |
8068 | case BUILT_IN_ISNAND128: | |
8069 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN); | |
48e1416a | 8070 | |
6c21be92 | 8071 | case BUILT_IN_FREE: |
8072 | if (integer_zerop (arg0)) | |
8073 | return build_empty_stmt (loc); | |
d064d976 | 8074 | break; |
c63f4ad3 | 8075 | |
6c21be92 | 8076 | default: |
8b4af95f | 8077 | break; |
6c21be92 | 8078 | } |
805e22b2 | 8079 | |
6c21be92 | 8080 | return NULL_TREE; |
3bc5c41b | 8081 | |
6c21be92 | 8082 | } |
728bac60 | 8083 | |
6c21be92 | 8084 | /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1. |
8085 | This function returns NULL_TREE if no simplification was possible. */ | |
c2f47e15 | 8086 | |
8087 | static tree | |
e80cc485 | 8088 | fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1) |
c2f47e15 | 8089 | { |
8090 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
8091 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
8092 | ||
6c21be92 | 8093 | if (TREE_CODE (arg0) == ERROR_MARK |
8094 | || TREE_CODE (arg1) == ERROR_MARK) | |
8095 | return NULL_TREE; | |
e5407ca6 | 8096 | |
744fe358 | 8097 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1)) |
6c21be92 | 8098 | return ret; |
e84da7c1 | 8099 | |
6c21be92 | 8100 | switch (fcode) |
8101 | { | |
e84da7c1 | 8102 | CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */ |
8103 | CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */ | |
8104 | if (validate_arg (arg0, REAL_TYPE) | |
9af5ce0c | 8105 | && validate_arg (arg1, POINTER_TYPE)) |
e84da7c1 | 8106 | return do_mpfr_lgamma_r (arg0, arg1, type); |
8107 | break; | |
c2f47e15 | 8108 | |
3838b9ae | 8109 | CASE_FLT_FN (BUILT_IN_FREXP): |
389dd41b | 8110 | return fold_builtin_frexp (loc, arg0, arg1, type); |
3838b9ae | 8111 | |
ebf8b4f5 | 8112 | CASE_FLT_FN (BUILT_IN_MODF): |
389dd41b | 8113 | return fold_builtin_modf (loc, arg0, arg1, type); |
ebf8b4f5 | 8114 | |
c2f47e15 | 8115 | case BUILT_IN_STRSTR: |
389dd41b | 8116 | return fold_builtin_strstr (loc, arg0, arg1, type); |
c2f47e15 | 8117 | |
c2f47e15 | 8118 | case BUILT_IN_STRSPN: |
389dd41b | 8119 | return fold_builtin_strspn (loc, arg0, arg1); |
c2f47e15 | 8120 | |
8121 | case BUILT_IN_STRCSPN: | |
389dd41b | 8122 | return fold_builtin_strcspn (loc, arg0, arg1); |
c2f47e15 | 8123 | |
8124 | case BUILT_IN_STRCHR: | |
8125 | case BUILT_IN_INDEX: | |
389dd41b | 8126 | return fold_builtin_strchr (loc, arg0, arg1, type); |
c2f47e15 | 8127 | |
8128 | case BUILT_IN_STRRCHR: | |
8129 | case BUILT_IN_RINDEX: | |
389dd41b | 8130 | return fold_builtin_strrchr (loc, arg0, arg1, type); |
c2f47e15 | 8131 | |
c2f47e15 | 8132 | case BUILT_IN_STRCMP: |
389dd41b | 8133 | return fold_builtin_strcmp (loc, arg0, arg1); |
c2f47e15 | 8134 | |
8135 | case BUILT_IN_STRPBRK: | |
389dd41b | 8136 | return fold_builtin_strpbrk (loc, arg0, arg1, type); |
c2f47e15 | 8137 | |
8138 | case BUILT_IN_EXPECT: | |
c83059be | 8139 | return fold_builtin_expect (loc, arg0, arg1, NULL_TREE); |
c2f47e15 | 8140 | |
9bc9f15f | 8141 | case BUILT_IN_ISGREATER: |
389dd41b | 8142 | return fold_builtin_unordered_cmp (loc, fndecl, |
8143 | arg0, arg1, UNLE_EXPR, LE_EXPR); | |
9bc9f15f | 8144 | case BUILT_IN_ISGREATEREQUAL: |
389dd41b | 8145 | return fold_builtin_unordered_cmp (loc, fndecl, |
8146 | arg0, arg1, UNLT_EXPR, LT_EXPR); | |
9bc9f15f | 8147 | case BUILT_IN_ISLESS: |
389dd41b | 8148 | return fold_builtin_unordered_cmp (loc, fndecl, |
8149 | arg0, arg1, UNGE_EXPR, GE_EXPR); | |
9bc9f15f | 8150 | case BUILT_IN_ISLESSEQUAL: |
389dd41b | 8151 | return fold_builtin_unordered_cmp (loc, fndecl, |
8152 | arg0, arg1, UNGT_EXPR, GT_EXPR); | |
9bc9f15f | 8153 | case BUILT_IN_ISLESSGREATER: |
389dd41b | 8154 | return fold_builtin_unordered_cmp (loc, fndecl, |
8155 | arg0, arg1, UNEQ_EXPR, EQ_EXPR); | |
9bc9f15f | 8156 | case BUILT_IN_ISUNORDERED: |
389dd41b | 8157 | return fold_builtin_unordered_cmp (loc, fndecl, |
8158 | arg0, arg1, UNORDERED_EXPR, | |
d5019fe8 | 8159 | NOP_EXPR); |
9bc9f15f | 8160 | |
7c2f0500 | 8161 | /* We do the folding for va_start in the expander. */ |
8162 | case BUILT_IN_VA_START: | |
8163 | break; | |
f0613857 | 8164 | |
0a39fd54 | 8165 | case BUILT_IN_OBJECT_SIZE: |
c2f47e15 | 8166 | return fold_builtin_object_size (arg0, arg1); |
0a39fd54 | 8167 | |
1cd6e20d | 8168 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: |
8169 | return fold_builtin_atomic_always_lock_free (arg0, arg1); | |
8170 | ||
8171 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
8172 | return fold_builtin_atomic_is_lock_free (arg0, arg1); | |
8173 | ||
c2f47e15 | 8174 | default: |
8175 | break; | |
8176 | } | |
8177 | return NULL_TREE; | |
8178 | } | |
8179 | ||
8180 | /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1, | |
e80cc485 | 8181 | and ARG2. |
c2f47e15 | 8182 | This function returns NULL_TREE if no simplification was possible. */ |
8183 | ||
8184 | static tree | |
389dd41b | 8185 | fold_builtin_3 (location_t loc, tree fndecl, |
e80cc485 | 8186 | tree arg0, tree arg1, tree arg2) |
c2f47e15 | 8187 | { |
8188 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
8189 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6c21be92 | 8190 | |
8191 | if (TREE_CODE (arg0) == ERROR_MARK | |
8192 | || TREE_CODE (arg1) == ERROR_MARK | |
8193 | || TREE_CODE (arg2) == ERROR_MARK) | |
8194 | return NULL_TREE; | |
8195 | ||
744fe358 | 8196 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, |
8197 | arg0, arg1, arg2)) | |
6c21be92 | 8198 | return ret; |
8199 | ||
c2f47e15 | 8200 | switch (fcode) |
8201 | { | |
8202 | ||
8203 | CASE_FLT_FN (BUILT_IN_SINCOS): | |
389dd41b | 8204 | return fold_builtin_sincos (loc, arg0, arg1, arg2); |
c2f47e15 | 8205 | |
8206 | CASE_FLT_FN (BUILT_IN_FMA): | |
b9be572e | 8207 | return fold_builtin_fma (loc, arg0, arg1, arg2, type); |
c2f47e15 | 8208 | |
e5407ca6 | 8209 | CASE_FLT_FN (BUILT_IN_REMQUO): |
8210 | if (validate_arg (arg0, REAL_TYPE) | |
9af5ce0c | 8211 | && validate_arg (arg1, REAL_TYPE) |
8212 | && validate_arg (arg2, POINTER_TYPE)) | |
e5407ca6 | 8213 | return do_mpfr_remquo (arg0, arg1, arg2); |
8214 | break; | |
e5407ca6 | 8215 | |
c2f47e15 | 8216 | case BUILT_IN_STRNCMP: |
389dd41b | 8217 | return fold_builtin_strncmp (loc, arg0, arg1, arg2); |
c2f47e15 | 8218 | |
7959b13b | 8219 | case BUILT_IN_MEMCHR: |
389dd41b | 8220 | return fold_builtin_memchr (loc, arg0, arg1, arg2, type); |
7959b13b | 8221 | |
c2f47e15 | 8222 | case BUILT_IN_BCMP: |
8223 | case BUILT_IN_MEMCMP: | |
389dd41b | 8224 | return fold_builtin_memcmp (loc, arg0, arg1, arg2);; |
c2f47e15 | 8225 | |
c83059be | 8226 | case BUILT_IN_EXPECT: |
8227 | return fold_builtin_expect (loc, arg0, arg1, arg2); | |
8228 | ||
0c93c8a9 | 8229 | case BUILT_IN_ADD_OVERFLOW: |
8230 | case BUILT_IN_SUB_OVERFLOW: | |
8231 | case BUILT_IN_MUL_OVERFLOW: | |
8232 | case BUILT_IN_SADD_OVERFLOW: | |
8233 | case BUILT_IN_SADDL_OVERFLOW: | |
8234 | case BUILT_IN_SADDLL_OVERFLOW: | |
8235 | case BUILT_IN_SSUB_OVERFLOW: | |
8236 | case BUILT_IN_SSUBL_OVERFLOW: | |
8237 | case BUILT_IN_SSUBLL_OVERFLOW: | |
8238 | case BUILT_IN_SMUL_OVERFLOW: | |
8239 | case BUILT_IN_SMULL_OVERFLOW: | |
8240 | case BUILT_IN_SMULLL_OVERFLOW: | |
8241 | case BUILT_IN_UADD_OVERFLOW: | |
8242 | case BUILT_IN_UADDL_OVERFLOW: | |
8243 | case BUILT_IN_UADDLL_OVERFLOW: | |
8244 | case BUILT_IN_USUB_OVERFLOW: | |
8245 | case BUILT_IN_USUBL_OVERFLOW: | |
8246 | case BUILT_IN_USUBLL_OVERFLOW: | |
8247 | case BUILT_IN_UMUL_OVERFLOW: | |
8248 | case BUILT_IN_UMULL_OVERFLOW: | |
8249 | case BUILT_IN_UMULLL_OVERFLOW: | |
8250 | return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2); | |
8251 | ||
650e4c94 | 8252 | default: |
8253 | break; | |
8254 | } | |
c2f47e15 | 8255 | return NULL_TREE; |
8256 | } | |
650e4c94 | 8257 | |
c2f47e15 | 8258 | /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS |
9d884767 | 8259 | arguments. IGNORE is true if the result of the |
8260 | function call is ignored. This function returns NULL_TREE if no | |
8261 | simplification was possible. */ | |
48e1416a | 8262 | |
2165588a | 8263 | tree |
e80cc485 | 8264 | fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool) |
c2f47e15 | 8265 | { |
8266 | tree ret = NULL_TREE; | |
a7f5bb2d | 8267 | |
c2f47e15 | 8268 | switch (nargs) |
8269 | { | |
8270 | case 0: | |
e80cc485 | 8271 | ret = fold_builtin_0 (loc, fndecl); |
c2f47e15 | 8272 | break; |
8273 | case 1: | |
e80cc485 | 8274 | ret = fold_builtin_1 (loc, fndecl, args[0]); |
c2f47e15 | 8275 | break; |
8276 | case 2: | |
e80cc485 | 8277 | ret = fold_builtin_2 (loc, fndecl, args[0], args[1]); |
c2f47e15 | 8278 | break; |
8279 | case 3: | |
e80cc485 | 8280 | ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]); |
c2f47e15 | 8281 | break; |
c2f47e15 | 8282 | default: |
e80cc485 | 8283 | ret = fold_builtin_varargs (loc, fndecl, args, nargs); |
c2f47e15 | 8284 | break; |
8285 | } | |
8286 | if (ret) | |
8287 | { | |
75a70cf9 | 8288 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); |
389dd41b | 8289 | SET_EXPR_LOCATION (ret, loc); |
c2f47e15 | 8290 | TREE_NO_WARNING (ret) = 1; |
8291 | return ret; | |
8292 | } | |
8293 | return NULL_TREE; | |
8294 | } | |
8295 | ||
0e80b01d | 8296 | /* Construct a new CALL_EXPR to FNDECL using the tail of the argument |
8297 | list ARGS along with N new arguments in NEWARGS. SKIP is the number | |
8298 | of arguments in ARGS to be omitted. OLDNARGS is the number of | |
8299 | elements in ARGS. */ | |
c2f47e15 | 8300 | |
8301 | static tree | |
0e80b01d | 8302 | rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args, |
8303 | int skip, tree fndecl, int n, va_list newargs) | |
c2f47e15 | 8304 | { |
0e80b01d | 8305 | int nargs = oldnargs - skip + n; |
8306 | tree *buffer; | |
c2f47e15 | 8307 | |
0e80b01d | 8308 | if (n > 0) |
c2f47e15 | 8309 | { |
0e80b01d | 8310 | int i, j; |
c2f47e15 | 8311 | |
0e80b01d | 8312 | buffer = XALLOCAVEC (tree, nargs); |
8313 | for (i = 0; i < n; i++) | |
8314 | buffer[i] = va_arg (newargs, tree); | |
8315 | for (j = skip; j < oldnargs; j++, i++) | |
8316 | buffer[i] = args[j]; | |
8317 | } | |
8318 | else | |
8319 | buffer = args + skip; | |
19fbe3a4 | 8320 | |
0e80b01d | 8321 | return build_call_expr_loc_array (loc, fndecl, nargs, buffer); |
8322 | } | |
c2f47e15 | 8323 | |
198622c0 | 8324 | /* Return true if FNDECL shouldn't be folded right now. |
8325 | If a built-in function has an inline attribute always_inline | |
8326 | wrapper, defer folding it after always_inline functions have | |
8327 | been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking | |
8328 | might not be performed. */ | |
8329 | ||
51d2c51e | 8330 | bool |
198622c0 | 8331 | avoid_folding_inline_builtin (tree fndecl) |
8332 | { | |
8333 | return (DECL_DECLARED_INLINE_P (fndecl) | |
8334 | && DECL_DISREGARD_INLINE_LIMITS (fndecl) | |
8335 | && cfun | |
8336 | && !cfun->always_inline_functions_inlined | |
8337 | && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl))); | |
8338 | } | |
8339 | ||
4ee9c684 | 8340 | /* A wrapper function for builtin folding that prevents warnings for |
491e04ef | 8341 | "statement without effect" and the like, caused by removing the |
4ee9c684 | 8342 | call node earlier than the warning is generated. */ |
8343 | ||
8344 | tree | |
389dd41b | 8345 | fold_call_expr (location_t loc, tree exp, bool ignore) |
4ee9c684 | 8346 | { |
c2f47e15 | 8347 | tree ret = NULL_TREE; |
8348 | tree fndecl = get_callee_fndecl (exp); | |
8349 | if (fndecl | |
8350 | && TREE_CODE (fndecl) == FUNCTION_DECL | |
48dc2227 | 8351 | && DECL_BUILT_IN (fndecl) |
8352 | /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized | |
8353 | yet. Defer folding until we see all the arguments | |
8354 | (after inlining). */ | |
8355 | && !CALL_EXPR_VA_ARG_PACK (exp)) | |
8356 | { | |
8357 | int nargs = call_expr_nargs (exp); | |
8358 | ||
8359 | /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but | |
8360 | instead last argument is __builtin_va_arg_pack (). Defer folding | |
8361 | even in that case, until arguments are finalized. */ | |
8362 | if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR) | |
8363 | { | |
8364 | tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1)); | |
8365 | if (fndecl2 | |
8366 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
8367 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
8368 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
8369 | return NULL_TREE; | |
8370 | } | |
8371 | ||
198622c0 | 8372 | if (avoid_folding_inline_builtin (fndecl)) |
8373 | return NULL_TREE; | |
8374 | ||
c2f47e15 | 8375 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
97d67146 | 8376 | return targetm.fold_builtin (fndecl, call_expr_nargs (exp), |
8377 | CALL_EXPR_ARGP (exp), ignore); | |
c2f47e15 | 8378 | else |
8379 | { | |
9d884767 | 8380 | tree *args = CALL_EXPR_ARGP (exp); |
8381 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); | |
c2f47e15 | 8382 | if (ret) |
389dd41b | 8383 | return ret; |
c2f47e15 | 8384 | } |
4ee9c684 | 8385 | } |
c2f47e15 | 8386 | return NULL_TREE; |
8387 | } | |
48e1416a | 8388 | |
9d884767 | 8389 | /* Fold a CALL_EXPR with type TYPE with FN as the function expression. |
8390 | N arguments are passed in the array ARGARRAY. Return a folded | |
8391 | expression or NULL_TREE if no simplification was possible. */ | |
805e22b2 | 8392 | |
8393 | tree | |
9d884767 | 8394 | fold_builtin_call_array (location_t loc, tree, |
d01f58f9 | 8395 | tree fn, |
8396 | int n, | |
8397 | tree *argarray) | |
7e15618b | 8398 | { |
9d884767 | 8399 | if (TREE_CODE (fn) != ADDR_EXPR) |
8400 | return NULL_TREE; | |
c2f47e15 | 8401 | |
9d884767 | 8402 | tree fndecl = TREE_OPERAND (fn, 0); |
8403 | if (TREE_CODE (fndecl) == FUNCTION_DECL | |
8404 | && DECL_BUILT_IN (fndecl)) | |
8405 | { | |
8406 | /* If last argument is __builtin_va_arg_pack (), arguments to this | |
8407 | function are not finalized yet. Defer folding until they are. */ | |
8408 | if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR) | |
8409 | { | |
8410 | tree fndecl2 = get_callee_fndecl (argarray[n - 1]); | |
8411 | if (fndecl2 | |
8412 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
8413 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
8414 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
8415 | return NULL_TREE; | |
8416 | } | |
8417 | if (avoid_folding_inline_builtin (fndecl)) | |
8418 | return NULL_TREE; | |
8419 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
8420 | return targetm.fold_builtin (fndecl, n, argarray, false); | |
8421 | else | |
8422 | return fold_builtin_n (loc, fndecl, argarray, n, false); | |
8423 | } | |
c2f47e15 | 8424 | |
9d884767 | 8425 | return NULL_TREE; |
c2f47e15 | 8426 | } |
8427 | ||
af1409ad | 8428 | /* Construct a new CALL_EXPR using the tail of the argument list of EXP |
8429 | along with N new arguments specified as the "..." parameters. SKIP | |
8430 | is the number of arguments in EXP to be omitted. This function is used | |
8431 | to do varargs-to-varargs transformations. */ | |
8432 | ||
8433 | static tree | |
8434 | rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...) | |
8435 | { | |
8436 | va_list ap; | |
8437 | tree t; | |
8438 | ||
8439 | va_start (ap, n); | |
8440 | t = rewrite_call_expr_valist (loc, call_expr_nargs (exp), | |
8441 | CALL_EXPR_ARGP (exp), skip, fndecl, n, ap); | |
8442 | va_end (ap); | |
c2f47e15 | 8443 | |
af1409ad | 8444 | return t; |
c2f47e15 | 8445 | } |
8446 | ||
8447 | /* Validate a single argument ARG against a tree code CODE representing | |
8448 | a type. */ | |
48e1416a | 8449 | |
c2f47e15 | 8450 | static bool |
b7bf20db | 8451 | validate_arg (const_tree arg, enum tree_code code) |
c2f47e15 | 8452 | { |
8453 | if (!arg) | |
8454 | return false; | |
8455 | else if (code == POINTER_TYPE) | |
8456 | return POINTER_TYPE_P (TREE_TYPE (arg)); | |
c7f617c2 | 8457 | else if (code == INTEGER_TYPE) |
8458 | return INTEGRAL_TYPE_P (TREE_TYPE (arg)); | |
c2f47e15 | 8459 | return code == TREE_CODE (TREE_TYPE (arg)); |
7e15618b | 8460 | } |
0eb671f7 | 8461 | |
75a70cf9 | 8462 | /* This function validates the types of a function call argument list |
8463 | against a specified list of tree_codes. If the last specifier is a 0, | |
8464 | that represents an ellipses, otherwise the last specifier must be a | |
8465 | VOID_TYPE. | |
8466 | ||
8467 | This is the GIMPLE version of validate_arglist. Eventually we want to | |
8468 | completely convert builtins.c to work from GIMPLEs and the tree based | |
8469 | validate_arglist will then be removed. */ | |
8470 | ||
8471 | bool | |
1a91d914 | 8472 | validate_gimple_arglist (const gcall *call, ...) |
75a70cf9 | 8473 | { |
8474 | enum tree_code code; | |
8475 | bool res = 0; | |
8476 | va_list ap; | |
8477 | const_tree arg; | |
8478 | size_t i; | |
8479 | ||
8480 | va_start (ap, call); | |
8481 | i = 0; | |
8482 | ||
8483 | do | |
8484 | { | |
d62e827b | 8485 | code = (enum tree_code) va_arg (ap, int); |
75a70cf9 | 8486 | switch (code) |
8487 | { | |
8488 | case 0: | |
8489 | /* This signifies an ellipses, any further arguments are all ok. */ | |
8490 | res = true; | |
8491 | goto end; | |
8492 | case VOID_TYPE: | |
8493 | /* This signifies an endlink, if no arguments remain, return | |
8494 | true, otherwise return false. */ | |
8495 | res = (i == gimple_call_num_args (call)); | |
8496 | goto end; | |
8497 | default: | |
8498 | /* If no parameters remain or the parameter's code does not | |
8499 | match the specified code, return false. Otherwise continue | |
8500 | checking any remaining arguments. */ | |
8501 | arg = gimple_call_arg (call, i++); | |
8502 | if (!validate_arg (arg, code)) | |
8503 | goto end; | |
8504 | break; | |
8505 | } | |
8506 | } | |
8507 | while (1); | |
8508 | ||
8509 | /* We need gotos here since we can only have one VA_CLOSE in a | |
8510 | function. */ | |
8511 | end: ; | |
8512 | va_end (ap); | |
8513 | ||
8514 | return res; | |
8515 | } | |
8516 | ||
fc2a2dcb | 8517 | /* Default target-specific builtin expander that does nothing. */ |
8518 | ||
8519 | rtx | |
aecda0d6 | 8520 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
8521 | rtx target ATTRIBUTE_UNUSED, | |
8522 | rtx subtarget ATTRIBUTE_UNUSED, | |
3754d046 | 8523 | machine_mode mode ATTRIBUTE_UNUSED, |
aecda0d6 | 8524 | int ignore ATTRIBUTE_UNUSED) |
fc2a2dcb | 8525 | { |
8526 | return NULL_RTX; | |
8527 | } | |
c7926a82 | 8528 | |
01537105 | 8529 | /* Returns true is EXP represents data that would potentially reside |
8530 | in a readonly section. */ | |
8531 | ||
b9ea678c | 8532 | bool |
01537105 | 8533 | readonly_data_expr (tree exp) |
8534 | { | |
8535 | STRIP_NOPS (exp); | |
8536 | ||
9ff0637e | 8537 | if (TREE_CODE (exp) != ADDR_EXPR) |
8538 | return false; | |
8539 | ||
8540 | exp = get_base_address (TREE_OPERAND (exp, 0)); | |
8541 | if (!exp) | |
8542 | return false; | |
8543 | ||
8544 | /* Make sure we call decl_readonly_section only for trees it | |
8545 | can handle (since it returns true for everything it doesn't | |
8546 | understand). */ | |
491e04ef | 8547 | if (TREE_CODE (exp) == STRING_CST |
9ff0637e | 8548 | || TREE_CODE (exp) == CONSTRUCTOR |
8549 | || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp))) | |
8550 | return decl_readonly_section (exp, 0); | |
01537105 | 8551 | else |
8552 | return false; | |
8553 | } | |
4ee9c684 | 8554 | |
c2f47e15 | 8555 | /* Simplify a call to the strstr builtin. S1 and S2 are the arguments |
8556 | to the call, and TYPE is its return type. | |
4ee9c684 | 8557 | |
c2f47e15 | 8558 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8559 | simplified form of the call as a tree. |
8560 | ||
8561 | The simplified form may be a constant or other expression which | |
8562 | computes the same value, but in a more efficient manner (including | |
8563 | calls to other builtin functions). | |
8564 | ||
8565 | The call may contain arguments which need to be evaluated, but | |
8566 | which are not useful to determine the result of the call. In | |
8567 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8568 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8569 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8570 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8571 | form of the builtin function call. */ | |
8572 | ||
8573 | static tree | |
389dd41b | 8574 | fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type) |
4ee9c684 | 8575 | { |
c2f47e15 | 8576 | if (!validate_arg (s1, POINTER_TYPE) |
8577 | || !validate_arg (s2, POINTER_TYPE)) | |
8578 | return NULL_TREE; | |
4ee9c684 | 8579 | else |
8580 | { | |
4ee9c684 | 8581 | tree fn; |
8582 | const char *p1, *p2; | |
8583 | ||
8584 | p2 = c_getstr (s2); | |
8585 | if (p2 == NULL) | |
c2f47e15 | 8586 | return NULL_TREE; |
4ee9c684 | 8587 | |
8588 | p1 = c_getstr (s1); | |
8589 | if (p1 != NULL) | |
8590 | { | |
8591 | const char *r = strstr (p1, p2); | |
daa1d5f5 | 8592 | tree tem; |
4ee9c684 | 8593 | |
4ee9c684 | 8594 | if (r == NULL) |
779b4c41 | 8595 | return build_int_cst (TREE_TYPE (s1), 0); |
c0c67e38 | 8596 | |
8597 | /* Return an offset into the constant string argument. */ | |
2cc66f2a | 8598 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
389dd41b | 8599 | return fold_convert_loc (loc, type, tem); |
4ee9c684 | 8600 | } |
8601 | ||
7efa231c | 8602 | /* The argument is const char *, and the result is char *, so we need |
8603 | a type conversion here to avoid a warning. */ | |
4ee9c684 | 8604 | if (p2[0] == '\0') |
389dd41b | 8605 | return fold_convert_loc (loc, type, s1); |
4ee9c684 | 8606 | |
8607 | if (p2[1] != '\0') | |
c2f47e15 | 8608 | return NULL_TREE; |
4ee9c684 | 8609 | |
b9a16870 | 8610 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
4ee9c684 | 8611 | if (!fn) |
c2f47e15 | 8612 | return NULL_TREE; |
4ee9c684 | 8613 | |
8614 | /* New argument list transforming strstr(s1, s2) to | |
8615 | strchr(s1, s2[0]). */ | |
7002a1c8 | 8616 | return build_call_expr_loc (loc, fn, 2, s1, |
8617 | build_int_cst (integer_type_node, p2[0])); | |
4ee9c684 | 8618 | } |
8619 | } | |
8620 | ||
c2f47e15 | 8621 | /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to |
8622 | the call, and TYPE is its return type. | |
4ee9c684 | 8623 | |
c2f47e15 | 8624 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8625 | simplified form of the call as a tree. |
8626 | ||
8627 | The simplified form may be a constant or other expression which | |
8628 | computes the same value, but in a more efficient manner (including | |
8629 | calls to other builtin functions). | |
8630 | ||
8631 | The call may contain arguments which need to be evaluated, but | |
8632 | which are not useful to determine the result of the call. In | |
8633 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8634 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8635 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8636 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8637 | form of the builtin function call. */ | |
8638 | ||
8639 | static tree | |
389dd41b | 8640 | fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type) |
4ee9c684 | 8641 | { |
c2f47e15 | 8642 | if (!validate_arg (s1, POINTER_TYPE) |
8643 | || !validate_arg (s2, INTEGER_TYPE)) | |
8644 | return NULL_TREE; | |
4ee9c684 | 8645 | else |
8646 | { | |
4ee9c684 | 8647 | const char *p1; |
8648 | ||
8649 | if (TREE_CODE (s2) != INTEGER_CST) | |
c2f47e15 | 8650 | return NULL_TREE; |
4ee9c684 | 8651 | |
8652 | p1 = c_getstr (s1); | |
8653 | if (p1 != NULL) | |
8654 | { | |
8655 | char c; | |
8656 | const char *r; | |
daa1d5f5 | 8657 | tree tem; |
4ee9c684 | 8658 | |
8659 | if (target_char_cast (s2, &c)) | |
c2f47e15 | 8660 | return NULL_TREE; |
4ee9c684 | 8661 | |
8662 | r = strchr (p1, c); | |
8663 | ||
8664 | if (r == NULL) | |
779b4c41 | 8665 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 8666 | |
8667 | /* Return an offset into the constant string argument. */ | |
2cc66f2a | 8668 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
389dd41b | 8669 | return fold_convert_loc (loc, type, tem); |
4ee9c684 | 8670 | } |
c2f47e15 | 8671 | return NULL_TREE; |
4ee9c684 | 8672 | } |
8673 | } | |
8674 | ||
c2f47e15 | 8675 | /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to |
8676 | the call, and TYPE is its return type. | |
4ee9c684 | 8677 | |
c2f47e15 | 8678 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8679 | simplified form of the call as a tree. |
8680 | ||
8681 | The simplified form may be a constant or other expression which | |
8682 | computes the same value, but in a more efficient manner (including | |
8683 | calls to other builtin functions). | |
8684 | ||
8685 | The call may contain arguments which need to be evaluated, but | |
8686 | which are not useful to determine the result of the call. In | |
8687 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8688 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8689 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8690 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8691 | form of the builtin function call. */ | |
8692 | ||
8693 | static tree | |
389dd41b | 8694 | fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type) |
4ee9c684 | 8695 | { |
c2f47e15 | 8696 | if (!validate_arg (s1, POINTER_TYPE) |
8697 | || !validate_arg (s2, INTEGER_TYPE)) | |
8698 | return NULL_TREE; | |
4ee9c684 | 8699 | else |
8700 | { | |
4ee9c684 | 8701 | tree fn; |
8702 | const char *p1; | |
8703 | ||
8704 | if (TREE_CODE (s2) != INTEGER_CST) | |
c2f47e15 | 8705 | return NULL_TREE; |
4ee9c684 | 8706 | |
8707 | p1 = c_getstr (s1); | |
8708 | if (p1 != NULL) | |
8709 | { | |
8710 | char c; | |
8711 | const char *r; | |
daa1d5f5 | 8712 | tree tem; |
4ee9c684 | 8713 | |
8714 | if (target_char_cast (s2, &c)) | |
c2f47e15 | 8715 | return NULL_TREE; |
4ee9c684 | 8716 | |
8717 | r = strrchr (p1, c); | |
8718 | ||
8719 | if (r == NULL) | |
779b4c41 | 8720 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 8721 | |
8722 | /* Return an offset into the constant string argument. */ | |
2cc66f2a | 8723 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
389dd41b | 8724 | return fold_convert_loc (loc, type, tem); |
4ee9c684 | 8725 | } |
8726 | ||
8727 | if (! integer_zerop (s2)) | |
c2f47e15 | 8728 | return NULL_TREE; |
4ee9c684 | 8729 | |
b9a16870 | 8730 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
4ee9c684 | 8731 | if (!fn) |
c2f47e15 | 8732 | return NULL_TREE; |
4ee9c684 | 8733 | |
8734 | /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */ | |
389dd41b | 8735 | return build_call_expr_loc (loc, fn, 2, s1, s2); |
4ee9c684 | 8736 | } |
8737 | } | |
8738 | ||
c2f47e15 | 8739 | /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments |
8740 | to the call, and TYPE is its return type. | |
4ee9c684 | 8741 | |
c2f47e15 | 8742 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8743 | simplified form of the call as a tree. |
8744 | ||
8745 | The simplified form may be a constant or other expression which | |
8746 | computes the same value, but in a more efficient manner (including | |
8747 | calls to other builtin functions). | |
8748 | ||
8749 | The call may contain arguments which need to be evaluated, but | |
8750 | which are not useful to determine the result of the call. In | |
8751 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8752 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8753 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8754 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8755 | form of the builtin function call. */ | |
8756 | ||
8757 | static tree | |
389dd41b | 8758 | fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type) |
4ee9c684 | 8759 | { |
c2f47e15 | 8760 | if (!validate_arg (s1, POINTER_TYPE) |
8761 | || !validate_arg (s2, POINTER_TYPE)) | |
8762 | return NULL_TREE; | |
4ee9c684 | 8763 | else |
8764 | { | |
4ee9c684 | 8765 | tree fn; |
8766 | const char *p1, *p2; | |
8767 | ||
8768 | p2 = c_getstr (s2); | |
8769 | if (p2 == NULL) | |
c2f47e15 | 8770 | return NULL_TREE; |
4ee9c684 | 8771 | |
8772 | p1 = c_getstr (s1); | |
8773 | if (p1 != NULL) | |
8774 | { | |
8775 | const char *r = strpbrk (p1, p2); | |
daa1d5f5 | 8776 | tree tem; |
4ee9c684 | 8777 | |
8778 | if (r == NULL) | |
779b4c41 | 8779 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 8780 | |
8781 | /* Return an offset into the constant string argument. */ | |
2cc66f2a | 8782 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
389dd41b | 8783 | return fold_convert_loc (loc, type, tem); |
4ee9c684 | 8784 | } |
8785 | ||
8786 | if (p2[0] == '\0') | |
05abc81b | 8787 | /* strpbrk(x, "") == NULL. |
8788 | Evaluate and ignore s1 in case it had side-effects. */ | |
389dd41b | 8789 | return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1); |
4ee9c684 | 8790 | |
8791 | if (p2[1] != '\0') | |
c2f47e15 | 8792 | return NULL_TREE; /* Really call strpbrk. */ |
4ee9c684 | 8793 | |
b9a16870 | 8794 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
4ee9c684 | 8795 | if (!fn) |
c2f47e15 | 8796 | return NULL_TREE; |
4ee9c684 | 8797 | |
8798 | /* New argument list transforming strpbrk(s1, s2) to | |
8799 | strchr(s1, s2[0]). */ | |
7002a1c8 | 8800 | return build_call_expr_loc (loc, fn, 2, s1, |
8801 | build_int_cst (integer_type_node, p2[0])); | |
4ee9c684 | 8802 | } |
8803 | } | |
8804 | ||
c2f47e15 | 8805 | /* Simplify a call to the strspn builtin. S1 and S2 are the arguments |
8806 | to the call. | |
4ee9c684 | 8807 | |
c2f47e15 | 8808 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8809 | simplified form of the call as a tree. |
8810 | ||
8811 | The simplified form may be a constant or other expression which | |
8812 | computes the same value, but in a more efficient manner (including | |
8813 | calls to other builtin functions). | |
8814 | ||
8815 | The call may contain arguments which need to be evaluated, but | |
8816 | which are not useful to determine the result of the call. In | |
8817 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8818 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8819 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8820 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8821 | form of the builtin function call. */ | |
8822 | ||
8823 | static tree | |
389dd41b | 8824 | fold_builtin_strspn (location_t loc, tree s1, tree s2) |
4ee9c684 | 8825 | { |
c2f47e15 | 8826 | if (!validate_arg (s1, POINTER_TYPE) |
8827 | || !validate_arg (s2, POINTER_TYPE)) | |
8828 | return NULL_TREE; | |
4ee9c684 | 8829 | else |
8830 | { | |
4ee9c684 | 8831 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); |
8832 | ||
c2f47e15 | 8833 | /* If either argument is "", return NULL_TREE. */ |
4ee9c684 | 8834 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) |
9bc9f15f | 8835 | /* Evaluate and ignore both arguments in case either one has |
8836 | side-effects. */ | |
389dd41b | 8837 | return omit_two_operands_loc (loc, size_type_node, size_zero_node, |
9bc9f15f | 8838 | s1, s2); |
c2f47e15 | 8839 | return NULL_TREE; |
4ee9c684 | 8840 | } |
8841 | } | |
8842 | ||
c2f47e15 | 8843 | /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments |
8844 | to the call. | |
4ee9c684 | 8845 | |
c2f47e15 | 8846 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 8847 | simplified form of the call as a tree. |
8848 | ||
8849 | The simplified form may be a constant or other expression which | |
8850 | computes the same value, but in a more efficient manner (including | |
8851 | calls to other builtin functions). | |
8852 | ||
8853 | The call may contain arguments which need to be evaluated, but | |
8854 | which are not useful to determine the result of the call. In | |
8855 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8856 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8857 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8858 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8859 | form of the builtin function call. */ | |
8860 | ||
8861 | static tree | |
389dd41b | 8862 | fold_builtin_strcspn (location_t loc, tree s1, tree s2) |
4ee9c684 | 8863 | { |
c2f47e15 | 8864 | if (!validate_arg (s1, POINTER_TYPE) |
8865 | || !validate_arg (s2, POINTER_TYPE)) | |
8866 | return NULL_TREE; | |
4ee9c684 | 8867 | else |
8868 | { | |
c2f47e15 | 8869 | /* If the first argument is "", return NULL_TREE. */ |
b5e46e2c | 8870 | const char *p1 = c_getstr (s1); |
4ee9c684 | 8871 | if (p1 && *p1 == '\0') |
8872 | { | |
8873 | /* Evaluate and ignore argument s2 in case it has | |
8874 | side-effects. */ | |
389dd41b | 8875 | return omit_one_operand_loc (loc, size_type_node, |
39761420 | 8876 | size_zero_node, s2); |
4ee9c684 | 8877 | } |
8878 | ||
8879 | /* If the second argument is "", return __builtin_strlen(s1). */ | |
b5e46e2c | 8880 | const char *p2 = c_getstr (s2); |
4ee9c684 | 8881 | if (p2 && *p2 == '\0') |
8882 | { | |
b9a16870 | 8883 | tree fn = builtin_decl_implicit (BUILT_IN_STRLEN); |
4ee9c684 | 8884 | |
8885 | /* If the replacement _DECL isn't initialized, don't do the | |
8886 | transformation. */ | |
8887 | if (!fn) | |
c2f47e15 | 8888 | return NULL_TREE; |
4ee9c684 | 8889 | |
389dd41b | 8890 | return build_call_expr_loc (loc, fn, 1, s1); |
4ee9c684 | 8891 | } |
c2f47e15 | 8892 | return NULL_TREE; |
4ee9c684 | 8893 | } |
8894 | } | |
8895 | ||
c2f47e15 | 8896 | /* Fold the next_arg or va_start call EXP. Returns true if there was an error |
743b0c6a | 8897 | produced. False otherwise. This is done so that we don't output the error |
8898 | or warning twice or three times. */ | |
75a70cf9 | 8899 | |
743b0c6a | 8900 | bool |
c2f47e15 | 8901 | fold_builtin_next_arg (tree exp, bool va_start_p) |
4ee9c684 | 8902 | { |
8903 | tree fntype = TREE_TYPE (current_function_decl); | |
c2f47e15 | 8904 | int nargs = call_expr_nargs (exp); |
8905 | tree arg; | |
d98fd4a4 | 8906 | /* There is good chance the current input_location points inside the |
8907 | definition of the va_start macro (perhaps on the token for | |
8908 | builtin) in a system header, so warnings will not be emitted. | |
8909 | Use the location in real source code. */ | |
8910 | source_location current_location = | |
8911 | linemap_unwind_to_first_non_reserved_loc (line_table, input_location, | |
8912 | NULL); | |
4ee9c684 | 8913 | |
257d99c3 | 8914 | if (!stdarg_p (fntype)) |
743b0c6a | 8915 | { |
8916 | error ("%<va_start%> used in function with fixed args"); | |
8917 | return true; | |
8918 | } | |
c2f47e15 | 8919 | |
8920 | if (va_start_p) | |
79012a9d | 8921 | { |
c2f47e15 | 8922 | if (va_start_p && (nargs != 2)) |
8923 | { | |
8924 | error ("wrong number of arguments to function %<va_start%>"); | |
8925 | return true; | |
8926 | } | |
8927 | arg = CALL_EXPR_ARG (exp, 1); | |
79012a9d | 8928 | } |
8929 | /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0) | |
8930 | when we checked the arguments and if needed issued a warning. */ | |
c2f47e15 | 8931 | else |
4ee9c684 | 8932 | { |
c2f47e15 | 8933 | if (nargs == 0) |
8934 | { | |
8935 | /* Evidently an out of date version of <stdarg.h>; can't validate | |
8936 | va_start's second argument, but can still work as intended. */ | |
d98fd4a4 | 8937 | warning_at (current_location, |
7edb1062 | 8938 | OPT_Wvarargs, |
8939 | "%<__builtin_next_arg%> called without an argument"); | |
c2f47e15 | 8940 | return true; |
8941 | } | |
8942 | else if (nargs > 1) | |
a0c938f0 | 8943 | { |
c2f47e15 | 8944 | error ("wrong number of arguments to function %<__builtin_next_arg%>"); |
a0c938f0 | 8945 | return true; |
8946 | } | |
c2f47e15 | 8947 | arg = CALL_EXPR_ARG (exp, 0); |
8948 | } | |
8949 | ||
a8dd994c | 8950 | if (TREE_CODE (arg) == SSA_NAME) |
8951 | arg = SSA_NAME_VAR (arg); | |
8952 | ||
c2f47e15 | 8953 | /* We destructively modify the call to be __builtin_va_start (ap, 0) |
48e1416a | 8954 | or __builtin_next_arg (0) the first time we see it, after checking |
c2f47e15 | 8955 | the arguments and if needed issuing a warning. */ |
8956 | if (!integer_zerop (arg)) | |
8957 | { | |
8958 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
79012a9d | 8959 | |
4ee9c684 | 8960 | /* Strip off all nops for the sake of the comparison. This |
8961 | is not quite the same as STRIP_NOPS. It does more. | |
8962 | We must also strip off INDIRECT_EXPR for C++ reference | |
8963 | parameters. */ | |
72dd6141 | 8964 | while (CONVERT_EXPR_P (arg) |
4ee9c684 | 8965 | || TREE_CODE (arg) == INDIRECT_REF) |
8966 | arg = TREE_OPERAND (arg, 0); | |
8967 | if (arg != last_parm) | |
a0c938f0 | 8968 | { |
b08cf617 | 8969 | /* FIXME: Sometimes with the tree optimizers we can get the |
8970 | not the last argument even though the user used the last | |
8971 | argument. We just warn and set the arg to be the last | |
8972 | argument so that we will get wrong-code because of | |
8973 | it. */ | |
d98fd4a4 | 8974 | warning_at (current_location, |
7edb1062 | 8975 | OPT_Wvarargs, |
d98fd4a4 | 8976 | "second parameter of %<va_start%> not last named argument"); |
743b0c6a | 8977 | } |
24158ad7 | 8978 | |
8979 | /* Undefined by C99 7.15.1.4p4 (va_start): | |
8980 | "If the parameter parmN is declared with the register storage | |
8981 | class, with a function or array type, or with a type that is | |
8982 | not compatible with the type that results after application of | |
8983 | the default argument promotions, the behavior is undefined." | |
8984 | */ | |
8985 | else if (DECL_REGISTER (arg)) | |
d98fd4a4 | 8986 | { |
8987 | warning_at (current_location, | |
7edb1062 | 8988 | OPT_Wvarargs, |
67cf9b55 | 8989 | "undefined behavior when second parameter of " |
d98fd4a4 | 8990 | "%<va_start%> is declared with %<register%> storage"); |
8991 | } | |
24158ad7 | 8992 | |
79012a9d | 8993 | /* We want to verify the second parameter just once before the tree |
a0c938f0 | 8994 | optimizers are run and then avoid keeping it in the tree, |
8995 | as otherwise we could warn even for correct code like: | |
8996 | void foo (int i, ...) | |
8997 | { va_list ap; i++; va_start (ap, i); va_end (ap); } */ | |
c2f47e15 | 8998 | if (va_start_p) |
8999 | CALL_EXPR_ARG (exp, 1) = integer_zero_node; | |
9000 | else | |
9001 | CALL_EXPR_ARG (exp, 0) = integer_zero_node; | |
743b0c6a | 9002 | } |
9003 | return false; | |
4ee9c684 | 9004 | } |
9005 | ||
9006 | ||
c2f47e15 | 9007 | /* Expand a call EXP to __builtin_object_size. */ |
0a39fd54 | 9008 | |
f7715905 | 9009 | static rtx |
0a39fd54 | 9010 | expand_builtin_object_size (tree exp) |
9011 | { | |
9012 | tree ost; | |
9013 | int object_size_type; | |
9014 | tree fndecl = get_callee_fndecl (exp); | |
0a39fd54 | 9015 | |
c2f47e15 | 9016 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
0a39fd54 | 9017 | { |
b8c23db3 | 9018 | error ("%Kfirst argument of %D must be a pointer, second integer constant", |
9019 | exp, fndecl); | |
0a39fd54 | 9020 | expand_builtin_trap (); |
9021 | return const0_rtx; | |
9022 | } | |
9023 | ||
c2f47e15 | 9024 | ost = CALL_EXPR_ARG (exp, 1); |
0a39fd54 | 9025 | STRIP_NOPS (ost); |
9026 | ||
9027 | if (TREE_CODE (ost) != INTEGER_CST | |
9028 | || tree_int_cst_sgn (ost) < 0 | |
9029 | || compare_tree_int (ost, 3) > 0) | |
9030 | { | |
b8c23db3 | 9031 | error ("%Klast argument of %D is not integer constant between 0 and 3", |
9032 | exp, fndecl); | |
0a39fd54 | 9033 | expand_builtin_trap (); |
9034 | return const0_rtx; | |
9035 | } | |
9036 | ||
e913b5cd | 9037 | object_size_type = tree_to_shwi (ost); |
0a39fd54 | 9038 | |
9039 | return object_size_type < 2 ? constm1_rtx : const0_rtx; | |
9040 | } | |
9041 | ||
9042 | /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
9043 | FCODE is the BUILT_IN_* to use. | |
c2f47e15 | 9044 | Return NULL_RTX if we failed; the caller should emit a normal call, |
0a39fd54 | 9045 | otherwise try to get the result in TARGET, if convenient (and in |
9046 | mode MODE if that's convenient). */ | |
9047 | ||
9048 | static rtx | |
3754d046 | 9049 | expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode, |
0a39fd54 | 9050 | enum built_in_function fcode) |
9051 | { | |
0a39fd54 | 9052 | tree dest, src, len, size; |
9053 | ||
c2f47e15 | 9054 | if (!validate_arglist (exp, |
0a39fd54 | 9055 | POINTER_TYPE, |
9056 | fcode == BUILT_IN_MEMSET_CHK | |
9057 | ? INTEGER_TYPE : POINTER_TYPE, | |
9058 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
c2f47e15 | 9059 | return NULL_RTX; |
0a39fd54 | 9060 | |
c2f47e15 | 9061 | dest = CALL_EXPR_ARG (exp, 0); |
9062 | src = CALL_EXPR_ARG (exp, 1); | |
9063 | len = CALL_EXPR_ARG (exp, 2); | |
9064 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 9065 | |
e913b5cd | 9066 | if (! tree_fits_uhwi_p (size)) |
c2f47e15 | 9067 | return NULL_RTX; |
0a39fd54 | 9068 | |
e913b5cd | 9069 | if (tree_fits_uhwi_p (len) || integer_all_onesp (size)) |
0a39fd54 | 9070 | { |
9071 | tree fn; | |
9072 | ||
9073 | if (! integer_all_onesp (size) && tree_int_cst_lt (size, len)) | |
9074 | { | |
b430e8d9 | 9075 | warning_at (tree_nonartificial_location (exp), |
9076 | 0, "%Kcall to %D will always overflow destination buffer", | |
9077 | exp, get_callee_fndecl (exp)); | |
c2f47e15 | 9078 | return NULL_RTX; |
0a39fd54 | 9079 | } |
9080 | ||
0a39fd54 | 9081 | fn = NULL_TREE; |
9082 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
9083 | mem{cpy,pcpy,move,set} is available. */ | |
9084 | switch (fcode) | |
9085 | { | |
9086 | case BUILT_IN_MEMCPY_CHK: | |
b9a16870 | 9087 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); |
0a39fd54 | 9088 | break; |
9089 | case BUILT_IN_MEMPCPY_CHK: | |
b9a16870 | 9090 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); |
0a39fd54 | 9091 | break; |
9092 | case BUILT_IN_MEMMOVE_CHK: | |
b9a16870 | 9093 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); |
0a39fd54 | 9094 | break; |
9095 | case BUILT_IN_MEMSET_CHK: | |
b9a16870 | 9096 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); |
0a39fd54 | 9097 | break; |
9098 | default: | |
9099 | break; | |
9100 | } | |
9101 | ||
9102 | if (! fn) | |
c2f47e15 | 9103 | return NULL_RTX; |
0a39fd54 | 9104 | |
0568e9c1 | 9105 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len); |
a65c4d64 | 9106 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
9107 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
0a39fd54 | 9108 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
9109 | } | |
9110 | else if (fcode == BUILT_IN_MEMSET_CHK) | |
c2f47e15 | 9111 | return NULL_RTX; |
0a39fd54 | 9112 | else |
9113 | { | |
957d0361 | 9114 | unsigned int dest_align = get_pointer_alignment (dest); |
0a39fd54 | 9115 | |
9116 | /* If DEST is not a pointer type, call the normal function. */ | |
9117 | if (dest_align == 0) | |
c2f47e15 | 9118 | return NULL_RTX; |
0a39fd54 | 9119 | |
9120 | /* If SRC and DEST are the same (and not volatile), do nothing. */ | |
9121 | if (operand_equal_p (src, dest, 0)) | |
9122 | { | |
9123 | tree expr; | |
9124 | ||
9125 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
9126 | { | |
9127 | /* Evaluate and ignore LEN in case it has side-effects. */ | |
9128 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
9129 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
9130 | } | |
9131 | ||
2cc66f2a | 9132 | expr = fold_build_pointer_plus (dest, len); |
0a39fd54 | 9133 | return expand_expr (expr, target, mode, EXPAND_NORMAL); |
9134 | } | |
9135 | ||
9136 | /* __memmove_chk special case. */ | |
9137 | if (fcode == BUILT_IN_MEMMOVE_CHK) | |
9138 | { | |
957d0361 | 9139 | unsigned int src_align = get_pointer_alignment (src); |
0a39fd54 | 9140 | |
9141 | if (src_align == 0) | |
c2f47e15 | 9142 | return NULL_RTX; |
0a39fd54 | 9143 | |
9144 | /* If src is categorized for a readonly section we can use | |
9145 | normal __memcpy_chk. */ | |
9146 | if (readonly_data_expr (src)) | |
9147 | { | |
b9a16870 | 9148 | tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); |
0a39fd54 | 9149 | if (!fn) |
c2f47e15 | 9150 | return NULL_RTX; |
0568e9c1 | 9151 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4, |
9152 | dest, src, len, size); | |
a65c4d64 | 9153 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
9154 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
0a39fd54 | 9155 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
9156 | } | |
9157 | } | |
c2f47e15 | 9158 | return NULL_RTX; |
0a39fd54 | 9159 | } |
9160 | } | |
9161 | ||
9162 | /* Emit warning if a buffer overflow is detected at compile time. */ | |
9163 | ||
9164 | static void | |
9165 | maybe_emit_chk_warning (tree exp, enum built_in_function fcode) | |
9166 | { | |
c2f47e15 | 9167 | int is_strlen = 0; |
0a39fd54 | 9168 | tree len, size; |
b430e8d9 | 9169 | location_t loc = tree_nonartificial_location (exp); |
0a39fd54 | 9170 | |
9171 | switch (fcode) | |
9172 | { | |
9173 | case BUILT_IN_STRCPY_CHK: | |
9174 | case BUILT_IN_STPCPY_CHK: | |
9175 | /* For __strcat_chk the warning will be emitted only if overflowing | |
9176 | by at least strlen (dest) + 1 bytes. */ | |
9177 | case BUILT_IN_STRCAT_CHK: | |
c2f47e15 | 9178 | len = CALL_EXPR_ARG (exp, 1); |
9179 | size = CALL_EXPR_ARG (exp, 2); | |
0a39fd54 | 9180 | is_strlen = 1; |
9181 | break; | |
b356dfef | 9182 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 9183 | case BUILT_IN_STRNCPY_CHK: |
1063acde | 9184 | case BUILT_IN_STPNCPY_CHK: |
c2f47e15 | 9185 | len = CALL_EXPR_ARG (exp, 2); |
9186 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 9187 | break; |
9188 | case BUILT_IN_SNPRINTF_CHK: | |
9189 | case BUILT_IN_VSNPRINTF_CHK: | |
c2f47e15 | 9190 | len = CALL_EXPR_ARG (exp, 1); |
9191 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 9192 | break; |
9193 | default: | |
9194 | gcc_unreachable (); | |
9195 | } | |
9196 | ||
0a39fd54 | 9197 | if (!len || !size) |
9198 | return; | |
9199 | ||
e913b5cd | 9200 | if (! tree_fits_uhwi_p (size) || integer_all_onesp (size)) |
0a39fd54 | 9201 | return; |
9202 | ||
9203 | if (is_strlen) | |
9204 | { | |
9205 | len = c_strlen (len, 1); | |
e913b5cd | 9206 | if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size)) |
0a39fd54 | 9207 | return; |
9208 | } | |
b356dfef | 9209 | else if (fcode == BUILT_IN_STRNCAT_CHK) |
9210 | { | |
c2f47e15 | 9211 | tree src = CALL_EXPR_ARG (exp, 1); |
e913b5cd | 9212 | if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size)) |
b356dfef | 9213 | return; |
9214 | src = c_strlen (src, 1); | |
e913b5cd | 9215 | if (! src || ! tree_fits_uhwi_p (src)) |
b356dfef | 9216 | { |
b430e8d9 | 9217 | warning_at (loc, 0, "%Kcall to %D might overflow destination buffer", |
9218 | exp, get_callee_fndecl (exp)); | |
b356dfef | 9219 | return; |
9220 | } | |
9221 | else if (tree_int_cst_lt (src, size)) | |
9222 | return; | |
9223 | } | |
e913b5cd | 9224 | else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len)) |
0a39fd54 | 9225 | return; |
9226 | ||
b430e8d9 | 9227 | warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer", |
9228 | exp, get_callee_fndecl (exp)); | |
0a39fd54 | 9229 | } |
9230 | ||
9231 | /* Emit warning if a buffer overflow is detected at compile time | |
9232 | in __sprintf_chk/__vsprintf_chk calls. */ | |
9233 | ||
9234 | static void | |
9235 | maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) | |
9236 | { | |
1e4adcfc | 9237 | tree size, len, fmt; |
0a39fd54 | 9238 | const char *fmt_str; |
c2f47e15 | 9239 | int nargs = call_expr_nargs (exp); |
0a39fd54 | 9240 | |
9241 | /* Verify the required arguments in the original call. */ | |
48e1416a | 9242 | |
c2f47e15 | 9243 | if (nargs < 4) |
0a39fd54 | 9244 | return; |
c2f47e15 | 9245 | size = CALL_EXPR_ARG (exp, 2); |
9246 | fmt = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 9247 | |
e913b5cd | 9248 | if (! tree_fits_uhwi_p (size) || integer_all_onesp (size)) |
0a39fd54 | 9249 | return; |
9250 | ||
9251 | /* Check whether the format is a literal string constant. */ | |
9252 | fmt_str = c_getstr (fmt); | |
9253 | if (fmt_str == NULL) | |
9254 | return; | |
9255 | ||
d4473c84 | 9256 | if (!init_target_chars ()) |
99eabcc1 | 9257 | return; |
9258 | ||
0a39fd54 | 9259 | /* If the format doesn't contain % args or %%, we know its size. */ |
99eabcc1 | 9260 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 9261 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
9262 | /* If the format is "%s" and first ... argument is a string literal, | |
9263 | we know it too. */ | |
c2f47e15 | 9264 | else if (fcode == BUILT_IN_SPRINTF_CHK |
9265 | && strcmp (fmt_str, target_percent_s) == 0) | |
0a39fd54 | 9266 | { |
9267 | tree arg; | |
9268 | ||
c2f47e15 | 9269 | if (nargs < 5) |
0a39fd54 | 9270 | return; |
c2f47e15 | 9271 | arg = CALL_EXPR_ARG (exp, 4); |
0a39fd54 | 9272 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
9273 | return; | |
9274 | ||
9275 | len = c_strlen (arg, 1); | |
e913b5cd | 9276 | if (!len || ! tree_fits_uhwi_p (len)) |
0a39fd54 | 9277 | return; |
9278 | } | |
9279 | else | |
9280 | return; | |
9281 | ||
9282 | if (! tree_int_cst_lt (len, size)) | |
b430e8d9 | 9283 | warning_at (tree_nonartificial_location (exp), |
9284 | 0, "%Kcall to %D will always overflow destination buffer", | |
9285 | exp, get_callee_fndecl (exp)); | |
0a39fd54 | 9286 | } |
9287 | ||
2c281b15 | 9288 | /* Emit warning if a free is called with address of a variable. */ |
9289 | ||
9290 | static void | |
9291 | maybe_emit_free_warning (tree exp) | |
9292 | { | |
9293 | tree arg = CALL_EXPR_ARG (exp, 0); | |
9294 | ||
9295 | STRIP_NOPS (arg); | |
9296 | if (TREE_CODE (arg) != ADDR_EXPR) | |
9297 | return; | |
9298 | ||
9299 | arg = get_base_address (TREE_OPERAND (arg, 0)); | |
182cf5a9 | 9300 | if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF) |
2c281b15 | 9301 | return; |
9302 | ||
9303 | if (SSA_VAR_P (arg)) | |
f74ea1c2 | 9304 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
9305 | "%Kattempt to free a non-heap object %qD", exp, arg); | |
2c281b15 | 9306 | else |
f74ea1c2 | 9307 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
9308 | "%Kattempt to free a non-heap object", exp); | |
2c281b15 | 9309 | } |
9310 | ||
c2f47e15 | 9311 | /* Fold a call to __builtin_object_size with arguments PTR and OST, |
9312 | if possible. */ | |
0a39fd54 | 9313 | |
f7715905 | 9314 | static tree |
c2f47e15 | 9315 | fold_builtin_object_size (tree ptr, tree ost) |
0a39fd54 | 9316 | { |
a6caa15f | 9317 | unsigned HOST_WIDE_INT bytes; |
0a39fd54 | 9318 | int object_size_type; |
9319 | ||
c2f47e15 | 9320 | if (!validate_arg (ptr, POINTER_TYPE) |
9321 | || !validate_arg (ost, INTEGER_TYPE)) | |
9322 | return NULL_TREE; | |
0a39fd54 | 9323 | |
0a39fd54 | 9324 | STRIP_NOPS (ost); |
9325 | ||
9326 | if (TREE_CODE (ost) != INTEGER_CST | |
9327 | || tree_int_cst_sgn (ost) < 0 | |
9328 | || compare_tree_int (ost, 3) > 0) | |
c2f47e15 | 9329 | return NULL_TREE; |
0a39fd54 | 9330 | |
e913b5cd | 9331 | object_size_type = tree_to_shwi (ost); |
0a39fd54 | 9332 | |
9333 | /* __builtin_object_size doesn't evaluate side-effects in its arguments; | |
9334 | if there are any side-effects, it returns (size_t) -1 for types 0 and 1 | |
9335 | and (size_t) 0 for types 2 and 3. */ | |
9336 | if (TREE_SIDE_EFFECTS (ptr)) | |
697bbc3f | 9337 | return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0); |
0a39fd54 | 9338 | |
9339 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
a6caa15f | 9340 | { |
6da74b21 | 9341 | bytes = compute_builtin_object_size (ptr, object_size_type); |
9342 | if (wi::fits_to_tree_p (bytes, size_type_node)) | |
9343 | return build_int_cstu (size_type_node, bytes); | |
a6caa15f | 9344 | } |
0a39fd54 | 9345 | else if (TREE_CODE (ptr) == SSA_NAME) |
9346 | { | |
0a39fd54 | 9347 | /* If object size is not known yet, delay folding until |
9348 | later. Maybe subsequent passes will help determining | |
9349 | it. */ | |
9350 | bytes = compute_builtin_object_size (ptr, object_size_type); | |
a6caa15f | 9351 | if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0) |
6da74b21 | 9352 | && wi::fits_to_tree_p (bytes, size_type_node)) |
9353 | return build_int_cstu (size_type_node, bytes); | |
0a39fd54 | 9354 | } |
9355 | ||
a6caa15f | 9356 | return NULL_TREE; |
0a39fd54 | 9357 | } |
9358 | ||
0e80b01d | 9359 | /* Builtins with folding operations that operate on "..." arguments |
9360 | need special handling; we need to store the arguments in a convenient | |
9361 | data structure before attempting any folding. Fortunately there are | |
9362 | only a few builtins that fall into this category. FNDECL is the | |
e80cc485 | 9363 | function, EXP is the CALL_EXPR for the call. */ |
0e80b01d | 9364 | |
9365 | static tree | |
e80cc485 | 9366 | fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs) |
0e80b01d | 9367 | { |
9368 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
9369 | tree ret = NULL_TREE; | |
9370 | ||
9371 | switch (fcode) | |
9372 | { | |
0e80b01d | 9373 | case BUILT_IN_FPCLASSIFY: |
9d884767 | 9374 | ret = fold_builtin_fpclassify (loc, args, nargs); |
0e80b01d | 9375 | break; |
9376 | ||
9377 | default: | |
9378 | break; | |
9379 | } | |
9380 | if (ret) | |
9381 | { | |
9382 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
9383 | SET_EXPR_LOCATION (ret, loc); | |
9384 | TREE_NO_WARNING (ret) = 1; | |
9385 | return ret; | |
9386 | } | |
9387 | return NULL_TREE; | |
9388 | } | |
9389 | ||
99eabcc1 | 9390 | /* Initialize format string characters in the target charset. */ |
9391 | ||
b9ea678c | 9392 | bool |
99eabcc1 | 9393 | init_target_chars (void) |
9394 | { | |
9395 | static bool init; | |
9396 | if (!init) | |
9397 | { | |
9398 | target_newline = lang_hooks.to_target_charset ('\n'); | |
9399 | target_percent = lang_hooks.to_target_charset ('%'); | |
9400 | target_c = lang_hooks.to_target_charset ('c'); | |
9401 | target_s = lang_hooks.to_target_charset ('s'); | |
9402 | if (target_newline == 0 || target_percent == 0 || target_c == 0 | |
9403 | || target_s == 0) | |
9404 | return false; | |
9405 | ||
9406 | target_percent_c[0] = target_percent; | |
9407 | target_percent_c[1] = target_c; | |
9408 | target_percent_c[2] = '\0'; | |
9409 | ||
9410 | target_percent_s[0] = target_percent; | |
9411 | target_percent_s[1] = target_s; | |
9412 | target_percent_s[2] = '\0'; | |
9413 | ||
9414 | target_percent_s_newline[0] = target_percent; | |
9415 | target_percent_s_newline[1] = target_s; | |
9416 | target_percent_s_newline[2] = target_newline; | |
9417 | target_percent_s_newline[3] = '\0'; | |
a0c938f0 | 9418 | |
99eabcc1 | 9419 | init = true; |
9420 | } | |
9421 | return true; | |
9422 | } | |
bffb7645 | 9423 | |
f0c477f2 | 9424 | /* Helper function for do_mpfr_arg*(). Ensure M is a normal number |
9425 | and no overflow/underflow occurred. INEXACT is true if M was not | |
fa7637bd | 9426 | exactly calculated. TYPE is the tree type for the result. This |
f0c477f2 | 9427 | function assumes that you cleared the MPFR flags and then |
9428 | calculated M to see if anything subsequently set a flag prior to | |
9429 | entering this function. Return NULL_TREE if any checks fail. */ | |
9430 | ||
9431 | static tree | |
d4473c84 | 9432 | do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact) |
f0c477f2 | 9433 | { |
9434 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
9435 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
9436 | result of calling FUNC was exact. */ | |
d4473c84 | 9437 | if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p () |
f0c477f2 | 9438 | && (!flag_rounding_math || !inexact)) |
9439 | { | |
9440 | REAL_VALUE_TYPE rr; | |
9441 | ||
66fa16e6 | 9442 | real_from_mpfr (&rr, m, type, GMP_RNDN); |
f0c477f2 | 9443 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value, |
9444 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
9445 | but the mpft_t is not, then we underflowed in the | |
9446 | conversion. */ | |
776a7bab | 9447 | if (real_isfinite (&rr) |
f0c477f2 | 9448 | && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0)) |
9449 | { | |
9450 | REAL_VALUE_TYPE rmode; | |
9451 | ||
9452 | real_convert (&rmode, TYPE_MODE (type), &rr); | |
9453 | /* Proceed iff the specified mode can hold the value. */ | |
9454 | if (real_identical (&rmode, &rr)) | |
9455 | return build_real (type, rmode); | |
9456 | } | |
9457 | } | |
9458 | return NULL_TREE; | |
9459 | } | |
9460 | ||
239d491a | 9461 | /* Helper function for do_mpc_arg*(). Ensure M is a normal complex |
9462 | number and no overflow/underflow occurred. INEXACT is true if M | |
9463 | was not exactly calculated. TYPE is the tree type for the result. | |
9464 | This function assumes that you cleared the MPFR flags and then | |
9465 | calculated M to see if anything subsequently set a flag prior to | |
652d9409 | 9466 | entering this function. Return NULL_TREE if any checks fail, if |
9467 | FORCE_CONVERT is true, then bypass the checks. */ | |
239d491a | 9468 | |
9469 | static tree | |
652d9409 | 9470 | do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert) |
239d491a | 9471 | { |
9472 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
9473 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
9474 | result of calling FUNC was exact. */ | |
652d9409 | 9475 | if (force_convert |
9476 | || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m)) | |
9477 | && !mpfr_overflow_p () && !mpfr_underflow_p () | |
9478 | && (!flag_rounding_math || !inexact))) | |
239d491a | 9479 | { |
9480 | REAL_VALUE_TYPE re, im; | |
9481 | ||
b0e7c4d4 | 9482 | real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN); |
9483 | real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN); | |
239d491a | 9484 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values, |
9485 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
9486 | but the mpft_t is not, then we underflowed in the | |
9487 | conversion. */ | |
652d9409 | 9488 | if (force_convert |
9489 | || (real_isfinite (&re) && real_isfinite (&im) | |
9490 | && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0) | |
9491 | && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0))) | |
239d491a | 9492 | { |
9493 | REAL_VALUE_TYPE re_mode, im_mode; | |
9494 | ||
9495 | real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re); | |
9496 | real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im); | |
9497 | /* Proceed iff the specified mode can hold the value. */ | |
652d9409 | 9498 | if (force_convert |
9499 | || (real_identical (&re_mode, &re) | |
9500 | && real_identical (&im_mode, &im))) | |
239d491a | 9501 | return build_complex (type, build_real (TREE_TYPE (type), re_mode), |
9502 | build_real (TREE_TYPE (type), im_mode)); | |
9503 | } | |
9504 | } | |
9505 | return NULL_TREE; | |
9506 | } | |
239d491a | 9507 | |
e5407ca6 | 9508 | /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set |
9509 | the pointer *(ARG_QUO) and return the result. The type is taken | |
9510 | from the type of ARG0 and is used for setting the precision of the | |
9511 | calculation and results. */ | |
9512 | ||
9513 | static tree | |
9514 | do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo) | |
9515 | { | |
9516 | tree const type = TREE_TYPE (arg0); | |
9517 | tree result = NULL_TREE; | |
48e1416a | 9518 | |
e5407ca6 | 9519 | STRIP_NOPS (arg0); |
9520 | STRIP_NOPS (arg1); | |
48e1416a | 9521 | |
e5407ca6 | 9522 | /* To proceed, MPFR must exactly represent the target floating point |
9523 | format, which only happens when the target base equals two. */ | |
9524 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
9525 | && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0) | |
9526 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)) | |
9527 | { | |
9528 | const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0); | |
9529 | const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1); | |
9530 | ||
776a7bab | 9531 | if (real_isfinite (ra0) && real_isfinite (ra1)) |
e5407ca6 | 9532 | { |
e2eb2b7f | 9533 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
9534 | const int prec = fmt->p; | |
9535 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
e5407ca6 | 9536 | tree result_rem; |
9537 | long integer_quo; | |
9538 | mpfr_t m0, m1; | |
9539 | ||
9540 | mpfr_inits2 (prec, m0, m1, NULL); | |
9541 | mpfr_from_real (m0, ra0, GMP_RNDN); | |
9542 | mpfr_from_real (m1, ra1, GMP_RNDN); | |
9543 | mpfr_clear_flags (); | |
e2eb2b7f | 9544 | mpfr_remquo (m0, &integer_quo, m0, m1, rnd); |
e5407ca6 | 9545 | /* Remquo is independent of the rounding mode, so pass |
9546 | inexact=0 to do_mpfr_ckconv(). */ | |
9547 | result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0); | |
9548 | mpfr_clears (m0, m1, NULL); | |
9549 | if (result_rem) | |
9550 | { | |
9551 | /* MPFR calculates quo in the host's long so it may | |
9552 | return more bits in quo than the target int can hold | |
9553 | if sizeof(host long) > sizeof(target int). This can | |
9554 | happen even for native compilers in LP64 mode. In | |
9555 | these cases, modulo the quo value with the largest | |
9556 | number that the target int can hold while leaving one | |
9557 | bit for the sign. */ | |
9558 | if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE) | |
9559 | integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1)); | |
9560 | ||
9561 | /* Dereference the quo pointer argument. */ | |
9562 | arg_quo = build_fold_indirect_ref (arg_quo); | |
9563 | /* Proceed iff a valid pointer type was passed in. */ | |
9564 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node) | |
9565 | { | |
9566 | /* Set the value. */ | |
7002a1c8 | 9567 | tree result_quo |
9568 | = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo, | |
9569 | build_int_cst (TREE_TYPE (arg_quo), | |
9570 | integer_quo)); | |
e5407ca6 | 9571 | TREE_SIDE_EFFECTS (result_quo) = 1; |
9572 | /* Combine the quo assignment with the rem. */ | |
9573 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
9574 | result_quo, result_rem)); | |
9575 | } | |
9576 | } | |
9577 | } | |
9578 | } | |
9579 | return result; | |
9580 | } | |
e84da7c1 | 9581 | |
9582 | /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the | |
9583 | resulting value as a tree with type TYPE. The mpfr precision is | |
9584 | set to the precision of TYPE. We assume that this mpfr function | |
9585 | returns zero if the result could be calculated exactly within the | |
9586 | requested precision. In addition, the integer pointer represented | |
9587 | by ARG_SG will be dereferenced and set to the appropriate signgam | |
9588 | (-1,1) value. */ | |
9589 | ||
9590 | static tree | |
9591 | do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type) | |
9592 | { | |
9593 | tree result = NULL_TREE; | |
9594 | ||
9595 | STRIP_NOPS (arg); | |
48e1416a | 9596 | |
e84da7c1 | 9597 | /* To proceed, MPFR must exactly represent the target floating point |
9598 | format, which only happens when the target base equals two. Also | |
9599 | verify ARG is a constant and that ARG_SG is an int pointer. */ | |
9600 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
9601 | && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg) | |
9602 | && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE | |
9603 | && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node) | |
9604 | { | |
9605 | const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg); | |
9606 | ||
9607 | /* In addition to NaN and Inf, the argument cannot be zero or a | |
9608 | negative integer. */ | |
776a7bab | 9609 | if (real_isfinite (ra) |
e84da7c1 | 9610 | && ra->cl != rvc_zero |
9af5ce0c | 9611 | && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type)))) |
e84da7c1 | 9612 | { |
e2eb2b7f | 9613 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
9614 | const int prec = fmt->p; | |
9615 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
e84da7c1 | 9616 | int inexact, sg; |
9617 | mpfr_t m; | |
9618 | tree result_lg; | |
9619 | ||
9620 | mpfr_init2 (m, prec); | |
9621 | mpfr_from_real (m, ra, GMP_RNDN); | |
9622 | mpfr_clear_flags (); | |
e2eb2b7f | 9623 | inexact = mpfr_lgamma (m, &sg, m, rnd); |
e84da7c1 | 9624 | result_lg = do_mpfr_ckconv (m, type, inexact); |
9625 | mpfr_clear (m); | |
9626 | if (result_lg) | |
9627 | { | |
9628 | tree result_sg; | |
9629 | ||
9630 | /* Dereference the arg_sg pointer argument. */ | |
9631 | arg_sg = build_fold_indirect_ref (arg_sg); | |
9632 | /* Assign the signgam value into *arg_sg. */ | |
9633 | result_sg = fold_build2 (MODIFY_EXPR, | |
9634 | TREE_TYPE (arg_sg), arg_sg, | |
7002a1c8 | 9635 | build_int_cst (TREE_TYPE (arg_sg), sg)); |
e84da7c1 | 9636 | TREE_SIDE_EFFECTS (result_sg) = 1; |
9637 | /* Combine the signgam assignment with the lgamma result. */ | |
9638 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
9639 | result_sg, result_lg)); | |
9640 | } | |
9641 | } | |
9642 | } | |
9643 | ||
9644 | return result; | |
9645 | } | |
75a70cf9 | 9646 | |
c699fab8 | 9647 | /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument |
9648 | mpc function FUNC on it and return the resulting value as a tree | |
9649 | with type TYPE. The mpfr precision is set to the precision of | |
9650 | TYPE. We assume that function FUNC returns zero if the result | |
652d9409 | 9651 | could be calculated exactly within the requested precision. If |
9652 | DO_NONFINITE is true, then fold expressions containing Inf or NaN | |
9653 | in the arguments and/or results. */ | |
c699fab8 | 9654 | |
63e89698 | 9655 | tree |
652d9409 | 9656 | do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite, |
c699fab8 | 9657 | int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t)) |
9658 | { | |
9659 | tree result = NULL_TREE; | |
48e1416a | 9660 | |
c699fab8 | 9661 | STRIP_NOPS (arg0); |
9662 | STRIP_NOPS (arg1); | |
9663 | ||
9664 | /* To proceed, MPFR must exactly represent the target floating point | |
9665 | format, which only happens when the target base equals two. */ | |
9666 | if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0) | |
9667 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE | |
9668 | && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1) | |
9669 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE | |
9670 | && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2) | |
9671 | { | |
9672 | const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0)); | |
9673 | const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0)); | |
9674 | const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1)); | |
9675 | const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1)); | |
9676 | ||
652d9409 | 9677 | if (do_nonfinite |
9678 | || (real_isfinite (re0) && real_isfinite (im0) | |
9679 | && real_isfinite (re1) && real_isfinite (im1))) | |
c699fab8 | 9680 | { |
9681 | const struct real_format *const fmt = | |
9682 | REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type))); | |
9683 | const int prec = fmt->p; | |
9684 | const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN; | |
9685 | const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN; | |
9686 | int inexact; | |
9687 | mpc_t m0, m1; | |
48e1416a | 9688 | |
c699fab8 | 9689 | mpc_init2 (m0, prec); |
9690 | mpc_init2 (m1, prec); | |
9af5ce0c | 9691 | mpfr_from_real (mpc_realref (m0), re0, rnd); |
9692 | mpfr_from_real (mpc_imagref (m0), im0, rnd); | |
9693 | mpfr_from_real (mpc_realref (m1), re1, rnd); | |
9694 | mpfr_from_real (mpc_imagref (m1), im1, rnd); | |
c699fab8 | 9695 | mpfr_clear_flags (); |
9696 | inexact = func (m0, m0, m1, crnd); | |
652d9409 | 9697 | result = do_mpc_ckconv (m0, type, inexact, do_nonfinite); |
c699fab8 | 9698 | mpc_clear (m0); |
9699 | mpc_clear (m1); | |
9700 | } | |
9701 | } | |
9702 | ||
9703 | return result; | |
9704 | } | |
239d491a | 9705 | |
75a70cf9 | 9706 | /* A wrapper function for builtin folding that prevents warnings for |
9707 | "statement without effect" and the like, caused by removing the | |
9708 | call node earlier than the warning is generated. */ | |
9709 | ||
9710 | tree | |
1a91d914 | 9711 | fold_call_stmt (gcall *stmt, bool ignore) |
75a70cf9 | 9712 | { |
9713 | tree ret = NULL_TREE; | |
9714 | tree fndecl = gimple_call_fndecl (stmt); | |
389dd41b | 9715 | location_t loc = gimple_location (stmt); |
75a70cf9 | 9716 | if (fndecl |
9717 | && TREE_CODE (fndecl) == FUNCTION_DECL | |
9718 | && DECL_BUILT_IN (fndecl) | |
9719 | && !gimple_call_va_arg_pack_p (stmt)) | |
9720 | { | |
9721 | int nargs = gimple_call_num_args (stmt); | |
9845fb99 | 9722 | tree *args = (nargs > 0 |
9723 | ? gimple_call_arg_ptr (stmt, 0) | |
9724 | : &error_mark_node); | |
75a70cf9 | 9725 | |
198622c0 | 9726 | if (avoid_folding_inline_builtin (fndecl)) |
9727 | return NULL_TREE; | |
75a70cf9 | 9728 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
9729 | { | |
9845fb99 | 9730 | return targetm.fold_builtin (fndecl, nargs, args, ignore); |
75a70cf9 | 9731 | } |
9732 | else | |
9733 | { | |
9d884767 | 9734 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); |
75a70cf9 | 9735 | if (ret) |
9736 | { | |
9737 | /* Propagate location information from original call to | |
9738 | expansion of builtin. Otherwise things like | |
9739 | maybe_emit_chk_warning, that operate on the expansion | |
9740 | of a builtin, will use the wrong location information. */ | |
9741 | if (gimple_has_location (stmt)) | |
9742 | { | |
9743 | tree realret = ret; | |
9744 | if (TREE_CODE (ret) == NOP_EXPR) | |
9745 | realret = TREE_OPERAND (ret, 0); | |
9746 | if (CAN_HAVE_LOCATION_P (realret) | |
9747 | && !EXPR_HAS_LOCATION (realret)) | |
389dd41b | 9748 | SET_EXPR_LOCATION (realret, loc); |
75a70cf9 | 9749 | return realret; |
9750 | } | |
9751 | return ret; | |
9752 | } | |
9753 | } | |
9754 | } | |
9755 | return NULL_TREE; | |
9756 | } | |
7bfefa9d | 9757 | |
b9a16870 | 9758 | /* Look up the function in builtin_decl that corresponds to DECL |
7bfefa9d | 9759 | and set ASMSPEC as its user assembler name. DECL must be a |
9760 | function decl that declares a builtin. */ | |
9761 | ||
9762 | void | |
9763 | set_builtin_user_assembler_name (tree decl, const char *asmspec) | |
9764 | { | |
9765 | tree builtin; | |
9766 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL | |
9767 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL | |
9768 | && asmspec != 0); | |
9769 | ||
b9a16870 | 9770 | builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl)); |
4d8e0d6d | 9771 | set_user_assembler_name (builtin, asmspec); |
7bfefa9d | 9772 | switch (DECL_FUNCTION_CODE (decl)) |
9773 | { | |
9774 | case BUILT_IN_MEMCPY: | |
9775 | init_block_move_fn (asmspec); | |
9776 | memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec); | |
9777 | break; | |
9778 | case BUILT_IN_MEMSET: | |
9779 | init_block_clear_fn (asmspec); | |
9780 | memset_libfunc = set_user_assembler_libfunc ("memset", asmspec); | |
9781 | break; | |
9782 | case BUILT_IN_MEMMOVE: | |
9783 | memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec); | |
9784 | break; | |
9785 | case BUILT_IN_MEMCMP: | |
9786 | memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec); | |
9787 | break; | |
9788 | case BUILT_IN_ABORT: | |
9789 | abort_libfunc = set_user_assembler_libfunc ("abort", asmspec); | |
9790 | break; | |
5a80a58b | 9791 | case BUILT_IN_FFS: |
9792 | if (INT_TYPE_SIZE < BITS_PER_WORD) | |
9793 | { | |
9794 | set_user_assembler_libfunc ("ffs", asmspec); | |
9795 | set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, | |
9796 | MODE_INT, 0), "ffs"); | |
9797 | } | |
9798 | break; | |
7bfefa9d | 9799 | default: |
9800 | break; | |
9801 | } | |
9802 | } | |
a6b74a67 | 9803 | |
9804 | /* Return true if DECL is a builtin that expands to a constant or similarly | |
9805 | simple code. */ | |
9806 | bool | |
9807 | is_simple_builtin (tree decl) | |
9808 | { | |
9809 | if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
9810 | switch (DECL_FUNCTION_CODE (decl)) | |
9811 | { | |
9812 | /* Builtins that expand to constants. */ | |
9813 | case BUILT_IN_CONSTANT_P: | |
9814 | case BUILT_IN_EXPECT: | |
9815 | case BUILT_IN_OBJECT_SIZE: | |
9816 | case BUILT_IN_UNREACHABLE: | |
9817 | /* Simple register moves or loads from stack. */ | |
fca0886c | 9818 | case BUILT_IN_ASSUME_ALIGNED: |
a6b74a67 | 9819 | case BUILT_IN_RETURN_ADDRESS: |
9820 | case BUILT_IN_EXTRACT_RETURN_ADDR: | |
9821 | case BUILT_IN_FROB_RETURN_ADDR: | |
9822 | case BUILT_IN_RETURN: | |
9823 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
9824 | case BUILT_IN_FRAME_ADDRESS: | |
9825 | case BUILT_IN_VA_END: | |
9826 | case BUILT_IN_STACK_SAVE: | |
9827 | case BUILT_IN_STACK_RESTORE: | |
9828 | /* Exception state returns or moves registers around. */ | |
9829 | case BUILT_IN_EH_FILTER: | |
9830 | case BUILT_IN_EH_POINTER: | |
9831 | case BUILT_IN_EH_COPY_VALUES: | |
9832 | return true; | |
9833 | ||
9834 | default: | |
9835 | return false; | |
9836 | } | |
9837 | ||
9838 | return false; | |
9839 | } | |
9840 | ||
9841 | /* Return true if DECL is a builtin that is not expensive, i.e., they are | |
9842 | most probably expanded inline into reasonably simple code. This is a | |
9843 | superset of is_simple_builtin. */ | |
9844 | bool | |
9845 | is_inexpensive_builtin (tree decl) | |
9846 | { | |
9847 | if (!decl) | |
9848 | return false; | |
9849 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD) | |
9850 | return true; | |
9851 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
9852 | switch (DECL_FUNCTION_CODE (decl)) | |
9853 | { | |
9854 | case BUILT_IN_ABS: | |
9855 | case BUILT_IN_ALLOCA: | |
581bf1c2 | 9856 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
74bdbe96 | 9857 | case BUILT_IN_BSWAP16: |
a6b74a67 | 9858 | case BUILT_IN_BSWAP32: |
9859 | case BUILT_IN_BSWAP64: | |
9860 | case BUILT_IN_CLZ: | |
9861 | case BUILT_IN_CLZIMAX: | |
9862 | case BUILT_IN_CLZL: | |
9863 | case BUILT_IN_CLZLL: | |
9864 | case BUILT_IN_CTZ: | |
9865 | case BUILT_IN_CTZIMAX: | |
9866 | case BUILT_IN_CTZL: | |
9867 | case BUILT_IN_CTZLL: | |
9868 | case BUILT_IN_FFS: | |
9869 | case BUILT_IN_FFSIMAX: | |
9870 | case BUILT_IN_FFSL: | |
9871 | case BUILT_IN_FFSLL: | |
9872 | case BUILT_IN_IMAXABS: | |
9873 | case BUILT_IN_FINITE: | |
9874 | case BUILT_IN_FINITEF: | |
9875 | case BUILT_IN_FINITEL: | |
9876 | case BUILT_IN_FINITED32: | |
9877 | case BUILT_IN_FINITED64: | |
9878 | case BUILT_IN_FINITED128: | |
9879 | case BUILT_IN_FPCLASSIFY: | |
9880 | case BUILT_IN_ISFINITE: | |
9881 | case BUILT_IN_ISINF_SIGN: | |
9882 | case BUILT_IN_ISINF: | |
9883 | case BUILT_IN_ISINFF: | |
9884 | case BUILT_IN_ISINFL: | |
9885 | case BUILT_IN_ISINFD32: | |
9886 | case BUILT_IN_ISINFD64: | |
9887 | case BUILT_IN_ISINFD128: | |
9888 | case BUILT_IN_ISNAN: | |
9889 | case BUILT_IN_ISNANF: | |
9890 | case BUILT_IN_ISNANL: | |
9891 | case BUILT_IN_ISNAND32: | |
9892 | case BUILT_IN_ISNAND64: | |
9893 | case BUILT_IN_ISNAND128: | |
9894 | case BUILT_IN_ISNORMAL: | |
9895 | case BUILT_IN_ISGREATER: | |
9896 | case BUILT_IN_ISGREATEREQUAL: | |
9897 | case BUILT_IN_ISLESS: | |
9898 | case BUILT_IN_ISLESSEQUAL: | |
9899 | case BUILT_IN_ISLESSGREATER: | |
9900 | case BUILT_IN_ISUNORDERED: | |
9901 | case BUILT_IN_VA_ARG_PACK: | |
9902 | case BUILT_IN_VA_ARG_PACK_LEN: | |
9903 | case BUILT_IN_VA_COPY: | |
9904 | case BUILT_IN_TRAP: | |
9905 | case BUILT_IN_SAVEREGS: | |
9906 | case BUILT_IN_POPCOUNTL: | |
9907 | case BUILT_IN_POPCOUNTLL: | |
9908 | case BUILT_IN_POPCOUNTIMAX: | |
9909 | case BUILT_IN_POPCOUNT: | |
9910 | case BUILT_IN_PARITYL: | |
9911 | case BUILT_IN_PARITYLL: | |
9912 | case BUILT_IN_PARITYIMAX: | |
9913 | case BUILT_IN_PARITY: | |
9914 | case BUILT_IN_LABS: | |
9915 | case BUILT_IN_LLABS: | |
9916 | case BUILT_IN_PREFETCH: | |
ca4c3545 | 9917 | case BUILT_IN_ACC_ON_DEVICE: |
a6b74a67 | 9918 | return true; |
9919 | ||
9920 | default: | |
9921 | return is_simple_builtin (decl); | |
9922 | } | |
9923 | ||
9924 | return false; | |
9925 | } |